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