From 9d97962759e089e46c3087e92ed3a4e594ea81d3 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 24 May 2020 21:51:39 +0200 Subject: [PATCH] use system jansson if available --- CHANGELOG.md | 2 + client/CMakeLists.txt | 1 + client/Makefile | 24 +++- client/android/CMakeLists.txt | 1 + client/deps/jansson.cmake | 1 - client/deps/jansson/Makefile | 1 - client/deps/jansson/jansson.h | 8 -- client/src/emv/emvjson.h | 1 + .../jansson/path.c => src/jansson_path.c} | 105 +++++++++++++++++- client/src/jansson_path.h | 30 +++++ 10 files changed, 158 insertions(+), 16 deletions(-) rename client/{deps/jansson/path.c => src/jansson_path.c} (71%) create mode 100644 client/src/jansson_path.h diff --git a/CHANGELOG.md b/CHANGELOG.md index be3956739..d9d62254f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Add `make check` mechanics (@doegox) - Change, log files moved to subfolders (@doegox) - Change, use system Lua library if available (@doegox) + - Change, use system Jansson library if available (@doegox) + - Change, use system Whereami library if available (@doegox) - Fix release version information (@doegox) ## [ice coffee.4.9237][2020-05-21] diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index a56ea5a9a..64cfca623 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -165,6 +165,7 @@ set (TARGET_SOURCES src/fileutils.c src/flash.c src/graph.c + src/jansson_path.c src/preferences.c src/pm3_binlib.c src/pm3_bitlib.c diff --git a/client/Makefile b/client/Makefile index f919a9388..d3c969b9c 100644 --- a/client/Makefile +++ b/client/Makefile @@ -82,17 +82,27 @@ ZLIB = $(OBJDIR)/libz.a ifneq ($(SKIPLUASYSTEM),1) LUAINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags lua5.2 2>/dev/null) LUALDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs lua5.2 2>/dev/null) - ifneq ($(LUAINCLUDES),) + ifneq ($(LUALDLIBS),) LUALIB = $(LUALDLIBS) LUALIBINC = $(LUAINCLUDES) LUASYSTEM = 1 endif endif +ifneq ($(SKIPJANSSONSYSTEM),1) + JANSSONINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags jansson 2>/dev/null) + JANSSONLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs jansson 2>/dev/null) + ifneq ($(JANSSONLDLIBS),) + JANSSONLIB = $(JANSSONLDLIBS) + JANSSONLIBINC = $(JANSSONINCLUDES) + JANSSONSYSTEM = 1 + endif +endif + ifneq ($(SKIPWHEREAMISYSTEM),1) ifneq (,$(wildcard /usr/include/whereami.h)) - WAILIBINC = WAILIB = -lwhereami + WAILIBINC = WAISYSTEM = 1 endif endif @@ -195,6 +205,9 @@ $(info native BT support: Bluez found, enabled) else $(info native BT support: Bluez not found, disabled) endif +ifeq ($(JANSSONSYSTEM),1) +$(info Jansson library: system library found) +endif ifeq ($(LUASYSTEM),1) $(info Lua library: system library found) endif @@ -339,9 +352,8 @@ CMDSRCS = crapto1/crapto1.c \ wiegand_formats.c \ wiegand_formatutils.c \ cardhelper.c \ - preferences.c - - + preferences.c \ + jansson_path.c COREOBJS = $(CORESRCS:%.c=$(OBJDIR)/%.o) CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o) @@ -430,8 +442,10 @@ ifneq ($(LUASYSTEM),1) endif jansson: +ifneq ($(JANSSONSYSTEM),1) $(info [*] MAKE $@) $(Q)$(MAKE) --no-print-directory -C $(JANSSONLIBPATH) all +endif tinycbor: $(info [*] MAKE $@) diff --git a/client/android/CMakeLists.txt b/client/android/CMakeLists.txt index 5b417ff63..ddaae2cd2 100644 --- a/client/android/CMakeLists.txt +++ b/client/android/CMakeLists.txt @@ -145,6 +145,7 @@ add_library(pm3rrg_rdv4 SHARED ${PM3_ROOT}/client/src/cmdlfpcf7931.c ${PM3_ROOT}/client/src/cmdhfmfhard.c ${PM3_ROOT}/client/src/cmdusart.c + ${PM3_ROOT}/client/src/jansson_path.c # android resources jni_tools.c pm3_main.c diff --git a/client/deps/jansson.cmake b/client/deps/jansson.cmake index a95ff0baf..c91a47047 100644 --- a/client/deps/jansson.cmake +++ b/client/deps/jansson.cmake @@ -9,7 +9,6 @@ add_library(pm3rrg_rdv4_jansson STATIC jansson/strbuffer.c jansson/strconv.c jansson/utf.c - jansson/path.c jansson/value.c ) diff --git a/client/deps/jansson/Makefile b/client/deps/jansson/Makefile index 5073b78a1..b51c4a77c 100644 --- a/client/deps/jansson/Makefile +++ b/client/deps/jansson/Makefile @@ -13,7 +13,6 @@ MYSRCS = \ strbuffer.c \ strconv.c \ utf.c \ - path.c \ value.c LIB_A = libjansson.a diff --git a/client/deps/jansson/jansson.h b/client/deps/jansson/jansson.h index 0f868c2f2..75cbb5d51 100644 --- a/client/deps/jansson/jansson.h +++ b/client/deps/jansson/jansson.h @@ -302,14 +302,6 @@ int json_equal(const json_t *json1, const json_t *json2); json_t *json_copy(json_t *json) JANSSON_ATTRS(warn_unused_result); json_t *json_deep_copy(const json_t *json) JANSSON_ATTRS(warn_unused_result); -json_t *json_path_get(const json_t *json, const char *path); -int json_path_set_new(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error); - -static JSON_INLINE -int json_path_set(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error) { - return json_path_set_new(json, path, json_incref(value), flags, error); -} - /* decoding */ #define JSON_REJECT_DUPLICATES 0x1 diff --git a/client/src/emv/emvjson.h b/client/src/emv/emvjson.h index f4f3d4fb4..e90aa42d8 100644 --- a/client/src/emv/emvjson.h +++ b/client/src/emv/emvjson.h @@ -13,6 +13,7 @@ #include "common.h" #include "jansson.h" +#include "jansson_path.h" #include "tlv.h" typedef struct { diff --git a/client/deps/jansson/path.c b/client/src/jansson_path.c similarity index 71% rename from client/deps/jansson/path.c rename to client/src/jansson_path.c index 11964ec6e..0c0d76a05 100644 --- a/client/deps/jansson/path.c +++ b/client/src/jansson_path.c @@ -11,7 +11,110 @@ #include #include "jansson.h" -#include "jansson_private.h" +#include "jansson_path.h" + +////// memory.c private functions + +/* C89 allows these to be macros */ +#undef malloc +#undef free + +/* memory function pointers */ +static json_malloc_t do_malloc = malloc; +static json_free_t do_free = free; + +static void *jsonp_malloc(size_t size) { + if (!size) + return NULL; + + return (*do_malloc)(size); +} + +static void jsonp_free(void *ptr) { + if (!ptr) + return; + + (*do_free)(ptr); +} + +static char *jsonp_strndup(const char *str, size_t len) { + char *new_str; + + new_str = jsonp_malloc(len + 1); + if (!new_str) + return NULL; + + memcpy(new_str, str, len); + new_str[len] = '\0'; + return new_str; +} + +static char *jsonp_strdup(const char *str) { + return jsonp_strndup(str, strlen(str)); +} + +////// error.c private functions + +static void jsonp_error_set_source(json_error_t *error, const char *source) { + size_t length; + + if (!error || !source) + return; + + length = strlen(source); + if (length < JSON_ERROR_SOURCE_LENGTH) { + strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH - 1); + } else { + size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4; + memcpy(error->source, "...", 3); + strncpy(error->source + 3, source + extra, length - extra + 1); + } +} + +static void jsonp_error_init(json_error_t *error, const char *source) { + if (error) { + error->text[0] = '\0'; + error->line = -1; + error->column = -1; + error->position = 0; + if (source) + jsonp_error_set_source(error, source); + else + error->source[0] = '\0'; + } +} + +static void jsonp_error_vset(json_error_t *error, int line, int column, + size_t position, enum json_error_code code, + const char *msg, va_list ap) { + if (!error) + return; + + if (error->text[0] != '\0') { + /* error already set */ + return; + } + + error->line = line; + error->column = column; + error->position = (int)position; + + vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH - 1, msg, ap); + error->text[JSON_ERROR_TEXT_LENGTH - 2] = '\0'; + error->text[JSON_ERROR_TEXT_LENGTH - 1] = code; +} + +static void jsonp_error_set(json_error_t *error, int line, int column, + size_t position, enum json_error_code code, + const char *msg, ...) { + va_list ap; + va_start(ap, msg); + jsonp_error_vset(error, line, column, position, code, msg, ap); + va_end(ap); +} + + +// original path.c from jansson fork json_t *json_path_get(const json_t *json, const char *path) { static const char root_chr = '$', array_open = '['; diff --git a/client/src/jansson_path.h b/client/src/jansson_path.h new file mode 100644 index 000000000..7737c1634 --- /dev/null +++ b/client/src/jansson_path.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2009-2016 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef JANSSON_PATH_H +#define JANSSON_PATH_H + +//#include +#include /* for size_t */ +//#include + +#ifdef __cplusplus +extern "C" { +#endif + +json_t *json_path_get(const json_t *json, const char *path); +int json_path_set_new(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error); + +static JSON_INLINE +int json_path_set(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error) { + return json_path_set_new(json, path, json_incref(value), flags, error); +} + +#ifdef __cplusplus +} +#endif +#endif