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/fpdf_page/pageint.h" | 7 #include "core/fpdfapi/fpdf_page/pageint.h" |
8 | 8 |
9 #include <limits.h> | 9 #include <limits.h> |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" | 24 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
25 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" | 25 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
26 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" | 26 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" |
27 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | 27 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
28 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 28 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
29 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 29 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
30 #include "core/fxcrt/include/fx_ext.h" | 30 #include "core/fxcrt/include/fx_ext.h" |
31 #include "core/fxcrt/include/fx_safe_types.h" | 31 #include "core/fxcrt/include/fx_safe_types.h" |
32 #include "core/include/fxcodec/fx_codec.h" | 32 #include "core/include/fxcodec/fx_codec.h" |
33 | 33 |
34 CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, FX_DWORD dwSize) { | 34 CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize) { |
35 m_pBuf = pData; | 35 m_pBuf = pData; |
36 m_Size = dwSize; | 36 m_Size = dwSize; |
37 m_Pos = 0; | 37 m_Pos = 0; |
38 m_pLastObj = NULL; | 38 m_pLastObj = NULL; |
39 } | 39 } |
40 | 40 |
41 CPDF_StreamParser::~CPDF_StreamParser() { | 41 CPDF_StreamParser::~CPDF_StreamParser() { |
42 if (m_pLastObj) { | 42 if (m_pLastObj) { |
43 m_pLastObj->Release(); | 43 m_pLastObj->Release(); |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 FX_DWORD DecodeAllScanlines(ICodec_ScanlineDecoder* pDecoder, | 47 uint32_t DecodeAllScanlines(ICodec_ScanlineDecoder* pDecoder, |
48 uint8_t*& dest_buf, | 48 uint8_t*& dest_buf, |
49 FX_DWORD& dest_size) { | 49 uint32_t& dest_size) { |
50 if (!pDecoder) { | 50 if (!pDecoder) { |
51 return static_cast<FX_DWORD>(-1); | 51 return static_cast<uint32_t>(-1); |
52 } | 52 } |
53 int ncomps = pDecoder->CountComps(); | 53 int ncomps = pDecoder->CountComps(); |
54 int bpc = pDecoder->GetBPC(); | 54 int bpc = pDecoder->GetBPC(); |
55 int width = pDecoder->GetWidth(); | 55 int width = pDecoder->GetWidth(); |
56 int height = pDecoder->GetHeight(); | 56 int height = pDecoder->GetHeight(); |
57 int pitch = (width * ncomps * bpc + 7) / 8; | 57 int pitch = (width * ncomps * bpc + 7) / 8; |
58 if (height == 0 || pitch > (1 << 30) / height) { | 58 if (height == 0 || pitch > (1 << 30) / height) { |
59 delete pDecoder; | 59 delete pDecoder; |
60 return static_cast<FX_DWORD>(-1); | 60 return static_cast<uint32_t>(-1); |
61 } | 61 } |
62 dest_buf = FX_Alloc2D(uint8_t, pitch, height); | 62 dest_buf = FX_Alloc2D(uint8_t, pitch, height); |
63 dest_size = pitch * height; // Safe since checked alloc returned. | 63 dest_size = pitch * height; // Safe since checked alloc returned. |
64 for (int row = 0; row < height; row++) { | 64 for (int row = 0; row < height; row++) { |
65 const uint8_t* pLine = pDecoder->GetScanline(row); | 65 const uint8_t* pLine = pDecoder->GetScanline(row); |
66 if (!pLine) | 66 if (!pLine) |
67 break; | 67 break; |
68 | 68 |
69 FXSYS_memcpy(dest_buf + row * pitch, pLine, pitch); | 69 FXSYS_memcpy(dest_buf + row * pitch, pLine, pitch); |
70 } | 70 } |
71 FX_DWORD srcoff = pDecoder->GetSrcOffset(); | 71 uint32_t srcoff = pDecoder->GetSrcOffset(); |
72 delete pDecoder; | 72 delete pDecoder; |
73 return srcoff; | 73 return srcoff; |
74 } | 74 } |
75 | 75 |
76 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( | 76 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( |
77 const uint8_t* src_buf, | 77 const uint8_t* src_buf, |
78 FX_DWORD src_size, | 78 uint32_t src_size, |
79 int width, | 79 int width, |
80 int height, | 80 int height, |
81 const CPDF_Dictionary* pParams); | 81 const CPDF_Dictionary* pParams); |
82 | 82 |
83 FX_DWORD PDF_DecodeInlineStream(const uint8_t* src_buf, | 83 uint32_t PDF_DecodeInlineStream(const uint8_t* src_buf, |
84 FX_DWORD limit, | 84 uint32_t limit, |
85 int width, | 85 int width, |
86 int height, | 86 int height, |
87 CFX_ByteString& decoder, | 87 CFX_ByteString& decoder, |
88 CPDF_Dictionary* pParam, | 88 CPDF_Dictionary* pParam, |
89 uint8_t*& dest_buf, | 89 uint8_t*& dest_buf, |
90 FX_DWORD& dest_size) { | 90 uint32_t& dest_size) { |
91 if (decoder == "CCITTFaxDecode" || decoder == "CCF") { | 91 if (decoder == "CCITTFaxDecode" || decoder == "CCF") { |
92 ICodec_ScanlineDecoder* pDecoder = | 92 ICodec_ScanlineDecoder* pDecoder = |
93 FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); | 93 FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); |
94 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); | 94 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); |
95 } | 95 } |
96 if (decoder == "ASCII85Decode" || decoder == "A85") { | 96 if (decoder == "ASCII85Decode" || decoder == "A85") { |
97 return A85Decode(src_buf, limit, dest_buf, dest_size); | 97 return A85Decode(src_buf, limit, dest_buf, dest_size); |
98 } | 98 } |
99 if (decoder == "ASCIIHexDecode" || decoder == "AHx") { | 99 if (decoder == "ASCIIHexDecode" || decoder == "AHx") { |
100 return HexDecode(src_buf, limit, dest_buf, dest_size); | 100 return HexDecode(src_buf, limit, dest_buf, dest_size); |
(...skipping 11 matching lines...) Expand all Loading... |
112 CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder( | 112 CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder( |
113 src_buf, limit, width, height, 0, | 113 src_buf, limit, width, height, 0, |
114 pParam ? pParam->GetIntegerBy("ColorTransform", 1) : 1); | 114 pParam ? pParam->GetIntegerBy("ColorTransform", 1) : 1); |
115 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); | 115 return DecodeAllScanlines(pDecoder, dest_buf, dest_size); |
116 } | 116 } |
117 if (decoder == "RunLengthDecode" || decoder == "RL") { | 117 if (decoder == "RunLengthDecode" || decoder == "RL") { |
118 return RunLengthDecode(src_buf, limit, dest_buf, dest_size); | 118 return RunLengthDecode(src_buf, limit, dest_buf, dest_size); |
119 } | 119 } |
120 dest_size = 0; | 120 dest_size = 0; |
121 dest_buf = 0; | 121 dest_buf = 0; |
122 return (FX_DWORD)-1; | 122 return (uint32_t)-1; |
123 } | 123 } |
124 | 124 |
125 CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, | 125 CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, |
126 CPDF_Dictionary* pDict, | 126 CPDF_Dictionary* pDict, |
127 CPDF_Object* pCSObj, | 127 CPDF_Object* pCSObj, |
128 FX_BOOL bDecode) { | 128 FX_BOOL bDecode) { |
129 if (m_Pos == m_Size) | 129 if (m_Pos == m_Size) |
130 return nullptr; | 130 return nullptr; |
131 | 131 |
132 if (PDFCharIsWhitespace(m_pBuf[m_Pos])) | 132 if (PDFCharIsWhitespace(m_pBuf[m_Pos])) |
133 m_Pos++; | 133 m_Pos++; |
134 | 134 |
135 CFX_ByteString Decoder; | 135 CFX_ByteString Decoder; |
136 CPDF_Dictionary* pParam = nullptr; | 136 CPDF_Dictionary* pParam = nullptr; |
137 CPDF_Object* pFilter = pDict->GetElementValue("Filter"); | 137 CPDF_Object* pFilter = pDict->GetElementValue("Filter"); |
138 if (pFilter) { | 138 if (pFilter) { |
139 if (CPDF_Array* pArray = pFilter->AsArray()) { | 139 if (CPDF_Array* pArray = pFilter->AsArray()) { |
140 Decoder = pArray->GetStringAt(0); | 140 Decoder = pArray->GetStringAt(0); |
141 CPDF_Array* pParams = pDict->GetArrayBy("DecodeParms"); | 141 CPDF_Array* pParams = pDict->GetArrayBy("DecodeParms"); |
142 if (pParams) | 142 if (pParams) |
143 pParam = pParams->GetDictAt(0); | 143 pParam = pParams->GetDictAt(0); |
144 } else { | 144 } else { |
145 Decoder = pFilter->GetString(); | 145 Decoder = pFilter->GetString(); |
146 pParam = pDict->GetDictBy("DecodeParms"); | 146 pParam = pDict->GetDictBy("DecodeParms"); |
147 } | 147 } |
148 } | 148 } |
149 FX_DWORD width = pDict->GetIntegerBy("Width"); | 149 uint32_t width = pDict->GetIntegerBy("Width"); |
150 FX_DWORD height = pDict->GetIntegerBy("Height"); | 150 uint32_t height = pDict->GetIntegerBy("Height"); |
151 FX_DWORD OrigSize = 0; | 151 uint32_t OrigSize = 0; |
152 if (pCSObj) { | 152 if (pCSObj) { |
153 FX_DWORD bpc = pDict->GetIntegerBy("BitsPerComponent"); | 153 uint32_t bpc = pDict->GetIntegerBy("BitsPerComponent"); |
154 FX_DWORD nComponents = 1; | 154 uint32_t nComponents = 1; |
155 CPDF_ColorSpace* pCS = pDoc->LoadColorSpace(pCSObj); | 155 CPDF_ColorSpace* pCS = pDoc->LoadColorSpace(pCSObj); |
156 if (!pCS) { | 156 if (!pCS) { |
157 nComponents = 3; | 157 nComponents = 3; |
158 } else { | 158 } else { |
159 nComponents = pCS->CountComponents(); | 159 nComponents = pCS->CountComponents(); |
160 pDoc->GetPageData()->ReleaseColorSpace(pCSObj); | 160 pDoc->GetPageData()->ReleaseColorSpace(pCSObj); |
161 } | 161 } |
162 FX_DWORD pitch = width; | 162 uint32_t pitch = width; |
163 if (bpc && pitch > INT_MAX / bpc) { | 163 if (bpc && pitch > INT_MAX / bpc) { |
164 return NULL; | 164 return NULL; |
165 } | 165 } |
166 pitch *= bpc; | 166 pitch *= bpc; |
167 if (nComponents && pitch > INT_MAX / nComponents) { | 167 if (nComponents && pitch > INT_MAX / nComponents) { |
168 return NULL; | 168 return NULL; |
169 } | 169 } |
170 pitch *= nComponents; | 170 pitch *= nComponents; |
171 if (pitch > INT_MAX - 7) { | 171 if (pitch > INT_MAX - 7) { |
172 return NULL; | 172 return NULL; |
173 } | 173 } |
174 pitch += 7; | 174 pitch += 7; |
175 pitch /= 8; | 175 pitch /= 8; |
176 OrigSize = pitch; | 176 OrigSize = pitch; |
177 } else { | 177 } else { |
178 if (width > INT_MAX - 7) { | 178 if (width > INT_MAX - 7) { |
179 return NULL; | 179 return NULL; |
180 } | 180 } |
181 OrigSize = ((width + 7) / 8); | 181 OrigSize = ((width + 7) / 8); |
182 } | 182 } |
183 if (height && OrigSize > INT_MAX / height) { | 183 if (height && OrigSize > INT_MAX / height) { |
184 return NULL; | 184 return NULL; |
185 } | 185 } |
186 OrigSize *= height; | 186 OrigSize *= height; |
187 uint8_t* pData = NULL; | 187 uint8_t* pData = NULL; |
188 FX_DWORD dwStreamSize; | 188 uint32_t dwStreamSize; |
189 if (Decoder.IsEmpty()) { | 189 if (Decoder.IsEmpty()) { |
190 if (OrigSize > m_Size - m_Pos) { | 190 if (OrigSize > m_Size - m_Pos) { |
191 OrigSize = m_Size - m_Pos; | 191 OrigSize = m_Size - m_Pos; |
192 } | 192 } |
193 pData = FX_Alloc(uint8_t, OrigSize); | 193 pData = FX_Alloc(uint8_t, OrigSize); |
194 FXSYS_memcpy(pData, m_pBuf + m_Pos, OrigSize); | 194 FXSYS_memcpy(pData, m_pBuf + m_Pos, OrigSize); |
195 dwStreamSize = OrigSize; | 195 dwStreamSize = OrigSize; |
196 m_Pos += OrigSize; | 196 m_Pos += OrigSize; |
197 } else { | 197 } else { |
198 FX_DWORD dwDestSize = OrigSize; | 198 uint32_t dwDestSize = OrigSize; |
199 dwStreamSize = | 199 dwStreamSize = |
200 PDF_DecodeInlineStream(m_pBuf + m_Pos, m_Size - m_Pos, width, height, | 200 PDF_DecodeInlineStream(m_pBuf + m_Pos, m_Size - m_Pos, width, height, |
201 Decoder, pParam, pData, dwDestSize); | 201 Decoder, pParam, pData, dwDestSize); |
202 if ((int)dwStreamSize < 0) { | 202 if ((int)dwStreamSize < 0) { |
203 FX_Free(pData); | 203 FX_Free(pData); |
204 return NULL; | 204 return NULL; |
205 } | 205 } |
206 if (bDecode) { | 206 if (bDecode) { |
207 m_Pos += dwStreamSize; | 207 m_Pos += dwStreamSize; |
208 dwStreamSize = dwDestSize; | 208 dwStreamSize = dwDestSize; |
209 if (CPDF_Array* pArray = pFilter->AsArray()) { | 209 if (CPDF_Array* pArray = pFilter->AsArray()) { |
210 pArray->RemoveAt(0); | 210 pArray->RemoveAt(0); |
211 CPDF_Array* pParams = pDict->GetArrayBy("DecodeParms"); | 211 CPDF_Array* pParams = pDict->GetArrayBy("DecodeParms"); |
212 if (pParams) | 212 if (pParams) |
213 pParams->RemoveAt(0); | 213 pParams->RemoveAt(0); |
214 } else { | 214 } else { |
215 pDict->RemoveAt("Filter"); | 215 pDict->RemoveAt("Filter"); |
216 pDict->RemoveAt("DecodeParms"); | 216 pDict->RemoveAt("DecodeParms"); |
217 } | 217 } |
218 } else { | 218 } else { |
219 FX_Free(pData); | 219 FX_Free(pData); |
220 FX_DWORD dwSavePos = m_Pos; | 220 uint32_t dwSavePos = m_Pos; |
221 m_Pos += dwStreamSize; | 221 m_Pos += dwStreamSize; |
222 while (1) { | 222 while (1) { |
223 FX_DWORD dwPrevPos = m_Pos; | 223 uint32_t dwPrevPos = m_Pos; |
224 CPDF_StreamParser::SyntaxType type = ParseNextElement(); | 224 CPDF_StreamParser::SyntaxType type = ParseNextElement(); |
225 if (type == CPDF_StreamParser::EndOfData) { | 225 if (type == CPDF_StreamParser::EndOfData) { |
226 break; | 226 break; |
227 } | 227 } |
228 if (type != CPDF_StreamParser::Keyword) { | 228 if (type != CPDF_StreamParser::Keyword) { |
229 dwStreamSize += m_Pos - dwPrevPos; | 229 dwStreamSize += m_Pos - dwPrevPos; |
230 continue; | 230 continue; |
231 } | 231 } |
232 if (GetWordSize() == 2 && GetWordBuf()[0] == 'E' && | 232 if (GetWordSize() == 2 && GetWordBuf()[0] == 'E' && |
233 GetWordBuf()[1] == 'I') { | 233 GetWordBuf()[1] == 'I') { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 } else if (m_WordSize == 5) { | 325 } else if (m_WordSize == 5) { |
326 if (memcmp(m_WordBuffer, "false", 5) == 0) { | 326 if (memcmp(m_WordBuffer, "false", 5) == 0) { |
327 m_pLastObj = new CPDF_Boolean(FALSE); | 327 m_pLastObj = new CPDF_Boolean(FALSE); |
328 return Others; | 328 return Others; |
329 } | 329 } |
330 } | 330 } |
331 return Keyword; | 331 return Keyword; |
332 } | 332 } |
333 | 333 |
334 void CPDF_StreamParser::SkipPathObject() { | 334 void CPDF_StreamParser::SkipPathObject() { |
335 FX_DWORD command_startpos = m_Pos; | 335 uint32_t command_startpos = m_Pos; |
336 if (!PositionIsInBounds()) | 336 if (!PositionIsInBounds()) |
337 return; | 337 return; |
338 | 338 |
339 int ch = m_pBuf[m_Pos++]; | 339 int ch = m_pBuf[m_Pos++]; |
340 while (1) { | 340 while (1) { |
341 while (PDFCharIsWhitespace(ch)) { | 341 while (PDFCharIsWhitespace(ch)) { |
342 if (!PositionIsInBounds()) | 342 if (!PositionIsInBounds()) |
343 return; | 343 return; |
344 ch = m_pBuf[m_Pos++]; | 344 ch = m_pBuf[m_Pos++]; |
345 } | 345 } |
(...skipping 12 matching lines...) Expand all Loading... |
358 | 358 |
359 while (PDFCharIsWhitespace(ch)) { | 359 while (PDFCharIsWhitespace(ch)) { |
360 if (!PositionIsInBounds()) | 360 if (!PositionIsInBounds()) |
361 return; | 361 return; |
362 ch = m_pBuf[m_Pos++]; | 362 ch = m_pBuf[m_Pos++]; |
363 } | 363 } |
364 | 364 |
365 if (PDFCharIsNumeric(ch)) | 365 if (PDFCharIsNumeric(ch)) |
366 continue; | 366 continue; |
367 | 367 |
368 FX_DWORD op_startpos = m_Pos - 1; | 368 uint32_t op_startpos = m_Pos - 1; |
369 while (!PDFCharIsWhitespace(ch) && !PDFCharIsDelimiter(ch)) { | 369 while (!PDFCharIsWhitespace(ch) && !PDFCharIsDelimiter(ch)) { |
370 if (!PositionIsInBounds()) | 370 if (!PositionIsInBounds()) |
371 return; | 371 return; |
372 ch = m_pBuf[m_Pos++]; | 372 ch = m_pBuf[m_Pos++]; |
373 } | 373 } |
374 | 374 |
375 if (IsPathOperator(&m_pBuf[op_startpos], m_Pos - 1 - op_startpos)) { | 375 if (IsPathOperator(&m_pBuf[op_startpos], m_Pos - 1 - op_startpos)) { |
376 command_startpos = m_Pos; | 376 command_startpos = m_Pos; |
377 break; | 377 break; |
378 } | 378 } |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 for (const auto& stream : m_StreamArray) { | 801 for (const auto& stream : m_StreamArray) { |
802 safeSize += stream->GetSize(); | 802 safeSize += stream->GetSize(); |
803 safeSize += 1; | 803 safeSize += 1; |
804 } | 804 } |
805 if (!safeSize.IsValid()) { | 805 if (!safeSize.IsValid()) { |
806 m_Status = Done; | 806 m_Status = Done; |
807 return; | 807 return; |
808 } | 808 } |
809 m_Size = safeSize.ValueOrDie(); | 809 m_Size = safeSize.ValueOrDie(); |
810 m_pData = FX_Alloc(uint8_t, m_Size); | 810 m_pData = FX_Alloc(uint8_t, m_Size); |
811 FX_DWORD pos = 0; | 811 uint32_t pos = 0; |
812 for (const auto& stream : m_StreamArray) { | 812 for (const auto& stream : m_StreamArray) { |
813 FXSYS_memcpy(m_pData + pos, stream->GetData(), stream->GetSize()); | 813 FXSYS_memcpy(m_pData + pos, stream->GetData(), stream->GetSize()); |
814 pos += stream->GetSize(); | 814 pos += stream->GetSize(); |
815 m_pData[pos++] = ' '; | 815 m_pData[pos++] = ' '; |
816 } | 816 } |
817 m_StreamArray.clear(); | 817 m_StreamArray.clear(); |
818 } else { | 818 } else { |
819 m_pData = (uint8_t*)m_pSingleStream->GetData(); | 819 m_pData = (uint8_t*)m_pSingleStream->GetData(); |
820 m_Size = m_pSingleStream->GetSize(); | 820 m_Size = m_pSingleStream->GetSize(); |
821 } | 821 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 } | 885 } |
886 m_Status = Done; | 886 m_Status = Done; |
887 return; | 887 return; |
888 } | 888 } |
889 steps++; | 889 steps++; |
890 if (pPause && pPause->NeedToPauseNow()) { | 890 if (pPause && pPause->NeedToPauseNow()) { |
891 break; | 891 break; |
892 } | 892 } |
893 } | 893 } |
894 } | 894 } |
OLD | NEW |