Skip to content

Protocol Library Export

finthetechguy edited this page Nov 23, 2026 · 1 revision

Import and Export

The export module moves messages and title keys between a Store and data from a console or emulator. It reads three kinds of source and writes two kinds of destination. All of it is file handling and runs on any platform.

Implemented in: export, export::source, export::savefs, export::folder.

Overview

flowchart LR
    save["CEC save image"] --> read["export::read"]
    folder["Folder tree"] --> read
    file["Message or envelope file"] --> read
    read --> source["Source: messages, title names and keys, skipped files"]
    source --> import["export::import"]
    import --> store["Store"]
    store --> deliver["CecFolder::deliver"]
    deliver --> emu["Emulator CEC folder inbox"]
    store --> raw["write_raw"]
    raw --> dir["TITLE/_id files"]
Loading

Reading a source

export::read(path) decides what the path is:

Path is Read as
A folder Walked recursively. If it holds MBoxList____ at its root it is treated as a console CEC folder, so title folders are found below it.
A file starting with SAVE A save partition image, read through its own filesystem
Any other file One message, or an envelope of messages

Inside a source, each file is taken by its name and place:

File Taken as
CEC/<title>/MBoxInfo____ with magic cc The title's signing key at 0x10, unless all zero
CEC/<title>/MBoxData.010 The title's name
A name starting with _, or contents starting with a container or envelope magic Messages. The box (InBox___ or OutBox__) is taken from the path when present.

A single container followed by padding is accepted: the container's own total size decides where it ends. A file named like a message that does not parse is listed as skipped with the reason.

Limits while reading:

Limit Value
File read from a source 4 MiB
Files in one folder tree 20000
Folder depth 16
Single input file 256 MiB

Save images

A CEC save image is read with a small read-only parser for the console's save filesystem.

Part Where
Magic SAVE at offset 0
Filesystem information Offset given by the u64 at offset 8
Block size u32 at info + 0x04, a power of two up to 1 MiB
Allocation table u64 offset at info + 0x28, entry count at info + 0x30
Data region u64 offset at info + 0x38, block count at info + 0x40
Directory table Block index at info + 0x48, entry count at info + 0x50, 0x28 bytes per entry
File table Block index at info + 0x58, entry count at info + 0x60, 0x30 bytes per entry

Directory entries hold the parent index at 0 and a 16-byte name at 4. File entries hold the parent directory at 0, a 16-byte name at 4, the first block at 0x1c and the size at 0x20.

The allocation table is a chain of 8-byte nodes. A node's second word gives the next node, with bit 31 marking the start of a run whose last block is stored in the next node. ctrpass follows the chain and refuses chains that loop, overlap, or leave the data region. Every table is checked to fit in the image before it is read.

Importing into a store

export::import(store, source, box):

  1. Registers each title that has a name or key. A key that differs from the store's is reported as a conflict and the store keeps its key.
  2. Adds each message to its box, or to the box given, or to the inbox when neither is known.
  3. Skips a message whose ID is already in that box.
  4. Refuses a message whose signature does not match the store's key for its title.
  5. Records the origin as "imported from" the path in the source.

The result counts messages added and duplicates, and lists refused messages, titles registered, and key conflicts.

Delivering into a CEC folder

CecFolder::open(path) accepts any of:

  • the CEC folder itself (it holds MBoxList____);
  • a folder with a CEC child;
  • an emulator data folder, where it looks in nand/data/*/sysdata/00010026/00000000/CEC.

CecFolder::deliver(message, time, dry_run) files one message in its title's inbox the way a receiving console does (see CEC Message Boxes):

flowchart TD
    m["Message"] --> box{"Title listed and has an inbox?"}
    box -- no --> nobox["NoBox: turn StreetPass on in the game first"]
    box -- yes --> key{"Folder has a key and the signature fails?"}
    key -- yes --> invalid["Invalid"]
    key -- no --> dup{"Same ID already listed or present?"}
    dup -- yes --> duplicate["Duplicate"]
    dup -- no --> big{"Larger than the box's message limit?"}
    big -- yes --> toobig["TooBig"]
    big -- no --> full{"Box count or bytes would pass its limits?"}
    full -- yes --> isfull["Full"]
    full -- no --> write["Mark new and unopened, set times, write file, update BoxInfo and MBoxInfo"]
    write --> res["Added, or Unverified when the folder has no key"]
Loading

With dry_run, every check runs and nothing is written.

Raw export

write_raw(messages, dest) writes each message unchanged as dest/<TITLE>/_<id>, with the file name rules of CEC Message Boxes.

Clone this wiki locally