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) * @author dswitkin@google.com (Daniel Switkin)
*/ */
public class WriterException extends Exception { public final class WriterException extends Exception {
public WriterException(String message) { public WriterException(String message) {
super(message); super(message);

View file

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