1 // Copyright 2018 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxcrt/cfx_bitstream.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace {
9
ReferenceGetBits32(const uint8_t * pData,int bitpos,int nbits)10 uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) {
11 int result = 0;
12 for (int i = 0; i < nbits; i++) {
13 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8)))
14 result |= 1 << (nbits - i - 1);
15 }
16 return result;
17 }
18
19 } // namespace
20
TEST(fxcrt,BitStream)21 TEST(fxcrt, BitStream) {
22 static const uint8_t kData[] = {0x00, 0x11, 0x22, 0x33,
23 0x44, 0x55, 0x66, 0x77};
24 CFX_BitStream bitstream(kData);
25
26 // Initial state.
27 EXPECT_FALSE(bitstream.IsEOF());
28 EXPECT_EQ(0U, bitstream.GetPos());
29 EXPECT_EQ(64U, bitstream.BitsRemaining());
30
31 // Read, read, read!
32 EXPECT_EQ(0x00U, bitstream.GetBits(8));
33 EXPECT_EQ(8U, bitstream.GetPos());
34 EXPECT_EQ(56U, bitstream.BitsRemaining());
35
36 EXPECT_EQ(0x00U, bitstream.GetBits(1));
37 EXPECT_EQ(9U, bitstream.GetPos());
38 EXPECT_EQ(55U, bitstream.BitsRemaining());
39
40 EXPECT_EQ(0x00U, bitstream.GetBits(2));
41 EXPECT_EQ(11U, bitstream.GetPos());
42 EXPECT_EQ(53U, bitstream.BitsRemaining());
43
44 EXPECT_EQ(0x04U, bitstream.GetBits(3));
45 EXPECT_EQ(14U, bitstream.GetPos());
46 EXPECT_EQ(50U, bitstream.BitsRemaining());
47
48 EXPECT_EQ(0x04U, bitstream.GetBits(4));
49 EXPECT_EQ(18U, bitstream.GetPos());
50 EXPECT_EQ(46U, bitstream.BitsRemaining());
51
52 EXPECT_EQ(0x44U, bitstream.GetBits(7));
53 EXPECT_EQ(25U, bitstream.GetPos());
54 EXPECT_EQ(39U, bitstream.BitsRemaining());
55
56 EXPECT_EQ(0xCDU, bitstream.GetBits(9));
57 EXPECT_EQ(34U, bitstream.GetPos());
58 EXPECT_EQ(30U, bitstream.BitsRemaining());
59
60 EXPECT_EQ(0x08AAU, bitstream.GetBits(15));
61 EXPECT_EQ(49U, bitstream.GetPos());
62 EXPECT_EQ(15U, bitstream.BitsRemaining());
63
64 // Cannot advance past the end.
65 EXPECT_EQ(0x00U, bitstream.GetBits(16));
66 EXPECT_EQ(49U, bitstream.GetPos());
67 EXPECT_EQ(15U, bitstream.BitsRemaining());
68
69 // Make sure SkipBits() works.
70 bitstream.SkipBits(14);
71 EXPECT_EQ(63U, bitstream.GetPos());
72 EXPECT_EQ(1U, bitstream.BitsRemaining());
73 bitstream.SkipBits(2);
74 EXPECT_EQ(65U, bitstream.GetPos());
75 EXPECT_EQ(0U, bitstream.BitsRemaining());
76 EXPECT_TRUE(bitstream.IsEOF());
77
78 // Make sure Rewind() works.
79 bitstream.Rewind();
80 EXPECT_FALSE(bitstream.IsEOF());
81 EXPECT_EQ(0U, bitstream.GetPos());
82 EXPECT_EQ(64U, bitstream.BitsRemaining());
83
84 // Read some more.
85 bitstream.SkipBits(5);
86 EXPECT_EQ(5U, bitstream.GetPos());
87 EXPECT_EQ(59U, bitstream.BitsRemaining());
88
89 EXPECT_EQ(0x0448U, bitstream.GetBits(17));
90 EXPECT_EQ(22U, bitstream.GetPos());
91 EXPECT_EQ(42U, bitstream.BitsRemaining());
92
93 // Make sure ByteAlign() works.
94 bitstream.ByteAlign();
95 EXPECT_EQ(24U, bitstream.GetPos());
96 EXPECT_EQ(40U, bitstream.BitsRemaining());
97
98 EXPECT_EQ(0x19A22AB3U, bitstream.GetBits(31));
99 EXPECT_EQ(55U, bitstream.GetPos());
100 EXPECT_EQ(9U, bitstream.BitsRemaining());
101
102 // Do some bigger reads.
103 bitstream.Rewind();
104 EXPECT_EQ(0x112233U, bitstream.GetBits(32));
105 EXPECT_EQ(32U, bitstream.GetPos());
106 EXPECT_EQ(32U, bitstream.BitsRemaining());
107
108 bitstream.Rewind();
109 bitstream.SkipBits(31);
110 EXPECT_EQ(0xA22AB33BU, bitstream.GetBits(32));
111 EXPECT_EQ(63U, bitstream.GetPos());
112 EXPECT_EQ(1U, bitstream.BitsRemaining());
113
114 // Skip past the end.
115 bitstream.SkipBits(1000);
116 EXPECT_TRUE(bitstream.IsEOF());
117 EXPECT_EQ(1063U, bitstream.GetPos());
118 EXPECT_EQ(0U, bitstream.BitsRemaining());
119 }
120
TEST(fxcrt,BitStreamSameAsReferenceGetBits32)121 TEST(fxcrt, BitStreamSameAsReferenceGetBits32) {
122 static const unsigned char kData[] = {0xDE, 0x3F, 0xB1, 0x7C,
123 0x12, 0x9A, 0x04, 0x56};
124 CFX_BitStream bitstream(kData);
125 for (int nbits = 1; nbits <= 32; ++nbits) {
126 for (size_t bitpos = 0; bitpos < sizeof(kData) * 8 - nbits; ++bitpos) {
127 bitstream.Rewind();
128 bitstream.SkipBits(bitpos);
129 EXPECT_EQ(bitstream.GetBits(nbits),
130 ReferenceGetBits32(kData, bitpos, nbits));
131 }
132 }
133 }
134