Tweaked small pieces of code, synchronizing from the parent "QR Code generator library" project.

pull/134/head
Project Nayuki 6 years ago
parent 132c8f32e2
commit f6e7b17602

@ -87,6 +87,8 @@ final class BitBuffer {
public void appendBits(int val, int len) { public void appendBits(int val, int len) {
if (len < 0 || len > 31 || val >>> len != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
if (Integer.MAX_VALUE - bitLength < len)
throw new IllegalStateException("Maximum length reached");
if (bitLength + len + 1 > data.length << 5) if (bitLength + len + 1 > data.length << 5)
data = Arrays.copyOf(data, data.length * 2); data = Arrays.copyOf(data, data.length * 2);

@ -381,6 +381,7 @@ public final class QrCode {
// Returns a new byte string representing the given data with the appropriate error correction // Returns a new byte string representing the given data with the appropriate error correction
// codewords appended to it, based on this object's version and error correction level. // codewords appended to it, based on this object's version and error correction level.
private byte[] addEccAndInterleave(byte[] data) { private byte[] addEccAndInterleave(byte[] data) {
Objects.requireNonNull(data);
if (data.length != getNumDataCodewords(version, errorCorrectionLevel)) if (data.length != getNumDataCodewords(version, errorCorrectionLevel))
throw new IllegalArgumentException(); throw new IllegalArgumentException();

@ -146,12 +146,14 @@ public final class QrSegment {
*/ */
public static QrSegment makeEci(int assignVal) { public static QrSegment makeEci(int assignVal) {
BitBuffer bb = new BitBuffer(); BitBuffer bb = new BitBuffer();
if (0 <= assignVal && assignVal < (1 << 7)) if (assignVal < 0)
throw new IllegalArgumentException("ECI assignment value out of range");
else if (assignVal < (1 << 7))
bb.appendBits(assignVal, 8); bb.appendBits(assignVal, 8);
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { else if (assignVal < (1 << 14)) {
bb.appendBits(2, 2); bb.appendBits(2, 2);
bb.appendBits(assignVal, 14); bb.appendBits(assignVal, 14);
} else if ((1 << 14) <= assignVal && assignVal < 1000000) { } else if (assignVal < 1_000_000) {
bb.appendBits(6, 3); bb.appendBits(6, 3);
bb.appendBits(assignVal, 21); bb.appendBits(assignVal, 21);
} else } else
@ -284,7 +286,7 @@ public final class QrSegment {
/*-- Constructor --*/ /*-- Constructor --*/
private Mode(int mode, int... ccbits) { private Mode(int mode, int... ccbits) {
this.modeBits = mode; modeBits = mode;
numBitsCharCount = ccbits; numBitsCharCount = ccbits;
} }
@ -294,6 +296,7 @@ public final class QrSegment {
// Returns the bit width of the character count field for a segment in this mode // Returns the bit width of the character count field for a segment in this mode
// in a QR Code at the given version number. The result is in the range [0, 16]. // in a QR Code at the given version number. The result is in the range [0, 16].
int numCharCountBits(int ver) { int numCharCountBits(int ver) {
assert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION;
return numBitsCharCount[(ver + 7) / 17]; return numBitsCharCount[(ver + 7) / 17];
} }

Loading…
Cancel
Save