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 {
		panic(err)
	}
	return EntryBytes(b.Bytes())
}
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.
package rpmpack
import (
	"bytes"
	"encoding/binary"
	"fmt"
	"sort"
	"github.com/pkg/errors"
)
const (
	signatures = 0x3e
	immutable  = 0x3f
	typeInt16       = 0x03
	typeInt32       = 0x04
	typeString      = 0x06
	typeBinary      = 0x07
	typeStringArray = 0x08
)
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 {
		panic(err)
	}
	return b.Bytes()
}
func intEntry(rpmtype, size int, value interface{}) IndexEntry {
	b := &bytes.Buffer{}
	if err := binary.Write(b, binary.BigEndian, value); err != nil {
		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 pad(w *bytes.Buffer, rpmtype, offset int) {
	if b, ok := boundaries[rpmtype]; ok && offset%b != 0 {
		if _, err := w.Write(make([]byte, b-offset%b)); err != nil {
			panic(err)
		}
	}
}
func (i *index) Bytes() ([]byte, error) {
	w := &bytes.Buffer{}
	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)
	w.Write([]byte{0x8e, 0xad, 0xe8, 0x01, 0, 0, 0, 0})
	if err := binary.Write(w, binary.BigEndian, []int32{int32(len(i.entries)) + 1, int32(entryData.Len())}); err != nil {
		return nil, errors.Wrap(err, "failed to write eigenHeader")
	}
	w.Write(i.eigenHeader().indexBytes(i.h, entryData.Len()-0x10))
	for ii, tag := range tags {
		e := i.entries[tag]
		w.Write(e.indexBytes(tag, offsets[ii]))
	}
	w.Write(entryData.Bytes())
	return w.Bytes(), nil
}