Loading Ionic.Zlib.dll as an assembly in MS SQL Server

Sep 21, 2009 at 10:02 PM

I've been working on using your ZLib DLL inside MS SQL Server so that I can use it in a CLR SP.  SQL Server has some special requirements for SAFE/EXTERNAL assemblies.  The only one that is a problem here is the use of non-readonly static variables.  Luckily readonly appears to be compatible with all uses of static variables in Zlib.  So here is a patch to implement that.  The unit tests pass and my limited testing indicates that everything works OK.

diff -u -r DotNetZip-1.8.4.23/ZLib/Crc32.cs DotNetZip-dev/ZLib/Crc32.cs
--- DotNetZip-1.8.4.23/ZLib/Crc32.cs	2009-08-18 21:04:20.000000000 -0500
+++ DotNetZip-dev/ZLib/Crc32.cs	2009-09-16 16:39:50.514842500 -0500
@@ -197,7 +197,7 @@
 
         // private member vars
         private Int64 _TotalBytesRead;
-        private static UInt32[] crc32Table;
+        private static readonly UInt32[] crc32Table;
         private const int BUFFER_SIZE = 8192;
         private UInt32 _RunningCrc32Result = 0xFFFFFFFF;
 
diff -u -r DotNetZip-1.8.4.23/ZLib/Deflate.cs DotNetZip-dev/ZLib/Deflate.cs
--- DotNetZip-1.8.4.23/ZLib/Deflate.cs	2009-08-19 18:25:04.000000000 -0500
+++ DotNetZip-dev/ZLib/Deflate.cs	2009-09-21 10:34:14.384813900 -0500
@@ -189,7 +189,7 @@
                 };
             }
 
-            private static Config[] Table;
+            private static readonly Config[] Table;
         }
 
 
diff -u -r DotNetZip-1.8.4.23/ZLib/GZipStream.cs DotNetZip-dev/ZLib/GZipStream.cs
--- DotNetZip-1.8.4.23/ZLib/GZipStream.cs	2009-08-12 15:35:30.000000000 -0500
+++ DotNetZip-dev/ZLib/GZipStream.cs	2009-09-16 16:39:50.366827700 -0500
@@ -774,8 +774,8 @@
         #endregion
 
 
-        internal static System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
-        internal static System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
+        internal static readonly System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+        internal static readonly System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
 
 
         private int EmitHeader()
diff -u -r DotNetZip-1.8.4.23/ZLib/Inflate.cs DotNetZip-dev/ZLib/Inflate.cs
--- DotNetZip-1.8.4.23/ZLib/Inflate.cs	2009-05-31 09:15:22.000000000 -0500
+++ DotNetZip-dev/ZLib/Inflate.cs	2009-09-16 16:39:50.453836400 -0500
@@ -1756,7 +1756,7 @@
             return ZlibConstants.Z_OK;
         }
 
