Houjie
2025-07-24 52a3ff1bce1417b39f6872d8e8cb378e9c2ccc6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var StringBuilder_1 = require("../../util/StringBuilder");
var constants_1 = require("./constants");
/**
 * Error Correction Code for ECC200.
 */
var ErrorCorrection = /** @class */ (function () {
    function ErrorCorrection() {
    }
    /**
     * Creates the ECC200 error correction for an encoded message.
     *
     * @param codewords  the codewords
     * @param symbolInfo information about the symbol to be encoded
     * @return the codewords with interleaved error correction.
     */
    ErrorCorrection.encodeECC200 = function (codewords, symbolInfo) {
        if (codewords.length !== symbolInfo.getDataCapacity()) {
            throw new Error('The number of codewords does not match the selected symbol');
        }
        var sb = new StringBuilder_1.default();
        sb.append(codewords);
        var blockCount = symbolInfo.getInterleavedBlockCount();
        if (blockCount === 1) {
            var ecc = this.createECCBlock(codewords, symbolInfo.getErrorCodewords());
            sb.append(ecc);
        }
        else {
            // sb.setLength(sb.capacity());
            var dataSizes = [];
            var errorSizes = [];
            for (var i = 0; i < blockCount; i++) {
                dataSizes[i] = symbolInfo.getDataLengthForInterleavedBlock(i + 1);
                errorSizes[i] = symbolInfo.getErrorLengthForInterleavedBlock(i + 1);
            }
            for (var block = 0; block < blockCount; block++) {
                var temp = new StringBuilder_1.default();
                for (var d = block; d < symbolInfo.getDataCapacity(); d += blockCount) {
                    temp.append(codewords.charAt(d));
                }
                var ecc = this.createECCBlock(temp.toString(), errorSizes[block]);
                var pos = 0;
                for (var e = block; e < errorSizes[block] * blockCount; e += blockCount) {
                    sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos++));
                }
            }
        }
        return sb.toString();
    };
    ErrorCorrection.createECCBlock = function (codewords, numECWords) {
        var table = -1;
        for (var i = 0; i < constants_1.FACTOR_SETS.length; i++) {
            if (constants_1.FACTOR_SETS[i] === numECWords) {
                table = i;
                break;
            }
        }
        if (table < 0) {
            throw new Error('Illegal number of error correction codewords specified: ' + numECWords);
        }
        var poly = constants_1.FACTORS[table];
        var ecc = [];
        for (var i = 0; i < numECWords; i++) {
            ecc[i] = 0;
        }
        for (var i = 0; i < codewords.length; i++) {
            var m = ecc[numECWords - 1] ^ codewords.charAt(i).charCodeAt(0);
            for (var k = numECWords - 1; k > 0; k--) {
                if (m !== 0 && poly[k] !== 0) {
                    ecc[k] = ecc[k - 1] ^ constants_1.ALOG[(constants_1.LOG[m] + constants_1.LOG[poly[k]]) % 255];
                }
                else {
                    ecc[k] = ecc[k - 1];
                }
            }
            if (m !== 0 && poly[0] !== 0) {
                ecc[0] = constants_1.ALOG[(constants_1.LOG[m] + constants_1.LOG[poly[0]]) % 255];
            }
            else {
                ecc[0] = 0;
            }
        }
        var eccReversed = [];
        for (var i = 0; i < numECWords; i++) {
            eccReversed[i] = ecc[numECWords - i - 1];
        }
        return eccReversed.map(function (c) { return String.fromCharCode(c); }).join('');
    };
    return ErrorCorrection;
}());
exports.default = ErrorCorrection;