Jump to content
Turan Can

Andorid Char xor is this a delphi bug or is it my fault?

Recommended Posts

Hi,


No one left here from the old great masters 🙂


Windows is running smoothly.
I think there is a very serious bug on Android.
in trouble numbers. I did not see any problems in the alphabet, but I did not look at what the ascii is.


Step to step output data
1 xor 0 > #1
2 xor 0 > #2
3 xor 0 > #3
4 xor 0 > #4
5 xor 0 > #5
6 xor 0 > #6
7 xor 0 > #7
//these two lines don't. instead "" gives it.
8 xor 0 > #1 -- 8>#10 why.....
9 xor 0 > #1 -- 9>'t'
1 xor 0 > #1
2 xor 0 > #2
3 xor 0 > #3
4 xor 0 > #4
5 xor 0 > #5
6 xor 0 > #6
7 xor 0 > #7

 

Sample code
procedure TForm1.Button5Click(Sender: TObject);
var
  I, Len: Integer;
  S, T, D, Buff: UTF8String;
begin
  Len := 16;
  SetLength(S, Len);
  SetLength(T, Len);
  SetLength(D, Len);
  S := '1234567891234567';
  T := '0000000000000000';
  D := T;
  XORBuffers(S[1], T[1], Len, D[1]);
  // XORBuffers(S[0],T[0],Len,D[0]);
end;

 

procedure TForm1.XORBuffers(const Source1, Source2; Size: Integer; var Dest);
var
  I: Integer;
  S, T,D:PByte;
😄Byte;
  Buff: Binary;
begin
  S := PByte(@Source1);
  T := PByte(@Source2);
  D := PByte(@Dest);
  for I := 0 to Size - 1 do
  begin
    C := Byte(S xor T);
    D := Byte(C);
  end;
  SetString(Buff, PAnsiChar(@D[0]), Size);
end;

Edited by Turan Can

Share this post


Link to post

What is this code meant to be doing?  As presented here it won't compile:

 

C := Byte(S xor T);

 

You can't use xor with pointers.

 

If you are hoping to perform bytewise xor on a UTF8 buffer and then interpret that as a string, that won't work. You can't guarantee that the output will be valid in any text encoding.

 

You talk about operations on Char, but you aren't using Char. Char is a UTF-16 character element.  You are (I think) attempting to operate on bytes.

 

It's pointless to call SetLength to allocate a string S and then write S := because the first string you allocated is just thrown away.  Each of your calls to SetLength is pointless and can be removed without changing the meaning of the program.

 

Your for loop doesn't increment the pointers, and doesn't refer to the loop variable.  That can't be right.

 

You have a smiley face emoji in your code. Is that meant to be a variable named D?  I guess you need to make sure that the code that you paste is correct.

 

I recommend that you produce a complete program that you verify compiles, that produces the output you describe, and that you reproduce faithfully here.  

Share this post


Link to post

Dear David heffernan;

 

first of all thank you for your answer.

This code has been working on windows for years.

I listened to you, I made a different example, but andorid has the same problem.
My request from you. Please, I would be very happy if you first tested and respond accordingly.

If I have a mistake, can you make a simple example?

Please test it.

Thank you.

 

#001#002#003#004#005#006#007#010'\t'#001#002#003#004#005#006#007

 

function XorUtf(Str1, Str2: UTF8String): UTF8String;
var
  I: Integer;
  Ci:Char;
begin
  for I := 0 to Length(Str1) - 1 do
  begin
    Ci := Char(Ord(Str1) xor Ord(Str2));
    Result := Result + Ci;
  end;
end;

 

procedure TForm1.Button7Click(Sender: TObject);
var
  I, Len: Integer;
  S, T, D, Buff: UTF8String;
begin
  Len := 16;

  SetLength(S, Len);
  SetLength(T, Len);
//  SetLength(D, Len);

  S := '1234567891234567';
  T := '0000000000000000';
D:=XorUtf(s,t);

end;

 

Share this post


Link to post

string xor

 

win32 output

#0#1#2#3#4#5#6#7#8#9#1#2#3#4#5#6

android output

#001#002#003#004#005#006#007#010'\t'#001#002#003#004#005#006#007

 

function XorStr(Str1, Str2: String): String;
var
  I: Integer;
  Ci:Char;
begin
  for I := 0 to Length(Str1) - 1 do
  begin
    Ci := Char(Ord(Str1) xor Ord(Str2));
    Result := Result + Ci;
  end;
end;

 

