Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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::SkipBytes(size_t num_bytes) {
66  if (!HasBytes(num_bytes))
67  return false;
68  pos_ += num_bytes;
69  return true;
70 }
71 
72 template <typename T>
73 bool BufferReader::Read(T* v) {
74  return ReadNBytes(v, sizeof(*v));
75 }
76 
77 template <typename T>
78 bool BufferReader::ReadNBytes(T* v, size_t num_bytes) {
79  DCHECK(v != NULL);
80  DCHECK_LE(num_bytes, sizeof(*v));
81  if (!HasBytes(num_bytes))
82  return false;
83 
84  // Sign extension is required only if
85  // |num_bytes| is less than size of T, and
86  // T is a signed type.
87  const bool sign_extension_required =
88  num_bytes < sizeof(*v) && static_cast<T>(-1) < 0;
89  // Perform sign extension by casting the byte value to int8_t, which will be
90  // sign extended automatically when it is implicitly converted to T.
91  T tmp = sign_extension_required ? static_cast<int8_t>(buf_[pos_++])
92  : buf_[pos_++];
93  for (size_t i = 1; i < num_bytes; ++i) {
94  tmp <<= 8;
95  tmp |= buf_[pos_++];
96  }
97  *v = tmp;
98  return true;
99 }
100 
101 } // namespace media
102 } // namespace shaka
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
bool HasBytes(size_t count)
Definition: buffer_reader.h:32
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT