- C 70.6%
- Zig 28.6%
- Python 0.3%
- Makefile 0.3%
- Batchfile 0.2%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| pkg2zip-master | ||
| src | ||
| test | ||
| .DS_Store | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| README.md | ||
| test_zip.zig | ||
zpkg2zip
A Zig reimplementation of pkg2zip - a utility that decrypts PlayStation Vita, PSP, and PSX PKG files and creates ZIP packages.
⚠️ Work in Progress: This is a learning project and partial implementation. Core PKG parsing and CLI structure are complete, but full extraction functionality is still being developed.
About
zpkg2zip is a cross-platform tool written in Zig that processes PlayStation package files:
- Vita packages: Applications, DLC, patches, themes, and PSM titles
- PSP packages: Games, DLC, themes, and updates
- PSX packages: PlayStation 1 games
The tool decrypts PKG files and optionally creates ZIP archives with the proper folder structure for installation on PlayStation Vita via VitaShell.
Features
Implemented ✅
- Command-line argument parsing (matching original pkg2zip)
- PKG header reading and validation
- Package type detection (Vita/PSP/PSX/PS3)
- Metadata parsing (content type, SFO info, items)
- Encryption key selection based on key type
- SFO (System File Object) parser
- Byte order conversion utilities
- Type-safe PKG format structures
- zRIF decoder skeleton
In Progress 🚧
- AES-128 CTR/ECB decryption implementation
- Full SFO metadata extraction
- ZIP file creation with compression
- PKG item extraction and processing
- License (RIF) generation from zRIF
- PSP ISO/CSO creation
- Output file naming and organization
Usage
# Build the project
zig build
# Run with a PKG file
./zig-out/bin/zpkg2zip package.pkg
# With zRIF license string
./zig-out/bin/zpkg2zip package.pkg eNpjYGBgYPBgYGDgYGRk...
# List package information
./zig-out/bin/zpkg2zip -l package.pkg
# Extract without creating ZIP
./zig-out/bin/zpkg2zip -x package.pkg
# Show help
./zig-out/bin/zpkg2zip -h
Command-Line Options
Parameters:
-x, --extract Extract only. No zip compression
-l, --list Shows the package (sfo) name and exits
-b, --no-bgdl Disable bgdl output for VITA Theme extraction
-q, --quiet Do not output anything to stdout
-h, --help Shows this help message
PSP/PSX only options:
-c[NUM] Create a *.CSO file instead of ISO. [NUM] is compression ratio (1-9)
-p, --psp Extracts PSP files in their original EBOOT.PBP format
-d, --decrypt Always decrypt PSP DLC/EDAT files
PSM only options:
-a, --android-psm Extract into PSM for Android format
Usage: zpkg2zip [-x] [-c[N]] [-b] [-p] <file.pkg> [zRIF]
Building
Prerequisites
- Zig 0.12.0 or later
Compile
# Debug build
zig build
# Release build (optimized)
zig build -Doptimize=ReleaseFast
# Run tests
zig build test
Project Structure
zpkg2zip/
├── src/
│ ├── main.zig # CLI entry point and main logic
│ ├── root.zig # Library exports
│ ├── types.zig # PKG types, constants, and enums
│ ├── utils.zig # Byte order conversion utilities
│ ├── sfo.zig # SFO (System File Object) parser
│ └── zrif.zig # zRIF license decoder
├── build.zig # Build configuration
└── README.md # This file
Module Overview
types.zig
Defines all PKG-related types, constants, and enums:
PkgType: Package type enumeration (Vita/PSP/PSX/PS3)PkgHeader: PKG file header structureMetaType: Metadata entry typesPackageMetadata: Extracted metadata from SFOConfig: CLI configuration optionsKeys: AES encryption keys for different platforms
utils.zig
Byte order conversion utilities for reading/writing integers:
- Little-endian:
readU16LE,readU32LE,readU64LE, etc. - Big-endian:
readU16BE,readU32BE,readU64BE, etc. - String utilities:
copyString,strlenZ
sfo.zig
System File Object (SFO) parser for extracting package metadata:
parseSfo: Parses SFO data and extracts metadata- Handles TITLE, CONTENT_ID, CATEGORY, version info, etc.
- UTF-8 string processing with validation
zrif.zig
zRIF license decoder (base64 + zlib compressed RIF format):
decode: Decodes zRIF string to RIF license data- Base64 decoding
- zlib decompression with custom dictionary support
main.zig
Command-line interface implementation:
- Argument parsing
- PKG file opening and validation
- Header parsing and metadata extraction
- Package type detection
- Encryption key selection
PKG File Format
The PKG format is documented at:
PKG Header Structure (192 bytes)
Offset Size Description
------ ---- -----------
0x00 4 Magic (0x7f504b47 - ".PKG")
0x04 2 PKG info
0x06 2 PKG type
0x08 4 Metadata offset
0x0C 4 Metadata count
0x10 4 Metadata size
0x14 4 Item count
0x18 8 Total size
0x20 8 Data offset
0x28 8 Data size
0x30 48 Content ID
0x60 16 Digest
0x70 16 IV (Initialization Vector)
...
0xE7 1 Key type (& 7)
Content Types
| Value | Type |
|---|---|
| 0x01 | PS3 |
| 0x06 | PSX |
| 0x07 | PSP |
| 0x09 | PSP Theme |
| 0x0E | PSP PCEngine |
| 0x0F | PSP Go |
| 0x10 | PSP Mini/NeoGeo |
| 0x15 | Vita Application |
| 0x16 | Vita DLC |
| 0x18 | Vita PSM |
| 0x1D | Vita PSM (alt) |
| 0x1F | Vita Theme |
References
- Original pkg2zip: https://github.com/mmozeiko/pkg2zip
- NoNpDrm plugin: https://github.com/TheOfficialFloW/NoNpDrm
- VitaShell: https://github.com/TheOfficialFloW/VitaShell
- Henkaku: https://henkaku.xyz/
License
This is a learning project and reimplementation. The original pkg2zip is public domain.
This Zig implementation is also released into the public domain under the Unlicense.
Disclaimer
This tool is for educational purposes and for use with content you legally own. The authors are not responsible for any misuse of this software.
Development Status
Current Phase: Foundation Complete ✅
The project currently implements:
- ✅ Complete CLI argument parsing
- ✅ PKG file format structures
- ✅ Header reading and validation
- ✅ Metadata parsing
- ✅ Package type detection
- ✅ Encryption key selection
- ✅ SFO parser with tests
- ✅ Utility functions with tests
Next Steps
- AES Decryption: Implement AES-128 CTR and ECB modes using
std.crypto.core.aes - Item Extraction: Read and decrypt PKG items
- ZIP Creation: Create ZIP archives using compression
- File Naming: Generate proper output filenames from metadata
- PSP Support: Implement ISO/CSO creation for PSP packages
- Testing: Add comprehensive tests with sample PKG files
Contributing
This is a learning project, but contributions are welcome! Areas where help would be appreciated:
- Implementing remaining encryption/decryption logic
- Adding comprehensive tests
- Improving error messages
- Performance optimizations
- Documentation improvements
Building for Release
# Linux/macOS
zig build -Doptimize=ReleaseFast
strip zig-out/bin/zpkg2zip
# Windows
zig build -Doptimize=ReleaseFast -Dtarget=x86_64-windows
# Cross-compile for other platforms
zig build -