Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/gzip backout previous. fix the bug it inspired inst...



details:   https://anonhg.NetBSD.org/src/rev/081fd75f3400
branches:  trunk
changeset: 565131:081fd75f3400
user:      mrg <mrg%NetBSD.org@localhost>
date:      Tue Mar 30 11:43:32 2004 +0000

description:
backout previous.  fix the bug it inspired instead.  we will want this
version of the code when doing file-type detection.

diffstat:

 usr.bin/gzip/unbzip2.c |  92 ++++++++++++++++++++++++++++++-------------------
 1 files changed, 56 insertions(+), 36 deletions(-)

diffs (114 lines):

diff -r 712fb4cbe45a -r 081fd75f3400 usr.bin/gzip/unbzip2.c
--- a/usr.bin/gzip/unbzip2.c    Tue Mar 30 11:42:04 2004 +0000
+++ b/usr.bin/gzip/unbzip2.c    Tue Mar 30 11:43:32 2004 +0000
@@ -1,54 +1,74 @@
-/*     $NetBSD: unbzip2.c,v 1.2 2004/03/30 09:15:07 mrg Exp $  */
+/*     $NetBSD: unbzip2.c,v 1.3 2004/03/30 11:43:32 mrg Exp $  */
 
 /* This file is #included by gzip.c */
 
+#define INBUFSIZE      (64 * 1024)
+#define OUTBUFSIZE     (64 * 1024)
+
 static off_t
 unbzip2(int in, int out)
 {
-       FILE            *f_in;
-       BZFILE          *b_in;
-       int             bzerror, n;
+       int             n, ret, end_of_file;
        off_t           bytes_out = 0;
-       char            buffer[64 * 1024];
+       bz_stream       bzs;
+       static char     *inbuf, *outbuf;
 
-       if ((in = dup(in)) < 0)
-               maybe_err(1, "dup");
+       if (inbuf == NULL && (inbuf = malloc(INBUFSIZE)) == NULL)
+               maybe_err(1, "malloc");
+       if (outbuf == NULL && (outbuf = malloc(OUTBUFSIZE)) == NULL)
+               maybe_err(1, "malloc");
 
-       if ((f_in = fdopen(in, "r")) == NULL)
-               maybe_err(1, "fdopen");
+       bzs.bzalloc = NULL;
+       bzs.bzfree = NULL;
+       bzs.opaque = NULL;
 
-       if ((b_in = BZ2_bzReadOpen(&bzerror, f_in, 0, 0, NULL, 0)) == NULL)
-               maybe_err(1, "BZ2_bzReadOpen");
+       end_of_file = 0;
+       ret = BZ2_bzDecompressInit(&bzs, 0, 0);
+       if (ret != BZ_OK)
+               maybe_errx(1, "bzip2 init");
 
-       do {
-               n = BZ2_bzRead(&bzerror, b_in, buffer, sizeof (buffer));
+       bzs.avail_in = 0;
 
-               switch (bzerror) {
-               case BZ_IO_ERROR:
-                       maybe_errx(1, "bzip2 I/O error");
-               case BZ_UNEXPECTED_EOF:
-                       maybe_errx(1, "bzip2 unexpected end of file");
-               case BZ_DATA_ERROR:
-                       maybe_errx(1, "bzip2 data integrity error");
-               case BZ_DATA_ERROR_MAGIC:
-                       maybe_errx(1, "bzip2 magic number error");
-               case BZ_MEM_ERROR:
-                       maybe_errx(1, "bzip2 out of memory");
-               case BZ_OK:
-               case BZ_STREAM_END:
-                       break;
-               default:
-                       maybe_errx(1, "bzip2 unknown error");
-               }
+       while (ret != BZ_STREAM_END) {
+               if (bzs.avail_in == 0 && !end_of_file) {
+                       n = read(in, inbuf, INBUFSIZE);
+                       if (n < 0)
+                               maybe_err(1, "read");
+                       if (n == 0)
+                               end_of_file = 1;
+                       bzs.next_in = inbuf;
+                       bzs.avail_in = n;
+               } else
+                       n = 0;
+
+               bzs.next_out = outbuf;
+               bzs.avail_out = OUTBUFSIZE;
+               ret = BZ2_bzDecompress(&bzs);
 
-               if ((n = write(out, buffer, n)) < 0)
-                       maybe_err(1, "write");
-               bytes_out += n;
-       } while (bzerror != BZ_STREAM_END);
+               switch (ret) {
+               case BZ_STREAM_END:
+               case BZ_OK:
+                       if (ret == BZ_OK && end_of_file)
+                               maybe_err(1, "read");
+                       if (!tflag) {
+                               n = write(out, outbuf, OUTBUFSIZE - bzs.avail_out);
+                               if (n < 0)
+                                       maybe_err(1, "write");
+                       }
+                       bytes_out += n;
+                       break;
 
-       (void)BZ2_bzReadClose(&bzerror, b_in);
-       (void)fclose(f_in);
+               case BZ_DATA_ERROR:
+                       maybe_errx(1, "bzip2 data integrity error");
+               case BZ_DATA_ERROR_MAGIC:
+                       maybe_errx(1, "bzip2 magic number error");
+               case BZ_MEM_ERROR:
+                       maybe_errx(1, "bzip2 out of memory");
+               }
+       }
 
+       if (BZ2_bzDecompressEnd(&bzs) != BZ_OK)
+               return (0);
 
        return (bytes_out);
 }



Home | Main Index | Thread Index | Old Index