2009-07-16 09:03:26 +00:00
|
|
|
/* abuild-tar.c - A TAR mangling utility for .APK packages
|
|
|
|
*
|
2014-11-01 15:09:13 +00:00
|
|
|
* Copyright (C) 2009-2014 Timo Teräs <timo.teras@iki.fi>
|
2009-07-16 09:03:26 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation. See http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
2015-10-06 11:01:47 +00:00
|
|
|
#define _GNU_SOURCE
|
2009-07-16 09:03:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2009-07-20 09:59:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2009-07-16 09:03:26 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
2015-10-06 11:01:47 +00:00
|
|
|
#include <openssl/engine.h>
|
2009-07-16 09:03:26 +00:00
|
|
|
|
|
|
|
#ifndef VERSION
|
|
|
|
#define VERSION ""
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct tar_header {
|
|
|
|
/* ustar header, Posix 1003.1 */
|
|
|
|
char name[100]; /* 0-99 */
|
|
|
|
char mode[8]; /* 100-107 */
|
|
|
|
char uid[8]; /* 108-115 */
|
|
|
|
char gid[8]; /* 116-123 */
|
|
|
|
char size[12]; /* 124-135 */
|
|
|
|
char mtime[12]; /* 136-147 */
|
|
|
|
char chksum[8]; /* 148-155 */
|
|
|
|
char typeflag; /* 156-156 */
|
|
|
|
char linkname[100]; /* 157-256 */
|
|
|
|
char magic[8]; /* 257-264 */
|
|
|
|
char uname[32]; /* 265-296 */
|
|
|
|
char gname[32]; /* 297-328 */
|
|
|
|
char devmajor[8]; /* 329-336 */
|
|
|
|
char devminor[8]; /* 337-344 */
|
|
|
|
char prefix[155]; /* 345-499 */
|
2013-07-05 04:21:11 +00:00
|
|
|
char padding[12]; /* 500-511 */
|
2009-07-16 09:03:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define GET_OCTAL(s) get_octal(s, sizeof(s))
|
|
|
|
#define PUT_OCTAL(s,v) put_octal(s, sizeof(s), v)
|
|
|
|
|
|
|
|
static inline int dx(int c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 0xa;
|
|
|
|
if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 0xa;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-06-14 05:49:54 +00:00
|
|
|
static size_t get_octal(char *s, size_t l)
|
2009-07-16 09:03:26 +00:00
|
|
|
{
|
2020-06-14 05:49:54 +00:00
|
|
|
size_t val;
|
2009-07-16 09:03:26 +00:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
val = 0;
|
|
|
|
while (l && s[0] != 0) {
|
|
|
|
ch = dx(s[0]);
|
|
|
|
if (ch < 0 || ch >= 8)
|
|
|
|
break;
|
|
|
|
val *= 8;
|
|
|
|
val += ch;
|
|
|
|
|
|
|
|
s++;
|
|
|
|
l--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_octal(char *s, size_t l, size_t value)
|
|
|
|
{
|
|
|
|
char *ptr = &s[l - 1];
|
|
|
|
|
|
|
|
*(ptr--) = '\0';
|
|
|
|
while (value != 0 && ptr >= s) {
|
|
|
|
*(ptr--) = '0' + (value % 8);
|
|
|
|
value /= 8;
|
|
|
|
}
|
|
|
|
while (ptr >= s)
|
|
|
|
*(ptr--) = '0';
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
static void tarhdr_checksum(struct tar_header *hdr)
|
|
|
|
{
|
|
|
|
const unsigned char *src;
|
|
|
|
size_t chksum, i;
|
|
|
|
|
|
|
|
/* Recalculate checksum */
|
|
|
|
memset(hdr->chksum, ' ', sizeof(hdr->chksum));
|
|
|
|
src = (const unsigned char *) hdr;
|
|
|
|
for (i = chksum = 0; i < sizeof(*hdr); i++)
|
|
|
|
chksum += src[i];
|
|
|
|
put_octal(hdr->chksum, sizeof(hdr->chksum)-1, chksum);
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:59:30 +00:00
|
|
|
static int usage(FILE *out)
|
2009-07-16 09:03:26 +00:00
|
|
|
{
|
2022-11-30 10:59:30 +00:00
|
|
|
fprintf(out,
|
2009-07-16 09:03:26 +00:00
|
|
|
"abuild-tar " VERSION "\n"
|
|
|
|
"\n"
|
2009-08-11 16:56:11 +00:00
|
|
|
"usage: abuild-tar [--hash[=<algorithm>]] [--cut]\n"
|
2009-07-16 09:03:26 +00:00
|
|
|
"\n"
|
|
|
|
"options:\n"
|
2009-08-11 16:56:11 +00:00
|
|
|
" --hash[=sha1|md5] Read tar archive from stdin, precalculate hash for \n"
|
2009-07-16 09:03:26 +00:00
|
|
|
" regular entries and output tar archive on stdout\n"
|
2009-07-21 09:30:34 +00:00
|
|
|
" --cut Remove the end of file tar record\n"
|
2009-07-16 09:03:26 +00:00
|
|
|
"\n");
|
2020-07-06 10:26:24 +00:00
|
|
|
return 1;
|
2009-07-16 09:03:26 +00:00
|
|
|
}
|
|
|
|
|
2009-07-20 09:59:31 +00:00
|
|
|
static ssize_t full_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t total, n;
|
2021-07-24 11:52:34 +00:00
|
|
|
char *p = buf;
|
2009-07-20 09:59:31 +00:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
do {
|
2021-07-24 11:52:34 +00:00
|
|
|
n = read(fd, p, count);
|
2009-07-20 09:59:31 +00:00
|
|
|
if (n < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2021-07-24 11:52:34 +00:00
|
|
|
p += n;
|
2009-07-20 17:41:04 +00:00
|
|
|
total += n;
|
2009-07-20 09:59:31 +00:00
|
|
|
count -= n;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (total == 0 && n < 0)
|
2014-11-01 15:09:13 +00:00
|
|
|
return -errno;
|
2009-07-20 09:59:31 +00:00
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t full_write(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t total, n;
|
2021-07-24 11:52:34 +00:00
|
|
|
const char *p = buf;
|
2009-07-20 09:59:31 +00:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
do {
|
2021-07-24 11:52:34 +00:00
|
|
|
n = write(fd, p, count);
|
2009-07-20 09:59:31 +00:00
|
|
|
if (n < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2021-07-24 11:52:34 +00:00
|
|
|
p += n;
|
2009-07-20 17:41:04 +00:00
|
|
|
total += n;
|
2009-07-20 09:59:31 +00:00
|
|
|
count -= n;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (total == 0 && n < 0)
|
2014-11-01 15:09:13 +00:00
|
|
|
return -errno;
|
2009-07-20 09:59:31 +00:00
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2016-05-20 08:19:08 +00:00
|
|
|
#if defined(__linux__)
|
2009-07-20 09:59:31 +00:00
|
|
|
static ssize_t full_splice(int from_fd, int to_fd, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t total, n;
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
do {
|
2009-07-21 09:32:25 +00:00
|
|
|
n = splice(from_fd, NULL, to_fd, NULL, count, 0);
|
2009-07-20 09:59:31 +00:00
|
|
|
if (n < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
|
|
|
count -= n;
|
2009-07-21 09:36:15 +00:00
|
|
|
total += n;
|
2009-07-20 09:59:31 +00:00
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (total == 0 && n < 0)
|
2014-11-01 15:09:13 +00:00
|
|
|
return -errno;
|
2009-07-20 09:59:31 +00:00
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
2016-05-20 08:19:08 +00:00
|
|
|
#else
|
|
|
|
#define full_splice(from_fd, to_fd, count) -1
|
|
|
|
#endif
|
2009-07-20 09:59:31 +00:00
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
#define BUF_INITIALIZER {0}
|
|
|
|
struct buf {
|
|
|
|
char *ptr;
|
|
|
|
size_t size;
|
|
|
|
size_t alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void buf_free(struct buf *b)
|
|
|
|
{
|
|
|
|
free(b->ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buf_resize(struct buf *b, size_t newsize)
|
2009-07-16 09:03:26 +00:00
|
|
|
{
|
|
|
|
void *ptr;
|
2014-11-01 15:09:13 +00:00
|
|
|
|
|
|
|
if (b->alloc >= newsize) return 0;
|
|
|
|
ptr = realloc(b->ptr, newsize);
|
|
|
|
if (!ptr) return -ENOMEM;
|
|
|
|
b->ptr = ptr;
|
|
|
|
b->alloc = newsize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buf_padto(struct buf *b, size_t alignment)
|
|
|
|
{
|
|
|
|
size_t oldsize, newsize;
|
|
|
|
oldsize = b->size;
|
|
|
|
newsize = (oldsize + alignment - 1) & -alignment;
|
|
|
|
if (buf_resize(b, newsize)) return -ENOMEM;
|
|
|
|
b->size = newsize;
|
2023-10-17 16:30:30 +00:00
|
|
|
if (b->ptr)
|
|
|
|
memset(b->ptr + oldsize, 0, newsize - oldsize);
|
2014-11-01 15:09:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buf_read_fd(struct buf *b, int fd, size_t size)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
r = buf_resize(b, size);
|
|
|
|
if (r) return r;
|
|
|
|
|
|
|
|
r = full_read(fd, b->ptr, size);
|
|
|
|
if (r == size) {
|
|
|
|
b->size = r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
b->size = 0;
|
|
|
|
if (r < 0) return r;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buf_write_fd(struct buf *b, int fd)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
r = full_write(fd, b->ptr, b->size);
|
|
|
|
if (r == b->size) return 0;
|
|
|
|
if (r < 0) return r;
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:26:24 +00:00
|
|
|
static int buf_add_ext_header_hexdump(struct buf *b, const char *hdr, const unsigned char *value, int valuelen)
|
2014-11-01 15:09:13 +00:00
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
/* "%u %s=%s\n" */
|
|
|
|
len = 1 + 1 + strlen(hdr) + 1 + valuelen*2 + 1;
|
|
|
|
for (i = len; i > 9; i /= 10) len++;
|
|
|
|
|
|
|
|
if (buf_resize(b, b->size + len)) return -ENOMEM;
|
2023-10-17 16:30:30 +00:00
|
|
|
if (b->ptr == NULL)
|
|
|
|
return 0;
|
2014-11-01 15:09:13 +00:00
|
|
|
b->size += snprintf(&b->ptr[b->size], len, "%u %s=", len, hdr);
|
|
|
|
for (i = 0; i < valuelen; i++)
|
2020-07-06 10:26:24 +00:00
|
|
|
b->size += snprintf(&b->ptr[b->size], 3, "%02x", (int)value[i]);
|
2014-11-01 15:09:13 +00:00
|
|
|
b->ptr[b->size++] = '\n';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:26:24 +00:00
|
|
|
static void add_legacy_checksum(struct tar_header *hdr, const EVP_MD *md, const unsigned char *digest)
|
2014-11-01 15:09:13 +00:00
|
|
|
{
|
2009-07-16 09:03:26 +00:00
|
|
|
struct {
|
|
|
|
char id[4];
|
|
|
|
uint16_t nid;
|
|
|
|
uint16_t size;
|
|
|
|
} mdinfo;
|
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
memcpy(mdinfo.id, "APK2", 4);
|
|
|
|
mdinfo.nid = EVP_MD_nid(md);
|
|
|
|
mdinfo.size = EVP_MD_size(md);
|
|
|
|
memcpy(&hdr->linkname[3], &mdinfo, sizeof(mdinfo));
|
|
|
|
memcpy(&hdr->linkname[3+sizeof(mdinfo)], digest, mdinfo.size);
|
|
|
|
tarhdr_checksum(hdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_it(const EVP_MD *md, int cut)
|
|
|
|
{
|
2020-07-06 10:26:24 +00:00
|
|
|
char checksumhdr[32];
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
2014-11-01 15:09:13 +00:00
|
|
|
struct buf data = BUF_INITIALIZER, pax = BUF_INITIALIZER;
|
|
|
|
struct tar_header hdr, paxhdr;
|
2021-07-24 11:40:17 +00:00
|
|
|
size_t name_len, size, aligned_size;
|
2014-11-01 15:09:13 +00:00
|
|
|
int dohash = 0, r, ret = 1;
|
|
|
|
|
2015-10-06 10:38:57 +00:00
|
|
|
memset(&paxhdr, 0, sizeof(paxhdr));
|
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
if (md) snprintf(checksumhdr, sizeof(checksumhdr), "APK-TOOLS.checksum.%s", EVP_MD_name(md));
|
2009-07-16 09:03:26 +00:00
|
|
|
|
|
|
|
do {
|
2009-07-20 09:59:31 +00:00
|
|
|
if (full_read(STDIN_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
|
2014-11-01 15:09:13 +00:00
|
|
|
goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
|
|
|
|
if (cut && hdr.name[0] == 0)
|
2014-11-01 15:09:13 +00:00
|
|
|
break;
|
2009-07-16 09:03:26 +00:00
|
|
|
|
|
|
|
size = GET_OCTAL(hdr.size);
|
|
|
|
aligned_size = (size + 511) & ~511;
|
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
if (hdr.typeflag == 'x') {
|
|
|
|
memcpy(&paxhdr, &hdr, sizeof(hdr));
|
|
|
|
if (buf_read_fd(&pax, STDIN_FILENO, aligned_size)) goto err;
|
|
|
|
pax.size = size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dohash = md && (hdr.typeflag == '0' || hdr.typeflag == '7' || hdr.typeflag == '2');
|
2009-08-17 12:02:18 +00:00
|
|
|
if (dohash) {
|
2014-11-01 15:09:13 +00:00
|
|
|
if (buf_read_fd(&data, STDIN_FILENO, aligned_size)) goto err;
|
|
|
|
|
|
|
|
if (hdr.typeflag != '2') {
|
|
|
|
EVP_Digest(data.ptr, size, digest, NULL, md, NULL);
|
|
|
|
add_legacy_checksum(&hdr, md, digest);
|
|
|
|
} else {
|
2021-07-24 11:40:17 +00:00
|
|
|
name_len = strnlen(hdr.linkname, sizeof(hdr.linkname));
|
|
|
|
EVP_Digest(hdr.linkname, name_len, digest, NULL, md, NULL);
|
2014-11-01 15:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf_add_ext_header_hexdump(&pax, checksumhdr, digest, EVP_MD_size(md));
|
|
|
|
PUT_OCTAL(paxhdr.size, pax.size);
|
|
|
|
tarhdr_checksum(&paxhdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pax.size) {
|
|
|
|
/* write pax header + content */
|
|
|
|
if (full_write(STDOUT_FILENO, &paxhdr, sizeof(paxhdr)) != sizeof(paxhdr) ||
|
|
|
|
buf_padto(&pax, 512) ||
|
|
|
|
buf_write_fd(&pax, STDOUT_FILENO)) goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-01 15:09:13 +00:00
|
|
|
if (full_write(STDOUT_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr)) goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
|
2009-08-17 12:02:18 +00:00
|
|
|
if (dohash) {
|
2014-11-01 15:09:13 +00:00
|
|
|
if (buf_write_fd(&data, STDOUT_FILENO)) goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
} else if (aligned_size != 0) {
|
2009-07-20 09:59:31 +00:00
|
|
|
r = full_splice(STDIN_FILENO, STDOUT_FILENO, aligned_size);
|
2009-07-16 09:03:26 +00:00
|
|
|
if (r == -1) {
|
|
|
|
while (aligned_size > 0) {
|
2009-07-20 09:59:31 +00:00
|
|
|
if (full_read(STDIN_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
|
2014-11-01 15:09:13 +00:00
|
|
|
goto err;
|
2009-07-20 09:59:31 +00:00
|
|
|
if (full_write(STDOUT_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
|
2014-11-01 15:09:13 +00:00
|
|
|
goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
aligned_size -= sizeof(hdr);
|
|
|
|
}
|
2014-11-01 15:09:13 +00:00
|
|
|
} else if (r != aligned_size) goto err;
|
2009-07-16 09:03:26 +00:00
|
|
|
}
|
2014-11-01 15:09:13 +00:00
|
|
|
|
|
|
|
memset(&paxhdr, 0, sizeof(paxhdr));
|
|
|
|
pax.size = 0;
|
2009-07-16 09:03:26 +00:00
|
|
|
} while (1);
|
2014-11-01 15:09:13 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
buf_free(&data);
|
|
|
|
buf_free(&pax);
|
|
|
|
return ret;
|
2009-07-16 09:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2022-11-30 10:59:30 +00:00
|
|
|
static int cut = 0, help = 0;
|
2009-07-16 09:03:26 +00:00
|
|
|
static const struct option options[] = {
|
|
|
|
{ "hash", optional_argument },
|
2022-11-30 10:59:30 +00:00
|
|
|
{ "help", no_argument, &help, 1 },
|
2009-07-20 09:59:31 +00:00
|
|
|
{ "cut", no_argument, &cut, 1 },
|
2009-07-16 09:03:26 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
const EVP_MD *md = NULL;
|
|
|
|
char *digest = NULL;
|
|
|
|
int ndx;
|
|
|
|
|
|
|
|
OpenSSL_add_all_algorithms();
|
2021-09-21 09:29:13 +00:00
|
|
|
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR < 3
|
|
|
|
ENGINE_load_builtin_engines();
|
|
|
|
ENGINE_register_all_complete();
|
|
|
|
#endif
|
2009-07-16 09:03:26 +00:00
|
|
|
|
2023-04-17 11:01:05 +00:00
|
|
|
int c;
|
|
|
|
while ((c = getopt_long(argc, argv, "", options, &ndx)) != -1) {
|
|
|
|
if (c == '?')
|
|
|
|
return usage(stderr);
|
2009-07-16 09:03:26 +00:00
|
|
|
if (ndx == 0)
|
|
|
|
digest = optarg ? optarg : "sha1";
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:59:30 +00:00
|
|
|
if (help)
|
|
|
|
return usage(stdout) && 0;
|
|
|
|
|
2009-07-16 09:03:26 +00:00
|
|
|
if (digest == NULL && cut == 0)
|
2022-11-30 10:59:30 +00:00
|
|
|
return usage(stderr);
|
2009-07-16 09:03:26 +00:00
|
|
|
if (isatty(STDIN_FILENO))
|
2022-11-30 10:59:30 +00:00
|
|
|
return usage(stderr);
|
2009-07-16 09:03:26 +00:00
|
|
|
|
|
|
|
if (digest != NULL) {
|
|
|
|
md = EVP_get_digestbyname(digest);
|
|
|
|
if (md == NULL)
|
2022-11-30 10:59:30 +00:00
|
|
|
return usage(stderr);
|
2009-07-16 09:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return do_it(md, cut);
|
|
|
|
}
|