/// <summary>
 /// to create a NtTransactCreate request packet.
 /// </summary>
 /// <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="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>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbNtTransactCreateRequestPacket CreateNtTransactCreateRequest(
     ushort uid,
     ushort treeId,
     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)
 {
     return this.CreateNtTransactCreateRequest(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, this.defaultParameters.MaxSetupCount,
         this.defaultParameters.MaxParameterCount, this.defaultParameters.MaxDataCount, ntTransParametersFlags,
         rootDirectoryFID, desiredAccess, allocationSize, extFileAttributes, shareAccess,
         createDisposition, createOptions, impersonationLevel, securityFlags, name,
         securityDescriptor, extendedAttributes);
 }
        /// <summary>
        /// Create a not transaction create request packet for client to create new or open share on server.
        /// </summary>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "fileName">A string that represents the name of the file to open or create. </param>
        /// <param name = "desiredAccess">
        /// Access wanted. This value MUST be specified in the ACCESS_MASK format, as specified in [CIFS] section 3.7. 
        /// </param>
        /// <param name = "extFileAttributes">Extended attributes and flags for this file or directory. </param>
        /// <param name = "shareAccess">Type of shared access requested for this file or directory. </param>
        /// <param name = "createDisposition">The action to take if a file does or does not exist. </param>
        /// <param name = "createOptions">The options to use if creating the file or directory. </param>
        /// <param name = "impersonationLevel">
        /// This field specifies the information given to the server about the client and how the server MUST   
        /// represent, or impersonate, the client. 
        /// </param>
        /// <param name = "createFlags">
        /// A 32-bit field containing a set of options that specify the security tracking mode. 
        /// </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 nt transaction create request packet</returns>
        public virtual SmbNtTransactCreateRequestPacket CreateNtTransCreateRequest(
            ushort treeId,
            string fileName,
            NtTransactDesiredAccess desiredAccess,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            CreateFlags createFlags,
            RawSecurityDescriptor securityDescriptor,
            FILE_FULL_EA_INFORMATION[] extendedAttributes)
        {
            Cifs.SmbNtTransactCreateRequestPacket request = this.cifsClient.CreateNtTransactCreateRequest(
                this.MessageId, this.GetSessionIdByTreeId(treeId), treeId,
                (SmbFlags)this.capability.Flag, (SmbFlags2)this.capability.Flags2,
                this.capability.MaxSetupCount, this.capability.MaxParameterCount, this.capability.MaxDataCount,
                (NtTransactFlags)createFlags,
                0, // No root directory
                desiredAccess,
                0, // AllocationSize should be 0
                extFileAttributes, shareAccess, createDisposition, (Cifs.NtTransactCreateOptions)createOptions,
                impersonationLevel,
                (NtTransactSecurityFlags)0x00,
                fileName,
                securityDescriptor, // No security descriptor
                extendedAttributes // No extended attributes
                );

            return new SmbNtTransactCreateRequestPacket(request);
        }
        /// <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;
        }