0bf1824
# LZMA support for Midnight Commander
0bf1824
# 2006-03-17
0bf1824
#
0bf1824
# This patch adds basic support for LZMA compressed files to
0bf1824
# Midnight Commander 4.6.1. You should have LZMA utils 4.32.x
0bf1824
# or later. Older versions of LZMA utils will *not* work.
0bf1824
#
0bf1824
# Copyright (C) 2006 Lasse Collin <lasse.collin@tukaani.org>
0bf1824
#
0bf1824
# This patch is free software; you can redistribute it and/or modify
0bf1824
# it under the terms of the GNU General Public License as published by
0bf1824
# the Free Software Foundation; either version 2 of the License, or
0bf1824
# (at your option) any later version.
0bf1824
#
0bf1824
# This program is distributed in the hope that it will be useful,
0bf1824
# but WITHOUT ANY WARRANTY; without even the implied warranty of
0bf1824
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0bf1824
# GNU General Public License for more details.
0bf1824
0bf1824
diff -Naru mc-4.6.1.orig/edit/edit.c mc-4.6.1/edit/edit.c
0bf1824
--- mc-4.6.1.orig/edit/edit.c	2005-05-27 17:19:18.000000000 +0300
0bf1824
+++ mc-4.6.1/edit/edit.c	2006-03-17 17:39:49.000000000 +0200
0bf1824
@@ -186,6 +186,7 @@
0bf1824
 static const struct edit_filters {
0bf1824
     const char *read, *write, *extension;
0bf1824
 } all_filters[] = {
0bf1824
+    { "lzma -cd %s 2>&1",   "lzma > %s",   ".bz2" },
0bf1824
     { "bzip2 -cd %s 2>&1",  "bzip2 > %s",  ".bz2" },
0bf1824
     { "gzip -cd %s 2>&1",   "gzip > %s",   ".gz"  },
0bf1824
     { "gzip -cd %s 2>&1",   "gzip > %s",   ".Z"   }
0bf1824
0bf1824
diff -Naru mc-4.6.1.orig/src/util.c mc-4.6.1/src/util.c
0bf1824
--- mc-4.6.1.orig/src/util.c	2005-05-27 17:19:18.000000000 +0300
0bf1824
+++ mc-4.6.1/src/util.c	2006-03-17 18:20:50.000000000 +0200
0bf1824
@@ -900,7 +900,7 @@
0bf1824
  * Warning: this function moves the current file pointer */
0bf1824
 int get_compression_type (int fd)
0bf1824
 {
0bf1824
-    unsigned char magic[4];
0bf1824
+    unsigned char magic[16];
0bf1824
 
0bf1824
     /* Read the magic signature */
0bf1824
     if (mc_read (fd, (char *) magic, 4) != 4)
0bf1824
@@ -944,6 +944,31 @@
0bf1824
 	    return COMPRESSION_BZIP2;
0bf1824
 	}
0bf1824
     }
0bf1824
+
0bf1824
+    /* LZMA files; both LZMA_Alone and LZMA utils formats. The LZMA_Alone
0bf1824
+     * format is used by the LZMA_Alone tool from LZMA SDK. The LZMA utils
0bf1824
+     * format is the default format of LZMA utils 4.32.1 and later. */
0bf1824
+    if (magic[0] < 0xE1 || (magic[0] == 0xFF && magic[1] == 'L' &&
0bf1824
+	magic[2] == 'Z' && magic[3] == 'M')) {
0bf1824
+	if (mc_read (fd, (char *) magic + 4, 9) == 9) {
0bf1824
+	    /* LZMA utils format */
0bf1824
+	    if (magic[0] == 0xFF && magic[4] == 'A' && magic[5] == 0x00)
0bf1824
+		return COMPRESSION_LZMA;
0bf1824
+	    /* The LZMA_Alone format has no magic bytes, thus we
0bf1824
+	     * need to play a wizard. This can give false positives,
0bf1824
+	     * thus the detection below should be removed when
0bf1824
+	     * the newer LZMA utils format has got popular. */
0bf1824
+	    if (magic[0] < 0xE1 && magic[4] < 0x20 &&
0bf1824
+		((magic[10] == 0x00 && magic[11] == 0x00 &&
0bf1824
+		  magic[12] == 0x00) ||
0bf1824
+		 (magic[5] == 0xFF && magic[6] == 0xFF &&
0bf1824
+		  magic[7] == 0xFF && magic[8] == 0xFF &&
0bf1824
+		  magic[9] == 0xFF && magic[10] == 0xFF &&
0bf1824
+		  magic[11] == 0xFF && magic[12] == 0xFF)))
0bf1824
+		return COMPRESSION_LZMA;
0bf1824
+	}
0bf1824
+    }
0bf1824
+
0bf1824
     return 0;
