-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbor.c
212 lines (184 loc) · 7.3 KB
/
cbor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <string.h>
#include "cbor.h"
#include "error_types.h"
static void cbor_write_bytes(const u_int8_t *bytes, const size_t bytes_size, u_int8_t *new_bytes, size_t *written) {
for (int i = 0; i < bytes_size; ++i) {
new_bytes[(*written) + i] = bytes[i];
}
*written = *written + bytes_size;
}
static void cbor_write(u_int8_t value, u_int8_t *new_bytes, size_t *written) {
new_bytes[*written] = value;
*written = *written + 1;
}
static void cbor_encode_type_and_length(MajorType majorType, long length, u_int8_t *new_bytes, size_t *written) {
int symbol = majorType << 5;
if (length <= 23L) {
cbor_write((u_int8_t) (symbol | length), new_bytes, written);
} else if (length <= 255L) {
symbol |= ONE_BYTE;
cbor_write((u_int8_t) symbol, new_bytes, written);
cbor_write((u_int8_t) length, new_bytes, written);
} else if (length <= 65535L) {
symbol |= TWO_BYTES;
cbor_write((u_int8_t) symbol, new_bytes, written);
cbor_write((u_int8_t) (length >> 8), new_bytes, written);
cbor_write((u_int8_t) (length & 0xFF), new_bytes, written);
} else if (length <= 4294967295L) {
symbol |= FOUR_BYTES;
cbor_write((u_int8_t) symbol, new_bytes, written);
cbor_write((u_int8_t) ((length >> 24) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 16) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 8) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) (length & 0xFF), new_bytes, written);
} else {
symbol |= EIGHT_BYTES;
cbor_write((u_int8_t) symbol, new_bytes, written);
cbor_write((u_int8_t) ((length >> 56) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 48) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 40) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 32) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 24) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 16) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) ((length >> 8) & 0xFF), new_bytes, written);
cbor_write((u_int8_t) (length & 0xFF), new_bytes, written);
}
}
static void cbor_encode_array(u_int8_t *new_bytes, size_t *written, cborItem *parent) {
cbor_encode_type_and_length(MT_ARRAY, parent->length, new_bytes, written);
cborItem *current = parent->firstItem;
while (current != NULL) {
cbor_encode(new_bytes, written, current);
current = current->nextItem;
}
}
static void cbor_encode_positive_int(u_int8_t *new_bytes, size_t *written, long value) {
cbor_encode_type_and_length(MT_UNSIGNED, value, new_bytes, written);
}
static void cbor_encode_negative_int(u_int8_t *new_bytes, size_t *written, long value) {
cbor_encode_type_and_length(MT_NEGATIVE, value, new_bytes, written);
}
static void cbor_encode_bytes(u_int8_t *new_bytes, size_t *written, const u_int8_t *bytes, size_t bytes_size) {
cbor_encode_type_and_length(MT_BYTES, bytes_size, new_bytes, written);
cbor_write_bytes(bytes, bytes_size, new_bytes, written);
}
static void cbor_encode_raw(u_int8_t *new_bytes, size_t *written, const u_int8_t *bytes, size_t bytes_size) {
cbor_write_bytes(bytes, bytes_size, new_bytes, written);
}
static void cbor_encode_map(u_int8_t *new_bytes, size_t *written, cborItem *parent) {
cbor_encode_type_and_length(MT_MAP, 0, new_bytes, written);
}
static void cbor_encode_tag(u_int8_t *new_bytes, size_t *written, uint8_t *value, size_t value_size, long tag) {
cbor_encode_type_and_length(MT_TAG, tag, new_bytes, written);
cbor_encode_bytes(new_bytes, written, value, value_size);
}
void cbor_encode_indefinite_length_array(u_int8_t *new_bytes, size_t *written, cborItem *parent) {
uint8_t symbol_array = (MT_ARRAY << 5) + 31;
cbor_write_bytes(&symbol_array, 1, new_bytes, written);
cborItem *current = parent->firstItem;
while (current != NULL) {
cbor_encode(new_bytes, written, current);
current = current->nextItem;
}
uint8_t symbol_primitive = (MT_PRIMITIVE << 5) + 31;
cbor_write_bytes(&symbol_primitive, 1, new_bytes, written);
}
int cbor_encode(u_int8_t *new_bytes, size_t *written, cborItem *item) {
switch (item->type) {
case MT_UNSIGNED :
cbor_encode_positive_int(new_bytes, written, item->uint);
break;
case MT_NEGATIVE :
cbor_encode_negative_int(new_bytes, written, item->uint);
break;
case MT_BYTES :
cbor_encode_bytes(new_bytes, written, item->bytes.bytes, item->bytes.size);
break;
case MT_TEXT :
//
break;
case MT_ARRAY :
cbor_encode_array(new_bytes, written, item);
break;
case MT_MAP :
cbor_encode_map(new_bytes, written, item);
break;
case MT_TAG :
cbor_encode_tag(new_bytes, written, item->bytes.bytes, item->bytes.size, item->tag);
break;
case MT_INDEFINITE :
cbor_encode_indefinite_length_array(new_bytes, written, item);
break;
case MT_RAW :
cbor_encode_raw(new_bytes, written, item->bytes.bytes, item->bytes.size);
break;
default :
return CARDANO_ENCOING_ERROR;
}
return NO_ERROR;
}
static void cbor_init_item(cborItem *item) {
item->type = MT_INVALID;
item->length = 0;
item->nextItem = NULL;
item->firstItem = NULL;
item->uint = 0;
item->tag = 0;
item->bytes.size = 0;
memset(item->bytes.bytes, 0, sizeof(item->bytes));
memset(item->str, 0, sizeof(item->str));
}
void cbor_create_integer(cborItem *item, long value) {
cbor_init_item(item);
long singedValue = value;
if (singedValue < 0) {
item->type = MT_NEGATIVE;
} else {
item->type = MT_UNSIGNED;
}
item->uint = value;
}
void cbor_create_tag(cborItem *item, uint8_t *value, size_t value_size, long tag) {
cbor_init_item(item);
item->type = MT_TAG;
item->tag = tag;
memcpy(item->bytes.bytes, value, value_size);
item->bytes.size = (pb_size_t) value_size;
}
void cbor_create_bytes(cborItem *item, uint8_t *bytes, size_t byte_size) {
cbor_init_item(item);
item->type = MT_BYTES;
item->bytes.size = (pb_size_t) byte_size;
memcpy(item->bytes.bytes, bytes, byte_size);
}
void cbor_create_raw(cborItem *item, uint8_t *bytes, size_t byte_size) {
cbor_init_item(item);
item->type = MT_RAW;
item->bytes.size = (pb_size_t) byte_size;
memcpy(item->bytes.bytes, bytes, byte_size);
}
void cbor_create_array(cborItem *parent) {
cbor_init_item(parent);
parent->type = MT_ARRAY;
}
void cbor_infinite_create_array(cborItem *parent) {
cbor_init_item(parent);
parent->type = MT_INDEFINITE;
}
void cbor_add_item_to_array(cborItem *parent, cborItem *newItem) {
if (parent->firstItem == NULL) {
parent->firstItem = newItem;
} else {
cborItem *current = parent->firstItem;
while (current->nextItem != NULL) {
current = current->nextItem;
}
current->nextItem = newItem;
}
newItem->nextItem = NULL;
parent->length = parent->length + 1;
}
void cbor_create_map(cborItem *parent) {
cbor_init_item(parent);
parent->type = MT_MAP;
}