rem: DBG("build_*: message=\n%.*s\n", len, buf);
add: DBG("build_*: message=\n%.*s\n", (int)len, buf);
context: char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,					     char *new_tag, unsigned int new_tag_len ,					     char *body, unsigned int body_len,					     char *content_type, unsigned int content_type_len,					     struct sip_msg* msg, unsigned int *returned_len,						 struct bookmark *bmark){	char              *buf, *p;	unsigned int      len,foo;	struct hdr_field  *hdr;	struct lump_rpl   *lump;	int               i;	char              backup;	char              *received_buf;	char              *rport_buf;	unsigned int      received_len;	unsigned int      rport_len;	unsigned int      delete_len;	char              *warning;	unsigned int      warning_len;	unsigned int	  text_len;	int r;	int content_len_len;	char *content_len;	char *after_body;	str to_tag;	char *totags;	received_buf=0;	received_len=0;	rport_buf=0;	rport_len=0;	delete_len=0;	buf=0;	/* make -Wall happy */	warning=0;	content_len=0;	text_len=strlen(text);	/* force parsing all headers -- we want to return all	Via's in the reply and they may be scattered down to the	end of header (non-block Vias are a really poor property	of SIP :( ) */	if (parse_headers( msg, HDR_EOH, 0 )==-1) {		LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "			"alas, parse_headers failed\n");		goto error00;	}	/* check if received needs to be added */	backup = msg->via1->host.s[msg->via1->host.len];	msg->via1->host.s[msg->via1->host.len] = 0;	r=check_via_address(&msg->rcv.src_ip, &msg->via1->host, 		msg->via1->port, received_dns);	msg->via1->host.s[msg->via1->host.len] = backup;	if (r!=0) {		if ((received_buf=received_builder(msg,&received_len))==0) {			LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "				"alas, received_builder failed\n");			goto error00;		}	}	/* check if rport needs to be updated */	if (msg->via1->rport && msg->via1->rport->value.s==0){		if ((rport_buf=rport_builder(msg, &rport_len))==0){			LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"							" rport_builder failed\n");			goto error01; /* free everything */		}		delete_len=msg->via1->rport->size+1; /* include ';' */	}	/*computes the lenght of the new response buffer*/	len = 0;	/* first line */	len += SIP_VERSION_LEN + 1/*space*/ + 3/*code*/ + 1/*space*/ +		text_len + CRLF_LEN/*new line*/;	/*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/	for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {		if (hdr->type==HDR_TO) {			if (new_tag)			{				to_tag=get_to(msg)->tag_value;				if (to_tag.s )					len+=new_tag_len-to_tag.len;				else					len+=new_tag_len+TOTAG_TOKEN_LEN/*";tag="*/;			}			else {				len+=hdr->len;				continue;			}		} else if (hdr->type==HDR_VIA) {				/* we always add CRLF to via*/				len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;				if (hdr==msg->h_via1) len += received_len+rport_len;				continue;		} else if (hdr->type==HDR_RECORDROUTE) {				/* RR only for 1xx and 2xx replies */				if (code<180 || code>=300) continue;		} else if (!(hdr->type==HDR_FROM 					|| hdr->type==HDR_CALLID					|| hdr->type==HDR_CSEQ)) {			continue;		}		len += hdr->len; /* we keep the original termination for these 							headers*/	}	len-=delete_len;	/*lumps length*/	for(lump=msg->reply_lump;lump;lump=lump->next)		len += lump->text.len;	if (server_signature) {		/*server header*/		len += SERVER_HDR_LEN + CRLF_LEN;	}	if (body_len) {		content_len=int2str(body_len, &content_len_len);		len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;	    len += body_len;	} else {		len +=CONTENT_LENGTH_LEN+1 + CRLF_LEN;	}	if(content_type_len) {	    len += content_type_len + CRLF_LEN;	}	if (sip_warning) {		warning = warning_builder(msg,&warning_len);		if (warning) len += warning_len + CRLF_LEN;		else LOG(L_WARN, "WARNING: warning skipped -- too big\n");	}	/* end of message */	len += CRLF_LEN; /*new line*/	/*allocating mem*/	buf = (char*) pkg_malloc( len+1 );	if (!buf)	{		LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "			" ; needs %d\n",len);		goto error01;	}	/* filling the buffer*/	p=buf;	/* first line */	memcpy( p , SIP_VERSION , SIP_VERSION_LEN );	p += SIP_VERSION_LEN;	*(p++) = ' ' ;	/*code*/	for ( i=2 , foo = code  ;  i>=0  ;  i-- , foo=foo/10 )		*(p+i) = '0' + foo - ( foo/10 )*10;	p += 3;	*(p++) = ' ' ;	memcpy( p , text , text_len );	p += text_len;	memcpy( p, CRLF, CRLF_LEN );	p+=CRLF_LEN;	/* headers*/	for ( hdr=msg->headers ; hdr ; hdr=hdr->next )		switch (hdr->type)		{			case HDR_VIA:				if (hdr==msg->h_via1){					if (rport_buf){						/* copy until rport */						append_str_trans( p, hdr->name.s ,							msg->via1->rport->start-hdr->name.s-1,msg);						/* copy new rport */						append_str(p, rport_buf, rport_len);						/* copy the rest of the via */						append_str_trans(p, msg->via1->rport->start+											msg->via1->rport->size, 											hdr->body.s+hdr->body.len-											msg->via1->rport->start-											msg->via1->rport->size, msg);					}else{						/* normal whole via copy */						append_str_trans( p, hdr->name.s , 								(hdr->body.s+hdr->body.len)-hdr->name.s, msg);					}					if (received_buf)						append_str( p, received_buf, received_len);				}else{					/* normal whole via copy */					append_str_trans( p, hdr->name.s,							(hdr->body.s+hdr->body.len)-hdr->name.s, msg);				}				append_str( p, CRLF,CRLF_LEN);				break;			case HDR_RECORDROUTE:				/* RR only for 1xx and 2xx replies */				if (code<180 || code>=300) break;				append_str(p, hdr->name.s, hdr->len);				break;			case HDR_TO:				if (new_tag){					if (to_tag.s ) { /* replacement */						/* before to-tag */						append_str( p, hdr->name.s, to_tag.s-hdr->name.s);						/* to tag replacement */						bmark->to_tag_val.s=p;						bmark->to_tag_val.len=new_tag_len;						append_str( p, new_tag,new_tag_len);						/* the rest after to-tag */						append_str( p, to_tag.s+to_tag.len,							hdr->name.s+hdr->len-(to_tag.s+to_tag.len));					}else{ /* adding a new to-tag */						after_body=hdr->body.s+hdr->body.len;						append_str( p, hdr->name.s, after_body-hdr->name.s);						append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);						bmark->to_tag_val.s=p;						bmark->to_tag_val.len=new_tag_len;						append_str( p, new_tag,new_tag_len);						append_str( p, after_body, 										hdr->name.s+hdr->len-after_body);					}					break;				} /* no new to-tag -- proceed to 1:1 copying  */				totags=((struct to_body*)(hdr->parsed))->tag_value.s;				if (totags) {					bmark->to_tag_val.s=p+(totags-hdr->name.s);					bmark->to_tag_val.len=							((struct to_body*)(hdr->parsed))->tag_value.len;				};			case HDR_FROM:			case HDR_CALLID:			case HDR_CSEQ:					append_str(p, hdr->name.s, hdr->len);		} /* for switch */	/*lumps*/	for(lump=msg->reply_lump;lump;lump=lump->next)	{		memcpy(p,lump->text.s,lump->text.len);		p += lump->text.len;	}	if (server_signature) {		/*server header*/		memcpy( p, SERVER_HDR , SERVER_HDR_LEN );		p+=SERVER_HDR_LEN;		memcpy( p, CRLF, CRLF_LEN );		p+=CRLF_LEN;	}		if (body_len) {		memcpy(p, CONTENT_LENGTH, CONTENT_LENGTH_LEN );		p+=CONTENT_LENGTH_LEN;		memcpy( p, content_len, content_len_len );		p+=content_len_len;		memcpy( p, CRLF, CRLF_LEN );		p+=CRLF_LEN;	} else {		/* content length header*/		memcpy( p, CONTENT_LENGTH "0" CRLF, CONTENT_LENGTH_LEN+1+CRLF_LEN );		p+=CONTENT_LENGTH_LEN+1+CRLF_LEN;	}	if(content_type_len){	    memcpy( p, content_type, content_type_len );	    p+=content_type_len;	    memcpy( p, CRLF, CRLF_LEN );	    p+=CRLF_LEN;	}	if (sip_warning && warning) {		memcpy( p, warning, warning_len);		p+=warning_len;		memcpy( p, CRLF, CRLF_LEN);		p+=CRLF_LEN;	}	/*end of message*/	memcpy( p, CRLF, CRLF_LEN );	p+=CRLF_LEN;	if(body_len){	    memcpy ( p, body, body_len );	    p+=body_len;	}	*(p) = 0;	*returned_len = len;	DBG("build_*: len=%d, diff=%d\n", len, p-buf);	DBG("build_*: rport_len=%d, delete_len=%d\n", rport_len, delete_len);	DBG("build_*: message=\n%.*s\n", len, buf);	/* in req2reply, received_buf is not introduced to lumps and	   needs to be deleted here	*/	if (received_buf) pkg_free(received_buf);	if (rport_buf) pkg_free(rport_buf);	return buf;error01:	if (received_buf) pkg_free(received_buf);	if (rport_buf) pkg_free(rport_buf);error00:	*returned_len=0;	return 0;}