]> The Tcpdump Group git mirrors - tcpdump/blob - checksum.c
move the crc10 verification to a new file checksum.c (will add other checksumming...
[tcpdump] / checksum.c
1 /*
2 * Copyright (c) 1998-2006 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * miscellaneous checksumming routines
16 *
17 * Original code by Hannes Gredler (hannes@juniper.net)
18 */
19
20 #ifndef lint
21 static const char rcsid[] _U_ =
22 "@(#) $Header: /tcpdump/master/tcpdump/checksum.c,v 1.1 2006-02-09 20:33:49 hannes Exp $";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <tcpdump-stdinc.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "interface.h"
36 #include "extract.h"
37 #include "addrtoname.h"
38
39 #define CRC10_POLYNOMIAL 0x633
40 static u_int16_t crc10_table[256];
41
42 static void
43 gen_crc10_table(void)
44 {
45 register int i, j;
46 register u_int16_t accum;
47
48 for ( i = 0; i < 256; i++ )
49 {
50 accum = ((unsigned short) i << 2);
51 for ( j = 0; j < 8; j++ )
52 {
53 if ((accum <<= 1) & 0x400) accum ^= CRC10_POLYNOMIAL;
54 }
55 crc10_table[i] = accum;
56 }
57 return;
58 }
59
60 u_int16_t
61 verify_crc10_cksum(u_int16_t accum, const u_char *p, int length)
62 {
63 register int i;
64
65 for ( i = 0; i < length; i++ )
66 {
67 accum = ((accum << 8) & 0x3ff)
68 ^ crc10_table[( accum >> 2) & 0xff]
69 ^ *p++;
70 }
71 return accum;
72 }
73
74 /* precomputed checksum tables */
75 void
76 init_checksum(void) {
77
78 gen_crc10_table();
79
80 }