re.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * $Id$
  3. *
  4. * regexp and regexp substitutions implementations
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. *
  30. * History:
  31. * --------
  32. * 2003-08-04 created by andrei
  33. * 2004-11-12 minor api extension, added *count (andrei)
  34. */
  35. /*!
  36. * \file
  37. * \brief SIP-router core :: regexp and regexp substitutions implementations
  38. * \ingroup core
  39. * Module: \ref core
  40. */
  41. #include "dprint.h"
  42. #include "mem/mem.h"
  43. #include "re.h"
  44. #include <string.h>
  45. #define MAX_REPLACE_WITH 100
  46. #define REPLACE_BUFFER_SIZE 1024
  47. void subst_expr_free(struct subst_expr* se)
  48. {
  49. if (se->replacement.s) pkg_free(se->replacement.s);
  50. if (se->re) { regfree(se->re); pkg_free(se->re); };
  51. pkg_free(se);
  52. }
  53. /* frees the entire list, head (l) too */
  54. void replace_lst_free(struct replace_lst* l)
  55. {
  56. struct replace_lst* t;
  57. while (l){
  58. t=l;
  59. l=l->next;
  60. if (t->rpl.s) pkg_free(t->rpl.s);
  61. pkg_free(t);
  62. }
  63. }
  64. int parse_repl(struct replace_with * rw, char ** begin,
  65. char * end, int *max_token_nb, int with_sep)
  66. {
  67. char* p0;
  68. char * repl;
  69. str s;
  70. int token_nb;
  71. int escape;
  72. int max_pmatch;
  73. char *p, c;
  74. /* parse replacement */
  75. p = *begin;
  76. c = *p;
  77. if(with_sep)
  78. p++;
  79. repl= p;
  80. token_nb=0;
  81. max_pmatch=0;
  82. escape=0;
  83. for(;p<end; p++){
  84. if (escape){
  85. escape=0;
  86. switch (*p){
  87. /* special char escapes */
  88. case '\\':
  89. rw[token_nb].size=2;
  90. rw[token_nb].offset=(p-1)-repl;
  91. rw[token_nb].type=REPLACE_CHAR;
  92. rw[token_nb].u.c='\\';
  93. break;
  94. case 'n':
  95. rw[token_nb].size=2;
  96. rw[token_nb].offset=(p-1)-repl;
  97. rw[token_nb].type=REPLACE_CHAR;
  98. rw[token_nb].u.c='\n';
  99. break;
  100. case 'r':
  101. rw[token_nb].size=2;
  102. rw[token_nb].offset=(p-1)-repl;
  103. rw[token_nb].type=REPLACE_CHAR;
  104. rw[token_nb].u.c='\r';
  105. break;
  106. case 't':
  107. rw[token_nb].size=2;
  108. rw[token_nb].offset=(p-1)-repl;
  109. rw[token_nb].type=REPLACE_CHAR;
  110. rw[token_nb].u.c='\t';
  111. break;
  112. case PV_MARKER:
  113. rw[token_nb].size=2;
  114. rw[token_nb].offset=(p-1)-repl;
  115. rw[token_nb].type=REPLACE_CHAR;
  116. rw[token_nb].u.c=PV_MARKER;
  117. break;
  118. /* special sip msg parts escapes */
  119. case 'u':
  120. rw[token_nb].size=2;
  121. rw[token_nb].offset=(p-1)-repl;
  122. rw[token_nb].type=REPLACE_URI;
  123. break;
  124. /* re matches */
  125. case '0': /* allow 0, too, reference to the whole match */
  126. case '1':
  127. case '2':
  128. case '3':
  129. case '4':
  130. case '5':
  131. case '6':
  132. case '7':
  133. case '8':
  134. case '9':
  135. rw[token_nb].size=2;
  136. rw[token_nb].offset=(p-1)-repl;
  137. rw[token_nb].type=REPLACE_NMATCH;
  138. rw[token_nb].u.nmatch=(*p)-'0';
  139. /* 0 is the whole matched str*/
  140. if (max_pmatch<rw[token_nb].u.nmatch)
  141. max_pmatch=rw[token_nb].u.nmatch;
  142. break;
  143. default: /* just print current char */
  144. if (*p!=c){
  145. WARN("subst_parser:\\%c unknown escape in %s\n", *p, *begin);
  146. }
  147. rw[token_nb].size=2;
  148. rw[token_nb].offset=(p-1)-repl;
  149. rw[token_nb].type=REPLACE_CHAR;
  150. rw[token_nb].u.c=*p;
  151. break;
  152. }
  153. token_nb++;
  154. if (token_nb>=MAX_REPLACE_WITH){
  155. ERR("subst_parser: too many escapes in the replace part %s\n", *begin);
  156. goto error;
  157. }
  158. }else if (*p=='\\') {
  159. escape=1;
  160. }else if (*p==PV_MARKER) {
  161. s.s = p;
  162. s.len = end - s.s;
  163. p0 = pv_parse_spec(&s, &rw[token_nb].u.spec);
  164. if(p0==NULL)
  165. {
  166. ERR("subst_parser: bad specifier in replace part %s\n", *begin);
  167. goto error;
  168. }
  169. rw[token_nb].size=p0-p;
  170. rw[token_nb].offset=p-repl;
  171. rw[token_nb].type=REPLACE_SPEC;
  172. token_nb++;
  173. p=p0-1;
  174. }else if (*p==c && with_sep){
  175. goto found_repl;
  176. }
  177. }
  178. if(with_sep){
  179. ERR("subst_parser: missing separator: %s\n", *begin);
  180. goto error;
  181. }
  182. found_repl:
  183. *max_token_nb = max_pmatch;
  184. *begin = p;
  185. return token_nb;
  186. error:
  187. return -1;
  188. }
  189. /* parse a /regular expression/replacement/flags into a subst_expr structure */
  190. struct subst_expr* subst_parser(str* subst)
  191. {
  192. char c;
  193. char* end;
  194. char* p;
  195. char* re;
  196. char* re_end;
  197. char* repl;
  198. char* repl_end;
  199. struct replace_with rw[MAX_REPLACE_WITH];
  200. int rw_no;
  201. int cflags; /* regcomp flags */
  202. int replace_all;
  203. struct subst_expr* se;
  204. regex_t* regex;
  205. int max_pmatch;
  206. int r;
  207. /* init */
  208. se=0;
  209. regex=0;
  210. cflags=REG_EXTENDED | REG_NEWLINE; /* don't match newline */
  211. replace_all=0;
  212. if (subst->len<3){
  213. LM_ERR("expression is too short: %.*s\n", subst->len, subst->s);
  214. goto error;
  215. }
  216. p=subst->s;
  217. c=*p;
  218. if (c=='\\'){
  219. LM_ERR("invalid separator char <%c> in %.*s\n", c, subst->len, subst->s);
  220. goto error;
  221. }
  222. p++;
  223. end=subst->s+subst->len;
  224. /* find re */
  225. re=p;
  226. for (;p<end;p++){
  227. /* if unescaped sep. char */
  228. if ((*p==c) && (*(p-1)!='\\')) goto found_re;
  229. }
  230. LM_ERR("no separator found: %.*s\n", subst->len, subst->s);
  231. goto error;
  232. found_re:
  233. re_end=p;
  234. if (end < (p + 2)) {
  235. LM_ERR("String too short\n");
  236. goto error;
  237. }
  238. repl=p+1;
  239. if ((rw_no = parse_repl(rw, &p, end, &max_pmatch, WITH_SEP)) < 0)
  240. goto error;
  241. repl_end = p;
  242. p++;
  243. /* parse flags */
  244. for(;p<end; p++){
  245. switch(*p){
  246. case 'i':
  247. cflags|=REG_ICASE;
  248. break;
  249. case 's':
  250. cflags&=(~REG_NEWLINE);
  251. break;
  252. case 'g':
  253. replace_all=1;
  254. break;
  255. default:
  256. LM_ERR("unknown flag %c in %.*s\n", *p, subst->len, subst->s);
  257. goto error;
  258. }
  259. }
  260. /* compile the re */
  261. if ((regex=pkg_malloc(sizeof(regex_t)))==0){
  262. LM_ERR("out of memory\n");
  263. goto error;
  264. }
  265. c=*re_end; /* regcomp expects null terminated strings -- save */
  266. *re_end=0;
  267. if (regcomp(regex, re, cflags)!=0){
  268. pkg_free(regex);
  269. regex=0;
  270. *re_end=c; /* restore */
  271. LM_ERR("bad regular expression %.*s in %.*s\n",
  272. (int)(re_end-re), re, subst->len, subst->s);
  273. goto error;
  274. }
  275. *re_end=c; /* restore */
  276. /* construct the subst_expr structure */
  277. se=pkg_malloc(sizeof(struct subst_expr)+
  278. ((rw_no)?(rw_no-1)*sizeof(struct replace_with):0));
  279. /* 1 replace_with structure is already included in subst_expr */
  280. if (se==0){
  281. LM_ERR("out of memory\n");
  282. goto error;
  283. }
  284. memset((void*)se, 0, sizeof(struct subst_expr));
  285. se->re=regex;
  286. se->replacement.len=repl_end-repl;
  287. if (se->replacement.len > 0) {
  288. if ((se->replacement.s=pkg_malloc(se->replacement.len))==0){
  289. LM_ERR("out of memory\n");
  290. goto error;
  291. }
  292. /* start copying */
  293. memcpy(se->replacement.s, repl, se->replacement.len);
  294. } else {
  295. se->replacement.s = NULL;
  296. }
  297. se->replace_all=replace_all;
  298. se->n_escapes=rw_no;
  299. se->max_pmatch=max_pmatch;
  300. for (r=0; r<rw_no; r++) se->replace[r]=rw[r];
  301. DBG("subst_parser: ok, se is %p\n", se);
  302. return se;
  303. error:
  304. if (se) { subst_expr_free(se); regex=0; }
  305. if (regex) { regfree (regex); pkg_free(regex); }
  306. return 0;
  307. }
  308. /* rpl.s will be alloc'ed with the proper size & rpl.len set
  309. * returns 0 on success, <0 on error*/
  310. static int replace_build(const char* match, int nmatch, regmatch_t* pmatch,
  311. struct subst_expr* se, struct sip_msg* msg, str* rpl)
  312. {
  313. int r;
  314. str* uri;
  315. pv_value_t sv;
  316. char* p;
  317. char* dest;
  318. char* end;
  319. int size;
  320. static char rbuf[REPLACE_BUFFER_SIZE];
  321. #define RBUF_APPEND(dst, src, size) \
  322. if ((dst) - rbuf + (size) >= REPLACE_BUFFER_SIZE - 1) { \
  323. LM_ERR("Buffer too small\n"); \
  324. goto error; \
  325. } \
  326. memcpy((dst), (src), (size)); \
  327. (dst) += (size);
  328. p=se->replacement.s;
  329. end=p+se->replacement.len;
  330. dest=rbuf;
  331. for (r=0; r<se->n_escapes; r++){
  332. /* copy the unescaped parts */
  333. size=se->replacement.s+se->replace[r].offset-p;
  334. RBUF_APPEND(dest, p, size);
  335. p+=size+se->replace[r].size;
  336. switch(se->replace[r].type){
  337. case REPLACE_NMATCH:
  338. if ((se->replace[r].u.nmatch<nmatch)&&(
  339. pmatch[se->replace[r].u.nmatch].rm_so!=-1)){
  340. /* do the replace */
  341. size=pmatch[se->replace[r].u.nmatch].rm_eo-
  342. pmatch[se->replace[r].u.nmatch].rm_so;
  343. RBUF_APPEND(dest,
  344. match+pmatch[se->replace[r].u.nmatch].rm_so,
  345. size);
  346. };
  347. break;
  348. case REPLACE_CHAR:
  349. RBUF_APPEND(dest, &se->replace[r].u.c, 1);
  350. break;
  351. case REPLACE_URI:
  352. if (msg->first_line.type!=SIP_REQUEST){
  353. LM_CRIT("uri substitution on a reply\n");
  354. break; /* ignore, we can continue */
  355. }
  356. uri= (msg->new_uri.s)?(&msg->new_uri):
  357. (&msg->first_line.u.request.uri);
  358. RBUF_APPEND(dest, uri->s, uri->len);
  359. break;
  360. case REPLACE_SPEC:
  361. if(pv_get_spec_value(msg, &se->replace[r].u.spec, &sv)!=0) {
  362. LM_ERR("item substitution returned error\n");
  363. break; /* ignore, we can continue */
  364. }
  365. RBUF_APPEND(dest, sv.rs.s, sv.rs.len);
  366. break;
  367. default:
  368. LM_CRIT("unknown type %d\n", se->replace[r].type);
  369. /* ignore it */
  370. }
  371. }
  372. RBUF_APPEND(dest, p, end-p);
  373. rpl->len = dest - rbuf;
  374. if ((rpl->s = pkg_malloc(rpl->len)) == NULL) {
  375. LM_ERR("Out of pkg memory\n");
  376. goto error;
  377. }
  378. memcpy(rpl->s, rbuf, rpl->len);
  379. return 0;
  380. error:
  381. return -1;
  382. }
  383. /* WARNING: input must be 0 terminated! */
  384. /* returns: 0 if no match or error, or subst result; if count!=0
  385. * it will be set to 0 (no match), the number of matches
  386. * or -1 (error).
  387. */
  388. struct replace_lst* subst_run(struct subst_expr* se, const char* input,
  389. struct sip_msg* msg, int* count)
  390. {
  391. struct replace_lst *head;
  392. struct replace_lst **crt;
  393. const char *p;
  394. int r;
  395. regmatch_t* pmatch;
  396. int nmatch;
  397. int eflags;
  398. int cnt;
  399. /* init */
  400. head=0;
  401. cnt=0;
  402. crt=&head;
  403. p=input;
  404. nmatch=se->max_pmatch+1;
  405. /* no of () referenced + 1 for the whole string: pmatch[0] */
  406. pmatch=pkg_malloc(nmatch*sizeof(regmatch_t));
  407. if (pmatch==0){
  408. LM_ERR("out of mem\n");
  409. goto error;
  410. }
  411. eflags=0;
  412. do{
  413. r=regexec(se->re, p, nmatch, pmatch, eflags);
  414. DBG("subst_run: running. r=%d\n", r);
  415. /* subst */
  416. if (r==0){ /* != REG_NOMATCH */
  417. if (pmatch[0].rm_so==-1) {
  418. LM_ERR("Unknown offset?\n");
  419. goto error;
  420. }
  421. if (pmatch[0].rm_so==pmatch[0].rm_eo) {
  422. LM_ERR("Matched string is empty, invalid regexp?\n");
  423. goto error;
  424. }
  425. *crt=pkg_malloc(sizeof(struct replace_lst));
  426. if (*crt==0){
  427. LM_ERR("out of mem\n");
  428. goto error;
  429. }
  430. memset(*crt, 0, sizeof(struct replace_lst));
  431. (*crt)->offset=pmatch[0].rm_so+(int)(p-input);
  432. (*crt)->size=pmatch[0].rm_eo-pmatch[0].rm_so;
  433. DBG("subst_run: matched (%d, %d): [%.*s]\n",
  434. (*crt)->offset, (*crt)->size,
  435. (*crt)->size, input+(*crt)->offset);
  436. /* create subst. string */
  437. /* construct the string from replace[] */
  438. if (replace_build(p, nmatch, pmatch, se, msg, &((*crt)->rpl))<0){
  439. goto error;
  440. }
  441. crt=&((*crt)->next);
  442. p+=pmatch[0].rm_eo;
  443. if (*(p-1) == '\n' || *(p-1) == '\r') eflags&=~REG_NOTBOL;
  444. else eflags|=REG_NOTBOL;
  445. cnt++;
  446. }
  447. }while((r==0) && se->replace_all);
  448. pkg_free(pmatch);
  449. if (count)*count=cnt;
  450. return head;
  451. error:
  452. if (head) replace_lst_free(head);
  453. if (pmatch) pkg_free(pmatch);
  454. if (count) *count=-1;
  455. return 0;
  456. }
  457. /* returns the substitution result in a str, input must be 0 term
  458. * 0 on no match or malloc error
  459. * if count is non zero it will be set to the number of matches, or -1
  460. * if error
  461. */
  462. str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se,
  463. int* count)
  464. {
  465. str* res;
  466. struct replace_lst *lst;
  467. struct replace_lst* l;
  468. int len;
  469. int size;
  470. const char* p;
  471. char* dest;
  472. const char* end;
  473. /* compute the len */
  474. len=strlen(input);
  475. end=input+len;
  476. lst=subst_run(se, input, msg, count);
  477. if (lst==0){
  478. DBG("subst_str: no match\n");
  479. return 0;
  480. }
  481. for (l=lst; l; l=l->next)
  482. len+=(int)(l->rpl.len)-l->size;
  483. res=pkg_malloc(sizeof(str));
  484. if (res==0){
  485. LM_ERR("mem. allocation error\n");
  486. goto error;
  487. }
  488. res->s=pkg_malloc(len+1); /* space for null termination */
  489. if (res->s==0){
  490. LM_ERR("mem. allocation error (res->s)\n");
  491. goto error;
  492. }
  493. res->s[len]=0;
  494. res->len=len;
  495. /* replace */
  496. dest=res->s;
  497. p=input;
  498. for(l=lst; l; l=l->next){
  499. size=l->offset+input-p;
  500. memcpy(dest, p, size); /* copy till offset */
  501. p+=size + l->size; /* skip l->size bytes */
  502. dest+=size;
  503. if (l->rpl.len){
  504. memcpy(dest, l->rpl.s, l->rpl.len);
  505. dest+=l->rpl.len;
  506. }
  507. }
  508. memcpy(dest, p, end-p);
  509. if(lst) replace_lst_free(lst);
  510. return res;
  511. error:
  512. if (lst) replace_lst_free(lst);
  513. if (res){
  514. if (res->s) pkg_free(res->s);
  515. pkg_free(res);
  516. }
  517. if (count) *count=-1;
  518. return 0;
  519. }