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