ByteBuffer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ByteBuffer.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. namespace Mono.Cecil.PE {
  30. class ByteBuffer {
  31. internal byte [] buffer;
  32. internal int length;
  33. internal int position;
  34. public ByteBuffer ()
  35. {
  36. this.buffer = Empty<byte>.Array;
  37. }
  38. public ByteBuffer (int length)
  39. {
  40. this.buffer = new byte [length];
  41. }
  42. public ByteBuffer (byte [] buffer)
  43. {
  44. this.buffer = buffer ?? Empty<byte>.Array;
  45. this.length = this.buffer.Length;
  46. }
  47. public void Reset (byte [] buffer)
  48. {
  49. this.buffer = buffer ?? Empty<byte>.Array;
  50. this.length = this.buffer.Length;
  51. }
  52. public void Advance (int length)
  53. {
  54. position += length;
  55. }
  56. public byte ReadByte ()
  57. {
  58. return buffer [position++];
  59. }
  60. public sbyte ReadSByte ()
  61. {
  62. return (sbyte) ReadByte ();
  63. }
  64. public byte [] ReadBytes (int length)
  65. {
  66. var bytes = new byte [length];
  67. Buffer.BlockCopy (buffer, position, bytes, 0, length);
  68. position += length;
  69. return bytes;
  70. }
  71. public ushort ReadUInt16 ()
  72. {
  73. ushort value = (ushort) (buffer [position]
  74. | (buffer [position + 1] << 8));
  75. position += 2;
  76. return value;
  77. }
  78. public short ReadInt16 ()
  79. {
  80. return (short) ReadUInt16 ();
  81. }
  82. public uint ReadUInt32 ()
  83. {
  84. uint value = (uint) (buffer [position]
  85. | (buffer [position + 1] << 8)
  86. | (buffer [position + 2] << 16)
  87. | (buffer [position + 3] << 24));
  88. position += 4;
  89. return value;
  90. }
  91. public int ReadInt32 ()
  92. {
  93. return (int) ReadUInt32 ();
  94. }
  95. public ulong ReadUInt64 ()
  96. {
  97. uint low = ReadUInt32 ();
  98. uint high = ReadUInt32 ();
  99. return (((ulong) high) << 32) | low;
  100. }
  101. public long ReadInt64 ()
  102. {
  103. return (long) ReadUInt64 ();
  104. }
  105. public uint ReadCompressedUInt32 ()
  106. {
  107. byte first = ReadByte ();
  108. if ((first & 0x80) == 0)
  109. return first;
  110. if ((first & 0x40) == 0)
  111. return ((uint) (first & ~0x80) << 8)
  112. | ReadByte ();
  113. return ((uint) (first & ~0xc0) << 24)
  114. | (uint) ReadByte () << 16
  115. | (uint) ReadByte () << 8
  116. | ReadByte ();
  117. }
  118. public int ReadCompressedInt32 ()
  119. {
  120. var value = (int) (ReadCompressedUInt32 () >> 1);
  121. if ((value & 1) == 0)
  122. return value;
  123. if (value < 0x40)
  124. return value - 0x40;
  125. if (value < 0x2000)
  126. return value - 0x2000;
  127. if (value < 0x10000000)
  128. return value - 0x10000000;
  129. return value - 0x20000000;
  130. }
  131. public float ReadSingle ()
  132. {
  133. if (!BitConverter.IsLittleEndian) {
  134. var bytes = ReadBytes (4);
  135. Array.Reverse (bytes);
  136. return BitConverter.ToSingle (bytes, 0);
  137. }
  138. float value = BitConverter.ToSingle (buffer, position);
  139. position += 4;
  140. return value;
  141. }
  142. public double ReadDouble ()
  143. {
  144. if (!BitConverter.IsLittleEndian) {
  145. var bytes = ReadBytes (8);
  146. Array.Reverse (bytes);
  147. return BitConverter.ToDouble (bytes, 0);
  148. }
  149. double value = BitConverter.ToDouble (buffer, position);
  150. position += 8;
  151. return value;
  152. }
  153. }
  154. }