arch

Support for ARCH archives, including compression and decompression.

An ARCH archive starts with an “ARCH” magic followed by a header, a name table, a file allocation table and the file data. Files can optionally be compressed with a byte-pair encoding scheme, based on Tinke’s implementation.

class hacktools.arch.ARCHArchive[source]

Structure of an ARCH archive header.

filenum

Number of files in the archive.

tableoff

Offset of the name table.

fatoff

Offset of the file allocation table.

nameindexoff

Offset of the name index.

dataoff

Offset of the file data.

files

List of files in the archive.

class hacktools.arch.ARCHFile[source]

Structure of a single file entry in an ARCH archive.

name

File name, read from the archive name table.

length

Length of the file data as stored in the archive.

declength

Decompressed length, equal to length if not encoded.

offset

Offset of the file data, relative to the archive dataoff.

nameoffset

Offset of the file name, relative to the archive tableoff.

encoded

Whether the file data is compressed.

hacktools.arch.read(f)[source]

Read the header and file table of an ARCH archive.

Parameters:

f (Stream) – Stream opened on the archive file.

Returns:

The parsed archive structure.

Return type:

ARCHArchive

hacktools.arch.repack(fin, f, archive, infolder)[source]

Repack an ARCH archive, replacing the files found in a folder.

Files that exist in infolder are read from there, compressing the ones marked as encoded in the archive, while the others are copied from fin. File data is aligned to 16 bytes with zeros.

Parameters:
  • fin (Stream) – Stream opened on the original archive file.

  • f (Stream) – Stream opened on the output archive file.

  • archive (ARCHArchive) – Archive structure returned by read().

  • infolder (str) – Path of the folder containing the replacement files.

Return type:

None

hacktools.arch.extract(f, archive, outfolder)[source]

Extract all the files of an ARCH archive to a folder.

Files marked as encoded are decompressed.

Parameters:
  • f (Stream) – Stream opened on the archive file.

  • archive (ARCHArchive) – Archive structure returned by read().

  • outfolder (str) – Path of the folder to extract the files to.

Return type:

None

hacktools.arch.compress(data)[source]

Compress data with the byte-pair encoding used by ARCH archives.

The most common byte pairs are recursively replaced with single bytes that don’t occur in the data, and a dictionary of these replacements is written before the encoded content.

Parameters:

data (bytes) – Data to compress.

Returns:

The compressed data.

Return type:

bytes

hacktools.arch.decompress(data, declen)[source]

Decompress byte-pair encoded data from an ARCH archive.

Parameters:
  • data (bytes) – Data to decompress.

  • declen (int) – Expected length of the decompressed data, currently unused.

Returns:

The decompressed data.

Return type:

bytes