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

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