Ethereal-dev: [Ethereal-dev] New: bitfield_string

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: "Peter J. Creath" <peter-ethereal@xxxxxxxxxx>
Date: Mon, 9 Feb 2004 14:57:12 -0500
Greetings,

This is a sister utility to value_string.[ch] that I whipped up to make dissecting bitfields a bit easier. It should probably live in epan/ if folks think it's worth adding to the tree. I've only attached the new .c/.h files (and didn't include the modified makefiles) so that people can look it over and see if it's even of interest.

Basically, it lets you generate both single-line and multi-line (tree) representations of bitfields by defining a table. It could probably be used to drastically simplify dissect_xdlc_control in xdlc.c. Here's a quick example of its use:

const bitfield_string my_bits[] = {
  { &hf_low, 0x0F, -1,   "LO=%d",    NULL },
  { &hf_mid, 0x10, -1,   "MID",      NULL },
  { &hf_hi,  0x60, 0x00, "HI=up",    NULL },
  { &hf_hi,  0x60, 0x20, "HI=down",  NULL },
  { &hf_hi,  0x60, 0x40, "HI=left",  NULL },
  { &hf_hi,  0x60, 0x60, "HI=right", NULL },
  { &hf_unk, -1,   -1,   "0x%02X",   NULL },
  { NULL, -1, -1, NULL, NULL }
};

bitfield_to_str(0xBA, my_bits, " | ") would return:

  "LO=10 | MID | HI=down | 0x80"

and proto_tree_add_bitfield(tree, my_bits, tvb, 0, 1, 0xBA, 8) would add:

  ".... 1010 = LO=10"
  "...1 .... = MID"
  ".01. .... = HI=down"
  "1... .... = 0x80"

to the proto_tree. For convenience, there's also a proto_tree_add_bitfield_inv() function which does the tree in the opposite order, but it doesn't work for complicated bitfields. (Is there any way to insert items in a tree and not just append them?)

I hope someone else finds this useful.

	-P

Attachment: %bitfield_string.h
Description: application/applefile

Attachment: bitfield_string.h
Description: Binary data

Attachment: %bitfield_string.c
Description: application/applefile

Attachment: bitfield_string.c
Description: Binary data