OLD | NEW |
---|---|
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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/page/cpdf_streamparser.h" | 7 #include "core/fpdfapi/page/cpdf_streamparser.h" |
8 | 8 |
9 #include <limits.h> | 9 #include <limits.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "core/fpdfapi/cpdf_modulemgr.h" | 14 #include "core/fpdfapi/cpdf_modulemgr.h" |
15 #include "core/fpdfapi/page/cpdf_docpagedata.h" | 15 #include "core/fpdfapi/page/cpdf_docpagedata.h" |
16 #include "core/fpdfapi/parser/cpdf_array.h" | 16 #include "core/fpdfapi/parser/cpdf_array.h" |
17 #include "core/fpdfapi/parser/cpdf_boolean.h" | 17 #include "core/fpdfapi/parser/cpdf_boolean.h" |
18 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 18 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
19 #include "core/fpdfapi/parser/cpdf_document.h" | 19 #include "core/fpdfapi/parser/cpdf_document.h" |
20 #include "core/fpdfapi/parser/cpdf_name.h" | 20 #include "core/fpdfapi/parser/cpdf_name.h" |
21 #include "core/fpdfapi/parser/cpdf_null.h" | 21 #include "core/fpdfapi/parser/cpdf_null.h" |
22 #include "core/fpdfapi/parser/cpdf_number.h" | 22 #include "core/fpdfapi/parser/cpdf_number.h" |
23 #include "core/fpdfapi/parser/cpdf_stream.h" | 23 #include "core/fpdfapi/parser/cpdf_stream.h" |
24 #include "core/fpdfapi/parser/cpdf_string.h" | 24 #include "core/fpdfapi/parser/cpdf_string.h" |
25 #include "core/fpdfapi/parser/fpdf_parser_decode.h" | 25 #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
26 #include "core/fpdfapi/parser/fpdf_parser_utility.h" | 26 #include "core/fpdfapi/parser/fpdf_parser_utility.h" |
27 #include "core/fxcodec/fx_codec.h" | 27 #include "core/fxcodec/fx_codec.h" |
28 #include "core/fxcrt/fx_ext.h" | 28 #include "core/fxcrt/fx_ext.h" |
29 | 29 |
30 CCodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( | |
Tom Sepez
2016/12/13 19:21:19
Note: moved to .h, sheesh.
| |
31 const uint8_t* src_buf, | |
32 uint32_t src_size, | |
33 int width, | |
34 int height, | |
35 const CPDF_Dictionary* pParams); | |
36 | |
37 namespace { | 30 namespace { |
38 | 31 |
39 const uint32_t kMaxNestedArrayLevel = 512; | 32 const uint32_t kMaxNestedArrayLevel = 512; |
40 const uint32_t kMaxWordBuffer = 256; | 33 const uint32_t kMaxWordBuffer = 256; |
41 const FX_STRSIZE kMaxStringLength = 32767; | 34 const FX_STRSIZE kMaxStringLength = 32767; |
42 | 35 |
43 uint32_t DecodeAllScanlines(CCodec_ScanlineDecoder* pDecoder, | 36 uint32_t DecodeAllScanlines(std::unique_ptr<CCodec_ScanlineDecoder> pDecoder, |
44 uint8_t*& dest_buf, | 37 uint8_t*& dest_buf, |
45 uint32_t& dest_size) { | 38 uint32_t& dest_size) { |
46 if (!pDecoder) | 39 if (!pDecoder) |
47 return FX_INVALID_OFFSET; | 40 return FX_INVALID_OFFSET; |
48 int ncomps = pDecoder->CountComps(); | 41 int ncomps = pDecoder->CountComps(); |
49 int bpc = pDecoder->GetBPC(); | 42 int bpc = pDecoder->GetBPC(); |
50 int width = pDecoder->GetWidth(); | 43 int width = pDecoder->GetWidth(); |
51 int height = pDecoder->GetHeight(); | 44 int height = pDecoder->GetHeight(); |
52 int pitch = (width * ncomps * bpc + 7) / 8; | 45 int pitch = (width * ncomps * bpc + 7) / 8; |
53 if (height == 0 || pitch > (1 << 30) / height) { | 46 if (height == 0 || pitch > (1 << 30) / height) |
54 delete pDecoder; | |
55 return FX_INVALID_OFFSET; | 47 return FX_INVALID_OFFSET; |
56 } | 48 |
57 dest_buf = FX_Alloc2D(uint8_t, pitch, height); | 49 dest_buf = FX_Alloc2D(uint8_t, pitch, height); |
58 dest_size = pitch * height; // Safe since checked alloc returned. | 50 dest_size = pitch * height; // Safe since checked alloc returned. |
59 for (int row = 0; row < height; row++) { | 51 for (int row = 0; row < height; row++) { |
60 const uint8_t* pLine = pDecoder->GetScanline(row); | 52 const uint8_t* pLine = pDecoder->GetScanline(row); |
61 if (!pLine) | 53 if (!pLine) |
62 break; | 54 break; |
63 | 55 |
64 FXSYS_memcpy(dest_buf + row * pitch, pLine, pitch); | 56 FXSYS_memcpy(dest_buf + row * pitch, pLine, pitch); |
65 } | 57 } |
66 uint32_t srcoff = pDecoder->GetSrcOffset(); | 58 return pDecoder->GetSrcOffset(); |
67 delete pDecoder; | |
68 return srcoff; | |
69 } | 59 } |
70 | 60 |
71 uint32_t PDF_DecodeInlineStream(const uint8_t* src_buf, | 61 uint32_t PDF_DecodeInlineStream(const uint8_t* src_buf, |
72 uint32_t limit, | 62 uint32_t limit, |
73 int width, | 63 int width, |
74 int height, | 64 int height, |
75 CFX_ByteString& decoder, | 65 CFX_ByteString& decoder, |
76 CPDF_Dictionary* pParam, | 66 CPDF_Dictionary* pParam, |
77 uint8_t*& dest_buf, | 67 uint8_t*& dest_buf, |
78 uint32_t& dest_size) { | 68 uint32_t& dest_size) { |
79 if (decoder == "CCITTFaxDecode" || decoder == "CCF") { | 69 if (decoder == "CCITTFaxDecode" || decoder == "CCF") { |
80 CCodec_ScanlineDecoder* pDecoder = | 70 std::unique_ptr<CCodec_ScanlineDecoder> pDecoder = |
81 FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); | 71 FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); |
82 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); | 72 return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size); |
83 } | 73 } |
84 if (decoder == "ASCII85Decode" || decoder == "A85") | 74 if (decoder == "ASCII85Decode" || decoder == "A85") |
85 return A85Decode(src_buf, limit, dest_buf, dest_size); | 75 return A85Decode(src_buf, limit, dest_buf, dest_size); |
86 if (decoder == "ASCIIHexDecode" || decoder == "AHx") | 76 if (decoder == "ASCIIHexDecode" || decoder == "AHx") |
87 return HexDecode(src_buf, limit, dest_buf, dest_size); | 77 return HexDecode(src_buf, limit, dest_buf, dest_size); |
88 if (decoder == "FlateDecode" || decoder == "Fl") { | 78 if (decoder == "FlateDecode" || decoder == "Fl") { |
89 return FPDFAPI_FlateOrLZWDecode(false, src_buf, limit, pParam, dest_size, | 79 return FPDFAPI_FlateOrLZWDecode(false, src_buf, limit, pParam, dest_size, |
90 dest_buf, dest_size); | 80 dest_buf, dest_size); |
91 } | 81 } |
92 if (decoder == "LZWDecode" || decoder == "LZW") { | 82 if (decoder == "LZWDecode" || decoder == "LZW") { |
93 return FPDFAPI_FlateOrLZWDecode(true, src_buf, limit, pParam, 0, dest_buf, | 83 return FPDFAPI_FlateOrLZWDecode(true, src_buf, limit, pParam, 0, dest_buf, |
94 dest_size); | 84 dest_size); |
95 } | 85 } |
96 if (decoder == "DCTDecode" || decoder == "DCT") { | 86 if (decoder == "DCTDecode" || decoder == "DCT") { |
97 CCodec_ScanlineDecoder* pDecoder = | 87 std::unique_ptr<CCodec_ScanlineDecoder> pDecoder = |
98 CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder( | 88 CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder( |
99 src_buf, limit, width, height, 0, | 89 src_buf, limit, width, height, 0, |
100 !pParam || pParam->GetIntegerFor("ColorTransform", 1)); | 90 !pParam || pParam->GetIntegerFor("ColorTransform", 1)); |
101 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); | 91 return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size); |
102 } | 92 } |
103 if (decoder == "RunLengthDecode" || decoder == "RL") | 93 if (decoder == "RunLengthDecode" || decoder == "RL") |
104 return RunLengthDecode(src_buf, limit, dest_buf, dest_size); | 94 return RunLengthDecode(src_buf, limit, dest_buf, dest_size); |
105 dest_size = 0; | 95 dest_size = 0; |
106 dest_buf = 0; | 96 dest_buf = 0; |
107 return (uint32_t)-1; | 97 return (uint32_t)-1; |
108 } | 98 } |
109 | 99 |
110 } // namespace | 100 } // namespace |
111 | 101 |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
605 | 595 |
606 if (buf.GetLength() > kMaxStringLength) | 596 if (buf.GetLength() > kMaxStringLength) |
607 return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); | 597 return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); |
608 | 598 |
609 return buf.MakeString(); | 599 return buf.MakeString(); |
610 } | 600 } |
611 | 601 |
612 bool CPDF_StreamParser::PositionIsInBounds() const { | 602 bool CPDF_StreamParser::PositionIsInBounds() const { |
613 return m_Pos < m_Size; | 603 return m_Pos < m_Size; |
614 } | 604 } |
OLD | NEW |