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 ZlibFileFuncDef IOFunctions
{
47 public override long Length
{
48 get { return DataStream.Length; }
55 public override long Position
{
56 get { return DataStream.Position; }
57 set { DataStream.Position = value; }
60 public ZipStream (Stream dataStream
, bool ownsStream
)
62 // FIXME: Not necessarily true
67 DataStream
= dataStream
;
68 OwnsStream
= ownsStream
;
70 ZlibFileFuncDef f
= new ZlibFileFuncDef();
72 f
.opaque
= IntPtr
.Zero
;
73 f
.zclose_file
= CloseFile_Native
;
74 f
.zerror_file
= TestError_Native
;
75 f
.zopen_file
= OpenFile_Native
;
76 f
.zread_file
= ReadFile_Native
;
77 f
.zseek_file
= SeekFile_Native
;
78 f
.ztell_file
= TellFile_Native
;
79 f
.zwrite_file
= WriteFile_Native
;
84 protected override void Dispose(bool disposing
)
91 DataStream
.Dispose ();
94 public override void Flush()
99 public override int Read(byte[] buffer
, int offset
, int count
)
101 return DataStream
.Read (buffer
, offset
, count
);
104 public override long Seek(long offset
, SeekOrigin origin
)
106 DataStream
.Seek (offset
, origin
);
107 return DataStream
.Position
;
110 public override void SetLength(long value)
112 DataStream
.SetLength (value);
115 public override void Write(byte[] buffer
, int offset
, int count
)
117 DataStream
.Write (buffer
, offset
, count
);
121 int CloseFile_Native (IntPtr opaque
, IntPtr stream
)
127 IntPtr
OpenFile_Native (IntPtr opaque
, string filename
, int mode
)
129 // always success. The stream is opened in managed code
130 return new IntPtr (1);
133 unsafe IntPtr
ReadFile_Native (IntPtr opaque
, IntPtr stream
, IntPtr buffer
, IntPtr size
)
135 int count
= size
.ToInt32 ();
136 byte[] b
= new byte[count
];
140 read
= Math
.Max (0, Read (b
, 0, count
));
141 byte* ptrBuffer
= (byte*) buffer
.ToPointer ();
142 for (int i
= 0; i
< count
&& i
< read
; i
++)
148 return new IntPtr (read
);
151 IntPtr
SeekFile_Native (IntPtr opaque
, IntPtr stream
, IntPtr offset
, int origin
)
154 if (origin
== ZipStream
.ZLIB_FILEFUNC_SEEK_CUR
)
155 seek
= SeekOrigin
.Current
;
156 else if (origin
== ZLIB_FILEFUNC_SEEK_END
)
157 seek
= SeekOrigin
.End
;
158 else if (origin
== ZLIB_FILEFUNC_SEEK_SET
)
159 seek
= SeekOrigin
.Begin
;
161 return new IntPtr (-1);
163 Seek (offset
.ToInt64 (), seek
);
165 return new IntPtr (0);
168 IntPtr
TellFile_Native (IntPtr opaque
, IntPtr stream
)
170 if (IntPtr
.Size
== 4)
171 return new IntPtr ((int)Position
);
172 else if (IntPtr
.Size
== 8)
173 return new IntPtr (Position
);
175 return new IntPtr (-1);
178 int TestError_Native (IntPtr opaque
, IntPtr stream
)
184 unsafe IntPtr
WriteFile_Native (IntPtr opaque
, IntPtr stream
, IntPtr buffer
, /* ulong */ IntPtr size
)
186 int count
= size
.ToInt32 ();
187 byte[] b
= new byte[count
];
189 byte* ptrBuffer
= (byte*) buffer
.ToPointer ();
190 for (int i
= 0; i
< count
; i
++)
199 return new IntPtr (count
);