projects/aports·blob v3.3.0_pre2 · refs

75 lines · 1.5 KiB

1#include <stdio.h>
2#include <fcntl.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <zlib.h>
7
8int main(void)
9{
10 char obuf[8*1024], ibuf[8*1024];
11 z_stream zs;
12 int r = Z_OK, rc = 1, fd = -1;
13 size_t len;
14
15 if (inflateInit2(&zs, 15+32) != Z_OK)
16 goto err;
17
18 zs.avail_out = sizeof obuf;
19 zs.next_out = obuf;
20
21 while (1) {
22 if (zs.avail_in == 0) {
23 zs.avail_in = read(STDIN_FILENO, ibuf, sizeof ibuf);
24 zs.next_in = ibuf;
25 if (zs.avail_in < 0) goto err;
26 if (zs.avail_in == 0 && r == Z_STREAM_END) goto ok;
27 }
28
29 r = inflate(&zs, Z_NO_FLUSH);
30 if (r != Z_OK && r != Z_STREAM_END) goto err;
31
32 len = sizeof obuf - zs.avail_out;
33 if (len) {
34 if (fd < 0) {
35 const char *fn;
36 if (strncmp(obuf, ".SIGN.", 6) == 0)
37 fn = "signatures.tar.gz";
38 else if (strncmp(obuf, ".PKGINFO", 8) == 0)
39 fn = "control.tar.gz";
40 else if (rc == 1)
41 fn = "data.tar.gz", rc = 2;
42 else
43 goto err;
44 fd = open(fn, O_CREAT|O_TRUNC|O_WRONLY, 0777);
45 if (fd < 0) goto err;
46 }
47 zs.next_out = obuf;
48 zs.avail_out = sizeof obuf;
49 }
50
51 if (zs.avail_in == 0 || r == Z_STREAM_END) {
52 len = (void *)zs.next_in - (void *)ibuf;
53 if (write(fd, ibuf, len) != len) goto err;
54 memmove(ibuf, zs.next_in, zs.avail_in);
55 zs.next_in = ibuf;
56 }
57
58 if (r == Z_STREAM_END) {
59 if (fd >= 0) {
60 close(fd);
61 fd = -1;
62 }
63 inflateEnd(&zs);
64 if (inflateInit2(&zs, 15+32) != Z_OK) goto err;
65 }
66 }
67ok:
68 rc = 0;
69err:
70 if (fd >= 0) close(fd);
71 inflateEnd(&zs);
72 if (rc) fprintf(stderr, "failed\n");
73 return rc;
74}
75