zlib.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. ;;; Guile-zlib --- GNU Guile bindings of zlib
  2. ;;; Copyright © 2016, 2017, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
  4. ;;; Copyright © 2013 David Thompson <dthompson2@worcester.edu>
  5. ;;; Copyright © 2023 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  6. ;;;
  7. ;;; This file is part of Guile-zlib.
  8. ;;;
  9. ;;; Guile-zlib is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; Guile-zlib is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with Guile-zlib. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (zlib)
  22. #:use-module (zlib config)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (ice-9 binary-ports)
  25. #:use-module (ice-9 match)
  26. #:use-module (system foreign)
  27. #:use-module (ice-9 receive)
  28. #:use-module (srfi srfi-1)
  29. #:export (make-gzip-input-port
  30. make-gzip-output-port
  31. call-with-gzip-input-port
  32. call-with-gzip-output-port
  33. %default-buffer-size
  34. %default-compression-level
  35. compress
  36. uncompress
  37. adler32
  38. crc32
  39. make-zlib-input-port
  40. make-zlib-output-port
  41. call-with-zlib-input-port
  42. call-with-zlib-output-port))
  43. ;;; Commentary:
  44. ;;;
  45. ;;; This file is extracted from Guix and originally written by Ludovic Courtès.
  46. ;;; Bindings to the gzip-related part of zlib's API. The main limitation of
  47. ;;; this API is that it requires a file descriptor as the source or sink.
  48. ;;;
  49. ;;; Code:
  50. (define %zlib
  51. (delay (dynamic-link %libz)))
  52. (define (zlib-procedure ret name parameters)
  53. "Return a procedure corresponding to C function NAME in libz, or #f if
  54. either zlib or the function could not be found."
  55. (match (false-if-exception (dynamic-func name (force %zlib)))
  56. ((? pointer? ptr)
  57. (pointer->procedure ret ptr parameters))
  58. (#f
  59. #f)))
  60. (define-wrapped-pointer-type <gzip-file>
  61. ;; Scheme counterpart of the 'gzFile' opaque type.
  62. gzip-file?
  63. pointer->gzip-file
  64. gzip-file->pointer
  65. (lambda (obj port)
  66. (format port "#<gzip-file ~a>"
  67. (number->string (object-address obj) 16))))
  68. (define gzerror
  69. (let ((proc (zlib-procedure '* "gzerror" '(* *))))
  70. (lambda (gzfile)
  71. (let* ((errnum* (make-bytevector (sizeof int)))
  72. (ptr (proc (gzip-file->pointer gzfile)
  73. (bytevector->pointer errnum*))))
  74. (values (bytevector-sint-ref errnum* 0
  75. (native-endianness) (sizeof int))
  76. (pointer->string ptr))))))
  77. (define gzdopen
  78. (let ((proc (zlib-procedure '* "gzdopen" (list int '*))))
  79. (lambda (fd mode)
  80. "Open file descriptor FD as a gzip stream with the given MODE. MODE must
  81. be a string denoting the how FD is to be opened, such as \"r\" for reading or
  82. \"w9\" for writing data compressed at level 9 to FD. Calling 'gzclose' also
  83. closes FD."
  84. (let ((result (proc fd (string->pointer mode))))
  85. (if (null-pointer? result)
  86. (throw 'zlib-error 'gzdopen)
  87. (pointer->gzip-file result))))))
  88. (define gzread!
  89. (let ((proc (zlib-procedure int "gzread" (list '* '* unsigned-int))))
  90. (lambda* (gzfile bv #:optional (start 0) (count (bytevector-length bv)))
  91. "Read up to COUNT bytes from GZFILE into BV at offset START. Return the
  92. number of uncompressed bytes actually read; it is zero if COUNT is zero or if
  93. the end-of-stream has been reached."
  94. (let ((ret (proc (gzip-file->pointer gzfile)
  95. (bytevector->pointer bv start)
  96. count)))
  97. (if (< ret 0)
  98. (throw 'zlib-error 'gzread! ret)
  99. ret)))))
  100. (define gzwrite
  101. (let ((proc (zlib-procedure int "gzwrite" (list '* '* unsigned-int))))
  102. (lambda* (gzfile bv #:optional (start 0) (count (bytevector-length bv)))
  103. "Write up to COUNT bytes from BV at offset START into GZFILE. Return
  104. the number of uncompressed bytes written, a strictly positive integer."
  105. (let ((ret (proc (gzip-file->pointer gzfile)
  106. (bytevector->pointer bv start)
  107. count)))
  108. (if (<= ret 0)
  109. (throw 'zlib-error 'gzwrite ret)
  110. ret)))))
  111. (define gzbuffer!
  112. (let ((proc (zlib-procedure int "gzbuffer" (list '* unsigned-int))))
  113. (lambda (gzfile size)
  114. "Change the internal buffer size of GZFILE to SIZE bytes."
  115. (let ((ret (proc (gzip-file->pointer gzfile) size)))
  116. (unless (zero? ret)
  117. (throw 'zlib-error 'gzbuffer! ret))))))
  118. (define gzeof?
  119. (let ((proc (zlib-procedure int "gzeof" '(*))))
  120. (lambda (gzfile)
  121. "Return true if the end-of-file has been reached on GZFILE."
  122. (not (zero? (proc (gzip-file->pointer gzfile)))))))
  123. (define gzclose
  124. (let ((proc (zlib-procedure int "gzclose" '(*))))
  125. (lambda (gzfile)
  126. "Close GZFILE."
  127. (let ((ret (proc (gzip-file->pointer gzfile))))
  128. (unless (zero? ret)
  129. (throw 'zlib-error 'gzclose ret (gzerror gzfile)))))))
  130. ;;;
  131. ;;; Port interface.
  132. ;;;
  133. (define %default-buffer-size
  134. ;; Default buffer size, as documented in <zlib.h>.
  135. 8192)
  136. (define %default-compression-level
  137. ;; Z_DEFAULT_COMPRESSION.
  138. -1)
  139. (define* (make-gzip-input-port port #:key (buffer-size %default-buffer-size))
  140. "Return an input port that decompresses data read from PORT, a file port. The
  141. new port inherits the encoding of PORT. PORT is automatically closed when the
  142. resulting port is closed. BUFFER-SIZE is the size in bytes of the internal
  143. buffer, 8 KiB by default; using a larger buffer increases decompression speed.
  144. An error is thrown if PORT contains buffered input, which would be lost (and is
  145. lost anyway)."
  146. (define gzfile
  147. (match (drain-input port)
  148. ("" ;PORT's buffer is empty
  149. ;; 'gzclose' will eventually close the file descriptor beneath PORT.
  150. ;; 'close-port' on PORT would get EBADF if 'gzclose' already closed it,
  151. ;; so that's no good; revealed ports are no good either because they
  152. ;; leak (see <https://bugs.gnu.org/28784>); calling 'close-port' after
  153. ;; 'gzclose' doesn't work either because it leads to a race condition
  154. ;; (see <https://bugs.gnu.org/29335>). So we dup and close PORT right
  155. ;; away.
  156. (gzdopen (dup (fileno port)) "r"))
  157. (_
  158. ;; This is unrecoverable but it's better than having the buffered input
  159. ;; be lost, leading to unclear end-of-file or corrupt-data errors down
  160. ;; the path.
  161. (throw 'zlib-error 'make-gzip-input-port
  162. "port contains buffered input" port))))
  163. (define (read! bv start count)
  164. (gzread! gzfile bv start count))
  165. (define encoding (port-encoding port))
  166. (unless (= buffer-size %default-buffer-size)
  167. (gzbuffer! gzfile buffer-size))
  168. (close-port port) ;we no longer need it
  169. (let ((result (make-custom-binary-input-port "gzip-input" read! #f #f
  170. (lambda () (gzclose gzfile)))))
  171. (set-port-encoding! result encoding)
  172. result))
  173. (define* (make-gzip-output-port port
  174. #:key
  175. (level %default-compression-level)
  176. (buffer-size %default-buffer-size))
  177. "Return an output port that compresses data at the given LEVEL, using PORT, a
  178. file port, as its sink. PORT must be a file port; it is automatically closed
  179. when the resulting port is closed. The new port inherits the encoding of PORT."
  180. (define gzfile
  181. (begin
  182. (force-output port) ;empty PORT's buffer
  183. (gzdopen (dup (fileno port))
  184. (string-append "w" (number->string level)))))
  185. (define (write! bv start count)
  186. (gzwrite gzfile bv start count))
  187. (define encoding (port-encoding port))
  188. (unless (= buffer-size %default-buffer-size)
  189. (gzbuffer! gzfile buffer-size))
  190. (close-port port)
  191. (let ((result (make-custom-binary-output-port "gzip-output" write! #f #f
  192. (lambda () (gzclose gzfile)))))
  193. (set-port-encoding! result encoding)
  194. result))
  195. (define* (call-with-gzip-input-port port proc
  196. #:key (buffer-size %default-buffer-size))
  197. "Call PROC with a port that wraps PORT and decompresses data read from it.
  198. PORT must be a file port; it is closed upon completion. The new port inherits
  199. the encoding of PORT. The gzip internal buffer size is set to BUFFER-SIZE
  200. bytes.
  201. See 'call-with-zlib-input-port' for a slightly slower variant that does not
  202. require PORT to be a file port."
  203. (let ((gzip (make-gzip-input-port port #:buffer-size buffer-size)))
  204. (dynamic-wind
  205. (const #t)
  206. (lambda ()
  207. (proc gzip))
  208. (lambda ()
  209. (close-port gzip)))))
  210. (define* (call-with-gzip-output-port port proc
  211. #:key
  212. (level %default-compression-level)
  213. (buffer-size %default-buffer-size))
  214. "Call PROC with an output port that wraps PORT and compresses data. PORT must
  215. be a file port; it is closed upon completion. The new port inherits the
  216. encoding of PORT. The gzip internal buffer size is set to BUFFER-SIZE bytes.
  217. See 'call-with-zlib-output-port' for a slightly slower variant that does not
  218. require PORT to be a file port."
  219. (let ((gzip (make-gzip-output-port port
  220. #:level level
  221. #:buffer-size buffer-size)))
  222. (dynamic-wind
  223. (const #t)
  224. (lambda ()
  225. (proc gzip))
  226. (lambda ()
  227. (close-port gzip)))))
  228. ;;;
  229. ;;; Raw operations, originally from davexunit's guile-zlib
  230. ;;; https://github.com/davexunit/guile-zlib
  231. ;;; fd28b7515efc4af6faf55854993cb0c8bed1f8c5
  232. ;;;
  233. ;;
  234. ;; ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  235. ;; const Bytef *source, uLong sourceLen));
  236. ;;
  237. ;; Decompresses the source buffer into the destination
  238. ;; buffer. sourceLen is the byte length of the source buffer. Upon
  239. ;; entry, destLen is the total size of the destination buffer, which
  240. ;; must be large enough to hold the entire uncompressed data. (The
  241. ;; size of the uncompressed data must have been saved previously by
  242. ;; the compressor and transmitted to the decompressor by some
  243. ;; mechanism outside the scope of this compression library.) Upon
  244. ;; exit, destLen is the actual size of the compressed buffer.
  245. ;;
  246. ;; uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  247. ;; enough memory, Z_BUF_ERROR if there was not enough room in the
  248. ;; output buffer, or Z_DATA_ERROR if the input data was corrupted or
  249. ;; incomplete. In the case where there is not enough room,
  250. ;; uncompress() will fill the output buffer with the uncompressed data
  251. ;; up to that point.
  252. (define %uncompress
  253. (zlib-procedure int "uncompress" (list '* '* '* unsigned-long)))
  254. ;; ZEXTERN int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen,
  255. ;; const Bytef *source, uLong *sourceLen);
  256. ;;
  257. ;; Same as uncompress, except that sourceLen is a pointer, where the length of
  258. ;; the source is *sourceLen. On return, *sourceLen is the number of source bytes
  259. ;; consumed.
  260. (define %uncompress2
  261. (zlib-procedure int "uncompress2" (list '* '* '* '*)))
  262. ;;
  263. ;; ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
  264. ;; const Bytef *source, uLong sourceLen));
  265. ;;
  266. ;; Compresses the source buffer into the destination buffer. sourceLen
  267. ;; is the byte length of the source buffer. Upon entry, destLen is the
  268. ;; total size of the destination buffer, which must be at least the
  269. ;; value returned by compressBound(sourceLen). Upon exit, destLen is
  270. ;; the actual size of the compressed buffer.
  271. ;;
  272. ;; compress returns Z_OK if success, Z_MEM_ERROR if there was not
  273. ;; enough memory, Z_BUF_ERROR if there was not enough room in the
  274. ;; output buffer.
  275. (define %compress
  276. (zlib-procedure int "compress" (list '* '* '* unsigned-long)))
  277. ;;
  278. ;; ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
  279. ;;
  280. ;; compressBound() returns an upper bound on the compressed size after
  281. ;; compress() or compress2() on sourceLen bytes. It would be used
  282. ;; before a compress() or compress2() call to allocate the destination
  283. ;; buffer.
  284. (define %compress-bound
  285. (zlib-procedure unsigned-long "compressBound" (list unsigned-long)))
  286. ;; Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  287. ;; return the updated checksum. If buf is Z_NULL, this function returns the
  288. ;; required initial value for the checksum.
  289. ;;
  290. ;; An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  291. ;; much faster.
  292. ;;
  293. ;; Usage example:
  294. ;;
  295. ;; uLong adler = adler32(0L, Z_NULL, 0);
  296. ;;
  297. ;; while (read_buffer(buffer, length) != EOF) {
  298. ;; adler = adler32(adler, buffer, length);
  299. ;; }
  300. ;; if (adler != original_adler) error();
  301. (define %adler32
  302. (zlib-procedure unsigned-long "adler32" (list unsigned-long '* unsigned-int)))
  303. ;; Update a running CRC-32 with the bytes buf[0..len-1] and return the
  304. ;; updated CRC-32. If buf is Z_NULL, this function returns the required
  305. ;; initial value for the crc. Pre- and post-conditioning (one's complement) is
  306. ;; performed within this function so it shouldn't be done by the application.
  307. ;;
  308. ;; Usage example:
  309. ;;
  310. ;; uLong crc = crc32(0L, Z_NULL, 0);
  311. ;;
  312. ;; while (read_buffer(buffer, length) != EOF) {
  313. ;; crc = crc32(crc, buffer, length);
  314. ;; }
  315. ;; if (crc != original_crc) error();
  316. (define %crc32
  317. (zlib-procedure unsigned-long "crc32" (list unsigned-long '* unsigned-int)))
  318. ;; There is a bit of guesswork involved when creating the bytevectors
  319. ;; to store compressed/uncompressed data in. This procedure provides a
  320. ;; convenient way to copy the portion of a bytevector that was
  321. ;; actually used.
  322. (define (bytevector-copy-region bv start end)
  323. (let* ((length (- end start))
  324. (new-bv (make-bytevector length)))
  325. (bytevector-copy! bv start new-bv 0 length)
  326. new-bv))
  327. ;; uncompress/compress take a bytevector that zlib writes the size of
  328. ;; the returned data to. This procedure saves me a few keystrokes when
  329. ;; fetching that value.
  330. (define (buffer-length bv)
  331. (bytevector-uint-ref bv 0
  332. (native-endianness) (sizeof unsigned-long)))
  333. (define (uncompress bv)
  334. "Uncompresses bytevector and returns a bytevector containing
  335. the uncompressed data."
  336. (define (try-uncompress length)
  337. (let* ((dest (make-bytevector (* (sizeof uint8) length)))
  338. (dest-length (make-bytevector 8))
  339. (source-length (make-bytevector 8)))
  340. (bytevector-u64-set! source-length 0 length (native-endianness))
  341. (bytevector-uint-set! dest-length 0 length
  342. (native-endianness) (sizeof unsigned-long))
  343. (let ((result (%uncompress2 (bytevector->pointer dest)
  344. (bytevector->pointer dest-length)
  345. (bytevector->pointer bv)
  346. (bytevector->pointer source-length))))
  347. (values result
  348. (bytevector-copy-region dest 0 (buffer-length dest-length))
  349. (buffer-length source-length)))))
  350. (let try-again ((tries 1)
  351. (length (inexact->exact (* (bytevector-length bv) 1.5))))
  352. ;; Bail after so many failed attempts. This shouldn't happen, but
  353. ;; I don't like the idea of a potentially unbounded loop that
  354. ;; keeps allocating larger and larger chunks of memory.
  355. (if (> tries 10)
  356. (throw 'zlib-error 'uncompress 0)
  357. (receive (ret-code uncompressed-data read-source-length)
  358. (try-uncompress length)
  359. ;; return code -5 means that destination buffer was too small.
  360. ;; return code 0 means everything went OK.
  361. (cond ((= ret-code -5)
  362. (let* ((compressesed-data-size (bytevector-length bv))
  363. (percent (* (/ read-source-length
  364. compressesed-data-size)
  365. 100))
  366. (next-size (inexact->exact (round (/ (* 100 length)
  367. percent)))))
  368. (try-again (1+ tries) next-size)))
  369. ((= ret-code 0)
  370. uncompressed-data)
  371. (else
  372. (throw 'zlib-error 'uncompress ret-code)))))))
  373. (define (compress bv)
  374. "Compresses bytevector and returns a bytevector containing the compressed data."
  375. (let* ((bv-length (bytevector-length bv))
  376. (dest-length (%compress-bound bv-length))
  377. (dest-bv (make-bytevector dest-length))
  378. (dest-length-bv (make-bytevector (sizeof unsigned-long)))
  379. (ret-code 0))
  380. (bytevector-uint-set! dest-length-bv 0 dest-length
  381. (native-endianness) (sizeof unsigned-long))
  382. (set! ret-code
  383. (%compress (bytevector->pointer dest-bv)
  384. (bytevector->pointer dest-length-bv)
  385. (bytevector->pointer bv)
  386. bv-length))
  387. (if (= ret-code 0)
  388. (bytevector-copy-region dest-bv 0
  389. (buffer-length dest-length-bv))
  390. (throw 'zlib-error 'compress ret-code))))
  391. (define %default-adler32 (%adler32 0 %null-pointer 0))
  392. (define %default-crc32 (%crc32 0 %null-pointer 0))
  393. (define* (adler32 bv #:optional (value %default-adler32))
  394. "Computes adler32 checksum with optional starting value."
  395. (%adler32 value (bytevector->pointer bv) (bytevector-length bv)))
  396. (define* (crc32 bv #:optional (value %default-crc32))
  397. "Computes crc32 checksum with optional starting value."
  398. (%crc32 value (bytevector->pointer bv) (bytevector-length bv)))
  399. ;;;
  400. ;;; Low-level zlib stream API.
  401. ;;;
  402. (define %zlib-version
  403. ;; Library version that we're targeting.
  404. "1.2.11")
  405. ;; struct zstream
  406. (define %stream-struct
  407. (list '* ;next_in
  408. unsigned-int ;avail_in
  409. unsigned-long ;total_in
  410. '* ;next_out
  411. unsigned-int ;avail_out
  412. unsigned-long ;total_out
  413. '* ;msg
  414. '* ;state
  415. '* ;zalloc
  416. '* ;zfree
  417. '* ;opaque
  418. int ;data_type
  419. unsigned-long ;adler
  420. unsigned-long)) ;reserved
  421. (define (offset-of types n)
  422. "Return the offset of the Nth field among TYPES, the list of types of a
  423. struct's fields."
  424. (if (zero? n)
  425. 0
  426. (let* ((base (sizeof (take types n)))
  427. (align (alignof (list-ref types n)))
  428. (mod (modulo base align)))
  429. (if (zero? mod)
  430. base
  431. (+ base (- align mod))))))
  432. (define-syntax-rule (define-stream-getter name index)
  433. "Define NAME as a procedure accessing the INDEXth field of %STREAM-STRUCT."
  434. (define name
  435. (let* ((offset (offset-of %stream-struct index))
  436. (type (list-ref %stream-struct index))
  437. (size (sizeof type)))
  438. (lambda (stream)
  439. (bytevector-uint-ref stream offset (native-endianness)
  440. size)))))
  441. (define-syntax-rule (define-stream-setter name index)
  442. "Define NAME as a procedure setting the INDEXth field of %STREAM-STRUCT."
  443. (define name
  444. (let* ((offset (offset-of %stream-struct index))
  445. (type (list-ref %stream-struct index))
  446. (size (sizeof type)))
  447. (lambda (stream value)
  448. (bytevector-uint-set! stream offset value
  449. (native-endianness) size)))))
  450. (define-stream-getter stream-avail-in 1)
  451. (define-stream-getter stream-avail-out 4)
  452. (define-stream-getter stream-error-message 6)
  453. (define-stream-setter set-stream-next-in! 0)
  454. (define-stream-setter set-stream-avail-in! 1)
  455. (define-stream-setter set-stream-next-out! 3)
  456. (define-stream-setter set-stream-avail-out! 4)
  457. (define (stream-error-message* stream)
  458. "Return the error message associated with STREAM or #f."
  459. (match (stream-error-message stream)
  460. ((? zero?) #f)
  461. (address (pointer->string (make-pointer address)))))
  462. (define inflate!
  463. (let ((proc (zlib-procedure int "inflate" `(* ,int))))
  464. (lambda (stream flush)
  465. (proc stream flush))))
  466. (define deflate!
  467. (let ((proc (zlib-procedure int "deflate" `(* ,int))))
  468. (lambda (stream flush)
  469. (proc stream flush))))
  470. (define (window-bits-for-format format)
  471. ;; Search for "windowBits" in <zlib.h>.
  472. (define MAX_WBITS 15) ;<zconf.h>
  473. (match format
  474. ('deflate (- MAX_WBITS)) ;raw deflate
  475. ('zlib MAX_WBITS) ;zlib header
  476. ('gzip (+ MAX_WBITS 16)))) ;gzip header
  477. (define inflate-init!
  478. (let ((proc (zlib-procedure int "inflateInit2_" `(* ,int * ,int))))
  479. (lambda (stream window-bits)
  480. (let ((ret (proc stream window-bits
  481. (string->pointer %zlib-version)
  482. (sizeof %stream-struct))))
  483. (unless (zero? ret)
  484. (throw 'zlib-error 'inflate-init! ret))))))
  485. (define deflate-init!
  486. (let ((proc (zlib-procedure int "deflateInit2_" `(* ,int ,int ,int ,int
  487. ,int * ,int))))
  488. (lambda* (stream level
  489. #:key
  490. (window-bits (window-bits-for-format 'zlib))
  491. (memory-level 8)
  492. (strategy Z_DEFAULT_STRATEGY))
  493. (let ((ret (proc stream level Z_DEFLATED
  494. window-bits memory-level strategy
  495. (string->pointer %zlib-version)
  496. (sizeof %stream-struct))))
  497. (unless (zero? ret)
  498. (throw 'zlib-error 'deflate-init! ret))))))
  499. (define inflate-end!
  500. (let ((proc (zlib-procedure int "inflateEnd" '(*))))
  501. (lambda (stream)
  502. (let ((ret (proc stream)))
  503. (unless (zero? ret)
  504. (throw 'zlib-error 'inflate-end! ret))))))
  505. (define deflate-end!
  506. (let ((proc (zlib-procedure int "deflateEnd" '(*))))
  507. (lambda (stream)
  508. (let ((ret (proc stream)))
  509. (unless (zero? ret)
  510. (throw 'zlib-error 'deflate-end! ret))))))
  511. ;; Error codes.
  512. (define Z_OK 0)
  513. (define Z_STREAM_END 1)
  514. (define Z_NEED_DICT 2)
  515. (define Z_ERRNO -1)
  516. (define Z_STREAM_ERROR -2)
  517. (define Z_DATA_ERROR -3)
  518. (define Z_MEM_ERROR -4)
  519. (define Z_BUF_ERROR -5)
  520. ;; Flush flags.
  521. (define Z_NO_FLUSH 0)
  522. (define Z_PARTIAL_FLUSH 1)
  523. (define Z_SYNC_FLUSH 2)
  524. (define Z_FULL_FLUSH 3)
  525. (define Z_FINISH 4)
  526. ;; 'deflate-init!' flags.
  527. (define Z_DEFLATED 8)
  528. (define Z_DEFAULT_STRATEGY 0)
  529. (define* (make-zlib-input-port port
  530. #:key
  531. (format 'zlib)
  532. (buffer-size %default-buffer-size)
  533. (close? #t))
  534. "Return an input port that decompresses data read from PORT. FORMAT is a
  535. symbol denoting the header format; it must be one of 'deflate (RFC 1950), 'zlib
  536. (RFC 1951), or 'gzip (RFC 1952). The new port inherits the encoding of PORT.
  537. When CLOSE? is true, PORT is automatically closed when the resulting port is
  538. closed."
  539. (define input-buffer (make-bytevector buffer-size))
  540. ;; Instead of writing uncompressed data directly to the user-provided
  541. ;; buffer, keep a large-enough buffer. That way, we know we cannot stumble
  542. ;; into Z_BUF_ERROR because of insufficient output space.
  543. (define output-buffer (make-bytevector %default-buffer-size))
  544. (define buffered 0)
  545. (define offset 0)
  546. (define eof? #f)
  547. (define stream (make-bytevector (sizeof %stream-struct)))
  548. (define pointer
  549. (let ((ptr (bytevector->pointer stream)))
  550. (lambda (bv)
  551. (if (eq? bv stream)
  552. ptr
  553. (bytevector->pointer bv)))))
  554. (define (read! bv start count)
  555. (cond ((> buffered 0)
  556. (let ((n (min count buffered)))
  557. (bytevector-copy! output-buffer offset bv start n)
  558. (set! buffered (- buffered n))
  559. (set! offset (+ offset n))
  560. n))
  561. (eof? 0)
  562. (else
  563. (set! offset 0)
  564. (set-stream-next-out! stream
  565. (pointer-address
  566. (bytevector->pointer output-buffer)))
  567. (set-stream-avail-out! stream (bytevector-length output-buffer))
  568. (let loop ((ret Z_OK)
  569. (flush? #f))
  570. (if (and (not flush?)
  571. (or (zero? (stream-avail-in stream))
  572. (= Z_BUF_ERROR ret)))
  573. (let ((n (get-bytevector-n! port input-buffer
  574. 0 buffer-size)))
  575. (if (eof-object? n)
  576. (loop ret #t)
  577. (begin
  578. (set-stream-next-in! stream
  579. (pointer-address
  580. (bytevector->pointer input-buffer)))
  581. (set-stream-avail-in! stream n)
  582. (loop ret flush?))))
  583. (let ((ret (inflate! (pointer stream)
  584. (if flush? Z_SYNC_FLUSH 0))))
  585. (set! buffered (- (bytevector-length output-buffer)
  586. (stream-avail-out stream)))
  587. (cond ((= ret Z_OK)
  588. (read! bv start count))
  589. ((= ret Z_STREAM_END)
  590. (set! eof? #t)
  591. (read! bv start count))
  592. ((and (not flush?) (= Z_BUF_ERROR ret))
  593. (loop ret flush?))
  594. (else
  595. (throw 'zlib-error ret
  596. (stream-error-message* stream))))))))))
  597. (define encoding (port-encoding port))
  598. (define result
  599. (make-custom-binary-input-port "zlib-input" read! #f #f
  600. (lambda ()
  601. (inflate-end! (pointer stream))
  602. (when close?
  603. (close-port port)))))
  604. ;; No need for extra buffering.
  605. (cond-expand ((or guile-2.2 guile-3.0)
  606. (setvbuf result 'none))
  607. (else #t)) ;not possible on 2.0
  608. (inflate-init! (pointer stream)
  609. (window-bits-for-format format))
  610. (set-stream-avail-in! stream 0)
  611. (set-port-encoding! result encoding)
  612. result)
  613. (define* (make-zlib-output-port port
  614. #:key
  615. (format 'zlib)
  616. (buffer-size %default-buffer-size)
  617. (level %default-compression-level)
  618. (close? #t))
  619. "Return an output port that compresses data at the given LEVEL, using PORT as
  620. its sink. FORMAT is a symbol denoting the header format; it must be one of
  621. 'deflate (RFC 1950), 'zlib (RFC 1951), or 'gzip (RFC 1952). The new port
  622. inherits the encoding of PORT.
  623. When FORMAT is 'gzip, the gzip header takes default values, and in particular
  624. no modification time and no file name.
  625. When CLOSE? is true, PORT is automatically closed when the resulting port is
  626. closed."
  627. (define output-buffer (make-bytevector buffer-size))
  628. (define stream (make-bytevector (sizeof %stream-struct)))
  629. (define pointer
  630. (let ((ptr (bytevector->pointer stream)))
  631. (lambda (bv)
  632. (if (eq? bv stream)
  633. ptr
  634. (bytevector->pointer bv)))))
  635. (define (output-compressed-data! stream)
  636. (put-bytevector port output-buffer 0
  637. (- buffer-size (stream-avail-out stream)))
  638. (set-stream-avail-out! stream buffer-size)
  639. (set-stream-next-out! stream
  640. (pointer-address
  641. (bytevector->pointer output-buffer))))
  642. (define* (write! bv start count #:optional flush?)
  643. (set-stream-next-in! stream (+ start (pointer-address
  644. (bytevector->pointer bv))))
  645. (set-stream-avail-in! stream count)
  646. (let loop ()
  647. (if (zero? (stream-avail-out stream))
  648. (begin
  649. (output-compressed-data! stream)
  650. (loop))
  651. (let ((ret (deflate! (pointer stream)
  652. (if flush? Z_FINISH Z_NO_FLUSH))))
  653. (cond ((= ret Z_BUF_ERROR)
  654. (loop))
  655. ((= ret Z_OK)
  656. (match (- count (stream-avail-in stream))
  657. (0 (loop)) ;zero would mean EOF
  658. (n n)))
  659. ((and flush? (= ret Z_STREAM_END))
  660. (- count (stream-avail-in stream)))
  661. (else
  662. (throw 'zlib-error 'deflate! ret
  663. (stream-error-message* stream))))))))
  664. (define (flush)
  665. (write! #vu8() 0 0 #t)
  666. (output-compressed-data! stream))
  667. (define (close)
  668. (flush)
  669. (deflate-end! (pointer stream))
  670. (when close?
  671. (close-port port)))
  672. (define encoding (port-encoding port))
  673. (deflate-init! (pointer stream) level
  674. #:window-bits (window-bits-for-format format))
  675. (set-stream-avail-out! stream buffer-size)
  676. (set-stream-next-out! stream
  677. (pointer-address (bytevector->pointer output-buffer)))
  678. (let ((result (make-custom-binary-output-port "zlib-output" write! #f #f close)))
  679. (set-port-encoding! result encoding)
  680. result))
  681. (define* (call-with-zlib-input-port port proc
  682. #:key
  683. (format 'zlib)
  684. (buffer-size %default-buffer-size))
  685. "Call PROC with a port that wraps PORT and decompresses data read from it.
  686. PORT is closed upon completion. The zlib internal buffer size is set to
  687. BUFFER-SIZE bytes."
  688. (let ((zlib (make-zlib-input-port port
  689. #:format format
  690. #:buffer-size buffer-size
  691. #:close? #t)))
  692. (dynamic-wind
  693. (const #t)
  694. (lambda ()
  695. (proc zlib))
  696. (lambda ()
  697. (close-port zlib)))))
  698. (define* (call-with-zlib-output-port port proc
  699. #:key
  700. (format 'zlib)
  701. (level %default-compression-level)
  702. (buffer-size %default-buffer-size))
  703. "Call PROC with an output port that wraps PORT and compresses data in the
  704. given FORMAT, with the given LEVEL. PORT is closed upon completion. The
  705. zlib internal buffer size is set to BUFFER-SIZE bytes."
  706. (let ((zlib (make-zlib-output-port port
  707. #:format format
  708. #:level level
  709. #:buffer-size buffer-size
  710. #:close? #t)))
  711. (dynamic-wind
  712. (const #t)
  713. (lambda ()
  714. (proc zlib))
  715. (lambda ()
  716. (close-port zlib)))))
  717. ;;; zlib.scm ends here