Skip to content

Commit

Permalink
Merge pull request #324 from bshifter/filterx-parse-cef
Browse files Browse the repository at this point in the history
Filterx parse cef/leef #1
  • Loading branch information
alltilla authored Oct 9, 2024
2 parents 9c24d7f + 3d16c1c commit fb32316
Show file tree
Hide file tree
Showing 19 changed files with 1,312 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/scanner/csv-scanner/csv-scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,23 @@ _parse_string_delimiters_at_current_position(CSVScanner *self)
static gboolean
_parse_character_delimiters_at_current_position(CSVScanner *self)
{
gboolean escaped = FALSE;
if (self->options->dialect == CSV_SCANNER_ESCAPE_UNQUOTED_DELIMITER &&
*self->src == '\\' &&
*(self->src + 1))
{
self->src++;
escaped = TRUE;
}
if (_strchr_optimized_for_single_char_haystack(self->options->delimiters, *self->src) != NULL)
{
if (escaped)
return FALSE;
self->src++;
return TRUE;
}
if (escaped)
self->src--;
return FALSE;
}

Expand Down
1 change: 1 addition & 0 deletions lib/scanner/csv-scanner/csv-scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef enum
CSV_SCANNER_ESCAPE_BACKSLASH,
CSV_SCANNER_ESCAPE_BACKSLASH_WITH_SEQUENCES,
CSV_SCANNER_ESCAPE_DOUBLE_CHAR,
CSV_SCANNER_ESCAPE_UNQUOTED_DELIMITER,
} CSVScannerDialect;

#define CSV_SCANNER_STRIP_WHITESPACE 0x0001
Expand Down
30 changes: 30 additions & 0 deletions lib/scanner/csv-scanner/tests/test_csv_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
*/
#include <criterion/criterion.h>
#include <stdio.h>

#include "scratch-buffers.h"
#include "apphook.h"
Expand Down Expand Up @@ -427,6 +428,7 @@ Test(csv_scanner, escape_backslash_x_sequences)
csv_scanner_deinit(&scanner);
}


Test(csv_scanner, escape_backslash_invalid_x_sequence)
{
_default_options_with_flags(2, CSV_SCANNER_STRIP_WHITESPACE);
Expand Down Expand Up @@ -476,6 +478,34 @@ Test(csv_scanner, columnless_no_flags)
csv_scanner_deinit(&scanner);
}

Test(csv_scanner, escaped_unquoted_delimiter)
{
_default_options_with_flags(3, CSV_SCANNER_STRIP_WHITESPACE);

csv_scanner_options_set_dialect(&options, CSV_SCANNER_ESCAPE_UNQUOTED_DELIMITER);
csv_scanner_options_set_delimiters(&options, "|");
csv_scanner_init(&scanner, &options, "first|foo\\|bar\\|ba\\z|last");

cr_expect(_column_index_equals(0));
cr_expect(!_scan_complete());

cr_expect(_scan_next());
cr_expect(_column_equals(0, "first"));
cr_expect(!_scan_complete());

cr_expect(_scan_next());
cr_expect(_column_equals(1, "foo|bar|ba\\z"));
cr_expect(!_scan_complete());

cr_expect(_scan_next());
cr_expect(_column_equals(2, "last"));
cr_expect(!_scan_complete());

cr_expect(!_scan_next());
cr_expect(_scan_complete());
csv_scanner_deinit(&scanner);
}

static void
setup(void)
{
Expand Down
7 changes: 7 additions & 0 deletions modules/cef/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ set(CEF_SOURCES
format-cef-extension.c
format-cef-extension.h
cef-plugin.c
event-format-parser.c
event-format-parser.h
event-format-parser-cfg.h
filterx-func-parse-cef.c
filterx-func-parse-cef.h
filterx-func-parse-leef.c
filterx-func-parse-leef.h
)

add_module(
Expand Down
7 changes: 7 additions & 0 deletions modules/cef/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ EXTRA_DIST += modules/cef/CMakeLists.txt
modules_cef_libcef_la_SOURCES = \
modules/cef/format-cef-extension.c \
modules/cef/format-cef-extension.h \
modules/cef/event-format-parser-cfg.h \
modules/cef/event-format-parser.c \
modules/cef/event-format-parser.h \
modules/cef/filterx-func-parse-cef.c \
modules/cef/filterx-func-parse-cef.h \
modules/cef/filterx-func-parse-leef.c \
modules/cef/filterx-func-parse-leef.h \
modules/cef/cef-plugin.c

modules_cef_libcef_la_CFLAGS = \
Expand Down
5 changes: 5 additions & 0 deletions modules/cef/cef-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
#include "format-cef-extension.h"
#include "plugin.h"
#include "plugin-types.h"
#include "filterx-func-parse-cef.h"
#include "filterx-func-parse-leef.h"
#include "filterx/expr-function.h"

static Plugin cef_plugins[] =
{
TEMPLATE_FUNCTION_PLUGIN(tf_cef, "format-cef-extension"),
FILTERX_GENERATOR_FUNCTION_PLUGIN(parse_cef),
FILTERX_GENERATOR_FUNCTION_PLUGIN(parse_leef),
};

gboolean
Expand Down
61 changes: 61 additions & 0 deletions modules/cef/event-format-parser-cfg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Axoflow
* Copyright (c) 2024 shifter
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef EVENT_FORMAT_PARSER_CFG_H_INCLUDED
#define EVENT_FORMAT_PARSER_CFG_H_INCLUDED

#include "filterx/filterx-object.h"

typedef struct _FilterXFunctionEventFormatParser FilterXFunctionEventFormatParser;

typedef FilterXObject *(*FieldParser)(FilterXFunctionEventFormatParser *parser, const gchar *value, gint value_len,
GError **error,
gpointer user_data);

typedef struct _Field
{
const gchar *name;
FieldParser field_parser;
} Field;

typedef struct _Header
{
const gchar *delimiters;
size_t num_fields;
Field *fields;
} Header;

typedef struct _Extensions
{
gchar value_separator;
const gchar *pair_separator;
} Extensions;

typedef struct _Config
{
const gchar *signature;
Header header;
Extensions extensions;
} Config;

#endif
Loading

0 comments on commit fb32316

Please sign in to comment.