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