123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /*!
- * \file src/utils/binary.cpp
- * \brief Binary file reading utilities
- *
- * \author xythobuz
- */
-
- #include <sstream>
-
- #include "global.h"
- #include "utils/binary.h"
-
- BinaryReader::~BinaryReader() {
- }
-
- uint8_t BinaryReader::readU8() {
- uint8_t ret;
- char* c = reinterpret_cast<char*>(&ret);
- read(c, 1);
- return ret;
- }
-
- uint16_t BinaryReader::readU16() {
- uint16_t a = readU8();
- uint16_t b = readU8();
- return (a | (b << 8));
- }
-
- uint32_t BinaryReader::readU32() {
- uint32_t a = readU16();
- uint32_t b = readU16();
- return (a | (b << 16));
- }
-
- uint64_t BinaryReader::readU64() {
- uint64_t a = readU32();
- uint64_t b = readU32();
- return (a | (b << 32));
- }
-
- float BinaryReader::readFloat() {
- uint32_t val = readU32();
- char* a = reinterpret_cast<char*>(&val);
-
- float ret;
- char* b = reinterpret_cast<char*>(&ret);
-
- for (int i = 0; i < 4; i++)
- b[i] = a[i];
-
- return ret;
- }
-
- namespace {
- /*! \fixme Left-Shift with signed integer is undefined!
- * So we can't use the same method as for unsigned integers.
- * Is there a portable way to read multi-byte signed integers,
- * without having to detect the endianness at run-time?
- */
- const int bigendiandetection = 1;
- #define ISBIGENDIAN() ((*reinterpret_cast<const char *>(&bigendiandetection)) == 0)
-
- void swapByteOrder(char* d, unsigned int n) {
- if (ISBIGENDIAN()) {
- for (unsigned int i = 0; i < (n / 2); i++) {
- char tmp = d[i];
- d[i] = d[n - i - 1];
- d[n - i - 1] = tmp;
- }
- }
- }
- }
-
- int8_t BinaryReader::read8() {
- int8_t ret;
- char* p = reinterpret_cast<char*>(&ret);
- read(p, sizeof(ret));
- return ret;
- }
-
- int16_t BinaryReader::read16() {
- int16_t ret;
- char* p = reinterpret_cast<char*>(&ret);
- read(p, sizeof(ret));
- swapByteOrder(p, 2);
- return ret;
- }
-
- int32_t BinaryReader::read32() {
- int32_t ret;
- char* p = reinterpret_cast<char*>(&ret);
- read(p, sizeof(ret));
- swapByteOrder(p, 4);
- return ret;
- }
-
- int64_t BinaryReader::read64() {
- int64_t ret;
- char* p = reinterpret_cast<char*>(&ret);
- read(p, sizeof(ret));
- swapByteOrder(p, 8);
- return ret;
- }
-
- // ----------------------------------------------------------------------------
-
- BinaryFile::BinaryFile(std::string f) {
- assert(open(f) == 0);
- }
-
- BinaryFile::~BinaryFile() {
- if (file.is_open())
- file.close();
- }
-
- int BinaryFile::open(std::string f) {
- if (file.is_open()) {
- return 1;
- } else {
- if (f == "")
- return 0;
- file.open(f, std::ios_base::in | std::ios_base::binary);
- return (file ? 0 : 1);
- }
- }
-
- long long BinaryFile::tell() {
- assert(file.is_open());
- return file.tellg();
- }
-
- void BinaryFile::seek(long long pos) {
- assert(file.is_open());
- file.seekg(pos);
- }
-
- bool BinaryFile::eof() {
- file.peek();
- return file.eof();
- }
-
- void BinaryFile::read(char* d, int c) {
- assert(file.is_open());
- assert(file.good());
- file.read(d, c);
- }
-
- // ----------------------------------------------------------------------------
-
- BinaryMemory::BinaryMemory(const char* d, long long m) : data(nullptr), offset(0), max(-1) {
- assert(open(d, m) == 0);
- }
-
- BinaryMemory::~BinaryMemory() {
- }
-
- int BinaryMemory::open(const char* d, long long m) {
- if (data != nullptr)
- return 1;
-
- if (d != nullptr) {
- data = d;
- offset = 0;
- max = m;
- }
-
- return 0;
- }
-
- long long BinaryMemory::tell() {
- assert(offset >= 0);
- return offset;
- }
-
- void BinaryMemory::seek(long long pos) {
- assert(pos >= 0);
- offset = pos;
- }
-
- bool BinaryMemory::eof() {
- return (offset > max);
- }
-
- void BinaryMemory::read(char* d, int c) {
- assert(offset >= 0);
- assert(c > 0);
- assert((offset + c) <= max);
- for (int i = 0; i < c; i++) {
- d[i] = data[offset + i];
- }
- offset += c;
- }
|