Shaka Packager SDK
buffer_reader.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/base/buffer_reader.h"
8 
9 #include "packager/base/logging.h"
10 
11 namespace shaka {
12 namespace media {
13 
14 bool BufferReader::Read1(uint8_t* v) {
15  DCHECK(v != NULL);
16  if (!HasBytes(1))
17  return false;
18  *v = buf_[pos_++];
19  return true;
20 }
21 
22 bool BufferReader::Read2(uint16_t* v) {
23  return Read(v);
24 }
25 bool BufferReader::Read2s(int16_t* v) {
26  return Read(v);
27 }
28 bool BufferReader::Read4(uint32_t* v) {
29  return Read(v);
30 }
31 bool BufferReader::Read4s(int32_t* v) {
32  return Read(v);
33 }
34 bool BufferReader::Read8(uint64_t* v) {
35  return Read(v);
36 }
37 bool BufferReader::Read8s(int64_t* v) {
38  return Read(v);
39 }
40 bool BufferReader::ReadNBytesInto8(uint64_t* v, size_t num_bytes) {
41  return ReadNBytes(v, num_bytes);
42 }
43 bool BufferReader::ReadNBytesInto8s(int64_t* v, size_t num_bytes) {
44  return ReadNBytes(v, num_bytes);
45 }
46 
47 bool BufferReader::ReadToVector(std::vector<uint8_t>* vec, size_t count) {
48  DCHECK(vec != NULL);
49  if (!HasBytes(count))
50  return false;
51  vec->assign(buf_ + pos_, buf_ + pos_ + count);
52  pos_ += count;
53  return true;
54 }
55 
56 bool BufferReader::ReadToString(std::string* str, size_t size) {
57  DCHECK(str);
58  if (!HasBytes(size))
59  return false;
60  str->assign(buf_ + pos_, buf_ + pos_ + size);
61  pos_ += size;
62  return true;
63 }
64 
65 bool BufferReader::ReadCString(std::string* str) {
66  DCHECK(str);
67  for (size_t count = 0; pos_ + count < size_; count++) {
68  if (buf_[pos_ + count] == 0) {
69  str->assign(buf_ + pos_, buf_ + pos_ + count);
70  pos_ += count + 1;
71  return true;
72  }
73  }
74  return false; // EOF
75 }
76 
77 bool BufferReader::SkipBytes(size_t num_bytes) {
78  if (!HasBytes(num_bytes))
79  return false;
80  pos_ += num_bytes;
81  return true;
82 }
83 
84 template <typename T>
85 bool BufferReader::Read(T* v) {
86  return ReadNBytes(v, sizeof(*v));
87 }
88 
89 template <typename T>
90 bool BufferReader::ReadNBytes(T* v, size_t num_bytes) {
91  DCHECK(v != NULL);
92  DCHECK_LE(num_bytes, sizeof(*v));
93  if (!HasBytes(num_bytes))
94  return false;
95 
96  // Sign extension is required only if
97  // |num_bytes| is less than size of T, and
98  // T is a signed type.
99  const bool sign_extension_required =
100  num_bytes < sizeof(*v) && static_cast<T>(-1) < 0;
101  // Perform sign extension by casting the byte value to int8_t, which will be
102  // sign extended automatically when it is implicitly converted to T.
103  T tmp = sign_extension_required ? static_cast<int8_t>(buf_[pos_++])
104  : buf_[pos_++];
105  for (size_t i = 1; i < num_bytes; ++i) {
106  tmp <<= 8;
107  tmp |= buf_[pos_++];
108  }
109  *v = tmp;
110  return true;
111 }
112 
113 } // namespace media
114 } // namespace shaka
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::BufferReader::SkipBytes
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
Definition: buffer_reader.cc:77
shaka::media::BufferReader::ReadNBytesInto8
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
Definition: buffer_reader.cc:40
shaka::media::BufferReader::ReadCString
bool ReadCString(std::string *str) WARN_UNUSED_RESULT
Reads a null-terminated string.
Definition: buffer_reader.cc:65
shaka::media::BufferReader::HasBytes
bool HasBytes(size_t count)
Definition: buffer_reader.h:32
shaka::media::BufferReader::Read1
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
Definition: buffer_reader.cc:14