getCrc32 function

int getCrc32(
  1. List<int> array, [
  2. int crc = 0
])

Get the CRC-32 checksum of the given array. You can append bytes to an already computed crc by specifying the previous crc value.

Implementation

int getCrc32(List<int> array, [int crc = 0]) {
  var len = array.length;
  crc = crc ^ 0xffffffff;
  var ip = 0;
  if (array is Uint8List && len >= 16) {
    // Slicing by eight: two 32-bit words at a time, each byte of them looked
    // up in its own table, which is about three times the speed of a byte at
    // a time. Only for typed data, where the words can be read whole.
    final t = _crc32Tables;
    final data = ByteData.sublistView(array);
    while (len >= 8) {
      final one = data.getUint32(ip, Endian.little) ^ crc;
      final two = data.getUint32(ip + 4, Endian.little);
      crc = t[0x700 + (one & 0xff)] ^
          t[0x600 + ((one >>> 8) & 0xff)] ^
          t[0x500 + ((one >>> 16) & 0xff)] ^
          t[0x400 + (one >>> 24)] ^
          t[0x300 + (two & 0xff)] ^
          t[0x200 + ((two >>> 8) & 0xff)] ^
          t[0x100 + ((two >>> 16) & 0xff)] ^
          t[two >>> 24];
      ip += 8;
      len -= 8;
    }
  }
  while (len >= 8) {
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    len -= 8;
  }
  if (len > 0) {
    do {
      crc = _crc32Table[(crc ^ array[ip++]) & 0xff] ^ (crc >> 8);
    } while (--len > 0);
  }
  return crc ^ 0xffffffff;
}