2007-03-13 08:26:20 +00:00
|
|
|
/*
|
2007-03-13 08:26:25 +00:00
|
|
|
* $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
|
2007-03-13 08:26:20 +00:00
|
|
|
*
|
2007-03-13 08:26:23 +00:00
|
|
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
2007-03-13 08:26:20 +00:00
|
|
|
* Michael Clark <michael@metaparadigm.com>
|
|
|
|
|
*
|
2007-03-13 08:26:23 +00:00
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the MIT license. See COPYING for details.
|
2007-03-13 08:26:20 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
2010-12-07 18:15:35 +00:00
|
|
|
#undef realloc
|
2007-03-13 08:26:20 +00:00
|
|
|
|
2017-07-13 10:11:15 +03:00
|
|
|
#include "strerror_override.h"
|
|
|
|
|
|
2016-06-26 02:20:05 +00:00
|
|
|
#include <stdarg.h>
|
2007-03-13 08:26:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2007-12-07 02:44:24 +00:00
|
|
|
#include <stddef.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#include <limits.h>
|
2007-03-13 08:26:18 +00:00
|
|
|
#include <string.h>
|
2010-10-06 16:39:20 +00:00
|
|
|
#include <ctype.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2007-03-13 08:26:18 +00:00
|
|
|
#include <sys/types.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#endif /* HAVE_SYS_TYPES_H */
|
|
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2007-03-13 08:26:18 +00:00
|
|
|
#include <sys/stat.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#endif /* HAVE_SYS_STAT_H */
|
|
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2007-03-13 08:26:18 +00:00
|
|
|
#include <fcntl.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#endif /* HAVE_FCNTL_H */
|
|
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2007-03-13 08:26:20 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2017-11-26 14:17:17 -05:00
|
|
|
# if MSC_VER < 1800
|
|
|
|
|
/* strtoll is available only since Visual Studio 2013 */
|
|
|
|
|
# define strtoll _strtoi64
|
|
|
|
|
# endif
|
2007-03-13 08:26:20 +00:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
# include <io.h>
|
|
|
|
|
#endif /* defined(WIN32) */
|
2007-03-13 08:26:18 +00:00
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#if !defined(HAVE_OPEN) && defined(WIN32)
|
2007-03-13 08:26:25 +00:00
|
|
|
# define open _open
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-25 01:15:39 -04:00
|
|
|
#include "snprintf_compat.h"
|
2007-03-13 08:26:25 +00:00
|
|
|
|
2007-03-13 08:26:18 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
#include "printbuf.h"
|
2010-10-06 16:39:20 +00:00
|
|
|
#include "json_inttypes.h"
|
2007-03-13 08:26:18 +00:00
|
|
|
#include "json_object.h"
|
|
|
|
|
#include "json_tokener.h"
|
|
|
|
|
#include "json_util.h"
|
|
|
|
|
|
2017-06-18 18:44:45 +00:00
|
|
|
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
|
|
|
|
|
|
2016-06-26 02:20:05 +00:00
|
|
|
static char _last_err[256] = "";
|
|
|
|
|
|
|
|
|
|
const char *json_util_get_last_err()
|
|
|
|
|
{
|
|
|
|
|
if (_last_err[0] == '\0')
|
|
|
|
|
return NULL;
|
|
|
|
|
return _last_err;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 09:25:11 -05:00
|
|
|
void _json_c_set_last_err(const char *err_fmt, ...)
|
2016-06-26 02:20:05 +00:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, err_fmt);
|
|
|
|
|
// Ignore (attempted) overruns from snprintf
|
|
|
|
|
(void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
2013-02-09 17:35:33 -06:00
|
|
|
|
2014-07-21 23:15:45 +02:00
|
|
|
struct json_object* json_object_from_fd(int fd)
|
2019-11-10 00:12:27 -05:00
|
|
|
{
|
|
|
|
|
return json_object_from_fd_ex(fd, -1);
|
|
|
|
|
}
|
|
|
|
|
struct json_object* json_object_from_fd_ex(int fd, int in_depth)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
|
|
|
|
struct printbuf *pb;
|
|
|
|
|
struct json_object *obj;
|
|
|
|
|
char buf[JSON_FILE_BUF_SIZE];
|
2014-07-21 23:15:45 +02:00
|
|
|
int ret;
|
2007-03-13 08:26:18 +00:00
|
|
|
|
|
|
|
|
if(!(pb = printbuf_new())) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
|
2012-04-28 13:26:09 -05:00
|
|
|
return NULL;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
2019-11-10 00:12:27 -05:00
|
|
|
|
|
|
|
|
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
|
|
|
|
if (in_depth != -1)
|
|
|
|
|
depth = in_depth;
|
|
|
|
|
json_tokener *tok = json_tokener_new_ex(depth);
|
|
|
|
|
if (!tok)
|
|
|
|
|
{
|
|
|
|
|
_json_c_set_last_err("json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth, strerror(errno));
|
|
|
|
|
printbuf_free(pb);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 08:26:18 +00:00
|
|
|
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
|
|
|
|
|
printbuf_memappend(pb, buf, ret);
|
|
|
|
|
}
|
|
|
|
|
if(ret < 0) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
|
2019-11-10 00:12:27 -05:00
|
|
|
json_tokener_free(tok);
|
2007-03-13 08:26:18 +00:00
|
|
|
printbuf_free(pb);
|
2012-04-28 13:26:09 -05:00
|
|
|
return NULL;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
2019-11-10 00:12:27 -05:00
|
|
|
|
|
|
|
|
obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
|
|
|
|
|
if (obj == NULL)
|
|
|
|
|
_json_c_set_last_err("json_tokener_parse_ex failed: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
|
|
|
|
|
|
|
|
|
|
json_tokener_free(tok);
|
|
|
|
|
printbuf_free(pb);
|
|
|
|
|
return obj;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-21 23:15:45 +02:00
|
|
|
struct json_object* json_object_from_file(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
struct json_object *obj;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
if((fd = open(filename, O_RDONLY)) < 0) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
|
2014-07-21 23:15:45 +02:00
|
|
|
filename, strerror(errno));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
obj = json_object_from_fd(fd);
|
|
|
|
|
close(fd);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-28 13:26:09 -05:00
|
|
|
/* extended "format and write to file" function */
|
|
|
|
|
|
2013-08-13 18:24:23 +02:00
|
|
|
int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2017-06-18 18:44:45 +00:00
|
|
|
int fd, ret;
|
|
|
|
|
int saved_errno;
|
|
|
|
|
|
|
|
|
|
if (!obj) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_to_file: object is null\n");
|
2017-06-18 18:44:45 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_to_file: error opening file %s: %s\n",
|
2017-06-18 18:44:45 +00:00
|
|
|
filename, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
ret = _json_object_to_fd(fd, obj, flags, filename);
|
|
|
|
|
saved_errno = errno;
|
|
|
|
|
close(fd);
|
|
|
|
|
errno = saved_errno;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2007-03-13 08:26:20 +00:00
|
|
|
|
2017-06-18 18:44:45 +00:00
|
|
|
int json_object_to_fd(int fd, struct json_object *obj, int flags)
|
|
|
|
|
{
|
|
|
|
|
if (!obj) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_to_fd: object is null\n");
|
2017-06-18 18:44:45 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2007-03-13 08:26:18 +00:00
|
|
|
|
2017-06-18 18:44:45 +00:00
|
|
|
return _json_object_to_fd(fd, obj, flags, NULL);
|
|
|
|
|
}
|
|
|
|
|
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
const char *json_str;
|
|
|
|
|
unsigned int wpos, wsize;
|
|
|
|
|
|
|
|
|
|
filename = filename ? filename : "(fd)";
|
|
|
|
|
|
|
|
|
|
if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
|
|
|
|
|
wpos = 0;
|
|
|
|
|
while(wpos < wsize) {
|
|
|
|
|
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
|
2017-06-18 18:44:45 +00:00
|
|
|
filename, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* because of the above check for ret < 0, we can safely cast and add */
|
|
|
|
|
wpos += (unsigned int)ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
2010-10-06 16:39:20 +00:00
|
|
|
|
2012-04-28 13:26:09 -05:00
|
|
|
// backwards compatible "format and write to file" function
|
|
|
|
|
|
2013-08-13 18:24:23 +02:00
|
|
|
int json_object_to_file(const char *filename, struct json_object *obj)
|
2012-04-28 13:26:09 -05:00
|
|
|
{
|
|
|
|
|
return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-27 11:06:49 +01:00
|
|
|
int json_parse_double(const char *buf, double *retval)
|
|
|
|
|
{
|
2017-07-30 07:30:05 +03:00
|
|
|
char *end;
|
|
|
|
|
*retval = strtod(buf, &end);
|
|
|
|
|
return end == buf ? 1 : 0;
|
2012-11-27 11:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
2010-10-06 16:39:20 +00:00
|
|
|
int json_parse_int64(const char *buf, int64_t *retval)
|
|
|
|
|
{
|
2017-09-07 17:21:14 +00:00
|
|
|
char *end = NULL;
|
|
|
|
|
int64_t val;
|
|
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
val = strtoll(buf, &end, 10);
|
|
|
|
|
if (end != buf)
|
|
|
|
|
*retval = val;
|
|
|
|
|
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
|
2010-10-06 16:39:20 +00:00
|
|
|
}
|
2010-12-07 18:15:35 +00:00
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifndef HAVE_REALLOC
|
2010-12-07 18:15:35 +00:00
|
|
|
void* rpl_realloc(void* p, size_t n)
|
|
|
|
|
{
|
|
|
|
|
if (n == 0)
|
|
|
|
|
n = 1;
|
|
|
|
|
if (p == 0)
|
|
|
|
|
return malloc(n);
|
|
|
|
|
return realloc(p, n);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-05-03 20:40:49 +00:00
|
|
|
|
|
|
|
|
#define NELEM(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
static const char* json_type_name[] = {
|
|
|
|
|
/* If you change this, be sure to update the enum json_type definition too */
|
|
|
|
|
"null",
|
|
|
|
|
"boolean",
|
|
|
|
|
"double",
|
|
|
|
|
"int",
|
|
|
|
|
"object",
|
|
|
|
|
"array",
|
|
|
|
|
"string",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *json_type_to_name(enum json_type o_type)
|
|
|
|
|
{
|
2013-02-09 16:35:24 -06:00
|
|
|
int o_type_int = (int)o_type;
|
|
|
|
|
if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
|
2011-05-03 20:40:49 +00:00
|
|
|
{
|
2017-11-29 09:25:11 -05:00
|
|
|
_json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
|
2011-05-03 20:40:49 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return json_type_name[o_type];
|
|
|
|
|
}
|
|
|
|
|
|