Initial commit

KaitaiStream: Initial commit
This commit is contained in:
Morgan Gangwere 2016-07-15 00:57:37 -07:00
parent 7afe4c765f
commit 9ba86edaf0
1 changed files with 170 additions and 0 deletions

170
KaitaiStream.cs Normal file
View File

@ -0,0 +1,170 @@
using System;
using System.IO;
namespace Kaitai
{
public class KaitaiStream
{
private Stream mStream;
#region Constructors
public KaitaiStream(String filename)
{
mStream = (Stream)(new FileStream(filename, FileMode.Open));
}
public KaitaiStream(byte[] buffer)
{
mStream = new MemoryStream(buffer);
}
#endregion
#region Raw stream passthru
public long pos()
{
return mStream.Position;
}
public void seek(long position)
{
mStream.seek(position, SeekOrigin.Begin);
}
public bool isEof()
{
return mStream.Position == mStream.Length;
}
public byte[] readBytes(int n)
{
try
{
byte[] tmp = new byte[n];
int readCount = mStream.Read(tmp, 0, n);
Array.Resize(tmp, readCount);
return tmp;
}
catch(Exception e)
{
throw new IOException("Failed to read "+n+" bytes from stream", e);
}
}
#endregion
#region Read Byte/SByte
public SByte readS1()
{
int val = mStream.ReadByte();
if(val == -1)
throw new Exception("At EOF!");
else
return (SByte)val;
}
public byte readU1()
{
int val = mStream.ReadByte();
if(val == -1)
throw new Exception("At EOF!");
else
return (byte)(val);
}
#enredgion
#region 2-byte integer values
#region 16-bit integers (unsigned)
public Uint16 readU2le() {
byte[] tmp;
mStream.Read(tmp, 0, 2);
return (Uint16)( (tmp[0] << 8)+(tmp[1] << 0) );
}
public Uint16 readU2be() {
byte[] tmp;
mStream.Read(tmp, 0, 2);
return (Uint16)( (tmp[1] << 8)+(tmp[0] << 0) );
}
#endregion
#region 16-bit integers (signed)
public Int16 readS2le() {
byte[] tmp;
mStream.Read(tmp, 0, 2);
return (Int16)( (tmp[0] << 8)+(tmp[1] << 0) );
}
public Int16 readS2be() {
byte[] tmp;
mStream.Read(tmp, 0, 2);
return (Int16)( (tmp[1] << 8)+(tmp[0] << 0) );
}
#endregion
#endregion
#region 4-byte integer reads
#region 32-bit integers (Unsigned)
public Uint32 readU4le() { throw new NotImplementedException(); }
public Uint32 readU4be() { throw new NotImplementedException(); }
#endregion
#region 32-bit integers (signed)
public Uint32 readS4le() { throw new NotImplementedException(); }
public Uint32 readS4be() { throw new NotImplementedException(); }
#endregion
#endregion // 4-byte integer reads
#region 8-byte integer reads
#region 64-bit integers (unsigned)
public Uint64 readU8le() { throw new NotImplementedException(); }
public Uint64 readU8be() { throw new NotImplementedException(); }
#endregion
#region 64-bit integers (signed)
public Uint64 readS8le() { throw new NotImplementedException(); }
public Uint64 readS8be() { throw new NotImplementedException(); }
#endregion
#endregion // 8-byte integer reads
public byte[] readBytesFull()
{
int count = mStream.Length - mStream.Position;
byte[] buffer = new byte[count];
mStream.Read(buffer, 0, count);
return buffer;
}
public byte[] ensureFixedContents(int len, byte[] expected)
{
IOException e = new IOException();
e.Message = "Expected bytes: "+Convert.ToBase64String(expected)+Environment.NewLine;
byte[] buff = readBytes(len);
e.message += "Got bytes: "+Convert.ToBase64String(buff);
for(int idx =0; idx < len; idx++)
if(buff[idx] != expected[idx]) throw e;
return buff;
}
public String readStrEos(String encoding)
{
System.Text.Encoding _encoding = System.Text.Encoding.GetEncoding(encoding);
return _encoding.GetString(readBytesFull());
}
public String readStrByteLimit(int len, String encoding)
{
throw new NotImplementedException();
}
}
}