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.

import java.io.*;

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 = in.read();
while (b != -1)
{
if (prev == 13 && b != 10)
out.write(13);
if (b != 13)
out.write(b);
prev = b;
b = in.read();
}
if (prev == 13)
out.write(13);
out.close();
in.close();
}
}