3 * @author devolve <http://sourceforge.net/projects/libb64>
10 * @section DESCRIPTION
12 * This is part of the libb64 project, and has been placed in the public domain.
13 * For details, see http://sourceforge.net/projects/libb64
15 * The cencode source provides support for base64 encoding data. It can be
16 * used in conjunction with the cdecode source. It's used by base64, which
17 * provides a higher-level interface to the functionality.
21 #include "pico/debug.h"
23 #include "pico/cencode.h"
25 const int CHARS_PER_LINE
= 72;
28 * Internal function for encoding base64 strings
30 void base64_init_encodestate(base64_encodestate
* state_in
)
32 state_in
->step
= step_A
;
34 state_in
->stepcount
= 0;
38 * Internal function for encoding base64 strings
40 char base64_encode_value(char value_in
)
42 static const char* encoding
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 if (value_in
> 63) return '=';
44 return encoding
[(int)value_in
];
48 * Internal function for encoding base64 strings
50 int base64_encode_block(const char* plaintext_in
, int length_in
, char* code_out
, base64_encodestate
* state_in
)
52 const char* plainchar
= plaintext_in
;
53 const char* const plaintextend
= plaintext_in
+ length_in
;
54 char* codechar
= code_out
;
58 result
= state_in
->result
;
60 switch (state_in
->step
)
65 if (plainchar
== plaintextend
)
67 state_in
->result
= result
;
68 state_in
->step
= step_A
;
69 return codechar
- code_out
;
71 fragment
= *plainchar
++;
72 result
= (fragment
& 0x0fc) >> 2;
73 *codechar
++ = base64_encode_value(result
);
74 result
= (fragment
& 0x003) << 4;
76 if (plainchar
== plaintextend
)
78 state_in
->result
= result
;
79 state_in
->step
= step_B
;
80 return codechar
- code_out
;
82 fragment
= *plainchar
++;
83 result
|= (fragment
& 0x0f0) >> 4;
84 *codechar
++ = base64_encode_value(result
);
85 result
= (fragment
& 0x00f) << 2;
87 if (plainchar
== plaintextend
)
89 state_in
->result
= result
;
90 state_in
->step
= step_C
;
91 return codechar
- code_out
;
93 fragment
= *plainchar
++;
94 result
|= (fragment
& 0x0c0) >> 6;
95 *codechar
++ = base64_encode_value(result
);
96 result
= (fragment
& 0x03f) >> 0;
97 *codechar
++ = base64_encode_value(result
);
99 ++(state_in
->stepcount
);
100 if (state_in
->stepcount
== CHARS_PER_LINE
/4)
102 //*codechar++ = '\n';
103 state_in
->stepcount
= 0;
107 /* control should not reach here */
108 return codechar
- code_out
;
112 * Internal function for encoding base64 strings
114 int base64_encode_blockend(char* code_out
, base64_encodestate
* state_in
)
116 char* codechar
= code_out
;
118 switch (state_in
->step
)
121 *codechar
++ = base64_encode_value(state_in
->result
);
126 *codechar
++ = base64_encode_value(state_in
->result
);
132 //*codechar++ = '\n';
134 return codechar
- code_out
;