2017-10-11 10:51:35 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-10-11 10:55:15 +03:00
|
|
|
#ifndef BUFFER_READER_H_
|
|
|
|
#define BUFFER_READER_H_
|
2017-10-11 10:51:35 +03:00
|
|
|
|
|
|
|
#include "mozilla/EndianUtils.h"
|
2017-10-13 04:44:48 +03:00
|
|
|
#include "nscore.h"
|
2017-10-11 10:51:35 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "MediaData.h"
|
2017-11-06 11:21:19 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2017-10-13 04:44:48 +03:00
|
|
|
#include "mozilla/Result.h"
|
2017-10-11 10:51:35 +03:00
|
|
|
|
2017-11-17 03:53:25 +03:00
|
|
|
namespace mozilla {
|
2017-10-11 10:51:35 +03:00
|
|
|
|
2017-11-06 11:21:19 +03:00
|
|
|
extern mozilla::LazyLogModule gMP4MetadataLog;
|
|
|
|
|
2017-10-11 10:55:15 +03:00
|
|
|
class MOZ_RAII BufferReader {
|
2017-10-11 10:51:35 +03:00
|
|
|
public:
|
2018-06-15 10:25:02 +03:00
|
|
|
BufferReader() : mPtr(nullptr), mRemaining(0), mLength(0) {}
|
2017-10-11 10:55:15 +03:00
|
|
|
BufferReader(const uint8_t* aData, size_t aSize)
|
2017-10-11 10:51:35 +03:00
|
|
|
: mPtr(aData), mRemaining(aSize), mLength(aSize) {}
|
|
|
|
template <size_t S>
|
2017-10-11 10:55:15 +03:00
|
|
|
explicit BufferReader(const AutoTArray<uint8_t, S>& aData)
|
2017-10-11 10:51:35 +03:00
|
|
|
: mPtr(aData.Elements()),
|
|
|
|
mRemaining(aData.Length()),
|
|
|
|
mLength(aData.Length()) {}
|
2017-10-11 10:55:15 +03:00
|
|
|
explicit BufferReader(const nsTArray<uint8_t>& aData)
|
2017-10-11 10:51:35 +03:00
|
|
|
: mPtr(aData.Elements()),
|
|
|
|
mRemaining(aData.Length()),
|
|
|
|
mLength(aData.Length()) {}
|
2017-10-11 10:55:15 +03:00
|
|
|
explicit BufferReader(const mozilla::MediaByteBuffer* aData)
|
2017-10-11 10:51:35 +03:00
|
|
|
: mPtr(aData->Elements()),
|
|
|
|
mRemaining(aData->Length()),
|
|
|
|
mLength(aData->Length()) {}
|
|
|
|
|
|
|
|
void SetData(const nsTArray<uint8_t>& aData) {
|
|
|
|
MOZ_ASSERT(!mPtr && !mRemaining);
|
|
|
|
mPtr = aData.Elements();
|
|
|
|
mRemaining = aData.Length();
|
|
|
|
mLength = mRemaining;
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:55:15 +03:00
|
|
|
~BufferReader() {}
|
2017-10-11 10:51:35 +03:00
|
|
|
|
|
|
|
size_t Offset() const { return mLength - mRemaining; }
|
|
|
|
|
|
|
|
size_t Remaining() const { return mRemaining; }
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<uint8_t, nsresult> ReadU8() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(1);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<uint16_t, nsresult> ReadU16() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(2);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readUint16(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<int16_t, nsresult> ReadLE16() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(2);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::LittleEndian::readInt16(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> ReadU24() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(3);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<int32_t, nsresult> Read24() {
|
2017-10-25 04:58:43 +03:00
|
|
|
return ReadU24().map([](uint32_t x) { return (int32_t)x; });
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<int32_t, nsresult> ReadLE24() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(3);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
int32_t result = int32_t(ptr[2] << 16 | ptr[1] << 8 | ptr[0]);
|
|
|
|
if (result & 0x00800000u) {
|
|
|
|
result -= 0x1000000;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> ReadU32() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(4);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readUint32(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<int32_t, nsresult> Read32() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(4);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readInt32(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<uint64_t, nsresult> ReadU64() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(8);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readUint64(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:16:32 +03:00
|
|
|
mozilla::Result<int64_t, nsresult> Read64() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Read(8);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-13 04:44:48 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readInt64(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* Read(size_t aCount) {
|
|
|
|
if (aCount > mRemaining) {
|
2019-01-22 22:21:23 +03:00
|
|
|
mPtr += mRemaining;
|
2017-10-11 10:51:35 +03:00
|
|
|
mRemaining = 0;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mRemaining -= aCount;
|
|
|
|
|
|
|
|
const uint8_t* result = mPtr;
|
|
|
|
mPtr += aCount;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* Rewind(size_t aCount) {
|
|
|
|
MOZ_ASSERT(aCount <= Offset());
|
|
|
|
size_t rewind = Offset();
|
|
|
|
if (aCount < rewind) {
|
|
|
|
rewind = aCount;
|
|
|
|
}
|
|
|
|
mRemaining += rewind;
|
|
|
|
mPtr -= rewind;
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2017-10-25 10:26:45 +03:00
|
|
|
mozilla::Result<uint8_t, nsresult> PeekU8() const {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Peek(1);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-25 10:26:45 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2017-10-20 06:54:29 +03:00
|
|
|
mozilla::Result<uint16_t, nsresult> PeekU16() const {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Peek(2);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-20 06:54:29 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readUint16(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:58:43 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> PeekU24() const {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Peek(3);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-25 04:58:43 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:58:43 +03:00
|
|
|
mozilla::Result<int32_t, nsresult> Peek24() const {
|
|
|
|
return PeekU24().map([](uint32_t x) { return (int32_t)x; });
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
|
2017-10-20 06:54:29 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> PeekU32() {
|
2017-10-11 10:51:35 +03:00
|
|
|
auto ptr = Peek(4);
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-20 06:54:29 +03:00
|
|
|
return mozilla::Err(NS_ERROR_FAILURE);
|
2017-10-11 10:51:35 +03:00
|
|
|
}
|
|
|
|
return mozilla::BigEndian::readUint32(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* Peek(size_t aCount) const {
|
|
|
|
if (aCount > mRemaining) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* Seek(size_t aOffset) {
|
|
|
|
if (aOffset >= mLength) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure, offset: %zu", __func__, aOffset));
|
2017-10-11 10:51:35 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPtr = mPtr - Offset() + aOffset;
|
|
|
|
mRemaining = mLength - aOffset;
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* Reset() {
|
|
|
|
mPtr -= Offset();
|
|
|
|
mRemaining = mLength;
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Align() const { return 4 - ((intptr_t)mPtr & 3); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool CanReadType() const {
|
|
|
|
return mRemaining >= sizeof(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T ReadType() {
|
|
|
|
auto ptr = Read(sizeof(T));
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-11 10:51:35 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *reinterpret_cast<const T*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
MOZ_MUST_USE bool ReadArray(nsTArray<T>& aDest, size_t aLength) {
|
|
|
|
auto ptr = Read(aLength * sizeof(T));
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-11 10:51:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDest.Clear();
|
|
|
|
aDest.AppendElements(reinterpret_cast<const T*>(ptr), aLength);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
MOZ_MUST_USE bool ReadArray(FallibleTArray<T>& aDest, size_t aLength) {
|
|
|
|
auto ptr = Read(aLength * sizeof(T));
|
|
|
|
if (!ptr) {
|
2017-11-06 11:21:19 +03:00
|
|
|
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
|
|
|
|
("%s: failure", __func__));
|
2017-10-11 10:51:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDest.Clear();
|
|
|
|
if (!aDest.SetCapacity(aLength, mozilla::fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ALWAYS_TRUE(aDest.AppendElements(reinterpret_cast<const T*>(ptr),
|
|
|
|
aLength, mozilla::fallible));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint8_t* mPtr;
|
|
|
|
size_t mRemaining;
|
|
|
|
size_t mLength;
|
|
|
|
};
|
|
|
|
|
2017-11-17 03:53:25 +03:00
|
|
|
} // namespace mozilla
|
2017-10-11 10:51:35 +03:00
|
|
|
|
|
|
|
#endif
|