1 using System;
  
   2 
  
   3 namespace System.IO
  
   4 {
  
   5     public class MemoryReader
  
   6     {
  
   7         public nothrow MemoryReader(byte* ptr_, long count_) : ptr(ptr_), pos(ptr), count(count_)
  
   8         {
  
   9         }
  
  10         public byte ReadByte()
  
  11         {
  
  12             if (pos - ptr >= count)
  
  13             {
  
  14                 throw Exception("memory reader: unexpected end of data");
  
  15             }
  
  16             return *pos++;
  
  17         }
  
  18         public sbyte ReadSByte()
  
  19         {
  
  20             return cast<sbyte>(ReadByte());
  
  21         }
  
  22         public ushort ReadUShort()
  
  23         {
  
  24             byte b0 = ReadByte();
  
  25             byte b1 = ReadByte();
  
  26             return (cast<ushort>(b0) << 8u) | cast<ushort>(b1);
  
  27         }
  
  28         public short ReadShort()
  
  29         {
  
  30             return cast<short>(ReadUShort());
  
  31         }
  
  32         public uint ReadUInt()
  
  33         {
  
  34             byte b0 = ReadByte();
  
  35             byte b1 = ReadByte();
  
  36             byte b2 = ReadByte();
  
  37             byte b3 = ReadByte();
  
  38             return (cast<uint>(b0) << 24u) | (cast<uint>(b1) << 16u) | (cast<uint>(b2) << 8u) | cast<uint>(b3);
  
  39         }
  
  40         public int ReadInt()
  
  41         {
  
  42             return cast<int>(ReadUInt());
  
  43         }
  
  44         public ulong ReadULong()
  
  45         {
  
  46             byte b0 = ReadByte();
  
  47             byte b1 = ReadByte();
  
  48             byte b2 = ReadByte();
  
  49             byte b3 = ReadByte();
  
  50             byte b4 = ReadByte();
  
  51             byte b5 = ReadByte();
  
  52             byte b6 = ReadByte();
  
  53             byte b7 = ReadByte();
  
  54             return (cast<ulong>(b0) << 56u) | (cast<ulong>(b1) << 48u) | (cast<ulong>(b2) << 40u) | (cast<ulong>(b3) << 32u) | 
  
  55                 (cast<ulong>(b4) << 24u) | (cast<ulong>(b5) << 16u) | (cast<ulong>(b6) << 8u) | cast<ulong>(b7);
  
  56         }
  
  57         public long ReadLong()
  
  58         {
  
  59             return cast<long>(ReadULong());
  
  60         }
  
  61         public DateTime ReadDateTime()
  
  62         {
  
  63             short year = ReadShort();
  
  64             Month month = cast<Month>(ReadSByte());
  
  65             sbyte day = ReadSByte();
  
  66             Date date(year, month, day);
  
  67             int secs = ReadInt();
  
  68             DateTime dt(date, secs);
  
  69             return dt;
  
  70         }
  
  71         public string ReadString()
  
  72         {
  
  73             string result;
  
  74             byte b = ReadByte();
  
  75             while (b != 0u)
  
  76             {
  
  77                 result.Append(cast<char>(b));
  
  78                 b = ReadByte();
  
  79             }
  
  80             return result;
  
  81         }
  
  82         private byte* ptr;
  
  83         private byte* pos;
  
  84         private long count;
  
  85     }
  
  86 }