summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
blob: e6474550a284a35f5ec8bdbd37e13d084e388b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "../../../include/fpdfapi/fpdf_parser.h"
const char PDF_CharType[256] = {
  //NUL  SOH  STX  ETX  EOT  ENQ  ACK  BEL  BS   HT   LF   VT   FF   CR   SO   SI
    'W', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'W', 'W', 'R', 'W', 'W', 'R', 'R',

  //DLE  DC1  DC2  DC3  DC4  NAK  SYN  ETB  CAN  EM   SUB  ESC  FS   GS   RS   US
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',

  //SP    !    "    #    $    %    &    ´    (    )    *    +    ,    -    .    /
    'W', 'R', 'R', 'R', 'R', 'D', 'R', 'R', 'D', 'D', 'R', 'N', 'R', 'N', 'N', 'D',

  // 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ?
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'R', 'R', 'D', 'R', 'D', 'R',

  // @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',

  // P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'D', 'R', 'D', 'R', 'R',

  // `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',

  // p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~   DEL
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'D', 'R', 'D', 'R', 'R',

    'W', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
    'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'W'
};

#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
CPDF_SimpleParser::CPDF_SimpleParser(const uint8_t* pData, FX_DWORD dwSize)
{
    m_pData = pData;
    m_dwSize = dwSize;
    m_dwCurPos = 0;
}
CPDF_SimpleParser::CPDF_SimpleParser(FX_BSTR str)
{
    m_pData = str.GetPtr();
    m_dwSize = str.GetLength();
    m_dwCurPos = 0;
}
void CPDF_SimpleParser::ParseWord(const uint8_t*& pStart, FX_DWORD& dwSize, int& type)
{
    pStart = NULL;
    dwSize = 0;
    type = PDFWORD_EOF;
    uint8_t ch;
    char chartype;
    while (1) {
        if (m_dwSize <= m_dwCurPos) {
            return;
        }
        ch = m_pData[m_dwCurPos++];
        chartype = PDF_CharType[ch];
        while (chartype == 'W') {
            if (m_dwSize <= m_dwCurPos) {
                return;
            }
            ch = m_pData[m_dwCurPos++];
            chartype = PDF_CharType[ch];
        }
        if (ch != '%') {
            break;
        }
        while (1) {
            if (m_dwSize <= m_dwCurPos) {
                return;
            }
            ch = m_pData[m_dwCurPos++];
            if (ch == '\r' || ch == '\n') {
                break;
            }
        }
        chartype = PDF_CharType[ch];
    }
    FX_DWORD start_pos = m_dwCurPos - 1;
    pStart = m_pData + start_pos;
    if (chartype == 'D') {
        if (ch == '/') {
            while (1) {
                if (m_dwSize <= m_dwCurPos) {
                    return;
                }
                ch = m_pData[m_dwCurPos++];
                chartype = PDF_CharType[ch];
                if (chartype != 'R' && chartype != 'N') {
                    m_dwCurPos --;
                    dwSize = m_dwCurPos - start_pos;
                    type = PDFWORD_NAME;
                    return;
                }
            }
        } else {
            type = PDFWORD_DELIMITER;
            dwSize = 1;
            if (ch == '<') {
                if (m_dwSize <= m_dwCurPos) {
                    return;
                }
                ch = m_pData[m_dwCurPos++];
                if (ch == '<') {
                    dwSize = 2;
                } else {
                    m_dwCurPos --;
                }
            } else if (ch == '>') {
                if (m_dwSize <= m_dwCurPos) {
                    return;
                }
                ch = m_pData[m_dwCurPos++];
                if (ch == '>') {
                    dwSize = 2;
                } else {
                    m_dwCurPos --;
                }
            }
        }
        return;
    }
    type = PDFWORD_NUMBER;
    dwSize = 1;
    while (1) {
        if (chartype != 'N') {
            type = PDFWORD_TEXT;
        }
        if (m_dwSize <= m_dwCurPos) {
            return;
        }
        ch = m_pData[m_dwCurPos++];
        chartype = PDF_CharType[ch];
        if (chartype == 'D' || chartype == 'W') {
            m_dwCurPos --;
            break;
        }
        dwSize ++;
    }
}
CFX_ByteStringC CPDF_SimpleParser::GetWord()
{
    const uint8_t* pStart;
    FX_DWORD dwSize;
    int type;
    ParseWord(pStart, dwSize, type);
    if (dwSize == 1 && pStart[0] == '<') {
        while (m_dwCurPos < m_dwSize && m_pData[m_dwCurPos] != '>') {
            m_dwCurPos ++;
        }
        if (m_dwCurPos < m_dwSize) {
            m_dwCurPos ++;
        }
        return CFX_ByteStringC(pStart, (FX_STRSIZE)(m_dwCurPos - (pStart - m_pData)));
    } else if (dwSize == 1 && pStart[0] == '(') {
        int level = 1;
        while (m_dwCurPos < m_dwSize) {
            if (m_pData[m_dwCurPos] == ')') {
                level --;
                if (level == 0) {
                    break;
                }
            }
            if (m_pData[m_dwCurPos] == '\\') {
                if (m_dwSize <= m_dwCurPos) {
                    break;
                }
                m_dwCurPos ++;
            } else if (m_pData[m_dwCurPos] == '(') {
                level ++;
            }
            if (m_dwSize <= m_dwCurPos) {
                break;
            }
            m_dwCurPos ++;
        }
        if (m_dwCurPos < m_dwSize) {
            m_dwCurPos ++;
        }
        return CFX_ByteStringC(pStart, (FX_STRSIZE)(m_dwCurPos - (pStart - m_pData)));
    }
    return CFX_ByteStringC(pStart, dwSize);
}
FX_BOOL CPDF_SimpleParser::SearchToken(FX_BSTR token)
{
    int token_len = token.GetLength();
    while (m_dwCurPos < m_dwSize - token_len) {
        if (FXSYS_memcmp32(m_pData + m_dwCurPos, token.GetPtr(), token_len) == 0) {
            break;
        }
        m_dwCurPos ++;
    }
    if (m_dwCurPos == m_dwSize - token_len) {
        return FALSE;
    }
    m_dwCurPos += token_len;
    return TRUE;
}
FX_BOOL CPDF_SimpleParser::SkipWord(FX_BSTR token)
{
    while (1) {
        CFX_ByteStringC word = GetWord();
        if (word.IsEmpty()) {
            return FALSE;
        }
        if (word == token) {
            return TRUE;
        }
    }
    return FALSE;
}
FX_BOOL CPDF_SimpleParser::FindTagPair(FX_BSTR start_token, FX_BSTR end_token,
                                       FX_DWORD& start_pos, FX_DWORD& end_pos)
{
    if (!start_token.IsEmpty()) {
        if (!SkipWord(start_token)) {
            return FALSE;
        }
        start_pos = m_dwCurPos;
    }
    while (1) {
        end_pos = m_dwCurPos;
        CFX_ByteStringC word = GetWord();
        if (word.IsEmpty()) {
            return FALSE;
        }
        if (word == end_token) {
            return TRUE;
        }
    }
    return FALSE;
}
FX_BOOL CPDF_SimpleParser::FindTagParam(FX_BSTR token, int nParams)
{
    nParams ++;
    FX_DWORD* pBuf = FX_Alloc(FX_DWORD, nParams);
    int buf_index = 0;
    int buf_count = 0;
    while (1) {
        pBuf[buf_index++] = m_dwCurPos;
        if (buf_index == nParams) {
            buf_index = 0;
        }
        buf_count ++;
        if (buf_count > nParams) {
            buf_count = nParams;
        }
        CFX_ByteStringC word = GetWord();
        if (word.IsEmpty()) {
            FX_Free(pBuf);
            return FALSE;
        }
        if (word == token) {
            if (buf_count < nParams) {
                continue;
            }
            m_dwCurPos = pBuf[buf_index];
            FX_Free(pBuf);
            return TRUE;
        }
    }
    return FALSE;
}
static int _hex2dec(char ch)
{
    if (ch >= '0' && ch <= '9') {
        return ch - '0';
    }
    if (ch >= 'a' && ch <= 'f') {
        return ch - 'a' + 10;
    }
    if (ch >= 'A' && ch <= 'F') {
        return ch - 'A' + 10;
    }
    return 0;
}
CFX_ByteString PDF_NameDecode(FX_BSTR bstr)
{
    int size = bstr.GetLength();
    const FX_CHAR* pSrc = bstr.GetCStr();
    if (FXSYS_memchr(pSrc, '#', size) == NULL) {
        return bstr;
    }
    CFX_ByteString result;
    FX_CHAR* pDestStart = result.GetBuffer(size);
    FX_CHAR* pDest = pDestStart;
    for (int i = 0; i < size; i ++) {
        if (pSrc[i] == '#' && i < size - 2) {
            *pDest ++ = _hex2dec(pSrc[i + 1]) * 16 + _hex2dec(pSrc[i + 2]);
            i += 2;
        } else {
            *pDest ++ = pSrc[i];
        }
    }
    result.ReleaseBuffer((FX_STRSIZE)(pDest - pDestStart));
    return result;
}
CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig)
{
    if (FXSYS_memchr(orig.c_str(), '#', orig.GetLength()) == NULL) {
        return orig;
    }
    return PDF_NameDecode(CFX_ByteStringC(orig));
}
CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig)
{
    uint8_t* src_buf = (uint8_t*)orig.c_str();
    int src_len = orig.GetLength();
    int dest_len = 0;
    int i;
    for (i = 0; i < src_len; i ++) {
        uint8_t ch = src_buf[i];
        if (ch >= 0x80 || PDF_CharType[ch] == 'W' || ch == '#' ||
                PDF_CharType[ch] == 'D') {
            dest_len += 3;
        } else {
            dest_len ++;
        }
    }
    if (dest_len == src_len) {
        return orig;
    }
    CFX_ByteString res;
    FX_CHAR* dest_buf = res.GetBuffer(dest_len);
    dest_len = 0;
    for (i = 0; i < src_len; i ++) {
        uint8_t ch = src_buf[i];
        if (ch >= 0x80 || PDF_CharType[ch] == 'W' || ch == '#' ||
                PDF_CharType[ch] == 'D') {
            dest_buf[dest_len++] = '#';
            dest_buf[dest_len++] = "0123456789ABCDEF"[ch / 16];
            dest_buf[dest_len++] = "0123456789ABCDEF"[ch % 16];
        } else {
            dest_buf[dest_len++] = ch;
        }
    }
    dest_buf[dest_len] = 0;
    res.ReleaseBuffer();
    return res;
}
CFX_ByteTextBuf& operator << (CFX_ByteTextBuf& buf, const CPDF_Object* pObj)
{
    if (pObj == NULL) {
        buf << FX_BSTRC(" null");
        return buf;
    }
    switch (pObj->GetType()) {
        case PDFOBJ_NULL:
            buf << FX_BSTRC(" null");
            break;
        case PDFOBJ_BOOLEAN:
        case PDFOBJ_NUMBER:
            buf << " " << pObj->GetString();
            break;
        case PDFOBJ_STRING: {
                CFX_ByteString str = pObj->GetString();
                FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex();
                buf << PDF_EncodeString(str, bHex);
                break;
            }
        case PDFOBJ_NAME: {
                CFX_ByteString str = pObj->GetString();
                buf << FX_BSTRC("/") << PDF_NameEncode(str);
                break;
            }
        case PDFOBJ_REFERENCE: {
                CPDF_Reference* p = (CPDF_Reference*)pObj;
                buf << " " << p->GetRefObjNum() << FX_BSTRC(" 0 R ");
                break;
            }
        case PDFOBJ_ARRAY: {
                CPDF_Array* p = (CPDF_Array*)pObj;
                buf << FX_BSTRC("[");
                for (FX_DWORD i = 0; i < p->GetCount(); i ++) {
                    CPDF_Object* pElement = p->GetElement(i);
                    if (pElement->GetObjNum()) {
                        buf << " " << pElement->GetObjNum() << FX_BSTRC(" 0 R");
                    } else {
                        buf << pElement;
                    }
                }
                buf << FX_BSTRC("]");
                break;
            }
        case PDFOBJ_DICTIONARY: {
                CPDF_Dictionary* p = (CPDF_Dictionary*)pObj;
                buf << FX_BSTRC("<<");
                FX_POSITION pos = p->GetStartPos();
                while (pos) {
                    CFX_ByteString key;
                    CPDF_Object* pValue = p->GetNextElement(pos, key);
                    buf << FX_BSTRC("/") << PDF_NameEncode(key);
                    if (pValue->GetObjNum()) {
                        buf << " " << pValue->GetObjNum() << FX_BSTRC(" 0 R ");
                    } else {
                        buf << pValue;
                    }
                }
                buf << FX_BSTRC(">>");
                break;
            }
        case PDFOBJ_STREAM: {
                CPDF_Stream* p = (CPDF_Stream*)pObj;
                buf << p->GetDict() << FX_BSTRC("stream\r\n");
                CPDF_StreamAcc acc;
                acc.LoadAllData(p, TRUE);
                buf.AppendBlock(acc.GetData(), acc.GetSize());
                buf << FX_BSTRC("\r\nendstream");
                break;
            }
        default:
            ASSERT(FALSE);
            break;
    }
    return buf;
}
FX_FLOAT PDF_ClipFloat(FX_FLOAT f)
{
    if (f < 0) {
        return 0;
    }
    if (f > 1.0f) {
        return 1.0f;
    }
    return f;
}
static CPDF_Object* SearchNumberNode(CPDF_Dictionary* pNode, int num)
{
    CPDF_Array* pLimits = pNode->GetArray("Limits");
    if (pLimits && (num < pLimits->GetInteger(0) || num > pLimits->GetInteger(1))) {
        return NULL;
    }
    CPDF_Array* pNumbers = pNode->GetArray("Nums");
    if (pNumbers) {
        FX_DWORD dwCount = pNumbers->GetCount() / 2;
        for (FX_DWORD i = 0; i < dwCount; i ++) {
            int index = pNumbers->GetInteger(i * 2);
            if (num == index) {
                return pNumbers->GetElementValue(i * 2 + 1);
            }
            if (index > num) {
                break;
            }
        }
        return NULL;
    }
    CPDF_Array* pKids = pNode->GetArray("Kids");
    if (pKids == NULL) {
        return NULL;
    }
    for (FX_DWORD i = 0; i < pKids->GetCount(); i ++) {
        CPDF_Dictionary* pKid = pKids->GetDict(i);
        if (pKid == NULL) {
            continue;
        }
        CPDF_Object* pFound = SearchNumberNode(pKid, num);
        if (pFound) {
            return pFound;
        }
    }
    return NULL;
}
CPDF_Object* CPDF_NumberTree::LookupValue(int num)
{
    return SearchNumberNode(m_pRoot, num);
}