Skip to content

Commit f815fbd

Browse files
committed
General uint encoding
1 parent 2125b15 commit f815fbd

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

misc/bytes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
3+
puts $*[0][2..-1].split('').each_slice(2).map {|_| '0x%02X' % _.join.to_i(16) }.join(', ')

src/cbor_encoders.c

+29-2
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,44 @@ size_t cbor_encode_uint32(uint32_t value, unsigned char * buffer, size_t buffer_
5050
buffer[4] = value;
5151
#endif
5252

53-
return 3;
53+
return 5;
5454
} else
5555
return 0;
5656
}
5757

5858
size_t cbor_encode_uint64(uint64_t value, unsigned char * buffer, size_t buffer_size)
5959
{
60+
if (buffer_size >= 9) {
61+
buffer[0] = 0x1B;
62+
63+
#if defined(__clang__) || defined(__GNUC__)
64+
*(uint64_t *)&buffer[1] = __builtin_bswap64(value);
65+
#else
66+
buffer[1] = value >> 56;
67+
buffer[2] = value >> 48;
68+
buffer[3] = value >> 40;
69+
buffer[4] = value >> 32;
70+
buffer[5] = value >> 24;
71+
buffer[6] = value >> 16;
72+
buffer[7] = value >> 8;
73+
buffer[8] = value;
74+
#endif
6075

76+
return 9;
77+
} else
78+
return 0;
6179
}
6280

6381
size_t cbor_encode_uint(uint64_t value, unsigned char * buffer, size_t buffer_size)
6482
{
65-
83+
if (value <= UINT16_MAX)
84+
if (value <= UINT8_MAX)
85+
return cbor_encode_uint8((uint8_t)value, buffer, buffer_size);
86+
else
87+
return cbor_encode_uint16((uint16_t)value, buffer, buffer_size);
88+
else
89+
if (value <= UINT32_MAX)
90+
return cbor_encode_uint32((uint32_t)value, buffer, buffer_size);
91+
else
92+
return cbor_encode_uint64((uint64_t)value, buffer, buffer_size);
6693
}

test/type_0_encoders_test.c

+37-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,53 @@
1010
unsigned char buffer[512];
1111

1212
static void test_embedded_uint8(void **state) {
13-
cbor_encode_uint8(14, buffer, 512);
13+
assert_int_equal(1, cbor_encode_uint8(14, buffer, 512));
1414
assert_memory_equal(buffer, (unsigned char[]){ 0x0E }, 1);
1515
}
1616

17+
static void test_uint8(void **state) {
18+
assert_int_equal(0, cbor_encode_uint8(180, buffer, 1));
19+
assert_int_equal(2, cbor_encode_uint8(255, buffer, 512));
20+
assert_memory_equal(buffer, ((unsigned char[]){ 0x18, 0xFF }), 2);
21+
}
22+
1723
static void test_uint16(void **state) {
18-
cbor_encode_uint16(1000, buffer, 512);
24+
assert_int_equal(0, cbor_encode_uint16(1000, buffer, 2));
25+
assert_int_equal(3, cbor_encode_uint16(1000, buffer, 512));
26+
assert_memory_equal(buffer, ((unsigned char[]){ 0x19, 0x03, 0xE8 }), 3);
27+
}
28+
29+
static void test_uint32(void **state) {
30+
assert_int_equal(0, cbor_encode_uint32(1000000, buffer, 4));
31+
assert_int_equal(5, cbor_encode_uint32(1000000, buffer, 512));
32+
assert_memory_equal(buffer, ((unsigned char[]){ 0x1A, 0x00, 0x0F, 0x42, 0x40 }), 5);
33+
}
34+
35+
static void test_uint64(void **state) {
36+
assert_int_equal(0, cbor_encode_uint64(18446744073709551615ULL, buffer, 8));
37+
assert_int_equal(9, cbor_encode_uint64(18446744073709551615ULL, buffer, 512));
38+
assert_memory_equal(buffer, ((unsigned char[]){ 0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }), 9);
39+
}
40+
41+
static void test_unspecified(void **state) {
42+
assert_int_equal(9, cbor_encode_uint(18446744073709551615ULL, buffer, 512));
43+
assert_memory_equal(buffer, ((unsigned char[]){ 0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }), 9);
44+
assert_int_equal(5, cbor_encode_uint(1000000, buffer, 512));
45+
assert_memory_equal(buffer, ((unsigned char[]){ 0x1A, 0x00, 0x0F, 0x42, 0x40 }), 5);
46+
assert_int_equal(3, cbor_encode_uint(1000, buffer, 512));
1947
assert_memory_equal(buffer, ((unsigned char[]){ 0x19, 0x03, 0xE8 }), 3);
48+
assert_int_equal(2, cbor_encode_uint(255, buffer, 512));
49+
assert_memory_equal(buffer, ((unsigned char[]){ 0x18, 0xFF }), 2);
2050
}
2151

2252
int main(void) {
2353
const UnitTest tests[] = {
2454
unit_test(test_embedded_uint8),
25-
unit_test(test_uint16)
55+
unit_test(test_uint8),
56+
unit_test(test_uint16),
57+
unit_test(test_uint32),
58+
unit_test(test_uint64),
59+
unit_test(test_unspecified)
2660
};
2761
return run_tests(tests);
2862
}

0 commit comments

Comments
 (0)