phargo

package module
v0.0.0-...-232554e Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 18 Imported by: 0

README

Phargo

Reader Php Phar files in golang

Info

Parser supports several signature algorithms:

  • md5
  • sha1
  • sha256
  • sha512
  • OpenSSL

Also supports compression formats:

  • None
  • Gzip
  • Bzip2

Can read manifest version, alias and metadata. For every file inside PHAR-archive can read it contents, name, timestamp and metadata. Checks file CRC and signature of entire archive.

Installation

  1. Download and install:
go get -u github.com/Sirherobrine23/phargo
  1. Import and use it:
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"

	"github.com/Sirherobrine23/phargo"
)

var pharFilePath = flag.String("file", "", "File path")

func main() {
	flag.Parse()

	// Open file
	file, err := os.Open(*pharFilePath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot open file: %s\n", err)
		os.Exit(1)
		return
	}

	// Get file size
	stat, _ := file.Stat()
	
	// Parse phar file
	pharInfo, err := phargo.NewReader(file, stat.Size())
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot parse file: %s\n", err)
		os.Exit(1)
		return
	}

	// Encode in json output
	js := json.NewEncoder(os.Stdout)
	js.SetIndent("", "  ")
	js.Encode(pharInfo)
}

Running the tests

Just run the command:

go test

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

Index

Constants

View Source
const (
	ManifestBitmapDeflate = 0x00001000
	ManifestBitmapBzip2   = 0x00002000

	EntryPermMask      = 0x000001FF
	EntryPermMask_usr  = 0x000001C0
	EntryPermShift_usr = 6
	EntryPermMask_grp  = 0x00000038
	EntryPermShift_grp = 3
	EntryPermMask_oth  = 0x00000007
	EntryPermDef_file  = 0x000001B6
	EntryPermDef_dir   = 0x000001FF

	CompressionMask      = 0xF000
	EntryCompressedNone  = 0x00000000
	EntryCompressedGzip  = 0x00001000
	EntryCompressedBzip2 = 0x00002000
)
View Source
const (
	SignatureMD5           = SignatureFlag(0x0001)
	SignatureSHA1          = SignatureFlag(0x0002)
	SignatureSHA256        = SignatureFlag(0x0003)
	SignatureSHA512        = SignatureFlag(0x0004)
	SignatureOpenSSL       = SignatureFlag(0x0010)
	SignatureOpenSSLSha256 = SignatureFlag(0x0011)
	SignatureOpenSSLSha512 = SignatureFlag(0x0012)
)

Variables

View Source
var (
	ErrOpenssl          = errors.New("openssl is disabled in this implementation")
	ErrInvalidSignature = errors.New("invalid signature")
	ErrGBMB             = errors.New("can't find GBMB constant at the end")
)

Functions

This section is empty.

Types

type File

type File struct {
	Filename         string
	Timestamp        time.Time
	Size             int64
	Flags            uint32
	SizeUncompressed int64
	SizeCompressed   int64
	CRC              uint32
	MetaSerialized   []byte
	// contains filtered or unexported fields
}

func ParseEntryManifest

func ParseEntryManifest(r io.ReaderAt, offset int64) (*File, int64, error)

Parse file entry manifest to struct

PHP Docs: https://www.php.net/manual/en/phar.fileformat.manifestfile.php

func (*File) FileInfo

func (file *File) FileInfo() fs.FileInfo

FileInfo returns an fs.FileInfo for the File.

func (File) Open

func (file File) Open() (io.ReadCloser, error)

Return file reader with descompression if compressed

type Manifest

type Manifest struct {
	Length        uint32
	EntitiesCount uint32
	Version       string
	Flags         uint32
	Alias         []byte
	AliasLength   uint32
	Metadata      []byte
	IsSigned      bool
}

func ParseManifest

func ParseManifest(r io.ReaderAt) (*Manifest, int64, error)

Parse phar menifest

PHP Docs: https://www.php.net/manual/en/phar.fileformat.phar.php

type Phar

type Phar struct {
	Menifest  *Manifest
	Signature *Signature
	Files     []*File
}

Parsed PHAR-file

func NewReader

func NewReader(r io.ReaderAt, size int64) (*Phar, error)

Parse phar file

func NewReaderFromFile

func NewReaderFromFile(file *os.File) (*Phar, error)

Parse phar file from *os.File

type Signature

type Signature struct {
	Signature SignatureFlag
	Hash      []byte
}

func GetSignature

func GetSignature(r io.ReaderAt, size int64) (*Signature, error)

Get phar signature

PHP Docs: https://www.php.net/manual/en/phar.fileformat.signature.php

Important Golang not support have in std openssl module, and return ErrOpenssl if presence of openssl signature

type SignatureFlag

type SignatureFlag uint32

func (SignatureFlag) MarshalText

func (sig SignatureFlag) MarshalText() (text []byte, err error)

func (SignatureFlag) String

func (sig SignatureFlag) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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