Browse code

core: Consolidate append_str macro usage

The definition of append_str macro was scattered across the source tree,
there were multiple definition of the macro, some of them with different
parameters.

This patch brings the definition of append_str macro into ut.h header
file and removes all other definitions from core and modules.

We chose the version with three parameters where the pointer and the
length of the source string are passed separately to the macro. This is
more flexible than passing both of them within one parameter as str
structure.

All references to append_mem_block have been removed because the same
operation can now be achieved with append_str. In addition to that we
updated parameters passed to append_str where needed, converting one
str parameter to <pointer,len> pair of parameters.

Some modules had yet another version of append_str defined, there we
renamed the macro to some other name to make sure that the local
definition does not conflict with the definition on ut.h

Jan Janak authored on 06/05/2009 12:47:14
Showing 9 changed files
... ...
@@ -41,17 +41,6 @@
41 41
 #endif
42 42
 
43 43
 
44
-#define append_str(_dest,_src,_len)				\
45
-	do{											\
46
-		memcpy( (_dest) , (_src) , (_len) );	\
47
-		(_dest) += (_len) ;						\
48
-	}while(0);									\
49
-	
50
-/*! append _c char to _dest string */
51
-#define append_chr(_dest,_c) \
52
-	*((_dest)++) = _c;
53
-
54
-
55 44
 /* INTeger-TO-Buffer-STRing : convers an unsigned long to a string 
56 45
  * IMPORTANT: the provided buffer must be at least INT2STR_MAX_LEN size !! */
57 46
 static inline char* int2bstr(unsigned long l, char *s, int* len)
... ...
@@ -105,7 +105,7 @@
105 105
 			append_chr(s,','); len++;\
106 106
 		}\
107 107
 		append_chr(s,'<');len++;\
108
-		append_mem_block(s,rs,rlen);\
108
+		append_str(s,rs,rlen);\
109 109
 		len += rlen; \
110 110
 		append_chr(s,'>');len++;\
111 111
 	} while(0)
... ...
@@ -824,7 +824,7 @@ static int assemble_msg(struct sip_msg* msg, struct tw_info *twi)
824 824
 		    "while copying optional header\n");
825 825
 		goto error;
826 826
 	}
827
-	append_mem_block(s,"P-MsgFlags: ",12);
827
+	append_str(s,"P-MsgFlags: ",12);
828 828
 	l = APPEND_BUFFER_MAX - (12+1); /* include trailing `\n'*/
829 829
 
830 830
 	if (int2reverse_hex(&s, &l, (int)msg->msg_flags) == -1) {
... ...
@@ -848,8 +848,8 @@ static int assemble_msg(struct sip_msg* msg, struct tw_info *twi)
848 848
 		    "copying command name\n");
849 849
 		goto error;
850 850
 	}
851
-	append_mem_block(s,"sip_request.",12);
852
-	append_str(s,twi->action);
851
+	append_str(s,"sip_request.",12);
852
+	append_str(s,twi->action.s, twi->action.len);
853 853
 	eol_line_len(1) = s - (char*)eol_line_s(1);
854 854
 
855 855
 	eol_line(2,REQ_LINE(msg).method);     /* method type */
... ...
@@ -86,22 +86,6 @@ extern int_str contacts_avp;
86 86
 #define FR_INV_TIMER_AVP  "callee_fr_inv_timer"
87 87
 
88 88
 
