changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/ffi/zstd/pkg.lisp

changeset 392: 077e7b391758
parent: 757b91ddcb2a
child: 6b87df03cdaf
author: Richard Westhaver <ellis@rwest.io>
date: Sat, 01 Jun 2024 15:44:55 -0400
permissions: -rw-r--r--
description: finish zstd init
1 ;;; ffi/zstd/pkg.lisp --- ZSTD FFI
2 
3 ;; from zstd.h:
4 #|
5  Introduction
6 
7  zstd, short for Zstandard, is a fast lossless compression algorithm, targeting
8  real-time compression scenarios at zlib-level and better compression ratios.
9  The zstd compression library provides in-memory compression and decompression
10  functions.
11 
12  The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
13  which is currently 22. Levels >= 20, labeled `--ultra`, should be used with
14  caution, as they require more memory. The library also offers negative
15  compression levels, which extend the range of speed vs. ratio preferences.
16  The lower the level, the faster the speed (at the cost of compression).
17 
18  Compression can be done in:
19  - a single step (described as Simple API)
20  - a single step, reusing a context (described as Explicit context)
21  - unbounded multiple steps (described as Streaming compression)
22 
23  The compression ratio achievable on small data can be highly improved using
24  a dictionary. Dictionary compression can be performed in:
25  - a single step (described as Simple dictionary API)
26  - a single step, reusing a dictionary (described as Bulk-processing
27  dictionary API)
28 
29  Advanced experimental functions can be accessed using
30  `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h.
31 
32  Advanced experimental APIs should never be used with a dynamically-linked
33  library. They are not "stable"; their definitions or signatures may change in
34  the future. Only static linking is allowed.
35 |#
36 
37 ;;; Code:
38 (defpackage :zstd
39  (:use :cl :std :sb-alien)
40  (:nicknames :zstd))
41 
42 (in-package :zstd)
43 
44 (define-alien-loader "zstd" t "/usr/lib/")
45 
46 ;;; Utils
47 (define-alien-routine "ZSTD_versionNumber"
48  unsigned)
49 
50 (define-alien-routine "ZSTD_versionString" c-string)
51 
52 (define-alien-routine "ZSTD_compressBound" size-t (src-size size-t))
53 (define-alien-routine "ZSTD_isError" unsigned (code size-t))
54 (define-alien-routine "ZSTD_getErrorName" c-string (code size-t))
55 (define-alien-routine "ZSTD_minCLevel" int)
56 (define-alien-routine "ZSTD_maxCLevel" int)
57 (define-alien-routine "ZSTD_defaultCLevel" int)
58 
59 ;;; Simple API
60 (define-alien-routine "ZSTD_compress" size-t
61  (dst (* t)) (dst-capacity size-t)
62  (src (* t)) (src-size size-t)
63  (compression int))
64 
65 (define-alien-routine "ZSTD_decompress" size-t
66  (dst (* t)) (dst-capacity size-t)
67  (src (* t)) (compressed-size size-t))
68 
69 ;;; Explicit Context API
70 (define-alien-type zstd-cctx (struct zstd-cctx-s))
71 
72 (define-alien-routine "ZSTD_createCCtx" (* zstd-cctx))
73 (define-alien-routine "ZSTD_freeCCtx" void (cctx (* zstd-cctx)))
74 (define-alien-routine "ZSTD_compressCCtx" size-t
75  (cctx (* zstd-cctx))
76  (dst (* t)) (dst-capacity size-t)
77  (src (* t)) (src-size size-t)
78  (compression-level int))
79 
80 (define-alien-type zstd-dctx (struct zstd-dctx-s))
81 
82 (define-alien-routine "ZSTD_createDCtx" (* zstd-dctx))
83 (define-alien-routine "ZSTD_freeDCtx" void (dctx (* zstd-dctx)))
84 (define-alien-routine "ZSTD_decompressDCtx" size-t
85  (dctx (* zstd-dctx))
86  (dst (* t)) (dst-capacity size-t)
87  (src (* t)) (src-size size-t))
88 
89 ;;; Streaming API
90 (define-alien-type zstd-inbuffer (struct zstd-inbuffer-s
91  (src (* t))
92  (size size-t)
93  (pos size-t)))
94 
95 (define-alien-type zstd-outbuffer (struct zstd-outbuffer-s
96  (dst (* t))
97  (size size-t)
98  (pos size-t)))
99 
100 (define-alien-type zstd-cstream zstd-cctx)
101 
102 (define-alien-routine "ZSTD_createCStream" (* zstd-cstream))
103 (define-alien-routine "ZSTD_freeCStream" void (zcs (* zstd-cstream)))
104 
105 (define-alien-type zstd-enddirective int)
106 
107 (define-alien-routine "ZSTD_compressStream2" size-t
108  (cctx (* zstd-cctx))
109  (output (* zstd-outbuffer))
110  (input (* zstd-inbuffer))
111  (end-op zstd-enddirective))
112 
113 (define-alien-routine "ZSTD_CStreamInSize" size-t)
114 (define-alien-routine "ZSTD_CStreamOutSize" size-t)
115 
116 (define-alien-type zstd-dstream zstd-dctx)
117 
118 (define-alien-routine "ZSTD_createDStream" (* zstd-dstream))
119 (define-alien-routine "ZSTD_freeDStream" void (zds (* zstd-dstream)))
120 (define-alien-routine "ZSTD_initDStream" size-t (zds (* zstd-dstream)))
121 
122 (define-alien-routine "ZSTD_decompressStream" size-t
123  (zds (* zstd-dstream))
124  (output (* zstd-outbuffer))
125  (input (* zstd-inbuffer)))
126 
127 (define-alien-routine "ZSTD_DStreamInSize" size-t)
128 (define-alien-routine "ZSTD_DStreamOutSize" size-t)
129 
130 ;;; Simple Dictionary API
131 (define-alien-routine "ZSTD_compress_usingDict" size-t
132  (cctx (* zstd-cctx))
133  (dst (* t))
134  (dst-capacity size-t)
135  (src (* t))
136  (src-size size-t)
137  (dict (* t))
138  (dict-size size-t)
139  (compression-level int))
140 
141 (define-alien-routine "ZSTD_decompress_usingDict" size-t
142  (dctx (* zstd-dctx))
143  (dst (* t))
144  (dst-capacity size-t)
145  (src (* t))
146  (src-size size-t)
147  (dict (* t))
148  (dict-size size-t))
149 
150 ;;; Bulk-processing Dictionary API
151 (define-alien-type zstd-cdict (struct zstd-cdict-s))
152 
153 (define-alien-routine "ZSTD_createCDict" (* zstd-cdict)
154  (dict-buffer (* t))
155  (dict-size size-t)
156  (compression-level int))
157 
158 (define-alien-routine "ZSTD_freeCDict" size-t (cdict (* zstd-cdict)))
159 
160 (define-alien-routine "ZSTD_compress_usingCDict" size-t
161  (cctx (* zstd-cctx))
162  (dst (* t))
163  (dst-capacity size-t)
164  (src (* t))
165  (src-size size-t)
166  (cdict (* zstd-cdict)))
167 
168 (define-alien-type zstd-ddict (struct zstd-ddict-s))
169 
170 (define-alien-routine "ZSTD_createDDict" (* zstd-ddict)
171  (dict-buffer (* t))
172  (dict-size size-t))
173 
174 (define-alien-routine "ZSTD_freeDDict" size-t (ddict (* zstd-ddict)))
175 
176 (define-alien-routine "ZSTD_compress_usingDDict" size-t
177  (dctx (* zstd-dctx))
178  (dst (* t))
179  (dst-capacity size-t)
180  (src (* t))
181  (src-size size-t)
182  (ddict (* zstd-ddict)))
183 
184 ;; dictionary utils
185 (define-alien-routine "ZSTD_getDictID_fromDict" unsigned
186  (dict (* t))
187  (dict-size size-t))
188 
189 (define-alien-routine "ZSTD_getDictID_fromCDict" unsigned
190  (cdict (* zstd-cdict)))
191 
192 (define-alien-routine "ZSTD_getDictID_fromDDict" unsigned
193  (cdict (* zstd-ddict)))
194 
195 (define-alien-routine "ZSTD_getDictID_fromFrame" unsigned
196  (src (* t))
197  (src-size size-t))