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/include/fxcodec/fx_codec.h" | 7 #include "core/include/fxcodec/fx_codec.h" |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 #include <utility> | 10 #include <utility> |
(...skipping 14 matching lines...) Expand all Loading... |
25 m_pPngModule(new CCodec_PngModule), | 25 m_pPngModule(new CCodec_PngModule), |
26 m_pGifModule(new CCodec_GifModule), | 26 m_pGifModule(new CCodec_GifModule), |
27 m_pBmpModule(new CCodec_BmpModule), | 27 m_pBmpModule(new CCodec_BmpModule), |
28 m_pTiffModule(new CCodec_TiffModule), | 28 m_pTiffModule(new CCodec_TiffModule), |
29 #endif // PDF_ENABLE_XFA | 29 #endif // PDF_ENABLE_XFA |
30 m_pFlateModule(new CCodec_FlateModule) { | 30 m_pFlateModule(new CCodec_FlateModule) { |
31 } | 31 } |
32 | 32 |
33 CCodec_ScanlineDecoder::ImageDataCache::ImageDataCache(int width, | 33 CCodec_ScanlineDecoder::ImageDataCache::ImageDataCache(int width, |
34 int height, | 34 int height, |
35 FX_DWORD pitch) | 35 uint32_t pitch) |
36 : m_Width(width), m_Height(height), m_Pitch(pitch), m_nCachedLines(0) {} | 36 : m_Width(width), m_Height(height), m_Pitch(pitch), m_nCachedLines(0) {} |
37 | 37 |
38 CCodec_ScanlineDecoder::ImageDataCache::~ImageDataCache() {} | 38 CCodec_ScanlineDecoder::ImageDataCache::~ImageDataCache() {} |
39 | 39 |
40 bool CCodec_ScanlineDecoder::ImageDataCache::AllocateCache() { | 40 bool CCodec_ScanlineDecoder::ImageDataCache::AllocateCache() { |
41 if (m_Pitch == 0 || m_Height < 0) | 41 if (m_Pitch == 0 || m_Height < 0) |
42 return false; | 42 return false; |
43 | 43 |
44 FX_SAFE_SIZE_T size = m_Pitch; | 44 FX_SAFE_SIZE_T size = m_Pitch; |
45 size *= m_Height; | 45 size *= m_Height; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 140 |
141 std::unique_ptr<ImageDataCache> cache( | 141 std::unique_ptr<ImageDataCache> cache( |
142 new ImageDataCache(m_OutputWidth, m_OutputHeight, m_Pitch)); | 142 new ImageDataCache(m_OutputWidth, m_OutputHeight, m_Pitch)); |
143 if (!cache->AllocateCache()) | 143 if (!cache->AllocateCache()) |
144 return; | 144 return; |
145 | 145 |
146 m_pDataCache = std::move(cache); | 146 m_pDataCache = std::move(cache); |
147 } | 147 } |
148 | 148 |
149 FX_BOOL CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, | 149 FX_BOOL CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, |
150 FX_DWORD src_size, | 150 uint32_t src_size, |
151 uint8_t*& dest_buf, | 151 uint8_t*& dest_buf, |
152 FX_DWORD& dest_size) { | 152 uint32_t& dest_size) { |
153 return FALSE; | 153 return FALSE; |
154 } | 154 } |
155 | 155 |
156 #define EXPONENT_DETECT(ptr) \ | 156 #define EXPONENT_DETECT(ptr) \ |
157 for (;; ptr++) { \ | 157 for (;; ptr++) { \ |
158 if (!std::isdigit(*ptr)) { \ | 158 if (!std::isdigit(*ptr)) { \ |
159 if (endptr) \ | 159 if (endptr) \ |
160 *endptr = (char*)ptr; \ | 160 *endptr = (char*)ptr; \ |
161 break; \ | 161 break; \ |
162 } else { \ | 162 } else { \ |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 } else { | 249 } else { |
250 while (exp_ret--) { | 250 while (exp_ret--) { |
251 ret /= 10.0; | 251 ret /= 10.0; |
252 } | 252 } |
253 } | 253 } |
254 return is_negative ? -ret : ret; | 254 return is_negative ? -ret : ret; |
255 } | 255 } |
256 #undef EXPONENT_DETECT | 256 #undef EXPONENT_DETECT |
257 | 257 |
258 FX_BOOL CCodec_BasicModule::A85Encode(const uint8_t* src_buf, | 258 FX_BOOL CCodec_BasicModule::A85Encode(const uint8_t* src_buf, |
259 FX_DWORD src_size, | 259 uint32_t src_size, |
260 uint8_t*& dest_buf, | 260 uint8_t*& dest_buf, |
261 FX_DWORD& dest_size) { | 261 uint32_t& dest_size) { |
262 return FALSE; | 262 return FALSE; |
263 } | 263 } |
264 | 264 |
265 #ifdef PDF_ENABLE_XFA | 265 #ifdef PDF_ENABLE_XFA |
266 CFX_DIBAttribute::CFX_DIBAttribute() | 266 CFX_DIBAttribute::CFX_DIBAttribute() |
267 : m_nXDPI(-1), | 267 : m_nXDPI(-1), |
268 m_nYDPI(-1), | 268 m_nYDPI(-1), |
269 m_fAspectRatio(-1.0f), | 269 m_fAspectRatio(-1.0f), |
270 m_wDPIUnit(0), | 270 m_wDPIUnit(0), |
271 m_nGifLeft(0), | 271 m_nGifLeft(0), |
272 m_nGifTop(0), | 272 m_nGifTop(0), |
273 m_pGifLocalPalette(nullptr), | 273 m_pGifLocalPalette(nullptr), |
274 m_nGifLocalPalNum(0), | 274 m_nGifLocalPalNum(0), |
275 m_nBmpCompressType(0) { | 275 m_nBmpCompressType(0) { |
276 FXSYS_memset(m_strTime, 0, sizeof(m_strTime)); | 276 FXSYS_memset(m_strTime, 0, sizeof(m_strTime)); |
277 } | 277 } |
278 CFX_DIBAttribute::~CFX_DIBAttribute() { | 278 CFX_DIBAttribute::~CFX_DIBAttribute() { |
279 for (const auto& pair : m_Exif) | 279 for (const auto& pair : m_Exif) |
280 FX_Free(pair.second); | 280 FX_Free(pair.second); |
281 } | 281 } |
282 #endif // PDF_ENABLE_XFA | 282 #endif // PDF_ENABLE_XFA |
283 | 283 |
284 class CCodec_RLScanlineDecoder : public CCodec_ScanlineDecoder { | 284 class CCodec_RLScanlineDecoder : public CCodec_ScanlineDecoder { |
285 public: | 285 public: |
286 CCodec_RLScanlineDecoder(); | 286 CCodec_RLScanlineDecoder(); |
287 ~CCodec_RLScanlineDecoder() override; | 287 ~CCodec_RLScanlineDecoder() override; |
288 | 288 |
289 FX_BOOL Create(const uint8_t* src_buf, | 289 FX_BOOL Create(const uint8_t* src_buf, |
290 FX_DWORD src_size, | 290 uint32_t src_size, |
291 int width, | 291 int width, |
292 int height, | 292 int height, |
293 int nComps, | 293 int nComps, |
294 int bpc); | 294 int bpc); |
295 | 295 |
296 // CCodec_ScanlineDecoder | 296 // CCodec_ScanlineDecoder |
297 void v_DownScale(int dest_width, int dest_height) override {} | 297 void v_DownScale(int dest_width, int dest_height) override {} |
298 FX_BOOL v_Rewind() override; | 298 FX_BOOL v_Rewind() override; |
299 uint8_t* v_GetNextLine() override; | 299 uint8_t* v_GetNextLine() override; |
300 FX_DWORD GetSrcOffset() override { return m_SrcOffset; } | 300 uint32_t GetSrcOffset() override { return m_SrcOffset; } |
301 | 301 |
302 protected: | 302 protected: |
303 FX_BOOL CheckDestSize(); | 303 FX_BOOL CheckDestSize(); |
304 void GetNextOperator(); | 304 void GetNextOperator(); |
305 void UpdateOperator(uint8_t used_bytes); | 305 void UpdateOperator(uint8_t used_bytes); |
306 | 306 |
307 uint8_t* m_pScanline; | 307 uint8_t* m_pScanline; |
308 const uint8_t* m_pSrcBuf; | 308 const uint8_t* m_pSrcBuf; |
309 FX_DWORD m_SrcSize; | 309 uint32_t m_SrcSize; |
310 FX_DWORD m_dwLineBytes; | 310 uint32_t m_dwLineBytes; |
311 FX_DWORD m_SrcOffset; | 311 uint32_t m_SrcOffset; |
312 FX_BOOL m_bEOD; | 312 FX_BOOL m_bEOD; |
313 uint8_t m_Operator; | 313 uint8_t m_Operator; |
314 }; | 314 }; |
315 CCodec_RLScanlineDecoder::CCodec_RLScanlineDecoder() | 315 CCodec_RLScanlineDecoder::CCodec_RLScanlineDecoder() |
316 : m_pScanline(NULL), | 316 : m_pScanline(NULL), |
317 m_pSrcBuf(NULL), | 317 m_pSrcBuf(NULL), |
318 m_SrcSize(0), | 318 m_SrcSize(0), |
319 m_dwLineBytes(0), | 319 m_dwLineBytes(0), |
320 m_SrcOffset(0), | 320 m_SrcOffset(0), |
321 m_bEOD(FALSE), | 321 m_bEOD(FALSE), |
322 m_Operator(0) {} | 322 m_Operator(0) {} |
323 CCodec_RLScanlineDecoder::~CCodec_RLScanlineDecoder() { | 323 CCodec_RLScanlineDecoder::~CCodec_RLScanlineDecoder() { |
324 FX_Free(m_pScanline); | 324 FX_Free(m_pScanline); |
325 } | 325 } |
326 FX_BOOL CCodec_RLScanlineDecoder::CheckDestSize() { | 326 FX_BOOL CCodec_RLScanlineDecoder::CheckDestSize() { |
327 FX_DWORD i = 0; | 327 uint32_t i = 0; |
328 FX_DWORD old_size = 0; | 328 uint32_t old_size = 0; |
329 FX_DWORD dest_size = 0; | 329 uint32_t dest_size = 0; |
330 while (i < m_SrcSize) { | 330 while (i < m_SrcSize) { |
331 if (m_pSrcBuf[i] < 128) { | 331 if (m_pSrcBuf[i] < 128) { |
332 old_size = dest_size; | 332 old_size = dest_size; |
333 dest_size += m_pSrcBuf[i] + 1; | 333 dest_size += m_pSrcBuf[i] + 1; |
334 if (dest_size < old_size) { | 334 if (dest_size < old_size) { |
335 return FALSE; | 335 return FALSE; |
336 } | 336 } |
337 i += m_pSrcBuf[i] + 2; | 337 i += m_pSrcBuf[i] + 2; |
338 } else if (m_pSrcBuf[i] > 128) { | 338 } else if (m_pSrcBuf[i] > 128) { |
339 old_size = dest_size; | 339 old_size = dest_size; |
340 dest_size += 257 - m_pSrcBuf[i]; | 340 dest_size += 257 - m_pSrcBuf[i]; |
341 if (dest_size < old_size) { | 341 if (dest_size < old_size) { |
342 return FALSE; | 342 return FALSE; |
343 } | 343 } |
344 i += 2; | 344 i += 2; |
345 } else { | 345 } else { |
346 break; | 346 break; |
347 } | 347 } |
348 } | 348 } |
349 if (((FX_DWORD)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 > | 349 if (((uint32_t)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 > |
350 dest_size) { | 350 dest_size) { |
351 return FALSE; | 351 return FALSE; |
352 } | 352 } |
353 return TRUE; | 353 return TRUE; |
354 } | 354 } |
355 FX_BOOL CCodec_RLScanlineDecoder::Create(const uint8_t* src_buf, | 355 FX_BOOL CCodec_RLScanlineDecoder::Create(const uint8_t* src_buf, |
356 FX_DWORD src_size, | 356 uint32_t src_size, |
357 int width, | 357 int width, |
358 int height, | 358 int height, |
359 int nComps, | 359 int nComps, |
360 int bpc) { | 360 int bpc) { |
361 m_pSrcBuf = src_buf; | 361 m_pSrcBuf = src_buf; |
362 m_SrcSize = src_size; | 362 m_SrcSize = src_size; |
363 m_OutputWidth = m_OrigWidth = width; | 363 m_OutputWidth = m_OrigWidth = width; |
364 m_OutputHeight = m_OrigHeight = height; | 364 m_OutputHeight = m_OrigHeight = height; |
365 m_nComps = nComps; | 365 m_nComps = nComps; |
366 m_bpc = bpc; | 366 m_bpc = bpc; |
367 m_bColorTransformed = FALSE; | 367 m_bColorTransformed = FALSE; |
368 m_DownScale = 1; | 368 m_DownScale = 1; |
369 // Aligning the pitch to 4 bytes requires an integer overflow check. | 369 // Aligning the pitch to 4 bytes requires an integer overflow check. |
370 FX_SAFE_DWORD pitch = width; | 370 FX_SAFE_DWORD pitch = width; |
371 pitch *= nComps; | 371 pitch *= nComps; |
372 pitch *= bpc; | 372 pitch *= bpc; |
373 pitch += 31; | 373 pitch += 31; |
374 pitch /= 32; | 374 pitch /= 32; |
375 pitch *= 4; | 375 pitch *= 4; |
376 if (!pitch.IsValid()) { | 376 if (!pitch.IsValid()) { |
377 return FALSE; | 377 return FALSE; |
378 } | 378 } |
379 m_Pitch = pitch.ValueOrDie(); | 379 m_Pitch = pitch.ValueOrDie(); |
380 // Overflow should already have been checked before this is called. | 380 // Overflow should already have been checked before this is called. |
381 m_dwLineBytes = (static_cast<FX_DWORD>(width) * nComps * bpc + 7) / 8; | 381 m_dwLineBytes = (static_cast<uint32_t>(width) * nComps * bpc + 7) / 8; |
382 m_pScanline = FX_Alloc(uint8_t, m_Pitch); | 382 m_pScanline = FX_Alloc(uint8_t, m_Pitch); |
383 return CheckDestSize(); | 383 return CheckDestSize(); |
384 } | 384 } |
385 FX_BOOL CCodec_RLScanlineDecoder::v_Rewind() { | 385 FX_BOOL CCodec_RLScanlineDecoder::v_Rewind() { |
386 FXSYS_memset(m_pScanline, 0, m_Pitch); | 386 FXSYS_memset(m_pScanline, 0, m_Pitch); |
387 m_SrcOffset = 0; | 387 m_SrcOffset = 0; |
388 m_bEOD = FALSE; | 388 m_bEOD = FALSE; |
389 m_Operator = 0; | 389 m_Operator = 0; |
390 return TRUE; | 390 return TRUE; |
391 } | 391 } |
392 uint8_t* CCodec_RLScanlineDecoder::v_GetNextLine() { | 392 uint8_t* CCodec_RLScanlineDecoder::v_GetNextLine() { |
393 if (m_SrcOffset == 0) { | 393 if (m_SrcOffset == 0) { |
394 GetNextOperator(); | 394 GetNextOperator(); |
395 } else { | 395 } else { |
396 if (m_bEOD) { | 396 if (m_bEOD) { |
397 return NULL; | 397 return NULL; |
398 } | 398 } |
399 } | 399 } |
400 FXSYS_memset(m_pScanline, 0, m_Pitch); | 400 FXSYS_memset(m_pScanline, 0, m_Pitch); |
401 FX_DWORD col_pos = 0; | 401 uint32_t col_pos = 0; |
402 FX_BOOL eol = FALSE; | 402 FX_BOOL eol = FALSE; |
403 while (m_SrcOffset < m_SrcSize && !eol) { | 403 while (m_SrcOffset < m_SrcSize && !eol) { |
404 if (m_Operator < 128) { | 404 if (m_Operator < 128) { |
405 FX_DWORD copy_len = m_Operator + 1; | 405 uint32_t copy_len = m_Operator + 1; |
406 if (col_pos + copy_len >= m_dwLineBytes) { | 406 if (col_pos + copy_len >= m_dwLineBytes) { |
407 copy_len = m_dwLineBytes - col_pos; | 407 copy_len = m_dwLineBytes - col_pos; |
408 eol = TRUE; | 408 eol = TRUE; |
409 } | 409 } |
410 if (copy_len >= m_SrcSize - m_SrcOffset) { | 410 if (copy_len >= m_SrcSize - m_SrcOffset) { |
411 copy_len = m_SrcSize - m_SrcOffset; | 411 copy_len = m_SrcSize - m_SrcOffset; |
412 m_bEOD = TRUE; | 412 m_bEOD = TRUE; |
413 } | 413 } |
414 FXSYS_memcpy(m_pScanline + col_pos, m_pSrcBuf + m_SrcOffset, copy_len); | 414 FXSYS_memcpy(m_pScanline + col_pos, m_pSrcBuf + m_SrcOffset, copy_len); |
415 col_pos += copy_len; | 415 col_pos += copy_len; |
416 UpdateOperator((uint8_t)copy_len); | 416 UpdateOperator((uint8_t)copy_len); |
417 } else if (m_Operator > 128) { | 417 } else if (m_Operator > 128) { |
418 int fill = 0; | 418 int fill = 0; |
419 if (m_SrcOffset - 1 < m_SrcSize - 1) { | 419 if (m_SrcOffset - 1 < m_SrcSize - 1) { |
420 fill = m_pSrcBuf[m_SrcOffset]; | 420 fill = m_pSrcBuf[m_SrcOffset]; |
421 } | 421 } |
422 FX_DWORD duplicate_len = 257 - m_Operator; | 422 uint32_t duplicate_len = 257 - m_Operator; |
423 if (col_pos + duplicate_len >= m_dwLineBytes) { | 423 if (col_pos + duplicate_len >= m_dwLineBytes) { |
424 duplicate_len = m_dwLineBytes - col_pos; | 424 duplicate_len = m_dwLineBytes - col_pos; |
425 eol = TRUE; | 425 eol = TRUE; |
426 } | 426 } |
427 FXSYS_memset(m_pScanline + col_pos, fill, duplicate_len); | 427 FXSYS_memset(m_pScanline + col_pos, fill, duplicate_len); |
428 col_pos += duplicate_len; | 428 col_pos += duplicate_len; |
429 UpdateOperator((uint8_t)duplicate_len); | 429 UpdateOperator((uint8_t)duplicate_len); |
430 } else { | 430 } else { |
431 m_bEOD = TRUE; | 431 m_bEOD = TRUE; |
432 break; | 432 break; |
433 } | 433 } |
434 } | 434 } |
435 return m_pScanline; | 435 return m_pScanline; |
436 } | 436 } |
437 void CCodec_RLScanlineDecoder::GetNextOperator() { | 437 void CCodec_RLScanlineDecoder::GetNextOperator() { |
438 if (m_SrcOffset >= m_SrcSize) { | 438 if (m_SrcOffset >= m_SrcSize) { |
439 m_Operator = 128; | 439 m_Operator = 128; |
440 return; | 440 return; |
441 } | 441 } |
442 m_Operator = m_pSrcBuf[m_SrcOffset]; | 442 m_Operator = m_pSrcBuf[m_SrcOffset]; |
443 m_SrcOffset++; | 443 m_SrcOffset++; |
444 } | 444 } |
445 void CCodec_RLScanlineDecoder::UpdateOperator(uint8_t used_bytes) { | 445 void CCodec_RLScanlineDecoder::UpdateOperator(uint8_t used_bytes) { |
446 if (used_bytes == 0) { | 446 if (used_bytes == 0) { |
447 return; | 447 return; |
448 } | 448 } |
449 if (m_Operator < 128) { | 449 if (m_Operator < 128) { |
450 FXSYS_assert((FX_DWORD)m_Operator + 1 >= used_bytes); | 450 FXSYS_assert((uint32_t)m_Operator + 1 >= used_bytes); |
451 if (used_bytes == m_Operator + 1) { | 451 if (used_bytes == m_Operator + 1) { |
452 m_SrcOffset += used_bytes; | 452 m_SrcOffset += used_bytes; |
453 GetNextOperator(); | 453 GetNextOperator(); |
454 return; | 454 return; |
455 } | 455 } |
456 m_Operator -= used_bytes; | 456 m_Operator -= used_bytes; |
457 m_SrcOffset += used_bytes; | 457 m_SrcOffset += used_bytes; |
458 if (m_SrcOffset >= m_SrcSize) { | 458 if (m_SrcOffset >= m_SrcSize) { |
459 m_Operator = 128; | 459 m_Operator = 128; |
460 } | 460 } |
461 return; | 461 return; |
462 } | 462 } |
463 uint8_t count = 257 - m_Operator; | 463 uint8_t count = 257 - m_Operator; |
464 FXSYS_assert((FX_DWORD)count >= used_bytes); | 464 FXSYS_assert((uint32_t)count >= used_bytes); |
465 if (used_bytes == count) { | 465 if (used_bytes == count) { |
466 m_SrcOffset++; | 466 m_SrcOffset++; |
467 GetNextOperator(); | 467 GetNextOperator(); |
468 return; | 468 return; |
469 } | 469 } |
470 count -= used_bytes; | 470 count -= used_bytes; |
471 m_Operator = 257 - count; | 471 m_Operator = 257 - count; |
472 } | 472 } |
473 ICodec_ScanlineDecoder* CCodec_BasicModule::CreateRunLengthDecoder( | 473 ICodec_ScanlineDecoder* CCodec_BasicModule::CreateRunLengthDecoder( |
474 const uint8_t* src_buf, | 474 const uint8_t* src_buf, |
475 FX_DWORD src_size, | 475 uint32_t src_size, |
476 int width, | 476 int width, |
477 int height, | 477 int height, |
478 int nComps, | 478 int nComps, |
479 int bpc) { | 479 int bpc) { |
480 CCodec_RLScanlineDecoder* pRLScanlineDecoder = new CCodec_RLScanlineDecoder; | 480 CCodec_RLScanlineDecoder* pRLScanlineDecoder = new CCodec_RLScanlineDecoder; |
481 if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, | 481 if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, |
482 bpc)) { | 482 bpc)) { |
483 delete pRLScanlineDecoder; | 483 delete pRLScanlineDecoder; |
484 return NULL; | 484 return NULL; |
485 } | 485 } |
486 return pRLScanlineDecoder; | 486 return pRLScanlineDecoder; |
487 } | 487 } |
OLD | NEW |