forked from someone42/hardware-bitcoin-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endian.c
79 lines (70 loc) · 1.84 KB
/
endian.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
/** \file endian.c
*
* \brief Contains functions which perform endian-specific type conversion.
*
* This file is licensed as described by the file LICENCE.
*/
#include "common.h"
#include "endian.h"
/** Write 32 bit unsigned integer into a byte array in big-endian format.
* \param out The destination byte array. This must have space for at
* least 4 bytes.
* \param in The source integer.
*/
void writeU32BigEndian(uint8_t *out, uint32_t in)
{
out[0] = (uint8_t)(in >> 24);
out[1] = (uint8_t)(in >> 16);
out[2] = (uint8_t)(in >> 8);
out[3] = (uint8_t)in;
}
/** Write 32 bit unsigned integer into a byte array in little-endian format.
* \param out The destination byte array. This must have space for at
* least 4 bytes.
* \param in The source integer.
*/
void writeU32LittleEndian(uint8_t *out, uint32_t in)
{
out[0] = (uint8_t)in;
out[1] = (uint8_t)(in >> 8);
out[2] = (uint8_t)(in >> 16);
out[3] = (uint8_t)(in >> 24);
}
/** Read a 32 bit unsigned integer from a byte array in big-endian format.
* \param in The source byte array.
* \return The integer.
*/
uint32_t readU32BigEndian(uint8_t *in)
{
return ((uint32_t)in[0] << 24)
| ((uint32_t)in[1] << 16)
| ((uint32_t)in[2] << 8)
| ((uint32_t)in[3]);
}
/** Read a 32 bit unsigned integer from a byte array in little-endian format.
* \param in The source byte array.
* \return The integer.
*/
uint32_t readU32LittleEndian(uint8_t *in)
{
return ((uint32_t)in[0])
| ((uint32_t)in[1] << 8)
| ((uint32_t)in[2] << 16)
| ((uint32_t)in[3] << 24);
}
// No-one needs readU32BigEndian(), so it is not implemented.
/** Swap endianness of a 32 bit unsigned integer.
* \param v The integer to modify.
*/
void swapEndian(uint32_t *v)
{
uint8_t t;
uint8_t *r;
r = (uint8_t *)v;
t = r[0];
r[0] = r[3];
r[3] = t;
t = r[1];
r[1] = r[2];
r[2] = t;
}