/// <summary>
        /// Create  TRANS2_FIND_NEXT2 Response 
        /// </summary>
        /// <param name="connection">the connection identified the client</param>
        /// <param name = "searchCount">search count </param>
        /// <param name = "isEndOfSearch">whether end of search </param>
        /// <param name = "eaErrorOffset">eaError Offset </param>
        /// <param name = "lastNameOffset">last name offset </param>
        /// <param name = "data">data specified by [CIFS] </param>
        /// <returns>The SmbTrans2FindNext2ResponsePacket </returns>
        /// <exception cref="ArgumentNullException">connection must not be null</exception>
        public virtual SmbTrans2FindNext2ResponsePacket CreateTrans2FindNext2Response(
            SmbServerConnection connection,
            ushort searchCount,
            bool isEndOfSearch,
            ushort eaErrorOffset,
            ushort lastNameOffset,
            object data)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            SmbTrans2FindNext2ResponsePacket packet = new SmbTrans2FindNext2ResponsePacket();

            // get the request packet
            SmbPacket request = connection.GetRequestPacket(connection.MessageId);

            // create smb packet header
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(
                SmbCommand.SMB_COM_TRANSACTION2,
                connection.ProcessId, connection.MessageId, request.SmbHeader.Uid, request.SmbHeader.Tid,
                (SmbFlags)connection.Capability.Flag, (SmbFlags2)connection.Capability.Flags2);

            // update smb parameters
            SMB_COM_TRANSACTION2_FinalResponse_SMB_Parameters smbParameters = packet.SmbParameters;

            smbParameters.Setup = new ushort[0];

            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION2_FinalResponse_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // update smb data
            SMB_COM_TRANSACTION2_FinalResponse_SMB_Data smbData = packet.SmbData;

            // update trans2 param
            TRANS2_FIND_NEXT2_Response_Trans2_Parameters trans2Parameters = packet.Trans2Parameters;

            trans2Parameters.SearchCount = searchCount;
            trans2Parameters.EndOfSearch = (ushort)(isEndOfSearch ? 1 : 0);
            trans2Parameters.EaErrorOffset = eaErrorOffset;
            trans2Parameters.LastNameOffset = lastNameOffset;

            // update trans2 data
            TRANS2_FIND_NEXT2_Response_Trans2_Data trans2Data = packet.Trans2Data;

            trans2Data.Data = data;

            // store the parameters and data to packet.
            packet.Trans2Parameters = trans2Parameters;
            packet.Trans2Data = trans2Data;
            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;

            packet.UpdateCountAndOffset();

            return packet;
        }
        private SmbPacket CreateTransaction2ResponsePacket(SmbPacket request, SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            if (smbHeader.Status == 0 && channel.Peek <byte>(0) == 0 && channel.Peek <ushort>(1) == 0)
            {
                return(smbPacket);
            }

            SmbTransaction2RequestPacket transaction2Request = request as SmbTransaction2RequestPacket;

            if (transaction2Request == null)
            {
                return(smbPacket);
            }

            // if no setup command. break
            if (transaction2Request.SmbParameters.SetupCount == 0)
            {
                return(smbPacket);
            }

            // decode packet using the setup command
            switch ((Trans2SubCommand)transaction2Request.SmbParameters.Setup[0])
            {
            case Trans2SubCommand.TRANS2_QUERY_FILE_INFORMATION:
                SmbTrans2QueryFileInformationRequestPacket queryFileRequest =
                    transaction2Request as SmbTrans2QueryFileInformationRequestPacket;
                if (queryFileRequest != null)
                {
                    smbPacket = new SmbTrans2QueryFileInformationResponsePacket(
                        queryFileRequest.Trans2Parameters.InformationLevel);
                }
                break;

            case Trans2SubCommand.TRANS2_QUERY_PATH_INFORMATION:
                SmbTrans2QueryPathInformationRequestPacket queryPathRequest =
                    transaction2Request as SmbTrans2QueryPathInformationRequestPacket;
                if (queryPathRequest != null)
                {
                    smbPacket = new SmbTrans2QueryPathInformationResponsePacket(
                        queryPathRequest.Trans2Parameters.InformationLevel);
                }
                break;

            case Trans2SubCommand.TRANS2_SET_FILE_INFORMATION:
                smbPacket = new SmbTrans2SetFileInformationResponsePacket();
                break;

            case Trans2SubCommand.TRANS2_SET_PATH_INFORMATION:
                smbPacket = new SmbTrans2SetPathInformationResponsePacket();
                break;

            case Trans2SubCommand.TRANS2_QUERY_FS_INFORMATION:
                SmbTrans2QueryFsInformationRequestPacket queryFsRequest =
                    transaction2Request as SmbTrans2QueryFsInformationRequestPacket;
                if (queryFsRequest != null)
                {
                    smbPacket = new SmbTrans2QueryFsInformationResponsePacket(
                        queryFsRequest.Trans2Parameters.InformationLevel);
                }
                break;

            case Trans2SubCommand.TRANS2_SET_FS_INFORMATION:
                smbPacket = new SmbTrans2SetFsInformationResponsePacket();
                break;

            case Trans2SubCommand.TRANS2_FIND_FIRST2:
                SmbTrans2FindFirst2RequestPacket first2Request =
                    transaction2Request as SmbTrans2FindFirst2RequestPacket;
                if (first2Request != null)
                {
                    smbPacket = new SmbTrans2FindFirst2ResponsePacket(first2Request.Trans2Parameters.InformationLevel,
                                                                      (first2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                                                                      == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                }
                break;

            case Trans2SubCommand.TRANS2_FIND_NEXT2:
                SmbTrans2FindNext2RequestPacket next2Request =
                    transaction2Request as SmbTrans2FindNext2RequestPacket;
                if (next2Request != null)
                {
                    smbPacket = new SmbTrans2FindNext2ResponsePacket(next2Request.Trans2Parameters.InformationLevel,
                                                                     (next2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                                                                     == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                }
                break;

            case Trans2SubCommand.TRANS2_GET_DFS_REFERRAL:
                smbPacket = new SmbTrans2GetDfsReferralResponsePacket();
                break;

            default:
                break;
            }

            return(smbPacket);
        }
        private SmbPacket CreateTransaction2ResponsePacket(SmbPacket request, SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            if (smbHeader.Status == 0 && channel.Peek<byte>(0) == 0 && channel.Peek<ushort>(1) == 0)
            {
                return smbPacket;
            }

            SmbTransaction2RequestPacket transaction2Request = request as SmbTransaction2RequestPacket;
            if (transaction2Request == null)
            {
                return smbPacket;
            }

            // if no setup command. break
            if (transaction2Request.SmbParameters.SetupCount == 0)
            {
                return smbPacket;
            }

            // decode packet using the setup command
            switch ((Trans2SubCommand)transaction2Request.SmbParameters.Setup[0])
            {
                case Trans2SubCommand.TRANS2_QUERY_FILE_INFORMATION:
                    SmbTrans2QueryFileInformationRequestPacket queryFileRequest =
                        transaction2Request as SmbTrans2QueryFileInformationRequestPacket;
                    if (queryFileRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryFileInformationResponsePacket(
                            queryFileRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_QUERY_PATH_INFORMATION:
                    SmbTrans2QueryPathInformationRequestPacket queryPathRequest =
                       transaction2Request as SmbTrans2QueryPathInformationRequestPacket;
                    if (queryPathRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryPathInformationResponsePacket(
                            queryPathRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_SET_FILE_INFORMATION:
                    smbPacket = new SmbTrans2SetFileInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_SET_PATH_INFORMATION:
                    smbPacket = new SmbTrans2SetPathInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_QUERY_FS_INFORMATION:
                    SmbTrans2QueryFsInformationRequestPacket queryFsRequest =
                        transaction2Request as SmbTrans2QueryFsInformationRequestPacket;
                    if (queryFsRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryFsInformationResponsePacket(
                            queryFsRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_SET_FS_INFORMATION:
                    smbPacket = new SmbTrans2SetFsInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_FIND_FIRST2:
                    SmbTrans2FindFirst2RequestPacket first2Request =
                       transaction2Request as SmbTrans2FindFirst2RequestPacket;
                    if (first2Request != null)
                    {
                        smbPacket = new SmbTrans2FindFirst2ResponsePacket(first2Request.Trans2Parameters.InformationLevel,
                            (first2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                            == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                    }
                    break;

                case Trans2SubCommand.TRANS2_FIND_NEXT2:
                    SmbTrans2FindNext2RequestPacket next2Request =
                       transaction2Request as SmbTrans2FindNext2RequestPacket;
                    if (next2Request != null)
                    {
                        smbPacket = new SmbTrans2FindNext2ResponsePacket(next2Request.Trans2Parameters.InformationLevel,
                            (next2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                            == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                    }
                    break;

                case Trans2SubCommand.TRANS2_GET_DFS_REFERRAL:
                    smbPacket = new SmbTrans2GetDfsReferralResponsePacket();
                    break;

                default:
                    break;
            }

            return smbPacket;
        }
 /// <summary>
 /// Verify whether the two response are equal or not
 /// </summary>
 /// <param name="smbTrans2FindNext2ResponsePacket1"> The SmbTrans2FindNext2ResponsePacket For First</param>
 /// <param name="smbTrans2FindNext2ResponsePacket2"> The SmbTrans2FindNext2ResponsePacket Response For Second</param>
 private bool VerifySmbTrans2FindNext2ResponsePacket(
          SmbTrans2FindNext2ResponsePacket smbTrans2FindNext2ResponsePacket1,
          SmbTrans2FindNext2ResponsePacket smbTrans2FindNext2ResponsePacket2)
 {
     return (smbTrans2FindNext2ResponsePacket1.IsSignRequired == smbTrans2FindNext2ResponsePacket2.IsSignRequired)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.PacketBytes,
         smbTrans2FindNext2ResponsePacket2.PacketBytes)
         && (smbTrans2FindNext2ResponsePacket1.PacketType ==
         smbTrans2FindNext2ResponsePacket2.PacketType)
         && (smbTrans2FindNext2ResponsePacket1.SmbData.ByteCount ==
         smbTrans2FindNext2ResponsePacket2.SmbData.ByteCount)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.SmbData.Pad1,
         smbTrans2FindNext2ResponsePacket2.SmbData.Pad1)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.SmbData.Pad2,
         smbTrans2FindNext2ResponsePacket2.SmbData.Pad2)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.SmbData.Trans2_Data,
         smbTrans2FindNext2ResponsePacket2.SmbData.Trans2_Data)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.SmbData.Trans2_Parameters,
         smbTrans2FindNext2ResponsePacket2.SmbData.Trans2_Parameters)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.DataCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.DataCount)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.DataDisplacement ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.DataDisplacement)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.DataOffset ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.DataOffset)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.ParameterCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.ParameterCount)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.ParameterDisplacement ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.ParameterDisplacement)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.ParameterOffset ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.ParameterOffset)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.Reserved1 ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.Reserved1)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.Reserved2 ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.Reserved2)
         && CompareArrayEquals(
         smbTrans2FindNext2ResponsePacket1.SmbParameters.Setup,
         smbTrans2FindNext2ResponsePacket2.SmbParameters.Setup)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.SetupCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.SetupCount)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.TotalDataCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.TotalDataCount)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.TotalParameterCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.TotalParameterCount)
         && (smbTrans2FindNext2ResponsePacket1.SmbParameters.WordCount ==
         smbTrans2FindNext2ResponsePacket2.SmbParameters.WordCount);
 }