SMB1Helper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. public class SMB1Helper
  14. {
  15. public static DateTime ReadFileTime(byte[] buffer, int offset)
  16. {
  17. long span = LittleEndianConverter.ToInt64(buffer, offset);
  18. if (span >= 0)
  19. {
  20. return DateTime.FromFileTimeUtc(span);
  21. }
  22. else
  23. {
  24. // Tick = 100ns
  25. return DateTime.Now.Subtract(TimeSpan.FromTicks(span));
  26. }
  27. }
  28. public static DateTime ReadFileTime(byte[] buffer, ref int offset)
  29. {
  30. offset += 8;
  31. return ReadFileTime(buffer, offset - 8);
  32. }
  33. /// <summary>
  34. /// SMB_DATE
  35. /// </summary>
  36. public static DateTime ReadSMBDate(byte[] buffer, int offset)
  37. {
  38. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  39. int year = ((value & 0xFE00) >> 9) + 1980;
  40. int month = ((value & 0x01E0) >> 5);
  41. int day = (value & 0x001F);
  42. return new DateTime(year, month, day);
  43. }
  44. public static void WriteSMBDate(byte[] buffer, int offset, DateTime date)
  45. {
  46. int year = date.Year - 1980;
  47. int month = date.Month;
  48. int day = date.Day;
  49. ushort value = (ushort)(year << 9 | month << 5 | day);
  50. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  51. }
  52. /// <summary>
  53. /// SMB_DATE
  54. /// </summary>
  55. public static TimeSpan ReadSMBTime(byte[] buffer, int offset)
  56. {
  57. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  58. int hours = ((value & 0xF800) >> 11);
  59. int minutes = ((value & 0x07E0) >> 5);
  60. int seconds = (value & 0x001F);
  61. return new TimeSpan(hours, minutes, seconds);
  62. }
  63. public static void WriteSMBTime(byte[] buffer, int offset, TimeSpan time)
  64. {
  65. ushort value = (ushort)(time.Hours << 11 | time.Minutes << 5 | time.Seconds);
  66. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  67. }
  68. public static DateTime ReadSMBDateTime(byte[] buffer, int offset)
  69. {
  70. DateTime date = ReadSMBDate(buffer, offset);
  71. TimeSpan time = ReadSMBTime(buffer, offset + 2);
  72. return date.Add(time);
  73. }
  74. public static DateTime ReadSMBDateTime(byte[] buffer, ref int offset)
  75. {
  76. offset += 4;
  77. return ReadSMBDateTime(buffer, offset - 4);
  78. }
  79. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime dateTime)
  80. {
  81. WriteSMBDate(buffer, offset, dateTime.Date);
  82. WriteSMBTime(buffer, offset + 2, dateTime.TimeOfDay);
  83. }
  84. public static void WriteSMBDateTime(byte[] buffer, ref int offset, DateTime dateTime)
  85. {
  86. WriteSMBDateTime(buffer, offset, dateTime);
  87. offset += 4;
  88. }
  89. public static string ReadSMBString(byte[] buffer, int offset, bool isUnicode)
  90. {
  91. if (isUnicode)
  92. {
  93. return ByteReader.ReadNullTerminatedUTF16String(buffer, offset);
  94. }
  95. else
  96. {
  97. return ByteReader.ReadNullTerminatedAnsiString(buffer, offset);
  98. }
  99. }
  100. public static string ReadSMBString(byte[] buffer, ref int offset, bool isUnicode)
  101. {
  102. if (isUnicode)
  103. {
  104. return ByteReader.ReadNullTerminatedUTF16String(buffer, ref offset);
  105. }
  106. else
  107. {
  108. return ByteReader.ReadNullTerminatedAnsiString(buffer, ref offset);
  109. }
  110. }
  111. public static void WriteSMBString(byte[] buffer, int offset, bool isUnicode, string value)
  112. {
  113. if (isUnicode)
  114. {
  115. ByteWriter.WriteNullTerminatedUTF16String(buffer, offset, value);
  116. }
  117. else
  118. {
  119. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset, value);
  120. }
  121. }
  122. public static void WriteSMBString(byte[] buffer, ref int offset, bool isUnicode, string value)
  123. {
  124. if (isUnicode)
  125. {
  126. ByteWriter.WriteNullTerminatedUTF16String(buffer, ref offset, value);
  127. }
  128. else
  129. {
  130. ByteWriter.WriteNullTerminatedAnsiString(buffer, ref offset, value);
  131. }
  132. }
  133. public static string ReadFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, int byteCount)
  134. {
  135. if (isUnicode)
  136. {
  137. return ByteReader.ReadUTF16String(buffer, ref offset, byteCount / 2);
  138. }
  139. else
  140. {
  141. return ByteReader.ReadAnsiString(buffer, ref offset, byteCount);
  142. }
  143. }
  144. public static void WriteFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, string value)
  145. {
  146. if (isUnicode)
  147. {
  148. ByteWriter.WriteUTF16String(buffer, ref offset, value);
  149. }
  150. else
  151. {
  152. ByteWriter.WriteAnsiString(buffer, ref offset, value);
  153. }
  154. }
  155. }
  156. }