*BSD News Article 3506


Return to BSD News archive

Path: sserve!manuel!munnari.oz.au!mips!mips!swrinde!cs.utexas.edu!zaphod.mps.ohio-state.edu!sample.eng.ohio-state.edu!purdue!mentor.cc.purdue.edu!noose.ecn.purdue.edu!neon.ecn.purdue.edu!tgt
From: tgt@neon.ecn.purdue.edu (Conan the Librarian)
Newsgroups: comp.unix.bsd
Subject: Reading 386bsd dist files (bin,src,etc)
Keywords: byte swapping problem
Message-ID: <1992Aug6.224348.16769@noose.ecn.purdue.edu>
Date: 6 Aug 92 22:43:48 GMT
Sender: news@noose.ecn.purdue.edu (USENET news)
Distribution: usa
Organization: Purdue University Engineering Computer Network
Lines: 94

Attempting to use cpio to extract the files in the various
386bsd dists, I ran into a byte swapping problem (running on both a Gould
and a SUN3).  CPIO generates the wonderfully-descript error message "Out of
phase -- get help" and then exits.  (Someday, I aspire to write code with
lots of abstruse error messages like this... :-)

Since I really want to look at the srcdist stuff off-line from my PC
(not enough memory yet!) but couldn't find any flag to massage cpio into
appropriate behavior on the Gould or the SUN (so much for archive portability)
I wrote the following program to do the necessary header "fix" (26 shorts in
each cpio header need to be byte-swapped).  I've tested it on srcdist on my
SUN3, and it appears to work properly (I am extracting the src files as I
write this).

-tom

================= snip snip =================================================

/******
 ** CPIOFIX - Program to do byte swapping on the shorts in a cpio header,
 **           converting from 386bsd (PC) format to SUN/Gould format and thus
 **	      enabling cpio to unarchive 386bsd dist files on these alternate
 **	      architectures.
 **
 **	      Copyright 1992 Tom Tobin, no rights reserved.
 ******/

#include <stdio.h>
#include <sys/file.h>

#define HSIZE 26			/* cpio header size (not including
					   filename at the end of the header) */
#define BBUFSIZE 256			/* file i/o buffer size */

main(argc, argv)
   int argc;
   char *argv[];
{
   int fdin, fdout, bcount, i, j, skip;
   char bytes[257], *bptr, btmp;

   if (argc == 3) {
      fdin = open(argv[1], O_RDONLY, 0755);
      fdout = open(argv[2], O_WRONLY|O_CREAT, 0744);
   }

   else {					/* run as a filter */
      fdin = 0;
      fdout = 1;
   }

   read(fdin, bytes, 1);
   j=0;
   skip=0;

   while ((bcount=read(fdin, &bytes[1], BBUFSIZE)) != 0) {
      for (i=skip, bptr=bytes+skip; i<bcount; i++, bptr++) {
         if (j != 0) {
	    if (j < (HSIZE)/2) {	/* swap bytes on all shorts in header */
	       btmp = *bptr;
	       *bptr = *(bptr+1);
	       *(bptr+1) = btmp;
	       i++;
	       j++;
	       bptr++;
            }
	    if (j == (HSIZE)/2)		/* clear header counter */
	       j=0;
	 }

         else if ((unsigned char) *bptr == 0xC7 && *(bptr+1) == 0x71) {
/*	    fprintf(stdout, "found a header\n");	/* header magic # */
	    btmp = *bptr;				/* swap bytes */
	    *bptr = *(bptr+1);
	    *(bptr+1) = btmp;
	    i++;
	    j++;
	    bptr++;
	 }
      }

      skip = (i==257? 1: 0);
      write(fdout, bytes, bcount==BBUFSIZE? bcount: bcount+1);
      bytes[0] = bytes[BBUFSIZE];
   }

   close(fdin);
   close(fdout);
}
--
Tom Tobin				UUCP:     pur-ee!tgt	
Dept. of ChE				INTERNET: tgt@ecn.purdue.edu
Purdue University			Ma Bell:  home (317) 463-0189
W. Lafayette, IN 47907				  office "   494-4052