summaryrefslogtreecommitdiff
path: root/src/base64.c
blob: 79909ea73310a35fd51ff53ebc459c3c33f7368c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of libnsutils.
 *
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

/**
 * \file
 * Base64 encoding and decoding implementation.
 *
 * Implements RFC4648 (https://tools.ietf.org/html/rfc4648)
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include "nsutils/base64.h"

static const uint8_t b64_decoding_table[256] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /*  0 -  7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /*  8 -  F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 10 - 17 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 18 - 1F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 20 - 27 */
    0xff, 0xff, 0xff,   62, 0xff, 0xff, 0xff,   63, /* 28 - 2F */
      52,   53,   54,   55,   56,   57,   58,   59, /* 30 - 37 */
      60,   61, 0xff, 0xff, 0xff,   64, 0xff, 0xff, /* 38 - 3F */
    0xff,    0,    1,    2,    3,    4,    5,    6, /* 40 - 47 */
       7,    8,    9,   10,   11,   12,   13,   14, /* 48 - 4F */
      15,   16,   17,   18,   19,   20,   21,   22, /* 50 - 57 */
      23,   24,   25, 0xff, 0xff, 0xff, 0xff, 0xff, /* 58 - 5F */
    0xff,   26,   27,   28,   29,   30,   31,   32, /* 60 - 67 */
      33,   34,   35,   36,   37,   38,   39,   40, /* 68 - 6F */
      41,   42,   43,   44,   45,   46,   47,   48, /* 70 - 77 */
      49,   50,   51, 0xff, 0xff, 0xff, 0xff, 0xff, /* 78 - 7F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 - 87 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 88 - 8F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 90 - 97 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 98 - 9F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* A0 - A7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* A8 - AF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* B0 - B7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* B8 - BF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* C0 - C7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* C8 - CF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* D0 - D7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* D8 - DF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* E0 - E7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* E8 - EF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* F0 - F7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* F8 - FF */
};
static const uint8_t b64_encoding_table[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'};
static const uint8_t b64url_decoding_table[256] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /*  0 -  7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /*  8 -  F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 10 - 17 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 18 - 1F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 20 - 27 */
    0xff, 0xff, 0xff, 0xff, 0xff,   62, 0xff, 0xff, /* 28 - 2F */
      52,   53,   54,   55,   56,   57,   58,   59, /* 30 - 37 */
      60,   61, 0xff, 0xff, 0xff,   64, 0xff, 0xff, /* 38 - 3F */
    0xff,    0,    1,    2,    3,    4,    5,    6, /* 40 - 47 */
       7,    8,    9,   10,   11,   12,   13,   14, /* 48 - 4F */
      15,   16,   17,   18,   19,   20,   21,   22, /* 50 - 57 */
      23,   24,   25, 0xff, 0xff, 0xff, 0xff,   63, /* 58 - 5F */
    0xff,   26,   27,   28,   29,   30,   31,   32, /* 60 - 67 */
      33,   34,   35,   36,   37,   38,   39,   40, /* 68 - 6F */
      41,   42,   43,   44,   45,   46,   47,   48, /* 70 - 77 */
      49,   50,   51, 0xff, 0xff, 0xff, 0xff, 0xff, /* 78 - 7F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 - 87 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 88 - 8F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 90 - 97 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 98 - 9F */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* A0 - A7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* A8 - AF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* B0 - B7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* B8 - BF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* C0 - C7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* C8 - CF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* D0 - D7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* D8 - DF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* E0 - E7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* E8 - EF */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* F0 - F7 */
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* F8 - FF */
};
static const uint8_t b64url_encoding_table[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '-', '_'};
static unsigned int mod_table[] = {0, 2, 1};


/**
 * Base 64 encode data using a given encoding table.
 *
 * allocate a buffer and encode source data into it using the Base64 encoding.
 *
 * \param input The source data to encode.
 * \param input_length The length of the source data.
 * \param output The buffer to recive data into, the caller must free.
 * \param output_length The length of data placed in \a output
 * \return NSERROR_OK on success and \a output updated else error code.
 */
