Skip to content

Commit 9388c23

Browse files
committedFeb 2, 2021
Handle UTF-16 correctly in Wireshark dissector
1 parent 674d67f commit 9388c23

File tree

1 file changed

+2
-31
lines changed

1 file changed

+2
-31
lines changed
 

‎util/wireshark/minetest.lua

+2-31
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ do
299299
t:add(f_length, buffer(2,2))
300300
local textlen = buffer(2,2):uint()
301301
if minetest_check_length(buffer, 4 + textlen*2, t) then
302-
t:add(f_message, minetest_convert_utf16(buffer(4, textlen*2), "Converted chat message"))
302+
t:add(f_message, buffer(4, textlen*2), buffer(4, textlen*2):ustring())
303303
end
304304
end
305305
}
@@ -1379,35 +1379,6 @@ function minetest_check_length(tvb, min_len, t)
13791379
end
13801380
end
13811381

1382-
-- Takes a Tvb or TvbRange (i.e. part of a packet) that
1383-
-- contains a UTF-16 string and returns a TvbRange containing
1384-
-- string converted to ASCII. Any characters outside the range
1385-
-- 0x20 to 0x7e are replaced by a question mark.
1386-
-- Parameter: tvb: Tvb or TvbRange that contains the UTF-16 data
1387-
-- Parameter: name: will be the name of the newly created Tvb.
1388-
-- Returns: New TvbRange containing the ASCII string.
1389-
-- TODO: Handle surrogates (should only produce one question mark)
1390-
-- TODO: Remove this when Wireshark supports UTF-16 strings natively.
1391-
function minetest_convert_utf16(tvb, name)
1392-
local hex, pos, char
1393-
hex = ""
1394-
for pos = 0, tvb:len() - 2, 2 do
1395-
char = tvb(pos, 2):uint()
1396-
if (char >= 0x20 and char <= 0x7e) or char == 0x0a then
1397-
hex = hex .. string.format(" %02x", char)
1398-
else
1399-
hex = hex .. " 3F"
1400-
end
1401-
end
1402-
if hex == "" then
1403-
-- This is a hack to avoid a failed assertion in tvbuff.c
1404-
-- (function: ensure_contiguous_no_exception)
1405-
return ByteArray.new("00"):tvb(name):range(0,0)
1406-
else
1407-
return ByteArray.new(hex):tvb(name):range()
1408-
end
1409-
end
1410-
14111382
-- Decodes a variable-length string as ASCII text
14121383
-- t_textlen, t_text should be the ProtoFields created by minetest_field_helper
14131384
-- alternatively t_text can be a ProtoField.string and t_textlen can be nil
@@ -1438,7 +1409,7 @@ function minetest_decode_helper_utf16(tvb, t, lentype, offset, f_textlen, f_text
14381409
end
14391410
local textlen = tvb(offset, n):uint() * 2
14401411
if minetest_check_length(tvb, offset + n + textlen, t) then
1441-
t:add(f_text, minetest_convert_utf16(tvb(offset + n, textlen), "UTF-16 text"))
1412+
t:add(f_text, tvb(offset + n, textlen), tvb(offset + n, textlen):ustring())
14421413
return offset + n + textlen
14431414
end
14441415
end

0 commit comments

Comments
 (0)
Please sign in to comment.