par2

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const HashSize = 16

HashSize is the size of the PAR2-contained hashes. These are MD5 hashes used for various purposes within packets.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	Name string `json:"name"` // Name of the PAR2 file

	// Sets represents the datasets of different set IDs,
	// though commonly there is only one dataset in a PAR2 file.
	Sets []Set `json:"sets"`
}

File represents a single parsed PAR2 file.

func ParseFile

func ParseFile(ctx context.Context, fsys afero.Fs, path string, panicAsErr bool) (p *File, e error)

ParseFile parses a PAR2 file into a File structure. panicAsErr controls if a panic should be returned as ParserPanicError. Beware the PAR2-specific packet and error handling as described in Parse.

type FilePacket

type FilePacket struct {
	SetID       Hash   `json:"set_id"`       // [Set] the packet belongs to
	FileID      Hash   `json:"file_id"`      // ID of the file (MD5)
	Name        string `json:"name"`         // Filename of the file
	Size        int64  `json:"size"`         // Size of the file
	Hash        Hash   `json:"hash"`         // MD5 hash of entire file
	Hash16k     Hash   `json:"hash_16k"`     // MD5 hash of first 16KB
	FromUnicode bool   `json:"from_unicode"` // Name came from a Unicode packet
}

FilePacket represents a PAR2 file description packet.

type FileSet added in v0.1.6

type FileSet struct {
	Files      []File `json:"files"`       // Unmerged PAR2 files and their sets
	SetsMerged []Set  `json:"sets_merged"` // Merged information of all PAR2 files
}

FileSet represents a set of multiple PAR2 File It includes a merged Set slice of their combined information.

type Hash

type Hash [HashSize]byte

Hash is the type used for all PAR2-contained hashes. According to the specification, it is a [16]byte array.

func (*Hash) MarshalJSON

func (h *Hash) MarshalJSON() ([]byte, error)

MarshalJSON marshals into a JSON byte slice.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON byte slice.

type MainPacket

type MainPacket struct {
	SetID          Hash   `json:"set_id"`           // [Set] the packet belongs to
	SliceSize      uint64 `json:"slice_size"`       // Recovery slice size
	RecoveryIDs    []Hash `json:"recovery_ids"`     // Protected (recovery) IDs
	NonRecoveryIDs []Hash `json:"non_recovery_ids"` // Auxiliary (non-recovery) IDs
}

MainPacket represents a PAR2 main packet.

func (*MainPacket) Copy added in v0.1.6

func (m *MainPacket) Copy() *MainPacket

Copy creates a deep copy of the MainPacket (returning nil on nil reciver).

func (*MainPacket) Equal added in v0.1.6

func (m *MainPacket) Equal(other *MainPacket) bool

Equal checks if two MainPackets are equal. Returns true if both are nil, false if only one is nil.

type ParserPanicError

type ParserPanicError struct {
	Value any
	Stack []byte
}

ParserPanicError is an error that is returned on a recovered parsing panic. It contains the value of the panic and a byte slice containing a stack trace.

func (*ParserPanicError) Error

func (e *ParserPanicError) Error() string

Error returns as string the error message.

type Set

type Set struct {
	SetID          Hash         `json:"set_id"`                // Dataset ID
	MainPacket     *MainPacket  `json:"main_packet,omitempty"` // Main packet (can be nil)
	RecoverySet    []FilePacket `json:"recovery_set"`          // Protected (recovery) files
	NonRecoverySet []FilePacket `json:"non_recovery_set"`      // Auxiliary (non-recovery) files

	// StrayPackets are packets that have the right set ID,
	// but whose file ID is not listed within the PAR2 [MainPacket].
	StrayPackets []FilePacket `json:"stray_packets"`

	// MissingRecoveryPackets are recovery files that have their
	// file ID in the PAR2 [MainPacket], but have not been found.
	MissingRecoveryPackets []Hash `json:"missing_recovery_packets"`

	// MissingNonRecoveryPackets are non-recovery files that have
	// their file ID in the PAR2 [MainPacket], but have not been found.
	MissingNonRecoveryPackets []Hash `json:"missing_non_recovery_packets"`
}

Set represents a dataset with unique set ID.

func Parse

func Parse(ctx context.Context, r io.ReadSeeker, checkMD5 bool) ([]Set, error)

Parse reads PAR2 data and returns a slice of Set in the order they appeared. In compliance with the specification, unparseable packets are silently skipped. Unless there is a fatal error, no parseable packets will return an empty slice. It parses: MainPacket, FilePacket and UnicodePacket, skipping all others.

type UnicodePacket

type UnicodePacket struct {
	SetID  Hash   `json:"set_id"`  // [Set] the packet belongs to
	FileID Hash   `json:"file_id"` // ID of the file (MD5)
	Name   string `json:"name"`    // Unicode name of the file
}

UnicodePacket represents a PAR2 unicode file description packet.

Jump to

Keyboard shortcuts

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