/Mono.Cecil.PE/ByteBuffer.cs

http://github.com/jbevain/cecil · C# · 336 lines · 263 code · 64 blank · 9 comment · 36 complexity · 2e45d631b077a8ba9d680c1bef3e33e6 MD5 · raw file

  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. namespace Mono.Cecil.PE {
  12. class ByteBuffer {
  13. internal byte [] buffer;
  14. internal int length;
  15. internal int position;
  16. public ByteBuffer ()
  17. {
  18. this.buffer = Empty<byte>.Array;
  19. }
  20. public ByteBuffer (int length)
  21. {
  22. this.buffer = new byte [length];
  23. }
  24. public ByteBuffer (byte [] buffer)
  25. {
  26. this.buffer = buffer ?? Empty<byte>.Array;
  27. this.length = this.buffer.Length;
  28. }
  29. public void Advance (int length)
  30. {
  31. position += length;
  32. }
  33. public byte ReadByte ()
  34. {
  35. return buffer [position++];
  36. }
  37. public sbyte ReadSByte ()
  38. {
  39. return (sbyte) ReadByte ();
  40. }
  41. public byte [] ReadBytes (int length)
  42. {
  43. var bytes = new byte [length];
  44. Buffer.BlockCopy (buffer, position, bytes, 0, length);
  45. position += length;
  46. return bytes;
  47. }
  48. public ushort ReadUInt16 ()
  49. {
  50. ushort value = (ushort) (buffer [position]
  51. | (buffer [position + 1] << 8));
  52. position += 2;
  53. return value;
  54. }
  55. public short ReadInt16 ()
  56. {
  57. return (short) ReadUInt16 ();
  58. }
  59. public uint ReadUInt32 ()
  60. {
  61. uint value = (uint) (buffer [position]
  62. | (buffer [position + 1] << 8)
  63. | (buffer [position + 2] << 16)
  64. | (buffer [position + 3] << 24));
  65. position += 4;
  66. return value;
  67. }
  68. public int ReadInt32 ()
  69. {
  70. return (int) ReadUInt32 ();
  71. }
  72. public ulong ReadUInt64 ()
  73. {
  74. uint low = ReadUInt32 ();
  75. uint high = ReadUInt32 ();
  76. return (((ulong) high) << 32) | low;
  77. }
  78. public long ReadInt64 ()
  79. {
  80. return (long) ReadUInt64 ();
  81. }
  82. public uint ReadCompressedUInt32 ()
  83. {
  84. byte first = ReadByte ();
  85. if ((first & 0x80) == 0)
  86. return first;
  87. if ((first & 0x40) == 0)
  88. return ((uint) (first & ~0x80) << 8)
  89. | ReadByte ();
  90. return ((uint) (first & ~0xc0) << 24)
  91. | (uint) ReadByte () << 16
  92. | (uint) ReadByte () << 8
  93. | ReadByte ();
  94. }
  95. public int ReadCompressedInt32 ()
  96. {
  97. var b = buffer [position];
  98. var u = (int) ReadCompressedUInt32 ();
  99. var v = u >> 1;
  100. if ((u & 1) == 0)
  101. return v;
  102. switch (b & 0xc0)
  103. {
  104. case 0:
  105. case 0x40:
  106. return v - 0x40;
  107. case 0x80:
  108. return v - 0x2000;
  109. default:
  110. return v - 0x10000000;
  111. }
  112. }
  113. public float ReadSingle ()
  114. {
  115. if (!BitConverter.IsLittleEndian) {
  116. var bytes = ReadBytes (4);
  117. Array.Reverse (bytes);
  118. return BitConverter.ToSingle (bytes, 0);
  119. }
  120. float value = BitConverter.ToSingle (buffer, position);
  121. position += 4;
  122. return value;
  123. }
  124. public double ReadDouble ()
  125. {
  126. if (!BitConverter.IsLittleEndian) {
  127. var bytes = ReadBytes (8);
  128. Array.Reverse (bytes);
  129. return BitConverter.ToDouble (bytes, 0);
  130. }
  131. double value = BitConverter.ToDouble (buffer, position);
  132. position += 8;
  133. return value;
  134. }
  135. public void WriteByte (byte value)
  136. {
  137. if (position == buffer.Length)
  138. Grow (1);
  139. buffer [position++] = value;
  140. if (position > length)
  141. length = position;
  142. }
  143. public void WriteSByte (sbyte value)
  144. {
  145. WriteByte ((byte) value);
  146. }
  147. public void WriteUInt16 (ushort value)
  148. {
  149. if (position + 2 > buffer.Length)
  150. Grow (2);
  151. buffer [position++] = (byte) value;
  152. buffer [position++] = (byte) (value >> 8);
  153. if (position > length)
  154. length = position;
  155. }
  156. public void WriteInt16 (short value)
  157. {
  158. WriteUInt16 ((ushort) value);
  159. }
  160. public void WriteUInt32 (uint value)
  161. {
  162. if (position + 4 > buffer.Length)
  163. Grow (4);
  164. buffer [position++] = (byte) value;
  165. buffer [position++] = (byte) (value >> 8);
  166. buffer [position++] = (byte) (value >> 16);
  167. buffer [position++] = (byte) (value >> 24);
  168. if (position > length)
  169. length = position;
  170. }
  171. public void WriteInt32 (int value)
  172. {
  173. WriteUInt32 ((uint) value);
  174. }
  175. public void WriteUInt64 (ulong value)
  176. {
  177. if (position + 8 > buffer.Length)
  178. Grow (8);
  179. buffer [position++] = (byte) value;
  180. buffer [position++] = (byte) (value >> 8);
  181. buffer [position++] = (byte) (value >> 16);
  182. buffer [position++] = (byte) (value >> 24);
  183. buffer [position++] = (byte) (value >> 32);
  184. buffer [position++] = (byte) (value >> 40);
  185. buffer [position++] = (byte) (value >> 48);
  186. buffer [position++] = (byte) (value >> 56);
  187. if (position > length)
  188. length = position;
  189. }
  190. public void WriteInt64 (long value)
  191. {
  192. WriteUInt64 ((ulong) value);
  193. }
  194. public void WriteCompressedUInt32 (uint value)
  195. {
  196. if (value < 0x80)
  197. WriteByte ((byte) value);
  198. else if (value < 0x4000) {
  199. WriteByte ((byte) (0x80 | (value >> 8)));
  200. WriteByte ((byte) (value & 0xff));
  201. } else {
  202. WriteByte ((byte) ((value >> 24) | 0xc0));
  203. WriteByte ((byte) ((value >> 16) & 0xff));
  204. WriteByte ((byte) ((value >> 8) & 0xff));
  205. WriteByte ((byte) (value & 0xff));
  206. }
  207. }
  208. public void WriteCompressedInt32 (int value)
  209. {
  210. if (value >= 0) {
  211. WriteCompressedUInt32 ((uint) (value << 1));
  212. return;
  213. }
  214. if (value > -0x40)
  215. value = 0x40 + value;
  216. else if (value >= -0x2000)
  217. value = 0x2000 + value;
  218. else if (value >= -0x20000000)
  219. value = 0x20000000 + value;
  220. WriteCompressedUInt32 ((uint) ((value << 1) | 1));
  221. }
  222. public void WriteBytes (byte [] bytes)
  223. {
  224. var length = bytes.Length;
  225. if (position + length > buffer.Length)
  226. Grow (length);
  227. Buffer.BlockCopy (bytes, 0, buffer, position, length);
  228. position += length;
  229. if (position > this.length)
  230. this.length = position;
  231. }
  232. public void WriteBytes (int length)
  233. {
  234. if (position + length > buffer.Length)
  235. Grow (length);
  236. position += length;
  237. if (position > this.length)
  238. this.length = position;
  239. }
  240. public void WriteBytes (ByteBuffer buffer)
  241. {
  242. if (position + buffer.length > this.buffer.Length)
  243. Grow (buffer.length);
  244. Buffer.BlockCopy (buffer.buffer, 0, this.buffer, position, buffer.length);
  245. position += buffer.length;
  246. if (position > this.length)
  247. this.length = position;
  248. }
  249. public void WriteSingle (float value)
  250. {
  251. var bytes = BitConverter.GetBytes (value);
  252. if (!BitConverter.IsLittleEndian)
  253. Array.Reverse (bytes);
  254. WriteBytes (bytes);
  255. }
  256. public void WriteDouble (double value)
  257. {
  258. var bytes = BitConverter.GetBytes (value);
  259. if (!BitConverter.IsLittleEndian)
  260. Array.Reverse (bytes);
  261. WriteBytes (bytes);
  262. }
  263. void Grow (int desired)
  264. {
  265. var current = this.buffer;
  266. var current_length = current.Length;
  267. var buffer = new byte [System.Math.Max (current_length + desired, current_length * 2)];
  268. Buffer.BlockCopy (current, 0, buffer, 0, current_length);
  269. this.buffer = buffer;
  270. }
  271. }
  272. }