Hi all!
I'm in the process of porting a FreePascal/SDL game of mine from Windows to Mac OS X. I've already successfully ported the game to Linux 32 and 64-bit with no code changes whatsoever, and I believe I've got the game running on Intel Mac OS X systems as well (I haven't received any crash reports from Intel Mac users and the game runs perfectly on my own Intel Mac). However, I've received crash reports from PowerPC Mac OS X users and I believe the issue is with a procedure that encrypts/decrypts game files by using byte shifting:
[pascal]
FUNCTION Encrypt(Const S : String; Key : Word) : String;
VAR
I : Byte;
TempString : String;
BEGIN
TempString := '';
FOR I := 1 TO Length(S) DO BEGIN
TempString := TempString + Char(Byte(S[I]) xor (Key shr );
Key := (Byte(TempString[I]) + Key) * C1 + C2;
END;
Encrypt := TempString;
END;
FUNCTION Decrypt(Const S : String; Key : Word) : String;
VAR I : Byte;
TempString : String;
BEGIN
TempString := '';
FOR I := 1 TO Length(S) DO BEGIN
TempString := TempString + Char(Byte(S[I]) xor (Key shr );
Key := (Byte(S[I]) + Key) * C1 + C2;
END;
Decrypt := TempString;
END;
[/pascal]
The game would crash on PowerPC's when first using the decrypt function to get info from an encrypted data file. So, my first thought was that it's due to PowerPC's being big-endian whereas my x86 is little-endian, so I added the built-in FreePascal endian "conversion" functions:
[pascal]
FUNCTION Encrypt(Const S : String; Key : Word) : String;
VAR
I : Byte;
TempString : String;
BEGIN
TempString := '';
FOR I := 1 TO Length(S) DO BEGIN
TempString := TempString + Char(NtoLE(Byte(S[I])) xor (Key shr );
Key := (NtoLE(Byte(TempString[I])) + Key) * C1 + C2;
END;
Encrypt := TempString;
END;
FUNCTION Decrypt(Const S : String; Key : Word) : String;
VAR I : Byte;
TempString : String;
BEGIN
TempString := '';
FOR I := 1 TO Length(S) DO BEGIN
TempString := TempString + Char(LEtoN(Byte(S[I])) xor (Key shr );
Key := (LEtoN(Byte(S[I])) + Key) * C1 + C2;
END;
Decrypt := TempString;
END;
[/pascal]
The game still crashes at the same point, though, so I'm obviously not doing something right. (I've since read that bytes don't have endian issues?) Any ideas as to how to fix this?
Bookmarks