From 94bb85c31dd63e70264e62d055a215fe010e56c8 Mon Sep 17 00:00:00 2001
From: Redo <a509dcfc@cock.li>
Date: Sat, 4 Jul 2020 02:02:41 -0500
Subject: [PATCH] added io library isolation so logic functions cant access the
 io functions

---
 sim/gatedef.lua    |  2 --
 sim/iosafe.lua     | 22 ++++++++++++++++++++++
 sim/main.lua       | 17 ++++++++++++-----
 sim/simulation.lua |  2 --
 4 files changed, 34 insertions(+), 9 deletions(-)
 create mode 100644 sim/iosafe.lua

diff --git a/sim/gatedef.lua b/sim/gatedef.lua
index c9ea4f9..fde6a85 100644
--- a/sim/gatedef.lua
+++ b/sim/gatedef.lua
@@ -1,6 +1,4 @@
 
-require "utility"
-
 GateDefinition = {
 	ports = {},
 	logic = function(gate) end,
diff --git a/sim/iosafe.lua b/sim/iosafe.lua
new file mode 100644
index 0000000..d767c10
--- /dev/null
+++ b/sim/iosafe.lua
@@ -0,0 +1,22 @@
+
+assert(not io.safe, "re-execution of iosafe")
+
+local iosafe = io
+_G.io = {safe = true}
+local requiresafe = require
+_G.require = nil
+
+local savedir = OPT_SAVE_DIR
+
+function io.open(fn, mode)
+	fn = fn:gsub("\\", "/")
+	assert(not fn:find("[^a-zA-Z0-9%._ ]"), "illegal character in file name \""..fn.."\"")
+	assert(not fn:find("%.%."), "illegal updir in file name \""..fn.."\"")
+	fn = fn:gsub("^/", "")
+	
+	fn = savedir.."/"..fn
+	
+	print("access file \""..fn.."\"")
+	
+	return iosafe.open(fn, mode)
+end
diff --git a/sim/main.lua b/sim/main.lua
index 8f056d5..4910181 100644
--- a/sim/main.lua
+++ b/sim/main.lua
@@ -1,8 +1,14 @@
 
 OPT_SAVE_DIR = arg[1] or error("must specify save location")
-OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("\\$", "").."\\"
+OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("\\", "/")
+OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "")
 print("Save location set to \""..OPT_SAVE_DIR.."\"")
 
+local socket = require("socket")
+
+dofile("iosafe.lua")
+
+dofile("utility.lua")
 dofile("simulation.lua")
 dofile("group.lua")
 dofile("wire.lua")
@@ -104,7 +110,6 @@ local function acceptclient()
 	print("Connection from " .. ip .. ":" .. port)
 end
 
-local socket = require("socket")
 server = assert(socket.bind("*", 25000))
 client = nil
 
@@ -245,10 +250,12 @@ while 1 do
 					if obj.logictype == 0 then
 						local numportsi = 0; for k, wire in pairs(obj.group.in_ports ) do numportsi = numportsi+1 end
 						local numportso = 0; for k, wire in pairs(obj.group.out_ports) do numportso = numportso+1 end
+						local numwires  = 0; for k, wire in pairs(obj.group.wires    ) do numwires  = numwires +1 end
 						
-						info = "\\c5WIRE<br>" .. (obj.group.state and "\\c2ON" or "\\c0OFF") .. "\n" ..
-							"IN PORTS: " ..numportsi.."\n"..
-							"OUT PORTS: "..numportso
+						info = "\\c5Net " .. tostring(obj.group):match("table: 0x(.+)"):upper() .. "\n" .. (obj.group.state and "\\c2On" or "\\c0Off") .. "\n" ..
+							"Wires: "..numwires.."\n"..
+							"In Ports: " ..numportsi.."\n"..
+							"Out Ports: "..numportso
 						;
 					else
 						info = "\\c5" .. obj.definition.name .. "<br>"
diff --git a/sim/simulation.lua b/sim/simulation.lua
index 7dabfa3..79fbe11 100644
--- a/sim/simulation.lua
+++ b/sim/simulation.lua
@@ -1,6 +1,4 @@
 
-require "utility"
-
 Simulation = {}
 
 function Simulation:new()