File: deflate-stream-interface.lisp

package info (click to toggle)
cl-salza 0.7.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 128 kB
  • ctags: 103
  • sloc: lisp: 921; makefile: 46; sh: 28
file content (144 lines) | stat: -rw-r--r-- 6,085 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;;; 
;;; deflate-stream-interface.lisp
;;; 
;;; Created: 2005-03-14 by Zach Beane <xach@xach.com>
;;; 
;;; Copyright (c) 2005 Zachary Beane, All Rights Reserved
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.
;;;
;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;; 
;;; $Id: deflate-stream-interface.lisp,v 1.10 2005/03/23 19:55:20 xach Exp $

(in-package :salza-deflate)

(defun start-deflate-stream (deflate-stream)
    ;; The block header
    ;; BFINAL is always set, since right now dynamic codes are not
    ;; supported so we never need to start a new block
    (write-bits 1 1 deflate-stream)
    (write-bits +deflate-fixed-tables-code+ 2 deflate-stream))

(defun finish-deflate-stream (deflate-stream)
  "Compress any pending input in the deflate-stream to its internal
buffer and add the end-of-block code."
  (finish-compress deflate-stream)
  (write-literal #x100 deflate-stream)
  (flush-deflate-stream deflate-stream)
  (funcall (deflate-stream-callback deflate-stream)
           deflate-stream))

(defun deflate-write-sequence (sequence deflate-stream
                               &key (start 0) (end (length sequence)))
  (compress-sequence sequence deflate-stream start end))


(defun deflate-write-byte (octet deflate-stream)
  "Finish any pending byte in the deflate stream and write BYTE as the
next octet to DEFLATE-STREAM."
  (flush-deflate-stream deflate-stream)
  (write-bits octet 8 deflate-stream))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (when (<= char-code-limit 256)
    (pushnew :octet-characters *features*)))

(defun string-to-octets (string start end)
  "Convert STRING to a sequence of octets, if possible."
  (declare (type string string)
           (type buffer-offset start end)
           (optimize (speed 3) (safety 0)))
  #+(and sbcl (not octet-characters))
  (sb-ext:string-to-octets string :start start :end end)
  #+(and allegro (not octet-characters))
  (excl:string-to-octets string :start start :end end :null-terminate nil)
  #+(and clisp (not octet-characters))
  (ext:convert-string-to-bytes string charset:ascii :start start :end end)
  #+(or octet-characters lispworks)
  (let* ((length (- end start))
         (result (make-array length :element-type 'octet)))
    (loop for i fixnum from start below end
          for j fixnum from 0
          do (setf (aref result j) (char-code (aref string i))))
    result)
  #+(and (not octet-characters) (not (or sbcl allegro clisp lispworks)))
  (error "Do not know how to convert a string to octets."))

(defun deflate-write-string (string deflate-stream
                             &key (start 0) (end (length string)))
  (deflate-write-sequence (string-to-octets string start end) deflate-stream))


;;; CRC32

(defun crc32-table ()
  (declare (optimize (speed 3) (safety 0)))
  (let ((table (make-array 512 :element-type '(unsigned-byte 16))))
    (dotimes (n 256 table)
      (let ((c n))
        (declare (type (unsigned-byte 32) c))
        (dotimes (k 8)
          (if (logbitp 0 c)
              (setf c (logxor #xEDB88320 (ash c -1)))
              (setf c (ash c -1)))
          (setf (aref table (ash n 1)) (ldb (byte 16 16) c)
                (aref table (1+ (ash n 1))) (ldb (byte 16 0) c)))))))

(let ((table (crc32-table)))
  (defun crc32 (high low buf end)
    (declare (optimize (speed 3) (safety 0) #+lispworks (hcl:fixnum-safety 0))
             (type (unsigned-byte 16) high low)
             (type octet-vector buf)
             (type (simple-array (unsigned-byte 16) (*)) table)
             (fixnum end))
    (let ((len end))
      (declare (fixnum len))
      (dotimes (n len (values high low))
        (declare (fixnum n))
        (let ((index (logxor (logand low #xFF) (aref buf n))))
          (declare (type (integer 0 255) index))
          (let ((high-index (ash index 1))
                (low-index (1+ (ash index 1))))
            (declare (type (integer 0 511) high-index low-index))
            (let ((t-high (aref table high-index))
                  (t-low (aref table low-index)))
              (declare (type (unsigned-byte 16) t-high t-low))
              (setf low (logxor (ash (logand high #xFF) 8)
                                (ash low -8)
                                t-low))
              (setf high (logxor (ash high -8) t-high)))))))))


(defun crc32-sequence (sequence &key (end (length sequence)))
  "Return an octet sequence of four bytes containing the
crc32 checksum of SEQUENCE."
  (multiple-value-bind (high low)
      (crc32 #xFFFF #xFFFF sequence end)
    (setf high (logxor #xFFFF high)
          low (logxor #xFFFF low))
    (make-array 4 :element-type 'octet
                :initial-contents (list (ldb (byte 8 8) high)
                                        (ldb (byte 8 0) high)
                                        (ldb (byte 8 8) low)
                                        (ldb (byte 8 0) low)))))