archivetest

package
v0.2.51 Latest Latest
Warning

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

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

Documentation

Overview

Package archivetest builds small RAR 4.x / RAR 5.0 archives (and Copy-mode 7z archives) for tests. It is for tests only: production code must not import it.

The archives are structurally valid with correct header CRCs. File data is written verbatim: Method, Encrypted and Solid only set the corresponding header flags (nothing is compressed or encrypted), which is all a header parser can observe.

Index

Constants

View Source
const (

	// RAR 4.x main header flags (for RAR4MainHeader).
	RAR4MainVolume       = 0x0001
	RAR4MainSolid        = 0x0008
	RAR4MainNewNumbering = 0x0010
	RAR4MainPassword     = 0x0080
	RAR4MainFirstVolume  = 0x0100
)

RAR 4.x constants.

Variables

View Source
var (
	RAR4Signature = []byte("Rar!\x1a\x07\x00")
	RAR5Signature = []byte("Rar!\x1a\x07\x01\x00")
)

Signatures.

View Source
var ErrVolumeTooSmall = errors.New("archivetest: volume size too small")

ErrVolumeTooSmall is returned when VolumeSize cannot hold a header.

Functions

func Build7zCopy

func Build7zCopy(files []SevenZipFile, solid bool) []byte

Build7zCopy writes a 7z archive with a plain (unencoded) header whose non-empty files live in Copy folders: one folder holding every file as a substream when solid (7-Zip itself never does that for Copy), else one folder per file.

func BuildRAR

func BuildRAR(files []RARFile, opts RAROptions) ([][]byte, error)

BuildRAR returns the bytes of each volume in order.

func Number7z

func Number7z(v uint64) []byte

Number7z encodes a 7z NUMBER.

func RAR4Block

func RAR4Block(typ byte, flags uint16, body []byte) []byte

RAR4Block serialises a RAR 4.x block with a correct HEAD_SIZE and CRC.

func RAR4EncodeName

func RAR4EncodeName(name string) ([]byte, bool)

RAR4EncodeName returns the RAR 4.x name field for name and whether it needs LHD_UNICODE. Non-ASCII names get an OEM fallback, a NUL and RAR's compact UTF-16 encoding (using all four opcodes: run copy, low byte, high-byte page and full 16-bit).

func RAR4EndHeader

func RAR4EndHeader(more bool, volNum int) []byte

RAR4EndHeader returns a RAR 4.x end-of-archive block; volNum < 0 omits the volume number.

func RAR4FileHeader

func RAR4FileHeader(h Header) []byte

RAR4FileHeader returns a RAR 4.x file header (or new sub-block if h.Service). Names containing non-ASCII characters are written with RAR's unicode name encoding.

func RAR4MainHeader

func RAR4MainHeader(flags uint16) []byte

RAR4MainHeader returns a RAR 4.x main archive header (RAR4Main* flags).

func RAR5Block

func RAR5Block(typ, flags uint64, fields, extra []byte, hasData bool, dataSize int64) []byte

RAR5Block serialises a RAR 5.0 block: CRC32, HeaderSize, type, flags, optional extra-area and data sizes, the type-specific fields and the extra area.

func RAR5EndHeader

func RAR5EndHeader(more bool) []byte

RAR5EndHeader returns a RAR 5.0 end-of-archive header.

func RAR5FileHeader

func RAR5FileHeader(h Header) []byte

RAR5FileHeader returns a RAR 5.0 file header (or service header if h.Service). A file-time extra record is always present; Encrypted adds a file encryption record.

func RAR5MainHeader

func RAR5MainHeader(multi bool, volNum int, solid bool) []byte

RAR5MainHeader returns a RAR 5.0 main archive header. volNum > 0 writes the volume-number field (absent in the first volume).

func RAR5Record

func RAR5Record(typ uint64, data []byte) []byte

RAR5Record serialises one extra-area record.

func Vint

func Vint(v uint64) []byte

Vint encodes a RAR 5.0 variable-length integer.

Types

type Header struct {
	Name        string
	PackSize    int64 // data bytes following the header in this volume
	UnpackSize  int64 // whole-file size
	Method      int   // 0 = store .. 5 (RAR4 writes 0x30+Method)
	CRC         uint32
	SplitBefore bool
	SplitAfter  bool
	Encrypted   bool
	Solid       bool
	Dir         bool
	UnknownSize bool // unpacked size unknown
	Large       bool // RAR4: write the 64-bit size fields even if not needed
	UTF8Name    bool // RAR4: set LHD_UNICODE and store the name as plain UTF-8
	Service     bool // RAR4 new sub-block (0x7a) / RAR5 service header (type 3)
	// contains filtered or unexported fields
}

Header describes one RAR file header (a whole file or one volume's part of it) for RAR4FileHeader / RAR5FileHeader.

type RARFile

type RARFile struct {
	Name        string // '/'-separated; RAR4 writes it as given
	Data        []byte
	Method      int // 0 = store; RAR4 writes 0x30+Method
	Encrypted   bool
	Solid       bool
	Dir         bool
	UnknownSize bool // header says the unpacked size is unknown
	Large       bool // RAR4: 64-bit size fields
}

RARFile is one input file for BuildRAR.

type RAROptions

type RAROptions struct {
	Version int // 4 or 5 (0 means 5)
	// VolumeSize is the size of every volume except the last, like rar -v.
	// 0 builds a single (non-volume) archive. Each volume holds headers,
	// as much file data as fits, and the end-of-archive block. A volume is
	// closed early only when the next file header plus one data byte would
	// not fit; a volume ending in a split file is always exactly
	// VolumeSize (RAR5 pads the DataSize vint if a vint length step would
	// otherwise leave a byte over).
	VolumeSize      int64
	HeaderEncrypted bool // RAR4 MHD_PASSWORD / RAR5 archive encryption header (single volume, garbage after it)
	SolidArchive    bool // archive-level solid flag
	NewNumbering    bool // RAR4 MHD_NEWNUMBERING
	// OmitEndVolumeNumber leaves EARC_VOLNUMBER out of RAR4 end blocks
	// (RAR 3.x+ writes it in volumes).
	OmitEndVolumeNumber bool
	// ServiceBlocks adds blocks a parser must skip after the main header:
	// a comment service header with data, an old-style block (RAR4) or an
	// unknown block type (RAR5).
	ServiceBlocks bool
	OmitEndBlock  bool
}

RAROptions controls BuildRAR.

type SevenZipFile

type SevenZipFile struct {
	Name string
	Data []byte
	Dir  bool
}

SevenZipFile is one input file for Build7zCopy. Empty Data without Dir is an empty file.

Jump to

Keyboard shortcuts

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