aboutsummaryrefslogtreecommitdiff
path: root/format/delta/apply/header.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 18:44:45 +0800
committerGravatar Runxi Yu2026-02-21 18:44:45 +0800
commit42ff39c8d340dabce79c4057a07a3932da295772 (patch)
treef90f6f8ad9401122654409bec61a1f8ccec7bb32 /format/delta/apply/header.go
parentbufpool: Import (diff)
signatureNo signature
format/delta/apply: Move core delta apply algorithm here
Diffstat (limited to 'format/delta/apply/header.go')
-rw-r--r--format/delta/apply/header.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/format/delta/apply/header.go b/format/delta/apply/header.go
new file mode 100644
index 00000000..f6aadea3
--- /dev/null
+++ b/format/delta/apply/header.go
@@ -0,0 +1,49 @@
+package apply
+
+import (
+ "fmt"
+ "io"
+)
+
+// ReadHeaderSizes reads the first two varints in one inflated delta stream.
+func ReadHeaderSizes(reader io.Reader) (int, int, error) {
+ // Two Git varints are read here. Each can take up to 10 bytes.
+ var buf [20]byte
+ n := 0
+
+ for {
+ if n >= len(buf) {
+ return 0, 0, fmt.Errorf("format/delta/apply: malformed delta varint")
+ }
+ if _, err := io.ReadFull(reader, buf[n:n+1]); err != nil {
+ return 0, 0, fmt.Errorf("format/delta/apply: malformed delta varint: %w", err)
+ }
+ n++
+ if buf[n-1]&0x80 == 0 {
+ break
+ }
+ }
+ pos := 0
+ srcSize, err := readVarint(buf[:n], &pos)
+ if err != nil {
+ return 0, 0, err
+ }
+
+ for {
+ if n >= len(buf) {
+ return 0, 0, fmt.Errorf("format/delta/apply: malformed delta varint")
+ }
+ if _, err := io.ReadFull(reader, buf[n:n+1]); err != nil {
+ return 0, 0, fmt.Errorf("format/delta/apply: malformed delta varint: %w", err)
+ }
+ n++
+ if buf[n-1]&0x80 == 0 {
+ break
+ }
+ }
+ dstSize, err := readVarint(buf[:n], &pos)
+ if err != nil {
+ return 0, 0, err
+ }
+ return srcSize, dstSize, nil
+}