varnish-cache/include/vct.h
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2009 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 */
32
33
/* from libvarnish/vct.c */
34
35
#include "vas.h"
36
37
#define VCT_OWS                 (1<<0)
38
#define VCT_CRLF                (1<<1)
39
#define VCT_LWS                 (VCT_CRLF | VCT_OWS)
40
#define VCT_CTL                 (1<<2)
41
#define VCT_ALPHA               (1<<3)
42
#define VCT_SEPARATOR           (1<<4)
43
#define VCT_DIGIT               (1<<5)
44
#define VCT_HEX                 (1<<6)
45
#define VCT_XMLNAMESTART        (1<<7)
46
#define VCT_XMLNAME             (1<<8)
47
#define VCT_TCHAR               (1<<9)
48
#define VCT_ID                  (1<<10)
49
#define VCT_IDENT               (VCT_ALPHA | VCT_DIGIT | VCT_ID)
50
#define VCT_BASE64              (1<<11)
51
#define VCT_VT                  (1<<12)
52
#define VCT_SPACE               (VCT_LWS | VCT_VT)
53
#define VCT_UPPER               (1<<13)
54
#define VCT_LOWER               (1<<14)
55
56
extern const uint16_t vct_typtab[256];
57
extern const uint8_t vct_lowertab[256];
58
59
const char *VCT_invalid_name(const char *b, const char *e);
60
61
static inline int
62 956810123
vct_is(int x, uint16_t y)
63
{
64
65 956810123
        x &= 0xff;
66 956810123
        return (vct_typtab[x] & (y));
67
}
68
69
#define vct_isows(x) vct_is(x, VCT_OWS)
70
#define vct_issp(x) vct_is(x, VCT_OWS)
71
#define vct_ishex(x) vct_is(x, VCT_HEX)
72
#define vct_islws(x) vct_is(x, VCT_LWS)
73
#define vct_isctl(x) vct_is(x, VCT_CTL)
74
#define vct_isspace(x) vct_is(x, VCT_SPACE)
75
#define vct_isdigit(x) vct_is(x, VCT_DIGIT)
76
#define vct_isalpha(x) vct_is(x, VCT_ALPHA)
77
#define vct_islower(x) vct_is(x, VCT_LOWER)
78
#define vct_isupper(x) vct_is(x, VCT_UPPER)
79
#define vct_isalnum(x) vct_is(x, VCT_ALPHA | VCT_DIGIT)
80
#define vct_isbase64(x) vct_is(x, VCT_BASE64)
81
#define vct_issep(x) vct_is(x, VCT_SEPARATOR)
82
#define vct_issepctl(x) vct_is(x, VCT_SEPARATOR | VCT_CTL)
83
#define vct_isident1(x) vct_isalpha(x)
84
#define vct_isident(x) vct_is(x, VCT_IDENT)
85
#define vct_isxmlnamestart(x) vct_is(x, VCT_XMLNAMESTART)
86
#define vct_isxmlname(x) vct_is(x, VCT_XMLNAMESTART | VCT_XMLNAME)
87
#define vct_istchar(x) vct_is(x, VCT_ALPHA | VCT_DIGIT | VCT_TCHAR)
88
#define vct_ishdrval(x) \
89
    (((uint8_t)(x) >= 0x20 && (uint8_t)(x) != 0x7f) ||(uint8_t)(x) == 0x09)
90
91
static inline int
92 35566294
vct_iscrlf(const char* p, const char* end)
93
{
94 35566294
        assert(p <= end);
95 35567120
        if (p == end)
96 40
                return (0);
97 35566478
        if ((p[0] == 0x0d && (p+1 < end) && p[1] == 0x0a)) // CR LF
98 4439209
                return (2);
99 31135194
        if (p[0] == 0x0a) // LF
100 5400
                return (1);
101 31129568
        return (0);
102 35574309
}
103
104
/* NB: VCT always operate in ASCII, don't replace 0x0d with \r etc. */
105
static inline char*
106 1604512
vct_skipcrlf(char* p, const char* end)
107
{
108 1604512
        return (p + vct_iscrlf(p, end));
109
}
110
111
static inline int
112 131280
vct_casecmp(const void *a, const void *b)
113
{
114 131280
        const uint8_t *aa = a;
115 131280
        const uint8_t *bb = b;
116
117 273320
        while (*aa && vct_lowertab[*aa] == vct_lowertab[*bb]) {
118 142040
                aa++;
119 142040
                bb++;
120
        }
121 131280
        if (!*aa && !*bb)
122 25840
                return (0);
123 105440
        if (!*aa)
124 40
                return (-1);
125 105400
        if (!*bb)
126 440
                return (1);
127 104960
        return ((int)vct_lowertab[*aa] - (int)vct_lowertab[*bb]);
128 131280
}
129
130
static inline int
131 6216735
vct_caselencmp(const void *a, const void *b, ssize_t sz)
132
{
133 6216735
        const uint8_t *aa = a;
134 6216735
        const uint8_t *bb = b;
135
136 6216735
        assert(sz >= 0);
137 24891588
        while (sz > 0 && *aa && vct_lowertab[*aa] == vct_lowertab[*bb]) {
138 18675087
                aa++;
139 18675087
                bb++;
140 18675087
                sz--;
141
        }
142 6216975
        if (!sz || (!*aa && !*bb))
143 1874088
                return (0);
144 4343041
        if (!*aa)
145 40
                return (-1);
146 4343030
        if (!*bb)
147 160
                return (1);
148 4342889
        return ((int)vct_lowertab[*aa] - (int)vct_lowertab[*bb]);
149 6217169
}