0bf1824
 }
0bf1824
 
0bf1824
@@ -954,6 +979,7 @@
0bf1824
 	case COMPRESSION_GZIP: return "#ugz";
0bf1824
 	case COMPRESSION_BZIP:   return "#ubz";
0bf1824
 	case COMPRESSION_BZIP2:  return "#ubz2";
0bf1824
+	case COMPRESSION_LZMA:  return "#ulzma";
0bf1824
 	}
0bf1824
 	/* Should never reach this place */
0bf1824
 	fprintf (stderr, "Fatal: decompress_extension called with an unknown argument\n");
0bf1824
diff -Naru mc-4.6.1.orig/src/util.h mc-4.6.1/src/util.h
0bf1824
--- mc-4.6.1.orig/src/util.h	2005-01-13 21:20:47.000000000 +0200
0bf1824
+++ mc-4.6.1/src/util.h	2006-03-17 17:34:34.000000000 +0200
0bf1824
@@ -169,7 +169,8 @@
0bf1824
 	COMPRESSION_NONE,
0bf1824
 	COMPRESSION_GZIP,
0bf1824
 	COMPRESSION_BZIP,
0bf1824
-	COMPRESSION_BZIP2
0bf1824
+	COMPRESSION_BZIP2,
0bf1824
+	COMPRESSION_LZMA
0bf1824
 };
0bf1824
 
0bf1824
 int get_compression_type (int fd);
0bf1824
diff -Naru mc-4.6.1.orig/vfs/extfs/iso9660.in mc-4.6.1/vfs/extfs/iso9660.in
0bf1824
--- mc-4.6.1.orig/vfs/extfs/iso9660.in	2004-10-29 12:14:38.000000000 +0300
0bf1824
+++ mc-4.6.1/vfs/extfs/iso9660.in	2006-03-17 17:45:28.000000000 +0200
0bf1824
@@ -25,6 +25,7 @@
0bf1824
 mcisofs_list () {
0bf1824
 # left as a reminder to implement compressed image support =)
0bf1824
 case "$1" in
0bf1824
+  *.lzma) MYCAT="lzma -dc";;
0bf1824
   *.bz2) MYCAT="bzip2 -dc";;
0bf1824
   *.gz)  MYCAT="gzip -dc";;
0bf1824
   *.z)   MYCAT="gzip -dc";;
0bf1824
diff -Naru mc-4.6.1.orig/vfs/extfs/lslR.in mc-4.6.1/vfs/extfs/lslR.in
0bf1824
--- mc-4.6.1.orig/vfs/extfs/lslR.in	2003-06-22 12:54:21.000000000 +0300
0bf1824
+++ mc-4.6.1/vfs/extfs/lslR.in	2006-03-17 17:45:08.000000000 +0200
0bf1824
@@ -12,6 +12,7 @@
0bf1824
 
0bf1824
 mclslRfs_list () {
0bf1824
 case "$1" in
0bf1824
+  *.lzma) MYCAT="lzma -dc";;
0bf1824
   *.bz2) MYCAT="bzip2 -dc";;
0bf1824
   *.gz)  MYCAT="gzip -dc";;
0bf1824
   *.z)   MYCAT="gzip -dc";;
0bf1824
diff -Naru mc-4.6.1.orig/vfs/extfs/mailfs.in mc-4.6.1/vfs/extfs/mailfs.in
0bf1824
--- mc-4.6.1.orig/vfs/extfs/mailfs.in	2002-12-24 08:56:17.000000000 +0200
0bf1824
+++ mc-4.6.1/vfs/extfs/mailfs.in	2006-03-17 17:53:47.000000000 +0200
0bf1824
@@ -7,6 +7,7 @@
0bf1824
 