89
-#define append_str(_dest,_str) \
90
-	do{ \
91
-		memcpy( (_dest) , (_str).s , (_str).len );\
92
-		(_dest) += (_str).len ;\
93
-	}while(0);
94
-
95
-#define  append_mem_block(_d,_s,_len) \
96
-		do{\
97
-			memcpy((_d),(_s),(_len));\
98
-			(_d) += (_len);\
99
-		}while(0);
100
-
101
-#define append_chr(_dest,_c) \
102
-	*((_dest)++) = _c;
103
-
104
-
105 89
 /* send a private buffer: utilize a retransmission structure
106 90
    but take a separate buffer not referred by it; healthy
107 91
    for reducing time spend in REPLIES locks
... ...
@@ -167,37 +167,37 @@ char *build_local(struct cell *Trans,unsigned int branch,
167 167
 	}
168 168
 	p = cancel_buf;
169 169
 
170
-	append_mem_block( p, method, method_len );
171
-	append_mem_block( p, " ", 1 );
172
-	append_str( p, Trans->uac[branch].uri);
173
-	append_mem_block( p, " " SIP_VERSION CRLF, 1+SIP_VERSION_LEN+CRLF_LEN );
170
+	append_str( p, method, method_len );
171
+	append_str( p, " ", 1 );
172
+	append_str( p, Trans->uac[branch].uri.s, Trans->uac[branch].uri.len);
173
+	append_str( p, " " SIP_VERSION CRLF, 1+SIP_VERSION_LEN+CRLF_LEN );
174 174
 
175 175
 	/* insert our via */
176
-	append_mem_block(p,via,via_len);
176
+	append_str(p,via,via_len);
177 177
 
178 178
 	/*other headers*/
179
-	append_str( p, Trans->from );
180
-	append_str( p, Trans->callid );
181
-	append_str( p, *to );
179
+	append_str( p, Trans->from.s, Trans->from.len );
180
+	append_str( p, Trans->callid.s, Trans->callid.len );
181
+	append_str( p, to->s, to->len );
182 182
 
183
-	append_str( p, Trans->cseq_n );
184
-	append_mem_block( p, " ", 1 );
185
-	append_mem_block( p, method, method_len );
186
-	append_mem_block( p, CRLF, CRLF_LEN );
183
+	append_str( p, Trans->cseq_n.s, Trans->cseq_n.len );
184
+	append_str( p, " ", 1 );
185
+	append_str( p, method, method_len );
186
+	append_str( p, CRLF, CRLF_LEN );
187 187
 
188 188
 	if (!is_local(Trans))  {
189 189
 		for ( hdr=Trans->uas.request->headers ; hdr ; hdr=hdr->next )
190 190
 			if(hdr->type==HDR_ROUTE_T) {
191
-				append_mem_block(p, hdr->name.s, hdr->len );
191
+				append_str(p, hdr->name.s, hdr->len );
192 192
 			}
193 193
 	}
194 194
 
195 195
 	/* User Agent header */
196 196
 	if (server_signature) {
197
-		append_mem_block(p,USER_AGENT CRLF, USER_AGENT_LEN+CRLF_LEN );
197
+		append_str(p,USER_AGENT CRLF, USER_AGENT_LEN+CRLF_LEN );
198 198
 	}
199 199
 	/* Content Length, EoM */
