FileTimeHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Utilities;
  10. namespace SMBLibrary
  11. {
  12. public class FileTimeHelper
  13. {
  14. public static readonly DateTime MinFileTimeValue = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  15. public static DateTime ReadFileTime(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
  23. {
  24. throw new System.IO.InvalidDataException("FILETIME cannot be negative");
  25. }
  26. }
  27. public static DateTime ReadFileTime(byte[] buffer, ref int offset)
  28. {
  29. offset += 8;
  30. return ReadFileTime(buffer, offset - 8);
  31. }
  32. public static void WriteFileTime(byte[] buffer, int offset, DateTime time)
  33. {
  34. long span = time.ToFileTimeUtc();
  35. LittleEndianWriter.WriteInt64(buffer, offset, span);
  36. }
  37. public static void WriteFileTime(byte[] buffer, ref int offset, DateTime time)
  38. {
  39. WriteFileTime(buffer, offset, time);
  40. offset += 8;
  41. }
  42. public static DateTime? ReadNullableFileTime(byte[] buffer, int offset)
  43. {
  44. long span = LittleEndianConverter.ToInt64(buffer, offset);
  45. if (span > 0)
  46. {
  47. return DateTime.FromFileTimeUtc(span);
  48. }
  49. else if (span == 0)
  50. {
  51. return null;
  52. }
  53. else
  54. {
  55. throw new System.IO.InvalidDataException("FILETIME cannot be negative");
  56. }
  57. }
  58. public static DateTime? ReadNullableFileTime(byte[] buffer, ref int offset)
  59. {
  60. offset += 8;
  61. return ReadNullableFileTime(buffer, offset - 8);
  62. }
  63. public static void WriteFileTime(byte[] buffer, int offset, DateTime? time)
  64. {
  65. long span = 0;
  66. if (time.HasValue)
  67. {
  68. span = time.Value.ToFileTimeUtc();
  69. }
  70. LittleEndianWriter.WriteInt64(buffer, offset, span);
  71. }
  72. public static void WriteFileTime(byte[] buffer, ref int offset, DateTime? time)
  73. {
  74. WriteFileTime(buffer, offset, time);
  75. offset += 8;
  76. }
  77. /// <summary>
  78. /// When setting file attributes, a value of -1 indicates to the server that it MUST NOT change this attribute for all subsequent operations on the same file handle.
  79. /// </summary>
  80. public static SetFileTime ReadSetFileTime(byte[] buffer, int offset)
  81. {
  82. long span = LittleEndianConverter.ToInt64(buffer, offset);
  83. return SetFileTime.FromFileTimeUtc(span);
  84. }
  85. /// <summary>
  86. /// When setting file attributes, a value of -1 indicates to the server that it MUST NOT change this attribute for all subsequent operations on the same file handle.
  87. /// </summary>
  88. public static void WriteSetFileTime(byte[] buffer, int offset, SetFileTime time)
  89. {
  90. long span = time.ToFileTimeUtc();
  91. LittleEndianWriter.WriteInt64(buffer, offset, span);
  92. }
  93. }
  94. }