-
Notifications
You must be signed in to change notification settings - Fork 23
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
No support for indefinite array #20
Comments
Thanks for the issue, I've confirmed that there is indeed a problem. I'll try to provide a fix asap. |
I have done a small implementation for this to make it work for now. If you wish I can create a pull request. |
I would appreciate that. |
Please find the code in PR #21 . |
Same is true for indefinite maps #include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "nanocbor/nanocbor.h"
size_t _encode(uint8_t *buf, size_t len)
{
nanocbor_encoder_t enc;
nanocbor_encoder_init(&enc, buf, len);
nanocbor_fmt_map_indefinite(&enc);
// nanocbor_fmt_map(&enc, 2);
/* write key - value pair*/
nanocbor_fmt_uint(&enc, 0x1000);
nanocbor_fmt_uint(&enc, 30);
/* write key - value pair*/
nanocbor_fmt_uint(&enc, 0x1001);
nanocbor_fmt_uint(&enc, 60);
nanocbor_fmt_end_indefinite(&enc);
return nanocbor_encoded_len(&enc);
}
bool _decode(const uint8_t *buf, size_t len)
{
nanocbor_value_t cfg, map;
nanocbor_decoder_init(&cfg, buf, len);
if (nanocbor_enter_map(&cfg, &map) < 0) {
puts("can't enter map");
return false;
}
while (!nanocbor_at_end(&map)) {
uint32_t key, value;
if (nanocbor_get_uint32(&map, &key) < 0) {
puts("can't decode key");
return false;
}
if (nanocbor_get_uint32(&map, &value) < 0) {
puts("can't decode value");
return false;
}
printf("%x: %d\n", key, value);
}
return true;
}
int main(void)
{
uint8_t buffer[64];
size_t len = _encode(buffer, sizeof(buffer));
printf("encoded to %zd bytes\n", len);
_decode(buffer, len);
return 0;
} Will print
with the indefinite version. When using a definite map with two elements I get
|
The decoder given in the test/fuzz isn't able to decode an indefinite array encoded as:
nanocbor_fmt_array_indefinite(enc); nanocbor_fmt_int(enc, 8); nanocbor_fmt_end_indefinite(enc);
The text was updated successfully, but these errors were encountered: