SMB1Helper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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? ReadNullableFileTime(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 if (span == 0)
  23. {
  24. return null;
  25. }
  26. else
  27. {
  28. // Tick = 100ns
  29. return DateTime.UtcNow.Subtract(TimeSpan.FromTicks(span));
  30. }
  31. }
  32. public static DateTime? ReadNullableFileTime(byte[] buffer, ref int offset)
  33. {
  34. offset += 8;
  35. return ReadNullableFileTime(buffer, offset - 8);
  36. }
  37. /// <summary>
  38. /// SMB_DATE
  39. /// </summary>
  40. public static DateTime ReadSMBDate(byte[] buffer, int offset)
  41. {
  42. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  43. int year = ((value & 0xFE00) >> 9) + 1980;
  44. int month = ((value & 0x01E0) >> 5);
  45. int day = (value & 0x001F);
  46. // SMB_DATE & SMB_TIME are represented in the local time zone of the server
  47. return new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Local);
  48. }
  49. public static void WriteSMBDate(byte[] buffer, int offset, DateTime date)
  50. {
  51. int year = date.Year - 1980;
  52. int month = date.Month;
  53. int day = date.Day;
  54. ushort value = (ushort)(year << 9 | month << 5 | day);
  55. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  56. }
  57. /// <summary>
  58. /// SMB_DATE
  59. /// </summary>
  60. public static TimeSpan ReadSMBTime(byte[] buffer, int offset)
  61. {
  62. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  63. int hours = ((value & 0xF800) >> 11);
  64. int minutes = ((value & 0x07E0) >> 5);
  65. int seconds = (value & 0x001F);
  66. return new TimeSpan(hours, minutes, seconds);
  67. }
  68. public static void WriteSMBTime(byte[] buffer, int offset, TimeSpan time)
  69. {
  70. ushort value = (ushort)(time.Hours << 11 | time.Minutes << 5 | time.Seconds);
  71. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  72. }
  73. public static DateTime ReadSMBDateTime(byte[] buffer, int offset)
  74. {
  75. DateTime date = ReadSMBDate(buffer, offset);
  76. TimeSpan time = ReadSMBTime(buffer, offset + 2);
  77. return date.Add(time);
  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 DateTime? ReadNullableSMBDateTime(byte[] buffer, int offset)
  85. {
  86. uint value = LittleEndianConverter.ToUInt32(buffer, offset);
  87. if (value > 0)
  88. {
  89. return ReadSMBDateTime(buffer, offset);
  90. }
  91. return null;
  92. }
  93. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime? dateTime)
  94. {
  95. if (dateTime.HasValue)
  96. {
  97. WriteSMBDateTime(buffer, offset, dateTime.Value);
  98. }
  99. else
  100. {
  101. LittleEndianWriter.WriteUInt32(buffer, offset, 0);
  102. }
  103. }
  104. public static string ReadSMBString(byte[] buffer, int offset, bool isUnicode)
  105. {
  106. if (isUnicode)
  107. {
  108. return ByteReader.ReadNullTerminatedUTF16String(buffer, offset);
  109. }
  110. else
  111. {
  112. return ByteReader.ReadNullTerminatedAnsiString(buffer, offset);
  113. }
  114. }
  115. public static string ReadSMBString(byte[] buffer, ref int offset, bool isUnicode)
  116. {
  117. if (isUnicode)
  118. {
  119. return ByteReader.ReadNullTerminatedUTF16String(buffer, ref offset);
  120. }
  121. else
  122. {
  123. return ByteReader.ReadNullTerminatedAnsiString(buffer, ref offset);
  124. }
  125. }
  126. public static void WriteSMBString(byte[] buffer, int offset, bool isUnicode, string value)
  127. {
  128. if (isUnicode)
  129. {
  130. ByteWriter.WriteNullTerminatedUTF16String(buffer, offset, value);
  131. }
  132. else
  133. {
  134. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset, value);
  135. }
  136. }
  137. public static void WriteSMBString(byte[] buffer, ref int offset, bool isUnicode, string value)
  138. {
  139. if (isUnicode)
  140. {
  141. ByteWriter.WriteNullTerminatedUTF16String(buffer, ref offset, value);
  142. }
  143. else
  144. {
  145. ByteWriter.WriteNullTerminatedAnsiString(buffer, ref offset, value);
  146. }
  147. }
  148. public static string ReadFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, int byteCount)
  149. {
  150. if (isUnicode)
  151. {
  152. return ByteReader.ReadUTF16String(buffer, ref offset, byteCount / 2);
  153. }
  154. else
  155. {
  156. return ByteReader.ReadAnsiString(buffer, ref offset, byteCount);
  157. }
  158. }
  159. public static void WriteFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, string value)
  160. {
  161. if (isUnicode)
  162. {
  163. ByteWriter.WriteUTF16String(buffer, ref offset, value);
  164. }
  165. else
  166. {
  167. ByteWriter.WriteAnsiString(buffer, ref offset, value);
  168. }
  169. }
  170. }
  171. }