fix premature decompression of bbqr zlib parts

This commit is contained in:
Craig Raw 2024-05-03 11:46:33 +02:00
parent 83719e7df2
commit 7b0dfd66a7
4 changed files with 26 additions and 3 deletions

View file

@ -37,6 +37,7 @@ public class BBQRDecoder {
if(receivedParts.size() == totalParts) {
byte[] data = concatParts();
data = header.inflate(data);
if(type == BBQRType.PSBT) {
result = new Result(new PSBT(data));

View file

@ -35,7 +35,7 @@ public class BBQREncoder {
BBQREncoding encoding = desiredEncoding;
try {
encoded = encoding.encode(data);
encoded = encoding.encode(encoding.deflate(data));
if(encoding == BBQREncoding.ZLIB) {
String uncompressed = BBQREncoding.BASE32.encode(data);
if(encoded.length() > uncompressed.length()) {

View file

@ -42,6 +42,11 @@ public enum BBQREncoding {
}, ZLIB("Z") {
@Override
public String encode(byte[] data) throws BBQREncodingException {
return BASE32.encode(data);
}
@Override
public byte[] deflate(byte[] data) throws BBQREncodingException {
try {
Deflater deflater = new Deflater(JZlib.Z_BEST_COMPRESSION, 10, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -49,7 +54,7 @@ public enum BBQREncoding {
zOut.write(data);
zOut.close();
return BASE32.encode(out.toByteArray());
return out.toByteArray();
} catch(Exception e) {
throw new BBQREncodingException("Error deflating with zlib", e);
}
@ -57,9 +62,14 @@ public enum BBQREncoding {
@Override
public byte[] decode(String part) throws BBQREncodingException {
return BASE32.decode(part);
}
@Override
public byte[] inflate(byte[] data) throws BBQREncodingException {
try {
Inflater inflater = new Inflater(10, true);
ByteArrayInputStream in = new ByteArrayInputStream(BASE32.decode(part));
ByteArrayInputStream in = new ByteArrayInputStream(data);
InflaterInputStream zIn = new InflaterInputStream(in, inflater);
byte[] decoded = zIn.readAllBytes();
zIn.close();
@ -96,6 +106,14 @@ public enum BBQREncoding {
return code;
}
public byte[] deflate(byte[] data) throws BBQREncodingException {
return data;
}
public byte[] inflate(byte[] data) throws BBQREncodingException {
return data;
}
public abstract String encode(byte[] data) throws BBQREncodingException;
public abstract byte[] decode(String part) throws BBQREncodingException;

View file

@ -13,6 +13,10 @@ public record BBQRHeader(BBQREncoding encoding, BBQRType type, int seqTotal, int
return encoding.decode(part.substring(8));
}
public byte[] inflate(byte[] data) {
return encoding.inflate(data);
}
public static BBQRHeader fromString(String part) {
if(part.length() < 8) {
throw new IllegalArgumentException("Part too short");