I've written a small .h and .cpp that can now handle (probably) all endianness problems. While I have adapted the functions for my own application, they might help someone.
endian_bis.h:
/**
* endian_bis.h - endian-gnostic binary input stream functions
* Copyright (C) 2015
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <cstdint>
#include <istream>
class BinaryInputStream {
public:
inline int8_t read_int8(std::istream &in) { char buf[1]; in.read(buf, 1); return read_int8(buf, 0); }
inline int16_t read_int16(std::istream &in) { char buf[2]; in.read(buf, 2); return read_int16(buf, 0); }
inline int32_t read_int32(std::istream &in) { char buf[4]; in.read(buf, 4); return read_int32(buf, 0); }
inline int64_t read_int64(std::istream &in) { char buf[8]; in.read(buf, 8); return read_int64(buf, 0); }
inline uint8_t read_uint8(std::istream &in) { char buf[1]; in.read(buf, 1); return read_uint8(buf, 0); }
inline uint16_t read_uint16(std::istream &in) { char buf[2]; in.read(buf, 2); return read_uint16(buf, 0); }
inline uint32_t read_uint32(std::istream &in) { char buf[4]; in.read(buf, 4); return read_uint32(buf, 0); }
inline uint64_t read_uint64(std::istream &in) { char buf[8]; in.read(buf, 8); return read_uint64(buf, 0); }
inline float read_float(std::istream &in) { char buf[4]; in.read(buf, 4); return read_float(buf, 0); }
inline double read_double(std::istream &in) { char buf[8]; in.read(buf, 8); return read_double(buf, 0); }
inline int8_t read_int8(char buf[], int off) { return (int8_t)buf[off]; }
inline uint8_t read_uint8(char buf[], int off) { return (uint8_t)buf[off]; }
virtual int16_t read_int16(char buf[], int off) = 0;
virtual int32_t read_int32(char buf[], int off) = 0;
virtual int64_t read_int64(char buf[], int off) = 0;
virtual uint16_t read_uint16(char buf[], int off) = 0;
virtual uint32_t read_uint32(char buf[], int off) = 0;
virtual uint64_t read_uint64(char buf[], int off) = 0;
virtual float read_float(char buf[], int off) = 0;
virtual double read_double(char buf[], int off) = 0;
static BinaryInputStream *endianCorrectStream(int streamIsBigEndian);
static BinaryInputStream *endianCorrectStream(std::istream &in,
uint32_t expectedBigEndianMagic,
uint32_t expectedLittleEndianMagic);
};
endian_bis.cpp:
/**
* endian_bis.cpp - endian-gnostic binary input stream functions
* Copyright (C) 2015 Jonah Schreiber (jonah.schreiber@gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "endian_bis.h"
#include <cstring>
/*
* Delegated functions
*/
static inline int16_t read_be_int16(char buf[], int off) {
return (int16_t)(((buf[off] & 0xff) << 8) |
((buf[off+1] & 0xff)));
}
static inline int32_t read_be_int32(char buf[], int off) {
return (int32_t)(((buf[off] & 0xff) << 24) |
((buf[off+1] & 0xff) << 16) |
((buf[off+2] & 0xff) << 8) |
((buf[off+3] & 0xff)));
}
template<int> static inline int64_t read_be_int64(char buf[], int off); // template indicates default word size (size_t)
template<> inline int64_t read_be_int64<4>(char buf[], int off) {
return (((int64_t)(((buf[off] & 0xff) << 24) |
((buf[off+1] & 0xff) << 16) |
((buf[off+2] & 0xff) << 8) |
((buf[off+3] & 0xff)))
) << 32) | (
(int64_t)(((buf[off+4] & 0xff) << 24) |
((buf[off+5] & 0xff) << 16) |
((buf[off+6] & 0xff) << 8) |
((buf[off+7] & 0xff))));
}
static inline uint16_t read_be_uint16(char buf[], int off) {
return (uint16_t)(((buf[off] & 0xff) << 8) |
((buf[off+1] & 0xff)));
}
static inline uint32_t read_be_uint32(char buf[], int off) {
return (uint32_t)(((buf[off] & 0xff) << 24) |
((buf[off+1] & 0xff) << 16) |
((buf[off+2] & 0xff) << 8) |
((buf[off+3] & 0xff)));
}
template<int> static inline uint64_t read_be_uint64(char buf[], int off); // template indicates default word size (size_t)
template<> inline uint64_t read_be_uint64<4>(char buf[], int off) {
return (((uint64_t)(((buf[off] & 0xff) << 24) |
((buf[off+1] & 0xff) << 16) |
((buf[off+2] & 0xff) << 8) |
((buf[off+3] & 0xff)))
) << 32) | (
(uint64_t)(((buf[off+4] & 0xff) << 24) |
((buf[off+5] & 0xff) << 16) |
((buf[off+6] & 0xff) << 8) |
((buf[off+7] & 0xff))));
}
inline static int16_t read_le_int16(char buf[], int off) {
return (int16_t)(((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff)));
}
inline static int32_t read_le_int32(char buf[], int off) {
return (int32_t)(((buf[off+3] & 0xff) << 24) |
((buf[off+2] & 0xff) << 16) |
((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff)));
}
template<int> static inline int64_t read_le_int64(char buf[], int off); // template indicates default word size (size_t)
template<> inline int64_t read_le_int64<4>(char buf[], int off) {
return (((int64_t)(((buf[off+7] & 0xff) << 24) |
((buf[off+6] & 0xff) << 16) |
((buf[off+5] & 0xff) << 8) |
((buf[off+4] & 0xff)))
) << 32) | (
(int64_t)(((buf[off+3] & 0xff) << 24) |
((buf[off+2] & 0xff) << 16) |
((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff))));
}
inline static uint16_t read_le_uint16(char buf[], int off) {
return (uint16_t)(((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff)));
}
inline static uint32_t read_le_uint32(char buf[], int off) {
return (uint32_t)(((buf[off+3] & 0xff) << 24) |
((buf[off+2] & 0xff) << 16) |
((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff)));
}
template<int> static inline uint64_t read_le_uint64(char buf[], int off); // template indicates default word size (size_t)
template<> inline uint64_t read_le_uint64<4>(char buf[], int off) {
return (((uint64_t)(((buf[off+7] & 0xff) << 24) |
((buf[off+6] & 0xff) << 16) |
((buf[off+5] & 0xff)<< 8) |
((buf[off+4] & 0xff)))
) << 32) | (
(uint64_t)(((buf[off+3] & 0xff) << 24) |
((buf[off+2] & 0xff) << 16) |
((buf[off+1] & 0xff) << 8) |
((buf[off] & 0xff))));
}
/* WARNING: UNTESTED FOR 64 BIT ARCHITECTURES; FILL IN 3 MORE METHODS LIKE THIS TO TEST
THE CORRECT FUNCTION WILL BE SELECTED AUTOMATICALLY AT COMPILE TIME
template<> inline uint64_t read_uint64_branch<8>(char buf[], int off) {
return (int64_t)((buf[off] << 56) |
(buf[off+1] << 48) |
(buf[off+2] << 40) |
(buf[off+3] << 32) |
(buf[off+4] << 24) |
(buf[off+5] << 16) |
(buf[off+6] << 8) |
(buf[off+7]));
}*/
inline static float read_matching_float(char buf[], int off) {
float f;
memcpy(&f, &buf[off], 4);
return f;
}
inline static float read_mismatched_float(char buf[], int off) {
float f;
char buf2[4] = {buf[3], buf[2], buf[1], buf[0]};
memcpy(&f, buf2, 4);
return f;
}
inline static double read_matching_double(char buf[], int off) {
double d;
memcpy(&d, &buf[off], 8);
return d;
}
inline static double read_mismatched_double(char buf[], int off) {
double d;
char buf2[8] = {buf[7], buf[6], buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]};
memcpy(&d, buf2, 4);
return d;
}
/*
* Types (singleton instantiations)
*/
/*
* Big-endian stream, Big-endian runtime
*/
static class : public BinaryInputStream {
public:
int16_t read_int16(char buf[], int off) { return read_be_int16(buf, off); }
int32_t read_int32(char buf[], int off) { return read_be_int32(buf, off); }
int64_t read_int64(char buf[], int off) { return read_be_int64<sizeof(size_t)>(buf, off); }
uint16_t read_uint16(char buf[], int off) { return read_be_uint16(buf, off); }
uint32_t read_uint32(char buf[], int off) { return read_be_uint32(buf, off); }
uint64_t read_uint64(char buf[], int off) { return read_be_uint64<sizeof(size_t)>(buf, off); }
float read_float(char buf[], int off) { return read_matching_float(buf, off); }
double read_double(char buf[], int off) { return read_matching_double(buf, off); }
} beStreamBeRuntime;
/*
* Big-endian stream, Little-endian runtime
*/
static class : public BinaryInputStream {
public:
int16_t read_int16(char buf[], int off) { return read_be_int16(buf, off); }
int32_t read_int32(char buf[], int off) { return read_be_int32(buf, off); }
int64_t read_int64(char buf[], int off) { return read_be_int64<sizeof(size_t)>(buf, off); }
uint16_t read_uint16(char buf[], int off) { return read_be_uint16(buf, off); }
uint32_t read_uint32(char buf[], int off) { return read_be_uint32(buf, off); }
uint64_t read_uint64(char buf[], int off) { return read_be_uint64<sizeof(size_t)>(buf, off); }
float read_float(char buf[], int off) { return read_mismatched_float(buf, off); }
double read_double(char buf[], int off) { return read_mismatched_double(buf, off); }
} beStreamLeRuntime;
/*
* Little-endian stream, Big-endian runtime
*/
static class : public BinaryInputStream {
public:
int16_t read_int16(char buf[], int off) { return read_le_int16(buf, off); }
int32_t read_int32(char buf[], int off) { return read_le_int32(buf, off); }
int64_t read_int64(char buf[], int off) { return read_le_int64<sizeof(size_t)>(buf, off); }
uint16_t read_uint16(char buf[], int off) { return read_le_uint16(buf, off); }
uint32_t read_uint32(char buf[], int off) { return read_le_uint32(buf, off); }
uint64_t read_uint64(char buf[], int off) { return read_le_uint64<sizeof(size_t)>(buf, off); }
float read_float(char buf[], int off) { return read_mismatched_float(buf, off); }
double read_double(char buf[], int off) { return read_mismatched_double(buf, off); }
} leStreamBeRuntime;
/*
* Little-endian stream, Little-endian runtime
*/
static class : public BinaryInputStream {
public:
int16_t read_int16(char buf[], int off) { return read_le_int16(buf, off); }
int32_t read_int32(char buf[], int off) { return read_le_int32(buf, off); }
int64_t read_int64(char buf[], int off) { return read_le_int64<sizeof(size_t)>(buf, off); }
uint16_t read_uint16(char buf[], int off) { return read_le_uint16(buf, off); }
uint32_t read_uint32(char buf[], int off) { return read_le_uint32(buf, off); }
uint64_t read_uint64(char buf[], int off) { return read_le_uint64<sizeof(size_t)>(buf, off); }
float read_float(char buf[], int off) { return read_matching_float(buf, off); }
double read_double(char buf[], int off) { return read_matching_double(buf, off); }
} leStreamLeRuntime;
/*
* "Factory" singleton methods (plus helper)
*/
static inline int isRuntimeBigEndian() {
union { int32_t i; int8_t c[4]; } bint = {0x01020304};
return bint.c[0] == 1;
}
BinaryInputStream *BinaryInputStream::endianCorrectStream(int streamIsBigEndian) {
if (streamIsBigEndian) {
if (isRuntimeBigEndian()) {
return &beStreamBeRuntime;
} else {
return &beStreamLeRuntime;
}
} else {
if (isRuntimeBigEndian()) {
return &leStreamBeRuntime;
} else {
return &leStreamLeRuntime;
}
}
}
BinaryInputStream *BinaryInputStream::endianCorrectStream(std::istream &in,
uint32_t expectedBigEndianMagic,
uint32_t expectedLittleEndianMagic) {
uint32_t magic = ((BinaryInputStream*)&beStreamBeRuntime)->read_uint32(in);
if (magic == expectedBigEndianMagic) {
if (isRuntimeBigEndian()) {
return &beStreamBeRuntime;
} else {
return &beStreamLeRuntime;
}
} else if (magic == expectedLittleEndianMagic) {
if (isRuntimeBigEndian()) {
return &leStreamBeRuntime;
} else {
return &leStreamLeRuntime;
}
} else {
return 0; /* not expected magic number */
}
}
Suggested use:
BinaryInputStream *bis = BinaryInputStream::endianCorrectStream(in, 0x01020304, 0x04030201);
if (bis == 0) {
cerr << "error: infile is not an Acme EarthQUAKEZ file" << endl;
return 1;
}
in.ignore(4);
int32_t number = bis->read_int32(in);
...