|
| 1 | +// |
| 2 | +// Copyright (C) Wojciech Jarosz. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can |
| 4 | +// be found in the LICENSE.txt file. |
| 5 | +// |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <cstddef> |
| 10 | +#include <cstdint> |
| 11 | + |
| 12 | +//! Endianness indicator |
| 13 | +enum class Endian |
| 14 | +{ |
| 15 | + Little = 0, |
| 16 | + Intel = Little, |
| 17 | + Big = 1, |
| 18 | + Motorola = Big |
| 19 | +}; |
| 20 | + |
| 21 | +//! Returns 1 if architecture is little endian, 0 in case of big endian. |
| 22 | +inline bool is_little_endian() |
| 23 | +{ |
| 24 | + unsigned int x = 1; |
| 25 | + char *c = (char *)&x; |
| 26 | + return bool((int)*c); |
| 27 | +} |
| 28 | + |
| 29 | +inline Endian host_endian() { return is_little_endian() ? Endian::Little : Endian::Big; } |
| 30 | + |
| 31 | +template <typename T> |
| 32 | +inline T swap_bytes(T value) |
| 33 | +{ |
| 34 | +#if defined(_MSC_VER) |
| 35 | +#pragma intrinsic(_byteswap_ushort) |
| 36 | +#pragma intrinsic(_byteswap_ulong) |
| 37 | +#pragma intrinsic(_byteswap_uint64) |
| 38 | + |
| 39 | +#define byte_swap_16 _byteswap_ushort |
| 40 | +#define byte_swap_32 _byteswap_ulong |
| 41 | +#define byte_swap_64 _byteswap_uint64 |
| 42 | +#else |
| 43 | +#define byte_swap_16 __builtin_bswap16 |
| 44 | +#define byte_swap_32 __builtin_bswap32 |
| 45 | +#define byte_swap_64 __builtin_bswap64 |
| 46 | +#endif |
| 47 | + |
| 48 | + if constexpr (sizeof(T) == 1) |
| 49 | + { |
| 50 | + return value; |
| 51 | + } |
| 52 | + else if constexpr (sizeof(T) == 2) |
| 53 | + { |
| 54 | + uint16_t swapped = byte_swap_16(*reinterpret_cast<uint16_t *>(&value)); |
| 55 | + return *reinterpret_cast<T *>(&swapped); |
| 56 | + } |
| 57 | + else if constexpr (sizeof(T) == 4) |
| 58 | + { |
| 59 | + uint32_t swapped = byte_swap_32(*reinterpret_cast<uint32_t *>(&value)); |
| 60 | + return *reinterpret_cast<T *>(&swapped); |
| 61 | + } |
| 62 | + else if constexpr (sizeof(T) == 8) |
| 63 | + { |
| 64 | + uint64_t swapped = byte_swap_64(*reinterpret_cast<uint64_t *>(&value)); |
| 65 | + return *reinterpret_cast<T *>(&swapped); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8, |
| 70 | + "Unsupported type size for byte swapping."); |
| 71 | + } |
| 72 | +#undef byte_swap_16 |
| 73 | +#undef byte_swap_32 |
| 74 | +#undef byte_swap_64 |
| 75 | +} |
| 76 | + |
| 77 | +/*! |
| 78 | + * @brief Read a value of type T from a byte array and convert to host endianness. |
| 79 | + * |
| 80 | + * Reads sizeof(T) bytes from the given pointer and interprets them as type T, |
| 81 | + * performing byte swapping if the data endianness differs from the machine's. |
| 82 | + * |
| 83 | + * @tparam T The type to read (e.g., float, double, uint32_t) |
| 84 | + * @param ptr Pointer to the byte array to read from |
| 85 | + * @param data_endian The endianness of the data in the byte array |
| 86 | + * @return The value of type T in host endianness |
| 87 | + */ |
| 88 | +template <typename T> |
| 89 | +T read_as(const unsigned char *ptr, Endian data_endian) |
| 90 | +{ |
| 91 | + T value; |
| 92 | + memcpy(&value, ptr, sizeof(T)); |
| 93 | + |
| 94 | + // Only swap bytes if necessary |
| 95 | + if (data_endian != host_endian()) |
| 96 | + value = swap_bytes(value); |
| 97 | + |
| 98 | + return value; |
| 99 | +} |
| 100 | + |
| 101 | +/*! |
| 102 | + * @brief Read an array of values of type T from a byte array and convert to host endianness. |
| 103 | + * |
| 104 | + * Reads count * sizeof(T) bytes from the input pointer and interprets them as an array of type T, |
| 105 | + * performing byte swapping on each element if the data endianness differs from the machine's. |
| 106 | + * |
| 107 | + * @tparam T The type to read (e.g., float, double, uint32_t, int32_t) |
| 108 | + * @param output Pointer to the output array where results will be written |
| 109 | + * @param input Pointer to the input byte array to read from |
| 110 | + * @param count Number of elements to read |
| 111 | + * @param data_endian The endianness of the data in the input byte array |
| 112 | + */ |
| 113 | +template <typename T> |
| 114 | +void read_array(T *output, const unsigned char *input, size_t count, Endian data_endian) |
| 115 | +{ |
| 116 | + // First, copy all bytes at once |
| 117 | + memcpy(output, input, count * sizeof(T)); |
| 118 | + |
| 119 | + // Only swap bytes if necessary |
| 120 | + if (data_endian != host_endian()) |
| 121 | + for (size_t i = 0; i < count; i++) output[i] = swap_bytes(output[i]); |
| 122 | +} |
| 123 | + |
| 124 | +/*! |
| 125 | + * @brief Write a value of type T to a byte array with specified endianness. |
| 126 | + * |
| 127 | + * Writes sizeof(T) bytes to the given pointer, performing byte swapping if the target |
| 128 | + * endianness differs from the machine's endianness. |
| 129 | + * |
| 130 | + * @tparam T The type to write (e.g., float, double, uint32_t) |
| 131 | + * @param ptr Pointer to the byte array to write to |
| 132 | + * @param value The value to write |
| 133 | + * @param target_endian The desired endianness for the data in the byte array |
| 134 | + */ |
| 135 | +template <typename T> |
| 136 | +void write_as(unsigned char *ptr, T value, Endian target_endian) |
| 137 | +{ |
| 138 | + // Swap bytes if target endianness doesn't match machine endianness |
| 139 | + if (target_endian != host_endian()) |
| 140 | + value = swap_bytes(value); |
| 141 | + |
| 142 | + memcpy(ptr, &value, sizeof(T)); |
| 143 | +} |
| 144 | + |
| 145 | +/*! |
| 146 | + * @brief Write an array of values of type T to a byte array with specified endianness. |
| 147 | + * |
| 148 | + * Writes count * sizeof(T) bytes to the output pointer, performing byte swapping on each |
| 149 | + * element if the target endianness differs from the machine's. |
| 150 | + * |
| 151 | + * @tparam T The type to write (e.g., float, double, uint32_t, int32_t) |
| 152 | + * @param output Pointer to the output byte array to write to |
| 153 | + * @param input Pointer to the input array of values |
| 154 | + * @param count Number of elements to write |
| 155 | + * @param target_endian The desired endianness for the data in the output byte array |
| 156 | + */ |
| 157 | +template <typename T> |
| 158 | +void write_array(unsigned char *output, const T *input, size_t count, Endian target_endian) |
| 159 | +{ |
| 160 | + // First, copy all bytes at once |
| 161 | + memcpy(output, input, count * sizeof(T)); |
| 162 | + |
| 163 | + // Only swap bytes if necessary |
| 164 | + if (target_endian != host_endian()) |
| 165 | + { |
| 166 | + T *output_typed = reinterpret_cast<T *>(output); |
| 167 | + for (size_t i = 0; i < count; i++) output_typed[i] = swap_bytes(output_typed[i]); |
| 168 | + } |
| 169 | +} |
0 commit comments