From 1dcf37c0fc6fc75e04a691796e7cbcc9771227f7 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Fri, 1 Jul 2022 13:57:20 +0200 Subject: [PATCH] add our own endianness check because there is apparently no proper way to check for that at compile time --- .gitignore | 4 ++ {test_data => resources}/README.md | 0 resources/check_endianness.cpp | 66 ++++++++++++++++++ {test_data => resources}/servers.dat | Bin .../servers.dat_nbt_decoded.txt | 0 scripts/build.sh | 14 ++++ scripts/clean.sh | 2 + 7 files changed, 86 insertions(+) rename {test_data => resources}/README.md (100%) create mode 100644 resources/check_endianness.cpp rename {test_data => resources}/servers.dat (100%) rename {test_data => resources}/servers.dat_nbt_decoded.txt (100%) diff --git a/.gitignore b/.gitignore index 1c0877d..545812e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,9 @@ /dependencies/* !/dependencies/.placeholder +# ignore endianness check +/.endianness +/resources/check_endianness + # ignore nano's temp files *.swp diff --git a/test_data/README.md b/resources/README.md similarity index 100% rename from test_data/README.md rename to resources/README.md diff --git a/resources/check_endianness.cpp b/resources/check_endianness.cpp new file mode 100644 index 0000000..af81c6a --- /dev/null +++ b/resources/check_endianness.cpp @@ -0,0 +1,66 @@ +// Copyright , FOSS-VG Developers and Contributers +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, version 3. +// +// 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// version 3 along with this program. +// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html + +#include +#include + +int main() { + uint32_t* testInteger = new uint32_t; + *testInteger = 0x04030201; + auto data = reinterpret_cast(testInteger); + + if ( + *data == 0x01 && + *(data+1)==0x02 && + *(data+2)==0x03 && + *(data+3)==0x04 + ) { + std::cerr << "Little Endian" << std::endl; + std::cout << "#define FOSSVG_LITTLE_ENDIAN" << std::endl; + } else if ( + *data == 0x04 && + *(data+1)==0x03 && + *(data+2)==0x02 && + *(data+3)==0x01 + ) { + std::cerr << "Big Endian" << std::endl; + std::cout << "#define FOSSVG_BIG_ENDIAN" << std::endl; + } else if ( + *data == 0x03 && + *(data+1)==0x04 && + *(data+2)==0x01 && + *(data+3)==0x02 + ) { + std::cerr << "PDP Endian (Little Word)" << std::endl; + std::cout << "#define FOSSVG_ENDIAN_LITTLE_WORD /* PDP-11-style byte-swapped words */" << std::endl; + } else if ( + *data == 0x02 && + *(data+1)==0x01 && + *(data+2)==0x04 && + *(data+3)==0x03 + ) { + std::cerr << "Honeywell Endian (Big Word)" << std::endl; + std::cout << "#define FOSSVG_ENDIAN_BIG_WORD /* Honeywell-316-style byte-swapped words */" << std::endl; + + } else { + std::cerr << "Unknown Endianness" << std::endl; + std::cout << "#define FOSSVG_ENDIAN_UNKNOWN" << std::endl; + } + + delete data; + + std::cerr << "WARNING: This test may be unreliable. If you encounter issues, check the endianness of your machine and set it manually. Bi-endianness is not supported." << std::endl; + return 0; +} diff --git a/test_data/servers.dat b/resources/servers.dat similarity index 100% rename from test_data/servers.dat rename to resources/servers.dat diff --git a/test_data/servers.dat_nbt_decoded.txt b/resources/servers.dat_nbt_decoded.txt similarity index 100% rename from test_data/servers.dat_nbt_decoded.txt rename to resources/servers.dat_nbt_decoded.txt diff --git a/scripts/build.sh b/scripts/build.sh index 400e1e5..f363fcf 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -23,6 +23,20 @@ if [ -z "$CXXFLAGS" ]; then fi CXX_WITH_FLAGS="$CXX $CXXFLAGS" +# if unknown, figure out endianness +if [ -f .endianness ]; then + echo "Endianness known:" + cat .endianness +else + echo "Endianness unknown." + if [ ! -x resources/check_endianness ]; then + echo "Building endianness test." + $CXX_WITH_FLAGS -o resources/check_endianness resources/check_endianness.cpp + fi + echo "Probing endianness:" + resources/check_endianness > .endianness +fi + # `.cpp` files in src/lib will be automatically picked up and compiled into # dynamically linked libraries. echo "Building libs..." diff --git a/scripts/clean.sh b/scripts/clean.sh index fa04dc3..4aa184c 100755 --- a/scripts/clean.sh +++ b/scripts/clean.sh @@ -16,6 +16,8 @@ # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html rm -rv ./bin +rm -vf .endianness +rm -vf resources/check_endianness mkdir -v ./bin set -v echo -n "" > ./bin/.placeholder