File: search.c

package info (click to toggle)
tcpxtract 1.0.1-18.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 744 kB
  • sloc: sh: 3,338; ansic: 1,922; yacc: 104; lex: 81; makefile: 21
file content (387 lines) | stat: -rw-r--r-- 13,139 bytes parent folder | download | duplicates (9)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* $Id$ */
/* Copyright (C) 2005 Nicholas Harbour
   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, 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.
*/

/* This file is part of
   Tcpxtract, a sniffer that extracts files based on headers
   by Nick Harbour
*/

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "search.h"
#include "conf.h"

static srch_node_t *new_srch_node(srch_nodetype_t);
static srch_node_t *add_simple(srch_node_t *, uint8_t, int, int, char *, unsigned long, spectype_t);
static srch_node_t *add_wildcard(srch_node_t *, int, int, char *, unsigned long, spectype_t);
static void update_search(srch_node_t *, srchptr_list_t **, srch_results_t **, uint8_t, int);
static void add_result(srch_results_t **, fileid_t *, spectype_t, int);

static size_t currlen;

srch_node_t *srch_machine;

void compile_srch(srch_node_t **srch_tree, int id, char *ext, unsigned long maxlen, char *spec, spectype_t type)
{
    int i = 0, speclen;
    srch_node_t *node_ptr;
    
    assert(srch_tree != NULL);
    assert(spec != NULL);
 
    speclen = strlen(spec);
    
    if (*srch_tree == NULL)
        *srch_tree = new_srch_node(TABLE);

    if (speclen == 0)
        return;

    currlen = 0;
    
    node_ptr = *srch_tree;

    while (i < speclen) {
        if (spec[i] == '\\') {
            if (i + 1 >= speclen)
                error("Dangling \'\\\' in file type specifier");
            switch (spec[++i]) {
            case '\\':
                node_ptr = add_simple(node_ptr, '\\', speclen - i, id, ext, maxlen, type);
                break;
            case 'x':
                if (i + 2 >= speclen)
                    error("Invalid hexadecimal code in file type specifier");
                else {
                    char c;
                    int ch;
                    char code[3] = {'\0'};
                    code[0] = spec[++i];
                    code[1] = spec[++i];
                    sscanf(code, "%02x", &ch);
                    c = (char) ch;
                    node_ptr = add_simple(node_ptr, c, speclen - i, id, ext, maxlen, type);
                }
                break;
            case 'n':
                node_ptr = add_simple(node_ptr, '\n', speclen - i, id, ext, maxlen, type);
                break;
            case 't':
                node_ptr = add_simple(node_ptr, '\t', speclen - i, id, ext, maxlen, type);
                break;
            case 'r':
                node_ptr = add_simple(node_ptr, '\r', speclen - i, id, ext, maxlen, type);
                break;
            case '0':
                node_ptr = add_simple(node_ptr, '\0', speclen - i, id, ext, maxlen, type);
                break;
            case '?':
                node_ptr = add_wildcard(node_ptr, speclen - i, id, ext, maxlen, type);
                break;
            default:
                error("Invalid escape character in file format specifier");
                break;
            }
        } else
            node_ptr = add_simple(node_ptr, spec[i], speclen - i, id, ext, maxlen, type);

        i++;
    }

    /* this assumes node_ptr is pointing to a COMPLETE node */
    node_ptr->data.fileid.len = currlen;
}

static srch_node_t *new_srch_node(srch_nodetype_t nodetype)
{
    srch_node_t *retval = ecalloc(sizeof (srch_node_t), 1);

    retval->nodetype = nodetype;
    return retval;
}

static srch_node_t *add_simple(srch_node_t *node, uint8_t c, int remaining, int id, char *ext, unsigned long maxlen, spectype_t type)
{
    srch_node_t *newnode;
    srch_node_t *retval;
    
    assert(node != NULL);

    currlen++;
    
    if (remaining == 1) {       /* if remaining is 1 then we need to point to a COMPLETE node */
        newnode = new_srch_node(COMPLETE);
        newnode->spectype = type;
        newnode->data.fileid.id = id;
        newnode->data.fileid.ext = ext;
        newnode->data.fileid.maxlen = maxlen;
        node->data.table[c] = newnode;
        retval = newnode;
    } else if (node->data.table[c] == NULL) {
        newnode = new_srch_node(TABLE);
        node->data.table[c] = newnode;
        retval = newnode;
    } else
        retval = node->data.table[c];

    return retval;
}

