Print this page
5005 libnvpair JSON output broken by lint fixes
5006 libnvpair JSON cannot print int16 arrays
Reviewed by: Robert Mustacchi <rm@joyent.com>
   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 /*
  12  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  13  */
  14 
  15 #include <stdio.h>
  16 #include <stdlib.h>
  17 #include <strings.h>
  18 #include <wchar.h>
  19 #include <sys/debug.h>
  20 
  21 #include "libnvpair.h"
  22 
  23 #define FPRINTF(fp, ...)                        \

  24         if (fprintf(fp, __VA_ARGS__) < 0)    \
  25                 return (-1)                     \

  26 
  27 /*
  28  * When formatting a string for JSON output we must escape certain characters,
  29  * as described in RFC4627.  This applies to both member names and
  30  * DATA_TYPE_STRING values.
  31  *
  32  * This function will only operate correctly if the following conditions are
  33  * met:
  34  *
  35  *       1. The input String is encoded in the current locale.
  36  *
  37  *       2. The current locale includes the Basic Multilingual Plane (plane 0)
  38  *          as defined in the Unicode standard.
  39  *
  40  * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all
  41  * representable Unicode characters included in their escaped numeric form.
  42  */
  43 static int
  44 nvlist_print_json_string(FILE *fp, const char *input)
  45 {


 311                         uint_t valsz, i;
 312                         VERIFY0(nvpair_value_uint16_array(curr, &val, &valsz));
 313                         FPRINTF(fp, "[");
 314                         for (i = 0; i < valsz; i++) {
 315                                 if (i > 0)
 316                                         FPRINTF(fp, ",");
 317                                 FPRINTF(fp, "%hu", val[i]);
 318                         }
 319                         FPRINTF(fp, "]");
 320                         break;
 321                 }
 322 
 323                 case DATA_TYPE_INT16_ARRAY: {
 324                         int16_t *val;
 325                         uint_t valsz, i;
 326                         VERIFY0(nvpair_value_int16_array(curr, &val, &valsz));
 327                         FPRINTF(fp, "[");
 328                         for (i = 0; i < valsz; i++) {
 329                                 if (i > 0)
 330                                         FPRINTF(fp, ",");
 331                                 FPRINTF(fp, "%hhd", val[i]);
 332                         }
 333                         FPRINTF(fp, "]");
 334                         break;
 335                 }
 336 
 337                 case DATA_TYPE_UINT32_ARRAY: {
 338                         uint32_t *val;
 339                         uint_t valsz, i;
 340                         VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz));
 341                         FPRINTF(fp, "[");
 342                         for (i = 0; i < valsz; i++) {
 343                                 if (i > 0)
 344                                         FPRINTF(fp, ",");
 345                                 FPRINTF(fp, "%u", val[i]);
 346                         }
 347                         FPRINTF(fp, "]");
 348                         break;
 349                 }
 350 
 351                 case DATA_TYPE_INT32_ARRAY: {


   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 /*
  12  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
  13  */
  14 
  15 #include <stdio.h>
  16 #include <stdlib.h>
  17 #include <strings.h>
  18 #include <wchar.h>
  19 #include <sys/debug.h>
  20 
  21 #include "libnvpair.h"
  22 
  23 #define FPRINTF(fp, ...)                                \
  24         do {                                            \
  25                 if (fprintf(fp, __VA_ARGS__) < 0)    \
  26                         return (-1);                    \
  27         } while (0)
  28 
  29 /*
  30  * When formatting a string for JSON output we must escape certain characters,
  31  * as described in RFC4627.  This applies to both member names and
  32  * DATA_TYPE_STRING values.
  33  *
  34  * This function will only operate correctly if the following conditions are
  35  * met:
  36  *
  37  *       1. The input String is encoded in the current locale.
  38  *
  39  *       2. The current locale includes the Basic Multilingual Plane (plane 0)
  40  *          as defined in the Unicode standard.
  41  *
  42  * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all
  43  * representable Unicode characters included in their escaped numeric form.
  44  */
  45 static int
  46 nvlist_print_json_string(FILE *fp, const char *input)
  47 {


 313                         uint_t valsz, i;
 314                         VERIFY0(nvpair_value_uint16_array(curr, &val, &valsz));
 315                         FPRINTF(fp, "[");
 316                         for (i = 0; i < valsz; i++) {
 317                                 if (i > 0)
 318                                         FPRINTF(fp, ",");
 319                                 FPRINTF(fp, "%hu", val[i]);
 320                         }
 321                         FPRINTF(fp, "]");
 322                         break;
 323                 }
 324 
 325                 case DATA_TYPE_INT16_ARRAY: {
 326                         int16_t *val;
 327                         uint_t valsz, i;
 328                         VERIFY0(nvpair_value_int16_array(curr, &val, &valsz));
 329                         FPRINTF(fp, "[");
 330                         for (i = 0; i < valsz; i++) {
 331                                 if (i > 0)
 332                                         FPRINTF(fp, ",");
 333                                 FPRINTF(fp, "%hd", val[i]);
 334                         }
 335                         FPRINTF(fp, "]");
 336                         break;
 337                 }
 338 
 339                 case DATA_TYPE_UINT32_ARRAY: {
 340                         uint32_t *val;
 341                         uint_t valsz, i;
 342                         VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz));
 343                         FPRINTF(fp, "[");
 344                         for (i = 0; i < valsz; i++) {
 345                                 if (i > 0)
 346                                         FPRINTF(fp, ",");
 347                                 FPRINTF(fp, "%u", val[i]);
 348                         }
 349                         FPRINTF(fp, "]");
 350                         break;
 351                 }
 352 
 353                 case DATA_TYPE_INT32_ARRAY: {