2 * dump-pack-idx-v1: Dump the 'SHA1 offset' list from a .idx v1 file
12 #include <arpa/inet.h>
14 #define FAN_OUT_SIZE (4 * 256)
15 #define PACK_IDX_SIGNATURE 0xff744f63
17 struct pack_idx_header
{
18 uint32_t idx_signature
;
22 static inline void hashcpy(unsigned char *sha_dst
,
23 const unsigned char *sha_src
)
25 memcpy(sha_dst
, sha_src
, 20);
28 static char *sha1_to_hex(const unsigned char *sha1
)
31 static char hexbuffer
[4][50];
32 static const char hex
[] = "0123456789abcdef";
33 char *buffer
= hexbuffer
[3 & ++bufno
], *buf
= buffer
;
36 for (i
= 0; i
< 20; i
++) {
37 unsigned int val
= *sha1
++;
38 *buf
++ = hex
[val
>> 4];
39 *buf
++ = hex
[val
& 0xf];
46 static void dump_index_v1(void *base
, off_t size
)
49 unsigned long nr_sha1
, i
;
51 list
= base
+ FAN_OUT_SIZE
;
52 nr_sha1
= (size
- (FAN_OUT_SIZE
+ 40)) / 24;
53 for (i
= 0; i
< nr_sha1
; i
++) {
54 unsigned char sha1
[20];
57 memcpy(&offset
, list
, sizeof(offset
));
58 hashcpy(sha1
, list
+ 4);
59 printf("%s %u\n", sha1_to_hex(sha1
), ntohl(offset
));
64 static void usage(void)
66 printf("dump-pack-idx-v1 [ -f -v ] < pack-idx >\n");
69 int main(int argc
, char *argv
[])
74 struct pack_idx_header
*hdr
;
75 int version
, force
, show_version
, opt
, err
, fd
;
78 show_version
= force
= 0;
80 while ((opt
= getopt(argc
, argv
, "vf")) != -1) {
98 idx_file
= argv
[optind
];
100 if (!strstr(idx_file
, ".idx") && !force
) {
102 "ERROR: '%s' doesn't seem to be an .idx file, try '-f'"
103 " if you know what you're doing\n", idx_file
);
107 fd
= open(idx_file
, O_RDONLY
);
113 err
= fstat(fd
, &buf
);
119 idx
= mmap(NULL
, buf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
121 if (idx
== MAP_FAILED
) {
127 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
))
128 version
= ntohl(hdr
->idx_version
);
131 printf("version: %d\n", version
);
136 printf("Only dumps index v1, this is index v%d\n", version
);
140 dump_index_v1(idx
, buf
.st_size
);
142 munmap(idx
, buf
.st_size
);