diff options
Diffstat (limited to 'core/fpdfapi')
-rw-r--r-- | core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 63 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_page/cpdf_image.cpp | 14 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 41 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp | 12 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h | 23 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 18 |
6 files changed, 83 insertions, 88 deletions
diff --git a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 0a833a4835..09833d82cc 100644 --- a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -388,8 +388,8 @@ class CPDF_FlateEncoder { CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode); CPDF_FlateEncoder(const uint8_t* pBuffer, uint32_t size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream = FALSE); + bool bFlateEncode, + bool bXRefStream); ~CPDF_FlateEncoder(); void CloneDict(); @@ -439,7 +439,8 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) m_bNewData = TRUE; m_bCloned = TRUE; - ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); + // TODO(thestig): Move to Init() and check return value. + ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize); m_pDict = ToDictionary(pStream->GetDict()->Clone()); m_pDict->SetAtInteger("Length", m_dwSize); m_pDict->SetAtName("Filter", "FlateDecode"); @@ -448,8 +449,8 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, uint32_t size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream) + bool bFlateEncode, + bool bXRefStream) : m_pData(nullptr), m_dwSize(0), m_pDict(nullptr), @@ -461,10 +462,11 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, return; } m_bNewData = TRUE; + // TODO(thestig): Move to Init() and check return value. if (bXRefStream) - ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); + ::PngEncode(pBuffer, size, &m_pData, &m_dwSize); else - ::FlateEncode(pBuffer, size, m_pData, m_dwSize); + ::FlateEncode(pBuffer, size, &m_pData, &m_dwSize); } CPDF_FlateEncoder::~CPDF_FlateEncoder() { @@ -577,7 +579,7 @@ FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { tempBuffer << m_Buffer; CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(), - TRUE); + true, false); CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler, m_dwObjNum, encoder.m_pData, encoder.m_dwSize); if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { @@ -785,26 +787,24 @@ FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator, } offset += offset_len + 6; } - FX_BOOL bPredictor = TRUE; CPDF_FlateEncoder encoder(m_Buffer.GetBuffer(), m_Buffer.GetLength(), TRUE, - bPredictor); - if (pFile->AppendString("/Filter /FlateDecode") < 0) { + TRUE); + if (pFile->AppendString("/Filter /FlateDecode") < 0) return FALSE; - } + offset += 20; - if (bPredictor) { - if ((len = pFile->AppendString("/DecodeParms<</Columns 7/Predictor 12>>")) < - 0) { - return FALSE; - } - offset += len; - } - if (pFile->AppendString("/Length ") < 0) { + if ((len = pFile->AppendString("/DecodeParms<</Columns 7/Predictor 12>>")) < + 0) { return FALSE; } - if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) { + + offset += len; + if (pFile->AppendString("/Length ") < 0) return FALSE; - } + + if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) + return FALSE; + offset += len + 8; if (bEOF) { if ((len = PDF_CreatorWriteTrailer(pCreator->m_pDocument, pFile, @@ -899,7 +899,7 @@ CPDF_Creator::~CPDF_Creator() { ResetStandardSecurity(); if (m_bEncryptCloned && m_pEncryptDict) { m_pEncryptDict->Release(); - m_pEncryptDict = NULL; + m_pEncryptDict = nullptr; } Clear(); } @@ -1441,7 +1441,7 @@ int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { m_dwFlags &= ~FPDFCREATE_INCREMENTAL; } CPDF_Dictionary* pDict = m_pDocument->GetRoot(); - m_pMetadata = pDict ? pDict->GetDirectObjectBy("Metadata") : NULL; + m_pMetadata = pDict ? pDict->GetDirectObjectBy("Metadata") : nullptr; if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { m_pXRefStream.reset(new CPDF_XRefStream); m_pXRefStream->Start(); @@ -1556,7 +1556,7 @@ int32_t CPDF_Creator::WriteDoc_Stage2(IFX_Pause* pPause) { m_iStage = 27; } if (m_iStage == 27) { - if (NULL != m_pEncryptDict && 0 == m_pEncryptDict->GetObjNum()) { + if (m_pEncryptDict && !m_pEncryptDict->GetObjNum()) { m_dwLastObjNum += 1; FX_FILESIZE saveOffset = m_Offset; if (WriteIndirectObj(m_dwLastObjNum, m_pEncryptDict) < 0) { @@ -1893,13 +1893,14 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { m_File.Flush(); return m_iStage = 100; } + void CPDF_Creator::Clear() { m_pXRefStream.reset(); m_File.Clear(); m_NewObjNumArray.RemoveAll(); if (m_pIDArray) { m_pIDArray->Release(); - m_pIDArray = NULL; + m_pIDArray = nullptr; } } @@ -1916,17 +1917,17 @@ bool CPDF_Creator::Create(uint32_t flags) { m_ObjectOffset.Clear(); m_NewObjNumArray.RemoveAll(); InitID(); - if (flags & FPDFCREATE_PROGRESSIVE) { + if (flags & FPDFCREATE_PROGRESSIVE) return true; - } - return Continue(NULL) > -1; + return Continue(nullptr) > -1; } + void CPDF_Creator::InitID(FX_BOOL bDefault) { - CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : NULL; + CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : nullptr; FX_BOOL bNewId = !m_pIDArray; if (!m_pIDArray) { m_pIDArray = new CPDF_Array; - CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetObjectAt(0) : NULL; + CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetObjectAt(0) : nullptr; if (pID1) { m_pIDArray->Add(pID1->Clone()); } else { diff --git a/core/fpdfapi/fpdf_page/cpdf_image.cpp b/core/fpdfapi/fpdf_page/cpdf_image.cpp index 84f6853b58..d7e50d8291 100644 --- a/core/fpdfapi/fpdf_page/cpdf_image.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_image.cpp @@ -79,17 +79,18 @@ CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { int32_t height; int32_t num_comps; int32_t bits; - FX_BOOL color_trans; + bool color_trans; if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( - pData, size, width, height, num_comps, bits, color_trans)) { - return NULL; + pData, size, &width, &height, &num_comps, &bits, &color_trans)) { + return nullptr; } + CPDF_Dictionary* pDict = new CPDF_Dictionary; pDict->SetAtName("Type", "XObject"); pDict->SetAtName("Subtype", "Image"); pDict->SetAtInteger("Width", width); pDict->SetAtInteger("Height", height); - const FX_CHAR* csname = NULL; + const FX_CHAR* csname = nullptr; if (num_comps == 1) { csname = "DeviceGray"; } else if (num_comps == 3) { @@ -114,9 +115,8 @@ CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { m_bIsMask = FALSE; m_Width = width; m_Height = height; - if (!m_pStream) { - m_pStream = new CPDF_Stream(NULL, 0, NULL); - } + if (!m_pStream) + m_pStream = new CPDF_Stream(nullptr, 0, nullptr); return pDict; } diff --git a/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 7c489a35dc..40ba362ad6 100644 --- a/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -532,45 +532,40 @@ CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) { result.AppendChar('('); for (int i = 0; i < srclen; i++) { uint8_t ch = src[i]; - if (ch == ')' || ch == '\\' || ch == '(') { - result.AppendChar('\\'); - } else if (ch == 0x0a) { + if (ch == 0x0a) { result << "\\n"; continue; - } else if (ch == 0x0d) { + } + if (ch == 0x0d) { result << "\\r"; continue; } + if (ch == ')' || ch == '\\' || ch == '(') + result.AppendChar('\\'); result.AppendChar(ch); } result.AppendChar(')'); return result.MakeString(); } -void FlateEncode(const uint8_t* src_buf, +bool FlateEncode(const uint8_t* src_buf, uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { + uint8_t** dest_buf, + uint32_t* dest_size) { CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); - if (pEncoders) { - pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size); - } + return pEncoders && + pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, + dest_size); } -void FlateEncode(const uint8_t* src_buf, - uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size) { +bool PngEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size) { CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); - if (pEncoders) { - pEncoders->GetFlateModule()->Encode(src_buf, src_size, predictor, Colors, - BitsPerComponent, Columns, dest_buf, - dest_size); - } + return pEncoders && + pEncoders->GetFlateModule()->PngEncode(src_buf, src_size, dest_buf, + dest_size); } uint32_t FlateDecode(const uint8_t* src_buf, diff --git a/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp b/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp index 308c9f11c9..e9ca958411 100644 --- a/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp +++ b/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp @@ -38,14 +38,14 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) { const pdfium::StrFuncTestData& data = flate_encode_cases[i]; - unsigned char* result; - unsigned int result_size; - FlateEncode(data.input, data.input_size, result, result_size); - ASSERT_TRUE(result); + unsigned char* buf = nullptr; + unsigned int buf_size; + EXPECT_TRUE(FlateEncode(data.input, data.input_size, &buf, &buf_size)); + ASSERT_TRUE(buf); EXPECT_EQ(std::string((const char*)data.expected, data.expected_size), - std::string((const char*)result, result_size)) + std::string((const char*)buf, buf_size)) << " for case " << i; - FX_Free(result); + FX_Free(buf); } } diff --git a/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h b/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h index 1f1095b396..5ae4011c76 100644 --- a/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h +++ b/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h @@ -24,18 +24,19 @@ CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr); CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, int len = -1); CFX_ByteString PDF_EncodeText(const CFX_WideString& str); -void FlateEncode(const uint8_t* src_buf, +bool FlateEncode(const uint8_t* src_buf, uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); -void FlateEncode(const uint8_t* src_buf, - uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size); + uint8_t** dest_buf, + uint32_t* dest_size); + +// This used to have more parameters like the predictor and bpc, but there was +// only one caller, so the interface has been simplified, the values are hard +// coded, and dead code has been removed. +bool PngEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); + uint32_t FlateDecode(const uint8_t* src_buf, uint32_t src_size, uint8_t*& dest_buf, diff --git a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 4071a115df..2b829d0c56 100644 --- a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -561,19 +561,20 @@ int CPDF_DIBSource::CreateDecoder() { src_data, src_size, m_Width, m_Height, m_nComponents, pParams ? pParams->GetIntegerBy("ColorTransform", 1) : 1)); if (!m_pDecoder) { - FX_BOOL bTransform = FALSE; + bool bTransform = false; int comps; int bpc; CCodec_JpegModule* pJpegModule = CPDF_ModuleMgr::Get()->GetJpegModule(); - if (pJpegModule->LoadInfo(src_data, src_size, m_Width, m_Height, comps, - bpc, bTransform)) { + if (pJpegModule->LoadInfo(src_data, src_size, &m_Width, &m_Height, &comps, + &bpc, &bTransform)) { if (m_nComponents != static_cast<uint32_t>(comps)) { FX_Free(m_pCompData); m_pCompData = nullptr; m_nComponents = static_cast<uint32_t>(comps); if (m_pColorSpace && - m_pColorSpace->CountComponents() != m_nComponents) + m_pColorSpace->CountComponents() != m_nComponents) { return 0; + } if (m_Family == PDFCS_LAB && m_nComponents != 3) return 0; m_pCompData = GetDecodeAndMaskArray(m_bDefaultDecode, m_bColorKey); @@ -613,17 +614,14 @@ int CPDF_DIBSource::CreateDecoder() { FX_SAFE_UINT32 requested_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); - if (!requested_pitch.IsValid()) { + if (!requested_pitch.IsValid()) return 0; - } FX_SAFE_UINT32 provided_pitch = CalculatePitch8( m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); - if (!provided_pitch.IsValid()) { + if (!provided_pitch.IsValid()) return 0; - } - if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { + if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) return 0; - } return 1; } |