23 lines
973 B
C
23 lines
973 B
C
/* Generic decode macro:
|
|
* DECODE_GENERIC(block, dest, count, bytes, type, is_big_endian)
|
|
* - block: uint8_t array pointer
|
|
* - dest: destination array (type[])
|
|
* - count: number of words
|
|
* - bytes: bytes per word (e.g., 4 or 8)
|
|
* - type: integer type (e.g., uint32_t, uint64_t)
|
|
* - is_big_endian: 1 for big-endian decode, 0 for little-endian decode
|
|
*/
|
|
#define DECODE_GENERIC(block, dest, count, bytes, type, is_big_endian) \
|
|
do { \
|
|
for (size_t _i = 0; _i < (count); ++_i) { \
|
|
const size_t _off = _i * (bytes); \
|
|
(dest)[_i] = (type)0; \
|
|
if (is_big_endian) { \
|
|
for (size_t _b = 0; _b < (bytes); ++_b) \
|
|
(dest)[_i] |= (type)(block[_off + _b]) << (8 * ((bytes) - 1 - _b)); \
|
|
} else { \
|
|
for (size_t _b = 0; _b < (bytes); ++_b) \
|
|
(dest)[_i] |= (type)(block[_off + _b]) << (8 * _b); \
|
|
} \
|
|
} \
|
|
} while (0)
|