blob: e551d2dbb0605a20486ea8625c2b36ddaaa16e96 [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
23#if 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
27#if 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
31#if 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
35#if HAVE_UNISTD_H
36# 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
Michael Clark837240f2007-03-13 08:26:25 +000045#if !HAVE_OPEN && defined(WIN32)
46# define open _open
47#endif
48
49
Michael Clarkf0d08882007-03-13 08:26:18 +000050#include "bits.h"
51#include "debug.h"
52#include "printbuf.h"
Michael Clarkc4dceae2010-10-06 16:39:20 +000053#include "json_inttypes.h"
Michael Clarkf0d08882007-03-13 08:26:18 +000054#include "json_object.h"
55#include "json_tokener.h"
56#include "json_util.h"
57
Michael Clark88ded9c2009-08-27 06:40:59 +000058struct json_object* json_object_from_file(const char *filename)
Michael Clarkf0d08882007-03-13 08:26:18 +000059{
60 struct printbuf *pb;
61 struct json_object *obj;
62 char buf[JSON_FILE_BUF_SIZE];
63 int fd, ret;
64
65 if((fd = open(filename, O_RDONLY)) < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000066 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000067 filename, strerror(errno));
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050068 return NULL; // XAX this is an API change!
Michael Clarkf0d08882007-03-13 08:26:18 +000069 }
70 if(!(pb = printbuf_new())) {
Michael Clarkbd0a5672010-10-13 14:09:41 +000071 close(fd);
Michael Clarkdfaf6702007-10-25 02:26:00 +000072 MC_ERROR("json_object_from_file: printbuf_new failed\n");
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050073 return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000074 }
75 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
76 printbuf_memappend(pb, buf, ret);
77 }
78 close(fd);
79 if(ret < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000080 MC_ABORT("json_object_from_file: error reading file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000081 filename, strerror(errno));
82 printbuf_free(pb);
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050083 return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000084 }
85 obj = json_tokener_parse(pb->buf);
86 printbuf_free(pb);
87 return obj;
88}
89
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050090/* extended "format and write to file" function */
91
92int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
Michael Clarkf0d08882007-03-13 08:26:18 +000093{
Michael Clark68cafad2009-01-06 22:56:57 +000094 const char *json_str;
Michael Clark4504df72007-03-13 08:26:20 +000095 int fd, ret;
96 unsigned int wpos, wsize;
Michael Clarkf0d08882007-03-13 08:26:18 +000097
98 if(!obj) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000099 MC_ERROR("json_object_to_file: object is null\n");
Michael Clarkf0d08882007-03-13 08:26:18 +0000100 return -1;
101 }
102
103 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +0000104 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +0000105 filename, strerror(errno));
106 return -1;
107 }
Michael Clark4504df72007-03-13 08:26:20 +0000108
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500109 if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
Michael Clarkf1ae67d2010-10-13 14:10:51 +0000110 close(fd);
111 return -1;
112 }
Michael Clark4504df72007-03-13 08:26:20 +0000113
114 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
Michael Clarkf0d08882007-03-13 08:26:18 +0000115 wpos = 0;
116 while(wpos < wsize) {
117 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
118 close(fd);
Michael Clarkdfaf6702007-10-25 02:26:00 +0000119 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +0000120 filename, strerror(errno));
121 return -1;
122 }
Michael Clark4504df72007-03-13 08:26:20 +0000123
124 /* because of the above check for ret < 0, we can safely cast and add */
125 wpos += (unsigned int)ret;
Michael Clarkf0d08882007-03-13 08:26:18 +0000126 }
127
128 close(fd);
129 return 0;
130}
Michael Clarkc4dceae2010-10-06 16:39:20 +0000131
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500132// backwards compatible "format and write to file" function
133
134int json_object_to_file(char *filename, struct json_object *obj)
135{
136 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
137}
138
Michael Clarkc4dceae2010-10-06 16:39:20 +0000139int json_parse_int64(const char *buf, int64_t *retval)
140{
141 int64_t num64;
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100142 const char *buf_skip_space;
143 int orig_has_neg;
Michael Clarkc4dceae2010-10-06 16:39:20 +0000144 if (sscanf(buf, "%" SCNd64, &num64) != 1)
145 {
ehaszla252669c2010-12-07 18:15:35 +0000146 MC_DEBUG("Failed to parse, sscanf != 1\n");
Michael Clarkc4dceae2010-10-06 16:39:20 +0000147 return 1;
148 }
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100149 buf_skip_space = buf;
150 orig_has_neg = 0;
Michael Clarkc4dceae2010-10-06 16:39:20 +0000151 // Skip leading spaces
152 while (isspace((int)*buf_skip_space) && *buf_skip_space)
153 buf_skip_space++;
154 if (*buf_skip_space == '-')
155 {
156 buf_skip_space++;
157 orig_has_neg = 1;
158 }
ehaszla252669c2010-12-07 18:15:35 +0000159 // Skip leading zeros, but keep at least one digit
160 while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
Michael Clarkc4dceae2010-10-06 16:39:20 +0000161 buf_skip_space++;
ehaszla252669c2010-12-07 18:15:35 +0000162 if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
163 orig_has_neg = 0; // "-0" is the same as just plain "0"
Michael Clarkc4dceae2010-10-06 16:39:20 +0000164
165 if (errno != ERANGE)
166 {
167 char buf_cmp[100];
168 char *buf_cmp_start = buf_cmp;
169 int recheck_has_neg = 0;
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100170 int buf_cmp_len;
Michael Clarkc4dceae2010-10-06 16:39:20 +0000171 snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
172 if (*buf_cmp_start == '-')
173 {
174 recheck_has_neg = 1;
175 buf_cmp_start++;
176 }
177 // No need to skip leading spaces or zeros here.
178
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100179 buf_cmp_len = strlen(buf_cmp_start);
Michael Clarkc4dceae2010-10-06 16:39:20 +0000180 /**
181 * If the sign is different, or
182 * some of the digits are different, or
183 * there is another digit present in the original string
184 * then we NOT successfully parsed the value.
185 */
186 if (orig_has_neg != recheck_has_neg ||
187 strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
188 (strlen(buf_skip_space) != buf_cmp_len &&
ehaszla252669c2010-12-07 18:15:35 +0000189 isdigit((int)buf_skip_space[buf_cmp_len])
Michael Clarkc4dceae2010-10-06 16:39:20 +0000190 )
191 )
192 {
193 errno = ERANGE;
194 }
195 }
196 if (errno == ERANGE)
197 {
198 if (orig_has_neg)
199 num64 = INT64_MIN;
200 else
201 num64 = INT64_MAX;
202 }
203 *retval = num64;
204 return 0;
205}
ehaszla252669c2010-12-07 18:15:35 +0000206
207#if HAVE_REALLOC == 0
208void* rpl_realloc(void* p, size_t n)
209{
210 if (n == 0)
211 n = 1;
212 if (p == 0)
213 return malloc(n);
214 return realloc(p, n);
215}
216#endif
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +0000217
218#define NELEM(a) (sizeof(a) / sizeof(a[0]))
219static const char* json_type_name[] = {
220 /* If you change this, be sure to update the enum json_type definition too */
221 "null",
222 "boolean",
223 "double",
224 "int",
225 "object",
226 "array",
227 "string",
228};
229
230const char *json_type_to_name(enum json_type o_type)
231{
232 if (o_type < 0 || o_type >= NELEM(json_type_name))
233 {
234 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
235 return NULL;
236 }
237 return json_type_name[o_type];
238}
239