/src/kamailio/src/core/parser/parse_expires.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Expires header field body parser |
3 | | * |
4 | | * Copyright (C) 2001-2003 FhG Fokus |
5 | | * |
6 | | * This file is part of Kamailio, a free SIP server. |
7 | | * |
8 | | * Kamailio 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 | | * Kamailio is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | * |
22 | | */ |
23 | | |
24 | | /*! \file |
25 | | * \brief Parser :: Expires header field body parser. |
26 | | * |
27 | | * |
28 | | * \ingroup parser |
29 | | */ |
30 | | |
31 | | |
32 | | #include "parse_expires.h" |
33 | | #include <stdio.h> /* printf */ |
34 | | #include "../mem/mem.h" /* pkg_malloc, pkg_free */ |
35 | | #include "../dprint.h" |
36 | | #include "../trim.h" /* trim_leading */ |
37 | | #include <string.h> /* memset */ |
38 | | #include "../ut.h" |
39 | | |
40 | | |
41 | | static inline int expires_parser(char *_s, int _l, exp_body_t *_e) |
42 | 0 | { |
43 | 0 | int i; |
44 | 0 | str tmp; |
45 | |
|
46 | 0 | tmp.s = _s; |
47 | 0 | tmp.len = _l; |
48 | |
|
49 | 0 | trim(&tmp); |
50 | |
|
51 | 0 | if(tmp.len == 0) { |
52 | 0 | LM_ERR("Empty body\n"); |
53 | 0 | _e->valid = 0; |
54 | 0 | return -1; |
55 | 0 | } |
56 | | |
57 | 0 | _e->text.s = tmp.s; |
58 | 0 | _e->text.len = tmp.len; |
59 | | |
60 | | /* more than 32bit/maxuint can't be valid */ |
61 | 0 | if(tmp.len > 10) { |
62 | 0 | _e->valid = 0; |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | 0 | for(i = 0; i < tmp.len; i++) { |
67 | 0 | if((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) { |
68 | 0 | _e->val *= 10; |
69 | 0 | _e->val += tmp.s[i] - '0'; |
70 | 0 | } else { |
71 | 0 | switch(tmp.s[i]) { |
72 | 0 | case ' ': |
73 | 0 | case '\t': |
74 | 0 | case '\r': |
75 | 0 | case '\n': |
76 | 0 | _e->text.len = i; |
77 | 0 | _e->valid = 1; |
78 | 0 | return 0; |
79 | | |
80 | 0 | default: |
81 | | /* Exit normally here, we want to be backwards compatible |
82 | | * with RFC2543 entities that can put absolute time here |
83 | | */ |
84 | | /* |
85 | | LM_ERR("Invalid character\n"); |
86 | | return -2; |
87 | | */ |
88 | 0 | _e->valid = 0; |
89 | 0 | return 0; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | _e->valid = 1; |
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | | |
99 | | /*! \brief |
100 | | * Parse expires header field body |
101 | | */ |
102 | | int parse_expires(struct hdr_field *_h) |
103 | 0 | { |
104 | 0 | exp_body_t *e; |
105 | |
|
106 | 0 | if(_h->parsed) { |
107 | 0 | return 0; /* Already parsed */ |
108 | 0 | } |
109 | | |
110 | 0 | e = (exp_body_t *)pkg_malloc(sizeof(exp_body_t)); |
111 | 0 | if(e == 0) { |
112 | 0 | PKG_MEM_ERROR; |
113 | 0 | return -1; |
114 | 0 | } |
115 | | |
116 | 0 | memset(e, 0, sizeof(exp_body_t)); |
117 | |
|
118 | 0 | if(expires_parser(_h->body.s, _h->body.len, e) < 0) { |
119 | 0 | LM_ERR("Error while parsing\n"); |
120 | 0 | pkg_free(e); |
121 | 0 | return -2; |
122 | 0 | } |
123 | | |
124 | 0 | _h->parsed = (void *)e; |
125 | 0 | return 0; |
126 | 0 | } |
127 | | |
128 | | |
129 | | /*! \brief |
130 | | * Free all memory associated with exp_body_t |
131 | | */ |
132 | | void free_expires(exp_body_t **_e) |
133 | 0 | { |
134 | 0 | pkg_free(*_e); |
135 | 0 | *_e = 0; |
136 | 0 | } |
137 | | |
138 | | |
139 | | /*! \brief |
140 | | * Print exp_body_t content, for debugging only |
141 | | */ |
142 | | void print_expires(exp_body_t *_e) |
143 | 0 | { |
144 | 0 | printf("===Expires===\n"); |
145 | 0 | printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s)); |
146 | 0 | printf("val : %d\n", _e->val); |
147 | 0 | printf("===/Expires===\n"); |
148 | 0 | } |