varnish-cache/lib/libvarnish/vav.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * const char **VAV_Parse(const char *s, int *argc, int flag)
31
 *      Parse a command like line into an argv[]
32
 *      Index zero contains NULL or an error message
33
 *      (The error message is a static const char* and does not
34
 *      need saving or copying.)
35
 *      "double quotes" and backslash substitution is handled.
36
 *
37
 * void VAV_Free(const char **argv)
38
 *      Free the result of VAV_Parse()
39
 *
40
 */
41
42
#include "config.h"
43
44
#include <ctype.h>
45
#include <stdint.h>
46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
50
#include "vdef.h"
51
52
#include "vas.h"
53
#include "vav.h"
54
#include "vnum.h"
55
56
static int
57 75784
vav_backslash_txt(const char *s, const char *e, char *res)
58
{
59
        int r, l, i;
60
        const char *p;
61
        char c;
62
63 75784
        AN(s);
64 75784
        if (e == NULL)
65 73376
                e = strchr(s, '\0');
66
67 75784
        l = pdiff(s, e);
68 75784
        if (l < 2)
69 28
                return (0);
70
71 75756
        assert(*s == '\\');
72 75756
        r = c = 0;
73 75756
        switch (s[1]) {
74
        case 'n':
75 35288
                c = '\n';
76 35288
                r = 2;
77 35288
                break;
78
        case 'r':
79 28092
                c = '\r';
80 28092
                r = 2;
81 28092
                break;
82
        case 't':
83 644
                c = '\t';
84 644
                r = 2;
85 644
                break;
86
        case '"':
87 3696
                c = '"';
88 3696
                r = 2;
89 3696
                break;
90
        case '\\':
91 868
                c = '\\';
92 868
                r = 2;
93 868
                break;
94
        case '0': case '1': case '2': case '3':
95
        case '4': case '5': case '6': case '7':
96 504
                for (r = 1; r < 4 && r < l; r++) {
97 392
                        if (!isdigit(s[r]))
98 28
                                break;
99 364
                        if (s[r] - '0' > 7)
100 0
                                break;
101 364
                        c <<= 3;        /*lint !e701 signed left shift */
102 364
                        c |= s[r] - '0';
103 364
                }
104 140
                break;
105
        case 'x':
106 6580
                if (l >= 4 && (i = VNUM_hex(s + 2, s + 4, &p)) >= 0 &&
107 6524
                    p == s + 4) {
108 6496
                        AZ(i & ~0xff);
109 6496
                        c = i;  /*lint !e734 loss of precision */
110 6496
                        r = 4;
111 6496
                }
112 6580
                break;
113
        default:
114 448
                break;
115
        }
116 75756
        if (res != NULL)
117 74524
                *res = c;
118 75756
        return (r);
119 75784
}
120
121
int
122 73376
VAV_BackSlash(const char *s, char *res)
123
{
124
125 73376
        return (vav_backslash_txt(s, NULL, res));
126
}
127
128
char *
129 11342079
VAV_BackSlashDecode(const char *s, const char *e)
130
{
131
        const char *q;
132
        char *p, *r;
133
        int i;
134
135 11342079
        if (e == NULL)
136 0
                e = strchr(s, '\0');
137 11342079
        assert(e != NULL);
138 11342079
        p = calloc(1, (e - s) + 1L);
139 11342079
        if (p == NULL)
140 0
                return (p);
141 103174430
        for (r = p, q = s; q < e; ) {
142 91832351
                if (*q != '\\') {
143 91831203
                        *r++ = *q++;
144 91831203
                        continue;
145
                }
146 1148
                i = vav_backslash_txt(q, e, r);
147 1148
                if (i == 0) {
148 0
                        free(p);
149 0
                        errno = EINVAL;
150 0
                        return (NULL);
151
                }
152 1148
                q += i;
153 1148
                r++;
154
        }
155 11342079
        *r = '\0';
156 11342079
        return (p);
157 11342079
}
158
159
static char err_invalid_backslash[] = "Invalid backslash sequence";
160
static char err_invalid_quote[] = "Invalid '\"'";
161
static char err_missing_quote[] = "Missing '\"'";
162
static char err_missing_separator[] = "Missing separator between arguments";
163
164
char **
165 3043972
VAV_ParseTxt(const char *b, const char *e, int *argc, int flag)
166
{
167
        char **argv;
168
        const char *p, *sep;
169
        int nargv, largv;
170
        int i, quote;
171
172 3043972
        AN(b);
173 3043972
        if (e == NULL)
174 2740618
                e = strchr(b, '\0');
175 3043972
        sep = NULL;
176 3043972
        quote = 0;
177 3043972
        nargv = 1;
178 3043972
        largv = 16;
179 3043972
        argv = calloc(largv, sizeof *argv);
180 3043972
        if (argv == NULL)
181 0
                return (NULL);
182
183 16346060
        while (b < e) {
184 13302452
                if (isspace(*b)) {
185 1956453
                        b++;
186 1956453
                        continue;
187
                }
188 11346027
                if (sep != NULL && isspace(*sep) &&
189 7686114
                    *b == ',' && (flag & ARGV_COMMA)) {
190 28
                        sep = NULL;
191 28
                        b++;
192 28
                        continue;
193
                }
194 11345971
                if (sep != NULL && *sep == '"' && *b == '"') {
195 28
                        argv[0] = err_missing_separator;
196 28
                        return (argv);
197
                }
198 11345943
                sep = NULL;
199 11345943
                if ((flag & ARGV_COMMENT) && *b == '#')
200 28
                        break;
201 11345915
                if (*b == '"' && !(flag & ARGV_NOESC)) {
202 49050
                        p = ++b;
203 49050
                        quote = 1;
204 49050
                } else {
205 11296865
                        p = b;
206 11296865
                        quote = 0;
207
                }
208 103194291
                while (b < e) {
209 100441483
                        if (*b == '\\' && !(flag & ARGV_NOESC)) {
210 1260
                                i = vav_backslash_txt(b, e, NULL);
211 1260
                                if (i == 0) {
212 112
                                        argv[0] = err_invalid_backslash;
213 112
                                        return (argv);
214
                                }
215 1148
                                b += i;
216 1148
                                continue;
217
                        }
218 100440223
                        if (!quote) {
219 100176720
                                if (isspace(*b)) {
220 7952529
                                        sep = b;
221 7952529
                                        break;
222
                                }
223 92224191
                                if ((flag & ARGV_COMMA) && *b == ',') {
224 591500
                                        sep = b;
225 591500
                                        break;
226
                                }
227 91632691
                                if (!(flag & ARGV_NOESC) && *b == '"') {
228 56
                                        argv[0] = err_invalid_quote;
229 56
                                        return (argv);
230
                                }
231 91632635
                                b++;
232 91632635
                                continue;
233
                        }
234 263503
                        if (*b == '"' && !(flag & ARGV_NOESC)) {
235 48910
                                sep = b;
236 48910
                                quote = 0;
237 48910
                                break;
238
                        }
239 214593
                        b++;
240
                }
241 11345747
                if (sep == NULL && quote) {
242 140
                        argv[0] = err_missing_quote;
243 140
                        return (argv);
244
                }
245
                /* Ensure slots for 1 new arg plus 1 trailing arg */
246 11345607
                if (nargv + 2 >= largv) {
247 26712
                        argv = realloc(argv, sizeof (*argv) * (largv += largv));
248 26712
                        assert(argv != NULL);
249 26712
                }
250 11345607
                if (flag & ARGV_NOESC) {
251 3528
                        argv[nargv] = malloc(1L + (b - p));
252 3528
                        assert(argv[nargv] != NULL);
253 3528
                        memcpy(argv[nargv], p, b - p);
254 3528
                        argv[nargv][b - p] = '\0';
255 3528
                } else {
256 11342079
                        argv[nargv] = VAV_BackSlashDecode(p, b);
257 11342079
                        assert(argv[nargv] != NULL);
258
                }
259 11345607
                nargv++;
260 11345607
                if (b < e)
261 8592939
                        b++;
262
        }
263 3043636
        if (sep != NULL && *sep == ',') {
264 28
                argv[nargv] = strdup("");
265 28
                assert(argv[nargv] != NULL);
266 28
                nargv++;
267 28
        }
268 3043636
        argv[nargv] = NULL;
269 3043636
        if (argc != NULL)
270 2861327
                *argc = nargv;
271 3043636
        return (argv);
272 3043972
}
273
274
char **
275 2740618
VAV_Parse(const char *s, int *argc, int flag)
276
{
277
278 2740618
        return (VAV_ParseTxt(s, NULL, argc, flag));
279
}
280
281
void
282 2946649
VAV_Free(char **argv)
283
{
284
        int i;
285
286 14007165
        for (i = 1; argv[i] != NULL; i++)
287 11060516
                free(argv[i]);
288 2946649
        free(argv);
289 2946649
}
290
291
#ifdef TESTPROG
292
293
#include <printf.h>
294
295
static void
296
VAV_Print(char **argv)
297
{
298
        int i;
299
300
        printf("---- %p\n", argv);
301
        if (argv[0] != NULL)
302
                printf("err %V\n", argv[0]);
303
        for (i = 1; argv[i] != NULL; i++)
304
                printf("%3d %V\n", i, argv[i]);
305
}
306
307
static void
308
Test(const char *str)
309
{
310
        char **av;
311
312
        printf("Test: <%V>\n", str);
313
        av = VAV_Parse(str, NULL, 0);
314
        VAV_Print(av);
315
}
316
317
#if defined __linux__
318
int
319
printf_v(FILE *stream, const struct printf_info *info,
320
    const void *const *args)
