From 61dc82f3e306b25ce3cd3d529df9ec7a0ec04b73 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Fri, 18 Jan 2008 20:18:08 +0200 Subject: [PATCH] Added the debug directory and the first debug tool (sync_flush). These tools are not built unless the user runs "make" in the debug directory. --- Makefile.am | 1 + configure.ac | 1 + debug/Makefile.am | 30 ++++++++++++ debug/README | 17 +++++++ debug/sync_flush.c | 116 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 debug/Makefile.am create mode 100644 debug/README create mode 100644 debug/sync_flush.c diff --git a/Makefile.am b/Makefile.am index 1060ffe4..593fa3be 100644 --- a/Makefile.am +++ b/Makefile.am @@ -12,6 +12,7 @@ ## Lesser General Public License for more details. ## +DIST_SUBDIRS = lib src po tests debug SUBDIRS = if COND_GNULIB diff --git a/configure.ac b/configure.ac index a25b9b02..dbafd73c 100644 --- a/configure.ac +++ b/configure.ac @@ -646,6 +646,7 @@ AC_CONFIG_FILES([ src/lzmadec/Makefile src/scripts/Makefile tests/Makefile + debug/Makefile ]) AC_OUTPUT diff --git a/debug/Makefile.am b/debug/Makefile.am new file mode 100644 index 00000000..b393db3e --- /dev/null +++ b/debug/Makefile.am @@ -0,0 +1,30 @@ +## +## Copyright (C) 2008 Lasse Collin +## +## This library is free software; you can redistribute it and/or +## modify it under the terms of the GNU Lesser General Public +## License as published by the Free Software Foundation; either +## version 2.1 of the License, or (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## Lesser General Public License for more details. +## + +noinst_PROGRAMS = \ + sync_flush + +AM_CPPFLAGS = \ + -I@top_srcdir@/src/common \ + -I@top_srcdir@/src/liblzma/api + +AM_LDFLAGS = -static + +LDADD = \ + @top_builddir@/src/liblzma/liblzma.la \ + @LTLIBINTL@ + +if COND_GNULIB +LDADD += @top_builddir@/lib/libgnu.a +endif diff --git a/debug/README b/debug/README new file mode 100644 index 00000000..749610d7 --- /dev/null +++ b/debug/README @@ -0,0 +1,17 @@ + +Debug tools +----------- + + This directory contains a few tiny programs that may be helpful when + debugging LZMA Utils. + + These tools are not meant to be installed. Often one needs to edit + the source code a little to make the programs do the wanted things. + If you don't know how these programs could help you, it is likely + that they really are useless to you. + + These aren't intended to be used as example programs. They take some + shortcuts here and there, which correct programs should not do. Many + possible errors (especially I/O errors) are ignored. Don't report + bugs or send patches to fix this kind of bugs. + diff --git a/debug/sync_flush.c b/debug/sync_flush.c new file mode 100644 index 00000000..24b8de61 --- /dev/null +++ b/debug/sync_flush.c @@ -0,0 +1,116 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file sync_flush.c +/// \brief Encode files using LZMA_SYNC_FLUSH +// +// Copyright (C) 2008 Lasse Collin +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "sysdefs.h" +#include + + +static lzma_stream strm = LZMA_STREAM_INIT; +static FILE *file_in; + + +static void +encode(size_t size, lzma_action action) +{ + static const size_t CHUNK = 64; + uint8_t in[CHUNK]; + uint8_t out[CHUNK]; + lzma_ret ret; + + do { + if (strm.avail_in == 0 && size > 0) { + const size_t amount = MIN(size, CHUNK); + strm.avail_in = fread(in, 1, amount, file_in); + strm.next_in = in; + size -= amount; // Intentionally not using avail_in. + } + + strm.next_out = out; + strm.avail_out = CHUNK; + + ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN); + + if (ret != LZMA_OK && ret != LZMA_STREAM_END) { + fprintf(stderr, "%s:%u: %s: ret == %d\n", + __FILE__, __LINE__, __func__, ret); + exit(1); + } + + fwrite(out, 1, CHUNK - strm.avail_out, stdout); + + } while (size > 0 || strm.avail_out == 0); + + if ((action == LZMA_RUN && ret != LZMA_OK) + || (action != LZMA_RUN && ret != LZMA_STREAM_END)) { + fprintf(stderr, "%s:%u: %s: ret == %d\n", + __FILE__, __LINE__, __func__, ret); + exit(1); + } +} + + +int +main(int argc, char **argv) +{ + lzma_init_encoder(); + + file_in = argc > 1 ? fopen(argv[1], "rb") : stdin; + + // Config + lzma_options_lzma opt_lzma = { + .dictionary_size = 1 << 16, + .literal_context_bits = LZMA_LITERAL_CONTEXT_BITS_DEFAULT, + .literal_pos_bits = LZMA_LITERAL_POS_BITS_DEFAULT, + .pos_bits = LZMA_POS_BITS_DEFAULT, + .preset_dictionary = NULL, + .mode = LZMA_MODE_BEST, + .fast_bytes = 32, + .match_finder = LZMA_MF_BT3, + .match_finder_cycles = 0, + }; + + lzma_options_stream opt_stream = { + .check = LZMA_CHECK_NONE, + .has_crc32 = false, + .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN, + .alignment = 0, + }; + opt_stream.filters[0].id = LZMA_FILTER_LZMA; + opt_stream.filters[0].options = &opt_lzma; + opt_stream.filters[1].id = LZMA_VLI_VALUE_UNKNOWN; + + // Init + if (lzma_stream_encoder_single(&strm, &opt_stream) != LZMA_OK) { + fprintf(stderr, "init failed\n"); + exit(1); + } + + // Encoding + encode(0, LZMA_SYNC_FLUSH); + encode(6, LZMA_SYNC_FLUSH); + encode(0, LZMA_SYNC_FLUSH); + encode(6, LZMA_SYNC_FLUSH); + encode(0, LZMA_SYNC_FLUSH); + encode(0, LZMA_FINISH); + + // Clean up + lzma_end(&strm); + + return 0; +}