3 #include <linux/errno.h>
9 * base64 encode/decode.
12 const char *pem_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14 static int encode_bits(int c)
19 static int decode_bits(char c)
21 if (c >= 'A' && c <= 'Z')
23 if (c >= 'a' && c <= 'z')
25 if (c >= '0' && c <= '9')
27 if (c == '+' || c == '-')
29 if (c == '/' || c == '_')
32 return 0; /* just non-negative, please */
36 static int set_str_val(char **pdst, const char *end, char c)
48 int ceph_armor_line_break(char *dst, const char *dst_end, const char *src, const char *end, int line_width)
53 #define SET_DST(c) do { \
54 int __ret = set_str_val(&dst, dst_end, c); \
63 SET_DST(encode_bits(a >> 2));
67 SET_DST(encode_bits(((a & 3) << 4) | (b >> 4)));
71 SET_DST(encode_bits(((b & 15) << 2) |
73 SET_DST(encode_bits(c & 63));
75 SET_DST(encode_bits((b & 15) << 2));
79 SET_DST(encode_bits(((a & 3) << 4)));
85 if (line_width && line == line_width) {
94 int ceph_armor(char *dst, const char *dst_end, const char *src, const char *end)
96 return ceph_armor_line_break(dst, dst_end, src, end, 0);
99 int ceph_unarmor(char *dst, const char *dst_end, const char *src, const char *end)
106 if (src[0] == '\n') {
113 a = decode_bits(src[0]);
114 b = decode_bits(src[1]);
115 c = decode_bits(src[2]);
116 d = decode_bits(src[3]);
117 if (a < 0 || b < 0 || c < 0 || d < 0)
120 SET_DST((a << 2) | (b >> 4));
123 SET_DST(((b & 15) << 4) | (c >> 2));
126 SET_DST(((c & 3) << 6) | d);