Avoid crash when aborting POST from the server side.
[polipo.git] / parse_time.c
blob71109b2ee7047332851a400e96988ed460726292
1 /*
2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "polipo.h"
25 const time_t time_t_max = ((time_t)~(1U << 31));
27 static inline int
28 d2i(char c)
30 if(c >= '0' && c <= '9')
31 return c - '0';
32 else
33 return -1;
36 static int
37 parse_int(const char *buf, int i, int len, int *val_return)
39 int val, d;
41 if(i >= len)
42 return -1;
44 val = d2i(buf[i]);
45 if(val < 0)
46 return -1;
47 else
48 i++;
50 while(i < len) {
51 d = d2i(buf[i]);
52 if(d < 0)
53 break;
54 val = val * 10 + d;
55 i++;
57 *val_return = val;
58 return i;
61 static const char month_names[12][3] = {
62 "jan", "feb", "mar", "apr", "may", "jun",
63 "jul", "aug", "sep", "oct", "nov", "dec",
66 static int
67 skip_word(const char *buf, int i, int len)
69 if(i >= len)
70 return -1;
72 if(!letter(buf[i]))
73 return -1;
75 while(i < len) {
76 if(!letter(buf[i]))
77 break;
78 i++;
81 return i;
84 static int
85 parse_month(const char *buf, int i, int len, int *val_return)
87 int j, k, l;
88 j = skip_word(buf, i, len);
89 if(j != i + 3)
90 return -1;
91 for(k = 0; k < 12; k++) {
92 for(l = 0; l < 3; l++) {
93 if(lwr(buf[i + l]) != month_names[k][l])
94 break;
96 if(l == 3)
97 break;
99 if(k >= 12)
100 return -1;
101 *val_return = k;
102 return j;
105 static int
106 issep(char c)
108 return c == ' ' || c == '\t' || c == ',' || c == ':' || c == '-';
112 skip_separator(const char *buf, int i, int len)
114 if(i >= len)
115 return -1;
117 if(issep(buf[i]))
118 i++;
119 else
120 return -1;
122 while(i < len) {
123 if(issep(buf[i]))
124 i++;
125 else
126 break;
128 return i;
132 parse_time(const char *buf, int offset, int len, time_t *time_return)
134 struct tm tm;
135 time_t t;
136 int i = offset;
138 i = skip_word(buf, i, len); if(i < 0) return -1;
139 i = skip_separator(buf, i, len); if(i < 0) return -1;
141 if(i >= len)
142 return -1;
144 if(d2i(buf[i]) >= 0) {
145 i = parse_int(buf, i, len, &tm.tm_mday); if(i < 0) return -1;
146 i = skip_separator(buf, i, len); if(i < 0) return -1;
147 i = parse_month(buf, i, len, &tm.tm_mon); if(i < 0) return -1;
148 i = skip_separator(buf, i, len); if(i < 0) return -1;
149 i = parse_int(buf, i, len, &tm.tm_year); if(i < 0) return -1;
150 if(tm.tm_year < 100)
151 tm.tm_year += 1900;
152 if(tm.tm_year < 1937)
153 tm.tm_year += 100;
154 if(tm.tm_year < 1937)
155 return -1;
157 i = skip_separator(buf, i, len); if(i < 0) return -1;
158 i = parse_int(buf, i, len, &tm.tm_hour); if(i < 0) return -1;
159 i = skip_separator(buf, i, len); if(i < 0) return -1;
160 i = parse_int(buf, i, len, &tm.tm_min); if(i < 0) return -1;
161 i = skip_separator(buf, i, len); if(i < 0) return -1;
162 i = parse_int(buf, i, len, &tm.tm_sec); if(i < 0) return -1;
163 i = skip_separator(buf, i, len); if(i < 0) return -1;
164 i = skip_word(buf, i, len); if(i < 0) return -1;
165 } else { /* funny American format */
166 i = parse_month(buf, i, len, &tm.tm_mon); if(i < 0) return -1;
167 i = skip_separator(buf, i, len); if(i < 0) return -1;
168 i = parse_int(buf, i, len, &tm.tm_mday); if(i < 0) return -1;
169 i = skip_separator(buf, i, len); if(i < 0) return -1;
170 i = parse_int(buf, i, len, &tm.tm_hour); if(i < 0) return -1;
171 i = skip_separator(buf, i, len); if(i < 0) return -1;
172 i = parse_int(buf, i, len, &tm.tm_min); if(i < 0) return -1;
173 i = skip_separator(buf, i, len); if(i < 0) return -1;
174 i = parse_int(buf, i, len, &tm.tm_sec); if(i < 0) return -1;
175 i = skip_separator(buf, i, len); if(i < 0) return -1;
176 i = parse_int(buf, i, len, &tm.tm_year); if(i < 0) return -1;
177 if(tm.tm_year < 100)
178 tm.tm_year += 1900;
179 if(tm.tm_year < 1937)
180 tm.tm_year += 100;
181 if(tm.tm_year < 1937 || tm.tm_year > 2040)
182 return -1;
185 if(tm.tm_year < 2038) {
186 tm.tm_year -= 1900;
187 tm.tm_isdst = -1;
188 t = mktime_gmt(&tm);
189 if(t == -1)
190 return -1;
191 } else {
192 t = time_t_max;
195 *time_return = t;
196 return i;
200 format_time(char *buf, int i, int len, time_t t)
202 struct tm *tm;
203 int rc;
205 if(i < 0 || i > len)
206 return -1;
208 tm = gmtime(&t);
209 if(tm == NULL)
210 return -1;
211 rc = strftime(buf + i, len - i, "%a, %d %b %Y %H:%M:%S GMT", tm);
212 if(rc <= 0) /* yes, that's <= */
213 return -1;
214 return i + rc;