[F] Fix zlib compression happening after response commit

This commit is contained in:
Azalea
2024-03-28 00:58:55 -04:00
parent 3f01152a4a
commit c6190146aa
9 changed files with 64 additions and 200 deletions

View File

@@ -3,17 +3,16 @@ package icu.samnyan.aqua.sega.util
import java.util.*
import kotlin.text.Charsets.UTF_8
object Decoder {
object AllNetBillingDecoder {
/**
* Decode the input byte array from Base64 MIME encoding and decompress the decoded byte array
*/
fun decode(src: ByteArray, base64: Boolean, nowrap: Boolean): Map<String, String> {
// Decode the input byte array from Base64 MIME encoding
var bytes = src
if (base64) bytes = Base64.getMimeDecoder().decode(bytes)
val bytes = if (base64) src else Base64.getMimeDecoder().decode(src)
// Decompress the decoded byte array
val output = Compression.decompress(bytes, nowrap).toString(UTF_8).trim()
val output = ZLib.decompress(bytes, nowrap).toString(UTF_8).trim()
// Split the string by '&' symbol to separate key-value pairs
return output.split("&").associate {

View File

@@ -1,61 +0,0 @@
package icu.samnyan.aqua.sega.util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
* @author samnyan (privateamusement@protonmail.com)
*/
public class Compression {
public static byte[] decompress(byte[] src, boolean nowrap) {
ByteBuf result = Unpooled.buffer();
byte[] buffer = new byte[100];
Inflater decompressor = new Inflater(nowrap);
decompressor.setInput(src);
try {
while (!decompressor.finished()) {
int count = decompressor.inflate(buffer);
if (count == 0) {
break;
}
result.writeBytes(buffer, result.readerIndex(), count);
}
decompressor.end();
return ByteBufUtil.toBytes(result);
} catch (DataFormatException e) {
e.printStackTrace();
return new byte[0];
}
}
public static byte[] decompress(byte[] src) {
return decompress(src, false);
}
public static byte[] compress(byte[] src) {
ByteBuf result = Unpooled.buffer();
byte[] buffer = new byte[100];
Deflater compressor = new Deflater();
compressor.setInput(src);
compressor.finish();
while (!compressor.finished()) {
int count = compressor.deflate(buffer);
if (count == 0) {
break;
}
result.writeBytes(buffer, result.readerIndex(), count);
}
compressor.end();
return ByteBufUtil.toBytes(result);
}
}

View File

@@ -0,0 +1,32 @@
package icu.samnyan.aqua.sega.util
import java.io.ByteArrayOutputStream
import java.util.zip.Deflater
import java.util.zip.Inflater
object ZLib {
fun decompress(src: ByteArray, nowrap: Boolean = false) = Inflater(nowrap).run {
val buffer = ByteArray(1024)
setInput(src)
ByteArrayOutputStream().use {
var count = -1
while (count != 0) {
count = inflate(buffer)
it.write(buffer, 0, count)
}
end()
it.toByteArray()
}
}
fun compress(src: ByteArray) = Deflater().run {
setInput(src)
finish()
val outputBuf = ByteArray(src.size * 4)
val compressedSize = deflate(outputBuf)
end()
outputBuf.copyOf(compressedSize)
}
}