aboutsummaryrefslogtreecommitdiff
path: root/depends
diff options
context:
space:
mode:
Diffstat (limited to 'depends')
-rw-r--r--depends/lzma/CMakeLists.txt54
-rw-r--r--depends/lzma/LICENSE.txt9
-rw-r--r--depends/lzma/easylzma_test.c282
-rw-r--r--depends/lzma/elzma.c557
-rw-r--r--depends/lzma/include/common.h118
-rw-r--r--depends/lzma/include/compress.h77
-rw-r--r--depends/lzma/include/decompress.h58
-rw-r--r--depends/lzma/include/simple.h37
-rwxr-xr-xdepends/lzma/pavlov/7zCrc.c35
-rwxr-xr-xdepends/lzma/pavlov/7zCrc.h24
-rwxr-xr-xdepends/lzma/pavlov/LzFind.c779
-rwxr-xr-xdepends/lzma/pavlov/LzFind.h107
-rwxr-xr-xdepends/lzma/pavlov/LzHash.h62
-rwxr-xr-xdepends/lzma/pavlov/LzmaDec.c1076
-rwxr-xr-xdepends/lzma/pavlov/LzmaDec.h220
-rwxr-xr-xdepends/lzma/pavlov/LzmaEnc.c2349
-rwxr-xr-xdepends/lzma/pavlov/LzmaEnc.h71
-rwxr-xr-xdepends/lzma/pavlov/LzmaLib.c41
-rwxr-xr-xdepends/lzma/pavlov/LzmaLib.h137
-rwxr-xr-xdepends/lzma/pavlov/Types.h87
-rw-r--r--depends/lzma/wrapper/common_internal.c46
-rw-r--r--depends/lzma/wrapper/common_internal.h60
-rw-r--r--depends/lzma/wrapper/compress.c297
-rw-r--r--depends/lzma/wrapper/decompress.c263
-rw-r--r--depends/lzma/wrapper/lzip_header.c96
-rw-r--r--depends/lzma/wrapper/lzip_header.h11
-rw-r--r--depends/lzma/wrapper/lzma_header.c134
-rw-r--r--depends/lzma/wrapper/lzma_header.h10
-rw-r--r--depends/lzma/wrapper/simple.c139
-rw-r--r--depends/pack200/CMakeLists.txt43
-rw-r--r--depends/pack200/include/unpack200.h1
-rw-r--r--depends/pack200/src/bands.cpp451
-rw-r--r--depends/pack200/src/bands.h492
-rw-r--r--depends/pack200/src/bytes.cpp217
-rw-r--r--depends/pack200/src/bytes.h284
-rw-r--r--depends/pack200/src/coding.cpp1049
-rw-r--r--depends/pack200/src/coding.h270
-rw-r--r--depends/pack200/src/constants.h442
-rw-r--r--depends/pack200/src/defines.h136
-rw-r--r--depends/pack200/src/main.cpp489
-rw-r--r--depends/pack200/src/unpack.cpp5105
-rw-r--r--depends/pack200/src/unpack.h585
-rw-r--r--depends/pack200/src/utils.cpp91
-rw-r--r--depends/pack200/src/utils.h54
-rw-r--r--depends/pack200/src/zip.cpp610
-rw-r--r--depends/pack200/src/zip.h130
46 files changed, 17685 insertions, 0 deletions
diff --git a/depends/lzma/CMakeLists.txt b/depends/lzma/CMakeLists.txt
new file mode 100644
index 00000000..4df2b762
--- /dev/null
+++ b/depends/lzma/CMakeLists.txt
@@ -0,0 +1,54 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+PROJECT(lzma)
+
+IF (WIN32)
+ ADD_DEFINITIONS(-DWIN32)
+ENDIF (WIN32)
+
+SET(SRCS
+# original code by Igor Pavlov
+# Lzma version 4.63
+# Minified ~_~
+pavlov/7zCrc.c
+pavlov/7zCrc.h
+pavlov/LzFind.c
+pavlov/LzFind.h
+pavlov/LzHash.h
+pavlov/LzmaDec.c
+pavlov/LzmaDec.h
+pavlov/LzmaEnc.c
+pavlov/LzmaEnc.h
+pavlov/LzmaLib.c
+pavlov/LzmaLib.h
+pavlov/Types.h
+
+# Public headers
+include/common.h
+include/compress.h
+include/decompress.h
+include/simple.h
+
+# Wrapper by Lloyd Hilaiel (lloyd@hilaiel.com)
+wrapper/common_internal.c
+wrapper/common_internal.h
+wrapper/compress.c
+wrapper/decompress.c
+wrapper/simple.c
+wrapper/lzip_header.c
+wrapper/lzip_header.h
+wrapper/lzma_header.c
+wrapper/lzma_header.h
+)
+
+# an include directory to allow easylzma implementation to find public
+# headers
+INCLUDE_DIRECTORIES(include)
+ADD_LIBRARY(lzma STATIC ${SRCS})
+
+# lzma compress/decompress tool
+ADD_EXECUTABLE(elzma elzma.c)
+TARGET_LINK_LIBRARIES(elzma lzma)
+# a simple test...
+ADD_EXECUTABLE(easylzma_test easylzma_test.c)
+TARGET_LINK_LIBRARIES(easylzma_test lzma)
diff --git a/depends/lzma/LICENSE.txt b/depends/lzma/LICENSE.txt
new file mode 100644
index 00000000..a8a34e6a
--- /dev/null
+++ b/depends/lzma/LICENSE.txt
@@ -0,0 +1,9 @@
+# Written in 2009 by Lloyd Hilaiel
+# Butchered in 2013 by Petr Mrazek
+#
+# License
+#
+# All the cruft you find here is public domain. You don't have to credit
+# anyone to use this code, but my personal request is that you mention
+# Igor Pavlov for his hard, high quality work.
+#
diff --git a/depends/lzma/easylzma_test.c b/depends/lzma/easylzma_test.c
new file mode 100644
index 00000000..69858728
--- /dev/null
+++ b/depends/lzma/easylzma_test.c
@@ -0,0 +1,282 @@
+/*
+ * Written in 2009 by Lloyd Hilaiel
+ *
+ * License
+ *
+ * All the cruft you find here is public domain. You don't have to credit
+ * anyone to use this code, but my personal request is that you mention
+ * Igor Pavlov for his hard, high quality work.
+ *
+ * Various compiled-in tests for the easylzma library which excercise
+ * API correctness and handling of corrupt data.
+ */
+
+#include "simple.h"
+
+#include <stdio.h>
+#include <string.h>
+
+static const char *sampleData =
+ "Overview\n"
+ "\n"
+ "Easylzma is a C library and command line tools for LZMA compression and \n"
+ "decompression. It uses a Igor Pavlov's reference implementation and SDK\n"
+ "written in C.\n"
+ "\n"
+ "License\n"
+ "\n"
+ "All the cruft you find here is public domain. You don't have to credit\n"
+ "anyone to use this code, but my personal request is that you mention\n"
+ "Igor Pavlov for his hard, high quality work.\n"
+ "\n"
+ "Project Goals\n"
+ "\n"
+ "1. A tiny C wrapper and portable build system around a subset of\n"
+ " Igor Pavlov's public domain LZMA compression and decompression\n"
+ " implementation.\n"
+ "2. A tiny and straighforward API\n"
+ "3. Support for multiple different prominent LZMA file formats (see section on\n"
+ " file formats below)\n"
+ "4. easy to build and use everywhere (doze and nix alike)\n"
+ "5. public domain licensing through and through. (hats off to Igor)\n"
+ "\n"
+ "Current State:\n"
+ "\n"
+ "THIS IS A WORK IN PROGRESS. The code here should be considered pre-alpha,\n"
+ "and this should only be used by tinkerers or hackers at this point. Once\n"
+ "feature completion is attained this message will be updated. See the\n"
+ "TODO file distributed with the source for remaining work to be done.\n"
+ "\n"
+ "Platforms Supported\n"
+ "\n"
+ "0.0.2 has been successfully compiled and run basic round trip testing\n"
+ "on the following platforms & compilers:\n"
+ "\n"
+ " * win32 - visual studio 2005\n"
+ " * osx - 10.4 & 10.5 (intel)\n"
+ " * netbsd ppc - 4.0.1 with gcc 4.1.2\n"
+ " (NOTE: memory allocation errors when dict size is default)\n"
+ " * freebsd 6.1 - amd64 gcc 3.4.4\n"
+ "\n"
+ "Features\n"
+ "\n"
+ "XXX: write me (and the code)\n"
+ "\n"
+ "Usage\n"
+ "\n"
+ "XXX: write me (and the code)\n"
+ "\n"
+ "The Saga of LZMA File Formats, and a couple cents.\n"
+ "\n"
+ "As far as I can tell, there are at least four different ways to put LZMA\n"
+ "compressed data in a stream:\n"
+ "\n"
+ "1. The LZMA-Alone format, which consists of a 13 byte header including\n"
+ " compression properties, dictionary size, and the uncompressed size of\n"
+ " the file, followed by compressed data. This format has some support\n"
+ " in Igor Pavlov's reference implementation and is in widespread use, as\n"
+ " it's supported by lzmautils: http://tukaani.org/lzma/\n"
+ "\n"
+ " The canonical (afaict) implementation of this format (lzmautis) is\n"
+ " BSD licensed.\n"
+ "\n"
+ "2. The lzip format (http://www.nongnu.org/lzip/lzip.html) - which\n"
+ " includes a CRC footer and leading \"magic number\". The former\n"
+ " affords data integrity gaurantees, while the latter simplifies\n"
+ " heuristic determination of file format. This format looks to have\n"
+ " reasonably widespread usage, though not quite as significant as\n"
+ " LZMA-Alone.\n"
+ "\n"
+ " The only implementation of this format I can find (lzip) is GPL licensed.\n"
+ "\n"
+ "3. the xz format ( http://tukaani.org/xz/xz-file-format.txt ) which is\n"
+ " a more complex representation that includes CRC support and a magic\n"
+ " number. This format is to be supported by the next iteration of\n"
+ " XZ Utils which is currently in beta. The source may be obtained\n"
+ " here: git://ctrl.tukaani.org/xz.git\n"
+ "\n"
+ " This format will address some criticisms to the LZMA-Alone format and\n"
+ " was developed collaboratively by Lasse Collin (the current maintainer\n"
+ " of XZ utils) and Igor Pavlov (the author of 7zip and the refrence\n"
+ " implementation of LZMA).\n"
+ "\n"
+ " The xz format will employ LZMA2 which consists of extensions on top\n"
+ " of LZMA, in the xz utils maintainer's words:\n"
+ "\n"
+ " \"The primary compression algorithm in .xz is currently LZMA2, which\n"
+ " is an extension on top of the orignal LZMA to fix a few practical\n"
+ " issues, like adding support for flushing the encoder (equivalent\n"
+ " to zlib's Z_SYNC_FLUSH), which isn't possible with the original\n"
+ " LZMA.\"\n"
+ "\n"
+ " Again, maintainers words, regarding licensing:\n"
+ "\n"
+ " \"XZ Utils currently contains a zlib-like compression library and a \n"
+ " gzip-like command line tool. It's currently under LGPLv2.1+ but I will \n"
+ " put it into the public domain before the first stable release.\"\n"
+ "\n"
+ "4. The 7zip disk format which can contain multiple files possibly stored in\n"
+ " LZMA compressed format.\n"
+ "\n"
+ "Given the state of things, the goal of this project is to develop something\n"
+ "based on the existing formats, and quickly leverage code generated by the XZ\n"
+ "Utils project, or simply kill this thing if that project produces something\n"
+ "that's easy to embed and has a clean API at a similar level of abstraction\n"
+ "as easylzma.\n"
+ "\n"
+ "lloyd - sometime in oh nine.\n";
+
+/* a test that we can round trip compress/decompress data using LZMA or LZIP
+ * formats */
+static int roundTripTest(elzma_file_format format)
+{
+ int rc;
+ unsigned char *compressed;
+ unsigned char *decompressed;
+ size_t sz;
+
+ rc = simpleCompress(format, (unsigned char *)sampleData, strlen(sampleData), &compressed,
+ &sz);
+
+ if (rc != ELZMA_E_OK)
+ return rc;
+
+ /* gross assurance that compression is actually compressing */
+ if (sz > strlen(sampleData))
+ {
+ free(compressed);
+ return 1;
+ }
+
+ rc = simpleDecompress(format, compressed, sz, &decompressed, &sz);
+
+ free(compressed);
+
+ if (rc != ELZMA_E_OK)
+ return rc;
+
+ if (sz != strlen(sampleData) || 0 != memcmp(decompressed, sampleData, sz))
+ {
+ free(decompressed);
+ return 1;
+ }
+
+ return ELZMA_E_OK;
+}
+
+/* "correct" lzip generated from the lzip program */
+/*|LZIP...3.?..????|*/
+/*|....?e2~........|*/
+static unsigned char correctLzip[] = {
+ 0x4c, 0x5a, 0x49, 0x50, 0x01, 0x0c, 0x00, 0x33, 0x1b, 0xec, 0x15, 0x07, 0xff, 0xff,
+ 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0xa8, 0x65, 0x32, 0x7e, 0x04, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+/* "correct" lzip generated from lzma utils */
+static unsigned char correctLzma[] = {0x5d, 0x00, 0x00, 0x80, 0x00, 0x04, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x33, 0x1b, 0xec, 0x14, 0x00, 0x00, 0x00};
+
+/* lzip with a bad CRC */
+static unsigned char corruptCRC[] = {
+ 0x4c, 0x5a, 0x49, 0x50, 0x01, 0x0c, 0x00, 0x33, 0x1b, 0xec, 0x15, 0x07, 0xff, 0xff,
+ 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0xa8, 0x65, 0x31, 0x7e, 0x04, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+/* lzip with a bad uncompressed size */
+static unsigned char corruptSize[] = {
+ 0x4c, 0x5a, 0x49, 0x50, 0x01, 0x0c, 0x00, 0x33, 0x1b, 0xec, 0x15, 0x07, 0xff, 0xff,
+ 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0xa8, 0x65, 0x32, 0x7e, 0x04, 0x01, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+/* lzma with a bad uncompressed size */
+static unsigned char corruptSizeLzma[] = {0x5d, 0x00, 0x00, 0x80, 0x00, 0x04, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x33, 0x1b, 0xec, 0x14, 0x00, 0x00, 0x00};
+
+/* lzma with a bad uncompressed size */
+static unsigned char corruptSizeLzma2[] = {0x5d, 0x00, 0x00, 0x80, 0x00, 0x03, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x33, 0x1b, 0xec, 0x14, 0x00, 0x00, 0x00};
+
+/* tests */
+static struct
+{
+ const char *testName; /* the name of the test */
+ int expectedCode; /* the expected output of the test */
+ elzma_file_format format;
+ unsigned char *data; /* input data */
+ unsigned int dataSize;
+} tests[] = {
+ {"correct lzip", ELZMA_E_OK, ELZMA_lzip, correctLzip, sizeof(correctLzip)},
+ {"lzip as lzma", ELZMA_E_DECOMPRESS_ERROR, ELZMA_lzma, correctLzip, sizeof(correctLzip)},
+ {"correct lzma", ELZMA_E_OK, ELZMA_lzma, correctLzma, sizeof(correctLzma)},
+ {"lzma as lzip", ELZMA_E_CORRUPT_HEADER, ELZMA_lzip, correctLzma, sizeof(correctLzma)},
+ {"corrupt crc", ELZMA_E_CRC32_MISMATCH, ELZMA_lzip, corruptCRC, sizeof(corruptCRC)},
+ {"bad lzip size", ELZMA_E_SIZE_MISMATCH, ELZMA_lzip, corruptSize, sizeof(corruptSize)},
+ {"bad lzma size", ELZMA_E_INSUFFICIENT_INPUT, ELZMA_lzma,
+ corruptSizeLzma, sizeof(corruptSizeLzma)},
+ {"bad lzma size 2", ELZMA_E_SIZE_MISMATCH, ELZMA_lzma,
+ corruptSizeLzma2, sizeof(corruptSizeLzma2)}};
+
+int main(void)
+{
+ unsigned int i;
+ unsigned int testsPassed = 0;
+ unsigned int testsRun = 0;
+
+ int rc = 0;
+
+ printf("round trip lzma test: ");
+ fflush(stdout);
+ testsRun++;
+ if (ELZMA_E_OK != (rc = roundTripTest(ELZMA_lzma)))
+ {
+ printf("fail! (%d)\n", rc);
+ }
+ else
+ {
+ testsPassed++;
+ printf("ok\n");
+ }
+
+ printf("round trip lzip test: ");
+ fflush(stdout);
+ testsRun++;
+ if (ELZMA_E_OK != (rc = roundTripTest(ELZMA_lzip)))
+ {
+ printf("fail (%d)!\n", rc);
+ }
+ else
+ {
+ testsPassed++;
+ printf("ok\n");
+ }
+
+ /* now run through the tests table */
+ for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
+ {
+ unsigned char *decompressed = NULL;
+ size_t sz = 0;
+
+ printf("%s test: ", tests[i].testName);
+ rc = simpleDecompress(tests[i].format, tests[i].data, tests[i].dataSize, &decompressed,
+ &sz);
+
+ testsRun++;
+ if (rc != tests[i].expectedCode)
+ {
+ printf("fail - got %d - expected %d\n", rc, tests[i].expectedCode);
+ }
+ else
+ {
+ testsPassed++;
+ printf("ok\n");
+ free(decompressed);
+ }
+ }
+
+ printf("\n%d/%d tests passed\n", testsPassed, testsRun);
+
+ return (testsPassed == testsRun) ? 0 : 1;
+}
diff --git a/depends/lzma/elzma.c b/depends/lzma/elzma.c
new file mode 100644
index 00000000..f715a7b2
--- /dev/null
+++ b/depends/lzma/elzma.c
@@ -0,0 +1,557 @@
+/*
+ * Written in 2009 by Lloyd Hilaiel
+ *
+ * License
+ *
+ * All the cruft you find here is public domain. You don't have to credit
+ * anyone to use this code, but my personal request is that you mention
+ * Igor Pavlov for his hard, high quality work.
+ *
+ * command line elzma tool for lzma compression
+ *
+ * At time of writing, the primary purpose of this tool is to test the
+ * easylzma library.
+ *
+ * TODO:
+ * - stdin/stdout support
+ * - multiple file support
+ * - much more
+ */
+
+#include "include/compress.h"
+#include "include/decompress.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#ifdef WIN32
+#include <stdio.h>
+#define unlink _unlink
+#else
+#include <unistd.h>
+#endif
+
+int deleteFile(const char *path)
+{
+ return unlink(path);
+}
+
+/* a utility to open a pair of files */
+/* XXX: respect overwrite flag */
+static int openFiles(const char *ifname, FILE **inFile, const char *ofname, FILE **outFile,
+ int overwrite)
+{
+ *inFile = fopen(ifname, "rb");
+ if (*inFile == NULL)
+ {
+ fprintf(stderr, "couldn't open '%s' for reading\n", ifname);
+ return 1;
+ }
+
+ *outFile = fopen(ofname, "wb");
+ if (*outFile == NULL)
+ {
+ fprintf(stderr, "couldn't open '%s' for writing\n", ofname);
+ return 1;
+ }
+
+ return 0;
+}
+
+#define ELZMA_COMPRESS_USAGE \
+ "Compress files using the LZMA algorithm (in place by default).\n" \
+ "\n" \
+ "Usage: elzma [options] [file]\n" \
+ " -1 .. -9 compression level, -1 is fast, -9 is best (default 5)\n" \
+ " -f, --force overwrite output files if they exist\n" \
+ " -h, --help output this message and exit\n" \
+ " -k, --keep don't delete input files\n" \
+ " --lzip compress to lzip disk format (.lz extension)\n" \
+ " --lzma compress to LZMA-Alone disk format (.lzma extension)\n" \
+ " -v, --verbose output verbose status information while compressing\n" \
+ " -z, --compress compress files (default when invoking elzma program)\n" \
+ " -d, --decompress decompress files (default when invoking unelzma program)\n" \
+ "\n" \
+ "Advanced Options:\n" \
+ " -s --set-max-dict (advanced) specify maximum dictionary size in bytes\n"
+
+/* parse arguments populating output parameters, return nonzero on failure */
+static int parseCompressArgs(int argc, char **argv, unsigned char *level, char **fname,
+ unsigned int *maxDictSize, unsigned int *verbose,
+ unsigned int *keep, unsigned int *overwrite,
+ elzma_file_format *format)
+{
+ int i;
+
+ if (argc < 2)
+ return 1;
+
+ for (i = 1; i < argc; i++)
+ {
+ if (argv[i][0] == '-')
+ {
+ char *val = NULL;
+ char *arg = &(argv[i][1]);
+ if (arg[0] == '-')
+ arg++;
+
+ /* now see what argument this is */
+ if (!strcmp(arg, "h") || !strcmp(arg, "help"))
+ {
+ return 1;
+ }
+ else if (!strcmp(arg, "s") || !strcmp(arg, "set-max-dict"))
+ {
+ unsigned int j = 0;
+ val = argv[++i];
+
+ /* validate argument is numeric */
+ for (j = 0; j < strlen(val); j++)
+ {
+ if (val[j] < '0' || val[j] > '9')
+ return 1;
+ }
+
+ *maxDictSize = strtoul(val, (char **)NULL, 10);
+
+ /* don't allow dictionary sizes less than 8k */
+ if (*maxDictSize < (1 < 13))
+ *maxDictSize = 1 < 13;
+ else
+ {
+ /* make sure dict size is compatible with lzip,
+ * this will effectively collapse it to a close power
+ * of 2 */
+ *maxDictSize = elzma_get_dict_size(*maxDictSize);
+ }
+ }
+ else if (!strcmp(arg, "v") || !strcmp(arg, "verbose"))
+ {
+ *verbose = 1;
+ }
+ else if (!strcmp(arg, "f") || !strcmp(arg, "force"))
+ {
+ *overwrite = 1;
+ }
+ else if (!strcmp(arg, "k") || !strcmp(arg, "keep"))
+ {
+ *keep = 1;
+ }
+ else if (strlen(arg) == 1 && arg[0] >= '1' && arg[0] <= '9')
+ {
+ *level = arg[0] - '0';
+ }
+ else if (!strcmp(arg, "lzma"))
+ {
+ *format = ELZMA_lzma;
+ }
+ else if (!strcmp(arg, "lzip"))
+ {
+ *format = ELZMA_lzip;
+ }
+ else if (!strcmp(arg, "z") || !strcmp(arg, "d") || !strcmp(arg, "compress") ||
+ !strcmp(arg, "decompress"))
+ {
+ /* noop */
+ }
+ else
+ {
+ return 1;
+ }
+ }
+ else
+ {
+ *fname = argv[i];
+ break;
+ }
+ }
+
+ /* proper number of arguments? */
+ if (i != argc - 1 || *fname == NULL)
+ return 1;
+
+ return 0;
+}
+
+/* callbacks for streamed input and output */
+static size_t elzmaWriteFunc(void *ctx, const void *buf, size_t size)
+{
+ size_t wt;
+ FILE *f = (FILE *)ctx;
+ assert(f != NULL);
+
+ wt = fwrite(buf, 1, size, f);
+
+ return wt;
+}
+
+static int elzmaReadFunc(void *ctx, void *buf, size_t *size)
+{
+ FILE *f = (FILE *)ctx;
+ assert(f != NULL);
+ *size = fread(buf, 1, *size, f);
+
+ return 0;
+}
+
+static void printProgressHeader(void)
+{
+ printf("|0%% 50%% 100%%|\n");
+}
+
+static void endProgress(int pCtx)
+{
+ while (pCtx++ < 64)
+ {
+ printf(".");
+ }
+ printf("|\n");
+}
+
+static void elzmaProgressFunc(void *ctx, size_t complete, size_t total)
+{
+ int *dots = (int *)ctx;
+ int wantDots = (int)(64 * (double)complete / (double)total);
+ if (*dots == 0)
+ {
+ printf("|");
+ (*dots)++;
+ }
+ while (wantDots > *dots)
+ {
+ printf(".");
+ (*dots)++;
+ }
+ fflush(stdout);
+}
+
+static int doCompress(int argc, char **argv)
+{
+ /* default compression parameters, some of which may be overridded by
+ * command line arguments */
+ unsigned char level = 5;
+ unsigned char lc = ELZMA_LC_DEFAULT;
+ unsigned char lp = ELZMA_LP_DEFAULT;
+ unsigned char pb = ELZMA_PB_DEFAULT;
+ unsigned int maxDictSize = ELZMA_DICT_SIZE_DEFAULT_MAX;
+ unsigned int dictSize = 0;
+ elzma_file_format format = ELZMA_lzma;
+ char *ext = ".lzma";
+ char *ifname = NULL;
+ char *ofname = NULL;
+ unsigned int verbose = 0;
+ FILE *inFile = NULL;
+ FILE *outFile = NULL;
+ elzma_compress_handle hand = NULL;
+ /* XXX: large file support */
+ unsigned int uncompressedSize = 0;
+ unsigned int keep = 0;
+ unsigned int overwrite = 0;
+
+ if (0 != parseCompressArgs(argc, argv, &level, &ifname, &maxDictSize, &verbose, &keep,
+ &overwrite, &format))
+ {
+ fprintf(stderr, ELZMA_COMPRESS_USAGE);
+ return 1;
+ }
+
+ /* extension switching based on compression type*/
+ if (format == ELZMA_lzip)
+ ext = ".lz";
+
+ /* generate output file name */
+ {
+ ofname = malloc(strlen(ifname) + strlen(ext) + 1);
+ ofname[0] = 0;
+ strcat(ofname, ifname);
+ strcat(ofname, ext);
+ }
+
+ /* now attempt to open input and ouput files */
+ /* XXX: stdin/stdout support */
+ if (0 != openFiles(ifname, &inFile, ofname, &outFile, overwrite))
+ {
+ return 1;
+ }
+
+ /* set uncompressed size */
+ if (0 != fseek(inFile, 0, SEEK_END) || 0 == (uncompressedSize = ftell(inFile)) ||
+ 0 != fseek(inFile, 0, SEEK_SET))
+ {
+ fprintf(stderr, "error seeking input file (%s) - zero length?\n", ifname);
+ deleteFile(ofname);
+ return 1;
+ }
+
+ /* determine a reasonable dictionary size given input size */
+ dictSize = elzma_get_dict_size(uncompressedSize);
+ if (dictSize > maxDictSize)
+ dictSize = maxDictSize;
+
+ if (verbose)
+ {
+ printf("compressing '%s' to '%s'\n", ifname, ofname);
+ printf("lc/lp/pb = %u/%u/%u | dictionary size = %u bytes\n", lc, lp, pb, dictSize);
+ printf("input file is %u bytes\n", uncompressedSize);
+ }
+
+ /* allocate a compression handle */
+ hand = elzma_compress_alloc();
+ if (hand == NULL)
+ {
+ fprintf(stderr, "couldn't allocate compression object\n");
+ deleteFile(ofname);
+ return 1;
+ }
+
+ if (ELZMA_E_OK !=
+ elzma_compress_config(hand, lc, lp, pb, level, dictSize, format, uncompressedSize))
+ {
+ fprintf(stderr, "couldn't configure compression with "
+ "provided parameters\n");
+ deleteFile(ofname);
+ return 1;
+ }
+
+ {
+ int rv;
+ int pCtx = 0;
+
+ if (verbose)
+ printProgressHeader();
+
+ rv = elzma_compress_run(hand, elzmaReadFunc, (void *)inFile, elzmaWriteFunc,
+ (void *)outFile, (verbose ? elzmaProgressFunc : NULL), &pCtx);
+
+ if (verbose)
+ endProgress(pCtx);
+
+ if (ELZMA_E_OK != rv)
+ {
+ fprintf(stderr, "error compressing\n");
+ deleteFile(ofname);
+ return 1;
+ }
+ }
+
+ /* clean up */
+ elzma_compress_free(&hand);
+ fclose(inFile);
+ fclose(outFile);
+ free(ofname);
+
+ if (!keep)
+ deleteFile(ifname);
+
+ return 0;
+}
+
+#define ELZMA_DECOMPRESS_USAGE \
+ "Decompress files compressed using the LZMA algorithm (in place by default).\n" \
+ "\n" \
+ "Usage: unelzma [options] [file]\n" \
+ " -f, --force overwrite output files if they exist\n" \
+ " -h, --help output this message and exit\n" \
+ " -k, --keep don't delete input files\n" \
+ " -v, --verbose output verbose status information while decompressing\n" \
+ " -z, --compress compress files (default when invoking elzma program)\n" \
+ " -d, --decompress decompress files (default when invoking unelzma program)\n" \
+ "\n"
+/* parse arguments populating output parameters, return nonzero on failure */
+static int parseDecompressArgs(int argc, char **argv, char **fname, unsigned int *verbose,
+ unsigned int *keep, unsigned int *overwrite)
+{
+ int i;
+
+ if (argc < 2)
+ return 1;
+
+ for (i = 1; i < argc; i++)
+ {
+ if (argv[i][0] == '-')
+ {
+ char *arg = &(argv[i][1]);
+ if (arg[0] == '-')
+ arg++;
+
+ /* now see what argument this is */
+ if (!strcmp(arg, "h") || !strcmp(arg, "help"))
+ {
+ return 1;
+ }
+ else if (!strcmp(arg, "v") || !strcmp(arg, "verbose"))
+ {
+ *verbose = 1;
+ }
+ else if (!strcmp(arg, "k") || !strcmp(arg, "keep"))
+ {
+ *keep = 1;
+ }
+ else if (!strcmp(arg, "f") || !strcmp(arg, "force"))
+ {
+ *overwrite = 1;
+ }
+ else if (!strcmp(arg, "z") || !strcmp(arg, "d") || !strcmp(arg, "compress") ||
+ !strcmp(arg, "decompress"))
+ {
+ /* noop */
+ }
+ else
+ {
+ return 1;
+ }
+ }
+ else
+ {
+ *fname = argv[i];
+ break;
+ }
+ }
+
+ /* proper number of arguments? */
+ if (i != argc - 1 || *fname == NULL)
+ return 1;
+
+ return 0;
+}