[Lazarus-es] Problema con Base64

Antonio Evora aevorar en gmail.com
Dom Dic 11 01:49:05 CET 2016


Estimados compañeros.

Tengo código para codificar un stream usando base64 y para 
decodificarlo. Usando el método TnxcxEncoding.TestDataEncoding se hace 
una codificación en base64 de un stream "controlado" (una cadena fija de 
solo erres) y el resultado, si el tamaño del stream es inferior a 256 
bytes, es correcto, pero si es superior faltan caracteres en el stream 
resultante (comprobado con una utilidad externa). No le encuentro lógica 
alguna.

Si alguien tiene alguna idea será bienvenida.

Un cordial saludo y gracias de antemano.

El código es:

unit cEncodingDecoding;

{$mode objfpc}{$H+}

interface

uses
   Classes, SysUtils;

type

   { TnxcxEncoding }

   TnxcxEncoding = class (TObject)
   public
     class function TestDataEncoding(): boolean;
     class function StreamToBase64(aStream: TStream): string;
   end;

   { TnxcxDecoding }

   TnxcxDecoding = class (TObject)
   public
     class function TestDatadecoding(): boolean;
     class function Base64ToStream(Base64: string; var aStream: 
TStream): Boolean;
   end;

implementation

uses base64, Forms, Clipbrd;

{ TnxcxDecoding }

class function TnxcxDecoding.TestDatadecoding: boolean;
var
   Texto: string;
   TextoEncoded: string;
   aStream: TStringStream;
   Base64Str: string;
   n: integer;
begin
   Texto:='';
   for n:=1 to 125840 do //257 do //125840 do
   begin
     Texto:=Texto+'r';
   end;

   TextoEncoded:='';
   for n:=1 to 41946 do //257 do //125840 do
   begin
     TextoEncoded:=TextoEncoded+'cnJy';
   end;
   TextoEncoded:=TextoEncoded+'cnI=';

   aStream:=TStringStream.Create(Texto);
   try
     Base64Str:=TnxcxEncoding.StreamToBase64(aStream);
Application.MessageBox(pchar(inttostr(Length(Base64Str))+LineEnding+LineEnding+inttostr(Length(TextoEncoded))),'');
Application.MessageBox(pchar(StringReplace(TextoEncoded,Base64Str,'',[])),'');
     aStream.Position:=0;
     aStream.Size:=0;
     Base64ToStream(Base64Str,TStream(aStream));
Application.MessageBox(pchar(inttostr(Length(aStream.DataString))+LineEnding+LineEnding+inttostr(Length(Texto))),'');
Application.MessageBox(pchar(StringReplace(Texto,aStream.DataString,'',[])),'');
   finally
     aStream.Free;
   end;

Application.MessageBox(pchar(inttostr(Length(DecodeStringBase64(EncodeStringBase64(Texto))))),'');
end;

class function TnxcxDecoding.Base64ToStream(Base64: string; var aStream: 
TStream
   ): Boolean;
var
   EncodedStream: TStringStream;
   Base64Decoder: TBase64DecodingStream;
begin
   Result:=False;

   EncodedStream:=TStringStream.Create(Base64);
   try
     try
       if EncodedStream.Size>0 then
       begin
Base64Decoder:=TBase64DecodingStream.Create(EncodedStream);
         try
           aStream.CopyFrom(Base64Decoder,Base64Decoder.Size);

           Result:=True;
         finally
           Base64Decoder.Free;
         end;
       end
       else
       begin
         Result:=True;
       end;
     except
       on e: Exception do
       begin
         exit;
       end;
     end;
   finally
     EncodedStream.Free;
   end;
end;

{ TnxcxEncoding }

class function TnxcxEncoding.TestDataEncoding: boolean;
begin

end;

class function TnxcxEncoding.StreamToBase64(aStream: TStream): string;
const
   BUFFERSIZE=4096;
var
   EncodedStream: TStringStream;
   Base64Encoder: TBase64EncodingStream;
   //aBuffer: array [0..BUFFERSIZE-1] of Byte;
   //Leido: LongInt;
   //TotalLeido: LongInt;
begin
   Result:='';

   EncodedStream:=TStringStream.Create('');
   try
     Base64Encoder:=TBase64EncodingStream.Create(EncodedStream);
     try
       //TotalLeido:=0;
       //
       //repeat
       //  Leido:=aStream.Read(aBuffer,BUFFERSIZE);
       //
       //  if Leido>0 then
       //  begin
       //    TotalLeido:=TotalLeido+Leido;
       //
       //    Base64Encoder.Write(aBuffer,Leido);
       //  end;
       //until Leido<BUFFERSIZE;

       Base64Encoder.CopyFrom(aStream,aStream.Size);

       //Clipboard.AsText:=inttostr(aStream.Size)+' - 
'+inttostr(Length(EncodedStream.DataString))+' (167788)';

       Result:=EncodedStream.DataString;
     finally
       Base64Encoder.Free;
     end;
   finally
     EncodedStream.Free;
   end;
end;

end.



Más información sobre la lista de distribución Lazarus-es