data_lump.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* $Id$
  2. *
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include "data_lump.h"
  28. #include "dprint.h"
  29. #include "mem/mem.h"
  30. #include "globals.h"
  31. #include "error.h"
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef DEBUG_DMALLOC
  35. #include <dmalloc.h>
  36. #endif
  37. /* adds a header to the end
  38. * returns pointer on success, 0 on error */
  39. struct lump* append_new_lump(struct lump** list, char* new_hdr,
  40. int len, int type)
  41. {
  42. struct lump** t;
  43. struct lump* tmp;
  44. for (t=list;*t;t=&((*t)->next));
  45. tmp=pkg_malloc(sizeof(struct lump));
  46. if (tmp==0){
  47. LOG(L_ERR, "ERROR: append_new_lump: out of memory\n");
  48. return 0;
  49. }
  50. memset(tmp,0,sizeof(struct lump));
  51. tmp->type=type;
  52. tmp->op=LUMP_ADD;
  53. tmp->u.value=new_hdr;
  54. tmp->len=len;
  55. *t=tmp;
  56. return tmp;
  57. }
  58. /* inserts a header to the beginning
  59. * returns pointer if success, 0 on error */
  60. struct lump* insert_new_lump(struct lump** list, char* new_hdr,
  61. int len, int type)
  62. {
  63. struct lump* tmp;
  64. tmp=pkg_malloc(sizeof(struct lump));
  65. if (tmp==0){
  66. LOG(L_ERR, "ERROR: insert_new_lump: out of memory\n");
  67. return 0;
  68. }
  69. memset(tmp,0,sizeof(struct lump));
  70. tmp->next=*list;
  71. tmp->type=type;
  72. tmp->op=LUMP_ADD;
  73. tmp->u.value=new_hdr;
  74. tmp->len=len;
  75. *list=tmp;
  76. return tmp;
  77. }
  78. /* inserts a header/data lump immediately after hdr
  79. * returns pointer on success, 0 on error */
  80. struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
  81. int len, int type)
  82. {
  83. struct lump* tmp;
  84. tmp=pkg_malloc(sizeof(struct lump));
  85. if (tmp==0){
  86. ser_error=E_OUT_OF_MEM;
  87. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  88. return 0;
  89. }
  90. memset(tmp,0,sizeof(struct lump));
  91. tmp->after=after->after;
  92. tmp->type=type;
  93. tmp->op=LUMP_ADD;
  94. tmp->u.value=new_hdr;
  95. tmp->len=len;
  96. after->after=tmp;
  97. return tmp;
  98. }
  99. /* inserts a header/data lump immediately before "before"
  100. * returns pointer on success, 0 on error */
  101. struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
  102. int len, int type)
  103. {
  104. struct lump* tmp;
  105. tmp=pkg_malloc(sizeof(struct lump));
  106. if (tmp==0){
  107. ser_error=E_OUT_OF_MEM;
  108. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  109. return 0;
  110. }
  111. memset(tmp,0,sizeof(struct lump));
  112. tmp->before=before->before;
  113. tmp->type=type;
  114. tmp->op=LUMP_ADD;
  115. tmp->u.value=new_hdr;
  116. tmp->len=len;
  117. before->before=tmp;
  118. return tmp;
  119. }
  120. /* removes an already existing header/data lump */
  121. struct lump* del_lump(struct lump** list, int offset, int len, int type)
  122. {
  123. struct lump* tmp;
  124. struct lump* prev, *t;
  125. tmp=pkg_malloc(sizeof(struct lump));
  126. if (tmp==0){
  127. LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
  128. return 0;
  129. }
  130. memset(tmp,0,sizeof(struct lump));
  131. tmp->op=LUMP_DEL;
  132. tmp->type=type;
  133. tmp->u.offset=offset;
  134. tmp->len=len;
  135. prev=0;
  136. for (t=*list;t; prev=t, t=t->next){
  137. /* insert it sorted after offset */
  138. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  139. break;
  140. }
  141. tmp->next=t;
  142. if (prev) prev->next=tmp;
  143. else *list=tmp;
  144. return tmp;
  145. }
  146. /* add an anhor */
  147. struct lump* anchor_lump(struct lump** list, int offset, int len, int type)
  148. {
  149. struct lump* tmp;
  150. struct lump* prev, *t;
  151. tmp=pkg_malloc(sizeof(struct lump));
  152. if (tmp==0){
  153. ser_error=E_OUT_OF_MEM;
  154. LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
  155. return 0;
  156. }
  157. memset(tmp,0,sizeof(struct lump));
  158. tmp->op=LUMP_NOP;
  159. tmp->type=type;
  160. tmp->u.offset=offset;
  161. tmp->len=len;
  162. prev=0;
  163. for (t=*list;t; prev=t, t=t->next){
  164. /* insert it sorted after offset */
  165. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  166. break;
  167. }
  168. tmp->next=t;
  169. if (prev) prev->next=tmp;
  170. else *list=tmp;
  171. return tmp;
  172. }
  173. void free_lump(struct lump* lmp)
  174. {
  175. if (lmp && (lmp->op==LUMP_ADD)){
  176. if (lmp->u.value) pkg_free(lmp->u.value);
  177. lmp->u.value=0;
  178. lmp->len=0;
  179. }
  180. }
  181. void free_lump_list(struct lump* l)
  182. {
  183. struct lump* t, *r, *foo,*crt;
  184. t=l;
  185. while(t){
  186. crt=t;
  187. t=t->next;
  188. /*
  189. dangerous recursive clean
  190. if (crt->before) free_lump_list(crt->before);
  191. if (crt->after) free_lump_list(crt->after);
  192. */
  193. /* no more recursion, clean after and before and that's it */
  194. r=crt->before;
  195. while(r){
  196. foo=r; r=r->before;
  197. free_lump(foo);
  198. pkg_free(foo);
  199. }
  200. r=crt->after;
  201. while(r){
  202. foo=r; r=r->after;
  203. free_lump(foo);
  204. pkg_free(foo);
  205. }
  206. /*clean current elem*/
  207. free_lump(crt);
  208. pkg_free(crt);
  209. }
  210. }