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 8
id_encode_l(size_t l)
41
{
42 8
        return l + 1;
43
}
44
45
size_t
46 40
id_decode_l(size_t l)
47
{
48 40
        return l;
49
}
50
51
ssize_t
52 162
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 162
        AN(buf);
59
60 162
        if (buflen < inlen + 1)
61 0
                return -1;
62 162
        if (in == NULL || inlen == 0)
63 24
                return 0;
64
65 138
        memcpy(buf, in, inlen);
66 138
        return inlen;
67
}
68
69
ssize_t
70 112
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 112
        char *dest = buf;
75 112
        size_t outlen = 0, c = SIZE_MAX;
76
77
        (void) enc;
78 112
        AN(buf);
79
80 112
        if (n >= 0)
81 25
                c = n;
82
83 389
        for (const char *s = p; c > 0 && s != vrt_magic_string_end;
84 165
             s = va_arg(ap, const char *)) {
85
                size_t len;
86
87 165
                if (s == NULL || *s == '\0')
88 57
                        continue;
89 108
                len = strlen(s);
90 108
                if (len > c)
91 20
                        len = c;
92 108
                c -= len;
93 108
                if ((outlen += len) > buflen) {
94 0
                        errno = ENOMEM;
95 0
                        return -1;
96
                }
97 108
                memcpy(dest, s, len);
98 108
                dest += len;
99
        }
100
101 112
        return outlen;
102
}