Remove remaining NETBSD uppercase references

This commit is contained in:
Eirikr Hinngart 2025-05-30 22:51:16 -07:00
parent 5188a03964
commit d8ef58adf3
19 changed files with 2124 additions and 2251 deletions

View File

@ -1,5 +1,3 @@
# $NetBSD: Makefile,v 1.18 2013/11/13 11:12:24 pettai Exp $
USE_FORT?= yes # data-driven bugs?
PROG= gzip

View File

@ -1,7 +1,5 @@
#!/bin/sh -
#
# $NetBSD: gzexe,v 1.3 2004/05/01 08:22:41 wiz Exp $
#
# $OpenBSD: gzexe,v 1.3 2003/08/05 18:22:17 deraadt Exp $
#
# Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,4 +1,3 @@
.\" $NetBSD: gzexe.1,v 1.3 2003/12/28 12:49:41 wiz Exp $
.\" $OpenBSD: gzexe.1,v 1.1 2003/07/31 07:32:47 otto Exp $
.\"
.\" Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,4 +1,3 @@
.\" $NetBSD: gzip.1,v 1.25 2015/04/06 21:41:17 wiz Exp $
.\"
.\" Copyright (c) 1997, 2003, 2004 Matthew R. Green
.\" All rights reserved.

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,9 @@
/* $NetBSD: unbzip2.c,v 1.13 2009/12/05 03:23:37 mrg Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* Copyright (c) 2006 The Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* This code is derived from software contributed to The Foundation
* by Simon Burge.
*
* Redistribution and use in source and binary forms, with or without
@ -16,7 +15,7 @@
* 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
* THIS SOFTWARE IS PROVIDED BY THE 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
@ -31,108 +30,107 @@
/* This file is #included by gzip.c */
static off_t
unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
{
int ret, end_of_file, cold = 0;
off_t bytes_out = 0;
bz_stream bzs;
static char *inbuf, *outbuf;
static off_t unbzip2(int in, int out, char *pre, size_t prelen,
off_t *bytes_in) {
int ret, end_of_file, cold = 0;
off_t bytes_out = 0;
bz_stream bzs;
static char *inbuf, *outbuf;
if (inbuf == NULL)
inbuf = malloc(BUFLEN);
if (outbuf == NULL)
outbuf = malloc(BUFLEN);
if (inbuf == NULL || outbuf == NULL)
maybe_err("malloc");
if (inbuf == NULL)
inbuf = malloc(BUFLEN);
if (outbuf == NULL)
outbuf = malloc(BUFLEN);
if (inbuf == NULL || outbuf == NULL)
maybe_err("malloc");
bzs.bzalloc = NULL;
bzs.bzfree = NULL;
bzs.opaque = NULL;
bzs.bzalloc = NULL;
bzs.bzfree = NULL;
bzs.opaque = NULL;
end_of_file = 0;
ret = BZ2_bzDecompressInit(&bzs, 0, 0);
if (ret != BZ_OK)
maybe_errx("bzip2 init");
end_of_file = 0;
ret = BZ2_bzDecompressInit(&bzs, 0, 0);
if (ret != BZ_OK)
maybe_errx("bzip2 init");
/* Prepend. */
bzs.avail_in = prelen;
bzs.next_in = pre;
/* Prepend. */
bzs.avail_in = prelen;
bzs.next_in = pre;
if (bytes_in)
*bytes_in = prelen;
if (bytes_in)
*bytes_in = prelen;
while (ret == BZ_OK) {
if (bzs.avail_in == 0 && !end_of_file) {
ssize_t n;
while (ret == BZ_OK) {
if (bzs.avail_in == 0 && !end_of_file) {
ssize_t n;
n = read(in, inbuf, BUFLEN);
if (n < 0)
maybe_err("read");
if (n == 0)
end_of_file = 1;
bzs.next_in = inbuf;
bzs.avail_in = n;
if (bytes_in)
*bytes_in += n;
}
n = read(in, inbuf, BUFLEN);
if (n < 0)
maybe_err("read");
if (n == 0)
end_of_file = 1;
bzs.next_in = inbuf;
bzs.avail_in = n;
if (bytes_in)
*bytes_in += n;
}
bzs.next_out = outbuf;
bzs.avail_out = BUFLEN;
ret = BZ2_bzDecompress(&bzs);
bzs.next_out = outbuf;
bzs.avail_out = BUFLEN;
ret = BZ2_bzDecompress(&bzs);
switch (ret) {
case BZ_STREAM_END:
case BZ_OK:
if (ret == BZ_OK && end_of_file) {
/*
* If we hit this after a stream end, consider
* it as the end of the whole file and don't
* bail out.
*/
if (cold == 1)
ret = BZ_STREAM_END;
else
maybe_errx("truncated file");
}
cold = 0;
if (!tflag && bzs.avail_out != BUFLEN) {
ssize_t n;
switch (ret) {
case BZ_STREAM_END:
case BZ_OK:
if (ret == BZ_OK && end_of_file) {
/*
* If we hit this after a stream end, consider
* it as the end of the whole file and don't
* bail out.
*/
if (cold == 1)
ret = BZ_STREAM_END;
else
maybe_errx("truncated file");
}
cold = 0;
if (!tflag && bzs.avail_out != BUFLEN) {
ssize_t n;
n = write(out, outbuf, BUFLEN - bzs.avail_out);
if (n < 0)
maybe_err("write");
bytes_out += n;
}
if (ret == BZ_STREAM_END && !end_of_file) {
if (BZ2_bzDecompressEnd(&bzs) != BZ_OK ||
BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
maybe_errx("bzip2 re-init");
cold = 1;
ret = BZ_OK;
}
break;
n = write(out, outbuf, BUFLEN - bzs.avail_out);
if (n < 0)
maybe_err("write");
bytes_out += n;
}
if (ret == BZ_STREAM_END && !end_of_file) {
if (BZ2_bzDecompressEnd(&bzs) != BZ_OK ||
BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
maybe_errx("bzip2 re-init");
cold = 1;
ret = BZ_OK;
}
break;
case BZ_DATA_ERROR:
maybe_warnx("bzip2 data integrity error");
break;
case BZ_DATA_ERROR:
maybe_warnx("bzip2 data integrity error");
break;
case BZ_DATA_ERROR_MAGIC:
maybe_warnx("bzip2 magic number error");
break;
case BZ_DATA_ERROR_MAGIC:
maybe_warnx("bzip2 magic number error");
break;
case BZ_MEM_ERROR:
maybe_warnx("bzip2 out of memory");
break;
case BZ_MEM_ERROR:
maybe_warnx("bzip2 out of memory");
break;
default:
maybe_warnx("unknown bzip2 error: %d", ret);
break;
}
}
default:
maybe_warnx("unknown bzip2 error: %d", ret);
break;
}
}
if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
return (-1);
if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
return (-1);
return (bytes_out);
return (bytes_out);
}

View File

@ -1,5 +1,5 @@
/* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij $ */
/* $NetBSD: unpack.c,v 1.2 2010/11/06 21:42:32 mrg Exp $ */
/* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij
* $ */
/*-
* Copyright (c) 2009 Xin LI <delphij@FreeBSD.org>
@ -56,8 +56,8 @@
* range [0..254], so all levels' symbol count would fit into 1 byte.
*/
#define PACK_HEADER_LENGTH 7
#define HTREE_MAXLEVEL 24
#define PACK_HEADER_LENGTH 7
#define HTREE_MAXLEVEL 24
/*
* unpack descriptor
@ -69,22 +69,22 @@
* leaf nodes count.
*/
typedef struct {
int symbol_size; /* Size of the symbol table */
int treelevels; /* Levels for the huffman tree */
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 */
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 */
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 */
off_t uncompressed_size; /* Uncompressed size */
FILE *fpIn; /* Input stream */
FILE *fpOut; /* Output stream */
} unpack_descriptor_t;
/*
@ -95,48 +95,43 @@ typedef struct {
* We don't zero out pointers here because nobody else would ever
* reference the memory block without scrubbing them.
*/
static void
unpack_descriptor_fini(unpack_descriptor_t *unpackd)
{
static void unpack_descriptor_fini(unpack_descriptor_t *unpackd) {
free(unpackd->symbolsin);
free(unpackd->inodesin);
free(unpackd->symbol);
free(unpackd->tree);
free(unpackd->symbolsin);
free(unpackd->inodesin);
free(unpackd->symbol);
free(unpackd->tree);
fclose(unpackd->fpIn);
fclose(unpackd->fpOut);
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)
{
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 definition.
*/
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;
/*
* 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 definition.
*/
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)
{
static void accepted_bytes(off_t *bytes_in, off_t newbytes) {
if (bytes_in != NULL)
(*bytes_in) += newbytes;
if (bytes_in != NULL)
(*bytes_in) += newbytes;
}
/*
@ -145,179 +140,170 @@ accepted_bytes(off_t *bytes_in, off_t newbytes)
*
* 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;
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);
/* 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");
/* 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);
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];
}
/* 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];
if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1)
maybe_errx("Huffman tree has insane levels");
/* Get the levels of the tree */
unpackd->treelevels = hdr[6];
if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1)
maybe_errx("Huffman tree has insane levels");
/* Let libc take care for buffering from now on */
if ((unpackd->fpIn = fdopen(in, "r")) == NULL)
maybe_err("Can not fdopen() input stream");
if ((unpackd->fpOut = fdopen(out, "w")) == NULL)
maybe_err("Can not fdopen() output stream");
/* Let libc take care for buffering from now on */
if ((unpackd->fpIn = fdopen(in, "r")) == NULL)
maybe_err("Can not fdopen() input stream");
if ((unpackd->fpOut = fdopen(out, "w")) == NULL)
maybe_err("Can not fdopen() output stream");
/* Allocate for the tables of bounds and the tree itself */
unpackd->inodesin =
calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin)));
unpackd->symbolsin =
calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin)));
unpackd->tree =
calloc(unpackd->treelevels, (sizeof (*(unpackd->tree))));
if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL ||
unpackd->tree == NULL)
maybe_err("calloc");
/* Allocate for the tables of bounds and the tree itself */
unpackd->inodesin = calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin)));
unpackd->symbolsin =
calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin)));
unpackd->tree = calloc(unpackd->treelevels, (sizeof(*(unpackd->tree))));
if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL ||
unpackd->tree == NULL)
maybe_err("calloc");
/* We count from 0 so adjust to match array upper bound */
unpackd->treelevels--;
/* We count from 0 so adjust to match array upper bound */
unpackd->treelevels--;
/* Read the levels symbol count table and calculate total */
unpackd->symbol_size = 1; /* EOB */
for (i = 0; i <= unpackd->treelevels; i++) {
if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
maybe_err("File appears to be truncated");
unpackd->symbolsin[i] = (unsigned char)thisbyte;
unpackd->symbol_size += unpackd->symbolsin[i];
}
accepted_bytes(bytes_in, unpackd->treelevels);
if (unpackd->symbol_size > 256)
maybe_errx("Bad symbol table");
/* Read the levels symbol count table and calculate total */
unpackd->symbol_size = 1; /* EOB */
for (i = 0; i <= unpackd->treelevels; i++) {
if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
maybe_err("File appears to be truncated");
unpackd->symbolsin[i] = (unsigned char)thisbyte;
unpackd->symbol_size += unpackd->symbolsin[i];
}
accepted_bytes(bytes_in, unpackd->treelevels);
if (unpackd->symbol_size > 256)
maybe_errx("Bad symbol table");
/* Allocate for the symbol table, point symbol_eob at the beginning */
unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size);
if (unpackd->symbol == NULL)
maybe_err("calloc");
/* Allocate for the symbol table, point symbol_eob at the beginning */
unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size);
if (unpackd->symbol == NULL)
maybe_err("calloc");
/*
* Read in the symbol table, which contain [2, 256] symbols.
* In order to fit the count in one byte, pack(1) would offset
* it by reducing 2 from the actual number from the last level.
*
* We adjust the last level's symbol count by 1 here, because
* the EOB symbol is not being transmitted explicitly. Another
* adjustment would be done later afterward.
*/
unpackd->symbolsin[unpackd->treelevels]++;
for (i = 0; i <= unpackd->treelevels; i++) {
unpackd->tree[i] = unpackd->symbol_eob;
for (j = 0; j < unpackd->symbolsin[i]; j++) {
if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
maybe_errx("Symbol table truncated");
*unpackd->symbol_eob++ = (char)thisbyte;
}
accepted_bytes(bytes_in, unpackd->symbolsin[i]);
}
/*
* Read in the symbol table, which contain [2, 256] symbols.
* In order to fit the count in one byte, pack(1) would offset
* it by reducing 2 from the actual number from the last level.
*
* We adjust the last level's symbol count by 1 here, because
* the EOB symbol is not being transmitted explicitly. Another
* adjustment would be done later afterward.
*/
unpackd->symbolsin[unpackd->treelevels]++;
for (i = 0; i <= unpackd->treelevels; i++) {
unpackd->tree[i] = unpackd->symbol_eob;
for (j = 0; j < unpackd->symbolsin[i]; j++) {
if ((thisbyte = fgetc(unpackd->fpIn)) == EOF)
maybe_errx("Symbol table truncated");
*unpackd->symbol_eob++ = (char)thisbyte;
}
accepted_bytes(bytes_in, unpackd->symbolsin[i]);
}
/* Now, take account for the EOB symbol as well */
unpackd->symbolsin[unpackd->treelevels]++;
/* Now, take account for the EOB symbol as well */
unpackd->symbolsin[unpackd->treelevels]++;
/*
* The symbolsin table has been constructed now.
* Calculate the internal nodes count table based on it.
*/
unpackd_fill_inodesin(unpackd, 0);
/*
* The symbolsin table has been constructed now.
* Calculate the internal nodes count table based on it.
*/
unpackd_fill_inodesin(unpackd, 0);
}
/*
* Decode huffman stream, based on the huffman tree.
*/
static void
unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in)
{
int thislevel, thiscode, thisbyte, inlevelindex;
int i;
off_t bytes_out = 0;
const char *thissymbol; /* The symbol pointer decoded from stream */
static void unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in) {
int thislevel, thiscode, thisbyte, inlevelindex;
int i;
off_t bytes_out = 0;
const char *thissymbol; /* The symbol pointer decoded from stream */
/*
* Decode huffman. Fetch every bytes from the file, get it
* into 'thiscode' bit-by-bit, then output the symbol we got
* when one has been found.
*
* Assumption: sizeof(int) > ((max tree levels + 1) / 8).
* bad things could happen if not.
*/
thislevel = 0;
thiscode = thisbyte = 0;
/*
* Decode huffman. Fetch every bytes from the file, get it
* into 'thiscode' bit-by-bit, then output the symbol we got
* when one has been found.
*
* Assumption: sizeof(int) > ((max tree levels + 1) / 8).
* bad things could happen if not.
*/
thislevel = 0;
thiscode = thisbyte = 0;
while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) {
accepted_bytes(bytes_in, 1);
while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) {
accepted_bytes(bytes_in, 1);
/*
* Split one bit from thisbyte, from highest to lowest,
* feed the bit into thiscode, until we got a symbol from
* the tree.
*/
for (i = 7; i >= 0; i--) {
thiscode = (thiscode << 1) | ((thisbyte >> i) & 1);
/*
* Split one bit from thisbyte, from highest to lowest,
* feed the bit into thiscode, until we got a symbol from
* the tree.
*/
for (i = 7; i >= 0; i--) {
thiscode = (thiscode << 1) | ((thisbyte >> i) & 1);
/* Did we got a symbol? (referencing leaf node) */
if (thiscode >= unpackd->inodesin[thislevel]) {
inlevelindex =
thiscode - unpackd->inodesin[thislevel];
if (inlevelindex > unpackd->symbolsin[thislevel])
maybe_errx("File corrupt");
/* Did we got a symbol? (referencing leaf node) */
if (thiscode >= unpackd->inodesin[thislevel]) {
inlevelindex = thiscode - unpackd->inodesin[thislevel];
if (inlevelindex > unpackd->symbolsin[thislevel])
maybe_errx("File corrupt");
thissymbol =
&(unpackd->tree[thislevel][inlevelindex]);
if ((thissymbol == unpackd->symbol_eob) &&
(bytes_out == unpackd->uncompressed_size))
goto finished;
thissymbol = &(unpackd->tree[thislevel][inlevelindex]);
if ((thissymbol == unpackd->symbol_eob) &&
(bytes_out == unpackd->uncompressed_size))
goto finished;
fputc((*thissymbol), unpackd->fpOut);
bytes_out++;
fputc((*thissymbol), unpackd->fpOut);
bytes_out++;
/* Prepare for next input */
thislevel = 0; thiscode = 0;
} else {
thislevel++;
if (thislevel > unpackd->treelevels)
maybe_errx("File corrupt");
}
}
}
/* Prepare for next input */
thislevel = 0;
thiscode = 0;
} else {
thislevel++;
if (thislevel > unpackd->treelevels)
maybe_errx("File corrupt");
}
}
}
finished:
if (bytes_out != unpackd->uncompressed_size)
maybe_errx("Premature EOF");
if (bytes_out != unpackd->uncompressed_size)
maybe_errx("Premature EOF");
}
/* Handler for pack(1)'ed file */
static off_t
unpack(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
{
unpack_descriptor_t unpackd;
static off_t unpack(int in, int out, char *pre, size_t prelen,
off_t *bytes_in) {
unpack_descriptor_t unpackd;
unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd);
unpack_decode(&unpackd, bytes_in);
unpack_descriptor_fini(&unpackd);
unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd);
unpack_decode(&unpackd, bytes_in);
unpack_descriptor_fini(&unpackd);
/* If we reached here, the unpack was successful */
return (unpackd.uncompressed_size);
/* If we reached here, the unpack was successful */
return (unpackd.uncompressed_size);
}

