abuild-tar.c 8.18 KB
Newer Older
1 2
/* abuild-tar.c - A TAR mangling utility for .APK packages
 *
3
 * Copyright (C) 2009-2014 Timo Teräs <timo.teras@iki.fi>
4 5 6 7 8 9 10 11 12
 * 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.
 */

#include <stdio.h>
#include <fcntl.h>
13 14
#include <errno.h>
#include <stdint.h>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <getopt.h>
#include <unistd.h>
#include <string.h>

#include <openssl/evp.h>

#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 */
42
	char padding[12];   /* 500-511 */
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
};

#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;
}

static int get_octal(char *s, size_t l)
{
	unsigned int val;
	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';
}

92 93 94 95 96 97 98 99 100 101 102 103 104
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);
}

105 106 107 108 109
static int usage(void)
{
	fprintf(stderr,
"abuild-tar " VERSION "\n"
"\n"
110
"usage: abuild-tar [--hash[=<algorithm>]] [--cut]\n"
111 112
"\n"
"options:\n"
113
"  --hash[=sha1|md5]	Read tar archive from stdin, precalculate hash for \n"
114
"			regular	entries and output tar archive on stdout\n"
Timo Teräs's avatar
Timo Teräs committed
115
"  --cut		Remove the end of file tar record\n"
116 117 118
"\n");
}

119 120 121 122 123 124 125 126 127 128 129 130
static ssize_t full_read(int fd, void *buf, size_t count)
{
	ssize_t total, n;

	total = 0;
	do {
		n = read(fd, buf, count);
		if (n < 0 && errno == EINTR)
			continue;
		if (n <= 0)
			break;
		buf += n;
131
		total += n;
132 133 134 135
		count -= n;
	} while (1);

	if (total == 0 && n < 0)
136
		return -errno;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	return total;
}

static ssize_t full_write(int fd, const void *buf, size_t count)
{
	ssize_t total, n;

	total = 0;
	do {
		n = write(fd, buf, count);
		if (n < 0 && errno == EINTR)
			continue;
		if (n <= 0)
			break;
		buf += n;
153
		total += n;
154 155 156 157
		count -= n;
	} while (1);

	if (total == 0 && n < 0)
158
		return -errno;
159 160 161 162 163 164 165 166 167 168

	return total;
}

static ssize_t full_splice(int from_fd, int to_fd, size_t count)
{
	ssize_t total, n;

	total = 0;
	do {
169
		n = splice(from_fd, NULL, to_fd, NULL, count, 0);
170 171 172 173 174
		if (n < 0 && errno == EINTR)
			continue;
		if (n <= 0)
			break;
		count -= n;
175
		total += n;
176 177 178
	} while (1);

	if (total == 0 && n < 0)
179
		return -errno;
180 181 182 183

	return total;
}

184 185 186 187 188 189 190 191 192 193 194 195 196
#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)
197 198
{
	void *ptr;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	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;
	memset(b->ptr + oldsize, 0, newsize - oldsize);
	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;
}

static int buf_add_ext_header_hexdump(struct buf *b, const char *hdr, const char *value, int valuelen)
{
	size_t oldsize = b->size;
	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;
	b->size += snprintf(&b->ptr[b->size], len, "%u %s=", len, hdr);
	for (i = 0; i < valuelen; i++)
		b->size += snprintf(&b->ptr[b->size], 3, "%02x", (int)(unsigned char)value[i]);
	b->ptr[b->size++] = '\n';

	return 0;
}

static void add_legacy_checksum(struct tar_header *hdr, const EVP_MD *md, const char *digest)
{
265 266 267 268 269 270
	struct {
		char id[4];
		uint16_t nid;
		uint16_t size;
	} mdinfo;

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	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)
{
	char checksumhdr[32], digest[EVP_MAX_MD_SIZE];
	struct buf data = BUF_INITIALIZER, pax = BUF_INITIALIZER;
	struct tar_header hdr, paxhdr;
	size_t size, aligned_size;
	int dohash = 0, r, ret = 1;

	if (md) snprintf(checksumhdr, sizeof(checksumhdr), "APK-TOOLS.checksum.%s", EVP_MD_name(md));
288 289

	do {
290
		if (full_read(STDIN_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
291
			goto err;
292 293

		if (cut && hdr.name[0] == 0)
294
			break;
295 296 297 298

		size = GET_OCTAL(hdr.size);
		aligned_size = (size + 511) & ~511;

299 300 301 302 303 304 305 306
		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');
307
		if (dohash) {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
			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 {
				EVP_Digest(hdr.linkname, strlen(hdr.linkname), digest, NULL, md, NULL);
			}

			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;
327 328
		}

329
		if (full_write(STDOUT_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr)) goto err;
330

331
		if (dohash) {
332
			if (buf_write_fd(&data, STDOUT_FILENO)) goto err;
333
		} else if (aligned_size != 0) {
334
			r = full_splice(STDIN_FILENO, STDOUT_FILENO, aligned_size);
335 336
			if (r == -1) {
				while (aligned_size > 0) {
337
					if (full_read(STDIN_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
338
						goto err;
339
					if (full_write(STDOUT_FILENO, &hdr, sizeof(hdr)) != sizeof(hdr))
340
						goto err;
341 342
					aligned_size -= sizeof(hdr);
				}
343
			} else if (r != aligned_size) goto err;
344
		}
345 346 347

		memset(&paxhdr, 0, sizeof(paxhdr));
		pax.size = 0;
348
	} while (1);
349 350 351 352 353 354

	ret = 0;
err:
	buf_free(&data);
	buf_free(&pax);
	return ret;
355 356 357 358 359 360 361
}

int main(int argc, char **argv)
{
	static int cut = 0;
	static const struct option options[] = {
		{ "hash", optional_argument },
362
		{ "cut", no_argument, &cut, 1 },
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		{ NULL }
	};
	const EVP_MD *md = NULL;
	char *digest = NULL;
	int ndx;

	OpenSSL_add_all_algorithms();
	ENGINE_load_builtin_engines();
	ENGINE_register_all_complete();

	while (getopt_long(argc, argv, "", options, &ndx) != -1) {
		if (ndx == 0)
			digest = optarg ? optarg : "sha1";
	}

	if (digest == NULL && cut == 0)
		return usage();
	if (isatty(STDIN_FILENO))
		return usage();

	if (digest != NULL) {
		md = EVP_get_digestbyname(digest);
		if (md == NULL)
			return usage();
	}

	return do_it(md, cut);
}