This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Chickenbreadlp's profile picture

a little problem with executing own code before and after fs.open

Started by Chickenbreadlp, 18 May 2016 - 06:13 PM
Chickenbreadlp #1
Posted 18 May 2016 - 08:13 PM
As stated in the Title, I want to execute custom code before fs.open is beeing triggered and also, after the file was "closed" I want to execute more custom code, and I want to do this system wide for any program that might use this function. (In case somebody wants to know why, I'm working on a login system with encryption)
Now comes the problem, I've already tried everything, but it always crashes with the following error code:

startup:111: vm error:
java.lang.ArrayIndexOutOfBoundsException
The only thing that changed whilst experimenting, was the number wich tells me the erroring line (in case thats english?)

This is the full original code which produced the bofore giving error:
Spoiler

local function unfoldkey(keyinput)
math.randomseed(tonumber(keyinput, 36))
y = {}
z = {}
for i=1, 16 do
  y[i] = math.random(0, 0xFF)
  z[i] = math.random(0, 0xFF)
end
math.randomseed(math.random())
return y, z
end
function ofsopen(sFile, sMode)
local file = fs.open(sFile, sMode)
return file
end
local function syscrypt_encrypt(file, y, z)
local lfile = string.lower(file)
if file == "startup" or file == "encryptor" or lfile:find(".syscrypt") then
else
  local prog = ofsopen(file, "rb")
  local progData = {}
  local progbyte = prog.read()
  repeat
   table.insert(progData, progbyte)
   progbyte = prog.read()
  until progbyte == nil
  prog.close()
   progData = ccaes.encrypt_bytestream(progData, y, z)
  local prog = ofsopen(file, "wb")
  for i=1, #progData do
   prog.write(progData[i])
  end
  prog.close()
end
end
local function syscrypt_decrypt(file, y, z)
local lfile = string.lower(file)
if file == "startup" or file == "encryptor" or lfile:find(".syscrypt") then
else
  local prog = ofsopen(file, "rb")
  local progData = {}
  local progbyte = prog.read()
  repeat
  table.insert(progData, progbyte)
  progbyte = prog.read()
  until progbyte == nil
  prog.close()
  progData = ccaes.decrypt_bytestream(progData, y, z)
  local prog = ofsopen(file, "wb")
  for i=1, #progData do
   prog.write(progData[i])
  end
  prog.close()
end
end
if syscrypt_keyhash ~= nil and syscrypt_keyhash ~= "" then
term.setbackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
if fs.exists("/.syscrypt/boot") then
  shell.run("/.syscrypt/boot")
else
  shell.run("shell")
end
else
local y = {}
local z = {}
local syscrypt_keyhash = ""
if fs.exists("/User/.SyscryptSetup") then
fs.delete("/User/.SyscryptSetup")
end
if fs.exists("/User/.SyscryptShut") then
fs.delete("/User/.SyscryptShut")
end
os.loadAPI("/.syscrypt/hash")
os.loadAPI("/.syscrypt/ccaes")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
local textXI = (52 - string.len("This Computer has been protected with Syscrypt")) / 2
local textXV = (52 - string.len("To decrypt, enter in your password below")) / 2
term.setCursorPos(textXI, 3)
print("This Computer has been protected with Syscrypt")
term.setCursorPos(textXV, 4)
print("To decrypt, enter in your password below")
while true do
term.setCursorPos(1,7)
term.clearLine()
term.setCursorPos(1,8)
term.clearLine()
term.setCursorPos(1,7)
write("Password: ")
local oldpe = os.pullEvent()
os.pullEvent = os.pullEventRaw
syscrypt_keyhash = hash.sha256(read("*"))
shell.run("/.syscrypt/reference_key")
if hash.sha256(syscrypt_keyhash) == syscrypt_key then

  y, z = unfoldkey(syscrypt_keyhash)
  function fs.open(sFile, sMode)
   if fs.exists(sFile) then
	decrypt(sFile, y, z)  -- this is line 111
	local file = ofsopen(sFile, sMode)
	local oclose = function()
	 file.close()
	end
	file.close = function( self )
	 oclose()
	 encrypt(sFile, y, z)
	end
	return file
   else
	return nil
   end
  end

  term.clear()
  term.setCursorPos(1,1)
  if fs.exists("/.syscrypt/boot") then
   shell.run("/.syscrypt/boot")
  else
   shell.run("shell")
  end
  break
else
  printError("Wrong Password!")
  sleep(2)
end
end
end
And this is just my custom fs.open function:
Spoiler

function fs.open(sFile, sMode)
if fs.exists(sFile) then
  decrypt(sFile, y, z)  -- in the original code, this is line 111
  local file = ofsopen(sFile, sMode)
  local oclose = function()
   file.close()
  end
  file.close = function( self )
   oclose()
   encrypt(sFile, y, z)
  end
  return file
else
  return nil
end
end
Edited on 18 May 2016 - 06:14 PM
KingofGamesYami #2
Posted 18 May 2016 - 08:40 PM
file.close is calling itself an infinite number of times. Also known as "oclose".
Chickenbreadlp #3
Posted 18 May 2016 - 08:58 PM
file.close is calling itself an infinite number of times. Also known as "oclose".
That causes the infinite loop :blink:/> :huh:/>. I just wanted to preserve the original file.close(). Is there a safe way to do it?
valithor #4
Posted 18 May 2016 - 10:23 PM
file.close is calling itself an infinite number of times. Also known as "oclose".
That causes the infinite loop :blink:/> :huh:/>. I just wanted to preserve the original file.close(). Is there a safe way to do it?


local osclose = file.close