200
-	append_mem_block(p, CONTENT_LENGTH "0" CRLF CRLF ,
200
+	append_str(p, CONTENT_LENGTH "0" CRLF CRLF ,
201 201
 		CONTENT_LENGTH_LEN+1 + CRLF_LEN + CRLF_LEN);
202 202
 	*p=0;
203 203
 
... ...
@@ -253,14 +253,14 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
253 253
 	d = cancel_buf;
254 254
 
255 255
 	/* method name + space */
256
-	append_mem_block(d, method, method_len);
256
+	append_str(d, method, method_len);
257 257
 	*d = ' ';
258 258
 	d++;
259 259
 	/* skip "INVITE " and copy the rest of the line including CRLF */
260 260
 	s += 7;
261 261
 	s1 = s;
262 262
 	s = eat_line(s, invite_buf_end - s);
263
-	append_mem_block(d, s1, s - s1);
263
+	append_str(d, s1, s - s1);
264 264
 
265 265
 	/* check every header field name,
266 266
 	we must exclude and modify some of the headers */
... ...
@@ -282,9 +282,9 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
282 282
 				while ((s < invite_buf_end)
283 283
 					&& ((*s == ':') || (*s == ' ') || (*s == '\t') || ((*s >= '0') && (*s <= '9')))
284 284
 					) s++;
285
-				append_mem_block(d, s1, s - s1);
286
-				append_mem_block(d, method, method_len);
287
-				append_mem_block(d, CRLF, CRLF_LEN);
285
+				append_str(d, s1, s - s1);
286
+				append_str(d, method, method_len);
287
+				append_str(d, CRLF, CRLF_LEN);
288 288
 				s = lw_next_line(s, invite_buf_end);
289 289
 				break;
290 290
 
... ...
@@ -292,7 +292,7 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
292 292
 				s = lw_next_line(s, invite_buf_end);
293 293
 				if (first_via) {
294 294
 					/* copy hf */
295
-					append_mem_block(d, s1, s - s1);
295
+					append_str(d, s1, s - s1);
296 296
 					first_via = 0;
297 297
 				} /* else skip this line, we need olny the first via */
298 298
 				break;
... ...
@@ -301,10 +301,10 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
301 301
 				if (to_len == 0) {
302 302
 					/* there is no To tag required, just copy paste the header */
303 303
 					s = lw_next_line(s, invite_buf_end);
304
-					append_mem_block(d, s1, s - s1);
304
+					append_str(d, s1, s - s1);
305 305
 				} else {
306 306
 					/* use the given To HF instead of the original one */
307
-					append_mem_block(d, to->s, to->len);
307
+					append_str(d, to->s, to->len);
308 308
 					/* move the pointer to the next line */
309 309
 					s = lw_next_line(s, invite_buf_end);
310 310
 				}
... ...
@@ -316,7 +316,7 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
316 316
 			case HDR_MAXFORWARDS_T:
317 317
 				/* copy hf */
318 318
 				s = lw_next_line(s, invite_buf_end);
319
-				append_mem_block(d, s1, s - s1);
319
+				append_str(d, s1, s - s1);
320 320
 				break;
321 321
 
322 322
 			case HDR_REQUIRE_T:
... ...
@@ -327,15 +327,15 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
327 327
 
328 328
 			case HDR_CONTENTLENGTH_T:
329 329
 				/* copy hf name with 0 value */
330
-				append_mem_block(d, s1, s - s1);
331
-				append_mem_block(d, ": 0" CRLF, 3 + CRLF_LEN);
330
+				append_str(d, s1, s - s1);
331
+				append_str(d, ": 0" CRLF, 3 + CRLF_LEN);
332 332
 				/* move the pointer to the next line */
333 333
 				s = lw_next_line(s, invite_buf_end);
334 334
 				break;
335 335
 
336 336
 			case HDR_EOH_T:
337 337
 				/* end of SIP message found */
338
-				append_mem_block(d, CRLF, CRLF_LEN);
338
+				append_str(d, CRLF, CRLF_LEN);
339 339
 				*len = d - cancel_buf;
340 340
 				/* LOG(L_DBG, "DBG: build_local: %.*s\n", *len, cancel_buf); */
341 341
 				return cancel_buf;
... ...
@@ -349,7 +349,7 @@ char *build_local_reparse(struct cell *Trans,unsigned int branch,
349 349
 						cfg_get(tm, tm_cfg, ac_extra_hdrs).s,
350 350
 						cfg_get(tm, tm_cfg, ac_extra_hdrs).len) == 0)
351 351
 				) {
352
-					append_mem_block(d, s1, s - s1);
352
+					append_str(d, s1, s - s1);
353 353
 				} /* else skip this line */
354 354
 				break;
355 355
 		}
... ...
@@ -444,7 +444,7 @@ static inline char* print_rs(char* p, struct rte* list, str* contact)
444 444
 	if (contact) {
445 445
 		if (list) memapp(p, ROUTE_SEPARATOR, ROUTE_SEPARATOR_LEN);
446 446
 		*p++ = '<';
447
-		append_str(p, *contact);
447
+		append_str(p, contact->s, contact->len);
448 448
 		*p++ = '>';
449 449
 	}
450 450
 	
... ...
@@ -1054,45 +1054,45 @@ char *build_dlg_ack(struct sip_msg* rpl, struct cell *Trans,
1054 1054
 	}
1055 1055
 	p = req_buf;
1056 1056
 	
