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

            this.smbParameters.WordCount = packet.SmbParameters.WordCount;
            this.smbParameters.TotalParameterCount = packet.SmbParameters.TotalParameterCount;
            this.smbParameters.TotalDataCount = packet.SmbParameters.TotalDataCount;
            this.smbParameters.MaxParameterCount = packet.SmbParameters.MaxParameterCount;
            this.smbParameters.MaxDataCount = packet.SmbParameters.MaxDataCount;
            this.smbParameters.MaxSetupCount = packet.SmbParameters.MaxSetupCount;
            this.smbParameters.Reserved1 = packet.SmbParameters.Reserved1;
            this.smbParameters.Flags = packet.SmbParameters.Flags;
            this.smbParameters.Timeout = packet.SmbParameters.Timeout;
            this.smbParameters.Reserved2 = packet.SmbParameters.Reserved2;
            this.smbParameters.ParameterCount = packet.SmbParameters.ParameterCount;
            this.smbParameters.ParameterOffset = packet.SmbParameters.ParameterOffset;
            this.smbParameters.DataCount = packet.SmbParameters.DataCount;
            this.smbParameters.DataOffset = packet.SmbParameters.DataOffset;
            this.smbParameters.SetupCount = packet.SmbParameters.SetupCount;
            this.smbParameters.Reserved3 = packet.SmbParameters.Reserved3;

            if (packet.smbParameters.Setup != null)
            {
                this.smbParameters.Setup = new ushort[packet.smbParameters.Setup.Length];
                Array.Copy(packet.smbParameters.Setup, this.smbParameters.Setup, packet.smbParameters.Setup.Length);
            }
            else
            {
                this.smbParameters.Setup = new ushort[0];
            }
            this.smbData.ByteCount = packet.SmbData.ByteCount;
            this.smbData.Name = packet.SmbData.Name;

            if (packet.smbData.Pad1 != null)
            {
                this.smbData.Pad1 = new byte[packet.smbData.Pad1.Length];
                Array.Copy(packet.smbData.Pad1, this.smbData.Pad1, packet.smbData.Pad1.Length);
            }
            else
            {
                this.smbData.Pad1 = new byte[0];
            }

            if (packet.smbData.Trans2_Parameters != null)
            {
                this.smbData.Trans2_Parameters = new byte[packet.smbData.Trans2_Parameters.Length];
                Array.Copy(packet.smbData.Trans2_Parameters,
                    this.smbData.Trans2_Parameters, packet.smbData.Trans2_Parameters.Length);
            }
            else
            {
                this.smbData.Trans2_Parameters = new byte[0];
            }

            if (packet.smbData.Pad2 != null)
            {
                this.smbData.Pad2 = new byte[packet.smbData.Pad2.Length];
                Array.Copy(packet.smbData.Pad2, this.smbData.Pad2, packet.smbData.Pad2.Length);
            }
            else
            {
                this.smbData.Pad2 = new byte[0];
            }

            if (packet.smbData.Trans2_Data != null)
            {
                this.smbData.Trans2_Data = new byte[packet.smbData.Trans2_Data.Length];
                Array.Copy(packet.smbData.Trans2_Data, this.smbData.Trans2_Data, packet.smbData.Trans2_Data.Length);
            }
            else
            {
                this.smbData.Trans2_Data = new byte[0];
            }
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        protected SmbTransaction2RequestPacket(SmbTransaction2RequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.smbParameters.WordCount           = packet.SmbParameters.WordCount;
            this.smbParameters.TotalParameterCount = packet.SmbParameters.TotalParameterCount;
            this.smbParameters.TotalDataCount      = packet.SmbParameters.TotalDataCount;
            this.smbParameters.MaxParameterCount   = packet.SmbParameters.MaxParameterCount;
            this.smbParameters.MaxDataCount        = packet.SmbParameters.MaxDataCount;
            this.smbParameters.MaxSetupCount       = packet.SmbParameters.MaxSetupCount;
            this.smbParameters.Reserved1           = packet.SmbParameters.Reserved1;
            this.smbParameters.Flags           = packet.SmbParameters.Flags;
            this.smbParameters.Timeout         = packet.SmbParameters.Timeout;
            this.smbParameters.Reserved2       = packet.SmbParameters.Reserved2;
            this.smbParameters.ParameterCount  = packet.SmbParameters.ParameterCount;
            this.smbParameters.ParameterOffset = packet.SmbParameters.ParameterOffset;
            this.smbParameters.DataCount       = packet.SmbParameters.DataCount;
            this.smbParameters.DataOffset      = packet.SmbParameters.DataOffset;
            this.smbParameters.SetupCount      = packet.SmbParameters.SetupCount;
            this.smbParameters.Reserved3       = packet.SmbParameters.Reserved3;

            if (packet.smbParameters.Setup != null)
            {
                this.smbParameters.Setup = new ushort[packet.smbParameters.Setup.Length];
                Array.Copy(packet.smbParameters.Setup, this.smbParameters.Setup, packet.smbParameters.Setup.Length);
            }
            else
            {
                this.smbParameters.Setup = new ushort[0];
            }
            this.smbData.ByteCount = packet.SmbData.ByteCount;
            this.smbData.Name      = packet.SmbData.Name;

            if (packet.smbData.Pad1 != null)
            {
                this.smbData.Pad1 = new byte[packet.smbData.Pad1.Length];
                Array.Copy(packet.smbData.Pad1, this.smbData.Pad1, packet.smbData.Pad1.Length);
            }
            else
            {
                this.smbData.Pad1 = new byte[0];
            }

            if (packet.smbData.Trans2_Parameters != null)
            {
                this.smbData.Trans2_Parameters = new byte[packet.smbData.Trans2_Parameters.Length];
                Array.Copy(packet.smbData.Trans2_Parameters,
                           this.smbData.Trans2_Parameters, packet.smbData.Trans2_Parameters.Length);
            }
            else
            {
                this.smbData.Trans2_Parameters = new byte[0];
            }

            if (packet.smbData.Pad2 != null)
            {
                this.smbData.Pad2 = new byte[packet.smbData.Pad2.Length];
                Array.Copy(packet.smbData.Pad2, this.smbData.Pad2, packet.smbData.Pad2.Length);
            }
            else
            {
                this.smbData.Pad2 = new byte[0];
            }

            if (packet.smbData.Trans2_Data != null)
            {
                this.smbData.Trans2_Data = new byte[packet.smbData.Trans2_Data.Length];
                Array.Copy(packet.smbData.Trans2_Data, this.smbData.Trans2_Data, packet.smbData.Trans2_Data.Length);
            }
            else
            {
                this.smbData.Trans2_Data = new byte[0];
            }
        }
 /// <summary>
 /// Create default Trans2 response
 /// </summary>
 /// <param name="connection">the connection on which the response will be sent.</param>
 /// <param name="request">the corresponding request</param>
 /// <returns>the default response to the request.</returns>
 private SmbPacket CreateDefaultTrans2Response(
     CifsServerPerConnection connection,
     SmbTransaction2RequestPacket request)
 {
     SmbTrans2Open2RequestPacket open2Request = request as SmbTrans2Open2RequestPacket;
     SmbTrans2FindFirst2RequestPacket findFirst2Request = request as SmbTrans2FindFirst2RequestPacket;
     SmbTrans2FindNext2RequestPacket findNext2Request = request as SmbTrans2FindNext2RequestPacket;
     SmbTrans2QueryFsInformationRequestPacket queryFsRequest = request as SmbTrans2QueryFsInformationRequestPacket;
     SmbTrans2SetFsInformationRequestPacket setFsInfoRequest = request as SmbTrans2SetFsInformationRequestPacket;
     SmbTrans2QueryPathInformationRequestPacket queryPathRequest = request as SmbTrans2QueryPathInformationRequestPacket;
     SmbTrans2SetPathInformationRequestPacket setPathRequest = request as SmbTrans2SetPathInformationRequestPacket;
     SmbTrans2QueryFileInformationRequestPacket queryFileRequest = request as SmbTrans2QueryFileInformationRequestPacket;
     SmbTrans2SetFileInformationRequestPacket setFileRequest = request as SmbTrans2SetFileInformationRequestPacket;
     SmbTrans2FsctlRequestPacket fsctlRequest = request as SmbTrans2FsctlRequestPacket;
     SmbTrans2Ioctl2RequestPacket ioctl2Request = request as SmbTrans2Ioctl2RequestPacket;
     SmbTrans2FindNotifyFirstRequestPacket findNotifyFirstRequest = request as SmbTrans2FindNotifyFirstRequestPacket;
     SmbTrans2FindNotifyNextRequestPacket findNotifyNextRequest = request as SmbTrans2FindNotifyNextRequestPacket;
     SmbTrans2CreateDirectoryRequestPacket createDirRequest = request as SmbTrans2CreateDirectoryRequestPacket;
     SmbTrans2SessionSetupRequestPacket sessionSetupRequest = request as SmbTrans2SessionSetupRequestPacket;
     SmbTrans2GetDfsReferalRequestPacket getDfsReferalRequest = request as SmbTrans2GetDfsReferalRequestPacket;
     SmbTrans2ReportDfsInconsistencyRequestPacket reportDfsRequest = request as SmbTrans2ReportDfsInconsistencyRequestPacket;
     SmbPacket response = null;
     if (open2Request != null)
     {
         response = this.CreateTrans2Open2FinalResponse(connection, open2Request, 0x0, FileTypeValue.FileTypeDisk,
             SMB_NMPIPE_STATUS.Endpoint, OpenResultsValues.LockStatus);
     }
     if (findFirst2Request != null)
     {
         response = this.CreateTrans2FindFirst2FinalResponse(connection, findFirst2Request, 0x0, null);
     }
     if (findNext2Request != null)
     {
         response = this.CreateTrans2FindNext2FinalResponse(connection, findNext2Request, 0x0, null);
     }
     if (queryFsRequest != null)
     {
         response = this.CreateTrans2QueryFsInformationFinalResponse(connection, queryFsRequest, null);
     }
     if (setFsInfoRequest != null)
     {
         response = this.CreateTrans2SetFsInformationFinalResponse(connection, setFsInfoRequest);
     }
     if (queryPathRequest != null)
     {
         response = this.CreateTrans2QueryPathInformationFinalResponse(connection, queryPathRequest, null);
     }
     if (setPathRequest != null)
     {
         response = this.CreateTrans2SetPathInformationFinalResponse(connection, setPathRequest);
     }
     if (queryFileRequest != null)
     {
         response = this.CreateTrans2QueryFileInformationFinalResponse(connection, queryFileRequest, null);
     }
     if (setFileRequest != null)
     {
         response = this.CreateTrans2SetFileInformationFinalResponse(connection, setFileRequest);
     }
     if (fsctlRequest != null)
     {
         response = this.CreateTrans2FsctlFinalResponse(connection, fsctlRequest);
     }
     if (ioctl2Request != null)
     {
         response = this.CreateTrans2Ioctl2FinalResponse(connection, ioctl2Request);
     }
     if (findNotifyFirstRequest != null)
     {
         response = this.CreateTrans2FindNotifyFirstFinalResponse(connection, findNotifyFirstRequest);
     }
     if (findNotifyNextRequest != null)
     {
         response = this.CreateTrans2FindNotifyNextFinalResponse(connection, findNotifyNextRequest);
     }
     if (createDirRequest != null)
     {
         response = this.CreateTrans2CreateDirectoryFinalResponse(connection, createDirRequest);
     }
     if (sessionSetupRequest != null)
     {
         response = this.CreateTrans2SessionSetupFinalResponse(connection, sessionSetupRequest);
     }
     if (getDfsReferalRequest != null)
     {
         response = this.CreateTrans2GetDfsReferalFinalResponse(connection, getDfsReferalRequest,
             new RESP_GET_DFS_REFERRAL());
     }
     if (reportDfsRequest != null)
     {
         response = this.CreateTrans2ReportDfsInconsistencyFinalResponse(connection, reportDfsRequest);
     }
     return response;
 }
        /// <summary>
        /// to split a trans2 request to a trans2 request with trans2 secondary requests.
        /// </summary>
        /// <param name="trans2Request">the trans2 request packet to be split.</param>
        /// <param name="parameterCount">the parameter count with which to split the request packet.</param>
        /// <param name="dataCount">the data count with which to split the request packet.</param>
        /// <returns>a requests array of the split trans2 request with trans2 secondary requests.</returns>
        public SmbPacket[] CreateTrans2With2ndRequests(
            SmbTransaction2RequestPacket trans2Request,
            int parameterCount,
            int dataCount)
        {
            trans2Request.ToBytes();
            trans2Request.isDivided = true;
            int paramLength = 0;

            if (trans2Request.SmbData.Trans2_Parameters != null)
            {
                paramLength = trans2Request.SmbData.Trans2_Parameters.Length;
            }
            double paramNumber = Math.Ceiling((double)paramLength / parameterCount);
            int dataLength = 0;

            if (trans2Request.SmbData.Trans2_Data != null)
            {
                dataLength = trans2Request.SmbData.Trans2_Data.Length;
            }
            int dataNumber = (int)Math.Ceiling((double)dataLength / dataCount);
            double packetCount = paramNumber > dataNumber ? paramNumber : dataNumber;
            List<SmbPacket> packetList = new List<SmbPacket>();

            if (packetCount > 1)
            {
                byte[] trans2Parameters = trans2Request.SmbData.Trans2_Parameters;
                byte[] trans2Data = trans2Request.SmbData.Trans2_Data;

                SMB_COM_TRANSACTION2_Request_SMB_Data trans2SmbData = trans2Request.SmbData;

                if (paramLength > parameterCount)
                {
                    trans2SmbData.Trans2_Parameters = new byte[parameterCount];
                    Array.Copy(trans2Parameters, trans2SmbData.Trans2_Parameters, parameterCount);
                }

                if (dataLength > dataCount)
                {
                    trans2SmbData.Trans2_Data = new byte[dataCount];
                    Array.Copy(trans2Data, trans2SmbData.Trans2_Data, dataCount);
                }
                trans2Request.SmbData = trans2SmbData;
                trans2Request.UpdateCountAndOffset();
                packetList.Add(trans2Request);
                int currentIndex = 1;
                int remainedParamCount = trans2Request.SmbParameters.TotalParameterCount - parameterCount;
                int remainedDataCount = trans2Request.SmbParameters.TotalDataCount - dataCount;

                while (currentIndex < packetCount)
                {
                    SmbTransaction2SecondaryRequestPacket packet = new SmbTransaction2SecondaryRequestPacket();
                    SmbHeader header = trans2Request.SmbHeader;
                    header.Command = SmbCommand.SMB_COM_TRANSACTION2_SECONDARY;
                    packet.SmbHeader = header;

                    // Set Smb_Parameters
                    SMB_COM_TRANSACTION2_SECONDARY_Request_SMB_Parameters smbParameters =
                        new SMB_COM_TRANSACTION2_SECONDARY_Request_SMB_Parameters();
                    smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<
                        SMB_COM_TRANSACTION2_SECONDARY_Request_SMB_Parameters>(smbParameters) / NumBytesOfWord);
                    smbParameters.TotalParameterCount = trans2Request.SmbParameters.TotalParameterCount;
                    smbParameters.TotalDataCount = trans2Request.SmbParameters.TotalDataCount;
                    smbParameters.FID = trans2Request.FID;

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

                    if (remainedParamCount > parameterCount)
                    {
                        smbData.Trans2_Parameters = new byte[parameterCount];
                        Array.Copy(trans2Parameters, parameterCount * currentIndex, smbData.Trans2_Parameters, 0, parameterCount);
                        smbParameters.ParameterDisplacement = (ushort)(parameterCount * currentIndex);
                        remainedParamCount -= parameterCount;
                    }
                    else if (remainedParamCount > 0)
                    {
                        smbData.Trans2_Parameters = new byte[remainedParamCount];
                        Array.Copy(trans2Parameters, parameterCount * currentIndex, smbData.Trans2_Parameters, 0, remainedParamCount);
                        smbParameters.ParameterDisplacement = (ushort)(parameterCount * currentIndex);
                        remainedParamCount -= parameterCount;
                    }
                    else
                    {
                        smbData.Trans2_Parameters = new byte[0];
                    }

                    if (remainedDataCount > dataCount)
                    {
                        smbData.Trans2_Data = new byte[dataCount];
                        Array.Copy(trans2Data, dataCount * currentIndex, smbData.Trans2_Data, 0, dataCount);
                        smbParameters.DataDisplacement = (ushort)(dataCount * currentIndex);
                        remainedDataCount -= dataCount;
                    }
                    else if (remainedDataCount > 0)
                    {
                        smbData.Trans2_Data = new byte[remainedDataCount];
                        Array.Copy(trans2Data, dataCount * currentIndex, smbData.Trans2_Data, 0, remainedDataCount);
                        smbParameters.DataDisplacement = (ushort)(dataCount * currentIndex);
                        remainedDataCount -= dataCount;
                    }
                    else
                    {
                        smbData.Trans2_Data = new byte[0];
                    }

                    packet.SmbParameters = smbParameters;
                    packet.SmbData = smbData;
                    currentIndex++;
                    packet.UpdateCountAndOffset();
                    packetList.Add(packet);
                }
            }
            else
            {
                packetList.Add(trans2Request);
            }

            return packetList.ToArray();
        }
        public SmbTransaction2InterimResponsePacket CreateTransaction2InterimResponse(
            CifsServerPerConnection connection,
            SmbTransaction2RequestPacket request)
        {
            SmbTransaction2InterimResponsePacket response = new SmbTransaction2InterimResponsePacket();
            response.SmbHeader = CifsMessageUtils.CreateSmbHeader(connection, request);

            return response;
        }