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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  114. {
  115. MC_DEBUG("Failed to parse, sscanf != 1\n");
  116. return 1;
  117. }
  118. const char *buf_skip_space = buf;
  119. int orig_has_neg = 0;
  120. // Skip leading spaces
  121. while (isspace((int)*buf_skip_space) && *buf_skip_space)
  122. buf_skip_space++;
  123. if (*buf_skip_space == '-')
  124. {
  125. buf_skip_space++;
  126. orig_has_neg = 1;
  127. }
  128. // Skip leading zeros, but keep at least one digit
  129. while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
  130. buf_skip_space++;
  131. if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
  132. orig_has_neg = 0; // "-0" is the same as just plain "0"
  133. if (errno != ERANGE)
  134. {
  135. char buf_cmp[100];
  136. char *buf_cmp_start = buf_cmp;
  137. int recheck_has_neg = 0;
  138. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  139. if (*buf_cmp_start == '-')
  140. {
  141. recheck_has_neg = 1;
  142. buf_cmp_start++;
  143. }
  144. // No need to skip leading spaces or zeros here.
  145. int buf_cmp_len = strlen(buf_cmp_start);
  146. /**
  147. * If the sign is different, or
  148. * some of the digits are different, or
  149. * there is another digit present in the original string
  150. * then we NOT successfully parsed the value.
  151. */
  152. if (orig_has_neg != recheck_has_neg ||
  153. strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  154. (strlen(buf_skip_space) != buf_cmp_len &&
  155. isdigit((int)buf_skip_space[buf_cmp_len])
  156. )
  157. )
  158. {
  159. errno = ERANGE;
  160. }
  161. }
  162. if (errno == ERANGE)
  163. {
  164. if (orig_has_neg)
  165. num64 = INT64_MIN;
  166. else
  167. num64 = INT64_MAX;
  168. }
  169. *retval = num64;
  170. return 0;
  171. }
  172. #if HAVE_REALLOC == 0
  173. void* rpl_realloc(void* p, size_t n)
  174. {
  175. if (n == 0)
  176. n = 1;
  177. if (p == 0)
  178. return malloc(n);
  179. return realloc(p, n);
  180. }
  181. #endif