-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdepackio.pas
1680 lines (1572 loc) · 51.3 KB
/
depackio.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Adlib Tracker II (AT2).
//
// AT2 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// AT2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AT2. If not, see <http://www.gnu.org/licenses/>.
unit DepackIO;
{$S-,Q-,R-,V-,B-,X+}
{$PACKRECORDS 1}
interface
// Compression algorithm: RDC
// Algorithm developed by Ed Ross
function RDC_decompress(var source,dest; size: Word): Word;
// Compression algorithm: LZSS
// Algorithm developed by Lempel-Ziv-Storer-Szymanski
function LZSS_decompress(var source,dest; size: Word): Word;
// Compression algorithm: LZW
// Algorithm developed by Lempel-Ziv-Welch
function LZW_decompress(var source,dest): Word;
// Compression algorithm: SixPack
// Algorithm developed by Philip G. Gage
function SIXPACK_decompress(var source,dest; size: Word): Word;
// Compression algorithm: aPack
// Algorithm developed by Joergen Ibsen
function APACK_decompress(var source,dest): Dword;
implementation
const
WORKMEM_SIZE = 64*1024;
var
work_mem: array[0..PRED(WORKMEM_SIZE)] of Byte;
ibufCount,ibufSize: Word;
input_size,output_size: Word;
input_ptr,output_ptr,work_ptr: pByte;
var
ibuf_idx,ibuf_end,obuf_idx,obuf_src: pByte;
ctrl_bits,ctrl_mask,
command,count,offs: Word;
{$IFNDEF CPU64}
procedure RDC_decode;
begin
asm
mov ctrl_mask,0
mov eax,input_ptr
mov ibuf_end,eax
xor eax,eax
mov ax,input_size
add ibuf_end,eax
mov eax,input_ptr
mov ibuf_idx,eax
mov eax,output_ptr
mov obuf_idx,eax
@@1: xor ecx,ecx
mov eax,ibuf_idx
cmp eax,ibuf_end
jnb @@7
mov ax,ctrl_mask
shr ax,1
mov ctrl_mask,ax
or ax,ax
jnz @@2
mov esi,ibuf_idx
lodsw
mov ctrl_bits,ax
add ibuf_idx,2
mov ctrl_mask,8000h
@@2: mov ax,ctrl_bits
and ax,ctrl_mask
or ax,ax
jnz @@3
mov esi,ibuf_idx
mov edi,obuf_idx
movsb
inc ibuf_idx
inc obuf_idx
jmp @@1
@@3: xor ah,ah
mov esi,ibuf_idx
lodsb
shr ax,4
and ax,0fh
mov command,ax
xor ah,ah
mov esi,ibuf_idx
lodsb
and ax,0fh
mov count,ax
inc ibuf_idx
cmp command,0
jnz @@4
add count,3
mov edi,obuf_idx
mov cx,count
mov esi,ibuf_idx
lodsb
rep stosb
inc ibuf_idx
mov cx,count
add obuf_idx,ecx
jmp @@1
@@4: cmp command,1
jnz @@5
xor ah,ah
mov esi,ibuf_idx
lodsb
shl ax,4
add count,ax
inc ibuf_idx
add count,19
mov edi,obuf_idx
mov cx,count
mov esi,ibuf_idx
lodsb
rep stosb
inc ibuf_idx
mov cx,count
add obuf_idx,ecx
jmp @@1
@@5: cmp command,2
jnz @@6
mov ax,count
add ax,3
mov offs,ax
xor ah,ah
mov esi,ibuf_idx
lodsb
shl ax,4
add offs,ax
inc ibuf_idx
xor ah,ah
mov esi,ibuf_idx
lodsb
mov count,ax
inc ibuf_idx
add count,16
mov eax,obuf_idx
mov cx,offs
sub eax,ecx
mov obuf_src,eax
mov esi,eax
mov edi,obuf_idx
mov cx,count
rep movsb
mov cx,count
add obuf_idx,ecx
jmp @@1
@@6: mov ax,count
add ax,3
mov offs,ax
xor ah,ah
mov esi,ibuf_idx
lodsb
shl ax,4
add offs,ax
inc ibuf_idx
mov eax,obuf_idx
mov cx,offs
sub eax,ecx
mov obuf_src,eax
mov esi,eax
mov edi,obuf_idx
mov cx,command
rep movsb
mov cx,command
add obuf_idx,ecx
jmp @@1
@@7: mov eax,obuf_idx
sub eax,output_ptr
mov output_size,ax
end;
end;
{$ELSE}
procedure RDC_decode;
begin
ctrl_mask := 0;
ibuf_idx := input_ptr;
obuf_idx := output_ptr;
ibuf_end := input_ptr+input_size;
While (ibuf_idx < ibuf_end) do
begin
ctrl_mask := ctrl_mask SHR 1;
If (ctrl_mask = 0) then
begin
ctrl_bits := pWord(ibuf_idx)^;
Inc(ibuf_idx,2);
ctrl_mask := $8000;
end;
If (ctrl_bits AND ctrl_mask = 0) then
begin
obuf_idx^ := ibuf_idx^;
Inc(ibuf_idx);
Inc(obuf_idx);
CONTINUE;
end;
command := (ibuf_idx^ SHR 4) AND 15;
count := ibuf_idx^ AND 15;
Inc(ibuf_idx);
Case command Of
// short RLE
0: begin
Inc(count,3);
FillChar(obuf_idx^,count,ibuf_idx^);
Inc(ibuf_idx);
Inc(obuf_idx,count);
end;
// long RLE
1: begin
Inc(count,ibuf_idx^ SHL 4);
Inc(ibuf_idx);
Inc(count,19);
FillChar(obuf_idx^,count,ibuf_idx^);
Inc(ibuf_idx);
Inc(obuf_idx, count);
end;
// long pattern
2: begin
offs := count+3;
Inc(offs,ibuf_idx^ SHL 4);
Inc(ibuf_idx);
count := ibuf_idx^;
Inc(ibuf_idx);
Inc(count,16);
obuf_src := obuf_idx-offs;
Move(obuf_src^,obuf_idx^,count);
Inc(obuf_idx,count);
end;
// short pattern
else begin
offs := count+3;
Inc(offs,ibuf_idx^ SHL 4);
Inc(ibuf_idx);
obuf_src := obuf_idx-offs;
Move(obuf_src^,obuf_idx^,command);
Inc(obuf_idx,command);
end;
end;
end;
output_size := obuf_idx-output_ptr;
end;
{$ENDIF}
function RDC_decompress(var source,dest; size: Word): Word;
begin
input_ptr := @source;
output_ptr := @dest;
input_size := size;
RDC_decode;
RDC_decompress := output_size;
end;
const
N_BITS = 12;
F_BITS = 4;
THRESHOLD = 2;
N = 1 SHL N_BITS;
F = (1 SHL F_BITS)+THRESHOLD;
{$IFNDEF CPU64}
procedure GetChar; assembler;
asm
push ebx
mov bx,ibufCount
cmp bx,ibufSize
jb @@1
jmp @@2
@@1: push edi
mov edi,input_ptr
mov al,byte ptr [edi+ebx]
pop edi
inc ebx
mov ibufCount,bx
pop ebx
clc
jmp @@3
@@2: pop ebx
stc
@@3:
end;
procedure PutChar; assembler;
asm
push ebx
mov bx,output_size
push edi
mov edi,output_ptr
mov byte ptr [edi+ebx],al
pop edi
inc ebx
mov output_size,bx
pop ebx
end;
procedure LZSS_decode;
begin
asm
mov ibufCount,0
mov ax,input_size
mov ibufSize,ax
mov output_size,0
xor ebx,ebx
xor edx,edx
mov edi,N-F
@@1: shr dx,1
or dh,dh
jnz @@2
call GetChar
jc @@5
mov dh,0ffh
mov dl,al
@@2: test dx,1
jz @@3
call GetChar
jc @@5
push esi
mov esi,work_ptr
add esi,edi
mov byte ptr [esi],al
pop esi
inc edi
and edi,N-1
call PutChar
jmp @@1
@@3: call GetChar
jc @@5
mov ch,al
call GetChar
jc @@5
mov bh,al
mov cl,4
shr bh,cl
mov bl,ch
mov cl,al
and cl,0fh
add cl,THRESHOLD
inc cl
@@4: and ebx,N-1
push esi
mov esi,work_ptr
mov al,byte ptr [esi+ebx]
add esi,edi
mov byte ptr [esi],al
pop esi
inc edi
and edi,N-1
call PutChar
inc ebx
dec cl
jnz @@4
jmp @@1
@@5:
end;
end;
{$ELSE}
procedure LZSS_decode;
label
j1,j2,j3,j4,j5;
var
al,cl,ch,cf: Byte;
dx: Word;
ebx,edi: Dword;
procedure GetChar;
begin
If (ibufCount < ibufSize) then
begin
al := input_ptr[ibufCount];
Inc(ibufCount);
cf := 0;
end
else
cf := 1;
end;
procedure PutChar;
begin
output_ptr[output_size] := al;
Inc(output_size);
end;
begin
ibufCount := 0; // mov ibufCount,0
ibufSize := input_size; // mov ax,input_size
// mov ibufSize,ax
output_size := 0; // mov output_size,0
ebx := 0; // xor ebx,ebx
dx := 0; // xor edx,edx
edi := N-F; // mov edi,N-F
j1: dx := dx SHR 1; //@@1: shr dx,1
If (dx SHR 8 <> 0) then // or dh,dh
GOTO j2; // jnz @@2
GetChar; // call GetChar
If (cf = 1) then GOTO j5; // jc @@5
dx := $ff00 OR al; // mov dh,0ffh
// mov dl,al
j2: If (dx AND 1 = 0) then //@@2: test dx,1
GOTO j3; // jz @@3
GetChar; // call GetChar
If (cf = 1) then GOTO j5; // jc @@5
// push esi
work_ptr[edi] := al; // mov esi,work_ptr
// add esi,edi
// mov byte ptr [esi],al
// pop esi
edi := (edi+1) AND (N-1); // inc edi
// and edi,N-1
PutChar; // caj PutChar
GOTO j1; // jmp @@1
j3: GetChar; //@@3: caj GetChar
If (cf = 1) then GOTO j5; // jc @@5
ch := al; // mov ch,al
GetChar; // call GetChar
If (cf = 1) then GOTO j5; // jc @@5
// mov bh,al
// mov cl,4
ebx := (al SHL 4) AND $ff00; // shr bh,cl
ebx := ebx OR ch; // mov bl,ch
// mov cl,al
// and cl,0fh
cl := (al AND $0f)+THRESHOLD; // add cl,THRESHOLD
Inc(cl); // inc cl
j4: ebx := ebx AND (N-1); //@@4: and ebx,N-1
// push esi
al := work_ptr[ebx]; // mov esi,work_ptr
// mov al,byte ptr [esi+ebx]
// add esi,edi
work_ptr[edi] := al; // mov byte ptr [esi],al
// pop esi
Inc(edi); // inc edi
edi := edi AND (N-1); // and edi,N-1
PutChar; // call PutChar
Inc(ebx); // inc ebx
Dec(cl); // dec cl
If (cl <> 0) then GOTO j4; // jnz @@4
GOTO j1; // jmp @@1
j5: //@@5:
end;
{$ENDIF}
function LZSS_decompress(var source,dest; size: Word): Word;
begin
input_ptr := @source;
output_ptr := @dest;
work_ptr := @work_mem;
input_size := size;
FillChar(work_ptr^,WORKMEM_SIZE,0);
LZSS_decode;
LZSS_decompress := output_size;
end;
var
le76,le77: Byte;
le6a,le6c,le6e,le70,le72,le74,le78,
le82a,le82b: Word;
const
le7a: array[0..4] of Word = ($1ff,$3ff,$7ff,$0fff,$1fff);
{$IFNDEF CPU64}
procedure NextCode; assembler;
asm
mov bx,le82a
mov ax,le82b
add bx,le78
adc ax,0
xchg bx,le82a
xchg ax,le82b
mov cx,bx
and cx,7
shr ax,1
rcr bx,1
shr ax,1
rcr bx,1
shr ax,1
rcr bx,1
mov esi,input_ptr
mov ax,[ebx+esi]
mov dl,[ebx+esi+2]
or cx,cx
jz @@2
@@1: shr dl,1
rcr ax,1
loop @@1
@@2: mov bx,le78
sub bx,9
shl bx,1
and ax,le7a[ebx]
end;
procedure LZW_decode;
begin
asm
xor eax,eax
xor ebx,ebx
xor ecx,ecx
mov le72,0
mov le78,9
mov le70,102h
mov le74,200h
mov edi,output_ptr
xor eax,eax
mov le6a,ax
mov le6c,ax
mov le6e,ax
mov le76,al
mov le77,al
mov le82a,ax
mov le82b,ax
@@1: call NextCode
cmp ax,101h
jnz @@2
jmp @@9
@@2: cmp ax,100h
jnz @@3
mov le78,9
mov le74,200h
mov le70,102h
call NextCode
mov le6a,ax
mov le6c,ax
mov le77,al
mov le76,al
mov al,le77
mov byte ptr [edi],al
inc edi
jmp @@1
@@3: mov le6a,ax
mov le6e,ax
cmp ax,le70
jb @@4
mov ax,le6c
mov le6a,ax
mov al,le76
push eax
inc le72
@@4: cmp le6a,0ffh
jbe @@5
mov esi,work_ptr
mov bx,le6a
shl bx,1
add bx,le6a
mov al,[ebx+esi+2]
push eax
inc le72
mov ax,[ebx+esi]
mov le6a,ax
jmp @@4
@@5: mov ax,le6a
mov le76,al
mov le77,al
push eax
inc le72
xor ecx,ecx
mov cx,le72
jecxz @@7
@@6: pop eax
mov byte ptr [edi],al
inc edi
loop @@6
@@7: mov le72,0
push esi
mov bx,le70
shl bx,1
add bx,le70
mov esi,work_ptr
mov al,le77
mov [ebx+esi+2],al
mov ax,le6c
mov [ebx+esi],ax
inc le70
pop esi
mov ax,le6e
mov le6c,ax
mov bx,le70
cmp bx,le74
jl @@8
cmp le78,14
jz @@8
inc le78
shl le74,1
@@8: jmp @@1
@@9: mov eax,edi
sub eax,output_ptr
mov output_size,ax
end;
end;
{$ELSE}
var
stack: array[WORD] of Byte;
ax,bx,cx,sp: Word;
edi,td: Dword;
procedure NextCode;
label j2;
begin
bx := le82a; // mov bx,le82a
ax := le82b; // mov ax,le82b
td := (ax SHL 16)+bx; // add bx,le78
td := td + le78; // adc ax,0
le82a := td AND $ffff; // xchg bx,le82a
le82b := td SHR 16; // xchg ax,le82b
cx := bx AND 7; // mov cx,bx
td := (ax SHL 16)+bx; // and cx,7
td := td SHR 1; // shr ax,1
// rcr bx,1
td := td SHR 1; // shr ax,1
// rcr bx,1
td := td SHR 1; // shr ax,1
bx := td; // rcr bx,1
td := input_ptr[bx]+ // mov esi,input_ptr
(input_ptr[bx+1] shl 8)+ // mov ax,[ebx+esi]
(input_ptr[bx+2] shl 16); // mov dl,[ebx+esi+2]
If (cx = 0) then // or cx,cx
GOTO j2; // jz @@2
While (cx <> 0) do
begin //@@1: shr dl,1
td := td SHR 1; Dec(cx); // rcr ax,1
end; // loop @@1
j2: bx := le78; //@@2: mov bx,le78
Dec(bx,9); // sub bx,9
// shl bx,1
ax:=td AND le7a[bx]; // and ax,[ebx+le7a_0]
end;
procedure LZW_decode;
label
j1,j2,j3,j4,j5,j7,j8,j9;
begin
sp := PRED(SizeOf(stack));
le72 := 0; // mov le72,0
le78 := 9; // mov le78,9
le70 := $102; // mov le70,102h
le74 := $200; // mov le74,200h
edi := 0; // mov edi,output_ptr
ax := 0; // xor eax,eax
le6a := 0; // mov le6a,ax
le6c := 0; // mov le6c,ax
le6e := 0; // mov le6e,ax
le76 := 0; // mov le76,al
le77 := 0; // mov le77,al
le82a := 0; // mov le82a,ax
le82b := 0; // mov le82b,ax
j1: NextCode; //@@1: call NextCode
If (ax <> $101) then // cmp ax,101h
GOTO j2; // jnz @@2
GOTO j9; // jmp @@9
j2: If (ax <> $100) then //@@2: cmp ax,100h
GOTO j3; // jnz @@3
le78 := 9; // mov le78,9
le74 := $200; // mov le74,200h
le70 := $102; // mov le70,102h
NextCode; // caj NextCode
le6a := ax; // mov le6a,ax
le6c := ax; // mov le6c,ax
le77 := ax; // mov le77,al
le76 := ax; // mov le76,al
// mov al,le77
output_ptr[edi] := ax; // mov byte ptr [edi],al
Inc(edi); // inc edi
GOTO j1; // jmp @@1
j3: le6a := ax; //@@3: mov le6a,ax
le6e := ax; // mov le6e,ax
If (ax < le70) then // cmp ax,le70
GOTO j4; // jb @@4
ax := le6c; // mov ax,le6c
le6a := ax; // mov le6a,ax
ax := (ax AND $ff00)+le76; // mov al,le76
Dec(sp); stack[sp] := ax; // push eax
Inc(le72); // inc le72
j4: If (le6a <= $ff) then //@@4: cmp le6a,0ffh
GOTO j5; // jbe @@5
// mov esi,work_ptr
// mov bx,le6a
bx := le6a*3; // shl bx,1
ax := (ax AND $ff00)+work_ptr[bx+2]; // add bx,le6a
Dec(sp); // mov al,[ebx+esi+2]
stack[sp] := ax; // push eax
Inc(le72); // inc le72
ax := work_ptr[bx]+(work_ptr[bx+1] SHL 8); // mov ax,[ebx+esi]
le6a := ax; // mov le6a,ax
GOTO j4; // jmp @@4
j5: ax := le6a; //@@5: mov ax,le6a
le76 := ax; // mov le76,al
le77 := ax; // mov le77,al
Dec(sp); stack[sp] := ax; // push eax
Inc(le72); // inc le72
// xor ecx,ecx
cx := le72; // mov cx,le72
If (cx = 0) then GOTO j7; // jecxz @@7
While (cx <> 0) do //
begin //
ax := stack[sp]; Inc(sp); //@@6: pop eax
output_ptr[edi] := ax; // mov byte ptr [edi],al
Inc(edi); Dec(cx); // inc edi
end; // loop @@6
j7: le72 := 0; //@@7: mov le72,0
// push esi
// mov bx,le70
// shl bx,1
bx:=le70*3; // add bx,le70
// mov esi,work_ptr
// mov al,le77
work_ptr[bx+2] := le77; // mov [ebx+esi+2],al
work_ptr[bx+1] := le6c SHR 8; // mov ax,le6c
work_ptr[bx+0] := le6c; // mov [ebx+esi],ax
Inc(le70); // inc le70
// pop esi
ax := le6e; // mov ax,le6e
le6c := ax; // mov le6c,ax
bx := le70; // mov bx,le70
If (bx < le74) then // cmp bx,le74
GOTO j8; // jl @@8
If (le78 = 14) then // cmp le78,14
GOTO j8; // jz @@8
Inc(le78); // inc le78
le74 := le74 SHL 1; // shl le74,1
j8: GOTO j1; //@@8: jmp @@1
j9: output_size := edi; //@@9: mov output_size,ax
end;
{$ENDIF}
function LZW_decompress(var source,dest): Word;
begin
input_ptr := @source;
output_ptr := @dest;
work_ptr := @work_mem;
LZW_decode;
LZW_decompress := output_size;
end;
const
MAXFREQ = 2000;
MINCOPY = 3;
MAXCOPY = 255;
COPYRANGES = 6;
TERMINATE = 256;
FIRSTCODE = 257;
ROOT = 1;
CODESPERRANGE = MAXCOPY-MINCOPY+1;
MAXCHAR = FIRSTCODE+COPYRANGES*CODESPERRANGE-1;
SUCCMAX = MAXCHAR+1;
TWICEMAX = 2*MAXCHAR+1;
MAXBUF = PRED(64*1024);
MAXDISTANCE = 21389;
MAXSIZE = 21389+MAXCOPY;
const
BitValue: array[1..14] of Word = (1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192);
CopyBits: array[0..PRED(COPYRANGES)] of Word = (4,6,8,10,12,14);
CopyMin: array[0..PRED(COPYRANGES)] of Word = (0,16,80,336,1360,5456);
var
leftC,rghtC: array[0..MAXCHAR] of Word;
dad,frq: array[0..TWICEMAX] of Word;
ibitCount,ibitBuffer,obufCount: Word;
{$IFNDEF CPU64}
var
index: Word;
procedure InitTree;
begin
asm
xor edi,edi
mov di,2
mov bx,2
mov cx,1
@@1: xor dx,dx
mov ax,di
div bx
push edi
shl di,1
mov word ptr dad[edi],ax
mov word ptr frq[edi],cx
pop edi
inc di
cmp di,TWICEMAX
jbe @@1
mov di,1
@@2: xor dx,dx
mov ax,di
mul bx
push edi
shl di,1
mov word ptr leftC[edi],ax
inc ax
mov word ptr rghtC[edi],ax
pop edi
inc di
cmp di,MAXCHAR
jbe @@2
end;
end;
procedure UpdateFreq(a,b: Word);
begin
asm
xor ecx,ecx
xor edi,edi
@@1: mov di,a
shl di,1
mov bx,word ptr frq[edi]
mov di,b
shl di,1
add bx,word ptr frq[edi]
mov di,a
shl di,1
mov dx,word ptr dad[edi]
mov di,dx
shl di,1
mov word ptr frq[edi],bx
mov a,dx
cmp a,ROOT
jz @@3
mov di,a
shl di,1
mov di,word ptr dad[edi]
mov ax,di
shl di,1
mov bx,word ptr leftC[edi]
cmp a,bx
jnz @@2
mov di,ax
shl di,1
mov bx,word ptr rghtC[edi]
mov b,bx
jmp @@3
@@2: mov di,ax
shl di,1
mov bx,word ptr leftC[edi]
mov b,bx
@@3: cmp a,ROOT
jnz @@1
mov bx,MAXFREQ
mov di,ROOT
shl di,1
cmp word ptr frq[edi],bx
jnz @@5
lea esi,[frq]
lea edi,[frq]
mov cx,TWICEMAX
movsw
@@4: lodsw
shr ax,1
stosw
loop @@4
@@5:
end;
end;
procedure UpdateModel(code: Word);
begin
asm
xor ecx,ecx
xor edi,edi
mov bx,code
add bx,SUCCMAX
mov di,bx
shl di,1
mov ax,di
mov cx,word ptr frq[edi]
inc cx
mov word ptr frq[edi],cx
mov di,ax
mov cx,ROOT
cmp word ptr dad[edi],cx
jz @@10
mov dx,word ptr dad[edi]
push edi
lea edi,[leftC]
mov cx,dx
shl cx,1
add edi,ecx
mov si,word ptr [edi]
pop edi
cmp si,bx
jnz @@1
mov di,dx
shl di,1
mov si,word ptr rghtC[edi]
@@1: push ebx
push edx
push ebx
push esi
call UpdateFreq
pop edx
pop ebx
@@2: xor edi,edi
mov di,dx
shl di,1
mov ax,word ptr dad[edi]
mov di,ax
shl di,1
mov cx,di
cmp word ptr leftC[edi],dx
jnz @@3
mov di,cx
mov si,word ptr rghtC[edi]
jmp @@4
@@3: mov si,word ptr leftC[edi]
@@4: xor edi,edi
mov di,bx
shl di,1
push eax
mov ax,word ptr frq[edi]
mov di,si
shl di,1
mov cx,ax
pop eax
cmp cx,word ptr frq[edi]
jbe @@9
mov di,ax
shl di,1
mov cx,di
cmp word ptr leftC[edi],dx
jnz @@5
mov di,cx
mov word ptr rghtC[edi],bx
jmp @@6
@@5: xor edi,edi
mov di,cx
mov word ptr leftC[edi],bx
@@6: lea edi,[leftC]
xor ecx,ecx
mov cx,dx
shl cx,1
add edi,ecx
cmp word ptr [edi],bx
jnz @@7
mov word ptr [edi],si
xor edi,edi
mov di,cx
mov cx,word ptr rghtC[edi]
jmp @@8
@@7: xor edi,edi
mov di,cx
mov word ptr rghtC[edi],si
mov cx,word ptr leftC[edi]
@@8: xor edi,edi
mov di,si
shl di,1
mov word ptr dad[edi],dx
mov di,bx
shl di,1
mov word ptr dad[edi],ax
push esi
push esi
push ecx
call UpdateFreq
pop ebx
@@9: xor edi,edi
mov di,bx
shl di,1
mov bx,word ptr dad[edi]
mov di,bx
shl di,1
mov dx,word ptr dad[edi]
cmp dx,ROOT
jnz @@2
@@10:
end;
end;