|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, the SerenityOS developers. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <AK/Platform.h> |
| 30 | + |
| 31 | +namespace AK { |
| 32 | + |
| 33 | +template<typename T> |
| 34 | +ALWAYS_INLINE T convert_between_host_and_little_endian(T value) |
| 35 | +{ |
| 36 | +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 37 | + return value; |
| 38 | +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 39 | + if constexpr (sizeof(T) == 8) |
| 40 | + return __builtin_bswap64(value); |
| 41 | + if constexpr (sizeof(T) == 4) |
| 42 | + return __builtin_bswap32(value); |
| 43 | + if constexpr (sizeof(T) == 2) |
| 44 | + return __builtin_bswap16(value); |
| 45 | + if constexpr (sizeof(T) == 1) |
| 46 | + return value; |
| 47 | +#endif |
| 48 | +} |
| 49 | + |
| 50 | +template<typename T> |
| 51 | +ALWAYS_INLINE T convert_between_host_and_big_endian(T value) |
| 52 | +{ |
| 53 | +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 54 | + if constexpr (sizeof(T) == 8) |
| 55 | + return __builtin_bswap64(value); |
| 56 | + if constexpr (sizeof(T) == 4) |
| 57 | + return __builtin_bswap32(value); |
| 58 | + if constexpr (sizeof(T) == 2) |
| 59 | + return __builtin_bswap16(value); |
| 60 | + if constexpr (sizeof(T) == 1) |
| 61 | + return value; |
| 62 | +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 63 | + return value; |
| 64 | +#endif |
| 65 | +} |
| 66 | + |
| 67 | +template<typename T> |
| 68 | +ALWAYS_INLINE T convert_between_host_and_network_endian(T value) |
| 69 | +{ |
| 70 | + return convert_between_host_and_big_endian(value); |
| 71 | +} |
| 72 | + |
| 73 | +template<typename T> |
| 74 | +class [[gnu::packed]] LittleEndian |
| 75 | +{ |
| 76 | +public: |
| 77 | + LittleEndian() { } |
| 78 | + |
| 79 | + LittleEndian(T value) |
| 80 | + : m_value(convert_between_host_and_little_endian(value)) |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + operator T() const { return convert_between_host_and_little_endian(m_value); } |
| 85 | + |
| 86 | +private: |
| 87 | + T m_value { 0 }; |
| 88 | +}; |
| 89 | + |
| 90 | +template<typename T> |
| 91 | +class [[gnu::packed]] BigEndian |
| 92 | +{ |
| 93 | +public: |
| 94 | + BigEndian() { } |
| 95 | + |
| 96 | + BigEndian(T value) |
| 97 | + : m_value(convert_between_host_and_big_endian(value)) |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + operator T() const { return convert_between_host_and_big_endian(m_value); } |
| 102 | + |
| 103 | +private: |
| 104 | + T m_value { 0 }; |
| 105 | +}; |
| 106 | + |
| 107 | +template<typename T> |
| 108 | +using NetworkOrdered = BigEndian<T>; |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +using AK::BigEndian; |
| 113 | +using AK::LittleEndian; |
| 114 | +using AK::NetworkOrdered; |
0 commit comments