Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
Macros | Typedefs | Enumerations | Functions
tlv_library.c File Reference
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "os_utils.h"
#include "os_pic.h"
#include "cx.h"
#include "tlv_library.h"
Include dependency graph for tlv_library.c:

Go to the source code of this file.

Macros

#define DER_LONG_FORM_FLAG   0x80
 
#define DER_FIRST_BYTE_VALUE_MASK   0x7f
 

Typedefs

typedef enum tlv_step_e tlv_step_t
 

Enumerations

enum  tlv_step_e { TLV_TAG , TLV_LENGTH , TLV_VALUE }
 

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.
 
static bool get_der_value_as_uint32 (const buffer_t *payload, size_t *offset, uint32_t *value)
 
static bool get_der_value_as_uint16 (const buffer_t *payload, size_t *offset, uint16_t *value)
 Parse a DER-encoded value and fit it into a uint16_t, or fail.
 
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.
 
static const _internal_tlv_handler_tfind_handler (const _internal_tlv_handler_t *handlers, uint8_t handlers_count, TLV_tag_t tag)
 Find the handler registered for a given tag.
 
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.
 

Macro Definition Documentation

◆ DER_FIRST_BYTE_VALUE_MASK

#define DER_FIRST_BYTE_VALUE_MASK   0x7f

Definition at line 29 of file tlv_library.c.

◆ DER_LONG_FORM_FLAG

#define DER_LONG_FORM_FLAG   0x80

Definition at line 28 of file tlv_library.c.

Typedef Documentation

◆ tlv_step_t

typedef enum tlv_step_e tlv_step_t

Enumeration Type Documentation

◆ tlv_step_e

enum tlv_step_e
Enumerator
TLV_TAG 
TLV_LENGTH 
TLV_VALUE 

Definition at line 386 of file tlv_library.c.

Function Documentation

◆ _parse_tlv_internal()

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.

Parameters
[in]handlersArray of per-tag handlers
[in]handlers_countNumber of entries in handlers
[in]common_handlerOptional handler called for every tag before the specific one (may be NULL)
[in]tag_to_flag_functionMaps a tag value to its reception flag bit
[in]payloadRaw TLV bytes to parse
[out]tlv_outOutput struct written into by the handlers
[out]received_tags_flagsReception tracker updated as tags are processed
Returns
true on success, false on any parse error or handler failure

Definition at line 408 of file tlv_library.c.

◆ find_handler()

static const _internal_tlv_handler_t * find_handler ( const _internal_tlv_handler_t handlers,
uint8_t  handlers_count,
TLV_tag_t  tag 
)
static

Find the handler registered for a given tag.

Parameters
[in]handlersArray of tag handlers
[in]handlers_countNumber of entries in handlers
[in]tagTag value to look up
Returns
Pointer to the matching handler, or NULL if no handler is registered for tag

Definition at line 373 of file tlv_library.c.

◆ get_bool_from_tlv_data()

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.

Parameters
[in]dataTLV data to read from
[out]valueParsed boolean value
Returns
true on success, false if data is NULL, the length is invalid, or the byte is neither 0 nor 1

Definition at line 169 of file tlv_library.c.

◆ get_buffer_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).

Populates out with a pointer into the TLV payload and its length. No data is copied; the caller must not outlive the original payload.

Parameters
[in]dataTLV data to read from
[out]outbuffer_t pointing into the TLV value bytes
[in]min_sizeMinimum acceptable length (0 to skip the lower-bound check)
[in]max_sizeMaximum acceptable length (0 to skip the upper-bound check)
Returns
true on success, false if data is NULL or the length is outside the allowed range

Definition at line 195 of file tlv_library.c.

◆ get_der_value_as_uint16()

static bool get_der_value_as_uint16 ( const buffer_t payload,
size_t *  offset,
uint16_t *  value 
)
static

Parse a DER-encoded value and fit it into a uint16_t, or fail.

Delegates to get_der_value_as_uint32() and rejects values exceeding UINT16_MAX.

Parameters
[in]payloadDER-encoded buffer
[in,out]offsetCurrent read position, advanced past the consumed bytes
[out]valueParsed uint16_t value
Returns
true on success, false if the underlying parse fails or the value overflows uint16_t

Definition at line 325 of file tlv_library.c.

◆ get_der_value_as_uint32()

static bool get_der_value_as_uint32 ( const buffer_t payload,
size_t *  offset,
uint32_t *  value 
)
static

Parse DER-encoded value

Parses a DER-encoded value (up to 4 bytes long) https://en.wikipedia.org/wiki/X.690

Parameters
[in]payloadthe TLV payload
[in,out]offsetthe payload offset
[out]valuethe parsed value
Returns
whether it was successful

Definition at line 281 of file tlv_library.c.

◆ get_string_from_tlv_data()

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.

Parameters
[in]dataTLV data to read from
[out]outDestination buffer (must hold at least value.size + 1 bytes)
[in]min_lengthMinimum acceptable string length in bytes, excluding NUL (0 to skip the lower-bound check)
[in]out_sizeSize of out in bytes including the NUL terminator (0 to skip the upper-bound check)
Returns
true on success, false if 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.

◆ get_uint16_t_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.

Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT16_MAX.

Parameters
[in]dataTLV data to read from
[out]valueParsed uint16_t value
Returns
true on success, false if data is NULL, the length is invalid, or the value overflows uint16_t

Definition at line 120 of file tlv_library.c.

◆ get_uint32_t_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.

Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT32_MAX.

Parameters
[in]dataTLV data to read from
[out]valueParsed uint32_t value
Returns
true on success, false if data is NULL, the length is invalid, or the value overflows uint32_t

Definition at line 96 of file tlv_library.c.

◆ get_uint64_t_from_tlv_data()

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.

Parameters
[in]dataTLV data to read from
[out]valueParsed uint64_t value
Returns
true on success, false if data is NULL or the length is out of range

Definition at line 64 of file tlv_library.c.

◆ get_uint8_t_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.

Delegates to get_uint64_t_from_tlv_data() and rejects values exceeding UINT8_MAX.

Parameters
[in]dataTLV data to read from
[out]valueParsed uint8_t value
Returns
true on success, false if data is NULL, the length is invalid, or the value overflows uint8_t

Definition at line 144 of file tlv_library.c.

◆ tlv_check_received_tags()

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.

Warning
Do not mix tags belonging to different TLV use cases; the flag mapping is use-case-specific and results would be undefined.
Parameters
[in]receivedTag-reception tracker filled in by the parser
[in]tagsArray of tag values that must have been received
[in]tag_countNumber of entries in tags
Returns
true if every listed tag was received, false if any is missing

Definition at line 349 of file tlv_library.c.

◆ tlv_enforce_u8_value()

bool tlv_enforce_u8_value ( const tlv_data_t data,
uint8_t  expected_value 
)

Enforce that a TLV data contains the expected uint8 value.

Parameters
[in]dataTLV data to check
[in]expected_valueExpected uint8 value
Returns
true if the value matches, false otherwise

Definition at line 38 of file tlv_library.c.