Get magic number from git package index in Haskell

I want to get the magic number from the git packfile index to make sure it is really a batch file. The documentation in package format indicates that the magic number is "/ 377tOc". When I open a batch file using Ruby, I return it when reading the file:

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

But in Haskell, I get the following:

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

As I understand it, I'm missing something obvious, but I don’t understand what it is. What am I doing wrong here?

+6
source share
1 answer

The non-ascii character ('\ 255') is simply displayed in decimal rather than octal.

Confirmation, according to od , the first 4 bytes are valid, 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 equal to 377 in octal.

+10
source

Source: https://habr.com/ru/post/885464/


All Articles