Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-09

Asci85

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Santiago A.

Hello:

I'm trying to use ascii85 packages to decode/encode strings. But I get this weird results:

--------------
Original Plain: 123456
Correct Encoded: <~0etOA2)Y~>

Encode 123456 = <~0etOA
Decode <~0etOA = 1234

Decode <~0etOA2)Y~> = 123456
--------------

It looks like decoder stops reading somewhere.
I'm using almost a verbatim copy of the packages example, but replacing TFileStreams by TStringStreams:

Here is the code,

---------------------

program Project1;
uses sysutils,ascii85,classes;
function Encode85(Plain: String): String;
var
 StrmIn,StrmOut:TStringStream;
 StrmEnc85:TASCII85EncoderStream;
begin
 StrmIn:=TStringStream.Create(Plain);
 try
   StrmOut:=TStringStream.Create('');
   try
     StrmEnc85:=TAscii85EncoderStream.Create(StrmOut,72,True);
     try
       StrmEnc85.CopyFrom(StrmIn,0);
       Result:=StrmOut.DataString;
     finally
       StrmEnc85.Free;
     end;
   finally
     StrmOut.Free;
   end;
 finally
   StrmIn.Free;
 end;
end;
function Decode85(Encoded: string): string;
var
 StrmIn,StrmOut:TStringStream;
 StrmDec85:TASCII85DecoderStream;
 buf:array [1..16] of byte;
 Count: LongInt;
begin
 StrmIn:=TStringStream.Create(Encoded);
 StrmDec85:=TASCII85DecoderStream.Create(StrmIn);
 try
   StrmOut:=TStringStream.Create('');
   try
     Repeat
       Count:=StrmDec85.Read(Buf,SizeOf(Buf));
       If Count>0 then
         StrmOut.WriteBuffer(Buf,Count);
     Until (Count<SizeOf(Buf));
     Result:=StrmOut.DataString;
   Finally
     StrmOut.Free;
   end;
 finally
   StrmDec85.Free;
 end;
end;
Const
  OriginalPlain='123456';
  CorrectEncoded='<~0etOA2)Y~>';
var
 Encoded,Decoded:string;
begin
 Writeln('Original Plain: ',OriginalPlain);
 Writeln('Correct Encoded: ',CorrectEncoded);
 writeln;
 Encoded:=Encode85(OriginalPlain);
 Writeln(Format('Encode %s = %s',[OriginalPlain,Encoded]));
 Decoded:=Decode85(Encoded);
 Writeln(Format('Decode %s = %s',[Encoded,Decoded]));
 Writeln;
 Encoded:='<~0etOA2)Y~>';
 Decoded:=Decode85(Encoded);
 Writeln(Format('Decode %s = %s',[Encoded,Decoded]));
 Writeln;
 readln;
end.

---------------------

Saludos Santiago A.

Reply by email

Santiago A.
to Santiago A.
Santiago A. wrote 16 lines
Hello: I'm trying to use ascii85 packages to decode/encode strings. But I get this weird results: -------------- Original Plain: 123456 Correct Encoded: <~0etOA2)Y~> Encode 123456 = <~0etOA Decode <~0etOA = 1234 Decode <~0etOA2)Y~> = 123456 -------------- It looks like decoder stops reading somewhere.

Sorry, I meant "Encoder" stops reading somewhere

Saludos Santiago A.

Reply by email

José Mejuto
to Santiago A.: Hello: I'm trying to use ascii85 packages to decode/encode strings. But I get this weird…

El 16/09/2016 a las 10:16, Santiago A. escribió:

Hello,

Show quoted text 6 lines
StrmEnc85:=TAscii85EncoderStream.Create(StrmOut,72,True); try StrmEnc85.CopyFrom(StrmIn,0); Result:=StrmOut.DataString; finally StrmEnc85.Free;

Maybe there is something wrong in ASCII85 encoder, but this code is wrong, it should be something like:

       try
         StrmEnc85.CopyFrom(StrmIn,0);
         StrmEnc85.Free;
         Result:=StrmOut.DataString;
       finally

Because stream must be flushed before extracting result and the flush is performed in the destroy.

Saludos!.

Reply by email

Santiago A.
to José Mejuto
José Mejuto wrote 13 lines
Maybe there is something wrong in ASCII85 encoder, but this code is wrong, it should be something like: try StrmEnc85.CopyFrom(StrmIn,0); StrmEnc85.Free; Result:=StrmOut.DataString; finally Because stream must be flushed before extracting result and the flush is performed in the destroy. .

That's right.

Solved! Thanks!

Saludos Santiago A.

Reply by email