Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | eda78e6 | 2012-04-06 04:32:28 | [diff] [blame] | 5 | // This header defines cross-platform ByteSwap() implementations for 16, 32 and |
6 | // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to | ||||
7 | // the traditional ntohX() and htonX() functions. | ||||
8 | // Use the functions defined here rather than using the platform-specific | ||||
9 | // functions directly. | ||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 10 | |
11 | #ifndef BASE_SYS_BYTEORDER_H_ | ||||
12 | #define BASE_SYS_BYTEORDER_H_ | ||||
13 | |||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include <stdint.h> |
15 | |||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 16 | #include "base/numerics/byte_conversions.h" |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 17 | #include "build/build_config.h" |
18 | |||||
robert.bradford | 1a09b177 | 2016-06-17 23:26:11 | [diff] [blame] | 19 | #if defined(COMPILER_MSVC) |
20 | #include <stdlib.h> | ||||
21 | #endif | ||||
22 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 23 | namespace base { |
24 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 25 | // Converts the bytes in |x| from network to host order (endianness), and |
26 | // returns the result. | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 27 | inline constexpr uint16_t NetToHost16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 28 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 29 | return ByteSwap(x); |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 30 | #else |
31 | return x; | ||||
32 | #endif | ||||
33 | } | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 34 | inline constexpr uint32_t NetToHost32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 35 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 36 | return ByteSwap(x); |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 37 | #else |
38 | return x; | ||||
39 | #endif | ||||
40 | } | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 41 | inline constexpr uint64_t NetToHost64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 42 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 43 | return ByteSwap(x); |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 44 | #else |
45 | return x; | ||||
46 | #endif | ||||
47 | } | ||||
48 | |||||
49 | // Converts the bytes in |x| from host to network order (endianness), and | ||||
50 | // returns the result. | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 51 | inline constexpr uint16_t HostToNet16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 52 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 53 | return ByteSwap(x); |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 54 | #else |
55 | return x; | ||||
56 | #endif | ||||
57 | } | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 58 | inline constexpr uint32_t HostToNet32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 59 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 60 | return ByteSwap(x); |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 61 | #else |
62 | return x; | ||||
63 | #endif | ||||
64 | } | ||||
danakj | 590d1523 | 2024-02-08 17:17:44 | [diff] [blame] | 65 | inline constexpr uint64_t HostToNet64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 66 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
David Benjamin | fad2e08 | 2024-04-02 19:14:39 | [diff] [blame] | 67 | return ByteSwap(x); |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 68 | #else |
69 | return x; | ||||
70 | #endif | ||||
71 | } | ||||
72 | |||||
73 | } // namespace base | ||||
74 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 75 | #endif // BASE_SYS_BYTEORDER_H_ |