1057
-	append_mem_block( p, ACK, ACK_LEN );
1058
-	append_mem_block( p, " ", 1 );
1059
-	append_str(p, ruri);
1060
-	append_mem_block( p, " " SIP_VERSION CRLF, 1 + SIP_VERSION_LEN + CRLF_LEN);
1057
+	append_str( p, ACK, ACK_LEN );
1058
+	append_str( p, " ", 1 );
1059
+	append_str(p, ruri.s, ruri.len);
1060
+	append_str( p, " " SIP_VERSION CRLF, 1 + SIP_VERSION_LEN + CRLF_LEN);
1061 1061
   	 
1062 1062
 	     /* insert our via */
1063
-	append_mem_block(p, via, via_len);
1063
+	append_str(p, via, via_len);
1064 1064
 	
1065 1065
 	     /*other headers*/
1066
-	append_str(p, Trans->from);
1067
-	append_str(p, Trans->callid);
1068
-	append_str(p, *to);
1066
+	append_str(p, Trans->from.s, Trans->from.len);
1067
+	append_str(p, Trans->callid.s, Trans->callid.len);
1068
+	append_str(p, to->s, to->len);
1069 1069
 	
1070
-	append_str(p, Trans->cseq_n);
1071
-	append_mem_block( p, " ", 1 );
1072
-	append_mem_block( p, ACK, ACK_LEN);
1073
-	append_mem_block(p, CRLF, CRLF_LEN);
1070
+	append_str(p, Trans->cseq_n.s, Trans->cseq_n.len);
1071
+	append_str( p, " ", 1 );
1072
+	append_str( p, ACK, ACK_LEN);
1073
+	append_str(p, CRLF, CRLF_LEN);
1074 1074
 	
1075 1075
 	     /* Routeset */
1076 1076
 	p = print_rs(p, list, cont);
1077 1077
 	
1078 1078
 	     /* User Agent header */
1079 1079
 	if (server_signature) {
1080
-		append_mem_block(p, USER_AGENT CRLF, USER_AGENT_LEN + CRLF_LEN);
1080
+		append_str(p, USER_AGENT CRLF, USER_AGENT_LEN + CRLF_LEN);
1081 1081
 	}
1082 1082
 	
1083 1083
 	/* extra headers */
1084 1084
 	if (hdrs)
1085
-		append_mem_block(p, hdrs->s, hdrs->len);
1085
+		append_str(p, hdrs->s, hdrs->len);
1086 1086
 	
1087 1087
 	     /* Content Length, EoH, (body) */