View File

@ -1,10 +1,9 @@
/* $NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* Copyright (c) 2011 The Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* This code is derived from software contributed to The Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
@ -17,13 +16,13 @@
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* This product includes software developed by the
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* 4. Neither the name of The Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* THIS SOFTWARE IS PROVIDED BY THE 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
@ -36,125 +35,121 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $");
#include <stdarg.h>
#include <errno.h>
#include <lzma.h>
#include <stdarg.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;
static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
lzma_ret ret;
lzma_action action = LZMA_RUN;
off_t bytes_out, bp;
uint8_t ibuf[BUFSIZ];
uint8_t obuf[BUFSIZ];
static off_t unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in) {
lzma_stream strm = LZMA_STREAM_INIT;
static const int flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED;
lzma_ret ret;
lzma_action action = LZMA_RUN;
off_t bytes_out, bp;
uint8_t ibuf[BUFSIZ];
uint8_t obuf[BUFSIZ];
if (bytes_in == NULL)
bytes_in = &bp;
if (bytes_in == NULL)
bytes_in = &bp;
strm.next_in = ibuf;
memcpy(ibuf, pre, prelen);
strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
if (strm.avail_in == (size_t)-1)
maybe_err("read failed");
strm.avail_in += prelen;
*bytes_in = strm.avail_in;
strm.next_in = ibuf;
memcpy(ibuf, pre, prelen);
strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
if (strm.avail_in == (size_t)-1)
maybe_err("read failed");
strm.avail_in += prelen;
*bytes_in = strm.avail_in;
if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
maybe_errx("Can't initialize decoder (%d)", ret);
if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
maybe_errx("Can't initialize decoder (%d)", ret);
strm.next_out = NULL;
strm.avail_out = 0;
if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
maybe_errx("Can't read headers (%d)", ret);
strm.next_out = NULL;
strm.avail_out = 0;
if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
maybe_errx("Can't read headers (%d)", ret);
bytes_out = 0;
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
bytes_out = 0;
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
for (;;) {
if (strm.avail_in == 0) {
strm.next_in = ibuf;
strm.avail_in = read(i, ibuf, sizeof(ibuf));
switch (strm.avail_in) {
case (size_t)-1:
maybe_err("read failed");
/*NOTREACHED*/
case 0:
action = LZMA_FINISH;
break;
default:
*bytes_in += strm.avail_in;
break;
}
}
for (;;) {
if (strm.avail_in == 0) {
strm.next_in = ibuf;
strm.avail_in = read(i, ibuf, sizeof(ibuf));
switch (strm.avail_in) {
case (size_t)-1:
maybe_err("read failed");
/*NOTREACHED*/
case 0:
action = LZMA_FINISH;
break;
default:
*bytes_in += strm.avail_in;
break;
}
}
ret = lzma_code(&strm, action);
ret = lzma_code(&strm, action);
// 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;
// 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");
if (write(o, obuf, write_size) != (ssize_t)write_size)
maybe_err("write failed");
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
bytes_out += write_size;
}
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
bytes_out += 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 bytes_out;
}
}
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 bytes_out;
}
}
const char *msg;
switch (ret) {
case LZMA_MEM_ERROR:
msg = strerror(ENOMEM);
break;
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_FORMAT_ERROR:
msg = "File format not recognized";
break;
case LZMA_OPTIONS_ERROR:
// FIXME: Better message?
msg = "Unsupported compression options";
break;
case LZMA_OPTIONS_ERROR:
// FIXME: Better message?
msg = "Unsupported compression options";
break;
case LZMA_DATA_ERROR:
msg = "File is corrupt";
break;
case LZMA_DATA_ERROR:
msg = "File is corrupt";
break;
case LZMA_BUF_ERROR:
msg = "Unexpected end of input";
break;
case LZMA_BUF_ERROR:
msg = "Unexpected end of input";
break;
case LZMA_MEMLIMIT_ERROR:
msg = "Reached memory limit";
break;
case LZMA_MEMLIMIT_ERROR:
msg = "Reached memory limit";
break;
default:
maybe_errx("Unknown error (%d)", ret);
break;
}
maybe_errx("%s", msg);
}
}
default:
maybe_errx("Unknown error (%d)", ret);
break;
}
maybe_errx("%s", msg);
}
}
}

