Home | History | Annotate | Line # | Download | only in pax
cpio.c revision 1.22.34.2
      1  1.22.34.1    martin /*	$NetBSD: cpio.c,v 1.22.34.2 2020/04/21 19:37:33 martin Exp $	*/
      2        1.5       cgd 
      3        1.1       jtc /*-
      4       1.16       agc  * Copyright (c) 1992 Keith Muller.
      5        1.1       jtc  * Copyright (c) 1992, 1993
      6        1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7        1.1       jtc  *
      8        1.1       jtc  * This code is derived from software contributed to Berkeley by
      9        1.1       jtc  * Keith Muller of the University of California, San Diego.
     10        1.1       jtc  *
     11        1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12        1.1       jtc  * modification, are permitted provided that the following conditions
     13        1.1       jtc  * are met:
     14        1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15        1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16        1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18        1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19       1.15       agc  * 3. Neither the name of the University nor the names of its contributors
     20       1.15       agc  *    may be used to endorse or promote products derived from this software
     21       1.15       agc  *    without specific prior written permission.
     22       1.15       agc  *
     23       1.15       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.15       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.15       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.15       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.15       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.15       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.15       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.15       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.15       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.15       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.15       agc  * SUCH DAMAGE.
     34       1.15       agc  */
     35       1.15       agc 
     36       1.17     lukem #if HAVE_NBTOOL_CONFIG_H
     37       1.17     lukem #include "nbtool_config.h"
     38       1.17     lukem #endif
     39       1.17     lukem 
     40        1.7  christos #include <sys/cdefs.h>
     41       1.17     lukem #if !defined(lint)
     42        1.5       cgd #if 0
     43        1.5       cgd static char sccsid[] = "@(#)cpio.c	8.1 (Berkeley) 5/31/93";
     44        1.5       cgd #else
     45  1.22.34.1    martin __RCSID("$NetBSD: cpio.c,v 1.22.34.2 2020/04/21 19:37:33 martin Exp $");
     46        1.5       cgd #endif
     47        1.1       jtc #endif /* not lint */
     48        1.1       jtc 
     49        1.1       jtc #include <sys/types.h>
     50        1.1       jtc #include <sys/time.h>
     51        1.1       jtc #include <sys/stat.h>
     52        1.1       jtc #include <sys/param.h>
     53        1.1       jtc #include <string.h>
     54        1.1       jtc #include <ctype.h>
     55        1.1       jtc #include <stdio.h>
     56        1.1       jtc #include <unistd.h>
     57        1.1       jtc #include <stdlib.h>
     58        1.1       jtc #include "pax.h"
     59        1.1       jtc #include "cpio.h"
     60        1.1       jtc #include "extern.h"
     61        1.1       jtc 
     62       1.11     lukem static int rd_nm(ARCHD *, int);
     63       1.11     lukem static int rd_ln_nm(ARCHD *);
     64       1.11     lukem static int com_rd(ARCHD *);
     65        1.1       jtc 
     66        1.1       jtc /*
     67        1.1       jtc  * Routines which support the different cpio versions
     68        1.1       jtc  */
     69        1.1       jtc 
     70        1.8       mrg int cpio_swp_head;		/* binary cpio header byte swap */
     71        1.1       jtc 
     72        1.1       jtc /*
     73        1.1       jtc  * Routines common to all versions of cpio
     74        1.1       jtc  */
     75        1.1       jtc 
     76        1.1       jtc /*
     77        1.1       jtc  * cpio_strd()
     78        1.1       jtc  *	Fire up the hard link detection code
     79        1.1       jtc  * Return:
     80        1.9     itohy  *	0 if ok -1 otherwise (the return values of lnk_start())
     81        1.1       jtc  */
     82        1.1       jtc 
     83        1.1       jtc int
     84        1.1       jtc cpio_strd(void)
     85        1.1       jtc {
     86       1.19       dsl 	return lnk_start();
     87        1.1       jtc }
     88        1.1       jtc 
     89        1.1       jtc /*
     90        1.7  christos  * cpio_subtrail()
     91        1.1       jtc  *	Called to determine if a header block is a valid trailer. We are
     92        1.1       jtc  *	passed the block, the in_sync flag (which tells us we are in resync
     93        1.1       jtc  *	mode; looking for a valid header), and cnt (which starts at zero)
     94        1.1       jtc  *	which is used to count the number of empty blocks we have seen so far.
     95        1.1       jtc  * Return:
     96        1.9     itohy  *	0 if a valid trailer, -1 if not a valid trailer,
     97        1.1       jtc  */
     98        1.1       jtc 
     99        1.1       jtc int
    100        1.7  christos cpio_subtrail(ARCHD *arcn)
    101        1.1       jtc {
    102        1.1       jtc 	/*
    103        1.1       jtc 	 * look for trailer id in file we are about to process
    104        1.1       jtc 	 */
    105        1.1       jtc 	if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
    106       1.19       dsl 		return 0;
    107       1.19       dsl 	return -1;
    108        1.1       jtc }
    109        1.1       jtc 
    110        1.1       jtc /*
    111        1.1       jtc  * com_rd()
    112        1.1       jtc  *	operations common to all cpio read functions.
    113        1.1       jtc  * Return:
    114        1.1       jtc  *	0
    115        1.1       jtc  */
    116        1.1       jtc 
    117        1.1       jtc static int
    118        1.6       tls com_rd(ARCHD *arcn)
    119        1.1       jtc {
    120        1.1       jtc 	arcn->skip = 0;
    121        1.1       jtc 	arcn->pat = NULL;
    122        1.1       jtc 	arcn->org_name = arcn->name;
    123        1.1       jtc 	switch(arcn->sb.st_mode & C_IFMT) {
    124        1.1       jtc 	case C_ISFIFO:
    125        1.1       jtc 		arcn->type = PAX_FIF;
    126        1.1       jtc 		break;
    127        1.1       jtc 	case C_ISDIR:
    128        1.1       jtc 		arcn->type = PAX_DIR;
    129        1.1       jtc 		break;
    130        1.1       jtc 	case C_ISBLK:
    131        1.1       jtc 		arcn->type = PAX_BLK;
    132        1.1       jtc 		break;
    133        1.1       jtc 	case C_ISCHR:
    134        1.1       jtc 		arcn->type = PAX_CHR;
    135        1.1       jtc 		break;
    136        1.1       jtc 	case C_ISLNK:
    137        1.1       jtc 		arcn->type = PAX_SLK;
    138        1.1       jtc 		break;
    139        1.1       jtc 	case C_ISOCK:
    140        1.1       jtc 		arcn->type = PAX_SCK;
    141        1.1       jtc 		break;
    142        1.1       jtc 	case C_ISCTG:
    143        1.1       jtc 	case C_ISREG:
    144        1.1       jtc 	default:
    145        1.1       jtc 		/*
    146        1.1       jtc 		 * we have file data, set up skip (pad is set in the format
    147        1.1       jtc 		 * specific sections)
    148        1.1       jtc 		 */
    149        1.1       jtc 		arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
    150        1.1       jtc 		arcn->type = PAX_REG;
    151        1.1       jtc 		arcn->skip = arcn->sb.st_size;
    152        1.1       jtc 		break;
    153        1.1       jtc 	}
    154        1.1       jtc 	if (chk_lnk(arcn) < 0)
    155       1.19       dsl 		return -1;
    156       1.19       dsl 	return 0;
    157        1.1       jtc }
    158        1.1       jtc 
    159        1.1       jtc /*
    160        1.1       jtc  * cpio_end_wr()
    161        1.1       jtc  *	write the special file with the name trailer in the proper format
    162        1.1       jtc  * Return:
    163        1.1       jtc  *	result of the write of the trailer from the cpio specific write func
    164        1.1       jtc  */
    165        1.1       jtc 
    166        1.1       jtc int
    167        1.1       jtc cpio_endwr(void)
    168        1.1       jtc {
    169        1.1       jtc 	ARCHD last;
    170        1.1       jtc 
    171        1.1       jtc 	/*
    172        1.1       jtc 	 * create a trailer request and call the proper format write function
    173        1.1       jtc 	 */
    174        1.4   mycroft 	memset(&last, 0, sizeof(last));
    175        1.1       jtc 	last.nlen = sizeof(TRAILER) - 1;
    176        1.1       jtc 	last.type = PAX_REG;
    177        1.1       jtc 	last.sb.st_nlink = 1;
    178        1.1       jtc 	(void)strcpy(last.name, TRAILER);
    179       1.19       dsl 	return (*frmt->wr)(&last);
    180        1.1       jtc }
    181        1.1       jtc 
    182        1.1       jtc /*
    183        1.1       jtc  * rd_nam()
    184        1.1       jtc  *	read in the file name which follows the cpio header
    185        1.1       jtc  * Return:
    186        1.1       jtc  *	0 if ok, -1 otherwise
    187        1.1       jtc  */
    188        1.1       jtc 
    189        1.1       jtc static int
    190        1.6       tls rd_nm(ARCHD *arcn, int nsz)
    191        1.1       jtc {
    192        1.1       jtc 	/*
    193        1.1       jtc 	 * do not even try bogus values
    194        1.1       jtc 	 */
    195       1.20     lukem 	if ((nsz <= 0) || (nsz > (int)sizeof(arcn->name))) {
    196        1.7  christos 		tty_warn(1, "Cpio file name length %d is out of range", nsz);
    197       1.19       dsl 		return -1;
    198        1.1       jtc 	}
    199        1.1       jtc 
    200        1.1       jtc 	/*
    201        1.1       jtc 	 * read the name and make sure it is not empty and is \0 terminated
    202        1.1       jtc 	 */
    203        1.1       jtc 	if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
    204        1.1       jtc 	    (arcn->name[0] == '\0')) {
    205        1.7  christos 		tty_warn(1, "Cpio file name in header is corrupted");
    206       1.19       dsl 		return -1;
    207        1.1       jtc 	}
    208       1.19       dsl 	return 0;
    209        1.1       jtc }
    210        1.1       jtc 
    211        1.1       jtc /*
    212        1.1       jtc  * rd_ln_nm()
    213        1.1       jtc  *	read in the link name for a file with links. The link name is stored
    214        1.1       jtc  *	like file data (and is NOT \0 terminated!)
    215        1.1       jtc  * Return:
    216        1.1       jtc  *	0 if ok, -1 otherwise
    217        1.1       jtc  */
    218        1.1       jtc 
    219        1.1       jtc static int
    220        1.6       tls rd_ln_nm(ARCHD *arcn)
    221        1.1       jtc {
    222        1.1       jtc 	/*
    223        1.1       jtc 	 * check the length specified for bogus values
    224        1.1       jtc 	 */
    225        1.1       jtc 	if ((arcn->sb.st_size == 0) ||
    226       1.20     lukem 	    (arcn->sb.st_size >= (off_t)sizeof(arcn->ln_name))) {
    227       1.11     lukem 		tty_warn(1, "Cpio link name length is invalid: " OFFT_F,
    228       1.11     lukem 		    (OFFT_T) arcn->sb.st_size);
    229       1.19       dsl 		return -1;
    230        1.1       jtc 	}
    231        1.1       jtc 
    232        1.1       jtc 	/*
    233        1.1       jtc 	 * read in the link name and \0 terminate it
    234        1.1       jtc 	 */
    235        1.1       jtc 	if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
    236        1.1       jtc 	    (int)arcn->sb.st_size) {
    237        1.7  christos 		tty_warn(1, "Cpio link name read error");
    238       1.19       dsl 		return -1;
    239        1.1       jtc 	}
    240        1.1       jtc 	arcn->ln_nlen = arcn->sb.st_size;
    241        1.1       jtc 	arcn->ln_name[arcn->ln_nlen] = '\0';
    242        1.1       jtc 
    243        1.1       jtc 	/*
    244        1.1       jtc 	 * watch out for those empty link names
    245        1.1       jtc 	 */
    246        1.1       jtc 	if (arcn->ln_name[0] == '\0') {
    247        1.7  christos 		tty_warn(1, "Cpio link name is corrupt");
    248       1.19       dsl 		return -1;
    249        1.1       jtc 	}
    250       1.19       dsl 	return 0;
    251        1.1       jtc }
    252        1.1       jtc 
    253        1.1       jtc /*
    254        1.1       jtc  * Routines common to the extended byte oriented cpio format
    255        1.1       jtc  */
    256        1.1       jtc 
    257        1.1       jtc /*
    258        1.1       jtc  * cpio_id()
    259        1.9     itohy  *	determine if a block given to us is a valid extended byte oriented
    260        1.1       jtc  *	cpio header
    261        1.1       jtc  * Return:
    262        1.9     itohy  *	0 if a valid header, -1 otherwise
    263        1.1       jtc  */
    264        1.1       jtc 
    265        1.1       jtc int
    266        1.1       jtc cpio_id(char *blk, int size)
    267        1.1       jtc {
    268       1.20     lukem 	if ((size < (int)sizeof(HD_CPIO)) ||
    269        1.1       jtc 	    (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
    270       1.19       dsl 		return -1;
    271       1.19       dsl 	return 0;
    272        1.1       jtc }
    273        1.1       jtc 
    274        1.1       jtc /*
    275        1.1       jtc  * cpio_rd()
    276        1.1       jtc  *	determine if a buffer is a byte oriented extended cpio archive entry.
    277        1.1       jtc  *	convert and store the values in the ARCHD parameter.
    278        1.1       jtc  * Return:
    279        1.1       jtc  *	0 if a valid header, -1 otherwise.
    280        1.1       jtc  */
    281        1.1       jtc 
    282        1.1       jtc int
    283        1.6       tls cpio_rd(ARCHD *arcn, char *buf)
    284        1.1       jtc {
    285        1.6       tls 	int nsz;
    286        1.6       tls 	HD_CPIO *hd;
    287        1.1       jtc 
    288        1.1       jtc 	/*
    289        1.1       jtc 	 * check that this is a valid header, if not return -1
    290        1.1       jtc 	 */
    291        1.1       jtc 	if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
    292       1.19       dsl 		return -1;
    293        1.1       jtc 	hd = (HD_CPIO *)buf;
    294        1.1       jtc 
    295        1.1       jtc 	/*
    296        1.1       jtc 	 * byte oriented cpio (posix) does not have padding! extract the octal
    297        1.1       jtc 	 * ascii fields from the header
    298        1.1       jtc 	 */
    299        1.1       jtc 	arcn->pad = 0L;
    300       1.22  christos 	arcn->sb.st_dev = (dev_t)asc_u32(hd->c_dev, sizeof(hd->c_dev), OCT);
    301       1.22  christos 	arcn->sb.st_ino = (ino_t)asc_u32(hd->c_ino, sizeof(hd->c_ino), OCT);
    302       1.22  christos 	arcn->sb.st_mode = (mode_t)asc_u32(hd->c_mode, sizeof(hd->c_mode), OCT);
    303       1.22  christos 	arcn->sb.st_uid = (uid_t)asc_u32(hd->c_uid, sizeof(hd->c_uid), OCT);
    304       1.22  christos 	arcn->sb.st_gid = (gid_t)asc_u32(hd->c_gid, sizeof(hd->c_gid), OCT);
    305       1.22  christos 	arcn->sb.st_nlink = (nlink_t)asc_u32(hd->c_nlink, sizeof(hd->c_nlink),
    306        1.1       jtc 	    OCT);
    307       1.22  christos 	arcn->sb.st_rdev = (dev_t)asc_u32(hd->c_rdev, sizeof(hd->c_rdev), OCT);
    308       1.22  christos 	arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->c_mtime, sizeof(hd->c_mtime),
    309        1.1       jtc 	    OCT);
    310        1.1       jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    311       1.11     lukem 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->c_filesize,
    312       1.11     lukem 	    sizeof(hd->c_filesize), OCT);
    313        1.1       jtc 
    314        1.1       jtc 	/*
    315        1.1       jtc 	 * check name size and if valid, read in the name of this entry (name
    316        1.1       jtc 	 * follows header in the archive)
    317        1.1       jtc 	 */
    318       1.22  christos 	if ((nsz = (int)asc_u32(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
    319       1.19       dsl 		return -1;
    320        1.1       jtc 	arcn->nlen = nsz - 1;
    321        1.1       jtc 	if (rd_nm(arcn, nsz) < 0)
    322       1.19       dsl 		return -1;
    323        1.1       jtc 
    324        1.1       jtc 	if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
    325        1.1       jtc 		/*
    326        1.9     itohy 		 * no link name to read for this file
    327        1.9     itohy 		 */
    328        1.1       jtc 		arcn->ln_nlen = 0;
    329        1.1       jtc 		arcn->ln_name[0] = '\0';
    330       1.19       dsl 		return com_rd(arcn);
    331        1.1       jtc 	}
    332        1.1       jtc 
    333        1.1       jtc 	/*
    334        1.1       jtc 	 * check link name size and read in the link name. Link names are
    335        1.1       jtc 	 * stored like file data.
    336        1.1       jtc 	 */
    337        1.1       jtc 	if (rd_ln_nm(arcn) < 0)
    338       1.19       dsl 		return -1;
    339        1.1       jtc 
    340        1.1       jtc 	/*
    341        1.1       jtc 	 * we have a valid header (with a link)
    342        1.1       jtc 	 */
    343       1.19       dsl 	return com_rd(arcn);
    344        1.1       jtc }
    345        1.1       jtc 
    346        1.1       jtc /*
    347        1.1       jtc  * cpio_endrd()
    348        1.9     itohy  *	no cleanup needed here, just return size of the trailer (for append)
    349        1.1       jtc  * Return:
    350        1.9     itohy  *	size of trailer header in this format
    351        1.1       jtc  */
    352        1.1       jtc 
    353        1.1       jtc off_t
    354        1.1       jtc cpio_endrd(void)
    355        1.1       jtc {
    356       1.19       dsl 	return (off_t)(sizeof(HD_CPIO) + sizeof(TRAILER));
    357        1.1       jtc }
    358        1.1       jtc 
    359        1.1       jtc /*
    360        1.1       jtc  * cpio_stwr()
    361        1.1       jtc  *	start up the device mapping table
    362        1.1       jtc  * Return:
    363        1.1       jtc  *	0 if ok, -1 otherwise (what dev_start() returns)
    364        1.1       jtc  */
    365        1.1       jtc 
    366        1.1       jtc int
    367        1.1       jtc cpio_stwr(void)
    368        1.1       jtc {
    369       1.19       dsl 	return dev_start();
    370        1.1       jtc }
    371        1.1       jtc 
    372        1.1       jtc /*
    373        1.1       jtc  * cpio_wr()
    374        1.1       jtc  *	copy the data in the ARCHD to buffer in extended byte oriented cpio
    375        1.1       jtc  *	format.
    376        1.1       jtc  * Return
    377        1.9     itohy  *	0 if file has data to be written after the header, 1 if file has NO
    378        1.1       jtc  *	data to write after the header, -1 if archive write failed
    379        1.1       jtc  */
    380        1.1       jtc 
    381        1.1       jtc int
    382        1.6       tls cpio_wr(ARCHD *arcn)
    383        1.1       jtc {
    384        1.6       tls 	HD_CPIO *hd;
    385        1.6       tls 	int nsz;
    386        1.1       jtc 	char hdblk[sizeof(HD_CPIO)];
    387        1.1       jtc 
    388        1.1       jtc 	/*
    389        1.1       jtc 	 * check and repair truncated device and inode fields in the header
    390        1.1       jtc 	 */
    391        1.1       jtc 	if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
    392       1.19       dsl 		return -1;
    393        1.1       jtc 
    394        1.1       jtc 	arcn->pad = 0L;
    395        1.1       jtc 	nsz = arcn->nlen + 1;
    396        1.1       jtc 	hd = (HD_CPIO *)hdblk;
    397        1.1       jtc 	if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
    398        1.1       jtc 		arcn->sb.st_rdev = 0;
    399        1.1       jtc 
    400        1.1       jtc 	switch(arcn->type) {
    401        1.1       jtc 	case PAX_CTG:
    402        1.1       jtc 	case PAX_REG:
    403        1.1       jtc 	case PAX_HRG:
    404        1.1       jtc 		/*
    405        1.1       jtc 		 * set data size for file data
    406        1.1       jtc 		 */
    407       1.11     lukem 		if (OFFT_ASC(arcn->sb.st_size, hd->c_filesize,
    408        1.1       jtc 		    sizeof(hd->c_filesize), OCT)) {
    409        1.7  christos 			tty_warn(1,"File is too large for cpio format %s",
    410        1.1       jtc 			    arcn->org_name);
    411       1.19       dsl 			return 1;
    412        1.1       jtc 		}
    413        1.1       jtc 		break;
    414        1.1       jtc 	case PAX_SLK:
    415        1.1       jtc 		/*
    416        1.1       jtc 		 * set data size to hold link name
    417        1.1       jtc 		 */
    418       1.22  christos 		if (u32_asc((uintmax_t)arcn->ln_nlen, hd->c_filesize,
    419        1.1       jtc 		    sizeof(hd->c_filesize), OCT))
    420        1.1       jtc 			goto out;
    421        1.1       jtc 		break;
    422        1.1       jtc 	default:
    423        1.1       jtc 		/*
    424        1.1       jtc 		 * all other file types have no file data
    425        1.1       jtc 		 */
    426       1.22  christos 		if (u32_asc((uintmax_t)0, hd->c_filesize, sizeof(hd->c_filesize),
    427        1.1       jtc 		     OCT))
    428        1.1       jtc 			goto out;
    429        1.1       jtc 		break;
    430        1.1       jtc 	}
    431        1.1       jtc 
    432        1.1       jtc 	/*
    433        1.1       jtc 	 * copy the values to the header using octal ascii
    434        1.1       jtc 	 */
    435       1.22  christos 	if (u32_asc((uintmax_t)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
    436       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
    437        1.9     itohy 		OCT) ||
    438       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
    439        1.1       jtc 		OCT) ||
    440       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
    441        1.1       jtc 		OCT) ||
    442       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
    443        1.1       jtc 		OCT) ||
    444       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
    445        1.1       jtc 		OCT) ||
    446       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
    447        1.1       jtc 		 OCT) ||
    448       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
    449        1.1       jtc 		OCT) ||
    450       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
    451        1.1       jtc 		OCT) ||
    452       1.22  christos 	    u32_asc((uintmax_t)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
    453        1.1       jtc 		goto out;
    454        1.1       jtc 
    455        1.1       jtc 	/*
    456        1.1       jtc 	 * write the file name to the archive
    457        1.1       jtc 	 */
    458        1.1       jtc 	if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
    459        1.1       jtc 	    (wr_rdbuf(arcn->name, nsz) < 0)) {
    460        1.7  christos 		tty_warn(1, "Unable to write cpio header for %s",
    461        1.7  christos 		    arcn->org_name);
    462       1.19       dsl 		return -1;
    463        1.1       jtc 	}
    464        1.1       jtc 
    465        1.1       jtc 	/*
    466        1.1       jtc 	 * if this file has data, we are done. The caller will write the file
    467        1.1       jtc 	 * data, if we are link tell caller we are done, go to next file
    468        1.1       jtc 	 */
    469       1.18      matt 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
    470       1.19       dsl 		return 0;
    471        1.1       jtc 	if (arcn->type != PAX_SLK)
    472       1.19       dsl 		return 1;
    473        1.1       jtc 
    474        1.1       jtc 	/*
    475        1.1       jtc 	 * write the link name to the archive, tell the caller to go to the
    476        1.1       jtc 	 * next file as we are done.
    477        1.1       jtc 	 */
    478        1.1       jtc 	if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
    479        1.7  christos 		tty_warn(1,"Unable to write cpio link name for %s",
    480        1.7  christos 		    arcn->org_name);
    481       1.19       dsl 		return -1;
    482        1.1       jtc 	}
    483       1.19       dsl 	return 1;
    484        1.1       jtc 
    485        1.1       jtc     out:
    486        1.1       jtc 	/*
    487        1.1       jtc 	 * header field is out of range
    488        1.1       jtc 	 */
    489        1.7  christos 	tty_warn(1, "Cpio header field is too small to store file %s",
    490        1.1       jtc 	    arcn->org_name);
    491       1.19       dsl 	return 1;
    492        1.1       jtc }
    493        1.1       jtc 
    494        1.1       jtc /*
    495        1.1       jtc  * Routines common to the system VR4 version of cpio (with/without file CRC)
    496        1.1       jtc  */
    497        1.1       jtc 
    498        1.1       jtc /*
    499        1.1       jtc  * vcpio_id()
    500        1.9     itohy  *	determine if a block given to us is a valid system VR4 cpio header
    501        1.9     itohy  *	WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
    502        1.1       jtc  *	uses HEX
    503        1.1       jtc  * Return:
    504        1.9     itohy  *	0 if a valid header, -1 otherwise
    505        1.1       jtc  */
    506        1.1       jtc 
    507        1.1       jtc int
    508        1.1       jtc vcpio_id(char *blk, int size)
    509        1.1       jtc {
    510       1.20     lukem 	if ((size < (int)sizeof(HD_VCPIO)) ||
    511        1.1       jtc 	    (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
    512       1.19       dsl 		return -1;
    513       1.19       dsl 	return 0;
    514        1.1       jtc }
    515        1.1       jtc 
    516        1.1       jtc /*
    517        1.1       jtc  * crc_id()
    518        1.9     itohy  *	determine if a block given to us is a valid system VR4 cpio header
    519        1.1       jtc  *	WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
    520        1.1       jtc  * Return:
    521        1.9     itohy  *	0 if a valid header, -1 otherwise
    522        1.1       jtc  */
    523        1.1       jtc 
    524        1.1       jtc int
    525        1.1       jtc crc_id(char *blk, int size)
    526        1.1       jtc {
    527       1.20     lukem 	if ((size < (int)sizeof(HD_VCPIO)) ||
    528        1.1       jtc 	    (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
    529       1.19       dsl 		return -1;
    530       1.19       dsl 	return 0;
    531        1.1       jtc }
    532        1.1       jtc 
    533        1.1       jtc /*
    534        1.1       jtc  * crc_strd()
    535        1.9     itohy  *	set file data CRC calculations. Fire up the hard link detection code
    536        1.1       jtc  * Return:
    537        1.9     itohy  *	0 if ok -1 otherwise (the return values of lnk_start())
    538        1.1       jtc  */
    539        1.1       jtc 
    540        1.1       jtc int
    541        1.1       jtc crc_strd(void)
    542        1.1       jtc {
    543        1.1       jtc 	docrc = 1;
    544       1.19       dsl 	return lnk_start();
    545        1.1       jtc }
    546        1.1       jtc 
    547        1.1       jtc /*
    548        1.1       jtc  * vcpio_rd()
    549        1.1       jtc  *	determine if a buffer is a system VR4 archive entry. (with/without CRC)
    550        1.1       jtc  *	convert and store the values in the ARCHD parameter.
    551        1.1       jtc  * Return:
    552        1.1       jtc  *	0 if a valid header, -1 otherwise.
    553        1.1       jtc  */
    554        1.1       jtc 
    555        1.1       jtc int
    556        1.6       tls vcpio_rd(ARCHD *arcn, char *buf)
    557        1.1       jtc {
    558        1.6       tls 	HD_VCPIO *hd;
    559        1.1       jtc 	dev_t devminor;
    560        1.1       jtc 	dev_t devmajor;
    561        1.6       tls 	int nsz;
    562        1.1       jtc 
    563        1.1       jtc 	/*
    564        1.1       jtc 	 * during the id phase it was determined if we were using CRC, use the
    565        1.1       jtc 	 * proper id routine.
    566        1.1       jtc 	 */
    567        1.1       jtc 	if (docrc) {
    568        1.1       jtc 		if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
    569       1.19       dsl 			return -1;
    570        1.1       jtc 	} else {
    571        1.1       jtc 		if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
    572       1.19       dsl 			return -1;
    573        1.1       jtc 	}
    574        1.1       jtc 
    575        1.1       jtc 	hd = (HD_VCPIO *)buf;
    576        1.1       jtc 	arcn->pad = 0L;
    577        1.1       jtc 
    578        1.1       jtc 	/*
    579        1.1       jtc 	 * extract the hex ascii fields from the header
    580        1.1       jtc 	 */
    581       1.22  christos 	arcn->sb.st_ino = (ino_t)asc_u32(hd->c_ino, sizeof(hd->c_ino), HEX);
    582       1.22  christos 	arcn->sb.st_mode = (mode_t)asc_u32(hd->c_mode, sizeof(hd->c_mode), HEX);
    583       1.22  christos 	arcn->sb.st_uid = (uid_t)asc_u32(hd->c_uid, sizeof(hd->c_uid), HEX);
    584       1.22  christos 	arcn->sb.st_gid = (gid_t)asc_u32(hd->c_gid, sizeof(hd->c_gid), HEX);
    585       1.22  christos 	arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->c_mtime,sizeof(hd->c_mtime),HEX);
    586        1.1       jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    587       1.11     lukem 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->c_filesize,
    588        1.1       jtc 	    sizeof(hd->c_filesize), HEX);
    589       1.22  christos 	arcn->sb.st_nlink = (nlink_t)asc_u32(hd->c_nlink, sizeof(hd->c_nlink),
    590        1.1       jtc 	    HEX);
    591       1.22  christos 	devmajor = (dev_t)asc_u32(hd->c_maj, sizeof(hd->c_maj), HEX);
    592       1.22  christos 	devminor = (dev_t)asc_u32(hd->c_min, sizeof(hd->c_min), HEX);
    593        1.1       jtc 	arcn->sb.st_dev = TODEV(devmajor, devminor);
    594       1.22  christos 	devmajor = (dev_t)asc_u32(hd->c_rmaj, sizeof(hd->c_maj), HEX);
    595       1.22  christos 	devminor = (dev_t)asc_u32(hd->c_rmin, sizeof(hd->c_min), HEX);
    596        1.1       jtc 	arcn->sb.st_rdev = TODEV(devmajor, devminor);
    597       1.22  christos 	arcn->crc = asc_u32(hd->c_chksum, sizeof(hd->c_chksum), HEX);
    598        1.1       jtc 
    599        1.1       jtc 	/*
    600        1.1       jtc 	 * check the length of the file name, if ok read it in, return -1 if
    601        1.1       jtc 	 * bogus
    602        1.1       jtc 	 */
    603       1.22  christos 	if ((nsz = (int)asc_u32(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
    604       1.19       dsl 		return -1;
    605        1.1       jtc 	arcn->nlen = nsz - 1;
    606        1.1       jtc 	if (rd_nm(arcn, nsz) < 0)
    607       1.19       dsl 		return -1;
    608        1.1       jtc 
    609        1.1       jtc 	/*
    610        1.9     itohy 	 * skip padding. header + filename is aligned to 4 byte boundaries
    611        1.1       jtc 	 */
    612        1.1       jtc 	if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
    613       1.19       dsl 		return -1;
    614        1.1       jtc 
    615        1.1       jtc 	/*
    616        1.1       jtc 	 * if not a link (or a file with no data), calculate pad size (for
    617        1.1       jtc 	 * padding which follows the file data), clear the link name and return
    618        1.1       jtc 	 */
    619        1.1       jtc 	if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
    620        1.1       jtc 		/*
    621        1.1       jtc 		 * we have a valid header (not a link)
    622        1.1       jtc 		 */
    623        1.1       jtc 		arcn->ln_nlen = 0;
    624        1.1       jtc 		arcn->ln_name[0] = '\0';
    625        1.1       jtc 		arcn->pad = VCPIO_PAD(arcn->sb.st_size);
    626       1.19       dsl 		return com_rd(arcn);
    627        1.1       jtc 	}
    628        1.1       jtc 
    629        1.1       jtc 	/*
    630        1.1       jtc 	 * read in the link name and skip over the padding
    631        1.1       jtc 	 */
    632        1.1       jtc 	if ((rd_ln_nm(arcn) < 0) ||
    633        1.1       jtc 	    (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
    634       1.19       dsl 		return -1;
    635        1.1       jtc 
    636        1.1       jtc 	/*
    637        1.1       jtc 	 * we have a valid header (with a link)
    638        1.1       jtc 	 */
    639       1.19       dsl 	return com_rd(arcn);
    640        1.1       jtc }
    641        1.1       jtc 
    642        1.1       jtc /*
    643        1.1       jtc  * vcpio_endrd()
    644        1.9     itohy  *	no cleanup needed here, just return size of the trailer (for append)
    645        1.1       jtc  * Return:
    646        1.9     itohy  *	size of trailer header in this format
    647        1.1       jtc  */
    648        1.1       jtc 
    649        1.1       jtc off_t
    650        1.1       jtc vcpio_endrd(void)
    651        1.1       jtc {
    652       1.21    martin 	return (off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
    653       1.21    martin 		(VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER))));
    654        1.1       jtc }
    655        1.1       jtc 
    656        1.1       jtc /*
    657        1.1       jtc  * crc_stwr()
    658        1.1       jtc  *	start up the device mapping table, enable crc file calculation
    659        1.1       jtc  * Return:
    660        1.1       jtc  *	0 if ok, -1 otherwise (what dev_start() returns)
    661        1.1       jtc  */
    662        1.1       jtc 
    663        1.1       jtc int
    664        1.1       jtc crc_stwr(void)
    665        1.1       jtc {
    666        1.1       jtc 	docrc = 1;
    667       1.19       dsl 	return dev_start();
    668        1.1       jtc }
    669        1.1       jtc 
    670        1.1       jtc /*
    671        1.1       jtc  * vcpio_wr()
    672        1.1       jtc  *	copy the data in the ARCHD to buffer in system VR4 cpio
    673        1.1       jtc  *	(with/without crc) format.
    674        1.1       jtc  * Return
    675        1.1       jtc  *	0 if file has data to be written after the header, 1 if file has
    676        1.1       jtc  *	NO data to write after the header, -1 if archive write failed
    677        1.1       jtc  */
    678        1.1       jtc 
    679        1.1       jtc int
    680        1.6       tls vcpio_wr(ARCHD *arcn)
    681        1.1       jtc {
    682        1.6       tls 	HD_VCPIO *hd;
    683        1.1       jtc 	unsigned int nsz;
    684        1.1       jtc 	char hdblk[sizeof(HD_VCPIO)];
    685        1.1       jtc 
    686        1.1       jtc 	/*
    687        1.1       jtc 	 * check and repair truncated device and inode fields in the cpio
    688        1.1       jtc 	 * header
    689        1.1       jtc 	 */
    690        1.1       jtc 	if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
    691       1.19       dsl 		return -1;
    692        1.1       jtc 	nsz = arcn->nlen + 1;
    693        1.1       jtc 	hd = (HD_VCPIO *)hdblk;
    694        1.1       jtc 	if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
    695        1.1       jtc 		arcn->sb.st_rdev = 0;
    696        1.1       jtc 
    697        1.1       jtc 	/*
    698        1.1       jtc 	 * add the proper magic value depending whether we were asked for
    699        1.1       jtc 	 * file data crc's, and the crc if needed.
    700        1.1       jtc 	 */
    701        1.1       jtc 	if (docrc) {
    702       1.22  christos 		if (u32_asc((uintmax_t)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
    703        1.9     itohy 			OCT) ||
    704       1.22  christos 		    u32_asc((uintmax_t)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
    705        1.9     itohy 			HEX))
    706        1.1       jtc 			goto out;
    707        1.1       jtc 	} else {
    708       1.22  christos 		if (u32_asc((uintmax_t)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
    709        1.9     itohy 			OCT) ||
    710       1.22  christos 		    u32_asc((uintmax_t)0, hd->c_chksum, sizeof(hd->c_chksum),HEX))
    711        1.1       jtc 			goto out;
    712        1.1       jtc 	}
    713        1.1       jtc 
    714        1.1       jtc 	switch(arcn->type) {
    715        1.1       jtc 	case PAX_CTG:
    716        1.1       jtc 	case PAX_REG:
    717        1.1       jtc 	case PAX_HRG:
    718        1.1       jtc 		/*
    719        1.1       jtc 		 * caller will copy file data to the archive. tell him how
    720        1.1       jtc 		 * much to pad.
    721        1.1       jtc 		 */
    722        1.1       jtc 		arcn->pad = VCPIO_PAD(arcn->sb.st_size);
    723       1.11     lukem 		if (OFFT_ASC(arcn->sb.st_size, hd->c_filesize,
    724        1.1       jtc 		    sizeof(hd->c_filesize), HEX)) {
    725        1.7  christos 			tty_warn(1,"File is too large for sv4cpio format %s",
    726        1.1       jtc 			    arcn->org_name);
    727       1.19       dsl 			return 1;
    728        1.1       jtc 		}
    729        1.1       jtc 		break;
    730        1.1       jtc 	case PAX_SLK:
    731        1.1       jtc 		/*
    732        1.1       jtc 		 * no file data for the caller to process, the file data has
    733        1.1       jtc 		 * the size of the link
    734        1.1       jtc 		 */
    735        1.1       jtc 		arcn->pad = 0L;
    736       1.22  christos 		if (u32_asc((uintmax_t)arcn->ln_nlen, hd->c_filesize,
    737        1.1       jtc 		    sizeof(hd->c_filesize), HEX))
    738        1.1       jtc 			goto out;
    739        1.1       jtc 		break;
    740        1.1       jtc 	default:
    741        1.1       jtc 		/*
    742        1.1       jtc 		 * no file data for the caller to process
    743        1.1       jtc 		 */
    744        1.1       jtc 		arcn->pad = 0L;
    745       1.22  christos 		if (u32_asc((uintmax_t)0, hd->c_filesize, sizeof(hd->c_filesize),
    746        1.1       jtc 		    HEX))
    747        1.1       jtc 			goto out;
    748        1.1       jtc 		break;
    749        1.1       jtc 	}
    750        1.1       jtc 
    751        1.1       jtc 	/*
    752        1.1       jtc 	 * set the other fields in the header
    753        1.1       jtc 	 */
    754       1.22  christos 	if (u32_asc((uintmax_t)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
    755        1.1       jtc 		HEX) ||
    756       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
    757        1.1       jtc 		HEX) ||
    758       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
    759        1.1       jtc 		HEX) ||
    760       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
    761        1.9     itohy 		HEX) ||
    762       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
    763        1.9     itohy 		HEX) ||
    764       1.22  christos 	    u32_asc((uintmax_t)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
    765        1.9     itohy 		HEX) ||
    766       1.22  christos 	    u32_asc((uintmax_t)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
    767        1.1       jtc 		HEX) ||
    768       1.22  christos 	    u32_asc((uintmax_t)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
    769        1.1       jtc 		HEX) ||
    770       1.22  christos 	    u32_asc((uintmax_t)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
    771        1.1       jtc 		HEX) ||
    772       1.22  christos 	    u32_asc((uintmax_t)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
    773        1.1       jtc 		HEX) ||
    774       1.22  christos 	    u32_asc((uintmax_t)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
    775        1.1       jtc 		goto out;
    776        1.1       jtc 
    777        1.1       jtc 	/*
    778        1.1       jtc 	 * write the header, the file name and padding as required.
    779        1.1       jtc 	 */
    780        1.1       jtc 	if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
    781        1.1       jtc 	    (wr_rdbuf(arcn->name, (int)nsz) < 0)  ||
    782        1.1       jtc 	    (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
    783        1.7  christos 		tty_warn(1,"Could not write sv4cpio header for %s",
    784        1.7  christos 		    arcn->org_name);
    785       1.19       dsl 		return -1;
    786        1.1       jtc 	}
    787        1.1       jtc 
    788        1.1       jtc 	/*
    789        1.1       jtc 	 * if we have file data, tell the caller we are done, copy the file
    790        1.1       jtc 	 */
    791        1.1       jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
    792        1.1       jtc 	    (arcn->type == PAX_HRG))
    793       1.19       dsl 		return 0;
    794        1.1       jtc 
    795        1.1       jtc 	/*
    796        1.1       jtc 	 * if we are not a link, tell the caller we are done, go to next file
    797        1.1       jtc 	 */
    798        1.1       jtc 	if (arcn->type != PAX_SLK)
    799       1.19       dsl 		return 1;
    800        1.1       jtc 
    801        1.1       jtc 	/*
    802        1.1       jtc 	 * write the link name, tell the caller we are done.
    803        1.1       jtc 	 */
    804        1.1       jtc 	if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
    805        1.1       jtc 	    (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
    806        1.7  christos 		tty_warn(1,"Could not write sv4cpio link name for %s",
    807        1.1       jtc 		    arcn->org_name);
    808       1.19       dsl 		return -1;
    809        1.1       jtc 	}
    810       1.19       dsl 	return 1;
    811        1.1       jtc 
    812        1.1       jtc     out:
    813        1.1       jtc 	/*
    814        1.1       jtc 	 * header field is out of range
    815        1.1       jtc 	 */
    816        1.7  christos 	tty_warn(1,"Sv4cpio header field is too small for file %s",
    817        1.7  christos 	    arcn->org_name);
    818       1.19       dsl 	return 1;
    819        1.1       jtc }
    820        1.1       jtc 
    821        1.1       jtc /*
    822        1.1       jtc  * Routines common to the old binary header cpio
    823        1.1       jtc  */
    824        1.1       jtc 
    825        1.1       jtc /*
    826        1.1       jtc  * bcpio_id()
    827        1.9     itohy  *	determine if a block given to us is a old binary cpio header
    828        1.1       jtc  *	(with/without header byte swapping)
    829        1.1       jtc  * Return:
    830        1.9     itohy  *	0 if a valid header, -1 otherwise
    831        1.1       jtc  */
    832        1.1       jtc 
    833        1.1       jtc int
    834        1.1       jtc bcpio_id(char *blk, int size)
    835        1.1       jtc {
    836       1.20     lukem 	if (size < (int)sizeof(HD_BCPIO))
    837       1.19       dsl 		return -1;
    838        1.1       jtc 
    839        1.1       jtc 	/*
    840        1.1       jtc 	 * check both normal and byte swapped magic cookies
    841        1.1       jtc 	 */
    842        1.1       jtc 	if (((u_short)SHRT_EXT(blk)) == MAGIC)
    843       1.19       dsl 		return 0;
    844        1.1       jtc 	if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
    845        1.8       mrg 		if (!cpio_swp_head)
    846        1.8       mrg 			++cpio_swp_head;
    847       1.19       dsl 		return 0;
    848        1.1       jtc 	}
    849       1.19       dsl 	return -1;
    850        1.1       jtc }
    851        1.1       jtc 
    852        1.1       jtc /*
    853        1.1       jtc  * bcpio_rd()
    854        1.1       jtc  *	determine if a buffer is a old binary archive entry. (it may have byte
    855        1.1       jtc  *	swapped header) convert and store the values in the ARCHD parameter.
    856        1.1       jtc  *	This is a very old header format and should not really be used.
    857        1.1       jtc  * Return:
    858        1.1       jtc  *	0 if a valid header, -1 otherwise.
    859        1.1       jtc  */
    860        1.1       jtc 
    861        1.1       jtc int
    862        1.6       tls bcpio_rd(ARCHD *arcn, char *buf)
    863        1.1       jtc {
    864        1.6       tls 	HD_BCPIO *hd;
    865        1.6       tls 	int nsz;
    866        1.1       jtc 
    867        1.1       jtc 	/*
    868        1.1       jtc 	 * check the header
    869        1.1       jtc 	 */
    870        1.1       jtc 	if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
    871       1.19       dsl 		return -1;
    872        1.1       jtc 
    873        1.1       jtc 	arcn->pad = 0L;
    874        1.1       jtc 	hd = (HD_BCPIO *)buf;
    875        1.8       mrg 	if (cpio_swp_head) {
    876        1.1       jtc 		/*
    877        1.9     itohy 		 * header has swapped bytes on 16 bit boundaries
    878        1.1       jtc 		 */
    879        1.1       jtc 		arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
    880        1.1       jtc 		arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
    881        1.1       jtc 		arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
    882        1.1       jtc 		arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
    883        1.1       jtc 		arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
    884        1.1       jtc 		arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
    885        1.1       jtc 		arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
    886        1.1       jtc 		arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
    887        1.1       jtc 		arcn->sb.st_mtime =  (arcn->sb.st_mtime << 16) |
    888        1.1       jtc 			((time_t)(RSHRT_EXT(hd->h_mtime_2)));
    889        1.1       jtc 		arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
    890        1.1       jtc 		arcn->sb.st_size = (arcn->sb.st_size << 16) |
    891        1.1       jtc 			((off_t)(RSHRT_EXT(hd->h_filesize_2)));
    892        1.1       jtc 		nsz = (int)(RSHRT_EXT(hd->h_namesize));
    893        1.1       jtc 	} else {
    894        1.1       jtc 		arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
    895        1.1       jtc 		arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
    896        1.1       jtc 		arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
    897        1.1       jtc 		arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
    898        1.1       jtc 		arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
    899        1.1       jtc 		arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
    900        1.1       jtc 		arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
    901        1.1       jtc 		arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
    902        1.1       jtc 		arcn->sb.st_mtime =  (arcn->sb.st_mtime << 16) |
    903        1.1       jtc 			((time_t)(SHRT_EXT(hd->h_mtime_2)));
    904        1.1       jtc 		arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
    905        1.1       jtc 		arcn->sb.st_size = (arcn->sb.st_size << 16) |
    906        1.1       jtc 			((off_t)(SHRT_EXT(hd->h_filesize_2)));
    907        1.1       jtc 		nsz = (int)(SHRT_EXT(hd->h_namesize));
    908        1.1       jtc 	}
    909        1.1       jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    910        1.1       jtc 
    911        1.1       jtc 	/*
    912        1.1       jtc 	 * check the file name size, if bogus give up. otherwise read the file
    913        1.1       jtc 	 * name
    914        1.1       jtc 	 */
    915        1.1       jtc 	if (nsz < 2)
    916       1.19       dsl 		return -1;
    917        1.1       jtc 	arcn->nlen = nsz - 1;
    918        1.1       jtc 	if (rd_nm(arcn, nsz) < 0)
    919       1.19       dsl 		return -1;
    920        1.1       jtc 
    921        1.1       jtc 	/*
    922        1.9     itohy 	 * header + file name are aligned to 2 byte boundaries, skip if needed
    923        1.1       jtc 	 */
    924        1.1       jtc 	if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
    925       1.19       dsl 		return -1;
    926        1.1       jtc 
    927        1.1       jtc 	/*
    928        1.1       jtc 	 * if not a link (or a file with no data), calculate pad size (for
    929        1.1       jtc 	 * padding which follows the file data), clear the link name and return
    930        1.1       jtc 	 */
    931        1.1       jtc 	if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
    932        1.1       jtc 		/*
    933        1.1       jtc 		 * we have a valid header (not a link)
    934        1.1       jtc 		 */
    935        1.1       jtc 		arcn->ln_nlen = 0;
    936        1.1       jtc 		arcn->ln_name[0] = '\0';
    937        1.1       jtc 		arcn->pad = BCPIO_PAD(arcn->sb.st_size);
    938       1.19       dsl 		return com_rd(arcn);
    939        1.1       jtc 	}
    940        1.1       jtc 
    941        1.1       jtc 	if ((rd_ln_nm(arcn) < 0) ||
    942        1.1       jtc 	    (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
    943       1.19       dsl 		return -1;
    944        1.1       jtc 
    945        1.1       jtc 	/*
    946        1.1       jtc 	 * we have a valid header (with a link)
    947        1.1       jtc 	 */
    948       1.19       dsl 	return com_rd(arcn);
    949        1.1       jtc }
    950        1.1       jtc 
    951        1.1       jtc /*
    952        1.1       jtc  * bcpio_endrd()
    953        1.9     itohy  *	no cleanup needed here, just return size of the trailer (for append)
    954        1.1       jtc  * Return:
    955        1.9     itohy  *	size of trailer header in this format
    956        1.1       jtc  */
    957        1.1       jtc 
    958        1.1       jtc off_t
    959        1.1       jtc bcpio_endrd(void)
    960        1.1       jtc {
    961       1.21    martin 	return (off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
    962       1.21    martin 		(BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER))));
    963        1.1       jtc }
    964        1.1       jtc 
    965        1.1       jtc /*
    966        1.1       jtc  * bcpio_wr()
    967        1.1       jtc  *	copy the data in the ARCHD to buffer in old binary cpio format
    968        1.1       jtc  *	There is a real chance of field overflow with this critter. So we
    969       1.14       wiz  *	always check the conversion is ok. nobody in their right mind
    970       1.13       wiz  *	should write an archive in this format...
    971        1.1       jtc  * Return
    972        1.9     itohy  *	0 if file has data to be written after the header, 1 if file has NO
    973        1.1       jtc  *	data to write after the header, -1 if archive write failed
    974        1.1       jtc  */
    975        1.1       jtc 
    976        1.1       jtc int
    977        1.6       tls bcpio_wr(ARCHD *arcn)
    978        1.1       jtc {
    979        1.6       tls 	HD_BCPIO *hd;
    980        1.6       tls 	int nsz;
    981        1.1       jtc 	char hdblk[sizeof(HD_BCPIO)];
    982        1.1       jtc 	off_t t_offt;
    983        1.1       jtc 	int t_int;
    984        1.1       jtc 	time_t t_timet;
    985        1.1       jtc 
    986        1.1       jtc 	/*
    987        1.1       jtc 	 * check and repair truncated device and inode fields in the cpio
    988        1.1       jtc 	 * header
    989        1.1       jtc 	 */
    990        1.1       jtc 	if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
    991       1.19       dsl 		return -1;
    992        1.1       jtc 
    993        1.1       jtc 	if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
    994        1.1       jtc 		arcn->sb.st_rdev = 0;
    995        1.1       jtc 	hd = (HD_BCPIO *)hdblk;
    996        1.1       jtc 
    997        1.1       jtc 	switch(arcn->type) {
    998        1.1       jtc 	case PAX_CTG:
    999        1.1       jtc 	case PAX_REG:
   1000        1.1       jtc 	case PAX_HRG:
   1001        1.1       jtc 		/*
   1002        1.1       jtc 		 * caller will copy file data to the archive. tell him how
   1003        1.1       jtc 		 * much to pad.
   1004        1.1       jtc 		 */
   1005        1.1       jtc 		arcn->pad = BCPIO_PAD(arcn->sb.st_size);
   1006        1.1       jtc 		hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
   1007        1.1       jtc 		hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
   1008        1.1       jtc 		hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
   1009        1.1       jtc 		hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
   1010        1.1       jtc 		t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
   1011        1.1       jtc 		t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
   1012        1.1       jtc 		if (arcn->sb.st_size != t_offt) {
   1013        1.7  christos 			tty_warn(1,"File is too large for bcpio format %s",
   1014        1.1       jtc 			    arcn->org_name);
   1015       1.19       dsl 			return 1;
   1016        1.1       jtc 		}
   1017        1.1       jtc 		break;
   1018        1.1       jtc 	case PAX_SLK:
   1019        1.1       jtc 		/*
   1020        1.1       jtc 		 * no file data for the caller to process, the file data has
   1021        1.1       jtc 		 * the size of the link
   1022        1.1       jtc 		 */
   1023        1.1       jtc 		arcn->pad = 0L;
   1024        1.1       jtc 		hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
   1025        1.1       jtc 		hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
   1026        1.1       jtc 		hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
   1027        1.1       jtc 		hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
   1028        1.1       jtc 		t_int = (int)(SHRT_EXT(hd->h_filesize_1));
   1029        1.1       jtc 		t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
   1030        1.1       jtc 		if (arcn->ln_nlen != t_int)
   1031        1.1       jtc 			goto out;
   1032        1.1       jtc 		break;
   1033        1.1       jtc 	default:
   1034        1.1       jtc 		/*
   1035        1.1       jtc 		 * no file data for the caller to process
   1036        1.1       jtc 		 */
   1037        1.1       jtc 		arcn->pad = 0L;
   1038        1.1       jtc 		hd->h_filesize_1[0] = (char)0;
   1039        1.1       jtc 		hd->h_filesize_1[1] = (char)0;
   1040        1.1       jtc 		hd->h_filesize_2[0] = (char)0;
   1041        1.1       jtc 		hd->h_filesize_2[1] = (char)0;
   1042        1.1       jtc 		break;
   1043        1.1       jtc 	}
   1044        1.1       jtc 
   1045        1.1       jtc 	/*
   1046        1.1       jtc 	 * build up the rest of the fields
   1047        1.1       jtc 	 */
   1048        1.1       jtc 	hd->h_magic[0] = CHR_WR_2(MAGIC);
   1049        1.1       jtc 	hd->h_magic[1] = CHR_WR_3(MAGIC);
   1050        1.1       jtc 	hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
   1051        1.1       jtc 	hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
   1052        1.1       jtc 	if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
   1053        1.1       jtc 		goto out;
   1054        1.1       jtc 	hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
   1055        1.1       jtc 	hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
   1056        1.1       jtc 	if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
   1057        1.1       jtc 		goto out;
   1058        1.1       jtc 	hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
   1059        1.1       jtc 	hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
   1060        1.1       jtc 	if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
   1061        1.1       jtc 		goto out;
   1062        1.1       jtc 	hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
   1063        1.1       jtc 	hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
   1064        1.1       jtc 	if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
   1065        1.1       jtc 		goto out;
   1066        1.1       jtc 	hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
   1067        1.1       jtc 	hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
   1068        1.1       jtc 	if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
   1069        1.1       jtc 		goto out;
   1070        1.1       jtc 	hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
   1071        1.1       jtc 	hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
   1072        1.1       jtc 	if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
   1073        1.1       jtc 		goto out;
   1074        1.1       jtc 	hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
   1075        1.1       jtc 	hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
   1076        1.1       jtc 	if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
   1077        1.1       jtc 		goto out;
   1078        1.1       jtc 	hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
   1079        1.1       jtc 	hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
   1080        1.1       jtc 	hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
   1081        1.1       jtc 	hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
   1082        1.1       jtc 	t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
   1083        1.1       jtc 	t_timet =  (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
   1084        1.1       jtc 	if (arcn->sb.st_mtime != t_timet)
   1085        1.1       jtc 		goto out;
   1086        1.1       jtc 	nsz = arcn->nlen + 1;
   1087        1.1       jtc 	hd->h_namesize[0] = CHR_WR_2(nsz);
   1088        1.1       jtc 	hd->h_namesize[1] = CHR_WR_3(nsz);
   1089        1.1       jtc 	if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
   1090        1.1       jtc 		goto out;
   1091        1.1       jtc 
   1092        1.1       jtc 	/*
   1093        1.1       jtc 	 * write the header, the file name and padding as required.
   1094        1.1       jtc 	 */
   1095        1.1       jtc 	if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
   1096        1.1       jtc 	    (wr_rdbuf(arcn->name, nsz) < 0) ||
   1097        1.1       jtc 	    (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
   1098        1.7  christos 		tty_warn(1, "Could not write bcpio header for %s",
   1099        1.7  christos 		    arcn->org_name);
   1100       1.19       dsl 		return -1;
   1101        1.1       jtc 	}
   1102        1.1       jtc 
   1103        1.1       jtc 	/*
   1104        1.1       jtc 	 * if we have file data, tell the caller we are done
   1105        1.1       jtc 	 */
   1106        1.1       jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
   1107        1.1       jtc 	    (arcn->type == PAX_HRG))
   1108       1.19       dsl 		return 0;
   1109        1.1       jtc 
   1110        1.1       jtc 	/*
   1111        1.1       jtc 	 * if we are not a link, tell the caller we are done, go to next file
   1112        1.1       jtc 	 */
   1113        1.1       jtc 	if (arcn->type != PAX_SLK)
   1114       1.19       dsl 		return 1;
   1115        1.1       jtc 
   1116        1.1       jtc 	/*
   1117        1.1       jtc 	 * write the link name, tell the caller we are done.
   1118        1.1       jtc 	 */
   1119        1.1       jtc 	if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
   1120        1.1       jtc 	    (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
   1121        1.7  christos 		tty_warn(1,"Could not write bcpio link name for %s",
   1122        1.7  christos 		    arcn->org_name);
   1123       1.19       dsl 		return -1;
   1124        1.1       jtc 	}
   1125       1.19       dsl 	return 1;
   1126        1.1       jtc 
   1127        1.1       jtc     out:
   1128        1.1       jtc 	/*
   1129        1.1       jtc 	 * header field is out of range
   1130        1.1       jtc 	 */
   1131        1.7  christos 	tty_warn(1,"Bcpio header field is too small for file %s",
   1132        1.7  christos 	    arcn->org_name);
   1133       1.19       dsl 	return 1;
   1134        1.1       jtc }
   1135