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
5 #include "bit_reader.h"
6
7 namespace media {
8
BitReader(const uint8_t * data,int size)9 BitReader::BitReader(const uint8_t* data, int size)
10 : initial_size_(size),
11 data_(data),
12 bytes_left_(size),
13 bit_reader_core_(this) {
14 DCHECK(data != NULL);
15 DCHECK_GE(size, 0);
16 }
17
~BitReader()18 BitReader::~BitReader() {}
19
ReadString(int num_bits,std::string * str)20 bool BitReader::ReadString(int num_bits, std::string* str) {
21 DCHECK_EQ(num_bits % 8, 0);
22 DCHECK_GT(num_bits, 0);
23 DCHECK(str);
24 int num_bytes = num_bits / 8;
25 str->resize(num_bytes);
26 char* ptr = &str->front();
27 while (num_bytes--) {
28 if (!ReadBits(8, ptr++))
29 return false;
30 }
31 return true;
32 }
33
GetBytes(int max_nbytes,const uint8_t ** out)34 int BitReader::GetBytes(int max_nbytes, const uint8_t** out) {
35 DCHECK_GE(max_nbytes, 0);
36 DCHECK(out);
37
38 int nbytes = max_nbytes;
39 if (nbytes > bytes_left_)
40 nbytes = bytes_left_;
41
42 *out = data_;
43 data_ += nbytes;
44 bytes_left_ -= nbytes;
45 return nbytes;
46 }
47
48 } // namespace media
49