| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /* $Id$
- *
- *
- * Copyright (C) 2001-2003 Fhg Fokus
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser 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
- */
- #include "data_lump.h"
- #include "dprint.h"
- #include "mem/mem.h"
- #include "globals.h"
- #include "error.h"
- #include <stdlib.h>
- #include <string.h>
- #ifdef DEBUG_DMALLOC
- #include <dmalloc.h>
- #endif
- /* adds a header to the end
- * returns pointer on success, 0 on error */
- struct lump* append_new_lump(struct lump** list, char* new_hdr,
- int len, int type)
- {
- struct lump** t;
- struct lump* tmp;
-
- for (t=list;*t;t=&((*t)->next));
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LOG(L_ERR, "ERROR: append_new_lump: out of memory\n");
- return 0;
- }
-
- memset(tmp,0,sizeof(struct lump));
- tmp->type=type;
- tmp->op=LUMP_ADD;
- tmp->u.value=new_hdr;
- tmp->len=len;
- *t=tmp;
- return tmp;
- }
- /* inserts a header to the beginning
- * returns pointer if success, 0 on error */
- struct lump* insert_new_lump(struct lump** list, char* new_hdr,
- int len, int type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LOG(L_ERR, "ERROR: insert_new_lump: out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->next=*list;
- tmp->type=type;
- tmp->op=LUMP_ADD;
- tmp->u.value=new_hdr;
- tmp->len=len;
- *list=tmp;
- return tmp;
- }
- /* inserts a header/data lump immediately after hdr
- * returns pointer on success, 0 on error */
- struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
- int len, int type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->after=after->after;
- tmp->type=type;
- tmp->op=LUMP_ADD;
- tmp->u.value=new_hdr;
- tmp->len=len;
- after->after=tmp;
- return tmp;
- }
- /* inserts a header/data lump immediately before "before"
- * returns pointer on success, 0 on error */
- struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
- int len, int type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->before=before->before;
- tmp->type=type;
- tmp->op=LUMP_ADD;
- tmp->u.value=new_hdr;
- tmp->len=len;
- before->before=tmp;
- return tmp;
- }
- /* removes an already existing header/data lump */
- struct lump* del_lump(struct lump** list, int offset, int len, int type)
- {
- struct lump* tmp;
- struct lump* prev, *t;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->op=LUMP_DEL;
- tmp->type=type;
- tmp->u.offset=offset;
- tmp->len=len;
- prev=0;
- for (t=*list;t; prev=t, t=t->next){
- /* insert it sorted after offset */
- if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
- break;
- }
- tmp->next=t;
- if (prev) prev->next=tmp;
- else *list=tmp;
- return tmp;
- }
- /* add an anhor */
- struct lump* anchor_lump(struct lump** list, int offset, int len, int type)
- {
- struct lump* tmp;
- struct lump* prev, *t;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->op=LUMP_NOP;
- tmp->type=type;
- tmp->u.offset=offset;
- tmp->len=len;
- prev=0;
- for (t=*list;t; prev=t, t=t->next){
- /* insert it sorted after offset */
- if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
- break;
- }
- tmp->next=t;
-
- if (prev) prev->next=tmp;
- else *list=tmp;
- return tmp;
- }
- void free_lump(struct lump* lmp)
- {
- if (lmp && (lmp->op==LUMP_ADD)){
- if (lmp->u.value) pkg_free(lmp->u.value);
- lmp->u.value=0;
- lmp->len=0;
- }
- }
- void free_lump_list(struct lump* l)
- {
- struct lump* t, *r, *foo,*crt;
- t=l;
- while(t){
- crt=t;
- t=t->next;
- /*
- dangerous recursive clean
- if (crt->before) free_lump_list(crt->before);
- if (crt->after) free_lump_list(crt->after);
- */
- /* no more recursion, clean after and before and that's it */
- r=crt->before;
- while(r){
- foo=r; r=r->before;
- free_lump(foo);
- pkg_free(foo);
- }
- r=crt->after;
- while(r){
- foo=r; r=r->after;
- free_lump(foo);
- pkg_free(foo);
- }
-
- /*clean current elem*/
- free_lump(crt);
- pkg_free(crt);
- }
- }
|