/********************************************************************
 * @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";
export class KavenCRC {
    static get CRC32() {
        return {
            Width: 32,
            Polynomial: new BitString(0x4C11DB7),
            InitialValue: new BitString(0xffffffff),
            FinalXORValue: new BitString(0xffffffff),
            InputReflected: true,
            ResultReflected: true,
        };
    }
    /**
     * The ECMA polynomial, defined in ECMA 182.
     * @link http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-64-ecma-182
     */
    static get CRC64_ECMA_182() {
        return {
            Width: 64,
            Polynomial: new BitString("42F0E1EBA9EA3693", 16),
            InitialValue: new BitString(0),
            FinalXORValue: new BitString(0),
            InputReflected: false,
            ResultReflected: false,
        };
    }
    /**
     * Wolfgang Ehrhardt
     */
    static get CRC64_ECMA_WE() {
        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,
        };
    }
    /**
     * XZ Utils
     */
    static get CRC64_ECMA_XZ() {
        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,
        };
    }
    /**
     * The ISO polynomial, defined in ISO 3309 and used in HDLC.
     */
    static get CRC64_ISO() {
        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,
        };
    }
    get ShiftBits() {
        return this.Width - 8;
    }
    get TopBitMask() {
        return new BitString(1).LeftShift(this.Width - 1);
    }
    get FullBitMask() {
        return new BitString("1".repeat(this.Width), 2);
    }
    constructor(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 = [];
    }
    MakeTable() {
        if (this.table.IsNotEmpty()) {
            return this.table;
        }
        for (let n = 0; n < 256; n++) {
            let c = new BitString(n).LeftShift(this.ShiftBits);
            for (let 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;
    }
    PrintTable(columns = 8) {
        this.MakeTable();
        for (const item of this.table.Split(columns, true)) {
            console.log(item.map(p => p.HEX).join(", "));
        }
    }
    Compute(str) {
        this.MakeTable();
        const data = str.ToUTF8ByteArray();
        let crc = this.InitialValue;
        for (const byte of data) {
            let 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
            const 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);
    }
    ComputerWithoutTable(str) {
        const data = str.ToUTF8ByteArray();
        let crc = this.InitialValue;
        for (const byte of data) {
            let 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 (let 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);
    }
}
//# sourceMappingURL=KavenCRC.js.map