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 "vmod_blob.h"
34
35
#include "vdef.h"
36
#include "vrt.h"
37
#include "vas.h"
38
39
size_t
40 96
id_encode_l(size_t l)
41
{
42 96
        return l + 1;
43
}
44
45
size_t
46 480
id_decode_l(size_t l)
47
{
48 480
        return l;
49
}
50
51
ssize_t
52 2052
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 2052
        AN(buf);
59
60 2052
        if (buflen < inlen + 1)
61 60
                return -1;
62 1992
        if (in == NULL || inlen == 0)
63 288
                return 0;
64
65 1704
        memcpy(buf, in, inlen);
66 1704
        return inlen;
67
}
68
69
ssize_t
70 1392
id_decode(const enum encoding enc,
71
          char *restrict const buf, const size_t buflen,
72
          ssize_t n, const char *restrict const p, va_list ap)
73
{
74 1392
        char *dest = buf;
75 1392
        size_t outlen = 0, c = SIZE_MAX;
76
77
        (void) enc;
78 1392
        AN(buf);
79
80 1392
        if (n >= 0)
81 300
                c = n;
82
83 4800
        for (const char *s = p; c > 0 && s != vrt_magic_string_end;
84 2016
             s = va_arg(ap, const char *)) {
85
                size_t len;
86
87 2028
                if (s == NULL || *s == '\0')
88 696
                        continue;
89 1332
                len = strlen(s);
90 1332
                if (len > c)
91 240
                        len = c;
92 1332
                c -= len;
93 1332
                if ((outlen += len) > buflen) {
94 12
                        errno = ENOMEM;
95 12
                        return -1;
96
                }
97 1320
                memcpy(dest, s, len);
98 1320
                dest += len;
99
        }
100
101 1380
        return outlen;
102
}