crc32.js 774 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Note: we can't get significant speed boost here.
  2. // So write code to minimize size - no pregenerated tables
  3. // and array tools dependencies.
  4. // Use ordinary array, since untyped makes no boost here
  5. function makeTable() {
  6. var c, table = [];
  7. for (var n = 0; n < 256; n++) {
  8. c = n;
  9. for (var k = 0; k < 8; k++) {
  10. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  11. }
  12. table[n] = c;
  13. }
  14. return table;
  15. }
  16. // Create table on load. Just 255 signed longs. Not a problem.
  17. var crcTable = makeTable();
  18. function crc32(crc, buf, len, pos) {
  19. var t = crcTable,
  20. end = pos + len;
  21. crc ^= -1;
  22. for (var i = pos; i < end; i++) {
  23. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  24. }
  25. return (crc ^ (-1)); // >>> 0;
  26. }
  27. export default crc32;