]>
git.proxmox.com Git - mirror_frr.git/blob - lib/base64.c
2 * This is part of the libb64 project, and has been placed in the public domain.
3 * For details, see http://sourceforge.net/projects/libb64
8 static const int CHARS_PER_LINE
= 72;
9 static const char *ENCODING
=
10 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12 void base64_init_encodestate(struct base64_encodestate
*state_in
)
14 state_in
->step
= step_A
;
16 state_in
->stepcount
= 0;
19 char base64_encode_value(char value_in
)
23 return ENCODING
[(int)value_in
];
26 int base64_encode_block(const char *plaintext_in
, int length_in
, char *code_out
,
27 struct base64_encodestate
*state_in
)
29 const char *plainchar
= plaintext_in
;
30 const char *const plaintextend
= plaintext_in
+ length_in
;
31 char *codechar
= code_out
;
35 result
= state_in
->result
;
37 switch (state_in
->step
) {
40 if (plainchar
== plaintextend
) {
41 state_in
->result
= result
;
42 state_in
->step
= step_A
;
43 return codechar
- code_out
;
45 fragment
= *plainchar
++;
46 result
= (fragment
& 0x0fc) >> 2;
47 *codechar
++ = base64_encode_value(result
);
48 result
= (fragment
& 0x003) << 4;
51 if (plainchar
== plaintextend
) {
52 state_in
->result
= result
;
53 state_in
->step
= step_B
;
54 return codechar
- code_out
;
56 fragment
= *plainchar
++;
57 result
|= (fragment
& 0x0f0) >> 4;
58 *codechar
++ = base64_encode_value(result
);
59 result
= (fragment
& 0x00f) << 2;
62 if (plainchar
== plaintextend
) {
63 state_in
->result
= result
;
64 state_in
->step
= step_C
;
65 return codechar
- code_out
;
67 fragment
= *plainchar
++;
68 result
|= (fragment
& 0x0c0) >> 6;
69 *codechar
++ = base64_encode_value(result
);
70 result
= (fragment
& 0x03f) >> 0;
71 *codechar
++ = base64_encode_value(result
);
73 ++(state_in
->stepcount
);
74 if (state_in
->stepcount
== CHARS_PER_LINE
/4) {
76 state_in
->stepcount
= 0;
80 /* control should not reach here */
81 return codechar
- code_out
;
84 int base64_encode_blockend(char *code_out
, struct base64_encodestate
*state_in
)
86 char *codechar
= code_out
;
88 switch (state_in
->step
) {
90 *codechar
++ = base64_encode_value(state_in
->result
);
95 *codechar
++ = base64_encode_value(state_in
->result
);
103 return codechar
- code_out
;
107 signed char base64_decode_value(signed char value_in
)
109 static const signed char decoding
[] = {
110 62, -1, -1, -1, 63, 52, 53, 54,
111 55, 56, 57, 58, 59, 60, 61, -1,
112 -1, -1, -2, -1, -1, -1, 0, 1,
113 2, 3, 4, 5, 6, 7, 8, 9,
114 10, 11, 12, 13, 14, 15, 16, 17,
115 18, 19, 20, 21, 22, 23, 24, 25,
116 -1, -1, -1, -1, -1, -1, 26, 27,
117 28, 29, 30, 31, 32, 33, 34, 35,
118 36, 37, 38, 39, 40, 41, 42, 43,
119 44, 45, 46, 47, 48, 49, 50, 51
122 if (value_in
< 0 || value_in
>= 80)
124 return decoding
[(int)value_in
];
127 void base64_init_decodestate(struct base64_decodestate
*state_in
)
129 state_in
->step
= step_a
;
130 state_in
->plainchar
= 0;
133 int base64_decode_block(const char *code_in
, int length_in
, char *plaintext_out
,
134 struct base64_decodestate
*state_in
)
136 const char *codec
= code_in
;
137 char *plainc
= plaintext_out
;
140 *plainc
= state_in
->plainchar
;
142 switch (state_in
->step
) {
146 if (codec
== code_in
+length_in
) {
147 state_in
->step
= step_a
;
148 state_in
->plainchar
= *plainc
;
149 return plainc
- plaintext_out
;
151 fragmt
= base64_decode_value(*codec
++);
152 } while (fragmt
< 0);
153 *plainc
= (fragmt
& 0x03f) << 2;
157 if (codec
== code_in
+length_in
) {
158 state_in
->step
= step_b
;
159 state_in
->plainchar
= *plainc
;
160 return plainc
- plaintext_out
;
162 fragmt
= base64_decode_value(*codec
++);
163 } while (fragmt
< 0);
164 *plainc
++ |= (fragmt
& 0x030) >> 4;
165 *plainc
= (fragmt
& 0x00f) << 4;
169 if (codec
== code_in
+length_in
) {
170 state_in
->step
= step_c
;
171 state_in
->plainchar
= *plainc
;
172 return plainc
- plaintext_out
;
174 fragmt
= base64_decode_value(*codec
++);
175 } while (fragmt
< 0);
176 *plainc
++ |= (fragmt
& 0x03c) >> 2;
177 *plainc
= (fragmt
& 0x003) << 6;
181 if (codec
== code_in
+length_in
) {
182 state_in
->step
= step_d
;
183 state_in
->plainchar
= *plainc
;
184 return plainc
- plaintext_out
;
186 fragmt
= base64_decode_value(*codec
++);
187 } while (fragmt
< 0);
188 *plainc
++ |= (fragmt
& 0x03f);
191 /* control should not reach here */
192 return plainc
- plaintext_out
;