Shaka Packager SDK
Loading...
Searching...
No Matches
buffer_reader.h
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#ifndef PACKAGER_MEDIA_BASE_BUFFER_READER_H_
8#define PACKAGER_MEDIA_BASE_BUFFER_READER_H_
9
10#include <cstdint>
11#include <string>
12#include <vector>
13
14#include <packager/macros/classes.h>
15
16namespace shaka {
17namespace media {
18
22 public:
24 BufferReader(const uint8_t* buf, size_t size)
25 : buf_(buf), size_(size), pos_(0) {}
26 ~BufferReader() {}
27
30 bool HasBytes(size_t count) { return pos() + count <= size(); }
31
36 [[nodiscard]] bool Read1(uint8_t* v);
37 [[nodiscard]] bool Read2(uint16_t* v);
38 [[nodiscard]] bool Read2s(int16_t* v);
39 [[nodiscard]] bool Read4(uint32_t* v);
40 [[nodiscard]] bool Read4s(int32_t* v);
41 [[nodiscard]] bool Read8(uint64_t* v);
42 [[nodiscard]] bool Read8s(int64_t* v);
44
50 [[nodiscard]] bool ReadNBytesInto8(uint64_t* v, size_t num_bytes);
51 [[nodiscard]] bool ReadNBytesInto8s(int64_t* v, size_t num_bytes);
53
54 [[nodiscard]] bool ReadToVector(std::vector<uint8_t>* t, size_t count);
55 [[nodiscard]] bool ReadToString(std::string* str, size_t size);
56
58 [[nodiscard]] bool ReadCString(std::string* str);
59
62 [[nodiscard]] bool SkipBytes(size_t num_bytes);
63
64 const uint8_t* data() const { return buf_; }
65 size_t size() const { return size_; }
66 void set_size(size_t size) { size_ = size; }
67 size_t pos() const { return pos_; }
68
69 private:
70 // Internal implementation of multi-byte reads.
71 template <typename T>
72 [[nodiscard]] bool Read(T* t);
73 template <typename T>
74 [[nodiscard]] bool ReadNBytes(T* t, size_t num_bytes);
75
76 const uint8_t* buf_;
77 size_t size_;
78 size_t pos_;
79
80 DISALLOW_COPY_AND_ASSIGN(BufferReader);
81};
82
83} // namespace media
84} // namespace shaka
85
86#endif // PACKAGER_MEDIA_BASE_BUFFER_READER_H_
bool ReadCString(std::string *str)
Reads a null-terminated string.
bool HasBytes(size_t count)
BufferReader(const uint8_t *buf, size_t size)
Create a BufferReader from a raw buffer.
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.