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 "pack" uncompression support, from Xin LI <...



details:   https://anonhg.NetBSD.org/src/rev/fda23267b443
branches:  trunk
changeset: 748038:fda23267b443
user:      mrg <mrg%NetBSD.org@localhost>
date:      Sun Oct 11 07:07:54 2009 +0000

description:
add "pack" uncompression support, from Xin LI <delphij%delphij.net@localhost>

diffstat:

 usr.bin/gzip/gzip.c   |   44 ++++++-
 usr.bin/gzip/unpack.c |  323 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 363 insertions(+), 4 deletions(-)

diffs (truncated from 448 to 300 lines):

diff -r 6a263b55d828 -r fda23267b443 usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Sun Oct 11 05:17:20 2009 +0000
+++ b/usr.bin/gzip/gzip.c       Sun Oct 11 07:07:54 2009 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.94 2009/04/12 10:31:14 lukem Exp $  */
+/*     $NetBSD: gzip.c,v 1.95 2009/10/11 07:07:54 mrg 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.94 2009/04/12 10:31:14 lukem Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.95 2009/10/11 07:07:54 mrg Exp $");
 #endif /* not lint */
 
 /*
@@ -78,6 +78,9 @@
 #ifndef NO_COMPRESS_SUPPORT
        FT_Z,
 #endif
+#ifndef NO_PACK_SUPPORT
+       FT_PACK,
+#endif
        FT_LAST,
        FT_UNKNOWN
 };
@@ -94,6 +97,10 @@
 #define Z_MAGIC                "\037\235"
 #endif
 
+#ifndef NO_PACK_SUPPORT
+#define PACK_MAGIC     "\037\036"
+#endif
+
 #define GZ_SUFFIX      ".gz"
 
 #define BUFLEN         (64 * 1024)
@@ -166,7 +173,7 @@
 
 static void    maybe_err(const char *fmt, ...)
     __attribute__((__format__(__printf__, 1, 2)));
-#ifndef NO_BZIP2_SUPPORT
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
 static void    maybe_errx(const char *fmt, ...)
     __attribute__((__format__(__printf__, 1, 2)));
 #endif
@@ -214,6 +221,10 @@
 static off_t   zuncompress(FILE *, FILE *, char *, size_t, off_t *);
 #endif
 
+#ifndef NO_PACK_SUPPORT
+static off_t   unpack(int, int, char *, size_t, off_t *);
+#endif
+
 int main(int, char *p[]);
 
 #ifdef SMALL
@@ -408,7 +419,7 @@
        exit(2);
 }
 
-#ifndef NO_BZIP2_SUPPORT
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
 /* ... without an errno. */
 void
 maybe_errx(const char *fmt, ...)
@@ -1070,6 +1081,11 @@
                return FT_Z;
        else
 #endif
+#ifndef NO_PACK_SUPPORT
+       if (memcmp(buf, PACK_MAGIC, 2) == 0)
+               return FT_PACK;
+       else
+#endif
                return FT_UNKNOWN;
 }
 
@@ -1422,6 +1438,17 @@
        } else
 #endif
 
