using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BrewMonster { public class CECRTDebug { } public class CECDataReader { public const int SEEK_SET = 0; /* set file offset to offset */ public const int SEEK_CUR = 1; /* set file offset to current plus offset */ public const int SEEK_END = 2; /* set file offset to EOF plus offset */ private byte[] data; private int length; private int m_pStart; private int m_pCur; private int m_pEnd; public CECDataReader(byte[] pDataBuf, int iDataLen) { if (pDataBuf == null || iDataLen <= 0) { throw new System.InvalidOperationException("Invalid data"); } data = new byte[iDataLen]; System.Array.Copy(pDataBuf, data, iDataLen); this.length = iDataLen; m_pStart = 0; m_pCur = 0; m_pEnd = iDataLen; } public void Offset(int iOffset, int iSeekFlag) { int pCur = 0; switch (iSeekFlag) { case SEEK_SET: pCur = m_pStart + iOffset; break; case SEEK_CUR: pCur = m_pCur + iOffset; break; case SEEK_END: pCur = m_pEnd + iOffset; break; } m_pCur = pCur; BoundCheck(0); } public int ReadInt() { BoundCheck(sizeof(int)); int value = System.BitConverter.ToInt32(data, m_pCur); m_pCur += sizeof(int); return value; } public short ReadShort() { BoundCheck(sizeof(short)); short value = System.BitConverter.ToInt16(data, m_pCur); m_pCur += sizeof(short); return value; } public ulong ReadULong() { BoundCheck(sizeof(ulong)); ulong value = System.BitConverter.ToUInt64(data, m_pCur); m_pCur += sizeof(ulong); return value; } public ushort ReadUShort() { BoundCheck(sizeof(ushort)); ushort value = System.BitConverter.ToUInt16(data, m_pCur); m_pCur += sizeof(ushort); return value; } public ushort[] ReadUShortArray(int size) { BoundCheck(size * sizeof(ushort)); ushort[] result = new ushort[size]; System.Array.Copy(data, m_pCur, result, 0, size * sizeof(ushort)); m_pCur += size * sizeof(ushort); return result; } public uint ReadUInt() { BoundCheck(sizeof(uint)); uint value = System.BitConverter.ToUInt32(data, m_pCur); m_pCur += sizeof(uint); return value; } public float ReadFloat() { BoundCheck(sizeof(float)); float value = System.BitConverter.ToSingle(data, m_pCur); m_pCur += sizeof(float); return value; } public byte ReadByte() { BoundCheck(sizeof(byte)); byte value = data[m_pCur]; m_pCur += sizeof(byte); return value; } public byte[] ReadData(int size) { BoundCheck(size); byte[] result = new byte[size]; System.Array.Copy(data, m_pCur, result, 0, size); m_pCur += size; return result; } public ushort[] ReadUshortArray(int size) { BoundCheck(size * sizeof(ushort)); ushort[] result = new ushort[size]; System.Array.Copy(data, m_pCur, result, 0, size * sizeof(ushort)); m_pCur += size * sizeof(ushort); return result; } public byte[] ReadByteArray(int size) { BoundCheck(size); byte[] result = new byte[size]; System.Array.Copy(data, m_pCur, result, 0, size); m_pCur += size; return result; } public void PrintData(int size) { for(int i =m_pCur; i < m_pCur + size; i++) { BMLogger.Log("[thn]CECDataReader: PrintData: data[" + i + "]: " + data[i]); } } void BoundCheck(int dwSize) { if (m_pCur + dwSize < m_pStart || m_pCur + dwSize > m_pEnd) { throw new System.InvalidOperationException("Out of bounds"); } } } }