summaryrefslogtreecommitdiff
path: root/guix/lzlib.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2019-05-24 15:20:46 +0200
committerLudovic Courtès <ludo@gnu.org>2019-05-27 22:47:24 +0200
commitc131bea2762ea0dd9e75e9340525dc54cd938647 (patch)
treeb9939a094c63db63bfa8f74d829b9fa6d3b6b136 /guix/lzlib.scm
parent2e5c71b2a21ec5f89f8bc38ca01480405b9767a1 (diff)
downloadpatches-c131bea2762ea0dd9e75e9340525dc54cd938647.tar
patches-c131bea2762ea0dd9e75e9340525dc54cd938647.tar.gz
lzlib: 'lzread!' never returns more than it was asked for.
Fixes a bug whereby 'lzread!' could return more than COUNT. * guix/lzlib.scm (lzread!): Rewrite in a semi-functional style.
Diffstat (limited to 'guix/lzlib.scm')
-rw-r--r--guix/lzlib.scm39
1 files changed, 19 insertions, 20 deletions
diff --git a/guix/lzlib.scm b/guix/lzlib.scm
index a484315c0e..31d84971cb 100644
--- a/guix/lzlib.scm
+++ b/guix/lzlib.scm
@@ -494,29 +494,28 @@ perhaps not yet read."
;; High level functions.
-(define* (lzread! decoder file-port bv
+
+(define* (lzread! decoder port bv
#:optional (start 0) (count (bytevector-length bv)))
- "Read up to COUNT bytes from FILE-PORT into BV at offset START. Return the
+ "Read up to COUNT bytes from PORT into BV at offset START. Return the
number of uncompressed bytes actually read; it is zero if COUNT is zero or if
the end-of-stream has been reached."
- ;; WARNING: Because we don't alternate between lz-reads and lz-writes, we can't
- ;; process more than lz-decompress-write-size from the file-port.
- (when (> count (lz-decompress-write-size decoder))
- (set! count (lz-decompress-write-size decoder)))
- (let ((file-bv (get-bytevector-n file-port count)))
- (unless (eof-object? file-bv)
- (lz-decompress-write decoder file-bv 0 (bytevector-length file-bv))))
- (let ((read 0))
- (let loop ((rd 0))
- (if (< start (bytevector-length bv))
- (begin
- (set! rd (lz-decompress-read decoder bv start (- (bytevector-length bv) start)))
- (set! start (+ start rd))
- (set! read (+ read rd)))
- (set! rd 0))
- (unless (= rd 0)
- (loop rd)))
- read))
+ (define (feed-decoder! decoder)
+ ;; Feed DECODER with data read from PORT.
+ (match (get-bytevector-n port (lz-decompress-write-size decoder))
+ ((? eof-object? eof) eof)
+ (bv (lz-decompress-write decoder bv))))
+
+ (let loop ((read 0)
+ (start start))
+ (cond ((< read count)
+ (match (lz-decompress-read decoder bv start (- count read))
+ (0 (if (eof-object? (feed-decoder! decoder))
+ read
+ (loop read start)))
+ (n (loop (+ read n) (+ start n)))))
+ (else
+ read))))
(define (lzwrite! encoder source source-offset source-count
target target-offset target-count)