use # length operator instead of string.len (canonicalize)

Used this perl command:

$ # perl -pi -e 's/string\.len\((.*?)\)/#\1/g' *.lua

Also fixed one instance where the above command didn't correctly
translate the intended code (string.len(a .. b .. c)).
This commit is contained in:
batrick 2011-05-11 15:08:55 +00:00
parent e700497f6c
commit 4444071f03
17 changed files with 82 additions and 82 deletions

View file

@ -272,7 +272,7 @@ ASN1Encoder = {
---
-- Encodes an ASN1 sequence
encodeSeq = function(self, seqData)
return bin.pack('HAA' , '30', self.encodeLength(string.len(seqData)), seqData)
return bin.pack('HAA' , '30', self.encodeLength(#seqData), seqData)
end,
---
@ -318,13 +318,13 @@ ASN1Encoder = {
-- Integer encoder
self.encoder['number'] = function( self, val )
local ival = self.encodeInt(val)
local len = self.encodeLength(string.len(ival))
local len = self.encodeLength(#ival)
return bin.pack('HAA', '02', len, ival)
end
-- Octet String encoder
self.encoder['string'] = function( self, val )
local len = self.encodeLength(string.len(val))
local len = self.encodeLength(#val)
return bin.pack('HAA', '04', len, val)
end

View file

@ -163,7 +163,7 @@ function dec(b64data)
local pos = 1
local byte
local nbyte = ''
for pos = 1, #b64data do -- while pos <= string.len(b64data) do
for pos = 1, #b64data do -- while pos <= #b64data do
byte = b64dec6bit(substr(b64data, pos, pos))
if not byte then return end
nbyte = nbyte .. byte

View file

@ -336,7 +336,7 @@ function reverse(ip)
-- padding
local mask = "0000"
for i, part in ipairs(ipParts) do
ipParts[i] = mask:sub(1, string.len(mask) - string.len(part)) .. part
ipParts[i] = mask:sub(1, #mask - #part) .. part
end
-- 32 parts from 8
local temp = {}

View file

@ -1461,12 +1461,12 @@ local function read_quoted_string(s, pos)
end
pos = pos + 1
pos = skip_space(s, pos)
while pos <= string.len(s) and string.sub(s, pos, pos) ~= "\"" do
while pos <= #s and string.sub(s, pos, pos) ~= "\"" do
local c
c = string.sub(s, pos, pos)
if c == "\\" then
if pos < string.len(s) then
if pos < #s then
pos = pos + 1
c = string.sub(s, pos, pos)
else
@ -1477,7 +1477,7 @@ local function read_quoted_string(s, pos)
chars[#chars + 1] = c
pos = pos + 1
end
if pos > string.len(s) or string.sub(s, pos, pos) ~= "\"" then
if pos > #s or string.sub(s, pos, pos) ~= "\"" then
return nil
end
@ -1561,7 +1561,7 @@ local function read_auth_challenge(s, pos)
params = {}
pos = skip_space(s, pos)
while pos < string.len(s) do
while pos < #s do
local name, val
local tmp_pos
@ -1595,7 +1595,7 @@ local function read_auth_challenge(s, pos)
pos = skip_space(s, pos)
if string.sub(s, pos, pos) == "," then
pos = skip_space(s, pos + 1)
if pos > string.len(s) then
if pos > #s then
return nil
end
end
@ -1615,7 +1615,7 @@ function parse_www_authenticate(s)
local pos
pos = 1
while pos <= string.len(s) do
while pos <= #s do
local challenge
pos, challenge = read_auth_challenge(s, pos)
@ -1696,7 +1696,7 @@ function can_use_head(host, port, result_404, path)
if data.status and data.status == 200 and data.header then
-- check that a body wasn't returned
if string.len(data.body) > 0 then
if #data.body > 0 then
stdnse.print_debug(1, "HTTP: Warning: Host returned data when performing HEAD.")
return false
end
@ -1892,7 +1892,7 @@ function page_exists(data, result_404, known_404, page, displayall)
if(data.status == 200) then
if(result_404 == 200) then
-- If the 404 response is also "200", deal with it (check if the body matches)
if(string.len(data.body) == 0) then
if(#data.body == 0) then
-- I observed one server that returned a blank string instead of an error, on some occasions
stdnse.print_debug(1, "HTTP: Page returned a totally empty body; page likely doesn't exist")
return false

View file

@ -248,7 +248,7 @@ compare_ip = function( left, op, right )
return nil, table.concat( err, " " )
end
if string.len( left ) ~= string.len( right ) then
if # left ~= # right then
-- shouldn't happen...
return nil, "Error in ipOps.compare_ip: Binary IP addresses were of different lengths."
end
@ -262,7 +262,7 @@ compare_ip = function( left, op, right )
-- starting from the leftmost bit, subtract the bit in right from the bit in left
local compare
for i = 1, string.len( left ), 1 do
for i = 1, # left , 1 do
compare = tonumber( string.sub( left, i, i ) ) - tonumber( string.sub( right, i, i ) )
if compare == 1 then
return true
@ -485,7 +485,7 @@ get_last_ip = function( ip, prefix )
if err then return nil, err end
prefix = tonumber( prefix )
if not prefix or ( prefix < 0 ) or ( prefix > string.len( first ) ) then
if not prefix or ( prefix < 0 ) or ( prefix > # first ) then
return nil, "Error in ipOps.get_last_ip: Invalid prefix length."
end
@ -534,7 +534,7 @@ ip_to_bin = function( ip )
-- padding
for i, v in ipairs( t ) do
t[i] = mask:sub( 1, string.len( mask ) - string.len( v ) ) .. v
t[i] = mask:sub( 1, # mask - # v ) .. v
end
return hex_to_bin( table.concat( t ) )
@ -559,9 +559,9 @@ bin_to_ip = function( binstring )
end
local af
if string.len( binstring ) == 32 then
if # binstring == 32 then
af = 4
elseif string.len( binstring ) == 128 then
elseif # binstring == 128 then
af = 6
else
return nil, "Error in ipOps.bin_to_ip: Expected exactly 32 or 128 binary digits."
@ -609,7 +609,7 @@ hex_to_bin = function( hex )
local t, mask, binchar = {}, "0000"
for hexchar in string.gmatch( hex, "%x" ) do
binchar = stdnse.tobinary( tonumber( hexchar, 16 ) )
t[#t+1] = mask:sub( 1, string.len( mask ) - string.len( binchar ) ) .. binchar
t[#t+1] = mask:sub( 1, # mask - # binchar ) .. binchar
end
return table.concat( t )

View file

@ -76,15 +76,15 @@ local tagEncoder = {}
tagEncoder['table'] = function(self, val)
if (val._ldap == '0A') then
local ival = self.encodeInt(val[1])
local len = self.encodeLength(string.len(ival))
local len = self.encodeLength(#ival)
return bin.pack('HAA', '0A', len, ival)
end
if (val._ldaptype) then
local len
if val[1] == nil or string.len(val[1]) == 0 then
if val[1] == nil or #val[1] == 0 then
return bin.pack('HC', val._ldaptype, 0)
else
len = self.encodeLength(string.len(val[1]))
len = self.encodeLength(#val[1])
return bin.pack('HAA', val._ldaptype, len, val[1])
end
end
@ -97,7 +97,7 @@ tagEncoder['table'] = function(self, val)
if (val["_snmp"]) then
tableType = bin.pack("H", val["_snmp"])
end
return bin.pack('AAA', tableType, self.encodeLength(string.len(encVal)), encVal)
return bin.pack('AAA', tableType, self.encodeLength(#encVal), encVal)
end

View file

@ -47,7 +47,7 @@ end
numbytes = function(num)
local n = num
return function(buf)
if(string.len(buf) >=n) then
if(#buf >=n) then
return n, n
end
return nil

View file

@ -113,7 +113,7 @@ function toBson(dict)
end
end
-- Get length
local length = string.len(elements) + 5
local length = #elements + 5
if length > 4 * 1024 * 1024 then
return false, "document too large - BSON documents are limited to 4 MB"
@ -563,7 +563,7 @@ function query(socket, data)
end
local bsonData = responseHeader["bson"]
if string.len(bsonData) == 0 then
if #bsonData == 0 then
dbg("No BSon data returned ")
return false, "No Bson data returned"
end
@ -626,7 +626,7 @@ function test()
local res
res = versionQuery()
print(type(res),res:len(),res)
local out= bin.unpack('C'..string.len(res),res)
local out= bin.unpack('C'..#res,res)
printBuffer(res)
end
--test()

View file

@ -309,7 +309,7 @@ function call_function(smbstate, opnum, arguments)
0x00, -- Packet type (0x00 = request)
0x03, -- Packet flags (0x03 = first frag + last frag)
0x10000000, -- Data representation (big endian)
0x18 + string.len(arguments), -- Frag length (0x18 = the size of this data)
0x18 + #arguments, -- Frag length (0x18 = the size of this data)
0x0000, -- Auth length
0x41414141, -- Call ID (I use 'AAAA' because it's easy to recognize)
0x00000038, -- Alloc hint
@ -318,7 +318,7 @@ function call_function(smbstate, opnum, arguments)
arguments
)
stdnse.print_debug(3, "MSRPC: Calling function 0x%02x with %d bytes of arguments", string.len(arguments), opnum)
stdnse.print_debug(3, "MSRPC: Calling function 0x%02x with %d bytes of arguments", #arguments, opnum)
-- Pass the information up to the smb layer
status, result = smb.write_file(smbstate, data, 0)
@ -391,7 +391,7 @@ function call_function(smbstate, opnum, arguments)
result['arguments'] = arguments
stdnse.print_debug(3, "MSRPC: Function call successful, %d bytes of returned argumenst", string.len(result['arguments']))
stdnse.print_debug(3, "MSRPC: Function call successful, %d bytes of returned argumenst", #result['arguments'])
return true, result
end
@ -4462,7 +4462,7 @@ RRAS_Opnums["RasRpcGetVersion"] = 15
function RRAS_SubmitRequest(smbstate, pReqBuffer, dwcbBufSize)
--sanity check
if(dwcbBufSize == nil) then
dwcbBufSize = string.len(pReqBuffer)
dwcbBufSize = #pReqBuffer
end
--pack the request
local req_blob
@ -4581,9 +4581,9 @@ function DNSSERVER_Query(smbstate, server_name, zone, operation)
srv_name_utf16 = msrpctypes.string_to_unicode(server_name, true)
req_blob = bin.pack("<IIIIAA",
unique_ptr,
string.len(srv_name_utf16)/2,
#srv_name_utf16/2,
0,
string.len(srv_name_utf16)/2,
#srv_name_utf16/2,
srv_name_utf16,
get_pad(srv_name_utf16, 4))
--[in, unique, string] LPCSTR pszZone,
@ -4593,9 +4593,9 @@ function DNSSERVER_Query(smbstate, server_name, zone, operation)
zone_ascii = zone .. string.char(0x00)
req_blob = req_blob .. bin.pack("<IIIIAA",
unique_ptr + 1,
string.len(zone_ascii),
#zone_ascii,
0,
string.len(zone_ascii),
#zone_ascii,
zone_ascii,
get_pad(zone_ascii, 4))
end
@ -4603,9 +4603,9 @@ function DNSSERVER_Query(smbstate, server_name, zone, operation)
operation_ascii = operation .. string.char(0x00)
req_blob = req_blob .. bin.pack("<IIIIAA",
unique_ptr+2,
string.len(operation_ascii),
#operation_ascii,
0,
string.len(operation_ascii),
#operation_ascii,
operation_ascii,
get_pad(operation_ascii, 4))
@ -4652,7 +4652,7 @@ end
--####################################################################--
function get_pad(data, align, pad_byte)
pad_byte = pad_byte or "\00"
return string.rep(pad_byte, (align-string.len(data)%align)%align)
return string.rep(pad_byte, (align-#data%align)%align)
end
--####################################################################--

View file

@ -142,7 +142,7 @@ function string_to_unicode(string, do_null)
-- Loop through the string, adding each character followed by a char(0)
for i = 1, string.len(string), 1 do
for i = 1, #string, 1 do
result = result .. string.sub(string, i, i) .. string.char(0)
end
@ -152,7 +152,7 @@ function string_to_unicode(string, do_null)
end
-- Align it to a multiple of 4, if necessary
if(string.len(result) % 4 ~= 0) then
if(#result % 4 ~= 0) then
result = result .. string.char(0) .. string.char(0)
end
@ -549,9 +549,9 @@ function marshall_unicode(str, do_null, max_length)
end
if(do_null) then
buffer_length = string.len(str) + 1
buffer_length = #str + 1
else
buffer_length = string.len(str)
buffer_length = #str
end
if(max_length == nil) then
@ -580,13 +580,13 @@ function marshall_ascii(str, max_length)
local result
local padding = ""
buffer_length = string.len(str) + 1
buffer_length = #str + 1
if(max_length == nil) then
max_length = buffer_length
end
while((string.len(str .. string.char(0) .. padding) % 4) ~= 0) do
while((#(str .. string.char(0 .. padding)) % 4) ~= 0) do
padding = padding .. string.char(0)
end
@ -1654,14 +1654,14 @@ local function marshall_lsa_String_internal(location, str, max_length, do_null)
if(str == nil) then
max_length = 0
else
max_length = string.len(str)
max_length = #str
end
end
if(str == nil) then
length = 0
else
length = string.len(str)
length = #str
end
if(do_null == nil) then
@ -2685,7 +2685,7 @@ function marshall_winreg_StringBuf(table, max_length)
if(name == nil) then
max_length = 0
else
max_length = string.len(name) + 1
max_length = #name + 1
end
end
@ -2697,7 +2697,7 @@ function marshall_winreg_StringBuf(table, max_length)
if(name == nil) then
length = 0
else
length = string.len(name) + 1
length = #name + 1
end
result = bin.pack("<SSA", length * 2, max_length * 2, marshall_ptr(ALL, marshall_unicode, {name, true, max_length}, name))

View file

@ -31,7 +31,7 @@ function name_encode(name, scope)
stdnse.print_debug(3, "Encoding name '%s'", name)
-- Truncate or pad the string to 16 bytes
if(string.len(name) >= 16) then
if(#name >= 16) then
name = string.sub(name, 1, 16)
else
local padding = " "
@ -41,7 +41,7 @@ function name_encode(name, scope)
repeat
name = name .. padding
until string.len(name) == 16
until #name == 16
end
-- Convert to uppercase
@ -49,7 +49,7 @@ function name_encode(name, scope)
-- Do the L1 encoding
local L1_encoded = ""
for i=1, string.len(name), 1 do
for i=1, #name, 1 do
local b = string.byte(name, i)
L1_encoded = L1_encoded .. string.char(bit.rshift(bit.band(b, 0xF0), 4) + 0x41)
L1_encoded = L1_encoded .. string.char(bit.rshift(bit.band(b, 0x0F), 0) + 0x41)
@ -62,7 +62,7 @@ function name_encode(name, scope)
-- Split the scope at its periods
local piece
for piece in string.gmatch(scope, "[^.]+") do
L2_encoded = L2_encoded .. string.char(string.len(piece)) .. piece
L2_encoded = L2_encoded .. string.char(#piece) .. piece
end
end
@ -97,15 +97,15 @@ function name_decode(encoded_name)
-- Decode the scope
local pos = 34
while string.len(encoded_name) > pos do
while #encoded_name > pos do
local len = string.byte(encoded_name, pos)
scope = scope .. string.sub(encoded_name, pos + 1, pos + len) .. "."
pos = pos + 1 + len
end
-- If there was a scope, remove the trailing period
if(string.len(scope) > 0) then
scope = string.sub(scope, 1, string.len(scope) - 1)
if(#scope > 0) then
scope = string.sub(scope, 1, #scope - 1)
end
stdnse.print_debug(3, "=> '%s'", name)

View file

@ -62,7 +62,7 @@ end
function print_hex(str)
-- Prints out the full lines
for line=1, string.len(str)/16, 1 do
for line=1, #str/16, 1 do
io.write(string.format("%08x ", (line - 1) * 16))
-- Loop through the string, printing the hex
@ -86,17 +86,17 @@ function print_hex(str)
end
-- Prints out the final, partial line
local line = math.floor((string.len(str)/16)) + 1
local line = math.floor((#str/16)) + 1
io.write(string.format("%08x ", (line - 1) * 16))
for char=1, string.len(str) % 16, 1 do
for char=1, #str % 16, 1 do
local ch = string.byte(str, ((line - 1) * 16) + char)
io.write(string.format("%02x ", ch))
end
io.write(string.rep(" ", 16 - (string.len(str) % 16)));
io.write(string.rep(" ", 16 - (#str % 16)));
io.write(" ")
for char=1, string.len(str) % 16, 1 do
for char=1, #str % 16, 1 do
local ch = string.byte(str, ((line - 1) * 16) + char)
if ch < 0x20 or ch > 0x7f then
ch = string.byte(".", 1)
@ -105,7 +105,7 @@ function print_hex(str)
end
-- Print out the length
io.write(string.format("\n Length: %d [0x%x]\n", string.len(str), string.len(str)))
io.write(string.format("\n Length: %d [0x%x]\n", #str, #str))
end

View file

@ -128,7 +128,7 @@ Packet = {}
--- Create a new Packet object.
-- @param packet Binary string with packet data.
-- @param packet_len Packet length. It could be more than
-- <code>string.len(packet)</code>.
-- <code>#packet</code>.
-- @param force_continue whether an error in parsing headers should be fatal or
-- not. This is especially useful when parsing ICMP packets, where a small ICMP
-- payload could be a TCP header. The problem is that parsing this payload
@ -236,7 +236,7 @@ end
-- @return Whether the parsing succeeded.
function Packet:ip_parse(force_continue)
self.ip_offset = 0
if string.len(self.buf) < 20 then -- too short
if #self.buf < 20 then -- too short
return false
end
self.ip_v = bit.rshift(bit.band(self:u8(self.ip_offset + 0), 0xF0), 4)
@ -391,7 +391,7 @@ end
-- @return Whether the parsing succeeded.
function Packet:icmp_parse(force_continue)
self.icmp_offset = self.ip_data_offset
if string.len(self.buf) < self.icmp_offset + 8 then -- let's say 8 bytes minimum
if #self.buf < self.icmp_offset + 8 then -- let's say 8 bytes minimum
return false
end
self.icmp = true
@ -403,7 +403,7 @@ function Packet:icmp_parse(force_continue)
self.icmp_payload = true
self.icmp_r0 = self:u32(self.icmp_offset + 4)
self.icmp_payload_offset = self.icmp_offset + 8
if string.len(self.buf) < self.icmp_payload_offset + 24 then
if #self.buf < self.icmp_payload_offset + 24 then
return false
end
self.icmp_payload = Packet:new(self.buf:sub(self.icmp_payload_offset+1), self.packet_len - self.icmp_payload_offset, true)
@ -423,12 +423,12 @@ end
function Packet:tcp_parse(force_continue)
self.tcp = true
self.tcp_offset = self.ip_data_offset
if string.len(self.buf) < self.tcp_offset + 4 then
if #self.buf < self.tcp_offset + 4 then
return false
end
self.tcp_sport = self:u16(self.tcp_offset + 0)
self.tcp_dport = self:u16(self.tcp_offset + 2)
if string.len(self.buf) < self.tcp_offset + 20 then
if #self.buf < self.tcp_offset + 20 then
if force_continue then
return true
else
@ -609,12 +609,12 @@ end
function Packet:udp_parse(force_continue)
self.udp = true
self.udp_offset = self.ip_data_offset
if string.len(self.buf) < self.udp_offset + 4 then
if #self.buf < self.udp_offset + 4 then
return false
end
self.udp_sport = self:u16(self.udp_offset + 0)
self.udp_dport = self:u16(self.udp_offset + 2)
if string.len(self.buf) < self.udp_offset + 8 then
if #self.buf < self.udp_offset + 8 then
if force_continue then
return true
else

View file

@ -670,7 +670,7 @@ local function smb_encode_parameters(parameters, overrides)
-- Make sure we have an overrides array
overrides = overrides or {}
return bin.pack("<CA", (overrides['parameters_length'] or (string.len(parameters) / 2)), parameters)
return bin.pack("<CA", (overrides['parameters_length'] or (#parameters / 2)), parameters)
end
--- Converts a string containing the data section into the encoded data string.
@ -686,7 +686,7 @@ local function smb_encode_data(data, overrides)
-- Make sure we have an overrides array
overrides = overrides or {}
return bin.pack("<SA", (overrides['data_length'] or string.len(data)), data)
return bin.pack("<SA", (overrides['data_length'] or #data), data)
end
---Sign the message, if possible. This is done by replacing the signature with the sequence
@ -774,12 +774,12 @@ function smb_send(smb, header, parameters, data, overrides)
-- Calculate the message signature
body = message_sign(smb, body)
local out = bin.pack(">I<A", string.len(body), body)
local out = bin.pack(">I<A", #body, body)
repeat
attempts = attempts - 1
stdnse.print_debug(3, "SMB: Sending SMB packet (len: %d, attempts remaining: %d)", string.len(out), attempts)
stdnse.print_debug(3, "SMB: Sending SMB packet (len: %d, attempts remaining: %d)", #out, attempts)
status, err = smb['socket']:send(out)
until(status or (attempts == 0))
@ -899,7 +899,7 @@ function smb_read(smb, read_data)
data = nil
end
stdnse.print_debug(3, "SMB: Received %d bytes", string.len(result))
stdnse.print_debug(3, "SMB: Received %d bytes", #result)
return true, header, parameters, data
end
@ -1650,7 +1650,7 @@ function create_file(smb, path, overrides)
0x00, -- ANDX reserved
0x0000, -- ANDX offset
0x00, -- Reserved
string.len(path), -- Path length
#path, -- Path length
(overrides['file_create_flags'] or 0x00000016), -- Create flags
(overrides['file_create_root_fid'] or 0x00000000), -- Root FID
(overrides['file_create_access_mask'] or 0x02000000), -- Access mask
@ -2106,9 +2106,9 @@ function send_transaction_named_pipe(smb, function_parameters, function_data, pi
-- Convert the parameter/data offsets into something more useful (the offset into the data section)
-- - 0x20 for the header, - 0x01 for the length.
parameter_offset = parameter_offset - 0x20 - 0x01 - string.len(parameters) - 0x02;
parameter_offset = parameter_offset - 0x20 - 0x01 - #parameters - 0x02;
-- - 0x20 for the header, - 0x01 for parameter length, the parameter length, and - 0x02 for the data length.
data_offset = data_offset - 0x20 - 0x01 - string.len(parameters) - 0x02;
data_offset = data_offset - 0x20 - 0x01 - #parameters - 0x02;
-- I'm not sure I entirely understand why the '+1' is here, but I think it has to do with the string starting at '1' and not '0'.
function_parameters = string.sub(data, parameter_offset + 1, parameter_offset + parameter_count)

View file

@ -34,7 +34,7 @@ tagEncoder['table'] = function(self, val)
-- counter or gauge or timeticks or opaque
elseif (val._snmp == '41' or val._snmp == '42' or val._snmp == '43' or val._snmp == '44') then
local val = self:encodeInt(val[1])
return bin.pack("HAA", val._snmp, self.encodeLength(string.len(val)), val)
return bin.pack("HAA", val._snmp, self.encodeLength(#val), val)
end
local encVal = ""
@ -46,7 +46,7 @@ tagEncoder['table'] = function(self, val)
if (val["_snmp"]) then
tableType = bin.pack("H", val["_snmp"])
end
return bin.pack('AAA', tableType, self.encodeLength(string.len(encVal)), encVal)
return bin.pack('AAA', tableType, self.encodeLength(#encVal), encVal)
end
---

View file

@ -96,7 +96,7 @@ function dump(t)
for i, row in ipairs(t) do
num_columns[i] = 0
for x, elem in pairs(row) do
local elem_width = string.len(elem)
local elem_width = #elem
if not column_width[x] or elem_width > column_width[x] then
column_width[x] = elem_width
end

View file

@ -350,5 +350,5 @@ function build_query(query)
for i,v in pairs(query) do
qstr = qstr .. i .. '=' .. v .. '&'
end
return string.sub(qstr, 0, string.len(qstr)-1)
return string.sub(qstr, 0, #qstr-1)
end