Added the method cbor_encode_raw to the API

This method allows for writing raw data directly to the encoding buffer. This can be useful if you have something stored as CBOR encoded data.

Fixes #162.

Signed-off-by: Tofik Sonono <tofiksonono@msn.com>
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
TSonono
2025-03-12 15:21:35 -07:00
committed by Thiago Macieira
parent c0aad2fb21
commit abb7b3fe35
2 changed files with 17 additions and 1 deletions
+3 -1
View File
@@ -212,7 +212,8 @@ CBOR_API const char *cbor_error_string(CborError error);
typedef enum CborEncoderAppendType
{
CborEncoderAppendCborData = 0,
CborEncoderAppendStringData = 1
CborEncoderAppendStringData = 1,
CborEncoderAppendRawData = 2
} CborEncoderAppendType;
typedef CborError (*CborEncoderWriteFunction)(void *, const void *, size_t, CborEncoderAppendType);
@@ -251,6 +252,7 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c
{ return cbor_encode_text_string(encoder, string, strlen(string)); }
CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
CBOR_API CborError cbor_encode_raw(CborEncoder *encoder, const uint8_t *raw, size_t length);
CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
{ return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
+14
View File
@@ -486,6 +486,20 @@ CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size
return encode_string(encoder, length, TextStringType << MajorTypeShift, string);
}
/**
* Puts the data of length \a length in \a raw into to the encoding buffer of \a
* encoder. This function can be used if you have stored CBOR encoded data and
* want to push it to your current encoding buffer. Be aware, you are
* responsible for the data in \a raw is valid and that the validity of the
* resulting stream after this operation remains valid.
*
* \sa CborError cbor_encode_byte_string
*/
CborError cbor_encode_raw(CborEncoder *encoder, const uint8_t *raw, size_t length)
{
return append_to_buffer(encoder, raw, length, CborEncoderAppendRawData);
}
#ifdef __GNUC__
__attribute__((noinline))
#endif