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 lzip support to gzip based on the example l...



details:   https://anonhg.NetBSD.org/src/rev/174b1540f57d
branches:  trunk
changeset: 445385:174b1540f57d
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Oct 26 22:10:15 2018 +0000

description:
Add lzip support to gzip based on the example lzip decoder.

diffstat:

 usr.bin/gzip/gzip.1 |    3 +-
 usr.bin/gzip/gzip.c |   42 +++-
 usr.bin/gzip/unlz.c |  633 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 675 insertions(+), 3 deletions(-)

diffs (truncated from 766 to 300 lines):

diff -r 00e2cadd7b9d -r 174b1540f57d usr.bin/gzip/gzip.1
--- a/usr.bin/gzip/gzip.1       Fri Oct 26 20:56:35 2018 +0000
+++ b/usr.bin/gzip/gzip.1       Fri Oct 26 22:10:15 2018 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: gzip.1,v 1.30 2017/10/22 17:36:49 abhinav Exp $
+.\"    $NetBSD: gzip.1,v 1.31 2018/10/26 22:10:15 christos Exp $
 .\"
 .\" Copyright (c) 1997, 2003, 2004, 2008, 2009, 2015, 2017 Matthew R. Green
 .\" All rights reserved.
@@ -96,6 +96,7 @@
 is also capable of decompressing files compressed using
 .Xr compress 1 ,
 .Xr bzip2 1 ,
+.Ar lzip ,
 or
 .Xr xz 1 .
 .Sh OPTIONS
diff -r 00e2cadd7b9d -r 174b1540f57d usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Fri Oct 26 20:56:35 2018 +0000
+++ b/usr.bin/gzip/gzip.c       Fri Oct 26 22:10:15 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.114 2018/10/06 16:36:45 martin Exp $        */
+/*     $NetBSD: gzip.c,v 1.115 2018/10/26 22:10:15 christos Exp $      */
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
@@ -31,7 +31,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008,\
  2009, 2010, 2011, 2015, 2017 Matthew R. Green.  All rights reserved.");
-__RCSID("$NetBSD: gzip.c,v 1.114 2018/10/06 16:36:45 martin Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.115 2018/10/26 22:10:15 christos Exp $");
 #endif /* not lint */
 
 /*
@@ -85,6 +85,9 @@
 #ifndef NO_XZ_SUPPORT
        FT_XZ,
 #endif
+#ifndef NO_LZ_SUPPORT
+       FT_LZ,
+#endif
        FT_LAST,
        FT_UNKNOWN
 };
@@ -111,6 +114,11 @@
 #define XZ_MAGIC       "\3757zXZ"
 #endif
 
+#ifndef NO_LZ_SUPPORT
+#define LZ_SUFFIX      ".lz"
+#define LZ_MAGIC       "LZIP"
+#endif
+
 #define GZ_SUFFIX      ".gz"
 
 #define BUFLEN         (64 * 1024)
@@ -154,6 +162,9 @@
 #ifndef NO_XZ_SUPPORT
        SUFFIX(XZ_SUFFIX,       ""),
 #endif
+#ifndef NO_LZ_SUPPORT
+       SUFFIX(LZ_SUFFIX,       ""),
+#endif
        SUFFIX(GZ_SUFFIX,       ""),    /* Overwritten by -S "" */
 #endif /* SMALL */
 #undef SUFFIX
@@ -260,6 +271,10 @@
 static off_t   unxz_len(int);
 #endif
 
+#ifndef NO_LZ_SUPPORT
+static off_t   unlz(int, int, char *, size_t, off_t *);
+#endif
+
 #ifdef SMALL
 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
 #else
@@ -1138,6 +1153,11 @@
                return FT_XZ;
        else
 #endif
+#ifndef NO_LZ_SUPPORT
+       if (memcmp(buf, LZ_MAGIC, 4) == 0)
+               return FT_LZ;
+#endif
+       else
                return FT_UNKNOWN;
 }
 
@@ -1589,6 +1609,15 @@
                break;
 #endif
 
+#ifndef NO_LZ_SUPPORT
+       case FT_LZ:
+               if (lflag) {
+                       maybe_warnx("no -l with lzip files");
+                       goto lose;
+               }
+               size = unlz(fd, zfd, NULL, 0, NULL);
+               break;
+#endif
 #ifndef SMALL
        case FT_UNKNOWN:
                if (lflag) {
@@ -1821,6 +1850,12 @@
                             (char *)header1, sizeof header1, &gsize);
                break;
 #endif
