Dont known about base64 but you should protect resources with try..finally
blocks:

S1 := TStringStream.Create(S);
try
  S2 := TStringStream.Create(S);
  try
    ...Use resources...
  finally
    S2.Free;
  end;
finally
  S1.Free;
end;

Also, why "DoNothing" procedure, just left except block empty:

        try
          ...
        except
          // Ignore (eat) exception
        end;

Of course ignoring exceptions is a very bad practice because many times lead
to hard to find bugs and problems.

And again, you should try binary blobs instead of such unnecessary
conversions.

Regards.

-----Mensaje original-----
De: A.J. Venter [mailto:[EMAIL PROTECTED] 
Enviado el: MiƩrcoles, 16 de Mayo de 2007 01:08 p.m.
Para: lazarus@miraclec.com
Asunto: Re: [lazarus] Image from SQL

So - second attempt at implementing it... same sad result - only now
the XPM fails as well...

This is my now somewhat improved code... am I missing something
extremely obvious here ?
Function DecodeB64(S : String) : String;
Procedure doNothing;
Begin
end;
var
  b64decoder: TBase64DecodingStream;
  S1 : TSTringStream;
  S2 : TStringStream;
begin
 S1 := TStringStream.Create(S);
 S2 := TStringStream.Create('');
  b64decoder := TBase64DecodingStream.Create(S1);
  With B64Decoder Do
        Begin
        try
                S2.CopyFrom(B64Decoder,B64Decoder.Size);
        Except
                DoNothing;
        end;
        end;

    Result := S2.DataString;
  S1.Free;
  S2.Free;
  b64decoder.Free;
end;

I'm stumped as to what it is I am doing wrong...
Any ideas ?

A.J.
On 5/16/07, A.J. Venter <[EMAIL PROTECTED]> wrote:
> A small update -
> Don't ask me WHY - but get this - I take the exact same unchanged
> code, and insert an XPM image.
> It works perfectly.
> I try a jpg or a png - and it fails.
>
> All I can imagine is that my b64decode function is broken - and that
> the breakage is ONLY affecting true binary sources, XPM (like my test
> program) is actually an ASCII format - so it survives the process
> intact - while png etc. fails.
>
> So - I guess I start hacking my b64decode function.
>
> A.J.
>
> On 5/16/07, A.J. Venter <[EMAIL PROTECTED]> wrote:
> > Hi again all
> > Based on everyone's advice, I chose to go with base64 encoding - and
> > started coding.
> >
> > Right now I am quite successfully:
> > 1) Reading the image
> > 2) Encoding it to base64 (using the encodeb64 function from the
examples)
> > 3) Storing the result in the database (I THINK it's storing right)
> >
> > Later I then select it, run it through decodeb64 (my own function
> > based on the above, code below) and then save this string to a file -
> > a file that has the right extension (the reason for this is actually
> > good - remote DB access, and I want to cache the images and only
> > download if they changed)
> > Finally - a simple TImage.Picture.LoadFromFile is meant to handle the
> > last bit... only it doesn't - somewhere along the way my data is
> > corrupted - the files being dumped differ from the orriginals - yet
> > oddly they are the exact same size.
> > I know we're not supposed to include attachments on the list, so for
> > now I didn't but if I may, I would like to add (assuming anybody says
> > it would be usefull:
> > 1) An orriginal image
> > 2) A post database process filedump of the same image
> > 3) The (apparently) base64 encoded database entry for said image
> >
> > I can say that I tested my b64encode/b64decode functions with a simple
> > program that lets me enter string, encodes it, writes that, decodes
> > THAT and writes it back (which should match the orriginal input) -
> > that at least seems to work fine.
> >
> > Any idea where the corruption is coming from ? All help, as always,
> > greatly appreciated.
> >
> > Relevant code snippets:
> > Function DecodeB64(S : String) : String;
> > Procedure doNothing;
> > Begin
> > end;
> > var
> >   b64decoder: TBase64DecodingStream;
> >   S1 : TSTringStream;
> >   S2 : String;
> >   InputStream: TStream;
> >   IsEnd: Boolean;
> >
> > begin
> >  S1 := TStringStream.Create(S);
> >  S2 := '';
> >   b64decoder := TBase64DecodingStream.Create(S1);
> > While not B64Decoder.EOF do
> >         Begin
> >         try
> >                 S2 := S2+(CHR(B64Decoder.ReadByte));
> >         except
> >                 doNothing;
> >         end;
> >         end;
> >
> >     Result := S2;
> >   b64decoder.Free;
> > end;
> >
> > Image Saving:
> > Procedure SaveBTNClick(Sender:TObject); //Snipped
> > Var B64 : String;
> >     S :TStringList;
> > Begin
> >       S := TStringList.Create;
> >       S.LoadFromFile(ImgFileName);
> >       B64 := EncodeB64(S.Text);
> >      Ad.SaveData(add_desc.text,b64);
> > end;
> >
> >
> > Procedure TZC_Advertising.SaveData(Description,Data:String);
> > Var Q : String;
> > Begin
> >  Q := 'UPDATE advertising SET data='+QS(Data)+'
> > timestamp='+QS('NOW')+' WHERE description = '+QS(Description)+' ;';
> >    FResult := ZC_DBQuery(Q);
> > //Notes: FResult is a PPGResult, ZC_DBQuery is a very well tested
> > wrapper function
> > //QS = QuoteString - it wraps the input in ''
> > end;
> >
> > Image Loading:
> > Function TZC_Advertising.GetData(Description:String):String;
> > Var
> >     S : String;
> > Begin
> > FResult := ZC_DBQuery('SELECT data FROM advertising WHERE description
> > = '+QS(Description)+';');
> > If PqNTuples(Fresult) >= 0 then
> > begin
> >    S := PQGetValue(FResult,0,0);
> >    GetData:=DecodeB64(S);
> > end else
> >    GetData := '';
> > end;
> >
> > //In the app:
> > IName :=
GetTempDir+Ad.GetTimeStamp(add_desc.text)+Ad.GetExt(add_desc.text);
> > Data := TStringList.Create;
> > Data.Add(Ad.GetData(add_desc.text));
> > Data.SaveToFile(Iname);
> > ad_Img.Picture.LoadFromFile(Iname);
> > Data.Free;
> >
> > --
> > A.J. Venter
> > CEO - OutKast Solutions C.C.
> > http://www.outkastsolutions.co.za
> > Cell: +27 83 455 9978
> > Fax: +27 21 413 2800
> > Office: +27 21 591 6766
> >
>
>
> --
> A.J. Venter
> CEO - OutKast Solutions C.C.
> http://www.outkastsolutions.co.za
> Cell: +27 83 455 9978
> Fax: +27 21 413 2800
> Office: +27 21 591 6766
>


-- 
A.J. Venter
CEO - OutKast Solutions C.C.
http://www.outkastsolutions.co.za
Cell: +27 83 455 9978
Fax: +27 21 413 2800
Office: +27 21 591 6766

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to