/*static char *SCCSID = "@(#)csc.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)csc.c      13.3 90/01/25";*/
#include <os2.h>

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "pb_ifs.h"
#include "superb.h"
#include "frame.h"
#include "lw.h"
#include "fsdata.h"
#include "pr_CACHE.h"
#include "pr_DEBUG.h"
#include "pr_MISC.h"


#if defined (DEBUG)

/*
 ***	CSC - Check Sector Checksums
 *
 *	CSC checks the checksums produced by RSC
 *	We only check the sectors which aren't marked dirty
 *
 *	ENTRY	pbuf = buffer header address
 *
 *	RETURNS TRUE if checksums match
 *
 *      HISTORY
 *          28-Sep-1988 09:43:04 by Bill McJohn [t-billm] 
 *				xlated from 9/27/88 assembly code
 */

BOOL far
CSC (pbuf)
PBUFNODE pbuf;            /* buffer header address */
{

/* #if SPB != 4
 * #error CSC: SPB is not 4.  CSC will not behave correctly ????????????
 * #endif
 */

    CHECKSUM cks;
    PCHAR pch;
    USHORT i;
    BYTE bDirt;

    /*
     * this pattern is from Gordon's assy code, not sure why it's
     * this value.
     */
    if (pbuf->lsnSec != 0x11223344L) {

	bDirt = pbuf->bDirtMask;

	for (i = 0; i < SPB; i++) {

	    /*
	     * if the dirty bit for the sector is set, we skip the checksum.
	     */
	    if ((bDirt % 2) == 0) {

		cks = CASfar ((PCHAR)((PSECTOR)(pbuf->pblkData) + i), SECSIZE);

		if (pbuf->ausCheckSum[i] != (HIUSHORT(cks) ^ LOUSHORT(cks))) {

		    /*
		     * DEBUG
		     * Note that we'll trap here if RSC
		     * is changed but we forget to change
		     * DoZap which 'knows' the checksum of
		     * a certain specific buffer content
		     */
		    return (FALSE);
		}
	    }

	    bDirt >>= 1;

	}

	return (TRUE);

    } else {

	/*  The buffer has the special 'unused' sector
	 *  number, so we'll fill it with repeating copies
	 *  of the sequence "G+S ".
	 */
	pch = (PCHAR)(pbuf -> pblkData);

	for (i = 1; i < SPB*SECSIZE/4 ; i++) {

	     *pch++ = 'G';
	     *pch++ = '+';
	     *pch++ = 'S';
	     *pch++ = ' ';
	}

	return TRUE;
    }
}

#endif
