2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_BUFFER_READER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_BUFFER_READER_H_
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cstdint>
|
2015-11-23 23:12:04 +00:00
|
|
|
#include <string>
|
2013-11-21 22:33:07 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-21 22:33:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// A simple buffer reader implementation, which reads data of various types
|
|
|
|
/// from a fixed byte array.
|
2013-11-21 22:33:07 +00:00
|
|
|
class BufferReader {
|
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a BufferReader from a raw buffer.
|
2014-09-30 21:52:21 +00:00
|
|
|
BufferReader(const uint8_t* buf, size_t size)
|
2013-11-21 22:33:07 +00:00
|
|
|
: buf_(buf), size_(size), pos_(0) {}
|
|
|
|
~BufferReader() {}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true if there are more than @a count bytes in the stream, false
|
|
|
|
/// otherwise.
|
2013-11-21 22:33:07 +00:00
|
|
|
bool HasBytes(size_t count) { return pos() + count <= size(); }
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Read a value from the stream, performing endian correction, and advance
|
|
|
|
/// the stream pointer.
|
|
|
|
/// @return false if there are not enough bytes in the buffer.
|
|
|
|
/// @{
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool Read1(uint8_t* v);
|
|
|
|
[[nodiscard]] bool Read2(uint16_t* v);
|
|
|
|
[[nodiscard]] bool Read2s(int16_t* v);
|
|
|
|
[[nodiscard]] bool Read4(uint32_t* v);
|
|
|
|
[[nodiscard]] bool Read4s(int32_t* v);
|
|
|
|
[[nodiscard]] bool Read8(uint64_t* v);
|
|
|
|
[[nodiscard]] bool Read8s(int64_t* v);
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Read N-byte integer of the corresponding signedness and store it in the
|
|
|
|
/// 8-byte return type.
|
|
|
|
/// @param num_bytes should not be larger than 8 bytes.
|
|
|
|
/// @return false if there are not enough bytes in the buffer, true otherwise.
|
|
|
|
/// @{
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool ReadNBytesInto8(uint64_t* v, size_t num_bytes);
|
|
|
|
[[nodiscard]] bool ReadNBytesInto8s(int64_t* v, size_t num_bytes);
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool ReadToVector(std::vector<uint8_t>* t, size_t count);
|
|
|
|
[[nodiscard]] bool ReadToString(std::string* str, size_t size);
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2020-10-13 21:43:18 +00:00
|
|
|
/// Reads a null-terminated string.
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool ReadCString(std::string* str);
|
2020-10-13 21:43:18 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Advance the stream by this many bytes.
|
|
|
|
/// @return false if there are not enough bytes in the buffer, true otherwise.
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool SkipBytes(size_t num_bytes);
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data() const { return buf_; }
|
2013-11-21 22:33:07 +00:00
|
|
|
size_t size() const { return size_; }
|
|
|
|
void set_size(size_t size) { size_ = size; }
|
|
|
|
size_t pos() const { return pos_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Internal implementation of multi-byte reads.
|
|
|
|
template <typename T>
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool Read(T* t);
|
2013-11-21 22:33:07 +00:00
|
|
|
template <typename T>
|
2022-11-04 22:46:41 +00:00
|
|
|
[[nodiscard]] bool ReadNBytes(T* t, size_t num_bytes);
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf_;
|
2013-11-21 22:33:07 +00:00
|
|
|
size_t size_;
|
|
|
|
size_t pos_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BufferReader);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_BUFFER_READER_H_
|