BSD 4_4 release
[unix-history] / usr / src / sys / netns / ns_cksum.c
index 893c57f..52eba8b 100644 (file)
@@ -1,18 +1,41 @@
 /*
 /*
- * Copyright (c) 1982, 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1982, 1992, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *      @(#)ns_cksum.c 1.3 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)ns_cksum.c  8.1 (Berkeley) 6/10/93
  */
  */
-#include "types.h"
-#include "mbuf.h"
+
+#include <sys/param.h>
+#include <sys/mbuf.h>
+
 /*
  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
  *
 /*
  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
  *
@@ -20,8 +43,8 @@
  * code and should be modified for each CPU to be as fast as possible.
  */
 
  * code and should be modified for each CPU to be as fast as possible.
  */
 
-#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
-#define FOLD(x) {l_util.l = x; x = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
+#define ADDCARRY(x)  { if ((x) > 65535) (x) -= 65535; }
+#define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
 
 u_short
 ns_cksum(m, len)
 
 u_short
 ns_cksum(m, len)
@@ -51,7 +74,11 @@ ns_cksum(m, len)
                         * There is a byte left from the last segment;
                         * ones-complement add it into the checksum.
                         */
                         * There is a byte left from the last segment;
                         * ones-complement add it into the checksum.
                         */
-                       sum  += *(u_char *)w; /* Big-Endian, else << 8 */
+#if BYTE_ORDER == BIG_ENDIAN
+                       sum  += *(u_char *)w;
+#else
+                       sum  += *(u_char *)w << 8;
+#endif
                        sum += sum;
                        w = (u_short *)(1 + (char *)w);
                        mlen = m->m_len - 1;
                        sum += sum;
                        w = (u_short *)(1 + (char *)w);
                        mlen = m->m_len - 1;
@@ -100,9 +127,15 @@ ns_cksum(m, len)
                }
                goto commoncase;
 uuuuglyy:
                }
                goto commoncase;
 uuuuglyy:
-/* Big-Endian; else reverse ww and vv */
+#if BYTE_ORDER == BIG_ENDIAN
 #define ww(n) (((u_char *)w)[n + n + 1])
 #define vv(n) (((u_char *)w)[n + n])
 #define ww(n) (((u_char *)w)[n + n + 1])
 #define vv(n) (((u_char *)w)[n + n])
+#else
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define vv(n) (((u_char *)w)[n + n + 1])
+#define ww(n) (((u_char *)w)[n + n])
+#endif
+#endif
                sum2 = 0;
 #ifndef TINY
                while ((mlen -= 32) >= 0) {
                sum2 = 0;
 #ifndef TINY
                while ((mlen -= 32) >= 0) {
@@ -148,7 +181,11 @@ uuuuglyy:
                sum += (sum2 << 8);
 commoncase:
                if (mlen == -1) {
                sum += (sum2 << 8);
 commoncase:
                if (mlen == -1) {
-                       sum += *(u_char *)w << 8; /* Big-Endian, else no << 8 */
+#if BYTE_ORDER == BIG_ENDIAN
+                       sum += *(u_char *)w << 8;
+#else
+                       sum += *(u_char *)w;
+#endif
                }
                FOLD(sum);
        }
                }
                FOLD(sum);
        }