Coverage Report

Created: 2026-12-01 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lighttpd1.4/src/burl.c
Line
Count
Source
1
/*
2
 * burl - buffer URL normalization
3
 *
4
 * Copyright(c) 2018 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
5
 * License: BSD 3-clause (same as lighttpd)
6
 */
7
#include "first.h"
8
#include "burl.h"
9
10
#include <string.h>
11
12
#include "buffer.h"
13
#include "base64.h"
14
15
static const char hex_chars_uc[] = "0123456789ABCDEF";
16
17
/* everything except: ! $ & ' ( ) * + , - . / 0-9 : ; = ? @ A-Z _ a-z ~ */
18
static const char encoded_chars_http_uri_reqd[] = {
19
  /*
20
  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
21
  */
22
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  00 -  0F control chars */
23
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  10 -  1F */
24
  1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  20 -  2F space " # % */
25
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,  /*  30 -  3F < > */
26
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  40 -  4F */
27
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,  /*  50 -  5F [ \ ] ^ */
28
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  60 -  6F ` */
29
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,  /*  70 -  7F { | } DEL */
30
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  80 -  8F */
31
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  90 -  9F */
32
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  A0 -  AF */
33
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  B0 -  BF */
34
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  C0 -  CF */
35
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  D0 -  DF */
36
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  E0 -  EF */
37
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  F0 -  FF */
38
};
39
40
41
/* c (char) and n (nibble) MUST be unsigned integer types */
42
#define li_cton(c,n) \
43
1.73M
  (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
44
45
/* b (byte) MUST be unsigned integer type
46
 * https://en.wikipedia.org/wiki/UTF-8
47
 * detect invalid UTF-8 byte and byte in overlong encoding of 7-bit ASCII
48
 * (but does not detect other invalid/overlong multibyte encoding sequences) */
49
1.44M
#define li_utf8_invalid_byte(b) light_utf8_invalid_byte(b)
50
51
52
static int burl_is_unreserved (const int c)
53
43.7k
{
54
43.7k
    return (light_isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~');
55
43.7k
}
56
57
58
__attribute_cold__
59
static int burl_normalize_basic_unreserved_fix (buffer *b, buffer *t, int i, int qs)
60
617
{
61
617
    int j = i;
62
617
    const int used = (int)buffer_clen(b);
63
617
    const unsigned char * const s = (unsigned char *)b->ptr;
64
617
    unsigned char * const p =
65
617
      (unsigned char *)buffer_string_prepare_copy(t,i+(used-i)*3+1);
66
617
    unsigned int n1, n2;
67
617
    int invalid_utf8 = 0;
68
617
    memcpy(p, s, (size_t)i);
69
732k
    for (; i < used; ++i, ++j) {
70
731k
        if (!encoded_chars_http_uri_reqd[s[i]]) {
71
161k
            p[j] = s[i];
72
161k
            if (__builtin_expect( (s[i] == '?'), 0) && -1 == qs) qs = j;
73
161k
        }
74
569k
        else if (s[i]=='%' && li_cton(s[i+1], n1) && li_cton(s[i+2], n2)) {
75
24.1k
            const unsigned int x = (n1 << 4) | n2;
76
24.1k
            if (burl_is_unreserved(x)) {
77
4.05k
                p[j] = x;
78
4.05k
            }
79
20.0k
            else {
80
20.0k
                p[j]   = '%';
81
20.0k
                p[++j] = hex_chars_uc[n1]; /*(s[i+1] & 0xdf)*/
82
20.0k
                p[++j] = hex_chars_uc[n2]; /*(s[i+2] & 0xdf)*/
83
20.0k
                invalid_utf8 |= li_utf8_invalid_byte(x);
84
20.0k
            }
85
24.1k
            i+=2;
86
24.1k
        }
87
545k
        else if (s[i] == '#') break; /* ignore fragment */
88
545k
        else {
89
545k
            p[j]   = '%';
90
545k
            p[++j] = hex_chars_uc[(s[i] >> 4) & 0xF];
91
545k
            p[++j] = hex_chars_uc[s[i] & 0xF];
92
545k
            invalid_utf8 |= li_utf8_invalid_byte(s[i]);
93
545k
        }
94
731k
    }
95
617
    buffer_copy_string_len(b, (char *)p, (size_t)j);
96
617
    return !invalid_utf8 ? qs : -2;
97
617
}
98
99
100
static int burl_normalize_basic_unreserved (buffer *b, buffer *t)
101
1.06k
{
102
1.06k
    const unsigned char * const s = (unsigned char *)b->ptr;
103
1.06k
    const int used = (int)buffer_clen(b);
104
1.06k
    unsigned int n1, n2, x;
105
1.06k
    int qs = -1;
106
1.06k
    int invalid_utf8 = 0;
107
108
110k
    for (int i = 0; i < used; ++i) {
109
109k
        if (!encoded_chars_http_uri_reqd[s[i]]) {
110
89.4k
            if (__builtin_expect( (s[i] == '?'), 0) && -1 == qs) qs = i;
111
89.4k
        }
112
20.1k
        else if (s[i]=='%' && li_cton(s[i+1], n1) && li_cton(s[i+2], n2)
113
19.6k
                 && !burl_is_unreserved((x = (n1 << 4) | n2))) {
114
19.5k
            invalid_utf8 |= li_utf8_invalid_byte(x);
115
19.5k
            if (s[i+1] >= 'a') b->ptr[i+1] &= 0xdf; /* uppercase hex */
116
19.5k
            if (s[i+2] >= 'a') b->ptr[i+2] &= 0xdf; /* uppercase hex */
117
19.5k
            i+=2;
118
19.5k
        }
119
618
        else if (s[i] == '#') { /* ignore fragment */
120
1
            buffer_truncate(b, (size_t)i);
121
1
            break;
122
1
        }
123
617
        else {
124
617
            qs = burl_normalize_basic_unreserved_fix(b, t, i, qs);
125
617
            break;
126
617
        }
127
109k
    }
128
129
1.06k
    return !invalid_utf8 ? qs : -2;
130
1.06k
}
131
132
133
__attribute_cold__
134
static int burl_normalize_basic_required_fix (buffer *b, buffer *t, int i, int qs)
135
573
{
136
573
    int j = i;
137
573
    const int used = (int)buffer_clen(b);
138
573
    const unsigned char * const s = (unsigned char *)b->ptr;
139
573
    unsigned char * const p =
140
573
      (unsigned char *)buffer_string_prepare_copy(t,i+(used-i)*3+1);
141
573
    unsigned int n1, n2;
142
573
    int invalid_utf8 = 0;
143
573
    memcpy(p, s, (size_t)i);
144
969k
    for (; i < used; ++i, ++j) {
145
968k
        if (!encoded_chars_http_uri_reqd[s[i]]) {
146
130k
            p[j] = s[i];
147
130k
            if (__builtin_expect( (s[i] == '?'), 0) && -1 == qs) qs = j;
148
130k
        }
149
838k
        else if (s[i]=='%' && li_cton(s[i+1], n1) && li_cton(s[i+2], n2)) {
150
26.9k
            const unsigned int x = (n1 << 4) | n2;
151
26.9k
            if (!encoded_chars_http_uri_reqd[x]
152
9.03k
                && (qs < 0
153
9.03k
                    ? (x != '/' && x != '?')
154
9.03k
                    : (x != '&' && x != '=' && x != ';' && x != '+'))) {
155
5.94k
                p[j] = x;
156
5.94k
            }
157
20.9k
            else {
158
20.9k
                p[j]   = '%';
159
20.9k
                p[++j] = hex_chars_uc[n1]; /*(s[i+1] & 0xdf)*/
160
20.9k
                p[++j] = hex_chars_uc[n2]; /*(s[i+2] & 0xdf)*/
161
20.9k
                invalid_utf8 |= li_utf8_invalid_byte(x);
162
20.9k
            }
163
26.9k
            i+=2;
164
26.9k
        }
165
811k
        else if (s[i] == '#') break; /* ignore fragment */
166
811k
        else {
167
811k
            p[j]   = '%';
168
811k
            p[++j] = hex_chars_uc[(s[i] >> 4) & 0xF];
169
811k
            p[++j] = hex_chars_uc[s[i] & 0xF];
170
811k
            invalid_utf8 |= li_utf8_invalid_byte(s[i]);
171
811k
        }
172
968k
    }
173
573
    buffer_copy_string_len(b, (char *)p, (size_t)j);
174
573
    return !invalid_utf8 ? qs : -2;
175
573
}
176
177
178
static int burl_normalize_basic_required (buffer *b, buffer *t)
179
1.27k
{
180
1.27k
    const unsigned char * const s = (unsigned char *)b->ptr;
181
1.27k
    const int used = (int)buffer_clen(b);
182
1.27k
    unsigned int n1, n2, x;
183
1.27k
    int qs = -1;
184
1.27k
    int invalid_utf8 = 0;
185
186
151k
    for (int i = 0; i < used; ++i) {
187
151k
        if (!encoded_chars_http_uri_reqd[s[i]]) {
188
121k
            if (__builtin_expect( (s[i] == '?'), 0) && -1 == qs) qs = i;
189
121k
        }
190
29.5k
        else if (s[i]=='%' && li_cton(s[i+1], n1) && li_cton(s[i+2], n2)
191
29.0k
                 && (encoded_chars_http_uri_reqd[(x = (n1 << 4) | n2)]
192
7.10k
                     || (qs < 0
193
7.10k
                         ? (x == '/' || x == '?')
194
28.9k
                         : (x == '&' || x == '=' || x == ';' || x == '+')))) {
195
28.9k
            invalid_utf8 |= li_utf8_invalid_byte(x);
196
28.9k
            if (s[i+1] >= 'a') b->ptr[i+1] &= 0xdf; /* uppercase hex */
197
28.9k
            if (s[i+2] >= 'a') b->ptr[i+2] &= 0xdf; /* uppercase hex */
198
28.9k
            i+=2;
199
28.9k
        }
200
575
        else if (s[i] == '#') { /* ignore fragment */
201
2
            buffer_truncate(b, (size_t)i);
202
2
            break;
203
2
        }
204
573
        else {
205
573
            qs = burl_normalize_basic_required_fix(b, t, i, qs);
206
573
            break;
207
573
        }
208
151k
    }
209
210
1.27k
    return !invalid_utf8 ? qs : -2;
211
1.27k
}
212
213
214
static int burl_contains_ctrls (const buffer *b)
215
753
{
216
753
    const char * const s = b->ptr;
217
753
    const int used = (int)buffer_clen(b);
218
706k
    for (int i = 0; i < used; ++i) {
219
705k
        if (s[i] == '%' && (s[i+1] < '2' || (s[i+1] == '7' && s[i+2] == 'F')))
220
105
            return 1;
221
705k
    }
222
648
    return 0;
223
753
}
224
225
226
__attribute_cold__
227
static void burl_normalize_qs20_to_plus_fix (buffer *b, int i)
228
0
{
229
0
    char * const s = b->ptr;
230
0
    const int used = (int)buffer_clen(b);
231
0
    int j = i;
232
0
    for (; i < used; ++i, ++j) {
233
0
        s[j] = s[i];
234
0
        if (s[i] == '%' && s[i+1] == '2' && s[i+2] == '0') {
235
0
            s[j] = '+';
236
0
            i+=2;
237
0
        }
238
0
    }
239
0
    buffer_truncate(b, j);
240
0
}
241
242
243
static void burl_normalize_qs20_to_plus (buffer *b, int qs)
244
0
{
245
0
    const char * const s = b->ptr;
246
0
    const int used = qs < 0 ? 0 : (int)buffer_clen(b);
247
0
    int i;
248
0
    if (qs < 0) return;
249
0
    for (i = qs+1; i < used; ++i) {
250
0
        if (s[i] == '%' && s[i+1] == '2' && s[i+2] == '0') break;
251
0
    }
252
0
    if (i != used) burl_normalize_qs20_to_plus_fix(b, i);
253
0
}
254
255
256
__attribute_cold__
257
static int burl_normalize_2F_to_slash_fix (buffer *b, int qs, int i)
258
0
{
259
0
    char * const s = b->ptr;
260
0
    const int blen = (int)buffer_clen(b);
261
0
    const int used = qs < 0 ? blen : qs;
262
0
    int j = i;
263
0
    for (; i < used; ++i, ++j) {
264
0
        s[j] = s[i];
265
0
        if (s[i] == '%' && s[i+1] == '2' && s[i+2] == 'F') {
266
0
            s[j] = '/';
267
0
            i+=2;
268
0
        }
269
0
    }
270
0
    if (qs >= 0) {
271
0
        const int qslen = blen - qs;
272
0
        memmove(s+j, s+qs, (size_t)qslen);
273
0
        qs = j;
274
0
        j += qslen;
275
0
    }
276
0
    buffer_truncate(b, j);
277
0
    return qs;
278
0
}
279
280
281
static int burl_normalize_2F_to_slash (buffer *b, int qs, int flags)
282
0
{
283
    /*("%2F" must already have been uppercased during normalization)*/
284
0
    const char * const s = b->ptr;
285
0
    const int used = qs < 0 ? (int)buffer_clen(b) : qs;
286
0
    for (int i = 0; i < used; ++i) {
287
0
        if (s[i] == '%' && s[i+1] == '2' && s[i+2] == 'F') {
288
0
            return !(flags & HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)
289
0
              ? burl_normalize_2F_to_slash_fix(b, qs, i)     /* _DECODE */
290
0
              : -2; /*(flags & HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)*/
291
0
        }
292
0
    }
293
0
    return qs;
294
0
}
295
296
297
static int burl_normalize_path (buffer *b, buffer *t, int qs, int flags)
298
0
{
299
0
    const unsigned char * const s = (unsigned char *)b->ptr;
300
0
    const int used = (int)buffer_clen(b);
301
0
    int path_simplify = 0;
302
0
    for (int i = 0, len = qs < 0 ? used : qs; i < len; ++i) {
303
0
        if (s[i] == '.' && (s[i+1] != '.' || ++i)
304
0
            && (s[i+1] == '/' || s[i+1] == '?' || s[i+1] == '\0')) {
305
0
            path_simplify = 1;
306
0
            break;
307
0
        }
308
0
        while (i < len && s[i] != '/') ++i;
309
0
        if (s[i] == '/' && s[i+1] == '/') { /*(s[len] != '/')*/
310
0
            path_simplify = 1;
311
0
            break;
312
0
        }
313
0
    }
314
315
0
    if (path_simplify) {
316
0
        if (flags & HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REJECT) return -2;
317
0
        if (qs >= 0) {
318
0
            buffer_copy_string_len(t, b->ptr+qs, used - qs);
319
0
            buffer_truncate(b, qs);
320
0
        }
321
322
0
        buffer_path_simplify(b);
323
324
0
        if (qs >= 0) {
325
0
            qs = (int)buffer_clen(b);
326
0
            buffer_append_string_len(b, BUF_PTR_LEN(t));
327
0
        }
328
0
    }
329
330
0
    return qs;
331
0
}
332
333
334
__attribute_cold__
335
__attribute_noinline__
336
__attribute_pure__
337
480
static int burl_scan_qmark (const buffer * const b) {
338
480
    const char * const qmark = strchr(b->ptr, '?');
339
480
    return qmark ? (int)(qmark - b->ptr) : -1;
340
480
}
341
342
343
int burl_normalize (buffer *b, buffer *t, int flags)
344
2.34k
{
345
2.34k
    int qs;
346
347
  #if defined(_WIN32) || defined(__CYGWIN__)
348
    /* Windows and Cygwin treat '\\' as '/' if '\\' is present in path;
349
     * convert to '/' for consistency before percent-encoding
350
     * normalization which will convert '\\' to "%5C" in the URL.
351
     * (Clients still should not be sending '\\' unencoded in requests.) */
352
    if (flags & HTTP_PARSEOPT_URL_NORMALIZE_PATH_BACKSLASH_TRANS) {
353
        for (char *p = b->ptr; *p != '?' && *p != '\0'; ++p) {
354
            if (*p == '\\') *p = '/';
355
            if (p[0] == '%' && p[1] == '5' && (p[2] | 0x20) == 'c') {
356
                p[1] = '2';
357
                p[2] = 'F';
358
                p += 2;
359
            }
360
        }
361
    }
362
  #endif
363
364
2.34k
    qs = (flags & HTTP_PARSEOPT_URL_NORMALIZE_REQUIRED)
365
2.34k
      ? burl_normalize_basic_required(b, t)
366
2.34k
      : burl_normalize_basic_unreserved(b, t);
367
2.34k
    if (-2 == qs) {
368
480
        if (flags & HTTP_PARSEOPT_URL_NORMALIZE_INVALID_UTF8_REJECT) return -2;
369
480
        qs = burl_scan_qmark(b);
370
480
    }
371
372
2.34k
    if (flags & HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT) {
373
753
        if (burl_contains_ctrls(b)) return -2;
374
753
    }
375
376
2.23k
    if (flags & (HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE
377
2.23k
                |HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)) {
378
0
        qs = burl_normalize_2F_to_slash(b, qs, flags);
379
0
        if (-2 == qs) return -2;
380
0
    }
381
382
2.23k
    if (flags & (HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE
383
2.23k
                |HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REJECT)) {
384
0
        qs = burl_normalize_path(b, t, qs, flags);
385
0
        if (-2 == qs) return -2;
386
0
    }
387
388
2.23k
    if (flags & HTTP_PARSEOPT_URL_NORMALIZE_QUERY_20_PLUS) {
389
0
        if (qs >= 0) burl_normalize_qs20_to_plus(b, qs);
390
0
    }
391
392
2.23k
    return qs;
393
2.23k
}
394
395
396
static void burl_append_encode_nde (buffer * const b, const char * const str, const size_t len)
397
0
{
398
    /* percent-encodes everything except unreserved  - . 0-9 A-Z _ a-z ~
399
     * unless already percent-encoded (does not double-encode) */
400
    /* Note: not checking for invalid UTF-8 */
401
0
    char * const p = buffer_string_prepare_append(b, len*3);
402
0
    unsigned int n1, n2;
403
0
    int j = 0;
404
0
    for (unsigned int i = 0; i < len; ++i, ++j) {
405
0
        if (str[i]=='%' && li_cton(str[i+1], n1) && li_cton(str[i+2], n2)) {
406
0
            const unsigned int x = (n1 << 4) | n2;
407
0
            if (burl_is_unreserved((int)x)) {
408
0
                p[j] = (char)x;
409
0
            }
410
0
            else { /* leave UTF-8, control chars, and required chars encoded */
411
0
                p[j]   = '%';
412
0
                p[++j] = str[i+1];
413
0
                p[++j] = str[i+2];
414
0
            }
415
0
            i+=2;
416
0
        }
417
0
        else if (burl_is_unreserved(str[i])) {
418
0
            p[j] = str[i];
419
0
        }
420
0
        else {
421
0
            p[j]   = '%';
422
0
            p[++j] = hex_chars_uc[(str[i] >> 4) & 0xF];
423
0
            p[++j] = hex_chars_uc[str[i] & 0xF];
424
0
        }
425
0
    }
426
0
    buffer_commit(b, j);
427
0
}
428
429
430
static void burl_append_encode_psnde (buffer * const b, const char * const str, const size_t len)
431
0
{
432
    /* percent-encodes everything except unreserved  - . 0-9 A-Z _ a-z ~ plus /
433
     * unless already percent-encoded (does not double-encode) */
434
    /* Note: not checking for invalid UTF-8 */
435
0
    char * const p = buffer_string_prepare_append(b, len*3);
436
0
    unsigned int n1, n2;
437
0
    int j = 0;
438
0
    for (unsigned int i = 0; i < len; ++i, ++j) {
439
0
        if (str[i]=='%' && li_cton(str[i+1], n1) && li_cton(str[i+2], n2)) {
440
0
            const unsigned int x = (n1 << 4) | n2;
441
0
            if (burl_is_unreserved((int)x)) {
442
0
                p[j] = (char)x;
443
0
            }
444
0
            else { /* leave UTF-8, control chars, and required chars encoded */
445
0
                p[j]   = '%';
446
0
                p[++j] = str[i+1];
447
0
                p[++j] = str[i+2];
448
0
            }
449
0
            i+=2;
450
0
        }
451
0
        else if (burl_is_unreserved(str[i]) || str[i] == '/') {
452
0
            p[j] = str[i];
453
0
        }
454
0
        else {
455
0
            p[j]   = '%';
456
0
            p[++j] = hex_chars_uc[(str[i] >> 4) & 0xF];
457
0
            p[++j] = hex_chars_uc[str[i] & 0xF];
458
0
        }
459
0
    }
460
0
    buffer_commit(b, j);
461
0
}
462
463
464
static void burl_append_encode_all (buffer * const b, const char * const str, const size_t len)
465
0
{
466
    /* percent-encodes everything except unreserved  - . 0-9 A-Z _ a-z ~
467
     * Note: double-encodes any existing '%') */
468
    /* Note: not checking for invalid UTF-8 */
469
0
    char * const p = buffer_string_prepare_append(b, len*3);
470
0
    int j = 0;
471
0
    for (unsigned int i = 0; i < len; ++i, ++j) {
472
0
        if (burl_is_unreserved(str[i])) {
473
0
            p[j] = str[i];
474
0
        }
475
0
        else {
476
0
            p[j]   = '%';
477
0
            p[++j] = hex_chars_uc[(str[i] >> 4) & 0xF];
478
0
            p[++j] = hex_chars_uc[str[i] & 0xF];
479
0
        }
480
0
    }
481
0
    buffer_commit(b, j);
482
0
}
483
484
485
static void burl_offset_tolower (buffer * const b, const size_t off)
486
0
{
487
    /*(skips over all percent-encodings, including encoding of alpha chars)*/
488
0
    for (char *p = b->ptr+off; p[0]; ++p) {
489
0
        if (light_isupper(p[0])) p[0] |= 0x20;
490
0
        else if (p[0]=='%' && light_isxdigit(p[1]) && light_isxdigit(p[2]))
491
0
            p+=2;
492
0
    }
493
0
}
494
495
496
static void burl_offset_toupper (buffer * const b, const size_t off)
497
0
{
498
    /*(skips over all percent-encodings, including encoding of alpha chars)*/
499
0
    for (char *p = b->ptr+off; p[0]; ++p) {
500
0
        if (light_islower(p[0])) p[0] &= 0xdf;
501
0
        else if (p[0]=='%' && light_isxdigit(p[1]) && light_isxdigit(p[2]))
502
0
            p+=2;
503
0
    }
504
0
}
505
506
507
void burl_append (buffer * const b, const char * const str, const size_t len, const int flags)
508
0
{
509
0
    size_t off = 0;
510
511
0
    if (0 == len) return;
512
513
0
    if (0 == flags) {
514
0
        buffer_append_string_len(b, str, len);
515
0
        return;
516
0
    }
517
518
0
    if (flags & (BURL_TOUPPER|BURL_TOLOWER)) off = buffer_clen(b);
519
520
0
    if (flags & BURL_ENCODE_NONE) {
521
0
        buffer_append_string_len(b, str, len);
522
0
    }
523
0
    else if (flags & BURL_ENCODE_ALL) {
524
0
        burl_append_encode_all(b, str, len);
525
0
    }
526
0
    else if (flags & BURL_ENCODE_NDE) {
527
0
        burl_append_encode_nde(b, str, len);
528
0
    }
529
0
    else if (flags & BURL_ENCODE_PSNDE) {
530
0
        burl_append_encode_psnde(b, str, len);
531
0
    }
532
0
    else if (flags & BURL_ENCODE_B64U) {
533
0
        const unsigned char *s = (const unsigned char *)str;
534
0
        buffer_append_base64_encode_no_padding(b, s, len, BASE64_URL);
535
0
    }
536
0
    else if (flags & BURL_DECODE_B64U) {
537
0
        buffer_append_base64_decode(b, str, len, BASE64_URL);
538
0
    }
539
540
    /* note: not normalizing str, which could come from arbitrary header,
541
     * so it is possible that alpha chars are percent-encoded upper/lowercase */
542
0
    if (flags & (BURL_TOLOWER|BURL_TOUPPER)) {
543
0
        (flags & BURL_TOLOWER)
544
0
          ? burl_offset_tolower(b, off)  /*(flags & BURL_TOLOWER)*/
545
0
          : burl_offset_toupper(b, off); /*(flags & BURL_TOUPPER)*/
546
0
    }
547
0
}