Help us shape the future of Go! Take the 2026 Go Developer Survey

par2

package
v0.2.49 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package par2 parses PAR2 2.0 metadata and performs bounded GF16 recovery. File descriptions name obfuscated posts and authenticate source slices. recovery.go reconstructs selected byte intervals, verifies complete target slices, and can subtract retained summaries of already verified input.

The parser is defensive: packets are located by scanning for the magic, every packet's MD5 is verified, corrupt, truncated and unknown packets are skipped and repeated packets (PAR2 files repeat critical packets) are de-duplicated.

Index

Constants

View Source
const (
	TypeMain     = "PAR 2.0\x00Main\x00\x00\x00\x00"
	TypeFileDesc = "PAR 2.0\x00FileDesc"
	TypeIFSC     = "PAR 2.0\x00IFSC\x00\x00\x00\x00"
	TypeCreator  = "PAR 2.0\x00Creator\x00"
	TypeUniName  = "PAR 2.0\x00UniFileN"
	TypeRecovery = "PAR 2.0\x00RecvSlic"
)

Packet types.

View Source
const Hash16kSize = 16 * 1024

Hash16kSize is how many leading bytes the "first 16k" MD5 covers.

View Source
const HeaderSize = 64

HeaderSize is the size of a packet header.

View Source
const Magic = "PAR2\x00PKT"

Magic starts every PAR2 packet.

Variables

View Source
var ErrErasure = errors.New("par2: missing or corrupt range")
View Source
var ErrInsufficient = errors.New("par2: insufficient independent recovery equations")
View Source
var ErrNoFiles = errors.New("par2: no valid file description packets")

ErrNoFiles is returned by Parse when the data holds no valid File Description packet.

Functions

func Hash16k

func Hash16k(prefix []byte, length int64) ([16]byte, bool)

Hash16k returns the MD5 PAR2 records for a file of the given length whose leading bytes are prefix, and whether prefix is long enough to compute it.

Types

type Block added in v0.2.8

type Block struct {
	File           *File
	Offset, Length int64
	Check          Slice
}

func (Block) Verify added in v0.2.8

func (b Block) Verify(data []byte) bool

type Field added in v0.2.8

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

Field is PAR2's GF(2^16), constructed explicitly (no mutable global tables).

func NewField added in v0.2.8

func NewField() *Field

func (*Field) Coefficient added in v0.2.8

func (f *Field) Coefficient(block int, exponent uint32) uint16

func (*Field) Mul added in v0.2.8

func (f *Field) Mul(a, b uint16) uint16

type File

type File struct {
	SetID ID
	ID    ID
	// Name is the file name as stored (the Unicode name when the set has
	// one). It may contain a relative path; use path.Base for display.
	Name    string
	Length  int64
	MD5     [16]byte // MD5 of the whole file
	MD5_16k [16]byte // MD5 of the first min(16 KiB, Length) bytes
	// SliceSize is the recovery set's slice size, 0 if the Main packet was
	// not seen. Slices are the IFSC checksums, nil if absent.
	SliceSize int64
	Slices    []Slice
}

File is one file of a recovery set.

func (*File) BaseName

func (f *File) BaseName() string

BaseName is the file name without any directory part.

func (*File) Verify

func (f *File) Verify(prefix []byte, size int64) (checks int, err error)

Verify checks a file's leading bytes (prefix) and, when known (> 0), its size against the recorded checksums: the length, the first-16k MD5, every slice prefix fully covers (CRC32 and MD5) and, when prefix is the whole file, its MD5. It returns how many checks ran (0 when prefix is too short to check anything) and a *MismatchError on the first failure.

type Hole added in v0.2.8

type Hole struct {
	Block      int
	Begin, End int64
}

Hole is a known unavailable byte interval within an input slice.

type ID

type ID [16]byte

ID is a 16-byte PAR2 identifier (recovery set or file ID).

type Layout added in v0.2.8

type Layout struct {
	ID        ID
	SliceSize int64
	Blocks    []Block
}

func (*Layout) Recover added in v0.2.8

func (l *Layout) Recover(ctx context.Context, f *Field, holes []Hole, read ReadRange, parity []Recovery, s *Summaries, opts RepairOptions) (map[int][]byte, RepairStats, error)

Recover reconstructs and verifies complete target slices, but reads only the damaged intervals from other slices. Helpers missing at the same interval become additional unknowns. Nothing is returned until ALL targets verify.

type Main added in v0.2.8

type Main struct {
	SliceSize int64
	Files     []ID
	Invalid   bool
}

Main describes only the protected files, in PAR2 coefficient order.

type MismatchError

