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

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