-
Notifications
You must be signed in to change notification settings - Fork 157
/
base64.c
123 lines (97 loc) · 2.35 KB
/
base64.c
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
/*
This code is public domain software.
*/
#include "base64.h"
#include <stdlib.h>
#include <string.h>
// base64 encoding
//
// buf: binary input data
// size: size of input (bytes)
// return: base64-encoded string (null-terminated)
// memory for output will be allocated here, free it later
//
char* base64_encode(const void* buf, size_t size)
{
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char* str = (char*) malloc((size+3)*4/3 + 1);
char* p = str;
const unsigned char* q = (const unsigned char*) buf;
size_t i = 0;
if (str == NULL) {
return NULL;
}
while (i < size) {
int c = q[i++];
c *= 256;
if (i < size)
c += q[i];
i++;
c *= 256;
if (i < size)
c += q[i];
i++;
*p++ = base64[(c & 0x00fc0000) >> 18];
*p++ = base64[(c & 0x0003f000) >> 12];
if (i > size + 1)
*p++ = '=';
else
*p++ = base64[(c & 0x00000fc0) >> 6];
if (i > size)
*p++ = '=';
else
*p++ = base64[c & 0x0000003f];
}
*p = 0;
return str;
}
// single base64 character conversion
//
static int POS(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 -1;
return -2;
}
// base64 decoding
//
// s: base64 string, must be null-terminated
// data: output buffer for decoded data
// data_len size of decoded data
// return: allocated data buffer
//
void* base64_decode(const char* s, size_t *data_len)
{
const char *p;
unsigned char *q, *data;
int n[4] = { 0, 0, 0, 0 };
size_t len = strlen(s);
if (len % 4)
return NULL;
data = (unsigned char*) malloc(len/4*3);
q = (unsigned char*) data;
for (p = s; *p; ) {
n[0] = POS(*p++);
n[1] = POS(*p++);
n[2] = POS(*p++);
n[3] = POS(*p++);
if (n[0] == -2 || n[1] == -2 || n[2] == -2 || n[3] == -2)
return NULL;
if (n[0] == -1 || n[1] == -1)
return NULL;
if (n[2] == -1 && n[3] != -1)
return NULL;
q[0] = (n[0] << 2) + (n[1] >> 4);
if (n[2] != -1)
q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
if (n[3] != -1)
q[2] = ((n[2] & 3) << 6) + n[3];
q += 3;
}
*data_len = q-data - (n[2]==-1) - (n[3]==-1);
return data;
}