1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Convert unicode.lua to use native bitwise ops

This commit is contained in:
dmiller
2017-03-09 04:04:06 +00:00
parent 5f87f3a1ac
commit bc7026e378

View File

@@ -5,8 +5,6 @@
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html -- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
local bit = require "bit"
local bin = require "bin"
local string = require "string" local string = require "string"
local table = require "table" local table = require "table"
local stdnse = require "stdnse" local stdnse = require "stdnse"
@@ -15,13 +13,11 @@ _ENV = stdnse.module("unicode", stdnse.seeall)
-- Localize a few functions for a tiny speed boost, since these will be looped -- Localize a few functions for a tiny speed boost, since these will be looped
-- over every char of a string -- over every char of a string
local band = bit.band
local lshift = bit.lshift
local rshift = bit.rshift
local byte = string.byte local byte = string.byte
local char = string.char local char = string.char
local pack = string.pack local pack = string.pack
local unpack = string.unpack local unpack = string.unpack
local concat = table.concat
---Decode a buffer containing Unicode data. ---Decode a buffer containing Unicode data.
@@ -98,7 +94,7 @@ function utf16_enc(cp, bigendian)
return pack(fmt, cp) return pack(fmt, cp)
elseif cp <= 0x10FFFF then elseif cp <= 0x10FFFF then
cp = cp - 0x10000 cp = cp - 0x10000
return pack(fmt .. fmt, 0xD800 + rshift(cp, 10), 0xDC00 + band(cp, 0x3FF)) return pack(fmt .. fmt, 0xD800 + (cp >> 10), 0xDC00 + (cp & 0x3FF))
else else
return nil return nil
end end
@@ -124,7 +120,7 @@ function utf16_dec(buf, pos, bigendian)
local cp local cp
cp, pos = unpack(fmt, buf, pos) cp, pos = unpack(fmt, buf, pos)
if cp >= 0xD800 and cp <= 0xDFFF then if cp >= 0xD800 and cp <= 0xDFFF then
local high = lshift(cp - 0xD800, 10) local high = (cp - 0xD800) << 10
cp, pos = unpack(fmt, buf, pos) cp, pos = unpack(fmt, buf, pos)
cp = 0x10000 + high + cp - 0xDC00 cp = 0x10000 + high + cp - 0xDC00
end end
@@ -161,8 +157,8 @@ function utf8_enc(cp)
end end
while n > 1 do while n > 1 do
bytes[n] = char(0x80 + band(cp, 0x3F)) bytes[n] = char(0x80 + (cp & 0x3F))
cp = rshift(cp, 6) cp = cp >> 6
n = n - 1 n = n - 1
end end
bytes[1] = char(mask + cp) bytes[1] = char(mask + cp)
@@ -209,7 +205,7 @@ function utf8_dec(buf, pos)
if bv < 0x80 or bv > 0xBF then if bv < 0x80 or bv > 0xBF then
return nil, string.format("Invalid UTF-8 sequence at %d", pos + i) return nil, string.format("Invalid UTF-8 sequence at %d", pos + i)
end end
cp = lshift(cp, 6) + band(bv, 0x3F) cp = (cp << 6) + (bv & 0x3F)
end end
return pos + 1 + n, cp return pos + 1 + n, cp