From: Aurelien Jacobs Date: Wed, 11 Dec 2013 23:20:47 +0000 (+0100) Subject: endian neutral helper macro to read 16/32 bits integer from unaligned memory X-Git-Tag: libsigrok-0.3.0~413 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e28ef28a3c9a5cd2c86e4ab4de2516ab82d91082;p=libsigrok.git endian neutral helper macro to read 16/32 bits integer from unaligned memory --- diff --git a/libsigrok-internal.h b/libsigrok-internal.h index c76f5983..82d6d791 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -50,6 +50,42 @@ #define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a) #endif +/** + * Read a 16 bits big endian integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding integer + */ +#define RB16(x) ((((const uint8_t*)(x))[0] << 8) | \ + ((const uint8_t*)(x))[1]) + +/** + * Read a 16 bits little endian integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding integer + */ +#define RL16(x) ((((const uint8_t*)(x))[1] << 8) | \ + ((const uint8_t*)(x))[0]) + +/** + * Read a 32 bits big endian integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding integer + */ +#define RB32(x) ((((const uint8_t*)(x))[0] << 24) | \ + (((const uint8_t*)(x))[1] << 16) | \ + (((const uint8_t*)(x))[2] << 8) | \ + ((const uint8_t*)(x))[3]) + +/** + * Read a 32 bits little endian integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding integer + */ +#define RL32(x) ((((const uint8_t*)(x))[3] << 24) | \ + (((const uint8_t*)(x))[2] << 16) | \ + (((const uint8_t*)(x))[1] << 8) | \ + ((const uint8_t*)(x))[0]) + /* Portability fixes for FreeBSD. */ #ifdef __FreeBSD__ #define LIBUSB_CLASS_APPLICATION 0xfe