aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/read_bytes.go
blob: 222d9a05232418cd8d216dfbfc4852c55d55c224 (about) (plain) (blame)
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
package packed

import (
	"fmt"

	objectheader "codeberg.org/lindenii/furgit/object/header"
	objectid "codeberg.org/lindenii/furgit/object/id"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

// ReadBytesContent reads an object's type and content bytes.
//
// It fully resolves the requested object bytes. For base pack entries, this
// includes verifying that the zlib stream inflates to exactly the declared
// object size and verifying the Adler-32 trailer.
func (store *Store) ReadBytesContent(id objectid.ObjectID) (objecttype.Type, []byte, error) {
	loc, err := store.lookup(id)
	if err != nil {
		return objecttype.TypeInvalid, nil, err
	}

	return store.deltaResolveContent(loc)
}

// ReadBytesFull reads a full serialized object as "type size\0content".
//
// Like ReadBytesContent, it fully resolves the requested object bytes. For
// base pack entries, this includes verifying that the zlib stream inflates to
// exactly the declared object size and verifying the Adler-32 trailer.
func (store *Store) ReadBytesFull(id objectid.ObjectID) ([]byte, error) {
	ty, content, err := store.ReadBytesContent(id)
	if err != nil {
		return nil, err
	}

	header, ok := objectheader.Encode(ty, int64(len(content)))
	if !ok {
		return nil, fmt.Errorf("objectstore/packed: failed to encode object header for type %d", ty)
	}

	out := make([]byte, len(header)+len(content))
	copy(out, header)
	copy(out[len(header):], content)

	return out, nil
}