• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The PDFium Authors
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 
7 #include <limits>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace {
12 
ReferenceGetBits32(const uint8_t * pData,int bitpos,int nbits)13 uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) {
14   int result = 0;
15   for (int i = 0; i < nbits; i++) {
16     if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8)))
17       result |= 1 << (nbits - i - 1);
18   }
19   return result;
20 }
21 
22 }  // namespace
23 
TEST(fxcrt,BitStream)24 TEST(fxcrt, BitStream) {
25   static const uint8_t kData[] = {0x00, 0x11, 0x22, 0x33,
26                                   0x44, 0x55, 0x66, 0x77};
27   CFX_BitStream bitstream(kData);
28 
29   // Initial state.
30   EXPECT_FALSE(bitstream.IsEOF());
31   EXPECT_EQ(0U, bitstream.GetPos());
32   EXPECT_EQ(64U, bitstream.BitsRemaining());
33 
34   // Read, read, read!
35   EXPECT_EQ(0x00U, bitstream.GetBits(8));
36   EXPECT_EQ(8U, bitstream.GetPos());
37   EXPECT_EQ(56U, bitstream.BitsRemaining());
38 
39   EXPECT_EQ(0x00U, bitstream.GetBits(1));
40   EXPECT_EQ(9U, bitstream.GetPos());
41   EXPECT_EQ(55U, bitstream.BitsRemaining());
42 
43   EXPECT_EQ(0x00U, bitstream.GetBits(2));
44   EXPECT_EQ(11U, bitstream.GetPos());
45   EXPECT_EQ(53U, bitstream.BitsRemaining());
46 
47   EXPECT_EQ(0x04U, bitstream.GetBits(3));
48   EXPECT_EQ(14U, bitstream.GetPos());
49   EXPECT_EQ(50U, bitstream.BitsRemaining());
50 
51   EXPECT_EQ(0x04U, bitstream.GetBits(4));
52   EXPECT_EQ(18U, bitstream.GetPos());
53   EXPECT_EQ(46U, bitstream.BitsRemaining());
54 
55   EXPECT_EQ(0x44U, bitstream.GetBits(7));
56   EXPECT_EQ(25U, bitstream.GetPos());
57   EXPECT_EQ(39U, bitstream.BitsRemaining());
58 
59   EXPECT_EQ(0xCDU, bitstream.GetBits(9));
60   EXPECT_EQ(34U, bitstream.GetPos());
61   EXPECT_EQ(30U, bitstream.BitsRemaining());
62 
63   EXPECT_EQ(0x08AAU, bitstream.GetBits(15));
64   EXPECT_EQ(49U, bitstream.GetPos());
65   EXPECT_EQ(15U, bitstream.BitsRemaining());
66 
67   // Cannot advance past the end.
68   EXPECT_EQ(0x00U, bitstream.GetBits(16));
69   EXPECT_EQ(49U, bitstream.GetPos());
70   EXPECT_EQ(15U, bitstream.BitsRemaining());
71 
72   // Make sure SkipBits() works.
73   bitstream.SkipBits(14);
74   EXPECT_EQ(63U, bitstream.GetPos());
75   EXPECT_EQ(1U, bitstream.BitsRemaining());
76   bitstream.SkipBits(2);
77   EXPECT_EQ(65U, bitstream.GetPos());
78   EXPECT_EQ(0U, bitstream.BitsRemaining());
79   EXPECT_TRUE(bitstream.IsEOF());
80 
81   // Make sure Rewind() works.
82   bitstream.Rewind();
83   EXPECT_FALSE(bitstream.IsEOF());
84   EXPECT_EQ(0U, bitstream.GetPos());
85   EXPECT_EQ(64U, bitstream.BitsRemaining());
86 
87   // Read some more.
88   bitstream.SkipBits(5);
89   EXPECT_EQ(5U, bitstream.GetPos());
90   EXPECT_EQ(59U, bitstream.BitsRemaining());
91 
92   EXPECT_EQ(0x0448U, bitstream.GetBits(17));
93   EXPECT_EQ(22U, bitstream.GetPos());
94   EXPECT_EQ(42U, bitstream.BitsRemaining());
95 
96   // Make sure ByteAlign() works.
97   bitstream.ByteAlign();
98   EXPECT_EQ(24U, bitstream.GetPos());
99   EXPECT_EQ(40U, bitstream.BitsRemaining());
100 
101   EXPECT_EQ(0x19A22AB3U, bitstream.GetBits(31));
102   EXPECT_EQ(55U, bitstream.GetPos());
103   EXPECT_EQ(9U, bitstream.BitsRemaining());
104 
105   // Do some bigger reads.
106   bitstream.Rewind();
107   EXPECT_EQ(0x112233U, bitstream.GetBits(32));
108   EXPECT_EQ(32U, bitstream.GetPos());
109   EXPECT_EQ(32U, bitstream.BitsRemaining());
110 
111   bitstream.Rewind();
112   bitstream.SkipBits(31);
113   EXPECT_EQ(0xA22AB33BU, bitstream.GetBits(32));
114   EXPECT_EQ(63U, bitstream.GetPos());
115   EXPECT_EQ(1U, bitstream.BitsRemaining());
116 
117   // Skip past the end.
118   bitstream.SkipBits(1000);
119   EXPECT_TRUE(bitstream.IsEOF());
120   EXPECT_EQ(1063U, bitstream.GetPos());
121   EXPECT_EQ(0U, bitstream.BitsRemaining());
122   EXPECT_EQ(0u, bitstream.GetBits(4));
123 
124   // Align past the end.
125   bitstream.ByteAlign();
126   EXPECT_TRUE(bitstream.IsEOF());
127   EXPECT_EQ(1064U, bitstream.GetPos());
128   EXPECT_EQ(0U, bitstream.BitsRemaining());
129   EXPECT_EQ(0u, bitstream.GetBits(4));
130 }
131 
TEST(fxcrt,BitStreamEmpty)132 TEST(fxcrt, BitStreamEmpty) {
133   CFX_BitStream bitstream({});
134   EXPECT_TRUE(bitstream.IsEOF());
135   EXPECT_EQ(0U, bitstream.GetPos());
136   EXPECT_EQ(0U, bitstream.BitsRemaining());
137 
138   // Getting bits returns zero and doesn't advance.
139   EXPECT_EQ(0u, bitstream.GetBits(4));
140   EXPECT_EQ(0U, bitstream.GetPos());
141 
142   // Skip past the end.
143   bitstream.SkipBits(63);
144   EXPECT_TRUE(bitstream.IsEOF());
145   EXPECT_EQ(63U, bitstream.GetPos());
146   EXPECT_EQ(0U, bitstream.BitsRemaining());
147   EXPECT_EQ(0u, bitstream.GetBits(4));
148 
149   // Align past the end.
150   bitstream.ByteAlign();
151   EXPECT_TRUE(bitstream.IsEOF());
152   EXPECT_EQ(64U, bitstream.GetPos());
153   EXPECT_EQ(0U, bitstream.BitsRemaining());
154   EXPECT_EQ(0u, bitstream.GetBits(4));
155 }
156 
TEST(fxcrt,BitStreamBig)157 TEST(fxcrt, BitStreamBig) {
158   // We can't actually allocate enough memory to test the limits of
159   // the bitstream arithmetic, but as long as we don't try to extract
160   // any bits, the calculations should be unaffected.
161   const uint8_t kNotReallyBigEnough[32] = {};
162   constexpr size_t kAllocationBytes = std::numeric_limits<size_t>::max() / 8;
163   constexpr size_t kAllocationBits = kAllocationBytes * 8;
164   CFX_BitStream bitstream({kNotReallyBigEnough, kAllocationBytes});
165   EXPECT_FALSE(bitstream.IsEOF());
166   EXPECT_EQ(0U, bitstream.GetPos());
167   EXPECT_EQ(kAllocationBits, bitstream.BitsRemaining());
168 
169   // Skip some bits.
170   bitstream.SkipBits(kAllocationBits - 1023);
171   EXPECT_FALSE(bitstream.IsEOF());
172   EXPECT_EQ(kAllocationBits - 1023, bitstream.GetPos());
173   EXPECT_EQ(1023u, bitstream.BitsRemaining());
174 
175   // Align to byte.
176   bitstream.ByteAlign();
177   EXPECT_FALSE(bitstream.IsEOF());
178   EXPECT_EQ(kAllocationBits - 1016, bitstream.GetPos());
179   EXPECT_EQ(1016u, bitstream.BitsRemaining());
180 }
181 
TEST(fxcrt,BitStreamSameAsReferenceGetBits32)182 TEST(fxcrt, BitStreamSameAsReferenceGetBits32) {
183   static const unsigned char kData[] = {0xDE, 0x3F, 0xB1, 0x7C,
184                                         0x12, 0x9A, 0x04, 0x56};
185   CFX_BitStream bitstream(kData);
186   for (int nbits = 1; nbits <= 32; ++nbits) {
187     for (size_t bitpos = 0; bitpos < sizeof(kData) * 8 - nbits; ++bitpos) {
188       bitstream.Rewind();
189       bitstream.SkipBits(bitpos);
190       EXPECT_EQ(bitstream.GetBits(nbits),
191                 ReferenceGetBits32(kData, bitpos, nbits));
192     }
193   }
194 }
195