type MismatchError struct {
	Name   string
	What   string // "length", "first 16 KiB MD5", "slice N MD5", "slice N CRC32", "file MD5"
	Detail string
}

MismatchError says which PAR2 checksum the content failed.

func (*MismatchError) Error

func (e *MismatchError) Error() string

type ReadRange added in v0.2.8

type ReadRange func(context.Context, int, int64, []byte) error

ReadRange reads exactly len(dst) bytes within one padded input slice. Missing or corrupt data must wrap ErrErasure; transport failures must not.

type Recovery added in v0.2.8

type Recovery struct {
	Exponent uint32
	Data     []byte
}

Recovery contains one fully checksummed packet's recovery payload.

func RecoveryPackets added in v0.2.8

func RecoveryPackets(data []byte, id ID, size int64, limit int) []Recovery

RecoveryPackets tolerates lost packets, but never returns unverified payloads.

type RecoveryScanner added in v0.2.8

type RecoveryScanner struct {
	ID        ID
	SliceSize int64
	// contains filtered or unexported fields
}

RecoveryScanner accepts consecutive decoded article bytes and releases valid recovery packets incrementally. Reset at every unavailable/nonconsecutive article; complete packets on either side of a hole remain usable.

func (*RecoveryScanner) Push added in v0.2.8

func (s *RecoveryScanner) Push(data []byte, limit int) []Recovery

func (*RecoveryScanner) Reset added in v0.2.8

func (s *RecoveryScanner) Reset()

type RepairOptions added in v0.2.8

type RepairOptions struct {
	WholeSlices bool
	ChunkBytes  int
	MaxTargets  int
}

type RepairStats added in v0.2.8

type RepairStats struct {
	InputBytes     int64
	SummaryBytes   int64
	RecoveredBytes int64
}

type Set

type Set struct {
	// Main retains the protected-file order required by the recovery equations.
	Main map[ID]Main
	// Files in order of first appearance.
	Files []*File
	// Creator is the first Creator packet's text, for diagnostics.
	Creator string
	// Packets counts valid, distinct packets; Skipped counts packet
	// headers that were corrupt, truncated or of an unusable type.
	Packets int
	Skipped int
}

Set is everything usable found in one or more PAR2 files. Files from several recovery sets may be present.

func Parse

func Parse(data []byte) (*Set, error)

Parse scans data (one or more concatenated PAR2 files, possibly with junk or gaps from missing articles) for packets. It returns ErrNoFiles if no valid File Description packet is found.

func (*Set) ByLength

func (s *Set) ByLength(n int64) []*File

ByLength returns the files of exactly n bytes.

func (*Set) Identify

func (s *Set) Identify(prefix []byte, size int64) (f *File, byHash bool)

Identify finds the file whose content starts with prefix and, if size > 0, is size bytes long. The first-16k MD5 decides when prefix covers it (and must then be unique among the files of that length); otherwise, or when no hash matches, a unique exact length does. byHash reports which rule matched. A file found by length alone whose 16k hash did not match is still returned, so the caller can name it and report the mismatch.

func (*Set) Match16k

func (s *Set) Match16k(sum [16]byte) []*File

Match16k returns the files whose first-16k MD5 is sum.

func (*Set) Named

func (s *Set) Named(name string) []*File

Named returns the files whose base name is name: exact matches, or, if there are none, case-insensitive ones.

func (*Set) RecoveryLayout added in v0.2.8

func (s *Set) RecoveryLayout(id ID, maxSlice int64) (*Layout, error)

RecoveryLayout rejects incomplete metadata rather than guessing coefficient order, treating unprotected files as protected, or allocating from huge sizes.

func (*Set) SortedNames

func (s *Set) SortedNames() []string

SortedNames lists the base names of the set's files, sorted (for logs).

type Slice

type Slice struct {
	MD5   [16]byte
	CRC32 uint32
}

Slice is the checksum pair of one input slice. The last slice of a file is hashed zero-padded to the slice size.

type Summaries added in v0.2.8

type Summaries struct {
	Exponents []uint32
	Data      [][]byte
	Seen      []bool
	// contains filtered or unexported fields
}

Summaries retain verified contributions to fixed recovery equations. Caller serializes access. A coverage bit is committed only after the contribution. Duplicate observations are harmless. No raw source data is retained here.

func NewSummaries added in v0.2.8

func NewSummaries(l *Layout, f *Field, exponents []uint32, maxBytes int64) (*Summaries, error)

func (*Summaries) Observe added in v0.2.8

func (s *Summaries) Observe(block int, data []byte) bool

Directories

Path Synopsis
Package par2test builds PAR2 2.0 metadata packets and index files for tests.
Package par2test builds PAR2 2.0 metadata packets and index files for tests.

Jump to

Keyboard shortcuts

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