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 "../../../../third_party/base/nonstd_unique_ptr.h" | 7 #include "../../../../third_party/base/nonstd_unique_ptr.h" |
8 #include "../../../include/fxcodec/fx_codec.h" | 8 #include "../../../include/fxcodec/fx_codec.h" |
9 #include "../../fx_zlib.h" | 9 #include "../../fx_zlib.h" |
10 #include "codec_int.h" | 10 #include "codec_int.h" |
11 | 11 |
12 extern "C" | 12 extern "C" |
13 { | 13 { |
14 static void* my_alloc_func (void* opaque, unsigned int items, unsigned int s
ize) | 14 static void* my_alloc_func (void* opaque, unsigned int items, unsigned int s
ize) |
15 { | 15 { |
16 return FX_Alloc2D(FX_BYTE, items, size); | 16 return FX_Alloc2D(uint8_t, items, size); |
17 } | 17 } |
18 static void my_free_func (void* opaque, void* address) | 18 static void my_free_func (void* opaque, void* address) |
19 { | 19 { |
20 FX_Free(address); | 20 FX_Free(address); |
21 } | 21 } |
22 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in
t), | 22 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in
t), |
23 void (*free_func)(void*, void*)) | 23 void (*free_func)(void*, void*)) |
24 { | 24 { |
25 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); | 25 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); |
26 if (p == NULL) { | 26 if (p == NULL) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 ((z_stream*)context)->zfree(0, context); | 72 ((z_stream*)context)->zfree(0, context); |
73 } | 73 } |
74 void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size
, const unsigned char* src_buf, unsigned long src_size) | 74 void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size
, const unsigned char* src_buf, unsigned long src_size) |
75 { | 75 { |
76 compress(dest_buf, dest_size, src_buf, src_size); | 76 compress(dest_buf, dest_size, src_buf, src_size); |
77 } | 77 } |
78 } | 78 } |
79 class CLZWDecoder | 79 class CLZWDecoder |
80 { | 80 { |
81 public: | 81 public: |
82 FX_BOOL Decode(FX_LPBYTE output, FX_DWORD& outlen, const FX_BYTE* input, FX_
DWORD& size, FX_BOOL bEarlyChange); | 82 FX_BOOL Decode(FX_LPBYTE output, FX_DWORD& outlen, const uint8_t* input, FX_
DWORD& size, FX_BOOL bEarlyChange); |
83 private: | 83 private: |
84 FX_DWORD m_InPos; | 84 FX_DWORD m_InPos; |
85 FX_DWORD m_OutPos; | 85 FX_DWORD m_OutPos; |
86 FX_LPBYTE m_pOutput; | 86 FX_LPBYTE m_pOutput; |
87 const FX_BYTE*» m_pInput; | 87 const uint8_t*» m_pInput; |
88 FX_BOOL m_Early; | 88 FX_BOOL m_Early; |
89 void» » AddCode(FX_DWORD prefix_code, FX_BYTE append_char); | 89 void» » AddCode(FX_DWORD prefix_code, uint8_t append_char); |
90 FX_DWORD m_CodeArray[5021]; | 90 FX_DWORD m_CodeArray[5021]; |
91 FX_DWORD m_nCodes; | 91 FX_DWORD m_nCodes; |
92 FX_BYTE» » m_DecodeStack[4000]; | 92 uint8_t» » m_DecodeStack[4000]; |
93 FX_DWORD m_StackLen; | 93 FX_DWORD m_StackLen; |
94 void DecodeString(FX_DWORD code); | 94 void DecodeString(FX_DWORD code); |
95 int m_CodeLen; | 95 int m_CodeLen; |
96 }; | 96 }; |
97 void CLZWDecoder::AddCode(FX_DWORD prefix_code, FX_BYTE append_char) | 97 void CLZWDecoder::AddCode(FX_DWORD prefix_code, uint8_t append_char) |
98 { | 98 { |
99 if (m_nCodes + m_Early == 4094) { | 99 if (m_nCodes + m_Early == 4094) { |
100 return; | 100 return; |
101 } | 101 } |
102 m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char; | 102 m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char; |
103 if (m_nCodes + m_Early == 512 - 258) { | 103 if (m_nCodes + m_Early == 512 - 258) { |
104 m_CodeLen = 10; | 104 m_CodeLen = 10; |
105 } else if (m_nCodes + m_Early == 1024 - 258) { | 105 } else if (m_nCodes + m_Early == 1024 - 258) { |
106 m_CodeLen = 11; | 106 m_CodeLen = 11; |
107 } else if (m_nCodes + m_Early == 2048 - 258) { | 107 } else if (m_nCodes + m_Early == 2048 - 258) { |
108 m_CodeLen = 12; | 108 m_CodeLen = 12; |
109 } | 109 } |
110 } | 110 } |
111 void CLZWDecoder::DecodeString(FX_DWORD code) | 111 void CLZWDecoder::DecodeString(FX_DWORD code) |
112 { | 112 { |
113 while (1) { | 113 while (1) { |
114 int index = code - 258; | 114 int index = code - 258; |
115 if (index < 0 || index >= (int)m_nCodes) { | 115 if (index < 0 || index >= (int)m_nCodes) { |
116 break; | 116 break; |
117 } | 117 } |
118 FX_DWORD data = m_CodeArray[index]; | 118 FX_DWORD data = m_CodeArray[index]; |
119 if (m_StackLen >= sizeof(m_DecodeStack)) { | 119 if (m_StackLen >= sizeof(m_DecodeStack)) { |
120 return; | 120 return; |
121 } | 121 } |
122 m_DecodeStack[m_StackLen++] = (FX_BYTE)data; | 122 m_DecodeStack[m_StackLen++] = (uint8_t)data; |
123 code = data >> 16; | 123 code = data >> 16; |
124 } | 124 } |
125 if (m_StackLen >= sizeof(m_DecodeStack)) { | 125 if (m_StackLen >= sizeof(m_DecodeStack)) { |
126 return; | 126 return; |
127 } | 127 } |
128 m_DecodeStack[m_StackLen++] = (FX_BYTE)code; | 128 m_DecodeStack[m_StackLen++] = (uint8_t)code; |
129 } | 129 } |
130 int CLZWDecoder::Decode(FX_LPBYTE dest_buf, FX_DWORD& dest_size, const FX_BYTE*
src_buf, FX_DWORD& src_size, FX_BOOL bEarlyChange) | 130 int CLZWDecoder::Decode(FX_LPBYTE dest_buf, FX_DWORD& dest_size, const uint8_t*
src_buf, FX_DWORD& src_size, FX_BOOL bEarlyChange) |
131 { | 131 { |
132 m_CodeLen = 9; | 132 m_CodeLen = 9; |
133 m_InPos = 0; | 133 m_InPos = 0; |
134 m_OutPos = 0; | 134 m_OutPos = 0; |
135 m_pInput = src_buf; | 135 m_pInput = src_buf; |
136 m_pOutput = dest_buf; | 136 m_pOutput = dest_buf; |
137 m_Early = bEarlyChange ? 1 : 0; | 137 m_Early = bEarlyChange ? 1 : 0; |
138 m_nCodes = 0; | 138 m_nCodes = 0; |
139 FX_DWORD old_code = (FX_DWORD) - 1; | 139 FX_DWORD old_code = (FX_DWORD) - 1; |
140 FX_BYTE last_char; | 140 uint8_t last_char; |
141 while (1) { | 141 while (1) { |
142 if (m_InPos + m_CodeLen > src_size * 8) { | 142 if (m_InPos + m_CodeLen > src_size * 8) { |
143 break; | 143 break; |
144 } | 144 } |
145 int byte_pos = m_InPos / 8; | 145 int byte_pos = m_InPos / 8; |
146 int bit_pos = m_InPos % 8, bit_left = m_CodeLen; | 146 int bit_pos = m_InPos % 8, bit_left = m_CodeLen; |
147 FX_DWORD code = 0; | 147 FX_DWORD code = 0; |
148 if (bit_pos) { | 148 if (bit_pos) { |
149 bit_left -= 8 - bit_pos; | 149 bit_left -= 8 - bit_pos; |
150 code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_le
ft; | 150 code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_le
ft; |
151 } | 151 } |
152 if (bit_left < 8) { | 152 if (bit_left < 8) { |
153 code |= m_pInput[byte_pos] >> (8 - bit_left); | 153 code |= m_pInput[byte_pos] >> (8 - bit_left); |
154 } else { | 154 } else { |
155 bit_left -= 8; | 155 bit_left -= 8; |
156 code |= m_pInput[byte_pos++] << bit_left; | 156 code |= m_pInput[byte_pos++] << bit_left; |
157 if (bit_left) { | 157 if (bit_left) { |
158 code |= m_pInput[byte_pos] >> (8 - bit_left); | 158 code |= m_pInput[byte_pos] >> (8 - bit_left); |
159 } | 159 } |
160 } | 160 } |
161 m_InPos += m_CodeLen; | 161 m_InPos += m_CodeLen; |
162 if (code < 256) { | 162 if (code < 256) { |
163 if (m_OutPos == dest_size) { | 163 if (m_OutPos == dest_size) { |
164 return -5; | 164 return -5; |
165 } | 165 } |
166 if (m_pOutput) { | 166 if (m_pOutput) { |
167 m_pOutput[m_OutPos] = (FX_BYTE)code; | 167 m_pOutput[m_OutPos] = (uint8_t)code; |
168 } | 168 } |
169 m_OutPos ++; | 169 m_OutPos ++; |
170 last_char = (FX_BYTE)code; | 170 last_char = (uint8_t)code; |
171 if (old_code != (FX_DWORD) - 1) { | 171 if (old_code != (FX_DWORD) - 1) { |
172 AddCode(old_code, last_char); | 172 AddCode(old_code, last_char); |
173 } | 173 } |
174 old_code = code; | 174 old_code = code; |
175 } else if (code == 256) { | 175 } else if (code == 256) { |
176 m_CodeLen = 9; | 176 m_CodeLen = 9; |
177 m_nCodes = 0; | 177 m_nCodes = 0; |
178 old_code = (FX_DWORD) - 1; | 178 old_code = (FX_DWORD) - 1; |
179 } else if (code == 257) { | 179 } else if (code == 257) { |
180 break; | 180 break; |
(...skipping 29 matching lines...) Expand all Loading... |
210 } else { | 210 } else { |
211 AddCode(old_code, last_char); | 211 AddCode(old_code, last_char); |
212 } | 212 } |
213 old_code = code; | 213 old_code = code; |
214 } | 214 } |
215 } | 215 } |
216 dest_size = m_OutPos; | 216 dest_size = m_OutPos; |
217 src_size = (m_InPos + 7) / 8; | 217 src_size = (m_InPos + 7) / 8; |
218 return 0; | 218 return 0; |
219 } | 219 } |
220 static FX_BYTE PaethPredictor(int a, int b, int c) | 220 static uint8_t PaethPredictor(int a, int b, int c) |
221 { | 221 { |
222 int p = a + b - c; | 222 int p = a + b - c; |
223 int pa = FXSYS_abs(p - a); | 223 int pa = FXSYS_abs(p - a); |
224 int pb = FXSYS_abs(p - b); | 224 int pb = FXSYS_abs(p - b); |
225 int pc = FXSYS_abs(p - c); | 225 int pc = FXSYS_abs(p - c); |
226 if (pa <= pb && pa <= pc) { | 226 if (pa <= pb && pa <= pc) { |
227 return (FX_BYTE)a; | 227 return (uint8_t)a; |
228 } | 228 } |
229 if (pb <= pc) { | 229 if (pb <= pc) { |
230 return (FX_BYTE)b; | 230 return (uint8_t)b; |
231 } | 231 } |
232 return (FX_BYTE)c; | 232 return (uint8_t)c; |
233 } | 233 } |
234 static FX_BOOL PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, | 234 static FX_BOOL PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, |
235 int predictor, int Colors, | 235 int predictor, int Colors, |
236 int BitsPerComponent, int Columns) | 236 int BitsPerComponent, int Columns) |
237 { | 237 { |
238 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 238 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
239 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 239 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
240 if (row_size <= 0) | 240 if (row_size <= 0) |
241 return FALSE; | 241 return FALSE; |
242 const int row_count = (data_size + row_size - 1) / row_size; | 242 const int row_count = (data_size + row_size - 1) / row_size; |
243 const int last_row_size = data_size % row_size; | 243 const int last_row_size = data_size % row_size; |
244 FX_LPBYTE dest_buf = FX_Alloc2D(FX_BYTE, row_size + 1, row_count); | 244 FX_LPBYTE dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); |
245 int byte_cnt = 0; | 245 int byte_cnt = 0; |
246 FX_LPBYTE pSrcData = data_buf; | 246 FX_LPBYTE pSrcData = data_buf; |
247 FX_LPBYTE pDestData = dest_buf; | 247 FX_LPBYTE pDestData = dest_buf; |
248 for (int row = 0; row < row_count; row++) { | 248 for (int row = 0; row < row_count; row++) { |
249 if (predictor == 10) { | 249 if (predictor == 10) { |
250 pDestData[0] = 0; | 250 pDestData[0] = 0; |
251 int move_size = row_size; | 251 int move_size = row_size; |
252 if (move_size * (row + 1) > (int)data_size) { | 252 if (move_size * (row + 1) > (int)data_size) { |
253 move_size = data_size - (move_size * row); | 253 move_size = data_size - (move_size * row); |
254 } | 254 } |
255 FXSYS_memmove32(pDestData + 1, pSrcData, move_size); | 255 FXSYS_memmove32(pDestData + 1, pSrcData, move_size); |
256 pDestData += (move_size + 1); | 256 pDestData += (move_size + 1); |
257 pSrcData += move_size; | 257 pSrcData += move_size; |
258 byte_cnt += move_size; | 258 byte_cnt += move_size; |
259 continue; | 259 continue; |
260 } | 260 } |
261 for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++)
{ | 261 for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++)
{ |
262 switch (predictor) { | 262 switch (predictor) { |
263 case 11: { | 263 case 11: { |
264 pDestData[0] = 1; | 264 pDestData[0] = 1; |
265 FX_BYTE left = 0; | 265 uint8_t left = 0; |
266 if (byte >= BytesPerPixel) { | 266 if (byte >= BytesPerPixel) { |
267 left = pSrcData[byte - BytesPerPixel]; | 267 left = pSrcData[byte - BytesPerPixel]; |
268 } | 268 } |
269 pDestData[byte + 1] = pSrcData[byte] - left; | 269 pDestData[byte + 1] = pSrcData[byte] - left; |
270 } | 270 } |
271 break; | 271 break; |
272 case 12: { | 272 case 12: { |
273 pDestData[0] = 2; | 273 pDestData[0] = 2; |
274 FX_BYTE up = 0; | 274 uint8_t up = 0; |
275 if (row) { | 275 if (row) { |
276 up = pSrcData[byte - row_size]; | 276 up = pSrcData[byte - row_size]; |
277 } | 277 } |
278 pDestData[byte + 1] = pSrcData[byte] - up; | 278 pDestData[byte + 1] = pSrcData[byte] - up; |
279 } | 279 } |
280 break; | 280 break; |
281 case 13: { | 281 case 13: { |
282 pDestData[0] = 3; | 282 pDestData[0] = 3; |
283 FX_BYTE left = 0; | 283 uint8_t left = 0; |
284 if (byte >= BytesPerPixel) { | 284 if (byte >= BytesPerPixel) { |
285 left = pSrcData[byte - BytesPerPixel]; | 285 left = pSrcData[byte - BytesPerPixel]; |
286 } | 286 } |
287 FX_BYTE up = 0; | 287 uint8_t up = 0; |
288 if (row) { | 288 if (row) { |
289 up = pSrcData[byte - row_size]; | 289 up = pSrcData[byte - row_size]; |
290 } | 290 } |
291 pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2; | 291 pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2; |
292 } | 292 } |
293 break; | 293 break; |
294 case 14: { | 294 case 14: { |
295 pDestData[0] = 4; | 295 pDestData[0] = 4; |
296 FX_BYTE left = 0; | 296 uint8_t left = 0; |
297 if (byte >= BytesPerPixel) { | 297 if (byte >= BytesPerPixel) { |
298 left = pSrcData[byte - BytesPerPixel]; | 298 left = pSrcData[byte - BytesPerPixel]; |
299 } | 299 } |
300 FX_BYTE up = 0; | 300 uint8_t up = 0; |
301 if (row) { | 301 if (row) { |
302 up = pSrcData[byte - row_size]; | 302 up = pSrcData[byte - row_size]; |
303 } | 303 } |
304 FX_BYTE upper_left = 0; | 304 uint8_t upper_left = 0; |
305 if (byte >= BytesPerPixel && row) { | 305 if (byte >= BytesPerPixel && row) { |
306 upper_left = pSrcData[byte - row_size - BytesPerPixe
l]; | 306 upper_left = pSrcData[byte - row_size - BytesPerPixe
l]; |
307 } | 307 } |
308 pDestData[byte + 1] = pSrcData[byte] - PaethPredictor(le
ft, up, upper_left); | 308 pDestData[byte + 1] = pSrcData[byte] - PaethPredictor(le
ft, up, upper_left); |
309 } | 309 } |
310 break; | 310 break; |
311 default: { | 311 default: { |
312 pDestData[byte + 1] = pSrcData[byte]; | 312 pDestData[byte + 1] = pSrcData[byte]; |
313 } | 313 } |
314 break; | 314 break; |
315 } | 315 } |
316 byte_cnt++; | 316 byte_cnt++; |
317 } | 317 } |
318 pDestData += (row_size + 1); | 318 pDestData += (row_size + 1); |
319 pSrcData += row_size; | 319 pSrcData += row_size; |
320 } | 320 } |
321 FX_Free(data_buf); | 321 FX_Free(data_buf); |
322 data_buf = dest_buf; | 322 data_buf = dest_buf; |
323 data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - la
st_row_size) : 0); | 323 data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - la
st_row_size) : 0); |
324 return TRUE; | 324 return TRUE; |
325 } | 325 } |
326 static void PNG_PredictLine(FX_LPBYTE pDestData, FX_LPCBYTE pSrcData, FX_LPCBYTE
pLastLine, | 326 static void PNG_PredictLine(FX_LPBYTE pDestData, FX_LPCBYTE pSrcData, FX_LPCBYTE
pLastLine, |
327 int bpc, int nColors, int nPixels) | 327 int bpc, int nColors, int nPixels) |
328 { | 328 { |
329 int row_size = (nPixels * bpc * nColors + 7) / 8; | 329 int row_size = (nPixels * bpc * nColors + 7) / 8; |
330 int BytesPerPixel = (bpc * nColors + 7) / 8; | 330 int BytesPerPixel = (bpc * nColors + 7) / 8; |
331 FX_BYTE tag = pSrcData[0]; | 331 uint8_t tag = pSrcData[0]; |
332 if (tag == 0) { | 332 if (tag == 0) { |
333 FXSYS_memmove32(pDestData, pSrcData + 1, row_size); | 333 FXSYS_memmove32(pDestData, pSrcData + 1, row_size); |
334 return; | 334 return; |
335 } | 335 } |
336 for (int byte = 0; byte < row_size; byte ++) { | 336 for (int byte = 0; byte < row_size; byte ++) { |
337 FX_BYTE raw_byte = pSrcData[byte + 1]; | 337 uint8_t raw_byte = pSrcData[byte + 1]; |
338 switch (tag) { | 338 switch (tag) { |
339 case 1: { | 339 case 1: { |
340 FX_BYTE left = 0; | 340 uint8_t left = 0; |
341 if (byte >= BytesPerPixel) { | 341 if (byte >= BytesPerPixel) { |
342 left = pDestData[byte - BytesPerPixel]; | 342 left = pDestData[byte - BytesPerPixel]; |
343 } | 343 } |
344 pDestData[byte] = raw_byte + left; | 344 pDestData[byte] = raw_byte + left; |
345 break; | 345 break; |
346 } | 346 } |
347 case 2: { | 347 case 2: { |
348 FX_BYTE up = 0; | 348 uint8_t up = 0; |
349 if (pLastLine) { | 349 if (pLastLine) { |
350 up = pLastLine[byte]; | 350 up = pLastLine[byte]; |
351 } | 351 } |
352 pDestData[byte] = raw_byte + up; | 352 pDestData[byte] = raw_byte + up; |
353 break; | 353 break; |
354 } | 354 } |
355 case 3: { | 355 case 3: { |
356 FX_BYTE left = 0; | 356 uint8_t left = 0; |
357 if (byte >= BytesPerPixel) { | 357 if (byte >= BytesPerPixel) { |
358 left = pDestData[byte - BytesPerPixel]; | 358 left = pDestData[byte - BytesPerPixel]; |
359 } | 359 } |
360 FX_BYTE up = 0; | 360 uint8_t up = 0; |
361 if (pLastLine) { | 361 if (pLastLine) { |
362 up = pLastLine[byte]; | 362 up = pLastLine[byte]; |
363 } | 363 } |
364 pDestData[byte] = raw_byte + (up + left) / 2; | 364 pDestData[byte] = raw_byte + (up + left) / 2; |
365 break; | 365 break; |
366 } | 366 } |
367 case 4: { | 367 case 4: { |
368 FX_BYTE left = 0; | 368 uint8_t left = 0; |
369 if (byte >= BytesPerPixel) { | 369 if (byte >= BytesPerPixel) { |
370 left = pDestData[byte - BytesPerPixel]; | 370 left = pDestData[byte - BytesPerPixel]; |
371 } | 371 } |
372 FX_BYTE up = 0; | 372 uint8_t up = 0; |
373 if (pLastLine) { | 373 if (pLastLine) { |
374 up = pLastLine[byte]; | 374 up = pLastLine[byte]; |
375 } | 375 } |
376 FX_BYTE upper_left = 0; | 376 uint8_t upper_left = 0; |
377 if (byte >= BytesPerPixel && pLastLine) { | 377 if (byte >= BytesPerPixel && pLastLine) { |
378 upper_left = pLastLine[byte - BytesPerPixel]; | 378 upper_left = pLastLine[byte - BytesPerPixel]; |
379 } | 379 } |
380 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_
left); | 380 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_
left); |
381 break; | 381 break; |
382 } | 382 } |
383 default: | 383 default: |
384 pDestData[byte] = raw_byte; | 384 pDestData[byte] = raw_byte; |
385 break; | 385 break; |
386 } | 386 } |
387 } | 387 } |
388 } | 388 } |
389 static FX_BOOL PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size, | 389 static FX_BOOL PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size, |
390 int Colors, int BitsPerComponent, int Columns) | 390 int Colors, int BitsPerComponent, int Columns) |
391 { | 391 { |
392 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 392 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
393 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 393 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
394 if (row_size <= 0) | 394 if (row_size <= 0) |
395 return FALSE; | 395 return FALSE; |
396 const int row_count = (data_size + row_size) / (row_size + 1); | 396 const int row_count = (data_size + row_size) / (row_size + 1); |
397 const int last_row_size = data_size % (row_size + 1); | 397 const int last_row_size = data_size % (row_size + 1); |
398 FX_LPBYTE dest_buf = FX_Alloc2D(FX_BYTE, row_size, row_count); | 398 FX_LPBYTE dest_buf = FX_Alloc2D(uint8_t, row_size, row_count); |
399 int byte_cnt = 0; | 399 int byte_cnt = 0; |
400 FX_LPBYTE pSrcData = data_buf; | 400 FX_LPBYTE pSrcData = data_buf; |
401 FX_LPBYTE pDestData = dest_buf; | 401 FX_LPBYTE pDestData = dest_buf; |
402 for (int row = 0; row < row_count; row ++) { | 402 for (int row = 0; row < row_count; row ++) { |
403 FX_BYTE tag = pSrcData[0]; | 403 uint8_t tag = pSrcData[0]; |
404 byte_cnt++; | 404 byte_cnt++; |
405 if (tag == 0) { | 405 if (tag == 0) { |
406 int move_size = row_size; | 406 int move_size = row_size; |
407 if ((row + 1) * (move_size + 1) > (int)data_size) { | 407 if ((row + 1) * (move_size + 1) > (int)data_size) { |
408 move_size = last_row_size - 1; | 408 move_size = last_row_size - 1; |
409 } | 409 } |
410 FXSYS_memmove32(pDestData, pSrcData + 1, move_size); | 410 FXSYS_memmove32(pDestData, pSrcData + 1, move_size); |
411 pSrcData += move_size + 1; | 411 pSrcData += move_size + 1; |
412 pDestData += move_size; | 412 pDestData += move_size; |
413 byte_cnt += move_size; | 413 byte_cnt += move_size; |
414 continue; | 414 continue; |
415 } | 415 } |
416 for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte ++
) { | 416 for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte ++
) { |
417 FX_BYTE raw_byte = pSrcData[byte + 1]; | 417 uint8_t raw_byte = pSrcData[byte + 1]; |
418 switch (tag) { | 418 switch (tag) { |
419 case 1: { | 419 case 1: { |
420 FX_BYTE left = 0; | 420 uint8_t left = 0; |
421 if (byte >= BytesPerPixel) { | 421 if (byte >= BytesPerPixel) { |
422 left = pDestData[byte - BytesPerPixel]; | 422 left = pDestData[byte - BytesPerPixel]; |
423 } | 423 } |
424 pDestData[byte] = raw_byte + left; | 424 pDestData[byte] = raw_byte + left; |
425 break; | 425 break; |
426 } | 426 } |
427 case 2: { | 427 case 2: { |
428 FX_BYTE up = 0; | 428 uint8_t up = 0; |
429 if (row) { | 429 if (row) { |
430 up = pDestData[byte - row_size]; | 430 up = pDestData[byte - row_size]; |
431 } | 431 } |
432 pDestData[byte] = raw_byte + up; | 432 pDestData[byte] = raw_byte + up; |
433 break; | 433 break; |
434 } | 434 } |
435 case 3: { | 435 case 3: { |
436 FX_BYTE left = 0; | 436 uint8_t left = 0; |
437 if (byte >= BytesPerPixel) { | 437 if (byte >= BytesPerPixel) { |
438 left = pDestData[byte - BytesPerPixel]; | 438 left = pDestData[byte - BytesPerPixel]; |
439 } | 439 } |
440 FX_BYTE up = 0; | 440 uint8_t up = 0; |
441 if (row) { | 441 if (row) { |
442 up = pDestData[byte - row_size]; | 442 up = pDestData[byte - row_size]; |
443 } | 443 } |
444 pDestData[byte] = raw_byte + (up + left) / 2; | 444 pDestData[byte] = raw_byte + (up + left) / 2; |
445 break; | 445 break; |
446 } | 446 } |
447 case 4: { | 447 case 4: { |
448 FX_BYTE left = 0; | 448 uint8_t left = 0; |
449 if (byte >= BytesPerPixel) { | 449 if (byte >= BytesPerPixel) { |
450 left = pDestData[byte - BytesPerPixel]; | 450 left = pDestData[byte - BytesPerPixel]; |
451 } | 451 } |
452 FX_BYTE up = 0; | 452 uint8_t up = 0; |
453 if (row) { | 453 if (row) { |
454 up = pDestData[byte - row_size]; | 454 up = pDestData[byte - row_size]; |
455 } | 455 } |
456 FX_BYTE upper_left = 0; | 456 uint8_t upper_left = 0; |
457 if (byte >= BytesPerPixel && row) { | 457 if (byte >= BytesPerPixel && row) { |
458 upper_left = pDestData[byte - row_size - BytesPerPix
el]; | 458 upper_left = pDestData[byte - row_size - BytesPerPix
el]; |
459 } | 459 } |
460 pDestData[byte] = raw_byte + PaethPredictor(left, up, up
per_left); | 460 pDestData[byte] = raw_byte + PaethPredictor(left, up, up
per_left); |
461 break; | 461 break; |
462 } | 462 } |
463 default: | 463 default: |
464 pDestData[byte] = raw_byte; | 464 pDestData[byte] = raw_byte; |
465 break; | 465 break; |
466 } | 466 } |
467 byte_cnt++; | 467 byte_cnt++; |
468 } | 468 } |
469 pSrcData += row_size + 1; | 469 pSrcData += row_size + 1; |
470 pDestData += row_size; | 470 pDestData += row_size; |
471 } | 471 } |
472 FX_Free(data_buf); | 472 FX_Free(data_buf); |
473 data_buf = dest_buf; | 473 data_buf = dest_buf; |
474 data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last
_row_size) : 0); | 474 data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last
_row_size) : 0); |
475 return TRUE; | 475 return TRUE; |
476 } | 476 } |
477 static void TIFF_PredictorEncodeLine(FX_LPBYTE dest_buf, int row_size, int BitsP
erComponent, int Colors, int Columns) | 477 static void TIFF_PredictorEncodeLine(FX_LPBYTE dest_buf, int row_size, int BitsP
erComponent, int Colors, int Columns) |
478 { | 478 { |
479 int BytesPerPixel = BitsPerComponent * Colors / 8; | 479 int BytesPerPixel = BitsPerComponent * Colors / 8; |
480 if (BitsPerComponent < 8) { | 480 if (BitsPerComponent < 8) { |
481 FX_BYTE mask = 0x01; | 481 uint8_t mask = 0x01; |
482 if (BitsPerComponent == 2) { | 482 if (BitsPerComponent == 2) { |
483 mask = 0x03; | 483 mask = 0x03; |
484 } else if (BitsPerComponent == 4) { | 484 } else if (BitsPerComponent == 4) { |
485 mask = 0x0F; | 485 mask = 0x0F; |
486 } | 486 } |
487 int row_bits = Colors * BitsPerComponent * Columns; | 487 int row_bits = Colors * BitsPerComponent * Columns; |
488 for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; i -= Bi
tsPerComponent) { | 488 for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; i -= Bi
tsPerComponent) { |
489 int col = i % 8; | 489 int col = i % 8; |
490 int index = i / 8; | 490 int index = i / 8; |
491 int col_pre = (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerCo
mponent); | 491 int col_pre = (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerCo
mponent); |
492 int index_pre = (col == 0) ? (index - 1) : index; | 492 int index_pre = (col == 0) ? (index - 1) : index; |
493 FX_BYTE cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & ma
sk; | 493 uint8_t cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & ma
sk; |
494 FX_BYTE left = (dest_buf[index_pre] >> (8 - col_pre - BitsPerCompone
nt)) & mask; | 494 uint8_t left = (dest_buf[index_pre] >> (8 - col_pre - BitsPerCompone
nt)) & mask; |
495 cur -= left; | 495 cur -= left; |
496 cur &= mask; | 496 cur &= mask; |
497 cur <<= (8 - col - BitsPerComponent); | 497 cur <<= (8 - col - BitsPerComponent); |
498 dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent))); | 498 dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent))); |
499 dest_buf[index] |= cur; | 499 dest_buf[index] |= cur; |
500 } | 500 } |
501 } else if (BitsPerComponent == 8) { | 501 } else if (BitsPerComponent == 8) { |
502 for (int i = row_size - 1; i >= BytesPerPixel; i--) { | 502 for (int i = row_size - 1; i >= BytesPerPixel; i--) { |
503 dest_buf[i] -= dest_buf[i - BytesPerPixel]; | 503 dest_buf[i] -= dest_buf[i - BytesPerPixel]; |
504 } | 504 } |
505 } else { | 505 } else { |
506 for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPer
Pixel) { | 506 for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPer
Pixel) { |
507 FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; | 507 FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; |
508 pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerP
ixel + 1]; | 508 pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerP
ixel + 1]; |
509 dest_buf[i] = pixel >> 8; | 509 dest_buf[i] = pixel >> 8; |
510 dest_buf[i + 1] = (FX_BYTE)pixel; | 510 dest_buf[i + 1] = (uint8_t)pixel; |
511 } | 511 } |
512 } | 512 } |
513 } | 513 } |
514 static FX_BOOL TIFF_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, | 514 static FX_BOOL TIFF_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, |
515 int Colors, int BitsPerComponent, int Column
s) | 515 int Colors, int BitsPerComponent, int Column
s) |
516 { | 516 { |
517 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 517 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
518 if (row_size == 0) | 518 if (row_size == 0) |
519 return FALSE; | 519 return FALSE; |
520 const int row_count = (data_size + row_size - 1) / row_size; | 520 const int row_count = (data_size + row_size - 1) / row_size; |
(...skipping 25 matching lines...) Expand all Loading... |
546 col_pre = col; | 546 col_pre = col; |
547 } | 547 } |
548 return; | 548 return; |
549 } | 549 } |
550 int BytesPerPixel = BitsPerComponent * Colors / 8; | 550 int BytesPerPixel = BitsPerComponent * Colors / 8; |
551 if (BitsPerComponent == 16) { | 551 if (BitsPerComponent == 16) { |
552 for (int i = BytesPerPixel; i < row_size; i += 2) { | 552 for (int i = BytesPerPixel; i < row_size; i += 2) { |
553 FX_WORD pixel = (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - By
tesPerPixel + 1]; | 553 FX_WORD pixel = (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - By
tesPerPixel + 1]; |
554 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; | 554 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; |
555 dest_buf[i] = pixel >> 8; | 555 dest_buf[i] = pixel >> 8; |
556 dest_buf[i + 1] = (FX_BYTE)pixel; | 556 dest_buf[i + 1] = (uint8_t)pixel; |
557 } | 557 } |
558 } else { | 558 } else { |
559 for (int i = BytesPerPixel; i < row_size; i ++) { | 559 for (int i = BytesPerPixel; i < row_size; i ++) { |
560 dest_buf[i] += dest_buf[i - BytesPerPixel]; | 560 dest_buf[i] += dest_buf[i - BytesPerPixel]; |
561 } | 561 } |
562 } | 562 } |
563 } | 563 } |
564 static FX_BOOL TIFF_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size, | 564 static FX_BOOL TIFF_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size, |
565 int Colors, int BitsPerComponent, int Columns) | 565 int Colors, int BitsPerComponent, int Columns) |
566 { | 566 { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) | 634 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) |
635 { | 635 { |
636 m_SrcBuf = src_buf; | 636 m_SrcBuf = src_buf; |
637 m_SrcSize = src_size; | 637 m_SrcSize = src_size; |
638 m_OutputWidth = m_OrigWidth = width; | 638 m_OutputWidth = m_OrigWidth = width; |
639 m_OutputHeight = m_OrigHeight = height; | 639 m_OutputHeight = m_OrigHeight = height; |
640 m_nComps = nComps; | 640 m_nComps = nComps; |
641 m_bpc = bpc; | 641 m_bpc = bpc; |
642 m_bColorTransformed = FALSE; | 642 m_bColorTransformed = FALSE; |
643 m_Pitch = (width * nComps * bpc + 7) / 8; | 643 m_Pitch = (width * nComps * bpc + 7) / 8; |
644 m_pScanline = FX_Alloc(FX_BYTE, m_Pitch); | 644 m_pScanline = FX_Alloc(uint8_t, m_Pitch); |
645 m_Predictor = 0; | 645 m_Predictor = 0; |
646 if (predictor) { | 646 if (predictor) { |
647 if (predictor >= 10) { | 647 if (predictor >= 10) { |
648 m_Predictor = 2; | 648 m_Predictor = 2; |
649 } else if (predictor == 2) { | 649 } else if (predictor == 2) { |
650 m_Predictor = 1; | 650 m_Predictor = 1; |
651 } | 651 } |
652 if (m_Predictor) { | 652 if (m_Predictor) { |
653 if (BitsPerComponent * Colors * Columns == 0) { | 653 if (BitsPerComponent * Colors * Columns == 0) { |
654 BitsPerComponent = m_bpc; | 654 BitsPerComponent = m_bpc; |
655 Colors = m_nComps; | 655 Colors = m_nComps; |
656 Columns = m_OrigWidth; | 656 Columns = m_OrigWidth; |
657 } | 657 } |
658 m_Colors = Colors; | 658 m_Colors = Colors; |
659 m_BitsPerComponent = BitsPerComponent; | 659 m_BitsPerComponent = BitsPerComponent; |
660 m_Columns = Columns; | 660 m_Columns = Columns; |
661 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; | 661 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; |
662 m_pLastLine = FX_Alloc(FX_BYTE, m_PredictPitch); | 662 m_pLastLine = FX_Alloc(uint8_t, m_PredictPitch); |
663 m_pPredictRaw = FX_Alloc(FX_BYTE, m_PredictPitch + 1); | 663 m_pPredictRaw = FX_Alloc(uint8_t, m_PredictPitch + 1); |
664 m_pPredictBuffer = FX_Alloc(FX_BYTE, m_PredictPitch); | 664 m_pPredictBuffer = FX_Alloc(uint8_t, m_PredictPitch); |
665 } | 665 } |
666 } | 666 } |
667 } | 667 } |
668 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() | 668 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() |
669 { | 669 { |
670 if (m_pFlate) { | 670 if (m_pFlate) { |
671 FPDFAPI_FlateEnd(m_pFlate); | 671 FPDFAPI_FlateEnd(m_pFlate); |
672 } | 672 } |
673 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 673 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
674 if (m_pFlate == NULL) { | 674 if (m_pFlate == NULL) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
730 FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_si
ze); | 730 FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_si
ze); |
731 static const FX_DWORD kMaxInitialAllocSize = 10000000; | 731 static const FX_DWORD kMaxInitialAllocSize = 10000000; |
732 if (guess_size > kMaxInitialAllocSize) { | 732 if (guess_size > kMaxInitialAllocSize) { |
733 guess_size = kMaxInitialAllocSize; | 733 guess_size = kMaxInitialAllocSize; |
734 alloc_step = kMaxInitialAllocSize; | 734 alloc_step = kMaxInitialAllocSize; |
735 } | 735 } |
736 FX_DWORD buf_size = guess_size; | 736 FX_DWORD buf_size = guess_size; |
737 FX_DWORD last_buf_size = buf_size; | 737 FX_DWORD last_buf_size = buf_size; |
738 void* context = nullptr; | 738 void* context = nullptr; |
739 | 739 |
740 FX_LPBYTE guess_buf = FX_Alloc(FX_BYTE, guess_size + 1); | 740 FX_LPBYTE guess_buf = FX_Alloc(uint8_t, guess_size + 1); |
741 FX_LPBYTE cur_buf = guess_buf; | 741 FX_LPBYTE cur_buf = guess_buf; |
742 guess_buf[guess_size] = '\0'; | 742 guess_buf[guess_size] = '\0'; |
743 context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 743 context = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
744 if (!context) | 744 if (!context) |
745 goto fail; | 745 goto fail; |
746 FPDFAPI_FlateInput(context, src_buf, src_size); | 746 FPDFAPI_FlateInput(context, src_buf, src_size); |
747 if (useOldImpl) { | 747 if (useOldImpl) { |
748 while (1) { | 748 while (1) { |
749 FX_INT32 ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); | 749 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); |
750 if (ret != Z_OK) | 750 if (ret != Z_OK) |
751 break; | 751 break; |
752 FX_INT32 avail_buf_size = FPDFAPI_FlateGetAvailOut(context); | 752 int32_t avail_buf_size = FPDFAPI_FlateGetAvailOut(context); |
753 if (avail_buf_size != 0) | 753 if (avail_buf_size != 0) |
754 break; | 754 break; |
755 | 755 |
756 // |avail_buf_size| == 0 case. | 756 // |avail_buf_size| == 0 case. |
757 FX_DWORD old_size = guess_size; | 757 FX_DWORD old_size = guess_size; |
758 guess_size += alloc_step; | 758 guess_size += alloc_step; |
759 if (guess_size < old_size || guess_size + 1 < guess_size) | 759 if (guess_size < old_size || guess_size + 1 < guess_size) |
760 goto fail; | 760 goto fail; |
761 guess_buf = FX_Realloc(FX_BYTE, guess_buf, guess_size + 1); | 761 guess_buf = FX_Realloc(uint8_t, guess_buf, guess_size + 1); |
762 if (!guess_buf) | 762 if (!guess_buf) |
763 goto fail; | 763 goto fail; |
764 guess_buf[guess_size] = '\0'; | 764 guess_buf[guess_size] = '\0'; |
765 cur_buf = guess_buf + old_size; | 765 cur_buf = guess_buf + old_size; |
766 buf_size = guess_size - old_size; | 766 buf_size = guess_size - old_size; |
767 } | 767 } |
768 dest_size = FPDFAPI_FlateGetTotalOut(context); | 768 dest_size = FPDFAPI_FlateGetTotalOut(context); |
769 offset = FPDFAPI_FlateGetTotalIn(context); | 769 offset = FPDFAPI_FlateGetTotalIn(context); |
770 if (guess_size / 2 > dest_size) { | 770 if (guess_size / 2 > dest_size) { |
771 guess_buf = FX_Realloc(FX_BYTE, guess_buf, dest_size + 1); | 771 guess_buf = FX_Realloc(uint8_t, guess_buf, dest_size + 1); |
772 if (!guess_buf) | 772 if (!guess_buf) |
773 goto fail; | 773 goto fail; |
774 guess_size = dest_size; | 774 guess_size = dest_size; |
775 guess_buf[guess_size] = '\0'; | 775 guess_buf[guess_size] = '\0'; |
776 } | 776 } |
777 dest_buf = guess_buf; | 777 dest_buf = guess_buf; |
778 } else { | 778 } else { |
779 CFX_ArrayTemplate<FX_LPBYTE> result_tmp_bufs; | 779 CFX_ArrayTemplate<FX_LPBYTE> result_tmp_bufs; |
780 while (1) { | 780 while (1) { |
781 FX_INT32 ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); | 781 int32_t ret = FPDFAPI_FlateOutput(context, cur_buf, buf_size); |
782 FX_INT32 avail_buf_size = FPDFAPI_FlateGetAvailOut(context); | 782 int32_t avail_buf_size = FPDFAPI_FlateGetAvailOut(context); |
783 if (ret != Z_OK) { | 783 if (ret != Z_OK) { |
784 last_buf_size = buf_size - avail_buf_size; | 784 last_buf_size = buf_size - avail_buf_size; |
785 result_tmp_bufs.Add(cur_buf); | 785 result_tmp_bufs.Add(cur_buf); |
786 break; | 786 break; |
787 } | 787 } |
788 if (avail_buf_size != 0) { | 788 if (avail_buf_size != 0) { |
789 last_buf_size = buf_size - avail_buf_size; | 789 last_buf_size = buf_size - avail_buf_size; |
790 result_tmp_bufs.Add(cur_buf); | 790 result_tmp_bufs.Add(cur_buf); |
791 break; | 791 break; |
792 } | 792 } |
793 | 793 |
794 // |avail_buf_size| == 0 case. | 794 // |avail_buf_size| == 0 case. |
795 result_tmp_bufs.Add(cur_buf); | 795 result_tmp_bufs.Add(cur_buf); |
796 cur_buf = FX_Alloc(FX_BYTE, buf_size + 1); | 796 cur_buf = FX_Alloc(uint8_t, buf_size + 1); |
797 cur_buf[buf_size] = '\0'; | 797 cur_buf[buf_size] = '\0'; |
798 } | 798 } |
799 dest_size = FPDFAPI_FlateGetTotalOut(context); | 799 dest_size = FPDFAPI_FlateGetTotalOut(context); |
800 offset = FPDFAPI_FlateGetTotalIn(context); | 800 offset = FPDFAPI_FlateGetTotalIn(context); |
801 if (result_tmp_bufs.GetSize() == 1) { | 801 if (result_tmp_bufs.GetSize() == 1) { |
802 dest_buf = result_tmp_bufs[0]; | 802 dest_buf = result_tmp_bufs[0]; |
803 } else { | 803 } else { |
804 FX_LPBYTE result_buf = FX_Alloc(FX_BYTE, dest_size); | 804 FX_LPBYTE result_buf = FX_Alloc(uint8_t, dest_size); |
805 FX_DWORD result_pos = 0; | 805 FX_DWORD result_pos = 0; |
806 for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) { | 806 for (int32_t i = 0; i < result_tmp_bufs.GetSize(); i++) { |
807 FX_LPBYTE tmp_buf = result_tmp_bufs[i]; | 807 FX_LPBYTE tmp_buf = result_tmp_bufs[i]; |
808 FX_DWORD tmp_buf_size = buf_size; | 808 FX_DWORD tmp_buf_size = buf_size; |
809 if (i == result_tmp_bufs.GetSize() - 1) { | 809 if (i == result_tmp_bufs.GetSize() - 1) { |
810 tmp_buf_size = last_buf_size; | 810 tmp_buf_size = last_buf_size; |
811 } | 811 } |
812 FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size); | 812 FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size); |
813 result_pos += tmp_buf_size; | 813 result_pos += tmp_buf_size; |
814 FX_Free(result_tmp_bufs[i]); | 814 FX_Free(result_tmp_bufs[i]); |
815 } | 815 } |
816 dest_buf = result_buf; | 816 dest_buf = result_buf; |
817 } | 817 } |
818 } | 818 } |
819 FPDFAPI_FlateEnd(context); | 819 FPDFAPI_FlateEnd(context); |
820 return; | 820 return; |
821 | 821 |
822 fail: | 822 fail: |
823 FX_Free(guess_buf); | 823 FX_Free(guess_buf); |
824 dest_buf = nullptr; | 824 dest_buf = nullptr; |
825 dest_size = 0; | 825 dest_size = 0; |
826 return; | 826 return; |
827 } | 827 } |
828 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(FX_LPCBYTE src_buf, FX
_DWORD src_size, int width, int height, | 828 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(FX_LPCBYTE src_buf, FX
_DWORD src_size, int width, int height, |
829 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) | 829 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) |
830 { | 830 { |
831 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; | 831 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; |
832 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, C
olors, BitsPerComponent, Columns); | 832 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, C
olors, BitsPerComponent, Columns); |
833 return pDecoder; | 833 return pDecoder; |
834 } | 834 } |
835 FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_b
uf, FX_DWORD src_size, FX_BOOL bEarlyChange, | 835 FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, const uint8_t* src_b
uf, FX_DWORD src_size, FX_BOOL bEarlyChange, |
836 int predictor, int Colors, int BitsPerComponent, int Columns, | 836 int predictor, int Colors, int BitsPerComponent, int Columns, |
837 FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size) | 837 FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size) |
838 { | 838 { |
839 dest_buf = NULL; | 839 dest_buf = NULL; |
840 FX_DWORD offset = 0; | 840 FX_DWORD offset = 0; |
841 int predictor_type = 0; | 841 int predictor_type = 0; |
842 if (predictor) { | 842 if (predictor) { |
843 if (predictor >= 10) { | 843 if (predictor >= 10) { |
844 predictor_type = 2; | 844 predictor_type = 2; |
845 } else if (predictor == 2) { | 845 } else if (predictor == 2) { |
846 predictor_type = 1; | 846 predictor_type = 1; |
847 } | 847 } |
848 } | 848 } |
849 if (bLZW) { | 849 if (bLZW) { |
850 { | 850 { |
851 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 851 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
852 dest_size = (FX_DWORD) - 1; | 852 dest_size = (FX_DWORD) - 1; |
853 offset = src_size; | 853 offset = src_size; |
854 int err = decoder->Decode(NULL, dest_size, src_buf, offset, | 854 int err = decoder->Decode(NULL, dest_size, src_buf, offset, |
855 bEarlyChange); | 855 bEarlyChange); |
856 if (err || dest_size == 0 || dest_size + 1 < dest_size) { | 856 if (err || dest_size == 0 || dest_size + 1 < dest_size) { |
857 return -1; | 857 return -1; |
858 } | 858 } |
859 } | 859 } |
860 { | 860 { |
861 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); | 861 nonstd::unique_ptr<CLZWDecoder> decoder(new CLZWDecoder); |
862 dest_buf = FX_Alloc( FX_BYTE, dest_size + 1); | 862 dest_buf = FX_Alloc( uint8_t, dest_size + 1); |
863 dest_buf[dest_size] = '\0'; | 863 dest_buf[dest_size] = '\0'; |
864 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); | 864 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); |
865 } | 865 } |
866 } else { | 866 } else { |
867 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); | 867 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); |
868 } | 868 } |
869 if (predictor_type == 0) { | 869 if (predictor_type == 0) { |
870 return offset; | 870 return offset; |
871 } | 871 } |
872 FX_BOOL ret = TRUE; | 872 FX_BOOL ret = TRUE; |
873 if (predictor_type == 2) { | 873 if (predictor_type == 2) { |
874 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 874 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
875 Columns); | 875 Columns); |
876 } else if (predictor_type == 1) { | 876 } else if (predictor_type == 1) { |
877 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 877 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
878 Columns); | 878 Columns); |
879 } | 879 } |
880 return ret ? offset : -1; | 880 return ret ? offset : -1; |
881 } | 881 } |
882 FX_BOOL CCodec_FlateModule::Encode(const FX_BYTE* src_buf, FX_DWORD src_size, | 882 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, |
883 int predictor, int Colors, int BitsPerCompone
nt, int Columns, | 883 int predictor, int Colors, int BitsPerCompone
nt, int Columns, |
884 FX_LPBYTE& dest_buf, FX_DWORD& dest_size) | 884 FX_LPBYTE& dest_buf, FX_DWORD& dest_size) |
885 { | 885 { |
886 if (predictor != 2 && predictor < 10) { | 886 if (predictor != 2 && predictor < 10) { |
887 return Encode(src_buf, src_size, dest_buf, dest_size); | 887 return Encode(src_buf, src_size, dest_buf, dest_size); |
888 } | 888 } |
889 FX_LPBYTE pSrcBuf = NULL; | 889 FX_LPBYTE pSrcBuf = NULL; |
890 pSrcBuf = FX_Alloc(FX_BYTE, src_size); | 890 pSrcBuf = FX_Alloc(uint8_t, src_size); |
891 FXSYS_memcpy32(pSrcBuf, src_buf, src_size); | 891 FXSYS_memcpy32(pSrcBuf, src_buf, src_size); |
892 FX_BOOL ret = TRUE; | 892 FX_BOOL ret = TRUE; |
893 if (predictor == 2) { | 893 if (predictor == 2) { |
894 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, | 894 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, |
895 Columns); | 895 Columns); |
896 } else if (predictor >= 10) { | 896 } else if (predictor >= 10) { |
897 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, | 897 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, |
898 BitsPerComponent, Columns); | 898 BitsPerComponent, Columns); |
899 } | 899 } |
900 if (ret) | 900 if (ret) |
901 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); | 901 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); |
902 FX_Free(pSrcBuf); | 902 FX_Free(pSrcBuf); |
903 return ret; | 903 return ret; |
904 } | 904 } |
905 FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPB
YTE& dest_buf, FX_DWORD& dest_size) | 905 FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPB
YTE& dest_buf, FX_DWORD& dest_size) |
906 { | 906 { |
907 dest_size = src_size + src_size / 1000 + 12; | 907 dest_size = src_size + src_size / 1000 + 12; |
908 dest_buf = FX_Alloc( FX_BYTE, dest_size); | 908 dest_buf = FX_Alloc( uint8_t, dest_size); |
909 unsigned long temp_size = dest_size; | 909 unsigned long temp_size = dest_size; |
910 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 910 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
911 dest_size = (FX_DWORD)temp_size; | 911 dest_size = (FX_DWORD)temp_size; |
912 return TRUE; | 912 return TRUE; |
913 } | 913 } |
OLD | NEW |