Debian Codegraph @

pack/index_decode.go

raw
1 package pack
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "hash"
8 "io"
9 )
10
11 const (
12 // indexMagicWidth is the width of the magic header of packfiles version
13 // 2 and newer.
14 indexMagicWidth = 4
15 // indexVersionWidth is the width of the version following the magic
16 // header.
17 indexVersionWidth = 4
18 // indexV2Width is the total width of the header in V2.
19 indexV2Width = indexMagicWidth + indexVersionWidth
20 // indexV1Width is the total width of the header in V1.
21 indexV1Width = 0
22
23 // indexFanoutEntries is the number of entries in the fanout table.
24 indexFanoutEntries = 256
25 // indexFanoutEntryWidth is the width of each entry in the fanout table.
26 indexFanoutEntryWidth = 4
27 // indexFanoutWidth is the width of the entire fanout table.
28 indexFanoutWidth = indexFanoutEntries * indexFanoutEntryWidth
29
30 // indexOffsetV1Start is the location of the first object outside of the
31 // V1 header.
32 indexOffsetV1Start = indexV1Width + indexFanoutWidth
33 // indexOffsetV2Start is the location of the first object outside of the
34 // V2 header.
35 indexOffsetV2Start = indexV2Width + indexFanoutWidth
36
37 // indexObjectCRCWidth is the width of the CRC accompanying each object
38 // in V2.
39 indexObjectCRCWidth = 4
40 // indexObjectSmallOffsetWidth is the width of the small offset encoded
41 // into each object.
42 indexObjectSmallOffsetWidth = 4
43 // indexObjectLargeOffsetWidth is the width of the optional large offset
44 // encoded into the small offset.
45 indexObjectLargeOffsetWidth = 8
46 )
47
48 var (
49 // ErrShortFanout is an error representing situations where the entire
50 // fanout table could not be read, and is thus too short.
51 ErrShortFanout = fmt.Errorf("gitobj/pack: too short fanout table")
52
53 // indexHeader is the first four "magic" bytes of index files version 2
54 // or newer.
55 indexHeader = []byte{0xff, 0x74, 0x4f, 0x63}
56 )
57
58 // DecodeIndex decodes an index whose underlying data is supplied by "r".
59 //
60 // DecodeIndex reads only the header and fanout table, and does not eagerly
61 // parse index entries.
62 //
63 // If there was an error parsing, it will be returned immediately.
64 func DecodeIndex(r io.ReaderAt, hash hash.Hash) (*Index, error) {
65 version, err := decodeIndexHeader(r, hash)
66 if err != nil {
67 return nil, err
68 }
69
70 fanout, err := decodeIndexFanout(r, version.Width())
71 if err != nil {
72 return nil, err
73 }
74
75 return &Index{
76 version: version,
77 fanout: fanout,
78
79 r: r,
80 }, nil
81 }
82
83 // decodeIndexHeader determines which version the index given by "r" is.
84 func decodeIndexHeader(r io.ReaderAt, hash hash.Hash) (IndexVersion, error) {
85 hdr := make([]byte, 4)
86 if _, err := r.ReadAt(hdr, 0); err != nil {
87 return nil, err
88 }
89
90 if bytes.Equal(hdr, indexHeader) {
91 vb := make([]byte, 4)
92 if _, err := r.ReadAt(vb, 4); err != nil {
93 return nil, err
94 }
95
96 version := binary.BigEndian.Uint32(vb)
97 switch version {
98 case 1:
99 return &V1{hash: hash}, nil
100 case 2:
101 return &V2{hash: hash}, nil
102 }
103 return nil, &UnsupportedVersionErr{uint32(version)}
104 }
105 return &V1{hash: hash}, nil
106 }
107
108 // decodeIndexFanout decodes the fanout table given by "r" and beginning at the
109 // given offset.
110 func decodeIndexFanout(r io.ReaderAt, offset int64) ([]uint32, error) {
111 b := make([]byte, 256*4)
112 if _, err := r.ReadAt(b, offset); err != nil {
113 if err == io.EOF {
114 return nil, ErrShortFanout
115 }
116 return nil, err
117 }
118
119 fanout := make([]uint32, 256)
120 for i, _ := range fanout {
121 fanout[i] = binary.BigEndian.Uint32(b[(i * 4):])
122 }
123
124 return fanout, nil
125 }