View File

@ -1,11 +1,9 @@
#!/bin/sh -
#
# $NetBSD: zdiff,v 1.5 2010/04/14 20:30:28 joerg Exp $
#
# $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
#
# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
# Copyright (c) 2010 Joerg Sonnenberger <joerg@NetBSD.org>
# Copyright (c) 2010 Joerg Sonnenberger <joerg@example.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above

View File

@ -1,8 +1,7 @@
.\" $NetBSD: zdiff.1,v 1.5 2010/04/14 19:52:05 wiz Exp $
.\" $OpenBSD: zdiff.1,v 1.2 2003/07/13 17:39:14 millert Exp $
.\"
.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
.\" Copyright (c) 2010 Joerg Sonnenberger <joerg@NetBSD.org>
.\" Copyright (c) 2010 Joerg Sonnenberger <joerg@example.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above

View File

@ -1,7 +1,5 @@
#!/bin/sh -
#
# $NetBSD: zforce,v 1.2 2003/12/28 12:43:43 wiz Exp $
#
# $OpenBSD: zforce,v 1.2 2003/08/05 18:22:17 deraadt Exp $
#
# Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,4 +1,3 @@
.\" $NetBSD: zforce.1,v 1.2 2003/12/28 12:43:43 wiz Exp $
.\" $OpenBSD: zforce.1,v 1.1 2003/07/29 11:50:09 otto Exp $
.\"
.\" Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,7 +1,5 @@
#!/bin/sh
#
# $NetBSD: zgrep,v 1.9 2015/07/06 12:05:40 nakayama Exp $
#
# Copyright (c) 2003 Thomas Klausner.
#
# Redistribution and use in source and binary forms, with or without

