varnish-cache/lib/libvmod_blob/base64.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 <errno.h>
31
32
#include "vdef.h"
33
#include "vrt.h"
34
#include "vas.h"
35
36
#include "base64.h"
37
38
#define base64_l(l)             (((l) << 2) / 3)
39
40
size_t
41 70
base64nopad_encode_l(size_t l)
42
{
43 70
        return base64_l(l) + 4;
44
}
45
46
size_t
47 196
base64_encode_l(size_t l)
48
{
49 196
        return (((base64_l(l)) + 3) & ~3) + 1;
50
}
51
52
size_t
53 124
base64_decode_l(size_t l)
54
{
55 124
        return ((l) * 3) >> 2;
56
}
57
58
static inline int
59 5074
decode(char *restrict *restrict dest, const char *restrict const buf,
60
       const size_t buflen, unsigned u, const int n)
61
{
62
        char *d;
63
64 5074
        if (n <= 1) {
65 0
                errno = EINVAL;
66 0
                return -1;
67
        }
68 5074
        d = *dest;
69 20126
        for (int i = 0; i < n - 1; i++) {
70 15054
                if (d == buf + buflen) {
71 2
                        errno = ENOMEM;
72 2
                        return -1;
73
                }
74 15052
                *d++ = (u >> 16) & 0xff;
75 15052
                u <<= 8;
76
        }
77 5072
        *dest += d - *dest;
78 5072
        return 1;
79
}
80
81
ssize_t
82 226
base64_encode(const enum encoding enc, const enum case_e kase,
83
              char *restrict const buf, const size_t buflen,
84
              const char *restrict const inbuf, const size_t inlength)
85
{
86 226
        const struct b64_alphabet *alpha = &b64_alphabet[enc];
87 226
        char *p = buf;
88 226
        const uint8_t *in = (const uint8_t *)inbuf;
89 226
        const uint8_t * const end = in + inlength;
90
91
        (void) kase;
92 226
        AN(buf);
93 226
        AN(alpha);
94 226
        if (in == NULL || inlength == 0)
95 8
                return 0;
96
97 274
        if ((enc == BASE64URLNOPAD &&
98 274
             buflen < base64nopad_encode_l(inlength)) ||
99 162
            (enc != BASE64URLNOPAD &&
100 162
             buflen < base64_encode_l(inlength))) {
101 2
                errno = ENOMEM;
102 2
                return -1;
103
        }
104
105 7130
        while (end - in >= 3) {
106 6698
                *p++ = alpha->b64[(in[0] >> 2) & 0x3f];
107 6698
                *p++ = alpha->b64[((in[0] << 4) | (in[1] >> 4)) & 0x3f];
108 6698
                *p++ = alpha->b64[((in[1] << 2) | (in[2] >> 6)) & 0x3f];
109 6698
                *p++ = alpha->b64[in[2] & 0x3f];
110 6698
                in += 3;
111
        }
112 216
        if (end - in > 0) {
113 184
                *p++ = alpha->b64[(in[0] >> 2) & 0x3f];
114 184
                if (end - in == 1) {
115 144
                        *p++ = alpha->b64[(in[0] << 4) & 0x3f];
116 144
                        if (alpha->padding) {
117 106
                                *p++ = alpha->padding;
118 106
                                *p++ = alpha->padding;
119
                        }
120
                }
121
                else {
122 40
                        *p++ = alpha->b64[((in[0] << 4) | (in[1] >> 4)) & 0x3f];
123 40
                        *p++ = alpha->b64[(in[1] << 2) & 0x3f];
124 40
                        if (alpha->padding) {
125 22
                                *p++ = alpha->padding;
126
                        }
127
                }
128
        }
129 216
        assert(p >= buf && p - buf <= buflen);
130 216
        return p - buf;
131
}
132
133
ssize_t
134 250
base64_decode(const enum encoding dec, char *restrict const buf,
135
              const size_t buflen, ssize_t inlen,
136
              const struct strands *restrict const strings)
137
{
138 250
        const struct b64_alphabet *alpha = &b64_alphabet[dec];
139 250
        char *dest = buf;
140 250
        unsigned u = 0, term = 0;
141 250
        int n = 0;
142 250
        size_t len = SIZE_MAX;
143
144 250
        AN(buf);
145 250
        AN(alpha);
146 250
        AN(strings);
147
148 250
        if (inlen >= 0)
149 94
                len = inlen;
150
151 568
        for (int i = 0; len > 0 && i < strings->n; i++) {
152 362
                const char *s = strings->p[i];
153
154 362
                if (s == NULL)
155 12
                        continue;
156 350
                if (*s && term) {
157 0
                        errno = EINVAL;
158 0
                        return -1;
159
                }
160 5852
                while (*s && len) {
161 30462
                        while (n < 4) {
162 20328
                                char b = alpha->i64[(unsigned) *s++];
163 20328
                                u <<= 6;
164 20328
                                if (b == ILL) {
165 42
                                        errno = EINVAL;
166 42
                                        return -1;
167
                                }
168 20286
                                n++;
169 20286
                                if (b == PAD) {
170 116
                                        term++;
171 116
                                        continue;
172
                                }
173 20170
                                u |= (unsigned) b;
174 20170
                                if (--len == 0)
175 60
                                        break;
176 20110
                                if (!*s)
177 156
                                        break;
178
                        }
179 5154
                        if (n == 4) {
180 5046
                                if (decode(&dest, buf, buflen, u, n-term) < 0)
181 2
                                        return -1;
182 5044
                                n = 0;
183
                        }
184
                }
185
        }
186 206
        if (n) {
187 28
                if (!alpha->padding)
188 26
                        u <<= (6 * (4 - n));
189 28
                if (decode(&dest, buf, buflen, u, n-term) < 0)
190 0
                        return -1;
191
        }
192
193 206
        return dest - buf;
194
}