FindInformation.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2014 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.SMB1
  12. {
  13. public class FindInformation : List<FindInformationEntry>
  14. {
  15. public FindInformation()
  16. {
  17. }
  18. public FindInformation(byte[] buffer, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
  19. {
  20. int offset = 0;
  21. while (offset < buffer.Length)
  22. {
  23. FindInformationEntry entry = FindInformationEntry.ReadEntry(buffer, ref offset, informationLevel, isUnicode, returnResumeKeys);
  24. this.Add(entry);
  25. }
  26. }
  27. public byte[] GetBytes(bool isUnicode)
  28. {
  29. for(int index = 0; index < this.Count; index++)
  30. {
  31. if (index < this.Count - 1)
  32. {
  33. FindInformationEntry entry = this[index];
  34. int entryLength = entry.GetLength(isUnicode);
  35. if (entry is FindFileBothDirectoryInfo)
  36. {
  37. ((FindFileBothDirectoryInfo)entry).NextEntryOffset = (uint)entryLength;
  38. }
  39. else if (entry is FindFileDirectoryInfo)
  40. {
  41. ((FindFileDirectoryInfo)entry).NextEntryOffset = (uint)entryLength;
  42. }
  43. else if (entry is FindFileFullDirectoryInfo)
  44. {
  45. ((FindFileFullDirectoryInfo)entry).NextEntryOffset = (uint)entryLength;
  46. }
  47. else if (entry is FindFileNamesInfo)
  48. {
  49. ((FindFileNamesInfo)entry).NextEntryOffset = (uint)entryLength;
  50. }
  51. }
  52. }
  53. int length = GetLength(isUnicode);
  54. byte[] buffer = new byte[length];
  55. int offset = 0;
  56. foreach (FindInformationEntry entry in this)
  57. {
  58. entry.WriteBytes(buffer, ref offset, isUnicode);
  59. }
  60. return buffer;
  61. }
  62. public int GetLength(bool isUnicode)
  63. {
  64. int length = 0;
  65. for (int index = 0; index < this.Count; index++)
  66. {
  67. FindInformationEntry entry = this[index];
  68. int entryLength = entry.GetLength(isUnicode);
  69. length += entryLength;
  70. }
  71. return length;
  72. }
  73. }
  74. }