From 772fa304a20ba99d58b670ef1051944a92e3addf Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Wed, 27 Mar 2013 18:12:07 -0400 Subject: [PATCH] [mkbundle] Remove Mono.Posix, ICSharpCode.SharpZipLib deps 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 | 3 +-- mcs/tools/mkbundle/mkbundle.cs | 14 +++++++------- mcs/tools/mkbundle/template_z.c | 3 ++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mcs/tools/mkbundle/Makefile b/mcs/tools/mkbundle/Makefile index ac97a4ac9fa..c4387a2fad3 100644 --- a/mcs/tools/mkbundle/Makefile +++ b/mcs/tools/mkbundle/Makefile @@ -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) diff --git a/mcs/tools/mkbundle/mkbundle.cs b/mcs/tools/mkbundle/mkbundle.cs index 945522b64d7..77cc7123e21 100644 --- a/mcs/tools/mkbundle/mkbundle.cs +++ b/mcs/tools/mkbundle/mkbundle.cs @@ -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 { diff --git a/mcs/tools/mkbundle/template_z.c b/mcs/tools/mkbundle/template_z.c index 7aac318bb88..7987f82c7e8 100644 --- a/mcs/tools/mkbundle/template_z.c +++ b/mcs/tools/mkbundle/template_z.c @@ -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; -- 2.25.1