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