Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to reset an encoder #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ struct CborEncoder
uint8_t *ptr;
ptrdiff_t bytes_needed;
} data;
uint8_t *start;
const uint8_t *end;
size_t remaining;
int flags;
Expand All @@ -228,6 +229,7 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c
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_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
{ return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
Expand Down Expand Up @@ -262,6 +264,9 @@ CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *en
return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
}

CBOR_INLINE_API void cbor_encoder_reset(CborEncoder *encoder)
{ encoder->data.ptr = encoder->start; }

/* Parser API */

enum CborParserIteratorFlags
Expand Down
14 changes: 12 additions & 2 deletions src/cborencoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
{
encoder->data.ptr = buffer;
encoder->start = buffer;
encoder->end = buffer + size;
encoder->remaining = 2;
encoder->flags = flags;
Expand Down Expand Up @@ -433,7 +434,7 @@ static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shif
*/

/**
* Appends the text string \a string of length \a length to the CBOR stream
* Appends the byte string \a string of length \a length to the CBOR stream
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
* provided by \a encoder. CBOR byte strings are arbitrary raw data.

* TinyCBOR makes no verification of correctness.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* TinyCBOR makes no verification of correctness.

*
Expand All @@ -445,7 +446,7 @@ CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, s
}

/**
* Appends the byte string \a string of length \a length to the CBOR stream
* Appends the text string \a string of length \a length to the CBOR stream
* provided by \a encoder. CBOR byte strings are arbitrary raw data.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* provided by \a encoder. CBOR byte strings are arbitrary raw data.
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
* TinyCBOR makes no verification of correctness.

*
* \sa cbor_encode_text_stringz, cbor_encode_text_string
Expand Down Expand Up @@ -642,4 +643,13 @@ CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *
* \sa cbor_encoder_init(), cbor_encoder_get_buffer_size(), CborEncoding
*/

/**
* \fn void cbor_encoder_reset(CborEncoder *encoder)
*
* Reset the encoder. The encoder will now start writing from the beginning of
* its allocated buffer space.
*
* \sa cbor_encoder_init
*/

/** @} */