10 static unsigned char stdin_next_byte(void)
15 ret
= read(0, &b
, sizeof(b
));
16 if (ret
!= sizeof(b
)) {
17 fprintf(stderr
, "read(): ");
19 fprintf(stderr
, "EOF\n");
21 fprintf(stderr
, "%s\n", strerror(errno
));
23 fprintf(stderr
, "read %d bytes (1 expected)\b", ret
);
30 static int read_object_header(int *type
, size_t *sizep
)
36 c
= stdin_next_byte();
41 if (sizeof(long) * 8 <= shift
) {
42 printf("don't know what happened\n");
45 c
= stdin_next_byte();
46 size
+= (c
& 0x7f) << shift
;
54 static void show_object_header(int type
, size_t size
)
81 printf("unknown: %d", type
);
84 printf(" {%d} size: %zd bytes\n", type
, size
);
87 static unsigned char *unpack_entry(size_t size
)
91 unsigned char *buf
, c
;
93 buf
= malloc(size
+ 1);
98 memset(&stream
, 0, sizeof(stream
));
99 stream
.next_out
= buf
;
100 stream
.avail_out
= size
;
102 inflateInit(&stream
);
104 c
= stdin_next_byte();
107 st
= inflate(&stream
, Z_FINISH
);
108 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
111 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
113 fprintf(stderr
, "ERROR: zlib: %s\n", stream
.msg
);
120 int main(int argc
, char *argv
[])
125 unsigned long offset
;
128 printf("unpack-pack-entry offset < packfile\n");
132 /* go to where the object is stored */
133 offset
= strtoul(argv
[1], (char **) NULL
, 10);
134 if (lseek(0, offset
, SEEK_SET
) < 0) {
140 read_object_header(&type
, &size
);
141 show_object_header(type
, size
);
143 if (type
== 6 || type
== 7) {
144 fprintf(stderr
, "(no support to unpack delta objects)\n");
148 obj
= unpack_entry(size
);
152 /* show the object */