1 /*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "base64.h"
18
19 #include "serd_internal.h"
20 #include "string_utils.h"
21
22 #include "serd/serd.h"
23
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /**
30 Base64 encoding table.
31
32 @see <a href="http://tools.ietf.org/html/rfc3548#section-3">RFC3548 S3</a>.
33 */
34 static const uint8_t b64_map[] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36
37 /**
38 Base64 decoding table.
39
40 This is indexed by encoded characters and returns the numeric value used
41 for decoding, shifted up by 47 to be in the range of printable ASCII.
42 A '$' is a placeholder for characters not in the base64 alphabet.
43 */
44 static const char b64_unmap[] =
45 "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$m$$$ncdefghijkl$$$$$$"
46 "$/0123456789:;<=>?@ABCDEFGH$$$$$$IJKLMNOPQRSTUVWXYZ[\\]^_`ab$$$$"
47 "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
48 "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
49
50 /** Encode 3 raw bytes to 4 base64 characters. */
51 static inline void
encode_chunk(uint8_t out[4],const uint8_t in[3],size_t n_in)52 encode_chunk(uint8_t out[4], const uint8_t in[3], size_t n_in)
53 {
54 out[0] = b64_map[in[0] >> 2];
55 out[1] = b64_map[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)];
56
57 out[2] = (n_in > 1) ? (b64_map[((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6)])
58 : (uint8_t)'=';
59
60 out[3] = ((n_in > 2) ? b64_map[in[2] & 0x3F] : (uint8_t)'=');
61 }
62
63 size_t
serd_base64_get_length(const size_t size,const bool wrap_lines)64 serd_base64_get_length(const size_t size, const bool wrap_lines)
65 {
66 return (size + 2) / 3 * 4 + (wrap_lines * ((size - 1) / 57));
67 }
68
69 bool
serd_base64_encode(uint8_t * const str,const void * const buf,const size_t size,const bool wrap_lines)70 serd_base64_encode(uint8_t* const str,
71 const void* const buf,
72 const size_t size,
73 const bool wrap_lines)
74 {
75 bool has_newline = false;
76
77 for (size_t i = 0, j = 0; i < size; i += 3, j += 4) {
78 uint8_t in[4] = {0, 0, 0, 0};
79 size_t n_in = MIN(3, size - i);
80 memcpy(in, (const uint8_t*)buf + i, n_in);
81
82 if (wrap_lines && i > 0 && (i % 57) == 0) {
83 str[j++] = '\n';
84 has_newline = true;
85 }
86
87 encode_chunk(str + j, in, n_in);
88 }
89
90 return has_newline;
91 }
92
93 static inline uint8_t
unmap(const uint8_t in)94 unmap(const uint8_t in)
95 {
96 return (uint8_t)(b64_unmap[in] - 47);
97 }
98
99 /** Decode 4 base64 characters to 3 raw bytes. */
100 static inline size_t
decode_chunk(const uint8_t in[4],uint8_t out[3])101 decode_chunk(const uint8_t in[4], uint8_t out[3])
102 {
103 out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4);
104 out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2);
105 out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3]));
106 return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '='));
107 }
108
109 void*
serd_base64_decode(const uint8_t * str,size_t len,size_t * size)110 serd_base64_decode(const uint8_t* str, size_t len, size_t* size)
111 {
112 void* buf = malloc((len * 3) / 4 + 2);
113
114 *size = 0;
115 for (size_t i = 0, j = 0; i < len; j += 3) {
116 uint8_t in[] = "====";
117 size_t n_in = 0;
118 for (; i < len && n_in < 4; ++n_in) {
119 for (; i < len && !is_base64(str[i]); ++i) {
120 // Skip junk
121 }
122
123 in[n_in] = str[i++];
124 }
125
126 if (n_in > 1) {
127 *size += decode_chunk(in, (uint8_t*)buf + j);
128 }
129 }
130
131 return buf;
132 }
133