blob: 531f9afb3a03fb1499ba284a25cac75dcb7df2d7 [file] [log] [blame]
Michael Clark4504df72007-03-13 08:26:20 +00001/*
Michael Clark837240f2007-03-13 08:26:25 +00002 * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
Michael Clark4504df72007-03-13 08:26:20 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clark4504df72007-03-13 08:26:20 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clark4504df72007-03-13 08:26:20 +00009 *
10 */
11
12#include "config.h"
ehaszla252669c2010-12-07 18:15:35 +000013#undef realloc
Michael Clark4504df72007-03-13 08:26:20 +000014
Michael Clarkf0d08882007-03-13 08:26:18 +000015#include <stdio.h>
16#include <stdlib.h>
Michael Clarkc8f4a6e2007-12-07 02:44:24 +000017#include <stddef.h>
Michael Clark4504df72007-03-13 08:26:20 +000018#include <limits.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000019#include <string.h>
20#include <errno.h>
Michael Clarkc4dceae2010-10-06 16:39:20 +000021#include <ctype.h>
Michael Clark4504df72007-03-13 08:26:20 +000022
Mateusz Loskota6f39a32012-05-21 23:22:36 +010023#ifdef HAVE_SYS_TYPES_H
Michael Clarkf0d08882007-03-13 08:26:18 +000024#include <sys/types.h>
Michael Clark4504df72007-03-13 08:26:20 +000025#endif /* HAVE_SYS_TYPES_H */
26
Mateusz Loskota6f39a32012-05-21 23:22:36 +010027#ifdef HAVE_SYS_STAT_H
Michael Clarkf0d08882007-03-13 08:26:18 +000028#include <sys/stat.h>
Michael Clark4504df72007-03-13 08:26:20 +000029#endif /* HAVE_SYS_STAT_H */
30
Mateusz Loskota6f39a32012-05-21 23:22:36 +010031#ifdef HAVE_FCNTL_H
Michael Clarkf0d08882007-03-13 08:26:18 +000032#include <fcntl.h>
Michael Clark4504df72007-03-13 08:26:20 +000033#endif /* HAVE_FCNTL_H */
34
Mateusz Loskota6f39a32012-05-21 23:22:36 +010035#ifdef HAVE_UNISTD_H
Michael Clark4504df72007-03-13 08:26:20 +000036# include <unistd.h>
37#endif /* HAVE_UNISTD_H */
38
39#ifdef WIN32
40# define WIN32_LEAN_AND_MEAN
41# include <windows.h>
42# include <io.h>
43#endif /* defined(WIN32) */
Michael Clarkf0d08882007-03-13 08:26:18 +000044
Mateusz Loskota6f39a32012-05-21 23:22:36 +010045#if !defined(HAVE_OPEN) && defined(WIN32)
Michael Clark837240f2007-03-13 08:26:25 +000046# define open _open
47#endif
48
Mateusz Loskota6f39a32012-05-21 23:22:36 +010049#if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
50 /* MSC has the version as _snprintf */
51# define snprintf _snprintf
52#elif !defined(HAVE_SNPRINTF)
53# error You do not have snprintf on your system.
54#endif /* HAVE_SNPRINTF */
Michael Clark837240f2007-03-13 08:26:25 +000055
Michael Clarkf0d08882007-03-13 08:26:18 +000056#include "bits.h"
57#include "debug.h"
58#include "printbuf.h"
Michael Clarkc4dceae2010-10-06 16:39:20 +000059#include "json_inttypes.h"
Michael Clarkf0d08882007-03-13 08:26:18 +000060#include "json_object.h"
61#include "json_tokener.h"
62#include "json_util.h"
63
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -060064static int sscanf_is_broken = 0;
65static int sscanf_is_broken_testdone = 0;
66static void sscanf_is_broken_test(void);
67
Michael Clark88ded9c2009-08-27 06:40:59 +000068struct json_object* json_object_from_file(const char *filename)
Michael Clarkf0d08882007-03-13 08:26:18 +000069{
70 struct printbuf *pb;
71 struct json_object *obj;
72 char buf[JSON_FILE_BUF_SIZE];
73 int fd, ret;
74
75 if((fd = open(filename, O_RDONLY)) < 0) {
Eric Haszlakiewiczb3bce4d2013-06-29 15:31:18 -050076 MC_ERROR("json_object_from_file: error opening file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000077 filename, strerror(errno));
Eric Haszlakiewiczeead1a72012-07-08 20:32:12 -050078 return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000079 }
80 if(!(pb = printbuf_new())) {
Michael Clarkbd0a5672010-10-13 14:09:41 +000081 close(fd);
Michael Clarkdfaf6702007-10-25 02:26:00 +000082 MC_ERROR("json_object_from_file: printbuf_new failed\n");
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050083 return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000084 }
85 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
86 printbuf_memappend(pb, buf, ret);
87 }
88 close(fd);
89 if(ret < 0) {
Eric Haszlakiewiczb3bce4d2013-06-29 15:31:18 -050090 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000091 filename, strerror(errno));
92 printbuf_free(pb);
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050093 return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000094 }
95 obj = json_tokener_parse(pb->buf);
96 printbuf_free(pb);
97 return obj;
98}
99
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500100/* extended "format and write to file" function */
101
Pascal Bach20e47082013-08-13 18:24:23 +0200102int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
Michael Clarkf0d08882007-03-13 08:26:18 +0000103{
Michael Clark68cafad2009-01-06 22:56:57 +0000104 const char *json_str;
Michael Clark4504df72007-03-13 08:26:20 +0000105 int fd, ret;
106 unsigned int wpos, wsize;
Michael Clarkf0d08882007-03-13 08:26:18 +0000107
108 if(!obj) {
Michael Clarkdfaf6702007-10-25 02:26:00 +0000109 MC_ERROR("json_object_to_file: object is null\n");
Michael Clarkf0d08882007-03-13 08:26:18 +0000110 return -1;
111 }
112
113 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +0000114 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +0000115 filename, strerror(errno));
116 return -1;
117 }
Michael Clark4504df72007-03-13 08:26:20 +0000118
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500119 if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
Michael Clarkf1ae67d2010-10-13 14:10:51 +0000120 close(fd);
121 return -1;
122 }
Michael Clark4504df72007-03-13 08:26:20 +0000123
124 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
Michael Clarkf0d08882007-03-13 08:26:18 +0000125 wpos = 0;
126 while(wpos < wsize) {
127 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
128 close(fd);
Michael Clarkdfaf6702007-10-25 02:26:00 +0000129 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +0000130 filename, strerror(errno));
131 return -1;
132 }
Michael Clark4504df72007-03-13 08:26:20 +0000133
134 /* because of the above check for ret < 0, we can safely cast and add */
135 wpos += (unsigned int)ret;
Michael Clarkf0d08882007-03-13 08:26:18 +0000136 }
137
138 close(fd);
139 return 0;
140}
Michael Clarkc4dceae2010-10-06 16:39:20 +0000141
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500142// backwards compatible "format and write to file" function
143
Pascal Bach20e47082013-08-13 18:24:23 +0200144int json_object_to_file(const char *filename, struct json_object *obj)
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500145{
146 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
147}
148
Remi Collet16a4a322012-11-27 11:06:49 +0100149int json_parse_double(const char *buf, double *retval)
150{
Remi Colleta01b6592012-12-13 09:47:33 +0100151 return (sscanf(buf, "%lf", retval)==1 ? 0 : 1);
Remi Collet16a4a322012-11-27 11:06:49 +0100152}
153
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600154/*
155 * Not all implementations of sscanf actually work properly.
156 * Check whether the one we're currently using does, and if
157 * it's broken, enable the workaround code.
158 */
159static void sscanf_is_broken_test()
160{
161 int64_t num64;
Anatol Belski990fa8e2013-06-04 20:18:28 +0200162 int ret_errno, is_int64_min, ret_errno2, is_int64_max;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600163
164 (void)sscanf(" -01234567890123456789012345", "%" SCNd64, &num64);
Anatol Belski990fa8e2013-06-04 20:18:28 +0200165 ret_errno = errno;
166 is_int64_min = (num64 == INT64_MIN);
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600167
168 (void)sscanf(" 01234567890123456789012345", "%" SCNd64, &num64);
Anatol Belski990fa8e2013-06-04 20:18:28 +0200169 ret_errno2 = errno;
170 is_int64_max = (num64 == INT64_MAX);
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600171
172 if (ret_errno != ERANGE || !is_int64_min ||
173 ret_errno2 != ERANGE || !is_int64_max)
174 {
175 MC_DEBUG("sscanf_is_broken_test failed, enabling workaround code\n");
176 sscanf_is_broken = 1;
177 }
178}
179
Michael Clarkc4dceae2010-10-06 16:39:20 +0000180int json_parse_int64(const char *buf, int64_t *retval)
181{
182 int64_t num64;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600183 const char *buf_sig_digits;
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100184 int orig_has_neg;
Greg Hazel77d04932013-01-03 16:54:04 -0800185 int saved_errno;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600186
187 if (!sscanf_is_broken_testdone)
188 {
189 sscanf_is_broken_test();
190 sscanf_is_broken_testdone = 1;
191 }
192
193 // Skip leading spaces
194 while (isspace((int)*buf) && *buf)
195 buf++;
196
Eric Haszlakiewicz77c62392012-07-29 12:13:54 -0500197 errno = 0; // sscanf won't always set errno, so initialize
Michael Clarkc4dceae2010-10-06 16:39:20 +0000198 if (sscanf(buf, "%" SCNd64, &num64) != 1)
199 {
ehaszla252669c2010-12-07 18:15:35 +0000200 MC_DEBUG("Failed to parse, sscanf != 1\n");
Michael Clarkc4dceae2010-10-06 16:39:20 +0000201 return 1;
202 }
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600203
Greg Hazel77d04932013-01-03 16:54:04 -0800204 saved_errno = errno;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600205 buf_sig_digits = buf;
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100206 orig_has_neg = 0;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600207 if (*buf_sig_digits == '-')
Michael Clarkc4dceae2010-10-06 16:39:20 +0000208 {
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600209 buf_sig_digits++;
Michael Clarkc4dceae2010-10-06 16:39:20 +0000210 orig_has_neg = 1;
211 }
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600212
213 // Not all sscanf implementations actually work
214 if (sscanf_is_broken && saved_errno != ERANGE)
Michael Clarkc4dceae2010-10-06 16:39:20 +0000215 {
216 char buf_cmp[100];
217 char *buf_cmp_start = buf_cmp;
218 int recheck_has_neg = 0;
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100219 int buf_cmp_len;
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600220
221 // Skip leading zeros, but keep at least one digit
222 while (buf_sig_digits[0] == '0' && buf_sig_digits[1] != '\0')
223 buf_sig_digits++;
224 if (num64 == 0) // assume all sscanf impl's will parse -0 to 0
225 orig_has_neg = 0; // "-0" is the same as just plain "0"
226
Michael Clarkc4dceae2010-10-06 16:39:20 +0000227 snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
228 if (*buf_cmp_start == '-')
229 {
230 recheck_has_neg = 1;
231 buf_cmp_start++;
232 }
233 // No need to skip leading spaces or zeros here.
234
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100235 buf_cmp_len = strlen(buf_cmp_start);
Michael Clarkc4dceae2010-10-06 16:39:20 +0000236 /**
237 * If the sign is different, or
238 * some of the digits are different, or
239 * there is another digit present in the original string
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600240 * then we have NOT successfully parsed the value.
Michael Clarkc4dceae2010-10-06 16:39:20 +0000241 */
242 if (orig_has_neg != recheck_has_neg ||
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600243 strncmp(buf_sig_digits, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
244 ((int)strlen(buf_sig_digits) != buf_cmp_len &&
245 isdigit((int)buf_sig_digits[buf_cmp_len])
Michael Clarkc4dceae2010-10-06 16:39:20 +0000246 )
247 )
248 {
Greg Hazel77d04932013-01-03 16:54:04 -0800249 saved_errno = ERANGE;
Michael Clarkc4dceae2010-10-06 16:39:20 +0000250 }
251 }
Eric Haszlakiewiczbfb32922013-02-09 17:35:33 -0600252
253 // Not all sscanf impl's set the value properly when out of range.
254 // Always do this, even for properly functioning implementations,
255 // since it shouldn't slow things down much.
Greg Hazel77d04932013-01-03 16:54:04 -0800256 if (saved_errno == ERANGE)
Michael Clarkc4dceae2010-10-06 16:39:20 +0000257 {
258 if (orig_has_neg)
259 num64 = INT64_MIN;
260 else
261 num64 = INT64_MAX;
262 }
263 *retval = num64;
264 return 0;
265}
ehaszla252669c2010-12-07 18:15:35 +0000266
Mateusz Loskota6f39a32012-05-21 23:22:36 +0100267#ifndef HAVE_REALLOC
ehaszla252669c2010-12-07 18:15:35 +0000268void* rpl_realloc(void* p, size_t n)
269{
270 if (n == 0)
271 n = 1;
272 if (p == 0)
273 return malloc(n);
274 return realloc(p, n);
275}
276#endif
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +0000277
278#define NELEM(a) (sizeof(a) / sizeof(a[0]))
279static const char* json_type_name[] = {
280 /* If you change this, be sure to update the enum json_type definition too */
281 "null",
282 "boolean",
283 "double",
284 "int",
285 "object",
286 "array",
287 "string",
288};
289
290const char *json_type_to_name(enum json_type o_type)
291{
Eric Haszlakiewiczca8b27d2013-02-09 16:35:24 -0600292 int o_type_int = (int)o_type;
293 if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +0000294 {
295 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
296 return NULL;
297 }
298 return json_type_name[o_type];
299}
300