parse_hname2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Fast 32-bit Header Field Name Parser
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  30. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  31. * 2003-05-01 added support for Accept HF (janakj)
  32. * 2007-01-26 Date, Identity, Identity_info HF support added (gergo)
  33. * 2007-07-27 added support for Retry-After (andrei)
  34. */
  35. /** Parser :: Fast 32-bit Header Field Name Parser.
  36. * @file
  37. * @ingroup parser
  38. */
  39. #include "../comp_defs.h"
  40. #include "parse_hname2.h"
  41. #include "keys.h"
  42. #include "../ut.h" /* q_memchr */
  43. #define LOWER_BYTE(b) ((b) | 0x20)
  44. #define LOWER_DWORD(d) ((d) | 0x20202020)
  45. /** Skip all white-chars and return position of the first non-white char.
  46. */
  47. static inline char* skip_ws(char* p, unsigned int size)
  48. {
  49. char* end;
  50. end = p + size;
  51. for(; p < end; p++) {
  52. if ((*p != ' ') && (*p != '\t')) return p;
  53. }
  54. return p;
  55. }
  56. /*! \name
  57. * Parser macros
  58. */
  59. /*@{ */
  60. #include "case_via.h" /* Via */
  61. #include "case_from.h" /* From */
  62. #include "case_to.h" /* To */
  63. #include "case_cseq.h" /* CSeq */
  64. #include "case_call.h" /* Call-ID */
  65. #include "case_cont.h" /* Contact, Content-Type, Content-Length, Content-Purpose,
  66. * Content-Action, Content-Disposition */
  67. #include "case_rout.h" /* Route */
  68. #include "case_max.h" /* Max-Forwards */
  69. #include "case_reco.h" /* Record-Route */
  70. #include "case_auth.h" /* Authorization */
  71. #include "case_expi.h" /* Expires */
  72. #include "case_prox.h" /* Proxy-Authorization, Proxy-Require */
  73. #include "case_allo.h" /* Allow */
  74. #include "case_unsu.h" /* Unsupported */
  75. #include "case_even.h" /* Event */
  76. #include "case_sip.h" /* Sip-If-Match */
  77. #include "case_acce.h" /* Accept, Accept-Language */
  78. #include "case_orga.h" /* Organization */
  79. #include "case_prio.h" /* Priority */
  80. #include "case_subj.h" /* Subject */
  81. #include "case_user.h" /* User-Agent */
  82. #include "case_serv.h" /* Server */
  83. #include "case_supp.h" /* Supported */
  84. #include "case_dive.h" /* Diversion */
  85. #include "case_remo.h" /* Remote-Party-ID */
  86. #include "case_refe.h" /* Refer-To */
  87. #include "case_sess.h" /* Session-Expires */
  88. #include "case_reje.h" /* Reject-Contact */
  89. #include "case_min.h" /* Min-SE */
  90. #include "case_subs.h" /* Subscription-State */
  91. #include "case_requ.h" /* Require */
  92. #include "case_www.h" /* WWW-Authenticate */
  93. #include "case_date.h" /* Date */
  94. #include "case_iden.h" /* Identity, Identity-info */
  95. #include "case_retr.h" /* Retry-After */
  96. #include "case_path.h" /* Path */
  97. #include "case_priv.h"
  98. #include "case_reas.h" /* Reason */
  99. #include "case_p_as.h" /* P-Asserted-Identity */
  100. #include "case_p_pr.h" /* P-Preferred-Identity */
  101. /*@} */
  102. #define READ(val) \
  103. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
  104. #define READ3(val) \
  105. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16))
  106. #define FIRST_QUATERNIONS \
  107. case _via1_: via1_CASE; \
  108. case _from_: from_CASE; \
  109. case _to12_: to12_CASE; \
  110. case _cseq_: cseq_CASE; \
  111. case _call_: call_CASE; \
  112. case _cont_: cont_CASE; \
  113. case _rout_: rout_CASE; \
  114. case _max__: max_CASE; \
  115. case _reco_: reco_CASE; \
  116. case _via2_: via2_CASE; \
  117. case _auth_: auth_CASE; \
  118. case _supp_: supp_CASE; \
  119. case _expi_: expi_CASE; \
  120. case _prox_: prox_CASE; \
  121. case _allo_: allo_CASE; \
  122. case _unsu_: unsu_CASE; \
  123. case _even_: even_CASE; \
  124. case _sip_ : sip_CASE; \
  125. case _acce_: acce_CASE; \
  126. case _orga_: orga_CASE; \
  127. case _prio_: prio_CASE; \
  128. case _subj_: subj_CASE; \
  129. case _subs_: subs_CASE; \
  130. case _user_: user_CASE; \
  131. case _serv_: serv_CASE; \
  132. case _dive_: dive_CASE; \
  133. case _remo_: remo_CASE; \
  134. case _refe_: refe_CASE; \
  135. case _sess_: sess_CASE; \
  136. case _reje_: reje_CASE; \
  137. case _min__: min_CASE; \
  138. case _requ_: requ_CASE; \
  139. case _www__: www_CASE; \
  140. case _date_: date_CASE; \
  141. case _iden_: iden_CASE; \
  142. case _retr_: retr_CASE; \
  143. case _path_: path_CASE; \
  144. case _priv_: priv_CASE; \
  145. case _reas_: reas_CASE; \
  146. case _p_as_: p_as_CASE; \
  147. case _p_pr_: p_pr_CASE;
  148. #define PARSE_COMPACT(id) \
  149. switch(*(p + 1)) { \
  150. case ' ': \
  151. hdr->type = id; \
  152. p += 2; \
  153. goto dc_end; \
  154. \
  155. case ':': \
  156. hdr->type = id; \
  157. hdr->name.len = 1; \
  158. return (p + 2); \
  159. }
  160. char* parse_hname2(char* const begin, const char* const end, struct hdr_field* const hdr)
  161. {
  162. register char* p;
  163. register unsigned int val;
  164. if ((end - begin) < 4) {
  165. hdr->type = HDR_ERROR_T;
  166. return begin;
  167. }
  168. p = begin;
  169. val = LOWER_DWORD(READ(p));
  170. hdr->name.s = begin;
  171. switch(val) {
  172. FIRST_QUATERNIONS;
  173. default:
  174. switch(LOWER_BYTE(*p)) {
  175. case 't':
  176. switch(LOWER_BYTE(*(p + 1))) {
  177. case 'o':
  178. case ' ':
  179. hdr->type = HDR_TO_T;
  180. p += 2;
  181. goto dc_end;
  182. case ':':
  183. hdr->type = HDR_TO_T;
  184. hdr->name.len = 1;
  185. return (p + 2);
  186. }
  187. break;
  188. case 'v': PARSE_COMPACT(HDR_VIA_T); break;
  189. case 'f': PARSE_COMPACT(HDR_FROM_T); break;
  190. case 'i': PARSE_COMPACT(HDR_CALLID_T); break;
  191. case 'm': PARSE_COMPACT(HDR_CONTACT_T); break;
  192. case 'l': PARSE_COMPACT(HDR_CONTENTLENGTH_T); break;
  193. case 'k': PARSE_COMPACT(HDR_SUPPORTED_T); break;
  194. case 'c': PARSE_COMPACT(HDR_CONTENTTYPE_T); break;
  195. case 'o': PARSE_COMPACT(HDR_EVENT_T); break;
  196. case 'x': PARSE_COMPACT(HDR_SESSIONEXPIRES_T);break;
  197. case 'a': PARSE_COMPACT(HDR_ACCEPTCONTACT_T); break;
  198. case 'u': PARSE_COMPACT(HDR_ALLOWEVENTS_T); break;
  199. case 'e': PARSE_COMPACT(HDR_CONTENTENCODING_T); break;
  200. case 'b': PARSE_COMPACT(HDR_REFERREDBY_T); break;
  201. case 'j': PARSE_COMPACT(HDR_REJECTCONTACT_T); break;
  202. case 'd': PARSE_COMPACT(HDR_REQUESTDISPOSITION_T); break;
  203. case 's': PARSE_COMPACT(HDR_SUBJECT_T); break;
  204. case 'r': PARSE_COMPACT(HDR_REFER_TO_T); break;
  205. case 'y': PARSE_COMPACT(HDR_IDENTITY_T); break;
  206. case 'n': PARSE_COMPACT(HDR_IDENTITY_INFO_T); break;
  207. }
  208. goto other;
  209. }
  210. /* Double colon hasn't been found yet */
  211. dc_end:
  212. p = skip_ws(p, end - p);
  213. if (*p != ':') {
  214. goto other;
  215. } else {
  216. hdr->name.len = p - hdr->name.s;
  217. return (p + 1);
  218. }
  219. /* Unknown header type */
  220. other:
  221. p = q_memchr(p, ':', end - p);
  222. if (!p) { /* No double colon found, error.. */
  223. hdr->type = HDR_ERROR_T;
  224. hdr->name.s = 0;
  225. hdr->name.len = 0;
  226. return 0;
  227. } else {
  228. hdr->type = HDR_OTHER_T;
  229. hdr->name.len = p - hdr->name.s;
  230. /*hdr_update_type(hdr);*/
  231. return (p + 1);
  232. }
  233. }