1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // Note: ported from Chromium commit head: 2de6929
5
6 #include "bit_reader.h"
7
8 namespace media {
9
BitReader(const uint8_t * data,int size)10 BitReader::BitReader(const uint8_t* data, int size)
11 : initial_size_(size),
12 data_(data),
13 bytes_left_(size),
14 bit_reader_core_(this) {
15 DCHECK(data != NULL);
16 DCHECK_GE(size, 0);
17 }
18
19 BitReader::~BitReader() = default;
20
ReadString(int num_bits,std::string * str)21 bool BitReader::ReadString(int num_bits, std::string* str) {
22 DCHECK_EQ(num_bits % 8, 0);
23 DCHECK_GT(num_bits, 0);
24 DCHECK(str);
25 int num_bytes = num_bits / 8;
26 str->resize(num_bytes);
27 char* ptr = &str->front();
28 while (num_bytes--) {
29 if (!ReadBits(8, ptr++))
30 return false;
31 }
32 return true;
33 }
34
GetBytes(int max_nbytes,const uint8_t ** out)35 int BitReader::GetBytes(int max_nbytes, const uint8_t** out) {
36 DCHECK_GE(max_nbytes, 0);
37 DCHECK(out);
38
39 int nbytes = max_nbytes;
40 if (nbytes > bytes_left_)
41 nbytes = bytes_left_;
42
43 *out = data_;
44 data_ += nbytes;
45 bytes_left_ -= nbytes;
46 return nbytes;
47 }
48
49 } // namespace media
50