summaryrefslogtreecommitdiff
path: root/src/utils/endian.h
blob: 8a4760b32f7efc844429a0ea81c0092e8cf47005 (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
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef parserutils_endian_h_
#define parserutils_endian_h_

static inline bool endian_host_is_le(void)
{
	static uint32_t magic = 0x10000002;

	return (((uint8_t *) &magic)[0] == 0x02);
}

static inline uint32_t endian_swap(uint32_t val)
{
	return ((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) |
		((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24);
}

static inline uint32_t endian_host_to_big(uint32_t host)
{
	if (endian_host_is_le())
		return endian_swap(host);

	return host;
}

static inline uint32_t endian_big_to_host(uint32_t big)
{
	if (endian_host_is_le())
		return endian_swap(big);

	return big;
}

#endif