summaryrefslogtreecommitdiffstats
path: root/openbsd-compat/endian_win32.c
blob: 756c0cbdc8380a2947e0c95f1bdbbfcc924b014d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * 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.
 * SPDX-License-Identifier: BSD-2-Clause
 */

#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 */