Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_BITBUFFER_H_ |
| 12 | #define WEBRTC_BASE_BITBUFFER_H_ |
| 13 | |
| 14 | #include "webrtc/base/common.h" |
| 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | // A class, similar to ByteBuffer, that can parse bit-sized data out of a set of |
| 19 | // bytes. Has a similar API to the read-only parts of ByteBuffer, plus methods |
| 20 | // for reading bit-sized data and processing exponential golomb encoded data. |
| 21 | // Sizes/counts specify bits/bytes, for clarity. |
| 22 | // Byte order is assumed big-endian/network. |
| 23 | class BitBuffer { |
| 24 | public: |
| 25 | BitBuffer(const uint8* bytes, size_t byte_count); |
| 26 | |
| 27 | // The remaining bits in the byte buffer. |
| 28 | uint64 RemainingBitCount() const; |
| 29 | |
| 30 | // Reads byte-sized values from the buffer. Returns false if there isn't |
| 31 | // enough data left for the specified type. |
| 32 | bool ReadUInt8(uint8* val); |
| 33 | bool ReadUInt16(uint16* val); |
| 34 | bool ReadUInt32(uint32* val); |
| 35 | |
| 36 | // Reads bit-sized values from the buffer. Returns false if there isn't enough |
| 37 | // data left for the specified type. |
| 38 | bool ReadBits(uint32* val, size_t bit_count); |
| 39 | |
| 40 | // Peeks bit-sized values from the buffer. Returns false if there isn't enough |
| 41 | // data left for the specified type. Doesn't move the current read offset. |
| 42 | bool PeekBits(uint32* val, size_t bit_count); |
| 43 | |
| 44 | // Reads the exponential golomb encoded value at the current bit offset. |
| 45 | // Exponential golomb values are encoded as: |
| 46 | // 1) x = source val + 1 |
| 47 | // 2) In binary, write [countbits(x) - 1] 0s, then x |
| 48 | // To decode, we count the number of leading 0 bits, read that many + 1 bits, |
| 49 | // and increment the result by 1. |
| 50 | // Returns false if there isn't enough data left for the specified type, or if |
| 51 | // the value wouldn't fit in a uint32. |
| 52 | bool ReadExponentialGolomb(uint32* val); |
| 53 | |
| 54 | // Moves current position |byte_count| bytes forward. Returns false if |
| 55 | // there aren't enough bytes left in the buffer. |
| 56 | bool ConsumeBytes(size_t byte_count); |
| 57 | // Moves current position |bit_count| bits forward. Returns false if |
| 58 | // there aren't enough bits left in the buffer. |
| 59 | bool ConsumeBits(size_t bit_count); |
| 60 | |
| 61 | private: |
| 62 | const uint8* const bytes_; |
| 63 | // The total size of |bytes_|. |
| 64 | size_t byte_count_; |
| 65 | // The current offset, in bytes, from the start of |bytes_|. |
| 66 | size_t byte_offset_; |
| 67 | // The current offset, in bits, into the current byte. |
| 68 | size_t bit_offset_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(BitBuffer); |
| 71 | }; |
| 72 | |
| 73 | } // namespace rtc |
| 74 | |
| 75 | #endif // WEBRTC_BASE_BITBUFFER_H_ |