+#ifndef NO_PACK_SUPPORT
+       if (method == FT_PACK) {
+               if (lflag) {
+                       maybe_warnx("no -l with packed files");
+                       goto lose;
+               }
+
+               size = unpack(fd, zfd, NULL, 0, NULL);
+       } else
+#endif
+
 #ifndef SMALL
        if (method == FT_UNKNOWN) {
                if (lflag) {
@@ -1615,6 +1642,12 @@
                fclose(in);
                break;
 #endif
+#ifndef NO_PACK_SUPPORT
+       case FT_PACK:
+               usize = unpack(STDIN_FILENO, STDOUT_FILENO,
+                              (char *)header1, sizeof header1, &gsize);
+               break;
+#endif
        }
 
 #ifndef SMALL
@@ -1987,6 +2020,9 @@
 #ifndef NO_COMPRESS_SUPPORT
 #include "zuncompress.c"
 #endif
+#ifndef NO_PACK_SUPPORT
+#include "unpack.c"
+#endif
 
 static ssize_t
 read_retry(int fd, void *buf, size_t sz)
diff -r 6a263b55d828 -r fda23267b443 usr.bin/gzip/unpack.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/gzip/unpack.c     Sun Oct 11 07:07:54 2009 +0000
@@ -0,0 +1,323 @@
+/*     $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij $      */
+/*     $NetBSD: unpack.c,v 1.1 2009/10/11 07:07:54 mrg Exp $   */
+
+/*-
+ * Copyright (c) 2009 Xin LI <delphij%FreeBSD.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* This file is #included by gzip.c */
+
+/*
+ * pack(1) file format:
+ *
+ * The first 7 bytes is the header:
+ *     00, 01 - Signature (US, RS), we already validated it earlier.
+ *     02..05 - Uncompressed size
+ *         06 - Level for the huffman tree (<=24)
+ *
+ * pack(1) will then store symbols (leaf) nodes count in each huffman
+ * tree levels, each level would consume 1 byte (See [1]).
+ *
+ * After the symbol count table, there is the symbol table, storing
+ * symbols represented by coresponding leaf node.  EOB is not being
+ * explicitly transmitted (not necessary anyway) in the symbol table.
+ *
+ * Compressed data goes after the symbol table.
+ *
+ * NOTES
+ *
+ * [1] If we count EOB into the symbols, that would mean that we will
+ * have at most 256 symbols in the huffman tree.  pack(1) rejects empty
+ * file and files that just repeats one character, which means that we
+ * will have at least 2 symbols.  Therefore, pack(1) would reduce the
+ * last level symbol count by 2 which makes it a number in
+ * range [0..254], so all levels' symbol count would fit into 1 byte.
+ */
+
+#define        PACK_HEADER_LENGTH      7
+#define        HTREE_MAXLEVEL          24
+
+/*
+ * unpack descriptor
+ *
+ * Represent the huffman tree in a similiar way that pack(1) would
+ * store in a packed file.  We store all symbols in a linear table,
+ * and store pointers to each level's first symbol.  In addition to
+ * that, maintain two counts for each level: inner nodes count and
+ * leaf nodes count.
+ */
+typedef struct {
+       int             symbol_size;    /* Size of the symbol table */
+       int             treelevels;     /* Levels for the huffman tree */
+
+       int             *symbolsin;     /* Table of leaf symbols count in
+                                          each level */
+       int             *inodesin;      /* Table of internal nodes count in
+                                          each level */
+
+       char            *symbol;        /* The symbol table */
+       char            *symbol_eob;    /* Pointer to the EOB symbol */
+       char            **tree;         /* Decoding huffman tree (pointers to
+                                          first symbol of each tree level */
+
+       off_t           uncompressed_size; /* Uncompressed size */
+       FILE            *fpIn;          /* Input stream */
+       FILE            *fpOut;         /* Output stream */
+} unpack_descriptor_t;
+
+/*
+ * Release resource allocated to an unpack descriptor.
+ *
+ * Caller is responsible to make sure that all of these pointers are
+ * initialized (in our case, they all point to valid memory block).
+ * We don't zero out pointers here because nobody else would ever
+ * reference the memory block without scrubing them.
+ */
+static void
+unpack_descriptor_fini(unpack_descriptor_t *unpackd)
+{
+
+       free(unpackd->symbolsin);
+       free(unpackd->inodesin);
+       free(unpackd->symbol);
+       free(unpackd->tree);
+
+       fclose(unpackd->fpIn);
+       fclose(unpackd->fpOut);
+}
+
+/*
+ * Recursively fill the internal node count table
+ */
+static void
+unpackd_fill_inodesin(const unpack_descriptor_t *unpackd, int level)
+{
+
+       /*
+        * The internal nodes would be 1/2 of total internal nodes and
+        * leaf nodes in the next level.  For the last level there
+        * would be no internal node by defination.
+        */
+       if (level < unpackd->treelevels) {
+               unpackd_fill_inodesin(unpackd, level + 1);
+               unpackd->inodesin[level] = (unpackd->inodesin[level + 1] +
+                                         unpackd->symbolsin[level + 1]) / 2;
+       } else
+               unpackd->inodesin[level] = 0;
+}
+
+/*
+ * Update counter for accepted bytes
+ */
+static void
+accepted_bytes(off_t *bytes_in, off_t newbytes)
+{
+
+       if (bytes_in != NULL)
+               (*bytes_in) += newbytes;
+}
+
+/*
+ * Read file header and construct the tree.  Also, prepare the buffered I/O
+ * for decode rountine.
+ *
+ * Return value is uncompressed size.
+ */
+static void
+unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in,
+    unpack_descriptor_t *unpackd)
+{
+       unsigned char hdr[PACK_HEADER_LENGTH];  /* buffer for header */
+       ssize_t bytesread;              /* Bytes read from the file */
+       int i, j, thisbyte;
+
+       /* Prepend the header buffer if we already read some data */
+       if (prelen != 0)
+               memcpy(hdr, pre, prelen);
+
+       /* Read in and fill the rest bytes of header */
+       bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen);
+       if (bytesread < 0)
+               maybe_err("Error reading pack header");
+
+       accepted_bytes(bytes_in, PACK_HEADER_LENGTH);
+
+       /* Obtain uncompressed length (bytes 2,3,4,5)*/
+       unpackd->uncompressed_size = 0;
+       for (i = 2; i <= 5; i++) {
+               unpackd->uncompressed_size <<= 8;
+               unpackd->uncompressed_size |= hdr[i];
+       }
+
+       /* Get the levels of the tree */
+       unpackd->treelevels = hdr[6];



Home | Main Index | Thread Index | Old Index