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

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