diff options
Diffstat (limited to 'fs/ceph/armor.c')
| -rw-r--r-- | fs/ceph/armor.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/fs/ceph/armor.c b/fs/ceph/armor.c new file mode 100644 index 000000000000..67b2c030924b --- /dev/null +++ b/fs/ceph/armor.c | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | |||
| 2 | #include <linux/errno.h> | ||
| 3 | |||
| 4 | /* | ||
| 5 | * base64 encode/decode. | ||
| 6 | */ | ||
| 7 | |||
| 8 | const char *pem_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
| 9 | |||
| 10 | static int encode_bits(int c) | ||
| 11 | { | ||
| 12 | return pem_key[c]; | ||
| 13 | } | ||
| 14 | |||
| 15 | static int decode_bits(char c) | ||
| 16 | { | ||
| 17 | if (c >= 'A' && c <= 'Z') | ||
| 18 | return c - 'A'; | ||
| 19 | if (c >= 'a' && c <= 'z') | ||
| 20 | return c - 'a' + 26; | ||
| 21 | if (c >= '0' && c <= '9') | ||
| 22 | return c - '0' + 52; | ||
| 23 | if (c == '+') | ||
| 24 | return 62; | ||
| 25 | if (c == '/') | ||
| 26 | return 63; | ||
| 27 | if (c == '=') | ||
| 28 | return 0; /* just non-negative, please */ | ||
| 29 | return -EINVAL; | ||
| 30 | } | ||
| 31 | |||
| 32 | int ceph_armor(char *dst, const char *src, const char *end) | ||
| 33 | { | ||
| 34 | int olen = 0; | ||
| 35 | int line = 0; | ||
| 36 | |||
| 37 | while (src < end) { | ||
| 38 | unsigned char a, b, c; | ||
| 39 | |||
| 40 | a = *src++; | ||
| 41 | *dst++ = encode_bits(a >> 2); | ||
| 42 | if (src < end) { | ||
| 43 | b = *src++; | ||
| 44 | *dst++ = encode_bits(((a & 3) << 4) | (b >> 4)); | ||
| 45 | if (src < end) { | ||
| 46 | c = *src++; | ||
| 47 | *dst++ = encode_bits(((b & 15) << 2) | | ||
| 48 | (c >> 6)); | ||
| 49 | *dst++ = encode_bits(c & 63); | ||
| 50 | } else { | ||
| 51 | *dst++ = encode_bits((b & 15) << 2); | ||
| 52 | *dst++ = '='; | ||
| 53 | } | ||
| 54 | } else { | ||
| 55 | *dst++ = encode_bits(((a & 3) << 4)); | ||
| 56 | *dst++ = '='; | ||
| 57 | *dst++ = '='; | ||
| 58 | } | ||
| 59 | olen += 4; | ||
| 60 | line += 4; | ||
| 61 | if (line == 64) { | ||
| 62 | line = 0; | ||
| 63 | *(dst++) = '\n'; | ||
| 64 | olen++; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return olen; | ||
| 68 | } | ||
| 69 | |||
| 70 | int ceph_unarmor(char *dst, const char *src, const char *end) | ||
| 71 | { | ||
| 72 | int olen = 0; | ||
| 73 | |||
| 74 | while (src < end) { | ||
| 75 | int a, b, c, d; | ||
| 76 | |||
| 77 | if (src < end && src[0] == '\n') | ||
| 78 | src++; | ||
| 79 | if (src + 4 > end) | ||
| 80 | return -EINVAL; | ||
| 81 | a = decode_bits(src[0]); | ||
| 82 | b = decode_bits(src[1]); | ||
| 83 | c = decode_bits(src[2]); | ||
| 84 | d = decode_bits(src[3]); | ||
| 85 | if (a < 0 || b < 0 || c < 0 || d < 0) | ||
| 86 | return -EINVAL; | ||
| 87 | |||
| 88 | *dst++ = (a << 2) | (b >> 4); | ||
| 89 | if (src[2] == '=') | ||
| 90 | return olen + 1; | ||
| 91 | *dst++ = ((b & 15) << 4) | (c >> 2); | ||
| 92 | if (src[3] == '=') | ||
| 93 | return olen + 2; | ||
| 94 | *dst++ = ((c & 3) << 6) | d; | ||
| 95 | olen += 3; | ||
| 96 | src += 4; | ||
| 97 | } | ||
| 98 | return olen; | ||
| 99 | } | ||
