Created
January 13, 2015 02:36
-
-
Save larvata/6cb632f3d739116c6a8a to your computer and use it in GitHub Desktop.
A binary reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuffWalker | |
{ | |
public byte[] Buffer; | |
private uint _positon; | |
public EndiannessEnum Endianness { get; set; } | |
public long RestBytesCount | |
{ | |
get { return this.Buffer.LongLength - _positon; } | |
} | |
public enum EndiannessEnum | |
{ | |
BigEndian, | |
LittleEndian | |
} | |
public void Skip(uint bytesToSkip) | |
{ | |
_positon += bytesToSkip; | |
} | |
public void Goto(uint position) | |
{ | |
_positon = position; | |
} | |
public uint GetCurrentPosition | |
{ | |
get { return _positon; } | |
} | |
public BuffWalker(string path) | |
{ | |
_positon = 0; | |
Buffer = File.ReadAllBytes(path); | |
} | |
public string GetString(uint bytesCount) | |
{ | |
var result = Encoding.UTF8.GetString(GetBytes(bytesCount)); | |
return result; | |
} | |
public Int16 GetInt16() | |
{ | |
var buff = GetBytes(2); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToInt16(buff.ToArray(), 0); | |
} | |
public Int32 GetInt32() | |
{ | |
var buff = GetBytes(4); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToInt32(buff.ToArray(), 0); | |
} | |
public Int64 GetInt64() | |
{ | |
var buff = GetBytes(8); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToInt64(buff.ToArray(), 0); | |
} | |
public UInt16 GetUInt16() | |
{ | |
var buff = GetBytes(2); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToUInt16(buff.ToArray(), 0); | |
} | |
public UInt32 GetUInt32() | |
{ | |
var buff = GetBytes(4); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToUInt32(buff.ToArray(), 0); | |
} | |
public UInt64 GetUInt64() | |
{ | |
var buff = GetBytes(8); | |
if (Endianness == EndiannessEnum.LittleEndian) | |
{ | |
buff = buff.Reverse().ToArray(); | |
} | |
return BitConverter.ToUInt64(buff.ToArray(), 0); | |
} | |
/// <summary> | |
/// bytesCount fixed to 10 bytes | |
/// </summary> | |
/// <returns></returns> | |
public DateTime GetDateTime() | |
{ | |
var year = Convert.ToInt32(GetString(2)); | |
var month = Convert.ToInt32(GetString(2)); | |
var day = Convert.ToInt32(GetString(2)); | |
var hour = Convert.ToInt32(GetString(2)); | |
var minute = Convert.ToInt32(GetString(2)); | |
var date = new DateTime(2000 + year, month, day, hour, minute, 0); | |
return date; | |
} | |
public byte[] GetBytes(uint bytesCount) | |
{ | |
var result = new byte[bytesCount]; | |
Array.Copy(Buffer, _positon, result, 0, bytesCount); | |
Skip(bytesCount); | |
return result; | |
} | |
public byte GetByte() | |
{ | |
var result = GetBytes(1); | |
return result[0]; | |
} | |
public byte[] GetWord() | |
{ | |
var result = GetBytes(2); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment