checksum.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Routines to support checksumming of bytes.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2004-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. extern int checksum_seed;
  23. extern int protocol_version;
  24. /*
  25. a simple 32 bit checksum that can be upadted from either end
  26. (inspired by Mark Adler's Adler-32 checksum)
  27. */
  28. uint32 get_checksum1(char *buf1, int32 len)
  29. {
  30. int32 i;
  31. uint32 s1, s2;
  32. schar *buf = (schar *)buf1;
  33. s1 = s2 = 0;
  34. for (i = 0; i < (len-4); i+=4) {
  35. s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
  36. 10*CHAR_OFFSET;
  37. s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
  38. }
  39. for (; i < len; i++) {
  40. s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
  41. }
  42. return (s1 & 0xffff) + (s2 << 16);
  43. }
  44. void get_checksum2(char *buf, int32 len, char *sum)
  45. {
  46. md_context m;
  47. if (protocol_version >= 30) {
  48. uchar seedbuf[4];
  49. md5_begin(&m);
  50. md5_update(&m, (uchar *)buf, len);
  51. if (checksum_seed) {
  52. SIVALu(seedbuf, 0, checksum_seed);
  53. md5_update(&m, seedbuf, 4);
  54. }
  55. md5_result(&m, (uchar *)sum);
  56. } else {
  57. int32 i;
  58. static char *buf1;
  59. static int32 len1;
  60. mdfour_begin(&m);
  61. if (len > len1) {
  62. if (buf1)
  63. free(buf1);
  64. buf1 = new_array(char, len+4);
  65. len1 = len;
  66. if (!buf1)
  67. out_of_memory("get_checksum2");
  68. }
  69. memcpy(buf1, buf, len);
  70. if (checksum_seed) {
  71. SIVAL(buf1,len,checksum_seed);
  72. len += 4;
  73. }
  74. for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
  75. mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
  76. /*
  77. * Prior to version 27 an incorrect MD4 checksum was computed
  78. * by failing to call mdfour_tail() for block sizes that
  79. * are multiples of 64. This is fixed by calling mdfour_update()
  80. * even when there are no more bytes.
  81. */
  82. if (len - i > 0 || protocol_version >= 27)
  83. mdfour_update(&m, (uchar *)(buf1+i), len-i);
  84. mdfour_result(&m, (uchar *)sum);
  85. }
  86. }
  87. void file_checksum(char *fname, char *sum, OFF_T size)
  88. {
  89. struct map_struct *buf;
  90. OFF_T i, len = size;
  91. md_context m;
  92. int32 remainder;
  93. int fd;
  94. memset(sum, 0, MAX_DIGEST_LEN);
  95. fd = do_open(fname, O_RDONLY, 0);
  96. if (fd == -1)
  97. return;
  98. buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK);
  99. if (protocol_version >= 30) {
  100. md5_begin(&m);
  101. for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
  102. md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
  103. CSUM_CHUNK);
  104. }
  105. remainder = (int32)(len - i);
  106. if (remainder > 0)
  107. md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
  108. md5_result(&m, (uchar *)sum);
  109. } else {
  110. mdfour_begin(&m);
  111. for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
  112. mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
  113. CSUM_CHUNK);
  114. }
  115. /* Prior to version 27 an incorrect MD4 checksum was computed
  116. * by failing to call mdfour_tail() for block sizes that
  117. * are multiples of 64. This is fixed by calling mdfour_update()
  118. * even when there are no more bytes. */
  119. remainder = (int32)(len - i);
  120. if (remainder > 0 || protocol_version >= 27)
  121. mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
  122. mdfour_result(&m, (uchar *)sum);
  123. }
  124. close(fd);
  125. unmap_file(buf);
  126. }
  127. static int32 sumresidue;
  128. static md_context md;
  129. void sum_init(int seed)
  130. {
  131. char s[4];
  132. if (protocol_version >= 30)
  133. md5_begin(&md);
  134. else {
  135. mdfour_begin(&md);
  136. sumresidue = 0;
  137. SIVAL(s, 0, seed);
  138. sum_update(s, 4);
  139. }
  140. }
  141. /**
  142. * Feed data into an MD4 accumulator, md. The results may be
  143. * retrieved using sum_end(). md is used for different purposes at
  144. * different points during execution.
  145. *
  146. * @todo Perhaps get rid of md and just pass in the address each time.
  147. * Very slightly clearer and slower.
  148. **/
  149. void sum_update(const char *p, int32 len)
  150. {
  151. if (protocol_version >= 30) {
  152. md5_update(&md, (uchar *)p, len);
  153. return;
  154. }
  155. if (len + sumresidue < CSUM_CHUNK) {
  156. memcpy(md.buffer + sumresidue, p, len);
  157. sumresidue += len;
  158. return;
  159. }
  160. if (sumresidue) {
  161. int32 i = CSUM_CHUNK - sumresidue;
  162. memcpy(md.buffer + sumresidue, p, i);
  163. mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
  164. len -= i;
  165. p += i;
  166. }
  167. while (len >= CSUM_CHUNK) {
  168. mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
  169. len -= CSUM_CHUNK;
  170. p += CSUM_CHUNK;
  171. }
  172. sumresidue = len;
  173. if (sumresidue)
  174. memcpy(md.buffer, p, sumresidue);
  175. }
  176. int sum_end(char *sum)
  177. {
  178. if (protocol_version >= 30) {
  179. md5_result(&md, (uchar *)sum);
  180. return MD5_DIGEST_LEN;
  181. }
  182. if (sumresidue || protocol_version >= 27)
  183. mdfour_update(&md, (uchar *)md.buffer, sumresidue);
  184. mdfour_result(&md, (uchar *)sum);
  185. return MD4_DIGEST_LEN;
  186. }