Code Coverage: vinyl-cache/include/vct.h
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2009 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
 */
31
32
/* from libvinyl/vct.c */
33
34
#include "vas.h"
35
36 47210719
#define VCT_OWS                 (1<<0)
37 6953936
#define VCT_CRLF                (1<<1)
38 6953936
#define VCT_LWS                 (VCT_CRLF | VCT_OWS)
39 118349
#define VCT_CTL                 (1<<2)
40 23237955
#define VCT_ALPHA               (1<<3)
41
#define VCT_SEPARATOR           (1<<4)
42 23703422
#define VCT_DIGIT               (1<<5)
43 57784
#define VCT_HEX                 (1<<6)
44 4303
#define VCT_XMLNAMESTART        (1<<7)
45 44
#define VCT_XMLNAME             (1<<8)
46 1128176
#define VCT_TCHAR               (1<<9)
47 17909983
#define VCT_ID                  (1<<10)
48 17909983
#define VCT_IDENT               (VCT_ALPHA | VCT_DIGIT | VCT_ID)
49
#define VCT_BASE64              (1<<11)
50 6739898
#define VCT_VT                  (1<<12)
51 6739898
#define VCT_SPACE               (VCT_LWS | VCT_VT)
52 26
#define VCT_UPPER               (1<<13)
53 43
#define VCT_LOWER               (1<<14)
54
55
extern const uint16_t vct_typtab[256];
56
extern const uint8_t vct_lowertab[256];
57
58
const char *VCT_invalid_name(const char *b, const char *e);
59
60
static inline int
61
vct_is(int x, uint16_t y)
62 72801367
{
63
64 72801367
        x &= 0xff;
65 72801367
        return (vct_typtab[x] & (y));
66 72801367
}
67
68 39824368
#define vct_isows(x) vct_is(x, VCT_OWS)
69 455186
#define vct_issp(x) vct_is(x, VCT_OWS)
70 83019
#define vct_ishex(x) vct_is(x, VCT_HEX)
71 227427
#define vct_islws(x) vct_is(x, VCT_LWS)
72 157388
#define vct_isctl(x) vct_is(x, VCT_CTL)
73 6739898
#define vct_isspace(x) vct_is(x, VCT_SPACE)
74 2190353
#define vct_isdigit(x) vct_is(x, VCT_DIGIT)
75 1701824
#define vct_isalpha(x) vct_is(x, VCT_ALPHA)
76 43
#define vct_islower(x) vct_is(x, VCT_LOWER)
77 95
#define vct_isupper(x) vct_is(x, VCT_UPPER)
78 2497972
#define vct_isalnum(x) vct_is(x, VCT_ALPHA | VCT_DIGIT)
79
#define vct_isbase64(x) vct_is(x, VCT_BASE64)
80
#define vct_issep(x) vct_is(x, VCT_SEPARATOR)
81
#define vct_issepctl(x) vct_is(x, VCT_SEPARATOR | VCT_CTL)
82 1701824
#define vct_isident1(x) vct_isalpha(x)
83 17909983
#define vct_isident(x) vct_is(x, VCT_IDENT)
84 4259
#define vct_isxmlnamestart(x) vct_is(x, VCT_XMLNAMESTART)
85 44
#define vct_isxmlname(x) vct_is(x, VCT_XMLNAMESTART | VCT_XMLNAME)
86 1131782
#define vct_istchar(x) vct_is(x, VCT_ALPHA | VCT_DIGIT | VCT_TCHAR)
87
#define vct_ishdrval(x) \
88 843339
    (((uint8_t)(x) >= 0x20 && (uint8_t)(x) != 0x7f) ||(uint8_t)(x) == 0x09)
89
90
static inline int
91
vct_iscrlf(const char* p, const char* end)
92 1319841
{
93 1319841
        assert(p <= end);
94 1319841
        if (p == end)
95 1
                return (0);
96 1319840
        if ((p[0] == 0x0d && (p+1 < end) && p[1] == 0x0a)) // CR LF
97 178505
                return (2);
98 1141335
        if (p[0] == 0x0a) // LF
99 129
                return (1);
100 1141206
        return (0);
101 1141335
}
102
103
/* NB: VCT always operate in ASCII, don't replace 0x0d with \r etc. */
104
static inline char*
105
vct_skipcrlf(char* p, const char* end)
106 56918
{
107 56918
        return (p + vct_iscrlf(p, end));
108 56918
}
109
110
static inline int
111
vct_casecmp(const void *a, const void *b)
112 6368
{
113 6368
        const uint8_t *aa = a;
114 6368
        const uint8_t *bb = b;
115
116 26961
        while (*aa && vct_lowertab[*aa] == vct_lowertab[*bb]) {
117 20593
                aa++;
118 20593
                bb++;
119 20593
        }
120 6368
        if (!*aa && !*bb)
121 3704
                return (0);
122 2664
        if (!*aa)
123 1
                return (-1);
124 2663
        if (!*bb)
125 13
                return (1);
126 2650
        return ((int)vct_lowertab[*aa] - (int)vct_lowertab[*bb]);
127 2663
}
128
129
static inline int
130
vct_caselencmp(const void *a, const void *b, ssize_t sz)
131 599751
{
132 599751
        const uint8_t *aa = a;
133 599751
        const uint8_t *bb = b;
134
135 599751
        assert(sz >= 0);
136 2067124
        while (sz > 0 && *aa && vct_lowertab[*aa] == vct_lowertab[*bb]) {
137 1467373
                aa++;
138 1467373
                bb++;
139 1467373
                sz--;
140 1467373
        }
141 599751
        if (!sz || (!*aa && !*bb))
142 155520
                return (0);
143 444231
        if (!*aa)
144 1
                return (-1);
145 444230
        if (!*bb)
146 7
                return (1);
147 444223
        return ((int)vct_lowertab[*aa] - (int)vct_lowertab[*bb]);
148 444230
}