|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/uds-releases/jasper/src/uds/numeric.h#2 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef NUMERIC_H
|
|
Packit Service |
310c69 |
#define NUMERIC_H 1
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "compiler.h"
|
|
Packit Service |
310c69 |
#include "numericDefs.h"
|
|
Packit Service |
310c69 |
#include "typeDefs.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#if !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__) \
|
|
Packit Service |
310c69 |
|| !defined(__BYTE_ORDER__)
|
|
Packit Service |
310c69 |
#error "GCC byte order macros not defined?"
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Define a type describing an integer value that is only byte-aligned
|
|
Packit Service |
310c69 |
* and may explicitly alias other types. GCC keeps getting better
|
|
Packit Service |
310c69 |
* about type-based alias analysis (both for optimization and for
|
|
Packit Service |
310c69 |
* warnings), so simply casting a pointer to pointer-to-uintXX_t isn't
|
|
Packit Service |
310c69 |
* good enough.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* C is okay with defining the structures directly in a cast, but
|
|
Packit Service |
310c69 |
* C++ is not, and we use this header in some C++ code internally.
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
#define UNALIGNED_WRAPPER(TYPE) \
|
|
Packit Service |
310c69 |
unaligned_wrap_##TYPE
|
|
Packit Service |
310c69 |
#define UNALIGNED_WRAPPER_DEF(TYPE) \
|
|
Packit Service |
310c69 |
typedef struct __attribute__((packed, may_alias)) { TYPE value; } \
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER(TYPE)
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER_DEF(int64_t);
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER_DEF(uint64_t);
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER_DEF(int32_t);
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER_DEF(uint32_t);
|
|
Packit Service |
310c69 |
UNALIGNED_WRAPPER_DEF(uint16_t);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#define GET_UNALIGNED(TYPE,ADDR) \
|
|
Packit Service |
310c69 |
(((const UNALIGNED_WRAPPER(TYPE) *)(ADDR))->value)
|
|
Packit Service |
310c69 |
#define PUT_UNALIGNED(TYPE,ADDR,VALUE) \
|
|
Packit Service |
310c69 |
(((UNALIGNED_WRAPPER(TYPE) *)(ADDR))->value = (VALUE))
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the minimum of two ints.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first int
|
|
Packit Service |
310c69 |
* @param b The second int
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The lesser of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE int minInt(int a, int b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a < b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the maximum of two ints.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first int
|
|
Packit Service |
310c69 |
* @param b The second int
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The greater of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE int maxInt(int a, int b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a > b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the maximum of two unsigned ints.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first value
|
|
Packit Service |
310c69 |
* @param b The second value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The greater of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE unsigned int maxUInt(unsigned int a, unsigned int b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a > b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the maximum of two signed longs.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first int
|
|
Packit Service |
310c69 |
* @param b The second int
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The greater of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE long maxLong(long a, long b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a > b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the maximum of two unsigned longs.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first int
|
|
Packit Service |
310c69 |
* @param b The second int
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The greater of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE unsigned long maxULong(unsigned long a, unsigned long b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a > b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the minimum of two size_ts.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first size_t
|
|
Packit Service |
310c69 |
* @param b The second size_t
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The lesser of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE size_t minSizeT(size_t a, size_t b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a < b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the maximum of two size_ts.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first size_t
|
|
Packit Service |
310c69 |
* @param b The second size_t
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The greater of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE size_t maxSizeT(size_t a, size_t b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a > b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Find the minimum of two uint64_ts.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a The first uint64_t
|
|
Packit Service |
310c69 |
* @param b The second uint64_t
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The lesser of a and b
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint64_t minUInt64(uint64_t a, uint64_t b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return ((a < b) ? a : b);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Multiply two uint64_t and check for overflow. Does division.
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
bool multiplyWouldOverflow(uint64_t a, uint64_t b);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit unsigned number from a buffer stored in
|
|
Packit Service |
310c69 |
* big-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint64_t getUInt64BE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint64_t num = GET_UNALIGNED(uint64_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit unsigned big-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt64BE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint64_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt64BE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 64 bit unsigned number in a buffer in
|
|
Packit Service |
310c69 |
* big-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt64BE(byte* data, uint64_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint64_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 64 bit unsigned number into a buffer at a given offset
|
|
Packit Service |
310c69 |
* using a big-endian representation. The offset will be advanced to
|
|
Packit Service |
310c69 |
* first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt64BE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint64_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt64BE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit unsigned number from a buffer stored in big-endian
|
|
Packit Service |
310c69 |
* representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint32_t getUInt32BE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint32_t num = GET_UNALIGNED(uint32_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit unsigned big-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt32BE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint32_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt32BE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 32 bit number in a buffer in
|
|
Packit Service |
310c69 |
* big-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt32BE(byte* data, uint32_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint32_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 32 bit number into a buffer at a given offset using a
|
|
Packit Service |
310c69 |
* big-endian representation. The offset will be advanced to first byte
|
|
Packit Service |
310c69 |
* after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt32BE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint32_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt32BE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 16 bit number from a buffer stored in
|
|
Packit Service |
310c69 |
* big-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint16_t getUInt16BE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint16_t num = GET_UNALIGNED(uint16_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = bswap_16(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 16 bit, big-endian number from a buffer at a specified offset.
|
|
Packit Service |
310c69 |
* The offset will be advanced to the first byte after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to
|
|
Packit Service |
310c69 |
* extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt16BE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint16_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt16BE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint16_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 16 bit number in a buffer in
|
|
Packit Service |
310c69 |
* big-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt16BE(byte* data, uint16_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = bswap_16(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint16_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 16 bit number into a buffer at a given offset using a
|
|
Packit Service |
310c69 |
* big-endian representation. The offset will be advanced to first byte
|
|
Packit Service |
310c69 |
* after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt16BE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint16_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt16BE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint16_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit signed number from a buffer stored in
|
|
Packit Service |
310c69 |
* little-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE int64_t getInt64LE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
int64_t num = GET_UNALIGNED(int64_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit signed little-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeInt64LE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
int64_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getInt64LE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(int64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a signed 64 bit number in a buffer in little-endian
|
|
Packit Service |
310c69 |
* representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeInt64LE(byte* data, int64_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(int64_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 64 bit signed number into a buffer at a given offset using
|
|
Packit Service |
310c69 |
* a little-endian representation. The offset will be advanced to
|
|
Packit Service |
310c69 |
* first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeInt64LE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
int64_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeInt64LE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(int64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit number from a buffer stored in
|
|
Packit Service |
310c69 |
* little-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint64_t getUInt64LE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint64_t num = GET_UNALIGNED(uint64_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 64 bit unsigned little-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt64LE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint64_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt64LE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 64 bit unsigned number in a buffer in little-endian
|
|
Packit Service |
310c69 |
* representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt64LE(byte* data, uint64_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap64(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint64_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 64 bit unsigned number into a buffer at a given offset
|
|
Packit Service |
310c69 |
* using a little-endian representation. The offset will be advanced
|
|
Packit Service |
310c69 |
* to first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt64LE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint64_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt64LE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint64_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit signed number from a buffer stored in
|
|
Packit Service |
310c69 |
* little-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE int32_t getInt32LE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
int32_t num = GET_UNALIGNED(int32_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit signed little-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeInt32LE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
int32_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getInt32LE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(int32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a signed 32 bit number in a buffer in little-endian
|
|
Packit Service |
310c69 |
* representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeInt32LE(byte* data, int32_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(int32_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 32 bit signed number into a buffer at a given offset using
|
|
Packit Service |
310c69 |
* a little-endian representation. The offset will be advanced to
|
|
Packit Service |
310c69 |
* first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeInt32LE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
int32_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeInt32LE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(int32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit unsigned number from a buffer stored in
|
|
Packit Service |
310c69 |
* little-endian representation.
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint32_t getUInt32LE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint32_t num = GET_UNALIGNED(uint32_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 32 bit unsigned little-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt32LE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint32_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt32LE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 32 bit unsigned number in a buffer in little-endian
|
|
Packit Service |
310c69 |
* representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt32LE(byte* data, uint32_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = __builtin_bswap32(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint32_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 32 bit unsigned number into a buffer at a given offset
|
|
Packit Service |
310c69 |
* using a little-endian representation. The offset will be advanced
|
|
Packit Service |
310c69 |
* to first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt32LE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint32_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt32LE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint32_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 16 bit number from a buffer stored in
|
|
Packit Service |
310c69 |
* little-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer from which to extract the number
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return The extracted quantity
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result))
|
|
Packit Service |
310c69 |
static INLINE uint16_t getUInt16LE(const byte* data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
uint16_t num = GET_UNALIGNED(uint16_t, data);
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = bswap_16(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
return num;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Extract a 16 bit unsigned little-endian number from a buffer at a
|
|
Packit Service |
310c69 |
* specified offset. The offset will be advanced to the first byte
|
|
Packit Service |
310c69 |
* after the number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param buffer The buffer from which to extract the number
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset into the buffer at which to
|
|
Packit Service |
310c69 |
* extract
|
|
Packit Service |
310c69 |
* @param decoded A pointer to hold the extracted number
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void decodeUInt16LE(const byte *buffer,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint16_t *decoded)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
*decoded = getUInt16LE(buffer + *offset);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint16_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Store a 16 bit number in a buffer in little-endian representation.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer in which to store the number
|
|
Packit Service |
310c69 |
* @param num The number to store
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void storeUInt16LE(byte* data, uint16_t num)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
|
|
Packit Service |
310c69 |
num = bswap_16(num);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
PUT_UNALIGNED(uint16_t, data, num);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Encode a 16 bit unsigned number into a buffer at a given offset
|
|
Packit Service |
310c69 |
* using a little-endian representation. The offset will be advanced
|
|
Packit Service |
310c69 |
* to first byte after the encoded number.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param data The buffer to encode into
|
|
Packit Service |
310c69 |
* @param offset A pointer to the offset at which to start encoding
|
|
Packit Service |
310c69 |
* @param toEncode The number to encode
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE void encodeUInt16LE(byte *data,
|
|
Packit Service |
310c69 |
size_t *offset,
|
|
Packit Service |
310c69 |
uint16_t toEncode)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
storeUInt16LE(data + *offset, toEncode);
|
|
Packit Service |
310c69 |
*offset += sizeof(uint16_t);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Special function wrapper required for compile-time assertions. This
|
|
Packit Service |
310c69 |
* function will fail to compile if any of the uint*_t types are not of the
|
|
Packit Service |
310c69 |
* size we expect. This function should never be called.
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void numericCompileTimeAssertions(void);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#endif /* NUMERIC_H */
|