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

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