2015-03-03 00:38:33 +08:00
|
|
|
/****************************************************************************
|
|
|
|
** GeomIO Library
|
|
|
|
** Copyright FougSys (2 Mar. 2015)
|
|
|
|
** contact@fougsys.fr
|
|
|
|
**
|
|
|
|
** This software is a reusable library whose purpose is to provide complete
|
|
|
|
** I/O support for various CAD file formats (eg. STL)
|
|
|
|
**
|
|
|
|
** This software is governed by the CeCILL-B license under French law and
|
|
|
|
** abiding by the rules of distribution of free software. You can use,
|
|
|
|
** modify and/ or redistribute the software under the terms of the CeCILL-B
|
|
|
|
** license as circulated by CEA, CNRS and INRIA at the following URL
|
2015-03-30 15:05:25 +08:00
|
|
|
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
|
2015-03-03 00:38:33 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/* WARNING :
|
|
|
|
* this header has no multi-inclusion guard. It must be included only once
|
|
|
|
* in the translation unit of use. The reason is that all functions
|
|
|
|
* defined here are meant to be inlined for performance purpose
|
2014-01-29 02:06:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../global.h"
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Reads a 16bit integer from memory-location \p bytes (little-endian) */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static uint16_t gmio_decode_uint16_le(const uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* |0 |1 | */
|
|
|
|
/* |BB|AA| -> 0xAABB */
|
|
|
|
return (bytes[1] << 8) | bytes[0];
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Reads a 16bit integer from memory-location \p bytes (big-endian) */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static uint16_t gmio_decode_uint16_be(const uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* |0 |1 | */
|
|
|
|
/* |AA|BB| -> 0xAABB */
|
|
|
|
return (bytes[0] << 8) | bytes[1];
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Writes 16bit integer \p val to the memory location at \p bytes in
|
|
|
|
* little-endian */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static void gmio_encode_uint16_le(uint16_t val, uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
bytes[0] = val & 0xFF;
|
|
|
|
bytes[1] = (val >> 8) & 0xFF;
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Reads a 32bit integer from memory-location \p bytes (little-endian) */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static uint32_t gmio_decode_uint32_le(const uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* |DD|CC|BB|AA| -> 0xAABBCCDD */
|
|
|
|
return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Reads a 32bit integer from memory-location \p bytes (mixed-endian) */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static uint32_t gmio_decode_uint32_me(const uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* |DD|CC|BB|AA| -> 0xCCDDAABB */
|
|
|
|
return (bytes[0] << 16) | (bytes[1] << 24) | (bytes[3] << 8) | bytes[2];
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Reads a 32bit integer from memory-location \p bytes (big-endian) */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static uint32_t gmio_decode_uint32_be(const uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* |DD|CC|BB|AA| -> 0xDDCCBBAA */
|
|
|
|
return bytes[3] | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Writes 32bit integer \p val to the memory location at \p bytes in
|
|
|
|
* little-endian */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static void gmio_encode_uint32_le(uint32_t val, uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
bytes[0] = val & 0xFF;
|
|
|
|
bytes[1] = (val >> 8) & 0xFF;
|
|
|
|
bytes[2] = (val >> 16) & 0xFF;
|
|
|
|
bytes[3] = (val >> 24) & 0xFF;
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:35:15 +08:00
|
|
|
/*! Writes 32bit integer \p val to the memory location at \p bytes in
|
|
|
|
* big-endian */
|
2014-03-28 23:33:35 +08:00
|
|
|
GMIO_INLINE static void gmio_encode_uint32_be(uint32_t val, uint8_t* bytes)
|
2014-01-29 02:06:24 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
bytes[0] = (val >> 24) & 0xFF;
|
|
|
|
bytes[1] = (val >> 16) & 0xFF;
|
|
|
|
bytes[2] = (val >> 8) & 0xFF;
|
|
|
|
bytes[3] = val & 0xFF;
|
2014-01-29 02:06:24 +08:00
|
|
|
}
|