/*static char *SCCSID = "@(#)scanea.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)scanea.c 13.2 90/01/25";*/
#define BSWAP0 BSWAP0

#include <os2def.h>

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "ea.h"
#include "fnode.h"
#include "volume.h"
#include "tables.h"
#include "pbmacros.h"
#include "superb.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"

#ifdef DEBUG
#include "pr_debug.h"
#endif
#include "pr_ea.h"
#include "pr_cache.h"
#include "pr_misc.h"
#include "pr_read.h"

/*
 ***	ScanEA - Scan for an EA in a List
 *
 *	ScanEA looks to see if an FEA record appears in an EA list
 *	(FEA list or GEA list).
 *	Note:  the asm version will search FEA lists only
 *
 *	ENTRY	pfea	(esi) = address of FEA record
 *		peal	(edi) = address of EA list
 *
 *	EXIT
 *		0 iff record does NOT appear in list
 *		offset to matching record if it does appear in list
 *
 *	NOTE
 *	    The exit condition may be treated as a BOOL iff the return
 *	    code is tested against FALSE and not against TRUE.
 *	    (i.e.  if( ScanEA() != FALSE ) instead of if( ScanEA() == TRUE ) )
 *	    see following compile time check.
 *
 *	    note also that 0 can never be returned as a valid offset
 *	    since we start looking at pfeal->list which is at offset
 *	    2 (for V1.2) or offset 4 (for V2.x)
 *
 *	HISTORY
 *	    19-Sep-89 by JR (John Rogers, JohnRo@Microsoft)
 *		- Avoid warnings from Microsoft C 5.2 (on slash-slash comments).
 */
#if FALSE != 0
#   error assumption incorrect: FALSE does not equal 0
#endif

USHORT
ScanEA (PFEA pfea, PEALIST peal, BOOL fFEAList)
{
    USHORT  cbKey;		/* size of FEA search key */
    PBYTE   pbKey;		/* FEA search key */
    PEA     peaNext;		/* next possible EA */
    USHORT  cbEABuf;		/* size of EAs left */
    USHORT  cbEA;		/* size of next EA */

    if ((cbEABuf = ((USHORT)peal->cbList - sizeof (peal->cbList))) > 0) {
	cbKey = pfea->cbName;
	pbKey = pfea->szName;
	peaNext = (PEA) peal->list;
	do {
	    if (fFEAList) {

		/* The list contains FEAs */
		/* if the lengths match */
		if (cbKey == peaNext->fea.cbName) {
		    /* lengths compare, now compare bytes */
		    if (memcmp (peaNext->fea.szName, pbKey, cbKey) == 0)
			return ( POINTER_SUBTRACT(peaNext, peal) );
		}
		cbEA = peaNext->fea.cbName + 1 +  /* size of name + nul byte */
		       peaNext->fea.cbValue +	  /* size of value */
		       sizeof (FEA) - 2;    /* size of FEA less name & value */

	    } else {

		/* The list contains GEAs */
		/* if the lengths match */
		if (cbKey == peaNext->gea.cbName) {
		    /* lengths compare, now compare bytes */
		    if (memcmp (peaNext->gea.szName, pbKey, cbKey) == 0)
			return ( POINTER_SUBTRACT(peaNext, peal) );
		}
		cbEA = peaNext->gea.cbName + 1 +  /* size of name + nul byte */
		       sizeof (GEA) - 1;	  /* size of GEA less name */
	    }

	    peaNext = (PEA) BUMP_PTR(peaNext, cbEA);
#ifdef DEBUG
	    if (cbEA > cbEABuf)
		INTERR ("ScanEA: bumping past # of bytes left");
#endif
	    cbEABuf -= cbEA;
	} while (cbEABuf > 0);
    }

    /* return 'FALSE' if we cant find it */
    return (0);

}
