Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | |
| 9 | #include "common/webm_endian.h" |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Swaps unsigned 32 bit values to big endian if needed. Returns |value| |
| 16 | // unmodified if architecture is big endian. Returns swapped bytes of |value| |
| 17 | // if architecture is little endian. Returns 0 otherwise. |
| 18 | uint32_t swap32_check_little_endian(uint32_t value) { |
| 19 | // Check endianness. |
| 20 | union { |
| 21 | uint32_t val32; |
| 22 | uint8_t c[4]; |
| 23 | } check; |
| 24 | check.val32 = 0x01234567U; |
| 25 | |
| 26 | // Check for big endian. |
| 27 | if (check.c[3] == 0x67) |
| 28 | return value; |
| 29 | |
| 30 | // Check for not little endian. |
| 31 | if (check.c[0] != 0x67) |
| 32 | return 0; |
| 33 | |
| 34 | return value << 24 | ((value << 8) & 0x0000FF00U) | |
| 35 | ((value >> 8) & 0x00FF0000U) | value >> 24; |
| 36 | } |
| 37 | |
| 38 | // Swaps unsigned 64 bit values to big endian if needed. Returns |value| |
| 39 | // unmodified if architecture is big endian. Returns swapped bytes of |value| |
| 40 | // if architecture is little endian. Returns 0 otherwise. |
| 41 | uint64_t swap64_check_little_endian(uint64_t value) { |
| 42 | // Check endianness. |
| 43 | union { |
| 44 | uint64_t val64; |
| 45 | uint8_t c[8]; |
| 46 | } check; |
| 47 | check.val64 = 0x0123456789ABCDEFULL; |
| 48 | |
| 49 | // Check for big endian. |
| 50 | if (check.c[7] == 0xEF) |
| 51 | return value; |
| 52 | |
| 53 | // Check for not little endian. |
| 54 | if (check.c[0] != 0xEF) |
| 55 | return 0; |
| 56 | |
| 57 | return value << 56 | ((value << 40) & 0x00FF000000000000ULL) | |
| 58 | ((value << 24) & 0x0000FF0000000000ULL) | |
| 59 | ((value << 8) & 0x000000FF00000000ULL) | |
| 60 | ((value >> 8) & 0x00000000FF000000ULL) | |
| 61 | ((value >> 24) & 0x0000000000FF0000ULL) | |
| 62 | ((value >> 40) & 0x000000000000FF00ULL) | value >> 56; |
| 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | namespace libwebm { |
| 68 | |
| 69 | uint32_t host_to_bigendian(uint32_t value) { |
| 70 | return swap32_check_little_endian(value); |
| 71 | } |
| 72 | |
| 73 | uint32_t bigendian_to_host(uint32_t value) { |
| 74 | return swap32_check_little_endian(value); |
| 75 | } |
| 76 | |
| 77 | uint64_t host_to_bigendian(uint64_t value) { |
| 78 | return swap64_check_little_endian(value); |
| 79 | } |
| 80 | |
| 81 | uint64_t bigendian_to_host(uint64_t value) { |
| 82 | return swap64_check_little_endian(value); |
| 83 | } |
| 84 | |
| 85 | } // namespace libwebm |