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