Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Ethereal-dev: [Ethereal-dev] patch: negative relative time fixes

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

From: Richard van der Hoff <richardv@xxxxxxxxxxxxx>
Date: Tue, 05 Jul 2005 04:08:14 +0100
Hi,

Here's a patch which fixes the following functions with respect to negative relative times:
  - get_timedelta()
  - addtime()
  - ftype-time.c:relative_val_from_unparsed()

I've also moved get_timedelta() and addtime() out of calldata.c into a new file, epan/nstime.c, as I needed to use them in a dissector I'm working on (and they therefore needed to go into libethereal).

Thanks,

Richard


--
Richard van der Hoff <richardv@xxxxxxxxxxxxx>
Systems Analyst
Tel: +44 (0) 845 666 7778
http://www.mxtelecom.com
Index: epan/nstime.c
===================================================================
RCS file: epan/nstime.c
diff -N epan/nstime.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ epan/nstime.c	5 Jul 2005 02:59:11 -0000	1.1
@@ -0,0 +1,92 @@
+/* nstime.c
+ * Routines for manipulating nstime_t structures
+ *
+ * Copyright (c) 2005 MX Telecom Ltd. <richardv@xxxxxxxxxxxxx>
+ * 
+ * $Id$
+ *
+ * Ethereal - Network traffic analyzer
+ * By Gerald Combs <gerald@xxxxxxxxxxxx>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "nstime.h"
+
+/* this is #defined so that we can clearly see that we have the right number of
+   zeros, rather than as a guard against the number of nanoseconds in a second
+   changing ;) */
+#define NS_PER_S 1000000000
+
+/*
+ * function: get_timedelta
+ * delta = b - a
+ */
+
+void get_timedelta(nstime_t *delta, const nstime_t *b, const nstime_t *a )
+{
+    if (b->secs == a->secs) {
+        /* The seconds part of b is the same as the seconds part of a, so if
+           the nanoseconds part of the first time is less than the nanoseconds
+           part of a, b is before a.  The nanoseconds part of the delta should
+           just be the difference between the nanoseconds part of b and the
+           nanoseconds part of a; don't adjust the seconds part of the delta,
+           as it's OK if the nanoseconds part is negative, and an overflow
+           can never result. */
+        delta->secs = 0;
+        delta->nsecs = b->nsecs - a->nsecs;
+    } else if (b->secs <= a->secs) {
+        /* The seconds part of b is less than the seconds part of a, so b is
+           before a.
+
+           Both the "seconds" and "nanoseconds" value of the delta
+           should have the same sign, so if the difference between the
+           nanoseconds values would be *positive*, subtract 1,000,000,000
+           from it, and add one to the seconds value. */
+        delta->secs = b->secs - a->secs;
+        delta->nsecs = b->nsecs - a->nsecs;
+        if(delta->nsecs > 0) {
+            delta->nsecs -= NS_PER_S;
+            delta->secs ++;
+        }
+    } else {
+        delta->secs = b->secs - a->secs;
+        delta->nsecs = b->nsecs - a->nsecs;
+        if(delta->nsecs < 0) {
+            delta->nsecs += NS_PER_S;
+            delta->secs --;
+        }
+    }
+}
+
+/*
+ * function: get_timesum
+ * sum = a + b
+ */
+
+void get_timesum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
+{
+    sum->secs = a->secs + b->secs;
+    sum->nsecs = a->nsecs + b->nsecs;
+    if(sum->nsecs>=NS_PER_S || (sum->nsecs>0 && sum->secs<0)){
+        sum->nsecs-=NS_PER_S;
+        sum->secs++;
+    } else if(sum->nsecs<=-NS_PER_S || (sum->nsecs<0 && sum->secs>0)) {
+        sum->nsecs+=NS_PER_S;
+        sum->secs--;
+    }    
+}
Index: epan/nstime.h
===================================================================
RCS file: /cvs/ethereal/epan/nstime.h,v
retrieving revision 1.1.1.2
retrieving revision 1.2
diff -u -a -u -r1.1.1.2 -r1.2
--- epan/nstime.h	27 Jul 2004 18:22:33 -0000	1.1.1.2
+++ epan/nstime.h	5 Jul 2005 02:59:11 -0000	1.2
@@ -32,4 +32,27 @@
 	int	nsecs;
 } nstime_t;
 
