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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Assertions.h"
#include "mozilla/EndianUtils.h"
#include <stddef.h>
using mozilla::BigEndian;
using mozilla::LittleEndian;
using mozilla::NativeEndian;
template<typename T>
void
TestSingleSwap(T aValue, T aSwappedValue)
{
#if MOZ_LITTLE_ENDIAN
MOZ_RELEASE_ASSERT(NativeEndian::swapToBigEndian(aValue) == aSwappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromBigEndian(aValue) == aSwappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapToNetworkOrder(aValue) == aSwappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromNetworkOrder(aValue) ==
aSwappedValue);
#else
MOZ_RELEASE_ASSERT(NativeEndian::swapToLittleEndian(aValue) == aSwappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromLittleEndian(aValue) ==
aSwappedValue);
#endif
}
template<typename T>
void
TestSingleNoSwap(T aValue, T aUnswappedValue)
{
#if MOZ_LITTLE_ENDIAN
MOZ_RELEASE_ASSERT(NativeEndian::swapToLittleEndian(aValue) ==
aUnswappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromLittleEndian(aValue) ==
aUnswappedValue);
#else
MOZ_RELEASE_ASSERT(NativeEndian::swapToBigEndian(aValue) == aUnswappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromBigEndian(aValue) == aUnswappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapToNetworkOrder(aValue) ==
aUnswappedValue);
MOZ_RELEASE_ASSERT(NativeEndian::swapFromNetworkOrder(aValue) ==
aUnswappedValue);
#endif
}
// EndianUtils.h functions are declared as protected in a base class and
// then re-exported as public in public derived classes. The
// standardese around explicit instantiation of templates is not clear
// in such cases. Provide these wrappers to make things more explicit.
// For your own enlightenment, you may wish to peruse:
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56152 and subsequently
// http://j.mp/XosS6S .
#define WRAP_COPYTO(NAME) \
template<typename T> \
void \
NAME(void* aDst, const T* aSrc, size_t aCount) \
{ \
NativeEndian::NAME<T>(aDst, aSrc, aCount); \
}
WRAP_COPYTO(copyAndSwapToLittleEndian)
WRAP_COPYTO(copyAndSwapToBigEndian)
WRAP_COPYTO(copyAndSwapToNetworkOrder)
#define WRAP_COPYFROM(NAME) \
template<typename T> \
void \
NAME(T* aDst, const void* aSrc, size_t aCount) \
{ \
NativeEndian::NAME<T>(aDst, aSrc, aCount); \
}
WRAP_COPYFROM(copyAndSwapFromLittleEndian)
WRAP_COPYFROM(copyAndSwapFromBigEndian)
WRAP_COPYFROM(copyAndSwapFromNetworkOrder)
#define WRAP_IN_PLACE(NAME) \
template<typename T> \
void \
NAME(T* aP, size_t aCount) \
{ \
NativeEndian::NAME<T>(aP, aCount); \
}
WRAP_IN_PLACE(swapToLittleEndianInPlace)
WRAP_IN_PLACE(swapFromLittleEndianInPlace)
WRAP_IN_PLACE(swapToBigEndianInPlace)
WRAP_IN_PLACE(swapFromBigEndianInPlace)
WRAP_IN_PLACE(swapToNetworkOrderInPlace)
WRAP_IN_PLACE(swapFromNetworkOrderInPlace)
enum SwapExpectation
{
Swap,
NoSwap
};
template<typename T, size_t Count>
void
TestBulkSwapToSub(enum SwapExpectation aExpectSwap,
const T (&aValues)[Count],
void (*aSwapperFunc)(void*, const T*, size_t),
T (*aReaderFunc)(const void*))
{
const size_t arraySize = 2 * Count;
const size_t bufferSize = arraySize * sizeof(T);
static uint8_t buffer[bufferSize];
const uint8_t fillValue = 0xa5;
static uint8_t checkBuffer[bufferSize];
MOZ_RELEASE_ASSERT(bufferSize > 2 * sizeof(T));
memset(checkBuffer, fillValue, bufferSize);
for (size_t startPosition = 0; startPosition < sizeof(T); ++startPosition) {
for (size_t nValues = 0; nValues < Count; ++nValues) {
memset(buffer, fillValue, bufferSize);
aSwapperFunc(buffer + startPosition, aValues, nValues);
MOZ_RELEASE_ASSERT(memcmp(buffer, checkBuffer, startPosition) == 0);
size_t valuesEndPosition = startPosition + sizeof(T) * nValues;
MOZ_RELEASE_ASSERT(memcmp(buffer + valuesEndPosition,
checkBuffer + valuesEndPosition,
bufferSize - valuesEndPosition) == 0);
if (aExpectSwap == NoSwap) {
MOZ_RELEASE_ASSERT(memcmp(buffer + startPosition, aValues,
nValues * sizeof(T)) == 0);
}
for (size_t i = 0; i < nValues; ++i) {
MOZ_RELEASE_ASSERT(
aReaderFunc(buffer + startPosition + sizeof(T) * i) == aValues[i]);
}
}
}
}
template<typename T, size_t Count>
void
TestBulkSwapFromSub(enum SwapExpectation aExpectSwap,
const T (&aValues)[Count],
void (*aSwapperFunc)(T*, const void*, size_t),
T (*aReaderFunc)(const void*))
{
const size_t arraySize = 2 * Count;
const size_t bufferSize = arraySize * sizeof(T);
static T buffer[arraySize];
const uint8_t fillValue = 0xa5;
static T checkBuffer[arraySize];
memset(checkBuffer, fillValue, bufferSize);
for (size_t startPosition = 0; startPosition < Count; ++startPosition) {
for (size_t nValues = 0; nValues < (Count - startPosition); ++nValues) {
memset(buffer, fillValue, bufferSize);
aSwapperFunc(buffer + startPosition, aValues, nValues);
MOZ_RELEASE_ASSERT(
memcmp(buffer, checkBuffer, startPosition * sizeof(T)) == 0);
size_t valuesEndPosition = startPosition + nValues;
MOZ_RELEASE_ASSERT(memcmp(buffer + valuesEndPosition,
checkBuffer + valuesEndPosition,
(arraySize - valuesEndPosition) * sizeof(T)) == 0);
if (aExpectSwap == NoSwap) {
MOZ_RELEASE_ASSERT(memcmp(buffer + startPosition, aValues,
nValues * sizeof(T)) == 0);
}
for (size_t i = 0; i < nValues; ++i) {
MOZ_RELEASE_ASSERT(aReaderFunc(buffer + startPosition + i) == aValues[i]);
}
}
}
}
template<typename T, size_t Count>
void
TestBulkInPlaceSub(enum SwapExpectation aExpectSwap,
const T (&aValues)[Count],
void (*aSwapperFunc)(T*, size_t),
T (*aReaderFunc)(const void*))
{
const size_t bufferCount = 4 * Count;
const size_t bufferSize = bufferCount * sizeof(T);
static T buffer[bufferCount];
const T fillValue = 0xa5;
static T checkBuffer[bufferCount];
MOZ_RELEASE_ASSERT(bufferSize > 2 * sizeof(T));
memset(checkBuffer, fillValue, bufferSize);
for (size_t startPosition = 0; startPosition < Count; ++startPosition) {
for (size_t nValues = 0; nValues < Count; ++nValues) {
memset(buffer, fillValue, bufferSize);
memcpy(buffer + startPosition, aValues, nValues * sizeof(T));
aSwapperFunc(buffer + startPosition, nValues);
MOZ_RELEASE_ASSERT(
memcmp(buffer, checkBuffer, startPosition * sizeof(T)) == 0);
size_t valuesEndPosition = startPosition + nValues;
MOZ_RELEASE_ASSERT(memcmp(buffer + valuesEndPosition,
checkBuffer + valuesEndPosition,
bufferSize - valuesEndPosition * sizeof(T)) == 0);
if (aExpectSwap == NoSwap) {
MOZ_RELEASE_ASSERT(memcmp(buffer + startPosition, aValues,
nValues * sizeof(T)) == 0);
}
for (size_t i = 0; i < nValues; ++i) {
MOZ_RELEASE_ASSERT(aReaderFunc(buffer + startPosition + i) == aValues[i]);
}
}
}
}
template<typename T>
struct Reader
{
};
#define SPECIALIZE_READER(TYPE, READ_FUNC) \
template<> \
struct Reader<TYPE> \
{ \
static TYPE readLE(const void* aP) { return LittleEndian::READ_FUNC(aP); }\
static TYPE readBE(const void* aP) { return BigEndian::READ_FUNC(aP); } \
};
SPECIALIZE_READER(uint16_t, readUint16)
SPECIALIZE_READER(uint32_t, readUint32)
SPECIALIZE_READER(uint64_t, readUint64)
SPECIALIZE_READER(int16_t, readInt16)
SPECIALIZE_READER(int32_t, readInt32)
SPECIALIZE_READER(int64_t, readInt64)
template<typename T, size_t Count>
void
TestBulkSwap(const T (&aBytes)[Count])
{
#if MOZ_LITTLE_ENDIAN
TestBulkSwapToSub(Swap, aBytes, copyAndSwapToBigEndian<T>,
Reader<T>::readBE);
TestBulkSwapFromSub(Swap, aBytes, copyAndSwapFromBigEndian<T>,
Reader<T>::readBE);
TestBulkSwapToSub(Swap, aBytes, copyAndSwapToNetworkOrder<T>,
Reader<T>::readBE);
TestBulkSwapFromSub(Swap, aBytes, copyAndSwapFromNetworkOrder<T>,
Reader<T>::readBE);
#else
TestBulkSwapToSub(Swap, aBytes, copyAndSwapToLittleEndian<T>,
Reader<T>::readLE);
TestBulkSwapFromSub(Swap, aBytes, copyAndSwapFromLittleEndian<T>,
Reader<T>::readLE);
#endif
}
template<typename T, size_t Count>
void
TestBulkNoSwap(const T (&aBytes)[Count])
{
#if MOZ_LITTLE_ENDIAN
TestBulkSwapToSub(NoSwap, aBytes, copyAndSwapToLittleEndian<T>,
Reader<T>::readLE);
TestBulkSwapFromSub(NoSwap, aBytes, copyAndSwapFromLittleEndian<T>,
Reader<T>::readLE);
#else
TestBulkSwapToSub(NoSwap, aBytes, copyAndSwapToBigEndian<T>,
Reader<T>::readBE);
TestBulkSwapFromSub(NoSwap, aBytes, copyAndSwapFromBigEndian<T>,
Reader<T>::readBE);
TestBulkSwapToSub(NoSwap, aBytes, copyAndSwapToNetworkOrder<T>,
Reader<T>::readBE);
TestBulkSwapFromSub(NoSwap, aBytes, copyAndSwapFromNetworkOrder<T>,
Reader<T>::readBE);
#endif
}
template<typename T, size_t Count>
void
TestBulkInPlaceSwap(const T (&aBytes)[Count])
{
#if MOZ_LITTLE_ENDIAN
TestBulkInPlaceSub(Swap, aBytes, swapToBigEndianInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(Swap, aBytes, swapFromBigEndianInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(Swap, aBytes, swapToNetworkOrderInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(Swap, aBytes, swapFromNetworkOrderInPlace<T>,
Reader<T>::readBE);
#else
TestBulkInPlaceSub(Swap, aBytes, swapToLittleEndianInPlace<T>,
Reader<T>::readLE);
TestBulkInPlaceSub(Swap, aBytes, swapFromLittleEndianInPlace<T>,
Reader<T>::readLE);
#endif
}
template<typename T, size_t Count>
void
TestBulkInPlaceNoSwap(const T (&aBytes)[Count])
{
#if MOZ_LITTLE_ENDIAN
TestBulkInPlaceSub(NoSwap, aBytes, swapToLittleEndianInPlace<T>,
Reader<T>::readLE);
TestBulkInPlaceSub(NoSwap, aBytes, swapFromLittleEndianInPlace<T>,
Reader<T>::readLE);
#else
TestBulkInPlaceSub(NoSwap, aBytes, swapToBigEndianInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(NoSwap, aBytes, swapFromBigEndianInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(NoSwap, aBytes, swapToNetworkOrderInPlace<T>,
Reader<T>::readBE);
TestBulkInPlaceSub(NoSwap, aBytes, swapFromNetworkOrderInPlace<T>,
Reader<T>::readBE);
#endif
}
int
main()
{
static const uint8_t unsigned_bytes[16] = {
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8
};
static const int8_t signed_bytes[16] = {
-0x0f, -0x0e, -0x0d, -0x0c, -0x0b, -0x0a, -0x09, -0x08,
-0x0f, -0x0e, -0x0d, -0x0c, -0x0b, -0x0a, -0x09, -0x08
};
static const uint16_t uint16_values[8] = {
0x102, 0x304, 0x506, 0x708, 0x102, 0x304, 0x506, 0x708
};
static const int16_t int16_values[8] = {
int16_t(0xf1f2), int16_t(0xf3f4), int16_t(0xf5f6), int16_t(0xf7f8),
int16_t(0xf1f2), int16_t(0xf3f4), int16_t(0xf5f6), int16_t(0xf7f8)
};
static const uint32_t uint32_values[4] = {
0x1020304, 0x5060708, 0x1020304, 0x5060708
};
static const int32_t int32_values[4] = {
int32_t(0xf1f2f3f4), int32_t(0xf5f6f7f8),
int32_t(0xf1f2f3f4), int32_t(0xf5f6f7f8)
};
static const uint64_t uint64_values[2] = {
0x102030405060708, 0x102030405060708
};
static const int64_t int64_values[2] = {
int64_t(0xf1f2f3f4f5f6f7f8), int64_t(0xf1f2f3f4f5f6f7f8)
};
uint8_t buffer[8];
MOZ_RELEASE_ASSERT(LittleEndian::readUint16(&unsigned_bytes[0]) == 0x201);
MOZ_RELEASE_ASSERT(BigEndian::readUint16(&unsigned_bytes[0]) == 0x102);
MOZ_RELEASE_ASSERT(
LittleEndian::readUint32(&unsigned_bytes[0]) == 0x4030201U);
MOZ_RELEASE_ASSERT(
BigEndian::readUint32(&unsigned_bytes[0]) == 0x1020304U);
MOZ_RELEASE_ASSERT(
LittleEndian::readUint64(&unsigned_bytes[0]) == 0x807060504030201ULL);
MOZ_RELEASE_ASSERT(
BigEndian::readUint64(&unsigned_bytes[0]) == 0x102030405060708ULL);
LittleEndian::writeUint16(&buffer[0], 0x201);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint16_t)) == 0);
BigEndian::writeUint16(&buffer[0], 0x102);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint16_t)) == 0);
LittleEndian::writeUint32(&buffer[0], 0x4030201U);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint32_t)) == 0);
BigEndian::writeUint32(&buffer[0], 0x1020304U);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint32_t)) == 0);
LittleEndian::writeUint64(&buffer[0], 0x807060504030201ULL);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint64_t)) == 0);
BigEndian::writeUint64(&buffer[0], 0x102030405060708ULL);
MOZ_RELEASE_ASSERT(
memcmp(&unsigned_bytes[0], &buffer[0], sizeof(uint64_t)) == 0);
MOZ_RELEASE_ASSERT(
LittleEndian::readInt16(&signed_bytes[0]) == int16_t(0xf2f1));
MOZ_RELEASE_ASSERT(
BigEndian::readInt16(&signed_bytes[0]) == int16_t(0xf1f2));
MOZ_RELEASE_ASSERT(
LittleEndian::readInt32(&signed_bytes[0]) == int32_t(0xf4f3f2f1));
MOZ_RELEASE_ASSERT(
BigEndian::readInt32(&signed_bytes[0]) == int32_t(0xf1f2f3f4));
MOZ_RELEASE_ASSERT(
LittleEndian::readInt64(&signed_bytes[0]) == int64_t(0xf8f7f6f5f4f3f2f1LL));
MOZ_RELEASE_ASSERT(
BigEndian::readInt64(&signed_bytes[0]) == int64_t(0xf1f2f3f4f5f6f7f8LL));
LittleEndian::writeInt16(&buffer[0], int16_t(0xf2f1));
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int16_t)) == 0);
BigEndian::writeInt16(&buffer[0], int16_t(0xf1f2));
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int16_t)) == 0);
LittleEndian::writeInt32(&buffer[0], 0xf4f3f2f1);
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int32_t)) == 0);
BigEndian::writeInt32(&buffer[0], 0xf1f2f3f4);
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int32_t)) == 0);
LittleEndian::writeInt64(&buffer[0], 0xf8f7f6f5f4f3f2f1LL);
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int64_t)) == 0);
BigEndian::writeInt64(&buffer[0], 0xf1f2f3f4f5f6f7f8LL);
MOZ_RELEASE_ASSERT(
memcmp(&signed_bytes[0], &buffer[0], sizeof(int64_t)) == 0);
TestSingleSwap(uint16_t(0xf2f1), uint16_t(0xf1f2));
TestSingleSwap(uint32_t(0xf4f3f2f1), uint32_t(0xf1f2f3f4));
TestSingleSwap(uint64_t(0xf8f7f6f5f4f3f2f1), uint64_t(0xf1f2f3f4f5f6f7f8));
TestSingleSwap(int16_t(0xf2f1), int16_t(0xf1f2));
TestSingleSwap(int32_t(0xf4f3f2f1), int32_t(0xf1f2f3f4));
TestSingleSwap(int64_t(0xf8f7f6f5f4f3f2f1), int64_t(0xf1f2f3f4f5f6f7f8));
TestSingleNoSwap(uint16_t(0xf2f1), uint16_t(0xf2f1));
TestSingleNoSwap(uint32_t(0xf4f3f2f1), uint32_t(0xf4f3f2f1));
TestSingleNoSwap(uint64_t(0xf8f7f6f5f4f3f2f1), uint64_t(0xf8f7f6f5f4f3f2f1));
TestSingleNoSwap(int16_t(0xf2f1), int16_t(0xf2f1));
TestSingleNoSwap(int32_t(0xf4f3f2f1), int32_t(0xf4f3f2f1));
TestSingleNoSwap(int64_t(0xf8f7f6f5f4f3f2f1), int64_t(0xf8f7f6f5f4f3f2f1));
TestBulkSwap(uint16_values);
TestBulkSwap(int16_values);
TestBulkSwap(uint32_values);
TestBulkSwap(int32_values);
TestBulkSwap(uint64_values);
TestBulkSwap(int64_values);
TestBulkNoSwap(uint16_values);
TestBulkNoSwap(int16_values);
TestBulkNoSwap(uint32_values);
TestBulkNoSwap(int32_values);
TestBulkNoSwap(uint64_values);
TestBulkNoSwap(int64_values);
TestBulkInPlaceSwap(uint16_values);
TestBulkInPlaceSwap(int16_values);
TestBulkInPlaceSwap(uint32_values);
TestBulkInPlaceSwap(int32_values);
TestBulkInPlaceSwap(uint64_values);
TestBulkInPlaceSwap(int64_values);
TestBulkInPlaceNoSwap(uint16_values);
TestBulkInPlaceNoSwap(int16_values);
TestBulkInPlaceNoSwap(uint32_values);
TestBulkInPlaceNoSwap(int32_values);
TestBulkInPlaceNoSwap(uint64_values);
TestBulkInPlaceNoSwap(int64_values);
return 0;
}
|