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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <limits.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <ctype.h>
  19. #if HAVE_SYS_TYPES_H
  20. #include <sys/types.h>
  21. #endif /* HAVE_SYS_TYPES_H */
  22. #if HAVE_SYS_STAT_H
  23. #include <sys/stat.h>
  24. #endif /* HAVE_SYS_STAT_H */
  25. #if HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif /* HAVE_FCNTL_H */
  28. #if HAVE_UNISTD_H
  29. # include <unistd.h>
  30. #endif /* HAVE_UNISTD_H */
  31. #ifdef WIN32
  32. # define WIN32_LEAN_AND_MEAN
  33. # include <windows.h>
  34. # include <io.h>
  35. #endif /* defined(WIN32) */
  36. #if !HAVE_OPEN && defined(WIN32)
  37. # define open _open
  38. #endif
  39. #include "bits.h"
  40. #include "debug.h"
  41. #include "printbuf.h"
  42. #include "json_inttypes.h"
  43. #include "json_object.h"
  44. #include "json_tokener.h"
  45. #include "json_util.h"
  46. struct json_object* json_object_from_file(const char *filename)
  47. {
  48. struct printbuf *pb;
  49. struct json_object *obj;
  50. char buf[JSON_FILE_BUF_SIZE];
  51. int fd, ret;
  52. if((fd = open(filename, O_RDONLY)) < 0) {
  53. MC_ERROR("json_object_from_file: error reading file %s: %s\n",
  54. filename, strerror(errno));
  55. return (struct json_object*)error_ptr(-1);
  56. }
  57. if(!(pb = printbuf_new())) {
  58. close(fd);
  59. MC_ERROR("json_object_from_file: printbuf_new failed\n");
  60. return (struct json_object*)error_ptr(-1);
  61. }
  62. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  63. printbuf_memappend(pb, buf, ret);
  64. }
  65. close(fd);
  66. if(ret < 0) {
  67. MC_ABORT("json_object_from_file: error reading file %s: %s\n",
  68. filename, strerror(errno));
  69. printbuf_free(pb);
  70. return (struct json_object*)error_ptr(-1);
  71. }
  72. obj = json_tokener_parse(pb->buf);
  73. printbuf_free(pb);
  74. return obj;
  75. }
  76. int json_object_to_file(char *filename, struct json_object *obj)
  77. {
  78. const char *json_str;
  79. int fd, ret;
  80. unsigned int wpos, wsize;
  81. if(!obj) {
  82. MC_ERROR("json_object_to_file: object is null\n");
  83. return -1;
  84. }
  85. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  86. MC_ERROR("json_object_to_file: error opening file %s: %s\n",
  87. filename, strerror(errno));
  88. return -1;
  89. }
  90. if(!(json_str = json_object_to_json_string(obj))) {
  91. close(fd);
  92. return -1;
  93. }
  94. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  95. wpos = 0;
  96. while(wpos < wsize) {
  97. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  98. close(fd);
  99. MC_ERROR("json_object_to_file: error writing file %s: %s\n",
  100. filename, strerror(errno));
  101. return -1;
  102. }
  103. /* because of the above check for ret < 0, we can safely cast and add */
  104. wpos += (unsigned int)ret;
  105. }
  106. close(fd);
  107. return 0;
  108. }
  109. int json_parse_int64(const char *buf, int64_t *retval)
  110. {
  111. int64_t num64;
  112. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  113. {
  114. printf("Failed to parse, sscanf != 1\n");
  115. return 1;
  116. }
  117. const char *buf_skip_space = buf;
  118. int orig_has_neg = 0;
  119. // Skip leading spaces
  120. while (isspace((int)*buf_skip_space) && *buf_skip_space)
  121. buf_skip_space++;
  122. if (*buf_skip_space == '-')
  123. {
  124. buf_skip_space++;
  125. orig_has_neg = 1;
  126. }
  127. // Skip leading zeros
  128. while (*buf_skip_space == '0' && *buf_skip_space)
  129. buf_skip_space++;
  130. if (errno != ERANGE)
  131. {
  132. char buf_cmp[100];
  133. char *buf_cmp_start = buf_cmp;
  134. int recheck_has_neg = 0;
  135. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  136. if (*buf_cmp_start == '-')
  137. {
  138. recheck_has_neg = 1;
  139. buf_cmp_start++;
  140. }
  141. // No need to skip leading spaces or zeros here.
  142. int buf_cmp_len = strlen(buf_cmp_start);
  143. /**
  144. * If the sign is different, or
  145. * some of the digits are different, or
  146. * there is another digit present in the original string
  147. * then we NOT successfully parsed the value.
  148. */
  149. if (orig_has_neg != recheck_has_neg ||
  150. strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  151. (strlen(buf_skip_space) != buf_cmp_len &&
  152. isdigit(buf_skip_space[buf_cmp_len])
  153. )
  154. )
  155. {
  156. errno = ERANGE;
  157. }
  158. }
  159. if (errno == ERANGE)
  160. {
  161. if (orig_has_neg)
  162. num64 = INT64_MIN;
  163. else
  164. num64 = INT64_MAX;
  165. }
  166. *retval = num64;
  167. return 0;
  168. }