Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/gzip Add lzma (.xz) support. Somehow this does not d...



details:   https://anonhg.NetBSD.org/src/rev/57af787bbbb1
branches:  trunk
changeset: 766216:57af787bbbb1
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jun 19 00:43:54 2011 +0000

description:
Add lzma (.xz) support. Somehow this does not decode after the first read yet.

diffstat:

 usr.bin/gzip/Makefile |    6 +-
 usr.bin/gzip/gzip.c   |   65 +++++++++++++++++++++++-----
 usr.bin/gzip/unxz.c   |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+), 16 deletions(-)

diffs (truncated from 314 to 300 lines):

diff -r 44ef10d3c912 -r 57af787bbbb1 usr.bin/gzip/Makefile
--- a/usr.bin/gzip/Makefile     Sat Jun 18 23:07:04 2011 +0000
+++ b/usr.bin/gzip/Makefile     Sun Jun 19 00:43:54 2011 +0000
@@ -1,12 +1,12 @@
-#      $NetBSD: Makefile,v 1.13 2009/04/14 22:15:20 lukem Exp $
+#      $NetBSD: Makefile,v 1.14 2011/06/19 00:43:54 christos Exp $
 
 USE_FORT?= yes # data-driven bugs?
 
 PROG=          gzip
 MAN=           gzip.1 gzexe.1 zdiff.1 zforce.1 zgrep.1 zmore.1 znew.1
 
-DPADD=         ${LIBZ} ${LIBBZ2}
-LDADD=         -lz -lbz2
+DPADD=         ${LIBZ} ${LIBBZ2} ${LIBLZMA}
+LDADD=         -lz -lbz2 -llzma
 
 SCRIPTS=       gzexe zdiff zforce zgrep zmore znew
 
diff -r 44ef10d3c912 -r 57af787bbbb1 usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Sat Jun 18 23:07:04 2011 +0000
+++ b/usr.bin/gzip/gzip.c       Sun Jun 19 00:43:54 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $        */
+/*     $NetBSD: gzip.c,v 1.100 2011/06/19 00:43:54 christos Exp $      */
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
@@ -30,7 +30,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
  Matthew R. Green.  All rights reserved.");
-__RCSID("$NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.100 2011/06/19 00:43:54 christos Exp $");
 #endif /* not lint */
 
 /*
@@ -81,6 +81,9 @@
 #ifndef NO_PACK_SUPPORT
        FT_PACK,
 #endif
+#ifndef NO_XZ_SUPPORT
+       FT_XZ,
+#endif
        FT_LAST,
        FT_UNKNOWN
 };
@@ -101,6 +104,12 @@
 #define PACK_MAGIC     "\037\036"
 #endif
 
+#ifndef NO_XZ_SUPPORT
+#include <lzma.h>
+#define XZ_SUFFIX      ".xz"
+#define XZ_MAGIC       "\3757zXZ"
+#endif
+
 #define GZ_SUFFIX      ".gz"
 
 #define BUFLEN         (64 * 1024)
@@ -227,6 +236,10 @@
 static off_t   unpack(int, int, char *, size_t, off_t *);
 #endif
 
+#ifndef NO_XZ_SUPPORT
+static off_t   unxz(int, int, char *, size_t, off_t *);
+#endif
+
 int main(int, char *p[]);
 
 #ifdef SMALL
@@ -1097,6 +1110,11 @@
                return FT_PACK;
        else
 #endif
+#ifndef NO_XZ_SUPPORT
+       if (memcmp(buf, XZ_MAGIC, 4) == 0)      /* XXX: We only have 4 bytes */
+               return FT_XZ;
+       else
+#endif
                return FT_UNKNOWN;
 }
 
@@ -1326,7 +1344,6 @@
        }
 
        method = file_gettype(header1);
-
 #ifndef SMALL
        if (fflag == 0 && method == FT_UNKNOWN) {
                maybe_warnx("%s: not in gzip format", file);
@@ -1401,9 +1418,9 @@
        } else
                zfd = STDOUT_FILENO;
 
+       switch (method) {
 #ifndef NO_BZIP2_SUPPORT
-       if (method == FT_BZIP2) {
-
+       case FT_BZIP2:
                /* XXX */
                if (lflag) {
                        maybe_warnx("no -l with bzip2 files");
@@ -1411,11 +1428,11 @@
                }
 
                size = unbzip2(fd, zfd, NULL, 0, NULL);
-       } else
+               break;
 #endif
 
 #ifndef NO_COMPRESS_SUPPORT
-       if (method == FT_Z) {
+       case FT_Z: {
                FILE *in, *out;
 
                /* XXX */
@@ -1448,30 +1465,42 @@
                        unlink(outfile);
                        goto lose;
                }
-       } else
+               break;
+       }
 #endif
 
 #ifndef NO_PACK_SUPPORT
