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