2017-01-10 12:16:16 +00:00
|
|
|
#include <stdio.h>
|
2020-07-08 10:40:13 +00:00
|
|
|
#include <err.h>
|
2017-01-10 12:16:16 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2020-07-08 10:27:38 +00:00
|
|
|
static int find_section(const char *buf, size_t bufsize, const char *str) {
|
|
|
|
int len = strlen(str);
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (len >= bufsize) return 0;
|
|
|
|
|
|
|
|
p = strrchr(buf, '/');
|
|
|
|
if (p++ == NULL) p = buf;
|
|
|
|
return strncmp(p, str, len) == 0;
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:16:16 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
char obuf[8*1024], ibuf[8*1024];
|
|
|
|
z_stream zs;
|
|
|
|
int r = Z_OK, rc = 1, fd = -1;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (inflateInit2(&zs, 15+32) != Z_OK)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
zs.avail_out = sizeof obuf;
|
2020-07-06 10:32:19 +00:00
|
|
|
zs.next_out = (Bytef *)obuf;
|
2017-01-10 12:16:16 +00:00
|
|
|
|
|
|
|
while (1) {
|
2020-07-08 10:40:13 +00:00
|
|
|
const char *fn = NULL;
|
|
|
|
|
2017-01-10 12:16:16 +00:00
|
|
|
if (zs.avail_in == 0) {
|
|
|
|
zs.avail_in = read(STDIN_FILENO, ibuf, sizeof ibuf);
|
2020-07-06 10:32:19 +00:00
|
|
|
zs.next_in = (Bytef *)ibuf;
|
2020-07-08 10:40:13 +00:00
|
|
|
if (zs.avail_in < 0) {
|
|
|
|
warn("Failed to read input");
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
if (zs.avail_in == 0 && r == Z_STREAM_END) goto ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = inflate(&zs, Z_NO_FLUSH);
|
2020-07-08 10:40:13 +00:00
|
|
|
if (r != Z_OK && r != Z_STREAM_END) {
|
|
|
|
warnx("Failed to inflate input");
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
|
|
|
|
len = sizeof obuf - zs.avail_out;
|
|
|
|
if (len) {
|
|
|
|
if (fd < 0) {
|
|
|
|
const char *fn;
|
2019-07-15 06:16:12 +00:00
|
|
|
|
2020-07-08 10:27:38 +00:00
|
|
|
if (find_section(obuf, len, ".SIGN."))
|
2017-01-10 12:16:16 +00:00
|
|
|
fn = "signatures.tar.gz";
|
2020-07-08 10:27:38 +00:00
|
|
|
else if (find_section(obuf, len, ".PKGINFO"))
|
2017-01-10 12:16:16 +00:00
|
|
|
fn = "control.tar.gz";
|
|
|
|
else if (rc == 1)
|
|
|
|
fn = "data.tar.gz", rc = 2;
|
2020-07-08 10:40:13 +00:00
|
|
|
else {
|
|
|
|
warnx("Failed to find .PKGINFO section");
|
2017-01-10 12:16:16 +00:00
|
|
|
goto err;
|
2020-07-08 10:40:13 +00:00
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
fd = open(fn, O_CREAT|O_TRUNC|O_WRONLY, 0777);
|
2020-07-08 10:40:13 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
warn("Failed to open %s", fn);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
}
|
2020-07-06 10:32:19 +00:00
|
|
|
zs.next_out = (Bytef *)obuf;
|
2017-01-10 12:16:16 +00:00
|
|
|
zs.avail_out = sizeof obuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zs.avail_in == 0 || r == Z_STREAM_END) {
|
|
|
|
len = (void *)zs.next_in - (void *)ibuf;
|
2020-07-08 10:40:13 +00:00
|
|
|
if (write(fd, ibuf, len) != len) {
|
|
|
|
warn("Failed to write to %s", fn);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
memmove(ibuf, zs.next_in, zs.avail_in);
|
2020-07-06 10:32:19 +00:00
|
|
|
zs.next_in = (Bytef *)ibuf;
|
2017-01-10 12:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r == Z_STREAM_END) {
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
inflateEnd(&zs);
|
2020-07-08 10:40:13 +00:00
|
|
|
if (inflateInit2(&zs, 15+32) != Z_OK) {
|
|
|
|
warnx("inflateInit2 failed");
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-10 12:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ok:
|
|
|
|
rc = 0;
|
|
|
|
err:
|
|
|
|
if (fd >= 0) close(fd);
|
|
|
|
inflateEnd(&zs);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|