vpk

package
v0.0.0-...-62eada6 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package vpk provides a reader for the Valve PacK archive format.

Index

Constants

View Source
const (
	// Magic is the "magic number" that every VPK directory must start with.
	Magic = 0x55aa1234
)

Variables

This section is empty.

Functions

func WriteDirectory

func WriteDirectory(w io.Writer, a *Archive) error

Types

type Archive

type Archive struct {
	Header
	Header2
	Files  []File
	Hashes []ChunkHash
	PubKey *rsa.PublicKey
}

Archive is the directory data read from a VPK.

func ReadArchive

func ReadArchive(r io.Reader) (*Archive, error)

ReadArchive reads and validates a VPK file.

Individual file and chunk hashes are not validated in this pass.

If a signed VPK is expected, check to make sure the PubKey field contains a trusted public key.

type ChunkHash

type ChunkHash struct {
	ArchiveIndex   uint32
	StartingOffset uint32
	Count          uint32
	MD5Checksum    [16]byte
}

ChunkHash is a hash of an arbitrary section of a VPK archive set.

func (*ChunkHash) Verify

func (h *ChunkHash) Verify(r io.ReaderAt) error

Verify returns a non-nil error if the ChunkHash does not match the actual data.

type DataChunk

type DataChunk struct {
	// A zero based index of the archive this file's data is contained in.
	// If 0x7fff, the data follows the directory.
	ArchiveIndex uint16

	// If ArchiveIndex is 0x7fff, the offset of the file data relative to
	// the end of the directory (see the header for more details).
	//
	// Otherwise, the offset of the data from the start of the specified
	// archive.
	EntryOffset uint32

	// If zero, the entire file is stored in the preload data.
	// Otherwise, the number of bytes stored starting at EntryOffset.
	EntryLength uint32
}

DataChunk is the location of a file's data.

type DirEntry

type DirEntry struct {
	// A 32bit CRC of the file's data.
	CRC uint32

	// The number of bytes contained in the index file.
	MetadataBytes uint16

	// The location of the contents of this file (minimum of 1 element).
	DataLocation []DataChunk
}

DirEntry is the raw VPK directory data.

type File

type File struct {
	Dir  string
	Base string
	Ext  string

	DirEntry

	Metadata []byte
	// contains filtered or unexported fields
}

File is metadata about a single file entry in a VPK.

func (*File) Bytes

func (f *File) Bytes(o *Opener) ([]byte, error)

Bytes reads the entire file into a slice of bytes.

func (*File) Name

func (f *File) Name() string

Name returns the full path of the File.

func (*File) Open

func (f *File) Open(o *Opener) (io.ReadCloser, error)

Open reads a file. Call the Close method to verify the file's checksum. The returned reader also implements io.ReaderAt.

func (*File) Size

func (f *File) Size() int

Size returns the total number of bytes in the file.

type Header struct {
	// Must be equal to the Magic constant in this package.
	Magic uint32

	// VPK file format version.
	Version uint32

	// The size, in bytes, of the directory tree.
	TreeSize uint32
}

Header is the VPK version 1 file format header.

type Header2

type Header2 struct {
	// How many bytes of file content are stored in this VPK file.
	EmbeddedChunkSize uint32

	// The size, in bytes, of the section containing MD5 checksums
	// for external archive content.
	ChunkHashesSize uint32

	// The size, in bytes, of the section containing MD5 checksums
	// for content in this file (should always be 48).
	SelfHashesSize uint32

	// The size, in bytes, of the section containing the public key
	// and signature.
	SignatureSize uint32
}

Header2 is the additional data that follows Header in VPK version 2.

type Opener

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

Opener caches file handles for a VPK.

func Dir

func Dir(prefix string) *Opener

Dir returns a new Opener with a given prefix.

func Single

func Single(path string) *Opener

Single returns a new Opener with a single-file VPK.

func (*Opener) Archive

func (o *Opener) Archive(index int) (*os.File, error)

Archive opens and returns prefix_###.vpk.

func (*Opener) Close

func (o *Opener) Close() error

Close closes all files that the Opener has opened, and returns the first error encountered.

func (*Opener) Dir

func (o *Opener) Dir() (*os.File, error)

Dir opens and returns prefix_dir.vpk.

func (*Opener) ReadArchive

func (o *Opener) ReadArchive() (*Archive, error)

ReadArchive reads the VPK archive tree for this Opener.

Jump to

Keyboard shortcuts

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