Browse Source

Moved UTime helper methods from SMB1Helper to UTimeHelper

Tal Aloni 8 years ago
parent
commit
2049be2220

+ 4 - 5
SMBLibrary/SMB1/Commands/CloseRequest.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -22,24 +22,23 @@ namespace SMBLibrary.SMB1
         /// <summary>
         /// A value of 0x00000000 or 0xFFFFFFFF results in the server not updating the last modification time
         /// </summary>
-        public DateTime LastTimeModified;
+        public DateTime? LastTimeModified;
 
         public CloseRequest() : base()
         {
-            LastTimeModified = SMB1Helper.UTimeNotSpecified;
         }
 
         public CloseRequest(byte[] buffer, int offset) : base(buffer, offset, false)
         {
             FID = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
-            LastTimeModified = SMB1Helper.ReadUTime(this.SMBParameters, 2);
+            LastTimeModified = UTimeHelper.ReadNullableUTime(this.SMBParameters, 2);
         }
 
         public override byte[] GetBytes(bool isUnicode)
         {
             this.SMBParameters = new byte[ParametersLength];
             LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, FID);
-            SMB1Helper.WriteUTime(this.SMBParameters, 2, LastTimeModified);
+            UTimeHelper.WriteUTime(this.SMBParameters, 2, LastTimeModified);
             return base.GetBytes(isUnicode);
         }
 

+ 3 - 3
SMBLibrary/SMB1/Commands/OpenAndXRequest.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -25,7 +25,7 @@ namespace SMBLibrary.SMB1
         public AccessModeOptions AccessMode;
         public SMBFileAttributes SearchAttrs;
         public SMBFileAttributes FileAttrs;
-        public DateTime CreationTime; // UTime
+        public DateTime? CreationTime; // UTime
         public OpenMode OpenMode;
         public uint AllocationSize;
         public uint Timeout;
@@ -44,7 +44,7 @@ namespace SMBLibrary.SMB1
             AccessMode = AccessModeOptions.Read(this.SMBParameters, ref parametersOffset);
             SearchAttrs = (SMBFileAttributes)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
             FileAttrs = (SMBFileAttributes)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
-            CreationTime = SMB1Helper.ReadUTime(this.SMBParameters, ref parametersOffset);
+            CreationTime = UTimeHelper.ReadNullableUTime(this.SMBParameters, ref parametersOffset);
             OpenMode = OpenMode.Read(this.SMBParameters, ref parametersOffset);
             AllocationSize = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
             Timeout = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);

+ 3 - 4
SMBLibrary/SMB1/Commands/OpenAndXResponse.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -23,7 +23,7 @@ namespace SMBLibrary.SMB1
         //ushort AndXOffset;
         public ushort FID;
         public SMBFileAttributes FileAttrs;
-        public DateTime LastWriteTime; // UTime
+        public DateTime? LastWriteTime; // UTime
         public uint FileDataSize;
         public AccessRights AccessRights;
         public ResourceType ResourceType;
@@ -33,7 +33,6 @@ namespace SMBLibrary.SMB1
 
         public OpenAndXResponse() : base()
         {
-            LastWriteTime = SMB1Helper.UTimeNotSpecified;
             Reserved = new byte[6];
         }
 
@@ -48,7 +47,7 @@ namespace SMBLibrary.SMB1
             int parametersOffset = 4;
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, FID);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)FileAttrs);
-            SMB1Helper.WriteUTime(this.SMBParameters, ref parametersOffset, LastWriteTime);
+            UTimeHelper.WriteUTime(this.SMBParameters, ref parametersOffset, LastWriteTime);
             LittleEndianWriter.WriteUInt32(this.SMBParameters, ref parametersOffset, FileDataSize);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)AccessRights);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)ResourceType);

+ 2 - 3
SMBLibrary/SMB1/Commands/OpenAndXResponseExtended.cs

@@ -23,7 +23,7 @@ namespace SMBLibrary.SMB1
         //ushort AndXOffset;
         public ushort FID;
         public SMBFileAttributes FileAttrs;
