Debian Codegraph @

lzip.go

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