0bf1824
 $zcat="zcat";                 # gunzip to stdout
0bf1824
 $bzcat="bzip2 -dc";           # bunzip2 to stdout
0bf1824
+$lzcat="lzma -dc";            # unlzma to stdout
0bf1824
 $file="file";                 # "file" command
0bf1824
 $TZ='GMT';                    # default timezone (for Date module)
0bf1824
 
0bf1824
@@ -132,6 +133,8 @@
0bf1824
     exit 1 unless (open IN, "$zcat $mbox_qname|");
0bf1824
 } elsif (/bzip/) {
0bf1824
     exit 1 unless (open IN, "$bzcat $mbox_qname|");
0bf1824
+} elsif (/lzma/) {
0bf1824
+    exit 1 unless (open IN, "$lzcat $mbox_qname|");
0bf1824
 } else {
0bf1824
     exit 1 unless (open IN, "<$mbox_name");
0bf1824
 }
0bf1824
diff -Naru mc-4.6.1.orig/vfs/extfs/patchfs.in mc-4.6.1/vfs/extfs/patchfs.in
0bf1824
--- mc-4.6.1.orig/vfs/extfs/patchfs.in	2004-11-17 01:00:40.000000000 +0200
0bf1824
+++ mc-4.6.1/vfs/extfs/patchfs.in	2006-03-17 17:52:47.000000000 +0200
0bf1824
@@ -12,6 +12,7 @@
0bf1824
 use File::Temp 'tempfile';
0bf1824
 
0bf1824
 # standard binaries
0bf1824
+my $lzma = 'lzma';
0bf1824
 my $bzip = 'bzip2';
0bf1824
 my $gzip = 'gzip';
0bf1824
 my $fileutil = 'file';
0bf1824
@@ -70,7 +71,9 @@
0bf1824
     my ($qfname)=(quotemeta $_[0]);
0bf1824
 
0bf1824
     $_=`$fileutil $qfname`;
0bf1824
-    if (/bzip/) {
0bf1824
+    if (/lzma/) {
0bf1824
+	return "$lzma -dc $qfname";
0bf1824
+    } elsif (/bzip/) {
0bf1824
 	return "$bzip -dc $qfname";
0bf1824
     } elsif (/gzip/) {
0bf1824
 	return "$gzip -dc $qfname";
0bf1824
@@ -86,7 +89,9 @@
0bf1824
     my ($sep) = $append ? '>>' : '>';
0bf1824
 
0bf1824
     $_=`$fileutil $qfname`;
0bf1824
-    if (/bzip/) {
0bf1824
+    if (/lzma/) {
0bf1824
+	return "$lzma -c $sep $qfname";
0bf1824
+    } elsif (/bzip/) {
0bf1824
 	return "$bzip -c $sep $qfname";
0bf1824
     } elsif (/gzip/) {
0bf1824
 	return "$gzip -c $sep $qfname";
0bf1824
diff -Naru mc-4.6.1.orig/vfs/extfs/sfs.ini mc-4.6.1/vfs/extfs/sfs.ini
0bf1824
--- mc-4.6.1.orig/vfs/extfs/sfs.ini	1998-12-15 17:57:43.000000000 +0200
0bf1824
+++ mc-4.6.1/vfs/extfs/sfs.ini	2006-03-17 17:44:01.000000000 +0200
0bf1824
@@ -10,6 +10,8 @@
0bf1824
 ubz/1	bzip -d < %1 > %3
0bf1824
 bz2/1	bzip2 < %1 > %3
0bf1824
 ubz2/1	bzip2 -d < %1 > %3
0bf1824
+lzma/1	lzma < %1 > %3
0bf1824
+ulzma/1	lzma -d < %1 > %3
0bf1824
 tar/1	tar cf %3 %1
0bf1824
 tgz/1	tar czf %3 %1
0bf1824
 uhtml/1	lynx -force_html -dump %1 > %3