OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ilm 1
/*
2
 * Copyright 2014 Robin Stuart
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
 * in compliance with the License. You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License
10
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
 * or implied. See the License for the specific language governing permissions and limitations under
12
 * the License.
13
 */
14
package uk.org.okapibarcode.backend;
15
 
16
/**
17
 *
18
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
19
 */
20
public class ReedSolomon {
21
    private int logmod;
22
    private int rlen;
23
 
24
    private int[] logt;
25
    private int[] alog;
26
    private int[] rspoly;
27
    public int[] res;
28
 
29
    public int getResult(final int count) {
30
        return this.res[count];
31
    }
32
 
33
    public void init_gf(final int poly) {
34
        int m, b, p, v;
35
 
36
        // Find the top bit, and hence the symbol size
37
        for (b = 1, m = 0; b <= poly; b <<= 1) {
38
            m++;
39
        }
40
        b >>= 1;
41
        m--;
42
 
43
        // Calculate the log/alog tables
44
        this.logmod = (1 << m) - 1;
45
        this.logt = new int[this.logmod + 1];
46
        this.alog = new int[this.logmod];
47
 
48
        for (p = 1, v = 0; v < this.logmod; v++) {
49
            this.alog[v] = p;
50
            this.logt[p] = v;
51
            p <<= 1;
52
            if ((p & b) != 0) {
53
                p ^= poly;
54
            }
55
        }
56
    }
57
 
58
    public void init_code(final int nsym, int index) {
59
        int i, k;
60
 
61
        this.rspoly = new int[nsym + 1];
62
 
63
        this.rlen = nsym;
64
 
65
        this.rspoly[0] = 1;
66
        for (i = 1; i <= nsym; i++) {
67
            this.rspoly[i] = 1;
68
            for (k = i - 1; k > 0; k--) {
69
                if (this.rspoly[k] != 0) {
70
                    this.rspoly[k] = this.alog[(this.logt[this.rspoly[k]] + index) % this.logmod];
71
                }
72
                this.rspoly[k] ^= this.rspoly[k - 1];
73
            }
74
            this.rspoly[0] = this.alog[(this.logt[this.rspoly[0]] + index) % this.logmod];
75
            index++;
76
        }
77
    }
78
 
79
    public void encode(final int len, final int[] data) {
80
        int i, k, m;
81
 
82
        this.res = new int[this.rlen];
83
        for (i = 0; i < this.rlen; i++) {
84
            this.res[i] = 0;
85
        }
86
 
87
        for (i = 0; i < len; i++) {
88
            m = this.res[this.rlen - 1] ^ data[i];
89
            for (k = this.rlen - 1; k > 0; k--) {
90
                if (m != 0 && this.rspoly[k] != 0) {
91
                    this.res[k] = this.res[k - 1] ^ this.alog[(this.logt[m] + this.logt[this.rspoly[k]]) % this.logmod];
92
                } else {
93
                    this.res[k] = this.res[k - 1];
94
                }
95
            }
96
            if (m != 0 && this.rspoly[0] != 0) {
97
                this.res[0] = this.alog[(this.logt[m] + this.logt[this.rspoly[0]]) % this.logmod];
98
            } else {
99
                this.res[0] = 0;
100
            }
101
        }
102
    }
103
}