2018-03-14 21:50:18 +01:00
|
|
|
/****************************************************************************
|
2021-12-20 06:43:08 +01:00
|
|
|
* apps/system/lzf/lzf_main.c
|
2018-03-14 21:50:18 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com>
|
2018-08-13 15:47:26 +02:00
|
|
|
*
|
2021-12-20 06:43:08 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2018-08-13 15:47:26 +02:00
|
|
|
*
|
2021-12-20 06:43:08 +01:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2018-08-13 15:47:26 +02:00
|
|
|
*
|
2018-03-14 21:50:18 +01:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2021-12-20 06:43:08 +01:00
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2018-08-13 15:47:26 +02:00
|
|
|
*
|
2021-12-20 06:43:08 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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
|
2018-03-14 21:50:18 +01:00
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2021-12-20 06:31:57 +01:00
|
|
|
#include <pthread.h>
|
2018-03-14 21:50:18 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <lzf.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
#define BLOCKSIZE ((1 << CONFIG_SYSTEM_LZF_BLOG) - 1)
|
2018-03-14 21:50:18 +01:00
|
|
|
#define MAX_BLOCKSIZE BLOCKSIZE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-04 16:58:27 +02:00
|
|
|
#if CONFIG_SYSTEM_LZF != CONFIG_m
|
2021-12-20 06:31:57 +01:00
|
|
|
static pthread_mutex_t g_exclmutex = PTHREAD_MUTEX_INITIALIZER;
|
2018-03-17 22:35:19 +01:00
|
|
|
#endif
|
|
|
|
|
2018-03-14 21:50:18 +01:00
|
|
|
static off_t g_nread;
|
|
|
|
static off_t g_nwritten;
|
|
|
|
|
|
|
|
static FAR const char *g_imagename;
|
2021-12-20 06:43:08 +01:00
|
|
|
static enum
|
|
|
|
{
|
|
|
|
COMPRESS, UNCOMPRESS
|
|
|
|
} g_mode;
|
2018-03-15 17:47:51 +01:00
|
|
|
static bool g_verbose;
|
|
|
|
static bool g_force;
|
2018-03-17 22:35:19 +01:00
|
|
|
static unsigned long g_blocksize;
|
2018-03-15 15:52:13 +01:00
|
|
|
static lzf_state_t g_htab;
|
2018-03-17 22:35:19 +01:00
|
|
|
static uint8_t g_buf1[MAX_BLOCKSIZE + LZF_MAX_HDR_SIZE + 16];
|
|
|
|
static uint8_t g_buf2[MAX_BLOCKSIZE + LZF_MAX_HDR_SIZE + 16];
|
2018-03-14 21:50:18 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-12-20 06:31:57 +01:00
|
|
|
#if CONFIG_SYSTEM_LZF != CONFIG_m
|
2018-03-17 22:35:19 +01:00
|
|
|
static void lzf_exit(int exitcode) noreturn_function;
|
|
|
|
static void lzf_exit(int exitcode)
|
|
|
|
{
|
2021-12-20 06:31:57 +01:00
|
|
|
pthread_mutex_unlock(&g_exclmutex);
|
2018-03-17 22:35:19 +01:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define lzf_exit(c) exit(c)
|
|
|
|
#endif
|
|
|
|
|
2018-03-14 21:50:18 +01:00
|
|
|
static void usage(int ret)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "\n"
|
2021-12-20 06:43:08 +01:00
|
|
|
"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"
|
2018-03-15 14:59:49 +01:00
|
|
|
"http://liblzf.plan9.de/\n"
|
|
|
|
"\n"
|
2018-03-17 22:35:19 +01:00
|
|
|
"usage: lzf [-dufhvb] [file ...]\n\n"
|
|
|
|
"-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 (max %lu)\n"
|
|
|
|
"\n", (unsigned long)MAX_BLOCKSIZE);
|
|
|
|
|
|
|
|
lzf_exit(ret);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t rread(int fd, FAR void *buf, size_t len)
|
|
|
|
{
|
|
|
|
FAR char *p = buf;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
ssize_t offset = 0;
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
while (len && (ret = read(fd, &p[offset], len)) > 0)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
offset += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_nread += offset;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 0 if all written else -1 */
|
|
|
|
|
|
|
|
static inline ssize_t wwrite(int fd, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
FAR char *b = buf;
|
|
|
|
ssize_t ret;
|
|
|
|
size_t l = len;
|
|
|
|
|
|
|
|
while (l)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
ret = write(fd, b, l);
|
2018-03-14 21:50:18 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: write error: %d\n", g_imagename, errno);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
l -= ret;
|
|
|
|
b += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_nwritten += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-20 06:43:08 +01:00
|
|
|
/* Anatomy: an lzf file consists of any number of blocks
|
|
|
|
* in the following format:
|
2018-03-14 21:50:18 +01:00
|
|
|
*
|
|
|
|
* \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)
|
|
|
|
*/
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
static int compress_fd(int from, int to)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-18 19:32:15 +01:00
|
|
|
FAR struct lzf_header_s *header;
|
2018-03-14 21:50:18 +01:00
|
|
|
ssize_t us;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
g_nread = g_nwritten = 0;
|
2018-03-17 22:35:19 +01:00
|
|
|
while ((us = rread(from, &g_buf1[LZF_MAX_HDR_SIZE], g_blocksize)) > 0)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2021-12-20 06:43:08 +01:00
|
|
|
len = lzf_compress(&g_buf1[LZF_MAX_HDR_SIZE], us,
|
|
|
|
&g_buf2[LZF_MAX_HDR_SIZE],
|
2018-03-16 16:55:27 +01:00
|
|
|
us > 4 ? us - 4 : us, g_htab, &header);
|
2018-03-15 14:59:49 +01:00
|
|
|
if (wwrite(to, header, len) == -1)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uncompress_fd(int from, int to)
|
|
|
|
{
|
2018-03-16 16:55:27 +01:00
|
|
|
uint8_t header[LZF_MAX_HDR_SIZE];
|
2018-03-14 21:50:18 +01:00
|
|
|
FAR uint8_t *p;
|
|
|
|
int l;
|
|
|
|
int rd;
|
|
|
|
ssize_t ret;
|
|
|
|
ssize_t cs;
|
|
|
|
ssize_t us;
|
|
|
|
ssize_t bytes;
|
|
|
|
ssize_t over = 0;
|
|
|
|
|
|
|
|
g_nread = g_nwritten = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
2018-03-16 16:55:27 +01:00
|
|
|
ret = rread(from, header + over, LZF_MAX_HDR_SIZE - over);
|
2018-03-14 21:50:18 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: read error: %d\n", g_imagename, errno);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += over;
|
|
|
|
over = 0;
|
|
|
|
if (!ret || header[0] == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:55:27 +01:00
|
|
|
if (ret < LZF_MIN_HDR_SIZE || header[0] != 'Z' || header[1] != 'V')
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2021-12-20 06:43:08 +01:00
|
|
|
fprintf(stderr, "%s: invalid data stream - "
|
|
|
|
"magic not found or short header\n",
|
2018-03-15 14:59:49 +01:00
|
|
|
g_imagename);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (header[2])
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
cs = -1;
|
|
|
|
us = (header[3] << 8) | header[4];
|
2018-03-16 16:55:27 +01:00
|
|
|
p = &header[LZF_TYPE0_HDR_SIZE];
|
2018-03-14 21:50:18 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2018-03-16 16:55:27 +01:00
|
|
|
if (ret < LZF_TYPE1_HDR_SIZE)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
goto short_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
cs = (header[3] << 8) | header[4];
|
|
|
|
us = (header[5] << 8) | header[6];
|
2018-03-16 16:55:27 +01:00
|
|
|
p = &header[LZF_TYPE1_HDR_SIZE];
|
2018-03-14 21:50:18 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: unknown blocktype\n", g_imagename);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = cs == -1 ? us : cs;
|
|
|
|
l = &header[ret] - p;
|
|
|
|
|
|
|
|
if (l > 0)
|
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
memcpy(g_buf1, p, l);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (l > bytes)
|
|
|
|
{
|
|
|
|
over = l - bytes;
|
2018-03-15 14:59:49 +01:00
|
|
|
memmove(header, &p[bytes], over);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
p = &g_buf1[l];
|
2018-03-14 21:50:18 +01:00
|
|
|
rd = bytes - l;
|
|
|
|
if (rd > 0)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
if ((ret = rread(from, p, rd)) != rd)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
goto short_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cs == -1)
|
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
if (wwrite (to, g_buf1, us))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
if (lzf_decompress(g_buf1, cs, g_buf2, us) != us)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2021-12-20 06:43:08 +01:00
|
|
|
fprintf(stderr, "%s: decompress: invalid stream - "
|
|
|
|
"data corrupted\n",
|
2018-03-15 14:59:49 +01:00
|
|
|
g_imagename);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
if (wwrite(to, g_buf2, us))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
short_read:
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: short data\n", g_imagename);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_out(FAR const char *name)
|
|
|
|
{
|
|
|
|
int m = O_EXCL;
|
|
|
|
|
|
|
|
if (g_force)
|
|
|
|
{
|
|
|
|
m = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-20 09:02:22 +01:00
|
|
|
return open(name, O_CREAT | O_WRONLY | O_TRUNC | m, 0600);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:47:51 +01:00
|
|
|
static int compose_name(FAR const char *fname, FAR char *oname, int namelen)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
FAR char *p;
|
|
|
|
|
|
|
|
if (g_mode == COMPRESS)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
if (strlen(fname) > PATH_MAX - 4)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s.lzf: name too long", g_imagename, fname);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-03-05 14:46:37 +01:00
|
|
|
strlcpy(oname, fname, namelen);
|
2018-03-15 14:59:49 +01:00
|
|
|
p = strchr(oname, '.');
|
2018-03-15 17:47:51 +01:00
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
*p = '_'; /* _ for dot */
|
|
|
|
}
|
|
|
|
|
2023-03-05 14:46:37 +01:00
|
|
|
strlcat(oname, ".lzf", namelen);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
if (strlen(fname) > PATH_MAX)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s: name too long\n", g_imagename, fname);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(oname, fname, namelen);
|
2018-03-15 17:47:51 +01:00
|
|
|
p = strstr(oname, ".lzf");
|
|
|
|
if (p == NULL)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s: unknown suffix\n", g_imagename, fname);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = 0;
|
2018-03-15 17:47:51 +01:00
|
|
|
p = strchr(oname, '_');
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
*p = '.';
|
|
|
|
}
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:47:51 +01:00
|
|
|
static int run_file(FAR const char *fname)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
struct stat mystat;
|
2023-03-05 14:46:37 +01:00
|
|
|
char oname[PATH_MAX];
|
2018-03-15 17:47:51 +01:00
|
|
|
int fd;
|
|
|
|
int fd2;
|
|
|
|
int ret;
|
2018-03-14 21:50:18 +01:00
|
|
|
|
2023-03-05 14:46:37 +01:00
|
|
|
if (compose_name(fname, oname, sizeof(oname)))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-15 16:23:44 +01:00
|
|
|
return -1;
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
ret = stat(fname, &mystat);
|
|
|
|
fd = open(fname, O_RDONLY);
|
2018-03-14 21:50:18 +01:00
|
|
|
|
|
|
|
if (ret || fd == -1)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s: %d\n", g_imagename, fname, errno);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
if (!S_ISREG(mystat.st_mode))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s: not a regular file.\n", g_imagename, fname);
|
|
|
|
close(fd);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd2 = open_out(oname);
|
|
|
|
if (fd2 == -1)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %s: %d\n", g_imagename, oname, errno);
|
|
|
|
close(fd);
|
2018-03-14 21:50:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_mode == COMPRESS)
|
|
|
|
{
|
|
|
|
ret = compress_fd(fd, fd2);
|
|
|
|
if (!ret && g_verbose)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %5.1f%% -- replaced with %s\n",
|
|
|
|
fname, g_nread == 0 ? 0 :
|
|
|
|
100.0 - g_nwritten / ((double) g_nread / 100.0), oname);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = uncompress_fd(fd, fd2);
|
|
|
|
if (!ret && g_verbose)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
fprintf(stderr, "%s: %5.1f%% -- replaced with %s\n",
|
|
|
|
fname, g_nwritten == 0 ? 0 :
|
|
|
|
100.0 - g_nread / ((double) g_nwritten / 100.0), oname);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
close(fd);
|
|
|
|
close(fd2);
|
2018-03-14 21:50:18 +01:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
unlink(fname);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* lzf_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
FAR char *p = argv[0];
|
|
|
|
int optc;
|
|
|
|
int ret = 0;
|
|
|
|
|
2019-10-04 16:58:27 +02:00
|
|
|
#if CONFIG_SYSTEM_LZF != CONFIG_m
|
2018-03-17 22:35:19 +01:00
|
|
|
/* Get exclusive access to the global variables. Global variables are
|
|
|
|
* used because the hash table and buffers are too large to allocate on
|
|
|
|
* the embedded stack. But the use of global variables has the downside
|
|
|
|
* or forcing serialization of this logic in order to work in a multi-
|
|
|
|
* tasking environment.
|
|
|
|
*
|
|
|
|
* REVISIT: An alternative would be to pack all of the globals into a
|
|
|
|
* structure and allocate a per-thread instance of that structure here.
|
|
|
|
*
|
|
|
|
* NOTE: This applies only in the FLAT and PROTECTED build modes. In the
|
|
|
|
* KERNEL build mode, this will be a separate process with its own private
|
|
|
|
* global variables.
|
|
|
|
*/
|
|
|
|
|
2021-12-20 06:31:57 +01:00
|
|
|
pthread_mutex_lock(&g_exclmutex);
|
2018-03-17 22:35:19 +01:00
|
|
|
#endif
|
|
|
|
|
2018-03-15 17:47:51 +01:00
|
|
|
/* Set defaults. */
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
g_mode = COMPRESS;
|
|
|
|
g_verbose = false;
|
|
|
|
g_force = 0;
|
|
|
|
g_blocksize = BLOCKSIZE;
|
2018-03-15 17:47:51 +01:00
|
|
|
|
2018-03-14 21:50:18 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2018-03-15 16:23:44 +01:00
|
|
|
/* Block size may be specified as an environment variable */
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
p = getenv("LZF_BLOCKSIZE");
|
2018-03-14 21:50:18 +01:00
|
|
|
if (p)
|
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
g_blocksize = strtoul(p, 0, 0);
|
|
|
|
if (g_blocksize == 0 || g_blocksize > MAX_BLOCKSIZE)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
g_blocksize = BLOCKSIZE;
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-15 16:23:44 +01:00
|
|
|
/* Get the program name sans path */
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
p = strrchr(argv[0], '/');
|
2018-03-14 21:50:18 +01:00
|
|
|
g_imagename = p ? ++p : argv[0];
|
|
|
|
|
2018-03-15 16:23:44 +01:00
|
|
|
/* Handle command line options */
|
2018-03-14 21:50:18 +01:00
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
while ((optc = getopt(argc, argv, "cdfhvb:")) != -1)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
|
|
|
switch (optc)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
g_mode = COMPRESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
g_mode = UNCOMPRESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
g_force = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
2018-03-15 14:59:49 +01:00
|
|
|
usage(0);
|
2018-03-14 21:50:18 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
g_verbose = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2018-03-17 22:35:19 +01:00
|
|
|
g_blocksize = strtoul(optarg, 0, 0);
|
|
|
|
if (g_blocksize == 0 || g_blocksize > MAX_BLOCKSIZE)
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2018-03-17 22:35:19 +01:00
|
|
|
g_blocksize = BLOCKSIZE;
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind == argc)
|
|
|
|
{
|
|
|
|
/* stdin stdout */
|
|
|
|
|
|
|
|
if (!g_force)
|
|
|
|
{
|
2018-03-15 16:23:44 +01:00
|
|
|
if ((g_mode == UNCOMPRESS) && isatty(0))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2021-12-20 06:43:08 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: compressed data not read from a terminal. "
|
2018-03-15 14:59:49 +01:00
|
|
|
"Use -f to force decompression.\n", g_imagename);
|
2018-03-17 22:35:19 +01:00
|
|
|
lzf_exit(1);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 14:59:49 +01:00
|
|
|
if (g_mode == COMPRESS && isatty(1))
|
2018-03-14 21:50:18 +01:00
|
|
|
{
|
2021-12-20 06:43:08 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: compressed data not written to a terminal. "
|
2018-03-15 14:59:49 +01:00
|
|
|
"Use -f to force compression.\n", g_imagename);
|
2018-03-17 22:35:19 +01:00
|
|
|
lzf_exit(1);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_mode == COMPRESS)
|
|
|
|
{
|
|
|
|
ret = compress_fd(0, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = uncompress_fd(0, 1);
|
|
|
|
}
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
lzf_exit(ret ? 1 : 0);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
{
|
2018-03-15 14:59:49 +01:00
|
|
|
ret |= run_file(argv[optind++]);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 22:35:19 +01:00
|
|
|
lzf_exit(ret ? 1 : 0);
|
2018-03-14 21:50:18 +01:00
|
|
|
}
|