Recent

Author Topic: DecodeStringBase64() - could not understand output  (Read 8660 times)

ertank

  • Sr. Member
  • ****
  • Posts: 274
Re: DecodeStringBase64() - could not understand output
« Reply #15 on: April 22, 2018, 05:12:14 pm »
@ertank, I can't test but try:
Code: Pascal  [Select][+][-]
  1.       TEncoding.FreeEncodings;
  2.       e := TEncoding.GetEncoding(DefaultSystemCodePage);

I am not sure what you need me to do. My initial code which has a problem is as following. Would you modify it the way I should do your test? Please note that I currently do not use LazUTF8 unit anywhere in my project. It is actually targeting arm-wince platform.
Code: Pascal  [Select][+][-]
  1. uses
  2.   Base64;
  3.  
  4. var
  5.   ct1: array [1..7] of Char;  // Using AnsiChar do not help either
  6. begin
  7.   ct1 := DecodeStringBase64('7EFBZP6kPw==');
  8.   // ct1 end up with following information in debug value check
  9.   // #195 #172 A A d #197 #159
  10. end;
  11.  

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: DecodeStringBase64() - could not understand output
« Reply #16 on: April 22, 2018, 07:12:55 pm »
I thought WinCE is all Widechar based?
The only true wisdom is knowing you know nothing

ertank

  • Sr. Member
  • ****
  • Posts: 274
Re: DecodeStringBase64() - could not understand output
« Reply #17 on: April 22, 2018, 07:56:26 pm »
I thought WinCE is all Widechar based?
I don't know its character base. However, I was happy using string and char all over my project in Lazarus. There was no problem of Base64 encode/decode and AES encypt/decrypt operations.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: DecodeStringBase64() - could not understand output
« Reply #18 on: April 22, 2018, 08:45:54 pm »
btw, that code you are having issues with works fine on my Win 10 PC in GUI mode using the 32 bit compiler.

Did not try the 64 bit

could you try WideString/WideChar ?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: DecodeStringBase64() - could not understand output
« Reply #19 on: April 22, 2018, 09:08:06 pm »
btw, that code you are having issues with works fine on my Win 10 PC in GUI mode using the 32 bit compiler.
Jamie, we know.
I explained already..
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: DecodeStringBase64() - could not understand output
« Reply #20 on: April 22, 2018, 09:20:18 pm »
Well, you seem to be the one with all the answers..
The only true wisdom is knowing you know nothing

ezlage

  • Guest
Re: DecodeStringBase64() - could not understand output
« Reply #21 on: April 22, 2018, 09:42:43 pm »
Try to use base64 stream and string stream to regenerate the base64 value, passing to StringStream other types of string (utf8string, ansistring, widestring, etc):
Code: Pascal  [Select][+][-]
  1. procedure EncodeStreamBase64(AStream: TStream);
  2. var
  3.   Encoder: TBase64EncodingStream=nil;
  4.   Swap: TMemoryStream=nil;
  5. begin
  6.   if Assigned(AStream) and (AStream.Size>%0)
  7.     then begin
  8.       Swap:=TMemoryStream.Create;
  9.       Swap.LoadFromStream(AStream);
  10.       AStream.Size:=%0;
  11.       Swap.Position:=%0;
  12.       Encoder:=TBase64EncodingStream.Create(AStream);
  13.       Encoder.CopyFrom(Swap,Swap.Size);
  14.       FreeAndNil(Encoder);
  15.       FreeAndNil(Swap);
  16.     end;
  17. end;

