Moved UTime helper methods from SMB1Helper to UTimeHelper

This commit is contained in:
Tal Aloni 2017-01-14 17:17:57 +02:00
parent 8e67e1fab8
commit 2049be2220
11 changed files with 106 additions and 58 deletions

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -22,24 +22,23 @@ namespace SMBLibrary.SMB1
/// <summary> /// <summary>
/// A value of 0x00000000 or 0xFFFFFFFF results in the server not updating the last modification time /// A value of 0x00000000 or 0xFFFFFFFF results in the server not updating the last modification time
/// </summary> /// </summary>
public DateTime LastTimeModified; public DateTime? LastTimeModified;
public CloseRequest() : base() public CloseRequest() : base()
{ {
LastTimeModified = SMB1Helper.UTimeNotSpecified;
} }
public CloseRequest(byte[] buffer, int offset) : base(buffer, offset, false) public CloseRequest(byte[] buffer, int offset) : base(buffer, offset, false)
{ {
FID = LittleEndianConverter.ToUInt16(this.SMBParameters, 0); FID = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
LastTimeModified = SMB1Helper.ReadUTime(this.SMBParameters, 2); LastTimeModified = UTimeHelper.ReadNullableUTime(this.SMBParameters, 2);
} }
public override byte[] GetBytes(bool isUnicode) public override byte[] GetBytes(bool isUnicode)
{ {
this.SMBParameters = new byte[ParametersLength]; this.SMBParameters = new byte[ParametersLength];
LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, FID); LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, FID);
SMB1Helper.WriteUTime(this.SMBParameters, 2, LastTimeModified); UTimeHelper.WriteUTime(this.SMBParameters, 2, LastTimeModified);
return base.GetBytes(isUnicode); return base.GetBytes(isUnicode);
} }

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -25,7 +25,7 @@ namespace SMBLibrary.SMB1
public AccessModeOptions AccessMode; public AccessModeOptions AccessMode;
public SMBFileAttributes SearchAttrs; public SMBFileAttributes SearchAttrs;
public SMBFileAttributes FileAttrs; public SMBFileAttributes FileAttrs;
public DateTime CreationTime; // UTime public DateTime? CreationTime; // UTime
public OpenMode OpenMode; public OpenMode OpenMode;
public uint AllocationSize; public uint AllocationSize;
public uint Timeout; public uint Timeout;
@ -44,7 +44,7 @@ namespace SMBLibrary.SMB1
AccessMode = AccessModeOptions.Read(this.SMBParameters, ref parametersOffset); AccessMode = AccessModeOptions.Read(this.SMBParameters, ref parametersOffset);
SearchAttrs = (SMBFileAttributes)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset); SearchAttrs = (SMBFileAttributes)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
FileAttrs = (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); OpenMode = OpenMode.Read(this.SMBParameters, ref parametersOffset);
AllocationSize = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset); AllocationSize = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
Timeout = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset); Timeout = LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -23,7 +23,7 @@ namespace SMBLibrary.SMB1
//ushort AndXOffset; //ushort AndXOffset;
public ushort FID; public ushort FID;
public SMBFileAttributes FileAttrs; public SMBFileAttributes FileAttrs;
public DateTime LastWriteTime; // UTime public DateTime? LastWriteTime; // UTime
public uint FileDataSize; public uint FileDataSize;
public AccessRights AccessRights; public AccessRights AccessRights;
public ResourceType ResourceType; public ResourceType ResourceType;
@ -33,7 +33,6 @@ namespace SMBLibrary.SMB1
public OpenAndXResponse() : base() public OpenAndXResponse() : base()
{ {
LastWriteTime = SMB1Helper.UTimeNotSpecified;
Reserved = new byte[6]; Reserved = new byte[6];
} }
@ -48,7 +47,7 @@ namespace SMBLibrary.SMB1
int parametersOffset = 4; int parametersOffset = 4;
LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, FID); LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, FID);
LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)FileAttrs); 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.WriteUInt32(this.SMBParameters, ref parametersOffset, FileDataSize);
LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)AccessRights); LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)AccessRights);
LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)ResourceType); LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)ResourceType);

View file

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

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -20,7 +20,7 @@ namespace SMBLibrary.SMB1
public const int SupportedBufferFormat = 0x04; public const int SupportedBufferFormat = 0x04;
// Parameters: // Parameters:
public SMBFileAttributes FileAttributes; public SMBFileAttributes FileAttributes;
public DateTime LastWriteTime; public DateTime? LastWriteTime;
public byte[] Reserved; // 10 bytes public byte[] Reserved; // 10 bytes
// Data: // Data:
public byte BufferFormat; public byte BufferFormat;
@ -35,7 +35,7 @@ namespace SMBLibrary.SMB1
public SetInformationRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode) public SetInformationRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
{ {
FileAttributes = (SMBFileAttributes)LittleEndianConverter.ToUInt16(this.SMBParameters, 0); 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); Reserved = ByteReader.ReadBytes(this.SMBParameters, 6, 10);
BufferFormat = ByteReader.ReadByte(this.SMBData, 0); BufferFormat = ByteReader.ReadByte(this.SMBData, 0);
@ -50,7 +50,7 @@ namespace SMBLibrary.SMB1
{ {
this.SMBParameters = new byte[ParametersLength]; this.SMBParameters = new byte[ParametersLength];
LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, (ushort)FileAttributes); 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); ByteWriter.WriteBytes(this.SMBParameters, 6, Reserved, 10);
int length = 1; int length = 1;

View file

@ -13,8 +13,6 @@ namespace SMBLibrary.SMB1
{ {
public class SMB1Helper public class SMB1Helper
{ {
public static readonly DateTime UTimeNotSpecified = new DateTime(1970, 1, 1);
public static DateTime ReadFileTime(byte[] buffer, int offset) public static DateTime ReadFileTime(byte[] buffer, int offset)
{ {
long span = LittleEndianConverter.ToInt64(buffer, offset); long span = LittleEndianConverter.ToInt64(buffer, offset);
@ -35,34 +33,6 @@ namespace SMBLibrary.SMB1
return ReadFileTime(buffer, offset - 8); 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> /// <summary>
/// SMB_DATE /// SMB_DATE
/// </summary> /// </summary>

View file

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

View file

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

View file

@ -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;
}
}
}

View file

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

View file

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