-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.lisp
333 lines (285 loc) · 12.6 KB
/
common.lisp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(cl:defpackage #:semz.decompress
(:use #:cl)
(:export #:decompress
#:decompress-all
#:make-decompression-stream
#:make-full-decompression-stream
#:list-supported-formats
#:make-simple-zlib-dictionary
#:adler-32
#:decompression-stream
#:decompression-stream-format
#:decompression-stream-header
#:decompression-error
#:eof
#:unrecognized-zlib-dictionary
#:unrecognized-zlib-dictionary-checksum)
(:import-from #:alexandria
#:array-length
#:clamp
#:compose
#:define-constant
#:ensure-list
#:iota
#:read-stream-content-into-byte-vector
#:remove-from-plistf
#:required-argument
#:with-gensyms)
(:import-from #:trivial-gray-streams
#:fundamental-binary-input-stream
#:stream-read-byte
#:stream-read-sequence))
(cl:in-package #:semz.decompress)
;;;; Types & conditions
(define-condition decompression-error (simple-error) ()
(:documentation "General superclass for errors related to decompression."))
(define-condition eof (decompression-error) ()
(:documentation
"Signalled when the input stream/buffer is exhausted. Notably implies that we
did not detect errors in the data up until this point, but this is not a hard
guarantee that the data can be continued in a valid manner since it would be
infeasible to verify this.
Even when the input is a stream, it is this condition which is signalled, not
`end-of-file'."))
(defun die (fmt &rest fmt-args)
(error 'decompression-error :format-control fmt :format-arguments fmt-args))
(defun %eof ()
(error 'eof :format-control "Premature end of input."))
(deftype octet ()
'(unsigned-byte 8))
(deftype buffer ()
'(simple-array octet (*)))
;;;; Fast functions
;;;
;;; We rely heavily on typed inline functions rather than big macrolets in the
;;; interest of readability; the macros below reduce the resulting clutter and
;;; make it easier to adjust optimization qualities.
;;;
;;; As a general rule, we want to go fast, ignore qualities that are already
;;; affected by heavy inlining, and keep the rest at the neutral defaults.
;;;
;;; On ABCL, (safety 0) makes a huge difference and basically just eliminates
;;; type checks and inlines accesses. Since ABCL is JVM-based, the bad
;;; consequences of (safety 0) are rather mild, too. Now if only it got an
;;; actually fast `replace' implementation...
;;;
;;; ECL has unusual default settings: (speed 3) (space 0) (safety 2) (debug 3).
;;; The manual documents how these settings affect things:
;;;
;;; https://ecl.common-lisp.dev/static/manual/Evaluation-and-compilation.html
;;;
;;; Setting safety and debug to 1 seems fine, considering what effects type
;;; declarations already have on implementations like CCL. (safety 0) speeds up
;;; things by quite a bit, but even ECL devs recommend against it, so that's an
;;; easy no.
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *optimize-decls*
'((speed 3) (space 0) (compilation-speed 0)
#-abcl (safety 1)
#+abcl (safety 0)
(debug 1))
"The implementation-dependent optimization qualities used for fast functions."))
(defmacro define-fast-function (name-with-optional-return-type (&rest args) &body body)
(destructuring-bind (name &optional (return-type '*))
(ensure-list name-with-optional-return-type)
(setf args (mapcar (lambda (x)
(if (listp x)
(progn
(assert (= 2 (length x)))
x)
(list x 'T)))
args))
`(progn
(declaim (ftype (function (,@(mapcar #'second args))
,@(if (eq return-type '*)
'()
`(,return-type)))
,name))
(defun ,name (,@(mapcar #'first args))
(declare ,@(mapcar (lambda (a)
(destructuring-bind (name type) a
`(type ,type ,name)))
args)
(optimize ,@*optimize-decls*))
,@body))))
(defmacro define-fast-inline-function (name-with-optional-return-type (&rest args) &body body)
`(progn
(declaim (inline ,(first (ensure-list name-with-optional-return-type))))
(define-fast-function ,name-with-optional-return-type (,@args) ,@body)))
;;;; Helpers
(define-constant +dummy-buffer+ (coerce #() 'buffer)
:test 'equalp
:documentation "Placeholder for buffer-typed slots.")
;;; We need to make some size choice when buffering things; 8192 seems fine.
(defparameter *default-buffer-size* (expt 2 13))
(defun ensure-simple-vector-constant (constant)
(assert (constantp constant))
(let ((result (eval constant)))
(assert (typep result 'simple-vector))
result))
(deftype element-of (sv-constant)
(let ((sv (ensure-simple-vector-constant sv-constant)))
(if (every #'integerp sv)
`(integer ,(reduce #'min sv) ,(reduce #'max sv))
`T)))
(deftype index-for (sv-constant)
`(mod ,(length (ensure-simple-vector-constant sv-constant))))
(defmacro csvref (sv-constant index)
"An `svref' for constants that derives integer bounds if possible."
`(the (element-of ,sv-constant) (svref ,sv-constant ,index)))
(defmacro onetime-macrolet ((&rest bindings) &body code)
(with-gensyms (define)
`(macrolet ((,define () (let* ,bindings ,@code)))
(,define))))
(defmacro with-prefixed-names ((&rest names) prefix &body body)
`(let (,@(mapcar
(lambda (name)
`(,name (intern (concatenate 'string
;; `prefix' is evaluated
(string ,prefix)
;; `name' isn't
,(string name)))))
names))
,@body))
(defmacro normalize-bounds (array start end)
(check-type array symbol)
(check-type start symbol)
(check-type end symbol)
`(progn
(check-type ,array (array * (*)))
(setf ,start (or ,start 0))
(setf ,end (or ,end (length ,array)))
(check-type ,start array-length)
(check-type ,end array-length)
(assert (<= 0 ,start ,end (length ,array)))))
;;; "Why not use nibbles?" - Because nibbles:
;;;
;;; 1. Has very brittle SBCL-specific code (this may change with sb-nibbles);
;;;
;;; 2. Is often slower than the naive code on anything other than SBCL;
;;;
;;; 3. Doesn't support 24-bit and 48-bit sizes.
;;;
;;; Why bother with a dependency in this case?
(macrolet
((define-le-accessor (name octet-count)
`(progn
(declaim (inline ,name (setf ,name))
(ftype (function (buffer array-length) (unsigned-byte ,(* 8 octet-count))) ,name))
(defun ,name (vector index)
(declare (type buffer vector)
(type array-length index))
(logior ,@(loop :for i :from 0 :below octet-count
:collect `(ash (aref vector (+ index ,i)) ,(* 8 i)))))
(defun (setf ,name) (value vector index)
(setf ,@(loop :for i :from 0 :below octet-count
:collect `(aref vector (+ index ,i))
:collect `(ldb (byte 8 ,(* 8 i)) value)))
value)))
(define-be-accessor (name octet-count)
`(progn
(declaim (inline ,name (setf ,name))
(ftype (function (buffer array-length) (unsigned-byte ,(* 8 octet-count))) ,name))
(defun ,name (vector index)
(declare (type buffer vector)
(type array-length index))
(logior ,@(loop :for i :from 0 :below octet-count
:collect `(ash (aref vector (+ index ,(- octet-count 1) (- ,i)))
,(* 8 i)))))
(defun (setf ,name) (value vector index)
(setf ,@(loop :for i :from 0 :below octet-count
:collect `(aref vector (+ index ,(- octet-count 1) (- ,i)))
:collect `(ldb (byte 8 ,(* 8 i)) value)))
value))))
(define-le-accessor ub16ref/le 2)
(define-le-accessor ub24ref/le 3)
(define-le-accessor ub32ref/le 4)
(define-le-accessor ub48ref/le 6)
(define-le-accessor ub64ref/le 8)
(define-be-accessor ub16ref/be 2)
(define-be-accessor ub32ref/be 4)
(define-be-accessor ub64ref/be 8))
(defun positions-if (predicate sequence)
(loop :for i :from 0 :below (length sequence)
:when (funcall predicate (elt sequence i))
:collect i))
(defun positions (elt sequence)
(positions-if (lambda (x) (eql x elt)) sequence))
(defun hexdigitp (char)
(not (not (position char "0123456789abcdefABCDEF"))))
(defun reverse-ub32-byte-order (ub32)
(logior (ash (ldb (byte 8 0) ub32) 24)
(ash (ldb (byte 8 8) ub32) 16)
(ash (ldb (byte 8 16) ub32) 8)
(ash (ldb (byte 8 24) ub32) 0)))
;;;; Internal interface
;;;
;;; For each format, there should be methods defined on these two functions.
;;; `format' is always an identifying keyword such as :deflate or :zlib.
;;;
;;; The decompressed output is divided into "chunks" in some
;;; implementation-defined manner. The only requirement is that there is some
;;; sane upper bound for chunk size (to keep memory usage bounded).
(defparameter *known-formats* '()
"`pushnew' the format keyword to this once you've implemented a new format.")
(defgeneric byte-source->decompression-state (format byte-source &key &allow-other-keys)
(:documentation
"Reads the necessary data from the given `byte-source' (see `io.lisp') to create
a decompression state for `format'. Returns two values: The decompression state
that can be passed to `next-decompressed-chunk' later and a plist of header
metadata which will be returned to the user."))
(defgeneric next-decompressed-chunk (state)
(:documentation
"Returns four values: chunk, start, end, finalp. The data is in `chunk',
between `start' and `end'. No callers modify the contents of `chunk'; methods
may change its contents on later calls, but not before that."))
(defgeneric make-reset-state (old-state)
(:documentation
"Returns two values:
1. A new state that reads the next member of the same type, handling potential
padding/etc in between, and maybe reuses large intermediate resources.
2. The new header plist.
If the format doesn't support multi-member files, returns nil."))
;;; Most formats don't support multiple members, so default to that.
(defmethod make-reset-state (old-state)
(declare (ignore old-state))
nil)
;;; Multi-member formats with trailing non-member data (e.g. padding) can handle
;;; it in `make-reset-state' and then return this dummy state to signal EOF.
(defclass eof-dummy-state () ())
(defmethod next-decompressed-chunk ((state eof-dummy-state))
(values +dummy-buffer+ 0 0 t))
;;;; Lempel-Ziv helpers
;;; We usually unify dictionaries and output buffers by designating the first
;;; `d' bytes as the dictionary area. When the output buffer is full, we publish
;;; the new data; on the next `next-decompressed-chunk' call, we then move the
;;; last `d' bytes into the dictionary area at the start. Until the buffer is
;;; filled for the first time, we write directly into the dictionary area.
(defun flush-dict-buffer (buffer buffer-i dict-size)
"Moves trailing data in `buffer' into the dictionary area as designated by
`dict-size'. Returns the new value of `buffer-i'."
(declare (type buffer buffer)
(type array-length buffer-i dict-size))
(if (<= buffer-i dict-size)
buffer-i
(progn
(replace buffer buffer
:start1 0 :end1 dict-size
:start2 (- buffer-i dict-size)
:end2 buffer-i)
dict-size)))
;;; Lempel-Ziv matches are a common feature and usually have relatively small
;;; lengths (~300 max). As a result, this simple loop implementation has less
;;; overhead than `replace' and deals with overlaps. The (safety 0) matters for
;;; heavily compressed files; the loop is simple and guarded by an assertion, so
;;; it's fine.
(define-fast-inline-function copy-match
((buffer buffer)
(src-i array-length)
(dest-i array-length)
(length array-length))
(assert (<= (+ src-i 1) dest-i (- (length buffer) length)))
(locally (declare (optimize (safety 0)))
(loop :for i :of-type array-length :from 0 :below length :do
(setf (aref buffer (+ dest-i i))
(aref buffer (+ src-i i))))))