0
|
1 |
/*
|
|
2 |
cdecoder.c - c source to a base64 decoding algorithm implementation
|
|
3 |
|
|
4 |
This is part of the libb64 project, and has been placed in the public domain.
|
|
5 |
For details, see http://sourceforge.net/projects/libb64
|
|
6 |
*/
|
|
7 |
|
|
8 |
#include "base64.h"
|
|
9 |
|
|
10 |
int base64_decode_value(char value_in)
|
|
11 |
{
|
|
12 |
static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,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,-1,-1,-1,-1,-1,-1,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};
|
|
13 |
static const char decoding_size = sizeof(decoding);
|
|
14 |
value_in -= 43;
|
|
15 |
if (value_in < 0 || value_in > decoding_size) return -1;
|
|
16 |
return decoding[(int)value_in];
|
|
17 |
}
|
|
18 |
|
|
19 |
void base64_init_decodestate(base64_decodestate* state_in)
|
|
20 |
{
|
|
21 |
state_in->step = step_a;
|
|
22 |
state_in->plainchar = 0;
|
|
23 |
}
|
|
24 |
|
|
25 |
int base64_decode_block(const char* code_in, const int length_in, uint8_t* plaintext_out, base64_decodestate* state_in)
|
|
26 |
{
|
|
27 |
const char* codechar = code_in;
|
|
28 |
uint8_t* plainchar = plaintext_out;
|
|
29 |
char fragment;
|
|
30 |
|
|
31 |
*plainchar = state_in->plainchar;
|
|
32 |
|
|
33 |
switch (state_in->step)
|
|
34 |
{
|
|
35 |
while (1)
|
|
36 |
{
|
|
37 |
case step_a:
|
|
38 |
do {
|
|
39 |
if (codechar == code_in+length_in)
|
|
40 |
{
|
|
41 |
state_in->step = step_a;
|
|
42 |
state_in->plainchar = *plainchar;
|
|
43 |
return plainchar - plaintext_out;
|
|
44 |
}
|
|
45 |
fragment = (char)base64_decode_value(*codechar++);
|
|
46 |
} while (fragment < 0);
|
|
47 |
*plainchar = (fragment & 0x03f) << 2;
|
|
48 |
case step_b:
|
|
49 |
do {
|
|
50 |
if (codechar == code_in+length_in)
|
|
51 |
{
|
|
52 |
state_in->step = step_b;
|
|
53 |
state_in->plainchar = *plainchar;
|
|
54 |
return plainchar - plaintext_out;
|
|
55 |
}
|
|
56 |
fragment = (char)base64_decode_value(*codechar++);
|
|
57 |
} while (fragment < 0);
|
|
58 |
*plainchar++ |= (fragment & 0x030) >> 4;
|
|
59 |
*plainchar = (fragment & 0x00f) << 4;
|
|
60 |
case step_c:
|
|
61 |
do {
|
|
62 |
if (codechar == code_in+length_in)
|
|
63 |
{
|
|
64 |
state_in->step = step_c;
|
|
65 |
state_in->plainchar = *plainchar;
|
|
66 |
return plainchar - plaintext_out;
|
|
67 |
}
|
|
68 |
fragment = (char)base64_decode_value(*codechar++);
|
|
69 |
} while (fragment < 0);
|
|
70 |
*plainchar++ |= (fragment & 0x03c) >> 2;
|
|
71 |
*plainchar = (fragment & 0x003) << 6;
|
|
72 |
case step_d:
|
|
73 |
do {
|
|
74 |
if (codechar == code_in+length_in)
|
|
75 |
{
|
|
76 |
state_in->step = step_d;
|
|
77 |
state_in->plainchar = *plainchar;
|
|
78 |
return plainchar - plaintext_out;
|
|
79 |
}
|
|
80 |
fragment = (char)base64_decode_value(*codechar++);
|
|
81 |
} while (fragment < 0);
|
|
82 |
*plainchar++ |= (fragment & 0x03f);
|
|
83 |
}
|
|
84 |
}
|
|
85 |
/* control should not reach here */
|
|
86 |
return plainchar - plaintext_out;
|
|
87 |
}
|
|
88 |
|
|
89 |
/*
|
|
90 |
cencoder.c - c source to a base64 encoding algorithm implementation
|
|
91 |
|
|
92 |
This is part of the libb64 project, and has been placed in the public domain.
|
|
93 |
For details, see http://sourceforge.net/projects/libb64
|
|
94 |
*/
|
|
95 |
|
|
96 |
const int CHARS_PER_LINE = 72;
|
|
97 |
|
|
98 |
void base64_init_encodestate(base64_encodestate* state_in)
|
|
99 |
{
|
|
100 |
state_in->step = step_A;
|
|
101 |
state_in->result = 0;
|
|
102 |
state_in->stepcount = 0;
|
|
103 |
}
|
|
104 |
|
|
105 |
char base64_encode_value(char value_in)
|
|
106 |
{
|
|
107 |
static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
108 |
if (value_in > 63) return '=';
|
|
109 |
return encoding[(int)value_in];
|
|
110 |
}
|
|
111 |
|
|
112 |
int base64_encode_block(const uint8_t* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
|
|
113 |
{
|
|
114 |
const uint8_t* plainchar = plaintext_in;
|
|
115 |
const uint8_t* const plaintextend = plaintext_in + length_in;
|
|
116 |
char* codechar = code_out;
|
|
117 |
char result;
|
|
118 |
uint8_t fragment;
|
|
119 |
|
|
120 |
result = state_in->result;
|
|
121 |
|
|
122 |
switch (state_in->step)
|
|
123 |
{
|
|
124 |
while (1)
|
|
125 |
{
|
|
126 |
case step_A:
|
|
127 |
if (plainchar == plaintextend)
|
|
128 |
{
|
|
129 |
state_in->result = result;
|
|
130 |
state_in->step = step_A;
|
|
131 |
return codechar - code_out;
|
|
132 |
}
|
|
133 |
fragment = *plainchar++;
|
|
134 |
result = (fragment & 0x0fc) >> 2;
|
|
135 |
*codechar++ = base64_encode_value(result);
|
|
136 |
result = (fragment & 0x003) << 4;
|
|
137 |
case step_B:
|
|
138 |
if (plainchar == plaintextend)
|
|
139 |
{
|
|
140 |
state_in->result = result;
|
|
141 |
state_in->step = step_B;
|
|
142 |
return codechar - code_out;
|
|
143 |
}
|
|
144 |
fragment = *plainchar++;
|
|
145 |
result |= (fragment & 0x0f0) >> 4;
|
|
146 |
*codechar++ = base64_encode_value(result);
|
|
147 |
result = (fragment & 0x00f) << 2;
|
|
148 |
case step_C:
|
|
149 |
if (plainchar == plaintextend)
|
|
150 |
{
|
|
151 |
state_in->result = result;
|
|
152 |
state_in->step = step_C;
|
|
153 |
return codechar - code_out;
|
|
154 |
}
|
|
155 |
fragment = *plainchar++;
|
|
156 |
result |= (fragment & 0x0c0) >> 6;
|
|
157 |
*codechar++ = base64_encode_value(result);
|
|
158 |
result = (fragment & 0x03f) >> 0;
|
|
159 |
*codechar++ = base64_encode_value(result);
|
|
160 |
|
|
161 |
++(state_in->stepcount);
|
|
162 |
if (state_in->stepcount == CHARS_PER_LINE/4)
|
|
163 |
{
|
|
164 |
*codechar++ = '\n';
|
|
165 |
state_in->stepcount = 0;
|
|
166 |
}
|
|
167 |
}
|
|
168 |
}
|
|
169 |
/* control should not reach here */
|
|
170 |
return codechar - code_out;
|
|
171 |
}
|
|
172 |
|
|
173 |
int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
|
|
174 |
{
|
|
175 |
char* codechar = code_out;
|
|
176 |
|
|
177 |
switch (state_in->step)
|
|
178 |
{
|
|
179 |
case step_B:
|
|
180 |
*codechar++ = base64_encode_value(state_in->result);
|
|
181 |
*codechar++ = '=';
|
|
182 |
*codechar++ = '=';
|
|
183 |
break;
|
|
184 |
case step_C:
|
|
185 |
*codechar++ = base64_encode_value(state_in->result);
|
|
186 |
*codechar++ = '=';
|
|
187 |
break;
|
|
188 |
case step_A:
|
|
189 |
break;
|
|
190 |
}
|
|
191 |
*codechar++ = '\n';
|
|
192 |
|
|
193 |
return codechar - code_out;
|
|
194 |
}
|
|
195 |
|
|
196 |
// end of base64.c ...
|
|
197 |
|