json_util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "strerror_override.h"
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif /* HAVE_SYS_TYPES_H */
  24. #ifdef HAVE_SYS_STAT_H
  25. #include <sys/stat.h>
  26. #endif /* HAVE_SYS_STAT_H */
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif /* HAVE_FCNTL_H */
  30. #ifdef HAVE_UNISTD_H
  31. # include <unistd.h>
  32. #endif /* HAVE_UNISTD_H */
  33. #ifdef WIN32
  34. # if MSC_VER < 1800
  35. /* strtoll is available only since Visual Studio 2013 */
  36. # define strtoll _strtoi64
  37. # endif
  38. # define WIN32_LEAN_AND_MEAN
  39. # include <windows.h>
  40. # include <io.h>
  41. #endif /* defined(WIN32) */
  42. #if !defined(HAVE_OPEN) && defined(WIN32)
  43. # define open _open
  44. #endif
  45. #include "snprintf_compat.h"
  46. #include "debug.h"
  47. #include "printbuf.h"
  48. #include "json_inttypes.h"
  49. #include "json_object.h"
  50. #include "json_tokener.h"
  51. #include "json_util.h"
  52. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
  53. static char _last_err[256] = "";
  54. const char *json_util_get_last_err()
  55. {
  56. if (_last_err[0] == '\0')
  57. return NULL;
  58. return _last_err;
  59. }
  60. void _json_c_set_last_err(const char *err_fmt, ...)
  61. {
  62. va_list ap;
  63. va_start(ap, err_fmt);
  64. // Ignore (attempted) overruns from snprintf
  65. (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
  66. va_end(ap);
  67. }
  68. struct json_object* json_object_from_fd(int fd)
  69. {
  70. struct printbuf *pb;
  71. struct json_object *obj;
  72. char buf[JSON_FILE_BUF_SIZE];
  73. int ret;
  74. if(!(pb = printbuf_new())) {
  75. _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
  76. return NULL;
  77. }
  78. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  79. printbuf_memappend(pb, buf, ret);
  80. }
  81. if(ret < 0) {
  82. _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
  83. printbuf_free(pb);
  84. return NULL;
  85. }
  86. obj = json_tokener_parse(pb->buf);
  87. printbuf_free(pb);
  88. return obj;
  89. }
  90. struct json_object* json_object_from_file(const char *filename)
  91. {
  92. struct json_object *obj;
  93. int fd;
  94. if((fd = open(filename, O_RDONLY)) < 0) {
  95. _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
  96. filename, strerror(errno));
  97. return NULL;
  98. }
  99. obj = json_object_from_fd(fd);
  100. close(fd);
  101. return obj;
  102. }
  103. /* extended "format and write to file" function */
  104. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  105. {
  106. int fd, ret;
  107. int saved_errno;
  108. if (!obj) {
  109. _json_c_set_last_err("json_object_to_file: object is null\n");
  110. return -1;
  111. }
  112. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  113. _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n",
  114. filename, strerror(errno));
  115. return -1;
  116. }
  117. ret = _json_object_to_fd(fd, obj, flags, filename);
  118. saved_errno = errno;
  119. close(fd);
  120. errno = saved_errno;
  121. return ret;
  122. }
  123. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  124. {
  125. if (!obj) {
  126. _json_c_set_last_err("json_object_to_fd: object is null\n");
  127. return -1;
  128. }
  129. return _json_object_to_fd(fd, obj, flags, NULL);
  130. }
  131. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  132. {
  133. int ret;
  134. const char *json_str;
  135. unsigned int wpos, wsize;
  136. filename = filename ? filename : "(fd)";
  137. if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
  138. return -1;
  139. }
  140. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  141. wpos = 0;
  142. while(wpos < wsize) {
  143. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  144. _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
  145. filename, strerror(errno));
  146. return -1;
  147. }
  148. /* because of the above check for ret < 0, we can safely cast and add */
  149. wpos += (unsigned int)ret;
  150. }
  151. return 0;
  152. }
  153. // backwards compatible "format and write to file" function
  154. int json_object_to_file(const char *filename, struct json_object *obj)
  155. {
  156. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  157. }
  158. int json_parse_double(const char *buf, double *retval)
  159. {
  160. char *end;
  161. *retval = strtod(buf, &end);
  162. return end == buf ? 1 : 0;
  163. }
  164. int json_parse_int64(const char *buf, int64_t *retval)
  165. {
  166. char *end = NULL;
  167. int64_t val;
  168. errno = 0;
  169. val = strtoll(buf, &end, 10);
  170. if (end != buf)
  171. *retval = val;
  172. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  173. }
  174. #ifndef HAVE_REALLOC
  175. void* rpl_realloc(void* p, size_t n)
  176. {
  177. if (n == 0)
  178. n = 1;
  179. if (p == 0)
  180. return malloc(n);
  181. return realloc(p, n);
  182. }
  183. #endif
  184. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  185. static const char* json_type_name[] = {
  186. /* If you change this, be sure to update the enum json_type definition too */
  187. "null",
  188. "boolean",
  189. "double",
  190. "int",
  191. "object",
  192. "array",
  193. "string",
  194. };
  195. const char *json_type_to_name(enum json_type o_type)
  196. {
  197. int o_type_int = (int)o_type;
  198. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  199. {
  200. _json_c_set_last_err("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. }