1088 1088
 	if (body) {
1089
-		append_mem_block(p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
1090
-		append_mem_block(p, body_len.s, body_len.len);
1091
-		append_mem_block(p, /*end crr. header*/CRLF /*EoH*/CRLF, CRLF_LEN + 
1089
+		append_str(p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
1090
+		append_str(p, body_len.s, body_len.len);
1091
+		append_str(p, /*end crr. header*/CRLF /*EoH*/CRLF, CRLF_LEN + 
1092 1092
 				CRLF_LEN);
1093
-		append_mem_block(p, body->s, body->len);
1093
+		append_str(p, body->s, body->len);
1094 1094
 	} else {
1095
-		append_mem_block(p, CONTENT_LENGTH "0" CRLF CRLF, 
1095
+		append_str(p, CONTENT_LENGTH "0" CRLF CRLF, 
1096 1096
 				CONTENT_LENGTH_LEN + 1 + CRLF_LEN + CRLF_LEN);
1097 1097
 	}
1098 1098
 
... ...
@@ -83,11 +83,11 @@ static inline int encode_branch_info(str *info, struct contact *con)
83 83
 		return 0;
84 84
     }
85 85
     at = info->s;
86
-    append_str(at, con->uri);
86
+    append_str(at, con->uri.s, con->uri.len);
87 87
     append_chr(at, '\n');
88
-    append_str(at, con->dst_uri);
88
+    append_str(at, con->dst_uri.s, con->dst_uri.len);
89 89
     append_chr(at, '\n');
90
-    append_str(at, con->path);
90
+    append_str(at, con->path.s, con->path.len);
91 91
     append_chr(at, '\n');
92 92
     if (con->sock) {
93 93
 		len = MAX_SOCKET_STR;
... ...
@@ -101,7 +101,7 @@ static inline int encode_branch_info(str *info, struct contact *con)
101 101
     at = at + len;
102 102
     append_chr(at, '\n');
103 103
     s = int2str(con->flags, &len);
104
-    append_mem_block(at, s, len);
104
+    append_str(at, s, len);
105 105
     append_chr(at, '\n');
106 106
     info->len = at - info->s + 1;
107 107
 
... ...
@@ -250,7 +250,7 @@ static inline int skip_cancel(struct sip_msg *msg)
250 250
     } while(0);
251 251
 
252 252
 
253
-#define append_str(buf, str)                  \
253
+#define str_append_str(buf, str)			  \
254 254
     do {                                      \
255 255
         memcpy((buf).s, (str).s, (str).len);  \
256 256
         (buf).s += (str).len;                 \
... ...
@@ -659,9 +659,9 @@ static int log_request(struct sip_msg* rq, str* ouri, struct hdr_field* to, str*
659 659
 
660 660
 	for (i = 0; i < attr_cnt; i++) {
661 661
 		append(buf, A_SEPARATOR);
662
-		append_str(buf, atr_arr[i]);
662
+		str_append_str(buf, atr_arr[i]);
663 663
 		append(buf, A_EQ);
664
-		append_str(buf, *(val_arr[i]))
664
+		str_append_str(buf, *(val_arr[i]))
665 665
 	}
666 666
 
667 667
 	     /* terminating text */
... ...
@@ -671,7 +671,7 @@ static int log_request(struct sip_msg* rq, str* ouri, struct hdr_field* to, str*
671 671
 	buf.s = log_msg;
672 672
 	buf.len = len;
673 673
 	append(buf, ACC);
674
-	append_str(buf, *txt);
674
+	str_append_str(buf, *txt);
675 675
 
676 676
 	LOG(log_level, "%s", log_msg);
677 677
 	pkg_free(log_msg);
... ...
@@ -98,10 +98,6 @@ struct tm_binds tmb;
98 98
 #define CONTENT_TYPE_HDR     "Content-Type: text/plain"
99 99
 #define CONTENT_TYPE_HDR_LEN (sizeof(CONTENT_TYPE_HDR)-1)
100 100
 
101
-#define append_str(_p,_s,_l) \
102
-	{memcpy((_p),(_s),(_l));\
103
-	(_p) += (_l);}
104
-
105 101
 #define is_in_sip_addr(_p) \
106 102
 	((_p)!=' ' && (_p)!='\t' && (_p)!='(' && (_p)!='[' && (_p)!='<' \
107 103
 	&& (_p)!='>' && (_p)!=']' && (_p)!=')' && (_p)!='?' && (_p)!='!' \
... ...
@@ -143,12 +143,6 @@
143 143
 #include "forward.h"
144 144
 
145 145
 
146
-#define append_str(_dest,_src,_len) \
147
-	do{\
148
-		memcpy( (_dest) , (_src) , (_len) );\
149
-		(_dest) += (_len) ;\
150
-	}while(0);
151
-
152 146
 #define append_str_trans(_dest,_src,_len,_msg) \
153 147
 	append_str( (_dest), (_src), (_len) );
154 148
 
... ...
@@ -149,6 +149,18 @@
149 149
 #endif
150 150
 
151 151
 
152
+#define append_str(_dest,_src,_len)				\
153
+	do{											\
154
+		memcpy( (_dest) , (_src) , (_len) );	\
155
+		(_dest) += (_len) ;						\
156
+	}while(0);									\
157
+
158
+	
159
+/*! append _c char to _dest string */
160
+#define append_chr(_dest,_c) \
161
+	*((_dest)++) = _c;
162
+
163
+
152 164
 /* links a value to a msgid */
153 165
 struct msgid_var{
154 166
 	union{