Shaka Packager SDK
buffer_reader.h
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 #ifndef PACKAGER_MEDIA_BASE_BUFFER_READER_H_
8 #define PACKAGER_MEDIA_BASE_BUFFER_READER_H_
9 
10 #include <stdint.h>
11 
12 #include <string>
13 #include <vector>
14 
15 #include "packager/base/compiler_specific.h"
16 #include "packager/base/macros.h"
17 
18 namespace shaka {
19 namespace media {
20 
23 class BufferReader {
24  public:
26  BufferReader(const uint8_t* buf, size_t size)
27  : buf_(buf), size_(size), pos_(0) {}
28  ~BufferReader() {}
29 
32  bool HasBytes(size_t count) { return pos() + count <= size(); }
33 
38  bool Read1(uint8_t* v) WARN_UNUSED_RESULT;
39  bool Read2(uint16_t* v) WARN_UNUSED_RESULT;
40  bool Read2s(int16_t* v) WARN_UNUSED_RESULT;
41  bool Read4(uint32_t* v) WARN_UNUSED_RESULT;
42  bool Read4s(int32_t* v) WARN_UNUSED_RESULT;
43  bool Read8(uint64_t* v) WARN_UNUSED_RESULT;
44  bool Read8s(int64_t* v) WARN_UNUSED_RESULT;
46 
52  bool ReadNBytesInto8(uint64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
53  bool ReadNBytesInto8s(int64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
55 
56  bool ReadToVector(std::vector<uint8_t>* t, size_t count) WARN_UNUSED_RESULT;
57  bool ReadToString(std::string* str, size_t size) WARN_UNUSED_RESULT;
58 
60  bool ReadCString(std::string* str) WARN_UNUSED_RESULT;
61 
64  bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT;
65 
66  const uint8_t* data() const { return buf_; }
67  size_t size() const { return size_; }
68  void set_size(size_t size) { size_ = size; }
69  size_t pos() const { return pos_; }
70 
71  private:
72  // Internal implementation of multi-byte reads.
73  template <typename T>
74  bool Read(T* t) WARN_UNUSED_RESULT;
75  template <typename T>
76  bool ReadNBytes(T* t, size_t num_bytes) WARN_UNUSED_RESULT;
77 
78  const uint8_t* buf_;
79  size_t size_;
80  size_t pos_;
81 
82  DISALLOW_COPY_AND_ASSIGN(BufferReader);
83 };
84 
85 } // namespace media
86 } // namespace shaka
87 
88 #endif // PACKAGER_MEDIA_BASE_BUFFER_READER_H_
bool HasBytes(size_t count)
Definition: buffer_reader.h:32
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
BufferReader(const uint8_t *buf, size_t size)
Create a BufferReader from a raw buffer.
Definition: buffer_reader.h:26
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
bool ReadCString(std::string *str) WARN_UNUSED_RESULT
Reads a null-terminated string.
All the methods that are virtual are virtual for mocking.