2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_BUFFER_READER_H_
|
|
|
|
#define MEDIA_BASE_BUFFER_READER_H_
|
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
#include <string>
|
2013-11-21 22:33:07 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/compiler_specific.h"
|
|
|
|
#include "packager/base/macros.h"
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
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.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Read1(uint8_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read2(uint16_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read2s(int16_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read4(uint32_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read4s(int32_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read8(uint64_t* v) WARN_UNUSED_RESULT;
|
|
|
|
bool Read8s(int64_t* v) WARN_UNUSED_RESULT;
|
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.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadNBytesInto8(uint64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
|
|
|
|
bool ReadNBytesInto8s(int64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadToVector(std::vector<uint8_t>* t, size_t count) WARN_UNUSED_RESULT;
|
2015-11-23 23:12:04 +00:00
|
|
|
bool ReadToString(std::string* str, size_t size) WARN_UNUSED_RESULT;
|
2013-11-21 22:33:07 +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.
|
2013-11-21 22:33:07 +00:00
|
|
|
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT;
|
|
|
|
|
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>
|
|
|
|
bool Read(T* t) WARN_UNUSED_RESULT;
|
|
|
|
template <typename T>
|
|
|
|
bool ReadNBytes(T* t, size_t num_bytes) WARN_UNUSED_RESULT;
|
|
|
|
|
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
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-11-21 22:33:07 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_BUFFER_READER_H_
|