From 11c10244ef5168a337d88611d61c5ebc88acd29e Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Wed, 28 Sep 2016 12:01:33 +0200 Subject: [PATCH] wsutil: split libjsmn. The libjsmn was imported into the tree and enhanced with a new function. This change splits it into the "original" libjsmn and an addictional module wsjsmn that contains the new function. This will make easier to port within the tree future versions of the library. Change-Id: I3f1caa91bee462e0767e5e18d0b6a10f0b1cad32 Reviewed-on: https://code.wireshark.org/review/17963 Petri-Dish: Peter Wu Tested-by: Petri Dish Buildbot Reviewed-by: Peter Wu --- debian/libwsutil0.symbols | 1 - epan/dissectors/packet-json.c | 2 +- wiretap/json.c | 2 +- wsutil/CMakeLists.txt | 3 + wsutil/Makefile.am | 7 ++- wsutil/jsmn.c | 105 +++++++--------------------------- wsutil/jsmn.h | 93 ++++++++++++------------------ wsutil/wsjsmn.c | 80 ++++++++++++++++++++++++++ wsutil/wsjsmn.h | 57 ++++++++++++++++++ 9 files changed, 204 insertions(+), 146 deletions(-) create mode 100644 wsutil/wsjsmn.c create mode 100644 wsutil/wsjsmn.h diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index 5224d04f7a..3f908b6c7b 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -89,7 +89,6 @@ libwsutil.so.0 libwsutil0 #MINVER# is_default_profile@Base 1.12.0~rc1 isdigit_string@Base 1.10.0 isprint_string@Base 1.10.0 - jsmn_init@Base 1.99.4 jsmn_is_json@Base 1.99.4 linear2alaw@Base 1.12.0~rc1 linear2ulaw@Base 1.12.0~rc1 diff --git a/epan/dissectors/packet-json.c b/epan/dissectors/packet-json.c index 1fa50ea182..5220e75850 100644 --- a/epan/dissectors/packet-json.c +++ b/epan/dissectors/packet-json.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include diff --git a/wiretap/json.c b/wiretap/json.c index 4842ee19d2..5c3c1bd243 100644 --- a/wiretap/json.c +++ b/wiretap/json.c @@ -25,7 +25,7 @@ #include "file_wrappers.h" #include "json.h" -#include +#include static gboolean json_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info) diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 539126df04..438bc42cbb 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -69,6 +69,7 @@ set(WSUTIL_COMMON_FILES type_util.c unicode-utils.c ws_mempbrk.c + wsjsmn.c ) set(WSUTIL_FILES ${WSUTIL_COMMON_FILES}) @@ -258,6 +259,8 @@ CHECKAPI( ${WSUTIL_COMMON_FILES} ) +set_source_files_properties(jsmn.c PROPERTIES COMPILE_DEFINITIONS "JSMN_STRICT") + # # Editor modelines - http://www.wireshark.org/tools/modelines.html # diff --git a/wsutil/Makefile.am b/wsutil/Makefile.am index 6f104b51f2..3886e26231 100644 --- a/wsutil/Makefile.am +++ b/wsutil/Makefile.am @@ -25,6 +25,7 @@ AM_CPPFLAGS = $(INCLUDEDIRS) $(WS_CPPFLAGS) -DWS_BUILD_DLL \ -DDATAFILE_DIR=\"$(pkgdatadir)\" \ -DEXTCAP_DIR=\"$(extcapdir)\" \ -DPLUGIN_INSTALL_DIR=\"$(plugindir)\" \ + -DJSMN_STRICT \ $(GLIB_CFLAGS) $(LIBGCRYPT_CFLAGS) # Optional headers for ABI checking @@ -92,7 +93,8 @@ libwsutil_nonrepl_INCLUDES = \ ws_cpuid.h \ ws_mempbrk.h \ ws_mempbrk_int.h \ - ws_printf.h + ws_printf.h \ + wsjsmn.h # Header files for functions in libwsutil's ABI on this platform. libwsutil_abi_INCLUDES = \ @@ -159,7 +161,8 @@ libwsutil_la_SOURCES = \ time_util.c \ type_util.c \ unicode-utils.c \ - ws_mempbrk.c + ws_mempbrk.c \ + wsjsmn.c if HAVE_OS_X_FRAMEWORKS libwsutil_la_SOURCES += cfutils.c cfutils.h diff --git a/wsutil/jsmn.c b/wsutil/jsmn.c index b87e373c58..d5e3bb5faf 100644 --- a/wsutil/jsmn.c +++ b/wsutil/jsmn.c @@ -1,35 +1,27 @@ /* - * Copyright (c) 2010 Serge A. Zaitsev - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ +Copyright (c) 2010 Serge A. Zaitsev -#include +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -/* WS modification starts here */ -#define JSMN_STRICT -/* WS modification ends here */ +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ #include "jsmn.h" -#include "log.h" - /** * Allocates a fresh unused token from the token pull. */ @@ -52,7 +44,7 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, * Fills token type and boundaries. */ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, - int start, int end) { + int start, int end) { token->type = type; token->start = start; token->end = end; @@ -109,7 +101,7 @@ found: } /** - * Filsl next token with JSON string. + * Fills next token with JSON string. */ static int jsmn_parse_string(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens) { @@ -183,7 +175,7 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, int r; int i; jsmntok_t *token; - int count = 0; + int count = parser->toknext; for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { char c; @@ -338,58 +330,3 @@ void jsmn_init(jsmn_parser *parser) { parser->toknext = 0; parser->toksuper = -1; } - -/* WS modification starts here */ -gboolean jsmn_is_json(const guint8* buf, const size_t len) -{ - /* We expect no more than 1024 tokens */ - guint max_tokens = 1024; - jsmntok_t* t; - jsmn_parser p; - gboolean ret = TRUE; - int rcode; - - t = g_new0(jsmntok_t, max_tokens); - - if (!t) - return FALSE; - - jsmn_init(&p); - rcode = jsmn_parse(&p, buf, len, t, max_tokens); - if (rcode < 0) { - switch (rcode) { - case JSMN_ERROR_NOMEM: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: not enough tokens were provided"); - break; - case JSMN_ERROR_INVAL: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: invalid character inside JSON string"); - break; - case JSMN_ERROR_PART: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: the string is not a full JSON packet, " - "more bytes expected"); - break; - default: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: unexpected error"); - break; - } - ret = FALSE; - } - - g_free(t); - - return ret; -} -/* WS modification ends here */ - -/* - * Editor modelines - https://www.wireshark.org/tools/modelines.html - * - * Local variables: - * c-basic-offset: 8 - * tab-width: 8 - * indent-tabs-mode: t - * End: - * - * vi: set shiftwidth=8 tabstop=8 noexpandtab: - * :indentSize=8:tabSize=8:noTabs=false: - */ diff --git a/wsutil/jsmn.h b/wsutil/jsmn.h index 7cf51aa81c..6f3224749b 100644 --- a/wsutil/jsmn.h +++ b/wsutil/jsmn.h @@ -1,66 +1,63 @@ /* - * Copyright (c) 2010 Serge A. Zaitsev - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ +Copyright (c) 2010 Serge A. Zaitsev + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ #ifndef __JSMN_H_ #define __JSMN_H_ #include -#include -#include - #ifdef __cplusplus extern "C" { #endif /** * JSON type identifier. Basic types are: - * o Object - * o Array - * o String - * o Other primitive: number, boolean (true/false) or null + * o Object + * o Array + * o String + * o Other primitive: number, boolean (true/false) or null */ typedef enum { - JSMN_PRIMITIVE = 0, + JSMN_UNDEFINED = 0, JSMN_OBJECT = 1, JSMN_ARRAY = 2, - JSMN_STRING = 3 + JSMN_STRING = 3, + JSMN_PRIMITIVE = 4 } jsmntype_t; -typedef enum { +enum jsmnerr { /* Not enough tokens were provided */ JSMN_ERROR_NOMEM = -1, /* Invalid character inside JSON string */ JSMN_ERROR_INVAL = -2, /* The string is not a full JSON packet, more bytes expected */ JSMN_ERROR_PART = -3 -} jsmnerr_t; +}; /** * JSON token description. - * type type (object, array, string etc.) - * start start position in JSON data string - * end end position in JSON data string - * size the size of the token + * @param type type (object, array, string etc.) + * @param start start position in JSON data string + * @param end end position in JSON data string */ typedef struct { jsmntype_t type; @@ -77,15 +74,15 @@ typedef struct { * the string being parsed now and current position in that string */ typedef struct { - unsigned int pos; /* offset in the JSON string */ - unsigned int toknext; /* next token to allocate */ - int toksuper; /* superior token node, e.g parent object or array */ + unsigned int pos; /* offset in the JSON string */ + unsigned int toknext; /* next token to allocate */ + int toksuper; /* superior token node, e.g parent object or array */ } jsmn_parser; /** * Create JSON parser over an array of tokens */ -WS_DLL_PUBLIC void jsmn_init(jsmn_parser *parser); +void jsmn_init(jsmn_parser *parser); /** * Run JSON parser. It parses a JSON data string into and array of tokens, each describing @@ -94,26 +91,8 @@ WS_DLL_PUBLIC void jsmn_init(jsmn_parser *parser); int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens); -/** - * Check if a buffer is json an returns true if it is. - */ -WS_DLL_PUBLIC gboolean jsmn_is_json(const guint8* buf, const size_t len); - #ifdef __cplusplus } #endif #endif /* __JSMN_H_ */ - -/* - * Editor modelines - http://www.wireshark.org/tools/modelines.html - * - * Local variables: - * c-basic-offset: 8 - * tab-width: 8 - * indent-tabs-mode: t - * End: - * - * vi: set shiftwidth=8 tabstop=8 noexpandtab: - * :indentSize=8:tabSize=8:noTabs=false: - */ diff --git a/wsutil/wsjsmn.c b/wsutil/wsjsmn.c new file mode 100644 index 0000000000..e18bd45a4e --- /dev/null +++ b/wsutil/wsjsmn.c @@ -0,0 +1,80 @@ +/* wsjsmn.c + * Utility to check if a payload is json using libjsmn + * + * Copyright 2016, Dario Lombardo + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program 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. + * + * This program 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. + */ + +#include "wsjsmn.h" +#include +#include "log.h" + +gboolean jsmn_is_json(const guint8* buf, const size_t len) +{ + /* We expect no more than 1024 tokens */ + guint max_tokens = 1024; + jsmntok_t* t; + jsmn_parser p; + gboolean ret = TRUE; + int rcode; + + t = g_new0(jsmntok_t, max_tokens); + + if (!t) + return FALSE; + + jsmn_init(&p); + rcode = jsmn_parse(&p, buf, len, t, max_tokens); + if (rcode < 0) { + switch (rcode) { + case JSMN_ERROR_NOMEM: + g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: not enough tokens were provided"); + break; + case JSMN_ERROR_INVAL: + g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: invalid character inside JSON string"); + break; + case JSMN_ERROR_PART: + g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: the string is not a full JSON packet, " + "more bytes expected"); + break; + default: + g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: unexpected error"); + break; + } + ret = FALSE; + } + + g_free(t); + + return ret; +} + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: t + * End: + * + * vi: set shiftwidth=4 tabstop=8 noexpandtab: + * :indentSize=4:tabSize=8:noTabs=false: + */ diff --git a/wsutil/wsjsmn.h b/wsutil/wsjsmn.h new file mode 100644 index 0000000000..0aa7220e4a --- /dev/null +++ b/wsutil/wsjsmn.h @@ -0,0 +1,57 @@ +/* wsjsmn.h + * Utility to check if a payload is json using libjsmn + * + * Copyright 2016, Dario Lombardo + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program 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. + * + * This program 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. + */ + +#ifndef __WSJSMN_H__ +#define __WSJSMN_H__ + +#include "ws_symbol_export.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Check if a buffer is json an returns true if it is. + */ +WS_DLL_PUBLIC gboolean jsmn_is_json(const guint8* buf, const size_t len); + +#ifdef __cplusplus +} +#endif + +#endif + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: t + * End: + * + * vi: set shiftwidth=4 tabstop=8 noexpandtab: + * :indentSize=4:tabSize=8:noTabs=false: + */ \ No newline at end of file