File indexing completed on 2026-01-07 10:14:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef OPENSSL_BYTEORDER_H
0011 # define OPENSSL_BYTEORDER_H
0012 # pragma once
0013
0014 # include <openssl/e_os2.h>
0015 # include <string.h>
0016
0017
0018
0019
0020
0021
0022
0023 # if defined(_MSC_VER) && _MSC_VER>=1300
0024 # include <stdlib.h>
0025 # pragma intrinsic(_byteswap_ushort)
0026 # pragma intrinsic(_byteswap_ulong)
0027 # pragma intrinsic(_byteswap_uint64)
0028 # define OSSL_HTOBE16(x) _byteswap_ushort(x)
0029 # define OSSL_HTOBE32(x) _byteswap_ulong(x)
0030 # define OSSL_HTOBE64(x) _byteswap_uint64(x)
0031 # define OSSL_BE16TOH(x) _byteswap_ushort(x)
0032 # define OSSL_BE32TOH(x) _byteswap_ulong(x)
0033 # define OSSL_BE64TOH(x) _byteswap_uint64(x)
0034 # define OSSL_HTOLE16(x) (x)
0035 # define OSSL_HTOLE32(x) (x)
0036 # define OSSL_HTOLE64(x) (x)
0037 # define OSSL_LE16TOH(x) (x)
0038 # define OSSL_LE32TOH(x) (x)
0039 # define OSSL_LE64TOH(x) (x)
0040
0041 # elif defined(__GLIBC__) && defined(__GLIBC_PREREQ)
0042 # if (__GLIBC_PREREQ(2, 19)) && defined(_DEFAULT_SOURCE)
0043 # include <endian.h>
0044 # define OSSL_HTOBE16(x) htobe16(x)
0045 # define OSSL_HTOBE32(x) htobe32(x)
0046 # define OSSL_HTOBE64(x) htobe64(x)
0047 # define OSSL_BE16TOH(x) be16toh(x)
0048 # define OSSL_BE32TOH(x) be32toh(x)
0049 # define OSSL_BE64TOH(x) be64toh(x)
0050 # define OSSL_HTOLE16(x) htole16(x)
0051 # define OSSL_HTOLE32(x) htole32(x)
0052 # define OSSL_HTOLE64(x) htole64(x)
0053 # define OSSL_LE16TOH(x) le16toh(x)
0054 # define OSSL_LE32TOH(x) le32toh(x)
0055 # define OSSL_LE64TOH(x) le64toh(x)
0056 # endif
0057
0058 # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
0059 # if defined(__OpenBSD__)
0060 # include <sys/types.h>
0061 # else
0062 # include <sys/endian.h>
0063 # endif
0064 # define OSSL_HTOBE16(x) htobe16(x)
0065 # define OSSL_HTOBE32(x) htobe32(x)
0066 # define OSSL_HTOBE64(x) htobe64(x)
0067 # define OSSL_BE16TOH(x) be16toh(x)
0068 # define OSSL_BE32TOH(x) be32toh(x)
0069 # define OSSL_BE64TOH(x) be64toh(x)
0070 # define OSSL_HTOLE16(x) htole16(x)
0071 # define OSSL_HTOLE32(x) htole32(x)
0072 # define OSSL_HTOLE64(x) htole64(x)
0073 # define OSSL_LE16TOH(x) le16toh(x)
0074 # define OSSL_LE32TOH(x) le32toh(x)
0075 # define OSSL_LE64TOH(x) le64toh(x)
0076
0077 # elif defined(__APPLE__)
0078 # include <libkern/OSByteOrder.h>
0079 # define OSSL_HTOBE16(x) OSSwapHostToBigInt16(x)
0080 # define OSSL_HTOBE32(x) OSSwapHostToBigInt32(x)
0081 # define OSSL_HTOBE64(x) OSSwapHostToBigInt64(x)
0082 # define OSSL_BE16TOH(x) OSSwapBigToHostInt16(x)
0083 # define OSSL_BE32TOH(x) OSSwapBigToHostInt32(x)
0084 # define OSSL_BE64TOH(x) OSSwapBigToHostInt64(x)
0085 # define OSSL_HTOLE16(x) OSSwapHostToLittleInt16(x)
0086 # define OSSL_HTOLE32(x) OSSwapHostToLittleInt32(x)
0087 # define OSSL_HTOLE64(x) OSSwapHostToLittleInt64(x)
0088 # define OSSL_LE16TOH(x) OSSwapLittleToHostInt16(x)
0089 # define OSSL_LE32TOH(x) OSSwapLittleToHostInt32(x)
0090 # define OSSL_LE64TOH(x) OSSwapLittleToHostInt64(x)
0091
0092 # endif
0093
0094 static ossl_inline ossl_unused unsigned char *
0095 OPENSSL_store_u16_le(unsigned char *out, uint16_t val)
0096 {
0097 # ifdef OSSL_HTOLE16
0098 uint16_t t = OSSL_HTOLE16(val);
0099
0100 memcpy(out, (unsigned char *)&t, 2);
0101 return out + 2;
0102 # else
0103 *out++ = (val & 0xff);
0104 *out++ = (val >> 8) & 0xff;
0105 return out;
0106 # endif
0107 }
0108
0109 static ossl_inline ossl_unused unsigned char *
0110 OPENSSL_store_u16_be(unsigned char *out, uint16_t val)
0111 {
0112 # ifdef OSSL_HTOBE16
0113 uint16_t t = OSSL_HTOBE16(val);
0114
0115 memcpy(out, (unsigned char *)&t, 2);
0116 return out + 2;
0117 # else
0118 *out++ = (val >> 8) & 0xff;
0119 *out++ = (val & 0xff);
0120 return out;
0121 # endif
0122 }
0123
0124 static ossl_inline ossl_unused unsigned char *
0125 OPENSSL_store_u32_le(unsigned char *out, uint32_t val)
0126 {
0127 # ifdef OSSL_HTOLE32
0128 uint32_t t = OSSL_HTOLE32(val);
0129
0130 memcpy(out, (unsigned char *)&t, 4);
0131 return out + 4;
0132 # else
0133 *out++ = (val & 0xff);
0134 *out++ = (val >> 8) & 0xff;
0135 *out++ = (val >> 16) & 0xff;
0136 *out++ = (val >> 24) & 0xff;
0137 return out;
0138 # endif
0139 }
0140
0141 static ossl_inline ossl_unused unsigned char *
0142 OPENSSL_store_u32_be(unsigned char *out, uint32_t val)
0143 {
0144 # ifdef OSSL_HTOBE32
0145 uint32_t t = OSSL_HTOBE32(val);
0146
0147 memcpy(out, (unsigned char *)&t, 4);
0148 return out + 4;
0149 # else
0150 *out++ = (val >> 24) & 0xff;
0151 *out++ = (val >> 16) & 0xff;
0152 *out++ = (val >> 8) & 0xff;
0153 *out++ = (val & 0xff);
0154 return out;
0155 # endif
0156 }
0157
0158 static ossl_inline ossl_unused unsigned char *
0159 OPENSSL_store_u64_le(unsigned char *out, uint64_t val)
0160 {
0161 # ifdef OSSL_HTOLE64
0162 uint64_t t = OSSL_HTOLE64(val);
0163
0164 memcpy(out, (unsigned char *)&t, 8);
0165 return out + 8;
0166 # else
0167 *out++ = (val & 0xff);
0168 *out++ = (val >> 8) & 0xff;
0169 *out++ = (val >> 16) & 0xff;
0170 *out++ = (val >> 24) & 0xff;
0171 *out++ = (val >> 32) & 0xff;
0172 *out++ = (val >> 40) & 0xff;
0173 *out++ = (val >> 48) & 0xff;
0174 *out++ = (val >> 56) & 0xff;
0175 return out;
0176 # endif
0177 }
0178
0179 static ossl_inline ossl_unused unsigned char *
0180 OPENSSL_store_u64_be(unsigned char *out, uint64_t val)
0181 {
0182 # ifdef OSSL_HTOLE64
0183 uint64_t t = OSSL_HTOBE64(val);
0184
0185 memcpy(out, (unsigned char *)&t, 8);
0186 return out + 8;
0187 # else
0188 *out++ = (val >> 56) & 0xff;
0189 *out++ = (val >> 48) & 0xff;
0190 *out++ = (val >> 40) & 0xff;
0191 *out++ = (val >> 32) & 0xff;
0192 *out++ = (val >> 24) & 0xff;
0193 *out++ = (val >> 16) & 0xff;
0194 *out++ = (val >> 8) & 0xff;
0195 *out++ = (val & 0xff);
0196 return out;
0197 # endif
0198 }
0199
0200 static ossl_inline ossl_unused const unsigned char *
0201 OPENSSL_load_u16_le(uint16_t *val, const unsigned char *in)
0202 {
0203 # ifdef OSSL_LE16TOH
0204 uint16_t t;
0205
0206 memcpy((unsigned char *)&t, in, 2);
0207 *val = OSSL_LE16TOH(t);
0208 return in + 2;
0209 # else
0210 uint16_t b0 = *in++;
0211 uint16_t b1 = *in++;
0212
0213 *val = b0 | (b1 << 8);
0214 return in;
0215 #endif
0216 }
0217
0218 static ossl_inline ossl_unused const unsigned char *
0219 OPENSSL_load_u16_be(uint16_t *val, const unsigned char *in)
0220 {
0221 # ifdef OSSL_LE16TOH
0222 uint16_t t;
0223
0224 memcpy((unsigned char *)&t, in, 2);
0225 *val = OSSL_BE16TOH(t);
0226 return in + 2;
0227 # else
0228 uint16_t b1 = *in++;
0229 uint16_t b0 = *in++;
0230
0231 *val = b0 | (b1 << 8);
0232 return in;
0233 #endif
0234 }
0235
0236 static ossl_inline ossl_unused const unsigned char *
0237 OPENSSL_load_u32_le(uint32_t *val, const unsigned char *in)
0238 {
0239 # ifdef OSSL_LE32TOH
0240 uint32_t t;
0241
0242 memcpy((unsigned char *)&t, in, 4);
0243 *val = OSSL_LE32TOH(t);
0244 return in + 4;
0245 # else
0246 uint32_t b0 = *in++;
0247 uint32_t b1 = *in++;
0248 uint32_t b2 = *in++;
0249 uint32_t b3 = *in++;
0250
0251 *val = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
0252 return in;
0253 #endif
0254 }
0255
0256 static ossl_inline ossl_unused const unsigned char *
0257 OPENSSL_load_u32_be(uint32_t *val, const unsigned char *in)
0258 {
0259 # ifdef OSSL_LE32TOH
0260 uint32_t t;
0261
0262 memcpy((unsigned char *)&t, in, 4);
0263 *val = OSSL_BE32TOH(t);
0264 return in + 4;
0265 # else
0266 uint32_t b3 = *in++;
0267 uint32_t b2 = *in++;
0268 uint32_t b1 = *in++;
0269 uint32_t b0 = *in++;
0270
0271 *val = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
0272 return in;
0273 #endif
0274 }
0275
0276 static ossl_inline ossl_unused const unsigned char *
0277 OPENSSL_load_u64_le(uint64_t *val, const unsigned char *in)
0278 {
0279 # ifdef OSSL_LE64TOH
0280 uint64_t t;
0281
0282 memcpy((unsigned char *)&t, in, 8);
0283 *val = OSSL_LE64TOH(t);
0284 return in + 8;
0285 # else
0286 uint64_t b0 = *in++;
0287 uint64_t b1 = *in++;
0288 uint64_t b2 = *in++;
0289 uint64_t b3 = *in++;
0290 uint64_t b4 = *in++;
0291 uint64_t b5 = *in++;
0292 uint64_t b6 = *in++;
0293 uint64_t b7 = *in++;
0294
0295 *val = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
0296 | (b4 << 32) | (b5 << 40) | (b6 << 48) | (b7 << 56);
0297 return in;
0298 #endif
0299 }
0300
0301 static ossl_inline ossl_unused const unsigned char *
0302 OPENSSL_load_u64_be(uint64_t *val, const unsigned char *in)
0303 {
0304 # ifdef OSSL_LE64TOH
0305 uint64_t t;
0306
0307 memcpy((unsigned char *)&t, in, 8);
0308 *val = OSSL_BE64TOH(t);
0309 return in + 8;
0310 # else
0311 uint64_t b7 = *in++;
0312 uint64_t b6 = *in++;
0313 uint64_t b5 = *in++;
0314 uint64_t b4 = *in++;
0315 uint64_t b3 = *in++;
0316 uint64_t b2 = *in++;
0317 uint64_t b1 = *in++;
0318 uint64_t b0 = *in++;
0319
0320 *val = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
0321 | (b4 << 32) | (b5 << 40) | (b6 << 48) | (b7 << 56);
0322 return in;
0323 #endif
0324 }
0325
0326 # undef OSSL_HTOBE16
0327 # undef OSSL_HTOBE32
0328 # undef OSSL_HTOBE64
0329 # undef OSSL_BE16TOH
0330 # undef OSSL_BE32TOH
0331 # undef OSSL_BE64TOH
0332 # undef OSSL_HTOLE16
0333 # undef OSSL_HTOLE32
0334 # undef OSSL_HTOLE64
0335 # undef OSSL_LE16TOH
0336 # undef OSSL_LE32TOH
0337 # undef OSSL_LE64TOH
0338
0339 #endif