web: home-page: Add tcc.html to index.html
[lcapit-junk-code.git] / git / dump-sha1-list.c
blobce0bb26c9ee677728b40b32a8746154b0f3591fc
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <arpa/inet.h>
12 #define FAN_OUT_SIZE (4 * 256)
13 #define PACK_IDX_SIGNATURE 0xff744f63
14 #define PACK_SIGNATURE 0x5041434b /* "PACK" */
16 struct pack_idx_header {
17 uint32_t idx_signature;
18 uint32_t idx_version;
21 struct pack_header {
22 uint32_t hdr_signature;
23 uint32_t hdr_version;
24 uint32_t hdr_entries;
27 static char *sha1_to_hex(const unsigned char *sha1)
29 static int bufno;
30 static char hexbuffer[4][50];
31 static const char hex[] = "0123456789abcdef";
32 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
33 int i;
35 for (i = 0; i < 20; i++) {
36 unsigned int val = *sha1++;
37 *buf++ = hex[val >> 4];
38 *buf++ = hex[val & 0xf];
40 *buf = '\0';
42 return buffer;
45 static int xread(int fd, void *buf, ssize_t size)
47 ssize_t bytes;
49 bytes = read(fd, buf, size);
50 if (bytes != size)
51 return -1;
52 return 0;
55 static void idx_open(const char *file, int *fd)
57 int version, err;
58 struct pack_idx_header hdr;
60 *fd = open(file, O_RDONLY);
61 if (*fd < 0) {
62 perror("open()");
63 exit(1);
66 err = xread(*fd, &hdr, sizeof(hdr));
67 if (err) {
68 perror("read_stdin()");
69 exit(1);
72 if (hdr.idx_signature != htonl(PACK_IDX_SIGNATURE)) {
73 fprintf(stderr,
74 "ERROR: '%s' is not an index file or is index v1\n",
75 file);
76 exit(1);
79 version = ntohl(hdr.idx_version);
80 if (version != 3) {
81 fprintf(stderr,
82 "ERROR: must be index v3, but it's index v%d\n",
83 version);
84 exit(1);
88 static void pck_open(const char *file, int *fd, size_t *nr_obj)
90 int err;
91 struct pack_header hdr;
93 *fd = open(file, O_RDONLY);
94 if (*fd < 0) {
95 perror("open()");
96 exit(1);
99 err = xread(*fd, &hdr, sizeof(hdr));
100 if (err) {
101 perror("read_stdin()");
102 exit(1);
105 if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) {
106 fprintf(stderr, "file '%s' is not a GIT packfile\n", file);
107 exit(1);
110 if (ntohl(hdr.hdr_version) != 4) {
111 fprintf(stderr, "pack file is not v4 (it's %d)\n",
112 htonl(hdr.hdr_version));
113 exit(1);
116 *nr_obj = ntohl(hdr.hdr_entries);
119 static void usage(void)
121 printf("dump-sha1-list < pack-idx > < pack-file >\n");
124 int main(int argc, char *argv[])
126 size_t i, nr_obj;
127 int idx_fd, pck_fd;
128 char *idx_file, *pck_file;
130 if (argc != 3) {
131 usage();
132 exit(1);
135 if (strstr(argv[1], ".idx")) {
136 idx_file = argv[1];
137 pck_file = argv[2];
138 } else {
139 pck_file = argv[1];
140 idx_file = argv[2];
143 idx_open(idx_file, &idx_fd);
144 pck_open(pck_file, &pck_fd, &nr_obj);
146 /* skip the fan out and crc32 tables */
147 if (lseek(idx_fd, FAN_OUT_SIZE + nr_obj * sizeof(uint32_t),
148 SEEK_CUR) < 0) {
149 perror("lseek()");
150 exit(1);
153 for (i = 0; i < nr_obj; i++) {
154 unsigned char sha1[20];
155 unsigned int offset;
157 xread(pck_fd, sha1, 20);
158 xread(idx_fd, &offset, sizeof(offset));
159 printf("%s %u\n", sha1_to_hex(sha1), ntohl(offset));
162 return 0;