romloader

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: GPL-3.0 Imports: 16 Imported by: 5 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

eblitui-romloader

A shared ROM loading utility for eblitui UIs. Handles loading ROM files from raw files and compressed archives (ZIP, 7z, gzip/tar.gz, RAR), and streaming disc images for disc-based systems (CHD and cue/bin).

Valid ROM extensions are passed by the caller rather than being hardcoded. Extensions come from SystemInfo.Extensions at the call site.

Usage

import "github.com/user-none/eblitui/romloader"

// Load a ROM, searching archives for files matching the given extensions
data, filename, err := romloader.Load(path, []string{".sms"})

The Load function auto-detects archive formats via magic bytes and extracts the first file whose extension matches the provided list. For non-archive files, the file is read directly if its extension matches.

Public API

// Load reads a ROM from a file path. It auto-detects compressed
// archives via magic bytes and extracts the first file matching
// one of the given extensions.
//
// Returns the ROM data, the filename (basename only), and any error.
func Load(path string, extensions []string) ([]byte, string, error)
Errors
var ErrNoROMFile         // no ROM file found in archive
var ErrUnsupportedFormat // unrecognized file format
var ErrFileTooLarge      // file exceeds 8MB safety limit

Supported Formats

Detection uses magic bytes first (reliable), then falls back to file extension.

Format Magic bytes Extensions
ZIP PK\x03\x04 .zip
7z 7z\xBC\xAF\x27\x1C .7z
GZIP/TAR \x1F\x8B .gz, .tgz, .tar.gz
RAR Rar! .rar
Raw ROM (none) Caller-provided

For archives, the loader searches for the first file whose extension matches one of the provided ROM extensions (case-insensitive). Directories and non-matching files are skipped.

For plain gzip files (not tar.gz), the decompressed content is returned directly since the file is not a multi-file archive.

Disc Images

For disc-based systems, OpenDisc opens a streaming reader over a CD image. The backend is selected by file extension:

  • .chd - a CHD (V5) disc image.
  • .cue - a cue sheet referencing one or more raw .bin track files.

Any other extension returns ErrUnsupportedDisc. A bare .bin is not an openable disc. It is only a component referenced by a cue sheet.

disc, err := romloader.OpenDisc("game.cue")
if err != nil {
    // ...
}
defer disc.Close()

sector, err := disc.ReadSector(lba) // raw 2352-byte sector at absolute LBA
n := disc.NumTracks()
num, typ, frames, pregap, startLBA, control := disc.Track(0)

The reader is streaming: file handles are held open and sectors are decoded on demand, so the whole image is never read into memory.

DiscExtensions reports the file extensions OpenDisc accepts (.chd, .cue), each with a leading dot, sorted. Callers that scan a directory or filter a file list use it to find disc images without hardcoding the set. Unlike ROM images, where the core declares the extensions it understands, disc formats are owned by romloader: a core reads a disc through a format-agnostic interface and never sees the file, so romloader is the only component that knows which containers it opens.

Disc API
func OpenDisc(path string) (*Disc, error)
func DiscExtensions() []string

func (d *Disc) ReadSector(lba int) ([]byte, error)
func (d *Disc) NumTracks() int
func (d *Disc) Track(i int) (number int, typ string, frames int, pregap int, startLBA int, control uint8)
func (d *Disc) NumTrackIndexes(i int) int
func (d *Disc) TrackIndex(i, n int) (indexNumber int, lba int)
func (d *Disc) Close() error

NumTrackIndexes/TrackIndex expose the per-track index map (index numbers >= 1, absolute LBA); n is a 0-based ordinal into that list, not the index number. Index 0 (the pregap) is not listed - it is implied for any position below the first entry. CHD carries no index map, so its reader synthesizes a single INDEX 01 per track from the pregap. A cue sheet supplies the real INDEX 01..99.

cue / bin rules
  • Supported track modes are raw 2352-byte only: MODE1/2352, MODE2/2352, and AUDIO.
  • FILE names are resolved relative to the cue's directory. Absolute paths and parent-directory traversal are rejected.
  • One-bin-per-track and single-bin-multiple-track layouts are both handled. An in-file pregap (INDEX 00) is read from the bin. A PREGAP/POSTGAP command generates silence not backed by any file.
  • Sector bytes are returned verbatim (data tracks big-endian on disc, audio little-endian).
Disc Errors
var ErrUnsupportedDisc // path extension is neither .chd nor .cue

dischash tool

cmd/dischash identifies disc images and prints a CRC-32 over their track data, for matching a disc against a reference (redump-style) breakdown. It reads any image OpenDisc accepts (CHD or cue/bin), recognizes Sega Saturn, Sega Dreamcast, and PlayStation 1 discs (showing product ID and title where the format provides them), and marks an unreadable disc as ERROR rather than dropping it. A cue and its matching CHD produce identical per-track hashes.

go run ./cmd/dischash -file game.chd      # one disc: overall CRC-32 + ID/title
go run ./cmd/dischash -v -file game.cue   # full per-track table
go run ./cmd/dischash -dir path/to/discs  # scan a folder, discs hashed in parallel

-file and -dir are mutually exclusive; -j sets the -dir concurrency (default: number of CPUs).

Dependencies

  • github.com/bodgit/sevenzip - 7z archive support
  • github.com/nwaples/rardecode/v2 - RAR archive support
  • github.com/klauspost/compress - zstd decompression used by the CHD backend

