/// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbNtTransactCreateRequestPacket(SmbNtTransactCreateRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.ntTransParameters.Flags = packet.ntTransParameters.Flags;
            this.ntTransParameters.RootDirectoryFID = packet.ntTransParameters.RootDirectoryFID;
            this.ntTransParameters.DesiredAccess = packet.ntTransParameters.DesiredAccess;
            this.ntTransParameters.AllocationSize = packet.ntTransParameters.AllocationSize;
            this.ntTransParameters.ExtFileAttributes = packet.ntTransParameters.ExtFileAttributes;
            this.ntTransParameters.ShareAccess = packet.ntTransParameters.ShareAccess;
            this.ntTransParameters.CreateDisposition = packet.ntTransParameters.CreateDisposition;
            this.ntTransParameters.CreateOptions = packet.ntTransParameters.CreateOptions;
            this.ntTransParameters.SecurityDescriptorLength = packet.ntTransParameters.SecurityDescriptorLength;
            this.ntTransParameters.EALength = packet.ntTransParameters.EALength;
            this.ntTransParameters.NameLength = packet.ntTransParameters.NameLength;
            this.ntTransParameters.ImpersonationLevel = packet.ntTransParameters.ImpersonationLevel;
            this.ntTransParameters.SecurityFlags = packet.ntTransParameters.SecurityFlags;

            if (packet.ntTransParameters.Name != null)
            {
                this.ntTransParameters.Name = new byte[packet.ntTransParameters.NameLength];
                Array.Copy(packet.ntTransParameters.Name,
                    this.ntTransParameters.Name, packet.ntTransParameters.NameLength);
            }
            else
            {
                this.ntTransParameters.Name = new byte[0];
            }
            this.ntTransData.SecurityDescriptor = packet.ntTransData.SecurityDescriptor;

            if (packet.ntTransData.ExtendedAttributes != null)
            {
                this.ntTransData.ExtendedAttributes = new FILE_FULL_EA_INFORMATION[packet.ntTransData.ExtendedAttributes.Length];
                Array.Copy(packet.ntTransData.ExtendedAttributes,
                    this.ntTransData.ExtendedAttributes, packet.ntTransData.ExtendedAttributes.Length);
            }
            else
            {
                this.ntTransData.ExtendedAttributes = new FILE_FULL_EA_INFORMATION[0];
            }
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbNtTransactCreateRequestPacket(SmbNtTransactCreateRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.ntTransParameters.Flags                    = packet.ntTransParameters.Flags;
            this.ntTransParameters.RootDirectoryFID         = packet.ntTransParameters.RootDirectoryFID;
            this.ntTransParameters.DesiredAccess            = packet.ntTransParameters.DesiredAccess;
            this.ntTransParameters.AllocationSize           = packet.ntTransParameters.AllocationSize;
            this.ntTransParameters.ExtFileAttributes        = packet.ntTransParameters.ExtFileAttributes;
            this.ntTransParameters.ShareAccess              = packet.ntTransParameters.ShareAccess;
            this.ntTransParameters.CreateDisposition        = packet.ntTransParameters.CreateDisposition;
            this.ntTransParameters.CreateOptions            = packet.ntTransParameters.CreateOptions;
            this.ntTransParameters.SecurityDescriptorLength = packet.ntTransParameters.SecurityDescriptorLength;
            this.ntTransParameters.EALength                 = packet.ntTransParameters.EALength;
            this.ntTransParameters.NameLength               = packet.ntTransParameters.NameLength;
            this.ntTransParameters.ImpersonationLevel       = packet.ntTransParameters.ImpersonationLevel;
            this.ntTransParameters.SecurityFlags            = packet.ntTransParameters.SecurityFlags;

            if (packet.ntTransParameters.Name != null)
            {
                this.ntTransParameters.Name = new byte[packet.ntTransParameters.NameLength];
                Array.Copy(packet.ntTransParameters.Name,
                           this.ntTransParameters.Name, packet.ntTransParameters.NameLength);
            }
            else
            {
                this.ntTransParameters.Name = new byte[0];
            }
            this.ntTransData.SecurityDescriptor = packet.ntTransData.SecurityDescriptor;

            if (packet.ntTransData.ExtendedAttributes != null)
            {
                this.ntTransData.ExtendedAttributes = new FILE_FULL_EA_INFORMATION[packet.ntTransData.ExtendedAttributes.Length];
                Array.Copy(packet.ntTransData.ExtendedAttributes,
                           this.ntTransData.ExtendedAttributes, packet.ntTransData.ExtendedAttributes.Length);
            }
            else
            {
                this.ntTransData.ExtendedAttributes = new FILE_FULL_EA_INFORMATION[0];
            }
        }
        /// <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;
        }
        public SmbNtTransactCreateResponsePacket CreateNtTransactCreateResponse(
            CifsServerPerConnection connection,
            SmbNtTransactCreateRequestPacket request,
            ulong allocationSize,
            ulong endOfFile,
            FileTypeValue resourceType,
            SMB_NMPIPE_STATUS nmPipeStatus,
            byte directory)
        {
            SmbNtTransactCreateResponsePacket response = new SmbNtTransactCreateResponsePacket();
            response.SmbHeader = CifsMessageUtils.CreateSmbHeader(connection, request);

            NT_TRANSACT_CREATE_Response_NT_Trans_Parameters ntTransParameters = response.NtTransParameters;
            ntTransParameters.OpLockLevel = OplockLevelValue.None;
            ntTransParameters.Reserved = 0x00;
            ntTransParameters.FID = (ushort)connection.GenerateFID();
            ntTransParameters.CreateAction = NtTransactCreateActionValues.FILE_CREATED;
            FileTime fileTime = new FileTime();
            fileTime.Time = (ulong)DateTime.Now.ToFileTime();
            ntTransParameters.CreationTime = fileTime;
            ntTransParameters.LastAccessTime = fileTime;
            ntTransParameters.LastWriteTime = fileTime;
            ntTransParameters.LastChangeTime = fileTime;
            ntTransParameters.ExtFileAttributes = request.NtTransParameters.ExtFileAttributes;
            ntTransParameters.AllocationSize = allocationSize;
            ntTransParameters.EndOfFile = endOfFile;
            ntTransParameters.ResourceType = resourceType;
            ntTransParameters.NMPipeStatus = nmPipeStatus;
            ntTransParameters.Directory = directory;
            response.NtTransParameters = ntTransParameters;

            response.UpdateCountAndOffset();

            return response;
        }