forked from harrisonpartch/spasim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64.Mod
More file actions
executable file
·137 lines (121 loc) · 3.46 KB
/
Base64.Mod
File metadata and controls
executable file
·137 lines (121 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
MODULE Base64; (** AUTHOR "P.Hunziker - Ported from Oberon.Base64.Mod (JG 23.8.94) "; PURPOSE "Base64 encoding and decoding"; *)
IMPORT Streams, KernelLog,Strings;
VAR
encTable: ARRAY 64 OF CHAR;
decTable: ARRAY 128 OF INTEGER;
PROCEDURE Decode*(R: Streams.Reader; W:Streams.Writer);
VAR
codes: ARRAY 4 OF INTEGER;
i: INTEGER;
ch: CHAR;
ok, end: BOOLEAN;
BEGIN
ok := TRUE; end := FALSE;
ch:=R.Get();
REPEAT
i := 0;
WHILE ok & (i < 4) DO
WHILE ch<=" " DO ch:=R.Get(); END;
codes[i] := decTable[ORD(ch)];
ok := codes[i] >= 0; INC(i);
IF ok THEN ch:=R.Get() END;
END;
IF i > 0 THEN
IF ok THEN
W.Char(CHR(ASH(codes[0], 2)+ASH(codes[1], -4)));
W.Char(CHR(ASH(codes[1], 4)+ASH(codes[2], -2)));
W.Char(CHR(ASH(codes[2], 6)+codes[3]))
ELSIF ch = "=" THEN
ok := TRUE; end := TRUE; DEC(i);
IF i = 2 THEN W.Char( CHR(ASH(codes[0], 2)+ASH(codes[1], -4)))
ELSIF i = 3 THEN
W.Char( CHR(ASH(codes[0], 2)+ASH(codes[1], -4)));
W.Char( CHR(ASH(codes[1], 4)+ASH(codes[2], -2)))
ELSIF i # 0 THEN ok := FALSE
END
ELSIF i = 4 THEN
ok := TRUE; end := TRUE;
W.Char( CHR(ASH(codes[0], 2)+ASH(codes[1], -4)));
W.Char( CHR(ASH(codes[1], 4)+ASH(codes[2], -2)));
W.Char( CHR(ASH(codes[2], 6)+codes[3]))
ELSIF i = 1 THEN ok := TRUE; end := TRUE
END
ELSE
end := TRUE
END;
UNTIL end;
W.Update;
END Decode;
PROCEDURE Encode*(R:Streams.Reader; W:Streams.Writer);
VAR
i, j, c, c0, c1, c2, l: LONGINT;
chars: ARRAY 3 OF CHAR;
PROCEDURE OutCode(k:LONGINT);
BEGIN
IF l > 80 THEN W.Ln; l := 0 END;
c0 :=ORD(chars[0]);
c := ASH(c0, -2);
W.Char( encTable[c]);
c0 := c0-ASH(c, 2);
c1 := ORD(chars[1]);
c := ASH(c0, 4)+ASH(c1, -4);
IF k>=1 THEN W.Char(encTable[c]); END;
c1 := c1 MOD ASH(1, 4);
c2 := ORD(chars[2]);
c := ASH(c1, 2)+ASH(c2, -6);
IF k>=2 THEN W.Char(encTable[c]); END;
c2 := c2 MOD ASH(1, 6);
IF k>=3 THEN W.Char(encTable[c2]); END;
INC(l, 4)
END OutCode;
BEGIN
l := 0;
R.Char(chars[0]); i := 1;
WHILE R.res=Streams.Ok DO
IF i >= 3 THEN OutCode(i); i := 0 END;
R.Char(chars[i]); INC(i)
END;
DEC(i);
IF i > 0 THEN
j := i;
WHILE i < 3 DO chars[i] := 0X; INC(i) END;
OutCode(j);
WHILE j<3 DO W.Char("="); INC(j) END;
END;
W.Update;
END Encode;
PROCEDURE InitTables;
VAR i, max: INTEGER;
BEGIN
max := ORD("Z")-ORD("A");
FOR i := 0 TO max DO encTable[i] := CHR(i+ORD("A")) END;
INC(max);
FOR i := max TO max+ORD("z")-ORD("a") DO encTable[i] := CHR(i-max+ORD("a")) END;
max := max+ORD("z")-ORD("a")+1;
FOR i := max TO max+ORD("9")-ORD("0") DO encTable[i] := CHR(i-max+ORD("0")) END;
encTable[62] := "X";
encTable[63] := "Y"; (* i don't like this .... *)
FOR i := 0 TO 127 DO decTable[i] := -1 END;
FOR i := 0 TO 63 DO decTable[ORD(encTable[i])] := i END
END InitTables;
(* testing: expected behaviour: "admin:1234" encode => "YWRtaW46MTIzNA==" decode => "admin:1234"*)
PROCEDURE Test*;
VAR W:Streams.StringWriter; R: Streams.StringReader; s:Strings.String; plain, base64: ARRAY 80 OF CHAR;
BEGIN
NEW(W,80); NEW(R,80);
KernelLog.String('admin:1234 => '); KernelLog.Ln;
R.Set('admin:1234');
Encode(R,W);
W.Get(base64);
KernelLog.String(base64); KernelLog.String(" => ");
NEW(W,80); NEW(R,80);
R.Set(base64);
Decode(R,W);
W.Get(plain);
KernelLog.String(plain); KernelLog.Ln;
END Test;
BEGIN
InitTables;
END Base64.
Base64.Test
SystemTools.Free Base64 ~