lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sunday 21, steve donovan wrote:
> On Sun, Feb 21, 2010 at 4:27 PM, Peter Smith <psmith135@gmail.com> wrote:
> > There is a related tvbrange:string() function but I can't figure out how
> > to use it in my case. Would you have an expert advise on it?

That function might be checking for embedded nulls (i.e. strlen(tvbrange) != 
tvbrange:len(). ).  There is another tvbrange:stringz() which might not have 
that restriction.

Over a year ago I created a wireshark protocol dissector [1] in Lua.  Some of 
the packets had to be decompressed, this required processing the full packet 
payload in Lua.  You might be able to do something like that to convert the 
packet to a Lua string.  The zero_decode function works on a ByteArray 
object, which you can get from calling TvbRange:bytes().  There might be some 
way to directly convert a ByteArray object into a string or you can use the 
ByteArray:get_index(idx) method to copy the bytes into a Lua string.

1. http://opensimulator.org/wiki/LLUDP_Dissector

-- 
Robert G. Jakabosky
local function grow_buff(buff, size)
	local old_size = buff:len()
	if old_size > size then return end
	-- buffer needs to grow
	buff:set_size(size)
	-- fill new space with zeros
	for i = old_size,size-1 do
		buff:set_index(i, 0)
	end
end

local function zero_decode(zero_buf)
	local out_buf = ByteArray.new()
	local zero_off = 0
	local zero_len = zero_buf:len()
	local out_size = 0
	local out_off = 0
	local b
	-- pre-allocate
	grow_buff(out_buf, zero_len)
	out_size = zero_len
	-- zero expand
	repeat
		b = zero_buf:get_index(zero_off)
		if b == 0 then
			-- get zero count
			local count = zero_buf:get_index(zero_off + 1)
			if count == 0 then count = 255 end
			out_off = out_off + count
			-- fill zeros
			if out_off > out_size then
				out_size = out_off + 128
				grow_buff(out_buf, out_size)
			end
			zero_off = zero_off + 2
		else
			if out_off >= out_size then
				out_size = out_off + (zero_len - zero_off) + 4
				grow_buff(out_buf, out_size)
			end
			-- copy non-zero bytes.
			out_buf:set_index(out_off,b)
			zero_off = zero_off + 1
			out_off = out_off + 1
		end
	until zero_off == zero_len
	-- truncate to real size.
	out_buf:set_size(out_off)
 
	return out_buf:tvb("Decompressed Data")
end