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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
|
/*
* $Id: re.c,v 1.5 2006/02/10 18:55:38 miconda Exp $
*
* regexp and regexp substitutions implementations
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser 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
*
* openser 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
*
*
* History:
* --------
* 2003-08-04 created by andrei
* 2004-11-12 minor api extension, added *count (andrei)
*/
#include "dprint.h"
#include "mem/mem.h"
#include "re.h"
#include <string.h>
void subst_expr_free(struct subst_expr* se)
{
if (se->replacement.s) pkg_free(se->replacement.s);
if (se->re) { regfree(se->re); pkg_free(se->re); };
pkg_free(se);
}
/* frees the entire list, head (l) too */
void replace_lst_free(struct replace_lst* l)
{
struct replace_lst* t;
while (l){
t=l;
l=l->next;
if (t->rpl.s) pkg_free(t->rpl.s);
pkg_free(t);
}
}
/* parse a /regular expression/replacement/flags into a subst_expr structure */
struct subst_expr* subst_parser(str* subst)
{
#define MAX_REPLACE_WITH 100
char c;
char* end;
char* p;
char* p0;
char* re;
char* re_end;
char* repl;
char* repl_end;
struct replace_with rw[MAX_REPLACE_WITH];
int rw_no;
int escape;
int cflags; /* regcomp flags */
int replace_all;
struct subst_expr* se;
regex_t* regex;
int max_pmatch;
int r;
/* init */
se=0;
regex=0;
cflags=REG_EXTENDED | REG_NEWLINE; /* don't match newline */
replace_all=0;
if (subst->len<3){
LOG(L_ERR, "ERROR: subst_parser: expression is too short: %.*s\n",
subst->len, subst->s);
goto error;
}
p=subst->s;
c=*p;
if (c=='\\'){
LOG(L_ERR, "ERROR: subst_parser: invalid separator char <%c>"
" in %.*s\n", c, subst->len, subst->s);
goto error;
}
p++;
end=subst->s+subst->len;
/* find re */
re=p;
for (;p<end;p++){
/* if unescaped sep. char */
if ((*p==c) && (*(p-1)!='\\')) goto found_re;
}
LOG(L_ERR, "ERROR: subst_parser: no separator found: %.*s\n", subst->len,
subst->s);
goto error;
found_re:
re_end=p;
p++;
/* parse replacement */
repl=p;
rw_no=0;
max_pmatch=0;
escape=0;
for(;p<end; p++){
if (escape){
escape=0;
switch (*p){
/* special char escapes */
case '\\':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c='\\';
break;
case 'n':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c='\n';
break;
case 'r':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c='\r';
break;
case 't':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c='\t';
break;
case ITEM_MARKER:
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c=ITEM_MARKER;
break;
/* special sip msg parts escapes */
case 'u':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_URI;
break;
/* re matches */
case '0': /* allow 0, too, reference to the whole match */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_NMATCH;
rw[rw_no].u.nmatch=(*p)-'0';/* 0 is the whole matched str*/
if (max_pmatch<rw[rw_no].u.nmatch)
max_pmatch=rw[rw_no].u.nmatch;
break;
default: /* just print current char */
if (*p!=c){
LOG(L_WARN, "subst_parser: WARNING: \\%c unknown"
" escape in %.*s\n", *p, subst->len, subst->s);
}
rw[rw_no].size=2;
rw[rw_no].offset=(p-1)-repl;
rw[rw_no].type=REPLACE_CHAR;
rw[rw_no].u.c=*p;
break;
}
rw_no++;
if (rw_no>=MAX_REPLACE_WITH){
LOG(L_ERR, "ERROR: subst_parser: too many escapes in the"
" replace part %.*s\n", subst->len, subst->s);
goto error;
}
}else if (*p=='\\') escape=1;
else if (*p==ITEM_MARKER) {
p0 = xl_parse_spec(p, &rw[rw_no].u.spec,
XL_DISABLE_COLORS|XL_THROW_ERROR );
if(p0==NULL)
{
LOG(L_ERR, "ERROR: subst_parser: bad specifier in"
" replace part %.*s\n", subst->len, subst->s);
goto error;
}
rw[rw_no].size=p0-p;
rw[rw_no].offset=p-repl;
rw[rw_no].type=REPLACE_SPEC;
rw_no++;
p=p0-1;
}else if (*p==c) goto found_repl;
}
LOG(L_ERR, "ERROR: subst_parser: missing separator: %.*s\n", subst->len,
subst->s);
goto error;
found_repl:
repl_end=p;
p++;
/* parse flags */
for(;p<end; p++){
switch(*p){
case 'i':
cflags|=REG_ICASE;
break;
case 's':
cflags&=(~REG_NEWLINE);
break;
case 'g':
replace_all=1;
break;
default:
LOG(L_ERR, "ERROR: subst_parser: unknown flag %c in %.*s\n",
*p, subst->len, subst->s);
goto error;
}
}
/* compile the re */
if ((regex=pkg_malloc(sizeof(regex_t)))==0){
LOG(L_ERR, "ERROR: subst_parser: out of memory (re)\n");
goto error;
}
c=*re_end; /* regcomp expects null terminated strings -- save */
*re_end=0;
if (regcomp(regex, re, cflags)!=0){
pkg_free(regex);
*re_end=c; /* restore */
LOG(L_ERR, "ERROR: subst_parser: bad regular expression %.*s in "
"%.*s\n", (int)(re_end-re), re, subst->len, subst->s);
goto error;
}
*re_end=c; /* restore */
/* construct the subst_expr structure */
se=pkg_malloc(sizeof(struct subst_expr)+
((rw_no)?(rw_no-1)*sizeof(struct replace_with):0));
/* 1 replace_with structure is already included in subst_expr */
if (se==0){
LOG(L_ERR, "ERROR: subst_parser: out of memory (subst_expr)\n");
goto error;
}
memset((void*)se, 0, sizeof(struct subst_expr));
se->replacement.len=repl_end-repl;
if ((se->replacement.s=pkg_malloc(se->replacement.len))==0){
LOG(L_ERR, "ERROR: subst_parser: out of memory (replacement)\n");
goto error;
}
/* start copying */
memcpy(se->replacement.s, repl, se->replacement.len);
se->re=regex;
se->replace_all=replace_all;
se->n_escapes=rw_no;
se->max_pmatch=max_pmatch;
for (r=0; r<rw_no; r++) se->replace[r]=rw[r];
DBG("subst_parser: ok, se is %p\n", se);
return se;
error:
if (se) { subst_expr_free(se); regex=0; }
if (regex) { regfree (regex); pkg_free(regex); }
return 0;
}
#if 0
static int replace_len(const char* match, int nmatch, regmatch_t* pmatch,
struct subst_expr* se, struct sip_msg* msg)
{
int r;
int len;
str* uri;
len=se->replacement.len;
for (r=0; r<se->n_escapes; r++){
switch(se->replace[r].type){
case REPLACE_NMATCH:
len-=se->replace[r].size;
if ((se->replace[r].u.nmatch<nmatch)&&(
pmatch[se->replace[r].u.nmatch].rm_so!=-1)){
/* do the replace */
len+=pmatch[se->replace[r].u.nmatch].rm_eo-
pmatch[se->replace[r].u.nmatch].rm_so;
};
break;
case REPLACE_CHAR:
len-=(se->replace[r].size-1);
break;
case REPLACE_URI:
len-=se->replace[r].size;
if (msg->first_line.type!=SIP_REQUEST){
LOG(L_CRIT, "BUG: replace_len: uri substitution on"
" a reply\n");
break; /* ignore, we can continue */
}
uri= (msg->new_uri.s)?(&msg->new_uri):
(&msg->first_line.u.request.uri);
len+=uri->len;
break;
default:
LOG(L_CRIT, "BUG: replace_len: unknown type %d\n",
se->replace[r].type);
/* ignore it */
}
}
return len;
}
#endif
/* rpl.s will be alloc'ed with the proper size & rpl.len set
* returns 0 on success, <0 on error*/
static int replace_build(const char* match, int nmatch, regmatch_t* pmatch,
struct subst_expr* se, struct sip_msg* msg, str* rpl)
{
int r;
str* uri;
xl_value_t sv;
char* p;
char* dest;
char* end;
int size;
#define REPLACE_BUFFER_SIZE 1024
static char rbuf[REPLACE_BUFFER_SIZE];
#if 0
/* use static bufer now since we cannot easily get the length */
rpl->len=replace_len(match, nmatch, pmatch, se, msg);
if (rpl->len==0){
rpl->s=0; /* empty string */
return 0;
}
rpl->s=pkg_malloc(rpl->len);
if (rpl->s==0){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
#endif
p=se->replacement.s;
end=p+se->replacement.len;
dest=rbuf;
for (r=0; r<se->n_escapes; r++){
/* copy the unescaped parts */
size=se->replacement.s+se->replace[r].offset-p;
if(dest-rbuf+size>=REPLACE_BUFFER_SIZE-1){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
memcpy(dest, p, size);
p+=size+se->replace[r].size;
dest+=size;
switch(se->replace[r].type){
case REPLACE_NMATCH:
if ((se->replace[r].u.nmatch<nmatch)&&(
pmatch[se->replace[r].u.nmatch].rm_so!=-1)){
/* do the replace */
size=pmatch[se->replace[r].u.nmatch].rm_eo-
pmatch[se->replace[r].u.nmatch].rm_so;
if(dest-rbuf+size>=REPLACE_BUFFER_SIZE-1){
LOG(L_ERR,
"ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
memcpy(dest,
match+pmatch[se->replace[r].u.nmatch].rm_so,
size);
dest+=size;
};
break;
case REPLACE_CHAR:
if(dest-rbuf+1>=REPLACE_BUFFER_SIZE-1){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
*dest=se->replace[r].u.c;
dest++;
break;
case REPLACE_URI:
if (msg->first_line.type!=SIP_REQUEST){
LOG(L_CRIT, "BUG: replace_build: uri substitution on"
" a reply\n");
break; /* ignore, we can continue */
}
uri= (msg->new_uri.s)?(&msg->new_uri):
(&msg->first_line.u.request.uri);
if(dest-rbuf+uri->len>=REPLACE_BUFFER_SIZE-1){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
memcpy(dest, uri->s, uri->len);
dest+=uri->len;
break;
case REPLACE_SPEC:
if(xl_get_spec_value(msg, &se->replace[r].u.spec, &sv, 0)!=0)
{
LOG(L_CRIT, "BUG: replace_build: item substitution"
" returned error\n");
break; /* ignore, we can continue */
}
if(dest-rbuf+sv.rs.len>=REPLACE_BUFFER_SIZE-1){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
memcpy(dest, sv.rs.s, sv.rs.len);
dest+=sv.rs.len;
break;
default:
LOG(L_CRIT, "BUG: replace_build: unknown type %d\n",
se->replace[r].type);
/* ignore it */
}
}
memcpy(dest, p, end-p);
rpl->len = (dest-rbuf)+(end-p);
rpl->s=pkg_malloc(rpl->len);
if (rpl->s==0){
LOG(L_ERR, "ERROR: replace_build: out of mem (rpl)\n");
goto error;
}
memcpy(rpl->s, rbuf, rpl->len);
return 0;
error:
return -1;
}
/* WARNING: input must be 0 terminated! */
/* returns: 0 if no match or error, or subst result; if count!=0
* it will be set to 0 (no match), the number of matches
* or -1 (error).
*/
struct replace_lst* subst_run(struct subst_expr* se, const char* input,
struct sip_msg* msg, int* count)
{
struct replace_lst *head;
struct replace_lst **crt;
const char *p;
int r;
regmatch_t* pmatch;
int nmatch;
int eflags;
int cnt;
/* init */
head=0;
cnt=0;
crt=&head;
p=input;
nmatch=se->max_pmatch+1;
/* no of () referenced + 1 for the whole string: pmatch[0] */
pmatch=pkg_malloc(nmatch*sizeof(regmatch_t));
if (pmatch==0){
LOG(L_ERR, "ERROR: subst_run_ out of mem. (pmatch)\n");
goto error;
}
eflags=0;
do{
r=regexec(se->re, p, nmatch, pmatch, eflags);
DBG("subst_run: running. r=%d\n", r);
/* subst */
if (r==0){ /* != REG_NOMATCH */
/* change eflags, not to match any more at string start */
eflags|=REG_NOTBOL;
*crt=pkg_malloc(sizeof(struct replace_lst));
if (*crt==0){
LOG(L_ERR, "ERROR: subst_run: out of mem (crt)\n");
goto error;
}
memset(*crt, 0, sizeof(struct replace_lst));
if (pmatch[0].rm_so==-1){
LOG(L_ERR, "ERROR: subst_run: unknown offset?\n");
goto error;
}
(*crt)->offset=pmatch[0].rm_so+(int)(p-input);
(*crt)->size=pmatch[0].rm_eo-pmatch[0].rm_so;
DBG("subst_run: matched (%d, %d): [%.*s]\n",
(*crt)->offset, (*crt)->size,
(*crt)->size, input+(*crt)->offset);
/* create subst. string */
/* construct the string from replace[] */
if (replace_build(p, nmatch, pmatch, se, msg, &((*crt)->rpl))<0){
goto error;
}
crt=&((*crt)->next);
p+=pmatch[0].rm_eo;
cnt++;
}
}while((r==0) && se->replace_all);
pkg_free(pmatch);
if (count)*count=cnt;
return head;
error:
if (head) replace_lst_free(head);
if (pmatch) pkg_free(pmatch);
if (count) *count=-1;
return 0;
}
/* returns the substitution result in a str, input must be 0 term
* 0 on no match or malloc error
* if count is non zero it will be set to the number of matches, or -1
* if error
*/
str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se,
int* count)
{
str* res;
struct replace_lst *lst;
struct replace_lst* l;
int len;
int size;
const char* p;
char* dest;
const char* end;
/* compute the len */
len=strlen(input);
end=input+len;
lst=subst_run(se, input, msg, count);
if (lst==0){
DBG("subst_str: no match\n");
return 0;
}
for (l=lst; l; l=l->next)
len+=(int)(l->rpl.len)-l->size;
res=pkg_malloc(sizeof(str));
if (res==0){
LOG(L_ERR, "ERROR: subst_str: mem. allocation error\n");
goto error;
}
res->s=pkg_malloc(len+1); /* space for null termination */
if (res->s==0){
LOG(L_ERR, "ERROR: subst_str: mem. allocation error (res->s)\n");
goto error;
}
res->s[len]=0;
res->len=len;
/* replace */
dest=res->s;
p=input;
for(l=lst; l; l=l->next){
size=l->offset+input-p;
memcpy(dest, p, size); /* copy till offset */
p+=size + l->size; /* skip l->size bytes */
dest+=size;
if (l->rpl.len){
memcpy(dest, l->rpl.s, l->rpl.len);
dest+=l->rpl.len;
}
}
memcpy(dest, p, end-p);
if(lst) replace_lst_free(lst);
return res;
error:
if (lst) replace_lst_free(lst);
if (res){
if (res->s) pkg_free(res->s);
pkg_free(res);
}
if (count) *count=-1;
return 0;
}
|