varnish-cache/vmod/vmod_blob_id.c
1
/*-
2
 * Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Nils Goroll <nils.goroll@uplex.de>
6
 *          Geoffrey Simmons <geoffrey.simmons@uplex.de>
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 are met:
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright notice,
15
 *    this list of conditions and the following disclaimer in the documentation
16
 *    and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include <string.h>
34
35
#include "vdef.h"
36
#include "vrt.h"
37
#include "vas.h"
38
39
#include "vmod_blob.h"
40
41
size_t
42 80
id_encode_l(size_t l)
43
{
44 80
        return (l + 1);
45
}
46
47
size_t
48 400
id_decode_l(size_t l)
49
{
50 400
        return (l);
51
}
52
53
ssize_t
54 1670
id_encode(const enum encoding enc, const enum case_e kase,
55
    blob_dest_t buf, blob_len_t buflen,
56
    blob_src_t in, blob_len_t inlen)
57
{
58 1670
        (void) enc;
59 1670
        (void) kase;
60 1670
        AN(buf);
61
62 1670
        if (buflen < inlen + 1)
63 40
                return (-1);
64 1630
        if (in == NULL || inlen == 0)
65 240
                return (0);
66
67 1390
        memcpy(buf, in, inlen);
68 1390
        return (inlen);
69 1670
}
70
71
ssize_t
72 1160
id_decode(const enum encoding enc, blob_dest_t buf,
73
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
74
{
75
        const char *s;
76 1160
        char *dest = buf;
77 1160
        size_t len, outlen = 0, c = SIZE_MAX;
78
        int i;
79
80 1160
        (void)enc;
81 1160
        AN(buf);
82 1160
        AN(strings);
83
84 1160
        if (n >= 0)
85 250
                c = n;
86
87 2840
        for (i = 0; c > 0 && i < strings->n; i++) {
88 1690
                s = strings->p[i];
89 1690
                if (s == NULL || *s == '\0')
90 580
                        continue;
91 1110
                len = strlen(s);
92 1110
                if (len > c)
93 200
                        len = c;
94 1110
                c -= len;
95 1110
                if ((outlen += len) > buflen) {
96 10
                        errno = ENOMEM;
97 10
                        return (-1);
98
                }
99 1100
                memcpy(dest, s, len);
100 1100
                dest += len;
101 1100
        }
102
103 1150
        return (outlen);
104 1160
}