Re: ConnectionIntercept stream question

Giganews Newsgroups
Subject: Re: ConnectionIntercept stream question
Posted by:  Remy Lebeau (TeamB) (gambit47.no.spam@no.spam.yahoo.com)
Date: Sun, 14 Dec 2003

"Jack Mays" <jackmays…@SPAMsupplant.net> wrote in message
news:3A85962A3B8AE240jackmays…@SPAMsupplant.net...

> The problem is i can't seem to erase what's in AStream
> so that i can rewite it with the decrypted data..

You don't havet to "erase" the stream.  You can simply seek to the beginning
of the stream and write your new data to it, which will automatically
overwrite whatever data is already there.  If the new data is smaller in
size than the original data size, then set the stream's Size property to
shrink the stream down to free any remaining data from the original content.
The alternative, if you really do want to "erase" the original content
before writing your own, is to just set the stream's Size to 0 first, then
write your new data as needed.

Try this code instead:

    procedure TCustomPeerThread.InterceptReceive(ASender:
TIdConnectionIntercept; AStream: TStream);
    var
        Client : TClientInfo;
        DataStrm: TStringStream;
        encString, decString: String;

    begin
        Client := TClientInfo(Data);

        DataStrm := TStringStream.Create('');
        try
            DataStrm.CopyFrom(AStream, 0);

            encString := Trim(Data.DataString);
            decString := DecryptData(encString, Client.SymKey,
Client.SymKeyType, Client.SymKeyBitSize, Client.RSAProcessed,
Client.FirstReceived);

            DataStrm.Position := 0;
            DataStrm.WriteString(decString);
            DataStrm.Size := Length(decString);
            // alternatively...
            // DataStrm.Size := 0;
            // DataStrm.WriteString(decString);

            AStream.Position := 0;
            AStream.CopyFrom(DataStrm, 0);
            AStream.Size := DataStrm.Size;
            AStream.Position := 0;
            // alternatively...
            // AStream.Size := 0;
            // AStream.CopyFrom(DataStrm, 0);
            // AStream.Position := 0;

        finally
            FreeAndNil(Data);
        end;
    end;

Gambit

Replies

In response to

ConnectionIntercept stream question posted by Jack Mays on Sun, 14 Dec 2003