From 2a1c8ac53f249bfe2789ea95170f11d308992449 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 31 Aug 2017 16:03:17 -0400 Subject: Move fx_extension implementation to cpp file This CL moves the GetBits32 implemenation into fx_extension.cpp. It also moves some of the fx_basic unittests to the correct unittest files. Change-Id: I2cf8657c228375508db0f02baa628d62a3b2ab25 Reviewed-on: https://pdfium-review.googlesource.com/12673 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- core/fxcrt/fx_basic_util.cpp | 40 ------------------ core/fxcrt/fx_basic_util_unittest.cpp | 76 ----------------------------------- core/fxcrt/fx_extension.cpp | 32 +++++++++++++++ core/fxcrt/fx_extension_unittest.cpp | 23 +++++++++++ core/fxcrt/fx_string_unittest.cpp | 53 ++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 116 deletions(-) delete mode 100644 core/fxcrt/fx_basic_util.cpp delete mode 100644 core/fxcrt/fx_basic_util_unittest.cpp create mode 100644 core/fxcrt/fx_string_unittest.cpp (limited to 'core/fxcrt') diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp deleted file mode 100644 index 704fd18418..0000000000 --- a/core/fxcrt/fx_basic_util.cpp +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2014 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. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include - -#include "core/fxcrt/fx_system.h" - -uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits) { - ASSERT(0 < nbits && nbits <= 32); - const uint8_t* dataPtr = &pData[bitpos / 8]; - int bitShift; - int bitMask; - int dstShift; - int bitCount = bitpos & 0x07; - if (nbits < 8 && nbits + bitCount <= 8) { - bitShift = 8 - nbits - bitCount; - bitMask = (1 << nbits) - 1; - dstShift = 0; - } else { - bitShift = 0; - int bitOffset = 8 - bitCount; - bitMask = (1 << std::min(bitOffset, nbits)) - 1; - dstShift = nbits - bitOffset; - } - uint32_t result = - static_cast((*dataPtr++ >> bitShift & bitMask) << dstShift); - while (dstShift >= 8) { - dstShift -= 8; - result |= *dataPtr++ << dstShift; - } - if (dstShift > 0) { - bitShift = 8 - dstShift; - bitMask = (1 << dstShift) - 1; - result |= *dataPtr++ >> bitShift & bitMask; - } - return result; -} diff --git a/core/fxcrt/fx_basic_util_unittest.cpp b/core/fxcrt/fx_basic_util_unittest.cpp deleted file mode 100644 index 9a130bece4..0000000000 --- a/core/fxcrt/fx_basic_util_unittest.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2016 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 - -#include "core/fxcrt/fx_extension.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) { - int result = 0; - for (int i = 0; i < nbits; i++) { - if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) - result |= 1 << (nbits - i - 1); - } - return result; -} - -} // namespace - -TEST(fxge, GetBits32) { - unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; - for (int nbits = 1; nbits <= 32; ++nbits) { - for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) { - EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits), - GetBits32(data, bitpos, nbits)); - } - } -} - -TEST(fxcrt, FX_atonum) { - int i; - EXPECT_TRUE(FX_atonum("10", &i)); - EXPECT_EQ(10, i); - - EXPECT_TRUE(FX_atonum("-10", &i)); - EXPECT_EQ(-10, i); - - EXPECT_TRUE(FX_atonum("+10", &i)); - EXPECT_EQ(10, i); - - EXPECT_TRUE(FX_atonum("-2147483648", &i)); - EXPECT_EQ(std::numeric_limits::min(), i); - - EXPECT_TRUE(FX_atonum("2147483647", &i)); - EXPECT_EQ(2147483647, i); - - // Value overflows. - EXPECT_TRUE(FX_atonum("-2147483649", &i)); - EXPECT_EQ(0, i); - - // Value overflows. - EXPECT_TRUE(FX_atonum("+2147483648", &i)); - EXPECT_EQ(0, i); - - // Value overflows. - EXPECT_TRUE(FX_atonum("4223423494965252", &i)); - EXPECT_EQ(0, i); - - // No explicit sign will allow the number to go negative. This is for things - // like the encryption Permissions flag (Table 3.20 PDF 1.7 spec) - EXPECT_TRUE(FX_atonum("4294965252", &i)); - EXPECT_EQ(-2044, i); - - EXPECT_TRUE(FX_atonum("-4294965252", &i)); - EXPECT_EQ(0, i); - - EXPECT_TRUE(FX_atonum("+4294965252", &i)); - EXPECT_EQ(0, i); - - float f; - EXPECT_FALSE(FX_atonum("3.24", &f)); - EXPECT_FLOAT_EQ(3.24f, f); -} diff --git a/core/fxcrt/fx_extension.cpp b/core/fxcrt/fx_extension.cpp index 10b568ec9e..2d485490ca 100644 --- a/core/fxcrt/fx_extension.cpp +++ b/core/fxcrt/fx_extension.cpp @@ -6,6 +6,7 @@ #include "core/fxcrt/fx_extension.h" +#include #include #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ @@ -271,3 +272,34 @@ CFX_ByteString FX_GUID_ToString(const FX_GUID* pGUID, bool bSeparator) { return bsStr; } #endif // PDF_ENABLE_XFA + +uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits) { + ASSERT(0 < nbits && nbits <= 32); + const uint8_t* dataPtr = &pData[bitpos / 8]; + int bitShift; + int bitMask; + int dstShift; + int bitCount = bitpos & 0x07; + if (nbits < 8 && nbits + bitCount <= 8) { + bitShift = 8 - nbits - bitCount; + bitMask = (1 << nbits) - 1; + dstShift = 0; + } else { + bitShift = 0; + int bitOffset = 8 - bitCount; + bitMask = (1 << std::min(bitOffset, nbits)) - 1; + dstShift = nbits - bitOffset; + } + uint32_t result = + static_cast((*dataPtr++ >> bitShift & bitMask) << dstShift); + while (dstShift >= 8) { + dstShift -= 8; + result |= *dataPtr++ << dstShift; + } + if (dstShift > 0) { + bitShift = 8 - dstShift; + bitMask = (1 << dstShift) - 1; + result |= *dataPtr++ >> bitShift & bitMask; + } + return result; +} diff --git a/core/fxcrt/fx_extension_unittest.cpp b/core/fxcrt/fx_extension_unittest.cpp index 38b66ba2d2..0500a14b6f 100644 --- a/core/fxcrt/fx_extension_unittest.cpp +++ b/core/fxcrt/fx_extension_unittest.cpp @@ -5,6 +5,19 @@ #include "core/fxcrt/fx_extension.h" #include "testing/gtest/include/gtest/gtest.h" +namespace { + +uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) { + int result = 0; + for (int i = 0; i < nbits; i++) { + if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) + result |= 1 << (nbits - i - 1); + } + return result; +} + +} // namespace + TEST(fxcrt, FXSYS_HexCharToInt) { EXPECT_EQ(10, FXSYS_HexCharToInt('a')); EXPECT_EQ(10, FXSYS_HexCharToInt('A')); @@ -89,3 +102,13 @@ TEST(fxcrt, FXSYS_ToUTF16BE) { EXPECT_EQ(8U, FXSYS_ToUTF16BE(0x2003E, buf)); EXPECT_STREQ("D840DC3E", buf); } + +TEST(fxcrt, GetBits32) { + unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; + for (int nbits = 1; nbits <= 32; ++nbits) { + for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) { + EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits), + GetBits32(data, bitpos, nbits)); + } + } +} diff --git a/core/fxcrt/fx_string_unittest.cpp b/core/fxcrt/fx_string_unittest.cpp new file mode 100644 index 0000000000..b311165638 --- /dev/null +++ b/core/fxcrt/fx_string_unittest.cpp @@ -0,0 +1,53 @@ +// Copyright 2016 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 + +#include "core/fxcrt/fx_string.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(fxstring, FX_atonum) { + int i; + EXPECT_TRUE(FX_atonum("10", &i)); + EXPECT_EQ(10, i); + + EXPECT_TRUE(FX_atonum("-10", &i)); + EXPECT_EQ(-10, i); + + EXPECT_TRUE(FX_atonum("+10", &i)); + EXPECT_EQ(10, i); + + EXPECT_TRUE(FX_atonum("-2147483648", &i)); + EXPECT_EQ(std::numeric_limits::min(), i); + + EXPECT_TRUE(FX_atonum("2147483647", &i)); + EXPECT_EQ(2147483647, i); + + // Value overflows. + EXPECT_TRUE(FX_atonum("-2147483649", &i)); + EXPECT_EQ(0, i); + + // Value overflows. + EXPECT_TRUE(FX_atonum("+2147483648", &i)); + EXPECT_EQ(0, i); + + // Value overflows. + EXPECT_TRUE(FX_atonum("4223423494965252", &i)); + EXPECT_EQ(0, i); + + // No explicit sign will allow the number to go negative. This is for things + // like the encryption Permissions flag (Table 3.20 PDF 1.7 spec) + EXPECT_TRUE(FX_atonum("4294965252", &i)); + EXPECT_EQ(-2044, i); + + EXPECT_TRUE(FX_atonum("-4294965252", &i)); + EXPECT_EQ(0, i); + + EXPECT_TRUE(FX_atonum("+4294965252", &i)); + EXPECT_EQ(0, i); + + float f; + EXPECT_FALSE(FX_atonum("3.24", &f)); + EXPECT_FLOAT_EQ(3.24f, f); +} -- cgit v1.2.3