1 // ZipStream.cs created with MonoDevelop
2 // User: alan at 15:16 20/10/2008
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
9 using System.Runtime.InteropServices;
13 class ZipStream : Stream
15 const int ZLIB_FILEFUNC_SEEK_CUR = 1;
16 const int ZLIB_FILEFUNC_SEEK_END = 2;
17 const int ZLIB_FILEFUNC_SEEK_SET = 0;
23 public override bool CanRead {
24 get { return canRead; }
27 public override bool CanSeek {
28 get { return canSeek; }
31 public override bool CanWrite {
32 get { return canWrite; }
35 public override bool CanTimeout {
39 private Stream DataStream {
43 public ZlibFileFuncDef32 IOFunctions32 {
47 public ZlibFileFuncDef64 IOFunctions64 {
51 public override long Length {
52 get { return DataStream.Length; }
59 public override long Position {
60 get { return DataStream.Position; }
61 set { DataStream.Position = value; }
64 public ZipStream (Stream dataStream, bool ownsStream)
66 // FIXME: Not necessarily true
71 DataStream = dataStream;
72 OwnsStream = ownsStream;
74 ZlibFileFuncDef32 f32 = new ZlibFileFuncDef32 ();
75 f32.opaque = IntPtr.Zero;
76 f32.zclose_file = CloseFile_Native;
77 f32.zerror_file = TestError_Native;
78 f32.zopen_file = OpenFile_Native;
79 f32.zread_file = ReadFile_Native32;
80 f32.zseek_file = SeekFile_Native32;
81 f32.ztell_file = TellFile_Native32;
82 f32.zwrite_file = WriteFile_Native32;
85 ZlibFileFuncDef64 f64 = new ZlibFileFuncDef64 ();
86 f64.opaque = IntPtr.Zero;
87 f64.zclose_file = CloseFile_Native;
88 f64.zerror_file = TestError_Native;
89 f64.zopen_file = OpenFile_Native;
90 f64.zread_file = ReadFile_Native64;
91 f64.zseek_file = SeekFile_Native64;
92 f64.ztell_file = TellFile_Native64;
93 f64.zwrite_file = WriteFile_Native64;
97 protected override void Dispose(bool disposing)
104 DataStream.Dispose ();
107 public override void Flush()
112 public override int Read(byte[] buffer, int offset, int count)
114 return DataStream.Read (buffer, offset, count);
117 public override long Seek(long offset, SeekOrigin origin)
119 DataStream.Seek (offset, origin);
120 return DataStream.Position;
123 public override void SetLength(long value)
125 DataStream.SetLength (value);
128 public override void Write(byte[] buffer, int offset, int count)
130 DataStream.Write (buffer, offset, count);
134 int CloseFile_Native (IntPtr opaque, IntPtr stream)
140 IntPtr OpenFile_Native (IntPtr opaque, string filename, int mode)
142 // always success. The stream is opened in managed code
143 return new IntPtr (1);
146 unsafe uint ReadFile_Native32 (IntPtr opaque, IntPtr stream, IntPtr buffer, uint size)
148 return (uint) ReadFile_Native64 (opaque, stream, buffer, size);
151 unsafe ulong ReadFile_Native64 (IntPtr opaque, IntPtr stream, IntPtr buffer, ulong size)
153 int count = (int) size;
154 byte[] b = new byte[count];
158 read = Math.Max (0, Read (b, 0, count));
159 byte* ptrBuffer = (byte*) buffer.ToPointer ();
160 for (int i = 0; i < count && i < read; i ++)
169 int SeekFile_Native32 (IntPtr opaque, IntPtr stream, uint offset, int origin)
171 return (int) SeekFile_Native64 (opaque, stream, offset, origin);
174 long SeekFile_Native64 (IntPtr opaque, IntPtr stream, ulong offset, int origin)
177 if (origin == ZipStream.ZLIB_FILEFUNC_SEEK_CUR)
178 seek = SeekOrigin.Current;
179 else if (origin == ZLIB_FILEFUNC_SEEK_END)
180 seek = SeekOrigin.End;
181 else if (origin == ZLIB_FILEFUNC_SEEK_SET)
182 seek = SeekOrigin.Begin;
186 Seek ((long) offset, seek);
191 int TellFile_Native32 (IntPtr opaque, IntPtr stream)
193 return (int) TellFile_Native64 (opaque, stream);
196 long TellFile_Native64 (IntPtr opaque, IntPtr stream)
201 int TestError_Native (IntPtr opaque, IntPtr stream)
207 unsafe uint WriteFile_Native32 (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ uint size)
209 return (uint) WriteFile_Native64 (opaque, stream, buffer, size);
212 unsafe ulong WriteFile_Native64 (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ ulong size)
214 int count = (int) size;
215 byte[] b = new byte[count];
217 byte* ptrBuffer = (byte*) buffer.ToPointer ();
218 for (int i = 0; i < count; i ++)
227 return (ulong) count;