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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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()
  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. int 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_file: 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: unable to allocate json_tokener(depth=%d): %s\n", depth,
  87. strerror(errno));
  88. printbuf_free(pb);
  89. return NULL;
  90. }
  91. while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
  92. {
  93. printbuf_memappend(pb, buf, ret);
  94. }
  95. if (ret < 0)
  96. {
  97. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd,
  98. strerror(errno));
  99. json_tokener_free(tok);
  100. printbuf_free(pb);
  101. return NULL;
  102. }
  103. obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
  104. if (obj == NULL)
  105. _json_c_set_last_err("json_tokener_parse_ex failed: %s\n",
  106. json_tokener_error_desc(json_tokener_get_error(tok)));
  107. json_tokener_free(tok);
  108. printbuf_free(pb);
  109. return obj;
  110. }
  111. struct json_object *json_object_from_file(const char *filename)
  112. {
  113. struct json_object *obj;
  114. int fd;
  115. if ((fd = open(filename, O_RDONLY)) < 0)
  116. {
  117. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n", filename,
  118. strerror(errno));
  119. return NULL;
  120. }
  121. obj = json_object_from_fd(fd);
  122. close(fd);
  123. return obj;
  124. }
  125. /* extended "format and write to file" function */
  126. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  127. {
  128. int fd, ret;
  129. int saved_errno;
  130. if (!obj)
  131. {
  132. _json_c_set_last_err("json_object_to_file: object is null\n");
  133. return -1;
  134. }
  135. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
  136. {
  137. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n", filename,
  138. strerror(errno));
  139. return -1;
  140. }
  141. ret = _json_object_to_fd(fd, obj, flags, filename);
  142. saved_errno = errno;
  143. close(fd);
  144. errno = saved_errno;
  145. return ret;
  146. }
  147. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  148. {
  149. if (!obj)
  150. {
  151. _json_c_set_last_err("json_object_to_fd: object is null\n");
  152. return -1;
  153. }
  154. return _json_object_to_fd(fd, obj, flags, NULL);
  155. }
  156. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  157. {
  158. int ret;
  159. const char *json_str;
  160. unsigned int wpos, wsize;
  161. filename = filename ? filename : "(fd)";
  162. if (!(json_str = json_object_to_json_string_ext(obj, flags)))
  163. {
  164. return -1;
  165. }
  166. /* CAW: probably unnecessary, but the most 64bit safe */
  167. wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
  168. wpos = 0;
  169. while (wpos < wsize)
  170. {
  171. if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0)
  172. {
  173. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  174. filename, strerror(errno));
  175. return -1;
  176. }
  177. /* because of the above check for ret < 0, we can safely cast and add */
  178. wpos += (unsigned int)ret;
  179. }
  180. return 0;
  181. }
  182. // backwards compatible "format and write to file" function
  183. int json_object_to_file(const char *filename, struct json_object *obj)
  184. {
  185. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  186. }
  187. // Deprecated json_parse_double function. See json_tokener_parse_double instead.
  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 = 0;
  209. while (*buf == ' ')
  210. buf++;
  211. if (*buf == '-')
  212. return 1; /* error: uint cannot be negative */
  213. val = strtoull(buf, &end, 10);
  214. if (end != buf)
  215. *retval = val;
  216. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  217. }
  218. #ifndef HAVE_REALLOC
  219. void *rpl_realloc(void *p, size_t n)
  220. {
  221. if (n == 0)
  222. n = 1;
  223. if (p == 0)
  224. return malloc(n);
  225. return realloc(p, n);
  226. }
  227. #endif
  228. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  229. /* clang-format off */
  230. static const char *json_type_name[] = {
  231. /* If you change this, be sure to update the enum json_type definition too */
  232. "null",
  233. "boolean",
  234. "double",
  235. "int",
  236. "object",
  237. "array",
  238. "string",
  239. };
  240. /* clang-format on */
  241. const char *json_type_to_name(enum json_type o_type)
  242. {
  243. int o_type_int = (int)o_type;
  244. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  245. {
  246. _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type,
  247. NELEM(json_type_name));
  248. return NULL;
  249. }
  250. return json_type_name[o_type];
  251. }