zlib.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. tune-zlib-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.
  141. PORT is automatically closed when the resulting port is closed. BUFFER-SIZE
  142. is the size in bytes of the internal buffer, 8 KiB by default; using a larger
  143. buffer increases decompression speed. An error is thrown if PORT contains
  144. buffered input, which would be lost (and is 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. (unless (= buffer-size %default-buffer-size)
  165. (gzbuffer! gzfile buffer-size))
  166. (close-port port) ;we no longer need it
  167. (make-custom-binary-input-port "gzip-input" read! #f #f
  168. (lambda ()
  169. (gzclose gzfile))))
  170. (define* (make-gzip-output-port port
  171. #:key
  172. (level %default-compression-level)
  173. (buffer-size %default-buffer-size))
  174. "Return an output port that compresses data at the given LEVEL, using PORT,
  175. a file port, as its sink. PORT must be a file port; it is automatically
  176. closed when the resulting port is closed."
  177. (define gzfile
  178. (begin
  179. (force-output port) ;empty PORT's buffer
  180. (gzdopen (dup (fileno port))
  181. (string-append "w" (number->string level)))))
  182. (define (write! bv start count)
  183. (gzwrite gzfile bv start count))
  184. (unless (= buffer-size %default-buffer-size)
  185. (gzbuffer! gzfile buffer-size))
  186. (close-port port)
  187. (make-custom-binary-output-port "gzip-output" write! #f #f
  188. (lambda ()
  189. (gzclose gzfile))))
  190. (define* (call-with-gzip-input-port port proc
  191. #:key (buffer-size %default-buffer-size))
  192. "Call PROC with a port that wraps PORT and decompresses data read from it.
  193. PORT must be a file port; it is closed upon completion. The gzip internal
  194. buffer size is set to BUFFER-SIZE bytes.
  195. See 'call-with-zlib-input-port' for a slightly slower variant that does not
  196. require PORT to be a file port."
  197. (let ((gzip (make-gzip-input-port port #:buffer-size buffer-size)))
  198. (dynamic-wind
  199. (const #t)
  200. (lambda ()
  201. (proc gzip))
  202. (lambda ()
  203. (close-port gzip)))))
  204. (define* (call-with-gzip-output-port port proc
  205. #:key
  206. (level %default-compression-level)
  207. (buffer-size %default-buffer-size))
  208. "Call PROC with an output port that wraps PORT and compresses data. PORT
  209. must be a file port; it is closed upon completion. The gzip internal buffer
  210. size is set to BUFFER-SIZE bytes.
  211. See 'call-with-zlib-output-port' for a slightly slower variant that does not
  212. require PORT to be a file port."
  213. (let ((gzip (make-gzip-output-port port
  214. #:level level
  215. #:buffer-size buffer-size)))
  216. (dynamic-wind
  217. (const #t)
  218. (lambda ()
  219. (proc gzip))
  220. (lambda ()
  221. (close-port gzip)))))
  222. ;;;
  223. ;;; Raw operations, originally from davexunit's guile-zlib
  224. ;;; https://github.com/davexunit/guile-zlib
  225. ;;; fd28b7515efc4af6faf55854993cb0c8bed1f8c5
  226. ;;;
  227. ;;
  228. ;; ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  229. ;; const Bytef *source, uLong sourceLen));
  230. ;;
  231. ;; Decompresses the source buffer into the destination
  232. ;; buffer. sourceLen is the byte length of the source buffer. Upon
  233. ;; entry, destLen is the total size of the destination buffer, which
  234. ;; must be large enough to hold the entire uncompressed data. (The
  235. ;; size of the uncompressed data must have been saved previously by
  236. ;; the compressor and transmitted to the decompressor by some
  237. ;; mechanism outside the scope of this compression library.) Upon
  238. ;; exit, destLen is the actual size of the compressed buffer.
  239. ;;
  240. ;; uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  241. ;; enough memory, Z_BUF_ERROR if there was not enough room in the
  242. ;; output buffer, or Z_DATA_ERROR if the input data was corrupted or
  243. ;; incomplete. In the case where there is not enough room,
  244. ;; uncompress() will fill the output buffer with the uncompressed data
  245. ;; up to that point.
  246. (define %uncompress
  247. (zlib-procedure int "uncompress" (list '* '* '* unsigned-long)))
  248. ;;
  249. ;; ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
  250. ;; const Bytef *source, uLong sourceLen));
  251. ;;
  252. ;; Compresses the source buffer into the destination buffer. sourceLen
  253. ;; is the byte length of the source buffer. Upon entry, destLen is the
  254. ;; total size of the destination buffer, which must be at least the
  255. ;; value returned by compressBound(sourceLen). Upon exit, destLen is
  256. ;; the actual size of the compressed buffer.
  257. ;;
  258. ;; compress returns Z_OK if success, Z_MEM_ERROR if there was not
  259. ;; enough memory, Z_BUF_ERROR if there was not enough room in the
  260. ;; output buffer.
  261. (define %compress
  262. (zlib-procedure int "compress" (list '* '* '* unsigned-long)))
  263. ;;
  264. ;; ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
  265. ;;
  266. ;; compressBound() returns an upper bound on the compressed size after
  267. ;; compress() or compress2() on sourceLen bytes. It would be used
  268. ;; before a compress() or compress2() call to allocate the destination
  269. ;; buffer.
  270. (define %compress-bound
  271. (zlib-procedure unsigned-long "compressBound" (list unsigned-long)))
  272. ;; Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  273. ;; return the updated checksum. If buf is Z_NULL, this function returns the
  274. ;; required initial value for the checksum.
  275. ;;
  276. ;; An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  277. ;; much faster.
  278. ;;
  279. ;; Usage example:
  280. ;;
  281. ;; uLong adler = adler32(0L, Z_NULL, 0);
  282. ;;
  283. ;; while (read_buffer(buffer, length) != EOF) {
  284. ;; adler = adler32(adler, buffer, length);
  285. ;; }
  286. ;; if (adler != original_adler) error();
  287. (define %adler32
  288. (zlib-procedure unsigned-long "adler32" (list unsigned-long '* unsigned-int)))
  289. ;; Update a running CRC-32 with the bytes buf[0..len-1] and return the
  290. ;; updated CRC-32. If buf is Z_NULL, this function returns the required
  291. ;; initial value for the crc. Pre- and post-conditioning (one's complement) is
  292. ;; performed within this function so it shouldn't be done by the application.
  293. ;;
  294. ;; Usage example:
  295. ;;
  296. ;; uLong crc = crc32(0L, Z_NULL, 0);
  297. ;;
  298. ;; while (read_buffer(buffer, length) != EOF) {
  299. ;; crc = crc32(crc, buffer, length);
  300. ;; }
  301. ;; if (crc != original_crc) error();
  302. (define %crc32
  303. (zlib-procedure unsigned-long "crc32" (list unsigned-long '* unsigned-int)))
  304. ;; There is a bit of guesswork involved when creating the bytevectors
  305. ;; to store compressed/uncompressed data in. This procedure provides a
  306. ;; convenient way to copy the portion of a bytevector that was
  307. ;; actually used.
  308. (define (bytevector-copy-region bv start end)
  309. (let* ((length (- end start))
  310. (new-bv (make-bytevector length)))
  311. (bytevector-copy! bv start new-bv 0 length)
  312. new-bv))
  313. ;; uncompress/compress take a bytevector that zlib writes the size of
  314. ;; the returned data to. This procedure saves me a few keystrokes when
  315. ;; fetching that value.
  316. (define (buffer-length bv)
  317. (bytevector-uint-ref bv 0
  318. (native-endianness) (sizeof unsigned-long)))
  319. (define (uncompress bv)
  320. "Uncompresses bytevector and returns a bytevector containing
  321. the uncompressed data."
  322. (define (try-uncompress length)
  323. (let* ((dest (make-bytevector (* (sizeof uint8) length)))
  324. (dest-length (make-bytevector (sizeof unsigned-long))))
  325. (bytevector-uint-set! dest-length 0 length
  326. (native-endianness) (sizeof unsigned-long))
  327. (values (%uncompress (bytevector->pointer dest)
  328. (bytevector->pointer dest-length)
  329. (bytevector->pointer bv)
  330. length)
  331. (bytevector-copy-region dest 0 (buffer-length dest-length)))))
  332. ;; We don't know how much space we need to store the uncompressed
  333. ;; data. So, we make an initial guess and keep increasing buffer
  334. ;; size until it works.
  335. (define (step-buffer-length length)
  336. (inexact->exact (round (* length 1.5))))
  337. (let try-again ((tries 1)
  338. (length (step-buffer-length (bytevector-length bv))))
  339. ;; Bail after so many failed attempts. This shouldn't happen, but
  340. ;; I don't like the idea of a potentially unbounded loop that
  341. ;; keeps allocating larger and larger chunks of memory.
  342. (if (> tries 10)
  343. (throw 'zlib-error 'uncompress 0)
  344. (receive (ret-code uncompressed-data)
  345. (try-uncompress length)
  346. ;; return code -5 means that destination buffer was too small.
  347. ;; return code 0 means everything went OK.
  348. (cond ((= ret-code -5)
  349. (try-again (1+ tries) (step-buffer-length length)))
  350. ((= ret-code 0)
  351. uncompressed-data)
  352. (else
  353. (throw 'zlib-error 'uncompress ret-code)))))))
  354. (define (compress bv)
  355. "Compresses bytevector and returns a bytevector containing the compressed data."
  356. (let* ((bv-length (bytevector-length bv))
  357. (dest-length (%compress-bound bv-length))
  358. (dest-bv (make-bytevector dest-length))
  359. (dest-length-bv (make-bytevector (sizeof unsigned-long)))
  360. (ret-code 0))
  361. (bytevector-uint-set! dest-length-bv 0 dest-length
  362. (native-endianness) (sizeof unsigned-long))
  363. (set! ret-code
  364. (%compress (bytevector->pointer dest-bv)
  365. (bytevector->pointer dest-length-bv)
  366. (bytevector->pointer bv)
  367. bv-length))
  368. (if (= ret-code 0)
  369. (bytevector-copy-region dest-bv 0
  370. (buffer-length dest-length-bv))
  371. (throw 'zlib-error 'compress ret-code))))
  372. (define %default-adler32 (%adler32 0 %null-pointer 0))
  373. (define %default-crc32 (%crc32 0 %null-pointer 0))
  374. (define* (adler32 bv #:optional (value %default-adler32))
  375. "Computes adler32 checksum with optional starting value."
  376. (%adler32 value (bytevector->pointer bv) (bytevector-length bv)))
  377. (define* (crc32 bv #:optional (value %default-crc32))
  378. "Computes crc32 checksum with optional starting value."
  379. (%crc32 value (bytevector->pointer bv) (bytevector-length bv)))
  380. ;;;
  381. ;;; Low-level zlib stream API.
  382. ;;;
  383. (define %zlib-version
  384. ;; Library version that we're targeting.
  385. "1.2.11")
  386. ;; struct zstream
  387. (define %stream-struct
  388. (list '* ;next_in
  389. unsigned-int ;avail_in
  390. unsigned-long ;total_in
  391. '* ;next_out
  392. unsigned-int ;avail_out
  393. unsigned-long ;total_out
  394. '* ;msg
  395. '* ;state
  396. '* ;zalloc
  397. '* ;zfree
  398. '* ;opaque
  399. int ;data_type
  400. unsigned-long ;adler
  401. unsigned-long)) ;reserved
  402. (define (offset-of types n)
  403. "Return the offset of the Nth field among TYPES, the list of types of a
  404. struct's fields."
  405. (if (zero? n)
  406. 0
  407. (let* ((base (sizeof (take types n)))
  408. (align (alignof (list-ref types n)))
  409. (mod (modulo base align)))
  410. (if (zero? mod)
  411. base
  412. (+ base (- align mod))))))
  413. (define-syntax-rule (define-stream-getter name index)
  414. "Define NAME as a procedure accessing the INDEXth field of %STREAM-STRUCT."
  415. (define name
  416. (let* ((offset (offset-of %stream-struct index))
  417. (type (list-ref %stream-struct index))
  418. (size (sizeof type)))
  419. (lambda (stream)
  420. (bytevector-uint-ref stream offset (native-endianness)
  421. size)))))
  422. (define-syntax-rule (define-stream-setter name index)
  423. "Define NAME as a procedure setting the INDEXth field of %STREAM-STRUCT."
  424. (define name
  425. (let* ((offset (offset-of %stream-struct index))
  426. (type (list-ref %stream-struct index))
  427. (size (sizeof type)))
  428. (lambda (stream value)
  429. (bytevector-uint-set! stream offset value
  430. (native-endianness) size)))))
  431. (define-stream-getter stream-avail-in 1)
  432. (define-stream-getter stream-avail-out 4)
  433. (define-stream-getter stream-error-message 6)
  434. (define-stream-setter set-stream-next-in! 0)
  435. (define-stream-setter set-stream-avail-in! 1)
  436. (define-stream-setter set-stream-next-out! 3)
  437. (define-stream-setter set-stream-avail-out! 4)
  438. (define (stream-error-message* stream)
  439. "Return the error message associated with STREAM or #f."
  440. (match (stream-error-message stream)
  441. ((? zero?) #f)
  442. (address (pointer->string (make-pointer address)))))
  443. (define inflate!
  444. (let ((proc (zlib-procedure int "inflate" `(* ,int))))
  445. (lambda (stream flush)
  446. (proc stream flush))))
  447. (define deflate!
  448. (let ((proc (zlib-procedure int "deflate" `(* ,int))))
  449. (lambda (stream flush)
  450. (proc stream flush))))
  451. (define (window-bits-for-format format)
  452. ;; Search for "windowBits" in <zlib.h>.
  453. (define MAX_WBITS 15) ;<zconf.h>
  454. (match format
  455. ('deflate (- MAX_WBITS)) ;raw deflate
  456. ('zlib MAX_WBITS) ;zlib header
  457. ('gzip (+ MAX_WBITS 16)))) ;gzip header
  458. (define inflate-init!
  459. (let ((proc (zlib-procedure int "inflateInit2_" `(* ,int * ,int))))
  460. (lambda (stream window-bits)
  461. (let ((ret (proc stream window-bits
  462. (string->pointer %zlib-version)
  463. (sizeof %stream-struct))))
  464. (unless (zero? ret)
  465. (throw 'zlib-error 'inflate-init! ret))))))
  466. (define deflate-init!
  467. (let ((proc (zlib-procedure int "deflateInit2_" `(* ,int ,int ,int ,int
  468. ,int * ,int))))
  469. (lambda* (stream level
  470. #:key
  471. (window-bits (window-bits-for-format 'zlib))
  472. (memory-level 8)
  473. (strategy Z_DEFAULT_STRATEGY))
  474. (let ((ret (proc stream level Z_DEFLATED
  475. window-bits memory-level strategy
  476. (string->pointer %zlib-version)
  477. (sizeof %stream-struct))))
  478. (unless (zero? ret)
  479. (throw 'zlib-error 'deflate-init! ret))))))
  480. (define inflate-end!
  481. (let ((proc (zlib-procedure int "inflateEnd" '(*))))
  482. (lambda (stream)
  483. (let ((ret (proc stream)))
  484. (unless (zero? ret)
  485. (throw 'zlib-error 'inflate-end! ret))))))
  486. (define deflate-end!
  487. (let ((proc (zlib-procedure int "deflateEnd" '(*))))
  488. (lambda (stream)
  489. (let ((ret (proc stream)))
  490. (unless (zero? ret)
  491. (throw 'zlib-error 'deflate-end! ret))))))
  492. ;; Error codes.
  493. (define Z_OK 0)
  494. (define Z_STREAM_END 1)
  495. (define Z_NEED_DICT 2)
  496. (define Z_ERRNO -1)
  497. (define Z_STREAM_ERROR -2)
  498. (define Z_DATA_ERROR -3)
  499. (define Z_MEM_ERROR -4)
  500. (define Z_BUF_ERROR -5)
  501. ;; Flush flags.
  502. (define Z_NO_FLUSH 0)
  503. (define Z_PARTIAL_FLUSH 1)
  504. (define Z_SYNC_FLUSH 2)
  505. (define Z_FULL_FLUSH 3)
  506. (define Z_FINISH 4)
  507. ;; 'deflate-init!' flags.
  508. (define Z_DEFLATED 8)
  509. (define Z_DEFAULT_STRATEGY 0)
  510. (define port-zlib-stream (make-object-property))
  511. (define tune-zlib-port!
  512. (let ((proc (zlib-procedure int "deflateTune" `(* ,int ,int ,int ,int))))
  513. (lambda (port good-length max-lazy nice-length max-chain)
  514. "Tune some very arcane parameters for the underlying compressor of
  515. PORT, a zlib port. This procedure assumes knowledge of the zlib source
  516. code."
  517. (let* ((stream (bytevector->pointer (port-zlib-stream port)))
  518. (ret (proc stream good-length max-lazy nice-length max-chain)))
  519. (unless (zero? ret)
  520. (throw 'zlib-error 'tune-zlib-port! ret))))))
  521. (define* (make-zlib-input-port port
  522. #:key
  523. (format 'zlib)
  524. (buffer-size %default-buffer-size)
  525. (close? #t))
  526. "Return an input port that decompresses data read from PORT. FORMAT is a
  527. symbol denoting the header format; it must be one of 'deflate (RFC 1950),
  528. 'zlib (RFC 1951), or 'gzip (RFC 1952).
  529. When CLOSE? is true, PORT is automatically closed when the resulting port is
  530. closed."
  531. (define input-buffer (make-bytevector buffer-size))
  532. ;; Instead of writing uncompressed data directly to the user-provided
  533. ;; buffer, keep a large-enough buffer. That way, we know we cannot stumble
  534. ;; into Z_BUF_ERROR because of insufficient output space.
  535. (define output-buffer (make-bytevector %default-buffer-size))
  536. (define buffered 0)
  537. (define offset 0)
  538. (define eof? #f)
  539. (define stream (make-bytevector (sizeof %stream-struct)))
  540. (define pointer
  541. (let ((ptr (bytevector->pointer stream)))
  542. (lambda (bv)
  543. (if (eq? bv stream)
  544. ptr
  545. (bytevector->pointer bv)))))
  546. (define (read! bv start count)
  547. (cond ((> buffered 0)
  548. (let ((n (min count buffered)))
  549. (bytevector-copy! output-buffer offset bv start n)
  550. (set! buffered (- buffered n))
  551. (set! offset (+ offset n))
  552. n))
  553. (eof? 0)
  554. (else
  555. (set! offset 0)
  556. (set-stream-next-out! stream
  557. (pointer-address
  558. (bytevector->pointer output-buffer)))
  559. (set-stream-avail-out! stream (bytevector-length output-buffer))
  560. (let loop ((ret Z_OK)
  561. (flush? #f))
  562. (if (and (not flush?)
  563. (or (zero? (stream-avail-in stream))
  564. (= Z_BUF_ERROR ret)))
  565. (let ((n (get-bytevector-n! port input-buffer
  566. 0 buffer-size)))
  567. (if (eof-object? n)
  568. (loop ret #t)
  569. (begin
  570. (set-stream-next-in! stream
  571. (pointer-address
  572. (bytevector->pointer input-buffer)))
  573. (set-stream-avail-in! stream n)
  574. (loop ret flush?))))
  575. (let ((ret (inflate! (pointer stream)
  576. (if flush? Z_SYNC_FLUSH 0))))
  577. (set! buffered (- (bytevector-length output-buffer)
  578. (stream-avail-out stream)))
  579. (cond ((= ret Z_OK)
  580. (read! bv start count))
  581. ((= ret Z_STREAM_END)
  582. (set! eof? #t)
  583. (read! bv start count))
  584. ((and (not flush?) (= Z_BUF_ERROR ret))
  585. (loop ret flush?))
  586. (else
  587. (throw 'zlib-error ret
  588. (stream-error-message* stream))))))))))
  589. (define result
  590. (make-custom-binary-input-port "zlib-input" read! #f #f
  591. (lambda ()
  592. (inflate-end! (pointer stream))
  593. (when close?
  594. (close-port port)))))
  595. ;; No need for extra buffering.
  596. (cond-expand ((or guile-2.2 guile-3.0)
  597. (setvbuf result 'none))
  598. (else #t)) ;not possible on 2.0
  599. (inflate-init! (pointer stream)
  600. (window-bits-for-format format))
  601. (set-stream-avail-in! stream 0)
  602. (set! (port-zlib-stream result) stream)
  603. result)
  604. (define* (make-zlib-output-port port
  605. #:key
  606. (format 'zlib)
  607. (buffer-size %default-buffer-size)
  608. (level %default-compression-level)
  609. (close? #t))
  610. "Return an output port that compresses data at the given LEVEL, using PORT
  611. as its sink. FORMAT is a symbol denoting the header format; it must be one
  612. of 'deflate (RFC 1950), 'zlib (RFC 1951), or 'gzip (RFC 1952).
  613. When FORMAT is 'gzip, the gzip header takes default values, and in particular
  614. no modification time and no file name.
  615. When CLOSE? is true, PORT is automatically closed when the resulting port is
  616. closed."
  617. (define output-buffer (make-bytevector buffer-size))
  618. (define stream (make-bytevector (sizeof %stream-struct)))
  619. (define pointer
  620. (let ((ptr (bytevector->pointer stream)))
  621. (lambda (bv)
  622. (if (eq? bv stream)
  623. ptr
  624. (bytevector->pointer bv)))))
  625. (define (output-compressed-data! stream)
  626. (put-bytevector port output-buffer 0
  627. (- buffer-size (stream-avail-out stream)))
  628. (set-stream-avail-out! stream buffer-size)
  629. (set-stream-next-out! stream
  630. (pointer-address
  631. (bytevector->pointer output-buffer))))
  632. (define* (write! bv start count #:optional flush?)
  633. (set-stream-next-in! stream (+ start (pointer-address
  634. (bytevector->pointer bv))))
  635. (set-stream-avail-in! stream count)
  636. (let loop ()
  637. (if (zero? (stream-avail-out stream))
  638. (begin
  639. (output-compressed-data! stream)
  640. (loop))
  641. (let ((ret (deflate! (pointer stream)
  642. (if flush? Z_FINISH Z_NO_FLUSH))))
  643. (cond ((= ret Z_BUF_ERROR)
  644. (loop))
  645. ((= ret Z_OK)
  646. (match (- count (stream-avail-in stream))
  647. (0 (loop)) ;zero would mean EOF
  648. (n n)))
  649. ((and flush? (= ret Z_STREAM_END))
  650. (- count (stream-avail-in stream)))
  651. (else
  652. (throw 'zlib-error 'deflate! ret
  653. (stream-error-message* stream))))))))
  654. (define (flush)
  655. (write! #vu8() 0 0 #t)
  656. (output-compressed-data! stream))
  657. (define (close)
  658. (flush)
  659. (deflate-end! (pointer stream))
  660. (when close?
  661. (close-port port)))
  662. (deflate-init! (pointer stream) level
  663. #:window-bits (window-bits-for-format format))
  664. (set-stream-avail-out! stream buffer-size)
  665. (set-stream-next-out! stream
  666. (pointer-address (bytevector->pointer output-buffer)))
  667. (define result
  668. (make-custom-binary-output-port "zlib-output" write! #f #f close))
  669. (set! (port-zlib-stream result) stream)
  670. result)
  671. (define* (call-with-zlib-input-port port proc
  672. #:key
  673. (format 'zlib)
  674. (buffer-size %default-buffer-size))
  675. "Call PROC with a port that wraps PORT and decompresses data read from it.
  676. PORT is closed upon completion. The zlib internal buffer size is set to
  677. BUFFER-SIZE bytes."
  678. (let ((zlib (make-zlib-input-port port
  679. #:format format
  680. #:buffer-size buffer-size
  681. #:close? #t)))
  682. (dynamic-wind
  683. (const #t)
  684. (lambda ()
  685. (proc zlib))
  686. (lambda ()
  687. (close-port zlib)))))
  688. (define* (call-with-zlib-output-port port proc
  689. #:key
  690. (format 'zlib)
  691. (level %default-compression-level)
  692. (buffer-size %default-buffer-size))
  693. "Call PROC with an output port that wraps PORT and compresses data in the
  694. given FORMAT, with the given LEVEL. PORT is closed upon completion. The
  695. zlib internal buffer size is set to BUFFER-SIZE bytes."
  696. (let ((zlib (make-zlib-output-port port
  697. #:format format
  698. #:level level
  699. #:buffer-size buffer-size
  700. #:close? #t)))
  701. (dynamic-wind
  702. (const #t)
  703. (lambda ()
  704. (proc zlib))
  705. (lambda ()
  706. (close-port zlib)))))
  707. ;;; zlib.scm ends here