lzf.c revision a260560bdbc7bd737d59547882cd5e8d77b80d0a
/*
* Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com>
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, 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
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, 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 OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include "lzf.h"
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#define MAX_BLOCKSIZE BLOCKSIZE
typedef unsigned char u8;
static const char *imagename;
static int verbose = 0;
static int force = 0;
#ifdef HAVE_GETOPT_LONG
{"compress", 0, 0, 'c'},
{"decompress", 0, 0, 'd'},
{"uncompress", 0, 0, 'd'},
{"force", 0, 0, 'f'},
{"help", 0, 0, 'h'},
{"verbose", 0, 0, 'v'},
{"blocksize", 1, 0, 'b'},
{0, 0, 0, 0}
};
static const char *opt =
"-c --compress compress\n"
"-d --decompress decompress\n"
"-f --force force overwrite of output file\n"
"-h --help give this help\n" "-v --verbose verbose mode\n" "-b # --blocksize # set blocksize\n" "\n";
#else
static const char *opt =
"-c compress\n"
"-d decompress\n"
"-f force overwrite of output file\n"
"-h give this help\n"
"-v verbose mode\n"
"-b # set blocksize\n"
"\n";
#endif
static void
{
"lzf, a very lightweight compression/decompression utility written by Stefan Traby.\n"
"uses liblzf written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n"
"\n"
"usage: lzf [-dufhvb] [file ...]\n"
" unlzf [file ...]\n"
" lzcat [file ...]\n"
"\n%s",
opt);
}
static inline ssize_t
{
char *p = buf;
{
}
if (rc < 0)
return rc;
return offset;
}
/* returns 0 if all written else -1 */
static inline ssize_t
{
char *b = buf;
while (l)
{
if (rc < 0)
{
perror ("");
return -1;
}
l -= rc;
b += rc;
}
nr_written += len;
return 0;
}
/*
* Anatomy: an lzf file consists of any number of blocks in the following format:
*
* \x00 EOF (optional)
* "ZV\0" 2-byte-usize <uncompressed data>
* "ZV\1" 2-byte-csize 2-byte-usize <compressed data>
* "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI)
*/
#define TYPE0_HDR_SIZE 5
#define TYPE1_HDR_SIZE 7
#define MAX_HDR_SIZE 7
#define MIN_HDR_SIZE 5
static int
{
nr_read = nr_written = 0;
{
if (cs)
{
header[0] = 'Z';
}
else
{ // write uncompressed
header[0] = 'Z';
header[2] = 0;
}
return -1;
}
return 0;
}
static int
{
u8 *p;
int l, rd;
nr_read = nr_written = 0;
while (1)
{
if (rc < 0)
{
perror ("");
return -1;
}
over = 0;
return 0;
{
return -1;
}
switch (header[2])
{
case 0:
cs = -1;
p = &header[TYPE0_HDR_SIZE];
break;
case 1:
if (rc < TYPE1_HDR_SIZE)
{
goto short_read;
}
p = &header[TYPE1_HDR_SIZE];
break;
default:
return -1;
}
if (l > 0)
if (l > bytes)
{
}
p = &buf1[l];
if (rd > 0)
goto short_read;
if (cs == -1)
{
return -1;
}
else
{
{
return -1;
}
return -1;
}
}
return 0;
return -1;
}
static int
{
int fd;
int m = O_EXCL;
if (force)
m = 0;
#if defined(__MINGW32__)
#endif
return fd;
}
static int
{
char *p;
{
{
return -1;
}
}
else
{
{
return -1;
}
{
return -1;
}
*p = 0;
}
return 0;
}
static int
{
int rc;
return -1;
#if !defined(__MINGW32__)
#else
#endif
#if defined(__MINGW32__)
#endif
{
perror ("");
return -1;
}
{
return -1;
}
{
return rc;
}
if (fd2 == -1)
{
perror ("");
return -1;
}
{
}
else
{
}
#if !defined(__MINGW32__)
#else
#endif
if (!rc)
return rc;
}
int
{
char *p = argv[0];
int optc;
int rc = 0;
errno = 0;
p = getenv ("LZF_BLOCKSIZE");
if (p)
{
}
mode = uncompress;
#ifdef HAVE_GETOPT_LONG
#else
#endif
{
switch (optc)
{
case 'c':
break;
case 'd':
mode = uncompress;
break;
case 'f':
force = 1;
break;
case 'h':
usage (0);
break;
case 'v':
verbose = 1;
break;
case 'b':
errno = 0;
break;
default:
usage (1);
break;
}
}
{ // stdin stdout
if (!force)
{
{
fprintf (stderr, "%s: compressed data not read from a terminal. Use -f to force decompression.\n", imagename);
exit (1);
}
{
fprintf (stderr, "%s: compressed data not written to a terminal. Use -f to force compression.\n", imagename);
exit (1);
}
}
else
}
}