summaryrefslogtreecommitdiff
path: root/src/commonlib/include/commonlib/endian.h
blob: 7b08eee7afb4d22804063e88cf393b6fbba9c869 (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2015 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef _COMMONLIB_ENDIAN_H_
#define _COMMONLIB_ENDIAN_H_

#include <stddef.h>
#include <stdint.h>
#include <string.h>

/* Endian agnostic functions working on single byte. */

static inline uint8_t read_ble8(const void *src)
{
	const uint8_t *s = src;
	return *s;
}

static inline uint8_t read_at_ble8(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_ble8(s);
}

static inline void write_ble8(void *dest, uint8_t val)
{
	*(uint8_t *)dest = val;
}

static inline void write_at_ble8(void *dest, uint8_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_ble8(d, val);
}

/* Big Endian functions. */

static inline uint8_t read_be8(const void *src)
{
	return read_ble8(src);
}

static inline uint8_t read_at_be8(const void *src, size_t offset)
{
	return read_at_ble8(src, offset);
}

static inline void write_be8(void *dest, uint8_t val)
{
	write_ble8(dest, val);
}

static inline void write_at_be8(void *dest, uint8_t val, size_t offset)
{
	write_at_ble8(dest, val, offset);
}

static inline uint16_t read_be16(const void *src)
{
	const uint8_t *s = src;
	return (((uint16_t)s[0]) << 8) | (((uint16_t)s[1]) << 0);
}

static inline uint16_t read_at_be16(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_be16(s);
}

static inline void write_be16(void *dest, uint16_t val)
{
	write_be8(dest, val >> 8);
	write_at_be8(dest, val >> 0, sizeof(uint8_t));
}

static inline void write_at_be16(void *dest, uint16_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_be16(d, val);
}

static inline uint32_t read_be32(const void *src)
{
	const uint8_t *s = src;
	return (((uint32_t)s[0]) << 24) | (((uint32_t)s[1]) << 16) |
		(((uint32_t)s[2]) << 8) | (((uint32_t)s[3]) << 0);
}

static inline uint32_t read_at_be32(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_be32(s);
}

static inline void write_be32(void *dest, uint32_t val)
{
	write_be16(dest, val >> 16);
	write_at_be16(dest, val >> 0, sizeof(uint16_t));
}

static inline void write_at_be32(void *dest, uint32_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_be32(d, val);
}

static inline uint64_t read_be64(const void *src)
{
	uint64_t val;
	val = read_be32(src);
	val <<= 32;
	val |= read_at_be32(src, sizeof(uint32_t));
	return val;
}

static inline uint64_t read_at_be64(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_be64(s);
}

static inline void write_be64(void *dest, uint64_t val)
{
	write_be32(dest, val >> 32);
	write_at_be32(dest, val >> 0, sizeof(uint32_t));
}

static inline void write_at_be64(void *dest, uint64_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_be64(d, val);
}

/* Little Endian functions. */

static inline uint8_t read_le8(const void *src)
{
	return read_ble8(src);
}

static inline uint8_t read_at_le8(const void *src, size_t offset)
{
	return read_at_ble8(src, offset);
}

static inline void write_le8(void *dest, uint8_t val)
{
	write_ble8(dest, val);
}

static inline void write_at_le8(void *dest, uint8_t val, size_t offset)
{
	write_at_ble8(dest, val, offset);
}

static inline uint16_t read_le16(const void *src)
{
	const uint8_t *s = src;
	return (((uint16_t)s[1]) << 8) | (((uint16_t)s[0]) << 0);
}

static inline uint16_t read_at_le16(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_le16(s);
}

static inline void write_le16(void *dest, uint16_t val)
{
	write_le8(dest, val >> 0);
	write_at_le8(dest, val >> 8, sizeof(uint8_t));
}

static inline void write_at_le16(void *dest, uint16_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_le16(d, val);
}

static inline uint32_t read_le32(const void *src)
{
	const uint8_t *s = src;
	return (((uint32_t)s[3]) << 24) | (((uint32_t)s[2]) << 16) |
		(((uint32_t)s[1]) << 8) | (((uint32_t)s[0]) << 0);
}

static inline uint32_t read_at_le32(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_le32(s);
}

static inline void write_le32(void *dest, uint32_t val)
{
	write_le16(dest, val >> 0);
	write_at_le16(dest, val >> 16, sizeof(uint16_t));
}

static inline void write_at_le32(void *dest, uint32_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_le32(d, val);
}

static inline uint64_t read_le64(const void *src)
{
	uint64_t val;
	val = read_at_le32(src, sizeof(uint32_t));
	val <<= 32;
	val |= read_le32(src);
	return val;
}

static inline uint64_t read_at_le64(const void *src, size_t offset)
{
	const uint8_t *s = src;
	s += offset;
	return read_le64(s);
}

static inline void write_le64(void *dest, uint64_t val)
{
	write_le32(dest, val >> 0);
	write_at_le32(dest, val >> 32, sizeof(uint32_t));
}

static inline void write_at_le64(void *dest, uint64_t val, size_t offset)
{
	uint8_t *d = dest;
	d += offset;
	write_le64(d, val);
}

static inline void zero_n(void *dest, size_t n)
{
	memset(dest, 0, n);
}

#endif