ID:2378903
 
Resolved
Several bitwise operators did not return correct results in some cases as a result of the new 24-bit twiddling change. Additionally, .dmb files compiled before version 512 will now use 16-bit twiddling as originally planned.
BYOND Version:512.1429, 512.1428, 512.1427
Operating System:Windows 10 Home 64-bit
Web Browser:Chrome 67.0.3396.87
Applies to:Dream Daemon
Status: Resolved (512.1430)

This issue has been resolved.
Descriptive Problem Summary:
Ever since BYOND Beta Version 512.1427 the following code (within the file after extracting) has given me a BYOND Error 'failed to parse savefile text at line ...' while I was using Air Mapster's RC5 Encryption Library. This does not happen in the previous build before Version 512.1427.

Numbered Steps to Reproduce Problem:
1. Download/Install any BYOND Beta Version 512.1427 and higher.
2. Extract the following zip file. Link: http://files.byondhome.com/Hydrugen/savefile_bug_src.zip
3. Open the source in Dream Maker and Run.
4. Relog and Check Options and Messages to see the error.

Code Snippet (if applicable) to Reproduce Problem:
mob
Login()
if(fexists("player/[src.key].sav"))
src.file2read()
src << "Savefile found and has been loaded!"
else
src << "Savefile not found!"
..()

Logout()
src.file2write()
..()

proc
file2write()
var/savefile/F = new
if(fexists("player/[src.key].sav"))
fdel("player/[src.key].sav")

// Do not remove. Prevents savefile size from basically duplicating in size.

src.Write(F)
text2file(RC5_Encrypt(F.ExportText("/"), md5("byond")), "player/[src.key].sav")


file2read()
var/savefile/F = new
F.ImportText("/", RC5_Decrypt(file2text("player/[src.key].sav"), md5("byond")))
src.Read(F)


Expected Results:
BYOND Error message doesn't appear on output/Options and Message and everything runs without a problem.

Actual Results:
BYOND Error: failed to parse savefile text at line 1 (reading 'end of file'):

Does the problem occur:
Every time? Or how often? Every time
In other games? Probably not
In other user accounts? Yes
On other computers? Yes

When does the problem NOT occur?
Using any BYOND Beta Version before 512.1427.

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? (Visit http://www.byond.com/download/build to download old versions for testing.)

I would say Verison 512.1424 to 512.1426 that it didn't occur.

Workarounds:
Downgrade between 512.1424 to 512.1426
I'm not actually sure this is a bug, but a consequence of the RC5 library not being compatible anymore due to a recent change. It's doing bitwise math but assuming the result is 16 bits. 512.1427 however made the long-requested change of switching all bitwise operations to 24-bit (the most integer precision available with floating point).

While I normally don't recommend editing libraries, I think a wise solution would be to make the following changes:

    proc/rotl(x, y)
return (x << (y & (src.w - 1)) | (x >> (src.w - (y & (src.w - 1))))) & 0xFFFF

proc/rotr(x, y)
return (x >> (y & (src.w - 1)) | (x << (src.w - (y & (src.w - 1))))) & 0xFFFF


            L[idx] = ((L[idx] << 8) + text2ascii(copytext(key, i+1, i+2))) & 0xFFFF  // line 263


I haven't tested those changes yet but I believe they'll make do as a fix.

I may want to change the way bit twiddling is handled in pre-512-compiled games to avoid this problem with already-compiled source, though.
I've tried the solution you recommend and I was still getting the BYOND Error: failed to parse savefile text at line 1 (reading 'end of file'):
Lummox JR resolved issue with message:
Several bitwise operators did not return correct results in some cases as a result of the new 24-bit twiddling change. Additionally, .dmb files compiled before version 512 will now use 16-bit twiddling as originally planned.
There was in fact a bug in the server code, and also the code update I suggested for the rc5 library was incomplete. These are the changes:

    proc/rotl(x, y)
return ((x << (y & (src.w - 1)) | ((x & 0xFFFF) >> (src.w - (y & (src.w - 1)))))) & 0xFFFF

proc/rotr(x, y)
return (((x & 0xFFFF) >> (y & (src.w - 1)) | (x << (src.w - (y & (src.w - 1)))))) & 0xFFFF

And line 263:

            L[idx] = ((L[idx] << 8) + text2ascii(copytext(key, i+1, i+2))) & 0xFFFF

With those changes to the library and the fix in the next build, the problems will disappear.

I've already pushed an update to the library, since Mike has been inactive for some time.