1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Functions for reading and writing integers in various endiannesses. */
10 * The classes LittleEndian and BigEndian expose static methods for
11 * reading and writing 16-, 32-, and 64-bit signed and unsigned integers
12 * in their respective endianness. The naming scheme is:
14 * {Little,Big}Endian::{read,write}{Uint,Int}<bitsize>
16 * For instance, LittleEndian::readInt32 will read a 32-bit signed
17 * integer from memory in little endian format. Similarly,
18 * BigEndian::writeUint16 will write a 16-bit unsigned integer to memory
19 * in big-endian format.
21 * The class NativeEndian exposes methods for conversion of existing
22 * data to and from the native endianness. These methods are intended
23 * for cases where data needs to be transferred, serialized, etc.
24 * swap{To,From}{Little,Big}Endian byteswap a single value if necessary.
25 * Bulk conversion functions are also provided which optimize the
26 * no-conversion-needed case:
28 * - copyAndSwap{To,From}{Little,Big}Endian;
29 * - swap{To,From}{Little,Big}EndianInPlace.
31 * The *From* variants are intended to be used for reading data and the
32 * *To* variants for writing data.
34 * Methods on NativeEndian work with integer data of any type.
35 * Floating-point data is not supported.
37 * For clarity in networking code, "Network" may be used as a synonym
38 * for "Big" in any of the above methods or class names.
40 * As an example, reading a file format header whose fields are stored
41 * in big-endian format might look like:
48 * uint32_t mTotalRecords;
52 * ExampleHeader(const void* data)
54 * const uint8_t* ptr = static_cast<const uint8_t*>(data);
55 * mMagic = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
56 * mLength = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
57 * mTotalRecords = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
58 * mChecksum = BigEndian::readUint64(ptr);
64 #ifndef mozilla_Endian_h
65 #define mozilla_Endian_h
67 #include "mozilla/Assertions.h"
68 #include "mozilla/Attributes.h"
69 #include "mozilla/Compiler.h"
70 #include "mozilla/DebugOnly.h"
71 #include "mozilla/TypeTraits.h"
78 # pragma intrinsic(_byteswap_ushort)
79 # pragma intrinsic(_byteswap_ulong)
80 # pragma intrinsic(_byteswap_uint64)
84 # if defined(_M_X64) || defined(_M_AMD64) || defined(_AMD64_)
85 # define MOZ_LITTLE_ENDIAN 1
87 # error "CPU type is unknown"
91 # define MOZ_LITTLE_ENDIAN 1
93 # error "CPU type is unknown"
95 #elif defined(__APPLE__) || defined(__powerpc__) || defined(__ppc__)
96 # if __LITTLE_ENDIAN__
97 # define MOZ_LITTLE_ENDIAN 1
99 # define MOZ_BIG_ENDIAN 1
101 #elif defined(__GNUC__) && \
102 defined(__BYTE_ORDER__) && \
103 defined(__ORDER_LITTLE_ENDIAN__) && \
104 defined(__ORDER_BIG_ENDIAN__)
106 * Some versions of GCC provide architecture-independent macros for
107 * this. Yes, there are more than two values for __BYTE_ORDER__.
109 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
110 # define MOZ_LITTLE_ENDIAN 1
111 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
112 # define MOZ_BIG_ENDIAN 1
114 # error "Can't handle mixed-endian architectures"
117 * We can't include useful headers like <endian.h> or <sys/isa_defs.h>
118 * here because they're not present on all platforms. Instead we have
119 * this big conditional that ideally will catch all the interesting
122 #elif defined(__sparc) || defined(__sparc__) || \
123 defined(_POWER) || defined(__hppa) || \
124 defined(_MIPSEB) || defined(__ARMEB__) || \
125 defined(__s390__) || defined(__AARCH64EB__) || \
126 (defined(__sh__) && defined(__LITTLE_ENDIAN__)) || \
127 (defined(__ia64) && defined(__BIG_ENDIAN__))
128 # define MOZ_BIG_ENDIAN 1
129 #elif defined(__i386) || defined(__i386__) || \
130 defined(__x86_64) || defined(__x86_64__) || \
131 defined(_MIPSEL) || defined(__ARMEL__) || \
132 defined(__alpha__) || defined(__AARCH64EL__) || \
133 (defined(__sh__) && defined(__BIG_ENDIAN__)) || \
134 (defined(__ia64) && !defined(__BIG_ENDIAN__))
135 # define MOZ_LITTLE_ENDIAN 1
139 # define MOZ_LITTLE_ENDIAN 0
140 #elif MOZ_LITTLE_ENDIAN
141 # define MOZ_BIG_ENDIAN 0
143 # error "Cannot determine endianness"
146 #if defined(__clang__)
147 # if __has_builtin(__builtin_bswap16)
148 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
150 #elif defined(__GNUC__)
151 # if MOZ_GCC_VERSION_AT_LEAST(4, 8, 0)
152 # define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
154 #elif defined(_MSC_VER)
155 # define MOZ_HAVE_BUILTIN_BYTESWAP16 _byteswap_ushort
163 * We need wrappers here because free functions with default template
164 * arguments and/or partial specialization of function templates are not
165 * supported by all the compilers we use.
167 template<typename T
, size_t Size
= sizeof(T
)>
173 static T
swap(T aValue
)
175 #if defined(MOZ_HAVE_BUILTIN_BYTESWAP16)
176 return MOZ_HAVE_BUILTIN_BYTESWAP16(aValue
);
178 return T(((aValue
& 0x00ff) << 8) | ((aValue
& 0xff00) >> 8));
186 static T
swap(T aValue
)
188 #if defined(__clang__) || defined(__GNUC__)
189 return T(__builtin_bswap32(aValue
));
190 #elif defined(_MSC_VER)
191 return T(_byteswap_ulong(aValue
));
193 return T(((aValue
& 0x000000ffU
) << 24) |
194 ((aValue
& 0x0000ff00U
) << 8) |
195 ((aValue
& 0x00ff0000U
) >> 8) |
196 ((aValue
& 0xff000000U
) >> 24));
204 static inline T
swap(T aValue
)
206 #if defined(__clang__) || defined(__GNUC__)
207 return T(__builtin_bswap64(aValue
));
208 #elif defined(_MSC_VER)
209 return T(_byteswap_uint64(aValue
));
211 return T(((aValue
& 0x00000000000000ffULL
) << 56) |
212 ((aValue
& 0x000000000000ff00ULL
) << 40) |
213 ((aValue
& 0x0000000000ff0000ULL
) << 24) |
214 ((aValue
& 0x00000000ff000000ULL
) << 8) |
215 ((aValue
& 0x000000ff00000000ULL
) >> 8) |
216 ((aValue
& 0x0000ff0000000000ULL
) >> 24) |
217 ((aValue
& 0x00ff000000000000ULL
) >> 40) |
218 ((aValue
& 0xff00000000000000ULL
) >> 56));
223 enum Endianness
{ Little
, Big
};
226 # define MOZ_NATIVE_ENDIANNESS detail::Big
228 # define MOZ_NATIVE_ENDIANNESS detail::Little
234 * Assert that the memory regions [aDest, aDest+aCount) and
235 * [aSrc, aSrc+aCount] do not overlap. aCount is given in bytes.
237 static void assertNoOverlap(const void* aDest
, const void* aSrc
,
240 DebugOnly
<const uint8_t*> byteDestPtr
= static_cast<const uint8_t*>(aDest
);
241 DebugOnly
<const uint8_t*> byteSrcPtr
= static_cast<const uint8_t*>(aSrc
);
242 MOZ_ASSERT((byteDestPtr
<= byteSrcPtr
&&
243 byteDestPtr
+ aCount
<= byteSrcPtr
) ||
244 (byteSrcPtr
<= byteDestPtr
&&
245 byteSrcPtr
+ aCount
<= byteDestPtr
));
249 static void assertAligned(T
* aPtr
)
251 MOZ_ASSERT((uintptr_t(aPtr
) % sizeof(T
)) == 0, "Unaligned pointer!");
256 * Return |aValue| converted from SourceEndian encoding to DestEndian
259 template<Endianness SourceEndian
, Endianness DestEndian
, typename T
>
260 static inline T
maybeSwap(T aValue
)
262 if (SourceEndian
== DestEndian
) {
265 return Swapper
<T
>::swap(aValue
);
269 * Convert |aCount| elements at |aPtr| from SourceEndian encoding to
270 * DestEndian encoding.
272 template<Endianness SourceEndian
, Endianness DestEndian
, typename T
>
273 static inline void maybeSwapInPlace(T
* aPtr
, size_t aCount
)
277 if (SourceEndian
== DestEndian
) {
280 for (size_t i
= 0; i
< aCount
; i
++) {
281 aPtr
[i
] = Swapper
<T
>::swap(aPtr
[i
]);
286 * Write |aCount| elements to the unaligned address |aDest| in DestEndian
287 * format, using elements found at |aSrc| in SourceEndian format.
289 template<Endianness SourceEndian
, Endianness DestEndian
, typename T
>
290 static void copyAndSwapTo(void* aDest
, const T
* aSrc
, size_t aCount
)
292 assertNoOverlap(aDest
, aSrc
, aCount
* sizeof(T
));
295 if (SourceEndian
== DestEndian
) {
296 memcpy(aDest
, aSrc
, aCount
* sizeof(T
));
300 uint8_t* byteDestPtr
= static_cast<uint8_t*>(aDest
);
301 for (size_t i
= 0; i
< aCount
; ++i
) {
305 uint8_t mBuffer
[sizeof(T
)];
307 u
.mVal
= maybeSwap
<SourceEndian
, DestEndian
>(aSrc
[i
]);
308 memcpy(byteDestPtr
, u
.mBuffer
, sizeof(T
));
309 byteDestPtr
+= sizeof(T
);
314 * Write |aCount| elements to |aDest| in DestEndian format, using elements
315 * found at the unaligned address |aSrc| in SourceEndian format.
317 template<Endianness SourceEndian
, Endianness DestEndian
, typename T
>
318 static void copyAndSwapFrom(T
* aDest
, const void* aSrc
, size_t aCount
)
320 assertNoOverlap(aDest
, aSrc
, aCount
* sizeof(T
));
321 assertAligned(aDest
);
323 if (SourceEndian
== DestEndian
) {
324 memcpy(aDest
, aSrc
, aCount
* sizeof(T
));
328 const uint8_t* byteSrcPtr
= static_cast<const uint8_t*>(aSrc
);
329 for (size_t i
= 0; i
< aCount
; ++i
) {
333 uint8_t mBuffer
[sizeof(T
)];
335 memcpy(u
.mBuffer
, byteSrcPtr
, sizeof(T
));
336 aDest
[i
] = maybeSwap
<SourceEndian
, DestEndian
>(u
.mVal
);
337 byteSrcPtr
+= sizeof(T
);
342 template<Endianness ThisEndian
>
343 class Endian
: private EndianUtils
346 /** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */
347 static MOZ_WARN_UNUSED_RESULT
uint16_t readUint16(const void* aPtr
)
349 return read
<uint16_t>(aPtr
);
352 /** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */
353 static MOZ_WARN_UNUSED_RESULT
uint32_t readUint32(const void* aPtr
)
355 return read
<uint32_t>(aPtr
);
358 /** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */
359 static MOZ_WARN_UNUSED_RESULT
uint64_t readUint64(const void* aPtr
)
361 return read
<uint64_t>(aPtr
);
364 /** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */
365 static MOZ_WARN_UNUSED_RESULT
int16_t readInt16(const void* aPtr
)
367 return read
<int16_t>(aPtr
);
370 /** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */
371 static MOZ_WARN_UNUSED_RESULT
int32_t readInt32(const void* aPtr
)
373 return read
<uint32_t>(aPtr
);
376 /** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */
377 static MOZ_WARN_UNUSED_RESULT
int64_t readInt64(const void* aPtr
)
379 return read
<int64_t>(aPtr
);
382 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
383 static void writeUint16(void* aPtr
, uint16_t aValue
)
388 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
389 static void writeUint32(void* aPtr
, uint32_t aValue
)
394 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
395 static void writeUint64(void* aPtr
, uint64_t aValue
)
400 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
401 static void writeInt16(void* aPtr
, int16_t aValue
)
406 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
407 static void writeInt32(void* aPtr
, int32_t aValue
)
412 /** Write |aValue| to |aPtr| using ThisEndian endianness. */
413 static void writeInt64(void* aPtr
, int64_t aValue
)
419 * Converts a value of type T to little-endian format.
421 * This function is intended for cases where you have data in your
422 * native-endian format and you need it to appear in little-endian
423 * format for transmission.
426 MOZ_WARN_UNUSED_RESULT
static T
swapToLittleEndian(T aValue
)
428 return maybeSwap
<ThisEndian
, Little
>(aValue
);
432 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
433 * them to little-endian format if ThisEndian is Big.
434 * As with memcpy, |aDest| and |aSrc| must not overlap.
437 static void copyAndSwapToLittleEndian(void* aDest
, const T
* aSrc
,
440 copyAndSwapTo
<ThisEndian
, Little
>(aDest
, aSrc
, aCount
);
444 * Likewise, but converts values in place.
447 static void swapToLittleEndianInPlace(T
* aPtr
, size_t aCount
)
449 maybeSwapInPlace
<ThisEndian
, Little
>(aPtr
, aCount
);
453 * Converts a value of type T to big-endian format.
456 MOZ_WARN_UNUSED_RESULT
static T
swapToBigEndian(T aValue
)
458 return maybeSwap
<ThisEndian
, Big
>(aValue
);
462 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
463 * them to big-endian format if ThisEndian is Little.
464 * As with memcpy, |aDest| and |aSrc| must not overlap.
467 static void copyAndSwapToBigEndian(void* aDest
, const T
* aSrc
,
470 copyAndSwapTo
<ThisEndian
, Big
>(aDest
, aSrc
, aCount
);
474 * Likewise, but converts values in place.
477 static void swapToBigEndianInPlace(T
* aPtr
, size_t aCount
)
479 maybeSwapInPlace
<ThisEndian
, Big
>(aPtr
, aCount
);
483 * Synonyms for the big-endian functions, for better readability
488 MOZ_WARN_UNUSED_RESULT
static T
swapToNetworkOrder(T aValue
)
490 return swapToBigEndian(aValue
);
495 copyAndSwapToNetworkOrder(void* aDest
, const T
* aSrc
, size_t aCount
)
497 copyAndSwapToBigEndian(aDest
, aSrc
, aCount
);
502 swapToNetworkOrderInPlace(T
* aPtr
, size_t aCount
)
504 swapToBigEndianInPlace(aPtr
, aCount
);
508 * Converts a value of type T from little-endian format.
511 MOZ_WARN_UNUSED_RESULT
static T
swapFromLittleEndian(T aValue
)
513 return maybeSwap
<Little
, ThisEndian
>(aValue
);
517 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
518 * them to little-endian format if ThisEndian is Big.
519 * As with memcpy, |aDest| and |aSrc| must not overlap.
522 static void copyAndSwapFromLittleEndian(T
* aDest
, const void* aSrc
,
525 copyAndSwapFrom
<Little
, ThisEndian
>(aDest
, aSrc
, aCount
);
529 * Likewise, but converts values in place.
532 static void swapFromLittleEndianInPlace(T
* aPtr
, size_t aCount
)
534 maybeSwapInPlace
<Little
, ThisEndian
>(aPtr
, aCount
);
538 * Converts a value of type T from big-endian format.
541 MOZ_WARN_UNUSED_RESULT
static T
swapFromBigEndian(T aValue
)
543 return maybeSwap
<Big
, ThisEndian
>(aValue
);
547 * Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
548 * them to big-endian format if ThisEndian is Little.
549 * As with memcpy, |aDest| and |aSrc| must not overlap.
552 static void copyAndSwapFromBigEndian(T
* aDest
, const void* aSrc
,
555 copyAndSwapFrom
<Big
, ThisEndian
>(aDest
, aSrc
, aCount
);
559 * Likewise, but converts values in place.
562 static void swapFromBigEndianInPlace(T
* aPtr
, size_t aCount
)
564 maybeSwapInPlace
<Big
, ThisEndian
>(aPtr
, aCount
);
568 * Synonyms for the big-endian functions, for better readability
572 MOZ_WARN_UNUSED_RESULT
static T
swapFromNetworkOrder(T aValue
)
574 return swapFromBigEndian(aValue
);
578 static void copyAndSwapFromNetworkOrder(T
* aDest
, const void* aSrc
,
581 copyAndSwapFromBigEndian(aDest
, aSrc
, aCount
);
585 static void swapFromNetworkOrderInPlace(T
* aPtr
, size_t aCount
)
587 swapFromBigEndianInPlace(aPtr
, aCount
);
592 * Read a value of type T, encoded in endianness ThisEndian from |aPtr|.
593 * Return that value encoded in native endianness.
596 static T
read(const void* aPtr
)
601 uint8_t mBuffer
[sizeof(T
)];
603 memcpy(u
.mBuffer
, aPtr
, sizeof(T
));
604 return maybeSwap
<ThisEndian
, MOZ_NATIVE_ENDIANNESS
>(u
.mVal
);
608 * Write a value of type T, in native endianness, to |aPtr|, in ThisEndian
612 static void write(void* aPtr
, T aValue
)
614 T tmp
= maybeSwap
<MOZ_NATIVE_ENDIANNESS
, ThisEndian
>(aValue
);
615 memcpy(aPtr
, &tmp
, sizeof(T
));
619 Endian(const Endian
& aTther
) = delete;
620 void operator=(const Endian
& aOther
) = delete;
623 template<Endianness ThisEndian
>
624 class EndianReadWrite
: public Endian
<ThisEndian
>
627 typedef Endian
<ThisEndian
> super
;
630 using super::readUint16
;
631 using super::readUint32
;
632 using super::readUint64
;
633 using super::readInt16
;
634 using super::readInt32
;
635 using super::readInt64
;
636 using super::writeUint16
;
637 using super::writeUint32
;
638 using super::writeUint64
;
639 using super::writeInt16
;
640 using super::writeInt32
;
641 using super::writeInt64
;
644 } /* namespace detail */
646 class LittleEndian MOZ_FINAL
: public detail::EndianReadWrite
<detail::Little
>
649 class BigEndian MOZ_FINAL
: public detail::EndianReadWrite
<detail::Big
>
652 typedef BigEndian NetworkEndian
;
654 class NativeEndian MOZ_FINAL
: public detail::Endian
<MOZ_NATIVE_ENDIANNESS
>
657 typedef detail::Endian
<MOZ_NATIVE_ENDIANNESS
> super
;
661 * These functions are intended for cases where you have data in your
662 * native-endian format and you need the data to appear in the appropriate
663 * endianness for transmission, serialization, etc.
665 using super::swapToLittleEndian
;
666 using super::copyAndSwapToLittleEndian
;
667 using super::swapToLittleEndianInPlace
;
668 using super::swapToBigEndian
;
669 using super::copyAndSwapToBigEndian
;
670 using super::swapToBigEndianInPlace
;
671 using super::swapToNetworkOrder
;
672 using super::copyAndSwapToNetworkOrder
;
673 using super::swapToNetworkOrderInPlace
;
676 * These functions are intended for cases where you have data in the
677 * given endianness (e.g. reading from disk or a file-format) and you
678 * need the data to appear in native-endian format for processing.
680 using super::swapFromLittleEndian
;
681 using super::copyAndSwapFromLittleEndian
;
682 using super::swapFromLittleEndianInPlace
;
683 using super::swapFromBigEndian
;
684 using super::copyAndSwapFromBigEndian
;
685 using super::swapFromBigEndianInPlace
;
686 using super::swapFromNetworkOrder
;
687 using super::copyAndSwapFromNetworkOrder
;
688 using super::swapFromNetworkOrderInPlace
;
691 #undef MOZ_NATIVE_ENDIANNESS
693 } /* namespace mozilla */
695 #endif /* mozilla_Endian_h */