Debian Codegraph @

lib/fruit/csblob/superblob.go

raw symbols SCIP
1 package csblob
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 )
8
9 type csMagic uint32
10
11 const (
12 csEmbeddedSignature csMagic = 0xfade0cc0
13 csDetachedSignature csMagic = 0xfade0cc1
14
15 csRequirement csMagic = 0xfade0c00
16 csRequirements csMagic = 0xfade0c01
17 csCodeDirectory csMagic = 0xfade0c02
18 csEntitlement csMagic = 0xfade7171
19 csEntitlementDER csMagic = 0xfade7172
20 csBlobWrapper csMagic = 0xfade0b01
21 )
22
23 // codedirectory.h
24 var csItypes = map[csMagic]uint32{
25 csRequirements: 0x2,
26 csEntitlement: 0x5,
27 csEntitlementDER: 0x7,
28 csBlobWrapper: 0x10000,
29 }
30
31 // codedirectory.h
32 //nolint:deadcode,varcheck // for doc purposes
33 const (
34 cdInfoSlot = 1
35 cdRequirementsSlot = 2
36 cdResourceDirSlot = 3
37 cdTopDirectorySlot = 4
38 cdEntitlementSlot = 5
39 cdRepSpecificSlot = 6
40 cdEntitlementDERSlot = 7
41
42 cdCodeDirectorySlot = 0
43 cdAlternateCodeDirectorySlots = 0x1000
44 cdSignatureSlot = 0x10000
45 cdIdentificationSlot = 0x10001
46 cdTicketSlot = 0x10002
47 )
48
49 type superItem struct {
50 magic csMagic
51 itype uint32
52 data []byte
53 }
54
55 func parseSuper(blob []byte) (magic csMagic, items []superItem, err error) {
56 if len(blob) < 12 {
57 return 0, nil, errShort
58 }
59 origLen := len(blob)
60 // read magic
61 magic = csMagic(binary.BigEndian.Uint32(blob))
62 length := binary.BigEndian.Uint32(blob[4:])
63 count := int(binary.BigEndian.Uint32(blob[8:]))
64 if length < 8 || length > uint32(len(blob)) {
65 return 0, nil, errors.New("invalid length in signature blob")
66 }
67 blob = blob[12:]
68 // read indexes
69 if len(blob) < 8*count {
70 return 0, nil, errShort
71 }
72 indexes, blob := blob[:8*count], blob[8*count:]
73 dataOffset := origLen - len(blob)
74 for i := 0; i < count; i++ {
75 itype := binary.BigEndian.Uint32(indexes[8*i:])
76 offset := int(binary.BigEndian.Uint32(indexes[4+8*i:]))
77 offset -= dataOffset
78 if offset > len(blob)-8 {
79 return 0, nil, errShort
80 }
81 length := int(binary.BigEndian.Uint32(blob[offset+4:]))
82 if offset+length > len(blob) {
83 return 0, nil, errShort
84 }
85 items = append(items, superItem{
86 magic: csMagic(binary.BigEndian.Uint32(blob[offset:])),
87 itype: itype,
88 data: blob[offset : offset+length],
89 })
90 }
91 return magic, items, nil
92 }
93
94 func newSuperItem(magic csMagic, payload []byte) superItem {
95 packed := make([]byte, 8+len(payload))
96 binary.BigEndian.PutUint32(packed, uint32(magic))
97 binary.BigEndian.PutUint32(packed[4:], uint32(len(payload)+8))
98 copy(packed[8:], payload)
99 return superItem{
100 magic: magic,
101 itype: csItypes[magic],
102 data: packed,
103 }
104 }
105
106 func marshalSuperBlob(magic csMagic, items []superItem) []byte {
107 ints := make([]uint32, 3+2*len(items))
108 ints[0] = uint32(magic)
109 length := uint32(4 * len(ints))
110 ints[2] = uint32(len(items))
111 for i, item := range items {
112 ints[3+2*i] = item.itype
113 ints[4+2*i] = length
114 length += uint32(len(item.data))
115 }
116 ints[1] = length
117 b := bytes.NewBuffer(make([]byte, 0, length))
118 _ = binary.Write(b, binary.BigEndian, ints)
119 for _, item := range items {
120 b.Write(item.data)
121 }
122 return b.Bytes()
123 }
124
125 var errShort = errors.New("short read in signature blob")