procedure TForm1.Button7Click(Sender: TObject);
var
  I, Len: Integer;
  S, T, D, Buff: string;
begin

  S := '1234567891234567';
  T := '0000000000000000';
  D := XorStr(S, T);
end;

Share this post


Link to post

That code is completely different from the original post, which did not compile. The new code doesn't compile either. 

 

The code can't do anything useful because you will generate strings that are invalid with respect to their encoding. 

 

What are you actually trying to do here? It's hard to get enthused for an algorithm that is so clearly flawed. 

Edited by David Heffernan

Share this post


Link to post

I have no clue what is this for but 0x8 is octal 10 and 0x9 is TAB which is "\t".

 

We still can't see the most important part of the code, how do you evaluate the "output".

 

Anyway, no matter what it is, this is for sure not the way this should be done.

 

Edit: Also, the output is suspicious.

"This code has been working on windows for years."

How many years? 20? Was it compiled with an AnsiString Delphi compiler?

 

Edited by Attila Kovacs

Share this post


Link to post

Dear friends;

First of all, thank you for your answers.


There are two results in the picture.
Is this true ? Do I need to use something different for the character table?

I wrote a very simple code at the bottom. This will explain everything

 

David and Atilla, this code will work

Please run windows first and then android.
Look into the list. I made it in the picture below.

 

procedure TForm1.Button9Click(Sender: TObject);
var
  Ch: Char;
  I: Integer;
  T: TStringList;
begin
  T := TStringList.Create;
  for I := 0 to 300 - 1 do
  begin
    Ch := Char(I);
    T.Append(Ch);
  end;
end;

 

Char.jpg

Edited by Turan Can

Share this post


Link to post
Guest

@Turan Can Try this

var
  Ch: Char;
  I: Integer;
  T: TStringList;
  TT:array [0..299] of Char;
  TTIntValues:array[0..299] of Integer ;
begin
  T := TStringList.Create;
  for I := 0 to 300 - 1 do
  begin
    Ch := Char(I);
    T.Append(Ch);
    TT[I]:=Char(I);
    TTIntValues[I]:=Ord(TT[I]);
    if  TTIntValues[I]<>I then
      raise Exception.Create('Error, char value is wrong');
  end;
end;

The content of TTIntValues is valid and right as it should, with no exception been raised, what you see in #010 or /t are just how the visualizer/debugger interprets and shows those special character, here i can't say for sure if it is right or wrong, but the value in those chars are 100% right.

Share this post


Link to post
Guest

The /t is Horizontal tab, it is #9 like Carriage Return (CR) is #13 and Line Feed (LN) is #10, but why the debugger shows only the /t while shows #010 instead #008 while #010 should be /n ?

That might be a bug in the parser inside the visualizer only.

Share this post


Link to post

Everything is fine here. No bug in Delphi. 

 

However, your code that performs xor on character ordinal values and pushes the output back into strings is defective. 

Share this post


Link to post

Hi Kas,

 

It is no different from the jeans sample I made.

your sample test.


I added 2 pictures windows and android.

 

This problem is actually a charset table problem. Possibly differences in windows charset and linux android char sets.

For this problem, Delphi had to come up with a solution. Example: At the start of the project, I started to think that there should be a setting like setwindows charset table.

 

So what is the source of the problem why I need this 🙂
I use AES 256 bit and the same data should be the same in andorid and linux.

 

https://github.com/Turan-Can/Delphi-Encryption-Compendium

 

android.jpg

windows.jpg

Share this post


Link to post
Guest

@Turan Can i do not use Delphi like you do, so i know nil about your specific problem.

But i must give you points for you calmness and courage in you replies, especially considering the language barrier. That is the plus i gave on the OP.

Other posters get annoyed, frustrated and angry.

Keep it up!

Share this post


Link to post

Probably the person I gave the job will not be able to do it but he insisted he said. 🙂 We will see it all together.
Dear friends, I hope you have a good understanding of what I want.
Look, this is such a big problem. Think a little, you will understand.

 

Thanks for your nice words Dany,


I try to give as much detail as I can.
It seems that I will not be able to solve the problem here. I gave this problem as a "freelancer" as a job.

250$ 🙂

https://www.freelancer.com/projects/delphi/Android-Project-for-Delphi/payments/

 

Edited by Turan Can

Share this post


Link to post

I think, it's a bug in the TStrings visualizer, not in the code itself. Your program should work correctly both in Windows and Android.

