# Question about the pack OBJ_OFS_DELTA format

5 messages from 2020-01-10 to 2020-01-11. Participants: Erik Fastermann, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/52611

## Erik Fastermann, 2020-01-10 08:26

Subject: Question about the pack OBJ_OFS_DELTA format
Message-ID: <1032627506.120165.1578644787174@email.ionos.de>
URL: https://gitlist.dev/e/1032627506.120165.1578644787174%40email.ionos.de

```
Hi all,

I'm trying to implement the Git pack format. Parsing the index file and
unpacking undeltified objects already works. However I'm unable to get
the offset, if the type is OBJ_OFS_DELTA.

The very much work-in-progress Go code and data can be found here:
https://github.com/erikfastermann/notes/tree/wip/git

From the docs (https://git-scm.com/docs/pack-format) I assume, that
the offset is a variable length integer, like the size.  However my
caclulation leads to a illogical result.

When trying the hash 8c40ff4767d973b672fe5aa431cb8ba0593dd26a with:

git verify-pack -v pack.pack

I get: offset: 138650 size: 30.

The base object 9b25d441c1bf358af01edc4eeba65870581a5ac1 (shown by
verify-pack) has the offset 136887, which I think means the delta should
be: 138650 - 136887 = 1763.

Using the command:

dd skip=138650 count=4 if=pack.pack bs=1 status=none | hexdump -C

I get: ee 01 8c 63

The first two bytes, the type and the size are correctly computed.

So the next varint should be the offset.

8c: 10001100 --- 63: 01100011

-> 1100011_0001100

-> 12684 ???

The result is the same when calculating it manually and with my program.

I probably have some crucial misunderstanding about the format, so a
clarification would be nice.

Thank you for your help.

Erik

```

## Jeff King, 2020-01-10 09:57

Subject: Re: Question about the pack OBJ_OFS_DELTA format
Message-ID: <20200110095707.GA459765@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20200110095707.GA459765%40coredump.intra.peff.net
In-Reply-To: <1032627506.120165.1578644787174@email.ionos.de>

```
On Fri, Jan 10, 2020 at 09:26:27AM +0100, Erik Fastermann wrote:

> I get: ee 01 8c 63
> 
> The first two bytes, the type and the size are correctly computed.
> 
> So the next varint should be the offset.
> 
> 8c: 10001100 --- 63: 01100011
> 
> -> 1100011_0001100
> 
> -> 12684 ???
> 
> The result is the same when calculating it manually and with my program.

The pack-format.txt file says:

       offset encoding:
            n bytes with MSB set in all but the last one.
            The offset is then the number constructed by
            concatenating the lower 7 bit of each byte, and
            for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1))
            to the result.

but I think is missing two bits of information:

  - the bytes are in most-significant to least-significant order, which
    IIRC is the opposite of the size varint

  - each 7-bit byte sneaks in some extra data by implicitly adding "1"
    to all but the last byte

So the low seven bits of "8c" is "12". Add one and multiply by 2^7 gets
you 1664. The low seven of "63" is 99. No addition or multiply because
it's the last byte.

The result is 1763, which is what you expected.

It does seem like the documentation could be a lot better. I had to dig
into the source (packfile.c:get_delta_base is pretty clear, but if
you're trying to do a non-GPL clean-room implementation, then obviously
don't look at it).

-Peff

```

## Erik Fastermann, 2020-01-10 13:56

Subject: Re: Question about the pack OBJ_OFS_DELTA format
Message-ID: <1429594513.146357.1578664568182@email.ionos.de>
URL: https://gitlist.dev/e/1429594513.146357.1578664568182%40email.ionos.de
In-Reply-To: <20200110095707.GA459765@coredump.intra.peff.net>

```
Thanks mate. That helped me a lot.

Erik

```

## Junio C Hamano, 2020-01-10 19:41

Subject: Re: Question about the pack OBJ_OFS_DELTA format
Message-ID: <xmqq1rs7m757.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq1rs7m757.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20200110095707.GA459765@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> The pack-format.txt file says:
>
>        offset encoding:
>             n bytes with MSB set in all but the last one.
>             The offset is then the number constructed by
>             concatenating the lower 7 bit of each byte, and
>             for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1))
>             to the result.
>
> but I think is missing two bits of information:
>
>   - the bytes are in most-significant to least-significant order, which
>     IIRC is the opposite of the size varint
>
>   - each 7-bit byte sneaks in some extra data by implicitly adding "1"
>     to all but the last byte

Isn't the latter mentioned in the paragraph you quoted?

```

## Jeff King, 2020-01-11 09:56

Subject: Re: Question about the pack OBJ_OFS_DELTA format
Message-ID: <20200111095604.GA1605786@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20200111095604.GA1605786%40coredump.intra.peff.net
In-Reply-To: <xmqq1rs7m757.fsf@gitster-ct.c.googlers.com>

```
On Fri, Jan 10, 2020 at 11:41:08AM -0800, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > The pack-format.txt file says:
> >
> >        offset encoding:
> >             n bytes with MSB set in all but the last one.
> >             The offset is then the number constructed by
> >             concatenating the lower 7 bit of each byte, and
> >             for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1))
> >             to the result.
> >
> > but I think is missing two bits of information:
> >
> >   - the bytes are in most-significant to least-significant order, which
> >     IIRC is the opposite of the size varint
> >
> >   - each 7-bit byte sneaks in some extra data by implicitly adding "1"
> >     to all but the last byte
> 
> Isn't the latter mentioned in the paragraph you quoted?

Hmm, yeah. I admit I had trouble parsing exactly what that part was
trying to say, and thought it was trying to talk about how you'd shift
the individual bytes. But reading more carefully, it does say "adding",
so yeah, it accounts for the extra.

It's a little confusing, I think, because in code you'd add just
continually add one before shifting, rather than trying to add in the
extra values at the end.

-Peff

```
