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 <cstddef>
11#include <cstdint>
12#include <string>
13#include <vector>
14
15#include <packager/macros/classes.h>
16
17namespace shaka {
18namespace media {
19
23 public:
25 BufferReader(const uint8_t* buf, size_t size)
26 : buf_(buf), size_(size), pos_(0) {}
27 ~BufferReader() {}
28
31 bool HasBytes(size_t count) { return pos() + count <= size(); }
32
37 [[nodiscard]] bool Read1(uint8_t* v);
38 [[nodiscard]] bool Read2(uint16_t* v);
39 [[nodiscard]] bool Read2s(int16_t* v);
40 [[nodiscard]] bool Read4(uint32_t* v);
41 [[nodiscard]] bool Read4s(int32_t* v);
42 [[nodiscard]] bool Read8(uint64_t* v);
43 [[nodiscard]] bool Read8s(int64_t* v);
45
51 [[nodiscard]] bool ReadNBytesInto8(uint64_t* v, size_t num_bytes);
52 [[nodiscard]] bool ReadNBytesInto8s(int64_t* v, size_t num_bytes);
54
55 [[nodiscard]] bool ReadToVector(std::vector<uint8_t>* t, size_t count);
56 [[nodiscard]] bool ReadToString(std::string* str, size_t size);
57
59 [[nodiscard]] bool ReadCString(std::string* str);
60
63 [[nodiscard]] bool SkipBytes(size_t num_bytes);
64
65 const uint8_t* data() const { return buf_; }
66 size_t size() const { return size_; }
67 void set_size(size_t size) { size_ = size; }
68 size_t pos() const { return pos_; }
69
70 private:
71 // Internal implementation of multi-byte reads.
72 template <typename T>
73 [[nodiscard]] bool Read(T* t);
74 template <typename T>
75 [[nodiscard]] bool ReadNBytes(T* t, size_t num_bytes);
76
77 const uint8_t* buf_;
78 size_t size_;
79 size_t pos_;
80
81 DISALLOW_COPY_AND_ASSIGN(BufferReader);
82};
83
84} // namespace media
85} // namespace shaka
86
87#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.