blob: 86a89aa6225d0d11b6ae9f2ba9201dde34c6c2ab [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]0e6f6192011-12-28 23:18:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eda78e62012-04-06 04:32:285// 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]0e6f6192011-12-28 23:18:2110
11#ifndef BASE_SYS_BYTEORDER_H_
12#define BASE_SYS_BYTEORDER_H_
13
avi9b6f42932015-12-26 22:15:1414#include <stdint.h>
15
danakj590d15232024-02-08 17:17:4416#include "base/numerics/byte_conversions.h"
[email protected]0e6f6192011-12-28 23:18:2117#include "build/build_config.h"
18
robert.bradford1a09b1772016-06-17 23:26:1119#if defined(COMPILER_MSVC)
20#include <stdlib.h>
21#endif
22
[email protected]0e6f6192011-12-28 23:18:2123namespace base {
24
[email protected]0e6f6192011-12-28 23:18:2125// Converts the bytes in |x| from network to host order (endianness), and
26// returns the result.
danakj590d15232024-02-08 17:17:4427inline constexpr uint16_t NetToHost16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3128#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3929 return ByteSwap(x);
[email protected]9eb7b11b2012-03-28 20:19:3130#else
31 return x;
32#endif
33}
danakj590d15232024-02-08 17:17:4434inline constexpr uint32_t NetToHost32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3135#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3936 return ByteSwap(x);
[email protected]9eb7b11b2012-03-28 20:19:3137#else
38 return x;
39#endif
40}
danakj590d15232024-02-08 17:17:4441inline constexpr uint64_t NetToHost64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:2142#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3943 return ByteSwap(x);
[email protected]0e6f6192011-12-28 23:18:2144#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.
danakj590d15232024-02-08 17:17:4451inline constexpr uint16_t HostToNet16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3152#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3953 return ByteSwap(x);
[email protected]9eb7b11b2012-03-28 20:19:3154#else
55 return x;
56#endif
57}
danakj590d15232024-02-08 17:17:4458inline constexpr uint32_t HostToNet32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3159#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3960 return ByteSwap(x);
[email protected]9eb7b11b2012-03-28 20:19:3161#else
62 return x;
63#endif
64}
danakj590d15232024-02-08 17:17:4465inline constexpr uint64_t HostToNet64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:2166#if defined(ARCH_CPU_LITTLE_ENDIAN)
David Benjaminfad2e082024-04-02 19:14:3967 return ByteSwap(x);
[email protected]0e6f6192011-12-28 23:18:2168#else
69 return x;
70#endif
71}
72
73} // namespace base
74
[email protected]0e6f6192011-12-28 23:18:2175#endif // BASE_SYS_BYTEORDER_H_