Advertisement
James0x57

ComputerCraft Get Binary Files From HTTP

May 24th, 2013
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.77 KB | None | 0 0
  1. -- Download, fix, and save binary files for ComputerCraft
  2. -- You are free to copy, alter, and distribute this under the following conditions:
  3. -- 1) You leave these comments and credits attached
  4. -- 2) You do not make money off of this
  5. -- Program by James0x57 - http://youtube.com/James0x57
  6. -- ComputerCraft by Dan200 - http://www.computercraft.info/
  7. -- You'll want to have the source file open in a hex editor on your desktop when you use this.
  8. -- ComputerCraft command to add this program: pastebin get U0RSyAj0 bHTTP
  9.  
  10. local args = {...};
  11. local rnTrap = false;
  12.  
  13. function originalByte(input, index, originalX)
  14.     local byt = string.byte(input, index);
  15.     local userIn = nil;
  16.  
  17.     -- \r\n is replaced with \n throughout, this tries to handle it
  18.     if rnTrap then -- last character in source file was 0x0D, user said next byte should be 0x0A
  19.         rnTrap = false;
  20.         return 0x0A, index; -- the index did not advance to the next character to handle this
  21.     end
  22.  
  23.     if byt <= 0x7F then
  24.         while byt == 0x0A do
  25.             print("Byte 0x0A encountered.");
  26.             print(string.format("Please check offset 0x%X of source file.", originalX));
  27.             print("Enter 1 if original byte = 0x0A");
  28.             print("Enter 2 if original byte = 0x0D");
  29.             userIn = string.find("12", read());
  30.             if userIn ~= nil then
  31.                 byt = ({0x0A,0x0D})[userIn];
  32.                 if byt == 0x0D then
  33.                     print(string.format("At offset 0x%X of the source file,", originalX+1));
  34.                     print("is there a byte = 0x0A?");
  35.                     print("(this is a special case we need to fix)");
  36.                     print("Enter 1 for 'Yes', 2 for 'No'");
  37.                     if read() == "1" then
  38.                         rnTrap = true; -- enter a second byte ( 0x0A ) after this is done
  39.                         index = index - 1;
  40.                     end
  41.                 end
  42.                 break;
  43.             end
  44.         end
  45.         return byt, index;
  46.     end
  47.  
  48.     if byt == 0xC2 then
  49.         index = index + 1;
  50.         byt = string.byte(input, index);
  51.     elseif byt == 0xC3 then
  52.         index = index + 1;
  53.         byt = string.byte(input, index)+0x40;
  54.     elseif byt == 0xE2 then
  55.         index = index + 1;
  56.         byt = string.byte(input, index);
  57.         index = index + 1;
  58.         if byt == 0x82 then
  59.             byt = 0x80;
  60.         elseif byt == 0x84 then
  61.             byt = 0x99;
  62.         else
  63.             byt = string.byte(input, index);
  64.             if byt == 0x9A then
  65.                 byt = 0x82;
  66.             elseif byt == 0x9E then
  67.                 byt = 0x84;
  68.             elseif byt == 0xA6 then
  69.                 byt = 0x85;
  70.             elseif byt == 0xA0 then
  71.                 byt = 0x86;
  72.             elseif byt == 0xA1 then
  73.                 byt = 0x87;
  74.             elseif byt == 0xB0 then
  75.                 byt = 0x89;
  76.             elseif byt == 0xB9 then
  77.                 byt = 0x8b;
  78.             elseif byt == 0x98 then
  79.                 byt = 0x91;
  80.             elseif byt == 0x99 then
  81.                 byt = 0x92;
  82.             elseif byt == 0x9C then
  83.                 byt = 0x93;
  84.             elseif byt == 0x9D then
  85.                 byt = 0x94;
  86.             elseif byt == 0xA2 then
  87.                 byt = 0x95;
  88.             elseif byt == 0x93 then
  89.                 byt = 0x96;
  90.             elseif byt == 0x94 then
  91.                 byt = 0x97;
  92.             elseif byt == 0xBA then
  93.                 byt = 0x9b;
  94.             end
  95.         end
  96.     elseif byt == 0xC5 then
  97.         index = index + 1;
  98.         byt = string.byte(input, index);
  99.         if byt == 0xA0 then
  100.             byt = 0x8a;
  101.         elseif byt == 0x92 then
  102.             byt = 0x8c;
  103.         elseif byt == 0xBD then
  104.             byt = 0x8e;
  105.         elseif byt == 0xA1 then
  106.             byt = 0x9a;
  107.         elseif byt == 0x93 then
  108.             byt = 0x9c;
  109.         elseif byt == 0xBE then
  110.             byt = 0x9e;
  111.         elseif byt == 0xB8 then
  112.             byt = 0x9f;
  113.         end
  114.     elseif byt == 0xEF then
  115.         index = index + 2;
  116.         while true do
  117.             print("Indeterminate 0xEFBFBD encountered.");
  118.             print(string.format("Please check offset 0x%X of source file.", originalX));
  119.             print("Enter 1 if original byte = 0x81");
  120.             print("Enter 2 if original byte = 0x8D");
  121.             print("Enter 3 if original byte = 0x8F");
  122.             print("Enter 4 if original byte = 0x90");
  123.             print("Enter 5 if original byte = 0x9D");
  124.             userIn = string.find("12345", read());
  125.             if userIn ~= nil then
  126.                 byt = ({0x81,0x8D,0x8F,0x90,0x9D})[userIn];
  127.                 break;
  128.             end
  129.         end
  130.     else
  131.         index = index + 1;
  132.         byt = string.byte(input, index);
  133.         if byt == 0x9C then
  134.             byt = 0x98;
  135.         elseif byt == 0x92 then
  136.             byt = 0x83;
  137.         elseif byt == 0x86 then
  138.             byt = 0x88;
  139.         end
  140.     end
  141.     return byt, index;
  142. end
  143.  
  144.  
  145. if #args > 1 and #args[1] > 10 and #args[2] > 2 and string.find(args[1], "http://", 1, 1, true)
  146.     -- and string.find(args[1], ".nbs", -4, true) ~= nil -- example force source file type
  147.     -- and string.find(args[2], ".nbs", -4, true) ~= nil -- example force destination file type
  148. then
  149.     local url = args[1];
  150.     local fn = args[2];
  151.     local dt = http.get(url).readAll();
  152.     local fh = fs.open(fn, 'wb');
  153.     local ret = "";
  154.     local userIn = "";
  155.     local temp = nil;
  156.     local byt = 0;
  157.     local originalX = 0;
  158.     if dt ~= nil and #dt > 0 then
  159.         local x = 1;
  160.         while x <= #dt do
  161.             byt, x = originalByte(dt, x, originalX);
  162.             originalX = originalX + 1;
  163.             if args[4] == 1 then -- debug mode
  164.                 ret = ret .. string.format("0%X ", byt):sub(-3);
  165.                 if #ret == 48 then
  166.                     print(ret);
  167.                     ret = "";
  168.                 end
  169.             end
  170.             if originalX % 250 == 0 then
  171.                 os.sleep(0); --yeild or die
  172.             end
  173.             fh.write(byt);
  174.             x = x+1;
  175.         end
  176.     end
  177.  
  178.     if args[4] == 1 then -- debug mode
  179.         print(ret);
  180.     end
  181.    
  182.     if args[3] ~= 0 then
  183.         print("If the last byte of the source file is either");
  184.         print("0x0D (\\r) or 0x0A (\\n), then it is missing.");
  185.         print("The last byte in the new file is 0x" .. string.format("0%X ", byt):sub(-3));
  186.         print(string.format("its offset is 0x%X", originalX-1));
  187.         while true do
  188.             print("Do you want to appened another byte?");
  189.             print("Enter 1 for 'Yes', 2 for 'No'");
  190.             if read() == "1" then
  191.                 print("Enter the byte prefixed with \"0x\" to use hex:");
  192.                 userIn = read();
  193.                 temp, userIn = pcall(tonumber, userIn);
  194.                 if temp == true and userIn < 0x100 then
  195.                     fh.write(userIn);
  196.                     print(string.format("Appeneded byte! 0x%X", userIn));
  197.                 else
  198.                     print("ERROR - Bad input");
  199.                 end
  200.             else
  201.                 break;
  202.             end
  203.         end
  204.     end
  205.     fh.close();
  206.     print("Binary file saved!");
  207. else
  208.     print("Bad arguments.");
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement