nstools

package module
v0.0.0-...-7d29ad5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 15 Imported by: 0

README

NSTools

A Work in Progress for working with nintendo switch files.

Documentation

Overview

Package nstools provides tools for working with Nintendo Switch files.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMagic = errors.New("invalid magic signature")
	ErrUnknownFile  = errors.New("unknown file type")
	ErrInvalidSig   = errors.New("invalid signature type")
	ErrNotFound     = errors.New("file not found")
	ErrInvalidKey   = errors.New("invalid key")
)

Errors

View Source
var (
	MagicPFS0 = [...]byte{0x50, 0x46, 0x53, 0x30} // PFS0
	MagicNCA2 = [...]byte{0x4E, 0x43, 0x41, 0x32} // NCA2
	MagicNCA3 = [...]byte{0x4E, 0x43, 0x41, 0x33} // NCA3
)

Magic signatures

Functions

func OpenFile

func OpenFile(path string, keys *Keys) (any, error)

func Pack

func Pack(w io.Writer, files []string) error

Pack creates a PFS0 container from the provided files and writes it to the provided writer.

func Unpack

func Unpack(data io.ReadSeeker, output string) error

Unpack extracts all files from the PFS0 stream to the provided output directory.

Types

type CNMT

type CNMT struct {
	Type                          string
	ID                            string `xml:"Id"`
	Version                       int
	RequiredDownloadSystemVersion int
	RequiredSystemVersion         int
	KeyGenerationMin              int
}

func CnmtFromNCA

func CnmtFromNCA(pfs0 *PFS0, keys *Keys) (*CNMT, error)

func CnmtFromXML

func CnmtFromXML(pfs0 *PFS0) (*CNMT, error)

CnmtFromXML tries to parse the XML data and populate the CNMT struct. it returns an ErrNotFound error if the cnmt.xml file is not found.

type ContentType

type ContentType uint8
const (
	ContentTypeProgram ContentType = iota
	ContentTypeMeta
	ContentTypeControl
	ContentTypeManual
	ContentTypeData
	ContentTypePublicData
)

type DistributionType

type DistributionType uint8
const (
	DistributionTypeDownload DistributionType = iota
	DistributionTypeGameCard
)

type FSEntry

type FSEntry struct {
	StartOffset uint32
	EndOffset   uint32
}

type Keys

type Keys struct {
	HeaderKey []byte
}

func NewKeys

func NewKeys(data []byte) *Keys

NewKeys creates a new Keys struct from the given data. The data has the following format:

key_name = hex_key_value

key_name2 = hex_key_value2

func (*Keys) Validate

func (keys *Keys) Validate() error

Validate checks if the keys are valid.

type NCA

type NCA struct {
	NCAHeader
	// contains filtered or unexported fields
}

func NewNCA

func NewNCA(data io.ReadSeeker) (*NCA, error)

func (*NCA) Decrypt

func (nca *NCA) Decrypt(keys *Keys) error

func (*NCA) String

func (nca *NCA) String() string

type NCAHeader

type NCAHeader struct {
	FirstSignature   [0x100]byte
	SecondSignature  [0x100]byte
	Magic            [4]byte
	DistributionType DistributionType
	ContentType      ContentType
	KeyGenerationOld uint8
	KeyIndex         uint8
	ContentSize      uint64
	ProgramID        string
	ContentIndex     uint32
	SDKVersion       uint32 // {byte3}.{byte2}.{byte1}, byte0 is always 0
	KeyGeneration    uint32
	RigthsID         string
	SectionTables    [4]FSEntry
	SectionHashes    [4][0x20]byte
}

func (*NCAHeader) String

func (ncah *NCAHeader) String() string

type PFS0

type PFS0 struct {
	Magic      [4]byte
	HeaderSize uint32
	Files      []Partition
	// contains filtered or unexported fields
}

PFS0 is a file system, a container that holds multiple files.

func OpenPFS0

func OpenPFS0(data io.ReadSeeker) (*PFS0, error)

OpenPFS0 opens a PFS0 container from the provided reader. Only the header is read, use the Extract method to extract files.

func (*PFS0) Close

func (pfs0 *PFS0) Close() error

func (*PFS0) Extract

func (pfs0 *PFS0) Extract(w io.Writer, fileIndex int) error

Extract writes the file at the given index to the provided writer.

type Partition

type Partition struct {
	Name   string
	Size   int64
	Offset int64
}

Partition is a file header in a PFS0 container. Use the Extract method to extract the file data.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL