summaryrefslogtreecommitdiff
path: root/chromium/media/base/bit_reader.cc
blob: a956e9765833fbe70fae51aff369930679907e12 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/bit_reader.h"

namespace media {

BitReader::BitReader(const uint8_t* data, int size)
    : initial_size_(size),
      data_(data),
      bytes_left_(size),
      bit_reader_core_(this) {
  DCHECK(data != NULL);
  DCHECK_GE(size, 0);
}

BitReader::~BitReader() = default;

bool BitReader::ReadString(int num_bits, std::string* str) {
  DCHECK_EQ(num_bits % 8, 0);
  DCHECK_GT(num_bits, 0);
  DCHECK(str);
  int num_bytes = num_bits / 8;
  str->resize(num_bytes);
  char* ptr = &str->front();
  while (num_bytes--) {
    if (!ReadBits(8, ptr++))
      return false;
  }
  return true;
}

int BitReader::GetBytes(int max_nbytes, const uint8_t** out) {
  DCHECK_GE(max_nbytes, 0);
  DCHECK(out);

  int nbytes = max_nbytes;
  if (nbytes > bytes_left_)
    nbytes = bytes_left_;

  *out = data_;
  data_ += nbytes;
  bytes_left_ -= nbytes;
  return nbytes;
}

}  // namespace media