CarotDav_decompile/Rei.Fs/ZlibStream.cs

259 lines
8.6 KiB
C#

// Decompiled with JetBrains decompiler
// Type: Rei.Fs.ZlibStream
// Assembly: Rei.Fs, Version=1.13.2.8796, Culture=neutral, PublicKeyToken=null
// MVID: D8B08A8B-697C-4439-9CFF-1BE4EE46F7B0
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.dll
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Rei.Fs
{
public class ZlibStream : Stream
{
private static FieldInfo finfo1 = typeof (DeflateStream).GetField("inflater", BindingFlags.Instance | BindingFlags.NonPublic);
private static FieldInfo finfo2 = ZlibStream.finfo1.FieldType.GetField("input", BindingFlags.Instance | BindingFlags.NonPublic);
private static MethodInfo minfo1 = ZlibStream.finfo2.FieldType.GetMethod("SkipToByteBoundary", BindingFlags.Instance | BindingFlags.Public);
private static MethodInfo minfo2 = ZlibStream.finfo2.FieldType.GetMethod("CopyTo", BindingFlags.Instance | BindingFlags.Public);
private static MethodInfo minfo3 = ZlibStream.finfo2.FieldType.GetMethod("SetInput", BindingFlags.Instance | BindingFlags.Public);
private static PropertyInfo pinfo1 = ZlibStream.finfo2.FieldType.GetProperty("AvailableBytes", BindingFlags.Instance | BindingFlags.Public);
private Stream m_stream;
private DeflateStream m_deflatestream;
private CompressionMode m_mode;
private bool m_leaveopen;
private Adler32 m_adler;
private bool m_rawdeflate;
public ZlibStream(Stream stream, CompressionMode mode)
: this(stream, mode, false)
{
}
public ZlibStream(Stream stream, CompressionMode mode, bool leaveOpen)
: this(stream, mode, leaveOpen, false)
{
}
public ZlibStream(Stream stream, CompressionMode mode, bool leaveOpen, bool canconvertrawdeflate)
{
if (stream == null)
throw new ArgumentNullException();
this.m_stream = stream;
this.m_deflatestream = (DeflateStream) null;
this.m_mode = mode;
this.m_leaveopen = leaveOpen;
this.m_rawdeflate = canconvertrawdeflate;
this.m_adler = (Adler32) null;
}
public override bool CanRead
{
get
{
if (this.m_stream == null)
return false;
if (this.m_deflatestream != null)
return this.m_deflatestream.CanRead;
return this.m_mode == CompressionMode.Decompress && this.m_stream.CanRead;
}
}
public override bool CanWrite
{
get
{
if (this.m_stream == null)
return false;
if (this.m_deflatestream != null)
return this.m_deflatestream.CanWrite;
return this.m_mode == CompressionMode.Compress && this.m_stream.CanWrite;
}
}
public override void Flush()
{
if (this.m_stream == null)
throw new ObjectDisposedException((string) null);
if (this.m_deflatestream == null)
this.m_stream.Flush();
else
this.m_deflatestream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (this.m_stream == null)
throw new ObjectDisposedException((string) null);
if (this.m_mode == CompressionMode.Compress)
throw new InvalidOperationException();
if (this.m_deflatestream == null)
{
byte[] numArray = new byte[2];
int num1 = this.m_stream.ReadByte();
if (num1 < 0)
throw new InvalidDataException();
numArray[0] = checked ((byte) num1);
int num2 = this.m_stream.ReadByte();
if (num2 < 0)
throw new InvalidDataException();
numArray[1] = checked ((byte) num2);
byte num3 = numArray[0];
if (((int) num3 & 15) == 8 && (int) (byte) ((uint) num3 >> 4) <= 7)
{
byte num4 = numArray[1];
int num5 = (int) num4 & 31;
bool flag = ((int) num4 & 32) != 0;
int num6 = (int) (byte) ((uint) num4 >> 7);
if (!flag && checked ((int) num3 * 256 + (int) num4) % 31 == 0)
{
this.m_adler = new Adler32();
this.m_deflatestream = new DeflateStream(this.m_stream, this.m_mode, true);
goto label_15;
}
}
if (!this.m_rawdeflate)
throw new NotSupportedException();
this.m_adler = (Adler32) null;
this.m_deflatestream = new DeflateStream(this.m_stream, this.m_mode, true);
object objectValue1 = RuntimeHelpers.GetObjectValue(ZlibStream.finfo1.GetValue((object) this.m_deflatestream));
object objectValue2 = RuntimeHelpers.GetObjectValue(ZlibStream.finfo2.GetValue(RuntimeHelpers.GetObjectValue(objectValue1)));
ZlibStream.minfo3.Invoke(RuntimeHelpers.GetObjectValue(objectValue2), new object[3]
{
(object) numArray,
(object) 0,
(object) 2
});
}
label_15:
int length = this.m_deflatestream.Read(buffer, offset, count);
if (this.m_adler != null)
{
if (length != 0)
{
this.m_adler.Calc(buffer, offset, length);
}
else
{
byte[] numArray = this.m_adler.Value;
object objectValue1 = RuntimeHelpers.GetObjectValue(ZlibStream.finfo1.GetValue((object) this.m_deflatestream));
object objectValue2 = RuntimeHelpers.GetObjectValue(ZlibStream.finfo2.GetValue(RuntimeHelpers.GetObjectValue(objectValue1)));
ZlibStream.minfo1.Invoke(RuntimeHelpers.GetObjectValue(objectValue2), (object[]) null);
byte[] buffer1 = new byte[checked (Conversions.ToInteger(ZlibStream.pinfo1.GetValue(RuntimeHelpers.GetObjectValue(objectValue2), (object[]) null)) - 1 + 1)];
int integer = Conversions.ToInteger(ZlibStream.minfo2.Invoke(RuntimeHelpers.GetObjectValue(objectValue2), new object[3]
{
(object) buffer1,
(object) 0,
(object) buffer1.Length
}));
while (integer < 4)
{
int num = this.m_stream.Read(buffer1, integer, checked (4 - integer));
if (num == 0)
throw new InvalidDataException();
checked { integer += num; }
}
if ((int) buffer1[0] != (int) numArray[0])
throw new InvalidDataException();
if ((int) buffer1[1] != (int) numArray[1])
throw new InvalidDataException();
if ((int) buffer1[2] != (int) numArray[2])
throw new InvalidDataException();
if ((int) buffer1[3] != (int) numArray[3])
throw new InvalidDataException();
this.m_adler = (Adler32) null;
}
}
return length;
}
public override void Write(byte[] buffer, int offset, int count)
{
if (this.m_stream == null)
throw new ObjectDisposedException((string) null);
if (this.m_mode == CompressionMode.Decompress)
throw new InvalidOperationException();
if (this.m_deflatestream == null)
{
this.m_stream.WriteByte((byte) 120);
this.m_stream.WriteByte((byte) 156);
this.m_adler = new Adler32();
this.m_deflatestream = new DeflateStream(this.m_stream, this.m_mode, true);
}
this.m_deflatestream.Write(buffer, offset, count);
this.m_adler.Calc(buffer, offset, count);
}
public override bool CanSeek
{
get
{
return false;
}
}
public override long Length
{
get
{
throw new NotImplementedException();
}
}
public override long Position
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
protected override void Dispose(bool disposing)
{
try
{
if (!disposing || this.m_stream == null)
return;
if (this.m_deflatestream != null)
{
this.m_deflatestream.Dispose();
this.m_deflatestream = (DeflateStream) null;
if (this.m_mode == CompressionMode.Compress)
{
byte[] numArray = this.m_adler.Value;
this.m_stream.WriteByte(numArray[0]);
this.m_stream.WriteByte(numArray[1]);
this.m_stream.WriteByte(numArray[2]);
this.m_stream.WriteByte(numArray[3]);
this.m_adler = (Adler32) null;
}
}
if (!this.m_leaveopen)
this.m_stream.Dispose();
this.m_stream = (Stream) null;
}
finally
{
base.Dispose(disposing);
}
}
}
}