summaryrefslogtreecommitdiff
path: root/rsync/checksum.c
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /rsync/checksum.c
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'rsync/checksum.c') (more/less context) (ignore whitespace changes)
-rw-r--r--rsync/checksum.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/rsync/checksum.c b/rsync/checksum.c
new file mode 100644
index 0000000..eab9e46
--- a/dev/null
+++ b/rsync/checksum.c
@@ -0,0 +1,81 @@
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- the library for network deltas
4 * $Id$
5 *
6 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@samba.org>
7 * Copyright (C) 1996 by Andrew Tridgell
8 * Copyright (C) 1996 by Paul Mackerras
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <config_rsync.h>
26
27#include <assert.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "rsync.h"
32#include "checksum.h"
33
34
35/* This can possibly be used to restart the checksum system in the
36 * case where we detected corruption. I'm not sure yet how to make
37 * this useful in librsync. */
38int checksum_seed = 0;
39
40/*
41 * A simple 32 bit checksum that can be updated from either end
42 * (inspired by Mark Adler's Adler-32 checksum)
43 */
44unsigned int rs_calc_weak_sum(void const *p, int len)
45{
46 int i;
47 unsigned s1, s2;
48 unsigned char const *buf = (unsigned char const *) p;
49
50 s1 = s2 = 0;
51 for (i = 0; i < (len - 4); i += 4) {
52 s2 += 4 * (s1 + buf[i]) + 3 * buf[i + 1] +
53 2 * buf[i + 2] + buf[i + 3] + 10 * RS_CHAR_OFFSET;
54 s1 += (buf[i + 0] + buf[i + 1] + buf[i + 2] + buf[i + 3] +
55 4 * RS_CHAR_OFFSET);
56 }
57 for (; i < len; i++) {
58 s1 += (buf[i] + RS_CHAR_OFFSET);
59 s2 += s1;
60 }
61 return (s1 & 0xffff) + (s2 << 16);
62}
63
64
65/**
66 * Calculate and store into SUM a strong MD4 checksum of the file
67 * blocks seen so far.
68 *
69 * In plain rsync, the checksum is perturbed by a seed value. This is
70 * used when retrying a failed transmission: we've discovered that the
71 * hashes collided at some point, so we're going to try again with
72 * different hashes to see if we can get it right. (Check tridge's
73 * thesis for details and to see if that's correct.)
74 *
75 * Since we can't retry a web transaction I'm not sure if it's very
76 * useful in rproxy.
77 */
78void rs_calc_strong_sum(void const *buf, size_t len, rs_strong_sum_t *sum)
79{
80 rs_mdfour((unsigned char *) sum, buf, len);
81}