Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

checksum.c File Reference

Go to the source code of this file.

Functions

uint32 get_checksum1 (char *buf1, int len)
void get_checksum2 (char *buf, int len, char *sum)
void file_checksum (char *fname, char *sum, OFF_T size)
void checksum_init (void)
void sum_init (void)
void sum_update (char *p, int len)
 Feed data into an MD4 accumulator, md. More...

void sum_end (char *sum)

Variables

int csum_length = 2
int checksum_seed = 0
int remote_version
int sumresidue
char sumrbuf [CSUM_CHUNK]
mdfour md


Function Documentation

uint32 get_checksum1 char *    buf1,
int    len
 

Definition at line 33 of file checksum.c.

Referenced by generate_sums(), and hash_search().

00034 {
00035     int i;
00036     uint32 s1, s2;
00037     schar *buf = (schar *)buf1;
00038 
00039     s1 = s2 = 0;
00040     for (i = 0; i < (len-4); i+=4) {
00041         s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 
00042           10*CHAR_OFFSET;
00043         s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET); 
00044     }
00045     for (; i < len; i++) {
00046         s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
00047     }
00048     return (s1 & 0xffff) + (s2 << 16);
00049 }

void get_checksum2 char *    buf,
int    len,
char *    sum
 

Definition at line 52 of file checksum.c.

References checksum_seed, mdfour_begin(), mdfour_result(), mdfour_update(), and out_of_memory().

Referenced by generate_sums(), and hash_search().

00053 {
00054         int i;
00055         static char *buf1;
00056         static int len1;
00057         struct mdfour m;
00058 
00059         if (len > len1) {
00060                 if (buf1) free(buf1);
00061                 buf1 = (char *)malloc(len+4);
00062                 len1 = len;
00063                 if (!buf1) out_of_memory("get_checksum2");
00064         }
00065         
00066         mdfour_begin(&m);
00067         
00068         memcpy(buf1,buf,len);
00069         if (checksum_seed) {
00070                 SIVAL(buf1,len,checksum_seed);
00071                 len += 4;
00072         }
00073         
00074         for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
00075                 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
00076         }
00077         if (len - i > 0) {
00078                 mdfour_update(&m, (uchar *)(buf1+i), (len-i));
00079         }
00080         
00081         mdfour_result(&m, (uchar *)sum);
00082 }

void file_checksum char *    fname,
char *    sum,
OFF_T    size
 

Definition at line 85 of file checksum.c.

References do_open(), map_file(), map_ptr(), mdfour_begin(), mdfour_result(), mdfour_update(), and unmap_file().

Referenced by make_file(), and skip_file().

00086 {
00087         OFF_T i;
00088         struct map_struct *buf;
00089         int fd;
00090         OFF_T len = size;
00091         char tmpchunk[CSUM_CHUNK];
00092         struct mdfour m;
00093         
00094         memset(sum,0,MD4_SUM_LENGTH);
00095         
00096         fd = do_open(fname, O_RDONLY, 0);
00097         if (fd == -1) return;
00098         
00099         buf = map_file(fd,size);
00100         
00101         mdfour_begin(&m);
00102 
00103         for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
00104                 memcpy(tmpchunk, map_ptr(buf,i,CSUM_CHUNK), CSUM_CHUNK);
00105                 mdfour_update(&m, (uchar *)tmpchunk, CSUM_CHUNK);
00106         }
00107 
00108         if (len - i > 0) {
00109                 memcpy(tmpchunk, map_ptr(buf,i,len-i), len-i);
00110                 mdfour_update(&m, (uchar *)tmpchunk, (len-i));
00111         }
00112 
00113         mdfour_result(&m, (uchar *)sum);
00114 
00115         close(fd);
00116         unmap_file(buf);
00117 }

void checksum_init void   
 

Definition at line 120 of file checksum.c.

References csum_length, and remote_version.

Referenced by setup_protocol().

00121 {
00122   if (remote_version >= 14)
00123     csum_length = 2; /* adaptive */
00124   else
00125     csum_length = SUM_LENGTH;
00126 }

void sum_init void   
 

Definition at line 134 of file checksum.c.

References checksum_seed, mdfour_begin(), sum_update(), and sumresidue.

Referenced by gen_challenge(), generate_hash(), match_sums(), and receive_data().

00135 {
00136         char s[4];
00137         mdfour_begin(&md);
00138         sumresidue=0;
00139         SIVAL(s,0,checksum_seed);
00140         sum_update(s,4);
00141 }

void sum_update char *    p,
int    len
 

Feed data into an MD4 accumulator, md.

The results may be retrieved using sum_end(). md is used for different purposes at different points during execution.

Todo:
Perhaps get rid of md and just pass in the address each time. Very slightly clearer and slower.

Definition at line 151 of file checksum.c.

References mdfour_update(), sumrbuf, and sumresidue.

Referenced by gen_challenge(), generate_hash(), matched(), receive_data(), and sum_init().

00152 {
00153         int i;
00154         if (len + sumresidue < CSUM_CHUNK) {
00155                 memcpy(sumrbuf+sumresidue, p, len);
00156                 sumresidue += len;
00157                 return;
00158         }
00159 
00160         if (sumresidue) {
00161                 i = MIN(CSUM_CHUNK-sumresidue,len);
00162                 memcpy(sumrbuf+sumresidue,p,i);
00163                 mdfour_update(&md, (uchar *)sumrbuf, (i+sumresidue));
00164                 len -= i;
00165                 p += i;
00166         }
00167 
00168         for(i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
00169                 memcpy(sumrbuf,p+i,CSUM_CHUNK);
00170                 mdfour_update(&md, (uchar *)sumrbuf, CSUM_CHUNK);
00171         }
00172 
00173         if (len - i > 0) {
00174                 sumresidue = len-i;
00175                 memcpy(sumrbuf,p+i,sumresidue);
00176         } else {
00177                 sumresidue = 0;    
00178         }
00179 }

void sum_end char *    sum
 

Definition at line 181 of file checksum.c.

References mdfour_result(), mdfour_update(), sumrbuf, and sumresidue.

Referenced by gen_challenge(), generate_hash(), match_sums(), and receive_data().

00182 {
00183         if (sumresidue) {
00184                 mdfour_update(&md, (uchar *)sumrbuf, sumresidue);
00185         }
00186 
00187         mdfour_result(&md, (uchar *)sum);
00188 }


Variable Documentation

int csum_length = 2
 

Definition at line 22 of file checksum.c.

Referenced by checksum_init(), receive_sums(), and send_files().

int checksum_seed = 0
 

Definition at line 26 of file checksum.c.

Referenced by get_checksum2(), setup_protocol(), and sum_init().

int remote_version
 

Definition at line 27 of file checksum.c.

Referenced by checksum_init().

int sumresidue [static]
 

Definition at line 130 of file checksum.c.

Referenced by sum_end(), sum_init(), and sum_update().

char sumrbuf[CSUM_CHUNK] [static]
 

Definition at line 131 of file checksum.c.

Referenced by sum_end(), and sum_update().

struct mdfour md [static]
 

Definition at line 132 of file checksum.c.


Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15