diff --git a/nselib/cvs.lua b/nselib/cvs.lua index 6ca28c7d6..ca64aa11d 100644 --- a/nselib/cvs.lua +++ b/nselib/cvs.lua @@ -86,11 +86,8 @@ Util = { 182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195, 243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,152 }; - local result = "" - for i = 1, #password do - result = result .. string.char(shifts[password:byte(i)+1]) - end - return 'A' .. result + return 'A' .. string.gsub(password, ".", + function(c) return string.char(shifts[c:byte()+1]) end) end } diff --git a/nselib/drda.lua b/nselib/drda.lua index 257577059..f96d47cfd 100644 --- a/nselib/drda.lua +++ b/nselib/drda.lua @@ -751,13 +751,10 @@ StringUtil = -- @param ascii string containing the ASCII value -- @return string containing the EBCDIC value toEBCDIC = function( ascii ) - local ret = "" - - for i=1, #ascii do - local val = ascii.byte(ascii,i) + 1 - ret = ret .. a2e_tbl:sub(val, val) - end - return ret + return string.gsub(ascii, ".", function(a) + local val = a:byte() + 1 + return a2e_tbl:sub(val, val) + end) end, --- Converts an EBCDIC string to ASCII @@ -765,13 +762,10 @@ StringUtil = -- @param ebcdic string containing EBCDIC value -- @return string containing ASCII value toASCII = function( ebcdic ) - local ret = "" - - for i=1, #ebcdic do - local val = ebcdic.byte(ebcdic,i) + 1 - ret = ret .. e2a_tbl:sub(val, val) - end - return ret + return string.gsub(ebcdic, ".", function(e) + local val = e:byte() + 1 + return e2a_tbl:sub(val, val) + end) end, --- Pads a string with a character diff --git a/nselib/mssql.lua b/nselib/mssql.lua index 46a2b36d4..7f9aedb8d 100644 --- a/nselib/mssql.lua +++ b/nselib/mssql.lua @@ -3064,15 +3064,13 @@ Auth = { -- @return string containing the encrypted password TDS7CryptPass = function(password) local xormask = 0x5a5a - local result = "" - for i=1, password:len() do - local c = bit.bxor( string.byte( password:sub( i, i ) ), xormask ) + return password:gsub(".", function(i) + local c = bit.bxor( string.byte( i ), xormask ) local m1= bit.band( bit.rshift( c, 4 ), 0x0F0F ) local m2= bit.band( bit.lshift( c, 4 ), 0xF0F0 ) - result = result .. bin.pack("S", bit.bor( m1, m2 ) ) - end - return result + return bin.pack("S", bit.bor( m1, m2 ) ) + end) end, LmResponse = function( password, nonce ) diff --git a/nselib/netbios.lua b/nselib/netbios.lua index 01e34de30..b028d9929 100644 --- a/nselib/netbios.lua +++ b/nselib/netbios.lua @@ -101,13 +101,12 @@ function name_decode(encoded_name) stdnse.debug3("Decoding name '%s'", encoded_name) - for i = 2, len + 1, 2 do - local ch = 0 - ch = bit.bor(ch, bit.lshift(string.byte(encoded_name, i) - 0x41, 4)) - ch = bit.bor(ch, bit.lshift(string.byte(encoded_name, i + 1) - 0x41, 0)) - - name = name .. string.char(ch) - end + name = name:gsub("(.)(.)", function (a, b) + local ch = 0 + ch = bit.bor(ch, bit.lshift(string.byte(a) - 0x41, 4)) + ch = bit.bor(ch, bit.lshift(string.byte(b) - 0x41, 0)) + return string.char(ch) + end) -- Decode the scope local pos = 34 diff --git a/nselib/rmi.lua b/nselib/rmi.lua index 1cbac885e..6c09a4a04 100644 --- a/nselib/rmi.lua +++ b/nselib/rmi.lua @@ -1156,16 +1156,9 @@ end -- returns the string with all non-printable chars -- coded as hex function makeStringReadable(data) - local r = "" - for i=1,#data,1 do - local x = data:byte(i) - if x > 31 and x <127 then - r = r .. data:sub(i,i) - else - r = r .. ("\\x%x"):format(x) - end - end - return r + return data:gsub("[\x00-\x1f\x7f-\xff]", function (x) + return ("\\x%02x"):format(x:byte()) + end) end function readNonProxyDesc(dis)