crc32.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. // Note: we can't get significant speed boost here.
  3. // So write code to minimize size - no pregenerated tables
  4. // and array tools dependencies.
  5. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  6. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  7. //
  8. // This software is provided 'as-is', without any express or implied
  9. // warranty. In no event will the authors be held liable for any damages
  10. // arising from the use of this software.
  11. //
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. //
  16. // 1. The origin of this software must not be misrepresented; you must not
  17. // claim that you wrote the original software. If you use this software
  18. // in a product, an acknowledgment in the product documentation would be
  19. // appreciated but is not required.
  20. // 2. Altered source versions must be plainly marked as such, and must not be
  21. // misrepresented as being the original software.
  22. // 3. This notice may not be removed or altered from any source distribution.
  23. // Use ordinary array, since untyped makes no boost here
  24. function makeTable() {
  25. var c, table = [];
  26. for (var n = 0; n < 256; n++) {
  27. c = n;
  28. for (var k = 0; k < 8; k++) {
  29. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  30. }
  31. table[n] = c;
  32. }
  33. return table;
  34. }
  35. // Create table on load. Just 255 signed longs. Not a problem.
  36. var crcTable = makeTable();
  37. function crc32(crc, buf, len, pos) {
  38. var t = crcTable,
  39. end = pos + len;
  40. crc ^= -1;
  41. for (var i = pos; i < end; i++) {
  42. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  43. }
  44. return (crc ^ (-1)); // >>> 0;
  45. }
  46. module.exports = crc32;