Blob Blame Raw
commit 57ae84b2d8b2e0af1a66d529e4e94b21f3df319d
Author: Bjรถrn Esser <besser82@fedoraproject.org>
Date:   Tue Dec 12 15:31:53 2017 +0100

    modules/json: Properly detect json-c version and adaptions for v0.13

diff --git a/modules/json/Makefile b/modules/json/Makefile
index 5c514655b..479bec701 100644
--- a/modules/json/Makefile
+++ b/modules/json/Makefile
@@ -2,7 +2,7 @@
 #
 # Memcached implementation for memcache API.
 #
-# 
+#
 # WARNING: do not run this directly, it should be run by the master Makefile
 
 include ../../Makefile.defs
@@ -23,8 +23,23 @@ ifeq ($(JSON_BUILDER),)
 	DEFS += -I$(LOCALBASE)/include -I$(SYSBASE)/include
 	LIBS += -L$(LOCALBASE)/lib -ljson
 else
-	JSON_LIB_VER = $(shell $(JSON_BUILDER) --modversion | sed  "s/\.\([0-9]\)\./.0\1./g" | sed  "s/\.\([0-9]\)\$$/.0\1/g" | tr -d "." | sed -e "s/^0*//" )
-	DEFS += $(shell $(JSON_BUILDER) --cflags) -DJSON_LIB_VERSION=$(JSON_LIB_VER)
+	JSON_PKG_VER = $(shell $(JSON_BUILDER) --modversion)
+	JSON_PKG_MAJOR = $(shell \
+		ver_tmp=`echo '$(JSON_PKG_VER)' | cut -f1 -d.`; \
+		if [ -z "$$ver_tmp" ]; then ver_tmp=0; fi; \
+		echo "$$ver_tmp";)
+	JSON_PKG_MINOR = $(shell \
+		ver_tmp=`echo '$(JSON_PKG_VER)' | cut -f2 -d.`; \
+		if [ -z "$$ver_tmp" ]; then ver_tmp=0; fi; \
+		echo "$$ver_tmp";)
+	JSON_PKG_MICRO = $(shell \
+		ver_tmp=`echo '$(JSON_PKG_VER)' | cut -f3 -d.`; \
+		if [ -z "$$ver_tmp" ]; then ver_tmp=0; fi; \
+		echo "$$ver_tmp";)
+	DEFS += $(shell $(JSON_BUILDER) --cflags) \
+		-DJSON_PKG_MAJOR=$(JSON_PKG_MAJOR) \
+		-DJSON_PKG_MINOR=$(JSON_PKG_MINOR) \
+		-DJSON_PKG_MICRO=$(JSON_PKG_MICRO)
 	LIBS += $(shell $(JSON_BUILDER) --libs)
 endif
 
diff --git a/modules/json/array_del.c b/modules/json/array_del.c
index b23f6d3b3..9be6e2f84 100644
--- a/modules/json/array_del.c
+++ b/modules/json/array_del.c
@@ -22,27 +22,34 @@
  * History:
  * ---------
  *  2009-09-04  first version (andreidragus)
+ *  2017-12-12  use opensips_json_c_helper.h (besser82)
  */
 
-#include <json.h>
-#include <json_object_private.h>
+#include "opensips_json_c_helper.h"
 
-void array_list_del_idx( struct array_list * arr, int idx)
+#if JSON_C_VERSION_NUM < JSON_C_VER_013
+void array_list_del_idx(struct array_list * arr, int idx)
 {
 	int i;
 
-	if( idx >= arr->length)
+	if(idx >= arr->length)
 		return;
 
 
 	arr->free_fn(arr->array[idx]);
 	arr->length--;
 
-	for( i=idx; i<arr->length; i++ )
+	for(i=idx; i<arr->length; i++)
 		arr->array[i]  = arr->array[i+1];
 };
+#endif
 
 void json_object_array_del(struct json_object* obj, int idx)
 {
+#if JSON_C_VERSION_NUM >= JSON_C_VER_013
+	struct array_list * arr = json_object_get_array(obj);
+	array_list_del_idx(arr, idx, arr->length);
+#else
 	array_list_del_idx(obj->o.c_array, idx);
+#endif
 };
diff --git a/modules/json/json.c b/modules/json/json.c
index 452e4a801..c709e44cf 100644
--- a/modules/json/json.c
+++ b/modules/json/json.c
@@ -22,6 +22,7 @@
  * History:
  * ---------
  *  2009-09-04  first version (andreidragus)
+ *  2017-12-12  use opensips_json_c_helper.h (besser82)
  */
 
 
@@ -43,14 +44,9 @@
 #include "../../mi/mi.h"
 #include "../tm/tm_load.h"
 #include "../rr/api.h"
