1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Use string.gsub instead of looped concat to modify strings

This commit is contained in:
dmiller
2015-02-27 14:55:29 +00:00
parent d16772a8a8
commit e275a96c72
5 changed files with 23 additions and 42 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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 )

View File

@@ -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

View File

@@ -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)