pub fn write_slice(&mut self, bytes: &[u8]) {
        // The basic algorithm is, for every byte:
        //   a = (a + byte) % MOD
        //   b = (b + a) % MOD
        // where MOD = 65521.
        //
        // For efficiency, we can defer the `% MOD` operations as long as neither a nor b overflows:
        // - Between calls to `write`, we ensure that a and b are always in range 0..MOD.
        // - We use 32-bit arithmetic in this function.
        // - Therefore, a and b must not increase by more than 2^32-MOD without performing a `% MOD`
        //   operation.
        //
        // According to Wikipedia, b is calculated as follows for non-incremental checksumming:
        //   b = n×D1 + (n−1)×D2 + (n−2)×D3 + ... + Dn + n*1 (mod 65521)
        // Where n is the number of bytes and Di is the i-th Byte. We need to change this to account
        // for the previous values of a and b, as well as treat every input Byte as being 255:
        //   b_inc = n×255 + (n-1)×255 + ... + 255 + n*65521
        // Or in other words:
        //   b_inc = n*65521 + n(n+1)/2*255
        // The max chunk size is thus the largest value of n so that b_inc <= 2^32-65521.
        //   2^32-65521 = n*65521 + n(n+1)/2*255
        // Plugging this into an equation solver since I can't math gives n = 5552.18..., so 5552.

        const MOD: u32 = 65521;
        const CHUNK_SIZE: usize = 5552;

        let mut a = u32::from(self.a);
        let mut b = u32::from(self.b);
        for chunk in bytes.chunks(CHUNK_SIZE) {
            for byte in chunk {
                let val = u32::from(*byte);
                a += val;
                b += a;
            }

            a %= MOD;
            b %= MOD;
        }
        self.a = a as u16;
        self.b = b as u16;
    }
/// Adds `bytes` to the checksum calculation.
///
/// If efficiency matters, this should be called with Byte slices that contain at least a few
/// thousand Bytes.
Adds `bytes` to the checksum calculation.
If efficiency matters, this should be called with Byte slices that contain at least a few
thousand Bytes.
Adds ` bytes ` to the checksum calculation . If efficiency matters this should be called with Byte slices that contain at least a few thousand Bytes .
pub fn write_slice(&mut self, bytes: &[u8]) {
        // The basic algorithm is, for every byte:
        //   a = (a + byte) % MOD
        //   b = (b + a) % MOD
        // where MOD = 65521.
        //
        // For efficiency, we can defer the `% MOD` operations as long as neither a nor b overflows:
        // - Between calls to `write`, we ensure that a and b are always in range 0..MOD.
        // - We use 32-bit arithmetic in this function.
        // - Therefore, a and b must not increase by more than 2^32-MOD without performing a `% MOD`
        //   operation.
        //
        // According to Wikipedia, b is calculated as follows for non-incremental checksumming:
        //   b = n×D1 + (n−1)×D2 + (n−2)×D3 + ... + Dn + n*1 (mod 65521)
        // Where n is the number of bytes and Di is the i-th Byte. We need to change this to account
        // for the previous values of a and b, as well as treat every input Byte as being 255:
        //   b_inc = n×255 + (n-1)×255 + ... + 255 + n*65521
        // Or in other words:
        //   b_inc = n*65521 + n(n+1)/2*255
        // The max chunk size is thus the largest value of n so that b_inc <= 2^32-65521.
        //   2^32-65521 = n*65521 + n(n+1)/2*255
        // Plugging this into an equation solver since I can't math gives n = 5552.18..., so 5552.
        const MOD: u32 = 65521;
        const CHUNK_SIZE: usize = 5552;
        let mut a = u32::from(self.a);
        let mut b = u32::from(self.b);
        for chunk in bytes.chunks(CHUNK_SIZE) {
            for byte in chunk {
                let val = u32::from(*byte);
                a += val;
                b += a;
            }
            a %= MOD;
            b %= MOD;
        }
        self.a = a as u16;
        self.b = b as u16;
    }
pub fn write_slice ( & mut self , bytes : & [ u8 ] ) { // The basic algorithm is, for every byte: //   a = (a + byte) % MOD //   b = (b + a) % MOD // where MOD = 65521. // // For efficiency, we can defer the `% MOD` operations as long as neither a nor b overflows: // - Between calls to `write`, we ensure that a and b are always in range 0..MOD. // - We use 32-bit arithmetic in this function. // - Therefore, a and b must not increase by more than 2^32-MOD without performing a `% MOD` //   operation. // // According to Wikipedia, b is calculated as follows for non-incremental checksumming: //   b = n×D1 + (n−1)×D2 + (n−2)×D3 + ... + Dn + n*1 (mod 65521) // Where n is the number of bytes and Di is the i-th Byte. We need to change this to account // for the previous values of a and b, as well as treat every input Byte as being 255: //   b_inc = n×255 + (n-1)×255 + ... + 255 + n*65521 // Or in other words: //   b_inc = n*65521 + n(n+1)/2*255 // The max chunk size is thus the largest value of n so that b_inc <= 2^32-65521. //   2^32-65521 = n*65521 + n(n+1)/2*255 // Plugging this into an equation solver since I can't math gives n = 5552.18..., so 5552. const MOD : u32 = 65521 ; const CHUNK_SIZE : usize = 5552 ; let mut a = u32 :: from ( self . a ) ; let mut b = u32 :: from ( self . b ) ; for chunk in bytes . chunks ( CHUNK_SIZE ) { for byte in chunk { let val = u32 :: from ( * byte ) ; a += val ; b += a ; } a %= MOD ; b %= MOD ; } self . a = a as u16 ; self . b = b as u16 ; }
Adds `bytes` to the checksum calculation.
Adds ` bytes ` to the checksum calculation .
&mut self
bytes
&[u8]
&mut self
bytes
&[u8]