+#ifndef NO_LZ_SUPPORT
+       case FT_LZ:
+               usize = unlz(STDIN_FILENO, STDOUT_FILENO,
+                            (char *)header1, sizeof header1, &gsize);
+               break;
+#endif
        }
 
 #ifndef SMALL
@@ -2217,6 +2252,9 @@
 #ifndef NO_XZ_SUPPORT
 #include "unxz.c"
 #endif
+#ifndef NO_LZ_SUPPORT
+#include "unlz.c"
+#endif
 
 static ssize_t
 read_retry(int fd, void *buf, size_t sz)
diff -r 00e2cadd7b9d -r 174b1540f57d usr.bin/gzip/unlz.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/gzip/unlz.c       Fri Oct 26 22:10:15 2018 +0000
@@ -0,0 +1,633 @@
+/*     $NetBSD: unlz.c,v 1.1 2018/10/26 22:10:15 christos Exp $        */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/*  Lzd - Educational decompressor for the lzip format
+    Copyright (C) 2013-2018 Antonio Diaz Diaz.
+
+    This program is free software. 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 program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+*/
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define LZ_STATES              12
+
+#define LITERAL_CONTEXT_BITS   3
+#define POS_STATE_BITS         2
+#define POS_STATES             (1 << POS_STATE_BITS)
+#define POS_STATE_MASK                 (POS_STATES - 1)
+
+#define STATES                 4
+#define DIS_SLOT_BITS          6
+
+#define DIS_MODEL_START                4
+#define DIS_MODEL_END          14
+
+#define MODELED_DISTANCES      (1 << (DIS_MODEL_END / 2))
+#define DIS_ALIGN_BITS         4
+#define DIS_ALIGN_SIZE         (1 << DIS_ALIGN_BITS)
+
+#define LOW_BITS               3
+#define MID_BITS               3
+#define HIGH_BITS              8
+
+#define LOW_SYMBOLS            (1 << LOW_BITS)
+#define MID_SYMBOLS            (1 << MID_BITS)
+#define HIGH_SYMBOLS           (1 << HIGH_BITS)
+
+#define MAX_SYMBOLS            (LOW_SYMBOLS + MID_SYMBOLS + HIGH_SYMBOLS)
+
+#define MIN_MATCH_LEN          2
+
+#define BIT_MODEL_MOVE_BITS    5
+#define BIT_MODEL_TOTAL_BITS   11
+#define BIT_MODEL_TOTAL        (1 << BIT_MODEL_TOTAL_BITS)
+#define BIT_MODEL_INIT         (BIT_MODEL_TOTAL / 2)
+
+static const int lz_st_next[] = {
+       0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5,
+};
+
+static bool
+lz_st_is_char(int st) {
+       return st < 7;
+}
+
+static int
+lz_st_get_char(int st) {
+       return lz_st_next[st];
+}
+
+static int
+lz_st_get_match(int st) {
+       return st < 7 ? 7 : 10;
+}
+
+static int
+lz_st_get_rep(int st) {
+       return st < 7 ? 8 : 11;
+}
+
+static int
+lz_st_get_short_rep(int st) {
+       return st < 7 ? 9 : 11;
+}
+
+struct lz_len_model {
+       int choice1;
+       int choice2;
+       int bm_low[POS_STATES][LOW_SYMBOLS];
+       int bm_mid[POS_STATES][MID_SYMBOLS];
+       int bm_high[HIGH_SYMBOLS];
+};
+
+static uint32_t lz_crc[256];
+
+static void
+lz_crc_init(void)
+{
+       for (unsigned i = 0; i < __arraycount(lz_crc); i++) {
+               unsigned c = i;
+               for (unsigned j = 0; j < 8; j++) {
+                       if (c & 1)
+                               c = 0xEDB88320U ^ (c >> 1);
+                       else
+                               c >>= 1;
+               }
+               lz_crc[i] = c;
+      }
+}
+
+static void
+lz_crc_update(uint32_t *crc, const uint8_t *buf, size_t len)
+{
+       for (size_t i = 0; i < len; i++)
+               *crc = lz_crc[(*crc ^ buf[i]) & 0xFF] ^ (*crc >> 8);
+}
+
+struct lz_range_decoder {
+       FILE *fp;
+       uint32_t code;
+       uint32_t range;
+};
+
+static int
+lz_rd_create(struct lz_range_decoder *rd, FILE *fp)
+{
+       rd->fp = fp;
+       rd->code = 0;
+       rd->range = ~0;
+       for (int i = 0; i < 5; i++)



Home | Main Index | Thread Index | Old Index