FileFullEAInformation.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 System.Text;
  10. using Utilities;
  11. namespace SMBLibrary
  12. {
  13. /// <summary>
  14. /// [MS-FSCC] 2.4.15 - FileFullEaInformation
  15. /// </summary>
  16. public class FileFullEAInformation : FileInformation
  17. {
  18. public const int FixedLength = 8;
  19. public uint NextEntryOffset;
  20. public byte Flags;
  21. private byte EaNameLength;
  22. private ushort EaValueLength;
  23. public string EaName; // 8-bit ASCII
  24. public string EaValue; // 8-bit ASCII
  25. public FileFullEAInformation()
  26. {
  27. }
  28. public FileFullEAInformation(byte[] buffer, int offset)
  29. {
  30. NextEntryOffset = LittleEndianReader.ReadUInt32(buffer, ref offset);
  31. Flags = ByteReader.ReadByte(buffer, ref offset);
  32. EaNameLength = ByteReader.ReadByte(buffer, ref offset);
  33. EaValueLength = LittleEndianReader.ReadUInt16(buffer, ref offset);
  34. EaName = ByteReader.ReadAnsiString(buffer, ref offset, EaNameLength);
  35. EaValue = ByteReader.ReadAnsiString(buffer, ref offset, EaValueLength);
  36. }
  37. public override void WriteBytes(byte[] buffer, int offset)
  38. {
  39. EaNameLength = (byte)EaName.Length;
  40. EaValueLength = (ushort)EaValue.Length;
  41. LittleEndianWriter.WriteUInt32(buffer, ref offset, NextEntryOffset);
  42. ByteWriter.WriteByte(buffer, ref offset, Flags);
  43. ByteWriter.WriteByte(buffer, ref offset, EaNameLength);
  44. LittleEndianWriter.WriteUInt16(buffer, ref offset, EaValueLength);
  45. ByteWriter.WriteAnsiString(buffer, ref offset, EaName);
  46. ByteWriter.WriteAnsiString(buffer, ref offset, EaValue);
  47. }
  48. public override FileInformationClass FileInformationClass
  49. {
  50. get
  51. {
  52. return FileInformationClass.FileFullEaInformation;
  53. }
  54. }
  55. public override int Length
  56. {
  57. get
  58. {
  59. return FixedLength + EaName.Length + EaValue.Length;
  60. }
  61. }
  62. public static List<FileFullEAInformation> ReadList(byte[] buffer, int offset)
  63. {
  64. List<FileFullEAInformation> result = new List<FileFullEAInformation>();
  65. FileFullEAInformation entry;
  66. do
  67. {
  68. entry = new FileFullEAInformation(buffer, offset);
  69. result.Add(entry);
  70. }
  71. while (entry.NextEntryOffset != 0);
  72. return result;
  73. }
  74. public static void WriteList(byte[] buffer, int offset, List<FileFullEAInformation> list)
  75. {
  76. // When multiple FILE_FULL_EA_INFORMATION data elements are present in the buffer, each MUST be aligned on a 4-byte boundary
  77. for (int index = 0; index < list.Count; index++)
  78. {
  79. FileFullEAInformation entry = list[index];
  80. entry.WriteBytes(buffer, offset);
  81. int entryLength = entry.Length;
  82. offset += entryLength;
  83. if (index < list.Count - 1)
  84. {
  85. int padding = (4 - (entryLength % 4)) % 4;
  86. offset += padding;
  87. }
  88. }
  89. }
  90. public int GetListLength(List<FileFullEAInformation> list)
  91. {
  92. // When multiple FILE_FULL_EA_INFORMATION data elements are present in the buffer, each MUST be aligned on a 4-byte boundary
  93. int length = 0;
  94. for (int index = 0; index < list.Count; index++)
  95. {
  96. length += list[index].Length;
  97. if (index < list.Count - 1)
  98. {
  99. int padding = (4 - (length % 4)) % 4;
  100. length += padding;
  101. }
  102. }
  103. return length;
  104. }
  105. }
  106. }