1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +00:00

Replace a couple of conversion functions with transcoders from unicode.lua

This commit is contained in:
dmiller
2018-09-19 04:09:54 +00:00
parent cb24d657e2
commit b5ab73d44d
2 changed files with 30 additions and 74 deletions

View File

@@ -115,6 +115,7 @@ local stdnse = require "stdnse"
local strbuf = require "strbuf"
local string = require "string"
local table = require "table"
local unicode = require "unicode"
_ENV = stdnse.module("mssql", stdnse.seeall)
-- Created 01/17/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
@@ -814,7 +815,7 @@ ColumnInfo =
pos, colinfo.msglen = bin.unpack("<C", data, pos )
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos)
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -829,7 +830,7 @@ ColumnInfo =
pos, colinfo.unknown, colinfo.msglen = bin.unpack("<CC", data, pos)
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos )
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -848,7 +849,7 @@ ColumnInfo =
pos, colinfo.msglen = bin.unpack("C", data, pos)
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos )
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -868,7 +869,7 @@ ColumnInfo =
pos, colinfo.unknown, colinfo.precision, colinfo.scale = bin.unpack("<CCC", data, pos)
pos, colinfo.msglen = bin.unpack("<C",data,pos)
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos )
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -895,7 +896,7 @@ ColumnInfo =
pos, colinfo.lts, colinfo.msglen = bin.unpack("<SC", data, pos)
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos )
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -919,7 +920,7 @@ ColumnInfo =
pos, colinfo.lts, colinfo.codepage, colinfo.flags, colinfo.charset,
colinfo.msglen = bin.unpack("<SSSCC", data, pos )
pos, tmp = bin.unpack("A" .. (colinfo.msglen * 2), data, pos)
colinfo.text = Util.FromWideChar(tmp)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
@@ -1055,7 +1056,7 @@ ColumnData =
pos, len = bin.unpack( "<I", data, pos )
pos, coldata = bin.unpack( "A"..len, data, pos )
return pos, Util.FromWideChar(coldata)
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.BITNTYPE] = function( data, pos )
@@ -1145,7 +1146,7 @@ ColumnData =
pos, len = bin.unpack( "<I", data, pos )
pos, coldata = bin.unpack( "A"..len, data, pos )
return pos, Util.FromWideChar(coldata)
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.FLTNTYPE] = function( data, pos )
@@ -1263,7 +1264,7 @@ ColumnData =
end
pos, coldata = bin.unpack( "A"..len, data, pos )
return pos, Util.FromWideChar(coldata)
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.SQLNCHAR] = function( data, pos )
@@ -1291,13 +1292,13 @@ Token =
token.type = TokenType.ErrorMessage
pos, token.size, token.errno, token.state, token.severity, token.errlen = bin.unpack( "<SICCS", data, pos )
pos, tmp = bin.unpack("A" .. (token.errlen * 2), data, pos )
token.error = Util.FromWideChar(tmp)
token.error = unicode.utf16to8(tmp)
pos, token.srvlen = bin.unpack("C", data, pos)
pos, tmp = bin.unpack("A" .. (token.srvlen * 2), data, pos )
token.server = Util.FromWideChar(tmp)
token.server = unicode.utf16to8(tmp)
pos, token.proclen = bin.unpack("C", data, pos)
pos, tmp = bin.unpack("A" .. (token.proclen * 2), data, pos )
token.proc = Util.FromWideChar(tmp)
token.proc = unicode.utf16to8(tmp)
pos, token.lineno = bin.unpack("<S", data, pos)
return pos, token
@@ -1509,7 +1510,7 @@ QueryPacket =
--
-- @return string containing the authentication packet
ToString = function( self )
return PacketType.Query, Util.ToWideChar( self.query )
return PacketType.Query, unicode.utf8to16( self.query )
end,
}
@@ -1874,16 +1875,16 @@ LoginPacket =
data = data .. bin.pack("<S", 0)
-- Auth info wide strings
data = data .. Util.ToWideChar(self.client)
data = data .. unicode.utf8to16(self.client)
if ( not(ntlmAuth) ) then
data = data .. Util.ToWideChar(self.username)
data = data .. unicode.utf8to16(self.username)
data = data .. Auth.TDS7CryptPass(self.password)
end
data = data .. Util.ToWideChar(self.app)
data = data .. Util.ToWideChar(self.server)
data = data .. Util.ToWideChar(self.library)
data = data .. Util.ToWideChar(self.locale)
data = data .. Util.ToWideChar(self.database)
data = data .. unicode.utf8to16(self.app)
data = data .. unicode.utf8to16(self.server)
data = data .. unicode.utf8to16(self.library)
data = data .. unicode.utf8to16(self.locale)
data = data .. unicode.utf8to16(self.database)
if ( ntlmAuth ) then
local NTLMSSP_NEGOTIATE = 1
@@ -1918,8 +1919,8 @@ NTAuthenticationPacket = {
ToString = function(self)
local ntlmssp = "NTLMSSP\0"
local NTLMSSP_AUTH = 3
local domain = Util.ToWideChar(self.domain:upper())
local user = Util.ToWideChar(self.username)
local domain = unicode.utf8to16(self.domain:upper())
local user = unicode.utf8to16(self.username)
local hostname, sessionkey = "", ""
local flags = 0x00008201
local ntlm_response = Auth.NtlmResponse(self.password, self.nonce)
@@ -3155,31 +3156,6 @@ Auth = {
--- "static" Utility class containing mostly conversion functions
Util =
{
--- Converts a string to a wide string
--
-- @param str string to be converted
-- @return string containing a two byte representation of str where a zero
-- byte character has been tagged on to each character.
ToWideChar = function( str )
return str:gsub("(.)", "%1\0" )
end,
--- Concerts a wide string to string
--
-- @param wstr containing the wide string to convert
-- @return string with every other character removed
FromWideChar = function( wstr )
local str = ""
if ( nil == wstr ) then
return nil
end
for i=1, wstr:len(), 2 do
str = str .. wstr:sub(i, i)
end
return str
end,
--- Takes a table as returned by Query and does some fancy formatting
-- better suitable for <code>stdnse.output_result</code>
--

View File

@@ -57,6 +57,7 @@ local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
local unicode = require "unicode"
_ENV = stdnse.module("ncp", stdnse.seeall)
@@ -494,7 +495,7 @@ ResponseParser = {
if ( iflags.BaseClass ) then
pos, len = bin.unpack("<I", data, pos)
pos, entry.baseclass = bin.unpack("A" .. len, data, pos)
entry.baseclass = Util.FromWideChar(entry.baseclass)
entry.baseclass = unicode.utf16to8(entry.baseclass)
entry.baseclass = Util.CToLuaString(entry.baseclass)
pos = ( len % 4 == 0 ) and pos or pos + ( 4 - ( len % 4 ) )
end
@@ -502,7 +503,7 @@ ResponseParser = {
if ( iflags.RelDN ) then
pos, len = bin.unpack("<I", data, pos)
pos, entry.rdn = bin.unpack("A" .. len, data, pos)
entry.rdn = Util.FromWideChar(entry.rdn)
entry.rdn = unicode.utf16to8(entry.rdn)
entry.rdn = Util.CToLuaString(entry.rdn)
pos = ( len % 4 == 0 ) and pos or pos + ( 4 - ( len % 4 ) )
end
@@ -510,7 +511,7 @@ ResponseParser = {
if ( iflags.DN ) then
pos, len = bin.unpack("<I", data, pos)
pos, entry.name = bin.unpack("A" .. len, data, pos)
entry.name = Util.FromWideChar(entry.name)
entry.name = unicode.utf16to8(entry.name)
entry.name = Util.CToLuaString(entry.name)
pos = ( len % 4 == 0 ) and pos or pos + ( 4 - ( len % 4 ) )
end
@@ -817,7 +818,7 @@ NCP = {
local pad = (4 - ( #name % 4 ) )
name = Util.ZeroPad(name, #name + pad)
local w_name = Util.ToWideChar(name)
local w_name = unicode.utf8to16(name)
local frag_handle, frag_size = 0xffffffff, 64176
local msg_size, unknown, proto_flags, nds_verb = 44 + #w_name, 0, 0, 1
local nds_reply_buf, version, flags, scope = 4096, 1, 0x2062, 0
@@ -885,8 +886,8 @@ NCP = {
local class = class and class .. '\0' or '*\0'
local name = name and name .. '\0' or '*\0'
local w_name = Util.ToWideChar(name)
local w_class = Util.ToWideChar(class)
local w_name = unicode.utf8to16(name)
local w_class = unicode.utf8to16(class)
local options = options or {}
local p = Packet:new()
p:setType(NCPType.ServiceRequest)
@@ -1079,27 +1080,6 @@ Helper = {
--- "static" Utility class containing mostly conversion functions
Util =
{
--- Converts a string to a wide string
--
-- @param str string to be converted
-- @return string containing a two byte representation of str where a zero
-- byte character has been tagged on to each character.
ToWideChar = function( str )
return str:gsub("(.)", "%1\0" )
end,
--- Concerts a wide string to string
--
-- @param wstr containing the wide string to convert
-- @return string with every other character removed
FromWideChar = function( wstr )
local str = ""
if ( nil == wstr ) then return nil end
for i=1, wstr:len(), 2 do str = str .. wstr:sub(i, i) end
return str
end,
--- Pads a string with zeroes
--
-- @param str string containing the string to be padded