DMBuffer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace InABox.DigitalMatter
  7. {
  8. public class DMBuffer
  9. {
  10. public byte[] Buffer
  11. {
  12. get => _buffer.ToArray();
  13. set => _buffer = value.ToList();
  14. }
  15. public ushort BufferSize => (ushort)_buffer.Count;
  16. public virtual void Reset()
  17. {
  18. _buffer = new List<byte>();
  19. }
  20. public ushort AddByte(byte value)
  21. {
  22. return Add(new[] { value });
  23. }
  24. public ushort AddBytes(byte[] value)
  25. {
  26. return Add(value);
  27. }
  28. public ushort AddInt8(sbyte value)
  29. {
  30. return Add(BitConverter.GetBytes(value));
  31. }
  32. public ushort AddUInt16(ushort value)
  33. {
  34. return Add(BitConverter.GetBytes(value));
  35. }
  36. public ushort AddInt16(short value)
  37. {
  38. return Add(BitConverter.GetBytes(value));
  39. }
  40. public ushort AddUInt32(uint value)
  41. {
  42. return Add(BitConverter.GetBytes(value));
  43. }
  44. public ushort AddInt32(int value)
  45. {
  46. return Add(BitConverter.GetBytes(value));
  47. }
  48. public ushort AddString(string value, int length)
  49. {
  50. return Add(FixedLengthString(value, length));
  51. }
  52. public ushort InsertByte(int index, byte value)
  53. {
  54. return Insert(index, new[] { value });
  55. }
  56. public ushort InsertBytes(int index, byte[] value)
  57. {
  58. return Insert(index, value);
  59. }
  60. public ushort InsertInt8(int index, sbyte value)
  61. {
  62. return Insert(index, BitConverter.GetBytes(value));
  63. }
  64. public ushort InsertUInt16(int index, ushort value)
  65. {
  66. return Insert(index, BitConverter.GetBytes(value));
  67. }
  68. public ushort InsertInt16(int index, short value)
  69. {
  70. return Insert(index, BitConverter.GetBytes(value));
  71. }
  72. public ushort InsertUInt32(int index, uint value)
  73. {
  74. return Insert(index, BitConverter.GetBytes(value));
  75. }
  76. public ushort InsertInt32(int index, int value)
  77. {
  78. return Insert(index, BitConverter.GetBytes(value));
  79. }
  80. public ushort InsertString(int index, string value, int length)
  81. {
  82. return Insert(index, FixedLengthString(value, length));
  83. }
  84. public byte PeekByte(int index)
  85. {
  86. return Peek(index, sizeof(byte)).First();
  87. }
  88. public byte[] PeekBytes(int index, int length)
  89. {
  90. return Peek(index, length);
  91. }
  92. public sbyte PeekInt8(int index)
  93. {
  94. return unchecked((sbyte)PeekByte(index));
  95. //hmmm...
  96. }
  97. public ushort PeekUInt16(int index)
  98. {
  99. return BitConverter.ToUInt16(Peek(index, sizeof(ushort)), 0);
  100. }
  101. public short PeekInt16(int index)
  102. {
  103. return BitConverter.ToInt16(Peek(index, sizeof(short)), 0);
  104. }
  105. public uint PeekUInt32(int index)
  106. {
  107. return BitConverter.ToUInt32(Peek(index, sizeof(uint)), 0);
  108. }
  109. public int PeekInt32(int index)
  110. {
  111. return BitConverter.ToInt32(Peek(index, sizeof(int)), 0);
  112. }
  113. public string PeekString(int index, int length)
  114. {
  115. return Encoding.Default.GetString(Peek(index, length)).Trim();
  116. }
  117. public byte TakeByte(int index)
  118. {
  119. return Take(index, sizeof(byte)).First();
  120. }
  121. public byte[] TakeBytes(int index, int length)
  122. {
  123. return Take(index, length);
  124. }
  125. public sbyte TakeInt8(int index)
  126. {
  127. return unchecked((sbyte)TakeByte(index));
  128. //hmmm...
  129. }
  130. public ushort TakeUInt16(int index)
  131. {
  132. return BitConverter.ToUInt16(Take(index, sizeof(ushort)), 0);
  133. }
  134. public short TakeInt16(int index)
  135. {
  136. return BitConverter.ToInt16(Take(index, sizeof(short)), 0);
  137. }
  138. public uint TakeUInt32(int index)
  139. {
  140. return BitConverter.ToUInt32(Take(index, sizeof(uint)), 0);
  141. }
  142. public int TakeInt32(int index)
  143. {
  144. return BitConverter.ToInt32(Take(index, sizeof(int)), 0);
  145. }
  146. public string TakeString(int index, int length)
  147. {
  148. return Encoding.Default.GetString(Take(index, length)).Trim();
  149. }
  150. public byte TakeByte()
  151. {
  152. return TakeByte(0);
  153. }
  154. public byte[] TakeBytes(int length)
  155. {
  156. return TakeBytes(0, length);
  157. }
  158. public sbyte TakeInt8()
  159. {
  160. return TakeInt8(0);
  161. //hmmm...
  162. }
  163. public ushort TakeUInt16()
  164. {
  165. return TakeUInt16(0);
  166. }
  167. public short TakeInt16()
  168. {
  169. return TakeInt16(0);
  170. }
  171. public uint TakeUInt32()
  172. {
  173. return TakeUInt32(0);
  174. }
  175. public int TakeInt32()
  176. {
  177. return TakeInt32(0);
  178. }
  179. public string TakeString(int length)
  180. {
  181. return TakeString(0, length);
  182. }
  183. #region Private Members
  184. private List<byte> _buffer = new();
  185. private ushort Add(byte[] bytes)
  186. {
  187. _buffer.AddRange(bytes);
  188. return BufferSize;
  189. }
  190. private ushort Insert(int index, byte[] bytes)
  191. {
  192. _buffer.InsertRange(index, bytes);
  193. return BufferSize;
  194. }
  195. private byte[] Peek(int index, int length)
  196. {
  197. return _buffer.Skip(index).Take(length).ToArray();
  198. }
  199. private byte[] Take(int index, int length)
  200. {
  201. var result = Peek(index, length);
  202. _buffer.RemoveRange(index, length);
  203. return result;
  204. }
  205. private byte[] FixedLengthString(string value, int length)
  206. {
  207. var result = Encoding.Default.GetBytes(value).ToList();
  208. while (result.Count < length)
  209. result.Add(0x00);
  210. return result.ToArray();
  211. }
  212. private bool[] DecodeBits(byte[] value, int index, int length)
  213. {
  214. var result = new List<bool>();
  215. var array = new BitArray(value);
  216. for (var i = index; i < index + length && i < array.Count; i++)
  217. result.Add(array[i]);
  218. return result.ToArray();
  219. }
  220. private byte[] EncodeBits(byte[] source, int index, bool[] bits)
  221. {
  222. var array = new BitArray(source);
  223. for (var i = 0; i < bits.Length; i++)
  224. array.Set(index + i, bits[i]);
  225. var result = new byte[source.Length];
  226. array.CopyTo(result, 0);
  227. return result;
  228. }
  229. #endregion
  230. #region Bit Manipulation
  231. public bool[] GetBits(byte[] value, int index, int length)
  232. {
  233. return DecodeBits(value, index, length);
  234. }
  235. public byte[] SetBits(byte[] value, int index, bool[] bits)
  236. {
  237. return EncodeBits(value, index, bits);
  238. }
  239. public bool[] DecodeByte(byte value, int index, int length)
  240. {
  241. return DecodeBits(new[] { value }, index, length);
  242. }
  243. public bool[] DecodeBytes(byte[] value, int index, int length)
  244. {
  245. return DecodeBits(value, index, length);
  246. }
  247. public bool[] DecodeInt8(sbyte value, int index, int length)
  248. {
  249. return DecodeBits(new[] { Convert.ToByte(value) }, index, length);
  250. }
  251. public bool[] DecodeInt16(short value, int index, int length)
  252. {
  253. return DecodeBits(BitConverter.GetBytes(value), index, length);
  254. }
  255. public bool[] DecodeUInt16(ushort value, int index, int length)
  256. {
  257. return DecodeBits(BitConverter.GetBytes(value), index, length);
  258. }
  259. public bool[] DecodeInt32(int value, int index, int length)
  260. {
  261. return DecodeBits(BitConverter.GetBytes(value), index, length);
  262. }
  263. public bool[] DecodeUInt32(uint value, int index, int length)
  264. {
  265. return DecodeBits(BitConverter.GetBytes(value), index, length);
  266. }
  267. public byte EncodeByte(bool[] bits)
  268. {
  269. return EncodeBits(new byte[] { default }, 0, bits).First();
  270. }
  271. public byte[] EncodeBytes(bool[] bits, int size)
  272. {
  273. return EncodeBits(Array.CreateInstance(typeof(byte), size).OfType<byte>().ToArray(), 0, bits);
  274. }
  275. public sbyte EncodeInt8(bool[] bits)
  276. {
  277. return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(default(sbyte)) }, 0, bits).First());
  278. }
  279. public short EncodeInt16(bool[] bits)
  280. {
  281. return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(default(short)), 0, bits), 0);
  282. }
  283. public ushort EncodeUInt16(bool[] bits)
  284. {
  285. return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(default(ushort)), 0, bits), 0);
  286. }
  287. public int EncodeInt32(bool[] bits)
  288. {
  289. return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(default(int)), 0, bits), 0);
  290. }
  291. public uint EncodeUInt32(bool[] bits)
  292. {
  293. return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(default(uint)), 0, bits), 0);
  294. }
  295. public byte UpdateByte(byte value, int index, bool[] bits)
  296. {
  297. return EncodeBits(new[] { value }, index, bits).First();
  298. }
  299. public byte[] UpdateBytes(byte[] value, int index, bool[] bits, int size)
  300. {
  301. return EncodeBits(value, 0, bits);
  302. }
  303. public sbyte UpdateInt8(sbyte value, int index, bool[] bits)
  304. {
  305. return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(value) }, 0, bits).First());
  306. }
  307. public short UpdateInt16(short value, int index, bool[] bits)
  308. {
  309. return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  310. }
  311. public ushort UpdateUInt16(ushort value, int index, bool[] bits)
  312. {
  313. return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  314. }
  315. public int UpdateInt32(int value, int index, bool[] bits)
  316. {
  317. return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  318. }
  319. public uint UpdateUInt32(uint value, int index, bool[] bits)
  320. {
  321. return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  322. }
  323. #endregion
  324. }
  325. }