changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/io/flate.lisp

changeset 658: 804b5ee20a46
parent: c6caddf91c72
author: Richard Westhaver <ellis@rwest.io>
date: Thu, 19 Sep 2024 23:23:02 -0400
permissions: -rw-r--r--
description: zstd completed (besides zdict), working on readline
1 ;;; io/flate.lisp --- Compressed IO Interface
2 
3 ;; Use compression (ZSTD) with Lisp objects and streams.
4 
5 ;;; Commentary:
6 
7 ;; compression ref: https://www.xach.com/lisp/salza2/ (compression only)
8 
9 ;; decompression ref: https://github.com/sharplispers/chipz (decompression only)
10 
11 ;; The libraries above are the current state-of-the-art for compression and
12 ;; decompression in Common Lisp. They are portable packages which depend on
13 ;; gray streams. They loosely cover deflate, zlib, gzip, and bzip2 data.
14 
15 ;; The compression backends are themselves hand-coded in Common Lisp, making
16 ;; them excellent reference material. However, we don't have much use for the
17 ;; compression backends offered.
18 
19 ;; We intend to almost exclusively support Zstd compression and decompression
20 ;; using our ZSTD FFI Lisp system, so we'll make a new library - FLATE - which
21 ;; provides a shared zstd compression/decompression to Lisp objects and
22 ;; streams.
23 
24 ;;; Code:
25 (in-package :io/flate)
26 
27 ;;; Vars
28 (defparameter *compression-buffer-size* 4096)
29 (defparameter *decompression-buffer-size* 4096)
30 (defparameter *default-compression-level* (zstd:zstd-defaultclevel))
31 
32 (defvar *compression-level*)
33 
34 ;;; Utils
35 
36 ;;; Errors
37 (eval-always (deferror flate-error () () (:auto t)))
38 
39 (deferror compression-error (flate-error) () (:auto t))
40 (deferror decompression-error (flate-error) () (:auto t))
41 
42 ;;; Proto
43 (defgeneric finish-compression (self))
44 (defgeneric finish-decompression (self))
45 ;; TODO 2024-06-08: maybe move this to generic io/stream protocol - 'RESET'
46 (defgeneric reset-compressor (self))
47 (defgeneric reset-decompressor (self))
48 (defgeneric make-compressed-stream (&optional stream))
49 (defgeneric make-decompressed-stream (&optional stream))
50 (defgeneric compress-object (self))
51 (defgeneric decompress-object (self))
52 
53 (defgeneric compress (input output state))
54 (defgeneric decompress (input output state))
55 
56 ;; decompress
57 
58 ;;; Compression
59 
60 ;; AKA 'DEFLATE'
61 
62 ;; compress-octet
63 ;; compress-octet-vector
64 
65 ;; finish-compression (finish-output?)
66 ;; with-compressor
67 ;; reset-compressor
68 
69 ;; make-compressed-stream
70 
71 (defclass compressor () ())
72 
73 (defclass compressed-stream (fundamental-binary-stream)
74  ())
75 
76 ;;; Decompression
77 
78 ;; AKA 'INFLATE'
79 
80 ;; From chipz:
81 ;; We provide several convenience functions for decompression:
82 ;;
83 ;; * decompress a buffer to a newly-consed buffer;
84 ;; * decompress a stream to a newly-consed buffer;
85 ;; * decompress a pathname to a newly-consed buffer;
86 ;; * decompress a buffer to a user-specified buffer;
87 ;; * decompress a buffer to a stream;
88 ;; * decompress a stream to a stream.
89 ;; * decompress a pathname to another pathname;
90 ;; * decompress a pathname to a stream;
91 ;;
92 ;; We do not provide stream->buffer decompression, as we have no way of
93 ;; knowing how much to read from the stream to fill the buffer, no way
94 ;; of determining what to do with possible state left in the
95 ;; INFLATE-STATE that we used, etc. Application-specific logic will
96 ;; have to handle those bits.
97 
98 ;; make-decompressed-stream
99 ;; decompress-octet
100 ;; decompress-octet-vector
101 
102 (defclass decompressor () ())
103 
104 (defclass decompressed-stream (fundamental-binary-stream)
105  ())
106 
107 ;;; API
108 
109 ;; zstd-stream
110 ;; zstd-file