[Mono-osx] Using zlib on Mac PPC

pablosantosluac at terra.es pablosantosluac at terra.es
Thu Sep 11 12:02:27 EDT 2008


Hi there,

I've been making some tests with Zlib at MacOS X PPC and it was driving 
me nuts. Whatever I tried, didn't work. I made the code work in windows, 
linux and solaris x86, and even MacOS x86, but not PPC.

Finally I found the problem, so I hope it can help:

I was using this import on the x86 platforms:

         [DllImport("z")]
         public static extern ZLibError compress(
             byte[] dest, ref long destLength,
             byte[] source, long sourceLength);

But the problem is I'm using a long, and the correct implementation 
(zlib.h defines a long, but AFAIK long is 8 bytes in C# and doesn't 
match this size on most of the C implementations) is an int:

         [DllImport("z")]
         public static extern ZLibError compress(
             byte[] dest, ref int destLength,
             byte[] source, int sourceLength);

Pretty easy, but it was making me crazy.

Now it works on all platforms I tested: MacOS PPC, Linux x86, Windows 
and Solaris SPARC


Hope it helps


pablo


www.plasticscm.com






using System;
using System.IO;
using System.Runtime.InteropServices;

namespace compress
{
     class Class1
     {

         public enum ZLibError : int
         {
             Z_OK = 0,
             Z_STREAM_END = 1,
             Z_NEED_DICT = 2,
             Z_ERRNO = (-1),
             Z_STREAM_ERROR = (-2),
             Z_DATA_ERROR = (-3),
             Z_MEM_ERROR = (-4),
             Z_BUF_ERROR = (-5),
             Z_VERSION_ERROR = (-6),
         }

         [DllImport("z")]
         public static extern ZLibError compress(
             byte[] dest, ref int destLength,
             byte[] source, int sourceLength);

         static void Main(string[] args)
         {

             if( args.Length < 1 )
             {
                 Console.WriteLine("usage: compress file");
                 return;
             }
             FileStream st = File.Open(args[0], FileMode.Open);

             byte[] data = new byte[1024];

             int srcLen = st.Read(data, 0, data.Length);

             Console.WriteLine("{0} bytes read", srcLen);

             int dstLen = data.Length;
             byte[] compressedData = new byte[srcLen];

             Console.WriteLine("data size {0}", data.Length);


             ZLibError error = compress(
                 compressedData, ref dstLen,
                 data, srcLen);

             if( error != ZLibError.Z_OK && error != ZLibError.Z_BUF_ERROR )
             {
                 Console.WriteLine("error compressing!! not setting 
compression level. {0}", error);
                 return;
             }

             Console.WriteLine("possible compressed size {0} bytes", 
dstLen);

             if( (dstLen >= data.Length) ||
                 (error == ZLibError.Z_BUF_ERROR) )
             {
                 Console.WriteLine("header overload! {0} dstLen:{1}", 
error, dstLen);
                 return;
             }

             Console.WriteLine("result {0}", error);
         }
     }
}


More information about the Mono-osx mailing list