321
{
322
        const char *v = *((char **)args[0]);
323
        return (fprintf(stream, "%*s",
324
            info->left ? -info->width : info->width, v));
325
}
326
327
int
328
printf_v_info(const struct printf_info *info, size_t n, int *argtypes,
329
    int *size)
330
{
331
        if (n > 0)
332
                argtypes[0] = PA_STRING;
333
        return (1);
334
}
335
#endif
336
337
int
338
main(int argc, char **argv)
339
{
340
        char buf[BUFSIZ];
341
342
        (void)argc;
343
        (void)argv;
344
345
#if defined __FreeBSD__
346
        register_printf_render_std("V");
347
#elif defined __linux__
348
        register_printf_specifier('V', printf_v, printf_v_info);
349
#else
350
#error Unsupported platform
351
#endif
352
353
        while (fgets(buf, sizeof buf, stdin))
354
                Test(buf);
355
356
        return (0);
357
}
358
#endif
359
360
#ifdef TEST_DRIVER
361
#  include <stdio.h>
362
363
struct test_case {
364
        int             flag;
365
        const char      *str;
366
        const char      **argv;
367
};
368
369
static const struct test_case *tests[] = {
370
#define TEST_PASS(flag, str, ...)                                       \
371
        &(const struct test_case){flag, str,                            \
372
            (const char **)&(const void *[]){NULL, __VA_ARGS__, NULL}}
373
#define TEST_FAIL(flag, str, err)                                       \
374
        &(const struct test_case){flag, str,                            \
375
            (const char **)&(const void *[]){err_ ## err, NULL}}
376
#define K ARGV_COMMENT
377
#define C ARGV_COMMA
378
#define N ARGV_NOESC
379
        TEST_PASS(K|C|N, "", NULL),
380
        TEST_PASS(0    , "foo", "foo"),
381
        TEST_PASS(0    , "foo bar", "foo", "bar"),
382
        TEST_PASS(  C  , "foo bar", "foo", "bar"),
383
        TEST_PASS(  C  , "foo,bar", "foo", "bar"),
384
        TEST_PASS(0    , "  foo  bar  ", "foo", "bar"),
385
        TEST_PASS(  C  , "  foo  ,  bar  ", "foo", "bar"),
386
        TEST_PASS(  C  , "foo bar ", "foo", "bar"),
387
        TEST_PASS(  C  , "foo,bar,", "foo", "bar", ""),
388
        TEST_PASS(0    , "foo \"bar baz\"", "foo", "bar baz"),
389
        TEST_PASS(0    , "foo #bar", "foo", "#bar"),
390
        TEST_PASS(K    , "foo #bar", "foo"),
391
        TEST_PASS(0    , "foo#bar", "foo#bar"),
392
        TEST_PASS(K    , "foo#bar", "foo#bar"),
393
        TEST_PASS(    N, "\\", "\\"),
394
        TEST_FAIL(0    , "\\", invalid_backslash),
395
        TEST_FAIL(0    , "\\x", invalid_backslash),
396
        TEST_FAIL(0    , "\\x2", invalid_backslash),
397
        TEST_FAIL(0    , "\\x2O", invalid_backslash),
398
        TEST_PASS(0    , "\\x20", " "),
399
        TEST_FAIL(0    , "\"foo", missing_quote),
400
        TEST_PASS(    N, "foo\"bar", "foo\"bar"),
401
        TEST_FAIL(0    , "foo\"bar", invalid_quote),
402
        TEST_FAIL(0    , "foo\"bar", invalid_quote),
403
        TEST_PASS(    N, "\"foo\"\"bar\"", "\"foo\"\"bar\""),
404
        TEST_FAIL(0    , "\"foo\"\"bar\"", missing_separator),
405
        NULL
406
#undef N
407
#undef C
408
#undef K
409
#undef TEST_FAIL
410
#undef TEST_PASS
411
};
412
413
static char **
414 728
test_run(const struct test_case *tc, int *ret)
415
{
416
        const char *exp, *act;
417
        char **argv, *tmp;
418
        int argc, i;
419
420 728
        i = strlen(tc->str);
421 728
        if (i == 0) {
422 28
                argv = VAV_Parse(tc->str, &argc, tc->flag);
423 28
        } else {
424 700
                tmp = malloc(i); /* sanitizer-friendly */
425 700
                AN(tmp);
426 700
                memcpy(tmp, tc->str, i);
427 700
                argv = VAV_ParseTxt(tmp, tmp + i, &argc, tc->flag);
428 700
                free(tmp);
429
        }
430 728
        AN(argv);
431
432 728
        if (tc->argv[0] != argv[0]) {
433 0
                exp = tc->argv[0] != NULL ? tc->argv[0] : "success";
434 0
                act = argv[0] != NULL ? argv[0] : "success";
435 0
                printf(
436
                    "ERROR: Parsing string <%s> with flags %x, "
437
                    "expected <%s> got <%s>.\n",
438 0
                    tc->str, tc->flag, exp, act);
439 0
                *ret = 1;
440 0
                return (argv);
441
        }
442
443 728
        if (tc->argv[0] != NULL)
444 224
                return (argv);
445
446 1260
        for (i = 1; i < argc && tc->argv[i] != NULL && argv[i] != NULL; i++) {
447 756
                if (!strcmp(tc->argv[i], argv[i]))
448 756
                        continue;
449 0
                printf(
450
                    "ERROR: Parsing string <%s> with flags %x, "
451
                    "expected <%s> for argv[%d] got <%s>.\n",
452 0
                    tc->str, tc->flag, tc->argv[i], i, argv[i]);
453 0
                *ret = 1;
454 0
                return (argv);
455
        }
456
457 504
        if (tc->argv[i] != NULL || argv[i] != NULL) {
458 0
                act = i < argc ? "less" : "more";
459 0
                printf(
460
                    "ERROR: Parsing string <%s> with flags %x, "
461
                    "got %s arguments (%d) than expected.\n",
462 0
                    tc->str, tc->flag, act, argc);
463 0
                *ret = 1;
464 0
                return (argv);
465
        }
466
467 504
        exp = tc->argv[0] == NULL ? "PASS" : "FAIL";
468 504
        printf("%s: <%s> with flags %x as expected.\n", exp, tc->str, tc->flag);
469 504
        return (argv);
470 728
}
471
472
int
473 28
main(int argc, char **argv)
474
{
475
        const struct test_case **tc;
476 28
        int ret = 0;
477
478 28
        (void)argc;
479 28
        (void)argv;
480
481 756
        for (tc = tests; ret == 0 && *tc != NULL; tc++) {
482 728
                argv = test_run(*tc, &ret);
483 728
                VAV_Free(argv);
484 728
        }
485
486 28
        return (0);
487
}
488
#endif /* TEST_DRIVER */