aboutsummaryrefslogtreecommitdiffstats
path: root/sway/base64.c
diff options
context:
space:
mode:
authorLibravatar nyorain <nyorain@gmail.com>2017-07-07 21:51:34 +0200
committerLibravatar nyorain <nyorain@gmail.com>2017-07-07 21:51:34 +0200
commitc0f2acce4e08eecdcf29058335aece113467daee (patch)
tree7f871abe8b5d1cdf4a74512810d68e9e66f0d57f /sway/base64.c
parentFix/Simplify get_clipboard ipc-server impl (diff)
downloadsway-c0f2acce4e08eecdcf29058335aece113467daee.tar.gz
sway-c0f2acce4e08eecdcf29058335aece113467daee.tar.zst
sway-c0f2acce4e08eecdcf29058335aece113467daee.zip
Rework get_clipboard implementation
Diffstat (limited to 'sway/base64.c')
-rw-r--r--sway/base64.c221
1 files changed, 221 insertions, 0 deletions
diff --git a/sway/base64.c b/sway/base64.c
new file mode 100644
index 00000000..0d51b5c7
--- /dev/null
+++ b/sway/base64.c
@@ -0,0 +1,221 @@
1/*
2 * Adapted from https://github.com/littlstar/b64.c
3 * License under the MIT License:
4 * Copyright (c) 2014 Little Star Media, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include <ctype.h>
26#include <stdlib.h>
27#include "util.h"
28
29static const char b64_table[] = {
30 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
31 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
32 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
33 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
34 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
35 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
36 'w', 'x', 'y', 'z', '0', '1', '2', '3',
37 '4', '5', '6', '7', '8', '9', '+', '/'
38};
39
40char *b64_encode(const char *src, size_t len, size_t *flen) {
41 int i = 0;
42 int j = 0;
43 char *enc = NULL;
44 size_t size = len * 4 / 3;
45 size_t idx = 0;
46 unsigned char buf[4];
47 char tmp[3];
48
49 // alloc
50 enc = (char *) malloc(size + 1);
51 if (NULL == enc) { return NULL; }
52
53 // parse until end of source
54 while (len--) {
55 // read up to 3 bytes at a time into `tmp'
56 tmp[i++] = *(src++);
57
58 // if 3 bytes read then encode into `buf'
59 if (3 == i) {
60 buf[0] = (tmp[0] & 0xfc) >> 2;
61 buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
62 buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
63 buf[3] = tmp[2] & 0x3f;
64
65 // shouldn't really happen
66 if (idx + 4 > size) {
67 size += 16;
68 enc = (char *) realloc(enc, size + 1);
69 }
70 for (i = 0; i < 4; ++i) {
71 enc[idx++] = b64_table[buf[i]];
72 }
73
74 // reset index
75 i = 0;
76 }
77 }
78
79 // remainder
80 if (i > 0) {
81 // fill `tmp' with `\0' at most 3 times
82 for (j = i; j < 3; ++j) {
83 tmp[j] = '\0';
84 }
85
86 // perform same codec as above
87 buf[0] = (tmp[0] & 0xfc) >> 2;
88 buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
89 buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
90 buf[3] = tmp[2] & 0x3f;
91
92 // perform same write to `enc` with new allocation
93 size_t delta = (i > 3 ? 0 : 3 - i) + (j > i + 1 ? 0 : i + 1 - j);
94 if (idx + delta > size) {
95 size += delta;
96 enc = (char *) realloc(enc, size + 1);
97 }
98 for (j = 0; (j < i + 1); ++j) {
99 enc[idx++] = b64_table[buf[j]];
100 }
101
102 // while there is still a remainder
103 // append `=' to `enc'
104 while ((i++ < 3)) {
105 enc[idx++] = '=';
106 }
107 }
108
109 enc[idx] = '\0';
110
111 if (flen)
112 *flen = size;
113 return enc;
114}
115
116unsigned char *b64_decode(const char *src, size_t len, size_t *decsize) {
117 int i = 0;
118 int j = 0;
119 int l = 0;
120 // max size estimate
121 size_t size = len * 3 / 4;
122 size_t idx = 0;
123 unsigned char *dec = NULL;
124 unsigned char buf[3];
125 unsigned char tmp[4];
126
127 // alloc
128 dec = (unsigned char *) malloc(size + 1);
129 if (NULL == dec) { return NULL; }
130
131 // parse until end of source
132 while (len--) {
133 if (isspace(src[j])) { j++; continue; }
134 // break if char is `=' or not base64 char
135 if ('=' == src[j]) { break; }
136 if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }
137
138 // read up to 4 bytes at a time into `tmp'
139 tmp[i++] = src[j++];
140
141 // if 4 bytes read then decode into `buf'
142 if (4 == i) {
143 // translate values in `tmp' from table
144 for (i = 0; i < 4; ++i) {
145 // find translation char in `b64_table'
146 for (l = 0; l < 64; ++l) {
147 if (tmp[i] == b64_table[l]) {
148 tmp[i] = l;
149 break;
150 }
151 }
152 }
153
154 // decode
155 buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
156 buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
157 buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
158
159 // unlikely
160 if (idx + 3 > size) {
161 size += 16;
162 dec = (unsigned char *) realloc(dec, size + 1);
163 }
164 if (dec != NULL){
165 for (i = 0; i < 3; ++i) {
166 dec[idx++] = buf[i];
167 }
168 } else {
169 return NULL;
170 }
171
172 // reset
173 i = 0;
174 }
175 }
176
177 // remainder
178 if (i > 0) {
179 // fill `tmp' with `\0' at most 4 times
180 for (j = i; j < 4; ++j) {
181 tmp[j] = '\0';
182 }
183
184 // translate remainder
185 for (j = 0; j < 4; ++j) {
186 // find translation char in `b64_table'
187 for (l = 0; l < 64; ++l) {
188 if (tmp[j] == b64_table[l]) {
189 tmp[j] = l;
190 break;
191 }
192 }
193 }
194
195 // decode remainder
196 buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
197 buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
198 buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
199
200 // write remainer decoded buffer to `dec'
201 if (idx + (i - 1) > size) {
202 size += 16;
203 dec = (unsigned char *) realloc(dec, size + 1);
204 }
205 if (dec != NULL){
206 for (j = 0; (j < i - 1); ++j) {
207 dec[idx++] = buf[j];
208 }
209 } else {
210 return NULL;
211 }
212 }
213
214 dec[idx] = '\0';
215 // Return back the size of decoded string if demanded.
216 if (decsize != NULL) {
217 *decsize = size;
218 }
219
220 return dec;
221}