From 9a3b5b4a3a1e8bd2da73ebc493e6fcbab95079f2 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 6 Jun 2009 19:18:06 +0000 Subject: Tweaking Base85 with a different charset, and a lookup table. --- lib/Base85.cc | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/Base85.cc b/lib/Base85.cc index c228022..d1f4baa 100644 --- a/lib/Base85.cc +++ b/lib/Base85.cc @@ -19,6 +19,28 @@ #include +// Banned chars: ",/:.[\] +static char cb85[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&'()*+-;<=>?@^_`{|}"; +static char lookup[] = { +// x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 62, 0, 63, 64, 65, 66, 67, 68, 69, 70, 71, 0, 72, 0, 0, // 2x + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 73, 74, 75, 76, 77, // 3x + 78, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4x + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 79, 80, // 5x + 81, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6x + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 82, 83, 84, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + String Base85::encode_block(unsigned char in_tab[4], int len) { unsigned int tuple; char r[6]; @@ -28,15 +50,15 @@ String Base85::encode_block(unsigned char in_tab[4], int len) { tuple |= in_tab[2] << 16; tuple |= in_tab[3] << 24; - r[0] = (tuple % 85) + '!'; + r[0] = cb85[tuple % 85]; tuple /= 85; - r[1] = (tuple % 85) + '!'; + r[1] = cb85[tuple % 85]; tuple /= 85; - r[2] = len > 1 ? (tuple % 85) + '!' : '~'; + r[2] = len > 1 ? cb85[tuple % 85] : '~'; tuple /= 85; - r[3] = len > 2 ? (tuple % 85) + '!' : '~'; + r[3] = len > 2 ? cb85[tuple % 85] : '~'; tuple /= 85; - r[4] = len > 3 ? (tuple % 85) + '!' : '~'; + r[4] = len > 3 ? cb85[tuple % 85] : '~'; r[5] = 0; return r; @@ -79,11 +101,11 @@ int Base85::decode_block(char s1, char s2, char s3, char s4, char s5, unsigned c s4 = (s4 == '~') || (s4 == 0) ? '!' : s4; s5 = (s5 == '~') || (s5 == 0) ? '!' : s5; - sb1 = s1 - '!'; - sb2 = s2 - '!'; - sb3 = s3 - '!'; - sb4 = s4 - '!'; - sb5 = s5 - '!'; + sb1 = lookup[s1]; + sb2 = lookup[s2]; + sb3 = lookup[s3]; + sb4 = lookup[s4]; + sb5 = lookup[s5]; tuple = sb5; tuple *= 85; tuple += sb4; tuple *= 85; -- cgit v1.2.3