bit_reader.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "./bit_reader.h"
  7. #include "../common/platform.h"
  8. #include <brotli/types.h>
  9. #if defined(__cplusplus) || defined(c_plusplus)
  10. extern "C" {
  11. #endif
  12. void BrotliInitBitReader(BrotliBitReader* const br) {
  13. br->val_ = 0;
  14. br->bit_pos_ = sizeof(br->val_) << 3;
  15. }
  16. BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
  17. size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1;
  18. /* Fixing alignment after unaligned BrotliFillWindow would result accumulator
  19. overflow. If unalignment is caused by BrotliSafeReadBits, then there is
  20. enough space in accumulator to fix alignment. */
  21. if (!BROTLI_ALIGNED_READ) {
  22. aligned_read_mask = 0;
  23. }
  24. if (BrotliGetAvailableBits(br) == 0) {
  25. if (!BrotliPullByte(br)) {
  26. return BROTLI_FALSE;
  27. }
  28. }
  29. while ((((size_t)br->next_in) & aligned_read_mask) != 0) {
  30. if (!BrotliPullByte(br)) {
  31. /* If we consumed all the input, we don't care about the alignment. */
  32. return BROTLI_TRUE;
  33. }
  34. }
  35. return BROTLI_TRUE;
  36. }
  37. #if defined(__cplusplus) || defined(c_plusplus)
  38. } /* extern "C" */
  39. #endif