HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / in_cksum.c
blobe14c1aca054f880adfc3d6cb3f070dbf954f92db
1 /* in_cksum.c
2 * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3 * pointers/lengths giving the pieces to be checksummed. Also using
4 * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
6 * $Id$
7 */
9 /*
10 * Copyright (c) 1988, 1992, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
40 #include "config.h"
42 #include <glib.h>
44 #include <epan/in_cksum.h>
47 * Checksum routine for Internet Protocol family headers (Portable Version).
49 * This routine is very heavily used in the network
50 * code and should be modified for each CPU to be as fast as possible.
53 #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
54 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
56 int
57 in_cksum(const vec_t *vec, int veclen)
59 register const guint16 *w;
60 register int sum = 0;
61 register int mlen = 0;
62 int byte_swapped = 0;
64 union {
65 guint8 c[2];
66 guint16 s;
67 } s_util;
68 union {
69 guint16 s[2];
70 guint32 l;
71 } l_util;
73 for (; veclen != 0; vec++, veclen--) {
74 if (vec->len == 0)
75 continue;
76 w = (const guint16 *)(const void *)vec->ptr;
77 if (mlen == -1) {
79 * The first byte of this chunk is the continuation
80 * of a word spanning between this chunk and the
81 * last chunk.
83 * s_util.c[0] is already saved when scanning previous
84 * chunk.
86 s_util.c[1] = *(const guint8 *)w;
87 sum += s_util.s;
88 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
89 mlen = vec->len - 1;
90 } else
91 mlen = vec->len;
93 * Force to even boundary.
95 if ((1 & (unsigned long) w) && (mlen > 0)) {
96 REDUCE;
97 sum <<= 8;
98 s_util.c[0] = *(const guint8 *)w;
99 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
100 mlen--;
101 byte_swapped = 1;
104 * Unroll the loop to make overhead from
105 * branches &c small.
107 while ((mlen -= 32) >= 0) {
108 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
109 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
110 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
111 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
112 w += 16;
114 mlen += 32;
115 while ((mlen -= 8) >= 0) {
116 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
117 w += 4;
119 mlen += 8;
120 if (mlen == 0 && byte_swapped == 0)
121 continue;
122 REDUCE;
123 while ((mlen -= 2) >= 0) {
124 sum += *w++;
126 if (byte_swapped) {
127 REDUCE;
128 sum <<= 8;
129 byte_swapped = 0;
130 if (mlen == -1) {
131 s_util.c[1] = *(const guint8 *)w;
132 sum += s_util.s;
133 mlen = 0;
134 } else
135 mlen = -1;
136 } else if (mlen == -1)
137 s_util.c[0] = *(const guint8 *)w;
139 if (mlen == -1) {
140 /* The last mbuf has odd # of bytes. Follow the
141 standard (the odd byte may be shifted left by 8 bits
142 or not as determined by endian-ness of the machine) */
143 s_util.c[1] = 0;
144 sum += s_util.s;
146 REDUCE;
147 return (~sum & 0xffff);
151 * Given the host-byte-order value of the checksum field in a packet
152 * header, and the network-byte-order computed checksum of the data
153 * that the checksum covers (including the checksum itself), compute
154 * what the checksum field *should* have been.
156 guint16
157 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
159 guint32 shouldbe;
162 * The value that should have gone into the checksum field
163 * is the negative of the value gotten by summing up everything
164 * *but* the checksum field.
166 * We can compute that by subtracting the value of the checksum
167 * field from the sum of all the data in the packet, and then
168 * computing the negative of that value.
170 * "sum" is the value of the checksum field, and "computed_sum"
171 * is the negative of the sum of all the data in the packets,
172 * so that's -(-computed_sum - sum), or (sum + computed_sum).
174 * All the arithmetic in question is one's complement, so the
175 * addition must include an end-around carry; we do this by
176 * doing the arithmetic in 32 bits (with no sign-extension),
177 * and then adding the upper 16 bits of the sum, which contain
178 * the carry, to the lower 16 bits of the sum, and then do it
179 * again in case *that* sum produced a carry.
181 * As RFC 1071 notes, the checksum can be computed without
182 * byte-swapping the 16-bit words; summing 16-bit words
183 * on a big-endian machine gives a big-endian checksum, which
184 * can be directly stuffed into the big-endian checksum fields
185 * in protocol headers, and summing words on a little-endian
186 * machine gives a little-endian checksum, which must be
187 * byte-swapped before being stuffed into a big-endian checksum
188 * field.
190 * "computed_sum" is a network-byte-order value, so we must put
191 * it in host byte order before subtracting it from the
192 * host-byte-order value from the header; the adjusted checksum
193 * will be in host byte order, which is what we'll return.
195 shouldbe = sum;
196 shouldbe += g_ntohs(computed_sum);
197 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
198 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
199 return shouldbe;