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

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