blob: 7b60c5fb65a3c9c8e3084d47dadfdc5d8b1ff3ef [file] [log] [blame]
Igor Sysoev8556e6d2003-10-23 15:54:19 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd90282d2004-09-28 08:34:51 +00005 */
6
7
Igor Sysoev8556e6d2003-10-23 15:54:19 +00008#include <ngx_config.h>
9#include <ngx_core.h>
10
11
Igor Sysoevc2068d02005-10-19 12:33:58 +000012ssize_t
13ngx_parse_size(ngx_str_t *line)
Igor Sysoev8556e6d2003-10-23 15:54:19 +000014{
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030015 u_char unit;
16 size_t len;
17 ssize_t size, scale, max;
Igor Sysoev8556e6d2003-10-23 15:54:19 +000018
19 len = line->len;
Igor Sysoev2e39e282008-04-16 19:33:23 +000020 unit = line->data[len - 1];
Igor Sysoev8556e6d2003-10-23 15:54:19 +000021
Igor Sysoev2e39e282008-04-16 19:33:23 +000022 switch (unit) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +000023 case 'K':
24 case 'k':
25 len--;
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030026 max = NGX_MAX_SIZE_T_VALUE / 1024;
Igor Sysoev8556e6d2003-10-23 15:54:19 +000027 scale = 1024;
28 break;
29
30 case 'M':
31 case 'm':
32 len--;
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030033 max = NGX_MAX_SIZE_T_VALUE / (1024 * 1024);
Igor Sysoev8556e6d2003-10-23 15:54:19 +000034 scale = 1024 * 1024;
35 break;
36
37 default:
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030038 max = NGX_MAX_SIZE_T_VALUE;
Igor Sysoev8556e6d2003-10-23 15:54:19 +000039 scale = 1;
40 }
41
Igor Sysoevc2068d02005-10-19 12:33:58 +000042 size = ngx_atosz(line->data, len);
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030043 if (size == NGX_ERROR || size > max) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +000044 return NGX_ERROR;
45 }
46
47 size *= scale;
48
49 return size;
50}
51
52
Igor Sysoev1765f472006-07-07 16:33:19 +000053off_t
54ngx_parse_offset(ngx_str_t *line)
55{
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030056 u_char unit;
57 off_t offset, scale, max;
58 size_t len;
Igor Sysoev1765f472006-07-07 16:33:19 +000059
60 len = line->len;
Igor Sysoev2e39e282008-04-16 19:33:23 +000061 unit = line->data[len - 1];
Igor Sysoev1765f472006-07-07 16:33:19 +000062
Igor Sysoev2e39e282008-04-16 19:33:23 +000063 switch (unit) {
Igor Sysoev1765f472006-07-07 16:33:19 +000064 case 'K':
65 case 'k':
66 len--;
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030067 max = NGX_MAX_OFF_T_VALUE / 1024;
Igor Sysoev1765f472006-07-07 16:33:19 +000068 scale = 1024;
69 break;
70
71 case 'M':
72 case 'm':
73 len--;
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030074 max = NGX_MAX_OFF_T_VALUE / (1024 * 1024);
Igor Sysoev1765f472006-07-07 16:33:19 +000075 scale = 1024 * 1024;
76 break;
77
78 case 'G':
79 case 'g':
80 len--;
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030081 max = NGX_MAX_OFF_T_VALUE / (1024 * 1024 * 1024);
Igor Sysoev1765f472006-07-07 16:33:19 +000082 scale = 1024 * 1024 * 1024;
83 break;
84
85 default:
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030086 max = NGX_MAX_OFF_T_VALUE;
Igor Sysoev1765f472006-07-07 16:33:19 +000087 scale = 1;
88 }
89
90 offset = ngx_atoof(line->data, len);
Ruslan Ermilov2a54e4b2015-03-17 00:26:15 +030091 if (offset == NGX_ERROR || offset > max) {
Igor Sysoev1765f472006-07-07 16:33:19 +000092 return NGX_ERROR;
93 }
94
95 offset *= scale;
96
97 return offset;
98}
99
100
Igor Sysoevc2068d02005-10-19 12:33:58 +0000101ngx_int_t
Ruslan Ermilova6101272011-10-07 08:03:16 +0000102ngx_parse_time(ngx_str_t *line, ngx_uint_t is_sec)
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000103{
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000104 u_char *p, *last;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000105 ngx_int_t value, total, scale;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300106 ngx_int_t max, cutoff, cutlim;
107 ngx_uint_t valid;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000108 enum {
109 st_start = 0,
110 st_year,
111 st_month,
112 st_week,
113 st_day,
114 st_hour,
115 st_min,
116 st_sec,
117 st_msec,
118 st_last
119 } step;
120
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000121 valid = 0;
122 value = 0;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000123 total = 0;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300124 cutoff = NGX_MAX_INT_T_VALUE / 10;
125 cutlim = NGX_MAX_INT_T_VALUE % 10;
Ruslan Ermilova6101272011-10-07 08:03:16 +0000126 step = is_sec ? st_start : st_month;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000127
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000128 p = line->data;
129 last = p + line->len;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000130
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000131 while (p < last) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000132
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000133 if (*p >= '0' && *p <= '9') {
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300134 if (value >= cutoff && (value > cutoff || *p - '0' > cutlim)) {
135 return NGX_ERROR;
136 }
137
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000138 value = value * 10 + (*p++ - '0');
139 valid = 1;
140 continue;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000141 }
142
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000143 switch (*p++) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000144
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000145 case 'y':
146 if (step > st_start) {
147 return NGX_ERROR;
148 }
149 step = st_year;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300150 max = NGX_MAX_INT_T_VALUE / (60 * 60 * 24 * 365);
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000151 scale = 60 * 60 * 24 * 365;
152 break;
153
154 case 'M':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000155 if (step >= st_month) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000156 return NGX_ERROR;
157 }
158 step = st_month;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300159 max = NGX_MAX_INT_T_VALUE / (60 * 60 * 24 * 30);
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000160 scale = 60 * 60 * 24 * 30;
161 break;
162
163 case 'w':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000164 if (step >= st_week) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000165 return NGX_ERROR;
166 }
167 step = st_week;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300168 max = NGX_MAX_INT_T_VALUE / (60 * 60 * 24 * 7);
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000169 scale = 60 * 60 * 24 * 7;
170 break;
171
172 case 'd':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000173 if (step >= st_day) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000174 return NGX_ERROR;
175 }
176 step = st_day;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300177 max = NGX_MAX_INT_T_VALUE / (60 * 60 * 24);
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000178 scale = 60 * 60 * 24;
179 break;
180
181 case 'h':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000182 if (step >= st_hour) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000183 return NGX_ERROR;
184 }
185 step = st_hour;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300186 max = NGX_MAX_INT_T_VALUE / (60 * 60);
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000187 scale = 60 * 60;
188 break;
189
190 case 'm':
Maxim Dounin3f6c21e2015-10-30 21:43:30 +0300191 if (p < last && *p == 's') {
Ruslan Ermilova6101272011-10-07 08:03:16 +0000192 if (is_sec || step >= st_msec) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000193 return NGX_ERROR;
194 }
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000195 p++;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000196 step = st_msec;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300197 max = NGX_MAX_INT_T_VALUE;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000198 scale = 1;
199 break;
200 }
201
Ruslan Ermilova6101272011-10-07 08:03:16 +0000202 if (step >= st_min) {
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000203 return NGX_ERROR;
204 }
205 step = st_min;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300206 max = NGX_MAX_INT_T_VALUE / 60;
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000207 scale = 60;
208 break;
209
210 case 's':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000211 if (step >= st_sec) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000212 return NGX_ERROR;
213 }
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000214 step = st_sec;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300215 max = NGX_MAX_INT_T_VALUE;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000216 scale = 1;
217 break;
218
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000219 case ' ':
Ruslan Ermilova6101272011-10-07 08:03:16 +0000220 if (step >= st_sec) {
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000221 return NGX_ERROR;
222 }
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000223 step = st_last;
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300224 max = NGX_MAX_INT_T_VALUE;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000225 scale = 1;
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000226 break;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000227
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000228 default:
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000229 return NGX_ERROR;
230 }
231
Ruslan Ermilova6101272011-10-07 08:03:16 +0000232 if (step != st_msec && !is_sec) {
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000233 scale *= 1000;
234 max /= 1000;
235 }
236
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300237 if (value > max) {
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000238 return NGX_ERROR;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000239 }
240
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300241 value *= scale;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000242
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300243 if (total > NGX_MAX_INT_T_VALUE - value) {
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000244 return NGX_ERROR;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000245 }
246
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300247 total += value;
248
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000249 value = 0;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000250
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000251 while (p < last && *p == ' ') {
252 p++;
253 }
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000254 }
255
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300256 if (!valid) {
257 return NGX_ERROR;
Igor Sysoevf57b24e2008-04-17 14:23:20 +0000258 }
259
Ruslan Ermilov3ea6a2a2015-03-17 00:26:20 +0300260 if (!is_sec) {
261 if (value > NGX_MAX_INT_T_VALUE / 1000) {
262 return NGX_ERROR;
263 }
264
265 value *= 1000;
266 }
267
268 if (total > NGX_MAX_INT_T_VALUE - value) {
269 return NGX_ERROR;
270 }
271
272 return total + value;
Igor Sysoev8556e6d2003-10-23 15:54:19 +0000273}