From bc7026e378d0a844acc9bbef354ef1d61c025e8a Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 9 Mar 2017 04:04:06 +0000 Subject: [PATCH] Convert unicode.lua to use native bitwise ops --- nselib/unicode.lua | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/nselib/unicode.lua b/nselib/unicode.lua index d7ec93bec..a5200b147 100644 --- a/nselib/unicode.lua +++ b/nselib/unicode.lua @@ -5,8 +5,6 @@ -- @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 table = require "table" 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 -- over every char of a string -local band = bit.band -local lshift = bit.lshift -local rshift = bit.rshift local byte = string.byte local char = string.char local pack = string.pack local unpack = string.unpack +local concat = table.concat ---Decode a buffer containing Unicode data. @@ -98,7 +94,7 @@ function utf16_enc(cp, bigendian) return pack(fmt, cp) elseif cp <= 0x10FFFF then 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 return nil end @@ -124,7 +120,7 @@ function utf16_dec(buf, pos, bigendian) local cp cp, pos = unpack(fmt, buf, pos) 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 = 0x10000 + high + cp - 0xDC00 end @@ -161,8 +157,8 @@ function utf8_enc(cp) end while n > 1 do - bytes[n] = char(0x80 + band(cp, 0x3F)) - cp = rshift(cp, 6) + bytes[n] = char(0x80 + (cp & 0x3F)) + cp = cp >> 6 n = n - 1 end bytes[1] = char(mask + cp) @@ -209,7 +205,7 @@ function utf8_dec(buf, pos) if bv < 0x80 or bv > 0xBF then return nil, string.format("Invalid UTF-8 sequence at %d", pos + i) end - cp = lshift(cp, 6) + band(bv, 0x3F) + cp = (cp << 6) + (bv & 0x3F) end return pos + 1 + n, cp