1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-16 04:39:03 +00:00

Simplifies code; possibly ipOps candidate

This commit is contained in:
nnposter
2018-08-05 21:29:37 +00:00
parent 3fc825b15e
commit 1e1f7c80c2
2 changed files with 12 additions and 22 deletions

View File

@@ -366,21 +366,16 @@ function get_prefix_length( range )
local first, last, err = ipOps.get_ips_from_range( range ) local first, last, err = ipOps.get_ips_from_range( range )
if err then return nil end if err then return nil end
first = ipOps.ip_to_bin( first ):reverse() first = ipOps.ip_to_bin(first)
last = ipOps.ip_to_bin( last ):reverse() last = ipOps.ip_to_bin(last)
local hostbits = 0 for pos = 1, #first do
for pos = 1, # first , 1 do if first:sub(pos, pos) ~= last:sub(pos, pos) then
return pos - 1
if first:sub( pos, pos ) == "0" and last:sub( pos, pos ) == "1" then
hostbits = hostbits + 1
else
break
end end
end end
return ( # first - hostbits ) return #first
end end

View File

@@ -434,21 +434,16 @@ function get_prefix_length( range )
local first, last, err = ipOps.get_ips_from_range( range ) local first, last, err = ipOps.get_ips_from_range( range )
if err then return nil end if err then return nil end
first = ipOps.ip_to_bin( first ):reverse() first = ipOps.ip_to_bin(first)
last = ipOps.ip_to_bin( last ):reverse() last = ipOps.ip_to_bin(last)
local hostbits = 0 for pos = 1, #first do
for pos = 1, string.len( first ), 1 do if first:sub(pos, pos) ~= last:sub(pos, pos) then
return pos - 1
if first:sub( pos, pos ) == "0" and last:sub( pos, pos ) == "1" then
hostbits = hostbits + 1
else
break
end end
end end
return ( string.len( first ) - hostbits ) return #first
end end