Decode with:
Code: Pascal  [Select][+][-]
  1. procedure DecodeStreamBase64(AStream: TStream);
  2. var
  3.   Decoder: TBase64DecodingStream=nil;
  4.   Swap: TMemoryStream=nil;
  5. begin
  6.   if Assigned(AStream) and (AStream.Size>%0)
  7.     then begin
  8.       Swap:=TMemoryStream.Create;
  9.       Swap.LoadFromStream(AStream);
  10.       AStream.Size:=%0;
  11.       Swap.Position:=%0;
  12.       Decoder:=TBase64DecodingStream.Create(Swap);
  13.       AStream.CopyFrom(Decoder,Decoder.Size);
  14.       FreeAndNil(Decoder);
  15.       FreeAndNil(Swap);
  16.     end;
  17. end;


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   cData: string='7EFBZP6kPw=='; //replace for the regenerated base64 value, try another types too, before regenerate (string, utf8string, ansistring, widestring, etc)
  4. var
  5.   StrStream: TStringStream=nil;
  6. begin
  7.   StrStream:=TStringStream.Create(cData);
  8.   DecodeStreamBase64(StrStream);
  9.   ShowMessage(StrStream.DataString);
  10.   FreeAndNil(StrStream);
  11. end;
« Last Edit: April 22, 2018, 09:58:58 pm by ezlage »

ezlage

  • Guest
Re: DecodeStringBase64() - could not understand output
« Reply #22 on: April 22, 2018, 10:03:25 pm »
Hello,

I have following Base64 encoded text:
Code: [Select]
7EFBZP6kPw==
It should normally decode into following bytes hex/decimal (as to my check on online base64 decoding to hex pages):
Code: [Select]
hex: ec 41 41 64 fe a4 3f
dec: 236 65 65 100 254 164 63

I could not manage to get that decoded data using a code like following:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Base64;
  3.  
  4. var
  5.   ct1: array [1..7] of Char;  // Using AnsiChar do not help either
  6. begin
  7.   ct1 := DecodeStringBase64('7EFBZP6kPw==');
  8.   // ct1 end up with following information in debug value check
  9.   // #195 #172 A A d #197 #159
  10. end;
  11.  
I am using following fpc and lazarus versions:
Code: [Select]
fpc 3.1.1 from trunk SVN revision: 38798
lazarus 1.8.3 from branches/fixes_1_8 SVN revision: 57681

I appreciate any help as I stuck to get that right.

Thanks & regards,
Ertan

Except if the array was the input that generated the base64 value, the position zero have information about how to interpret another positions. What happens if you use array[0..7] and at read it ignore first or last position?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: DecodeStringBase64() - could not understand output
« Reply #23 on: April 23, 2018, 08:11:00 pm »
@ertank, I can't test but try:
Code: Pascal  [Select][+][-]
  1.       TEncoding.FreeEncodings;
  2.       e := TEncoding.GetEncoding(DefaultSystemCodePage);

I am not sure what you need me to do. My initial code which has a problem is as following. Would you modify it the way I should do your test? Please note that I currently do not use LazUTF8 unit anywhere in my project. It is actually targeting arm-wince platform.

You don't have to use LazUTF8 directly. It is enough to have it in your project through some other unit. Since your project is a GUI application, LazUTF8 is included.

Now there is another bug related to TEncoding. To make sure simply do the following:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Base64;
  3.  
  4. var
  5.   ct1: array [1..7] of Char;  // Using AnsiChar do not help either
  6. begin
  7. {//Before: These two are not identical
  8.   WriteLn('DefaultSystemCodePage: ', DefaultSystemCodePage);  //65001
  9.   WriteLn('TEncoding.Default: ',TEncoding.Default.EncodingName);//Windows-1254
  10. }
  11.  
  12.   TEncoding.FreeEncodings;
  13.   TMBCSEncoding.Create;
  14.  
  15. {//After: These two should be identical
  16.   WriteLn('DefaultSystemCodePage: ', DefaultSystemCodePage);//65001
  17.   WriteLn('TEncoding.Default: ',TEncoding.Default.EncodingName);//UTF8
  18. }
  19.   ct1 := DecodeStringBase64('7EFBZP6kPw==');
  20.   // ct1 end up with following information in debug value check
  21.   // #195 #172 A A d #197 #159
  22. end;

 

TinyPortal © 2005-2018