CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
crc32.js78 linesDownload Raw Back to lib
1"use strict";2 3var utils = require("./utils");4 5/**6 * The following functions come from pako, from pako/lib/zlib/crc32.js7 * released under the MIT license, see pako https://github.com/nodeca/pako/8 */9 10// Use ordinary array, since untyped makes no boost here11function makeTable() {12    var c, table = [];13 14    for(var n =0; n < 256; n++){15        c = n;16        for(var k =0; k < 8; k++){17            c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));18        }19        table[n] = c;20    }21 22    return table;23}24 25// Create table on load. Just 255 signed longs. Not a problem.26var crcTable = makeTable();27 28 29function crc32(crc, buf, len, pos) {30    var t = crcTable, end = pos + len;31 32    crc = crc ^ (-1);33 34    for (var i = pos; i < end; i++ ) {35        crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];36    }37 38    return (crc ^ (-1)); // >>> 0;39}40 41// That's all for the pako functions.42 43/**44 * Compute the crc32 of a string.45 * This is almost the same as the function crc32, but for strings. Using the46 * same function for the two use cases leads to horrible performances.47 * @param {Number} crc the starting value of the crc.48 * @param {String} str the string to use.49 * @param {Number} len the length of the string.50 * @param {Number} pos the starting position for the crc32 computation.51 * @return {Number} the computed crc32.52 */53function crc32str(crc, str, len, pos) {54    var t = crcTable, end = pos + len;55 56    crc = crc ^ (-1);57 58    for (var i = pos; i < end; i++ ) {59        crc = (crc >>> 8) ^ t[(crc ^ str.charCodeAt(i)) & 0xFF];60    }61 62    return (crc ^ (-1)); // >>> 0;63}64 65module.exports = function crc32wrapper(input, crc) {66    if (typeof input === "undefined" || !input.length) {67        return 0;68    }69 70    var isArray = utils.getTypeOf(input) !== "string";71 72    if(isArray) {73        return crc32(crc|0, input, input.length, 0);74    } else {75        return crc32str(crc|0, input, input.length, 0);76    }77};78