1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/errno.h>
5 int ceph_armor(char *dst
, const char *src
, const char *end
);
6 int ceph_unarmor(char *dst
, const char *src
, const char *end
);
9 * base64 encode/decode.
12 static const char *pem_key
=
13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
15 static int encode_bits(int c
)
20 static int decode_bits(char c
)
22 if (c
>= 'A' && c
<= 'Z')
24 if (c
>= 'a' && c
<= 'z')
26 if (c
>= '0' && c
<= '9')
33 return 0; /* just non-negative, please */
37 int ceph_armor(char *dst
, const char *src
, const char *end
)
43 unsigned char a
, b
, c
;
46 *dst
++ = encode_bits(a
>> 2);
49 *dst
++ = encode_bits(((a
& 3) << 4) | (b
>> 4));
52 *dst
++ = encode_bits(((b
& 15) << 2) |
54 *dst
++ = encode_bits(c
& 63);
56 *dst
++ = encode_bits((b
& 15) << 2);
60 *dst
++ = encode_bits(((a
& 3) << 4));
75 int ceph_unarmor(char *dst
, const char *src
, const char *end
)
88 a
= decode_bits(src
[0]);
89 b
= decode_bits(src
[1]);
90 c
= decode_bits(src
[2]);
91 d
= decode_bits(src
[3]);
92 if (a
< 0 || b
< 0 || c
< 0 || d
< 0)
95 *dst
++ = (a
<< 2) | (b
>> 4);
98 *dst
++ = ((b
& 15) << 4) | (c
>> 2);
101 *dst
++ = ((c
& 3) << 6) | d
;