aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dfilter/dfunctions.c
blob: f3edfafed449308f7ffcbcac7807422c98203abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * 
 * Copyright 2006 Gilbert Ramirez <gram@alumni.rice.edu>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>

#include "dfunctions.h"
#include "dfilter-int.h"

#include <string.h>
#include <ctype.h>

#include <ftypes/ftypes.h>
#include <epan/exceptions.h>
#include <epan/emem.h>

/* lowercase an ASCII character.
 * (thanks to Guy Harris for the function) */
static gchar
string_ascii_to_lower(gchar c)
{
    return ((c & 0x80) ? c : tolower(c));
}

/* uppercase an ASCII character. */
static gchar
string_ascii_to_upper(gchar c)
{
    return ((c & 0x80) ? c : toupper(c));
}


/* Convert an FT_STRING using a callback function */
static gboolean
string_walk(GList* arg1list, GList **retval, gchar(*conv_func)(gchar))
{
    GList       *arg1;
    fvalue_t    *arg_fvalue;
    fvalue_t    *new_ft_string;
    char *s, *c;

    arg1 = arg1list;
    while (arg1) {
        arg_fvalue = (fvalue_t *)arg1->data; 
        switch (fvalue_ftype(arg_fvalue)->ftype) {
            case FT_STRING:
            case FT_STRINGZ:
            case FT_UINT_STRING:
                s = (char *)ep_strdup(fvalue_get(arg_fvalue));
                for (c = s; *c; c++) {
                        /**c = string_ascii_to_lower(*c);*/
                        *c = conv_func(*c);
                }

                new_ft_string = fvalue_new(FT_STRING);
                fvalue_set(new_ft_string, s, FALSE);
                *retval = g_list_append(*retval, new_ft_string);
                break;

            /* XXX - it would be nice to handle FT_TVBUFF, too */

            default:
                break;
        } 
        arg1 = arg1->next;
    }

    return TRUE;
}

/* dfilter function: lower() */
static gboolean
df_func_lower(GList* arg1list, GList *arg2junk _U_, GList **retval)
{
    return string_walk(arg1list, retval, string_ascii_to_lower);
}

/* dfilter function: upper() */
static gboolean
df_func_upper(GList* arg1list, GList *arg2junk _U_, GList **retval)
{
    return string_walk(arg1list, retval, string_ascii_to_upper);
}

/* dfilter function: len() */
static gboolean
df_func_len(GList* arg1list, GList *arg2junk _U_, GList **retval)
{
    GList       *arg1;
    fvalue_t    *arg_fvalue;
    fvalue_t    *ft_len;

    arg1 = arg1list;
    while (arg1) {
        arg_fvalue = (fvalue_t *)arg1->data; 
        switch (fvalue_ftype(arg_fvalue)->ftype) {
            case FT_STRING:
            case FT_STRINGZ:
            case FT_UINT_STRING:
                ft_len = fvalue_new(FT_UINT32);
                fvalue_set_uinteger(ft_len, (guint) strlen(fvalue_get(arg_fvalue)));
                *retval = g_list_append(*retval, ft_len);
                break;

            /* XXX - it would be nice to handle other types */

            default:
                break;
        } 
        arg1 = arg1->next;
    }

    return TRUE;
}

/* For upper(), lower() and len(), checks that the parameter passed to
 * it is an FT_STRING */
static void
ul_semcheck_params(int param_num, stnode_t *st_node)
{
    sttype_id_t type;
    ftenum_t    ftype;
    header_field_info *hfinfo;

    type = stnode_type_id(st_node);

    if (param_num == 0) {
        switch(type) {
            case STTYPE_FIELD:
                hfinfo = (header_field_info *)stnode_data(st_node);
                ftype = hfinfo->type;
                if (ftype != FT_STRING && ftype != FT_STRINGZ
                        && ftype != FT_UINT_STRING) {
                    dfilter_fail("Only strings can be used in upper() or lower() or len()");
                    THROW(TypeError);
                }
                break;
            default:
                dfilter_fail("Only string-type fields can be used in upper() or lower() or len()");
                THROW(TypeError);
        }
    }
    else {
        g_assert_not_reached();
    }
}

/* The table of all display-filter functions */
static df_func_def_t
df_functions[] = {
    { "lower", df_func_lower, FT_STRING, 1, 1, ul_semcheck_params },
    { "upper", df_func_upper, FT_STRING, 1, 1, ul_semcheck_params },
    { "len", df_func_len, FT_UINT32, 1, 1, ul_semcheck_params },
    { NULL, NULL, 0, 0, 0, NULL }
};

/* Lookup a display filter function record by name */
df_func_def_t*
df_func_lookup(char *name)
{
    df_func_def_t *func_def;

    func_def = df_functions;
    while (func_def->function != NULL) {
        if (strcmp(func_def->name, name) == 0) {
            return func_def;
        }
        func_def++;
    }
    return NULL;
}