1 /* $Id$ */
2 /*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #ifndef __PJSIP_PRINT_H__
21 #define __PJSIP_PRINT_H__
22
23 #define copy_advance_char_check(buf,chr) \
24 do { \
25 if (1 >= (endbuf-buf)) return -1; \
26 *buf++ = chr; \
27 } while (0)
28
29 #define copy_advance_check(buf,str) \
30 do { \
31 if ((str).slen >= (endbuf-buf)) return -1; \
32 if ((str).slen) { \
33 pj_memcpy(buf, (str).ptr, (str).slen); \
34 buf += (str).slen; \
35 } \
36 } while (0)
37
38 #define copy_advance_pair_check(buf,str1,len1,str2) \
39 do { \
40 if (str2.slen) { \
41 printed = len1+(int)str2.slen; \
42 if (printed >= (endbuf-buf)) return -1; \
43 pj_memcpy(buf,str1,len1); \
44 pj_memcpy(buf+len1, str2.ptr, str2.slen); \
45 buf += printed; \
46 } \
47 } while (0)
48
49 #define copy_advance_pair_quote_check(buf,str1,len1,str2,quotebegin,quoteend) \
50 do { \
51 if (str2.slen) { \
52 printed = len1+str2.slen+2; \
53 if (printed >= (endbuf-buf)) return -1; \
54 pj_memcpy(buf,str1,len1); \
55 *(buf+len1)=quotebegin; \
56 pj_memcpy(buf+len1+1, str2.ptr, str2.slen); \
57 *(buf+printed-1) = quoteend; \
58 buf += printed; \
59 } \
60 } while (0)
61
62 #define copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend) \
63 do { \
64 printed = len1+(int)str2.slen+2; \
65 if (printed >= (endbuf-buf)) return -1; \
66 pj_memcpy(buf,str1,len1); \
67 *(buf+len1)=quotebegin; \
68 pj_memcpy(buf+len1+1, str2.ptr, str2.slen); \
69 *(buf+printed-1) = quoteend; \
70 buf += printed; \
71 } while (0)
72
73 #define copy_advance_pair_escape(buf,str1,len1,str2,unres) \
74 do { \
75 if (str2.slen) { \
76 if (len1+str2.slen >= (endbuf-buf)) return -1; \
77 pj_memcpy(buf,str1,len1); \
78 printed=(int)pj_strncpy2_escape(buf+len1,&str2,(endbuf-buf-len1), \
79 &unres);\
80 if (printed < 0) return -1; \
81 buf += (printed+len1); \
82 } \
83 } while (0)
84
85
86 #define copy_advance_no_check(buf,str) \
87 do { \
88 pj_memcpy(buf, (str).ptr, (str).slen); \
89 buf += (str).slen; \
90 } while (0)
91
92 #define copy_advance_escape(buf,str,unres) \
93 do { \
94 printed = \
95 (int)pj_strncpy2_escape(buf, &(str), (endbuf-buf), &(unres)); \
96 if (printed < 0) return -1; \
97 buf += printed; \
98 } while (0)
99
100 #define copy_advance_pair_no_check(buf,str1,len1,str2) \
101 if (str2.slen) { \
102 pj_memcpy(buf,str1,len1); \
103 pj_memcpy(buf+len1, str2.ptr, str2.slen); \
104 buf += len1+str2.slen; \
105 }
106
107 #define copy_advance copy_advance_check
108 #define copy_advance_pair copy_advance_pair_check
109
110 /*
111 * Append str1 and quoted str2 and copy to buf.
112 * No string is copied if str2 is empty.
113 */
114 #define copy_advance_pair_quote_cond(buf,str1,len1,str2,quotebegin,quoteend) \
115 do { \
116 if (str2.slen && *str2.ptr!=quotebegin) \
117 copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend); \
118 else \
119 copy_advance_pair(buf,str1,len1,str2); \
120 } while (0)
121
122 /*
123 * Append str1 and quoted str2 and copy to buf.
124 * In case str2 is empty, str1 will be appended with empty quote.
125 */
126 #define copy_advance_pair_quote_cond_always(buf,str1,len1,str2,quotebegin, \
127 quoteend)\
128 do { \
129 if (!str2.slen) \
130 copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend); \
131 else \
132 copy_advance_pair_quote_cond(buf,str1,len1,str2,quotebegin,quoteend);\
133 } while (0)
134
135 /*
136 * Internal type declarations.
137 */
138 typedef void* (*pjsip_hdr_clone_fptr)(pj_pool_t *, const void*);
139 typedef int (*pjsip_hdr_print_fptr)(void *hdr, char *buf, pj_size_t len);
140
141 typedef struct pjsip_hdr_name_info_t
142 {
143 char *name;
144 unsigned name_len;
145 char *sname;
146 } pjsip_hdr_name_info_t;
147
148 extern const pjsip_hdr_name_info_t pjsip_hdr_names[];
149
init_hdr(void * hptr,pjsip_hdr_e htype,void * vptr)150 PJ_INLINE(void) init_hdr(void *hptr, pjsip_hdr_e htype, void *vptr)
151 {
152 pjsip_hdr *hdr = (pjsip_hdr*) hptr;
153 hdr->type = htype;
154 hdr->name.ptr = pjsip_hdr_names[htype].name;
155 hdr->name.slen = pjsip_hdr_names[htype].name_len;
156 if (pjsip_hdr_names[htype].sname) {
157 hdr->sname.ptr = pjsip_hdr_names[htype].sname;
158 hdr->sname.slen = 1;
159 } else {
160 hdr->sname = hdr->name;
161 }
162 hdr->vptr = (pjsip_hdr_vptr*) vptr;
163 pj_list_init(hdr);
164 }
165
166 #endif /* __PJSIP_PRINT_H__ */
167
168