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

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