Skip to content

Commit 9dfce38

Browse files
committed
uint encoding WIP
1 parent 9950d81 commit 9dfce38

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(SOURCES cbor.c cbor_internal.c cbor_stack.c cbor_unicode.c)
1+
set(SOURCES cbor.c cbor_internal.c cbor_stack.c cbor_unicode.c cbor_encoders.c)
22

33
add_library(cbor ${SOURCES})
44
add_library(cbor_shared SHARED ${SOURCES})

src/cbor.h

+8
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ struct cbor_decoder_result {
238238

239239
struct cbor_decoder_result cbor_stream_decode(cbor_data, size_t, const struct cbor_callbacks *, void *);
240240

241+
size_t cbor_encode_uint8(uint8_t, unsigned char *, size_t);
242+
size_t cbor_encode_uint16(uint16_t, unsigned char *, size_t);
243+
size_t cbor_encode_uint32(uint32_t, unsigned char *, size_t);
244+
size_t cbor_encode_uint64(uint64_t, unsigned char *, size_t);
245+
246+
size_t cbor_encode_uint(uint64_t, unsigned char *, size_t);
247+
248+
241249
cbor_item_t * cbor_load(cbor_data source, size_t source_size, cbor_flags_t flags, struct cbor_load_result * result);
242250

243251
void cbor_incref(cbor_item_t * item);

src/cbor_encoders.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "cbor.h"
2+
#include "cbor_internal.h"
3+
#include "magic.h"
4+
5+
size_t cbor_encode_uint8(uint8_t value, unsigned char * buffer, size_t buffer_size)
6+
{
7+
if (value <= 23) {
8+
if (buffer_size >= 1) {
9+
buffer[0] = value;
10+
return 1;
11+
}
12+
} else {
13+
if (buffer_size >= 2) {
14+
buffer[0] = 0x18;
15+
buffer[1] = value;
16+
return 2;
17+
}
18+
}
19+
return 0;
20+
}
21+
22+
size_t cbor_encode_uint16(uint16_t value, unsigned char * buffer, size_t buffer_size)
23+
{
24+
if (buffer_size >= 3) {
25+
buffer[0] = 0x19;
26+
27+
#if defined(__clang__) || defined(__GNUC__)
28+
*(uint16_t *)&buffer[1] = __builtin_bswap16(value);
29+
#else
30+
buffer[1] = value >> 8;
31+
buffer[2] = value;
32+
#endif
33+
34+
return 3;
35+
} else
36+
return 0;
37+
}
38+
39+
size_t cbor_encode_uint32(uint32_t value, unsigned char * buffer, size_t buffer_size)
40+
{
41+
if (buffer_size >= 5) {
42+
buffer[0] = 0x1A;
43+
44+
#if defined(__clang__) || defined(__GNUC__)
45+
*(uint32_t *)&buffer[1] = __builtin_bswap32(value);
46+
#else
47+
buffer[1] = value >> 24;
48+
buffer[2] = value >> 16;
49+
buffer[3] = value >> 8;
50+
buffer[4] = value;
51+
#endif
52+
53+
return 3;
54+
} else
55+
return 0;
56+
}
57+
58+
size_t cbor_encode_uint64(uint64_t value, unsigned char * buffer, size_t buffer_size)
59+
{
60+
61+
}
62+
63+
size_t cbor_encode_uint(uint64_t value, unsigned char * buffer, size_t buffer_size)
64+
{
65+
66+
}

test/type_0_encoders_test.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdarg.h>
2+
#include <stddef.h>
3+
#include <setjmp.h>
4+
#include <cmocka.h>
5+
#include <stdio.h>
6+
#include "cbor.h"
7+
#include <inttypes.h>
8+
9+
10+
unsigned char buffer[512];
11+
12+
static void test_embedded_uint8(void **state) {
13+
cbor_encode_uint8(14, buffer, 512);
14+
assert_memory_equal(buffer, (unsigned char[]){ 0x0E }, 1);
15+
}
16+
17+
static void test_uint16(void **state) {
18+
cbor_encode_uint16(1000, buffer, 512);
19+
assert_memory_equal(buffer, ((unsigned char[]){ 0x19, 0x03, 0xE8 }), 3);
20+
}
21+
22+
int main(void) {
23+
const UnitTest tests[] = {
24+
unit_test(test_embedded_uint8),
25+
unit_test(test_uint16)
26+
};
27+
return run_tests(tests);
28+
}

0 commit comments

Comments
 (0)