0

I am trying to implement ECC : Error correcting Code / reed-solomon. I try to apply it over a file but each split at the time. My problem is that it works for the first splits but then I get this error :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8217
at package_name.GF28.mult(GF28.java:73)
at package_name.Encoder.encode(Encoder.java:36)
at package_name.ErrorCodesMain.Ecc(ErrorCodesMain.java:27)

This exception occurs after encoding the first three splits, each split having 72bytes. Any suggestion of what can be wrong?

I tried to narrow where the problem comes from and I see that it comes from the class constructing the Galois Field: Here is where the bug happens:

public GF28 mult(char b) {
    if (!multsDone[val][b]) {
        mults[val][b] = mult(val, b);
        mults[b][val] = mults[val][b];
        multsDone[val][b] = true;
        multsDone[b][val] = true;}

    return new GF28(mults[val][b]);
}

and here is the whole class code

package package_name;
import java.util.*;

public class GF28 {
public static char[][] adds;
public static char[][] mults;
public static char[] inverse;
public static boolean[][] addsDone;
public static boolean[][] multsDone;
public static boolean[] inverseDone;

public static final char PX = (char) 0x11B;

public static void init() {
    int max = 1 << 8;
    adds = new char[max][max];
    mults = new char[max][max];
    addsDone = new boolean[max][max];
    multsDone = new boolean[max][max];
    inverse = new char[max];
    inverseDone = new boolean[max];
    for (int i = 0; i < adds.length; i++) {
        inverse[i] = 0;
        inverseDone[i] = false;
        for (int j = 0; j < adds[0].length; j++) {
            adds[i][j] = 0;
            mults[i][j] = 0;
            addsDone[i][j] = false;
            multsDone[i][j] = false;
        }
    }
}

public static char add(char a, char b) {
    return (char) (a ^ b);
}

public static char mult(char a, char b) {
    int p = a;
    int r = 0;
    while (b != 0) {
        if ((b & 1) == 1)
            r = r ^ p;
        b = (char) (b >>> 1);
        p = p << 1;
        if ((p & 0x100) == 0x100)
            p = p ^ PX;
    }
    return (char) r;
}

char val;

public GF28(char val) {
    this.val = val;
}

public GF28 add(char b) {
    if (!addsDone[val][b]) {
        adds[val][b] = add(val, b);
        adds[b][val] = adds[val][b];
        addsDone[val][b] = true;
        addsDone[b][val] = true;
    }
    return new GF28(adds[val][b]);
}

public GF28 add(GF28 b) {
    return add(b.val);
}

public GF28 mult(char b) {
    if (!multsDone[val][b]) {
        mults[val][b] = mult(val, b);
        mults[b][val] = mults[val][b];
        multsDone[val][b] = true;
        multsDone[b][val] = true;}

    return new GF28(mults[val][b]);
}

public GF28 mult(GF28 b) {
    return mult(b.val);
}

public GF28 getInverse() {
    return inverse(val);
}

public static GF28 inverse(char a) {
    if (!inverseDone[a]) {
        GF28 ga = new GF28(a);
        for (char i = 0; i < (1 << 8); i++) {
            if (ga.mult(i).val == 1) {
                inverseDone[a] = true;
                inverseDone[i] = true;
                inverse[a] = i;
                inverse[i] = a;
                break;
            }
        }
    }
    return new GF28(inverse[a]);
}

public static String getBinaryString(char c) {
    String s = "";
    for (int i = 0; i < 8; i++) {
        s = (c % 2) + s;
        c = (char) (c >> 1);
    }
    return "0b" + s;
}

public static ArrayList<Integer> findGenerators() {
    ArrayList<Integer> gs = new ArrayList<Integer>();
    for (int j = 1; j < 1 << 8; j++) {
        GF28 gz = new GF28((char) j);
        HashSet<Character> hs = new HashSet<Character>();
        hs.add(gz.val);
        for (int i = 2; i <= 1 << 8; i++) {
            gz = gz.mult((char) j);
            hs.add(gz.val);
        }
        if (hs.size() == 255)
            gs.add(j);
    }
    return gs;
}

}

rima101
  • 43
  • 1
  • 7
  • You have a bug in your program. I suggest you find the simplest example which shows this error and step through your code with a debugger. – Peter Lawrey Aug 11 '13 at 19:13
  • I narrowed the problem, it is in the Galois Field as I edited in my post – rima101 Aug 11 '13 at 19:28
  • Do you have a short example which reproduces the problem i.e. we can run stand alone? Have you tried using a debugger? – Peter Lawrey Aug 11 '13 at 19:33
  • I am not familiar with using a debugger. I am not that good in java I am afraid – rima101 Aug 11 '13 at 19:37
  • Using an IDE and a debugger is essential IMHO, after that you should learn to use a profiler. – Peter Lawrey Aug 11 '13 at 19:40
  • But I am not a programer and nor planning to become one. I am a security engineer who is having to deal in a project to use Reed Solomon in Java. I searched everywhere and only found this code. Anyway, thanks for the advice. I ll try to learn. – rima101 Aug 11 '13 at 20:13
  • If you don't want to program, you need to pay someone to do it. Asking someone to do your work for your for free is not considered well on this forum. – Peter Lawrey Aug 11 '13 at 20:16
  • I don't think I asked anybody to do any coding for me. I posted my code that is just a small fraction of the whole thing and asked people who are more experienced if they can help with a bug, and I think that is the purpose of this forum, helping each other solving errors and bugs. Anyway Thank you again. – rima101 Aug 11 '13 at 20:23
  • The purpose of the forum is to help people help themselves by teaching them. The first question often asked is what have you done and what exactly are you have trouble with and how can we teach you to be able to do this for yourself the next time. – Peter Lawrey Aug 11 '13 at 20:33

0 Answers0