/********************************************************************************
*
* Copyright (c) Simon Downey <simon@ark.io> <https://github.com/sleepdefic1t>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************/
#include <stdint.h>
//////////////////////////////////////////////////////////////////////
#define FLAG_64_BYTE 0xFFFFFFFFFFFFFFFF
#define FLAG_32_BYTE 0xFFFFFFFF
#define FLAG_16_BYTE 0xFFFF
#define FLAG_8_BYTE 0xFF
//////////////////////////////////////////////////////////////////////
uint16_t unpack2LE(uint8_t *src) {
return ((uint16_t)src[0] & FLAG_8_BYTE) |
((uint16_t)src[1] & FLAG_8_BYTE) << 8U;
}
uint32_t unpack4LE(uint8_t *src) {
return ((uint32_t)src[0] & FLAG_8_BYTE) |
((uint32_t)src[1] & FLAG_8_BYTE) << 8U |
((uint32_t)src[2] & FLAG_16_BYTE) << 16U |
((uint32_t)src[3] & FLAG_32_BYTE) << 24U;
}
uint64_t unpack8LE(uint8_t *src) {
return ((uint64_t)src[0] & FLAG_8_BYTE) |
((uint64_t)src[1] & FLAG_8_BYTE) << 8U |
((uint64_t)src[2] & FLAG_16_BYTE) << 16U |
((uint64_t)src[3] & FLAG_32_BYTE) << 24U |
((uint64_t)src[4] & FLAG_32_BYTE) << 32U |
((uint64_t)src[5] & FLAG_64_BYTE) << 40U |
((uint64_t)src[6] & FLAG_64_BYTE) << 48U |
((uint64_t)src[7] & FLAG_64_BYTE) << 56U;
}
//////////////////////////////////////////////////////////////////////
uint16_t unpack2BE(uint8_t *src) {
return ((uint16_t)src[0] & FLAG_8_BYTE) << 8U |
((uint16_t)src[1] & FLAG_8_BYTE);
}
uint32_t unpack4BE(uint8_t *src) {
return ((uint32_t)src[0] & FLAG_32_BYTE) << 24U |
((uint32_t)src[1] & FLAG_16_BYTE) << 16U |
((uint32_t)src[2] & FLAG_8_BYTE) << 8U |
((uint32_t)src[3] & FLAG_8_BYTE);
}
uint64_t unpack8BE(uint8_t *src) {
return ((uint64_t)src[0] & FLAG_64_BYTE) << 56U |
((uint64_t)src[1] & FLAG_64_BYTE) << 48U |
((uint64_t)src[2] & FLAG_64_BYTE) << 40U |
((uint64_t)src[3] & FLAG_32_BYTE) << 32U |
((uint64_t)src[4] & FLAG_32_BYTE) << 24U |
((uint64_t)src[5] & FLAG_16_BYTE) << 16U |
((uint64_t)src[6] & FLAG_8_BYTE) << 8U |
((uint64_t)src[7] & FLAG_8_BYTE);
}
//////////////////////////////////////////////////////////////////////