forked from diskfs/go-diskfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fragment.go
51 lines (46 loc) · 1.01 KB
/
fragment.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
package squashfs
import (
"encoding/binary"
"fmt"
)
const (
fragmentEntriesPerBlock = 512
fragmentEntrySize = 16
)
type fragmentEntry struct {
start uint64
size uint32
compressed bool
}
type fragmentTable struct {
entries []*fragmentEntry
}
func (f *fragmentEntry) toBytes() []byte {
b := make([]byte, 16)
binary.LittleEndian.PutUint64(b[0:8], f.start)
size := f.size
if !f.compressed {
size |= (1 << 24)
}
binary.LittleEndian.PutUint32(b[8:12], size)
return b
}
func parseFragmentEntry(b []byte) (*fragmentEntry, error) {
target := 16
if len(b) < target {
return nil, fmt.Errorf("Mismatched fragment entry size, received %d bytes, less than minimum %d", len(b), target)
}
start := binary.LittleEndian.Uint64(b[0:8])
size := binary.LittleEndian.Uint32(b[8:12])
unCompFlag := uint32(1 << 24)
compressed := true
if size&unCompFlag == unCompFlag {
compressed = false
}
size &= 0xffffff
return &fragmentEntry{
size: size,
compressed: compressed,
start: start,
}, nil
}