epan/dissectors/packet-xml.c try to decrypt data, but the data doesn't look correct yet
[metze/wireshark/wip.git] / epan / in_cksum.c
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.
5  *
6  * Copyright (c) 1988, 1992, 1993
7  *      The Regents of the University of California.  All rights reserved.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  *
11  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
12  */
13
14 #include "config.h"
15
16 #include <glib.h>
17
18 #include <epan/tvbuff.h>
19 #include <epan/in_cksum.h>
20
21 /*
22  * Checksum routine for Internet Protocol family headers (Portable Version).
23  *
24  * This routine is very heavily used in the network
25  * code and should be modified for each CPU to be as fast as possible.
26  */
27
28 #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
29 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
30
31 int
32 in_cksum(const vec_t *vec, int veclen)
33 {
34         register const guint16 *w;
35         register int sum = 0;
36         register int mlen = 0;
37         int byte_swapped = 0;
38
39         union {
40                 guint8  c[2];
41                 guint16 s;
42         } s_util;
43         union {
44                 guint16 s[2];
45                 guint32 l;
46         } l_util;
47
48         for (; veclen != 0; vec++, veclen--) {
49                 if (vec->len == 0)
50                         continue;
51                 w = (const guint16 *)(const void *)vec->ptr;
52                 if (mlen == -1) {
53                         /*
54                          * The first byte of this chunk is the continuation
55                          * of a word spanning between this chunk and the
56                          * last chunk.
57                          *
58                          * s_util.c[0] is already saved when scanning previous
59                          * chunk.
60                          */
61                         s_util.c[1] = *(const guint8 *)w;
62                         sum += s_util.s;
63                         w = (const guint16 *)(const void *)((const guint8 *)w + 1);
64                         mlen = vec->len - 1;
65                 } else
66                         mlen = vec->len;
67                 /*
68                  * Force to even boundary.
69                  */
70                 if ((1 & (gintptr)w) && (mlen > 0)) {
71                         REDUCE;
72                         sum <<= 8;
73                         s_util.c[0] = *(const guint8 *)w;
74                         w = (const guint16 *)(const void *)((const guint8 *)w + 1);
75                         mlen--;
76                         byte_swapped = 1;
77                 }
78                 /*
79                  * Unroll the loop to make overhead from
80                  * branches &c small.
81                  */
82                 while ((mlen -= 32) >= 0) {
83                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
84                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
85                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
86                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
87                         w += 16;
88                 }
89                 mlen += 32;
90                 while ((mlen -= 8) >= 0) {
91                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
92                         w += 4;
93                 }
94                 mlen += 8;
95                 if (mlen == 0 && byte_swapped == 0)
96                         continue;
97                 REDUCE;
98                 while ((mlen -= 2) >= 0) {
99                         sum += *w++;
100                 }
101                 if (byte_swapped) {
102                         REDUCE;
103                         sum <<= 8;
104                         byte_swapped = 0;
105                         if (mlen == -1) {
106                                 s_util.c[1] = *(const guint8 *)w;
107                                 sum += s_util.s;
108                                 mlen = 0;
109                         } else
110                                 mlen = -1;
111                 } else if (mlen == -1)
112                         s_util.c[0] = *(const guint8 *)w;
113         }
114         if (mlen == -1) {
115                 /* The last mbuf has odd # of bytes. Follow the
116                    standard (the odd byte may be shifted left by 8 bits
117                    or not as determined by endian-ness of the machine) */
118                 s_util.c[1] = 0;
119                 sum += s_util.s;
120         }
121         REDUCE;
122         return (~sum & 0xffff);
123 }
124
125 guint16
126 ip_checksum(const guint8 *ptr, int len)
127 {
128         vec_t cksum_vec[1];
129
130         SET_CKSUM_VEC_PTR(cksum_vec[0], ptr, len);
131         return in_cksum(&cksum_vec[0], 1);
132 }
133
134 guint16
135 ip_checksum_tvb(tvbuff_t *tvb, int offset, int len)
136 {
137         vec_t cksum_vec[1];
138
139         SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, offset, len);
140         return in_cksum(&cksum_vec[0], 1);
141 }
142
143 /*
144  * Given the host-byte-order value of the checksum field in a packet
145  * header, and the network-byte-order computed checksum of the data
146  * that the checksum covers (including the checksum itself), compute
147  * what the checksum field *should* have been.
148  */
149 guint16
150 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
151 {
152         guint32 shouldbe;
153
154         /*
155          * The value that should have gone into the checksum field
156          * is the negative of the value gotten by summing up everything
157          * *but* the checksum field.
158          *
159          * We can compute that by subtracting the value of the checksum
160          * field from the sum of all the data in the packet, and then
161          * computing the negative of that value.
162          *
163          * "sum" is the value of the checksum field, and "computed_sum"
164          * is the negative of the sum of all the data in the packets,
165          * so that's -(-computed_sum - sum), or (sum + computed_sum).
166          *
167          * All the arithmetic in question is one's complement, so the
168          * addition must include an end-around carry; we do this by
169          * doing the arithmetic in 32 bits (with no sign-extension),
170          * and then adding the upper 16 bits of the sum, which contain
171          * the carry, to the lower 16 bits of the sum, and then do it
172          * again in case *that* sum produced a carry.
173          *
174          * As RFC 1071 notes, the checksum can be computed without
175          * byte-swapping the 16-bit words; summing 16-bit words
176          * on a big-endian machine gives a big-endian checksum, which
177          * can be directly stuffed into the big-endian checksum fields
178          * in protocol headers, and summing words on a little-endian
179          * machine gives a little-endian checksum, which must be
180          * byte-swapped before being stuffed into a big-endian checksum
181          * field.
182          *
183          * "computed_sum" is a network-byte-order value, so we must put
184          * it in host byte order before subtracting it from the
185          * host-byte-order value from the header; the adjusted checksum
186          * will be in host byte order, which is what we'll return.
187          */
188         shouldbe = sum;
189         shouldbe += g_ntohs(computed_sum);
190         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
191         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
192         return shouldbe;
193 }
194
195 /*
196  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
197  *
198  * Local variables:
199  * c-basic-offset: 8
200  * tab-width: 8
201  * indent-tabs-mode: t
202  * End:
203  *
204  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
205  * :indentSize=8:tabSize=8:noTabs=false:
206  */