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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "strerror_override.h"
  14. #include <ctype.h>
  15. #include <limits.h>
  16. #include <stdarg.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif /* HAVE_SYS_TYPES_H */
  24. #ifdef HAVE_SYS_STAT_H
  25. #include <sys/stat.h>
  26. #endif /* HAVE_SYS_STAT_H */
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif /* HAVE_FCNTL_H */
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif /* HAVE_UNISTD_H */
  33. #ifdef WIN32
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <io.h>
  36. #include <windows.h>
  37. #endif /* defined(WIN32) */
  38. #if !defined(HAVE_OPEN) && defined(WIN32)
  39. #define open _open
  40. #endif
  41. #include "snprintf_compat.h"
  42. #include "debug.h"
  43. #include "json_inttypes.h"
  44. #include "json_object.h"
  45. #include "json_tokener.h"
  46. #include "json_util.h"
  47. #include "printbuf.h"
  48. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
  49. static char _last_err[256] = "";
  50. const char *json_util_get_last_err()
  51. {
  52. if (_last_err[0] == '\0')
  53. return NULL;
  54. return _last_err;
  55. }
  56. void _json_c_set_last_err(const char *err_fmt, ...)
  57. {
  58. va_list ap;
  59. va_start(ap, err_fmt);
  60. // Ignore (attempted) overruns from snprintf
  61. (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
  62. va_end(ap);
  63. }
  64. struct json_object *json_object_from_fd(int fd)
  65. {
  66. return json_object_from_fd_ex(fd, -1);
  67. }
  68. struct json_object *json_object_from_fd_ex(int fd, int in_depth)
  69. {
  70. struct printbuf *pb;
  71. struct json_object *obj;
  72. char buf[JSON_FILE_BUF_SIZE];
  73. int ret;
  74. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  75. json_tokener *tok;
  76. if (!(pb = printbuf_new()))
  77. {
  78. _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
  79. return NULL;
  80. }
  81. if (in_depth != -1)
  82. depth = in_depth;
  83. tok = json_tokener_new_ex(depth);
  84. if (!tok)
  85. {
  86. _json_c_set_last_err(
  87. "json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth,
  88. strerror(errno));
  89. printbuf_free(pb);
  90. return NULL;
  91. }
  92. while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
  93. {
  94. printbuf_memappend(pb, buf, ret);
  95. }
  96. if (ret < 0)
  97. {
  98. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd,
  99. strerror(errno));
  100. json_tokener_free(tok);
  101. printbuf_free(pb);
  102. return NULL;
  103. }
  104. obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
  105. if (obj == NULL)
  106. _json_c_set_last_err("json_tokener_parse_ex failed: %s\n",
  107. json_tokener_error_desc(json_tokener_get_error(tok)));
  108. json_tokener_free(tok);
  109. printbuf_free(pb);
  110. return obj;
  111. }
  112. struct json_object *json_object_from_file(const char *filename)
  113. {
  114. struct json_object *obj;
  115. int fd;
  116. if ((fd = open(filename, O_RDONLY)) < 0)
  117. {
  118. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n", filename,
  119. strerror(errno));
  120. return NULL;
  121. }
  122. obj = json_object_from_fd(fd);
  123. close(fd);
  124. return obj;
  125. }
  126. /* extended "format and write to file" function */
  127. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  128. {
  129. int fd, ret;
  130. int saved_errno;
  131. if (!obj)
  132. {
  133. _json_c_set_last_err("json_object_to_file: object is null\n");
  134. return -1;
  135. }
  136. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
  137. {
  138. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n", filename,
  139. strerror(errno));
  140. return -1;
  141. }
  142. ret = _json_object_to_fd(fd, obj, flags, filename);
  143. saved_errno = errno;
  144. close(fd);
  145. errno = saved_errno;
  146. return ret;
  147. }
  148. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  149. {
  150. if (!obj)
  151. {
  152. _json_c_set_last_err("json_object_to_fd: object is null\n");
  153. return -1;
  154. }
  155. return _json_object_to_fd(fd, obj, flags, NULL);
  156. }
  157. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  158. {
  159. int ret;
  160. const char *json_str;
  161. unsigned int wpos, wsize;
  162. filename = filename ? filename : "(fd)";
  163. if (!(json_str = json_object_to_json_string_ext(obj, flags)))
  164. {
  165. return -1;
  166. }
  167. /* CAW: probably unnecessary, but the most 64bit safe */
  168. wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
  169. wpos = 0;
  170. while (wpos < wsize)
  171. {
  172. if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0)
  173. {
  174. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  175. filename, strerror(errno));
  176. return -1;
  177. }
  178. /* because of the above check for ret < 0, we can safely cast and add */
  179. wpos += (unsigned int)ret;
  180. }
  181. return 0;
  182. }
  183. // backwards compatible "format and write to file" function
  184. int json_object_to_file(const char *filename, struct json_object *obj)
  185. {
  186. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  187. }
  188. int json_parse_double(const char *buf, double *retval)
  189. {
  190. char *end;
  191. *retval = strtod(buf, &end);
  192. return end == buf ? 1 : 0;
  193. }
  194. int json_parse_int64(const char *buf, int64_t *retval)
  195. {
  196. char *end = NULL;
  197. int64_t val;
  198. errno = 0;
  199. val = strtoll(buf, &end, 10);
  200. if (end != buf)
  201. *retval = val;
  202. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  203. }
  204. int json_parse_uint64(const char *buf, uint64_t *retval)
  205. {
  206. char *end = NULL;
  207. uint64_t val;
  208. errno = 1;
  209. while (*buf == ' ')
  210. {
  211. buf++;
  212. }
  213. if (*buf == '-')
  214. errno = 0;
  215. val = strtoull(buf, &end, 10);
  216. if (end != buf)
  217. *retval = val;
  218. return ((errno == 0) || (end == buf)) ? 1 : 0;
  219. }
  220. #ifndef HAVE_REALLOC
  221. void *rpl_realloc(void *p, size_t n)
  222. {
  223. if (n == 0)
  224. n = 1;
  225. if (p == 0)
  226. return malloc(n);
  227. return realloc(p, n);
  228. }
  229. #endif
  230. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  231. /* clang-format off */
  232. static const char *json_type_name[] = {
  233. /* If you change this, be sure to update the enum json_type definition too */
  234. "null",
  235. "boolean",
  236. "double",
  237. "int",
  238. "object",
  239. "array",
  240. "string",
  241. };
  242. /* clang-format on */
  243. const char *json_type_to_name(enum json_type o_type)
  244. {
  245. int o_type_int = (int)o_type;
  246. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  247. {
  248. _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type,
  249. NELEM(json_type_name));
  250. return NULL;
  251. }
  252. return json_type_name[o_type];
  253. }