diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2015-11-02 15:58:55 -0500 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2015-11-02 15:58:55 -0500 |
commit | e948a4ee08b78e27da37bc12244fa1b66d927dc7 (patch) | |
tree | 9d66e81e97358328bb16effbec800eca04027af7 /core | |
parent | e65be3a55628294d0f45da456d856b4539f0cb15 (diff) | |
download | pdfium-e948a4ee08b78e27da37bc12244fa1b66d927dc7.tar.xz |
Add a test for the HexDecode method.
I moved the declaration to be public, instead of having it implicit in one
file and defined in a seperate cpp file.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1410873009 .
Diffstat (limited to 'core')
-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 |
4 files changed, 83 insertions, 27 deletions
diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h index b1334e4c7a..6fec26d687 100644 --- a/core/include/fpdfapi/fpdf_parser.h +++ b/core/include/fpdfapi/fpdf_parser.h @@ -944,6 +944,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]); + } +} |