ByteReader.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Utilities
  6. {
  7. public class ByteReader
  8. {
  9. public static byte ReadByte(byte[] buffer, int offset)
  10. {
  11. return buffer[offset];
  12. }
  13. public static byte ReadByte(byte[] buffer, ref int offset)
  14. {
  15. offset++;
  16. return buffer[offset - 1];
  17. }
  18. public static byte[] ReadBytes(byte[] buffer, int offset, int length)
  19. {
  20. byte[] result = new byte[length];
  21. Array.Copy(buffer, offset, result, 0, length);
  22. return result;
  23. }
  24. public static byte[] ReadBytes(byte[] buffer, ref int offset, int length)
  25. {
  26. offset += length;
  27. return ReadBytes(buffer, offset - length, length);
  28. }
  29. /// <summary>
  30. /// Will return the ANSI string stored in the buffer
  31. /// </summary>
  32. public static string ReadAnsiString(byte[] buffer, int offset, int count)
  33. {
  34. // ASCIIEncoding.ASCII.GetString will convert some values to '?' (byte value of 63)
  35. // Any codepage will do, but the only one that Mono supports is 28591.
  36. return ASCIIEncoding.GetEncoding(28591).GetString(buffer, offset, count);
  37. }
  38. public static string ReadAnsiString(byte[] buffer, ref int offset, int count)
  39. {
  40. offset += count;
  41. return ReadAnsiString(buffer, offset - count, count);
  42. }
  43. public static string ReadUTF16String(byte[] buffer, int offset, int numberOfCharacters)
  44. {
  45. int numberOfBytes = numberOfCharacters * 2;
  46. return Encoding.Unicode.GetString(buffer, offset, numberOfBytes);
  47. }
  48. public static string ReadUTF16String(byte[] buffer, ref int offset, int numberOfCharacters)
  49. {
  50. int numberOfBytes = numberOfCharacters * 2;
  51. offset += numberOfBytes;
  52. return ReadUTF16String(buffer, offset - numberOfBytes, numberOfCharacters);
  53. }
  54. public static string ReadNullTerminatedAnsiString(byte[] buffer, int offset)
  55. {
  56. StringBuilder builder = new StringBuilder();
  57. char c = (char)ByteReader.ReadByte(buffer, offset);
  58. while (c != '\0')
  59. {
  60. builder.Append(c);
  61. offset++;
  62. c = (char)ByteReader.ReadByte(buffer, offset);
  63. }
  64. return builder.ToString();
  65. }
  66. public static string ReadNullTerminatedUTF16String(byte[] buffer, int offset)
  67. {
  68. StringBuilder builder = new StringBuilder();
  69. char c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
  70. while (c != 0)
  71. {
  72. builder.Append(c);
  73. offset += 2;
  74. c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
  75. }
  76. return builder.ToString();
  77. }
  78. public static string ReadNullTerminatedAnsiString(byte[] buffer, ref int offset)
  79. {
  80. string result = ReadNullTerminatedAnsiString(buffer, offset);
  81. offset += result.Length + 1;
  82. return result;
  83. }
  84. public static string ReadNullTerminatedUTF16String(byte[] buffer, ref int offset)
  85. {
  86. string result = ReadNullTerminatedUTF16String(buffer, offset);
  87. offset += result.Length * 2 + 2;
  88. return result;
  89. }
  90. public static byte[] ReadBytes(Stream stream, int count)
  91. {
  92. MemoryStream temp = new MemoryStream();
  93. ByteUtils.CopyStream(stream, temp, count);
  94. return temp.ToArray();
  95. }
  96. /// <summary>
  97. /// Return all bytes from current stream position to the end of the stream
  98. /// </summary>
  99. public static byte[] ReadAllBytes(Stream stream)
  100. {
  101. MemoryStream temp = new MemoryStream();
  102. ByteUtils.CopyStream(stream, temp);
  103. return temp.ToArray();
  104. }
  105. }
  106. }