2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-01-31 01:45:39 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/* Functions for reading and writing integers in various endiannesses. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The classes LittleEndian and BigEndian expose static methods for
|
|
|
|
* reading and writing 16-, 32-, and 64-bit signed and unsigned integers
|
2018-03-01 20:05:13 +03:00
|
|
|
* in their respective endianness. The addresses read from or written
|
|
|
|
* to may be misaligned (although misaligned accesses may incur
|
|
|
|
* architecture-specific performance costs). The naming scheme is:
|
2013-01-31 01:45:39 +04:00
|
|
|
*
|
|
|
|
* {Little,Big}Endian::{read,write}{Uint,Int}<bitsize>
|
|
|
|
*
|
|
|
|
* For instance, LittleEndian::readInt32 will read a 32-bit signed
|
|
|
|
* integer from memory in little endian format. Similarly,
|
|
|
|
* BigEndian::writeUint16 will write a 16-bit unsigned integer to memory
|
|
|
|
* in big-endian format.
|
|
|
|
*
|
|
|
|
* The class NativeEndian exposes methods for conversion of existing
|
|
|
|
* data to and from the native endianness. These methods are intended
|
|
|
|
* for cases where data needs to be transferred, serialized, etc.
|
|
|
|
* swap{To,From}{Little,Big}Endian byteswap a single value if necessary.
|
|
|
|
* Bulk conversion functions are also provided which optimize the
|
|
|
|
* no-conversion-needed case:
|
|
|
|
*
|
|
|
|
* - copyAndSwap{To,From}{Little,Big}Endian;
|
|
|
|
* - swap{To,From}{Little,Big}EndianInPlace.
|
|
|
|
*
|
|
|
|
* The *From* variants are intended to be used for reading data and the
|
|
|
|
* *To* variants for writing data.
|
|
|
|
*
|
|
|
|
* Methods on NativeEndian work with integer data of any type.
|
|
|
|
* Floating-point data is not supported.
|
|
|
|
*
|
|
|
|
* For clarity in networking code, "Network" may be used as a synonym
|
|
|
|
* for "Big" in any of the above methods or class names.
|
|
|
|
*
|
|
|
|
* As an example, reading a file format header whose fields are stored
|
|
|
|
* in big-endian format might look like:
|
|
|
|
*
|
|
|
|
* class ExampleHeader
|
|
|
|
* {
|
2014-06-13 10:34:08 +04:00
|
|
|
* private:
|
|
|
|
* uint32_t mMagic;
|
|
|
|
* uint32_t mLength;
|
|
|
|
* uint32_t mTotalRecords;
|
|
|
|
* uint64_t mChecksum;
|
2013-01-31 01:45:39 +04:00
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* public:
|
|
|
|
* ExampleHeader(const void* data)
|
|
|
|
* {
|
|
|
|
* const uint8_t* ptr = static_cast<const uint8_t*>(data);
|
|
|
|
* mMagic = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
|
|
|
|
* mLength = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
|
|
|
|
* mTotalRecords = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t);
|
|
|
|
* mChecksum = BigEndian::readUint64(ptr);
|
|
|
|
* }
|
|
|
|
* ...
|
2013-01-31 01:45:39 +04:00
|
|
|
* };
|
|
|
|
*/
|
|
|
|
|
2016-05-22 23:31:11 +03:00
|
|
|
#ifndef mozilla_EndianUtils_h
|
|
|
|
#define mozilla_EndianUtils_h
|
2013-01-31 01:45:39 +04:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Compiler.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2013-01-31 01:45:39 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-12-14 22:58:13 +03:00
|
|
|
#if defined(_MSC_VER)
|
2013-01-31 01:45:39 +04:00
|
|
|
# include <stdlib.h>
|
|
|
|
# pragma intrinsic(_byteswap_ushort)
|
|
|
|
# pragma intrinsic(_byteswap_ulong)
|
|
|
|
# pragma intrinsic(_byteswap_uint64)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN64)
|
|
|
|
# if defined(_M_X64) || defined(_M_AMD64) || defined(_AMD64_)
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
2018-08-04 01:07:34 +03:00
|
|
|
# elif defined(_M_ARM64)
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
2013-01-31 01:45:39 +04:00
|
|
|
# else
|
|
|
|
# error "CPU type is unknown"
|
|
|
|
# endif
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
# if defined(_M_IX86)
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
2015-03-21 17:24:00 +03:00
|
|
|
# elif defined(_M_ARM)
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
2013-01-31 01:45:39 +04:00
|
|
|
# else
|
|
|
|
# error "CPU type is unknown"
|
|
|
|
# endif
|
2014-02-28 18:57:24 +04:00
|
|
|
#elif defined(__APPLE__) || defined(__powerpc__) || defined(__ppc__)
|
2013-01-31 01:45:39 +04:00
|
|
|
# if __LITTLE_ENDIAN__
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
|
|
|
# elif __BIG_ENDIAN__
|
|
|
|
# define MOZ_BIG_ENDIAN 1
|
|
|
|
# endif
|
|
|
|
#elif defined(__GNUC__) && defined(__BYTE_ORDER__) && \
|
|
|
|
defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
|
|
|
|
/*
|
|
|
|
* Some versions of GCC provide architecture-independent macros for
|
|
|
|
* this. Yes, there are more than two values for __BYTE_ORDER__.
|
|
|
|
*/
|
|
|
|
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
|
|
|
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
# define MOZ_BIG_ENDIAN 1
|
|
|
|
# else
|
|
|
|
# error "Can't handle mixed-endian architectures"
|
|
|
|
# endif
|
|
|
|
/*
|
|
|
|
* We can't include useful headers like <endian.h> or <sys/isa_defs.h>
|
|
|
|
* here because they're not present on all platforms. Instead we have
|
|
|
|
* this big conditional that ideally will catch all the interesting
|
|
|
|
* cases.
|
|
|
|
*/
|
|
|
|
#elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || \
|
|
|
|
defined(__hppa) || defined(_MIPSEB) || defined(__ARMEB__) || \
|
2014-01-24 02:16:27 +04:00
|
|
|
defined(__s390__) || defined(__AARCH64EB__) || \
|
2013-01-31 01:45:39 +04:00
|
|
|
(defined(__sh__) && defined(__LITTLE_ENDIAN__)) || \
|
|
|
|
(defined(__ia64) && defined(__BIG_ENDIAN__))
|
|
|
|
# define MOZ_BIG_ENDIAN 1
|
|
|
|
#elif defined(__i386) || defined(__i386__) || defined(__x86_64) || \
|
|
|
|
defined(__x86_64__) || defined(_MIPSEL) || defined(__ARMEL__) || \
|
2014-01-24 02:16:27 +04:00
|
|
|
defined(__alpha__) || defined(__AARCH64EL__) || \
|
2013-01-31 01:45:39 +04:00
|
|
|
(defined(__sh__) && defined(__BIG_ENDIAN__)) || \
|
|
|
|
(defined(__ia64) && !defined(__BIG_ENDIAN__))
|
|
|
|
# define MOZ_LITTLE_ENDIAN 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MOZ_BIG_ENDIAN
|
|
|
|
# define MOZ_LITTLE_ENDIAN 0
|
|
|
|
#elif MOZ_LITTLE_ENDIAN
|
|
|
|
# define MOZ_BIG_ENDIAN 0
|
|
|
|
#else
|
|
|
|
# error "Cannot determine endianness"
|
|
|
|
#endif
|
|
|
|
|
2013-04-04 10:40:39 +04:00
|
|
|
#if defined(__clang__)
|
|
|
|
# if __has_builtin(__builtin_bswap16)
|
|
|
|
# define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
|
|
|
|
# endif
|
|
|
|
#elif defined(__GNUC__)
|
2016-04-18 03:08:10 +03:00
|
|
|
# define MOZ_HAVE_BUILTIN_BYTESWAP16 __builtin_bswap16
|
2013-04-04 10:40:39 +04:00
|
|
|
#elif defined(_MSC_VER)
|
2016-04-18 03:08:10 +03:00
|
|
|
# define MOZ_HAVE_BUILTIN_BYTESWAP16 _byteswap_ushort
|
2013-04-04 10:40:39 +04:00
|
|
|
#endif
|
|
|
|
|
2013-01-31 01:45:39 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need wrappers here because free functions with default template
|
|
|
|
* arguments and/or partial specialization of function templates are not
|
|
|
|
* supported by all the compilers we use.
|
|
|
|
*/
|
|
|
|
template <typename T, size_t Size = sizeof(T)>
|
|
|
|
struct Swapper;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Swapper<T, 2> {
|
2014-06-13 10:34:08 +04:00
|
|
|
static T swap(T aValue) {
|
2013-04-04 10:40:39 +04:00
|
|
|
#if defined(MOZ_HAVE_BUILTIN_BYTESWAP16)
|
2014-06-13 10:34:08 +04:00
|
|
|
return MOZ_HAVE_BUILTIN_BYTESWAP16(aValue);
|
2013-01-31 01:45:39 +04:00
|
|
|
#else
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(((aValue & 0x00ff) << 8) | ((aValue & 0xff00) >> 8));
|
2013-01-31 01:45:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Swapper<T, 4> {
|
2014-06-13 10:34:08 +04:00
|
|
|
static T swap(T aValue) {
|
2013-01-31 01:45:39 +04:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(__builtin_bswap32(aValue));
|
2013-01-31 01:45:39 +04:00
|
|
|
#elif defined(_MSC_VER)
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(_byteswap_ulong(aValue));
|
2013-01-31 01:45:39 +04:00
|
|
|
#else
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(((aValue & 0x000000ffU) << 24) | ((aValue & 0x0000ff00U) << 8) |
|
|
|
|
((aValue & 0x00ff0000U) >> 8) | ((aValue & 0xff000000U) >> 24));
|
2013-01-31 01:45:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Swapper<T, 8> {
|
2014-06-13 10:34:08 +04:00
|
|
|
static inline T swap(T aValue) {
|
2013-01-31 01:45:39 +04:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(__builtin_bswap64(aValue));
|
2013-01-31 01:45:39 +04:00
|
|
|
#elif defined(_MSC_VER)
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(_byteswap_uint64(aValue));
|
2013-01-31 01:45:39 +04:00
|
|
|
#else
|
2014-06-13 10:34:08 +04:00
|
|
|
return T(((aValue & 0x00000000000000ffULL) << 56) |
|
|
|
|
((aValue & 0x000000000000ff00ULL) << 40) |
|
|
|
|
((aValue & 0x0000000000ff0000ULL) << 24) |
|
|
|
|
((aValue & 0x00000000ff000000ULL) << 8) |
|
|
|
|
((aValue & 0x000000ff00000000ULL) >> 8) |
|
|
|
|
((aValue & 0x0000ff0000000000ULL) >> 24) |
|
|
|
|
((aValue & 0x00ff000000000000ULL) >> 40) |
|
|
|
|
((aValue & 0xff00000000000000ULL) >> 56));
|
2013-01-31 01:45:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Endianness { Little, Big };
|
|
|
|
|
|
|
|
#if MOZ_BIG_ENDIAN
|
|
|
|
# define MOZ_NATIVE_ENDIANNESS detail::Big
|
|
|
|
#else
|
|
|
|
# define MOZ_NATIVE_ENDIANNESS detail::Little
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class EndianUtils {
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Assert that the memory regions [aDest, aDest+aCount) and
|
|
|
|
* [aSrc, aSrc+aCount] do not overlap. aCount is given in bytes.
|
|
|
|
*/
|
|
|
|
static void assertNoOverlap(const void* aDest, const void* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
DebugOnly<const uint8_t*> byteDestPtr = static_cast<const uint8_t*>(aDest);
|
|
|
|
DebugOnly<const uint8_t*> byteSrcPtr = static_cast<const uint8_t*>(aSrc);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
(byteDestPtr <= byteSrcPtr && byteDestPtr + aCount <= byteSrcPtr) ||
|
|
|
|
(byteSrcPtr <= byteDestPtr && byteSrcPtr + aCount <= byteDestPtr));
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
template <typename T>
|
|
|
|
static void assertAligned(T* aPtr) {
|
|
|
|
MOZ_ASSERT((uintptr_t(aPtr) % sizeof(T)) == 0, "Unaligned pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Return |aValue| converted from SourceEndian encoding to DestEndian
|
|
|
|
* encoding.
|
|
|
|
*/
|
|
|
|
template <Endianness SourceEndian, Endianness DestEndian, typename T>
|
|
|
|
static inline T maybeSwap(T aValue) {
|
|
|
|
if (SourceEndian == DestEndian) {
|
|
|
|
return aValue;
|
2013-01-31 01:45:39 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
return Swapper<T>::swap(aValue);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Convert |aCount| elements at |aPtr| from SourceEndian encoding to
|
|
|
|
* DestEndian encoding.
|
|
|
|
*/
|
|
|
|
template <Endianness SourceEndian, Endianness DestEndian, typename T>
|
|
|
|
static inline void maybeSwapInPlace(T* aPtr, size_t aCount) {
|
|
|
|
assertAligned(aPtr);
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
if (SourceEndian == DestEndian) {
|
|
|
|
return;
|
2013-01-31 01:45:39 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
for (size_t i = 0; i < aCount; i++) {
|
|
|
|
aPtr[i] = Swapper<T>::swap(aPtr[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Write |aCount| elements to the unaligned address |aDest| in DestEndian
|
|
|
|
* format, using elements found at |aSrc| in SourceEndian format.
|
|
|
|
*/
|
|
|
|
template <Endianness SourceEndian, Endianness DestEndian, typename T>
|
|
|
|
static void copyAndSwapTo(void* aDest, const T* aSrc, size_t aCount) {
|
|
|
|
assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
|
|
|
|
assertAligned(aSrc);
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
if (SourceEndian == DestEndian) {
|
|
|
|
memcpy(aDest, aSrc, aCount * sizeof(T));
|
|
|
|
return;
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
uint8_t* byteDestPtr = static_cast<uint8_t*>(aDest);
|
|
|
|
for (size_t i = 0; i < aCount; ++i) {
|
|
|
|
union {
|
|
|
|
T mVal;
|
|
|
|
uint8_t mBuffer[sizeof(T)];
|
|
|
|
} u;
|
|
|
|
u.mVal = maybeSwap<SourceEndian, DestEndian>(aSrc[i]);
|
|
|
|
memcpy(byteDestPtr, u.mBuffer, sizeof(T));
|
|
|
|
byteDestPtr += sizeof(T);
|
2013-01-31 01:45:39 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Write |aCount| elements to |aDest| in DestEndian format, using elements
|
|
|
|
* found at the unaligned address |aSrc| in SourceEndian format.
|
|
|
|
*/
|
|
|
|
template <Endianness SourceEndian, Endianness DestEndian, typename T>
|
|
|
|
static void copyAndSwapFrom(T* aDest, const void* aSrc, size_t aCount) {
|
|
|
|
assertNoOverlap(aDest, aSrc, aCount * sizeof(T));
|
|
|
|
assertAligned(aDest);
|
|
|
|
|
|
|
|
if (SourceEndian == DestEndian) {
|
|
|
|
memcpy(aDest, aSrc, aCount * sizeof(T));
|
|
|
|
return;
|
2013-01-31 01:45:39 +04:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
const uint8_t* byteSrcPtr = static_cast<const uint8_t*>(aSrc);
|
|
|
|
for (size_t i = 0; i < aCount; ++i) {
|
|
|
|
union {
|
|
|
|
T mVal;
|
|
|
|
uint8_t mBuffer[sizeof(T)];
|
|
|
|
} u;
|
|
|
|
memcpy(u.mBuffer, byteSrcPtr, sizeof(T));
|
|
|
|
aDest[i] = maybeSwap<SourceEndian, DestEndian>(u.mVal);
|
|
|
|
byteSrcPtr += sizeof(T);
|
2013-01-31 01:45:39 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <Endianness ThisEndian>
|
|
|
|
class Endian : private EndianUtils {
|
2014-06-13 10:34:08 +04:00
|
|
|
protected:
|
|
|
|
/** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE uint16_t readUint16(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<uint16_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE uint32_t readUint32(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<uint32_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE uint64_t readUint64(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<uint64_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2018-03-01 20:05:13 +03:00
|
|
|
/** Read a uintptr_t in ThisEndian endianness from |aPtr| and return it. */
|
|
|
|
static MOZ_MUST_USE uintptr_t readUintptr(const void* aPtr) {
|
|
|
|
return read<uintptr_t>(aPtr);
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE int16_t readInt16(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<int16_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE int32_t readInt32(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<uint32_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */
|
2016-04-27 07:16:50 +03:00
|
|
|
static MOZ_MUST_USE int64_t readInt64(const void* aPtr) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return read<int64_t>(aPtr);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2018-03-01 20:05:13 +03:00
|
|
|
/** Read an intptr_t in ThisEndian endianness from |aPtr| and return it. */
|
|
|
|
static MOZ_MUST_USE intptr_t readIntptr(const void* aPtr) {
|
|
|
|
return read<intptr_t>(aPtr);
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeUint16(void* aPtr, uint16_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeUint32(void* aPtr, uint32_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeUint64(void* aPtr, uint64_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2018-03-01 20:05:13 +03:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeUintptr(void* aPtr, uintptr_t aValue) {
|
|
|
|
write(aPtr, aValue);
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeInt16(void* aPtr, int16_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeInt32(void* aPtr, int32_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeInt64(void* aPtr, int64_t aValue) { write(aPtr, aValue); }
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2018-03-01 20:05:13 +03:00
|
|
|
/** Write |aValue| to |aPtr| using ThisEndian endianness. */
|
|
|
|
static void writeIntptr(void* aPtr, intptr_t aValue) { write(aPtr, aValue); }
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/*
|
|
|
|
* Converts a value of type T to little-endian format.
|
|
|
|
*
|
|
|
|
* This function is intended for cases where you have data in your
|
|
|
|
* native-endian format and you need it to appear in little-endian
|
|
|
|
* format for transmission.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapToLittleEndian(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return maybeSwap<ThisEndian, Little>(aValue);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/*
|
|
|
|
* Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
|
2019-03-07 00:36:47 +03:00
|
|
|
* them to little-endian format if ThisEndian is Big. |aSrc| as a typed
|
|
|
|
* pointer must be aligned; |aDest| need not be.
|
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* As with memcpy, |aDest| and |aSrc| must not overlap.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapToLittleEndian(void* aDest, const T* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapTo<ThisEndian, Little>(aDest, aSrc, aCount);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/*
|
|
|
|
* Likewise, but converts values in place.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void swapToLittleEndianInPlace(T* aPtr, size_t aCount) {
|
|
|
|
maybeSwapInPlace<ThisEndian, Little>(aPtr, aCount);
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/*
|
|
|
|
* Converts a value of type T to big-endian format.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapToBigEndian(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return maybeSwap<ThisEndian, Big>(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
|
2019-03-07 00:36:47 +03:00
|
|
|
* them to big-endian format if ThisEndian is Little. |aSrc| as a typed
|
|
|
|
* pointer must be aligned; |aDest| need not be.
|
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* As with memcpy, |aDest| and |aSrc| must not overlap.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapToBigEndian(void* aDest, const T* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapTo<ThisEndian, Big>(aDest, aSrc, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, but converts values in place.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2014-07-11 06:10:17 +04:00
|
|
|
static void swapToBigEndianInPlace(T* aPtr, size_t aCount) {
|
2014-06-13 10:34:08 +04:00
|
|
|
maybeSwapInPlace<ThisEndian, Big>(aPtr, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synonyms for the big-endian functions, for better readability
|
|
|
|
* in network code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapToNetworkOrder(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return swapToBigEndian(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapToNetworkOrder(void* aDest, const T* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapToBigEndian(aDest, aSrc, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void swapToNetworkOrderInPlace(T* aPtr, size_t aCount) {
|
|
|
|
swapToBigEndianInPlace(aPtr, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a value of type T from little-endian format.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapFromLittleEndian(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return maybeSwap<Little, ThisEndian>(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
|
2019-03-07 00:36:47 +03:00
|
|
|
* them to little-endian format if ThisEndian is Big. |aDest| as a typed
|
|
|
|
* pointer must be aligned; |aSrc| need not be.
|
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* As with memcpy, |aDest| and |aSrc| must not overlap.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapFromLittleEndian(T* aDest, const void* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapFrom<Little, ThisEndian>(aDest, aSrc, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, but converts values in place.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void swapFromLittleEndianInPlace(T* aPtr, size_t aCount) {
|
|
|
|
maybeSwapInPlace<Little, ThisEndian>(aPtr, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a value of type T from big-endian format.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapFromBigEndian(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return maybeSwap<Big, ThisEndian>(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies |aCount| values of type T starting at |aSrc| to |aDest|, converting
|
2019-03-07 00:36:47 +03:00
|
|
|
* them to big-endian format if ThisEndian is Little. |aDest| as a typed
|
|
|
|
* pointer must be aligned; |aSrc| need not be.
|
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* As with memcpy, |aDest| and |aSrc| must not overlap.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapFromBigEndian(T* aDest, const void* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapFrom<Big, ThisEndian>(aDest, aSrc, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Likewise, but converts values in place.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void swapFromBigEndianInPlace(T* aPtr, size_t aCount) {
|
|
|
|
maybeSwapInPlace<Big, ThisEndian>(aPtr, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synonyms for the big-endian functions, for better readability
|
|
|
|
* in network code.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE static T swapFromNetworkOrder(T aValue) {
|
2014-06-13 10:34:08 +04:00
|
|
|
return swapFromBigEndian(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void copyAndSwapFromNetworkOrder(T* aDest, const void* aSrc,
|
|
|
|
size_t aCount) {
|
|
|
|
copyAndSwapFromBigEndian(aDest, aSrc, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void swapFromNetworkOrderInPlace(T* aPtr, size_t aCount) {
|
|
|
|
swapFromBigEndianInPlace(aPtr, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Read a value of type T, encoded in endianness ThisEndian from |aPtr|.
|
|
|
|
* Return that value encoded in native endianness.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static T read(const void* aPtr) {
|
|
|
|
union {
|
|
|
|
T mVal;
|
|
|
|
uint8_t mBuffer[sizeof(T)];
|
|
|
|
} u;
|
|
|
|
memcpy(u.mBuffer, aPtr, sizeof(T));
|
|
|
|
return maybeSwap<ThisEndian, MOZ_NATIVE_ENDIANNESS>(u.mVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a value of type T, in native endianness, to |aPtr|, in ThisEndian
|
|
|
|
* endianness.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static void write(void* aPtr, T aValue) {
|
|
|
|
T tmp = maybeSwap<MOZ_NATIVE_ENDIANNESS, ThisEndian>(aValue);
|
|
|
|
memcpy(aPtr, &tmp, sizeof(T));
|
|
|
|
}
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
Endian() = delete;
|
|
|
|
Endian(const Endian& aTther) = delete;
|
|
|
|
void operator=(const Endian& aOther) = delete;
|
2013-01-31 01:45:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <Endianness ThisEndian>
|
|
|
|
class EndianReadWrite : public Endian<ThisEndian> {
|
2014-06-13 10:34:08 +04:00
|
|
|
private:
|
|
|
|
typedef Endian<ThisEndian> super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using super::readInt16;
|
|
|
|
using super::readInt32;
|
|
|
|
using super::readInt64;
|
2018-03-01 20:05:13 +03:00
|
|
|
using super::readIntptr;
|
2014-06-13 10:34:08 +04:00
|
|
|
using super::readUint16;
|
|
|
|
using super::readUint32;
|
|
|
|
using super::readUint64;
|
2018-03-01 20:05:13 +03:00
|
|
|
using super::readUintptr;
|
2014-06-13 10:34:08 +04:00
|
|
|
using super::writeInt16;
|
|
|
|
using super::writeInt32;
|
|
|
|
using super::writeInt64;
|
2018-03-01 20:05:13 +03:00
|
|
|
using super::writeIntptr;
|
2014-06-13 10:34:08 +04:00
|
|
|
using super::writeUint16;
|
|
|
|
using super::writeUint32;
|
|
|
|
using super::writeUint64;
|
2018-03-01 20:05:13 +03:00
|
|
|
using super::writeUintptr;
|
2013-01-31 01:45:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class LittleEndian final : public detail::EndianReadWrite<detail::Little> {};
|
2013-01-31 01:45:39 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class BigEndian final : public detail::EndianReadWrite<detail::Big> {};
|
2013-01-31 01:45:39 +04:00
|
|
|
|
|
|
|
typedef BigEndian NetworkEndian;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class NativeEndian final : public detail::Endian<MOZ_NATIVE_ENDIANNESS> {
|
2014-06-13 10:34:08 +04:00
|
|
|
private:
|
|
|
|
typedef detail::Endian<MOZ_NATIVE_ENDIANNESS> super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* These functions are intended for cases where you have data in your
|
|
|
|
* native-endian format and you need the data to appear in the appropriate
|
|
|
|
* endianness for transmission, serialization, etc.
|
|
|
|
*/
|
|
|
|
using super::copyAndSwapToBigEndian;
|
|
|
|
using super::copyAndSwapToLittleEndian;
|
|
|
|
using super::copyAndSwapToNetworkOrder;
|
|
|
|
using super::swapToBigEndian;
|
|
|
|
using super::swapToBigEndianInPlace;
|
|
|
|
using super::swapToLittleEndian;
|
|
|
|
using super::swapToLittleEndianInPlace;
|
|
|
|
using super::swapToNetworkOrder;
|
|
|
|
using super::swapToNetworkOrderInPlace;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These functions are intended for cases where you have data in the
|
|
|
|
* given endianness (e.g. reading from disk or a file-format) and you
|
|
|
|
* need the data to appear in native-endian format for processing.
|
|
|
|
*/
|
|
|
|
using super::copyAndSwapFromBigEndian;
|
|
|
|
using super::copyAndSwapFromLittleEndian;
|
|
|
|
using super::copyAndSwapFromNetworkOrder;
|
|
|
|
using super::swapFromBigEndian;
|
|
|
|
using super::swapFromBigEndianInPlace;
|
|
|
|
using super::swapFromLittleEndian;
|
|
|
|
using super::swapFromLittleEndianInPlace;
|
|
|
|
using super::swapFromNetworkOrder;
|
|
|
|
using super::swapFromNetworkOrderInPlace;
|
2013-01-31 01:45:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef MOZ_NATIVE_ENDIANNESS
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2016-05-22 23:31:11 +03:00
|
|
|
#endif /* mozilla_EndianUtils_h */
|