intel / intel/tinycbor

Reading and writing CBOR documents in pieces

Open
#146 17 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
C
Stars
630
Forks
222
Avg merge
1d 3h
Merged PRs (30d)
1

Description

Hi,

Is it possible to parse and and emit a CBOR document in parts? The use case here is where I might have a CBOR file that's in the order of 2kB in size, but I don't want to allocate a 2kB buffer to read the whole thing in one go (I've allocated 4kB for a stack and don't have a lot of heap space available).

The thinking is when reading, I might allocate a 256 or 512-byte buffer (on the stack), read the first buffer-full worth of data, then call the parser on that. As I get near the end, I watch for `CborErrorAdvancePastEOF`… when this happens, I obtain a copy of TinyCBOR's read pointer, do a `memmove` to move the unread data to the start of my buffer, `read` some more in, then tell TinyCBOR to move back to the start of the buffer to resume reading whatever CBOR value was being pointed to at the time.

Likewise writing; when `CborErrorOutOfMemory` is encountered, I can do a `write` of the buffer, then tell TinyCBOR to continue from the start of the buffer and resume writing the output.

Such a feature would *really* work well with CoAP block-wise transfers, as the data could be effectively "streamed" instead of having to buffer the lot.

I tried looking around for whether there was a flag I could specify on the decoder, but couldn't see any flags in the documentation.

Regards,
Stuart Longland

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.