SMB1Helper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Now.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. return new DateTime(year, month, day);
  47. }
  48. public static void WriteSMBDate(byte[] buffer, int offset, DateTime date)
  49. {
  50. int year = date.Year - 1980;
  51. int month = date.Month;
  52. int day = date.Day;
  53. ushort value = (ushort)(year << 9 | month << 5 | day);
  54. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  55. }
  56. /// <summary>
  57. /// SMB_DATE
  58. /// </summary>
  59. public static TimeSpan ReadSMBTime(byte[] buffer, int offset)
  60. {
  61. ushort value = LittleEndianConverter.ToUInt16(buffer, offset);
  62. int hours = ((value & 0xF800) >> 11);
  63. int minutes = ((value & 0x07E0) >> 5);
  64. int seconds = (value & 0x001F);
  65. return new TimeSpan(hours, minutes, seconds);
  66. }
  67. public static void WriteSMBTime(byte[] buffer, int offset, TimeSpan time)
  68. {
  69. ushort value = (ushort)(time.Hours << 11 | time.Minutes << 5 | time.Seconds);
  70. LittleEndianWriter.WriteUInt16(buffer, offset, value);
  71. }
  72. public static DateTime ReadSMBDateTime(byte[] buffer, int offset)
  73. {
  74. DateTime date = ReadSMBDate(buffer, offset);
  75. TimeSpan time = ReadSMBTime(buffer, offset + 2);
  76. return date.Add(time);
  77. }
  78. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime dateTime)
  79. {
  80. WriteSMBDate(buffer, offset, dateTime.Date);
  81. WriteSMBTime(buffer, offset + 2, dateTime.TimeOfDay);
  82. }
  83. public static DateTime? ReadNullableSMBDateTime(byte[] buffer, int offset)
  84. {
  85. uint value = LittleEndianConverter.ToUInt32(buffer, offset);
  86. if (value > 0)
  87. {
  88. return ReadSMBDateTime(buffer, offset);
  89. }
  90. return null;
  91. }
  92. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime? dateTime)
  93. {
  94. if (dateTime.HasValue)
  95. {
  96. WriteSMBDateTime(buffer, offset, dateTime.Value);
  97. }
  98. else
  99. {
  100. LittleEndianWriter.WriteUInt32(buffer, offset, 0);
  101. }
  102. }
  103. public static string ReadSMBString(byte[] buffer, int offset, bool isUnicode)
  104. {
  105. if (isUnicode)
  106. {
  107. return ByteReader.ReadNullTerminatedUTF16String(buffer, offset);
  108. }
  109. else
  110. {
  111. return ByteReader.ReadNullTerminatedAnsiString(buffer, offset);
  112. }
  113. }
  114. public static string ReadSMBString(byte[] buffer, ref int offset, bool isUnicode)
  115. {
  116. if (isUnicode)
  117. {
  118. return ByteReader.ReadNullTerminatedUTF16String(buffer, ref offset);
  119. }
  120. else
  121. {
  122. return ByteReader.ReadNullTerminatedAnsiString(buffer, ref offset);
  123. }
  124. }
  125. public static void WriteSMBString(byte[] buffer, int offset, bool isUnicode, string value)
  126. {
  127. if (isUnicode)
  128. {
  129. ByteWriter.WriteNullTerminatedUTF16String(buffer, offset, value);
  130. }
  131. else
  132. {
  133. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset, value);
  134. }
  135. }
  136. public static void WriteSMBString(byte[] buffer, ref int offset, bool isUnicode, string value)
  137. {
  138. if (isUnicode)
  139. {
  140. ByteWriter.WriteNullTerminatedUTF16String(buffer, ref offset, value);
  141. }
  142. else
  143. {
  144. ByteWriter.WriteNullTerminatedAnsiString(buffer, ref offset, value);
  145. }
  146. }
  147. public static string ReadFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, int byteCount)
  148. {
  149. if (isUnicode)
  150. {
  151. return ByteReader.ReadUTF16String(buffer, ref offset, byteCount / 2);
  152. }
  153. else
  154. {
  155. return ByteReader.ReadAnsiString(buffer, ref offset, byteCount);
  156. }
  157. }
  158. public static void WriteFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, string value)
  159. {
  160. if (isUnicode)
  161. {
  162. ByteWriter.WriteUTF16String(buffer, ref offset, value);
  163. }
  164. else
  165. {
  166. ByteWriter.WriteAnsiString(buffer, ref offset, value);
  167. }
  168. }
  169. }
  170. }