Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_parser_cfg.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library 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 GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #include <osipparser2/osip_port.h>
22 #include <osipparser2/osip_parser.h>
23 #include "parser.h"
24
25 #ifndef USE_GPERF
26
27 static __osip_message_config_t pconfig[NUMBER_OF_HEADERS];
28
29 /*
30   list of compact header:
31   i: Call-ID   => ok
32   m: Contact   => ok
33   e: Content-Encoding   => ok
34   l: Content-Length   => ok
35   c: Content-Type   => ok
36   f: From   => ok
37   s: Subject   => NOT A SUPPORTED HEADER! will be
38                  available in the list of unknown headers
39   t: To   => ok
40   v: Via   => ok
41 */
42 /* This method must be called before using the parser */
43 int
44 parser_init ()
45 {
46
47   pconfig[0].hname = ACCEPT;
48   pconfig[0].setheader = (&osip_message_set_accept);
49   pconfig[1].hname = ACCEPT_ENCODING;
50   pconfig[1].setheader = (&osip_message_set_accept_encoding);
51   pconfig[2].hname = ACCEPT_LANGUAGE;
52   pconfig[2].setheader = (&osip_message_set_accept_language);
53   pconfig[3].hname = ALERT_INFO;
54   pconfig[3].setheader = (&osip_message_set_alert_info);
55   pconfig[4].hname = ALLOW;
56   pconfig[4].setheader = (&osip_message_set_allow);
57   pconfig[5].hname = AUTHORIZATION;
58   pconfig[5].setheader = (&osip_message_set_authorization);
59   pconfig[6].hname = CONTENT_TYPE_SHORT;        /* "l" */
60   pconfig[6].setheader = (&osip_message_set_content_type);
61   pconfig[7].hname = CALL_ID;
62   pconfig[7].setheader = (&osip_message_set_call_id);
63   pconfig[8].hname = CALL_INFO;
64   pconfig[8].setheader = (&osip_message_set_call_info);
65   pconfig[9].hname = CONTACT;
66   pconfig[9].setheader = (&osip_message_set_contact);
67   pconfig[10].hname = CONTENT_ENCODING;
68   pconfig[10].setheader = (&osip_message_set_content_encoding);
69   pconfig[11].hname = CONTENT_LENGTH;
70   pconfig[11].setheader = (&osip_message_set_content_length);
71   pconfig[12].hname = CONTENT_TYPE;
72   pconfig[12].setheader = (&osip_message_set_content_type);
73   pconfig[13].hname = CSEQ;
74   pconfig[13].setheader = (&osip_message_set_cseq);
75   pconfig[14].hname = CONTENT_ENCODING_SHORT;   /* "e" */
76   pconfig[14].setheader = (&osip_message_set_content_encoding);
77   pconfig[15].hname = ERROR_INFO;
78   pconfig[15].setheader = (&osip_message_set_error_info);
79   pconfig[16].hname = FROM_SHORT;       /* "f" */
80   pconfig[16].setheader = (&osip_message_set_from);
81   pconfig[17].hname = FROM;
82   pconfig[17].setheader = (&osip_message_set_from);
83   pconfig[18].hname = CALL_ID_SHORT;    /* "i" */
84   pconfig[18].setheader = (&osip_message_set_call_id);
85   pconfig[19].hname = CONTENT_LENGTH_SHORT;     /* "l" */
86   pconfig[19].setheader = (&osip_message_set_content_length);
87   pconfig[20].hname = CONTACT_SHORT;    /* "m" */
88   pconfig[20].setheader = (&osip_message_set_contact);
89   pconfig[21].hname = MIME_VERSION;
90   pconfig[21].setheader = (&osip_message_set_mime_version);
91   pconfig[22].hname = PROXY_AUTHENTICATE;
92   pconfig[22].setheader = (&osip_message_set_proxy_authenticate);
93   pconfig[23].hname = PROXY_AUTHORIZATION;
94   pconfig[23].setheader = (&osip_message_set_proxy_authorization);
95   pconfig[24].hname = RECORD_ROUTE;
96   pconfig[24].setheader = (&osip_message_set_record_route);
97   pconfig[25].hname = ROUTE;
98   pconfig[25].setheader = (&osip_message_set_route);
99   pconfig[26].hname = TO_SHORT;
100   pconfig[26].setheader = (&osip_message_set_to);
101   pconfig[27].hname = TO;
102   pconfig[27].setheader = (&osip_message_set_to);
103   pconfig[28].hname = VIA_SHORT;
104   pconfig[28].setheader = (&osip_message_set_via);
105   pconfig[29].hname = VIA;
106   pconfig[29].setheader = (&osip_message_set_via);
107   pconfig[30].hname = WWW_AUTHENTICATE;
108   pconfig[30].setheader = (&osip_message_set_www_authenticate);
109
110   return 0;
111 }
112
113 /* search the header hname through pconfig[] tab. 
114    A quicker algorithm should be used.
115    It returns the index of the header in the __osip_message_config_t tab.
116 */
117 int
118 __osip_message_is_known_header (const char *hname)
119 {
120   size_t length;
121   int iinf = 0;
122   int isup = NUMBER_OF_HEADERS;
123   int i = NUMBER_OF_HEADERS / 2;
124
125   length = strlen (hname);
126
127   for (;;)
128     {
129       if (i < 0 || i > NUMBER_OF_HEADERS - 1)
130         return -1;
131
132       if ((length == strlen (pconfig[i].hname))
133           && osip_strncasecmp (hname, (const char *) pconfig[i].hname,
134                                length) == 0)
135         return i;
136
137       if (iinf == isup)
138         return -1;              /* not found */
139       if (iinf == isup - 1)
140         {
141           if ((i < NUMBER_OF_HEADERS - 1)
142               && (length == strlen (pconfig[i + 1].hname))
143               && osip_strncasecmp (hname,
144                                    (const char *) pconfig[i + 1].hname,
145                                    length) == 0)
146             return i + 1;
147           else
148             return -1;
149 /* Unreachable code??
150           if ((i > 0) && (length == strlen (pconfig[i - 1].hname))
151               && osip_strncasecmp (hname,
152                           (const char *) pconfig[i - 1].hname, length) == 0)
153             return i - 1;
154           else
155             return -1;
156 */
157         }
158       if (0 < osip_strncasecmp (hname,
159                                 (const char *) pconfig[i].hname, length))
160         {
161           /* if this is true, search further */
162           iinf = i;
163           if (i == i + (isup - i) / 2)
164             i++;
165           else
166             i = i + (isup - i) / 2;
167         }
168       else
169         {
170           isup = i;
171           if (i == i - (i - iinf) / 2)
172             i--;
173           else
174             i = i - (i - iinf) / 2;
175         }
176     }                           /* end of (while (1)) */
177   return -1;
178 }
179
180 #else /* USE_GPERF */
181 /* C code produced by gperf version 2.7.2 */
182 /* Command-line: gperf sip.gperf  */
183
184 #define TOTAL_KEYWORDS 53
185 #define MIN_WORD_LENGTH 1
186 #define MAX_WORD_LENGTH 19
187 #define MIN_HASH_VALUE 1
188 #define MAX_HASH_VALUE 132
189 /* maximum key range = 132, duplicates = 0 */
190
191 #ifdef __GNUC__
192 __inline
193 #else
194 #ifdef __cplusplus
195 inline
196 #endif
197 #endif
198 static unsigned char asso_values[] = {
199   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
200   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
201   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
202   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
203   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
204   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
205   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
206   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
207   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
208   133, 133, 133, 133, 133, 133, 133, 20, 133, 25,
209   41, 0, 5, 20, 25, 1, 133, 133, 10, 60,
210   60, 0, 0, 0, 45, 15, 45, 30, 40, 0,
211   133, 15, 133, 133, 133, 133, 133, 133, 133, 133,
212   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
213   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
214   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
215   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
216   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
217   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
218   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
219   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
220   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
221   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
222   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
223   133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
224   133, 133, 133, 133, 133, 133
225 };
226 static const __osip_message_config_t pconfig[133] = {
227   {"", NULL},
228   {CONTENT_ENCODING_SHORT, &osip_message_set_content_encoding},
229   {"", NULL},
230   {CALL_ID_SHORT, &osip_message_set_call_id},
231   {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL},
232   {ERROR_INFO, &osip_message_set_error_info},
233   {FROM_SHORT, &osip_message_set_from},
234   {IN_REPLY_TO, NULL},
235   {PROXY_REQUIRE, NULL},
236   {"", NULL}, {"", NULL},
237   {WWW_AUTHENTICATE, &osip_message_set_www_authenticate},
238   {"", NULL},
239   {PROXY_AUTHENTICATE, &osip_message_set_proxy_authenticate},
240   {"", NULL}, {"", NULL},
241   {CONTENT_LENGTH_SHORT, &osip_message_set_content_length},
242   {EXPIRES, NULL},
243   {PRIORITY, NULL},
244   {"", NULL},
245   {ALLOW, &osip_message_set_allow},
246   {"", NULL},
247   {WARNING, NULL},
248   {"", NULL},
249   {CSEQ, &osip_message_set_cseq},
250   {ALERT_INFO, &osip_message_set_alert_info},
251   {SUBJECT_SHORT, NULL},
252   {"", NULL}, {"", NULL},
253   {CALL_INFO, &osip_message_set_call_info},
254   {ACCEPT_LANGUAGE, &osip_message_set_accept_language},
255   {"", NULL},
256   {CONTENT_TYPE, &osip_message_set_content_type},
257   {"", NULL},
258   {AUTHENTICATION_INFO, NULL},
259   {"", NULL},
260   {CONTENT_LANGUAGE, NULL},
261   {"", NULL}, {"", NULL}, {"", NULL},
262   {SIPDATE, NULL},
263   {"", NULL},
264   {"to", &osip_message_set_to},
265   {"", NULL}, {"", NULL},
266   {ROUTE, &osip_message_set_route},
267   {CONTENT_TYPE_SHORT, &osip_message_set_content_type},
268   {REQUIRE, NULL},
269   {REPLY_TO, NULL},
270   {TIMESTAMP, NULL},
271   {ACCEPT_ENCODING, &osip_message_set_accept_encoding},
272   {"", NULL},
273   {RECORD_ROUTE, &osip_message_set_record_route},
274   {"", NULL}, {"", NULL}, {"", NULL},
275   {CONTENT_ENCODING, &osip_message_set_content_encoding},
276   {"", NULL},
277   {VIA, &osip_message_set_via},
278   {CONTENT_LENGTH, &osip_message_set_content_length},
279   {SUPPORTED, NULL},
280   {SERVER, NULL},
281   {SUBJECT, NULL},
282   {"", NULL},
283   {FROM, &osip_message_set_from},
284   {"", NULL},
285   {ACCEPT, &osip_message_set_accept},
286   {ORGANIZATION, NULL},
287   {CALL_ID, &osip_message_set_call_id},
288   {"", NULL}, {"", NULL}, {"", NULL},
289   {CONTACT, &osip_message_set_contact},
290   {"", NULL},
291   {PROXY_AUTHORIZATION, &osip_message_set_proxy_authorization},
292   {"", NULL},
293   {VIA_SHORT, &osip_message_set_via},
294   {UNSUPPORTED, NULL},
295   {"", NULL}, {"", NULL},
296   {USER_AGENT, NULL},
297   {MIN_EXPIRES, NULL},
298   {MAX_FORWARDS, NULL},
299   {"", NULL}, {"", NULL}, {"", NULL},
300   {TO_SHORT, &osip_message_set_to},
301   {"", NULL},
302   {AUTHORIZATION, &osip_message_set_authorization},
303   {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"",
304                                                                            NULL},
305   {RETRY_AFTER, NULL},
306   {"", NULL}, {"", NULL},
307   {CONTENT_DISPOSITION, NULL},
308   {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"",
309                                                                            NULL},
310   {"", NULL}, {"", NULL},
311   {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"",
312                                                                            NULL},
313   {CONTACT_SHORT, &osip_message_set_contact},
314   {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"", NULL}, {"",
315                                                                            NULL},
316   {"", NULL}, {"", NULL},
317   {"", NULL},
318   {MIME_VERSION, &osip_message_set_mime_version}
319 };
320 static unsigned int
321 hash (str, len)
322      register const char *str;
323      register unsigned int len;
324 {
325   return len + asso_values[(unsigned char) str[len - 1]] +
326     asso_values[(unsigned char) str[0]];
327 }
328
329 #ifdef __GNUC__
330 __inline
331 #endif
332   int
333 in_word_set (str, len)
334      register const char *str;
335      register unsigned int len;
336 {
337   if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
338     {
339       register int key = hash (str, len);
340
341       if (key <= MAX_HASH_VALUE && key >= 0)
342         {
343           register const char *s = pconfig[key].hname;
344
345           if (*str == *s && !strcmp (str + 1, s + 1)
346               && (pconfig[key].setheader != NULL))
347             return key;
348         }
349     }
350   return -1;
351 }
352
353 int
354 parser_init ()
355 {
356   return 0;                     /* do not need initialization when using gperf */
357 }
358
359 int
360 __osip_message_is_known_header (const char *hname)
361 {
362   int iLength;
363
364   iLength = strlen (hname);
365   return in_word_set (hname, iLength);
366 }
367
368 #endif
369
370 /* This method calls the method that is able to parse the header */
371 int
372 __osip_message_call_method (int i, osip_message_t * dest, const char *hvalue)
373 {
374   return pconfig[i].setheader (dest, hvalue);
375 }