In my Lua dissector, I have to decode BCDs. I see no readily available tools for this in Wireshark Lua. Can someone explain how I can do this? For example, a BCD of asked 30 Jan '13, 08:24 Aruna Sirigere edited 03 Feb '13, 10:21 helloworld |
2 Answers:
It depends on how you have the BCD sequence of bytes in Lua. For example do you literally have a Lua string of "21436587"? If so, then simply doing this will work:
That's just a shorthand for:
But if the BCD is in the packet and your Lua script only has a Tvb object, for example from a dissector function being called, then one way is to turn it into a Lua string of hex ascii, since BCD is just nibble-reversed hex representation. I could be wrong, but I don't think Wireshark's API provides a quick accessor to Tvb to just get a hex ascii representation, so you have to go through ByteRange to get it. So for example, do this:
That's the really long/verbose way, which can be shortened to something like this:
Where 'offset' and 'length' are whatever numbers they need to be. Note I haven't tried the above snippets, but something like that should work. Update: actually it looks like tostring metamethod of TvbRange gets the hex string, and without colons, so this should work:
answered 03 Feb '13, 08:27 Hadriel edited 03 Feb '13, 09:27 |
I'm no LUA expert but either you have to fetch byte by byte and do the conversion or add something similar to tvb_bcd_dig_to_ep_str() to the LUA methods in wslua_tvb.c I suppose. answered 30 Jan '13, 14:47 Anders ♦ Hello Anders, Thanks for your response. I am not using C program. I am using LUA script and I just want to use readily available functions to do it. If no ready functions available, at least if some one can give the lua script using some logic. Thanks in Advance. (31 Jan '13, 01:45) Aruna Sirigere |
Thanks a lot Hadriel. Below code made the trick.. it did exactly what I was looking for.
local fixed = tostring(buf(offset,length)):gsub("(.)(.)", "%2%1")