Add README.txt.
[salza2.git] / gzip.lisp
blob347a0e3090f0c4e673d4d5765e3bf758d91f67ac
1 ;;;
2 ;;; Copyright (c) 2007 Zachary Beane, All Rights Reserved
3 ;;;
4 ;;; Redistribution and use in source and binary forms, with or without
5 ;;; modification, are permitted provided that the following conditions
6 ;;; are met:
7 ;;;
8 ;;; * Redistributions of source code must retain the above copyright
9 ;;; notice, this list of conditions and the following disclaimer.
10 ;;;
11 ;;; * Redistributions in binary form must reproduce the above
12 ;;; copyright notice, this list of conditions and the following
13 ;;; disclaimer in the documentation and/or other materials
14 ;;; provided with the distribution.
15 ;;;
16 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ;;;
29 (in-package #:salza2)
31 (defvar *gzip-signature* (octet-vector #x1F #x8B)
32 "These two octets precede all data in the gzip format.")
34 (defconstant +gzip-fast-compression+ 4
35 "Code for gzip compression level. This is present only to create valid
36 gzip data; it has no meaning to the compressor and is only a hint to
37 the decompressor.")
39 ;;; These are all used to create valid files, not to control or modify
40 ;;; the compression process.
42 (defconstant +gzip-deflate-compression+ 8)
43 (defconstant +gzip-flags+ 0)
44 (defconstant +gzip-unix-os+ 3)
45 (defconstant +gzip-mtime+ 0)
47 (defun gzip-write-u32 (value compressor)
48 ;; LSB
49 (write-octet (ldb (byte 8 0) value) compressor)
50 (write-octet (ldb (byte 8 8) value) compressor)
51 (write-octet (ldb (byte 8 16) value) compressor)
52 (write-octet (ldb (byte 8 24) value) compressor))
54 (defclass gzip-compressor (deflate-compressor)
55 ((checksum
56 :initarg :checksum
57 :accessor checksum)
58 (data-length
59 :initarg :data-length
60 :accessor data-length))
61 (:default-initargs
62 :checksum (make-instance 'crc32-checksum)
63 :data-length 0))
65 (defmethod start-data-format :before ((compressor gzip-compressor))
66 (write-octet-vector *gzip-signature* compressor)
67 (write-octet +gzip-deflate-compression+ compressor)
68 (write-octet +gzip-flags+ compressor)
69 (gzip-write-u32 +gzip-mtime+ compressor)
70 (write-octet +gzip-fast-compression+ compressor)
71 (write-octet +gzip-unix-os+ compressor))
73 (defmethod process-input :after ((compressor gzip-compressor)
74 input start count)
75 (incf (data-length compressor) count)
76 (update (checksum compressor) input start count))
78 (defmethod finish-data-format :after ((compressor gzip-compressor))
79 (gzip-write-u32 (result (checksum compressor)) compressor)
80 (gzip-write-u32 (data-length compressor) compressor))
82 (defmethod reset :after ((compressor gzip-compressor))
83 (reset (checksum compressor))
84 (setf (data-length compressor) 0))