zlib.scm 30 KB

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