OSDN Git Service

wwww
[proj16/16.git] / 16 / ADT2PLAY / unpk_lib.pas
1 unit UNPK_LIB;\r
2 interface\r
3 \r
4 // Compression algorithm: RDC\r
5 // Algorithm developed by Ed Ross\r
6 function RDC_decompress(var source,dest; size: Word): Word;\r
7 \r
8 // Compression algorithm: LZSS\r
9 // Algorithm developed by Lempel-Ziv-Storer-Szymanski\r
10 function LZSS_decompress(var source,dest; size: Word): Word;\r
11 \r
12 // Compression algorithm: LZW\r
13 // Algorithm developed by Lempel-Ziv-Welch\r
14 function LZW_decompress(var source,dest): Word;\r
15 \r
16 // Compression algorithm: SixPack\r
17 // Algorithm developed by Philip G. Gage\r
18 function SIXPACK_decompress(var source,dest; size: Word): Word;\r
19 \r
20 // Compression algorithm: aPack\r
21 // Algorithm developed by Joergen Ibsen\r
22 function APACK_decompress(var source,dest): Longint;\r
23 \r
24 implementation\r
25 \r
26 const\r
27   WORKMEM_SIZE = 64*1024;\r
28 \r
29 var\r
30   work_mem: array[0..PRED(WORKMEM_SIZE)] of Byte;\r
31   ibufCount,ibufSize: Word;\r
32   input_size,output_size: Word;\r
33   input_ptr,output_ptr,work_ptr: Pointer;\r
34 \r
35 var\r
36   ibuf_idx,ibuf_end,obuf_idx,obuf_src: Pointer;\r
37   ctrl_bits,ctrl_mask,\r
38   command,count,offs: Word;\r
39 \r
40 procedure RDC_decode; assembler;\r
41 asm\r
42         mov     ctrl_mask,0\r
43         mov     eax,input_ptr\r
44         mov     ibuf_end,eax\r
45         xor     eax,eax\r
46         mov     ax,input_size\r
47         add     ibuf_end,eax\r
48         mov     eax,input_ptr\r
49         mov     ibuf_idx,eax\r
50         mov     eax,output_ptr\r
51         mov     obuf_idx,eax\r
52 @@1:    xor     ecx,ecx\r
53         mov     eax,ibuf_idx\r
54         cmp     eax,ibuf_end\r
55         jnb     @@7\r
56         mov     ax,ctrl_mask\r
57         shr     ax,1\r
58         mov     ctrl_mask,ax\r
59         or      ax,ax\r
60         jnz     @@2\r
61         mov     esi,ibuf_idx\r
62         lodsw\r
63         mov     ctrl_bits,ax\r
64         add     ibuf_idx,2\r
65         mov     ctrl_mask,8000h\r
66 @@2:    mov     ax,ctrl_bits\r
67         and     ax,ctrl_mask\r
68         or      ax,ax\r
69         jnz     @@3\r
70         mov     esi,ibuf_idx\r
71         mov     edi,obuf_idx\r
72         movsb\r
73         inc     ibuf_idx\r
74         inc     obuf_idx\r
75         jmp     @@1\r
76 @@3:    xor     ah,ah\r
77         mov     esi,ibuf_idx\r
78         lodsb\r
79         shr     ax,4\r
80         and     ax,0fh\r
81         mov     command,ax\r
82         xor     ah,ah\r
83         mov     esi,ibuf_idx\r
84         lodsb\r
85         and     ax,0fh\r
86         mov     count,ax\r
87         inc     ibuf_idx\r
88         cmp     command,0\r
89         jnz     @@4\r
90         add     count,3\r
91         mov     edi,obuf_idx\r
92         mov     cx,count\r
93         mov     esi,ibuf_idx\r
94         lodsb\r
95         rep     stosb\r
96         inc     ibuf_idx\r
97         mov     cx,count\r
98         add     obuf_idx,ecx\r
99         jmp     @@1\r
100 @@4:    cmp     command,1\r
101         jnz     @@5\r
102         xor     ah,ah\r
103         mov     esi,ibuf_idx\r
104         lodsb\r
105         shl     ax,4\r
106         add     count,ax\r
107         inc     ibuf_idx\r
108         add     count,19\r
109         mov     edi,obuf_idx\r
110         mov     cx,count\r
111         mov     esi,ibuf_idx\r
112         lodsb\r
113         rep     stosb\r
114         inc     ibuf_idx\r
115         mov     cx,count\r
116         add     obuf_idx,ecx\r
117         jmp     @@1\r
118 @@5:    cmp     command,2\r
119         jnz     @@6\r
120         mov     ax,count\r
121         add     ax,3\r
122         mov     offs,ax\r
123         xor     ah,ah\r
124         mov     esi,ibuf_idx\r
125         lodsb\r
126         shl     ax,4\r
127         add     offs,ax\r
128         inc     ibuf_idx\r
129         xor     ah,ah\r
130         mov     esi,ibuf_idx\r
131         lodsb\r
132         mov     count,ax\r
133         inc     ibuf_idx\r
134         add     count,16\r
135         mov     eax,obuf_idx\r
136         mov     cx,offs\r
137         sub     eax,ecx\r
138         mov     obuf_src,eax\r
139         mov     esi,eax\r
140         mov     edi,obuf_idx\r
141         mov     cx,count\r
142         rep     movsb\r
143         mov     cx,count\r
144         add     obuf_idx,ecx\r
145         jmp     @@1\r
146 @@6:    mov     ax,count\r
147         add     ax,3\r
148         mov     offs,ax\r
149         xor     ah,ah\r
150         mov     esi,ibuf_idx\r
151         lodsb\r
152         shl     ax,4\r
153         add     offs,ax\r
154         inc     ibuf_idx\r
155         mov     eax,obuf_idx\r
156         mov     cx,offs\r
157         sub     eax,ecx\r
158         mov     obuf_src,eax\r
159         mov     esi,eax\r
160         mov     edi,obuf_idx\r
161         mov     cx,command\r
162         rep     movsb\r
163         mov     cx,command\r
164         add     obuf_idx,ecx\r
165         jmp     @@1\r
166 @@7:    mov     eax,obuf_idx\r
167         sub     eax,output_ptr\r
168         mov     output_size,ax\r
169 end;\r
170 \r
171 function RDC_decompress(var source,dest; size: Word): Word;\r
172 begin\r
173   input_ptr := @source;\r
174   output_ptr := @dest;\r
175   input_size := size;\r
176   RDC_decode;\r
177   RDC_decompress := output_size;\r
178 end;\r
179 \r
180 const\r
181   N = 4096;\r
182   F = 18;\r
183   THRESHOLD = 2;\r
184 \r
185 procedure GetChar; assembler;\r
186 asm\r
187         push    ebx\r
188         mov     bx,ibufCount\r
189         cmp     bx,ibufSize\r
190         jb      @@1\r
191         jmp     @@2\r
192 @@1:    push    edi\r
193         mov     edi,input_ptr\r
194         mov     al,byte ptr [edi+ebx]\r
195         pop     edi\r
196         inc     ebx\r
197         mov     ibufCount,bx\r
198         pop     ebx\r
199         clc\r
200         jmp     @@3\r
201 @@2:    pop     ebx\r
202         stc\r
203 @@3:\r
204 end;\r
205 \r
206 procedure PutChar; assembler;\r
207 asm\r
208         push    ebx\r
209         mov     bx,output_size\r
210         push    edi\r
211         mov     edi,output_ptr\r
212         mov     byte ptr [edi+ebx],al\r
213         pop     edi\r
214         inc     ebx\r
215         mov     output_size,bx\r
216         pop     ebx\r
217 end;\r
218 \r
219 procedure LZSS_decode; assembler;\r
220 asm\r
221         mov     ibufCount,0\r
222         mov     ax,input_size\r
223         mov     ibufSize,ax\r
224         mov     output_size,0\r
225         xor     ebx,ebx\r
226         xor     edx,edx\r
227         mov     edi,N-F\r
228 @@1:    shr     dx,1\r
229         or      dh,dh\r
230         jnz     @@2\r
231         call    GetChar\r
232         jc      @@5\r
233         mov     dh,0ffh\r
234         mov     dl,al\r
235 @@2:    test    dx,1\r
236         jz      @@3\r
237         call    GetChar\r
238         jc      @@5\r
239         push    esi\r
240         mov     esi,work_ptr\r
241         add     esi,edi\r
242         mov     byte ptr [esi],al\r
243         pop     esi\r
244         inc     edi\r
245         and     edi,N-1\r
246         call    PutChar\r
247         jmp     @@1\r
248 @@3:    call    GetChar\r
249         jc      @@5\r
250         mov     ch,al\r
251         call    GetChar\r
252         jc      @@5\r
253         mov     bh,al\r
254         mov     cl,4\r
255         shr     bh,cl\r
256         mov     bl,ch\r
257         mov     cl,al\r
258         and     cl,0fh\r
259         add     cl,THRESHOLD\r
260         inc     cl\r
261 @@4:    and     ebx,N-1\r
262         push    esi\r
263         mov     esi,work_ptr\r
264         mov     al,byte ptr [esi+ebx]\r
265         add     esi,edi\r
266         mov     byte ptr [esi],al\r
267         pop     esi\r
268         inc     edi\r
269         and     edi,N-1\r
270         call    PutChar\r
271         inc     ebx\r
272         dec     cl\r
273         jnz     @@4\r
274         jmp     @@1\r
275 @@5:\r
276 end;\r
277 \r
278 function LZSS_decompress(var source,dest; size: Word): Word;\r
279 \r
280 begin\r
281   input_ptr := @source;\r
282   output_ptr := @dest;\r
283   work_ptr := @work_mem;\r
284   input_size := size;\r
285   FillChar(work_ptr^,WORKMEM_SIZE,0);\r
286   LZSS_decode;\r
287   LZSS_decompress := output_size;\r
288 end;\r
289 \r
290 var\r
291   le76,le77: Byte;\r
292   le6a,le6c,le6e,le70,le72,le74,le78,\r
293   le7a_0,le7a_2,le7a_4,le7a_6,le7a_8,le82a,le82b: Word;\r
294 \r
295 procedure NextCode; assembler;\r
296 asm\r
297         mov     bx,le82a\r
298         mov     ax,le82b\r
299         add     bx,le78\r
300         adc     ax,0\r
301         xchg    bx,le82a\r
302         xchg    ax,le82b\r
303         mov     cx,bx\r
304         and     cx,7\r
305         shr     ax,1\r
306         rcr     bx,1\r
307         shr     ax,1\r
308         rcr     bx,1\r
309         shr     ax,1\r
310         rcr     bx,1\r
311         mov     esi,input_ptr\r
312         mov     ax,[ebx+esi]\r
313         mov     dl,[ebx+esi+2]\r
314         or      cx,cx\r
315         jz      @@2\r
316 @@1:    shr     dl,1\r
317         rcr     ax,1\r
318         loop    @@1\r
319 @@2:    mov     bx,le78\r
320         sub     bx,9\r
321         shl     bx,1\r
322         and     ax,[ebx+le7a_0]\r
323 end;\r
324 \r
325 function LZW_decode: Word; assembler;\r
326 asm\r
327         xor     eax,eax\r
328         xor     ebx,ebx\r
329         xor     ecx,ecx\r
330         mov     le72,0\r
331         mov     le78,9\r
332         mov     le70,102h\r
333         mov     le74,200h\r
334         mov     edi,output_ptr\r
335         xor     eax,eax\r
336         mov     le6a,ax\r
337         mov     le6c,ax\r
338         mov     le6e,ax\r
339         mov     le76,al\r
340         mov     le77,al\r
341         mov     le82a,ax\r
342         mov     le82b,ax\r
343         mov     le7a_0,1ffh\r
344         mov     le7a_2,3ffh\r
345         mov     le7a_4,7ffh\r
346         mov     le7a_6,0fffh\r
347         mov     le7a_8,1fffh\r
348 @@1:    call    NextCode\r
349         cmp     ax,101h\r
350         jnz     @@2\r
351         jmp     @@9\r
352 @@2:    cmp     ax,100h\r
353         jnz     @@3\r
354         mov     le78,9\r
355         mov     le74,200h\r
356         mov     le70,102h\r
357         call    NextCode\r
358         mov     le6a,ax\r
359         mov     le6c,ax\r
360         mov     le77,al\r
361         mov     le76,al\r
362         mov     al,le77\r
363         mov     byte ptr [edi],al\r
364         inc     edi\r
365         jmp     @@1\r
366 @@3:    mov     le6a,ax\r
367         mov     le6e,ax\r
368         cmp     ax,le70\r
369         jb      @@4\r
370         mov     ax,le6c\r
371         mov     le6a,ax\r
372         mov     al,le76\r
373         push    eax\r
374         inc     le72\r
375 @@4:    cmp     le6a,0ffh\r
376         jbe     @@5\r
377         mov     esi,work_ptr\r
378         mov     bx,le6a\r
379         shl     bx,1\r
380         add     bx,le6a\r
381         mov     al,[ebx+esi+2]\r
382         push    eax\r
383         inc     le72\r
384         mov     ax,[ebx+esi]\r
385         mov     le6a,ax\r
386         jmp     @@4\r
387 @@5:    mov     ax,le6a\r
388         mov     le76,al\r
389         mov     le77,al\r
390         push    eax\r
391         inc     le72\r
392         xor     ecx,ecx\r
393         mov     cx,le72\r
394         jecxz   @@7\r
395 @@6:    pop     eax\r
396         mov     byte ptr [edi],al\r
397         inc     edi\r
398         loop    @@6\r
399 @@7:    mov     le72,0\r
400         push    esi\r
401         mov     bx,le70\r
402         shl     bx,1\r
403         add     bx,le70\r
404         mov     esi,work_ptr\r
405         mov     al,le77\r
406         mov     [ebx+esi+2],al\r
407         mov     ax,le6c\r
408         mov     [ebx+esi],ax\r
409         inc     le70\r
410         pop     esi\r
411         mov     ax,le6e\r
412         mov     le6c,ax\r
413         mov     bx,le70\r
414         cmp     bx,le74\r
415         jl      @@8\r
416         cmp     le78,14\r
417         jz      @@8\r
418         inc     le78\r
419         shl     le74,1\r
420 @@8:    jmp     @@1\r
421 @@9:    mov     output_size,ax\r
422 end;\r
423 \r
424 function LZW_decompress(var source,dest): Word;\r
425 begin\r
426   input_ptr := @source;\r
427   output_ptr := @dest;\r
428   work_ptr := @work_mem;\r
429   LZW_decode;\r
430   LZW_decompress := output_size;\r
431 end;\r
432 \r
433 const\r
434   MAXFREQ       = 2000;\r
435   MINCOPY       = 3;\r
436   MAXCOPY       = 255;\r
437   COPYRANGES    = 6;\r
438   TERMINATE     = 256;\r
439   FIRSTCODE     = 257;\r
440   ROOT          = 1;\r
441   CODESPERRANGE = MAXCOPY-MINCOPY+1;\r
442   MAXCHAR       = FIRSTCODE+COPYRANGES*CODESPERRANGE-1;\r
443   SUCCMAX       = MAXCHAR+1;\r
444   TWICEMAX      = 2*MAXCHAR+1;\r
445   MAXBUF        = PRED(64*1024);\r
446   MAXDISTANCE   = 21389;\r
447   MAXSIZE       = 21389+MAXCOPY;\r
448 \r
449 const\r
450   BitValue: array[1..14] of Word = (1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192);\r
451   CopyBits: array[0..PRED(COPYRANGES)] of Word = (4,6,8,10,12,14);\r
452   CopyMin:  array[0..PRED(COPYRANGES)] of Word = (0,16,80,336,1360,5456);\r
453 \r
454 var\r
455   leftC,rghtC: array[0..MAXCHAR] of Word;\r
456   dad,freq: array[0..TWICEMAX] of Word;\r
457   index,ibitCount,ibitBuffer,obufCount: Word;\r
458 \r
459 procedure InitTree; assembler;\r
460 asm\r
461         xor     edi,edi\r
462         mov     di,2\r
463         mov     bx,2\r
464         mov     cx,1\r
465 @@1:    xor     dx,dx\r
466         mov     ax,di\r
467         div     bx\r
468         push    edi\r
469         shl     di,1\r
470         mov     word ptr dad[edi],ax\r
471         mov     word ptr freq[edi],cx\r
472         pop     edi\r
473         inc     di\r
474         cmp     di,TWICEMAX\r
475         jbe     @@1\r
476         mov     di,1\r
477 @@2:    xor     dx,dx\r
478         mov     ax,di\r
479         mul     bx\r
480         push    edi\r
481         shl     di,1\r
482         mov     word ptr leftC[edi],ax\r
483         inc     ax\r
484         mov     word ptr rghtC[edi],ax\r
485         pop     edi\r
486         inc     di\r
487         cmp     di,MAXCHAR\r
488         jbe     @@2\r
489 end;\r
490 \r
491 procedure UpdateFreq(a,b: Word); assembler;\r
492 asm\r
493         xor     ecx,ecx\r
494         xor     edi,edi\r
495 @@1:    mov     di,a\r
496         shl     di,1\r
497         mov     bx,word ptr freq[edi]\r
498         mov     di,b\r
499         shl     di,1\r
500         add     bx,word ptr freq[edi]\r
501         mov     di,a\r
502         shl     di,1\r
503         mov     dx,word ptr dad[edi]\r
504         mov     di,dx\r
505         shl     di,1\r
506         mov     word ptr freq[edi],bx\r
507         mov     a,dx\r
508         cmp     a,ROOT\r
509         jz      @@3\r
510         mov     di,a\r
511         shl     di,1\r
512         mov     di,word ptr dad[edi]\r
513         mov     ax,di\r
514         shl     di,1\r
515         mov     bx,word ptr leftC[edi]\r
516         cmp     a,bx\r
517         jnz     @@2\r
518         mov     di,ax\r
519         shl     di,1\r
520         mov     bx,word ptr rghtC[edi]\r
521         mov     b,bx\r
522         jmp     @@3\r
523 @@2:    mov     di,ax\r
524         shl     di,1\r
525         mov     bx,word ptr leftC[edi]\r
526         mov     b,bx\r
527 @@3:    cmp     a,ROOT\r
528         jnz     @@1\r
529         mov     bx,MAXFREQ\r
530         mov     di,ROOT\r
531         shl     di,1\r
532         cmp     word ptr freq[edi],bx\r
533         jnz     @@5\r
534         lea     esi,[freq]\r
535         lea     edi,[freq]\r
536         mov     cx,TWICEMAX\r
537         movsw\r
538 @@4:    lodsw\r
539         shr     ax,1\r
540         stosw\r
541         loop    @@4\r
542 @@5:\r
543 end;\r
544 \r
545 procedure UpdateModel(code: Word); assembler;\r
546 asm\r
547         xor     ecx,ecx\r
548         xor     edi,edi\r
549         mov     bx,code\r
550         add     bx,SUCCMAX\r
551         mov     di,bx\r
552         shl     di,1\r
553         mov     ax,di\r
554         mov     cx,word ptr freq[edi]\r
555         inc     cx\r
556         mov     word ptr freq[edi],cx\r
557         mov     di,ax\r
558         mov     cx,ROOT\r
559         cmp     word ptr dad[edi],cx\r
560         jz      @@10\r
561         mov     dx,word ptr dad[edi]\r
562         push    edi\r
563         lea     edi,[leftC]\r
564         mov     cx,dx\r
565         shl     cx,1\r
566         add     edi,ecx\r
567         mov     si,word ptr [edi]\r
568         pop     edi\r
569         cmp     si,bx\r
570         jnz     @@1\r
571         mov     di,dx\r
572         shl     di,1\r
573         mov     si,word ptr rghtC[edi]\r
574 @@1:    push    ebx\r
575         push    edx\r
576         push    ebx\r
577         push    esi\r
578         call    UpdateFreq\r
579         pop     edx\r
580         pop     ebx\r
581 @@2:    xor     edi,edi\r
582         mov     di,dx\r
583         shl     di,1\r
584         mov     ax,word ptr dad[edi]\r
585         mov     di,ax\r
586         shl     di,1\r
587         mov     cx,di\r
588         cmp     word ptr leftC[edi],dx\r
589         jnz     @@3\r
590         mov     di,cx\r
591         mov     si,word ptr rghtC[edi]\r
592         jmp     @@4\r
593 @@3:    mov     si,word ptr leftC[edi]\r
594 @@4:    xor     edi,edi\r
595         mov     di,bx\r
596         shl     di,1\r
597         push    eax\r
598         mov     ax,word ptr freq[edi]\r
599         mov     di,si\r
600         shl     di,1\r
601         mov     cx,ax\r
602         pop     eax\r
603         cmp     cx,word ptr freq[edi]\r
604         jbe     @@9\r
605         mov     di,ax\r
606         shl     di,1\r
607         mov     cx,di\r
608         cmp     word ptr leftC[edi],dx\r
609         jnz     @@5\r
610         mov     di,cx\r
611         mov     word ptr rghtC[edi],bx\r
612         jmp     @@6\r
613 @@5:    xor     edi,edi\r
614         mov     di,cx\r
615         mov     word ptr leftC[edi],bx\r
616 @@6:    lea     edi,[leftC]\r
617         xor     ecx,ecx\r
618         mov     cx,dx\r
619         shl     cx,1\r
620         add     edi,ecx\r
621         cmp     word ptr [edi],bx\r
622         jnz     @@7\r
623         mov     word ptr [edi],si\r
624         xor     edi,edi\r
625         mov     di,cx\r
626         mov     cx,word ptr rghtC[edi]\r
627         jmp     @@8\r
628 @@7:    xor     edi,edi\r
629         mov     di,cx\r
630         mov     word ptr rghtC[edi],si\r
631         mov     cx,word ptr leftC[edi]\r
632 @@8:    xor     edi,edi\r
633         mov     di,si\r
634         shl     di,1\r
635         mov     word ptr dad[edi],dx\r
636         mov     di,bx\r
637         shl     di,1\r
638         mov     word ptr dad[edi],ax\r
639         push    esi\r
640         push    esi\r
641         push    ecx\r
642         call    UpdateFreq\r
643         pop     ebx\r
644 @@9:    xor     edi,edi\r
645         mov     di,bx\r
646         shl     di,1\r
647         mov     bx,word ptr dad[edi]\r
648         mov     di,bx\r
649         shl     di,1\r
650         mov     dx,word ptr dad[edi]\r
651         cmp     dx,ROOT\r
652         jnz     @@2\r
653 @@10:\r
654 end;\r
655 \r
656 function InputCode(bits: Word): Word; assembler;\r
657 asm\r
658         xor     bx,bx\r
659         xor     ecx,ecx\r
660         mov     cx,1\r
661 @@1:    cmp     ibitCount,0\r
662         jnz     @@3\r
663         cmp     ibufCount,MAXBUF\r
664         jnz     @@2\r
665         mov     ax,input_size\r
666         mov     ibufCount,0\r
667 @@2:    mov     edi,input_ptr\r
668         xor     edx,edx\r
669         mov     dx,ibufCount\r
670         shl     dx,1\r
671         add     edi,edx\r
672         mov     ax,[edi]\r
673         mov     ibitBuffer,ax\r
674         inc     ibufCount\r
675         mov     ibitCount,15\r
676         jmp     @@4\r
677 @@3:    dec     ibitCount\r
678 @@4:    cmp     ibitBuffer,7fffh\r
679         jbe     @@5\r
680         xor     edi,edi\r
681         mov     di,cx\r
682         dec     di\r
683         shl     di,1\r
684         mov     ax,word ptr BitValue[edi]\r
685         or      bx,ax\r
686 @@5:    shl     ibitBuffer,1\r
687         inc     cx\r
688         cmp     cx,bits\r
689         jbe     @@1\r
690         mov     ax,bx\r
691 end;\r
692 \r
693 function Uncompress: Word; assembler;\r
694 asm\r
695         xor     eax,eax\r
696         xor     ebx,ebx\r
697         mov     bx,1\r
698         mov     dx,ibitCount\r
699         mov     cx,ibitBuffer\r
700         mov     ax,ibufCount\r
701 @@1:    or      dx,dx\r
702         jnz     @@3\r
703         cmp     ax,MAXBUF\r
704         jnz     @@2\r
705         mov     ax,input_size\r
706         xor     ax,ax\r
707 @@2:    shl     ax,1\r
708         mov     edi,input_ptr\r
709         add     edi,eax\r
710         shr     ax,1\r
711         mov     cx,[edi]\r
712         inc     ax\r
713         mov     dx,15\r
714         jmp     @@4\r
715 @@3:    dec     dx\r
716 @@4:    cmp     cx,7fffh\r
717         jbe     @@5\r
718         mov     edi,ebx\r
719         shl     edi,1\r
720         mov     bx,word ptr rghtC[edi]\r
721         jmp     @@6\r
722 @@5:    mov     edi,ebx\r
723         shl     edi,1\r
724         mov     bx,word ptr leftC[edi]\r
725 @@6:    shl     cx,1\r
726         cmp     bx,MAXCHAR\r
727         jle     @@1\r
728         sub     bx,SUCCMAX\r
729         mov     ibitCount,dx\r
730         mov     ibitBuffer,cx\r
731         mov     ibufCount,ax\r
732         push    ebx\r
733         push    ebx\r
734         call    UpdateModel\r
735         pop     eax\r
736 end;\r
737 \r
738 procedure SIXPACK_decode; assembler;\r
739 asm\r
740         mov     ibitCount,0\r
741         mov     ibitBuffer,0\r
742         mov     obufCount,0\r
743         mov     ibufCount,0\r
744         xor     ebx,ebx\r
745         xor     ecx,ecx\r
746         mov     count,0\r
747         call    InitTree\r
748         call    Uncompress\r
749 @@1:    cmp     ax,TERMINATE\r
750         jz      @@10\r
751         cmp     ax,256\r
752         jae     @@3\r
753         mov     edi,output_ptr\r
754         push    ebx\r
755         mov     bx,obufCount\r
756         add     edi,ebx\r
757         pop     ebx\r
758         stosb\r
759         inc     obufCount\r
760         mov     bx,MAXBUF\r
761         cmp     obufCount,bx\r
762         jnz     @@2\r
763         mov     output_size,bx\r
764         mov     obufCount,0\r
765 @@2:    mov     edi,work_ptr\r
766         push    ebx\r
767         mov     bx,count\r
768         add     edi,ebx\r
769         pop     ebx\r
770         stosb\r
771         inc     count\r
772         cmp     count,MAXSIZE\r
773         jnz     @@9\r
774         mov     count,0\r
775         jmp     @@9\r
776 @@3:    sub     ax,FIRSTCODE\r
777         mov     cx,ax\r
778         xor     dx,dx\r
779         mov     bx,CODESPERRANGE\r
780         div     bx\r
781         mov     index,ax\r
782         xor     dx,dx\r
783         mul     bx\r
784         mov     bx,cx\r
785         add     bx,MINCOPY\r
786         sub     bx,ax\r
787         mov     si,bx\r
788         xor     edi,edi\r
789         mov     di,index\r
790         shl     di,1\r
791         mov     bx,word ptr CopyBits[edi]\r
792         push    ebx\r
793         call    InputCode\r
794         add     ax,si\r
795         xor     edi,edi\r
796         mov     di,index\r
797         shl     di,1\r
798         add     ax,word ptr CopyMin[edi]\r
799         mov     bx,count\r
800         mov     dx,bx\r
801         sub     dx,ax\r
802         mov     cx,dx\r
803         cmp     count,ax\r
804         jae     @@4\r
805         add     cx,MAXSIZE\r
806 @@4:    xor     dx,dx\r
807 @@5:    mov     edi,work_ptr\r
808         add     edi,ecx\r
809         mov     al,byte ptr [edi]\r
810         mov     edi,output_ptr\r
811         push    ebx\r
812         mov     bx,obufCount\r
813         add     edi,ebx\r
814         pop     ebx\r
815         mov     byte ptr [edi],al\r
816         inc     obufCount\r
817         mov     ax,MAXBUF\r
818         cmp     obufCount,ax\r
819         jnz     @@6\r
820         mov     output_size,ax\r
821         mov     obufCount,0\r
822 @@6:    mov     edi,work_ptr\r
823         push    edi\r
824         add     edi,ecx\r
825         mov     al,byte ptr [edi]\r
826         pop     edi\r
827         add     edi,ebx\r
828         mov     byte ptr [edi],al\r
829         inc     bx\r
830         cmp     bx,MAXSIZE\r
831         jnz     @@7\r
832         xor     bx,bx\r
833 @@7:    inc     cx\r
834         cmp     cx,MAXSIZE\r
835         jnz     @@8\r
836         xor     cx,cx\r
837 @@8:    inc     dx\r
838         cmp     dx,si\r
839         jb      @@5\r
840         mov     ax,si\r
841         add     count,ax\r
842         cmp     count,MAXSIZE\r
843         jb      @@9\r
844         sub     count,MAXSIZE\r
845 @@9:    call    Uncompress\r
846         jmp     @@1\r
847 @@10:   mov     bx,obufCount\r
848         mov     output_size,bx\r
849 end;\r
850 \r
851 function SIXPACK_decompress(var source,dest; size: Word): Word;\r
852 begin\r
853   input_ptr := @source;\r
854   output_ptr := @dest;\r
855   work_ptr := @work_mem;\r
856   input_size := size;\r
857   SIXPACK_decode;\r
858   SIXPACK_decompress := output_size;\r
859 end;\r
860 \r
861 function APACK_decompress(var source,dest): Longint; assembler;\r
862 asm\r
863         mov     esi,[source]\r
864         mov     edi,[dest]\r
865         cld\r
866         mov     dl,80h\r
867 @@1:    movsb\r
868 @@2:    add     dl,dl\r
869         jnz     @@3\r
870         mov     dl,[esi]\r
871         inc     esi\r
872         adc     dl,dl\r
873 @@3:    jnc     @@1\r
874         xor     ecx,ecx\r
875         add     dl,dl\r
876         jnz     @@4\r
877         mov     dl,[esi]\r
878         inc     esi\r
879         adc     dl,dl\r
880 @@4:    jnc     @@8\r
881         xor     eax,eax\r
882         add     dl,dl\r
883         jnz     @@5\r
884         mov     dl,[esi]\r
885         inc     esi\r
886         adc     dl,dl\r
887 @@5:    jnc     @@15\r
888         inc     ecx\r
889         mov     al,10h\r
890 @@6:    add     dl,dl\r
891         jnz     @@7\r
892         mov     dl,[esi]\r
893         inc     esi\r
894         adc     dl,dl\r
895 @@7:    adc     al,al\r
896         jnc     @@6\r
897         jnz     @@24\r
898         stosb\r
899         jmp     @@2\r
900 @@8:    inc     ecx\r
901 @@9:    add     dl,dl\r
902         jnz     @@10\r
903         mov     dl,[esi]\r
904         inc     esi\r
905         adc     dl,dl\r
906 @@10:   adc     ecx,ecx\r
907         add     dl,dl\r
908         jnz     @@11\r
909         mov     dl,[esi]\r
910         inc     esi\r
911         adc     dl,dl\r
912 @@11:   jc      @@9\r
913         dec     ecx\r
914         loop    @@16\r
915         xor     ecx,ecx\r
916         inc     ecx\r
917 @@12:   add     dl,dl\r
918         jnz     @@13\r
919         mov     dl,[esi]\r
920         inc     esi\r
921         adc     dl,dl\r
922 @@13:   adc     ecx,ecx\r
923         add     dl,dl\r
924         jnz     @@14\r
925         mov     dl,[esi]\r
926         inc     esi\r
927         adc     dl,dl\r
928 @@14:   jc      @@12\r
929         jmp     @@23\r
930 @@15:   lodsb\r
931         shr     eax,1\r
932         jz      @@25\r
933         adc     ecx,ecx\r
934         jmp     @@20\r
935 @@16:   xchg    eax,ecx\r
936         dec     eax\r
937         shl     eax,8\r
938         lodsb\r
939         xor     ecx,ecx\r
940         inc     ecx\r
941 @@17:   add     dl,dl\r
942         jnz     @@18\r
943         mov     dl,[esi]\r
944         inc     esi\r
945         adc     dl,dl\r
946 @@18:   adc     ecx,ecx\r
947         add     dl,dl\r
948         jnz     @@19\r
949         mov     dl,[esi]\r
950         inc     esi\r
951         adc     dl,dl\r
952 @@19:   jc      @@17\r
953         cmp     eax,32000\r
954         jae     @@20\r
955         cmp     ah,5\r
956         jae     @@21\r
957         cmp     eax,7fh\r
958         ja      @@22\r
959 @@20:   inc     ecx\r
960 @@21:   inc     ecx\r
961 @@22:   xchg    eax,@dummy\r
962 @@23:   mov     eax,@dummy\r
963 @@24:   push    esi\r
964         mov     esi,edi\r
965         sub     esi,eax\r
966         rep     movsb\r
967         pop     esi\r
968         jmp     @@2\r
969 @@25:   sub     edi,[dest]\r
970         mov     eax,edi\r
971         jmp     @ret\r
972 \r
973 @dummy: dd 0\r
974 @ret:\r
975 end;\r
976 \r
977 end.\r