From: Mathieu Pilato Date: Fri, 31 Mar 2023 08:09:12 +0000 (+0200) Subject: binary_helpers: Add bv_get_value function. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=9697b4a5a4be6639909fe0f10bd10f8a486e036b;p=libsigrok.git binary_helpers: Add bv_get_value function. --- diff --git a/src/binary_helpers.c b/src/binary_helpers.c index 72c05ef1..7482371c 100644 --- a/src/binary_helpers.c +++ b/src/binary_helpers.c @@ -62,3 +62,54 @@ SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec, *out = value; return SR_OK; } + +SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, + const uint8_t *data) +{ + float value; + const uint8_t *ptr; + + ptr = &data[spec->offset]; + + switch (spec->type) { + case BVT_UINT8: + value = read_u8(ptr); + break; + case BVT_BE_UINT16: + value = read_u16be(ptr); + break; + case BVT_BE_UINT24: + value = read_u24be(ptr); + break; + case BVT_BE_UINT32: + value = read_u32be(ptr); + break; + case BVT_BE_UINT64: + value = read_u64be(ptr); + break; + case BVT_BE_FLOAT: + value = read_fltbe(ptr); + break; + case BVT_LE_UINT16: + value = read_u16le(ptr); + break; + case BVT_LE_UINT24: + value = read_u24le(ptr); + break; + case BVT_LE_UINT32: + value = read_u32le(ptr); + break; + case BVT_LE_UINT64: + value = read_u64le(ptr); + break; + case BVT_LE_FLOAT: + value = read_fltle(ptr); + break; + default: + return SR_ERR_ARG; + } + + if (out) + *out = value; + return SR_OK; +} diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index b47a8e5d..bb2fc6cc 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -2253,6 +2253,18 @@ struct binary_value_spec { SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec, const uint8_t *data, size_t length); +/** + * Read extract a value from a binary data image, without bound check. + * + * @param[out] out Pointer to output buffer (conversion result) + * @param[in] spec Binary value specification + * @param[in] data Pointer to binary input data + * + * @return SR_OK on success, SR_ERR_* error code on failure. + */ +SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, + const uint8_t *data); + /*--- crc.c -----------------------------------------------------------------*/ #define SR_CRC16_DEFAULT_INIT 0xffffU