static srch_node_t *add_wildcard(srch_node_t *node, int remaining, int id, char *ext, unsigned long maxlen, spectype_t type)
{
    srch_node_t *newnode;
    int i;
    
    assert(node != NULL);

    currlen++;
    
    if (remaining == 1) {       /* if remaining is 1 then we need to point to a COMPLETE node */
        newnode = new_srch_node(COMPLETE);
        newnode->spectype = type;
        newnode->data.fileid.id = id;
        newnode->data.fileid.ext = ext;
        newnode->data.fileid.maxlen = maxlen;
        for (i = 0; i < 256; i++)
            if (node->data.table[i] == NULL)    /* a specific char trumps a wildcard */
                node->data.table[i] = newnode;  /* shhh, that indicates a slight "feature" */
        return newnode;
    } else {
        newnode = new_srch_node(TABLE);
        for (i = 0; i < 256; i++)
            if (node->data.table[i] == NULL)
                node->data.table[i] = newnode;
        return newnode;
    }
}

/* the overall search interface.  You call this bad boy and give it a
 * pointer to your data buffer (i.e. a packet) */
srch_results_t *search(srch_node_t *tree, srchptr_list_t **srchptr_list, uint8_t *buf, size_t len)
{
    srch_results_t *retval = NULL;
    int i;
    
    assert(tree != NULL);
    assert(srchptr_list != NULL);
    assert(buf != NULL);

    for (i = 0; i < len; i++)
        update_search(tree, srchptr_list, &retval, buf[i], i); 
        
    return retval;
}

static void add_srchptr(srchptr_list_t **srchptr_list, srch_node_t *node)
{
    srchptr_list_t *ptr, *ptr2;
    
    assert(srchptr_list != NULL);
    assert(node != NULL);

    ptr = ecalloc(1, sizeof (srchptr_list_t));
    ptr->next = *srchptr_list;
    if (ptr->next != NULL)
        ptr->next->prev = ptr;
    ptr->node = node;
    *srchptr_list = ptr;
    for (ptr2 = ptr->next; ptr2 != NULL && ptr2 != ptr; ptr2 = ptr2->next)
        ;
}

static void remv_srchptr(srchptr_list_t **srchptr_list, srchptr_list_t *sptr)
{
    assert(srchptr_list != NULL);
    assert(sptr != NULL);

    if (sptr->prev != NULL)
        sptr->prev->next = sptr->next;
    if (sptr->next != NULL)
        sptr->next->prev = sptr->prev;
    if (*srchptr_list == sptr)
        *srchptr_list = sptr->next;
    free(sptr);
}

/* I sincerely apologize for this function.  This is called once for every byte of
 * data so I don't want to waste cycles with layers and layers of function calls.
 * The end result is a long, complex and unmaintainable function that is quick */
/* The inner demon of the search mechanism.  this updates all state machine pointers
 * with the current character and fixes the search results list appropriately */
/* FIXME: perhaps make this inline for speed */
static void update_search(srch_node_t *tree, srchptr_list_t **srchptr_list, srch_results_t **results, uint8_t c, int offset)
{
    if (*srchptr_list != NULL) {        /* start by updating existing threads */
        srchptr_list_t *ptr;
        srchptr_list_t *nxt;
        for (ptr = *srchptr_list; ptr != NULL; ptr = nxt) {
            nxt = ptr->next;
            if (ptr->node->data.table[c] != NULL) {
                srch_node_t *node = ptr->node->data.table[c];
                switch (node->nodetype) {
                case TABLE:
                    ptr->node = node;
                    break;
                case COMPLETE:
                    add_result(results, &node->data.fileid, node->spectype, offset);
                    remv_srchptr(srchptr_list, ptr);
                    break;
                default:
                    error("Barf! Unknown node type");
                    break;
                }
            } else {
                remv_srchptr(srchptr_list, ptr);
            }
        }
    }

    /* now see if we want to start a new thread (i.e. a new potential match) */
    if (tree->data.table[c] != NULL) {
        srch_node_t *node = tree->data.table[c];

        switch (node->nodetype) {
        case TABLE:            /* this should be 99.99% of them */
            add_srchptr(srchptr_list, node);
            break;
        case COMPLETE:       /* In the unlikely event of a one byte header */
            /* note to all ideots: if you carve for a one byte header,
             * you deserve the enormous flood of files that will spew forth.
             */
            add_result(results, &node->data.fileid, node->spectype, offset);
            break;
        default:
            error("Barf! Unknown node type");
            break;     
        }
    }
}

