2003-12-09 23:02:40 +00:00
|
|
|
/*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
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
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
fv->value.re = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gregex_fvalue_free(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
if (fv->value.re) {
|
|
|
|
g_regex_unref(fv->value.re);
|
|
|
|
fv->value.re = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a FT_PCRE from a parsed string pattern.
|
|
|
|
* Uses the specified logfunc() to report errors. */
|
|
|
|
static gboolean
|
|
|
|
val_from_string(fvalue_t *fv, char *pattern, LogFunc logfunc)
|
|
|
|
{
|
|
|
|
GError *regex_error = NULL;
|
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
gregex_fvalue_free(fv);
|
|
|
|
|
|
|
|
fv->value.re = g_regex_new(
|
|
|
|
pattern, /* pattern */
|
2011-08-23 09:25:06 +00:00
|
|
|
G_REGEX_OPTIMIZE, /* Compile options */
|
2009-12-18 01:15:08 +00:00
|
|
|
0, /* Match options */
|
|
|
|
®ex_error /* Compile / study errors */
|
|
|
|
);
|
|
|
|
|
|
|
|
if (regex_error) {
|
|
|
|
if (logfunc) {
|
|
|
|
logfunc(regex_error->message);
|
|
|
|
}
|
|
|
|
g_error_free(regex_error);
|
|
|
|
if (fv->value.re) {
|
|
|
|
g_regex_unref(fv->value.re);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a FT_PCRE from an unparsed string pattern.
|
|
|
|
* Uses the specified logfunc() to report errors. */
|
|
|
|
static gboolean
|
|
|
|
val_from_unparsed(fvalue_t *fv, char *pattern, gboolean allow_partial_value _U_, LogFunc logfunc)
|
|
|
|
{
|
|
|
|
g_assert(! allow_partial_value);
|
|
|
|
|
|
|
|
return val_from_string(fv, pattern, logfunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gregex_repr_len(fvalue_t *fv, ftrepr_t rtype)
|
|
|
|
{
|
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
|
|
|
return (int)strlen(g_regex_get_pattern(fv->value.re));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gregex_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
|
|
|
|
{
|
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
|
|
|
strcpy(buf, g_regex_get_pattern(fv->value.re));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
gregex_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
|
|
|
{
|
|
|
|
g_assert(value != NULL);
|
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
gregex_fvalue_free(fv);
|
|
|
|
g_assert(! already_copied);
|
|
|
|
val_from_unparsed(fv, value, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gregex_fvalue_get(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
return fv->value.re;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ftype_register_pcre(void)
|
|
|
|
{
|
|
|
|
static ftype_t pcre_type = {
|
|
|
|
FT_PCRE, /* ftype */
|
|
|
|
"FT_PCRE", /* name */
|
|
|
|
"Compiled Perl-Compatible Regular Expression (GRegex) object", /* pretty_name */
|
|
|
|
0, /* wire_size */
|
|
|
|
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 */
|
|
|
|
|
|
|
|
gregex_fvalue_set, /* set_value */
|
|
|
|
NULL, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_sinteger */
|
|
|
|
NULL, /* set_value_integer64 */
|
|
|
|
NULL, /* set_value_floating */
|
|
|
|
|
|
|
|
gregex_fvalue_get, /* get_value */
|
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_sinteger */
|
|
|
|
NULL, /* get_value_integer64 */
|
|
|
|
NULL, /* get_value_floating */
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=4 noexpandtab
|
|
|
|
* :indentSize=4:tabSize=4:noTabs=false:
|
|
|
|
*/
|