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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #if HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif /* HAVE_SYS_TYPES_H */
  23. #if HAVE_SYS_STAT_H
  24. #include <sys/stat.h>
  25. #endif /* HAVE_SYS_STAT_H */
  26. #if HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #endif /* HAVE_FCNTL_H */
  29. #if 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 !HAVE_OPEN && defined(WIN32)
  38. # define open _open
  39. #endif
  40. #include "bits.h"
  41. #include "debug.h"
  42. #include "printbuf.h"
  43. #include "json_inttypes.h"
  44. #include "json_object.h"
  45. #include "json_tokener.h"
  46. #include "json_util.h"
  47. struct json_object* json_object_from_file(const char *filename)
  48. {
  49. struct printbuf *pb;
  50. struct json_object *obj;
  51. char buf[JSON_FILE_BUF_SIZE];
  52. int fd, ret;
  53. if((fd = open(filename, O_RDONLY)) < 0) {
  54. MC_ERROR("json_object_from_file: error reading file %s: %s\n",
  55. filename, strerror(errno));
  56. return NULL; // XAX this is an API change!
  57. }
  58. if(!(pb = printbuf_new())) {
  59. close(fd);
  60. MC_ERROR("json_object_from_file: printbuf_new failed\n");
  61. return NULL;
  62. }
  63. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  64. printbuf_memappend(pb, buf, ret);
  65. }
  66. close(fd);
  67. if(ret < 0) {
  68. MC_ABORT("json_object_from_file: error reading file %s: %s\n",
  69. filename, strerror(errno));
  70. printbuf_free(pb);
  71. return NULL;
  72. }
  73. obj = json_tokener_parse(pb->buf);
  74. printbuf_free(pb);
  75. return obj;
  76. }
  77. /* extended "format and write to file" function */
  78. int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
  79. {
  80. const char *json_str;
  81. int fd, ret;
  82. unsigned int wpos, wsize;
  83. if(!obj) {
  84. MC_ERROR("json_object_to_file: object is null\n");
  85. return -1;
  86. }
  87. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  88. MC_ERROR("json_object_to_file: error opening file %s: %s\n",
  89. filename, strerror(errno));
  90. return -1;
  91. }
  92. if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
  93. close(fd);
  94. return -1;
  95. }
  96. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  97. wpos = 0;
  98. while(wpos < wsize) {
  99. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  100. close(fd);
  101. MC_ERROR("json_object_to_file: error writing file %s: %s\n",
  102. filename, strerror(errno));
  103. return -1;
  104. }
  105. /* because of the above check for ret < 0, we can safely cast and add */
  106. wpos += (unsigned int)ret;
  107. }
  108. close(fd);
  109. return 0;
  110. }
  111. // backwards compatible "format and write to file" function
  112. int json_object_to_file(char *filename, struct json_object *obj)
  113. {
  114. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  115. }
  116. int json_parse_int64(const char *buf, int64_t *retval)
  117. {
  118. int64_t num64;
  119. const char *buf_skip_space;
  120. int orig_has_neg;
  121. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  122. {
  123. MC_DEBUG("Failed to parse, sscanf != 1\n");
  124. return 1;
  125. }
  126. buf_skip_space = buf;
  127. orig_has_neg = 0;
  128. // Skip leading spaces
  129. while (isspace((int)*buf_skip_space) && *buf_skip_space)
  130. buf_skip_space++;
  131. if (*buf_skip_space == '-')
  132. {
  133. buf_skip_space++;
  134. orig_has_neg = 1;
  135. }
  136. // Skip leading zeros, but keep at least one digit
  137. while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
  138. buf_skip_space++;
  139. if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
  140. orig_has_neg = 0; // "-0" is the same as just plain "0"
  141. if (errno != ERANGE)
  142. {
  143. char buf_cmp[100];
  144. char *buf_cmp_start = buf_cmp;
  145. int recheck_has_neg = 0;
  146. int buf_cmp_len;
  147. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  148. if (*buf_cmp_start == '-')
  149. {
  150. recheck_has_neg = 1;
  151. buf_cmp_start++;
  152. }
  153. // No need to skip leading spaces or zeros here.
  154. buf_cmp_len = strlen(buf_cmp_start);
  155. /**
  156. * If the sign is different, or
  157. * some of the digits are different, or
  158. * there is another digit present in the original string
  159. * then we NOT successfully parsed the value.
  160. */
  161. if (orig_has_neg != recheck_has_neg ||
  162. strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  163. (strlen(buf_skip_space) != buf_cmp_len &&
  164. isdigit((int)buf_skip_space[buf_cmp_len])
  165. )
  166. )
  167. {
  168. errno = ERANGE;
  169. }
  170. }
  171. if (errno == ERANGE)
  172. {
  173. if (orig_has_neg)
  174. num64 = INT64_MIN;
  175. else
  176. num64 = INT64_MAX;
  177. }
  178. *retval = num64;
  179. return 0;
  180. }
  181. #if HAVE_REALLOC == 0
  182. void* rpl_realloc(void* p, size_t n)
  183. {
  184. if (n == 0)
  185. n = 1;
  186. if (p == 0)
  187. return malloc(n);
  188. return realloc(p, n);
  189. }
  190. #endif
  191. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  192. static const char* json_type_name[] = {
  193. /* If you change this, be sure to update the enum json_type definition too */
  194. "null",
  195. "boolean",
  196. "double",
  197. "int",
  198. "object",
  199. "array",
  200. "string",
  201. };
  202. const char *json_type_to_name(enum json_type o_type)
  203. {
  204. if (o_type < 0 || o_type >= NELEM(json_type_name))
  205. {
  206. MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  207. return NULL;
  208. }
  209. return json_type_name[o_type];
  210. }