aboutsummaryrefslogtreecommitdiff
path: root/format/packfile/ofs.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-26 09:17:14 +0000
committerGravatar Runxi Yu2026-03-26 09:18:30 +0000
commit3e884f5f3d42cbc4874a04da31dde10314b0cfad (patch)
treef5e1e325fd1a2a0801791c054010213214475d80 /format/packfile/ofs.go
parentnetwork/receivepack: Rename from receivepack (diff)
signatureNo signature
format: Move commitgraph and packfile here
Diffstat (limited to 'format/packfile/ofs.go')
-rw-r--r--format/packfile/ofs.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/format/packfile/ofs.go b/format/packfile/ofs.go
new file mode 100644
index 00000000..4992a506
--- /dev/null
+++ b/format/packfile/ofs.go
@@ -0,0 +1,26 @@
+package packfile
+
+import "fmt"
+
+// ParseOfsDeltaDistance parses one ofs-delta backward distance.
+func ParseOfsDeltaDistance(buf []byte) (uint64, int, error) {
+ if len(buf) == 0 {
+ return 0, 0, fmt.Errorf("packfile: malformed ofs-delta distance")
+ }
+
+ b := buf[0]
+ dist := uint64(b & 0x7f)
+
+ consumed := 1
+ for b&0x80 != 0 {
+ if consumed >= len(buf) {
+ return 0, 0, fmt.Errorf("packfile: malformed ofs-delta distance")
+ }
+
+ b = buf[consumed]
+ consumed++
+ dist = ((dist + 1) << 7) + uint64(b&0x7f)
+ }
+
+ return dist, consumed, nil
+}