DMBuffer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using NPOI.SS.Formula.Functions;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.Immutable;
  6. using System.Linq;
  7. using System.Text;
  8. namespace InABox.DigitalMatter
  9. {
  10. public interface IDMBuffer
  11. {
  12. /// <summary>
  13. /// The remaining size of the buffer.
  14. /// </summary>
  15. public ushort BufferSize { get; }
  16. #region Bit Manipulation
  17. private static bool[] DecodeBits(byte[] value, int index, int length)
  18. {
  19. var result = new List<bool>();
  20. var array = new BitArray(value);
  21. for (var i = index; i < index + length && i < array.Count; i++)
  22. result.Add(array[i]);
  23. return result.ToArray();
  24. }
  25. private static byte[] EncodeBits(byte[] source, int index, bool[] bits)
  26. {
  27. var array = new BitArray(source);
  28. for (var i = 0; i < bits.Length; i++)
  29. array.Set(index + i, bits[i]);
  30. var result = new byte[source.Length];
  31. array.CopyTo(result, 0);
  32. return result;
  33. }
  34. public static bool[] GetBits(byte[] value, int index, int length)
  35. {
  36. return DecodeBits(value, index, length);
  37. }
  38. public static byte[] SetBits(byte[] value, int index, bool[] bits)
  39. {
  40. return EncodeBits(value, index, bits);
  41. }
  42. public static bool[] DecodeByte(byte value, int index, int length)
  43. {
  44. return DecodeBits(new[] { value }, index, length);
  45. }
  46. public static bool[] DecodeBytes(byte[] value, int index, int length)
  47. {
  48. return DecodeBits(value, index, length);
  49. }
  50. public static bool[] DecodeInt8(sbyte value, int index, int length)
  51. {
  52. return DecodeBits(new[] { Convert.ToByte(value) }, index, length);
  53. }
  54. public static bool[] DecodeInt16(short value, int index, int length)
  55. {
  56. return DecodeBits(BitConverter.GetBytes(value), index, length);
  57. }
  58. public static bool[] DecodeUInt16(ushort value, int index, int length)
  59. {
  60. return DecodeBits(BitConverter.GetBytes(value), index, length);
  61. }
  62. public static bool[] DecodeInt32(int value, int index, int length)
  63. {
  64. return DecodeBits(BitConverter.GetBytes(value), index, length);
  65. }
  66. public static bool[] DecodeUInt32(uint value, int index, int length)
  67. {
  68. return DecodeBits(BitConverter.GetBytes(value), index, length);
  69. }
  70. public static byte EncodeByte(bool[] bits)
  71. {
  72. return EncodeBits(new byte[] { default }, 0, bits).First();
  73. }
  74. public static byte[] EncodeBytes(bool[] bits, int size)
  75. {
  76. return EncodeBits(Array.CreateInstance(typeof(byte), size).OfType<byte>().ToArray(), 0, bits);
  77. }
  78. public static sbyte EncodeInt8(bool[] bits)
  79. {
  80. return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(default(sbyte)) }, 0, bits).First());
  81. }
  82. public static short EncodeInt16(bool[] bits)
  83. {
  84. return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(default(short)), 0, bits), 0);
  85. }
  86. public static ushort EncodeUInt16(bool[] bits)
  87. {
  88. return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(default(ushort)), 0, bits), 0);
  89. }
  90. public static int EncodeInt32(bool[] bits)
  91. {
  92. return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(default(int)), 0, bits), 0);
  93. }
  94. public static uint EncodeUInt32(bool[] bits)
  95. {
  96. return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(default(uint)), 0, bits), 0);
  97. }
  98. public static byte UpdateByte(byte value, int index, bool[] bits)
  99. {
  100. return EncodeBits(new[] { value }, index, bits).First();
  101. }
  102. public static byte[] UpdateBytes(byte[] value, int index, bool[] bits, int size)
  103. {
  104. return EncodeBits(value, 0, bits);
  105. }
  106. public static sbyte UpdateInt8(sbyte value, int index, bool[] bits)
  107. {
  108. return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(value) }, 0, bits).First());
  109. }
  110. public static short UpdateInt16(short value, int index, bool[] bits)
  111. {
  112. return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  113. }
  114. public static ushort UpdateUInt16(ushort value, int index, bool[] bits)
  115. {
  116. return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  117. }
  118. public static int UpdateInt32(int value, int index, bool[] bits)
  119. {
  120. return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  121. }
  122. public static uint UpdateUInt32(uint value, int index, bool[] bits)
  123. {
  124. return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
  125. }
  126. #endregion
  127. }
  128. public interface IDMReadBuffer : IDMBuffer
  129. {
  130. /// <summary>
  131. /// Read <paramref name="length"/> bytes, starting at <paramref name="index"/>, but don't advance the read pointer.
  132. /// </summary>
  133. /// <param name="index">Index to start the read.</param>
  134. /// <param name="length">Number of bytes to read.</param>
  135. /// <returns>The bytes read.</returns>
  136. protected byte[] Peek(int index, int length);
  137. /// <summary>
  138. /// Read <paramref name="length"/> bytes, advancing the read pointer by <paramref name="length"/>.
  139. /// </summary>
  140. /// <param name="length">Number of bytes to read.</param>
  141. /// <returns>The bytes read.</returns>
  142. protected byte[] Take(int length);
  143. public byte PeekByte(int index)
  144. {
  145. return Peek(index, sizeof(byte)).First();
  146. }
  147. public byte[] PeekBytes(int index, int length)
  148. {
  149. return Peek(index, length);
  150. }
  151. public sbyte PeekInt8(int index)
  152. {
  153. return unchecked((sbyte)PeekByte(index));
  154. //hmmm...
  155. }
  156. public ushort PeekUInt16(int index)
  157. {
  158. return BitConverter.ToUInt16(Peek(index, sizeof(ushort)), 0);
  159. }
  160. public short PeekInt16(int index)
  161. {
  162. return BitConverter.ToInt16(Peek(index, sizeof(short)), 0);
  163. }
  164. public uint PeekUInt32(int index)
  165. {
  166. return BitConverter.ToUInt32(Peek(index, sizeof(uint)), 0);
  167. }
  168. public int PeekInt32(int index)
  169. {
  170. return BitConverter.ToInt32(Peek(index, sizeof(int)), 0);
  171. }
  172. public string PeekString(int index, int length)
  173. {
  174. return Encoding.Default.GetString(Peek(index, length)).Trim();
  175. }
  176. public byte TakeByte()
  177. {
  178. return Take(sizeof(byte)).First();
  179. }
  180. public byte[] TakeBytes(int length)
  181. {
  182. return Take(length);
  183. }
  184. public sbyte TakeInt8()
  185. {
  186. return unchecked((sbyte)TakeByte());
  187. //hmmm...
  188. }
  189. public ushort TakeUInt16()
  190. {
  191. return BitConverter.ToUInt16(Take(sizeof(ushort)), 0);
  192. }
  193. public short TakeInt16()
  194. {
  195. return BitConverter.ToInt16(Take(sizeof(short)), 0);
  196. }
  197. public uint TakeUInt32()
  198. {
  199. return BitConverter.ToUInt32(Take(sizeof(uint)), 0);
  200. }
  201. public int TakeInt32()
  202. {
  203. return BitConverter.ToInt32(Take(sizeof(int)), 0);
  204. }
  205. public string TakeString(int length)
  206. {
  207. return Encoding.Default.GetString(Take(length)).Trim();
  208. }
  209. }
  210. public interface IDMWriteBuffer : IDMBuffer
  211. {
  212. protected ushort Add(byte[] bytes);
  213. protected ushort Insert(int index, byte[] bytes);
  214. void Reset();
  215. public ushort AddByte(byte value)
  216. {
  217. return Add(new[] { value });
  218. }
  219. public ushort AddBytes(byte[] value)
  220. {
  221. return Add(value);
  222. }
  223. public ushort AddInt8(sbyte value)
  224. {
  225. return Add(BitConverter.GetBytes((short)value));
  226. }
  227. public ushort AddUInt16(ushort value)
  228. {
  229. return Add(BitConverter.GetBytes(value));
  230. }
  231. public ushort AddInt16(short value)
  232. {
  233. return Add(BitConverter.GetBytes(value));
  234. }
  235. public ushort AddUInt32(uint value)
  236. {
  237. return Add(BitConverter.GetBytes(value));
  238. }
  239. public ushort AddInt32(int value)
  240. {
  241. return Add(BitConverter.GetBytes(value));
  242. }
  243. public ushort AddString(string value, int length)
  244. {
  245. return Add(FixedLengthString(value, length));
  246. }
  247. public ushort InsertByte(int index, byte value)
  248. {
  249. return Insert(index, new[] { value });
  250. }
  251. public ushort InsertBytes(int index, byte[] value)
  252. {
  253. return Insert(index, value);
  254. }
  255. public ushort InsertInt8(int index, sbyte value)
  256. {
  257. return Insert(index, BitConverter.GetBytes((short)value));
  258. }
  259. public ushort InsertUInt16(int index, ushort value)
  260. {
  261. return Insert(index, BitConverter.GetBytes(value));
  262. }
  263. public ushort InsertInt16(int index, short value)
  264. {
  265. return Insert(index, BitConverter.GetBytes(value));
  266. }
  267. public ushort InsertUInt32(int index, uint value)
  268. {
  269. return Insert(index, BitConverter.GetBytes(value));
  270. }
  271. public ushort InsertInt32(int index, int value)
  272. {
  273. return Insert(index, BitConverter.GetBytes(value));
  274. }
  275. public ushort InsertString(int index, string value, int length)
  276. {
  277. return Insert(index, FixedLengthString(value, length));
  278. }
  279. #region Private Members
  280. private static byte[] FixedLengthString(string value, int length)
  281. {
  282. var result = Encoding.Default.GetBytes(value).ToList();
  283. while (result.Count < length)
  284. result.Add(0x00);
  285. return result.ToArray();
  286. }
  287. #endregion
  288. }
  289. public class DMArrayReadBuffer : IDMReadBuffer
  290. {
  291. private byte[] _buffer { get; set; }
  292. private int _offset;
  293. public ushort BufferSize => (ushort)(_buffer.Length - _offset);
  294. public DMArrayReadBuffer(byte[] buffer)
  295. {
  296. _buffer = buffer;
  297. _offset = 0;
  298. }
  299. byte[] IDMReadBuffer.Peek(int index, int length)
  300. {
  301. index += _offset;
  302. length = Math.Min(length, _buffer.Length - index);
  303. var newBytes = new byte[length];
  304. Buffer.BlockCopy(_buffer, index, newBytes, 0, length);
  305. return newBytes;
  306. }
  307. byte[] IDMReadBuffer.Take(int length)
  308. {
  309. length = Math.Min(length, _buffer.Length - _offset);
  310. var newBytes = new byte[length];
  311. Buffer.BlockCopy(_buffer, _offset, newBytes, 0, length);
  312. _offset += length;
  313. return newBytes;
  314. }
  315. }
  316. public class DMListReadBuffer : IDMReadBuffer
  317. {
  318. private List<byte> _buffer { get; set; }
  319. private int _offset;
  320. public ushort BufferSize => (ushort)(_buffer.Count - _offset);
  321. public DMListReadBuffer(List<byte> buffer)
  322. {
  323. _buffer = buffer;
  324. _offset = 0;
  325. }
  326. byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
  327. private byte[] Peek(int index, int length)
  328. {
  329. index += _offset;
  330. length = Math.Min(length, _buffer.Count - index);
  331. var newBytes = new byte[length];
  332. _buffer.CopyTo(index, newBytes, 0, length);
  333. return newBytes;
  334. }
  335. byte[] IDMReadBuffer.Take(int length)
  336. {
  337. var result = Peek(0, length);
  338. _offset += length;
  339. return result;
  340. }
  341. }
  342. public class DMPartialReadBuffer : IDMReadBuffer
  343. {
  344. private ushort _offset;
  345. private ushort _length;
  346. public IDMReadBuffer Buffer { get; }
  347. public ushort BufferSize => (ushort)(_length - (Offset - _offset));
  348. public ushort Offset { get; private set; }
  349. public DMPartialReadBuffer(IDMReadBuffer buffer, ushort offset, ushort length)
  350. {
  351. _length = length;
  352. _offset = offset;
  353. Offset = offset;
  354. Buffer = buffer;
  355. }
  356. byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
  357. private byte[] Peek(int index, int length)
  358. {
  359. index += Offset;
  360. length = Math.Min(length, _offset + _length - index);
  361. return Buffer.PeekBytes(index, length);
  362. }
  363. byte[] IDMReadBuffer.Take(int length)
  364. {
  365. var bytes = Peek(0, length);
  366. Offset += (ushort)bytes.Length;
  367. return bytes;
  368. }
  369. }
  370. public class DMListWriteBuffer : IDMWriteBuffer
  371. {
  372. private List<byte> _buffer { get; set; }
  373. private int _offset;
  374. public List<byte> Buffer => _buffer;
  375. public ushort BufferSize => (ushort)(_buffer.Count - _offset);
  376. public DMListWriteBuffer(List<byte> buffer)
  377. {
  378. _buffer = buffer;
  379. _offset = 0;
  380. }
  381. public void Reset()
  382. {
  383. _buffer.Clear();
  384. }
  385. ushort IDMWriteBuffer.Add(byte[] bytes)
  386. {
  387. _buffer.AddRange(bytes);
  388. return BufferSize;
  389. }
  390. ushort IDMWriteBuffer.Insert(int index, byte[] bytes)
  391. {
  392. _buffer.InsertRange(index, bytes);
  393. return BufferSize;
  394. }
  395. }
  396. public class DMBuffer : IDMReadBuffer, IDMWriteBuffer
  397. {
  398. public byte[] Buffer
  399. {
  400. get => _buffer.ToArray();
  401. set => _buffer = value.ToList();
  402. }
  403. public DMBuffer() { }
  404. public DMBuffer(byte[] buffer)
  405. {
  406. Buffer = buffer;
  407. }
  408. public DMBuffer(List<byte> buffer)
  409. {
  410. _buffer = buffer;
  411. }
  412. public ushort BufferSize => (ushort)_buffer.Count;
  413. public void Reset()
  414. {
  415. _buffer = new List<byte>();
  416. }
  417. #region Private Members
  418. private List<byte> _buffer = new();
  419. ushort IDMWriteBuffer.Add(byte[] bytes)
  420. {
  421. _buffer.AddRange(bytes);
  422. return BufferSize;
  423. }
  424. ushort IDMWriteBuffer.Insert(int index, byte[] bytes)
  425. {
  426. _buffer.InsertRange(index, bytes);
  427. return BufferSize;
  428. }
  429. private byte[] Peek(int index, int length)
  430. {
  431. return _buffer.Skip(index).Take(length).ToArray();
  432. }
  433. byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
  434. byte[] IDMReadBuffer.Take(int length)
  435. {
  436. var result = Peek(0, length);
  437. _buffer.RemoveRange(0, length);
  438. return result;
  439. }
  440. #endregion
  441. }
  442. }