diff --git a/debian/control b/debian/control
index 5673839..4bc0c07 100644
--- a/debian/control
+++ b/debian/control
@@ -19,3 +19,21 @@ Description: Default and general compression method of 7z format in 7-Zip progra
  LZMA provides high compression ratio and very fast decompression, so it
  is very suitable for embedded applications. For example, it can be used
  for ROM (firmware) compression.
+
+Package: lzma-source
+Architecture: all
+Depends: module-assistant, debhelper (>> 4.0.0), bzip2, make
+Description: Source for the lzma kernel module
+ LZMA is a compression algorithm, based on the famous Lempel Ziv
+ compression method.
+ .
+ The main characteristics of the algorithm are very good compression,
+ fast decompression, use of lot of RAM for compression and low usage of
+ RAM for decompression.
+ .
+ LZMA provides high compression ratio and very fast decompression, so it
+ is very suitable for embedded applications. For example, it can be used
+ for ROM (firmware) compression.
+ .
+ This package provides the source code for the lzma kernel modules.
+ Kernel source and headers are required to compile these modules.
diff --git a/debian/control.modules.in b/debian/control.modules.in
new file mode 100644
index 0000000..0d8daad
--- /dev/null
+++ b/debian/control.modules.in
@@ -0,0 +1,24 @@
+Source: lzma
+Section: utils
+Priority: optional
+Maintainer: Mohammed Adnčne Trojette <adn+deb@diwi.org>
+Build-Depends: debhelper (>= 4.0.0), linux-headers-_KVERS_
+Standards-Version: 3.7.2
+
+Package: lzma-modules-_KVERS_
+Architecture: any
+Provides: lzma-modules
+Depends: linux-modules-_KVERS_ | linux-image-_KVERS_
+Description: Source of the lzma kernel module
+ LZMA is a compression algorithm, based on the famous Lempel Ziv
+ compression method.
+ .
+ The main characteristics of the algorithm are very good compression,
+ fast decompression, use of lot of RAM for compression and low usage of
+ RAM for decompression.
+ .
+ LZMA provides high compression ratio and very fast decompression, so it
+ is very suitable for embedded applications. For example, it can be used
+ for ROM (firmware) compression.
+ .
+ This package contains the compiled kernel module for _KVERS_
diff --git a/debian/dirs b/debian/dirs
deleted file mode 100644
index e772481..0000000
--- a/debian/dirs
+++ /dev/null
@@ -1 +0,0 @@
-usr/bin
diff --git a/debian/docs b/debian/docs
deleted file mode 100644
index 98a7da9..0000000
--- a/debian/docs
+++ /dev/null
@@ -1,6 +0,0 @@
-7zC.txt
-7zFormat.txt
-history.txt
-LGPL.txt
-lzma.txt
-Methods.txt
diff --git a/debian/install b/debian/install
deleted file mode 100644
index 46c2d8c..0000000
--- a/debian/install
+++ /dev/null
@@ -1,2 +0,0 @@
-C/7zip/Compress/LZMA_Alone/lzma usr/bin
-C/7zip/Compress/LZMA_Alone/lzma_alone usr/bin
diff --git a/debian/links b/debian/links
deleted file mode 100644
index 4327da6..0000000
--- a/debian/links
+++ /dev/null
@@ -1,4 +0,0 @@
-usr/bin/lzma usr/bin/unlzma
-usr/bin/lzma usr/bin/lzcat
-usr/share/man/man1/lzma.1.gz usr/share/man/man1/unlzma.1.gz
-usr/share/man/man1/lzma.1.gz usr/share/man/man1/lzcat.1.gz
diff --git a/debian/lzma.dirs b/debian/lzma.dirs
new file mode 100644
index 0000000..e772481
--- /dev/null
+++ b/debian/lzma.dirs
@@ -0,0 +1 @@
+usr/bin
diff --git a/debian/lzma.docs b/debian/lzma.docs
new file mode 100644
index 0000000..98a7da9
--- /dev/null
+++ b/debian/lzma.docs
@@ -0,0 +1,6 @@
+7zC.txt
+7zFormat.txt
+history.txt
+LGPL.txt
+lzma.txt
+Methods.txt
diff --git a/debian/lzma.install b/debian/lzma.install
new file mode 100644
index 0000000..46c2d8c
--- /dev/null
+++ b/debian/lzma.install
@@ -0,0 +1,2 @@
+C/7zip/Compress/LZMA_Alone/lzma usr/bin
+C/7zip/Compress/LZMA_Alone/lzma_alone usr/bin
diff --git a/debian/lzma.links b/debian/lzma.links
new file mode 100644
index 0000000..4327da6
--- /dev/null
+++ b/debian/lzma.links
@@ -0,0 +1,4 @@
+usr/bin/lzma usr/bin/unlzma
+usr/bin/lzma usr/bin/lzcat
+usr/share/man/man1/lzma.1.gz usr/share/man/man1/unlzma.1.gz
+usr/share/man/man1/lzma.1.gz usr/share/man/man1/lzcat.1.gz
diff --git a/debian/patches/04_sqlzma_h.diff b/debian/patches/04_sqlzma_h.diff
new file mode 100644
index 0000000..25ad179
--- /dev/null
+++ b/debian/patches/04_sqlzma_h.diff
@@ -0,0 +1,82 @@
+--- /dev/null	2007-10-03 00:21:41.724022693 +0100
++++ lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.h	2007-10-06 02:52:55.000000000 +0100
+@@ -0,0 +1,79 @@
++/*
++ * Copyright (C) 2006 Junjiro Okajima
++ * Copyright (C) 2006 Tomas Matejicek, slax.org
++ *
++ * LICENSE follows the described one in lzma.
++ */
++
++/* $Id: sqlzma.h,v 1.13 2007/01/07 15:12:48 jro Exp $ */
++
++#ifndef __sqlzma_h__
++#define __sqlzma_h__
++
++#ifndef __KERNEL__
++#include <stdlib.h>
++#include <string.h>
++#include <zlib.h>
++#ifdef _REENTRANT
++#include <pthread.h>
++#endif
++#else
++#include <linux/zlib.h>
++#endif
++#define _7ZIP_BYTE_DEFINED
++
++/*
++ * detect the compression method automatically by the first byte of compressed
++ * data.
++ * according to rfc1950, the first byte of zlib compression must be 0x?8.
++ */
++#define is_lzma(c)	(c == 0x5d)
++
++/* ---------------------------------------------------------------------- */
++
++#ifdef __cplusplus
++extern "C" {
++#endif
++
++#ifndef __KERNEL__
++/* for mksquashfs only */
++int sqlzma_cm(int lzma, z_stream *stream, Bytef *next_in, uInt avail_in,
++	      Bytef *next_out, uInt avail_out);
++#endif
++
++/* ---------------------------------------------------------------------- */
++/*
++ * Three patterns for sqlzma uncompression. very dirty code.
++ * - kernel space (squashfs kernel module)
++ * - user space with pthread (mksquashfs)
++ * - user space without pthread (unsquashfs)
++ */
++
++struct sized_buf {
++	unsigned int	sz;
++	unsigned char	*buf;
++};
++
++enum {SQUN_PROB, SQUN_RESULT, SQUN_LAST};
++struct sqlzma_un {
++	int			un_lzma;
++	struct sized_buf	un_a[SQUN_LAST];
++	unsigned char		un_prob[31960]; /* unlzma 64KB */
++	z_stream		un_stream;
++#define un_cmbuf	un_stream.next_in
++#define un_cmlen	un_stream.avail_in
++#define un_resbuf	un_stream.next_out
++#define un_resroom	un_stream.avail_out
++#define un_reslen	un_stream.total_out
++};
++
++int sqlzma_init(struct sqlzma_un *un, int do_lzma, unsigned int res_sz);
++int sqlzma_un(struct sqlzma_un *un, struct sized_buf *src, struct sized_buf *dst);
++void sqlzma_fin(struct sqlzma_un *un);
++
++/* ---------------------------------------------------------------------- */
++
++#ifdef __cplusplus
++};
++#endif
++#endif
diff --git a/debian/patches/05_fix_path.diff b/debian/patches/05_fix_path.diff
new file mode 100644
index 0000000..88a5444
--- /dev/null
+++ b/debian/patches/05_fix_path.diff
@@ -0,0 +1,57 @@
+--- lzma443/C/7zip/Compress/LZMA_C/kmod/module.c.old	2007-10-06 03:34:00.000000000 +0100
++++ lzma443/C/7zip/Compress/LZMA_C/kmod/module.c	2007-10-06 03:33:18.000000000 +0100
+@@ -11,7 +11,7 @@
+ #include <linux/init.h>
+ #include <linux/module.h>
+ 
+-#include "../LzmaDecode.c"
++#include "LzmaDecode.c"
+ 
+ EXPORT_SYMBOL(LzmaDecodeProperties);
+ EXPORT_SYMBOL(LzmaDecode);
+--- lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk.old	2007-10-06 04:15:57.000000000 +0100
++++ lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk	2007-10-06 04:16:49.000000000 +0100
+@@ -6,10 +6,6 @@
+ 
+ # $Id: sqlzma.mk,v 1.23 2006/11/27 03:54:58 jro Exp $
+ 
+-ifndef Sqlzma
+-$(error Sqlzma is not defined)
+-endif
+-
+ include makefile.gcc
+ 
+ ifdef UseDebugFlags
+@@ -39,8 +35,7 @@
+ OutBuffer_r.o StreamUtils_r.o: CXXFLAGS += -I../../Common
+ Alloc_r.o CRC_r.o: CXXFLAGS += -I../../../Common
+ 
+-comp.o: CXXFLAGS += -I${Sqlzma}
+-comp.o: comp.cc ${Sqlzma}/sqlzma.h
++comp.o: comp.cc sqlzma.h
+ 
+ liblzma_r.a: ${RObjs} comp.o
+ 	${AR} cr $@ $^
+--- lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk.old	2007-10-06 04:16:00.000000000 +0100
++++ lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk	2007-10-06 04:16:24.000000000 +0100
+@@ -6,10 +6,6 @@
+ 
+ # $Id: sqlzma.mk,v 1.20 2007/01/07 15:12:48 jro Exp $
+ 
+-ifndef Sqlzma
+-$(error Sqlzma is not defined)
+-endif
+-
+ include makefile.gcc
+ ifdef KDir
+ include kmod.mk
+@@ -28,8 +24,7 @@
+ # -pthread
+ %_r.o: CFLAGS += -D_REENTRANT -include pthread.h
+ 
+-uncomp.o uncomp_r.o: CFLAGS += -I${Sqlzma}
+-uncomp.o: uncomp.c ${Sqlzma}/sqlzma.h
++uncomp.o: uncomp.c sqlzma.h
+ 
+ libunlzma.a: uncomp.o LzmaDecode.o
+ 	${AR} cr $@ $^
diff --git a/debian/patches/06_sqlzma.diff b/debian/patches/06_sqlzma.diff
new file mode 100644
index 0000000..e8fee74
--- /dev/null
+++ b/debian/patches/06_sqlzma.diff
@@ -0,0 +1,721 @@
+Index: lzma443/C/7zip/Compress/LZMA_Alone/comp.cc
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_Alone/comp.cc
+diff -N lzma443/C/7zip/Compress/LZMA_Alone/comp.cc
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_Alone/comp.cc	27 Nov 2006 03:54:58 -0000	1.29
+@@ -0,0 +1,253 @@
++/*
++ * Copyright (C) 2006 Junjiro Okajima
++ * Copyright (C) 2006 Tomas Matejicek, slax.org
++ *
++ * LICENSE follows the described one in lzma.txt.
++ */
++
++/* $Id: comp.cc,v 1.29 2006/11/27 03:54:58 jro Exp $ */
++
++// extract some parts from lzma443/C/7zip/Compress/LZMA_Alone/LzmaAlone.cpp
++
++#include <stdio.h>
++#include <stdlib.h>
++#include <assert.h>
++#include <errno.h>
++
++#include "StdAfx.h"
++#include "../../../Common/MyInitGuid.h"
++#include "../../../Common/MyWindows.h"
++#include "../../../Common/StringConvert.h"
++#include "../LZMA/LZMAEncoder.h"
++
++#include <pthread.h>
++#include <zlib.h>
++#include "sqlzma.h"
++
++//////////////////////////////////////////////////////////////////////
++
++class CMemoryStream {
++protected:
++	Bytef *m_data;
++	UInt64 m_limit;
++	UInt64 m_pos;
++
++public:
++	CMemoryStream(Bytef *data, UInt64 size)
++		: m_data(data), m_limit(size), m_pos(0) {}
++
++	virtual ~CMemoryStream() {}
++};
++
++class CInMemoryStream : public CMemoryStream, public IInStream,
++			public CMyUnknownImp {
++//protected:
++	CMyComPtr<ISequentialInStream> m_stream;
++
++public:
++	MY_UNKNOWN_IMP1(IInStream);
++
++	CInMemoryStream(Bytef *data, UInt64 size)
++		: CMemoryStream(data, size), m_stream(this) {}
++
++	virtual ~CInMemoryStream() {}
++
++	STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)
++	{
++		UInt64 room = m_limit - m_pos;
++		if (size > room)
++			size = room;
++		if (size) {
++			memcpy(data, m_data + m_pos, size);
++			m_pos += size;
++		}
++		if (processedSize)
++			*processedSize = size;
++		return S_OK;
++	}
++
++	// disabled all
++	STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) {
++		assert(0);
++		return E_NOTIMPL;
++	}
++};
++
++class COutMemoryStream : public CMemoryStream, public IOutStream,
++			 public CMyUnknownImp {
++//protected:
++	CMyComPtr<ISequentialOutStream> m_stream;
++
++public:
++	MY_UNKNOWN_IMP1(IOutStream);
++
++	COutMemoryStream(Bytef *data, UInt64 size)
++		: CMemoryStream(data, size), m_stream(this) {}
++
++	virtual ~COutMemoryStream() {}
++
++	UInt32 GetSize() {return m_pos;}
++
++	STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize) {
++		if (m_pos + size > m_limit)
++			return -ENOSPC;
++		memcpy(m_data + m_pos, data, size);
++		m_pos += size;
++		if (processedSize)
++			*processedSize = size;
++		return S_OK;
++	}
++
++	// disabled all
++	STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) {
++		assert(0);
++		return E_NOTIMPL;
++	}
++	STDMETHOD(SetSize)(Int64 newSize) {
++		assert(0);
++		return E_NOTIMPL;
++	}
++};
++
++//////////////////////////////////////////////////////////////////////
++
++static int
++LzmaCompress(Bytef *next_in, uInt avail_in, Bytef *next_out, uInt avail_out,
++	     uLong *total_out)
++{
++	int err;
++	HRESULT res;
++	const Byte a[] = {
++		avail_in, avail_in >> 8, avail_in >> 16, avail_in >> 24,
++		0, 0, 0, 0
++	};
++
++	NCompress::NLZMA::CEncoder encoderSpec;
++	CMyComPtr<ICompressCoder> encoder = &encoderSpec;
++	encoder->AddRef();
++	CInMemoryStream inStreamSpec(next_in, avail_in);
++	CMyComPtr<ISequentialInStream> inStream = &inStreamSpec;
++	inStream->AddRef();
++	COutMemoryStream outStreamSpec(next_out, avail_out);
++	CMyComPtr<ISequentialOutStream> outStream = &outStreamSpec;
++	outStream->AddRef();
++
++	// these values are dpending upon is_lzma() macro in sqlzma.h
++	const UInt32 dictionary = 1 << 23;
++	const UString mf = L"BT4";
++	const UInt32 posStateBits = 2;
++	const UInt32 litContextBits = 3; // for normal files
++	// UInt32 litContextBits = 0; // for 32-bit data
++	const UInt32 litPosBits = 0;
++	// UInt32 litPosBits = 2; // for 32-bit data
++	const UInt32 algorithm = 2;
++	const UInt32 numFastBytes = 128;
++	const UInt32 matchFinderCycles = 16 + numFastBytes / 2;
++	//const bool matchFinderCyclesDefined = false;
++	const PROPID propIDs[] = {
++		NCoderPropID::kDictionarySize,
++		NCoderPropID::kPosStateBits,
++		NCoderPropID::kLitContextBits,
++		NCoderPropID::kLitPosBits,
++		NCoderPropID::kAlgorithm,
++		NCoderPropID::kNumFastBytes,
++		NCoderPropID::kMatchFinder,
++		NCoderPropID::kEndMarker,
++		NCoderPropID::kMatchFinderCycles
++	};
++	const int kNumPropsMax = sizeof(propIDs) / sizeof(propIDs[0]);
++	PROPVARIANT properties[kNumPropsMax];
++	for (int p = 0; p < 6; p++)
++		properties[p].vt = VT_UI4;
++	properties[0].ulVal = UInt32(dictionary);
++	properties[1].ulVal = UInt32(posStateBits);
++	properties[2].ulVal = UInt32(litContextBits);
++	properties[3].ulVal = UInt32(litPosBits);
++	properties[4].ulVal = UInt32(algorithm);
++	properties[5].ulVal = UInt32(numFastBytes);
++
++	properties[6].vt = VT_BSTR;
++	properties[6].bstrVal = (BSTR)(const wchar_t *)mf;
++	properties[7].vt = VT_BOOL;
++	properties[7].boolVal = VARIANT_FALSE;	// EOS
++	properties[8].vt = VT_UI4;
++	properties[8].ulVal = UInt32(matchFinderCycles);
++
++	err = -EINVAL;
++	res = encoderSpec.SetCoderProperties(propIDs, properties,
++					     kNumPropsMax - 1);
++	if (res)
++		goto out;
++	res = encoderSpec.WriteCoderProperties(outStream);
++	if (res)
++		goto out;
++
++	UInt32 r;
++	res = outStream->Write(a, sizeof(a), &r);
++	if (res || r != sizeof(a))
++		goto out;
++
++	err = encoder->Code(inStream, outStream, 0, /*broken*/0, 0);
++	if (err)
++		goto out;
++	*total_out = outStreamSpec.GetSize();
++
++ out:
++	return err;
++}
++
++//////////////////////////////////////////////////////////////////////
++
++#define Failure(p) do { \
++	fprintf(stderr, "%s:%d: please report to jro " \
++		"{%02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x}\n", \
++		__func__, __LINE__, \
++		p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); \
++	abort(); \
++}while(0)
++
++extern "C" int
++sqlzma_cm(int try_lzma, z_stream *stream, Bytef *next_in, uInt avail_in,
++	  Bytef *next_out, uInt avail_out)
++{
++	int err;
++	Bytef *p = next_out;
++	uInt l = avail_out;
++
++	stream->next_in = next_in;
++	stream->avail_in = avail_in;
++	stream->next_out = p;
++	stream->avail_out = l;
++	err = deflate(stream, Z_FINISH);
++	if (err != Z_STREAM_END && err != Z_OK)
++		goto out_err;
++	if (avail_in < stream->total_out)
++		return err;
++	if (is_lzma(*p))
++		Failure(p);
++
++	if (try_lzma) {
++		unsigned char a[stream->total_out];
++		uLong processed;
++
++		memcpy(a, p, stream->total_out);
++
++		// malloc family in glibc and stdc++ seems to be thread-safe
++		err = LzmaCompress(next_in, avail_in, p, l, &processed);
++		if (!err && processed <= stream->total_out) {
++			if (!is_lzma(*next_out))
++				Failure(next_out);
++			stream->total_out = processed;
++			err = Z_STREAM_END;
++		} else {
++			//puts("by zlib");
++			memcpy(p, a, stream->total_out);
++			err = Z_STREAM_END;
++		}
++	}
++	return err;
++
++ out_err:
++	fprintf(stderr, "%s: ZLIB err %s\n", __func__, zError(err));
++	return err;
++}
+Index: lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk
+diff -N lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_Alone/sqlzma.mk	27 Nov 2006 03:54:58 -0000	1.23
+@@ -0,0 +1,54 @@
++
++# Copyright (C) 2006 Junjiro Okajima
++# Copyright (C) 2006 Tomas Matejicek, slax.org
++#
++# LICENSE follows the described one in lzma.txt.
++
++# $Id: sqlzma.mk,v 1.23 2006/11/27 03:54:58 jro Exp $
++
++ifndef Sqlzma
++$(error Sqlzma is not defined)
++endif
++
++include makefile.gcc
++
++ifdef UseDebugFlags
++DebugFlags = -Wall -O0 -g -UNDEBUG
++endif
++# -pthread
++CXXFLAGS = ${CFLAGS} -D_REENTRANT -include pthread.h -DNDEBUG ${DebugFlags}
++Tgt = liblzma_r.a
++
++all: ${Tgt}
++
++RObjs = LZMAEncoder_r.o Alloc_r.o LZInWindow_r.o CRC_r.o StreamUtils_r.o \
++	OutBuffer_r.o RangeCoderBit_r.o
++%_r.cc: ../LZMA/%.cpp
++	ln $< $@
++%_r.cc: ../LZ/%.cpp
++	ln $< $@
++%_r.cc: ../RangeCoder/%.cpp
++	ln $< $@
++%_r.cc: ../../Common/%.cpp
++	ln $< $@
++%_r.cc: ../../../Common/%.cpp
++	ln $< $@
++LZMAEncoder_r.o: CXXFLAGS += -I../LZMA
++LZInWindow_r.o: CXXFLAGS += -I../LZ
++RangeCoderBit_r.o: CXXFLAGS += -I../RangeCoder
++OutBuffer_r.o StreamUtils_r.o: CXXFLAGS += -I../../Common
++Alloc_r.o CRC_r.o: CXXFLAGS += -I../../../Common
++
++comp.o: CXXFLAGS += -I${Sqlzma}
++comp.o: comp.cc ${Sqlzma}/sqlzma.h
++
++liblzma_r.a: ${RObjs} comp.o
++	${AR} cr $@ $^
++
++clean: clean_sqlzma
++clean_sqlzma:
++	$(RM) comp.o *_r.o ${Tgt} *~
++
++# Local variables: ;
++# compile-command: (concat "make Sqlzma=../../../../.. -f " (file-name-nondirectory (buffer-file-name)));
++# End: ;
+Index: lzma443/C/7zip/Compress/LZMA_C/kmod.mk
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/kmod.mk
+diff -N lzma443/C/7zip/Compress/LZMA_C/kmod.mk
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/kmod.mk	7 Jan 2007 15:12:48 -0000	1.9
+@@ -0,0 +1,40 @@
++
++# Copyright (C) 2006 Junjiro Okajima
++# Copyright (C) 2006 Tomas Matejicek, slax.org
++#
++# LICENSE follows the described one in lzma.txt.
++
++# $Id: kmod.mk,v 1.9 2007/01/07 15:12:48 jro Exp $
++
++ifndef Sqlzma
++$(error Sqlzma is not defined)
++endif
++ifndef KDir
++$(error KDir is not defined)
++endif
++
++#include makefile.gcc
++
++Kmod = kmod
++EXTRA_CFLAGS += -Wall -Werror -I${CURDIR} -I${Sqlzma}
++# -D_LZMA_PROB32
++EXTRA_CFLAGS += $(shell ${CPP} ${CFLAGS} -P testflags.c)
++
++all: ${Kmod}/uncomp.c
++	${MAKE} EXTRA_CFLAGS="${EXTRA_CFLAGS}" M=${CURDIR}/${Kmod} \
++		-C ${KDir} C=0 V=0 modules
++
++${Kmod}/uncomp.c: uncomp.c
++	ln $< $@
++
++clean: clean_kmod
++clean_kmod:
++	${MAKE} -C ${KDir} M=${CURDIR}/${Kmod} V=0 clean
++	${RM} ${Kmod}/*~
++	-@test -e ${Kmod}/uncomp.c && \
++		diff -q ${Kmod}/uncomp.c uncomp.c > /dev/null && \
++		find ${Kmod}/uncomp.c -links +1 | xargs -r ${RM}
++
++# Local variables: ;
++# compile-command: (concat "make Sqlzma=../../../../.. KDir=/lib/modules/`uname -r`/build -f " (file-name-nondirectory (buffer-file-name)));
++# End: ;
+Index: lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk
+diff -N lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/sqlzma.mk	7 Jan 2007 15:12:48 -0000	1.20
+@@ -0,0 +1,45 @@
++
++# Copyright (C) 2006 Junjiro Okajima
++# Copyright (C) 2006 Tomas Matejicek, slax.org
++#
++# LICENSE follows the described one in lzma.txt.
++
++# $Id: sqlzma.mk,v 1.20 2007/01/07 15:12:48 jro Exp $
++
++ifndef Sqlzma
++$(error Sqlzma is not defined)
++endif
++
++include makefile.gcc
++ifdef KDir
++include kmod.mk
++endif
++
++ifdef UseDebugFlags
++DebugFlags = -O0 -g -UNDEBUG
++endif
++CFLAGS += -DNDEBUG ${DebugFlags}
++Tgt = libunlzma.a libunlzma_r.a
++
++all: ${Tgt}
++
++%_r.c: %.c
++	ln $< $@
++# -pthread
++%_r.o: CFLAGS += -D_REENTRANT -include pthread.h
++
++uncomp.o uncomp_r.o: CFLAGS += -I${Sqlzma}
++uncomp.o: uncomp.c ${Sqlzma}/sqlzma.h
++
++libunlzma.a: uncomp.o LzmaDecode.o
++	${AR} cr $@ $^
++libunlzma_r.a: uncomp_r.o LzmaDecode_r.o
++	${AR} cr $@ $^
++
++clean: clean_sqlzma
++clean_sqlzma:
++	$(RM) ${Tgt} uncomp.o uncomp_r.o LzmaDecode_r.o *~
++
++# Local variables: ;
++# compile-command: (concat "make Sqlzma=../../../../.. -f " (file-name-nondirectory (buffer-file-name)));
++# End: ;
+Index: lzma443/C/7zip/Compress/LZMA_C/testflags.c
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/testflags.c
+diff -N lzma443/C/7zip/Compress/LZMA_C/testflags.c
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/testflags.c	6 Nov 2006 06:33:10 -0000	1.1
+@@ -0,0 +1,5 @@
++#ifdef _LZMA_PROB32
++-D_LZMA_PROB32
++#else
++-U_LZMA_PROB32
++#endif
+Index: lzma443/C/7zip/Compress/LZMA_C/uncomp.c
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/uncomp.c
+diff -N lzma443/C/7zip/Compress/LZMA_C/uncomp.c
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/uncomp.c	4 Mar 2007 12:45:53 -0000	1.30
+@@ -0,0 +1,221 @@
++/*
++ * Copyright (C) 2006, 2007 Junjiro Okajima
++ * Copyright (C) 2006, 2007 Tomas Matejicek, slax.org
++ *
++ * LICENSE follows the described one in lzma.txt.
++ */
++
++/* $Id: uncomp.c,v 1.30 2007/03/04 12:45:53 jro Exp $ */
++
++/* extract some parts from lzma443/C/7zip/Compress/LZMA_C/LzmaTest.c */
++
++#ifndef __KERNEL__
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
++#include <errno.h>
++#include <assert.h>
++#include <pthread.h>
++#define unlikely(x)		__builtin_expect(!!(x), 0)
++#define BUG_ON(x)		assert(!(x))
++/* sqlzma buffers are always larger than a page. true? */
++#define kmalloc(sz,gfp)		malloc(sz)
++#define kfree(p)		free(p)
++#define zlib_inflate(s, f)	inflate(s, f)
++#define zlib_inflateInit(s)	inflateInit(s)
++#define zlib_inflateReset(s)	inflateReset(s)
++#define zlib_inflateEnd(s)	inflateEnd(s)
++#else
++#include <linux/init.h>
++#include <linux/module.h>
++#include <linux/kernel.h>
++#include <linux/vmalloc.h>
++#ifndef WARN_ON_ONCE
++#define WARN_ON_ONCE(b)	WARN_ON(b)
++#endif
++#endif /* __KERNEL__ */
++
++#include "sqlzma.h"
++#include "LzmaDecode.h"
++
++static int LzmaUncompress(struct sqlzma_un *un)
++{
++	int err, i, ret;
++	SizeT outSize, inProcessed, outProcessed, srclen;
++	/* it's about 24-80 bytes structure, if int is 32-bit */
++	CLzmaDecoderState state;
++	unsigned char *dst, *src, a[8];
++	struct sized_buf *sbuf;
++
++	/* Decode LZMA properties and allocate memory */
++	err = -EINVAL;
++	src = un->un_cmbuf;
++	ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
++	src += LZMA_PROPERTIES_SIZE;
++	if (unlikely(ret != LZMA_RESULT_OK))
++		goto out;
++	i = LzmaGetNumProbs(&state.Properties);
++	if (unlikely(i <= 0))
++		i = 1;
++	i *= sizeof(CProb);
++	sbuf = un->un_a + SQUN_PROB;
++	if (unlikely(sbuf->sz < i)) {
++		if (sbuf->buf && sbuf->buf != un->un_prob)
++			kfree(sbuf->buf);
++#ifdef __KERNEL__
++		printk("%s:%d: %d --> %d\n", __func__, __LINE__, sbuf->sz, i);
++#else
++		printf("%d --> %d\n", sbuf->sz, i);
++#endif
++		err = -ENOMEM;
++		sbuf->sz = 0;
++		sbuf->buf = kmalloc(i, GFP_ATOMIC);
++		if (unlikely(!sbuf->buf))
++			goto out;
++		sbuf->sz = i;
++	}
++	state.Probs = (void*)sbuf->buf;
++
++	/* Read uncompressed size */
++	memcpy(a, src, sizeof(a));
++	src += sizeof(a);
++	outSize = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24);
++
++	err = -EINVAL;
++	dst = un->un_resbuf;
++	if (unlikely(!dst || outSize > un->un_reslen))
++		goto out;
++	un->un_reslen = outSize;
++	srclen = un->un_cmlen - (src - un->un_cmbuf);
++
++	/* Decompress */
++	err = LzmaDecode(&state, src, srclen, &inProcessed, dst, outSize,
++			 &outProcessed);
++	if (err)
++		err = -EINVAL;
++
++ out:
++#ifndef __KERNEL__
++	if (err)
++		fprintf(stderr, "err %d\n", err);
++#endif
++	return err;
++}
++
++int sqlzma_un(struct sqlzma_un *un, struct sized_buf *src,
++	      struct sized_buf *dst)
++{
++	int err, by_lzma = 0;
++	if (un->un_lzma && is_lzma(*src->buf)) {
++		by_lzma = 1;
++		un->un_cmbuf = src->buf;
++		un->un_cmlen = src->sz;
++		un->un_resbuf = dst->buf;
++		un->un_reslen = dst->sz;
++
++		/* this library is thread-safe */
++		err = LzmaUncompress(un);
++		goto out;
++	}
++
++	err = zlib_inflateReset(&un->un_stream);
++	if (unlikely(err != Z_OK))
++		goto out;
++	un->un_stream.next_in = src->buf;
++	un->un_stream.avail_in = src->sz;
++	un->un_stream.next_out = dst->buf;
++	un->un_stream.avail_out = dst->sz;
++	err = zlib_inflate(&un->un_stream, Z_FINISH);
++	if (err == Z_STREAM_END)
++		err = 0;
++
++ out:
++	if (err) {
++#ifdef __KERNEL__
++		WARN_ON_ONCE(1);
++#else
++		char a[64] = "ZLIB ";
++		if (by_lzma) {
++			strcpy(a, "LZMA ");
++#ifdef _REENTRANT
++			strerror_r(err, a + 5, sizeof(a) - 5);
++#else
++			strncat(a, strerror(err), sizeof(a) - 5);
++#endif
++		} else
++			strncat(a, zError(err), sizeof(a) - 5);
++		fprintf(stderr, "%s: %.*s\n", __func__, sizeof(a), a);
++#endif
++	}
++	return err;
++}
++
++int sqlzma_init(struct sqlzma_un *un, int do_lzma, unsigned int res_sz)
++{
++	int err;
++
++	err = -ENOMEM;
++	un->un_lzma = do_lzma;
++	memset(un->un_a, 0, sizeof(un->un_a));
++	un->un_a[SQUN_PROB].buf = un->un_prob;
++	un->un_a[SQUN_PROB].sz = sizeof(un->un_prob);
++	if (res_sz) {
++		un->un_a[SQUN_RESULT].buf = kmalloc(res_sz, GFP_KERNEL);
++		if (unlikely(!un->un_a[SQUN_RESULT].buf))
++			return err;
++		un->un_a[SQUN_RESULT].sz = res_sz;
++	}
++
++	un->un_stream.next_in = NULL;
++	un->un_stream.avail_in = 0;
++#ifdef __KERNEL__
++	un->un_stream.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
++	if (unlikely(!un->un_stream.workspace))
++		return err;
++#else
++	un->un_stream.opaque = NULL;
++	un->un_stream.zalloc = Z_NULL;
++	un->un_stream.zfree = Z_NULL;
++#endif
++	err = zlib_inflateInit(&un->un_stream);
++	if (unlikely(err == Z_MEM_ERROR))
++		return -ENOMEM;
++	BUG_ON(err);
++	return err;
++}
++
++void sqlzma_fin(struct sqlzma_un *un)
++{
++	int i;
++	for (i = 0; i < SQUN_LAST; i++)
++		if (un->un_a[i].buf && un->un_a[i].buf != un->un_prob)
++			kfree(un->un_a[i].buf);
++	BUG_ON(zlib_inflateEnd(&un->un_stream) != Z_OK);
++}
++
++#ifdef __KERNEL__
++EXPORT_SYMBOL(sqlzma_un);
++EXPORT_SYMBOL(sqlzma_init);
++EXPORT_SYMBOL(sqlzma_fin);
++
++#if 0
++static int __init sqlzma_init(void)
++{
++	return 0;
++}
++
++static void __exit sqlzma_exit(void)
++{
++}
++
++module_init(sqlzma_init);
++module_exit(sqlzma_exit);
++#endif
++
++MODULE_LICENSE("GPL");
++MODULE_AUTHOR("Junjiro Okajima <hooanon05 at yahoo dot co dot jp>");
++MODULE_VERSION("$Id: uncomp.c,v 1.30 2007/03/04 12:45:53 jro Exp $");
++MODULE_DESCRIPTION("LZMA uncompress for squashfs. "
++		   "Some functions for squashfs to support LZMA and "
++		   "a tiny wrapper for LzmaDecode.c in LZMA SDK from www.7-zip.org.");
++#endif
+Index: lzma443/C/7zip/Compress/LZMA_C/kmod/Makefile
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/kmod/Makefile
+diff -N lzma443/C/7zip/Compress/LZMA_C/kmod/Makefile
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/kmod/Makefile	27 Nov 2006 03:54:58 -0000	1.3
+@@ -0,0 +1,11 @@
++
++# Copyright (C) 2006 Junjiro Okajima
++# Copyright (C) 2006 Tomas Matejicek, slax.org
++#
++# LICENSE follows the described one in lzma.txt.
++
++# $Id: Makefile,v 1.3 2006/11/27 03:54:58 jro Exp $
++
++obj-m += unlzma.o sqlzma.o
++unlzma-y := module.o
++sqlzma-y := uncomp.o
+Index: lzma443/C/7zip/Compress/LZMA_C/kmod/module.c
+===================================================================
+RCS file: lzma443/C/7zip/Compress/LZMA_C/kmod/module.c
+diff -N lzma443/C/7zip/Compress/LZMA_C/kmod/module.c
+--- /dev/null	1 Jan 1970 00:00:00 -0000
++++ lzma443/C/7zip/Compress/LZMA_C/kmod/module.c	27 Nov 2006 03:54:58 -0000	1.5
+@@ -0,0 +1,36 @@
++
++/*
++ * Copyright (C) 2006 Junjiro Okajima
++ * Copyright (C) 2006 Tomas Matejicek, slax.org
++ *
++ * LICENSE follows the described one in lzma.txt.
++ */
++
++/* $Id: module.c,v 1.5 2006/11/27 03:54:58 jro Exp $ */
++
++#include <linux/init.h>
++#include <linux/module.h>
++
++#include "../LzmaDecode.c"
++
++EXPORT_SYMBOL(LzmaDecodeProperties);
++EXPORT_SYMBOL(LzmaDecode);
++
++#if 0
++static int __init unlzma_init(void)
++{
++	return 0;
++}
++
++static void __exit unlzma_exit(void)
++{
++}
++
++module_init(unlzma_init);
++module_exit(unlzma_exit);
++#endif
++
++MODULE_LICENSE("GPL");
++MODULE_VERSION("$Id: module.c,v 1.5 2006/11/27 03:54:58 jro Exp $");
++MODULE_DESCRIPTION("LZMA uncompress. "
++		   "A tiny wrapper for LzmaDecode.c in LZMA SDK from www.7-zip.org.");
diff --git a/debian/patches/series b/debian/patches/series
index 33f6d2e..7f384b9 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -1,3 +1,6 @@
+06_sqlzma.diff
+05_fix_path.diff
+04_sqlzma_h.diff
 03_lzma_manpage.diff
 01_large_files.diff
 02_lzmp.diff
