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