Ethereal-dev: Re: [ethereal-dev] re: the new proto_tree_add_XXX_format() calls

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

From: Gilbert Ramirez <gram@xxxxxxxxxx>
Date: Tue, 25 Apr 2000 03:48:30 -0500
On Tue, Apr 25, 2000 at 06:04:19PM +1000, C. L. McAvaney wrote:
> G'day all,
> I am currently writing a dissector for a protocol that we use in my research 
> group locally and previous to versions 0.8.4 I was using the 
> proto_tree_add_item_format() call, and have since changed to the newer 
> seperated calls (Gerald pointed me in this direction), but there doesn't seem 
> to be one for the common "int" type.  I was wondering if somebody is 
> implementing this to be added or is there some other call that I should be 
> using??  I need an int version as I have some int types that I want to display 
> formatted a little differently sometimes.
> 
> Thanks,
> Christopher

Here's a patch which implements proto_tree_add_int_format() call. Let me know if
this works for you.

--gilbert
Index: proto.c
===================================================================
RCS file: /usr/local/cvsroot/ethereal/proto.c,v
retrieving revision 1.62
diff -u -r1.62 proto.c
--- proto.c	2000/04/13 18:18:55	1.62
+++ proto.c	2000/04/25 08:45:24
@@ -121,6 +121,8 @@
 proto_tree_set_double(field_info *fi, double value);
 static void
 proto_tree_set_uint(field_info *fi, guint32 value);
+static void
+proto_tree_set_int(field_info *fi, gint32 value);
 
 static int proto_register_field_init(header_field_info *hfinfo, int parent);
 
@@ -389,11 +391,14 @@
 		case FT_UINT16:
 		case FT_UINT24:
 		case FT_UINT32:
+			proto_tree_set_uint(fi, va_arg(ap, unsigned int));
+			break;
+
 		case FT_INT8:
 		case FT_INT16:
 		case FT_INT24:
 		case FT_INT32:
-			proto_tree_set_uint(fi, va_arg(ap, unsigned int));
+			proto_tree_set_int(fi, va_arg(ap, int));
 			break;
 
 		case FT_IPv4:
@@ -810,6 +815,57 @@
 	}
 }
 
+/* Add any FT_INT* to a proto_tree */
+proto_item *
+proto_tree_add_int_format(proto_tree *tree, int hfindex, gint start, gint length,
+		gint32 value, const char *format, ...)
+{
+	proto_item		*pi = NULL;
+	va_list			ap;
+	field_info		*new_fi;
+	header_field_info	*hfinfo;
+
+	if (!tree)
+		return (NULL);
+
+	hfinfo = proto_registrar_get_nth(hfindex);
+	switch(hfinfo->type) {
+		case FT_INT8:
+		case FT_INT16:
+		case FT_INT24:
+		case FT_INT32:
+			va_start(ap, format);
+			pi = proto_tree_add_pi(tree, hfindex, start, length,
+					format, TRUE, &new_fi, ap);
+			proto_tree_set_int(new_fi, value);
+			va_end(ap);
+			break;
+
+		default:
+			g_assert_not_reached();
+	}
+
+	return pi;
+}
+
+/* Set the FT_INT* value */
+static void
+proto_tree_set_int(field_info *fi, gint32 value)
+{
+	header_field_info *hfinfo;
+
+	hfinfo = fi->hfinfo;
+	fi->value.numeric = (guint32) value;
+	if (hfinfo->bitmask) {
+		/* Mask out irrelevant portions */
+		fi->value.numeric &= hfinfo->bitmask;
+
+		/* Shift bits */
+		if (hfinfo->bitshift > 0) {
+			fi->value.numeric >>= hfinfo->bitshift;
+		}
+	}
+}
 
 
 /* Create a new field_info struct, and initialize it */
Index: proto.h
===================================================================
RCS file: /usr/local/cvsroot/ethereal/proto.h,v
retrieving revision 1.28
diff -u -r1.28 proto.h
--- proto.h	2000/04/13 18:18:56	1.28
+++ proto.h	2000/04/25 08:45:27
@@ -307,6 +307,17 @@
 	gint length, guint32 value, const char *format, ...);
 #endif
 
+#if __GNUC__ == 2
+proto_item *
+proto_tree_add_int_format(proto_tree *tree, int hfindex, gint start,
+	gint length, gint32 value, const char *format, ...)
+	__attribute__((format (printf, 6, 7)));
+#else
+proto_item *
+proto_tree_add_int_format(proto_tree *tree, int hfindex, gint start,
+	gint length, gint32 value, const char *format, ...);
+#endif
+
 
 #if __GNUC__ == 2
 proto_item *