param_parser.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * 32-bit Digest parameter 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. #include "param_parser.h"
  28. #include "digest_keys.h"
  29. #include "../../trim.h"
  30. #include "../../ut.h"
  31. #define LOWER_BYTE(b) ((b) | 0x20)
  32. #define LOWER_DWORD(d) ((d) | 0x20202020)
  33. /*
  34. * Parse short (less than 4 bytes) parameter names
  35. */
  36. #define PARSE_SHORT \
  37. switch(LOWER_BYTE(*p)) { \
  38. case 'u': \
  39. if (LOWER_BYTE(*(p + 1)) == 'r') { \
  40. if (LOWER_BYTE(*(p + 2)) == 'i') { \
  41. *_type = PAR_URI; \
  42. p += 3; \
  43. goto end; \
  44. } \
  45. } \
  46. break; \
  47. \
  48. case 'q': \
  49. if (LOWER_BYTE(*(p + 1)) == 'o') { \
  50. if (LOWER_BYTE(*(p + 2)) == 'p') { \
  51. *_type = PAR_QOP; \
  52. p += 3; \
  53. goto end; \
  54. } \
  55. } \
  56. break; \
  57. \
  58. case 'n': \
  59. if (LOWER_BYTE(*(p + 1)) == 'c') { \
  60. *_type = PAR_NC; \
  61. p += 2; \
  62. goto end; \
  63. } \
  64. break; \
  65. }
  66. /*
  67. * Read 4-bytes from memory and store them in an integer variable
  68. * Reading byte by byte ensures, that the code works also on HW which
  69. * does not allow reading 4-bytes at once from unaligned memory position
  70. * (Sparc for example)
  71. */
  72. #define READ(val) \
  73. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
  74. #define name_CASE \
  75. switch(LOWER_DWORD(val)) { \
  76. case _name_: \
  77. *_type = PAR_USERNAME; \
  78. p += 4; \
  79. goto end; \
  80. }
  81. #define user_CASE \
  82. p += 4; \
  83. val = READ(p); \
  84. name_CASE; \
  85. goto other;
  86. #define real_CASE \
  87. p += 4; \
  88. if (LOWER_BYTE(*p) == 'm') { \
  89. *_type = PAR_REALM; \
  90. p++; \
  91. goto end; \
  92. }
  93. #define nonc_CASE \
  94. p += 4; \
  95. if (LOWER_BYTE(*p) == 'e') { \
  96. *_type = PAR_NONCE; \
  97. p++; \
  98. goto end; \
  99. }
  100. #define onse_CASE \
  101. switch(LOWER_DWORD(val)) { \
  102. case _onse_: \
  103. *_type = PAR_RESPONSE; \
  104. p += 4; \
  105. goto end; \
  106. }
  107. #define resp_CASE \
  108. p += 4; \
  109. val = READ(p); \
  110. onse_CASE; \
  111. goto other;
  112. #define cnon_CASE \
  113. p += 4; \
  114. if (LOWER_BYTE(*p) == 'c') { \
  115. p++; \
  116. if (LOWER_BYTE(*p) == 'e') { \
  117. *_type = PAR_CNONCE; \
  118. p++; \
  119. goto end; \
  120. } \
  121. } \
  122. goto other;
  123. #define opaq_CASE \
  124. p += 4; \
  125. if (LOWER_BYTE(*p) == 'u') { \
  126. p++; \
  127. if (LOWER_BYTE(*p) == 'e') { \
  128. *_type = PAR_OPAQUE; \
  129. p++; \
  130. goto end; \
  131. } \
  132. } \
  133. goto other;
  134. #define rith_CASE \
  135. switch(LOWER_DWORD(val)) { \
  136. case _rith_: \
  137. p += 4; \
  138. if (LOWER_BYTE(*p) == 'm') { \
  139. *_type = PAR_ALGORITHM; \
  140. p++; \
  141. goto end; \
  142. } \
  143. goto other; \
  144. }
  145. #define algo_CASE \
  146. p += 4; \
  147. val = READ(p); \
  148. rith_CASE; \
  149. goto other
  150. #define FIRST_QUATERNIONS \
  151. case _user_: user_CASE; \
  152. case _real_: real_CASE; \
  153. case _nonc_: nonc_CASE; \
  154. case _resp_: resp_CASE; \
  155. case _cnon_: cnon_CASE; \
  156. case _opaq_: opaq_CASE; \
  157. case _algo_: algo_CASE;
  158. int parse_param_name(str* _s, dig_par_t* _type)
  159. {
  160. register char* p;
  161. register int val;
  162. char* end;
  163. end = _s->s + _s->len;
  164. p = _s->s;
  165. val = READ(p);
  166. if (_s->len < 4) {
  167. goto other;
  168. }
  169. switch(LOWER_DWORD(val)) {
  170. FIRST_QUATERNIONS;
  171. default:
  172. PARSE_SHORT;
  173. goto other;
  174. }
  175. end:
  176. _s->len -= p - _s->s;
  177. _s->s = p;
  178. trim_leading(_s);
  179. if (_s->s[0] == '=') {
  180. return 0;
  181. }
  182. other:
  183. p = q_memchr(p, '=', end - p);
  184. if (!p) {
  185. return -1; /* Parse error */
  186. } else {
  187. *_type = PAR_OTHER;
  188. _s->len -= p - _s->s;
  189. _s->s = p;
  190. return 0;
  191. }
  192. }