2023-11-23 07:52:51 +00:00
|
|
|
#include <stdint.h>
|
2024-01-04 09:57:16 +00:00
|
|
|
#include <string.h>
|
2023-11-23 07:52:51 +00:00
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
#include "nest/cbor.h"
|
2023-11-23 07:52:51 +00:00
|
|
|
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void write_item(struct cbor_writer *writer, uint8_t major, uint64_t num);
|
2023-11-23 07:52:51 +00:00
|
|
|
void check_memory(struct cbor_writer *writer, int add_size);
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
struct cbor_writer *cbor_init(uint8_t *buff, uint32_t capacity, struct linpool *lp)
|
2023-11-29 14:14:16 +00:00
|
|
|
{
|
2023-11-24 12:57:27 +00:00
|
|
|
struct cbor_writer *writer = (struct cbor_writer*)lp_alloc(lp, sizeof(struct cbor_writer));
|
|
|
|
writer->cbor = buff;
|
|
|
|
writer->capacity = capacity;
|
2023-11-23 07:52:51 +00:00
|
|
|
writer->pt =0;
|
2023-11-24 12:57:27 +00:00
|
|
|
writer->lp = lp;
|
2023-11-23 07:52:51 +00:00
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cbor_open_block(struct cbor_writer *writer) { // We will need to close the block later manualy
|
|
|
|
check_memory(writer, 2);
|
|
|
|
writer->cbor[writer->pt] = 0xbf;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:14:16 +00:00
|
|
|
void cbor_open_list(struct cbor_writer *writer)
|
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
check_memory(writer, 2);
|
|
|
|
writer->cbor[writer->pt] = 0x9f;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:14:16 +00:00
|
|
|
void cbor_close_block_or_list(struct cbor_writer *writer)
|
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
check_memory(writer, 2);
|
|
|
|
writer->cbor[writer->pt] = 0xff;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_open_block_with_length(struct cbor_writer *writer, uint32_t length)
|
2023-11-29 14:14:16 +00:00
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
write_item(writer, 5, length);
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_open_list_with_length(struct cbor_writer *writer, uint32_t length)
|
2023-11-29 14:14:16 +00:00
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
write_item(writer, 4, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-06 07:40:40 +00:00
|
|
|
void cbor_add_int(struct cbor_writer *writer, int64_t item)
|
2023-11-29 14:14:16 +00:00
|
|
|
{
|
|
|
|
if (item >= 0)
|
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
write_item(writer, 0, item); // 0 is the "major" (three bits) introducing positive int, 1 is for negative
|
|
|
|
}
|
2023-11-29 14:14:16 +00:00
|
|
|
else
|
|
|
|
{
|
2023-12-06 07:40:40 +00:00
|
|
|
write_item(writer, 1, -item);
|
2023-11-23 07:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 10:57:40 +00:00
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_add_ipv4(struct cbor_writer *writer, uint32_t addr)
|
2023-12-07 09:28:44 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 6, 52); // 6 is TAG, 52 is tag number for ipv4
|
|
|
|
write_item(writer, 2, 4); // bytestring of length 4
|
|
|
|
for (int i = 3; i>=0; i--)
|
|
|
|
{
|
|
|
|
writer->cbor[writer->pt] = (addr>>(i*8)) & 0xff;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_add_ipv6(struct cbor_writer *writer, uint32_t addr[4])
|
2023-12-07 09:28:44 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 6, 54); // 6 is TAG, 54 is tag number for ipv6
|
2024-01-09 09:20:15 +00:00
|
|
|
write_item(writer, 2, 16); // bytestring of length 16
|
2024-01-04 09:57:16 +00:00
|
|
|
for (int j = 0; j < 4; j++)
|
2023-12-07 09:28:44 +00:00
|
|
|
{
|
2024-01-04 09:57:16 +00:00
|
|
|
for (int i = 3; i>=0; i--)
|
|
|
|
{
|
|
|
|
writer->cbor[writer->pt] = (addr[j]>>(i*8)) & 0xff;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
2023-12-07 09:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
|
2024-01-04 10:46:34 +00:00
|
|
|
void cbor_add_ipv4_prefix(struct cbor_writer *writer, net_addr_ip4 *n)
|
2023-12-07 09:28:44 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 6, 52); // 6 is TAG, 52 is tag number for ipv4
|
|
|
|
cbor_open_block_with_length(writer, 2);
|
2024-01-04 10:46:34 +00:00
|
|
|
cbor_add_int(writer, n->pxlen);
|
2023-12-07 09:28:44 +00:00
|
|
|
write_item(writer, 2, 4); // bytestring of length 4
|
2024-01-04 10:46:34 +00:00
|
|
|
put_ip4(&writer->cbor[writer->pt], n->prefix);
|
|
|
|
writer->pt += 4;
|
2023-12-07 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-04 10:22:09 +00:00
|
|
|
void cbor_add_ipv6_prefix(struct cbor_writer *writer, net_addr_ip6 *n)
|
2023-12-07 09:28:44 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 6, 54); // 6 is TAG, 54 is tag number for ipv6
|
|
|
|
cbor_open_block_with_length(writer, 2);
|
2024-01-04 10:22:09 +00:00
|
|
|
cbor_add_int(writer, n->pxlen);
|
|
|
|
|
|
|
|
write_item(writer, 2, 16);
|
|
|
|
put_ip6(&writer->cbor[writer->pt], n->prefix);
|
|
|
|
writer->pt += 16;
|
2023-12-07 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_add_uint(struct cbor_writer *writer, uint64_t item)
|
2023-12-06 07:40:40 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 0, item);
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:14:16 +00:00
|
|
|
void cbor_add_tag(struct cbor_writer *writer, int item)
|
|
|
|
{
|
2023-11-28 10:57:40 +00:00
|
|
|
write_item(writer, 6, item);
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:14:16 +00:00
|
|
|
void cbor_add_string(struct cbor_writer *writer, const char *string)
|
|
|
|
{
|
2023-11-23 07:52:51 +00:00
|
|
|
int length = strlen(string);
|
|
|
|
write_item(writer, 3, length); // 3 is major, then goes length of string and string
|
|
|
|
check_memory(writer, length);
|
|
|
|
memcpy(writer->cbor+writer->pt, string, length);
|
|
|
|
writer->pt+=length;
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void cbor_nonterminated_string(struct cbor_writer *writer, const char *string, uint32_t length)
|
2023-12-01 13:43:24 +00:00
|
|
|
{
|
|
|
|
write_item(writer, 3, length); // 3 is major, then goes length of string and string
|
|
|
|
check_memory(writer, length);
|
|
|
|
memcpy(writer->cbor+writer->pt, string, length);
|
|
|
|
writer->pt+=length;
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:57:16 +00:00
|
|
|
void write_item(struct cbor_writer *writer, uint8_t major, uint64_t num)
|
2023-11-29 14:14:16 +00:00
|
|
|
{
|
2023-12-20 15:17:12 +00:00
|
|
|
//log("write major %i %li", major, num);
|
2023-11-23 07:52:51 +00:00
|
|
|
major = major<<5;
|
|
|
|
check_memory(writer, 10);
|
2024-01-04 09:57:16 +00:00
|
|
|
if (num > ((uint64_t)1<<(4*8))-1)
|
2023-12-06 07:40:40 +00:00
|
|
|
{ // We need 8 bytes to encode the num
|
|
|
|
major += 0x1b; // reserving those bytes
|
|
|
|
writer->cbor[writer->pt] = major;
|
|
|
|
writer->pt++;
|
|
|
|
for (int i = 7; i>=0; i--)
|
|
|
|
{ // write n-th byte of num
|
|
|
|
uint8_t to_write = (num>>(i*8)) & 0xff;
|
|
|
|
writer->cbor[writer->pt] = to_write;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-29 14:14:16 +00:00
|
|
|
if (num > (1<<(2*8))-1)
|
|
|
|
{ // We need 4 bytes to encode the num
|
2023-11-23 07:52:51 +00:00
|
|
|
major += 0x1a; // reserving those bytes
|
|
|
|
writer->cbor[writer->pt] = major;
|
|
|
|
writer->pt++;
|
2023-11-29 14:14:16 +00:00
|
|
|
for (int i = 3; i>=0; i--)
|
|
|
|
{ // write n-th byte of num
|
2023-11-23 07:52:51 +00:00
|
|
|
uint8_t to_write = (num>>(i*8)) & 0xff;
|
|
|
|
writer->cbor[writer->pt] = to_write;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-29 14:14:16 +00:00
|
|
|
if (num > (1<<(8))-1)
|
|
|
|
{ // We need 2 bytes to encode the num
|
2023-11-23 07:52:51 +00:00
|
|
|
major += 0x19; // reserving those bytes
|
|
|
|
writer->cbor[writer->pt] = major;
|
|
|
|
writer->pt++;
|
2023-11-29 14:14:16 +00:00
|
|
|
for (int i = 1; i>=0; i--)
|
|
|
|
{ // write n-th byte of num
|
2023-11-23 07:52:51 +00:00
|
|
|
uint8_t to_write = (num>>(i*8)) & 0xff;
|
|
|
|
writer->cbor[writer->pt] = to_write;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-29 14:14:16 +00:00
|
|
|
if (num > 23)
|
|
|
|
{ // byte is enough, but aditional value would be too big
|
2023-11-28 10:57:40 +00:00
|
|
|
major += 0x18; // reserving that byte
|
2023-11-23 07:52:51 +00:00
|
|
|
writer->cbor[writer->pt] = major;
|
|
|
|
writer->pt++;
|
|
|
|
uint8_t to_write = num & 0xff;
|
|
|
|
writer->cbor[writer->pt] = to_write;
|
|
|
|
writer->pt++;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-20 15:17:12 +00:00
|
|
|
//log("write item major %i num %i writer->pt %i writer->capacity %i writer %i", major, num, writer->pt, writer->capacity, writer);
|
2023-11-23 07:52:51 +00:00
|
|
|
major += num; // we can store the num as additional value
|
|
|
|
writer->cbor[writer->pt] = major;
|
|
|
|
writer->pt++;
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:14:16 +00:00
|
|
|
void check_memory(struct cbor_writer *writer, int add_size)
|
|
|
|
{
|
|
|
|
if (writer->capacity - writer->pt-add_size < 0)
|
|
|
|
{
|
2023-11-24 12:57:27 +00:00
|
|
|
bug("There is not enough space for cbor response in given buffer");
|
2023-11-23 07:52:51 +00:00
|
|
|
}
|
|
|
|
}
|