-
Notifications
You must be signed in to change notification settings - Fork 16
/
endianm.c
134 lines (121 loc) · 3.5 KB
/
endianm.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
This file is Copyright © 1999-2005 André Majorel,
Copyright © 2006-2021 contributors to the DeuTex project.
Those functions allow to read little-endian and big-endian integers
from memory regardless of the endianness of the CPU.
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "deutex.h"
#include "endianm.h"
/*
* read_i16_le
* Read a little-endian 16-bit signed integer from memory area
* pointed to by <ptr>.
*/
void read_i16_le(const void *ptr, i16 * buf)
{
*buf = ((const unsigned char *) ptr)[0]
| (((const unsigned char *) ptr)[1] << 8);
}
/*
* read_i32_le
* Read a little-endian 32-bit signed integer from memory area
* pointed to by <ptr>.
*/
void read_i32_le(const void *ptr, i32 * buf)
{
*buf = ((const unsigned char *) ptr)[0]
| ((u16) ((const unsigned char *) ptr)[1] << 8)
| ((i32) ((const unsigned char *) ptr)[2] << 16)
| ((i32) ((const unsigned char *) ptr)[3] << 24);
}
/*
* read_i32_be
* Read a big-endian 32-bit signed integer from memory area
* pointed to by <ptr>.
*/
void read_i32_be(const void *ptr, i32 * buf)
{
*buf = ((i32) ((const unsigned char *) ptr)[0] << 24)
| ((i32) ((const unsigned char *) ptr)[1] << 16)
| ((u16) ((const unsigned char *) ptr)[2] << 8)
| (((const unsigned char *) ptr)[3]);
}
/*
* peek_i16_le
* Read a little-endian 16-bit signed integer from memory area
* pointed to by <ptr>.
*/
i16 peek_i16_le(const void *ptr)
{
return ((const unsigned char *) ptr)[0]
| (((const unsigned char *) ptr)[1] << 8);
}
/*
* peek_u16_le
* Read a little-endian 16-bit unsigned integer from memory area
* pointed to by <ptr>.
*/
u16 peek_u16_le(const void *ptr)
{
return ((const unsigned char *) ptr)[0]
| (((const unsigned char *) ptr)[1] << 8);
}
/*
* peek_i32_be
* Read a big-endian 32-bit signed integer from memory area
* pointed to by <ptr>.
*/
i32 peek_i32_be(const void *ptr)
{
return ((i32) (((const unsigned char *) ptr)[0]) << 24)
| ((i32) (((const unsigned char *) ptr)[1]) << 16)
| ((u16) (((const unsigned char *) ptr)[2]) << 8)
| ((const unsigned char *) ptr)[3];
}
/*
* peek_i32_le
* Read a little-endian 32-bit signed integer from memory area
* pointed to by <ptr>.
*/
i32 peek_i32_le(const void *ptr)
{
return ((const unsigned char *) ptr)[0]
| ((u16) ((const unsigned char *) ptr)[1] << 8)
| ((i32) ((const unsigned char *) ptr)[2] << 16)
| ((i32) ((const unsigned char *) ptr)[3] << 24);
}
/*
* write_i16_le
* Write a little-endian 16-bit signed integer to memory area
* pointed to by <ptr>.
*/
void write_i16_le(void *ptr, i16 val)
{
((unsigned char *) ptr)[0] = val;
((unsigned char *) ptr)[1] = val >> 8;
}
/*
* write_i32_be
* Write a big-endian 32-bit signed integer to memory area
* pointed to by <ptr>.
*/
void write_i32_be(void *ptr, i32 val)
{
((unsigned char *) ptr)[0] = val >> 24;
((unsigned char *) ptr)[1] = val >> 16;
((unsigned char *) ptr)[2] = val >> 8;
((unsigned char *) ptr)[3] = val;
}
/*
* write_i32_le
* Write a little-endian 32-bit signed integer to memory area
* pointed to by <ptr>.
*/
void write_i32_le(void *ptr, i32 val)
{
((unsigned char *) ptr)[0] = val;
((unsigned char *) ptr)[1] = val >> 8;
((unsigned char *) ptr)[2] = val >> 16;
((unsigned char *) ptr)[3] = val >> 24;
}