Corrected logic to handle case where remainder polynomial has leading 0 coefficients.

git-svn-id: https://zxing.googlecode.com/svn/trunk@730 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-11-19 12:39:18 +00:00
parent a16d3ba6ef
commit 0a1a550a0f
3 changed files with 11 additions and 8 deletions

View file

@ -22,7 +22,7 @@ package com.google.zxing;
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public class WriterException extends Exception {
public final class WriterException extends Exception {
public WriterException(String message) {
super(message);

View file

@ -51,9 +51,12 @@ public final class ReedSolomonEncoder {
}
public void encode(int[] toEncode, int ecBytes) {
if (ecBytes == 0) {
throw new IllegalArgumentException("No error correction bytes");
}
int dataBytes = toEncode.length - ecBytes;
if (dataBytes < 0) {
throw new IllegalArgumentException("Too few data bytes provided: " + dataBytes);
if (dataBytes <= 0) {
throw new IllegalArgumentException("No data bytes provided");
}
GF256Poly generator = buildGenerator(ecBytes);
int[] infoCoefficients = new int[dataBytes];
@ -62,11 +65,11 @@ public final class ReedSolomonEncoder {
info = info.multiplyByMonomial(ecBytes, 1);
GF256Poly remainder = info.divide(generator)[1];
int[] coefficients = remainder.getCoefficients();
if (coefficients.length < ecBytes) {
throw new RuntimeException("Coefficients array is smaller than EC array (" +
coefficients.length + " < " + ecBytes + ")");
int numZeroCoefficients = ecBytes - coefficients.length;
for (int i = 0; i < numZeroCoefficients; i++) {
toEncode[dataBytes + i] = 0;
}
System.arraycopy(coefficients, 0, toEncode, dataBytes, ecBytes);
System.arraycopy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.length);
}
}

View file

@ -43,7 +43,7 @@ public final class ReedSolomonEncoderQRCodeTestCase extends AbstractReedSolomonT
for (int i = 0; i < 100; i++) {
int size = random.nextInt(1000);
int[] toEncode = new int[size];
int ecBytes = random.nextInt(2 * (1 + size / 8));
int ecBytes = 1 + random.nextInt(2 * (1 + size / 8));
int dataBytes = size - ecBytes;
for (int j = 0; j < dataBytes; j++) {
toEncode[j] = random.nextInt(256);