SMB1Helper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 readonly DateTime UTimeNotSpecified = new DateTime(1970, 1, 1);
  16. public static DateTime ReadFileTime(byte[] buffer, int offset)
  17. {
  18. long span = LittleEndianConverter.ToInt64(buffer, offset);
  19. if (span >= 0)
  20. {
  21. return DateTime.FromFileTimeUtc(span);
  22. }
  23. else
  24. {
  25. // Tick = 100ns
  26. return DateTime.Now.Subtract(TimeSpan.FromTicks(span));
  27. }
  28. }
  29. public static DateTime ReadFileTime(byte[] buffer, ref int offset)
  30. {
  31. offset += 8;
  32. return ReadFileTime(buffer, offset - 8);
  33. }
  34. // UTime - The number of seconds since Jan 1, 1970, 00:00:00
  35. public static DateTime ReadUTime(byte[] buffer, int offset)
  36. {
  37. uint span = LittleEndianConverter.ToUInt32(buffer, offset);
  38. DateTime result = new DateTime(1970, 1, 1);
  39. return result.AddSeconds(span);
  40. }
  41. public static DateTime ReadUTime(byte[] buffer, ref int offset)
  42. {
  43. offset += 4;
  44. return ReadUTime(buffer, offset - 4);
  45. }
  46. // UTime - The number of seconds since Jan 1, 1970, 00:00:00
  47. public static void WriteUTime(byte[] buffer, int offset, DateTime time)
  48. {
  49. TimeSpan timespan = time - new DateTime(1970, 1, 1);
  50. uint span = (uint)timespan.TotalSeconds;
  51. LittleEndianWriter.WriteUInt32(buffer, offset, span);
  52. }
  53. public static void WriteUTime(byte[] buffer, ref int offset, DateTime time)
  54. {
  55. WriteUTime(buffer, offset, time);
  56. offset += 4;
  57. }
  58. /// <summary>
  59. /// SMB_DATE
  60. /// </summary>
  61. public static DateTime ReadSMBDate(byte[] buffer, int offset)
  62. {
  63. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  64. int year = ((value & 0xFE00) >> 9) + 1980;
  65. int month = ((value & 0x01E0) >> 5);
  66. int day = (value & 0x001F);
  67. return new DateTime(year, month, day);
  68. }
  69. public static void WriteSMBDate(byte[] buffer, int offset, DateTime date)
  70. {
  71. int year = date.Year - 1980;
  72. int month = date.Month;
  73. int day = date.Day;
  74. ushort value = (ushort)(year << 9 | month << 5 | day);
  75. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  76. }
  77. /// <summary>
  78. /// SMB_DATE
  79. /// </summary>
  80. public static TimeSpan ReadSMBTime(byte[] buffer, int offset)
  81. {
  82. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  83. int hours = ((value & 0xF800) >> 11);
  84. int minutes = ((value & 0x07E0) >> 5);
  85. int seconds = (value & 0x001F);
  86. return new TimeSpan(hours, minutes, seconds);
  87. }
  88. public static void WriteSMBTime(byte[] buffer, int offset, TimeSpan time)
  89. {
  90. ushort value = (ushort)(time.Hours << 11 | time.Minutes << 5 | time.Seconds);
  91. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  92. }
  93. public static DateTime ReadSMBDateTime(byte[] buffer, int offset)
  94. {
  95. DateTime date = ReadSMBDate(buffer, offset);
  96. TimeSpan time = ReadSMBTime(buffer, offset + 2);
  97. return date.Add(time);
  98. }
  99. public static DateTime ReadSMBDateTime(byte[] buffer, ref int offset)
  100. {
  101. offset += 4;
  102. return ReadSMBDateTime(buffer, offset - 4);
  103. }
  104. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime dateTime)
  105. {
  106. WriteSMBDate(buffer, offset, dateTime.Date);
  107. WriteSMBTime(buffer, offset + 2, dateTime.TimeOfDay);
  108. }
  109. public static void WriteSMBDateTime(byte[] buffer, ref int offset, DateTime dateTime)
  110. {
  111. WriteSMBDateTime(buffer, offset, dateTime);
  112. offset += 4;
  113. }
  114. public static string ReadSMBString(byte[] buffer, int offset, bool isUnicode)
  115. {
  116. if (isUnicode)
  117. {
  118. return ByteReader.ReadNullTerminatedUTF16String(buffer, offset);
  119. }
  120. else
  121. {
  122. return ByteReader.ReadNullTerminatedAnsiString(buffer, offset);
  123. }
  124. }
  125. public static string ReadSMBString(byte[] buffer, ref int offset, bool isUnicode)
  126. {
  127. if (isUnicode)
  128. {
  129. return ByteReader.ReadNullTerminatedUTF16String(buffer, ref offset);
  130. }
  131. else
  132. {
  133. return ByteReader.ReadNullTerminatedAnsiString(buffer, ref offset);
  134. }
  135. }
  136. public static void WriteSMBString(byte[] buffer, int offset, bool isUnicode, string value)
  137. {
  138. if (isUnicode)
  139. {
  140. ByteWriter.WriteNullTerminatedUTF16String(buffer, offset, value);
  141. }
  142. else
  143. {
  144. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset, value);
  145. }
  146. }
  147. public static void WriteSMBString(byte[] buffer, ref int offset, bool isUnicode, string value)
  148. {
  149. if (isUnicode)
  150. {
  151. ByteWriter.WriteNullTerminatedUTF16String(buffer, ref offset, value);
  152. }
  153. else
  154. {
  155. ByteWriter.WriteNullTerminatedAnsiString(buffer, ref offset, value);
  156. }
  157. }
  158. public static string ReadFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, int byteCount)
  159. {
  160. if (isUnicode)
  161. {
  162. return ByteReader.ReadUTF16String(buffer, ref offset, byteCount / 2);
  163. }
  164. else
  165. {
  166. return ByteReader.ReadAnsiString(buffer, ref offset, byteCount);
  167. }
  168. }
  169. public static void WriteFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, string value)
  170. {
  171. if (isUnicode)
  172. {
  173. ByteWriter.WriteUTF16String(buffer, ref offset, value);
  174. }
  175. else
  176. {
  177. ByteWriter.WriteAnsiString(buffer, ref offset, value);
  178. }
  179. }
  180. }
  181. }