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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. MC_ERROR("json_object_from_file: printbuf_new failed\n");
  59. return (struct json_object*)error_ptr(-1);
  60. }
  61. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  62. printbuf_memappend(pb, buf, ret);
  63. }
  64. close(fd);
  65. if(ret < 0) {
  66. MC_ABORT("json_object_from_file: error reading file %s: %s\n",
  67. filename, strerror(errno));
  68. printbuf_free(pb);
  69. return (struct json_object*)error_ptr(-1);
  70. }
  71. obj = json_tokener_parse(pb->buf);
  72. printbuf_free(pb);
  73. return obj;
  74. }
  75. int json_object_to_file(char *filename, struct json_object *obj)
  76. {
  77. const char *json_str;
  78. int fd, ret;
  79. unsigned int wpos, wsize;
  80. if(!obj) {
  81. MC_ERROR("json_object_to_file: object is null\n");
  82. return -1;
  83. }
  84. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  85. MC_ERROR("json_object_to_file: error opening file %s: %s\n",
  86. filename, strerror(errno));
  87. return -1;
  88. }
  89. if(!(json_str = json_object_to_json_string(obj))) { return -1; }
  90. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  91. wpos = 0;
  92. while(wpos < wsize) {
  93. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  94. close(fd);
  95. MC_ERROR("json_object_to_file: error writing file %s: %s\n",
  96. filename, strerror(errno));
  97. return -1;
  98. }
  99. /* because of the above check for ret < 0, we can safely cast and add */
  100. wpos += (unsigned int)ret;
  101. }
  102. close(fd);
  103. return 0;
  104. }
  105. int json_parse_int64(const char *buf, int64_t *retval)
  106. {
  107. int64_t num64;
  108. if (sscanf(buf, "%" SCNd64, &num64) != 1)
  109. {
  110. printf("Failed to parse, sscanf != 1\n");
  111. return 1;
  112. }
  113. const char *buf_skip_space = buf;
  114. int orig_has_neg = 0;
  115. // Skip leading spaces
  116. while (isspace((int)*buf_skip_space) && *buf_skip_space)
  117. buf_skip_space++;
  118. if (*buf_skip_space == '-')
  119. {
  120. buf_skip_space++;
  121. orig_has_neg = 1;
  122. }
  123. // Skip leading zeros
  124. while (*buf_skip_space == '0' && *buf_skip_space)
  125. buf_skip_space++;
  126. if (errno != ERANGE)
  127. {
  128. char buf_cmp[100];
  129. char *buf_cmp_start = buf_cmp;
  130. int recheck_has_neg = 0;
  131. snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
  132. if (*buf_cmp_start == '-')
  133. {
  134. recheck_has_neg = 1;
  135. buf_cmp_start++;
  136. }
  137. // No need to skip leading spaces or zeros here.
  138. int buf_cmp_len = strlen(buf_cmp_start);
  139. /**
  140. * If the sign is different, or
  141. * some of the digits are different, or
  142. * there is another digit present in the original string
  143. * then we NOT successfully parsed the value.
  144. */
  145. if (orig_has_neg != recheck_has_neg ||
  146. strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
  147. (strlen(buf_skip_space) != buf_cmp_len &&
  148. isdigit(buf_skip_space[buf_cmp_len])
  149. )
  150. )
  151. {
  152. errno = ERANGE;
  153. }
  154. }
  155. if (errno == ERANGE)
  156. {
  157. if (orig_has_neg)
  158. num64 = INT64_MIN;
  159. else
  160. num64 = INT64_MAX;
  161. }
  162. *retval = num64;
  163. return 0;
  164. }