1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: dc3e7f6ca215410a327b236615883d1680baacdb $
19  *
20  * @brief Workarounds for missing functions in older json-c libraries.
21  * @file jsonc_missing.c
22  *
23  * @author Aaron Hurt <ahurt@anbcs.com>
24  * @copyright 2013-2014 The FreeRADIUS Server Project.
25  */
26 
27 RCSID("$Id: dc3e7f6ca215410a327b236615883d1680baacdb $")
28 
29 #include <string.h>
30 
31 #include "jsonc_missing.h"
32 
33 #ifndef HAVE_JSON_C_VERSION
json_c_version(void)34 	const char *json_c_version(void) {
35 		return "Unknown (less than 0.10) - Please upgrade";
36 	}
37 #endif
38 
39 #ifndef HAVE_JSON_OBJECT_GET_STRING_LEN
json_object_get_string_len(json_object * obj)40 int json_object_get_string_len(json_object *obj) {
41 	if ((obj == NULL) || (json_object_get_type(obj) != json_type_string))
42 		return 0;
43 	return (int)strlen(json_object_get_string(obj));
44 }
45 #endif
46 
47 #ifndef HAVE_JSON_OBJECT_OBJECT_GET_EX
json_object_object_get_ex(struct json_object * jso,const char * key,struct json_object ** value)48 int json_object_object_get_ex(struct json_object *jso, const char *key, struct json_object **value) {
49 	struct json_object *jobj;
50 
51 	if ((jso == NULL) || (key == NULL)) return 0;
52 	if (value != NULL) *value = NULL;
53 
54 	switch (json_object_get_type(jso)) {
55 	case json_type_object:
56 		jobj = json_object_object_get(jso, key);
57 		if (jobj == NULL) return 0;
58 
59 		if (value != NULL) *value = jobj;
60 		return 1;
61 
62 	default:
63 		if (value != NULL) *value = NULL;
64 		return 0;
65 	}
66 }
67 #endif
68 
69 #ifndef HAVE_JSON_TOKENER_GET_ERROR
json_tokener_get_error(json_tokener * tok)70 enum json_tokener_error json_tokener_get_error(json_tokener *tok) {
71 	return tok->err;
72 }
73 #endif
74 
75 #ifndef HAVE_JSON_TOKENER_ERROR_DESC
json_tokener_error_desc(enum json_tokener_error jerr)76 const char *json_tokener_error_desc(enum json_tokener_error jerr) {
77 	int jerr_int = (int)jerr;
78 	if (json_tokener_errors[jerr_int] == NULL)
79 		return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()";
80 	return json_tokener_errors[jerr_int];
81 }
82 #endif
83