OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef _JBIG2_BIT_STREAM_H_ | 7 #ifndef CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ |
8 #define _JBIG2_BIT_STREAM_H_ | 8 #define CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ |
9 | 9 |
10 #include "JBig2_Define.h" | 10 #include "../../../include/fxcrt/fx_basic.h" |
11 | 11 |
12 class CJBig2_BitStream { | 12 class CJBig2_BitStream { |
13 public: | 13 public: |
14 CJBig2_BitStream(const uint8_t* pBuffer, FX_DWORD dwLength); | 14 CJBig2_BitStream(const uint8_t* pBuffer, FX_DWORD dwLength); |
15 | |
16 CJBig2_BitStream(CJBig2_BitStream& bs); | |
17 | |
18 ~CJBig2_BitStream(); | 15 ~CJBig2_BitStream(); |
19 | 16 |
| 17 // TODO(thestig): readFoo() should return bool. |
20 int32_t readNBits(FX_DWORD nBits, FX_DWORD* dwResult); | 18 int32_t readNBits(FX_DWORD nBits, FX_DWORD* dwResult); |
21 | |
22 int32_t readNBits(FX_DWORD nBits, int32_t* nResult); | 19 int32_t readNBits(FX_DWORD nBits, int32_t* nResult); |
23 | |
24 int32_t read1Bit(FX_DWORD* dwResult); | 20 int32_t read1Bit(FX_DWORD* dwResult); |
25 | |
26 int32_t read1Bit(FX_BOOL* bResult); | 21 int32_t read1Bit(FX_BOOL* bResult); |
27 | |
28 int32_t read1Byte(uint8_t* cResult); | 22 int32_t read1Byte(uint8_t* cResult); |
29 | |
30 int32_t readInteger(FX_DWORD* dwResult); | 23 int32_t readInteger(FX_DWORD* dwResult); |
31 | |
32 int32_t readShortInteger(FX_WORD* wResult); | 24 int32_t readShortInteger(FX_WORD* wResult); |
33 | |
34 void alignByte(); | 25 void alignByte(); |
35 | 26 uint8_t getCurByte() const; |
36 void align4Byte(); | 27 void incByteIdx(); |
37 | 28 uint8_t getCurByte_arith() const; |
38 uint8_t getAt(FX_DWORD dwOffset); | 29 uint8_t getNextByte_arith() const; |
39 | 30 FX_DWORD getOffset() const; |
40 uint8_t getCurByte(); | |
41 | |
42 uint8_t getNextByte(); | |
43 | |
44 int32_t incByteIdx(); | |
45 | |
46 uint8_t getCurByte_arith(); | |
47 | |
48 uint8_t getNextByte_arith(); | |
49 | |
50 FX_DWORD getOffset(); | |
51 | |
52 void setOffset(FX_DWORD dwOffset); | 31 void setOffset(FX_DWORD dwOffset); |
53 | 32 FX_DWORD getBitPos() const; |
54 FX_DWORD getBitPos(); | |
55 | |
56 void setBitPos(FX_DWORD dwBitPos); | 33 void setBitPos(FX_DWORD dwBitPos); |
57 | 34 const uint8_t* getBuf() const; |
58 const uint8_t* getBuf(); | 35 FX_DWORD getLength() const { return m_dwLength; } |
59 | 36 const uint8_t* getPointer() const; |
60 FX_DWORD getLength() { return m_dwLength; } | |
61 | |
62 const uint8_t* getPointer(); | |
63 | |
64 void offset(FX_DWORD dwOffset); | 37 void offset(FX_DWORD dwOffset); |
65 | 38 FX_DWORD getByteLeft() const; |
66 FX_DWORD getByteLeft(); | |
67 | 39 |
68 private: | 40 private: |
| 41 void AdvanceBit(); |
| 42 bool IsInBound() const; |
| 43 FX_DWORD LengthInBits() const; |
| 44 |
69 const uint8_t* m_pBuf; | 45 const uint8_t* m_pBuf; |
| 46 FX_DWORD m_dwLength; |
| 47 FX_DWORD m_dwByteIdx; |
| 48 FX_DWORD m_dwBitIdx; |
70 | 49 |
71 FX_DWORD m_dwLength; | 50 CJBig2_BitStream(const CJBig2_BitStream&) = delete; |
| 51 void operator=(const CJBig2_BitStream&) = delete; |
| 52 }; |
72 | 53 |
73 FX_DWORD m_dwByteIdx; | 54 #endif // CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ |
74 | |
75 FX_DWORD m_dwBitIdx; | |
76 }; | |
77 inline CJBig2_BitStream::CJBig2_BitStream(const uint8_t* pBuffer, | |
78 FX_DWORD dwLength) { | |
79 m_pBuf = pBuffer; | |
80 m_dwLength = dwLength; | |
81 m_dwByteIdx = 0; | |
82 m_dwBitIdx = 0; | |
83 if (m_dwLength > 256 * 1024 * 1024) { | |
84 m_dwLength = 0; | |
85 m_pBuf = NULL; | |
86 } | |
87 } | |
88 inline CJBig2_BitStream::CJBig2_BitStream(CJBig2_BitStream& bs) { | |
89 m_pBuf = bs.m_pBuf; | |
90 m_dwLength = bs.m_dwLength; | |
91 m_dwByteIdx = bs.m_dwByteIdx; | |
92 m_dwBitIdx = bs.m_dwBitIdx; | |
93 } | |
94 inline CJBig2_BitStream::~CJBig2_BitStream() {} | |
95 inline int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, | |
96 FX_DWORD* dwResult) { | |
97 FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx; | |
98 if (dwTemp <= (m_dwLength << 3)) { | |
99 *dwResult = 0; | |
100 if (dwTemp + dwBits <= (m_dwLength << 3)) { | |
101 dwTemp = dwBits; | |
102 } else { | |
103 dwTemp = (m_dwLength << 3) - dwTemp; | |
104 } | |
105 while (dwTemp > 0) { | |
106 *dwResult = | |
107 (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01); | |
108 if (m_dwBitIdx == 7) { | |
109 m_dwByteIdx++; | |
110 m_dwBitIdx = 0; | |
111 } else { | |
112 m_dwBitIdx++; | |
113 } | |
114 dwTemp--; | |
115 } | |
116 return 0; | |
117 } else { | |
118 return -1; | |
119 } | |
120 } | |
121 inline int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, int32_t* nResult) { | |
122 FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx; | |
123 if (dwTemp <= (m_dwLength << 3)) { | |
124 *nResult = 0; | |
125 if (dwTemp + dwBits <= (m_dwLength << 3)) { | |
126 dwTemp = dwBits; | |
127 } else { | |
128 dwTemp = (m_dwLength << 3) - dwTemp; | |
129 } | |
130 while (dwTemp > 0) { | |
131 *nResult = | |
132 (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01); | |
133 if (m_dwBitIdx == 7) { | |
134 m_dwByteIdx++; | |
135 m_dwBitIdx = 0; | |
136 } else { | |
137 m_dwBitIdx++; | |
138 } | |
139 dwTemp--; | |
140 } | |
141 return 0; | |
142 } else { | |
143 return -1; | |
144 } | |
145 } | |
146 | |
147 inline int32_t CJBig2_BitStream::read1Bit(FX_DWORD* dwResult) { | |
148 if (m_dwByteIdx < m_dwLength) { | |
149 *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01; | |
150 if (m_dwBitIdx == 7) { | |
151 m_dwByteIdx++; | |
152 m_dwBitIdx = 0; | |
153 } else { | |
154 m_dwBitIdx++; | |
155 } | |
156 return 0; | |
157 } else { | |
158 return -1; | |
159 } | |
160 } | |
161 | |
162 inline int32_t CJBig2_BitStream::read1Bit(FX_BOOL* bResult) { | |
163 if (m_dwByteIdx < m_dwLength) { | |
164 *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01; | |
165 if (m_dwBitIdx == 7) { | |
166 m_dwByteIdx++; | |
167 m_dwBitIdx = 0; | |
168 } else { | |
169 m_dwBitIdx++; | |
170 } | |
171 return 0; | |
172 } else { | |
173 return -1; | |
174 } | |
175 } | |
176 inline int32_t CJBig2_BitStream::read1Byte(uint8_t* cResult) { | |
177 if (m_dwByteIdx < m_dwLength) { | |
178 *cResult = m_pBuf[m_dwByteIdx]; | |
179 m_dwByteIdx++; | |
180 return 0; | |
181 } else { | |
182 return -1; | |
183 } | |
184 } | |
185 | |
186 inline int32_t CJBig2_BitStream::readInteger(FX_DWORD* dwResult) { | |
187 if (m_dwByteIdx + 3 < m_dwLength) { | |
188 *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16) | | |
189 (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3]; | |
190 m_dwByteIdx += 4; | |
191 return 0; | |
192 } else { | |
193 return -1; | |
194 } | |
195 } | |
196 | |
197 inline int32_t CJBig2_BitStream::readShortInteger(FX_WORD* dwResult) { | |
198 if (m_dwByteIdx + 1 < m_dwLength) { | |
199 *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1]; | |
200 m_dwByteIdx += 2; | |
201 return 0; | |
202 } else { | |
203 return -1; | |
204 } | |
205 } | |
206 inline void CJBig2_BitStream::alignByte() { | |
207 if (m_dwBitIdx != 0) { | |
208 m_dwByteIdx++; | |
209 m_dwBitIdx = 0; | |
210 } | |
211 } | |
212 inline void CJBig2_BitStream::align4Byte() { | |
213 if (m_dwBitIdx != 0) { | |
214 m_dwByteIdx++; | |
215 m_dwBitIdx = 0; | |
216 } | |
217 m_dwByteIdx = (m_dwByteIdx + 3) & -4; | |
218 } | |
219 inline uint8_t CJBig2_BitStream::getAt(FX_DWORD dwOffset) { | |
220 if (dwOffset < m_dwLength) { | |
221 return m_pBuf[dwOffset]; | |
222 } else { | |
223 return 0; | |
224 } | |
225 } | |
226 inline uint8_t CJBig2_BitStream::getCurByte() { | |
227 if (m_dwByteIdx < m_dwLength) { | |
228 return m_pBuf[m_dwByteIdx]; | |
229 } else { | |
230 return 0; | |
231 } | |
232 } | |
233 inline uint8_t CJBig2_BitStream::getNextByte() { | |
234 if (m_dwByteIdx + 1 < m_dwLength) { | |
235 return m_pBuf[m_dwByteIdx + 1]; | |
236 } else { | |
237 return 0; | |
238 } | |
239 } | |
240 inline int32_t CJBig2_BitStream::incByteIdx() { | |
241 if (m_dwByteIdx < m_dwLength) { | |
242 m_dwByteIdx++; | |
243 return 0; | |
244 } else { | |
245 return -1; | |
246 } | |
247 } | |
248 inline uint8_t CJBig2_BitStream::getCurByte_arith() { | |
249 if (m_dwByteIdx < m_dwLength) { | |
250 return m_pBuf[m_dwByteIdx]; | |
251 } else { | |
252 return 0xff; | |
253 } | |
254 } | |
255 inline uint8_t CJBig2_BitStream::getNextByte_arith() { | |
256 if (m_dwByteIdx + 1 < m_dwLength) { | |
257 return m_pBuf[m_dwByteIdx + 1]; | |
258 } else { | |
259 return 0xff; | |
260 } | |
261 } | |
262 inline FX_DWORD CJBig2_BitStream::getOffset() { | |
263 return m_dwByteIdx; | |
264 } | |
265 inline void CJBig2_BitStream::setOffset(FX_DWORD dwOffset) { | |
266 if (dwOffset > m_dwLength) { | |
267 dwOffset = m_dwLength; | |
268 } | |
269 m_dwByteIdx = dwOffset; | |
270 } | |
271 inline FX_DWORD CJBig2_BitStream::getBitPos() { | |
272 return (m_dwByteIdx << 3) + m_dwBitIdx; | |
273 } | |
274 inline void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos) { | |
275 m_dwByteIdx = dwBitPos >> 3; | |
276 m_dwBitIdx = dwBitPos & 7; | |
277 } | |
278 inline const uint8_t* CJBig2_BitStream::getBuf() { | |
279 return m_pBuf; | |
280 } | |
281 inline const uint8_t* CJBig2_BitStream::getPointer() { | |
282 return m_pBuf + m_dwByteIdx; | |
283 } | |
284 inline void CJBig2_BitStream::offset(FX_DWORD dwOffset) { | |
285 m_dwByteIdx += dwOffset; | |
286 } | |
287 inline FX_DWORD CJBig2_BitStream::getByteLeft() { | |
288 return m_dwLength - m_dwByteIdx; | |
289 } | |
290 #endif | |
OLD | NEW |