| 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 <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include "core/include/fpdfapi/fpdf_module.h" | 9 #include "core/include/fpdfapi/fpdf_module.h" |
| 10 #include "core/include/fpdfapi/fpdf_parser.h" | 10 #include "core/include/fpdfapi/fpdf_parser.h" |
| 11 #include "core/include/fxcodec/fx_codec.h" | 11 #include "core/include/fxcodec/fx_codec.h" |
| 12 #include "core/include/fxcrt/fx_ext.h" | 12 #include "core/include/fxcrt/fx_ext.h" |
| 13 #include "third_party/base/stl_util.h" |
| 13 | 14 |
| 14 #define _STREAM_MAX_SIZE_ 20 * 1024 * 1024 | 15 #define _STREAM_MAX_SIZE_ 20 * 1024 * 1024 |
| 15 | 16 |
| 16 const FX_WORD PDFDocEncoding[256] = { | 17 const FX_WORD PDFDocEncoding[256] = { |
| 17 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, | 18 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, |
| 18 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, | 19 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, |
| 19 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6, | 20 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x02d8, 0x02c7, 0x02c6, |
| 20 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023, | 21 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc, 0x0020, 0x0021, 0x0022, 0x0023, |
| 21 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, | 22 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, |
| 22 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, | 23 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 CFX_ByteString& ImageEncoding, | 333 CFX_ByteString& ImageEncoding, |
| 333 CPDF_Dictionary*& pImageParms, | 334 CPDF_Dictionary*& pImageParms, |
| 334 FX_DWORD last_estimated_size, | 335 FX_DWORD last_estimated_size, |
| 335 FX_BOOL bImageAcc) { | 336 FX_BOOL bImageAcc) { |
| 336 CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr; | 337 CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr; |
| 337 if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName())) | 338 if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName())) |
| 338 return FALSE; | 339 return FALSE; |
| 339 | 340 |
| 340 CPDF_Object* pParams = | 341 CPDF_Object* pParams = |
| 341 pDict ? pDict->GetElementValue("DecodeParms") : nullptr; | 342 pDict ? pDict->GetElementValue("DecodeParms") : nullptr; |
| 342 CFX_ByteStringArray DecoderList; | 343 std::vector<CFX_ByteString> DecoderList; |
| 343 CFX_ArrayTemplate<CPDF_Object*> ParamList; | 344 CFX_ArrayTemplate<CPDF_Object*> ParamList; |
| 344 if (CPDF_Array* pDecoders = pDecoder->AsArray()) { | 345 if (CPDF_Array* pDecoders = pDecoder->AsArray()) { |
| 345 CPDF_Array* pParamsArray = ToArray(pParams); | 346 CPDF_Array* pParamsArray = ToArray(pParams); |
| 346 if (!pParamsArray) | 347 if (!pParamsArray) |
| 347 pParams = nullptr; | 348 pParams = nullptr; |
| 348 | 349 |
| 349 for (FX_DWORD i = 0; i < pDecoders->GetCount(); i++) { | 350 for (FX_DWORD i = 0; i < pDecoders->GetCount(); i++) { |
| 350 CFX_ByteStringC str = pDecoders->GetConstStringAt(i); | 351 DecoderList.push_back(pDecoders->GetConstStringAt(i)); |
| 351 DecoderList.Add(str); | |
| 352 ParamList.Add(pParams ? pParamsArray->GetDictAt(i) : nullptr); | 352 ParamList.Add(pParams ? pParamsArray->GetDictAt(i) : nullptr); |
| 353 } | 353 } |
| 354 } else { | 354 } else { |
| 355 DecoderList.Add(pDecoder->GetConstString()); | 355 DecoderList.push_back(pDecoder->GetConstString()); |
| 356 ParamList.Add(pParams ? pParams->GetDict() : nullptr); | 356 ParamList.Add(pParams ? pParams->GetDict() : nullptr); |
| 357 } | 357 } |
| 358 uint8_t* last_buf = (uint8_t*)src_buf; | 358 uint8_t* last_buf = (uint8_t*)src_buf; |
| 359 FX_DWORD last_size = src_size; | 359 FX_DWORD last_size = src_size; |
| 360 for (int i = 0; i < DecoderList.GetSize(); i++) { | 360 int nSize = pdfium::CollectionSize<int>(DecoderList); |
| 361 int estimated_size = | 361 for (int i = 0; i < nSize; i++) { |
| 362 i == DecoderList.GetSize() - 1 ? last_estimated_size : 0; | 362 int estimated_size = i == nSize - 1 ? last_estimated_size : 0; |
| 363 CFX_ByteString decoder = DecoderList[i]; | 363 CFX_ByteString decoder = DecoderList[i]; |
| 364 // Use ToDictionary here because we can push nullptr into the ParamList. | 364 // Use ToDictionary here because we can push nullptr into the ParamList. |
| 365 CPDF_Dictionary* pParam = ToDictionary(ParamList[i]); | 365 CPDF_Dictionary* pParam = ToDictionary(ParamList[i]); |
| 366 uint8_t* new_buf = nullptr; | 366 uint8_t* new_buf = nullptr; |
| 367 FX_DWORD new_size = (FX_DWORD)-1; | 367 FX_DWORD new_size = (FX_DWORD)-1; |
| 368 int offset = -1; | 368 int offset = -1; |
| 369 if (decoder == "FlateDecode" || decoder == "Fl") { | 369 if (decoder == "FlateDecode" || decoder == "Fl") { |
| 370 if (bImageAcc && i == DecoderList.GetSize() - 1) { | 370 if (bImageAcc && i == nSize - 1) { |
| 371 ImageEncoding = "FlateDecode"; | 371 ImageEncoding = "FlateDecode"; |
| 372 dest_buf = (uint8_t*)last_buf; | 372 dest_buf = (uint8_t*)last_buf; |
| 373 dest_size = last_size; | 373 dest_size = last_size; |
| 374 pImageParms = pParam; | 374 pImageParms = pParam; |
| 375 return TRUE; | 375 return TRUE; |
| 376 } | 376 } |
| 377 offset = FPDFAPI_FlateOrLZWDecode(FALSE, last_buf, last_size, pParam, | 377 offset = FPDFAPI_FlateOrLZWDecode(FALSE, last_buf, last_size, pParam, |
| 378 estimated_size, new_buf, new_size); | 378 estimated_size, new_buf, new_size); |
| 379 } else if (decoder == "LZWDecode" || decoder == "LZW") { | 379 } else if (decoder == "LZWDecode" || decoder == "LZW") { |
| 380 offset = FPDFAPI_FlateOrLZWDecode(TRUE, last_buf, last_size, pParam, | 380 offset = FPDFAPI_FlateOrLZWDecode(TRUE, last_buf, last_size, pParam, |
| 381 estimated_size, new_buf, new_size); | 381 estimated_size, new_buf, new_size); |
| 382 } else if (decoder == "ASCII85Decode" || decoder == "A85") { | 382 } else if (decoder == "ASCII85Decode" || decoder == "A85") { |
| 383 offset = A85Decode(last_buf, last_size, new_buf, new_size); | 383 offset = A85Decode(last_buf, last_size, new_buf, new_size); |
| 384 } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") { | 384 } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") { |
| 385 offset = HexDecode(last_buf, last_size, new_buf, new_size); | 385 offset = HexDecode(last_buf, last_size, new_buf, new_size); |
| 386 } else if (decoder == "RunLengthDecode" || decoder == "RL") { | 386 } else if (decoder == "RunLengthDecode" || decoder == "RL") { |
| 387 if (bImageAcc && i == DecoderList.GetSize() - 1) { | 387 if (bImageAcc && i == nSize - 1) { |
| 388 ImageEncoding = "RunLengthDecode"; | 388 ImageEncoding = "RunLengthDecode"; |
| 389 dest_buf = (uint8_t*)last_buf; | 389 dest_buf = (uint8_t*)last_buf; |
| 390 dest_size = last_size; | 390 dest_size = last_size; |
| 391 pImageParms = pParam; | 391 pImageParms = pParam; |
| 392 return TRUE; | 392 return TRUE; |
| 393 } | 393 } |
| 394 offset = RunLengthDecode(last_buf, last_size, new_buf, new_size); | 394 offset = RunLengthDecode(last_buf, last_size, new_buf, new_size); |
| 395 } else if (decoder == "Crypt") { | 395 } else if (decoder == "Crypt") { |
| 396 continue; | 396 continue; |
| 397 } else { | 397 } else { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 FX_DWORD src_size, | 580 FX_DWORD src_size, |
| 581 uint8_t*& dest_buf, | 581 uint8_t*& dest_buf, |
| 582 FX_DWORD& dest_size) { | 582 FX_DWORD& dest_size) { |
| 583 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); | 583 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); |
| 584 if (pEncoders) { | 584 if (pEncoders) { |
| 585 return pEncoders->GetFlateModule()->FlateOrLZWDecode( | 585 return pEncoders->GetFlateModule()->FlateOrLZWDecode( |
| 586 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); | 586 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); |
| 587 } | 587 } |
| 588 return 0; | 588 return 0; |
| 589 } | 589 } |
| OLD | NEW |