OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / ser / parser / parse_expires.c
1 /*
2  * $Id: parse_expires.c,v 1.6 2004/08/24 09:01:25 janakj Exp $
3  *
4  * Expires header field body parser
5  *
6  * Copyright (C) 2001-2003 FhG Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License 
26  * along with this program; if not, write to the Free Software 
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  *
29  * History:
30  * --------
31  * 2003-04-26 ZSW (jiri)
32  */
33
34
35 #include "parse_expires.h"
36 #include <stdio.h>          /* printf */
37 #include "../mem/mem.h"     /* pkg_malloc, pkg_free */
38 #include "../dprint.h"
39 #include "../trim.h"        /* trim_leading */
40 #include <string.h>         /* memset */
41 #include "../ut.h"
42
43
44 static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
45 {
46         int i;
47         str tmp;
48         
49         tmp.s = _s;
50         tmp.len = _l;
51
52         trim_leading(&tmp);
53
54         if (tmp.len == 0) {
55                 LOG(L_ERR, "expires_parser(): Empty body\n");
56                 _e->valid = 0;
57                 return -1;
58         }
59
60         _e->text.s = tmp.s;
61
62         for(i = 0; i < tmp.len; i++) {
63                 if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
64                         _e->val *= 10;
65                         _e->val += tmp.s[i] - '0';
66                 } else {
67                         switch(tmp.s[i]) {
68                         case ' ':
69                         case '\t':
70                         case '\r':
71                         case '\n':
72                                 _e->text.len = i;
73                                 _e->valid = 1;
74                                 return 0;
75
76                         default:
77                                      /* Exit normally here, we want to be backwards compatible with
78                                       * RFC2543 entities that can put absolute time here
79                                       */
80                                      /*
81                                 LOG(L_ERR, "expires_parser(): Invalid character\n");
82                                 return -2;
83                                      */
84                                 _e->valid = 0;
85                                 return 0;
86                         }
87                 }
88         }
89
90         _e->text.len = _l;
91         _e->valid = 1;
92         return 0;
93 }
94
95
96 /*
97  * Parse expires header field body
98  */
99 int parse_expires(struct hdr_field* _h)
100 {
101         exp_body_t* e;
102
103         if (_h->parsed) {
104                 return 0;  /* Already parsed */
105         }
106
107         e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
108         if (e == 0) {
109                 LOG(L_ERR, "parse_expires(): No memory left\n");
110                 return -1;
111         }
112         
113         memset(e, 0, sizeof(exp_body_t));
114
115         if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
116                 LOG(L_ERR, "parse_expires(): Error while parsing\n");
117                 pkg_free(e);
118                 return -2;
119         }
120         
121         _h->parsed = (void*)e;
122         return 0;
123 }
124
125
126 /*
127  * Free all memory associated with exp_body_t
128  */
129 void free_expires(exp_body_t** _e)
130 {
131         pkg_free(*_e);
132         *_e = 0;
133 }
134
135
136 /*
137  * Print exp_body_t content, for debugging only
138  */
139 void print_expires(exp_body_t* _e)
140 {
141         printf("===Expires===\n");
142         printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
143         printf("val : %d\n", _e->val);
144         printf("===/Expires===\n");
145 }