[net.sources.mac] 2 in 1!

mark@hyper.UUCP (Mark Mendel) (04/25/86)

	[What a deal!]

Here is a CRC program that will give the same result when run on unix
on a .data or .rsrc file as when run on the Mac.  On the Mac, under Aztec
C shell, "crc file" crc's the data fork and "crc file fersher" crc's the
resource fork.

This is also a request: I've gotten 2 bad Cap'n Magnetos so far! The first
had a checksum error xbin-ing.  The second xbinned OK but crashes.  However,
a checksum is not terribly good on such a large program; a double-bit error
could easily occur.  The CRC on my resource fork is F879.  Could somebody
mail me a version that they've verified works, together with its CRC?

The following program represents many seconds of effort. If you like it, and arevery stupid, send me lots of money.

-------- cut here --------- beware of signature -----
/* a crc routine */

/* this causes main() to be defined */
#define	TEST

/* #define	MAC */

#ifndef MAC
#define	openrf	open
#endif

unsigned short
calccrc( icrc, icp, icnt )
    unsigned short icrc;
    unsigned char *icp;
    int unsigned icnt;
{
    register unsigned short crc = icrc;
    register unsigned char *cp = icp;
    register unsigned int cnt = icnt;
    register i;

    while( cnt-- ) {
	crc ^= ((int)*cp++) << 8;
	for( i = 8; --i >= 0; )
	    if( crc & 0x8000 )
		crc = (crc<<1) ^ 0x1021;
	    else
		crc <<= 1;
    }

    return( crc );
}

#ifdef TEST

#include <stdio.h>
#include <fcntl.h>

#define MAXBUF	4096

main( ac, av )
    int ac; char **av;
{
    int fd = 0;
    int nr;
    char buf[MAXBUF];
    unsigned short crc;

    if( ac > 1 )
	if( (fd = open( av[1], O_RDONLY )) < 0 ) {
	    perror( av[1] );
	    exit( -1 );
    }
    crc = 0;
    while( (nr = read( fd, buf, MAXBUF )) > 0 )
	crc = calccrc( crc, buf, nr );
    printf( "%04x\n", crc );
    if( nr != 0 )
	perror( "reading" );
}

#endif

-- 
-------------------------------------------
Mark G. Mendel, Network Systems Corporation
ihnp4!umn-cs!hyper!mark