-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbyteordering.c
110 lines (97 loc) · 2.37 KB
/
byteordering.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "byteordering.h"
/**
* \addtogroup byteordering
*
* Architecture-dependent handling of byte-ordering.
*
* @{
*/
/**
* \file
* Byte-order handling implementation (license: GPLv2 or LGPLv2.1)
*
* \author Roland Riegel
*/
#if DOXYGEN || SWAP_NEEDED
/**
* \internal
* Swaps the bytes of a 16-bit integer.
*
* \param[in] i A 16-bit integer which to swap.
* \returns The swapped 16-bit integer.
*/
uint16_t swap16(uint16_t i)
{
return SWAP16(i);
}
/**
* \internal
* Swaps the bytes of a 32-bit integer.
*
* \param[in] i A 32-bit integer which to swap.
* \returns The swapped 32-bit integer.
*/
uint32_t swap32(uint32_t i)
{
return SWAP32(i);
}
#endif
/**
* Reads a 16-bit integer from memory in little-endian byte order.
*
* \param[in] p Pointer from where to read the integer.
* \returns The 16-bit integer read from memory.
*/
uint16_t read16(const uint8_t* p)
{
return (((uint16_t) p[1]) << 8) |
(((uint16_t) p[0]) << 0);
}
/**
* Reads a 32-bit integer from memory in little-endian byte order.
*
* \param[in] p Pointer from where to read the integer.
* \returns The 32-bit integer read from memory.
*/
uint32_t read32(const uint8_t* p)
{
return (((uint32_t) p[3]) << 24) |
(((uint32_t) p[2]) << 16) |
(((uint32_t) p[1]) << 8) |
(((uint32_t) p[0]) << 0);
}
/**
* Writes a 16-bit integer into memory in little-endian byte order.
*
* \param[in] p Pointer where to write the integer to.
* \param[in] i The 16-bit integer to write.
*/
void write16(uint8_t* p, uint16_t i)
{
p[1] = (uint8_t) ((i & 0xff00) >> 8);
p[0] = (uint8_t) ((i & 0x00ff) >> 0);
}
/**
* Writes a 32-bit integer into memory in little-endian byte order.
*
* \param[in] p Pointer where to write the integer to.
* \param[in] i The 32-bit integer to write.
*/
void write32(uint8_t* p, uint32_t i)
{
p[3] = (uint8_t) ((i & 0xff000000) >> 24);
p[2] = (uint8_t) ((i & 0x00ff0000) >> 16);
p[1] = (uint8_t) ((i & 0x0000ff00) >> 8);
p[0] = (uint8_t) ((i & 0x000000ff) >> 0);
}
/**
* @}
*/