Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZoltan Varga <vargaz@gmail.com>2005-11-14 18:31:01 +0300
committerZoltan Varga <vargaz@gmail.com>2005-11-14 18:31:01 +0300
commit7d4a3b19048d57d6b7780d21b8fa34721580c445 (patch)
tree3f0a5bad65d6a1ad24aa4642369d91b68986b112 /mcs/class/ICSharpCode.SharpZipLib
parentca6e807d93c72eb755c021c140108ffb00a22e3d (diff)
parent9e73e5c2759bf3bdf4c9ecd222f8014e3ddda8c6 (diff)
Remove old version and re-add new one.
svn path=/trunk/mcs/; revision=53019
Diffstat (limited to 'mcs/class/ICSharpCode.SharpZipLib')
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ChangeLog24
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.dll.sources50
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/AssemblyInfo.cs59
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2.cs99
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs197
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs62
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs966
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs1804
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/Adler32.cs200
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/CRC32.cs211
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/IChecksum.cs93
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/StrangeCRC.cs184
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs380
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/INameTransform.cs57
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/NameFilter.cs213
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/PathFilter.cs137
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs472
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZIPConstants.cs92
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZipException.cs61
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs340
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs207
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/ICSharpCode.SharpZLib.prjx82
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Main.cs36
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/SharpZipBaseException.cs73
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs73
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarArchive.cs680
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs482
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarEntry.cs539
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarException.cs60
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarHeader.cs1090
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs636
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs428
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs552
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs186
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs791
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs887
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs57
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs817
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs207
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs225
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs274
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs471
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs656
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs226
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs270
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/FastZip.cs430
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs465
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs733
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipException.cs63
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipFile.cs1012
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs522
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs147
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs592
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/Makefile12
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/README28
-rwxr-xr-xmcs/class/ICSharpCode.SharpZipLib/SharpZipLib.keybin0 -> 596 bytes
-rw-r--r--mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.pubbin0 -> 160 bytes
57 files changed, 19710 insertions, 0 deletions
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ChangeLog b/mcs/class/ICSharpCode.SharpZipLib/ChangeLog
new file mode 100644
index 00000000000..7ef058d0faf
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ChangeLog
@@ -0,0 +1,24 @@
+2005-10-15 Zoltan Varga <vargaz@freemail.hu>
+
+ * Import 0.84 version of SharpZipLib and merge local changes. This is
+ needed by newer versions of IKVM.
+
+ * ICSharpCode.SharpZipLib: Remove 0.6 version.
+
+2004-11-25 Raja R Harinath <rharinath@novell.com>
+
+ * Makefile (EXTRA_DISTFILES): Add SharpZipLib.pub.
+
+2004-06-21 Raja R Harinath <rharinath@novell.com>
+
+ * Makefile (LIBRARY_SNK): Sign with SharpZipLib.key. The library
+ is delay-signed according to ICSharpCode.SharpZipLib/AssemblyInfo.cs.
+
+2004-06-09 Sebastien Pouliot <sebastien@ximian.com>
+
+ * Makefile: Don't sign this assembly as it is fully signed during
+ compilation (we have the original private key). Thanks to Jackson.
+
+2004-05-28 Duncan Mak <duncan@ximian.com>
+
+ * SharpZipLib.pub: Added public key.
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.dll.sources b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.dll.sources
new file mode 100644
index 00000000000..24bde2e34e5
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.dll.sources
@@ -0,0 +1,50 @@
+ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs
+ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs
+ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs
+ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs
+ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs
+ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs
+ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs
+ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs
+ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs
+ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs
+ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs
+ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs
+ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs
+ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
+ICSharpCode.SharpZipLib/Zip/ZipConstants.cs
+ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs
+ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
+ICSharpCode.SharpZipLib/Zip/FastZip.cs
+ICSharpCode.SharpZipLib/Zip/ZipException.cs
+ICSharpCode.SharpZipLib/Zip/ZipFile.cs
+ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
+ICSharpCode.SharpZipLib/Checksums/StrangeCRC.cs
+ICSharpCode.SharpZipLib/Checksums/IChecksum.cs
+ICSharpCode.SharpZipLib/Checksums/Adler32.cs
+ICSharpCode.SharpZipLib/Checksums/CRC32.cs
+ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
+ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs
+ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
+ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs
+ICSharpCode.SharpZipLib/BZip2/BZip2.cs
+ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs
+ICSharpCode.SharpZipLib/Tar/TarHeader.cs
+ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
+ICSharpCode.SharpZipLib/Tar/TarInputStream.cs
+ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs
+ICSharpCode.SharpZipLib/Tar/TarException.cs
+ICSharpCode.SharpZipLib/Tar/TarArchive.cs
+ICSharpCode.SharpZipLib/Tar/TarEntry.cs
+ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs
+ICSharpCode.SharpZipLib/Core/PathFilter.cs
+ICSharpCode.SharpZipLib/Core/INameTransform.cs
+ICSharpCode.SharpZipLib/Core/NameFilter.cs
+ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs
+ICSharpCode.SharpZipLib/GZip/GZIPConstants.cs
+ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs
+ICSharpCode.SharpZipLib/GZip/GZipException.cs
+ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs
+ICSharpCode.SharpZipLib/AssemblyInfo.cs
+ICSharpCode.SharpZipLib/Main.cs
+ICSharpCode.SharpZipLib/SharpZipBaseException.cs
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/AssemblyInfo.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/AssemblyInfo.cs
new file mode 100644
index 00000000000..97c55c6b1d5
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/AssemblyInfo.cs
@@ -0,0 +1,59 @@
+// AssemblyInfo.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+[assembly: CLSCompliant(true)]
+
+[assembly: AssemblyTitle("ICSharpCode.SharpZipLibrary")]
+[assembly: AssemblyDescription("A free C# compression library")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("#ZipLibrary")]
+[assembly: AssemblyCopyright("Copyright 2001-2005 Mike Krueger")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+#if NET_2_0
+[assembly: AssemblyVersion("2.84.0.0")]
+#else
+// not worrying about the NET_1_0 profile for now.
+[assembly: AssemblyVersion("0.84.0.0")]
+#endif
+
+[assembly: AssemblyDelaySign(true)]
+[assembly: AssemblyKeyFile("SharpZipLib.pub")]
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2.cs
new file mode 100644
index 00000000000..7a6669e87bf
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2.cs
@@ -0,0 +1,99 @@
+// BZip2.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+namespace ICSharpCode.SharpZipLib.BZip2
+{
+
+ /// <summary>
+ /// Does all the compress and decompress pre-operation stuff.
+ /// Sets up the streams and file header characters.
+ /// Uses multiply overloaded methods to call for the compress/decompress.
+ /// </summary>
+ public sealed class BZip2
+ {
+ /// <summary>
+ /// Decompress <paramref name="instream">input</paramref> writing
+ /// decompressed data to <paramref name="outstream">output stream</paramref>
+ /// </summary>
+ public static void Decompress(Stream instream, Stream outstream)
+ {
+ System.IO.Stream bos = outstream;
+ System.IO.Stream bis = instream;
+ BZip2InputStream bzis = new BZip2InputStream(bis);
+ int ch = bzis.ReadByte();
+ while (ch != -1) {
+ bos.WriteByte((byte)ch);
+ ch = bzis.ReadByte();
+ }
+ bos.Flush();
+ }
+
+ /// <summary>
+ /// Compress <paramref name="instream">input stream</paramref> sending
+ /// result to <paramref name="outputstream">output stream</paramref>
+ /// </summary>
+ public static void Compress(Stream instream, Stream outstream, int blockSize)
+ {
+ System.IO.Stream bos = outstream;
+ System.IO.Stream bis = instream;
+ int ch = bis.ReadByte();
+ BZip2OutputStream bzos = new BZip2OutputStream(bos, blockSize);
+ while (ch != -1) {
+ bzos.WriteByte((byte)ch);
+ ch = bis.ReadByte();
+ }
+ bis.Close();
+ bzos.Close();
+ }
+ }
+}
+
+/* derived from a file which contained this license :
+ * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+*/
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs
new file mode 100644
index 00000000000..9170ced44b5
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs
@@ -0,0 +1,197 @@
+// BZip2Constants.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.BZip2
+{
+
+ /// <summary>
+ /// Defines internal values for both compression and decompression
+ /// </summary>
+ public sealed class BZip2Constants
+ {
+ /// <summary>
+ /// Random numbers used to randomise repetitive blocks
+ /// </summary>
+ public readonly static int[] rNums = {
+ 619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
+ 985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
+ 733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
+ 419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
+ 878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
+ 862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
+ 150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
+ 170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
+ 73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
+ 909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
+ 641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
+ 161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
+ 382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
+ 98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
+ 227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
+ 469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
+ 184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
+ 715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
+ 951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
+ 652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
+ 645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
+ 609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
+ 653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
+ 411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
+ 170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
+ 857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
+ 669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
+ 944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
+ 344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
+ 897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
+ 433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
+ 686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
+ 946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
+ 978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
+ 680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
+ 707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
+ 297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
+ 134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
+ 343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
+ 140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
+ 170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
+ 369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
+ 804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
+ 896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
+ 661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
+ 768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
+ 61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
+ 372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
+ 780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
+ 920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
+ 645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
+ 936, 638
+ };
+
+ /// <summary>
+ /// When multiplied by compression parameter (1-9) gives the block size for compression
+ /// 9 gives the best compresssion but uses the most memory.
+ /// </summary>
+ public readonly static int baseBlockSize = 100000;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int MAX_ALPHA_SIZE = 258;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int MAX_CODE_LEN = 23;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int RUNA = 0;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int RUNB = 1;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int N_GROUPS = 6;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int G_SIZE = 50;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int N_ITERS = 4;
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int MAX_SELECTORS = (2 + (900000 / G_SIZE));
+
+ /// <summary>
+ /// Backend constant
+ /// </summary>
+ public readonly static int NUM_OVERSHOOT_BYTES = 20;
+
+ private BZip2Constants()
+ {
+ }
+ }
+}
+
+/* This file was derived from a file containing this license:
+ *
+ * This file is a part of bzip2 and/or libbzip2, a program and
+ * library for lossless, block-sorting data compression.
+ *
+ * Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. The origin of this software must not be misrepresented; you must
+ * not claim that you wrote the original software. If you use this
+ * software in a product, an acknowledgment in the product
+ * documentation would be appreciated but is not required.
+ *
+ * 3. Altered source versions must be plainly marked as such, and must
+ * not be misrepresented as being the original software.
+ *
+ * 4. The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Java version ported by Keiron Liddle, Aftex Software <keiron@aftexsw.com> 1999-2001
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs
new file mode 100644
index 00000000000..13330ccdc39
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs
@@ -0,0 +1,62 @@
+// BZip2.cs
+//
+// Copyright 2004 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using ICSharpCode.SharpZipLib;
+
+namespace ICSharpCode.SharpZipLib.BZip2
+{
+ /// <summary>
+ /// BZip2Exception represents exceptions specific to Bzip2 algorithm
+ /// </summary>
+ public class BZip2Exception : SharpZipBaseException
+ {
+ /// <summary>
+ /// Initialise a new instance of BZip2Exception.
+ /// </summary>
+ public BZip2Exception()
+ {
+ }
+
+ /// <summary>
+ /// Initialise a new instance of BZip2Exception with its message set to message.
+ /// </summary>
+ /// <param name="message">The message describing the error.</param>
+ public BZip2Exception(string message) : base(message)
+ {
+ }
+
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
new file mode 100644
index 00000000000..60b94715b09
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
@@ -0,0 +1,966 @@
+// BZip2InputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Checksums;
+
+namespace ICSharpCode.SharpZipLib.BZip2
+{
+
+ /// <summary>
+ /// An input stream that decompresses files in the BZip2 format
+ /// </summary>
+ public class BZip2InputStream : Stream
+ {
+ /// <summary>
+ /// Gets a value indicating if the stream supports reading
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return baseStream.CanRead;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports seeking.
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return baseStream.CanSeek;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports writing.
+ /// This property always returns false
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets the length in bytes of the stream.
+ /// </summary>
+ public override long Length {
+ get {
+ return baseStream.Length;
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the streams position.
+ /// Setting the position is not supported and will throw a NotSupportException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any attempt to set the position</exception>
+ public override long Position {
+ get {
+ return baseStream.Position;
+ }
+ set {
+ throw new NotSupportedException("BZip2InputStream position cannot be set");
+ }
+ }
+
+ /// <summary>
+ /// Flushes the stream.
+ /// </summary>
+ public override void Flush()
+ {
+ if (baseStream != null) {
+ baseStream.Flush();
+ }
+ }
+
+ /// <summary>
+ /// Set the streams position. This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException("BZip2InputStream Seek not supported");
+ }
+
+ /// <summary>
+ /// Sets the length of this stream to the given value.
+ /// This operation is not supported and will throw a NotSupportedExceptionortedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void SetLength(long val)
+ {
+ throw new NotSupportedException("BZip2InputStream SetLength not supported");
+ }
+
+ /// <summary>
+ /// Writes a block of bytes to this stream using data from a buffer.
+ /// This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void Write(byte[] array, int offset, int count)
+ {
+ throw new NotSupportedException("BZip2InputStream Write not supported");
+ }
+
+ /// <summary>
+ /// Writes a byte to the current position in the file stream.
+ /// This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void WriteByte(byte val)
+ {
+ throw new NotSupportedException("BZip2InputStream WriteByte not supported");
+ }
+
+ /// <summary>
+ /// Read a sequence of bytes and advances the read position by one byte.
+ /// </summary>
+ /// <param name="b">Array of bytes to store values in</param>
+ /// <param name="offset">Offset in array to begin storing data</param>
+ /// <param name="count">The maximum number of bytes to read</param>
+ /// <returns>The total number of bytes read into the buffer. This might be less
+ /// than the number of bytes requested if that number of bytes are not
+ /// currently available or zero if the end of the stream is reached.
+ /// </returns>
+ public override int Read(byte[] b, int offset, int count)
+ {
+ for (int i = 0; i < count; ++i) {
+ int rb = ReadByte();
+ if (rb == -1) {
+ return i;
+ }
+ b[offset + i] = (byte)rb;
+ }
+ return count;
+ }
+
+ /// <summary>
+ /// Closes the stream, releasing any associated resources.
+ /// </summary>
+ public override void Close()
+ {
+ if (baseStream != null) {
+ baseStream.Close();
+ }
+ }
+
+ void MakeMaps()
+ {
+ nInUse = 0;
+ for (int i = 0; i < 256; ++i) {
+ if (inUse[i]) {
+ seqToUnseq[nInUse] = (byte)i;
+ unseqToSeq[i] = (byte)nInUse;
+ nInUse++;
+ }
+ }
+ }
+
+ /*--
+ index of the last char in the block, so
+ the block size == last + 1.
+ --*/
+ int last;
+
+ /*--
+ index in zptr[] of original string after sorting.
+ --*/
+ int origPtr;
+
+ /*--
+ always: in the range 0 .. 9.
+ The current block size is 100000 * this number.
+ --*/
+ int blockSize100k;
+
+ bool blockRandomised;
+
+ int bsBuff;
+ int bsLive;
+ IChecksum mCrc = new StrangeCRC();
+
+ bool[] inUse = new bool[256];
+ int nInUse;
+
+ byte[] seqToUnseq = new byte[256];
+ byte[] unseqToSeq = new byte[256];
+
+ byte[] selector = new byte[BZip2Constants.MAX_SELECTORS];
+ byte[] selectorMtf = new byte[BZip2Constants.MAX_SELECTORS];
+
+ int[] tt;
+ byte[] ll8;
+
+ /*--
+ freq table collected to save a pass over the data
+ during decompression.
+ --*/
+ int[] unzftab = new int[256];
+
+ int[][] limit = new int[BZip2Constants.N_GROUPS][];
+ int[][] baseArray = new int[BZip2Constants.N_GROUPS][];
+ int[][] perm = new int[BZip2Constants.N_GROUPS][];
+ int[] minLens = new int[BZip2Constants.N_GROUPS];
+
+ Stream baseStream;
+ bool streamEnd = false;
+
+ int currentChar = -1;
+
+ const int START_BLOCK_STATE = 1;
+ const int RAND_PART_A_STATE = 2;
+ const int RAND_PART_B_STATE = 3;
+ const int RAND_PART_C_STATE = 4;
+ const int NO_RAND_PART_A_STATE = 5;
+ const int NO_RAND_PART_B_STATE = 6;
+ const int NO_RAND_PART_C_STATE = 7;
+
+ int currentState = START_BLOCK_STATE;
+
+ int storedBlockCRC, storedCombinedCRC;
+ int computedBlockCRC;
+ uint computedCombinedCRC;
+
+ int count, chPrev, ch2;
+ int tPos;
+ int rNToGo = 0;
+ int rTPos = 0;
+ int i2, j2;
+ byte z;
+
+ /// <summary>
+ /// Construct instance for reading from stream
+ /// </summary>
+ /// <param name="stream">Data source</param>
+ public BZip2InputStream(Stream stream)
+ {
+ // init arrays
+ for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
+ limit[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
+ baseArray[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
+ perm[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
+ }
+
+ ll8 = null;
+ tt = null;
+ BsSetStream(stream);
+ Initialize();
+ InitBlock();
+ SetupBlock();
+ }
+
+ /// <summary>
+ /// Read a byte from stream advancing position
+ /// </summary>
+ /// <returns>byte read or -1 on end of stream</returns>
+ public override int ReadByte()
+ {
+ if (streamEnd) {
+ return -1; // ok
+ }
+
+ int retChar = currentChar;
+ switch (currentState) {
+ case RAND_PART_B_STATE:
+ SetupRandPartB();
+ break;
+ case RAND_PART_C_STATE:
+ SetupRandPartC();
+ break;
+ case NO_RAND_PART_B_STATE:
+ SetupNoRandPartB();
+ break;
+ case NO_RAND_PART_C_STATE:
+ SetupNoRandPartC();
+ break;
+ case START_BLOCK_STATE:
+ case NO_RAND_PART_A_STATE:
+ case RAND_PART_A_STATE:
+ break;
+ default:
+ break;
+ }
+ return retChar;
+ }
+
+ void Initialize()
+ {
+ char magic1 = BsGetUChar();
+ char magic2 = BsGetUChar();
+
+ char magic3 = BsGetUChar();
+ char magic4 = BsGetUChar();
+
+ if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9') {
+ streamEnd = true;
+ return;
+ }
+
+ SetDecompressStructureSizes(magic4 - '0');
+ computedCombinedCRC = 0;
+ }
+
+ void InitBlock()
+ {
+ char magic1 = BsGetUChar();
+ char magic2 = BsGetUChar();
+ char magic3 = BsGetUChar();
+ char magic4 = BsGetUChar();
+ char magic5 = BsGetUChar();
+ char magic6 = BsGetUChar();
+
+ if (magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90) {
+ Complete();
+ return;
+ }
+
+ if (magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59) {
+ BadBlockHeader();
+ streamEnd = true;
+ return;
+ }
+
+ storedBlockCRC = BsGetInt32();
+
+ blockRandomised = (BsR(1) == 1);
+
+ GetAndMoveToFrontDecode();
+
+ mCrc.Reset();
+ currentState = START_BLOCK_STATE;
+ }
+
+ void EndBlock()
+ {
+ computedBlockCRC = (int)mCrc.Value;
+
+ /*-- A bad CRC is considered a fatal error. --*/
+ if (storedBlockCRC != computedBlockCRC) {
+ CrcError();
+ }
+
+ // 1528150659
+ computedCombinedCRC = ((computedCombinedCRC << 1) & 0xFFFFFFFF) | (computedCombinedCRC >> 31);
+ computedCombinedCRC = computedCombinedCRC ^ (uint)computedBlockCRC;
+ }
+
+ void Complete()
+ {
+ storedCombinedCRC = BsGetInt32();
+ if (storedCombinedCRC != (int)computedCombinedCRC) {
+ CrcError();
+ }
+
+ streamEnd = true;
+ }
+
+ static void CompressedStreamEOF()
+ {
+ throw new BZip2Exception("BZip2 input stream end of compressed stream");
+ }
+
+ static void BlockOverrun()
+ {
+ throw new BZip2Exception("BZip2 input stream block overrun");
+ }
+
+ static void BadBlockHeader()
+ {
+ throw new BZip2Exception("BZip2 input stream bad block header");
+ }
+
+ static void CrcError()
+ {
+ throw new BZip2Exception("BZip2 input stream crc error");
+ }
+
+
+ void BsSetStream(Stream f)
+ {
+ baseStream = f;
+ bsLive = 0;
+ bsBuff = 0;
+ }
+
+ void FillBuffer()
+ {
+ int thech = 0;
+
+ try {
+ thech = baseStream.ReadByte();
+ } catch (Exception) {
+ CompressedStreamEOF();
+ }
+
+ if (thech == -1) {
+ CompressedStreamEOF();
+ }
+
+ bsBuff = (bsBuff << 8) | (thech & 0xFF);
+ bsLive += 8;
+ }
+
+ int BsR(int n)
+ {
+ while (bsLive < n) {
+ FillBuffer();
+ }
+
+ int v = (bsBuff >> (bsLive - n)) & ((1 << n) - 1);
+ bsLive -= n;
+ return v;
+ }
+
+ char BsGetUChar()
+ {
+ return (char)BsR(8);
+ }
+
+ int BsGetint()
+ {
+ int u = 0;
+ u = (u << 8) | BsR(8);
+ u = (u << 8) | BsR(8);
+ u = (u << 8) | BsR(8);
+ u = (u << 8) | BsR(8);
+ return u;
+ }
+
+ int BsGetIntVS(int numBits)
+ {
+ return (int)BsR(numBits);
+ }
+
+ int BsGetInt32()
+ {
+ return (int)BsGetint();
+ }
+
+ void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize)
+ {
+ int pp = 0;
+
+ for (int i = minLen; i <= maxLen; ++i) {
+ for (int j = 0; j < alphaSize; ++j) {
+ if (length[j] == i) {
+ perm[pp] = j;
+ ++pp;
+ }
+ }
+ }
+
+ for (int i = 0; i < BZip2Constants.MAX_CODE_LEN; i++) {
+ baseArray[i] = 0;
+ }
+
+ for (int i = 0; i < alphaSize; i++) {
+ ++baseArray[length[i] + 1];
+ }
+
+ for (int i = 1; i < BZip2Constants.MAX_CODE_LEN; i++) {
+ baseArray[i] += baseArray[i - 1];
+ }
+
+ for (int i = 0; i < BZip2Constants.MAX_CODE_LEN; i++) {
+ limit[i] = 0;
+ }
+
+ int vec = 0;
+
+ for (int i = minLen; i <= maxLen; i++) {
+ vec += (baseArray[i + 1] - baseArray[i]);
+ limit[i] = vec - 1;
+ vec <<= 1;
+ }
+
+ for (int i = minLen + 1; i <= maxLen; i++) {
+ baseArray[i] = ((limit[i - 1] + 1) << 1) - baseArray[i];
+ }
+ }
+
+ void RecvDecodingTables()
+ {
+ char[][] len = new char[BZip2Constants.N_GROUPS][];
+ for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
+ len[i] = new char[BZip2Constants.MAX_ALPHA_SIZE];
+ }
+
+ bool[] inUse16 = new bool[16];
+
+ /*--- Receive the mapping table ---*/
+ for (int i = 0; i < 16; i++) {
+ inUse16[i] = (BsR(1) == 1);
+ }
+
+ for (int i = 0; i < 16; i++) {
+ if (inUse16[i]) {
+ for (int j = 0; j < 16; j++) {
+ inUse[i * 16 + j] = (BsR(1) == 1);
+ }
+ } else {
+ for (int j = 0; j < 16; j++) {
+ inUse[i * 16 + j] = false;
+ }
+ }
+ }
+
+ MakeMaps();
+ int alphaSize = nInUse + 2;
+
+ /*--- Now the selectors ---*/
+ int nGroups = BsR(3);
+ int nSelectors = BsR(15);
+
+ for (int i = 0; i < nSelectors; i++) {
+ int j = 0;
+ while (BsR(1) == 1) {
+ j++;
+ }
+ selectorMtf[i] = (byte)j;
+ }
+
+ /*--- Undo the MTF values for the selectors. ---*/
+ byte[] pos = new byte[BZip2Constants.N_GROUPS];
+ for (int v = 0; v < nGroups; v++) {
+ pos[v] = (byte)v;
+ }
+
+ for (int i = 0; i < nSelectors; i++) {
+ int v = selectorMtf[i];
+ byte tmp = pos[v];
+ while (v > 0) {
+ pos[v] = pos[v - 1];
+ v--;
+ }
+ pos[0] = tmp;
+ selector[i] = tmp;
+ }
+
+ /*--- Now the coding tables ---*/
+ for (int t = 0; t < nGroups; t++) {
+ int curr = BsR(5);
+ for (int i = 0; i < alphaSize; i++) {
+ while (BsR(1) == 1) {
+ if (BsR(1) == 0) {
+ curr++;
+ } else {
+ curr--;
+ }
+ }
+ len[t][i] = (char)curr;
+ }
+ }
+
+ /*--- Create the Huffman decoding tables ---*/
+ for (int t = 0; t < nGroups; t++) {
+ int minLen = 32;
+ int maxLen = 0;
+ for (int i = 0; i < alphaSize; i++) {
+ maxLen = Math.Max(maxLen, len[t][i]);
+ minLen = Math.Min(minLen, len[t][i]);
+ }
+ HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize);
+ minLens[t] = minLen;
+ }
+ }
+
+ void GetAndMoveToFrontDecode()
+ {
+ byte[] yy = new byte[256];
+ int nextSym;
+
+ int limitLast = BZip2Constants.baseBlockSize * blockSize100k;
+ origPtr = BsGetIntVS(24);
+
+ RecvDecodingTables();
+ int EOB = nInUse+1;
+ int groupNo = -1;
+ int groupPos = 0;
+
+ /*--
+ Setting up the unzftab entries here is not strictly
+ necessary, but it does save having to do it later
+ in a separate pass, and so saves a block's worth of
+ cache misses.
+ --*/
+ for (int i = 0; i <= 255; i++) {
+ unzftab[i] = 0;
+ }
+
+ for (int i = 0; i <= 255; i++) {
+ yy[i] = (byte)i;
+ }
+
+ last = -1;
+
+ if (groupPos == 0) {
+ groupNo++;
+ groupPos = BZip2Constants.G_SIZE;
+ }
+
+ groupPos--;
+ int zt = selector[groupNo];
+ int zn = minLens[zt];
+ int zvec = BsR(zn);
+ int zj;
+
+ while (zvec > limit[zt][zn]) {
+ if (zn > 20) { // the longest code
+ throw new BZip2Exception("Bzip data error"); // -jr- 17-Dec-2003 from bzip 1.02 why 20???
+ }
+ zn++;
+ while (bsLive < 1) {
+ FillBuffer();
+ }
+ zj = (bsBuff >> (bsLive-1)) & 1;
+ bsLive--;
+ zvec = (zvec << 1) | zj;
+ }
+ if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MAX_ALPHA_SIZE) {
+ throw new BZip2Exception("Bzip data error"); // -jr- 17-Dec-2003 from bzip 1.02
+ }
+ nextSym = perm[zt][zvec - baseArray[zt][zn]];
+
+ while (true) {
+ if (nextSym == EOB) {
+ break;
+ }
+
+ if (nextSym == BZip2Constants.RUNA || nextSym == BZip2Constants.RUNB) {
+ int s = -1;
+ int n = 1;
+ do {
+ if (nextSym == BZip2Constants.RUNA) {
+ s += (0 + 1) * n;
+ } else if (nextSym == BZip2Constants.RUNB) {
+ s += (1 + 1) * n;
+ }
+
+ n <<= 1;
+
+ if (groupPos == 0) {
+ groupNo++;
+ groupPos = BZip2Constants.G_SIZE;
+ }
+
+ groupPos--;
+
+ zt = selector[groupNo];
+ zn = minLens[zt];
+ zvec = BsR(zn);
+
+ while (zvec > limit[zt][zn]) {
+ zn++;
+ while (bsLive < 1) {
+ FillBuffer();
+ }
+ zj = (bsBuff >> (bsLive - 1)) & 1;
+ bsLive--;
+ zvec = (zvec << 1) | zj;
+ }
+ nextSym = perm[zt][zvec - baseArray[zt][zn]];
+ } while (nextSym == BZip2Constants.RUNA || nextSym == BZip2Constants.RUNB);
+
+ s++;
+ byte ch = seqToUnseq[yy[0]];
+ unzftab[ch] += s;
+
+ while (s > 0) {
+ last++;
+ ll8[last] = ch;
+ s--;
+ }
+
+ if (last >= limitLast) {
+ BlockOverrun();
+ }
+ continue;
+ } else {
+ last++;
+ if (last >= limitLast) {
+ BlockOverrun();
+ }
+
+ byte tmp = yy[nextSym - 1];
+ unzftab[seqToUnseq[tmp]]++;
+ ll8[last] = seqToUnseq[tmp];
+
+ for (int j = nextSym-1; j > 0; --j) {
+ yy[j] = yy[j - 1];
+ }
+ yy[0] = tmp;
+
+ if (groupPos == 0) {
+ groupNo++;
+ groupPos = BZip2Constants.G_SIZE;
+ }
+
+ groupPos--;
+ zt = selector[groupNo];
+ zn = minLens[zt];
+ zvec = BsR(zn);
+ while (zvec > limit[zt][zn]) {
+ zn++;
+ while (bsLive < 1) {
+ FillBuffer();
+ }
+ zj = (bsBuff >> (bsLive-1)) & 1;
+ bsLive--;
+ zvec = (zvec << 1) | zj;
+ }
+ nextSym = perm[zt][zvec - baseArray[zt][zn]];
+ continue;
+ }
+ }
+ }
+
+ void SetupBlock()
+ {
+ int[] cftab = new int[257];
+
+ cftab[0] = 0;
+ Array.Copy(unzftab, 0, cftab, 1, 256);
+
+ for (int i = 1; i <= 256; i++) {
+ cftab[i] += cftab[i - 1];
+ }
+
+ for (int i = 0; i <= last; i++) {
+ byte ch = ll8[i];
+ tt[cftab[ch]] = i;
+ cftab[ch]++;
+ }
+
+ cftab = null;
+
+ tPos = tt[origPtr];
+
+ count = 0;
+ i2 = 0;
+ ch2 = 256; /*-- not a char and not EOF --*/
+
+ if (blockRandomised) {
+ rNToGo = 0;
+ rTPos = 0;
+ SetupRandPartA();
+ } else {
+ SetupNoRandPartA();
+ }
+ }
+
+ void SetupRandPartA()
+ {
+ if (i2 <= last) {
+ chPrev = ch2;
+ ch2 = ll8[tPos];
+ tPos = tt[tPos];
+ if (rNToGo == 0) {
+ rNToGo = BZip2Constants.rNums[rTPos];
+ rTPos++;
+ if (rTPos == 512) {
+ rTPos = 0;
+ }
+ }
+ rNToGo--;
+ ch2 ^= (int)((rNToGo == 1) ? 1 : 0);
+ i2++;
+
+ currentChar = ch2;
+ currentState = RAND_PART_B_STATE;
+ mCrc.Update(ch2);
+ } else {
+ EndBlock();
+ InitBlock();
+ SetupBlock();
+ }
+ }
+
+ void SetupNoRandPartA()
+ {
+ if (i2 <= last) {
+ chPrev = ch2;
+ ch2 = ll8[tPos];
+ tPos = tt[tPos];
+ i2++;
+
+ currentChar = ch2;
+ currentState = NO_RAND_PART_B_STATE;
+ mCrc.Update(ch2);
+ } else {
+ EndBlock();
+ InitBlock();
+ SetupBlock();
+ }
+ }
+
+ void SetupRandPartB()
+ {
+ if (ch2 != chPrev) {
+ currentState = RAND_PART_A_STATE;
+ count = 1;
+ SetupRandPartA();
+ } else {
+ count++;
+ if (count >= 4) {
+ z = ll8[tPos];
+ tPos = tt[tPos];
+ if (rNToGo == 0) {
+ rNToGo = BZip2Constants.rNums[rTPos];
+ rTPos++;
+ if (rTPos == 512) {
+ rTPos = 0;
+ }
+ }
+ rNToGo--;
+ z ^= (byte)((rNToGo == 1) ? 1 : 0);
+ j2 = 0;
+ currentState = RAND_PART_C_STATE;
+ SetupRandPartC();
+ } else {
+ currentState = RAND_PART_A_STATE;
+ SetupRandPartA();
+ }
+ }
+ }
+
+ void SetupRandPartC()
+ {
+ if (j2 < (int)z) {
+ currentChar = ch2;
+ mCrc.Update(ch2);
+ j2++;
+ } else {
+ currentState = RAND_PART_A_STATE;
+ i2++;
+ count = 0;
+ SetupRandPartA();
+ }
+ }
+
+ void SetupNoRandPartB()
+ {
+ if (ch2 != chPrev) {
+ currentState = NO_RAND_PART_A_STATE;
+ count = 1;
+ SetupNoRandPartA();
+ } else {
+ count++;
+ if (count >= 4) {
+ z = ll8[tPos];
+ tPos = tt[tPos];
+ currentState = NO_RAND_PART_C_STATE;
+ j2 = 0;
+ SetupNoRandPartC();
+ } else {
+ currentState = NO_RAND_PART_A_STATE;
+ SetupNoRandPartA();
+ }
+ }
+ }
+
+ void SetupNoRandPartC()
+ {
+ if (j2 < (int)z) {
+ currentChar = ch2;
+ mCrc.Update(ch2);
+ j2++;
+ } else {
+ currentState = NO_RAND_PART_A_STATE;
+ i2++;
+ count = 0;
+ SetupNoRandPartA();
+ }
+ }
+
+ void SetDecompressStructureSizes(int newSize100k)
+ {
+ if (!(0 <= newSize100k && newSize100k <= 9 && 0 <= blockSize100k && blockSize100k <= 9)) {
+ throw new BZip2Exception("Invalid block size");
+ }
+
+ blockSize100k = newSize100k;
+
+ if (newSize100k == 0) {
+ return;
+ }
+
+ int n = BZip2Constants.baseBlockSize * newSize100k;
+ ll8 = new byte[n];
+ tt = new int[n];
+ }
+ }
+}
+/* This file was derived from a file containing under this license:
+ *
+ * This file is a part of bzip2 and/or libbzip2, a program and
+ * library for lossless, block-sorting data compression.
+ *
+ * Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. The origin of this software must not be misrepresented; you must
+ * not claim that you wrote the original software. If you use this
+ * software in a product, an acknowledgment in the product
+ * documentation would be appreciated but is not required.
+ *
+ * 3. Altered source versions must be plainly marked as such, and must
+ * not be misrepresented as being the original software.
+ *
+ * 4. The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Java version ported by Keiron Liddle, Aftex Software <keiron@aftexsw.com> 1999-2001
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
new file mode 100644
index 00000000000..d965f22fbbf
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
@@ -0,0 +1,1804 @@
+// BZip2OutputStream.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Checksums;
+
+namespace ICSharpCode.SharpZipLib.BZip2
+{
+
+ // TODO: Update to BZip2 1.0.1, 1.0.2
+
+ /// <summary>
+ /// An output stream that compresses into the BZip2 format
+ /// including file header chars into another stream.
+ /// </summary>
+ public class BZip2OutputStream : Stream
+ {
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports reading
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports seeking
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports writing
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return baseStream.CanWrite;
+ }
+ }
+
+ /// <summary>
+ /// Gets the length in bytes of the stream
+ /// </summary>
+ public override long Length {
+ get {
+ return baseStream.Length;
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the current position of this stream.
+ /// </summary>
+ public override long Position {
+ get {
+ return baseStream.Position;
+ }
+ set {
+ throw new NotSupportedException("BZip2OutputStream position cannot be set");
+ }
+ }
+
+ /// <summary>
+ /// Sets the current position of this stream to the given value.
+ /// </summary>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException("BZip2OutputStream Seek not supported");
+ }
+
+ /// <summary>
+ /// Sets the length of this stream to the given value.
+ /// </summary>
+ public override void SetLength(long val)
+ {
+ throw new NotSupportedException("BZip2OutputStream SetLength not supported");
+ }
+
+ /// <summary>
+ /// Read a byte from the stream advancing the position.
+ /// </summary>
+ public override int ReadByte()
+ {
+ throw new NotSupportedException("BZip2OutputStream ReadByte not supported");
+ }
+
+ /// <summary>
+ /// Read a block of bytes
+ /// </summary>
+ public override int Read(byte[] b, int off, int len)
+ {
+ throw new NotSupportedException("BZip2OutputStream Read not supported");
+ }
+
+ /// <summary>
+ /// Write a block of bytes to the stream
+ /// </summary>
+ public override void Write(byte[] buf, int off, int len)
+ {
+ for (int i = 0; i < len; ++i) {
+ WriteByte(buf[off + i]);
+ }
+ }
+
+ readonly static int SETMASK = (1 << 21);
+ readonly static int CLEARMASK = (~SETMASK);
+ readonly static int GREATER_ICOST = 15;
+ readonly static int LESSER_ICOST = 0;
+ readonly static int SMALL_THRESH = 20;
+ readonly static int DEPTH_THRESH = 10;
+
+ /*--
+ If you are ever unlucky/improbable enough
+ to get a stack overflow whilst sorting,
+ increase the following constant and try
+ again. In practice I have never seen the
+ stack go above 27 elems, so the following
+ limit seems very generous.
+ --*/
+ readonly static int QSORT_STACK_SIZE = 1000;
+
+ static void Panic()
+ {
+ throw new BZip2Exception("BZip2 output stream panic");
+ }
+
+ void MakeMaps()
+ {
+ int i;
+ nInUse = 0;
+ for (i = 0; i < 256; i++) {
+ if (inUse[i]) {
+ seqToUnseq[nInUse] = (char)i;
+ unseqToSeq[i] = (char)nInUse;
+ nInUse++;
+ }
+ }
+ }
+
+ static void HbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen)
+ {
+ /*--
+ Nodes and heap entries run from 1. Entry 0
+ for both the heap and nodes is a sentinel.
+ --*/
+ int nNodes, nHeap, n1, n2, j, k;
+ bool tooLong;
+
+ int[] heap = new int[BZip2Constants.MAX_ALPHA_SIZE + 2];
+ int[] weight = new int[BZip2Constants.MAX_ALPHA_SIZE * 2];
+ int[] parent = new int[BZip2Constants.MAX_ALPHA_SIZE * 2];
+
+ for (int i = 0; i < alphaSize; ++i) {
+ weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
+ }
+
+ while (true) {
+ nNodes = alphaSize;
+ nHeap = 0;
+
+ heap[0] = 0;
+ weight[0] = 0;
+ parent[0] = -2;
+
+ for (int i = 1; i <= alphaSize; ++i) {
+ parent[i] = -1;
+ nHeap++;
+ heap[nHeap] = i;
+ int zz = nHeap;
+ int tmp = heap[zz];
+ while (weight[tmp] < weight[heap[zz >> 1]]) {
+ heap[zz] = heap[zz >> 1];
+ zz >>= 1;
+ }
+ heap[zz] = tmp;
+ }
+ if (!(nHeap < (BZip2Constants.MAX_ALPHA_SIZE+2))) {
+ Panic();
+ }
+
+ while (nHeap > 1) {
+ n1 = heap[1];
+ heap[1] = heap[nHeap];
+ nHeap--;
+ int zz = 1;
+ int yy = 0;
+ int tmp = heap[zz];
+ while (true) {
+ yy = zz << 1;
+ if (yy > nHeap) {
+ break;
+ }
+ if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) {
+ yy++;
+ }
+ if (weight[tmp] < weight[heap[yy]]) {
+ break;
+ }
+
+ heap[zz] = heap[yy];
+ zz = yy;
+ }
+ heap[zz] = tmp;
+ n2 = heap[1];
+ heap[1] = heap[nHeap];
+ nHeap--;
+
+ zz = 1;
+ yy = 0;
+ tmp = heap[zz];
+ while (true) {
+ yy = zz << 1;
+ if (yy > nHeap) {
+ break;
+ }
+ if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) {
+ yy++;
+ }
+ if (weight[tmp] < weight[heap[yy]]) {
+ break;
+ }
+ heap[zz] = heap[yy];
+ zz = yy;
+ }
+ heap[zz] = tmp;
+ nNodes++;
+ parent[n1] = parent[n2] = nNodes;
+
+ weight[nNodes] = (int)((weight[n1] & 0xffffff00) + (weight[n2] & 0xffffff00)) |
+ (int)(1 + (((weight[n1] & 0x000000ff) > (weight[n2] & 0x000000ff)) ? (weight[n1] & 0x000000ff) : (weight[n2] & 0x000000ff)));
+
+ parent[nNodes] = -1;
+ nHeap++;
+ heap[nHeap] = nNodes;
+
+ zz = nHeap;
+ tmp = heap[zz];
+ while (weight[tmp] < weight[heap[zz >> 1]]) {
+ heap[zz] = heap[zz >> 1];
+ zz >>= 1;
+ }
+ heap[zz] = tmp;
+ }
+ if (!(nNodes < (BZip2Constants.MAX_ALPHA_SIZE * 2))) {
+ Panic();
+ }
+
+ tooLong = false;
+ for (int i = 1; i <= alphaSize; ++i) {
+ j = 0;
+ k = i;
+ while (parent[k] >= 0) {
+ k = parent[k];
+ j++;
+ }
+ len[i - 1] = (char)j;
+ if (j > maxLen) {
+ tooLong = true;
+ }
+ }
+
+ if (!tooLong) {
+ break;
+ }
+
+ for (int i = 1; i < alphaSize; ++i) {
+ j = weight[i] >> 8;
+ j = 1 + (j / 2);
+ weight[i] = j << 8;
+ }
+ }
+ }
+
+ /*--
+ index of the last char in the block, so
+ the block size == last + 1.
+ --*/
+ int last;
+
+ /*--
+ index in zptr[] of original string after sorting.
+ --*/
+ int origPtr;
+
+ /*--
+ always: in the range 0 .. 9.
+ The current block size is 100000 * this number.
+ --*/
+ int blockSize100k;
+
+ bool blockRandomised;
+
+ int bytesOut;
+ int bsBuff;
+ int bsLive;
+ IChecksum mCrc = new StrangeCRC();
+
+ bool[] inUse = new bool[256];
+ int nInUse;
+
+ char[] seqToUnseq = new char[256];
+ char[] unseqToSeq = new char[256];
+
+ char[] selector = new char[BZip2Constants.MAX_SELECTORS];
+ char[] selectorMtf = new char[BZip2Constants.MAX_SELECTORS];
+
+ byte[] block;
+ int[] quadrant;
+ int[] zptr;
+ short[] szptr;
+ int[] ftab;
+
+ int nMTF;
+
+ int[] mtfFreq = new int[BZip2Constants.MAX_ALPHA_SIZE];
+
+ /*
+ * Used when sorting. If too many long comparisons
+ * happen, we stop sorting, randomise the block
+ * slightly, and try again.
+ */
+ int workFactor;
+ int workDone;
+ int workLimit;
+ bool firstAttempt;
+ int nBlocksRandomised;
+
+ int currentChar = -1;
+ int runLength = 0;
+
+ /// <summary>
+ /// Construct a default output stream with maximum block size
+ /// </summary>
+ /// <param name="stream">The stream to write BZip data onto.</param>
+ public BZip2OutputStream(Stream stream) : this(stream, 9)
+ {
+ }
+
+ /// <summary>
+ /// Initialise a new instance of the <see cref="BZip2OutputStream"></see>
+ /// for the specified stream, using the given blocksize.
+ /// </summary>
+ /// <param name="stream">The stream to write compressed data to.</param>
+ /// <param name="blockSize">The block size to use.</param>
+ /// <remarks>
+ /// Valid block sizes are in the range 1..9, with 1 giving
+ /// the lowest compression and 9 the highest.
+ /// </remarks>
+ public BZip2OutputStream(Stream stream, int blockSize)
+ {
+ block = null;
+ quadrant = null;
+ zptr = null;
+ ftab = null;
+
+ BsSetStream(stream);
+
+ workFactor = 50;
+ if (blockSize > 9) {
+ blockSize = 9;
+ }
+ if (blockSize < 1) {
+ blockSize = 1;
+ }
+ blockSize100k = blockSize;
+ AllocateCompressStructures();
+ Initialize();
+ InitBlock();
+ }
+
+ /// <summary>
+ /// Write a byte to the stream.
+ /// </summary>
+ public override void WriteByte(byte bv)
+ {
+ int b = (256 + bv) % 256;
+ if (currentChar != -1) {
+ if (currentChar == b) {
+ runLength++;
+ if (runLength > 254) {
+ WriteRun();
+ currentChar = -1;
+ runLength = 0;
+ }
+ } else {
+ WriteRun();
+ runLength = 1;
+ currentChar = b;
+ }
+ } else {
+ currentChar = b;
+ runLength++;
+ }
+ }
+
+ void WriteRun()
+ {
+ if (last < allowableBlockSize) {
+ inUse[currentChar] = true;
+ for (int i = 0; i < runLength; i++) {
+ mCrc.Update(currentChar);
+ }
+
+ switch (runLength) {
+ case 1:
+ last++;
+ block[last + 1] = (byte)currentChar;
+ break;
+ case 2:
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ break;
+ case 3:
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ break;
+ default:
+ inUse[runLength - 4] = true;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)currentChar;
+ last++;
+ block[last + 1] = (byte)(runLength - 4);
+ break;
+ }
+ } else {
+ EndBlock();
+ InitBlock();
+ WriteRun();
+ }
+ }
+
+ bool closed = false;
+
+ /// <summary>
+ /// Free any resources and other cleanup before garbage collection reclaims memory
+ /// </summary>
+ ~BZip2OutputStream()
+ {
+ Close();
+ }
+
+ /// <summary>
+ /// End the current block and end compression.
+ /// Close the stream and free any resources
+ /// </summary>
+ public override void Close()
+ {
+ if (!closed) {
+ closed = true;
+
+ if (runLength > 0) {
+ WriteRun();
+ }
+
+ currentChar = -1;
+ EndBlock();
+ EndCompression();
+ Flush();
+ baseStream.Close();
+ }
+ }
+
+ /// <summary>
+ /// Flush output buffers
+ /// </summary>
+ public override void Flush()
+ {
+ baseStream.Flush();
+ }
+
+ uint blockCRC, combinedCRC;
+
+ void Initialize()
+ {
+ bytesOut = 0;
+ nBlocksRandomised = 0;
+
+ /*--- Write `magic' bytes h indicating file-format == huffmanised,
+ followed by a digit indicating blockSize100k.
+ ---*/
+
+ // TODO adding header here should be optional?
+ BsPutUChar('B');
+ BsPutUChar('Z');
+
+ BsPutUChar('h');
+ BsPutUChar('0' + blockSize100k);
+
+ combinedCRC = 0;
+ }
+
+ int allowableBlockSize;
+
+ void InitBlock()
+ {
+ // blockNo++;
+ mCrc.Reset();
+ last = -1;
+ // ch = 0;
+
+ for (int i = 0; i < 256; i++) {
+ inUse[i] = false;
+ }
+
+ /*--- 20 is just a paranoia constant ---*/
+ allowableBlockSize = BZip2Constants.baseBlockSize * blockSize100k - 20;
+ }
+
+ void EndBlock()
+ {
+ if (last < 0) { // dont do anything for empty files, (makes empty files compatible with original Bzip)
+ return;
+ }
+
+ blockCRC = (uint)mCrc.Value;
+ combinedCRC = (combinedCRC << 1) | (combinedCRC >> 31);
+ combinedCRC ^= blockCRC;
+
+ /*-- sort the block and establish posn of original string --*/
+ DoReversibleTransformation();
+
+ /*--
+ A 6-byte block header, the value chosen arbitrarily
+ as 0x314159265359 :-). A 32 bit value does not really
+ give a strong enough guarantee that the value will not
+ appear by chance in the compressed datastream. Worst-case
+ probability of this event, for a 900k block, is about
+ 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48 bits.
+ For a compressed file of size 100Gb -- about 100000 blocks --
+ only a 48-bit marker will do. NB: normal compression/
+ decompression do *not* rely on these statistical properties.
+ They are only important when trying to recover blocks from
+ damaged files.
+ --*/
+ BsPutUChar(0x31);
+ BsPutUChar(0x41);
+ BsPutUChar(0x59);
+ BsPutUChar(0x26);
+ BsPutUChar(0x53);
+ BsPutUChar(0x59);
+
+ /*-- Now the block's CRC, so it is in a known place. --*/
+ BsPutint((int)blockCRC);
+
+ /*-- Now a single bit indicating randomisation. --*/
+ if (blockRandomised) {
+ BsW(1,1);
+ nBlocksRandomised++;
+ } else {
+ BsW(1,0);
+ }
+
+ /*-- Finally, block's contents proper. --*/
+ MoveToFrontCodeAndSend();
+ }
+
+ void EndCompression()
+ {
+ /*--
+ Now another magic 48-bit number, 0x177245385090, to
+ indicate the end of the last block. (sqrt(pi), if
+ you want to know. I did want to use e, but it contains
+ too much repetition -- 27 18 28 18 28 46 -- for me
+ to feel statistically comfortable. Call me paranoid.)
+ --*/
+ BsPutUChar(0x17);
+ BsPutUChar(0x72);
+ BsPutUChar(0x45);
+ BsPutUChar(0x38);
+ BsPutUChar(0x50);
+ BsPutUChar(0x90);
+
+ BsPutint((int)combinedCRC);
+
+ BsFinishedWithStream();
+ }
+
+ void HbAssignCodes (int[] code, char[] length, int minLen, int maxLen, int alphaSize)
+ {
+ int vec = 0;
+ for (int n = minLen; n <= maxLen; ++n) {
+ for (int i = 0; i < alphaSize; ++i) {
+ if (length[i] == n) {
+ code[i] = vec;
+ ++vec;
+ }
+ }
+ vec <<= 1;
+ }
+ }
+
+ void BsSetStream(Stream f)
+ {
+ baseStream = f;
+ bsLive = 0;
+ bsBuff = 0;
+ bytesOut = 0;
+ }
+
+ void BsFinishedWithStream()
+ {
+ while (bsLive > 0)
+ {
+ int ch = (bsBuff >> 24);
+ baseStream.WriteByte((byte)ch); // write 8-bit
+ bsBuff <<= 8;
+ bsLive -= 8;
+ bytesOut++;
+ }
+ }
+
+ void BsW(int n, int v)
+ {
+ while (bsLive >= 8) {
+ int ch = (bsBuff >> 24);
+ baseStream.WriteByte((byte)ch); // write 8-bit
+ bsBuff <<= 8;
+ bsLive -= 8;
+ ++bytesOut;
+ }
+ bsBuff |= (v << (32 - bsLive - n));
+ bsLive += n;
+ }
+
+ void BsPutUChar(int c)
+ {
+ BsW(8, c);
+ }
+
+ void BsPutint(int u)
+ {
+ BsW(8, (u >> 24) & 0xFF);
+ BsW(8, (u >> 16) & 0xFF);
+ BsW(8, (u >> 8) & 0xFF);
+ BsW(8, u & 0xFF);
+ }
+
+ void BsPutIntVS(int numBits, int c)
+ {
+ BsW(numBits, c);
+ }
+
+ void SendMTFValues()
+ {
+ char[][] len = new char[BZip2Constants.N_GROUPS][];
+ for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
+ len[i] = new char[BZip2Constants.MAX_ALPHA_SIZE];
+ }
+
+ int gs, ge, totc, bt, bc, iter;
+ int nSelectors = 0, alphaSize, minLen, maxLen, selCtr;
+ int nGroups, nBytes;
+
+ alphaSize = nInUse + 2;
+ for (int t = 0; t < BZip2Constants.N_GROUPS; t++) {
+ for (int v = 0; v < alphaSize; v++) {
+ len[t][v] = (char)GREATER_ICOST;
+ }
+ }
+
+ /*--- Decide how many coding tables to use ---*/
+ if (nMTF <= 0) {
+ Panic();
+ }
+
+ if (nMTF < 200) {
+ nGroups = 2;
+ } else if (nMTF < 600) {
+ nGroups = 3;
+ } else if (nMTF < 1200) {
+ nGroups = 4;
+ } else if (nMTF < 2400) {
+ nGroups = 5;
+ } else {
+ nGroups = 6;
+ }
+
+ /*--- Generate an initial set of coding tables ---*/
+ int nPart = nGroups;
+ int remF = nMTF;
+ gs = 0;
+ while (nPart > 0) {
+ int tFreq = remF / nPart;
+ int aFreq = 0;
+ ge = gs - 1;
+ while (aFreq < tFreq && ge < alphaSize - 1) {
+ ge++;
+ aFreq += mtfFreq[ge];
+ }
+
+ if (ge > gs && nPart != nGroups && nPart != 1 && ((nGroups - nPart) % 2 == 1)) {
+ aFreq -= mtfFreq[ge];
+ ge--;
+ }
+
+ for (int v = 0; v < alphaSize; v++) {
+ if (v >= gs && v <= ge) {
+ len[nPart - 1][v] = (char)LESSER_ICOST;
+ } else {
+ len[nPart - 1][v] = (char)GREATER_ICOST;
+ }
+ }
+
+ nPart--;
+ gs = ge + 1;
+ remF -= aFreq;
+ }
+
+ int[][] rfreq = new int[BZip2Constants.N_GROUPS][];
+ for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
+ rfreq[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
+ }
+
+ int[] fave = new int[BZip2Constants.N_GROUPS];
+ short[] cost = new short[BZip2Constants.N_GROUPS];
+ /*---
+ Iterate up to N_ITERS times to improve the tables.
+ ---*/
+ for (iter = 0; iter < BZip2Constants.N_ITERS; ++iter) {
+ for (int t = 0; t < nGroups; ++t) {
+ fave[t] = 0;
+ }
+
+ for (int t = 0; t < nGroups; ++t) {
+ for (int v = 0; v < alphaSize; ++v) {
+ rfreq[t][v] = 0;
+ }
+ }
+
+ nSelectors = 0;
+ totc = 0;
+ gs = 0;
+ while (true) {
+ /*--- Set group start & end marks. --*/
+ if (gs >= nMTF) {
+ break;
+ }
+ ge = gs + BZip2Constants.G_SIZE - 1;
+ if (ge >= nMTF) {
+ ge = nMTF - 1;
+ }
+
+ /*--
+ Calculate the cost of this group as coded
+ by each of the coding tables.
+ --*/
+ for (int t = 0; t < nGroups; t++) {
+ cost[t] = 0;
+ }
+
+ if (nGroups == 6) {
+ short cost0, cost1, cost2, cost3, cost4, cost5;
+ cost0 = cost1 = cost2 = cost3 = cost4 = cost5 = 0;
+ for (int i = gs; i <= ge; ++i) {
+ short icv = szptr[i];
+ cost0 += (short)len[0][icv];
+ cost1 += (short)len[1][icv];
+ cost2 += (short)len[2][icv];
+ cost3 += (short)len[3][icv];
+ cost4 += (short)len[4][icv];
+ cost5 += (short)len[5][icv];
+ }
+ cost[0] = cost0;
+ cost[1] = cost1;
+ cost[2] = cost2;
+ cost[3] = cost3;
+ cost[4] = cost4;
+ cost[5] = cost5;
+ } else {
+ for (int i = gs; i <= ge; ++i) {
+ short icv = szptr[i];
+ for (int t = 0; t < nGroups; t++) {
+ cost[t] += (short)len[t][icv];
+ }
+ }
+ }
+
+ /*--
+ Find the coding table which is best for this group,
+ and record its identity in the selector table.
+ --*/
+ bc = 999999999;
+ bt = -1;
+ for (int t = 0; t < nGroups; ++t) {
+ if (cost[t] < bc) {
+ bc = cost[t];
+ bt = t;
+ }
+ }
+ totc += bc;
+ fave[bt]++;
+ selector[nSelectors] = (char)bt;
+ nSelectors++;
+
+ /*--
+ Increment the symbol frequencies for the selected table.
+ --*/
+ for (int i = gs; i <= ge; ++i) {
+ ++rfreq[bt][szptr[i]];
+ }
+
+ gs = ge+1;
+ }
+
+ /*--
+ Recompute the tables based on the accumulated frequencies.
+ --*/
+ for (int t = 0; t < nGroups; ++t) {
+ HbMakeCodeLengths(len[t], rfreq[t], alphaSize, 20);
+ }
+ }
+
+ rfreq = null;
+ fave = null;
+ cost = null;
+
+ if (!(nGroups < 8)) {
+ Panic();
+ }
+ if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZip2Constants.G_SIZE)))) {
+ Panic();
+ }
+
+ /*--- Compute MTF values for the selectors. ---*/
+ char[] pos = new char[BZip2Constants.N_GROUPS];
+ char ll_i, tmp2, tmp;
+ for (int i = 0; i < nGroups; i++) {
+ pos[i] = (char)i;
+ }
+ for (int i = 0; i < nSelectors; i++) {
+ ll_i = selector[i];
+ int j = 0;
+ tmp = pos[j];
+ while (ll_i != tmp) {
+ j++;
+ tmp2 = tmp;
+ tmp = pos[j];
+ pos[j] = tmp2;
+ }
+ pos[0] = tmp;
+ selectorMtf[i] = (char)j;
+ }
+
+ int[][] code = new int[BZip2Constants.N_GROUPS][];
+
+ for (int i = 0; i < BZip2Constants.N_GROUPS; ++i) {
+ code[i] = new int[BZip2Constants.MAX_ALPHA_SIZE];
+ }
+
+ /*--- Assign actual codes for the tables. --*/
+ for (int t = 0; t < nGroups; t++) {
+ minLen = 32;
+ maxLen = 0;
+ for (int i = 0; i < alphaSize; i++) {
+ if (len[t][i] > maxLen) {
+ maxLen = len[t][i];
+ }
+ if (len[t][i] < minLen) {
+ minLen = len[t][i];
+ }
+ }
+ if (maxLen > 20) {
+ Panic();
+ }
+ if (minLen < 1) {
+ Panic();
+ }
+ HbAssignCodes(code[t], len[t], minLen, maxLen, alphaSize);
+ }
+
+ /*--- Transmit the mapping table. ---*/
+ bool[] inUse16 = new bool[16];
+ for (int i = 0; i < 16; ++i) {
+ inUse16[i] = false;
+ for (int j = 0; j < 16; ++j) {
+ if (inUse[i * 16 + j]) {
+ inUse16[i] = true;
+ }
+ }
+ }
+
+ nBytes = bytesOut;
+ for (int i = 0; i < 16; ++i) {
+ if (inUse16[i]) {
+ BsW(1,1);
+ } else {
+ BsW(1,0);
+ }
+ }
+
+ for (int i = 0; i < 16; ++i) {
+ if (inUse16[i]) {
+ for (int j = 0; j < 16; ++j) {
+ if (inUse[i * 16 + j]) {
+ BsW(1,1);
+ } else {
+ BsW(1,0);
+ }
+ }
+ }
+ }
+
+ /*--- Now the selectors. ---*/
+ nBytes = bytesOut;
+ BsW(3, nGroups);
+ BsW(15, nSelectors);
+ for (int i = 0; i < nSelectors; ++i) {
+ for (int j = 0; j < selectorMtf[i]; ++j) {
+ BsW(1,1);
+ }
+ BsW(1,0);
+ }
+
+ /*--- Now the coding tables. ---*/
+ nBytes = bytesOut;
+
+ for (int t = 0; t < nGroups; ++t) {
+ int curr = len[t][0];
+ BsW(5, curr);
+ for (int i = 0; i < alphaSize; ++i) {
+ while (curr < len[t][i]) {
+ BsW(2, 2);
+ curr++; /* 10 */
+ }
+ while (curr > len[t][i]) {
+ BsW(2, 3);
+ curr--; /* 11 */
+ }
+ BsW (1, 0);
+ }
+ }
+
+ /*--- And finally, the block data proper ---*/
+ nBytes = bytesOut;
+ selCtr = 0;
+ gs = 0;
+ while (true) {
+ if (gs >= nMTF) {
+ break;
+ }
+ ge = gs + BZip2Constants.G_SIZE - 1;
+ if (ge >= nMTF) {
+ ge = nMTF - 1;
+ }
+
+ for (int i = gs; i <= ge; i++) {
+ BsW(len[selector[selCtr]][szptr[i]], code[selector[selCtr]][szptr[i]]);
+ }
+
+ gs = ge + 1;
+ ++selCtr;
+ }
+ if (!(selCtr == nSelectors)) {
+ Panic();
+ }
+ }
+
+ void MoveToFrontCodeAndSend ()
+ {
+ BsPutIntVS(24, origPtr);
+ GenerateMTFValues();
+ SendMTFValues();
+ }
+
+ Stream baseStream;
+
+ void SimpleSort(int lo, int hi, int d)
+ {
+ int i, j, h, bigN, hp;
+ int v;
+
+ bigN = hi - lo + 1;
+ if (bigN < 2) {
+ return;
+ }
+
+ hp = 0;
+ while (incs[hp] < bigN) {
+ hp++;
+ }
+ hp--;
+
+ for (; hp >= 0; hp--) {
+ h = incs[hp];
+
+ i = lo + h;
+ while (true) {
+ /*-- copy 1 --*/
+ if (i > hi)
+ break;
+ v = zptr[i];
+ j = i;
+ while (FullGtU(zptr[j-h]+d, v+d)) {
+ zptr[j] = zptr[j-h];
+ j = j - h;
+ if (j <= (lo + h - 1))
+ break;
+ }
+ zptr[j] = v;
+ i++;
+
+ /*-- copy 2 --*/
+ if (i > hi) {
+ break;
+ }
+ v = zptr[i];
+ j = i;
+ while (FullGtU ( zptr[j-h]+d, v+d )) {
+ zptr[j] = zptr[j-h];
+ j = j - h;
+ if (j <= (lo + h - 1)) {
+ break;
+ }
+ }
+ zptr[j] = v;
+ i++;
+
+ /*-- copy 3 --*/
+ if (i > hi) {
+ break;
+ }
+ v = zptr[i];
+ j = i;
+ while (FullGtU ( zptr[j-h]+d, v+d)) {
+ zptr[j] = zptr[j-h];
+ j = j - h;
+ if (j <= (lo + h - 1)) {
+ break;
+ }
+ }
+ zptr[j] = v;
+ i++;
+
+ if (workDone > workLimit && firstAttempt) {
+ return;
+ }
+ }
+ }
+ }
+
+ void Vswap(int p1, int p2, int n )
+ {
+ int temp = 0;
+ while (n > 0) {
+ temp = zptr[p1];
+ zptr[p1] = zptr[p2];
+ zptr[p2] = temp;
+ p1++;
+ p2++;
+ n--;
+ }
+ }
+
+ byte Med3(byte a, byte b, byte c )
+ {
+ byte t;
+ if (a > b) {
+ t = a;
+ a = b;
+ b = t;
+ }
+ if (b > c) {
+ t = b;
+ b = c;
+ c = t;
+ }
+ if (a > b) {
+ b = a;
+ }
+ return b;
+ }
+
+ class StackElem
+ {
+ public int ll;
+ public int hh;
+ public int dd;
+ }
+
+ void QSort3(int loSt, int hiSt, int dSt)
+ {
+ int unLo, unHi, ltLo, gtHi, med, n, m;
+ int sp, lo, hi, d;
+ StackElem[] stack = new StackElem[QSORT_STACK_SIZE];
+ for (int count = 0; count < QSORT_STACK_SIZE; count++) {
+ stack[count] = new StackElem();
+ }
+
+ sp = 0;
+
+ stack[sp].ll = loSt;
+ stack[sp].hh = hiSt;
+ stack[sp].dd = dSt;
+ sp++;
+
+ while (sp > 0) {
+ if (sp >= QSORT_STACK_SIZE) {
+ Panic();
+ }
+
+ sp--;
+ lo = stack[sp].ll;
+ hi = stack[sp].hh;
+ d = stack[sp].dd;
+
+ if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) {
+ SimpleSort(lo, hi, d);
+ if (workDone > workLimit && firstAttempt) {
+ return;
+ }
+ continue;
+ }
+
+ med = Med3(block[zptr[lo] + d + 1],
+ block[zptr[hi ] + d + 1],
+ block[zptr[(lo + hi) >> 1] + d + 1]);
+
+ unLo = ltLo = lo;
+ unHi = gtHi = hi;
+
+ while (true) {
+ while (true) {
+ if (unLo > unHi) {
+ break;
+ }
+ n = ((int)block[zptr[unLo]+d + 1]) - med;
+ if (n == 0) {
+ int temp = 0;
+ temp = zptr[unLo];
+ zptr[unLo] = zptr[ltLo];
+ zptr[ltLo] = temp;
+ ltLo++;
+ unLo++;
+ continue;
+ }
+ if (n > 0) {
+ break;
+ }
+ unLo++;
+ }
+ while (true) {
+ if (unLo > unHi) {
+ break;
+ }
+ n = ((int)block[zptr[unHi]+d + 1]) - med;
+ if (n == 0) {
+ int temp = 0;
+ temp = zptr[unHi];
+ zptr[unHi] = zptr[gtHi];
+ zptr[gtHi] = temp;
+ gtHi--;
+ unHi--;
+ continue;
+ }
+ if (n < 0) {
+ break;
+ }
+ unHi--;
+ }
+ if (unLo > unHi) {
+ break;
+ }
+ {
+ int temp = zptr[unLo];
+ zptr[unLo] = zptr[unHi];
+ zptr[unHi] = temp;
+ unLo++;
+ unHi--;
+ }
+ }
+
+ if (gtHi < ltLo) {
+ stack[sp].ll = lo;
+ stack[sp].hh = hi;
+ stack[sp].dd = d+1;
+ sp++;
+ continue;
+ }
+
+ n = ((ltLo-lo) < (unLo-ltLo)) ? (ltLo-lo) : (unLo-ltLo);
+ Vswap(lo, unLo-n, n);
+ m = ((hi-gtHi) < (gtHi-unHi)) ? (hi-gtHi) : (gtHi-unHi);
+ Vswap(unLo, hi-m+1, m);
+
+ n = lo + unLo - ltLo - 1;
+ m = hi - (gtHi - unHi) + 1;
+
+ stack[sp].ll = lo;
+ stack[sp].hh = n;
+ stack[sp].dd = d;
+ sp++;
+
+ stack[sp].ll = n + 1;
+ stack[sp].hh = m - 1;
+ stack[sp].dd = d+1;
+ sp++;
+
+ stack[sp].ll = m;
+ stack[sp].hh = hi;
+ stack[sp].dd = d;
+ sp++;
+ }
+ }
+
+ void MainSort()
+ {
+ int i, j, ss, sb;
+ int[] runningOrder = new int[256];
+ int[] copy = new int[256];
+ bool[] bigDone = new bool[256];
+ int c1, c2;
+ int numQSorted;
+
+ /*--
+ In the various block-sized structures, live data runs
+ from 0 to last+NUM_OVERSHOOT_BYTES inclusive. First,
+ set up the overshoot area for block.
+ --*/
+
+ // if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" );
+ for (i = 0; i < BZip2Constants.NUM_OVERSHOOT_BYTES; i++) {
+ block[last + i + 2] = block[(i % (last + 1)) + 1];
+ }
+ for (i = 0; i <= last + BZip2Constants.NUM_OVERSHOOT_BYTES; i++) {
+ quadrant[i] = 0;
+ }
+
+ block[0] = (byte)(block[last + 1]);
+
+ if (last < 4000) {
+ /*--
+ Use simpleSort(), since the full sorting mechanism
+ has quite a large constant overhead.
+ --*/
+ for (i = 0; i <= last; i++) {
+ zptr[i] = i;
+ }
+ firstAttempt = false;
+ workDone = workLimit = 0;
+ SimpleSort(0, last, 0);
+ } else {
+ numQSorted = 0;
+ for (i = 0; i <= 255; i++) {
+ bigDone[i] = false;
+ }
+ for (i = 0; i <= 65536; i++) {
+ ftab[i] = 0;
+ }
+
+ c1 = block[0];
+ for (i = 0; i <= last; i++) {
+ c2 = block[i + 1];
+ ftab[(c1 << 8) + c2]++;
+ c1 = c2;
+ }
+
+ for (i = 1; i <= 65536; i++) {
+ ftab[i] += ftab[i - 1];
+ }
+
+ c1 = block[1];
+ for (i = 0; i < last; i++) {
+ c2 = block[i + 2];
+ j = (c1 << 8) + c2;
+ c1 = c2;
+ ftab[j]--;
+ zptr[ftab[j]] = i;
+ }
+
+ j = ((block[last + 1]) << 8) + (block[1]);
+ ftab[j]--;
+ zptr[ftab[j]] = last;
+
+ /*--
+ Now ftab contains the first loc of every small bucket.
+ Calculate the running order, from smallest to largest
+ big bucket.
+ --*/
+
+ for (i = 0; i <= 255; i++) {
+ runningOrder[i] = i;
+ }
+
+ int vv;
+ int h = 1;
+ do {
+ h = 3 * h + 1;
+ } while (h <= 256);
+ do {
+ h = h / 3;
+ for (i = h; i <= 255; i++) {
+ vv = runningOrder[i];
+ j = i;
+ while ((ftab[((runningOrder[j-h])+1) << 8] - ftab[(runningOrder[j-h]) << 8]) > (ftab[((vv)+1) << 8] - ftab[(vv) << 8])) {
+ runningOrder[j] = runningOrder[j-h];
+ j = j - h;
+ if (j <= (h - 1)) {
+ break;
+ }
+ }
+ runningOrder[j] = vv;
+ }
+ } while (h != 1);
+
+ /*--
+ The main sorting loop.
+ --*/
+ for (i = 0; i <= 255; i++) {
+
+ /*--
+ Process big buckets, starting with the least full.
+ --*/
+ ss = runningOrder[i];
+
+ /*--
+ Complete the big bucket [ss] by quicksorting
+ any unsorted small buckets [ss, j]. Hopefully
+ previous pointer-scanning phases have already
+ completed many of the small buckets [ss, j], so
+ we don't have to sort them at all.
+ --*/
+ for (j = 0; j <= 255; j++) {
+ sb = (ss << 8) + j;
+ if(!((ftab[sb] & SETMASK) == SETMASK)) {
+ int lo = ftab[sb] & CLEARMASK;
+ int hi = (ftab[sb+1] & CLEARMASK) - 1;
+ if (hi > lo) {
+ QSort3(lo, hi, 2);
+ numQSorted += (hi - lo + 1);
+ if (workDone > workLimit && firstAttempt) {
+ return;
+ }
+ }
+ ftab[sb] |= SETMASK;
+ }
+ }
+
+ /*--
+ The ss big bucket is now done. Record this fact,
+ and update the quadrant descriptors. Remember to
+ update quadrants in the overshoot area too, if
+ necessary. The "if (i < 255)" test merely skips
+ this updating for the last bucket processed, since
+ updating for the last bucket is pointless.
+ --*/
+ bigDone[ss] = true;
+
+ if (i < 255) {
+ int bbStart = ftab[ss << 8] & CLEARMASK;
+ int bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
+ int shifts = 0;
+
+ while ((bbSize >> shifts) > 65534) {
+ shifts++;
+ }
+
+ for (j = 0; j < bbSize; j++) {
+ int a2update = zptr[bbStart + j];
+ int qVal = (j >> shifts);
+ quadrant[a2update] = qVal;
+ if (a2update < BZip2Constants.NUM_OVERSHOOT_BYTES) {
+ quadrant[a2update + last + 1] = qVal;
+ }
+ }
+
+ if (!(((bbSize-1) >> shifts) <= 65535)) {
+ Panic();
+ }
+ }
+
+ /*--
+ Now scan this big bucket so as to synthesise the
+ sorted order for small buckets [t, ss] for all t != ss.
+ --*/
+ for (j = 0; j <= 255; j++) {
+ copy[j] = ftab[(j << 8) + ss] & CLEARMASK;
+ }
+
+ for (j = ftab[ss << 8] & CLEARMASK; j < (ftab[(ss+1) << 8] & CLEARMASK); j++) {
+ c1 = block[zptr[j]];
+ if (!bigDone[c1]) {
+ zptr[copy[c1]] = zptr[j] == 0 ? last : zptr[j] - 1;
+ copy[c1] ++;
+ }
+ }
+
+ for (j = 0; j <= 255; j++) {
+ ftab[(j << 8) + ss] |= SETMASK;
+ }
+ }
+ }
+ }
+
+ void RandomiseBlock()
+ {
+ int i;
+ int rNToGo = 0;
+ int rTPos = 0;
+ for (i = 0; i < 256; i++) {
+ inUse[i] = false;
+ }
+
+ for (i = 0; i <= last; i++) {
+ if (rNToGo == 0) {
+ rNToGo = (int)BZip2Constants.rNums[rTPos];
+ rTPos++;
+ if (rTPos == 512) {
+ rTPos = 0;
+ }
+ }
+ rNToGo--;
+ block[i + 1] ^= (byte)((rNToGo == 1) ? 1 : 0);
+ // handle 16 bit signed numbers
+ block[i + 1] &= 0xFF;
+
+ inUse[block[i + 1]] = true;
+ }
+ }
+
+ void DoReversibleTransformation()
+ {
+ workLimit = workFactor * last;
+ workDone = 0;
+ blockRandomised = false;
+ firstAttempt = true;
+
+ MainSort();
+
+ if (workDone > workLimit && firstAttempt) {
+ RandomiseBlock();
+ workLimit = workDone = 0;
+ blockRandomised = true;
+ firstAttempt = false;
+ MainSort();
+ }
+
+ origPtr = -1;
+ for (int i = 0; i <= last; i++) {
+ if (zptr[i] == 0) {
+ origPtr = i;
+ break;
+ }
+ }
+
+ if (origPtr == -1) {
+ Panic();
+ }
+ }
+
+ bool FullGtU(int i1, int i2)
+ {
+ int k;
+ byte c1, c2;
+ int s1, s2;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ i1++;
+ i2++;
+
+ k = last + 1;
+
+ do {
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ s1 = quadrant[i1];
+ s2 = quadrant[i2];
+ if (s1 != s2) {
+ return s1 > s2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ s1 = quadrant[i1];
+ s2 = quadrant[i2];
+ if (s1 != s2) {
+ return s1 > s2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ s1 = quadrant[i1];
+ s2 = quadrant[i2];
+ if (s1 != s2) {
+ return s1 > s2;
+ }
+ i1++;
+ i2++;
+
+ c1 = block[i1 + 1];
+ c2 = block[i2 + 1];
+ if (c1 != c2) {
+ return c1 > c2;
+ }
+ s1 = quadrant[i1];
+ s2 = quadrant[i2];
+ if (s1 != s2) {
+ return s1 > s2;
+ }
+ i1++;
+ i2++;
+
+ if (i1 > last) {
+ i1 -= last;
+ i1--;
+ }
+ if (i2 > last) {
+ i2 -= last;
+ i2--;
+ }
+
+ k -= 4;
+ ++workDone;
+ } while (k >= 0);
+
+ return false;
+ }
+
+ /*--
+ Knuth's increments seem to work better
+ than Incerpi-Sedgewick here. Possibly
+ because the number of elems to sort is
+ usually small, typically <= 20.
+ --*/
+ readonly int[] incs = new int[] {
+ 1, 4, 13, 40, 121, 364, 1093, 3280,
+ 9841, 29524, 88573, 265720,
+ 797161, 2391484
+ };
+
+ void AllocateCompressStructures()
+ {
+ int n = BZip2Constants.baseBlockSize * blockSize100k;
+ block = new byte[(n + 1 + BZip2Constants.NUM_OVERSHOOT_BYTES)];
+ quadrant = new int[(n + BZip2Constants.NUM_OVERSHOOT_BYTES)];
+ zptr = new int[n];
+ ftab = new int[65537];
+
+ if (block == null || quadrant == null || zptr == null || ftab == null) {
+ // int totalDraw = (n + 1 + NUM_OVERSHOOT_BYTES) + (n + NUM_OVERSHOOT_BYTES) + n + 65537;
+ // compressOutOfMemory ( totalDraw, n );
+ }
+
+ /*
+ The back end needs a place to store the MTF values
+ whilst it calculates the coding tables. We could
+ put them in the zptr array. However, these values
+ will fit in a short, so we overlay szptr at the
+ start of zptr, in the hope of reducing the number
+ of cache misses induced by the multiple traversals
+ of the MTF values when calculating coding tables.
+ Seems to improve compression speed by about 1%.
+ */
+ // szptr = zptr;
+
+
+ szptr = new short[2 * n];
+ }
+
+ void GenerateMTFValues()
+ {
+ char[] yy = new char[256];
+ int i, j;
+ char tmp;
+ char tmp2;
+ int zPend;
+ int wr;
+ int EOB;
+
+ MakeMaps();
+ EOB = nInUse+1;
+
+ for (i = 0; i <= EOB; i++) {
+ mtfFreq[i] = 0;
+ }
+
+ wr = 0;
+ zPend = 0;
+ for (i = 0; i < nInUse; i++) {
+ yy[i] = (char) i;
+ }
+
+
+ for (i = 0; i <= last; i++) {
+ char ll_i;
+
+ ll_i = unseqToSeq[block[zptr[i]]];
+
+ j = 0;
+ tmp = yy[j];
+ while (ll_i != tmp) {
+ j++;
+ tmp2 = tmp;
+ tmp = yy[j];
+ yy[j] = tmp2;
+ }
+ yy[0] = tmp;
+
+ if (j == 0) {
+ zPend++;
+ } else {
+ if (zPend > 0) {
+ zPend--;
+ while (true) {
+ switch (zPend % 2) {
+ case 0:
+ szptr[wr] = (short)BZip2Constants.RUNA;
+ wr++;
+ mtfFreq[BZip2Constants.RUNA]++;
+ break;
+ case 1:
+ szptr[wr] = (short)BZip2Constants.RUNB;
+ wr++;
+ mtfFreq[BZip2Constants.RUNB]++;
+ break;
+ }
+ if (zPend < 2) {
+ break;
+ }
+ zPend = (zPend - 2) / 2;
+ }
+ zPend = 0;
+ }
+ szptr[wr] = (short)(j + 1);
+ wr++;
+ mtfFreq[j + 1]++;
+ }
+ }
+
+ if (zPend > 0) {
+ zPend--;
+ while (true) {
+ switch (zPend % 2) {
+ case 0:
+ szptr[wr] = (short)BZip2Constants.RUNA;
+ wr++;
+ mtfFreq[BZip2Constants.RUNA]++;
+ break;
+ case 1:
+ szptr[wr] = (short)BZip2Constants.RUNB;
+ wr++;
+ mtfFreq[BZip2Constants.RUNB]++;
+ break;
+ }
+ if (zPend < 2) {
+ break;
+ }
+ zPend = (zPend - 2) / 2;
+ }
+ }
+
+ szptr[wr] = (short)EOB;
+ wr++;
+ mtfFreq[EOB]++;
+
+ nMTF = wr;
+ }
+ }
+}
+
+/* This file was derived from a file containing under this license:
+ *
+ * This file is a part of bzip2 and/or libbzip2, a program and
+ * library for lossless, block-sorting data compression.
+ *
+ * Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. The origin of this software must not be misrepresented; you must
+ * not claim that you wrote the original software. If you use this
+ * software in a product, an acknowledgment in the product
+ * documentation would be appreciated but is not required.
+ *
+ * 3. Altered source versions must be plainly marked as such, and must
+ * not be misrepresented as being the original software.
+ *
+ * 4. The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Java version ported by Keiron Liddle, Aftex Software <keiron@aftexsw.com> 1999-2001
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/Adler32.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/Adler32.cs
new file mode 100644
index 00000000000..806ae4a511f
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/Adler32.cs
@@ -0,0 +1,200 @@
+// Adler32.cs - Computes Adler32 data checksum of a data stream
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Checksums
+{
+
+ /// <summary>
+ /// Computes Adler32 checksum for a stream of data. An Adler32
+ /// checksum is not as reliable as a CRC32 checksum, but a lot faster to
+ /// compute.
+ ///
+ /// The specification for Adler32 may be found in RFC 1950.
+ /// ZLIB Compressed Data Format Specification version 3.3)
+ ///
+ ///
+ /// From that document:
+ ///
+ /// "ADLER32 (Adler-32 checksum)
+ /// This contains a checksum value of the uncompressed data
+ /// (excluding any dictionary data) computed according to Adler-32
+ /// algorithm. This algorithm is a 32-bit extension and improvement
+ /// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
+ /// standard.
+ ///
+ /// Adler-32 is composed of two sums accumulated per byte: s1 is
+ /// the sum of all bytes, s2 is the sum of all s1 values. Both sums
+ /// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
+ /// Adler-32 checksum is stored as s2*65536 + s1 in most-
+ /// significant-byte first (network) order."
+ ///
+ /// "8.2. The Adler-32 algorithm
+ ///
+ /// The Adler-32 algorithm is much faster than the CRC32 algorithm yet
+ /// still provides an extremely low probability of undetected errors.
+ ///
+ /// The modulo on unsigned long accumulators can be delayed for 5552
+ /// bytes, so the modulo operation time is negligible. If the bytes
+ /// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
+ /// and order sensitive, unlike the first sum, which is just a
+ /// checksum. That 65521 is prime is important to avoid a possible
+ /// large class of two-byte errors that leave the check unchanged.
+ /// (The Fletcher checksum uses 255, which is not prime and which also
+ /// makes the Fletcher check insensitive to single byte changes 0 -
+ /// 255.)
+ ///
+ /// The sum s1 is initialized to 1 instead of zero to make the length
+ /// of the sequence part of s2, so that the length does not have to be
+ /// checked separately. (Any sequence of zeroes has a Fletcher
+ /// checksum of zero.)"
+ /// </summary>
+ /// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream"/>
+ /// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream"/>
+ public sealed class Adler32 : IChecksum
+ {
+ /// <summary>
+ /// largest prime smaller than 65536
+ /// </summary>
+ readonly static uint BASE = 65521;
+
+ uint checksum;
+
+ /// <summary>
+ /// Returns the Adler32 data checksum computed so far.
+ /// </summary>
+ public long Value {
+ get {
+ return checksum;
+ }
+ }
+
+ /// <summary>
+ /// Creates a new instance of the Adler32 class.
+ /// The checksum starts off with a value of 1.
+ /// </summary>
+ public Adler32()
+ {
+ Reset();
+ }
+
+ /// <summary>
+ /// Resets the Adler32 checksum to the initial value.
+ /// </summary>
+ public void Reset()
+ {
+ checksum = 1;
+ }
+
+ /// <summary>
+ /// Updates the checksum with the byte b.
+ /// </summary>
+ /// <param name="bval">
+ /// The data value to add. The high byte of the int is ignored.
+ /// </param>
+ public void Update(int bval)
+ {
+ // We could make a length 1 byte array and call update again, but I
+ // would rather not have that overhead
+ uint s1 = checksum & 0xFFFF;
+ uint s2 = checksum >> 16;
+
+ s1 = (s1 + ((uint)bval & 0xFF)) % BASE;
+ s2 = (s1 + s2) % BASE;
+
+ checksum = (s2 << 16) + s1;
+ }
+
+ /// <summary>
+ /// Updates the checksum with an array of bytes.
+ /// </summary>
+ /// <param name="buffer">
+ /// The source of the data to update with.
+ /// </param>
+ public void Update(byte[] buffer)
+ {
+ Update(buffer, 0, buffer.Length);
+ }
+
+ /// <summary>
+ /// Updates the checksum with the bytes taken from the array.
+ /// </summary>
+ /// <param name="buf">
+ /// an array of bytes
+ /// </param>
+ /// <param name="off">
+ /// the start of the data used for this update
+ /// </param>
+ /// <param name="len">
+ /// the number of bytes to use for this update
+ /// </param>
+ public void Update(byte[] buf, int off, int len)
+ {
+ if (buf == null) {
+ throw new ArgumentNullException("buf");
+ }
+
+ if (off < 0 || len < 0 || off + len > buf.Length) {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ //(By Per Bothner)
+ uint s1 = checksum & 0xFFFF;
+ uint s2 = checksum >> 16;
+
+ while (len > 0) {
+ // We can defer the modulo operation:
+ // s1 maximally grows from 65521 to 65521 + 255 * 3800
+ // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
+ int n = 3800;
+ if (n > len) {
+ n = len;
+ }
+ len -= n;
+ while (--n >= 0) {
+ s1 = s1 + (uint)(buf[off++] & 0xFF);
+ s2 = s2 + s1;
+ }
+ s1 %= BASE;
+ s2 %= BASE;
+ }
+
+ checksum = (s2 << 16) | s1;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/CRC32.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/CRC32.cs
new file mode 100644
index 00000000000..a78ad4c29f6
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/CRC32.cs
@@ -0,0 +1,211 @@
+// CRC32.cs - Computes CRC32 data checksum of a data stream
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Checksums
+{
+
+ /// <summary>
+ /// Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
+ /// x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
+ ///
+ /// Polynomials over GF(2) are represented in binary, one bit per coefficient,
+ /// with the lowest powers in the most significant bit. Then adding polynomials
+ /// is just exclusive-or, and multiplying a polynomial by x is a right shift by
+ /// one. If we call the above polynomial p, and represent a byte as the
+ /// polynomial q, also with the lowest power in the most significant bit (so the
+ /// byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
+ /// where a mod b means the remainder after dividing a by b.
+ ///
+ /// This calculation is done using the shift-register method of multiplying and
+ /// taking the remainder. The register is initialized to zero, and for each
+ /// incoming bit, x^32 is added mod p to the register if the bit is a one (where
+ /// x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
+ /// x (which is shifting right by one and adding x^32 mod p if the bit shifted
+ /// out is a one). We start with the highest power (least significant bit) of
+ /// q and repeat for all eight bits of q.
+ ///
+ /// The table is simply the CRC of all possible eight bit values. This is all
+ /// the information needed to generate CRC's on data a byte at a time for all
+ /// combinations of CRC register values and incoming bytes.
+ /// </summary>
+ public sealed class Crc32 : IChecksum
+ {
+ readonly static uint CrcSeed = 0xFFFFFFFF;
+
+ readonly static uint[] CrcTable = new uint[] {
+ 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419,
+ 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4,
+ 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
+ 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
+ 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856,
+ 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
+ 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
+ 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
+ 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
+ 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A,
+ 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599,
+ 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
+ 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190,
+ 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
+ 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E,
+ 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
+ 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED,
+ 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
+ 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3,
+ 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
+ 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
+ 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
+ 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010,
+ 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
+ 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17,
+ 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6,
+ 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615,
+ 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
+ 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344,
+ 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
+ 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A,
+ 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
+ 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1,
+ 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C,
+ 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
+ 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
+ 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE,
+ 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
+ 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C,
+ 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
+ 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B,
+ 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
+ 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1,
+ 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
+ 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278,
+ 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
+ 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66,
+ 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
+ 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
+ 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8,
+ 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B,
+ 0x2D02EF8D
+ };
+
+ internal static uint ComputeCrc32(uint oldCrc, byte bval)
+ {
+ return (uint)(Crc32.CrcTable[(oldCrc ^ bval) & 0xFF] ^ (oldCrc >> 8));
+ }
+
+ /// <summary>
+ /// The crc data checksum so far.
+ /// </summary>
+ uint crc = 0;
+
+ /// <summary>
+ /// Returns the CRC32 data checksum computed so far.
+ /// </summary>
+ public long Value {
+ get {
+ return (long)crc;
+ }
+ set {
+ crc = (uint)value;
+ }
+ }
+
+ /// <summary>
+ /// Resets the CRC32 data checksum as if no update was ever called.
+ /// </summary>
+ public void Reset()
+ {
+ crc = 0;
+ }
+
+ /// <summary>
+ /// Updates the checksum with the int bval.
+ /// </summary>
+ /// <param name = "bval">
+ /// the byte is taken as the lower 8 bits of bval
+ /// </param>
+ public void Update(int bval)
+ {
+ crc ^= CrcSeed;
+ crc = CrcTable[(crc ^ bval) & 0xFF] ^ (crc >> 8);
+ crc ^= CrcSeed;
+ }
+
+ /// <summary>
+ /// Updates the checksum with the bytes taken from the array.
+ /// </summary>
+ /// <param name="buffer">
+ /// buffer an array of bytes
+ /// </param>
+ public void Update(byte[] buffer)
+ {
+ Update(buffer, 0, buffer.Length);
+ }
+
+ /// <summary>
+ /// Adds the byte array to the data checksum.
+ /// </summary>
+ /// <param name = "buf">
+ /// the buffer which contains the data
+ /// </param>
+ /// <param name = "off">
+ /// the offset in the buffer where the data starts
+ /// </param>
+ /// <param name = "len">
+ /// the length of the data
+ /// </param>
+ public void Update(byte[] buf, int off, int len)
+ {
+ if (buf == null) {
+ throw new ArgumentNullException("buf");
+ }
+
+ if (off < 0 || len < 0 || off + len > buf.Length) {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ crc ^= CrcSeed;
+
+ while (--len >= 0) {
+ crc = CrcTable[(crc ^ buf[off++]) & 0xFF] ^ (crc >> 8);
+ }
+
+ crc ^= CrcSeed;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/IChecksum.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/IChecksum.cs
new file mode 100644
index 00000000000..98385b316ac
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/IChecksum.cs
@@ -0,0 +1,93 @@
+// IChecksum.cs - Interface to compute a data checksum
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.Checksums
+{
+
+ /// <summary>
+ /// Interface to compute a data checksum used by checked input/output streams.
+ /// A data checksum can be updated by one byte or with a byte array. After each
+ /// update the value of the current checksum can be returned by calling
+ /// <code>getValue</code>. The complete checksum object can also be reset
+ /// so it can be used again with new data.
+ /// </summary>
+ public interface IChecksum
+ {
+ /// <summary>
+ /// Returns the data checksum computed so far.
+ /// </summary>
+ long Value
+ {
+ get;
+ }
+
+ /// <summary>
+ /// Resets the data checksum as if no update was ever called.
+ /// </summary>
+ void Reset();
+
+ /// <summary>
+ /// Adds one byte to the data checksum.
+ /// </summary>
+ /// <param name = "bval">
+ /// the data value to add. The high byte of the int is ignored.
+ /// </param>
+ void Update(int bval);
+
+ /// <summary>
+ /// Updates the data checksum with the bytes taken from the array.
+ /// </summary>
+ /// <param name="buffer">
+ /// buffer an array of bytes
+ /// </param>
+ void Update(byte[] buffer);
+
+ /// <summary>
+ /// Adds the byte array to the data checksum.
+ /// </summary>
+ /// <param name = "buf">
+ /// the buffer which contains the data
+ /// </param>
+ /// <param name = "off">
+ /// the offset in the buffer where the data starts
+ /// </param>
+ /// <param name = "len">
+ /// the length of the data
+ /// </param>
+ void Update(byte[] buf, int off, int len);
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/StrangeCRC.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/StrangeCRC.cs
new file mode 100644
index 00000000000..13f43233cf5
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Checksums/StrangeCRC.cs
@@ -0,0 +1,184 @@
+// StrangeCRC.cs - computes a crc used in the bziplib ... I don't think that
+// this is the 'standard' crc, please correct me, if I'm wrong
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Checksums
+{
+ /// <summary>
+ /// Bzip2 checksum algorithm
+ /// </summary>
+ public class StrangeCRC : IChecksum
+ {
+ readonly static uint[] crc32Table = {
+ 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
+ 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
+ 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
+ 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
+ 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
+ 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
+ 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
+ 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
+ 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
+ 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
+ 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
+ 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
+ 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
+ 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
+ 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
+ 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
+ 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
+ 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
+ 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
+ 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
+ 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
+ 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
+ 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
+ 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
+ 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
+ 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
+ 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
+ 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
+ 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
+ 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
+ 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
+ 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
+ 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
+ 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
+ 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
+ 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
+ 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
+ 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
+ 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
+ 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
+ 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
+ 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
+ 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
+ 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
+ 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
+ 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
+ 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
+ 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
+ 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
+ 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
+ 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
+ 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
+ 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
+ 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
+ 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
+ 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
+ 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
+ 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
+ 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
+ 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
+ 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
+ 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
+ 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
+ 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
+ };
+
+ int globalCrc;
+
+ /// <summary>
+ /// Initialise a default instance of <see cref="StrangeCRC"></see>
+ /// </summary>
+ public StrangeCRC()
+ {
+ Reset();
+ }
+
+ /// <summary>
+ /// Reset the state of Crc.
+ /// </summary>
+ public void Reset()
+ {
+ globalCrc = -1;
+ }
+
+ /// <summary>
+ /// Get the current Crc value.
+ /// </summary>
+ public long Value {
+ get {
+ return ~globalCrc;
+ }
+ }
+
+ /// <summary>
+ /// Update the Crc value.
+ /// </summary>
+ /// <param name="inCh">data update is based on</param>
+ public void Update(int inCh)
+ {
+ int temp = (globalCrc >> 24) ^ inCh;
+ if (temp < 0) {
+ temp = 256 + temp;
+ }
+ globalCrc = (int)((globalCrc << 8) ^ crc32Table[temp]);
+ }
+
+ /// <summary>
+ /// Update Crc based on a block of data
+ /// </summary>
+ public void Update(byte[] buf)
+ {
+ Update(buf, 0, buf.Length);
+ }
+
+ /// <summary>
+ /// Update Crc based on a portion of a block of data
+ /// </summary>
+ /// <param name="buf">block of data</param>
+ /// <param name="off">index of first byte to use</param>
+ /// <param name="len">number of bytes to use</param>
+ public void Update(byte[] buf, int off, int len)
+ {
+ if (buf == null) {
+ throw new ArgumentNullException("buf");
+ }
+
+ if (off < 0 || len < 0 || off + len > buf.Length) {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ for (int i = 0; i < len; ++i) {
+ Update(buf[off++]);
+ }
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs
new file mode 100644
index 00000000000..82d720a6bae
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs
@@ -0,0 +1,380 @@
+// FileSystemScanner.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+using System;
+using System.IO;
+
+namespace ICSharpCode.SharpZipLib.Core
+{
+ /// <summary>
+ /// Event arguments for scanning.
+ /// </summary>
+ public class ScanEventArgs : EventArgs
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="ScanEventArgs"/>
+ /// </summary>
+ /// <param name="name"></param>
+ public ScanEventArgs(string name)
+ {
+ this.name = name;
+ ContinueRunning = true;
+ }
+
+ string name;
+
+ /// <summary>
+ /// The name for this event.
+ /// </summary>
+ public string Name
+ {
+ get { return name; }
+ }
+
+ bool continueRunning;
+
+ /// <summary>
+ /// Get set a value indicating if scanning should continue or not.
+ /// </summary>
+ public bool ContinueRunning
+ {
+ get { return continueRunning; }
+ set { continueRunning = value; }
+ }
+ }
+
+ /// <summary>
+ /// Event arguments for directories.
+ /// </summary>
+ public class DirectoryEventArgs : ScanEventArgs
+ {
+ /// <summary>
+ /// Initialize an instance of <see cref="DirectoryEventArgs"></see>.
+ /// </summary>
+ /// <param name="name">The name for this directory.</param>
+ /// <param name="hasMatchingFiles">Flag value indicating if any matching files are contained in this directory.</param>
+ public DirectoryEventArgs(string name, bool hasMatchingFiles)
+ : base (name)
+ {
+ this.hasMatchingFiles = hasMatchingFiles;
+ }
+
+ /// <summary>
+ /// Get a value indicating if the directory contains any matching files or not.
+ /// </summary>
+ public bool HasMatchingFiles
+ {
+ get { return hasMatchingFiles; }
+ }
+
+ bool hasMatchingFiles;
+ }
+
+ /// <summary>
+ /// Arguments passed when scan failures are detected.
+ /// </summary>
+ public class ScanFailureEventArgs
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="ScanFailureEventArgs"></see>
+ /// </summary>
+ /// <param name="name">The name to apply.</param>
+ /// <param name="e">The exception to use.</param>
+ public ScanFailureEventArgs(string name, Exception e)
+ {
+ this.name = name;
+ this.exception = e;
+ continueRunning = true;
+ }
+
+ string name;
+
+ /// <summary>
+ /// The applicable name.
+ /// </summary>
+ public string Name
+ {
+ get { return name; }
+ }
+
+ Exception exception;
+
+ /// <summary>
+ /// The applicable exception.
+ /// </summary>
+ public Exception Exception
+ {
+ get { return exception; }
+ }
+
+ bool continueRunning;
+
+ /// <summary>
+ /// Get / set a value indicating wether scanning should continue.
+ /// </summary>
+ public bool ContinueRunning
+ {
+ get { return continueRunning; }
+ set { continueRunning = value; }
+ }
+ }
+
+ /// <summary>
+ /// Delegate invokked when a directory is processed.
+ /// </summary>
+ public delegate void ProcessDirectoryDelegate(object Sender, DirectoryEventArgs e);
+
+ /// <summary>
+ /// Delegate invoked when a file is processed.
+ /// </summary>
+ public delegate void ProcessFileDelegate(object sender, ScanEventArgs e);
+
+ /// <summary>
+ /// Delegate invoked when a directory failure is detected.
+ /// </summary>
+ public delegate void DirectoryFailureDelegate(object sender, ScanFailureEventArgs e);
+
+ /// <summary>
+ /// Delegate invoked when a file failure is detected.
+ /// </summary>
+ public delegate void FileFailureDelegate(object sender, ScanFailureEventArgs e);
+
+ /// <summary>
+ /// FileSystemScanner provides facilities scanning of files and directories.
+ /// </summary>
+ public class FileSystemScanner
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="FileSystemScanner"></see>
+ /// </summary>
+ /// <param name="filter">The file filter to apply when scanning.</param>
+ public FileSystemScanner(string filter)
+ {
+ fileFilter = new PathFilter(filter);
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="FileSystemScanner"></see>
+ /// </summary>
+ /// <param name="fileFilter">The file <see cref="NameFilter"></see>filter to apply.</param>
+ /// <param name="directoryFilter">The directory <see cref="NameFilter"></see>filter to apply.</param>
+ public FileSystemScanner(string fileFilter, string directoryFilter)
+ {
+ this.fileFilter = new PathFilter(fileFilter);
+ this.directoryFilter = new PathFilter(directoryFilter);
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="FileSystemScanner"></see>
+ /// </summary>
+ /// <param name="fileFilter">The file <see cref="NameFilter"></see>filter to apply.</param>
+ public FileSystemScanner(IScanFilter fileFilter)
+ {
+ this.fileFilter = fileFilter;
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="FileSystemScanner"></see>
+ /// </summary>
+ /// <param name="fileFilter">The file <see cref="IScanFilter"></see>filter to apply.</param>
+ /// <param name="directoryFilter">The directory <see cref="IScanFilter"></see>filter to apply.</param>
+ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
+ {
+ this.fileFilter = fileFilter;
+ this.directoryFilter = directoryFilter;
+ }
+
+ /// <summary>
+ /// Delegate to invoke when a directory is processed.
+ /// </summary>
+ public ProcessDirectoryDelegate ProcessDirectory;
+
+ /// <summary>
+ /// Delegate to invoke when a file is processed.
+ /// </summary>
+ public ProcessFileDelegate ProcessFile;
+
+ /// <summary>
+ /// Delegate to invoke when a directory failure is detected.
+ /// </summary>
+ public DirectoryFailureDelegate DirectoryFailure;
+
+ /// <summary>
+ /// Delegate to invoke when a file failure is detected.
+ /// </summary>
+ public FileFailureDelegate FileFailure;
+
+ /// <summary>
+ /// Raise the DirectoryFailure event.
+ /// </summary>
+ /// <param name="directory">Rhe directory name.</param>
+ /// <param name="e">The exception detected.</param>
+ public void OnDirectoryFailure(string directory, Exception e)
+ {
+ if ( DirectoryFailure == null ) {
+ alive = false;
+ } else {
+ ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
+ DirectoryFailure(this, args);
+ alive = args.ContinueRunning;
+ }
+ }
+
+ /// <summary>
+ /// Raise the FileFailure event.
+ /// </summary>
+ /// <param name="file">The file name.</param>
+ /// <param name="e">The exception detected.</param>
+ public void OnFileFailure(string file, Exception e)
+ {
+ if ( FileFailure == null ) {
+ alive = false;
+ } else {
+ ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
+ FileFailure(this, args);
+ alive = args.ContinueRunning;
+ }
+ }
+
+ /// <summary>
+ /// Raise the ProcessFile event.
+ /// </summary>
+ /// <param name="file">The file name.</param>
+ public void OnProcessFile(string file)
+ {
+ if ( ProcessFile != null ) {
+ ScanEventArgs args = new ScanEventArgs(file);
+ ProcessFile(this, args);
+ alive = args.ContinueRunning;
+ }
+ }
+
+ /// <summary>
+ /// Raise the ProcessDirectory event.
+ /// </summary>
+ /// <param name="directory">The directory name.</param>
+ /// <param name="hasMatchingFiles">Flag indicating if the directory has matching files.</param>
+ public void OnProcessDirectory(string directory, bool hasMatchingFiles)
+ {
+ if ( ProcessDirectory != null ) {
+ DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles);
+ ProcessDirectory(this, args);
+ alive = args.ContinueRunning;
+ }
+ }
+
+ /// <summary>
+ /// Scan a directory.
+ /// </summary>
+ /// <param name="directory">The base directory to scan.</param>
+ /// <param name="recurse">True to recurse subdirectories, false to do a single directory.</param>
+ public void Scan(string directory, bool recurse)
+ {
+ alive = true;
+ ScanDir(directory, recurse);
+ }
+
+ void ScanDir(string directory, bool recurse)
+ {
+
+ try {
+ string[] names = System.IO.Directory.GetFiles(directory);
+ bool hasMatch = false;
+ for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex) {
+ if ( !fileFilter.IsMatch(names[fileIndex]) ) {
+ names[fileIndex] = null;
+ } else {
+ hasMatch = true;
+ }
+ }
+
+ OnProcessDirectory(directory, hasMatch);
+
+ if ( alive && hasMatch ) {
+ foreach (string fileName in names) {
+ try {
+ if ( fileName != null ) {
+ OnProcessFile(fileName);
+ if ( !alive ) {
+ break;
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ OnFileFailure(fileName, e);
+ }
+ }
+ }
+ }
+ catch (Exception e) {
+ OnDirectoryFailure(directory, e);
+ }
+
+ if ( alive && recurse ) {
+ try {
+ string[] names = System.IO.Directory.GetDirectories(directory);
+ foreach (string fulldir in names) {
+ if ((directoryFilter == null) || (directoryFilter.IsMatch(fulldir))) {
+ ScanDir(fulldir, true);
+ if ( !alive ) {
+ break;
+ }
+ }
+ }
+ }
+ catch (Exception e) {
+ OnDirectoryFailure(directory, e);
+ }
+ }
+ }
+
+ #region Instance Fields
+ /// <summary>
+ /// The file filter currently in use.
+ /// </summary>
+ IScanFilter fileFilter;
+ /// <summary>
+ /// The directory filter currently in use.
+ /// </summary>
+ IScanFilter directoryFilter;
+ /// <summary>
+ /// Falg indicating if scanning is still alive. Used to cancel a scan.
+ /// </summary>
+ bool alive;
+ #endregion
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/INameTransform.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/INameTransform.cs
new file mode 100644
index 00000000000..ded89f7215e
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/INameTransform.cs
@@ -0,0 +1,57 @@
+// INameTransform.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.Core
+{
+ /// <summary>
+ /// INameTransform defines how file system names are transformed for use with archives.
+ /// </summary>
+ public interface INameTransform
+ {
+ /// <summary>
+ /// Given a file name determine the transformed equivalent.
+ /// </summary>
+ /// <param name="name">The name to transform.</param>
+ /// <returns>The transformed name.</returns>
+ string TransformFile(string name);
+
+ /// <summary>
+ /// Given a directory name determine the transformed equivalent.
+ /// </summary>
+ /// <param name="name">The name to transform.</param>
+ /// <returns>The transformed directory name</returns>
+ string TransformDirectory(string name);
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/NameFilter.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/NameFilter.cs
new file mode 100644
index 00000000000..41bef5df28a
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/NameFilter.cs
@@ -0,0 +1,213 @@
+// NameFilter.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+using System;
+using System.Collections;
+using System.Text.RegularExpressions;
+
+namespace ICSharpCode.SharpZipLib.Core
+{
+ /// <summary>
+ /// NameFilter is a string matching class which allows for both positive and negative
+ /// matching.
+ /// A filter is a sequence of independant <see cref="Regex"></see> regular expressions separated by semi-colons ';'
+ /// Each expression can be prefixed by a plus '+' sign or a minus '-' sign to denote the expression
+ /// is intended to include or exclude names. If neither a plus or minus sign is found include is the default
+ /// A given name is tested for inclusion before checking exclusions. Only names matching an include spec
+ /// and not matching an exclude spec are deemed to match the filter.
+ /// An empty filter matches any name.
+ /// </summary>
+ public class NameFilter
+ {
+ /// <summary>
+ /// Construct an instance based on the filter expression passed
+ /// </summary>
+ /// <param name="filter">The filter expression.</param>
+ public NameFilter(string filter)
+ {
+ this.filter = filter;
+ inclusions = new ArrayList();
+ exclusions = new ArrayList();
+ Compile();
+ }
+
+ /// <summary>
+ /// Test a string to see if it is a valid regular expression.
+ /// </summary>
+ /// <param name="e">The expression to test.</param>
+ /// <returns>True if expression is a valid <see cref="System.Text.RegularExpressions.Regex"/> false otherwise.</returns>
+ public static bool IsValidExpression(string e)
+ {
+ bool result = true;
+ try {
+ Regex exp = new Regex(e, RegexOptions.IgnoreCase | RegexOptions.Singleline);
+ }
+ catch {
+ result = false;
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Test an expression to see if it is valid as a filter.
+ /// </summary>
+ /// <param name="toTest">The filter expression to test.</param>
+ /// <returns>True if the expression is valid, false otherwise.</returns>
+ public static bool IsValidFilterExpression(string toTest)
+ {
+ bool result = true;
+
+ try
+ {
+ string[] items = toTest.Split(';');
+ for (int i = 0; i < items.Length; ++i) {
+ if (items[i] != null && items[i].Length > 0) {
+ string toCompile;
+
+ if (items[i][0] == '+')
+ toCompile = items[i].Substring(1, items[i].Length - 1);
+ else if (items[i][0] == '-')
+ toCompile = items[i].Substring(1, items[i].Length - 1);
+ else
+ toCompile = items[i];
+
+ Regex testRE = new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Singleline);
+ }
+ }
+ }
+ catch (Exception) {
+ result = false;
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Convert this filter to its string equivalent.
+ /// </summary>
+ /// <returns>The string equivalent for this filter.</returns>
+ public override string ToString()
+ {
+ return filter;
+ }
+
+ /// <summary>
+ /// Test a value to see if it is included by the filter.
+ /// </summary>
+ /// <param name="testValue">The value to test.</param>
+ /// <returns>True if the value is included, false otherwise.</returns>
+ public bool IsIncluded(string testValue)
+ {
+ bool result = false;
+ if (inclusions.Count == 0)
+ result = true;
+ else {
+ foreach (Regex r in inclusions) {
+ if (r.IsMatch(testValue)) {
+ result = true;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Test a value to see if it is excluded by the filter.
+ /// </summary>
+ /// <param name="testValue">The value to test.</param>
+ /// <returns>True if the value is excluded, false otherwise.</returns>
+ public bool IsExcluded(string testValue)
+ {
+ bool result = false;
+ foreach (Regex r in exclusions) {
+ if (r.IsMatch(testValue)) {
+ result = true;
+ break;
+ }
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Test a value to see if it matches the filter.
+ /// </summary>
+ /// <param name="testValue">The value to test.</param>
+ /// <returns>True if the value matches, false otherwise.</returns>
+ public bool IsMatch(string testValue)
+ {
+ return IsIncluded(testValue) == true && IsExcluded(testValue) == false;
+ }
+
+ /// <summary>
+ /// Compile this filter.
+ /// </summary>
+ void Compile()
+ {
+ if (filter == null)
+ return;
+
+ string[] items = filter.Split(';');
+ for (int i = 0; i < items.Length; ++i) {
+ if (items[i] != null && items[i].Length > 0) {
+ bool include = items[i][0] != '-';
+ string toCompile;
+
+ if (items[i][0] == '+')
+ toCompile = items[i].Substring(1, items[i].Length - 1);
+ else if (items[i][0] == '-')
+ toCompile = items[i].Substring(1, items[i].Length - 1);
+ else
+ toCompile = items[i];
+
+ // NOTE: Regular expressions can fail to compile here for a number of reasons that cause an exception
+ // these are left unhandled here as the caller is responsible for ensuring all is valid.
+ // several functions IsValidFilterExpression and IsValidExpression are provided for such checking
+ if (include)
+ inclusions.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline));
+ else
+ exclusions.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline));
+ }
+ }
+ }
+
+ #region Instance Fields
+ string filter;
+ ArrayList inclusions;
+ ArrayList exclusions;
+ #endregion
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/PathFilter.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/PathFilter.cs
new file mode 100644
index 00000000000..379ea85a090
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Core/PathFilter.cs
@@ -0,0 +1,137 @@
+// PathFilter.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+using System;
+using System.IO;
+
+namespace ICSharpCode.SharpZipLib.Core
+{
+ /// <summary>
+ /// Scanning filters support these operations.
+ /// </summary>
+ public interface IScanFilter
+ {
+ /// <summary>
+ /// Test a name to see if is 'matches' the filter.
+ /// </summary>
+ /// <param name="name">The name to test.</param>
+ /// <returns>Returns true if the name matches the filter, false if it does not match.</returns>
+ bool IsMatch(string name);
+ }
+
+ /// <summary>
+ /// PathFilter filters directories and files by full path name.
+ /// </summary>
+ public class PathFilter : IScanFilter
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="PathFilter"></see>.
+ /// </summary>
+ /// <param name="filter">The <see cref="NameFilter"></see>filter expression to apply.</param>
+ public PathFilter(string filter)
+ {
+ nameFilter = new NameFilter(filter);
+ }
+
+ /// <summary>
+ /// Test a name to see if it matches the filter.
+ /// </summary>
+ /// <param name="name">The name to test.</param>
+ /// <returns>True if the name matches, false otherwise.</returns>
+ public virtual bool IsMatch(string name)
+ {
+ return nameFilter.IsMatch(Path.GetFullPath(name));
+ }
+
+ #region Instance Fields
+ NameFilter nameFilter;
+ #endregion
+ }
+
+ /// <summary>
+ /// NameAnsSizeFilter filters based on name and file size.
+ /// </summary>
+ public class NameAndSizeFilter : PathFilter
+ {
+
+ /// <summary>
+ /// Initialise a new instance of NameAndSizeFilter.
+ /// </summary>
+ /// <param name="filter">The filter to apply.</param>
+ /// <param name="minSize">The minimum file size to include.</param>
+ /// <param name="maxSize">The maximum file size to include.</param>
+ public NameAndSizeFilter(string filter, long minSize, long maxSize) : base(filter)
+ {
+ this.minSize = minSize;
+ this.maxSize = maxSize;
+ }
+
+ /// <summary>
+ /// Test a filename to see if it matches the filter.
+ /// </summary>
+ /// <param name="fileName">The filename to test.</param>
+ /// <returns>True if the filter matches, false otherwise.</returns>
+ public override bool IsMatch(string fileName)
+ {
+ FileInfo fileInfo = new FileInfo(fileName);
+ long length = fileInfo.Length;
+ return base.IsMatch(fileName) &&
+ (MinSize <= length) && (MaxSize >= length);
+ }
+
+ long minSize = 0;
+
+ /// <summary>
+ /// The minimum size for a file that will match this filter.
+ /// </summary>
+ public long MinSize
+ {
+ get { return minSize; }
+ set { minSize = value; }
+ }
+
+ long maxSize = long.MaxValue;
+
+ /// <summary>
+ /// The maximum size for a file that will match this filter.
+ /// </summary>
+ public long MaxSize
+ {
+ get { return maxSize; }
+ set { maxSize = value; }
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs
new file mode 100644
index 00000000000..9d8e10dcb22
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs
@@ -0,0 +1,472 @@
+//
+// PkzipClassic encryption
+//
+// Copyright 2004 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+//
+
+using System;
+using System.Security.Cryptography;
+
+using ICSharpCode.SharpZipLib.Checksums;
+
+namespace ICSharpCode.SharpZipLib.Encryption
+{
+ /// <summary>
+ /// PkzipClassic embodies the classic or original encryption facilities used in Pkzip archives.
+ /// While it has been superceded by more recent and more powerful algorithms, its still in use and
+ /// is viable for preventing casual snooping
+ /// </summary>
+ public abstract class PkzipClassic : SymmetricAlgorithm
+ {
+ /// <summary>
+ /// Generates new encryption keys based on given seed
+ /// </summary>
+ static public byte[] GenerateKeys(byte[] seed)
+ {
+ if ( seed == null )
+ {
+ throw new ArgumentNullException("seed");
+ }
+
+ if ( seed.Length == 0 )
+ {
+ throw new ArgumentException("seed");
+ }
+
+ uint[] newKeys = new uint[] {
+ 0x12345678,
+ 0x23456789,
+ 0x34567890
+ };
+
+ for (int i = 0; i < seed.Length; ++i)
+ {
+ newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]);
+ newKeys[1] = newKeys[1] + (byte)newKeys[0];
+ newKeys[1] = newKeys[1] * 134775813 + 1;
+ newKeys[2] = Crc32.ComputeCrc32(newKeys[2], (byte)(newKeys[1] >> 24));
+ }
+
+ byte[] result = new byte[12];
+ result[0] = (byte)(newKeys[0] & 0xff);
+ result[1] = (byte)((newKeys[0] >> 8) & 0xff);
+ result[2] = (byte)((newKeys[0] >> 16) & 0xff);
+ result[3] = (byte)((newKeys[0] >> 24) & 0xff);
+ result[4] = (byte)(newKeys[1] & 0xff);
+ result[5] = (byte)((newKeys[1] >> 8) & 0xff);
+ result[6] = (byte)((newKeys[1] >> 16) & 0xff);
+ result[7] = (byte)((newKeys[1] >> 24) & 0xff);
+ result[8] = (byte)(newKeys[2] & 0xff);
+ result[9] = (byte)((newKeys[2] >> 8) & 0xff);
+ result[10] = (byte)((newKeys[2] >> 16) & 0xff);
+ result[11] = (byte)((newKeys[2] >> 24) & 0xff);
+ return result;
+ }
+ }
+
+ /// <summary>
+ /// PkzipClassicCryptoBase provides the low level facilities for encryption
+ /// and decryption using the PkzipClassic algorithm.
+ /// </summary>
+ class PkzipClassicCryptoBase
+ {
+ uint[] keys = null;
+
+ /// <summary>
+ /// Transform a single byte
+ /// </summary>
+ /// <returns>
+ /// The transformed value
+ /// </returns>
+ protected byte TransformByte()
+ {
+ uint temp = ((keys[2] & 0xFFFF) | 2);
+ return (byte)((temp * (temp ^ 1)) >> 8);
+ }
+
+ protected void SetKeys(byte[] keyData)
+ {
+ if ( keyData == null ) {
+ throw new ArgumentNullException("keyData");
+ }
+
+ if ( keyData.Length != 12 ) {
+ throw new InvalidOperationException("Keys not valid");
+ }
+
+ keys = new uint[3];
+ keys[0] = (uint)((keyData[3] << 24) | (keyData[2] << 16) | (keyData[1] << 8) | keyData[0]);
+ keys[1] = (uint)((keyData[7] << 24) | (keyData[6] << 16) | (keyData[5] << 8) | keyData[4]);
+ keys[2] = (uint)((keyData[11] << 24) | (keyData[10] << 16) | (keyData[9] << 8) | keyData[8]);
+ }
+
+ /// <summary>
+ /// Update encryption keys
+ /// </summary>
+ protected void UpdateKeys(byte ch)
+ {
+ keys[0] = Crc32.ComputeCrc32(keys[0], ch);
+ keys[1] = keys[1] + (byte)keys[0];
+ keys[1] = keys[1] * 134775813 + 1;
+ keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
+ }
+
+ /// <summary>
+ /// Reset the internal state.
+ /// </summary>
+ protected void Reset()
+ {
+ keys[0] = 0;
+ keys[1] = 0;
+ keys[2] = 0;
+ }
+ }
+
+ /// <summary>
+ /// PkzipClassic CryptoTransform for encryption.
+ /// </summary>
+ class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="PkzipClassicEncryptCryptoTransform"></see>
+ /// </summary>
+ /// <param name="keyBlock">The key block to use.</param>
+ internal PkzipClassicEncryptCryptoTransform(byte[] keyBlock)
+ {
+ SetKeys(keyBlock);
+ }
+
+ #region ICryptoTransform Members
+
+ /// <summary>
+ /// Transforms the specified region of the specified byte array.
+ /// </summary>
+ /// <param name="inputBuffer">The input for which to compute the transform.</param>
+ /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
+ /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
+ /// <returns>The computed transform.</returns>
+ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
+ {
+ byte[] result = new byte[inputCount];
+ TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
+ return result;
+ }
+
+ /// <summary>
+ /// Transforms the specified region of the input byte array and copies
+ /// the resulting transform to the specified region of the output byte array.
+ /// </summary>
+ /// <param name="inputBuffer">The input for which to compute the transform.</param>
+ /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
+ /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
+ /// <param name="outputBuffer">The output to which to write the transform.</param>
+ /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
+ /// <returns>The number of bytes written.</returns>
+ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
+ {
+ for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
+ byte oldbyte = inputBuffer[i];
+ outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte());
+ UpdateKeys(oldbyte);
+ }
+ return inputCount;
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current transform can be reused.
+ /// </summary>
+ public bool CanReuseTransform
+ {
+ get {
+ return true;
+ }
+ }
+
+ /// <summary>
+ /// Gets the size of the input data blocks in bytes.
+ /// </summary>
+ public int InputBlockSize
+ {
+ get {
+ return 1;
+ }
+ }
+
+ /// <summary>
+ /// Gets the size of the output data blocks in bytes.
+ /// </summary>
+ public int OutputBlockSize
+ {
+ get {
+ return 1;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether multiple blocks can be transformed.
+ /// </summary>
+ public bool CanTransformMultipleBlocks
+ {
+ get {
+ return true;
+ }
+ }
+
+ #endregion
+
+ #region IDisposable Members
+
+ /// <summary>
+ /// Cleanup internal state.
+ /// </summary>
+ public void Dispose()
+ {
+ Reset();
+ }
+
+ #endregion
+ }
+
+
+ /// <summary>
+ /// PkzipClassic CryptoTransform for decryption.
+ /// </summary>
+ class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="PkzipClassicDecryptCryptoTransform"></see>.
+ /// </summary>
+ /// <param name="keyBlock">The key block to decrypt with.</param>
+ internal PkzipClassicDecryptCryptoTransform(byte[] keyBlock)
+ {
+ SetKeys(keyBlock);
+ }
+
+ #region ICryptoTransform Members
+
+ /// <summary>
+ /// Transforms the specified region of the specified byte array.
+ /// </summary>
+ /// <param name="inputBuffer">The input for which to compute the transform.</param>
+ /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
+ /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
+ /// <returns>The computed transform.</returns>
+ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
+ {
+ byte[] result = new byte[inputCount];
+ TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
+ return result;
+ }
+
+ /// <summary>
+ /// Transforms the specified region of the input byte array and copies
+ /// the resulting transform to the specified region of the output byte array.
+ /// </summary>
+ /// <param name="inputBuffer">The input for which to compute the transform.</param>
+ /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
+ /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
+ /// <param name="outputBuffer">The output to which to write the transform.</param>
+ /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
+ /// <returns>The number of bytes written.</returns>
+ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
+ {
+ for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
+ byte newByte = (byte)(inputBuffer[i] ^ TransformByte());
+ outputBuffer[outputOffset++] = newByte;
+ UpdateKeys(newByte);
+ }
+ return inputCount;
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current transform can be reused.
+ /// </summary>
+ public bool CanReuseTransform
+ {
+ get {
+ return true;
+ }
+ }
+
+ /// <summary>
+ /// Gets the size of the input data blocks in bytes.
+ /// </summary>
+ public int InputBlockSize
+ {
+ get {
+ return 1;
+ }
+ }
+
+ /// <summary>
+ /// Gets the size of the output data blocks in bytes.
+ /// </summary>
+ public int OutputBlockSize
+ {
+ get {
+ return 1;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether multiple blocks can be transformed.
+ /// </summary>
+ public bool CanTransformMultipleBlocks
+ {
+ get {
+ return true;
+ }
+ }
+
+ #endregion
+
+ #region IDisposable Members
+
+ /// <summary>
+ /// Cleanup internal state.
+ /// </summary>
+ public void Dispose()
+ {
+ Reset();
+ }
+
+ #endregion
+ }
+
+ /// <summary>
+ /// Defines a wrapper object to access the Pkzip algorithm.
+ /// This class cannot be inherited.
+ /// </summary>
+ public sealed class PkzipClassicManaged : PkzipClassic
+ {
+ /// <summary>
+ /// Get / set the applicable block size.
+ /// </summary>
+ /// <remarks>The only valid block size is 8.</remarks>
+ public override int BlockSize
+ {
+ get { return 8; }
+ set {
+ if (value != 8)
+ throw new CryptographicException();
+ }
+ }
+
+ /// <summary>
+ /// Get an array of legal <see cref="KeySizes">key sizes.</see>
+ /// </summary>
+ public override KeySizes[] LegalKeySizes
+ {
+ get {
+ KeySizes[] keySizes = new KeySizes[1];
+ keySizes[0] = new KeySizes(12 * 8, 12 * 8, 0);
+ return keySizes;
+ }
+ }
+
+ /// <summary>
+ /// Generate an initial vector.
+ /// </summary>
+ public override void GenerateIV()
+ {
+ // Do nothing.
+ }
+
+ /// <summary>
+ /// Get an array of legal <see cref="KeySizes">block sizes</see>.
+ /// </summary>
+ public override KeySizes[] LegalBlockSizes
+ {
+ get {
+ KeySizes[] keySizes = new KeySizes[1];
+ keySizes[0] = new KeySizes(1 * 8, 1 * 8, 0);
+ return keySizes;
+ }
+ }
+
+ byte[] key;
+
+ /// <summary>
+ /// Get / set the key value applicable.
+ /// </summary>
+ public override byte[] Key
+ {
+ get {
+ return key;
+ }
+
+ set {
+ key = value;
+ }
+ }
+
+ /// <summary>
+ /// Generate a new random key.
+ /// </summary>
+ public override void GenerateKey()
+ {
+ key = new byte[12];
+ Random rnd = new Random();
+ rnd.NextBytes(key);
+ }
+
+ /// <summary>
+ /// Create an encryptor.
+ /// </summary>
+ /// <param name="rgbKey">The key to use for this encryptor.</param>
+ /// <param name="rgbIV">Initialisation vector for the new encryptor.</param>
+ /// <returns>Returns a new PkzipClassic encryptor</returns>
+ public override ICryptoTransform CreateEncryptor(
+ byte[] rgbKey,
+ byte[] rgbIV
+ )
+ {
+ return new PkzipClassicEncryptCryptoTransform(rgbKey);
+ }
+
+ /// <summary>
+ /// Create a decryptor.
+ /// </summary>
+ /// <param name="rgbKey">Keys to use for this new decryptor.</param>
+ /// <param name="rgbIV">Initialisation vector for the new decryptor.</param>
+ /// <returns>Returns a new decryptor.</returns>
+ public override ICryptoTransform CreateDecryptor(
+ byte[] rgbKey,
+ byte[] rgbIV
+ )
+ {
+ return new PkzipClassicDecryptCryptoTransform(rgbKey);
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZIPConstants.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZIPConstants.cs
new file mode 100644
index 00000000000..8b2de9ab979
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZIPConstants.cs
@@ -0,0 +1,92 @@
+// GZIPConstants.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.GZip
+{
+
+ /// <summary>
+ /// This class contains constants used for gzip.
+ /// </summary>
+ public class GZipConstants
+ {
+ /// <summary>
+ /// Magic number found at start of GZIP header
+ /// </summary>
+ public static readonly int GZIP_MAGIC = 0x1F8B;
+
+ /* The flag byte is divided into individual bits as follows:
+
+ bit 0 FTEXT
+ bit 1 FHCRC
+ bit 2 FEXTRA
+ bit 3 FNAME
+ bit 4 FCOMMENT
+ bit 5 reserved
+ bit 6 reserved
+ bit 7 reserved
+ */
+
+ /// <summary>
+ /// Flag bit mask for text
+ /// </summary>
+ public const int FTEXT = 0x1;
+
+ /// <summary>
+ /// Flag bitmask for Crc
+ /// </summary>
+ public const int FHCRC = 0x2;
+
+ /// <summary>
+ /// Flag bit mask for extra
+ /// </summary>
+ public const int FEXTRA = 0x4;
+
+ /// <summary>
+ /// flag bitmask for name
+ /// </summary>
+ public const int FNAME = 0x8;
+
+ /// <summary>
+ /// flag bit mask indicating comment is present
+ /// </summary>
+ public const int FCOMMENT = 0x10;
+
+ GZipConstants()
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZipException.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZipException.cs
new file mode 100644
index 00000000000..04668a63789
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GZipException.cs
@@ -0,0 +1,61 @@
+// GzipOutputStream.cs
+//
+// Copyright 2004 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using ICSharpCode.SharpZipLib;
+
+namespace ICSharpCode.SharpZipLib.GZip
+{
+ /// <summary>
+ /// GZipException represents a Gzip specific exception
+ /// </summary>
+ public class GZipException : SharpZipBaseException
+ {
+ /// <summary>
+ /// Initialise a new instance of GZipException
+ /// </summary>
+ public GZipException()
+ {
+ }
+
+ /// <summary>
+ /// Initialise a new instance of GZipException with its message string.
+ /// </summary>
+ /// <param name="message">A <see cref="string"></see>string that describes the error.</param>
+ public GZipException(string message) : base(message)
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs
new file mode 100644
index 00000000000..e229b855d06
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs
@@ -0,0 +1,340 @@
+// GzipInputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.GZip
+{
+
+ /// <summary>
+ /// This filter stream is used to decompress a "GZIP" format stream.
+ /// The "GZIP" format is described baseInputStream RFC 1952.
+ ///
+ /// author of the original java version : John Leuner
+ /// </summary>
+ /// <example> This sample shows how to unzip a gzipped file
+ /// <code>
+ /// using System;
+ /// using System.IO;
+ ///
+ /// using ICSharpCode.SharpZipLib.GZip;
+ ///
+ /// class MainClass
+ /// {
+ /// public static void Main(string[] args)
+ /// {
+ /// Stream s = new GZipInputStream(File.OpenRead(args[0]));
+ /// FileStream fs = File.Create(Path.GetFileNameWithoutExtension(args[0]));
+ /// int size = 2048;
+ /// byte[] writeData = new byte[2048];
+ /// while (true) {
+ /// size = s.Read(writeData, 0, size);
+ /// if (size > 0) {
+ /// fs.Write(writeData, 0, size);
+ /// } else {
+ /// break;
+ /// }
+ /// }
+ /// s.Close();
+ /// }
+ /// }
+ /// </code>
+ /// </example>
+ public class GZipInputStream : InflaterInputStream
+ {
+ /// <summary>
+ /// CRC-32 value for uncompressed data
+ /// </summary>
+ protected Crc32 crc = new Crc32();
+
+ /// <summary>
+ /// Indicates end of stream
+ /// </summary>
+ protected bool eos;
+
+ // Have we read the GZIP header yet?
+ bool readGZIPHeader;
+
+ /// <summary>
+ /// Creates a GzipInputStream with the default buffer size
+ /// </summary>
+ /// <param name="baseInputStream">
+ /// The stream to read compressed data from (baseInputStream GZIP format)
+ /// </param>
+ public GZipInputStream(Stream baseInputStream) : this(baseInputStream, 4096)
+ {
+ }
+
+ /// <summary>
+ /// Creates a GZIPInputStream with the specified buffer size
+ /// </summary>
+ /// <param name="baseInputStream">
+ /// The stream to read compressed data from (baseInputStream GZIP format)
+ /// </param>
+ /// <param name="size">
+ /// Size of the buffer to use
+ /// </param>
+ public GZipInputStream(Stream baseInputStream, int size) : base(baseInputStream, new Inflater(true), size)
+ {
+ }
+
+ /// <summary>
+ /// Reads uncompressed data into an array of bytes
+ /// </summary>
+ /// <param name="buf">
+ /// The buffer to read uncompressed data into
+ /// </param>
+ /// <param name="offset">
+ /// The offset indicating where the data should be placed
+ /// </param>
+ /// <param name="len">
+ /// The number of uncompressed bytes to be read
+ /// </param>
+ public override int Read(byte[] buf, int offset, int len)
+ {
+ // We first have to read the GZIP header, then we feed all the
+ // rest of the data to the base class.
+ //
+ // As we do that we continually update the CRC32. Once the data is
+ // finished, we check the CRC32
+ //
+ // This means we don't need our own buffer, as everything is done
+ // baseInputStream the superclass.
+ if (!readGZIPHeader) {
+ ReadHeader();
+ }
+
+ if (eos) {
+ return 0;
+ }
+
+ // We don't have to read the header, so we just grab data from the superclass
+ int numRead = base.Read(buf, offset, len);
+ if (numRead > 0) {
+ crc.Update(buf, offset, numRead);
+ }
+
+ if (inf.IsFinished) {
+ ReadFooter();
+ }
+ return numRead;
+ }
+
+ void ReadHeader()
+ {
+ /* 1. Check the two magic bytes */
+ Crc32 headCRC = new Crc32();
+ int magic = baseInputStream.ReadByte();
+ if (magic < 0) {
+ eos = true;
+ return;
+ }
+ headCRC.Update(magic);
+ if (magic != (GZipConstants.GZIP_MAGIC >> 8)) {
+ throw new GZipException("Error baseInputStream GZIP header, first byte doesn't match");
+ }
+
+ magic = baseInputStream.ReadByte();
+ if (magic != (GZipConstants.GZIP_MAGIC & 0xFF)) {
+ throw new GZipException("Error baseInputStream GZIP header, second byte doesn't match");
+ }
+ headCRC.Update(magic);
+
+ /* 2. Check the compression type (must be 8) */
+ int CM = baseInputStream.ReadByte();
+ if (CM != 8) {
+ throw new GZipException("Error baseInputStream GZIP header, data not baseInputStream deflate format");
+ }
+ headCRC.Update(CM);
+
+ /* 3. Check the flags */
+ int flags = baseInputStream.ReadByte();
+ if (flags < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+ headCRC.Update(flags);
+
+ /* This flag byte is divided into individual bits as follows:
+
+ bit 0 FTEXT
+ bit 1 FHCRC
+ bit 2 FEXTRA
+ bit 3 FNAME
+ bit 4 FCOMMENT
+ bit 5 reserved
+ bit 6 reserved
+ bit 7 reserved
+ */
+
+ /* 3.1 Check the reserved bits are zero */
+
+ if ((flags & 0xd0) != 0) {
+ throw new GZipException("Reserved flag bits baseInputStream GZIP header != 0");
+ }
+
+ /* 4.-6. Skip the modification time, extra flags, and OS type */
+ for (int i=0; i< 6; i++) {
+ int readByte = baseInputStream.ReadByte();
+ if (readByte < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+ headCRC.Update(readByte);
+ }
+
+ /* 7. Read extra field */
+ if ((flags & GZipConstants.FEXTRA) != 0) {
+ /* Skip subfield id */
+ for (int i=0; i< 2; i++) {
+ int readByte = baseInputStream.ReadByte();
+ if (readByte < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+ headCRC.Update(readByte);
+ }
+ if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+
+ int len1, len2, extraLen;
+ len1 = baseInputStream.ReadByte();
+ len2 = baseInputStream.ReadByte();
+ if ((len1 < 0) || (len2 < 0)) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+ headCRC.Update(len1);
+ headCRC.Update(len2);
+
+ extraLen = (len1 << 8) | len2;
+ for (int i = 0; i < extraLen;i++) {
+ int readByte = baseInputStream.ReadByte();
+ if (readByte < 0)
+ {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+ headCRC.Update(readByte);
+ }
+ }
+
+ /* 8. Read file name */
+ if ((flags & GZipConstants.FNAME) != 0) {
+ int readByte;
+ while ( (readByte = baseInputStream.ReadByte()) > 0) {
+ headCRC.Update(readByte);
+ }
+ if (readByte < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP file name");
+ }
+ headCRC.Update(readByte);
+ }
+
+ /* 9. Read comment */
+ if ((flags & GZipConstants.FCOMMENT) != 0) {
+ int readByte;
+ while ( (readByte = baseInputStream.ReadByte()) > 0) {
+ headCRC.Update(readByte);
+ }
+
+ if (readByte < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP comment");
+ }
+ headCRC.Update(readByte);
+ }
+
+ /* 10. Read header CRC */
+ if ((flags & GZipConstants.FHCRC) != 0) {
+ int tempByte;
+ int crcval = baseInputStream.ReadByte();
+ if (crcval < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+
+ tempByte = baseInputStream.ReadByte();
+ if (tempByte < 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP header");
+ }
+
+ crcval = (crcval << 8) | tempByte;
+ if (crcval != ((int) headCRC.Value & 0xffff)) {
+ throw new GZipException("Header CRC value mismatch");
+ }
+ }
+
+ readGZIPHeader = true;
+ }
+
+ void ReadFooter()
+ {
+ byte[] footer = new byte[8];
+ int avail = inf.RemainingInput;
+
+ if (avail > 8) {
+ avail = 8;
+ }
+
+ System.Array.Copy(inputBuffer.RawData, inputBuffer.RawLength - inf.RemainingInput, footer, 0, avail);
+ int needed = 8 - avail;
+
+ while (needed > 0) {
+ int count = baseInputStream.Read(footer, 8 - needed, needed);
+ if (count <= 0) {
+ throw new GZipException("Early EOF baseInputStream GZIP footer");
+ }
+ needed -= count; // Jewel Jan 16
+ }
+ int crcval = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8) | ((footer[2] & 0xff) << 16) | (footer[3] << 24);
+ if (crcval != (int) crc.Value) {
+ throw new GZipException("GZIP crc sum mismatch, theirs \"" + crcval + "\" and ours \"" + (int) crc.Value);
+ }
+
+ int total = (footer[4] & 0xff) | ((footer[5] & 0xff) << 8) | ((footer[6] & 0xff) << 16) | (footer[7] << 24);
+ if (total != inf.TotalOut) {
+ throw new GZipException("Number of bytes mismatch in footer");
+ }
+
+ /* XXX Should we support multiple members.
+ * Difficult, since there may be some bytes still baseInputStream dataBuffer
+ */
+ eos = true;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs
new file mode 100644
index 00000000000..15803b757de
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs
@@ -0,0 +1,207 @@
+// GzipOutputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.GZip
+{
+
+ /// <summary>
+ /// This filter stream is used to compress a stream into a "GZIP" stream.
+ /// The "GZIP" format is described in RFC 1952.
+ ///
+ /// author of the original java version : John Leuner
+ /// </summary>
+ /// <example> This sample shows how to gzip a file
+ /// <code>
+ /// using System;
+ /// using System.IO;
+ ///
+ /// using ICSharpCode.SharpZipLib.GZip;
+ ///
+ /// class MainClass
+ /// {
+ /// public static void Main(string[] args)
+ /// {
+ /// Stream s = new GZipOutputStream(File.Create(args[0] + ".gz"));
+ /// FileStream fs = File.OpenRead(args[0]);
+ /// byte[] writeData = new byte[fs.Length];
+ /// fs.Read(writeData, 0, (int)fs.Length);
+ /// s.Write(writeData, 0, writeData.Length);
+ /// s.Close();
+ /// }
+ /// }
+ /// </code>
+ /// </example>
+ public class GZipOutputStream : DeflaterOutputStream
+ {
+ /// <summary>
+ /// CRC-32 value for uncompressed data
+ /// </summary>
+ protected Crc32 crc = new Crc32();
+
+ /// <summary>
+ /// Creates a GzipOutputStream with the default buffer size
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// The stream to read data (to be compressed) from
+ /// </param>
+ public GZipOutputStream(Stream baseOutputStream) : this(baseOutputStream, 4096)
+ {
+ }
+
+ /// <summary>
+ /// Creates a GZipOutputStream with the specified buffer size
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// The stream to read data (to be compressed) from
+ /// </param>
+ /// <param name="size">
+ /// Size of the buffer to use
+ /// </param>
+ public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size)
+ {
+ WriteHeader();
+ }
+
+ void WriteHeader()
+ {
+ int mod_time = (int)(DateTime.Now.Ticks / 10000L); // Ticks give back 100ns intervals
+ byte[] gzipHeader = {
+ /* The two magic bytes */
+ (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) GZipConstants.GZIP_MAGIC,
+
+ /* The compression type */
+ (byte) Deflater.DEFLATED,
+
+ /* The flags (not set) */
+ 0,
+
+ /* The modification time */
+ (byte) mod_time, (byte) (mod_time >> 8),
+ (byte) (mod_time >> 16), (byte) (mod_time >> 24),
+
+ /* The extra flags */
+ 0,
+
+ /* The OS type (unknown) */
+ (byte) 255
+ };
+ baseOutputStream.Write(gzipHeader, 0, gzipHeader.Length);
+ }
+
+ /// <summary>
+ /// Write given buffer to output updating crc
+ /// </summary>
+ /// <param name="buf">Buffer to write</param>
+ /// <param name="off">Offset of first byte in buf to write</param>
+ /// <param name="len">Number of bytes to write</param>
+ public override void Write(byte[] buf, int off, int len)
+ {
+ crc.Update(buf, off, len);
+ base.Write(buf, off, len);
+ }
+
+ /// <summary>
+ /// Writes remaining compressed output data to the output stream
+ /// and closes it.
+ /// </summary>
+ public override void Close()
+ {
+ Finish();
+
+ if ( IsStreamOwner ) {
+ baseOutputStream.Close();
+ }
+ }
+
+ /// <summary>
+ /// Sets the active compression level (1-9). The new level will be activated
+ /// immediately.
+ /// </summary>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// Level specified is not supported.
+ /// </exception>
+ /// <see cref="Deflater"/>
+ public void SetLevel(int level)
+ {
+ if (level < Deflater.BEST_SPEED) {
+ throw new ArgumentOutOfRangeException("level");
+ }
+ def.SetLevel(level);
+ }
+
+ /// <summary>
+ /// Get the current compression level.
+ /// </summary>
+ /// <returns>The current compression level.</returns>
+ public int GetLevel()
+ {
+ return def.GetLevel();
+ }
+
+ /// <summary>
+ /// Finish compression and write any footer information required to stream
+ /// </summary>
+ public override void Finish()
+ {
+ base.Finish();
+
+ int totalin = def.TotalIn;
+ int crcval = (int) (crc.Value & 0xffffffff);
+
+ // System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin));
+
+ byte[] gzipFooter = {
+ (byte) crcval, (byte) (crcval >> 8),
+ (byte) (crcval >> 16), (byte) (crcval >> 24),
+
+ (byte) totalin, (byte) (totalin >> 8),
+ (byte) (totalin >> 16), (byte) (totalin >> 24)
+ };
+
+ baseOutputStream.Write(gzipFooter, 0, gzipFooter.Length);
+ // System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )");
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/ICSharpCode.SharpZLib.prjx b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/ICSharpCode.SharpZLib.prjx
new file mode 100644
index 00000000000..0fd60e59101
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/ICSharpCode.SharpZLib.prjx
@@ -0,0 +1,82 @@
+<Project name="ICSharpCode.SharpZipLib" standardNamespace="ICSharpCode.SharpZipLib" description="A nifty C# compression library" newfilesearch="None" enableviewstate="True" version="1.1" projecttype="C#">
+ <Contents>
+ <File name=".\build.bat" subtype="Code" buildaction="Exclude" dependson="" data="" />
+ <File name=".\Main.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\AssemblyInfo.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Checksums\IChecksum.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Checksums\Adler32.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Checksums\Crc32.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipEntry.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipInputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipOutputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipConstants.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipFile.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\GZip\GZipOutputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\GZip\GZipInputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\GZip\GZipConstants.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipException.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\NZipLib.ndoc" subtype="Code" buildaction="Exclude" dependson="" data="" />
+ <File name=".\NZipLib.key" subtype="Code" buildaction="Exclude" dependson="" data="" />
+ <File name=".\SharpZipLib.key" subtype="Code" buildaction="Exclude" dependson="" data="" />
+ <File name=".\BZip2\BZip2InputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\BZip2\BZip2OutputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\BZip2\BZip2Constants.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\BZip2\BZip2.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Checksums\StrangeCrc.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarEntry.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarHeader.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarBuffer.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarOutputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\InvalidHeaderException.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarInputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarArchive.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\PendingBuffer.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\InflaterDynHeader.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\InflaterHuffmanTree.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\DeflaterPending.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\DeflaterHuffman.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\DeflaterEngine.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Inflater.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\DeflaterConstants.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Deflater.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Streams\DeflaterOutputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Streams\InflaterInputStream.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Streams\StreamManipulator.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\Compression\Streams\OutputWindow.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar" subtype="Directory" buildaction="Nothing" dependson="" data="" />
+ <File name=".\Zip\Compression" subtype="Directory" buildaction="Nothing" dependson="" data="" />
+ <File name=".\Zip\Compression\Streams" subtype="Directory" buildaction="Nothing" dependson="" data="" />
+ <File name=".\SharpZipBaseException.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Tar\TarException.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\GZip\GZipException.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\BZip2\BZip2Exception.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Core" subtype="Directory" buildaction="Compile" dependson="" data="" />
+ <File name=".\Core\NameFilter.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\FastZip.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Core\FileSystemScanner.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Core\PathFilter.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Core\INameTransform.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Zip\ZipNameTransform.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ <File name=".\Encryption" subtype="Directory" buildaction="Compile" dependson="" data="" />
+ <File name=".\Encryption\PkzipClassic.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
+ </Contents>
+ <References />
+ <DeploymentInformation target="" script="" strategy="File" />
+ <Configuration runwithwarnings="True" name="Release">
+ <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="False" optimize="True" unsafecodeallowed="False" generateoverflowchecks="False" mainclass="" target="Library" definesymbols="" generatexmldocumentation="True" win32Icon="" noconfig="False" nostdlib="False" />
+ <Execution commandlineparameters="" consolepause="True" />
+ <Output directory="..\bin" assembly="ICSharpCode.SharpZipLib" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
+ </Configuration>
+ <Configurations active="Release">
+ <Configuration runwithwarnings="True" name="Release">
+ <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="False" optimize="True" unsafecodeallowed="False" generateoverflowchecks="False" mainclass="" target="Library" definesymbols="" generatexmldocumentation="True" win32Icon="" noconfig="False" nostdlib="False" />
+ <Execution commandlineparameters="" consolepause="True" />
+ <Output directory="..\bin" assembly="ICSharpCode.SharpZipLib" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
+ </Configuration>
+ <Configuration runwithwarnings="False" name="Debug">
+ <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="True" optimize="True" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Library" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" />
+ <Execution commandlineparameters="" consolepause="True" />
+ <Output directory="..\bin" assembly="ICSharpCode.SharpZipLib" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
+ </Configuration>
+ </Configurations>
+</Project> \ No newline at end of file
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Main.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Main.cs
new file mode 100644
index 00000000000..6e4f27fad7a
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Main.cs
@@ -0,0 +1,36 @@
+// Main.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+//
+
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/SharpZipBaseException.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/SharpZipBaseException.cs
new file mode 100644
index 00000000000..82950798f6a
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/SharpZipBaseException.cs
@@ -0,0 +1,73 @@
+// SharpZipBaseException.cs
+//
+// Copyright 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib
+{
+ /// <summary>
+ /// SharpZipBaseException is the base exception class for the SharpZipLibrary.
+ /// All library exceptions are derived from this.
+ /// </summary>
+ public class SharpZipBaseException : ApplicationException
+ {
+ /// <summary>
+ /// Initializes a new instance of the SharpZipLibraryException class.
+ /// </summary>
+ public SharpZipBaseException()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the SharpZipLibraryException class with a specified error message.
+ /// </summary>
+ public SharpZipBaseException(string msg) : base(msg)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the SharpZipLibraryException class with a specified
+ /// error message and a reference to the inner exception that is the cause of this exception.
+ /// </summary>
+ /// <param name="message">Error message string</param>
+ /// <param name="innerException">The inner exception</param>
+ public SharpZipBaseException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs
new file mode 100644
index 00000000000..76d4c596dcc
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs
@@ -0,0 +1,73 @@
+// InvalidHeaderException.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.Tar {
+
+ /// <summary>
+ /// This exception is used to indicate that there is a problem
+ /// with a TAR archive header.
+ /// </summary>
+ public class InvalidHeaderException : TarException
+ {
+ /// <summary>
+ /// Initialise a new instance of the InvalidHeaderException class.
+ /// </summary>
+ public InvalidHeaderException()
+ {
+ }
+
+ /// <summary>
+ /// Initialises a new instance of the InvalidHeaderException class with a specified message.
+ /// </summary>
+ public InvalidHeaderException(string msg) : base(msg)
+ {
+ }
+ }
+}
+
+/* The original Java file had this header:
+** Authored by Timothy Gerard Endres
+** <mailto:time@gjt.org> <http://www.trustice.com>
+**
+** This work has been placed into the public domain.
+** You may use this work in any way and for any purpose you wish.
+**
+** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+** REDISTRIBUTION OF THIS SOFTWARE.
+**
+*/
+
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
new file mode 100644
index 00000000000..441daf747dc
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
@@ -0,0 +1,680 @@
+// TarArchive.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar {
+ /// <summary>
+ /// Used to advise clients of 'events' while processing archives
+ /// </summary>
+ public delegate void ProgressMessageHandler(TarArchive archive, TarEntry entry, string message);
+
+ /// <summary>
+ /// The TarArchive class implements the concept of a
+ /// 'Tape Archive'. A tar archive is a series of entries, each of
+ /// which represents a file system object. Each entry in
+ /// the archive consists of a header block followed by 0 or more data blocks.
+ /// Directory entries consist only of the header block, and are followed by entries
+ /// for the directory's contents. File entries consist of a
+ /// header followed by the number of blocks needed to
+ /// contain the file's contents. All entries are written on
+ /// block boundaries. Blocks are 512 bytes long.
+ ///
+ /// TarArchives are instantiated in either read or write mode,
+ /// based upon whether they are instantiated with an InputStream
+ /// or an OutputStream. Once instantiated TarArchives read/write
+ /// mode can not be changed.
+ ///
+ /// There is currently no support for random access to tar archives.
+ /// However, it seems that subclassing TarArchive, and using the
+ /// TarBuffer.getCurrentRecordNum() and TarBuffer.getCurrentBlockNum()
+ /// methods, this would be rather trvial.
+ /// </summary>
+ public class TarArchive
+ {
+ bool keepOldFiles;
+ bool asciiTranslate;
+
+ int userId;
+ string userName;
+ int groupId;
+ string groupName;
+
+ string rootPath;
+ string pathPrefix;
+
+ int recordSize;
+ byte[] recordBuf;
+
+ TarInputStream tarIn;
+ TarOutputStream tarOut;
+
+ /// <summary>
+ /// Client hook allowing detailed information to be reported during processing
+ /// </summary>
+ public event ProgressMessageHandler ProgressMessageEvent;
+
+ /// <summary>
+ /// Raises the ProgressMessage event
+ /// </summary>
+ /// <param name="entry">TarEntry for this event</param>
+ /// <param name="message">message for this event. Null is no message</param>
+ protected virtual void OnProgressMessageEvent(TarEntry entry, string message)
+ {
+ if (ProgressMessageEvent != null) {
+ ProgressMessageEvent(this, entry, message);
+ }
+ }
+
+ /// <summary>
+ /// Constructor for a TarArchive.
+ /// </summary>
+ protected TarArchive()
+ {
+ }
+
+ /// <summary>
+ /// The InputStream based constructors create a TarArchive for the
+ /// purposes of extracting or listing a tar archive. Thus, use
+ /// these constructors when you wish to extract files from or list
+ /// the contents of an existing tar archive.
+ /// </summary>
+ public static TarArchive CreateInputTarArchive(Stream inputStream)
+ {
+ return CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor);
+ }
+
+ /// <summary>
+ /// Create TarArchive for reading setting block factor
+ /// </summary>
+ /// <param name="inputStream">Stream for tar archive contents</param>
+ /// <param name="blockFactor">The blocking factor to apply</param>
+ /// <returns>
+ /// TarArchive
+ /// </returns>
+ public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor)
+ {
+ TarArchive archive = new TarArchive();
+ archive.tarIn = new TarInputStream(inputStream, blockFactor);
+ archive.Initialize(blockFactor * TarBuffer.BlockSize);
+ return archive;
+ }
+
+ /// <summary>
+ /// Create a TarArchive for writing to, using the default blocking factor
+ /// </summary>
+ /// <param name="outputStream">Stream to write to</param>
+ public static TarArchive CreateOutputTarArchive(Stream outputStream)
+ {
+ return CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor);
+ }
+
+ /// <summary>
+ /// Create a TarArchive for writing to
+ /// </summary>
+ /// <param name="outputStream">The stream to write to</param>
+ /// <param name="blockFactor">The blocking factor to use for buffering.</param>
+ public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor)
+ {
+ TarArchive archive = new TarArchive();
+ archive.tarOut = new TarOutputStream(outputStream, blockFactor);
+ archive.Initialize(blockFactor * TarBuffer.BlockSize);
+ return archive;
+ }
+
+ /// <summary>
+ /// Common constructor initialization code.
+ /// </summary>
+ void Initialize(int recordSize)
+ {
+ this.recordSize = recordSize;
+ this.rootPath = null;
+ this.pathPrefix = null;
+
+ this.userId = 0;
+ this.userName = String.Empty;
+ this.groupId = 0;
+ this.groupName = String.Empty;
+
+ this.keepOldFiles = false;
+
+ this.recordBuf = new byte[RecordSize];
+ }
+
+ /// <summary>
+ /// Set the flag that determines whether existing files are
+ /// kept, or overwritten during extraction.
+ /// </summary>
+ /// <param name="keepOldFiles">
+ /// If true, do not overwrite existing files.
+ /// </param>
+ public void SetKeepOldFiles(bool keepOldFiles)
+ {
+ this.keepOldFiles = keepOldFiles;
+ }
+
+ /// <summary>
+ /// Set the ascii file translation flag. If ascii file translation
+ /// is true, then the file is checked to see if it a binary file or not.
+ /// If the flag is true and the test indicates it is ascii text
+ /// file, it will be translated. The translation converts the local
+ /// operating system's concept of line ends into the UNIX line end,
+ /// '\n', which is the defacto standard for a TAR archive. This makes
+ /// text files compatible with UNIX.
+ /// </summary>
+ /// <param name= "asciiTranslate">
+ /// If true, translate ascii text files.
+ /// </param>
+ public void SetAsciiTranslation(bool asciiTranslate)
+ {
+ this.asciiTranslate = asciiTranslate;
+ }
+
+ /// <summary>
+ /// PathPrefix is added to entry names as they are written if the value is not null.
+ /// A slash character is appended after PathPrefix
+ /// </summary>
+ public string PathPrefix
+ {
+ get { return pathPrefix; }
+ set { pathPrefix = value; }
+
+ }
+
+ /// <summary>
+ /// RootPath is removed from entry names if it is found at the
+ /// beginning of the name.
+ /// </summary>
+ public string RootPath
+ {
+ get { return rootPath; }
+ set { rootPath = value; }
+ }
+
+ /// <summary>
+ /// Set user and group information that will be used to fill in the
+ /// tar archive's entry headers. This information based on that available
+ /// for the linux operating system, which is not always available on other
+ /// operating systems. TarArchive allows the programmer to specify values
+ /// to be used in their place.
+ /// </summary>
+ /// <param name="userId">
+ /// The user id to use in the headers.
+ /// </param>
+ /// <param name="userName">
+ /// The user name to use in the headers.
+ /// </param>
+ /// <param name="groupId">
+ /// The group id to use in the headers.
+ /// </param>
+ /// <param name="groupName">
+ /// The group name to use in the headers.
+ /// </param>
+ public void SetUserInfo(int userId, string userName, int groupId, string groupName)
+ {
+ this.userId = userId;
+ this.userName = userName;
+ this.groupId = groupId;
+ this.groupName = groupName;
+ applyUserInfoOverrides = true;
+ }
+
+ bool applyUserInfoOverrides = false;
+
+ /// <summary>
+ /// Get or set a value indicating if overrides defined by <see cref="SetUserInfo">SetUserInfo</see> should be applied.
+ /// </summary>
+ /// <remarks>If overrides are not applied then the values as set in each header will be used.</remarks>
+ public bool ApplyUserInfoOverrides
+ {
+ get { return applyUserInfoOverrides; }
+ set { applyUserInfoOverrides = value; }
+ }
+
+ /// <summary>
+ /// Get the archive user id.
+ /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
+ /// on how to allow setting values on a per entry basis.
+ /// </summary>
+ /// <returns>
+ /// The current user id.
+ /// </returns>
+ public int UserId {
+ get {
+ return this.userId;
+ }
+ }
+
+ /// <summary>
+ /// Get the archive user name.
+ /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
+ /// on how to allow setting values on a per entry basis.
+ /// </summary>
+ /// <returns>
+ /// The current user name.
+ /// </returns>
+ public string UserName {
+ get {
+ return this.userName;
+ }
+ }
+
+ /// <summary>
+ /// Get the archive group id.
+ /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
+ /// on how to allow setting values on a per entry basis.
+ /// </summary>
+ /// <returns>
+ /// The current group id.
+ /// </returns>
+ public int GroupId {
+ get {
+ return this.groupId;
+ }
+ }
+
+ /// <summary>
+ /// Get the archive group name.
+ /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
+ /// on how to allow setting values on a per entry basis.
+ /// </summary>
+ /// <returns>
+ /// The current group name.
+ /// </returns>
+ public string GroupName {
+ get {
+ return this.groupName;
+ }
+ }
+
+ /// <summary>
+ /// Get the archive's record size. Because of its history, tar
+ /// supports the concept of buffered IO consisting of RECORDS of
+ /// BLOCKS. This allowed tar to match the IO characteristics of
+ /// the physical device being used. Of course, in the C# world,
+ /// this makes no sense, WITH ONE EXCEPTION - archives are expected
+ /// to be properly "blocked". Thus, all of the horrible TarBuffer
+ /// support boils down to simply getting the "boundaries" correct.
+ /// </summary>
+ /// <returns>
+ /// The record size this archive is using.
+ /// </returns>
+ public int RecordSize {
+ get {
+ if (this.tarIn != null) {
+ return this.tarIn.GetRecordSize();
+ } else if (this.tarOut != null) {
+ return this.tarOut.GetRecordSize();
+ }
+ return TarBuffer.DefaultRecordSize;
+ }
+ }
+
+ /// <summary>
+ /// Close the archive. This simply calls the underlying
+ /// tar stream's close() method.
+ /// </summary>
+ public void CloseArchive()
+ {
+ if (this.tarIn != null) {
+ this.tarIn.Close();
+ } else if (this.tarOut != null) {
+ this.tarOut.Flush();
+ this.tarOut.Close();
+ }
+ }
+
+ /// <summary>
+ /// Perform the "list" command for the archive contents.
+ ///
+ /// NOTE That this method uses the <see cref="ProgressMessageEvent"> progress event</see> to actually list
+ /// the contents. If the progress display event is not set, nothing will be listed!
+ /// </summary>
+ public void ListContents()
+ {
+ while (true) {
+ TarEntry entry = this.tarIn.GetNextEntry();
+
+ if (entry == null) {
+ break;
+ }
+ OnProgressMessageEvent(entry, null);
+ }
+ }
+
+ /// <summary>
+ /// Perform the "extract" command and extract the contents of the archive.
+ /// </summary>
+ /// <param name="destDir">
+ /// The destination directory into which to extract.
+ /// </param>
+ public void ExtractContents(string destDir)
+ {
+ while (true) {
+ TarEntry entry = this.tarIn.GetNextEntry();
+
+ if (entry == null) {
+ break;
+ }
+
+ this.ExtractEntry(destDir, entry);
+ }
+ }
+
+ void EnsureDirectoryExists(string directoryName)
+ {
+ if (!Directory.Exists(directoryName)) {
+ try {
+ Directory.CreateDirectory(directoryName);
+ }
+ catch (Exception e) {
+ throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message);
+ }
+ }
+ }
+
+ // TODO: Is there a better way to test for a text file?
+ // It no longer reads entire files into memory but is still a weak test!
+ // assumes that ascii 0-7, 14-31 or 255 are binary
+ // and that all non text files contain one of these values
+ bool IsBinary(string filename)
+ {
+ using (FileStream fs = File.OpenRead(filename))
+ {
+ int sampleSize = System.Math.Min(4096, (int)fs.Length);
+ byte[] content = new byte[sampleSize];
+
+ int bytesRead = fs.Read(content, 0, sampleSize);
+
+ for (int i = 0; i < bytesRead; ++i) {
+ byte b = content[i];
+ if (b < 8 || (b > 13 && b < 32) || b == 255) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Extract an entry from the archive. This method assumes that the
+ /// tarIn stream has been properly set with a call to getNextEntry().
+ /// </summary>
+ /// <param name="destDir">
+ /// The destination directory into which to extract.
+ /// </param>
+ /// <param name="entry">
+ /// The TarEntry returned by tarIn.getNextEntry().
+ /// </param>
+ void ExtractEntry(string destDir, TarEntry entry)
+ {
+ OnProgressMessageEvent(entry, null);
+
+ string name = entry.Name;
+
+ if (Path.IsPathRooted(name) == true) {
+ // NOTE:
+ // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt
+ name = name.Substring(Path.GetPathRoot(name).Length);
+ }
+
+ name = name.Replace('/', Path.DirectorySeparatorChar);
+
+ string destFile = Path.Combine(destDir, name);
+
+ if (entry.IsDirectory) {
+ EnsureDirectoryExists(destFile);
+ } else {
+ string parentDirectory = Path.GetDirectoryName(destFile);
+ EnsureDirectoryExists(parentDirectory);
+
+ bool process = true;
+ FileInfo fileInfo = new FileInfo(destFile);
+ if (fileInfo.Exists) {
+ if (this.keepOldFiles) {
+ OnProgressMessageEvent(entry, "Destination file already exists");
+ process = false;
+ } else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0) {
+ OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
+ process = false;
+ }
+ }
+
+ if (process) {
+ bool asciiTrans = false;
+
+ Stream outputStream = File.Create(destFile);
+ if (this.asciiTranslate) {
+ asciiTrans = !IsBinary(destFile);
+ }
+
+ StreamWriter outw = null;
+ if (asciiTrans) {
+ outw = new StreamWriter(outputStream);
+ }
+
+ byte[] rdbuf = new byte[32 * 1024];
+
+ while (true) {
+ int numRead = this.tarIn.Read(rdbuf, 0, rdbuf.Length);
+
+ if (numRead <= 0) {
+ break;
+ }
+
+ if (asciiTrans) {
+ for (int off = 0, b = 0; b < numRead; ++b) {
+ if (rdbuf[b] == 10) {
+ string s = Encoding.ASCII.GetString(rdbuf, off, (b - off));
+ outw.WriteLine(s);
+ off = b + 1;
+ }
+ }
+ } else {
+ outputStream.Write(rdbuf, 0, numRead);
+ }
+ }
+
+ if (asciiTrans) {
+ outw.Close();
+ } else {
+ outputStream.Close();
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Write an entry to the archive. This method will call the putNextEntry
+ /// and then write the contents of the entry, and finally call closeEntry()
+ /// for entries that are files. For directories, it will call putNextEntry(),
+ /// and then, if the recurse flag is true, process each entry that is a
+ /// child of the directory.
+ /// </summary>
+ /// <param name="sourceEntry">
+ /// The TarEntry representing the entry to write to the archive.
+ /// </param>
+ /// <param name="recurse">
+ /// If true, process the children of directory entries.
+ /// </param>
+ public void WriteEntry(TarEntry sourceEntry, bool recurse)
+ {
+ try
+ {
+ if ( recurse ) {
+ TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName,
+ sourceEntry.GroupId, sourceEntry.GroupName);
+ }
+ InternalWriteEntry(sourceEntry, recurse);
+ }
+ finally
+ {
+ if ( recurse ) {
+ TarHeader.RestoreSetValues();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Write an entry to the archive. This method will call the putNextEntry
+ /// and then write the contents of the entry, and finally call closeEntry()
+ /// for entries that are files. For directories, it will call putNextEntry(),
+ /// and then, if the recurse flag is true, process each entry that is a
+ /// child of the directory.
+ /// </summary>
+ /// <param name="sourceEntry">
+ /// The TarEntry representing the entry to write to the archive.
+ /// </param>
+ /// <param name="recurse">
+ /// If true, process the children of directory entries.
+ /// </param>
+ void InternalWriteEntry(TarEntry sourceEntry, bool recurse)
+ {
+ bool asciiTrans = false;
+
+ string tempFileName = null;
+ string entryFilename = sourceEntry.File;
+
+ TarEntry entry = (TarEntry)sourceEntry.Clone();
+
+ if ( applyUserInfoOverrides ) {
+ entry.GroupId = groupId;
+ entry.GroupName = groupName;
+ entry.UserId = userId;
+ entry.UserName = userName;
+ }
+
+ OnProgressMessageEvent(entry, null);
+
+ if (this.asciiTranslate && !entry.IsDirectory) {
+ asciiTrans = !IsBinary(entryFilename);
+
+ if (asciiTrans) {
+ tempFileName = Path.GetTempFileName();
+
+ StreamReader inStream = File.OpenText(entryFilename);
+ Stream outStream = File.Create(tempFileName);
+
+ while (true) {
+ string line = inStream.ReadLine();
+ if (line == null) {
+ break;
+ }
+ byte[] data = Encoding.ASCII.GetBytes(line);
+ outStream.Write(data, 0, data.Length);
+ outStream.WriteByte((byte)'\n');
+ }
+
+ inStream.Close();
+
+ outStream.Flush();
+ outStream.Close();
+
+ entry.Size = new FileInfo(tempFileName).Length;
+
+ entryFilename = tempFileName;
+ }
+ }
+
+ string newName = null;
+
+ if (this.rootPath != null) {
+ if (entry.Name.StartsWith(this.rootPath)) {
+ newName = entry.Name.Substring(this.rootPath.Length + 1 );
+ }
+ }
+
+ if (this.pathPrefix != null) {
+ newName = (newName == null) ? this.pathPrefix + "/" + entry.Name : this.pathPrefix + "/" + newName;
+ }
+
+ if (newName != null) {
+ entry.Name = newName;
+ }
+
+ this.tarOut.PutNextEntry(entry);
+
+ if (entry.IsDirectory) {
+ if (recurse) {
+ TarEntry[] list = entry.GetDirectoryEntries();
+ for (int i = 0; i < list.Length; ++i) {
+ InternalWriteEntry(list[i], recurse);
+ }
+ }
+ } else {
+ Stream inputStream = File.OpenRead(entryFilename);
+ int numWritten = 0;
+ byte[] eBuf = new byte[32 * 1024];
+ while (true) {
+ int numRead = inputStream.Read(eBuf, 0, eBuf.Length);
+
+ if (numRead <=0) {
+ break;
+ }
+
+ this.tarOut.Write(eBuf, 0, numRead);
+ numWritten += numRead;
+ }
+
+ inputStream.Close();
+
+ if (tempFileName != null && tempFileName.Length > 0) {
+ File.Delete(tempFileName);
+ }
+
+ this.tarOut.CloseEntry();
+ }
+ }
+ }
+}
+
+
+/* The original Java file had this header:
+ ** Authored by Timothy Gerard Endres
+ ** <mailto:time@gjt.org> <http://www.trustice.com>
+ **
+ ** This work has been placed into the public domain.
+ ** You may use this work in any way and for any purpose you wish.
+ **
+ ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+ ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+ ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+ ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+ ** REDISTRIBUTION OF THIS SOFTWARE.
+ **
+ */
+
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
new file mode 100644
index 00000000000..6bebbe0852a
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
@@ -0,0 +1,482 @@
+// TarBuffer.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar
+{
+
+ /// <summary>
+ /// The TarBuffer class implements the tar archive concept
+ /// of a buffered input stream. This concept goes back to the
+ /// days of blocked tape drives and special io devices. In the
+ /// C# universe, the only real function that this class
+ /// performs is to ensure that files have the correct "record"
+ /// size, or other tars will complain.
+ /// <p>
+ /// You should never have a need to access this class directly.
+ /// TarBuffers are created by Tar IO Streams.
+ /// </p>
+ /// </summary>
+ public class TarBuffer
+ {
+
+/* A quote from GNU tar man file on blocking and records
+ A `tar' archive file contains a series of blocks. Each block
+contains `BLOCKSIZE' bytes. Although this format may be thought of as
+being on magnetic tape, other media are often used.
+
+ Each file archived is represented by a header block which describes
+the file, followed by zero or more blocks which give the contents of
+the file. At the end of the archive file there may be a block filled
+with binary zeros as an end-of-file marker. A reasonable system should
+write a block of zeros at the end, but must not assume that such a
+block exists when reading an archive.
+
+ The blocks may be "blocked" for physical I/O operations. Each
+record of N blocks (where N is set by the `--blocking-factor=512-SIZE'
+(`-b 512-SIZE') option to `tar') is written with a single `write ()'
+operation. On magnetic tapes, the result of such a write is a single
+record. When writing an archive, the last record of blocks should be
+written at the full size, with blocks after the zero block containing
+all zeros. When reading an archive, a reasonable system should
+properly handle an archive whose last record is shorter than the rest,
+or which contains garbage records after a zero block.
+*/
+
+ /// <summary>
+ /// The size of a block in a tar archive in bytes.
+ /// </summary>
+ /// <remarks>This is 512 bytes.</remarks>
+ public const int BlockSize = 512;
+
+ /// <summary>
+ /// The number of blocks in a default record.
+ /// </summary>
+ /// <remarks>
+ /// The default value is 20 block per record.
+ /// </remarks>
+ public const int DefaultBlockFactor = 20;
+
+ /// <summary>
+ /// The size in bytes of a default record.
+ /// </summary>
+ /// <remarks>
+ /// The default size is 10KB.
+ /// </remarks>
+ public const int DefaultRecordSize = BlockSize * DefaultBlockFactor;
+
+ Stream inputStream;
+ Stream outputStream;
+
+ byte[] recordBuffer;
+ int currentBlockIndex;
+ int currentRecordIndex;
+
+ int recordSize = DefaultRecordSize;
+
+ /// <summary>
+ /// Get the record size for this buffer
+ /// </summary>
+ public int RecordSize {
+ get {
+ return recordSize;
+ }
+ }
+
+ int blockFactor = DefaultBlockFactor;
+
+ /// <summary>
+ /// Get the Blocking factor for the buffer
+ /// </summary>
+ public int BlockFactor {
+ get {
+ return blockFactor;
+ }
+ }
+
+
+ /// <summary>
+ /// Construct a default TarBuffer
+ /// </summary>
+ protected TarBuffer()
+ {
+ }
+
+ /// <summary>
+ /// Create TarBuffer for reading with default BlockFactor
+ /// </summary>
+ /// <param name="inputStream">Stream to buffer</param>
+ /// <returns>TarBuffer</returns>
+ public static TarBuffer CreateInputTarBuffer(Stream inputStream)
+ {
+ return CreateInputTarBuffer(inputStream, TarBuffer.DefaultBlockFactor);
+ }
+
+ /// <summary>
+ /// Construct TarBuffer for reading inputStream setting BlockFactor
+ /// </summary>
+ /// <param name="inputStream">Stream to buffer</param>
+ /// <param name="blockFactor">Blocking factor to apply</param>
+ /// <returns>TarBuffer</returns>
+ public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor)
+ {
+ TarBuffer tarBuffer = new TarBuffer();
+ tarBuffer.inputStream = inputStream;
+ tarBuffer.outputStream = null;
+ tarBuffer.Initialize(blockFactor);
+
+ return tarBuffer;
+ }
+
+ /// <summary>
+ /// Construct TarBuffer for writing with default BlockFactor
+ /// </summary>
+ /// <param name="outputStream">output stream for buffer</param>
+ /// <returns>TarBuffer</returns>
+ public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
+ {
+ return CreateOutputTarBuffer(outputStream, TarBuffer.DefaultBlockFactor);
+ }
+
+ /// <summary>
+ /// Construct TarBuffer for writing Tar output to streams.
+ /// </summary>
+ /// <param name="outputStream">Output stream to write to.</param>
+ /// <param name="blockFactor">Blocking factor to apply</param>
+ /// <returns>TarBuffer</returns>
+ public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor)
+ {
+ TarBuffer tarBuffer = new TarBuffer();
+ tarBuffer.inputStream = null;
+ tarBuffer.outputStream = outputStream;
+ tarBuffer.Initialize(blockFactor);
+
+ return tarBuffer;
+ }
+
+ /// <summary>
+ /// Initialization common to all constructors.
+ /// </summary>
+ void Initialize(int blockFactor)
+ {
+ this.blockFactor = blockFactor;
+ this.recordSize = blockFactor * BlockSize;
+
+ this.recordBuffer = new byte[RecordSize];
+
+ if (inputStream != null) {
+ this.currentRecordIndex = -1;
+ this.currentBlockIndex = BlockFactor;
+ } else {
+ this.currentRecordIndex = 0;
+ this.currentBlockIndex = 0;
+ }
+ }
+
+ /// <summary>
+ /// Get the TAR Buffer's block factor
+ /// </summary>
+ public int GetBlockFactor()
+ {
+ return this.blockFactor;
+ }
+
+ /// <summary>
+ /// Get the TAR Buffer's record size.
+ /// </summary>
+ public int GetRecordSize()
+ {
+ return this.recordSize;
+ }
+
+ /// <summary>
+ /// Determine if an archive block indicates End of Archive. End of
+ /// archive is indicated by a block that consists entirely of null bytes.
+ /// All remaining blocks for the record should also be null's
+ /// However some older tars only do a couple of null blocks (Old GNU tar for one)
+ /// and also partial records
+ /// </summary>
+ /// <param name = "block">The data block to check.</param>
+ public bool IsEOFBlock(byte[] block)
+ {
+ for (int i = 0, sz = BlockSize; i < sz; ++i) {
+ if (block[i] != 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Skip over a block on the input stream.
+ /// </summary>
+ public void SkipBlock()
+ {
+ if (this.inputStream == null) {
+ throw new TarException("no input stream defined");
+ }
+
+ if (this.currentBlockIndex >= this.BlockFactor) {
+ if (!this.ReadRecord()) {
+ return;
+ }
+ }
+
+ this.currentBlockIndex++;
+ }
+
+ /// <summary>
+ /// Read a block from the input stream.
+ /// </summary>
+ /// <returns>
+ /// The block of data read.
+ /// </returns>
+ public byte[] ReadBlock()
+ {
+ if (this.inputStream == null) {
+ throw new TarException("TarBuffer.ReadBlock - no input stream defined");
+ }
+
+ if (this.currentBlockIndex >= this.BlockFactor) {
+ if (!this.ReadRecord()) {
+ return null;
+ }
+ }
+
+ byte[] result = new byte[BlockSize];
+
+ Array.Copy(this.recordBuffer, (this.currentBlockIndex * BlockSize), result, 0, BlockSize );
+ this.currentBlockIndex++;
+ return result;
+ }
+
+ /// <summary>
+ /// Read a record from data stream.
+ /// </summary>
+ /// <returns>
+ /// false if End-Of-File, else true.
+ /// </returns>
+ bool ReadRecord()
+ {
+ if (this.inputStream == null) {
+ throw new TarException("no input stream stream defined");
+ }
+
+ this.currentBlockIndex = 0;
+
+ int offset = 0;
+ int bytesNeeded = RecordSize;
+
+ while (bytesNeeded > 0) {
+ long numBytes = this.inputStream.Read(this.recordBuffer, offset, bytesNeeded);
+
+ //
+ // NOTE
+ // We have found EOF, and the record is not full!
+ //
+ // This is a broken archive. It does not follow the standard
+ // blocking algorithm. However, because we are generous, and
+ // it requires little effort, we will simply ignore the error
+ // and continue as if the entire record were read. This does
+ // not appear to break anything upstream. We used to return
+ // false in this case.
+ //
+ // Thanks to 'Yohann.Roussel@alcatel.fr' for this fix.
+ //
+ if (numBytes <= 0) {
+ break;
+ }
+
+ offset += (int)numBytes;
+ bytesNeeded -= (int)numBytes;
+ }
+
+ this.currentRecordIndex++;
+ return true;
+ }
+
+ /// <summary>
+ /// Get the current block number, within the current record, zero based.
+ /// </summary>
+ /// <returns>
+ /// The current zero based block number.
+ /// </returns>
+ /// <remarks>
+ /// The absolute block number = (<see cref="GetCurrentRecordNum">record number</see> * <see cref="BlockFactor">block factor</see>) + <see cref="GetCurrentBlockNum">block number</see>.
+ /// </remarks>
+ public int GetCurrentBlockNum()
+ {
+ return this.currentBlockIndex;
+ }
+
+ /// <summary>
+ /// Get the current record number.
+ /// </summary>
+ /// <returns>
+ /// The current zero based record number.
+ /// </returns>
+ public int GetCurrentRecordNum()
+ {
+ return this.currentRecordIndex;
+ }
+
+ /// <summary>
+ /// Write a block of data to the archive.
+ /// </summary>
+ /// <param name="block">
+ /// The data to write to the archive.
+ /// </param>
+ public void WriteBlock(byte[] block)
+ {
+ if (this.outputStream == null) {
+ throw new TarException("TarBuffer.WriteBlock - no output stream defined");
+ }
+
+ if (block.Length != BlockSize) {
+ throw new TarException("TarBuffer.WriteBlock - block to write has length '" + block.Length + "' which is not the block size of '" + BlockSize + "'" );
+ }
+
+ if (this.currentBlockIndex >= BlockFactor) {
+ this.WriteRecord();
+ }
+
+ Array.Copy(block, 0, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
+ this.currentBlockIndex++;
+ }
+
+ /// <summary>
+ /// Write an archive record to the archive, where the record may be
+ /// inside of a larger array buffer. The buffer must be "offset plus
+ /// record size" long.
+ /// </summary>
+ /// <param name="buf">
+ /// The buffer containing the record data to write.
+ /// </param>
+ /// <param name="offset">
+ /// The offset of the record data within buf.
+ /// </param>
+ public void WriteBlock(byte[] buf, int offset)
+ {
+ if (this.outputStream == null) {
+ throw new TarException("TarBuffer.WriteBlock - no output stream stream defined");
+ }
+
+ if ((offset + BlockSize) > buf.Length) {
+ throw new TarException("TarBuffer.WriteBlock - record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
+ }
+
+ if (this.currentBlockIndex >= this.BlockFactor) {
+ this.WriteRecord();
+ }
+
+ Array.Copy(buf, offset, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
+
+ this.currentBlockIndex++;
+ }
+
+ /// <summary>
+ /// Write a TarBuffer record to the archive.
+ /// </summary>
+ void WriteRecord()
+ {
+ if (this.outputStream == null)
+ {
+ throw new TarException("TarBuffer.WriteRecord no output stream defined");
+ }
+
+ this.outputStream.Write(this.recordBuffer, 0, RecordSize);
+ this.outputStream.Flush();
+
+ this.currentBlockIndex = 0;
+ this.currentRecordIndex++;
+ }
+
+ /// <summary>
+ /// Flush the current data block if it has any data in it.
+ /// </summary>
+ void Flush()
+ {
+ if (this.outputStream == null)
+ {
+ throw new TarException("TarBuffer.Flush no output stream defined");
+ }
+
+ if (this.currentBlockIndex > 0)
+ {
+ this.WriteRecord();
+ }
+ outputStream.Flush();
+ }
+
+ /// <summary>
+ /// Close the TarBuffer. If this is an output buffer, also flush the
+ /// current block before closing.
+ /// </summary>
+ public void Close()
+ {
+ if (outputStream != null)
+ {
+ Flush();
+
+ outputStream.Close();
+ outputStream = null;
+ }
+ else if (inputStream != null)
+ {
+ inputStream.Close();
+ inputStream = null;
+ }
+ }
+ }
+}
+
+/* The original Java file had this header:
+ *
+ ** Authored by Timothy Gerard Endres
+ ** <mailto:time@gjt.org> <http://www.trustice.com>
+ **
+ ** This work has been placed into the public domain.
+ ** You may use this work in any way and for any purpose you wish.
+ **
+ ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+ ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+ ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+ ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+ ** REDISTRIBUTION OF THIS SOFTWARE.
+ **
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarEntry.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
new file mode 100644
index 00000000000..d5a84e3d4bc
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
@@ -0,0 +1,539 @@
+// TarEntry.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar
+{
+
+ /// <summary>
+ /// This class represents an entry in a Tar archive. It consists
+ /// of the entry's header, as well as the entry's File. Entries
+ /// can be instantiated in one of three ways, depending on how
+ /// they are to be used.
+ /// <p>
+ /// TarEntries that are created from the header bytes read from
+ /// an archive are instantiated with the TarEntry( byte[] )
+ /// constructor. These entries will be used when extracting from
+ /// or listing the contents of an archive. These entries have their
+ /// header filled in using the header bytes. They also set the File
+ /// to null, since they reference an archive entry not a file.</p>
+ /// <p>
+ /// TarEntries that are created from files that are to be written
+ /// into an archive are instantiated with the CreateEntryFromFile(string)
+ /// pseudo constructor. These entries have their header filled in using
+ /// the File's information. They also keep a reference to the File
+ /// for convenience when writing entries.</p>
+ /// <p>
+ /// Finally, TarEntries can be constructed from nothing but a name.
+ /// This allows the programmer to construct the entry by hand, for
+ /// instance when only an InputStream is available for writing to
+ /// the archive, and the header information is constructed from
+ /// other information. In this case the header fields are set to
+ /// defaults and the File is set to null.</p>
+ ///
+ /// <see cref="TarHeader"/>
+ /// </summary>
+ public class TarEntry : ICloneable
+ {
+ /// <summary>
+ /// The name of the file this entry represents or null if the entry is not based on a file.
+ /// </summary>
+ string file;
+
+ /// <summary>
+ /// The entry's header information.
+ /// </summary>
+ TarHeader header;
+
+ /// <summary>
+ /// Only allow creation of Entries with the static CreateXYZ factory methods.
+ /// </summary>
+ private TarEntry()
+ {
+ }
+
+ /// <summary>
+ /// Construct an entry from an archive's header bytes. File is set
+ /// to null.
+ /// </summary>
+ /// <param name = "headerBuf">
+ /// The header bytes from a tar archive entry.
+ /// </param>
+ public TarEntry(byte[] headerBuf)
+ {
+ this.Initialize();
+ this.header.ParseBuffer(headerBuf);
+ }
+
+
+ /// <summary>
+ /// Construct a TarEntry using the <paramref name="header">header</paramref> provided
+ /// </summary>
+ /// <param name="header">Header details for entry</param>
+ public TarEntry(TarHeader header)
+ {
+ file = null;
+ this.header = header;
+ }
+
+ /// <summary>
+ /// Clone this tar entry.
+ /// </summary>
+ /// <returns>Returns a clone of this entry.</returns>
+ public object Clone()
+ {
+ TarEntry entry = new TarEntry();
+ entry.file = this.file;
+ entry.header = (TarHeader)this.header.Clone();
+ entry.Name = this.Name;
+ return entry;
+ }
+
+ /// <summary>
+ /// Construct an entry with only a <paramref name="name"></paramref>.
+ /// This allows the programmer to construct the entry's header "by hand".
+ /// </summary>
+ public static TarEntry CreateTarEntry(string name)
+ {
+ TarEntry entry = new TarEntry();
+ entry.Initialize();
+ entry.NameTarHeader(entry.header, name);
+ return entry;
+ }
+
+ /// <summary>
+ /// Construct an entry for a file. File is set to file, and the
+ /// header is constructed from information from the file.
+ /// </summary>
+ /// <param name = "fileName">
+ /// The file that the entry represents.
+ /// </param>
+ public static TarEntry CreateEntryFromFile(string fileName)
+ {
+ TarEntry entry = new TarEntry();
+ entry.Initialize();
+ entry.GetFileTarHeader(entry.header, fileName);
+ return entry;
+ }
+
+ /// <summary>
+ /// Initialization code common to all pseudo constructors.
+ /// </summary>
+ void Initialize()
+ {
+ this.file = null;
+ this.header = new TarHeader();
+ }
+
+ /// <summary>
+ /// Determine if the two entries are equal. Equality is determined
+ /// by the header names being equal.
+ /// </summary>
+ /// <returns>
+ /// True if the entries are equal.
+ /// </returns>
+ public override bool Equals(object it)
+ {
+ if (!(it is TarEntry))
+ {
+ return false;
+ }
+ return this.Name.Equals(((TarEntry)it).Name);
+ }
+
+ /// <summary>
+ /// Must be overridden when you override Equals.
+ /// </summary>
+ public override int GetHashCode()
+ {
+ return Name.GetHashCode();
+ }
+
+
+ /// <summary>
+ /// Determine if the given entry is a descendant of this entry.
+ /// Descendancy is determined by the name of the descendant
+ /// starting with this entry's name.
+ /// </summary>
+ /// <param name = "desc">
+ /// Entry to be checked as a descendent of this.
+ /// </param>
+ /// <returns>
+ /// True if entry is a descendant of this.
+ /// </returns>
+ public bool IsDescendent(TarEntry desc)
+ {
+ return desc.Name.StartsWith(Name);
+ }
+
+ /// <summary>
+ /// Get this entry's header.
+ /// </summary>
+ /// <returns>
+ /// This entry's TarHeader.
+ /// </returns>
+ public TarHeader TarHeader
+ {
+ get {
+ return this.header;
+ }
+ }
+
+ /// <summary>
+ /// Get/Set this entry's name.
+ /// </summary>
+ public string Name
+ {
+ get {
+ return header.Name;
+ }
+ set {
+ header.Name = value;
+ }
+ }
+
+ /// <summary>
+ /// Get/set this entry's user id.
+ /// </summary>
+ public int UserId
+ {
+ get {
+ return header.UserId;
+ }
+ set {
+ header.UserId = value;
+ }
+ }
+
+ /// <summary>
+ /// Get/set this entry's group id.
+ /// </summary>
+ public int GroupId
+ {
+ get {
+ return this.header.GroupId;
+ }
+ set {
+ this.header.GroupId = value;
+ }
+ }
+
+ /// <summary>
+ /// Get/set this entry's user name.
+ /// </summary>
+ public string UserName
+ {
+ get {
+ return this.header.UserName;
+ }
+ set {
+ this.header.UserName = value;
+ }
+ }
+
+ /// <summary>
+ /// Get/set this entry's group name.
+ /// </summary>
+ public string GroupName
+ {
+ get {
+ return this.header.GroupName;
+ }
+ set {
+ this.header.GroupName = value;
+ }
+ }
+
+ /// <summary>
+ /// Convenience method to set this entry's group and user ids.
+ /// </summary>
+ /// <param name="userId">
+ /// This entry's new user id.
+ /// </param>
+ /// <param name="groupId">
+ /// This entry's new group id.
+ /// </param>
+ public void SetIds(int userId, int groupId)
+ {
+ UserId = userId;
+ GroupId = groupId;
+ }
+
+ /// <summary>
+ /// Convenience method to set this entry's group and user names.
+ /// </summary>
+ /// <param name="userName">
+ /// This entry's new user name.
+ /// </param>
+ /// <param name="groupName">
+ /// This entry's new group name.
+ /// </param>
+ public void SetNames(string userName, string groupName)
+ {
+ UserName = userName;
+ GroupName = groupName;
+ }
+
+ /// <summary>
+ /// Get/Set the modification time for this entry
+ /// </summary>
+ public DateTime ModTime {
+ get {
+ return this.header.ModTime;
+ }
+ set {
+ this.header.ModTime = value;
+ }
+ }
+
+ /// <summary>
+ /// Get this entry's file.
+ /// </summary>
+ /// <returns>
+ /// This entry's file.
+ /// </returns>
+ public string File {
+ get {
+ return this.file;
+ }
+ }
+
+ /// <summary>
+ /// Get/set this entry's recorded file size.
+ /// </summary>
+ public long Size {
+ get {
+ return this.header.Size;
+ }
+ set {
+ this.header.Size = value;
+ }
+ }
+
+ /// <summary>
+ /// Convenience method that will modify an entry's name directly
+ /// in place in an entry header buffer byte array.
+ /// </summary>
+ /// <param name="outbuf">
+ /// The buffer containing the entry header to modify.
+ /// </param>
+ /// <param name="newName">
+ /// The new name to place into the header buffer.
+ /// </param>
+ public void AdjustEntryName(byte[] outbuf, string newName)
+ {
+ int offset = 0;
+ TarHeader.GetNameBytes(newName, outbuf, offset, TarHeader.NAMELEN);
+ }
+
+ /// <summary>
+ /// Return true if this entry represents a directory, false otherwise
+ /// </summary>
+ /// <returns>
+ /// True if this entry is a directory.
+ /// </returns>
+ public bool IsDirectory {
+ get {
+ if (this.file != null) {
+ return Directory.Exists(file);
+ }
+
+ if (this.header != null) {
+ if (this.header.TypeFlag == TarHeader.LF_DIR || Name.EndsWith( "/" )) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Fill in a TarHeader with information from a File.
+ /// </summary>
+ /// <param name="hdr">
+ /// The TarHeader to fill in.
+ /// </param>
+ /// <param name="file">
+ /// The file from which to get the header information.
+ /// </param>
+ public void GetFileTarHeader(TarHeader hdr, string file)
+ {
+ this.file = file;
+
+ // bugfix from torhovl from #D forum:
+ string name = file;
+
+#if !COMPACT_FRAMEWORK
+ // 23-Jan-2004 GnuTar allows device names in path where the name is not local to the current directory
+ if (name.IndexOf(Environment.CurrentDirectory) == 0) {
+ name = name.Substring(Environment.CurrentDirectory.Length);
+ }
+#endif
+
+/*
+ if (Path.DirectorySeparatorChar == '\\')
+ { // check if the OS is Windows
+ // Strip off drive letters!
+ if (name.Length > 2)
+ {
+ char ch1 = name[0];
+ char ch2 = name[1];
+
+ if (ch2 == ':' && Char.IsLetter(ch1))
+ {
+ name = name.Substring(2);
+ }
+ }
+ }
+*/
+
+ name = name.Replace(Path.DirectorySeparatorChar, '/');
+
+ // No absolute pathnames
+ // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
+ // so we loop on starting /'s.
+ while (name.StartsWith("/")) {
+ name = name.Substring(1);
+ }
+
+ hdr.LinkName = String.Empty;
+ hdr.Name = name;
+
+ if (Directory.Exists(file)) {
+ hdr.Mode = 1003; // Magic number for security access for a UNIX filesystem
+ hdr.TypeFlag = TarHeader.LF_DIR;
+ if (hdr.Name.Length == 0 || hdr.Name[hdr.Name.Length - 1] != '/') {
+ hdr.Name = hdr.Name + "/";
+ }
+
+ hdr.Size = 0;
+ } else {
+ hdr.Mode = 33216; // Magic number for security access for a UNIX filesystem
+ hdr.TypeFlag = TarHeader.LF_NORMAL;
+ hdr.Size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
+ }
+
+ hdr.ModTime = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)).ToUniversalTime();
+ hdr.DevMajor = 0;
+ hdr.DevMinor = 0;
+ }
+
+ /// <summary>
+ /// Get entries for all files present in this entries directory.
+ /// If this entry doesnt represent a directory zero entries are returned.
+ /// </summary>
+ /// <returns>
+ /// An array of TarEntry's for this entry's children.
+ /// </returns>
+ public TarEntry[] GetDirectoryEntries()
+ {
+ if (this.file == null || !Directory.Exists(this.file)) {
+ return new TarEntry[0];
+ }
+
+ string[] list = Directory.GetFileSystemEntries(this.file);
+ TarEntry[] result = new TarEntry[list.Length];
+
+ for (int i = 0; i < list.Length; ++i) {
+ result[i] = TarEntry.CreateEntryFromFile(list[i]);
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Write an entry's header information to a header buffer.
+ /// </summary>
+ /// <param name = "outbuf">
+ /// The tar entry header buffer to fill in.
+ /// </param>
+ public void WriteEntryHeader(byte[] outbuf)
+ {
+ this.header.WriteHeader(outbuf);
+ }
+
+ /// <summary>
+ /// Fill in a TarHeader given only the entry's name.
+ /// </summary>
+ /// <param name="hdr">
+ /// The TarHeader to fill in.
+ /// </param>
+ /// <param name="name">
+ /// The tar entry name.
+ /// </param>
+ public void NameTarHeader(TarHeader hdr, string name)
+ {
+ bool isDir = name.EndsWith("/");
+
+ hdr.Name = name;
+ hdr.Mode = isDir ? 1003 : 33216;
+ hdr.UserId = 0;
+ hdr.GroupId = 0;
+ hdr.Size = 0;
+
+ hdr.ModTime = DateTime.UtcNow;
+
+ hdr.TypeFlag = isDir ? TarHeader.LF_DIR : TarHeader.LF_NORMAL;
+
+ hdr.LinkName = String.Empty;
+ hdr.UserName = String.Empty;
+ hdr.GroupName = String.Empty;
+
+ hdr.DevMajor = 0;
+ hdr.DevMinor = 0;
+ }
+ }
+}
+
+
+
+/* The original Java file had this header:
+ *
+ ** Authored by Timothy Gerard Endres
+ ** <mailto:time@gjt.org> <http://www.trustice.com>
+ **
+ ** This work has been placed into the public domain.
+ ** You may use this work in any way and for any purpose you wish.
+ **
+ ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+ ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+ ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+ ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+ ** REDISTRIBUTION OF THIS SOFTWARE.
+ **
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarException.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarException.cs
new file mode 100644
index 00000000000..ab07726f458
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarException.cs
@@ -0,0 +1,60 @@
+// TarException.cs
+//
+// Copyright 2004 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using ICSharpCode.SharpZipLib;
+
+namespace ICSharpCode.SharpZipLib.Tar {
+
+ /// <summary>
+ /// TarExceptions are used for exceptions specific to tar classes and code.
+ /// </summary>
+ public class TarException : SharpZipBaseException
+ {
+ /// <summary>
+ /// Initialises a new instance of the TarException class.
+ /// </summary>
+ public TarException()
+ {
+ }
+
+ /// <summary>
+ /// Initialises a new instance of the TarException class with a specified message.
+ /// </summary>
+ /// <param name="message">The message that describes the error.</param>
+ public TarException(string message) : base(message)
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarHeader.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarHeader.cs
new file mode 100644
index 00000000000..eeedcd0ded3
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarHeader.cs
@@ -0,0 +1,1090 @@
+// TarHeader.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+/* The tar format and its POSIX successor PAX have a long history which makes for compatability
+ issues when creating and reading files.
+
+ This is further complicated by a large number of programs with variations on formats
+ One common issue is the handling of names longer than 100 characters.
+ GNU style long names are currently supported.
+
+This is the ustar (Posix 1003.1) header.
+
+struct header
+{
+ char t_name[100]; // 0 Filename
+ char t_mode[8]; // 100 Permissions
+ char t_uid[8]; // 108 Numerical User ID
+ char t_gid[8]; // 116 Numerical Group ID
+ char t_size[12]; // 124 Filesize
+ char t_mtime[12]; // 136 st_mtime
+ char t_chksum[8]; // 148 Checksum
+ char t_typeflag; // 156 Type of File
+ char t_linkname[100]; // 157 Target of Links
+ char t_magic[6]; // 257 "ustar" or other...
+ char t_version[2]; // 263 Version fixed to 00
+ char t_uname[32]; // 265 User Name
+ char t_gname[32]; // 297 Group Name
+ char t_devmajor[8]; // 329 Major for devices
+ char t_devminor[8]; // 337 Minor for devices
+ char t_prefix[155]; // 345 Prefix for t_name
+ char t_mfill[12]; // 500 Filler up to 512
+};
+
+*/
+
+using System;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar
+{
+
+
+ /// <summary>
+ /// This class encapsulates the Tar Entry Header used in Tar Archives.
+ /// The class also holds a number of tar constants, used mostly in headers.
+ /// </summary>
+ public class TarHeader : ICloneable
+ {
+ /// <summary>
+ /// The length of the name field in a header buffer.
+ /// </summary>
+ public readonly static int NAMELEN = 100;
+
+ /// <summary>
+ /// The length of the mode field in a header buffer.
+ /// </summary>
+ public readonly static int MODELEN = 8;
+
+ /// <summary>
+ /// The length of the user id field in a header buffer.
+ /// </summary>
+ public readonly static int UIDLEN = 8;
+
+ /// <summary>
+ /// The length of the group id field in a header buffer.
+ /// </summary>
+ public readonly static int GIDLEN = 8;
+
+ /// <summary>
+ /// The length of the checksum field in a header buffer.
+ /// </summary>
+ public readonly static int CHKSUMLEN = 8;
+
+ /// <summary>
+ /// Offset of checksum in a header buffer.
+ /// </summary>
+ public const int CHKSUMOFS = 148;
+
+ /// <summary>
+ /// The length of the size field in a header buffer.
+ /// </summary>
+ public readonly static int SIZELEN = 12;
+
+ /// <summary>
+ /// The length of the magic field in a header buffer.
+ /// </summary>
+ public readonly static int MAGICLEN = 6;
+
+ /// <summary>
+ /// The length of the version field in a header buffer.
+ /// </summary>
+ public readonly static int VERSIONLEN = 2;
+
+ /// <summary>
+ /// The length of the modification time field in a header buffer.
+ /// </summary>
+ public readonly static int MODTIMELEN = 12;
+
+ /// <summary>
+ /// The length of the user name field in a header buffer.
+ /// </summary>
+ public readonly static int UNAMELEN = 32;
+
+ /// <summary>
+ /// The length of the group name field in a header buffer.
+ /// </summary>
+ public readonly static int GNAMELEN = 32;
+
+ /// <summary>
+ /// The length of the devices field in a header buffer.
+ /// </summary>
+ public readonly static int DEVLEN = 8;
+
+ //
+ // LF_ constants represent the "type" of an entry
+ //
+
+ /// <summary>
+ /// The "old way" of indicating a normal file.
+ /// </summary>
+ public const byte LF_OLDNORM = 0;
+
+ /// <summary>
+ /// Normal file type.
+ /// </summary>
+ public const byte LF_NORMAL = (byte) '0';
+
+ /// <summary>
+ /// Link file type.
+ /// </summary>
+ public const byte LF_LINK = (byte) '1';
+
+ /// <summary>
+ /// Symbolic link file type.
+ /// </summary>
+ public const byte LF_SYMLINK = (byte) '2';
+
+ /// <summary>
+ /// Character device file type.
+ /// </summary>
+ public const byte LF_CHR = (byte) '3';
+
+ /// <summary>
+ /// Block device file type.
+ /// </summary>
+ public const byte LF_BLK = (byte) '4';
+
+ /// <summary>
+ /// Directory file type.
+ /// </summary>
+ public const byte LF_DIR = (byte) '5';
+
+ /// <summary>
+ /// FIFO (pipe) file type.
+ /// </summary>
+ public const byte LF_FIFO = (byte) '6';
+
+ /// <summary>
+ /// Contiguous file type.
+ /// </summary>
+ public const byte LF_CONTIG = (byte) '7';
+
+ /// <summary>
+ /// Posix.1 2001 global extended header
+ /// </summary>
+ public const byte LF_GHDR = (byte) 'g';
+
+ /// <summary>
+ /// Posix.1 2001 extended header
+ /// </summary>
+ public readonly static byte LF_XHDR = (byte) 'x';
+
+
+
+
+ // POSIX allows for upper case ascii type as extensions
+
+ /// <summary>
+ /// Solaris access control list file type
+ /// </summary>
+ public const byte LF_ACL = (byte) 'A';
+
+ /// <summary>
+ /// GNU dir dump file type
+ /// This is a dir entry that contains the names of files that were in the
+ /// dir at the time the dump was made
+ /// </summary>
+ public const byte LF_GNU_DUMPDIR = (byte) 'D';
+
+ /// <summary>
+ /// Solaris Extended Attribute File
+ /// </summary>
+ public const byte LF_EXTATTR = (byte) 'E' ;
+
+ /// <summary>
+ /// Inode (metadata only) no file content
+ /// </summary>
+ public const byte LF_META = (byte) 'I';
+
+ /// <summary>
+ /// Identifies the next file on the tape as having a long link name
+ /// </summary>
+ public const byte LF_GNU_LONGLINK = (byte) 'K';
+
+ /// <summary>
+ /// Identifies the next file on the tape as having a long name
+ /// </summary>
+ public const byte LF_GNU_LONGNAME = (byte) 'L';
+
+ /// <summary>
+ /// Continuation of a file that began on another volume
+ /// </summary>
+ public const byte LF_GNU_MULTIVOL = (byte) 'M';
+
+ /// <summary>
+ /// For storing filenames that dont fit in the main header (old GNU)
+ /// </summary>
+ public const byte LF_GNU_NAMES = (byte) 'N';
+
+ /// <summary>
+ /// GNU Sparse file
+ /// </summary>
+ public const byte LF_GNU_SPARSE = (byte) 'S';
+
+ /// <summary>
+ /// GNU Tape/volume header ignore on extraction
+ /// </summary>
+ public const byte LF_GNU_VOLHDR = (byte) 'V';
+
+ /// <summary>
+ /// The magic tag representing a POSIX tar archive. (includes trailing NULL)
+ /// </summary>
+ public readonly static string TMAGIC = "ustar ";
+
+ /// <summary>
+ /// The magic tag representing an old GNU tar archive where version is included in magic and overwrites it
+ /// </summary>
+ public readonly static string GNU_TMAGIC = "ustar ";
+
+
+ string name;
+
+ /// <summary>
+ /// Get/set the name for this tar entry.
+ /// </summary>
+ /// <exception cref="ArgumentNullException">Thrown when attempting to set the property to null.</exception>
+ public string Name
+ {
+ get { return name; }
+ set {
+ if ( value == null ) {
+ throw new ArgumentNullException();
+ }
+ name = value;
+ }
+ }
+
+ int mode;
+
+ /// <summary>
+ /// Get/set the entry's Unix style permission mode.
+ /// </summary>
+ public int Mode
+ {
+ get { return mode; }
+ set { mode = value; }
+ }
+
+ int userId;
+
+ /// <summary>
+ /// The entry's user id.
+ /// </summary>
+ /// <remarks>
+ /// This is only directly relevant to unix systems.
+ /// The default is zero.
+ /// </remarks>
+ public int UserId
+ {
+ get { return userId; }
+ set { userId = value; }
+ }
+
+ int groupId;
+
+ /// <summary>
+ /// Get/set the entry's group id.
+ /// </summary>
+ /// <remarks>
+ /// This is only directly relevant to linux/unix systems.
+ /// The default value is zero.
+ /// </remarks>
+ public int GroupId
+ {
+ get { return groupId; }
+ set { groupId = value; }
+ }
+
+
+ long size;
+
+ /// <summary>
+ /// Get/set the entry's size.
+ /// </summary>
+ /// <exception cref="ArgumentOutOfRangeException">Thrown when setting the size to less than zero.</exception>
+ public long Size
+ {
+ get { return size; }
+ set {
+ if ( value < 0 ) {
+ throw new ArgumentOutOfRangeException();
+ }
+ size = value;
+ }
+ }
+
+ DateTime modTime;
+
+ /// <summary>
+ /// Get/set the entry's modification time.
+ /// </summary>
+ /// <remarks>
+ /// The modification time is only accurate to within a second.
+ /// </remarks>
+ /// <exception cref="ArgumentOutOfRangeException">Thrown when setting the date time to less than 1/1/1970.</exception>
+ public DateTime ModTime
+ {
+ get { return modTime; }
+ set {
+ if ( value < dateTime1970 )
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+ modTime = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second);
+ }
+ }
+
+ int checksum;
+
+ /// <summary>
+ /// Get the entry's checksum. This is only valid/updated after writing or reading an entry.
+ /// </summary>
+ public int Checksum
+ {
+ get { return checksum; }
+ }
+
+ bool isChecksumValid;
+
+ /// <summary>
+ /// Get value of true if the header checksum is valid, false otherwise.
+ /// </summary>
+ public bool IsChecksumValid
+ {
+ get { return isChecksumValid; }
+ }
+
+ byte typeFlag;
+
+ /// <summary>
+ /// Get/set the entry's type flag.
+ /// </summary>
+ public byte TypeFlag
+ {
+ get { return typeFlag; }
+ set { typeFlag = value; }
+ }
+
+ string linkName;
+
+ /// <summary>
+ /// The entry's link name.
+ /// </summary>
+ /// <exception cref="ArgumentNullException">Thrown when attempting to set LinkName to null.</exception>
+ public string LinkName
+ {
+ get { return linkName; }
+ set {
+ if ( value == null ) {
+ throw new ArgumentNullException();
+ }
+ linkName = value;
+ }
+ }
+
+ string magic;
+
+ /// <summary>
+ /// Get/set the entry's magic tag.
+ /// </summary>
+ /// <exception cref="ArgumentNullException">Thrown when attempting to set Magic to null.</exception>
+ public string Magic
+ {
+ get { return magic; }
+ set {
+ if ( value == null ) {
+ throw new ArgumentNullException();
+ }
+ magic = value;
+ }
+ }
+
+ string version;
+
+ /// <summary>
+ /// The entry's version.
+ /// </summary>
+ /// <exception cref="ArgumentNullException">Thrown when attempting to set Version to null.</exception>
+ public string Version
+ {
+ get { return version; }
+ set {
+ if ( value == null ) {
+ throw new ArgumentNullException();
+ }
+ version = value;
+ }
+ }
+
+ string userName;
+
+ /// <summary>
+ /// The entry's user name.
+ /// </summary>
+ /// <remarks>
+ /// See <see cref="ResetValueDefaults">ResetValueDefaults</see>
+ /// for detail on how this value is derived.
+ /// </remarks>
+ public string UserName
+ {
+ get { return userName; }
+ set {
+ if (value != null) {
+ userName = value.Substring(0, Math.Min(UNAMELEN, value.Length));
+ }
+ else {
+#if COMPACT_FRAMEWORK
+ string currentUser = "PocketPC";
+#else
+ string currentUser = Environment.UserName;
+#endif
+ if (currentUser.Length > UNAMELEN) {
+ currentUser = currentUser.Substring(0, UNAMELEN);
+ }
+ userName = currentUser;
+ }
+ }
+ }
+
+ string groupName;
+
+ /// <summary>
+ /// Get/set the entry's group name.
+ /// </summary>
+ /// <remarks>
+ /// This is only directly relevant to unix systems.
+ /// </remarks>
+ public string GroupName
+ {
+ get { return groupName; }
+ set {
+ if ( value == null ) {
+ groupName = "None";
+ }
+ else {
+ groupName = value;
+ }
+ }
+ }
+
+ int devMajor;
+
+ /// <summary>
+ /// Get/set the entry's major device number.
+ /// </summary>
+ public int DevMajor
+ {
+ get { return devMajor; }
+ set { devMajor = value; }
+ }
+
+ int devMinor;
+
+ /// <summary>
+ /// Get/set the entry's minor device number.
+ /// </summary>
+ public int DevMinor
+ {
+ get { return devMinor; }
+ set { devMinor = value; }
+ }
+
+ /// <summary>
+ /// Initialise a default TarHeader instance
+ /// </summary>
+ public TarHeader()
+ {
+ this.Magic = TarHeader.TMAGIC;
+ this.Version = " ";
+
+ this.Name = "";
+ this.LinkName = "";
+
+ this.UserId = defaultUserId;
+ this.GroupId = defaultGroupId;
+ this.UserName = defaultUser;
+ this.GroupName = defaultGroupName;
+ this.Size = 0;
+ }
+
+ // Values used during recursive operations.
+ static internal int userIdAsSet = 0;
+ static internal int groupIdAsSet = 0;
+ static internal string userNameAsSet = null;
+ static internal string groupNameAsSet = "None";
+
+ static internal int defaultUserId = 0;
+ static internal int defaultGroupId = 0;
+ static internal string defaultGroupName = "None";
+ static internal string defaultUser = null;
+
+ static internal void RestoreSetValues()
+ {
+ defaultUserId = userIdAsSet;
+ defaultUser = userNameAsSet;
+ defaultGroupId = groupIdAsSet;
+ defaultGroupName = groupNameAsSet;
+ }
+
+ /// <summary>
+ /// Set defaults for values used when constructing a TarHeader instance.
+ /// </summary>
+ /// <param name="userId">Value to apply as a default for userId.</param>
+ /// <param name="userName">Value to apply as a default for userName.</param>
+ /// <param name="groupId">Value to apply as a default for groupId.</param>
+ /// <param name="groupName">Value to apply as a default for groupName.</param>
+ static public void SetValueDefaults(int userId, string userName, int groupId, string groupName)
+ {
+ defaultUserId = userIdAsSet = userId;
+ defaultUser = userNameAsSet = userName;
+ defaultGroupId = groupIdAsSet = groupId;
+ defaultGroupName = groupNameAsSet = groupName;
+ }
+
+ static internal void SetActiveDefaults(int userId, string userName, int groupId, string groupName)
+ {
+ defaultUserId = userId;
+ defaultUser = userName;
+ defaultGroupId = groupId;
+ defaultGroupName = groupName;
+ }
+
+ /// <summary>
+ /// Reset value defaults to initial values.
+ /// </summary>
+ /// <remarks>
+ /// The default values are user id=0, group id=0, groupname="None", user name=null.
+ /// When the default user name is null the value from Environment.UserName is used. Or "PocketPC" for the Compact framework.
+ /// When the default group name is null the value "None" is used.
+ /// </remarks>
+ static public void ResetValueDefaults()
+ {
+ defaultUserId = 0;
+ defaultGroupId = 0;
+ defaultGroupName = "None";
+ defaultUser = null;
+ }
+
+ /// <summary>
+ /// Clone a TAR header.
+ /// </summary>
+ public object Clone()
+ {
+ TarHeader hdr = new TarHeader();
+
+ hdr.Name = Name;
+ hdr.Mode = this.Mode;
+ hdr.UserId = this.UserId;
+ hdr.GroupId = this.GroupId;
+ hdr.Size = this.Size;
+ hdr.ModTime = this.ModTime;
+ hdr.TypeFlag = this.TypeFlag;
+ hdr.LinkName = this.LinkName;
+ hdr.Magic = this.Magic;
+ hdr.Version = this.Version;
+ hdr.UserName = this.UserName;
+ hdr.GroupName = this.GroupName;
+ hdr.DevMajor = this.DevMajor;
+ hdr.DevMinor = this.DevMinor;
+
+ return hdr;
+ }
+ /// <summary>
+ /// Get a hash code for the current object.
+ /// </summary>
+ /// <returns>A hash code for the current object.</returns>
+ public override int GetHashCode()
+ {
+ return Name.GetHashCode();
+ }
+
+ /// <summary>
+ /// Determines if this instance is equal to the specified object.
+ /// </summary>
+ /// <param name="obj">The object to compare with.</param>
+ /// <returns>true if the objects are equal, false otherwise.</returns>
+ public override bool Equals(object obj)
+ {
+ if ( obj is TarHeader ) {
+ TarHeader th = obj as TarHeader;
+ return name == th.name
+ && mode == th.mode
+ && UserId == th.UserId
+ && GroupId == th.GroupId
+ && Size == th.Size
+ && ModTime == th.ModTime
+ && Checksum == th.Checksum
+ && TypeFlag == th.TypeFlag
+ && LinkName == th.LinkName
+ && Magic == th.Magic
+ && Version == th.Version
+ && UserName == th.UserName
+ && GroupName == th.GroupName
+ && DevMajor == th.DevMajor
+ && DevMinor == th.DevMinor;
+ }
+ else {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Get the name of this entry.
+ /// </summary>
+ /// <returns>The entry's name.</returns>
+ /// <remarks>
+ /// This is obsolete use the Name property instead.
+ /// </remarks>
+ [Obsolete]
+ public string GetName()
+ {
+ return this.name.ToString();
+ }
+
+ /// <summary>
+ /// Parse an octal string from a header buffer.
+ /// </summary>
+ /// <param name = "header">The header buffer from which to parse.</param>
+ /// <param name = "offset">The offset into the buffer from which to parse.</param>
+ /// <param name = "length">The number of header bytes to parse.</param>
+ /// <returns>The long equivalent of the octal string.</returns>
+ public static long ParseOctal(byte[] header, int offset, int length)
+ {
+ long result = 0;
+ bool stillPadding = true;
+
+ int end = offset + length;
+ for (int i = offset; i < end ; ++i) {
+ if (header[i] == 0) {
+ break;
+ }
+
+ if (header[i] == (byte)' ' || header[i] == '0') {
+ if (stillPadding) {
+ continue;
+ }
+
+ if (header[i] == (byte)' ') {
+ break;
+ }
+ }
+
+ stillPadding = false;
+
+ result = (result << 3) + (header[i] - '0');
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Parse a name from a header buffer.
+ /// </summary>
+ /// <param name="header">
+ /// The header buffer from which to parse.
+ /// </param>
+ /// <param name="offset">
+ /// The offset into the buffer from which to parse.
+ /// </param>
+ /// <param name="length">
+ /// The number of header bytes to parse.
+ /// </param>
+ /// <returns>
+ /// The name parsed.
+ /// </returns>
+ public static StringBuilder ParseName(byte[] header, int offset, int length)
+ {
+ StringBuilder result = new StringBuilder(length);
+
+ for (int i = offset; i < offset + length; ++i) {
+ if (header[i] == 0) {
+ break;
+ }
+ result.Append((char)header[i]);
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Add <paramref name="name">name</paramref> to the buffer as a collection of bytes
+ /// </summary>
+ /// <param name="name">The name to add</param>
+ /// <param name="nameOffset">The offset of the first character</param>
+ /// <param name="buf">The buffer to add to</param>
+ /// <param name="bufferOffset">The index of the first byte to add</param>
+ /// <param name="length">The number of characters/bytes to add</param>
+ /// <returns>The next free index in the <paramref name="buf">buffer</paramref></returns>
+ public static int GetNameBytes(StringBuilder name, int nameOffset, byte[] buf, int bufferOffset, int length)
+ {
+ return GetNameBytes(name.ToString(), nameOffset, buf, bufferOffset, length);
+ }
+
+ /// <summary>
+ /// Add <paramref name="name">name</paramref> to the buffer as a collection of bytes
+ /// </summary>
+ /// <param name="name">The name to add</param>
+ /// <param name="nameOffset">The offset of the first character</param>
+ /// <param name="buf">The buffer to add to</param>
+ /// <param name="bufferOffset">The index of the first byte to add</param>
+ /// <param name="length">The number of characters/bytes to add</param>
+ /// <returns>The next free index in the <paramref name="buf">buffer</paramref></returns>
+ public static int GetNameBytes(string name, int nameOffset, byte[] buf, int bufferOffset, int length)
+ {
+ int i;
+
+ for (i = 0 ; i < length - 1 && nameOffset + i < name.Length; ++i) {
+ buf[bufferOffset + i] = (byte)name[nameOffset + i];
+ }
+
+ for (; i < length ; ++i) {
+ buf[bufferOffset + i] = 0;
+ }
+
+ return bufferOffset + length;
+ }
+
+ /// <summary>
+ /// Add an entry name to the buffer
+ /// </summary>
+ /// <param name="name">
+ /// The name to add
+ /// </param>
+ /// <param name="buf">
+ /// The buffer to add to
+ /// </param>
+ /// <param name="offset">
+ /// The offset into the buffer from which to start adding
+ /// </param>
+ /// <param name="length">
+ /// The number of header bytes to add
+ /// </param>
+ /// <returns>
+ /// The index of the next free byte in the buffer
+ /// </returns>
+ public static int GetNameBytes(StringBuilder name, byte[] buf, int offset, int length)
+ {
+ return GetNameBytes(name.ToString(), 0, buf, offset, length);
+ }
+
+ /// <summary>
+ /// Add an entry name to the buffer
+ /// </summary>
+ /// <param name="name">The name to add</param>
+ /// <param name="buf">The buffer to add to</param>
+ /// <param name="offset">The offset into the buffer from which to start adding</param>
+ /// <param name="length">The number of header bytes to add</param>
+ /// <returns>The index of the next free byte in the buffer</returns>
+ public static int GetNameBytes(string name, byte[] buf, int offset, int length)
+ {
+ return GetNameBytes(name, 0, buf, offset, length);
+ }
+
+ /// <summary>
+ /// Add a string to a buffer as a collection of ascii bytes.
+ /// </summary>
+ /// <param name="toAdd">The string to add</param>
+ /// <param name="nameOffset">The offset of the first character to add.</param>
+ /// <param name="buffer">The buffer to add to.</param>
+ /// <param name="bufferOffset">The offset to start adding at.</param>
+ /// <param name="length">The number of ascii characters to add.</param>
+ /// <returns>The next free index in the buffer.</returns>
+ public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int bufferOffset, int length )
+ {
+ for (int i = 0 ; i < length && nameOffset + i < toAdd.Length; ++i)
+ {
+ buffer[bufferOffset + i] = (byte)toAdd[nameOffset + i];
+ }
+ return bufferOffset + length;
+ }
+
+ /// <summary>
+ /// Put an octal representation of a value into a buffer
+ /// </summary>
+ /// <param name = "val">
+ /// the value to be converted to octal
+ /// </param>
+ /// <param name = "buf">
+ /// buffer to store the octal string
+ /// </param>
+ /// <param name = "offset">
+ /// The offset into the buffer where the value starts
+ /// </param>
+ /// <param name = "length">
+ /// The length of the octal string to create
+ /// </param>
+ /// <returns>
+ /// The offset of the character next byte after the octal string
+ /// </returns>
+ public static int GetOctalBytes(long val, byte[] buf, int offset, int length)
+ {
+ int idx = length - 1;
+
+ // Either a space or null is valid here. We use NULL as per GNUTar
+ buf[offset + idx] = 0;
+ --idx;
+
+ if (val > 0) {
+ for (long v = val; idx >= 0 && v > 0; --idx) {
+ buf[offset + idx] = (byte)((byte)'0' + (byte)(v & 7));
+ v >>= 3;
+ }
+ }
+
+ for (; idx >= 0; --idx) {
+ buf[offset + idx] = (byte)'0';
+ }
+
+ return offset + length;
+ }
+
+ /// <summary>
+ /// Put an octal representation of a value into a buffer
+ /// </summary>
+ /// <param name = "val">Value to be convert to octal</param>
+ /// <param name = "buf">The buffer to update</param>
+ /// <param name = "offset">The offset into the buffer to store the value</param>
+ /// <param name = "length">The length of the octal string</param>
+ /// <returns>Index of next byte</returns>
+ public static int GetLongOctalBytes(long val, byte[] buf, int offset, int length)
+ {
+ return GetOctalBytes(val, buf, offset, length);
+ }
+
+ /// <summary>
+ /// Add the checksum integer to header buffer.
+ /// </summary>
+ /// <param name = "val"></param>
+ /// <param name = "buf">The header buffer to set the checksum for</param>
+ /// <param name = "offset">The offset into the buffer for the checksum</param>
+ /// <param name = "length">The number of header bytes to update.
+ /// It's formatted differently from the other fields: it has 6 digits, a
+ /// null, then a space -- rather than digits, a space, then a null.
+ /// The final space is already there, from checksumming
+ /// </param>
+ /// <returns>The modified buffer offset</returns>
+ private static int GetCheckSumOctalBytes(long val, byte[] buf, int offset, int length)
+ {
+ TarHeader.GetOctalBytes(val, buf, offset, length - 1);
+ return offset + length;
+ }
+
+ /// <summary>
+ /// Compute the checksum for a tar entry header.
+ /// The checksum field must be all spaces prior to this happening
+ /// </summary>
+ /// <param name = "buf">The tar entry's header buffer.</param>
+ /// <returns>The computed checksum.</returns>
+ private static int ComputeCheckSum(byte[] buf)
+ {
+ int sum = 0;
+ for (int i = 0; i < buf.Length; ++i) {
+ sum += buf[i];
+ }
+ return sum;
+ }
+
+ /// <summary>
+ /// Make a checksum for a tar entry ignoring the checksum contents.
+ /// </summary>
+ /// <param name = "buf">The tar entry's header buffer.</param>
+ /// <returns>The checksum for the buffer</returns>
+ private static int MakeCheckSum(byte[] buf)
+ {
+ int sum = 0;
+ for ( int i = 0; i < CHKSUMOFS; ++i )
+ {
+ sum += buf[i];
+ }
+
+ for ( int i = 0; i < TarHeader.CHKSUMLEN; ++i)
+ {
+ sum += (byte)' ';
+ }
+
+ for (int i = CHKSUMOFS + CHKSUMLEN; i < buf.Length; ++i)
+ {
+ sum += buf[i];
+ }
+ return sum;
+ }
+
+
+ readonly static long timeConversionFactor = 10000000L; // 1 tick == 100 nanoseconds
+ readonly static DateTime dateTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
+
+ static int GetCTime(System.DateTime dateTime)
+ {
+ return (int)((dateTime.Ticks - dateTime1970.Ticks) / timeConversionFactor);
+ }
+
+ static DateTime GetDateTimeFromCTime(long ticks)
+ {
+ DateTime result;
+
+ try {
+ result = new DateTime(dateTime1970.Ticks + ticks * timeConversionFactor);
+ }
+ catch {
+ result = dateTime1970;
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Parse TarHeader information from a header buffer.
+ /// </summary>
+ /// <param name = "header">
+ /// The tar entry header buffer to get information from.
+ /// </param>
+ public void ParseBuffer(byte[] header)
+ {
+ int offset = 0;
+
+ name = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
+ offset += TarHeader.NAMELEN;
+
+ mode = (int)TarHeader.ParseOctal(header, offset, TarHeader.MODELEN);
+ offset += TarHeader.MODELEN;
+
+ UserId = (int)TarHeader.ParseOctal(header, offset, TarHeader.UIDLEN);
+ offset += TarHeader.UIDLEN;
+
+ GroupId = (int)TarHeader.ParseOctal(header, offset, TarHeader.GIDLEN);
+ offset += TarHeader.GIDLEN;
+
+ Size = TarHeader.ParseOctal(header, offset, TarHeader.SIZELEN);
+ offset += TarHeader.SIZELEN;
+
+ ModTime = GetDateTimeFromCTime(TarHeader.ParseOctal(header, offset, TarHeader.MODTIMELEN));
+ offset += TarHeader.MODTIMELEN;
+
+ checksum = (int)TarHeader.ParseOctal(header, offset, TarHeader.CHKSUMLEN);
+ offset += TarHeader.CHKSUMLEN;
+
+ TypeFlag = header[ offset++ ];
+
+ LinkName = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
+ offset += TarHeader.NAMELEN;
+
+ Magic = TarHeader.ParseName(header, offset, TarHeader.MAGICLEN).ToString();
+ offset += TarHeader.MAGICLEN;
+
+ Version = TarHeader.ParseName(header, offset, TarHeader.VERSIONLEN).ToString();
+ offset += TarHeader.VERSIONLEN;
+
+ UserName = TarHeader.ParseName(header, offset, TarHeader.UNAMELEN).ToString();
+ offset += TarHeader.UNAMELEN;
+
+ GroupName = TarHeader.ParseName(header, offset, TarHeader.GNAMELEN).ToString();
+ offset += TarHeader.GNAMELEN;
+
+ DevMajor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
+ offset += TarHeader.DEVLEN;
+
+ DevMinor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
+
+ // Fields past this point not currently parsed or used...
+
+ // TODO: prefix information.
+
+ isChecksumValid = Checksum == TarHeader.MakeCheckSum(header);
+ }
+
+ /// <summary>
+ /// 'Write' header information to buffer provided, updating the <see cref="Checksum">check sum</see>.
+ /// </summary>
+ /// <param name="outbuf">output buffer for header information</param>
+ public void WriteHeader(byte[] outbuf)
+ {
+ int offset = 0;
+
+ offset = GetNameBytes(this.Name, outbuf, offset, TarHeader.NAMELEN);
+ offset = GetOctalBytes(this.mode, outbuf, offset, TarHeader.MODELEN);
+ offset = GetOctalBytes(this.UserId, outbuf, offset, TarHeader.UIDLEN);
+ offset = GetOctalBytes(this.GroupId, outbuf, offset, TarHeader.GIDLEN);
+
+ long size = this.Size;
+
+ offset = GetLongOctalBytes(size, outbuf, offset, TarHeader.SIZELEN);
+ offset = GetLongOctalBytes(GetCTime(this.ModTime), outbuf, offset, TarHeader.MODTIMELEN);
+
+ int csOffset = offset;
+ for (int c = 0; c < TarHeader.CHKSUMLEN; ++c) {
+ outbuf[offset++] = (byte)' ';
+ }
+
+ outbuf[offset++] = this.TypeFlag;
+
+ offset = GetNameBytes(this.LinkName, outbuf, offset, NAMELEN);
+ offset = GetAsciiBytes(this.Magic, 0, outbuf, offset, MAGICLEN);
+ offset = GetNameBytes(this.Version, outbuf, offset, VERSIONLEN);
+ offset = GetNameBytes(this.UserName, outbuf, offset, UNAMELEN);
+ offset = GetNameBytes(this.GroupName, outbuf, offset, GNAMELEN);
+
+ if (this.TypeFlag == LF_CHR || this.TypeFlag == LF_BLK) {
+ offset = GetOctalBytes(this.DevMajor, outbuf, offset, DEVLEN);
+ offset = GetOctalBytes(this.DevMinor, outbuf, offset, DEVLEN);
+ }
+
+ for ( ; offset < outbuf.Length; ) {
+ outbuf[offset++] = 0;
+ }
+
+ checksum = ComputeCheckSum(outbuf);
+
+ GetCheckSumOctalBytes(checksum, outbuf, csOffset, CHKSUMLEN);
+ isChecksumValid = true;
+ }
+ }
+}
+
+/* The original Java file had this header:
+ *
+** Authored by Timothy Gerard Endres
+** <mailto:time@gjt.org> <http://www.trustice.com>
+**
+** This work has been placed into the public domain.
+** You may use this work in any way and for any purpose you wish.
+**
+** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+** REDISTRIBUTION OF THIS SOFTWARE.
+**
+*/
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs
new file mode 100644
index 00000000000..dcbc5189d27
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs
@@ -0,0 +1,636 @@
+// TarInputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar
+{
+
+ /// <summary>
+ /// The TarInputStream reads a UNIX tar archive as an InputStream.
+ /// methods are provided to position at each successive entry in
+ /// the archive, and the read each entry as a normal input stream
+ /// using read().
+ /// </summary>
+ public class TarInputStream : Stream
+ {
+ /// <summary>
+ /// Flag set when last block has been read
+ /// </summary>
+ protected bool hasHitEOF;
+
+ /// <summary>
+ /// Size of this entry as recorded in header
+ /// </summary>
+ protected long entrySize;
+
+ /// <summary>
+ /// Number of bytes read for this entry so far
+ /// </summary>
+ protected long entryOffset;
+
+ /// <summary>
+ /// Buffer used with calls to <code>Read()</code>
+ /// </summary>
+ protected byte[] readBuf;
+
+ /// <summary>
+ /// Working buffer
+ /// </summary>
+ protected TarBuffer buffer;
+
+ /// <summary>
+ /// Current entry being read
+ /// </summary>
+ protected TarEntry currEntry;
+
+ /// <summary>
+ /// Factory used to create TarEntry or descendant class instance
+ /// </summary>
+ protected IEntryFactory eFactory;
+
+ Stream inputStream;
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports reading
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return inputStream.CanRead;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports seeking
+ /// This property always returns false.
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating if the stream supports writing.
+ /// This property always returns false.
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// The length in bytes of the stream
+ /// </summary>
+ public override long Length {
+ get {
+ return inputStream.Length;
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the position within the stream.
+ /// Setting the Position is not supported and throws a NotSupportedExceptionNotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any attempt to set position</exception>
+ public override long Position {
+ get {
+ return inputStream.Position;
+ }
+ set {
+ throw new NotSupportedException("TarInputStream Seek not supported");
+ }
+ }
+
+ /// <summary>
+ /// Flushes the baseInputStream
+ /// </summary>
+ public override void Flush()
+ {
+ inputStream.Flush();
+ }
+
+ /// <summary>
+ /// Set the streams position. This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException("TarInputStream Seek not supported");
+ }
+
+ /// <summary>
+ /// Sets the length of the stream
+ /// This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void SetLength(long val)
+ {
+ throw new NotSupportedException("TarInputStream SetLength not supported");
+ }
+
+ /// <summary>
+ /// Writes a block of bytes to this stream using data from a buffer.
+ /// This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void Write(byte[] array, int offset, int count)
+ {
+ throw new NotSupportedException("TarInputStream Write not supported");
+ }
+
+ /// <summary>
+ /// Writes a byte to the current position in the file stream.
+ /// This operation is not supported and will throw a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void WriteByte(byte val)
+ {
+ throw new NotSupportedException("TarInputStream WriteByte not supported");
+ }
+
+
+ /// <summary>
+ /// Construct a TarInputStream with default block factor
+ /// </summary>
+ /// <param name="inputStream">stream to source data from</param>
+ public TarInputStream(Stream inputStream) : this(inputStream, TarBuffer.DefaultBlockFactor)
+ {
+ }
+
+ /// <summary>
+ /// Construct a TarInputStream with user specified block factor
+ /// </summary>
+ /// <param name="inputStream">stream to source data from</param>
+ /// <param name="blockFactor">block factor to apply to archive</param>
+ public TarInputStream(Stream inputStream, int blockFactor)
+ {
+ this.inputStream = inputStream;
+ this.buffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor);
+
+ this.readBuf = null;
+ this.hasHitEOF = false;
+ this.eFactory = null;
+ }
+
+ /// <summary>
+ /// Set the entry factory for this instance.
+ /// </summary>
+ /// <param name="factory">The factory for creating new entries</param>
+ public void SetEntryFactory(IEntryFactory factory)
+ {
+ this.eFactory = factory;
+ }
+
+ /// <summary>
+ /// Closes this stream. Calls the TarBuffer's close() method.
+ /// The underlying stream is closed by the TarBuffer.
+ /// </summary>
+ public override void Close()
+ {
+ this.buffer.Close();
+ }
+
+ /// <summary>
+ /// Get the record size being used by this stream's TarBuffer.
+ /// </summary>
+ /// <returns>
+ /// TarBuffer record size.
+ /// </returns>
+ public int GetRecordSize()
+ {
+ return this.buffer.GetRecordSize();
+ }
+
+ /// <summary>
+ /// Get the available data that can be read from the current
+ /// entry in the archive. This does not indicate how much data
+ /// is left in the entire archive, only in the current entry.
+ /// This value is determined from the entry's size header field
+ /// and the amount of data already read from the current entry.
+ /// </summary>
+ /// <returns>
+ /// The number of available bytes for the current entry.
+ /// </returns>
+ public long Available {
+ get {
+ return this.entrySize - this.entryOffset;
+ }
+ }
+
+ /// <summary>
+ /// Skip bytes in the input buffer. This skips bytes in the
+ /// current entry's data, not the entire archive, and will
+ /// stop at the end of the current entry's data if the number
+ /// to skip extends beyond that point.
+ /// </summary>
+ /// <param name="numToSkip">
+ /// The number of bytes to skip.
+ /// </param>
+ public void Skip(long numToSkip)
+ {
+ // TODO: REVIEW
+ // This is horribly inefficient, but it ensures that we
+ // properly skip over bytes via the TarBuffer...
+ //
+ byte[] skipBuf = new byte[8 * 1024];
+
+ for (long num = numToSkip; num > 0;) {
+ int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num;
+ int numRead = this.Read(skipBuf, 0, toRead);
+
+ if (numRead == -1) {
+ break;
+ }
+
+ num -= numRead;
+ }
+ }
+
+ /// <summary>
+ /// Since we do not support marking just yet, we return false.
+ /// </summary>
+ public bool IsMarkSupported {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Since we do not support marking just yet, we do nothing.
+ /// </summary>
+ /// <param name ="markLimit">
+ /// The limit to mark.
+ /// </param>
+ public void Mark(int markLimit)
+ {
+ }
+
+ /// <summary>
+ /// Since we do not support marking just yet, we do nothing.
+ /// </summary>
+ public void Reset()
+ {
+ }
+
+ void SkipToNextEntry()
+ {
+ long numToSkip = this.entrySize - this.entryOffset;
+
+ if (numToSkip > 0) {
+ this.Skip(numToSkip);
+ }
+
+ this.readBuf = null;
+ }
+
+ /// <summary>
+ /// Get the next entry in this tar archive. This will skip
+ /// over any remaining data in the current entry, if there
+ /// is one, and place the input stream at the header of the
+ /// next entry, and read the header and instantiate a new
+ /// TarEntry from the header bytes and return that entry.
+ /// If there are no more entries in the archive, null will
+ /// be returned to indicate that the end of the archive has
+ /// been reached.
+ /// </summary>
+ /// <returns>
+ /// The next TarEntry in the archive, or null.
+ /// </returns>
+ public TarEntry GetNextEntry()
+ {
+ if (this.hasHitEOF) {
+ return null;
+ }
+
+ if (this.currEntry != null) {
+ SkipToNextEntry();
+ }
+
+ byte[] headerBuf = this.buffer.ReadBlock();
+
+ if (headerBuf == null) {
+ this.hasHitEOF = true;
+ } else if (this.buffer.IsEOFBlock(headerBuf)) {
+ this.hasHitEOF = true;
+ }
+
+ if (this.hasHitEOF) {
+ this.currEntry = null;
+ } else {
+ try {
+ TarHeader header = new TarHeader();
+ header.ParseBuffer(headerBuf);
+ if ( !header.IsChecksumValid )
+ {
+ throw new TarException("Header checksum is invalid");
+ }
+ this.entryOffset = 0;
+ this.entrySize = header.Size;
+
+ StringBuilder longName = null;
+
+ if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) {
+
+ byte[] nameBuffer = new byte[TarBuffer.BlockSize];
+
+ long numToRead = this.entrySize;
+
+ longName = new StringBuilder();
+
+ while (numToRead > 0) {
+ int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead));
+
+ if (numRead == -1) {
+ throw new InvalidHeaderException("Failed to read long name entry");
+ }
+
+ longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
+ numToRead -= numRead;
+ }
+
+ SkipToNextEntry();
+ headerBuf = this.buffer.ReadBlock();
+ } else if (header.TypeFlag == TarHeader.LF_GHDR) { // POSIX global extended header
+ // Ignore things we dont understand completely for now
+ SkipToNextEntry();
+ headerBuf = this.buffer.ReadBlock();
+ } else if (header.TypeFlag == TarHeader.LF_XHDR) { // POSIX extended header
+ // Ignore things we dont understand completely for now
+ SkipToNextEntry();
+ headerBuf = this.buffer.ReadBlock();
+ } else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) {
+ // TODO: could show volume name when verbose
+ SkipToNextEntry();
+ headerBuf = this.buffer.ReadBlock();
+ } else if (header.TypeFlag != TarHeader.LF_NORMAL &&
+ header.TypeFlag != TarHeader.LF_OLDNORM &&
+ header.TypeFlag != TarHeader.LF_DIR) {
+ // Ignore things we dont understand completely for now
+ SkipToNextEntry();
+ headerBuf = this.buffer.ReadBlock();
+ }
+
+ if (this.eFactory == null) {
+ this.currEntry = new TarEntry(headerBuf);
+ if (longName != null) {
+ currEntry.Name = longName.ToString();
+ }
+ } else {
+ this.currEntry = this.eFactory.CreateEntry(headerBuf);
+ }
+
+ // Magic was checked here for 'ustar' but there are multiple valid possibilities
+ // so this is not done anymore.
+
+ this.entryOffset = 0;
+
+ // TODO: Review How do we resolve this discrepancy?!
+ this.entrySize = this.currEntry.Size;
+ } catch (InvalidHeaderException ex) {
+ this.entrySize = 0;
+ this.entryOffset = 0;
+ this.currEntry = null;
+ throw new InvalidHeaderException("bad header in record " + this.buffer.GetCurrentBlockNum() + " block " + this.buffer.GetCurrentBlockNum() + ", " + ex.Message);
+ }
+ }
+ return this.currEntry;
+ }
+
+ /// <summary>
+ /// Reads a byte from the current tar archive entry.
+ /// This method simply calls read(byte[], int, int).
+ /// </summary>
+ public override int ReadByte()
+ {
+ byte[] oneByteBuffer = new byte[1];
+ int num = this.Read(oneByteBuffer, 0, 1);
+ if (num <= 0) { // return -1 to indicate that no byte was read.
+ return -1;
+ }
+ return (int)oneByteBuffer[0];
+ }
+
+ /// <summary>
+ /// Reads bytes from the current tar archive entry.
+ ///
+ /// This method is aware of the boundaries of the current
+ /// entry in the archive and will deal with them appropriately
+ /// </summary>
+ /// <param name="outputBuffer">
+ /// The buffer into which to place bytes read.
+ /// </param>
+ /// <param name="offset">
+ /// The offset at which to place bytes read.
+ /// </param>
+ /// <param name="count">
+ /// The number of bytes to read.
+ /// </param>
+ /// <returns>
+ /// The number of bytes read, or 0 at end of stream/EOF.
+ /// </returns>
+ public override int Read(byte[] outputBuffer, int offset, int count)
+ {
+ int totalRead = 0;
+
+ if (this.entryOffset >= this.entrySize) {
+ return 0;
+ }
+
+ long numToRead = count;
+
+ if ((numToRead + this.entryOffset) > this.entrySize) {
+ numToRead = this.entrySize - this.entryOffset;
+ }
+
+ if (this.readBuf != null) {
+ int sz = (numToRead > this.readBuf.Length) ? this.readBuf.Length : (int)numToRead;
+
+ Array.Copy(this.readBuf, 0, outputBuffer, offset, sz);
+
+ if (sz >= this.readBuf.Length) {
+ this.readBuf = null;
+ } else {
+ int newLen = this.readBuf.Length - sz;
+ byte[] newBuf = new byte[newLen];
+ Array.Copy(this.readBuf, sz, newBuf, 0, newLen);
+ this.readBuf = newBuf;
+ }
+
+ totalRead += sz;
+ numToRead -= sz;
+ offset += sz;
+ }
+
+ while (numToRead > 0) {
+ byte[] rec = this.buffer.ReadBlock();
+ if (rec == null) {
+ // Unexpected EOF!
+ throw new TarException("unexpected EOF with " + numToRead + " bytes unread");
+ }
+
+ int sz = (int)numToRead;
+ int recLen = rec.Length;
+
+ if (recLen > sz) {
+ Array.Copy(rec, 0, outputBuffer, offset, sz);
+ this.readBuf = new byte[recLen - sz];
+ Array.Copy(rec, sz, this.readBuf, 0, recLen - sz);
+ } else {
+ sz = recLen;
+ Array.Copy(rec, 0, outputBuffer, offset, recLen);
+ }
+
+ totalRead += sz;
+ numToRead -= sz;
+ offset += sz;
+ }
+
+ this.entryOffset += totalRead;
+
+ return totalRead;
+ }
+
+ /// <summary>
+ /// Copies the contents of the current tar archive entry directly into
+ /// an output stream.
+ /// </summary>
+ /// <param name="outputStream">
+ /// The OutputStream into which to write the entry's data.
+ /// </param>
+ public void CopyEntryContents(Stream outputStream)
+ {
+ byte[] buf = new byte[32 * 1024];
+
+ while (true) {
+ int numRead = this.Read(buf, 0, buf.Length);
+ if (numRead <= 0) {
+ break;
+ }
+ outputStream.Write(buf, 0, numRead);
+ }
+ }
+
+ /// <summary>
+ /// This interface is provided, along with the method setEntryFactory(), to allow
+ /// the programmer to have their own TarEntry subclass instantiated for the
+ /// entries return from getNextEntry().
+ /// </summary>
+ public interface IEntryFactory
+ {
+ /// <summary>
+ /// Create an entry based on name alone
+ /// </summary>
+ /// <param name="name">
+ /// Name of the new EntryPointNotFoundException to create
+ /// </param>
+ /// <returns>created TarEntry or descendant class</returns>
+ TarEntry CreateEntry(string name);
+
+ /// <summary>
+ /// Create an instance based on an actual file
+ /// </summary>
+ /// <param name="fileName">
+ /// Name of file to represent in the entry
+ /// </param>
+ /// <returns>
+ /// Created TarEntry or descendant class
+ /// </returns>
+ TarEntry CreateEntryFromFile(string fileName);
+
+ /// <summary>
+ /// Create a tar entry based on the header information passed
+ /// </summary>
+ /// <param name="headerBuf">
+ /// Buffer containing header information to base entry on
+ /// </param>
+ /// <returns>
+ /// Created TarEntry or descendant class
+ /// </returns>
+ TarEntry CreateEntry(byte[] headerBuf);
+ }
+
+ /// <summary>
+ /// Standard entry factory class creating instances of the class TarEntry
+ /// </summary>
+ public class EntryFactoryAdapter : IEntryFactory
+ {
+ /// <summary>
+ /// Create a TarEntry based on named
+ /// </summary>
+ public TarEntry CreateEntry(string name)
+ {
+ return TarEntry.CreateTarEntry(name);
+ }
+
+ /// <summary>
+ /// Create a tar entry with details obtained from <paramref name="fileName">file</paramref>
+ /// </summary>
+ public TarEntry CreateEntryFromFile(string fileName)
+ {
+ return TarEntry.CreateEntryFromFile(fileName);
+ }
+
+ /// <summary>
+ /// Create and entry based on details in <paramref name="headerBuf">header</paramref>
+ /// </summary>
+ public TarEntry CreateEntry(byte[] headerBuf)
+ {
+ return new TarEntry(headerBuf);
+ }
+ }
+ }
+
+
+}
+
+/* The original Java file had this header:
+ ** Authored by Timothy Gerard Endres
+ ** <mailto:time@gjt.org> <http://www.trustice.com>
+ **
+ ** This work has been placed into the public domain.
+ ** You may use this work in any way and for any purpose you wish.
+ **
+ ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+ ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+ ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+ ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+ ** REDISTRIBUTION OF THIS SOFTWARE.
+ **
+ */
+
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs
new file mode 100644
index 00000000000..c5003b5774a
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs
@@ -0,0 +1,428 @@
+// TarOutputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Tar
+{
+
+ /// <summary>
+ /// The TarOutputStream writes a UNIX tar archive as an OutputStream.
+ /// Methods are provided to put entries, and then write their contents
+ /// by writing to this stream using write().
+ /// </summary>
+ /// public
+ public class TarOutputStream : Stream
+ {
+ /// <summary>
+ /// flag indicating debugging code should be activated or not
+ /// </summary>
+ protected bool debug;
+
+ /// <summary>
+ /// Size for the current entry
+ /// </summary>
+ protected long currSize;
+
+ /// <summary>
+ /// bytes written for this entry so far
+ /// </summary>
+ protected long currBytes;
+
+ /// <summary>
+ /// single block working buffer
+ /// </summary>
+ protected byte[] blockBuf;
+
+ /// <summary>
+ /// current 'Assembly' buffer length
+ /// </summary>
+ protected int assemLen;
+
+ /// <summary>
+ /// 'Assembly' buffer used to assmble data before writing
+ /// </summary>
+ protected byte[] assemBuf;
+
+ /// <summary>
+ /// TarBuffer used to provide correct blocking factor
+ /// </summary>
+ protected TarBuffer buffer;
+
+ /// <summary>
+ /// the destination stream for the archive contents
+ /// </summary>
+ protected Stream outputStream;
+
+ /// <summary>
+ /// true if the stream supports reading; otherwise, false.
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return outputStream.CanRead;
+ }
+ }
+
+ /// <summary>
+ /// true if the stream supports seeking; otherwise, false.
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return outputStream.CanSeek;
+ }
+ }
+
+ /// <summary>
+ /// true if stream supports writing; otherwise, false.
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return outputStream.CanWrite;
+ }
+ }
+
+ /// <summary>
+ /// length of stream in bytes
+ /// </summary>
+ public override long Length {
+ get {
+ return outputStream.Length;
+ }
+ }
+
+ /// <summary>
+ /// gets or sets the position within the current stream.
+ /// </summary>
+ public override long Position {
+ get {
+ return outputStream.Position;
+ }
+ set {
+ outputStream.Position = value;
+ }
+ }
+
+ /// <summary>
+ /// set the position within the current stream
+ /// </summary>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ return outputStream.Seek(offset, origin);
+ }
+
+ /// <summary>
+ /// set the length of the current stream
+ /// </summary>
+ public override void SetLength(long val)
+ {
+ outputStream.SetLength(val);
+ }
+
+ /// <summary>
+ /// Read a byte from the stream and advance the position within the stream
+ /// by one byte or returns -1 if at the end of the stream.
+ /// </summary>
+ /// <returns>The byte value or -1 if at end of stream</returns>
+ public override int ReadByte()
+ {
+ return outputStream.ReadByte();
+ }
+
+ /// <summary>
+ /// read bytes from the current stream and advance the position within the
+ /// stream by the number of bytes read.
+ /// </summary>
+ /// <returns>The total number of bytes read, or zero if at the end of the stream</returns>
+ public override int Read(byte[] b, int off, int len)
+ {
+ return outputStream.Read(b, off, len);
+ }
+
+ /// <summary>
+ /// All buffered data is written to destination
+ /// </summary>
+ public override void Flush()
+ {
+ outputStream.Flush();
+ }
+
+ /// <summary>
+ /// Construct TarOutputStream using default block factor
+ /// </summary>
+ /// <param name="outputStream">stream to write to</param>
+ public TarOutputStream(Stream outputStream) : this(outputStream, TarBuffer.DefaultBlockFactor)
+ {
+ }
+
+ /// <summary>
+ /// Construct TarOutputStream with user specified block factor
+ /// </summary>
+ /// <param name="outputStream">stream to write to</param>
+ /// <param name="blockFactor">blocking factor</param>
+ public TarOutputStream(Stream outputStream, int blockFactor)
+ {
+ this.outputStream = outputStream;
+ this.buffer = TarBuffer.CreateOutputTarBuffer(outputStream, blockFactor);
+
+ this.debug = false;
+ this.assemLen = 0;
+ this.assemBuf = new byte[TarBuffer.BlockSize];
+ this.blockBuf = new byte[TarBuffer.BlockSize];
+ }
+
+ /// <summary>
+ /// Ends the TAR archive without closing the underlying OutputStream.
+ /// The result is that the EOF record of nulls is written.
+ /// </summary>
+ public void Finish()
+ {
+ this.WriteEOFRecord();
+ }
+
+ /// <summary>
+ /// Ends the TAR archive and closes the underlying OutputStream.
+ /// This means that finish() is called followed by calling the
+ /// TarBuffer's close().
+ /// </summary>
+ public override void Close()
+ {
+ this.Finish();
+ this.buffer.Close();
+ }
+
+ /// <summary>
+ /// Get the record size being used by this stream's TarBuffer.
+ /// </summary>
+ /// <returns>
+ /// The TarBuffer record size.
+ /// </returns>
+ public int GetRecordSize()
+ {
+ return this.buffer.GetRecordSize();
+ }
+
+ /// <summary>
+ /// Put an entry on the output stream. This writes the entry's
+ /// header and positions the output stream for writing
+ /// the contents of the entry. Once this method is called, the
+ /// stream is ready for calls to write() to write the entry's
+ /// contents. Once the contents are written, closeEntry()
+ /// <B>MUST</B> be called to ensure that all buffered data
+ /// is completely written to the output stream.
+ /// </summary>
+ /// <param name="entry">
+ /// The TarEntry to be written to the archive.
+ /// </param>
+ public void PutNextEntry(TarEntry entry)
+ {
+ if (entry.TarHeader.Name.Length >= TarHeader.NAMELEN) {
+ TarHeader longHeader = new TarHeader();
+ longHeader.TypeFlag = TarHeader.LF_GNU_LONGNAME;
+ longHeader.Name = longHeader.Name + "././@LongLink";
+ longHeader.UserId = 0;
+ longHeader.GroupId = 0;
+ longHeader.GroupName = "";
+ longHeader.UserName = "";
+ longHeader.LinkName = "";
+
+ longHeader.Size = entry.TarHeader.Name.Length;
+
+ longHeader.WriteHeader(this.blockBuf);
+ this.buffer.WriteBlock(this.blockBuf); // Add special long filename header block
+
+ int nameCharIndex = 0;
+
+ while (nameCharIndex < entry.TarHeader.Name.Length) {
+ Array.Clear(blockBuf, 0, blockBuf.Length);
+ TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameCharIndex, this.blockBuf, 0, TarBuffer.BlockSize);
+ nameCharIndex += TarBuffer.BlockSize;
+ this.buffer.WriteBlock(this.blockBuf);
+ }
+ }
+
+ entry.WriteEntryHeader(this.blockBuf);
+ this.buffer.WriteBlock(this.blockBuf);
+
+ this.currBytes = 0;
+
+ this.currSize = entry.IsDirectory ? 0 : entry.Size;
+ }
+
+ /// <summary>
+ /// Close an entry. This method MUST be called for all file
+ /// entries that contain data. The reason is that we must
+ /// buffer data written to the stream in order to satisfy
+ /// the buffer's block based writes. Thus, there may be
+ /// data fragments still being assembled that must be written
+ /// to the output stream before this entry is closed and the
+ /// next entry written.
+ /// </summary>
+ public void CloseEntry()
+ {
+ if (this.assemLen > 0) {
+ for (int i = this.assemLen; i < this.assemBuf.Length; ++i) {
+ this.assemBuf[i] = 0;
+ }
+
+ this.buffer.WriteBlock(this.assemBuf);
+
+ this.currBytes += this.assemLen;
+ this.assemLen = 0;
+ }
+
+ if (this.currBytes < this.currSize) {
+ throw new TarException("entry closed at '" + this.currBytes + "' before the '" + this.currSize + "' bytes specified in the header were written");
+ }
+ }
+
+ /// <summary>
+ /// Writes a byte to the current tar archive entry.
+ /// This method simply calls Write(byte[], int, int).
+ /// </summary>
+ /// <param name="b">
+ /// The byte to be written.
+ /// </param>
+ public override void WriteByte(byte b)
+ {
+ this.Write(new byte[] { b }, 0, 1);
+ }
+
+ /// <summary>
+ /// Writes bytes to the current tar archive entry. This method
+ /// is aware of the current entry and will throw an exception if
+ /// you attempt to write bytes past the length specified for the
+ /// current entry. The method is also (painfully) aware of the
+ /// record buffering required by TarBuffer, and manages buffers
+ /// that are not a multiple of recordsize in length, including
+ /// assembling records from small buffers.
+ /// </summary>
+ /// <param name = "wBuf">
+ /// The buffer to write to the archive.
+ /// </param>
+ /// <param name = "wOffset">
+ /// The offset in the buffer from which to get bytes.
+ /// </param>
+ /// <param name = "numToWrite">
+ /// The number of bytes to write.
+ /// </param>
+ public override void Write(byte[] wBuf, int wOffset, int numToWrite)
+ {
+ if (wBuf == null) {
+ throw new ArgumentNullException("TarOutputStream.Write buffer null");
+ }
+
+ if ((this.currBytes + numToWrite) > this.currSize) {
+ throw new ArgumentOutOfRangeException("request to write '" + numToWrite + "' bytes exceeds size in header of '" + this.currSize + "' bytes");
+ }
+
+ //
+ // We have to deal with assembly!!!
+ // The programmer can be writing little 32 byte chunks for all
+ // we know, and we must assemble complete blocks for writing.
+ // TODO REVIEW Maybe this should be in TarBuffer? Could that help to
+ // eliminate some of the buffer copying.
+ //
+ if (this.assemLen > 0) {
+ if ((this.assemLen + numToWrite ) >= this.blockBuf.Length) {
+ int aLen = this.blockBuf.Length - this.assemLen;
+
+ Array.Copy(this.assemBuf, 0, this.blockBuf, 0, this.assemLen);
+ Array.Copy(wBuf, wOffset, this.blockBuf, this.assemLen, aLen);
+
+ this.buffer.WriteBlock(this.blockBuf);
+
+ this.currBytes += this.blockBuf.Length;
+
+ wOffset += aLen;
+ numToWrite -= aLen;
+
+ this.assemLen = 0;
+ } else { // ( (this.assemLen + numToWrite ) < this.blockBuf.length )
+ Array.Copy(wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite);
+ wOffset += numToWrite;
+ this.assemLen += numToWrite;
+ numToWrite -= numToWrite;
+ }
+ }
+
+ //
+ // When we get here we have EITHER:
+ // o An empty "assemble" buffer.
+ // o No bytes to write (numToWrite == 0)
+ //
+ while (numToWrite > 0) {
+ if (numToWrite < this.blockBuf.Length) {
+ Array.Copy(wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite);
+ this.assemLen += numToWrite;
+ break;
+ }
+
+ this.buffer.WriteBlock(wBuf, wOffset);
+
+ int num = this.blockBuf.Length;
+ this.currBytes += num;
+ numToWrite -= num;
+ wOffset += num;
+ }
+ }
+
+ /// <summary>
+ /// Write an EOF (end of archive) record to the tar archive.
+ /// An EOF record consists of a record of all zeros.
+ /// </summary>
+ void WriteEOFRecord()
+ {
+ Array.Clear(blockBuf, 0, blockBuf.Length);
+ this.buffer.WriteBlock(this.blockBuf);
+ }
+ }
+}
+
+/* The original Java file had this header:
+ ** Authored by Timothy Gerard Endres
+ ** <mailto:time@gjt.org> <http://www.trustice.com>
+ **
+ ** This work has been placed into the public domain.
+ ** You may use this work in any way and for any purpose you wish.
+ **
+ ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
+ ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
+ ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
+ ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
+ ** REDISTRIBUTION OF THIS SOFTWARE.
+ **
+ */
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs
new file mode 100644
index 00000000000..729ade7827c
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs
@@ -0,0 +1,552 @@
+// Deflater.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// This is the Deflater class. The deflater class compresses input
+ /// with the deflate algorithm described in RFC 1951. It has several
+ /// compression levels and three different strategies described below.
+ ///
+ /// This class is <i>not</i> thread safe. This is inherent in the API, due
+ /// to the split of deflate and setInput.
+ ///
+ /// author of the original java version : Jochen Hoenicke
+ /// </summary>
+ public class Deflater
+ {
+ /// <summary>
+ /// The best and slowest compression level. This tries to find very
+ /// long and distant string repetitions.
+ /// </summary>
+ public static int BEST_COMPRESSION = 9;
+
+ /// <summary>
+ /// The worst but fastest compression level.
+ /// </summary>
+ public static int BEST_SPEED = 1;
+
+ /// <summary>
+ /// The default compression level.
+ /// </summary>
+ public static int DEFAULT_COMPRESSION = -1;
+
+ /// <summary>
+ /// This level won't compress at all but output uncompressed blocks.
+ /// </summary>
+ public static int NO_COMPRESSION = 0;
+
+ /// <summary>
+ /// The compression method. This is the only method supported so far.
+ /// There is no need to use this constant at all.
+ /// </summary>
+ public static int DEFLATED = 8;
+
+ /*
+ * The Deflater can do the following state transitions:
+ *
+ * (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---.
+ * / | (2) (5) |
+ * / v (5) |
+ * (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
+ * \ | (3) | ,-------'
+ * | | | (3) /
+ * v v (5) v v
+ * (1) -> BUSY_STATE ----> FINISHING_STATE
+ * | (6)
+ * v
+ * FINISHED_STATE
+ * \_____________________________________/
+ * | (7)
+ * v
+ * CLOSED_STATE
+ *
+ * (1) If we should produce a header we start in INIT_STATE, otherwise
+ * we start in BUSY_STATE.
+ * (2) A dictionary may be set only when we are in INIT_STATE, then
+ * we change the state as indicated.
+ * (3) Whether a dictionary is set or not, on the first call of deflate
+ * we change to BUSY_STATE.
+ * (4) -- intentionally left blank -- :)
+ * (5) FINISHING_STATE is entered, when flush() is called to indicate that
+ * there is no more INPUT. There are also states indicating, that
+ * the header wasn't written yet.
+ * (6) FINISHED_STATE is entered, when everything has been flushed to the
+ * internal pending output buffer.
+ * (7) At any time (7)
+ *
+ */
+
+ private static int IS_SETDICT = 0x01;
+ private static int IS_FLUSHING = 0x04;
+ private static int IS_FINISHING = 0x08;
+
+ private static int INIT_STATE = 0x00;
+ private static int SETDICT_STATE = 0x01;
+ // private static int INIT_FINISHING_STATE = 0x08;
+ // private static int SETDICT_FINISHING_STATE = 0x09;
+ private static int BUSY_STATE = 0x10;
+ private static int FLUSHING_STATE = 0x14;
+ private static int FINISHING_STATE = 0x1c;
+ private static int FINISHED_STATE = 0x1e;
+ private static int CLOSED_STATE = 0x7f;
+
+ /// <summary>
+ /// Compression level.
+ /// </summary>
+ private int level;
+
+ /// <summary>
+ /// If true no Zlib/RFC1950 headers or footers are generated
+ /// </summary>
+ private bool noZlibHeaderOrFooter;
+
+ /// <summary>
+ /// The current state.
+ /// </summary>
+ private int state;
+
+ /// <summary>
+ /// The total bytes of output written.
+ /// </summary>
+ private long totalOut;
+
+ /// <summary>
+ /// The pending output.
+ /// </summary>
+ private DeflaterPending pending;
+
+ /// <summary>
+ /// The deflater engine.
+ /// </summary>
+ private DeflaterEngine engine;
+
+ /// <summary>
+ /// Creates a new deflater with default compression level.
+ /// </summary>
+ public Deflater() : this(DEFAULT_COMPRESSION, false)
+ {
+
+ }
+
+ /// <summary>
+ /// Creates a new deflater with given compression level.
+ /// </summary>
+ /// <param name="lvl">
+ /// the compression level, a value between NO_COMPRESSION
+ /// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
+ /// </param>
+ /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
+ public Deflater(int lvl) : this(lvl, false)
+ {
+
+ }
+
+ /// <summary>
+ /// Creates a new deflater with given compression level.
+ /// </summary>
+ /// <param name="level">
+ /// the compression level, a value between NO_COMPRESSION
+ /// and BEST_COMPRESSION.
+ /// </param>
+ /// <param name="noZlibHeaderOrFooter">
+ /// true, if we should suppress the Zlib/RFC1950 header at the
+ /// beginning and the adler checksum at the end of the output. This is
+ /// useful for the GZIP/PKZIP formats.
+ /// </param>
+ /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
+ public Deflater(int level, bool noZlibHeaderOrFooter)
+ {
+ if (level == DEFAULT_COMPRESSION) {
+ level = 6;
+ } else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) {
+ throw new ArgumentOutOfRangeException("level");
+ }
+
+ pending = new DeflaterPending();
+ engine = new DeflaterEngine(pending);
+ this.noZlibHeaderOrFooter = noZlibHeaderOrFooter;
+ SetStrategy(DeflateStrategy.Default);
+ SetLevel(level);
+ Reset();
+ }
+
+
+ /// <summary>
+ /// Resets the deflater. The deflater acts afterwards as if it was
+ /// just created with the same compression level and strategy as it
+ /// had before.
+ /// </summary>
+ public void Reset()
+ {
+ state = (noZlibHeaderOrFooter ? BUSY_STATE : INIT_STATE);
+ totalOut = 0;
+ pending.Reset();
+ engine.Reset();
+ }
+
+ /// <summary>
+ /// Gets the current adler checksum of the data that was processed so far.
+ /// </summary>
+ public int Adler {
+ get {
+ return engine.Adler;
+ }
+ }
+
+ /// <summary>
+ /// Gets the number of input bytes processed so far.
+ /// </summary>
+ public int TotalIn {
+ get {
+ return engine.TotalIn;
+ }
+ }
+
+ /// <summary>
+ /// Gets the number of output bytes so far.
+ /// </summary>
+ public long TotalOut {
+ get {
+ return totalOut;
+ }
+ }
+
+ /// <summary>
+ /// Flushes the current input block. Further calls to deflate() will
+ /// produce enough output to inflate everything in the current input
+ /// block. This is not part of Sun's JDK so I have made it package
+ /// private. It is used by DeflaterOutputStream to implement
+ /// flush().
+ /// </summary>
+ public void Flush()
+ {
+ state |= IS_FLUSHING;
+ }
+
+ /// <summary>
+ /// Finishes the deflater with the current input block. It is an error
+ /// to give more input after this method was called. This method must
+ /// be called to force all bytes to be flushed.
+ /// </summary>
+ public void Finish()
+ {
+ state |= IS_FLUSHING | IS_FINISHING;
+ }
+
+ /// <summary>
+ /// Returns true if the stream was finished and no more output bytes
+ /// are available.
+ /// </summary>
+ public bool IsFinished {
+ get {
+ return state == FINISHED_STATE && pending.IsFlushed;
+ }
+ }
+
+ /// <summary>
+ /// Returns true, if the input buffer is empty.
+ /// You should then call setInput().
+ /// NOTE: This method can also return true when the stream
+ /// was finished.
+ /// </summary>
+ public bool IsNeedingInput {
+ get {
+ return engine.NeedsInput();
+ }
+ }
+
+ /// <summary>
+ /// Sets the data which should be compressed next. This should be only
+ /// called when needsInput indicates that more input is needed.
+ /// If you call setInput when needsInput() returns false, the
+ /// previous input that is still pending will be thrown away.
+ /// The given byte array should not be changed, before needsInput() returns
+ /// true again.
+ /// This call is equivalent to <code>setInput(input, 0, input.length)</code>.
+ /// </summary>
+ /// <param name="input">
+ /// the buffer containing the input data.
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// if the buffer was finished() or ended().
+ /// </exception>
+ public void SetInput(byte[] input)
+ {
+ SetInput(input, 0, input.Length);
+ }
+
+ /// <summary>
+ /// Sets the data which should be compressed next. This should be
+ /// only called when needsInput indicates that more input is needed.
+ /// The given byte array should not be changed, before needsInput() returns
+ /// true again.
+ /// </summary>
+ /// <param name="input">
+ /// the buffer containing the input data.
+ /// </param>
+ /// <param name="off">
+ /// the start of the data.
+ /// </param>
+ /// <param name="len">
+ /// the length of the data.
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// if the buffer was finished() or ended() or if previous input is still pending.
+ /// </exception>
+ public void SetInput(byte[] input, int off, int len)
+ {
+ if ((state & IS_FINISHING) != 0) {
+ throw new InvalidOperationException("finish()/end() already called");
+ }
+ engine.SetInput(input, off, len);
+ }
+
+ /// <summary>
+ /// Sets the compression level. There is no guarantee of the exact
+ /// position of the change, but if you call this when needsInput is
+ /// true the change of compression level will occur somewhere near
+ /// before the end of the so far given input.
+ /// </summary>
+ /// <param name="lvl">
+ /// the new compression level.
+ /// </param>
+ public void SetLevel(int lvl)
+ {
+ if (lvl == DEFAULT_COMPRESSION) {
+ lvl = 6;
+ } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
+ throw new ArgumentOutOfRangeException("lvl");
+ }
+
+ if (level != lvl) {
+ level = lvl;
+ engine.SetLevel(lvl);
+ }
+ }
+
+ /// <summary>
+ /// Get current compression level
+ /// </summary>
+ /// <returns>Returns the current compression level</returns>
+ public int GetLevel() {
+ return level;
+ }
+
+ /// <summary>
+ /// Sets the compression strategy. Strategy is one of
+ /// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
+ /// position where the strategy is changed, the same as for
+ /// setLevel() applies.
+ /// </summary>
+ /// <param name="strategy">
+ /// The new compression strategy.
+ /// </param>
+ public void SetStrategy(DeflateStrategy strategy)
+ {
+ engine.Strategy = strategy;
+ }
+
+ /// <summary>
+ /// Deflates the current input block with to the given array.
+ /// </summary>
+ /// <param name="output">
+ /// The buffer where compressed data is stored
+ /// </param>
+ /// <returns>
+ /// The number of compressed bytes added to the output, or 0 if either
+ /// needsInput() or finished() returns true or length is zero.
+ /// </returns>
+ public int Deflate(byte[] output)
+ {
+ return Deflate(output, 0, output.Length);
+ }
+
+ /// <summary>
+ /// Deflates the current input block to the given array.
+ /// </summary>
+ /// <param name="output">
+ /// Buffer to store the compressed data.
+ /// </param>
+ /// <param name="offset">
+ /// Offset into the output array.
+ /// </param>
+ /// <param name="length">
+ /// The maximum number of bytes that may be stored.
+ /// </param>
+ /// <returns>
+ /// The number of compressed bytes added to the output, or 0 if either
+ /// needsInput() or finished() returns true or length is zero.
+ /// </returns>
+ /// <exception cref="System.InvalidOperationException">
+ /// If end() was previously called.
+ /// </exception>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// If offset and/or length don't match the array length.
+ /// </exception>
+ public int Deflate(byte[] output, int offset, int length)
+ {
+ int origLength = length;
+
+ if (state == CLOSED_STATE) {
+ throw new InvalidOperationException("Deflater closed");
+ }
+
+ if (state < BUSY_STATE) {
+ /* output header */
+ int header = (DEFLATED +
+ ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
+ int level_flags = (level - 1) >> 1;
+ if (level_flags < 0 || level_flags > 3) {
+ level_flags = 3;
+ }
+ header |= level_flags << 6;
+ if ((state & IS_SETDICT) != 0) {
+ /* Dictionary was set */
+ header |= DeflaterConstants.PRESET_DICT;
+ }
+ header += 31 - (header % 31);
+
+
+ pending.WriteShortMSB(header);
+ if ((state & IS_SETDICT) != 0) {
+ int chksum = engine.Adler;
+ engine.ResetAdler();
+ pending.WriteShortMSB(chksum >> 16);
+ pending.WriteShortMSB(chksum & 0xffff);
+ }
+
+ state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));
+ }
+
+ for (;;) {
+ int count = pending.Flush(output, offset, length);
+ offset += count;
+ totalOut += count;
+ length -= count;
+
+ if (length == 0 || state == FINISHED_STATE) {
+ break;
+ }
+
+ if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) {
+ if (state == BUSY_STATE) {
+ /* We need more input now */
+ return origLength - length;
+ } else if (state == FLUSHING_STATE) {
+ if (level != NO_COMPRESSION) {
+ /* We have to supply some lookahead. 8 bit lookahead
+ * is needed by the zlib inflater, and we must fill
+ * the next byte, so that all bits are flushed.
+ */
+ int neededbits = 8 + ((-pending.BitCount) & 7);
+ while (neededbits > 0) {
+ /* write a static tree block consisting solely of
+ * an EOF:
+ */
+ pending.WriteBits(2, 10);
+ neededbits -= 10;
+ }
+ }
+ state = BUSY_STATE;
+ } else if (state == FINISHING_STATE) {
+ pending.AlignToByte();
+
+ // Compressed data is complete. Write footer information if required.
+ if (!noZlibHeaderOrFooter) {
+ int adler = engine.Adler;
+ pending.WriteShortMSB(adler >> 16);
+ pending.WriteShortMSB(adler & 0xffff);
+ }
+ state = FINISHED_STATE;
+ }
+ }
+ }
+ return origLength - length;
+ }
+
+ /// <summary>
+ /// Sets the dictionary which should be used in the deflate process.
+ /// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.
+ /// </summary>
+ /// <param name="dict">
+ /// the dictionary.
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// if setInput () or deflate () were already called or another dictionary was already set.
+ /// </exception>
+ public void SetDictionary(byte[] dict)
+ {
+ SetDictionary(dict, 0, dict.Length);
+ }
+
+ /// <summary>
+ /// Sets the dictionary which should be used in the deflate process.
+ /// The dictionary is a byte array containing strings that are
+ /// likely to occur in the data which should be compressed. The
+ /// dictionary is not stored in the compressed output, only a
+ /// checksum. To decompress the output you need to supply the same
+ /// dictionary again.
+ /// </summary>
+ /// <param name="dict">
+ /// The dictionary data
+ /// </param>
+ /// <param name="offset">
+ /// An offset into the dictionary.
+ /// </param>
+ /// <param name="length">
+ /// The length of the dictionary data to use
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// If setInput () or deflate () were already called or another dictionary was already set.
+ /// </exception>
+ public void SetDictionary(byte[] dict, int offset, int length)
+ {
+ if (state != INIT_STATE) {
+ throw new InvalidOperationException();
+ }
+
+ state = SETDICT_STATE;
+ engine.SetDictionary(dict, offset, length);
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs
new file mode 100644
index 00000000000..5c12bf13641
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs
@@ -0,0 +1,186 @@
+// DeflaterConstants.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// This class contains constants used for deflation.
+ /// </summary>
+ public class DeflaterConstants
+ {
+ /// <summary>
+ /// Set to true to enable debugging
+ /// </summary>
+ public const bool DEBUGGING = false;
+
+ /// <summary>
+ /// Written to Zip file to identify a stored block
+ /// </summary>
+ public const int STORED_BLOCK = 0;
+
+ /// <summary>
+ /// Identifies static tree in Zip file
+ /// </summary>
+ public const int STATIC_TREES = 1;
+
+ /// <summary>
+ /// Identifies dynamic tree in Zip file
+ /// </summary>
+ public const int DYN_TREES = 2;
+
+ /// <summary>
+ /// Header flag indicating a preset dictionary for deflation
+ /// </summary>
+ public const int PRESET_DICT = 0x20;
+
+ /// <summary>
+ /// Sets internal buffer sizes for Huffman encoding
+ /// </summary>
+ public const int DEFAULT_MEM_LEVEL = 8;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int MAX_MATCH = 258;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int MIN_MATCH = 3;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int MAX_WBITS = 15;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int WSIZE = 1 << MAX_WBITS;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int WMASK = WSIZE - 1;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int HASH_BITS = DEFAULT_MEM_LEVEL + 7;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int HASH_SIZE = 1 << HASH_BITS;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int HASH_MASK = HASH_SIZE - 1;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int HASH_SHIFT = (HASH_BITS + MIN_MATCH - 1) / MIN_MATCH;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int MAX_DIST = WSIZE - MIN_LOOKAHEAD;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int PENDING_BUF_SIZE = 1 << (DEFAULT_MEM_LEVEL + 8);
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int MAX_BLOCK_SIZE = Math.Min(65535, PENDING_BUF_SIZE - 5);
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int DEFLATE_STORED = 0;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int DEFLATE_FAST = 1;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public const int DEFLATE_SLOW = 2;
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int[] GOOD_LENGTH = { 0, 4, 4, 4, 4, 8, 8, 8, 32, 32 };
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int[] MAX_LAZY = { 0, 4, 5, 6, 4, 16, 16, 32, 128, 258 };
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int[] NICE_LENGTH = { 0, 8, 16, 32, 16, 32, 128, 128, 258, 258 };
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int[] MAX_CHAIN = { 0, 4, 8, 32, 16, 32, 128, 256, 1024, 4096 };
+
+ /// <summary>
+ /// Internal compression engine constant
+ /// </summary>
+ public static int[] COMPR_FUNC = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2 };
+
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs
new file mode 100644
index 00000000000..5ef88f0aea4
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs
@@ -0,0 +1,791 @@
+// DeflaterEngine.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+using ICSharpCode.SharpZipLib.Checksums;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// Strategies for deflater
+ /// </summary>
+ public enum DeflateStrategy
+ {
+ /// <summary>
+ /// The default strategy
+ /// </summary>
+ Default = 0,
+
+ /// <summary>
+ /// This strategy will only allow longer string repetitions. It is
+ /// useful for random data with a small character set.
+ /// </summary>
+ Filtered = 1,
+
+
+ /// <summary>
+ /// This strategy will not look for string repetitions at all. It
+ /// only encodes with Huffman trees (which means, that more common
+ /// characters get a smaller encoding.
+ /// </summary>
+ HuffmanOnly = 2
+ }
+
+ // DEFLATE ALGORITHM:
+ //
+ // The uncompressed stream is inserted into the window array. When
+ // the window array is full the first half is thrown away and the
+ // second half is copied to the beginning.
+ //
+ // The head array is a hash table. Three characters build a hash value
+ // and they the value points to the corresponding index in window of
+ // the last string with this hash. The prev array implements a
+ // linked list of matches with the same hash: prev[index & WMASK] points
+ // to the previous index with the same hash.
+ //
+
+
+ /// <summary>
+ /// Low level compression engine for deflate algorithm which uses a 32K sliding window
+ /// with secondary compression from Huffman/Shannon-Fano codes.
+ /// </summary>
+ public class DeflaterEngine : DeflaterConstants
+ {
+ static int TOO_FAR = 4096;
+
+ int ins_h;
+
+ /// <summary>
+ /// Hashtable, hashing three characters to an index for window, so
+ /// that window[index]..window[index+2] have this hash code.
+ /// Note that the array should really be unsigned short, so you need
+ /// to and the values with 0xffff.
+ /// </summary>
+ short[] head;
+
+ /// <summary>
+ /// <code>prev[index &amp; WMASK]</code> points to the previous index that has the
+ /// same hash code as the string starting at index. This way
+ /// entries with the same hash code are in a linked list.
+ /// Note that the array should really be unsigned short, so you need
+ /// to and the values with 0xffff.
+ /// </summary>
+ short[] prev;
+
+ int matchStart;
+ int matchLen;
+ bool prevAvailable;
+ int blockStart;
+
+ /// <summary>
+ /// Points to the current character in the window.
+ /// </summary>
+ int strstart;
+
+ /// <summary>
+ /// lookahead is the number of characters starting at strstart in
+ /// window that are valid.
+ /// So window[strstart] until window[strstart+lookahead-1] are valid
+ /// characters.
+ /// </summary>
+ int lookahead;
+
+ /// <summary>
+ /// This array contains the part of the uncompressed stream that
+ /// is of relevance. The current character is indexed by strstart.
+ /// </summary>
+ byte[] window;
+
+ DeflateStrategy strategy;
+ int max_chain, max_lazy, niceLength, goodLength;
+
+ /// <summary>
+ /// The current compression function.
+ /// </summary>
+ int comprFunc;
+
+ /// <summary>
+ /// The input data for compression.
+ /// </summary>
+ byte[] inputBuf;
+
+ /// <summary>
+ /// The total bytes of input read.
+ /// </summary>
+ int totalIn;
+
+ /// <summary>
+ /// The offset into inputBuf, where input data starts.
+ /// </summary>
+ int inputOff;
+
+ /// <summary>
+ /// The end offset of the input data.
+ /// </summary>
+ int inputEnd;
+
+ DeflaterPending pending;
+ DeflaterHuffman huffman;
+
+ /// <summary>
+ /// The adler checksum
+ /// </summary>
+ Adler32 adler;
+
+ /// <summary>
+ /// Construct instance with pending buffer
+ /// </summary>
+ /// <param name="pending">
+ /// Pending buffer to use
+ /// </param>>
+ public DeflaterEngine(DeflaterPending pending)
+ {
+ this.pending = pending;
+ huffman = new DeflaterHuffman(pending);
+ adler = new Adler32();
+
+ window = new byte[2 * WSIZE];
+ head = new short[HASH_SIZE];
+ prev = new short[WSIZE];
+
+ // We start at index 1, to avoid an implementation deficiency, that
+ // we cannot build a repeat pattern at index 0.
+ blockStart = strstart = 1;
+ }
+
+ /// <summary>
+ /// Reset internal state
+ /// </summary>
+ public void Reset()
+ {
+ huffman.Reset();
+ adler.Reset();
+ blockStart = strstart = 1;
+ lookahead = 0;
+ totalIn = 0;
+ prevAvailable = false;
+ matchLen = MIN_MATCH - 1;
+
+ for (int i = 0; i < HASH_SIZE; i++) {
+ head[i] = 0;
+ }
+
+ for (int i = 0; i < WSIZE; i++) {
+ prev[i] = 0;
+ }
+ }
+
+ /// <summary>
+ /// Reset Adler checksum
+ /// </summary>
+ public void ResetAdler()
+ {
+ adler.Reset();
+ }
+
+ /// <summary>
+ /// Get current value of Adler checksum
+ /// </summary>
+ public int Adler {
+ get {
+ return (int)adler.Value;
+ }
+ }
+
+ /// <summary>
+ /// Total data processed
+ /// </summary>
+ public int TotalIn {
+ get {
+ return totalIn;
+ }
+ }
+
+ /// <summary>
+ /// Get/set the <see cref="DeflateStrategy">deflate strategy</see>
+ /// </summary>
+ public DeflateStrategy Strategy {
+ get {
+ return strategy;
+ }
+ set {
+ strategy = value;
+ }
+ }
+
+ /// <summary>
+ /// Set the deflate level (0-9)
+ /// </summary>
+ public void SetLevel(int lvl)
+ {
+ goodLength = DeflaterConstants.GOOD_LENGTH[lvl];
+ max_lazy = DeflaterConstants.MAX_LAZY[lvl];
+ niceLength = DeflaterConstants.NICE_LENGTH[lvl];
+ max_chain = DeflaterConstants.MAX_CHAIN[lvl];
+
+ if (DeflaterConstants.COMPR_FUNC[lvl] != comprFunc) {
+/*
+ if (DeflaterConstants.DEBUGGING) {
+ Console.WriteLine("Change from " + comprFunc + " to "
+ + DeflaterConstants.COMPR_FUNC[lvl]);
+ }
+*/
+ switch (comprFunc) {
+ case DEFLATE_STORED:
+ if (strstart > blockStart) {
+ huffman.FlushStoredBlock(window, blockStart,
+ strstart - blockStart, false);
+ blockStart = strstart;
+ }
+ UpdateHash();
+ break;
+ case DEFLATE_FAST:
+ if (strstart > blockStart) {
+ huffman.FlushBlock(window, blockStart, strstart - blockStart,
+ false);
+ blockStart = strstart;
+ }
+ break;
+ case DEFLATE_SLOW:
+ if (prevAvailable) {
+ huffman.TallyLit(window[strstart-1] & 0xff);
+ }
+ if (strstart > blockStart) {
+ huffman.FlushBlock(window, blockStart, strstart - blockStart, false);
+ blockStart = strstart;
+ }
+ prevAvailable = false;
+ matchLen = MIN_MATCH - 1;
+ break;
+ }
+ comprFunc = COMPR_FUNC[lvl];
+ }
+ }
+
+ void UpdateHash()
+ {
+/*
+ if (DEBUGGING) {
+ Console.WriteLine("updateHash: "+strstart);
+ }
+*/
+ ins_h = (window[strstart] << HASH_SHIFT) ^ window[strstart + 1];
+ }
+
+ /// <summary>
+ /// Inserts the current string in the head hash and returns the previous
+ /// value for this hash.
+ /// </summary>
+ /// <returns>The previous hash value</returns>
+ int InsertString()
+ {
+ short match;
+ int hash = ((ins_h << HASH_SHIFT) ^ window[strstart + (MIN_MATCH -1)]) & HASH_MASK;
+/*
+ if (DeflaterConstants.DEBUGGING) {
+ if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^
+ (window[strstart + 1] << HASH_SHIFT) ^
+ (window[strstart + 2])) & HASH_MASK)) {
+ throw new SharpZipBaseException("hash inconsistent: " + hash + "/"
+ +window[strstart] + ","
+ +window[strstart+1] + ","
+ +window[strstart+2] + "," + HASH_SHIFT);
+ }
+ }
+*/
+ prev[strstart & WMASK] = match = head[hash];
+ head[hash] = (short)strstart;
+ ins_h = hash;
+ return match & 0xffff;
+ }
+
+ void SlideWindow()
+ {
+ Array.Copy(window, WSIZE, window, 0, WSIZE);
+ matchStart -= WSIZE;
+ strstart -= WSIZE;
+ blockStart -= WSIZE;
+
+ /* Slide the hash table (could be avoided with 32 bit values
+ * at the expense of memory usage).
+ */
+ for (int i = 0; i < HASH_SIZE; ++i) {
+ int m = head[i] & 0xffff;
+ head[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
+ }
+
+ /* Slide the prev table. */
+ for (int i = 0; i < WSIZE; i++) {
+ int m = prev[i] & 0xffff;
+ prev[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
+ }
+ }
+
+ /// <summary>
+ /// Fill the window
+ /// </summary>
+ public void FillWindow()
+ {
+ /* If the window is almost full and there is insufficient lookahead,
+ * move the upper half to the lower one to make room in the upper half.
+ */
+ if (strstart >= WSIZE + MAX_DIST) {
+ SlideWindow();
+ }
+
+ /* If there is not enough lookahead, but still some input left,
+ * read in the input
+ */
+ while (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd) {
+ int more = 2 * WSIZE - lookahead - strstart;
+
+ if (more > inputEnd - inputOff) {
+ more = inputEnd - inputOff;
+ }
+
+ System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
+ adler.Update(inputBuf, inputOff, more);
+
+ inputOff += more;
+ totalIn += more;
+ lookahead += more;
+ }
+
+ if (lookahead >= MIN_MATCH) {
+ UpdateHash();
+ }
+ }
+
+ /// <summary>
+ /// Find the best (longest) string in the window matching the
+ /// string starting at strstart.
+ ///
+ /// Preconditions:
+ /// <code>
+ /// strstart + MAX_MATCH &lt;= window.length.</code>
+ /// </summary>
+ /// <param name="curMatch"></param>
+ /// <returns>True if a match greater than the minimum length is found</returns>
+ bool FindLongestMatch(int curMatch)
+ {
+ int chainLength = this.max_chain;
+ int niceLength = this.niceLength;
+ short[] prev = this.prev;
+ int scan = this.strstart;
+ int match;
+ int best_end = this.strstart + matchLen;
+ int best_len = Math.Max(matchLen, MIN_MATCH - 1);
+
+ int limit = Math.Max(strstart - MAX_DIST, 0);
+
+ int strend = strstart + MAX_MATCH - 1;
+ byte scan_end1 = window[best_end - 1];
+ byte scan_end = window[best_end];
+
+ /* Do not waste too much time if we already have a good match: */
+ if (best_len >= this.goodLength) {
+ chainLength >>= 2;
+ }
+
+ /* Do not look for matches beyond the end of the input. This is necessary
+ * to make deflate deterministic.
+ */
+ if (niceLength > lookahead) {
+ niceLength = lookahead;
+ }
+
+/*
+ if (DeflaterConstants.DEBUGGING && strstart > 2 * WSIZE - MIN_LOOKAHEAD) {
+ throw new InvalidOperationException("need lookahead");
+ }
+*/
+ do {
+/*
+ if (DeflaterConstants.DEBUGGING && curMatch >= strstart) {
+ throw new InvalidOperationException("future match");
+ }
+*/
+ if (window[curMatch + best_len] != scan_end ||
+ window[curMatch + best_len - 1] != scan_end1 ||
+ window[curMatch] != window[scan] ||
+ window[curMatch + 1] != window[scan + 1]) {
+ continue;
+ }
+
+ match = curMatch + 2;
+ scan += 2;
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart + 258.
+ */
+ while (window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] &&
+ window[++scan] == window[++match] && scan < strend) ;
+
+ if (scan > best_end) {
+/*
+ if (DeflaterConstants.DEBUGGING && ins_h == 0)
+ System.err.println("Found match: "+curMatch+"-"+(scan-strstart));
+*/
+ matchStart = curMatch;
+ best_end = scan;
+ best_len = scan - strstart;
+
+ if (best_len >= niceLength) {
+ break;
+ }
+
+ scan_end1 = window[best_end - 1];
+ scan_end = window[best_end];
+ }
+ scan = strstart;
+ } while ((curMatch = (prev[curMatch & WMASK] & 0xffff)) > limit && --chainLength != 0);
+
+ matchLen = Math.Min(best_len, lookahead);
+ return matchLen >= MIN_MATCH;
+ }
+
+ /// <summary>
+ /// Set compression dictionary
+ /// </summary>
+ public void SetDictionary(byte[] buffer, int offset, int length)
+ {
+/*
+ if (DeflaterConstants.DEBUGGING && strstart != 1) {
+ throw new InvalidOperationException("strstart not 1");
+ }
+*/
+ adler.Update(buffer, offset, length);
+ if (length < MIN_MATCH) {
+ return;
+ }
+ if (length > MAX_DIST) {
+ offset += length - MAX_DIST;
+ length = MAX_DIST;
+ }
+
+ System.Array.Copy(buffer, offset, window, strstart, length);
+
+ UpdateHash();
+ --length;
+ while (--length > 0) {
+ InsertString();
+ strstart++;
+ }
+ strstart += 2;
+ blockStart = strstart;
+ }
+
+ bool DeflateStored(bool flush, bool finish)
+ {
+ if (!flush && lookahead == 0) {
+ return false;
+ }
+
+ strstart += lookahead;
+ lookahead = 0;
+
+ int storedLen = strstart - blockStart;
+
+ if ((storedLen >= DeflaterConstants.MAX_BLOCK_SIZE) || /* Block is full */
+ (blockStart < WSIZE && storedLen >= MAX_DIST) || /* Block may move out of window */
+ flush) {
+ bool lastBlock = finish;
+ if (storedLen > DeflaterConstants.MAX_BLOCK_SIZE) {
+ storedLen = DeflaterConstants.MAX_BLOCK_SIZE;
+ lastBlock = false;
+ }
+
+/*
+ if (DeflaterConstants.DEBUGGING) {
+ Console.WriteLine("storedBlock["+storedLen+","+lastBlock+"]");
+ }
+*/
+
+ huffman.FlushStoredBlock(window, blockStart, storedLen, lastBlock);
+ blockStart += storedLen;
+ return !lastBlock;
+ }
+ return true;
+ }
+
+ private bool DeflateFast(bool flush, bool finish)
+ {
+ if (lookahead < MIN_LOOKAHEAD && !flush) {
+ return false;
+ }
+
+ while (lookahead >= MIN_LOOKAHEAD || flush) {
+ if (lookahead == 0) {
+ /* We are flushing everything */
+ huffman.FlushBlock(window, blockStart, strstart - blockStart, finish);
+ blockStart = strstart;
+ return false;
+ }
+
+ if (strstart > 2 * WSIZE - MIN_LOOKAHEAD) {
+ /* slide window, as findLongestMatch needs this.
+ * This should only happen when flushing and the window
+ * is almost full.
+ */
+ SlideWindow();
+ }
+
+ int hashHead;
+ if (lookahead >= MIN_MATCH &&
+ (hashHead = InsertString()) != 0 &&
+ strategy != DeflateStrategy.HuffmanOnly &&
+ strstart - hashHead <= MAX_DIST &&
+ FindLongestMatch(hashHead)) {
+ /* longestMatch sets matchStart and matchLen */
+/*
+ if (DeflaterConstants.DEBUGGING) {
+ for (int i = 0 ; i < matchLen; i++) {
+ if (window[strstart+i] != window[matchStart + i]) {
+ throw new SharpZipBaseException("Match failure");
+ }
+ }
+ }
+*/
+ // -jr- Hak hak hak this stops problems with fast/low compression and index out of range
+ if (huffman.TallyDist(strstart - matchStart, matchLen)) {
+ bool lastBlock = finish && lookahead == 0;
+ huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
+ blockStart = strstart;
+ }
+
+ lookahead -= matchLen;
+ if (matchLen <= max_lazy && lookahead >= MIN_MATCH) {
+ while (--matchLen > 0) {
+ ++strstart;
+ InsertString();
+ }
+ ++strstart;
+ } else {
+ strstart += matchLen;
+ if (lookahead >= MIN_MATCH - 1) {
+ UpdateHash();
+ }
+ }
+ matchLen = MIN_MATCH - 1;
+ continue;
+ } else {
+ /* No match found */
+ huffman.TallyLit(window[strstart] & 0xff);
+ ++strstart;
+ --lookahead;
+ }
+
+ if (huffman.IsFull()) {
+ bool lastBlock = finish && lookahead == 0;
+ huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
+ blockStart = strstart;
+ return !lastBlock;
+ }
+ }
+ return true;
+ }
+
+ bool DeflateSlow(bool flush, bool finish)
+ {
+ if (lookahead < MIN_LOOKAHEAD && !flush) {
+ return false;
+ }
+
+ while (lookahead >= MIN_LOOKAHEAD || flush) {
+ if (lookahead == 0) {
+ if (prevAvailable) {
+ huffman.TallyLit(window[strstart-1] & 0xff);
+ }
+ prevAvailable = false;
+
+ // We are flushing everything
+/*
+ if (DeflaterConstants.DEBUGGING && !flush) {
+ throw new SharpZipBaseException("Not flushing, but no lookahead");
+ }
+*/
+ huffman.FlushBlock(window, blockStart, strstart - blockStart,
+ finish);
+ blockStart = strstart;
+ return false;
+ }
+
+ if (strstart >= 2 * WSIZE - MIN_LOOKAHEAD) {
+ /* slide window, as findLongestMatch need this.
+ * This should only happen when flushing and the window
+ * is almost full.
+ */
+ SlideWindow();
+ }
+
+ int prevMatch = matchStart;
+ int prevLen = matchLen;
+ if (lookahead >= MIN_MATCH) {
+ int hashHead = InsertString();
+ if (strategy != DeflateStrategy.HuffmanOnly && hashHead != 0 && strstart - hashHead <= MAX_DIST && FindLongestMatch(hashHead)) {
+ /* longestMatch sets matchStart and matchLen */
+
+ /* Discard match if too small and too far away */
+ if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == MIN_MATCH && strstart - matchStart > TOO_FAR))) {
+ matchLen = MIN_MATCH - 1;
+ }
+ }
+ }
+
+ /* previous match was better */
+ if (prevLen >= MIN_MATCH && matchLen <= prevLen) {
+/*
+ if (DeflaterConstants.DEBUGGING) {
+ for (int i = 0 ; i < matchLen; i++) {
+ if (window[strstart-1+i] != window[prevMatch + i])
+ throw new SharpZipBaseException();
+ }
+ }
+*/
+ huffman.TallyDist(strstart - 1 - prevMatch, prevLen);
+ prevLen -= 2;
+ do {
+ strstart++;
+ lookahead--;
+ if (lookahead >= MIN_MATCH) {
+ InsertString();
+ }
+ } while (--prevLen > 0);
+ strstart ++;
+ lookahead--;
+ prevAvailable = false;
+ matchLen = MIN_MATCH - 1;
+ } else {
+ if (prevAvailable) {
+ huffman.TallyLit(window[strstart-1] & 0xff);
+ }
+ prevAvailable = true;
+ strstart++;
+ lookahead--;
+ }
+
+ if (huffman.IsFull()) {
+ int len = strstart - blockStart;
+ if (prevAvailable) {
+ len--;
+ }
+ bool lastBlock = (finish && lookahead == 0 && !prevAvailable);
+ huffman.FlushBlock(window, blockStart, len, lastBlock);
+ blockStart += len;
+ return !lastBlock;
+ }
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Deflate drives actual compression of data
+ /// </summary>
+ public bool Deflate(bool flush, bool finish)
+ {
+ bool progress;
+ do {
+ FillWindow();
+ bool canFlush = flush && inputOff == inputEnd;
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("window: ["+blockStart+","+strstart+","
+ // +lookahead+"], "+comprFunc+","+canFlush);
+ // }
+ switch (comprFunc) {
+ case DEFLATE_STORED:
+ progress = DeflateStored(canFlush, finish);
+ break;
+ case DEFLATE_FAST:
+ progress = DeflateFast(canFlush, finish);
+ break;
+ case DEFLATE_SLOW:
+ progress = DeflateSlow(canFlush, finish);
+ break;
+ default:
+ throw new InvalidOperationException("unknown comprFunc");
+ }
+ } while (pending.IsFlushed && progress); /* repeat while we have no pending output and progress was made */
+ return progress;
+ }
+
+
+ /// <summary>
+ /// Sets input data to be deflated. Should only be called when <code>NeedsInput()</code>
+ /// returns true
+ /// </summary>
+ /// <param name="buf">The buffer containing input data.</param>
+ /// <param name="off">The index of the first byte of data.</param>
+ /// <param name="len">The number of bytes of data to use as input.</param>
+ public void SetInput(byte[] buf, int off, int len)
+ {
+ if (inputOff < inputEnd) {
+ throw new InvalidOperationException("Old input was not completely processed");
+ }
+
+ int end = off + len;
+
+ /* We want to throw an ArrayIndexOutOfBoundsException early. The
+ * check is very tricky: it also handles integer wrap around.
+ */
+ if (0 > off || off > end || end > buf.Length) {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ inputBuf = buf;
+ inputOff = off;
+ inputEnd = end;
+ }
+
+ /// <summary>
+ /// Return true if input is needed via <see cref="SetInput"> SetInput</see>
+ /// </summary>
+ public bool NeedsInput()
+ {
+ return inputEnd == inputOff;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs
new file mode 100644
index 00000000000..0753fad07a9
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs
@@ -0,0 +1,887 @@
+// DeflaterHuffman.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// This is the DeflaterHuffman class.
+ ///
+ /// This class is <i>not</i> thread safe. This is inherent in the API, due
+ /// to the split of deflate and setInput.
+ ///
+ /// author of the original java version : Jochen Hoenicke
+ /// </summary>
+ public class DeflaterHuffman
+ {
+ static int BUFSIZE = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6);
+ static int LITERAL_NUM = 286;
+ static int DIST_NUM = 30;
+ static int BITLEN_NUM = 19;
+ static int REP_3_6 = 16;
+ static int REP_3_10 = 17;
+ static int REP_11_138 = 18;
+ static int EOF_SYMBOL = 256;
+ static int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
+
+ static byte[] bit4Reverse = {
+ 0,
+ 8,
+ 4,
+ 12,
+ 2,
+ 10,
+ 6,
+ 14,
+ 1,
+ 9,
+ 5,
+ 13,
+ 3,
+ 11,
+ 7,
+ 15
+ };
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public class Tree
+ {
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public short[] freqs;
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public byte[] length;
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public int minNumCodes;
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public int numCodes;
+
+ short[] codes;
+ int[] bl_counts;
+ int maxLength;
+ DeflaterHuffman dh;
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public Tree(DeflaterHuffman dh, int elems, int minCodes, int maxLength)
+ {
+ this.dh = dh;
+ this.minNumCodes = minCodes;
+ this.maxLength = maxLength;
+ freqs = new short[elems];
+ bl_counts = new int[maxLength];
+ }
+
+ /// <summary>
+ /// Resets the internal state of the tree
+ /// </summary>
+ public void Reset()
+ {
+ for (int i = 0; i < freqs.Length; i++) {
+ freqs[i] = 0;
+ }
+ codes = null;
+ length = null;
+ }
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public void WriteSymbol(int code)
+ {
+ // if (DeflaterConstants.DEBUGGING) {
+ // freqs[code]--;
+ // // Console.Write("writeSymbol("+freqs.length+","+code+"): ");
+ // }
+ dh.pending.WriteBits(codes[code] & 0xffff, length[code]);
+ }
+
+ /// <summary>
+ /// Check that at least one frequency is non-zero
+ /// </summary>
+ /// <exception cref="SharpZipBaseException">
+ /// No frequencies are non-zero
+ /// </exception>
+ public void CheckEmpty()
+ {
+ bool empty = true;
+ for (int i = 0; i < freqs.Length; i++) {
+ if (freqs[i] != 0) {
+ //Console.WriteLine("freqs[" + i + "] == " + freqs[i]);
+ empty = false;
+ }
+ }
+
+ if (!empty) {
+ throw new SharpZipBaseException("!Empty");
+ }
+ //Console.WriteLine("checkEmpty suceeded!");
+ }
+
+ /// <summary>
+ /// Set static codes and length
+ /// </summary>
+ /// <param name="stCodes">new codes</param>
+ /// <param name="stLength">length for new codes</param>
+ public void SetStaticCodes(short[] stCodes, byte[] stLength)
+ {
+ codes = stCodes;
+ length = stLength;
+ }
+
+ /// <summary>
+ /// Build dynamic codes and lengths
+ /// </summary>
+ public void BuildCodes()
+ {
+ int numSymbols = freqs.Length;
+ int[] nextCode = new int[maxLength];
+ int code = 0;
+ codes = new short[freqs.Length];
+
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("buildCodes: "+freqs.Length);
+ // }
+
+ for (int bits = 0; bits < maxLength; bits++) {
+ nextCode[bits] = code;
+ code += bl_counts[bits] << (15 - bits);
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("bits: " + ( bits + 1) + " count: " + bl_counts[bits]
+ // +" nextCode: "+code);
+ // }
+ }
+ if (DeflaterConstants.DEBUGGING && code != 65536) {
+ throw new SharpZipBaseException("Inconsistent bl_counts!");
+ }
+
+ for (int i=0; i < numCodes; i++) {
+ int bits = length[i];
+ if (bits > 0) {
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("codes["+i+"] = rev(" + nextCode[bits-1]+"),
+ // +bits);
+ // }
+ codes[i] = BitReverse(nextCode[bits-1]);
+ nextCode[bits-1] += 1 << (16 - bits);
+ }
+ }
+ }
+
+ void BuildLength(int[] childs)
+ {
+ this.length = new byte [freqs.Length];
+ int numNodes = childs.Length / 2;
+ int numLeafs = (numNodes + 1) / 2;
+ int overflow = 0;
+
+ for (int i = 0; i < maxLength; i++) {
+ bl_counts[i] = 0;
+ }
+
+ /* First calculate optimal bit lengths */
+ int[] lengths = new int[numNodes];
+ lengths[numNodes-1] = 0;
+
+ for (int i = numNodes - 1; i >= 0; i--) {
+ if (childs[2*i+1] != -1) {
+ int bitLength = lengths[i] + 1;
+ if (bitLength > maxLength) {
+ bitLength = maxLength;
+ overflow++;
+ }
+ lengths[childs[2*i]] = lengths[childs[2*i+1]] = bitLength;
+ } else {
+ /* A leaf node */
+ int bitLength = lengths[i];
+ bl_counts[bitLength - 1]++;
+ this.length[childs[2*i]] = (byte) lengths[i];
+ }
+ }
+
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("Tree "+freqs.Length+" lengths:");
+ // for (int i=0; i < numLeafs; i++) {
+ // //Console.WriteLine("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]]
+ // + " len: "+length[childs[2*i]]);
+ // }
+ // }
+
+ if (overflow == 0) {
+ return;
+ }
+
+ int incrBitLen = maxLength - 1;
+ do {
+ /* Find the first bit length which could increase: */
+ while (bl_counts[--incrBitLen] == 0)
+ ;
+
+ /* Move this node one down and remove a corresponding
+ * amount of overflow nodes.
+ */
+ do {
+ bl_counts[incrBitLen]--;
+ bl_counts[++incrBitLen]++;
+ overflow -= 1 << (maxLength - 1 - incrBitLen);
+ } while (overflow > 0 && incrBitLen < maxLength - 1);
+ } while (overflow > 0);
+
+ /* We may have overshot above. Move some nodes from maxLength to
+ * maxLength-1 in that case.
+ */
+ bl_counts[maxLength-1] += overflow;
+ bl_counts[maxLength-2] -= overflow;
+
+ /* Now recompute all bit lengths, scanning in increasing
+ * frequency. It is simpler to reconstruct all lengths instead of
+ * fixing only the wrong ones. This idea is taken from 'ar'
+ * written by Haruhiko Okumura.
+ *
+ * The nodes were inserted with decreasing frequency into the childs
+ * array.
+ */
+ int nodePtr = 2 * numLeafs;
+ for (int bits = maxLength; bits != 0; bits--) {
+ int n = bl_counts[bits-1];
+ while (n > 0) {
+ int childPtr = 2*childs[nodePtr++];
+ if (childs[childPtr + 1] == -1) {
+ /* We found another leaf */
+ length[childs[childPtr]] = (byte) bits;
+ n--;
+ }
+ }
+ }
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("*** After overflow elimination. ***");
+ // for (int i=0; i < numLeafs; i++) {
+ // //Console.WriteLine("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]]
+ // + " len: "+length[childs[2*i]]);
+ // }
+ // }
+ }
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public void BuildTree()
+ {
+ int numSymbols = freqs.Length;
+
+ /* heap is a priority queue, sorted by frequency, least frequent
+ * nodes first. The heap is a binary tree, with the property, that
+ * the parent node is smaller than both child nodes. This assures
+ * that the smallest node is the first parent.
+ *
+ * The binary tree is encoded in an array: 0 is root node and
+ * the nodes 2*n+1, 2*n+2 are the child nodes of node n.
+ */
+ int[] heap = new int[numSymbols];
+ int heapLen = 0;
+ int maxCode = 0;
+ for (int n = 0; n < numSymbols; n++) {
+ int freq = freqs[n];
+ if (freq != 0) {
+ /* Insert n into heap */
+ int pos = heapLen++;
+ int ppos;
+ while (pos > 0 && freqs[heap[ppos = (pos - 1) / 2]] > freq) {
+ heap[pos] = heap[ppos];
+ pos = ppos;
+ }
+ heap[pos] = n;
+
+ maxCode = n;
+ }
+ }
+
+ /* We could encode a single literal with 0 bits but then we
+ * don't see the literals. Therefore we force at least two
+ * literals to avoid this case. We don't care about order in
+ * this case, both literals get a 1 bit code.
+ */
+ while (heapLen < 2) {
+ int node = maxCode < 2 ? ++maxCode : 0;
+ heap[heapLen++] = node;
+ }
+
+ numCodes = Math.Max(maxCode + 1, minNumCodes);
+
+ int numLeafs = heapLen;
+ int[] childs = new int[4*heapLen - 2];
+ int[] values = new int[2*heapLen - 1];
+ int numNodes = numLeafs;
+ for (int i = 0; i < heapLen; i++) {
+ int node = heap[i];
+ childs[2*i] = node;
+ childs[2*i+1] = -1;
+ values[i] = freqs[node] << 8;
+ heap[i] = i;
+ }
+
+ /* Construct the Huffman tree by repeatedly combining the least two
+ * frequent nodes.
+ */
+ do {
+ int first = heap[0];
+ int last = heap[--heapLen];
+
+ /* Propagate the hole to the leafs of the heap */
+ int ppos = 0;
+ int path = 1;
+
+ while (path < heapLen) {
+ if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) {
+ path++;
+ }
+
+ heap[ppos] = heap[path];
+ ppos = path;
+ path = path * 2 + 1;
+ }
+
+ /* Now propagate the last element down along path. Normally
+ * it shouldn't go too deep.
+ */
+ int lastVal = values[last];
+ while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) {
+ heap[path] = heap[ppos];
+ }
+ heap[path] = last;
+
+
+ int second = heap[0];
+
+ /* Create a new node father of first and second */
+ last = numNodes++;
+ childs[2*last] = first;
+ childs[2*last+1] = second;
+ int mindepth = Math.Min(values[first] & 0xff, values[second] & 0xff);
+ values[last] = lastVal = values[first] + values[second] - mindepth + 1;
+
+ /* Again, propagate the hole to the leafs */
+ ppos = 0;
+ path = 1;
+
+ while (path < heapLen) {
+ if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) {
+ path++;
+ }
+
+ heap[ppos] = heap[path];
+ ppos = path;
+ path = ppos * 2 + 1;
+ }
+
+ /* Now propagate the new element down along path */
+ while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) {
+ heap[path] = heap[ppos];
+ }
+ heap[path] = last;
+ } while (heapLen > 1);
+
+ if (heap[0] != childs.Length / 2 - 1) {
+ throw new SharpZipBaseException("Heap invariant violated");
+ }
+
+ BuildLength(childs);
+ }
+
+ /// <summary>
+ /// Get encoded length
+ /// </summary>
+ /// <returns>Encoded length, the sum of frequencies * lengths</returns>
+ public int GetEncodedLength()
+ {
+ int len = 0;
+ for (int i = 0; i < freqs.Length; i++) {
+ len += freqs[i] * length[i];
+ }
+ return len;
+ }
+
+ /// <summary>
+ /// Not documented
+ /// </summary>
+ public void CalcBLFreq(Tree blTree)
+ {
+ int max_count; /* max repeat count */
+ int min_count; /* min repeat count */
+ int count; /* repeat count of the current code */
+ int curlen = -1; /* length of current code */
+
+ int i = 0;
+ while (i < numCodes) {
+ count = 1;
+ int nextlen = length[i];
+ if (nextlen == 0) {
+ max_count = 138;
+ min_count = 3;
+ } else {
+ max_count = 6;
+ min_count = 3;
+ if (curlen != nextlen) {
+ blTree.freqs[nextlen]++;
+ count = 0;
+ }
+ }
+ curlen = nextlen;
+ i++;
+
+ while (i < numCodes && curlen == length[i]) {
+ i++;
+ if (++count >= max_count) {
+ break;
+ }
+ }
+
+ if (count < min_count) {
+ blTree.freqs[curlen] += (short)count;
+ } else if (curlen != 0) {
+ blTree.freqs[REP_3_6]++;
+ } else if (count <= 10) {
+ blTree.freqs[REP_3_10]++;
+ } else {
+ blTree.freqs[REP_11_138]++;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Write tree values
+ /// </summary>
+ /// <param name="blTree">Tree to write</param>
+ public void WriteTree(Tree blTree)
+ {
+ int max_count; /* max repeat count */
+ int min_count; /* min repeat count */
+ int count; /* repeat count of the current code */
+ int curlen = -1; /* length of current code */
+
+ int i = 0;
+ while (i < numCodes) {
+ count = 1;
+ int nextlen = length[i];
+ if (nextlen == 0) {
+ max_count = 138;
+ min_count = 3;
+ } else {
+ max_count = 6;
+ min_count = 3;
+ if (curlen != nextlen) {
+ blTree.WriteSymbol(nextlen);
+ count = 0;
+ }
+ }
+ curlen = nextlen;
+ i++;
+
+ while (i < numCodes && curlen == length[i]) {
+ i++;
+ if (++count >= max_count) {
+ break;
+ }
+ }
+
+ if (count < min_count) {
+ while (count-- > 0) {
+ blTree.WriteSymbol(curlen);
+ }
+ } else if (curlen != 0) {
+ blTree.WriteSymbol(REP_3_6);
+ dh.pending.WriteBits(count - 3, 2);
+ } else if (count <= 10) {
+ blTree.WriteSymbol(REP_3_10);
+ dh.pending.WriteBits(count - 3, 3);
+ } else {
+ blTree.WriteSymbol(REP_11_138);
+ dh.pending.WriteBits(count - 11, 7);
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Pending buffer to use
+ /// </summary>
+ public DeflaterPending pending;
+
+ Tree literalTree, distTree, blTree;
+
+ short[] d_buf;
+ byte[] l_buf;
+ int last_lit;
+ int extra_bits;
+
+ static short[] staticLCodes;
+ static byte[] staticLLength;
+ static short[] staticDCodes;
+ static byte[] staticDLength;
+
+ /// <summary>
+ /// Reverse the bits of a 16 bit value.
+ /// </summary>
+ /// <param name="toReverse">Value to reverse bits</param>
+ /// <returns>Value with bits reversed</returns>
+ public static short BitReverse(int toReverse)
+ {
+ return (short) (bit4Reverse[toReverse & 0xF] << 12 |
+ bit4Reverse[(toReverse >> 4) & 0xF] << 8 |
+ bit4Reverse[(toReverse >> 8) & 0xF] << 4 |
+ bit4Reverse[toReverse >> 12]);
+ }
+
+
+ static DeflaterHuffman()
+ {
+ /* See RFC 1951 3.2.6 */
+ /* Literal codes */
+ staticLCodes = new short[LITERAL_NUM];
+ staticLLength = new byte[LITERAL_NUM];
+ int i = 0;
+ while (i < 144) {
+ staticLCodes[i] = BitReverse((0x030 + i) << 8);
+ staticLLength[i++] = 8;
+ }
+ while (i < 256) {
+ staticLCodes[i] = BitReverse((0x190 - 144 + i) << 7);
+ staticLLength[i++] = 9;
+ }
+ while (i < 280) {
+ staticLCodes[i] = BitReverse((0x000 - 256 + i) << 9);
+ staticLLength[i++] = 7;
+ }
+ while (i < LITERAL_NUM) {
+ staticLCodes[i] = BitReverse((0x0c0 - 280 + i) << 8);
+ staticLLength[i++] = 8;
+ }
+
+ /* Distant codes */
+ staticDCodes = new short[DIST_NUM];
+ staticDLength = new byte[DIST_NUM];
+ for (i = 0; i < DIST_NUM; i++) {
+ staticDCodes[i] = BitReverse(i << 11);
+ staticDLength[i] = 5;
+ }
+ }
+
+ /// <summary>
+ /// Construct instance with pending buffer
+ /// </summary>
+ /// <param name="pending">Pending buffer to use</param>
+ public DeflaterHuffman(DeflaterPending pending)
+ {
+ this.pending = pending;
+
+ literalTree = new Tree(this, LITERAL_NUM, 257, 15);
+ distTree = new Tree(this, DIST_NUM, 1, 15);
+ blTree = new Tree(this, BITLEN_NUM, 4, 7);
+
+ d_buf = new short[BUFSIZE];
+ l_buf = new byte [BUFSIZE];
+ }
+
+ /// <summary>
+ /// Reset internal state
+ /// </summary>
+ public void Reset()
+ {
+ last_lit = 0;
+ extra_bits = 0;
+ literalTree.Reset();
+ distTree.Reset();
+ blTree.Reset();
+ }
+
+ int Lcode(int len)
+ {
+ if (len == 255) {
+ return 285;
+ }
+
+ int code = 257;
+ while (len >= 8) {
+ code += 4;
+ len >>= 1;
+ }
+ return code + len;
+ }
+
+ int Dcode(int distance)
+ {
+ int code = 0;
+ while (distance >= 4) {
+ code += 2;
+ distance >>= 1;
+ }
+ return code + distance;
+ }
+
+ /// <summary>
+ /// Write all trees to pending buffer
+ /// </summary>
+ public void SendAllTrees(int blTreeCodes)
+ {
+ blTree.BuildCodes();
+ literalTree.BuildCodes();
+ distTree.BuildCodes();
+ pending.WriteBits(literalTree.numCodes - 257, 5);
+ pending.WriteBits(distTree.numCodes - 1, 5);
+ pending.WriteBits(blTreeCodes - 4, 4);
+ for (int rank = 0; rank < blTreeCodes; rank++) {
+ pending.WriteBits(blTree.length[BL_ORDER[rank]], 3);
+ }
+ literalTree.WriteTree(blTree);
+ distTree.WriteTree(blTree);
+ // if (DeflaterConstants.DEBUGGING) {
+ // blTree.CheckEmpty();
+ // }
+ }
+
+ /// <summary>
+ /// Compress current buffer writing data to pending buffer
+ /// </summary>
+ public void CompressBlock()
+ {
+ for (int i = 0; i < last_lit; i++) {
+ int litlen = l_buf[i] & 0xff;
+ int dist = d_buf[i];
+ if (dist-- != 0) {
+ // if (DeflaterConstants.DEBUGGING) {
+ // Console.Write("["+(dist+1)+","+(litlen+3)+"]: ");
+ // }
+
+ int lc = Lcode(litlen);
+ literalTree.WriteSymbol(lc);
+
+ int bits = (lc - 261) / 4;
+ if (bits > 0 && bits <= 5) {
+ pending.WriteBits(litlen & ((1 << bits) - 1), bits);
+ }
+
+ int dc = Dcode(dist);
+ distTree.WriteSymbol(dc);
+
+ bits = dc / 2 - 1;
+ if (bits > 0) {
+ pending.WriteBits(dist & ((1 << bits) - 1), bits);
+ }
+ } else {
+ // if (DeflaterConstants.DEBUGGING) {
+ // if (litlen > 32 && litlen < 127) {
+ // Console.Write("("+(char)litlen+"): ");
+ // } else {
+ // Console.Write("{"+litlen+"}: ");
+ // }
+ // }
+ literalTree.WriteSymbol(litlen);
+ }
+ }
+ // if (DeflaterConstants.DEBUGGING) {
+ // Console.Write("EOF: ");
+ // }
+ literalTree.WriteSymbol(EOF_SYMBOL);
+ // if (DeflaterConstants.DEBUGGING) {
+ // literalTree.CheckEmpty();
+ // distTree.CheckEmpty();
+ // }
+ }
+
+ /// <summary>
+ /// Flush block to output with no compression
+ /// </summary>
+ /// <param name="stored">Data to write</param>
+ /// <param name="storedOffset">Index of first byte to write</param>
+ /// <param name="storedLength">Count of bytes to write</param>
+ /// <param name="lastBlock">True if this is the last block</param>
+ public void FlushStoredBlock(byte[] stored, int storedOffset, int storedLength, bool lastBlock)
+ {
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("Flushing stored block "+ storedLength);
+ // }
+ pending.WriteBits((DeflaterConstants.STORED_BLOCK << 1) + (lastBlock ? 1 : 0), 3);
+ pending.AlignToByte();
+ pending.WriteShort(storedLength);
+ pending.WriteShort(~storedLength);
+ pending.WriteBlock(stored, storedOffset, storedLength);
+ Reset();
+ }
+
+ /// <summary>
+ /// Flush block to output with compression
+ /// </summary>
+ /// <param name="stored">Data to flush</param>
+ /// <param name="storedOffset">Index of first byte to flush</param>
+ /// <param name="storedLength">Count of bytes to flush</param>
+ /// <param name="lastBlock">True if this is the last block</param>
+ public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool lastBlock)
+ {
+ literalTree.freqs[EOF_SYMBOL]++;
+
+ /* Build trees */
+ literalTree.BuildTree();
+ distTree.BuildTree();
+
+ /* Calculate bitlen frequency */
+ literalTree.CalcBLFreq(blTree);
+ distTree.CalcBLFreq(blTree);
+
+ /* Build bitlen tree */
+ blTree.BuildTree();
+
+ int blTreeCodes = 4;
+ for (int i = 18; i > blTreeCodes; i--) {
+ if (blTree.length[BL_ORDER[i]] > 0) {
+ blTreeCodes = i+1;
+ }
+ }
+ int opt_len = 14 + blTreeCodes * 3 + blTree.GetEncodedLength() +
+ literalTree.GetEncodedLength() + distTree.GetEncodedLength() +
+ extra_bits;
+
+ int static_len = extra_bits;
+ for (int i = 0; i < LITERAL_NUM; i++) {
+ static_len += literalTree.freqs[i] * staticLLength[i];
+ }
+ for (int i = 0; i < DIST_NUM; i++) {
+ static_len += distTree.freqs[i] * staticDLength[i];
+ }
+ if (opt_len >= static_len) {
+ /* Force static trees */
+ opt_len = static_len;
+ }
+
+ if (storedOffset >= 0 && (storedLength + 4 < (opt_len >> 3))) {
+ /* Store Block */
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("Storing, since " + storedLength + " < " + opt_len
+ // + " <= " + static_len);
+ // }
+ FlushStoredBlock(stored, storedOffset, storedLength, lastBlock);
+ } else if (opt_len == static_len) {
+ /* Encode with static tree */
+ pending.WriteBits((DeflaterConstants.STATIC_TREES << 1) + (lastBlock ? 1 : 0), 3);
+ literalTree.SetStaticCodes(staticLCodes, staticLLength);
+ distTree.SetStaticCodes(staticDCodes, staticDLength);
+ CompressBlock();
+ Reset();
+ } else {
+ /* Encode with dynamic tree */
+ pending.WriteBits((DeflaterConstants.DYN_TREES << 1) + (lastBlock ? 1 : 0), 3);
+ SendAllTrees(blTreeCodes);
+ CompressBlock();
+ Reset();
+ }
+ }
+
+ /// <summary>
+ /// Get value indicating if internal buffer is full
+ /// </summary>
+ /// <returns>true if buffer is full</returns>
+ public bool IsFull()
+ {
+ return last_lit >= BUFSIZE;
+ }
+
+ /// <summary>
+ /// Add literal to buffer
+ /// </summary>
+ /// <param name="lit"></param>
+ /// <returns>Value indicating internal buffer is full</returns>
+ public bool TallyLit(int lit)
+ {
+ // if (DeflaterConstants.DEBUGGING) {
+ // if (lit > 32 && lit < 127) {
+ // //Console.WriteLine("("+(char)lit+")");
+ // } else {
+ // //Console.WriteLine("{"+lit+"}");
+ // }
+ // }
+ d_buf[last_lit] = 0;
+ l_buf[last_lit++] = (byte)lit;
+ literalTree.freqs[lit]++;
+ return IsFull();
+ }
+
+ /// <summary>
+ /// Add distance code and length to literal and distance trees
+ /// </summary>
+ /// <param name="dist">Distance code</param>
+ /// <param name="len">Length</param>
+ /// <returns>Value indicating if internal buffer is full</returns>
+ public bool TallyDist(int dist, int len)
+ {
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("["+dist+","+len+"]");
+ // }
+
+ d_buf[last_lit] = (short)dist;
+ l_buf[last_lit++] = (byte)(len - 3);
+
+ int lc = Lcode(len - 3);
+ literalTree.freqs[lc]++;
+ if (lc >= 265 && lc < 285) {
+ extra_bits += (lc - 261) / 4;
+ }
+
+ int dc = Dcode(dist - 1);
+ distTree.freqs[dc]++;
+ if (dc >= 4) {
+ extra_bits += dc / 2 - 1;
+ }
+ return IsFull();
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs
new file mode 100644
index 00000000000..1eb5d29d138
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs
@@ -0,0 +1,57 @@
+// DeflaterPending.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// This class stores the pending output of the Deflater.
+ ///
+ /// author of the original java version : Jochen Hoenicke
+ /// </summary>
+ public class DeflaterPending : PendingBuffer
+ {
+ /// <summary>
+ /// Construct instance with default buffer size
+ /// </summary>
+ public DeflaterPending() : base(DeflaterConstants.PENDING_BUF_SIZE)
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs
new file mode 100644
index 00000000000..36c423ae784
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs
@@ -0,0 +1,817 @@
+// Inflater.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// Inflater is used to decompress data that has been compressed according
+ /// to the "deflate" standard described in rfc1951.
+ ///
+ /// By default Zlib (rfc1950) headers and footers are expected in the input.
+ /// You can use constructor <code> public Inflater(bool noHeader)</code> passing true
+ /// if there is no Zlib header information
+ ///
+ /// The usage is as following. First you have to set some input with
+ /// <code>setInput()</code>, then inflate() it. If inflate doesn't
+ /// inflate any bytes there may be three reasons:
+ /// <ul>
+ /// <li>needsInput() returns true because the input buffer is empty.
+ /// You have to provide more input with <code>setInput()</code>.
+ /// NOTE: needsInput() also returns true when, the stream is finished.
+ /// </li>
+ /// <li>needsDictionary() returns true, you have to provide a preset
+ /// dictionary with <code>setDictionary()</code>.</li>
+ /// <li>finished() returns true, the inflater has finished.</li>
+ /// </ul>
+ /// Once the first output byte is produced, a dictionary will not be
+ /// needed at a later stage.
+ ///
+ /// author of the original java version : John Leuner, Jochen Hoenicke
+ /// </summary>
+ public class Inflater
+ {
+ /// <summary>
+ /// Copy lengths for literal codes 257..285
+ /// </summary>
+ static int[] CPLENS = {
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
+ };
+
+ /// <summary>
+ /// Extra bits for literal codes 257..285
+ /// </summary>
+ static int[] CPLEXT = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
+ 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
+ };
+
+ /// <summary>
+ /// Copy offsets for distance codes 0..29
+ /// </summary>
+ static int[] CPDIST = {
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577
+ };
+
+ /// <summary>
+ /// Extra bits for distance codes
+ /// </summary>
+ static int[] CPDEXT = {
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
+ 12, 12, 13, 13
+ };
+
+ /// <summary>
+ /// These are the possible states for an inflater
+ /// </summary>
+ const int DECODE_HEADER = 0;
+ const int DECODE_DICT = 1;
+ const int DECODE_BLOCKS = 2;
+ const int DECODE_STORED_LEN1 = 3;
+ const int DECODE_STORED_LEN2 = 4;
+ const int DECODE_STORED = 5;
+ const int DECODE_DYN_HEADER = 6;
+ const int DECODE_HUFFMAN = 7;
+ const int DECODE_HUFFMAN_LENBITS = 8;
+ const int DECODE_HUFFMAN_DIST = 9;
+ const int DECODE_HUFFMAN_DISTBITS = 10;
+ const int DECODE_CHKSUM = 11;
+ const int FINISHED = 12;
+
+ /// <summary>
+ /// This variable contains the current state.
+ /// </summary>
+ int mode;
+
+ /// <summary>
+ /// The adler checksum of the dictionary or of the decompressed
+ /// stream, as it is written in the header resp. footer of the
+ /// compressed stream.
+ /// Only valid if mode is DECODE_DICT or DECODE_CHKSUM.
+ /// </summary>
+ int readAdler;
+
+ /// <summary>
+ /// The number of bits needed to complete the current state. This
+ /// is valid, if mode is DECODE_DICT, DECODE_CHKSUM,
+ /// DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS.
+ /// </summary>
+ int neededBits;
+ int repLength;
+ int repDist;
+ int uncomprLen;
+
+ /// <summary>
+ /// True, if the last block flag was set in the last block of the
+ /// inflated stream. This means that the stream ends after the
+ /// current block.
+ /// </summary>
+ bool isLastBlock;
+
+ /// <summary>
+ /// The total number of inflated bytes.
+ /// </summary>
+ int totalOut;
+
+ /// <summary>
+ /// The total number of bytes set with setInput(). This is not the
+ /// value returned by the TotalIn property, since this also includes the
+ /// unprocessed input.
+ /// </summary>
+ int totalIn;
+
+ /// <summary>
+ /// This variable stores the noHeader flag that was given to the constructor.
+ /// True means, that the inflated stream doesn't contain a Zlib header or
+ /// footer.
+ /// </summary>
+ bool noHeader;
+
+ StreamManipulator input;
+ OutputWindow outputWindow;
+ InflaterDynHeader dynHeader;
+ InflaterHuffmanTree litlenTree, distTree;
+ Adler32 adler;
+
+ /// <summary>
+ /// Creates a new inflater or RFC1951 decompressor
+ /// RFC1950/Zlib headers and footers will be expected in the input data
+ /// </summary>
+ public Inflater() : this(false)
+ {
+ }
+
+ /// <summary>
+ /// Creates a new inflater.
+ /// </summary>
+ /// <param name="noHeader">
+ /// True if no RFC1950/Zlib header and footer fields are expected in the input data
+ ///
+ /// This is used for GZIPed/Zipped input.
+ ///
+ /// For compatibility with
+ /// Sun JDK you should provide one byte of input more than needed in
+ /// this case.
+ /// </param>
+ public Inflater(bool noHeader)
+ {
+ this.noHeader = noHeader;
+ this.adler = new Adler32();
+ input = new StreamManipulator();
+ outputWindow = new OutputWindow();
+ mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER;
+ }
+
+ /// <summary>
+ /// Resets the inflater so that a new stream can be decompressed. All
+ /// pending input and output will be discarded.
+ /// </summary>
+ public void Reset()
+ {
+ mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER;
+ totalIn = totalOut = 0;
+ input.Reset();
+ outputWindow.Reset();
+ dynHeader = null;
+ litlenTree = null;
+ distTree = null;
+ isLastBlock = false;
+ adler.Reset();
+ }
+
+ /// <summary>
+ /// Decodes a zlib/RFC1950 header.
+ /// </summary>
+ /// <returns>
+ /// False if more input is needed.
+ /// </returns>
+ /// <exception cref="SharpZipBaseException">
+ /// The header is invalid.
+ /// </exception>
+ private bool DecodeHeader()
+ {
+ int header = input.PeekBits(16);
+ if (header < 0) {
+ return false;
+ }
+ input.DropBits(16);
+
+ /* The header is written in "wrong" byte order */
+ header = ((header << 8) | (header >> 8)) & 0xffff;
+ if (header % 31 != 0) {
+ throw new SharpZipBaseException("Header checksum illegal");
+ }
+
+ if ((header & 0x0f00) != (Deflater.DEFLATED << 8)) {
+ throw new SharpZipBaseException("Compression Method unknown");
+ }
+
+ /* Maximum size of the backwards window in bits.
+ * We currently ignore this, but we could use it to make the
+ * inflater window more space efficient. On the other hand the
+ * full window (15 bits) is needed most times, anyway.
+ int max_wbits = ((header & 0x7000) >> 12) + 8;
+ */
+
+ if ((header & 0x0020) == 0) { // Dictionary flag?
+ mode = DECODE_BLOCKS;
+ } else {
+ mode = DECODE_DICT;
+ neededBits = 32;
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Decodes the dictionary checksum after the deflate header.
+ /// </summary>
+ /// <returns>
+ /// False if more input is needed.
+ /// </returns>
+ private bool DecodeDict()
+ {
+ while (neededBits > 0) {
+ int dictByte = input.PeekBits(8);
+ if (dictByte < 0) {
+ return false;
+ }
+ input.DropBits(8);
+ readAdler = (readAdler << 8) | dictByte;
+ neededBits -= 8;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Decodes the huffman encoded symbols in the input stream.
+ /// </summary>
+ /// <returns>
+ /// false if more input is needed, true if output window is
+ /// full or the current block ends.
+ /// </returns>
+ /// <exception cref="SharpZipBaseException">
+ /// if deflated stream is invalid.
+ /// </exception>
+ private bool DecodeHuffman()
+ {
+ int free = outputWindow.GetFreeSpace();
+ while (free >= 258) {
+ int symbol;
+ switch (mode) {
+ case DECODE_HUFFMAN:
+ /* This is the inner loop so it is optimized a bit */
+ while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) {
+ outputWindow.Write(symbol);
+ if (--free < 258) {
+ return true;
+ }
+ }
+
+ if (symbol < 257) {
+ if (symbol < 0) {
+ return false;
+ } else {
+ /* symbol == 256: end of block */
+ distTree = null;
+ litlenTree = null;
+ mode = DECODE_BLOCKS;
+ return true;
+ }
+ }
+
+ try {
+ repLength = CPLENS[symbol - 257];
+ neededBits = CPLEXT[symbol - 257];
+ } catch (Exception) {
+ throw new SharpZipBaseException("Illegal rep length code");
+ }
+ goto case DECODE_HUFFMAN_LENBITS; /* fall through */
+
+ case DECODE_HUFFMAN_LENBITS:
+ if (neededBits > 0) {
+ mode = DECODE_HUFFMAN_LENBITS;
+ int i = input.PeekBits(neededBits);
+ if (i < 0) {
+ return false;
+ }
+ input.DropBits(neededBits);
+ repLength += i;
+ }
+ mode = DECODE_HUFFMAN_DIST;
+ goto case DECODE_HUFFMAN_DIST;/* fall through */
+
+ case DECODE_HUFFMAN_DIST:
+ symbol = distTree.GetSymbol(input);
+ if (symbol < 0) {
+ return false;
+ }
+
+ try {
+ repDist = CPDIST[symbol];
+ neededBits = CPDEXT[symbol];
+ } catch (Exception) {
+ throw new SharpZipBaseException("Illegal rep dist code");
+ }
+
+ goto case DECODE_HUFFMAN_DISTBITS;/* fall through */
+
+ case DECODE_HUFFMAN_DISTBITS:
+ if (neededBits > 0) {
+ mode = DECODE_HUFFMAN_DISTBITS;
+ int i = input.PeekBits(neededBits);
+ if (i < 0) {
+ return false;
+ }
+ input.DropBits(neededBits);
+ repDist += i;
+ }
+
+ outputWindow.Repeat(repLength, repDist);
+ free -= repLength;
+ mode = DECODE_HUFFMAN;
+ break;
+
+ default:
+ throw new SharpZipBaseException("Inflater unknown mode");
+ }
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Decodes the adler checksum after the deflate stream.
+ /// </summary>
+ /// <returns>
+ /// false if more input is needed.
+ /// </returns>
+ /// <exception cref="SharpZipBaseException">
+ /// If checksum doesn't match.
+ /// </exception>
+ private bool DecodeChksum()
+ {
+ while (neededBits > 0) {
+ int chkByte = input.PeekBits(8);
+ if (chkByte < 0) {
+ return false;
+ }
+ input.DropBits(8);
+ readAdler = (readAdler << 8) | chkByte;
+ neededBits -= 8;
+ }
+ if ((int) adler.Value != readAdler) {
+ throw new SharpZipBaseException("Adler chksum doesn't match: " + (int)adler.Value + " vs. " + readAdler);
+ }
+ mode = FINISHED;
+ return false;
+ }
+
+ /// <summary>
+ /// Decodes the deflated stream.
+ /// </summary>
+ /// <returns>
+ /// false if more input is needed, or if finished.
+ /// </returns>
+ /// <exception cref="SharpZipBaseException">
+ /// if deflated stream is invalid.
+ /// </exception>
+ private bool Decode()
+ {
+ switch (mode) {
+ case DECODE_HEADER:
+ return DecodeHeader();
+ case DECODE_DICT:
+ return DecodeDict();
+ case DECODE_CHKSUM:
+ return DecodeChksum();
+
+ case DECODE_BLOCKS:
+ if (isLastBlock) {
+ if (noHeader) {
+ mode = FINISHED;
+ return false;
+ } else {
+ input.SkipToByteBoundary();
+ neededBits = 32;
+ mode = DECODE_CHKSUM;
+ return true;
+ }
+ }
+
+ int type = input.PeekBits(3);
+ if (type < 0) {
+ return false;
+ }
+ input.DropBits(3);
+
+ if ((type & 1) != 0) {
+ isLastBlock = true;
+ }
+ switch (type >> 1){
+ case DeflaterConstants.STORED_BLOCK:
+ input.SkipToByteBoundary();
+ mode = DECODE_STORED_LEN1;
+ break;
+ case DeflaterConstants.STATIC_TREES:
+ litlenTree = InflaterHuffmanTree.defLitLenTree;
+ distTree = InflaterHuffmanTree.defDistTree;
+ mode = DECODE_HUFFMAN;
+ break;
+ case DeflaterConstants.DYN_TREES:
+ dynHeader = new InflaterDynHeader();
+ mode = DECODE_DYN_HEADER;
+ break;
+ default:
+ throw new SharpZipBaseException("Unknown block type " + type);
+ }
+ return true;
+
+ case DECODE_STORED_LEN1:
+ {
+ if ((uncomprLen = input.PeekBits(16)) < 0) {
+ return false;
+ }
+ input.DropBits(16);
+ mode = DECODE_STORED_LEN2;
+ }
+ goto case DECODE_STORED_LEN2; /* fall through */
+
+ case DECODE_STORED_LEN2:
+ {
+ int nlen = input.PeekBits(16);
+ if (nlen < 0) {
+ return false;
+ }
+ input.DropBits(16);
+ if (nlen != (uncomprLen ^ 0xffff)) {
+ throw new SharpZipBaseException("broken uncompressed block");
+ }
+ mode = DECODE_STORED;
+ }
+ goto case DECODE_STORED;/* fall through */
+
+ case DECODE_STORED:
+ {
+ int more = outputWindow.CopyStored(input, uncomprLen);
+ uncomprLen -= more;
+ if (uncomprLen == 0) {
+ mode = DECODE_BLOCKS;
+ return true;
+ }
+ return !input.IsNeedingInput;
+ }
+
+ case DECODE_DYN_HEADER:
+ if (!dynHeader.Decode(input)) {
+ return false;
+ }
+
+ litlenTree = dynHeader.BuildLitLenTree();
+ distTree = dynHeader.BuildDistTree();
+ mode = DECODE_HUFFMAN;
+ goto case DECODE_HUFFMAN; /* fall through */
+
+ case DECODE_HUFFMAN:
+ case DECODE_HUFFMAN_LENBITS:
+ case DECODE_HUFFMAN_DIST:
+ case DECODE_HUFFMAN_DISTBITS:
+ return DecodeHuffman();
+
+ case FINISHED:
+ return false;
+
+ default:
+ throw new SharpZipBaseException("Inflater.Decode unknown mode");
+ }
+ }
+
+ /// <summary>
+ /// Sets the preset dictionary. This should only be called, if
+ /// needsDictionary() returns true and it should set the same
+ /// dictionary, that was used for deflating. The getAdler()
+ /// function returns the checksum of the dictionary needed.
+ /// </summary>
+ /// <param name="buffer">
+ /// The dictionary.
+ /// </param>
+ public void SetDictionary(byte[] buffer)
+ {
+ SetDictionary(buffer, 0, buffer.Length);
+ }
+
+ /// <summary>
+ /// Sets the preset dictionary. This should only be called, if
+ /// needsDictionary() returns true and it should set the same
+ /// dictionary, that was used for deflating. The getAdler()
+ /// function returns the checksum of the dictionary needed.
+ /// </summary>
+ /// <param name="buffer">
+ /// The dictionary.
+ /// </param>
+ /// <param name="offset">
+ /// The offset into buffer where the dictionary starts.
+ /// </param>
+ /// <param name="len">
+ /// The length of the dictionary.
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// No dictionary is needed.
+ /// </exception>
+ /// <exception cref="SharpZipBaseException">
+ /// The adler checksum for the buffer is invalid
+ /// </exception>
+ public void SetDictionary(byte[] buffer, int offset, int len)
+ {
+ if (!IsNeedingDictionary) {
+ throw new InvalidOperationException();
+ }
+
+ adler.Update(buffer, offset, len);
+ if ((int)adler.Value != readAdler) {
+ throw new SharpZipBaseException("Wrong adler checksum");
+ }
+ adler.Reset();
+ outputWindow.CopyDict(buffer, offset, len);
+ mode = DECODE_BLOCKS;
+ }
+
+ /// <summary>
+ /// Sets the input. This should only be called, if needsInput()
+ /// returns true.
+ /// </summary>
+ /// <param name="buf">
+ /// the input.
+ /// </param>
+ public void SetInput(byte[] buf)
+ {
+ SetInput(buf, 0, buf.Length);
+ }
+
+ /// <summary>
+ /// Sets the input. This should only be called, if needsInput()
+ /// returns true.
+ /// </summary>
+ /// <param name="buffer">
+ /// The source of input data
+ /// </param>
+ /// <param name="offset">
+ /// The offset into buffer where the input starts.
+ /// </param>
+ /// <param name="length">
+ /// The number of bytes of input to use.
+ /// </param>
+ /// <exception cref="System.InvalidOperationException">
+ /// No input is needed.
+ /// </exception>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// The off and/or len are wrong.
+ /// </exception>
+ public void SetInput(byte[] buffer, int offset, int length)
+ {
+ input.SetInput(buffer, offset, length);
+ totalIn += length;
+ }
+
+ /// <summary>
+ /// Inflates the compressed stream to the output buffer. If this
+ /// returns 0, you should check, whether needsDictionary(),
+ /// needsInput() or finished() returns true, to determine why no
+ /// further output is produced.
+ /// </summary>
+ /// <param name = "buf">
+ /// the output buffer.
+ /// </param>
+ /// <returns>
+ /// the number of bytes written to the buffer, 0 if no further
+ /// output can be produced.
+ /// </returns>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// if buf has length 0.
+ /// </exception>
+ /// <exception cref="System.FormatException">
+ /// if deflated stream is invalid.
+ /// </exception>
+ public int Inflate(byte[] buf)
+ {
+ return Inflate(buf, 0, buf.Length);
+ }
+
+ /// <summary>
+ /// Inflates the compressed stream to the output buffer. If this
+ /// returns 0, you should check, whether needsDictionary(),
+ /// needsInput() or finished() returns true, to determine why no
+ /// further output is produced.
+ /// </summary>
+ /// <param name = "buf">
+ /// the output buffer.
+ /// </param>
+ /// <param name = "offset">
+ /// the offset into buffer where the output should start.
+ /// </param>
+ /// <param name = "len">
+ /// the maximum length of the output.
+ /// </param>
+ /// <returns>
+ /// the number of bytes written to the buffer, 0 if no further output can be produced.
+ /// </returns>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// if len is &lt;= 0.
+ /// </exception>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// if the offset and/or len are wrong.
+ /// </exception>
+ /// <exception cref="System.FormatException">
+ /// if deflated stream is invalid.
+ /// </exception>
+ public int Inflate(byte[] buf, int offset, int len)
+ {
+ if (len < 0) {
+ throw new ArgumentOutOfRangeException("len < 0");
+ }
+
+ // Special case: len may be zero
+ if (len == 0) {
+ if (IsFinished == false) { // -jr- 08-Nov-2003 INFLATE_BUG fix..
+ Decode();
+ }
+ return 0;
+ }
+/*
+ // Check for correct buff, off, len triple
+ if (off < 0 || off + len >= buf.Length) {
+ throw new ArgumentException("off/len outside buf bounds");
+ }
+*/
+ int count = 0;
+ int more;
+ do {
+ if (mode != DECODE_CHKSUM) {
+ /* Don't give away any output, if we are waiting for the
+ * checksum in the input stream.
+ *
+ * With this trick we have always:
+ * needsInput() and not finished()
+ * implies more output can be produced.
+ */
+ more = outputWindow.CopyOutput(buf, offset, len);
+ adler.Update(buf, offset, more);
+ offset += more;
+ count += more;
+ totalOut += more;
+ len -= more;
+ if (len == 0) {
+ return count;
+ }
+ }
+ } while (Decode() || (outputWindow.GetAvailable() > 0 && mode != DECODE_CHKSUM));
+ return count;
+ }
+
+ /// <summary>
+ /// Returns true, if the input buffer is empty.
+ /// You should then call setInput().
+ /// NOTE: This method also returns true when the stream is finished.
+ /// </summary>
+ public bool IsNeedingInput {
+ get {
+ return input.IsNeedingInput;
+ }
+ }
+
+ /// <summary>
+ /// Returns true, if a preset dictionary is needed to inflate the input.
+ /// </summary>
+ public bool IsNeedingDictionary {
+ get {
+ return mode == DECODE_DICT && neededBits == 0;
+ }
+ }
+
+ /// <summary>
+ /// Returns true, if the inflater has finished. This means, that no
+ /// input is needed and no output can be produced.
+ /// </summary>
+ public bool IsFinished {
+ get {
+ return mode == FINISHED && outputWindow.GetAvailable() == 0;
+ }
+ }
+
+ /// <summary>
+ /// Gets the adler checksum. This is either the checksum of all
+ /// uncompressed bytes returned by inflate(), or if needsDictionary()
+ /// returns true (and thus no output was yet produced) this is the
+ /// adler checksum of the expected dictionary.
+ /// </summary>
+ /// <returns>
+ /// the adler checksum.
+ /// </returns>
+ public int Adler {
+ get {
+ return IsNeedingDictionary ? readAdler : (int) adler.Value;
+ }
+ }
+
+ /// <summary>
+ /// Gets the total number of output bytes returned by inflate().
+ /// </summary>
+ /// <returns>
+ /// the total number of output bytes.
+ /// </returns>
+ public int TotalOut {
+ get {
+ return totalOut;
+ }
+ }
+
+ /// <summary>
+ /// Gets the total number of processed compressed input bytes.
+ /// </summary>
+ /// <returns>
+ /// The total number of bytes of processed input bytes.
+ /// </returns>
+ public int TotalIn {
+ get {
+ return totalIn - RemainingInput;
+ }
+ }
+
+#if TEST_HAK
+ /// <summary>
+ /// -jr test hak trying to figure out a bug
+ ///</summary>
+ public int UnseenInput {
+ get {
+ return totalIn - ((input.AvailableBits + 7) >> 3);
+ }
+ }
+
+ /// <summary>
+ /// -jr test hak trying to figure out a bug
+ ///</summary>
+ public int PlainTotalIn {
+ get {
+ return totalIn;
+ }
+ }
+#endif
+
+ /// <summary>
+ /// Gets the number of unprocessed input bytes. Useful, if the end of the
+ /// stream is reached and you want to further process the bytes after
+ /// the deflate stream.
+ /// </summary>
+ /// <returns>
+ /// The number of bytes of the input which have not been processed.
+ /// </returns>
+ public int RemainingInput {
+ get {
+ return input.AvailableBytes;
+ }
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs
new file mode 100644
index 00000000000..5ec6c1730e8
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs
@@ -0,0 +1,207 @@
+// InflaterDynHeader.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ class InflaterDynHeader
+ {
+ const int LNUM = 0;
+ const int DNUM = 1;
+ const int BLNUM = 2;
+ const int BLLENS = 3;
+ const int LENS = 4;
+ const int REPS = 5;
+
+ static readonly int[] repMin = { 3, 3, 11 };
+ static readonly int[] repBits = { 2, 3, 7 };
+
+ byte[] blLens;
+ byte[] litdistLens;
+
+ InflaterHuffmanTree blTree;
+
+ int mode;
+ int lnum, dnum, blnum, num;
+ int repSymbol;
+ byte lastLen;
+ int ptr;
+
+ static readonly int[] BL_ORDER =
+ { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
+
+ public InflaterDynHeader()
+ {
+ }
+
+ public bool Decode(StreamManipulator input)
+ {
+ decode_loop:
+ for (;;) {
+ switch (mode) {
+ case LNUM:
+ lnum = input.PeekBits(5);
+ if (lnum < 0) {
+ return false;
+ }
+ lnum += 257;
+ input.DropBits(5);
+ // System.err.println("LNUM: "+lnum);
+ mode = DNUM;
+ goto case DNUM; // fall through
+ case DNUM:
+ dnum = input.PeekBits(5);
+ if (dnum < 0) {
+ return false;
+ }
+ dnum++;
+ input.DropBits(5);
+ // System.err.println("DNUM: "+dnum);
+ num = lnum+dnum;
+ litdistLens = new byte[num];
+ mode = BLNUM;
+ goto case BLNUM; // fall through
+ case BLNUM:
+ blnum = input.PeekBits(4);
+ if (blnum < 0) {
+ return false;
+ }
+ blnum += 4;
+ input.DropBits(4);
+ blLens = new byte[19];
+ ptr = 0;
+ // System.err.println("BLNUM: "+blnum);
+ mode = BLLENS;
+ goto case BLLENS; // fall through
+ case BLLENS:
+ while (ptr < blnum) {
+ int len = input.PeekBits(3);
+ if (len < 0) {
+ return false;
+ }
+ input.DropBits(3);
+ // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
+ blLens[BL_ORDER[ptr]] = (byte) len;
+ ptr++;
+ }
+ blTree = new InflaterHuffmanTree(blLens);
+ blLens = null;
+ ptr = 0;
+ mode = LENS;
+ goto case LENS; // fall through
+ case LENS:
+ {
+ int symbol;
+ while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
+ /* Normal case: symbol in [0..15] */
+
+ // System.err.println("litdistLens["+ptr+"]: "+symbol);
+ litdistLens[ptr++] = lastLen = (byte)symbol;
+
+ if (ptr == num) {
+ /* Finished */
+ return true;
+ }
+ }
+
+ /* need more input ? */
+ if (symbol < 0) {
+ return false;
+ }
+
+ /* otherwise repeat code */
+ if (symbol >= 17) {
+ /* repeat zero */
+ // System.err.println("repeating zero");
+ lastLen = 0;
+ } else {
+ if (ptr == 0) {
+ throw new SharpZipBaseException();
+ }
+ }
+ repSymbol = symbol-16;
+ }
+ mode = REPS;
+ goto case REPS; // fall through
+ case REPS:
+ {
+ int bits = repBits[repSymbol];
+ int count = input.PeekBits(bits);
+ if (count < 0) {
+ return false;
+ }
+ input.DropBits(bits);
+ count += repMin[repSymbol];
+ // System.err.println("litdistLens repeated: "+count);
+
+ if (ptr + count > num) {
+ throw new SharpZipBaseException();
+ }
+ while (count-- > 0) {
+ litdistLens[ptr++] = lastLen;
+ }
+
+ if (ptr == num) {
+ /* Finished */
+ return true;
+ }
+ }
+ mode = LENS;
+ goto decode_loop;
+ }
+ }
+ }
+
+ public InflaterHuffmanTree BuildLitLenTree()
+ {
+ byte[] litlenLens = new byte[lnum];
+ Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
+ return new InflaterHuffmanTree(litlenLens);
+ }
+
+ public InflaterHuffmanTree BuildDistTree()
+ {
+ byte[] distLens = new byte[dnum];
+ Array.Copy(litdistLens, lnum, distLens, 0, dnum);
+ return new InflaterHuffmanTree(distLens);
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs
new file mode 100644
index 00000000000..987ab75d09c
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs
@@ -0,0 +1,225 @@
+// InflaterHuffmanTree.cs
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// Huffman tree used for inflation
+ /// </summary>
+ public class InflaterHuffmanTree
+ {
+ static int MAX_BITLEN = 15;
+ short[] tree;
+
+ /// <summary>
+ /// Literal length tree
+ /// </summary>
+ public static InflaterHuffmanTree defLitLenTree;
+
+ /// <summary>
+ /// Distance tree
+ /// </summary>
+ public static InflaterHuffmanTree defDistTree;
+
+ static InflaterHuffmanTree()
+ {
+ try {
+ byte[] codeLengths = new byte[288];
+ int i = 0;
+ while (i < 144) {
+ codeLengths[i++] = 8;
+ }
+ while (i < 256) {
+ codeLengths[i++] = 9;
+ }
+ while (i < 280) {
+ codeLengths[i++] = 7;
+ }
+ while (i < 288) {
+ codeLengths[i++] = 8;
+ }
+ defLitLenTree = new InflaterHuffmanTree(codeLengths);
+
+ codeLengths = new byte[32];
+ i = 0;
+ while (i < 32) {
+ codeLengths[i++] = 5;
+ }
+ defDistTree = new InflaterHuffmanTree(codeLengths);
+ } catch (Exception) {
+ throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal");
+ }
+ }
+
+ /// <summary>
+ /// Constructs a Huffman tree from the array of code lengths.
+ /// </summary>
+ /// <param name = "codeLengths">
+ /// the array of code lengths
+ /// </param>
+ public InflaterHuffmanTree(byte[] codeLengths)
+ {
+ BuildTree(codeLengths);
+ }
+
+ void BuildTree(byte[] codeLengths)
+ {
+ int[] blCount = new int[MAX_BITLEN + 1];
+ int[] nextCode = new int[MAX_BITLEN + 1];
+
+ for (int i = 0; i < codeLengths.Length; i++) {
+ int bits = codeLengths[i];
+ if (bits > 0) {
+ blCount[bits]++;
+ }
+ }
+
+ int code = 0;
+ int treeSize = 512;
+ for (int bits = 1; bits <= MAX_BITLEN; bits++) {
+ nextCode[bits] = code;
+ code += blCount[bits] << (16 - bits);
+ if (bits >= 10) {
+ /* We need an extra table for bit lengths >= 10. */
+ int start = nextCode[bits] & 0x1ff80;
+ int end = code & 0x1ff80;
+ treeSize += (end - start) >> (16 - bits);
+ }
+ }
+
+/* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
+ if (code != 65536)
+ {
+ throw new SharpZipBaseException("Code lengths don't add up properly.");
+ }
+*/
+ /* Now create and fill the extra tables from longest to shortest
+ * bit len. This way the sub trees will be aligned.
+ */
+ tree = new short[treeSize];
+ int treePtr = 512;
+ for (int bits = MAX_BITLEN; bits >= 10; bits--) {
+ int end = code & 0x1ff80;
+ code -= blCount[bits] << (16 - bits);
+ int start = code & 0x1ff80;
+ for (int i = start; i < end; i += 1 << 7) {
+ tree[DeflaterHuffman.BitReverse(i)] = (short) ((-treePtr << 4) | bits);
+ treePtr += 1 << (bits-9);
+ }
+ }
+
+ for (int i = 0; i < codeLengths.Length; i++) {
+ int bits = codeLengths[i];
+ if (bits == 0) {
+ continue;
+ }
+ code = nextCode[bits];
+ int revcode = DeflaterHuffman.BitReverse(code);
+ if (bits <= 9) {
+ do {
+ tree[revcode] = (short) ((i << 4) | bits);
+ revcode += 1 << bits;
+ } while (revcode < 512);
+ } else {
+ int subTree = tree[revcode & 511];
+ int treeLen = 1 << (subTree & 15);
+ subTree = -(subTree >> 4);
+ do {
+ tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
+ revcode += 1 << bits;
+ } while (revcode < treeLen);
+ }
+ nextCode[bits] = code + (1 << (16 - bits));
+ }
+
+ }
+
+ /// <summary>
+ /// Reads the next symbol from input. The symbol is encoded using the
+ /// huffman tree.
+ /// </summary>
+ /// <param name="input">
+ /// input the input source.
+ /// </param>
+ /// <returns>
+ /// the next symbol, or -1 if not enough input is available.
+ /// </returns>
+ public int GetSymbol(StreamManipulator input)
+ {
+ int lookahead, symbol;
+ if ((lookahead = input.PeekBits(9)) >= 0) {
+ if ((symbol = tree[lookahead]) >= 0) {
+ input.DropBits(symbol & 15);
+ return symbol >> 4;
+ }
+ int subtree = -(symbol >> 4);
+ int bitlen = symbol & 15;
+ if ((lookahead = input.PeekBits(bitlen)) >= 0) {
+ symbol = tree[subtree | (lookahead >> 9)];
+ input.DropBits(symbol & 15);
+ return symbol >> 4;
+ } else {
+ int bits = input.AvailableBits;
+ lookahead = input.PeekBits(bits);
+ symbol = tree[subtree | (lookahead >> 9)];
+ if ((symbol & 15) <= bits) {
+ input.DropBits(symbol & 15);
+ return symbol >> 4;
+ } else {
+ return -1;
+ }
+ }
+ } else {
+ int bits = input.AvailableBits;
+ lookahead = input.PeekBits(bits);
+ symbol = tree[lookahead];
+ if (symbol >= 0 && (symbol & 15) <= bits) {
+ input.DropBits(symbol & 15);
+ return symbol >> 4;
+ } else {
+ return -1;
+ }
+ }
+ }
+ }
+}
+
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs
new file mode 100644
index 00000000000..dbed2ecdd65
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs
@@ -0,0 +1,274 @@
+// PendingBuffer.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression
+{
+
+ /// <summary>
+ /// This class is general purpose class for writing data to a buffer.
+ ///
+ /// It allows you to write bits as well as bytes
+ /// Based on DeflaterPending.java
+ ///
+ /// author of the original java version : Jochen Hoenicke
+ /// </summary>
+ public class PendingBuffer
+ {
+ /// <summary>Internal work buffer
+ /// </summary>
+ protected byte[] buf;
+
+ int start;
+ int end;
+
+ uint bits;
+ int bitCount;
+
+ /// <summary>
+ /// construct instance using default buffer size of 4096
+ /// </summary>
+ public PendingBuffer() : this( 4096 )
+ {
+
+ }
+
+ /// <summary>
+ /// construct instance using specified buffer size
+ /// </summary>
+ /// <param name="bufsize">
+ /// size to use for internal buffer
+ /// </param>
+ public PendingBuffer(int bufsize)
+ {
+ buf = new byte[bufsize];
+ }
+
+ /// <summary>
+ /// Clear internal state/buffers
+ /// </summary>
+ public void Reset()
+ {
+ start = end = bitCount = 0;
+ }
+
+ /// <summary>
+ /// write a byte to buffer
+ /// </summary>
+ /// <param name="b">
+ /// value to write
+ /// </param>
+ public void WriteByte(int b)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ buf[end++] = (byte) b;
+ }
+
+ /// <summary>
+ /// Write a short value to buffer LSB first
+ /// </summary>
+ /// <param name="s">
+ /// value to write
+ /// </param>
+ public void WriteShort(int s)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ buf[end++] = (byte) s;
+ buf[end++] = (byte) (s >> 8);
+ }
+
+ /// <summary>
+ /// write an integer LSB first
+ /// </summary>
+ /// <param name="s">value to write</param>
+ public void WriteInt(int s)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ buf[end++] = (byte) s;
+ buf[end++] = (byte) (s >> 8);
+ buf[end++] = (byte) (s >> 16);
+ buf[end++] = (byte) (s >> 24);
+ }
+
+ /// <summary>
+ /// Write a block of data to buffer
+ /// </summary>
+ /// <param name="block">data to write</param>
+ /// <param name="offset">offset of first byte to write</param>
+ /// <param name="len">number of bytes to write</param>
+ public void WriteBlock(byte[] block, int offset, int len)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ System.Array.Copy(block, offset, buf, end, len);
+ end += len;
+ }
+
+ /// <summary>
+ /// The number of bits written to the buffer
+ /// </summary>
+ public int BitCount {
+ get {
+ return bitCount;
+ }
+ }
+
+ /// <summary>
+ /// Align internal buffer on a byte boundary
+ /// </summary>
+ public void AlignToByte()
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ if (bitCount > 0) {
+ buf[end++] = (byte) bits;
+ if (bitCount > 8) {
+ buf[end++] = (byte) (bits >> 8);
+ }
+ }
+ bits = 0;
+ bitCount = 0;
+ }
+
+ /// <summary>
+ /// Write bits to internal buffer
+ /// </summary>
+ /// <param name="b">source of bits</param>
+ /// <param name="count">number of bits to write</param>
+ public void WriteBits(int b, int count)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ // if (DeflaterConstants.DEBUGGING) {
+ // //Console.WriteLine("writeBits("+b+","+count+")");
+ // }
+ bits |= (uint)(b << bitCount);
+ bitCount += count;
+ if (bitCount >= 16) {
+ buf[end++] = (byte) bits;
+ buf[end++] = (byte) (bits >> 8);
+ bits >>= 16;
+ bitCount -= 16;
+ }
+ }
+
+ /// <summary>
+ /// Write a short value to internal buffer most significant byte first
+ /// </summary>
+ /// <param name="s">value to write</param>
+ public void WriteShortMSB(int s)
+ {
+ if (DeflaterConstants.DEBUGGING && start != 0) {
+ throw new SharpZipBaseException();
+ }
+ buf[end++] = (byte) (s >> 8);
+ buf[end++] = (byte) s;
+ }
+
+ /// <summary>
+ /// Indicates if buffer has been flushed
+ /// </summary>
+ public bool IsFlushed {
+ get {
+ return end == 0;
+ }
+ }
+
+ /// <summary>
+ /// Flushes the pending buffer into the given output array. If the
+ /// output array is to small, only a partial flush is done.
+ /// </summary>
+ /// <param name="output">
+ /// the output array;
+ /// </param>
+ /// <param name="offset">
+ /// the offset into output array;
+ /// </param>
+ /// <param name="length">
+ /// length the maximum number of bytes to store;
+ /// </param>
+ /// <exception name="ArgumentOutOfRangeException">
+ /// IndexOutOfBoundsException if offset or length are invalid.
+ /// </exception>
+ public int Flush(byte[] output, int offset, int length)
+ {
+ if (bitCount >= 8) {
+ buf[end++] = (byte) bits;
+ bits >>= 8;
+ bitCount -= 8;
+ }
+ if (length > end - start) {
+ length = end - start;
+ System.Array.Copy(buf, start, output, offset, length);
+ start = 0;
+ end = 0;
+ } else {
+ System.Array.Copy(buf, start, output, offset, length);
+ start += length;
+ }
+ return length;
+ }
+
+ /// <summary>
+ /// Convert internal buffer to byte array.
+ /// Buffer is empty on completion
+ /// </summary>
+ /// <returns>
+ /// converted buffer contents contents
+ /// </returns>
+ public byte[] ToByteArray()
+ {
+ byte[] ret = new byte[end - start];
+ System.Array.Copy(buf, start, ret, 0, ret.Length);
+ start = 0;
+ end = 0;
+ return ret;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs
new file mode 100644
index 00000000000..651570b9d3e
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs
@@ -0,0 +1,471 @@
+// DeflaterOutputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
+{
+
+ /// <summary>
+ /// A special stream deflating or compressing the bytes that are
+ /// written to it. It uses a Deflater to perform actual deflating.<br/>
+ /// Authors of the original java version : Tom Tromey, Jochen Hoenicke
+ /// </summary>
+ public class DeflaterOutputStream : Stream
+ {
+ /// <summary>
+ /// This buffer is used temporarily to retrieve the bytes from the
+ /// deflater and write them to the underlying output stream.
+ /// </summary>
+ protected byte[] buf;
+
+ /// <summary>
+ /// The deflater which is used to deflate the stream.
+ /// </summary>
+ protected Deflater def;
+
+ /// <summary>
+ /// Base stream the deflater depends on.
+ /// </summary>
+ protected Stream baseOutputStream;
+
+ bool isClosed = false;
+ bool isStreamOwner = true;
+
+ /// <summary>
+ /// Get/set flag indicating ownership of underlying stream.
+ /// When the flag is true <see cref="Close"></see> will close the underlying stream also.
+ /// </summary>
+ public bool IsStreamOwner
+ {
+ get { return isStreamOwner; }
+ set { isStreamOwner = value; }
+ }
+
+ /// <summary>
+ /// Allows client to determine if an entry can be patched after its added
+ /// </summary>
+ public bool CanPatchEntries {
+ get {
+ return baseOutputStream.CanSeek;
+ }
+ }
+
+ /// <summary>
+ /// Gets value indicating stream can be read from
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return baseOutputStream.CanRead;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating if seeking is supported for this stream
+ /// This property always returns false
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Get value indicating if this stream supports writing
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return baseOutputStream.CanWrite;
+ }
+ }
+
+ /// <summary>
+ /// Get current length of stream
+ /// </summary>
+ public override long Length {
+ get {
+ return baseOutputStream.Length;
+ }
+ }
+
+ /// <summary>
+ /// The current position within the stream.
+ /// Always throws a NotSupportedExceptionNotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any attempt to set position</exception>
+ public override long Position {
+ get {
+ return baseOutputStream.Position;
+ }
+ set {
+ throw new NotSupportedException("DefalterOutputStream Position not supported");
+ }
+ }
+
+ /// <summary>
+ /// Sets the current position of this stream to the given value. Not supported by this class!
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException("DeflaterOutputStream Seek not supported");
+ }
+
+ /// <summary>
+ /// Sets the length of this stream to the given value. Not supported by this class!
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void SetLength(long val)
+ {
+ throw new NotSupportedException("DeflaterOutputStream SetLength not supported");
+ }
+
+ /// <summary>
+ /// Read a byte from stream advancing position by one
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override int ReadByte()
+ {
+ throw new NotSupportedException("DeflaterOutputStream ReadByte not supported");
+ }
+
+ /// <summary>
+ /// Read a block of bytes from stream
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override int Read(byte[] b, int off, int len)
+ {
+ throw new NotSupportedException("DeflaterOutputStream Read not supported");
+ }
+
+ /// <summary>
+ /// Asynchronous reads are not supported a NotSupportedException is always thrown
+ /// </summary>
+ /// <param name="buffer"></param>
+ /// <param name="offset"></param>
+ /// <param name="count"></param>
+ /// <param name="callback"></param>
+ /// <param name="state"></param>
+ /// <returns></returns>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
+ {
+ throw new NotSupportedException("DeflaterOutputStream BeginRead not currently supported");
+ }
+
+ /// <summary>
+ /// Asynchronous writes arent supported, a NotSupportedException is always thrown
+ /// </summary>
+ /// <param name="buffer"></param>
+ /// <param name="offset"></param>
+ /// <param name="count"></param>
+ /// <param name="callback"></param>
+ /// <param name="state"></param>
+ /// <returns></returns>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
+ {
+ throw new NotSupportedException("DeflaterOutputStream BeginWrite not currently supported");
+ }
+
+ /// <summary>
+ /// Deflates everything in the input buffers. This will call
+ /// <code>def.deflate()</code> until all bytes from the input buffers
+ /// are processed.
+ /// </summary>
+ protected void Deflate()
+ {
+ while (!def.IsNeedingInput) {
+ int len = def.Deflate(buf, 0, buf.Length);
+
+ if (len <= 0) {
+ break;
+ }
+
+ if (this.keys != null) {
+ this.EncryptBlock(buf, 0, len);
+ }
+
+ baseOutputStream.Write(buf, 0, len);
+ }
+
+ if (!def.IsNeedingInput) {
+ throw new SharpZipBaseException("DeflaterOutputStream can't deflate all input?");
+ }
+ }
+
+ /// <summary>
+ /// Creates a new DeflaterOutputStream with a default Deflater and default buffer size.
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// the output stream where deflated output should be written.
+ /// </param>
+ public DeflaterOutputStream(Stream baseOutputStream) : this(baseOutputStream, new Deflater(), 512)
+ {
+ }
+
+ /// <summary>
+ /// Creates a new DeflaterOutputStream with the given Deflater and
+ /// default buffer size.
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// the output stream where deflated output should be written.
+ /// </param>
+ /// <param name="defl">
+ /// the underlying deflater.
+ /// </param>
+ public DeflaterOutputStream(Stream baseOutputStream, Deflater defl) : this(baseOutputStream, defl, 512)
+ {
+ }
+
+ /// <summary>
+ /// Creates a new DeflaterOutputStream with the given Deflater and
+ /// buffer size.
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// The output stream where deflated output is written.
+ /// </param>
+ /// <param name="deflater">
+ /// The underlying deflater to use
+ /// </param>
+ /// <param name="bufsize">
+ /// The buffer size to use when deflating
+ /// </param>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// bufsize is less than or equal to zero.
+ /// </exception>
+ /// <exception cref="ArgumentException">
+ /// baseOutputStream does not support writing
+ /// </exception>
+ /// <exception cref="ArgumentNullException">
+ /// deflater instance is null
+ /// </exception>
+ public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufsize)
+ {
+ if (baseOutputStream.CanWrite == false) {
+ throw new ArgumentException("baseOutputStream", "must support writing");
+ }
+
+ if (deflater == null) {
+ throw new ArgumentNullException("deflater");
+ }
+
+ if (bufsize <= 0) {
+ throw new ArgumentOutOfRangeException("bufsize");
+ }
+
+ this.baseOutputStream = baseOutputStream;
+ buf = new byte[bufsize];
+ def = deflater;
+ }
+
+ /// <summary>
+ /// Flushes the stream by calling flush() on the deflater and then
+ /// on the underlying stream. This ensures that all bytes are
+ /// flushed.
+ /// </summary>
+ public override void Flush()
+ {
+ def.Flush();
+ Deflate();
+ baseOutputStream.Flush();
+ }
+
+ /// <summary>
+ /// Finishes the stream by calling finish() on the deflater.
+ /// </summary>
+ /// <exception cref="SharpZipBaseException">
+ /// Not all input is deflated
+ /// </exception>
+ public virtual void Finish()
+ {
+ def.Finish();
+ while (!def.IsFinished) {
+ int len = def.Deflate(buf, 0, buf.Length);
+ if (len <= 0) {
+ break;
+ }
+
+ if (this.keys != null) {
+ this.EncryptBlock(buf, 0, len);
+ }
+
+ baseOutputStream.Write(buf, 0, len);
+ }
+ if (!def.IsFinished) {
+ throw new SharpZipBaseException("Can't deflate all input?");
+ }
+ baseOutputStream.Flush();
+ keys = null;
+ }
+
+ /// <summary>
+ /// Calls finish() and closes the underlying
+ /// stream when <see cref="IsStreamOwner"></see> is true.
+ /// </summary>
+ public override void Close()
+ {
+ if ( !isClosed ) {
+ isClosed = true;
+ Finish();
+ if ( isStreamOwner ) {
+ baseOutputStream.Close();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Writes a single byte to the compressed output stream.
+ /// </summary>
+ /// <param name="bval">
+ /// The byte value.
+ /// </param>
+ public override void WriteByte(byte bval)
+ {
+ byte[] b = new byte[1];
+ b[0] = bval;
+ Write(b, 0, 1);
+ }
+
+ /// <summary>
+ /// Writes bytes from an array to the compressed stream.
+ /// </summary>
+ /// <param name="buf">
+ /// The byte array
+ /// </param>
+ /// <param name="off">
+ /// The offset into the byte array where to start.
+ /// </param>
+ /// <param name="len">
+ /// The number of bytes to write.
+ /// </param>
+ public override void Write(byte[] buf, int off, int len)
+ {
+ def.SetInput(buf, off, len);
+ Deflate();
+ }
+
+ #region Encryption
+
+ // TODO: Refactor this code. The presence of Zip specific code in this low level class is wrong
+ string password = null;
+ uint[] keys = null;
+
+ /// <summary>
+ /// Get/set the password used for encryption. When null no encryption is performed
+ /// </summary>
+ public string Password {
+ get {
+ return password;
+ }
+ set {
+ if ( value != null && value.Length == 0 ) {
+ password = null;
+ } else {
+ password = value;
+ }
+ }
+ }
+
+
+ /// <summary>
+ /// Encrypt a single byte
+ /// </summary>
+ /// <returns>
+ /// The encrypted value
+ /// </returns>
+ protected byte EncryptByte()
+ {
+ uint temp = ((keys[2] & 0xFFFF) | 2);
+ return (byte)((temp * (temp ^ 1)) >> 8);
+ }
+
+
+ /// <summary>
+ /// Encrypt a block of data
+ /// </summary>
+ /// <param name="buffer">
+ /// Data to encrypt. NOTE the original contents of the buffer are lost
+ /// </param>
+ /// <param name="offset">
+ /// Offset of first byte in buffer to encrypt
+ /// </param>
+ /// <param name="length">
+ /// Number of bytes in buffer to encrypt
+ /// </param>
+ protected void EncryptBlock(byte[] buffer, int offset, int length)
+ {
+ // TODO: refactor to use crypto transform
+ for (int i = offset; i < offset + length; ++i) {
+ byte oldbyte = buffer[i];
+ buffer[i] ^= EncryptByte();
+ UpdateKeys(oldbyte);
+ }
+ }
+
+ /// <summary>
+ /// Initializes encryption keys based on given password
+ /// </summary>
+ protected void InitializePassword(string password) {
+ keys = new uint[] {
+ 0x12345678,
+ 0x23456789,
+ 0x34567890
+ };
+
+ for (int i = 0; i < password.Length; ++i) {
+ UpdateKeys((byte)password[i]);
+ }
+ }
+
+ /// <summary>
+ /// Update encryption keys
+ /// </summary>
+ protected void UpdateKeys(byte ch)
+ {
+ keys[0] = Crc32.ComputeCrc32(keys[0], ch);
+ keys[1] = keys[1] + (byte)keys[0];
+ keys[1] = keys[1] * 134775813 + 1;
+ keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
+ }
+ #endregion
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs
new file mode 100644
index 00000000000..232bf84f529
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs
@@ -0,0 +1,656 @@
+// InflaterInputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.Security.Cryptography;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Checksums;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
+{
+
+ /// <summary>
+ /// An input buffer customised for use by <see cref="InflaterInputStream"/>
+ /// </summary>
+ /// <remarks>
+ /// The buffer supports decryption of incoming data.
+ /// </remarks>
+ public class InflaterInputBuffer
+ {
+ /// <summary>
+ /// Initialise a new instance of <see cref="InflaterInputBuffer"/>
+ /// </summary>
+ /// <param name="stream">The stream to buffer.</param>
+ public InflaterInputBuffer(Stream stream)
+ {
+ inputStream = stream;
+ rawData = new byte[4096];
+ clearText = rawData;
+ }
+
+ /// <summary>
+ /// Get the length of bytes bytes in the <see cref="RawData"/>
+ /// </summary>
+ public int RawLength
+ {
+ get {
+ return rawLength;
+ }
+ }
+
+ /// <summary>
+ /// Get the contents of the raw data buffer.
+ /// </summary>
+ /// <remarks>This may contain encrypted data.</remarks>
+ public byte[] RawData
+ {
+ get {
+ return rawData;
+ }
+ }
+
+ /// <summary>
+ /// Get the number of useable bytes in <see cref="ClearText"/>
+ /// </summary>
+ public int ClearTextLength
+ {
+ get {
+ return clearTextLength;
+ }
+ }
+
+ /// <summary>
+ /// Get the contents of the clear text buffer.
+ /// </summary>
+ public byte[] ClearText
+ {
+ get {
+ return clearText;
+ }
+ }
+
+ /// <summary>
+ /// Get/set the number of bytes available
+ /// </summary>
+ public int Available
+ {
+ get { return available; }
+ set { available = value; }
+ }
+
+ /// <summary>
+ /// Call <see cref="Inflater.SetInput"/> passing the current clear text buffer contents.
+ /// </summary>
+ /// <param name="inflater">The inflater to set input for.</param>
+ public void SetInflaterInput(Inflater inflater)
+ {
+ if ( available > 0 ) {
+ inflater.SetInput(clearText, clearTextLength - available, available);
+ available = 0;
+ }
+ }
+
+ /// <summary>
+ /// Fill the buffer from the underlying input stream.
+ /// </summary>
+ public void Fill()
+ {
+ rawLength = 0;
+ int toRead = rawData.Length;
+
+ while (toRead > 0) {
+ int count = inputStream.Read(rawData, rawLength, toRead);
+ if ( count <= 0 ) {
+ if (rawLength == 0) {
+ throw new SharpZipBaseException("Unexpected EOF");
+ }
+ break;
+ }
+ rawLength += count;
+ toRead -= count;
+ }
+
+ if ( cryptoTransform != null ) {
+ clearTextLength = cryptoTransform.TransformBlock(rawData, 0, rawLength, clearText, 0);
+ }
+ else {
+ clearTextLength = rawLength;
+ }
+
+ available = clearTextLength;
+ }
+
+ /// <summary>
+ /// Read a buffer directly from the input stream
+ /// </summary>
+ /// <param name="buffer">The buffer to fill</param>
+ /// <returns>Returns the number of bytes read.</returns>
+ public int ReadRawBuffer(byte[] buffer)
+ {
+ return ReadRawBuffer(buffer, 0, buffer.Length);
+ }
+
+ /// <summary>
+ /// Read a buffer directly from the input stream
+ /// </summary>
+ /// <param name="outBuffer">The buffer to read into</param>
+ /// <param name="offset">The offset to start reading data into.</param>
+ /// <param name="length">The number of bytes to read.</param>
+ /// <returns>Returns the number of bytes read.</returns>
+ public int ReadRawBuffer(byte[] outBuffer, int offset, int length)
+ {
+ if ( length <= 0 ) {
+ throw new ArgumentOutOfRangeException("length");
+ }
+
+ int currentOffset = offset;
+ int currentLength = length;
+
+ while ( currentLength > 0 ) {
+ if ( available <= 0 ) {
+ Fill();
+ if (available <= 0) {
+ return 0;
+ }
+ }
+ int toCopy = Math.Min(currentLength, available);
+ System.Array.Copy(rawData, rawLength - (int)available, outBuffer, currentOffset, toCopy);
+ currentOffset += toCopy;
+ currentLength -= toCopy;
+ available -= toCopy;
+ }
+ return length;
+ }
+
+ /// <summary>
+ /// Read clear text data from the input stream.
+ /// </summary>
+ /// <param name="outBuffer">The buffer to add data to.</param>
+ /// <param name="offset">The offset to start adding data at.</param>
+ /// <param name="length">The number of bytes to read.</param>
+ /// <returns>Returns the number of bytes actually read.</returns>
+ public int ReadClearTextBuffer(byte[] outBuffer, int offset, int length)
+ {
+ if ( length <= 0 ) {
+ throw new ArgumentOutOfRangeException("length");
+ }
+
+ int currentOffset = offset;
+ int currentLength = length;
+
+ while ( currentLength > 0 ) {
+ if ( available <= 0 ) {
+ Fill();
+ if (available <= 0) {
+ return 0;
+ }
+ }
+
+ int toCopy = Math.Min(currentLength, available);
+ System.Array.Copy(clearText, clearTextLength - (int)available, outBuffer, currentOffset, toCopy);
+ currentOffset += toCopy;
+ currentLength -= toCopy;
+ available -= toCopy;
+ }
+ return length;
+ }
+
+ /// <summary>
+ /// Read a byte from the input stream.
+ /// </summary>
+ /// <returns>Returns the byte read.</returns>
+ public int ReadLeByte()
+ {
+ if (available <= 0) {
+ Fill();
+ if (available <= 0) {
+ throw new ZipException("EOF in header");
+ }
+ }
+ byte result = (byte)(rawData[rawLength - available] & 0xff);
+ available -= 1;
+ return result;
+ }
+
+ /// <summary>
+ /// Read an unsigned short in little endian byte order.
+ /// </summary>
+ public int ReadLeShort()
+ {
+ return ReadLeByte() | (ReadLeByte() << 8);
+ }
+
+ /// <summary>
+ /// Read an int in little endian byte order.
+ /// </summary>
+ public int ReadLeInt()
+ {
+ return ReadLeShort() | (ReadLeShort() << 16);
+ }
+
+ /// <summary>
+ /// Read an int baseInputStream little endian byte order.
+ /// </summary>
+ public long ReadLeLong()
+ {
+ return ReadLeInt() | (ReadLeInt() << 32);
+ }
+
+ /// <summary>
+ /// Get/set the <see cref="ICryptoTransform"/> to apply to any data.
+ /// </summary>
+ /// <remarks>Set this value to null to have no transform applied.</remarks>
+ public ICryptoTransform CryptoTransform
+ {
+ set {
+ cryptoTransform = value;
+ if ( cryptoTransform != null ) {
+ if ( rawData == clearText ) {
+ if ( internalClearText == null ) {
+ internalClearText = new byte[4096];
+ }
+ clearText = internalClearText;
+ }
+ clearTextLength = rawLength;
+ if ( available > 0 ) {
+ cryptoTransform.TransformBlock(rawData, rawLength - available, available, clearText, rawLength - available);
+ }
+ } else {
+ clearText = rawData;
+ clearTextLength = rawLength;
+ }
+ }
+ }
+
+ #region Instance Fields
+ int rawLength;
+ byte[] rawData;
+
+ int clearTextLength;
+ byte[] clearText;
+
+ byte[] internalClearText;
+
+ int available;
+
+ ICryptoTransform cryptoTransform;
+ Stream inputStream;
+ #endregion
+ }
+
+ /// <summary>
+ /// This filter stream is used to decompress data compressed using the "deflate"
+ /// format. The "deflate" format is described in RFC 1951.
+ ///
+ /// This stream may form the basis for other decompression filters, such
+ /// as the <see cref="ICSharpCode.SharpZipLib.GZip.GZipInputStream">GZipInputStream</see>.
+ ///
+ /// Author of the original java version : John Leuner.
+ /// </summary>
+ public class InflaterInputStream : Stream
+ {
+ /// <summary>
+ /// Decompressor for this stream
+ /// </summary>
+ protected Inflater inf;
+
+ /// <summary>
+ /// <see cref="InflaterInputBuffer">Input buffer</see> for this stream.
+ /// </summary>
+ protected InflaterInputBuffer inputBuffer;
+
+ /// <summary>
+ /// Base stream the inflater reads from.
+ /// </summary>
+ protected Stream baseInputStream;
+
+ /// <summary>
+ /// The compressed size
+ /// </summary>
+ protected long csize;
+
+ bool isClosed = false;
+ bool isStreamOwner = true;
+
+ /// <summary>
+ /// Get/set flag indicating ownership of underlying stream.
+ /// When the flag is true <see cref="Close"/> will close the underlying stream also.
+ /// </summary>
+ /// <remarks>
+ /// The default value is true.
+ /// </remarks>
+ public bool IsStreamOwner
+ {
+ get { return isStreamOwner; }
+ set { isStreamOwner = value; }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports reading
+ /// </summary>
+ public override bool CanRead {
+ get {
+ return baseInputStream.CanRead;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value of false indicating seeking is not supported for this stream.
+ /// </summary>
+ public override bool CanSeek {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value of false indicating that this stream is not writeable.
+ /// </summary>
+ public override bool CanWrite {
+ get {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// A value representing the length of the stream in bytes.
+ /// </summary>
+ public override long Length {
+ get {
+ return inputBuffer.RawLength;
+ }
+ }
+
+ /// <summary>
+ /// The current position within the stream.
+ /// Throws a NotSupportedException when attempting to set the position
+ /// </summary>
+ /// <exception cref="NotSupportedException">Attempting to set the position</exception>
+ public override long Position {
+ get {
+ return baseInputStream.Position;
+ }
+ set {
+ throw new NotSupportedException("InflaterInputStream Position not supported");
+ }
+ }
+
+ /// <summary>
+ /// Flushes the baseInputStream
+ /// </summary>
+ public override void Flush()
+ {
+ baseInputStream.Flush();
+ }
+
+ /// <summary>
+ /// Sets the position within the current stream
+ /// Always throws a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException("Seek not supported");
+ }
+
+ /// <summary>
+ /// Set the length of the current stream
+ /// Always throws a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void SetLength(long val)
+ {
+ throw new NotSupportedException("InflaterInputStream SetLength not supported");
+ }
+
+ /// <summary>
+ /// Writes a sequence of bytes to stream and advances the current position
+ /// This method always throws a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void Write(byte[] array, int offset, int count)
+ {
+ throw new NotSupportedException("InflaterInputStream Write not supported");
+ }
+
+ /// <summary>
+ /// Writes one byte to the current stream and advances the current position
+ /// Always throws a NotSupportedException
+ /// </summary>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override void WriteByte(byte val)
+ {
+ throw new NotSupportedException("InflaterInputStream WriteByte not supported");
+ }
+
+ /// <summary>
+ /// Entry point to begin an asynchronous write. Always throws a NotSupportedException.
+ /// </summary>
+ /// <param name="buffer">The buffer to write data from</param>
+ /// <param name="offset">Offset of first byte to write</param>
+ /// <param name="count">The maximum number of bytes to write</param>
+ /// <param name="callback">The method to be called when the asynchronous write operation is completed</param>
+ /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests</param>
+ /// <returns>An <see cref="System.IAsyncResult">IAsyncResult</see> that references the asynchronous write</returns>
+ /// <exception cref="NotSupportedException">Any access</exception>
+ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
+ {
+ throw new NotSupportedException("InflaterInputStream BeginWrite not supported");
+ }
+
+ /// <summary>
+ /// Create an InflaterInputStream with the default decompressor
+ /// and a default buffer size of 4KB.
+ /// </summary>
+ /// <param name = "baseInputStream">
+ /// The InputStream to read bytes from
+ /// </param>
+ public InflaterInputStream(Stream baseInputStream) : this(baseInputStream, new Inflater(), 4096)
+ {
+ }
+
+ /// <summary>
+ /// Create an InflaterInputStream with the specified decompressor
+ /// and a default buffer size of 4KB.
+ /// </summary>
+ /// <param name = "baseInputStream">
+ /// The source of input data
+ /// </param>
+ /// <param name = "inf">
+ /// The decompressor used to decompress data read from baseInputStream
+ /// </param>
+ public InflaterInputStream(Stream baseInputStream, Inflater inf) : this(baseInputStream, inf, 4096)
+ {
+ }
+
+ /// <summary>
+ /// Create an InflaterInputStream with the specified decompressor
+ /// and the specified buffer size.
+ /// </summary>
+ /// <param name = "baseInputStream">
+ /// The InputStream to read bytes from
+ /// </param>
+ /// <param name = "inflater">
+ /// The decompressor to use
+ /// </param>
+ /// <param name = "bufferSize">
+ /// Size of the buffer to use
+ /// </param>
+ public InflaterInputStream(Stream baseInputStream, Inflater inflater, int bufferSize)
+ {
+ if (baseInputStream == null) {
+ throw new ArgumentNullException("InflaterInputStream baseInputStream is null");
+ }
+
+ if (inflater == null) {
+ throw new ArgumentNullException("InflaterInputStream Inflater is null");
+ }
+
+ if (bufferSize <= 0) {
+ throw new ArgumentOutOfRangeException("bufferSize");
+ }
+
+ this.baseInputStream = baseInputStream;
+ this.inf = inflater;
+
+ inputBuffer = new InflaterInputBuffer(baseInputStream);
+ }
+
+ /// <summary>
+ /// Returns 0 once the end of the stream (EOF) has been reached.
+ /// Otherwise returns 1.
+ /// </summary>
+ public virtual int Available {
+ get {
+ return inf.IsFinished ? 0 : 1;
+ }
+ }
+
+ /// <summary>
+ /// Closes the input stream. When <see cref="IsStreamOwner"></see>
+ /// is true the underlying stream is also closed.
+ /// </summary>
+ public override void Close()
+ {
+ if ( !isClosed ) {
+ isClosed = true;
+ if ( isStreamOwner ) {
+ baseInputStream.Close();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Fills the buffer with more data to decompress.
+ /// </summary>
+ /// <exception cref="SharpZipBaseException">
+ /// Stream ends early
+ /// </exception>
+ protected void Fill()
+ {
+ inputBuffer.Fill();
+ inputBuffer.SetInflaterInput(inf);
+ }
+
+ /// <summary>
+ /// Decompresses data into the byte array
+ /// </summary>
+ /// <param name ="b">
+ /// The array to read and decompress data into
+ /// </param>
+ /// <param name ="off">
+ /// The offset indicating where the data should be placed
+ /// </param>
+ /// <param name ="len">
+ /// The number of bytes to decompress
+ /// </param>
+ /// <returns>The number of bytes read. Zero signals the end of stream</returns>
+ /// <exception cref="SharpZipBaseException">
+ /// Inflater needs a dictionary
+ /// </exception>
+ public override int Read(byte[] b, int off, int len)
+ {
+ for (;;) {
+ int count;
+ try {
+ count = inf.Inflate(b, off, len);
+ } catch (Exception e) {
+ throw new SharpZipBaseException(e.ToString());
+ }
+
+ if (count > 0) {
+ return count;
+ }
+
+ if (inf.IsNeedingDictionary) {
+ throw new SharpZipBaseException("Need a dictionary");
+ } else if (inf.IsFinished) {
+ return 0;
+ } else if (inf.IsNeedingInput) {
+ Fill();
+ } else {
+ throw new InvalidOperationException("Don't know what to do");
+ }
+ }
+ }
+
+ /// <summary>
+ /// Skip specified number of bytes of uncompressed data
+ /// </summary>
+ /// <param name ="n">
+ /// Number of bytes to skip
+ /// </param>
+ /// <returns>
+ /// The number of bytes skipped, zero if the end of
+ /// stream has been reached
+ /// </returns>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// Number of bytes to skip is zero or less
+ /// </exception>
+ public long Skip(long n)
+ {
+ if (n <= 0) {
+ throw new ArgumentOutOfRangeException("n");
+ }
+
+ // v0.80 Skip by seeking if underlying stream supports it...
+ if (baseInputStream.CanSeek) {
+ baseInputStream.Seek(n, SeekOrigin.Current);
+ return n;
+ } else {
+ int len = 2048;
+ if (n < len) {
+ len = (int) n;
+ }
+ byte[] tmp = new byte[len];
+ return (long)baseInputStream.Read(tmp, 0, tmp.Length);
+ }
+ }
+
+ /// <summary>
+ /// Clear any cryptographic state.
+ /// </summary>
+ protected void StopDecrypting()
+ {
+ inputBuffer.CryptoTransform = null;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs
new file mode 100644
index 00000000000..9112f007824
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs
@@ -0,0 +1,226 @@
+// OutputWindow.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
+{
+
+ /// <summary>
+ /// Contains the output from the Inflation process.
+ /// We need to have a window so that we can refer backwards into the output stream
+ /// to repeat stuff.<br/>
+ /// Author of the original java version : John Leuner
+ /// </summary>
+ public class OutputWindow
+ {
+ private static int WINDOW_SIZE = 1 << 15;
+ private static int WINDOW_MASK = WINDOW_SIZE - 1;
+
+ private byte[] window = new byte[WINDOW_SIZE]; //The window is 2^15 bytes
+ private int windowEnd = 0;
+ private int windowFilled = 0;
+
+ /// <summary>
+ /// Write a byte to this output window
+ /// </summary>
+ /// <param name="abyte">value to write</param>
+ /// <exception cref="InvalidOperationException">
+ /// if window is full
+ /// </exception>
+ public void Write(int abyte)
+ {
+ if (windowFilled++ == WINDOW_SIZE) {
+ throw new InvalidOperationException("Window full");
+ }
+ window[windowEnd++] = (byte) abyte;
+ windowEnd &= WINDOW_MASK;
+ }
+
+
+ private void SlowRepeat(int repStart, int len, int dist)
+ {
+ while (len-- > 0) {
+ window[windowEnd++] = window[repStart++];
+ windowEnd &= WINDOW_MASK;
+ repStart &= WINDOW_MASK;
+ }
+ }
+
+ /// <summary>
+ /// Append a byte pattern already in the window itself
+ /// </summary>
+ /// <param name="len">length of pattern to copy</param>
+ /// <param name="dist">distance from end of window pattern occurs</param>
+ /// <exception cref="InvalidOperationException">
+ /// If the repeated data overflows the window
+ /// </exception>
+ public void Repeat(int len, int dist)
+ {
+ if ((windowFilled += len) > WINDOW_SIZE) {
+ throw new InvalidOperationException("Window full");
+ }
+
+ int rep_start = (windowEnd - dist) & WINDOW_MASK;
+ int border = WINDOW_SIZE - len;
+ if (rep_start <= border && windowEnd < border) {
+ if (len <= dist) {
+ System.Array.Copy(window, rep_start, window, windowEnd, len);
+ windowEnd += len;
+ } else {
+ /* We have to copy manually, since the repeat pattern overlaps. */
+ while (len-- > 0) {
+ window[windowEnd++] = window[rep_start++];
+ }
+ }
+ } else {
+ SlowRepeat(rep_start, len, dist);
+ }
+ }
+
+ /// <summary>
+ /// Copy from input manipulator to internal window
+ /// </summary>
+ /// <param name="input">source of data</param>
+ /// <param name="len">length of data to copy</param>
+ /// <returns>the number of bytes copied</returns>
+ public int CopyStored(StreamManipulator input, int len)
+ {
+ len = Math.Min(Math.Min(len, WINDOW_SIZE - windowFilled), input.AvailableBytes);
+ int copied;
+
+ int tailLen = WINDOW_SIZE - windowEnd;
+ if (len > tailLen) {
+ copied = input.CopyBytes(window, windowEnd, tailLen);
+ if (copied == tailLen) {
+ copied += input.CopyBytes(window, 0, len - tailLen);
+ }
+ } else {
+ copied = input.CopyBytes(window, windowEnd, len);
+ }
+
+ windowEnd = (windowEnd + copied) & WINDOW_MASK;
+ windowFilled += copied;
+ return copied;
+ }
+
+ /// <summary>
+ /// Copy dictionary to window
+ /// </summary>
+ /// <param name="dict">source dictionary</param>
+ /// <param name="offset">offset of start in source dictionary</param>
+ /// <param name="len">length of dictionary</param>
+ /// <exception cref="InvalidOperationException">
+ /// If window isnt empty
+ /// </exception>
+ public void CopyDict(byte[] dict, int offset, int len)
+ {
+ if (windowFilled > 0) {
+ throw new InvalidOperationException();
+ }
+
+ if (len > WINDOW_SIZE) {
+ offset += len - WINDOW_SIZE;
+ len = WINDOW_SIZE;
+ }
+ System.Array.Copy(dict, offset, window, 0, len);
+ windowEnd = len & WINDOW_MASK;
+ }
+
+ /// <summary>
+ /// Get remaining unfilled space in window
+ /// </summary>
+ /// <returns>Number of bytes left in window</returns>
+ public int GetFreeSpace()
+ {
+ return WINDOW_SIZE - windowFilled;
+ }
+
+ /// <summary>
+ /// Get bytes available for output in window
+ /// </summary>
+ /// <returns>Number of bytes filled</returns>
+ public int GetAvailable()
+ {
+ return windowFilled;
+ }
+
+ /// <summary>
+ /// Copy contents of window to output
+ /// </summary>
+ /// <param name="output">buffer to copy to</param>
+ /// <param name="offset">offset to start at</param>
+ /// <param name="len">number of bytes to count</param>
+ /// <returns>The number of bytes copied</returns>
+ /// <exception cref="InvalidOperationException">
+ /// If a window underflow occurs
+ /// </exception>
+ public int CopyOutput(byte[] output, int offset, int len)
+ {
+ int copy_end = windowEnd;
+ if (len > windowFilled) {
+ len = windowFilled;
+ } else {
+ copy_end = (windowEnd - windowFilled + len) & WINDOW_MASK;
+ }
+
+ int copied = len;
+ int tailLen = len - copy_end;
+
+ if (tailLen > 0) {
+ System.Array.Copy(window, WINDOW_SIZE - tailLen, output, offset, tailLen);
+ offset += tailLen;
+ len = copy_end;
+ }
+ System.Array.Copy(window, copy_end - len, output, offset, len);
+ windowFilled -= copied;
+ if (windowFilled < 0) {
+ throw new InvalidOperationException();
+ }
+ return copied;
+ }
+
+ /// <summary>
+ /// Reset by clearing window so <see cref="GetAvailable">GetAvailable</see> returns 0
+ /// </summary>
+ public void Reset()
+ {
+ windowFilled = windowEnd = 0;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs
new file mode 100644
index 00000000000..ca07d141e01
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs
@@ -0,0 +1,270 @@
+// StreamManipulator.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
+{
+
+ /// <summary>
+ /// This class allows us to retrieve a specified number of bits from
+ /// the input buffer, as well as copy big byte blocks.
+ ///
+ /// It uses an int buffer to store up to 31 bits for direct
+ /// manipulation. This guarantees that we can get at least 16 bits,
+ /// but we only need at most 15, so this is all safe.
+ ///
+ /// There are some optimizations in this class, for example, you must
+ /// never peek more than 8 bits more than needed, and you must first
+ /// peek bits before you may drop them. This is not a general purpose
+ /// class but optimized for the behaviour of the Inflater.
+ ///
+ /// authors of the original java version : John Leuner, Jochen Hoenicke
+ /// </summary>
+ public class StreamManipulator
+ {
+ private byte[] window;
+ private int window_start = 0;
+ private int window_end = 0;
+
+ private uint buffer = 0;
+ private int bits_in_buffer = 0;
+
+ /// <summary>
+ /// Get the next n bits but don't increase input pointer. n must be
+ /// less or equal 16 and if this call succeeds, you must drop
+ /// at least n - 8 bits in the next call.
+ /// </summary>
+ /// <returns>
+ /// the value of the bits, or -1 if not enough bits available. */
+ /// </returns>
+ public int PeekBits(int n)
+ {
+ if (bits_in_buffer < n) {
+ if (window_start == window_end) {
+ return -1; // ok
+ }
+ buffer |= (uint)((window[window_start++] & 0xff |
+ (window[window_start++] & 0xff) << 8) << bits_in_buffer);
+ bits_in_buffer += 16;
+ }
+ return (int)(buffer & ((1 << n) - 1));
+ }
+
+ /// <summary>
+ /// Drops the next n bits from the input. You should have called PeekBits
+ /// with a bigger or equal n before, to make sure that enough bits are in
+ /// the bit buffer.
+ /// </summary>
+ public void DropBits(int n)
+ {
+ buffer >>= n;
+ bits_in_buffer -= n;
+ }
+
+ /// <summary>
+ /// Gets the next n bits and increases input pointer. This is equivalent
+ /// to PeekBits followed by dropBits, except for correct error handling.
+ /// </summary>
+ /// <returns>
+ /// the value of the bits, or -1 if not enough bits available.
+ /// </returns>
+ public int GetBits(int n)
+ {
+ int bits = PeekBits(n);
+ if (bits >= 0) {
+ DropBits(n);
+ }
+ return bits;
+ }
+
+ /// <summary>
+ /// Gets the number of bits available in the bit buffer. This must be
+ /// only called when a previous PeekBits() returned -1.
+ /// </summary>
+ /// <returns>
+ /// the number of bits available.
+ /// </returns>
+ public int AvailableBits {
+ get {
+ return bits_in_buffer;
+ }
+ }
+
+ /// <summary>
+ /// Gets the number of bytes available.
+ /// </summary>
+ /// <returns>
+ /// The number of bytes available.
+ /// </returns>
+ public int AvailableBytes {
+ get {
+ return window_end - window_start + (bits_in_buffer >> 3);
+ }
+ }
+
+ /// <summary>
+ /// Skips to the next byte boundary.
+ /// </summary>
+ public void SkipToByteBoundary()
+ {
+ buffer >>= (bits_in_buffer & 7);
+ bits_in_buffer &= ~7;
+ }
+
+ /// <summary>
+ /// Returns true when SetInput can be called
+ /// </summary>
+ public bool IsNeedingInput {
+ get {
+ return window_start == window_end;
+ }
+ }
+
+ /// <summary>
+ /// Copies length bytes from input buffer to output buffer starting
+ /// at output[offset]. You have to make sure, that the buffer is
+ /// byte aligned. If not enough bytes are available, copies fewer
+ /// bytes.
+ /// </summary>
+ /// <param name="output">
+ /// The buffer to copy bytes to.
+ /// </param>
+ /// <param name="offset">
+ /// The offset in the buffer at which copying starts
+ /// </param>
+ /// <param name="length">
+ /// The length to copy, 0 is allowed.
+ /// </param>
+ /// <returns>
+ /// The number of bytes copied, 0 if no bytes were available.
+ /// </returns>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// Length is less than zero
+ /// </exception>
+ /// <exception cref="InvalidOperationException">
+ /// Bit buffer isnt byte aligned
+ /// </exception>
+ public int CopyBytes(byte[] output, int offset, int length)
+ {
+ if (length < 0) {
+ throw new ArgumentOutOfRangeException("length");
+ }
+ if ((bits_in_buffer & 7) != 0) {
+ /* bits_in_buffer may only be 0 or a multiple of 8 */
+ throw new InvalidOperationException("Bit buffer is not byte aligned!");
+ }
+
+ int count = 0;
+ while (bits_in_buffer > 0 && length > 0) {
+ output[offset++] = (byte) buffer;
+ buffer >>= 8;
+ bits_in_buffer -= 8;
+ length--;
+ count++;
+ }
+
+ if (length == 0) {
+ return count;
+ }
+
+ int avail = window_end - window_start;
+ if (length > avail) {
+ length = avail;
+ }
+ System.Array.Copy(window, window_start, output, offset, length);
+ window_start += length;
+
+ if (((window_start - window_end) & 1) != 0) {
+ /* We always want an even number of bytes in input, see peekBits */
+ buffer = (uint)(window[window_start++] & 0xff);
+ bits_in_buffer = 8;
+ }
+ return count + length;
+ }
+
+ /// <summary>
+ /// Constructs a default StreamManipulator with all buffers empty
+ /// </summary>
+ public StreamManipulator()
+ {
+ }
+
+
+ /// <summary>
+ /// resets state and empties internal buffers
+ /// </summary>
+ public void Reset()
+ {
+ buffer = (uint)(window_start = window_end = bits_in_buffer = 0);
+ }
+
+ /// <summary>
+ /// Add more input for consumption.
+ /// Only call when IsNeedingInput returns true
+ /// </summary>
+ /// <param name="buf">data to be input</param>
+ /// <param name="off">offset of first byte of input</param>
+ /// <param name="len">length of input</param>
+ public void SetInput(byte[] buf, int off, int len)
+ {
+ if (window_start < window_end) {
+ throw new InvalidOperationException("Old input was not completely processed");
+ }
+
+ int end = off + len;
+
+ /* We want to throw an ArrayIndexOutOfBoundsException early. The
+ * check is very tricky: it also handles integer wrap around.
+ */
+ if (0 > off || off > end || end > buf.Length) {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ if ((len & 1) != 0) {
+ /* We always want an even number of bytes in input, see peekBits */
+ buffer |= (uint)((buf[off++] & 0xff) << bits_in_buffer);
+ bits_in_buffer += 8;
+ }
+
+ window = buf;
+ window_start = off;
+ window_end = end;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/FastZip.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/FastZip.cs
new file mode 100644
index 00000000000..6c7937e377c
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/FastZip.cs
@@ -0,0 +1,430 @@
+// SimpleZip.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+using System;
+using System.IO;
+using ICSharpCode.SharpZipLib.Core;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+ /// <summary>
+ /// FastZipEvents supports all events applicable to <see cref="FastZip">FastZip</see> operations.
+ /// </summary>
+ public class FastZipEvents
+ {
+ /// <summary>
+ /// Delegate to invoke when processing directories.
+ /// </summary>
+ public ProcessDirectoryDelegate ProcessDirectory;
+
+ /// <summary>
+ /// Delegate to invoke when processing files.
+ /// </summary>
+ public ProcessFileDelegate ProcessFile;
+
+ /// <summary>
+ /// Delegate to invoke when processing directory failures.
+ /// </summary>
+ public DirectoryFailureDelegate DirectoryFailure;
+
+ /// <summary>
+ /// Delegate to invoke when processing file failures.
+ /// </summary>
+ public FileFailureDelegate FileFailure;
+
+ /// <summary>
+ /// Raise the directory failure event.
+ /// </summary>
+ /// <param name="directory">The directory.</param>
+ /// <param name="e">The exception for this event.</param>
+ public void OnDirectoryFailure(string directory, Exception e)
+ {
+ if ( DirectoryFailure != null ) {
+ ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
+ DirectoryFailure(this, args);
+ }
+ }
+
+ /// <summary>
+ /// Raises the file failure event.
+ /// </summary>
+ /// <param name="file">The file for this event.</param>
+ /// <param name="e">The exception for this event.</param>
+ public void OnFileFailure(string file, Exception e)
+ {
+ if ( FileFailure != null ) {
+ ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
+ FileFailure(this, args);
+ }
+ }
+
+ /// <summary>
+ /// Raises the ProcessFileEvent.
+ /// </summary>
+ /// <param name="file">The file for this event.</param>
+ public void OnProcessFile(string file)
+ {
+ if ( ProcessFile != null ) {
+ ScanEventArgs args = new ScanEventArgs(file);
+ ProcessFile(this, args);
+ }
+ }
+
+ /// <summary>
+ /// Raises the ProcessDirectoryEvent.
+ /// </summary>
+ /// <param name="directory">The directory for this event.</param>
+ /// <param name="hasMatchingFiles">Flag indicating if directory has matching files as determined by the current filter.</param>
+ public void OnProcessDirectory(string directory, bool hasMatchingFiles)
+ {
+ if ( ProcessDirectory != null ) {
+ DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles);
+ ProcessDirectory(this, args);
+ }
+ }
+
+ }
+
+ /// <summary>
+ /// FastZip provides facilities for creating and extracting zip files.
+ /// Only relative paths are supported.
+ /// </summary>
+ public class FastZip
+ {
+ /// <summary>
+ /// Initialize a default instance of FastZip.
+ /// </summary>
+ public FastZip()
+ {
+ this.events = null;
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="FastZip"/>
+ /// </summary>
+ /// <param name="events"></param>
+ public FastZip(FastZipEvents events)
+ {
+ this.events = events;
+ }
+
+ /// <summary>
+ /// Defines the desired handling when overwriting files.
+ /// </summary>
+ public enum Overwrite {
+ /// <summary>
+ /// Prompt the user to confirm overwriting
+ /// </summary>
+ Prompt,
+ /// <summary>
+ /// Never overwrite files.
+ /// </summary>
+ Never,
+ /// <summary>
+ /// Always overwrite files.
+ /// </summary>
+ Always
+ }
+
+ /// <summary>
+ /// Get/set a value indicating wether empty directories should be created.
+ /// </summary>
+ public bool CreateEmptyDirectories
+ {
+ get { return createEmptyDirectories; }
+ set { createEmptyDirectories = value; }
+ }
+
+ /// <summary>
+ /// Delegate called when confirming overwriting of files.
+ /// </summary>
+ public delegate bool ConfirmOverwriteDelegate(string fileName);
+
+ /// <summary>
+ /// Create a zip file.
+ /// </summary>
+ /// <param name="zipFileName">The name of the zip file to create.</param>
+ /// <param name="sourceDirectory">The directory to source files from.</param>
+ /// <param name="recurse">True to recurse directories, false for no recursion.</param>
+ /// <param name="fileFilter">The file filter to apply.</param>
+ /// <param name="directoryFilter">The directory filter to apply.</param>
+ public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
+ {
+ NameTransform = new ZipNameTransform(true, sourceDirectory);
+ this.sourceDirectory = sourceDirectory;
+
+ outputStream = new ZipOutputStream(File.Create(zipFileName));
+ try {
+ FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
+ scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
+ if ( this.CreateEmptyDirectories ) {
+ scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
+ }
+ scanner.Scan(sourceDirectory, recurse);
+ }
+ finally {
+ outputStream.Close();
+ }
+ }
+
+ /// <summary>
+ /// Create a zip file/archive.
+ /// </summary>
+ /// <param name="zipFileName">The name of the zip file to create.</param>
+ /// <param name="sourceDirectory">The directory to obtain files and directories from.</param>
+ /// <param name="recurse">True to recurse directories, false for no recursion.</param>
+ /// <param name="fileFilter">The file filter to apply.</param>
+ public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter)
+ {
+ CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, null);
+ }
+
+ /// <summary>
+ /// Extract the contents of a zip file.
+ /// </summary>
+ /// <param name="zipFileName">The zip file to extract from.</param>
+ /// <param name="targetDirectory">The directory to save extracted information in.</param>
+ /// <param name="fileFilter">A filter to apply to files.</param>
+ public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter)
+ {
+ ExtractZip(zipFileName, targetDirectory, Overwrite.Always, null, fileFilter, null);
+ }
+
+ /// <summary>
+ /// Exatract the contents of a zip file.
+ /// </summary>
+ /// <param name="zipFileName">The zip file to extract from.</param>
+ /// <param name="targetDirectory">The directory to save extracted information in.</param>
+ /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
+ /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
+ /// <param name="fileFilter">A filter to apply to files.</param>
+ /// <param name="directoryFilter">A filter to apply to directories.</param>
+ public void ExtractZip(string zipFileName, string targetDirectory,
+ Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
+ string fileFilter, string directoryFilter)
+ {
+ if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) {
+ throw new ArgumentNullException("confirmDelegate");
+ }
+ this.overwrite = overwrite;
+ this.confirmDelegate = confirmDelegate;
+ this.targetDirectory = targetDirectory;
+ this.fileFilter = new NameFilter(fileFilter);
+ this.directoryFilter = new NameFilter(directoryFilter);
+
+ inputStream = new ZipInputStream(File.OpenRead(zipFileName));
+
+ try {
+
+ if (password != null) {
+ inputStream.Password = password;
+ }
+
+ ZipEntry entry;
+ while ( (entry = inputStream.GetNextEntry()) != null ) {
+ if ( this.directoryFilter.IsMatch(Path.GetDirectoryName(entry.Name)) && this.fileFilter.IsMatch(entry.Name) ) {
+ ExtractEntry(entry);
+ }
+ }
+ }
+ finally {
+ inputStream.Close();
+ }
+ }
+
+ void ProcessDirectory(object sender, DirectoryEventArgs e)
+ {
+ if ( !e.HasMatchingFiles && createEmptyDirectories ) {
+ if ( events != null ) {
+ events.OnProcessDirectory(e.Name, e.HasMatchingFiles);
+ }
+
+ if (e.Name != sourceDirectory) {
+ string cleanedName = nameTransform.TransformDirectory(e.Name);
+ ZipEntry entry = new ZipEntry(cleanedName);
+ outputStream.PutNextEntry(entry);
+ }
+ }
+ }
+
+ void ProcessFile(object sender, ScanEventArgs e)
+ {
+ if ( events != null ) {
+ events.OnProcessFile(e.Name);
+ }
+ string cleanedName = nameTransform.TransformFile(e.Name);
+ ZipEntry entry = new ZipEntry(cleanedName);
+ outputStream.PutNextEntry(entry);
+ AddFileContents(e.Name);
+ }
+
+ void AddFileContents(string name)
+ {
+ if ( buffer == null ) {
+ buffer = new byte[4096];
+ }
+
+ FileStream stream = File.OpenRead(name);
+ try {
+ int length;
+ do {
+ length = stream.Read(buffer, 0, buffer.Length);
+ outputStream.Write(buffer, 0, length);
+ } while ( length > 0 );
+ }
+ finally {
+ stream.Close();
+ }
+ }
+
+ void ExtractFileEntry(ZipEntry entry, string targetName)
+ {
+ bool proceed = true;
+ if ((overwrite == Overwrite.Prompt) && (confirmDelegate != null)) {
+ if (File.Exists(targetName) == true) {
+ proceed = confirmDelegate(targetName);
+ }
+ }
+
+ if ( proceed ) {
+
+ if ( events != null ) {
+ events.OnProcessFile(entry.Name);
+ }
+
+ FileStream streamWriter = File.Create(targetName);
+
+ try {
+ if ( buffer == null ) {
+ buffer = new byte[4096];
+ }
+
+ int size;
+
+ do {
+ size = inputStream.Read(buffer, 0, buffer.Length);
+ streamWriter.Write(buffer, 0, size);
+ } while (size > 0);
+ }
+ finally {
+ streamWriter.Close();
+ }
+
+ if (restoreDateTime) {
+ File.SetLastWriteTime(targetName, entry.DateTime);
+ }
+ }
+ }
+
+ bool NameIsValid(string name)
+ {
+ return name != null && name.Length > 0 && name.IndexOfAny(Path.InvalidPathChars) < 0;
+ }
+
+ void ExtractEntry(ZipEntry entry)
+ {
+ bool doExtraction = NameIsValid(entry.Name);
+
+ string dirName = null;
+ string targetName = null;
+
+ if ( doExtraction ) {
+ string entryFileName;
+ if (Path.IsPathRooted(entry.Name)) {
+ string workName = Path.GetPathRoot(entry.Name);
+ workName = entry.Name.Substring(workName.Length);
+ entryFileName = Path.Combine(Path.GetDirectoryName(workName), Path.GetFileName(entry.Name));
+ } else {
+ entryFileName = entry.Name;
+ }
+
+ targetName = Path.Combine(targetDirectory, entryFileName);
+ dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
+
+ doExtraction = doExtraction && (entryFileName.Length > 0);
+ }
+
+ if ( doExtraction && !Directory.Exists(dirName) )
+ {
+ if ( !entry.IsDirectory || this.CreateEmptyDirectories ) {
+ try {
+ Directory.CreateDirectory(dirName);
+ }
+ catch {
+ doExtraction = false;
+ }
+ }
+ }
+
+ if ( doExtraction && entry.IsFile ) {
+ ExtractFileEntry(entry, targetName);
+ }
+ }
+
+ /// <summary>
+ /// Get or set the <see cref="ZipNameTransform"> active when creating Zip files.</see>
+ /// </summary>
+ public ZipNameTransform NameTransform
+ {
+ get { return nameTransform; }
+ set {
+ if ( value == null ) {
+ nameTransform = new ZipNameTransform();
+ }
+ else {
+ nameTransform = value;
+ }
+ }
+ }
+
+ #region Instance Fields
+ byte[] buffer;
+ ZipOutputStream outputStream;
+ ZipInputStream inputStream;
+ string password = null;
+ string targetDirectory;
+ string sourceDirectory;
+ NameFilter fileFilter;
+ NameFilter directoryFilter;
+ Overwrite overwrite;
+ ConfirmOverwriteDelegate confirmDelegate;
+ bool restoreDateTime = false;
+ bool createEmptyDirectories = false;
+ FastZipEvents events;
+ ZipNameTransform nameTransform;
+ #endregion
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs
new file mode 100644
index 00000000000..71f2c869b2f
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs
@@ -0,0 +1,465 @@
+// ZipConstants.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.Text;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+
+ /// <summary>
+ /// The kind of compression used for an entry in an archive
+ /// </summary>
+ public enum CompressionMethod
+ {
+ /// <summary>
+ /// A direct copy of the file contents is held in the archive
+ /// </summary>
+ Stored = 0,
+
+ /// <summary>
+ /// Common Zip compression method using a sliding dictionary
+ /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees
+ /// </summary>
+ Deflated = 8,
+
+ /// <summary>
+ /// An extension to deflate with a 64KB window. Not supported by #Zip
+ /// </summary>
+ Deflate64 = 9,
+
+ /// <summary>
+ /// Not supported by #Zip
+ /// </summary>
+ BZip2 = 11,
+
+ /// <summary>
+ /// WinZip special for AES encryption, Not supported by #Zip
+ /// </summary>
+ WinZipAES = 99,
+
+ }
+
+ /// <summary>
+ /// Defines the contents of the general bit flags field for an archive entry.
+ /// </summary>
+ [Flags]
+ enum GeneralBitFlags : int
+ {
+ /// <summary>
+ /// If set indicates that the file is encrypted
+ /// </summary>
+ Encrypted = 0x0001,
+ /// <summary>
+ /// Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating)
+ /// </summary>
+ Method = 0x0006,
+ /// <summary>
+ /// If set a trailing data desciptor is appended to the entry data
+ /// </summary>
+ Descriptor = 0x0008,
+ Reserved = 0x0010,
+ /// <summary>
+ /// If set indicates the file contains Pkzip compressed patched data.
+ /// </summary>
+ Patched = 0x0020,
+ /// <summary>
+ /// If set strong encryption has been used for this entry.
+ /// </summary>
+ StrongEncryption = 0x0040,
+ /// <summary>
+ /// Reserved by PKWare for enhanced compression.
+ /// </summary>
+ EnhancedCompress = 0x1000,
+ /// <summary>
+ /// If set indicates that values in the local header are masked to hide
+ /// their actual values.
+ /// </summary>
+ /// <remarks>
+ /// Used when encrypting ht ecentral directory contents.
+ /// </remarks>
+ HeaderMasked = 0x2000
+ }
+
+ /// <summary>
+ /// This class contains constants used for Zip format files
+ /// </summary>
+ public sealed class ZipConstants
+ {
+ /// <summary>
+ /// The version made by field for entries in the central header when created by this library
+ /// </summary>
+ /// <remarks>
+ /// This is also the Zip version for the library when comparing against the version required to extract
+ /// for an entry. See <see cref="ZipInputStream.CanDecompressEntry">ZipInputStream.CanDecompressEntry</see>.
+ /// </remarks>
+ public const int VERSION_MADE_BY = 20;
+
+ /// <summary>
+ /// The minimum version required to support strong encryption
+ /// </summary>
+ public const int VERSION_STRONG_ENCRYPTION = 50;
+
+ // The local entry header
+
+ /// <summary>
+ /// Size of local entry header (excluding variable length fields at end)
+ /// </summary>
+ public const int LOCHDR = 30;
+
+ /// <summary>
+ /// Signature for local entry header
+ /// </summary>
+ public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24);
+
+ /// <summary>
+ /// Offset of version to extract in local entry header
+ /// </summary>
+ public const int LOCVER = 4;
+
+ /// <summary>
+ /// Offset of general purpose flags in local entry header
+ /// </summary>
+ public const int LOCFLG = 6;
+
+ /// <summary>
+ /// Offset of compression method in local entry header
+ /// </summary>
+ public const int LOCHOW = 8;
+
+ /// <summary>
+ /// Offset of last mod file time + date in local entry header
+ /// </summary>
+ public const int LOCTIM = 10;
+
+ /// <summary>
+ /// Offset of crc-32 in local entry header
+ /// </summary>
+ public const int LOCCRC = 14;
+
+ /// <summary>
+ /// Offset of compressed size in local entry header
+ /// </summary>
+ public const int LOCSIZ = 18;
+
+ /// <summary>
+ /// Offset of uncompressed size in local entry header
+ /// </summary>
+ public const int LOCLEN = 22;
+
+ /// <summary>
+ /// Offset of file name length in local entry header
+ /// </summary>
+ public const int LOCNAM = 26;
+
+ /// <summary>
+ /// Offset of extra field length in local entry header
+ /// </summary>
+ public const int LOCEXT = 28;
+
+
+ /// <summary>
+ /// Signature for spanning entry
+ /// </summary>
+ public const int SPANNINGSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
+
+ /// <summary>
+ /// Signature for temporary spanning entry
+ /// </summary>
+ public const int SPANTEMPSIG = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24);
+
+ /// <summary>
+ /// Signature for data descriptor
+ /// </summary>
+ /// <remarks>
+ /// This is only used where the length, Crc, or compressed size isnt known when the
+ /// entry is created and the output stream doesnt support seeking.
+ /// The local entry cannot be 'patched' with the correct values in this case
+ /// so the values are recorded after the data prefixed by this header, as well as in the central directory.
+ /// </remarks>
+ public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
+
+ /// <summary>
+ /// Size of data descriptor
+ /// </summary>
+ public const int EXTHDR = 16;
+
+ /// <summary>
+ /// Offset of crc-32 in data descriptor
+ /// </summary>
+ public const int EXTCRC = 4;
+
+ /// <summary>
+ /// Offset of compressed size in data descriptor
+ /// </summary>
+ public const int EXTSIZ = 8;
+
+ /// <summary>
+ /// Offset of uncompressed length in data descriptor
+ /// </summary>
+ public const int EXTLEN = 12;
+
+
+ /// <summary>
+ /// Signature for central header
+ /// </summary>
+ public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24);
+
+ /// <summary>
+ /// Size of central header entry
+ /// </summary>
+ public const int CENHDR = 46;
+
+ /// <summary>
+ /// Offset of version made by in central file header
+ /// </summary>
+ public const int CENVEM = 4;
+
+ /// <summary>
+ /// Offset of version needed to extract in central file header
+ /// </summary>
+ public const int CENVER = 6;
+
+ /// <summary>
+ /// Offset of general purpose bit flag in central file header
+ /// </summary>
+ public const int CENFLG = 8;
+
+ /// <summary>
+ /// Offset of compression method in central file header
+ /// </summary>
+ public const int CENHOW = 10;
+
+ /// <summary>
+ /// Offset of time/date in central file header
+ /// </summary>
+ public const int CENTIM = 12;
+
+ /// <summary>
+ /// Offset of crc-32 in central file header
+ /// </summary>
+ public const int CENCRC = 16;
+
+ /// <summary>
+ /// Offset of compressed size in central file header
+ /// </summary>
+ public const int CENSIZ = 20;
+
+ /// <summary>
+ /// Offset of uncompressed size in central file header
+ /// </summary>
+ public const int CENLEN = 24;
+
+ /// <summary>
+ /// Offset of file name length in central file header
+ /// </summary>
+ public const int CENNAM = 28;
+
+ /// <summary>
+ /// Offset of extra field length in central file header
+ /// </summary>
+ public const int CENEXT = 30;
+
+ /// <summary>
+ /// Offset of file comment length in central file header
+ /// </summary>
+ public const int CENCOM = 32;
+
+ /// <summary>
+ /// Offset of disk start number in central file header
+ /// </summary>
+ public const int CENDSK = 34;
+
+ /// <summary>
+ /// Offset of internal file attributes in central file header
+ /// </summary>
+ public const int CENATT = 36;
+
+ /// <summary>
+ /// Offset of external file attributes in central file header
+ /// </summary>
+ public const int CENATX = 38;
+
+ /// <summary>
+ /// Offset of relative offset of local header in central file header
+ /// </summary>
+ public const int CENOFF = 42;
+
+
+ /// <summary>
+ /// Signature for Zip64 central file header
+ /// </summary>
+ public const int CENSIG64 = 'P' | ('K' << 8) | (6 << 16) | (6 << 24);
+
+
+
+ /// <summary>
+ /// Central header digitial signature
+ /// </summary>
+ public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24);
+
+
+ // The entries at the end of central directory
+
+ /// <summary>
+ /// End of central directory record signature
+ /// </summary>
+ public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24);
+
+ /// <summary>
+ /// Size of end of central record (excluding variable fields)
+ /// </summary>
+ public const int ENDHDR = 22;
+
+ // The following two fields are missing in SUN JDK
+
+ /// <summary>
+ /// Offset of number of this disk
+ /// </summary>
+ public const int ENDNRD = 4;
+
+ /// <summary>
+ /// Offset of number of disk with start of central directory
+ /// </summary>
+ public const int ENDDCD = 6;
+
+ /// <summary>
+ /// Offset of number of entries in the central directory of this disk
+ /// </summary>
+ public const int ENDSUB = 8;
+
+ /// <summary>
+ /// Offset of total number of entries in the central directory
+ /// </summary>
+ public const int ENDTOT = 10;
+
+ /// <summary>
+ /// Offset of size of central directory
+ /// </summary>
+ public const int ENDSIZ = 12;
+
+ /// <summary>
+ /// Offset of offset of start of central directory with respect to starting disk number
+ /// </summary>
+ public const int ENDOFF = 16;
+
+ /// <summary>
+ /// Offset of ZIP file comment length
+ /// </summary>
+ public const int ENDCOM = 20;
+
+ /// <summary>
+ /// Size of cryptographic header stored before entry data
+ /// </summary>
+ public const int CRYPTO_HEADER_SIZE = 12;
+
+
+#if !COMPACT_FRAMEWORK
+
+ static int defaultCodePage = 0;
+
+ /// <summary>
+ /// Default encoding used for string conversion. 0 gives the default system Ansi code page.
+ /// Dont use unicode encodings if you want to be Zip compatible!
+ /// Using the default code page isnt the full solution neccessarily
+ /// there are many variable factors, codepage 850 is often a good choice for
+ /// European users, however be careful about compatability.
+ /// </summary>
+ public static int DefaultCodePage {
+ get {
+ return defaultCodePage;
+ }
+ set {
+ defaultCodePage = value;
+ }
+ }
+#endif
+
+ /// <summary>
+ /// Convert a portion of a byte array to a string.
+ /// </summary>
+ /// <param name="data">
+ /// Data to convert to string
+ /// </param>
+ /// <param name="length">
+ /// Number of bytes to convert starting from index 0
+ /// </param>
+ /// <returns>
+ /// data[0]..data[length - 1] converted to a string
+ /// </returns>
+ public static string ConvertToString(byte[] data, int length)
+ {
+#if COMPACT_FRAMEWORK
+ return Encoding.ASCII.GetString(data, 0, length);
+#else
+ return Encoding.GetEncoding(DefaultCodePage).GetString(data, 0, length);
+#endif
+ }
+
+ /// <summary>
+ /// Convert byte array to string
+ /// </summary>
+ /// <param name="data">
+ /// Byte array to convert
+ /// </param>
+ /// <returns>
+ /// <paramref name="data">data</paramref>converted to a string
+ /// </returns>
+ public static string ConvertToString(byte[] data)
+ {
+ return ConvertToString(data, data.Length);
+ }
+
+ /// <summary>
+ /// Convert a string to a byte array
+ /// </summary>
+ /// <param name="str">
+ /// String to convert to an array
+ /// </param>
+ /// <returns>Converted array</returns>
+ public static byte[] ConvertToArray(string str)
+ {
+#if COMPACT_FRAMEWORK
+ return Encoding.ASCII.GetBytes(str);
+#else
+ return Encoding.GetEncoding(DefaultCodePage).GetBytes(str);
+#endif
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
new file mode 100644
index 00000000000..446d7c67c92
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
@@ -0,0 +1,733 @@
+// ZipEntry.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+
+ /// <summary>
+ /// This class represents an entry in a zip archive. This can be a file
+ /// or a directory
+ /// ZipFile and ZipInputStream will give you instances of this class as
+ /// information about the members in an archive. ZipOutputStream
+ /// uses an instance of this class when creating an entry in a Zip file.
+ /// <br/>
+ /// <br/>Author of the original java version : Jochen Hoenicke
+ /// </summary>
+ public class ZipEntry : ICloneable
+ {
+ static int KNOWN_SIZE = 1;
+ static int KNOWN_CSIZE = 2;
+ static int KNOWN_CRC = 4;
+ static int KNOWN_TIME = 8;
+ static int KNOWN_EXTERN_ATTRIBUTES = 16;
+
+ ushort known = 0; // Bit flags made up of above bits
+ int externalFileAttributes = -1; // contains external attributes (os dependant)
+
+ ushort versionMadeBy; // Contains host system and version information
+ // only relevant for central header entries
+
+ string name;
+ ulong size;
+ ulong compressedSize;
+ ushort versionToExtract; // Version required to extract (library handles <= 2.0)
+ uint crc;
+ uint dosTime;
+
+ CompressionMethod method = CompressionMethod.Deflated;
+ byte[] extra = null;
+ string comment = null;
+
+ int flags; // general purpose bit flags
+
+ int zipFileIndex = -1; // used by ZipFile
+ int offset; // used by ZipFile and ZipOutputStream
+
+ /// <summary>
+ /// Get/Set flag indicating if entry is encrypted.
+ /// A simple helper routine to aid interpretation of <see cref="Flags">flags</see>
+ /// </summary>
+ public bool IsCrypted {
+ get {
+ return (flags & 1) != 0;
+ }
+ set {
+ if (value) {
+ flags |= 1;
+ } else {
+ flags &= ~1;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Get/Set general purpose bit flag for entry
+ /// </summary>
+ /// <remarks>
+ /// General purpose bit flag<br/>
+ /// Bit 0: If set, indicates the file is encrypted<br/>
+ /// Bit 1-2 Only used for compression type 6 Imploding, and 8, 9 deflating<br/>
+ /// Imploding:<br/>
+ /// Bit 1 if set indicates an 8K sliding dictionary was used. If clear a 4k dictionary was used<br/>
+ /// Bit 2 if set indicates 3 Shannon-Fanno trees were used to encode the sliding dictionary, 2 otherwise<br/>
+ /// <br/>
+ /// Deflating:<br/>
+ /// Bit 2 Bit 1<br/>
+ /// 0 0 Normal compression was used<br/>
+ /// 0 1 Maximum compression was used<br/>
+ /// 1 0 Fast compression was used<br/>
+ /// 1 1 Super fast compression was used<br/>
+ /// <br/>
+ /// Bit 3: If set, the fields crc-32, compressed size
+ /// and uncompressed size are were not able to be written during zip file creation
+ /// The correct values are held in a data descriptor immediately following the compressed data. <br/>
+ /// Bit 4: Reserved for use by PKZIP for enhanced deflating<br/>
+ /// Bit 5: If set indicates the file contains compressed patch data<br/>
+ /// Bit 6: If set indicates strong encryption was used.<br/>
+ /// Bit 7-15: Unused or reserved<br/>
+ /// </remarks>
+ public int Flags {
+ get {
+ return flags;
+ }
+ set {
+ flags = value;
+ }
+ }
+
+
+ /// <summary>
+ /// Get/Set index of this entry in Zip file
+ /// </summary>
+ public int ZipFileIndex {
+ get {
+ return zipFileIndex;
+ }
+ set {
+ zipFileIndex = value;
+ }
+ }
+
+ /// <summary>
+ /// Get/set offset for use in central header
+ /// </summary>
+ public int Offset {
+ get {
+ return offset;
+ }
+ set {
+ if (((ulong)value & 0xFFFFFFFF00000000L) != 0) {
+ throw new ArgumentOutOfRangeException("Offset");
+ }
+ offset = value;
+ }
+ }
+
+
+ /// <summary>
+ /// Get/Set external file attributes as an integer.
+ /// The values of this are operating system dependant see
+ /// <see cref="HostSystem">HostSystem</see> for details
+ /// </summary>
+ public int ExternalFileAttributes {
+ get {
+ if ((known & KNOWN_EXTERN_ATTRIBUTES) == 0) {
+ return -1;
+ } else {
+ return externalFileAttributes;
+ }
+ }
+
+ set {
+ externalFileAttributes = value;
+ known |= (ushort)KNOWN_EXTERN_ATTRIBUTES;
+ }
+ }
+
+ /// <summary>
+ /// Get the version made by for this entry or zero if unknown.
+ /// The value / 10 indicates the major version number, and
+ /// the value mod 10 is the minor version number
+ /// </summary>
+ public int VersionMadeBy {
+ get {
+ return versionMadeBy & 0xff;
+ }
+ }
+
+ /// <summary>
+ /// Gets the compatability information for the <see cref="ExternalFileAttributes">external file attribute</see>
+ /// If the external file attributes are compatible with MS-DOS and can be read
+ /// by PKZIP for DOS version 2.04g then this value will be zero. Otherwise the value
+ /// will be non-zero and identify the host system on which the attributes are compatible.
+ /// </summary>
+ ///
+ /// <remarks>
+ /// The values for this as defined in the Zip File format and by others are shown below. The values are somewhat
+ /// misleading in some cases as they are not all used as shown. You should consult the relevant documentation
+ /// to obtain up to date and correct information. The modified appnote by the infozip group is
+ /// particularly helpful as it documents a lot of peculiarities. The document is however a little dated.
+ /// <list type="table">
+ /// <item>0 - MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)</item>
+ /// <item>1 - Amiga</item>
+ /// <item>2 - OpenVMS</item>
+ /// <item>3 - Unix</item>
+ /// <item>4 - VM/CMS</item>
+ /// <item>5 - Atari ST</item>
+ /// <item>6 - OS/2 HPFS</item>
+ /// <item>7 - Macintosh</item>
+ /// <item>8 - Z-System</item>
+ /// <item>9 - CP/M</item>
+ /// <item>10 - Windows NTFS</item>
+ /// <item>11 - MVS (OS/390 - Z/OS)</item>
+ /// <item>12 - VSE</item>
+ /// <item>13 - Acorn Risc</item>
+ /// <item>14 - VFAT</item>
+ /// <item>15 - Alternate MVS</item>
+ /// <item>16 - BeOS</item>
+ /// <item>17 - Tandem</item>
+ /// <item>18 - OS/400</item>
+ /// <item>19 - OS/X (Darwin)</item>
+ /// <item>99 - WinZip AES</item>
+ /// <item>remainder - unused</item>
+ /// </list>
+ /// </remarks>
+
+ public int HostSystem {
+ get { return (versionMadeBy >> 8) & 0xff; }
+ }
+
+ /// <summary>
+ /// Creates a zip entry with the given name.
+ /// </summary>
+ /// <param name="name">
+ /// The name for this entry. Can include directory components.
+ /// The convention for names is 'unix' style paths with no device names and
+ /// path elements separated by '/' characters. This is not enforced see <see cref="CleanName">CleanName</see>
+ /// on how to ensure names are valid if this is desired.
+ /// </param>
+ /// <exception cref="ArgumentNullException">
+ /// The name passed is null
+ /// </exception>
+ public ZipEntry(string name) : this(name, 0, ZipConstants.VERSION_MADE_BY)
+ {
+ }
+
+ /// <summary>
+ /// Creates a zip entry with the given name and version required to extract
+ /// </summary>
+ /// <param name="name">
+ /// The name for this entry. Can include directory components.
+ /// The convention for names is 'unix' style paths with no device names and
+ /// path elements separated by '/' characters. This is not enforced see <see cref="CleanName">CleanName</see>
+ /// on how to ensure names are valid if this is desired.
+ /// </param>
+ /// <param name="versionRequiredToExtract">
+ /// The minimum 'feature version' required this entry
+ /// </param>
+ /// <exception cref="ArgumentNullException">
+ /// The name passed is null
+ /// </exception>
+ internal ZipEntry(string name, int versionRequiredToExtract) : this(name, versionRequiredToExtract, ZipConstants.VERSION_MADE_BY)
+ {
+ }
+
+ /// <summary>
+ /// Initializes an entry with the given name and made by information
+ /// </summary>
+ /// <param name="name">Name for this entry</param>
+ /// <param name="madeByInfo">Version and HostSystem Information</param>
+ /// <param name="versionRequiredToExtract">Minimum required zip feature version required to extract this entry</param>
+ /// <exception cref="ArgumentNullException">
+ /// The name passed is null
+ /// </exception>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// versionRequiredToExtract should be 0 (auto-calculate) or > 10
+ /// </exception>
+ /// <remarks>
+ /// This constructor is used by the ZipFile class when reading from the central header
+ /// It is not generally useful, use the constructor specifying the name only.
+ /// </remarks>
+ internal ZipEntry(string name, int versionRequiredToExtract, int madeByInfo)
+ {
+ if (name == null) {
+ throw new System.ArgumentNullException("ZipEntry name");
+ }
+
+ if ( name.Length == 0 ) {
+ throw new ArgumentException("ZipEntry name is empty");
+ }
+
+ if (versionRequiredToExtract != 0 && versionRequiredToExtract < 10) {
+ throw new ArgumentOutOfRangeException("versionRequiredToExtract");
+ }
+
+ this.DateTime = System.DateTime.Now;
+ this.name = name;
+ this.versionMadeBy = (ushort)madeByInfo;
+ this.versionToExtract = (ushort)versionRequiredToExtract;
+ }
+
+ /// <summary>
+ /// Creates a copy of the given zip entry.
+ /// </summary>
+ /// <param name="e">
+ /// The entry to copy.
+ /// </param>
+ public ZipEntry(ZipEntry e)
+ {
+ known = e.known;
+ name = e.name;
+ size = e.size;
+ compressedSize = e.compressedSize;
+ crc = e.crc;
+ dosTime = e.dosTime;
+ method = e.method;
+ ExtraData = e.ExtraData; // Note use of property ensuring data is unique
+ comment = e.comment;
+ versionToExtract = e.versionToExtract;
+ versionMadeBy = e.versionMadeBy;
+ externalFileAttributes = e.externalFileAttributes;
+ flags = e.flags;
+
+ zipFileIndex = -1;
+ offset = 0;
+ }
+
+ /// <summary>
+ /// Get minimum Zip feature version required to extract this entry
+ /// </summary>
+ /// <remarks>
+ /// Minimum features are defined as:<br/>
+ /// 1.0 - Default value<br/>
+ /// 1.1 - File is a volume label<br/>
+ /// 2.0 - File is a folder/directory<br/>
+ /// 2.0 - File is compressed using Deflate compression<br/>
+ /// 2.0 - File is encrypted using traditional encryption<br/>
+ /// 2.1 - File is compressed using Deflate64<br/>
+ /// 2.5 - File is compressed using PKWARE DCL Implode<br/>
+ /// 2.7 - File is a patch data set<br/>
+ /// 4.5 - File uses Zip64 format extensions<br/>
+ /// 4.6 - File is compressed using BZIP2 compression<br/>
+ /// 5.0 - File is encrypted using DES<br/>
+ /// 5.0 - File is encrypted using 3DES<br/>
+ /// 5.0 - File is encrypted using original RC2 encryption<br/>
+ /// 5.0 - File is encrypted using RC4 encryption<br/>
+ /// 5.1 - File is encrypted using AES encryption<br/>
+ /// 5.1 - File is encrypted using corrected RC2 encryption<br/>
+ /// 5.1 - File is encrypted using corrected RC2-64 encryption<br/>
+ /// 6.1 - File is encrypted using non-OAEP key wrapping<br/>
+ /// 6.2 - Central directory encryption (not confirmed yet)<br/>
+ /// </remarks>
+ public int Version {
+ get {
+ if (versionToExtract != 0) {
+ return versionToExtract;
+ } else {
+ int result = 10;
+ if (CompressionMethod.Deflated == method) {
+ result = 20;
+ } else if (IsDirectory == true) {
+ result = 20;
+ } else if (IsCrypted == true) {
+ result = 20;
+ } else if ((known & KNOWN_EXTERN_ATTRIBUTES) != 0 && (externalFileAttributes & 0x08) != 0) {
+ result = 11;
+ }
+ return result;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating if the entry requires Zip64 extensions to be stored
+ /// </summary>
+ public bool RequiresZip64 {
+ get {
+ return (this.size > uint.MaxValue) || (this.compressedSize > uint.MaxValue);
+ }
+ }
+
+ /// <summary>
+ /// Get/Set DosTime
+ /// </summary>
+ public long DosTime {
+ get {
+ if ((known & KNOWN_TIME) == 0) {
+ return 0;
+ } else {
+ return dosTime;
+ }
+ }
+ set {
+ this.dosTime = (uint)value;
+ known |= (ushort)KNOWN_TIME;
+ }
+ }
+
+
+ /// <summary>
+ /// Gets/Sets the time of last modification of the entry.
+ /// </summary>
+ public DateTime DateTime {
+ get {
+ // Although technically not valid some archives have dates set to zero.
+ // This mimics some archivers handling and is a good a cludge as any probably.
+ if ( dosTime == 0 ) {
+ return DateTime.Now;
+ }
+ else {
+ uint sec = 2 * (dosTime & 0x1f);
+ uint min = (dosTime >> 5) & 0x3f;
+ uint hrs = (dosTime >> 11) & 0x1f;
+ uint day = (dosTime >> 16) & 0x1f;
+ uint mon = ((dosTime >> 21) & 0xf);
+ uint year = ((dosTime >> 25) & 0x7f) + 1980;
+ return new System.DateTime((int)year, (int)mon, (int)day, (int)hrs, (int)min, (int)sec);
+ }
+ }
+ set {
+ DosTime = ((uint)value.Year - 1980 & 0x7f) << 25 |
+ ((uint)value.Month) << 21 |
+ ((uint)value.Day) << 16 |
+ ((uint)value.Hour) << 11 |
+ ((uint)value.Minute) << 5 |
+ ((uint)value.Second) >> 1;
+ }
+ }
+
+ /// <summary>
+ /// Returns the entry name. The path components in the entry should
+ /// always separated by slashes ('/'). Dos device names like C: should also
+ /// be removed. See <see cref="CleanName">CleanName</see>.
+ /// </summary>
+ public string Name {
+ get {
+ return name;
+ }
+ }
+
+ /// <summary>
+ /// Cleans a name making it conform to Zip file conventions.
+ /// Devices names ('c:\') and UNC share names ('\\server\share') are removed
+ /// and forward slashes ('\') are converted to back slashes ('/').
+ /// </summary>
+ /// <param name="name">Name to clean</param>
+ /// <param name="relativePath">Make names relative if true or absolute if false</param>
+ static public string CleanName(string name, bool relativePath)
+ {
+ if (name == null) {
+ return "";
+ }
+
+ if (Path.IsPathRooted(name) == true) {
+ // NOTE:
+ // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt
+ name = name.Substring(Path.GetPathRoot(name).Length);
+ }
+
+ name = name.Replace(@"\", "/");
+
+ if (relativePath == true) {
+ if (name.Length > 0 && (name[0] == Path.AltDirectorySeparatorChar || name[0] == Path.DirectorySeparatorChar)) {
+ name = name.Remove(0, 1);
+ }
+ } else {
+ if (name.Length > 0 && name[0] != Path.AltDirectorySeparatorChar && name[0] != Path.DirectorySeparatorChar) {
+ name = name.Insert(0, "/");
+ }
+ }
+ return name;
+ }
+
+ /// <summary>
+ /// Cleans a name making it conform to Zip file conventions.
+ /// Devices names ('c:\') and UNC share names ('\\server\share') are removed
+ /// and forward slashes ('\') are converted to back slashes ('/').
+ /// Names are made relative by trimming leading slashes which is compatible
+ /// with Windows-XPs built in Zip file handling.
+ /// </summary>
+ /// <param name="name">Name to clean</param>
+ static public string CleanName(string name)
+ {
+ return CleanName(name, true);
+ }
+
+ /// <summary>
+ /// Gets/Sets the size of the uncompressed data.
+ /// </summary>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// If the size is not in the range 0..0xffffffffL
+ /// </exception>
+ /// <returns>
+ /// The size or -1 if unknown.
+ /// </returns>
+ public long Size {
+ get {
+ return (known & KNOWN_SIZE) != 0 ? (long)size : -1L;
+ }
+ set {
+ if (((ulong)value & 0xFFFFFFFF00000000L) != 0) {
+ throw new ArgumentOutOfRangeException("size");
+ }
+ this.size = (ulong)value;
+ this.known |= (ushort)KNOWN_SIZE;
+ }
+ }
+
+ /// <summary>
+ /// Gets/Sets the size of the compressed data.
+ /// </summary>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// Size is not in the range 0..0xffffffff
+ /// </exception>
+ /// <returns>
+ /// The size or -1 if unknown.
+ /// </returns>
+ public long CompressedSize {
+ get {
+ return (known & KNOWN_CSIZE) != 0 ? (long)compressedSize : -1L;
+ }
+ set {
+ if (((ulong)value & 0xffffffff00000000L) != 0) {
+ throw new ArgumentOutOfRangeException();
+ }
+ this.compressedSize = (ulong)value;
+ this.known |= (ushort)KNOWN_CSIZE;
+ }
+ }
+
+ /// <summary>
+ /// Gets/Sets the crc of the uncompressed data.
+ /// </summary>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// Crc is not in the range 0..0xffffffffL
+ /// </exception>
+ /// <returns>
+ /// The crc value or -1 if unknown.
+ /// </returns>
+ public long Crc {
+ get {
+ return (known & KNOWN_CRC) != 0 ? crc & 0xffffffffL : -1L;
+ }
+ set {
+ if (((ulong)crc & 0xffffffff00000000L) != 0) {
+ throw new ArgumentOutOfRangeException();
+ }
+ this.crc = (uint)value;
+ this.known |= (ushort)KNOWN_CRC;
+ }
+ }
+
+ /// <summary>
+ /// Gets/Sets the compression method. Only Deflated and Stored are supported.
+ /// </summary>
+ /// <returns>
+ /// The compression method for this entry
+ /// </returns>
+ /// <see cref="ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated"/>
+ /// <see cref="ICSharpCode.SharpZipLib.Zip.CompressionMethod.Stored"/>
+ public CompressionMethod CompressionMethod {
+ get {
+ return method;
+ }
+ set {
+ this.method = value;
+ }
+ }
+
+ /// <summary>
+ /// Gets/Sets the extra data.
+ /// </summary>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// Extra data is longer than 0xffff bytes.
+ /// </exception>
+ /// <returns>
+ /// Extra data or null if not set.
+ /// </returns>
+ public byte[] ExtraData {
+ get {
+ return extra;
+ }
+ set {
+ if (value == null) {
+ this.extra = null;
+ return;
+ }
+
+ if (value.Length > 0xffff) {
+ throw new System.ArgumentOutOfRangeException();
+ }
+
+ this.extra = new byte[value.Length];
+ Array.Copy(value, 0, this.extra, 0, value.Length);
+
+ try {
+ int pos = 0;
+ while (pos < extra.Length) {
+ int sig = (extra[pos++] & 0xff) | (extra[pos++] & 0xff) << 8;
+ int len = (extra[pos++] & 0xff) | (extra[pos++] & 0xff) << 8;
+
+ if (len < 0 || pos + len > extra.Length) {
+ // This is still lenient but the extra data is corrupt
+ // TODO: drop the extra data? or somehow indicate to user
+ // there is a problem...
+ break;
+ }
+
+ if (sig == 0x5455) {
+ // extended time stamp, unix format by Rainer Prem <Rainer@Prem.de>
+ int flags = extra[pos];
+ // Can include other times but these are ignored. Length of data should
+ // actually be 1 + 4 * no of bits in flags.
+ if ((flags & 1) != 0 && len >= 5) {
+ int iTime = ((extra[pos+1] & 0xff) |
+ (extra[pos + 2] & 0xff) << 8 |
+ (extra[pos + 3] & 0xff) << 16 |
+ (extra[pos + 4] & 0xff) << 24);
+
+ DateTime = (new DateTime ( 1970, 1, 1, 0, 0, 0 ) + new TimeSpan ( 0, 0, 0, iTime, 0 )).ToLocalTime ();
+ known |= (ushort)KNOWN_TIME;
+ }
+ } else if (sig == 0x0001) {
+ // ZIP64 extended information extra field
+ // Of variable size depending on which fields in header are too small
+ // fields appear here if the corresponding local or central directory record field
+ // is set to 0xFFFF or 0xFFFFFFFF and the entry is in Zip64 format.
+ //
+ // Original Size 8 bytes
+ // Compressed size 8 bytes
+ // Relative header offset 8 bytes
+ // Disk start number 4 bytes
+ }
+ pos += len;
+ }
+ } catch (Exception) {
+ /* be lenient */
+ return;
+ }
+ }
+ }
+
+
+ /// <summary>
+ /// Gets/Sets the entry comment.
+ /// </summary>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// If comment is longer than 0xffff.
+ /// </exception>
+ /// <returns>
+ /// The comment or null if not set.
+ /// </returns>
+ public string Comment {
+ get {
+ return comment;
+ }
+ set {
+ // TODO: this test is strictly incorrect as the length is in characters
+ // While the test is correct in that a comment of this length or greater
+ // is definitely invalid, shorter comments may also have an invalid length.
+ if (value != null && value.Length > 0xffff) {
+ throw new ArgumentOutOfRangeException();
+ }
+ this.comment = value;
+ }
+ }
+
+ /// <summary>
+ /// Gets a value indicating of the if the entry is a directory. A directory is determined by
+ /// an entry name with a trailing slash '/'. The external file attributes
+ /// can also mark a file as a directory. The trailing slash convention should always be followed
+ /// however.
+ /// </summary>
+ public bool IsDirectory {
+ get {
+ int nlen = name.Length;
+ bool result = nlen > 0 && name[nlen - 1] == '/';
+
+ if (result == false && (known & KNOWN_EXTERN_ATTRIBUTES) != 0) {
+ if (HostSystem == 0 && (ExternalFileAttributes & 16) != 0) {
+ result = true;
+ }
+ }
+ return result;
+ }
+ }
+
+ /// <summary>
+ /// Get a value of true if the entry appears to be a file; false otherwise
+ /// </summary>
+ /// <remarks>
+ /// This only takes account Windows attributes. Other operating systems are ignored.
+ /// For linux and others the result may be incorrect.
+ /// </remarks>
+ public bool IsFile {
+ get {
+ bool result = !IsDirectory;
+
+ // Exclude volume labels
+ if ( result && (known & KNOWN_EXTERN_ATTRIBUTES) != 0) {
+ if (HostSystem == 0 && (ExternalFileAttributes & 8) != 0) {
+ result = false;
+ }
+ }
+ return result;
+ }
+ }
+
+ /// <summary>
+ /// Creates a copy of this zip entry.
+ /// </summary>
+ public object Clone()
+ {
+ return this.MemberwiseClone();
+ }
+
+ /// <summary>
+ /// Gets the string representation of this ZipEntry.
+ /// </summary>
+ public override string ToString()
+ {
+ return name;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipException.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipException.cs
new file mode 100644
index 00000000000..15c02550afa
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipException.cs
@@ -0,0 +1,63 @@
+// ZipException.cs
+//
+// Copyright (C) 2001 Mike Krueger
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+
+ /// <summary>
+ /// Represents errors specific to Zip file handling
+ /// </summary>
+ public class ZipException : SharpZipBaseException
+ {
+ /// <summary>
+ /// Initializes a new instance of the ZipException class.
+ /// </summary>
+ public ZipException()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the ZipException class with a specified error message.
+ /// </summary>
+ public ZipException(string msg) : base(msg)
+ {
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
new file mode 100644
index 00000000000..78055937aa3
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
@@ -0,0 +1,1012 @@
+// ZipFile.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.Security.Cryptography;
+using System.Collections;
+using System.IO;
+using System.Text;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Encryption;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+
+ /// <summary>
+ /// Arguments used with KeysRequiredEvent
+ /// </summary>
+ public class KeysRequiredEventArgs : EventArgs
+ {
+ string fileName;
+
+ /// <summary>
+ /// Get the name of the file for which keys are required.
+ /// </summary>
+ public string FileName
+ {
+ get { return fileName; }
+ }
+
+ byte[] key;
+
+ /// <summary>
+ /// Get/set the key value
+ /// </summary>
+ public byte[] Key
+ {
+ get { return key; }
+ set { key = value; }
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
+ /// </summary>
+ /// <param name="name">The name of the file for which keys are required.</param>
+ public KeysRequiredEventArgs(string name)
+ {
+ fileName = name;
+ }
+
+ /// <summary>
+ /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
+ /// </summary>
+ /// <param name="name">The name of the file for which keys are required.</param>
+ /// <param name="keyValue">The current key value.</param>
+ public KeysRequiredEventArgs(string name, byte[] keyValue)
+ {
+ fileName = name;
+ key = keyValue;
+ }
+ }
+
+ /// <summary>
+ /// This class represents a Zip archive. You can ask for the contained
+ /// entries, or get an input stream for a file entry. The entry is
+ /// automatically decompressed.
+ ///
+ /// This class is thread safe: You can open input streams for arbitrary
+ /// entries in different threads.
+ /// <br/>
+ /// <br/>Author of the original java version : Jochen Hoenicke
+ /// </summary>
+ /// <example>
+ /// <code>
+ /// using System;
+ /// using System.Text;
+ /// using System.Collections;
+ /// using System.IO;
+ ///
+ /// using ICSharpCode.SharpZipLib.Zip;
+ ///
+ /// class MainClass
+ /// {
+ /// static public void Main(string[] args)
+ /// {
+ /// ZipFile zFile = new ZipFile(args[0]);
+ /// Console.WriteLine("Listing of : " + zFile.Name);
+ /// Console.WriteLine("");
+ /// Console.WriteLine("Raw Size Size Date Time Name");
+ /// Console.WriteLine("-------- -------- -------- ------ ---------");
+ /// foreach (ZipEntry e in zFile) {
+ /// DateTime d = e.DateTime;
+ /// Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
+ /// d.ToString("dd-MM-yy"), d.ToString("t"),
+ /// e.Name);
+ /// }
+ /// }
+ /// }
+ /// </code>
+ /// </example>
+ public class ZipFile : IEnumerable
+ {
+ string name;
+ string comment;
+ Stream baseStream;
+ bool isStreamOwner = true;
+ long offsetOfFirstEntry = 0;
+ ZipEntry[] entries;
+
+ #region KeyHandling
+
+ /// <summary>
+ /// Delegate for handling keys/password setting during compresion/decompression.
+ /// </summary>
+ public delegate void KeysRequiredEventHandler(
+ object sender,
+ KeysRequiredEventArgs e
+ );
+
+ /// <summary>
+ /// Event handler for handling encryption keys.
+ /// </summary>
+ public KeysRequiredEventHandler KeysRequired;
+
+ /// <summary>
+ /// Handles getting of encryption keys when required.
+ /// </summary>
+ /// <param name="fileName">The file for which encryptino keys are required.</param>
+ void OnKeysRequired(string fileName)
+ {
+ if (KeysRequired != null) {
+ KeysRequiredEventArgs krea = new KeysRequiredEventArgs(fileName, key);
+ KeysRequired(this, krea);
+ key = krea.Key;
+ }
+ }
+
+ byte[] key = null;
+
+ /// <summary>
+ /// Get/set the encryption key value.
+ /// </summary>
+ byte[] Key
+ {
+ get { return key; }
+ set { key = value; }
+ }
+
+ /// <summary>
+ /// Password to be used for encrypting/decrypting files.
+ /// </summary>
+ /// <remarks>Set to null if no password is required.</remarks>
+ public string Password
+ {
+ set
+ {
+ if ( (value == null) || (value.Length == 0) ) {
+ key = null;
+ }
+ else {
+ key = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(value));
+ }
+ }
+ }
+
+ byte[] iv = null;
+
+ bool HaveKeys
+ {
+ get { return key != null; }
+ }
+ #endregion
+
+ /// <summary>
+ /// Opens a Zip file with the given name for reading.
+ /// </summary>
+ /// <exception cref="IOException">
+ /// An i/o error occurs
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// The file doesn't contain a valid zip archive.
+ /// </exception>
+ public ZipFile(string name)
+ {
+ this.name = name;
+ this.baseStream = File.OpenRead(name);
+ try {
+ ReadEntries();
+ }
+ catch {
+ Close();
+ throw;
+ }
+ }
+
+ /// <summary>
+ /// Opens a Zip file reading the given FileStream
+ /// </summary>
+ /// <exception cref="IOException">
+ /// An i/o error occurs.
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// The file doesn't contain a valid zip archive.
+ /// </exception>
+ public ZipFile(FileStream file)
+ {
+ this.baseStream = file;
+ this.name = file.Name;
+ try {
+ ReadEntries();
+ }
+ catch {
+ Close();
+ throw;
+ }
+ }
+
+ /// <summary>
+ /// Opens a Zip file reading the given Stream
+ /// </summary>
+ /// <exception cref="IOException">
+ /// An i/o error occurs
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// The file doesn't contain a valid zip archive.<br/>
+ /// The stream provided cannot seek
+ /// </exception>
+ public ZipFile(Stream baseStream)
+ {
+ this.baseStream = baseStream;
+ this.name = null;
+ try {
+ ReadEntries();
+ }
+ catch {
+ Close();
+ throw;
+ }
+ }
+
+
+ /// <summary>
+ /// Get/set a flag indicating if the underlying stream is owned by the ZipFile instance.
+ /// If the flag is true then the stream will be closed when <see cref="Close">Close</see> is called.
+ /// </summary>
+ /// <remarks>
+ /// The default value is true in all cases.
+ /// </remarks>
+ bool IsStreamOwner
+ {
+ get { return isStreamOwner; }
+ set { isStreamOwner = value; }
+ }
+
+ /// <summary>
+ /// Read an unsigned short in little endian byte order.
+ /// </summary>
+ /// <returns>Returns the value read.</returns>
+ /// <exception cref="IOException">
+ /// An i/o error occurs.
+ /// </exception>
+ /// <exception cref="EndOfStreamException">
+ /// The file ends prematurely
+ /// </exception>
+ int ReadLeShort()
+ {
+ return baseStream.ReadByte() | baseStream.ReadByte() << 8;
+ }
+
+ /// <summary>
+ /// Read an int in little endian byte order.
+ /// </summary>
+ /// <returns>Returns the value read.</returns>
+ /// <exception cref="IOException">
+ /// An i/o error occurs.
+ /// </exception>
+ /// <exception cref="System.IO.EndOfStreamException">
+ /// The file ends prematurely
+ /// </exception>
+ int ReadLeInt()
+ {
+ return ReadLeShort() | ReadLeShort() << 16;
+ }
+
+ // NOTE this returns the offset of the first byte after the signature.
+ long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
+ {
+ long pos = endLocation - minimumBlockSize;
+ if (pos < 0) {
+ return -1;
+ }
+
+ long giveUpMarker = Math.Max(pos - maximumVariableData, 0);
+
+ // TODO: this loop could be optimised for speed.
+ do
+ {
+ if (pos < giveUpMarker) {
+ return -1;
+ }
+ baseStream.Seek(pos--, SeekOrigin.Begin);
+ } while (ReadLeInt() != signature);
+
+ return baseStream.Position;
+ }
+
+ /// <summary>
+ /// Search for and read the central directory of a zip file filling the entries
+ /// array. This is called exactly once by the constructors.
+ /// </summary>
+ /// <exception cref="System.IO.IOException">
+ /// An i/o error occurs.
+ /// </exception>
+ /// <exception cref="ICSharpCode.SharpZipLib.Zip.ZipException">
+ /// The central directory is malformed or cannot be found
+ /// </exception>
+ void ReadEntries()
+ {
+ // Search for the End Of Central Directory. When a zip comment is
+ // present the directory may start earlier.
+ //
+ // TODO: The search is limited to 64K which is the maximum size of a trailing comment field to aid speed.
+ // This should be compatible with both SFX and ZIP files but has only been tested for Zip files
+ // Need to confirm this is valid in all cases.
+ // Could also speed this up by reading memory in larger blocks.
+
+ if (baseStream.CanSeek == false) {
+ throw new ZipException("ZipFile stream must be seekable");
+ }
+
+ long locatedCentralDirOffset = LocateBlockWithSignature(ZipConstants.ENDSIG, baseStream.Length, ZipConstants.ENDHDR, 0xffff);
+ if (locatedCentralDirOffset < 0) {
+ throw new ZipException("Cannot find central directory");
+ }
+
+ int thisDiskNumber = ReadLeShort();
+ int startCentralDirDisk = ReadLeShort();
+ int entriesForThisDisk = ReadLeShort();
+ int entriesForWholeCentralDir = ReadLeShort();
+ int centralDirSize = ReadLeInt();
+ int offsetOfCentralDir = ReadLeInt();
+ int commentSize = ReadLeShort();
+
+ byte[] zipComment = new byte[commentSize];
+ baseStream.Read(zipComment, 0, zipComment.Length);
+ comment = ZipConstants.ConvertToString(zipComment);
+
+/* Its seems possible that this is too strict, more digging required.
+ if (thisDiskNumber != 0 || startCentralDirDisk != 0 || entriesForThisDisk != entriesForWholeCentralDir) {
+ throw new ZipException("Spanned archives are not currently handled");
+ }
+*/
+
+ entries = new ZipEntry[entriesForWholeCentralDir];
+
+ // SFX support, find the offset of the first entry vis the start of the stream
+ // This applies to Zip files that are appended to the end of the SFX stub.
+ // Zip files created by some archivers have the offsets altered to reflect the true offsets
+ // and so dont require any adjustment here...
+ if (offsetOfCentralDir < locatedCentralDirOffset - (4 + centralDirSize)) {
+ offsetOfFirstEntry = locatedCentralDirOffset - (4 + centralDirSize + offsetOfCentralDir);
+ if (offsetOfFirstEntry <= 0) {
+ throw new ZipException("Invalid SFX file");
+ }
+ }
+
+ baseStream.Seek(offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin);
+
+ for (int i = 0; i < entriesForThisDisk; i++) {
+ if (ReadLeInt() != ZipConstants.CENSIG) {
+ throw new ZipException("Wrong Central Directory signature");
+ }
+
+ int versionMadeBy = ReadLeShort();
+ int versionToExtract = ReadLeShort();
+ int bitFlags = ReadLeShort();
+ int method = ReadLeShort();
+ int dostime = ReadLeInt();
+ int crc = ReadLeInt();
+ int csize = ReadLeInt();
+ int size = ReadLeInt();
+ int nameLen = ReadLeShort();
+ int extraLen = ReadLeShort();
+ int commentLen = ReadLeShort();
+
+ int diskStartNo = ReadLeShort(); // Not currently used
+ int internalAttributes = ReadLeShort(); // Not currently used
+
+ int externalAttributes = ReadLeInt();
+ int offset = ReadLeInt();
+
+ byte[] buffer = new byte[Math.Max(nameLen, commentLen)];
+
+ baseStream.Read(buffer, 0, nameLen);
+ string name = ZipConstants.ConvertToString(buffer, nameLen);
+
+ ZipEntry entry = new ZipEntry(name, versionToExtract, versionMadeBy);
+ entry.CompressionMethod = (CompressionMethod)method;
+ entry.Crc = crc & 0xffffffffL;
+ entry.Size = size & 0xffffffffL;
+ entry.CompressedSize = csize & 0xffffffffL;
+ entry.Flags = bitFlags;
+ entry.DosTime = (uint)dostime;
+
+ if (extraLen > 0) {
+ byte[] extra = new byte[extraLen];
+ baseStream.Read(extra, 0, extraLen);
+ entry.ExtraData = extra;
+ }
+
+ if (commentLen > 0) {
+ baseStream.Read(buffer, 0, commentLen);
+ entry.Comment = ZipConstants.ConvertToString(buffer, commentLen);
+ }
+
+ entry.ZipFileIndex = i;
+ entry.Offset = offset;
+ entry.ExternalFileAttributes = externalAttributes;
+
+ entries[i] = entry;
+ }
+ }
+
+ /// <summary>
+ /// Closes the ZipFile. If the stream is <see cref="IsStreamOwner">owned</see> then this also closes the underlying input stream.
+ /// Once closed, no further instance methods should be called.
+ /// </summary>
+ /// <exception cref="System.IO.IOException">
+ /// An i/o error occurs.
+ /// </exception>
+ public void Close()
+ {
+ entries = null;
+ if ( isStreamOwner ) {
+ lock(baseStream) {
+ baseStream.Close();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Returns an enumerator for the Zip entries in this Zip file.
+ /// </summary>
+ /// <exception cref="InvalidOperationException">
+ /// The Zip file has been closed.
+ /// </exception>
+ public IEnumerator GetEnumerator()
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipFile has closed");
+ }
+
+ return new ZipEntryEnumeration(entries);
+ }
+
+ /// <summary>
+ /// Return the index of the entry with a matching name
+ /// </summary>
+ /// <param name="name">Entry name to find</param>
+ /// <param name="ignoreCase">If true the comparison is case insensitive</param>
+ /// <returns>The index position of the matching entry or -1 if not found</returns>
+ /// <exception cref="InvalidOperationException">
+ /// The Zip file has been closed.
+ /// </exception>
+ public int FindEntry(string name, bool ignoreCase)
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipFile has been closed");
+ }
+
+ for (int i = 0; i < entries.Length; i++) {
+ if (string.Compare(name, entries[i].Name, ignoreCase) == 0) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /// <summary>
+ /// Indexer property for ZipEntries
+ /// </summary>
+ [System.Runtime.CompilerServices.IndexerNameAttribute("EntryByIndex")]
+ public ZipEntry this[int index] {
+ get {
+ return (ZipEntry) entries[index].Clone();
+ }
+ }
+
+ /// <summary>
+ /// Searches for a zip entry in this archive with the given name.
+ /// String comparisons are case insensitive
+ /// </summary>
+ /// <param name="name">
+ /// The name to find. May contain directory components separated by slashes ('/').
+ /// </param>
+ /// <returns>
+ /// The zip entry, or null if no entry with that name exists.
+ /// </returns>
+ /// <exception cref="InvalidOperationException">
+ /// The Zip file has been closed.
+ /// </exception>
+ public ZipEntry GetEntry(string name)
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipFile has been closed");
+ }
+
+ int index = FindEntry(name, true);
+ return index >= 0 ? (ZipEntry) entries[index].Clone() : null;
+ }
+ /// <summary>
+ /// Test an archive for integrity/validity
+ /// </summary>
+ /// <param name="testData">Perform low level data Crc check</param>
+ /// <returns>true iff the test passes, false otherwise</returns>
+ public bool TestArchive(bool testData)
+ {
+ bool result = true;
+ try {
+ for (int i = 0; i < Size; ++i) {
+ long offset = TestLocalHeader(this[i], true, true);
+ if (testData) {
+ Stream entryStream = this.GetInputStream(this[i]);
+ // TODO: events for updating info, recording errors etc
+ Crc32 crc = new Crc32();
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0) {
+ crc.Update(buffer, 0, bytesRead);
+ }
+
+ if (this[i].Crc != crc.Value) {
+ result = false;
+ // TODO: Event here....
+ break; // Do all entries giving more info at some point?
+ }
+ }
+ }
+ }
+ catch {
+ result = false;
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Test the local header against that provided from the central directory
+ /// </summary>
+ /// <param name="entry">
+ /// The entry to test against
+ /// </param>
+ /// <param name="fullTest">
+ /// If true be extremely picky about the testing, otherwise be relaxed
+ /// </param>
+ /// <param name="extractTest">
+ /// Apply extra testing to see if the entry can be extracted by the library
+ /// </param>
+ /// <returns>The offset of the entries data in the file</returns>
+ long TestLocalHeader(ZipEntry entry, bool fullTest, bool extractTest)
+ {
+ lock(baseStream)
+ {
+ baseStream.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
+ if (ReadLeInt() != ZipConstants.LOCSIG) {
+ throw new ZipException("Wrong local header signature");
+ }
+
+ short shortValue = (short)ReadLeShort(); // version required to extract
+ if (extractTest == true && shortValue > ZipConstants.VERSION_MADE_BY) {
+ throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", shortValue));
+ }
+
+ short localFlags = (short)ReadLeShort(); // general purpose bit flags.
+ if (extractTest == true) {
+ if ((localFlags & (int)(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0) {
+ throw new ZipException("The library doesnt support the zip version required to extract this entry");
+ }
+ }
+
+ if (localFlags != entry.Flags) {
+ throw new ZipException("Central header/local header flags mismatch");
+ }
+
+ if (entry.CompressionMethod != (CompressionMethod)ReadLeShort()) {
+ throw new ZipException("Central header/local header compression method mismatch");
+ }
+
+ shortValue = (short)ReadLeShort(); // file time
+ shortValue = (short)ReadLeShort(); // file date
+
+ int intValue = ReadLeInt(); // Crc
+
+ if (fullTest) {
+ if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0) {
+ if (intValue != (int)entry.Crc)
+ throw new ZipException("Central header/local header crc mismatch");
+ }
+ }
+
+ intValue = ReadLeInt(); // compressed Size
+ intValue = ReadLeInt(); // uncompressed size
+
+ // TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings
+ // Assuming a code page at this point is not valid? Best is to store the name length in the ZipEntry probably
+ int storedNameLength = ReadLeShort();
+ if (entry.Name.Length > storedNameLength) {
+ throw new ZipException("file name length mismatch");
+ }
+
+ int extraLen = storedNameLength + ReadLeShort();
+ return offsetOfFirstEntry + entry.Offset + ZipConstants.LOCHDR + extraLen;
+ }
+ }
+
+ /// <summary>
+ /// Checks, if the local header of the entry at index i matches the
+ /// central directory, and returns the offset to the data.
+ /// </summary>
+ /// <returns>
+ /// The start offset of the (compressed) data.
+ /// </returns>
+ /// <exception cref="System.IO.EndOfStreamException">
+ /// The stream ends prematurely
+ /// </exception>
+ /// <exception cref="ICSharpCode.SharpZipLib.Zip.ZipException">
+ /// The local header signature is invalid, the entry and central header file name lengths are different
+ /// or the local and entry compression methods dont match
+ /// </exception>
+ long CheckLocalHeader(ZipEntry entry)
+ {
+ return TestLocalHeader(entry, false, true);
+ }
+
+ // Refactor this, its done elsewhere as well
+ void ReadFully(Stream s, byte[] outBuf)
+ {
+ int off = 0;
+ int len = outBuf.Length;
+ while (len > 0) {
+ int count = s.Read(outBuf, off, len);
+ if (count <= 0) {
+ throw new ZipException("Unexpected EOF");
+ }
+ off += count;
+ len -= count;
+ }
+ }
+
+ void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry)
+ {
+ byte[] cryptbuffer = new byte[ZipConstants.CRYPTO_HEADER_SIZE];
+ ReadFully(classicCryptoStream, cryptbuffer);
+
+ if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0) {
+ if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)(entry.Crc >> 24)) {
+ throw new ZipException("Invalid password");
+ }
+ }
+ else {
+ if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)((entry.DosTime >> 8) & 0xff)) {
+ throw new ZipException("Invalid password");
+ }
+ }
+ }
+
+ Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
+ {
+ CryptoStream result = null;
+
+ if (entry.Version < ZipConstants.VERSION_STRONG_ENCRYPTION
+ || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
+ PkzipClassicManaged classicManaged = new PkzipClassicManaged();
+
+ OnKeysRequired(entry.Name);
+ if (HaveKeys == false) {
+ throw new ZipException("No password available for encrypted stream");
+ }
+
+ result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, iv), CryptoStreamMode.Read);
+ CheckClassicPassword(result, entry);
+ }
+ else {
+ throw new ZipException("Decryption method not supported");
+ }
+
+ return result;
+ }
+
+ void WriteEncryptionHeader(Stream stream, long crcValue)
+ {
+ byte[] cryptBuffer = new byte[ZipConstants.CRYPTO_HEADER_SIZE];
+ Random rnd = new Random();
+ rnd.NextBytes(cryptBuffer);
+ cryptBuffer[11] = (byte)(crcValue >> 24);
+ stream.Write(cryptBuffer, 0, cryptBuffer.Length);
+ }
+
+ Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
+ {
+ CryptoStream result = null;
+ if (entry.Version < ZipConstants.VERSION_STRONG_ENCRYPTION
+ || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
+ PkzipClassicManaged classicManaged = new PkzipClassicManaged();
+
+ OnKeysRequired(entry.Name);
+ if (HaveKeys == false) {
+ throw new ZipException("No password available for encrypted stream");
+ }
+
+ result = new CryptoStream(baseStream, classicManaged.CreateEncryptor(key, iv), CryptoStreamMode.Write);
+ if (entry.Crc < 0 || (entry.Flags & 8) != 0) {
+ WriteEncryptionHeader(result, entry.DosTime << 16);
+ }
+ else {
+ WriteEncryptionHeader(result, entry.Crc);
+ }
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Gets an output stream for the specified <see cref="ZipEntry"/>
+ /// </summary>
+ /// <param name="entry">The entry to get an outputstream for.</param>
+ /// <param name="fileName"></param>
+ /// <returns>The output stream obtained for the entry.</returns>
+ Stream GetOutputStream(ZipEntry entry, string fileName)
+ {
+ baseStream.Seek(0, SeekOrigin.End);
+ Stream result = File.OpenWrite(fileName);
+
+ if (entry.IsCrypted == true)
+ {
+ result = CreateAndInitEncryptionStream(result, entry);
+ }
+
+ switch (entry.CompressionMethod)
+ {
+ case CompressionMethod.Stored:
+ break;
+
+ case CompressionMethod.Deflated:
+ result = new DeflaterOutputStream(result);
+ break;
+
+ default:
+ throw new ZipException("Unknown compression method " + entry.CompressionMethod);
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Creates an input stream reading the given zip entry as
+ /// uncompressed data. Normally zip entry should be an entry
+ /// returned by GetEntry().
+ /// </summary>
+ /// <returns>
+ /// the input stream.
+ /// </returns>
+ /// <exception cref="InvalidOperationException">
+ /// The ZipFile has already been closed
+ /// </exception>
+ /// <exception cref="ICSharpCode.SharpZipLib.Zip.ZipException">
+ /// The compression method for the entry is unknown
+ /// </exception>
+ /// <exception cref="IndexOutOfRangeException">
+ /// The entry is not found in the ZipFile
+ /// </exception>
+ public Stream GetInputStream(ZipEntry entry)
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipFile has closed");
+ }
+
+ int index = entry.ZipFileIndex;
+ if (index < 0 || index >= entries.Length || entries[index].Name != entry.Name) {
+ index = FindEntry(entry.Name, true);
+ if (index < 0) {
+ throw new IndexOutOfRangeException();
+ }
+ }
+ return GetInputStream(index);
+ }
+
+ /// <summary>
+ /// Creates an input stream reading a zip entry
+ /// </summary>
+ /// <param name="entryIndex">The index of the entry to obtain an input stream for.</param>
+ /// <returns>
+ /// An input stream.
+ /// </returns>
+ /// <exception cref="InvalidOperationException">
+ /// The ZipFile has already been closed
+ /// </exception>
+ /// <exception cref="ICSharpCode.SharpZipLib.Zip.ZipException">
+ /// The compression method for the entry is unknown
+ /// </exception>
+ /// <exception cref="IndexOutOfRangeException">
+ /// The entry is not found in the ZipFile
+ /// </exception>
+ public Stream GetInputStream(int entryIndex)
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipFile has closed");
+ }
+
+ long start = CheckLocalHeader(entries[entryIndex]);
+ CompressionMethod method = entries[entryIndex].CompressionMethod;
+ Stream istr = new PartialInputStream(baseStream, start, entries[entryIndex].CompressedSize);
+
+ if (entries[entryIndex].IsCrypted == true) {
+ istr = CreateAndInitDecryptionStream(istr, entries[entryIndex]);
+ if (istr == null) {
+ throw new ZipException("Unable to decrypt this entry");
+ }
+ }
+
+ switch (method) {
+ case CompressionMethod.Stored:
+ return istr;
+ case CompressionMethod.Deflated:
+ return new InflaterInputStream(istr, new Inflater(true));
+ default:
+ throw new ZipException("Unsupported compression method " + method);
+ }
+ }
+
+
+ /// <summary>
+ /// Gets the comment for the zip file.
+ /// </summary>
+ public string ZipFileComment {
+ get {
+ return comment;
+ }
+ }
+
+ /// <summary>
+ /// Gets the name of this zip file.
+ /// </summary>
+ public string Name {
+ get {
+ return name;
+ }
+ }
+
+ /// <summary>
+ /// Gets the number of entries in this zip file.
+ /// </summary>
+ /// <exception cref="InvalidOperationException">
+ /// The Zip file has been closed.
+ /// </exception>
+ public int Size {
+ get {
+ if (entries != null) {
+ return entries.Length;
+ } else {
+ throw new InvalidOperationException("ZipFile is closed");
+ }
+ }
+ }
+
+ class ZipEntryEnumeration : IEnumerator
+ {
+ ZipEntry[] array;
+ int ptr = -1;
+
+ public ZipEntryEnumeration(ZipEntry[] arr)
+ {
+ array = arr;
+ }
+
+ public object Current {
+ get {
+ return array[ptr];
+ }
+ }
+
+ public void Reset()
+ {
+ ptr = -1;
+ }
+
+ public bool MoveNext()
+ {
+ return (++ptr < array.Length);
+ }
+ }
+
+ class PartialInputStream : InflaterInputStream
+ {
+ Stream baseStream;
+ long filepos, end;
+
+ public PartialInputStream(Stream baseStream, long start, long len) : base(baseStream)
+ {
+ this.baseStream = baseStream;
+ filepos = start;
+ end = start + len;
+ }
+
+ public override int Available
+ {
+ get {
+ long amount = end - filepos;
+ if (amount > Int32.MaxValue) {
+ return Int32.MaxValue;
+ }
+
+ return (int) amount;
+ }
+ }
+
+ /// <summary>
+ /// Read a byte from this stream.
+ /// </summary>
+ /// <returns>Returns the byte read or -1 on end of stream.</returns>
+ public override int ReadByte()
+ {
+ if (filepos == end) {
+ return -1; //ok
+ }
+
+ lock(baseStream) {
+ baseStream.Seek(filepos++, SeekOrigin.Begin);
+ return baseStream.ReadByte();
+ }
+ }
+
+
+ /// <summary>
+ /// Close this partial input stream.
+ /// </summary>
+ /// <remarks>
+ /// The underlying stream is not closed. Close the parent ZipFile class to do that.
+ /// </remarks>
+ public override void Close()
+ {
+ // Do nothing at all!
+ }
+
+ public override int Read(byte[] b, int off, int len)
+ {
+ if (len > end - filepos) {
+ len = (int) (end - filepos);
+ if (len == 0) {
+ return 0;
+ }
+ }
+
+ lock(baseStream) {
+ baseStream.Seek(filepos, SeekOrigin.Begin);
+ int count = baseStream.Read(b, off, len);
+ if (count > 0) {
+ filepos += len;
+ }
+ return count;
+ }
+ }
+
+ public long SkipBytes(long amount)
+ {
+ if (amount < 0) {
+ throw new ArgumentOutOfRangeException();
+ }
+ if (amount > end - filepos) {
+ amount = end - filepos;
+ }
+ filepos += amount;
+ return amount;
+ }
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
new file mode 100644
index 00000000000..a93533ba4df
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
@@ -0,0 +1,522 @@
+// ZipInputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.Text;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+using ICSharpCode.SharpZipLib.Encryption;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+ /// <summary>
+ /// This is an InflaterInputStream that reads the files baseInputStream an zip archive
+ /// one after another. It has a special method to get the zip entry of
+ /// the next file. The zip entry contains information about the file name
+ /// size, compressed size, Crc, etc.
+ /// It includes support for Stored and Deflated entries.
+ /// <br/>
+ /// <br/>Author of the original java version : Jochen Hoenicke
+ /// </summary>
+ ///
+ /// <example> This sample shows how to read a zip file
+ /// <code lang="C#">
+ /// using System;
+ /// using System.Text;
+ /// using System.IO;
+ ///
+ /// using ICSharpCode.SharpZipLib.Zip;
+ ///
+ /// class MainClass
+ /// {
+ /// public static void Main(string[] args)
+ /// {
+ /// ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
+ ///
+ /// ZipEntry theEntry;
+ /// while ((theEntry = s.GetNextEntry()) != null) {
+ /// int size = 2048;
+ /// byte[] data = new byte[2048];
+ ///
+ /// Console.Write("Show contents (y/n) ?");
+ /// if (Console.ReadLine() == "y") {
+ /// while (true) {
+ /// size = s.Read(data, 0, data.Length);
+ /// if (size > 0) {
+ /// Console.Write(new ASCIIEncoding().GetString(data, 0, size));
+ /// } else {
+ /// break;
+ /// }
+ /// }
+ /// }
+ /// }
+ /// s.Close();
+ /// }
+ /// }
+ /// </code>
+ /// </example>
+ public class ZipInputStream : InflaterInputStream
+ {
+ // Delegate for reading bytes from a stream.
+ delegate int ReaderDelegate(byte[] b, int offset, int length);
+
+ /// <summary>
+ /// The current reader this instance.
+ /// </summary>
+ ReaderDelegate internalReader;
+
+ Crc32 crc = new Crc32();
+ ZipEntry entry = null;
+
+ long size;
+ int method;
+ int flags;
+ string password = null;
+
+ /// <summary>
+ /// Creates a new Zip input stream, for reading a zip archive.
+ /// </summary>
+ public ZipInputStream(Stream baseInputStream) : base(baseInputStream, new Inflater(true))
+ {
+ internalReader = new ReaderDelegate(InitialRead);
+ }
+
+
+ /// <summary>
+ /// Optional password used for encryption when non-null
+ /// </summary>
+ public string Password
+ {
+ get {
+ return password;
+ }
+ set {
+ password = value;
+ }
+ }
+
+
+ /// <summary>
+ /// Gets a value indicating if the entry can be decompressed
+ /// </summary>
+ /// <remarks>
+ /// The entry can only be decompressed if the library supports the zip features required to extract it.
+ /// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details.
+ /// </remarks>
+ public bool CanDecompressEntry {
+ get {
+ return entry != null && entry.Version <= ZipConstants.VERSION_MADE_BY;
+ }
+ }
+
+ /// <summary>
+ /// Advances to the next entry in the archive
+ /// </summary>
+ /// <returns>
+ /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
+ /// </returns>
+ /// <remarks>
+ /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
+ /// </remarks>
+ /// <exception cref="InvalidOperationException">
+ /// Input stream is closed
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// Password is not set, password is invalid, compression method is invalid,
+ /// version required to extract is not supported
+ /// </exception>
+ public ZipEntry GetNextEntry()
+ {
+ if (crc == null) {
+ throw new InvalidOperationException("Closed.");
+ }
+
+ if (entry != null) {
+ CloseEntry();
+ }
+
+ int header = inputBuffer.ReadLeInt();
+
+ if (header == ZipConstants.CENSIG ||
+ header == ZipConstants.ENDSIG ||
+ header == ZipConstants.CENDIGITALSIG ||
+ header == ZipConstants.CENSIG64) {
+ // No more individual entries exist
+ Close();
+ return null;
+ }
+
+ // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
+ // SPANNINGSIG is same as descriptor signature and is untested as yet.
+ if (header == ZipConstants.SPANTEMPSIG || header == ZipConstants.SPANNINGSIG) {
+ header = inputBuffer.ReadLeInt();
+ }
+
+ if (header != ZipConstants.LOCSIG) {
+ throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
+ }
+
+ short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();
+
+ flags = inputBuffer.ReadLeShort();
+ method = inputBuffer.ReadLeShort();
+ uint dostime = (uint)inputBuffer.ReadLeInt();
+ int crc2 = inputBuffer.ReadLeInt();
+ csize = inputBuffer.ReadLeInt();
+ size = inputBuffer.ReadLeInt();
+ int nameLen = inputBuffer.ReadLeShort();
+ int extraLen = inputBuffer.ReadLeShort();
+
+ bool isCrypted = (flags & 1) == 1;
+
+ byte[] buffer = new byte[nameLen];
+ inputBuffer.ReadRawBuffer(buffer);
+
+ string name = ZipConstants.ConvertToString(buffer);
+
+ entry = new ZipEntry(name, versionRequiredToExtract);
+ entry.Flags = flags;
+
+ if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CRYPTO_HEADER_SIZE != size))) {
+ throw new ZipException("Stored, but compressed != uncompressed");
+ }
+
+ if (method != (int)CompressionMethod.Stored && method != (int)CompressionMethod.Deflated) {
+ throw new ZipException("Unknown compression method " + method);
+ }
+
+ entry.CompressionMethod = (CompressionMethod)method;
+
+ if ((flags & 8) == 0) {
+ entry.Crc = crc2 & 0xFFFFFFFFL;
+ entry.Size = size & 0xFFFFFFFFL;
+ entry.CompressedSize = csize & 0xFFFFFFFFL;
+ } else {
+
+ // This allows for GNU, WinZip and possibly other archives, the PKZIP spec says these are zero
+ // under these circumstances.
+ if (crc2 != 0) {
+ entry.Crc = crc2 & 0xFFFFFFFFL;
+ }
+
+ if (size != 0) {
+ entry.Size = size & 0xFFFFFFFFL;
+ }
+ if (csize != 0) {
+ entry.CompressedSize = csize & 0xFFFFFFFFL;
+ }
+ }
+
+ entry.DosTime = dostime;
+
+ if (extraLen > 0) {
+ byte[] extra = new byte[extraLen];
+ inputBuffer.ReadRawBuffer(extra);
+ entry.ExtraData = extra;
+ }
+
+ internalReader = new ReaderDelegate(InitialRead);
+ return entry;
+ }
+
+ // Read data descriptor at the end of compressed data.
+ void ReadDataDescriptor()
+ {
+ if (inputBuffer.ReadLeInt() != ZipConstants.EXTSIG) {
+ throw new ZipException("Data descriptor signature not found");
+ }
+
+ entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL;
+ csize = inputBuffer.ReadLeInt();
+ size = inputBuffer.ReadLeInt();
+
+ entry.Size = size & 0xFFFFFFFFL;
+ entry.CompressedSize = csize & 0xFFFFFFFFL;
+ }
+
+ /// <summary>
+ /// Closes the current zip entry and moves to the next one.
+ /// </summary>
+ /// <exception cref="InvalidOperationException">
+ /// The stream is closed
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// The Zip stream ends early
+ /// </exception>
+ public void CloseEntry()
+ {
+ if (crc == null) {
+ throw new InvalidOperationException("Closed.");
+ }
+
+ if (entry == null) {
+ return;
+ }
+
+ if (method == (int)CompressionMethod.Deflated) {
+ if ((flags & 8) != 0) {
+ // We don't know how much we must skip, read until end.
+ byte[] tmp = new byte[2048];
+ while (Read(tmp, 0, tmp.Length) > 0)
+ ;
+ // read will close this entry
+ return;
+ }
+ csize -= inf.TotalIn;
+ inputBuffer.Available -= inf.RemainingInput;
+ }
+
+ if (inputBuffer.Available > csize && csize >= 0) {
+ inputBuffer.Available = (int)((long)inputBuffer.Available - csize);
+ } else {
+ csize -= inputBuffer.Available;
+ inputBuffer.Available = 0;
+ while (csize != 0) {
+ int skipped = (int)base.Skip(csize & 0xFFFFFFFFL);
+
+ if (skipped <= 0) {
+ throw new ZipException("Zip archive ends early.");
+ }
+
+ csize -= skipped;
+ }
+ }
+
+ size = 0;
+ crc.Reset();
+ if (method == (int)CompressionMethod.Deflated) {
+ inf.Reset();
+ }
+ entry = null;
+ }
+
+ /// <summary>
+ /// Returns 1 if there is an entry available
+ /// Otherwise returns 0.
+ /// </summary>
+ public override int Available {
+ get {
+ return entry != null ? 1 : 0;
+ }
+ }
+
+ /// <summary>
+ /// Reads a byte from the current zip entry.
+ /// </summary>
+ /// <returns>
+ /// The byte or -1 if end of stream is reached.
+ /// </returns>
+ /// <exception name="System.IO.IOException">
+ /// An i/o error occured.
+ /// </exception>
+ /// <exception name="ICSharpCode.SharpZipLib.ZipException">
+ /// The deflated stream is corrupted.
+ /// </exception>
+ public override int ReadByte()
+ {
+ byte[] b = new byte[1];
+ if (Read(b, 0, 1) <= 0) {
+ return -1;
+ }
+ return b[0] & 0xff;
+ }
+
+ // Perform the initial read on an entry which may include
+ // reading encryption headers and setting up inflation.
+ int InitialRead(byte[] destination, int offset, int count)
+ {
+ if (entry.Version > ZipConstants.VERSION_MADE_BY) {
+ throw new ZipException("Libray cannot extract this entry version required (" + entry.Version.ToString() + ")");
+ }
+
+ // test for encryption
+ if (entry.IsCrypted) {
+
+ if (password == null) {
+ throw new ZipException("No password set.");
+ }
+
+ // Generate and set crypto transform...
+ PkzipClassicManaged managed = new PkzipClassicManaged();
+ byte[] key = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(password));
+
+ inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);
+
+ byte[] cryptbuffer = new byte[ZipConstants.CRYPTO_HEADER_SIZE];
+ inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CRYPTO_HEADER_SIZE);
+
+ if ((flags & 8) == 0) {
+ if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)(entry.Crc >> 24)) {
+ throw new ZipException("Invalid password");
+ }
+ }
+ else {
+ if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)((entry.DosTime >> 8) & 0xff)) {
+ throw new ZipException("Invalid password");
+ }
+ }
+
+ if (csize >= ZipConstants.CRYPTO_HEADER_SIZE) {
+ csize -= ZipConstants.CRYPTO_HEADER_SIZE;
+ }
+ }
+ else {
+ inputBuffer.CryptoTransform = null;
+ }
+
+ if (method == (int)CompressionMethod.Deflated && inputBuffer.Available > 0) {
+ inputBuffer.SetInflaterInput(inf);
+ }
+
+ internalReader = new ReaderDelegate(BodyRead);
+ return BodyRead(destination, offset, count);
+ }
+
+
+ /// <summary>
+ /// Read a block of bytes from the stream.
+ /// </summary>
+ /// <param name="destination">The destination for the bytes.</param>
+ /// <param name="index">The index to start storing data.</param>
+ /// <param name="count">The number of bytes to attempt to read.</param>
+ /// <returns>Returns the number of bytes read.</returns>
+ /// <remarks>Zero bytes read means end of stream.</remarks>
+ public override int Read(byte[] destination, int index, int count)
+ {
+ return internalReader(destination, index, count);
+ }
+
+ /// <summary>
+ /// Reads a block of bytes from the current zip entry.
+ /// </summary>
+ /// <returns>
+ /// The number of bytes read (this may be less than the length requested, even before the end of stream), or 0 on end of stream.
+ /// </returns>
+ /// <exception name="IOException">
+ /// An i/o error occured.
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// The deflated stream is corrupted.
+ /// </exception>
+ /// <exception cref="InvalidOperationException">
+ /// The stream is not open.
+ /// </exception>
+ public int BodyRead(byte[] b, int off, int len)
+ {
+ if (crc == null) {
+ throw new InvalidOperationException("Closed.");
+ }
+
+ if (entry == null || len <= 0 ) {
+ return 0;
+ }
+
+ bool finished = false;
+
+ switch (method) {
+ case (int)CompressionMethod.Deflated:
+ len = base.Read(b, off, len);
+ if (len <= 0) {
+ if (!inf.IsFinished) {
+ throw new ZipException("Inflater not finished!?");
+ }
+ inputBuffer.Available = inf.RemainingInput;
+
+ if ((flags & 8) == 0 && (inf.TotalIn != csize || inf.TotalOut != size)) {
+ throw new ZipException("size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
+ }
+ inf.Reset();
+ finished = true;
+ }
+ break;
+
+ case (int)CompressionMethod.Stored:
+ if (len > csize && csize >= 0) {
+ len = (int)csize;
+ }
+ len = inputBuffer.ReadClearTextBuffer(b, off, len);
+ if (len > 0) {
+ csize -= len;
+ size -= len;
+ }
+
+ if (csize == 0) {
+ finished = true;
+ } else {
+ if (len < 0) {
+ throw new ZipException("EOF in stored block");
+ }
+ }
+ break;
+ }
+
+ if (len > 0) {
+ crc.Update(b, off, len);
+ }
+
+ if (finished) {
+ StopDecrypting();
+
+ if ((flags & 8) != 0) {
+ ReadDataDescriptor();
+ }
+
+ if ((crc.Value & 0xFFFFFFFFL) != entry.Crc && entry.Crc != -1) {
+ throw new ZipException("CRC mismatch");
+ }
+ crc.Reset();
+ entry = null;
+ }
+ return len;
+ }
+
+ /// <summary>
+ /// Closes the zip input stream
+ /// </summary>
+ public override void Close()
+ {
+ base.Close();
+ crc = null;
+ entry = null;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs
new file mode 100644
index 00000000000..b2e7789e6d4
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs
@@ -0,0 +1,147 @@
+// ZipNameTransform.cs
+//
+// Copyright 2005 John Reilly
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+
+using System;
+using System.IO;
+
+using ICSharpCode.SharpZipLib.Core;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+ /// <summary>
+ /// ZipNameTransform transforms name as per the Zip file convention.
+ /// </summary>
+ public class ZipNameTransform : INameTransform
+ {
+ /// <summary>
+ /// Initialize a new instance of <see cref="ZipNameTransform"></see>
+ /// </summary>
+ /// <remarks>Relative paths default to true with this constructor.</remarks>
+ public ZipNameTransform()
+ {
+ relativePath = true;
+ }
+
+ /// <summary>
+ /// Initialize a new instance of <see cref="ZipNameTransform"></see>
+ /// </summary>
+ /// <param name="useRelativePaths">If true relative paths are created,
+ /// if false absolute paths are created. </param>
+ public ZipNameTransform(bool useRelativePaths)
+ {
+ relativePath = useRelativePaths;
+ }
+
+ /// <summary>
+ /// Initialize a new instance of <see cref="ZipNameTransform"></see>
+ /// </summary>
+ /// <param name="useRelativePaths">If true relative paths are created,
+ /// if false absolute paths are created. </param>
+ /// <param name="trimPrefix">The string to trim from front of paths if found.</param>
+ public ZipNameTransform(bool useRelativePaths, string trimPrefix)
+ {
+ this.trimPrefix = trimPrefix;
+ relativePath = useRelativePaths;
+ }
+
+ /// <summary>
+ /// Transform a directory name according to the Zip file naming conventions.
+ /// </summary>
+ /// <param name="name">The directory name to transform.</param>
+ /// <returns>The transformed name.</returns>
+ public string TransformDirectory(string name)
+ {
+ name = TransformFile(name);
+ if (name.Length > 0) {
+ if ( !name.EndsWith("/") ) {
+ name += "/";
+ }
+ }
+ else {
+ name = "/";
+ }
+ return name;
+ }
+
+ /// <summary>
+ /// Transform a file name according to the Zip file naming conventions.
+ /// </summary>
+ /// <param name="name">The file name to transform.</param>
+ /// <returns>The transformed name.</returns>
+ public string TransformFile(string name)
+ {
+ if (name != null) {
+ if ( trimPrefix != null && name.IndexOf(trimPrefix) == 0 ) {
+ name = name.Substring(trimPrefix.Length);
+ }
+
+ if (Path.IsPathRooted(name) == true) {
+ // NOTE:
+ // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt
+ name = name.Substring(Path.GetPathRoot(name).Length);
+ }
+
+ if (relativePath == true) {
+ if (name.Length > 0 && (name[0] == Path.AltDirectorySeparatorChar || name[0] == Path.DirectorySeparatorChar)) {
+ name = name.Remove(0, 1);
+ }
+ } else {
+ if (name.Length > 0 && name[0] != Path.AltDirectorySeparatorChar && name[0] != Path.DirectorySeparatorChar) {
+ name = name.Insert(0, "/");
+ }
+ }
+ name = name.Replace(@"\", "/");
+ }
+ else {
+ name = "";
+ }
+ return name;
+ }
+
+ string trimPrefix;
+
+ /// <summary>
+ /// Get/set the path prefix to be trimmed from paths if present.
+ /// </summary>
+ public string TrimPrefix
+ {
+ get { return trimPrefix; }
+ set { trimPrefix = value; }
+ }
+
+ bool relativePath;
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
new file mode 100644
index 00000000000..b98c86dca37
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
@@ -0,0 +1,592 @@
+// ZipOutputStream.cs
+//
+// Copyright (C) 2001 Mike Krueger
+// Copyright (C) 2004 John Reilly
+//
+// This file was translated from java, it was part of the GNU Classpath
+// Copyright (C) 2001 Free Software Foundation, Inc.
+//
+// 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
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Linking this library statically or dynamically with other modules is
+// making a combined work based on this library. Thus, the terms and
+// conditions of the GNU General Public License cover the whole
+// combination.
+//
+// As a special exception, the copyright holders of this library give you
+// permission to link this library with independent modules to produce an
+// executable, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting executable under
+// terms of your choice, provided that you also meet, for each linked
+// independent module, the terms and conditions of the license of that
+// module. An independent module is a module which is not derived from
+// or based on this library. If you modify this library, you may extend
+// this exception to your version of the library, but you are not
+// obligated to do so. If you do not wish to do so, delete this
+// exception statement from your version.
+
+using System;
+using System.IO;
+using System.Collections;
+using System.Text;
+
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
+namespace ICSharpCode.SharpZipLib.Zip
+{
+ /// <summary>
+ /// This is a DeflaterOutputStream that writes the files into a zip
+ /// archive one after another. It has a special method to start a new
+ /// zip entry. The zip entries contains information about the file name
+ /// size, compressed size, CRC, etc.
+ ///
+ /// It includes support for Stored and Deflated entries.
+ /// This class is not thread safe.
+ /// <br/>
+ /// <br/>Author of the original java version : Jochen Hoenicke
+ /// </summary>
+ /// <example> This sample shows how to create a zip file
+ /// <code>
+ /// using System;
+ /// using System.IO;
+ ///
+ /// using ICSharpCode.SharpZipLib.Zip;
+ ///
+ /// class MainClass
+ /// {
+ /// public static void Main(string[] args)
+ /// {
+ /// string[] filenames = Directory.GetFiles(args[0]);
+ ///
+ /// ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
+ ///
+ /// s.SetLevel(5); // 0 - store only to 9 - means best compression
+ ///
+ /// foreach (string file in filenames) {
+ /// FileStream fs = File.OpenRead(file);
+ ///
+ /// byte[] buffer = new byte[fs.Length];
+ /// fs.Read(buffer, 0, buffer.Length);
+ ///
+ /// ZipEntry entry = new ZipEntry(file);
+ ///
+ /// s.PutNextEntry(entry);
+ ///
+ /// s.Write(buffer, 0, buffer.Length);
+ ///
+ /// }
+ ///
+ /// s.Finish();
+ /// s.Close();
+ /// }
+ /// }
+ /// </code>
+ /// </example>
+ public class ZipOutputStream : DeflaterOutputStream
+ {
+ private ArrayList entries = new ArrayList();
+ private Crc32 crc = new Crc32();
+ private ZipEntry curEntry = null;
+
+ int defaultCompressionLevel = Deflater.DEFAULT_COMPRESSION;
+ CompressionMethod curMethod = CompressionMethod.Deflated;
+
+
+ private long size;
+ private long offset = 0;
+
+ private byte[] zipComment = new byte[0];
+
+ /// <summary>
+ /// Gets boolean indicating central header has been added for this archive...
+ /// No further entries can be added once this has been done.
+ /// </summary>
+ public bool IsFinished {
+ get {
+ return entries == null;
+ }
+ }
+
+ /// <summary>
+ /// Creates a new Zip output stream, writing a zip archive.
+ /// </summary>
+ /// <param name="baseOutputStream">
+ /// The output stream to which the archive contents are written.
+ /// </param>
+ public ZipOutputStream(Stream baseOutputStream) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true))
+ {
+ }
+
+ /// <summary>
+ /// Set the zip file comment.
+ /// </summary>
+ /// <param name="comment">
+ /// The comment string
+ /// </param>
+ /// <exception name ="ArgumentOutOfRangeException">
+ /// Encoding of comment is longer than 0xffff bytes.
+ /// </exception>
+ public void SetComment(string comment)
+ {
+ byte[] commentBytes = ZipConstants.ConvertToArray(comment);
+ if (commentBytes.Length > 0xffff) {
+ throw new ArgumentOutOfRangeException("comment");
+ }
+ zipComment = commentBytes;
+ }
+
+ /// <summary>
+ /// Sets default compression level. The new level will be activated
+ /// immediately.
+ /// </summary>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// Level specified is not supported.
+ /// </exception>
+ /// <see cref="Deflater"/>
+ public void SetLevel(int level)
+ {
+ defaultCompressionLevel = level;
+ def.SetLevel(level);
+ }
+
+ /// <summary>
+ /// Get the current deflate compression level
+ /// </summary>
+ /// <returns>The current compression level</returns>
+ public int GetLevel()
+ {
+ return def.GetLevel();
+ }
+
+ /// <summary>
+ /// Write an unsigned short in little endian byte order.
+ /// </summary>
+ private void WriteLeShort(int value)
+ {
+ baseOutputStream.WriteByte((byte)(value & 0xff));
+ baseOutputStream.WriteByte((byte)((value >> 8) & 0xff));
+ }
+
+ /// <summary>
+ /// Write an int in little endian byte order.
+ /// </summary>
+ private void WriteLeInt(int value)
+ {
+ WriteLeShort(value);
+ WriteLeShort(value >> 16);
+ }
+
+ /// <summary>
+ /// Write an int in little endian byte order.
+ /// </summary>
+ private void WriteLeLong(long value)
+ {
+ WriteLeInt((int)value);
+ WriteLeInt((int)(value >> 32));
+ }
+
+
+ bool patchEntryHeader = false;
+
+ long headerPatchPos = -1;
+
+ /// <summary>
+ /// Starts a new Zip entry. It automatically closes the previous
+ /// entry if present.
+ /// All entry elements bar name are optional, but must be correct if present.
+ /// If the compression method is stored and the output is not patchable
+ /// the compression for that entry is automatically changed to deflate level 0
+ /// </summary>
+ /// <param name="entry">
+ /// the entry.
+ /// </param>
+ /// <exception cref="System.IO.IOException">
+ /// if an I/O error occured.
+ /// </exception>
+ /// <exception cref="System.InvalidOperationException">
+ /// if stream was finished
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// Too many entries in the Zip file<br/>
+ /// Entry name is too long<br/>
+ /// Finish has already been called<br/>
+ /// </exception>
+ public void PutNextEntry(ZipEntry entry)
+ {
+ if (entries == null) {
+ throw new InvalidOperationException("ZipOutputStream was finished");
+ }
+
+ if (curEntry != null) {
+ CloseEntry();
+ }
+
+ if (entries.Count >= 0xffff) {
+ throw new ZipException("Too many entries for Zip file");
+ }
+
+ CompressionMethod method = entry.CompressionMethod;
+ int compressionLevel = defaultCompressionLevel;
+
+ entry.Flags = 0;
+ patchEntryHeader = false;
+ bool headerInfoAvailable = true;
+
+ if (method == CompressionMethod.Stored) {
+ if (entry.CompressedSize >= 0) {
+ if (entry.Size < 0) {
+ entry.Size = entry.CompressedSize;
+ } else if (entry.Size != entry.CompressedSize) {
+ throw new ZipException("Method STORED, but compressed size != size");
+ }
+ } else {
+ if (entry.Size >= 0) {
+ entry.CompressedSize = entry.Size;
+ }
+ }
+
+ if (entry.Size < 0 || entry.Crc < 0) {
+ if (CanPatchEntries == true) {
+ headerInfoAvailable = false;
+ }
+ else {
+ // Cant patch entries so storing is not possible.
+ method = CompressionMethod.Deflated;
+ compressionLevel = 0;
+ }
+ }
+ }
+
+ if (method == CompressionMethod.Deflated) {
+ if (entry.Size == 0) {
+ // No need to compress - no data.
+ entry.CompressedSize = entry.Size;
+ entry.Crc = 0;
+ method = CompressionMethod.Stored;
+ } else if (entry.CompressedSize < 0 || entry.Size < 0 || entry.Crc < 0) {
+ headerInfoAvailable = false;
+ }
+ }
+
+ if (headerInfoAvailable == false) {
+ if (CanPatchEntries == false) {
+ entry.Flags |= 8;
+ } else {
+ patchEntryHeader = true;
+ }
+ }
+
+ if (Password != null) {
+ entry.IsCrypted = true;
+ if (entry.Crc < 0) {
+ // Need to append data descriptor as crc is used for encryption and its not known.
+ entry.Flags |= 8;
+ }
+ }
+ entry.Offset = (int)offset;
+ entry.CompressionMethod = (CompressionMethod)method;
+
+ curMethod = method;
+
+ // Write the local file header
+ WriteLeInt(ZipConstants.LOCSIG);
+
+ WriteLeShort(entry.Version);
+ WriteLeShort(entry.Flags);
+ WriteLeShort((byte)method);
+ WriteLeInt((int)entry.DosTime);
+ if (headerInfoAvailable == true) {
+ WriteLeInt((int)entry.Crc);
+ WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CRYPTO_HEADER_SIZE : (int)entry.CompressedSize);
+ WriteLeInt((int)entry.Size);
+ } else {
+ if (patchEntryHeader == true) {
+ headerPatchPos = baseOutputStream.Position;
+ }
+ WriteLeInt(0); // Crc
+ WriteLeInt(0); // Compressed size
+ WriteLeInt(0); // Uncompressed size
+ }
+
+ byte[] name = ZipConstants.ConvertToArray(entry.Name);
+
+ if (name.Length > 0xFFFF) {
+ throw new ZipException("Entry name too long.");
+ }
+
+ byte[] extra = entry.ExtraData;
+ if (extra == null) {
+ extra = new byte[0];
+ }
+
+ if (extra.Length > 0xFFFF) {
+ throw new ZipException("Extra data too long.");
+ }
+
+ WriteLeShort(name.Length);
+ WriteLeShort(extra.Length);
+ baseOutputStream.Write(name, 0, name.Length);
+ baseOutputStream.Write(extra, 0, extra.Length);
+
+ offset += ZipConstants.LOCHDR + name.Length + extra.Length;
+
+ // Activate the entry.
+ curEntry = entry;
+ crc.Reset();
+ if (method == CompressionMethod.Deflated) {
+ def.Reset();
+ def.SetLevel(compressionLevel);
+ }
+ size = 0;
+
+ if (entry.IsCrypted == true) {
+ if (entry.Crc < 0) { // so testing Zip will says its ok
+ WriteEncryptionHeader(entry.DosTime << 16);
+ } else {
+ WriteEncryptionHeader(entry.Crc);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Closes the current entry, updating header and footer information as required
+ /// </summary>
+ /// <exception cref="System.IO.IOException">
+ /// An I/O error occurs.
+ /// </exception>
+ /// <exception cref="System.InvalidOperationException">
+ /// No entry is active.
+ /// </exception>
+ public void CloseEntry()
+ {
+ if (curEntry == null) {
+ throw new InvalidOperationException("No open entry");
+ }
+
+ // First finish the deflater, if appropriate
+ if (curMethod == CompressionMethod.Deflated) {
+ base.Finish();
+ }
+
+ long csize = curMethod == CompressionMethod.Deflated ? def.TotalOut : size;
+
+ if (curEntry.Size < 0) {
+ curEntry.Size = size;
+ } else if (curEntry.Size != size) {
+ throw new ZipException("size was " + size + ", but I expected " + curEntry.Size);
+ }
+
+ if (curEntry.CompressedSize < 0) {
+ curEntry.CompressedSize = csize;
+ } else if (curEntry.CompressedSize != csize) {
+ throw new ZipException("compressed size was " + csize + ", but I expected " + curEntry.CompressedSize);
+ }
+
+ if (curEntry.Crc < 0) {
+ curEntry.Crc = crc.Value;
+ } else if (curEntry.Crc != crc.Value) {
+ throw new ZipException("crc was " + crc.Value + ", but I expected " + curEntry.Crc);
+ }
+
+ offset += csize;
+
+ if (offset > 0xffffffff) {
+ throw new ZipException("Maximum Zip file size exceeded");
+ }
+
+ if (curEntry.IsCrypted == true) {
+ curEntry.CompressedSize += ZipConstants.CRYPTO_HEADER_SIZE;
+ }
+
+ // Patch the header if possible
+ if (patchEntryHeader == true) {
+ long curPos = baseOutputStream.Position;
+ baseOutputStream.Seek(headerPatchPos, SeekOrigin.Begin);
+ WriteLeInt((int)curEntry.Crc);
+ WriteLeInt((int)curEntry.CompressedSize);
+ WriteLeInt((int)curEntry.Size);
+ baseOutputStream.Seek(curPos, SeekOrigin.Begin);
+ patchEntryHeader = false;
+ }
+
+ // Add data descriptor if flagged as required
+ if ((curEntry.Flags & 8) != 0) {
+ WriteLeInt(ZipConstants.EXTSIG);
+ WriteLeInt((int)curEntry.Crc);
+ WriteLeInt((int)curEntry.CompressedSize);
+ WriteLeInt((int)curEntry.Size);
+ offset += ZipConstants.EXTHDR;
+ }
+
+ entries.Add(curEntry);
+ curEntry = null;
+ }
+
+ void WriteEncryptionHeader(long crcValue)
+ {
+ offset += ZipConstants.CRYPTO_HEADER_SIZE;
+
+ InitializePassword(Password);
+
+ byte[] cryptBuffer = new byte[ZipConstants.CRYPTO_HEADER_SIZE];
+ Random rnd = new Random();
+ rnd.NextBytes(cryptBuffer);
+ cryptBuffer[11] = (byte)(crcValue >> 24);
+
+ EncryptBlock(cryptBuffer, 0, cryptBuffer.Length);
+ baseOutputStream.Write(cryptBuffer, 0, cryptBuffer.Length);
+ }
+
+ /// <summary>
+ /// Writes the given buffer to the current entry.
+ /// </summary>
+ /// <exception cref="ZipException">
+ /// Archive size is invalid
+ /// </exception>
+ /// <exception cref="System.InvalidOperationException">
+ /// No entry is active.
+ /// </exception>
+ public override void Write(byte[] b, int off, int len)
+ {
+ if (curEntry == null) {
+ throw new InvalidOperationException("No open entry.");
+ }
+
+ if (len <= 0)
+ return;
+
+ crc.Update(b, off, len);
+ size += len;
+
+ if (size > 0xffffffff || size < 0) {
+ throw new ZipException("Maximum entry size exceeded");
+ }
+
+
+ switch (curMethod) {
+ case CompressionMethod.Deflated:
+ base.Write(b, off, len);
+ break;
+
+ case CompressionMethod.Stored:
+ if (Password != null) {
+ byte[] buf = new byte[len];
+ Array.Copy(b, off, buf, 0, len);
+ EncryptBlock(buf, 0, len);
+ baseOutputStream.Write(buf, off, len);
+ } else {
+ baseOutputStream.Write(b, off, len);
+ }
+ break;
+ }
+ }
+
+ /// <summary>
+ /// Finishes the stream. This will write the central directory at the
+ /// end of the zip file and flush the stream.
+ /// </summary>
+ /// <remarks>
+ /// This is automatically called when the stream is closed.
+ /// </remarks>
+ /// <exception cref="System.IO.IOException">
+ /// An I/O error occurs.
+ /// </exception>
+ /// <exception cref="ZipException">
+ /// Comment exceeds the maximum length<br/>
+ /// Entry name exceeds the maximum length
+ /// </exception>
+ public override void Finish()
+ {
+ if (entries == null) {
+ return;
+ }
+
+ if (curEntry != null) {
+ CloseEntry();
+ }
+
+ int numEntries = 0;
+ int sizeEntries = 0;
+
+ foreach (ZipEntry entry in entries) {
+ CompressionMethod method = entry.CompressionMethod;
+ WriteLeInt(ZipConstants.CENSIG);
+ WriteLeShort(ZipConstants.VERSION_MADE_BY);
+ WriteLeShort(entry.Version);
+ WriteLeShort(entry.Flags);
+ WriteLeShort((short)method);
+ WriteLeInt((int)entry.DosTime);
+ WriteLeInt((int)entry.Crc);
+ WriteLeInt((int)entry.CompressedSize);
+ WriteLeInt((int)entry.Size);
+
+ byte[] name = ZipConstants.ConvertToArray(entry.Name);
+
+ if (name.Length > 0xffff) {
+ throw new ZipException("Name too long.");
+ }
+
+ byte[] extra = entry.ExtraData;
+ if (extra == null) {
+ extra = new byte[0];
+ }
+
+ byte[] entryComment = entry.Comment != null ? ZipConstants.ConvertToArray(entry.Comment) : new byte[0];
+ if (entryComment.Length > 0xffff) {
+ throw new ZipException("Comment too long.");
+ }
+
+ WriteLeShort(name.Length);
+ WriteLeShort(extra.Length);
+ WriteLeShort(entryComment.Length);
+ WriteLeShort(0); // disk number
+ WriteLeShort(0); // internal file attr
+ // external file attribute
+
+ if (entry.ExternalFileAttributes != -1) {
+ WriteLeInt(entry.ExternalFileAttributes);
+ } else {
+ if (entry.IsDirectory) { // mark entry as directory (from nikolam.AT.perfectinfo.com)
+ WriteLeInt(16);
+ } else {
+ WriteLeInt(0);
+ }
+ }
+
+ WriteLeInt(entry.Offset);
+
+ baseOutputStream.Write(name, 0, name.Length);
+ baseOutputStream.Write(extra, 0, extra.Length);
+ baseOutputStream.Write(entryComment, 0, entryComment.Length);
+ ++numEntries;
+ sizeEntries += ZipConstants.CENHDR + name.Length + extra.Length + entryComment.Length;
+ }
+
+ WriteLeInt(ZipConstants.ENDSIG);
+ WriteLeShort(0); // number of this disk
+ WriteLeShort(0); // no of disk with start of central dir
+ WriteLeShort(numEntries); // entries in central dir for this disk
+ WriteLeShort(numEntries); // total entries in central directory
+ WriteLeInt(sizeEntries); // size of the central directory
+ WriteLeInt((int)offset); // offset of start of central dir
+ WriteLeShort(zipComment.Length);
+ baseOutputStream.Write(zipComment, 0, zipComment.Length);
+ baseOutputStream.Flush();
+ entries = null;
+ }
+ }
+}
diff --git a/mcs/class/ICSharpCode.SharpZipLib/Makefile b/mcs/class/ICSharpCode.SharpZipLib/Makefile
new file mode 100644
index 00000000000..fe5ae55972c
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/Makefile
@@ -0,0 +1,12 @@
+thisdir = class/ICSharpCode.SharpZipLib
+SUBDIRS =
+include ../../build/rules.make
+
+LIBRARY = ICSharpCode.SharpZipLib.dll
+LIBRARY_SNK = SharpZipLib.key
+
+LIB_MCS_FLAGS = /r:$(corlib) /r:System.dll /r:System.Xml.dll
+NO_TEST = yes
+EXTRA_DISTFILES = SharpZipLib.pub $(LIBRARY_SNK)
+
+include ../../build/library.make
diff --git a/mcs/class/ICSharpCode.SharpZipLib/README b/mcs/class/ICSharpCode.SharpZipLib/README
new file mode 100644
index 00000000000..e38911920ac
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/README
@@ -0,0 +1,28 @@
+I imported the 0.6 version of the library. The only changes from the origional were
+the case of some of the file names -- I changed them to match the names in cvs.
+
+Ben Maurer. May, 2004.
+
+--------------------
+
+This directory contains a local copy of the Zip library from the NZipLib project.
+
+I have added this as a temporary measure to simplify the building and development
+of the Mono Documentation browser, but ideally it should use the real
+library. Do not fix bugs here, fix it on the original, and incorporate them
+here.
+
+This code is released under this license, from:
+
+ http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
+
+Here it is:
+
+The library is released under the GPL with the following exception:
+
+Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.
+
+As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.
+
+
+Miguel. Jan, 2003
diff --git a/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.key b/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.key
new file mode 100755
index 00000000000..58cf194dfdb
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.key
Binary files differ
diff --git a/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.pub b/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.pub
new file mode 100644
index 00000000000..c74794f1332
--- /dev/null
+++ b/mcs/class/ICSharpCode.SharpZipLib/SharpZipLib.pub
Binary files differ