xref: /freebsd/stand/libsa/pkgfs.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
1 /*-
2  * Copyright (c) 2007-2014, Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "stand.h"
31 
32 #include <sys/stat.h>
33 #include <sys/stdint.h>
34 #include <string.h>
35 #include <zlib.h>
36 
37 #ifdef PKGFS_DEBUG
38 #define	DBG(x)	printf x
39 #else
40 #define	DBG(x)
41 #endif
42 
43 static int   pkg_open(const char *, struct open_file *);
44 static int   pkg_close(struct open_file *);
45 static int   pkg_read(struct open_file *, void *, size_t, size_t *);
46 static off_t pkg_seek(struct open_file *, off_t, int);
47 static int   pkg_stat(struct open_file *, struct stat *);
48 static int   pkg_readdir(struct open_file *, struct dirent *);
49 static off_t pkg_atol(const char *, unsigned);
50 
51 struct fs_ops pkgfs_fsops = {
52 	"pkg",
53 	pkg_open,
54 	pkg_close,
55 	pkg_read,
56 	null_write,
57 	pkg_seek,
58 	pkg_stat,
59 	pkg_readdir
60 };
61 
62 #define PKG_BUFSIZE	512
63 #define	PKG_MAXCACHESZ	(16384 * 3)
64 
65 #define	PKG_FILEEXT	".tgz"
66 
67 /*
68  * Layout of POSIX 'ustar' header.
69  */
70 struct ustar_hdr {
71 	char	ut_name[100];
72 	char	ut_mode[8];
73 	char	ut_uid[8];
74 	char	ut_gid[8];
75 	char	ut_size[12];
76 	char	ut_mtime[12];
77 	char	ut_checksum[8];
78 	char	ut_typeflag[1];
79 	char	ut_linkname[100];
80 	char	ut_magic[6];		/* For POSIX: "ustar\0" */
81 	char	ut_version[2];		/* For POSIX: "00" */
82 	char	ut_uname[32];
83 	char	ut_gname[32];
84 	char	ut_rdevmajor[8];
85 	char	ut_rdevminor[8];
86 	union {
87 		struct {
88 			char	prefix[155];
89 		} posix;
90 		struct {
91 			char	atime[12];
92 			char	ctime[12];
93 			char	offset[12];
94 			char	longnames[4];
95 			char	unused[1];
96 			struct gnu_sparse {
97 				char	offset[12];
98 				char	numbytes[12];
99 			} sparse[4];
100 			char	isextended[1];
101 			char	realsize[12];
102 		} gnu;
103 	} u;
104 	u_char __padding[12];
105 };
106 
107 struct package;
108 
109 struct tarfile
110 {
111 	struct package *tf_pkg;
112 	struct tarfile *tf_next;
113 	struct ustar_hdr tf_hdr;
114 	off_t	tf_ofs;
115 	off_t	tf_size;
116 	off_t	tf_fp;
117 	size_t	tf_cachesz;
118 	void	*tf_cache;
119 };
120 
121 struct package
122 {
123 	struct package *pkg_chain;
124 	int	pkg_fd;
125 	off_t	pkg_ofs;
126 	z_stream pkg_zs;
127 	struct tarfile *pkg_first;
128 	struct tarfile *pkg_last;
129 	u_char	pkg_buf[PKG_BUFSIZE];
130 };
131 
132 static struct package *package = NULL;
133 
134 static int new_package(int, struct package **);
135 static int cache_data(struct tarfile *tf, int);
136 
137 void
138 pkgfs_cleanup(void)
139 {
140 	struct package *chain;
141 	struct tarfile *tf, *tfn;
142 
143 	while (package != NULL) {
144 		inflateEnd(&package->pkg_zs);
145 		close(package->pkg_fd);
146 
147 		tf = package->pkg_first;
148 		while (tf != NULL) {
149 			tfn = tf->tf_next;
150 			if (tf->tf_cachesz > 0)
151 				free(tf->tf_cache);
152 			free(tf);
153 			tf = tfn;
154 		}
155 
156 		chain = package->pkg_chain;
157 		free(package);
158 		package = chain;
159 	}
160 }
161 
162 int
163 pkgfs_init(const char *pkgname, struct fs_ops *proto)
164 {
165 	struct package *pkg;
166 	int error, fd;
167 
168 	pkg = NULL;
169 	if (proto != &pkgfs_fsops)
170 		pkgfs_cleanup();
171 
172 	exclusive_file_system = proto;
173 
174 	fd = open(pkgname, O_RDONLY);
175 
176 	exclusive_file_system = NULL;
177 
178 	if (fd == -1)
179 		return (errno);
180 
181 	error = new_package(fd, &pkg);
182 	if (error) {
183 		close(fd);
184 		return (error);
185 	}
186 
187 	if (pkg == NULL)
188 		return (EDOOFUS);
189 
190 	pkg->pkg_chain = package;
191 	package = pkg;
192 	exclusive_file_system = &pkgfs_fsops;
193 	return (0);
194 }
195 
196 static int get_mode(struct tarfile *);
197 static int get_zipped(struct package *, void *, size_t);
198 static int new_package(int, struct package **);
199 static struct tarfile *scan_tarfile(struct package *, struct tarfile *);
200 
201 static int
202 pkg_open(const char *fn, struct open_file *f)
203 {
204 	struct tarfile *tf;
205 
206 	if (fn == NULL || f == NULL)
207 		return (EINVAL);
208 
209 	if (package == NULL)
210 		return (ENXIO);
211 
212 	/*
213 	 * We can only read from a package, so reject request to open
214 	 * for write-only or read-write.
215 	 */
216 	if (f->f_flags != F_READ)
217 		return (EPERM);
218 
219 	/*
220 	 * Scan the file headers for the named file. We stop scanning
221 	 * at the first filename that has the .pkg extension. This is
222 	 * a package within a package. We assume we have all the files
223 	 * we need up-front and without having to dig within nested
224 	 * packages.
225 	 *
226 	 * Note that we preserve streaming properties as much as possible.
227 	 */
228 	while (*fn == '/')
229 		fn++;
230 
231 	/*
232 	 * Allow opening of the root directory for use by readdir()
233 	 * to support listing files in the package.
234 	 */
235 	if (*fn == '\0') {
236 		f->f_fsdata = NULL;
237 		return (0);
238 	}
239 
240 	tf = scan_tarfile(package, NULL);
241 	while (tf != NULL) {
242 		if (strcmp(fn, tf->tf_hdr.ut_name) == 0) {
243 			f->f_fsdata = tf;
244 			tf->tf_fp = 0;	/* Reset the file pointer. */
245 			return (0);
246 		}
247 		tf = scan_tarfile(package, tf);
248 	}
249 	return (errno);
250 }
251 
252 static int
253 pkg_close(struct open_file *f)
254 {
255 	struct tarfile *tf;
256 
257 	tf = (struct tarfile *)f->f_fsdata;
258 	if (tf == NULL)
259 		return (0);
260 
261 	/*
262 	 * Free up the cache if we read all of the file.
263 	 */
264 	if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) {
265 		free(tf->tf_cache);
266 		tf->tf_cachesz = 0;
267 	}
268 	return (0);
269 }
270 
271 static int
272 pkg_read(struct open_file *f, void *buf, size_t size, size_t *res)
273 {
274 	struct tarfile *tf;
275 	char *p;
276 	off_t fp;
277 	size_t sz;
278 
279 	tf = (struct tarfile *)f->f_fsdata;
280 	if (tf == NULL) {
281 		if (res != NULL)
282 			*res = size;
283 		return (EBADF);
284 	}
285 
286 	if (tf->tf_cachesz == 0)
287 		cache_data(tf, 1);
288 
289 	fp = tf->tf_fp;
290 	p = buf;
291 	sz = 0;
292 	while (size > 0) {
293 		sz = tf->tf_size - fp;
294 		if (fp < tf->tf_cachesz && tf->tf_cachesz < tf->tf_size)
295 			sz = tf->tf_cachesz - fp;
296 		if (size < sz)
297 			sz = size;
298 		if (sz == 0)
299 			break;
300 
301 		if (fp < tf->tf_cachesz) {
302 			/* Satisfy the request from cache. */
303 			memcpy(p, tf->tf_cache + fp, sz);
304 			fp += sz;
305 			p += sz;
306 			size -= sz;
307 			continue;
308 		}
309 
310 		if (get_zipped(tf->tf_pkg, p, sz) == -1) {
311 			sz = -1;
312 			break;
313 		}
314 
315 		fp += sz;
316 		p += sz;
317 		size -= sz;
318 	}
319 
320 	tf->tf_fp = fp;
321 	if (res != NULL)
322 		*res = size;
323 	return ((sz == -1) ? errno : 0);
324 }
325 
326 static off_t
327 pkg_seek(struct open_file *f, off_t ofs, int whence)
328 {
329 	char buf[512];
330 	struct tarfile *tf;
331 	off_t delta;
332 	off_t nofs;
333 	size_t sz, res;
334 	int error;
335 
336 	tf = (struct tarfile *)f->f_fsdata;
337 	if (tf == NULL) {
338 		errno = EBADF;
339 		return (-1);
340 	}
341 
342 	switch (whence) {
343 	case SEEK_SET:
344 		delta = ofs - tf->tf_fp;
345 		break;
346 	case SEEK_CUR:
347 		delta = ofs;
348 		break;
349 	case SEEK_END:
350 		delta = tf->tf_size - tf->tf_fp + ofs;
351 		break;
352 	default:
353 		errno = EINVAL;
354 		return (-1);
355 	}
356 
357 	if (delta < 0) {
358 		/* seeking backwards - ok if within cache */
359 		if (tf->tf_cachesz > 0 && tf->tf_fp <= tf->tf_cachesz) {
360 			nofs = tf->tf_fp + delta;
361 			if (nofs >= 0) {
362 				tf->tf_fp = nofs;
363 				return (tf->tf_fp);
364 			}
365 		}
366 		DBG(("%s: negative file seek (%jd)\n", __func__,
367 		    (intmax_t)delta));
368 		errno = ESPIPE;
369 		return (-1);
370 	}
371 
372 	while (delta > 0 && tf->tf_fp < tf->tf_size) {
373 		sz = (delta > sizeof(buf)) ? sizeof(buf) : delta;
374 		error = pkg_read(f, buf, sz, &res);
375 		if (error != 0) {
376 			errno = error;
377 			return (-1);
378 		}
379 		delta -= sz - res;
380 	}
381 
382 	return (tf->tf_fp);
383 }
384 
385 static int
386 pkg_stat(struct open_file *f, struct stat *sb)
387 {
388 	struct tarfile *tf;
389 
390 	tf = (struct tarfile *)f->f_fsdata;
391 	if (tf == NULL)
392 		return (EBADF);
393 	memset(sb, 0, sizeof(*sb));
394 	sb->st_mode = get_mode(tf);
395 	if ((sb->st_mode & S_IFMT) == 0) {
396 		/* tar file bug - assume regular file */
397 		sb->st_mode |= S_IFREG;
398 	}
399 	sb->st_size = tf->tf_size;
400 	sb->st_blocks = (tf->tf_size + 511) / 512;
401 	sb->st_mtime = pkg_atol(tf->tf_hdr.ut_mtime, 12);
402 	sb->st_dev = (off_t)((uintptr_t)tf->tf_pkg);
403 	sb->st_ino = tf->tf_ofs;	/* unique per tf_pkg */
404 	return (0);
405 }
406 
407 static int
408 pkg_readdir(struct open_file *f, struct dirent *d)
409 {
410 	struct tarfile *tf;
411 
412 	tf = (struct tarfile *)f->f_fsdata;
413 	if (tf != NULL)
414 		return (EBADF);
415 
416 	tf = scan_tarfile(package, NULL);
417 	if (tf == NULL)
418 		return (ENOENT);
419 
420 	d->d_fileno = 0;
421 	d->d_reclen = sizeof(*d);
422 	d->d_type = DT_REG;
423 	memcpy(d->d_name, tf->tf_hdr.ut_name, sizeof(d->d_name));
424 	return (0);
425 }
426 
427 /*
428  * Low-level support functions.
429  */
430 
431 static int
432 get_byte(struct package *pkg, off_t *op)
433 {
434 	int c;
435 
436 	if (pkg->pkg_zs.avail_in == 0) {
437 		c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
438 		if (c <= 0)
439 			return (-1);
440 		pkg->pkg_zs.avail_in = c;
441 		pkg->pkg_zs.next_in = pkg->pkg_buf;
442 	}
443 
444 	c = *pkg->pkg_zs.next_in;
445 	pkg->pkg_zs.next_in++;
446 	pkg->pkg_zs.avail_in--;
447 	(*op)++;
448 	return (c);
449 }
450 
451 static int
452 get_zipped(struct package *pkg, void *buf, size_t bufsz)
453 {
454 	int c;
455 
456 	pkg->pkg_zs.next_out = buf;
457 	pkg->pkg_zs.avail_out = bufsz;
458 
459 	while (pkg->pkg_zs.avail_out) {
460 		if (pkg->pkg_zs.avail_in == 0) {
461 			c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
462 			if (c <= 0) {
463 				errno = EIO;
464 				return (-1);
465 			}
466 			pkg->pkg_zs.avail_in = c;
467 			pkg->pkg_zs.next_in = pkg->pkg_buf;
468 		}
469 
470 		c = inflate(&pkg->pkg_zs, Z_SYNC_FLUSH);
471 		if (c != Z_OK && c != Z_STREAM_END) {
472 			errno = EIO;
473 			return (-1);
474 		}
475 	}
476 
477 	pkg->pkg_ofs += bufsz;
478 	return (0);
479 }
480 
481 /**
482  * @brief
483  * cache data of a tarfile
484  *
485  * @param[in] tf
486  *	tarfile pointer
487  *
488  * @param[in] force
489  *	If file size > PKG_MAXCACHESZ, cache that much
490  *
491  * @return 0, -1 (errno set to error value)
492  */
493 static int
494 cache_data(struct tarfile *tf, int force)
495 {
496 	struct package *pkg;
497 	size_t sz;
498 
499 	if (tf == NULL) {
500 		DBG(("%s: no file to cache data for?\n", __func__));
501 		errno = EINVAL;
502 		return (-1);
503 	}
504 
505 	pkg = tf->tf_pkg;
506 	if (pkg == NULL) {
507 		DBG(("%s: no package associated with file?\n", __func__));
508 		errno = EINVAL;
509 		return (-1);
510 	}
511 
512 	if (tf->tf_cachesz > 0) {
513 		DBG(("%s: data already cached\n", __func__));
514 		errno = EINVAL;
515 		return (-1);
516 	}
517 
518 	if (tf->tf_ofs != pkg->pkg_ofs) {
519 		DBG(("%s: caching after force read of file %s?\n",
520 		    __func__, tf->tf_hdr.ut_name));
521 		errno = EINVAL;
522 		return (-1);
523 	}
524 
525 	/* We don't cache everything... */
526 	if (tf->tf_size > PKG_MAXCACHESZ && !force)  {
527 		errno = ENOBUFS;
528 		return (-1);
529 	}
530 
531 	sz = tf->tf_size < PKG_MAXCACHESZ ? tf->tf_size : PKG_MAXCACHESZ;
532 	/* All files are padded to a multiple of 512 bytes. */
533 	sz = (sz + 0x1ff) & ~0x1ff;
534 
535 	tf->tf_cache = malloc(sz);
536 	if (tf->tf_cache == NULL) {
537 		DBG(("%s: could not allocate %d bytes\n", __func__, (int)sz));
538 		errno = ENOMEM;
539 		return (-1);
540 	}
541 
542 	tf->tf_cachesz = sz;
543 	return (get_zipped(pkg, tf->tf_cache, sz));
544 }
545 
546 /*
547  * Note that this implementation does not (and should not!) obey
548  * locale settings; you cannot simply substitute strtol here, since
549  * it does obey locale.
550  */
551 static off_t
552 pkg_atol8(const char *p, unsigned char_cnt)
553 {
554         int64_t l, limit, last_digit_limit;
555         int digit, sign, base;
556 
557         base = 8;
558         limit = INT64_MAX / base;
559         last_digit_limit = INT64_MAX % base;
560 
561         while (*p == ' ' || *p == '\t')
562                 p++;
563         if (*p == '-') {
564                 sign = -1;
565                 p++;
566         } else
567                 sign = 1;
568 
569         l = 0;
570         digit = *p - '0';
571         while (digit >= 0 && digit < base  && char_cnt-- > 0) {
572                 if (l>limit || (l == limit && digit > last_digit_limit)) {
573                         l = UINT64_MAX; /* Truncate on overflow. */
574                         break;
575                 }
576                 l = (l * base) + digit;
577                 digit = *++p - '0';
578         }
579         return (sign < 0) ? -l : l;
580 }
581 
582 /*
583  * Parse a base-256 integer.  This is just a straight signed binary
584  * value in big-endian order, except that the high-order bit is
585  * ignored.  Remember that "int64_t" may or may not be exactly 64
586  * bits; the implementation here tries to avoid making any assumptions
587  * about the actual size of an int64_t.  It does assume we're using
588  * twos-complement arithmetic, though.
589  */
590 static int64_t
591 pkg_atol256(const char *_p, unsigned char_cnt)
592 {
593         int64_t l, upper_limit, lower_limit;
594         const unsigned char *p = (const unsigned char *)_p;
595 
596         upper_limit = INT64_MAX / 256;
597         lower_limit = INT64_MIN / 256;
598 
599         /* Pad with 1 or 0 bits, depending on sign. */
600         if ((0x40 & *p) == 0x40)
601                 l = (int64_t)-1;
602         else
603                 l = 0;
604         l = (l << 6) | (0x3f & *p++);
605         while (--char_cnt > 0) {
606                 if (l > upper_limit) {
607                         l = INT64_MAX; /* Truncate on overflow */
608                         break;
609                 } else if (l < lower_limit) {
610                         l = INT64_MIN;
611                         break;
612                 }
613                 l = (l << 8) | (0xff & (int64_t)*p++);
614         }
615         return (l);
616 }
617 
618 static off_t
619 pkg_atol(const char *p, unsigned char_cnt)
620 {
621 	/*
622 	 * Technically, GNU pkg considers a field to be in base-256
623 	 * only if the first byte is 0xff or 0x80.
624 	 */
625 	if (*p & 0x80)
626 		return (pkg_atol256(p, char_cnt));
627 	return (pkg_atol8(p, char_cnt));
628 }
629 
630 static int
631 get_mode(struct tarfile *tf)
632 {
633 	return (pkg_atol(tf->tf_hdr.ut_mode, sizeof(tf->tf_hdr.ut_mode)));
634 }
635 
636 /* GZip flag byte */
637 #define ASCII_FLAG	0x01 /* bit 0 set: file probably ascii text */
638 #define HEAD_CRC	0x02 /* bit 1 set: header CRC present */
639 #define EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
640 #define ORIG_NAME	0x08 /* bit 3 set: original file name present */
641 #define COMMENT		0x10 /* bit 4 set: file comment present */
642 #define RESERVED	0xE0 /* bits 5..7: reserved */
643 
644 static int
645 new_package(int fd, struct package **pp)
646 {
647 	struct package *pkg;
648 	off_t ofs;
649 	int flags, i, error;
650 
651 	pkg = malloc(sizeof(*pkg));
652 	if (pkg == NULL)
653 		return (ENOMEM);
654 
655 	bzero(pkg, sizeof(*pkg));
656 	pkg->pkg_fd = fd;
657 
658 	/*
659 	 * Parse the header.
660 	 */
661 	error = EFTYPE;
662 	ofs = 0;
663 
664 	/* Check megic. */
665 	if (get_byte(pkg, &ofs) != 0x1f || get_byte(pkg, &ofs) != 0x8b)
666 		goto fail;
667 	/* Check method. */
668 	if (get_byte(pkg, &ofs) != Z_DEFLATED)
669 		goto fail;
670 	/* Check flags. */
671 	flags = get_byte(pkg, &ofs);
672 	if (flags & RESERVED)
673 		goto fail;
674 
675 	/* Skip time, xflags and OS code. */
676 	for (i = 0; i < 6; i++) {
677 		if (get_byte(pkg, &ofs) == -1)
678 			goto fail;
679 	}
680 
681 	/* Skip extra field. */
682 	if (flags & EXTRA_FIELD) {
683 		i = (get_byte(pkg, &ofs) & 0xff) |
684 		    ((get_byte(pkg, &ofs) << 8) & 0xff);
685 		while (i-- > 0) {
686 			if (get_byte(pkg, &ofs) == -1)
687 				goto fail;
688 		}
689 	}
690 
691 	/* Skip original file name. */
692 	if (flags & ORIG_NAME) {
693 		do {
694 			i = get_byte(pkg, &ofs);
695 		} while (i != 0 && i != -1);
696 		if (i == -1)
697 			goto fail;
698 	}
699 
700 	/* Print the comment if it's there. */
701 	if (flags & COMMENT) {
702 		while (1) {
703 			i = get_byte(pkg, &ofs);
704 			if (i == -1)
705 				goto fail;
706 			if (i == 0)
707 				break;
708 			putchar(i);
709 		}
710 	}
711 
712 	/* Skip the CRC. */
713 	if (flags & HEAD_CRC) {
714 		if (get_byte(pkg, &ofs) == -1)
715 			goto fail;
716 		if (get_byte(pkg, &ofs) == -1)
717 			goto fail;
718 	}
719 
720 	/*
721 	 * Done parsing the ZIP header. Spkgt the inflation engine.
722 	 */
723 	error = inflateInit2(&pkg->pkg_zs, -15);
724 	if (error != Z_OK)
725 		goto fail;
726 
727 	*pp = pkg;
728 	return (0);
729 
730  fail:
731 	free(pkg);
732 	return (error);
733 }
734 
735 static struct tarfile *
736 scan_tarfile(struct package *pkg, struct tarfile *last)
737 {
738 	char buf[512];
739 	struct tarfile *cur;
740 	off_t ofs;
741 	size_t sz;
742 
743 	cur = (last != NULL) ? last->tf_next : pkg->pkg_first;
744 	if (cur == NULL) {
745 		ofs = (last != NULL) ? last->tf_ofs + last->tf_size :
746 		    pkg->pkg_ofs;
747 		ofs = (ofs + 0x1ff) & ~0x1ff;
748 
749 		/* Check if we've reached EOF. */
750 		if (ofs < pkg->pkg_ofs) {
751 			errno = ENOSPC;
752 			return (NULL);
753 		}
754 
755 		if (ofs != pkg->pkg_ofs) {
756 			if (last != NULL && pkg->pkg_ofs == last->tf_ofs) {
757 				if (cache_data(last, 0) == -1)
758 					return (NULL);
759 			} else {
760 				sz = ofs - pkg->pkg_ofs;
761 				while (sz != 0) {
762 					if (sz > sizeof(buf))
763 						sz = sizeof(buf);
764 					if (get_zipped(pkg, buf, sz) == -1)
765 						return (NULL);
766 					sz = ofs - pkg->pkg_ofs;
767 				}
768 			}
769 		}
770 
771 		cur = malloc(sizeof(*cur));
772 		if (cur == NULL)
773 			return (NULL);
774 		memset(cur, 0, sizeof(*cur));
775 		cur->tf_pkg = pkg;
776 
777 		while (1) {
778 			if (get_zipped(pkg, &cur->tf_hdr,
779 			    sizeof(cur->tf_hdr)) == -1) {
780 				free(cur);
781 				return (NULL);
782 			}
783 
784 			/*
785 			 * There are always 2 empty blocks appended to
786 			 * a PKG. It marks the end of the archive.
787 			 */
788 			if (strncmp(cur->tf_hdr.ut_magic, "ustar", 5) != 0) {
789 				free(cur);
790 				errno = ENOSPC;
791 				return (NULL);
792 			}
793 
794 			cur->tf_ofs = pkg->pkg_ofs;
795 			cur->tf_size = pkg_atol(cur->tf_hdr.ut_size,
796 			    sizeof(cur->tf_hdr.ut_size));
797 
798 			if (cur->tf_hdr.ut_name[0] != '+')
799 				break;
800 
801 			/*
802 			 * Skip package meta-files.
803 			 */
804 			ofs = cur->tf_ofs + cur->tf_size;
805 			ofs = (ofs + 0x1ff) & ~0x1ff;
806 			while (pkg->pkg_ofs < ofs) {
807 				if (get_zipped(pkg, buf, sizeof(buf)) == -1) {
808 					free(cur);
809 					return (NULL);
810 				}
811 			}
812 		}
813 
814 		if (last != NULL)
815 			last->tf_next = cur;
816 		else
817 			pkg->pkg_first = cur;
818 		pkg->pkg_last = cur;
819 	}
820 
821 	return (cur);
822 }
823