blob: dbc616b4691303ff20ea64ce87f5baceed74bfa5 (
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
|
#pragma once
#include <stdint.h>
#include <stddef.h>
static inline int base64_decode_value(unsigned char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
if (c == '=') return -2;
return -1;
}
static inline int decode_base64(const unsigned char *in, size_t in_len, unsigned char *out) {
size_t out_len = 0;
int buf = 0;
int bits = 0;
for (size_t i = 0; i < in_len; ++i) {
int v = base64_decode_value(in[i]);
if (v == -1) {
continue;
}
if (v == -2) {
break;
}
buf = (buf << 6) | v;
bits += 6;
while (bits >= 8) {
bits -= 8;
out[out_len++] = (unsigned char)((buf >> bits) & 0xFF);
}
}
return (int)out_len;
}
|