* 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
| 695 | * This might be made one of several selectable heuristics. |
| 696 | */ |
| 697 | static void sum_sizes_sqroot(struct sum_struct *sum, int64 len) |
| 698 | { |
| 699 | int32 blength; |
| 700 | int s2length; |
| 701 | /* The strong sum can be no longer than the negotiated checksum digest: |
| 702 | * a short checksum (e.g. xxh64 = 8 bytes, when xxh128/xxh3 are absent) |
| 703 | * makes xfer_sum_len < SUM_LENGTH, and the sender rejects an s2length |
| 704 | * larger than xfer_sum_len (io.c). */ |
| 705 | int max_s2length = MIN(SUM_LENGTH, xfer_sum_len); |
| 706 | int64 l; |
| 707 | |
| 708 | if (len < 0) { |
| 709 | /* The file length overflowed our int64 var, so we can't process this file. */ |
| 710 | sum->count = -1; /* indicate overflow error */ |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | if (block_size) |
| 715 | blength = block_size; |
| 716 | else if (len <= BLOCK_SIZE * BLOCK_SIZE) |
| 717 | blength = BLOCK_SIZE; |
| 718 | else { |
| 719 | int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE; |
| 720 | int32 c; |
| 721 | int cnt; |
| 722 | for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {} |
| 723 | if (c < 0 || c >= max_blength) |
| 724 | blength = max_blength; |
| 725 | else { |
| 726 | blength = 0; |
| 727 | do { |
| 728 | blength |= c; |
| 729 | if (len < (int64)blength * blength) |
| 730 | blength &= ~c; |
| 731 | c >>= 1; |
| 732 | } while (c >= 8); /* round to multiple of 8 */ |
| 733 | blength = MAX(blength, BLOCK_SIZE); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (protocol_version < 27) { |
| 738 | s2length = csum_length; |
| 739 | } else if (csum_length == SUM_LENGTH) { |
| 740 | s2length = max_s2length; |
| 741 | } else { |
| 742 | int32 c; |
| 743 | int b = BLOCKSUM_BIAS; |
| 744 | for (l = len; l >>= 1; b += 2) {} |
| 745 | for (c = blength; (c >>= 1) && b; b--) {} |
| 746 | /* add a bit, subtract rollsum, round up. */ |
| 747 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ |
| 748 | s2length = MAX(s2length, csum_length); |
| 749 | s2length = MIN(s2length, max_s2length); |
| 750 | } |
| 751 | |
| 752 | sum->flength = len; |
| 753 | sum->blength = blength; |
| 754 | sum->s2length = s2length; |
no test coverage detected