diff options
| author | 2026-02-21 00:42:00 +0800 | |
|---|---|---|
| committer | 2026-02-21 00:42:00 +0800 | |
| commit | a03e6e14a1e807136b05f28072f37dcf8c030f6b (patch) | |
| tree | c9e4069fbd959d05f684895a8bbd0ccbc311ea77 /objectheader/parse.go | |
| parent | testgit: Use objectid's SupportedAlgorithms (diff) | |
| signature | No signature | |
objectheader: Move out of internal
Diffstat (limited to 'objectheader/parse.go')
| -rw-r--r-- | objectheader/parse.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/objectheader/parse.go b/objectheader/parse.go new file mode 100644 index 00000000..c3d7d5dd --- /dev/null +++ b/objectheader/parse.go @@ -0,0 +1,40 @@ +package objectheader + +import ( + "bytes" + "strconv" + + "codeberg.org/lindenii/furgit/objecttype" +) + +// Parse parses a canonical loose-object header ("type size\\x00"). +// It returns the parsed type, size, bytes consumed (including trailing NUL), +// and whether parsing succeeded. +func Parse(data []byte) (objecttype.Type, int64, int, bool) { + space := bytes.IndexByte(data, ' ') + if space <= 0 { + return objecttype.TypeInvalid, 0, 0, false + } + + nulRel := bytes.IndexByte(data[space+1:], 0) + if nulRel < 0 { + return objecttype.TypeInvalid, 0, 0, false + } + nul := space + 1 + nulRel + + ty, ok := objecttype.ParseName(string(data[:space])) + if !ok { + return objecttype.TypeInvalid, 0, 0, false + } + + sizeBytes := data[space+1 : nul] + if len(sizeBytes) == 0 { + return objecttype.TypeInvalid, 0, 0, false + } + size, err := strconv.ParseInt(string(sizeBytes), 10, 64) + if err != nil || size < 0 { + return objecttype.TypeInvalid, 0, 0, false + } + + return ty, size, nul + 1, true +} |
