[jit] Use MONO_INS_IS_PCONST_NULL () macro in more places.
[mono.git] / mcs / class / WindowsBase / ZipSharp / ZipStream.cs
1 // ZipStream.cs created with MonoDevelop
2 // User: alan at 15:16 20/10/2008
3 //
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
5 //
6
7 using System;
8 using System.IO;
9 using System.Runtime.InteropServices;
10
11 namespace zipsharp
12 {
13         class ZipStream : Stream
14         {
15                 const int ZLIB_FILEFUNC_SEEK_CUR = 1;
16                 const int ZLIB_FILEFUNC_SEEK_END = 2;
17                 const int ZLIB_FILEFUNC_SEEK_SET = 0;
18                 
19                 bool canRead;
20                 bool canSeek;
21                 bool canWrite;
22                 
23                 public override bool CanRead {
24                         get { return canRead; }
25                 }
26
27                 public override bool CanSeek {
28                         get { return canSeek; }
29                 }
30
31                 public override bool CanWrite {
32                         get { return canWrite; }
33                 }
34
35                 public override bool CanTimeout {
36                         get { return false; }
37                 }
38                 
39                 private Stream DataStream {
40                         get; set;
41                 }
42
43                 public ZlibFileFuncDef IOFunctions {
44                         get; set;
45                 }
46
47                 public override long Length {
48                         get { return DataStream.Length; }
49                 }
50
51                 bool OwnsStream {
52                         get; set;
53                 }
54                 
55                 public override long Position {
56                         get { return DataStream.Position; }
57                         set { DataStream.Position = value; }
58                 }
59                 
60                 public ZipStream (Stream dataStream, bool ownsStream)
61                 {
62                         // FIXME: Not necessarily true
63                         canRead = true;
64                         canSeek = true;
65                         canWrite = true;
66
67                         DataStream = dataStream;
68                         OwnsStream = ownsStream;
69                         
70                         ZlibFileFuncDef f = new ZlibFileFuncDef();
71                         
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;
80
81                         IOFunctions = f;
82                 }
83
84                 protected override void Dispose(bool disposing)
85                 {
86                         if (!disposing)
87                                 return;
88
89                         DataStream.Flush ();
90                         if (OwnsStream)
91                                 DataStream.Dispose ();
92                 }
93
94                 public override void Flush()
95                 {
96                         DataStream.Flush ();
97                 }
98                 
99                 public override int Read(byte[] buffer, int offset, int count)
100                 {
101                         return DataStream.Read (buffer, offset, count);
102                 }
103
104                 public override long Seek(long offset, SeekOrigin origin)
105                 {
106                         DataStream.Seek (offset, origin);
107                         return DataStream.Position;
108                 }
109
110                 public override void SetLength(long value)
111                 {
112                         DataStream.SetLength (value);
113                 }
114
115                 public override void Write(byte[] buffer, int offset, int count)
116                 {
117                         DataStream.Write (buffer, offset, count);
118                         Flush ();
119                 }
120
121                 int CloseFile_Native (IntPtr opaque, IntPtr stream)
122                 {
123                         Close ();
124                         return 0;
125                 }
126                 
127                 IntPtr OpenFile_Native (IntPtr opaque, string filename, int mode)
128                 {
129                         // always success. The stream is opened in managed code
130                         return new IntPtr (1);
131                 }
132
133                 unsafe IntPtr ReadFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, IntPtr size)
134                 {
135                         int count = size.ToInt32 ();
136                         byte[] b = new byte[count];
137                         int read;
138                         
139                         try {
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 ++)
143                                         ptrBuffer[i] = b[i];
144                         } catch {
145                                 read = -1;
146                         }
147
148                         return new IntPtr (read);
149                 }
150
151                 IntPtr SeekFile_Native (IntPtr opaque, IntPtr stream, IntPtr offset, int origin)
152                 {
153                         SeekOrigin seek;
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;
160                         else
161                                 return new IntPtr (-1);
162
163                         Seek (offset.ToInt64 (), seek);
164                         
165                         return new IntPtr (0);
166                 }
167
168                 IntPtr TellFile_Native (IntPtr opaque, IntPtr stream)
169                 {
170                         if (IntPtr.Size == 4)
171                                 return new IntPtr ((int)Position);
172                         else if (IntPtr.Size == 8)
173                                 return new IntPtr (Position);
174                         else
175                                 return new IntPtr (-1);
176                 }
177
178                 int TestError_Native (IntPtr opaque, IntPtr stream)
179                 {
180                         // No errors here.
181                         return 0;
182                 }
183
184                 unsafe IntPtr WriteFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ IntPtr size)
185                 {
186                         int count = size.ToInt32 ();
187                         byte[] b = new byte[count];
188
189                         byte* ptrBuffer = (byte*) buffer.ToPointer ();
190                         for (int i = 0; i < count; i ++)
191                                 b[i] = ptrBuffer[i];
192
193                         try {
194                                 Write (b, 0, count);
195                         } catch {
196                                 
197                         }
198
199                         return new IntPtr (count);
200                 }
201         }
202 }