+#include "opensips_json_c_helper.h"
 
 
-#include <json.h>
-#include <json_object_private.h>
-
-
-#define JSON_BUFF_SIZE 4096
-
 enum
 {
 	TAG_KEY = 1,
@@ -93,7 +89,7 @@ typedef struct _json_name
 }json_name;
 
 pv_json_t * all;
-char buff[JSON_BUFF_SIZE];
+char buff[JSON_FILE_BUF_SIZE];
 
 static int mod_init(void);
 static int child_init(int );
@@ -298,19 +294,16 @@ json_t * get_object(pv_json_t * var, pv_param_t* pvp ,  json_tag ** tag,
 				!json_object_is_type( cur_obj, json_type_object ) )
 				goto error;
 
-#if JSON_LIB_VERSION < 10
+#if JSON_C_VERSION_NUM >= JSON_C_VER_010
+			if (!json_object_object_get_ex( cur_obj,buff, &cur_obj ) &&
+				tag == NULL)
+				goto error;
+#else
 			cur_obj = json_object_object_get( cur_obj, buff );
 
 			if( cur_obj == NULL && tag == NULL)
 				goto error;
-#else
-			if (!json_object_object_get_ex( cur_obj,buff, &cur_obj ) &&
-				tag == NULL)
-				goto error;
 #endif
-
-
-
 		}
 
 		if( cur_tag->type & TAG_IDX )
@@ -418,7 +411,7 @@ int pv_get_json (struct sip_msg* msg,  pv_param_t* pvp, pv_value_t* val)
 	{
 		val->flags = PV_VAL_STR;
 		val->rs.s = (char*)json_object_get_string( obj );
-#if JSON_LIB_VERSION >= 10
+#if JSON_C_VERSION_NUM >= JSON_C_VER_010
 		val->rs.len = json_object_get_string_len( obj );
 #else
 		val->rs.len = strlen(val->rs.s);
@@ -590,7 +583,7 @@ int pv_set_json (struct sip_msg* msg,  pv_param_t* pvp, int flag ,
 		if (obj == NULL)
 		{
 			LM_ERR("Error parsing json: %s\n",
-#if JSON_LIB_VERSION >= 10
+#if JSON_C_VERSION_NUM >= JSON_C_VER_010
 				json_tokener_error_desc(parse_status)
 #else
 				json_tokener_errors[(unsigned long)obj]
diff --git a/modules/json/opensips_json_c_helper.h b/modules/json/opensips_json_c_helper.h
new file mode 100644
index 000000000..71c5c2189
--- /dev/null
+++ b/modules/json/opensips_json_c_helper.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2017 Bjรถrn Esser <besser82@fedoraproject.org>
+ *
+ * This file is part of opensips, a free SIP server.
+ *
+ * opensips is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * opensips is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ *
+ * History:
+ * ---------
+ *  2017-12-12 first version (besser82)
+ */
+
+#ifndef OPENSIPS_JSON_C_HELPER_H
+#define OPENSIPS_JSON_C_HELPER_H
+
+// If those are not defined, we assume to build against json-c v0.9.
+// Starting with v0.11 there is json_c_version.h, which we prefer
+// anyways, so there are no regressions in this case.  Everything
+// conditionalized for v0.10 or later doesn't produce any fallout,
+// when we are asuming v0.9 and building against v0.10.
+#ifndef JSON_PKG_MAJOR
+#define JSON_PKG_MAJOR	0
+#endif
+#ifndef JSON_PKG_MINOR
+#define JSON_PKG_MINOR	9
+#endif
+#ifndef JSON_PKG_MICRO
+#define JSON_PKG_MICRO	0
+#endif
+
+// json.h automatically includes json_c_version.h, if available.
+#include <json.h>
+
+// We prefer JSON_C_VERSION_NUM defined in json_c_version.h.  If it is
+// not defined, we construct it the same way from our JSON_PKG_* defines.
+#ifndef JSON_C_VERSION_NUM
+#define JSON_C_VERSION_NUM (JSON_PKG_MAJOR << 16) | \
+			   (JSON_PKG_MINOR << 8)  | \
+			    JSON_PKG_MICRO
+#endif
+
+// Macros for checking specific versions.
+#define JSON_C_VER_010 (10 << 8)
+#define JSON_C_VER_013 (13 << 8)
+
+// json_object_private.h is gone and not needed anymore in json-c v0.13+.
+#if JSON_C_VERSION_NUM < JSON_C_VER_013
+#include <json_object_private.h>
+#endif
+
+// Newer versions of json-c define this in their headers, so we prefer
+// their definition in that case.
+#ifndef JSON_FILE_BUF_SIZE
+#define JSON_FILE_BUF_SIZE 4096
+#endif
+
+#endif // OPENSIPS_JSON_C_HELPER_H