   
            OPT ALINK

            XREF  _decompressMM

   ; open everything

       movea.l a0,a4          preserve command line
       clr.b   -1(a4,d0.w)
       lea     dosname,a1
       move.l  4,A6
       jsr     -408(a6)       OldOpenLibrary
       move.l  d0,_DOSBase

       move.l  _DOSBase,A6
       jsr     -60(a6)        Output
       move.l  d0,stdout

       move.l  a4,d1          input filename
       move.l  #1005,d2       MODE_OLDFILE
       move.l  _DOSBase,A6
       jsr     -30(a6)        Open
       move.l  d0,fi          input file handle 

       move.l  #fname,d1      'ram:q'
       move.l  #1006,d2       MODE_NEWFILE
       move.l  _DOSBase,A6
       jsr     -30(a6)        Open
       move.l  d0,fo          output file-handle

   ; read the input header, check that file is compressed, set up input and
   ;  output memory buffers.

       move.l  #$E,d3
       move.l  #hbuf,d2
       move.l  fi,d1
       move.l  _DOSBase,a6
       jsr     -42(a6)        Read 'LH,compressed length,decompressed length'
       move.l  #hbuf,a0
       move.l  2(a0),ulength
       move.l  6(a0),length
       add.l   #$10,length    Include 16 bytes of header in buffer
           
       cmpi.w  #$4C48,(a0)    check that file is compressed
       beq     compressed
       move.l  #$14,d3
       move.l  #notcomp,d2
       move.l  stdout,d1
       move.l  _DOSBase,a6
       jsr     -48(a6) 
       bra     close

compressed
       moveq.l #-1,d3
       moveq.l #2,d2
       move.l  fi,d1
       move.l  _DOSBase,a6
       jsr     -66(a6)        Seek to position in file after 'LH' 

       move.l  length,d0     
       move.l  #$10001,d1     MEMF_PUBLIC|MEMF_CLEAR
       move.l  4,A6
       jsr     -198(a6)       AllocMem for input buffer
       move.l  d0,ibuf

       move.l  length,d3
       move.l  ibuf,d2
       move.l  fi,d1
       move.l  _DOSBase,A6
       jsr     -42(a6)        Read compressed file into buffer (less 'LH')

       move.l  ulength,d0
       move.l  #$10001,d1     MEMF_PUBLIC|MEMF_CLEAR
       move.l  4,A6
       jsr     -198(a6)       AllocMem for output buffer
       move.l  d0,obuf

  ; decompress and check for error

       move.l  obuf,-(a7)
       move.l  ibuf,-(a7)
       jsr     _decompressMM
       addq.l  #8,a7
       cmpi    #2,d0
       bne     memOK
       move.l  #$14,d3
       move.l  #baddecomp,d2
       move.l  stdout,d1
       move.l  _DOSBase,a6
       jsr     -48(a6)        Write
       jmp     close

  ; make use of decompressed output

memOK
       move.l  ulength,d3
       move.l  obuf,d2        Decoded output
       move.l  fo,d1
       move.l  _DOSBase,a6
       jsr     -48(a6)        Write

       ;(you can replace this write with your own application code)

  ; clean up
 
close
       tst.l   fi
       beq.b   closefo
       move.l  fi,d1
       move.l  _DOSBase,A6
       jsr     -36(a6)        Close
closefo
       tst.l   fo
       beq.b   freeobuf
       move.l  fo,d1
       move.l  _DOSBase,A6
       jsr     -36(a6)        Close
freeobuf
       tst.l   obuf
       beq.b   freeibuf
       move.l  obuf,a1
       move.l  ulength,d0
       move.l  4,A6
       jsr     -210(a6)       FreeMem 
freeibuf
       tst.l   ibuf
       beq.b   quit
       move.l  ibuf,a1
       move.l  length,d0
       move.l  4,A6
       jsr     -210(a6)       FreeMem 
quit
       rts
       
_DOSBase    dc.l  0
fi          dc.l  0
fo          dc.l  0
stdout      dc.l  0
hbuf        dc.l  0,0,0   temp store for header
ibuf        dc.l  0   input buffer
obuf        dc.l  0   output buffer 
length      dc.l  0   compressed length
ulength     dc.l  0   decompressed length
dosname     dc.b  'dos.library',0
fname       dc.b  'ram:q',0
notcomp     dc.b  'file not compressed',10,0
nomem       dc.b  'insufficient memory',10,0
baddecomp   dc.b  'decompression failed',10,0


















