Merge branch 'jn/5.1.1alpha+20120614'

Recent changes from upstream, including a documentation update and
small correctness fix for the "minimal version to decode" field.
Thanks to Lasse Collin for advice about which patches to apply.

After these changes, the xz and liblzma API and ABI match 5.1.2alpha,
except that no features that were not already present in Debian sid
have been added.
diff --git a/configure.ac b/configure.ac
index 25eb838..0c2f2fc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -471,7 +471,40 @@
 	AC_CHECK_DECLS([CLOCK_MONOTONIC], [], [], [[#include <time.h>]])
 	CFLAGS=$OLD_CFLAGS
 fi
-AM_CONDITIONAL([COND_THREADS], [test "x$ax_pthread_ok" = xyes])
+
+# As a Debian-specific hack, liblzma can use dlopen() to check if extra
+# paranoia is needed because unversioned symbols from liblzma.so.2 are
+# present in the same process.  See src/liblzma/common/common.c.
+AC_MSG_CHECKING([if lzma_code checks should be relaxed for compatibility])
+AC_ARG_ENABLE([liblzma2-compat], [AC_HELP_STRING([--enable-liblzma2-compat],
+		[Relax run-time checks to accomodate old binaries built
+		with smaller sizeof(lzma_stream).  The default is "dynamic",
+		which means to only use the relaxed checks when the dynamic
+		loader reports that liblzma.so.2 is loaded in the same process.])],
+	[], [enable_liblzma2_compat=dynamic])
+case $enable_liblzma2_compat in
+dynamic)
+	AC_SEARCH_LIBS([dlopen], [dl])
+	AC_DEFINE([LIBLZMA2_COMPAT_DYNAMIC], [1],
+		[Define to 1 to use dlopen() to check if lzma_code() checks
+		should be more tolerant because the process is also linked to
+		liblzma from Debian 6.0.])
+	AC_MSG_RESULT([auto])
+	;;
+yes)
+	AC_DEFINE([LIBLZMA2_COMPAT], [1],
+		[Define to 1 to unconditionally make lzma_code() checks tolerant
+		to accomodate callers built against liblzma from Debian 6.0.])
+	AC_MSG_RESULT([yes])
+	;;
+no)
+	AC_MSG_RESULT([no])
+	;;
+*)
+	AC_MSG_RESULT([])
+	AC_MSG_ERROR([--enable-liblzma2: unrecognized value $enable_liblzma2_compat])
+	;;
+esac
 
 echo
 echo "Initializing Libtool:"
