/* (C) Tim Graves 20th April 1994
   This code is supplied AS IS. no warrantee either expressed or implied 
   is provided. This code may be freeley modified and modified as long as my
   origional authorship is acknowledged. 
   
   Tim Graves
    Sun Microsystems
     
     */
/* this is a set of routines for doing very simple checksums on the sun
 side of things. other unix specifie routines may endup bein installed here */
#include <stdio.h>
#include <fcntl.h>
#include "psion.h" 
static char vsn[] = "@(#) sunutils.c 2.3@(#)" ;
long crc = 0 ;
long sunsum (fname)
char * fname ;
{
	int fileid ;
	char buffer[1024];
	int rlen ;
	
	if ((fileid = open (fname, O_RDONLY, 0)) == -1)
	{
		return (BADPARAM) ;
	}
	crc = 0 ;
	while((rlen= read(fileid, &buffer, 1024)) > 0)
	   docrc(buffer, rlen) ;
	
	close(fileid) ;
	return (crc) ;
}
/*
 Calculate the 16-bit CRC of a null-terminated string using a byte-oriented
 tableless algorithm invented by Andy Lowry (Columbia University).  The
 magic number 010201 is derived from the CRC-CCITT polynomial x^16+x^12+x^5+1.
 Note - this function could be adapted for strings containing imbedded 0's
 by including a length argument.
*/
docrc(s, len)
char *s; 
int len ;
{
    unsigned int c, q;

    while ((c = *s++) != '\0') {
        c &= 0177;
        q = (crc ^ c) & 017;            /* Low-order nibble */
        crc = (crc >> 4) ^ (q * 010201);
        q = (crc ^ (c >> 4)) & 017;     /* High order nibble */
        crc = (crc >> 4) ^ (q * 010201);
    }
}
