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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if(!(pb = printbuf_new())) {
  79. _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
  80. return NULL;
  81. }
  82. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  83. if (in_depth != -1)
  84. depth = in_depth;
  85. json_tokener *tok = json_tokener_new_ex(depth);
  86. if (!tok)
  87. {
  88. _json_c_set_last_err("json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth, strerror(errno));
  89. printbuf_free(pb);
  90. return NULL;
  91. }
  92. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  93. printbuf_memappend(pb, buf, ret);
  94. }
  95. if(ret < 0) {
  96. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
  97. json_tokener_free(tok);
  98. printbuf_free(pb);
  99. return NULL;
  100. }
  101. obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
  102. if (obj == NULL)
  103. _json_c_set_last_err("json_tokener_parse_ex failed: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
  104. json_tokener_free(tok);
  105. printbuf_free(pb);
  106. return obj;
  107. }
  108. struct json_object* json_object_from_file(const char *filename)
  109. {
  110. struct json_object *obj;
  111. int fd;
  112. if((fd = open(filename, O_RDONLY)) < 0) {
  113. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
  114. filename, strerror(errno));
  115. return NULL;
  116. }
  117. obj = json_object_from_fd(fd);
  118. close(fd);
  119. return obj;
  120. }
  121. /* extended "format and write to file" function */
  122. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  123. {
  124. int fd, ret;
  125. int saved_errno;
  126. if (!obj) {
  127. _json_c_set_last_err("json_object_to_file: object is null\n");
  128. return -1;
  129. }
  130. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  131. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n",
  132. filename, strerror(errno));
  133. return -1;
  134. }
  135. ret = _json_object_to_fd(fd, obj, flags, filename);
  136. saved_errno = errno;
  137. close(fd);
  138. errno = saved_errno;
  139. return ret;
  140. }
  141. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  142. {
  143. if (!obj) {
  144. _json_c_set_last_err("json_object_to_fd: object is null\n");
  145. return -1;
  146. }
  147. return _json_object_to_fd(fd, obj, flags, NULL);
  148. }
  149. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  150. {
  151. int ret;
  152. const char *json_str;
  153. unsigned int wpos, wsize;
  154. filename = filename ? filename : "(fd)";
  155. if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
  156. return -1;
  157. }
  158. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  159. wpos = 0;
  160. while(wpos < wsize) {
  161. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  162. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  163. filename, strerror(errno));
  164. return -1;
  165. }
  166. /* because of the above check for ret < 0, we can safely cast and add */
  167. wpos += (unsigned int)ret;
  168. }
  169. return 0;
  170. }
  171. // backwards compatible "format and write to file" function
  172. int json_object_to_file(const char *filename, struct json_object *obj)
  173. {
  174. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  175. }
  176. int json_parse_double(const char *buf, double *retval)
  177. {
  178. char *end;
  179. *retval = strtod(buf, &end);
  180. return end == buf ? 1 : 0;
  181. }
  182. int json_parse_int64(const char *buf, int64_t *retval)
  183. {
  184. char *end = NULL;
  185. int64_t val;
  186. errno = 0;
  187. val = strtoll(buf, &end, 10);
  188. if (end != buf)
  189. *retval = val;
  190. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  191. }
  192. #ifndef HAVE_REALLOC
  193. void* rpl_realloc(void* p, size_t n)
  194. {
  195. if (n == 0)
  196. n = 1;
  197. if (p == 0)
  198. return malloc(n);
  199. return realloc(p, n);
  200. }
  201. #endif
  202. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  203. static const char* json_type_name[] = {
  204. /* If you change this, be sure to update the enum json_type definition too */
  205. "null",
  206. "boolean",
  207. "double",
  208. "int",
  209. "object",
  210. "array",
  211. "string",
  212. };
  213. const char *json_type_to_name(enum json_type o_type)
  214. {
  215. int o_type_int = (int)o_type;
  216. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  217. {
  218. _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  219. return NULL;
  220. }
  221. return json_type_name[o_type];
  222. }