359 lines
7.3 KiB
C++
359 lines
7.3 KiB
C++
|
|
/*
|
||
|
|
Copyright (c) 2019 - 2020, Syoyo Fujita.
|
||
|
|
All rights reserved.
|
||
|
|
|
||
|
|
Redistribution and use in source and binary forms, with or without
|
||
|
|
modification, are permitted provided that the following conditions are met:
|
||
|
|
* Redistributions of source code must retain the above copyright
|
||
|
|
notice, this list of conditions and the following disclaimer.
|
||
|
|
* Redistributions in binary form must reproduce the above copyright
|
||
|
|
notice, this list of conditions and the following disclaimer in the
|
||
|
|
documentation and/or other materials provided with the distribution.
|
||
|
|
* Neither the name of the Syoyo Fujita nor the
|
||
|
|
names of its contributors may be used to endorse or promote products
|
||
|
|
derived from this software without specific prior written permission.
|
||
|
|
|
||
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||
|
|
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
||
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
*/
|
||
|
|
|
||
|
|
//
|
||
|
|
// Simple byte stream reader. Consider endianness when reading 2, 4, 8 bytes data.
|
||
|
|
//
|
||
|
|
|
||
|
|
#include <cstdint>
|
||
|
|
#include <cstdlib>
|
||
|
|
#include <cstring>
|
||
|
|
#include <memory>
|
||
|
|
|
||
|
|
namespace tinyusdz {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
static inline void swap2(unsigned short *val) {
|
||
|
|
unsigned short tmp = *val;
|
||
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(val);
|
||
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
|
||
|
|
|
||
|
|
dst[0] = src[1];
|
||
|
|
dst[1] = src[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void swap4(uint32_t *val) {
|
||
|
|
uint32_t tmp = *val;
|
||
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(val);
|
||
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
|
||
|
|
|
||
|
|
dst[0] = src[3];
|
||
|
|
dst[1] = src[2];
|
||
|
|
dst[2] = src[1];
|
||
|
|
dst[3] = src[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void swap4(int *val) {
|
||
|
|
int tmp = *val;
|
||
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(val);
|
||
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
|
||
|
|
|
||
|
|
dst[0] = src[3];
|
||
|
|
dst[1] = src[2];
|
||
|
|
dst[2] = src[1];
|
||
|
|
dst[3] = src[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void swap8(uint64_t *val) {
|
||
|
|
uint64_t tmp = (*val);
|
||
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(val);
|
||
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
|
||
|
|
|
||
|
|
dst[0] = src[7];
|
||
|
|
dst[1] = src[6];
|
||
|
|
dst[2] = src[5];
|
||
|
|
dst[3] = src[4];
|
||
|
|
dst[4] = src[3];
|
||
|
|
dst[5] = src[2];
|
||
|
|
dst[6] = src[1];
|
||
|
|
dst[7] = src[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void swap8(int64_t *val) {
|
||
|
|
int64_t tmp = (*val);
|
||
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(val);
|
||
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(&tmp);
|
||
|
|
|
||
|
|
dst[0] = src[7];
|
||
|
|
dst[1] = src[6];
|
||
|
|
dst[2] = src[5];
|
||
|
|
dst[3] = src[4];
|
||
|
|
dst[4] = src[3];
|
||
|
|
dst[5] = src[2];
|
||
|
|
dst[6] = src[1];
|
||
|
|
dst[7] = src[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
///
|
||
|
|
/// Simple stream reader
|
||
|
|
///
|
||
|
|
class StreamReader {
|
||
|
|
public:
|
||
|
|
explicit StreamReader(const uint8_t *binary, const size_t length,
|
||
|
|
const bool swap_endian)
|
||
|
|
: binary_(binary), length_(length), swap_endian_(swap_endian), idx_(0) {
|
||
|
|
(void)pad_;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool seek_set(const uint64_t offset) const {
|
||
|
|
if (offset > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
idx_ = offset;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool seek_from_currect(const int64_t offset) const {
|
||
|
|
if ((int64_t(idx_) + offset) < 0) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (size_t((int64_t(idx_) + offset)) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
idx_ = size_t(int64_t(idx_) + offset);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
size_t read(const size_t n, const uint64_t dst_len, uint8_t *dst) const {
|
||
|
|
size_t len = n;
|
||
|
|
if ((idx_ + len) > length_) {
|
||
|
|
len = length_ - size_t(idx_);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (len > 0) {
|
||
|
|
if (dst_len < len) {
|
||
|
|
// dst does not have enough space. return 0 for a while.
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
memcpy(dst, &binary_[idx_], len);
|
||
|
|
idx_ += len;
|
||
|
|
return len;
|
||
|
|
|
||
|
|
} else {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read1(uint8_t *ret) const {
|
||
|
|
if ((idx_ + 1) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const uint8_t val = binary_[idx_];
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 1;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read_bool(bool *ret) const {
|
||
|
|
if ((idx_ + 1) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const char val = static_cast<const char>(binary_[idx_]);
|
||
|
|
|
||
|
|
(*ret) = bool(val);
|
||
|
|
idx_ += 1;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read1(char *ret) const {
|
||
|
|
if ((idx_ + 1) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const char val = static_cast<const char>(binary_[idx_]);
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 1;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read2(unsigned short *ret) const {
|
||
|
|
if ((idx_ + 2) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
unsigned short val =
|
||
|
|
*(reinterpret_cast<const unsigned short *>(&binary_[idx_]));
|
||
|
|
|
||
|
|
if (swap_endian_) {
|
||
|
|
swap2(&val);
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 2;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read4(uint32_t *ret) const {
|
||
|
|
if ((idx_ + 4) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint32_t val = *(reinterpret_cast<const uint32_t *>(&binary_[idx_]));
|
||
|
|
|
||
|
|
if (swap_endian_) {
|
||
|
|
swap4(&val);
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 4;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read4(int *ret) const {
|
||
|
|
if ((idx_ + 4) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
int val = *(reinterpret_cast<const int *>(&binary_[idx_]));
|
||
|
|
|
||
|
|
if (swap_endian_) {
|
||
|
|
swap4(&val);
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 4;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read8(uint64_t *ret) const {
|
||
|
|
if ((idx_ + 8) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint64_t val = *(reinterpret_cast<const uint64_t *>(&binary_[idx_]));
|
||
|
|
|
||
|
|
if (swap_endian_) {
|
||
|
|
swap8(&val);
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 8;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read8(int64_t *ret) const {
|
||
|
|
if ((idx_ + 8) > length_) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
int64_t val = *(reinterpret_cast<const int64_t *>(&binary_[idx_]));
|
||
|
|
|
||
|
|
if (swap_endian_) {
|
||
|
|
swap8(&val);
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = val;
|
||
|
|
idx_ += 8;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read_float(float *ret) const {
|
||
|
|
if (!ret) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
float value;
|
||
|
|
if (!read4(reinterpret_cast<int *>(&value))) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = value;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read_double(double *ret) const {
|
||
|
|
if (!ret) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
double value;
|
||
|
|
if (!read8(reinterpret_cast<uint64_t *>(&value))) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*ret) = value;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#if 0
|
||
|
|
bool read_value(Value *inout) {
|
||
|
|
if (!inout) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (inout->Type() == VALUE_TYPE_FLOAT) {
|
||
|
|
float value;
|
||
|
|
if (!read_float(&value)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*inout) = Value(value);
|
||
|
|
} else if (inout->Type() == VALUE_TYPE_INT) {
|
||
|
|
int value;
|
||
|
|
if (!read4(&value)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
(*inout) = Value(value);
|
||
|
|
} else {
|
||
|
|
TINYVDBIO_ASSERT(0);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
size_t tell() const { return size_t(idx_); }
|
||
|
|
|
||
|
|
const uint8_t *data() const { return binary_; }
|
||
|
|
|
||
|
|
bool swap_endian() const { return swap_endian_; }
|
||
|
|
|
||
|
|
size_t size() const { return length_; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
const uint8_t *binary_;
|
||
|
|
const size_t length_;
|
||
|
|
bool swap_endian_;
|
||
|
|
char pad_[7];
|
||
|
|
mutable uint64_t idx_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace tinyusdz
|