From 37e2bd1acf843db4eef891d994390520b8fcf3fa Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 23 Feb 2017 23:43:25 -0800 Subject: Fix a wrong variable usage in PDF_EncodeText(). BUG=chromium:694147 Change-Id: I388cb1d117318edb0339f5c7ee1d2b072f0fb741 Reviewed-on: https://pdfium-review.googlesource.com/2832 Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- core/fpdfapi/parser/fpdf_parser_decode.cpp | 2 +- .../fpdfapi/parser/fpdf_parser_decode_unittest.cpp | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 884b5c50d1..480e2c111f 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -500,7 +500,7 @@ CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, int len) { dest_buf2[1] = 0xff; dest_buf2 += 2; for (int j = 0; j < len; j++) { - *dest_buf2++ = pString[i] >> 8; + *dest_buf2++ = pString[j] >> 8; *dest_buf2++ = (uint8_t)pString[j]; } result.ReleaseBuffer(encLen); diff --git a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp index 83860f9146..30d30a433d 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp @@ -76,3 +76,36 @@ TEST(fpdf_parser_decode, HexDecode) { FX_Free(result); } } + +TEST(fpdf_parser_decode, EncodeText) { + struct EncodeTestData { + const FX_WCHAR* input; + const FX_CHAR* expected_output; + FX_STRSIZE expected_length; + } test_data[] = { + // Empty src string. + {L"", "", 0}, + // ASCII text. + {L"the quick\tfox", "the quick\tfox", 13}, + // Unicode text. + {L"\x0330\x0331", "\xFE\xFF\x03\x30\x03\x31", 6}, + // More Unicode text. + {L"\x7F51\x9875\x0020\x56FE\x7247\x0020" + L"\x8D44\x8BAF\x66F4\x591A\x0020\x00BB", + "\xFE\xFF\x7F\x51\x98\x75\x00\x20\x56\xFE\x72\x47\x00" + "\x20\x8D\x44\x8B\xAF\x66\xF4\x59\x1A\x00\x20\x00\xBB", + 26}, + }; + + for (size_t i = 0; i < FX_ArraySize(test_data); ++i) { + const auto& test_case = test_data[i]; + CFX_ByteString output = PDF_EncodeText(test_case.input); + ASSERT_EQ(test_case.expected_length, output.GetLength()) << "for case " + << i; + const FX_CHAR* str_ptr = output.c_str(); + for (FX_STRSIZE j = 0; j < test_case.expected_length; ++j) { + EXPECT_EQ(test_case.expected_output[j], str_ptr[j]) << "for case " << i + << " char " << j; + } + } +} -- cgit v1.2.3