Documentation
¶
Index ¶
Constants ¶
const ( DefaultPoolBuffer int64 = 4 * 1024 // 4KB ExtractBuffer int64 = 64 * 1024 // 64KB MaxPoolBuffer int64 = 128 * 1024 // 128KB MaxBytes int64 = 1 << 32 // 4096MB ReadBuffer int64 = 64 * 1024 // 64KB ZipBuffer int64 = 2 * 1024 // 2KB DefaultMaxArchiveBytes int64 = 32 << 30 // 32GiB total uncompressed across all entries DefaultMaxArchiveRatio float64 = 100 // uncompressed/input expansion ceiling )
common values used across malcontent for extracting and reading files.
Variables ¶
var ErrArchiveBytesCap = errors.New("archive total uncompressed bytes exceeded")
ErrArchiveBytesCap is returned by ArchiveCounter.Add once the running total of uncompressed bytes would exceed ArchiveCounter.MaxBytes (seeded from DefaultMaxArchiveBytes when the caller supplies no override). Extractors wrap this sentinel and abort the in-flight extraction.
var ErrArchiveRatioCap = errors.New("archive expansion ratio exceeded")
ErrArchiveRatioCap is returned by ArchiveCounter.Add once the running total of uncompressed bytes exceeds InputBytes * MaxRatio.
Functions ¶
func GetContents ¶
GetContents takes a file, reads its contents, and returns them as a slice of bytes. If a file was stat'd as small but grew past the small ceiling between stat and read, the read spills through to the large (up-to-MaxBytes) path so content is never silently truncated.
Types ¶
type ArchiveCounter ¶ added in v1.24.0
type ArchiveCounter struct {
Total atomic.Int64
MaxBytes int64 // 0 = unlimited
MaxRatio float64 // <= 0 = unlimited; ratio measured against InputBytes
InputBytes int64 // size of the outer archive blob; 0 disables ratio check
// contains filtered or unexported fields
}
ArchiveCounter accumulates uncompressed bytes written by an extractor and enforces a byte cap and an expansion-ratio cap. A zero value disables a cap; a nil receiver disables accounting entirely so callers may opt out.
Total is updated with atomic semantics so concurrent extractor goroutines (e.g., the zip errgroup fan-out) may share a single counter without locks.
func (*ArchiveCounter) Add ¶ added in v1.24.0
func (c *ArchiveCounter) Add(n int) error
Add records additional uncompressed bytes against the counter. A nil receiver is a documented no-op so call sites can pass a nil counter to opt out without nil-checking. The byte-cap and ratio-cap guards are evaluated after the atomic increment; this preserves a single source of truth for Total under concurrent writers.
func (*ArchiveCounter) Remaining ¶ added in v1.24.0
func (c *ArchiveCounter) Remaining() int64
Remaining returns the number of bytes still available under the byte cap. A nil receiver or a zero MaxBytes (unlimited) returns MaxInt64 so callers can unconditionally use min(Remaining(), otherLimit) without nil checks.