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 <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Dario Lombardo 2016-09-28 12:01:33 +02:00 committed by Peter Wu
parent 534787e402
commit 11c10244ef
9 changed files with 204 additions and 146 deletions

View File

@ -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

View File

@ -31,7 +31,7 @@
#include <epan/packet.h>
#include <epan/tvbparse.h>
#include <wsutil/jsmn.h>
#include <wsutil/wsjsmn.h>
#include <wsutil/str_util.h>
#include <wsutil/unicode-utils.h>

View File

@ -25,7 +25,7 @@
#include "file_wrappers.h"
#include "json.h"
#include <wsutil/jsmn.h>
#include <wsutil/wsjsmn.h>
static gboolean json_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
Buffer *buf, int *err, gchar **err_info)

View File

@ -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
#

View File

@ -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

View File

@ -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 <stdlib.h>
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:
*/

View File

@ -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 <stddef.h>
#include <glib.h>
#include <ws_symbol_export.h>
#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:
*/

80
wsutil/wsjsmn.c Normal file
View File

@ -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 <gerald@wireshark.org>
* 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 <wsutil/jsmn.h>
#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:
*/

57
wsutil/wsjsmn.h Normal file
View File

@ -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 <gerald@wireshark.org>
* 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 <glib.h>
#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:
*/