diff --git a/debian/rules b/debian/rules
index 479490e..4b95d98 100755
--- a/debian/rules
+++ b/debian/rules
@@ -4,6 +4,12 @@
 # Uncomment this to turn on verbose mode.
 #export DH_VERBOSE=1
 
+SRC_DIR_C = C/7zip/Compress/LZMA_C
+SRC_DIR_ALONE = C/7zip/Compress/LZMA_Alone
+MOD = $(SRC_DIR_C)/kmod/module.c $(SRC_DIR_C)/uncomp.c
+MOD_HEADERS = $(SRC_DIR_C)/LzmaDecode.h $(SRC_DIR_C)/LzmaTypes.h \
+	$(SRC_DIR_C)/LzmaDecode.c $(SRC_DIR_ALONE)/sqlzma.h
+
 build: build-stamp
 
 build-stamp:
@@ -11,7 +17,7 @@ build-stamp:
 
 	# Add here commands to compile the package.
 	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
-	$(MAKE) -C C/7zip/Compress/LZMA_Alone/ -f makefile.gcc
+	$(MAKE) -C $(SRC_DIR_ALONE) -f sqlzma.mk
 
 	touch build-stamp
 
@@ -20,9 +26,15 @@ clean:
 	dh_testroot
 	rm -f build-stamp
 	# Add here commands to clean up after the build process.