View File

@ -1,4 +1,3 @@
.\" $NetBSD: zgrep.1,v 1.3 2008/05/08 15:35:23 wiz Exp $
.\"
.\" Copyright (c) 2003 Thomas Klausner.
.\"
@ -91,4 +90,4 @@ In case of missing arguments or missing pattern,
.Xr zcat 1
.Sh AUTHORS
.An Thomas Klausner
.Aq wiz@NetBSD.org
.Aq wiz@example.org

View File

@ -1,7 +1,5 @@
#!/bin/sh -
#
# $NetBSD: zmore,v 1.5 2013/12/06 13:33:15 pettai Exp $
#
# $OpenBSD: zmore,v 1.6 2008/08/20 09:22:02 mpf Exp $
#
# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>

View File

@ -1,4 +1,3 @@
.\" $NetBSD: zmore.1,v 1.4 2013/11/12 21:58:37 pettai Exp $
.\" $OpenBSD: zmore.1,v 1.10 2009/08/16 09:41:08 sobrado Exp $
.\"
.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>

View File

@ -1,7 +1,5 @@
#!/bin/ksh -
#
# $NetBSD: znew,v 1.3 2008/04/27 09:07:13 nakayama Exp $
#
# $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $
#
# Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,4 +1,3 @@
.\" $NetBSD: znew.1,v 1.2 2003/12/28 12:43:43 wiz Exp $
.\" $OpenBSD: znew.1,v 1.1 2003/08/02 20:52:50 otto Exp $
.\"
.\" Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>