diff --git a/debian/.gitignore b/debian/.gitignore
new file mode 100644
index 0000000..7936ab7
--- /dev/null
+++ b/debian/.gitignore
@@ -0,0 +1,23 @@
+/changelog.upstream
+/generated-m4.list
+/generated-po.list
+/generated-build-aux.list
+
+*.debhelper.log
+*.debhelper
+
+/*-stamp
+
+/normal-build/
+/static-build/
+/xzdec-build/
+/tmp/
+
+/liblzma5/
+/xz-utils/
+/xzdec/
+/liblzma-dev/
+/liblzma-doc/
+
+/files
+*.substvars
diff --git a/debian/README.source b/debian/README.source
new file mode 100644
index 0000000..f17a7de
--- /dev/null
+++ b/debian/README.source
@@ -0,0 +1,16 @@
+XZ Utils sources for Debian
+---------------------------
+
+This Debian package is developed in a public Git repository (see the Vcs-Git
+field of debian/control) based on XZ Utils’s repository on tukaani.org.
+To build from a git checkout:
+
+	debian/rules get-orig-source
+	mv xz-utils_*.tar.gz ..
+	debian/autogen.sh
+	debuild
+
+To build a released version, no special instructions apply.
+“dpkg-buildpackage” and “apt-get -b xz-utils” should work as usual.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 08 Feb 2010 17:30:00 -0600
diff --git a/debian/autogen.sh b/debian/autogen.sh
new file mode 100755
index 0000000..05a3a9f
--- /dev/null
+++ b/debian/autogen.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+# Generate debian/changelog.upstream, debian/generated-m4.list,
+# and debian/generated-po.list.
+#
+# Uses debian/changelog, the git revision log, and .gitignore
+# files from the current checkout.
+
+set -e
+
+changelog_needs_update() {
+	test -e debian/changelog.upstream &&
+	read line < debian/changelog.upstream ||
+	return 0
+
+	ver=${line#Version } &&
+	ver=${ver%;*} &&
+	test "$ver" != "" ||
+	return 0
+
+	read line < debian/changelog &&
+	rhs=${line#*(} &&
+	deb_ver=${rhs%)*} &&
+	new_ver=${deb_ver%-*} ||
+	return 0
+
+	test "$ver" != "$new_ver"
+}
+
+cp -f m4/.gitignore debian/generated-m4.list
+cp -f po/.gitignore debian/generated-po.list
+sed -n 's,^build-aux/,, p' .gitignore > debian/generated-build-aux.list
+! changelog_needs_update || exec sh debian/changelog.upstream.sh
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..5f8a175
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,626 @@
+xz-utils (5.1.1alpha+20120614-1.1) unstable; urgency=low
+
+  * liblzma: report a LZMA_DATA_ERROR when range encoded data starts
+    with a nonzero byte.  This is a sanity check to catch malformed
+    files that no known encoders produce.
+  * xz -v -v --list: Support for decompressing blocks with zero-length
+    uncompressed data was added in xz 5.0.2, not 5.0.3.
+  * xz.1: "xz --robot -v -v --list" gained a "minimum xz version to
+    decompress" field.
+  * xz-utils/README.Debian: Document differences from upstream.
+    Closes: #685217.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 10 Sep 2012 14:00:55 -0700
+
+xz-utils (5.1.1alpha+20120614-1) unstable; urgency=low
+
+  * New snapshot, taken from upstream commit f1675f76.
+    - liblzma: BCJ filters would return a spurious LZMA_BUF_ERROR
+      result in certain circumstances.  As a result, the xz tool
+      could produce inappropriate "Compressed data is corrupt" or
+      "Unexpected end of input" failures (reported in XZ Embedded as
+      Fedora bug 735408).
+    - "xz -v -v --list" prints the minimal XZ Utils version needed
+      to decompress a stream.
+    - "xz --robot -v --list" formats filenames more plainly.
+    - xzgrep does not fail unconditionally when the decompressor
+      dies with SIGPIPE due to unconsumed output any more.  This
+      makes the exit status from commands such as "xzgrep -q" and
+      "xzgrep -l" more predictable and convenient for scripts.
+    - examples/xz_pipe_decomp.c did not check correctly for
+      truncated input.
+    - There is a new set of sample programs.  The old ones have
+      been renamed to examples_old but are still maintained.
+  * liblzma:
+    - If dlopen() indicates that liblzma.so.2 is loaded, check fewer
+      reserved fields in the stream argument to the lzma_code()
+      function.  This prevents out-of-bounds reads when liblzma5
+      functions are called by code built against liblzma2 (see
+      <http://sourceware.org/PR12977>).
+    - Remove the lzma_code@Base compatibility symbol.
+      Closes: #649522.  Thanks to Eduard Bloch.
+    - Install run-time library to /lib so it can be used before /usr
+      is mounted.  Closes: #672120.
+    - Configure with --disable-threads for now to avoid needlessly
+      linking to libpthread and librt.
+  * liblzma-dev:
+    - Install an appropriate library for static linking instead of
+      the decompression-only version used to build xzdec.
+      Closes: #673001.  Thanks to Anton Tolchanov.
+    - Use the liblzma2-compatible version of lzma_code() in the
+      static library.
+    - Multi-Arch: same.  Thanks to Riku Voipio.
+    - Install new examples and revised old examples to
+      /usr/share/doc/liblzma-dev.
+  * xz-utils:
+    - Use update-alternatives to provide lzma, unlzma, lzcat, and
+      the rest of the lz* scripts without conflicting with the lzma
+      package.
+    - Conflicts: lzma versions before before 9.22-1, when it started
+      providing lzma, unlzma, and lzcat through the alternatives
+      system.
+    - Conflicts: and Replaces: xz-lzma.
+    - README.Debian: Update instructions on configuring the package
+      to provide lzma compatibility commands.
+  * Remove xz-lzma package.
+  * Use dpkg-buildflags to retrieve compiler flags, including
+    hardening options.  Build-Depends: dpkg-dev (>= 1.16.1).
+    Closes: #653739.
+  * debian/rules:
+    - build-arch: Simplify by using a double-colon rule instead of a
+      dependency on a dummy noop target.
+    - build-indep: Assume doxygen is available, instead of trying to
+      get by by assuming the caller meant to invoke build-arch when
+      it is missing.  Build-Depends: dpkg-dev (>= 1.16.2).
+    - Rearrange for clarity.
+  * debian/symbols: Bump the minimal version for lzma_code(),
+    lzma_raw_buffer_decode(), and lzma_raw_buffer_encode().
+  * Use machine-readable copyright-format 1.0.
+  * Update copyright file.
+  * Standards-Version: 3.9.3 (checked).
+  * debian/changelog.upstream.sh: Use ‘git rev-list | git diff-tree’
+    instead of ‘git log’ to avoid depending on the build system’s
+    configuration.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Sat, 16 Jun 2012 15:03:17 -0500
+
+xz-utils (5.1.1alpha+20110809-3) unstable; urgency=low
+
+  * liblzma: Match upstream ABI.
+    - Remove the lzma_chunk_size() function.
+    - A few ABI tweaks to reserved space in structures.
+    - Enable ELF symbol versioning.
+    - Bump soname to 5.
+    - Continue to leave out threading support, since the relevant
+      interfaces in liblzma are not yet stable.
+  * xz-utils/README.Debian: Remove note on ABI differences.
+  * Remove liblzma/README.Debian.
+  * liblzma: Introduce a lzma_code@Base compatibility symbol to ensure
+    programs linked against unversioned symbols from liblzma2 can
+    share a process image with liblzma5 without breaking.
+  * debian/symbols: XZ_5.0 symbols come from liblzma5.  Build-Depends:
+    dpkg-dev (>= 1.15.6); thanks to Jakub Wilk for a reminder.
+  * debian/symbols: The lzma_code@Base symbol is not guaranteed to
+    continue to exist in the future, so tell dpkg-shlibdeps to produce
+    an error if some package manages to use it.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 20 Oct 2011 21:31:31 -0500
+
+xz-utils (5.1.1alpha+20110809-2) unstable; urgency=low
+
+  * debian/rules build-arch: Do not trigger an infinite "make"
+    recursion loop when DEB_BUILD_OPTIONS=nocheck.  Closes: #638071.
+    Thanks to Thorsten Glaser.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 16 Aug 2011 18:11:47 -0500
+
+xz-utils (5.1.1alpha+20110809-1) unstable; urgency=low
+
+  * New snapshot, taken from upstream commit 5c5b225.
+    - liblzma:
+      - lzma_stream_encoder() leaked memory.
+      - The LZMA2 decoder rejected streams that decompress to a
+        zero-length string.
+      - lzma_easy_buffer_encode() and lzma_stream_buffer_encode()
+        used to write such streams when asked to compress a 0-length
+        buffer.
+      - lzma_index_init() would segfault on allocation failure.
+      - liblzma: Various functions check their arguments (especially
+        choice of integrity check) better, making it harder to create
+        a corrupt .xz file instead of receiving an error.
+    - xz and scripts:
+      - "xz -v -v --list" would read and try to free() uninitialized
+        memory, continuing past the end of an on-stack array, when
+        asked to describe certain corrupted XZ files.
+      - xz -S.suf now refuses to compress a file whose name already
+        ends with the suffix “.suf”.
+      - xz --force can be used to compress setuid, setgid, sticky,
+        and multiply linked files.
+      - xz uses posix_fadvise() to speed up sequential input.
+      - xz --block-size forces a full flush periodically during
+        compression, for better random-access reading support and to
+        make simple parallelism possible in the future.
+      - unxz: The new --single-stream option discards trailing
+        garbage after the end of the first stream.
+      - xzdiff can read gzip- and bzip2-compressed files.
+      - xzdiff and xzgrep can read lzop-compressed files.
+      - xzegrep and xzfgrep perform extended regex and fixed-string
+        matches, respectively.  (The previous behavior was to always
+        use basic regexes.)
+      - xzgrep -l (= --files-with-match) works.
+      - The Czech “xz --help” output uses a more correct term for
+        files with holes.  Thanks to Petr Hubený.  Closes: #605762.
+      - xz: New Polish and French translations.
+    - The Doxygen-generated documentation uses short, relative paths
+      to name source files.  Closes: #572273.
+  * Update copyright file.
+  * Remove example programs from debian/examples.  They are included
+    in the upstream source package now.
+  * Move liblzma to an architecture-specific directory, so the
+    native library and foreign-architecture versions can be installed
+    simultaneously.
+    - liblzma2: Pre-Depends: multiarch-support.
+    - Use debhelper compatibility level 9.  This requires passing
+      the dh sequence name before other options when invoking dh in
+      debian/rules.  Build-Depends: debhelper (>= 8.1.3).
+    - liblzma2, liblzma-dev: Install files under /usr/lib/<triplet>
+      instead of /usr/lib.
+    - Thanks to Steve Langasek for explaining the process on the
+      Debian wiki.
+  * Standards-Version: 3.9.2 (checked).
+  * debian/control: liblzma2 is Multi-Arch: same, while xz-utils
+    and xzdec are Multi-Arch: foreign.  Closes: #637883.  Thanks to
+    Riku Voipio.
+  * debian/symbols: Bump the minimal version for LZMA2 encoder
+    functions that reject more bad arguments and skip empty blocks.
+  * debian/rules: ./configure --disable-symbol-versions for now.
+  * debian/rules: Provide build-arch and build-indep targets that
+    only build the code and the API documentation, respectively.
+    Thanks to Roger Leigh for suggesting it through lintian.
+    Build-Depends: debhelper (>= 8.9.0).
+  * debian/rules: Treat DEB_BUILD_OPTIONS=nocheck as a request not to
+    run build-time tests.  Thanks to Thorsten Glaser for a reminder.
+    Closes: #627209.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 16 Aug 2011 00:40:31 -0500
+
+xz-utils (5.0.0-2) unstable; urgency=low
+
+  * Upload to unstable.
+  * liblzma2, liblzma-dev, xz-lzma: Install release notes.
+  * debian/symbols: Bump the minimal versions for lzma_index_encoder,
+    lzma_index_decoder to the version in which they gained LZMA_FINISH
+    support.
+  * xz-utils/README.Debian: Document differences from upstream ABI.
+  * debian/changelog.upstream.sh: Avoid empty changelog entries when
+    multiple Debian revisions match an upstream tag.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 11 Nov 2010 13:45:20 -0600
+
+xz-utils (5.0.0-1) experimental; urgency=low
+
+  * Upload to experimental.
+  * New stable upstream release.  Closes: #592298.
+    - xz avoids SA_RESTART.  This should not have a visible effect
+      because xz already blocked signals during operations that
+      would be difficult to resume after interruption.
+    - liblzma: The coders returned by lzma_index_encoder() and
+      lzma_index_decoder() support an LZMA_FINISH action, with
+      identical semantics to LZMA_RUN.
+    - The printable (PDF and PS) versions of the manual are easier
+      to read.
+    - Many documentation and build system cleanups.
+  * liblzma: Back out upstream ABI tweaks.
+  * liblzma/README.Debian: Document ABI differences from upstream.
+  * Copy sample code not included in the release tarball from
+    the development upstream repository to debian/examples.
+  * debian/rules clean: Remove some files not included in the
+    release tarball.
+  * copyright: List new INSTALL.generic license.
+  * debian/rules clean: Take new build-aux scripts into account.
+  * debian/rules:  Adapt for upstream build system changes.
+    Closes: #540443.
+  * xz-utils, xzdec: Install release notes.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 25 Oct 2010 05:45:18 -0500
+
+xz-utils (4.999.9beta+20100927-1) experimental; urgency=low
+
+  * New snapshot, taken from upstream commit cec0ddc.
+    - liblzma: The meaning of --extreme has been tweaked to address
+      cases in which it made the compression ratio worse.  Some files
+      might benefit slightly less from --extreme.
+    - xz: Table columns are not misaligned any more in messages with
+      multi-byte characters (e.g., file sizes with LANG=fi_FI.UTF-8).
+    - xz: New German and Italian translations.
+    - Various documentation, message, and translation updates.
+  * Update copyright file.
+  * debian/rules get-orig-source: Update upstream Git URL.
+  * xz-utils/README.Debian: Advertise XZ_DEFAULTS.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 27 Sep 2010 16:22:22 -0500
+
+xz-utils (4.999.9beta+20100903-1) experimental; urgency=low
+
+  * New snapshot, taken from upstream commit 373ee26.
+    - liblzma: The settings for compression levels 0 through 5
+      (used by xz -0/.../-5) have been tweaked.  This affects the
+      speed, compression ratio, and memory usage at compression and
+      decompression time.
+    - Does not search so hard for a match when a low "nice match
+      length" setting is specified without a depth.
+    - xz: The -1/.../-9 preset options override any previously
+      specified filters, rather than vice versa.  To mix presets
+      with filter options, use the --lzma2=preset=n option.
+    - xz: Warns about --memlimit-compress settings that result in
+      compression settings changing even if no explicit -1/.../-9,
+      --lzma1, or --lzma2 option was used.
+    - "xz -v -v --compress" now prints an estimate of the memory
+      required to decompress a file.
+  * debian/copyright: Update upstream Git URL.
+  * debian/changelog.upstream.sh: Permit terser changelog lines.
+  * Standards-Version: 3.9.1 (checked).
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Fri, 03 Sep 2010 17:05:20 -0500
+
+xz-utils (4.999.9beta+20100810-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit d5653ba8a.
+    - xz: The memory usage limiter is now disabled by default.
+    - xz: Support for an XZ_DEFAULTS environment variable was added.
+      It contains user-specific default settings, such as memory usage
+      limits, and can be overridden by the command line and XZ_OPT.
+    - xz: The new --memlimit-compress and --memlimit-decompress
+      options allow the memory usage limit to be set separately for
+      compression and decompression.  A new --memlimit alias has been
+      added for the existing --memory option.
+    - xz: The --info-memory option (with or without --robot) now prints
+      how much RAM is installed in addition to the compression and
+      decompression memory limits.
+  * liblzma-doc: Simplify package description.
+  * debian/control: Build-Depends: autopoint | gettext (<< 0.18-1),
+    autopoint | cvs instead of autopoint, to help with backporting.
+    Thanks to Thorsten Glaser for a report.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 10 Aug 2010 20:27:14 -0500
+
+xz-utils (4.999.9beta+20100713-1) unstable; urgency=low
+
+  [ Thorsten Glaser ]
+  * Explicitly depend on autoconf (>= 2.64~) due to AC_PREREQ,
+    discovered during backporting.
+
+  [ Jonathan Nieder ]
+  * Upload to unstable.
+  * New upstream snapshot, taken from upstream commit a540198f.
+    - xz: The new --no-adjust option can be used to error out if there
+      is insufficient memory for the chosen compression level.
+  * liblzma-dev: Install examples to /usr/share/doc/liblzma-dev.
+  * Update copyright file.
+
+  * Drop unpack-time conflicts in favor of configuration-time
+    conflicts (Replaces + Breaks):
+     - xz-utils: against old xz-lzma (for lzmainfo).
+     - xzdec: against old xz-lzma (for lzmadec).
+     - xz-lzma: against lzip 1.7 (for lzdiff and lzgrep).
+  * Standards-Version: 3.9.0.
+
+  * debian/autogen.sh: Don’t regenerate upstream changelog when it is
+    not out of date.
+  * debian/rules get-orig-source: Use gzip --no-name --rsyncable.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 15 Jul 2010 01:53:37 -0500
+
+xz-utils (4.999.9beta+20100602-1) experimental; urgency=low
+
+  * New upstream snapshot, taken from upstream commit d8b41eed.
+    - The output from "xz -v --list" and "xz -v -v --list" has been
+      improved.
+      "xz -v -v --list" can be used to obtain an estimate for the memory
+      required to decompress a file.
+    - The xz manual page has some new material, including examples.
+      Closes: #578465.
+    - liblzma: The lzma_block_compressed_size() function no longer
+      returns LZMA_DATA_ERROR for valid Blocks.
+  * debian/symbols: Bump minimal version for lzma_block_compressed_size.
+  * Correct the 4.999.9beta+20100307-1 changelog entry to describe
+    which public functions were affected.
+
+  * Add new xz-lzma package to allow xz to provide lzma and related
+    commands.  Conflicts: and Provides: lzma.
+  * xz-utils: Suggests: xz-lzma.
+  * xz-utils/README.Debian: Take xz-lzma into account.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Fri, 04 Jun 2010 15:53:58 -0500
+
+xz-utils (4.999.9beta+20100527-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit a334348dc.
+    - Show both elapsed time and estimated remaining time in xz -v.
+      Thanks to Trent W. Buck for the idea.  Closes: #574583.
+    - liblzma: Remove the Subblock filter from the public API
+      (the ABI is unchanged).
+  * liblzma-dev.NEWS: Update.
+  * xz-utils: Update Czech translation.
+  * Change priority of xz-utils and liblzma2 to required and
+    xzdec to extra.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 27 May 2010 16:27:04 -0500
+
+xz-utils (4.999.9beta+20100307-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit f4b2b526.
+    - liblzma: The stream initialization functions were not correctly
+      initializing the internal allow_buffer_error flag.  As a result,
+      ever since 4.999.9beta-1 in rare circumstances when decompressing
+      LZMA files, the xz tool would produce spurious "Unexpected end
+      of input" failures (Gentoo bug 305591).
+    - The default memory usage limit on low-memory systems has been
+      increased.
+  * debian/copyright: Clarify.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 09 Mar 2010 10:22:24 -0600
+
+xz-utils (4.999.9beta+20100212-4) unstable; urgency=low
+
+  * debian/control: Build-Depends: autopoint instead of cvs.
+    Closes: #572481.
+  * debian/copyright: Clean up and include more details.
+  * debian/rules get-orig-source: Unindent comment lines.
+  * xz-utils: include Czech translation.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 04 Mar 2010 10:17:15 -0600
+
+xz-utils (4.999.9beta+20100212-3) unstable; urgency=low
+
+  * liblzma-dev: Remove unneeded .la file.
+  * xz-utils: Dynamically link the xz binary.
+  * Do not waste time installing and running doxygen on buildds that
+    are not going to build arch-all packages.
+    Build-Depends-Indep: doxygen.  Closes: #540444.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 23 Feb 2010 18:23:29 -0600
+
+xz-utils (4.999.9beta+20100212-2) unstable; urgency=low
+
+  * Split off xzdec and lzmadec commands into new xzdec package.
+    Thanks to Guillem Jover for the idea.  Closes: #571060.
+  * xz-utils.NEWS: advertise this change.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 23 Feb 2010 08:09:39 -0600
+
+xz-utils (4.999.9beta+20100212-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit eb7d51a3f.
+     - xz --force compresses symlinks without complaint now.
+     - liblzma: the optimized CRC32 implementation for x86 no longer
+       misdetects the Hurd as Mac OS X.
+  * debian/rules: ./configure --enable-assembler on the Hurd.
+  * xz-utils: Simplify NEWS.Debian.gz.
+  * liblzma-dev: Add a NEWS file to keep track of future API changes.
+
+  * Drop unnecessary build-time conflict with autoconf2.13.
+  * Add a blank line to the end of all NEWS files.
+    (Works around: #247356)
+  * debian/README.source: Leave out “-I -i” options.  Ignoring version
+    control cruft is the default behavior with dpkg source format 3.0.
+  * debian/changelog.upstream.sh: Pass ‘git log’ an explicit --format
+    option to avoid problems if format.pretty is set in gitconfig.
+
+  * Correct the 4.999.9beta+20100117-1 changelog entry.  It said:
+     + The user-specified memory usage limit was not being honored in
+       some cases.
+    No such bug was observed, so describe the change instead.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 16 Feb 2010 00:30:24 -0600
+
+xz-utils (4.999.9beta+20100131-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit f9dd797a.
+     + xz: the --list option to display information about a compressed
+       file (e.g., its uncompressed size) is now functional.  The output
+       of the xz --robot --list command still may change in future
+       versions.
+  * xz-utils: NEWS.Debian.gz: Clarify how interleaved output can
+    go wrong.
+  * debian/rules: Simplify:
+     - remove support for comma as a delimiter for $(DEB_BUILD_OPTIONS).
+     - rely on dh --parallel instead of tricky logic of our own to
+       handle parallel builds.  Build-Depends: debhelper (>= 7.4.10).
+  * Correct some typos in the 4.999.9beta+20100117-1 changelog entry.
+  * Standards-Version: 3.8.4 (checked).
+  * Update copyright file.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Sun, 31 Jan 2010 17:32:02 -0600
+
+xz-utils (4.999.9beta+20100117-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit 07a11dad.
+     + Fix some logic errors in the internal memory usage configuration
+       API.
+     + xz will no longer compress or decompress special files in place,
+       even with --force.
+       --force still enables compression of the target of a symlink.
+     + xz will no longer read compressed data from a terminal or write
+       it to a terminal, even with --force.
+     + liblzma: many Index handling API improvements:
+        - An lzma_index includes more useful metadata that is not
+          actually from the Index.  This is particularly helpful for
+          describing multiple-stream files.
+           * lzma_index_cat() does not take a stream padding argument
+             any more; use lzma_index_stream_padding() instead.
+           * lzma_index_memusage() takes two arguments now.
+             The new lzma_index_memused() convenience function
+             calculates the total memory usage of an lzma_index.
+           * lzma_index_count() has been replaced with
+             lzma_index_block_count() and lzma_index_stream_count().
+           * The new lzma_index_stream_flags() allows storing Stream
+             Flags (e.g., checksum types) with the Index information.
+           * The new lzma_index_checks() function indicates the list of
+             checksum types in use.
+           * lzma_index_equal() has been removed.  Its semantics were
+             unclear, and something approximately equivalent can be
+             implemented by iterating over Blocks in application code.
+        - An lzma_index no longer represents a position within an Index.
+          The new lzma_index_iter type can be used for that purpose.
+           * Certain functions generate an iterator on demand now rather
+             than clobbering their lzma_index arguments.  Using a single
+             lzma_index for multiple concurrent invocations is safe now:
+              lzma_index_buffer_encode(), lzma_index_encoder()
+           * Functions that used to specify a Record by modifying an
+             lzma_index argument have been renamed and taught to write
+             an lzma_index_iter instead:
+              lzma_index_locate() -> lzma_index_iter_locate()
+              lzma_index_rewind() -> lzma_index_iter_rewind()
+              lzma_index_read() -> lzma_index_iter_next()
+           * The lzma_index_record type used by lzma_index_read() for
+             complex seeks is no more.  lzma_index_iter_next() instead
+             takes a parameter of the new lzma_index_iter_mode type.
+           * New function: lzma_index_iter_init()
+           * lzma_index_init() cannot reuse memory from an existing
+             lzma_index any more.  The signature changed accordingly.
+     + xz: support for decompressing to a sparse file
+        - Try to write a sparse file if the decompressed stream contains
+          long runs of null bytes.
+        - The new --no-sparse option disables this behavior.
+     + xz: add Czech translation.
+     + liblzma-dev: lzma_filters_copy() and lzma_filters_update() are
+       declared not to throw exceptions.
+  * liblzma: bump soname.
+  * xz-utils: Add NEWS.Debian describing the sparse file support.
+  * debian/rules: Stop explicitly disabling assembler optimizations on
+    the Hurd.  The configure script will check if a platform is
+    supported.
+
+  * debian/rules: Update clean target to handle comments and wildcards
+    in .gitignore.
+  * Build-Depends: perl.
+
+  * debian/rules: Speed up build by using autoreconf --symlink.
+  * Remove debian/dirs lists.
+  * Update copyright file and fix a small typo.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 18 Jan 2010 04:19:50 -0600
+
+xz-utils (4.999.9beta+20091116-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit d315ca4.
+     + liblzma: add some new functions:
+        - lzma_filters_copy() to copy a filter chain;
+        - lzma_filters_update() to change compression options mid-stream;
+        - lzma_physmem() to retrieve the total quantity of installed RAM.
+     + Fix a design error in liblzma API.
+        - Stop requiring caller to maintain filter chain during
+          compression.  The library used to read it for new compression
+          options.  Callers that need to change options mid-stream should
+          use lzma_filters_update() instead.
+        - Remove 'persistent' member from struct lzma_options_lzma.
+     + xz: add --robot --info-memory options, for front-ends to use to
+       read the memory usage limit.
+  * liblzma: bump soname; add README.Debian explaining why soname
+    differs from upstream.
+  * Use source format 3.0 (quilt).
+  * Update and clarify copyright file.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 16 Nov 2009 16:33:35 -0600
+
+xz-utils (4.999.9beta+20091016-1) unstable; urgency=low
+
+  * New upstream snapshot, taken from upstream commit 78e92c1.
+  * Drop xzmore.1 patch, applied upstream.
+  * debian/rules: Disable assembler optimizations on hurd. (Closes: #553331)
+  * debian/control: Clarify binary package descriptions.  Thanks to
+    Justin B Rye and Ben Finney for the text.
+  * debian/rules get-orig-source: Use commit date (instead of last patch
+    date) to name snapshots.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Sun, 01 Nov 2009 00:22:04 -0500
+
+xz-utils (4.999.9beta+20091004-1) unstable; urgency=low
+
+  * Upload to unstable. (Closes: #543562)
+  * New upstream snapshot, taken from upstream commit ebfb2c5.
+  * Patch xzmore.1 to use dashes instead of minus signs.
+  * debian/control: Conflicts: xz-lzma. (Closes: #550249)
+  * liblzma-doc: Register Doxygen-generated docs with doc-base.
+  * debian/changelog.upstream.sh: Declare local variables.
+  * debian/changelog.upstream.sh: Cope with multiple Debian revisions per
+    upstream version.
+  * debian/rules: Use ":=" where appropriate.
+  * Add watch file.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Fri, 16 Oct 2009 13:58:54 -0500
+
+xz-utils (4.999.9beta+20091002-1) experimental; urgency=low
+
+  * New upstream snapshot, taken from upstream commit 29fd321.
+  * debian/control: Build-Depends: cvs (for autopoint).
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Sun, 04 Oct 2009 00:21:08 -0500
+
+xz-utils (4.999.9beta-1) experimental; urgency=low
+
+  [ Jonathan Nieder ]
+  * New upstream release.
+     - Fix a data corruption in the compression code. (Closes: #544872)
+     - xzdiff: Fix comparing two compressed files. (Closes: #540543)
+     - Most of XZ Utils is now in the public domain. (Closes: #541391)
+     - Add doc/history.txt to the distributed tarball. (Closes: #541540)
+  * Update and clarify copyright file.
+  * Drop lzma compatibility commands for now. (Closes: #542060, #540232)
+    Add instructions to README.Debian for installing them locally.
+  * debian/control: Add information about the packaging Git repository.
+  * debian/control: Clarify package descriptions.
+  * debian/README.source: Clarify build instructions.
+  * Add debian/.gitignore listing build products.
+  * Build xz-utils with static liblzma.
+     - debian/rules: Use configure --enable-dynamic=mixed.
+  * Optimize xzdec for size, following suggestion in PACKAGERS.
+     - debian/rules: Build xzdec separately so it can be optimized for size.
+     - debian/control: Build-Depends: debhelper (>= 7.3.0).
+  * Generate upstream changelog instead of shipping placeholder.
+     - debian/README.source: Add instructions for regenerating upstream
+       changelog file.
+     - debian/changelog.upstream.sh: New script.
+     - debian/rules: Install generated changelog file.
+  * liblzma-doc: Drop documentation already shipped in
+    /usr/share/doc/xz-utils.
+  * liblzma-dev, liblzma-doc: Add short README.Debian with pointers to
+    development documentation.
+  * Drop debian/xzdec.1, since upstream ships a better man page.
+  * Run autotools at build time.
+     + debian/control:
+        - Build-Depends: autoconf, automake, libtool, gettext
+        - Build-Conflicts: autoconf2.13, automake1.4
+     + debian/rules:
+        - configure: autoreconf -fi
+        - clean: remove autotools-generated files to avoid .diff.gz
+          changes when built twice in a row.
+  * debian/rules: Add get-orig-source target.
+  * Prepare for upload to experimental.
+
+  [ Cristian Greco ]
+  * debian/copyright: refer to versioned GPL-3 file.
+  * debian/control: remove duplicate field ‘section’ in package xz-utils.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Wed, 30 Sep 2009 01:44:04 -0500
+
+xz-utils (4.999.8beta-1) experimental; urgency=low
+
+  [ Jonathan Nieder ]
+  * Initial package for Debian. (Closes: #518803)
+     + debian/rules:
+        - add minimal rules file (using Debhelper 7), a control file
+          describing the binary packages, and an appropriate compat file.
+        - use configure --enable-dynamic; use up-to-date config.guess and
+          config.sub files.
+        - do not run automake and so on on autobuilders. Instead, we can
+          run them ourselves when we build the source package.
+     + debian/README.source:
+        - add informations about the packaging GIT repository.
+  * Add liblzma-doc package with Doxygen-generated API docs.
+
+  [ Mohammed Adnène Trojette ]
+  * Prepare for upload to experimental.
+
+ -- Mohammed Adnène Trojette <adn+deb@diwi.org>  Sat, 27 Jun 2009 17:26:56 +0200
diff --git a/debian/changelog.upstream.sh b/debian/changelog.upstream.sh
new file mode 100644
index 0000000..afaec0f
--- /dev/null
+++ b/debian/changelog.upstream.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+# Generate debian/changelog.upstream from debian/changelog and
+# the git revision log.  Inspired by Gerrit Pape’s
+# debian/changelog.upstream.sh, from the git-core Debian package.
+
+set -e
+
+# If argument matches /^Version: /, output remaining text.
+# Result is true if and only if argument matches.
+version_line() {
+	local line result
+	line=$1
+	result=${line#Version: }
+
+	if test "$result" = "$line"
+	then
+		return 1
+	else
+		printf "%s\n" "$result"
+		return 0
+	fi
+}
+
+# If argument matches /^\* New.*snapshot.*commit /,
+# output remaining text.
+# Result is true if and only if argument matches.
+commit_id_line() {
+	local line result
+	line=$1
+	result=${line#\* New*snapshot*commit }
+
+	if test "$result" = "$line"
+	then
+		return 1
+	else
+		printf "%s\n" "$result"
+		return 0
+	fi
+}
+
+# Read standard input, scanning for a changelog entry of the
+# form “New snapshot, taken from upstream commit <blah>.”
+# Output is <blah>.
+# Fails and writes a message to standard error if no such entry is
+# found before the next Version: line with a different upstream
+# version (or EOF).
+# $1 is the upstream version sought.
+read_commit_id() {
+	local upstream_version line version cid
+	upstream_version=$1
+
+	while read line
+	do
+		if
+			version=$(version_line "$line") &&
+			test "${version%-*}" != "$upstream_version"
+		then
+			break
+		fi
+
+		if cid=$(commit_id_line "$line")
+		then
+			printf "%s\n" "${cid%.}"
+			return 0
+		fi
+	done
+
+	echo >&2 "No commit id for $upstream_version"
+	return 1
+}
+
+last=none
+last_cid=none
+# Add a list of all revisions up to $last to debian/changelog.upstream
+# and set last=$2.
+# $1 is a user-readable name for the commit $2
+add_version() {
+	local new new_cid limiter
+	new=$1
+	new_cid=$2
+
+	if test "$last" = none
+	then
+		: > debian/changelog.upstream
+		last=$new
+		last_cid=$new_cid
+		return 0
+	fi
+
+	exec >> debian/changelog.upstream
+	if test "$new" = none
+	then
+		echo "Version $last:"
+		echo "Version $last:" | tr "[:print:]" -
+		limiter=
+	elif test "$new" = "$last"
+	then
+		return 0
+	else
+		echo "Version $last; changes since $new:"
+		echo "Version $last; changes since $new:" | tr "[:print:]" -
+		limiter="$new_cid.."
+	fi
+	echo
+	git rev-list --no-merges "$limiter$last_cid" |
+	git diff-tree --date=iso --stat --format=medium --stdin
+	test "$new" = none || echo
+
+	last=$new
+	last_cid=$new_cid
+}
+
+dpkg-parsechangelog --format rfc822 --all | {
+while read line
+do
+	if version=$(version_line "$line")
+	then
+		# strip Debian revision
+		upstream_version=${version%-*}
+
+		if git rev-parse --verify -q "v$upstream_version" > /dev/null
+		then
+			# upstream release
+			add_version "$upstream_version" "v$upstream_version"
+		else
+			# snapshot
+			cid=$(read_commit_id "$upstream_version") || exit 1
+			add_version "$upstream_version" "$cid"
+		fi
+	fi
+done
+add_version none none
+}
diff --git a/debian/clean.sh b/debian/clean.sh
new file mode 100644
index 0000000..1a2f8b3
--- /dev/null
+++ b/debian/clean.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+# Remove files generated by autoreconf -fi.
+# For use by the clean target in debian/rules.
+
+set -e
+
+remove_files='#!/usr/bin/perl
+# Remove specified files from the current directory.
+# Each line must be a filename, a comment (starting with #),
+# a simple glob (of the form *.extension), or blank.
+# Filenames are restricted to a small character set.
+
+use strict;
+use warnings;
+
+my $fnchar = qr/[-_~.@[:alnum:]]/;
+
+my $empty = qr/^$/;
+my $comment = qr/^#/;
+my $simple = qr/^$fnchar+$/;
+my $glob = qr/^\*$fnchar+$/;
+while (my $line = <>) {
+	chomp $line;
+	next if $line =~ $empty;
+	next if $line =~ $comment;
+
+	if ($line =~ $simple) {
+		unlink $line;
+		next;
+	}
+	if ($line =~ $glob) {
+		unlink glob($line);
+		next;
+	}
+
+	die "cannot parse $line";
+}
+'
+
+dh_testdir
+rm -f debug/translation.bash tests/test_block.c ChangeLog
+rm -f ABOUT-NLS aclocal.m4 config.h.in configure
+(cd po && perl -e "$remove_files") < debian/generated-po.list
+(cd m4 && perl -e "$remove_files") < debian/generated-m4.list
+(cd build-aux && perl -e "$remove_files") < debian/generated-build-aux.list
+find . -name Makefile.in -delete
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..986f0c1
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,114 @@
+Source: xz-utils
+Section: utils
+Priority: required
+Maintainer: Jonathan Nieder <jrnieder@gmail.com>
+Uploaders: Mohammed Adnène Trojette <adn+deb@diwi.org>
+Build-Depends: debhelper (>= 8.9.0), perl, dpkg-dev (>= 1.16.2),
+ autoconf (>= 2.64~), automake, libtool (>= 2.2),
+ gettext, autopoint | gettext (<< 0.18-1), autopoint | cvs
+Build-Depends-Indep: doxygen
+Build-Conflicts: automake1.4
+Standards-Version: 3.9.3
+Vcs-Browser: http://git.debian.org/?p=collab-maint/xz.git
+Vcs-Git: git://git.debian.org/collab-maint/xz.git
+Homepage: http://tukaani.org/xz/
+
+Package: liblzma5
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Pre-Depends: ${misc:Pre-Depends}
+Multi-Arch: same
+Description: XZ-format compression library
+ XZ is the successor to the Lempel-Ziv/Markov-chain Algorithm
+ compression format, which provides memory-hungry but powerful
+ compression (often better than bzip2) and fast, easy decompression.
+ .
+ The native format of liblzma is XZ; it also supports raw (headerless)
+ streams and the older LZMA format used by lzma. (For 7-Zip's related
+ format, use the p7zip package instead.)
+
+Package: xz-utils
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Multi-Arch: foreign
+Conflicts: lzma (<< 9.22-1), xz-lzma
+Breaks: lzip (<< 1.8~rc2)
+Replaces: lzip (<< 1.8~rc2), xz-lzma
+Provides: lzma
+Description: XZ-format compression utilities
+ XZ is the successor to the Lempel-Ziv/Markov-chain Algorithm
+ compression format, which provides memory-hungry but powerful
+ compression (often better than bzip2) and fast, easy decompression.
+ .
+ This package provides the command line tools for working with XZ
+ compression, including xz, unxz, xzcat, xzgrep, and so on. They can
+ also handle the older LZMA format, and if invoked via appropriate
+ symlinks will emulate the behavior of the commands in the lzma
+ package.
+ .
+ The XZ format is similar to the older LZMA format but includes some
+ improvements for general use:
+ .
+  * 'file' magic for detecting XZ files;
+  * crc64 data integrity check;
+  * limited random-access reading support;
+  * improved support for multithreading (not used in xz-utils);
+  * support for flushing the encoder.
+
+Package: xzdec
+Priority: extra
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Multi-Arch: foreign
+Suggests: xz-utils
+Breaks: xz-lzma (<< 4.999.9beta+20091004-1)
+Replaces: xz-lzma (<< 4.999.9beta+20091004-1)
+Description: XZ-format compression utilities - tiny decompressors
+ XZ is the successor to the Lempel-Ziv/Markov-chain Algorithm
+ compression format, which provides memory-hungry but powerful
+ compression (often better than bzip2) and fast, easy decompression.
+ .
+ This package provides the xzdec and lzmadec utilities, which write
+ the decompressed version of a compressed file to standard output. The
+ binaries are very small, so they can be easily stored on small media
+ with some compressed files, and they are linked statically against
+ liblzma so they can be used on machines without a compatible version
+ of liblzma installed. However, they have:
+ .
+   * no compression support;
+   * no support for writing to a file other than standard output;
+   * no translated messages;
+   * been optimized for size rather than speed.
+ .
+ For a full-featured xzcat command without these limitations, use
+ the xz-utils package instead.
+
+Package: liblzma-dev
+Section: libdevel
+Priority: optional
+Multi-Arch: same
+Architecture: any
+Depends: liblzma5 (= ${binary:Version}), ${misc:Depends}
+Suggests: liblzma-doc
+Description: XZ-format compression library - development files
+ XZ is the successor to the Lempel-Ziv/Markov-chain Algorithm
+ compression format, which provides memory-hungry but powerful
+ compression (often better than bzip2) and fast, easy decompression.
+ .
+ The native format of liblzma is XZ; it also supports raw (headerless)
+ streams and the older LZMA format used by lzma. (For 7-Zip's related
+ format, use the p7zip package instead.) This package provides the
+ development library needed to build programs using liblzma.
+
+Package: liblzma-doc
+Section: doc
+Priority: optional
+Architecture: all
+Depends: ${misc:Depends}
+Suggests: liblzma-dev
+Description: XZ-format compression library - API documentation
+ This package contains a reference manual for the liblzma data
+ compression library, in Doxygen-generated HTML files. The purpose
+ of each struct, macro, and function in the public interface is
+ explained.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..3327901
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,383 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: XZ Utils
+Upstream-Contact:
+ Lasse Collin <lasse.collin@tukaani.org>
+ http://tukaani.org/xz/lists.html
+Source:
+ http://tukaani.org/xz
+ http://git.tukaani.org/xz.git
+Comment:
+ XZ Utils is developed and maintained upstream by Lasse Collin.  Major
+ portions are based on code by other authors; see AUTHORS for details.
+ Most of the source has been put into the public domain, but some files
+ have not (details below).
+ .
+ This file describes the source package.  The binary packages contain
+ some files derived from other works: for example, images in the API
+ documentation come from Doxygen.
+License:
+ Different licenses apply to different files in this package. Here
+ is a rough summary of which licenses apply to which parts of this
+ package (but check the individual files to be sure!):
+ .
+   - liblzma is in the public domain.
+ .
+   - xz, xzdec, and lzmadec command line tools are in the public
+     domain unless GNU getopt_long had to be compiled and linked
+     in from the lib directory. The getopt_long code is under
+     GNU LGPLv2.1+.
+ .
+   - The scripts to grep, diff, and view compressed files have been
+     adapted from gzip. These scripts and their documentation are
+     under GNU GPLv2+.
+ .
+   - All the documentation in the doc directory and most of the
+     XZ Utils specific documentation files in other directories
+     are in the public domain.
+ .
+   - Translated messages are in the public domain.
+ .
+   - The build system contains public domain files, and files that
+     are under GNU GPLv2+ or GNU GPLv3+. None of these files end up
+     in the binaries being built.
+ .
+   - Test files and test code in the tests directory, and debugging
+     utilities in the debug directory are in the public domain.
+ .
+   - The extra directory may contain public domain files, and files
+     that are under various free software licenses.
+ .
+ You can do whatever you want with the files that have been put into
+ the public domain. If you find public domain legally problematic,
+ take the previous sentence as a license grant. If you still find
+ the lack of copyright legally problematic, you have too many
+ lawyers.
+ .
+ As usual, this software is provided "as is", without any warranty.
+ .
+ If you copy significant amounts of public domain code from XZ Utils
+ into your project, acknowledging this somewhere in your software is
+ polite (especially if it is proprietary, non-free software), but
+ naturally it is not legally required. Here is an example of a good
+ notice to put into "about box" or into documentation:
+ .
+     This software includes code from XZ Utils <http://tukaani.org/xz/>.
+ .
+ The following license texts are included in the following files:
+   - COPYING.LGPLv2.1: GNU Lesser General Public License version 2.1
+   - COPYING.GPLv2: GNU General Public License version 2
+   - COPYING.GPLv3: GNU General Public License version 3
+ .
+ Note that the toolchain (compiler, linker etc.) may add some code
+ pieces that are copyrighted. Thus, it is possible that e.g. liblzma
+ binary wouldn't actually be in the public domain in its entirety
+ even though it contains no copyrighted code from the XZ Utils source
+ package.
+ .
+ If you have questions, don't hesitate to ask the author(s) for more
+ information.
+
+Files: *
+Copyright: 2006-2012, Lasse Collin
+           1999-2008, Igor Pavlov
+           2006, Ville Koskinen
+           1998, Steve Reid
+           2000, Wei Dai
+           2003, Kevin Springle
+           2009, Jonathan Nieder
+           2010, Anders F Björklund
+License: PD
+ This file has been put in the public domain.
+ You can do whatever you want with this file.
+Comment:
+  From: Lasse Collin <lasse.collin@tukaani.org>
+  To: Jonathan Nieder <jrnieder@gmail.com>
+  Subject: Re: XZ utils for Debian
+  Date: Sun, 19 Jul 2009 13:28:23 +0300
+  Message-Id: <200907191328.23816.lasse.collin@tukaani.org>
+ .
+ [...]
+ .
+  > AUTHORS, ChangeLog, COPYING, README, THANKS, TODO,
+  > dos/README, windows/README
+ .
+  COPYING says that most docs are in the public domain. Maybe that's not
+  clear enough, but on the other hand it looks a bit stupid to put
+  copyright information in tiny and relatively small docs like README.
+ .
+  I don't dare to say that _all_ XZ Utils specific docs are in the public
+  domain unless otherwise mentioned in the file. I'm including PDF files
+  generated by groff + ps2pdf, and some day I might include Doxygen-
+  generated HTML docs too. Those don't include any copyright notices, but
+  it seems likely that groff + ps2pdf or at least Doxygen put some
+  copyrighted content into the generated files.
+
+Files: INSTALL NEWS PACKAGERS
+ windows/README-Windows.txt
+ windows/INSTALL-Windows.txt
+Copyright: 2009-2010, Lasse Collin
+License: probably-PD
+ See the note on AUTHORS, README, and so on above.
+
+Files: src/scripts/* lib/* extra/scanlzma/scanlzma.c
+Copyright: © 1993, Jean-loup Gailly
+           © 1989-1994, 1996-1999, 2001-2007, Free Software Foundation, Inc.
+           © 2006 Timo Lindfors
+           2005, Charles Levert
+           2005, 2009, Lasse Collin
+           2009, Andrew Dudman
+Other-Authors: Paul Eggert, Ulrich Drepper
+License: GPL-2+
+
+Files: src/scripts/Makefile.am src/scripts/xzless.1
+Copyright: 2009, Andrew Dudman
+           2009, Lasse Collin
+License: PD
+ This file has been put in the public domain.
+ You can do whatever you want with this file.
+
+Files: doc/examples/xz_pipe_comp.c doc/examples/xz_pipe_decomp.c
+Copyright: 2010, Daniel Mealha Cabrita
+License: PD
+ Not copyrighted -- provided to the public domain.
+
+Files: lib/getopt.c lib/getopt1.c lib/getopt.in.h
+Copyright: © 1987-2007 Free Software Foundation, Inc.
+Other-Authors: Ulrich Drepper
+License: LGPL-2.1+
+
+Files: m4/getopt.m4 m4/posix-shell.m4
+Copyright: © 2002-2006, 2008 Free Software Foundation, Inc.
+           © 2007-2008 Free Software Foundation, Inc.
+Other-Authors: Bruno Haible, Paul Eggert
+License: permissive-fsf
+
+Files: m4/acx_pthread.m4
+Copyright: © 2008, Steven G. Johnson <stevenj@alum.mit.edu>
+License: Autoconf
+
+Files: Doxyfile.in
+Copyright: © 1997-2007 by Dimitri van Heesch
+Origin: Doxygen 1.4.7
+License: GPL-2
+
+Files: src/liblzma/check/crc32_table_?e.h
+ src/liblzma/check/crc64_table_?e.h
+ src/liblzma/lzma/fastpos_table.c
+ src/liblzma/rangecoder/price_table.c
+Copyright: none, automatically generated data
+Generated-With:
+ src/liblzma/check/crc32_tablegen.c
+ src/liblzma/check/crc64_tablegen.c
+ src/liblzma/lzma/fastpos_tablegen.c
+ src/liblzma/rangecoder/price_tablegen.c
+License: none
+ No copyright to license.
+
+Files: .gitignore m4/.gitignore po/.gitignore po/LINGUAS po/POTFILES.in
+Copyright: none; these are just short lists.
+License: none
+ No copyright to license.
+
+Files: tests/compress_prepared_bcj_*
+Copyright: 2008-2009, Lasse Collin
+Source-Code: tests/bcj_test.c
+License: PD
+ This file has been put into the public domain.
+ You can do whatever you want with this file.
+Comment:
+ changelog.gz (commit 975d8fd) explains:
+ .
+ Recreated the BCJ test files for x86 and SPARC. The old files
+ were linked with crt*.o, which are copyrighted, and thus the
+ old test files were not in the public domain as a whole. They
+ are freely distributable though, but it is better to be careful
+ and avoid including any copyrighted pieces in the test files.
+ The new files are just compiled and assembled object files,
+ and thus don't contain any copyrighted code.
+
+Files: po/cs.po po/de.po po/fr.po
+Copyright: 2010, Marek ÄŒernocký
+           2010, Andre Noll
+           2011, Adrien Nader
+License: PD
+ This file is put in the public domain.
+
+Files: po/it.po po/pl.po
+Copyright: 2009, 2010, Gruppo traduzione italiano di Ubuntu-it
+           2010, Lorenzo De Liso
+           2009, 2010, 2011, Milo Casagrande
+           2011, Jakub Bogusz
+License: PD
+ This file is in the public domain
+
+Files: INSTALL.generic
+Copyright: © 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+             2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+License: permissive-nowarranty
+
+Files: dos/config.h
+Copyright: © 1992, 1993, 1994, 1999, 2000, 2001, 2002, 2005
+             Free Software Foundation, Inc.
+           2007-2010, Lasse Collin
+Other-Authors: Roland McGrath, Akim Demaille, Paul Eggert,
+               David Mackenzie, Bruno Haible, and many others.
+Origin: configure.ac from XZ Utils,
+        visibility.m4 serial 1 (gettext-0.15),
+        Autoconf 2.52g
+License: config-h
+ configure.ac:
+ .
+  # Author: Lasse Collin
+  #
+  # This file has been put into the public domain.
+  # You can do whatever you want with this file.
+ .
+ visibility.m4:
+ .
+  dnl Copyright (C) 2005 Free Software Foundation, Inc.
+  dnl This file is free software; the Free Software Foundation
+  dnl gives unlimited permission to copy and/or distribute it,
+  dnl with or without modifications, as long as this notice is preserved.
+ .
+ dnl From Bruno Haible.
+ .
+ comments from Autoconf 2.52g:
+ .
+  # Copyright 1992, 1993, 1994, 1999, 2000, 2001, 2002
+  # Free Software Foundation, Inc.
+ .
+ [...]
+ .
+  # As a special exception, the Free Software Foundation gives unlimited
+  # permission to copy, distribute and modify the configure scripts that
+  # are the output of Autoconf.  You need not follow the terms of the GNU
+  # General Public License when using or distributing such scripts, even
+  # though portions of the text of Autoconf appear in them.  The GNU
+  # General Public License (GPL) does govern all other use of the material
+  # that constitutes the Autoconf program.
+ .
+ On Debian systems, the complete text of the GNU General Public
+ License version 2 can be found in ‘/usr/share/common-licenses/GPL-2’.
+ dos/config.h was generated with autoheader, which tells Autoconf to
+ output a script to generate a config.h file and then runs it.
+
+Files: po/Makevars
+Origin: gettext-runtime/po/Makevars (gettext-0.12)
+Copyright: © 2003 Free Software Foundation, Inc.
+Authors: Bruno Haible
+License: LGPL-2.1+
+ The gettext-runtime package is under the LGPL, see files intl/COPYING.LIB-2.0
+ and intl/COPYING.LIB-2.1.
+ .
+ On Debian systems, the complete text of intl/COPYING.LIB-2.0 from
+ gettext-runtime 0.12 can be found in ‘/usr/share/common-licenses/LGPL-2’
+ and the text of intl/COPYING.LIB-2.1 can be found in
+ ‘/usr/share/common-licenses/LGPL-2.1’.
+ .
+ po/Makevars consists mostly of helpful comments and does not contain a
+ copyright and license notice.
+
+Files: COPYING.GPLv2 COPYING.GPLv3 COPYING.LGPLv2.1
+Copyright: © 1989, 1991, 1999, 2007 Free Software Foundation, Inc.
+License: noderivs
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Files: debian/*
+Copyright: 2009-2012, Jonathan Nieder
+License: PD-debian
+ The Debian packaging files are in the public domain.
+ You may freely use, modify, distribute, and relicense them.
+
+License: LGPL-2.1+
+ This program 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, or (at your option)
+ any later version.
+ .
+ This program 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.
+ .
+ You should have received a copy of the GNU Lesser General Public License along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ .
+ On Debian systems, the complete text of the GNU Lesser General Public
+ License version 2.1 can be found in ‘/usr/share/common-licenses/LGPL-2.1’.
+
+License: GPL-2
+ Permission to use, copy, modify, and distribute this software and its
+ documentation under the terms of the GNU General Public License is
+ hereby granted. No representations are made about the suitability of
+ this software for any purpose. It is provided "as is" without express
+ or implied warranty. See the GNU General Public License for more
+ details.
+ .
+ Documents produced by doxygen are derivative works derived from the
+ input used in their production; they are not affected by this license.
+ .
+ On Debian systems, the complete text of the version of the GNU General
+ Public License distributed with Doxygen can be found in
+ ‘/usr/share/common-licenses/GPL-2’.
+
+License: GPL-2+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+ .
+ This program 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 General Public License for more details.
+ .
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ .
+ On Debian systems, the complete text of the GNU General Public License
+ version 2 can be found in ‘/usr/share/common-licenses/GPL-2’.
+
+License: Autoconf
+ This program is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at your
+ option) any later version.
+ .
+ This program 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 General
+ Public License for more details.
+ .
+ You should have received a copy of the GNU General Public License along
+ with this program. If not, see <http://www.gnu.org/licenses/>.
+ .
+ As a special exception, the respective Autoconf Macro's copyright owner
+ gives unlimited permission to copy, distribute and modify the configure
+ scripts that are the output of Autoconf when processing the Macro. You
+ need not follow the terms of the GNU General Public License when using
+ or distributing such scripts, even though portions of the text of the
+ Macro appear in them. The GNU General Public License (GPL) does govern
+ all other use of the material that constitutes the Autoconf Macro.
+ .
+ This special exception to the GPL applies to versions of the Autoconf
+ Macro released by the Autoconf Archive. When you make and distribute a
+ modified version of the Autoconf Macro, you may extend this special
+ exception to the GPL to apply to your modified version as well.
+ .
+ On Debian systems, the complete text of the GNU General Public
+ License version 3 can be found in ‘/usr/share/common-licenses/GPL-3’.
+
+License: permissive-fsf
+ This file is free software; the Free Software Foundation
+ gives unlimited permission to copy and/or distribute it,
+ with or without modifications, as long as this notice is preserved.
+
+License: permissive-nowarranty
+ Copying and distribution of this file, with or without modification,
+ are permitted in any medium without royalty provided the copyright
+ notice and this notice are preserved.  This file is offered as-is,
+ without warranty of any kind.
diff --git a/debian/liblzma-dev.NEWS b/debian/liblzma-dev.NEWS
new file mode 100644
index 0000000..da790a0
--- /dev/null
+++ b/debian/liblzma-dev.NEWS
@@ -0,0 +1,24 @@
+xz-utils (4.999.9beta+20100527-1) unstable; urgency=low
+
+  Starting with this version, the declarations needed to use the Subblock
+  filter are not present in the liblzma headers.  liblzma never supported
+  the Subblock filter, so compiled applications should not be affected by
+  this change.
+
+  The lzma_restrict macro has been removed from the liblzma headers.  This
+  only affects the declarations contained there; the semantics are still the
+  same.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 27 May 2010 15:52:28 -0500
+
+xz-utils (4.999.9beta+20100212-1) unstable; urgency=low
+  
+  Starting with this version, the lzma_block_header_decode() function
+  is advertised to require its block->version parameter to be initialized.
+  Future versions of liblzma may use an alternate version of the Block
+  options if the application declares it supports it.  Existing applications
+  will continue to work for now because the only supported Block format
+  version is zero.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Mon, 15 Feb 2010 23:59:21 -0600
+
diff --git a/debian/liblzma-dev.README.Debian b/debian/liblzma-dev.README.Debian
new file mode 100644
index 0000000..81e397d
--- /dev/null
+++ b/debian/liblzma-dev.README.Debian
@@ -0,0 +1,19 @@
+From the XZ Utils README (included in the xz-utils package):
+
+1.3. Documentation for liblzma
+
+    The liblzma API headers include short docs about each function
+    and data type as Doxygen tags. These docs should be quite OK as
+    a quick reference.
+
+    I have planned to write a bunch of very well documented example
+    programs, which (due to comments) should work as a tutorial to
+    various features of liblzma. No such example programs have been
+    written yet.
+
+    For now, if you have never used liblzma, libbzip2, or zlib, I
+    recommend learning *basics* of zlib API. Once you know that, it
+    should be easier to learn liblzma.
+
+        http://zlib.net/manual.html
+        http://zlib.net/zlib_how.html
diff --git a/debian/liblzma-dev.docs b/debian/liblzma-dev.docs
new file mode 100644
index 0000000..9c1a7dc
--- /dev/null
+++ b/debian/liblzma-dev.docs
@@ -0,0 +1,5 @@
+doc/xz-file-format.txt
+doc/lzma-file-format.txt
+doc/examples
+doc/examples_old
+TODO
diff --git a/debian/liblzma-dev.install b/debian/liblzma-dev.install
new file mode 100644
index 0000000..97de04c
--- /dev/null
+++ b/debian/liblzma-dev.install
@@ -0,0 +1,5 @@
+usr/include/*
+usr/include/lzma/*
+usr/lib/*/lib*.a
+usr/lib/*/lib*.so
+usr/lib/*/pkgconfig/*
diff --git a/debian/liblzma-dev.links b/debian/liblzma-dev.links
new file mode 100644
index 0000000..06a0344
--- /dev/null
+++ b/debian/liblzma-dev.links
@@ -0,0 +1,3 @@
+usr/share/doc/liblzma5/AUTHORS usr/share/doc/liblzma-dev/AUTHORS
+usr/share/doc/liblzma5/THANKS usr/share/doc/liblzma-dev/THANKS
+usr/share/doc/liblzma5/NEWS.gz usr/share/doc/liblzma-dev/NEWS.gz
diff --git a/debian/liblzma-doc.README.Debian b/debian/liblzma-doc.README.Debian
new file mode 100644
index 0000000..81e397d
--- /dev/null
+++ b/debian/liblzma-doc.README.Debian
@@ -0,0 +1,19 @@
+From the XZ Utils README (included in the xz-utils package):
+
+1.3. Documentation for liblzma
+
+    The liblzma API headers include short docs about each function
+    and data type as Doxygen tags. These docs should be quite OK as
+    a quick reference.
+
+    I have planned to write a bunch of very well documented example
+    programs, which (due to comments) should work as a tutorial to
+    various features of liblzma. No such example programs have been
+    written yet.
+
+    For now, if you have never used liblzma, libbzip2, or zlib, I
+    recommend learning *basics* of zlib API. Once you know that, it
+    should be easier to learn liblzma.
+
+        http://zlib.net/manual.html
+        http://zlib.net/zlib_how.html
diff --git a/debian/liblzma-doc.doc-base b/debian/liblzma-doc.doc-base
new file mode 100644
index 0000000..f7cfdc2
--- /dev/null
+++ b/debian/liblzma-doc.doc-base
@@ -0,0 +1,12 @@
+Document: liblzma-doc
+Title: liblzma API Documentation
+Author: Lasse Collin
+Abstract: liblzma is a compression library supporting the .xz and .lzma
+ formats, among others. This Doxygen-generated reference manual
+ describes the data structures and functions making up its public
+ interface.
+Section: Programming/C
+
+Format: HTML
+Index: /usr/share/doc/liblzma-doc/html/index.html
+Files: /usr/share/doc/liblzma-doc/html/*
diff --git a/debian/liblzma-doc.docs b/debian/liblzma-doc.docs
new file mode 100644
index 0000000..f8172bb
--- /dev/null
+++ b/debian/liblzma-doc.docs
@@ -0,0 +1 @@
+debian/normal-build/doc/html/
diff --git a/debian/liblzma5.docs b/debian/liblzma5.docs
new file mode 100644
index 0000000..aa4127d
--- /dev/null
+++ b/debian/liblzma5.docs
@@ -0,0 +1,3 @@
+AUTHORS
+THANKS
+NEWS
diff --git a/debian/liblzma5.install b/debian/liblzma5.install
new file mode 100644
index 0000000..1b15eaa
--- /dev/null
+++ b/debian/liblzma5.install
@@ -0,0 +1 @@
+lib/*/liblzma.so.*
diff --git a/debian/patches/abi-liblzma2-compat b/debian/patches/abi-liblzma2-compat
new file mode 100644
index 0000000..051ab5c
--- /dev/null
+++ b/debian/patches/abi-liblzma2-compat
@@ -0,0 +1,208 @@
+From: Jonathan Nieder <jrnieder@gmail.com>
+Date: Thu, 17 May 2012 18:49:00 -0500
+Subject: liblzma: skip ABI-incompatible check when liblzma.so.2 is loaded
+
+When liblzma started using ELF symbol versioning at the same time
+as a soname bump (2 → 5) and a small increase in the reserved space at
+the end of the lzma_stream structure checked by lzma_code, introducing
+an unversioned compatibility symbol to ease the transition seemed like
+a great idea.  After all:
+
+ - most applications only use one version of the library (liblzma.so.2
+   or liblzma.so.5) and would obviously work fine
+
+ - applications linking to the new version of the library
+   (liblzma.so.5) should use the default, versioned lzma_code symbol so
+   errors initializing the reserved space can be noticed
+
+ - symbol versioning should ensure application/library mixtures
+   independently making use of both versions of the library also
+   work.  Calls using the unversioned symbol names would be resolved
+   using the old symbol from liblzma.so.2 or the compatibility symbol
+   from liblzma.so.5, avoiding segfaults and spurious
+   LZMA_OPTIONS_ERROR errors.
+
+ - application/library mixtures using both versions of the library and
+   passing lzma_stream objects between the two would break, but that
+   was never supposed to be supported, anyway.
+
+Three toolchain bugs dash that plan.
+
+Current (2.22) versions of the gold linker cannot be used to build
+libraries providing versioned and unversioned symbols with the same
+name.  On the other hand, BFD ld doesn't mind.  So GNU gold refuses to
+link versions of liblzma including the compatibility symbol (PR12261):
+
+	/usr/bin/ld: error: symbol lzma_code has undefined version
+
+Annoying, but livable.  liblzma with the compatibility symbol just
+has to be built with BFD ld.
+
+More importantly, gold does not support linking to libraries providing
+versioned and unversioned symbols with the same name.  If I link some
+code to a version of liblzma with the compatibility symbol:
+
+	ld -o demo demo.o -llzma
+
+then the documented behavior, implemented by BFD ld, is to interpret
+calls to lzma_code as referring to the default version
+(lzma_code@XZ_5.0).  Current versions of GNU gold treat such calls as
+referring to whichever symbol comes first in liblzma.so.5's symbol
+table.  If the unversioned symbol comes first (and in Debian liblzma5
+5.1.1alpha+20110809-3 it does), GNU gold will mislink new applications
+to use the unversioned compatibility symbol (PR13521):
+
+	$ ld.gold -o test.gold test.o -llzma
+	$ eu-readelf -s test.gold | grep lzma_code
+	    1: 0000000000000000      0 FUNC    GLOBAL DEFAULT    UNDEF lzma_code
+	    5: 0000000000000000      0 FUNC    GLOBAL DEFAULT    UNDEF lzma_code
+
+There is no warning.
+
+Worse, ld.so from glibc unpredictably will sometimes use the versioned
+symbol to resolve references to the unversioned base version when both
+are present (PR12977).
+
+Clearly no one has been testing mixtures of versioned and
+unversioned symbols at all, and we cannot trust the symbol resolution
+process to do anything in particular for them.
+
+This patch implements an alternative method to implement the same
+compatibility goals described above.
+
+ - No more compatibility symbol.  liblzma.so.5 will define lzma_code
+   only once, with version XZ_5.0.
+
+ - When initializing an lzma_stream object, use dlopen("liblzma.so.2",
+   RTLD_NOLOAD) to detect whether the caller might have been expecting
+   the old ABI, and store that information in the private
+   stream->internal->liblzma2_compat field.
+
+ - In lzma_code, when checking reserved fields, skip fields past the
+   old end of the lzma_stream structure ifying reserved fields if and
+   only if this->internal->liblzma2_compat is false.
+
+That's it.  Hopefully this time it will work reliably.
+
+Thanks to Eduard Bloch for noticing PR13521 and to Ian Lance Taylor
+for PR12977.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ configure.ac                |    5 +++++
+ src/liblzma/common/common.c |   40 ++++++++++++++++++++++++++++++++++++++--
+ src/liblzma/common/common.h |    4 ++++
+ 3 files changed, 47 insertions(+), 2 deletions(-)
+
+diff --git a/configure.ac b/configure.ac
+index 25eb838f..88b81ba1 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -471,6 +471,11 @@ if test "x$enable_threads" = xyes; then
+ 	AC_CHECK_DECLS([CLOCK_MONOTONIC], [], [], [[#include <time.h>]])
+ 	CFLAGS=$OLD_CFLAGS
+ fi
++
++# As a Debian-specific hack, liblzma uses dlopen() to check if extra
++# paranoia is needed because unversioned symbols from liblzma.so.2 are
++# present in the same process.  See src/liblzma/common/common.c.
++AC_SEARCH_LIBS([dlopen], [dl])
+ 
+ echo
+ echo "Initializing Libtool:"
+diff --git a/src/liblzma/common/common.c b/src/liblzma/common/common.c
+index 50c984c7..e61d940d 100644
+--- a/src/liblzma/common/common.c
++++ b/src/liblzma/common/common.c
+@@ -12,6 +12,8 @@
+ 
+ #include "common.h"
+ 
++#include <dlfcn.h>
++
+ 
+ /////////////
+ // Version //
+@@ -141,6 +143,17 @@ lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
+ // External to internal API wrapper //
+ //////////////////////////////////////
+ 
++static bool
++liblzma2_loaded(void)
++{
++	void *handle = dlopen("liblzma.so.2", RTLD_LAZY | RTLD_NOLOAD);
++	if (handle) {
++		dlclose(handle);
++		return true;
++	}
++	return false;
++}
++
+ extern lzma_ret
+ lzma_strm_init(lzma_stream *strm)
+ {
+@@ -154,6 +167,7 @@ lzma_strm_init(lzma_stream *strm)
+ 			return LZMA_MEM_ERROR;
+ 
+ 		strm->internal->next = LZMA_NEXT_CODER_INIT;
++		strm->internal->liblzma2_compat = liblzma2_loaded();
+ 	}
+ 
+ 	memzero(strm->internal->supported_actions,
+@@ -168,6 +182,24 @@ lzma_strm_init(lzma_stream *strm)
+ }
+ 
+ 
++// Before v5.0.0~6 (liblzma: A few ABI tweaks to reserve space in
++// structures, 2010-10-23), the reserved fields in lzma_stream were:
++//
++//	void *reserved_ptr1;
++//	void *reserved_ptr2;
++//	uint64_t reserved_int1;
++//	uint64_t reserved_int2;
++//	lzma_reserved_enum reserved_enum1;
++//	lzma_reserved_enum reserved_enum2;
++//
++// Nowadays there are two more pointers between reserved_ptr2 and
++// reserved_int1 and two size_t fields between reserved_int2 and
++// reserved_enum1.
++//
++// When strm->internal->liblzma2_compat is set, limit the checks of
++// reserved fields to fields that were present in the old ABI to avoid
++// segfaults and spurious "Unsupported options" from callers sharing
++// the process image that expect the old ABI.
+ extern LZMA_API(lzma_ret)
+ lzma_code(lzma_stream *strm, lzma_action action)
+ {
+@@ -185,8 +217,12 @@ lzma_code(lzma_stream *strm, lzma_action action)
+ 	if (strm->reserved_ptr1 != NULL
+ 			|| strm->reserved_ptr2 != NULL
+ 			|| strm->reserved_ptr3 != NULL
+-			|| strm->reserved_ptr4 != NULL
+-			|| strm->reserved_int1 != 0
++			|| strm->reserved_ptr4 != NULL)
++		return LZMA_OPTIONS_ERROR;
++
++	if (strm->internal->liblzma2_compat)
++		; /* Enough checks. */
++	else if (strm->reserved_int1 != 0
+ 			|| strm->reserved_int2 != 0
+ 			|| strm->reserved_int3 != 0
+ 			|| strm->reserved_int4 != 0
+diff --git a/src/liblzma/common/common.h b/src/liblzma/common/common.h
+index 45aba4f0..475661d8 100644
+--- a/src/liblzma/common/common.h
++++ b/src/liblzma/common/common.h
+@@ -200,6 +200,10 @@ struct lzma_internal_s {
+ 	/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
+ 	/// made (no input consumed and no output produced by next.code).
+ 	bool allow_buf_error;
++
++	/// Indicates whether we are sharing a process image with
++	/// liblzma.so.2 and need to tread carefully.
++	bool liblzma2_compat;
+ };
+ 
+ 
+-- 
+1.7.10.2
diff --git a/debian/patches/abi-threaded-encoder b/debian/patches/abi-threaded-encoder
new file mode 100644
index 0000000..04babbf
--- /dev/null
+++ b/debian/patches/abi-threaded-encoder
@@ -0,0 +1,2163 @@
+From: Jonathan Nieder <jrnieder@gmail.com>
+Date: Sat, 11 Jun 2011 21:41:15 -0500
+Subject: Remove threading functionality for now
+
+This reverts the following commits:
+
+ - 6ef4eabc0 (Bump the version number to 5.1.1alpha and liblzma soname
+   to 5.0.99)
+ - 70e750f59 (xz: Update the man page about threading)
+ - c29e6630c (xz: Print the maximum number of worker threads in xz -vv)
+ - 335fe260a (xz: Minor internal changes to handling of --threads)
+ - 24e0406c0 (xz: Add support for threaded compression)
+ - 9a4377be0 (Put the unstable APIs behind #ifdef LZMA_UNSTABLE)
+ - de678e0c9 (liblmza: Add lzma_stream_encoder_mt() for threaded
+   compression)
+
+The multithreaded compression functions, while useful, are not set in
+stone as part of the stable ABI.  Changes will be easier to weather
+until the functions stabilize if they are left out from the
+non-experimental development branch of Debian for now.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ configure.ac                           |    1 -
+ src/liblzma/Makefile.am                |    2 +-
+ src/liblzma/api/lzma/container.h       |  167 ------
+ src/liblzma/api/lzma/version.h         |    2 +-
+ src/liblzma/common/Makefile.inc        |    7 -
+ src/liblzma/common/common.c            |    9 +-
+ src/liblzma/common/common.h            |   16 -
+ src/liblzma/common/outqueue.c          |  184 ------
+ src/liblzma/common/outqueue.h          |  155 -----
+ src/liblzma/common/stream_encoder_mt.c | 1013 --------------------------------
+ src/xz/args.c                          |    5 +-
+ src/xz/coder.c                         |  210 +++----
+ src/xz/hardware.c                      |   24 +-
+ src/xz/hardware.h                      |    9 +-
+ src/xz/private.h                       |    2 -
+ src/xz/xz.1                            |   34 +-
+ 16 files changed, 117 insertions(+), 1723 deletions(-)
+ delete mode 100644 src/liblzma/common/outqueue.c
+ delete mode 100644 src/liblzma/common/outqueue.h
+ delete mode 100644 src/liblzma/common/stream_encoder_mt.c
+
+diff --git a/configure.ac b/configure.ac
+index 25eb838..c9585e5 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -471,7 +471,6 @@ if test "x$enable_threads" = xyes; then
+ 	AC_CHECK_DECLS([CLOCK_MONOTONIC], [], [], [[#include <time.h>]])
+ 	CFLAGS=$OLD_CFLAGS
+ fi
+-AM_CONDITIONAL([COND_THREADS], [test "x$ax_pthread_ok" = xyes])
+ 
+ echo
+ echo "Initializing Libtool:"
+diff --git a/src/liblzma/Makefile.am b/src/liblzma/Makefile.am
+index 5bd205d..ac2d1ed 100644
+--- a/src/liblzma/Makefile.am
++++ b/src/liblzma/Makefile.am
+@@ -24,7 +24,7 @@ liblzma_la_CPPFLAGS = \
+ 	-I$(top_srcdir)/src/liblzma/simple \
+ 	-I$(top_srcdir)/src/common \
+ 	-DTUKLIB_SYMBOL_PREFIX=lzma_
+-liblzma_la_LDFLAGS = -no-undefined -version-info 5:99:0
++liblzma_la_LDFLAGS = -no-undefined -version-info 5:0:0
+ 
+ if COND_SYMVERS
+ EXTRA_DIST += liblzma.map
+diff --git a/src/liblzma/api/lzma/container.h b/src/liblzma/api/lzma/container.h
+index 499d8b9..7a9ffc6 100644
+--- a/src/liblzma/api/lzma/container.h
++++ b/src/liblzma/api/lzma/container.h
+@@ -60,129 +60,6 @@
+ #define LZMA_PRESET_EXTREME       (UINT32_C(1) << 31)
+ 
+ 
+-#ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
+-/**
+- * \brief       Multithreading options
+- */
+-typedef struct {
+-	/**
+-	 * \brief       Flags
+-	 *
+-	 * Set this to zero if no flags are wanted.
+-	 *
+-	 * No flags are currently supported.
+-	 */
+-	uint32_t flags;
+-
+-	/**
+-	 * \brief       Number of worker threads to use
+-	 */
+-	uint32_t threads;
+-
+-	/**
+-	 * \brief       Maximum uncompressed size of a Block
+-	 *
+-	 * The encoder will start a new .xz Block every block_size bytes.
+-	 * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
+-	 * the caller may tell liblzma to start a new Block earlier.
+-	 *
+-	 * With LZMA2, a recommended block size is 2-4 times the LZMA2
+-	 * dictionary size. With very small dictionaries, it is recommended
+-	 * to use at least 1 MiB block size for good compression ratio, even
+-	 * if this is more than four times the dictionary size. Note that
+-	 * these are only recommendations for typical use cases; feel free
+-	 * to use other values. Just keep in mind that using a block size
+-	 * less than the LZMA2 dictionary size is waste of RAM.
+-	 *
+-	 * Set this to 0 to let liblzma choose the block size depending
+-	 * on the compression options. For LZMA2 it will be 3*dict_size
+-	 * or 1 MiB, whichever is more.
+-	 */
+-	uint64_t block_size;
+-
+-	/**
+-	 * \brief       Timeout to allow lzma_code() to return early
+-	 *
+-	 * Multithreading can make liblzma to consume input and produce
+-	 * output in a very bursty way: it may first read a lot of input
+-	 * to fill internal buffers, then no input or output occurs for
+-	 * a while.
+-	 *
+-	 * In single-threaded mode, lzma_code() won't return until it has
+-	 * either consumed all the input or filled the output buffer. If
+-	 * this is done in multithreaded mode, it may cause a call
+-	 * lzma_code() to take even tens of seconds, which isn't acceptable
+-	 * in all applications.
+-	 *
+-	 * To avoid very long blocking times in lzma_code(), a timeout
+-	 * (in milliseconds) may be set here. If lzma_code() would block
+-	 * longer than this number of milliseconds, it will return with
+-	 * LZMA_OK. Reasonable values are 100 ms or more. The xz command
+-	 * line tool uses 300 ms.
+-	 *
+-	 * If long blocking times are fine for you, set timeout to a special
+-	 * value of 0, which will disable the timeout mechanism and will make
+-	 * lzma_code() block until all the input is consumed or the output
+-	 * buffer has been filled.
+-	 *
+-	 * \note        Even with a timeout, lzma_code() might sometimes take
+-	 *              somewhat long time to return. No timing guarantees
+-	 *              are made.
+-	 */
+-	uint32_t timeout;
+-
+-	/**
+-	 * \brief       Compression preset (level and possible flags)
+-	 *
+-	 * The preset is set just like with lzma_easy_encoder().
+-	 * The preset is ignored if filters below is non-NULL.
+-	 */
+-	uint32_t preset;
+-
+-	/**
+-	 * \brief       Filter chain (alternative to a preset)
+-	 *
+-	 * If this is NULL, the preset above is used. Otherwise the preset
+-	 * is ignored and the filter chain specified here is used.
+-	 */
+-	const lzma_filter *filters;
+-
+-	/**
+-	 * \brief       Integrity check type
+-	 *
+-	 * See check.h for available checks. The xz command line tool
+-	 * defaults to LZMA_CHECK_CRC64, which is a good choice if you
+-	 * are unsure.
+-	 */
+-	lzma_check check;
+-
+-	/*
+-	 * Reserved space to allow possible future extensions without
+-	 * breaking the ABI. You should not touch these, because the names
+-	 * of these variables may change. These are and will never be used
+-	 * with the currently supported options, so it is safe to leave these
+-	 * uninitialized.
+-	 */
+-	lzma_reserved_enum reserved_enum1;
+-	lzma_reserved_enum reserved_enum2;
+-	lzma_reserved_enum reserved_enum3;
+-	uint32_t reserved_int1;
+-	uint32_t reserved_int2;
+-	uint32_t reserved_int3;
+-	uint32_t reserved_int4;
+-	uint64_t reserved_int5;
+-	uint64_t reserved_int6;
+-	uint64_t reserved_int7;
+-	uint64_t reserved_int8;
+-	void *reserved_ptr1;
+-	void *reserved_ptr2;
+-	void *reserved_ptr3;
+-	void *reserved_ptr4;
+-
+-} lzma_mt;
+-#endif
+-
+-
+ /**
+  * \brief       Calculate approximate memory usage of easy encoder
+  *
+@@ -313,50 +190,6 @@ extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
+ 		lzma_nothrow lzma_attr_warn_unused_result;
+ 
+ 
+-#ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
+-/**
+- * \brief       Calculate approximate memory usage of multithreaded .xz encoder
+- *
+- * Since doing the encoding in threaded mode doesn't affect the memory
+- * requirements of single-threaded decompressor, you can use
+- * lzma_easy_decoder_memusage(options->preset) or
+- * lzma_raw_decoder_memusage(options->filters) to calculate
+- * the decompressor memory requirements.
+- *
+- * \param       options Compression options
+- *
+- * \return      Number of bytes of memory required for encoding with the
+- *              given options. If an error occurs, for example due to
+- *              unsupported preset or filter chain, UINT64_MAX is returned.
+- */
+-extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
+-		const lzma_mt *options) lzma_nothrow lzma_attr_pure;
+-
+-
+-/**
+- * \brief       Initialize multithreaded .xz Stream encoder
+- *
+- * This provides the functionality of lzma_easy_encoder() and
+- * lzma_stream_encoder() as a single function for multithreaded use.
+- *
+- * TODO: For lzma_code(), only LZMA_RUN and LZMA_FINISH are currently
+- * supported. Support for other actions has been planned.
+- *
+- * \param       strm    Pointer to properly prepared lzma_stream
+- * \param       options Pointer to multithreaded compression options
+- *
+- * \return      - LZMA_OK
+- *              - LZMA_MEM_ERROR
+- *              - LZMA_UNSUPPORTED_CHECK
+- *              - LZMA_OPTIONS_ERROR
+- *              - LZMA_PROG_ERROR
+- */
+-extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
+-		lzma_stream *strm, const lzma_mt *options)
+-		lzma_nothrow lzma_attr_warn_unused_result;
+-#endif
+-
+-
+ /**
+  * \brief       Initialize .lzma encoder (legacy file format)
+  *
+diff --git a/src/liblzma/api/lzma/version.h b/src/liblzma/api/lzma/version.h
+index 4bf7e40..a908ea2 100644
+--- a/src/liblzma/api/lzma/version.h
++++ b/src/liblzma/api/lzma/version.h
+@@ -22,7 +22,7 @@
+  */
+ #define LZMA_VERSION_MAJOR 5
+ #define LZMA_VERSION_MINOR 1
+-#define LZMA_VERSION_PATCH 1
++#define LZMA_VERSION_PATCH 0
+ #define LZMA_VERSION_STABILITY LZMA_VERSION_STABILITY_ALPHA
+ 
+ #ifndef LZMA_VERSION_COMMIT
+diff --git a/src/liblzma/common/Makefile.inc b/src/liblzma/common/Makefile.inc
+index dd5a8c8..81d751e 100644
+--- a/src/liblzma/common/Makefile.inc
++++ b/src/liblzma/common/Makefile.inc
+@@ -40,13 +40,6 @@ liblzma_la_SOURCES += \
+ 	common/stream_encoder.c \
+ 	common/stream_flags_encoder.c \
+ 	common/vli_encoder.c
+-
+-if COND_THREADS
+-liblzma_la_SOURCES += \
+-	common/outqueue.c \
+-	common/outqueue.h \
+-	common/stream_encoder_mt.c
+-endif
+ endif
+ 
+ if COND_MAIN_DECODER
+diff --git a/src/liblzma/common/common.c b/src/liblzma/common/common.c
+index 85ae96a..50c984c 100644
+--- a/src/liblzma/common/common.c
++++ b/src/liblzma/common/common.c
+@@ -263,9 +263,7 @@ lzma_code(lzma_stream *strm, lzma_action action)
+ 
+ 	strm->internal->avail_in = strm->avail_in;
+ 
+-	// Cast is needed to silence a warning about LZMA_TIMED_OUT, which
+-	// isn't part of lzma_ret enumeration.
+-	switch ((unsigned int)(ret)) {
++	switch (ret) {
+ 	case LZMA_OK:
+ 		// Don't return LZMA_BUF_ERROR when it happens the first time.
+ 		// This is to avoid returning LZMA_BUF_ERROR when avail_out
+@@ -281,11 +279,6 @@ lzma_code(lzma_stream *strm, lzma_action action)
+ 		}
+ 		break;
+ 
+-	case LZMA_TIMED_OUT:
+-		strm->internal->allow_buf_error = false;
+-		ret = LZMA_OK;
+-		break;
+-
+ 	case LZMA_STREAM_END:
+ 		if (strm->internal->sequence == ISEQ_SYNC_FLUSH
+ 				|| strm->internal->sequence == ISEQ_FULL_FLUSH)
+diff --git a/src/liblzma/common/common.h b/src/liblzma/common/common.h
+index 5c92af2..45aba4f 100644
+--- a/src/liblzma/common/common.h
++++ b/src/liblzma/common/common.h
+@@ -32,8 +32,6 @@
+ 
+ #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
+ 
+-#define LZMA_UNSTABLE
+-
+ #include "lzma.h"
+ 
+ // These allow helping the compiler in some often-executed branches, whose
+@@ -51,13 +49,6 @@
+ #define LZMA_BUFFER_SIZE 4096
+ 
+ 
+-/// Maximum number of worker threads within one multithreaded component.
+-/// The limit exists solely to make it simpler to prevent integer overflows
+-/// when allocating structures etc. This should be big enough for now...
+-/// the code won't scale anywhere close to this number anyway.
+-#define LZMA_THREADS_MAX 16384
+-
+-
+ /// Starting value for memory usage estimates. Instead of calculating size
+ /// of _every_ structure and taking into account malloc() overhead etc., we
+ /// add a base size to all memory usage estimates. It's not very accurate
+@@ -78,13 +69,6 @@
+ 	| LZMA_CONCATENATED )
+ 
+ 
+-/// Special return value (lzma_ret) to indicate that a timeout was reached
+-/// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
+-/// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
+-/// there's no need to have it in the public API.
+-#define LZMA_TIMED_OUT 32
+-
+-
+ /// Type of encoder/decoder specific data; the actual structure is defined
+ /// differently in different coders.
+ typedef struct lzma_coder_s lzma_coder;
+diff --git a/src/liblzma/common/outqueue.c b/src/liblzma/common/outqueue.c
+deleted file mode 100644
+index d7a87d9..0000000
+--- a/src/liblzma/common/outqueue.c
++++ /dev/null
+@@ -1,184 +0,0 @@
+-///////////////////////////////////////////////////////////////////////////////
+-//
+-/// \file       outqueue.c
+-/// \brief      Output queue handling in multithreaded coding
+-//
+-//  Author:     Lasse Collin
+-//
+-//  This file has been put into the public domain.
+-//  You can do whatever you want with this file.
+-//
+-///////////////////////////////////////////////////////////////////////////////
+-
+-#include "outqueue.h"
+-
+-
+-/// This is to ease integer overflow checking: We may allocate up to
+-/// 2 * LZMA_THREADS_MAX buffers and we need some extra memory for other
+-/// data structures (that's the second /2).
+-#define BUF_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX / 2 / 2)
+-
+-
+-static lzma_ret
+-get_options(uint64_t *bufs_alloc_size, uint32_t *bufs_count,
+-		uint64_t buf_size_max, uint32_t threads)
+-{
+-	if (threads > LZMA_THREADS_MAX || buf_size_max > BUF_SIZE_MAX)
+-		return LZMA_OPTIONS_ERROR;
+-
+-	// The number of buffers is twice the number of threads.
+-	// This wastes RAM but keeps the threads busy when buffers
+-	// finish out of order.
+-	//
+-	// NOTE: If this is changed, update BUF_SIZE_MAX too.
+-	*bufs_count = threads * 2;
+-	*bufs_alloc_size = *bufs_count * buf_size_max;
+-
+-	return LZMA_OK;
+-}
+-
+-
+-extern uint64_t
+-lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads)
+-{
+-	uint64_t bufs_alloc_size;
+-	uint32_t bufs_count;
+-
+-	if (get_options(&bufs_alloc_size, &bufs_count, buf_size_max, threads)
+-			!= LZMA_OK)
+-		return UINT64_MAX;
+-
+-	return sizeof(lzma_outq) + bufs_count * sizeof(lzma_outbuf)
+-			+ bufs_alloc_size;
+-}
+-
+-
+-extern lzma_ret
+-lzma_outq_init(lzma_outq *outq, lzma_allocator *allocator,
+-		uint64_t buf_size_max, uint32_t threads)
+-{
+-	uint64_t bufs_alloc_size;
+-	uint32_t bufs_count;
+-
+-	// Set bufs_count and bufs_alloc_size.
+-	return_if_error(get_options(&bufs_alloc_size, &bufs_count,
+-			buf_size_max, threads));
+-
+-	// Allocate memory if needed.
+-	if (outq->buf_size_max != buf_size_max
+-			|| outq->bufs_allocated != bufs_count) {
+-		lzma_outq_end(outq, allocator);
+-
+-#if SIZE_MAX < UINT64_MAX
+-		if (bufs_alloc_size > SIZE_MAX)
+-			return LZMA_MEM_ERROR;
+-#endif
+-
+-		outq->bufs = lzma_alloc(bufs_count * sizeof(lzma_outbuf),
+-				allocator);
+-		outq->bufs_mem = lzma_alloc((size_t)(bufs_alloc_size),
+-				allocator);
+-
+-		if (outq->bufs == NULL || outq->bufs_mem == NULL) {
+-			lzma_outq_end(outq, allocator);
+-			return LZMA_MEM_ERROR;
+-		}
+-	}
+-
+-	// Initialize the rest of the main structure. Initialization of
+-	// outq->bufs[] is done when they are actually needed.
+-	outq->buf_size_max = (size_t)(buf_size_max);
+-	outq->bufs_allocated = bufs_count;
+-	outq->bufs_pos = 0;
+-	outq->bufs_used = 0;
+-	outq->read_pos = 0;
+-
+-	return LZMA_OK;
+-}
+-
+-
+-extern void
+-lzma_outq_end(lzma_outq *outq, lzma_allocator *allocator)
+-{
+-	lzma_free(outq->bufs, allocator);
+-	outq->bufs = NULL;
+-
+-	lzma_free(outq->bufs_mem, allocator);
+-	outq->bufs_mem = NULL;
+-
+-	return;
+-}
+-
+-
+-extern lzma_outbuf *
+-lzma_outq_get_buf(lzma_outq *outq)
+-{
+-	// Caller must have checked it with lzma_outq_has_buf().
+-	assert(outq->bufs_used < outq->bufs_allocated);
+-
+-	// Initialize the new buffer.
+-	lzma_outbuf *buf = &outq->bufs[outq->bufs_pos];
+-	buf->buf = outq->bufs_mem + outq->bufs_pos * outq->buf_size_max;
+-	buf->size = 0;
+-	buf->finished = false;
+-
+-	// Update the queue state.
+-	if (++outq->bufs_pos == outq->bufs_allocated)
+-		outq->bufs_pos = 0;
+-
+-	++outq->bufs_used;
+-
+-	return buf;
+-}
+-
+-
+-extern bool
+-lzma_outq_is_readable(const lzma_outq *outq)
+-{
+-	uint32_t i = outq->bufs_pos - outq->bufs_used;
+-	if (outq->bufs_pos < outq->bufs_used)
+-		i += outq->bufs_allocated;
+-
+-	return outq->bufs[i].finished;
+-}
+-
+-
+-extern lzma_ret
+-lzma_outq_read(lzma_outq *restrict outq, uint8_t *restrict out,
+-		size_t *restrict out_pos, size_t out_size,
+-		lzma_vli *restrict unpadded_size,
+-		lzma_vli *restrict uncompressed_size)
+-{
+-	// There must be at least one buffer from which to read.
+-	if (outq->bufs_used == 0)
+-		return LZMA_OK;
+-
+-	// Get the buffer.
+-	uint32_t i = outq->bufs_pos - outq->bufs_used;
+-	if (outq->bufs_pos < outq->bufs_used)
+-		i += outq->bufs_allocated;
+-
+-	lzma_outbuf *buf = &outq->bufs[i];
+-
+-	// If it isn't finished yet, we cannot read from it.
+-	if (!buf->finished)
+-		return LZMA_OK;
+-
+-	// Copy from the buffer to output.
+-	lzma_bufcpy(buf->buf, &outq->read_pos, buf->size,
+-			out, out_pos, out_size);
+-
+-	// Return if we didn't get all the data from the buffer.
+-	if (outq->read_pos < buf->size)
+-		return LZMA_OK;
+-
+-	// The buffer was finished. Tell the caller its size information.
+-	*unpadded_size = buf->unpadded_size;
+-	*uncompressed_size = buf->uncompressed_size;
+-
+-	// Free this buffer for further use.
+-	--outq->bufs_used;
+-	outq->read_pos = 0;
+-
+-	return LZMA_STREAM_END;
+-}
+diff --git a/src/liblzma/common/outqueue.h b/src/liblzma/common/outqueue.h
+deleted file mode 100644
+index 154f91b..0000000
+--- a/src/liblzma/common/outqueue.h
++++ /dev/null
+@@ -1,155 +0,0 @@
+-///////////////////////////////////////////////////////////////////////////////
+-//
+-/// \file       outqueue.h
+-/// \brief      Output queue handling in multithreaded coding
+-//
+-//  Author:     Lasse Collin
+-//
+-//  This file has been put into the public domain.
+-//  You can do whatever you want with this file.
+-//
+-///////////////////////////////////////////////////////////////////////////////
+-
+-#include "common.h"
+-
+-
+-/// Output buffer for a single thread
+-typedef struct {
+-	/// Pointer to the output buffer of lzma_outq.buf_size_max bytes
+-	uint8_t *buf;
+-
+-	/// Amount of data written to buf
+-	size_t size;
+-
+-	/// Additional size information
+-	lzma_vli unpadded_size;
+-	lzma_vli uncompressed_size;
+-
+-	/// True when no more data will be written into this buffer.
+-	///
+-	/// \note       This is read by another thread and thus access
+-	///             to this variable needs a mutex.
+-	bool finished;
+-
+-} lzma_outbuf;
+-
+-
+-typedef struct {
+-	/// Array of buffers that are used cyclically.
+-	lzma_outbuf *bufs;
+-
+-	/// Memory allocated for all the buffers
+-	uint8_t *bufs_mem;
+-
+-	/// Amount of buffer space available in each buffer
+-	size_t buf_size_max;
+-
+-	/// Number of buffers allocated
+-	uint32_t bufs_allocated;
+-
+-	/// Position in the bufs array. The next buffer to be taken
+-	/// into use is bufs[bufs_pos].
+-	uint32_t bufs_pos;
+-
+-	/// Number of buffers in use
+-	uint32_t bufs_used;
+-
+-	/// Position in the buffer in lzma_outq_read()
+-	size_t read_pos;
+-
+-} lzma_outq;
+-
+-
+-/**
+- * \brief       Calculate the memory usage of an output queue
+- *
+- * \return      Approximate memory usage in bytes or UINT64_MAX on error.
+- */
+-extern uint64_t lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads);
+-
+-
+-/// \brief      Initialize an output queue
+-///
+-/// \param      outq            Pointer to an output queue. Before calling
+-///                             this function the first time, *outq should
+-///                             have been zeroed with memzero() so that this
+-///                             function knows that there are no previous
+-///                             allocations to free.
+-/// \param      allocator       Pointer to allocator or NULL
+-/// \param      buf_size_max    Maximum amount of data that a single buffer
+-///                             in the queue may need to store.
+-/// \param      threads         Number of buffers that may be in use
+-///                             concurrently. Note that more than this number
+-///                             of buffers will actually get allocated to
+-///                             improve performance when buffers finish
+-///                             out of order.
+-///
+-/// \return     - LZMA_OK
+-///             - LZMA_MEM_ERROR
+-///
+-extern lzma_ret lzma_outq_init(lzma_outq *outq, lzma_allocator *allocator,
+-		uint64_t buf_size_max, uint32_t threads);
+-
+-
+-/// \brief      Free the memory associated with the output queue
+-extern void lzma_outq_end(lzma_outq *outq, lzma_allocator *allocator);
+-
+-
+-/// \brief      Get a new buffer
+-///
+-/// lzma_outq_has_buf() must be used to check that there is a buffer
+-/// available before calling lzma_outq_get_buf().
+-///
+-extern lzma_outbuf *lzma_outq_get_buf(lzma_outq *outq);
+-
+-
+-/// \brief      Test if there is data ready to be read
+-///
+-/// Call to this function must be protected with the same mutex that
+-/// is used to protect lzma_outbuf.finished.
+-///
+-extern bool lzma_outq_is_readable(const lzma_outq *outq);
+-
+-
+-/// \brief      Read finished data
+-///
+-/// \param      outq            Pointer to an output queue
+-/// \param      out             Beginning of the output buffer
+-/// \param      out_pos         The next byte will be written to
+-///                             out[*out_pos].
+-/// \param      out_size        Size of the out buffer; the first byte into
+-///                             which no data is written to is out[out_size].
+-/// \param      unpadded_size   Unpadded Size from the Block encoder
+-/// \param      uncompressed_size Uncompressed Size from the Block encoder
+-///
+-/// \return     - LZMA: All OK. Either no data was available or the buffer
+-///               being read didn't become empty yet.
+-///             - LZMA_STREAM_END: The buffer being read was finished.
+-///               *unpadded_size and *uncompressed_size were set.
+-///
+-/// \note       This reads lzma_outbuf.finished variables and thus call
+-///             to this function needs to be protected with a mutex.
+-///
+-extern lzma_ret lzma_outq_read(lzma_outq *restrict outq,
+-		uint8_t *restrict out, size_t *restrict out_pos,
+-		size_t out_size, lzma_vli *restrict unpadded_size,
+-		lzma_vli *restrict uncompressed_size);
+-
+-
+-/// \brief      Test if there is at least one buffer free
+-///
+-/// This must be used before getting a new buffer with lzma_outq_get_buf().
+-///
+-static inline bool
+-lzma_outq_has_buf(const lzma_outq *outq)
+-{
+-	return outq->bufs_used < outq->bufs_allocated;
+-}
+-
+-
+-/// \brief      Test if the queue is completely empty
+-static inline bool
+-lzma_outq_is_empty(const lzma_outq *outq)
+-{
+-	return outq->bufs_used == 0;
+-}
+diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
+deleted file mode 100644
+index a4b2800..0000000
+--- a/src/liblzma/common/stream_encoder_mt.c
++++ /dev/null
+@@ -1,1013 +0,0 @@
+-///////////////////////////////////////////////////////////////////////////////
+-//
+-/// \file       stream_encoder_mt.c
+-/// \brief      Multithreaded .xz Stream encoder
+-//
+-//  Author:     Lasse Collin
+-//
+-//  This file has been put into the public domain.
+-//  You can do whatever you want with this file.
+-//
+-///////////////////////////////////////////////////////////////////////////////
+-
+-#include "filter_encoder.h"
+-#include "easy_preset.h"
+-#include "block_encoder.h"
+-#include "index_encoder.h"
+-#include "outqueue.h"
+-
+-
+-/// Maximum supported block size. This makes it simpler to prevent integer
+-/// overflows if we are given unusually large block size.
+-#define BLOCK_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX)
+-
+-
+-typedef enum {
+-	/// Waiting for work.
+-	THR_IDLE,
+-
+-	/// Encoding is in progress.
+-	THR_RUN,
+-
+-	/// Encoding is in progress but no more input data will
+-	/// be read.
+-	THR_FINISH,
+-
+-	/// The main thread wants the thread to stop whatever it was doing
+-	/// but not exit.
+-	THR_STOP,
+-
+-	/// The main thread wants the thread to exit. We could use
+-	/// cancellation but since there's stopped anyway, this is lazier.
+-	THR_EXIT,
+-
+-} worker_state;
+-
+-
+-typedef struct worker_thread_s worker_thread;
+-struct worker_thread_s {
+-	worker_state state;
+-
+-	/// Input buffer of coder->block_size bytes. The main thread will
+-	/// put new input into this and update in_size accordingly. Once
+-	/// no more input is coming, state will be set to THR_FINISH.
+-	uint8_t *in;
+-
+-	/// Amount of data available in the input buffer. This is modified
+-	/// only by the main thread.
+-	size_t in_size;
+-
+-	/// Output buffer for this thread. This is set by the main
+-	/// thread every time a new Block is started with this thread
+-	/// structure.
+-	lzma_outbuf *outbuf;
+-
+-	/// Pointer to the main structure is needed when putting this
+-	/// thread back to the stack of free threads.
+-	lzma_coder *coder;
+-
+-	/// The allocator is set by the main thread. Since a copy of the
+-	/// pointer is kept here, the application must not change the
+-	/// allocator before calling lzma_end().
+-	lzma_allocator *allocator;
+-
+-	/// Block encoder
+-	lzma_next_coder block_encoder;
+-
+-	/// Compression options for this Block
+-	lzma_block block_options;
+-
+-	/// Next structure in the stack of free worker threads.
+-	worker_thread *next;
+-
+-	pthread_mutex_t mutex;
+-	pthread_cond_t cond;
+-
+-	/// The ID of this thread is used to join the thread
+-	/// when it's not needed anymore.
+-	pthread_t thread_id;
+-};
+-
+-
+-struct lzma_coder_s {
+-	enum {
+-		SEQ_STREAM_HEADER,
+-		SEQ_BLOCK,
+-		SEQ_INDEX,
+-		SEQ_STREAM_FOOTER,
+-	} sequence;
+-
+-	/// Start a new Block every block_size bytes of input unless
+-	/// LZMA_FULL_FLUSH or LZMA_FULL_BARRIER is used earlier.
+-	size_t block_size;
+-
+-	/// The filter chain currently in use
+-	lzma_filter filters[LZMA_FILTERS_MAX + 1];
+-
+-
+-	/// Index to hold sizes of the Blocks
+-	lzma_index *index;
+-
+-	/// Index encoder
+-	lzma_next_coder index_encoder;
+-
+-
+-	/// Stream Flags for encoding the Stream Header and Stream Footer.
+-	lzma_stream_flags stream_flags;
+-
+-	/// Buffer to hold Stream Header and Stream Footer.
+-	uint8_t header[LZMA_STREAM_HEADER_SIZE];
+-
+-	/// Read position in header[]
+-	size_t header_pos;
+-
+-
+-	/// Output buffer queue for compressed data
+-	lzma_outq outq;
+-
+-
+-	/// True if wait_max is used.
+-	bool has_timeout;
+-
+-	/// Maximum wait time if cannot use all the input and cannot
+-	/// fill the output buffer.
+-	struct timespec wait_max;
+-
+-
+-	/// Error code from a worker thread
+-	lzma_ret thread_error;
+-
+-	/// Array of allocated thread-specific structures
+-	worker_thread *threads;
+-
+-	/// Number of structures in "threads" above. This is also the
+-	/// number of threads that will be created at maximum.
+-	uint32_t threads_max;
+-
+-	/// Number of thread structures that have been initialized, and
+-	/// thus the number of worker threads actually created so far.
+-	uint32_t threads_initialized;
+-
+-	/// Stack of free threads. When a thread finishes, it puts itself
+-	/// back into this stack. This starts as empty because threads
+-	/// are created only when actually needed.
+-	worker_thread *threads_free;
+-
+-	/// The most recent worker thread to which the main thread writes
+-	/// the new input from the application.
+-	worker_thread *thr;
+-
+-	pthread_mutex_t mutex;
+-	mythread_cond cond;
+-};
+-
+-
+-/// Tell the main thread that something has gone wrong.
+-static void
+-worker_error(worker_thread *thr, lzma_ret ret)
+-{
+-	assert(ret != LZMA_OK);
+-	assert(ret != LZMA_STREAM_END);
+-
+-	mythread_sync(thr->coder->mutex) {
+-		if (thr->coder->thread_error == LZMA_OK)
+-			thr->coder->thread_error = ret;
+-
+-		mythread_cond_signal(&thr->coder->cond);
+-	}
+-
+-	return;
+-}
+-
+-
+-static worker_state
+-worker_encode(worker_thread *thr, worker_state state)
+-{
+-	// Set the Block options.
+-	thr->block_options = (lzma_block){
+-		.version = 0,
+-		.check = thr->coder->stream_flags.check,
+-		.compressed_size = thr->coder->outq.buf_size_max,
+-		.uncompressed_size = thr->coder->block_size,
+-
+-		// TODO: To allow changing the filter chain, the filters
+-		// array must be copied to each worker_thread.
+-		.filters = thr->coder->filters,
+-	};
+-
+-	// Calculate maximum size of the Block Header. This amount is
+-	// reserved in the beginning of the buffer so that Block Header
+-	// along with Compressed Size and Uncompressed Size can be
+-	// written there.
+-	lzma_ret ret = lzma_block_header_size(&thr->block_options);
+-	if (ret != LZMA_OK) {
+-		worker_error(thr, ret);
+-		return THR_STOP;
+-	}
+-
+-	// Initialize the Block encoder.
+-	ret = lzma_block_encoder_init(&thr->block_encoder,
+-			thr->allocator, &thr->block_options);
+-	if (ret != LZMA_OK) {
+-		worker_error(thr, ret);
+-		return THR_STOP;
+-	}
+-
+-	size_t in_pos = 0;
+-	size_t in_size = 0;
+-
+-	thr->outbuf->size = thr->block_options.header_size;
+-	const size_t out_size = thr->coder->outq.buf_size_max;
+-
+-	do {
+-		mythread_sync(thr->mutex) {
+-			while (in_size == thr->in_size
+-					&& thr->state == THR_RUN)
+-				pthread_cond_wait(&thr->cond, &thr->mutex);
+-
+-			state = thr->state;
+-			in_size = thr->in_size;
+-
+-			// TODO? Store in_pos and out_pos into *thr here
+-			// so that the application may read them via
+-			// some currently non-existing function to get
+-			// progress information.
+-		}
+-
+-		// Return if we were asked to stop or exit.
+-		if (state >= THR_STOP)
+-			return state;
+-
+-		lzma_action action = state == THR_FINISH
+-				? LZMA_FINISH : LZMA_RUN;
+-
+-		// Limit the amount of input given to the Block encoder
+-		// at once. This way this thread can react fairly quickly
+-		// if the main thread wants us to stop or exit.
+-		static const size_t in_chunk_max = 16384;
+-		size_t in_limit = in_size;
+-		if (in_size - in_pos > in_chunk_max) {
+-			in_limit = in_pos + in_chunk_max;
+-			action = LZMA_RUN;
+-		}
+-
+-		ret = thr->block_encoder.code(
+-				thr->block_encoder.coder, thr->allocator,
+-				thr->in, &in_pos, in_limit, thr->outbuf->buf,
+-				&thr->outbuf->size, out_size, action);
+-	} while (ret == LZMA_OK);
+-
+-	if (ret != LZMA_STREAM_END) {
+-		worker_error(thr, ret);
+-		return THR_STOP;
+-	}
+-
+-	assert(state == THR_FINISH);
+-
+-	// Encode the Block Header. By doing it after the compression,
+-	// we can store the Compressed Size and Uncompressed Size fields.
+-	ret = lzma_block_header_encode(&thr->block_options, thr->outbuf->buf);
+-	if (ret != LZMA_OK) {
+-		worker_error(thr, ret);
+-		return THR_STOP;
+-	}
+-
+-	// Set the size information that will be read by the main thread
+-	// to write the Index field.
+-	thr->outbuf->unpadded_size
+-			= lzma_block_unpadded_size(&thr->block_options);
+-	assert(thr->outbuf->unpadded_size != 0);
+-	thr->outbuf->uncompressed_size = thr->block_options.uncompressed_size;
+-
+-	return THR_FINISH;
+-}
+-
+-
+-static void *
+-worker_start(void *thr_ptr)
+-{
+-	worker_thread *thr = thr_ptr;
+-	worker_state state = THR_IDLE; // Init to silence a warning
+-
+-	while (true) {
+-		// Wait for work.
+-		mythread_sync(thr->mutex) {
+-			while (true) {
+-				// The thread is already idle so if we are
+-				// requested to stop, just set the state.
+-				if (thr->state == THR_STOP) {
+-					thr->state = THR_IDLE;
+-					pthread_cond_signal(&thr->cond);
+-				}
+-
+-				state = thr->state;
+-				if (state != THR_IDLE)
+-					break;
+-
+-				pthread_cond_wait(&thr->cond, &thr->mutex);
+-			}
+-		}
+-
+-		assert(state != THR_IDLE);
+-		assert(state != THR_STOP);
+-
+-		if (state <= THR_FINISH)
+-			state = worker_encode(thr, state);
+-
+-		if (state == THR_EXIT)
+-			break;
+-
+-		// Mark the thread as idle. Signal is needed for the case
+-		// where the main thread is waiting for the threads to stop.
+-		mythread_sync(thr->mutex) {
+-			thr->state = THR_IDLE;
+-			pthread_cond_signal(&thr->cond);
+-		}
+-
+-		mythread_sync(thr->coder->mutex) {
+-			// Mark the output buffer as finished if
+-			// no errors occurred.
+-			thr->outbuf->finished = state == THR_FINISH;
+-
+-			// Return this thread to the stack of free threads.
+-			thr->next = thr->coder->threads_free;
+-			thr->coder->threads_free = thr;
+-
+-			mythread_cond_signal(&thr->coder->cond);
+-		}
+-	}
+-
+-	// Exiting, free the resources.
+-	pthread_mutex_destroy(&thr->mutex);
+-	pthread_cond_destroy(&thr->cond);
+-
+-	lzma_next_end(&thr->block_encoder, thr->allocator);
+-	lzma_free(thr->in, thr->allocator);
+-	return NULL;
+-}
+-
+-
+-/// Make the threads stop but not exit. Optionally wait for them to stop.
+-static void
+-threads_stop(lzma_coder *coder, bool wait)
+-{
+-	// Tell the threads to stop.
+-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
+-		mythread_sync(coder->threads[i].mutex) {
+-			coder->threads[i].state = THR_STOP;
+-			pthread_cond_signal(&coder->threads[i].cond);
+-		}
+-	}
+-
+-	if (!wait)
+-		return;
+-
+-	// Wait for the threads to settle in the idle state.
+-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
+-		mythread_sync(coder->threads[i].mutex) {
+-			while (coder->threads[i].state != THR_IDLE)
+-				pthread_cond_wait(&coder->threads[i].cond,
+-						&coder->threads[i].mutex);
+-		}
+-	}
+-
+-	return;
+-}
+-
+-
+-/// Stop the threads and free the resources associated with them.
+-/// Wait until the threads have exited.
+-static void
+-threads_end(lzma_coder *coder, lzma_allocator *allocator)
+-{
+-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
+-		mythread_sync(coder->threads[i].mutex) {
+-			coder->threads[i].state = THR_EXIT;
+-			pthread_cond_signal(&coder->threads[i].cond);
+-		}
+-	}
+-
+-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
+-		int ret = pthread_join(coder->threads[i].thread_id, NULL);
+-		assert(ret == 0);
+-		(void)ret;
+-	}
+-
+-	lzma_free(coder->threads, allocator);
+-	return;
+-}
+-
+-
+-/// Initialize a new worker_thread structure and create a new thread.
+-static lzma_ret
+-initialize_new_thread(lzma_coder *coder, lzma_allocator *allocator)
+-{
+-	worker_thread *thr = &coder->threads[coder->threads_initialized];
+-
+-	thr->in = lzma_alloc(coder->block_size, allocator);
+-	if (thr->in == NULL)
+-		return LZMA_MEM_ERROR;
+-
+-	if (pthread_mutex_init(&thr->mutex, NULL))
+-		goto error_mutex;
+-
+-	if (pthread_cond_init(&thr->cond, NULL))
+-		goto error_cond;
+-
+-	thr->state = THR_IDLE;
+-	thr->allocator = allocator;
+-	thr->coder = coder;
+-	thr->block_encoder = LZMA_NEXT_CODER_INIT;
+-
+-	if (mythread_create(&thr->thread_id, &worker_start, thr))
+-		goto error_thread;
+-
+-	++coder->threads_initialized;
+-	coder->thr = thr;
+-
+-	return LZMA_OK;
+-
+-error_thread:
+-	pthread_cond_destroy(&thr->cond);
+-
+-error_cond:
+-	pthread_mutex_destroy(&thr->mutex);
+-
+-error_mutex:
+-	lzma_free(thr->in, allocator);
+-	return LZMA_MEM_ERROR;
+-}
+-
+-
+-static lzma_ret
+-get_thread(lzma_coder *coder, lzma_allocator *allocator)
+-{
+-	// If there are no free output subqueues, there is no
+-	// point to try getting a thread.
+-	if (!lzma_outq_has_buf(&coder->outq))
+-		return LZMA_OK;
+-
+-	// If there is a free structure on the stack, use it.
+-	mythread_sync(coder->mutex) {
+-		if (coder->threads_free != NULL) {
+-			coder->thr = coder->threads_free;
+-			coder->threads_free = coder->threads_free->next;
+-		}
+-	}
+-
+-	if (coder->thr == NULL) {
+-		// If there are no uninitialized structures left, return.
+-		if (coder->threads_initialized == coder->threads_max)
+-			return LZMA_OK;
+-
+-		// Initialize a new thread.
+-		return_if_error(initialize_new_thread(coder, allocator));
+-	}
+-
+-	// Reset the parts of the thread state that have to be done
+-	// in the main thread.
+-	mythread_sync(coder->thr->mutex) {
+-		coder->thr->state = THR_RUN;
+-		coder->thr->in_size = 0;
+-		coder->thr->outbuf = lzma_outq_get_buf(&coder->outq);
+-		pthread_cond_signal(&coder->thr->cond);
+-	}
+-
+-	return LZMA_OK;
+-}
+-
+-
+-static lzma_ret
+-stream_encode_in(lzma_coder *coder, lzma_allocator *allocator,
+-		const uint8_t *restrict in, size_t *restrict in_pos,
+-		size_t in_size, lzma_action action)
+-{
+-	while (*in_pos < in_size
+-			|| (coder->thr != NULL && action != LZMA_RUN)) {
+-		if (coder->thr == NULL) {
+-			// Get a new thread.
+-			const lzma_ret ret = get_thread(coder, allocator);
+-			if (coder->thr == NULL)
+-				return ret;
+-		}
+-
+-		// Copy the input data to thread's buffer.
+-		size_t thr_in_size = coder->thr->in_size;
+-		lzma_bufcpy(in, in_pos, in_size, coder->thr->in,
+-				&thr_in_size, coder->block_size);
+-
+-		// Tell the Block encoder to finish if
+-		//  - it has got block_size bytes of input; or
+-		//  - all input was used and LZMA_FINISH, LZMA_FULL_FLUSH,
+-		//    or LZMA_FULL_BARRIER was used.
+-		//
+-		// TODO: LZMA_SYNC_FLUSH and LZMA_SYNC_BARRIER.
+-		const bool finish = thr_in_size == coder->block_size
+-				|| (*in_pos == in_size && action != LZMA_RUN);
+-
+-		bool block_error = false;
+-
+-		mythread_sync(coder->thr->mutex) {
+-			if (coder->thr->state == THR_IDLE) {
+-				// Something has gone wrong with the Block
+-				// encoder. It has set coder->thread_error
+-				// which we will read a few lines later.
+-				block_error = true;
+-			} else {
+-				// Tell the Block encoder its new amount
+-				// of input and update the state if needed.
+-				coder->thr->in_size = thr_in_size;
+-
+-				if (finish)
+-					coder->thr->state = THR_FINISH;
+-
+-				pthread_cond_signal(&coder->thr->cond);
+-			}
+-		}
+-
+-		if (block_error) {
+-			lzma_ret ret;
+-
+-			mythread_sync(coder->mutex) {
+-				ret = coder->thread_error;
+-			}
+-
+-			return ret;
+-		}
+-
+-		if (finish)
+-			coder->thr = NULL;
+-	}
+-
+-	return LZMA_OK;
+-}
+-
+-
+-/// Wait until more input can be consumed, more output can be read, or
+-/// an optional timeout is reached.
+-static bool
+-wait_for_work(lzma_coder *coder, struct timespec *wait_abs,
+-		bool *has_blocked, bool has_input)
+-{
+-	if (coder->has_timeout && !*has_blocked) {
+-		// Every time when stream_encode_mt() is called via
+-		// lzma_code(), *has_block starts as false. We set it
+-		// to true here and calculate the absolute time when
+-		// we must return if there's nothing to do.
+-		//
+-		// The idea of *has_blocked is to avoid unneeded calls
+-		// to mythread_cond_abstime(), which may do a syscall
+-		// depending on the operating system.
+-		*has_blocked = true;
+-		*wait_abs = coder->wait_max;
+-		mythread_cond_abstime(&coder->cond, wait_abs);
+-	}
+-
+-	bool timed_out = false;
+-
+-	mythread_sync(coder->mutex) {
+-		// There are four things that we wait. If one of them
+-		// becomes possible, we return.
+-		//  - If there is input left, we need to get a free
+-		//    worker thread and an output buffer for it.
+-		//  - Data ready to be read from the output queue.
+-		//  - A worker thread indicates an error.
+-		//  - Time out occurs.
+-		while ((!has_input || coder->threads_free == NULL
+-					|| !lzma_outq_has_buf(&coder->outq))
+-				&& !lzma_outq_is_readable(&coder->outq)
+-				&& coder->thread_error == LZMA_OK
+-				&& !timed_out) {
+-			if (coder->has_timeout)
+-				timed_out = mythread_cond_timedwait(
+-						&coder->cond, &coder->mutex,
+-						wait_abs) != 0;
+-			else
+-				mythread_cond_wait(&coder->cond,
+-						&coder->mutex);
+-		}
+-	}
+-
+-	return timed_out;
+-}
+-
+-
+-static lzma_ret
+-stream_encode_mt(lzma_coder *coder, lzma_allocator *allocator,
+-		const uint8_t *restrict in, size_t *restrict in_pos,
+-		size_t in_size, uint8_t *restrict out,
+-		size_t *restrict out_pos, size_t out_size, lzma_action action)
+-{
+-	switch (coder->sequence) {
+-	case SEQ_STREAM_HEADER:
+-		lzma_bufcpy(coder->header, &coder->header_pos,
+-				sizeof(coder->header),
+-				out, out_pos, out_size);
+-		if (coder->header_pos < sizeof(coder->header))
+-			return LZMA_OK;
+-
+-		coder->header_pos = 0;
+-		coder->sequence = SEQ_BLOCK;
+-
+-	// Fall through
+-
+-	case SEQ_BLOCK: {
+-		// Initialized to silence warnings.
+-		lzma_vli unpadded_size = 0;
+-		lzma_vli uncompressed_size = 0;
+-		lzma_ret ret = LZMA_OK;
+-
+-		// These are for wait_for_work().
+-		bool has_blocked = false;
+-		struct timespec wait_abs;
+-
+-		while (true) {
+-			mythread_sync(coder->mutex) {
+-				// Check for Block encoder errors.
+-				ret = coder->thread_error;
+-				if (ret != LZMA_OK) {
+-					assert(ret != LZMA_STREAM_END);
+-					break;
+-				}
+-
+-				// Try to read compressed data to out[].
+-				ret = lzma_outq_read(&coder->outq,
+-						out, out_pos, out_size,
+-						&unpadded_size,
+-						&uncompressed_size);
+-			}
+-
+-			if (ret == LZMA_STREAM_END) {
+-				// End of Block. Add it to the Index.
+-				ret = lzma_index_append(coder->index,
+-						allocator, unpadded_size,
+-						uncompressed_size);
+-
+-				// If we didn't fill the output buffer yet,
+-				// try to read more data. Maybe the next
+-				// outbuf has been finished already too.
+-				if (*out_pos < out_size)
+-					continue;
+-			}
+-
+-			if (ret != LZMA_OK) {
+-				// coder->thread_error was set or
+-				// lzma_index_append() failed.
+-				threads_stop(coder, false);
+-				return ret;
+-			}
+-
+-			// Check if the last Block was finished.
+-			if (action == LZMA_FINISH
+-					&& *in_pos == in_size
+-					&& lzma_outq_is_empty(
+-						&coder->outq))
+-				break;
+-
+-			// Try to give uncompressed data to a worker thread.
+-			ret = stream_encode_in(coder, allocator,
+-					in, in_pos, in_size, action);
+-			if (ret != LZMA_OK) {
+-				threads_stop(coder, false);
+-				return ret;
+-			}
+-
+-			// Return if
+-			//  - we have used all the input and expect to
+-			//    get more input; or
+-			//  - the output buffer has been filled.
+-			//
+-			// TODO: Support flushing.
+-			if ((*in_pos == in_size && action != LZMA_FINISH)
+-					|| *out_pos == out_size)
+-				return LZMA_OK;
+-
+-			// Neither in nor out has been used completely.
+-			// Wait until there's something we can do.
+-			if (wait_for_work(coder, &wait_abs, &has_blocked,
+-					*in_pos < in_size))
+-				return LZMA_TIMED_OUT;
+-		}
+-
+-		// All Blocks have been encoded and the threads have stopped.
+-		// Prepare to encode the Index field.
+-		return_if_error(lzma_index_encoder_init(
+-				&coder->index_encoder, allocator,
+-				coder->index));
+-		coder->sequence = SEQ_INDEX;
+-	}
+-
+-	// Fall through
+-
+-	case SEQ_INDEX: {
+-		// Call the Index encoder. It doesn't take any input, so
+-		// those pointers can be NULL.
+-		const lzma_ret ret = coder->index_encoder.code(
+-				coder->index_encoder.coder, allocator,
+-				NULL, NULL, 0,
+-				out, out_pos, out_size, LZMA_RUN);
+-		if (ret != LZMA_STREAM_END)
+-			return ret;
+-
+-		// Encode the Stream Footer into coder->buffer.
+-		coder->stream_flags.backward_size
+-				= lzma_index_size(coder->index);
+-		if (lzma_stream_footer_encode(&coder->stream_flags,
+-				coder->header) != LZMA_OK)
+-			return LZMA_PROG_ERROR;
+-
+-		coder->sequence = SEQ_STREAM_FOOTER;
+-	}
+-
+-	// Fall through
+-
+-	case SEQ_STREAM_FOOTER:
+-		lzma_bufcpy(coder->header, &coder->header_pos,
+-				sizeof(coder->header),
+-				out, out_pos, out_size);
+-		return coder->header_pos < sizeof(coder->header)
+-				? LZMA_OK : LZMA_STREAM_END;
+-	}
+-
+-	assert(0);
+-	return LZMA_PROG_ERROR;
+-}
+-
+-
+-static void
+-stream_encoder_mt_end(lzma_coder *coder, lzma_allocator *allocator)
+-{
+-	// Threads must be killed before the output queue can be freed.
+-	threads_end(coder, allocator);
+-	lzma_outq_end(&coder->outq, allocator);
+-
+-	for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+-		lzma_free(coder->filters[i].options, allocator);
+-
+-	lzma_next_end(&coder->index_encoder, allocator);
+-	lzma_index_end(coder->index, allocator);
+-
+-	mythread_cond_destroy(&coder->cond);
+-	pthread_mutex_destroy(&coder->mutex);
+-
+-	lzma_free(coder, allocator);
+-	return;
+-}
+-
+-
+-/// Options handling for lzma_stream_encoder_mt_init() and
+-/// lzma_stream_encoder_mt_memusage()
+-static lzma_ret
+-get_options(const lzma_mt *options, lzma_options_easy *opt_easy,
+-		const lzma_filter **filters, uint64_t *block_size,
+-		uint64_t *outbuf_size_max)
+-{
+-	// Validate some of the options.
+-	if (options == NULL)
+-		return LZMA_PROG_ERROR;
+-
+-	if (options->flags != 0 || options->threads == 0
+-			|| options->threads > LZMA_THREADS_MAX)
+-		return LZMA_OPTIONS_ERROR;
+-
+-	if (options->filters != NULL) {
+-		// Filter chain was given, use it as is.
+-		*filters = options->filters;
+-	} else {
+-		// Use a preset.
+-		if (lzma_easy_preset(opt_easy, options->preset))
+-			return LZMA_OPTIONS_ERROR;
+-
+-		*filters = opt_easy->filters;
+-	}
+-
+-	// Block size
+-	if (options->block_size > 0) {
+-		if (options->block_size > BLOCK_SIZE_MAX)
+-			return LZMA_OPTIONS_ERROR;
+-
+-		*block_size = options->block_size;
+-	} else {
+-		// Determine the Block size from the filter chain.
+-		*block_size = lzma_mt_block_size(*filters);
+-		if (*block_size == 0)
+-			return LZMA_OPTIONS_ERROR;
+-
+-		assert(*block_size <= BLOCK_SIZE_MAX);
+-	}
+-
+-	// Calculate the maximum amount output that a single output buffer
+-	// may need to hold. This is the same as the maximum total size of
+-	// a Block.
+-	//
+-	// FIXME: As long as the encoder keeps the whole input buffer
+-	// available and doesn't start writing output before finishing
+-	// the Block, it could use lzma_stream_buffer_bound() and use
+-	// uncompressed LZMA2 chunks if the data doesn't compress.
+-	*outbuf_size_max = *block_size + *block_size / 16 + 16384;
+-
+-	return LZMA_OK;
+-}
+-
+-
+-static lzma_ret
+-stream_encoder_mt_init(lzma_next_coder *next, lzma_allocator *allocator,
+-		const lzma_mt *options)
+-{
+-	lzma_next_coder_init(&stream_encoder_mt_init, next, allocator);
+-
+-	// Get the filter chain.
+-	lzma_options_easy easy;
+-	const lzma_filter *filters;
+-	uint64_t block_size;
+-	uint64_t outbuf_size_max;
+-	return_if_error(get_options(options, &easy, &filters,
+-			&block_size, &outbuf_size_max));
+-
+-#if SIZE_MAX < UINT64_MAX
+-	if (block_size > SIZE_MAX)
+-		return LZMA_MEM_ERROR;
+-#endif
+-
+-	// FIXME TODO: Validate the filter chain so that we can give
+-	// an error in this function instead of delaying it to the first
+-	// call to lzma_code().
+-
+-	// Validate the Check ID.
+-	if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
+-		return LZMA_PROG_ERROR;
+-
+-	if (!lzma_check_is_supported(options->check))
+-		return LZMA_UNSUPPORTED_CHECK;
+-
+-	// Allocate and initialize the base structure if needed.
+-	if (next->coder == NULL) {
+-		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
+-		if (next->coder == NULL)
+-			return LZMA_MEM_ERROR;
+-
+-		// For the mutex and condition variable initializations
+-		// the error handling has to be done here because
+-		// stream_encoder_mt_end() doesn't know if they have
+-		// already been initialized or not.
+-		if (pthread_mutex_init(&next->coder->mutex, NULL)) {
+-			lzma_free(next->coder, allocator);
+-			next->coder = NULL;
+-			return LZMA_MEM_ERROR;
+-		}
+-
+-		if (mythread_cond_init(&next->coder->cond)) {
+-			pthread_mutex_destroy(&next->coder->mutex);
+-			lzma_free(next->coder, allocator);
+-			next->coder = NULL;
+-			return LZMA_MEM_ERROR;
+-		}
+-
+-		next->code = &stream_encode_mt;
+-		next->end = &stream_encoder_mt_end;
+-// 		next->update = &stream_encoder_mt_update;
+-
+-		next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
+-		next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
+-		next->coder->index = NULL;
+-		memzero(&next->coder->outq, sizeof(next->coder->outq));
+-		next->coder->threads = NULL;
+-		next->coder->threads_max = 0;
+-		next->coder->threads_initialized = 0;
+-	}
+-
+-	// Basic initializations
+-	next->coder->sequence = SEQ_STREAM_HEADER;
+-	next->coder->block_size = (size_t)(block_size);
+-	next->coder->thread_error = LZMA_OK;
+-	next->coder->thr = NULL;
+-
+-	// Allocate the thread-specific base structures.
+-	assert(options->threads > 0);
+-	if (next->coder->threads_max != options->threads) {
+-		threads_end(next->coder, allocator);
+-
+-		next->coder->threads = NULL;
+-		next->coder->threads_max = 0;
+-
+-		next->coder->threads_initialized = 0;
+-		next->coder->threads_free = NULL;
+-
+-		next->coder->threads = lzma_alloc(
+-				options->threads * sizeof(worker_thread),
+-				allocator);
+-		if (next->coder->threads == NULL)
+-			return LZMA_MEM_ERROR;
+-
+-		next->coder->threads_max = options->threads;
+-	} else {
+-		// Reuse the old structures and threads. Tell the running
+-		// threads to stop and wait until they have stopped.
+-		threads_stop(next->coder, true);
+-	}
+-
+-	// Output queue
+-	return_if_error(lzma_outq_init(&next->coder->outq, allocator,
+-			outbuf_size_max, options->threads));
+-
+-	// Timeout
+-	if (options->timeout > 0) {
+-		next->coder->wait_max.tv_sec = options->timeout / 1000;
+-		next->coder->wait_max.tv_nsec
+-				= (options->timeout % 1000) * 1000000L;
+-		next->coder->has_timeout = true;
+-	} else {
+-		next->coder->has_timeout = false;
+-	}
+-
+-	// Free the old filter chain and copy the new one.
+-	for (size_t i = 0; next->coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+-		lzma_free(next->coder->filters[i].options, allocator);
+-
+-	return_if_error(lzma_filters_copy(options->filters,
+-			next->coder->filters, allocator));
+-
+-	// Index
+-	lzma_index_end(next->coder->index, allocator);
+-	next->coder->index = lzma_index_init(allocator);
+-	if (next->coder->index == NULL)
+-		return LZMA_MEM_ERROR;
+-
+-	// Stream Header
+-	next->coder->stream_flags.version = 0;
+-	next->coder->stream_flags.check = options->check;
+-	return_if_error(lzma_stream_header_encode(
+-			&next->coder->stream_flags, next->coder->header));
+-
+-	next->coder->header_pos = 0;
+-
+-	return LZMA_OK;
+-}
+-
+-
+-extern LZMA_API(lzma_ret)
+-lzma_stream_encoder_mt(lzma_stream *strm, const lzma_mt *options)
+-{
+-	lzma_next_strm_init(stream_encoder_mt_init, strm, options);
+-
+-	strm->internal->supported_actions[LZMA_RUN] = true;
+-// 	strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
+-// 	strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
+-// 	strm->internal->supported_actions[LZMA_FULL_BARRIER] = true;
+-	strm->internal->supported_actions[LZMA_FINISH] = true;
+-
+-	return LZMA_OK;
+-}
+-
+-
+-// This function name is a monster but it's consistent with the older
+-// monster names. :-( 31 chars is the max that C99 requires so in that
+-// sense it's not too long. ;-)
+-extern LZMA_API(uint64_t)
+-lzma_stream_encoder_mt_memusage(const lzma_mt *options)
+-{
+-	lzma_options_easy easy;
+-	const lzma_filter *filters;
+-	uint64_t block_size;
+-	uint64_t outbuf_size_max;
+-
+-	if (get_options(options, &easy, &filters, &block_size,
+-			&outbuf_size_max) != LZMA_OK)
+-		return UINT64_MAX;
+-
+-	// Memory usage of the input buffers
+-	const uint64_t inbuf_memusage = options->threads * block_size;
+-
+-	// Memory usage of the filter encoders
+-	uint64_t filters_memusage
+-			= lzma_raw_encoder_memusage(options->filters);
+-	if (filters_memusage == UINT64_MAX)
+-		return UINT64_MAX;
+-
+-	filters_memusage *= options->threads;
+-
+-	// Memory usage of the output queue
+-	const uint64_t outq_memusage = lzma_outq_memusage(
+-			outbuf_size_max, options->threads);
+-	if (outq_memusage == UINT64_MAX)
+-		return UINT64_MAX;
+-
+-	// Sum them with overflow checking.
+-	uint64_t total_memusage = LZMA_MEMUSAGE_BASE + sizeof(lzma_coder)
+-			+ options->threads * sizeof(worker_thread);
+-
+-	if (UINT64_MAX - total_memusage < inbuf_memusage)
+-		return UINT64_MAX;
+-
+-	total_memusage += inbuf_memusage;
+-
+-	if (UINT64_MAX - total_memusage < filters_memusage)
+-		return UINT64_MAX;
+-
+-	total_memusage += filters_memusage;
+-
+-	if (UINT64_MAX - total_memusage < outq_memusage)
+-		return UINT64_MAX;
+-
+-	return total_memusage + outq_memusage;
+-}
+diff --git a/src/xz/args.c b/src/xz/args.c
+index dea93c5..f207e7f 100644
+--- a/src/xz/args.c
++++ b/src/xz/args.c
+@@ -179,9 +179,8 @@ parse_real(args_info *args, int argc, char **argv)
+ 			break;
+ 
+ 		case 'T':
+-			// The max is from src/liblzma/common/common.h.
+-			hardware_threads_set(str_to_uint64("threads",
+-					optarg, 0, 16384));
++			hardware_threadlimit_set(str_to_uint64(
++					"threads", optarg, 0, UINT32_MAX));
+ 			break;
+ 
+ 		// --version
+diff --git a/src/xz/coder.c b/src/xz/coder.c
+index 41193a7..4139da4 100644
+--- a/src/xz/coder.c
++++ b/src/xz/coder.c
+@@ -55,14 +55,6 @@ static lzma_check check;
+ /// This becomes false if the --check=CHECK option is used.
+ static bool check_default = true;
+ 
+-#ifdef HAVE_PTHREAD
+-static lzma_mt mt_options = {
+-	.flags = 0,
+-	.timeout = 300,
+-	.filters = filters,
+-};
+-#endif
+-
+ 
+ extern void
+ coder_set_check(lzma_check new_check)
+@@ -125,15 +117,6 @@ memlimit_too_small(uint64_t memory_usage)
+ extern void
+ coder_set_compression_settings(void)
+ {
+-	// The default check type is CRC64, but fallback to CRC32
+-	// if CRC64 isn't supported by the copy of liblzma we are
+-	// using. CRC32 is always supported.
+-	if (check_default) {
+-		check = LZMA_CHECK_CRC64;
+-		if (!lzma_check_is_supported(check))
+-			check = LZMA_CHECK_CRC32;
+-	}
+-
+ 	// Options for LZMA1 or LZMA2 in case we are using a preset.
+ 	static lzma_options_lzma opt_lzma;
+ 
+@@ -187,30 +170,15 @@ coder_set_compression_settings(void)
+ 	// Print the selected filter chain.
+ 	message_filters_show(V_DEBUG, filters);
+ 
+-	// Get the memory usage. Note that if --format=raw was used,
+-	// we can be decompressing.
++	// If using --format=raw, we can be decoding. The memusage function
++	// also validates the filter chain and the options used for the
++	// filters.
+ 	const uint64_t memory_limit = hardware_memlimit_get(opt_mode);
+ 	uint64_t memory_usage;
+-	if (opt_mode == MODE_COMPRESS) {
+-#ifdef HAVE_PTHREAD
+-		if (opt_format == FORMAT_XZ && hardware_threads_get() > 1) {
+-			mt_options.threads = hardware_threads_get();
+-			mt_options.block_size = opt_block_size;
+-			mt_options.check = check;
+-			memory_usage = lzma_stream_encoder_mt_memusage(
+-					&mt_options);
+-			if (memory_usage != UINT64_MAX)
+-				message(V_DEBUG, _("Using up to %" PRIu32
+-						" threads."),
+-						mt_options.threads);
+-		} else
+-#endif
+-		{
+-			memory_usage = lzma_raw_encoder_memusage(filters);
+-		}
+-	} else {
++	if (opt_mode == MODE_COMPRESS)
++		memory_usage = lzma_raw_encoder_memusage(filters);
++	else
+ 		memory_usage = lzma_raw_decoder_memusage(filters);
+-	}
+ 
+ 	if (memory_usage == UINT64_MAX)
+ 		message_fatal(_("Unsupported filter chain or filter options"));
+@@ -226,99 +194,90 @@ coder_set_compression_settings(void)
+ 						round_up_to_mib(decmem), 0));
+ 	}
+ 
+-	if (memory_usage <= memory_limit)
+-		return;
++	if (memory_usage > memory_limit) {
++		// If --no-auto-adjust was used or we didn't find LZMA1 or
++		// LZMA2 as the last filter, give an error immediately.
++		// --format=raw implies --no-auto-adjust.
++		if (!opt_auto_adjust || opt_format == FORMAT_RAW)
++			memlimit_too_small(memory_usage);
+ 
+-	// If --no-auto-adjust was used or we didn't find LZMA1 or
+-	// LZMA2 as the last filter, give an error immediately.
+-	// --format=raw implies --no-auto-adjust.
+-	if (!opt_auto_adjust || opt_format == FORMAT_RAW)
+-		memlimit_too_small(memory_usage);
++		assert(opt_mode == MODE_COMPRESS);
+ 
+-	assert(opt_mode == MODE_COMPRESS);
++		// Look for the last filter if it is LZMA2 or LZMA1, so
++		// we can make it use less RAM. With other filters we don't
++		// know what to do.
++		size_t i = 0;
++		while (filters[i].id != LZMA_FILTER_LZMA2
++				&& filters[i].id != LZMA_FILTER_LZMA1) {
++			if (filters[i].id == LZMA_VLI_UNKNOWN)
++				memlimit_too_small(memory_usage);
++
++			++i;
++		}
+ 
+-#ifdef HAVE_PTHREAD
+-	if (opt_format == FORMAT_XZ && mt_options.threads > 1) {
+-		// Try to reduce the number of threads before
+-		// adjusting the compression settings down.
+-		do {
+-			// FIXME? The real single-threaded mode has
+-			// lower memory usage, but it's not comparable
+-			// because it doesn't write the size info
+-			// into Block Headers.
+-			if (--mt_options.threads == 0)
++		// Decrease the dictionary size until we meet the memory
++		// usage limit. First round down to full mebibytes.
++		lzma_options_lzma *opt = filters[i].options;
++		const uint32_t orig_dict_size = opt->dict_size;
++		opt->dict_size &= ~((UINT32_C(1) << 20) - 1);
++		while (true) {
++			// If it is below 1 MiB, auto-adjusting failed. We
++			// could be more sophisticated and scale it down even
++			// more, but let's see if many complain about this
++			// version.
++			//
++			// FIXME: Displays the scaled memory usage instead
++			// of the original.
++			if (opt->dict_size < (UINT32_C(1) << 20))
+ 				memlimit_too_small(memory_usage);
+ 
+-			memory_usage = lzma_stream_encoder_mt_memusage(
+-					&mt_options);
++			memory_usage = lzma_raw_encoder_memusage(filters);
+ 			if (memory_usage == UINT64_MAX)
+ 				message_bug();
+ 
+-		} while (memory_usage > memory_limit);
+-
+-		message(V_WARNING, _("Adjusted the number of threads "
+-			"from %s to %s to not exceed "
+-			"the memory usage limit of %s MiB"),
+-			uint64_to_str(hardware_threads_get(), 0),
+-			uint64_to_str(mt_options.threads, 1),
+-			uint64_to_str(round_up_to_mib(
+-				memory_limit), 2));
++			// Accept it if it is low enough.
++			if (memory_usage <= memory_limit)
++				break;
++
++			// Otherwise 1 MiB down and try again. I hope this
++			// isn't too slow method for cases where the original
++			// dict_size is very big.
++			opt->dict_size -= UINT32_C(1) << 20;
++		}
++
++		// Tell the user that we decreased the dictionary size.
++		message(V_WARNING, _("Adjusted LZMA%c dictionary size "
++				"from %s MiB to %s MiB to not exceed "
++				"the memory usage limit of %s MiB"),
++				filters[i].id == LZMA_FILTER_LZMA2
++					? '2' : '1',
++				uint64_to_str(orig_dict_size >> 20, 0),
++				uint64_to_str(opt->dict_size >> 20, 1),
++				uint64_to_str(round_up_to_mib(
++					memory_limit), 2));
+ 	}
+-#endif
+-
+-	if (memory_usage <= memory_limit)
+-		return;
+-
+-	// Look for the last filter if it is LZMA2 or LZMA1, so we can make
+-	// it use less RAM. With other filters we don't know what to do.
+-	size_t i = 0;
+-	while (filters[i].id != LZMA_FILTER_LZMA2
+-			&& filters[i].id != LZMA_FILTER_LZMA1) {
+-		if (filters[i].id == LZMA_VLI_UNKNOWN)
+-			memlimit_too_small(memory_usage);
+ 
+-		++i;
++/*
++	// Limit the number of worker threads so that memory usage
++	// limit isn't exceeded.
++	assert(memory_usage > 0);
++	size_t thread_limit = memory_limit / memory_usage;
++	if (thread_limit == 0)
++		thread_limit = 1;
++
++	if (opt_threads > thread_limit)
++		opt_threads = thread_limit;
++*/
++
++	if (check_default) {
++		// The default check type is CRC64, but fallback to CRC32
++		// if CRC64 isn't supported by the copy of liblzma we are
++		// using. CRC32 is always supported.
++		check = LZMA_CHECK_CRC64;
++		if (!lzma_check_is_supported(check))
++			check = LZMA_CHECK_CRC32;
+ 	}
+ 
+-	// Decrease the dictionary size until we meet the memory
+-	// usage limit. First round down to full mebibytes.
+-	lzma_options_lzma *opt = filters[i].options;
+-	const uint32_t orig_dict_size = opt->dict_size;
+-	opt->dict_size &= ~((UINT32_C(1) << 20) - 1);
+-	while (true) {
+-		// If it is below 1 MiB, auto-adjusting failed. We could be
+-		// more sophisticated and scale it down even more, but let's
+-		// see if many complain about this version.
+-		//
+-		// FIXME: Displays the scaled memory usage instead
+-		// of the original.
+-		if (opt->dict_size < (UINT32_C(1) << 20))
+-			memlimit_too_small(memory_usage);
+-
+-		memory_usage = lzma_raw_encoder_memusage(filters);
+-		if (memory_usage == UINT64_MAX)
+-			message_bug();
+-
+-		// Accept it if it is low enough.
+-		if (memory_usage <= memory_limit)
+-			break;
+-
+-		// Otherwise 1 MiB down and try again. I hope this
+-		// isn't too slow method for cases where the original
+-		// dict_size is very big.
+-		opt->dict_size -= UINT32_C(1) << 20;
+-	}
+-
+-	// Tell the user that we decreased the dictionary size.
+-	message(V_WARNING, _("Adjusted LZMA%c dictionary size "
+-			"from %s MiB to %s MiB to not exceed "
+-			"the memory usage limit of %s MiB"),
+-			filters[i].id == LZMA_FILTER_LZMA2
+-				? '2' : '1',
+-			uint64_to_str(orig_dict_size >> 20, 0),
+-			uint64_to_str(opt->dict_size >> 20, 1),
+-			uint64_to_str(round_up_to_mib(memory_limit), 2));
+-
+ 	return;
+ }
+ 
+@@ -400,14 +359,7 @@ coder_init(file_pair *pair)
+ 			break;
+ 
+ 		case FORMAT_XZ:
+-#ifdef HAVE_PTHREAD
+-			if (hardware_threads_get() > 1)
+-				ret = lzma_stream_encoder_mt(
+-						&strm, &mt_options);
+-			else
+-#endif
+-				ret = lzma_stream_encoder(
+-						&strm, filters, check);
++			ret = lzma_stream_encoder(&strm, filters, check);
+ 			break;
+ 
+ 		case FORMAT_LZMA:
+@@ -528,8 +480,8 @@ coder_normal(file_pair *pair)
+ 	// to the .xz format. If block_remaining == UINT64_MAX, only
+ 	// a single block is created.
+ 	uint64_t block_remaining = UINT64_MAX;
+-	if (hardware_threads_get() == 1 && opt_mode == MODE_COMPRESS
+-			&& opt_format == FORMAT_XZ && opt_block_size > 0)
++	if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_XZ
++			&& opt_block_size > 0)
+ 		block_remaining = opt_block_size;
+ 
+ 	strm.next_out = out_buf.u8;
+diff --git a/src/xz/hardware.c b/src/xz/hardware.c
+index 925926c..a4733c2 100644
+--- a/src/xz/hardware.c
++++ b/src/xz/hardware.c
+@@ -14,9 +14,9 @@
+ #include "tuklib_cpucores.h"
+ 
+ 
+-/// Maximum number of worker threads. This can be set with
++/// Maximum number of free *coder* threads. This can be set with
+ /// the --threads=NUM command line option.
+-static uint32_t threads_max = 1;
++static uint32_t threadlimit;
+ 
+ /// Memory usage limit for compression
+ static uint64_t memlimit_compress;
+@@ -29,16 +29,15 @@ static uint64_t total_ram;
+ 
+ 
+ extern void
+-hardware_threads_set(uint32_t n)
++hardware_threadlimit_set(uint32_t new_threadlimit)
+ {
+-	if (n == 0) {
+-		// Automatic number of threads was requested.
+-		// Use the number of available CPU cores.
+-		threads_max = tuklib_cpucores();
+-		if (threads_max == 0)
+-			threads_max = 1;
++	if (new_threadlimit == 0) {
++		// The default is the number of available CPU cores.
++		threadlimit = tuklib_cpucores();
++		if (threadlimit == 0)
++			threadlimit = 1;
+ 	} else {
+-		threads_max = n;
++		threadlimit = new_threadlimit;
+ 	}
+ 
+ 	return;
+@@ -46,9 +45,9 @@ hardware_threads_set(uint32_t n)
+ 
+ 
+ extern uint32_t
+-hardware_threads_get(void)
++hardware_threadlimit_get(void)
+ {
+-	return threads_max;
++	return threadlimit;
+ }
+ 
+ 
+@@ -140,5 +139,6 @@ hardware_init(void)
+ 
+ 	// Set the defaults.
+ 	hardware_memlimit_set(0, true, true, false);
++	hardware_threadlimit_set(0);
+ 	return;
+ }
+diff --git a/src/xz/hardware.h b/src/xz/hardware.h
+index 4fae618..ad526f2 100644
+--- a/src/xz/hardware.h
++++ b/src/xz/hardware.h
+@@ -15,11 +15,12 @@
+ extern void hardware_init(void);
+ 
+ 
+-/// Set the maximum number of worker threads.
+-extern void hardware_threads_set(uint32_t threadlimit);
++/// Set custom value for maximum number of coder threads.
++extern void hardware_threadlimit_set(uint32_t threadlimit);
+ 
+-/// Get the maximum number of worker threads.
+-extern uint32_t hardware_threads_get(void);
++/// Get the maximum number of coder threads. Some additional helper threads
++/// are allowed on top of this).
++extern uint32_t hardware_threadlimit_get(void);
+ 
+ 
+ /// Set the memory usage limit. There are separate limits for compression
+diff --git a/src/xz/private.h b/src/xz/private.h
+index 978f81a..6b01e51 100644
+--- a/src/xz/private.h
++++ b/src/xz/private.h
+@@ -12,8 +12,6 @@
+ 
+ #include "sysdefs.h"
+ #include "mythread.h"
+-
+-#define LZMA_UNSTABLE
+ #include "lzma.h"
+ 
+ #include <sys/types.h>
+diff --git a/src/xz/xz.1 b/src/xz/xz.1
+index f4680f4..0329128 100644
+--- a/src/xz/xz.1
++++ b/src/xz/xz.1
+@@ -5,7 +5,7 @@
+ .\" This file has been put into the public domain.
+ .\" You can do whatever you want with this file.
+ .\"
+-.TH XZ 1 "2011-04-12" "Tukaani" "XZ Utils"
++.TH XZ 1 "2011-04-11" "Tukaani" "XZ Utils"
+ .
+ .SH NAME
+ xz, unxz, xzcat, lzma, unlzma, lzcat \- Compress or decompress .xz and .lzma files
+@@ -907,30 +907,24 @@ Automatic adjusting is always disabled when creating raw streams
+ .TP
+ \fB\-T\fR \fIthreads\fR, \fB\-\-threads=\fIthreads
+ Specify the number of worker threads to use.
+-Setting
+-.I threads
+-to a special value
+-.B 0
+-makes
+-.B xz
+-use as many threads as there are CPU cores on the system.
+ The actual number of threads can be less than
+ .I threads
+-if the input file is not big enough
+-for threading with the given settings or
+ if using more threads would exceed the memory usage limit.
+ .IP ""
+-Currently the only threading method is to split the input into
+-blocks and compress them independently from each other.
+-The default block size depends on the compression level and
+-can be overriden with the
+-.BI \-\-block\-size= size
+-option.
++.B "Multithreaded compression and decompression are not"
++.B "implemented yet, so this option has no effect for now."
+ .IP ""
+-.B "It is possible that the details of this option change before"
+-.B "the next stable XZ Utils release."
+-.B "This may include the meaning of the special value 0."
+-.\" FIXME
++.B "As of writing (2010-09-27), it hasn't been decided"
++.B "if threads will be used by default on multicore systems"
++.B "once support for threading has been implemented."
++.B "Comments are welcome."
++The complicating factor is that using many threads
++will increase the memory usage dramatically.
++Note that if multithreading will be the default,
++it will probably be done so that single-threaded and
++multithreaded modes produce the same output,
++so compression ratio won't be significantly affected
++if threading will be enabled by default.
+ .
+ .SS "Custom compressor filter chains"
+ A custom filter chain allows specifying
+-- 
+1.7.6
+
diff --git a/debian/patches/abi-version-script b/debian/patches/abi-version-script
new file mode 100644
index 0000000..2c5a2c4
--- /dev/null
+++ b/debian/patches/abi-version-script
@@ -0,0 +1,44 @@
+From: Jonathan Nieder <jrnieder@gmail.com>
+Date: Sat, 11 Jun 2011 23:33:43 -0500
+Subject: liblzma: Remove XZ_5.1.1alpha version symbol
+
+Now that the lzma_stream_encoder_mt{,_memusage} symbols are gone on
+this branch, liblzma should stop pretending to satisfy dependencies on
+XZ_5.1.1alpha.
+
+After this change, programs relying on those symbols will error out
+immediately at startup like they are supposed to:
+
+	app: liblzma.so.5: version `XZ_5.1.1alpha' not found (required by app)
+
+And your scripts that look for version definition entries with
+readelf -s (like RPM’s find-provides) can tell that this copy of
+liblzma lacks support for multithreaded encoding.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ src/liblzma/liblzma.map |    8 +-------
+ 1 files changed, 1 insertions(+), 7 deletions(-)
+
+diff --git a/src/liblzma/liblzma.map b/src/liblzma/liblzma.map
+index 835eb26..47a7c22 100644
+--- a/src/liblzma/liblzma.map
++++ b/src/liblzma/liblzma.map
+@@ -93,13 +93,7 @@ global:
+ 	lzma_vli_decode;
+ 	lzma_vli_encode;
+ 	lzma_vli_size;
+-};
+-
+-XZ_5.1.1alpha {
+-global:
+-	lzma_stream_encoder_mt;
+-	lzma_stream_encoder_mt_memusage;
+ 
+ local:
+ 	*;
+-} XZ_5.0;
++};
+-- 
+1.7.7
+
diff --git a/debian/patches/configure-liblzma2-compat b/debian/patches/configure-liblzma2-compat
new file mode 100644
index 0000000..46a9233
--- /dev/null
+++ b/debian/patches/configure-liblzma2-compat
@@ -0,0 +1,195 @@
+From: Jonathan Nieder <jrnieder@gmail.com>
+Date: Sat, 16 Jun 2012 05:57:42 -0500
+Subject: liblzma: make dlopen()-based liblzma2 compatibility optional
+
+Suppose I want to build a statically linked program:
+
+	gcc -static -o app app.c -lrpm -llzma
+
+Suppose further that librpm.a was built against a pre-5.0 version of
+liblzma so it does not allocate as much space for reserved fields at
+the end of lzma_stream as the current API requires.
+
+(This is a hypothetical scenario --- Debian librpm does not provide a
+static library.)
+
+If liblzma uses unpatched lzma_code() from XZ Utils >= 5.0, then
+during calls to librpm that try to compress or decompress an
+xz-compressed RPM, lzma_code’s reserved field checks will overflow the
+buffer and segfault.
+
+If liblzma uses the modified version of lzma_code() which asks libdl
+if liblzma.so.2 is resident and refrains from checking reserved fields
+past the end of the old lzma_stream struct when the answer is "yes",
+the behavior is no better.  The dynamic library liblzma.so.2 is _not_
+resident, so lzma_code() dutifully reads reserved fields past the end
+of the buffer --- segfault.
+
+So the only safe behavior in the static library is to unconditionally
+disable checks that might break for callers we want to continue to
+support.
+
+The new "./configure --enable-liblzma2-compat" option implements all
+three sets of semantics:
+
+ - "./configure --disable-liblzma2-compat" means to check the full set
+   of reserved fields unconditionally.  You can use this to check how
+   your application would behave with the unpatched library.
+
+ - "./configure --enable-liblzma2-compat=auto" means to skip checks of
+   reserved fields past the old end of struct lzma_stream when
+   liblzma.so.2 is resident.  If a DSO built against liblzma2 shares
+   the process image, the ABI-incompatible checks are skipped for
+   safety, whereas in the usual case when no such DSO is resident, the
+   full set of checks is run to help application developers remember
+   to zero all reserved fields.
+
+ - "./configure --enable-liblzma2-compat" makes liblzma skip the
+   ABI-incompatible checks unconditionallty.  You can use this if you
+   want your copy of liblzma to be usable by static libraries that
+   were built against the old library.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ configure.ac                |   33 +++++++++++++++++++++++++++++++--
+ src/liblzma/common/common.c |   40 +++++++++++++++++++++++++++++++++++-----
+ src/liblzma/common/common.h |    2 ++
+ 3 files changed, 68 insertions(+), 7 deletions(-)
+
+diff --git a/configure.ac b/configure.ac
+index cbf92659..eb510fd9 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -468,10 +468,39 @@ if test "x$enable_threads" = xyes; then
+ 	CFLAGS=$OLD_CFLAGS
+ fi
+ 
+-# As a Debian-specific hack, liblzma uses dlopen() to check if extra
++# As a Debian-specific hack, liblzma can use dlopen() to check if extra
+ # paranoia is needed because unversioned symbols from liblzma.so.2 are
+ # present in the same process.  See src/liblzma/common/common.c.
+-AC_SEARCH_LIBS([dlopen], [dl])
++AC_MSG_CHECKING([if lzma_code checks should be relaxed for compatibility])
++AC_ARG_ENABLE([liblzma2-compat], [AC_HELP_STRING([--enable-liblzma2-compat],
++		[Relax run-time checks to accomodate old binaries built
++		with smaller sizeof(lzma_stream).  The default is "dynamic",
++		which means to only use the relaxed checks when the dynamic
++		loader reports that liblzma.so.2 is loaded in the same process.])],
++	[], [enable_liblzma2_compat=dynamic])
++case $enable_liblzma2_compat in
++dynamic)
++	AC_SEARCH_LIBS([dlopen], [dl])
++	AC_DEFINE([LIBLZMA2_COMPAT_DYNAMIC], [1],
++		[Define to 1 to use dlopen() to check if lzma_code() checks
++		should be more tolerant because the process is also linked to
++		liblzma from Debian 6.0.])
++	AC_MSG_RESULT([auto])
++	;;
++yes)
++	AC_DEFINE([LIBLZMA2_COMPAT], [1],
++		[Define to 1 to unconditionally make lzma_code() checks tolerant
++		to accomodate callers built against liblzma from Debian 6.0.])
++	AC_MSG_RESULT([yes])
++	;;
++no)
++	AC_MSG_RESULT([no])
++	;;
++*)
++	AC_MSG_RESULT([])
++	AC_MSG_ERROR([--enable-liblzma2: unrecognized value $enable_liblzma2_compat])
++	;;
++esac
+ 
+ echo
+ echo "Initializing Libtool:"
+diff --git a/src/liblzma/common/common.c b/src/liblzma/common/common.c
+index e61d940d..3bfdb755 100644
+--- a/src/liblzma/common/common.c
++++ b/src/liblzma/common/common.c
+@@ -143,16 +143,46 @@ lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
+ // External to internal API wrapper //
+ //////////////////////////////////////
+ 
+-static bool
+-liblzma2_loaded(void)
++#ifdef LIBLZMA2_COMPAT_DYNAMIC
++
++static void
++init_liblzma2_compat(lzma_stream *strm)
+ {
+ 	void *handle = dlopen("liblzma.so.2", RTLD_LAZY | RTLD_NOLOAD);
+ 	if (handle) {
+ 		dlclose(handle);
+-		return true;
++		strm->internal->liblzma2_compat = true;
++		return;
+ 	}
++	strm->internal->liblzma2_compat = false;
++}
++
++static bool
++liblzma2_loaded(lzma_stream *strm)
++{
++	return strm->internal->liblzma2_compat;
++}
++
++#else
++
++static void
++init_liblzma2_compat(lzma_stream *strm)
++{
++}
++
++#ifdef LIBLZMA2_COMPAT
++static bool liblzma2_loaded(lzma_stream *strm)
++{
++	return true;
++}
++#else
++static bool liblzma2_loaded(lzma_stream *strm)
++{
+ 	return false;
+ }
++#endif
++
++#endif
+ 
+ extern lzma_ret
+ lzma_strm_init(lzma_stream *strm)
+@@ -167,7 +197,7 @@ lzma_strm_init(lzma_stream *strm)
+ 			return LZMA_MEM_ERROR;
+ 
+ 		strm->internal->next = LZMA_NEXT_CODER_INIT;
+-		strm->internal->liblzma2_compat = liblzma2_loaded();
++		init_liblzma2_compat(strm);
+ 	}
+ 
+ 	memzero(strm->internal->supported_actions,
+@@ -220,7 +250,7 @@ lzma_code(lzma_stream *strm, lzma_action action)
+ 			|| strm->reserved_ptr4 != NULL)
+ 		return LZMA_OPTIONS_ERROR;
+ 
+-	if (strm->internal->liblzma2_compat)
++	if (liblzma2_loaded(strm))
+ 		; /* Enough checks. */
+ 	else if (strm->reserved_int1 != 0
+ 			|| strm->reserved_int2 != 0
+diff --git a/src/liblzma/common/common.h b/src/liblzma/common/common.h
+index 475661d8..4081c2d3 100644
+--- a/src/liblzma/common/common.h
++++ b/src/liblzma/common/common.h
+@@ -201,9 +201,11 @@ struct lzma_internal_s {
+ 	/// made (no input consumed and no output produced by next.code).
+ 	bool allow_buf_error;
+ 
++#ifdef LIBLZMA2_COMPAT_DYNAMIC
+ 	/// Indicates whether we are sharing a process image with
+ 	/// liblzma.so.2 and need to tread carefully.
+ 	bool liblzma2_compat;
++#endif
+ };
+ 
+ 
+-- 
+1.7.10.4
+
diff --git a/debian/patches/decoder-check-first-0x00 b/debian/patches/decoder-check-first-0x00
new file mode 100644
index 0000000..442e318
--- /dev/null
+++ b/debian/patches/decoder-check-first-0x00
@@ -0,0 +1,69 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Thu, 28 Jun 2012 10:47:49 +0300
+Subject: liblzma: Check that the first byte of range encoded data is 0x00.
+
+It is just to be more pedantic and thus perhaps catch broken
+files slightly earlier.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ src/liblzma/lzma/lzma_decoder.c        |    8 ++++++--
+ src/liblzma/rangecoder/range_decoder.h |   12 +++++++++---
+ 2 files changed, 15 insertions(+), 5 deletions(-)
+
+diff --git a/src/liblzma/lzma/lzma_decoder.c b/src/liblzma/lzma/lzma_decoder.c
+index 5abbc0d..b8f9317 100644
+--- a/src/liblzma/lzma/lzma_decoder.c
++++ b/src/liblzma/lzma/lzma_decoder.c
+@@ -289,8 +289,12 @@ lzma_decode(lzma_coder *restrict coder, lzma_dict *restrict dictptr,
+ 	// Initialization //
+ 	////////////////////
+ 
+-	if (!rc_read_init(&coder->rc, in, in_pos, in_size))
+-		return LZMA_OK;
++	{
++		const lzma_ret ret = rc_read_init(
++				&coder->rc, in, in_pos, in_size);
++		if (ret != LZMA_STREAM_END)
++			return ret;
++	}
+ 
+ 	///////////////
+ 	// Variables //
+diff --git a/src/liblzma/rangecoder/range_decoder.h b/src/liblzma/rangecoder/range_decoder.h
+index fb96180..e0b051f 100644
+--- a/src/liblzma/rangecoder/range_decoder.h
++++ b/src/liblzma/rangecoder/range_decoder.h
+@@ -25,20 +25,26 @@ typedef struct {
+ 
+ 
+ /// Reads the first five bytes to initialize the range decoder.
+-static inline bool
++static inline lzma_ret
+ rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
+ 		size_t *restrict in_pos, size_t in_size)
+ {
+ 	while (rc->init_bytes_left > 0) {
+ 		if (*in_pos == in_size)
+-			return false;
++			return LZMA_OK;
++
++		// The first byte is always 0x00. It could have been omitted
++		// in LZMA2 but it wasn't, so one byte is wasted in every
++		// LZMA2 chunk.
++		if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
++			return LZMA_DATA_ERROR;
+ 
+ 		rc->code = (rc->code << 8) | in[*in_pos];
+ 		++*in_pos;
+ 		--rc->init_bytes_left;
+ 	}
+ 
+-	return true;
++	return LZMA_STREAM_END;
+ }
+ 
+ 
+-- 
+1.7.9.6 (Apple Git-31.1)
+
diff --git a/debian/patches/man-date b/debian/patches/man-date
new file mode 100644
index 0000000..f43d3b7
--- /dev/null
+++ b/debian/patches/man-date
@@ -0,0 +1,25 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Fri, 22 Jun 2012 14:34:03 +0300
+Subject: xz: Update man page date to match the latest update.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ src/xz/xz.1 |    2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/xz/xz.1 b/src/xz/xz.1
+index 3ff89f0..cb8cd1e 100644
+--- a/src/xz/xz.1
++++ b/src/xz/xz.1
+@@ -5,7 +5,7 @@
+ .\" This file has been put into the public domain.
+ .\" You can do whatever you want with this file.
+ .\"
+-.TH XZ 1 "2011-04-11" "Tukaani" "XZ Utils"
++.TH XZ 1 "2012-05-27" "Tukaani" "XZ Utils"
+ .
+ .SH NAME
+ xz, unxz, xzcat, lzma, unlzma, lzcat \- Compress or decompress .xz and .lzma files
+-- 
+1.7.9.6 (Apple Git-31.1)
+
diff --git a/debian/patches/man-xz-lvv-minver b/debian/patches/man-xz-lvv-minver
new file mode 100644
index 0000000..8b2f44d
--- /dev/null
+++ b/debian/patches/man-xz-lvv-minver
@@ -0,0 +1,55 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Sun, 1 Jul 2012 18:44:33 +0300
+Subject: xz: Update the man page about the new field in --robot -lvv.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ src/xz/xz.1 |   18 +++++++++++++++++-
+ 1 file changed, 17 insertions(+), 1 deletion(-)
+
+diff --git a/src/xz/xz.1 b/src/xz/xz.1
+index cb8cd1e..4da09ba 100644
+--- a/src/xz/xz.1
++++ b/src/xz/xz.1
+@@ -5,7 +5,7 @@
+ .\" This file has been put into the public domain.
+ .\" You can do whatever you want with this file.
+ .\"
+-.TH XZ 1 "2012-05-27" "Tukaani" "XZ Utils"
++.TH XZ 1 "2012-07-01" "Tukaani" "XZ Utils"
+ .
+ .SH NAME
+ xz, unxz, xzcat, lzma, unlzma, lzcat \- Compress or decompress .xz and .lzma files
+@@ -1889,6 +1889,14 @@ or
+ .B no
+ indicating if all block headers have both compressed size and
+ uncompressed size stored in them
++.PP
++.I Since
++.B xz
++.I 5.1.2alpha:
++.IP 4. 4
++Minimum
++.B xz
++version required to decompress the file
+ .RE
+ .PD
+ .PP
+@@ -1939,6 +1947,14 @@ or
+ .B no
+ indicating if all block headers have both compressed size and
+ uncompressed size stored in them
++.PP
++.I Since
++.B xz
++.I 5.1.2alpha:
++.IP 12. 4
++Minimum
++.B xz
++version required to decompress the file
+ .RE
+ .PD
+ .PP
+-- 
+1.7.9.6 (Apple Git-31.1)
+
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 0000000..8020937
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1,8 @@
+abi-threaded-encoder
+abi-version-script
+abi-liblzma2-compat
+configure-liblzma2-compat
+man-date
+man-xz-lvv-minver
+xz-lvv-empty-block-minver
+decoder-check-first-0x00
diff --git a/debian/patches/xz-lvv-empty-block-minver b/debian/patches/xz-lvv-empty-block-minver
new file mode 100644
index 0000000..2c658d9
--- /dev/null
+++ b/debian/patches/xz-lvv-empty-block-minver
@@ -0,0 +1,38 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Wed, 4 Jul 2012 19:58:23 +0300
+Subject: xz: Fix the version number printed by xz -lvv.
+
+The decoder bug was fixed in 5.0.2 instead of 5.0.3.
+
+Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
+---
+ src/xz/list.c |    6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/src/xz/list.c b/src/xz/list.c
+index e71fbe2..a9e0adb 100644
+--- a/src/xz/list.c
++++ b/src/xz/list.c
+@@ -482,7 +482,7 @@ parse_block_header(file_pair *pair, const lzma_index_iter *iter,
+ 	// Determine the minimum XZ Utils version that supports this Block.
+ 	//
+ 	// Currently the only thing that 5.0.0 doesn't support is empty
+-	// LZMA2 Block. This bug was fixed in 5.0.3.
++	// LZMA2 Block. This decoder bug was fixed in 5.0.2.
+ 	{
+ 		size_t i = 0;
+ 		while (filters[i + 1].id != LZMA_VLI_UNKNOWN)
+@@ -490,8 +490,8 @@ parse_block_header(file_pair *pair, const lzma_index_iter *iter,
+ 
+ 		if (filters[i].id == LZMA_FILTER_LZMA2
+ 				&& iter->block.uncompressed_size == 0
+-				&& xfi->min_version < 50000032U)
+-			xfi->min_version = 50000032U;
++				&& xfi->min_version < 50000022U)
++			xfi->min_version = 50000022U;
+ 	}
+ 
+ 	// Convert the filter chain to human readable form.
+-- 
+1.7.9.6 (Apple Git-31.1)
+
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..fcbf5c2
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,164 @@
+#!/usr/bin/make -f
+
+build clean install binary-arch binary-indep binary:
+	+dh $@ --parallel $(opt_no_act)
+
+# Runs tests, too, if and only if nocheck is unset (see below).
+build-arch:: build-plain build-static build-small
+
+build-indep: debian/normal-build/Makefile
+	cd debian/normal-build && doxygen Doxyfile
+
+autoreconf_sources := $(wildcard Makefile.am */Makefile.am */*/Makefile.am)
+autoreconf_sources += configure.ac
+
+autoreconf_products := $(wildcard Makefile.in */Makefile.in */*/Makefile.in)
+autoreconf_products += configure	# and more, but these will do
+
+configure_input := $(autoreconf_products)
+configure_input += Doxyfile.in
+configure_input += src/liblzma/liblzma.pc.in
+configure_input += $(wildcard src/scripts/*.in)
+
+override_dh_auto_clean:
+	rm -f debian/*-stamp
+	dh_auto_clean --builddirectory debian/xzdec-build
+	dh_auto_clean --builddirectory debian/normal-build
+	dh_auto_clean --builddirectory debian/static-build
+	sh debian/clean.sh
+
+override_dh_auto_install:
+	dh_auto_install --builddirectory debian/xzdec-build
+	dh_auto_install --builddirectory debian/normal-build
+	dh_auto_install --builddirectory debian/static-build
+	set -e; arch=$$(dpkg-architecture -qDEB_HOST_MULTIARCH); \
+	install -d debian/tmp/lib/$$arch; \
+	mv debian/tmp/usr/lib/$$arch/liblzma.so.* debian/tmp/lib/$$arch/; \
+	dso=$$(basename $$(readlink debian/tmp/usr/lib/$$arch/liblzma.so)); \
+	ln -s -f /lib/$$arch/$$dso debian/tmp/usr/lib/$$arch/liblzma.so
+
+override_dh_installchangelogs:
+	dh_installchangelogs debian/changelog.upstream
+
+build-plain: debian/normal-build/Makefile
+	dh_auto_build --builddirectory debian/normal-build
+
+build-static: debian/static-build/Makefile
+	dh_auto_build --builddirectory debian/static-build
+
+build-small: debian/xzdec-build/Makefile
+	dh_auto_build --builddirectory debian/xzdec-build
+
+debian/test-plain-stamp: | build-plain
+	dh_auto_test --builddirectory debian/normal-build
+	>$@
+
+debian/test-static-stamp: | build-static
+	dh_auto_test --builddirectory debian/static-build
+	>$@
+
+debian/test-xzdec-stamp: | build-small
+	$(MAKE) -C debian/xzdec-build/tests create_compress_files
+	$(MAKE) -C debian/xzdec-build/tests \
+		TESTS="test_check test_files.sh test_compress.sh" \
+		check-TESTS
+	>$@
+
+debian/normal-build/Makefile debian/normal-build/Doxyfile: $(configure_input)
+	dh_auto_configure --builddirectory debian/normal-build -- \
+		--disable-threads --disable-static \
+		$(opt_optimize) $(opt_quiet) \
+		--disable-xzdec --disable-lzmadec
+
+debian/static-build/Makefile: $(configure_input)
+	dh_auto_configure --builddirectory debian/static-build -- \
+		--disable-threads --disable-shared \
+		--enable-liblzma2-compat \
+		$(opt_optimize) $(opt_quiet) \
+		--disable-lzmainfo --disable-scripts \
+		--disable-xzdec --disable-lzmadec \
+		--disable-xz --disable-lzma-links
+
+debian/xzdec-build/Makefile: $(configure_input)
+	dh_auto_configure --builddirectory debian/xzdec-build -- \
+		--disable-shared --disable-nls --disable-encoders \
+		--enable-small --disable-threads \
+		--disable-liblzma2-compat \
+		$(opt_optimize_small) $(opt_quiet) \
+		--disable-lzmainfo --disable-scripts \
+		--disable-xz --disable-lzma-links
+
+$(autoreconf_products): $(autoreconf_sources)
+	autoreconf -is
+
+small_flags_env =
+small_flags_env += DEB_CFLAGS_MAINT_STRIP='-O2 -O3'
+small_flags_env += DEB_CFLAGS_MAINT_PREPEND=-Os
+flags_cmd = dpkg-buildflags --export=configure
+opt_optimize = $(shell $(flags_cmd))
+opt_optimize_small = $(shell $(small_flags_env) $(flags_cmd))
+
+opt_no_act =
+opt_quiet =
+
+# Unless noopt is set, use optimized CRC32 routine on the Hurd.
+ifeq (hurd,$(shell dpkg-architecture -qDEB_HOST_ARCH_OS))
+    cputype := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
+    ifeq (i386,$(cputype))
+        opt_optimize += --enable-assembler=x86
+        opt_optimize_small += --enable-assembler=x86
+    endif
+    ifeq (amd64,$(cputype))
+        opt_optimize += --enable-assembler=x86_64
+        opt_optimize_small += --enable-assembler=x86_64
+    endif
+endif
+
+ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
+    opt_optimize += --disable-assembler
+    opt_optimize_small += --disable-assembler
+endif
+
+ifneq (,$(findstring n,$(MAKEFLAGS)))
+    opt_no_act = --no-act
+endif
+
+ifneq (,$(filter quiet,$(DEB_BUILD_OPTIONS)))
+    opt_quiet = --quiet
+    MAKEFLAGS += --quiet
+endif
+
+ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
+build-arch:: debian/test-plain-stamp
+build-arch:: debian/test-static-stamp
+build-arch:: debian/test-xzdec-stamp
+endif
+
+# Build a tarball with the latest upstream version.
+# This is made complicated by the need to choose a nice version number.
+REPO = http://git.tukaani.org/xz.git
+BRANCH = master
+get-orig-source:
+	mkdir debian-orig-source
+	-set -e; cd debian-orig-source; \
+	: Fetch latest upstream version.; \
+		git init -q; \
+		git fetch -q --tags $(REPO); \
+		git fetch -q $(REPO) $(BRANCH); \
+	: Determine version number.; \
+		commit_name=$$(git describe FETCH_HEAD); \
+		release=$${commit_name%%-*}; \
+		date=$$(date --utc --date="$$( \
+			git log -1 --pretty=format:%cD "$$commit_name" \
+			)" "+%Y%m%d"); \
+		if test "$$commit_name" = "$$release"; \
+		then upstream_version=$${commit_name#v}; \
+		else upstream_version="$${release#v}+$${date}"; \
+		fi; \
+	: Generate tarball.; \
+		echo "packaging $$commit_name"; \
+		git archive --format=tar "$$commit_name" \
+			--prefix="xz-utils-$$upstream_version/" | \
+		gzip -n --rsyncable -9 \
+			> "../xz-utils_$$upstream_version.orig.tar.gz"
+	rm -fr debian-orig-source
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..163aaf8
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (quilt)
diff --git a/debian/symbols b/debian/symbols
new file mode 100644
index 0000000..ae398c9
--- /dev/null
+++ b/debian/symbols
@@ -0,0 +1,6 @@
+liblzma.so.5 liblzma5 #MINVER#
+* Build-Depends-Package: liblzma-dev
+ (symver)XZ_5.0 5.1.1alpha+20110809
+ lzma_code@XZ_5.0 5.1.1alpha+20120614
+ lzma_raw_buffer_decode@XZ_5.0 5.1.1alpha+20120614
+ lzma_raw_buffer_encode@XZ_5.0 5.1.1alpha+20120614
diff --git a/debian/watch b/debian/watch
new file mode 100644
index 0000000..6352a9c
--- /dev/null
+++ b/debian/watch
@@ -0,0 +1,3 @@
+version=3
+opts=dversionmangle=s/\+\d{8}$// \
+http://tukaani.org/xz/xz-([\d.]*(?:beta)?)\.tar\.gz
diff --git a/debian/xz-utils.NEWS b/debian/xz-utils.NEWS
new file mode 100644
index 0000000..6eac958
--- /dev/null
+++ b/debian/xz-utils.NEWS
@@ -0,0 +1,31 @@
+xz-utils (4.999.9beta+20100212-2) unstable; urgency=low
+
+  The tiny xzdec and lzmadec decompressors have been moved to the new
+  xzdec package.  One can use xzcat (which is an abbreviation for
+  “xz --decompress --stdout”) to replace them where space is not a
+  premium.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 23 Feb 2010 08:24:10 -0600
+
+xz-utils (4.999.9beta+20100117-1) unstable; urgency=low
+
+  From this version on, xz produces sparse files by default when
+  decompressing.  This saves disk space when reproducing files with long
+  runs of zero bytes, such as file system images.  Implementing this
+  feature requires seeking forward beyond the end of a file so that the
+  operating system knows where to put in a hole.
+
+  If you use xz --decompress to write to end of a file that is appended
+  to at the same time by another process, this could cause concurrent
+  output from the other command to be overwritten.  This is a rare and
+  quite weird thing to do.  If you really want to do it, note that xz’s
+  buffering strategy is not part of its documented interface; to safely
+  interleave concurrent output from xz and another command, a
+  construction such as
+
+  	xz --decompress input.xz | dd ibs=1k obs=1k >> log
+
+  would be needed to avoid unpredictable results.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Tue, 02 Feb 2010 15:51:37 -0600
+
diff --git a/debian/xz-utils.README.Debian b/debian/xz-utils.README.Debian
new file mode 100644
index 0000000..180be4c
--- /dev/null
+++ b/debian/xz-utils.README.Debian
@@ -0,0 +1,81 @@
+XZ Utils for Debian
+===================
+
+Contents:
+ 1. History
+ 2. Differences from standard XZ Utils
+ 3. LZMA Utils compatibility
+ 4. Configuration
+
+History
+-------
+
+XZ Utils should have been called LZMA Utils 4.42, but it came too late.
+The old .lzma file format has some problems, worst of which is the lack
+of magic number, but it gets enough use to still need to be supported.
+See /usr/share/doc/xz-utils/history.txt.gz for the full story.
+
+Differences from standard XZ Utils
+----------------------------------
+
+XZ Utils 5.1.y has some experimental features which are disabled in
+Debian to allow interfaces to evolve.  Debian liblzma is also modified
+to avoid breakage when the same process loads liblzma2 from Debian 6.0
+(squeeze) and liblzma5.
+
+abi-threaded-encoder
+  Disable threaded compression in liblzma and xz.
+
+abi-version-script
+  liblzma: Do not pretend to satisfy dependencies on XZ_5.1.1alpha.
+
+abi-liblzma2-compat, configure-liblzma2-compat
+  Do not check reserved fields past the historical end of the
+  lzma_stream structure if liblzma.so.2 is loaded in the same
+  process image.  Likewise when linked statically.
+  (See bug #649522.)
+
+man-date, man-xz-lvv-minver (from upstream)
+  Document the "Minimum version required to decompress" field of
+  "xz --robot -v -v --list" output.
+
+xz-lvv-empty-block-minver (from upstream)
+  Fix the version number printed by "xz -lvv" for files with Blocks
+  of zero uncompressed_size: the decoder bug preventing reading such
+  files was fixed in xz 5.0.2, not 5.0.3.
+
+decoder-check-first-0x00 (from upstream)
+  Check that the first byte of range encoded data is zero to catch
+  broken files sooner.
+
+LZMA Utils compatibility
+------------------------
+
+To support old scripts and muscle memory, XZ Utils can emulate the
+legacy LZMA Utils interface.  To use this feature, you can install some
+subset of the following list of symbolic links to your $PATH.
+
+	lzma, unlzma, lzcat -> /usr/bin/xz
+	lzgrep, lzegrep, lzfgrep -> /usr/bin/xzgrep
+	lzless -> /usr/bin/xzless
+	lzmore -> /usr/bin/xzmore
+	lzdiff, lzcmp -> /usr/bin/xzdiff
+
+If you would like XZ Utils to provide these commands by default for
+all users, use "update-alternatives --config lzma".
+
+Configuration
+-------------
+
+The memory usage of xz can vary from a few hundred kilobytes to several
+gigabytes depending on the compression settings.  If you would like xz
+to automatically scale down its settings while compressing to decrease
+memory usage, you can declare so by adding an option like the
+following to your environment (e.g., in ~/.profile):
+
+	XZ_DEFAULTS=--memlimit-compress=256MiB
+	export XZ_DEFAULTS
+
+See the "Memory usage" section of the xz(1) manual page for details.
+
+ -- Jonathan Nieder <jrnieder@gmail.com>  Fri, 17 Aug 2012 23:37:47 -0700
diff --git a/debian/xz-utils.docs b/debian/xz-utils.docs
new file mode 100644
index 0000000..463b710
--- /dev/null
+++ b/debian/xz-utils.docs
@@ -0,0 +1,7 @@
+README
+NEWS
+AUTHORS
+THANKS
+doc/history.txt
+doc/faq.txt
+extra
diff --git a/debian/xz-utils.install b/debian/xz-utils.install
new file mode 100644
index 0000000..9acbd88
--- /dev/null
+++ b/debian/xz-utils.install
@@ -0,0 +1,19 @@
+usr/bin/xz
+usr/bin/unxz
+usr/bin/xzcat
+usr/bin/xzmore
+usr/bin/xzless
+usr/bin/xzdiff
+usr/bin/xzcmp
+usr/bin/xz*grep
+usr/share/man/man1/xz.1
+usr/share/man/man1/unxz.1
+usr/share/man/man1/xzcat.1
+usr/share/man/man1/xzmore.1
+usr/share/man/man1/xzless.1
+usr/share/man/man1/xzdiff.1
+usr/share/man/man1/xzcmp.1
+usr/share/man/man1/xz*grep.1
+usr/bin/lzmainfo
+usr/share/man/man1/lzmainfo.1
+usr/share/locale/*/LC_MESSAGES/xz.mo
diff --git a/debian/xz-utils.postinst b/debian/xz-utils.postinst
new file mode 100644
index 0000000..a2cf46f
--- /dev/null
+++ b/debian/xz-utils.postinst
@@ -0,0 +1,19 @@
+#!/bin/sh
+set -e
+
+slave () {
+	dir=$1 filename=$2 target=$3
+	echo "--slave $dir/$filename $filename $dir/$target"
+}
+
+update-alternatives --install /usr/bin/lzma lzma /usr/bin/xz 20 $(
+	slave /usr/share/man/man1 lzma.1.gz xz.1.gz
+	slave /usr/bin unlzma unxz
+	slave /usr/share/man/man1 unlzma.1.gz unxz.1.gz
+	for util in cat more less diff cmp grep egrep fgrep
+	do
+		slave /usr/bin lz$util xz$util
+		slave /usr/share/man/man1 lz$util.1.gz xz$util.1.gz
+	done
+)
+#DEBHELPER#
diff --git a/debian/xz-utils.prerm b/debian/xz-utils.prerm
new file mode 100644
index 0000000..8dd3d8f
--- /dev/null
+++ b/debian/xz-utils.prerm
@@ -0,0 +1,10 @@
+#!/bin/sh
+set -e
+if
+	test "$1" != upgrade ||
+	# downgrading to a pre-alternatives version
+	dpkg --compare-versions "$2" lt-nl 5.1.1alpha+20120614-1
+then
+	update-alternatives --remove lzma /usr/bin/xz
+fi
+#DEBHELPER#
diff --git a/debian/xzdec.docs b/debian/xzdec.docs
new file mode 100644
index 0000000..7b5ff1f
--- /dev/null
+++ b/debian/xzdec.docs
@@ -0,0 +1,6 @@
+README
+NEWS
+AUTHORS
+THANKS
+doc/history.txt
+doc/faq.txt
diff --git a/debian/xzdec.install b/debian/xzdec.install
new file mode 100644
index 0000000..7f67bd8
--- /dev/null
+++ b/debian/xzdec.install
@@ -0,0 +1,4 @@
+usr/bin/xzdec
+usr/bin/lzmadec
+usr/share/man/man1/xzdec.1
+usr/share/man/man1/lzmadec.1
diff --git a/src/liblzma/Makefile.am b/src/liblzma/Makefile.am
index 5bd205d..ac2d1ed 100644
--- a/src/liblzma/Makefile.am
+++ b/src/liblzma/Makefile.am
@@ -24,7 +24,7 @@
 	-I$(top_srcdir)/src/liblzma/simple \
 	-I$(top_srcdir)/src/common \
 	-DTUKLIB_SYMBOL_PREFIX=lzma_
-liblzma_la_LDFLAGS = -no-undefined -version-info 5:99:0
+liblzma_la_LDFLAGS = -no-undefined -version-info 5:0:0
 
 if COND_SYMVERS
 EXTRA_DIST += liblzma.map
diff --git a/src/liblzma/api/lzma/container.h b/src/liblzma/api/lzma/container.h
index 499d8b9..7a9ffc6 100644
--- a/src/liblzma/api/lzma/container.h
+++ b/src/liblzma/api/lzma/container.h
@@ -60,129 +60,6 @@
 #define LZMA_PRESET_EXTREME       (UINT32_C(1) << 31)
 
 
-#ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
-/**
- * \brief       Multithreading options
- */
-typedef struct {
-	/**
-	 * \brief       Flags
-	 *
-	 * Set this to zero if no flags are wanted.
-	 *
-	 * No flags are currently supported.
-	 */
-	uint32_t flags;
-
-	/**
-	 * \brief       Number of worker threads to use
-	 */
-	uint32_t threads;
-
-	/**
-	 * \brief       Maximum uncompressed size of a Block
-	 *
-	 * The encoder will start a new .xz Block every block_size bytes.
-	 * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
-	 * the caller may tell liblzma to start a new Block earlier.
-	 *
-	 * With LZMA2, a recommended block size is 2-4 times the LZMA2
-	 * dictionary size. With very small dictionaries, it is recommended
-	 * to use at least 1 MiB block size for good compression ratio, even
-	 * if this is more than four times the dictionary size. Note that
-	 * these are only recommendations for typical use cases; feel free
-	 * to use other values. Just keep in mind that using a block size
-	 * less than the LZMA2 dictionary size is waste of RAM.
-	 *
-	 * Set this to 0 to let liblzma choose the block size depending
-	 * on the compression options. For LZMA2 it will be 3*dict_size
-	 * or 1 MiB, whichever is more.
-	 */
-	uint64_t block_size;
-
-	/**
-	 * \brief       Timeout to allow lzma_code() to return early
-	 *
-	 * Multithreading can make liblzma to consume input and produce
-	 * output in a very bursty way: it may first read a lot of input
-	 * to fill internal buffers, then no input or output occurs for
-	 * a while.
-	 *
-	 * In single-threaded mode, lzma_code() won't return until it has
-	 * either consumed all the input or filled the output buffer. If
-	 * this is done in multithreaded mode, it may cause a call
-	 * lzma_code() to take even tens of seconds, which isn't acceptable
-	 * in all applications.
-	 *
-	 * To avoid very long blocking times in lzma_code(), a timeout
-	 * (in milliseconds) may be set here. If lzma_code() would block
-	 * longer than this number of milliseconds, it will return with
-	 * LZMA_OK. Reasonable values are 100 ms or more. The xz command
-	 * line tool uses 300 ms.
-	 *
-	 * If long blocking times are fine for you, set timeout to a special
-	 * value of 0, which will disable the timeout mechanism and will make
-	 * lzma_code() block until all the input is consumed or the output
-	 * buffer has been filled.
-	 *
-	 * \note        Even with a timeout, lzma_code() might sometimes take
-	 *              somewhat long time to return. No timing guarantees
-	 *              are made.
-	 */
-	uint32_t timeout;
-
-	/**
-	 * \brief       Compression preset (level and possible flags)
-	 *
-	 * The preset is set just like with lzma_easy_encoder().
-	 * The preset is ignored if filters below is non-NULL.
-	 */
-	uint32_t preset;
-
-	/**
-	 * \brief       Filter chain (alternative to a preset)
-	 *
-	 * If this is NULL, the preset above is used. Otherwise the preset
-	 * is ignored and the filter chain specified here is used.
-	 */
-	const lzma_filter *filters;
-
-	/**
-	 * \brief       Integrity check type
-	 *
-	 * See check.h for available checks. The xz command line tool
-	 * defaults to LZMA_CHECK_CRC64, which is a good choice if you
-	 * are unsure.
-	 */
-	lzma_check check;
-
-	/*
-	 * Reserved space to allow possible future extensions without
-	 * breaking the ABI. You should not touch these, because the names
-	 * of these variables may change. These are and will never be used
-	 * with the currently supported options, so it is safe to leave these
-	 * uninitialized.
-	 */
-	lzma_reserved_enum reserved_enum1;
-	lzma_reserved_enum reserved_enum2;
-	lzma_reserved_enum reserved_enum3;
-	uint32_t reserved_int1;
-	uint32_t reserved_int2;
-	uint32_t reserved_int3;
-	uint32_t reserved_int4;
-	uint64_t reserved_int5;
-	uint64_t reserved_int6;
-	uint64_t reserved_int7;
-	uint64_t reserved_int8;
-	void *reserved_ptr1;
-	void *reserved_ptr2;
-	void *reserved_ptr3;
-	void *reserved_ptr4;
-
-} lzma_mt;
-#endif
-
-
 /**
  * \brief       Calculate approximate memory usage of easy encoder
  *
@@ -313,50 +190,6 @@
 		lzma_nothrow lzma_attr_warn_unused_result;
 
 
-#ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
-/**
- * \brief       Calculate approximate memory usage of multithreaded .xz encoder
- *
- * Since doing the encoding in threaded mode doesn't affect the memory
- * requirements of single-threaded decompressor, you can use
- * lzma_easy_decoder_memusage(options->preset) or
- * lzma_raw_decoder_memusage(options->filters) to calculate
- * the decompressor memory requirements.
- *
- * \param       options Compression options
- *
- * \return      Number of bytes of memory required for encoding with the
- *              given options. If an error occurs, for example due to
- *              unsupported preset or filter chain, UINT64_MAX is returned.
- */
-extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
-		const lzma_mt *options) lzma_nothrow lzma_attr_pure;
-
-
-/**
- * \brief       Initialize multithreaded .xz Stream encoder
- *
- * This provides the functionality of lzma_easy_encoder() and
- * lzma_stream_encoder() as a single function for multithreaded use.
- *
- * TODO: For lzma_code(), only LZMA_RUN and LZMA_FINISH are currently
- * supported. Support for other actions has been planned.
- *
- * \param       strm    Pointer to properly prepared lzma_stream
- * \param       options Pointer to multithreaded compression options
- *
- * \return      - LZMA_OK
- *              - LZMA_MEM_ERROR
- *              - LZMA_UNSUPPORTED_CHECK
- *              - LZMA_OPTIONS_ERROR
- *              - LZMA_PROG_ERROR
- */
-extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
-		lzma_stream *strm, const lzma_mt *options)
-		lzma_nothrow lzma_attr_warn_unused_result;
-#endif
-
-
 /**
  * \brief       Initialize .lzma encoder (legacy file format)
  *
diff --git a/src/liblzma/api/lzma/version.h b/src/liblzma/api/lzma/version.h
index 4bf7e40..a908ea2 100644
--- a/src/liblzma/api/lzma/version.h
+++ b/src/liblzma/api/lzma/version.h
@@ -22,7 +22,7 @@
  */
 #define LZMA_VERSION_MAJOR 5
 #define LZMA_VERSION_MINOR 1
-#define LZMA_VERSION_PATCH 1
+#define LZMA_VERSION_PATCH 0
 #define LZMA_VERSION_STABILITY LZMA_VERSION_STABILITY_ALPHA
 
 #ifndef LZMA_VERSION_COMMIT
diff --git a/src/liblzma/common/Makefile.inc b/src/liblzma/common/Makefile.inc
index dd5a8c8..81d751e 100644
--- a/src/liblzma/common/Makefile.inc
+++ b/src/liblzma/common/Makefile.inc
@@ -40,13 +40,6 @@
 	common/stream_encoder.c \
 	common/stream_flags_encoder.c \
 	common/vli_encoder.c
-
-if COND_THREADS
-liblzma_la_SOURCES += \
-	common/outqueue.c \
-	common/outqueue.h \
-	common/stream_encoder_mt.c
-endif
 endif
 
 if COND_MAIN_DECODER
diff --git a/src/liblzma/common/common.c b/src/liblzma/common/common.c
index 85ae96a..3bfdb75 100644
--- a/src/liblzma/common/common.c
+++ b/src/liblzma/common/common.c
@@ -12,6 +12,8 @@
 
 #include "common.h"
 
+#include <dlfcn.h>
+
 
 /////////////
 // Version //
@@ -141,6 +143,47 @@
 // External to internal API wrapper //
 //////////////////////////////////////
 
+#ifdef LIBLZMA2_COMPAT_DYNAMIC
+
+static void
+init_liblzma2_compat(lzma_stream *strm)
+{
+	void *handle = dlopen("liblzma.so.2", RTLD_LAZY | RTLD_NOLOAD);
+	if (handle) {
+		dlclose(handle);
+		strm->internal->liblzma2_compat = true;
+		return;
+	}
+	strm->internal->liblzma2_compat = false;
+}
+
+static bool
+liblzma2_loaded(lzma_stream *strm)
+{
+	return strm->internal->liblzma2_compat;
+}
+
+#else
+
+static void
+init_liblzma2_compat(lzma_stream *strm)
+{
+}
+
+#ifdef LIBLZMA2_COMPAT
+static bool liblzma2_loaded(lzma_stream *strm)
+{
+	return true;
+}
+#else
+static bool liblzma2_loaded(lzma_stream *strm)
+{
+	return false;
+}
+#endif
+
+#endif
+
 extern lzma_ret
 lzma_strm_init(lzma_stream *strm)
 {
@@ -154,6 +197,7 @@
 			return LZMA_MEM_ERROR;
 
 		strm->internal->next = LZMA_NEXT_CODER_INIT;
+		init_liblzma2_compat(strm);
 	}
 
 	memzero(strm->internal->supported_actions,
@@ -168,6 +212,24 @@
 }
 
 
+// Before v5.0.0~6 (liblzma: A few ABI tweaks to reserve space in
+// structures, 2010-10-23), the reserved fields in lzma_stream were:
+//
+//	void *reserved_ptr1;
+//	void *reserved_ptr2;
+//	uint64_t reserved_int1;
+//	uint64_t reserved_int2;
+//	lzma_reserved_enum reserved_enum1;
+//	lzma_reserved_enum reserved_enum2;
+//
+// Nowadays there are two more pointers between reserved_ptr2 and
+// reserved_int1 and two size_t fields between reserved_int2 and
+// reserved_enum1.
+//
+// When strm->internal->liblzma2_compat is set, limit the checks of
+// reserved fields to fields that were present in the old ABI to avoid
+// segfaults and spurious "Unsupported options" from callers sharing
+// the process image that expect the old ABI.
 extern LZMA_API(lzma_ret)
 lzma_code(lzma_stream *strm, lzma_action action)
 {
@@ -185,8 +247,12 @@
 	if (strm->reserved_ptr1 != NULL
 			|| strm->reserved_ptr2 != NULL
 			|| strm->reserved_ptr3 != NULL
-			|| strm->reserved_ptr4 != NULL
-			|| strm->reserved_int1 != 0
+			|| strm->reserved_ptr4 != NULL)
+		return LZMA_OPTIONS_ERROR;
+
+	if (liblzma2_loaded(strm))
+		; /* Enough checks. */
+	else if (strm->reserved_int1 != 0
 			|| strm->reserved_int2 != 0
 			|| strm->reserved_int3 != 0
 			|| strm->reserved_int4 != 0
@@ -263,9 +329,7 @@
 
 	strm->internal->avail_in = strm->avail_in;
 
-	// Cast is needed to silence a warning about LZMA_TIMED_OUT, which
-	// isn't part of lzma_ret enumeration.
-	switch ((unsigned int)(ret)) {
+	switch (ret) {
 	case LZMA_OK:
 		// Don't return LZMA_BUF_ERROR when it happens the first time.
 		// This is to avoid returning LZMA_BUF_ERROR when avail_out
@@ -281,11 +345,6 @@
 		}
 		break;
 
-	case LZMA_TIMED_OUT:
-		strm->internal->allow_buf_error = false;
-		ret = LZMA_OK;
-		break;
-
 	case LZMA_STREAM_END:
 		if (strm->internal->sequence == ISEQ_SYNC_FLUSH
 				|| strm->internal->sequence == ISEQ_FULL_FLUSH)
diff --git a/src/liblzma/common/common.h b/src/liblzma/common/common.h
index 5c92af2..4081c2d 100644
--- a/src/liblzma/common/common.h
+++ b/src/liblzma/common/common.h
@@ -32,8 +32,6 @@
 
 #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
 
-#define LZMA_UNSTABLE
-
 #include "lzma.h"
 
 // These allow helping the compiler in some often-executed branches, whose
@@ -51,13 +49,6 @@
 #define LZMA_BUFFER_SIZE 4096
 
 
-/// Maximum number of worker threads within one multithreaded component.
-/// The limit exists solely to make it simpler to prevent integer overflows
-/// when allocating structures etc. This should be big enough for now...
-/// the code won't scale anywhere close to this number anyway.
-#define LZMA_THREADS_MAX 16384
-
-
 /// Starting value for memory usage estimates. Instead of calculating size
 /// of _every_ structure and taking into account malloc() overhead etc., we
 /// add a base size to all memory usage estimates. It's not very accurate
@@ -78,13 +69,6 @@
 	| LZMA_CONCATENATED )
 
 
-/// Special return value (lzma_ret) to indicate that a timeout was reached
-/// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
-/// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
-/// there's no need to have it in the public API.
-#define LZMA_TIMED_OUT 32
-
-
 /// Type of encoder/decoder specific data; the actual structure is defined
 /// differently in different coders.
 typedef struct lzma_coder_s lzma_coder;
@@ -216,6 +200,12 @@
 	/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
 	/// made (no input consumed and no output produced by next.code).
 	bool allow_buf_error;
+
+#ifdef LIBLZMA2_COMPAT_DYNAMIC
+	/// Indicates whether we are sharing a process image with
+	/// liblzma.so.2 and need to tread carefully.
+	bool liblzma2_compat;
+#endif
 };
 
 
diff --git a/src/liblzma/common/outqueue.c b/src/liblzma/common/outqueue.c
deleted file mode 100644
index d7a87d9..0000000
--- a/src/liblzma/common/outqueue.c
+++ /dev/null
@@ -1,184 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-//
-/// \file       outqueue.c
-/// \brief      Output queue handling in multithreaded coding
-//
-//  Author:     Lasse Collin
-//
-//  This file has been put into the public domain.
-//  You can do whatever you want with this file.
-//
-///////////////////////////////////////////////////////////////////////////////
-
-#include "outqueue.h"
-
-
-/// This is to ease integer overflow checking: We may allocate up to
-/// 2 * LZMA_THREADS_MAX buffers and we need some extra memory for other
-/// data structures (that's the second /2).
-#define BUF_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX / 2 / 2)
-
-
-static lzma_ret
-get_options(uint64_t *bufs_alloc_size, uint32_t *bufs_count,
-		uint64_t buf_size_max, uint32_t threads)
-{
-	if (threads > LZMA_THREADS_MAX || buf_size_max > BUF_SIZE_MAX)
-		return LZMA_OPTIONS_ERROR;
-
-	// The number of buffers is twice the number of threads.
-	// This wastes RAM but keeps the threads busy when buffers
-	// finish out of order.
-	//
-	// NOTE: If this is changed, update BUF_SIZE_MAX too.
-	*bufs_count = threads * 2;
-	*bufs_alloc_size = *bufs_count * buf_size_max;
-
-	return LZMA_OK;
-}
-
-
-extern uint64_t
-lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads)
-{
-	uint64_t bufs_alloc_size;
-	uint32_t bufs_count;
-
-	if (get_options(&bufs_alloc_size, &bufs_count, buf_size_max, threads)
-			!= LZMA_OK)
-		return UINT64_MAX;
-
-	return sizeof(lzma_outq) + bufs_count * sizeof(lzma_outbuf)
-			+ bufs_alloc_size;
-}
-
-
-extern lzma_ret
-lzma_outq_init(lzma_outq *outq, lzma_allocator *allocator,
-		uint64_t buf_size_max, uint32_t threads)
-{
-	uint64_t bufs_alloc_size;
-	uint32_t bufs_count;
-
-	// Set bufs_count and bufs_alloc_size.
-	return_if_error(get_options(&bufs_alloc_size, &bufs_count,
-			buf_size_max, threads));
-
-	// Allocate memory if needed.
-	if (outq->buf_size_max != buf_size_max
-			|| outq->bufs_allocated != bufs_count) {
-		lzma_outq_end(outq, allocator);
-
-#if SIZE_MAX < UINT64_MAX
-		if (bufs_alloc_size > SIZE_MAX)
-			return LZMA_MEM_ERROR;
-#endif
-
-		outq->bufs = lzma_alloc(bufs_count * sizeof(lzma_outbuf),
-				allocator);
-		outq->bufs_mem = lzma_alloc((size_t)(bufs_alloc_size),
-				allocator);
-
-		if (outq->bufs == NULL || outq->bufs_mem == NULL) {
-			lzma_outq_end(outq, allocator);
-			return LZMA_MEM_ERROR;
-		}
-	}
-
-	// Initialize the rest of the main structure. Initialization of
-	// outq->bufs[] is done when they are actually needed.
-	outq->buf_size_max = (size_t)(buf_size_max);
-	outq->bufs_allocated = bufs_count;
-	outq->bufs_pos = 0;
-	outq->bufs_used = 0;
-	outq->read_pos = 0;
-
-	return LZMA_OK;
-}
-
-
-extern void
-lzma_outq_end(lzma_outq *outq, lzma_allocator *allocator)
-{
-	lzma_free(outq->bufs, allocator);
-	outq->bufs = NULL;
-
-	lzma_free(outq->bufs_mem, allocator);
-	outq->bufs_mem = NULL;
-
-	return;
-}
-
-
-extern lzma_outbuf *
-lzma_outq_get_buf(lzma_outq *outq)
-{
-	// Caller must have checked it with lzma_outq_has_buf().
-	assert(outq->bufs_used < outq->bufs_allocated);
-
-	// Initialize the new buffer.
-	lzma_outbuf *buf = &outq->bufs[outq->bufs_pos];
-	buf->buf = outq->bufs_mem + outq->bufs_pos * outq->buf_size_max;
-	buf->size = 0;
-	buf->finished = false;
-
-	// Update the queue state.
-	if (++outq->bufs_pos == outq->bufs_allocated)
-		outq->bufs_pos = 0;
-
-	++outq->bufs_used;
-
-	return buf;
-}
-
-
-extern bool
-lzma_outq_is_readable(const lzma_outq *outq)
-{
-	uint32_t i = outq->bufs_pos - outq->bufs_used;
-	if (outq->bufs_pos < outq->bufs_used)
-		i += outq->bufs_allocated;
-
-	return outq->bufs[i].finished;
-}
-
-
-extern lzma_ret
-lzma_outq_read(lzma_outq *restrict outq, uint8_t *restrict out,
-		size_t *restrict out_pos, size_t out_size,
-		lzma_vli *restrict unpadded_size,
-		lzma_vli *restrict uncompressed_size)
-{
-	// There must be at least one buffer from which to read.
-	if (outq->bufs_used == 0)
-		return LZMA_OK;
-
-	// Get the buffer.
-	uint32_t i = outq->bufs_pos - outq->bufs_used;
-	if (outq->bufs_pos < outq->bufs_used)
-		i += outq->bufs_allocated;
-
-	lzma_outbuf *buf = &outq->bufs[i];
-
-	// If it isn't finished yet, we cannot read from it.
-	if (!buf->finished)
-		return LZMA_OK;
-
-	// Copy from the buffer to output.
-	lzma_bufcpy(buf->buf, &outq->read_pos, buf->size,
-			out, out_pos, out_size);
-
-	// Return if we didn't get all the data from the buffer.
-	if (outq->read_pos < buf->size)
-		return LZMA_OK;
-
-	// The buffer was finished. Tell the caller its size information.
-	*unpadded_size = buf->unpadded_size;
-	*uncompressed_size = buf->uncompressed_size;
-
-	// Free this buffer for further use.
-	--outq->bufs_used;
-	outq->read_pos = 0;
-
-	return LZMA_STREAM_END;
-}
diff --git a/src/liblzma/common/outqueue.h b/src/liblzma/common/outqueue.h
deleted file mode 100644
index 154f91b..0000000
--- a/src/liblzma/common/outqueue.h
+++ /dev/null
@@ -1,155 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-//
-/// \file       outqueue.h
-/// \brief      Output queue handling in multithreaded coding
-//
-//  Author:     Lasse Collin
-//
-//  This file has been put into the public domain.
-//  You can do whatever you want with this file.
-//
-///////////////////////////////////////////////////////////////////////////////
-
-#include "common.h"
-
-
-/// Output buffer for a single thread
-typedef struct {
-	/// Pointer to the output buffer of lzma_outq.buf_size_max bytes
-	uint8_t *buf;
-
-	/// Amount of data written to buf
-	size_t size;
-
-	/// Additional size information
-	lzma_vli unpadded_size;
-	lzma_vli uncompressed_size;
-
-	/// True when no more data will be written into this buffer.
-	///
-	/// \note       This is read by another thread and thus access
-	///             to this variable needs a mutex.
-	bool finished;
-
-} lzma_outbuf;
-
-
-typedef struct {
-	/// Array of buffers that are used cyclically.
-	lzma_outbuf *bufs;
-
-	/// Memory allocated for all the buffers
-	uint8_t *bufs_mem;
-
-	/// Amount of buffer space available in each buffer
-	size_t buf_size_max;
-
-	/// Number of buffers allocated
-	uint32_t bufs_allocated;
-
-	/// Position in the bufs array. The next buffer to be taken
-	/// into use is bufs[bufs_pos].
-	uint32_t bufs_pos;
-
-	/// Number of buffers in use
-	uint32_t bufs_used;
-
-	/// Position in the buffer in lzma_outq_read()
-	size_t read_pos;
-
-} lzma_outq;
-
-
-/**
- * \brief       Calculate the memory usage of an output queue
- *
- * \return      Approximate memory usage in bytes or UINT64_MAX on error.
- */
-extern uint64_t lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads);
-
-
-/// \brief      Initialize an output queue
-///
-/// \param      outq            Pointer to an output queue. Before calling
-///                             this function the first time, *outq should
-///                             have been zeroed with memzero() so that this
-///                             function knows that there are no previous
-///                             allocations to free.
-/// \param      allocator       Pointer to allocator or NULL
-/// \param      buf_size_max    Maximum amount of data that a single buffer
-///                             in the queue may need to store.
-/// \param      threads         Number of buffers that may be in use
-///                             concurrently. Note that more than this number
-///                             of buffers will actually get allocated to
-///                             improve performance when buffers finish
-///                             out of order.
-///
-/// \return     - LZMA_OK
-///             - LZMA_MEM_ERROR
-///
-extern lzma_ret lzma_outq_init(lzma_outq *outq, lzma_allocator *allocator,
-		uint64_t buf_size_max, uint32_t threads);
-
-
-/// \brief      Free the memory associated with the output queue
-extern void lzma_outq_end(lzma_outq *outq, lzma_allocator *allocator);
-
-
-/// \brief      Get a new buffer
-///
-/// lzma_outq_has_buf() must be used to check that there is a buffer
-/// available before calling lzma_outq_get_buf().
-///
-extern lzma_outbuf *lzma_outq_get_buf(lzma_outq *outq);
-
-
-/// \brief      Test if there is data ready to be read
-///
-/// Call to this function must be protected with the same mutex that
-/// is used to protect lzma_outbuf.finished.
-///
-extern bool lzma_outq_is_readable(const lzma_outq *outq);
-
-
-/// \brief      Read finished data
-///
-/// \param      outq            Pointer to an output queue
-/// \param      out             Beginning of the output buffer
-/// \param      out_pos         The next byte will be written to
-///                             out[*out_pos].
-/// \param      out_size        Size of the out buffer; the first byte into
-///                             which no data is written to is out[out_size].
-/// \param      unpadded_size   Unpadded Size from the Block encoder
-/// \param      uncompressed_size Uncompressed Size from the Block encoder
-///
-/// \return     - LZMA: All OK. Either no data was available or the buffer
-///               being read didn't become empty yet.
-///             - LZMA_STREAM_END: The buffer being read was finished.
-///               *unpadded_size and *uncompressed_size were set.
-///
-/// \note       This reads lzma_outbuf.finished variables and thus call
-///             to this function needs to be protected with a mutex.
-///
-extern lzma_ret lzma_outq_read(lzma_outq *restrict outq,
-		uint8_t *restrict out, size_t *restrict out_pos,
-		size_t out_size, lzma_vli *restrict unpadded_size,
-		lzma_vli *restrict uncompressed_size);
-
-
-/// \brief      Test if there is at least one buffer free
-///
-/// This must be used before getting a new buffer with lzma_outq_get_buf().
-///
-static inline bool
-lzma_outq_has_buf(const lzma_outq *outq)
-{
-	return outq->bufs_used < outq->bufs_allocated;
-}
-
-
-/// \brief      Test if the queue is completely empty
-static inline bool
-lzma_outq_is_empty(const lzma_outq *outq)
-{
-	return outq->bufs_used == 0;
-}
diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
deleted file mode 100644
index a4b2800..0000000
--- a/src/liblzma/common/stream_encoder_mt.c
+++ /dev/null
@@ -1,1013 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-//
-/// \file       stream_encoder_mt.c
-/// \brief      Multithreaded .xz Stream encoder
-//
-//  Author:     Lasse Collin
-//
-//  This file has been put into the public domain.
-//  You can do whatever you want with this file.
-//
-///////////////////////////////////////////////////////////////////////////////
-
-#include "filter_encoder.h"
-#include "easy_preset.h"
-#include "block_encoder.h"
-#include "index_encoder.h"
-#include "outqueue.h"
-
-
-/// Maximum supported block size. This makes it simpler to prevent integer
-/// overflows if we are given unusually large block size.
-#define BLOCK_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX)
-
-
-typedef enum {
-	/// Waiting for work.
-	THR_IDLE,
-
-	/// Encoding is in progress.
-	THR_RUN,
-
-	/// Encoding is in progress but no more input data will
-	/// be read.
-	THR_FINISH,
-
-	/// The main thread wants the thread to stop whatever it was doing
-	/// but not exit.
-	THR_STOP,
-
-	/// The main thread wants the thread to exit. We could use
-	/// cancellation but since there's stopped anyway, this is lazier.
-	THR_EXIT,
-
-} worker_state;
-
-
-typedef struct worker_thread_s worker_thread;
-struct worker_thread_s {
-	worker_state state;
-
-	/// Input buffer of coder->block_size bytes. The main thread will
-	/// put new input into this and update in_size accordingly. Once
-	/// no more input is coming, state will be set to THR_FINISH.
-	uint8_t *in;
-
-	/// Amount of data available in the input buffer. This is modified
-	/// only by the main thread.
-	size_t in_size;
-
-	/// Output buffer for this thread. This is set by the main
-	/// thread every time a new Block is started with this thread
-	/// structure.
-	lzma_outbuf *outbuf;
-
-	/// Pointer to the main structure is needed when putting this
-	/// thread back to the stack of free threads.
-	lzma_coder *coder;
-
-	/// The allocator is set by the main thread. Since a copy of the
-	/// pointer is kept here, the application must not change the
-	/// allocator before calling lzma_end().
-	lzma_allocator *allocator;
-
-	/// Block encoder
-	lzma_next_coder block_encoder;
-
-	/// Compression options for this Block
-	lzma_block block_options;
-
-	/// Next structure in the stack of free worker threads.
-	worker_thread *next;
-
-	pthread_mutex_t mutex;
-	pthread_cond_t cond;
-
-	/// The ID of this thread is used to join the thread
-	/// when it's not needed anymore.
-	pthread_t thread_id;
-};
-
-
-struct lzma_coder_s {
-	enum {
-		SEQ_STREAM_HEADER,
-		SEQ_BLOCK,
-		SEQ_INDEX,
-		SEQ_STREAM_FOOTER,
-	} sequence;
-
-	/// Start a new Block every block_size bytes of input unless
-	/// LZMA_FULL_FLUSH or LZMA_FULL_BARRIER is used earlier.
-	size_t block_size;
-
-	/// The filter chain currently in use
-	lzma_filter filters[LZMA_FILTERS_MAX + 1];
-
-
-	/// Index to hold sizes of the Blocks
-	lzma_index *index;
-
-	/// Index encoder
-	lzma_next_coder index_encoder;
-
-
-	/// Stream Flags for encoding the Stream Header and Stream Footer.
-	lzma_stream_flags stream_flags;
-
-	/// Buffer to hold Stream Header and Stream Footer.
-	uint8_t header[LZMA_STREAM_HEADER_SIZE];
-
-	/// Read position in header[]
-	size_t header_pos;
-
-
-	/// Output buffer queue for compressed data
-	lzma_outq outq;
-
-
-	/// True if wait_max is used.
-	bool has_timeout;
-
-	/// Maximum wait time if cannot use all the input and cannot
-	/// fill the output buffer.
-	struct timespec wait_max;
-
-
-	/// Error code from a worker thread
-	lzma_ret thread_error;
-
-	/// Array of allocated thread-specific structures
-	worker_thread *threads;
-
-	/// Number of structures in "threads" above. This is also the
-	/// number of threads that will be created at maximum.
-	uint32_t threads_max;
-
-	/// Number of thread structures that have been initialized, and
-	/// thus the number of worker threads actually created so far.
-	uint32_t threads_initialized;
-
-	/// Stack of free threads. When a thread finishes, it puts itself
-	/// back into this stack. This starts as empty because threads
-	/// are created only when actually needed.
-	worker_thread *threads_free;
-
-	/// The most recent worker thread to which the main thread writes
-	/// the new input from the application.
-	worker_thread *thr;
-
-	pthread_mutex_t mutex;
-	mythread_cond cond;
-};
-
-
-/// Tell the main thread that something has gone wrong.
-static void
-worker_error(worker_thread *thr, lzma_ret ret)
-{
-	assert(ret != LZMA_OK);
-	assert(ret != LZMA_STREAM_END);
-
-	mythread_sync(thr->coder->mutex) {
-		if (thr->coder->thread_error == LZMA_OK)
-			thr->coder->thread_error = ret;
-
-		mythread_cond_signal(&thr->coder->cond);
-	}
-
-	return;
-}
-
-
-static worker_state
-worker_encode(worker_thread *thr, worker_state state)
-{
-	// Set the Block options.
-	thr->block_options = (lzma_block){
-		.version = 0,
-		.check = thr->coder->stream_flags.check,
-		.compressed_size = thr->coder->outq.buf_size_max,
-		.uncompressed_size = thr->coder->block_size,
-
-		// TODO: To allow changing the filter chain, the filters
-		// array must be copied to each worker_thread.
-		.filters = thr->coder->filters,
-	};
-
-	// Calculate maximum size of the Block Header. This amount is
-	// reserved in the beginning of the buffer so that Block Header
-	// along with Compressed Size and Uncompressed Size can be
-	// written there.
-	lzma_ret ret = lzma_block_header_size(&thr->block_options);
-	if (ret != LZMA_OK) {
-		worker_error(thr, ret);
-		return THR_STOP;
-	}
-
-	// Initialize the Block encoder.
-	ret = lzma_block_encoder_init(&thr->block_encoder,
-			thr->allocator, &thr->block_options);
-	if (ret != LZMA_OK) {
-		worker_error(thr, ret);
-		return THR_STOP;
-	}
-
-	size_t in_pos = 0;
-	size_t in_size = 0;
-
-	thr->outbuf->size = thr->block_options.header_size;
-	const size_t out_size = thr->coder->outq.buf_size_max;
-
-	do {
-		mythread_sync(thr->mutex) {
-			while (in_size == thr->in_size
-					&& thr->state == THR_RUN)
-				pthread_cond_wait(&thr->cond, &thr->mutex);
-
-			state = thr->state;
-			in_size = thr->in_size;
-
-			// TODO? Store in_pos and out_pos into *thr here
-			// so that the application may read them via
-			// some currently non-existing function to get
-			// progress information.
-		}
-
-		// Return if we were asked to stop or exit.
-		if (state >= THR_STOP)
-			return state;
-
-		lzma_action action = state == THR_FINISH
-				? LZMA_FINISH : LZMA_RUN;
-
-		// Limit the amount of input given to the Block encoder
-		// at once. This way this thread can react fairly quickly
-		// if the main thread wants us to stop or exit.
-		static const size_t in_chunk_max = 16384;
-		size_t in_limit = in_size;
-		if (in_size - in_pos > in_chunk_max) {
-			in_limit = in_pos + in_chunk_max;
-			action = LZMA_RUN;
-		}
-
-		ret = thr->block_encoder.code(
-				thr->block_encoder.coder, thr->allocator,
-				thr->in, &in_pos, in_limit, thr->outbuf->buf,
-				&thr->outbuf->size, out_size, action);
-	} while (ret == LZMA_OK);
-
-	if (ret != LZMA_STREAM_END) {
-		worker_error(thr, ret);
-		return THR_STOP;
-	}
-
-	assert(state == THR_FINISH);
-
-	// Encode the Block Header. By doing it after the compression,
-	// we can store the Compressed Size and Uncompressed Size fields.
-	ret = lzma_block_header_encode(&thr->block_options, thr->outbuf->buf);
-	if (ret != LZMA_OK) {
-		worker_error(thr, ret);
-		return THR_STOP;
-	}
-
-	// Set the size information that will be read by the main thread
-	// to write the Index field.
-	thr->outbuf->unpadded_size
-			= lzma_block_unpadded_size(&thr->block_options);
-	assert(thr->outbuf->unpadded_size != 0);
-	thr->outbuf->uncompressed_size = thr->block_options.uncompressed_size;
-
-	return THR_FINISH;
-}
-
-
-static void *
-worker_start(void *thr_ptr)
-{
-	worker_thread *thr = thr_ptr;
-	worker_state state = THR_IDLE; // Init to silence a warning
-
-	while (true) {
-		// Wait for work.
-		mythread_sync(thr->mutex) {
-			while (true) {
-				// The thread is already idle so if we are
-				// requested to stop, just set the state.
-				if (thr->state == THR_STOP) {
-					thr->state = THR_IDLE;
-					pthread_cond_signal(&thr->cond);
-				}
-
-				state = thr->state;
-				if (state != THR_IDLE)
-					break;
-
-				pthread_cond_wait(&thr->cond, &thr->mutex);
-			}
-		}
-
-		assert(state != THR_IDLE);
-		assert(state != THR_STOP);
-
-		if (state <= THR_FINISH)
-			state = worker_encode(thr, state);
-
-		if (state == THR_EXIT)
-			break;
-
-		// Mark the thread as idle. Signal is needed for the case
-		// where the main thread is waiting for the threads to stop.
-		mythread_sync(thr->mutex) {
-			thr->state = THR_IDLE;
-			pthread_cond_signal(&thr->cond);
-		}
-
-		mythread_sync(thr->coder->mutex) {
-			// Mark the output buffer as finished if
-			// no errors occurred.
-			thr->outbuf->finished = state == THR_FINISH;
-
-			// Return this thread to the stack of free threads.
-			thr->next = thr->coder->threads_free;
-			thr->coder->threads_free = thr;
-
-			mythread_cond_signal(&thr->coder->cond);
-		}
-	}
-
-	// Exiting, free the resources.
-	pthread_mutex_destroy(&thr->mutex);
-	pthread_cond_destroy(&thr->cond);
-
-	lzma_next_end(&thr->block_encoder, thr->allocator);
-	lzma_free(thr->in, thr->allocator);
-	return NULL;
-}
-
-
-/// Make the threads stop but not exit. Optionally wait for them to stop.
-static void
-threads_stop(lzma_coder *coder, bool wait)
-{
-	// Tell the threads to stop.
-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
-		mythread_sync(coder->threads[i].mutex) {
-			coder->threads[i].state = THR_STOP;
-			pthread_cond_signal(&coder->threads[i].cond);
-		}
-	}
-
-	if (!wait)
-		return;
-
-	// Wait for the threads to settle in the idle state.
-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
-		mythread_sync(coder->threads[i].mutex) {
-			while (coder->threads[i].state != THR_IDLE)
-				pthread_cond_wait(&coder->threads[i].cond,
-						&coder->threads[i].mutex);
-		}
-	}
-
-	return;
-}
-
-
-/// Stop the threads and free the resources associated with them.
-/// Wait until the threads have exited.
-static void
-threads_end(lzma_coder *coder, lzma_allocator *allocator)
-{
-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
-		mythread_sync(coder->threads[i].mutex) {
-			coder->threads[i].state = THR_EXIT;
-			pthread_cond_signal(&coder->threads[i].cond);
-		}
-	}
-
-	for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
-		int ret = pthread_join(coder->threads[i].thread_id, NULL);
-		assert(ret == 0);
-		(void)ret;
-	}
-
-	lzma_free(coder->threads, allocator);
-	return;
-}
-
-
-/// Initialize a new worker_thread structure and create a new thread.
-static lzma_ret
-initialize_new_thread(lzma_coder *coder, lzma_allocator *allocator)
-{
-	worker_thread *thr = &coder->threads[coder->threads_initialized];
-
-	thr->in = lzma_alloc(coder->block_size, allocator);
-	if (thr->in == NULL)
-		return LZMA_MEM_ERROR;
-
-	if (pthread_mutex_init(&thr->mutex, NULL))
-		goto error_mutex;
-
-	if (pthread_cond_init(&thr->cond, NULL))
-		goto error_cond;
-
-	thr->state = THR_IDLE;
-	thr->allocator = allocator;
-	thr->coder = coder;
-	thr->block_encoder = LZMA_NEXT_CODER_INIT;
-
-	if (mythread_create(&thr->thread_id, &worker_start, thr))
-		goto error_thread;
-
-	++coder->threads_initialized;
-	coder->thr = thr;
-
-	return LZMA_OK;
-
-error_thread:
-	pthread_cond_destroy(&thr->cond);
-
-error_cond:
-	pthread_mutex_destroy(&thr->mutex);
-
-error_mutex:
-	lzma_free(thr->in, allocator);
-	return LZMA_MEM_ERROR;
-}
-
-
-static lzma_ret
-get_thread(lzma_coder *coder, lzma_allocator *allocator)
-{
-	// If there are no free output subqueues, there is no
-	// point to try getting a thread.
-	if (!lzma_outq_has_buf(&coder->outq))
-		return LZMA_OK;
-
-	// If there is a free structure on the stack, use it.
-	mythread_sync(coder->mutex) {
-		if (coder->threads_free != NULL) {
-			coder->thr = coder->threads_free;
-			coder->threads_free = coder->threads_free->next;
-		}
-	}
-
-	if (coder->thr == NULL) {
-		// If there are no uninitialized structures left, return.
-		if (coder->threads_initialized == coder->threads_max)
-			return LZMA_OK;
-
-		// Initialize a new thread.
-		return_if_error(initialize_new_thread(coder, allocator));
-	}
-
-	// Reset the parts of the thread state that have to be done
-	// in the main thread.
-	mythread_sync(coder->thr->mutex) {
-		coder->thr->state = THR_RUN;
-		coder->thr->in_size = 0;
-		coder->thr->outbuf = lzma_outq_get_buf(&coder->outq);
-		pthread_cond_signal(&coder->thr->cond);
-	}
-
-	return LZMA_OK;
-}
-
-
-static lzma_ret
-stream_encode_in(lzma_coder *coder, lzma_allocator *allocator,
-		const uint8_t *restrict in, size_t *restrict in_pos,
-		size_t in_size, lzma_action action)
-{
-	while (*in_pos < in_size
-			|| (coder->thr != NULL && action != LZMA_RUN)) {
-		if (coder->thr == NULL) {
-			// Get a new thread.
-			const lzma_ret ret = get_thread(coder, allocator);
-			if (coder->thr == NULL)
-				return ret;
-		}
-
-		// Copy the input data to thread's buffer.
-		size_t thr_in_size = coder->thr->in_size;
-		lzma_bufcpy(in, in_pos, in_size, coder->thr->in,
-				&thr_in_size, coder->block_size);
-
-		// Tell the Block encoder to finish if
-		//  - it has got block_size bytes of input; or
-		//  - all input was used and LZMA_FINISH, LZMA_FULL_FLUSH,
-		//    or LZMA_FULL_BARRIER was used.
-		//
-		// TODO: LZMA_SYNC_FLUSH and LZMA_SYNC_BARRIER.
-		const bool finish = thr_in_size == coder->block_size
-				|| (*in_pos == in_size && action != LZMA_RUN);
-
-		bool block_error = false;
-
-		mythread_sync(coder->thr->mutex) {
-			if (coder->thr->state == THR_IDLE) {
-				// Something has gone wrong with the Block
-				// encoder. It has set coder->thread_error
-				// which we will read a few lines later.
-				block_error = true;
-			} else {
-				// Tell the Block encoder its new amount
-				// of input and update the state if needed.
-				coder->thr->in_size = thr_in_size;
-
-				if (finish)
-					coder->thr->state = THR_FINISH;
-
-				pthread_cond_signal(&coder->thr->cond);
-			}
-		}
-
-		if (block_error) {
-			lzma_ret ret;
-
-			mythread_sync(coder->mutex) {
-				ret = coder->thread_error;
-			}
-
-			return ret;
-		}
-
-		if (finish)
-			coder->thr = NULL;
-	}
-
-	return LZMA_OK;
-}
-
-
-/// Wait until more input can be consumed, more output can be read, or
-/// an optional timeout is reached.
-static bool
-wait_for_work(lzma_coder *coder, struct timespec *wait_abs,
-		bool *has_blocked, bool has_input)
-{
-	if (coder->has_timeout && !*has_blocked) {
-		// Every time when stream_encode_mt() is called via
-		// lzma_code(), *has_block starts as false. We set it
-		// to true here and calculate the absolute time when
-		// we must return if there's nothing to do.
-		//
-		// The idea of *has_blocked is to avoid unneeded calls
-		// to mythread_cond_abstime(), which may do a syscall
-		// depending on the operating system.
-		*has_blocked = true;
-		*wait_abs = coder->wait_max;
-		mythread_cond_abstime(&coder->cond, wait_abs);
-	}
-
-	bool timed_out = false;
-
-	mythread_sync(coder->mutex) {
-		// There are four things that we wait. If one of them
-		// becomes possible, we return.
-		//  - If there is input left, we need to get a free
-		//    worker thread and an output buffer for it.
-		//  - Data ready to be read from the output queue.
-		//  - A worker thread indicates an error.
-		//  - Time out occurs.
-		while ((!has_input || coder->threads_free == NULL
-					|| !lzma_outq_has_buf(&coder->outq))
-				&& !lzma_outq_is_readable(&coder->outq)
-				&& coder->thread_error == LZMA_OK
-				&& !timed_out) {
-			if (coder->has_timeout)
-				timed_out = mythread_cond_timedwait(
-						&coder->cond, &coder->mutex,
-						wait_abs) != 0;
-			else
-				mythread_cond_wait(&coder->cond,
-						&coder->mutex);
-		}
-	}
-
-	return timed_out;
-}
-
-
-static lzma_ret
-stream_encode_mt(lzma_coder *coder, lzma_allocator *allocator,
-		const uint8_t *restrict in, size_t *restrict in_pos,
-		size_t in_size, uint8_t *restrict out,
-		size_t *restrict out_pos, size_t out_size, lzma_action action)
-{
-	switch (coder->sequence) {
-	case SEQ_STREAM_HEADER:
-		lzma_bufcpy(coder->header, &coder->header_pos,
-				sizeof(coder->header),
-				out, out_pos, out_size);
-		if (coder->header_pos < sizeof(coder->header))
-			return LZMA_OK;
-
-		coder->header_pos = 0;
-		coder->sequence = SEQ_BLOCK;
-
-	// Fall through
-
-	case SEQ_BLOCK: {
-		// Initialized to silence warnings.
-		lzma_vli unpadded_size = 0;
-		lzma_vli uncompressed_size = 0;
-		lzma_ret ret = LZMA_OK;
-
-		// These are for wait_for_work().
-		bool has_blocked = false;
-		struct timespec wait_abs;
-
-		while (true) {
-			mythread_sync(coder->mutex) {
-				// Check for Block encoder errors.
-				ret = coder->thread_error;
-				if (ret != LZMA_OK) {
-					assert(ret != LZMA_STREAM_END);
-					break;
-				}
-
-				// Try to read compressed data to out[].
-				ret = lzma_outq_read(&coder->outq,
-						out, out_pos, out_size,
-						&unpadded_size,
-						&uncompressed_size);
-			}
-
-			if (ret == LZMA_STREAM_END) {
-				// End of Block. Add it to the Index.
-				ret = lzma_index_append(coder->index,
-						allocator, unpadded_size,
-						uncompressed_size);
-
-				// If we didn't fill the output buffer yet,
-				// try to read more data. Maybe the next
-				// outbuf has been finished already too.
-				if (*out_pos < out_size)
-					continue;
-			}
-
-			if (ret != LZMA_OK) {
-				// coder->thread_error was set or
-				// lzma_index_append() failed.
-				threads_stop(coder, false);
-				return ret;
-			}
-
-			// Check if the last Block was finished.
-			if (action == LZMA_FINISH
-					&& *in_pos == in_size
-					&& lzma_outq_is_empty(
-						&coder->outq))
-				break;
-
-			// Try to give uncompressed data to a worker thread.
-			ret = stream_encode_in(coder, allocator,
-					in, in_pos, in_size, action);
-			if (ret != LZMA_OK) {
-				threads_stop(coder, false);
-				return ret;
-			}
-
-			// Return if
-			//  - we have used all the input and expect to
-			//    get more input; or
-			//  - the output buffer has been filled.
-			//
-			// TODO: Support flushing.
-			if ((*in_pos == in_size && action != LZMA_FINISH)
-					|| *out_pos == out_size)
-				return LZMA_OK;
-
-			// Neither in nor out has been used completely.
-			// Wait until there's something we can do.
-			if (wait_for_work(coder, &wait_abs, &has_blocked,
-					*in_pos < in_size))
-				return LZMA_TIMED_OUT;
-		}
-
-		// All Blocks have been encoded and the threads have stopped.
-		// Prepare to encode the Index field.
-		return_if_error(lzma_index_encoder_init(
-				&coder->index_encoder, allocator,
-				coder->index));
-		coder->sequence = SEQ_INDEX;
-	}
-
-	// Fall through
-
-	case SEQ_INDEX: {
-		// Call the Index encoder. It doesn't take any input, so
-		// those pointers can be NULL.
-		const lzma_ret ret = coder->index_encoder.code(
-				coder->index_encoder.coder, allocator,
-				NULL, NULL, 0,
-				out, out_pos, out_size, LZMA_RUN);
-		if (ret != LZMA_STREAM_END)
-			return ret;
-
-		// Encode the Stream Footer into coder->buffer.
-		coder->stream_flags.backward_size
-				= lzma_index_size(coder->index);
-		if (lzma_stream_footer_encode(&coder->stream_flags,
-				coder->header) != LZMA_OK)
-			return LZMA_PROG_ERROR;
-
-		coder->sequence = SEQ_STREAM_FOOTER;
-	}
-
-	// Fall through
-
-	case SEQ_STREAM_FOOTER:
-		lzma_bufcpy(coder->header, &coder->header_pos,
-				sizeof(coder->header),
-				out, out_pos, out_size);
-		return coder->header_pos < sizeof(coder->header)
-				? LZMA_OK : LZMA_STREAM_END;
-	}
-
-	assert(0);
-	return LZMA_PROG_ERROR;
-}
-
-
-static void
-stream_encoder_mt_end(lzma_coder *coder, lzma_allocator *allocator)
-{
-	// Threads must be killed before the output queue can be freed.
-	threads_end(coder, allocator);
-	lzma_outq_end(&coder->outq, allocator);
-
-	for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
-		lzma_free(coder->filters[i].options, allocator);
-
-	lzma_next_end(&coder->index_encoder, allocator);
-	lzma_index_end(coder->index, allocator);
-
-	mythread_cond_destroy(&coder->cond);
-	pthread_mutex_destroy(&coder->mutex);
-
-	lzma_free(coder, allocator);
-	return;
-}
-
-
-/// Options handling for lzma_stream_encoder_mt_init() and
-/// lzma_stream_encoder_mt_memusage()
-static lzma_ret
-get_options(const lzma_mt *options, lzma_options_easy *opt_easy,
-		const lzma_filter **filters, uint64_t *block_size,
-		uint64_t *outbuf_size_max)
-{
-	// Validate some of the options.
-	if (options == NULL)
-		return LZMA_PROG_ERROR;
-
-	if (options->flags != 0 || options->threads == 0
-			|| options->threads > LZMA_THREADS_MAX)
-		return LZMA_OPTIONS_ERROR;
-
-	if (options->filters != NULL) {
-		// Filter chain was given, use it as is.
-		*filters = options->filters;
-	} else {
-		// Use a preset.
-		if (lzma_easy_preset(opt_easy, options->preset))
-			return LZMA_OPTIONS_ERROR;
-
-		*filters = opt_easy->filters;
-	}
-
-	// Block size
-	if (options->block_size > 0) {
-		if (options->block_size > BLOCK_SIZE_MAX)
-			return LZMA_OPTIONS_ERROR;
-
-		*block_size = options->block_size;
-	} else {
-		// Determine the Block size from the filter chain.
-		*block_size = lzma_mt_block_size(*filters);
-		if (*block_size == 0)
-			return LZMA_OPTIONS_ERROR;
-
-		assert(*block_size <= BLOCK_SIZE_MAX);
-	}
-
-	// Calculate the maximum amount output that a single output buffer
-	// may need to hold. This is the same as the maximum total size of
-	// a Block.
-	//
-	// FIXME: As long as the encoder keeps the whole input buffer
-	// available and doesn't start writing output before finishing
-	// the Block, it could use lzma_stream_buffer_bound() and use
-	// uncompressed LZMA2 chunks if the data doesn't compress.
-	*outbuf_size_max = *block_size + *block_size / 16 + 16384;
-
-	return LZMA_OK;
-}
-
-
-static lzma_ret
-stream_encoder_mt_init(lzma_next_coder *next, lzma_allocator *allocator,
-		const lzma_mt *options)
-{
-	lzma_next_coder_init(&stream_encoder_mt_init, next, allocator);
-
-	// Get the filter chain.
-	lzma_options_easy easy;
-	const lzma_filter *filters;
-	uint64_t block_size;
-	uint64_t outbuf_size_max;
-	return_if_error(get_options(options, &easy, &filters,
-			&block_size, &outbuf_size_max));
-
-#if SIZE_MAX < UINT64_MAX
-	if (block_size > SIZE_MAX)
-		return LZMA_MEM_ERROR;
-#endif
-
-	// FIXME TODO: Validate the filter chain so that we can give
-	// an error in this function instead of delaying it to the first
-	// call to lzma_code().
-
-	// Validate the Check ID.
-	if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
-		return LZMA_PROG_ERROR;
-
-	if (!lzma_check_is_supported(options->check))
-		return LZMA_UNSUPPORTED_CHECK;
-
-	// Allocate and initialize the base structure if needed.
-	if (next->coder == NULL) {
-		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
-		if (next->coder == NULL)
-			return LZMA_MEM_ERROR;
-
-		// For the mutex and condition variable initializations
-		// the error handling has to be done here because
-		// stream_encoder_mt_end() doesn't know if they have
-		// already been initialized or not.
-		if (pthread_mutex_init(&next->coder->mutex, NULL)) {
-			lzma_free(next->coder, allocator);
-			next->coder = NULL;
-			return LZMA_MEM_ERROR;
-		}
-
-		if (mythread_cond_init(&next->coder->cond)) {
-			pthread_mutex_destroy(&next->coder->mutex);
-			lzma_free(next->coder, allocator);
-			next->coder = NULL;
-			return LZMA_MEM_ERROR;
-		}
-
-		next->code = &stream_encode_mt;
-		next->end = &stream_encoder_mt_end;
-// 		next->update = &stream_encoder_mt_update;
-
-		next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
-		next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
-		next->coder->index = NULL;
-		memzero(&next->coder->outq, sizeof(next->coder->outq));
-		next->coder->threads = NULL;
-		next->coder->threads_max = 0;
-		next->coder->threads_initialized = 0;
-	}
-
-	// Basic initializations
-	next->coder->sequence = SEQ_STREAM_HEADER;
-	next->coder->block_size = (size_t)(block_size);
-	next->coder->thread_error = LZMA_OK;
-	next->coder->thr = NULL;
-
-	// Allocate the thread-specific base structures.
-	assert(options->threads > 0);
-	if (next->coder->threads_max != options->threads) {
-		threads_end(next->coder, allocator);
-
-		next->coder->threads = NULL;
-		next->coder->threads_max = 0;
-
-		next->coder->threads_initialized = 0;
-		next->coder->threads_free = NULL;
-
-		next->coder->threads = lzma_alloc(
-				options->threads * sizeof(worker_thread),
-				allocator);
-		if (next->coder->threads == NULL)
-			return LZMA_MEM_ERROR;
-
-		next->coder->threads_max = options->threads;
-	} else {
-		// Reuse the old structures and threads. Tell the running
-		// threads to stop and wait until they have stopped.
-		threads_stop(next->coder, true);
-	}
-
-	// Output queue
-	return_if_error(lzma_outq_init(&next->coder->outq, allocator,
-			outbuf_size_max, options->threads));
-
-	// Timeout
-	if (options->timeout > 0) {
-		next->coder->wait_max.tv_sec = options->timeout / 1000;
-		next->coder->wait_max.tv_nsec
-				= (options->timeout % 1000) * 1000000L;
-		next->coder->has_timeout = true;
-	} else {
-		next->coder->has_timeout = false;
-	}
-
-	// Free the old filter chain and copy the new one.
-	for (size_t i = 0; next->coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
-		lzma_free(next->coder->filters[i].options, allocator);
-
-	return_if_error(lzma_filters_copy(options->filters,
-			next->coder->filters, allocator));
-
-	// Index
-	lzma_index_end(next->coder->index, allocator);
-	next->coder->index = lzma_index_init(allocator);
-	if (next->coder->index == NULL)
-		return LZMA_MEM_ERROR;
-
-	// Stream Header
-	next->coder->stream_flags.version = 0;
-	next->coder->stream_flags.check = options->check;
-	return_if_error(lzma_stream_header_encode(
-			&next->coder->stream_flags, next->coder->header));
-
-	next->coder->header_pos = 0;
-
-	return LZMA_OK;
-}
-
-
-extern LZMA_API(lzma_ret)
-lzma_stream_encoder_mt(lzma_stream *strm, const lzma_mt *options)
-{
-	lzma_next_strm_init(stream_encoder_mt_init, strm, options);
-
-	strm->internal->supported_actions[LZMA_RUN] = true;
-// 	strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
-// 	strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
-// 	strm->internal->supported_actions[LZMA_FULL_BARRIER] = true;
-	strm->internal->supported_actions[LZMA_FINISH] = true;
-
-	return LZMA_OK;
-}
-
-
-// This function name is a monster but it's consistent with the older
-// monster names. :-( 31 chars is the max that C99 requires so in that
-// sense it's not too long. ;-)
-extern LZMA_API(uint64_t)
-lzma_stream_encoder_mt_memusage(const lzma_mt *options)
-{
-	lzma_options_easy easy;
-	const lzma_filter *filters;
-	uint64_t block_size;
-	uint64_t outbuf_size_max;
-
-	if (get_options(options, &easy, &filters, &block_size,
-			&outbuf_size_max) != LZMA_OK)
-		return UINT64_MAX;
-
-	// Memory usage of the input buffers
-	const uint64_t inbuf_memusage = options->threads * block_size;
-
-	// Memory usage of the filter encoders
-	uint64_t filters_memusage
-			= lzma_raw_encoder_memusage(options->filters);
-	if (filters_memusage == UINT64_MAX)
-		return UINT64_MAX;
-
-	filters_memusage *= options->threads;
-
-	// Memory usage of the output queue
-	const uint64_t outq_memusage = lzma_outq_memusage(
-			outbuf_size_max, options->threads);
-	if (outq_memusage == UINT64_MAX)
-		return UINT64_MAX;
-
-	// Sum them with overflow checking.
-	uint64_t total_memusage = LZMA_MEMUSAGE_BASE + sizeof(lzma_coder)
-			+ options->threads * sizeof(worker_thread);
-
-	if (UINT64_MAX - total_memusage < inbuf_memusage)
-		return UINT64_MAX;
-
-	total_memusage += inbuf_memusage;
-
-	if (UINT64_MAX - total_memusage < filters_memusage)
-		return UINT64_MAX;
-
-	total_memusage += filters_memusage;
-
-	if (UINT64_MAX - total_memusage < outq_memusage)
-		return UINT64_MAX;
-
-	return total_memusage + outq_memusage;
-}
diff --git a/src/liblzma/liblzma.map b/src/liblzma/liblzma.map
index 835eb26..47a7c22 100644
--- a/src/liblzma/liblzma.map
+++ b/src/liblzma/liblzma.map
@@ -93,13 +93,7 @@
 	lzma_vli_decode;
 	lzma_vli_encode;
 	lzma_vli_size;
-};
-
-XZ_5.1.1alpha {
-global:
-	lzma_stream_encoder_mt;
-	lzma_stream_encoder_mt_memusage;
 
 local:
 	*;
-} XZ_5.0;
+};
diff --git a/src/xz/args.c b/src/xz/args.c
index 54b3ff3..1b88c1e 100644
--- a/src/xz/args.c
+++ b/src/xz/args.c
@@ -179,9 +179,8 @@
 			break;
 
 		case 'T':
-			// The max is from src/liblzma/common/common.h.
-			hardware_threads_set(str_to_uint64("threads",
-					optarg, 0, 16384));
+			hardware_threadlimit_set(str_to_uint64(
+					"threads", optarg, 0, UINT32_MAX));
 			break;
 
 		// --version
diff --git a/src/xz/coder.c b/src/xz/coder.c
index 2ed88cd..a3366d0 100644
--- a/src/xz/coder.c
+++ b/src/xz/coder.c
@@ -55,14 +55,6 @@
 /// This becomes false if the --check=CHECK option is used.
 static bool check_default = true;
 
-#ifdef HAVE_PTHREAD
-static lzma_mt mt_options = {
-	.flags = 0,
-	.timeout = 300,
-	.filters = filters,
-};
-#endif
-
 
 extern void
 coder_set_check(lzma_check new_check)
@@ -125,15 +117,6 @@
 extern void
 coder_set_compression_settings(void)
 {
-	// The default check type is CRC64, but fallback to CRC32
-	// if CRC64 isn't supported by the copy of liblzma we are
-	// using. CRC32 is always supported.
-	if (check_default) {
-		check = LZMA_CHECK_CRC64;
-		if (!lzma_check_is_supported(check))
-			check = LZMA_CHECK_CRC32;
-	}
-
 	// Options for LZMA1 or LZMA2 in case we are using a preset.
 	static lzma_options_lzma opt_lzma;
 
@@ -187,30 +170,15 @@
 	// Print the selected filter chain.
 	message_filters_show(V_DEBUG, filters);
 
-	// Get the memory usage. Note that if --format=raw was used,
-	// we can be decompressing.
+	// If using --format=raw, we can be decoding. The memusage function
+	// also validates the filter chain and the options used for the
+	// filters.
 	const uint64_t memory_limit = hardware_memlimit_get(opt_mode);
 	uint64_t memory_usage;
-	if (opt_mode == MODE_COMPRESS) {
-#ifdef HAVE_PTHREAD
-		if (opt_format == FORMAT_XZ && hardware_threads_get() > 1) {
-			mt_options.threads = hardware_threads_get();
-			mt_options.block_size = opt_block_size;
-			mt_options.check = check;
-			memory_usage = lzma_stream_encoder_mt_memusage(
-					&mt_options);
-			if (memory_usage != UINT64_MAX)
-				message(V_DEBUG, _("Using up to %" PRIu32
-						" threads."),
-						mt_options.threads);
-		} else
-#endif
-		{
-			memory_usage = lzma_raw_encoder_memusage(filters);
-		}
-	} else {
+	if (opt_mode == MODE_COMPRESS)
+		memory_usage = lzma_raw_encoder_memusage(filters);
+	else
 		memory_usage = lzma_raw_decoder_memusage(filters);
-	}
 
 	if (memory_usage == UINT64_MAX)
 		message_fatal(_("Unsupported filter chain or filter options"));
@@ -226,99 +194,90 @@
 						round_up_to_mib(decmem), 0));
 	}
 
-	if (memory_usage <= memory_limit)
-		return;
+	if (memory_usage > memory_limit) {
+		// If --no-auto-adjust was used or we didn't find LZMA1 or
+		// LZMA2 as the last filter, give an error immediately.
+		// --format=raw implies --no-auto-adjust.
+		if (!opt_auto_adjust || opt_format == FORMAT_RAW)
+			memlimit_too_small(memory_usage);
 
-	// If --no-auto-adjust was used or we didn't find LZMA1 or
-	// LZMA2 as the last filter, give an error immediately.
-	// --format=raw implies --no-auto-adjust.
-	if (!opt_auto_adjust || opt_format == FORMAT_RAW)
-		memlimit_too_small(memory_usage);
+		assert(opt_mode == MODE_COMPRESS);
 
-	assert(opt_mode == MODE_COMPRESS);
-
-#ifdef HAVE_PTHREAD
-	if (opt_format == FORMAT_XZ && mt_options.threads > 1) {
-		// Try to reduce the number of threads before
-		// adjusting the compression settings down.
-		do {
-			// FIXME? The real single-threaded mode has
-			// lower memory usage, but it's not comparable
-			// because it doesn't write the size info
-			// into Block Headers.
-			if (--mt_options.threads == 0)
+		// Look for the last filter if it is LZMA2 or LZMA1, so
+		// we can make it use less RAM. With other filters we don't
+		// know what to do.
+		size_t i = 0;
+		while (filters[i].id != LZMA_FILTER_LZMA2
+				&& filters[i].id != LZMA_FILTER_LZMA1) {
+			if (filters[i].id == LZMA_VLI_UNKNOWN)
 				memlimit_too_small(memory_usage);
 
-			memory_usage = lzma_stream_encoder_mt_memusage(
-					&mt_options);
+			++i;
+		}
+
+		// Decrease the dictionary size until we meet the memory
+		// usage limit. First round down to full mebibytes.
+		lzma_options_lzma *opt = filters[i].options;
+		const uint32_t orig_dict_size = opt->dict_size;
+		opt->dict_size &= ~((UINT32_C(1) << 20) - 1);
+		while (true) {
+			// If it is below 1 MiB, auto-adjusting failed. We
+			// could be more sophisticated and scale it down even
+			// more, but let's see if many complain about this
+			// version.
+			//
+			// FIXME: Displays the scaled memory usage instead
+			// of the original.
+			if (opt->dict_size < (UINT32_C(1) << 20))
+				memlimit_too_small(memory_usage);
+
+			memory_usage = lzma_raw_encoder_memusage(filters);
 			if (memory_usage == UINT64_MAX)
 				message_bug();
 
-		} while (memory_usage > memory_limit);
+			// Accept it if it is low enough.
+			if (memory_usage <= memory_limit)
+				break;
 
-		message(V_WARNING, _("Adjusted the number of threads "
-			"from %s to %s to not exceed "
-			"the memory usage limit of %s MiB"),
-			uint64_to_str(hardware_threads_get(), 0),
-			uint64_to_str(mt_options.threads, 1),
-			uint64_to_str(round_up_to_mib(
-				memory_limit), 2));
-	}
-#endif
+			// Otherwise 1 MiB down and try again. I hope this
+			// isn't too slow method for cases where the original
+			// dict_size is very big.
+			opt->dict_size -= UINT32_C(1) << 20;
+		}
 
-	if (memory_usage <= memory_limit)
-		return;
-
-	// Look for the last filter if it is LZMA2 or LZMA1, so we can make
-	// it use less RAM. With other filters we don't know what to do.
-	size_t i = 0;
-	while (filters[i].id != LZMA_FILTER_LZMA2
-			&& filters[i].id != LZMA_FILTER_LZMA1) {
-		if (filters[i].id == LZMA_VLI_UNKNOWN)
-			memlimit_too_small(memory_usage);
-
-		++i;
+		// Tell the user that we decreased the dictionary size.
+		message(V_WARNING, _("Adjusted LZMA%c dictionary size "
+				"from %s MiB to %s MiB to not exceed "
+				"the memory usage limit of %s MiB"),
+				filters[i].id == LZMA_FILTER_LZMA2
+					? '2' : '1',
+				uint64_to_str(orig_dict_size >> 20, 0),
+				uint64_to_str(opt->dict_size >> 20, 1),
+				uint64_to_str(round_up_to_mib(
+					memory_limit), 2));
 	}
 
-	// Decrease the dictionary size until we meet the memory
-	// usage limit. First round down to full mebibytes.
-	lzma_options_lzma *opt = filters[i].options;
-	const uint32_t orig_dict_size = opt->dict_size;
-	opt->dict_size &= ~((UINT32_C(1) << 20) - 1);
-	while (true) {
-		// If it is below 1 MiB, auto-adjusting failed. We could be
-		// more sophisticated and scale it down even more, but let's
-		// see if many complain about this version.
-		//
-		// FIXME: Displays the scaled memory usage instead
-		// of the original.
-		if (opt->dict_size < (UINT32_C(1) << 20))
-			memlimit_too_small(memory_usage);
+/*
+	// Limit the number of worker threads so that memory usage
+	// limit isn't exceeded.
+	assert(memory_usage > 0);
+	size_t thread_limit = memory_limit / memory_usage;
+	if (thread_limit == 0)
+		thread_limit = 1;
 
-		memory_usage = lzma_raw_encoder_memusage(filters);
-		if (memory_usage == UINT64_MAX)
-			message_bug();
+	if (opt_threads > thread_limit)
+		opt_threads = thread_limit;
+*/
 
-		// Accept it if it is low enough.
-		if (memory_usage <= memory_limit)
-			break;
-
-		// Otherwise 1 MiB down and try again. I hope this
-		// isn't too slow method for cases where the original
-		// dict_size is very big.
-		opt->dict_size -= UINT32_C(1) << 20;
+	if (check_default) {
+		// The default check type is CRC64, but fallback to CRC32
+		// if CRC64 isn't supported by the copy of liblzma we are
+		// using. CRC32 is always supported.
+		check = LZMA_CHECK_CRC64;
+		if (!lzma_check_is_supported(check))
+			check = LZMA_CHECK_CRC32;
 	}
 
-	// Tell the user that we decreased the dictionary size.
-	message(V_WARNING, _("Adjusted LZMA%c dictionary size "
-			"from %s MiB to %s MiB to not exceed "
-			"the memory usage limit of %s MiB"),
-			filters[i].id == LZMA_FILTER_LZMA2
-				? '2' : '1',
-			uint64_to_str(orig_dict_size >> 20, 0),
-			uint64_to_str(opt->dict_size >> 20, 1),
-			uint64_to_str(round_up_to_mib(memory_limit), 2));
-
 	return;
 }
 
@@ -400,14 +359,7 @@
 			break;
 
 		case FORMAT_XZ:
-#ifdef HAVE_PTHREAD
-			if (hardware_threads_get() > 1)
-				ret = lzma_stream_encoder_mt(
-						&strm, &mt_options);
-			else
-#endif
-				ret = lzma_stream_encoder(
-						&strm, filters, check);
+			ret = lzma_stream_encoder(&strm, filters, check);
 			break;
 
 		case FORMAT_LZMA:
@@ -528,8 +480,8 @@
 	// to the .xz format. If block_remaining == UINT64_MAX, only
 	// a single block is created.
 	uint64_t block_remaining = UINT64_MAX;
-	if (hardware_threads_get() == 1 && opt_mode == MODE_COMPRESS
-			&& opt_format == FORMAT_XZ && opt_block_size > 0)
+	if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_XZ
+			&& opt_block_size > 0)
 		block_remaining = opt_block_size;
 
 	strm.next_out = out_buf.u8;
diff --git a/src/xz/hardware.c b/src/xz/hardware.c
index 925926c..a4733c2 100644
--- a/src/xz/hardware.c
+++ b/src/xz/hardware.c
@@ -14,9 +14,9 @@
 #include "tuklib_cpucores.h"
 
 
-/// Maximum number of worker threads. This can be set with
+/// Maximum number of free *coder* threads. This can be set with
 /// the --threads=NUM command line option.
-static uint32_t threads_max = 1;
+static uint32_t threadlimit;
 
 /// Memory usage limit for compression
 static uint64_t memlimit_compress;
@@ -29,16 +29,15 @@
 
 
 extern void
-hardware_threads_set(uint32_t n)
+hardware_threadlimit_set(uint32_t new_threadlimit)
 {
-	if (n == 0) {
-		// Automatic number of threads was requested.
-		// Use the number of available CPU cores.
-		threads_max = tuklib_cpucores();
-		if (threads_max == 0)
-			threads_max = 1;
+	if (new_threadlimit == 0) {
+		// The default is the number of available CPU cores.
+		threadlimit = tuklib_cpucores();
+		if (threadlimit == 0)
+			threadlimit = 1;
 	} else {
-		threads_max = n;
+		threadlimit = new_threadlimit;
 	}
 
 	return;
@@ -46,9 +45,9 @@
 
 
 extern uint32_t
-hardware_threads_get(void)
+hardware_threadlimit_get(void)
 {
-	return threads_max;
+	return threadlimit;
 }
 
 
@@ -140,5 +139,6 @@
 
 	// Set the defaults.
 	hardware_memlimit_set(0, true, true, false);
+	hardware_threadlimit_set(0);
 	return;
 }
diff --git a/src/xz/hardware.h b/src/xz/hardware.h
index 4fae618..ad526f2 100644
--- a/src/xz/hardware.h
+++ b/src/xz/hardware.h
@@ -15,11 +15,12 @@
 extern void hardware_init(void);
 
 
-/// Set the maximum number of worker threads.
-extern void hardware_threads_set(uint32_t threadlimit);
+/// Set custom value for maximum number of coder threads.
+extern void hardware_threadlimit_set(uint32_t threadlimit);
 
-/// Get the maximum number of worker threads.
-extern uint32_t hardware_threads_get(void);
+/// Get the maximum number of coder threads. Some additional helper threads
+/// are allowed on top of this).
+extern uint32_t hardware_threadlimit_get(void);
 
 
 /// Set the memory usage limit. There are separate limits for compression
diff --git a/src/xz/private.h b/src/xz/private.h
index 978f81a..6b01e51 100644
--- a/src/xz/private.h
+++ b/src/xz/private.h
@@ -12,8 +12,6 @@
 
 #include "sysdefs.h"
 #include "mythread.h"
-
-#define LZMA_UNSTABLE
 #include "lzma.h"
 
 #include <sys/types.h>
diff --git a/src/xz/xz.1 b/src/xz/xz.1
index 4da09ba..0952f2d 100644
--- a/src/xz/xz.1
+++ b/src/xz/xz.1
@@ -907,30 +907,24 @@
 .TP
 \fB\-T\fR \fIthreads\fR, \fB\-\-threads=\fIthreads
 Specify the number of worker threads to use.
-Setting
-.I threads
-to a special value
-.B 0
-makes
-.B xz
-use as many threads as there are CPU cores on the system.
 The actual number of threads can be less than
 .I threads
-if the input file is not big enough
-for threading with the given settings or
 if using more threads would exceed the memory usage limit.
 .IP ""
-Currently the only threading method is to split the input into
-blocks and compress them independently from each other.
-The default block size depends on the compression level and
-can be overriden with the
-.BI \-\-block\-size= size
-option.
+.B "Multithreaded compression and decompression are not"
+.B "implemented yet, so this option has no effect for now."
 .IP ""
-.B "It is possible that the details of this option change before"
-.B "the next stable XZ Utils release."
-.B "This may include the meaning of the special value 0."
-.\" FIXME
+.B "As of writing (2010-09-27), it hasn't been decided"
+.B "if threads will be used by default on multicore systems"
+.B "once support for threading has been implemented."
+.B "Comments are welcome."
+The complicating factor is that using many threads
+will increase the memory usage dramatically.
+Note that if multithreading will be the default,
+it will probably be done so that single-threaded and
+multithreaded modes produce the same output,
+so compression ratio won't be significantly affected
+if threading will be enabled by default.
 .
 .SS "Custom compressor filter chains"
 A custom filter chain allows specifying