lib/checksum.c: fix endianess bug
authorArnd Bergmann <arnd@arndb.de>
Fri, 19 Jun 2009 08:41:19 +0000 (10:41 +0200)
committerArnd Bergmann <arnd@arndb.de>
Fri, 19 Jun 2009 12:58:13 +0000 (14:58 +0200)
The new generic checksum code has a small dependency on endianess and
worked only on big-endian systems. I could not find a nice efficient
way to express this, so I added an #ifdef. Using
'result += le16_to_cpu(*buff);' would have worked as well, but
would be slightly less efficient on big-endian systems and IMHO
would not be clearer.

Also fix a bug that prevents this from working on 64-bit machines.
If you have a 64-bit CPU and want to use the generic checksum
code, you should probably do some more optimizations anyway, but
at least the code should not break.

Reported-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
lib/checksum.c

index 12e5a1c91cdab8134a88c921236f1559aca5a99b..b2e2fd4684618341adaff20dea8d04edfd99ec90 100644 (file)
@@ -55,7 +55,11 @@ static unsigned int do_csum(const unsigned char *buff, int len)
                goto out;
        odd = 1 & (unsigned long) buff;
        if (odd) {
+#ifdef __LITTLE_ENDIAN
                result = *buff;
+#else
+               result += (*buff << 8);
+#endif
                len--;
                buff++;
        }
@@ -71,7 +75,7 @@ static unsigned int do_csum(const unsigned char *buff, int len)
                if (count) {
                        unsigned long carry = 0;
                        do {
-                               unsigned long w = *(unsigned long *) buff;
+                               unsigned long w = *(unsigned int *) buff;
                                count--;
                                buff += 4;
                                result += carry;
@@ -87,7 +91,11 @@ static unsigned int do_csum(const unsigned char *buff, int len)
                }
        }
        if (len & 1)
+#ifdef __LITTLE_ENDIAN
+               result += *buff;
+#else
                result += (*buff << 8);
+#endif
        result = from32to16(result);
        if (odd)
                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);