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.
|
|
|
|
*
|
|
|
|
* PCRE is provided with libpcre (http://www.pcre.org/).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ftypes-int.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBPCRE
|
2005-10-31 02:42:22 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-12-09 23:02:40 +00:00
|
|
|
#include <pcre.h>
|
|
|
|
|
|
|
|
/* Create a pcre_tuple_t object based on the given string pattern */
|
|
|
|
static pcre_tuple_t *
|
|
|
|
pcre_tuple_new(const char *value)
|
|
|
|
{
|
|
|
|
pcre_tuple_t *tuple;
|
|
|
|
const char *pcre_error_text;
|
|
|
|
int pcre_error_offset;
|
|
|
|
|
2006-12-24 16:12:04 +00:00
|
|
|
tuple = g_malloc(sizeof(pcre_tuple_t));
|
|
|
|
tuple->string = g_strdup(value); /* The RE as string */
|
2006-02-23 21:22:54 +00:00
|
|
|
tuple->ex = NULL;
|
2006-12-24 16:12:04 +00:00
|
|
|
/* Compile the RE */
|
|
|
|
tuple->re = pcre_compile(
|
2003-12-09 23:02:40 +00:00
|
|
|
value, /* pattern */
|
|
|
|
0, /* PCRE options */
|
|
|
|
&pcre_error_text, /* PCRE constant error string */
|
|
|
|
&pcre_error_offset, /* Start offset of error in pattern */
|
|
|
|
NULL /* Default char tables (C locale) */
|
|
|
|
);
|
|
|
|
if (pcre_error_text) {
|
2006-12-24 16:12:04 +00:00
|
|
|
tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
|
2004-01-25 17:33:20 +00:00
|
|
|
"%s (character position %d)",
|
|
|
|
value, pcre_error_text, pcre_error_offset);
|
2003-12-09 23:02:40 +00:00
|
|
|
return tuple;
|
|
|
|
} else {
|
|
|
|
tuple->error = NULL;
|
|
|
|
}
|
2006-12-24 16:12:04 +00:00
|
|
|
/* Study the RE */
|
|
|
|
tuple->ex = pcre_study(tuple->re, 0, &pcre_error_text);
|
2003-12-09 23:02:40 +00:00
|
|
|
if (pcre_error_text) {
|
|
|
|
if (tuple->error) {
|
2006-12-24 16:12:04 +00:00
|
|
|
tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
|
2003-12-09 23:02:40 +00:00
|
|
|
"%s. %s",
|
2004-01-25 17:33:20 +00:00
|
|
|
value, tuple->error, pcre_error_text);
|
2003-12-09 23:02:40 +00:00
|
|
|
} else {
|
2006-12-24 16:12:04 +00:00
|
|
|
tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
|
2003-12-09 23:02:40 +00:00
|
|
|
"%s",
|
2004-01-25 17:33:20 +00:00
|
|
|
value, pcre_error_text);
|
2003-12-09 23:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
2006-12-24 16:12:04 +00:00
|
|
|
static void
|
|
|
|
pcre_tuple_free(pcre_tuple_t *tuple)
|
|
|
|
{
|
|
|
|
if (tuple) {
|
2009-03-13 22:06:48 +00:00
|
|
|
g_free(tuple->string);
|
|
|
|
g_free(tuple->re);
|
|
|
|
g_free(tuple->ex);
|
|
|
|
g_free(tuple->error);
|
2006-12-24 16:12:04 +00:00
|
|
|
g_free(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-09 23:02:40 +00:00
|
|
|
static void
|
|
|
|
pcre_fvalue_new(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
fv->value.re = NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-24 16:12:04 +00:00
|
|
|
static void
|
|
|
|
pcre_fvalue_free(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
if (fv->value.re) {
|
|
|
|
pcre_tuple_free(fv->value.re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-09 23:02:40 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
2006-12-24 16:12:04 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
pcre_fvalue_free(fv);
|
|
|
|
|
2003-12-09 23:02:40 +00:00
|
|
|
fv->value.re = pcre_tuple_new(pattern);
|
|
|
|
if (fv->value.re->error) {
|
|
|
|
logfunc(fv->value.re->error);
|
|
|
|
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)
|
|
|
|
{
|
2006-12-24 16:12:04 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
pcre_fvalue_free(fv);
|
2003-12-09 23:02:40 +00:00
|
|
|
g_assert(! allow_partial_value);
|
|
|
|
|
|
|
|
fv->value.re = pcre_tuple_new(pattern);
|
|
|
|
if (fv->value.re->error) {
|
|
|
|
logfunc(fv->value.re->error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:42:22 +00:00
|
|
|
static int
|
|
|
|
pcre_repr_len(fvalue_t *fv, ftrepr_t rtype)
|
|
|
|
{
|
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
2009-04-22 03:07:37 +00:00
|
|
|
return (int)strlen(fv->value.re->string);
|
2005-10-31 02:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pcre_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
|
|
|
|
{
|
|
|
|
g_assert(rtype == FTREPR_DFILTER);
|
|
|
|
strcpy(buf, fv->value.re->string);
|
|
|
|
}
|
|
|
|
|
2003-12-09 23:02:40 +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
|
|
|
|
pcre_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
|
|
|
{
|
|
|
|
g_assert(value != NULL);
|
2006-12-24 16:12:04 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
pcre_fvalue_free(fv);
|
2003-12-09 23:02:40 +00:00
|
|
|
g_assert(! already_copied);
|
|
|
|
fv->value.re = pcre_tuple_new(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
pcre_fvalue_get(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
return fv->value.re;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ftype_register_pcre(void)
|
|
|
|
{
|
|
|
|
static ftype_t pcre_type = {
|
2006-05-02 14:26:17 +00:00
|
|
|
FT_PCRE, /* ftype */
|
2004-08-22 00:31:58 +00:00
|
|
|
"FT_PCRE", /* name */
|
|
|
|
"Compiled Perl-Compatible Regular Expression object", /* pretty_name */
|
|
|
|
0, /* wire_size */
|
2003-12-09 23:02:40 +00:00
|
|
|
pcre_fvalue_new, /* new_value */
|
2006-12-24 16:12:04 +00:00
|
|
|
pcre_fvalue_free, /* free_value */
|
2003-12-09 23:02:40 +00:00
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
val_from_string, /* val_from_string */
|
2005-10-31 02:42:22 +00:00
|
|
|
pcre_to_repr, /* val_to_string_repr */
|
|
|
|
pcre_repr_len, /* len_string_repr */
|
2003-12-09 23:02:40 +00:00
|
|
|
|
|
|
|
pcre_fvalue_set, /* set_value */
|
2007-01-18 11:44:24 +00:00
|
|
|
NULL, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value_integer64 */
|
|
|
|
NULL, /* set_value_floating */
|
2003-12-09 23:02:40 +00:00
|
|
|
|
|
|
|
pcre_fvalue_get, /* get_value */
|
2007-01-18 11:44:24 +00:00
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
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 */
|
2003-12-09 23:02:40 +00:00
|
|
|
};
|
|
|
|
ftype_register(FT_PCRE, &pcre_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_LIBPCRE */
|
|
|
|
|
|
|
|
void
|
|
|
|
ftype_register_pcre(void)
|
|
|
|
{
|
|
|
|
static ftype_t pcre_type = {
|
2006-05-02 14:26:17 +00:00
|
|
|
FT_PCRE, /* ftype */
|
2004-08-22 00:31:58 +00:00
|
|
|
"FT_PCRE", /* name */
|
|
|
|
"Compiled Perl-Compatible Regular Expression object", /* pretty_name */
|
|
|
|
0, /* wire_size */
|
2003-12-09 23:02:40 +00:00
|
|
|
NULL, /* new_value */
|
|
|
|
NULL, /* free_value */
|
|
|
|
NULL, /* val_from_unparsed */
|
|
|
|
NULL, /* val_from_string */
|
|
|
|
NULL, /* val_to_string_repr */
|
|
|
|
NULL, /* len_string_repr */
|
|
|
|
|
|
|
|
NULL, /* set_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value_integer64 */
|
2003-12-09 23:02:40 +00:00
|
|
|
NULL, /* set_value_floating */
|
|
|
|
|
|
|
|
NULL, /* get_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value_integer64 */
|
2003-12-09 23:02:40 +00:00
|
|
|
NULL, /* get_value_floating */
|
|
|
|
|
|
|
|
NULL, /* cmp_eq */
|
|
|
|
NULL, /* cmp_ne */
|
|
|
|
NULL, /* cmp_gt */
|
|
|
|
NULL, /* cmp_ge */
|
|
|
|
NULL, /* cmp_lt */
|
|
|
|
NULL, /* cmp_le */
|
2004-02-27 12:00:32 +00:00
|
|
|
NULL, /* cmp_bitwise_and */
|
2003-12-09 23:02:40 +00:00
|
|
|
NULL, /* cmp_contains */
|
|
|
|
NULL, /* cmp_matches */
|
|
|
|
|
|
|
|
NULL, /* len */
|
|
|
|
NULL, /* slice */
|
|
|
|
};
|
|
|
|
ftype_register(FT_PCRE, &pcre_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBPCRE */
|