/// <summary>
 /// to decode the NtTrans parameters: from the general NtTransParameters to the concrete NtTrans Parameters.
 /// </summary>
 protected override void DecodeNtTransParameters()
 {
     if (this.smbData.NT_Trans_Parameters != null)
     {
         if ((this.smbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
         {
             using (MemoryStream memoryStream = new MemoryStream(this.smbData.NT_Trans_Parameters))
             {
                 using (Channel channel = new Channel(null, memoryStream))
                 {
                     this.ntTransParameters.Flags = channel.Read<NtTransactFlags>();
                     this.ntTransParameters.RootDirectoryFID = channel.Read<uint>();
                     this.ntTransParameters.DesiredAccess = channel.Read<NtTransactDesiredAccess>();
                     this.ntTransParameters.AllocationSize = channel.Read<ulong>();
                     this.ntTransParameters.ExtFileAttributes = channel.Read<SMB_EXT_FILE_ATTR>();
                     this.ntTransParameters.ShareAccess = channel.Read<NtTransactShareAccess>();
                     this.ntTransParameters.CreateDisposition = channel.Read<NtTransactCreateDisposition>();
                     this.ntTransParameters.CreateOptions = channel.Read<NtTransactCreateOptions>();
                     this.ntTransParameters.SecurityDescriptorLength = channel.Read<uint>();
                     this.ntTransParameters.EALength = channel.Read<uint>();
                     this.ntTransParameters.NameLength = channel.Read<uint>();
                     this.ntTransParameters.ImpersonationLevel = channel.Read<NtTransactImpersonationLevel>();
                     this.ntTransParameters.SecurityFlags = channel.Read<NtTransactSecurityFlags>();
                     // Padding data
                     channel.ReadBytes(PaddingSize);
                     this.ntTransParameters.Name = channel.ReadBytes((int)this.ntTransParameters.NameLength);
                 }
             }
         }
         else
         {
             this.ntTransParameters = TypeMarshal.ToStruct<NT_TRANSACT_CREATE_Request_NT_Trans_Parameters>(
                 this.smbData.NT_Trans_Parameters);
         }
     }
 }
        /// <summary>
        /// to create a NtTransactCreate request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="ntTransParametersFlags">A 32-bit field containing a set of flags that modify the client
        /// request. Unused bits SHOULD be set to 0 by the client when sending a message and MUST be ignored when
        /// received by the server. </param>
        /// <param name="rootDirectoryFID">If nonzero, this value is the FID of an opened root directory, and the Name
        /// field MUST be handled as relative to the directory specified by this FID. If this value is zero the Name
        /// field MUST be handled as relative to the root of the share (the TID). The FID MUST have been acquired in a
        /// previous message exchange</param>
        /// <param name="desiredAccess">A 32-bit field containing standard, specific, and generic access rights</param>
        /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
        /// bytes</param>
        /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
        /// behavior flag values.</param>
        /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other processes.
        /// </param>
        /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
        /// exists or if the file is a new file and does not already exist.</param>
        /// <param name="createOptions">ULONG A 32-bit field containing flag options to use if creating the file or
        /// directory. This field MUST be set to 0 or a combination of the following possible values</param>
        /// <param name="impersonationLevel">ULONG A value that indicates what security context the server SHOULD use
        /// when executing the command on behalf of the client</param>
        /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking mode.</param>
        /// <param name="name">The name of the file; not null-terminated.  </param>
        /// <param name="securityDescriptor">The security descriptor to use when requesting access to the file</param>
        /// <param name="extendedAttributes">The list of extended attributes that SHOULD be applied to the new file.
        /// </param>
        /// <returns>a NtTransactCreate request packet</returns>
        public SmbNtTransactCreateRequestPacket CreateNtTransactCreateRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            NtTransactFlags ntTransParametersFlags,
            uint rootDirectoryFID,
            NtTransactDesiredAccess desiredAccess,
            ulong allocationSize,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            NtTransactSecurityFlags securityFlags,
            string name,
            RawSecurityDescriptor securityDescriptor,
            FILE_FULL_EA_INFORMATION[] extendedAttributes)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            if (extendedAttributes == null)
            {
                extendedAttributes = new FILE_FULL_EA_INFORMATION[0];
            }

            SmbNtTransactCreateRequestPacket packet = new SmbNtTransactCreateRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_CREATE;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set NT_TransParameters
            NT_TRANSACT_CREATE_Request_NT_Trans_Parameters ntTransParameters =
                new NT_TRANSACT_CREATE_Request_NT_Trans_Parameters();
            ntTransParameters.Flags = ntTransParametersFlags;
            ntTransParameters.RootDirectoryFID = rootDirectoryFID;
            ntTransParameters.DesiredAccess = desiredAccess;
            ntTransParameters.AllocationSize = allocationSize;
            if (createDisposition == NtTransactCreateDisposition.FILE_OPEN
                || createDisposition == NtTransactCreateDisposition.NONE
                || (extFileAttributes & SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
                == SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
            {
                ntTransParameters.AllocationSize = 0;
            }

            ntTransParameters.ExtFileAttributes = extFileAttributes;
            ntTransParameters.ShareAccess = shareAccess;
            ntTransParameters.CreateDisposition = createDisposition;
            ntTransParameters.CreateOptions = createOptions;
            ntTransParameters.SecurityDescriptorLength = securityDescriptor == null
                ? 0 : (uint)securityDescriptor.BinaryLength;
            ntTransParameters.ImpersonationLevel = impersonationLevel;
            ntTransParameters.SecurityFlags = securityFlags;

            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
            {
                ntTransParameters.Name = Encoding.Unicode.GetBytes(name);
            }
            else
            {
                ntTransParameters.Name = Encoding.ASCII.GetBytes(name);
            }
            ntTransParameters.NameLength = (uint)ntTransParameters.Name.Length;

            // The size of the preceding SmbParameters part plus Header is an odd number for all cifs messages
            // Use ntTransParameters field to judge whether needs to add one 16-bits align pad.
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE &&
                CifsMessageUtils.GetSize<NT_TRANSACT_CREATE_Request_NT_Trans_Parameters>(ntTransParameters) % twoBytesAlign == 0)
            {
                // pad 1 byte for 16-bits align:
                smbData.Pad2 = new byte[1];
            }
            else
            {
                smbData.Pad2 = new byte[0];
            }

            // Set NT_TransData
            NT_TRANSACT_CREATE_Request_NT_Trans_Data ntTransData = new NT_TRANSACT_CREATE_Request_NT_Trans_Data();
            ntTransData.ExtendedAttributes = extendedAttributes;

            for (int i = 0; i < ntTransData.ExtendedAttributes.Length; i++)
            {
                int eaNameLength = ntTransData.ExtendedAttributes[i].EaName == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaName.Length;
                int eaValueLength = ntTransData.ExtendedAttributes[i].EaValue == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaValue.Length;
                uint currentLength = (uint)(EA.FULL_EA_FIXED_SIZE + eaNameLength + eaValueLength);
                uint alignPad = (fourBytesAlign - currentLength % fourBytesAlign) % fourBytesAlign;

                if (i != ntTransData.ExtendedAttributes.Length - 1)
                {
                    currentLength += alignPad;
                    ntTransData.ExtendedAttributes[i].NextEntryOffset = currentLength;
                }
                ntTransParameters.EALength += currentLength;
            }

            ntTransData.SecurityDescriptor = securityDescriptor;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }