diff options
author | Pascal Quantin <pascal@wireshark.org> | 2019-06-16 12:40:14 +0200 |
---|---|---|
committer | Pascal Quantin <pascal@wireshark.org> | 2019-06-16 11:30:24 +0000 |
commit | be3d469ddccf2295bf4fb5335e6116cd8d5303ee (patch) | |
tree | d9f491b06026beef12a044c7106fff0c6b6c7ace /wsutil | |
parent | 3487b8f9c35932d4fa71f5b770278442d0047473 (diff) |
NGAP: fix dissection of N2 Information Content
Change-Id: I8aaf578c8eb71533313cf2cfd42871eae0c0ff57
Reviewed-on: https://code.wireshark.org/review/33603
Petri-Dish: Pascal Quantin <pascal@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Pascal Quantin <pascal@wireshark.org>
Diffstat (limited to 'wsutil')
-rw-r--r-- | wsutil/wsjson.c | 71 | ||||
-rw-r--r-- | wsutil/wsjson.h | 19 |
2 files changed, 90 insertions, 0 deletions
diff --git a/wsutil/wsjson.c b/wsutil/wsjson.c index b6a8ddeb4a..96145af81f 100644 --- a/wsutil/wsjson.c +++ b/wsutil/wsjson.c @@ -15,6 +15,7 @@ #include "wsjson.h" #include <string.h> +#include <errno.h> #include <wsutil/jsmn.h> #include <wsutil/str_util.h> #include <wsutil/unicode-utils.h> @@ -69,6 +70,76 @@ json_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens) return jsmn_parse(&p, buf, strlen(buf), tokens, max_tokens); } +static +jsmntok_t *json_get_next_object(jsmntok_t *cur) +{ + int i; + jsmntok_t *next = cur+1; + + for (i = 0; i < cur->size; i++) { + next = json_get_next_object(next); + } + return next; +} + +jsmntok_t *json_get_object(const char *buf, jsmntok_t *parent, const gchar* name) +{ + int i; + jsmntok_t *cur = parent+1; + + for (i = 0; i < parent->size; i++) { + if (cur->type == JSMN_STRING && + !strncmp(&buf[cur->start], name, cur->end - cur->start) + && strlen(name) == (size_t)(cur->end - cur->start) && + cur->size == 1 && (cur+1)->type == JSMN_OBJECT) { + return cur+1; + } + cur = json_get_next_object(cur); + } + return NULL; +} + +char *json_get_string(char *buf, jsmntok_t *parent, const gchar* name) +{ + int i; + jsmntok_t *cur = parent+1; + + for (i = 0; i < parent->size; i++) { + if (cur->type == JSMN_STRING && + !strncmp(&buf[cur->start], name, cur->end - cur->start) + && strlen(name) == (size_t)(cur->end - cur->start) && + cur->size == 1 && (cur+1)->type == JSMN_STRING) { + buf[(cur+1)->end] = '\0'; + if (!json_decode_string_inplace(&buf[(cur+1)->start])) + return NULL; + return &buf[(cur+1)->start]; + } + cur = json_get_next_object(cur); + } + return NULL; +} + +gboolean json_get_double(char *buf, jsmntok_t *parent, const gchar* name, gdouble *val) +{ + int i; + jsmntok_t *cur = parent+1; + + for (i = 0; i < parent->size; i++) { + if (cur->type == JSMN_STRING && + !strncmp(&buf[cur->start], name, cur->end - cur->start) + && strlen(name) == (size_t)(cur->end - cur->start) && + cur->size == 1 && (cur+1)->type == JSMN_PRIMITIVE) { + buf[(cur+1)->end] = '\0'; + *val = g_ascii_strtod(&buf[(cur+1)->start], NULL); + if (errno != 0) + return FALSE; + return TRUE; + } + cur = json_get_next_object(cur); + } + return FALSE; +} + gboolean json_decode_string_inplace(char *text) { diff --git a/wsutil/wsjson.h b/wsutil/wsjson.h index 197e82eccf..165de1e743 100644 --- a/wsutil/wsjson.h +++ b/wsutil/wsjson.h @@ -30,6 +30,25 @@ WS_DLL_PUBLIC gboolean json_validate(const guint8 *buf, const size_t len); WS_DLL_PUBLIC int json_parse(const char *buf, jsmntok_t *tokens, unsigned int max_tokens); /** + * Get the pointer to an object belonging to parent object and named as the name variable. + * Returns NULL if not found. + */ +WS_DLL_PUBLIC jsmntok_t *json_get_object(const char *buf, jsmntok_t *parent, const gchar* name); + +/** + * Get the unescaped value of a string object belonging to parent object and named as the name variable. + * Returns NULL if not found. Caution: it modifies input buffer. + */ +WS_DLL_PUBLIC char *json_get_string(char *buf, jsmntok_t *parent, const gchar* name); + +/** + * Get the value of a number object belonging to parent object and named as the name variable. + * Returns FALSE if not found. Caution: it modifies input buffer. + * Scientific notation not supported yet. + */ +WS_DLL_PUBLIC gboolean json_get_double(char *buf, jsmntok_t *parent, const gchar* name, gdouble *val); + +/** * Decode the contents of a JSON string value by overwriting the input data. * Returns TRUE on success and FALSE if invalid characters were encountered. */ |