2 * IS-IS Rout(e)ing protocol - iso_checksum.c
3 * ISO checksum related routines
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "iso_checksum.h"
28 * Calculations of the OSI checksum.
29 * ISO/IEC 8473 defines the sum as
36 * sum (L-i+1)a (mod 255) = 0
42 * Verifies that the checksum is correct.
43 * Return 0 on correct and 1 on invalid checksum.
44 * Based on Annex C.4 of ISO/IEC 8473
48 iso_csum_verify (u_char
* buffer
, int len
, uint16_t * csum
)
62 * If both are zero return correct
64 if (c0
== 0 && c1
== 0)
68 * If either, but not both are zero return incorrect
70 if (c0
== 0 || c1
== 0)
74 * Otherwise initialize to zero and calculate...
81 partial_len
= MIN(len
, 5803);
83 for (i
= 0; i
< partial_len
; i
++)
95 if (c0
== 0 && c1
== 0)
102 * Creates the checksum. *csum points to the position of the checksum in the
104 * Based on Annex C.4 of ISO/IEC 8473
108 iso_csum_create (u_char
* buffer
, int len
, u_int16_t n
)
119 int i
, init_len
, partial_len
;
124 * Zero the csum in the packet.
126 csum
= (u_int16_t
*) (buffer
+ n
);
136 partial_len
= MIN(len
, 5803);
138 for (i
= 0; i
< partial_len
; i
++)
150 mul
= (init_len
- n
)*(c0
);
169 checksum
= (y
<< 8) | (x
& 0xFF);
183 checksum
= ((y
<< 8) | x
);
187 * Now we write this to the packet
191 /* return the checksum for user usage */