From 995a601c50874729ac40e2cc444b8894cf2aea96 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Tue, 20 Jun 2017 17:42:39 -0400 Subject: Cleanup fpdf_parser_decode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL fixes some nits in fpdf_parser_decode, especially avoiding non-const reference parameters. Change-Id: Ibb914850afd924bb398f886ac862f7589519ef7e Reviewed-on: https://pdfium-review.googlesource.com/6750 Commit-Queue: Nicolás Peña Reviewed-by: dsinclair --- core/fxcodec/codec/ccodec_flatemodule.h | 6 ++-- core/fxcodec/codec/fx_codec_flate.cpp | 52 +++++++++++++--------------- core/fxcodec/codec/fx_codec_rle_unittest.cpp | 14 ++++---- 3 files changed, 35 insertions(+), 37 deletions(-) (limited to 'core/fxcodec') diff --git a/core/fxcodec/codec/ccodec_flatemodule.h b/core/fxcodec/codec/ccodec_flatemodule.h index 5178943ca0..ae34103f3b 100644 --- a/core/fxcodec/codec/ccodec_flatemodule.h +++ b/core/fxcodec/codec/ccodec_flatemodule.h @@ -34,12 +34,14 @@ class CCodec_FlateModule { int BitsPerComponent, int Columns, uint32_t estimated_size, - uint8_t*& dest_buf, - uint32_t& dest_size); + uint8_t** dest_buf, + uint32_t* dest_size); + bool Encode(const uint8_t* src_buf, uint32_t src_size, uint8_t** dest_buf, uint32_t* dest_size); + bool PngEncode(const uint8_t* src_buf, uint32_t src_size, uint8_t** dest_buf, diff --git a/core/fxcodec/codec/fx_codec_flate.cpp b/core/fxcodec/codec/fx_codec_flate.cpp index 5af3e99c2e..02be295ce6 100644 --- a/core/fxcodec/codec/fx_codec_flate.cpp +++ b/core/fxcodec/codec/fx_codec_flate.cpp @@ -766,48 +766,44 @@ uint32_t CCodec_FlateModule::FlateOrLZWDecode(bool bLZW, int BitsPerComponent, int Columns, uint32_t estimated_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - dest_buf = nullptr; + uint8_t** dest_buf, + uint32_t* dest_size) { + *dest_buf = nullptr; uint32_t offset = 0; int predictor_type = 0; if (predictor) { - if (predictor >= 10) { + if (predictor >= 10) predictor_type = 2; - } else if (predictor == 2) { + else if (predictor == 2) predictor_type = 1; - } } if (bLZW) { - { - auto decoder = pdfium::MakeUnique(); - dest_size = 0xFFFFFFFF; - offset = src_size; - int err = - decoder->Decode(nullptr, dest_size, src_buf, offset, bEarlyChange); - if (err || dest_size == 0 || dest_size + 1 < dest_size) { - return FX_INVALID_OFFSET; - } - } - { - auto decoder = pdfium::MakeUnique(); - dest_buf = FX_Alloc(uint8_t, dest_size + 1); - dest_buf[dest_size] = '\0'; - decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); - } + auto decoder = pdfium::MakeUnique(); + *dest_size = 0xFFFFFFFF; + offset = src_size; + int err = + decoder->Decode(nullptr, *dest_size, src_buf, offset, bEarlyChange); + if (err || *dest_size == 0 || *dest_size + 1 < *dest_size) + return FX_INVALID_OFFSET; + + decoder = pdfium::MakeUnique(); + *dest_buf = FX_Alloc(uint8_t, *dest_size + 1); + (*dest_buf)[*dest_size] = '\0'; + decoder->Decode(*dest_buf, *dest_size, src_buf, offset, bEarlyChange); } else { - FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, + FlateUncompress(src_buf, src_size, estimated_size, *dest_buf, *dest_size, offset); } - if (predictor_type == 0) { + if (predictor_type == 0) return offset; - } + bool ret = true; if (predictor_type == 2) { - ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); - } else if (predictor_type == 1) { ret = - TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); + PNG_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent, Columns); + } else if (predictor_type == 1) { + ret = TIFF_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent, + Columns); } return ret ? offset : FX_INVALID_OFFSET; } diff --git a/core/fxcodec/codec/fx_codec_rle_unittest.cpp b/core/fxcodec/codec/fx_codec_rle_unittest.cpp index d90a515d45..f9a4b3fcb7 100644 --- a/core/fxcodec/codec/fx_codec_rle_unittest.cpp +++ b/core/fxcodec/codec/fx_codec_rle_unittest.cpp @@ -75,7 +75,7 @@ TEST(fxcodec, RLETestNormalInputs) { pEncoders->RunLengthEncode(src_buf_1, src_size, &dest_buf, &dest_size)); uint8_t* decoded_buf = nullptr; uint32_t decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_1[i], decoded_buf[i]) << " at " << i; @@ -89,7 +89,7 @@ TEST(fxcodec, RLETestNormalInputs) { pEncoders->RunLengthEncode(src_buf_2, src_size, &dest_buf, &dest_size)); decoded_buf = nullptr; decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_2[i], decoded_buf[i]) << " at " << i; @@ -103,7 +103,7 @@ TEST(fxcodec, RLETestNormalInputs) { pEncoders->RunLengthEncode(src_buf_3, src_size, &dest_buf, &dest_size)); decoded_buf = nullptr; decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_3[i], decoded_buf[i]) << " at " << i; @@ -144,7 +144,7 @@ TEST(fxcodec, RLETestFullLengthInputs) { pEncoders->RunLengthEncode(src_buf_1, src_size, &dest_buf, &dest_size)); uint8_t* decoded_buf = nullptr; uint32_t decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_1[i], decoded_buf[i]) << " at " << i; @@ -158,7 +158,7 @@ TEST(fxcodec, RLETestFullLengthInputs) { pEncoders->RunLengthEncode(src_buf_2, src_size, &dest_buf, &dest_size)); decoded_buf = nullptr; decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_2[i], decoded_buf[i]) << " at " << i; @@ -172,7 +172,7 @@ TEST(fxcodec, RLETestFullLengthInputs) { pEncoders->RunLengthEncode(src_buf_3, src_size, &dest_buf, &dest_size)); decoded_buf = nullptr; decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_3[i], decoded_buf[i]) << " at " << i; @@ -186,7 +186,7 @@ TEST(fxcodec, RLETestFullLengthInputs) { pEncoders->RunLengthEncode(src_buf_4, src_size, &dest_buf, &dest_size)); decoded_buf = nullptr; decoded_size = 0; - RunLengthDecode(dest_buf, dest_size, decoded_buf, decoded_size); + RunLengthDecode(dest_buf, dest_size, &decoded_buf, &decoded_size); ASSERT_EQ(src_size, decoded_size); for (uint32_t i = 0; i < src_size; i++) EXPECT_EQ(src_buf_4[i], decoded_buf[i]) << " at " << i; -- cgit v1.2.3