| 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 "core/fpdfapi/parser/fpdf_parser_decode.h" | 7 #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
| 8 | 8 |
| 9 #include <limits.h> | 9 #include <limits.h> |
| 10 | 10 |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 Columns = pParams->GetIntegerFor("Columns", 1); | 290 Columns = pParams->GetIntegerFor("Columns", 1); |
| 291 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) { | 291 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) { |
| 292 return nullptr; | 292 return nullptr; |
| 293 } | 293 } |
| 294 } | 294 } |
| 295 return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder( | 295 return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder( |
| 296 src_buf, src_size, width, height, nComps, bpc, predictor, Colors, | 296 src_buf, src_size, width, height, nComps, bpc, predictor, Colors, |
| 297 BitsPerComponent, Columns); | 297 BitsPerComponent, Columns); |
| 298 } | 298 } |
| 299 | 299 |
| 300 uint32_t FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, | 300 uint32_t FPDFAPI_FlateOrLZWDecode(bool bLZW, |
| 301 const uint8_t* src_buf, | 301 const uint8_t* src_buf, |
| 302 uint32_t src_size, | 302 uint32_t src_size, |
| 303 CPDF_Dictionary* pParams, | 303 CPDF_Dictionary* pParams, |
| 304 uint32_t estimated_size, | 304 uint32_t estimated_size, |
| 305 uint8_t*& dest_buf, | 305 uint8_t*& dest_buf, |
| 306 uint32_t& dest_size) { | 306 uint32_t& dest_size) { |
| 307 int predictor = 0; | 307 int predictor = 0; |
| 308 int Colors = 0; | 308 int Colors = 0; |
| 309 int BitsPerComponent = 0; | 309 int BitsPerComponent = 0; |
| 310 int Columns = 0; | 310 int Columns = 0; |
| 311 FX_BOOL bEarlyChange = TRUE; | 311 bool bEarlyChange = true; |
| 312 if (pParams) { | 312 if (pParams) { |
| 313 predictor = pParams->GetIntegerFor("Predictor"); | 313 predictor = pParams->GetIntegerFor("Predictor"); |
| 314 bEarlyChange = !!pParams->GetIntegerFor("EarlyChange", 1); | 314 bEarlyChange = !!pParams->GetIntegerFor("EarlyChange", 1); |
| 315 Colors = pParams->GetIntegerFor("Colors", 1); | 315 Colors = pParams->GetIntegerFor("Colors", 1); |
| 316 BitsPerComponent = pParams->GetIntegerFor("BitsPerComponent", 8); | 316 BitsPerComponent = pParams->GetIntegerFor("BitsPerComponent", 8); |
| 317 Columns = pParams->GetIntegerFor("Columns", 1); | 317 Columns = pParams->GetIntegerFor("Columns", 1); |
| 318 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) | 318 if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) |
| 319 return (uint32_t)-1; | 319 return (uint32_t)-1; |
| 320 } | 320 } |
| 321 return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode( | 321 return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode( |
| 322 bLZW, src_buf, src_size, bEarlyChange, predictor, Colors, | 322 bLZW, src_buf, src_size, bEarlyChange, predictor, Colors, |
| 323 BitsPerComponent, Columns, estimated_size, dest_buf, dest_size); | 323 BitsPerComponent, Columns, estimated_size, dest_buf, dest_size); |
| 324 } | 324 } |
| 325 | 325 |
| 326 FX_BOOL PDF_DataDecode(const uint8_t* src_buf, | 326 bool PDF_DataDecode(const uint8_t* src_buf, |
| 327 uint32_t src_size, | 327 uint32_t src_size, |
| 328 const CPDF_Dictionary* pDict, | 328 const CPDF_Dictionary* pDict, |
| 329 uint8_t*& dest_buf, | 329 uint8_t*& dest_buf, |
| 330 uint32_t& dest_size, | 330 uint32_t& dest_size, |
| 331 CFX_ByteString& ImageEncoding, | 331 CFX_ByteString& ImageEncoding, |
| 332 CPDF_Dictionary*& pImageParms, | 332 CPDF_Dictionary*& pImageParms, |
| 333 uint32_t last_estimated_size, | 333 uint32_t last_estimated_size, |
| 334 FX_BOOL bImageAcc) { | 334 bool bImageAcc) { |
| 335 CPDF_Object* pDecoder = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; | 335 CPDF_Object* pDecoder = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| 336 if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName())) | 336 if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName())) |
| 337 return FALSE; | 337 return false; |
| 338 | 338 |
| 339 CPDF_Object* pParams = | 339 CPDF_Object* pParams = |
| 340 pDict ? pDict->GetDirectObjectFor("DecodeParms") : nullptr; | 340 pDict ? pDict->GetDirectObjectFor("DecodeParms") : nullptr; |
| 341 | 341 |
| 342 std::vector<std::pair<CFX_ByteString, CPDF_Object*>> DecoderArray; | 342 std::vector<std::pair<CFX_ByteString, CPDF_Object*>> DecoderArray; |
| 343 if (CPDF_Array* pDecoders = pDecoder->AsArray()) { | 343 if (CPDF_Array* pDecoders = pDecoder->AsArray()) { |
| 344 CPDF_Array* pParamsArray = ToArray(pParams); | 344 CPDF_Array* pParamsArray = ToArray(pParams); |
| 345 for (size_t i = 0; i < pDecoders->GetCount(); i++) { | 345 for (size_t i = 0; i < pDecoders->GetCount(); i++) { |
| 346 DecoderArray.push_back( | 346 DecoderArray.push_back( |
| 347 {pDecoders->GetStringAt(i), | 347 {pDecoders->GetStringAt(i), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 360 CPDF_Dictionary* pParam = ToDictionary(DecoderArray[i].second); | 360 CPDF_Dictionary* pParam = ToDictionary(DecoderArray[i].second); |
| 361 uint8_t* new_buf = nullptr; | 361 uint8_t* new_buf = nullptr; |
| 362 uint32_t new_size = (uint32_t)-1; | 362 uint32_t new_size = (uint32_t)-1; |
| 363 int offset = -1; | 363 int offset = -1; |
| 364 if (decoder == "FlateDecode" || decoder == "Fl") { | 364 if (decoder == "FlateDecode" || decoder == "Fl") { |
| 365 if (bImageAcc && i == nSize - 1) { | 365 if (bImageAcc && i == nSize - 1) { |
| 366 ImageEncoding = "FlateDecode"; | 366 ImageEncoding = "FlateDecode"; |
| 367 dest_buf = (uint8_t*)last_buf; | 367 dest_buf = (uint8_t*)last_buf; |
| 368 dest_size = last_size; | 368 dest_size = last_size; |
| 369 pImageParms = pParam; | 369 pImageParms = pParam; |
| 370 return TRUE; | 370 return true; |
| 371 } | 371 } |
| 372 offset = FPDFAPI_FlateOrLZWDecode(FALSE, last_buf, last_size, pParam, | 372 offset = FPDFAPI_FlateOrLZWDecode(false, last_buf, last_size, pParam, |
| 373 estimated_size, new_buf, new_size); | 373 estimated_size, new_buf, new_size); |
| 374 } else if (decoder == "LZWDecode" || decoder == "LZW") { | 374 } else if (decoder == "LZWDecode" || decoder == "LZW") { |
| 375 offset = FPDFAPI_FlateOrLZWDecode(TRUE, last_buf, last_size, pParam, | 375 offset = FPDFAPI_FlateOrLZWDecode(true, last_buf, last_size, pParam, |
| 376 estimated_size, new_buf, new_size); | 376 estimated_size, new_buf, new_size); |
| 377 } else if (decoder == "ASCII85Decode" || decoder == "A85") { | 377 } else if (decoder == "ASCII85Decode" || decoder == "A85") { |
| 378 offset = A85Decode(last_buf, last_size, new_buf, new_size); | 378 offset = A85Decode(last_buf, last_size, new_buf, new_size); |
| 379 } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") { | 379 } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") { |
| 380 offset = HexDecode(last_buf, last_size, new_buf, new_size); | 380 offset = HexDecode(last_buf, last_size, new_buf, new_size); |
| 381 } else if (decoder == "RunLengthDecode" || decoder == "RL") { | 381 } else if (decoder == "RunLengthDecode" || decoder == "RL") { |
| 382 if (bImageAcc && i == nSize - 1) { | 382 if (bImageAcc && i == nSize - 1) { |
| 383 ImageEncoding = "RunLengthDecode"; | 383 ImageEncoding = "RunLengthDecode"; |
| 384 dest_buf = (uint8_t*)last_buf; | 384 dest_buf = (uint8_t*)last_buf; |
| 385 dest_size = last_size; | 385 dest_size = last_size; |
| 386 pImageParms = pParam; | 386 pImageParms = pParam; |
| 387 return TRUE; | 387 return true; |
| 388 } | 388 } |
| 389 offset = RunLengthDecode(last_buf, last_size, new_buf, new_size); | 389 offset = RunLengthDecode(last_buf, last_size, new_buf, new_size); |
| 390 } else if (decoder == "Crypt") { | 390 } else if (decoder == "Crypt") { |
| 391 continue; | 391 continue; |
| 392 } else { | 392 } else { |
| 393 // If we get here, assume it's an image decoder. | 393 // If we get here, assume it's an image decoder. |
| 394 if (decoder == "DCT") { | 394 if (decoder == "DCT") { |
| 395 decoder = "DCTDecode"; | 395 decoder = "DCTDecode"; |
| 396 } else if (decoder == "CCF") { | 396 } else if (decoder == "CCF") { |
| 397 decoder = "CCITTFaxDecode"; | 397 decoder = "CCITTFaxDecode"; |
| 398 } | 398 } |
| 399 ImageEncoding = decoder; | 399 ImageEncoding = decoder; |
| 400 pImageParms = pParam; | 400 pImageParms = pParam; |
| 401 dest_buf = (uint8_t*)last_buf; | 401 dest_buf = (uint8_t*)last_buf; |
| 402 dest_size = last_size; | 402 dest_size = last_size; |
| 403 if (CPDF_Array* pDecoders = pDecoder->AsArray()) | 403 if (CPDF_Array* pDecoders = pDecoder->AsArray()) |
| 404 pDecoders->RemoveAt(i + 1, pDecoders->GetCount() - i - 1); | 404 pDecoders->RemoveAt(i + 1, pDecoders->GetCount() - i - 1); |
| 405 return TRUE; | 405 return true; |
| 406 } | 406 } |
| 407 if (last_buf != src_buf) { | 407 if (last_buf != src_buf) { |
| 408 FX_Free(last_buf); | 408 FX_Free(last_buf); |
| 409 } | 409 } |
| 410 if (offset == -1) { | 410 if (offset == -1) { |
| 411 FX_Free(new_buf); | 411 FX_Free(new_buf); |
| 412 return FALSE; | 412 return false; |
| 413 } | 413 } |
| 414 last_buf = new_buf; | 414 last_buf = new_buf; |
| 415 last_size = new_size; | 415 last_size = new_size; |
| 416 } | 416 } |
| 417 ImageEncoding = ""; | 417 ImageEncoding = ""; |
| 418 pImageParms = nullptr; | 418 pImageParms = nullptr; |
| 419 dest_buf = last_buf; | 419 dest_buf = last_buf; |
| 420 dest_size = last_size; | 420 dest_size = last_size; |
| 421 return TRUE; | 421 return true; |
| 422 } | 422 } |
| 423 | 423 |
| 424 CFX_WideString PDF_DecodeText(const uint8_t* src_data, uint32_t src_len) { | 424 CFX_WideString PDF_DecodeText(const uint8_t* src_data, uint32_t src_len) { |
| 425 CFX_WideString result; | 425 CFX_WideString result; |
| 426 if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) || | 426 if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) || |
| 427 (src_data[0] == 0xff && src_data[1] == 0xfe))) { | 427 (src_data[0] == 0xff && src_data[1] == 0xfe))) { |
| 428 bool bBE = src_data[0] == 0xfe; | 428 bool bBE = src_data[0] == 0xfe; |
| 429 uint32_t max_chars = (src_len - 2) / 2; | 429 uint32_t max_chars = (src_len - 2) / 2; |
| 430 if (!max_chars) { | 430 if (!max_chars) { |
| 431 return result; | 431 return result; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 *dest_buf2++ = (uint8_t)pString[j]; | 505 *dest_buf2++ = (uint8_t)pString[j]; |
| 506 } | 506 } |
| 507 result.ReleaseBuffer(encLen); | 507 result.ReleaseBuffer(encLen); |
| 508 return result; | 508 return result; |
| 509 } | 509 } |
| 510 | 510 |
| 511 CFX_ByteString PDF_EncodeText(const CFX_WideString& str) { | 511 CFX_ByteString PDF_EncodeText(const CFX_WideString& str) { |
| 512 return PDF_EncodeText(str.c_str(), str.GetLength()); | 512 return PDF_EncodeText(str.c_str(), str.GetLength()); |
| 513 } | 513 } |
| 514 | 514 |
| 515 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) { | 515 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, bool bHex) { |
| 516 CFX_ByteTextBuf result; | 516 CFX_ByteTextBuf result; |
| 517 int srclen = src.GetLength(); | 517 int srclen = src.GetLength(); |
| 518 if (bHex) { | 518 if (bHex) { |
| 519 result.AppendChar('<'); | 519 result.AppendChar('<'); |
| 520 for (int i = 0; i < srclen; i++) { | 520 for (int i = 0; i < srclen; i++) { |
| 521 result.AppendChar("0123456789ABCDEF"[src[i] / 16]); | 521 result.AppendChar("0123456789ABCDEF"[src[i] / 16]); |
| 522 result.AppendChar("0123456789ABCDEF"[src[i] % 16]); | 522 result.AppendChar("0123456789ABCDEF"[src[i] % 16]); |
| 523 } | 523 } |
| 524 result.AppendChar('>'); | 524 result.AppendChar('>'); |
| 525 return result.MakeString(); | 525 return result.MakeString(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 dest_size); | 563 dest_size); |
| 564 } | 564 } |
| 565 | 565 |
| 566 uint32_t FlateDecode(const uint8_t* src_buf, | 566 uint32_t FlateDecode(const uint8_t* src_buf, |
| 567 uint32_t src_size, | 567 uint32_t src_size, |
| 568 uint8_t*& dest_buf, | 568 uint8_t*& dest_buf, |
| 569 uint32_t& dest_size) { | 569 uint32_t& dest_size) { |
| 570 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); | 570 CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); |
| 571 if (pEncoders) { | 571 if (pEncoders) { |
| 572 return pEncoders->GetFlateModule()->FlateOrLZWDecode( | 572 return pEncoders->GetFlateModule()->FlateOrLZWDecode( |
| 573 FALSE, src_buf, src_size, FALSE, 0, 0, 0, 0, 0, dest_buf, dest_size); | 573 false, src_buf, src_size, false, 0, 0, 0, 0, 0, dest_buf, dest_size); |
| 574 } | 574 } |
| 575 return 0; | 575 return 0; |
| 576 } | 576 } |
| OLD | NEW |