CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
tlv.h
Go to the documentation of this file.
1
21
22#pragma once
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
39
45size_t tlv_tag_size(uint16_t tag);
46
52size_t tlv_len_size(size_t value_len);
53
63tlv_status_t tlv_write_tag(uint8_t *buf, size_t buf_max, uint16_t tag,
64 size_t *written);
65
78tlv_status_t tlv_write_len(uint8_t *buf, size_t buf_max, size_t value_len,
79 size_t *written);
80
92tlv_status_t tlv_build(uint8_t *buf, size_t buf_max, uint16_t tag,
93 const uint8_t *value, size_t value_len, size_t *written);
94
106tlv_status_t tlv_read_tag(const uint8_t *buf, size_t buf_len, size_t *pos,
107 uint16_t *tag_out);
108
119tlv_status_t tlv_read_len(const uint8_t *buf, size_t buf_len, size_t *pos,
120 size_t *length_out);
121
123typedef struct {
124 uint16_t tag;
125 size_t length;
126 const uint8_t *value;
127 size_t total_size;
128} tlv_t;
129
138tlv_status_t tlv_parse(const uint8_t *buf, size_t buf_len, size_t *pos, tlv_t *out);
139
140#ifdef __cplusplus
141}
142#endif
143
Parsed TLV: pointers alias into the caller-supplied buffer.
Definition tlv.h:123
size_t length
Definition tlv.h:125
size_t total_size
Definition tlv.h:127
uint16_t tag
Definition tlv.h:124
const uint8_t * value
Definition tlv.h:126
tlv_status_t tlv_parse(const uint8_t *buf, size_t buf_len, size_t *pos, tlv_t *out)
Parse a single TLV starting at pos. On success pos advances past the entire field and out is filled.
Definition tlv.cpp:122
tlv_status_t tlv_write_tag(uint8_t *buf, size_t buf_max, uint16_t tag, size_t *written)
Encode a tag. Writes either one or two bytes depending on the magnitude of tag.
Definition tlv.cpp:19
tlv_status_t tlv_build(uint8_t *buf, size_t buf_max, uint16_t tag, const uint8_t *value, size_t value_len, size_t *written)
Build a complete TLV: tag, length, and value (which may be NULL when value_len == 0).
Definition tlv.cpp:54
tlv_status_t tlv_write_len(uint8_t *buf, size_t buf_max, size_t value_len, size_t *written)
Encode a BER definite length field.
Definition tlv.cpp:34
tlv_status_t tlv_read_tag(const uint8_t *buf, size_t buf_len, size_t *pos, uint16_t *tag_out)
Read a tag from the input buffer at pos.
Definition tlv.cpp:76
tlv_status_t
BER-TLV codec for OpenPGP Data Objects.
Definition tlv.h:32
@ TLV_ERR_BUF_TOO_SMALL
Definition tlv.h:34
@ TLV_ERR_BAD_TAG
Definition tlv.h:36
@ TLV_ERR_NULL
Definition tlv.h:37
@ TLV_ERR_BAD_LENGTH
Definition tlv.h:35
@ TLV_OK
Definition tlv.h:33
size_t tlv_len_size(size_t value_len)
Compute the BER definite-length encoded size for a length value.
Definition tlv.cpp:13
tlv_status_t tlv_read_len(const uint8_t *buf, size_t buf_len, size_t *pos, size_t *length_out)
Read a BER definite length field starting at pos.
Definition tlv.cpp:98
size_t tlv_tag_size(uint16_t tag)
Compute the encoded size of a tag.
Definition tlv.cpp:9