aboutsummaryrefslogtreecommitdiffstats
path: root/src/bit_reader.h
blob: aa4a97f4ba3ebe4011574ccc157fad5cf5d38bb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SRC_BIT_READER_H_
#define SRC_BIT_READER_H_

#include <cstddef>
#include <cstdint>

#include "puffin/src/include/puffin/common.h"

namespace puffin {

// An abstract class for reading bits from a deflate stream. It can be used
// either for the beginning of the deflate stream or for any place inside the
// deflate stream. For more information on the pattern of reading, refer to
// RFC1951 in https://www.ietf.org/rfc/rfc1951.txt
class BitReaderInterface {
 public:
  virtual ~BitReaderInterface() = default;

  // Caches at least |nbits| starting from the next available bit (next bit that
  // will be read in |ReadBits|) in the cache. The maximum of number of bits
  // that can be cached is implementation dependent.
  //
  // |nbits| IN  The number of bits to see if available in the input.
  virtual bool CacheBits(size_t nbits) = 0;

  // Reads |nbits| from the cached input. Users should call |CacheBits| with
  // greater than or equal to |nbits| bits before calling this function.
  //
  // |nbits| IN  The number of bits to read from the cache.
  // Returns the read bits as an unsigned integer.
  virtual uint32_t ReadBits(size_t nbits) = 0;

  // Drops |nbits| from the input cache. Users should be careful that |nbits|
  // does not exceed the number of bits in the cache.
  //
  // |nbits| IN  The number of bits to drop from the cache.
  virtual void DropBits(size_t nbits) = 0;

  // TODO(*): Add ReadAndDropBits(uint32_t nbits); Because it is a common
  // pattern.

  // Returns an unsigned byte equal to the unread bits in the first cached
  // byte. This function should not advance the bit pointer in any way. A call
  // to |SkipBoundaryBits| should do the advancement.
  virtual uint8_t ReadBoundaryBits() = 0;

  // Moves the current bit pointer to the beginning of the next byte and returns
  // the number of bits skipped.
  virtual size_t SkipBoundaryBits() = 0;

  // Populates a function that allows reading from the byte that has the next
  // avilable bit for reading. This function clears all the bits that have been
  // cached previously. As a consequence the next |CacheBits| starts reading
  // from a byte boundary. The returned functin can only read |length| bytes. It
  // might be necessary to call |ReadBoundaryBits| and |SkipBoundaryBits| before
  // this function.
  virtual bool GetByteReaderFn(
      size_t length,
      std::function<bool(uint8_t* buffer, size_t count)>* read_fn) = 0;

  // Returns the number of bytes read till now. This size includes the last
  // partially read byte.
  virtual size_t Offset() const = 0;

  // Returns the number of bits read (dropped) till now.
  virtual uint64_t OffsetInBits() const = 0;

  // Returns the number of bits remaining to be cached.
  virtual uint64_t BitsRemaining() const = 0;
};

// A raw buffer implementation of |BitReaderInterface|.
class BufferBitReader : public BitReaderInterface {
 public:
  // Sets the beginning of the buffer that the users wants to read.
  //
  // |in_buf|  IN  The input buffer
  // |in_size| IN  The size of the input buffer
  BufferBitReader(const uint8_t* in_buf, size_t in_size)
      : in_buf_(in_buf),
        in_size_(in_size),
        index_(0),
        in_cache_(0),
        in_cache_bits_(0) {}

  ~BufferBitReader() override = default;

  // Can only cache up to 32 bits.
  bool CacheBits(size_t nbits) override;
  uint32_t ReadBits(size_t nbits) override;
  void DropBits(size_t nbits) override;
  uint8_t ReadBoundaryBits() override;
  size_t SkipBoundaryBits() override;
  bool GetByteReaderFn(
      size_t length,
      std::function<bool(uint8_t* buffer, size_t count)>* read_fn) override;
  size_t Offset() const override;
  uint64_t OffsetInBits() const override;
  uint64_t BitsRemaining() const override;

 private:
  const uint8_t* in_buf_;  // The input buffer.
  uint64_t in_size_;       // The number of bytes in |in_buf_|.
  uint64_t index_;         // The index to the next byte to be read.
  uint32_t in_cache_;      // The temporary buffer to put input data into.
  size_t in_cache_bits_;   // The number of bits available in |in_cache_|.

  DISALLOW_COPY_AND_ASSIGN(BufferBitReader);
};

}  // namespace puffin

#endif  // SRC_BIT_READER_H_