+/* functions */
+
+/* calculate the delta between two times
+ *
+ * delta = b-a
+ *
+ * Note that it is acceptable for two or more of the arguments to point at the
+ * same structure.
+ */
+extern void get_timedelta(nstime_t *delta, const nstime_t *b, const nstime_t *a );
+
+/* calculate the sum of two times
+ *
+ * sum = a+b
+ *
+ * Note that it is acceptable for two or more of the arguments to point at the
+ * same structure.
+ */
+extern void get_timesum(nstime_t *sum, const nstime_t *b, const nstime_t *a );
+
+/* sum += a */
+#define addtime(sum, a) get_timesum(sum, sum, b)
+
 #endif /* __NSTIME_H__  */
Index: epan/Makefile.common
===================================================================
RCS file: /cvs/ethereal/epan/Makefile.common,v
retrieving revision 1.1.1.6
retrieving revision 1.7
diff -u -a -u -r1.1.1.6 -r1.7
--- epan/Makefile.common	4 Jul 2005 18:17:28 -0000	1.1.1.6
+++ epan/Makefile.common	5 Jul 2005 02:59:11 -0000	1.7
@@ -52,6 +52,7 @@
 	in_cksum.c		\
 	ipproto.c		\
 	ipv4.c			\
+	nstime.c		\
 	osi-utils.c		\
 	packet.c		\
 	plugins.c		\
Index: timestats.c
===================================================================
RCS file: /cvs/ethereal/timestats.c,v
retrieving revision 1.1.1.3
retrieving revision 1.2
diff -u -a -u -r1.1.1.3 -r1.2
--- timestats.c	4 Jul 2005 18:17:26 -0000	1.1.1.3
+++ timestats.c	5 Jul 2005 02:59:11 -0000	1.2
@@ -26,36 +26,6 @@
 #include "timestats.h"
 
 /*
- * function: get_timedelta
- * delta = b - a
- */
-
-void get_timedelta(nstime_t *delta, const nstime_t *b, const nstime_t *a )
-{
-	delta->secs = b->secs - a->secs;
-	delta->nsecs= b->nsecs - a->nsecs;
-	if(delta->nsecs<0){
-		delta->nsecs+=1000000000;
-		delta->secs--;
-	}
-}
-
-/*
- * function: addtime
- * sum += a
- */
-
-void addtime(nstime_t *sum, const nstime_t *a)
-{
-	sum->secs += a->secs;
-	sum->nsecs += a->nsecs;
-	if(sum->nsecs>1000000000){
-		sum->nsecs-=1000000000;
-		sum->secs++;
-	}
-}
-
-/*
  * function: nstime_to_msec
  * converts nstime to gdouble, time base is milli seconds
  */
Index: timestats.h
===================================================================
RCS file: /cvs/ethereal/timestats.h,v
retrieving revision 1.1.1.3
retrieving revision 1.2
diff -u -a -u -r1.1.1.3 -r1.2
--- timestats.h	4 Jul 2005 18:17:26 -0000	1.1.1.3
+++ timestats.h	5 Jul 2005 02:59:11 -0000	1.2
@@ -43,12 +43,6 @@
 
 /* functions */
 
-/* delta = b - a */
-extern void get_timedelta(nstime_t *delta, const nstime_t *b, const nstime_t *a );
-
-/* sum += a */
-extern void addtime(nstime_t *sum, const nstime_t *a);
-
 /* converts nstime to gdouble, time base is milli seconds*/
 extern gdouble nstime_to_msec(const nstime_t *time);
 
Index: epan/ftypes/ftype-time.c
===================================================================
RCS file: /cvs/ethereal/epan/ftypes/ftype-time.c,v
retrieving revision 1.1.1.4
retrieving revision 1.2
diff -u -a -u -r1.1.1.4 -r1.2
--- epan/ftypes/ftype-time.c	4 Jul 2005 18:17:35 -0000	1.1.1.4
+++ epan/ftypes/ftype-time.c	5 Jul 2005 02:59:11 -0000	1.2
@@ -170,9 +170,15 @@
 relative_val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
 {
 	char    *curptr, *endptr;
-
+        gboolean negative = FALSE;
+        
 	curptr = s;
 
+        if(*curptr == '-') {
+            negative = TRUE;
+            curptr++;
+        }
+        
 	/*
 	 * If it doesn't begin with ".", it should contain a seconds
 	 * value.
@@ -212,9 +218,10 @@
 		fv->value.time.nsecs = 0;
 	}
 
-	/*
-	 * XXX - what about negative values?
-	 */
+        if(negative) {
+            fv->value.time.secs = -fv->value.time.secs;
+            fv->value.time.nsecs = -fv->value.time.nsecs;
+        }
 	return TRUE;
 
 fail: