Browse code

pseudo-variable engine api

- imported pvar.h and unified PV and transformations interface in
pvapi.c
- PV engine does not use anymore static array plus a linked list for PV
implementations, PVs will be in module and indexed in a hash table
(small performance enhancement)
- transformations are registered via same model as PV, they will be
implemented in modules, the core will held a hash table to index them
- the core gets a smaller footprint now and better flexibility in
hadling PVs and transformations. pv and transformations will be moved
in pv module
- compiles but no testing so far

Daniel-Constantin Mierla authored on 20/11/2008 11:39:29
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,952 @@
0
+/*
1
+ * $Id: items.h 2111 2007-05-01 11:18:08Z juhe $
2
+ *
3
+ * Copyright (C) 2001-2003 FhG Fokus
4
+ *
5
+ * This file is part of Kamailio, a free SIP server.
6
+ *
7
+ * Kamailio is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * Kamailio is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License 
18
+ * along with this program; if not, write to the Free Software 
19
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
+ */
21
+
22
+/*!
23
+ * \file
24
+ * \brief PV API specification
25
+ */
26
+
27
+#include <stdio.h>
28
+#include <string.h>
29
+#include <sys/types.h>
30
+#include <unistd.h>
31
+
32
+#include "mem/mem.h"
33
+#include "ut.h"
34
+#include "dprint.h"
35
+#include "hashes.h"
36
+#include "pvar.h"
37
+
38
+#define is_in_str(p, in) (p<in->s+in->len && *p)
39
+
40
+#define PV_TABLE_SIZE	16
41
+#define TR_TABLE_SIZE	2
42
+
43
+
44
+void tr_destroy(trans_t *t);
45
+void tr_free(trans_t *t);
46
+void tr_param_free(tr_param_t *tp);
47
+
48
+typedef struct _pv_item
49
+{
50
+	pv_export_t pve;
51
+	unsigned int pvid;
52
+	struct _pv_item *next;
53
+} pv_item_t, *pv_item_p;
54
+
55
+static pv_item_t* _pv_table[PV_TABLE_SIZE];
56
+static int _pv_table_set = 0;
57
+
58
+
59
+/**
60
+ *
61
+ */
62
+void pv_init_table()
63
+{
64
+	memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
65
+	_pv_table_set = 1;
66
+}
67
+
68
+/**
69
+ *
70
+ */
71
+static int is_pv_valid_char(char c)
72
+{
73
+	if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
74
+			|| (c=='_') || (c=='.'))
75
+		return 1;
76
+	return 0;
77
+}
78
+
79
+/**
80
+ *
81
+ */
82
+int pv_table_add(pv_export_t *e)
83
+{
84
+	char *p;
85
+	str  *in;
86
+	pv_item_t *pvi = NULL;
87
+	pv_item_t *pvj = NULL;
88
+	pv_item_t *pvn = NULL;
89
+	int found;
90
+	int pvid;
91
+
92
+	if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
93
+	{
94
+		LM_ERR("invalid parameters\n");
95
+		return -1;
96
+	}
97
+	
98
+	if(_pv_table_set==0)
99
+	{
100
+		LM_DBG("PV table not initialized, doing it now\n");
101
+		pv_init_table();
102
+	}
103
+	in = &(e->name);
104
+	p = in->s;	
105
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
106
+		p++;
107
+	if(is_in_str(p,in))
108
+	{
109
+		LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
110
+		return -1;
111
+	}
112
+	found = 0;
113
+	pvid = get_hash1_raw(in->s, in->len);
114
+
115
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
116
+	while(pvi)
117
+	{
118
+		if(pvi->pvid > pvid)
119
+			break;
120
+		if(pvi->pve.name.len > in->len)
121
+			break;
122
+		if(pvi->pve.name.len==in->len)
123
+		{
124
+			found = strncmp(pvi->pve.name.s, in->s, in->len);
125
+			if(found>0)
126
+				break;
127
+			if(found==0)
128
+			{
129
+				LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
130
+				return -1;
131
+			}
132
+		}
133
+		pvj = pvi;
134
+		pvi = pvi->next;
135
+	}
136
+
137
+	pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
138
+	if(pvn==0)
139
+	{
140
+		LM_ERR("no more memory\n");
141
+		return -1;
142
+	}
143
+	memset(pvn, 0, sizeof(pv_item_t));
144
+	memcpy(&(pvn->pve), e, sizeof(pv_export_t));
145
+	pvn->pvid = pvid;
146
+
147
+	if(pvj==0)
148
+	{
149
+		pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
150
+		_pv_table[pvid%PV_TABLE_SIZE] = pvn;
151
+		goto done;
152
+	}
153
+	pvn->next = pvj->next;
154
+	pvj->next = pvn;
155
+
156
+done:
157
+	return 0;
158
+}
159
+
160
+/**
161
+ *
162
+ */
163
+int register_pvars_mod(char *mod_name, pv_export_t *items)
164
+{
165
+	int ret;
166
+	int i;
167
+
168
+	if (items==0)
169
+		return 0;
170
+
171
+	for ( i=0 ; items[i].name.s ; i++ ) {
172
+		ret = pv_table_add(&items[i]);
173
+		if (ret!=0) {
174
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
175
+					items[i].name.len, items[i].name.s, mod_name);
176
+		}
177
+	}
178
+	return 0;
179
+}
180
+
181
+/**
182
+ *
183
+ */
184
+int pv_table_free(void)
185
+{
186
+	pv_item_p xe;
187
+	pv_item_p xe1;
188
+	int i;
189
+
190
+	for(i=0; i<PV_TABLE_SIZE; i++)
191
+	{
192
+		xe = _pv_table[i];
193
+		while(xe!=0)
194
+		{
195
+			xe1 = xe;
196
+			xe = xe->next;
197
+			pkg_free(xe1);
198
+		}
199
+		memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
200
+		_pv_table_set = 0;
201
+	}
202
+	
203
+	return 0;
204
+}
205
+
206
+/********** helper functions ********/
207
+/**
208
+ * convert unsigned int to pv_value_t
209
+ */
210
+int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
211
+		pv_value_t *res, unsigned int uival)
212
+{
213
+	int l = 0;
214
+	char *ch = NULL;
215
+
216
+	if(res==NULL)
217
+		return -1;
218
+
219
+	ch = int2str(uival, &l);
220
+	res->rs.s = ch;
221
+	res->rs.len = l;
222
+
223
+	res->ri = (int)uival;
224
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
225
+	return 0;
226
+}
227
+
228
+/**
229
+ * convert signed int to pv_value_t
230
+ */
231
+int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
232
+		pv_value_t *res, int sival)
233
+{
234
+	int l = 0;
235
+	char *ch = NULL;
236
+
237
+	if(res==NULL)
238
+		return -1;
239
+
240
+	ch = int2str(sival, &l);
241
+	res->rs.s = ch;
242
+	res->rs.len = l;
243
+
244
+	res->ri = sival;
245
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
246
+	return 0;
247
+}
248
+
249
+/**
250
+ * convert str to pv_value_t
251
+ */
252
+int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
253
+		pv_value_t *res, str *sval)
254
+{
255
+	if(res==NULL)
256
+		return -1;
257
+
258
+	res->rs = *sval;
259
+	res->flags = PV_VAL_STR;
260
+	return 0;
261
+}
262
+
263
+/**
264
+ * convert str-int to pv_value_t (type is str)
265
+ */
266
+int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
267
+		pv_value_t *res, str *sval, int ival)
268
+{
269
+	if(res==NULL)
270
+		return -1;
271
+
272
+	res->rs = *sval;
273
+	res->ri = ival;
274
+	res->flags = PV_VAL_STR|PV_VAL_INT;
275
+	return 0;
276
+}
277
+
278
+/**
279
+ * convert int-str to pv_value_t (type is int)
280
+ */
281
+int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
282
+		pv_value_t *res, int ival, str *sval)
283
+{
284
+	if(res==NULL)
285
+		return -1;
286
+
287
+	res->rs = *sval;
288
+	res->ri = ival;
289
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
290
+	return 0;
291
+}
292
+
293
+/*** ============================= ***/
294
+static str pv_str_marker = { PV_MARKER_STR, 1 };
295
+static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
296
+		pv_value_t *res)
297
+{
298
+	return pv_get_strintval(msg, param, res, &pv_str_marker, (int)pv_str_marker.s[0]);
299
+}
300
+
301
+pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
302
+{
303
+	pv_item_t *pvi;
304
+	unsigned int pvid;
305
+
306
+	if(pvname==0 || e==0)
307
+	{
308
+		LM_ERR("bad parameters\n");
309
+		return NULL;
310
+	}
311
+
312
+	/* search in PV table */
313
+	pvid = get_hash1_raw(pvname->s, pvname->len);
314
+	pvi = _pv_table[pvid%PV_TABLE_SIZE];
315
+	while(pvi)
316
+	{
317
+		if(pvi->pvid > pvid)
318
+			break;
319
+		if(pvi->pve.name.len > pvname->len)
320
+			break;
321
+
322
+		if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
323
+			&& memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
324
+		{
325
+			/*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
326
+					_pv_names_table[i].type);*/
327
+			/* copy data from table to spec */
328
+			e->type = pvi->pve.type;
329
+			e->getf = pvi->pve.getf;
330
+			e->setf = pvi->pve.setf;
331
+			return &(pvi->pve);
332
+		}
333
+		pvi = pvi->next;
334
+	}
335
+
336
+	return NULL;
337
+}
338
+
339
+char* pv_parse_spec(str *in, pv_spec_p e)
340
+{
341
+	char *p;
342
+	str s;
343
+	str pvname;
344
+	int pvstate;
345
+	trans_t *tr = NULL;
346
+	pv_export_t *pte = NULL;
347
+	int n=0;
348
+
349
+	if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
350
+	{
351
+		LM_ERR("bad parameters\n");
352
+		return NULL;
353
+	}
354
+	
355
+	/* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
356
+	tr = 0;
357
+	pvstate = 0;
358
+	memset(e, 0, sizeof(pv_spec_t));
359
+	p = in->s;
360
+	p++;
361
+	if(*p==PV_LNBRACKET)
362
+	{
363
+		p++;
364
+		pvstate = 1;
365
+	}
366
+	pvname.s = p;
367
+	if(*p == PV_MARKER) {
368
+		p++;
369
+		if(pvstate==1)
370
+		{
371
+			if(*p!=PV_RNBRACKET)
372
+				goto error;
373
+			p++;
374
+		}
375
+		e->getf = pv_get_marker;
376
+		e->type = PVT_MARKER;
377
+		pvname.len = 1;
378
+		goto done_all;
379
+	}
380
+	while(is_in_str(p,in) && is_pv_valid_char(*p))
381
+		p++;
382
+	pvname.len = p - pvname.s;
383
+	if(pvstate==1)
384
+	{
385
+		if(*p==PV_RNBRACKET)
386
+		{ /* full pv name ended here*/
387
+			goto done_inm;
388
+		} else if(*p==PV_LNBRACKET) {
389
+			p++;
390
+			pvstate = 2;
391
+		} else if(*p==PV_LIBRACKET) {
392
+			p++;
393
+			pvstate = 3;
394
+		} else if(*p==TR_LBRACKET) {
395
+			p++;
396
+			pvstate = 4;
397
+		} else {
398
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
399
+					pvstate);
400
+			goto error;
401
+		}
402
+	} else { 
403
+		if(!is_in_str(p, in)) {
404
+			p--;
405
+			goto done_inm;
406
+		} else if(*p==PV_LNBRACKET) {
407
+			p++;
408
+			pvstate = 5;
409
+		} else {
410
+			/* still in input str, but end of PV */
411
+			/* p is increased at the end, so decrement here */
412
+			p--;
413
+			goto done_inm;
414
+		}
415
+	}
416
+
417
+done_inm:
418
+	if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
419
+	{
420
+		LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
421
+		goto error;
422
+	}
423
+	if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
424
+	{
425
+		LM_ERR("pvar \"%.*s\" expects an inner name\n",
426
+				pvname.len, pvname.s);
427
+		goto error;
428
+	}
429
+	if(pvstate==2 || pvstate==5)
430
+	{
431
+		if(pte->parse_name==NULL)
432
+		{
433
+			LM_ERR("pvar \"%.*s\" does not get name param\n",
434
+					pvname.len, pvname.s);
435
+			goto error;
436
+		}
437
+		s.s = p;
438
+		n = 0;
439
+		while(is_in_str(p, in))
440
+		{
441
+			if(*p==PV_RNBRACKET)
442
+			{
443
+				if(n==0)
444
+					break;
445
+				n--;
446
+			}
447
+			if(*p == PV_LNBRACKET)
448
+				n++;
449
+			p++;
450
+		}
451
+
452
+		if(!is_in_str(p, in))
453
+			goto error;
454
+
455
+		if(p==s.s)
456
+		{
457
+			LM_ERR("pvar \"%.*s\" does not get empty name param\n",
458
+					pvname.len, pvname.s);
459
+			goto error;
460
+		}
461
+		s.len = p - s.s;
462
+		if(pte->parse_name(e, &s)!=0)
463
+		{
464
+			LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
465
+					pvname.len, pvname.s, s.len, s.s);
466
+			goto error;
467
+		}
468
+		if(pvstate==2)
469
+		{
470
+			p++;
471
+			if(*p==PV_RNBRACKET)
472
+			{ /* full pv name ended here*/
473
+				goto done_vnm;
474
+			} else if(*p==PV_LIBRACKET) {
475
+				p++;
476
+				pvstate = 3;
477
+			} else if(*p==TR_LBRACKET) {
478
+				p++;
479
+				pvstate = 4;
480
+			} else {
481
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
482
+					pvstate);
483
+				goto error;
484
+			}
485
+		} else {
486
+			if(*p==PV_RNBRACKET)
487
+			{ /* full pv name ended here*/
488
+				p++;
489
+				goto done_all;
490
+			} else {
491
+				LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
492
+					pvstate);
493
+				goto error;
494
+			}
495
+		}
496
+	}
497
+done_vnm:
498
+	if(pvstate==3)
499
+	{
500
+		if(pte->parse_index==NULL)
501
+		{
502
+			LM_ERR("pvar \"%.*s\" does not get index param\n",
503
+					pvname.len, pvname.s);
504
+			goto error;
505
+		}
506
+		s.s = p;
507
+		n = 0;
508
+		while(is_in_str(p, in))
509
+		{
510
+			if(*p==PV_RIBRACKET)
511
+			{
512
+				if(n==0)
513
+					break;
514
+				n--;
515
+			}
516
+			if(*p == PV_LIBRACKET)
517
+				n++;
518
+			p++;
519
+		}
520
+		if(!is_in_str(p, in))
521
+			goto error;
522
+
523
+		if(p==s.s)
524
+		{
525
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
526
+					pvname.len, pvname.s);
527
+			goto error;
528
+		}
529
+		s.len = p - s.s;
530
+		if(pte->parse_index(e, &s)!=0)
531
+		{
532
+			LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
533
+					pvname.len, pvname.s, s.len, s.s);
534
+			goto error;
535
+		}
536
+		p++;
537
+		if(*p==PV_RNBRACKET)
538
+		{ /* full pv name ended here*/
539
+			goto done_idx;
540
+		} else if(*p==TR_LBRACKET) {
541
+			p++;
542
+			pvstate = 4;
543
+		} else {
544
+			LM_ERR("invalid char '%c' in [%.*s] (%d)\n", *p, in->len, in->s,
545
+					pvstate);
546
+			goto error;
547
+		}
548
+	}
549
+done_idx:
550
+	if(pvstate==4)
551
+	{
552
+		s.s = p-1;
553
+		n = 0;
554
+		while(is_in_str(p, in))
555
+		{
556
+			if(*p==TR_RBRACKET)
557
+			{
558
+				if(n==0)
559
+				{
560
+					/* yet another transformation */
561
+					p++;
562
+					while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
563
+
564
+					if(!is_in_str(p, in) || *p != TR_LBRACKET)
565
+					{
566
+						p--;
567
+						break;
568
+					}
569
+				}
570
+				n--;
571
+			}
572
+			if(*p == TR_LBRACKET)
573
+				n++;
574
+			p++;
575
+		}
576
+		if(!is_in_str(p, in))
577
+			goto error;
578
+
579
+		if(p==s.s)
580
+		{
581
+			LM_ERR("pvar \"%.*s\" does not get empty index param\n",
582
+					pvname.len, pvname.s);
583
+			goto error;
584
+		}
585
+		s.len = p - s.s + 1;
586
+
587
+		p = tr_lookup(&s, &tr);
588
+		if(p==NULL)
589
+		{
590
+			LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
591
+			goto error;
592
+		}
593
+		if(*p!=PV_RNBRACKET)
594
+		{
595
+			LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
596
+			goto error;
597
+		}
598
+		e->trans = (void*)tr;
599
+	}
600
+	p++;
601
+
602
+done_all:
603
+	if(pte!=NULL && pte->init_param)
604
+		pte->init_param(e, pte->iparam);
605
+	return p;
606
+
607
+error:
608
+	if(p!=NULL)
609
+		LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
610
+			in->len, in->s, (int)(p-in->s), pvstate);
611
+	else
612
+		LM_ERR("invalid parsing in [%.*s] at (%d)\n", in->len, in->s, pvstate);
613
+	return NULL;
614
+
615
+} /* end: pv_parse_spec */
616
+
617
+void pv_spec_free(pv_spec_t *spec)
618
+{
619
+	if(spec==0) return;
620
+	/* TODO: free name if it is PV */
621
+	if(spec->trans)
622
+		tr_free((trans_t*)spec->trans);
623
+	pkg_free(spec);
624
+}
625
+
626
+static inline char* tr_get_class(str *in, char *p, str *tclass)
627
+{
628
+	tclass->s = p;
629
+	while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
630
+	if(*p!=TR_CLASS_MARKER || tclass->s == p)
631
+	{
632
+		LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
633
+		return NULL;
634
+	}
635
+	tclass->len = p - tclass->s;
636
+	p++;
637
+
638
+	return p;
639
+}
640
+
641
+static inline trans_t* tr_new()
642
+{
643
+	trans_t *t = NULL;
644
+
645
+	t = (trans_t*)pkg_malloc(sizeof(trans_t));
646
+	if(t == NULL)
647
+	{
648
+		LM_ERR("no more private memory\n");
649
+		return NULL;
650
+	}
651
+	memset(t, 0, sizeof(trans_t));
652
+	return t;
653
+}
654
+
655
+char* tr_lookup(str *in, trans_t **tr)
656
+{
657
+	char *p;
658
+	char *p0;
659
+	str tclass;
660
+	tr_export_t *te = NULL; 
661
+	trans_t *t = NULL;
662
+	trans_t *t0 = NULL;
663
+	str s;
664
+
665
+	if(in==NULL || in->s==NULL || tr==NULL)
666
+		return NULL;
667
+	
668
+	p = in->s;
669
+	do {
670
+		while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
671
+		if(*p != TR_LBRACKET)
672
+			break;
673
+		p++;
674
+
675
+		if((t = tr_new())==NULL) return NULL;
676
+
677
+		if(t0==NULL) *tr = t;
678
+		else t0->next = t;
679
+		t0 = t;
680
+
681
+		/* find transformation class */
682
+		p = tr_get_class(in, p, &tclass);
683
+		if(p==NULL) goto error;
684
+
685
+		/* locate transformation */
686
+		te = tr_lookup_class(&tclass);
687
+		if(te==NULL)
688
+		{
689
+			LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
690
+				tclass.len, tclass.s, in->len, in->s);
691
+			goto error;
692
+		}
693
+
694
+		s.s = p; s.len = in->s + in->len - p;
695
+		p0 = te->tparse(&s, t);
696
+		if(p0==NULL)
697
+			goto error;
698
+		p = p0;
699
+
700
+		if(*p != TR_RBRACKET)
701
+		{
702
+			LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
703
+					in->s, *p);
704
+			goto error;
705
+		}
706
+
707
+		p++;
708
+		if(!is_in_str(p, in))
709
+			break;
710
+	} while(1);
711
+
712
+	return p;
713
+error:
714
+	LM_ERR("error parsing [%.*s]\n", in->len, in->s);
715
+	t = *tr;
716
+	while(t)
717
+	{
718
+		t0 = t;
719
+		t = t->next;
720
+		tr_destroy(t0);
721
+		pkg_free(t0);
722
+	}
723
+	return NULL;
724
+}
725
+
726
+/*!
727
+ * \brief Destroy transformation including eventual parameter
728
+ * \param t transformation
729
+ */
730
+void tr_destroy(trans_t *t)
731
+{
732
+	tr_param_t *tp;
733
+	tr_param_t *tp0;
734
+	if(t==NULL) return;
735
+
736
+	tp = t->params;
737
+	while(tp)
738
+	{
739
+		tp0 = tp;
740
+		tp = tp->next;
741
+		tr_param_free(tp0);
742
+	}
743
+	memset(t, 0, sizeof(trans_t));
744
+}
745
+
746
+
747
+/*!
748
+ * \brief Free allocated memory of transformation list
749
+ * \param t transformation list
750
+ */
751
+void tr_free(trans_t *t)
752
+{
753
+	trans_t *t0;
754
+
755
+	while(t)
756
+	{
757
+		t0 = t;
758
+		t = t->next;
759
+		tr_destroy(t0);
760
+		pkg_free(t0);
761
+	}
762
+}
763
+
764
+
765
+/*!
766
+ * \brief Free transformation parameter list
767
+ * \param tp transformation list
768
+ */
769
+void tr_param_free(tr_param_t *tp)
770
+{
771
+	tr_param_t *tp0;
772
+
773
+	if(tp==NULL) return;
774
+	while(tp)
775
+	{
776
+		tp0 = tp;
777
+		tp = tp->next;
778
+		if(tp0->type==TR_PARAM_SPEC)
779
+			pv_spec_free((pv_spec_t*)tp0->v.data);
780
+		pkg_free(tp0);
781
+	}
782
+}
783
+
784
+typedef struct _tr_item
785
+{
786
+	tr_export_t tre;
787
+	unsigned int trid;
788
+	struct _tr_item *next;
789
+} tr_item_t, *tr_item_p;
790
+
791
+static tr_item_t* _tr_table[PV_TABLE_SIZE];
792
+static int _tr_table_set = 0;
793
+
794
+
795
+/**
796
+ *
797
+ */
798
+void tr_init_table()
799
+{
800
+	memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
801
+	_tr_table_set = 1;
802
+}
803
+
804
+/**
805
+ *
806
+ */
807
+int tr_table_add(tr_export_t *e)
808
+{
809
+	tr_item_t *tri = NULL;
810
+	tr_item_t *trj = NULL;
811
+	tr_item_t *trn = NULL;
812
+	int found;
813
+	int trid;
814
+
815
+	if(e==NULL || e->tclass.s==NULL)
816
+	{
817
+		LM_ERR("invalid parameters\n");
818
+		return -1;
819
+	}
820
+	
821
+	if(_tr_table_set==0)
822
+	{
823
+		LM_DBG("TR table not initialized, doing it now\n");
824
+		tr_init_table();
825
+	}
826
+
827
+	found = 0;
828
+	trid = get_hash1_raw(e->tclass.s, e->tclass.len);
829
+
830
+	tri = _tr_table[trid%PV_TABLE_SIZE];
831
+	while(tri)
832
+	{
833
+		if(tri->trid > trid)
834
+			break;
835
+		if(tri->tre.tclass.len > e->tclass.len)
836
+			break;
837
+		if(tri->tre.tclass.len==e->tclass.len)
838
+		{
839
+			found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
840
+			if(found>0)
841
+				break;
842
+			if(found==0)
843
+			{
844
+				LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
845
+						e->tclass.s);
846
+				return -1;
847
+			}
848
+		}
849
+		trj = tri;
850
+		tri = tri->next;
851
+	}
852
+
853
+	trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
854
+	if(trn==0)
855
+	{
856
+		LM_ERR("no more memory\n");
857
+		return -1;
858
+	}
859
+	memset(trn, 0, sizeof(tr_item_t));
860
+	memcpy(&(trn->tre), e, sizeof(tr_export_t));
861
+	trn->trid = trid;
862
+
863
+	if(trj==0)
864
+	{
865
+		trn->next = _tr_table[trid%PV_TABLE_SIZE];
866
+		_tr_table[trid%PV_TABLE_SIZE] = trn;
867
+		goto done;
868
+	}
869
+	trn->next = trj->next;
870
+	trj->next = trn;
871
+
872
+done:
873
+	return 0;
874
+}
875
+
876
+/**
877
+ *
878
+ */
879
+int register_trans_mod(char *mod_name, tr_export_t *items)
880
+{
881
+	int ret;
882
+	int i;
883
+
884
+	if (items==0)
885
+		return 0;
886
+
887
+	for ( i=0 ; items[i].tclass.s ; i++ ) {
888
+		ret = tr_table_add(&items[i]);
889
+		if (ret!=0) {
890
+			LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
891
+					items[i].tclass.len, items[i].tclass.s, mod_name);
892
+		}
893
+	}
894
+	return 0;
895
+}
896
+
897
+/**
898
+ *
899
+ */
900
+int tr_table_free(void)
901
+{
902
+	tr_item_p te;
903
+	tr_item_p te1;
904
+	int i;
905
+
906
+	for(i=0; i<TR_TABLE_SIZE; i++)
907
+	{
908
+		te = _tr_table[i];
909
+		while(te!=0)
910
+		{
911
+			te1 = te;
912
+			te = te->next;
913
+			pkg_free(te1);
914
+		}
915
+		memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
916
+		_tr_table_set = 0;
917
+	}
918
+	
919
+	return 0;
920
+}
921
+
922
+tr_export_t* tr_lookup_class(str *tclass)
923
+{
924
+	tr_item_t *tri;
925
+	unsigned int trid;
926
+
927
+	if(tclass==0 || tclass->s==0)
928
+	{
929
+		LM_ERR("bad parameters\n");
930
+		return NULL;
931
+	}
932
+
933
+	/* search in TR table */
934
+	trid = get_hash1_raw(tclass->s, tclass->len);
935
+	tri = _tr_table[trid%TR_TABLE_SIZE];
936
+	while(tri)
937
+	{
938
+		if(tri->trid > trid)
939
+			break;
940
+		if(tri->tre.tclass.len > tclass->len)
941
+			break;
942
+
943
+		if(tri->trid==trid && tri->tre.tclass.len==tclass->len
944
+				&& memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
945
+			return &(tri->tre);
946
+		tri = tri->next;
947
+	}
948
+
949
+	return NULL;
950
+}
951
+