Nevertheless, I suggest use TBytes for byte manipulation instead of UTF8String. See "Delphi in a Unicode World" by Nick Hodges, Chapter "USING STRINGS AS DATA BUFFERS".

Edited by Kryvich

Share this post


Link to post

No, this is not a tstringlist issue. this is officially chartable difference.
You will see the difference when each char 0..to bla bla binds to numeric or character.
char (8) > 10
char (9) > t tab

linux android

This is a serious mistake.
It affects thousands of delphi android users.
Think a little. Your application on Android sends data to an application on windows.

 

I'm not asking questions easily. I solve all my work myself. However, this made me drink "alcohol jack daniels oopss I put a reset on my brain."  for exactly 1 week 🙂

Delphi needs to urgently a patch.
You will either tell the project settings that you want to use the charset table, or it should offer two different char set options for windows and android.

 

Share this post


Link to post

If you are working with AES encryption then the entire topic is moot because you should not be using text at all. Your entire code should operate on bytes. 

Share this post


Link to post

Dear David,

Pls...
What am I telling 🙂 and what am I writing. Please do not write without testing the samples.

This is not a help. We extend the subject in vain.

 

Would you please make an example code?

 

 

Edited by Turan Can

Share this post


Link to post

It's not possible to run most of the code you presented as it doesn't compile. 

 

As for my points regarding encryption and text/bytes, those points don't require any code to be run. 

 

Please feel free to ignore my advice if you feel I am wrong. 

Share this post


Link to post
12 hours ago, Turan Can said:

No, this is not a tstringlist issue. this is officially chartable difference.
You will see the difference when each char 0..to bla bla binds to numeric or character.
char (8) > 10
char (9) > t tab

linux android

This is a serious mistake.
It affects thousands of delphi android users.

I don't quite understand what you are referring to.  Are you trying to point out the difference between 0-based string indexing on mobile platforms vs 1-based string indexing on desktop platforms?  Because that difference is well documented by Embarcadero.  And it is possible to continue using 1-based string indexing on mobile platforms, and to use 0-based string indexing on desktop platforms.

12 hours ago, Turan Can said:

Think a little. Your application on Android sends data to an application on windows.

And what is the ACTUAL PROBLEM you are having with that?  You are not making yourself clear.

 

Share this post


Link to post
On 1/25/2020 at 8:54 AM, Turan Can said:

function XorUtf(Str1, Str2: UTF8String): UTF8String;

var
  I: Integer;
  Ci:Char;
begin
  for I := 0 to Length(Str1) - 1 do
  begin
    Ci := Char(Ord(Str1) xor Ord(Str2));
    Result := Result + Ci;
  end;
end;

 

procedure TForm1.Button7Click(Sender: TObject);
var
  I, Len: Integer;
  S, T, D, Buff: UTF8String;
begin
  Len := 16;

  SetLength(S, Len);
  SetLength(T, Len);
//  SetLength(D, Len);

  S := '1234567891234567';
  T := '0000000000000000';
D:=XorUtf(s,t);

end;

That code will not work.  It needs to be more like this instead:

function XorUtf(Str1, Str2: UTF8String): UTF8String;
var
  I: Integer;
  Ci: UTF8Char;
begin
  for I := Low(Str1) to High(Str1) do
  begin
    Ci := Str1[I] xor Str2[I];
    Result := Result + Ci;
  end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  S, T, D: UTF8String;
begin
  S := '1234567891234567';
  T := '0000000000000000';
  D := XorUtf(s, t);
end;

Just note that the output of XorUtf() is not guaranteed to be a valid UTF-8 string if non-ASCII characters are present in the source strings, so UTF8String is really not an appropriate return type in this situation.

Share this post


Link to post

David, I care about what you write.
Is there anything wrong with this simple code?
Is it normal to see the output differently in Windows and android?

//win32 debug ch #8,#9,#$A

//Android debug ch #10,\t,#012

 

Remy, I know what you're saying. That's not the problem.

 

procedure TForm1.Button10Click(Sender: TObject);
const
  CTable: array [0 .. 2] of Byte = ($8, $9, $A);
var
  Ch: Char;
  I: Integer;
begin
  for I := 0 to High(CTable) do
    Ch := Char(CTable);

//win32 debug ch #8,#9,#$A

//Android debug ch #10,\t,#012
end;

Share this post


Link to post

It's just an artifact of the debugger visualiser. You are seeing some octal representations. The values are fine. As others have told you already. 

 

Your real problem is the misuse of strings to perform binary operations. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×