shithub: jbig2

Download patch

ref: 7fa4382f2f5cac1f1471617a23c9d1affa994e18
parent: 65885f2808d815a4ce41d4712c19dc1dac61b6ab
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon May 2 16:49:28 EDT 2005

Alter our portability fallback implementation of memcmp() to properly return
negative values is the first argument is "less than" the second. The previous
one was fine for our purposes, but could cause problems if it was accidentally
used by client applications.

Thanks to Ray Johnston for pointing this out.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@401 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/memcmp.c
+++ b/memcmp.c
@@ -1,7 +1,7 @@
 /*
     jbig2dec
     
-    Copyright (C) 2001-2002 artofcode LLC.
+    Copyright (C) 2001-2005 artofcode LLC.
     
     This software is distributed under license and may not
     be copied, modified or distributed except as expressly
@@ -13,7 +13,7 @@
     Artifex Software, Inc.,  101 Lucas Valley Road #110,
     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
 
-    $Id: memcmp.c,v 1.1 2002/08/05 17:10:44 giles Exp $
+    $Id$
 */
 
 #ifdef HAVE_CONFIG_H
@@ -26,24 +26,27 @@
 
 /*
  * compares two byte strings 'a' and 'b', both assumed to be 'len' bytes long
- * returns zero if the two strings are identical, otherwise returns the difference
- * between the values of the first two differing bytes, considered as unsigned chars
+ * returns zero if the two strings are identical, otherwise returns -1 or 1
+ * depending on the relative magnitude of the first differing elements,
+ * considered as unsigned chars
  */
 
 int memcmp(const void *b1, const void *b2, size_t len)
 {
 	unsigned char *a, *b;
-	unsigned char c;
 	size_t i;
 
 	a = (unsigned char *)b1;
 	b = (unsigned char *)b2;
 	for(i = 0; i < len; i++) {
-		c = *a - *b;
-		if (c) return (int)c; /* strings differ */
+		if (*a != *b) {
+			 /* strings differ */
+			return (*a < *b) ? -1 : 1;
+		}
 		a++;
 		b++;
 	}
 
-	return 0; /* strings match */
+	/* strings match */
+	return 0;
 }