2 cdecoder.c - c source to a base64 decoding algorithm implementation
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
11 static int8_t base64_decode_value(int8_t value_in
) {
12 static const int8_t decoding
[] = {
13 62, -1, -1, -1, 63, 52, 53, 54,
14 55, 56, 57, 58, 59, 60, 61, -1,
15 -1, -1, -2, -1, -1, -1, 0, 1,
16 2, 3, 4, 5, 6, 7, 8, 9,
17 10, 11, 12, 13, 14, 15, 16, 17,
18 18, 19, 20, 21, 22, 23, 24, 25,
19 -1, -1, -1, -1, -1, -1, 26, 27,
20 28, 29, 30, 31, 32, 33, 34, 35,
21 36, 37, 38, 39, 40, 41, 42, 43,
22 44, 45, 46, 47, 48, 49, 50, 51 };
23 static const size_t decoding_size
= sizeof(decoding
)/sizeof(*decoding
);
25 if (value_in
< 0 || value_in
>= decoding_size
) return -1;
26 return decoding
[value_in
];
30 _hidden
void _isds_base64_init_decodestate(base64_decodestate
*state_in
) {
31 state_in
->step
= step_a
;
32 state_in
->plainchar
= 0;
36 _hidden
size_t _isds_base64_decode_block(const int8_t *code_in
,
37 const size_t length_in
, int8_t *plaintext_out
,
38 base64_decodestate
*state_in
) {
39 const int8_t *codechar
= code_in
;
40 int8_t *plainchar
= plaintext_out
;
43 *plainchar
= state_in
->plainchar
;
45 switch (state_in
->step
) {
49 if (codechar
== code_in
+ length_in
) {
50 state_in
->step
= step_a
;
51 state_in
->plainchar
= *plainchar
;
52 return plainchar
- plaintext_out
;
54 fragment
= base64_decode_value(*codechar
++);
55 } while (fragment
< 0);
56 *plainchar
= (fragment
& 0x03f) << 2;
59 if (codechar
== code_in
+length_in
) {
60 state_in
->step
= step_b
;
61 state_in
->plainchar
= *plainchar
;
62 return plainchar
- plaintext_out
;
64 fragment
= base64_decode_value(*codechar
++);
65 } while (fragment
< 0);
66 *plainchar
++ |= (fragment
& 0x030) >> 4;
67 *plainchar
= (fragment
& 0x00f) << 4;
70 if (codechar
== code_in
+length_in
) {
71 state_in
->step
= step_c
;
72 state_in
->plainchar
= *plainchar
;
73 return plainchar
- plaintext_out
;
75 fragment
= base64_decode_value(*codechar
++);
76 } while (fragment
< 0);
77 *plainchar
++ |= (fragment
& 0x03c) >> 2;
78 *plainchar
= (fragment
& 0x003) << 6;
81 if (codechar
== code_in
+length_in
) {
82 state_in
->step
= step_d
;
83 state_in
->plainchar
= *plainchar
;
84 return plainchar
- plaintext_out
;
86 fragment
= base64_decode_value(*codechar
++);
87 } while (fragment
< 0);
88 *plainchar
++ |= (fragment
& 0x03f);
92 /* control should not reach here */
93 return plainchar
- plaintext_out
;