vppapigen: more _fromjson autogeneration coverity fixes
[vpp.git] / src / vat2 / jsonconvert.c
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vppinfra/cJSON.h>
17 #include <vnet/ethernet/mac_address.h>
18 #include <vnet/ip/ip6_packet.h>
19 #include <vnet/ip/ip_format_fns.h>
20 #include <vpp/api/types.h>
21 #include "jsonconvert.h"
22
23 #define _(T)                                    \
24 int vl_api_ ##T## _fromjson(cJSON *o, T *d)     \
25 {                                               \
26     if (!cJSON_IsNumber(o)) return -1;          \
27     memcpy(d, &o->valueint, sizeof(T));         \
28     return 0;                                   \
29 }
30   foreach_vat2_fromjson
31 #undef _
32
33 int vl_api_bool_fromjson(cJSON *o, bool *d)
34 {
35     if (!cJSON_IsBool(o)) return -1;
36     *d = o->valueint ? true : false;
37     return 0;
38 }
39
40 int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
41 {
42     unformat_input_t input;
43     char *p = cJSON_GetStringValue(o);
44     unformat_init_string (&input, p, strlen(p));
45     if (!unformat (&input, "0x%U", unformat_hex_string, s))
46       return -1;
47     return 0;
48 }
49
50 u8 *
51 u8string_fromjson(cJSON *o, char *fieldname)
52 {
53     u8 *s = 0;
54     unformat_input_t input;
55     cJSON *item = cJSON_GetObjectItem(o, fieldname);
56     if (!item) {
57         printf("Illegal JSON, no such fieldname %s\n", fieldname);
58         return 0;
59     }
60
61     char *p = cJSON_GetStringValue(item);
62     unformat_init_string (&input, p, strlen(p));
63     if (!unformat (&input, "0x%U", unformat_hex_string, &s))
64       return 0;
65     return s;
66 }
67
68 int
69 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
70 {
71     u8 *s = u8string_fromjson(o, fieldname);
72     if (!s)
73       return -1;
74     memcpy(data, s, vec_len(s));
75     vec_free(s);
76     return 0;
77 }
78
79 /* Parse an IP4 address %d.%d.%d.%d. */
80 uword
81 unformat_ip4_address (unformat_input_t * input, va_list * args)
82 {
83   u8 *result = va_arg (*args, u8 *);
84   unsigned a[4];
85
86   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
87     return 0;
88
89   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
90     return 0;
91
92   result[0] = a[0];
93   result[1] = a[1];
94   result[2] = a[2];
95   result[3] = a[3];
96
97   return 1;
98 }
99
100 /* Parse an IP6 address. */
101 uword
102 unformat_ip6_address (unformat_input_t * input, va_list * args)
103 {
104   ip6_address_t *result = va_arg (*args, ip6_address_t *);
105   u16 hex_quads[8];
106   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
107   uword c, n_colon, double_colon_index;
108
109   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
110   double_colon_index = ARRAY_LEN (hex_quads);
111   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
112     {
113       hex_digit = 16;
114       if (c >= '0' && c <= '9')
115         hex_digit = c - '0';
116       else if (c >= 'a' && c <= 'f')
117         hex_digit = c + 10 - 'a';
118       else if (c >= 'A' && c <= 'F')
119         hex_digit = c + 10 - 'A';
120       else if (c == ':' && n_colon < 2)
121         n_colon++;
122       else
123         {
124           unformat_put_input (input);
125           break;
126         }
127
128       /* Too many hex quads. */
129       if (n_hex_quads >= ARRAY_LEN (hex_quads))
130         return 0;
131
132       if (hex_digit < 16)
133         {
134           hex_quad = (hex_quad << 4) | hex_digit;
135
136           /* Hex quad must fit in 16 bits. */
137           if (n_hex_digits >= 4)
138             return 0;
139
140           n_colon = 0;
141           n_hex_digits++;
142         }
143
144       /* Save position of :: */
145       if (n_colon == 2)
146         {
147           /* More than one :: ? */
148           if (double_colon_index < ARRAY_LEN (hex_quads))
149             return 0;
150           double_colon_index = n_hex_quads;
151         }
152
153       if (n_colon > 0 && n_hex_digits > 0)
154         {
155           hex_quads[n_hex_quads++] = hex_quad;
156           hex_quad = 0;
157           n_hex_digits = 0;
158         }
159     }
160
161   if (n_hex_digits > 0)
162     hex_quads[n_hex_quads++] = hex_quad;
163
164
165   {
166     word i;
167
168     /* Expand :: to appropriate number of zero hex quads. */
169     if (double_colon_index < ARRAY_LEN (hex_quads))
170       {
171         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
172
173         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
174           hex_quads[n_zero + i] = hex_quads[i];
175
176         for (i = 0; i < n_zero; i++)
177           {
178             ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
179             hex_quads[double_colon_index + i] = 0;
180           }
181
182         n_hex_quads = ARRAY_LEN (hex_quads);
183       }
184
185     /* Too few hex quads given. */
186     if (n_hex_quads < ARRAY_LEN (hex_quads))
187       return 0;
188
189     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
190       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
191
192     return 1;
193   }
194 }
195
196 u8 *
197 format_ip6_address (u8 * s, va_list * args)
198 {
199   ip6_address_t *a = va_arg (*args, ip6_address_t *);
200   u32 max_zero_run = 0, this_zero_run = 0;
201   int max_zero_run_index = -1, this_zero_run_index = 0;
202   int in_zero_run = 0, i;
203   int last_double_colon = 0;
204
205   /* Ugh, this is a pain. Scan forward looking for runs of 0's */
206   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
207     {
208       if (a->as_u16[i] == 0)
209         {
210           if (in_zero_run)
211             this_zero_run++;
212           else
213             {
214               in_zero_run = 1;
215               this_zero_run = 1;
216               this_zero_run_index = i;
217             }
218         }
219       else
220         {
221           if (in_zero_run)
222             {
223               /* offer to compress the biggest run of > 1 zero */
224               if (this_zero_run > max_zero_run && this_zero_run > 1)
225                 {
226                   max_zero_run_index = this_zero_run_index;
227                   max_zero_run = this_zero_run;
228                 }
229             }
230           in_zero_run = 0;
231           this_zero_run = 0;
232         }
233     }
234
235   if (in_zero_run)
236     {
237       if (this_zero_run > max_zero_run && this_zero_run > 1)
238         {
239           max_zero_run_index = this_zero_run_index;
240           max_zero_run = this_zero_run;
241         }
242     }
243
244   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
245     {
246       if (i == max_zero_run_index)
247         {
248           s = format (s, "::");
249           i += max_zero_run - 1;
250           last_double_colon = 1;
251         }
252       else
253         {
254           s = format (s, "%s%x",
255                       (last_double_colon || i == 0) ? "" : ":",
256                       clib_net_to_host_u16 (a->as_u16[i]));
257           last_double_colon = 0;
258         }
259     }
260
261   return s;
262 }
263
264 int
265 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
266                                vl_api_ip4_address_t *a)
267 {
268     unformat_input_t input;
269     char *p = cJSON_GetStringValue(o);
270     if (!p)
271       return -1;
272     unformat_init_string (&input, p, strlen(p));
273     unformat(&input, "%U", unformat_ip4_address, a);
274     return 0;
275 }
276
277 int
278 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
279                               vl_api_ip4_prefix_t *a)
280 {
281     unformat_input_t input;
282     char *p = cJSON_GetStringValue(o);
283     if (!p)
284       return -1;
285     unformat_init_string (&input, p, strlen(p));
286     unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
287     return 0;
288 }
289
290 int
291 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
292                                            vl_api_ip4_prefix_t *a)
293 {
294   return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
295 }
296 int
297 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
298                                vl_api_ip6_address_t *a)
299 {
300     unformat_input_t input;
301     char *p = cJSON_GetStringValue(o);
302     if (!p)
303       return -1;
304     unformat_init_string (&input, p, strlen(p));
305     unformat(&input, "%U", unformat_ip6_address, a);
306     return 0;
307 }
308
309 int
310 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
311                               vl_api_ip6_prefix_t *a)
312 {
313   unformat_input_t input;
314   char *p = cJSON_GetStringValue(o);
315   if (!p)
316     return -1;
317   unformat_init_string (&input, p, strlen(p));
318   unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
319   return 0;
320 }
321
322 int
323 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
324                                            vl_api_ip6_prefix_t *a)
325 {
326   return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
327 }
328
329 int
330 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
331 {
332   unformat_input_t input;
333
334   char *p = cJSON_GetStringValue(o);
335   if (!p)
336     return -1;
337   unformat_init_string (&input, p, strlen(p));
338   if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
339     a->af = ADDRESS_IP4;
340   else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
341     a->af = ADDRESS_IP6;
342   else
343     return -1;
344   return 0;
345 }
346
347 int
348 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
349 {
350   unformat_input_t input;
351
352   char *p = cJSON_GetStringValue(o);
353
354   if (!p)
355     return -1;
356   unformat_init_string (&input, p, strlen(p));
357   int plen;
358   if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
359     a->address.af = ADDRESS_IP4;
360   else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
361     a->address.af = ADDRESS_IP6;
362   else
363     return -1;
364   a->len = plen;
365   return 0;
366 }
367
368 int
369 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
370                                        vl_api_prefix_t *a)
371 {
372   return vl_api_prefix_t_fromjson(mp, len, o, a);
373 }
374
375 uword
376 unformat_mac_address (unformat_input_t * input, va_list * args)
377 {
378   mac_address_t *mac = va_arg (*args, mac_address_t *);
379   u32 i, a[3];
380
381   if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
382                 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
383                 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
384     return (1);
385   else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
386     {
387       for (i = 0; i < ARRAY_LEN (a); i++)
388         if (a[i] >= (1 << 16))
389           return 0;
390
391       mac->bytes[0] = (a[0] >> 8) & 0xff;
392       mac->bytes[1] = (a[0] >> 0) & 0xff;
393       mac->bytes[2] = (a[1] >> 8) & 0xff;
394       mac->bytes[3] = (a[1] >> 0) & 0xff;
395       mac->bytes[4] = (a[2] >> 8) & 0xff;
396       mac->bytes[5] = (a[2] >> 0) & 0xff;
397
398       return (1);
399     }
400   return (0);
401 }
402
403 int
404 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
405                                vl_api_mac_address_t *a)
406 {
407   unformat_input_t input;
408
409   char *p = cJSON_GetStringValue(o);
410   unformat_init_string (&input, p, strlen(p));
411   unformat(&input, "%U", unformat_mac_address, a);
412   return 0;
413 }
414
415 /* Format an IP4 address. */
416 u8 *
417 format_ip4_address (u8 * s, va_list * args)
418 {
419   u8 *a = va_arg (*args, u8 *);
420   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
421 }
422
423 int
424 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
425 {
426   /* copy without nul terminator */
427   u32 len = strlen (buf);
428   if (len > 0)
429     clib_memcpy_fast (str->buf, buf, len);
430   str->length = htonl (len);
431   return len + sizeof (u32);
432 }
433
434 u8 *
435 format_vl_api_interface_index_t (u8 *s, va_list *args)
436 {
437   u32 *a = va_arg (*args, u32 *);
438   return format (s, "%u", *a);
439 }
440
441 uword
442 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
443 {
444     u32 *a = va_arg (*args, u32 *);
445
446     if (!unformat (input, "%u", a))
447         return 0;
448     return 1;
449 }
450
451 void
452 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
453 {
454
455     if (astr == 0) return;
456     u32 length = clib_net_to_host_u32 (astr->length);
457
458     char *cstr = malloc(length + 1);
459     memcpy(cstr, astr->buf, length);
460     cstr[length] = '\0';
461     cJSON_AddStringToObject(object, name, cstr);
462     free(cstr);
463 }
464
465 u8 *
466 format_vl_api_timestamp_t(u8 * s, va_list * args)
467 {
468     f64 timestamp = va_arg (*args, f64);
469     struct tm *tm;
470     word msec;
471
472     time_t t = timestamp;
473     tm = gmtime (&t);
474     msec = 1e6 * (timestamp - t);
475     return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
476                    1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
477                    tm->tm_sec, msec);
478 }
479
480 u8 *
481 format_vl_api_timedelta_t(u8 * s, va_list * args)
482 {
483     return format_vl_api_timestamp_t(s, args);
484 }
485
486 uword
487 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
488 {
489     return 0;
490 }
491
492 uword
493 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
494 {
495     return 0;
496 }
497 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
498 {
499     return 0;
500 }
501 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
502 {
503     return 0;
504 }
505
506 cJSON *
507 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
508   u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
509   cJSON *o = cJSON_CreateString((char *)s);
510   vec_free(s);
511   return o;
512 }
513 cJSON *
514 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
515   u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
516   cJSON *o = cJSON_CreateString((char *)s);
517   vec_free(s);
518   return o;
519 }
520 cJSON *
521 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
522   u8 *s = format(0, "%U", format_vl_api_address_t, a);
523   cJSON *o = cJSON_CreateString((char *)s);
524   vec_free(s);
525   return o;
526 }
527 u8 *
528 format_vl_api_mac_address_t (u8 * s, va_list * args)
529 {
530   const mac_address_t *mac = va_arg (*args, mac_address_t *);
531
532   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
533                  mac->bytes[0], mac->bytes[1], mac->bytes[2],
534                  mac->bytes[3], mac->bytes[4], mac->bytes[5]);
535 }
536 #define _(T)                                                \
537   cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) {   \
538   u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a);      \
539   cJSON *o = cJSON_CreateString((char *)s);                 \
540   vec_free(s);                                              \
541   return o;                                                 \
542   }
543 foreach_vat2_tojson
544 #undef _