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