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

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