|
Embedded SDK
Embedded SDK
|
#include <stddef.h>#include <stdint.h>#include <stdbool.h>#include "tlv_internals.h"#include "buffer.h"

Go to the source code of this file.
Classes | |
| struct | tlv_data_s |
| struct | _internal_tlv_handler_t |
Macros | |
| #define | DEFINE_TLV_PARSER(TAG_LIST, COMMON_HANDLER, PARSE_FUNCTION_NAME) |
| Creates a parser function for a given TLV use case. | |
| #define | TLV_CHECK_RECEIVED_TAGS(received, ...) |
Typedefs | |
| typedef struct tlv_data_s | tlv_data_t |
| typedef bool() | tlv_handler_cb_t(const tlv_data_t *data, void *tlv_extracted) |
| typedef TLV_reception_internal_t | TLV_reception_t |
| typedef enum tag_unicity_e | tag_unicity_t |
Enumerations | |
| enum | tag_unicity_e { ENFORCE_UNIQUE_TAG , ALLOW_MULTIPLE_TAG } |
Functions | |
| bool | tlv_enforce_u8_value (const tlv_data_t *data, uint8_t expected_value) |
| Enforce that a TLV data contains the expected uint8 value. | |
| bool | get_uint64_t_from_tlv_data (const tlv_data_t *data, uint64_t *value) |
| Extract a uint64_t value from TLV data. | |
| bool | get_uint32_t_from_tlv_data (const tlv_data_t *data, uint32_t *value) |
| Extract a uint32_t value from TLV data. | |
| bool | get_uint16_t_from_tlv_data (const tlv_data_t *data, uint16_t *value) |
| Extract a uint16_t value from TLV data. | |
| bool | get_uint8_t_from_tlv_data (const tlv_data_t *data, uint8_t *value) |
| Extract a uint8_t value from TLV data. | |
| bool | get_bool_from_tlv_data (const tlv_data_t *data, bool *value) |
| Extract a boolean value from TLV data. | |
| bool | get_buffer_from_tlv_data (const tlv_data_t *data, buffer_t *out, uint16_t min_size, uint16_t max_size) |
| Extract a sized buffer from TLV data (zero-copy). | |
| bool | get_string_from_tlv_data (const tlv_data_t *data, char *out, uint16_t min_length, uint16_t out_size) |
| Copy a NUL-terminated string from TLV data. | |
| bool | tlv_check_received_tags (TLV_reception_t received, const TLV_tag_t *tags, size_t tag_count) |
Check that every tag in tags was received during parsing. | |
| bool | _parse_tlv_internal (const _internal_tlv_handler_t *handlers, uint8_t handlers_count, tlv_handler_cb_t *common_handler, tag_to_flag_function_t *tag_to_flag_function, const buffer_t *payload, void *tlv_out, TLV_reception_t *received_tags_flags) |
| Parse a raw TLV payload using a pre-built handler table. | |
| #define DEFINE_TLV_PARSER | ( | TAG_LIST, | |
| COMMON_HANDLER, | |||
| PARSE_FUNCTION_NAME | |||
| ) |
Creates a parser function for a given TLV use case.
| TAG_LIST | A X-macro of TAG and associated handles of the following format: TAG_LIST(X) \
X(<uint32_t>, TAG_NAME, <tlv_handler_cb_t>, <tag_unicity_t>)
enum tag_unicity_e tag_unicity_t bool() tlv_handler_cb_t(const tlv_data_t *data, void *tlv_extracted) Definition tlv_library.h:102 |
| COMMON_HANDLER | An optional handler to call for all tags in addition to the specific one |
| PARSE_FUNCTION_NAME | Name of the function that will parse the TAG_LIST |
Example:
Expands to:
enum { TAG_A = 0x0A, TAG_B = 0x1F, TAG_C = 0x77};enum { TAG_A_INDEX, TAG_B_INDEX, TAG_C_INDEX, my_tlv_parser_TAG_COUNT };enum { TAG_A_FLAG = 1 << 0, TAG_B_FLAG = 1 << 1, TAG_C_FLAG = 1 << 2 };my_tlv_parser_tag_to_flag()my_tlv_parser() Definition at line 144 of file tlv_library.h.
| #define TLV_CHECK_RECEIVED_TAGS | ( | received, | |
| ... | |||
| ) |
Macro wrapper of the above function to accept a list of tags as argument.
Definition at line 225 of file tlv_library.h.
| typedef enum tag_unicity_e tag_unicity_t |
| typedef struct tlv_data_s tlv_data_t |
| typedef bool() tlv_handler_cb_t(const tlv_data_t *data, void *tlv_extracted) |
Definition at line 102 of file tlv_library.h.
Definition at line 106 of file tlv_library.h.
| enum tag_unicity_e |
| Enumerator | |
|---|---|
| ENFORCE_UNIQUE_TAG | |
| ALLOW_MULTIPLE_TAG | |
Definition at line 108 of file tlv_library.h.
| bool _parse_tlv_internal | ( | const _internal_tlv_handler_t * | handlers, |
| uint8_t | handlers_count, | ||
| tlv_handler_cb_t * | common_handler, | ||
| tag_to_flag_function_t * | tag_to_flag_function, | ||
| const buffer_t * | payload, | ||
| void * | tlv_out, | ||
| TLV_reception_t * | received_tags_flags | ||
| ) |
Parse a raw TLV payload using a pre-built handler table.
This is the internal engine behind every parser generated by DEFINE_TLV_PARSER. Prefer using the generated wrapper for your use case rather than calling this directly.
| [in] | handlers | Array of per-tag handlers |
| [in] | handlers_count | Number of entries in handlers |
| [in] | common_handler | Optional handler called for every tag before the specific one (may be NULL) |
| [in] | tag_to_flag_function | Maps a tag value to its reception flag bit |
| [in] | payload | Raw TLV bytes to parse |
| [out] | tlv_out | Output struct written into by the handlers |
| [out] | received_tags_flags | Reception tracker updated as tags are processed |
Definition at line 408 of file tlv_library.c.
| bool get_bool_from_tlv_data | ( | const tlv_data_t * | data, |
| bool * | value | ||
| ) |
Extract a boolean value from TLV data.
The TLV value must be exactly 1 byte with value 0 or 1. Any other value causes the function to return false.
| [in] | data | TLV data to read from |
| [out] | value | Parsed boolean value |
data is NULL, the length is invalid, or the byte is neither 0 nor 1 Definition at line 169 of file tlv_library.c.
| bool get_buffer_from_tlv_data | ( | const tlv_data_t * | data, |
| buffer_t * | out, | ||
| uint16_t | min_size, | ||
| uint16_t | max_size | ||
| ) |
Extract a sized buffer from TLV data (zero-copy).
Populates out with a pointer into the TLV payload and its length. No data is copied; the caller must not outlive the original payload.
| [in] | data | TLV data to read from |
| [out] | out | buffer_t pointing into the TLV value bytes |
| [in] | min_size | Minimum acceptable length (0 to skip the lower-bound check) |
| [in] | max_size | Maximum acceptable length (0 to skip the upper-bound check) |
data is NULL or the length is outside the allowed range Definition at line 195 of file tlv_library.c.
| bool get_string_from_tlv_data | ( | const tlv_data_t * | data, |
| char * | out, | ||
| uint16_t | min_length, | ||
| uint16_t | out_size | ||
| ) |
Copy a NUL-terminated string from TLV data.
Copies the TLV bytes into out and appends a NUL terminator. The function rejects payloads that contain embedded NUL bytes.
| [in] | data | TLV data to read from |
| [out] | out | Destination buffer (must hold at least value.size + 1 bytes) |
| [in] | min_length | Minimum acceptable string length in bytes, excluding NUL (0 to skip the lower-bound check) |
| [in] | out_size | Size of out in bytes including the NUL terminator (0 to skip the upper-bound check) |
data is NULL, an embedded NUL is found, or the length is outside the allowed range Definition at line 233 of file tlv_library.c.
| bool get_uint16_t_from_tlv_data | ( | const tlv_data_t * | data, |
| uint16_t * | value | ||
| ) |
Extract a uint16_t value from TLV data.
Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT16_MAX.
| [in] | data | TLV data to read from |
| [out] | value | Parsed uint16_t value |
data is NULL, the length is invalid, or the value overflows uint16_t Definition at line 120 of file tlv_library.c.
| bool get_uint32_t_from_tlv_data | ( | const tlv_data_t * | data, |
| uint32_t * | value | ||
| ) |
Extract a uint32_t value from TLV data.
Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT32_MAX.
| [in] | data | TLV data to read from |
| [out] | value | Parsed uint32_t value |
data is NULL, the length is invalid, or the value overflows uint32_t Definition at line 96 of file tlv_library.c.
| bool get_uint64_t_from_tlv_data | ( | const tlv_data_t * | data, |
| uint64_t * | value | ||
| ) |
Extract a uint64_t value from TLV data.
The value is interpreted as a big-endian unsigned integer. The TLV length must be between 1 and 8 bytes; shorter payloads are zero-padded. See also https://en.wikipedia.org/wiki/X.690 for DER encoding rules.
| [in] | data | TLV data to read from |
| [out] | value | Parsed uint64_t value |
data is NULL or the length is out of range Definition at line 64 of file tlv_library.c.
| bool get_uint8_t_from_tlv_data | ( | const tlv_data_t * | data, |
| uint8_t * | value | ||
| ) |
Extract a uint8_t value from TLV data.
Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT8_MAX.
| [in] | data | TLV data to read from |
| [out] | value | Parsed uint8_t value |
data is NULL, the length is invalid, or the value overflows uint8_t Definition at line 144 of file tlv_library.c.
| bool tlv_check_received_tags | ( | TLV_reception_t | received, |
| const TLV_tag_t * | tags, | ||
| size_t | tag_count | ||
| ) |
Check that every tag in tags was received during parsing.
Prefer the variadic macro TLV_CHECK_RECEIVED_TAGS which wraps this function.
| [in] | received | Tag-reception tracker filled in by the parser |
| [in] | tags | Array of tag values that must have been received |
| [in] | tag_count | Number of entries in tags |
Definition at line 349 of file tlv_library.c.
| bool tlv_enforce_u8_value | ( | const tlv_data_t * | data, |
| uint8_t | expected_value | ||
| ) |
Enforce that a TLV data contains the expected uint8 value.
| [in] | data | TLV data to check |
| [in] | expected_value | Expected uint8 value |
Definition at line 38 of file tlv_library.c.