-       if (method == FT_PACK) {
+       case FT_PACK:
                if (lflag) {
                        maybe_warnx("no -l with packed files");
                        goto lose;
                }
 
                size = unpack(fd, zfd, NULL, 0, NULL);
-       } else
+               break;
+#endif
+
+#ifndef NO_XZ_SUPPORT
+       case FT_XZ:
+               if (lflag) {
+                       maybe_warnx("no -l with xz files");
+                       goto lose;
+               }
+
+               size = unxz(fd, zfd, NULL, 0, NULL);
+               break;
 #endif
 
 #ifndef SMALL
-       if (method == FT_UNKNOWN) {
+       case FT_UNKNOWN:
                if (lflag) {
                        maybe_warnx("no -l for unknown filetypes");
                        goto lose;
                }
                size = cat_fd(NULL, 0, NULL, fd);
-       } else
+               break;
 #endif
-       {
+       default:
                if (lflag) {
                        print_list(fd, isb.st_size, outfile, isb.st_mtime);
                        close(fd);
@@ -1479,6 +1508,7 @@
                }
 
                size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
+               break;
        }
 
        if (close(fd) != 0)
@@ -1661,6 +1691,12 @@
                               (char *)header1, sizeof header1, &gsize);
                break;
 #endif
+#ifndef NO_XZ_SUPPORT
+       case FT_XZ:
+               usize = unxz(STDIN_FILENO, STDOUT_FILENO,
+                            (char *)header1, sizeof header1, &gsize);
+               break;
+#endif
        }
 
 #ifndef SMALL
@@ -2037,6 +2073,9 @@
 #ifndef NO_PACK_SUPPORT
 #include "unpack.c"
 #endif
+#ifndef NO_XZ_SUPPORT
+#include "unxz.c"
+#endif
 
 static ssize_t
 read_retry(int fd, void *buf, size_t sz)
diff -r 44ef10d3c912 -r 57af787bbbb1 usr.bin/gzip/unxz.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/gzip/unxz.c       Sun Jun 19 00:43:54 2011 +0000
@@ -0,0 +1,113 @@
+
+#include <stdarg.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <lzma.h>
+
+static off_t
+unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
+{
+       lzma_stream strm = LZMA_STREAM_INIT;
+       lzma_ret ret;
+       off_t x = 0;
+
+       // Initialize the decoder
+       ret = lzma_alone_decoder(&strm, UINT64_MAX);
+       if (ret != LZMA_OK) {
+               errno = ret == LZMA_MEM_ERROR ? ENOMEM : EINVAL;
+               maybe_errx("Cannot initialize decoder");
+       }
+
+       // Input and output buffers
+       uint8_t ibuf[BUFSIZ];
+       uint8_t obuf[BUFSIZ];
+
+       *bytes_in = prelen;
+       strm.next_in = ibuf;
+       strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
+       if (strm.avail_in == (size_t)-1)
+               maybe_errx("Read failed");
+
+       memcpy(ibuf, pre, prelen);
+       *bytes_in += strm.avail_in;
+
+       strm.next_out = obuf;
+       strm.avail_out = sizeof(obuf);
+       if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, 0)) != LZMA_OK)
+               maybe_errx("Can't initialize decoder");
+
+       for (;;) {
+               if (strm.avail_in == 0) {
+                       strm.next_in = ibuf;
+                       strm.avail_in = read(i, ibuf, sizeof(ibuf));
+//                     fprintf(stderr, "read = %zu\n", strm.avail_in);
+                       if (strm.avail_in == (size_t)-1)
+                               maybe_errx("Read failed");
+               }
+
+               ret = lzma_code(&strm, LZMA_RUN);
+//             fprintf(stderr, "ret = %d %zu %zu\n", ret, strm.avail_in, strm.avail_out);
+
+               // Write and check write error before checking decoder error.
+               // This way as much data as possible gets written to output
+               // even if decoder detected an error.
+               if (strm.avail_out == 0 || ret != LZMA_OK) {
+                       const size_t write_size = sizeof(obuf) - strm.avail_out;
+
+                       if (write(o, obuf, write_size) != (ssize_t)write_size)
+                               maybe_err("write failed");
+
+                       strm.next_out = obuf;
+                       strm.avail_out = sizeof(obuf);
+                       x += write_size;
+               }
+
+               if (ret != LZMA_OK) {
+                       if (ret == LZMA_STREAM_END) {
+                               // Check that there's no trailing garbage.
+                               if (strm.avail_in != 0 || read(i, ibuf, 1))
+                                       ret = LZMA_DATA_ERROR;
+                               else {
+                                       lzma_end(&strm);
+                                       return x;
+                               }
+                       }
+
+                       const char *msg;
+                       switch (ret) {
+                       case LZMA_MEM_ERROR:
+                               msg = strerror(ENOMEM);
+                               break;
+
+                       case LZMA_FORMAT_ERROR:
+                               msg = "File format not recognized";
+                               break;
+
+                       case LZMA_OPTIONS_ERROR:
+                               // FIXME: Better message?
+                               msg = "Unsupported compression options";
+                               break;
+
+                       case LZMA_DATA_ERROR:
+                               msg = "File is corrupt";
+                               break;
+
+                       case LZMA_BUF_ERROR:
+                               msg = "Unexpected end of input";
+                               break;
+



Home | Main Index | Thread Index | Old Index