OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/bit_reader.h" | 5 #include "media/base/bit_reader.h" |
6 | 6 |
7 namespace media { | 7 namespace media { |
8 | 8 |
9 BitReader::BitReader(const uint8_t* data, int size) | 9 BitReader::BitReader(const uint8_t* data, int size) |
10 : initial_size_(size), | 10 : initial_size_(size), |
11 data_(data), | 11 data_(data), |
12 bytes_left_(size), | 12 bytes_left_(size), |
13 bit_reader_core_(this) { | 13 bit_reader_core_(this) { |
14 DCHECK(data != NULL); | 14 DCHECK(data != NULL); |
15 DCHECK_GE(size, 0); | 15 DCHECK_GE(size, 0); |
16 } | 16 } |
17 | 17 |
18 BitReader::~BitReader() {} | 18 BitReader::~BitReader() {} |
19 | 19 |
20 bool BitReader::ReadString(int num_bits, std::string* str) { | |
ddorwin
2016/04/12 00:40:47
Why not just pass in num_bytes?
dougsteed
2016/05/08 23:18:44
All the other public functions are in terms of bit
| |
21 int num_bytes = num_bits / 8; | |
22 DCHECK_EQ(num_bits % 8, 0); | |
ddorwin
2016/04/12 00:40:47
Move the DCHECKs up to first line - they are check
dougsteed
2016/05/08 23:18:43
Done.
| |
23 DCHECK(str); | |
24 str->resize(num_bytes); | |
25 char* ptr = &str->front(); | |
ddorwin
2016/04/12 00:40:47
Then add a note to .h that the num_ value must be
ddorwin
2016/04/12 00:40:47
Calling front() on an empty string has undefined b
dougsteed
2016/05/08 23:18:43
Done.
| |
26 while (num_bytes--) | |
ddorwin
2016/04/12 00:40:47
With the nesting, it's probably best to add braces
dougsteed
2016/05/08 23:18:43
Done.
| |
27 if (!ReadBits(8, ptr++)) | |
ddorwin
2016/04/12 00:40:47
Should we note in the .h that this does not guaran
dougsteed
2016/05/08 23:18:43
Done.
| |
28 return false; | |
29 return true; | |
30 } | |
31 | |
20 int BitReader::GetBytes(int max_nbytes, const uint8_t** out) { | 32 int BitReader::GetBytes(int max_nbytes, const uint8_t** out) { |
21 DCHECK_GE(max_nbytes, 0); | 33 DCHECK_GE(max_nbytes, 0); |
22 DCHECK(out); | 34 DCHECK(out); |
23 | 35 |
24 int nbytes = max_nbytes; | 36 int nbytes = max_nbytes; |
25 if (nbytes > bytes_left_) | 37 if (nbytes > bytes_left_) |
26 nbytes = bytes_left_; | 38 nbytes = bytes_left_; |
27 | 39 |
28 *out = data_; | 40 *out = data_; |
29 data_ += nbytes; | 41 data_ += nbytes; |
30 bytes_left_ -= nbytes; | 42 bytes_left_ -= nbytes; |
31 return nbytes; | 43 return nbytes; |
32 } | 44 } |
33 | 45 |
34 } // namespace media | 46 } // namespace media |
OLD | NEW |