-        public DateTime LastWriteTime; // UTime
+        public DateTime? LastWriteTime; // UTime
         public uint FileDataSize;
         public AccessRights AccessRights;
         public ResourceType ResourceType;
@@ -36,7 +36,6 @@ namespace SMBLibrary.SMB1
 
         public OpenAndXResponseExtended() : base()
         {
-            LastWriteTime = SMB1Helper.UTimeNotSpecified;
         }
 
         public OpenAndXResponseExtended(byte[] buffer, int offset) : base(buffer, offset, false)
@@ -50,7 +49,7 @@ namespace SMBLibrary.SMB1
             int parametersOffset = 4;
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, FID);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)FileAttrs);
-            SMB1Helper.WriteUTime(this.SMBParameters, ref parametersOffset, LastWriteTime);
+            UTimeHelper.WriteUTime(this.SMBParameters, ref parametersOffset, LastWriteTime);
             LittleEndianWriter.WriteUInt32(this.SMBParameters, ref parametersOffset, FileDataSize);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)AccessRights);
             LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)ResourceType);

+ 4 - 4
SMBLibrary/SMB1/Commands/SetInformationRequest.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -20,7 +20,7 @@ namespace SMBLibrary.SMB1
         public const int SupportedBufferFormat = 0x04;
         // Parameters:
         public SMBFileAttributes FileAttributes;
-        public DateTime LastWriteTime;
+        public DateTime? LastWriteTime;
         public byte[] Reserved; // 10 bytes
         // Data:
         public byte BufferFormat;
