summaryrefslogtreecommitdiff
path: root/appl/lib/encoding
diff options
context:
space:
mode:
Diffstat (limited to 'appl/lib/encoding')
-rw-r--r--appl/lib/encoding/base16.b43
-rw-r--r--appl/lib/encoding/base32.b60
-rw-r--r--appl/lib/encoding/base32a.b57
-rw-r--r--appl/lib/encoding/base64.b92
-rw-r--r--appl/lib/encoding/mkfile16
5 files changed, 268 insertions, 0 deletions
diff --git a/appl/lib/encoding/base16.b b/appl/lib/encoding/base16.b
new file mode 100644
index 00000000..6c91c200
--- /dev/null
+++ b/appl/lib/encoding/base16.b
@@ -0,0 +1,43 @@
+implement Encoding;
+
+include "encoding.m";
+
+hex: con "0123456789ABCDEF";
+
+enc(a: array of byte): string
+{
+ o: string;
+ for(i := 0; i < len a; i++){
+ n := int a[i];
+ o[len o] = hex[n>>4];
+ o[len o] = hex[n & 16rF];
+ }
+ return o;
+}
+
+dec(s: string): array of byte
+{
+ a := array[(len s+1)/2] of byte; # upper bound
+ o := 0;
+ j := 0;
+ n := 0;
+ for(i := 0; i < len s; i++){
+ c := s[i];
+ n <<= 4;
+ case c {
+ '0' to '9' =>
+ n |= c-'0';
+ 'A' to 'F' =>
+ n |= c-'A'+10;
+ 'a' to 'f' =>
+ n |= c-'a'+10;
+ * =>
+ continue;
+ }
+ if(++j == 2){
+ a[o++] = byte n;
+ j = n = 0;
+ }
+ }
+ return a[0:o];
+}
diff --git a/appl/lib/encoding/base32.b b/appl/lib/encoding/base32.b
new file mode 100644
index 00000000..d745bcc0
--- /dev/null
+++ b/appl/lib/encoding/base32.b
@@ -0,0 +1,60 @@
+implement Encoding;
+
+include "encoding.m";
+
+b32: con "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+
+enc(a: array of byte): string
+{
+ if(len a == 0)
+ return "========";
+ out := "";
+ nbit := len a * 8;
+ for(bit := 0; bit < nbit; bit += 5){
+ b := bit >> 3;
+ r := bit & 7;
+ v := int a[b] << r;
+ if(r > 3){
+ if(b+1 < len a)
+ v |= int (a[b+1] >> (8-r));
+ }
+ out[len out] = b32[(v>>3) & 16r1F];
+ }
+ while(len out & 7)
+ out[len out] = '='; # RFC3548 says pad: we pad.
+ return out;
+}
+
+Naughty: con 255;
+
+t32d := array[256] of {
+ 'a' => byte 0, 'b' => byte 1, 'c' => byte 2, 'd' => byte 3, 'e' => byte 4, 'f' => byte 5, 'g' => byte 6, 'h' => byte 7,
+ 'i' => byte 8, 'j' => byte 9, 'k' => byte 10, 'l' => byte 11, 'm' => byte 12, 'n' => byte 13, 'o' => byte 14, 'p' => byte 15,
+ 'q' => byte 16, 'r' => byte 17, 's' => byte 18, 't' => byte 19, 'u' => byte 20, 'v' => byte 21, 'w' => byte 22, 'x' => byte 23,
+ 'y' => byte 24, 'z' => byte 25,
+ 'A' => byte 0, 'B' => byte 1, 'C' => byte 2, 'D' => byte 3, 'E' => byte 4, 'F' => byte 5, 'G' => byte 6, 'H' => byte 7,
+ 'I' => byte 8, 'J' => byte 9, 'K' => byte 10, 'L' => byte 11, 'M' => byte 12, 'N' => byte 13, 'O' => byte 14, 'P' => byte 15,
+ 'Q' => byte 16, 'R' => byte 17, 'S' => byte 18, 'T' => byte 19, 'U' => byte 20, 'V' => byte 21, 'W' => byte 22, 'X' => byte 23,
+ 'Y' => byte 24, 'Z' => byte 25,
+ '2' => byte 26, '3' => byte 27, '4' => byte 28, '5' => byte 29, '6' => byte 30, '7' => byte 31,
+ * => byte Naughty
+};
+
+dec(s: string): array of byte
+{
+ a := array[(8*len s + 4)/5] of byte;
+ o := 0;
+ v := 0;
+ j := 0;
+ for(i := 0; i < len s; i++){
+ if((c := s[i]) > 16rFF || (c = int t32d[c]) == Naughty)
+ continue;
+ v <<= 5;
+ v |= c;
+ if((j += 5) >= 8){
+ a[o++] = byte (v>>(j-8));
+ j -= 8;
+ }
+ }
+ return a[0:o];
+}
diff --git a/appl/lib/encoding/base32a.b b/appl/lib/encoding/base32a.b
new file mode 100644
index 00000000..8d44a344
--- /dev/null
+++ b/appl/lib/encoding/base32a.b
@@ -0,0 +1,57 @@
+implement Encoding;
+
+include "encoding.m";
+
+b32: con "23456789abcdefghijkmnpqrstuvwxyz";
+
+enc(a: array of byte): string
+{
+ if(len a == 0)
+ return "========";
+ out := "";
+ nbit := len a * 8;
+ for(bit := 0; bit < nbit; bit += 5){
+ b := bit >> 3;
+ r := bit & 7;
+ v := int a[b] << r;
+ if(r > 3){
+ if(b+1 < len a)
+ v |= int (a[b+1] >> (8-r));
+ }
+ out[len out] = b32[(v>>3) & 16r1F];
+ }
+ # RFC3548 says pad with =; this follows alternative tradition (a)
+ return out;
+}
+
+INVAL: con 255;
+
+t32d := array[256] of {
+ '2' => byte 0, '3' => byte 1, '4' => byte 2, '5' => byte 3, '6' => byte 4, '7' => byte 5, '8' => byte 6, '9' => byte 7,
+ 'a' => byte 8, 'b' => byte 9, 'c' => byte 10, 'd' => byte 11, 'e' => byte 12, 'f' => byte 13, 'g' => byte 14, 'h' => byte 15,
+ 'i' => byte 16, 'j' => byte 17, 'k' => byte 18, 'm' => byte 19, 'n' => byte 20, 'p' => byte 21, 'q' => byte 22, 'r' => byte 23,
+ 's' => byte 24, 't' => byte 25, 'u' => byte 26, 'v' => byte 27, 'w' => byte 28, 'x' => byte 29, 'y' => byte 30, 'z' => byte 31,
+ 'A' => byte 8, 'B' => byte 9, 'C' => byte 10, 'D' => byte 11, 'E' => byte 12, 'F' => byte 13, 'G' => byte 14, 'H' => byte 15,
+ 'I' => byte 16, 'J' => byte 17, 'K' => byte 18, 'M' => byte 19, 'N' => byte 20, 'P' => byte 21, 'Q' => byte 22, 'R' => byte 23,
+ 'S' => byte 24, 'T' => byte 25, 'U' => byte 26, 'V' => byte 27, 'W' => byte 28, 'X' => byte 29, 'Y' => byte 30, 'Z' => byte 31,
+ * => byte INVAL
+};
+
+dec(s: string): array of byte
+{
+ a := array[(8*len s + 4)/5] of byte;
+ o := 0;
+ v := 0;
+ j := 0;
+ for(i := 0; i < len s; i++){
+ if((c := s[i]) > 16rFF || (c = int t32d[c]) == INVAL)
+ continue;
+ v <<= 5;
+ v |= c;
+ if((j += 5) >= 8){
+ a[o++] = byte (v>>(j-8));
+ j -= 8;
+ }
+ }
+ return a[0:o];
+}
diff --git a/appl/lib/encoding/base64.b b/appl/lib/encoding/base64.b
new file mode 100644
index 00000000..43a59f81
--- /dev/null
+++ b/appl/lib/encoding/base64.b
@@ -0,0 +1,92 @@
+implement Encoding;
+
+include "encoding.m";
+
+enc(a: array of byte) : string
+{
+ n := len a;
+ if(n == 0)
+ return "";
+ out := "";
+ j := 0;
+ i := 0;
+ while(i < n) {
+ x := int a[i++] << 16;
+ if(i < n)
+ x |= (int a[i++]&255) << 8;
+ if(i < n)
+ x |= (int a[i++]&255);
+ out[j++] = c64(x>>18);
+ out[j++] = c64(x>>12);
+ out[j++] = c64(x>> 6);
+ out[j++] = c64(x);
+ }
+ nmod3 := n % 3;
+ if(nmod3 != 0) {
+ out[j-1] = '=';
+ if(nmod3 == 1)
+ out[j-2] = '=';
+ }
+ return out;
+}
+
+c64(c: int) : int
+{
+ v: con "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ return v[c&63];
+}
+
+INVAL: con byte 255;
+
+t64d := array[256] of {
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL, byte 62,INVAL,INVAL,INVAL, byte 63,
+ byte 52, byte 53, byte 54, byte 55, byte 56, byte 57, byte 58, byte 59, byte 60, byte 61,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL, byte 0, byte 1, byte 2, byte 3, byte 4, byte 5, byte 6, byte 7, byte 8, byte 9, byte 10, byte 11, byte 12, byte 13, byte 14,
+ byte 15, byte 16, byte 17, byte 18, byte 19, byte 20, byte 21, byte 22, byte 23, byte 24, byte 25,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL, byte 26, byte 27, byte 28, byte 29, byte 30, byte 31, byte 32, byte 33, byte 34, byte 35, byte 36, byte 37, byte 38, byte 39, byte 40,
+ byte 41, byte 42, byte 43, byte 44, byte 45, byte 46, byte 47, byte 48, byte 49, byte 50, byte 51,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,
+ INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL,INVAL
+};
+
+dec(s: string): array of byte
+{
+ b24 := 0;
+ i := 0;
+ out := array[(3*len s+3)/4] of byte; # upper bound, especially if s contains white space
+ o := 0;
+ for(n := 0; n < len s; n++){
+ if((c := s[n]) > 16rFF || (c = int t64d[c]) == int INVAL)
+ continue;
+ case i++ {
+ 0 =>
+ b24 = c<<18;
+ 1 =>
+ b24 |= c<<12;
+ 2 =>
+ b24 |= c<<6;
+ 3 =>
+ b24 |= c;
+ out[o++] = byte (b24>>16);
+ out[o++] = byte (b24>>8);
+ out[o++] = byte b24;
+ i = 0;
+ }
+ }
+ case i {
+ 2 =>
+ out[o++] = byte (b24>>16);
+ 3 =>
+ out[o++] = byte (b24>>16);
+ out[o++] = byte (b24>>8);
+ }
+ return out[0:o];
+}
diff --git a/appl/lib/encoding/mkfile b/appl/lib/encoding/mkfile
new file mode 100644
index 00000000..1d8c60d3
--- /dev/null
+++ b/appl/lib/encoding/mkfile
@@ -0,0 +1,16 @@
+<../../../mkconfig
+
+TARG=\
+ base16.dis\
+ base32.dis\
+ base32a.dis\
+ base64.dis\
+
+MODULES=\
+
+SYSMODULES= \
+ encoding.m\
+
+DISBIN=$ROOT/dis/lib/encoding
+
+<$ROOT/mkfiles/mkdis