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

par2

package
v0.2.2 Latest Latest
Warning

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

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

Documentation

Overview

Package par2 parses the metadata packets of PAR2 2.0 files.

Ether never repairs with PAR2: a recovery block is a linear combination of every slice of the set, so rebuilding one missing article would mean reading the whole file, which defeats streaming. What it does use is the cheap part of a PAR2 set, the index file: File Description packets give every file's real name, exact length, MD5 of the whole file and MD5 of its first 16 KiB, and Input File Slice Checksum packets give the MD5 and CRC32 of each slice. That lets the stream engine name obfuscated posts from the first article of each file and check that what it serves is the content the poster hashed.

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"
)

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 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 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 ID

type ID [16]byte

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

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 Set

type Set struct {
	// 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) 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.

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