freebsd-dev/contrib/libfido2/openbsd-compat/endian_win32.c
Ed Maste 0afa8e065e Import libfido2 at 'contrib/libfido2/'
git-subtree-dir: contrib/libfido2
git-subtree-mainline: d586c978b9
git-subtree-split: a58dee945a
2021-10-06 21:29:18 -04:00

52 lines
1.3 KiB
C

/*
* Copyright (c) 2020 Yubico AB. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
#include "openbsd-compat.h"
#if defined(_WIN32) && !defined(HAVE_ENDIAN_H)
/*
* Hopefully, if the endianness differs from the end result, the compiler
* optimizes these functions with some type of bswap instruction. Or,
* otherwise, to just return the input value unmodified. GCC and clang
* both does these optimization at least. This should be preferred over
* relying on some BYTE_ORDER macro, which may or may not be defined.
*/
uint32_t
htole32(uint32_t in)
{
uint32_t out = 0;
uint8_t *b = (uint8_t *)&out;
b[0] = (uint8_t)((in >> 0) & 0xff);
b[1] = (uint8_t)((in >> 8) & 0xff);
b[2] = (uint8_t)((in >> 16) & 0xff);
b[3] = (uint8_t)((in >> 24) & 0xff);
return (out);
}
uint64_t
htole64(uint64_t in)
{
uint64_t out = 0;
uint8_t *b = (uint8_t *)&out;
b[0] = (uint8_t)((in >> 0) & 0xff);
b[1] = (uint8_t)((in >> 8) & 0xff);
b[2] = (uint8_t)((in >> 16) & 0xff);
b[3] = (uint8_t)((in >> 24) & 0xff);
b[4] = (uint8_t)((in >> 32) & 0xff);
b[5] = (uint8_t)((in >> 40) & 0xff);
b[6] = (uint8_t)((in >> 48) & 0xff);
b[7] = (uint8_t)((in >> 56) & 0xff);
return (out);
}
#endif /* WIN32 && !HAVE_ENDIAN_H */