Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 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_check(buf,str) \ |
| 24 | do { \ |
| 25 | if ((str).slen >= (endbuf-buf)) return -1; \ |
| 26 | pj_memcpy(buf, (str).ptr, (str).slen); \ |
| 27 | buf += (str).slen; \ |
| 28 | } while (0) |
| 29 | |
| 30 | #define copy_advance_pair_check(buf,str1,len1,str2) \ |
| 31 | do { \ |
| 32 | if (str2.slen) { \ |
| 33 | printed = len1+(int)str2.slen; \ |
| 34 | if (printed >= (endbuf-buf)) return -1; \ |
| 35 | pj_memcpy(buf,str1,len1); \ |
| 36 | pj_memcpy(buf+len1, str2.ptr, str2.slen); \ |
| 37 | buf += printed; \ |
| 38 | } \ |
| 39 | } while (0) |
| 40 | |
| 41 | #define copy_advance_pair_quote_check(buf,str1,len1,str2,quotebegin,quoteend) \ |
| 42 | do { \ |
| 43 | if (str2.slen) { \ |
| 44 | printed = len1+str2.slen+2; \ |
| 45 | if (printed >= (endbuf-buf)) return -1; \ |
| 46 | pj_memcpy(buf,str1,len1); \ |
| 47 | *(buf+len1)=quotebegin; \ |
| 48 | pj_memcpy(buf+len1+1, str2.ptr, str2.slen); \ |
| 49 | *(buf+printed-1) = quoteend; \ |
| 50 | buf += printed; \ |
| 51 | } \ |
| 52 | } while (0) |
| 53 | |
| 54 | #define copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend) \ |
| 55 | do { \ |
| 56 | printed = len1+(int)str2.slen+2; \ |
| 57 | if (printed >= (endbuf-buf)) return -1; \ |
| 58 | pj_memcpy(buf,str1,len1); \ |
| 59 | *(buf+len1)=quotebegin; \ |
| 60 | pj_memcpy(buf+len1+1, str2.ptr, str2.slen); \ |
| 61 | *(buf+printed-1) = quoteend; \ |
| 62 | buf += printed; \ |
| 63 | } while (0) |
| 64 | |
| 65 | #define copy_advance_pair_escape(buf,str1,len1,str2,unres) \ |
| 66 | do { \ |
| 67 | if (str2.slen) { \ |
| 68 | if (len1+str2.slen >= (endbuf-buf)) return -1; \ |
| 69 | pj_memcpy(buf,str1,len1); \ |
| 70 | printed=(int)pj_strncpy2_escape(buf+len1,&str2,(endbuf-buf-len1), \ |
| 71 | &unres);\ |
| 72 | if (printed < 0) return -1; \ |
| 73 | buf += (printed+len1); \ |
| 74 | } \ |
| 75 | } while (0) |
| 76 | |
| 77 | |
| 78 | #define copy_advance_no_check(buf,str) \ |
| 79 | do { \ |
| 80 | pj_memcpy(buf, (str).ptr, (str).slen); \ |
| 81 | buf += (str).slen; \ |
| 82 | } while (0) |
| 83 | |
| 84 | #define copy_advance_escape(buf,str,unres) \ |
| 85 | do { \ |
| 86 | printed = \ |
| 87 | (int)pj_strncpy2_escape(buf, &(str), (endbuf-buf), &(unres)); \ |
| 88 | if (printed < 0) return -1; \ |
| 89 | buf += printed; \ |
| 90 | } while (0) |
| 91 | |
| 92 | #define copy_advance_pair_no_check(buf,str1,len1,str2) \ |
| 93 | if (str2.slen) { \ |
| 94 | pj_memcpy(buf,str1,len1); \ |
| 95 | pj_memcpy(buf+len1, str2.ptr, str2.slen); \ |
| 96 | buf += len1+str2.slen; \ |
| 97 | } |
| 98 | |
| 99 | #define copy_advance copy_advance_check |
| 100 | #define copy_advance_pair copy_advance_pair_check |
| 101 | |
| 102 | #define copy_advance_pair_quote_cond(buf,str1,len1,str2,quotebegin,quoteend) \ |
| 103 | do { \ |
| 104 | if (str2.slen && *str2.ptr!=quotebegin) \ |
| 105 | copy_advance_pair_quote(buf,str1,len1,str2,quotebegin,quoteend); \ |
| 106 | else \ |
| 107 | copy_advance_pair(buf,str1,len1,str2); \ |
| 108 | } while (0) |
| 109 | |
| 110 | /* |
| 111 | * Internal type declarations. |
| 112 | */ |
| 113 | typedef void* (*pjsip_hdr_clone_fptr)(pj_pool_t *, const void*); |
| 114 | typedef int (*pjsip_hdr_print_fptr)(void *hdr, char *buf, pj_size_t len); |
| 115 | |
| 116 | typedef struct pjsip_hdr_name_info_t |
| 117 | { |
| 118 | char *name; |
| 119 | unsigned name_len; |
| 120 | char *sname; |
| 121 | } pjsip_hdr_name_info_t; |
| 122 | |
| 123 | extern const pjsip_hdr_name_info_t pjsip_hdr_names[]; |
| 124 | |
| 125 | PJ_INLINE(void) init_hdr(void *hptr, pjsip_hdr_e htype, void *vptr) |
| 126 | { |
| 127 | pjsip_hdr *hdr = (pjsip_hdr*) hptr; |
| 128 | hdr->type = htype; |
| 129 | hdr->name.ptr = pjsip_hdr_names[htype].name; |
| 130 | hdr->name.slen = pjsip_hdr_names[htype].name_len; |
| 131 | if (pjsip_hdr_names[htype].sname) { |
| 132 | hdr->sname.ptr = pjsip_hdr_names[htype].sname; |
| 133 | hdr->sname.slen = 1; |
| 134 | } else { |
| 135 | hdr->sname = hdr->name; |
| 136 | } |
| 137 | hdr->vptr = (pjsip_hdr_vptr*) vptr; |
| 138 | pj_list_init(hdr); |
| 139 | } |
| 140 | |
| 141 | #endif /* __PJSIP_PRINT_H__ */ |
| 142 | |