ZIP and gzip archive support use Go's standard library. The cue/bin disc backend uses only the standard library. The CHD backend's remaining codecs (zlib, LZMA, FLAC) are stdlib or implemented in-package.

Testing

go test ./...

Used By

  • eblitui-standalone (ROM loading for desktop UI)
  • eblitui-ios (ROM loading for iOS app)

Not used by eblitui-libretro (the frontend provides ROM data directly).

Documentation

Overview

Package romloader handles loading ROM files from various sources, including compressed archives (ZIP, 7z, gzip, tar.gz, RAR).

Index

Constants

This section is empty.

Variables

View Source
var ErrArchiveTooLarge = errors.New("archive exceeds maximum decompressed size")

ErrArchiveTooLarge is returned when an archive decompresses to more than the total size limit while searching for a ROM

View Source
var ErrFileTooLarge = errors.New("file exceeds maximum size limit")

ErrFileTooLarge is returned when extracted content or a cue sheet exceeds its size limit

View Source
var ErrNoFile = errors.New("no matching file found in archive")

ErrNoFile is returned when no matching file is found in an archive

View Source
var ErrUnsupportedDisc = errors.New("romloader: unsupported disc format (expected .chd or .cue)")

ErrUnsupportedDisc is returned by OpenDisc when the path's extension is neither .chd nor .cue.

View Source
var ErrUnsupportedFormat = errors.New("unsupported file format")

ErrUnsupportedFormat is returned for unrecognized file formats

Functions

func DiscExtensions added in v0.4.0

func DiscExtensions() []string

DiscExtensions returns the lowercase file extensions OpenDisc accepts, each with a leading dot (".chd", ".cue"), sorted ascending. Callers that scan a directory or filter a file list use it to discover which files are disc images without hardcoding the set. Unlike ROM images, where the core declares the extensions it understands, the disc formats are owned by romloader: a core reads a disc through a format-agnostic interface and never sees the file, so romloader is the only component that knows which container formats it can open.

func Load

func Load(path string, extensions []string) ([]byte, string, error)

Load reads a ROM from a file path. It auto-detects compressed archives via magic bytes and extracts the first file matching one of the given extensions. For raw (non-archive) files, the extension must match or the file is loaded as-is if no archive format is detected.

Returns the ROM data, the filename (basename only, useful for display), and any error.

func LoadBIOS added in v0.2.0

func LoadBIOS(path string) ([]byte, error)

LoadBIOS reads a BIOS file from the given path, supporting the same archive formats as Load (ZIP, 7z, gzip/tar, RAR). Unlike Load, it does not require the contained file to have a specific extension - the first non-directory file in an archive is returned.

Types

type Disc added in v0.3.0

type Disc struct {
	// contains filtered or unexported fields
}

Disc is a streaming reader over a disc image. It owns the open file handles and decodes sectors on demand, so the whole image is never read into memory. Internally it is a facade over a format-specific backend (CHD or bin/cue); OpenDisc selects the backend. Its method set uses only stdlib types so it satisfies a disc-reader interface structurally without a shared type.

func OpenDisc added in v0.3.0

func OpenDisc(path string) (*Disc, error)

OpenDisc opens a disc image for streaming, selected by extension: ".chd" is opened as a CHD image, ".cue" as a cue sheet whose referenced bin track files are opened and held for the disc's lifetime. Any other extension returns ErrUnsupportedDisc. File handles are held open for on-demand sector reads until Close is called.

func (*Disc) Close added in v0.3.0

func (d *Disc) Close() error

Close releases the decoder and all underlying file handles.

func (*Disc) NumTrackIndexes added in v0.4.0

func (d *Disc) NumTrackIndexes(i int) int

NumTrackIndexes returns the count of index entries (index numbers >= 1) exposed for track index i in [0, NumTracks). Index 0 (the pregap) is never reported here; it is implied for any FAD below the first entry.

func (*Disc) NumTracks added in v0.3.0

func (d *Disc) NumTracks() int

NumTracks returns the number of tracks on the disc.

func (*Disc) ReadSector added in v0.3.0

func (d *Disc) ReadSector(lba int) ([]byte, error)

ReadSector returns the raw 2352-byte sector at the given LBA.

func (*Disc) ReadSectorData added in v0.4.1

func (d *Disc) ReadSectorData(lba int) ([]byte, error)

ReadSectorData returns the cooked user data of the sector at the given absolute LBA: the 2048-byte user-data region of a Mode 1 or Mode 2 (Form 1) data sector with the sync/header (and Mode 2 subheader) stripped. Audio sectors have no header and are returned whole (the full 2352 bytes). Use it when a consumer identifies a disc by its volume/boot headers rather than raw sector bytes; ReadSector remains the raw view.

func (*Disc) Track added in v0.3.0

func (d *Disc) Track(i int) (number int, typ string, frames int, pregap int, startLBA int, control uint8)

Track returns the TOC fields for track index i in [0, NumTracks).

func (*Disc) TrackIndex added in v0.4.0

func (d *Disc) TrackIndex(i, n int) (indexNumber int, lba int)

TrackIndex returns the nth index entry of track index i. n is a 0-based ordinal into the exposed list in [0, NumTrackIndexes(i)), not the index number; entry 0 is the lowest-numbered exposed index (normally INDEX 01). The returned lba is the absolute disc LBA of the index.

Directories

Path Synopsis
cmd
dischash command
Command dischash hashes a disc image's track data to match it against a reference (redump-style) breakdown.
Command dischash hashes a disc image's track data to match it against a reference (redump-style) breakdown.

Jump to

Keyboard shortcuts

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