Thursday, March 29, 2007

FTP ASCII unmangler

FTP text mode is evil.

I made the mistake of transferring several important binary files from OS/X to windows last night, using FTP. Actually, a few mistakes were made along the way. 1) I didn't check that I was in BIN mode first, 2) I didn't verify the integrity of the files after the transfer, and 3) I deleted the sources.

Luckily, it was Unix-to-Windows, which means all #10 octets were replaced with #13#10. First I tried dos2unix with no luck. Then I wrote a program to replace all #13#10 sequences with #10 and crossed my fingers.

It worked. Here it is in all it's inefficient glory. Maybe this will help someone else someday. No guarantees, but it's worth a shot if you're desperate.


public class Unmangle
public static void main(String[] args) throws Exception
InputStream in = new FileInputStream(args[0]);
OutputStream out = new FileOutputStream(args[1]);
int prev = 0;
int b =;
while (b != -1)
if (prev == 13 && b != 10)
if (b != 13)
prev = b;
b =;
if (prev == 13)

1 comment:

Randy said...

You just saved my life. I did the same thing a couple of weeks ago; lost my calendar, contacts, mail, photos, the works.

I tried various ways of fixing/correcting the zip-file but with no luck. I never thought of the #10->#13#10.

If you're ever in Sweden; beer is on me.