UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2
3/**
4* from crc32.js (C) 2014-2015 SheetJS -- http://sheetjs.com
5*/
6
7var use_in32 = typeof Int32Array !== 'undefined';
8
9/* istanbul ignore next */
10var table = (function() {
11 var c = 0;
12 var table = new Array(256);
13
14 for(var n = 0; n != 256; ++n) {
15 c = n;
16 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
17 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
18 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
19 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
20 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
21 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
22 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
23 c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
24 table[n] = c;
25 }
26 return use_in32 ? new Int32Array(table) : table;
27}());
28
29/* much much faster to intertwine utf8 and crc */
30/* istanbul ignore next */
31function crc32_str(str) {
32 for(var crc = -1, i = 0, L = str.length, c, d; i < L;) {
33 c = str.charCodeAt(i++);
34 if(c < 0x80) {
35 crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
36 } else if(c < 0x800) {
37 crc = (crc >>> 8) ^ table[(crc ^ (192 | ((c >> 6) & 31))) & 0xFF];
38 crc = (crc >>> 8) ^ table[(crc ^ (128 | (c & 63))) & 0xFF];
39 } else if(c >= 0xD800 && c < 0xE000) {
40 c = (c & 1023) + 64; d = str.charCodeAt(i++) & 1023;
41 crc = (crc >>> 8) ^ table[(crc ^ (240 | ((c >> 8) & 7))) & 0xFF];
42 crc = (crc >>> 8) ^ table[(crc ^ (128 | ((c >> 2) & 63))) & 0xFF];
43 crc = (crc >>> 8) ^ table[(crc ^ (128 | ((d >> 6) & 15) | (c & 3))) & 0xFF];
44 crc = (crc >>> 8) ^ table[(crc ^ (128 | (d & 63))) & 0xFF];
45 } else {
46 crc = (crc >>> 8) ^ table[(crc ^ (224 | ((c >> 12) & 15))) & 0xFF];
47 crc = (crc >>> 8) ^ table[(crc ^ (128 | ((c >> 6) & 63))) & 0xFF];
48 crc = (crc >>> 8) ^ table[(crc ^ (128 | (c & 63))) & 0xFF];
49 }
50 }
51 return crc ^ -1;
52}
53
54module.exports = crc32_str;