Simplify UTF16LE_Encode and add unittest.
authorBo Xu <bo_xu@foxitsoftware.com>
Wed, 21 Jan 2015 20:17:23 +0000 (12:17 -0800)
committerBo Xu <bo_xu@foxitsoftware.com>
Wed, 21 Jan 2015 20:17:23 +0000 (12:17 -0800)
Previously, UTF16LE_Encode take an optional flag to indicate
if the returned byte string has trailing zeros. In fact, no where
needs the flag to be false. So just get rid of it so callers won't
misuse.

The bug is found by https://codereview.chromium.org/837723009

R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/860973002

BUILD.gn
core/include/fxcrt/fx_string.h
core/src/fxcrt/fx_basic_wstring.cpp
core/src/fxcrt/fx_basic_wstring_unittest.cpp [new file with mode: 0644]
fpdfsdk/src/fpdfdoc.cpp
fpdfsdk/src/fpdfview.cpp
pdfium.gyp

index d9413fd..7581d73 100644 (file)
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -797,6 +797,7 @@ static_library("formfiller") {
 test("pdfium_unittests") {
   sources = [
     "core/src/fxcrt/fx_basic_bstring_unittest.cpp",
+    "core/src/fxcrt/fx_basic_wstring_unittest.cpp",
     "testing/fx_string_testhelpers.cpp",
     "testing/fx_string_testhelpers.h",
   ]
index eae1c15..33634f8 100644 (file)
@@ -774,7 +774,7 @@ public:
 
     CFX_ByteString                     UTF8Encode() const;
 
-    CFX_ByteString                     UTF16LE_Encode(FX_BOOL bTerminate = TRUE) const;
+    CFX_ByteString                     UTF16LE_Encode() const;
 
     void                                       ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL);
 protected:
index 0827fb6..0945d2f 100644 (file)
@@ -323,25 +323,21 @@ CFX_ByteString CFX_WideString::UTF8Encode() const
 {
     return FX_UTF8Encode(*this);
 }
-CFX_ByteString CFX_WideString::UTF16LE_Encode(FX_BOOL bTerminate) const
+CFX_ByteString CFX_WideString::UTF16LE_Encode() const
 {
     if (m_pData == NULL) {
-        return bTerminate ? CFX_ByteString(FX_BSTRC("\0\0")) : CFX_ByteString();
+        return CFX_ByteString(FX_BSTRC("\0\0"));
     }
     int len = m_pData->m_nDataLength;
     CFX_ByteString result;
-    FX_LPSTR buffer = result.GetBuffer(len * 2 + (bTerminate ? 2 : 0));
+    FX_LPSTR buffer = result.GetBuffer(len * 2 + 2);
     for (int i = 0; i < len; i ++) {
         buffer[i * 2] = m_pData->m_String[i] & 0xff;
         buffer[i * 2 + 1] = m_pData->m_String[i] >> 8;
     }
-    if (bTerminate) {
-        buffer[len * 2] = 0;
-        buffer[len * 2 + 1] = 0;
-        result.ReleaseBuffer(len * 2 + 2);
-    } else {
-        result.ReleaseBuffer(len * 2);
-    }
+    buffer[len * 2] = 0;
+    buffer[len * 2 + 1] = 0;
+    result.ReleaseBuffer(len * 2 + 2);
     return result;
 }
 void CFX_WideString::ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap)
diff --git a/core/src/fxcrt/fx_basic_wstring_unittest.cpp b/core/src/fxcrt/fx_basic_wstring_unittest.cpp
new file mode 100644 (file)
index 0000000..c6e5c2f
--- /dev/null
@@ -0,0 +1,29 @@
+\feff// Copyright 2014 PDFium Authors. All rights reserved.\r
+// Use of this source code is governed by a BSD-style license that can be\r
+// found in the LICENSE file.\r
+\r
+#include "testing/gtest/include/gtest/gtest.h"\r
+#include "../../../testing/fx_string_testhelpers.h"\r
+#include "../../include/fxcrt/fx_basic.h"\r
+\r
+#define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str))\r
+\r
+TEST(fxcrt, WideStringUTF16LE_Encode) {\r
+  struct UTF16LEEncodeCase {\r
+    CFX_WideString ws;\r
+    CFX_ByteString bs;\r
+  } utf16le_encode_cases[] = {\r
+    { L"", ByteStringLiteral("\0\0") },\r
+    { L"abc", ByteStringLiteral("a\0b\0c\0\0\0") },\r
+    { L"abcdef", ByteStringLiteral("a\0b\0c\0d\0e\0f\0\0\0") },\r
+    { L"abc\0def", ByteStringLiteral("a\0b\0c\0\0\0") },\r
+    { L"\xaabb\xccdd", ByteStringLiteral("\xbb\xaa\xdd\xcc\0\0") },\r
+    { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },\r
+  };\r
+\r
+  for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {\r
+    EXPECT_EQ(utf16le_encode_cases[i].bs,\r
+        utf16le_encode_cases[i].ws.UTF16LE_Encode())\r
+        << " for case number " << i;\r
+  }\r
+}\r
index 41f5c8a..e1ed3bc 100644 (file)
@@ -53,14 +53,12 @@ DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void*
         return 0;
     CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
     CFX_WideString title = bookmark.GetTitle();
-    CFX_ByteString encodedTitle = title.UTF16LE_Encode(FALSE);
+    CFX_ByteString encodedTitle = title.UTF16LE_Encode();
     unsigned long len = encodedTitle.GetLength();
-    if (buffer && buflen >= len + 2) {
+    if (buffer && buflen >= len) {
         FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
-        ((FX_BYTE*)buffer)[len] = 0;
-        ((FX_BYTE*)buffer)[len + 1] = 0;
     }
-    return len + 2;
+    return len;
 }
 
 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title)
@@ -292,11 +290,8 @@ DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTR
        // Use UTF-16LE encoding
        CFX_ByteString encodedText = text.UTF16LE_Encode();
        unsigned long len = encodedText.GetLength();
-       if (buffer && buflen >= len + 2) {
+       if (buffer && buflen >= len) {
                FXSYS_memcpy(buffer, encodedText.c_str(), len);
-               // use double zero as trailer
-               ((FX_BYTE*)buffer)[len] = 0;
-               ((FX_BYTE*)buffer)[len + 1] = 0;
        }
-       return len+2;
+       return len;
 }
index 43feaf4..87ef3ee 100644 (file)
@@ -855,11 +855,9 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index,
     CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
     unsigned int len = utf16Name.GetLength();
     if (!buffer) {
-        buflen = len + 2;
-    } else if (buflen >= len + 2) {
+        buflen = len;
+    } else if (buflen >= len) {
         memcpy(buffer, utf16Name.c_str(), len);
-        ((FX_BYTE*)buffer)[len] = 0;
-        ((FX_BYTE*)buffer)[len + 1] = 0;
     } else {
         len = -1;
     }
index 9039190..b454573 100644 (file)
         'testing/fx_string_testhelpers.h',
         'testing/fx_string_testhelpers.cpp',
         'core/src/fxcrt/fx_basic_bstring_unittest.cpp',
+        'core/src/fxcrt/fx_basic_wstring_unittest.cpp',
       ],
     },
     {