/**
  * Compute the syndrome of the input [parity, message]
  *
  * @param data [parity, message]
  * @param syndrome The syndromes (checksums) of the data
  * @return true If syndromes are all zeros
  */
 private boolean computeSyndrome(int[] data, int[] syndrome) {
   boolean corruptionFound = false;
   for (int i = 0; i < paritySize; i++) {
     syndrome[i] = GF.substitute(data, primitivePower[i]);
     if (syndrome[i] != 0) {
       corruptionFound = true;
     }
   }
   return !corruptionFound;
 }
 @Override
 public void decode(int[] data, int[] erasedLocation, int[] erasedValue) {
   if (erasedLocation.length == 0) {
     return;
   }
   assert (erasedLocation.length == erasedValue.length);
   for (int i = 0; i < erasedLocation.length; i++) {
     data[erasedLocation[i]] = 0;
   }
   for (int i = 0; i < erasedLocation.length; i++) {
     errSignature[i] = primitivePower[erasedLocation[i]];
     erasedValue[i] = GF.substitute(data, primitivePower[i]);
   }
   GF.solveVandermondeSystem(errSignature, erasedValue, erasedLocation.length);
 }
  /**
   * Given parity symbols followed by message symbols, return the locations of symbols that are
   * corrupted. Can resolve up to (parity length / 2) error locations.
   *
   * @param data The message and parity. The parity should be placed in the first part of the array.
   *     In each integer, the relevant portion is present in the least significant bits of each int.
   *     The number of elements in data is stripeSize() + paritySize(). <b>Note that data may be
   *     changed after calling this method.</b>
   * @param errorLocations The set to put the error location results
   * @return true If the locations can be resolved, return true.
   */
  public boolean computeErrorLocations(int[] data, Set<Integer> errorLocations) {
    assert (data.length == paritySize + stripeSize && errorLocations != null);
    errorLocations.clear();
    int maxError = paritySize / 2;
    int[][] syndromeMatrix = new int[maxError][];
    for (int i = 0; i < syndromeMatrix.length; ++i) {
      syndromeMatrix[i] = new int[maxError + 1];
    }
    int[] syndrome = new int[paritySize];

    if (computeSyndrome(data, syndrome)) {
      // Parity check OK. No error location added.
      return true;
    }
    for (int i = 0; i < maxError; ++i) {
      for (int j = 0; j < maxError + 1; ++j) {
        syndromeMatrix[i][j] = syndrome[i + j];
      }
    }
    GF.gaussianElimination(syndromeMatrix);
    int[] polynomial = new int[maxError + 1];
    polynomial[0] = 1;
    for (int i = 0; i < maxError; ++i) {
      polynomial[i + 1] = syndromeMatrix[maxError - 1 - i][maxError];
    }
    for (int i = 0; i < paritySize + stripeSize; ++i) {
      int possibleRoot = GF.divide(1, primitivePower[i]);
      if (GF.substitute(polynomial, possibleRoot) == 0) {
        errorLocations.add(i);
      }
    }
    // Now recover with error locations and check the syndrome again
    int[] locations = new int[errorLocations.size()];
    int k = 0;
    for (int loc : errorLocations) {
      locations[k++] = loc;
    }
    int[] erasedValue = new int[locations.length];
    decode(data, locations, erasedValue);
    for (int i = 0; i < locations.length; ++i) {
      data[locations[i]] = erasedValue[i];
    }
    return computeSyndrome(data, syndrome);
  }
Пример #4
0
 /**
  * A "bulk" version of the substitute. Tends to be 2X faster than the "int" substitute in a loop.
  *
  * @param p input polynomial
  * @param q store the return result
  * @param x input field
  */
 public void substitute(byte[][] p, byte[] q, int x) {
   substitute(p, q, x, 0, p[0].length);
 }