You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_util.c 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  12. #undef realloc
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <ctype.h>
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif /* HAVE_SYS_TYPES_H */
  23. #ifdef HAVE_SYS_STAT_H
  24. #include <sys/stat.h>
  25. #endif /* HAVE_SYS_STAT_H */
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #endif /* HAVE_FCNTL_H */
  29. #ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. #endif /* HAVE_UNISTD_H */
  32. #ifdef HAVE_LOCALE_H
  33. #include <locale.h>
  34. #endif /* HAVE_LOCALE_H */
  35. #ifdef WIN32
  36. # define WIN32_LEAN_AND_MEAN
  37. # include <windows.h>
  38. # include <io.h>
  39. #endif /* defined(WIN32) */
  40. #if !defined(HAVE_OPEN) && defined(WIN32)
  41. # define open _open
  42. #endif
  43. #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
  44. /* MSC has the version as _snprintf */
  45. # define snprintf _snprintf
  46. #elif !defined(HAVE_SNPRINTF)
  47. # error You do not have snprintf on your system.
  48. #endif /* HAVE_SNPRINTF */
  49. #include "bits.h"
  50. #include "debug.h"
  51. #include "printbuf.h"
  52. #include "json_inttypes.h"
  53. #include "json_object.h"
  54. #include "json_tokener.h"
  55. #include "json_util.h"
  56. struct json_object* json_object_from_file(const char *filename)
  57. {
  58. struct printbuf *pb;
  59. struct json_object *obj;
  60. char buf[JSON_FILE_BUF_SIZE];
  61. int fd, ret;
  62. if((fd = open(filename, O_RDONLY)) < 0) {
  63. MC_ERROR("json_object_from_file: error reading file %s: %s\n",
  64. filename, strerror(errno));
  65. return NULL;
  66. }
  67. if(!(pb = printbuf_new())) {
  68. close(fd);
  69. MC_ERROR("json_object_from_file: printbuf_new failed\n");
  70. return NULL;
  71. }
  72. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  73. printbuf_memappend(pb, buf, ret);
  74. }
  75. close(fd);
  76. if(ret < 0) {
  77. MC_ABORT("json_object_from_file: error reading file %s: %s\n",
  78. filename, strerror(errno));
  79. printbuf_free(pb);
  80. return NULL;
  81. }
  82. obj = json_tokener_parse(pb->buf);
  83. printbuf_free(pb);
  84. return obj;
  85. }
  86. /* extended "format and write to file" function */
  87. int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
  88. {
  89. const char *json_str;
  90. int fd, ret;
  91. unsigned int wpos, wsize;
  92. if(!obj) {
  93. MC_ERROR("json_object_to_file: object is null\n");
  94. return -1;
  95. }
  96. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  97. MC_ERROR("json_object_to_file: error opening file %s: %s\n",
  98. filename, strerror(errno));
  99. return -1;
  100. }
  101. if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
  102. close(fd);
  103. return -1;
  104. }
  105. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  106. wpos = 0;
  107. while(wpos < wsize) {
  108. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  109. close(fd);
  110. MC_ERROR("json_object_to_file: error writing file %s: %s\n",
  111. filename, strerror(errno));
  112. return -1;
  113. }
  114. /* because of the above check for ret < 0, we can safely cast and add */
  115. wpos += (unsigned int)ret;
  116. }
  117. close(fd);
  118. return 0;
  119. }
  120. // backwards compatible "format and write to file" function
  121. int json_object_to_file(char *filename, struct json_object *obj)
  122. {
  123. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  124. }
  125. int json_parse_double(const char *buf, double *retval)
  126. {
  127. int ret;
  128. #ifdef HAVE_SETLOCALE
  129. char *old=NULL, *tmp;
  130. tmp = setlocale(LC_NUMERIC, NULL);
  131. if (tmp) old = strdup(tmp);
  132. setlocale(LC_NUMERIC, "C");
  133. #endif
  134. ret = sscanf(buf, "%lf", retval);
  135. #ifdef HAVE_SETLOCALE
  136. setlocale(LC_NUMERIC, old);
  137. if (old) free(old);
  138. #endif
  139. return (ret==1 ? 0 : 1);
  140. }
  141. int json_parse_int64(const char *buf, int64_t *retval)
  142. {
  143. int64_t num64;
  144. const char *buf_skip_space;
  145. int orig_has_neg;
  146. int _errno;
  147. errno = 0; // sscanf won't always set errno, so initialize
  148. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  149. {
  150. MC_DEBUG("Failed to parse, sscanf != 1\n");
  151. return 1;
  152. }
  153. _errno = errno;
  154. buf_skip_space = buf;
  155. orig_has_neg = 0;
  156. // Skip leading spaces
  157. while (isspace((int)*buf_skip_space) && *buf_skip_space)
  158. buf_skip_space++;
  159. if (*buf_skip_space == '-')
  160. {
  161. buf_skip_space++;
  162. orig_has_neg = 1;
  163. }
  164. // Skip leading zeros, but keep at least one digit
  165. while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
  166. buf_skip_space++;
  167. if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
  168. orig_has_neg = 0; // "-0" is the same as just plain "0"
  169. if (_errno != ERANGE)
  170. {
  171. char buf_cmp[100];
  172. char *buf_cmp_start = buf_cmp;
  173. int recheck_has_neg = 0;
  174. int buf_cmp_len;
  175. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  176. if (*buf_cmp_start == '-')
  177. {
  178. recheck_has_neg = 1;
  179. buf_cmp_start++;
  180. }
  181. // No need to skip leading spaces or zeros here.
  182. buf_cmp_len = strlen(buf_cmp_start);
  183. /**
  184. * If the sign is different, or
  185. * some of the digits are different, or
  186. * there is another digit present in the original string
  187. * then we NOT successfully parsed the value.
  188. */
  189. if (orig_has_neg != recheck_has_neg ||
  190. strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  191. (strlen(buf_skip_space) != buf_cmp_len &&
  192. isdigit((int)buf_skip_space[buf_cmp_len])
  193. )
  194. )
  195. {
  196. _errno = ERANGE;
  197. }
  198. }
  199. if (_errno == ERANGE)
  200. {
  201. if (orig_has_neg)
  202. num64 = INT64_MIN;
  203. else
  204. num64 = INT64_MAX;
  205. }
  206. *retval = num64;
  207. return 0;
  208. }
  209. #ifndef HAVE_REALLOC
  210. void* rpl_realloc(void* p, size_t n)
  211. {
  212. if (n == 0)
  213. n = 1;
  214. if (p == 0)
  215. return malloc(n);
  216. return realloc(p, n);
  217. }
  218. #endif
  219. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  220. static const char* json_type_name[] = {
  221. /* If you change this, be sure to update the enum json_type definition too */
  222. "null",
  223. "boolean",
  224. "double",
  225. "int",
  226. "object",
  227. "array",
  228. "string",
  229. };
  230. const char *json_type_to_name(enum json_type o_type)
  231. {
  232. if (o_type < 0 || o_type >= NELEM(json_type_name))
  233. {
  234. MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  235. return NULL;
  236. }
  237. return json_type_name[o_type];
  238. }