summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser
diff options
context:
space:
mode:
Diffstat (limited to 'core/fpdfapi/parser')
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode.cpp2
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp33
2 files changed, 34 insertions, 1 deletions
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;
+ }
+ }
+}