lzlib.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Pierre Neidhardt <mail@ambrevar.xyz>
  3. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix lzlib)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (rnrs arithmetic bitwise)
  22. #:use-module (ice-9 binary-ports)
  23. #:use-module (ice-9 match)
  24. #:use-module (system foreign)
  25. #:use-module (guix config)
  26. #:use-module (srfi srfi-11)
  27. #:export (lzlib-available?
  28. make-lzip-input-port
  29. make-lzip-output-port
  30. make-lzip-input-port/compressed
  31. call-with-lzip-input-port
  32. call-with-lzip-output-port
  33. %default-member-length-limit
  34. %default-compression-level))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; Bindings to the lzlib / liblz API. Some convenience functions are also
  38. ;;; provided (see the export).
  39. ;;;
  40. ;;; While the bindings are complete, the convenience functions only support
  41. ;;; single member archives. To decompress single member archives, we loop
  42. ;;; until lz-decompress-read returns 0. This is simpler. To support multiple
  43. ;;; members properly, we need (among others) to call lz-decompress-finish and
  44. ;;; loop over lz-decompress-read until lz-decompress-finished? returns #t.
  45. ;;; Otherwise a multi-member archive starting with an empty member would only
  46. ;;; decompress the empty member and stop there, resulting in truncated output.
  47. ;;; Code:
  48. (define %lzlib
  49. ;; File name of lzlib's shared library. When updating via 'guix pull',
  50. ;; '%liblz' might be undefined so protect against it.
  51. (delay (dynamic-link (if (defined? '%liblz)
  52. %liblz
  53. "liblz"))))
  54. (define (lzlib-available?)
  55. "Return true if lzlib is available, #f otherwise."
  56. (false-if-exception (force %lzlib)))
  57. (define (lzlib-procedure ret name parameters)
  58. "Return a procedure corresponding to C function NAME in liblz, or #f if
  59. either lzlib or the function could not be found."
  60. (match (false-if-exception (dynamic-func name (force %lzlib)))
  61. ((? pointer? ptr)
  62. (pointer->procedure ret ptr parameters))
  63. (#f
  64. #f)))
  65. (define-wrapped-pointer-type <lz-decoder>
  66. ;; Scheme counterpart of the 'LZ_Decoder' opaque type.
  67. lz-decoder?
  68. pointer->lz-decoder
  69. lz-decoder->pointer
  70. (lambda (obj port)
  71. (format port "#<lz-decoder ~a>"
  72. (number->string (object-address obj) 16))))
  73. (define-wrapped-pointer-type <lz-encoder>
  74. ;; Scheme counterpart of the 'LZ_Encoder' opaque type.
  75. lz-encoder?
  76. pointer->lz-encoder
  77. lz-encoder->pointer
  78. (lambda (obj port)
  79. (format port "#<lz-encoder ~a>"
  80. (number->string (object-address obj) 16))))
  81. ;; From lzlib.h
  82. (define %error-number-ok 0)
  83. (define %error-number-bad-argument 1)
  84. (define %error-number-mem-error 2)
  85. (define %error-number-sequence-error 3)
  86. (define %error-number-header-error 4)
  87. (define %error-number-unexpected-eof 5)
  88. (define %error-number-data-error 6)
  89. (define %error-number-library-error 7)
  90. ;; Compression bindings.
  91. (define lz-compress-open
  92. (let ((proc (lzlib-procedure '* "LZ_compress_open" (list int int uint64)))
  93. ;; member-size is an "unsigned long long", and the C standard guarantees
  94. ;; a minimum range of 0..2^64-1.
  95. (unlimited-size (- (expt 2 64) 1)))
  96. (lambda* (dictionary-size match-length-limit #:optional (member-size unlimited-size))
  97. "Initialize the internal stream state for compression and returns a
  98. pointer that can only be used as the encoder argument for the other
  99. lz-compress functions, or a null pointer if the encoder could not be
  100. allocated.
  101. See the manual: (lzlib) Compression functions."
  102. (let ((encoder-ptr (proc dictionary-size match-length-limit member-size)))
  103. (if (not (= (lz-compress-error encoder-ptr) -1))
  104. (pointer->lz-encoder encoder-ptr)
  105. (throw 'lzlib-error 'lz-compress-open))))))
  106. (define lz-compress-close
  107. (let ((proc (lzlib-procedure int "LZ_compress_close" '(*))))
  108. (lambda (encoder)
  109. "Close encoder. ENCODER can no longer be used as an argument to any
  110. lz-compress function. "
  111. (let ((ret (proc (lz-encoder->pointer encoder))))
  112. (if (= ret -1)
  113. (throw 'lzlib-error 'lz-compress-close ret)
  114. ret)))))
  115. (define lz-compress-finish
  116. (let ((proc (lzlib-procedure int "LZ_compress_finish" '(*))))
  117. (lambda (encoder)
  118. "Tell that all the data for this member have already been written (with
  119. the `lz-compress-write' function). It is safe to call `lz-compress-finish' as
  120. many times as needed. After all the produced compressed data have been read
  121. with `lz-compress-read' and `lz-compress-member-finished?' returns #t, a new
  122. member can be started with 'lz-compress-restart-member'."
  123. (let ((ret (proc (lz-encoder->pointer encoder))))
  124. (if (= ret -1)
  125. (throw 'lzlib-error 'lz-compress-finish (lz-compress-error encoder))
  126. ret)))))
  127. (define lz-compress-restart-member
  128. (let ((proc (lzlib-procedure int "LZ_compress_restart_member" (list '* uint64))))
  129. (lambda (encoder member-size)
  130. "Start a new member in a multimember data stream.
  131. Call this function only after `lz-compress-member-finished?' indicates that the
  132. current member has been fully read (with the `lz-compress-read' function)."
  133. (let ((ret (proc (lz-encoder->pointer encoder) member-size)))
  134. (if (= ret -1)
  135. (throw 'lzlib-error 'lz-compress-restart-member
  136. (lz-compress-error encoder))
  137. ret)))))
  138. (define lz-compress-sync-flush
  139. (let ((proc (lzlib-procedure int "LZ_compress_sync_flush" (list '*))))
  140. (lambda (encoder)
  141. "Make available to `lz-compress-read' all the data already written with
  142. the `LZ-compress-write' function. First call `lz-compress-sync-flush'. Then
  143. call 'lz-compress-read' until it returns 0.
  144. Repeated use of `LZ-compress-sync-flush' may degrade compression ratio,
  145. so use it only when needed. "
  146. (let ((ret (proc (lz-encoder->pointer encoder))))
  147. (if (= ret -1)
  148. (throw 'lzlib-error 'lz-compress-sync-flush
  149. (lz-compress-error encoder))
  150. ret)))))
  151. (define lz-compress-read
  152. (let ((proc (lzlib-procedure int "LZ_compress_read" (list '* '* int))))
  153. (lambda* (encoder lzfile-bv #:optional (start 0) (count (bytevector-length lzfile-bv)))
  154. "Read up to COUNT bytes from the encoder stream, storing the results in LZFILE-BV.
  155. Return the number of uncompressed bytes written, a positive integer."
  156. (let ((ret (proc (lz-encoder->pointer encoder)
  157. (bytevector->pointer lzfile-bv start)
  158. count)))
  159. (if (= ret -1)
  160. (throw 'lzlib-error 'lz-compress-read (lz-compress-error encoder))
  161. ret)))))
  162. (define lz-compress-write
  163. (let ((proc (lzlib-procedure int "LZ_compress_write" (list '* '* int))))
  164. (lambda* (encoder bv #:optional (start 0) (count (bytevector-length bv)))
  165. "Write up to COUNT bytes from BV to the encoder stream. Return the
  166. number of uncompressed bytes written, a strictly positive integer."
  167. (let ((ret (proc (lz-encoder->pointer encoder)
  168. (bytevector->pointer bv start)
  169. count)))
  170. (if (< ret 0)
  171. (throw 'lzlib-error 'lz-compress-write (lz-compress-error encoder))
  172. ret)))))
  173. (define lz-compress-write-size
  174. (let ((proc (lzlib-procedure int "LZ_compress_write_size" '(*))))
  175. (lambda (encoder)
  176. "The maximum number of bytes that can be immediately written through the
  177. `lz-compress-write' function.
  178. It is guaranteed that an immediate call to `lz-compress-write' will accept a
  179. SIZE up to the returned number of bytes. "
  180. (let ((ret (proc (lz-encoder->pointer encoder))))
  181. (if (= ret -1)
  182. (throw 'lzlib-error 'lz-compress-write-size (lz-compress-error encoder))
  183. ret)))))
  184. (define lz-compress-error
  185. (let ((proc (lzlib-procedure int "LZ_compress_errno" '(*))))
  186. (lambda (encoder)
  187. "ENCODER can be a Scheme object or a pointer."
  188. (let* ((error-number (proc (if (lz-encoder? encoder)
  189. (lz-encoder->pointer encoder)
  190. encoder))))
  191. error-number))))
  192. (define lz-compress-finished?
  193. (let ((proc (lzlib-procedure int "LZ_compress_finished" '(*))))
  194. (lambda (encoder)
  195. "Return #t if all the data have been read and `lz-compress-close' can
  196. be safely called. Otherwise return #f."
  197. (let ((ret (proc (lz-encoder->pointer encoder))))
  198. (match ret
  199. (1 #t)
  200. (0 #f)
  201. (_ (throw 'lzlib-error 'lz-compress-finished? (lz-compress-error encoder))))))))
  202. (define lz-compress-member-finished?
  203. (let ((proc (lzlib-procedure int "LZ_compress_member_finished" '(*))))
  204. (lambda (encoder)
  205. "Return #t if the current member, in a multimember data stream, has
  206. been fully read and 'lz-compress-restart-member' can be safely called.
  207. Otherwise return #f."
  208. (let ((ret (proc (lz-encoder->pointer encoder))))
  209. (match ret
  210. (1 #t)
  211. (0 #f)
  212. (_ (throw 'lzlib-error 'lz-compress-member-finished? (lz-compress-error encoder))))))))
  213. (define lz-compress-data-position
  214. (let ((proc (lzlib-procedure uint64 "LZ_compress_data_position" '(*))))
  215. (lambda (encoder)
  216. "Return the number of input bytes already compressed in the current
  217. member."
  218. (let ((ret (proc (lz-encoder->pointer encoder))))
  219. (if (= ret -1)
  220. (throw 'lzlib-error 'lz-compress-data-position
  221. (lz-compress-error encoder))
  222. ret)))))
  223. (define lz-compress-member-position
  224. (let ((proc (lzlib-procedure uint64 "LZ_compress_member_position" '(*))))
  225. (lambda (encoder)
  226. "Return the number of compressed bytes already produced, but perhaps
  227. not yet read, in the current member."
  228. (let ((ret (proc (lz-encoder->pointer encoder))))
  229. (if (= ret -1)
  230. (throw 'lzlib-error 'lz-compress-member-position
  231. (lz-compress-error encoder))
  232. ret)))))
  233. (define lz-compress-total-in-size
  234. (let ((proc (lzlib-procedure uint64 "LZ_compress_total_in_size" '(*))))
  235. (lambda (encoder)
  236. "Return the total number of input bytes already compressed."
  237. (let ((ret (proc (lz-encoder->pointer encoder))))
  238. (if (= ret -1)
  239. (throw 'lzlib-error 'lz-compress-total-in-size
  240. (lz-compress-error encoder))
  241. ret)))))
  242. (define lz-compress-total-out-size
  243. (let ((proc (lzlib-procedure uint64 "LZ_compress_total_out_size" '(*))))
  244. (lambda (encoder)
  245. "Return the total number of compressed bytes already produced, but
  246. perhaps not yet read."
  247. (let ((ret (proc (lz-encoder->pointer encoder))))
  248. (if (= ret -1)
  249. (throw 'lzlib-error 'lz-compress-total-out-size
  250. (lz-compress-error encoder))
  251. ret)))))
  252. ;; Decompression bindings.
  253. (define lz-decompress-open
  254. (let ((proc (lzlib-procedure '* "LZ_decompress_open" '())))
  255. (lambda ()
  256. "Initializes the internal stream state for decompression and returns a
  257. pointer that can only be used as the decoder argument for the other
  258. lz-decompress functions, or a null pointer if the decoder could not be
  259. allocated.
  260. See the manual: (lzlib) Decompression functions."
  261. (let ((decoder-ptr (proc)))
  262. (if (not (= (lz-decompress-error decoder-ptr) -1))
  263. (pointer->lz-decoder decoder-ptr)
  264. (throw 'lzlib-error 'lz-decompress-open))))))
  265. (define lz-decompress-close
  266. (let ((proc (lzlib-procedure int "LZ_decompress_close" '(*))))
  267. (lambda (decoder)
  268. "Close decoder. DECODER can no longer be used as an argument to any
  269. lz-decompress function. "
  270. (let ((ret (proc (lz-decoder->pointer decoder))))
  271. (if (= ret -1)
  272. (throw 'lzlib-error 'lz-decompress-close ret)
  273. ret)))))
  274. (define lz-decompress-finish
  275. (let ((proc (lzlib-procedure int "LZ_decompress_finish" '(*))))
  276. (lambda (decoder)
  277. "Tell that all the data for this stream have already been written (with
  278. the `lz-decompress-write' function). It is safe to call
  279. `lz-decompress-finish' as many times as needed."
  280. (let ((ret (proc (lz-decoder->pointer decoder))))
  281. (if (= ret -1)
  282. (throw 'lzlib-error 'lz-decompress-finish (lz-decompress-error decoder))
  283. ret)))))
  284. (define lz-decompress-reset
  285. (let ((proc (lzlib-procedure int "LZ_decompress_reset" '(*))))
  286. (lambda (decoder)
  287. "Reset the internal state of DECODER as it was just after opening it
  288. with the `lz-decompress-open' function. Data stored in the internal buffers
  289. is discarded. Position counters are set to 0."
  290. (let ((ret (proc (lz-decoder->pointer decoder))))
  291. (if (= ret -1)
  292. (throw 'lzlib-error 'lz-decompress-reset
  293. (lz-decompress-error decoder))
  294. ret)))))
  295. (define lz-decompress-sync-to-member
  296. (let ((proc (lzlib-procedure int "LZ_decompress_sync_to_member" '(*))))
  297. (lambda (decoder)
  298. "Reset the error state of DECODER and enters a search state that lasts
  299. until a new member header (or the end of the stream) is found. After a
  300. successful call to `lz-decompress-sync-to-member', data written with
  301. `lz-decompress-write' will be consumed and 'lz-decompress-read' will return 0
  302. until a header is found.
  303. This function is useful to discard any data preceding the first member, or to
  304. discard the rest of the current member, for example in case of a data
  305. error. If the decoder is already at the beginning of a member, this function
  306. does nothing."
  307. (let ((ret (proc (lz-decoder->pointer decoder))))
  308. (if (= ret -1)
  309. (throw 'lzlib-error 'lz-decompress-sync-to-member
  310. (lz-decompress-error decoder))
  311. ret)))))
  312. (define lz-decompress-read
  313. (let ((proc (lzlib-procedure int "LZ_decompress_read" (list '* '* int))))
  314. (lambda* (decoder file-bv #:optional (start 0) (count (bytevector-length file-bv)))
  315. "Read up to COUNT bytes from the decoder stream, storing the results in FILE-BV.
  316. Return the number of uncompressed bytes written, a non-negative positive integer."
  317. (let ((ret (proc (lz-decoder->pointer decoder)
  318. (bytevector->pointer file-bv start)
  319. count)))
  320. (if (< ret 0)
  321. (throw 'lzlib-error 'lz-decompress-read (lz-decompress-error decoder))
  322. ret)))))
  323. (define lz-decompress-write
  324. (let ((proc (lzlib-procedure int "LZ_decompress_write" (list '* '* int))))
  325. (lambda* (decoder bv #:optional (start 0) (count (bytevector-length bv)))
  326. "Write up to COUNT bytes from BV to the decoder stream. Return the
  327. number of uncompressed bytes written, a non-negative integer."
  328. (let ((ret (proc (lz-decoder->pointer decoder)
  329. (bytevector->pointer bv start)
  330. count)))
  331. (if (< ret 0)
  332. (throw 'lzlib-error 'lz-decompress-write (lz-decompress-error decoder))
  333. ret)))))
  334. (define lz-decompress-write-size
  335. (let ((proc (lzlib-procedure int "LZ_decompress_write_size" '(*))))
  336. (lambda (decoder)
  337. "Return the maximum number of bytes that can be immediately written
  338. through the `lz-decompress-write' function.
  339. It is guaranteed that an immediate call to `lz-decompress-write' will accept a
  340. SIZE up to the returned number of bytes. "
  341. (let ((ret (proc (lz-decoder->pointer decoder))))
  342. (if (= ret -1)
  343. (throw 'lzlib-error 'lz-decompress-write-size (lz-decompress-error decoder))
  344. ret)))))
  345. (define lz-decompress-error
  346. (let ((proc (lzlib-procedure int "LZ_decompress_errno" '(*))))
  347. (lambda (decoder)
  348. "DECODER can be a Scheme object or a pointer."
  349. (let* ((error-number (proc (if (lz-decoder? decoder)
  350. (lz-decoder->pointer decoder)
  351. decoder))))
  352. error-number))))
  353. (define lz-decompress-finished?
  354. (let ((proc (lzlib-procedure int "LZ_decompress_finished" '(*))))
  355. (lambda (decoder)
  356. "Return #t if all the data have been read and `lz-decompress-close' can
  357. be safely called. Otherwise return #f."
  358. (let ((ret (proc (lz-decoder->pointer decoder))))
  359. (match ret
  360. (1 #t)
  361. (0 #f)
  362. (_ (throw 'lzlib-error 'lz-decompress-finished? (lz-decompress-error decoder))))))))
  363. (define lz-decompress-member-finished?
  364. (let ((proc (lzlib-procedure int "LZ_decompress_member_finished" '(*))))
  365. (lambda (decoder)
  366. "Return #t if the current member, in a multimember data stream, has
  367. been fully read and `lz-decompress-restart-member' can be safely called.
  368. Otherwise return #f."
  369. (let ((ret (proc (lz-decoder->pointer decoder))))
  370. (match ret
  371. (1 #t)
  372. (0 #f)
  373. (_ (throw 'lzlib-error 'lz-decompress-member-finished? (lz-decompress-error decoder))))))))
  374. (define lz-decompress-member-version
  375. (let ((proc (lzlib-procedure int "LZ_decompress_member_version" '(*))))
  376. (lambda (decoder)
  377. (let ((ret (proc (lz-decoder->pointer decoder))))
  378. "Return the version of current member from member header."
  379. (if (= ret -1)
  380. (throw 'lzlib-error 'lz-decompress-data-position
  381. (lz-decompress-error decoder))
  382. ret)))))
  383. (define lz-decompress-dictionary-size
  384. (let ((proc (lzlib-procedure int "LZ_decompress_dictionary_size" '(*))))
  385. (lambda (decoder)
  386. (let ((ret (proc (lz-decoder->pointer decoder))))
  387. "Return the dictionary size of current member from member header."
  388. (if (= ret -1)
  389. (throw 'lzlib-error 'lz-decompress-member-position
  390. (lz-decompress-error decoder))
  391. ret)))))
  392. (define lz-decompress-data-crc
  393. (let ((proc (lzlib-procedure unsigned-int "LZ_decompress_data_crc" '(*))))
  394. (lambda (decoder)
  395. (let ((ret (proc (lz-decoder->pointer decoder))))
  396. "Return the 32 bit Cyclic Redundancy Check of the data decompressed
  397. from the current member. The returned value is valid only when
  398. `lz-decompress-member-finished' returns #t. "
  399. (if (= ret -1)
  400. (throw 'lzlib-error 'lz-decompress-member-position
  401. (lz-decompress-error decoder))
  402. ret)))))
  403. (define lz-decompress-data-position
  404. (let ((proc (lzlib-procedure uint64 "LZ_decompress_data_position" '(*))))
  405. (lambda (decoder)
  406. "Return the number of decompressed bytes already produced, but perhaps
  407. not yet read, in the current member."
  408. (let ((ret (proc (lz-decoder->pointer decoder))))
  409. (if (= ret -1)
  410. (throw 'lzlib-error 'lz-decompress-data-position
  411. (lz-decompress-error decoder))
  412. ret)))))
  413. (define lz-decompress-member-position
  414. (let ((proc (lzlib-procedure uint64 "LZ_decompress_member_position" '(*))))
  415. (lambda (decoder)
  416. "Return the number of input bytes already decompressed in the current
  417. member."
  418. (let ((ret (proc (lz-decoder->pointer decoder))))
  419. (if (= ret -1)
  420. (throw 'lzlib-error 'lz-decompress-member-position
  421. (lz-decompress-error decoder))
  422. ret)))))
  423. (define lz-decompress-total-in-size
  424. (let ((proc (lzlib-procedure uint64 "LZ_decompress_total_in_size" '(*))))
  425. (lambda (decoder)
  426. (let ((ret (proc (lz-decoder->pointer decoder))))
  427. "Return the total number of input bytes already compressed."
  428. (if (= ret -1)
  429. (throw 'lzlib-error 'lz-decompress-total-in-size
  430. (lz-decompress-error decoder))
  431. ret)))))
  432. (define lz-decompress-total-out-size
  433. (let ((proc (lzlib-procedure uint64 "LZ_decompress_total_out_size" '(*))))
  434. (lambda (decoder)
  435. (let ((ret (proc (lz-decoder->pointer decoder))))
  436. "Return the total number of compressed bytes already produced, but
  437. perhaps not yet read."
  438. (if (= ret -1)
  439. (throw 'lzlib-error 'lz-decompress-total-out-size
  440. (lz-decompress-error decoder))
  441. ret)))))
  442. ;; High level functions.
  443. (define* (lzread! decoder port bv
  444. #:optional (start 0) (count (bytevector-length bv)))
  445. "Read up to COUNT bytes from PORT into BV at offset START. Return the
  446. number of uncompressed bytes actually read; it is zero if COUNT is zero or if
  447. the end-of-stream has been reached."
  448. (define (feed-decoder! decoder)
  449. ;; Feed DECODER with data read from PORT.
  450. (match (get-bytevector-n port (lz-decompress-write-size decoder))
  451. ((? eof-object? eof) eof)
  452. (bv (lz-decompress-write decoder bv))))
  453. (let loop ((read 0)
  454. (start start))
  455. (cond ((< read count)
  456. (match (lz-decompress-read decoder bv start (- count read))
  457. (0 (cond ((lz-decompress-finished? decoder)
  458. read)
  459. ((eof-object? (feed-decoder! decoder))
  460. (lz-decompress-finish decoder)
  461. (loop read start))
  462. (else ;read again
  463. (loop read start))))
  464. (n (loop (+ read n) (+ start n)))))
  465. (else
  466. read))))
  467. (define (lzwrite! encoder source source-offset source-count
  468. target target-offset target-count)
  469. "Write up to SOURCE-COUNT bytes from SOURCE to ENCODER, and read up to
  470. TARGET-COUNT bytes into TARGET at TARGET-OFFSET. Return two values: the
  471. number of bytes read from SOURCE, and the number of bytes written to TARGET,
  472. possibly zero."
  473. (define read
  474. (if (> (lz-compress-write-size encoder) 0)
  475. (match (lz-compress-write encoder source source-offset source-count)
  476. (0 (lz-compress-finish encoder) 0)
  477. (n n))
  478. 0))
  479. (define written
  480. (lz-compress-read encoder target target-offset target-count))
  481. (values read written))
  482. (define* (lzwrite encoder bv lz-port
  483. #:optional (start 0) (count (bytevector-length bv)))
  484. "Write up to COUNT bytes from BV at offset START into LZ-PORT. Return
  485. the number of uncompressed bytes written, a non-negative integer."
  486. (let ((written 0)
  487. (read 0))
  488. (while (and (< 0 (lz-compress-write-size encoder))
  489. (< written count))
  490. (set! written (+ written
  491. (lz-compress-write encoder bv (+ start written) (- count written)))))
  492. (when (= written 0)
  493. (lz-compress-finish encoder))
  494. (let ((lz-bv (make-bytevector written)))
  495. (let loop ((rd 0))
  496. (set! rd (lz-compress-read encoder lz-bv 0 (bytevector-length lz-bv)))
  497. (put-bytevector lz-port lz-bv 0 rd)
  498. (set! read (+ read rd))
  499. (unless (= rd 0)
  500. (loop rd))))
  501. ;; `written' is the total byte count of uncompressed data.
  502. written))
  503. ;;;
  504. ;;; Port interface.
  505. ;;;
  506. ;; Alist of (levels (dictionary-size match-length-limit)). 0 is the fastest.
  507. ;; See bbexample.c in lzlib's source.
  508. (define %compression-levels
  509. `((0 (65535 16))
  510. (1 (,(bitwise-arithmetic-shift-left 1 20) 5))
  511. (2 (,(bitwise-arithmetic-shift-left 3 19) 6))
  512. (3 (,(bitwise-arithmetic-shift-left 1 21) 8))
  513. (4 (,(bitwise-arithmetic-shift-left 3 20) 12))
  514. (5 (,(bitwise-arithmetic-shift-left 1 22) 20))
  515. (6 (,(bitwise-arithmetic-shift-left 1 23) 36))
  516. (7 (,(bitwise-arithmetic-shift-left 1 24) 68))
  517. (8 (,(bitwise-arithmetic-shift-left 3 23) 132))
  518. (9 (,(bitwise-arithmetic-shift-left 1 25) 273))))
  519. (define %default-compression-level
  520. 6)
  521. (define* (make-lzip-input-port port)
  522. "Return an input port that decompresses data read from PORT, a file port.
  523. PORT is automatically closed when the resulting port is closed."
  524. (define decoder (lz-decompress-open))
  525. (define (read! bv start count)
  526. (lzread! decoder port bv start count))
  527. (make-custom-binary-input-port "lzip-input" read! #f #f
  528. (lambda ()
  529. (lz-decompress-close decoder)
  530. (close-port port))))
  531. (define* (make-lzip-output-port port
  532. #:key
  533. (level %default-compression-level))
  534. "Return an output port that compresses data at the given LEVEL, using PORT,
  535. a file port, as its sink. PORT is automatically closed when the resulting
  536. port is closed."
  537. (define encoder (apply lz-compress-open
  538. (car (assoc-ref %compression-levels level))))
  539. (define (write! bv start count)
  540. (lzwrite encoder bv port start count))
  541. (make-custom-binary-output-port "lzip-output" write! #f #f
  542. (lambda ()
  543. (lz-compress-finish encoder)
  544. ;; "lz-read" the trailing metadata added by `lz-compress-finish'.
  545. (let ((lz-bv (make-bytevector (* 64 1024))))
  546. (let loop ((rd 0))
  547. (set! rd (lz-compress-read encoder lz-bv 0 (bytevector-length lz-bv)))
  548. (put-bytevector port lz-bv 0 rd)
  549. (unless (= rd 0)
  550. (loop rd))))
  551. (lz-compress-close encoder)
  552. (close-port port))))
  553. (define* (make-lzip-input-port/compressed port
  554. #:key
  555. (level %default-compression-level))
  556. "Return an input port that compresses data read from PORT, with the given LEVEL.
  557. PORT is automatically closed when the resulting port is closed."
  558. (define encoder (apply lz-compress-open
  559. (car (assoc-ref %compression-levels level))))
  560. (define input-buffer (make-bytevector 8192))
  561. (define input-len 0)
  562. (define input-offset 0)
  563. (define input-eof? #f)
  564. (define (read! bv start count)
  565. (cond
  566. (input-eof?
  567. (match (lz-compress-read encoder bv start count)
  568. (0 (if (lz-compress-finished? encoder)
  569. 0
  570. (read! bv start count)))
  571. (n n)))
  572. ((= input-offset input-len)
  573. (match (get-bytevector-n! port input-buffer 0
  574. (bytevector-length input-buffer))
  575. ((? eof-object?)
  576. (set! input-eof? #t)
  577. (lz-compress-finish encoder))
  578. (count
  579. (set! input-offset 0)
  580. (set! input-len count)))
  581. (read! bv start count))
  582. (else
  583. (let-values (((read written)
  584. (lzwrite! encoder
  585. input-buffer input-offset
  586. (- input-len input-offset)
  587. bv start count)))
  588. (set! input-offset (+ input-offset read))
  589. ;; Make sure we don't return zero except on EOF.
  590. (if (= 0 written)
  591. (read! bv start count)
  592. written)))))
  593. (make-custom-binary-input-port "lzip-input/compressed"
  594. read! #f #f
  595. (lambda ()
  596. (close-port port))))
  597. (define* (call-with-lzip-input-port port proc)
  598. "Call PROC with a port that wraps PORT and decompresses data read from it.
  599. PORT is closed upon completion."
  600. (let ((lzip (make-lzip-input-port port)))
  601. (dynamic-wind
  602. (const #t)
  603. (lambda ()
  604. (proc lzip))
  605. (lambda ()
  606. (close-port lzip)))))
  607. (define* (call-with-lzip-output-port port proc
  608. #:key
  609. (level %default-compression-level))
  610. "Call PROC with an output port that wraps PORT and compresses data. PORT is
  611. close upon completion."
  612. (let ((lzip (make-lzip-output-port port
  613. #:level level)))
  614. (dynamic-wind
  615. (const #t)
  616. (lambda ()
  617. (proc lzip))
  618. (lambda ()
  619. (close-port lzip)))))
  620. ;;; lzlib.scm ends here