From c8fdcd80b5e0aa59957c4c6234b58d743edb2e40 Mon Sep 17 00:00:00 2001 From: dmiller Date: Mon, 30 Nov 2020 17:59:18 +0000 Subject: [PATCH] Precalc/reuse some tables and values --- nselib/base32.lua | 7 +++++-- nselib/dhcp6.lua | 5 +++-- nselib/http.lua | 10 +++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/nselib/base32.lua b/nselib/base32.lua index 889790888..e830ce1eb 100644 --- a/nselib/base32.lua +++ b/nselib/base32.lua @@ -108,7 +108,10 @@ function enc (p, hexExtend) end -local db32table_standard = setmetatable({}, {__index = function (t, k) error "invalid encoding: invalid character" end}) +local db32metatable = { + __index = function (t, k) error "invalid encoding: invalid character" end +} +local db32table_standard = setmetatable({}, db32metatable) do local r = {["="] = 0} for i, v in ipairs(b32standard) do @@ -118,7 +121,7 @@ do db32table_standard[i] = r[char(i)] end end -local db32table_hex = setmetatable({}, {__index = function (t, k) error "invalid encoding: invalid character" end}) +local db32table_hex = setmetatable({}, db32metatable) do local r = {["="] = 0} for i, v in ipairs(b32hexExtend) do diff --git a/nselib/dhcp6.lua b/nselib/dhcp6.lua index cba464360..3f242c652 100644 --- a/nselib/dhcp6.lua +++ b/nselib/dhcp6.lua @@ -62,6 +62,7 @@ DHCP6.OptionTypes = { OPTION_CLIENT_FQDN = 0x27, } +local DHCP6_EPOCH = os.time({year=2000, day=1, month=1, hour=0, min=0, sec=0}) -- DHCP6 options DHCP6.Option = { @@ -110,7 +111,7 @@ DHCP6.Option = { type = DHCP6.OptionTypes.OPTION_CLIENTID, duid = duid or 1, hwtype = hwtype or 1, - time = time or os.time() - os.time({year=2000, day=1, month=1, hour=0, min=0, sec=0}), + time = time or os.time() - DHCP6_EPOCH, mac = mac, } setmetatable(o, self) @@ -131,7 +132,7 @@ DHCP6.Option = { end opt.hwtype, opt.time = string.unpack(">I2I4", data, pos) opt.mac = data:sub(pos) - opt.time = opt.time + os.time({year=2000, day=1, month=1, hour=0, min=0, sec=0}) + opt.time = opt.time + DHCP6_EPOCH return opt end, diff --git a/nselib/http.lua b/nselib/http.lua index 52852a76d..e6936ea9c 100644 --- a/nselib/http.lua +++ b/nselib/http.lua @@ -1034,19 +1034,19 @@ local function cmp_last_used (r1, r2) return (r1.last_used or 0) < (r2.last_used or 0); end +local arg_max_cache_size = tonumber(stdnse.get_script_args({'http.max-cache-size', 'http-max-cache-size'}) or 1e6); local function check_size (cache) - local max_size = tonumber(stdnse.get_script_args({'http.max-cache-size', 'http-max-cache-size'}) or 1e6); local size = cache.size; - if size > max_size then + if size > arg_max_cache_size then stdnse.debug1( "Current http cache size (%d bytes) exceeds max size of %d", - size, max_size); + size, arg_max_cache_size); table.sort(cache, cmp_last_used); for i, record in ipairs(cache) do - if size <= max_size then break end + if size <= arg_max_cache_size then break end local result = record.result; if type(result.body) == "string" then size = size - record.size; @@ -1056,7 +1056,7 @@ local function check_size (cache) cache.size = size; end stdnse.debug2("Final http cache size (%d bytes) of max size of %d", - size, max_size); + size, arg_max_cache_size); return size; end