SMB1Helper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 DateTime ReadSMBDateTime(byte[] buffer, ref int offset)
  79. {
  80. offset += 4;
  81. return ReadSMBDateTime(buffer, offset - 4);
  82. }
  83. public static void WriteSMBDateTime(byte[] buffer, int offset, DateTime dateTime)
  84. {
  85. WriteSMBDate(buffer, offset, dateTime.Date);
  86. WriteSMBTime(buffer, offset + 2, dateTime.TimeOfDay);
  87. }
  88. public static void WriteSMBDateTime(byte[] buffer, ref int offset, DateTime dateTime)
  89. {
  90. WriteSMBDateTime(buffer, offset, dateTime);
  91. offset += 4;
  92. }
  93. public static string ReadSMBString(byte[] buffer, int offset, bool isUnicode)
  94. {
  95. if (isUnicode)
  96. {
  97. return ByteReader.ReadNullTerminatedUTF16String(buffer, offset);
  98. }
  99. else
  100. {
  101. return ByteReader.ReadNullTerminatedAnsiString(buffer, offset);
  102. }
  103. }
  104. public static string ReadSMBString(byte[] buffer, ref int offset, bool isUnicode)
  105. {
  106. if (isUnicode)
  107. {
  108. return ByteReader.ReadNullTerminatedUTF16String(buffer, ref offset);
  109. }
  110. else
  111. {
  112. return ByteReader.ReadNullTerminatedAnsiString(buffer, ref offset);
  113. }
  114. }
  115. public static void WriteSMBString(byte[] buffer, int offset, bool isUnicode, string value)
  116. {
  117. if (isUnicode)
  118. {
  119. ByteWriter.WriteNullTerminatedUTF16String(buffer, offset, value);
  120. }
  121. else
  122. {
  123. ByteWriter.WriteNullTerminatedAnsiString(buffer, offset, value);
  124. }
  125. }
  126. public static void WriteSMBString(byte[] buffer, ref int offset, bool isUnicode, string value)
  127. {
  128. if (isUnicode)
  129. {
  130. ByteWriter.WriteNullTerminatedUTF16String(buffer, ref offset, value);
  131. }
  132. else
  133. {
  134. ByteWriter.WriteNullTerminatedAnsiString(buffer, ref offset, value);
  135. }
  136. }
  137. public static string ReadFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, int byteCount)
  138. {
  139. if (isUnicode)
  140. {
  141. return ByteReader.ReadUTF16String(buffer, ref offset, byteCount / 2);
  142. }
  143. else
  144. {
  145. return ByteReader.ReadAnsiString(buffer, ref offset, byteCount);
  146. }
  147. }
  148. public static void WriteFixedLengthString(byte[] buffer, ref int offset, bool isUnicode, string value)
  149. {
  150. if (isUnicode)
  151. {
  152. ByteWriter.WriteUTF16String(buffer, ref offset, value);
  153. }
  154. else
  155. {
  156. ByteWriter.WriteAnsiString(buffer, ref offset, value);
  157. }
  158. }
  159. }
  160. }