@@ -35,7 +35,7 @@ namespace SMBLibrary.SMB1
         public SetInformationRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
         {
             FileAttributes = (SMBFileAttributes)LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
-            LastWriteTime = SMB1Helper.ReadUTime(this.SMBParameters, 2);
+            LastWriteTime = UTimeHelper.ReadNullableUTime(this.SMBParameters, 2);
             Reserved = ByteReader.ReadBytes(this.SMBParameters, 6, 10);
 
             BufferFormat = ByteReader.ReadByte(this.SMBData, 0);
@@ -50,7 +50,7 @@ namespace SMBLibrary.SMB1
         {
             this.SMBParameters = new byte[ParametersLength];
             LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, (ushort)FileAttributes);
-            SMB1Helper.WriteUTime(this.SMBParameters, 2, LastWriteTime);
+            UTimeHelper.WriteUTime(this.SMBParameters, 2, LastWriteTime);
             ByteWriter.WriteBytes(this.SMBParameters, 6, Reserved, 10);
 
             int length = 1;

+ 0 - 30
SMBLibrary/SMB1/SMB1Helper.cs

@@ -13,8 +13,6 @@ namespace SMBLibrary.SMB1
 {
     public class SMB1Helper
     {
-        public static readonly DateTime UTimeNotSpecified = new DateTime(1970, 1, 1);
-
         public static DateTime ReadFileTime(byte[] buffer, int offset)
         {
             long span = LittleEndianConverter.ToInt64(buffer, offset);
@@ -35,34 +33,6 @@ namespace SMBLibrary.SMB1
             return ReadFileTime(buffer, offset - 8);
         }
 
-        // UTime - The number of seconds since Jan 1, 1970, 00:00:00
-        public static DateTime ReadUTime(byte[] buffer, int offset)
-        {
-            uint span = LittleEndianConverter.ToUInt32(buffer, offset);
-            DateTime result = new DateTime(1970, 1, 1);
-            return result.AddSeconds(span);
-        }
-
-        public static DateTime ReadUTime(byte[] buffer, ref int offset)
-        {
-            offset += 4;
-            return ReadUTime(buffer, offset - 4);
-        }
-
-        // UTime - The number of seconds since Jan 1, 1970, 00:00:00
-        public static void WriteUTime(byte[] buffer, int offset, DateTime time)
-        {
-            TimeSpan timespan = time - new DateTime(1970, 1, 1);
-            uint span = (uint)timespan.TotalSeconds;
-            LittleEndianWriter.WriteUInt32(buffer, offset, span);
-        }
-
-        public static void WriteUTime(byte[] buffer, ref int offset, DateTime time)
-        {
-            WriteUTime(buffer, offset, time);
-            offset += 4;
-        }
-
         /// <summary>
         /// SMB_DATE
         /// </summary>

+ 3 - 4
SMBLibrary/SMB1/Transaction2Subcommands/Transaction2Open2Request.cs

@@ -21,7 +21,7 @@ namespace SMBLibrary.SMB1
         public AccessModeOptions AccessMode;
         public ushort Reserved1;
         public SMBFileAttributes FileAttributes;
-        public DateTime CreationTime; // UTIME (seconds since Jan 1, 1970)
+        public DateTime? CreationTime; // UTIME (seconds since Jan 1, 1970)
         public OpenMode OpenMode;
         public uint AllocationSize;
         public byte[] Reserved; // 10 bytes
@@ -31,7 +31,6 @@ namespace SMBLibrary.SMB1
 
         public Transaction2Open2Request() : base()
         {
-            CreationTime = SMB1Helper.UTimeNotSpecified;
             Reserved = new byte[10];
         }
 
@@ -41,7 +40,7 @@ namespace SMBLibrary.SMB1
             AccessMode = new AccessModeOptions(parameters, 2);
             Reserved1 = LittleEndianConverter.ToUInt16(parameters, 4);
             FileAttributes = (SMBFileAttributes)LittleEndianConverter.ToUInt16(parameters, 6);
-            CreationTime = SMB1Helper.ReadUTime(parameters, 8);
+            CreationTime = UTimeHelper.ReadNullableUTime(parameters, 8);
             OpenMode = new OpenMode(parameters, 12);
             AllocationSize = LittleEndianConverter.ToUInt32(parameters, 14);
             Reserved = ByteReader.ReadBytes(parameters, 18, 10);
@@ -72,7 +71,7 @@ namespace SMBLibrary.SMB1
             AccessMode.WriteBytes(parameters, 2);
             LittleEndianWriter.WriteUInt16(parameters, 4, Reserved1);
             LittleEndianWriter.WriteUInt16(parameters, 6, (ushort)FileAttributes);
-            SMB1Helper.WriteUTime(parameters, 8, CreationTime);
+            UTimeHelper.WriteUTime(parameters, 8, CreationTime);
             OpenMode.WriteBytes(parameters, 12);
             LittleEndianWriter.WriteUInt32(parameters, 14, AllocationSize);
             ByteWriter.WriteBytes(parameters, 18, Reserved, 10);

+ 3 - 4
SMBLibrary/SMB1/Transaction2Subcommands/Transaction2Open2Response.cs

@@ -20,7 +20,7 @@ namespace SMBLibrary.SMB1
         // Parameters
         public ushort FID;
         public SMBFileAttributes FileAttributes;
-        public DateTime CreationTime;
+        public DateTime? CreationTime;
         public uint FileDataSize;
         public AccessModeOptions AccessMode;
         public ResourceType ResourceType;
@@ -32,14 +32,13 @@ namespace SMBLibrary.SMB1
 
         public Transaction2Open2Response() : base()
         {
-            CreationTime = SMB1Helper.UTimeNotSpecified;
         }
 
         public Transaction2Open2Response(byte[] parameters, byte[] data, bool isUnicode) : base()
         {
             FID = LittleEndianConverter.ToUInt16(parameters, 0);
             FileAttributes = (SMBFileAttributes)LittleEndianConverter.ToUInt16(parameters, 2);
-            CreationTime = SMB1Helper.ReadUTime(parameters, 4);
+            CreationTime = UTimeHelper.ReadNullableUTime(parameters, 4);
             FileDataSize = LittleEndianConverter.ToUInt32(parameters, 8);
             AccessMode = new AccessModeOptions(parameters, 12);
             ResourceType = (ResourceType)LittleEndianConverter.ToUInt16(parameters, 14);
@@ -55,7 +54,7 @@ namespace SMBLibrary.SMB1
             byte[] parameters = new byte[ParametersLength];
             LittleEndianWriter.WriteUInt16(parameters, 0, FID);
             LittleEndianWriter.WriteUInt16(parameters, 2, (ushort)FileAttributes);
-            SMB1Helper.WriteUTime(parameters, 4, CreationTime);
+            UTimeHelper.WriteUTime(parameters, 4, CreationTime);
             LittleEndianWriter.WriteUInt32(parameters, 8, FileDataSize);
             AccessMode.WriteBytes(parameters, 12);
             LittleEndianWriter.WriteUInt16(parameters, 14, (ushort)ResourceType);

+ 82 - 0
SMBLibrary/SMB1/UTimeHelper.cs

@@ -0,0 +1,82 @@
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+ * 
+ * You can redistribute this program and/or modify it under the terms of
+ * the GNU Lesser Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ */
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Utilities;
+
+namespace SMBLibrary.SMB1
+{
+    /// <summary>
+    /// UTime - The number of seconds since Jan 1, 1970, 00:00:00
+    /// </summary>
+    public class UTimeHelper
+    {
+        public static readonly DateTime MinUTimeValue = new DateTime(1970, 1, 1);
+
+        public static DateTime ReadUTime(byte[] buffer, int offset)
+        {
+            uint span = LittleEndianConverter.ToUInt32(buffer, offset);
+            return MinUTimeValue.AddSeconds(span);
+        }
+
+        public static DateTime ReadUTime(byte[] buffer, ref int offset)
+        {
+            offset += 4;
+            return ReadUTime(buffer, offset - 4);
+        }
+
+        public static DateTime? ReadNullableUTime(byte[] buffer, int offset)
+        {
+            uint span = LittleEndianConverter.ToUInt32(buffer, offset);
+            if (span > 0)
+            {
+                return MinUTimeValue.AddSeconds(span);
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        public static DateTime? ReadNullableUTime(byte[] buffer, ref int offset)
+        {
+            offset += 4;
+            return ReadNullableUTime(buffer, offset - 4);
+        }
+
+        public static void WriteUTime(byte[] buffer, int offset, DateTime time)
+        {
+            TimeSpan timespan = time - MinUTimeValue;
+            uint span = (uint)timespan.TotalSeconds;
+            LittleEndianWriter.WriteUInt32(buffer, offset, span);
+        }
+
+        public static void WriteUTime(byte[] buffer, ref int offset, DateTime time)
+        {
+            WriteUTime(buffer, offset, time);
+            offset += 4;
+        }
+
+        public static void WriteUTime(byte[] buffer, int offset, DateTime? time)
+        {
+            uint span = 0;
+            if (time.HasValue)
+            {
+                TimeSpan timespan = time.Value - MinUTimeValue;
+                span = (uint)timespan.TotalSeconds;
+            }
+            LittleEndianWriter.WriteUInt32(buffer, offset, span);
+        }
+
+        public static void WriteUTime(byte[] buffer, ref int offset, DateTime? time)
+        {
+            WriteUTime(buffer, offset, time);
+            offset += 4;
+        }
+    }
+}

+ 1 - 0
SMBLibrary/SMBLibrary.csproj

@@ -385,6 +385,7 @@
     <Compile Include="SMB1\TransactionSubcommands\TransactionWaitNamedPipeRequest.cs" />
     <Compile Include="SMB1\TransactionSubcommands\TransactionWriteNamedPipeRequest.cs" />
     <Compile Include="SMB1\TransactionSubcommands\TransactionWriteNamedPipeResponse.cs" />
+    <Compile Include="SMB1\UTimeHelper.cs" />
     <Compile Include="Structures\ACE\AccessAllowedACE.cs" />
     <Compile Include="Structures\ACE\ACE.cs" />
     <Compile Include="Structures\ACE\AceHeader.cs" />

+ 1 - 1
SMBLibrary/Server/SMB1/FileSystemResponseHelper.cs

@@ -241,7 +241,7 @@ namespace SMBLibrary.Server.SMB1
             }
             fileSystem.SetAttributes(request.FileName, isHidden, isReadOnly, isArchived);
 
-            if (request.LastWriteTime != SMB1Helper.UTimeNotSpecified)
+            if (request.LastWriteTime.HasValue)
             {
                 fileSystem.SetDates(request.FileName, null, request.LastWriteTime, null);
             }