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

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