25 Commits

Author SHA1 Message Date
374358711b Beginning libWiiPy refactors
No major functional changes have been made so far other than a couple of things being shifted between files, but a lot of bad code has been rewritten to hopefully make libWiiPy maintainable into the future.
2025-12-16 23:14:33 -05:00
ce5d118de1 Create cert.sys during EmuNAND title installation if not found 2025-07-12 13:03:42 -04:00
6d38df9133 Type __init()__ functions, clears all mypy issues 2025-05-27 13:57:39 -04:00
2ca2ff1f44 Fix all mypy errors 2025-05-27 13:46:21 -04:00
79ab33c18a Added support for progress callbacks in NUS download functions 2025-05-24 23:38:55 -04:00
e06bb39f4c Properly create/update uid.sys during EmuNAND title installs 2025-04-18 13:54:28 -04:00
8269a0db98 (Title) Add check to make sure Ticket data exist before decrypting content 2025-04-17 16:31:16 -04:00
8adbef26b1 Updated README to reflect features since v0.5.0 2025-02-10 16:26:45 -05:00
5dde9f7835 Changed how the Title ID is handled in Tickets, stubbed out NUS endpoint validation 2025-02-10 13:36:39 -05:00
93abad1f31 LZ77 Compression: Now 12.5% faster! 2025-01-29 22:48:58 -05:00
9eabf2caee Added a lower LZ77 compression level that runs faster 2025-01-26 12:56:41 -05:00
5ae867197b (doc) Workaround for ScrollSpy issue breaking TOC 2025-01-25 20:40:44 -05:00
6552dc5fa8 Add LZ77 Compression Function (#22)
* Unfinished wiiload module and LZ77 compression code

* Updated WIP LZ77 compressor, still not working yet

* Updated WIP LZ77 compressor, still not working yet (again)

* LZ77 compression is now fully functional! (But still very slow)

* Added compress_lz77 docstrings, temporarily removed unfinished wiiload module
2025-01-23 22:26:34 -05:00
93790d6f58 Added docs for LZ77 module 2025-01-16 22:43:51 -05:00
f0b79e1f39 Match Root-CA00000002-XS00000004 as a dev Ticket 2025-01-12 22:23:32 -05:00
06b36290ed Add error handling for custom NUS endpoints 2025-01-12 21:54:16 -05:00
47472e7b94 Added LZ77 decompression module 2025-01-08 18:43:48 -05:00
7c5af6ebe0 Always user all uppercase when getting installed titles on EmuNAND 2025-01-02 17:20:09 -05:00
046645eb56 Added method to title module to get if a title is signed legitimately 2024-12-23 23:50:14 -05:00
e45c7a3076 (docs) Began writing module descriptions 2024-12-21 18:09:37 -05:00
c2f6225500 Entirely restructured API documentation, now much easier to navigate 2024-12-20 19:21:53 -05:00
04d17a58d2 Added missing note about retail vs development root keys 2024-12-16 22:24:27 -05:00
aa9e8fb4ea Updated placeholder docstrings in cert module 2024-12-16 22:22:11 -05:00
8a15b1e82e Handle cert chain data in a title as a CertificateChain instead of bytes 2024-12-16 22:14:07 -05:00
ece19177c4 Added error handling to cert module, added support for dev CA cert 2024-12-16 21:55:58 -05:00
60 changed files with 1409 additions and 600 deletions

View File

@@ -2,7 +2,7 @@
# libWiiPy
libWiiPy is a modern Python 3 library for handling the various files and formats found on the Wii. It aims to be simple to use, well maintained, and offer as many features as reasonably possible in one library, so that a newly-written Python program could do 100% of its Wii-related work with just one library. It also aims to be fully cross-platform, so that any tools written with it can also be cross-platform.
libWiiPy is inspired by [libWiiSharp](https://github.com/TheShadowEevee/libWiiSharp), which was originally created by `Leathl` and is now maintained by [@TheShadowEevee](https://github.com/TheShadowEevee). If you're looking for a Wii library that isn't in Python, then go check it out!
libWiiPy is inspired by [libWiiSharp](https://github.com/TheShadowEevee/libWiiSharp), which was originally created by `Leathl` and is now maintained by [@TheShadowEevee](https://github.com/TheShadowEevee).
# Features
@@ -10,14 +10,18 @@ This list will expand as libWiiPy is developed, but these features are currently
- TMD and Ticket parsing/editing (`.tmd`, `.tik`)
- Title parsing/editing, including content encryption/decryption (both retail and development)
- WAD file parsing/editing (`.wad`)
- Downloading titles from the NUS
- Downloading titles and their components from the NUS
- Certificate, TMD, and Ticket signature verification
- Packing and unpacking U8 archives (`.app`, `.arc`)
- Decompressing ASH files (`.ash`, both the standard variants and the variants found in My Pokémon Ranch)
- Compressing/Decompressing LZ77-compressed files
- IOS patching
- NAND-related functionality:
- EmuNAND title management (currently requires an existing EmuNAND)
- `content.map` parsing/editing
- `setting.txt` parsing/editing
- `uid.sys` parsing/editing
- Limited channel banner parsing/editing
- Assorted miscellaneous features used to make the other core features possible
For a more detailed look at what's available in libWiiPy, check out our [API docs](https://ninjacheetah.github.io/libWiiPy).
@@ -68,6 +72,3 @@ Thank you to all of the contributors to the documentation on the WiiBrew pages t
### One additional special thanks to [@DamiDoop](https://github.com/DamiDoop)!
She made the very cool banner you can see at the top of this README, and has also helped greatly with my sanity throughout debugging this library.
**Note:** While libWiiPy is directly inspired by libWiiSharp and aims to have feature parity with it, no code from either libWiiSharp or Wii.py was used in the making of this library. All code is original and is written by [@NinjaCheetah](https://github.com/NinjaCheetah), [@rvtr](https://github.com/rvtr), and any other GitHub contributors.

23
docs/source/api.md Normal file
View File

@@ -0,0 +1,23 @@
# API Documentation
libWiiPy is divided up into a few subpackages to organize related features.
| Package | Description |
|--------------------------------------|-----------------------------------------------------------------|
| [libWiiPy.archive](/archive/archive) | Used to pack and extract archive formats used on the Wii |
| [libWiiPy.media](/media/media) | Used for parsing and manipulating media formats used on the Wii |
| [libWiiPy.nand](/nand/nand) | Used for working with EmuNANDs and core system files on the Wii |
| [libWiiPy.title](/title/title) | Used for parsing and manipulating Wii titles |
When using libWiiPy in your project, you can choose to either only import the package that you need, or you can use `import libWiiPy` to import the entire package, which each module being available at `libWiiPy.<package>.<module>`.
## Full Package Contents
```{toctree}
:maxdepth: 8
/archive/archive
/media/media
/nand/nand
/title/title
```

View File

@@ -0,0 +1,23 @@
# libWiiPy.archive Package
## Description
The `libWiiPy.archive` package contains modules for packing and extracting archive formats used by the Wii. This currently includes packing and unpacking support for U8 archives and decompression support for ASH archives.
## Modules
| Module | Description |
|----------------------------------------|---------------------------------------------------------|
| [libWiiPy.archive.ash](/archive/ash) | Provides support for decompressing ASH archives |
| [libWiiPy.archive.lz77](/archive/lz77) | Provides support for the LZ77 compression scheme |
| [libWiiPy.archive.u8](/archive/u8) | Provides support for packing and extracting U8 archives |
## Full Package Contents
```{toctree}
:maxdepth: 4
/archive/ash
/archive/lz77
/archive/u8
```

View File

@@ -0,0 +1,16 @@
# libWiiPy.archive.ash Module
## Description
The `libWiiPy.archive.ash` module provides support for handling ASH files, which are a compressed format primarily used in the Wii Menu, but also in some other titles such as My Pokémon Ranch.
At present, libWiiPy only has support for decompressing ASH files, with compression as a planned feature for the future.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.archive.ash
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.archive.lz77 Module
## Description
The `libWiiPy.archive.lz77` module provides support for handling LZ77 compression, which is a compression format used across the Wii and other Nintendo consoles.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.archive.lz77
:members:
:undoc-members:
:show-inheritance:
```

16
docs/source/archive/u8.md Normal file
View File

@@ -0,0 +1,16 @@
# libWiiPy.archive.u8 Module
## Description
The `libWiiPy.archive.u8` module provides support for handling U8 archives, which are a non-compressed archive format used extensively on the Wii to join multiple files into one.
This module exposes functions for both packing and unpacking U8 archives, as well as code to parse IMET headers. IMET headers are a header format used specifically for U8 archives containing the banner of a channel, as they store the localized name of the channel along with other banner metadata.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.archive.u8
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -11,12 +11,19 @@ from datetime import date
project = 'libWiiPy'
copyright = f'{date.today().year}, NinjaCheetah & Contributors'
author = 'NinjaCheetah & Contributors'
version = 'main'
release = 'main'
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
extensions = ['myst_parser', 'sphinx.ext.napoleon', 'sphinx_copybutton', 'sphinx_tippy', 'sphinx_design']
extensions = [
'myst_parser',
'sphinx.ext.napoleon',
'sphinx_copybutton',
'sphinx_tippy',
'sphinx_design'
]
templates_path = ['_templates']
exclude_patterns = ["Thumbs.db", ".DS_Store"]
@@ -30,7 +37,8 @@ html_logo = "banner.png"
html_title = "libWiiPy API Docs"
html_theme_options = {
"repository_url": "https://github.com/NinjaCheetah/libWiiPy",
"use_repository_button": True
"use_repository_button": True,
"show_toc_level": 3
}
# MyST Configuration

View File

@@ -4,9 +4,11 @@ sd_hide_title: true
# Overview
# libWiiPy API Docs
# libWiiPy Documentation
Welcome to the API documentation website for libWiiPy! libWiiPy is a modern Python 3 library for handling the various files and formats found on the Wii.
Welcome to the documentation website for libWiiPy! libWiiPy is a modern Python 3 library for handling the various files and formats found on the Wii.
Just need to see the API? [libWiiPy API Documentation](/api)
```{toctree}
:hidden:
@@ -34,13 +36,12 @@ titles/nus-downloading.md
```{toctree}
:hidden:
:caption: Other Useful Pages
:caption: More
modules.md
api.md
```
## Indices and tables
* [Full Index](<project:#genindex>)
* [Module Index](<project:#modules>)
* <project:#search>

View File

@@ -1,28 +0,0 @@
# libWiiPy.archive package
## Submodules
### libWiiPy.archive.ash module
```{eval-rst}
.. automodule:: libWiiPy.archive.ash
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.archive.u8 module
```{eval-rst}
.. automodule:: libWiiPy.archive.u8
:members:
:undoc-members:
:show-inheritance:
```
## Module contents
```{eval-rst}
.. automodule:: libWiiPy.archive
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -1,36 +0,0 @@
# libWiiPy package
## Subpackages
```{toctree}
:maxdepth: 4
libWiiPy.archive
libWiiPy.media
libWiiPy.nand
libWiiPy.title
```
## Submodules
### libWiiPy.shared module
libWiiPy's ``shared`` module is private and contains only private functions used by other modules.
```{eval-rst}
.. automodule:: libWiiPy.shared
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.types module
libWiiPy's ``types`` module is private and contains only private classes used by other modules.
```{eval-rst}
.. automodule:: libWiiPy.types
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -1,11 +0,0 @@
# libWiiPy.media package
## Submodules
### libWiiPy.media.banner module
```{eval-rst}
.. automodule:: libWiiPy.media.banner
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -1,27 +0,0 @@
# libWiiPy.nand package
## Submodules
### libWiiPy.nand.emunand module
```{eval-rst}
.. automodule:: libWiiPy.nand.emunand
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.nand.setting module
```{eval-rst}
.. automodule:: libWiiPy.nand.setting
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.nand.sys module
```{eval-rst}
.. automodule:: libWiiPy.nand.sys
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -1,99 +0,0 @@
# libWiiPy.title package
## Submodules
### libWiiPy.title.cert module
```{eval-rst}
.. automodule:: libWiiPy.title.cert
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.commonkeys module
```{eval-rst}
.. automodule:: libWiiPy.title.commonkeys
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.content module
```{eval-rst}
.. automodule:: libWiiPy.title.content
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.crypto module
```{eval-rst}
.. automodule:: libWiiPy.title.crypto
:members:
:undoc-members:
:show-inheritance:
```
### libWiipy.title.iospatcher module
```{eval-rst}
.. automodule:: libWiiPy.title.iospatcher
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.nus module
```{eval-rst}
.. automodule:: libWiiPy.title.nus
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.ticket module
```{eval-rst}
.. automodule:: libWiiPy.title.ticket
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.title module
```{eval-rst}
.. automodule:: libWiiPy.title.title
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.tmd module
```{eval-rst}
.. automodule:: libWiiPy.title.tmd
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.util module
```{eval-rst}
.. automodule:: libWiiPy.title.util
:members:
:undoc-members:
:show-inheritance:
```
### libWiiPy.title.wad module
```{eval-rst}
.. automodule:: libWiiPy.title.wad
:members:
:undoc-members:
:show-inheritance:
```
## Module contents
```{eval-rst}
.. automodule:: libWiiPy.title
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.media.banner Module
## Description
The `libWiiPy.media.banner` module is essentially a stub at this point in time. It only provides one dataclass that is likely to become a traditional class when fully implemented. It is not recommended to use this module for anything yet.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.media.banner
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,19 @@
# libWiiPy.media Package
## Description
The `libWiiPy.media` package contains modules used for parsing and editing media formats used by the Wii. This currently only includes limited support for parsing channel banners.
## Modules
| Module | Description |
|----------------------------------------|---------------------------------------------------|
| [libWiiPy.media.banner](/media/banner) | Provides support for basic channel banner parsing |
## Full Package Contents
```{toctree}
:maxdepth: 4
/media/banner
```

View File

@@ -1,7 +0,0 @@
# Modules Overview
```{toctree}
:maxdepth: 4
libWiiPy
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.nand.emunand Module
## Description
The `libWiiPy.nand.emunand` module provides support for creating and managing Wii EmuNANDs. At present, you cannot create an EmuNAND compatible with something like NEEK on a real Wii with the features provided by this library, but you can create an EmuNAND compatible with Dolphin.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.nand.emunand
:members:
:undoc-members:
:show-inheritance:
```

23
docs/source/nand/nand.md Normal file
View File

@@ -0,0 +1,23 @@
# libWiiPy.nand Package
## Description
The `libWiiPy.nand` package contains modules for parsing and manipulating EmuNANDs as well as modules for parsing and editing core system files found on the Wii's NAND.
## Modules
| Module | Description |
|----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| [libWiiPy.nand.emunand](/nand/emunand) | Provides support for parsing, creating, and editing EmuNANDs |
| [libWiiPy.nand.setting](/nand/setting) | Provides support for parsing, creating, and editing `setting.txt`, which is used to store the console's region and serial number |
| [libWiiPy.nand.sys](/nand/sys) | Provides support for parsing, creating, and editing `uid.sys`, which is used to store a log of all titles run on a console |
## Full Package Contents
```{toctree}
:maxdepth: 4
/nand/emunand
/nand/setting
/nand/sys
```

View File

@@ -0,0 +1,16 @@
# libWiiPy.nand.setting Module
## Description
The `libWiiPy.nand.setting` module provides support for handling the Wii's `setting.txt` file. This file is stored as part of the Wii Menu's save data (stored in `/title/00000001/00000002/data/`) and is an encrypted text file that's primarily used to store your console's serial number and region information.
This module allows you to encrypt or decrypt this file, and exposes the keys stored in it for editing.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.nand.setting
:members:
:undoc-members:
:show-inheritance:
```

14
docs/source/nand/sys.md Normal file
View File

@@ -0,0 +1,14 @@
# libWiiPy.nand.sys Module
## Description
The `libWiiPy.nand.sys` module provides support for editing system files used on the Wii. Currently, it only offers support for `uid.sys`, which keeps a record of the Title IDs of every title launched on the console, assigning each one a unique ID.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.nand.sys
:members:
:undoc-members:
:show-inheritance:
```

16
docs/source/title/cert.md Normal file
View File

@@ -0,0 +1,16 @@
# libWiiPy.title.cert Module
## Description
The `libWiiPy.title.cert` module provides support for parsing the various signing certificates used by the Wii for content validation.
This module allows you to write your own code for validating the authenticity of a TMD or Ticket by providing the certificates from the Wii's certificate chain. Both retail and development certificate chains are supported.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.cert
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.commonkeys Module
## Description
The `libWiiPy.title.commonkeys` module simply provides easy access to the Wii's common encryption keys.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.commonkeys
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.content Module
## Description
The `libWiiPy.title.content` module provides support for parsing, adding, removing, and editing content files from a digital Wii title.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.content
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.crypto Module
## Description
The `libWiiPy.title.crypto` module provides low-level cryptography functions required for handling digital Wii titles. It does not expose many functions that are likely to be required during typical use, and instead acts more as a dependency for other modules.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.crypto
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.iospatcher Module
## Description
The `libWiiPy.title.iospatcher` module provides support for applying various binary patches to IOS' ES module. These patches and what they do can be found attached to the methods used to apply them.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.iospatcher
:members:
:undoc-members:
:show-inheritance:
```

15
docs/source/title/nus.md Normal file
View File

@@ -0,0 +1,15 @@
# libWiiPy.title.nus Module
## Description
The `libWiiPy.title.nus` module provides support for downloading digital Wii titles from the Nintendo Update Servers. This module provides easy methods for downloading TMDs, common Tickets (when present), encrypted content, and the certificate chain.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.nus
:members:
:undoc-members:
:show-inheritance:
:special-members: __call__
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.ticket Module
## Description
The `libWiiPy.title.ticket` module provides support for handling Tickets, which are the license files used to decrypt the content of digital titles during installation. This module allows for easy parsing and editing of Tickets.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.ticket
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,40 @@
# libWiiPy.title Package
## Description
The `libWiiPy.title` package contains modules for interacting with Wii titles. This is the most complete package in libWiiPy, as it offers the functionality one would be most likely to need. As a result, it gets the most attention during development and should be the most reliable.
## Modules
| Module | Description |
|------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
| [libWiiPy.title.cert](/title/cert) | Provides support for parsing and validating the certificates used for title verification |
| [libWiiPy.title.commonkeys](/title/commonkeys) | Provides easy access to all common encryption keys |
| [libWiiPy.title.content](/title/content) | Provides support for parsing and editing content included as part of digital titles |
| [libWiiPy.title.crypto](/title/crypto) | Provides low-level cryptography functions used to handle encryption in other modules |
| [libWiiPy.title.iospatcher](/title/iospatcher) | Provides an easy interface to apply patches to IOSes |
| [libWiiPy.title.nus](/title/nus) | Provides support for downloading TMDs, Tickets, encrypted content, and the certificate chain from the Nintendo Update Servers |
| [libWiiPy.title.ticket](/title/ticket) | Provides support for parsing and editing Tickets used for content decryption |
| [libWiiPy.title.title](/title/title.title) | Provides high-level support for parsing and editing an entire title with the context of each component |
| [libWiiPy.title.tmd](/title/tmd) | Provides support for parsing and editing TMDs (Title Metadata) |
| [libWiiPy.title.wad](/title/wad) | Provides support for parsing and editing WAD files, allowing you to load each component into the other available classes |
| [libWiiPy.title.types](/title/types) | Provides shared types used across the title module. |
| [libWiiPy.title.versions](/title/versions) | Provides utility functions for converting the format that a title's version is in. |
## Full Package Contents
```{toctree}
:maxdepth: 4
/title/cert
/title/commonkeys
/title/content
/title/crypto
/title/iospatcher
/title/nus
/title/ticket
/title/title.title
/title/tmd
/title/wad
/title/types
/title/versions
```

View File

@@ -0,0 +1,17 @@
# libWiiPy.title.title Module
## Description
The `libWiiPy.title.title` module provides a high-level interface for handling all the components of a digital Wii title through one class. It allows for directly importing a WAD, and will automatically extract the various components and load them into their appropriate classes. Additionally, it provides duplicates of some methods found in those classes that require fewer arguments, as it has the context of the other components and is able to retrieve additional data automatically.
An example of that idea can be seen with the method `get_content_by_index()`. In its original definition, which can be seen at <project:#libWiiPy.title.content.ContentRegion.get_content_by_index>, you are required to supply the Title Key for the title that the content is sourced from. In contrast, when using <project:#libWiiPy.title.title.Title.get_content_by_index>, you do not need to supply a Title Key, as the Title object already has the context of the Ticket and can retrieve the Title Key from it automatically. In a similar vein, this module provides the easiest route for verifying that a title is legitimately signed by Nintendo. The method <project:#libWiiPy.title.title.Title.get_is_signed> is able to access the entire certificate chain, the TMD, and the Ticket, and is therefore able to verify all components of the title by itself.
Because using <project:#libWiiPy.title.title.Title> allows many operations to be much simpler than if you manage the components separately, it's generally recommended to use it whenever possible.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.title
:members:
:undoc-members:
```

14
docs/source/title/tmd.md Normal file
View File

@@ -0,0 +1,14 @@
# libWiiPy.title.tmd Module
## Description
The `libWiiPy.title.tmd` module provides support for handling TMD (Title Metadata) files, which contain the metadata of both digital and physical Wii titles. This module allows for easy parsing and editing of TMDs.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.tmd
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.types Module
## Description
The `libWiiPy.title.types` module provides shared types used across the title module.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.types
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -0,0 +1,14 @@
# libWiiPy.title.versions Module
## Description
The `libWiiPy.title.versions` module provides functions for converting the format that a title's version is in.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.versions
:members:
:undoc-members:
:show-inheritance:
```

14
docs/source/title/wad.md Normal file
View File

@@ -0,0 +1,14 @@
# libWiiPy.title.wad Module
## Description
The `libWiiPy.title.wad` module provides support for handling WAD (Wii Archive Data) files, which is the format used to deliver digital Wii titles. This module allows for extracting the various components for a WAD, as well as properly padding and writing out that data when it has been edited using other modules.
## Module Contents
```{eval-rst}
.. automodule:: libWiiPy.title.wad
:members:
:undoc-members:
:show-inheritance:
```

View File

@@ -34,7 +34,7 @@ And viola! We have a WAD object that we can use to get each separate part of our
## Picking the WAD Apart
Now that we have our WAD loaded, we need to separate it out into its components. On top of the parts we already established, a WAD also contains a certificate, checked by IOS during official title installations to ensure that a title was signed by Nintendo, and potentially two more areas called the footer and the CRL. Footers aren't a necessary part of a WAD, and when they do exist, they typically only contain the build timestamp and the machine it was built on. CRLs are even less common, and have never actually been found inside any WAD, but we know they exist because of things we've seen that Nintendo would really rather we hadn't. Because these three components don't have data we can edit, they're only ever represented as bytes, and do not have their own classes.
Now that we have our WAD loaded, we need to separate it out into its components. On top of the parts we already established, a WAD also contains a certificate chain, which is used by IOS during official title installations to ensure that a title was signed by Nintendo, and potentially two more areas called the footer and the CRL. Footers aren't a necessary part of a WAD, and when they do exist, they typically only contain the build timestamp and the machine it was built on. CRLs are even less common, and have never actually been found inside any WAD, but we know they exist because of things we've seen that Nintendo would really rather we hadn't. Certificate chains also have a class that we'll cover after the main three components, but the latter two components don't have data we can edit, so they're only ever represented as bytes and do not have their own classes.
### The TMD
@@ -110,11 +110,19 @@ Now that we know things are working, why don't we speed things up a little by us
And just like that, we have our TMD, Ticket, and decrypted content all extracted! From here, what you do with them is up to you and whatever program you're working on. For example, to make a simple WAD extractor, you may want to write all these files to an output directory.
### The Certificate Chain
As mentioned at the start of this guide, WADs also contain a certificate chain. We don't necessarily need this data right now, but getting it is very similar to the other components:
```pycon
>>> certificate_chain = libWiiPy.title.CertificateChain()
>>> certificate_chain.load(wad.get_cert_data())
>>>
```
### The Other Data
As mentioned earlier in this guide, WADs also contain up to three extra regions of data: the certificate, the footer, and the CRL. The procedure for extracting all of these is pretty simple, and follows the same formula as any other data in a WAD:
Also mentioned earlier in this guide, WADs may contain two additional regions of data know as the footer (or "meta"), and the CRL. The procedure for extracting all of these is pretty simple, and follows the same formula as any other data in a WAD:
```pycon
>>> certificate = wad.get_cert_data()
>>> footer = wad.get_meta_data()
>>> crl = wad.get_crl_data()
>>>
@@ -123,7 +131,7 @@ As mentioned earlier in this guide, WADs also contain up to three extra regions
Beyond getting their raw data, there isn't anything you can directly do with these components with libWiiPy. If one of these components doesn't exist, libWiiPy will simply return an empty bytes object.
:::{note}
Managed to find a WAD somewhere with CRL data? I'd love to here more, so feel free to email me at [ninjacheetah@ncxprogramming.com](mailto:ninjacheetah@ncxprogramming.com).
Managed to find a WAD somewhere with CRL data? I'd love to hear more, so feel free to email me at [ninjacheetah@ncxprogramming.com](mailto:ninjacheetah@ncxprogramming.com).
:::
<hr>

View File

@@ -1,6 +1,6 @@
[project]
name = "libWiiPy"
version = "0.6.0"
version = "1.0.0"
authors = [
{ name="NinjaCheetah", email="ninjacheetah@ncxprogramming.com" },
{ name="Lillian Skinner", email="lillian@randommeaninglesscharacters.com" }
@@ -13,7 +13,7 @@ classifiers = [
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
@@ -23,7 +23,8 @@ classifiers = [
]
dependencies = [
"pycryptodome",
"requests"
"requests",
"types-requests"
]
keywords = ["Wii", "wii"]

View File

@@ -1,6 +1,7 @@
build
pycryptodome
requests
types-requests
sphinx
sphinx-book-theme
myst-parser

View File

@@ -2,4 +2,5 @@
# https://github.com/NinjaCheetah/libWiiPy
from .ash import *
from .lz77 import *
from .u8 import *

View File

@@ -5,13 +5,14 @@
# co-authored by NinjaCheetah.
# https://github.com/NinjaCheetah/ASH0-tools
#
# See <link pending> for details about the ASH archive format.
# See <link pending> for details about the ASH compression format.
import io
from dataclasses import dataclass as _dataclass
from dataclasses import dataclass
from typing import List
@_dataclass
@dataclass
class _ASHBitReader:
"""
An _ASHBitReader class used to parse individual words in an ASH file. Private class used by the ASH module.
@@ -93,7 +94,7 @@ def _ash_bit_reader_read_bits(bit_reader: _ASHBitReader, num_bits: int):
return bits
def _ash_read_tree(bit_reader: _ASHBitReader, width: int, left_tree: [int], right_tree: [int]):
def _ash_read_tree(bit_reader: _ASHBitReader, width: int, left_tree: List[int], right_tree: List[int]):
# Read either the symbol or distance tree from the ASH file, and return the root of that tree.
work = [0] * (2 * (1 << width))
work_pos = 0

View File

@@ -0,0 +1,291 @@
# "archive/lz77.py" from libWiiPy by NinjaCheetah & Contributors
# https://github.com/NinjaCheetah/libWiiPy
#
# See https://wiibrew.org/wiki/LZ77 for details about the LZ77 compression format.
import io
from dataclasses import dataclass as _dataclass
from typing import List, Tuple
_LZ_MIN_DISTANCE = 0x01 # Minimum distance for each reference.
_LZ_MAX_DISTANCE = 0x1000 # Maximum distance for each reference.
_LZ_MIN_LENGTH = 0x03 # Minimum length for each reference.
_LZ_MAX_LENGTH = 0x12 # Maximum length for each reference.
@_dataclass
class _LZNode:
dist: int = 0
len: int = 0
weight: int = 0
def _compress_compare_bytes(buffer: List[int], offset1: int, offset2: int, abs_len_max: int) -> int:
# Compare bytes up to the maximum length we can match. Start by comparing the first 3 bytes, since that's the
# minimum match length and this allows for a more optimized early exit.
num_matched = 0
while num_matched < abs_len_max:
if buffer[offset1 + num_matched] != buffer[offset2 + num_matched]:
break
num_matched += 1
return num_matched
def _compress_search_matches_optimized(buffer: List[int], pos: int) -> Tuple[int, int]:
bytes_left = len(buffer) - pos
global _LZ_MAX_DISTANCE, _LZ_MIN_LENGTH, _LZ_MAX_LENGTH, _LZ_MIN_DISTANCE
# Default to only looking back 4096 bytes, unless we've moved fewer than 4096 bytes, in which case we should
# only look as far back as we've gone.
max_dist = min(_LZ_MAX_DISTANCE, pos)
# Default to only matching up to 18 bytes, unless fewer than 18 bytes remain, in which case we can only match
# up to that many bytes.
max_len = min(_LZ_MAX_LENGTH, bytes_left)
# Log the longest match we found and its offset.
biggest_match, biggest_match_pos = 0, 0
# Search for matches.
for i in range(_LZ_MIN_DISTANCE, max_dist + 1):
num_matched = _compress_compare_bytes(buffer, pos - i, pos, max_len)
if num_matched > biggest_match:
biggest_match = num_matched
biggest_match_pos = i
if biggest_match == max_len:
break
return biggest_match, biggest_match_pos
def _compress_search_matches_greedy(buffer: List[int], pos: int) -> Tuple[int, int]:
# Finds and returns the first valid match, rather that finding the best one.
bytes_left = len(buffer) - pos
global _LZ_MAX_DISTANCE, _LZ_MAX_LENGTH, _LZ_MIN_DISTANCE
# Default to only looking back 4096 bytes, unless we've moved fewer than 4096 bytes, in which case we should
# only look as far back as we've gone.
max_dist = min(_LZ_MAX_DISTANCE, pos)
# Default to only matching up to 18 bytes, unless fewer than 18 bytes remain, in which case we can only match
# up to that many bytes.
max_len = min(_LZ_MAX_LENGTH, bytes_left)
match, match_pos = 0, 0
for i in range(_LZ_MIN_DISTANCE, max_dist + 1):
match = _compress_compare_bytes(buffer, pos - i, pos, max_len)
match_pos = i
if match >= _LZ_MIN_LENGTH or match == max_len:
break
return match, match_pos
def _compress_node_is_ref(node: _LZNode) -> bool:
return node.len >= _LZ_MIN_LENGTH
def _compress_get_node_cost(length: int) -> int:
if length >= _LZ_MIN_LENGTH:
num_bytes = 2
else:
num_bytes = 1
return 1 + (num_bytes * 8)
def _compress_lz77_optimized(data: bytes) -> bytes:
# Optimized compressor based around a node graph that finds optimal string matches. Originally the default
# implementation, but unfortunately it's very slow.
nodes = [_LZNode() for _ in range(len(data))]
# Iterate over the uncompressed data, starting from the end.
pos = len(data)
global _LZ_MAX_LENGTH, _LZ_MIN_LENGTH, _LZ_MIN_DISTANCE
data_list = list(data)
while pos:
pos -= 1
node = nodes[pos]
# Limit the maximum search length when we're near the end of the file.
max_search_len = min(_LZ_MAX_LENGTH, len(data_list) - pos)
if max_search_len < _LZ_MIN_DISTANCE:
max_search_len = 1
# Initialize as 1 for each, since that's all we could use if we weren't compressing.
length, dist = 1, 1
if max_search_len >= _LZ_MIN_LENGTH:
length, dist = _compress_search_matches_optimized(data_list, pos)
# Treat as direct bytes if it's too short to copy.
if length == 0 or length < _LZ_MIN_LENGTH:
length = 1
# If the node goes to the end of the file, the weight is the cost of the node.
if (pos + length) == len(data_list):
node.len = length
node.dist = dist
node.weight = _compress_get_node_cost(length)
# Otherwise, search for possible matches and determine the one with the best cost.
else:
weight_best = 0xFFFFFFFF # This was originally UINT_MAX, but that isn't a thing here so 32-bit it is!
len_best = 1
while length:
weight_next = nodes[pos + length].weight
weight = _compress_get_node_cost(length) + weight_next
if weight < weight_best:
len_best = length
weight_best = weight
length -= 1
if length != 0 and length < _LZ_MIN_LENGTH:
length = 1
node.len = len_best
node.dist = dist
node.weight = weight_best
# Write the compressed data.
with io.BytesIO() as buffer:
# Write the header data.
buffer.write(b'LZ77\x10') # The LZ type on the Wii is *always* 0x10.
buffer.write(len(data).to_bytes(3, 'little'))
src_pos = 0
while src_pos < len(data):
head = 0
head_pos = buffer.tell()
buffer.write(b'\x00') # Reserve a byte for the chunk head.
i = 0
while i < 8 and src_pos < len(data):
current_node = nodes[src_pos]
length = current_node.len
dist = current_node.dist
# This is a reference node.
if _compress_node_is_ref(current_node):
encoded = (((length - _LZ_MIN_LENGTH) & 0xF) << 12) | ((dist - _LZ_MIN_DISTANCE) & 0xFFF)
buffer.write(encoded.to_bytes(2))
head = (head | (1 << (7 - i))) & 0xFF
# This is a direct copy node.
else:
buffer.write(data[src_pos:src_pos + 1])
src_pos += length
i += 1
pos = buffer.tell()
buffer.seek(head_pos)
buffer.write(head.to_bytes(1))
buffer.seek(pos)
buffer.seek(0)
out_data = buffer.read()
return out_data
def _compress_lz77_greedy(data: bytes) -> bytes:
# Greedy compressor that processes the file start to end and saves the first matches found. Faster than the
# optimized implementation, but creates larger files.
global _LZ_MAX_LENGTH, _LZ_MIN_LENGTH, _LZ_MIN_DISTANCE
with io.BytesIO() as buffer:
# Write the header data.
buffer.write(b'LZ77\x10') # The LZ type on the Wii is *always* 0x10.
buffer.write(len(data).to_bytes(3, 'little'))
src_pos = 0
data_list = list(data)
while src_pos < len(data):
head = 0
head_pos = buffer.tell()
buffer.write(b'\x00') # Reserve a byte for the chunk head.
i = 0
while i < 8 and src_pos < len(data):
length, dist = _compress_search_matches_greedy(data_list, src_pos)
# This is a reference node.
if length >= _LZ_MIN_LENGTH:
encoded = (((length - _LZ_MIN_LENGTH) & 0xF) << 12) | ((dist - _LZ_MIN_DISTANCE) & 0xFFF)
buffer.write(encoded.to_bytes(2))
head = (head | (1 << (7 - i))) & 0xFF
src_pos += length
# This is a direct copy node.
else:
buffer.write(data[src_pos:src_pos + 1])
src_pos += 1
i += 1
pos = buffer.tell()
buffer.seek(head_pos)
buffer.write(head.to_bytes(1))
buffer.seek(pos)
buffer.seek(0)
out_data = buffer.read()
return out_data
def compress_lz77(data: bytes, compression_level: int = 1) -> bytes:
"""
Compresses data using the Wii's LZ77 compression algorithm and returns the compressed result. Supports two
different levels of compression, one based around a "greedy" LZ compression algorithm and the other based around
an optimized LZ compression algorithm. The greedy compressor, level 1, will produce a larger compressed file but
will run noticeably faster than the optimized compressor, which is level 2, especially for larger data.
Parameters
----------
data: bytes
The data to compress.
compression_level: int
The compression level to use, either 1 and 2. Default value is 1.
Returns
-------
bytes
The LZ77-compressed data.
"""
if compression_level == 1:
out_data = _compress_lz77_greedy(data)
elif compression_level == 2:
out_data = _compress_lz77_optimized(data)
else:
raise ValueError(f"Invalid compression level \"{compression_level}\"!\"")
return out_data
def decompress_lz77(lz77_data: bytes) -> bytes:
"""
Decompresses LZ77-compressed data and returns the decompressed result. Supports data both with and without the
magic number 'LZ77' (which may not be present if the data is embedded in something else).
Parameters
----------
lz77_data: bytes
The LZ77-compressed data to decompress.
Returns
-------
bytes
The decompressed data.
"""
with io.BytesIO(lz77_data) as data:
magic = data.read(4)
# Assume if we didn't get the magic number that this data starts without it.
if magic != b'LZ77':
data.seek(0)
# Other compression types are used by Nintendo, but only type 0x10 was used on the Wii.
compression_type = int.from_bytes(data.read(1))
if compression_type != 0x10:
raise ValueError("This data is using an unsupported compression type!")
decompressed_size = int.from_bytes(data.read(3), byteorder='little')
# Use an integer list for storing decompressed data, this is much faster than using (and appending to) a
# bytes object.
out_data = [0] * decompressed_size
pos = 0
while pos < decompressed_size:
flag = int.from_bytes(data.read(1))
# Read bits in the flag from most to least significant.
for x in range(7, -1, -1):
# Avoids a buffer overrun if the final flag isn't fully used.
if pos >= decompressed_size:
break
# Result of 1, this means we're copying bytes from earlier in the data.
if flag & (1 << x):
reference = int.from_bytes(data.read(2))
length = 3 + ((reference >> 12) & 0xF)
offset = pos - (reference & 0xFFF) - 1
for _ in range(length):
out_data[pos] = out_data[offset]
pos += 1
offset += 1
# Avoids a buffer overrun if the copy length would extend past the end of the file.
if pos >= decompressed_size:
break
# Result of 0, use the next byte directly.
else:
out_data[pos] = int.from_bytes(data.read(1))
pos += 1
out_bytes = bytes(out_data)
return out_bytes

View File

@@ -57,7 +57,7 @@ class U8Archive:
imet_header: IMETHeader
The IMET header of the U8 archive, if one exists. Otherwise, an empty IMETHeader object.
"""
def __init__(self):
def __init__(self) -> None:
self.u8_magic = b''
self.u8_node_list: List[_U8Node] = [] # All the nodes in the header of a U8 file.
self.file_name_list: List[str] = []
@@ -68,16 +68,16 @@ class U8Archive:
self.root_node: _U8Node = _U8Node(0, 0, 0, 0)
self.imet_header: IMETHeader = IMETHeader()
def load(self, u8_data: bytes) -> None:
def load(self, u8: bytes) -> None:
"""
Loads raw U8 data into a new U8 object. This allows for extracting the file and updating its contents.
Parameters
----------
u8_data : bytes
u8 : bytes
The data for the U8 file to load.
"""
with io.BytesIO(u8_data) as u8_data:
with io.BytesIO(u8) as u8_data:
# Read the first 4 bytes of the file to ensure that it's a U8 archive.
u8_data.seek(0x0)
self.u8_magic = u8_data.read(4)
@@ -126,7 +126,7 @@ class U8Archive:
# Seek back before the root node so that it gets read with all the rest.
u8_data.seek(u8_data.tell() - 12)
# Iterate over the number of nodes that the root node lists.
for node in range(root_node_size):
for _ in range(root_node_size):
node_type = int.from_bytes(u8_data.read(1))
node_name_offset = int.from_bytes(u8_data.read(3))
node_data_offset = int.from_bytes(u8_data.read(4))
@@ -160,7 +160,7 @@ class U8Archive:
# This is 0 because the header size DOES NOT include the initial 32 bytes describing the file.
header_size = 0
# Add 12 bytes for each node, since that's how many bytes each one is made up of.
for node in range(len(self.u8_node_list)):
for _ in range(len(self.u8_node_list)):
header_size += 12
# Add the number of bytes used for each file/folder name in the string table.
for file_name in self.file_name_list:
@@ -170,13 +170,13 @@ class U8Archive:
# Adjust all nodes to place file data in the same order as the nodes. Why isn't it already like this?
current_data_offset = data_offset
current_name_offset = 0
for node in range(len(self.u8_node_list)):
if self.u8_node_list[node].type == 0:
self.u8_node_list[node].data_offset = _align_value(current_data_offset, 32)
current_data_offset += _align_value(self.u8_node_list[node].size, 32)
for idx in range(len(self.u8_node_list)):
if self.u8_node_list[idx].type == 0:
self.u8_node_list[idx].data_offset = _align_value(current_data_offset, 32)
current_data_offset += _align_value(self.u8_node_list[idx].size, 32)
# Calculate the name offsets, including the extra 1 for the NULL byte at the end of each name.
self.u8_node_list[node].name_offset = current_name_offset
current_name_offset += len(self.file_name_list[node]) + 1
self.u8_node_list[idx].name_offset = current_name_offset
current_name_offset += len(self.file_name_list[idx]) + 1
# Begin joining all the U8 archive data into bytes.
u8_data = b''
# Magic number.
@@ -300,7 +300,7 @@ def _pack_u8_dir(u8_archive: U8Archive, current_path, node_count, parent_node):
return u8_archive, node_count
def pack_u8(input_path, generate_imet=False, imet_titles:List[str]=None) -> bytes:
def pack_u8(input_path, generate_imet=False, imet_titles:List[str] | None = None) -> bytes:
"""
Packs the provided file or folder into a new U8 archive, and returns the raw file data for it.
@@ -369,7 +369,7 @@ class IMETHeader:
md5_hash : bytes
MD5 sum of the entire header, with this field being all zeros during the hashing.
"""
def __init__(self):
def __init__(self) -> None:
self.magic: str = "" # Should always be "IMET"
self.header_size: int = 0 # Always 1536? I assumed this would mean something, but it's just the header length.
self.imet_version: int = 0 # Always 3?
@@ -513,13 +513,15 @@ class IMETHeader:
raise ValueError(f"The specified language is not valid!")
return self.channel_names[target_languages]
# If multiple channel names were requested.
else:
elif type(target_languages) == List:
channel_names = []
for lang in target_languages:
if lang not in self.LocalizedTitles:
raise ValueError(f"The specified language at index {target_languages.index(lang)} is not valid!")
channel_names.append(self.channel_names[lang])
return channel_names
else:
raise TypeError("Target languages must be type int or List[int]!")
def set_channel_names(self, channel_names: Tuple[int, str] | List[Tuple[int, str]]) -> None:
"""
@@ -544,7 +546,7 @@ class IMETHeader:
f"42 characters!")
self.channel_names[channel_names[0]] = channel_names[1]
# If a list of channel names was provided.
else:
elif type(channel_names) == list:
for name in channel_names:
if name[0] not in self.LocalizedTitles:
raise ValueError(f"The target language \"{name[0]}\" for the name at index {channel_names.index(name)} "
@@ -553,3 +555,5 @@ class IMETHeader:
raise ValueError(f"The channel name \"{name[1]}\" at index {channel_names.index(name)} is too long! "
f"Channel names cannot exceed 42 characters!")
self.channel_names[name[0]] = name[1]
else:
raise TypeError("Channel names must be type Tuple[int, str] or List[Tuple[int, str]]!")

65
src/libWiiPy/constants.py Normal file
View File

@@ -0,0 +1,65 @@
# "constants.py" from libWiiPy by NinjaCheetah & Contributors
# https://github.com/NinjaCheetah/libWiiPy
#
# This file defines constant values referenced across the library.
_WII_MENU_VERSIONS = {
"Prelaunch": [0, 1, 2],
"1.0J": 64,
"1.0U": 33,
"1.0E": 34,
"2.0J": 128,
"2.0U": 97,
"2.0E": 130,
"2.1E": 162,
"2.2J": 192,
"2.2U": 193,
"2.2E": 194,
"3.0J": 224,
"3.0U": 225,
"3.0E": 226,
"3.1J": 256,
"3.1U": 257,
"3.1E": 258,
"3.2J": 288,
"3.2U": 289,
"3.2E": 290,
"3.3J": 352,
"3.3U": 353,
"3.3E": 354,
"3.3K": 326,
"3.4J": 384,
"3.4U": 385,
"3.4E": 386,
"3.5K": 390,
"4.0J": 416,
"4.0U": 417,
"4.0E": 418,
"4.1J": 448,
"4.1U": 449,
"4.1E": 450,
"4.1K": 454,
"4.2J": 480,
"4.2U": 481,
"4.2E": 482,
"4.2K": 486,
"4.3J": 512,
"4.3U": 513,
"4.3E": 514,
"4.3K": 518,
"4.3U-Mini": 4609,
"4.3E-Mini": 4610
}
_VWII_MENU_VERSIONS = {
"vWii-1.0.0J": 512,
"vWii-1.0.0U": 513,
"vWii-1.0.0E": 514,
"vWii-4.0.0J": 544,
"vWii-4.0.0U": 545,
"vWii-4.0.0E": 546,
"vWii-5.2.0J": 608,
"vWii-5.2.0U": 609,
"vWii-5.2.0E": 610,
}

View File

@@ -14,16 +14,10 @@ class IMD5Header:
An IMD5 header is always 32 bytes long.
Attributes
----------
magic : str
Magic number for the header, should be "IMD5".
file_size : int
The size of the file this header precedes.
zeros : int
8 bytes of zero padding.
md5_hash : bytes
The MD5 hash of the file this header precedes.
:ivar magic: Magic number for the header, should be "IMD5".
:ivar file_size: The size of the file this header precedes.
:ivar zeros: 8 bytes of zero padding.
:ivar md5_hash: The MD5 hash of the file this header precedes.
"""
magic: str # Should always be "IMD5"
file_size: int

View File

@@ -7,7 +7,8 @@ import os
import pathlib
import shutil
from dataclasses import dataclass as _dataclass
from typing import List
from typing import Callable, List
from ..title.ticket import Ticket
from ..title.title import Title
from ..title.tmd import TMD
@@ -32,9 +33,9 @@ class EmuNAND:
emunand_root : pathlib.Path
The path to the EmuNAND root directory.
"""
def __init__(self, emunand_root: str | pathlib.Path, callback: callable = None):
def __init__(self, emunand_root: str | pathlib.Path, callback: Callable | None = None):
self.emunand_root = pathlib.Path(emunand_root)
self.log = callback if callback is not None else None
self.log = callback if callback is not None else lambda x: None
self.import_dir = self.emunand_root.joinpath("import")
self.meta_dir = self.emunand_root.joinpath("meta")
@@ -70,12 +71,14 @@ class EmuNAND:
skip_hash : bool, optional
Skip the hash check and install the title regardless of its hashes. Defaults to false.
"""
self.log(f"[PROGRESS] Starting install of title with Title ID {title.tmd.title_id}...")
# Save the upper and lower portions of the Title ID, because these are used as target install directories.
tid_upper = title.tmd.title_id[:8]
tid_lower = title.tmd.title_id[8:]
# Tickets are installed as <tid_lower>.tik in /ticket/<tid_upper>/
ticket_dir = self.ticket_dir.joinpath(tid_upper)
self.log(f"[PROGRESS] Installing ticket to \"{ticket_dir}\"...")
ticket_dir.mkdir(exist_ok=True)
ticket_dir.joinpath(f"{tid_lower}.tik").write_bytes(title.ticket.dump())
@@ -86,19 +89,25 @@ class EmuNAND:
title_dir = title_dir.joinpath(tid_lower)
title_dir.mkdir(exist_ok=True)
content_dir = title_dir.joinpath("content")
self.log(f"[PROGRESS] Installing TMD to \"{content_dir}\"...")
if content_dir.exists():
shutil.rmtree(content_dir) # Clear the content directory so old contents aren't left behind.
content_dir.mkdir(exist_ok=True)
content_dir.joinpath("title.tmd").write_bytes(title.tmd.dump())
self.log(f"[PROGRESS] Installing content to \"{content_dir}\"...")
if skip_hash:
self.log("[WARN] Not checking content hashes! Content validity will not be verified.")
for content_file in range(0, title.tmd.num_contents):
if title.tmd.content_records[content_file].content_type == 1:
content_file_name = f"{title.tmd.content_records[content_file].content_id:08X}".lower()
self.log(f"[PROGRESS] Installing content \"{content_file_name}.app\" to \"{content_dir}\"... ")
content_dir.joinpath(f"{content_file_name}.app").write_bytes(
title.get_content_by_index(content_file, skip_hash=skip_hash))
title_dir.joinpath("data").mkdir(exist_ok=True) # Empty directory used for save data for the title.
# Shared contents need to be installed to /shared1/, with incremental names determined by /shared1/content.map.
content_map_path = self.shared1_dir.joinpath("content.map")
self.log(f"[PROGRESS] Installing shared content to \"{self.shared1_dir}\"...")
content_map = _SharedContentMap()
existing_hashes = []
if content_map_path.exists():
@@ -108,7 +117,10 @@ class EmuNAND:
for content_file in range(0, title.tmd.num_contents):
if title.tmd.content_records[content_file].content_type == 32769:
if title.tmd.content_records[content_file].content_hash not in existing_hashes:
self.log(f"[PROGRESS] Adding shared content hash to content.map...")
content_file_name = content_map.add_content(title.tmd.content_records[content_file].content_hash)
self.log(f"[PROGRESS] Installing shared content \"{content_file_name}.app\" to "
f"\"{self.shared1_dir}\"...")
self.shared1_dir.joinpath(f"{content_file_name}.app").write_bytes(
title.get_content_by_index(content_file, skip_hash=skip_hash))
self.shared1_dir.joinpath("content.map").write_bytes(content_map.dump())
@@ -120,6 +132,7 @@ class EmuNAND:
meta_dir = self.meta_dir.joinpath(tid_upper)
meta_dir.mkdir(exist_ok=True)
meta_dir = meta_dir.joinpath(tid_lower)
self.log(f"[PROGRESS] Installing meta data to \"{meta_dir}\"...")
meta_dir.mkdir(exist_ok=True)
meta_dir.joinpath("title.met").write_bytes(title.wad.get_meta_data())
@@ -127,7 +140,25 @@ class EmuNAND:
uid_sys_path = self.sys_dir.joinpath("uid.sys")
uid_sys = _UidSys()
if not uid_sys_path.exists():
self.log("[WARN] uid.sys does not exist! Creating it with the default entry.")
uid_sys.create()
else:
uid_sys.load(uid_sys_path.read_bytes())
self.log("[PROGRESS] Adding title to uid.sys and assigning a new UID...")
uid_sys.add(title.tmd.title_id)
uid_sys_path.write_bytes(uid_sys.dump())
# Check for a cert.sys and initialize it using the certs in the WAD if it doesn't exist.
cert_sys_path = self.sys_dir.joinpath("cert.sys")
if not cert_sys_path.exists():
self.log("[WARN] cert.sys does not exist! Creating it using certs from the installed title...")
cert_sys_data = b''
cert_sys_data += title.cert_chain.ticket_cert.dump()
cert_sys_data += title.cert_chain.ca_cert.dump()
cert_sys_data += title.cert_chain.tmd_cert.dump()
cert_sys_path.write_bytes(cert_sys_data)
self.log("[PROGRESS] Completed title installation.")
def uninstall_title(self, tid: str) -> None:
"""
@@ -170,12 +201,8 @@ class EmuNAND:
An InstalledTitles object that is used to track a title type and any titles that belong to that type that are
installed to an EmuNAND.
Attributes
----------
type : str
The type (Title ID high) of the installed titles.
titles : List[str]
The Title ID low of each installed title.
:ivar type: The type (Title ID high) of the installed titles.
:ivar titles: The Title ID low of each installed title.
"""
type: str
titles: List[str]
@@ -199,8 +226,8 @@ class EmuNAND:
valid_lows = []
for low in tid_lows:
if low.joinpath("content", "title.tmd").exists():
valid_lows.append(low.name)
installed_titles.append(self.InstalledTitles(high.name, valid_lows))
valid_lows.append(low.name.upper())
installed_titles.append(self.InstalledTitles(high.name.upper(), valid_lows))
return installed_titles
def get_title_tmd(self, tid: str) -> TMD:

View File

@@ -4,10 +4,11 @@
# See https://wiibrew.org/wiki//title/00000001/00000002/data/setting.txt for information about setting.txt.
import io
from typing import List
from ..shared import _pad_bytes
_key = 0x73B5DBFA
_KEY = 0x73B5DBFA
class SettingTxt:
"""
@@ -32,7 +33,7 @@ class SettingTxt:
game : str
Another region code, possibly set by the hidden region select channel.
"""
def __init__(self):
def __init__(self) -> None:
self.area: str = ""
self.model: str = ""
self.dvd: int = 0
@@ -52,17 +53,17 @@ class SettingTxt:
The data of an encrypted setting.txt file.
"""
with io.BytesIO(setting_txt) as setting_data:
global _key # I still don't actually know what *kind* of encryption this is.
setting_txt_dec: [int] = []
global _KEY # I still don't actually know what *kind* of encryption this is.
setting_txt_dec: List[int] = []
for i in range(0, 256):
setting_txt_dec.append(int.from_bytes(setting_data.read(1)) ^ (_key & 0xff))
_key = (_key << 1) | (_key >> 31)
setting_txt_dec = bytes(setting_txt_dec)
setting_txt_dec.append(int.from_bytes(setting_data.read(1)) ^ (_KEY & 0xff))
_KEY = (_KEY << 1) | (_KEY >> 31)
setting_txt_bytes = bytes(setting_txt_dec)
try:
setting_str = setting_txt_dec.decode('utf-8')
setting_str = setting_txt_bytes.decode('utf-8')
except UnicodeDecodeError:
last_newline_pos = setting_txt_dec.rfind(b'\n') # This makes sure we don't try to decode any garbage data.
setting_str = setting_txt_dec[:last_newline_pos + 1].decode('utf-8')
last_newline_pos = setting_txt_bytes.rfind(b'\n') # This makes sure we don't try to decode any garbage data.
setting_str = setting_txt_bytes[:last_newline_pos + 1].decode('utf-8')
self.load_decrypted(setting_str)
def load_decrypted(self, setting_txt: str) -> None:
@@ -102,15 +103,15 @@ class SettingTxt:
"""
setting_str = self.dump_decrypted()
setting_txt_dec = setting_str.encode()
global _key
global _KEY
# This could probably be made more efficient somehow.
setting_txt_enc: [int] = []
setting_txt_enc: List[int] = []
with io.BytesIO(setting_txt_dec) as setting_data:
for i in range(0, len(setting_txt_dec)):
setting_txt_enc.append(int.from_bytes(setting_data.read(1)) ^ (_key & 0xff))
_key = (_key << 1) | (_key >> 31)
setting_txt_enc = _pad_bytes(bytes(setting_txt_enc), 256)
return setting_txt_enc
setting_txt_enc.append(int.from_bytes(setting_data.read(1)) ^ (_KEY & 0xff))
_KEY = (_KEY << 1) | (_KEY >> 31)
setting_txt_bytes = _pad_bytes(bytes(setting_txt_enc), 256)
return setting_txt_bytes
def dump_decrypted(self) -> str:
"""

View File

@@ -36,7 +36,7 @@ class UidSys:
The entries stored in the uid.sys file.
"""
def __init__(self):
def __init__(self) -> None:
self.uid_entries: List[_UidSysEntry] = []
def load(self, uid_sys: bytes) -> None:
@@ -77,7 +77,8 @@ class UidSys:
def add(self, title_id: str | bytes) -> int:
"""
Adds a new Title ID to the uid.sys file and returns the UID assigned to that title.
Adds a new Title ID to the uid.sys file and returns the UID assigned to that title. The new entry will only
be added if the provided Title ID doesn't already have an assigned UID.
Parameters
----------
@@ -90,11 +91,8 @@ class UidSys:
The UID assigned to the new Title ID.
"""
if type(title_id) is bytes:
# This catches the format b'0000000100000002'
if len(title_id) == 16:
title_id_converted = title_id.encode()
# This catches the format b'\x00\x00\x00\x01\x00\x00\x00\x02'
elif len(title_id) == 8:
if len(title_id) == 8:
title_id_converted = binascii.hexlify(title_id).decode()
# If it isn't one of those lengths, it cannot possibly be valid, so reject it.
else:
@@ -106,6 +104,11 @@ class UidSys:
title_id_converted = title_id
else:
raise TypeError("Title ID type is not valid! It must be either type str or bytes.")
# Ensure this TID hasn't already been assigned a UID. If it has, just exit early and return the UID.
if self.uid_entries.count != 0:
for entry in self.uid_entries:
if entry.title_id == title_id_converted:
return entry.uid
# Generate the new UID by incrementing the current highest UID by 1.
try:
new_uid = self.uid_entries[-1].uid + 1

0
src/libWiiPy/py.typed Normal file
View File

View File

@@ -47,69 +47,3 @@ def _pad_bytes(data, alignment=64) -> bytes:
while (len(data) % alignment) != 0:
data += b'\x00'
return data
def _bitmask(x: int) -> int:
return 1 << x
_wii_menu_versions = {
"Prelaunch": [0, 1, 2],
"1.0J": 64,
"1.0U": 33,
"1.0E": 34,
"2.0J": 128,
"2.0U": 97,
"2.0E": 130,
"2.1E": 162,
"2.2J": 192,
"2.2U": 193,
"2.2E": 194,
"3.0J": 224,
"3.0U": 225,
"3.0E": 226,
"3.1J": 256,
"3.1U": 257,
"3.1E": 258,
"3.2J": 288,
"3.2U": 289,
"3.2E": 290,
"3.3J": 352,
"3.3U": 353,
"3.3E": 354,
"3.3K": 326,
"3.4J": 384,
"3.4U": 385,
"3.4E": 386,
"3.5K": 390,
"4.0J": 416,
"4.0U": 417,
"4.0E": 418,
"4.1J": 448,
"4.1U": 449,
"4.1E": 450,
"4.1K": 454,
"4.2J": 480,
"4.2U": 481,
"4.2E": 482,
"4.2K": 486,
"4.3J": 512,
"4.3U": 513,
"4.3E": 514,
"4.3K": 518,
"4.3U-Mini": 4609,
"4.3E-Mini": 4610
}
_vwii_menu_versions = {
"vWii-1.0.0J": 512,
"vWii-1.0.0U": 513,
"vWii-1.0.0E": 514,
"vWii-4.0.0J": 544,
"vWii-4.0.0U": 545,
"vWii-4.0.0E": 546,
"vWii-5.2.0J": 608,
"vWii-5.2.0U": 609,
"vWii-5.2.0E": 610,
}

View File

@@ -9,5 +9,6 @@ from .nus import *
from .ticket import *
from .title import *
from .tmd import *
from .util import *
from .types import *
from .versions import *
from .wad import *

View File

@@ -5,33 +5,47 @@
import io
from enum import IntEnum as _IntEnum
from ..shared import _align_value, _pad_bytes
from .ticket import Ticket
from .tmd import TMD
from Crypto.Hash import SHA1
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from ..shared import _align_value, _pad_bytes
from .ticket import Ticket
from .tmd import TMD
class CertificateType(_IntEnum):
"""
The type of a certificate.
"""
RSA_4096 = 0x00010000
RSA_2048 = 0x00010001
ECC = 0x00010002
class CertificateSignatureLength(_IntEnum):
"""
The length of a certificate's signature.
"""
RSA_4096 = 0x200
RSA_2048 = 0x100
ECC = 0x3C
class CertificateKeyType(_IntEnum):
"""
The type of key contained in a certificate.
"""
RSA_4096 = 0x00000000
RSA_2048 = 0x00000001
ECC = 0x00000002
class CertificateKeyLength(_IntEnum):
"""
The length of the key contained in a certificate.
"""
RSA_4096 = 0x200
RSA_2048 = 0x100
ECC = 0x3C
@@ -43,14 +57,28 @@ class Certificate:
Attributes
----------
something : bool
I'm a placeholder attribute for later.
type: CertificateType
The type of the certificate, either RSA-2048, RSA-4096, or ECC.
signature: bytes
The signature data of the certificate.
issuer: str
The certificate that issued this certificate.
pub_key_type: CertificateKeyType
The type of public key contained in the certificate, either RSA-2048, RSA-4096, or ECC.
child_name: str
The name of this certificate.
pub_key_id: int
The ID of this certificate's public key.
pub_key_modulus: int
The modulus of this certificate's public key. Combined with the exponent to get the full key.
pub_key_exponent: int
The exponent of this certificate's public key. Combined with the modulus to get the full key.
"""
def __init__(self):
self.type: CertificateType | None = None
def __init__(self) -> None:
self.type: CertificateType = CertificateType.RSA_4096
self.signature: bytes = b''
self.issuer: str = ""
self.pub_key_type: CertificateKeyType | None = None
self.pub_key_type: CertificateKeyType = CertificateKeyType.RSA_4096
self.child_name: str = ""
self.pub_key_id: int = 0
self.pub_key_modulus: int = 0
@@ -137,7 +165,7 @@ class CertificateChain:
ticket_cert: Certificate
The XS (Ticket) certificate from the chain.
"""
def __init__(self):
def __init__(self) -> None:
self.ca_cert: Certificate = Certificate()
self.tmd_cert: Certificate = Certificate()
self.ticket_cert: Certificate = Certificate()
@@ -198,7 +226,8 @@ class CertificateChain:
def verify_ca_cert(ca_cert: Certificate) -> bool:
"""
Verify a Wii CA certificate using the root public key.
Verify a Wii CA certificate using the root public key. The retail or development root key will be automatically
selected based off of the name of the CA certificate provided.
Parameters
----------
@@ -210,23 +239,44 @@ def verify_ca_cert(ca_cert: Certificate) -> bool:
bool
Whether the certificate is valid or not.
"""
root_key_modulus = (b'\xf8$lX\xba\xe7P\x03\x01\xfb\xb7\xc2\xeb\xe0\x01\x05q\xda\x92#x\xf0QN\xc0\x03\x1d\xd0\xd2\x1e'
b'\xd3\xd0~\xfc\x85 i\xb5\xde\x9b\xb9Q\xa8\xbc\x90\xa2D\x92m7\x92\x95\xae\x946\xaa\xa6\xa3\x02Q'
b'\x0c{\x1d\xed\xd5\xfb \x86\x9d\x7f0\x16\xf6\xbee\xd3\x83\xa1m\xb32\x1b\x955\x18\x90\xb1p\x02'
b'\x93~\xe1\x93\xf5~\x99\xa2GN\x9d8$\xc7\xae\xe3\x85A\xf5g\xe7Q\x8cz\x0e8\xe7\xeb\xafA\x19\x1b'
b'\xcf\xf1{B\xa6\xb4\xed\xe6\xce\x8d\xe71\x8f\x7fR\x04\xb3\x99\x0e"gE\xaf\xd4\x85\xb2D\x93\x00'
b'\x8b\x08\xc7\xf6\xb7\xe5k\x02\xb3\xe8\xfe\x0c\x9d\x85\x9c\xb8\xb6\x82#\xb8\xab\'\xee_e8\x07'
b'\x8b-\xb9\x1e*\x15>\x85\x81\x80r\xa2;m\xd92\x81\x05Oo\xb0\xf6\xf5\xad(>\xca\x0bz\xf3TU\xe0='
b'\xa7\xb6\x83&\xf3\xec\x83J\xf3\x14\x04\x8a\xc6\xdf \xd2\x85\x08g<\xabb\xa2\xc7\xbc\x13\x1aS>'
b'\x0bf\x80k\x1c0fK7#1\xbd\xc4\xb0\xca\xd8\xd1\x1e\xe7\xbb\xd9(UH\xaa\xec\x1ff\xe8!\xb3\xc8'
b'\xa0Gi\x00\xc5\xe6\x88\xe8\x0c\xce<a\xd6\x9c\xbb\xa17\xc6`Ozr\xdd\x8c{>=Q)\r\xaajY{\x08\x1f'
b'\x9d63\xa3Fz5a\t\xac\xa7\xdd}./\xb2\xc1\xae\xb8\xe2\x0fH\x92\xd8\xb9\xf8\xb4oN<\x11\xf4\xf4}'
b'\x8bu}\xfe\xfe\xa3\x89\x9c3Y\\^\xfd\xeb\xcb\xab\xe8A>:\x9a\x80<i5n\xb2\xb2\xad\\\xc4\xc8XE^'
b'\xf5\xf7\xb3\x06D\xb4|d\x06\x8c\xdf\x80\x9fv\x02Z-\xb4F\xe0=|\xf6/4\xe7\x02E{\x02\xa4\xcf]'
b'\x9d\xd5<\xa5:|\xa6)x\x8cg\xca\x08\xbf\xec\xcaC\xa9W\xad\x16\xc9N\x1c\xd8u\xca\x10}\xce~\x01'
b'\x18\xf0\xdfk\xfe\xe5\x1d\xdb\xd9\x91\xc2n`\xcdHX\xaaY,\x82\x00u\xf2\x9fRl\x91|o\xe5@>\xa7'
b'\xd4\xa5\x0c\xec;s\x84\xde\x88n\x82\xd2\xebMNB\xb5\xf2\xb1I\xa8\x1e\xa7\xceqD\xdc)\x94\xcf'
b'\xc4N\x1f\x91\xcb\xd4\x95')
if ca_cert.issuer != "Root" or ca_cert.child_name.find("CA") == -1:
raise ValueError("The provided certificate is not a CA certificate!")
if ca_cert.child_name == "CA00000001":
root_key_modulus = \
(b'\xf8$lX\xba\xe7P\x03\x01\xfb\xb7\xc2\xeb\xe0\x01\x05q\xda\x92#x\xf0QN\xc0\x03\x1d\xd0\xd2\x1e\xd3\xd0~'
b'\xfc\x85 i\xb5\xde\x9b\xb9Q\xa8\xbc\x90\xa2D\x92m7\x92\x95\xae\x946\xaa\xa6\xa3\x02Q\x0c{\x1d\xed\xd5'
b'\xfb \x86\x9d\x7f0\x16\xf6\xbee\xd3\x83\xa1m\xb32\x1b\x955\x18\x90\xb1p\x02\x93~\xe1\x93\xf5~\x99\xa2GN'
b'\x9d8$\xc7\xae\xe3\x85A\xf5g\xe7Q\x8cz\x0e8\xe7\xeb\xafA\x19\x1b\xcf\xf1{B\xa6\xb4\xed\xe6\xce\x8d\xe71'
b'\x8f\x7fR\x04\xb3\x99\x0e"gE\xaf\xd4\x85\xb2D\x93\x00\x8b\x08\xc7\xf6\xb7\xe5k\x02\xb3\xe8\xfe\x0c\x9d'
b'\x85\x9c\xb8\xb6\x82#\xb8\xab\'\xee_e8\x07\x8b-\xb9\x1e*\x15>\x85\x81\x80r\xa2;m\xd92\x81\x05Oo\xb0\xf6'
b'\xf5\xad(>\xca\x0bz\xf3TU\xe0=\xa7\xb6\x83&\xf3\xec\x83J\xf3\x14\x04\x8a\xc6\xdf \xd2\x85\x08g<\xabb\xa2'
b'\xc7\xbc\x13\x1aS>\x0bf\x80k\x1c0fK7#1\xbd\xc4\xb0\xca\xd8\xd1\x1e\xe7\xbb\xd9(UH\xaa\xec\x1ff\xe8!\xb3'
b'\xc8\xa0Gi\x00\xc5\xe6\x88\xe8\x0c\xce<a\xd6\x9c\xbb\xa17\xc6`Ozr\xdd\x8c{>=Q)\r\xaajY{\x08\x1f\x9d63'
b'\xa3Fz5a\t\xac\xa7\xdd}./\xb2\xc1\xae\xb8\xe2\x0fH\x92\xd8\xb9\xf8\xb4oN<\x11\xf4\xf4}\x8bu}\xfe\xfe\xa3'
b'\x89\x9c3Y\\^\xfd\xeb\xcb\xab\xe8A>:\x9a\x80<i5n\xb2\xb2\xad\\\xc4\xc8XE^\xf5\xf7\xb3\x06D\xb4|d\x06\x8c'
b'\xdf\x80\x9fv\x02Z-\xb4F\xe0=|\xf6/4\xe7\x02E{\x02\xa4\xcf]\x9d\xd5<\xa5:|\xa6)x\x8cg\xca\x08\xbf\xec'
b'\xcaC\xa9W\xad\x16\xc9N\x1c\xd8u\xca\x10}\xce~\x01\x18\xf0\xdfk\xfe\xe5\x1d\xdb\xd9\x91\xc2n`\xcdHX\xaa'
b'Y,\x82\x00u\xf2\x9fRl\x91|o\xe5@>\xa7\xd4\xa5\x0c\xec;s\x84\xde\x88n\x82\xd2\xebMNB\xb5\xf2\xb1I\xa8\x1e'
b'\xa7\xceqD\xdc)\x94\xcf\xc4N\x1f\x91\xcb\xd4\x95')
elif ca_cert.child_name == "CA00000002":
root_key_modulus = \
(b'\x00\xd0\x1f\xe1\x00\xd45V\xb2KV\xda\xe9q\xb5\xa5\xd3\x84\xb90\x03\xbe\x1b\xbf(\xa20[\x06\x06EF}[\x02Q'
b'\xd2V\x1a\'O\x9e\x9f\x9c\xecdaP\xab=*\xe36hf\xac\xa4\xba\xe8\x1a\xe3\xd7\x9a\xa6\xb0J\x8b\xcb\xa7\xe6'
b'\xfbd\x89E\xeb\xdf\xdb\x85\xba\t\x1f\xd7\xd1\x14\xb5\xa3\xa7\x80\xe3\xa2.n\xcd\x87\xb5\xa4\xc6\xf9\x10'
b'\xe4\x03"\x08\x81K\x0c\xee\xa1\xa1}\xf79i_a~\xf65(\xdb\x94\x967\xa0V\x03\x7f{2A8\x95\xc0\xa8\xf1\x98.'
b'\x15e\xe3\x8e\xed\xc2.Y\x0e\xe2g{\x86\t\xf4\x8c.0?\xbc@\\\xac\x18\x04/\x82 \x84\xe4\x93h\x03\xda\x7fA4'
b'\x92HV+\x8e\xe1/x\xf8\x03$c0\xbc{\xe7\xeerJ\xf4X\xa4r\xe7\xabF\xa1\xa7\xc1\x0c/\x18\xfa\x07\xc3\xdd\xd8'
b'\x98\x06\xa1\x1c\x9c\xc10\xb2G\xa3<\x8dG\xdeg\xf2\x9eUw\xb1\x1cCI=[\xbav4\xa7\xe4\xe7\x151\xb7\xdfY\x81'
b'\xfe$\xa1\x14UL\xbd\x8f\x00\\\xe1\xdb5\x08\\\xcf\xc7x\x06\xb6\xde%@h\xa2l\xb5I-E\x80C\x8f\xe1\xe5\xa9'
b'\xedu\xc5\xedE\x1d\xcex\x949\xcc\xc3\xba(\xa21*\x1b\x87\x19\xef\x0fs\xb7\x13\x95\x0c\x02Y\x1atb\xa6\x07'
b'\xf3|\n\xa7\xa1\x8f\xa9C\xa3mu*_A\x92\xf0\x13a\x00\xaa\x9c\xb4\x1b\xbe\x14\xbe\xb1\xf9\xfci/\xdf\xa0\x94'
b'F\xdeZ\x9d\xde,\xa5\xf6\x8c\x1c\x0c!B\x92\x87\xcb-\xaa\xa3\xd2cu/s\xe0\x9f\xafDy\xd2\x81t)\xf6\x98\x00'
b'\xaf\xdekY-\xc1\x98\x82\xbd\xf5\x81\xcc\xab\xf2\xcb\x91\x02\x9e\xf3\\L\xfd\xbb\xffI\xc1\xfa\x1b/\xe3\x1d'
b'\xe7\xa5`\xec\xb4~\xbc\xfe2B[\x95o\x81\xb6\x99\x17H~;x\x91Q\xdb.x\xb1\xfd.\xbe~bk>\xa1e\xb4\xfb\x00\xcc'
b'\xb7Q\xafPs)\xc4\xa3\x93\x9e\xa6\xdd\x9cP\xa0\xe78k\x01EykA\xafa\xf7\x85U\x94O;\xc2-\xc3\xbd\r\x00\xf8y'
b'\x8aB\xb1\xaa\xa0\x83 e\x9a\xc79Z\xb4\xf3)')
else:
raise ValueError("The provided CA certificate is not valid!")
root_key_exponent = 0x00010001
cert_hash = SHA1.new(ca_cert.dump()[576:])
public_key = RSA.construct((int.from_bytes(root_key_modulus), root_key_exponent))
@@ -253,6 +303,12 @@ def verify_cert_sig(ca_cert: Certificate, target_cert: Certificate) -> bool:
bool
Whether the certificate's signature is valid or not.
"""
if ca_cert.issuer != "Root" or ca_cert.child_name.find("CA") == -1:
raise ValueError("The provided certificate is not a CA certificate!")
# The issuer of the TMD/Ticket certs is Root-CA0000000X, so prepend "Root-" to the CA cert child name. If these
# don't match, then there's probably a mismatch between retail and development certs.
if f"Root-{ca_cert.child_name}" != target_cert.issuer:
raise ValueError("The certificate you are trying to verify does not match the provided CA certificate!")
cert_hash = SHA1.new(target_cert.dump()[320:])
public_key = RSA.construct((ca_cert.pub_key_modulus, ca_cert.pub_key_exponent))
try:
@@ -278,6 +334,10 @@ def verify_tmd_sig(tmd_cert: Certificate, tmd: TMD) -> bool:
bool
Whether the TMD's signature is valid or not.
"""
if tmd_cert.issuer.find("Root-CA") == -1 or tmd_cert.child_name.find("CP") == -1:
raise ValueError("The provided TMD certificate is not valid!")
if f"{tmd_cert.issuer}-{tmd_cert.child_name}" != tmd.signature_issuer:
raise ValueError("The signature you are trying to verify was not created with the provided TMD certificate!")
tmd_hash = SHA1.new(tmd.dump()[320:])
public_key = RSA.construct((tmd_cert.pub_key_modulus, tmd_cert.pub_key_exponent))
try:
@@ -303,6 +363,10 @@ def verify_ticket_sig(ticket_cert: Certificate, ticket: Ticket) -> bool:
bool
Whether the Ticket's signature is valid or not.
"""
if ticket_cert.issuer.find("Root-CA") == -1 or ticket_cert.child_name.find("XS") == -1:
raise ValueError("The provided Ticket certificate is not valid!")
if f"{ticket_cert.issuer}-{ticket_cert.child_name}" != ticket.signature_issuer:
raise ValueError("The signature you are trying to verify was not created with the provided Ticket certificate!")
ticket_hash = SHA1.new(ticket.dump()[320:])
public_key = RSA.construct((ticket_cert.pub_key_modulus, ticket_cert.pub_key_exponent))
try:

View File

@@ -33,13 +33,12 @@ def get_common_key(common_key_index, dev=False) -> bytes:
match common_key_index:
case 0:
if dev:
common_key_bin = binascii.unhexlify(development_key)
return binascii.unhexlify(development_key)
else:
common_key_bin = binascii.unhexlify(common_key)
return binascii.unhexlify(common_key)
case 1:
common_key_bin = binascii.unhexlify(korean_key)
return binascii.unhexlify(korean_key)
case 2:
common_key_bin = binascii.unhexlify(vwii_key)
return binascii.unhexlify(vwii_key)
case _:
common_key_bin = binascii.unhexlify(common_key)
return common_key_bin
return binascii.unhexlify(common_key)

View File

@@ -9,7 +9,7 @@ import hashlib
from typing import List
from dataclasses import dataclass as _dataclass
from enum import IntEnum as _IntEnum
from ..types import _ContentRecord
from .types import ContentRecord
from ..shared import _pad_bytes, _align_value
from .crypto import decrypt_content, encrypt_content
@@ -28,20 +28,20 @@ class ContentRegion:
Attributes
----------
content_records : List[_ContentRecord]
content_records : List[ContentRecord]
The content records for the content stored in the region.
num_contents : int
The total number of contents stored in the region.
"""
def __init__(self):
self.content_records: List[_ContentRecord] = []
def __init__(self) -> None:
self.content_records: List[ContentRecord] = []
self.content_region_size: int = 0 # Size of the content region.
self.num_contents: int = 0 # Number of contents in the content region.
self.content_start_offsets: List[int] = [0] # The start offsets of each content in the content region.
self.content_list: List[bytes] = []
def load(self, content_region: bytes, content_records: List[_ContentRecord]) -> None:
def load(self, content_region: bytes, content_records: List[ContentRecord]) -> None:
"""
Loads the raw content region and builds a list of all the contents.
@@ -49,7 +49,7 @@ class ContentRegion:
----------
content_region : bytes
The raw data for the content region being loaded.
content_records : list[_ContentRecord]
content_records : list[ContentRecord]
A list of ContentRecord objects detailing all contents contained in the region.
"""
self.content_records = content_records
@@ -66,16 +66,16 @@ class ContentRegion:
start_offset += 64 - (content.content_size % 64)
self.content_start_offsets.append(start_offset)
# Build a list of all the encrypted content data.
for content in range(self.num_contents):
for idx in range(self.num_contents):
# Seek to the start of the content based on the list of offsets.
content_region_data.seek(self.content_start_offsets[content])
content_region_data.seek(self.content_start_offsets[idx])
# Calculate the number of bytes we need to read by adding bytes up the nearest multiple of 16 if needed.
bytes_to_read = self.content_records[content].content_size
if (bytes_to_read % 16) != 0:
bytes_to_read += 16 - (bytes_to_read % 16)
content_size = self.content_records[idx].content_size
if (content_size % 16) != 0:
content_size += 16 - (content_size % 16)
# Read the file based on the size of the content in the associated record, then append that data to
# the list of content.
content_enc = content_region_data.read(bytes_to_read)
content_enc = content_region_data.read(content_size)
self.content_list.append(content_enc)
def dump(self) -> tuple[bytes, int]:
@@ -303,7 +303,7 @@ class ContentRegion:
raise ValueError("Content with an index of " + str(index) + " already exists!")
# If we're good, then append all the data and create a new ContentRecord().
self.content_list.append(enc_content)
self.content_records.append(_ContentRecord(cid, index, content_type, content_size, content_hash))
self.content_records.append(ContentRecord(cid, index, content_type, content_size, content_hash))
self.num_contents += 1
def add_content(self, dec_content: bytes, cid: int, content_type: int, title_key: bytes) -> None:
@@ -336,8 +336,8 @@ class ContentRegion:
enc_content = encrypt_content(dec_content, title_key, index)
self.add_enc_content(enc_content, cid, index, content_type, content_size, content_hash)
def set_enc_content(self, enc_content: bytes, index: int, content_size: int, content_hash: bytes, cid: int = None,
content_type: int = None) -> None:
def set_enc_content(self, enc_content: bytes, index: int, content_size: int, content_hash: bytes,
cid: int | None = None, content_type: int | None = None) -> None:
"""
Sets the content at the provided content index to the provided new encrypted content. The provided hash and
content size are set in the corresponding content record. A new Content ID or content type can also be
@@ -373,8 +373,8 @@ class ContentRegion:
self.content_list.append(b'')
self.content_list[index] = enc_content
def set_content(self, dec_content: bytes, index: int, title_key: bytes, cid: int = None,
content_type: int = None) -> None:
def set_content(self, dec_content: bytes, index: int, title_key: bytes, cid: int | None = None,
content_type: int | None = None) -> None:
"""
Sets the content at the provided content index to the provided new decrypted content. The hash and content size
of this content will be generated and then set in the corresponding content record. A new Content ID or content
@@ -525,7 +525,7 @@ class SharedContentMap:
The shared content records stored in content.map.
"""
def __init__(self):
def __init__(self) -> None:
self.shared_records: List[_SharedContentRecord] = []
def load(self, content_map: bytes) -> None:

View File

@@ -10,7 +10,6 @@ from Crypto.Cipher import AES as _AES
def _convert_tid_to_iv(title_id: str | bytes) -> bytes:
# Converts a Title ID in various formats into the format required to act as an IV. Private function used by other
# crypto functions.
title_key_iv = b''
if type(title_id) is bytes:
# This catches the format b'0000000100000002'
if len(title_id) == 16:

View File

@@ -20,7 +20,7 @@ class IOSPatcher:
dip_module_index : int
The content index that DIP resides in and where DIP patches are applied. -1 if DIP patches are not applied.
"""
def __init__(self):
def __init__(self) -> None:
self.title: Title = Title()
self.es_module_index: int = -1
self.dip_module_index: int = -1

View File

@@ -4,9 +4,9 @@
# See https://wiibrew.org/wiki/NUS for details about the NUS
import requests
import hashlib
from typing import List
from urllib.parse import urlparse as _urlparse
#import hashlib
from typing import Any, List, Protocol
#from urllib.parse import urlparse as _urlparse
from .title import Title
from .tmd import TMD
from .ticket import Ticket
@@ -14,13 +14,36 @@ from .ticket import Ticket
_nus_endpoint = ["http://nus.cdn.shop.wii.com/ccs/download/", "http://ccs.cdn.wup.shop.nintendo.net/ccs/download/"]
def download_title(title_id: str, title_version: int = None, wiiu_endpoint: bool = False,
endpoint_override: str = None) -> Title:
class DownloadCallback(Protocol):
"""
The format of a callable passed to a NUS download function.
"""
def __call__(self, done: int, total: int) -> Any:
"""
This function will be called with the current number of bytes downloaded and the total size of the file being
downloaded.
Parameters
----------
done : int
The number of bytes already downloaded.
total : int
The total size of the file being downloaded.
"""
...
def download_title(title_id: str, title_version: int | None = None, wiiu_endpoint: bool = False,
endpoint_override: str | None = None, progress: DownloadCallback = lambda done, total: None) -> Title:
"""
Download an entire title and all of its contents, then load the downloaded components into a Title object for
further use. This method is NOT recommended for general use, as it has absolutely no verbosity. It is instead
further use. This method is NOT recommended for general use, as it has extremely limited verbosity. It is instead
recommended to call the individual download methods instead to provide more flexibility and output.
Be aware that you will receive fairly vague feedback from this function if you attach a progress callback. The
callback will be connected to each of the individual functions called by this function, but there will be no
indication of which function is currently running, just the progress of its download.
Parameters
----------
title_id : str
@@ -32,27 +55,34 @@ def download_title(title_id: str, title_version: int = None, wiiu_endpoint: bool
endpoint_override: str, optional
A custom endpoint URL to use instead of the standard Wii or Wii U endpoints. Defaults to no override, and if
set entirely overrides the "wiiu_endpoint" parameter.
progress: DownloadCallback, optional
A callback function used to return the progress of the downloads. The provided callable must match the signature
defined in DownloadCallback.
Returns
-------
Title
A Title object containing all the data from the downloaded title.
See Also
--------
libWiiPy.title.nus.DownloadCallback
"""
# First, create the new title.
title = Title()
# Download and load the TMD, Ticket, and certs.
title.load_tmd(download_tmd(title_id, title_version, wiiu_endpoint, endpoint_override))
title.load_ticket(download_ticket(title_id, wiiu_endpoint, endpoint_override))
title.wad.set_cert_data(download_cert(wiiu_endpoint, endpoint_override))
# Download and load the certificate chain, TMD, and Ticket.
title.load_cert_chain(download_cert_chain(wiiu_endpoint, endpoint_override))
title.load_tmd(download_tmd(title_id, title_version, wiiu_endpoint, endpoint_override, progress))
title.load_ticket(download_ticket(title_id, wiiu_endpoint, endpoint_override, progress))
# Download all contents
title.load_content_records()
title.content.content_list = download_contents(title_id, title.tmd, wiiu_endpoint, endpoint_override)
title.content.content_list = download_contents(title_id, title.tmd, wiiu_endpoint, endpoint_override, progress)
# Return the completed title.
return title
def download_tmd(title_id: str, title_version: int = None, wiiu_endpoint: bool = False,
endpoint_override: str = None) -> bytes:
def download_tmd(title_id: str, title_version: int | None = None, wiiu_endpoint: bool = False,
endpoint_override: str | None = None, progress: DownloadCallback = lambda done, total: None) -> bytes:
"""
Downloads the TMD of the Title specified in the object. Will download the latest version by default, or another
version if it was manually specified in the object.
@@ -68,11 +98,18 @@ def download_tmd(title_id: str, title_version: int = None, wiiu_endpoint: bool =
endpoint_override: str, optional
A custom endpoint URL to use instead of the standard Wii or Wii U endpoints. Defaults to no override, and if
set entirely overrides the "wiiu_endpoint" parameter.
progress: DownloadCallback, optional
A callback function used to return the progress of the download. The provided callable must match the signature
defined in DownloadCallback.
Returns
-------
bytes
The TMD file from the NUS.
See Also
--------
libWiiPy.title.nus.DownloadCallback
"""
# Build the download URL. The structure is download/<TID>/tmd for latest and download/<TID>/tmd.<version> for
# when a specific version is requested.
@@ -88,13 +125,28 @@ def download_tmd(title_id: str, title_version: int = None, wiiu_endpoint: bool =
if title_version is not None:
tmd_url += "." + str(title_version)
# Make the request.
tmd_request = requests.get(url=tmd_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
try:
response = requests.get(url=tmd_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
except requests.exceptions.ConnectionError:
if endpoint_override:
raise ValueError("A connection could not be made to the NUS endpoint. Please make sure that your endpoint "
"override is valid.")
else:
raise Exception("A connection could not be made to the NUS endpoint. The NUS may be unavailable.")
# Handle a 404 if the TID/version doesn't exist.
if tmd_request.status_code != 200:
if response.status_code == 404:
raise ValueError("The requested Title ID or TMD version does not exist. Please check the Title ID and Title"
" version and then try again.")
# Save the raw TMD.
raw_tmd = tmd_request.content
elif response.status_code != 200:
raise Exception(f"An unknown error occurred while downloading the TMD. "
f"Got HTTP status code: {response.status_code}")
total_size = int(response.headers["Content-Length"])
progress(0, total_size)
# Stream the TMD's data in chunks so that we can post updates to the callback function (assuming one was supplied).
raw_tmd = b""
for chunk in response.iter_content(512):
raw_tmd += chunk
progress(len(raw_tmd), total_size)
# Use a TMD object to load the data and then return only the actual TMD.
tmd_temp = TMD()
tmd_temp.load(raw_tmd)
@@ -102,7 +154,8 @@ def download_tmd(title_id: str, title_version: int = None, wiiu_endpoint: bool =
return tmd
def download_ticket(title_id: str, wiiu_endpoint: bool = False, endpoint_override: str = None) -> bytes:
def download_ticket(title_id: str, wiiu_endpoint: bool = False, endpoint_override: str | None = None,
progress: DownloadCallback = lambda done, total: None) -> bytes:
"""
Downloads the Ticket of the Title specified in the object. This will only work if the Title ID specified is for
a free title.
@@ -116,11 +169,18 @@ def download_ticket(title_id: str, wiiu_endpoint: bool = False, endpoint_overrid
endpoint_override: str, optional
A custom endpoint URL to use instead of the standard Wii or Wii U endpoints. Defaults to no override, and if
set entirely overrides the "wiiu_endpoint" parameter.
progress: DownloadCallback, optional
A callback function used to return the progress of the download. The provided callable must match the signature
defined in DownloadCallback.
Returns
-------
bytes
The Ticket file from the NUS.
See Also
--------
libWiiPy.title.nus.DownloadCallback
"""
# Build the download URL. The structure is download/<TID>/cetk, and cetk will only exist if this is a free
# title.
@@ -133,12 +193,27 @@ def download_ticket(title_id: str, wiiu_endpoint: bool = False, endpoint_overrid
endpoint_url = _nus_endpoint[0]
ticket_url = endpoint_url + title_id + "/cetk"
# Make the request.
ticket_request = requests.get(url=ticket_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
if ticket_request.status_code != 200:
try:
response = requests.get(url=ticket_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
except requests.exceptions.ConnectionError:
if endpoint_override:
raise ValueError("A connection could not be made to the NUS endpoint. Please make sure that your endpoint "
"override is valid.")
else:
raise Exception("A connection could not be made to the NUS endpoint. The NUS may be unavailable.")
if response.status_code == 404:
raise ValueError("The requested Title ID does not exist, or refers to a non-free title. Tickets can only"
" be downloaded for titles that are free on the NUS.")
# Save the raw cetk file.
cetk = ticket_request.content
elif response.status_code != 200:
raise Exception(f"An unknown error occurred while downloading the Ticket. "
f"Got HTTP status code: {response.status_code}")
total_size = int(response.headers["Content-Length"])
progress(0, total_size)
# Stream the Ticket's data just like with the TMD.
cetk = b""
for chunk in response.iter_content(chunk_size=1024):
cetk += chunk
progress(len(cetk), total_size)
# Use a Ticket object to load only the Ticket data from cetk and return it.
ticket_temp = Ticket()
ticket_temp.load(cetk)
@@ -146,9 +221,9 @@ def download_ticket(title_id: str, wiiu_endpoint: bool = False, endpoint_overrid
return ticket
def download_cert(wiiu_endpoint: bool = False, endpoint_override: str = None) -> bytes:
def download_cert_chain(wiiu_endpoint: bool = False, endpoint_override: str | None = None) -> bytes:
"""
Downloads the signing certificate used by all WADs. This uses System Menu 4.3U as the source.
Downloads the signing certificate chain used by all WADs. This uses System Menu 4.3U as the source.
Parameters
----------
@@ -163,7 +238,7 @@ def download_cert(wiiu_endpoint: bool = False, endpoint_override: str = None) ->
bytes
The cert file.
"""
# Download the TMD and cetk for the System Menu 4.3U.
# Download the TMD and cetk for System Menu 4.3U (v513).
if endpoint_override is not None:
endpoint_url = _validate_endpoint(endpoint_override)
else:
@@ -173,24 +248,32 @@ def download_cert(wiiu_endpoint: bool = False, endpoint_override: str = None) ->
endpoint_url = _nus_endpoint[0]
tmd_url = endpoint_url + "0000000100000002/tmd.513"
cetk_url = endpoint_url + "0000000100000002/cetk"
try:
tmd = requests.get(url=tmd_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True).content
cetk = requests.get(url=cetk_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True).content
# Assemble the certificate.
cert = b''
except requests.exceptions.ConnectionError:
if endpoint_override:
raise ValueError("A connection could not be made to the NUS endpoint. Please make sure that your endpoint "
"override is valid.")
else:
raise Exception("A connection could not be made to the NUS endpoint. The NUS may be unavailable.")
# Assemble the certificate chain.
cert_chain = b''
# Certificate Authority data.
cert += cetk[0x2A4 + 768:]
# Certificate Policy data.
cert += tmd[0x328:0x328 + 768]
# XS data.
cert += cetk[0x2A4:0x2A4 + 768]
# Since the cert is always the same, check the hash to make sure nothing went wildly wrong.
if hashlib.sha1(cert).hexdigest() != "ace0f15d2a851c383fe4657afc3840d6ffe30ad0":
raise Exception("An unknown error has occurred downloading and creating the certificate.")
return cert
cert_chain += cetk[0x2A4 + 768:]
# Certificate Policy (TMD certificate) data.
cert_chain += tmd[0x328:0x328 + 768]
# XS (Ticket certificate) data.
cert_chain += cetk[0x2A4:0x2A4 + 768]
# Since the cert chain is always the same, check the hash to make sure nothing went wildly wrong.
# This is currently disabled because of the possibility that one may be downloading non-retail certs (gasp!).
#if hashlib.sha1(cert_chain).hexdigest() != "ace0f15d2a851c383fe4657afc3840d6ffe30ad0":
# raise Exception("An unknown error has occurred downloading and creating the certificate.")
return cert_chain
def download_content(title_id: str, content_id: int, wiiu_endpoint: bool = False,
endpoint_override: str = None) -> bytes:
def download_content(title_id: str, content_id: int, wiiu_endpoint: bool = False, endpoint_override: str | None = None,
progress: DownloadCallback = lambda done, total: None) -> bytes:
"""
Downloads a specified content for the title specified in the object.
@@ -205,16 +288,20 @@ def download_content(title_id: str, content_id: int, wiiu_endpoint: bool = False
endpoint_override: str, optional
A custom endpoint URL to use instead of the standard Wii or Wii U endpoints. Defaults to no override, and if
set entirely overrides the "wiiu_endpoint" parameter.
progress: DownloadCallback, optional
A callback function used to return the progress of the download. The provided callable must match the signature
defined in DownloadCallback.
Returns
-------
bytes
The downloaded content.
See Also
--------
libWiiPy.title.nus.DownloadCallback
"""
# Build the download URL. The structure is download/<TID>/<Content ID>.
content_id_hex = hex(content_id)[2:]
if len(content_id_hex) < 2:
content_id_hex = "0" + content_id_hex
if endpoint_override is not None:
endpoint_url = _validate_endpoint(endpoint_override)
else:
@@ -222,19 +309,34 @@ def download_content(title_id: str, content_id: int, wiiu_endpoint: bool = False
endpoint_url = _nus_endpoint[1]
else:
endpoint_url = _nus_endpoint[0]
content_url = endpoint_url + title_id + "/000000" + content_id_hex
content_url = f"{endpoint_url}{title_id}/{content_id:08X}"
# Make the request.
content_request = requests.get(url=content_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
if content_request.status_code != 200:
raise ValueError("The requested Title ID does not exist, or an invalid Content ID is present in the"
" content records provided.\n Failed while downloading Content ID: 000000" +
content_id_hex)
content_data = content_request.content
return content_data
try:
response = requests.get(url=content_url, headers={'User-Agent': 'wii libnup/1.0'}, stream=True)
except requests.exceptions.ConnectionError:
if endpoint_override:
raise ValueError("A connection could not be made to the NUS endpoint. Please make sure that your endpoint "
"override is valid.")
else:
raise Exception("A connection could not be made to the NUS endpoint. The NUS may be unavailable.")
if response.status_code == 404:
raise ValueError(f"The requested Title ID does not exist, or an invalid Content ID is present in the"
f" content records provided.\n Failed while downloading Content ID: {content_id:08X}")
elif response.status_code != 200:
raise Exception(f"An unknown error occurred while downloading the content. "
f"Got HTTP status code: {response.status_code}")
total_size = int(response.headers["Content-Length"])
progress(0, total_size)
# Stream the content just like the TMD/Ticket.
content = b""
for chunk in response.iter_content(chunk_size=1024):
content += chunk
progress(len(content), total_size)
return content
def download_contents(title_id: str, tmd: TMD, wiiu_endpoint: bool = False,
endpoint_override: str = None) -> List[bytes]:
def download_contents(title_id: str, tmd: TMD, wiiu_endpoint: bool = False, endpoint_override: str | None = None,
progress: DownloadCallback = lambda done, total: None) -> List[bytes]:
"""
Downloads all the contents for the title specified in the object. This requires a TMD to already be available
so that the content records can be accessed.
@@ -250,11 +352,18 @@ def download_contents(title_id: str, tmd: TMD, wiiu_endpoint: bool = False,
endpoint_override: str, optional
A custom endpoint URL to use instead of the standard Wii or Wii U endpoints. Defaults to no override, and if
set entirely overrides the "wiiu_endpoint" parameter.
progress: DownloadCallback, optional
A callback function used to return the progress of the downloads. The provided callable must match the signature
defined in DownloadCallback.
Returns
-------
List[bytes]
A list of all the downloaded contents.
See Also
--------
libWiiPy.title.nus.DownloadCallback
"""
# Retrieve the content records from the TMD.
content_records = tmd.content_records
@@ -266,7 +375,7 @@ def download_contents(title_id: str, tmd: TMD, wiiu_endpoint: bool = False,
content_list = []
for content_id in content_ids:
# Call self.download_content() for each Content ID.
content = download_content(title_id, content_id, wiiu_endpoint, endpoint_override)
content = download_content(title_id, content_id, wiiu_endpoint, endpoint_override, progress)
content_list.append(content)
return content_list
@@ -286,9 +395,10 @@ def _validate_endpoint(endpoint: str) -> str:
The validated NUS endpoint with the proper path.
"""
# Find the root of the URL and then assemble the correct URL based on that.
new_url = _urlparse(endpoint)
if new_url.netloc == "":
endpoint_url = "http://" + new_url.path + "/ccs/download/"
else:
endpoint_url = "http://" + new_url.netloc + "/ccs/download/"
return endpoint_url
# TODO: Rewrite in a way that makes more sense and un-stub
#new_url = _urlparse(endpoint)
#if new_url.netloc == "":
# endpoint_url = "http://" + new_url.path + "/ccs/download/"
#else:
# endpoint_url = "http://" + new_url.netloc + "/ccs/download/"
return endpoint

View File

@@ -9,7 +9,7 @@ import hashlib
from dataclasses import dataclass as _dataclass
from .crypto import decrypt_title_key
from typing import List
from .util import title_ver_standard_to_dec
from .versions import title_ver_standard_to_dec
@_dataclass
@@ -57,7 +57,7 @@ class Ticket:
common_key_index : int
The index of the common key required to decrypt this ticket's Title Key.
"""
def __init__(self):
def __init__(self) -> None:
# If this is a dev ticket
self.is_dev: bool = False # Defaults to false, set to true during load if this ticket is using dev certs.
# Signature blob header
@@ -75,7 +75,7 @@ class Ticket:
self.title_version: int = 0 # Version of the ticket's associated title.
self.permitted_titles: bytes = b'' # Permitted titles mask
# "Permit mask. The current disc title is ANDed with the inverse of this mask to see if the result matches the
# Permitted Titles Mask."
# Permitted Titles Mask." -WiiBrew
self.permit_mask: bytes = b''
self.title_export_allowed: int = 0 # Whether title export is allowed with a PRNG key or not.
self.common_key_index: int = 0 # Which common key should be used. 0 = Common Key, 1 = Korean Key, 2 = vWii Key
@@ -107,7 +107,7 @@ class Ticket:
self.signature = ticket_data.read(256)
# Signature issuer.
ticket_data.seek(0x140)
self.signature_issuer = str(ticket_data.read(64).decode())
self.signature_issuer = str(ticket_data.read(64).replace(b'\x00', b'').decode())
# ECDH data.
ticket_data.seek(0x180)
self.ecdh_data = ticket_data.read(60)
@@ -128,7 +128,7 @@ class Ticket:
self.console_id = int.from_bytes(ticket_data.read(4))
# Title ID.
ticket_data.seek(0x1DC)
self.title_id = binascii.hexlify(ticket_data.read(8))
self.title_id = ticket_data.read(8)
# Unknown data 1.
ticket_data.seek(0x1E4)
self.unknown1 = ticket_data.read(2)
@@ -160,7 +160,8 @@ class Ticket:
limit_value = int.from_bytes(ticket_data.read(4))
self.title_limits_list.append(_TitleLimit(limit_type, limit_value))
# Check certs to see if this is a retail or dev ticket. Treats unknown certs as being retail for now.
if self.signature_issuer.find("Root-CA00000002-XS00000006") != -1:
if (self.signature_issuer.find("Root-CA00000002-XS00000006") != -1 or
self.signature_issuer.find("Root-CA00000002-XS00000004") != -1):
self.is_dev = True
else:
self.is_dev = False
@@ -182,7 +183,10 @@ class Ticket:
# Padding to 64 bytes.
ticket_data += b'\x00' * 60
# Signature issuer.
ticket_data += str.encode(self.signature_issuer)
signature_issuer = self.signature_issuer.encode()
while len(signature_issuer) < 0x40:
signature_issuer += b'\x00'
ticket_data += signature_issuer
# ECDH data.
ticket_data += self.ecdh_data
# Ticket version.
@@ -198,7 +202,7 @@ class Ticket:
# Console ID.
ticket_data += int.to_bytes(self.console_id, 4)
# Title ID.
ticket_data += binascii.unhexlify(self.title_id)
ticket_data += self.title_id
# Unknown data 1.
ticket_data += self.unknown1
# Title version.
@@ -277,8 +281,7 @@ class Ticket:
"""
if self.signature != b'\x00' * 256:
return False
test_hash = hashlib.sha1(self.dump()[320:]).hexdigest()
if test_hash[:2] != '00':
if hashlib.sha1(self.dump()[320:]).hexdigest()[:2] != '00':
return False
return True
@@ -291,8 +294,7 @@ class Ticket:
str
The Title ID of the title.
"""
title_id_str = str(self.title_id.decode())
return title_id_str
return str(self.title_id.decode())
def get_common_key_type(self) -> str:
"""
@@ -314,6 +316,8 @@ class Ticket:
return "Korean"
case 2:
return "vWii"
case _:
return "Unknown"
def get_title_key(self) -> bytes:
"""
@@ -339,7 +343,7 @@ class Ticket:
"""
if len(title_id) != 16:
raise ValueError("Invalid Title ID! Title IDs must be 8 bytes long.")
self.title_id = title_id.encode()
self.title_id = binascii.unhexlify(title_id.encode())
def set_title_version(self, new_version: str | int) -> None:
"""
@@ -364,8 +368,8 @@ class Ticket:
version_converted = title_ver_standard_to_dec(new_version, str(self.title_id.decode()))
self.title_version = version_converted
elif type(new_version) is int:
# Validate that the version isn't higher than v65280. If the check passes, set that as the title version.
if new_version > 65535:
# Validate that the version isn't higher than 0xFFFF (v65535).
if new_version > 0xFFFF:
raise ValueError("Title version is not valid! Integer version number cannot exceed v65535.")
self.title_version = new_version
else:

View File

@@ -4,11 +4,16 @@
# See https://wiibrew.org/wiki/Title for details about how titles are formatted
import math
from .content import ContentRegion
from .ticket import Ticket
from .tmd import TMD
from .wad import WAD
from .cert import (CertificateChain as _CertificateChain,
verify_ca_cert as _verify_ca_cert, verify_cert_sig as _verify_cert_sig,
verify_tmd_sig as _verify_tmd_sig, verify_ticket_sig as _verify_ticket_sig)
from .content import ContentRegion as _ContentRegion
from .crypto import encrypt_title_key
from .ticket import Ticket as _Ticket
from .tmd import TMD as _TMD
from .types import ContentType
from .wad import WAD as _WAD
class Title:
@@ -21,6 +26,8 @@ class Title:
----------
wad: WAD
A WAD object of a WAD containing the title's data.
cert_chain: CertificateChain
The chain of certificates used to verify the contents of a title.
tmd: TMD
A TMD object of the title's TMD.
ticket: Ticket
@@ -28,11 +35,12 @@ class Title:
content: ContentRegion
A ContentRegion object containing the title's contents.
"""
def __init__(self):
self.wad: WAD = WAD()
self.tmd: TMD = TMD()
self.ticket: Ticket = Ticket()
self.content: ContentRegion = ContentRegion()
def __init__(self) -> None:
self.wad: _WAD = _WAD()
self.cert_chain: _CertificateChain = _CertificateChain()
self.tmd: _TMD = _TMD()
self.ticket: _Ticket = _Ticket()
self.content: _ContentRegion = _ContentRegion()
def load_wad(self, wad: bytes) -> None:
"""
@@ -45,16 +53,19 @@ class Title:
The data for the WAD you wish to load.
"""
# Create a new WAD object based on the WAD data provided.
self.wad = WAD()
self.wad = _WAD()
self.wad.load(wad)
# Load the certificate chain.
self.cert_chain = _CertificateChain()
self.cert_chain.load(self.wad.get_cert_data())
# Load the TMD.
self.tmd = TMD()
self.tmd = _TMD()
self.tmd.load(self.wad.get_tmd_data())
# Load the ticket.
self.ticket = Ticket()
self.ticket = _Ticket()
self.ticket.load(self.wad.get_ticket_data())
# Load the content.
self.content = ContentRegion()
self.content = _ContentRegion()
self.content.load(self.wad.get_content_data(), self.tmd.content_records)
# Ensure that the Title IDs of the TMD and Ticket match before doing anything else. If they don't, throw an
# error because clearly something strange has gone on with the WAD and editing it probably won't work.
@@ -75,6 +86,8 @@ class Title:
# Set WAD type to ib if the title being packed is boot2.
if self.tmd.title_id == "0000000100000001":
self.wad.wad_type = "ib"
# Dump the certificate chain and set it in the WAD.
self.wad.set_cert_data(self.cert_chain.dump())
# Dump the TMD and set it in the WAD.
# This requires updating the content records and number of contents in the TMD first.
self.tmd.content_records = self.content.content_records # This may not be needed because it's a ref already
@@ -87,6 +100,19 @@ class Title:
self.wad.set_content_data(content_data, content_size)
return self.wad.dump()
def load_cert_chain(self, cert_chain: bytes) -> None:
"""
Load an existing certificate chain into the title. Note that this will overwrite any existing certificate chain
data for this title.
Parameters
----------
cert_chain: bytes
The data for the certificate chain to load.
"""
self.cert_chain.load(cert_chain)
def load_tmd(self, tmd: bytes) -> None:
"""
Load existing TMD data into the title. Note that this will overwrite any existing TMD data for this title.
@@ -94,9 +120,8 @@ class Title:
Parameters
----------
tmd : bytes
The data for the WAD you wish to load.
The data for the TMD to load.
"""
# Load TMD.
self.tmd.load(tmd)
def load_ticket(self, ticket: bytes) -> None:
@@ -107,9 +132,8 @@ class Title:
Parameters
----------
ticket : bytes
The data for the WAD you wish to load.
The data for the Ticket to load.
"""
# Load Ticket.
self.ticket.load(ticket)
def load_content_records(self) -> None:
@@ -156,7 +180,7 @@ class Title:
self.tmd.set_title_version(title_version)
self.ticket.set_title_version(title_version)
def get_content_by_index(self, index: id, skip_hash=False) -> bytes:
def get_content_by_index(self, index: int, skip_hash=False) -> bytes:
"""
Gets an individual content from the content region based on the provided index, in decrypted form.
@@ -172,6 +196,8 @@ class Title:
bytes
The decrypted content listed in the content record.
"""
if self.ticket.title_id == "":
raise ValueError("A Ticket must be loaded to get decrypted content.")
dec_content = self.content.get_content_by_index(index, self.ticket.get_title_key(), skip_hash)
return dec_content
@@ -191,6 +217,8 @@ class Title:
bytes
The decrypted content listed in the content record.
"""
if self.ticket.title_id == "":
raise ValueError("A Ticket must be loaded to get decrypted content.")
dec_content = self.content.get_content_by_cid(cid, self.ticket.get_title_key(), skip_hash)
return dec_content
@@ -217,7 +245,7 @@ class Title:
# For contents, get their sizes from the content records, because they store the intended sizes of the decrypted
# contents, which are usually different from the encrypted sizes.
for record in self.content.content_records:
if record.content_type == 32769:
if record.content_type == ContentType.SHARED:
if absolute:
title_size += record.content_size
else:
@@ -295,8 +323,8 @@ class Title:
# Update the TMD to match.
self.tmd.content_records = self.content.content_records
def set_enc_content(self, enc_content: bytes, index: int, content_size: int, content_hash: bytes, cid: int = None,
content_type: int = None) -> None:
def set_enc_content(self, enc_content: bytes, index: int, content_size: int, content_hash: bytes,
cid: int | None = None, content_type: int | None = None) -> None:
"""
Sets the content at the provided index to the provided new encrypted content. The provided hash and content size
are set in the corresponding content record. A new Content ID or content type can also be specified, but if it
@@ -324,7 +352,8 @@ class Title:
# Update the TMD to match.
self.tmd.content_records = self.content.content_records
def set_content(self, dec_content: bytes, index: int, cid: int = None, content_type: int = None) -> None:
def set_content(self, dec_content: bytes, index: int, cid: int | None = None,
content_type: int | None = None) -> None:
"""
Sets the content at the provided index to the provided new decrypted content. The hash and content size of this
content will be generated and then set in the corresponding content record. A new Content ID or content type can
@@ -398,3 +427,34 @@ class Title:
return True
else:
return False
def get_is_signed(self) -> bool:
"""
Uses the certificate chain to verify whether the Title object contains a properly signed title or not. This
verifies both the TMD and Ticket, and if either one fails verification then the title is not considered valid.
This will validate the entire certificate chain. If any part of the chain doesn't match the other pieces, then
this method will raise an exception.
Returns
-------
bool
Whether the title is properly signed or not.
See Also
--------
libWiiPy.title.cert
"""
# I did not understand short-circuiting when I originally wrote this code, and it was 5 nested if statements
# which looked silly. I now understand that this is functionally identical!
try:
if _verify_ca_cert(self.cert_chain.ca_cert) and \
_verify_cert_sig(self.cert_chain.ca_cert, self.cert_chain.tmd_cert) and \
_verify_tmd_sig(self.cert_chain.tmd_cert, self.tmd) and \
_verify_cert_sig(self.cert_chain.ca_cert, self.cert_chain.ticket_cert) and \
_verify_ticket_sig(self.cert_chain.ticket_cert, self.ticket):
return True
except ValueError:
raise ValueError("This title's certificate chain is not valid, or does not match the signature type of "
"the TMD/Ticket.")
return False

View File

@@ -10,9 +10,9 @@ import math
import struct
from typing import List
from enum import IntEnum as _IntEnum
from ..types import _ContentRecord
from ..shared import _bitmask
from .util import title_ver_dec_to_standard, title_ver_standard_to_dec
from .types import ContentRecord, ContentType, TitleType, Region
from .versions import title_ver_standard_to_dec
class TMD:
@@ -34,9 +34,9 @@ class TMD:
num_contents : int
The number of contents listed in the TMD.
"""
def __init__(self):
def __init__(self) -> None:
self.blob_header: bytes = b''
self.signature_type: int = 0
self.signature_type: bytes = b''
self.signature: bytes = b''
self.signature_issuer: str = "" # Follows the format "Root-CA%08x-CP%08x"
self.tmd_version: int = 0 # This seems to always be 0 no matter what?
@@ -55,11 +55,10 @@ class TMD:
self.reserved2: bytes = b'' # Other "Reserved" data from WiiBrew.
self.access_rights: int = 0
self.title_version: int = 0 # The version of the associated title.
self.title_version_converted: int = 0 # The title version in vX.X format.
self.num_contents: int = 0 # The number of contents contained in the associated title.
self.boot_index: int = 0 # The content index that contains the bootable executable.
self.minor_version: int = 0 # Minor version (unused typically).
self.content_records: List[_ContentRecord] = []
self.content_records: List[ContentRecord] = []
def load(self, tmd: bytes) -> None:
"""
@@ -83,7 +82,7 @@ class TMD:
self.signature = tmd_data.read(256)
# Signing certificate issuer.
tmd_data.seek(0x140)
self.signature_issuer = str(tmd_data.read(64).decode())
self.signature_issuer = str(tmd_data.read(64).replace(b'\x00', b'').decode())
# TMD version, seems to usually be 0, but I've seen references to other numbers.
tmd_data.seek(0x180)
self.tmd_version = int.from_bytes(tmd_data.read(1))
@@ -137,8 +136,6 @@ class TMD:
# Version number straight from the TMD.
tmd_data.seek(0x1DC)
self.title_version = int.from_bytes(tmd_data.read(2))
# Calculate the converted version number via util module.
self.title_version_converted = title_ver_dec_to_standard(self.title_version, self.title_id, bool(self.vwii))
# The number of contents listed in the TMD.
tmd_data.seek(0x1DE)
self.num_contents = int.from_bytes(tmd_data.read(2))
@@ -154,7 +151,7 @@ class TMD:
tmd_data.seek(0x1E4 + (36 * content))
content_record_hdr = struct.unpack(">LHH4x4s20s", tmd_data.read(36))
self.content_records.append(
_ContentRecord(int(content_record_hdr[0]), int(content_record_hdr[1]),
ContentRecord(int(content_record_hdr[0]), int(content_record_hdr[1]),
int(content_record_hdr[2]), int.from_bytes(content_record_hdr[3]),
binascii.hexlify(content_record_hdr[4])))
@@ -175,7 +172,10 @@ class TMD:
# Padding to 64 bytes.
tmd_data += b'\x00' * 60
# Signing certificate issuer.
tmd_data += str.encode(self.signature_issuer)
signature_issuer = self.signature_issuer.encode()
while len(signature_issuer) < 0x40:
signature_issuer += b'\x00'
tmd_data += signature_issuer
# TMD version.
tmd_data += int.to_bytes(self.tmd_version, 1)
# Certificate Authority CRL version.
@@ -251,7 +251,8 @@ class TMD:
self.minor_version = current_int
# Trim off the first 320 bytes, because we're only looking for the hash of the TMD's body.
# This is a try-except because an OverflowError will be thrown if the number being used to brute-force the
# hash gets too big, as it is only a 16-bit integer. If that happens, then fakesigning has failed.
# hash gets too big, as it is only a 16-bit integer. If that happens, then fakesigning has failed. This
# shouldn't ever realistically happen, though.
try:
test_hash = hashlib.sha1(self.dump()[320:]).hexdigest()
except OverflowError:
@@ -273,8 +274,7 @@ class TMD:
"""
if self.signature != b'\x00' * 256:
return False
test_hash = hashlib.sha1(self.dump()[320:]).hexdigest()
if test_hash[:2] != '00':
if hashlib.sha1(self.dump()[320:]).hexdigest()[:2] != '00':
return False
return True
@@ -292,16 +292,18 @@ class TMD:
The region of the title.
"""
match self.region:
case 0:
case Region.JPN:
return "JPN"
case 1:
case Region.USA:
return "USA"
case 2:
case Region.EUR:
return "EUR"
case 3:
return "None"
case 4:
case Region.WORLD:
return "World"
case Region.KOR:
return "KOR"
case _:
raise ValueError(f"Title contains unknown region \"{self.region}\".")
def get_title_type(self) -> str:
"""
@@ -316,19 +318,19 @@ class TMD:
The type of the title.
"""
match self.title_id[:8]:
case '00000001':
case TitleType.SYSTEM:
return "System"
case '00010000':
case TitleType.GAME:
return "Game"
case '00010001':
case TitleType.CHANNEL:
return "Channel"
case '00010002':
case TitleType.SYSTEM_CHANNEL:
return "SystemChannel"
case '00010004':
case TitleType.GAME_CHANNEL:
return "GameChannel"
case '00010005':
case TitleType.DLC:
return "DLC"
case '00010008':
case TitleType.HIDDEN_CHANNEL:
return "HiddenChannel"
case _:
return "Unknown"
@@ -358,20 +360,20 @@ class TMD:
# This is the literal index in the list of content that we're going to get.
target_index = current_indices.index(content_index)
match self.content_records[target_index].content_type:
case 1:
case ContentType.NORMAL:
return "Normal"
case 2:
case ContentType.DEVELOPMENT:
return "Development/Unknown"
case 3:
case ContentType.HASH_TREE:
return "Hash Tree"
case 16385:
case ContentType.DLC:
return "DLC"
case 32769:
case ContentType.SHARED:
return "Shared"
case _:
return "Unknown"
def get_content_record(self, record) -> _ContentRecord:
def get_content_record(self, record) -> ContentRecord:
"""
Gets the content record at the specified index.
@@ -388,8 +390,8 @@ class TMD:
if record < self.num_contents:
return self.content_records[record]
else:
raise IndexError("Invalid content record! TMD lists '" + str(self.num_contents - 1) +
"' contents but index was '" + str(record) + "'!")
raise IndexError(f"Invalid content record! TMD lists \"{self.num_contents - 1}\" contents "
f"but index was \"{record}\"!")
def get_content_size(self, absolute=False, dlc=False) -> int:
"""
@@ -413,13 +415,13 @@ class TMD:
"""
title_size = 0
for record in self.content_records:
if record.content_type == 0x8001:
if record.content_type == ContentType.SHARED:
if absolute:
title_size += record.content_size
elif record.content_type == 0x4001:
elif record.content_type == ContentType.DLC:
if dlc:
title_size += record.content_size
elif record.content_type != 3:
elif record.content_type != ContentType.DEVELOPMENT:
title_size += record.content_size
return title_size
@@ -448,10 +450,6 @@ class TMD:
blocks = math.ceil(title_size_bytes / 131072)
return blocks
class AccessFlags(_IntEnum):
AHB = 0
DVD_VIDEO = 1
def get_access_right(self, flag: int) -> bool:
"""
Gets whether the specified access rights flag is enabled or not. This is done by checking the specified bit.
@@ -471,7 +469,7 @@ class TMD:
--------
libWiiPy.title.tmd.TMD.AccessFlags
"""
return bool(self.access_rights & _bitmask(flag))
return bool(self.access_rights & (1 << flag))
def set_title_id(self, title_id) -> None:
"""
@@ -497,7 +495,7 @@ class TMD:
Parameters
----------
new_version : str, int
The new version of the title. See description for valid formats.
The new version of the title.
"""
if type(new_version) is str:
# Validate string input is in the correct format, then validate that the version isn't higher than v255.0.
@@ -507,16 +505,20 @@ class TMD:
raise ValueError("Title version is not valid! String version must be entered in format \"X.X\".")
if int(version_str_split[0]) > 255 or int(version_str_split[1]) > 255:
raise ValueError("Title version is not valid! String version number cannot exceed v255.255.")
self.title_version_converted = new_version
version_converted = title_ver_standard_to_dec(new_version, self.title_id)
version_converted: int = title_ver_standard_to_dec(new_version, self.title_id)
self.title_version = version_converted
elif type(new_version) is int:
# Validate that the version isn't higher than v65280. If the check passes, set that as the title version,
# then convert to standard form and set that as well.
if new_version > 65535:
# Validate that the version isn't higher than 0xFFFF (v65535).
if new_version > 0xFFFF:
raise ValueError("Title version is not valid! Integer version number cannot exceed v65535.")
self.title_version = new_version
version_converted = title_ver_dec_to_standard(new_version, self.title_id, bool(self.vwii))
self.title_version_converted = version_converted
else:
raise TypeError("Title version type is not valid! Type must be either integer or string.")
class AccessFlags(_IntEnum):
"""
Flags set in a TMD's access rights field used to enable specific feature access.
"""
AHB = 0
DVD_VIDEO = 1

View File

@@ -1,11 +1,14 @@
# "types.py" from libWiiPy by NinjaCheetah & Contributors
# "title/types.py" from libWiiPy by NinjaCheetah & Contributors
# https://github.com/NinjaCheetah/libWiiPy
#
# Shared types used across the title module.
from dataclasses import dataclass
from dataclasses import dataclass as _dataclass
from enum import IntEnum as _IntEnum, StrEnum as _StrEnum
@dataclass
class _ContentRecord:
@_dataclass
class ContentRecord:
"""
A content record object that contains the details of a content contained in a title. This information must match
the content stored at the index in the record, or else the content will not decrypt properly, as the hash of the
@@ -29,3 +32,38 @@ class _ContentRecord:
content_type: int # Type of content, possible values of: 0x0001: Normal, 0x4001: DLC, 0x8001: Shared.
content_size: int
content_hash: bytes
class ContentType(_IntEnum):
"""
The type of an individual piece of content.
"""
NORMAL = 0x0001
DEVELOPMENT = 0x0002
HASH_TREE = 0x0003
DLC = 0x4001
SHARED = 0x8001
class TitleType(_StrEnum):
"""
The type of a title.
"""
SYSTEM = "00000001"
GAME = "00010000"
CHANNEL = "00010001"
SYSTEM_CHANNEL = "00010002"
GAME_CHANNEL = "00010004"
DLC = "00010005"
HIDDEN_CHANNEL = "00010008"
class Region(_IntEnum):
"""
The region of a title.
"""
JPN = 0
USA = 1
EUR = 2
WORLD = 3
KOR = 4

View File

@@ -1,10 +1,9 @@
# "title/util.py" from libWiiPy by NinjaCheetah & Contributors
# "title/versions.py" from libWiiPy by NinjaCheetah & Contributors
# https://github.com/NinjaCheetah/libWiiPy
#
# General title-related utilities that don't fit within a specific module.
# Functions for converting the format that a title's version is in.
import math
from ..shared import _wii_menu_versions, _vwii_menu_versions
from ..constants import _WII_MENU_VERSIONS, _VWII_MENU_VERSIONS
def title_ver_dec_to_standard(version: int, title_id: str, vwii: bool = False) -> str:
@@ -27,26 +26,18 @@ def title_ver_dec_to_standard(version: int, title_id: str, vwii: bool = False) -
str
The version of the title, in standard form.
"""
version_out = ""
if title_id == "0000000100000002":
try:
if vwii:
try:
version_out = list(_vwii_menu_versions.keys())[list(_vwii_menu_versions.values()).index(version)]
except ValueError:
version_out = ""
return list(_VWII_MENU_VERSIONS.keys())[list(_VWII_MENU_VERSIONS.values()).index(version)]
else:
try:
version_out = list(_wii_menu_versions.keys())[list(_wii_menu_versions.values()).index(version)]
return list(_WII_MENU_VERSIONS.keys())[list(_WII_MENU_VERSIONS.values()).index(version)]
except ValueError:
version_out = ""
raise ValueError(f"Unrecognized System Menu version \"{version}\".")
else:
# For most channels, we need to get the floored value of version / 256 for the major version, and the version %
# 256 as the minor version. Minor versions > 9 are intended, as Nintendo themselves frequently used them.
version_upper = math.floor(version / 256)
version_lower = version % 256
version_out = f"{version_upper}.{version_lower}"
return version_out
# Typical titles use a two-byte version format where the upper byte is the major version, and the lower byte is
# the minor version.
return f"{version >> 8}.{version & 0xFF}"
def title_ver_standard_to_dec(version: str, title_id: str) -> int:
@@ -68,13 +59,15 @@ def title_ver_standard_to_dec(version: str, title_id: str) -> int:
int
The version of the title, in decimal form.
"""
version_out = 0
if title_id == "0000000100000002":
raise ValueError("The System Menu's version cannot currently be converted.")
for key in _WII_MENU_VERSIONS.keys():
if version.casefold() == key.casefold():
return _WII_MENU_VERSIONS[key]
for key in _VWII_MENU_VERSIONS.keys():
if version.casefold() == key.casefold():
return _VWII_MENU_VERSIONS[key]
raise ValueError(f"Unrecognized System Menu version \"{version}\".")
else:
version_str_split = version.split(".")
version_upper = int(version_str_split[0]) * 256
version_lower = int(version_str_split[1])
version_out = version_upper + version_lower
version_out = (int(version_str_split[0]) << 8) + int(version_str_split[1])
return version_out

View File

@@ -29,7 +29,7 @@ class WAD:
wad_meta_size : int
The size of the WAD's meta/footer.
"""
def __init__(self):
def __init__(self) -> None:
self.wad_hdr_size: int = 64
self.wad_type: str = "Is"
self.wad_version: bytes = b'\x00\x00'
@@ -49,17 +49,17 @@ class WAD:
self.wad_content_data: bytes = b''
self.wad_meta_data: bytes = b''
def load(self, wad_data: bytes) -> None:
def load(self, wad: bytes) -> None:
"""
Loads raw WAD data and sets all attributes of the WAD object. This allows for manipulating an already
existing WAD file.
Parameters
----------
wad_data : bytes
wad : bytes
The data for the WAD file to load.
"""
with io.BytesIO(wad_data) as wad_data:
with io.BytesIO(wad) as wad_data:
# Read the first 8 bytes of the file to ensure that it's a WAD. Has two possible valid values for the two
# different types of WADs that might be encountered.
wad_data.seek(0x0)
@@ -311,7 +311,7 @@ class WAD:
# Calculate the size of the new Ticket data.
self.wad_tik_size = len(tik_data)
def set_content_data(self, content_data, size: int = None) -> None:
def set_content_data(self, content_data, size: int | None = None) -> None:
"""
Sets the content data of the WAD. Also calculates the new size.