Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Download, fix, and save binary files for ComputerCraft
- -- You are free to copy, alter, and distribute this under the following conditions:
- -- 1) You leave these comments and credits attached
- -- 2) You do not make money off of this
- -- Program by James0x57 - http://youtube.com/James0x57
- -- ComputerCraft by Dan200 - http://www.computercraft.info/
- -- You'll want to have the source file open in a hex editor on your desktop when you use this.
- -- ComputerCraft command to add this program: pastebin get U0RSyAj0 bHTTP
- local args = {...};
- local rnTrap = false;
- function originalByte(input, index, originalX)
- local byt = string.byte(input, index);
- local userIn = nil;
- -- \r\n is replaced with \n throughout, this tries to handle it
- if rnTrap then -- last character in source file was 0x0D, user said next byte should be 0x0A
- rnTrap = false;
- return 0x0A, index; -- the index did not advance to the next character to handle this
- end
- if byt <= 0x7F then
- while byt == 0x0A do
- print("Byte 0x0A encountered.");
- print(string.format("Please check offset 0x%X of source file.", originalX));
- print("Enter 1 if original byte = 0x0A");
- print("Enter 2 if original byte = 0x0D");
- userIn = string.find("12", read());
- if userIn ~= nil then
- byt = ({0x0A,0x0D})[userIn];
- if byt == 0x0D then
- print(string.format("At offset 0x%X of the source file,", originalX+1));
- print("is there a byte = 0x0A?");
- print("(this is a special case we need to fix)");
- print("Enter 1 for 'Yes', 2 for 'No'");
- if read() == "1" then
- rnTrap = true; -- enter a second byte ( 0x0A ) after this is done
- index = index - 1;
- end
- end
- break;
- end
- end
- return byt, index;
- end
- if byt == 0xC2 then
- index = index + 1;
- byt = string.byte(input, index);
- elseif byt == 0xC3 then
- index = index + 1;
- byt = string.byte(input, index)+0x40;
- elseif byt == 0xE2 then
- index = index + 1;
- byt = string.byte(input, index);
- index = index + 1;
- if byt == 0x82 then
- byt = 0x80;
- elseif byt == 0x84 then
- byt = 0x99;
- else
- byt = string.byte(input, index);
- if byt == 0x9A then
- byt = 0x82;
- elseif byt == 0x9E then
- byt = 0x84;
- elseif byt == 0xA6 then
- byt = 0x85;
- elseif byt == 0xA0 then
- byt = 0x86;
- elseif byt == 0xA1 then
- byt = 0x87;
- elseif byt == 0xB0 then
- byt = 0x89;
- elseif byt == 0xB9 then
- byt = 0x8b;
- elseif byt == 0x98 then
- byt = 0x91;
- elseif byt == 0x99 then
- byt = 0x92;
- elseif byt == 0x9C then
- byt = 0x93;
- elseif byt == 0x9D then
- byt = 0x94;
- elseif byt == 0xA2 then
- byt = 0x95;
- elseif byt == 0x93 then
- byt = 0x96;
- elseif byt == 0x94 then
- byt = 0x97;
- elseif byt == 0xBA then
- byt = 0x9b;
- end
- end
- elseif byt == 0xC5 then
- index = index + 1;
- byt = string.byte(input, index);
- if byt == 0xA0 then
- byt = 0x8a;
- elseif byt == 0x92 then
- byt = 0x8c;
- elseif byt == 0xBD then
- byt = 0x8e;
- elseif byt == 0xA1 then
- byt = 0x9a;
- elseif byt == 0x93 then
- byt = 0x9c;
- elseif byt == 0xBE then
- byt = 0x9e;
- elseif byt == 0xB8 then
- byt = 0x9f;
- end
- elseif byt == 0xEF then
- index = index + 2;
- while true do
- print("Indeterminate 0xEFBFBD encountered.");
- print(string.format("Please check offset 0x%X of source file.", originalX));
- print("Enter 1 if original byte = 0x81");
- print("Enter 2 if original byte = 0x8D");
- print("Enter 3 if original byte = 0x8F");
- print("Enter 4 if original byte = 0x90");
- print("Enter 5 if original byte = 0x9D");
- userIn = string.find("12345", read());
- if userIn ~= nil then
- byt = ({0x81,0x8D,0x8F,0x90,0x9D})[userIn];
- break;
- end
- end
- else
- index = index + 1;
- byt = string.byte(input, index);
- if byt == 0x9C then
- byt = 0x98;
- elseif byt == 0x92 then
- byt = 0x83;
- elseif byt == 0x86 then
- byt = 0x88;
- end
- end
- return byt, index;
- end
- if #args > 1 and #args[1] > 10 and #args[2] > 2 and string.find(args[1], "http://", 1, 1, true)
- -- and string.find(args[1], ".nbs", -4, true) ~= nil -- example force source file type
- -- and string.find(args[2], ".nbs", -4, true) ~= nil -- example force destination file type
- then
- local url = args[1];
- local fn = args[2];
- local dt = http.get(url).readAll();
- local fh = fs.open(fn, 'wb');
- local ret = "";
- local userIn = "";
- local temp = nil;
- local byt = 0;
- local originalX = 0;
- if dt ~= nil and #dt > 0 then
- local x = 1;
- while x <= #dt do
- byt, x = originalByte(dt, x, originalX);
- originalX = originalX + 1;
- if args[4] == 1 then -- debug mode
- ret = ret .. string.format("0%X ", byt):sub(-3);
- if #ret == 48 then
- print(ret);
- ret = "";
- end
- end
- if originalX % 250 == 0 then
- os.sleep(0); --yeild or die
- end
- fh.write(byt);
- x = x+1;
- end
- end
- if args[4] == 1 then -- debug mode
- print(ret);
- end
- if args[3] ~= 0 then
- print("If the last byte of the source file is either");
- print("0x0D (\\r) or 0x0A (\\n), then it is missing.");
- print("The last byte in the new file is 0x" .. string.format("0%X ", byt):sub(-3));
- print(string.format("its offset is 0x%X", originalX-1));
- while true do
- print("Do you want to appened another byte?");
- print("Enter 1 for 'Yes', 2 for 'No'");
- if read() == "1" then
- print("Enter the byte prefixed with \"0x\" to use hex:");
- userIn = read();
- temp, userIn = pcall(tonumber, userIn);
- if temp == true and userIn < 0x100 then
- fh.write(userIn);
- print(string.format("Appeneded byte! 0x%X", userIn));
- else
- print("ERROR - Bad input");
- end
- else
- break;
- end
- end
- end
- fh.close();
- print("Binary file saved!");
- else
- print("Bad arguments.");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement