diff options
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | core/include/fpdfapi/fpdf_parser.h | 18 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 20 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 20 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp | 52 | ||||
-rw-r--r-- | pdfium.gyp | 1 |
6 files changed, 85 insertions, 27 deletions
@@ -1474,6 +1474,7 @@ static_library("xfa") { test("pdfium_unittests") { sources = [ "core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp", + "core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp", "core/src/fxcodec/codec/fx_codec_jpx_unittest.cpp", "core/src/fxcrt/fx_basic_bstring_unittest.cpp", "core/src/fxcrt/fx_basic_memmgr_unittest.cpp", diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h index 32509eb837..d87be61ef9 100644 --- a/core/include/fpdfapi/fpdf_parser.h +++ b/core/include/fpdfapi/fpdf_parser.h @@ -945,6 +945,24 @@ enum PDF_DATAAVAIL_STATUS { PDF_DATAAVAIL_TRAILER_APPEND }; +// Public for testing. +FX_DWORD A85Decode(const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t*& dest_buf, + FX_DWORD& dest_size); +// Public for testing. +FX_DWORD HexDecode(const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t*& dest_buf, + FX_DWORD& dest_size); +// Public for testing. +FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, + const uint8_t* src_buf, + FX_DWORD src_size, + CPDF_Dictionary* pParams, + FX_DWORD estimated_size, + uint8_t*& dest_buf, + FX_DWORD& dest_size); FX_BOOL PDF_DataDecode(const uint8_t* src_buf, FX_DWORD src_size, const CPDF_Dictionary* pDict, diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index 1fa27e3805..187d6152e9 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -278,21 +278,7 @@ ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder( int width, int height, const CPDF_Dictionary* pParams); -FX_DWORD _A85Decode(const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t*& dest_buf, - FX_DWORD& dest_size); -FX_DWORD _HexDecode(const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t*& dest_buf, - FX_DWORD& dest_size); -FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, - const uint8_t* src_buf, - FX_DWORD src_size, - CPDF_Dictionary* pParams, - FX_DWORD estimated_size, - uint8_t*& dest_buf, - FX_DWORD& dest_size); + FX_DWORD PDF_DecodeInlineStream(const uint8_t* src_buf, FX_DWORD limit, int width, @@ -307,10 +293,10 @@ FX_DWORD PDF_DecodeInlineStream(const uint8_t* src_buf, return _DecodeAllScanlines(pDecoder, dest_buf, dest_size); } if (decoder == FX_BSTRC("ASCII85Decode") || decoder == FX_BSTRC("A85")) { - return _A85Decode(src_buf, limit, dest_buf, dest_size); + return A85Decode(src_buf, limit, dest_buf, dest_size); } if (decoder == FX_BSTRC("ASCIIHexDecode") || decoder == FX_BSTRC("AHx")) { - return _HexDecode(src_buf, limit, dest_buf, dest_size); + return HexDecode(src_buf, limit, dest_buf, dest_size); } if (decoder == FX_BSTRC("FlateDecode") || decoder == FX_BSTRC("Fl")) { return FPDFAPI_FlateOrLZWDecode(FALSE, src_buf, limit, pParam, dest_size, diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 255d0ce29c..e68fcb6800 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -42,10 +42,10 @@ const FX_WORD PDFDocEncoding[256] = { 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff}; -FX_DWORD _A85Decode(const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t*& dest_buf, - FX_DWORD& dest_size) { +FX_DWORD A85Decode(const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t*& dest_buf, + FX_DWORD& dest_size) { dest_size = 0; dest_buf = NULL; if (src_size == 0) { @@ -117,10 +117,10 @@ FX_DWORD _A85Decode(const uint8_t* src_buf, } return pos; } -FX_DWORD _HexDecode(const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t*& dest_buf, - FX_DWORD& dest_size) { +FX_DWORD HexDecode(const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t*& dest_buf, + FX_DWORD& dest_size) { FX_DWORD i; for (i = 0; i < src_size; i++) if (src_buf[i] == '>') { @@ -380,10 +380,10 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf, estimated_size, new_buf, new_size); } else if (decoder == FX_BSTRC("ASCII85Decode") || decoder == FX_BSTRC("A85")) { - offset = _A85Decode(last_buf, last_size, new_buf, new_size); + offset = A85Decode(last_buf, last_size, new_buf, new_size); } else if (decoder == FX_BSTRC("ASCIIHexDecode") || decoder == FX_BSTRC("AHx")) { - offset = _HexDecode(last_buf, last_size, new_buf, new_size); + offset = HexDecode(last_buf, last_size, new_buf, new_size); } else if (decoder == FX_BSTRC("RunLengthDecode") || decoder == FX_BSTRC("RL")) { if (bImageAcc && i == DecoderList.GetSize() - 1) { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp new file mode 100644 index 0000000000..f01e21293c --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp @@ -0,0 +1,52 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "../../../include/fpdfapi/fpdf_parser.h" + +TEST(fpdf_parser_decode, HexDecode) { + { + // Empty src string. + uint8_t* dest = nullptr; + FX_DWORD dest_size; + uint8_t src[] = ""; + EXPECT_EQ(0, HexDecode(src, 0, dest, dest_size)); + EXPECT_EQ(0, dest_size); + EXPECT_EQ('\0', dest[0]); + } + + { + // Regular conversion. + uint8_t* dest = nullptr; + FX_DWORD dest_size; + uint8_t src[] = "12Ac>zzz"; + EXPECT_EQ(5, HexDecode(src, 8, dest, dest_size)); + EXPECT_EQ(2, dest_size); + EXPECT_EQ(18, dest[0]); + EXPECT_EQ(172, dest[1]); + } + + { + // Non-multiple length. + uint8_t* dest = nullptr; + FX_DWORD dest_size; + uint8_t src[] = "12A>zzz"; + EXPECT_EQ(4, HexDecode(src, 8, dest, dest_size)); + EXPECT_EQ(2, dest_size); + EXPECT_EQ(18, dest[0]); + EXPECT_EQ(160, dest[1]); + } + + { + // Skips unknown characters. + uint8_t* dest = nullptr; + FX_DWORD dest_size; + uint8_t src[] = "12tk \tAc>zzz"; + EXPECT_EQ(10, HexDecode(src, 13, dest, dest_size)); + EXPECT_EQ(2, dest_size); + EXPECT_EQ(18, dest[0]); + EXPECT_EQ(172, dest[1]); + } +} diff --git a/pdfium.gyp b/pdfium.gyp index 44a7611740..0ca90687d1 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -749,6 +749,7 @@ ], 'sources': [ 'core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp', + 'core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp', 'core/src/fxcodec/codec/fx_codec_jpx_unittest.cpp', 'core/src/fxcrt/fx_basic_bstring_unittest.cpp', 'core/src/fxcrt/fx_basic_memmgr_unittest.cpp', |