forked from MarkHofmann11/WWIVEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWECOMP.PAS
151 lines (136 loc) · 4.02 KB
/
WECOMP.PAS
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
UNIT WEComp;
{$DEFINE CAN_OVERLAY}
{$I WEGLOBAL.PAS}
{ --
-- Compression Module for WWIVEdit 2.4
-- This code is Copyright (c) 1991-1992 Adam Caldwell
-- It may be freely distributed so long as no fee is charged for it.
--
-- }
INTERFACE
FUNCTION Squash(s:string):string;
FUNCTION UnSquash(s:string):string;
IMPLEMENTATION
TYPE
StringTable = ARRAY[0..255] OF String[4];
CONST
ST : StringTable =
('''','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z','&','.',',',':',';',
'"','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z','!','~',' ','-','?',
'mi','om','ni','ho','ce','as','ha','di','ur','pe','na','ac','il','ea','ia','lo',
'ol','nd','ve','th','ca','ll','ou','si','me','io','ng','ch','to','et','se','us',
'he','tr','el','ta','es','ma','ne','li','it','is','de','la','co','ro','ic','nt',
'ri','or','al','st','ti','ra','ar','at','re','le','en','te','on','an','in','er',
'met','ran','cha','den','han','ind','mon','ica','rea','ary','che','ack','ite','lle','ria','nte',
'one','rin','ers','par','tan','age','der','ari','ten','ish','art','ere','mat','tat','son','nce',
'ber','eri','ome','ect','sto','tro','pre','ina','lat','int','ort','nti','ast','era','gra','com',
'min','ess','ide','ard','ell','lin','tin','ton','ric','all','lan','ian','sti','str','ill','sta',
'tri','rat','res','ver','pro','ist','men','est','ste','the','ive','per','abl','ran','tor','her',
'man','tra','tic','and','ati','ine','con','tio','ous','ant','ble','ter','ion','ent','ing','ate',
'inte','ttin','ator','cont','olog','stra','nter','tric','ress','ater','tory','tran','tati','land','nate','ance',
'ment','enti','ting','comp','tive','ight','ible','late','ious','ster','ther','sion','rate','atio','able','tion');
Start : ARRAY[0..4] OF byte= ( 0,16*4-1,16*8-1,16*14-1,255);
FUNCTION Squash1(s:string):string;
VAR
Temp : String;
i:integer;
BEGIN
Temp:='';
REPEAT
i:=length(s);
IF i>4 THEN i:=4;
i:=start[i];
WHILE copy(s,1,length(st[i]))<>st[i] DO
dec(i);
temp:=temp+chr(i);
delete(s,1,length(st[i]));
UNTIL s='';
Squash1:=Temp;
END;
FUNCTION Squash2(s:string):string;
VAR
Temp : String;
i:integer;
BEGIN
Temp:='';
REPEAT
i:=length(s);
IF i>4 THEN i:=4;
i:=start[i];
WHILE copy(s,length(s)-length(st[i])+1,length(st[i]))<>st[i] DO
dec(i);
temp:=chr(i)+temp;
delete(s,length(s)-length(st[i])+1,length(st[i]));
UNTIL s='';
Squash2:=Temp;
END;
FUNCTION Squash3(s:string):string;
{ -- String replacement based on random replacement... On my test data, this
-- algorithm only provided a smaller result on a few pieces of data, hence
-- I have ignored it to get better performance }
VAR
Temp : String;
i, j,p :integer;
t : ARRAY[1..100] OF integer;
cleft : integer;
BEGIN
FOR i:=1 TO length(s) DO
t[i]:=-1;
cleft:=length(s);
REPEAT
i:=255;
WHILE pos(st[i],s)=0 DO
dec(i);
p:=pos(st[i],s);
t[p]:=i;
FOR j:=p TO p+length(st[i])-1 DO
BEGIN
s[j]:=#0;
dec(cleft);
END;
UNTIL cleft=0;
temp:='';
FOR i:=1 TO length(s) DO
IF t[i]>=0 THEN temp:=temp+chr(t[i]);
Squash3:=temp;
END;
FUNCTION Squash(s:string):string;
VAR
s1,s2:string;
BEGIN
s1:=squash1(s);
s2:=squash2(s);
IF length(s1)<=length(s2)
THEN squash:=s1
ELSE squash:=s2;
END;
FUNCTION UnSquash(s:string):string;
{ -- A Lesson in speed... This procedure can be shortened (more or less) to:
--
-- FOR i:=1 TO length(s) DO
-- temp:=temp+st[ord(s[i])];
--
-- However, the lower bit of code is approximately 37% faster
-- For 31,000 runs uncompressing "stattion" [a non-sense word], the above
-- algorithm took 24 seconds on my test machine. The code below took only
-- 15 seconds.
-- }
VAR
Temp : String;
i : integer;
len, ind,bytes : integer;
BEGIN
len:=0;
FOR i:=1 TO length(s) DO
BEGIN
ind:=ord(s[i]);
bytes:=ord(st[ind][0]);
Move(st[ind][1],Temp[len+1],bytes);
len:=len+bytes;
END;
temp[0]:=chr(len);
UnSquash:=Temp;
END;
END.