View File

@ -1,4 +1,3 @@
/* $NetBSD: zuncompress.c,v 1.11 2011/08/16 13:55:02 joerg Exp $ */
/*-
* Copyright (c) 1985, 1986, 1992, 1993
@ -32,49 +31,47 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: NetBSD: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp
* from: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp
*/
/* This file is #included by gzip.c */
static int zread(void *, char *, int);
static int zread(void *, char *, int);
#define tab_prefixof(i) (zs->zs_codetab[i])
#define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i]
#define de_stack ((char_type *)&tab_suffixof(1 << BITS))
#define tab_prefixof(i) (zs->zs_codetab[i])
#define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i]
#define de_stack ((char_type *)&tab_suffixof(1 << BITS))
#define BITS 16 /* Default bits. */
#define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */
#define BIT_MASK 0x1f /* Defines for third byte of header. */
#define BLOCK_MASK 0x80
#define CHECK_GAP 10000 /* Ratio check interval. */
#define BUFSIZE (64 * 1024)
#define BITS 16 /* Default bits. */
#define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */
#define BIT_MASK 0x1f /* Defines for third byte of header. */
#define BLOCK_MASK 0x80
#define CHECK_GAP 10000 /* Ratio check interval. */
#define BUFSIZE (64 * 1024)
/*
* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
* a fourth header byte (for expansion).
*/
#define INIT_BITS 9 /* Initial number of bits/code. */
#define INIT_BITS 9 /* Initial number of bits/code. */
/*
* the next two codes should not be changed lightly, as they must not
* lie within the contiguous general code space.
*/
#define FIRST 257 /* First free entry. */
#define CLEAR 256 /* Table clear output code. */
#define FIRST 257 /* First free entry. */
#define CLEAR 256 /* Table clear output code. */
#define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
#define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
typedef long code_int;
typedef long count_int;
typedef u_char char_type;
typedef long code_int;
typedef long count_int;
typedef u_char char_type;
static char_type magic_header[] = {'\037', '\235'}; /* 1F 9D */
static char_type magic_header[] =
{'\037', '\235'}; /* 1F 9D */
static char_type rmask[9] =
{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f,
0x1f, 0x3f, 0x7f, 0xff};
/* XXX zuncompress global */
off_t total_compressed_bytes;
@ -82,124 +79,116 @@ size_t compressed_prelen;
char *compressed_pre;
struct s_zstate {
FILE *zs_fp; /* File stream for I/O */
char zs_mode; /* r or w */
enum {
S_START, S_MIDDLE, S_EOF
} zs_state; /* State of computation */
int zs_n_bits; /* Number of bits/code. */
int zs_maxbits; /* User settable max # bits/code. */
code_int zs_maxcode; /* Maximum code, given n_bits. */
code_int zs_maxmaxcode; /* Should NEVER generate this code. */
count_int zs_htab [HSIZE];
u_short zs_codetab [HSIZE];
code_int zs_hsize; /* For dynamic table sizing. */
code_int zs_free_ent; /* First unused entry. */
/*
* Block compression parameters -- after all codes are used up,
* and compression rate changes, start over.
*/
int zs_block_compress;
int zs_clear_flg;
long zs_ratio;
count_int zs_checkpoint;
int zs_offset;
long zs_in_count; /* Length of input. */
long zs_bytes_out; /* Length of compressed output. */
long zs_out_count; /* # of codes output (for debugging). */
char_type zs_buf[BITS];
union {
struct {
long zs_fcode;
code_int zs_ent;
code_int zs_hsize_reg;
int zs_hshift;
} w; /* Write parameters */
struct {
char_type *zs_stackp;
int zs_finchar;
code_int zs_code, zs_oldcode, zs_incode;
int zs_roffset, zs_size;
char_type zs_gbuf[BITS];
} r; /* Read parameters */
} u;
FILE *zs_fp; /* File stream for I/O */
char zs_mode; /* r or w */
enum { S_START, S_MIDDLE, S_EOF } zs_state; /* State of computation */
int zs_n_bits; /* Number of bits/code. */
int zs_maxbits; /* User settable max # bits/code. */
code_int zs_maxcode; /* Maximum code, given n_bits. */
code_int zs_maxmaxcode; /* Should NEVER generate this code. */
count_int zs_htab[HSIZE];
u_short zs_codetab[HSIZE];
code_int zs_hsize; /* For dynamic table sizing. */
code_int zs_free_ent; /* First unused entry. */
/*
* Block compression parameters -- after all codes are used up,
* and compression rate changes, start over.
*/
int zs_block_compress;
int zs_clear_flg;
long zs_ratio;
count_int zs_checkpoint;
int zs_offset;
long zs_in_count; /* Length of input. */
long zs_bytes_out; /* Length of compressed output. */
long zs_out_count; /* # of codes output (for debugging). */
char_type zs_buf[BITS];
union {
struct {
long zs_fcode;
code_int zs_ent;
code_int zs_hsize_reg;
int zs_hshift;
} w; /* Write parameters */
struct {
char_type *zs_stackp;
int zs_finchar;
code_int zs_code, zs_oldcode, zs_incode;
int zs_roffset, zs_size;
char_type zs_gbuf[BITS];
} r; /* Read parameters */
} u;
};
static code_int getcode(struct s_zstate *zs);
static code_int getcode(struct s_zstate *zs);
static off_t
zuncompress(FILE *in, FILE *out, char *pre, size_t prelen,
off_t *compressed_bytes)
{
off_t bin, bout = 0;
char *buf;
static off_t zuncompress(FILE *in, FILE *out, char *pre, size_t prelen,
off_t *compressed_bytes) {
off_t bin, bout = 0;
char *buf;
buf = malloc(BUFSIZE);
if (buf == NULL)
return -1;
buf = malloc(BUFSIZE);
if (buf == NULL)
return -1;
/* XXX */
compressed_prelen = prelen;
if (prelen != 0)
compressed_pre = pre;
else
compressed_pre = NULL;
/* XXX */
compressed_prelen = prelen;
if (prelen != 0)
compressed_pre = pre;
else
compressed_pre = NULL;
while ((bin = fread(buf, 1, sizeof(buf), in)) != 0) {
if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) {
free(buf);
return -1;
}
bout += bin;
}
while ((bin = fread(buf, 1, sizeof(buf), in)) != 0) {
if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) {
free(buf);
return -1;
}
bout += bin;
}
if (compressed_bytes)
*compressed_bytes = total_compressed_bytes;
if (compressed_bytes)
*compressed_bytes = total_compressed_bytes;
free(buf);
return bout;
free(buf);
return bout;
}
static int
zclose(void *zs)
{
free(zs);
/* We leave the caller to close the fd passed to zdopen() */
return 0;
static int zclose(void *zs) {
free(zs);
/* We leave the caller to close the fd passed to zdopen() */
return 0;
}
FILE *
zdopen(int fd)
{
struct s_zstate *zs;
FILE *zdopen(int fd) {
struct s_zstate *zs;
if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
return (NULL);
if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
return (NULL);
zs->zs_state = S_START;
zs->zs_state = S_START;
/* XXX we can get rid of some of these */
zs->zs_hsize = HSIZE; /* For dynamic table sizing. */
zs->zs_free_ent = 0; /* First unused entry. */
zs->zs_block_compress = BLOCK_MASK;
zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */
zs->zs_ratio = 0;
zs->zs_checkpoint = CHECK_GAP;
zs->zs_in_count = 1; /* Length of input. */
zs->zs_out_count = 0; /* # of codes output (for debugging). */
zs->u.r.zs_roffset = 0;
zs->u.r.zs_size = 0;
/* XXX we can get rid of some of these */
zs->zs_hsize = HSIZE; /* For dynamic table sizing. */
zs->zs_free_ent = 0; /* First unused entry. */
zs->zs_block_compress = BLOCK_MASK;
zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */
zs->zs_ratio = 0;
zs->zs_checkpoint = CHECK_GAP;
zs->zs_in_count = 1; /* Length of input. */
zs->zs_out_count = 0; /* # of codes output (for debugging). */
zs->u.r.zs_roffset = 0;
zs->u.r.zs_size = 0;
/*
* Layering compress on top of stdio in order to provide buffering,
* and ensure that reads and write work with the data specified.
*/
if ((zs->zs_fp = fdopen(fd, "r")) == NULL) {
free(zs);
return NULL;
}
/*
* Layering compress on top of stdio in order to provide buffering,
* and ensure that reads and write work with the data specified.
*/
if ((zs->zs_fp = fdopen(fd, "r")) == NULL) {
free(zs);
return NULL;
}
return funopen(zs, zread, NULL, NULL, zclose);
return funopen(zs, zread, NULL, NULL, zclose);
}
/*
@ -208,117 +197,115 @@ zdopen(int fd)
* compressed file. The tables used herein are shared with those of the
* compress() routine. See the definitions above.
*/
static int
zread(void *cookie, char *rbp, int num)
{
u_int count, i;
struct s_zstate *zs;
u_char *bp, header[3];
static int zread(void *cookie, char *rbp, int num) {
u_int count, i;
struct s_zstate *zs;
u_char *bp, header[3];
if (num == 0)
return (0);
if (num == 0)
return (0);
zs = cookie;
count = num;
bp = (u_char *)rbp;
switch (zs->zs_state) {
case S_START:
zs->zs_state = S_MIDDLE;
break;
case S_MIDDLE:
goto middle;
case S_EOF:
goto eof;
}
zs = cookie;
count = num;
bp = (u_char *)rbp;
switch (zs->zs_state) {
case S_START:
zs->zs_state = S_MIDDLE;
break;
case S_MIDDLE:
goto middle;
case S_EOF:
goto eof;
}
/* Check the magic number */
for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--)
header[i] = *compressed_pre++;
/* Check the magic number */
for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--)
header[i] = *compressed_pre++;
if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) !=
sizeof(header) - i ||
memcmp(header, magic_header, sizeof(magic_header)) != 0) {
errno = EFTYPE;
return (-1);
}
total_compressed_bytes = 0;
zs->zs_maxbits = header[2]; /* Set -b from file. */
zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK;
zs->zs_maxbits &= BIT_MASK;
zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) {
errno = EFTYPE;
return (-1);
}
/* As above, initialize the first 256 entries in the table. */
zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) {
tab_prefixof(zs->u.r.zs_code) = 0;
tab_suffixof(zs->u.r.zs_code) = (char_type) zs->u.r.zs_code;
}
zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256;
if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) !=
sizeof(header) - i ||
memcmp(header, magic_header, sizeof(magic_header)) != 0) {
errno = EFTYPE;
return (-1);
}
total_compressed_bytes = 0;
zs->zs_maxbits = header[2]; /* Set -b from file. */
zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK;
zs->zs_maxbits &= BIT_MASK;
zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) {
errno = EFTYPE;
return (-1);
}
/* As above, initialize the first 256 entries in the table. */
zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) {
tab_prefixof(zs->u.r.zs_code) = 0;
tab_suffixof(zs->u.r.zs_code) = (char_type)zs->u.r.zs_code;
}
zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256;
zs->u.r.zs_oldcode = -1;
zs->u.r.zs_stackp = de_stack;
zs->u.r.zs_oldcode = -1;
zs->u.r.zs_stackp = de_stack;
while ((zs->u.r.zs_code = getcode(zs)) > -1) {
while ((zs->u.r.zs_code = getcode(zs)) > -1) {
if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) {
for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0;
zs->u.r.zs_code--)
tab_prefixof(zs->u.r.zs_code) = 0;
zs->zs_clear_flg = 1;
zs->zs_free_ent = FIRST;
zs->u.r.zs_oldcode = -1;
continue;
}
zs->u.r.zs_incode = zs->u.r.zs_code;
if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) {
for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--)
tab_prefixof(zs->u.r.zs_code) = 0;
zs->zs_clear_flg = 1;
zs->zs_free_ent = FIRST;
zs->u.r.zs_oldcode = -1;
continue;
}
zs->u.r.zs_incode = zs->u.r.zs_code;
/* Special case for KwKwK string. */
if (zs->u.r.zs_code >= zs->zs_free_ent) {
if (zs->u.r.zs_code > zs->zs_free_ent ||
zs->u.r.zs_oldcode == -1) {
/* Bad stream. */
errno = EINVAL;
return (-1);
}
*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar;
zs->u.r.zs_code = zs->u.r.zs_oldcode;
}
/*
* The above condition ensures that code < free_ent.
* The construction of tab_prefixof in turn guarantees that
* each iteration decreases code and therefore stack usage is
* bound by 1 << BITS - 256.
*/
/* Special case for KwKwK string. */
if (zs->u.r.zs_code >= zs->zs_free_ent) {
if (zs->u.r.zs_code > zs->zs_free_ent || zs->u.r.zs_oldcode == -1) {
/* Bad stream. */
errno = EINVAL;
return (-1);
}
*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar;
zs->u.r.zs_code = zs->u.r.zs_oldcode;
}
/*
* The above condition ensures that code < free_ent.
* The construction of tab_prefixof in turn guarantees that
* each iteration decreases code and therefore stack usage is
* bound by 1 << BITS - 256.
*/
/* Generate output characters in reverse order. */
while (zs->u.r.zs_code >= 256) {
*zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code);
zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code);
}
*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code);
/* Generate output characters in reverse order. */
while (zs->u.r.zs_code >= 256) {
*zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code);
zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code);
}
*zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code);
/* And put them out in forward order. */
middle: do {
if (count-- == 0)
return (num);
*bp++ = *--zs->u.r.zs_stackp;
} while (zs->u.r.zs_stackp > de_stack);
/* And put them out in forward order. */
middle:
do {
if (count-- == 0)
return (num);
*bp++ = *--zs->u.r.zs_stackp;
} while (zs->u.r.zs_stackp > de_stack);
/* Generate the new entry. */
if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode &&
zs->u.r.zs_oldcode != -1) {
tab_prefixof(zs->u.r.zs_code) = (u_short) zs->u.r.zs_oldcode;
tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar;
zs->zs_free_ent = zs->u.r.zs_code + 1;
}
/* Generate the new entry. */
if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode &&
zs->u.r.zs_oldcode != -1) {
tab_prefixof(zs->u.r.zs_code) = (u_short)zs->u.r.zs_oldcode;
tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar;
zs->zs_free_ent = zs->u.r.zs_code + 1;
}
/* Remember previous code. */
zs->u.r.zs_oldcode = zs->u.r.zs_incode;
}
zs->zs_state = S_EOF;
eof: return (num - count);
/* Remember previous code. */
zs->u.r.zs_oldcode = zs->u.r.zs_incode;
}
zs->zs_state = S_EOF;
eof:
return (num - count);
}
/*-
@ -328,68 +315,68 @@ eof: return (num - count);
* Outputs:
* code or -1 is returned.
*/
static code_int
getcode(struct s_zstate *zs)
{
code_int gcode;
int r_off, bits, i;
char_type *bp;
static code_int getcode(struct s_zstate *zs) {
code_int gcode;
int r_off, bits, i;
char_type *bp;
bp = zs->u.r.zs_gbuf;
if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size ||
zs->zs_free_ent > zs->zs_maxcode) {
/*
* If the next entry will be too big for the current gcode
* size, then we must increase the size. This implies reading
* a new buffer full, too.
*/
if (zs->zs_free_ent > zs->zs_maxcode) {
zs->zs_n_bits++;
if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */
zs->zs_maxcode = zs->zs_maxmaxcode;
else
zs->zs_maxcode = MAXCODE(zs->zs_n_bits);
}
if (zs->zs_clear_flg > 0) {
zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
zs->zs_clear_flg = 0;
}
/* XXX */
for (i = 0; i < zs->zs_n_bits && compressed_prelen; i++, compressed_prelen--)
zs->u.r.zs_gbuf[i] = *compressed_pre++;
zs->u.r.zs_size = fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp);
zs->u.r.zs_size += i;
if (zs->u.r.zs_size <= 0) /* End of file. */
return (-1);
zs->u.r.zs_roffset = 0;
bp = zs->u.r.zs_gbuf;
if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size ||
zs->zs_free_ent > zs->zs_maxcode) {
/*
* If the next entry will be too big for the current gcode
* size, then we must increase the size. This implies reading
* a new buffer full, too.
*/
if (zs->zs_free_ent > zs->zs_maxcode) {
zs->zs_n_bits++;
if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */
zs->zs_maxcode = zs->zs_maxmaxcode;
else
zs->zs_maxcode = MAXCODE(zs->zs_n_bits);
}
if (zs->zs_clear_flg > 0) {
zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
zs->zs_clear_flg = 0;
}
/* XXX */
for (i = 0; i < zs->zs_n_bits && compressed_prelen;
i++, compressed_prelen--)
zs->u.r.zs_gbuf[i] = *compressed_pre++;
zs->u.r.zs_size =
fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp);
zs->u.r.zs_size += i;
if (zs->u.r.zs_size <= 0) /* End of file. */
return (-1);
zs->u.r.zs_roffset = 0;
total_compressed_bytes += zs->u.r.zs_size;
total_compressed_bytes += zs->u.r.zs_size;
/* Round size down to integral number of codes. */
zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1);
}
r_off = zs->u.r.zs_roffset;
bits = zs->zs_n_bits;
/* Round size down to integral number of codes. */
zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1);
}
r_off = zs->u.r.zs_roffset;
bits = zs->zs_n_bits;
/* Get to the first byte. */
bp += (r_off >> 3);
r_off &= 7;
/* Get to the first byte. */
bp += (r_off >> 3);
r_off &= 7;
/* Get first part (low order bits). */
gcode = (*bp++ >> r_off);
bits -= (8 - r_off);
r_off = 8 - r_off; /* Now, roffset into gcode word. */
/* Get first part (low order bits). */
gcode = (*bp++ >> r_off);
bits -= (8 - r_off);
r_off = 8 - r_off; /* Now, roffset into gcode word. */
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
if (bits >= 8) {
gcode |= *bp++ << r_off;
r_off += 8;
bits -= 8;
}
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
if (bits >= 8) {
gcode |= *bp++ << r_off;
r_off += 8;
bits -= 8;
}
/* High order bits. */
gcode |= (*bp & rmask[bits]) << r_off;
zs->u.r.zs_roffset += zs->zs_n_bits;
/* High order bits. */
gcode |= (*bp & rmask[bits]) << r_off;
zs->u.r.zs_roffset += zs->zs_n_bits;
return (gcode);
return (gcode);
}