forked from google/rpmpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
630 lines (541 loc) · 16.1 KB
/
header.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpmpack
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"reflect"
"sort"
"time"
)
const (
signatures = 0x3e
immutable = 0x3f
typeInt16 = 0x03
typeInt32 = 0x04
typeString = 0x06
typeBinary = 0x07
typeStringArray = 0x08
typei18nString = 0x09
)
// Only integer types are aligned. This is not just an optimization - some versions
// of rpm fail when integers are not aligned. Other versions fail when non-integers are aligned.
var boundaries = map[int]int{
typeInt16: 2,
typeInt32: 4,
}
type IndexEntry struct {
rpmtype, count int
data []byte
}
func (e IndexEntry) indexBytes(tag, contentOffset int) []byte {
b := &bytes.Buffer{}
if err := binary.Write(b, binary.BigEndian, []int32{int32(tag), int32(e.rpmtype), int32(contentOffset), int32(e.count)}); err != nil {
// binary.Write can fail if the underlying Write fails, or the types are invalid.
// bytes.Buffer's write never error out, it can only panic with OOM.
panic(err)
}
return b.Bytes()
}
func (e IndexEntry) toString() (string, error) {
if e.rpmtype != typeString && e.rpmtype != typei18nString {
return "", fmt.Errorf("rpmtype %d is not a string type", e.rpmtype)
}
return string(e.data[:len(e.data)-1]), nil
}
var IndexEntryToString = IndexEntry.toString
func (e IndexEntry) toUint16() (uint16, error) {
if e.rpmtype != typeInt16 {
return 0, fmt.Errorf("rpmtype %d is not a uint16 type", e.rpmtype)
}
b := &bytes.Buffer{}
b.Write(e.data)
value := uint16(0)
binary.Read(b, binary.BigEndian, &value)
return value, nil
}
func (e IndexEntry) toUint32() (uint32, error) {
if e.rpmtype != typeInt32 {
return 0, fmt.Errorf("rpmtype %d is not a uint32 type", e.rpmtype)
}
b := &bytes.Buffer{}
b.Write(e.data)
value := uint32(0)
binary.Read(b, binary.BigEndian, &value)
return value, nil
}
func (e *index) toRelations(nameTag int, versionTag int, flagsTag int) (Relations, error) {
names, err := popTag(e.entries, nameTag, IndexEntry.toStringArray)
if err != nil {
return nil, fmt.Errorf("failed to find name tag %d %w", nameTag, err)
}
versions, err := popTag(e.entries, versionTag, IndexEntry.toStringArray)
if err != nil {
return nil, fmt.Errorf("failed to find versions tag %d %w", versionTag, err)
}
flags, err := popTag(e.entries, flagsTag, IndexEntry.toInt32Array)
if err != nil {
return nil, fmt.Errorf("failed to find flags tag %d %w", flagsTag, err)
}
if (names == nil && versions == nil && flags == nil) {
return nil, nil
}
if (names == nil || versions == nil || flags == nil) {
return nil, fmt.Errorf("one of names versions or flags is nil %v %v %v", names, versions, flags)
}
if len(names) != len(versions) || len(names) != len(flags) {
return nil, fmt.Errorf("missmatch in counts %d %d %d", len(names), len(versions), len(flags))
}
out := make(Relations, len(names))
for i := range names {
sense, err := SenseFromFlag(flags[i])
if err != nil {
return nil, fmt.Errorf("failed to parse sense at %d: %w", i, err)
}
out[i] = &Relation{
Name: names[i],
Version: versions[i],
Sense: sense,
}
}
return out, nil
}
func (e IndexEntry) toTime() (time.Time, error) {
val, err := e.toUint32()
if err != nil {
return time.Time{}, err
}
return time.Unix(int64(val), 0), nil
}
func (e IndexEntry) toStringArray() ([]string, error) {
if e.rpmtype != typeStringArray {
return nil, fmt.Errorf("rpmtype %d is not a string array type", e.rpmtype)
}
data := e.data
out := make([]string, e.count)
for i := 0; i < e.count; i++ {
end := bytes.IndexByte(data, '\x00')
if end > -1 {
out[i] = string(data[:end])
data = data[end+1:]
} else {
out[i] = string(data)
data = nil
}
}
return out, nil
}
func (e IndexEntry) toInt32Array() ([]int32, error) {
if e.rpmtype != typeInt32 {
return nil, fmt.Errorf("rpmtype %d is not an int type", e.rpmtype)
}
out := make([]int32, e.count)
b := &bytes.Buffer{}
b.Write(e.data)
binary.Read(b, binary.BigEndian, &out)
return out, nil
}
func (e IndexEntry) toUint32Array() ([]uint32, error) {
if e.rpmtype != typeInt32 {
return nil, fmt.Errorf("rpmtype %d is not an int type", e.rpmtype)
}
out := make([]uint32, e.count)
b := &bytes.Buffer{}
b.Write(e.data)
binary.Read(b, binary.BigEndian, &out)
return out, nil
}
func (e IndexEntry) toUint16Array() ([]uint16, error) {
if e.rpmtype != typeInt16 {
return nil, fmt.Errorf("rpmtype %d is not an int type", e.rpmtype)
}
out := make([]uint16, e.count)
b := &bytes.Buffer{}
b.Write(e.data)
binary.Read(b, binary.BigEndian, &out)
return out, nil
}
func (e IndexEntry) toInt16Array() ([]int16, error) {
if e.rpmtype != typeInt16 {
return nil, fmt.Errorf("rpmtype %d is not an int type", e.rpmtype)
}
out := make([]int16, e.count)
b := &bytes.Buffer{}
b.Write(e.data)
binary.Read(b, binary.BigEndian, &out)
return out, nil
}
func (e *IndexEntry) setData(data []byte) {
e.data = data
}
func readIndex(inp io.Reader) (int32, int32, *IndexEntry, error) {
indexData := []int32{0, 0, 0, 0} // tag rpmtype contentOffset count
err := binary.Read(inp, binary.BigEndian, indexData)
if err != nil {
return 0, 0, nil, fmt.Errorf("failed to read index data: %w", err)
}
out := IndexEntry{
rpmtype: int(indexData[1]),
count: int(indexData[3]),
}
return indexData[0], indexData[2], &out, nil
}
func intEntry(rpmtype, size int, value interface{}) IndexEntry {
b := &bytes.Buffer{}
if err := binary.Write(b, binary.BigEndian, value); err != nil {
// binary.Write can fail if the underlying Write fails, or the types are invalid.
// bytes.Buffer's write never error out, it can only panic with OOM.
panic(err)
}
return IndexEntry{rpmtype, size, b.Bytes()}
}
func EntryInt16(value []int16) IndexEntry {
return intEntry(typeInt16, len(value), value)
}
func EntryUint16(value []uint16) IndexEntry {
return intEntry(typeInt16, len(value), value)
}
func EntryInt32(value []int32) IndexEntry {
return intEntry(typeInt32, len(value), value)
}
func EntryUint32(value []uint32) IndexEntry {
return intEntry(typeInt32, len(value), value)
}
func EntryString(value string) IndexEntry {
return IndexEntry{typeString, 1, append([]byte(value), byte(00))}
}
func EntryBytes(value []byte) IndexEntry {
return IndexEntry{typeBinary, len(value), value}
}
func EntryStringSlice(value []string) IndexEntry {
b := [][]byte{}
for _, v := range value {
b = append(b, []byte(v))
}
bb := append(bytes.Join(b, []byte{00}), byte(00))
return IndexEntry{typeStringArray, len(value), bb}
}
type index struct {
entries map[int]IndexEntry
h int
}
func newIndex(h int) *index {
return &index{entries: make(map[int]IndexEntry), h: h}
}
func (i *index) Add(tag int, e IndexEntry) {
i.entries[tag] = e
}
func (i *index) AddEntries(m map[int]IndexEntry) {
for t, e := range m {
i.Add(t, e)
}
}
func (i *index) sortedTags() []int {
t := []int{}
for k := range i.entries {
t = append(t, k)
}
sort.Ints(t)
return t
}
func (i *index) Equals(o *index) bool {
return i.h == o.h &&
reflect.DeepEqual(i.entries, o.entries)
}
func pad(w *bytes.Buffer, rpmtype, offset int) {
// We need to align integer entries...
if b, ok := boundaries[rpmtype]; ok && offset%b != 0 {
if _, err := w.Write(make([]byte, b-offset%b)); err != nil {
// binary.Write can fail if the underlying Write fails, or the types are invalid.
// bytes.Buffer's write never error out, it can only panic with OOM.
panic(err)
}
}
}
// Bytes returns the bytes of the index.
func (i *index) Bytes() ([]byte, error) {
w := &bytes.Buffer{}
// Even the header has three parts: The lead, the index entries, and the entries.
// Because of alignment, we can only tell the actual size and offset after writing
// the entries.
entryData := &bytes.Buffer{}
tags := i.sortedTags()
offsets := make([]int, len(tags))
for ii, tag := range tags {
e := i.entries[tag]
pad(entryData, e.rpmtype, entryData.Len())
offsets[ii] = entryData.Len()
entryData.Write(e.data)
}
entryData.Write(i.eigenHeader().data)
// 4 magic and 4 reserved
w.Write([]byte{0x8e, 0xad, 0xe8, 0x01, 0, 0, 0, 0})
// 4 count and 4 size
// We add the pseudo-entry "eigenHeader" to count.
if err := binary.Write(w, binary.BigEndian, []int32{int32(len(i.entries)) + 1, int32(entryData.Len())}); err != nil {
return nil, fmt.Errorf("failed to write eigenHeader: %w", err)
}
// Write the eigenHeader index entry
w.Write(i.eigenHeader().indexBytes(i.h, entryData.Len()-0x10))
// Write all of the other index entries
for ii, tag := range tags {
e := i.entries[tag]
w.Write(e.indexBytes(tag, offsets[ii]))
}
w.Write(entryData.Bytes())
return w.Bytes(), nil
}
func (i *index) CountHeaders() int {
return len(i.entries)
}
// the eigenHeader is a weird entry. Its index entry is sorted first, but its content
// is last. The content is a 16 byte index entry, which is almost the same as the index
// entry except for the offset. The offset here is ... minus the length of the index entry region.
// Which is always 0x10 * number of entries.
// I kid you not.
func (i *index) eigenHeader() IndexEntry {
b := &bytes.Buffer{}
if err := binary.Write(b, binary.BigEndian, []int32{int32(i.h), int32(typeBinary), -int32(0x10 * (len(i.entries) + 1)), int32(0x10)}); err != nil {
// binary.Write can fail if the underlying Write fails, or the types are invalid.
// bytes.Buffer's write never error out, it can only panic with OOM.
panic(err)
}
return EntryBytes(b.Bytes())
}
func indexEntrySize(rpmtype int) int {
switch rpmtype {
case typeInt16:
return 2
case typeInt32:
return 4
case typeString:
return 1
case typeBinary:
return 1
case typeStringArray:
return 1
case typei18nString:
return 1
}
return -1
}
func readIndexEntry(entry IndexEntry, data []byte, offset int) ([]byte, error) {
size := indexEntrySize(entry.rpmtype)
if size < 1 {
return nil, fmt.Errorf("can't handle %d data type yet", entry.rpmtype)
}
if len(data) < offset + (size * entry.count) {
return nil, fmt.Errorf("buffer is too small size: %d, offset: %d, size: %d, count: %d", len(data), offset, size, entry.count)
}
if entry.rpmtype == typeInt16 || entry.rpmtype == typeInt32 {
return data[offset:offset + ( size * entry.count )], nil
}
if entry.rpmtype == typeString || entry.rpmtype == typei18nString {
data = data[offset:]
end := bytes.IndexByte(data, '\x00')
if end > -1 {
return data[:end+1], nil
}
return data, nil
}
if entry.rpmtype == typeStringArray {
data = data[offset:]
out := []byte{}
offset = 0
for i := 0 ; i < entry.count ; i++ {
offset = bytes.IndexByte(data, '\x00')
out = append(out, data[:offset+1]...)
data = data[offset+1:]
}
return out, nil
}
if entry.rpmtype == typeBinary {
data = data[offset:]
return data[:entry.count], nil
}
return nil, fmt.Errorf("not implemented")
}
func readHeaderIndex(inp io.Reader, countEntries int, expectedHeaderType int, size int32) (*index, map[int]int, error) {
out := index {
entries: make(map[int]IndexEntry, countEntries),
}
offsets := make(map[int]int, countEntries)
for i := 0; i < int(countEntries); i++ {
tag, contentOffset, indexEntry, err := readIndex(inp)
if err != nil {
return nil, nil, fmt.Errorf("failed to read index entry at %d: %w", i, err)
}
if i == 0 {
out.h = int(tag)
if out.h != expectedHeaderType {
return nil, nil, fmt.Errorf("missmatch type of header expected %x but got %x", expectedHeaderType, out.h)
}
if contentOffset + 0x10 != size {
return nil, nil, fmt.Errorf("failed to read eigen header offset not matching: %x %x", contentOffset, size)
}
continue
}
out.entries[int(tag)] = *indexEntry
offsets[int(tag)] = int(contentOffset)
}
return &out, offsets, nil
}
func ReadHeader(inp io.Reader, expectedHeaderType int) (*index, error) {
data, err := readExactly(inp, 8)
if err != nil {
return nil, err
}
if [8]byte(data) != [8]byte{0x8e, 0xad, 0xe8, 0x01, 0, 0, 0, 0} {
return nil, fmt.Errorf("header lead doesn't match expected: %v", data)
}
countEntries, err := readInt32(inp)
if err != nil {
return nil, fmt.Errorf("failed to read amount of entries: %w", err)
}
size, err := readInt32(inp)
if err != nil {
return nil, fmt.Errorf("failed to read length of entries: %w", err)
}
out, offsets, err := readHeaderIndex(inp, int(countEntries), expectedHeaderType, size)
if err != nil {
return nil, fmt.Errorf("failed to read header index: %w", err)
}
body, err := readExactly(inp, int64(size))
if err != nil {
return nil, fmt.Errorf("failed to read")
}
for tag := range(offsets) {
buf, err := readIndexEntry(out.entries[tag], body, offsets[tag])
if err != nil {
return nil, fmt.Errorf("failed to extract data for %x: %w", tag, err)
}
entry := out.entries[tag]
entry.setData(buf)
out.entries[tag] = entry
}
return out, nil
}
type Lead struct {
magic [4]byte
major, minor uint8
typeFile uint16
archNum uint16
name string
nameFromBinary bool
osnum, signatureType uint16
reserved [16]uint8;
} ;
func NewLead(data RPMMetaData) *Lead {
out := &Lead{}
out.magic = [4]byte{'\xed', '\xab', '\xee', '\xdb'}
out.major = 0x03
out.minor = 0x00
out.typeFile = 0 // assume binary
out.archNum = 0 // i386? lead is ignored so it doesn't matter
out.name = data.Name
out.osnum = 0x01 // linux?
out.signatureType = 0x05
out.reserved = [16]uint8{}
return out
}
func computeName(name string, version string) []byte {
if name == "" {
return make([]byte, 66)
}
if version != "" {
name = fmt.Sprintf("%s-%s", name, version)
}
if len(name) > 65 {
return []byte(name[:65])
}
var n []byte = []byte(name[:])
n = append(n, make([]byte, 66-len(n))...)
return n
}
func (r *Lead) toArray(fullVersion string) ([]byte, error) {
buf := new(bytes.Buffer)
buf.Write(r.magic[:])
buf.Write([]byte{r.major, r.minor})
binary.Write(buf, binary.BigEndian, r.typeFile)
binary.Write(buf, binary.BigEndian, r.archNum)
if r.nameFromBinary {
fullVersion = ""
}
buf.Write(computeName(r.name, fullVersion))
binary.Write(buf, binary.BigEndian, r.osnum)
binary.Write(buf, binary.BigEndian, r.signatureType)
buf.Write(r.reserved[:])
out := buf.Bytes()
if len(out) != 96 {
return nil, fmt.Errorf("invalid lead length expected 96, got %d", len(out))
}
return out, nil
}
func (r *Lead) ToString() string {
return fmt.Sprintf(`magic: %s
major: %d
minor: %d
file type: %d
arch: %d
name: %s
os number: %d
signature type: %d
`,
r.magic, r.major, r.minor, r.typeFile, r.archNum, r.name, r.osnum, r.signatureType)
}
func readExactly(inp io.Reader, limit int64) ([]byte, error) {
return io.ReadAll(io.LimitReader(inp, limit))
}
func readUint16(inp io.Reader) (uint16, error) {
if val, err := readExactly(inp, 2); err != nil {
return 0, fmt.Errorf("failed to read uint16: %v", err)
} else {
return binary.BigEndian.Uint16(val), nil
}
}
func readInt32(inp io.Reader) (int32, error) {
if val, err := readExactly(inp, 4); err != nil {
return 0, fmt.Errorf("failed to read int32: %v", err)
} else {
return int32(binary.BigEndian.Uint32(val)), nil
}
}
func readString(inp io.Reader, length int64) (string, error) {
if val, err := readExactly(inp, length); err != nil {
return "", fmt.Errorf("failed to read string: %s", err)
} else {
return string(bytes.Trim(val, "\x00")), nil
}
}
func (r *Lead) Equals(o *Lead) bool {
return r.magic == o.magic &&
r.major == o.major &&
r.minor == o.minor &&
r.typeFile == o.typeFile &&
r.archNum == o.archNum &&
r.name == o.name &&
r.osnum == o.osnum &&
r.signatureType == o.signatureType
}
func popTag[A any](m map[int]IndexEntry, key int, chain func(IndexEntry) (A, error)) (A, error) {
v, ok := m[key]
if !ok {
var defaultValue A
return defaultValue, fmt.Errorf("key %d not found", key)
}
delete(m, key)
return chain(v)
}