wip: support big endian

This commit is contained in:
Marcus Holland-Moritz 2025-07-27 15:02:25 +02:00
parent 8330736b3b
commit 101ac1cd50
2 changed files with 111 additions and 4 deletions

106
include/dwarfs/endian.h Normal file
View File

@ -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 <bit>
#include <compare>
#include <concepts>
#include <cstdint>
#include <type_traits>
namespace dwarfs {
template <std::unsigned_integral T, std::endian Endian>
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 <typename E>
requires std::is_enum_v<E>
constexpr explicit operator E() const noexcept {
return static_cast<E>(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<T>(lhs) <=> static_cast<T>(rhs);
}
friend constexpr bool
operator==(boxed_endian lhs, boxed_endian rhs) noexcept {
return static_cast<T>(lhs) == static_cast<T>(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<uint16_t, std::endian::little>;
using uint32le_t = boxed_endian<uint32_t, std::endian::little>;
using uint64le_t = boxed_endian<uint64_t, std::endian::little>;
static_assert(sizeof(uint16le_t) == 2);
static_assert(sizeof(uint32le_t) == 4);
static_assert(sizeof(uint64le_t) == 8);
} // namespace dwarfs

View File

@ -39,6 +39,7 @@
#include <dwarfs/block_compressor.h> // TODO: or the other way round?
#include <dwarfs/checksum.h>
#include <dwarfs/endian.h>
namespace dwarfs {
@ -87,10 +88,10 @@ struct section_header_v2 {
std::array<uint8_t, 32> // [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;