Path: blob/main/contrib/libfido2/openbsd-compat/endian_win32.c
39562 views
/*1* Copyright (c) 2020 Yubico AB. All rights reserved.2* Use of this source code is governed by a BSD-style3* license that can be found in the LICENSE file.4* SPDX-License-Identifier: BSD-2-Clause5*/67#include "openbsd-compat.h"89#if defined(_WIN32) && !defined(HAVE_ENDIAN_H)1011/*12* Hopefully, if the endianness differs from the end result, the compiler13* optimizes these functions with some type of bswap instruction. Or,14* otherwise, to just return the input value unmodified. GCC and clang15* both does these optimization at least. This should be preferred over16* relying on some BYTE_ORDER macro, which may or may not be defined.17*/1819uint32_t20htole32(uint32_t in)21{22uint32_t out = 0;23uint8_t *b = (uint8_t *)&out;2425b[0] = (uint8_t)((in >> 0) & 0xff);26b[1] = (uint8_t)((in >> 8) & 0xff);27b[2] = (uint8_t)((in >> 16) & 0xff);28b[3] = (uint8_t)((in >> 24) & 0xff);2930return (out);31}3233uint64_t34htole64(uint64_t in)35{36uint64_t out = 0;37uint8_t *b = (uint8_t *)&out;3839b[0] = (uint8_t)((in >> 0) & 0xff);40b[1] = (uint8_t)((in >> 8) & 0xff);41b[2] = (uint8_t)((in >> 16) & 0xff);42b[3] = (uint8_t)((in >> 24) & 0xff);43b[4] = (uint8_t)((in >> 32) & 0xff);44b[5] = (uint8_t)((in >> 40) & 0xff);45b[6] = (uint8_t)((in >> 48) & 0xff);46b[7] = (uint8_t)((in >> 56) & 0xff);4748return (out);49}5051#endif /* WIN32 && !HAVE_ENDIAN_H */525354