2003-12-09 23:02:40 +00:00
|
|
|
/*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2003-12-09 23:02:40 +00:00
|
|
|
* Copyright 2001 Gerald Combs
|
|
|
|
*
|
2018-02-08 16:59:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2003-12-09 23:02:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Perl-Compatible Regular Expression (PCRE) internal field type.
|
|
|
|
* Used with the "matches" dfilter operator, allowing efficient
|
|
|
|
* compilation and studying of a PCRE pattern in dfilters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <ftypes-int.h>
|
|
|
|
|
2009-12-18 01:15:08 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
gregex_fvalue_new(fvalue_t *fv)
|
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
fv->value.re = NULL;
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gregex_fvalue_free(fvalue_t *fv)
|
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
if (fv->value.re) {
|
|
|
|
g_regex_unref(fv->value.re);
|
|
|
|
fv->value.re = NULL;
|
|
|
|
}
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a FT_PCRE from a parsed string pattern.
|
2015-01-18 10:22:19 +00:00
|
|
|
* On failure, if err_msg is non-null, set *err_msg to point to a
|
|
|
|
* g_malloc()ed error message. */
|
2009-12-18 01:15:08 +00:00
|
|
|
static gboolean
|
2015-01-18 10:22:19 +00:00
|
|
|
val_from_string(fvalue_t *fv, const char *pattern, gchar **err_msg)
|
2009-12-18 01:15:08 +00:00
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
GError *regex_error = NULL;
|
2017-06-21 21:34:30 +00:00
|
|
|
GRegexCompileFlags cflags = (GRegexCompileFlags)(G_REGEX_CASELESS | G_REGEX_OPTIMIZE);
|
2012-02-05 13:30:27 +00:00
|
|
|
|
2019-02-08 16:20:37 +00:00
|
|
|
/*
|
|
|
|
* As FT_BYTES and FT_PROTOCOL contain arbitrary binary data and FT_STRING
|
|
|
|
* is not guaranteed to contain valid UTF-8, we have to disable support for
|
|
|
|
* UTF-8 patterns and treat every pattern and subject as raw bytes.
|
|
|
|
*
|
|
|
|
* Should support for UTF-8 patterns be necessary, then we should compile a
|
|
|
|
* pattern without G_REGEX_RAW. Additionally, we MUST use g_utf8_validate()
|
|
|
|
* before calling g_regex_match_full() or risk crashes.
|
|
|
|
*/
|
|
|
|
cflags = (GRegexCompileFlags)(cflags | G_REGEX_RAW);
|
2012-02-05 13:30:27 +00:00
|
|
|
|
2011-09-21 16:41:14 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
gregex_fvalue_free(fv);
|
|
|
|
|
|
|
|
fv->value.re = g_regex_new(
|
|
|
|
pattern, /* pattern */
|
2012-02-05 13:30:27 +00:00
|
|
|
cflags, /* Compile options */
|
2013-03-19 19:02:25 +00:00
|
|
|
(GRegexMatchFlags)0, /* Match options */
|
2011-09-21 16:41:14 +00:00
|
|
|
®ex_error /* Compile / study errors */
|
|
|
|
);
|
|
|
|
|
|
|
|
if (regex_error) {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (err_msg) {
|
|
|
|
*err_msg = g_strdup(regex_error->message);
|
2011-09-21 16:41:14 +00:00
|
|
|
}
|
|
|
|
g_error_free(regex_error);
|
|
|
|
if (fv->value.re) {
|
|
|
|
g_regex_unref(fv->value.re);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a FT_PCRE from an unparsed string pattern.
|
2015-01-18 10:22:19 +00:00
|
|
|
* On failure, if err_msg is non-null, set *err_msg to point to a
|
|
|
|
* g_malloc()ed error message. */
|
2009-12-18 01:15:08 +00:00
|
|
|
static gboolean
|
2016-03-28 13:25:12 +00:00
|
|
|
val_from_unparsed(fvalue_t *fv, const char *pattern, gboolean allow_partial_value, gchar **err_msg)
|
2009-12-18 01:15:08 +00:00
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
g_assert(! allow_partial_value);
|
2009-12-18 01:15:08 +00:00
|
|
|
|
2015-01-18 10:22:19 +00:00
|
|
|
return val_from_string(fv, pattern, err_msg);
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-04 02:56:16 +00:00
|
|
|
gregex_repr_len(fvalue_t *fv, ftrepr_t rtype, int field_display _U_)
|
2009-12-18 01:15:08 +00:00
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
|
|
|
return (int)strlen(g_regex_get_pattern(fv->value.re));
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-10 20:37:44 +00:00
|
|
|
gregex_to_repr(fvalue_t *fv, ftrepr_t rtype, int field_display _U_, char *buf, unsigned int size)
|
2009-12-18 01:15:08 +00:00
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
2016-05-10 20:37:44 +00:00
|
|
|
g_strlcpy(buf, g_regex_get_pattern(fv->value.re), size);
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BEHOLD - value contains the string representation of the regular expression,
|
|
|
|
* and we want to store the compiled PCRE RE object into the value. */
|
|
|
|
static void
|
2014-01-15 01:05:03 +00:00
|
|
|
gregex_fvalue_set(fvalue_t *fv, const char *value)
|
2009-12-18 01:15:08 +00:00
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
g_assert(value != NULL);
|
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
gregex_fvalue_free(fv);
|
2014-01-15 01:05:03 +00:00
|
|
|
val_from_unparsed(fv, value, FALSE, NULL);
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gregex_fvalue_get(fvalue_t *fv)
|
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
return fv->value.re;
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ftype_register_pcre(void)
|
|
|
|
{
|
2011-09-21 16:41:14 +00:00
|
|
|
static ftype_t pcre_type = {
|
2015-06-11 01:59:31 +00:00
|
|
|
FT_PCRE, /* ftype */
|
|
|
|
"FT_PCRE", /* name */
|
2011-09-21 16:41:14 +00:00
|
|
|
"Compiled Perl-Compatible Regular Expression (GRegex) object", /* pretty_name */
|
2015-06-11 01:59:31 +00:00
|
|
|
0, /* wire_size */
|
2011-09-21 16:41:14 +00:00
|
|
|
gregex_fvalue_new, /* new_value */
|
|
|
|
gregex_fvalue_free, /* free_value */
|
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
val_from_string, /* val_from_string */
|
|
|
|
gregex_to_repr, /* val_to_string_repr */
|
|
|
|
gregex_repr_len, /* len_string_repr */
|
|
|
|
|
2017-03-09 21:56:07 +00:00
|
|
|
{ .set_value_string = gregex_fvalue_set }, /* union set_value */
|
2017-03-19 20:16:03 +00:00
|
|
|
{ .get_value_ptr = gregex_fvalue_get }, /* union get_value */
|
2011-09-21 16:41:14 +00:00
|
|
|
|
|
|
|
NULL, /* cmp_eq */
|
|
|
|
NULL, /* cmp_ne */
|
|
|
|
NULL, /* cmp_gt */
|
|
|
|
NULL, /* cmp_ge */
|
|
|
|
NULL, /* cmp_lt */
|
|
|
|
NULL, /* cmp_le */
|
|
|
|
NULL, /* cmp_bitwise_and */
|
|
|
|
NULL, /* cmp_contains */
|
|
|
|
NULL, /* cmp_matches */
|
|
|
|
|
|
|
|
NULL, /* len */
|
|
|
|
NULL, /* slice */
|
|
|
|
};
|
|
|
|
ftype_register(FT_PCRE, &pcre_type);
|
2009-12-18 01:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2009-12-18 01:15:08 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
2011-09-21 16:41:14 +00:00
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
2009-12-18 01:15:08 +00:00
|
|
|
* End:
|
|
|
|
*
|
2011-09-21 16:41:14 +00:00
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
2009-12-18 01:15:08 +00:00
|
|
|
*/
|