diff options
| author | Vasile Vilvoiu <vasi@vilvoiu.ro> | 2021-11-28 17:47:09 +0200 |
|---|---|---|
| committer | Vasile Vilvoiu <vasi@vilvoiu.ro> | 2021-11-28 17:47:09 +0200 |
| commit | 2a53efebd80d4687f3137345d5680e29a79f208b (patch) | |
| tree | ec8f1b45e1691a81d16f5ff0a9a2b80d27e84f33 | |
| parent | a5d8f823bd73591d99cee9d2b13dacf8edc07a65 (diff) | |
Add API for retrieval of encoded buffer length.
Closes #7.
| -rw-r--r-- | include/ecbor.h.in | 11 | ||||
| -rw-r--r-- | src/libecbor/ecbor_encoder.c | 22 |
2 files changed, 32 insertions, 1 deletions
diff --git a/include/ecbor.h.in b/include/ecbor.h.in index 2f71dc9..b9e4726 100644 --- a/include/ecbor.h.in +++ b/include/ecbor.h.in @@ -40,6 +40,7 @@ typedef enum { ECBOR_ERR_NULL_VALUE = 14, ECBOR_ERR_NULL_ARRAY = 15, ECBOR_ERR_NULL_MAP = 16, + ECBOR_ERR_NULL_PARAMETER = 17, ECBOR_ERR_NULL_ITEM = 20, @@ -169,6 +170,9 @@ typedef enum { typedef struct { /* mode of operation for this context */ ecbor_mode_t mode; + + /* output buffer base pointer */ + uint8_t *base; /* output buffer position */ uint8_t *out_position; @@ -235,6 +239,9 @@ ecbor_initialize_decode_tree (ecbor_decode_context_t *context, extern ecbor_error_t ecbor_encode (ecbor_encode_context_t *context, ecbor_item_t *item); +extern ecbor_error_t +ecbor_get_encoded_buffer_size(const ecbor_encode_context_t *context, size_t *out_size); + /* * Decoding routines */ @@ -407,6 +414,8 @@ ecbor_get_bool (ecbor_item_t *item, uint8_t *value); /* * Inline API */ +#define ECBOR_GET_ENCODED_BUFFER_SIZE(c) \ + (size_t)((c)->out_position - (c)->base) #define ECBOR_GET_TYPE(i) \ ((i)->type) #define ECBOR_GET_LENGTH(i) \ @@ -472,4 +481,4 @@ ecbor_get_bool (ecbor_item_t *item, uint8_t *value); } #endif -#endif
\ No newline at end of file +#endif diff --git a/src/libecbor/ecbor_encoder.c b/src/libecbor/ecbor_encoder.c index 2b01094..eca207a 100644 --- a/src/libecbor/ecbor_encoder.c +++ b/src/libecbor/ecbor_encoder.c @@ -18,6 +18,7 @@ ecbor_initialize_encode (ecbor_encode_context_t *context, return ECBOR_ERR_NULL_OUTPUT_BUFFER; } + context->base = buffer; context->out_position = buffer; context->bytes_left = buffer_size; context->mode = ECBOR_MODE_ENCODE; @@ -35,6 +36,7 @@ ecbor_initialize_encode_streamed (ecbor_encode_context_t *context, return ECBOR_ERR_NULL_OUTPUT_BUFFER; } + context->base = buffer; context->out_position = buffer; context->bytes_left = buffer_size; context->mode = ECBOR_MODE_ENCODE_STREAMED; @@ -42,6 +44,26 @@ ecbor_initialize_encode_streamed (ecbor_encode_context_t *context, return ECBOR_OK; } + +ecbor_error_t +ecbor_get_encoded_buffer_size(const ecbor_encode_context_t *context, size_t *out_size) +{ + /* check parameters */ + if (context == NULL) { + return ECBOR_ERR_NULL_CONTEXT; + } + if (out_size == NULL) { + return ECBOR_ERR_NULL_PARAMETER; + } + /* check sanity */ + if (context->base > context->out_position) { + return ECBOR_ERR_INVALID_END_OF_BUFFER; + } + /* return length */ + *out_size = context->out_position - context->base; + return ECBOR_OK; +} + static ecbor_error_t ecbor_encode_uint (ecbor_encode_context_t *context, uint8_t major_type, uint64_t value) |
