Advertisement
ozozx235

bReader.lua

Mar 29th, 2024 (edited)
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. function pullArg()
  2.  
  3.     condition = read()
  4.  
  5.     prog = tonumber(string.sub(condition,1,string.find(condition,"%s")-1))
  6.  
  7.     type = string.sub(condition,string.find(condition,"%s")+1)
  8.  
  9.     if prog == 0 then
  10.  
  11.         return 0, "nil"
  12.  
  13.     end
  14.  
  15.     if not (type == "nil" or type == "bin" or type == "int" or type == "str" or prog == nil) then
  16.  
  17.         print("Invalid argument, format should be as follows:")
  18.  
  19.         print("\"<number> <type [nil,bin,int,str]>\"")
  20.  
  21.         print("input \"0 nil\" to exit")
  22.  
  23.         return pullArg()
  24.  
  25.     end
  26.  
  27.     return prog, type
  28.  
  29. end
  30.  
  31. local tArgs = { ... }
  32.  
  33. if #tArgs<1 then
  34.  
  35.     print("No File")
  36.  
  37.     return
  38.  
  39. end
  40.  
  41. print("is " .. tArgs[1] .. " a File?")
  42.  
  43. if not fs.exists(tArgs[1]) or (fs.exists(tArgs[1]) and fs.isDir(tArgs[1])) then
  44.  
  45.     print("Wrong File Bozo")
  46.  
  47.     return
  48.  
  49. end
  50.  
  51. print(fs.getName(tArgs[1]) .. " Exists!")
  52.  
  53. bin = fs.open(tArgs[1],"rb").readAll()
  54.  
  55. print(#bin .. "b")
  56.  
  57. i=1
  58.  
  59. while i < #bin do
  60.  
  61.     prog, type = pullArg()
  62.  
  63.     if prog == 0 then
  64.  
  65.         print("Reading cut short at byte "..i)
  66.  
  67.         return
  68.  
  69.     end
  70.  
  71.     buffer = nil
  72.  
  73.     if type =="bin" then
  74.  
  75.         for j=1,prog do
  76.  
  77.             buffer = buffer .. " " .. string.byte(bin,i+j-1)
  78.  
  79.         end
  80.  
  81.         print(buffer)
  82.  
  83.     elseif type =="int" then
  84.  
  85.             -- print(buffer)
  86.  
  87.         for j=1,prog do
  88.  
  89.             if buffer == nil then
  90.  
  91.                 buffer = 0
  92.  
  93.             end
  94.  
  95.             buffer = string.byte(bin,i+j-1)*math.pow(16,2*(j-1)) + buffer
  96.  
  97.         end
  98.  
  99.         print(buffer)
  100.  
  101.     elseif type =="str" then
  102.  
  103.         for j=1,prog do
  104.  
  105.             if buffer == nil then
  106.  
  107.                 buffer = ""
  108.  
  109.             end
  110.  
  111.             buffer = buffer .. "" .. string.char(string.byte(bin,i+j-1))
  112.  
  113.         end
  114.  
  115.         print(buffer)
  116.  
  117.     end
  118.  
  119.     i=i+prog
  120.  
  121. end
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement