-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-base64.c
140 lines (123 loc) · 3.61 KB
/
fast-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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
typedef unsigned char BYTE;
#include "lookup_tables.h"
/*
Buffer size is measured in chunks. Each chunk is either
3 bytes of raw data, or 4 bytes of base64 characters.
*/
#define BUF_CHUNKS 10000
const static size_t DATA_BUF_SIZE = BUF_CHUNKS * 3;
const static size_t B64_BUF_SIZE = BUF_CHUNKS * 4;
void encode_base64(
const BYTE* data,
const size_t len,
char* out,
size_t* nout)
{
BYTE* dp = (BYTE*) data;
BYTE* dll = dp + len;
BYTE* dl = dll - 3;
char* op = out;
/* main encoding loop */
while (dp < dl) {
*op++ = C0_ENCODE_TABLE[*dp];
*op++ = C1_ENCODE_TABLE[*((unsigned short*) dp++ )];
*op++ = C2_ENCODE_TABLE[*((unsigned short*) dp++ )];
*op++ = C3_ENCODE_TABLE[*dp++];
}
*op++ = C0_ENCODE_TABLE[*dp];
/* last block */
switch (dll - dp) {
case 3:
/* last block contains 3 bytes */
*op++ = C1_ENCODE_TABLE[*((unsigned short*) dp++ )];
*op++ = C2_ENCODE_TABLE[*((unsigned short*) dp++ )];
*op = C3_ENCODE_TABLE[*dp];
break;
case 2:
/* last block contains 2 bytes */
*op++ = C1_ENCODE_TABLE[*((unsigned short*) dp++ )];
*op++ = C2_ENCODE_TABLE[(unsigned short)*dp];
*op = '=';
break;
case 1:
/* last block contains 1 byte */
*op++ = C1_ENCODE_TABLE[(unsigned short)*dp];
*op++ = '=';
*op = '=';
break;
}
*nout = (op - out) + 1;
}
void decode_base64(
const char* b64,
const size_t len,
BYTE* out,
size_t* nout)
{
char* sp = (char*) b64;
char* sll = sp + len;
char* sl = sll - 4;
BYTE* op = out;
/* main decoding loop */
while (sp < sl) {
*op++ = B0_DECODE_TABLE[*((unsigned short*) sp++ )];
*op++ = B1_DECODE_TABLE[*((unsigned short*) sp++ )];
*op++ = B2_DECODE_TABLE[*((unsigned short*) sp++ )];
sp++;
}
/* last block */
if (*(sll - 2) == '=') {
/* last block contains 1 byte */
*op = B0_DECODE_TABLE[*((unsigned short*) sp )];
} else if (*(sll - 1) == '=') {
/* last block contains 2 bytes */
*op++ = B0_DECODE_TABLE[*((unsigned short*) sp++ )];
*op = B1_DECODE_TABLE[*((unsigned short*) sp )];
} else {
/* last block contains 3 bytes */
*op++ = B0_DECODE_TABLE[*((unsigned short*) sp++ )];
*op++ = B1_DECODE_TABLE[*((unsigned short*) sp++ )];
*op = B2_DECODE_TABLE[*((unsigned short*) sp )];
}
*nout = (op - out) + 1;
}
int main(
int argc,
char** argv)
{
BYTE data_buf[DATA_BUF_SIZE];
char b64_buf[B64_BUF_SIZE];
size_t nread, nout;
int encode = 1;
int opt;
while ((opt = getopt(argc, argv, "d")) != -1)
switch (opt) {
case 'd':
encode = 0;
break;
default:
exit(-1);
}
if (encode) {
while ((nread = read(0, data_buf, DATA_BUF_SIZE)) > 0) {
encode_base64(data_buf, nread, b64_buf, &nout);
if (!write(1, b64_buf, nout)) {
fprintf(stderr, "error writing base64 data to stdout");
}
}
puts("");
} else {
while ((nread = read(0, b64_buf, B64_BUF_SIZE)) > 0) {
decode_base64(b64_buf, nread, data_buf, &nout);
if (!write(1, data_buf, nout)) {
fprintf(stderr, "error writing binary data to stdout");
}
}
}
return 0;
}