This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

How do I decode BCDs in Lua?

0

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 0x12345678 is sent on the wire as 0x21436587. I have to dissect it and show it as 12345678.

asked 30 Jan '13, 08:24

Aruna%20Sirigere's gravatar image

Aruna Sirigere
6224
accept rate: 0%

edited 03 Feb '13, 10:21

helloworld's gravatar image

helloworld
3.1k42041


2 Answers:

3

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:

-- assuming myString = "21436587"
local fixed = myString:gsub("(.)(.)", "%2%1")

That's just a shorthand for:

local fixed = string.gsub(myString, "(.)(.)", "%2%1")

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:

-- assuming 'buf' is the Tvb object passed into dissector
local tvb = buf(offset,length)  -- gets a TvbRange from your packet, starting at offset, for length bytes
local range = tvb:bytes()  -- converts TvbRange to ByteRange
local hexStr =  tostring(range)  -- turns ByteRange into hex representation string, e.g, "21:43:65:87"
local fixed = hexStr:gsub("(.)(.):?", "%2%1")  -- reverses the nibbles and removes the colons

That's the really long/verbose way, which can be shortened to something like this:

local fixed = tostring(buf(offset,length):bytes()):gsub("(.)(.):?", "%2%1")

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:

local fixed = tostring(buf(offset,length)):gsub("(.)(.)", "%2%1")

answered 03 Feb '13, 08:27

Hadriel's gravatar image

Hadriel
2.7k2939
accept rate: 18%

edited 03 Feb '13, 09:27

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")

(04 Feb '13, 22:59) Aruna Sirigere

0

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's gravatar image

Anders ♦
4.6k952
accept rate: 17%

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