print_util.h 5.5 KB

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