1 /* Copyright 2001 Rien Croonenborghs, Ben Kibbey, Shaun Jackman, Ivan Brozovic
2
3 This file is part of lcab.
4 lcab is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 lcab is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with lcab; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #include "checksum.h"
18
19 // compute the checksum for the datablock
compute_checksum(byte * in,word ncbytes,CHECKSUM seed)20 CHECKSUM compute_checksum(byte *in, word ncbytes, CHECKSUM seed)
21 {
22 int no_ulongs;
23 CHECKSUM csum=0;
24 byte *stroom;
25 CHECKSUM temp;
26
27 no_ulongs = ncbytes / 4;
28 csum = seed;
29 stroom = in;
30
31 while(no_ulongs-->0)
32 {
33 temp = ((CHECKSUM) (*stroom++));
34 temp |= (((CHECKSUM) (*stroom++)) << 8);
35 temp |= (((CHECKSUM) (*stroom++)) << 16);
36 temp |= (((CHECKSUM) (*stroom++)) << 24);
37
38 csum ^= temp;
39 }
40
41 temp = 0;
42 switch(ncbytes%4)
43 {
44 case 3: temp |= (((CHECKSUM) (*stroom++)) << 16);
45 case 2: temp |= (((CHECKSUM) (*stroom++)) << 8);
46 case 1: temp |= ((CHECKSUM) (*stroom++));
47 default: break;
48 }
49
50 csum ^= temp;
51
52 return csum;
53 }
54