Skip to content
File

Blob: src/workerd/api/crypto/endianness.c++

3.1 KB
1// Copyright (c) 2017-2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#include "endianness.h"
6 
7#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
8 
9#define __WINDOWS__
10 
11#endif
12 
13#if defined(__linux__) || defined(__CYGWIN__)
14 
15#include <endian.h>
16 
17#elifdef __APPLE__
18 
19#include <libkern/OSByteOrder.h>
20 
21uint16_t htobe16(uint16_t x) {
22 return OSSwapHostToBigInt16(x);
23}
24uint16_t htole16(uint16_t x) {
25 return OSSwapHostToLittleInt16(x);
26}
27uint16_t be16toh(uint16_t x) {
28 return OSSwapBigToHostInt16(x);
29}
30uint16_t le16toh(uint16_t x) {
31 return OSSwapLittleToHostInt16(x);
32}
33 
34uint32_t htobe32(uint32_t x) {
35 return OSSwapHostToBigInt32(x);
36}
37uint32_t htole32(uint32_t x) {
38 return OSSwapHostToLittleInt32(x);
39}
40uint32_t be32toh(uint32_t x) {
41 return OSSwapBigToHostInt32(x);
42}
43uint32_t le32toh(uint32_t x) {
44 return OSSwapLittleToHostInt32(x);
45}
46 
47uint64_t htobe64(uint64_t x) {
48 return OSSwapHostToBigInt64(x);
49}
50uint64_t htole64(uint64_t x) {
51 return OSSwapHostToLittleInt64(x);
52}
53uint64_t be64toh(uint64_t x) {
54 return OSSwapBigToHostInt64(x);
55}
56uint64_t le64toh(uint64_t x) {
57 return OSSwapLittleToHostInt64(x);
58}
59 
60#elifdef __OpenBSD__
61 
62#include <sys/endian.h>
63 
64#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
65 
66#include <sys/endian.h>
67 
68uint16_t be16toh(uint16_t x) {
69 return betoh16(x);
70}
71uint16_t le16toh(uint16_t x) {
72 return letoh16(x);
73}
74 
75uint32_t be32toh(uint32_t x) {
76 return betoh32(x);
77}
78uint32_t le32toh(uint32_t x) {
79 return letoh32(x);
80}
81 
82uint64_t be64toh(uint64_t x) {
83 return betoh64(x);
84}
85uint64_t le64toh(uint64_t x) {
86 return letoh64(x);
87}
88 
89#elifdef __WINDOWS__
90 
91#include <winsock2.h>
92 
93#if BYTE_ORDER == LITTLE_ENDIAN
94 
95uint16_t htobe16(uint16_t x) {
96 return htons(x);
97}
98uint16_t htole16(uint16_t x) {
99 return x;
100}
101uint16_t be16toh(uint16_t x) {
102 return ntohs(x);
103}
104uint16_t le16toh(uint16_t x) {
105 return x;
106}
107 
108uint32_t htobe32(uint32_t x) {
109 return htonl(x);
110}
111uint32_t htole32(uint32_t x) {
112 return x;
113}
114uint32_t be32toh(uint32_t x) {
115 return ntohl(x);
116}
117uint32_t le32toh(uint32_t x) {
118 return x;
119}
120 
121uint64_t htobe64(uint64_t x) {
122 return htonll(x);
123}
124uint64_t htole64(uint64_t x) {
125 return x;
126}
127uint64_t be64toh(uint64_t x) {
128 return ntohll(x);
129}
130uint64_t le64toh(uint64_t x) {
131 return x;
132}
133 
134#elif BYTE_ORDER == BIG_ENDIAN
135 
136/* that would be xbox 360 */
137uint16_t htobe16(uint16_t x) {
138 return x;
139}
140uint16_t htole16(uint16_t x) {
141 return __builtin_bswap16(x);
142}
143uint16_t be16toh(uint16_t x) {
144 return x;
145}
146uint16_t le16toh(uint16_t x) {
147 return __builtin_bswap16(x);
148}
149 
150uint32_t htobe32(uint32_t x) {
151 return x;
152}
153uint32_t htole32(uint32_t x) {
154 return __builtin_bswap32(x);
155}
156uint32_t be32toh(uint32_t x) {
157 return x;
158}
159uint32_t le32toh(uint32_t x) {
160 return __builtin_bswap32(x);
161}
162 
163uint64_t htobe64(uint64_t x) {
164 return x;
165}
166uint64_t htole64(uint64_t x) {
167 return __builtin_bswap64(x);
168}
169uint64_t be64toh(uint64_t x) {
170 return x;
171}
172uint64_t le64toh(uint64_t x) {
173 return __builtin_bswap64(x);
174}
175 
176#else
177 
178#error byte order not supported
179 
180#endif
181 
182#else
183 
184#error platform not supported
185 
186#endif