From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- appl/spree/lib/base64.b | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 appl/spree/lib/base64.b (limited to 'appl/spree/lib/base64.b') diff --git a/appl/spree/lib/base64.b b/appl/spree/lib/base64.b new file mode 100644 index 00000000..c8381467 --- /dev/null +++ b/appl/spree/lib/base64.b @@ -0,0 +1,72 @@ +implement Base64; +include "base64.m"; + +PADCH: con '='; +encode(b: array of byte): string +{ + chmap := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz0123456789+/"; + r := ""; + blen := len b; + full := (blen + 2)/ 3; + rplen := (4*blen + 2) / 3; + ip := 0; + rp := 0; + for (i:=0; i=0; j--) + if (ip < blen) + word = word | int b[ip++] << 8*j; + for (l:=3; l>=0; l--) + if (rp < rplen) + r[rp++] = chmap[(word >> (6*l)) & 16r3f]; + else + r[rp++] = PADCH; + } + return r; +} + +# Decode a base 64 string to a byte stream +# Must be a multiple of 4 characters in length +decode(s: string): array of byte +{ + + tch: int; + slen := len s; + rlen := (3*slen+3)/4; + if (slen >= 4 && s[slen-1] == PADCH) + rlen--; + if (slen >= 4 && s[slen-2] == PADCH) + rlen--; + r := array[rlen] of byte; + full := slen / 4; + sp := 0; + rp := 0; + for (i:=0; i + tch = ch - 'A'; + 'a' to 'z' => + tch = ch - 'a' + 26; + '0' to '9' => + tch = ch - '0' + 52; + '+' => + tch = 62; + '/' => + tch = 63; + * => + tch = 0; + } + word = (word << 6) | tch; + } + for (l:=2; l>=0; l--) + if (rp < rlen) + r[rp++] = byte( (word >> 8*l) & 16rff); + + } + return r; +} + -- cgit v1.2.3