2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 int csum_length
=2; /* initial value */
26 int checksum_seed
= 0;
27 extern int remote_version
;
30 a simple 32 bit checksum that can be upadted from either end
31 (inspired by Mark Adler's Adler-32 checksum)
33 uint32
get_checksum1(char *buf1
,int len
)
37 schar
*buf
= (schar
*)buf1
;
40 for (i
= 0; i
< (len
-4); i
+=4) {
41 s2
+= 4*(s1
+ buf
[i
]) + 3*buf
[i
+1] + 2*buf
[i
+2] + buf
[i
+3] +
43 s1
+= (buf
[i
+0] + buf
[i
+1] + buf
[i
+2] + buf
[i
+3] + 4*CHAR_OFFSET
);
45 for (; i
< len
; i
++) {
46 s1
+= (buf
[i
]+CHAR_OFFSET
); s2
+= s1
;
48 return (s1
& 0xffff) + (s2
<< 16);
52 void get_checksum2(char *buf
,int len
,char *sum
)
61 buf1
= (char *)malloc(len
+4);
63 if (!buf1
) out_of_memory("get_checksum2");
70 SIVAL(buf1
,len
,checksum_seed
);
74 for(i
= 0; i
+ CSUM_CHUNK
<= len
; i
+= CSUM_CHUNK
) {
75 mdfour_update(&m
, (uchar
*)(buf1
+i
), CSUM_CHUNK
);
78 mdfour_update(&m
, (uchar
*)(buf1
+i
), (len
-i
));
81 mdfour_result(&m
, (uchar
*)sum
);
85 void file_checksum(char *fname
,char *sum
,OFF_T size
)
88 struct map_struct
*buf
;
91 char tmpchunk
[CSUM_CHUNK
];
94 memset(sum
,0,MD4_SUM_LENGTH
);
96 fd
= do_open(fname
, O_RDONLY
, 0);
99 buf
= map_file(fd
,size
);
103 for(i
= 0; i
+ CSUM_CHUNK
<= len
; i
+= CSUM_CHUNK
) {
104 memcpy(tmpchunk
, map_ptr(buf
,i
,CSUM_CHUNK
), CSUM_CHUNK
);
105 mdfour_update(&m
, (uchar
*)tmpchunk
, CSUM_CHUNK
);
109 memcpy(tmpchunk
, map_ptr(buf
,i
,len
-i
), len
-i
);
110 mdfour_update(&m
, (uchar
*)tmpchunk
, (len
-i
));
113 mdfour_result(&m
, (uchar
*)sum
);
120 void checksum_init(void)
122 if (remote_version
>= 14)
123 csum_length
= 2; /* adaptive */
125 csum_length
= SUM_LENGTH
;
130 static int sumresidue
;
131 static char sumrbuf
[CSUM_CHUNK
];
132 static struct mdfour md
;
139 SIVAL(s
,0,checksum_seed
);
144 * Feed data into an MD4 accumulator, md. The results may be
145 * retrieved using sum_end(). md is used for different purposes at
146 * different points during execution.
148 * @todo Perhaps get rid of md and just pass in the address each time.
149 * Very slightly clearer and slower.
151 void sum_update(char *p
, int len
)
154 if (len
+ sumresidue
< CSUM_CHUNK
) {
155 memcpy(sumrbuf
+sumresidue
, p
, len
);
161 i
= MIN(CSUM_CHUNK
-sumresidue
,len
);
162 memcpy(sumrbuf
+sumresidue
,p
,i
);
163 mdfour_update(&md
, (uchar
*)sumrbuf
, (i
+sumresidue
));
168 for(i
= 0; i
+ CSUM_CHUNK
<= len
; i
+= CSUM_CHUNK
) {
169 memcpy(sumrbuf
,p
+i
,CSUM_CHUNK
);
170 mdfour_update(&md
, (uchar
*)sumrbuf
, CSUM_CHUNK
);
175 memcpy(sumrbuf
,p
+i
,sumresidue
);
181 void sum_end(char *sum
)
184 mdfour_update(&md
, (uchar
*)sumrbuf
, sumresidue
);
187 mdfour_result(&md
, (uchar
*)sum
);