]> sigrok.org Git - libsigrok.git/commitdiff
binary_helpers: Add bv_get_value function.
authorMathieu Pilato <redacted>
Fri, 31 Mar 2023 08:09:12 +0000 (10:09 +0200)
committerGerhard Sittig <redacted>
Sun, 30 Apr 2023 09:34:07 +0000 (11:34 +0200)
src/binary_helpers.c
src/libsigrok-internal.h

index 72c05ef101a0cbc33e161971d0a32ae95a16441c..7482371cf577e7aa48fda36bfd2d59a7e0dd5a80 100644 (file)
@@ -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;
+}
index b47a8e5d6e0542126d946cdf8ae8ae0a827684f2..bb2fc6ccd5db8accd73b01ffaa1d3bc74727a966 100644 (file)
@@ -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