/********************************************************************
 * @author:      Kaven
 * @email:       kaven@wuwenkai.com
 * @website:     http://kavenblog.com
 * @file:        [basic] /src/libs/hashing_algorithms/KavenCRC.ts
 * @create:      2019-04-02 17:02:48.463
 * @modify:      2019-04-03 14:31:49.920
 * @version:     1.1.20
 * @times:       33
 * @lines:       242
 * @copyright:   Copyright © 2019 Kaven. All Rights Reserved.
 * @description: [description]
 * @license:
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ********************************************************************/
import { BitString } from "../BitString";
var KavenCRC = /** @class */ (function () {
    function KavenCRC(modal) {
        this.Width = modal.Width;
        this.Polynomial = modal.Polynomial;
        this.InitialValue = modal.InitialValue;
        this.FinalXORValue = modal.FinalXORValue;
        this.InputReflected = modal.InputReflected;
        this.ResultReflected = modal.ResultReflected;
        this.table = [];
    }
    Object.defineProperty(KavenCRC, "CRC32", {
        get: function () {
            return {
                Width: 32,
                Polynomial: new BitString(0x4C11DB7),
                InitialValue: new BitString(0xffffffff),
                FinalXORValue: new BitString(0xffffffff),
                InputReflected: true,
                ResultReflected: true,
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC, "CRC64_ECMA_182", {
        /**
         * The ECMA polynomial, defined in ECMA 182.
         * @link http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-64-ecma-182
         */
        get: function () {
            return {
                Width: 64,
                Polynomial: new BitString("42F0E1EBA9EA3693", 16),
                InitialValue: new BitString(0),
                FinalXORValue: new BitString(0),
                InputReflected: false,
                ResultReflected: false,
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC, "CRC64_ECMA_WE", {
        /**
         * Wolfgang Ehrhardt
         */
        get: function () {
            return {
                Width: 64,
                Polynomial: new BitString("42F0E1EBA9EA3693", 16),
                InitialValue: new BitString("F".repeat(16), 16),
                FinalXORValue: new BitString("F".repeat(16), 16),
                InputReflected: false,
                ResultReflected: false,
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC, "CRC64_ECMA_XZ", {
        /**
         * XZ Utils
         */
        get: function () {
            return {
                Width: 64,
                Polynomial: new BitString("42F0E1EBA9EA3693", 16),
                InitialValue: new BitString("F".repeat(16), 16),
                FinalXORValue: new BitString("F".repeat(16), 16),
                InputReflected: true,
                ResultReflected: true,
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC, "CRC64_ISO", {
        /**
         * The ISO polynomial, defined in ISO 3309 and used in HDLC.
         */
        get: function () {
            return {
                Width: 64,
                Polynomial: new BitString(0x1B),
                InitialValue: new BitString("F".repeat(16), 16),
                FinalXORValue: new BitString("F".repeat(16), 16),
                InputReflected: true,
                ResultReflected: true,
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC.prototype, "ShiftBits", {
        get: function () {
            return this.Width - 8;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC.prototype, "TopBitMask", {
        get: function () {
            return new BitString(1).LeftShift(this.Width - 1);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(KavenCRC.prototype, "FullBitMask", {
        get: function () {
            return new BitString("1".repeat(this.Width), 2);
        },
        enumerable: true,
        configurable: true
    });
    KavenCRC.prototype.MakeTable = function () {
        if (this.table.IsNotEmpty()) {
            return this.table;
        }
        for (var n = 0; n < 256; n++) {
            var c = new BitString(n).LeftShift(this.ShiftBits);
            for (var k = 0; k < 8; k++) {
                if (c.AND(this.TopBitMask).AnyBit()) {
                    c = c.LeftShift(1);
                    c = c.XOR(this.Polynomial);
                }
                else {
                    c = c.LeftShift(1);
                }
            }
            this.table[n] = c.AND(this.FullBitMask).SetLength(this.Width);
        }
        return this.table;
    };
    KavenCRC.prototype.PrintTable = function (columns) {
        if (columns === void 0) { columns = 8; }
        this.MakeTable();
        for (var _i = 0, _a = this.table.Split(columns, true); _i < _a.length; _i++) {
            var item = _a[_i];
            console.log(item.map(function (p) { return p.HEX; }).join(", "));
        }
    };
    KavenCRC.prototype.Compute = function (str) {
        this.MakeTable();
        var data = str.ToUTF8ByteArray();
        var crc = this.InitialValue;
        for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
            var byte = data_1[_i];
            var current = new BitString(byte);
            if (this.InputReflected) {
                current = current.Reflect(8);
            }
            current = current.LeftShift(this.ShiftBits);
            // update the MSB of crc value with next input byte
            crc = crc.XOR(current);
            // this MSB byte value is the index into the lookup table
            var pos = crc.RightShift(this.ShiftBits).AND(0xff).ToNumber();
            // shift out this index
            crc = crc.LeftShift(8).AND(this.FullBitMask);
            //  XOR-in remainder from lookup table using the calculated index
            crc = crc.XOR(this.table[pos]);
        }
        if (this.ResultReflected) {
            crc = crc.Reflect(this.Width);
        }
        return crc.XOR(this.FinalXORValue).SetLength(this.Width);
    };
    KavenCRC.prototype.ComputerWithoutTable = function (str) {
        var data = str.ToUTF8ByteArray();
        var crc = this.InitialValue;
        for (var _i = 0, data_2 = data; _i < data_2.length; _i++) {
            var byte = data_2[_i];
            var current = new BitString(byte);
            if (this.InputReflected) {
                current = current.Reflect(8);
            }
            current = current.LeftShift(this.ShiftBits);
            // update the MSB of crc value with next input byte
            crc = crc.XOR(current);
            for (var i = 0; i < 8; i++) {
                if (crc.AND(this.TopBitMask).AnyBit()) {
                    crc = crc.LeftShift(1);
                    crc = crc.XOR(this.Polynomial);
                }
                else {
                    crc = crc.LeftShift(1);
                }
            }
        }
        if (this.ResultReflected) {
            crc = crc.Reflect(this.Width);
        }
        return crc.XOR(this.FinalXORValue).SetLength(this.Width);
    };
    return KavenCRC;
}());
export { KavenCRC };
//# sourceMappingURL=KavenCRC.js.map