Code Coverage: vinyl-cache/vmod/vmod_blob_id.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright 2015-2016,2026 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
#include "miniobj.h"
38
39
#include "vmod_blob.h"
40
41
size_t
42
id_encode_l(size_t l)
43 8
{
44 8
        return (l + 1);
45 8
}
46
47
size_t
48
id_decode_l(size_t l)
49 44
{
50 44
        return (l);
51 44
}
52
53
ssize_t
54
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 167
{
58 167
        (void) enc;
59 167
        (void) kase;
60 167
        AN(buf);
61
62 167
        if (buflen < inlen + 1)
63 4
                return (-1);
64 163
        if (in == NULL || inlen == 0)
65 24
                return (0);
66
67 139
        vmemcpy(buf, in, inlen);
68 139
        return (inlen);
69 163
}
70
71
ssize_t
72
id_decode(const enum encoding enc, blob_dest_t buf,
73
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings, size_t *consumedp)
74 123
{
75 123
        const char *s;
76 123
        char *dest = buf;
77 123
        size_t len, outlen = 0, c = SIZE_MAX;
78 123
        ssize_t r;
79 123
        int i;
80
81 123
        (void)enc;
82 123
        AN(buf);
83 123
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
84
85
        /*lint --e{801} goto used as defer pattern */
86
87 123
        if (n >= 0)
88 25
                c = n;
89
90 310
        for (i = 0; c > 0 && i < strings->n; i++) {
91 192
                s = strings->p[i];
92 192
                if (s == NULL || *s == '\0')
93 58
                        continue;
94 134
                len = vstrlen(s);
95 134
                if (len > c)
96 20
                        len = c;
97 134
                c -= len;
98 134
                if ((outlen + len) > buflen) {
99 5
                        vmemcpy(dest, s, buflen - outlen);
100 5
                        outlen = buflen;
101 5
                        errno = ENOMEM;
102 5
                        r = -1;
103 5
                        goto out;
104 5
                }
105 129
                vmemcpy(dest, s, len);
106 129
                outlen += len;
107 129
                dest += len;
108 129
        }
109
110 118
        r = outlen;
111
112 123
    out:
113 123
        if (consumedp)
114 30
                *consumedp = outlen;
115 123
        return (r);
116 118
}