?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. ;       Copyright (c) 2003-2004 Arjan Bakker
  2. ;      
  3. ;       Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. ;       this software and associated documentation files (the "Software"), to deal in
  5. ;       the Software without restriction, including without limitation the rights to
  6. ;       use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. ;       the Software, and to permit persons to whom the Software is furnished to do so,
  8. ;       subject to the following conditions:
  9. ;      
  10. ;       The above copyright notice and this permission notice shall be included in all
  11. ;       copies or substantial portions of the Software.
  12. ;      
  13. ;       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ;       IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. ;       FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. ;       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. ;       IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. ;       CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19.  
  20.                 MODULE  bitbuster
  21.  
  22. ; File: bitbuster
  23. ;
  24. ;       The bitbuster module gives you depack support for data packed
  25. ;       using the bitbuster algorithm.
  26. ;
  27. ; FUNCTION NAMES:
  28. ;       The functions <depack> and <depack_raw> are MODULE local, which means you
  29. ;       have to add the prefix "bitbuster." to its name. This is to prevent possible
  30. ;       name clashes with functions from other libraries you might be using.
  31. ;       However, if you define <MAKE_BITBUSTER_GLOBAL> then these functions will be
  32. ;       available without the "bitbuster." prefix as well.
  33. ;
  34. ; COPYRIGHT & CREDITS:
  35. ;       This module has been released by Arjan Bakker under the MIT License;
  36. ;       please see the top of the source file(s) for the full copyright statement.
  37.        
  38. ; DEFINE:       MAKE_BITBUSTER_GLOBAL
  39. ;       Defining this will make all public functions that are normally only
  40. ;       available with the "bitbuster." prefix to also be available without
  41. ;       this prefix. See the introduction of <bitbuster> for more information.
  42.  
  43. ; DEFINE:       BITBUSTER_OPTIMIZE_SPEED
  44. ;       Defining this will optimize the bitbuster depacker for speed, at the cost of
  45. ;       bigger code.
  46.  
  47.  
  48.                 IFDEF   BITBUSTER_OPTIMIZE_SPEED
  49. ; use macro's for getbit routines when optimizing bitbuster depacker for speed
  50. ; (inlined code, no overhead by calls)
  51.                
  52.                 ; macro to get a bit from the bitstream
  53.                 ; carry if bit is set, nocarry if bit is clear
  54.                 ; must be entered with second registerset switched in!
  55.                 MACRO   GET_BIT_FROM_BITSTREAM
  56.                 add     a,a             ; shift out new bit
  57.                 jp      nz,.done        ; if remaining value isn't zere, we're done
  58.  
  59.                 ld      a,(hl)          ; get 8 bits from bitstream
  60.                 inc     hl              ; increase source data address
  61.  
  62.                 rla                     ; (bit 0 will be set!!!!)
  63. .done:          
  64.                 ENDM
  65.  
  66.                 ENDIF                   ; IFDEF BITBUSTER_OPTIMIZE_SPEED
  67.        
  68.        
  69.                 IFNDEF  BITBUSTER_OPTIMIZE_SPEED
  70. ; use calls for getbit code when not optimizing for speed
  71.  
  72.                 ; macro to get a bit from the bitstream
  73.                 ; carry if bit is set, nocarry if bit is clear
  74.                 ; must be entered with second registerset switched in!
  75.                 MACRO   GET_BIT_FROM_BITSTREAM
  76.                 call    get_bit_from_bitstream
  77.                 ENDM
  78.  
  79.                 ENDIF                   ; IFNDEF BITBUSTER_OPTIMIZE_SPEED  
  80.        
  81.                        
  82. ; FUNCTION:     depack
  83. ;       Depack a blob of data that was packed with Bitbuster.
  84. ;
  85. ; ENTRY:
  86. ;       HL - Address of packed data
  87. ;       DE - Address to depack to
  88. ;
  89. ; EXIT:
  90. ;       HL - Size of depacked data
  91. ;       A  - Number of blocks left to decompress
  92. ;       FZ - Last block has been decompressed
  93. ;       FNZ- Last block hasn't been decompressed
  94. ;
  95. ; MODIFIES:
  96. ;       #AF, BC, BC', DE, DE', HL, HL'#
  97. ;
  98. depack:         ;EXPORT bitbuster.depack
  99.                 ;IFDEF  MAKE_BITBUSTER_GLOBAL
  100. @depack:        ;EXPORT depack         
  101.                 ;ENDIF
  102.                
  103.                 ld      a,(block_count)
  104.                 or      a
  105.                 jr      nz,depack_continue      ; continue depacking a block if more blocks left
  106.                
  107.                 ld      a,(hl)
  108.                 ld      (block_count),a         ; store block count
  109.                
  110.                 inc     hl                      ; move to size of first block
  111.                 ld      (block_start),hl        ; store starting address of block
  112.                
  113. depack_continue:
  114.                 ld      hl,(block_start)        ; load starting address of block
  115.                 inc     hl
  116.                 inc     hl                      ; move over block size
  117.                
  118.                 push    de
  119.                 call    depack_raw     
  120.                
  121.                 ld      (block_start),hl        ; store starting address of next chunk 
  122.                 pop     hl
  123.                 ex      de,hl
  124.                 OR      A,A
  125.                 SBC     HL,DE
  126.                
  127.                 ld      a,(block_count)
  128.                 dec     a
  129.                 ld      (block_count),a         ; decrease number of blocks to decompress
  130.                 ret
  131.                
  132. ; FUNCTION:     depack_raw
  133. ;       Depack data that was packed with Bitbuster.
  134. ;       Decompresses the RAW data, i.e. the data that is storead after the block
  135. ;       count and block size!
  136. ;
  137. ; ENTRY:
  138. ;       HL - Address of packed data
  139. ;       DE - Address to depack to
  140. ;
  141. ; EXIT:
  142. ;       HL - Address of first byte after compressed data
  143. ;       DE - Address of first byte after decompressed data
  144. ;
  145. ; MODIFIES:
  146. ;       #AF, BC, BC', DE, DE', HL, HL'#
  147. ;
  148. depack_raw:     ;EXPORT bitbuster.depack_raw
  149.                 ;IFDEF  MAKE_BITBUSTER_GLOBAL
  150. @depack_raw:    ;EXPORT depack_raw             
  151.                 ;ENDIF
  152.                
  153.                 ld      a,128
  154.  
  155. depack_loop:    GET_BIT_FROM_BITSTREAM          ; get compression type bit
  156.                 jp      c,output_compressed     ; if set, we got lz77 compression
  157.                 ldi                             ; copy byte from compressed data to destination (literal byte)
  158.        
  159.                 IFDEF   BITBUSTER_OPTIMIZE_SPEED
  160.                 GET_BIT_FROM_BITSTREAM          ; get compression type bit
  161.                 jp      c,output_compressed     ; if set, we got lz77 compression
  162.                 ldi                             ; copy byte from compressed data to destination (literal byte)
  163.                 GET_BIT_FROM_BITSTREAM          ; get compression type bit
  164.                 jp      c,output_compressed     ; if set, we got lz77 compression
  165.                 ldi                             ; copy byte from compressed data to destination (literal byte)
  166.                 ENDIF
  167.  
  168.                 jp      depack_loop
  169.        
  170.  
  171. ;handle compressed data
  172. output_compressed:
  173. ; calculate length value
  174.                 ld      bc,1
  175. get_gamma_value:
  176.                 GET_BIT_FROM_BITSTREAM          ; get more bits
  177.                 jr      nc,get_gamma_value_end
  178.                 GET_BIT_FROM_BITSTREAM          ; get next bit of value from bitstream
  179.                 rl      c
  180.                 rl      b
  181.                 jp      nc,get_gamma_value      ; repeat unless overflow occurred (=end of block marker)
  182.                 ret
  183.        
  184. get_gamma_value_end:
  185.                 inc     bc                      ; length was stored as length-2 so correct this
  186.  
  187.                 ld      (source_length),bc
  188.  
  189.                 ld      c,(hl)                  ; get lowest 7 bits of offset, plus offset extension bit
  190.                 inc     hl                      ; to next byte in compressed data
  191.                
  192.                 ld      b,0
  193.                 bit     7,c
  194.                 jr      z,output_match1         ; no need to get extra bits if carry not set
  195.        
  196.                 GET_BIT_FROM_BITSTREAM          ; get offset bit 10
  197.                 rl      b
  198.                 GET_BIT_FROM_BITSTREAM          ; get offset bit 9
  199.                 rl      b
  200.                 GET_BIT_FROM_BITSTREAM          ; get offset bit 8
  201.                 rl      b
  202.                 GET_BIT_FROM_BITSTREAM          ; get offset bit 7
  203.        
  204.                 jp      c,output_match2         ; since extension mark already makes bit 7 set
  205.                 res     7,c                     ; only clear it if the bit should be cleared
  206. output_match1:  scf
  207. output_match2:  push    hl                      ; address compressed data on stack
  208.  
  209.                 ld      h,d
  210.                 ld      l,e                     ; destination address in HL...
  211.                 sbc     hl,bc                   ; calculate source address
  212.                        
  213.                 ld      bc,(source_length)
  214.        
  215.                 ldir                            ; transfer data
  216.        
  217.                 pop     hl                      ; address compressed data back from stack
  218.                
  219.                 IFDEF   BITBUSTER_OPTIMIZE_SPEED
  220.                 GET_BIT_FROM_BITSTREAM          ; get compression type bit
  221.                 jp      c,output_compressed     ; if set, we got lz77 compression
  222.                 ldi                             ; copy byte from compressed data to destination (literal byte)
  223.                 GET_BIT_FROM_BITSTREAM          ; get compression type bit
  224.                 jp      c,output_compressed     ; if set, we got lz77 compression
  225.                 ldi                             ; copy byte from compressed data to destination (literal byte)
  226.                 ENDIF
  227.        
  228.                 jp      depack_loop
  229.        
  230.        
  231.                 IFNDEF  BITBUSTER_OPTIMIZE_SPEED
  232.  
  233. ; get a bit from the bitstream
  234. ; carry if bit is set, nocarry if bit is clear
  235. ; must be entered with regular registerset switched in!
  236. get_bit_from_bitstream:
  237.                 add     a,a             ; shift out new bit
  238.                 ret     nz              ; if remaining value isn't zere, we're done
  239.                                          
  240.                 ld      a,(hl)          ; get 8 bits from bitstream
  241.                 inc     hl              ; increase source data address
  242.        
  243.                 rla                     ;(bit 0 will be set!!!!)
  244.                 ret
  245.                 ENDIF                   ; IFNDEF BITBUSTER_OPTIMIZE_SPEED
  246.                
  247. source_length:  dw      0               ; length of source string
  248. block_count:    db      0               ; number of blocks to decompress
  249. block_start:    dw      0               ; starting address of next block
  250.        
  251.        
  252.                 ENDMODULE
  253.