| 1 |
;;;; Deflate --- RFC 1951 Deflate Decompression |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2000-2010 PMSF IT Consulting Pierre R. Mai. |
| 4 |
;;;; |
| 5 |
;;;; Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
;;;; a copy of this software and associated documentation files (the |
| 7 |
;;;; "Software"), to deal in the Software without restriction, including |
| 8 |
;;;; without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
;;;; distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
;;;; permit persons to whom the Software is furnished to do so, subject to |
| 11 |
;;;; the following conditions: |
| 12 |
;;;; |
| 13 |
;;;; The above copyright notice and this permission notice shall be |
| 14 |
;;;; included in all copies or substantial portions of the Software. |
| 15 |
;;;; |
| 16 |
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
;;;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
;;;; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
;;;; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 |
;;;; OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
;;;; |
| 24 |
;;;; Except as contained in this notice, the name of the author shall |
| 25 |
;;;; not be used in advertising or otherwise to promote the sale, use or |
| 26 |
;;;; other dealings in this Software without prior written authorization |
| 27 |
;;;; from the author. |
| 28 |
;;;; |
| 29 |
;;;; $Id$ |
| 30 |
|
| 31 |
(cl:defpackage "DEFLATE" |
| 32 |
(:use "COMMON-LISP") |
| 33 |
(:export #:decompression-error #:deflate-decompression-error |
| 34 |
#:zlib-decompression-error #:gzip-decompression-error |
| 35 |
#:inflate-stream |
| 36 |
#:inflate-zlib-stream #:parse-zlib-header #:parse-zlib-footer |
| 37 |
#:inflate-gzip-stream #:parse-gzip-header #:parse-gzip-footer |
| 38 |
#:gunzip)) |
| 39 |
|
| 40 |
(cl:in-package "DEFLATE") |
| 41 |
|
| 42 |
(declaim (optimize (safety 0) (speed 3))) |
| 43 |
|
| 44 |
;;;; %File Description: |
| 45 |
;;;; |
| 46 |
;;;; This file contains routines implementing the RFC 1951 Deflate |
| 47 |
;;;; Compression and/or Decompression method, as used by e.g. gzip and |
| 48 |
;;;; other compression and archiving tools and protocols. It also |
| 49 |
;;;; implements handling routines for zlib-style (RFC 1950) and |
| 50 |
;;;; gzip-style (RFC 1952) wrappers around raw Deflate streams. |
| 51 |
;;;; |
| 52 |
;;;; The main entry points are the functions inflate-stream, and its |
| 53 |
;;;; cousins inflate-zlib-stream and inflate-gzip-stream, which take |
| 54 |
;;;; an input-stream and an output-stream as their arguments, and |
| 55 |
;;;; inflate the RFC 1951, RFC 1950 or RFC 1952-style deflate formats |
| 56 |
;;;; from the input-stream to the output-stream. |
| 57 |
;;;; |
| 58 |
|
| 59 |
;;; |
| 60 |
;;; Conditions |
| 61 |
;;; |
| 62 |
|
| 63 |
(define-condition decompression-error (simple-error) |
| 64 |
()) |
| 65 |
|
| 66 |
(define-condition deflate-decompression-error (decompression-error) |
| 67 |
() |
| 68 |
(:report |
| 69 |
(lambda (c s) |
| 70 |
(with-standard-io-syntax |
| 71 |
(let ((*print-readably* nil)) |
| 72 |
(format s |
| 73 |
"Error detected during deflate decompression: ~?" |
| 74 |
(simple-condition-format-control c) |
| 75 |
(simple-condition-format-arguments c))))))) |
| 76 |
|
| 77 |
(define-condition zlib-decompression-error (decompression-error) |
| 78 |
() |
| 79 |
(:report |
| 80 |
(lambda (c s) |
| 81 |
(with-standard-io-syntax |
| 82 |
(let ((*print-readably* nil)) |
| 83 |
(format s |
| 84 |
"Error detected during zlib decompression: ~?" |
| 85 |
(simple-condition-format-control c) |
| 86 |
(simple-condition-format-arguments c))))))) |
| 87 |
|
| 88 |
(define-condition gzip-decompression-error (decompression-error) |
| 89 |
() |
| 90 |
(:report |
| 91 |
(lambda (c s) |
| 92 |
(with-standard-io-syntax |
| 93 |
(let ((*print-readably* nil)) |
| 94 |
(format s |
| 95 |
"Error detected during zlib decompression: ~?" |
| 96 |
(simple-condition-format-control c) |
| 97 |
(simple-condition-format-arguments c))))))) |
| 98 |
|
| 99 |
;;; |
| 100 |
;;; Adler-32 Checksums |
| 101 |
;;; |
| 102 |
|
| 103 |
(defconstant +adler-32-start-value+ 1 |
| 104 |
"Start value for Adler-32 checksums as per RFC 1950.") |
| 105 |
|
| 106 |
(defconstant +adler-32-base+ 65521 |
| 107 |
"Base value for Adler-32 checksums as per RFC 1950.") |
| 108 |
|
| 109 |
(declaim (ftype |
| 110 |
(function ((unsigned-byte 32) (simple-array (unsigned-byte 8) (*)) fixnum) |
| 111 |
(unsigned-byte 32)) |
| 112 |
update-adler32-checksum)) |
| 113 |
(defun update-adler32-checksum (crc buffer end) |
| 114 |
(declare (type (unsigned-byte 32) crc) |
| 115 |
(type (simple-array (unsigned-byte 8) (*)) buffer) |
| 116 |
(type fixnum end) |
| 117 |
(optimize (speed 3) (debug 0) (space 0) (safety 0)) |
| 118 |
#+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note)) |
| 119 |
(let ((s1 (ldb (byte 16 0) crc)) |
| 120 |
(s2 (ldb (byte 16 16) crc))) |
| 121 |
(declare (type (unsigned-byte 32) s1 s2)) |
| 122 |
(dotimes (i end) |
| 123 |
(declare (type fixnum i)) |
| 124 |
(setq s1 (mod (+ s1 (aref buffer i)) +adler-32-base+) |
| 125 |
s2 (mod (+ s2 s1) +adler-32-base+))) |
| 126 |
(dpb s2 (byte 16 16) s1))) |
| 127 |
|
| 128 |
;;; |
| 129 |
;;; CRC-32 Checksums |
| 130 |
;;; |
| 131 |
|
| 132 |
(defconstant +crc-32-start-value+ 0 |
| 133 |
"Start value for CRC-32 checksums as per RFC 1952.") |
| 134 |
|
| 135 |
(defconstant +crc-32-polynomial+ #xedb88320 |
| 136 |
"CRC-32 Polynomial as per RFC 1952.") |
| 137 |
|
| 138 |
(declaim (ftype #-lispworks (function () (simple-array (unsigned-byte 32) (256))) |
| 139 |
#+lispworks (function () (sys:simple-int32-vector 256)) |
| 140 |
generate-crc32-table)) |
| 141 |
(defun generate-crc32-table () |
| 142 |
(let ((result #-lispworks (make-array 256 :element-type '(unsigned-byte 32)) |
| 143 |
#+lispworks (sys:make-simple-int32-vector 256))) |
| 144 |
(dotimes (i #-lispworks (length result) #+lispworks 256 result) |
| 145 |
(let ((cur i)) |
| 146 |
(dotimes (k 8) |
| 147 |
(setq cur (if (= 1 (logand cur 1)) |
| 148 |
(logxor (ash cur -1) +crc-32-polynomial+) |
| 149 |
(ash cur -1)))) |
| 150 |
#-lispworks (setf (aref result i) cur) |
| 151 |
#+lispworks (setf (sys:int32-aref result i) |
| 152 |
(sys:integer-to-int32 |
| 153 |
(dpb (ldb (byte 32 0) cur) (byte 32 0) |
| 154 |
(if (logbitp 31 cur) -1 0)))))))) |
| 155 |
|
| 156 |
(declaim (ftype |
| 157 |
(function ((unsigned-byte 32) (simple-array (unsigned-byte 8) (*)) fixnum) |
| 158 |
(unsigned-byte 32)) |
| 159 |
update-crc32-checksum)) |
| 160 |
#-lispworks |
| 161 |
(defun update-crc32-checksum (crc buffer end) |
| 162 |
(declare (type (unsigned-byte 32) crc) |
| 163 |
(type (simple-array (unsigned-byte 8) (*)) buffer) |
| 164 |
(type fixnum end) |
| 165 |
(optimize (speed 3) (debug 0) (space 0) (safety 0)) |
| 166 |
#+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note)) |
| 167 |
(let ((table (load-time-value (generate-crc32-table))) |
| 168 |
(cur (logxor crc #xffffffff))) |
| 169 |
(declare (type (simple-array (unsigned-byte 32) (256)) table) |
| 170 |
(type (unsigned-byte 32) cur)) |
| 171 |
(dotimes (i end) |
| 172 |
(declare (type fixnum i)) |
| 173 |
(let ((index (logand #xff (logxor cur (aref buffer i))))) |
| 174 |
(declare (type (unsigned-byte 8) index)) |
| 175 |
(setq cur (logxor (aref table index) (ash cur -8))))) |
| 176 |
(logxor cur #xffffffff))) |
| 177 |
|
| 178 |
#+lispworks |
| 179 |
(defun update-crc32-checksum (crc buffer end) |
| 180 |
(declare (type (unsigned-byte 32) crc) |
| 181 |
(type (simple-array (unsigned-byte 8) (*)) buffer) |
| 182 |
(type fixnum end) |
| 183 |
(optimize (speed 3) (debug 0) (space 0) (safety 0) (float 0))) |
| 184 |
(let ((table (load-time-value (generate-crc32-table))) |
| 185 |
(cur (sys:int32-lognot (sys:integer-to-int32 |
| 186 |
(dpb (ldb (byte 32 0) crc) (byte 32 0) |
| 187 |
(if (logbitp 31 crc) -1 0)))))) |
| 188 |
(declare (type (sys:simple-int32-vector 256) table) |
| 189 |
(type sys:int32 cur)) |
| 190 |
(dotimes (i end) |
| 191 |
(declare (type fixnum i)) |
| 192 |
(let ((index (sys:int32-to-integer |
| 193 |
(sys:int32-logand #xff (sys:int32-logxor cur (aref buffer i)))))) |
| 194 |
(declare (type fixnum index)) |
| 195 |
(setq cur (sys:int32-logxor (sys:int32-aref table index) |
| 196 |
(sys:int32-logand #x00ffffff |
| 197 |
(sys:int32>> cur 8)))))) |
| 198 |
(ldb (byte 32 0) (sys:int32-to-integer (sys:int32-lognot cur))))) |
| 199 |
|
| 200 |
;;; |
| 201 |
;;; Helper Data Structures: Sliding Window Stream |
| 202 |
;;; |
| 203 |
|
| 204 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 205 |
(defconstant +sliding-window-size+ 32768 |
| 206 |
"Size of sliding window for RFC 1951 Deflate compression scheme.")) |
| 207 |
|
| 208 |
(defstruct sliding-window-stream |
| 209 |
(stream nil :type stream :read-only t) |
| 210 |
(buffer (make-array +sliding-window-size+ :element-type '(unsigned-byte 8)) |
| 211 |
:type (simple-array (unsigned-byte 8) (#.+sliding-window-size+)) :read-only t) |
| 212 |
(buffer-end 0 :type fixnum) |
| 213 |
(checksum nil :type symbol :read-only t) |
| 214 |
(checksum-value 0 :type (unsigned-byte 32))) |
| 215 |
|
| 216 |
(declaim (inline sliding-window-stream-write-byte)) |
| 217 |
(defun sliding-window-stream-write-byte (stream byte) |
| 218 |
(declare (type sliding-window-stream stream) (type (unsigned-byte 8) byte) |
| 219 |
#+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note)) |
| 220 |
"Write a single byte to the sliding-window-stream." |
| 221 |
(let ((end (sliding-window-stream-buffer-end stream))) |
| 222 |
(declare (type fixnum end)) |
| 223 |
(unless (< end +sliding-window-size+) |
| 224 |
(write-sequence (sliding-window-stream-buffer stream) |
| 225 |
(sliding-window-stream-stream stream)) |
| 226 |
(case (sliding-window-stream-checksum stream) |
| 227 |
(:adler-32 (setf (sliding-window-stream-checksum-value stream) |
| 228 |
(update-adler32-checksum |
| 229 |
(sliding-window-stream-checksum-value stream) |
| 230 |
(sliding-window-stream-buffer stream) |
| 231 |
+sliding-window-size+))) |
| 232 |
(:crc-32 (setf (sliding-window-stream-checksum-value stream) |
| 233 |
(update-crc32-checksum |
| 234 |
(sliding-window-stream-checksum-value stream) |
| 235 |
(sliding-window-stream-buffer stream) |
| 236 |
+sliding-window-size+)))) |
| 237 |
(setq end 0)) |
| 238 |
(setf (aref (sliding-window-stream-buffer stream) end) byte |
| 239 |
(sliding-window-stream-buffer-end stream) (1+ end)))) |
| 240 |
|
| 241 |
(defun sliding-window-stream-flush (stream) |
| 242 |
(declare (type sliding-window-stream stream)) |
| 243 |
"Flush any remaining buffered bytes from the stream." |
| 244 |
(let ((end (sliding-window-stream-buffer-end stream))) |
| 245 |
(declare (type fixnum end)) |
| 246 |
(unless (zerop end) |
| 247 |
(case (sliding-window-stream-checksum stream) |
| 248 |
(:adler-32 (setf (sliding-window-stream-checksum-value stream) |
| 249 |
(update-adler32-checksum |
| 250 |
(sliding-window-stream-checksum-value stream) |
| 251 |
(sliding-window-stream-buffer stream) |
| 252 |
end))) |
| 253 |
(:crc-32 (setf (sliding-window-stream-checksum-value stream) |
| 254 |
(update-crc32-checksum |
| 255 |
(sliding-window-stream-checksum-value stream) |
| 256 |
(sliding-window-stream-buffer stream) |
| 257 |
end)))) |
| 258 |
(write-sequence (sliding-window-stream-buffer stream) |
| 259 |
(sliding-window-stream-stream stream) |
| 260 |
:end end)))) |
| 261 |
|
| 262 |
(defun sliding-window-stream-copy-bytes (stream distance length) |
| 263 |
(declare (type sliding-window-stream stream) (type fixnum distance length)) |
| 264 |
"Copy a number of bytes from the current sliding window." |
| 265 |
(let* ((end (sliding-window-stream-buffer-end stream)) |
| 266 |
(start (mod (- end distance) +sliding-window-size+)) |
| 267 |
(buffer (sliding-window-stream-buffer stream))) |
| 268 |
(declare (type fixnum end start) |
| 269 |
(type (simple-array (unsigned-byte 8) (#.+sliding-window-size+)) buffer)) |
| 270 |
(dotimes (i length) |
| 271 |
(sliding-window-stream-write-byte |
| 272 |
stream |
| 273 |
(aref buffer (mod (+ start i) +sliding-window-size+)))))) |
| 274 |
|
| 275 |
;;; |
| 276 |
;;; Helper Data Structures: Bit-wise Input Stream |
| 277 |
;;; |
| 278 |
|
| 279 |
(defstruct bit-stream |
| 280 |
(stream nil :type stream :read-only t) |
| 281 |
(next-byte 0 :type fixnum) |
| 282 |
(bits 0 :type (unsigned-byte 29)) |
| 283 |
(bit-count 0 :type (unsigned-byte 8))) |
| 284 |
|
| 285 |
(declaim (inline bit-stream-get-byte)) |
| 286 |
(defun bit-stream-get-byte (stream) |
| 287 |
(declare (type bit-stream stream)) |
| 288 |
"Read another byte from the underlying stream." |
| 289 |
(the (unsigned-byte 8) (read-byte (bit-stream-stream stream)))) |
| 290 |
|
| 291 |
(declaim (inline bit-stream-read-bits)) |
| 292 |
(defun bit-stream-read-bits (stream bits) |
| 293 |
(declare (type bit-stream stream) #-ecl (type (unsigned-byte 8) bits)) |
| 294 |
"Read single or multiple bits from the given bit-stream." |
| 295 |
(loop while (< (bit-stream-bit-count stream) bits) |
| 296 |
do |
| 297 |
;; Fill bits |
| 298 |
(setf (bit-stream-bits stream) |
| 299 |
(logior (bit-stream-bits stream) |
| 300 |
(the (unsigned-byte 29) |
| 301 |
(ash (bit-stream-get-byte stream) |
| 302 |
(bit-stream-bit-count stream)))) |
| 303 |
(bit-stream-bit-count stream) (+ (bit-stream-bit-count stream) 8))) |
| 304 |
;; Return properly masked bits |
| 305 |
(if (= (bit-stream-bit-count stream) bits) |
| 306 |
(prog1 (bit-stream-bits stream) |
| 307 |
(setf (bit-stream-bits stream) 0 |
| 308 |
(bit-stream-bit-count stream) 0)) |
| 309 |
(prog1 (ldb (byte bits 0) (bit-stream-bits stream)) |
| 310 |
(setf (bit-stream-bits stream) (ash (bit-stream-bits stream) (- bits)) |
| 311 |
(bit-stream-bit-count stream) (- (bit-stream-bit-count stream) bits))))) |
| 312 |
|
| 313 |
(declaim (inline bit-stream-copy-block)) |
| 314 |
(defun bit-stream-copy-block (stream out-stream) |
| 315 |
(declare (type bit-stream stream) (type sliding-window-stream out-stream) |
| 316 |
(optimize (speed 3) (safety 0) (space 0) (debug 0))) |
| 317 |
"Copy a given block of bytes directly from the underlying stream." |
| 318 |
;; Skip any remaining unprocessed bits |
| 319 |
(setf (bit-stream-bits stream) 0 |
| 320 |
(bit-stream-bit-count stream) 0) |
| 321 |
;; Get LEN/NLEN and copy bytes |
| 322 |
(let* ((len (logior (bit-stream-get-byte stream) |
| 323 |
(ash (bit-stream-get-byte stream) 8))) |
| 324 |
(nlen (ldb (byte 16 0) |
| 325 |
(lognot (logior (bit-stream-get-byte stream) |
| 326 |
(ash (bit-stream-get-byte stream) 8)))))) |
| 327 |
(unless (= len nlen) |
| 328 |
(error 'deflate-decompression-error |
| 329 |
:format-control |
| 330 |
"Block length mismatch for stored block: LEN(~D) vs. NLEN(~D)!" |
| 331 |
:format-arguments (list len nlen))) |
| 332 |
(dotimes (i len) |
| 333 |
(sliding-window-stream-write-byte out-stream (bit-stream-get-byte stream))))) |
| 334 |
|
| 335 |
;;; |
| 336 |
;;; Huffman Coding |
| 337 |
;;; |
| 338 |
|
| 339 |
;;; A decode-tree struct contains all information necessary to decode |
| 340 |
;;; the given canonical huffman code. Note that length-count contains |
| 341 |
;;; the number of codes with a given length for each length, whereas |
| 342 |
;;; the code-symbols array contains the symbols corresponding to the |
| 343 |
;;; codes in canoical order of the codes. |
| 344 |
;;; |
| 345 |
;;; Decoding then uses this information and the principles underlying |
| 346 |
;;; canonical huffman codes to determine whether the currently |
| 347 |
;;; collected word falls between the first code and the last code for |
| 348 |
;;; the current length, and if so, uses the offset to determine the |
| 349 |
;;; code's symbol. Otherwise more bits are needed. |
| 350 |
|
| 351 |
(defstruct decode-tree |
| 352 |
(length-count (make-array 16 :element-type 'fixnum :initial-element 0) |
| 353 |
:type (simple-array fixnum (*)) :read-only t) |
| 354 |
(code-symbols (make-array 16 :element-type 'fixnum :initial-element 0) |
| 355 |
:type (simple-array fixnum (*)))) |
| 356 |
|
| 357 |
(defun make-huffman-decode-tree (code-lengths) |
| 358 |
"Construct a huffman decode-tree for the canonical huffman code with |
| 359 |
the code lengths of each symbol given in the input array." |
| 360 |
(let* ((max-length (reduce #'max code-lengths :initial-value 0)) |
| 361 |
(next-code (make-array (1+ max-length) :element-type 'fixnum |
| 362 |
:initial-element 0)) |
| 363 |
(code-symbols (make-array (length code-lengths) :element-type 'fixnum |
| 364 |
:initial-element 0)) |
| 365 |
(length-count (make-array (1+ max-length) :element-type 'fixnum |
| 366 |
:initial-element 0))) |
| 367 |
;; Count length occurences and calculate offsets of smallest codes |
| 368 |
(loop for index from 1 to max-length |
| 369 |
for code = 0 then (+ code (aref length-count (1- index))) |
| 370 |
do |
| 371 |
(setf (aref next-code index) code) |
| 372 |
initially |
| 373 |
;; Count length occurences |
| 374 |
(loop for length across code-lengths |
| 375 |
do |
| 376 |
(incf (aref length-count length)) |
| 377 |
finally |
| 378 |
(setf (aref length-count 0) 0))) |
| 379 |
;; Construct code symbols mapping |
| 380 |
(loop for length across code-lengths |
| 381 |
for index upfrom 0 |
| 382 |
unless (zerop length) |
| 383 |
do |
| 384 |
(setf (aref code-symbols (aref next-code length)) index) |
| 385 |
(incf (aref next-code length))) |
| 386 |
;; Return result |
| 387 |
(make-decode-tree :length-count length-count :code-symbols code-symbols))) |
| 388 |
|
| 389 |
(declaim (inline read-huffman-code)) |
| 390 |
(defun read-huffman-code (bit-stream decode-tree) |
| 391 |
(declare (type bit-stream bit-stream) (type decode-tree decode-tree) |
| 392 |
(optimize (speed 3) (safety 0) (space 0) (debug 0))) |
| 393 |
"Read the next huffman code word from the given bit-stream and |
| 394 |
return its decoded symbol, for the huffman code given by decode-tree." |
| 395 |
(loop with length-count of-type (simple-array fixnum (*)) |
| 396 |
= (decode-tree-length-count decode-tree) |
| 397 |
with code-symbols of-type (simple-array fixnum (*)) |
| 398 |
= (decode-tree-code-symbols decode-tree) |
| 399 |
for code of-type fixnum = (bit-stream-read-bits bit-stream 1) |
| 400 |
then (+ (* code 2) (bit-stream-read-bits bit-stream 1)) |
| 401 |
for index of-type fixnum = 0 then (+ index count) |
| 402 |
for first of-type fixnum = 0 then (* (+ first count) 2) |
| 403 |
for length of-type fixnum upfrom 1 below (length length-count) |
| 404 |
for count = (aref length-count length) |
| 405 |
thereis (when (< code (the fixnum (+ first count))) |
| 406 |
(aref code-symbols (+ index (- code first)))) |
| 407 |
finally |
| 408 |
(error 'deflate-decompression-error |
| 409 |
:format-control |
| 410 |
"Corrupted Data detected during decompression: ~ |
| 411 |
Incorrect huffman code (~X) in huffman decode!" |
| 412 |
:format-arguments (list code)))) |
| 413 |
|
| 414 |
;;; |
| 415 |
;;; Standard Huffman Tables |
| 416 |
;;; |
| 417 |
|
| 418 |
(defparameter *std-lit-decode-tree* |
| 419 |
(make-huffman-decode-tree |
| 420 |
(concatenate 'vector |
| 421 |
(make-sequence 'vector 144 :initial-element 8) |
| 422 |
(make-sequence 'vector 112 :initial-element 9) |
| 423 |
(make-sequence 'vector 24 :initial-element 7) |
| 424 |
(make-sequence 'vector 8 :initial-element 8)))) |
| 425 |
|
| 426 |
(defparameter *std-dist-decode-tree* |
| 427 |
(make-huffman-decode-tree |
| 428 |
(make-sequence 'vector 32 :initial-element 5))) |
| 429 |
|
| 430 |
;;; |
| 431 |
;;; Dynamic Huffman Table Handling |
| 432 |
;;; |
| 433 |
|
| 434 |
(defparameter *code-length-entry-order* |
| 435 |
#(16 17 18 0 8 7 9 6 10 5 11 4 12 3 13 2 14 1 15) |
| 436 |
"Order of Code Length Tree Code Lengths.") |
| 437 |
|
| 438 |
(defun decode-code-length-entries (bit-stream count decode-tree) |
| 439 |
"Decode the given number of code length entries from the bit-stream |
| 440 |
using the given decode-tree, and return a corresponding array of code |
| 441 |
lengths for further processing." |
| 442 |
(do ((result (make-array count :element-type 'fixnum :initial-element 0)) |
| 443 |
(index 0)) |
| 444 |
((>= index count) result) |
| 445 |
(let ((code (read-huffman-code bit-stream decode-tree))) |
| 446 |
(ecase code |
| 447 |
((0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) |
| 448 |
(setf (aref result index) code) |
| 449 |
(incf index)) |
| 450 |
(16 |
| 451 |
(let ((length (+ 3 (bit-stream-read-bits bit-stream 2)))) |
| 452 |
(dotimes (i length) |
| 453 |
(setf (aref result (+ index i)) (aref result (1- index)))) |
| 454 |
(incf index length))) |
| 455 |
(17 |
| 456 |
(let ((length (+ 3 (bit-stream-read-bits bit-stream 3)))) |
| 457 |
(dotimes (i length) |
| 458 |
(setf (aref result (+ index i)) 0)) |
| 459 |
(incf index length))) |
| 460 |
(18 |
| 461 |
(let ((length (+ 11 (bit-stream-read-bits bit-stream 7)))) |
| 462 |
(dotimes (i length) |
| 463 |
(setf (aref result (+ index i)) 0)) |
| 464 |
(incf index length))))))) |
| 465 |
|
| 466 |
(defun decode-huffman-tables (bit-stream) |
| 467 |
"Decode the stored huffman tables from the given bit-stream, returning |
| 468 |
the corresponding decode-trees for literals/length and distance codes." |
| 469 |
(let* ((hlit (bit-stream-read-bits bit-stream 5)) |
| 470 |
(hdist (bit-stream-read-bits bit-stream 5)) |
| 471 |
(hclen (bit-stream-read-bits bit-stream 4))) |
| 472 |
;; Construct Code Length Decode Tree |
| 473 |
(let ((cl-decode-tree |
| 474 |
(loop with code-lengths = (make-array 19 :element-type '(unsigned-byte 8) |
| 475 |
:initial-element 0) |
| 476 |
for index from 0 below (+ hclen 4) |
| 477 |
for code-length = (bit-stream-read-bits bit-stream 3) |
| 478 |
for code-index = (aref *code-length-entry-order* index) |
| 479 |
do |
| 480 |
(setf (aref code-lengths code-index) code-length) |
| 481 |
finally |
| 482 |
(return (make-huffman-decode-tree code-lengths))))) |
| 483 |
;; Decode Code Length Table and generate separate huffman trees |
| 484 |
(let ((entries (decode-code-length-entries bit-stream |
| 485 |
(+ hlit 257 hdist 1) |
| 486 |
cl-decode-tree))) |
| 487 |
(values |
| 488 |
(make-huffman-decode-tree (subseq entries 0 (+ hlit 257))) |
| 489 |
(make-huffman-decode-tree (subseq entries (+ hlit 257)))))))) |
| 490 |
|
| 491 |
;;; |
| 492 |
;;; Compressed Block Handling |
| 493 |
;;; |
| 494 |
|
| 495 |
(declaim (inline decode-length-entry)) |
| 496 |
(defun decode-length-entry (symbol bit-stream) |
| 497 |
"Decode the given length symbol into a proper length specification." |
| 498 |
(cond |
| 499 |
((<= symbol 264) (- symbol 254)) |
| 500 |
((<= symbol 268) (+ 11 (* (- symbol 265) 2) (bit-stream-read-bits bit-stream 1))) |
| 501 |
((<= symbol 272) (+ 19 (* (- symbol 269) 4) (bit-stream-read-bits bit-stream 2))) |
| 502 |
((<= symbol 276) (+ 35 (* (- symbol 273) 8) (bit-stream-read-bits bit-stream 3))) |
| 503 |
((<= symbol 280) (+ 67 (* (- symbol 277) 16) (bit-stream-read-bits bit-stream 4))) |
| 504 |
((<= symbol 284) |
| 505 |
(+ 131 (* (- symbol 281) 32) (bit-stream-read-bits bit-stream 5))) |
| 506 |
((= symbol 285) 258) |
| 507 |
(t |
| 508 |
(error 'deflate-decompression-error |
| 509 |
:format-control "Strange Length Code in bitstream: ~D" |
| 510 |
:format-arguments (list symbol))))) |
| 511 |
|
| 512 |
(declaim (inline decode-distance-entry)) |
| 513 |
(defun decode-distance-entry (symbol bit-stream) |
| 514 |
"Decode the given distance symbol into a proper distance specification." |
| 515 |
(cond |
| 516 |
((<= symbol 3) (1+ symbol)) |
| 517 |
(t |
| 518 |
(multiple-value-bind (order offset) (truncate symbol 2) |
| 519 |
(let* ((extra-bits (1- order)) |
| 520 |
(factor (ash 1 extra-bits))) |
| 521 |
(+ (1+ (ash 1 order)) |
| 522 |
(* offset factor) |
| 523 |
(bit-stream-read-bits bit-stream extra-bits))))))) |
| 524 |
|
| 525 |
(defun decode-huffman-block (bit-stream window-stream |
| 526 |
lit-decode-tree dist-decode-tree) |
| 527 |
"Decode the huffman code block using the huffman codes given by |
| 528 |
lit-decode-tree and dist-decode-tree." |
| 529 |
(do ((symbol (read-huffman-code bit-stream lit-decode-tree) |
| 530 |
(read-huffman-code bit-stream lit-decode-tree))) |
| 531 |
((= symbol 256)) |
| 532 |
(cond |
| 533 |
((<= symbol 255) |
| 534 |
(sliding-window-stream-write-byte window-stream symbol)) |
| 535 |
(t |
| 536 |
(let ((length (decode-length-entry symbol bit-stream)) |
| 537 |
(distance (decode-distance-entry |
| 538 |
(read-huffman-code bit-stream dist-decode-tree) bit-stream))) |
| 539 |
(sliding-window-stream-copy-bytes window-stream distance length)))))) |
| 540 |
|
| 541 |
;;; |
| 542 |
;;; Block Handling Code |
| 543 |
;;; |
| 544 |
|
| 545 |
(defun decode-block (bit-stream window-stream) |
| 546 |
"Decompress a block read from bit-stream into window-stream." |
| 547 |
(let* ((finalp (not (zerop (bit-stream-read-bits bit-stream 1)))) |
| 548 |
(type (bit-stream-read-bits bit-stream 2))) |
| 549 |
(ecase type |
| 550 |
(#b00 (bit-stream-copy-block bit-stream window-stream)) |
| 551 |
(#b01 |
| 552 |
(decode-huffman-block bit-stream window-stream |
| 553 |
*std-lit-decode-tree* |
| 554 |
*std-dist-decode-tree*)) |
| 555 |
(#b10 |
| 556 |
(multiple-value-bind (lit-decode-tree dist-decode-tree) |
| 557 |
(decode-huffman-tables bit-stream) |
| 558 |
(decode-huffman-block bit-stream window-stream |
| 559 |
lit-decode-tree dist-decode-tree))) |
| 560 |
(#b11 |
| 561 |
(error 'deflate-decompression-error |
| 562 |
:format-control "Encountered Reserved Block Type ~D!" |
| 563 |
:format-arguments (list type)))) |
| 564 |
(not finalp))) |
| 565 |
|
| 566 |
;;; |
| 567 |
;;; ZLIB - RFC 1950 handling |
| 568 |
;;; |
| 569 |
|
| 570 |
(defun parse-zlib-header (input-stream) |
| 571 |
"Parse a ZLIB-style header as per RFC 1950 from the input-stream and |
| 572 |
return the compression-method, compression-level dictionary-id and flags |
| 573 |
fields of the header as return values. Checks the header for corruption |
| 574 |
and signals a zlib-decompression-error in case of corruption." |
| 575 |
(let ((compression-method (read-byte input-stream)) |
| 576 |
(flags (read-byte input-stream))) |
| 577 |
(unless (zerop (mod (+ (* compression-method 256) flags) 31)) |
| 578 |
(error 'zlib-decompression-error |
| 579 |
:format-control "Corrupted Header ~2,'0X,~2,'0X!" |
| 580 |
:format-arguments (list compression-method flags))) |
| 581 |
(let ((dict (unless (zerop (ldb (byte 1 5) flags)) |
| 582 |
(parse-zlib-checksum input-stream)))) |
| 583 |
(values (ldb (byte 4 0) compression-method) |
| 584 |
(ldb (byte 4 4) compression-method) |
| 585 |
dict |
| 586 |
(ldb (byte 2 6) flags))))) |
| 587 |
|
| 588 |
(defun parse-zlib-checksum (input-stream) |
| 589 |
(+ (* (read-byte input-stream) 256 256 256) |
| 590 |
(* (read-byte input-stream) 256 256) |
| 591 |
(* (read-byte input-stream) 256) |
| 592 |
(read-byte input-stream))) |
| 593 |
|
| 594 |
(defun parse-zlib-footer (input-stream) |
| 595 |
"Parse the ZLIB-style footer as per RFC 1950 from the input-stream and |
| 596 |
return the Adler-32 checksum contained in the footer as its return value." |
| 597 |
(parse-zlib-checksum input-stream)) |
| 598 |
|
| 599 |
;;; |
| 600 |
;;; GZIP - RFC 1952 handling |
| 601 |
;;; |
| 602 |
|
| 603 |
(defconstant +gzip-header-id1+ 31 |
| 604 |
"GZIP Header Magic Value ID1 as per RFC 1952.") |
| 605 |
|
| 606 |
(defconstant +gzip-header-id2+ 139 |
| 607 |
"GZIP Header Magic Value ID2 as per RFC 1952.") |
| 608 |
|
| 609 |
(defun parse-gzip-header (input-stream) |
| 610 |
"Parse a GZIP-style header as per RFC 1952 from the input-stream and |
| 611 |
return the compression-method, text-flag, modification time, XFLAGS, |
| 612 |
OS, FEXTRA flags, filename, comment and CRC16 fields of the header as |
| 613 |
return values (or nil if any given field is not present). Checks the |
| 614 |
header for magic values and correct flags settings and signals a |
| 615 |
gzip-decompression-error in case of incorrect or unsupported magic |
| 616 |
values or flags." |
| 617 |
(let ((id1 (read-byte input-stream)) |
| 618 |
(id2 (read-byte input-stream)) |
| 619 |
(compression-method (read-byte input-stream)) |
| 620 |
(flags (read-byte input-stream))) |
| 621 |
(unless (and (= id1 +gzip-header-id1+) (= id2 +gzip-header-id2+)) |
| 622 |
(error 'gzip-decompression-error |
| 623 |
:format-control |
| 624 |
"Header missing magic values ~2,'0X,~2,'0X (got ~2,'0X,~2,'0X instead)!" |
| 625 |
:format-arguments (list +gzip-header-id1+ +gzip-header-id2+ id1 id2))) |
| 626 |
(unless (= compression-method 8) |
| 627 |
(error 'gzip-decompression-error |
| 628 |
:format-control "Unknown compression-method in Header ~2,'0X!" |
| 629 |
:format-arguments (list compression-method))) |
| 630 |
(unless (zerop (ldb (byte 3 5) flags)) |
| 631 |
(error 'gzip-decompression-error |
| 632 |
:format-control "Unknown flags in Header ~2,'0X!" |
| 633 |
:format-arguments (list flags))) |
| 634 |
(values compression-method |
| 635 |
;; FTEXT |
| 636 |
(= 1 (ldb (byte 1 0) flags)) |
| 637 |
;; MTIME |
| 638 |
(parse-gzip-mtime input-stream) |
| 639 |
;; XFLAGS |
| 640 |
(read-byte input-stream) |
| 641 |
;; OS |
| 642 |
(read-byte input-stream) |
| 643 |
;; FEXTRA |
| 644 |
(unless (zerop (ldb (byte 1 2) flags)) |
| 645 |
(parse-gzip-extra input-stream)) |
| 646 |
;; FNAME |
| 647 |
(unless (zerop (ldb (byte 1 3) flags)) |
| 648 |
(parse-gzip-string input-stream)) |
| 649 |
;; FCOMMENT |
| 650 |
(unless (zerop (ldb (byte 1 4) flags)) |
| 651 |
(parse-gzip-string input-stream)) |
| 652 |
;; CRC16 |
| 653 |
(unless (zerop (ldb (byte 1 1) flags)) |
| 654 |
(+ (read-byte input-stream) |
| 655 |
(* (read-byte input-stream 256))))))) |
| 656 |
|
| 657 |
(defun parse-gzip-mtime (input-stream) |
| 658 |
(let ((time (+ (read-byte input-stream) |
| 659 |
(* (read-byte input-stream) 256) |
| 660 |
(* (read-byte input-stream) 256 256) |
| 661 |
(* (read-byte input-stream) 256 256 256)))) |
| 662 |
(if (zerop time) |
| 663 |
nil |
| 664 |
(+ time 2208988800)))) |
| 665 |
|
| 666 |
(defun parse-gzip-extra (input-stream) |
| 667 |
(let* ((length (+ (read-byte input-stream) (* (read-byte input-stream) 256))) |
| 668 |
(result (make-array length :element-type '(unsigned-byte 8)))) |
| 669 |
(read-sequence result input-stream) |
| 670 |
result)) |
| 671 |
|
| 672 |
(defun parse-gzip-string (input-stream) |
| 673 |
(with-output-to-string (string) |
| 674 |
(loop for value = (read-byte input-stream) |
| 675 |
until (zerop value) |
| 676 |
do (write-char (code-char value) string)))) |
| 677 |
|
| 678 |
(defun parse-gzip-checksum (input-stream) |
| 679 |
(+ (read-byte input-stream) |
| 680 |
(* (read-byte input-stream) 256) |
| 681 |
(* (read-byte input-stream) 256 256) |
| 682 |
(* (read-byte input-stream) 256 256 256))) |
| 683 |
|
| 684 |
(defun parse-gzip-footer (input-stream) |
| 685 |
"Parse the GZIP-style footer as per RFC 1952 from the input-stream and |
| 686 |
return the CRC-32 checksum and ISIZE fields contained in the footer as |
| 687 |
its return values." |
| 688 |
(values (parse-gzip-checksum input-stream) |
| 689 |
;; ISIZE |
| 690 |
(+ (read-byte input-stream) |
| 691 |
(* (read-byte input-stream) 256) |
| 692 |
(* (read-byte input-stream) 256 256) |
| 693 |
(* (read-byte input-stream) 256 256 256)))) |
| 694 |
|
| 695 |
;;; |
| 696 |
;;; Main Entry Points |
| 697 |
;;; |
| 698 |
|
| 699 |
(defun inflate-stream (input-stream output-stream &key checksum) |
| 700 |
"Inflate the RFC 1951 data from the given input stream into the |
| 701 |
given output stream, which are required to have an element-type |
| 702 |
of (unsigned-byte 8). If checksum is given, it indicates the |
| 703 |
checksumming algorithm to employ in calculating a checksum of |
| 704 |
the expanded content, which is then returned from this function. |
| 705 |
Valid values are :adler-32 for Adler-32 checksum (see RFC 1950), |
| 706 |
or :crc-32 for CRC-32 as per ISO 3309 (see RFC 1952, ZIP)." |
| 707 |
(loop with window-stream = (make-sliding-window-stream :stream output-stream |
| 708 |
:checksum checksum |
| 709 |
:checksum-value |
| 710 |
(ecase checksum |
| 711 |
((nil) 0) |
| 712 |
(:crc-32 +crc-32-start-value+) |
| 713 |
(:adler-32 +adler-32-start-value+))) |
| 714 |
with bit-stream = (make-bit-stream :stream input-stream) |
| 715 |
while (decode-block bit-stream window-stream) |
| 716 |
finally (sliding-window-stream-flush window-stream) |
| 717 |
(when checksum |
| 718 |
(return (sliding-window-stream-checksum-value window-stream))))) |
| 719 |
|
| 720 |
(defun inflate-zlib-stream (input-stream output-stream &key check-checksum) |
| 721 |
"Inflate the RFC 1950 zlib data from the given input stream into |
| 722 |
the given output stream, which are required to have an element-type |
| 723 |
of (unsigned-byte 8). This returns the Adler-32 checksum of the |
| 724 |
file as its first return value, with the compression level as its |
| 725 |
second return value. Note that it is the responsibility of the |
| 726 |
caller to check whether the expanded data matches the Adler-32 |
| 727 |
checksum, unless the check-checksum keyword argument is set to |
| 728 |
true, in which case the checksum is checked internally and a |
| 729 |
zlib-decompression-error is signalled if they don't match." |
| 730 |
(multiple-value-bind (cm cinfo dictid flevel) (parse-zlib-header input-stream) |
| 731 |
(unless (= cm 8) |
| 732 |
(error 'zlib-decompression-error |
| 733 |
:format-control "Unknown compression method ~D!" |
| 734 |
:format-arguments (list cm))) |
| 735 |
(unless (<= cinfo 7) |
| 736 |
(error 'zlib-decompression-error |
| 737 |
:format-control "Unsupported sliding window size 2^~D = ~D!" |
| 738 |
:format-arguments (list (+ 8 cinfo) (expt 2 (+ 8 cinfo))))) |
| 739 |
(unless (null dictid) |
| 740 |
(error 'zlib-decompression-error |
| 741 |
:format-control "Unknown preset dictionary id ~8,'0X!" |
| 742 |
:format-arguments (list dictid))) |
| 743 |
(let ((checksum-new (inflate-stream input-stream output-stream |
| 744 |
:checksum (when check-checksum :adler-32))) |
| 745 |
(checksum-old (parse-zlib-footer input-stream))) |
| 746 |
(when (and check-checksum (not (= checksum-old checksum-new))) |
| 747 |
(error 'zlib-decompression-error |
| 748 |
:format-control |
| 749 |
"Checksum mismatch for decompressed stream: ~8,'0X != ~8,'0X!" |
| 750 |
:format-arguments (list checksum-old checksum-new))) |
| 751 |
(values checksum-old flevel)))) |
| 752 |
|
| 753 |
(defun inflate-gzip-stream (input-stream output-stream &key check-checksum) |
| 754 |
"Inflate the RFC 1952 gzip data from the given input stream into |
| 755 |
the given output stream, which are required to have an element-type |
| 756 |
of (unsigned-byte 8). This returns the CRC-32 checksum of the |
| 757 |
file as its first return value, with any filename, modification time, |
| 758 |
and comment fields as further return values or nil if not present. |
| 759 |
Note that it is the responsibility of the caller to check whether the |
| 760 |
expanded data matches the CRC-32 checksum, unless the check-checksum |
| 761 |
keyword argument is set to true, in which case the checksum is checked |
| 762 |
internally and a gzip-decompression-error is signalled if they don't |
| 763 |
match." |
| 764 |
(multiple-value-bind (cm ftext mtime xfl os fextra fname fcomment) |
| 765 |
(parse-gzip-header input-stream) |
| 766 |
(declare (ignore ftext xfl os fextra)) |
| 767 |
(unless (= cm 8) |
| 768 |
(error 'gzip-decompression-error |
| 769 |
:format-control "Unknown compression method ~D!" |
| 770 |
:format-arguments (list cm))) |
| 771 |
(let ((checksum-new (inflate-stream input-stream output-stream |
| 772 |
:checksum (when check-checksum :crc-32))) |
| 773 |
(checksum-old (parse-gzip-footer input-stream))) |
| 774 |
;; Handle Checksums |
| 775 |
(when (and check-checksum (not (= checksum-old checksum-new))) |
| 776 |
(error 'gzip-decompression-error |
| 777 |
:format-control |
| 778 |
"Checksum mismatch for decompressed stream: ~8,'0X != ~8,'0X!" |
| 779 |
:format-arguments (list checksum-old checksum-new))) |
| 780 |
(values checksum-old fname mtime fcomment)))) |
| 781 |
|
| 782 |
(defun gunzip (input-file output-file) |
| 783 |
(with-open-file (input input-file |
| 784 |
:element-type '(unsigned-byte 8)) |
| 785 |
(with-open-file (output output-file |
| 786 |
:direction :output |
| 787 |
:if-exists :supersede |
| 788 |
:element-type '(unsigned-byte 8)) |
| 789 |
(inflate-gzip-stream input output))) |
| 790 |
(probe-file output-file)) |
| 791 |
|
| 792 |
(provide '#:deflate) |