-	$(MAKE) clean -C C/7zip/Compress/LZMA_Alone/ -f makefile.gcc
+	$(MAKE) clean -C $(SRC_DIR_ALONE) -f makefile.gcc
+
+	# Cleanup and avoid using sqlzma.mk
+	rm -f $(SRC_DIR_ALONE)/*.o $(SRC_DIR_ALONE)/*.a
+
 	QUILT_PATCHES=debian/patches quilt pop -a -R || test $$? = 2
-	rm -rf .pc
+
+	# Remove temporary debian/modules directory
+	rm -rf .pc debian/modules
 	dh_clean 
 
 install: build
@@ -31,6 +43,25 @@ install: build
 	dh_clean -k 
 	dh_installdirs
 
+	# Create the needed directory for lzma-source
+	mkdir -m755 -p debian/modules/lzma/debian \
+		debian/lzma-source/usr/src
+
+	# Copy the source and header files
+	install -m644 $(MOD) $(MOD_HEADERS) $(SRC_DIR_C)/kmod/Makefile \
+		debian/modules/lzma
+
+	# Copy the debian specific files
+	install -m644 debian/changelog debian/compat debian/copyright \
+		debian/control.modules.in debian/modules/lzma/debian
+
+	# This rules file is specific to m-a
+	install -m755 debian/rules.modules \
+		debian/modules/lzma/debian/rules
+
+	# Finally create the tarball which contains the module directory
+	cd debian && tar jcf lzma-source/usr/src/lzma.tar.bz2 modules
+
 # Build architecture-independent files here.
 binary-indep: build install
 # We have nothing to do by default.
diff --git a/debian/rules.modules b/debian/rules.modules
new file mode 100644
index 0000000..c2947ab
--- /dev/null
+++ b/debian/rules.modules
@@ -0,0 +1,52 @@
+#!/usr/bin/make -f
+# -*- mode: makefile; coding: utf-8 -*-
+
+# prefix of the target package name
+PACKAGE=lzma-modules
+# modifieable for experiments or debugging m-a
+MA_DIR ?= /usr/share/modass
+# load generic variable handling
+-include $(MA_DIR)/include/generic.make
+# load default rules, including kdist, kdist_image, ...
+-include $(MA_DIR)/include/common-rules.make
+
+# module assistant calculates all needed things for us and sets
+# following variables:
+# KSRC (kernel source directory), KVERS (kernel version string), KDREV
+# (revision of the Debian kernel-image package), CC (the correct
+# compiler), VERSION (the final package version string), PKGNAME (full
+# package name with KVERS included), DEB_DESTDIR (path to store DEBs)
+
+# The kdist_configure target is called by make-kpkg modules_config and
+# by kdist* rules by dependency. It should configure the module so it is
+# ready for compilation (mostly useful for calling configure).
+# prep-deb-files from module-assistant creates the neccessary debian/ files 
+kdist_config: prep-deb-files
+
+# the kdist_clean target is called by make-kpkg modules_clean and from
+# kdist* rules. It is responsible for cleaning up any changes that have
+# been made by the other kdist_commands (except for the .deb files created)
+kdist_clean:
+	$(MAKE) -C $(KSRC) M=$(PWD) clean
+
+binary-modules:
+	dh_testroot
+	dh_clean -k
+	dh_installdirs
+
+	$(MAKE) -C $(KSRC) M=$(PWD) modules
+
+	# Install the module
+	mkdir -p debian/$(PACKAGE)-$(KVERS)/lib/modules/$(KVERS)/extra
+	cp sqlzma.ko unlzma.ko debian/$(PACKAGE)-$(KVERS)/lib/modules/$(KVERS)/extra
+
+	dh_installdocs
+	dh_installchangelogs
+	dh_installmodules
+	dh_compress
+	dh_fixperms
+	dh_installdeb
+	dh_gencontrol -- -v$(VERSION)
+	dh_md5sums
+	dh_builddeb --destdir=$(DEB_DESTDIR)
+	dh_clean -k
