| 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 "../../../include/fxcodec/fx_codec.h" | 7 #include "../../../include/fxcodec/fx_codec.h" |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| 11 #include "../../../../third_party/base/logging.h" | 11 #include "../../../../third_party/base/logging.h" |
| 12 #include "../../../include/fxcrt/fx_safe_types.h" | 12 #include "../../../include/fxcrt/fx_safe_types.h" |
| 13 #include "codec_int.h" | 13 #include "codec_int.h" |
| 14 | 14 |
| 15 CCodec_ModuleMgr::CCodec_ModuleMgr() | 15 CCodec_ModuleMgr::CCodec_ModuleMgr() |
| 16 : m_pBasicModule(new CCodec_BasicModule), | 16 : m_pBasicModule(new CCodec_BasicModule), |
| 17 m_pFaxModule(new CCodec_FaxModule), | 17 m_pFaxModule(new CCodec_FaxModule), |
| 18 m_pJpegModule(new CCodec_JpegModule), | 18 m_pJpegModule(new CCodec_JpegModule), |
| 19 m_pJpxModule(new CCodec_JpxModule), | 19 m_pJpxModule(new CCodec_JpxModule), |
| 20 m_pJbig2Module(new CCodec_Jbig2Module), | 20 m_pJbig2Module(new CCodec_Jbig2Module), |
| 21 m_pIccModule(new CCodec_IccModule), | 21 m_pIccModule(new CCodec_IccModule), |
| 22 m_pFlateModule(new CCodec_FlateModule) {} | 22 m_pFlateModule(new CCodec_FlateModule) {} |
| 23 | 23 |
| 24 CCodec_ScanlineDecoder::ImageDataCache::ImageDataCache(int width, | 24 CCodec_ScanlineDecoder::ImageDataCache::ImageDataCache(int width, |
| 25 int height, | 25 int height, |
| 26 int pitch) | 26 FX_DWORD pitch) |
| 27 : m_Width(width), m_Height(height), m_Pitch(pitch), m_nCachedLines(0) { | 27 : m_Width(width), m_Height(height), m_Pitch(pitch), m_nCachedLines(0) { |
| 28 } | 28 } |
| 29 | 29 |
| 30 CCodec_ScanlineDecoder::ImageDataCache::~ImageDataCache() { | 30 CCodec_ScanlineDecoder::ImageDataCache::~ImageDataCache() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 bool CCodec_ScanlineDecoder::ImageDataCache::AllocateCache() { | 33 bool CCodec_ScanlineDecoder::ImageDataCache::AllocateCache() { |
| 34 if (m_Pitch <= 0 || m_Height < 0) | 34 if (m_Pitch == 0 || m_Height < 0) |
| 35 return false; | 35 return false; |
| 36 | 36 |
| 37 FX_SAFE_SIZE_T size = m_Pitch; | 37 FX_SAFE_SIZE_T size = m_Pitch; |
| 38 size *= m_Height; | 38 size *= m_Height; |
| 39 if (!size.IsValid()) | 39 if (!size.IsValid()) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 m_Data.reset(FX_TryAlloc(uint8_t, size.ValueOrDie())); | 42 m_Data.reset(FX_TryAlloc(uint8_t, size.ValueOrDie())); |
| 43 return IsValid(); | 43 return IsValid(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void CCodec_ScanlineDecoder::ImageDataCache::AppendLine(const uint8_t* line) { | 46 void CCodec_ScanlineDecoder::ImageDataCache::AppendLine(const uint8_t* line) { |
| 47 // If the callers adds more lines than there is room, fail. | 47 // If the callers adds more lines than there is room, fail. |
| 48 if (m_Pitch <= 0 || m_nCachedLines >= m_Height) { | 48 if (m_Pitch == 0 || m_nCachedLines >= m_Height) { |
| 49 NOTREACHED(); | 49 NOTREACHED(); |
| 50 return; | 50 return; |
| 51 } | 51 } |
| 52 | 52 |
| 53 size_t offset = m_Pitch; | 53 size_t offset = m_Pitch; |
| 54 FXSYS_memcpy(m_Data.get() + offset * m_nCachedLines, line, m_Pitch); | 54 FXSYS_memcpy(m_Data.get() + offset * m_nCachedLines, line, m_Pitch); |
| 55 ++m_nCachedLines; | 55 ++m_nCachedLines; |
| 56 } | 56 } |
| 57 | 57 |
| 58 const uint8_t* CCodec_ScanlineDecoder::ImageDataCache::GetLine(int line) const { | 58 const uint8_t* CCodec_ScanlineDecoder::ImageDataCache::GetLine(int line) const { |
| 59 if (m_Pitch <= 0 || line < 0 || line >= m_nCachedLines) | 59 if (m_Pitch == 0 || line < 0 || line >= m_nCachedLines) |
| 60 return nullptr; | 60 return nullptr; |
| 61 | 61 |
| 62 size_t offset = m_Pitch; | 62 size_t offset = m_Pitch; |
| 63 return m_Data.get() + offset * line; | 63 return m_Data.get() + offset * line; |
| 64 } | 64 } |
| 65 | 65 |
| 66 CCodec_ScanlineDecoder::CCodec_ScanlineDecoder() | 66 CCodec_ScanlineDecoder::CCodec_ScanlineDecoder() |
| 67 : m_NextLine(-1), m_pLastScanline(nullptr) { | 67 : m_NextLine(-1), m_pLastScanline(nullptr) { |
| 68 } | 68 } |
| 69 | 69 |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 int nComps, | 331 int nComps, |
| 332 int bpc) { | 332 int bpc) { |
| 333 m_pSrcBuf = src_buf; | 333 m_pSrcBuf = src_buf; |
| 334 m_SrcSize = src_size; | 334 m_SrcSize = src_size; |
| 335 m_OutputWidth = m_OrigWidth = width; | 335 m_OutputWidth = m_OrigWidth = width; |
| 336 m_OutputHeight = m_OrigHeight = height; | 336 m_OutputHeight = m_OrigHeight = height; |
| 337 m_nComps = nComps; | 337 m_nComps = nComps; |
| 338 m_bpc = bpc; | 338 m_bpc = bpc; |
| 339 m_bColorTransformed = FALSE; | 339 m_bColorTransformed = FALSE; |
| 340 m_DownScale = 1; | 340 m_DownScale = 1; |
| 341 m_Pitch = (width * nComps * bpc + 31) / 32 * 4; | 341 // Aligning the pitch to 4 bytes requires an integer overflow check. |
| 342 m_dwLineBytes = (width * nComps * bpc + 7) / 8; | 342 FX_SAFE_DWORD pitch = width; |
| 343 pitch *= nComps; |
| 344 pitch *= bpc; |
| 345 pitch += 31; |
| 346 pitch /= 32; |
| 347 pitch *= 4; |
| 348 if (!pitch.IsValid()) { |
| 349 return FALSE; |
| 350 } |
| 351 m_Pitch = pitch.ValueOrDie(); |
| 352 // Overflow should already have been checked before this is called. |
| 353 m_dwLineBytes = (static_cast<FX_DWORD>(width) * nComps * bpc + 7) / 8; |
| 343 m_pScanline = FX_Alloc(uint8_t, m_Pitch); | 354 m_pScanline = FX_Alloc(uint8_t, m_Pitch); |
| 344 return CheckDestSize(); | 355 return CheckDestSize(); |
| 345 } | 356 } |
| 346 FX_BOOL CCodec_RLScanlineDecoder::v_Rewind() { | 357 FX_BOOL CCodec_RLScanlineDecoder::v_Rewind() { |
| 347 FXSYS_memset(m_pScanline, 0, m_Pitch); | 358 FXSYS_memset(m_pScanline, 0, m_Pitch); |
| 348 m_SrcOffset = 0; | 359 m_SrcOffset = 0; |
| 349 m_bEOD = FALSE; | 360 m_bEOD = FALSE; |
| 350 m_Operator = 0; | 361 m_Operator = 0; |
| 351 return TRUE; | 362 return TRUE; |
| 352 } | 363 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 int nComps, | 450 int nComps, |
| 440 int bpc) { | 451 int bpc) { |
| 441 CCodec_RLScanlineDecoder* pRLScanlineDecoder = new CCodec_RLScanlineDecoder; | 452 CCodec_RLScanlineDecoder* pRLScanlineDecoder = new CCodec_RLScanlineDecoder; |
| 442 if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, | 453 if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, |
| 443 bpc)) { | 454 bpc)) { |
| 444 delete pRLScanlineDecoder; | 455 delete pRLScanlineDecoder; |
| 445 return NULL; | 456 return NULL; |
| 446 } | 457 } |
| 447 return pRLScanlineDecoder; | 458 return pRLScanlineDecoder; |
| 448 } | 459 } |
| OLD | NEW |