[fpc-pascal] Asci85

Santiago A. svaa at ciberpiula.net
Fri Sep 16 10:16:55 CEST 2016


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.




More information about the fpc-pascal mailing list