1*bb154e3eSDorjoy Chowdhury /*
2*bb154e3eSDorjoy Chowdhury * QEMU CBOR helpers
3*bb154e3eSDorjoy Chowdhury *
4*bb154e3eSDorjoy Chowdhury * Copyright (c) 2024 Dorjoy Chowdhury <dorjoychy111@gmail.com>
5*bb154e3eSDorjoy Chowdhury *
6*bb154e3eSDorjoy Chowdhury * This work is licensed under the terms of the GNU GPL, version 2 or
7*bb154e3eSDorjoy Chowdhury * (at your option) any later version. See the COPYING file in the
8*bb154e3eSDorjoy Chowdhury * top-level directory.
9*bb154e3eSDorjoy Chowdhury */
10*bb154e3eSDorjoy Chowdhury
11*bb154e3eSDorjoy Chowdhury #include "hw/virtio/cbor-helpers.h"
12*bb154e3eSDorjoy Chowdhury
qemu_cbor_map_add(cbor_item_t * map,cbor_item_t * key,cbor_item_t * value)13*bb154e3eSDorjoy Chowdhury bool qemu_cbor_map_add(cbor_item_t *map, cbor_item_t *key, cbor_item_t *value)
14*bb154e3eSDorjoy Chowdhury {
15*bb154e3eSDorjoy Chowdhury bool success = false;
16*bb154e3eSDorjoy Chowdhury struct cbor_pair pair = (struct cbor_pair) {
17*bb154e3eSDorjoy Chowdhury .key = cbor_move(key),
18*bb154e3eSDorjoy Chowdhury .value = cbor_move(value)
19*bb154e3eSDorjoy Chowdhury };
20*bb154e3eSDorjoy Chowdhury
21*bb154e3eSDorjoy Chowdhury success = cbor_map_add(map, pair);
22*bb154e3eSDorjoy Chowdhury if (!success) {
23*bb154e3eSDorjoy Chowdhury cbor_incref(pair.key);
24*bb154e3eSDorjoy Chowdhury cbor_incref(pair.value);
25*bb154e3eSDorjoy Chowdhury }
26*bb154e3eSDorjoy Chowdhury
27*bb154e3eSDorjoy Chowdhury return success;
28*bb154e3eSDorjoy Chowdhury }
29*bb154e3eSDorjoy Chowdhury
qemu_cbor_array_push(cbor_item_t * array,cbor_item_t * value)30*bb154e3eSDorjoy Chowdhury bool qemu_cbor_array_push(cbor_item_t *array, cbor_item_t *value)
31*bb154e3eSDorjoy Chowdhury {
32*bb154e3eSDorjoy Chowdhury bool success = false;
33*bb154e3eSDorjoy Chowdhury
34*bb154e3eSDorjoy Chowdhury success = cbor_array_push(array, cbor_move(value));
35*bb154e3eSDorjoy Chowdhury if (!success) {
36*bb154e3eSDorjoy Chowdhury cbor_incref(value);
37*bb154e3eSDorjoy Chowdhury }
38*bb154e3eSDorjoy Chowdhury
39*bb154e3eSDorjoy Chowdhury return success;
40*bb154e3eSDorjoy Chowdhury }
41*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_bool_to_map(cbor_item_t * map,const char * key,bool value)42*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_bool_to_map(cbor_item_t *map, const char *key, bool value)
43*bb154e3eSDorjoy Chowdhury {
44*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
45*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
46*bb154e3eSDorjoy Chowdhury
47*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
48*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
49*bb154e3eSDorjoy Chowdhury goto cleanup;
50*bb154e3eSDorjoy Chowdhury }
51*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_bool(value);
52*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
53*bb154e3eSDorjoy Chowdhury goto cleanup;
54*bb154e3eSDorjoy Chowdhury }
55*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
56*bb154e3eSDorjoy Chowdhury goto cleanup;
57*bb154e3eSDorjoy Chowdhury }
58*bb154e3eSDorjoy Chowdhury
59*bb154e3eSDorjoy Chowdhury return true;
60*bb154e3eSDorjoy Chowdhury
61*bb154e3eSDorjoy Chowdhury cleanup:
62*bb154e3eSDorjoy Chowdhury if (key_cbor) {
63*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
64*bb154e3eSDorjoy Chowdhury }
65*bb154e3eSDorjoy Chowdhury if (value_cbor) {
66*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
67*bb154e3eSDorjoy Chowdhury }
68*bb154e3eSDorjoy Chowdhury return false;
69*bb154e3eSDorjoy Chowdhury }
70*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_uint8_to_map(cbor_item_t * map,const char * key,uint8_t value)71*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_uint8_to_map(cbor_item_t *map, const char *key,
72*bb154e3eSDorjoy Chowdhury uint8_t value)
73*bb154e3eSDorjoy Chowdhury {
74*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
75*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
76*bb154e3eSDorjoy Chowdhury
77*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
78*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
79*bb154e3eSDorjoy Chowdhury goto cleanup;
80*bb154e3eSDorjoy Chowdhury }
81*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_uint8(value);
82*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
83*bb154e3eSDorjoy Chowdhury goto cleanup;
84*bb154e3eSDorjoy Chowdhury }
85*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
86*bb154e3eSDorjoy Chowdhury goto cleanup;
87*bb154e3eSDorjoy Chowdhury }
88*bb154e3eSDorjoy Chowdhury
89*bb154e3eSDorjoy Chowdhury return true;
90*bb154e3eSDorjoy Chowdhury
91*bb154e3eSDorjoy Chowdhury cleanup:
92*bb154e3eSDorjoy Chowdhury if (key_cbor) {
93*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
94*bb154e3eSDorjoy Chowdhury }
95*bb154e3eSDorjoy Chowdhury if (value_cbor) {
96*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
97*bb154e3eSDorjoy Chowdhury }
98*bb154e3eSDorjoy Chowdhury return false;
99*bb154e3eSDorjoy Chowdhury }
100*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_map_to_map(cbor_item_t * map,const char * key,size_t nested_map_size,cbor_item_t ** nested_map)101*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_map_to_map(cbor_item_t *map, const char *key,
102*bb154e3eSDorjoy Chowdhury size_t nested_map_size,
103*bb154e3eSDorjoy Chowdhury cbor_item_t **nested_map)
104*bb154e3eSDorjoy Chowdhury {
105*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
106*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
107*bb154e3eSDorjoy Chowdhury
108*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
109*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
110*bb154e3eSDorjoy Chowdhury goto cleanup;
111*bb154e3eSDorjoy Chowdhury }
112*bb154e3eSDorjoy Chowdhury value_cbor = cbor_new_definite_map(nested_map_size);
113*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
114*bb154e3eSDorjoy Chowdhury goto cleanup;
115*bb154e3eSDorjoy Chowdhury }
116*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
117*bb154e3eSDorjoy Chowdhury goto cleanup;
118*bb154e3eSDorjoy Chowdhury }
119*bb154e3eSDorjoy Chowdhury *nested_map = value_cbor;
120*bb154e3eSDorjoy Chowdhury
121*bb154e3eSDorjoy Chowdhury return true;
122*bb154e3eSDorjoy Chowdhury
123*bb154e3eSDorjoy Chowdhury cleanup:
124*bb154e3eSDorjoy Chowdhury if (key_cbor) {
125*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
126*bb154e3eSDorjoy Chowdhury }
127*bb154e3eSDorjoy Chowdhury if (value_cbor) {
128*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
129*bb154e3eSDorjoy Chowdhury }
130*bb154e3eSDorjoy Chowdhury return false;
131*bb154e3eSDorjoy Chowdhury }
132*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_bytestring_to_map(cbor_item_t * map,const char * key,uint8_t * arr,size_t len)133*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_bytestring_to_map(cbor_item_t *map, const char *key,
134*bb154e3eSDorjoy Chowdhury uint8_t *arr, size_t len)
135*bb154e3eSDorjoy Chowdhury {
136*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
137*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
138*bb154e3eSDorjoy Chowdhury
139*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
140*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
141*bb154e3eSDorjoy Chowdhury goto cleanup;
142*bb154e3eSDorjoy Chowdhury }
143*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_bytestring(arr, len);
144*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
145*bb154e3eSDorjoy Chowdhury goto cleanup;
146*bb154e3eSDorjoy Chowdhury }
147*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
148*bb154e3eSDorjoy Chowdhury goto cleanup;
149*bb154e3eSDorjoy Chowdhury }
150*bb154e3eSDorjoy Chowdhury
151*bb154e3eSDorjoy Chowdhury return true;
152*bb154e3eSDorjoy Chowdhury
153*bb154e3eSDorjoy Chowdhury cleanup:
154*bb154e3eSDorjoy Chowdhury if (key_cbor) {
155*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
156*bb154e3eSDorjoy Chowdhury }
157*bb154e3eSDorjoy Chowdhury if (value_cbor) {
158*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
159*bb154e3eSDorjoy Chowdhury }
160*bb154e3eSDorjoy Chowdhury return false;
161*bb154e3eSDorjoy Chowdhury }
162*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_null_to_map(cbor_item_t * map,const char * key)163*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_null_to_map(cbor_item_t *map, const char *key)
164*bb154e3eSDorjoy Chowdhury {
165*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
166*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
167*bb154e3eSDorjoy Chowdhury
168*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
169*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
170*bb154e3eSDorjoy Chowdhury goto cleanup;
171*bb154e3eSDorjoy Chowdhury }
172*bb154e3eSDorjoy Chowdhury value_cbor = cbor_new_null();
173*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
174*bb154e3eSDorjoy Chowdhury goto cleanup;
175*bb154e3eSDorjoy Chowdhury }
176*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
177*bb154e3eSDorjoy Chowdhury goto cleanup;
178*bb154e3eSDorjoy Chowdhury }
179*bb154e3eSDorjoy Chowdhury
180*bb154e3eSDorjoy Chowdhury return true;
181*bb154e3eSDorjoy Chowdhury
182*bb154e3eSDorjoy Chowdhury cleanup:
183*bb154e3eSDorjoy Chowdhury if (key_cbor) {
184*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
185*bb154e3eSDorjoy Chowdhury }
186*bb154e3eSDorjoy Chowdhury if (value_cbor) {
187*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
188*bb154e3eSDorjoy Chowdhury }
189*bb154e3eSDorjoy Chowdhury return false;
190*bb154e3eSDorjoy Chowdhury }
191*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_string_to_map(cbor_item_t * map,const char * key,const char * value)192*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_string_to_map(cbor_item_t *map, const char *key,
193*bb154e3eSDorjoy Chowdhury const char *value)
194*bb154e3eSDorjoy Chowdhury {
195*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
196*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
197*bb154e3eSDorjoy Chowdhury
198*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
199*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
200*bb154e3eSDorjoy Chowdhury goto cleanup;
201*bb154e3eSDorjoy Chowdhury }
202*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_string(value);
203*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
204*bb154e3eSDorjoy Chowdhury goto cleanup;
205*bb154e3eSDorjoy Chowdhury }
206*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
207*bb154e3eSDorjoy Chowdhury goto cleanup;
208*bb154e3eSDorjoy Chowdhury }
209*bb154e3eSDorjoy Chowdhury
210*bb154e3eSDorjoy Chowdhury return true;
211*bb154e3eSDorjoy Chowdhury
212*bb154e3eSDorjoy Chowdhury cleanup:
213*bb154e3eSDorjoy Chowdhury if (key_cbor) {
214*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
215*bb154e3eSDorjoy Chowdhury }
216*bb154e3eSDorjoy Chowdhury if (value_cbor) {
217*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
218*bb154e3eSDorjoy Chowdhury }
219*bb154e3eSDorjoy Chowdhury return false;
220*bb154e3eSDorjoy Chowdhury }
221*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_uint8_array_to_map(cbor_item_t * map,const char * key,uint8_t * arr,size_t len)222*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_uint8_array_to_map(cbor_item_t *map, const char *key,
223*bb154e3eSDorjoy Chowdhury uint8_t *arr, size_t len)
224*bb154e3eSDorjoy Chowdhury {
225*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
226*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
227*bb154e3eSDorjoy Chowdhury
228*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
229*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
230*bb154e3eSDorjoy Chowdhury goto cleanup;
231*bb154e3eSDorjoy Chowdhury }
232*bb154e3eSDorjoy Chowdhury value_cbor = cbor_new_definite_array(len);
233*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
234*bb154e3eSDorjoy Chowdhury goto cleanup;
235*bb154e3eSDorjoy Chowdhury }
236*bb154e3eSDorjoy Chowdhury
237*bb154e3eSDorjoy Chowdhury for (int i = 0; i < len; ++i) {
238*bb154e3eSDorjoy Chowdhury cbor_item_t *tmp = cbor_build_uint8(arr[i]);
239*bb154e3eSDorjoy Chowdhury if (!tmp) {
240*bb154e3eSDorjoy Chowdhury goto cleanup;
241*bb154e3eSDorjoy Chowdhury }
242*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_array_push(value_cbor, tmp)) {
243*bb154e3eSDorjoy Chowdhury cbor_decref(&tmp);
244*bb154e3eSDorjoy Chowdhury goto cleanup;
245*bb154e3eSDorjoy Chowdhury }
246*bb154e3eSDorjoy Chowdhury }
247*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
248*bb154e3eSDorjoy Chowdhury goto cleanup;
249*bb154e3eSDorjoy Chowdhury }
250*bb154e3eSDorjoy Chowdhury
251*bb154e3eSDorjoy Chowdhury return true;
252*bb154e3eSDorjoy Chowdhury
253*bb154e3eSDorjoy Chowdhury cleanup:
254*bb154e3eSDorjoy Chowdhury if (key_cbor) {
255*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
256*bb154e3eSDorjoy Chowdhury }
257*bb154e3eSDorjoy Chowdhury if (value_cbor) {
258*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
259*bb154e3eSDorjoy Chowdhury }
260*bb154e3eSDorjoy Chowdhury return false;
261*bb154e3eSDorjoy Chowdhury }
262*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_uint8_key_bytestring_to_map(cbor_item_t * map,uint8_t key,uint8_t * buf,size_t len)263*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_uint8_key_bytestring_to_map(cbor_item_t *map, uint8_t key,
264*bb154e3eSDorjoy Chowdhury uint8_t *buf, size_t len)
265*bb154e3eSDorjoy Chowdhury {
266*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
267*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
268*bb154e3eSDorjoy Chowdhury
269*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_uint8(key);
270*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
271*bb154e3eSDorjoy Chowdhury goto cleanup;
272*bb154e3eSDorjoy Chowdhury }
273*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_bytestring(buf, len);
274*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
275*bb154e3eSDorjoy Chowdhury goto cleanup;
276*bb154e3eSDorjoy Chowdhury }
277*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
278*bb154e3eSDorjoy Chowdhury goto cleanup;
279*bb154e3eSDorjoy Chowdhury }
280*bb154e3eSDorjoy Chowdhury
281*bb154e3eSDorjoy Chowdhury return true;
282*bb154e3eSDorjoy Chowdhury
283*bb154e3eSDorjoy Chowdhury cleanup:
284*bb154e3eSDorjoy Chowdhury if (key_cbor) {
285*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
286*bb154e3eSDorjoy Chowdhury }
287*bb154e3eSDorjoy Chowdhury if (value_cbor) {
288*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
289*bb154e3eSDorjoy Chowdhury }
290*bb154e3eSDorjoy Chowdhury return false;
291*bb154e3eSDorjoy Chowdhury }
292*bb154e3eSDorjoy Chowdhury
qemu_cbor_add_uint64_to_map(cbor_item_t * map,const char * key,uint64_t value)293*bb154e3eSDorjoy Chowdhury bool qemu_cbor_add_uint64_to_map(cbor_item_t *map, const char *key,
294*bb154e3eSDorjoy Chowdhury uint64_t value)
295*bb154e3eSDorjoy Chowdhury {
296*bb154e3eSDorjoy Chowdhury cbor_item_t *key_cbor = NULL;
297*bb154e3eSDorjoy Chowdhury cbor_item_t *value_cbor = NULL;
298*bb154e3eSDorjoy Chowdhury
299*bb154e3eSDorjoy Chowdhury key_cbor = cbor_build_string(key);
300*bb154e3eSDorjoy Chowdhury if (!key_cbor) {
301*bb154e3eSDorjoy Chowdhury goto cleanup;
302*bb154e3eSDorjoy Chowdhury }
303*bb154e3eSDorjoy Chowdhury value_cbor = cbor_build_uint64(value);
304*bb154e3eSDorjoy Chowdhury if (!value_cbor) {
305*bb154e3eSDorjoy Chowdhury goto cleanup;
306*bb154e3eSDorjoy Chowdhury }
307*bb154e3eSDorjoy Chowdhury if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) {
308*bb154e3eSDorjoy Chowdhury goto cleanup;
309*bb154e3eSDorjoy Chowdhury }
310*bb154e3eSDorjoy Chowdhury
311*bb154e3eSDorjoy Chowdhury return true;
312*bb154e3eSDorjoy Chowdhury
313*bb154e3eSDorjoy Chowdhury cleanup:
314*bb154e3eSDorjoy Chowdhury if (key_cbor) {
315*bb154e3eSDorjoy Chowdhury cbor_decref(&key_cbor);
316*bb154e3eSDorjoy Chowdhury }
317*bb154e3eSDorjoy Chowdhury if (value_cbor) {
318*bb154e3eSDorjoy Chowdhury cbor_decref(&value_cbor);
319*bb154e3eSDorjoy Chowdhury }
320*bb154e3eSDorjoy Chowdhury return false;
321*bb154e3eSDorjoy Chowdhury }
322