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