-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzip.go
76 lines (58 loc) · 1.47 KB
/
lzip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-FileCopyrightText: 2024 Shun Sakai
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Package lzip implements reading and writing of lzip format compressed files.
// The package supports version 1 of the specification.
//
// See the following for the specification:
//
// - https://www.nongnu.org/lzip/manual/lzip_manual.html#File-format
// - https://datatracker.ietf.org/doc/html/draft-diaz-lzip-09#section-2
package lzip
import (
"math/bits"
"github.com/ulikunitz/xz/lzma"
)
const (
headerSize = 6
trailerSize = 20
)
const magic = "LZIP"
const magicSize = 4
type version byte
const (
version0 version = iota
version1
)
// MinDictSize is the minimum dictionary size, which is 4 KiB.
const MinDictSize = lzma.MinDictCap
// MaxDictSize is the maximum dictionary size, which is 512 MiB.
const MaxDictSize = 1 << 29
// DefaultDictSize is the default dictionary size, which is 8 MiB.
const DefaultDictSize = 1 << 23
// MaxMemberSize is the maximum member size, which is 2 PiB.
const MaxMemberSize = 1 << 51
type header struct {
magic [magicSize]byte
version
dictSize byte
}
func newHeader(dictSize uint32) *header {
ds := bits.Len32(dictSize - 1)
if dictSize > MinDictSize {
base := 1 << dictSize
frac := base / 16
for i := 7; i >= 1; i-- {
if (base - (i * frac)) >= ds {
ds |= i << 5
}
}
}
z := &header{[magicSize]byte([]byte(magic)), version1, byte(ds)}
return z
}
type trailer struct {
crc uint32
dataSize uint64
memberSize uint64
}