RenameRequest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.IO;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_RENAME Request
  15. /// </summary>
  16. public class RenameRequest : SMB1Command
  17. {
  18. public const int SupportedBufferFormat = 0x04;
  19. public const int ParametersLength = 2;
  20. // Parameters:
  21. public SMBFileAttributes SearchAttributes;
  22. // Data:
  23. public byte BufferFormat1;
  24. public string OldFileName; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  25. public byte BufferFormat2;
  26. public string NewFileName; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  27. public RenameRequest() : base()
  28. {
  29. BufferFormat1 = SupportedBufferFormat;
  30. BufferFormat2 = SupportedBufferFormat;
  31. }
  32. public RenameRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  33. {
  34. SearchAttributes = (SMBFileAttributes)LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
  35. int dataOffset = 0;
  36. BufferFormat1 = ByteReader.ReadByte(this.SMBData, ref dataOffset);
  37. if (BufferFormat1 != SupportedBufferFormat)
  38. {
  39. throw new InvalidDataException("Unsupported Buffer Format");
  40. }
  41. OldFileName = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  42. BufferFormat2 = ByteReader.ReadByte(this.SMBData, ref dataOffset);
  43. if (BufferFormat2 != SupportedBufferFormat)
  44. {
  45. throw new InvalidDataException("Unsupported Buffer Format");
  46. }
  47. if (isUnicode)
  48. {
  49. dataOffset++;
  50. }
  51. NewFileName = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  52. }
  53. public override byte[] GetBytes(bool isUnicode)
  54. {
  55. this.SMBParameters = new byte[ParametersLength];
  56. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, (ushort)SearchAttributes);
  57. if (isUnicode)
  58. {
  59. int padding = 1;
  60. this.SMBData = new byte[2 + OldFileName.Length * 2 + NewFileName.Length * 2 + 4 + padding];
  61. }
  62. else
  63. {
  64. this.SMBData = new byte[2 + OldFileName.Length + NewFileName.Length + 2];
  65. }
  66. int dataOffset = 0;
  67. ByteWriter.WriteByte(this.SMBData, ref dataOffset, BufferFormat1);
  68. SMB1Helper.WriteSMBString(this.SMBData, ref dataOffset, isUnicode, OldFileName);
  69. ByteWriter.WriteByte(this.SMBData, ref dataOffset, BufferFormat2);
  70. if (isUnicode)
  71. {
  72. dataOffset++; // padding
  73. }
  74. SMB1Helper.WriteSMBString(this.SMBData, ref dataOffset, isUnicode, NewFileName);
  75. return base.GetBytes(isUnicode);
  76. }
  77. public override CommandName CommandName
  78. {
  79. get
  80. {
  81. return CommandName.SMB_COM_RENAME;
  82. }
  83. }
  84. }
  85. }