CreateContext.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /// <summary>
  13. /// [MS-SMB2] 2.2.13.2 - SMB2_CREATE_CONTEXT
  14. /// </summary>
  15. public class CreateContext
  16. {
  17. public int FixedLength = 16;
  18. /// <summary>
  19. /// The offset from the beginning of this Create Context to the beginning of a subsequent 8-byte aligned Create Context.
  20. /// This field MUST be set to 0 if there are no subsequent contexts.
  21. /// </summary>
  22. public uint Next;
  23. private ushort NameOffset; // The offset from the beginning of this structure to the 8-byte aligned name value
  24. private ushort NameLength;
  25. public ushort Reserved;
  26. private ushort DataOffset; // The offset from the beginning of this structure to the 8-byte aligned data payload
  27. private uint DataLength;
  28. public string Name = String.Empty;
  29. public byte[] Data = new byte[0];
  30. public CreateContext()
  31. {
  32. }
  33. public CreateContext(byte[] buffer, int offset)
  34. {
  35. Next = LittleEndianConverter.ToUInt32(buffer, offset + 0);
  36. NameOffset = LittleEndianConverter.ToUInt16(buffer, offset + 4);
  37. NameLength = LittleEndianConverter.ToUInt16(buffer, offset + 6);
  38. Reserved = LittleEndianConverter.ToUInt16(buffer, offset + 8);
  39. DataOffset = LittleEndianConverter.ToUInt16(buffer, offset + 10);
  40. DataLength = LittleEndianConverter.ToUInt32(buffer, offset + 12);
  41. if (NameLength > 0)
  42. {
  43. Name = ByteReader.ReadUTF16String(buffer, offset + NameOffset, NameLength / 2);
  44. }
  45. if (DataLength > 0)
  46. {
  47. Data = ByteReader.ReadBytes(buffer, offset + DataOffset, (int)DataLength);
  48. }
  49. }
  50. private void WriteBytes(byte[] buffer, int offset)
  51. {
  52. LittleEndianWriter.WriteUInt32(buffer, offset + 0, Next);
  53. NameOffset = 0;
  54. NameLength = (ushort)(Name.Length * 2);
  55. if (Name.Length > 0)
  56. {
  57. NameOffset = (ushort)FixedLength;
  58. }
  59. LittleEndianWriter.WriteUInt16(buffer, offset + 4, NameOffset);
  60. LittleEndianWriter.WriteUInt16(buffer, offset + 6, NameLength);
  61. LittleEndianWriter.WriteUInt16(buffer, offset + 8, Reserved);
  62. DataOffset = 0;
  63. DataLength = (uint)Data.Length;
  64. if (Data.Length > 0)
  65. {
  66. int paddedNameLength = (int)Math.Ceiling((double)(Name.Length * 2) / 8) * 8;
  67. DataOffset = (ushort)(FixedLength + paddedNameLength);
  68. }
  69. LittleEndianWriter.WriteUInt16(buffer, offset + 10, DataOffset);
  70. ByteWriter.WriteUTF16String(buffer, NameOffset, Name);
  71. ByteWriter.WriteBytes(buffer, DataOffset, Data);
  72. }
  73. public int Length
  74. {
  75. get
  76. {
  77. if (Data.Length > 0)
  78. {
  79. int paddedNameLength = (int)Math.Ceiling((double)(Name.Length * 2) / 8) * 8;
  80. return FixedLength + paddedNameLength + Data.Length;
  81. }
  82. else
  83. {
  84. return FixedLength + Name.Length * 2;
  85. }
  86. }
  87. }
  88. public static List<CreateContext> ReadCreateContextList(byte[] buffer, int offset)
  89. {
  90. List<CreateContext> result = new List<CreateContext>();
  91. CreateContext createContext;
  92. do
  93. {
  94. createContext = new CreateContext(buffer, offset);
  95. result.Add(createContext);
  96. offset += (int)createContext.Next;
  97. }
  98. while (createContext.Next != 0);
  99. return result;
  100. }
  101. public static void WriteCreateContextList(byte[] buffer, int offset, List<CreateContext> createContexts)
  102. {
  103. for (int index = 0; index < createContexts.Count; index++)
  104. {
  105. CreateContext createContext = createContexts[index];
  106. int length = createContext.Length;
  107. int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
  108. if (index < createContexts.Count - 1)
  109. {
  110. createContext.Next = (uint)paddedLength;
  111. }
  112. else
  113. {
  114. createContext.Next = 0;
  115. }
  116. createContext.WriteBytes(buffer, offset);
  117. offset += paddedLength;
  118. }
  119. }
  120. public static int GetCreateContextListLength(List<CreateContext> createContexts)
  121. {
  122. int result = 0;
  123. for(int index = 0; index < createContexts.Count; index++)
  124. {
  125. CreateContext createContext = createContexts[index];
  126. int length = createContext.Length;
  127. if (index < createContexts.Count - 1)
  128. {
  129. int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
  130. result += paddedLength;
  131. }
  132. else
  133. {
  134. result += length;
  135. }
  136. }
  137. return result;
  138. }
  139. }
  140. }