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 7.2 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <ctype.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 <windows.h>
  35. # include <io.h>
  36. #endif /* defined(WIN32) */
  37. #if !defined(HAVE_OPEN) && defined(WIN32)
  38. # define open _open
  39. #endif
  40. #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
  41. /* MSC has the version as _snprintf */
  42. # define snprintf _snprintf
  43. #elif !defined(HAVE_SNPRINTF)
  44. # error You do not have snprintf on your system.
  45. #endif /* HAVE_SNPRINTF */
  46. #include "bits.h"
  47. #include "debug.h"
  48. #include "printbuf.h"
  49. #include "json_inttypes.h"
  50. #include "json_object.h"
  51. #include "json_tokener.h"
  52. #include "json_util.h"
  53. static int sscanf_is_broken = 0;
  54. static int sscanf_is_broken_testdone = 0;
  55. static void sscanf_is_broken_test(void);
  56. struct json_object* json_object_from_file(const char *filename)
  57. {
  58. struct printbuf *pb;
  59. struct json_object *obj;
  60. char buf[JSON_FILE_BUF_SIZE];
  61. int fd;
  62. ssize_t ret;
  63. if((fd = open(filename, O_RDONLY)) < 0) {
  64. MC_ERROR("json_object_from_file: error reading file %s: %s\n",
  65. filename, strerror(errno));
  66. return NULL;
  67. }
  68. if(!(pb = printbuf_new())) {
  69. close(fd);
  70. MC_ERROR("json_object_from_file: printbuf_new failed\n");
  71. return NULL;
  72. }
  73. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  74. printbuf_memappend(pb, buf, ret);
  75. }
  76. close(fd);
  77. if(ret < 0) {
  78. MC_ABORT("json_object_from_file: error reading file %s: %s\n",
  79. filename, strerror(errno));
  80. printbuf_free(pb);
  81. return NULL;
  82. }
  83. obj = json_tokener_parse(pb->buf);
  84. printbuf_free(pb);
  85. return obj;
  86. }
  87. /* extended "format and write to file" function */
  88. int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
  89. {
  90. const char *json_str;
  91. int fd;
  92. ssize_t ret;
  93. unsigned int wpos, wsize;
  94. if(!obj) {
  95. MC_ERROR("json_object_to_file: object is null\n");
  96. return -1;
  97. }
  98. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  99. MC_ERROR("json_object_to_file: error opening file %s: %s\n",
  100. filename, strerror(errno));
  101. return -1;
  102. }
  103. if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
  104. close(fd);
  105. return -1;
  106. }
  107. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  108. wpos = 0;
  109. while(wpos < wsize) {
  110. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  111. close(fd);
  112. MC_ERROR("json_object_to_file: error writing file %s: %s\n",
  113. filename, strerror(errno));
  114. return -1;
  115. }
  116. /* because of the above check for ret < 0, we can safely cast and add */
  117. wpos += (unsigned int)ret;
  118. }
  119. close(fd);
  120. return 0;
  121. }
  122. // backwards compatible "format and write to file" function
  123. int json_object_to_file(char *filename, struct json_object *obj)
  124. {
  125. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  126. }
  127. int json_parse_double(const char *buf, double *retval)
  128. {
  129. return (sscanf(buf, "%lf", retval)==1 ? 0 : 1);
  130. }
  131. /*
  132. * Not all implementations of sscanf actually work properly.
  133. * Check whether the one we're currently using does, and if
  134. * it's broken, enable the workaround code.
  135. */
  136. static void sscanf_is_broken_test()
  137. {
  138. int64_t num64;
  139. (void)sscanf(" -01234567890123456789012345", "%" SCNd64, &num64);
  140. int ret_errno = errno;
  141. int is_int64_min = (num64 == INT64_MIN);
  142. (void)sscanf(" 01234567890123456789012345", "%" SCNd64, &num64);
  143. int ret_errno2 = errno;
  144. int is_int64_max = (num64 == INT64_MAX);
  145. if (ret_errno != ERANGE || !is_int64_min ||
  146. ret_errno2 != ERANGE || !is_int64_max)
  147. {
  148. MC_DEBUG("sscanf_is_broken_test failed, enabling workaround code\n");
  149. sscanf_is_broken = 1;
  150. }
  151. }
  152. int json_parse_int64(const char *buf, int64_t *retval)
  153. {
  154. int64_t num64;
  155. const char *buf_sig_digits;
  156. int orig_has_neg;
  157. int saved_errno;
  158. if (!sscanf_is_broken_testdone)
  159. {
  160. sscanf_is_broken_test();
  161. sscanf_is_broken_testdone = 1;
  162. }
  163. // Skip leading spaces
  164. while (isspace((int)*buf) && *buf)
  165. buf++;
  166. errno = 0; // sscanf won't always set errno, so initialize
  167. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  168. {
  169. MC_DEBUG("Failed to parse, sscanf != 1\n");
  170. return 1;
  171. }
  172. saved_errno = errno;
  173. buf_sig_digits = buf;
  174. orig_has_neg = 0;
  175. if (*buf_sig_digits == '-')
  176. {
  177. buf_sig_digits++;
  178. orig_has_neg = 1;
  179. }
  180. // Not all sscanf implementations actually work
  181. if (sscanf_is_broken && saved_errno != ERANGE)
  182. {
  183. char buf_cmp[100];
  184. char *buf_cmp_start = buf_cmp;
  185. int recheck_has_neg = 0;
  186. size_t buf_cmp_len;
  187. // Skip leading zeros, but keep at least one digit
  188. while (buf_sig_digits[0] == '0' && buf_sig_digits[1] != '\0')
  189. buf_sig_digits++;
  190. if (num64 == 0) // assume all sscanf impl's will parse -0 to 0
  191. orig_has_neg = 0; // "-0" is the same as just plain "0"
  192. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  193. if (*buf_cmp_start == '-')
  194. {
  195. recheck_has_neg = 1;
  196. buf_cmp_start++;
  197. }
  198. // No need to skip leading spaces or zeros here.
  199. buf_cmp_len = strlen(buf_cmp_start);
  200. /**
  201. * If the sign is different, or
  202. * some of the digits are different, or
  203. * there is another digit present in the original string
  204. * then we have NOT successfully parsed the value.
  205. */
  206. if (orig_has_neg != recheck_has_neg ||
  207. strncmp(buf_sig_digits, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  208. (strlen(buf_sig_digits) != buf_cmp_len &&
  209. isdigit((int)buf_sig_digits[buf_cmp_len])
  210. )
  211. )
  212. {
  213. saved_errno = ERANGE;
  214. }
  215. }
  216. // Not all sscanf impl's set the value properly when out of range.
  217. // Always do this, even for properly functioning implementations,
  218. // since it shouldn't slow things down much.
  219. if (saved_errno == ERANGE)
  220. {
  221. if (orig_has_neg)
  222. num64 = INT64_MIN;
  223. else
  224. num64 = INT64_MAX;
  225. }
  226. *retval = num64;
  227. return 0;
  228. }
  229. #ifndef HAVE_REALLOC
  230. void* rpl_realloc(void* p, size_t n)
  231. {
  232. if (n == 0)
  233. n = 1;
  234. if (p == 0)
  235. return malloc(n);
  236. return realloc(p, n);
  237. }
  238. #endif
  239. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  240. static const char* json_type_name[] = {
  241. /* If you change this, be sure to update the enum json_type definition too */
  242. "null",
  243. "boolean",
  244. "double",
  245. "int",
  246. "object",
  247. "array",
  248. "string",
  249. };
  250. const char *json_type_to_name(enum json_type o_type)
  251. {
  252. int o_type_int = (int)o_type;
  253. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  254. {
  255. MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  256. return NULL;
  257. }
  258. return json_type_name[o_type];
  259. }