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
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_reader.h"
|
2013-11-21 22:33:07 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.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-09-30 21:52:21 +00:00
|
|
|
bool BufferReader::Read1(uint8_t* v) {
|
2013-11-21 22:33:07 +00:00
|
|
|
DCHECK(v != NULL);
|
|
|
|
if (!HasBytes(1))
|
|
|
|
return false;
|
|
|
|
*v = buf_[pos_++];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool BufferReader::Read2(uint16_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::Read2s(int16_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::Read4(uint32_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::Read4s(int32_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::Read8(uint64_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::Read8s(int64_t* v) {
|
|
|
|
return Read(v);
|
|
|
|
}
|
|
|
|
bool BufferReader::ReadNBytesInto8(uint64_t* v, size_t num_bytes) {
|
2013-11-21 22:33:07 +00:00
|
|
|
return ReadNBytes(v, num_bytes);
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool BufferReader::ReadNBytesInto8s(int64_t* v, size_t num_bytes) {
|
2013-11-21 22:33:07 +00:00
|
|
|
return ReadNBytes(v, num_bytes);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool BufferReader::ReadToVector(std::vector<uint8_t>* vec, size_t count) {
|
2013-11-21 22:33:07 +00:00
|
|
|
DCHECK(vec != NULL);
|
|
|
|
if (!HasBytes(count))
|
|
|
|
return false;
|
|
|
|
vec->assign(buf_ + pos_, buf_ + pos_ + count);
|
|
|
|
pos_ += count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
bool BufferReader::ReadToString(std::string* str, size_t size) {
|
|
|
|
DCHECK(str);
|
|
|
|
if (!HasBytes(size))
|
|
|
|
return false;
|
|
|
|
str->assign(buf_ + pos_, buf_ + pos_ + size);
|
|
|
|
pos_ += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:43:18 +00:00
|
|
|
bool BufferReader::ReadCString(std::string* str) {
|
|
|
|
DCHECK(str);
|
|
|
|
for (size_t count = 0; pos_ + count < size_; count++) {
|
|
|
|
if (buf_[pos_ + count] == 0) {
|
|
|
|
str->assign(buf_ + pos_, buf_ + pos_ + count);
|
|
|
|
pos_ += count + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // EOF
|
|
|
|
}
|
|
|
|
|
2013-11-21 22:33:07 +00:00
|
|
|
bool BufferReader::SkipBytes(size_t num_bytes) {
|
|
|
|
if (!HasBytes(num_bytes))
|
|
|
|
return false;
|
|
|
|
pos_ += num_bytes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool BufferReader::Read(T* v) {
|
|
|
|
return ReadNBytes(v, sizeof(*v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool BufferReader::ReadNBytes(T* v, size_t num_bytes) {
|
|
|
|
DCHECK(v != NULL);
|
|
|
|
DCHECK_LE(num_bytes, sizeof(*v));
|
|
|
|
if (!HasBytes(num_bytes))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sign extension is required only if
|
|
|
|
// |num_bytes| is less than size of T, and
|
|
|
|
// T is a signed type.
|
|
|
|
const bool sign_extension_required =
|
|
|
|
num_bytes < sizeof(*v) && static_cast<T>(-1) < 0;
|
2014-09-30 21:52:21 +00:00
|
|
|
// Perform sign extension by casting the byte value to int8_t, which will be
|
2013-11-21 22:33:07 +00:00
|
|
|
// sign extended automatically when it is implicitly converted to T.
|
2014-09-30 21:52:21 +00:00
|
|
|
T tmp = sign_extension_required ? static_cast<int8_t>(buf_[pos_++])
|
|
|
|
: buf_[pos_++];
|
2013-11-21 22:33:07 +00:00
|
|
|
for (size_t i = 1; i < num_bytes; ++i) {
|
|
|
|
tmp <<= 8;
|
|
|
|
tmp |= buf_[pos_++];
|
|
|
|
}
|
|
|
|
*v = tmp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|