M57: 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 <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
(cherry picked from commit 37e2bd1acf843db4eef891d994390520b8fcf3fa)

Change-Id: I295812cd4af287d0855a3d4626da49807ade6b50
Reviewed-on: https://pdfium-review.googlesource.com/2886
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 884b5c5..480e2c1 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -500,7 +500,7 @@
   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 83860f9..30d30a4 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp
@@ -76,3 +76,36 @@
     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;
+    }
+  }
+}