-        private static byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
+        private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
 
         internal int Sync()
         {
diff -u -r DotNetZip-1.8.4.23/ZLib/Zlib.cs DotNetZip-dev/ZLib/Zlib.cs
--- DotNetZip-1.8.4.23/ZLib/Zlib.cs	2009-08-14 09:51:42.000000000 -0500
+++ DotNetZip-dev/ZLib/Zlib.cs	2009-09-21 10:33:11.138489900 -0500
@@ -374,13 +374,13 @@
         internal static readonly short[] static_dtree = new short[] { 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5, 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5, 1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5, 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
 
         //UPGRADE_NOTE: The initialization of  'static_l_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_l_desc;
+        internal static readonly StaticTree static_l_desc;
 
         //UPGRADE_NOTE: The initialization of  'static_d_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_d_desc;
+        internal static readonly StaticTree static_d_desc;
 
         //UPGRADE_NOTE: The initialization of  'static_bl_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_bl_desc;
+        internal static readonly StaticTree static_bl_desc;
 
         internal short[] static_tree; // static tree or null
         internal int[] extra_bits; // extra bits for each code or null
@@ -416,9 +416,9 @@
     internal sealed class Adler
     {
         // largest prime smaller than 65536
-        private static int BASE = 65521;
+        private static readonly int BASE = 65521;
         // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
-        private static int NMAX = 5552;
+        private static readonly int NMAX = 5552;
 
         static internal long Adler32(long adler, byte[] buf, int index, int len)
         {

diff -u -r DotNetZip-1.8.4.23/ZLib/Crc32.cs DotNetZip-dev/ZLib/Crc32.cs
--- DotNetZip-1.8.4.23/ZLib/Crc32.cs    2009-08-18 21:04:20.000000000 -0500
+++ DotNetZip-dev/ZLib/Crc32.cs    2009-09-16 16:39:50.514842500 -0500
@@ -197,7 +197,7 @@
 
         // private member vars
         private Int64 _TotalBytesRead;
-        private static UInt32[] crc32Table;
+        private static readonly UInt32[] crc32Table;
         private const int BUFFER_SIZE = 8192;
         private UInt32 _RunningCrc32Result = 0xFFFFFFFF;
 
diff -u -r DotNetZip-1.8.4.23/ZLib/Deflate.cs DotNetZip-dev/ZLib/Deflate.cs
--- DotNetZip-1.8.4.23/ZLib/Deflate.cs    2009-08-19 18:25:04.000000000 -0500
+++ DotNetZip-dev/ZLib/Deflate.cs    2009-09-21 10:34:14.384813900 -0500
@@ -189,7 +189,7 @@
                 };
             }
 
-            private static Config[] Table;
+            private static readonly Config[] Table;
         }
 
 
diff -u -r DotNetZip-1.8.4.23/ZLib/GZipStream.cs DotNetZip-dev/ZLib/GZipStream.cs
--- DotNetZip-1.8.4.23/ZLib/GZipStream.cs    2009-08-12 15:35:30.000000000 -0500
+++ DotNetZip-dev/ZLib/GZipStream.cs    2009-09-16 16:39:50.366827700 -0500
@@ -774,8 +774,8 @@
         #endregion
 
 
-        internal static System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
-        internal static System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
+        internal static readonly System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+        internal static readonly System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
 
 
         private int EmitHeader()
diff -u -r DotNetZip-1.8.4.23/ZLib/Inflate.cs DotNetZip-dev/ZLib/Inflate.cs
--- DotNetZip-1.8.4.23/ZLib/Inflate.cs    2009-05-31 09:15:22.000000000 -0500
+++ DotNetZip-dev/ZLib/Inflate.cs    2009-09-16 16:39:50.453836400 -0500
@@ -1756,7 +1756,7 @@
             return ZlibConstants.Z_OK;
         }
 
-        private static byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
+        private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
 
         internal int Sync()
         {
diff -u -r DotNetZip-1.8.4.23/ZLib/Zlib.cs DotNetZip-dev/ZLib/Zlib.cs
--- DotNetZip-1.8.4.23/ZLib/Zlib.cs    2009-08-14 09:51:42.000000000 -0500
+++ DotNetZip-dev/ZLib/Zlib.cs    2009-09-21 10:33:11.138489900 -0500
@@ -374,13 +374,13 @@
         internal static readonly short[] static_dtree = new short[] { 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5, 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5, 1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5, 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
 
         //UPGRADE_NOTE: The initialization of  'static_l_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_l_desc;
+        internal static readonly StaticTree static_l_desc;
 
         //UPGRADE_NOTE: The initialization of  'static_d_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_d_desc;
+        internal static readonly StaticTree static_d_desc;
 
         //UPGRADE_NOTE: The initialization of  'static_bl_desc' was moved to static method 'com.jcraft.jzlib.StaticTree'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
-        internal static StaticTree static_bl_desc;
+        internal static readonly StaticTree static_bl_desc;
 
         internal short[] static_tree; // static tree or null
         internal int[] extra_bits; // extra bits for each code or null
@@ -416,9 +416,9 @@
     internal sealed class Adler
     {
         // largest prime smaller than 65536
-        private static int BASE = 65521;
+        private static readonly int BASE = 65521;
         // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
-        private static int NMAX = 5552;
+        private static readonly int NMAX = 5552;
 
         static internal long Adler32(long adler, byte[] buf, int index, int len)
         {

Coordinator
Sep 22, 2009 at 7:51 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Sep 22, 2009 at 7:51 AM

Thanks for the update.