mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-26 05:26:45 +00:00
fix premature decompression of bbqr zlib parts
This commit is contained in:
parent
83719e7df2
commit
7b0dfd66a7
4 changed files with 26 additions and 3 deletions
|
@ -37,6 +37,7 @@ public class BBQRDecoder {
|
||||||
|
|
||||||
if(receivedParts.size() == totalParts) {
|
if(receivedParts.size() == totalParts) {
|
||||||
byte[] data = concatParts();
|
byte[] data = concatParts();
|
||||||
|
data = header.inflate(data);
|
||||||
|
|
||||||
if(type == BBQRType.PSBT) {
|
if(type == BBQRType.PSBT) {
|
||||||
result = new Result(new PSBT(data));
|
result = new Result(new PSBT(data));
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class BBQREncoder {
|
||||||
BBQREncoding encoding = desiredEncoding;
|
BBQREncoding encoding = desiredEncoding;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
encoded = encoding.encode(data);
|
encoded = encoding.encode(encoding.deflate(data));
|
||||||
if(encoding == BBQREncoding.ZLIB) {
|
if(encoding == BBQREncoding.ZLIB) {
|
||||||
String uncompressed = BBQREncoding.BASE32.encode(data);
|
String uncompressed = BBQREncoding.BASE32.encode(data);
|
||||||
if(encoded.length() > uncompressed.length()) {
|
if(encoded.length() > uncompressed.length()) {
|
||||||
|
|
|
@ -42,6 +42,11 @@ public enum BBQREncoding {
|
||||||
}, ZLIB("Z") {
|
}, ZLIB("Z") {
|
||||||
@Override
|
@Override
|
||||||
public String encode(byte[] data) throws BBQREncodingException {
|
public String encode(byte[] data) throws BBQREncodingException {
|
||||||
|
return BASE32.encode(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] deflate(byte[] data) throws BBQREncodingException {
|
||||||
try {
|
try {
|
||||||
Deflater deflater = new Deflater(JZlib.Z_BEST_COMPRESSION, 10, true);
|
Deflater deflater = new Deflater(JZlib.Z_BEST_COMPRESSION, 10, true);
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
@ -49,7 +54,7 @@ public enum BBQREncoding {
|
||||||
zOut.write(data);
|
zOut.write(data);
|
||||||
zOut.close();
|
zOut.close();
|
||||||
|
|
||||||
return BASE32.encode(out.toByteArray());
|
return out.toByteArray();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw new BBQREncodingException("Error deflating with zlib", e);
|
throw new BBQREncodingException("Error deflating with zlib", e);
|
||||||
}
|
}
|
||||||
|
@ -57,9 +62,14 @@ public enum BBQREncoding {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] decode(String part) throws BBQREncodingException {
|
public byte[] decode(String part) throws BBQREncodingException {
|
||||||
|
return BASE32.decode(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] inflate(byte[] data) throws BBQREncodingException {
|
||||||
try {
|
try {
|
||||||
Inflater inflater = new Inflater(10, true);
|
Inflater inflater = new Inflater(10, true);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(BASE32.decode(part));
|
ByteArrayInputStream in = new ByteArrayInputStream(data);
|
||||||
InflaterInputStream zIn = new InflaterInputStream(in, inflater);
|
InflaterInputStream zIn = new InflaterInputStream(in, inflater);
|
||||||
byte[] decoded = zIn.readAllBytes();
|
byte[] decoded = zIn.readAllBytes();
|
||||||
zIn.close();
|
zIn.close();
|
||||||
|
@ -96,6 +106,14 @@ public enum BBQREncoding {
|
||||||
return code;
|
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 String encode(byte[] data) throws BBQREncodingException;
|
||||||
|
|
||||||
public abstract byte[] decode(String part) throws BBQREncodingException;
|
public abstract byte[] decode(String part) throws BBQREncodingException;
|
||||||
|
|
|
@ -13,6 +13,10 @@ public record BBQRHeader(BBQREncoding encoding, BBQRType type, int seqTotal, int
|
||||||
return encoding.decode(part.substring(8));
|
return encoding.decode(part.substring(8));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] inflate(byte[] data) {
|
||||||
|
return encoding.inflate(data);
|
||||||
|
}
|
||||||
|
|
||||||
public static BBQRHeader fromString(String part) {
|
public static BBQRHeader fromString(String part) {
|
||||||
if(part.length() < 8) {
|
if(part.length() < 8) {
|
||||||
throw new IllegalArgumentException("Part too short");
|
throw new IllegalArgumentException("Part too short");
|
||||||
|
|
Loading…
Reference in a new issue