From 101ac1cd5045d2b153a3fc865909953f40a16d27 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 27 Jul 2025 15:02:25 +0200 Subject: [PATCH] wip: support big endian --- include/dwarfs/endian.h | 106 +++++++++++++++++++++++++++++++++++++++ include/dwarfs/fstypes.h | 9 ++-- 2 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 include/dwarfs/endian.h diff --git a/include/dwarfs/endian.h b/include/dwarfs/endian.h new file mode 100644 index 00000000..e213280e --- /dev/null +++ b/include/dwarfs/endian.h @@ -0,0 +1,106 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the “Software”), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace dwarfs { + +template +class boxed_endian { + public: + static_assert(Endian == std::endian::little || Endian == std::endian::big); + + constexpr boxed_endian() = default; + constexpr explicit boxed_endian(T v) noexcept + : raw_{swap(v)} {} + + constexpr operator T() const noexcept { return swap(raw_); } + + template + requires std::is_enum_v + constexpr explicit operator E() const noexcept { + return static_cast(swap(raw_)); + } + + constexpr T load() const noexcept { return swap(raw_); } + + constexpr boxed_endian& operator=(T v) noexcept { + raw_ = swap(v); + return *this; + } + + friend constexpr auto + operator<=>(boxed_endian lhs, boxed_endian rhs) noexcept { + return static_cast(lhs) <=> static_cast(rhs); + } + + friend constexpr bool + operator==(boxed_endian lhs, boxed_endian rhs) noexcept { + return static_cast(lhs) == static_cast(rhs); + } + + private: + T raw_{}; + + static constexpr T swap(T value) noexcept { + if constexpr (std::endian::native == Endian) { + return value; + } else if constexpr (sizeof(T) == 1) { + return value; + } else { +#if defined(__GNUC__) || defined(__clang__) + static_assert(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8); + if constexpr (sizeof(T) == 2) { + return __builtin_bswap16(value); + } else if constexpr (sizeof(T) == 4) { + return __builtin_bswap32(value); + } else if constexpr (sizeof(T) == 8) { + return __builtin_bswap64(value); + } +#else +#error "Unsupported compiler for byte swapping" +#endif + } + } +}; + +using uint16le_t = boxed_endian; +using uint32le_t = boxed_endian; +using uint64le_t = boxed_endian; + +static_assert(sizeof(uint16le_t) == 2); +static_assert(sizeof(uint32le_t) == 4); +static_assert(sizeof(uint64le_t) == 8); + +} // namespace dwarfs diff --git a/include/dwarfs/fstypes.h b/include/dwarfs/fstypes.h index 31bb593e..6b501851 100644 --- a/include/dwarfs/fstypes.h +++ b/include/dwarfs/fstypes.h @@ -39,6 +39,7 @@ #include // TODO: or the other way round? #include +#include namespace dwarfs { @@ -87,10 +88,10 @@ struct section_header_v2 { std::array // [8] SHA2-512/256 starting from next field sha2_512_256; // uint64_t xxh3_64; // [40] XXH3-64 starting from next field - uint32_t number; // [48] section number - uint16_t type; // [52] section type - uint16_t compression; // [54] compression - uint64_t length; // [56] length of section + uint32le_t number; // [48] section number + uint16le_t type; // [52] section type + uint16le_t compression; // [54] compression + uint64le_t length; // [56] length of section std::string to_string() const; void dump(std::ostream& os) const;