wsutil: rename some wsjson functions

Rename wsjson_unescape_json_string to json_decode_string_inplace
(inspired by the g_base64_decode_inplace name). Rename
wsjson_is_valid_json to json_validate (inspired by g_unichar_validate).

Ideally json_parse is inlined with its user (sharkd_session.c), but that
requires exporting the jsmn_init and jsmn_parse functions... Hence the
dependency on jsmn.h remains in wsjson.h.

Change-Id: I7ecfe3565f15516e9115cbd7e025362df2da5416
Reviewed-on: https://code.wireshark.org/review/30731
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Peter Wu 2018-11-19 15:52:30 +01:00 committed by Anders Broman
parent e2a5ad1feb
commit fb9c6905ef
6 changed files with 23 additions and 23 deletions

View File

@ -100,6 +100,9 @@ libwsutil.so.0 libwsutil0 #MINVER#
isdigit_string@Base 1.10.0
isprint_string@Base 1.10.0
isprint_utf8_string@Base 2.6.1
json_decode_string_inplace@Base 2.9.0
json_parse@Base 2.9.0
json_validate@Base 2.9.0
linear2alaw@Base 1.12.0~rc1
linear2ulaw@Base 1.12.0~rc1
local_interfaces_to_list@Base 2.1.2
@ -201,6 +204,3 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_utf8_char_len@Base 1.12.0~rc1
ws_vadd_crash_info@Base 2.5.2
ws_xton@Base 1.12.0~rc1
wsjson_parse@Base 2.9.0
wsjson_unescape_json_string@Base 2.9.0
wsjson_is_valid_json@Base 2.9.0

View File

@ -755,7 +755,7 @@ dissect_json_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *dat
guint len = tvb_captured_length(tvb);
const guint8* buf = tvb_get_string_enc(wmem_packet_scope(), tvb, 0, len, ENC_ASCII);
if (wsjson_is_valid_json(buf, len) == FALSE)
if (json_validate(buf, len) == FALSE)
return FALSE;
return (dissect_json(tvb, pinfo, tree, data) != 0);

View File

@ -79,12 +79,6 @@ struct sharkd_filter_item
static GHashTable *filter_table = NULL;
static gboolean
json_unescape_str(char *input)
{
return wsjson_unescape_json_string(input, input);
}
static const char *
json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
{
@ -4321,7 +4315,7 @@ sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
buf[tokens[i + 1].end] = '\0';
/* unescape only value, as keys are simple strings */
if (tokens[i + 1].type == JSMN_STRING && !json_unescape_str(&buf[tokens[i + 1].start]))
if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(&buf[tokens[i + 1].start]))
{
fprintf(stderr, "sanity check(3b): [%d] cannot unescape string\n", i + 1);
return;
@ -4416,7 +4410,7 @@ sharkd_session_main(void)
/* every command is line seperated JSON */
int ret;
ret = wsjson_parse(buf, NULL, 0);
ret = json_parse(buf, NULL, 0);
if (ret < 0)
{
fprintf(stderr, "invalid JSON -> closing\n");
@ -4434,7 +4428,7 @@ sharkd_session_main(void)
memset(tokens, 0, ret * sizeof(jsmntok_t));
ret = wsjson_parse(buf, tokens, ret);
ret = json_parse(buf, tokens, ret);
if (ret < 0)
{
fprintf(stderr, "invalid JSON(2) -> closing\n");

View File

@ -42,7 +42,7 @@ wtap_open_return_val json_open(wtap *wth, int *err, gchar **err_info)
return WTAP_OPEN_NOT_MINE;
}
if (wsjson_is_valid_json(filebuf, bytes_read) == FALSE) {
if (json_validate(filebuf, bytes_read) == FALSE) {
g_free(filebuf);
return WTAP_OPEN_NOT_MINE;
}

View File

@ -1,5 +1,5 @@
/* wsjson.c
* Utility to check if a payload is json using other libraries.
* JSON parsing functions.
*
* Copyright 2016, Dario Lombardo
*
@ -24,7 +24,8 @@
#include <json-glib/json-glib.h>
#endif
gboolean wsjson_is_valid_json(const guint8* buf, const size_t len)
gboolean
json_validate(const guint8 *buf, const size_t len)
{
gboolean ret = TRUE;
#ifdef HAVE_JSONGLIB
@ -69,7 +70,8 @@ gboolean wsjson_is_valid_json(const guint8* buf, const size_t len)
return ret;
}
int wsjson_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens)
int
json_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens)
{
jsmn_parser p;
@ -77,8 +79,11 @@ int wsjson_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens)
return jsmn_parse(&p, buf, strlen(buf), tokens, max_tokens);
}
gboolean wsjson_unescape_json_string(const char *input, char *output)
gboolean
json_decode_string_inplace(char *text)
{
const char *input = text;
char *output = text;
while (*input) {
char ch = *input++;

View File

@ -1,5 +1,5 @@
/* wsjson.h
* Utility to check if a payload is json using libjsmn
* JSON parsing functions.
*
* Copyright 2016, Dario Lombardo
*
@ -25,14 +25,15 @@ extern "C" {
/**
* Check if a buffer is json an returns true if it is.
*/
WS_DLL_PUBLIC gboolean wsjson_is_valid_json(const guint8* buf, const size_t len);
WS_DLL_PUBLIC gboolean json_validate(const guint8 *buf, const size_t len);
WS_DLL_PUBLIC int wsjson_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens);
WS_DLL_PUBLIC int json_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens);
/**
* Try to unescape input JSON string. output can be the same pointer as input, or must have the same buffer size as input.
* Decode the contents of a JSON string value by overwriting the input data.
* Returns TRUE on success and FALSE if invalid characters were encountered.
*/
WS_DLL_PUBLIC gboolean wsjson_unescape_json_string(const char *input, char *output);
WS_DLL_PUBLIC gboolean json_decode_string_inplace(char *text);
#ifdef __cplusplus
}