summaryrefslogtreecommitdiff
path: root/pjsip/include/pjsip/print_util.h
blob: b1362a7207dd2aa1a0efd42dda885b06a918166f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id$ */
/* 
 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
 *
 * This program 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.
 *
 * This program 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 
 */
#ifndef __PJSIP_PRINT_H__
#define __PJSIP_PRINT_H__

/* Minimum space left in the buffer */
#define MIN_SPACE	10

#define copy_advance_check(buf,str)   \
	do { \
	    if ((str).slen+MIN_SPACE >= (endbuf-buf)) return -1;	\
	    pj_memcpy(buf, (str).ptr, (str).slen); \
	    buf += (str).slen; \
	} while (0)

/*
static char *imp_copy_advance_pair(char *buf, char *endbuf, const char *str1, int len1, const pj_str_t *str2)
{
    if (str2->slen) {
	int printed = len1+str2->slen;
	if (printed+MIN_SPACE >= (endbuf-buf)) return NULL;
	pj_memcpy(buf,str1,len1);
	pj_memcpy(buf+len1, str2->ptr, str2->slen);
	return buf + printed;
    } else
	return buf;
}
*/

#define copy_advance_pair_check(buf,str1,len1,str2)   \
	do { \
	    if (str2.slen) { \
		printed = len1+str2.slen; \
		if (printed+MIN_SPACE >= (endbuf-buf)) return -1;	\
		pj_memcpy(buf,str1,len1); \
		pj_memcpy(buf+len1, str2.ptr, str2.slen); \
		buf += printed; \
	    } \
	} while (0)
/*
#define copy_advance_pair(buf,str1,len1,str2)   \
	do { \
	    buf = imp_copy_advance_pair(buf, endbuf, str1, len1, &str2); \
	    if (buf == NULL) return -1; \
	} while (0)
*/

#define copy_advance_pair_quote_check(buf,str1,len1,str2,quotebegin,quoteend) \
	do { \
	    if (str2.slen) { \
		printed = len1+str2.slen+2; \
		if (printed+MIN_SPACE >= (endbuf-buf)) return -1;	\
		pj_memcpy(buf,str1,len1); \
		*(buf+len1)=quotebegin; \
		pj_memcpy(buf+len1+1, str2.ptr, str2.slen); \
		*(buf+printed-1) = quoteend; \
		buf += printed; \
	    } \
	} while (0)

#define copy_advance_pair_escape(buf,str1,len1,str2,unres)	\
	do { \
	    if (str2.slen) { \
		pj_ssize_t esc_len; \
		if (len1+str2.slen+MIN_SPACE >= (endbuf-buf)) return -1; \
		pj_memcpy(buf,str1,len1); \
		buf += len1; \
		esc_len=pj_strncpy2_escape(buf, &str2, (endbuf-buf), &unres); \
		if (esc_len < 0) return -1; \
		buf += esc_len; \
		if (endbuf-buf < MIN_SPACE) return -1; \
	    } \
	} while (0)


#define copy_advance_no_check(buf,str)   \
	do { \
	    pj_memcpy(buf, (str).ptr, (str).slen); \
	    buf += (str).slen; \
	} while (0)

#define copy_advance_escape(buf,str,unres)    \
	do { \
	    pj_ssize_t len = \
		pj_strncpy2_escape(buf, &(str), (endbuf-buf), &(unres)); \
	    if (len < 0) return -1; \
	    buf += len; \
	    if (endbuf-buf < MIN_SPACE) return -1; \
	} while (0)

#define copy_advance_pair_no_check(buf,str1,len1,str2)   \
	if (str2.slen) { \
	    pj_memcpy(buf,str1,len1); \
	    pj_memcpy(buf+len1, str2.ptr, str2.slen); \
	    buf += len1+str2.slen; \
	}

#define copy_advance 		copy_advance_check
#define copy_advance_pair 	copy_advance_pair_check
#define copy_advance_pair_quote	copy_advance_pair_quote_check

#define copy_advance_pair_quote_cond(buf,str1,len1,str2,quotebegin,quoteend) \
	do {	\
	    if (str2.slen && *str2.ptr!=quotebegin) \
		copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend); \
	    else \
		copy_advance_pair(buf,str1,len1,str2); \
	} while (0)

/*
 * Internal type declarations.
 */
typedef void* (*pjsip_hdr_clone_fptr)(pj_pool_t *, const void*);
typedef int   (*pjsip_hdr_print_fptr)(void *hdr, char *buf, pj_size_t len);

extern const pj_str_t pjsip_hdr_names[];

PJ_INLINE(void) init_hdr(void *hptr, pjsip_hdr_e htype, void *vptr)
{
    pjsip_hdr *hdr = hptr;
    hdr->type = htype;
    hdr->name = hdr->sname = pjsip_hdr_names[htype];
    hdr->vptr = vptr;
    pj_list_init(hdr);
}

#endif	/* __PJSIP_PRINT_H__ */