From ef182955849737098fadf0a05cb834be1cc7000b Mon Sep 17 00:00:00 2001 From: Janmejay Singh Date: Wed, 22 Oct 2014 10:31:39 +0530 Subject: [PATCH 1/2] Moved from parser receving "data" as escaped string to it receiving node(ln_fieldList_t), which will allow it to access either raw or escaped data. This will allow writing parsers that accept as many options as desired, in the same form as char-to/char-sep take the terminating char. --- src/parser.c | 8 +++++--- src/parser.h | 25 +++++++++++++------------ src/ptree.c | 10 ++++++---- src/ptree.h | 6 +++--- src/samp.c | 1 + 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/parser.c b/src/parser.c index e578dae..9d3879f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -73,11 +73,13 @@ hParseInt(const unsigned char **buf, size_t *lenBuf) * else in case of an error. */ #define BEGINParser(ParserName) \ -int ln_parse##ParserName(const char *str, size_t strLen, size_t *offs, \ - __attribute__((unused)) es_str_t *ed, size_t *parsed,\ - __attribute__((unused)) struct json_object **value) \ +int ln_parse##ParserName(const char *str, size_t strLen, size_t *offs, \ + __attribute__((unused)) const ln_fieldList_t *node, \ + size_t *parsed, \ + __attribute__((unused)) struct json_object **value) \ { \ int r = LN_WRONGPARSER; \ + __attribute__((unused)) es_str_t *ed = node->data; \ *parsed = 0; #define ENDParser \ diff --git a/src/parser.h b/src/parser.h index c8e91d4..cbec884 100644 --- a/src/parser.h +++ b/src/parser.h @@ -25,6 +25,7 @@ */ #ifndef LIBLOGNORM_PARSER_H_INCLUDED #define LIBLOGNORM_PARSER_H_INCLUDED +#include "ptree.h" /** * Parser interface. @@ -39,67 +40,67 @@ /** * Parser for RFC5424 date. */ -int ln_parseRFC5424Date(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseRFC5424Date(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parser for RFC3164 date. */ -int ln_parseRFC3164Date(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseRFC3164Date(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parser for numbers. */ -int ln_parseNumber(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseNumber(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parser for Words (SP-terminated strings). */ -int ln_parseWord(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseWord(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse everything up to a specific character. */ -int ln_parseCharTo(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseCharTo(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse everything up to a specific character (relaxed constraints, suitable for CSV) */ -int ln_parseCharSeparated(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseCharSeparated(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Get everything till the rest of string. */ -int ln_parseRest(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseRest(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse a quoted string. */ -int ln_parseQuotedString(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseQuotedString(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse an ISO date. */ -int ln_parseISODate(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseISODate(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse a timestamp in 12hr format. */ -int ln_parseTime12hr(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseTime12hr(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parse a timestamp in 24hr format. */ -int ln_parseTime24hr(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseTime24hr(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); /** * Parser for IPv4 addresses. */ -int ln_parseIPv4(const char *str, size_t strlen, size_t *offs, es_str_t *ed, size_t *parsed, struct json_object **value); +int ln_parseIPv4(const char *str, size_t strlen, size_t *offs, const ln_fieldList_t *node, size_t *parsed, struct json_object **value); #endif /* #ifndef LIBLOGNORM_PARSER_H_INCLUDED */ diff --git a/src/ptree.c b/src/ptree.c index 6e7b8f4..4a335eb 100644 --- a/src/ptree.c +++ b/src/ptree.c @@ -76,6 +76,8 @@ ln_deletePTreeNode(ln_fieldList_t *node) es_deleteStr(node->name); if(node->data != NULL) es_deleteStr(node->data); + if(node->raw_data != NULL) + es_deleteStr(node->raw_data); free(node); } @@ -373,9 +375,9 @@ ln_addFDescrToPTree(struct ln_ptree **tree, ln_fieldList_t *node) for(curr = (*tree)->froot ; curr != NULL ; curr = curr->next) { if(!es_strcmp(curr->name, node->name) && curr->parser == node->parser - && ((curr->data == NULL && node->data == NULL) - || (curr->data != NULL && node->data != NULL - && !es_strcmp(curr->data, node->data)))) { + && ((curr->raw_data == NULL && node->raw_data == NULL) + || (curr->raw_data != NULL && node->raw_data != NULL + && !es_strcmp(curr->raw_data, node->raw_data)))) { *tree = curr->subtree; ln_deletePTreeNode(node); r = 0; @@ -716,7 +718,7 @@ ln_normalizeRec(struct ln_ptree *tree, const char *str, size_t strLen, size_t of } } else { value = NULL; - localR = node->parser(str, strLen, &i, node->data, &parsed, &value); + localR = node->parser(str, strLen, &i, node, &parsed, &value); ln_dbgprintf(tree->ctx, "parser returns %d, parsed %zu", localR, parsed); if(localR == 0) { /* potential hit, need to verify */ diff --git a/src/ptree.h b/src/ptree.h index 01d9599..529b806 100644 --- a/src/ptree.h +++ b/src/ptree.h @@ -50,9 +50,9 @@ typedef struct ln_fieldList_s ln_fieldList_t; struct ln_fieldList_s { es_str_t *name; /**< field name */ es_str_t *data; /**< extra data to be passed to parser */ - int (*parser)(const char*, size_t, size_t*, es_str_t*, size_t*, - struct json_object **); - /**< parser to use */ + es_str_t *raw_data; /**< extra untouched (unescaping is not done) data availble to be used by parser */ + int (*parser)(const char*, size_t, size_t*, const ln_fieldList_t *, + size_t*, struct json_object **); /**< parser to use */ ln_ptree *subtree; /**< subtree to follow if parser succeeded */ ln_fieldList_t *next; /**< list housekeeping, next node (or NULL) */ unsigned char isIPTables; /**< special parser: iptables! */ diff --git a/src/samp.c b/src/samp.c index c935bdf..d368cb1 100644 --- a/src/samp.c +++ b/src/samp.c @@ -211,6 +211,7 @@ parseFieldDescr(ln_ctx ctx, struct ln_ptree **subtree, es_str_t *rule, } CHKR(es_addChar(&node->data, buf[i++])); } + node->raw_data = es_strdup(node->data); es_unescapeStr(node->data); if(ctx->debug) { cstr = es_str2cstr(node->data, NULL); -- 2.0.4