+ * set (initialize) the size entries in the per-file sum_struct
+ * calculating dynamic block and checksum sizes.
+ *
+ * This is only called from generate_and_send_sums() but is a separate
+ * function to encapsulate the logic.
+ *
+ * The block size is a rounded square root of file length.
+ *
+ * The checksum size is determined according to:
+ * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
+ * provided by Donovan Baarda which gives a probability of rsync
+ * algorithm corrupting data and falling back using the whole md4
+ * checksums.
+ *
+ * This might be made one of several selectable heuristics.
+ */
+static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
+{
+ int32 blength;
+ int s2length;
+
+ if (block_size)
+ blength = block_size;
+ else if (len <= BLOCK_SIZE * BLOCK_SIZE)
+ blength = BLOCK_SIZE;
+ else {
+ int32 c;
+ int64 l;
+ int cnt;
+ for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
+ if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
+ blength = MAX_BLOCK_SIZE;
+ else {
+ blength = 0;
+ do {
+ blength |= c;
+ if (len < (int64)blength * blength)
+ blength &= ~c;
+ c >>= 1;
+ } while (c >= 8); /* round to multiple of 8 */
+ blength = MAX(blength, BLOCK_SIZE);
+ }
+ }
+
+ if (protocol_version < 27) {
+ s2length = csum_length;
+ } else if (csum_length == SUM_LENGTH) {
+ s2length = SUM_LENGTH;
+ } else {
+ int32 c;
+ int64 l;
+ int b = BLOCKSUM_BIAS;
+ for (l = len; l >>= 1; b += 2) {}
+ for (c = blength; c >>= 1 && b; b--) {}
+ /* add a bit, subtract rollsum, round up. */
+ s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
+ s2length = MAX(s2length, csum_length);
+ s2length = MIN(s2length, SUM_LENGTH);
+ }
+
+ sum->flength = len;
+ sum->blength = blength;
+ sum->s2length = s2length;
+ sum->remainder = len % blength;
+ sum->count = len / blength + (sum->remainder != 0);
+
+ if (sum->count && verbose > 2) {
+ rprintf(FINFO,
+ "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
+ (double)sum->count, (long)sum->remainder, (long)sum->blength,
+ sum->s2length, (double)sum->flength);
+ }
+}