[mkbundle] Remove Mono.Posix, ICSharpCode.SharpZipLib deps
authorJonathan Pryor <jonpryor@vt.edu>
Wed, 27 Mar 2013 22:12:07 +0000 (18:12 -0400)
committerJonathan Pryor <jonpryor@vt.edu>
Wed, 27 Mar 2013 22:12:07 +0000 (18:12 -0400)
I want to make execution of mkbundle possible under .NET, and as such
I want to remove unnecessary dependencies.

Mono.Posix is only used for UnixMarshal heap methods; there's NO need
to use these when Marshal can be used.

That leaves DeflaterOutputStream, and it's possible to configure zlib
to accept deflate streams, allowing us to use GZipStream:

http://stackoverflow.com/a/1838702/83444

Update template_z.c accordingly so that the compressed data works.

Two side results of this:

 1. Increased compression; my (really stupid) test app went from a
    compression ration of 31.39% to 32%

 2. This MAY make things WORSE when running on .NET < 4.5. This is
    fixed in .NET 4.5:

http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx

Starting with the .NET Framework 4.5, the DeflateStream class
uses the zlib library. As a result, it provides a better
compression algorithm and, in most cases, a smaller compressed
file than it provides in earlier versions of the .NET
Framework.

mcs/tools/mkbundle/Makefile
mcs/tools/mkbundle/mkbundle.cs
mcs/tools/mkbundle/template_z.c

index ac97a4ac9facb92580124e74d0a9eef8d05a3fd1..c4387a2fad308c0f70a48ea82d70456cfecc5e53 100644 (file)
@@ -8,8 +8,7 @@ OTHER_RES = template.c template_z.c template_main.c
 
 RESOURCE_FILES = $(OTHER_RES)
 
-LOCAL_MCS_FLAGS=-r:Mono.Posix.dll -r:ICSharpCode.SharpZipLib.dll \
-               $(OTHER_RES:%=-resource:%)
+LOCAL_MCS_FLAGS= $(OTHER_RES:%=-resource:%)
 
 EXTRA_DISTFILES = $(RESOURCE_FILES)
 
index 945522b64d78ed26890ccce3a8e444893dc0f33b..77cc7123e2162dc426940c0319eddf436131a6ff 100644 (file)
@@ -14,10 +14,10 @@ using System.Xml;
 using System.Collections.Generic;
 using System.Reflection;
 using System.IO;
+using System.IO.Compression;
 using System.Runtime.InteropServices;
 using System.Text;
-using Mono.Unix;
-using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
+
 
 #if NET_4_5
 using System.Threading.Tasks;
@@ -291,12 +291,12 @@ class MakeBundle {
                                int n;
                                if (compress) {
                                        MemoryStream ms = new MemoryStream ();
-                                       DeflaterOutputStream deflate = new DeflaterOutputStream (ms);
+                                       GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
                                        while ((n = stream.Read (buffer, 0, buffer.Length)) != 0){
                                                deflate.Write (buffer, 0, n);
                                        }
                                        stream.Close ();
-                                       deflate.Finish ();
+                                       deflate.Close ();
                                        byte [] bytes = ms.GetBuffer ();
                                        stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
                                }
@@ -604,10 +604,10 @@ class MakeBundle {
                        return;
                }
 
-               IntPtr buf = UnixMarshal.AllocHeap(8192);
+               IntPtr buf = Marshal.AllocHGlobal (8192);
                if (uname (buf) != 0){
                        Console.WriteLine ("Warning: Unable to detect OS");
-                       UnixMarshal.FreeHeap(buf);
+                       Marshal.FreeHGlobal (buf);
                        return;
                }
                string os = Marshal.PtrToStringAnsi (buf);
@@ -615,7 +615,7 @@ class MakeBundle {
                if (os == "Darwin")
                        style = "osx";
                
-               UnixMarshal.FreeHeap(buf);
+               Marshal.FreeHGlobal (buf);
        }
 
        static bool IsUnix {
index 7aac318bb88f91034569adbdc887bd2d593aa457..7987f82c7e893a540266476c92d57e71839c0192 100644 (file)
@@ -13,7 +13,8 @@ my_inflate (const Byte *compr, uLong compr_len, Byte *uncompr, uLong uncompr_len
        memset (&stream, 0, sizeof (z_stream));
        stream.next_in = (Byte *) compr;
        stream.avail_in = (uInt) compr_len;
-       err = inflateInit (&stream);
+       /* To decompress gzip format: http://stackoverflow.com/a/1838702/83444 */
+       err = inflateInit2 (&stream, 16+MAX_WBITS);
        if (err != Z_OK)
                return 1;