ARZL

LZRA (ARZL in big-endian) is a compression and encoding format used on PS Vita. It is used for example to store files used by the SKBL like the NSKBL and some TrustZone kernel modules. It is also used on GIM texture data used by /sce_sys/right/right.suprx.

Naming
It must be part of the "LZ" algorithms. See LZ algorithms overview. It might be similar to:
 * LZMA (Lempel-Ziv-Markov). 1998. See,.
 * LZHAM (Lempel-Ziv-Huffman-Arithmetic-Markov). 2010. See.
 * LZR (Lempel-Ziv-Renau): PSP. 2004. PSP also uses KL3E and KL4E. LZR modification to LZ77 allows pointers to reference anything that has been encoded without being limited by the length of the search. See psxtract (?is PS3 same algorithm?) or pspdecrypt. See:
 * BenHur's code: pspdecrypt/libLZR.c by BenHur, rfactortools/libLZR.c by BenHur
 * TPUnix's code: npdpc/tlzrc.c by TPUnix
 * LZRC (LZMA based, unknown variant of LZRC): PS3. 2006. See:
 * psxtract: See PSXtract/Windows/lz.cpp by Hykem or PSXtract/Windows/lz.c by Hykem or psxtract/Linux/lz.c by Hykem
 * sign_np: See sign_np/tlzrc.c by Hykem
 * make_npdata: make_npdata/Windows/lz.cpp by Hykem or make_npdata/Linux/lz.cpp by Hykem

There is a typo in SKBL functions names where it is named ARLZ instead of ARZL.

Header
ARZL header is simply the string "ARZL" (41 52 5A 4C).

Encoding
To encode data into ARZL: 1) Apply ARM filter. See . 2) ARZL encode

Decoding
To decode ARZL data: 1) ARZL decode. See SKBL. 2) Remove ARM filter. See and SKBL.

ARM Filter
ARZL encoded/decoded data is not the raw data but filtered data. It is applied an ARM filter for efficient compression, rather than obfuscation.

Although there are three versions of the ARM filters, the basic operation is the same. The filter is just bit swaps as well as some deterministic changes using information from the offset.

Old filter
In firmwares 0.920-1.06, the ARM filter is different and operates on byte-sized instead of word-sized operands. The "new" filter algorithm is used instead since firmware 1.50.

Use the following algorithm for filtering e.g. NSKBL from these firmwares:

Version 1
ARM filter version 1 is the same as version 0 except that the offset information is added instead of subtracted.

Version 2
ARM filter version 2 is the same as version 0 but in addition, there is an additional operation to swap two nibbles in certain conditions. The condition is found through a learning process and may be overfitted.

Tools
TODO