From 7e33dbeec78dfa051df52619672133da6a799240 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 23 May 2018 23:41:00 +0000 Subject: Remove GetBits32(). Replace it with CFX_BitStream. Change-Id: Ib74657f888b8dec8b6fdad7b49e28d250991c590 Reviewed-on: https://pdfium-review.googlesource.com/32852 Commit-Queue: Lei Zhang Reviewed-by: dsinclair --- core/fxcrt/cfx_bitstream_unittest.cpp | 21 +++++++++++++++++---- core/fxcrt/fx_extension.cpp | 31 ------------------------------- core/fxcrt/fx_extension.h | 2 -- core/fxcrt/fx_extension_unittest.cpp | 23 ----------------------- 4 files changed, 17 insertions(+), 60 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/cfx_bitstream_unittest.cpp b/core/fxcrt/cfx_bitstream_unittest.cpp index 991cd43147..e70455bc67 100644 --- a/core/fxcrt/cfx_bitstream_unittest.cpp +++ b/core/fxcrt/cfx_bitstream_unittest.cpp @@ -3,9 +3,21 @@ // found in the LICENSE file. #include "core/fxcrt/cfx_bitstream.h" -#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, BitStream) { static const uint8_t kData[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; @@ -106,14 +118,15 @@ TEST(fxcrt, BitStream) { EXPECT_EQ(0U, bitstream.BitsRemaining()); } -TEST(fxcrt, BitStreamSameAsGetBit32) { +TEST(fxcrt, BitStreamSameAsReferenceGetBits32) { unsigned char kData[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; CFX_BitStream bitstream(kData); - for (uint32_t nbits = 1; nbits <= 32; ++nbits) { + for (int nbits = 1; nbits <= 32; ++nbits) { for (size_t bitpos = 0; bitpos < sizeof(kData) * 8 - nbits; ++bitpos) { bitstream.Rewind(); bitstream.SkipBits(bitpos); - EXPECT_EQ(bitstream.GetBits(nbits), GetBits32(kData, bitpos, nbits)); + EXPECT_EQ(bitstream.GetBits(nbits), + ReferenceGetBits32(kData, bitpos, nbits)); } } } diff --git a/core/fxcrt/fx_extension.cpp b/core/fxcrt/fx_extension.cpp index e27c78d7bf..c754a85508 100644 --- a/core/fxcrt/fx_extension.cpp +++ b/core/fxcrt/fx_extension.cpp @@ -167,34 +167,3 @@ size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf) { FXSYS_IntToFourHexChars(0xDC00 + unicode % 0x400, buf + 4); return 8; } - -uint32_t GetBits32(pdfium::span pData, int bitpos, int nbits) { - ASSERT(0 < nbits && nbits <= 32); - size_t bytepos = 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( - (pData[bytepos++] >> bitShift & bitMask) << dstShift); - while (dstShift >= 8) { - dstShift -= 8; - result |= pData[bytepos++] << dstShift; - } - if (dstShift > 0) { - bitShift = 8 - dstShift; - bitMask = (1 << dstShift) - 1; - result |= pData[bytepos++] >> bitShift & bitMask; - } - return result; -} diff --git a/core/fxcrt/fx_extension.h b/core/fxcrt/fx_extension.h index f5c0dad53f..cef943fc99 100644 --- a/core/fxcrt/fx_extension.h +++ b/core/fxcrt/fx_extension.h @@ -92,6 +92,4 @@ void FXSYS_IntToFourHexChars(uint16_t c, char* buf); size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf); -uint32_t GetBits32(pdfium::span pData, int bitpos, int nbits); - #endif // CORE_FXCRT_FX_EXTENSION_H_ diff --git a/core/fxcrt/fx_extension_unittest.cpp b/core/fxcrt/fx_extension_unittest.cpp index 155b7014b7..39f26b534b 100644 --- a/core/fxcrt/fx_extension_unittest.cpp +++ b/core/fxcrt/fx_extension_unittest.cpp @@ -5,19 +5,6 @@ #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')); @@ -103,16 +90,6 @@ TEST(fxcrt, FXSYS_ToUTF16BE) { 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)); - } - } -} - TEST(fxcrt, FXSYS_wcstof) { int32_t used_len = 0; EXPECT_FLOAT_EQ(-12.0f, FXSYS_wcstof(L"-12", 3, &used_len)); -- cgit v1.2.3