/// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTransactionSecondaryRequestPacket(SmbTransactionSecondaryRequestPacket 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.ParameterCount = packet.SmbParameters.ParameterCount;
            this.smbParameters.ParameterOffset = packet.SmbParameters.ParameterOffset;
            this.smbParameters.ParameterDisplacement = packet.SmbParameters.ParameterDisplacement;
            this.smbParameters.DataCount = packet.SmbParameters.DataCount;
            this.smbParameters.DataOffset = packet.SmbParameters.DataOffset;
            this.smbParameters.DataDisplacement = packet.SmbParameters.DataDisplacement;

            this.smbData.ByteCount = packet.SmbData.ByteCount;
            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.Trans_Parameters != null)
            {
                this.smbData.Trans_Parameters = new byte[packet.smbData.Trans_Parameters.Length];
                Array.Copy(packet.smbData.Trans_Parameters,
                    this.smbData.Trans_Parameters, packet.smbData.Trans_Parameters.Length);
            }
            else
            {
                this.smbData.Trans_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.Trans_Data != null)
            {
                this.smbData.Trans_Data = new byte[packet.smbData.Trans_Data.Length];
                Array.Copy(packet.smbData.Trans_Data, this.smbData.Trans_Data, packet.smbData.Trans_Data.Length);
            }
            else
            {
                this.smbData.Trans_Data = new byte[0];
            }
        }
Example #2
0
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTransactionSecondaryRequestPacket(SmbTransactionSecondaryRequestPacket 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.ParameterCount        = packet.SmbParameters.ParameterCount;
            this.smbParameters.ParameterOffset       = packet.SmbParameters.ParameterOffset;
            this.smbParameters.ParameterDisplacement = packet.SmbParameters.ParameterDisplacement;
            this.smbParameters.DataCount             = packet.SmbParameters.DataCount;
            this.smbParameters.DataOffset            = packet.SmbParameters.DataOffset;
            this.smbParameters.DataDisplacement      = packet.SmbParameters.DataDisplacement;

            this.smbData.ByteCount = packet.SmbData.ByteCount;
            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.Trans_Parameters != null)
            {
                this.smbData.Trans_Parameters = new byte[packet.smbData.Trans_Parameters.Length];
                Array.Copy(packet.smbData.Trans_Parameters,
                           this.smbData.Trans_Parameters, packet.smbData.Trans_Parameters.Length);
            }
            else
            {
                this.smbData.Trans_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.Trans_Data != null)
            {
                this.smbData.Trans_Data = new byte[packet.smbData.Trans_Data.Length];
                Array.Copy(packet.smbData.Trans_Data, this.smbData.Trans_Data, packet.smbData.Trans_Data.Length);
            }
            else
            {
                this.smbData.Trans_Data = new byte[0];
            }
        }
        /// <summary>
        /// to split a trans request to a trans request with trans secondary requests.
        /// </summary>
        /// <param name="transRequest">the trans 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 trans request with trans secondary requests.</returns>
        /// <exception cref="System.ArgumentNullException">the transRequest must not be null.</exception>
        public SmbPacket[] CreateTransWith2ndRequests(
            SmbTransactionRequestPacket transRequest,
            int parameterCount,
            int dataCount)
        {
            if (transRequest == null)
            {
                throw new ArgumentNullException("transRequest");
            }

            transRequest.ToBytes();
            transRequest.isDivided = true;

            int paramLength = 0;

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

            if (transRequest.SmbData.Trans_Data != null)
            {
                dataLength = transRequest.SmbData.Trans_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[] transParameters = transRequest.SmbData.Trans_Parameters;
                byte[] transData = transRequest.SmbData.Trans_Data;
                SMB_COM_TRANSACTION_Request_SMB_Data transSmbData = transRequest.SmbData;

                if (paramLength > parameterCount)
                {
                    transSmbData.Trans_Parameters = new byte[parameterCount];
                    Array.Copy(transParameters, transSmbData.Trans_Parameters, parameterCount);
                }

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

                while (currentIndex < packetCount)
                {
                    SmbTransactionSecondaryRequestPacket packet = new SmbTransactionSecondaryRequestPacket();
                    SmbHeader header = transRequest.SmbHeader;
                    header.Command = SmbCommand.SMB_COM_TRANSACTION_SECONDARY;
                    packet.SmbHeader = header;

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

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

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

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

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

            return packetList.ToArray();
        }