Get magic number from git packfile index in Haskell
Asked Answered
A

1

7

I'm wanting to get the magic number from a git packfile index to ensure that it is indeed a packfile. The pack format documentation states that the magic number is "/377tOc". When I open the packfile with Ruby for example, I get this back when reading the file:

> File.open("pack-4412d2306cfe9a0b6d1b9b4430abc767022e8a3c.idx").read(4)
=> "\377tOc"

But in Haskell I get this:

> h <- openFile "pack-4412d2306cfe9a0b6d1b9b4430abc767022e8a3c.idx" ReadMode
> Data.ByteString.hGet h 4
=> "\255tOc"

I take it I'm missing something obvious, but it's not clear to me what that is. What am I doing wrong here?

Amadou answered 7/4, 2011 at 22:30 Comment(0)
S
11

The non-ascii character ('\255') is just being shown in decimal, not octal.

Confirming, according to od the first 4 bytes are indeed, in octal/ascii or 1 byte decimal:

> $ od -c foo.idx  | head -1
0000000 377   t   O   c  \0  \0  \0 002  \0  \0 002 250  \0  \0 005   B

> $ od -t u1 /tmp/x | head -1
0000000 255 116  79  99   0   0   0   2   0   0   2 168   0   0   5  66

And in Haskell:

> s <- Data.ByteString.readFile "foo.idx"
> Data.ByteString.take 4 s
"\255tOc"

So, just remember that 255 in decimal is 377 in octal.

Stiletto answered 7/4, 2011 at 22:45 Comment(1)
Thanks Don, that's just what I needed to hear. What confused me is that I thought the \ indicated it was octal. Some Googling turned up book.realworldhaskell.org/read/… which indicates that octal numbers are preceeded with an oh, so \o377 == \255. This addendum is for the benefit of others, since you obviously know all this :)Amadou

© 2022 - 2024 — McMap. All rights reserved.