LockElement.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (C) 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.SMB2
  11. {
  12. public struct LockElement
  13. {
  14. public const int StructureLength = 24;
  15. public ulong Offset;
  16. public ulong Length;
  17. public LockFlags Flags;
  18. public uint Reserved;
  19. public LockElement(byte[] buffer, int offset)
  20. {
  21. Offset = LittleEndianConverter.ToUInt64(buffer, offset + 0);
  22. Length = LittleEndianConverter.ToUInt64(buffer, offset + 8);
  23. Flags = (LockFlags)LittleEndianConverter.ToUInt32(buffer, offset + 16);
  24. Reserved = LittleEndianConverter.ToUInt32(buffer, offset + 20);
  25. }
  26. public void WriteBytes(byte[] buffer, int offset)
  27. {
  28. LittleEndianWriter.WriteUInt64(buffer, offset + 0, Offset);
  29. LittleEndianWriter.WriteUInt64(buffer, offset + 8, Length);
  30. LittleEndianWriter.WriteUInt64(buffer, offset + 16, (uint)Flags);
  31. LittleEndianWriter.WriteUInt64(buffer, offset + 20, Reserved);
  32. }
  33. public bool SharedLock
  34. {
  35. get
  36. {
  37. return (Flags & LockFlags.SharedLock) > 0;
  38. }
  39. set
  40. {
  41. if (value)
  42. {
  43. Flags |= LockFlags.SharedLock;
  44. }
  45. else
  46. {
  47. Flags &= ~LockFlags.SharedLock;
  48. }
  49. }
  50. }
  51. public bool ExclusiveLock
  52. {
  53. get
  54. {
  55. return (Flags & LockFlags.ExclusiveLock) > 0;
  56. }
  57. set
  58. {
  59. if (value)
  60. {
  61. Flags |= LockFlags.ExclusiveLock;
  62. }
  63. else
  64. {
  65. Flags &= ~LockFlags.ExclusiveLock;
  66. }
  67. }
  68. }
  69. public bool Unlock
  70. {
  71. get
  72. {
  73. return (Flags & LockFlags.Unlock) > 0;
  74. }
  75. set
  76. {
  77. if (value)
  78. {
  79. Flags |= LockFlags.Unlock;
  80. }
  81. else
  82. {
  83. Flags &= ~LockFlags.Unlock;
  84. }
  85. }
  86. }
  87. public bool FailImmediately
  88. {
  89. get
  90. {
  91. return (Flags & LockFlags.FailImmediately) > 0;
  92. }
  93. set
  94. {
  95. if (value)
  96. {
  97. Flags |= LockFlags.FailImmediately;
  98. }
  99. else
  100. {
  101. Flags &= ~LockFlags.FailImmediately;
  102. }
  103. }
  104. }
  105. public static List<LockElement> ReadLockList(byte[] buffer, int offset, int lockCount)
  106. {
  107. List<LockElement> result = new List<LockElement>();
  108. for(int lockIndex = 0; lockIndex < lockCount; lockIndex++)
  109. {
  110. LockElement element = new LockElement(buffer, offset + lockIndex * StructureLength);
  111. result.Add(element);
  112. }
  113. return result;
  114. }
  115. public static void WriteLockList(byte[] buffer, int offset, List<LockElement> locks)
  116. {
  117. for (int lockIndex = 0; lockIndex < locks.Count; lockIndex++)
  118. {
  119. LockElement element = locks[lockIndex];
  120. element.WriteBytes(buffer, offset + lockIndex * StructureLength);
  121. }
  122. }
  123. }
  124. }