Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp

Issue 1360103002: Get rid of gotos in CPDF_SyntaxParser and FlateUncompress(). (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: address comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/include/fxcrt/fx_basic.h ('k') | core/src/fxcodec/codec/fx_codec_flate.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "../../../../third_party/base/nonstd_unique_ptr.h"
11 #include "../../../include/fpdfapi/fpdf_module.h" 12 #include "../../../include/fpdfapi/fpdf_module.h"
12 #include "../../../include/fpdfapi/fpdf_page.h" 13 #include "../../../include/fpdfapi/fpdf_page.h"
13 #include "../../../include/fpdfapi/fpdf_parser.h" 14 #include "../../../include/fpdfapi/fpdf_parser.h"
14 #include "../../../include/fxcrt/fx_safe_types.h" 15 #include "../../../include/fxcrt/fx_safe_types.h"
15 #include "../fpdf_page/pageint.h" 16 #include "../fpdf_page/pageint.h"
16 17
18 namespace {
19
20 int CompareFileSize(const void* p1, const void* p2) {
21 FX_FILESIZE ret = *(FX_FILESIZE*)p1 - *(FX_FILESIZE*)p2;
22 return ret == 0 ? 0 : ret;
Tom Sepez 2015/10/02 17:23:45 nit: this is just return ret; which then gets co
Lei Zhang 2015/10/02 18:26:53 Done.
23 }
24
25 int32_t GetHeaderOffset(IFX_FileRead* pFile) {
26 const FX_DWORD tag = FXDWORD_FROM_LSBFIRST(0x46445025);
27 const size_t kBufSize = 4;
28 uint8_t buf[kBufSize];
29 int32_t offset = 0;
30 while (offset <= 1024) {
31 if (!pFile->ReadBlock(buf, offset, kBufSize))
32 return -1;
33
34 if (*(FX_DWORD*)buf == tag)
35 return offset;
36
37 ++offset;
38 }
39 return -1;
40 }
41
42 int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteStringC& key) {
43 CPDF_Object* pObj = pDict->GetElement(key);
44 if (pObj && (pObj->GetType() == PDFOBJ_NUMBER))
45 return ((CPDF_Number*)pObj)->GetInteger();
46 return 0;
47 }
48
49 bool CheckDirectType(CPDF_Dictionary* pDict,
50 const CFX_ByteStringC& key,
51 int32_t iType) {
52 CPDF_Object* pObj = pDict->GetElement(key);
53 return !pObj || pObj->GetType() == iType;
54 }
55
56 FX_DWORD GetVarInt(const uint8_t* p, int32_t n) {
57 FX_DWORD result = 0;
58 for (int32_t i = 0; i < n; ++i)
59 result = result * 256 + p[i];
60 return result;
61 }
62
63 } // namespace
64
17 FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict) { 65 FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict) {
18 CPDF_Object* pType = pDict->GetElementValue(FX_BSTRC("Type")); 66 CPDF_Object* pType = pDict->GetElementValue(FX_BSTRC("Type"));
19 if (!pType) { 67 if (!pType) {
20 pType = pDict->GetElementValue(FX_BSTRC("FT")); 68 pType = pDict->GetElementValue(FX_BSTRC("FT"));
21 if (!pType) { 69 if (!pType) {
22 return FALSE; 70 return FALSE;
23 } 71 }
24 } 72 }
25 if (pType->GetString() == FX_BSTRC("Sig")) { 73 if (pType->GetString() == FX_BSTRC("Sig")) {
26 return TRUE; 74 return TRUE;
27 } 75 }
28 return FALSE; 76 return FALSE;
29 } 77 }
30 static int _CompareFileSize(const void* p1, const void* p2) {
31 FX_FILESIZE ret = (*(FX_FILESIZE*)p1) - (*(FX_FILESIZE*)p2);
32 if (ret > 0) {
33 return 1;
34 }
35 if (ret < 0) {
36 return -1;
37 }
38 return 0;
39 }
40 78
41 CPDF_Parser::CPDF_Parser() { 79 CPDF_Parser::CPDF_Parser() {
42 m_pDocument = NULL; 80 m_pDocument = NULL;
43 m_pTrailer = NULL; 81 m_pTrailer = NULL;
44 m_pEncryptDict = NULL; 82 m_pEncryptDict = NULL;
45 m_pSecurityHandler = NULL; 83 m_pSecurityHandler = NULL;
46 m_pLinearized = NULL; 84 m_pLinearized = NULL;
47 m_dwFirstPageNo = 0; 85 m_dwFirstPageNo = 0;
48 m_dwXrefStartObjNum = 0; 86 m_dwXrefStartObjNum = 0;
49 m_bOwnFileRead = TRUE; 87 m_bOwnFileRead = TRUE;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 for (int32_t i = 0; i < iLen; ++i) { 130 for (int32_t i = 0; i < iLen; ++i) {
93 if (CPDF_Dictionary* trailer = m_Trailers.GetAt(i)) 131 if (CPDF_Dictionary* trailer = m_Trailers.GetAt(i))
94 trailer->Release(); 132 trailer->Release();
95 } 133 }
96 m_Trailers.RemoveAll(); 134 m_Trailers.RemoveAll();
97 if (m_pLinearized) { 135 if (m_pLinearized) {
98 m_pLinearized->Release(); 136 m_pLinearized->Release();
99 m_pLinearized = NULL; 137 m_pLinearized = NULL;
100 } 138 }
101 } 139 }
102 static int32_t GetHeaderOffset(IFX_FileRead* pFile) {
103 FX_DWORD tag = FXDWORD_FROM_LSBFIRST(0x46445025);
104 uint8_t buf[4];
105 int32_t offset = 0;
106 while (1) {
107 if (!pFile->ReadBlock(buf, offset, 4)) {
108 return -1;
109 }
110 if (*(FX_DWORD*)buf == tag) {
111 return offset;
112 }
113 offset++;
114 if (offset > 1024) {
115 return -1;
116 }
117 }
118 return -1;
119 }
120 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler(); 140 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler();
121 CPDF_SecurityHandler* FPDF_CreatePubKeyHandler(void*); 141 CPDF_SecurityHandler* FPDF_CreatePubKeyHandler(void*);
122 FX_DWORD CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, 142 FX_DWORD CPDF_Parser::StartParse(IFX_FileRead* pFileAccess,
123 FX_BOOL bReParse, 143 FX_BOOL bReParse,
124 FX_BOOL bOwnFileRead) { 144 FX_BOOL bOwnFileRead) {
125 CloseParser(bReParse); 145 CloseParser(bReParse);
126 m_bXRefStream = FALSE; 146 m_bXRefStream = FALSE;
127 m_LastXRefOffset = 0; 147 m_LastXRefOffset = 0;
128 m_bOwnFileRead = bOwnFileRead; 148 m_bOwnFileRead = bOwnFileRead;
129 int32_t offset = GetHeaderOffset(pFileAccess); 149 int32_t offset = GetHeaderOffset(pFileAccess);
(...skipping 22 matching lines...) Expand all
152 } 172 }
153 m_Syntax.RestorePos(m_Syntax.m_FileLen - m_Syntax.m_HeaderOffset - 9); 173 m_Syntax.RestorePos(m_Syntax.m_FileLen - m_Syntax.m_HeaderOffset - 9);
154 if (!bReParse) { 174 if (!bReParse) {
155 m_pDocument = new CPDF_Document(this); 175 m_pDocument = new CPDF_Document(this);
156 } 176 }
157 FX_BOOL bXRefRebuilt = FALSE; 177 FX_BOOL bXRefRebuilt = FALSE;
158 if (m_Syntax.SearchWord(FX_BSTRC("startxref"), TRUE, FALSE, 4096)) { 178 if (m_Syntax.SearchWord(FX_BSTRC("startxref"), TRUE, FALSE, 4096)) {
159 FX_FILESIZE startxref_offset = m_Syntax.SavePos(); 179 FX_FILESIZE startxref_offset = m_Syntax.SavePos();
160 void* pResult = FXSYS_bsearch(&startxref_offset, m_SortedOffset.GetData(), 180 void* pResult = FXSYS_bsearch(&startxref_offset, m_SortedOffset.GetData(),
161 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), 181 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
162 _CompareFileSize); 182 CompareFileSize);
163 if (pResult == NULL) { 183 if (pResult == NULL) {
164 m_SortedOffset.Add(startxref_offset); 184 m_SortedOffset.Add(startxref_offset);
165 } 185 }
166 m_Syntax.GetKeyword(); 186 m_Syntax.GetKeyword();
167 FX_BOOL bNumber; 187 FX_BOOL bNumber;
168 CFX_ByteString xrefpos_str = m_Syntax.GetNextWord(bNumber); 188 CFX_ByteString xrefpos_str = m_Syntax.GetNextWord(bNumber);
169 if (!bNumber) { 189 if (!bNumber) {
170 return PDFPARSE_ERROR_FORMAT; 190 return PDFPARSE_ERROR_FORMAT;
171 } 191 }
172 m_LastXRefOffset = (FX_FILESIZE)FXSYS_atoi64(xrefpos_str); 192 m_LastXRefOffset = (FX_FILESIZE)FXSYS_atoi64(xrefpos_str);
(...skipping 27 matching lines...) Expand all
200 dwRet = SetEncryptHandler(); 220 dwRet = SetEncryptHandler();
201 if (dwRet != PDFPARSE_ERROR_SUCCESS) { 221 if (dwRet != PDFPARSE_ERROR_SUCCESS) {
202 return dwRet; 222 return dwRet;
203 } 223 }
204 m_pDocument->LoadDoc(); 224 m_pDocument->LoadDoc();
205 if (m_pDocument->GetRoot() == NULL) { 225 if (m_pDocument->GetRoot() == NULL) {
206 return PDFPARSE_ERROR_FORMAT; 226 return PDFPARSE_ERROR_FORMAT;
207 } 227 }
208 } 228 }
209 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 229 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
210 sizeof(FX_FILESIZE), _CompareFileSize); 230 sizeof(FX_FILESIZE), CompareFileSize);
211 FX_DWORD RootObjNum = GetRootObjNum(); 231 FX_DWORD RootObjNum = GetRootObjNum();
212 if (RootObjNum == 0) { 232 if (RootObjNum == 0) {
213 ReleaseEncryptHandler(); 233 ReleaseEncryptHandler();
214 RebuildCrossRef(); 234 RebuildCrossRef();
215 RootObjNum = GetRootObjNum(); 235 RootObjNum = GetRootObjNum();
216 if (RootObjNum == 0) { 236 if (RootObjNum == 0) {
217 return PDFPARSE_ERROR_FORMAT; 237 return PDFPARSE_ERROR_FORMAT;
218 } 238 }
219 dwRet = SetEncryptHandler(); 239 dwRet = SetEncryptHandler();
220 if (dwRet != PDFPARSE_ERROR_SUCCESS) { 240 if (dwRet != PDFPARSE_ERROR_SUCCESS) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 return 0; 326 return 0;
307 } 327 }
308 if (m_V5Type[objnum] == 1) { 328 if (m_V5Type[objnum] == 1) {
309 return m_CrossRef[objnum]; 329 return m_CrossRef[objnum];
310 } 330 }
311 if (m_V5Type[objnum] == 2) { 331 if (m_V5Type[objnum] == 2) {
312 return m_CrossRef[(int32_t)m_CrossRef[objnum]]; 332 return m_CrossRef[(int32_t)m_CrossRef[objnum]];
313 } 333 }
314 return 0; 334 return 0;
315 } 335 }
316 static int32_t GetDirectInteger(CPDF_Dictionary* pDict, 336
317 const CFX_ByteStringC& key) {
318 CPDF_Object* pObj = pDict->GetElement(key);
319 if (pObj == NULL) {
320 return 0;
321 }
322 if (pObj->GetType() == PDFOBJ_NUMBER) {
323 return ((CPDF_Number*)pObj)->GetInteger();
324 }
325 return 0;
326 }
327 static FX_BOOL CheckDirectType(CPDF_Dictionary* pDict,
328 const CFX_ByteStringC& key,
329 int32_t iType) {
330 CPDF_Object* pObj = pDict->GetElement(key);
331 if (!pObj) {
332 return TRUE;
333 }
334 return pObj->GetType() == iType;
335 }
336 FX_BOOL CPDF_Parser::LoadAllCrossRefV4(FX_FILESIZE xrefpos) { 337 FX_BOOL CPDF_Parser::LoadAllCrossRefV4(FX_FILESIZE xrefpos) {
337 if (!LoadCrossRefV4(xrefpos, 0, TRUE, FALSE)) { 338 if (!LoadCrossRefV4(xrefpos, 0, TRUE, FALSE)) {
338 return FALSE; 339 return FALSE;
339 } 340 }
340 m_pTrailer = LoadTrailerV4(); 341 m_pTrailer = LoadTrailerV4();
341 if (m_pTrailer == NULL) { 342 if (m_pTrailer == NULL) {
342 return FALSE; 343 return FALSE;
343 } 344 }
344 int32_t xrefsize = GetDirectInteger(m_pTrailer, FX_BSTRC("Size")); 345 int32_t xrefsize = GetDirectInteger(m_pTrailer, FX_BSTRC("Size"));
345 if (xrefsize <= 0 || xrefsize > (1 << 20)) { 346 if (xrefsize <= 0 || xrefsize > (1 << 20)) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 return FALSE; 418 return FALSE;
418 } 419 }
419 return TRUE; 420 return TRUE;
420 } 421 }
421 FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos, 422 FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos,
422 FX_DWORD dwObjCount) { 423 FX_DWORD dwObjCount) {
423 FX_FILESIZE dwStartPos = pos - m_Syntax.m_HeaderOffset; 424 FX_FILESIZE dwStartPos = pos - m_Syntax.m_HeaderOffset;
424 m_Syntax.RestorePos(dwStartPos); 425 m_Syntax.RestorePos(dwStartPos);
425 void* pResult = 426 void* pResult =
426 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 427 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
427 sizeof(FX_FILESIZE), _CompareFileSize); 428 sizeof(FX_FILESIZE), CompareFileSize);
428 if (pResult == NULL) { 429 if (pResult == NULL) {
429 m_SortedOffset.Add(pos); 430 m_SortedOffset.Add(pos);
430 } 431 }
431 FX_DWORD start_objnum = 0; 432 FX_DWORD start_objnum = 0;
432 FX_DWORD count = dwObjCount; 433 FX_DWORD count = dwObjCount;
433 FX_FILESIZE SavedPos = m_Syntax.SavePos(); 434 FX_FILESIZE SavedPos = m_Syntax.SavePos();
434 int32_t recordsize = 20; 435 int32_t recordsize = 20;
435 char* pBuf = FX_Alloc(char, 1024 * recordsize + 1); 436 char* pBuf = FX_Alloc(char, 1024 * recordsize + 1);
436 pBuf[1024 * recordsize] = '\0'; 437 pBuf[1024 * recordsize] = '\0';
437 int32_t nBlocks = count / 1024 + 1; 438 int32_t nBlocks = count / 1024 + 1;
(...skipping 26 matching lines...) Expand all
464 } 465 }
465 m_CrossRef.SetAtGrow(objnum, offset); 466 m_CrossRef.SetAtGrow(objnum, offset);
466 int32_t version = FXSYS_atoi(pEntry + 11); 467 int32_t version = FXSYS_atoi(pEntry + 11);
467 if (version >= 1) { 468 if (version >= 1) {
468 m_bVersionUpdated = TRUE; 469 m_bVersionUpdated = TRUE;
469 } 470 }
470 m_ObjVersion.SetAtGrow(objnum, version); 471 m_ObjVersion.SetAtGrow(objnum, version);
471 if (m_CrossRef[objnum] < m_Syntax.m_FileLen) { 472 if (m_CrossRef[objnum] < m_Syntax.m_FileLen) {
472 void* pResult = FXSYS_bsearch( 473 void* pResult = FXSYS_bsearch(
473 &m_CrossRef[objnum], m_SortedOffset.GetData(), 474 &m_CrossRef[objnum], m_SortedOffset.GetData(),
474 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), _CompareFileSize); 475 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), CompareFileSize);
475 if (pResult == NULL) { 476 if (pResult == NULL) {
476 m_SortedOffset.Add(m_CrossRef[objnum]); 477 m_SortedOffset.Add(m_CrossRef[objnum]);
477 } 478 }
478 } 479 }
479 m_V5Type.SetAtGrow(objnum, 1); 480 m_V5Type.SetAtGrow(objnum, 1);
480 } 481 }
481 } 482 }
482 } 483 }
483 FX_Free(pBuf); 484 FX_Free(pBuf);
484 m_Syntax.RestorePos(SavedPos + count * recordsize); 485 m_Syntax.RestorePos(SavedPos + count * recordsize);
485 return TRUE; 486 return TRUE;
486 } 487 }
487 FX_BOOL CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, 488
488 FX_FILESIZE streampos, 489 bool CPDF_Parser::FindPosInOffsets(FX_FILESIZE pos) const {
489 FX_BOOL bSkip, 490 return FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
490 FX_BOOL bFirst) { 491 sizeof(FX_FILESIZE), CompareFileSize);
492 }
493
494 bool CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos,
495 FX_FILESIZE streampos,
496 FX_BOOL bSkip,
497 FX_BOOL bFirst) {
491 m_Syntax.RestorePos(pos); 498 m_Syntax.RestorePos(pos);
492 if (m_Syntax.GetKeyword() != FX_BSTRC("xref")) { 499 if (m_Syntax.GetKeyword() != FX_BSTRC("xref"))
493 return FALSE; 500 return false;
494 } 501
495 void* pResult = 502 if (!FindPosInOffsets(pos))
496 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
497 sizeof(FX_FILESIZE), _CompareFileSize);
498 if (pResult == NULL) {
499 m_SortedOffset.Add(pos); 503 m_SortedOffset.Add(pos);
500 } 504
501 if (streampos) { 505 if (streampos && !FindPosInOffsets(streampos))
502 void* pResult = FXSYS_bsearch(&streampos, m_SortedOffset.GetData(),
503 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
504 _CompareFileSize);
505 if (pResult == NULL) {
506 m_SortedOffset.Add(streampos); 506 m_SortedOffset.Add(streampos);
507 } 507
508 }
509 while (1) { 508 while (1) {
510 FX_FILESIZE SavedPos = m_Syntax.SavePos(); 509 FX_FILESIZE SavedPos = m_Syntax.SavePos();
511 FX_BOOL bIsNumber; 510 FX_BOOL bIsNumber;
512 CFX_ByteString word = m_Syntax.GetNextWord(bIsNumber); 511 CFX_ByteString word = m_Syntax.GetNextWord(bIsNumber);
513 if (word.IsEmpty()) { 512 if (word.IsEmpty())
514 return FALSE; 513 return false;
515 } 514
516 if (!bIsNumber) { 515 if (!bIsNumber) {
517 m_Syntax.RestorePos(SavedPos); 516 m_Syntax.RestorePos(SavedPos);
518 break; 517 break;
519 } 518 }
520 FX_DWORD start_objnum = FXSYS_atoi(word); 519 FX_DWORD start_objnum = FXSYS_atoi(word);
521 if (start_objnum >= (1 << 20)) { 520 if (start_objnum >= (1 << 20))
522 return FALSE; 521 return false;
523 } 522
524 FX_DWORD count = m_Syntax.GetDirectNum(); 523 FX_DWORD count = m_Syntax.GetDirectNum();
525 m_Syntax.ToNextWord(); 524 m_Syntax.ToNextWord();
526 SavedPos = m_Syntax.SavePos(); 525 SavedPos = m_Syntax.SavePos();
527 FX_BOOL bFirstItem = FALSE; 526 FX_BOOL bFirstItem = FALSE;
528 int32_t recordsize = 20; 527 int32_t recordsize = 20;
529 if (bFirst) { 528 if (bFirst)
530 bFirstItem = TRUE; 529 bFirstItem = TRUE;
531 }
532 m_dwXrefStartObjNum = start_objnum; 530 m_dwXrefStartObjNum = start_objnum;
533 if (!bSkip) { 531 if (!bSkip) {
534 char* pBuf = FX_Alloc(char, 1024 * recordsize + 1); 532 char* pBuf = FX_Alloc(char, 1024 * recordsize + 1);
535 pBuf[1024 * recordsize] = '\0'; 533 pBuf[1024 * recordsize] = '\0';
536 int32_t nBlocks = count / 1024 + 1; 534 int32_t nBlocks = count / 1024 + 1;
537 FX_BOOL bFirstBlock = TRUE; 535 FX_BOOL bFirstBlock = TRUE;
538 for (int32_t block = 0; block < nBlocks; block++) { 536 for (int32_t block = 0; block < nBlocks; block++) {
539 int32_t block_size = block == nBlocks - 1 ? count % 1024 : 1024; 537 int32_t block_size = block == nBlocks - 1 ? count % 1024 : 1024;
540 m_Syntax.ReadBlock((uint8_t*)pBuf, block_size * recordsize); 538 m_Syntax.ReadBlock((uint8_t*)pBuf, block_size * recordsize);
541 for (int32_t i = 0; i < block_size; i++) { 539 for (int32_t i = 0; i < block_size; i++) {
(...skipping 13 matching lines...) Expand all
555 } 553 }
556 } 554 }
557 m_CrossRef.SetAtGrow(objnum, 0); 555 m_CrossRef.SetAtGrow(objnum, 0);
558 m_V5Type.SetAtGrow(objnum, 0); 556 m_V5Type.SetAtGrow(objnum, 0);
559 } else { 557 } else {
560 FX_FILESIZE offset = (FX_FILESIZE)FXSYS_atoi64(pEntry); 558 FX_FILESIZE offset = (FX_FILESIZE)FXSYS_atoi64(pEntry);
561 if (offset == 0) { 559 if (offset == 0) {
562 for (int32_t c = 0; c < 10; c++) { 560 for (int32_t c = 0; c < 10; c++) {
563 if (pEntry[c] < '0' || pEntry[c] > '9') { 561 if (pEntry[c] < '0' || pEntry[c] > '9') {
564 FX_Free(pBuf); 562 FX_Free(pBuf);
565 return FALSE; 563 return false;
566 } 564 }
567 } 565 }
568 } 566 }
569 m_CrossRef.SetAtGrow(objnum, offset); 567 m_CrossRef.SetAtGrow(objnum, offset);
570 int32_t version = FXSYS_atoi(pEntry + 11); 568 int32_t version = FXSYS_atoi(pEntry + 11);
571 if (version >= 1) { 569 if (version >= 1) {
572 m_bVersionUpdated = TRUE; 570 m_bVersionUpdated = TRUE;
573 } 571 }
574 m_ObjVersion.SetAtGrow(objnum, version); 572 m_ObjVersion.SetAtGrow(objnum, version);
575 if (m_CrossRef[objnum] < m_Syntax.m_FileLen) { 573 if (m_CrossRef[objnum] < m_Syntax.m_FileLen &&
576 void* pResult = 574 !FindPosInOffsets(m_CrossRef[objnum])) {
577 FXSYS_bsearch(&m_CrossRef[objnum], m_SortedOffset.GetData(), 575 m_SortedOffset.Add(m_CrossRef[objnum]);
578 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
579 _CompareFileSize);
580 if (pResult == NULL) {
581 m_SortedOffset.Add(m_CrossRef[objnum]);
582 }
583 } 576 }
584 m_V5Type.SetAtGrow(objnum, 1); 577 m_V5Type.SetAtGrow(objnum, 1);
585 } 578 }
586 if (bFirstBlock) { 579 if (bFirstBlock) {
587 bFirstBlock = FALSE; 580 bFirstBlock = FALSE;
588 } 581 }
589 } 582 }
590 } 583 }
591 FX_Free(pBuf); 584 FX_Free(pBuf);
592 } 585 }
593 m_Syntax.RestorePos(SavedPos + count * recordsize); 586 m_Syntax.RestorePos(SavedPos + count * recordsize);
594 } 587 }
595 if (streampos) 588 return !streampos || LoadCrossRefV5(streampos, streampos, FALSE);
596 if (!LoadCrossRefV5(streampos, streampos, FALSE)) {
597 return FALSE;
598 }
599 return TRUE;
600 } 589 }
590
601 FX_BOOL CPDF_Parser::LoadAllCrossRefV5(FX_FILESIZE xrefpos) { 591 FX_BOOL CPDF_Parser::LoadAllCrossRefV5(FX_FILESIZE xrefpos) {
602 if (!LoadCrossRefV5(xrefpos, xrefpos, TRUE)) { 592 if (!LoadCrossRefV5(xrefpos, xrefpos, TRUE)) {
603 return FALSE; 593 return FALSE;
604 } 594 }
605 while (xrefpos) 595 while (xrefpos)
606 if (!LoadCrossRefV5(xrefpos, xrefpos, FALSE)) { 596 if (!LoadCrossRefV5(xrefpos, xrefpos, FALSE)) {
607 return FALSE; 597 return FALSE;
608 } 598 }
609 m_ObjectStreamMap.InitHashTable(101, FALSE); 599 m_ObjectStreamMap.InitHashTable(101, FALSE);
610 m_bXRefStream = TRUE; 600 m_bXRefStream = TRUE;
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 if (PDF_CharType[byte] == 'W' || PDF_CharType[byte] == 'D') { 755 if (PDF_CharType[byte] == 'W' || PDF_CharType[byte] == 'D') {
766 if (objnum > 0x1000000) { 756 if (objnum > 0x1000000) {
767 status = 0; 757 status = 0;
768 break; 758 break;
769 } 759 }
770 FX_FILESIZE obj_pos = start_pos - m_Syntax.m_HeaderOffset; 760 FX_FILESIZE obj_pos = start_pos - m_Syntax.m_HeaderOffset;
771 last_obj = start_pos; 761 last_obj = start_pos;
772 void* pResult = 762 void* pResult =
773 FXSYS_bsearch(&obj_pos, m_SortedOffset.GetData(), 763 FXSYS_bsearch(&obj_pos, m_SortedOffset.GetData(),
774 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), 764 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
775 _CompareFileSize); 765 CompareFileSize);
776 if (pResult == NULL) { 766 if (pResult == NULL) {
777 m_SortedOffset.Add(obj_pos); 767 m_SortedOffset.Add(obj_pos);
778 } 768 }
779 FX_FILESIZE obj_end = 0; 769 FX_FILESIZE obj_end = 0;
780 CPDF_Object* pObject = ParseIndirectObjectAtByStrict( 770 CPDF_Object* pObject = ParseIndirectObjectAtByStrict(
781 m_pDocument, obj_pos, objnum, NULL, &obj_end); 771 m_pDocument, obj_pos, objnum, NULL, &obj_end);
782 if (pObject) { 772 if (pObject) {
783 int iType = pObject->GetType(); 773 int iType = pObject->GetType();
784 if (iType == PDFOBJ_STREAM) { 774 if (iType == PDFOBJ_STREAM) {
785 CPDF_Stream* pStream = (CPDF_Stream*)pObject; 775 CPDF_Stream* pStream = (CPDF_Stream*)pObject;
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 pos += size; 973 pos += size;
984 } 974 }
985 if (last_xref != -1 && last_xref > last_obj) { 975 if (last_xref != -1 && last_xref > last_obj) {
986 last_trailer = last_xref; 976 last_trailer = last_xref;
987 } else if (last_trailer == -1 || last_xref < last_obj) { 977 } else if (last_trailer == -1 || last_xref < last_obj) {
988 last_trailer = m_Syntax.m_FileLen; 978 last_trailer = m_Syntax.m_FileLen;
989 } 979 }
990 FX_FILESIZE offset = last_trailer - m_Syntax.m_HeaderOffset; 980 FX_FILESIZE offset = last_trailer - m_Syntax.m_HeaderOffset;
991 void* pResult = 981 void* pResult =
992 FXSYS_bsearch(&offset, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 982 FXSYS_bsearch(&offset, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
993 sizeof(FX_FILESIZE), _CompareFileSize); 983 sizeof(FX_FILESIZE), CompareFileSize);
994 if (pResult == NULL) { 984 if (pResult == NULL) {
995 m_SortedOffset.Add(offset); 985 m_SortedOffset.Add(offset);
996 } 986 }
997 FX_Free(buffer); 987 FX_Free(buffer);
998 return TRUE; 988 return TRUE;
999 } 989 }
1000 static FX_DWORD _GetVarInt(const uint8_t* p, int32_t n) { 990
1001 FX_DWORD result = 0;
1002 for (int32_t i = 0; i < n; i++) {
1003 result = result * 256 + p[i];
1004 }
1005 return result;
1006 }
1007 FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos, 991 FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE pos,
1008 FX_FILESIZE& prev, 992 FX_FILESIZE& prev,
1009 FX_BOOL bMainXRef) { 993 FX_BOOL bMainXRef) {
1010 CPDF_Stream* pStream = 994 CPDF_Stream* pStream =
1011 (CPDF_Stream*)ParseIndirectObjectAt(m_pDocument, pos, 0, NULL); 995 (CPDF_Stream*)ParseIndirectObjectAt(m_pDocument, pos, 0, NULL);
1012 if (!pStream) { 996 if (!pStream) {
1013 return FALSE; 997 return FALSE;
1014 } 998 }
1015 if (m_pDocument) { 999 if (m_pDocument) {
1016 CPDF_Dictionary* pDict = m_pDocument->GetRoot(); 1000 CPDF_Dictionary* pDict = m_pDocument->GetRoot();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 dwMaxObjNum += count; 1087 dwMaxObjNum += count;
1104 FX_DWORD dwV5Size = 1088 FX_DWORD dwV5Size =
1105 pdfium::base::checked_cast<FX_DWORD, int32_t>(m_V5Type.GetSize()); 1089 pdfium::base::checked_cast<FX_DWORD, int32_t>(m_V5Type.GetSize());
1106 if (!dwMaxObjNum.IsValid() || dwMaxObjNum.ValueOrDie() > dwV5Size) { 1090 if (!dwMaxObjNum.IsValid() || dwMaxObjNum.ValueOrDie() > dwV5Size) {
1107 continue; 1091 continue;
1108 } 1092 }
1109 for (FX_DWORD j = 0; j < count; j++) { 1093 for (FX_DWORD j = 0; j < count; j++) {
1110 int32_t type = 1; 1094 int32_t type = 1;
1111 const uint8_t* entrystart = segstart + j * totalWidth; 1095 const uint8_t* entrystart = segstart + j * totalWidth;
1112 if (WidthArray[0]) { 1096 if (WidthArray[0]) {
1113 type = _GetVarInt(entrystart, WidthArray[0]); 1097 type = GetVarInt(entrystart, WidthArray[0]);
1114 } 1098 }
1115 if (m_V5Type[startnum + j] == 255) { 1099 if (m_V5Type[startnum + j] == 255) {
1116 FX_FILESIZE offset = 1100 FX_FILESIZE offset =
1117 _GetVarInt(entrystart + WidthArray[0], WidthArray[1]); 1101 GetVarInt(entrystart + WidthArray[0], WidthArray[1]);
1118 m_CrossRef[startnum + j] = offset; 1102 m_CrossRef[startnum + j] = offset;
1119 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(), 1103 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(),
1120 m_SortedOffset.GetSize(), 1104 m_SortedOffset.GetSize(),
1121 sizeof(FX_FILESIZE), _CompareFileSize); 1105 sizeof(FX_FILESIZE), CompareFileSize);
1122 if (pResult == NULL) { 1106 if (pResult == NULL) {
1123 m_SortedOffset.Add(offset); 1107 m_SortedOffset.Add(offset);
1124 } 1108 }
1125 continue; 1109 continue;
1126 } 1110 }
1127 if (m_V5Type[startnum + j]) { 1111 if (m_V5Type[startnum + j]) {
1128 continue; 1112 continue;
1129 } 1113 }
1130 m_V5Type[startnum + j] = type; 1114 m_V5Type[startnum + j] = type;
1131 if (type == 0) { 1115 if (type == 0) {
1132 m_CrossRef[startnum + j] = 0; 1116 m_CrossRef[startnum + j] = 0;
1133 } else { 1117 } else {
1134 FX_FILESIZE offset = 1118 FX_FILESIZE offset =
1135 _GetVarInt(entrystart + WidthArray[0], WidthArray[1]); 1119 GetVarInt(entrystart + WidthArray[0], WidthArray[1]);
1136 m_CrossRef[startnum + j] = offset; 1120 m_CrossRef[startnum + j] = offset;
1137 if (type == 1) { 1121 if (type == 1) {
1138 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(), 1122 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(),
1139 m_SortedOffset.GetSize(), 1123 m_SortedOffset.GetSize(),
1140 sizeof(FX_FILESIZE), _CompareFileSize); 1124 sizeof(FX_FILESIZE), CompareFileSize);
1141 if (pResult == NULL) { 1125 if (pResult == NULL) {
1142 m_SortedOffset.Add(offset); 1126 m_SortedOffset.Add(offset);
1143 } 1127 }
1144 } else { 1128 } else {
1145 if (offset < 0 || offset >= m_V5Type.GetSize()) { 1129 if (offset < 0 || offset >= m_V5Type.GetSize()) {
1146 pStream->Release(); 1130 pStream->Release();
1147 return FALSE; 1131 return FALSE;
1148 } 1132 }
1149 m_V5Type[offset] = 255; 1133 m_V5Type[offset] = 255;
1150 } 1134 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1192 } 1176 }
1193 if (m_V5Type[objnum] == 0) { 1177 if (m_V5Type[objnum] == 0) {
1194 return TRUE; 1178 return TRUE;
1195 } 1179 }
1196 if (m_V5Type[objnum] == 2) { 1180 if (m_V5Type[objnum] == 2) {
1197 return TRUE; 1181 return TRUE;
1198 } 1182 }
1199 FX_FILESIZE pos = m_CrossRef[objnum]; 1183 FX_FILESIZE pos = m_CrossRef[objnum];
1200 void* pResult = 1184 void* pResult =
1201 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 1185 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
1202 sizeof(FX_FILESIZE), _CompareFileSize); 1186 sizeof(FX_FILESIZE), CompareFileSize);
1203 if (pResult == NULL) { 1187 if (pResult == NULL) {
1204 return TRUE; 1188 return TRUE;
1205 } 1189 }
1206 if ((FX_FILESIZE*)pResult - (FX_FILESIZE*)m_SortedOffset.GetData() == 1190 if ((FX_FILESIZE*)pResult - (FX_FILESIZE*)m_SortedOffset.GetData() ==
1207 m_SortedOffset.GetSize() - 1) { 1191 m_SortedOffset.GetSize() - 1) {
1208 return FALSE; 1192 return FALSE;
1209 } 1193 }
1210 FX_FILESIZE size = ((FX_FILESIZE*)pResult)[1] - pos; 1194 FX_FILESIZE size = ((FX_FILESIZE*)pResult)[1] - pos;
1211 FX_FILESIZE SavedPos = m_Syntax.SavePos(); 1195 FX_FILESIZE SavedPos = m_Syntax.SavePos();
1212 m_Syntax.RestorePos(pos); 1196 m_Syntax.RestorePos(pos);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 if (m_V5Type[objnum] == 2) { 1259 if (m_V5Type[objnum] == 2) {
1276 objnum = (FX_DWORD)m_CrossRef[objnum]; 1260 objnum = (FX_DWORD)m_CrossRef[objnum];
1277 } 1261 }
1278 if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) { 1262 if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) {
1279 FX_FILESIZE offset = m_CrossRef[objnum]; 1263 FX_FILESIZE offset = m_CrossRef[objnum];
1280 if (offset == 0) { 1264 if (offset == 0) {
1281 return 0; 1265 return 0;
1282 } 1266 }
1283 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(), 1267 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(),
1284 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), 1268 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
1285 _CompareFileSize); 1269 CompareFileSize);
1286 if (pResult == NULL) { 1270 if (pResult == NULL) {
1287 return 0; 1271 return 0;
1288 } 1272 }
1289 if ((FX_FILESIZE*)pResult - (FX_FILESIZE*)m_SortedOffset.GetData() == 1273 if ((FX_FILESIZE*)pResult - (FX_FILESIZE*)m_SortedOffset.GetData() ==
1290 m_SortedOffset.GetSize() - 1) { 1274 m_SortedOffset.GetSize() - 1) {
1291 return 0; 1275 return 0;
1292 } 1276 }
1293 return ((FX_FILESIZE*)pResult)[1] - offset; 1277 return ((FX_FILESIZE*)pResult)[1] - offset;
1294 } 1278 }
1295 return 0; 1279 return 0;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1356 if (!bIsNumber) { 1340 if (!bIsNumber) {
1357 m_Syntax.RestorePos(SavedPos); 1341 m_Syntax.RestorePos(SavedPos);
1358 return; 1342 return;
1359 } 1343 }
1360 if (m_Syntax.GetKeyword() != FX_BSTRC("obj")) { 1344 if (m_Syntax.GetKeyword() != FX_BSTRC("obj")) {
1361 m_Syntax.RestorePos(SavedPos); 1345 m_Syntax.RestorePos(SavedPos);
1362 return; 1346 return;
1363 } 1347 }
1364 void* pResult = 1348 void* pResult =
1365 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 1349 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
1366 sizeof(FX_FILESIZE), _CompareFileSize); 1350 sizeof(FX_FILESIZE), CompareFileSize);
1367 if (pResult == NULL) { 1351 if (pResult == NULL) {
1368 m_Syntax.RestorePos(SavedPos); 1352 m_Syntax.RestorePos(SavedPos);
1369 return; 1353 return;
1370 } 1354 }
1371 FX_FILESIZE nextoff = ((FX_FILESIZE*)pResult)[1]; 1355 FX_FILESIZE nextoff = ((FX_FILESIZE*)pResult)[1];
1372 FX_BOOL bNextOffValid = FALSE; 1356 FX_BOOL bNextOffValid = FALSE;
1373 if (nextoff != pos) { 1357 if (nextoff != pos) {
1374 m_Syntax.RestorePos(nextoff); 1358 m_Syntax.RestorePos(nextoff);
1375 word = m_Syntax.GetNextWord(bIsNumber); 1359 word = m_Syntax.GetNextWord(bIsNumber);
1376 if (word == FX_BSTRC("xref")) { 1360 if (word == FX_BSTRC("xref")) {
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1635 dwRet = SetEncryptHandler(); 1619 dwRet = SetEncryptHandler();
1636 if (dwRet != PDFPARSE_ERROR_SUCCESS) { 1620 if (dwRet != PDFPARSE_ERROR_SUCCESS) {
1637 return dwRet; 1621 return dwRet;
1638 } 1622 }
1639 m_pDocument->LoadAsynDoc(m_pLinearized->GetDict()); 1623 m_pDocument->LoadAsynDoc(m_pLinearized->GetDict());
1640 if (m_pDocument->GetRoot() == NULL) { 1624 if (m_pDocument->GetRoot() == NULL) {
1641 return PDFPARSE_ERROR_FORMAT; 1625 return PDFPARSE_ERROR_FORMAT;
1642 } 1626 }
1643 } 1627 }
1644 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 1628 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
1645 sizeof(FX_FILESIZE), _CompareFileSize); 1629 sizeof(FX_FILESIZE), CompareFileSize);
1646 FX_DWORD RootObjNum = GetRootObjNum(); 1630 FX_DWORD RootObjNum = GetRootObjNum();
1647 if (RootObjNum == 0) { 1631 if (RootObjNum == 0) {
1648 ReleaseEncryptHandler(); 1632 ReleaseEncryptHandler();
1649 RebuildCrossRef(); 1633 RebuildCrossRef();
1650 RootObjNum = GetRootObjNum(); 1634 RootObjNum = GetRootObjNum();
1651 if (RootObjNum == 0) { 1635 if (RootObjNum == 0) {
1652 return PDFPARSE_ERROR_FORMAT; 1636 return PDFPARSE_ERROR_FORMAT;
1653 } 1637 }
1654 dwRet = SetEncryptHandler(); 1638 dwRet = SetEncryptHandler();
1655 if (dwRet != PDFPARSE_ERROR_SUCCESS) { 1639 if (dwRet != PDFPARSE_ERROR_SUCCESS) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1707 delete pStream; 1691 delete pStream;
1708 } 1692 }
1709 m_ObjectStreamMap.RemoveAll(); 1693 m_ObjectStreamMap.RemoveAll();
1710 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) && 1694 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) &&
1711 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) { 1695 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) {
1712 m_LastXRefOffset = 0; 1696 m_LastXRefOffset = 0;
1713 m_Syntax.m_MetadataObjnum = dwSaveMetadataObjnum; 1697 m_Syntax.m_MetadataObjnum = dwSaveMetadataObjnum;
1714 return PDFPARSE_ERROR_FORMAT; 1698 return PDFPARSE_ERROR_FORMAT;
1715 } 1699 }
1716 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 1700 FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
1717 sizeof(FX_FILESIZE), _CompareFileSize); 1701 sizeof(FX_FILESIZE), CompareFileSize);
1718 m_Syntax.m_MetadataObjnum = dwSaveMetadataObjnum; 1702 m_Syntax.m_MetadataObjnum = dwSaveMetadataObjnum;
1719 return PDFPARSE_ERROR_SUCCESS; 1703 return PDFPARSE_ERROR_SUCCESS;
1720 } 1704 }
1721 1705
1722 // static 1706 // static
1723 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; 1707 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0;
1724 1708
1725 CPDF_SyntaxParser::CPDF_SyntaxParser() { 1709 CPDF_SyntaxParser::CPDF_SyntaxParser() {
1726 m_pFileAccess = NULL; 1710 m_pFileAccess = NULL;
1727 m_pCryptoHandler = NULL; 1711 m_pCryptoHandler = NULL;
1728 m_pFileBuf = NULL; 1712 m_pFileBuf = NULL;
1729 m_BufSize = CPDF_ModuleMgr::kFileBufSize; 1713 m_BufSize = CPDF_ModuleMgr::kFileBufSize;
1730 m_pFileBuf = NULL; 1714 m_pFileBuf = NULL;
1731 m_MetadataObjnum = 0; 1715 m_MetadataObjnum = 0;
1732 m_dwWordPos = 0; 1716 m_dwWordPos = 0;
1733 m_bFileStream = FALSE; 1717 m_bFileStream = FALSE;
1734 } 1718 }
1735 CPDF_SyntaxParser::~CPDF_SyntaxParser() { 1719 CPDF_SyntaxParser::~CPDF_SyntaxParser() {
1736 FX_Free(m_pFileBuf); 1720 FX_Free(m_pFileBuf);
1737 } 1721 }
1722
1738 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { 1723 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) {
1739 FX_FILESIZE save_pos = m_Pos; 1724 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos);
1740 m_Pos = pos; 1725 m_Pos = pos;
1741 FX_BOOL ret = GetNextChar(ch); 1726 return GetNextChar(ch);
1742 m_Pos = save_pos;
1743 return ret;
1744 } 1727 }
1728
1745 FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) { 1729 FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) {
1746 FX_FILESIZE pos = m_Pos + m_HeaderOffset; 1730 FX_FILESIZE pos = m_Pos + m_HeaderOffset;
1747 if (pos >= m_FileLen) { 1731 if (pos >= m_FileLen) {
1748 return FALSE; 1732 return FALSE;
1749 } 1733 }
1750 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { 1734 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) {
1751 FX_FILESIZE read_pos = pos; 1735 FX_FILESIZE read_pos = pos;
1752 FX_DWORD read_size = m_BufSize; 1736 FX_DWORD read_size = m_BufSize;
1753 if ((FX_FILESIZE)read_size > m_FileLen) { 1737 if ((FX_FILESIZE)read_size > m_FileLen) {
1754 read_size = (FX_DWORD)m_FileLen; 1738 read_size = (FX_DWORD)m_FileLen;
(...skipping 938 matching lines...) Expand 10 before | Expand all | Expand 10 after
2693 } else { 2677 } else {
2694 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1; 2678 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1;
2695 pos--; 2679 pos--;
2696 } 2680 }
2697 if (pos < 0) { 2681 if (pos < 0) {
2698 return FALSE; 2682 return FALSE;
2699 } 2683 }
2700 } 2684 }
2701 return FALSE; 2685 return FALSE;
2702 } 2686 }
2687
2703 struct _SearchTagRecord { 2688 struct _SearchTagRecord {
2704 const uint8_t* m_pTag; 2689 const uint8_t* m_pTag;
2705 FX_DWORD m_Len; 2690 FX_DWORD m_Len;
2706 FX_DWORD m_Offset; 2691 FX_DWORD m_Offset;
2707 }; 2692 };
2693
2708 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, 2694 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags,
2709 FX_BOOL bWholeWord, 2695 FX_BOOL bWholeWord,
2710 FX_FILESIZE limit) { 2696 FX_FILESIZE limit) {
2711 int32_t ntags = 1, i; 2697 int32_t ntags = 1;
2712 for (i = 0; i < tags.GetLength(); i++) 2698 for (int i = 0; i < tags.GetLength(); ++i) {
2713 if (tags[i] == 0) { 2699 if (tags[i] == 0) {
2714 ntags++; 2700 ++ntags;
2715 } 2701 }
2716 _SearchTagRecord* pPatterns = FX_Alloc(_SearchTagRecord, ntags); 2702 }
2717 FX_DWORD start = 0, itag = 0, max_len = 0; 2703
2718 for (i = 0; i <= tags.GetLength(); i++) { 2704 std::vector<_SearchTagRecord> patterns(ntags);
2705 FX_DWORD start = 0;
2706 FX_DWORD itag = 0;
2707 FX_DWORD max_len = 0;
2708 for (int i = 0; i <= tags.GetLength(); ++i) {
2719 if (tags[i] == 0) { 2709 if (tags[i] == 0) {
2720 FX_DWORD len = i - start; 2710 FX_DWORD len = i - start;
2721 if (len > max_len) { 2711 max_len = std::max(len, max_len);
2722 max_len = len; 2712 patterns[itag].m_pTag = tags.GetPtr() + start;
2723 } 2713 patterns[itag].m_Len = len;
2724 pPatterns[itag].m_pTag = tags.GetPtr() + start; 2714 patterns[itag].m_Offset = 0;
2725 pPatterns[itag].m_Len = len;
2726 pPatterns[itag].m_Offset = 0;
2727 start = i + 1; 2715 start = i + 1;
2728 itag++; 2716 ++itag;
2729 } 2717 }
2730 } 2718 }
2731 FX_FILESIZE pos = m_Pos; 2719
2732 uint8_t byte; 2720 const FX_FILESIZE pos_limit = m_Pos + limit;
2733 GetCharAt(pos++, byte); 2721 for (FX_FILESIZE pos = m_Pos; !limit || pos < pos_limit; ++pos) {
2734 int32_t found = -1; 2722 uint8_t byte;
2735 while (1) { 2723 if (!GetCharAt(pos, byte))
2736 for (i = 0; i < ntags; i++) { 2724 break;
2737 if (pPatterns[i].m_pTag[pPatterns[i].m_Offset] == byte) { 2725
2738 pPatterns[i].m_Offset++; 2726 for (int i = 0; i < ntags; ++i) {
2739 if (pPatterns[i].m_Offset == pPatterns[i].m_Len) { 2727 _SearchTagRecord& pat = patterns[i];
2740 if (!bWholeWord || 2728 if (pat.m_pTag[pat.m_Offset] != byte) {
2741 IsWholeWord(pos - pPatterns[i].m_Len, limit, pPatterns[i].m_pTag, 2729 pat.m_Offset = (pat.m_pTag[0] == byte) ? 1 : 0;
2742 pPatterns[i].m_Len)) { 2730 continue;
2743 found = i;
2744 goto end;
2745 } else {
2746 if (pPatterns[i].m_pTag[0] == byte) {
2747 pPatterns[i].m_Offset = 1;
2748 } else {
2749 pPatterns[i].m_Offset = 0;
2750 }
2751 }
2752 }
2753 } else {
2754 if (pPatterns[i].m_pTag[0] == byte) {
2755 pPatterns[i].m_Offset = 1;
2756 } else {
2757 pPatterns[i].m_Offset = 0;
2758 }
2759 } 2731 }
2732
2733 ++pat.m_Offset;
2734 if (pat.m_Offset != pat.m_Len)
2735 continue;
2736
2737 if (!bWholeWord ||
2738 IsWholeWord(pos - pat.m_Len, limit, pat.m_pTag, pat.m_Len)) {
2739 return i;
2740 }
2741
2742 pat.m_Offset = (pat.m_pTag[0] == byte) ? 1 : 0;
2760 } 2743 }
2761 if (limit && pos >= m_Pos + limit) {
2762 goto end;
2763 }
2764 if (!GetCharAt(pos, byte)) {
2765 goto end;
2766 }
2767 pos++;
2768 } 2744 }
2769 end: 2745 return -1;
2770 FX_Free(pPatterns);
2771 return found;
2772 } 2746 }
2747
2773 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, 2748 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag,
2774 FX_FILESIZE limit) { 2749 FX_FILESIZE limit) {
2775 int32_t taglen = tag.GetLength(); 2750 int32_t taglen = tag.GetLength();
2776 int32_t match = 0; 2751 int32_t match = 0;
2777 limit += m_Pos; 2752 limit += m_Pos;
2778 FX_FILESIZE startpos = m_Pos; 2753 FX_FILESIZE startpos = m_Pos;
2779 while (1) { 2754 while (1) {
2780 uint8_t ch; 2755 uint8_t ch;
2781 if (!GetNextChar(ch)) { 2756 if (!GetNextChar(ch)) {
2782 return -1; 2757 return -1;
(...skipping 24 matching lines...) Expand all
2807 break; 2782 break;
2808 } 2783 }
2809 } 2784 }
2810 } 2785 }
2811 2786
2812 class CPDF_DataAvail final : public IPDF_DataAvail { 2787 class CPDF_DataAvail final : public IPDF_DataAvail {
2813 public: 2788 public:
2814 CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); 2789 CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead);
2815 ~CPDF_DataAvail() override; 2790 ~CPDF_DataAvail() override;
2816 2791
2817 virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) override; 2792 FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) override;
2818 2793
2819 virtual void SetDocument(CPDF_Document* pDoc) override; 2794 void SetDocument(CPDF_Document* pDoc) override;
2820 2795
2821 virtual FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) override; 2796 FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) override;
2822 2797
2823 virtual int32_t IsFormAvail(IFX_DownloadHints* pHints) override; 2798 int32_t IsFormAvail(IFX_DownloadHints* pHints) override;
2824 2799
2825 virtual int32_t IsLinearizedPDF() override; 2800 int32_t IsLinearizedPDF() override;
2826 2801
2827 virtual FX_BOOL IsLinearized() override { return m_bLinearized; } 2802 FX_BOOL IsLinearized() override { return m_bLinearized; }
2828 2803
2829 virtual void GetLinearizedMainXRefInfo(FX_FILESIZE* pPos, 2804 void GetLinearizedMainXRefInfo(FX_FILESIZE* pPos, FX_DWORD* pSize) override;
2830 FX_DWORD* pSize) override;
2831 2805
2832 protected: 2806 protected:
2833 static const int kMaxDataAvailRecursionDepth = 64; 2807 static const int kMaxDataAvailRecursionDepth = 64;
2834 static int s_CurrentDataAvailRecursionDepth; 2808 static int s_CurrentDataAvailRecursionDepth;
2835 2809
2836 FX_DWORD GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset); 2810 FX_DWORD GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset);
2837 FX_BOOL IsObjectsAvail(CFX_PtrArray& obj_array, 2811 FX_BOOL IsObjectsAvail(CFX_PtrArray& obj_array,
2838 FX_BOOL bParsePage, 2812 FX_BOOL bParsePage,
2839 IFX_DownloadHints* pHints, 2813 IFX_DownloadHints* pHints,
2840 CFX_PtrArray& ret_array); 2814 CFX_PtrArray& ret_array);
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
3100 if (pParser->m_V5Type[objnum] == 2) { 3074 if (pParser->m_V5Type[objnum] == 2) {
3101 objnum = (FX_DWORD)pParser->m_CrossRef[objnum]; 3075 objnum = (FX_DWORD)pParser->m_CrossRef[objnum];
3102 } 3076 }
3103 if (pParser->m_V5Type[objnum] == 1 || pParser->m_V5Type[objnum] == 255) { 3077 if (pParser->m_V5Type[objnum] == 1 || pParser->m_V5Type[objnum] == 255) {
3104 offset = pParser->m_CrossRef[objnum]; 3078 offset = pParser->m_CrossRef[objnum];
3105 if (offset == 0) { 3079 if (offset == 0) {
3106 return 0; 3080 return 0;
3107 } 3081 }
3108 void* pResult = FXSYS_bsearch(&offset, pParser->m_SortedOffset.GetData(), 3082 void* pResult = FXSYS_bsearch(&offset, pParser->m_SortedOffset.GetData(),
3109 pParser->m_SortedOffset.GetSize(), 3083 pParser->m_SortedOffset.GetSize(),
3110 sizeof(FX_FILESIZE), _CompareFileSize); 3084 sizeof(FX_FILESIZE), CompareFileSize);
3111 if (pResult == NULL) { 3085 if (pResult == NULL) {
3112 return 0; 3086 return 0;
3113 } 3087 }
3114 if ((FX_FILESIZE*)pResult - 3088 if ((FX_FILESIZE*)pResult -
3115 (FX_FILESIZE*)pParser->m_SortedOffset.GetData() == 3089 (FX_FILESIZE*)pParser->m_SortedOffset.GetData() ==
3116 pParser->m_SortedOffset.GetSize() - 1) { 3090 pParser->m_SortedOffset.GetSize() - 1) {
3117 return 0; 3091 return 0;
3118 } 3092 }
3119 return (FX_DWORD)(((FX_FILESIZE*)pResult)[1] - offset); 3093 return (FX_DWORD)(((FX_FILESIZE*)pResult)[1] - offset);
3120 } 3094 }
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
3352 FX_BOOL CPDF_DataAvail::LoadAllXref(IFX_DownloadHints* pHints) { 3326 FX_BOOL CPDF_DataAvail::LoadAllXref(IFX_DownloadHints* pHints) {
3353 m_parser.m_Syntax.InitParser(m_pFileRead, (FX_DWORD)m_dwHeaderOffset); 3327 m_parser.m_Syntax.InitParser(m_pFileRead, (FX_DWORD)m_dwHeaderOffset);
3354 m_parser.m_bOwnFileRead = FALSE; 3328 m_parser.m_bOwnFileRead = FALSE;
3355 if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) && 3329 if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) &&
3356 !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) { 3330 !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) {
3357 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; 3331 m_docStatus = PDF_DATAAVAIL_LOADALLFILE;
3358 return FALSE; 3332 return FALSE;
3359 } 3333 }
3360 FXSYS_qsort(m_parser.m_SortedOffset.GetData(), 3334 FXSYS_qsort(m_parser.m_SortedOffset.GetData(),
3361 m_parser.m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), 3335 m_parser.m_SortedOffset.GetSize(), sizeof(FX_FILESIZE),
3362 _CompareFileSize); 3336 CompareFileSize);
3363 m_dwRootObjNum = m_parser.GetRootObjNum(); 3337 m_dwRootObjNum = m_parser.GetRootObjNum();
3364 m_dwInfoObjNum = m_parser.GetInfoObjNum(); 3338 m_dwInfoObjNum = m_parser.GetInfoObjNum();
3365 m_pCurrentParser = &m_parser; 3339 m_pCurrentParser = &m_parser;
3366 m_docStatus = PDF_DATAAVAIL_ROOT; 3340 m_docStatus = PDF_DATAAVAIL_ROOT;
3367 return TRUE; 3341 return TRUE;
3368 } 3342 }
3369 CPDF_Object* CPDF_DataAvail::GetObject(FX_DWORD objnum, 3343 CPDF_Object* CPDF_DataAvail::GetObject(FX_DWORD objnum,
3370 IFX_DownloadHints* pHints, 3344 IFX_DownloadHints* pHints,
3371 FX_BOOL* pExistInFile) { 3345 FX_BOOL* pExistInFile) {
3372 CPDF_Object* pRet = NULL; 3346 CPDF_Object* pRet = NULL;
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after
4683 return FALSE; 4657 return FALSE;
4684 } 4658 }
4685 CPDF_PageNode::~CPDF_PageNode() { 4659 CPDF_PageNode::~CPDF_PageNode() {
4686 int32_t iSize = m_childNode.GetSize(); 4660 int32_t iSize = m_childNode.GetSize();
4687 for (int32_t i = 0; i < iSize; ++i) { 4661 for (int32_t i = 0; i < iSize; ++i) {
4688 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; 4662 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i];
4689 delete pNode; 4663 delete pNode;
4690 } 4664 }
4691 m_childNode.RemoveAll(); 4665 m_childNode.RemoveAll();
4692 } 4666 }
OLDNEW
« no previous file with comments | « core/include/fxcrt/fx_basic.h ('k') | core/src/fxcodec/codec/fx_codec_flate.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698