| 1 |
package idxfile |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"encoding/hex" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"io/fs" |
| 11 |
|
| 12 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 13 |
"github.com/go-git/go-git/v5/utils/binary" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
// ErrUnsupportedVersion is returned by Decode when the idx file version |
| 18 |
// is not supported. |
| 19 |
ErrUnsupportedVersion = errors.New("unsupported version") |
| 20 |
// ErrMalformedIdxFile is returned by Decode when the idx file is corrupted. |
| 21 |
ErrMalformedIdxFile = errors.New("malformed IDX file") |
| 22 |
) |
| 23 |
|
| 24 |
const ( |
| 25 |
fanout = 256 |
| 26 |
objectIDLength = hash.Size |
| 27 |
) |
| 28 |
|
| 29 |
// Byte sizes of the idx v2 layout elements, used by the size formula |
| 30 |
// in [validateIdxV2Size]. See [gitformat-pack] for the canonical |
| 31 |
// layout. |
| 32 |
// |
| 33 |
// [gitformat-pack]: https://git-scm.com/docs/gitformat-pack |
| 34 |
const ( |
| 35 |
headerLen = 8 // magic + version |
| 36 |
fanoutLen = fanout * 4 // uint32 per bucket |
| 37 |
crc32Len = 4 // CRC32 per object |
| 38 |
offset32Len = 4 // 32-bit offset per object |
| 39 |
offset64Len = 8 // 64-bit overflow offset |
| 40 |
trailerHashes = 2 // pack checksum + idx checksum, each hashsz |
| 41 |
) |
| 42 |
|
| 43 |
// statInput is the optional shape the [Decoder] probes for at the |
| 44 |
// start of [Decoder.Decode] to learn the on-disk length of the idx |
| 45 |
// blob, which it uses to validate the canonical-Git size formula |
| 46 |
// before any allocations driven by the fanout table. Callers that |
| 47 |
// pass an [*os.File] or a `billy.File` backed by an `*os.File` |
| 48 |
// (the production call sites in `storage/filesystem`) satisfy it |
| 49 |
// directly; arbitrary [io.Reader]s do not, and decode for them |
| 50 |
// retains the pre-existing behaviour of erroring out at the |
| 51 |
// truncated-payload boundary instead. |
| 52 |
// |
| 53 |
// The interface is intentionally unexported so the public |
| 54 |
// [NewDecoder] signature stays compatible with v5. |
| 55 |
type statInput interface { |
| 56 |
Stat() (fs.FileInfo, error) |
| 57 |
} |
| 58 |
|
| 59 |
// Decoder reads and decodes idx files from an input stream. |
| 60 |
type Decoder struct { |
| 61 |
io.Reader |
| 62 |
src io.Reader |
| 63 |
h hash.Hash |
| 64 |
} |
| 65 |
|
| 66 |
// NewDecoder builds a new idx stream decoder, that reads from r. |
| 67 |
func NewDecoder(r io.Reader) *Decoder { |
| 68 |
h := hash.New(crypto.SHA1) |
| 69 |
tr := io.TeeReader(r, h) |
| 70 |
return &Decoder{tr, r, h} |
| 71 |
} |
| 72 |
|
| 73 |
// Decode reads from the stream and decode the content into the MemoryIndex struct. |
| 74 |
func (d *Decoder) Decode(idx *MemoryIndex) error { |
| 75 |
idxSize := int64(-1) |
| 76 |
if in, ok := d.src.(statInput); ok { |
| 77 |
fi, err := in.Stat() |
| 78 |
if err != nil { |
| 79 |
return fmt.Errorf("%w: stat input: %w", ErrMalformedIdxFile, err) |
| 80 |
} |
| 81 |
idxSize = fi.Size() |
| 82 |
} |
| 83 |
|
| 84 |
if err := validateHeader(d); err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
|
| 88 |
headerFlow := []func(*MemoryIndex, io.Reader) error{ |
| 89 |
readVersion, |
| 90 |
readFanout, |
| 91 |
} |
| 92 |
for _, f := range headerFlow { |
| 93 |
if err := f(idx, d); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if idxSize >= 0 { |
| 99 |
if err := validateIdxV2Size(idx, idxSize); err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
bodyFlow := []func(*MemoryIndex, io.Reader) error{ |
| 105 |
readObjectNames, |
| 106 |
readCRC32, |
| 107 |
readOffsets, |
| 108 |
readPackChecksum, |
| 109 |
} |
| 110 |
for _, f := range bodyFlow { |
| 111 |
if err := f(idx, d); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
actual := d.h.Sum(nil) |
| 117 |
if err := readIdxChecksum(idx, d); err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
|
| 121 |
if !bytes.Equal(actual, idx.IdxChecksum[:]) { |
| 122 |
return fmt.Errorf("%w: checksum mismatch: %q instead of %q", |
| 123 |
ErrMalformedIdxFile, hex.EncodeToString(idx.IdxChecksum[:]), hex.EncodeToString(actual)) |
| 124 |
} |
| 125 |
|
| 126 |
return nil |
| 127 |
} |
| 128 |
|
| 129 |
func validateHeader(r io.Reader) error { |
| 130 |
h := make([]byte, 4) |
| 131 |
if _, err := io.ReadFull(r, h); err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
if !bytes.Equal(h, idxHeader) { |
| 136 |
return ErrMalformedIdxFile |
| 137 |
} |
| 138 |
|
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func readVersion(idx *MemoryIndex, r io.Reader) error { |
| 143 |
v, err := binary.ReadUint32(r) |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
|
| 148 |
if v != VersionSupported { |
| 149 |
return fmt.Errorf("%w: v%d", ErrUnsupportedVersion, v) |
| 150 |
} |
| 151 |
|
| 152 |
idx.Version = v |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func readFanout(idx *MemoryIndex, r io.Reader) error { |
| 157 |
for k := 0; k < fanout; k++ { |
| 158 |
n, err := binary.ReadUint32(r) |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
if k > 0 && n < idx.Fanout[k-1] { |
| 164 |
return fmt.Errorf("%w: fanout table is not monotonically non-decreasing at entry %d", ErrMalformedIdxFile, k) |
| 165 |
} |
| 166 |
|
| 167 |
idx.Fanout[k] = n |
| 168 |
idx.FanoutMapping[k] = noMapping |
| 169 |
} |
| 170 |
|
| 171 |
return nil |
| 172 |
} |
| 173 |
|
| 174 |
func readObjectNames(idx *MemoryIndex, r io.Reader) error { |
| 175 |
for k := 0; k < fanout; k++ { |
| 176 |
var buckets uint32 |
| 177 |
if k == 0 { |
| 178 |
buckets = idx.Fanout[k] |
| 179 |
} else { |
| 180 |
buckets = idx.Fanout[k] - idx.Fanout[k-1] |
| 181 |
} |
| 182 |
|
| 183 |
if buckets == 0 { |
| 184 |
continue |
| 185 |
} |
| 186 |
|
| 187 |
idx.FanoutMapping[k] = len(idx.Names) |
| 188 |
|
| 189 |
nameLen := int(buckets * objectIDLength) |
| 190 |
bin := make([]byte, nameLen) |
| 191 |
if _, err := io.ReadFull(r, bin); err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
|
| 195 |
idx.Names = append(idx.Names, bin) |
| 196 |
idx.Offset32 = append(idx.Offset32, make([]byte, buckets*4)) |
| 197 |
idx.CRC32 = append(idx.CRC32, make([]byte, buckets*4)) |
| 198 |
} |
| 199 |
|
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
func readCRC32(idx *MemoryIndex, r io.Reader) error { |
| 204 |
for k := 0; k < fanout; k++ { |
| 205 |
if pos := idx.FanoutMapping[k]; pos != noMapping { |
| 206 |
if _, err := io.ReadFull(r, idx.CRC32[pos]); err != nil { |
| 207 |
return err |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return nil |
| 213 |
} |
| 214 |
|
| 215 |
func readOffsets(idx *MemoryIndex, r io.Reader) error { |
| 216 |
var o64cnt int64 |
| 217 |
for k := 0; k < fanout; k++ { |
| 218 |
if pos := idx.FanoutMapping[k]; pos != noMapping { |
| 219 |
if _, err := io.ReadFull(r, idx.Offset32[pos]); err != nil { |
| 220 |
return err |
| 221 |
} |
| 222 |
|
| 223 |
for p := 0; p < len(idx.Offset32[pos]); p += 4 { |
| 224 |
if idx.Offset32[pos][p]&(byte(1)<<7) > 0 { |
| 225 |
o64cnt++ |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if o64cnt > 0 { |
| 232 |
idx.Offset64 = make([]byte, o64cnt*8) |
| 233 |
if _, err := io.ReadFull(r, idx.Offset64); err != nil { |
| 234 |
return err |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
func readPackChecksum(idx *MemoryIndex, r io.Reader) error { |
| 242 |
if _, err := io.ReadFull(r, idx.PackfileChecksum[:]); err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
|
| 246 |
return nil |
| 247 |
} |
| 248 |
|
| 249 |
func readIdxChecksum(idx *MemoryIndex, r io.Reader) error { |
| 250 |
if _, err := io.ReadFull(r, idx.IdxChecksum[:]); err != nil { |
| 251 |
return err |
| 252 |
} |
| 253 |
|
| 254 |
return nil |
| 255 |
} |
| 256 |
|
| 257 |
// validateIdxV2Size enforces the size formula used by canonical Git |
| 258 |
// load_idx for idx v2 files: the on-disk length must lie within |
| 259 |
// [minSize, maxSize] where |
| 260 |
// |
| 261 |
// perObject = hashsz + crc32Len + offset32Len |
| 262 |
// minSize = headerLen + fanoutLen + trailerHashes*hashsz + nr*perObject |
| 263 |
// maxSize = minSize + (nr-1)*offset64Len when nr > 0 |
| 264 |
// |
| 265 |
// with nr taken from the last fanout entry and hashsz fixed at |
| 266 |
// [objectIDLength] (SHA-1 in v5). Multiplications use a self-checking |
| 267 |
// overflow guard so inputs whose claimed object count overflows the |
| 268 |
// formula are rejected rather than wrapping into a smaller value. |
| 269 |
func validateIdxV2Size(idx *MemoryIndex, idxSize int64) error { |
| 270 |
nr := int64(idx.Fanout[fanout-1]) |
| 271 |
hashsz := int64(objectIDLength) |
| 272 |
|
| 273 |
minSize := minIdxV2Size(nr, hashsz) |
| 274 |
maxSize := maxIdxV2Size(nr, hashsz) |
| 275 |
if minSize < 0 || maxSize < 0 { |
| 276 |
return fmt.Errorf("%w: object count %d is inconsistent with file size", ErrMalformedIdxFile, nr) |
| 277 |
} |
| 278 |
|
| 279 |
if idxSize < minSize || idxSize > maxSize { |
| 280 |
return fmt.Errorf("%w: file size %d is inconsistent with object count %d", ErrMalformedIdxFile, idxSize, nr) |
| 281 |
} |
| 282 |
return nil |
| 283 |
} |
| 284 |
|
| 285 |
// minIdxV2Size returns the minimum on-disk size of an idx v2 file |
| 286 |
// holding nr objects with the given hash size, mirroring the |
| 287 |
// computation in canonical Git load_idx. Returns -1 when any |
| 288 |
// intermediate multiplication or addition would overflow int64. |
| 289 |
func minIdxV2Size(nr, hashsz int64) int64 { |
| 290 |
perObject := hashsz + crc32Len + offset32Len |
| 291 |
fixed := int64(headerLen+fanoutLen) + trailerHashes*hashsz |
| 292 |
|
| 293 |
objects, ok := mulInt64(nr, perObject) |
| 294 |
if !ok { |
| 295 |
return -1 |
| 296 |
} |
| 297 |
sum, ok := addInt64(fixed, objects) |
| 298 |
if !ok { |
| 299 |
return -1 |
| 300 |
} |
| 301 |
return sum |
| 302 |
} |
| 303 |
|
| 304 |
// maxIdxV2Size returns the maximum on-disk size of an idx v2 file |
| 305 |
// holding nr objects with the given hash size, mirroring the |
| 306 |
// computation in canonical Git load_idx. Returns -1 on overflow. |
| 307 |
func maxIdxV2Size(nr, hashsz int64) int64 { |
| 308 |
minSize := minIdxV2Size(nr, hashsz) |
| 309 |
if minSize < 0 { |
| 310 |
return -1 |
| 311 |
} |
| 312 |
if nr == 0 { |
| 313 |
return minSize |
| 314 |
} |
| 315 |
overflow, ok := mulInt64(nr-1, offset64Len) |
| 316 |
if !ok { |
| 317 |
return -1 |
| 318 |
} |
| 319 |
sum, ok := addInt64(minSize, overflow) |
| 320 |
if !ok { |
| 321 |
return -1 |
| 322 |
} |
| 323 |
return sum |
| 324 |
} |
| 325 |
|
| 326 |
// mulInt64 returns a*b and whether the result fits in an int64 without |
| 327 |
// overflow. Negative operands or overflow yield ok=false. The overflow |
| 328 |
// check uses the standard self-inverse identity: a*b/b == a only when |
| 329 |
// the multiplication did not wrap. |
| 330 |
func mulInt64(a, b int64) (int64, bool) { |
| 331 |
if a < 0 || b < 0 { |
| 332 |
return 0, false |
| 333 |
} |
| 334 |
if a == 0 || b == 0 { |
| 335 |
return 0, true |
| 336 |
} |
| 337 |
c := a * b |
| 338 |
if c/b != a { |
| 339 |
return 0, false |
| 340 |
} |
| 341 |
return c, true |
| 342 |
} |
| 343 |
|
| 344 |
// addInt64 returns a+b and whether the result fits in an int64 without |
| 345 |
// overflow. Negative operands or overflow yield ok=false. |
| 346 |
func addInt64(a, b int64) (int64, bool) { |
| 347 |
if a < 0 || b < 0 { |
| 348 |
return 0, false |
| 349 |
} |
| 350 |
c := a + b |
| 351 |
if c < a { |
| 352 |
return 0, false |
| 353 |
} |
| 354 |
return c, true |
| 355 |
} |