Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/gzip handle stdin with header partially read.



details:   https://anonhg.NetBSD.org/src/rev/991b730cd94c
branches:  trunk
changeset: 445411:991b730cd94c
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Oct 27 23:40:04 2018 +0000

description:
handle stdin with header partially read.

diffstat:

 usr.bin/gzip/unlz.c |  29 ++++++++++++++++++-----------
 1 files changed, 18 insertions(+), 11 deletions(-)

diffs (70 lines):

diff -r ed89f8c8fbf4 -r 991b730cd94c usr.bin/gzip/unlz.c
--- a/usr.bin/gzip/unlz.c       Sat Oct 27 22:32:54 2018 +0000
+++ b/usr.bin/gzip/unlz.c       Sat Oct 27 23:40:04 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: unlz.c,v 1.3 2018/10/27 13:20:21 christos Exp $        */
+/*     $NetBSD: unlz.c,v 1.4 2018/10/27 23:40:04 christos Exp $        */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -537,7 +537,7 @@
 
 
 static off_t
-lz_decode(int fin, int fdout, unsigned dict_size)
+lz_decode(int fin, int fdout, unsigned dict_size, off_t *insize)
 {
        struct lz_decoder lz;
        off_t rv = -1;
@@ -568,15 +568,14 @@
        if (crc != lz_get_crc(&lz) || data_size != lz_get_data_position(&lz))
                goto out;
 
-#if 0
        rv = 0;
        for (int i = 19; i >= 12; --i) {
                rv <<= 8;
                rv += trailer[i];
        }
-#else
+       if (insize)
+               *insize = rv;
        rv = ftello(lz.fout);
-#endif
 out:
        lz_destroy(&lz);
        return rv;
@@ -612,13 +611,21 @@
 
        char header[HDR_SIZE];
 
-       switch (read(fin, header, sizeof(header))) {
+       if (prelen > sizeof(header))
+               return -1;
+       if (pre && prelen)
+               memcpy(header, pre, prelen);
+       
+       ssize_t nr = read(fin, header + prelen, sizeof(header) - prelen);
+       switch (nr) {
+       case -1:
+               return -1;
        case 0:
-               return 0;
-       case sizeof(header):
+               return prelen ? -1 : 0;
+       default:
+               if ((size_t)nr != sizeof(header) - prelen)
+                       return -1;
                break;
-       default:
-               return -1;
        }
 
        if (memcmp(header, hdrmagic, sizeof(hdrmagic)) != 0)
@@ -628,5 +635,5 @@
        if (dict_size == 0)
                return -1;
 
-       return lz_decode(fin, fout, dict_size);
+       return lz_decode(fin, fout, dict_size, bytes_in);
 }



Home | Main Index | Thread Index | Old Index