static nsuerror
table_encode_alloc(const uint8_t *encoding_table,
                   const uint8_t *input,
                   size_t input_length,
                   uint8_t **output,
                   size_t *output_length)
{
        uint8_t *encoded;
        size_t encoded_len;
        size_t i; /* input index */
        size_t j; /* output index */

        encoded_len = 4 * ((input_length + 2) / 3);

        encoded = malloc(encoded_len);
        if (encoded == NULL) {
                return NSUERROR_NOMEM;
        }

        for (i = 0, j = 0; i < input_length;) {

                uint32_t octet_a = i < input_length ? input[i++] : 0;
                uint32_t octet_b = i < input_length ? input[i++] : 0;
                uint32_t octet_c = i < input_length ? input[i++] : 0;

                uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

                encoded[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
                encoded[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
                encoded[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
                encoded[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
        }

        for (i = 0; i < mod_table[input_length % 3]; i++) {
                encoded[encoded_len - 1 - i] = '=';
        }

        *output = encoded;
        *output_length = encoded_len;

        return NSUERROR_OK;
}

/*
 * standard base64 encoding
 *
 * exported interface documented in nsutils/base64.h
 */
nsuerror
nsu_base64_encode_alloc(const uint8_t *input,
                        size_t input_length,
                        uint8_t **output,
                        size_t *output_length)
{
        return table_encode_alloc(b64_encoding_table,
                                  input,
                                  input_length,
                                  output,
                                  output_length);
}

/*
 * url base64 encoding
 *
 * exported interface documented in nsutils/base64.h
 */
nsuerror
nsu_base64_encode_alloc_url(const uint8_t *input,
                            size_t input_length,
                            uint8_t **output,
                            size_t *output_length)
{
        return table_encode_alloc(b64url_encoding_table,
                                  input,
                                  input_length,
                                  output,
                                  output_length);
}


/**
 * Base 64 decode data with a given decoding table.
 *
 * Allocate a buffer and decode source data into it using the Base64 decoding
 * table.
 *
 * \param input The source data to decode.
 * \param input_length The length of the source data.
 * \param output The buffer to recive data into, the caller must free.
 * \param output_length The length of data placed in \a output
 * \return NSERROR_OK on success and \a output updated else error code.
 */
static nsuerror
base64_decode_alloc(const uint8_t *decoding_table,
                    const uint8_t *input,
                    size_t input_length,
                    uint8_t **output,
                    size_t *output_length)
{
        uint8_t *decoded;
        size_t decoded_len;
        size_t idx;
        size_t opidx;
        uint8_t sextet[4];
        int sextet_idx;

        decoded_len = ((input_length + 3) / 4) * 3;
        if (input[input_length - 1] == '=') (decoded_len)--;
        if (input[input_length - 2] == '=') (decoded_len)--;

        decoded = malloc(decoded_len);
        if (decoded == NULL) {
                return NSUERROR_NOMEM;
        }

        sextet_idx = 0;
        idx = 0;
        opidx = 0;
        while (idx < input_length) {
                sextet[sextet_idx] = decoding_table[input[idx++]];
                if (sextet[sextet_idx] >= 64) {
                        /* not in encoding set */
                        if (sextet[sextet_idx] == 64) {
                                break; /* pad recived - input complete */
                        }
                } else {
                        sextet_idx++;
                        if (sextet_idx == 4) {
                                if (opidx >= (decoded_len - 3)) {
                                        break; /* insufficient output buffer space */
                                }
                                decoded[opidx++] = (sextet[0] << 2) | (sextet[1] >> 4);
                                decoded[opidx++] = (sextet[1] << 4) | (sextet[2] >> 2);
                                decoded[opidx++] = (sextet[2] << 6) | (sextet[3]);

                                sextet_idx = 0;
                        }
                }
        }

        /* deal with any remaining recived bytes ensuring output buffer is not overrun */
        switch (sextet_idx) {
        case 1:
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[0] << 2);
                }
                break;

        case 2:
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[0] << 2) | (sextet[1] >> 4);
                }
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[1] << 4);
                }
                break;

        case 3:
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[0] << 2) | (sextet[1] >> 4);
                }
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[1] << 4) | (sextet[2] >> 2);
                }
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[2] << 6);
                }
                break;

        case 4:
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[0] << 2) | (sextet[1] >> 4);
                }
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[1] << 4) | (sextet[2] >> 2);
                }
                if (opidx < decoded_len) {
                        decoded[opidx++] = (sextet[2] << 6) | (sextet[3]);
                }
                break;
        }

        *output = decoded;
        *output_length = opidx;

        return NSUERROR_OK;
}


/*
 * standard base64 decoding
 *
 * exported interface documented in nsutils/base64.h
 */
nsuerror
nsu_base64_decode_alloc(const uint8_t *input,
                        size_t input_length,
                        uint8_t **output,
                        size_t *output_length)
{
        return base64_decode_alloc(b64_decoding_table,
                                   input,
                                   input_length,
                                   output,
                                   output_length);
}


/*
 * standard base64 decoding
 *
 * exported interface documented in nsutils/base64.h
 */
nsuerror
nsu_base64_decode_alloc_url(const uint8_t *input,
                            size_t input_length,
                            uint8_t **output,
                            size_t *output_length)
{
        return base64_decode_alloc(b64url_decoding_table,
                                   input,
                                   input_length,
                                   output,
                                   output_length);
}