forked from tlahdekorpi/rpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lead.go
72 lines (60 loc) · 1.28 KB
/
lead.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
package rpm
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
)
var leadMagic = [...]byte{0xed, 0xab, 0xee, 0xdb}
type LeadType uint16
const (
LeadBinary LeadType = iota
LeadSource
)
type leadName [66]byte
func (l leadName) MarshalJSON() ([]byte, error) {
var i int
if i = bytes.IndexByte(l[:], 0); i == -1 {
i = len(l)
}
return json.Marshal(string(l[:i]))
}
func (l *leadName) UnmarshalJSON(b []byte) error {
var name string
if err := json.Unmarshal(b, &name); err != nil {
return err
}
copy((*l)[:], name)
return nil
}
type Lead struct {
Magic [4]byte
Major uint8
Minor uint8
Type LeadType
ArchNum uint16
Name leadName
OsNum uint16
SignatureType uint16
// pad to 96 bytes, 8 byte aligned
_ [16]byte
}
func NewLead(name string, lt LeadType) *Lead {
// defined as 5 in lib/rpmlead.c, 3.0 signature type
const headerSigType = 5
r := &Lead{
Magic: leadMagic,
Major: 3,
Minor: 0,
SignatureType: headerSigType,
Type: lt,
ArchNum: 1, // i386/x86_64
OsNum: 1, // linux
}
// defined again in the payload header
copy(r.Name[:], name)
return r
}
func (l *Lead) WriteTo(w io.Writer) (int64, error) {
return 96, binary.Write(w, binary.BigEndian, l)
}