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 /* The value is 80, so it fits into int8_t. */
24 static const int8_t decoding_size
= sizeof(decoding
)/sizeof(*decoding
);
26 if (value_in
< 0 || value_in
>= decoding_size
) return -1;
27 return decoding
[value_in
];
31 _hidden
void _isds_base64_init_decodestate(base64_decodestate
*state_in
) {
32 state_in
->step
= step_a
;
33 state_in
->plainchar
= 0;
37 _hidden
size_t _isds_base64_decode_block(const int8_t *code_in
,
38 const size_t length_in
, int8_t *plaintext_out
,
39 base64_decodestate
*state_in
) {
40 const int8_t *codechar
= code_in
;
41 int8_t *plainchar
= plaintext_out
;
44 *plainchar
= state_in
->plainchar
;
46 switch (state_in
->step
) {
50 if (codechar
== code_in
+ length_in
) {
51 state_in
->step
= step_a
;
52 state_in
->plainchar
= *plainchar
;
53 return plainchar
- plaintext_out
;
55 fragment
= base64_decode_value(*codechar
++);
56 } while (fragment
< 0);
57 *plainchar
= (fragment
& 0x03f) << 2;
60 if (codechar
== code_in
+length_in
) {
61 state_in
->step
= step_b
;
62 state_in
->plainchar
= *plainchar
;
63 return plainchar
- plaintext_out
;
65 fragment
= base64_decode_value(*codechar
++);
66 } while (fragment
< 0);
67 *plainchar
++ |= (fragment
& 0x030) >> 4;
68 *plainchar
= (fragment
& 0x00f) << 4;
71 if (codechar
== code_in
+length_in
) {
72 state_in
->step
= step_c
;
73 state_in
->plainchar
= *plainchar
;
74 return plainchar
- plaintext_out
;
76 fragment
= base64_decode_value(*codechar
++);
77 } while (fragment
< 0);
78 *plainchar
++ |= (fragment
& 0x03c) >> 2;
79 *plainchar
= (fragment
& 0x003) << 6;
82 if (codechar
== code_in
+length_in
) {
83 state_in
->step
= step_d
;
84 state_in
->plainchar
= *plainchar
;
85 return plainchar
- plaintext_out
;
87 fragment
= base64_decode_value(*codechar
++);
88 } while (fragment
< 0);
89 *plainchar
++ |= (fragment
& 0x03f);
93 /* control should not reach here */
94 return plainchar
- plaintext_out
;