static void update_search2(srch_node_t *tree, srchptr_list_t **srchptr_list, srch_results_t **results, uint8_t c, int offset)
{
    if (*srchptr_list != NULL) {        /* start by updating existing threads */
        srchptr_list_t *ptr;
        srchptr_list_t *nxt = NULL, *prv = NULL;
                
        for (ptr = *srchptr_list; ptr != NULL; prv = ptr, nxt = ptr->next, ptr = nxt) {
            if (ptr->node->data.table[c] != NULL) {
                srch_node_t *node = ptr->node->data.table[c];
                switch (node->nodetype) {
                case TABLE:
                    ptr->node = node;
                    break;
                case COMPLETE:
                    add_result(results, &node->data.fileid, node->spectype, offset);
                    
                    /* remove thread from list */
                    if (prv != NULL)
                        prv->next = nxt;
                    else
                        *srchptr_list = nxt;
                    if (nxt != NULL)
                        nxt->prev = prv;
                    free(ptr);
                    break;
                default:
                    error("Barf! Unknown node type");
                    break;
                }
            } else { /*remove thread from list */
                if (prv != NULL)
                    prv->next = nxt;
                else
                    *srchptr_list = nxt;
                if (nxt != NULL)
                    nxt->prev = prv;
                free(ptr);
            }
        }
    }

    /* now see if we want to start a new thread (i.e. a new potential match) */
    if (tree->data.table[c] != NULL) {
        srch_node_t *node = tree->data.table[c];
        srchptr_list_t *ptr;

        switch (node->nodetype) {
        case TABLE:            /* this should be 99.99% of them */
            if (*srchptr_list == NULL) {
                *srchptr_list = ecalloc(1, sizeof **srchptr_list);
                (*srchptr_list)->next = NULL;
                (*srchptr_list)->prev = NULL;
                ptr = *srchptr_list;
            } else {
                for (ptr = *srchptr_list; ptr->next != NULL; ptr = ptr->next)
                    ;
                ptr->next = emalloc(sizeof *ptr->next);
                ptr->next->prev = ptr;
                ptr = ptr->next;
                ptr->next = NULL;
            }
            ptr->node = node;
            break;
        case COMPLETE:       /* In the unlikely event of a one byte header */
            /* note to all ideots: if you carve for a one byte header,
             * you deserve the enormous flood of files that will spew forth.
             */
            add_result(results, &node->data.fileid, node->spectype, offset);
            break;
        default:
            error("Barf! Unknown node type");
            break;     
        }
    }
}

/* Add a result to a results list, allocating as needed */
static void add_result(srch_results_t **results, fileid_t *fileid, spectype_t spectype, int offset)
{
    srch_results_t **ptr, *prev = NULL;

    assert(results != NULL);

    /* find the last element in the list, for setting prev */
    for (ptr = results; *ptr != NULL && (*ptr)->next != NULL; ptr = &(*ptr)->next)
        ;
    if (*ptr != NULL) {
        prev = *ptr;
        ptr = &(*ptr)->next;
    }
        
    *ptr = emalloc(sizeof **ptr);
    (*ptr)->next = NULL;
    (*ptr)->prev = NULL;
    (*ptr)->fileid = fileid;
    (*ptr)->spectype = spectype;
    (*ptr)->offset.start = offset - (fileid->len - 1);
    (*ptr)->offset.end = offset;
}

void free_results_list(srch_results_t **results)
{
    srch_results_t *rptr, *nxt;

    assert(results != NULL);

    for (rptr = *results; rptr != NULL; rptr = nxt) {
        nxt = rptr->next;
        free(rptr);
    }
    *results = NULL;
}