Update compression to work with both pops and npumdimg.

This commit is contained in:
Li 2023-04-16 20:55:40 +12:00
parent eb5120a674
commit 4665da300c
5 changed files with 57 additions and 44 deletions

View File

@ -1075,7 +1075,7 @@ namespace PbpResign
Console.WriteLine("VersionKey: " + BitConverter.ToString(NewVersionKey.ToArray()));
NpUmdImg npumd = new NpUmdImg(new NpDrmInfo(NewVersionKey.ToArray(), CId, npHdr.NpFlags),
"fft.iso", "ULUS10297", File.ReadAllBytes("TEST\\PARAM.SFO"), false);
"fft.iso", "ULUS10297", File.ReadAllBytes("TEST\\PARAM.SFO"), true);
npumd.CreatePsar();
byte[] paramFile = File.ReadAllBytes("TEST\\PARAM.SFO");

View File

@ -133,7 +133,6 @@ namespace PopsBuilder.Psp
{
byte[] lzRcBuf = Lz.compress(isoBuf, true);
//memset(lzrc_buf + lzrc_size, 0, 16);
//
int ratio = (lzRcBuf.Length * 100) / BLOCK_SZ;

View File

@ -22,17 +22,8 @@ namespace PspCrypto
//return 0;
var lzrc = new Lzrc(np9660);
// create a buffer big enough to hold compression result
byte[] compression_result = new byte[in_buf.Length];
// (this could get resized by the compression code, if its too small)
// compress data, and get the compressed data length
int compressed_length = lzrc.lzrc_compress(ref compression_result, compression_result.Length, in_buf, in_buf.Length);
// resize array to actual compressed length ...
Array.Resize(ref compression_result, compressed_length);
return compression_result;
return lzrc.lzrc_compress(in_buf, in_buf.Length);
}
public static int decompress(byte[] @out, byte[] @in, int size, int insize, bool np9660=false)
{

View File

@ -27,15 +27,16 @@ namespace PspCrypto
private byte[][] bm_match;
private byte[][] bm_len;
const int max_tbl_sz = 65280;
const int tbl_sz = 65536;
const int MAX_WIN_SZ = 16384;
const int MAX_TBL_SZ = 65280;
const int TBL_SZ = 65536;
static byte[] text_buf = new byte[tbl_sz];
static int t_start, t_end, t_fill, sp_fill;
static int t_len, t_pos;
private byte[] text_buf = new byte[TBL_SZ];
private int t_start, t_end, t_fill, sp_fill;
private int t_len, t_pos;
static int[] prev = new int[tbl_sz], next = new int[tbl_sz];
static int[] root = new int[tbl_sz];
private int[] prev = new int[TBL_SZ], next = new int[TBL_SZ];
private int[] root = new int[TBL_SZ];
public Lzrc(bool np9660 = false)
{
@ -91,14 +92,14 @@ namespace PspCrypto
output[out_ptr++] = b;
}
void re_init(ref byte[] out_buf, int out_len, ref byte[] in_buf, int in_len)
void re_init(ref byte[] in_buf, int in_len)
{
input = in_buf;
this.in_len = in_len;
in_ptr = 0;
output = out_buf;
this.out_len = out_len;
this.output = new byte[in_len];
this.out_len = in_len;
out_ptr = 0;
range = 0xffffffff;
@ -268,7 +269,7 @@ namespace PspCrypto
{
int i;
for (i = 0; i < tbl_sz; i++)
for (i = 0; i < TBL_SZ; i++)
{
root[i] = -1;
prev[i] = -1;
@ -290,7 +291,7 @@ namespace PspCrypto
if (sp_fill == in_len)
return;
content_size = (t_fill < t_end) ? (max_tbl_sz + t_fill - t_end) : (t_fill - t_end);
content_size = (t_fill < t_end) ? (MAX_TBL_SZ + t_fill - t_end) : (t_fill - t_end);
if (content_size >= 509)
return;
@ -308,7 +309,7 @@ namespace PspCrypto
}
else
{
back_size = max_tbl_sz - t_fill;
back_size = MAX_TBL_SZ - t_fill;
if (t_start == 0)
back_size -= 1;
if (sp_fill + back_size > in_len)
@ -331,12 +332,12 @@ namespace PspCrypto
sp_fill += front_size;
Array.ConstrainedCopy(text_buf, 255, text_buf, max_tbl_sz, front_size);
Array.ConstrainedCopy(text_buf, 255, text_buf, MAX_TBL_SZ, front_size);
//memcpy(text_buf + max_tbl_sz, text_buf, 255);
t_fill += front_size;
if (t_fill >= max_tbl_sz)
t_fill -= max_tbl_sz;
if (t_fill >= MAX_TBL_SZ)
t_fill -= MAX_TBL_SZ;
}
}
void remove_node(int p)
@ -370,7 +371,7 @@ namespace PspCrypto
//src = text_buf[pos..];
//win = text_buf[t_start..];
content_size = (t_fill < pos) ? (max_tbl_sz + t_fill - pos) : (t_fill - pos);
content_size = (t_fill < pos) ? (MAX_TBL_SZ + t_fill - pos) : (t_fill - pos);
t_len = 1;
t_pos = 0;
match_len = t_len;
@ -420,7 +421,7 @@ namespace PspCrypto
{
int mp = pos - p;
if (mp < 0)
mp += max_tbl_sz;
mp += MAX_TBL_SZ;
if (mp < t_pos)
{
t_len = i;
@ -454,15 +455,15 @@ namespace PspCrypto
int i, win_size;
int tmp_len, tmp_pos;
win_size = (t_end >= t_start) ? (t_end - t_start) : (max_tbl_sz + t_end - t_start);
win_size = (t_end >= t_start) ? (t_end - t_start) : (MAX_TBL_SZ + t_end - t_start);
for (i = 0; i < length; i++)
{
if (win_size == 16384)
if (win_size == MAX_WIN_SZ)
{
remove_node(t_start);
t_start += 1;
if (t_start == max_tbl_sz)
if (t_start == MAX_TBL_SZ)
t_start = 0;
}
else
@ -475,8 +476,8 @@ namespace PspCrypto
insert_node(t_end, out tmp_len, out tmp_pos, 0);
}
t_end += 1;
if (t_end >= max_tbl_sz)
t_end -= max_tbl_sz;
if (t_end >= MAX_TBL_SZ)
t_end -= MAX_TBL_SZ;
}
}
void re_bittree(ref byte[] probs,int index, int limit, int number)
@ -499,7 +500,6 @@ namespace PspCrypto
tmp = number >> n;
bit = (number >> (n - 1)) & 1;
re_bit(ref probs, index + tmp, bit);
n -= 1;
} while (n > 0);
}
@ -567,7 +567,7 @@ namespace PspCrypto
{
if (out_ptr == out_len)
{
out_len += 0x100;
out_len += 0x1000;
Array.Resize(ref output, out_len);
}
@ -637,7 +637,14 @@ namespace PspCrypto
re_putbyte((byte)((code >> 8) & 0xff));
re_putbyte((byte)((code >> 0) & 0xff));
}
public int lzrc_compress(ref byte[] out_buf, int out_len, byte[] in_buf, int in_len)
private byte[] re_trunc()
{
Array.Resize(ref output, out_ptr);
return output;
}
public byte[] lzrc_compress(byte[] in_buf, int in_len)
{
int match_step, re_state, len_state, dist_state;
int i, cur_byte, last_byte;
@ -645,10 +652,8 @@ namespace PspCrypto
int match_dist, dist_bits, limit;
int round = -1;
len_state = 0;
// initalize buffers to all 0x80
re_init(ref out_buf, out_len, ref in_buf, in_len);
re_init(ref in_buf, in_len);
// initalize the tree
init_tree();
@ -698,11 +703,11 @@ namespace PspCrypto
cur_byte = re_getbyte();
re_bittree(ref bm_literal[((last_byte >> lc) & 0x07)], 0, 0x100, cur_byte);
if (in_ptr >= in_len)
if (!this.np9660 && in_ptr >= in_len)
{
re_normalize();
re_flush();
return out_ptr;
return re_trunc();
}
}
else
@ -736,7 +741,7 @@ namespace PspCrypto
{
re_normalize();
re_flush();
return out_ptr;
return re_trunc();
}
}
@ -756,9 +761,13 @@ namespace PspCrypto
dist_state += 7;
if (this.np9660)
{
limit = 44;
}
else
{
limit = 16;
}
}
@ -766,7 +775,9 @@ namespace PspCrypto
dist_bits = 0;
if(match_dist > 0) {
while ((match_dist >> dist_bits) != 1)
{
dist_bits += 1;
}
}
else
{

View File

@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PsvImage", "PsvImage\PsvIma
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PopsBuilder", "PopsBuilder\PopsBuilder.csproj", "{D1DF66DB-52BE-489C-A292-525BC71F2BB2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnicornManaged", "UnicornManaged\UnicornManaged.csproj", "{AF774802-8809-415F-AE18-437036630FEA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnicornTest", "UnicornTest\UnicornTest.csproj", "{E7B6A043-340B-4BAF-99B6-B7AB903C817E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +37,14 @@ Global
{D1DF66DB-52BE-489C-A292-525BC71F2BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1DF66DB-52BE-489C-A292-525BC71F2BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1DF66DB-52BE-489C-A292-525BC71F2BB2}.Release|Any CPU.Build.0 = Release|Any CPU
{AF774802-8809-415F-AE18-437036630FEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF774802-8809-415F-AE18-437036630FEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF774802-8809-415F-AE18-437036630FEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF774802-8809-415F-AE18-437036630FEA}.Release|Any CPU.Build.0 = Release|Any CPU
{E7B6A043-340B-4BAF-99B6-B7AB903C817E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7B6A043-340B-4BAF-99B6-B7AB903C817E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7B6A043-340B-4BAF-99B6-B7AB903C817E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7B6A043-340B-4BAF-99B6-B7AB903C817E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE