ByteBuffer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #if !READ_ONLY
  136. public void WriteByte (byte value)
  137. {
  138. if (position == buffer.Length)
  139. Grow (1);
  140. buffer [position++] = value;
  141. if (position > length)
  142. length = position;
  143. }
  144. public void WriteSByte (sbyte value)
  145. {
  146. WriteByte ((byte) value);
  147. }
  148. public void WriteUInt16 (ushort value)
  149. {
  150. if (position + 2 > buffer.Length)
  151. Grow (2);
  152. buffer [position++] = (byte) value;
  153. buffer [position++] = (byte) (value >> 8);
  154. if (position > length)
  155. length = position;
  156. }
  157. public void WriteInt16 (short value)
  158. {
  159. WriteUInt16 ((ushort) value);
  160. }
  161. public void WriteUInt32 (uint value)
  162. {
  163. if (position + 4 > buffer.Length)
  164. Grow (4);
  165. buffer [position++] = (byte) value;
  166. buffer [position++] = (byte) (value >> 8);
  167. buffer [position++] = (byte) (value >> 16);
  168. buffer [position++] = (byte) (value >> 24);
  169. if (position > length)
  170. length = position;
  171. }
  172. public void WriteInt32 (int value)
  173. {
  174. WriteUInt32 ((uint) value);
  175. }
  176. public void WriteUInt64 (ulong value)
  177. {
  178. if (position + 8 > buffer.Length)
  179. Grow (8);
  180. buffer [position++] = (byte) value;
  181. buffer [position++] = (byte) (value >> 8);
  182. buffer [position++] = (byte) (value >> 16);
  183. buffer [position++] = (byte) (value >> 24);
  184. buffer [position++] = (byte) (value >> 32);
  185. buffer [position++] = (byte) (value >> 40);
  186. buffer [position++] = (byte) (value >> 48);
  187. buffer [position++] = (byte) (value >> 56);
  188. if (position > length)
  189. length = position;
  190. }
  191. public void WriteInt64 (long value)
  192. {
  193. WriteUInt64 ((ulong) value);
  194. }
  195. public void WriteCompressedUInt32 (uint value)
  196. {
  197. if (value < 0x80)
  198. WriteByte ((byte) value);
  199. else if (value < 0x4000) {
  200. WriteByte ((byte) (0x80 | (value >> 8)));
  201. WriteByte ((byte) (value & 0xff));
  202. } else {
  203. WriteByte ((byte) ((value >> 24) | 0xc0));
  204. WriteByte ((byte) ((value >> 16) & 0xff));
  205. WriteByte ((byte) ((value >> 8) & 0xff));
  206. WriteByte ((byte) (value & 0xff));
  207. }
  208. }
  209. public void WriteCompressedInt32 (int value)
  210. {
  211. if (value >= 0) {
  212. WriteCompressedUInt32 ((uint) (value << 1));
  213. return;
  214. }
  215. if (value > -0x40)
  216. value = 0x40 + value;
  217. else if (value >= -0x2000)
  218. value = 0x2000 + value;
  219. else if (value >= -0x20000000)
  220. value = 0x20000000 + value;
  221. WriteCompressedUInt32 ((uint) ((value << 1) | 1));
  222. }
  223. public void WriteBytes (byte [] bytes)
  224. {
  225. var length = bytes.Length;
  226. if (position + length > buffer.Length)
  227. Grow (length);
  228. Buffer.BlockCopy (bytes, 0, buffer, position, length);
  229. position += length;
  230. if (position > this.length)
  231. this.length = position;
  232. }
  233. public void WriteBytes (int length)
  234. {
  235. if (position + length > buffer.Length)
  236. Grow (length);
  237. position += length;
  238. if (position > this.length)
  239. this.length = position;
  240. }
  241. public void WriteBytes (ByteBuffer buffer)
  242. {
  243. if (position + buffer.length > this.buffer.Length)
  244. Grow (buffer.length);
  245. Buffer.BlockCopy (buffer.buffer, 0, this.buffer, position, buffer.length);
  246. position += buffer.length;
  247. if (position > this.length)
  248. this.length = position;
  249. }
  250. public void WriteSingle (float value)
  251. {
  252. var bytes = BitConverter.GetBytes (value);
  253. if (!BitConverter.IsLittleEndian)
  254. Array.Reverse (bytes);
  255. WriteBytes (bytes);
  256. }
  257. public void WriteDouble (double value)
  258. {
  259. var bytes = BitConverter.GetBytes (value);
  260. if (!BitConverter.IsLittleEndian)
  261. Array.Reverse (bytes);
  262. WriteBytes (bytes);
  263. }
  264. void Grow (int desired)
  265. {
  266. var current = this.buffer;
  267. var current_length = current.Length;
  268. var buffer = new byte [System.Math.Max (current_length + desired, current_length * 2)];
  269. Buffer.BlockCopy (current, 0, buffer, 0, current_length);
  270. this.buffer = buffer;
  271. }
  272. #endif
  273. }
  274. }