bit_reader.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Bit reading helpers */
  6. #ifndef BROTLI_DEC_BIT_READER_H_
  7. #define BROTLI_DEC_BIT_READER_H_
  8. #include <string.h> /* memcpy */
  9. #include <brotli/types.h>
  10. #include "../common/constants.h"
  11. #include "../common/platform.h"
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
  16. /* 162 bits + 7 bytes */
  17. #define BROTLI_FAST_INPUT_SLACK 28
  18. BROTLI_INTERNAL extern const brotli_reg_t kBrotliBitMask[33];
  19. static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {
  20. if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
  21. /* Masking with this expression turns to a single
  22. "Unsigned Bit Field Extract" UBFX instruction on ARM. */
  23. return ~(~((brotli_reg_t)0) << n);
  24. } else {
  25. return kBrotliBitMask[n];
  26. }
  27. }
  28. typedef struct {
  29. brotli_reg_t val_; /* pre-fetched bits */
  30. brotli_reg_t bit_pos_; /* current bit-reading position in val_ */
  31. const uint8_t* next_in; /* the byte we're reading from */
  32. const uint8_t* guard_in; /* position from which "fast-path" is prohibited */
  33. const uint8_t* last_in; /* == next_in + avail_in */
  34. } BrotliBitReader;
  35. typedef struct {
  36. brotli_reg_t val_;
  37. brotli_reg_t bit_pos_;
  38. const uint8_t* next_in;
  39. size_t avail_in;
  40. } BrotliBitReaderState;
  41. /* Initializes the BrotliBitReader fields. */
  42. BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* br);
  43. /* Ensures that accumulator is not empty.
  44. May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
  45. Returns BROTLI_FALSE if data is required but there is no input available.
  46. For !BROTLI_UNALIGNED_READ_FAST this function also prepares bit reader for
  47. aligned reading. */
  48. BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* br);
  49. /* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden
  50. the main code-path. Never called for RFC brotli streams, required only for
  51. "large-window" mode and other extensions. */
  52. BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(
  53. BrotliBitReader* br, brotli_reg_t n_bits, brotli_reg_t* val);
  54. static BROTLI_INLINE size_t
  55. BrotliBitReaderGetAvailIn(BrotliBitReader* const br) {
  56. return (size_t)(br->last_in - br->next_in);
  57. }
  58. static BROTLI_INLINE void BrotliBitReaderSaveState(
  59. BrotliBitReader* const from, BrotliBitReaderState* to) {
  60. to->val_ = from->val_;
  61. to->bit_pos_ = from->bit_pos_;
  62. to->next_in = from->next_in;
  63. to->avail_in = BrotliBitReaderGetAvailIn(from);
  64. }
  65. static BROTLI_INLINE void BrotliBitReaderSetInput(
  66. BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) {
  67. br->next_in = next_in;
  68. br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);
  69. if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {
  70. br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);
  71. } else {
  72. br->guard_in = next_in;
  73. }
  74. }
  75. static BROTLI_INLINE void BrotliBitReaderRestoreState(
  76. BrotliBitReader* const to, BrotliBitReaderState* from) {
  77. to->val_ = from->val_;
  78. to->bit_pos_ = from->bit_pos_;
  79. to->next_in = from->next_in;
  80. BrotliBitReaderSetInput(to, from->next_in, from->avail_in);
  81. }
  82. static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits(
  83. const BrotliBitReader* br) {
  84. return br->bit_pos_;
  85. }
  86. /* Returns amount of unread bytes the bit reader still has buffered from the
  87. BrotliInput, including whole bytes in br->val_. Result is capped with
  88. maximal ring-buffer size (larger number won't be utilized anyway). */
  89. static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
  90. static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
  91. size_t avail_in = BrotliBitReaderGetAvailIn(br);
  92. if (avail_in > kCap) return kCap;
  93. return avail_in + (BrotliGetAvailableBits(br) >> 3);
  94. }
  95. /* Checks if there is at least |num| bytes left in the input ring-buffer
  96. (excluding the bits remaining in br->val_). */
  97. static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
  98. BrotliBitReader* const br) {
  99. return TO_BROTLI_BOOL(br->next_in < br->guard_in);
  100. }
  101. /* Load more bits into accumulator. */
  102. static BROTLI_INLINE brotli_reg_t BrotliBitReaderLoadBits(brotli_reg_t val,
  103. brotli_reg_t new_bits,
  104. brotli_reg_t count,
  105. brotli_reg_t offset) {
  106. BROTLI_DCHECK(
  107. !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));
  108. (void)count;
  109. return val | (new_bits << offset);
  110. }
  111. /* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
  112. Precondition: accumulator contains at least 1 bit.
  113. |n_bits| should be in the range [1..24] for regular build. For portable
  114. non-64-bit little-endian build only 16 bits are safe to request. */
  115. static BROTLI_INLINE void BrotliFillBitWindow(
  116. BrotliBitReader* const br, brotli_reg_t n_bits) {
  117. #if (BROTLI_64_BITS)
  118. if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  119. (n_bits <= 8)) {
  120. brotli_reg_t bit_pos = br->bit_pos_;
  121. if (bit_pos <= 8) {
  122. br->val_ = BrotliBitReaderLoadBits(br->val_,
  123. BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);
  124. br->bit_pos_ = bit_pos + 56;
  125. br->next_in += 7;
  126. }
  127. } else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  128. (n_bits <= 16)) {
  129. brotli_reg_t bit_pos = br->bit_pos_;
  130. if (bit_pos <= 16) {
  131. br->val_ = BrotliBitReaderLoadBits(br->val_,
  132. BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);
  133. br->bit_pos_ = bit_pos + 48;
  134. br->next_in += 6;
  135. }
  136. } else {
  137. brotli_reg_t bit_pos = br->bit_pos_;
  138. if (bit_pos <= 32) {
  139. br->val_ = BrotliBitReaderLoadBits(br->val_,
  140. (uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);
  141. br->bit_pos_ = bit_pos + 32;
  142. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  143. }
  144. }
  145. #else
  146. if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
  147. (n_bits <= 8)) {
  148. brotli_reg_t bit_pos = br->bit_pos_;
  149. if (bit_pos <= 8) {
  150. br->val_ = BrotliBitReaderLoadBits(br->val_,
  151. BROTLI_UNALIGNED_LOAD32LE(br->next_in), 24, bit_pos);
  152. br->bit_pos_ = bit_pos + 24;
  153. br->next_in += 3;
  154. }
  155. } else {
  156. brotli_reg_t bit_pos = br->bit_pos_;
  157. if (bit_pos <= 16) {
  158. br->val_ = BrotliBitReaderLoadBits(br->val_,
  159. (uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in), 16, bit_pos);
  160. br->bit_pos_ = bit_pos + 16;
  161. br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
  162. }
  163. }
  164. #endif
  165. }
  166. /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
  167. more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
  168. static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
  169. BrotliFillBitWindow(br, 17);
  170. }
  171. /* Tries to pull one byte of input to accumulator.
  172. Returns BROTLI_FALSE if there is no input available. */
  173. static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
  174. if (br->next_in == br->last_in) {
  175. return BROTLI_FALSE;
  176. }
  177. br->val_ = BrotliBitReaderLoadBits(br->val_,
  178. (brotli_reg_t)*br->next_in, 8, br->bit_pos_);
  179. br->bit_pos_ += 8;
  180. ++br->next_in;
  181. return BROTLI_TRUE;
  182. }
  183. /* Returns currently available bits.
  184. The number of valid bits could be calculated by BrotliGetAvailableBits. */
  185. static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
  186. BrotliBitReader* const br) {
  187. return br->val_;
  188. }
  189. /* Like BrotliGetBits, but does not mask the result.
  190. The result contains at least 16 valid bits. */
  191. static BROTLI_INLINE brotli_reg_t BrotliGet16BitsUnmasked(
  192. BrotliBitReader* const br) {
  193. BrotliFillBitWindow(br, 16);
  194. return (brotli_reg_t)BrotliGetBitsUnmasked(br);
  195. }
  196. /* Returns the specified number of bits from |br| without advancing bit
  197. position. */
  198. static BROTLI_INLINE brotli_reg_t BrotliGetBits(
  199. BrotliBitReader* const br, brotli_reg_t n_bits) {
  200. BrotliFillBitWindow(br, n_bits);
  201. return BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  202. }
  203. /* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
  204. is not enough input. */
  205. static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
  206. BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
  207. while (BrotliGetAvailableBits(br) < n_bits) {
  208. if (!BrotliPullByte(br)) {
  209. return BROTLI_FALSE;
  210. }
  211. }
  212. *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  213. return BROTLI_TRUE;
  214. }
  215. /* Advances the bit pos by |n_bits|. */
  216. static BROTLI_INLINE void BrotliDropBits(
  217. BrotliBitReader* const br, brotli_reg_t n_bits) {
  218. br->bit_pos_ -= n_bits;
  219. br->val_ >>= n_bits;
  220. }
  221. /* Make sure that there are no spectre bits in accumulator.
  222. This is important for the cases when some bytes are skipped
  223. (i.e. never placed into accumulator). */
  224. static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) {
  225. /* Actually, it is enough to normalize when br->bit_pos_ == 0 */
  226. if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) {
  227. br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;
  228. }
  229. }
  230. static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
  231. brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
  232. brotli_reg_t unused_bits = unused_bytes << 3;
  233. br->next_in =
  234. (unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);
  235. br->bit_pos_ -= unused_bits;
  236. BrotliBitReaderNormalize(br);
  237. }
  238. /* Reads the specified number of bits from |br| and advances the bit pos.
  239. Precondition: accumulator MUST contain at least |n_bits|. */
  240. static BROTLI_INLINE void BrotliTakeBits(BrotliBitReader* const br,
  241. brotli_reg_t n_bits,
  242. brotli_reg_t* val) {
  243. *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
  244. BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n",
  245. (int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_,
  246. (int)n_bits, (int)*val));
  247. BrotliDropBits(br, n_bits);
  248. }
  249. /* Reads the specified number of bits from |br| and advances the bit pos.
  250. Assumes that there is enough input to perform BrotliFillBitWindow.
  251. Up to 24 bits are allowed to be requested from this method. */
  252. static BROTLI_INLINE brotli_reg_t BrotliReadBits24(
  253. BrotliBitReader* const br, brotli_reg_t n_bits) {
  254. BROTLI_DCHECK(n_bits <= 24);
  255. if (BROTLI_64_BITS || (n_bits <= 16)) {
  256. brotli_reg_t val;
  257. BrotliFillBitWindow(br, n_bits);
  258. BrotliTakeBits(br, n_bits, &val);
  259. return val;
  260. } else {
  261. brotli_reg_t low_val;
  262. brotli_reg_t high_val;
  263. BrotliFillBitWindow(br, 16);
  264. BrotliTakeBits(br, 16, &low_val);
  265. BrotliFillBitWindow(br, 8);
  266. BrotliTakeBits(br, n_bits - 16, &high_val);
  267. return low_val | (high_val << 16);
  268. }
  269. }
  270. /* Same as BrotliReadBits24, but allows reading up to 32 bits. */
  271. static BROTLI_INLINE brotli_reg_t BrotliReadBits32(
  272. BrotliBitReader* const br, brotli_reg_t n_bits) {
  273. BROTLI_DCHECK(n_bits <= 32);
  274. if (BROTLI_64_BITS || (n_bits <= 16)) {
  275. brotli_reg_t val;
  276. BrotliFillBitWindow(br, n_bits);
  277. BrotliTakeBits(br, n_bits, &val);
  278. return val;
  279. } else {
  280. brotli_reg_t low_val;
  281. brotli_reg_t high_val;
  282. BrotliFillBitWindow(br, 16);
  283. BrotliTakeBits(br, 16, &low_val);
  284. BrotliFillBitWindow(br, 16);
  285. BrotliTakeBits(br, n_bits - 16, &high_val);
  286. return low_val | (high_val << 16);
  287. }
  288. }
  289. /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
  290. is not enough input. |n_bits| MUST be positive.
  291. Up to 24 bits are allowed to be requested from this method. */
  292. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
  293. BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
  294. BROTLI_DCHECK(n_bits <= 24);
  295. while (BrotliGetAvailableBits(br) < n_bits) {
  296. if (!BrotliPullByte(br)) {
  297. return BROTLI_FALSE;
  298. }
  299. }
  300. BrotliTakeBits(br, n_bits, val);
  301. return BROTLI_TRUE;
  302. }
  303. /* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
  304. static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
  305. BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
  306. BROTLI_DCHECK(n_bits <= 32);
  307. if (BROTLI_64_BITS || (n_bits <= 24)) {
  308. while (BrotliGetAvailableBits(br) < n_bits) {
  309. if (!BrotliPullByte(br)) {
  310. return BROTLI_FALSE;
  311. }
  312. }
  313. BrotliTakeBits(br, n_bits, val);
  314. return BROTLI_TRUE;
  315. } else {
  316. return BrotliSafeReadBits32Slow(br, n_bits, val);
  317. }
  318. }
  319. /* Advances the bit reader position to the next byte boundary and verifies
  320. that any skipped bits are set to zero. */
  321. static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
  322. brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
  323. brotli_reg_t pad_bits = 0;
  324. if (pad_bits_count != 0) {
  325. BrotliTakeBits(br, pad_bits_count, &pad_bits);
  326. }
  327. BrotliBitReaderNormalize(br);
  328. return TO_BROTLI_BOOL(pad_bits == 0);
  329. }
  330. static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {
  331. /* Check detour is legal: accumulator must to be empty. */
  332. BROTLI_DCHECK(br->bit_pos_ == 0);
  333. BROTLI_DCHECK(br->val_ == 0);
  334. br->next_in += num;
  335. }
  336. /* Copies remaining input bytes stored in the bit reader to the output. Value
  337. |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
  338. warmed up again after this. */
  339. static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
  340. BrotliBitReader* br, size_t num) {
  341. while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
  342. *dest = (uint8_t)BrotliGetBitsUnmasked(br);
  343. BrotliDropBits(br, 8);
  344. ++dest;
  345. --num;
  346. }
  347. BrotliBitReaderNormalize(br);
  348. if (num > 0) {
  349. memcpy(dest, br->next_in, num);
  350. BrotliDropBytes(br, num);
  351. }
  352. }
  353. BROTLI_UNUSED_FUNCTION void BrotliBitReaderSuppressUnusedFunctions(void) {
  354. BROTLI_UNUSED(&BrotliBitReaderSuppressUnusedFunctions);
  355. BROTLI_UNUSED(&BrotliBitReaderGetAvailIn);
  356. BROTLI_UNUSED(&BrotliBitReaderLoadBits);
  357. BROTLI_UNUSED(&BrotliBitReaderRestoreState);
  358. BROTLI_UNUSED(&BrotliBitReaderSaveState);
  359. BROTLI_UNUSED(&BrotliBitReaderSetInput);
  360. BROTLI_UNUSED(&BrotliBitReaderUnload);
  361. BROTLI_UNUSED(&BrotliCheckInputAmount);
  362. BROTLI_UNUSED(&BrotliCopyBytes);
  363. BROTLI_UNUSED(&BrotliFillBitWindow16);
  364. BROTLI_UNUSED(&BrotliGet16BitsUnmasked);
  365. BROTLI_UNUSED(&BrotliGetBits);
  366. BROTLI_UNUSED(&BrotliGetRemainingBytes);
  367. BROTLI_UNUSED(&BrotliJumpToByteBoundary);
  368. BROTLI_UNUSED(&BrotliReadBits24);
  369. BROTLI_UNUSED(&BrotliReadBits32);
  370. BROTLI_UNUSED(&BrotliSafeGetBits);
  371. BROTLI_UNUSED(&BrotliSafeReadBits);
  372. BROTLI_UNUSED(&BrotliSafeReadBits32);
  373. }
  374. #if defined(__cplusplus) || defined(c_plusplus)
  375. } /* extern "C" */
  376. #endif
  377. #endif /* BROTLI_DEC_BIT_READER_H_ */