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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*
* Parse a regular expression, and extract a static suffix.
*
* Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2007-2008 Sourcefire, Inc.
*
* Authors: Török Edvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "clamav.h"
#include "others.h"
#include "jsparse/textbuf.h"
#include "regex_suffix.h"
#define MODULE "regex_suffix: "
enum node_type {
root=0,
concat,
alternate, /* | */
optional,/* ?, * */
leaf, /* a character */
leaf_class /* character class */
/* (x)+ is transformed into (x)*(x) */
};
struct node {
enum node_type type; /* must be first field */
struct node *parent;
union {
struct {
struct node* left;
struct node* right;
} children;
uint8_t* leaf_class_bitmap;
uint8_t leaf_char;
} u;
};
/* --- Prototypes --*/
static int build_suffixtree_descend(struct node *n, struct text_buffer *buf, suffix_callback cb, void *cbdata, struct regex_list *regex);
/* -----------------*/
static uint8_t dot_bitmap[32] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
static struct node* make_node(enum node_type type, struct node *left, struct node *right)
{
struct node *n;
if(type == concat) {
if(left == NULL)
return right;
if(right == NULL)
return left;
}
n = cli_malloc(sizeof(*n));
if(!n) {
cli_errmsg("make_node: Unable to allocate memory for new node\n");
return NULL;
}
n->type = type;
n->parent = NULL;
n->u.children.left = left;
n->u.children.right = right;
if(left)
left->parent = n;
if(right)
right->parent = n;
return n;
}
static struct node *dup_node(struct node *p)
{
struct node *node_left, *node_right;
struct node *d;
if(!p)
return NULL;
d = cli_malloc(sizeof(*d));
if(!d) {
cli_errmsg("dup_node: Unable to allocate memory for duplicate node\n");
return NULL;
}
d->type = p->type;
d->parent = NULL;
switch(p->type) {
case leaf:
d->u.leaf_char = p->u.leaf_char;
break;
case leaf_class:
d->u.leaf_class_bitmap = cli_malloc(32);
if(!d->u.leaf_class_bitmap) {
cli_errmsg("make_node: Unable to allocate memory for leaf class\n");
free(d);
return NULL;
}
memcpy(d->u.leaf_class_bitmap, p->u.leaf_class_bitmap, 32);
break;
default:
node_left = dup_node(p->u.children.left);
node_right = dup_node(p->u.children.right);
d->u.children.left = node_left;
d->u.children.right = node_right;
if(node_left)
node_left->parent = d;
if(node_right)
node_right->parent = d;
break;
}
return d;
}
static struct node *make_charclass(uint8_t *bitmap)
{
struct node *v = cli_malloc(sizeof(*v));
if(!v) {
cli_errmsg("make_charclass: Unable to allocate memory for character class\n");
return NULL;
}
v->type = leaf_class;
v->parent = NULL;
v->u.leaf_class_bitmap = bitmap;
return v;
}
static struct node *make_leaf(char c)
{
struct node *v = cli_malloc(sizeof(*v));
if(!v)
return NULL;
v->type = leaf;
v->parent = NULL;
v->u.leaf_char = c;
return v;
}
static void destroy_tree(struct node *n)
{
if(!n)
return;
switch(n->type) {
case concat:
case alternate:
case optional:
destroy_tree(n->u.children.left);
destroy_tree(n->u.children.right);
break;
case leaf_class:
if(n->u.leaf_class_bitmap != dot_bitmap)
free(n->u.leaf_class_bitmap);
break;
case root:
case leaf:
break;
}
free(n);
}
static uint8_t* parse_char_class(const char *pat, size_t *pos)
{
unsigned char range_start=0;
int hasprev = 0;
uint8_t* bitmap = cli_malloc(32);
if(!bitmap) {
cli_errmsg("parse_char_class: Unable to allocate memory for bitmap\n");
return NULL;
}
if (pat[*pos]=='^') {
memset(bitmap,0xFF,32);/*match chars not in brackets*/
++*pos;
}
else
memset(bitmap,0x00,32);
do {
/* literal ] can be first character, so test for it at the end of the loop, for example: []] */
if (pat[*pos]=='-' && hasprev) {
/* it is a range*/
unsigned char range_end;
unsigned int c;
assert(range_start);
++*pos;
if (pat[*pos]=='[')
if (pat[*pos+1]=='.') {
/* collating sequence not handled */
free(bitmap);
/* we are parsing the regex for a
* filter, be conservative and
* tell the filter that anything could
* match here */
while(pat[*pos] != ']') ++*pos;
++*pos;
while(pat[*pos] != ']') ++*pos;
return dot_bitmap;
}
else
range_end = pat[*pos];
else
range_end = pat[*pos];
for(c=range_start+1;c<=range_end;c++)
bitmap[c>>3] ^= 1<<(c&0x7);
hasprev = 0;
}
else if (pat[*pos]=='[' && pat[*pos]==':') {
/* char class */
free(bitmap);
while(pat[*pos] != ']') ++*pos;
++*pos;
while(pat[*pos] != ']') ++*pos;
return dot_bitmap;
} else {
bitmap[pat[*pos]>>3] ^= 1<<(pat[*pos]&0x7);
range_start = pat[*pos];
++*pos;
hasprev = 1;
}
} while(pat[*pos]!=']');
return bitmap;
}
static struct node* parse_regex(const char *p, size_t *last)
{
struct node *v = NULL;
struct node *right;
struct node *tmp;
while(p[*last] != '$' && p[*last] != '\0') {
switch(p[*last]) {
case '|':
++*last;
right = parse_regex(p, last);
v = make_node(alternate, v, right);
if(!v)
return NULL;
break;
case '*':
case '?':
v = make_node(optional, v, NULL);
if(!v)
return NULL;
++*last;
break;
case '+':
/* (x)* */
tmp = make_node(optional, v, NULL);
if(!tmp)
return NULL;
/* (x) */
right = dup_node(v);
if(!right)
return NULL;
/* (x)*(x) => (x)+ */
v = make_node(concat, tmp, right);
if(!v)
return NULL;
++*last;
break;
case '(':
++*last;
right = parse_regex(p, last);
if(!right)
return NULL;
++*last;
v = make_node(concat, v, right);
break;
case ')':
return v;
case '.':
right = make_charclass(dot_bitmap);
if(!right)
return NULL;
v = make_node(concat, v, right);
if(!v)
return NULL;
++*last;
break;
case '[':
++*last;
right = make_charclass( parse_char_class(p, last) );
if(!right)
return NULL;
v = make_node(concat, v, right);
if(!v)
return NULL;
++*last;
break;
case '\\':
/* next char is escaped, advance pointer
* and let fall-through handle it */
++*last;
default:
right = make_leaf(p[*last]);
v = make_node(concat, v, right);
if(!v)
return NULL;
++*last;
break;
}
}
return v;
}
#define BITMAP_HASSET(b, i) (b[i>>3] & (1<<(i&7)))
static int build_suffixtree_ascend(struct node *n, struct text_buffer *buf, struct node *prev, suffix_callback cb, void *cbdata, struct regex_list *regex)
{
size_t i, cnt;
while(n) {
struct node *q = n;
switch(n->type) {
case root:
textbuffer_putc(buf, '\0');
if(cb(cbdata, buf->data, buf->pos-1, regex) < 0)
return CL_EMEM;
return 0;
case leaf:
textbuffer_putc(buf, n->u.leaf_char);
n = n->parent;
break;
case leaf_class:
cnt = 0;
for(i=0;i<255;i++)
if (BITMAP_HASSET(n->u.leaf_class_bitmap, i))
cnt++;
if (cnt > 16) {
textbuffer_putc(buf, '\0');
if(cb(cbdata, buf->data, buf->pos-1, regex) < 0)
return CL_EMEM;
return 0;
}
/* handle small classes by expanding */
for(i=0;i<255;i++) {
if(BITMAP_HASSET(n->u.leaf_class_bitmap, i)) {
size_t pos;
pos = buf->pos;
textbuffer_putc(buf, i);
if(build_suffixtree_ascend(n->parent, buf, n, cb, cbdata, regex) < 0)
return CL_EMEM;
buf->pos = pos;
}
}
return 0;
case concat:
if(prev != n->u.children.left) {
if(build_suffixtree_descend(n->u.children.left, buf, cb, cbdata, regex) < 0)
return CL_EMEM;
/* we're done here, descend will call
* ascend if needed */
return 0;
} else {
n = n->parent;
}
break;
case alternate:
n = n->parent;
break;
case optional:
textbuffer_putc(buf, '\0');
if(cb(cbdata, buf->data, buf->pos-1, regex) < 0)
return CL_EMEM;
return 0;
}
prev = q;
}
return 0;
}
static int build_suffixtree_descend(struct node *n, struct text_buffer *buf, suffix_callback cb, void *cbdata, struct regex_list *regex)
{
size_t pos;
while(n && n->type == concat) {
n = n->u.children.right;
}
if(!n)
return 0;
/* find out end of the regular expression,
* if it ends with a static pattern */
switch(n->type) {
case alternate:
/* save pos as restart point */
pos = buf->pos;
if(build_suffixtree_descend(n->u.children.left, buf, cb, cbdata, regex) < 0)
return CL_EMEM;
buf->pos = pos;
if(build_suffixtree_descend(n->u.children.right, buf, cb, cbdata, regex) < 0)
return CL_EMEM;
buf->pos = pos;
break;
case optional:
textbuffer_putc(buf, '\0');
if(cb(cbdata, buf->data, buf->pos-1, regex) < 0)
return CL_EMEM;
return 0;
case leaf:
case leaf_class:
if(build_suffixtree_ascend(n, buf, NULL, cb, cbdata, regex) < 0)
return CL_EMEM;
return 0;
default:
break;
}
return 0;
}
int cli_regex2suffix(const char *pattern, regex_t *preg, suffix_callback cb, void *cbdata)
{
struct regex_list regex;
struct text_buffer buf;
struct node root_node;
struct node *n;
size_t last = 0;
int rc;
assert(pattern);
regex.preg = preg;
rc = cli_regcomp(regex.preg, pattern, REG_EXTENDED);
if(rc) {
size_t buflen = cli_regerror(rc, regex.preg, NULL, 0);
char *errbuf = cli_malloc(buflen);
if(errbuf) {
cli_regerror(rc, regex.preg, errbuf, buflen);
cli_errmsg(MODULE "Error compiling regular expression %s: %s\n", pattern, errbuf);
free(errbuf);
} else {
cli_errmsg(MODULE "Error compiling regular expression: %s\n", pattern);
}
return rc;
}
regex.nxt = NULL;
regex.pattern = cli_strdup(pattern);
n = parse_regex(pattern, &last);
if(!n)
return REG_ESPACE;
memset(&buf, 0, sizeof(buf));
memset(&root_node, 0, sizeof(buf));
n->parent = &root_node;
rc = build_suffixtree_descend(n, &buf, cb, cbdata, ®ex);
free(regex.pattern);
free(buf.data);
destroy_tree(n);
return rc;
}
|