Asci85

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Asci85

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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Ascii85

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

> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Asci85

José Mejuto
In reply to this post by Santiago A.
El 16/09/2016 a las 10:16, Santiago A. escribió:

Hello,

>      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!.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Asci85

Santiago A.
El 16/09/2016 a las 15:01, José Mejuto escribió:

>
> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal