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 _hidden
int base64_decode_value(char value_in
)
13 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};
14 static const char decoding_size
= sizeof(decoding
);
16 if (value_in
< 0 || value_in
> decoding_size
) return -1;
17 return decoding
[(int)value_in
];
20 void base64_init_decodestate(base64_decodestate
* state_in
)
22 state_in
->step
= step_a
;
23 state_in
->plainchar
= 0;
26 _hidden
int base64_decode_block(const char* code_in
, const int length_in
,
27 char* plaintext_out
, base64_decodestate
* state_in
)
29 const char* codechar
= code_in
;
30 char* plainchar
= plaintext_out
;
33 *plainchar
= state_in
->plainchar
;
35 switch (state_in
->step
)
41 if (codechar
== code_in
+length_in
)
43 state_in
->step
= step_a
;
44 state_in
->plainchar
= *plainchar
;
45 return plainchar
- plaintext_out
;
47 fragment
= (char)base64_decode_value(*codechar
++);
48 } while (fragment
< 0);
49 *plainchar
= (fragment
& 0x03f) << 2;
52 if (codechar
== code_in
+length_in
)
54 state_in
->step
= step_b
;
55 state_in
->plainchar
= *plainchar
;
56 return plainchar
- plaintext_out
;
58 fragment
= (char)base64_decode_value(*codechar
++);
59 } while (fragment
< 0);
60 *plainchar
++ |= (fragment
& 0x030) >> 4;
61 *plainchar
= (fragment
& 0x00f) << 4;
64 if (codechar
== code_in
+length_in
)
66 state_in
->step
= step_c
;
67 state_in
->plainchar
= *plainchar
;
68 return plainchar
- plaintext_out
;
70 fragment
= (char)base64_decode_value(*codechar
++);
71 } while (fragment
< 0);
72 *plainchar
++ |= (fragment
& 0x03c) >> 2;
73 *plainchar
= (fragment
& 0x003) << 6;
76 if (codechar
== code_in
+length_in
)
78 state_in
->step
= step_d
;
79 state_in
->plainchar
= *plainchar
;
80 return plainchar
- plaintext_out
;
82 fragment
= (char)base64_decode_value(*codechar
++);
83 } while (fragment
< 0);
84 *plainchar
++ |= (fragment
& 0x03f);
87 /* control should not reach here */
88 return plainchar
- plaintext_out
;