/*	SNMP.c - SNMP support routines.
*
*******************************************************************************
* Copyright (c) 1996-97 Digi International Inc., All Rights Reserved          *
*                                                                             *
* This  software  contains  proprietary  and  confidential   information   of *
* Digi International  Inc.   By  accepting  transfer  of this copy, Recipient *
* agrees to retain this software in  confidence,  to  prevent  disclosure  to *
* others,  and  to make no  use of this software other than that for which it *
* was delivered.  This is an unpublished copyright work  Digi   International *
* Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is *
* strictly prohibited.                                                        *
*                                                                             *
* Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc. *
* Confidential  when   combined   with   the   aggregated  modules  for  this *
* product) OBJECT CODE ONLY SOURCE MATERIALS                                  *
*                                                                             *
* (C) COPYRIGHT Digi International Inc. 1996-97                               *
*                                                                             *
* All Rights Reserved                                                         *
*                                                                             *
* US   Government   Users   Restricted   Rights   -   Use,   duplication   or *
* disclosure  restricted by GSA ADP Schedule Contract with Digi International *
* Inc.                                                                        *
*                                                                             *
*******************************************************************************
*
*/

#include "DS_RAS.h"
#include "SNMPx.h"

static ASN_OBJECT_ARRAY(Scalar) { 0 };
ASN_OBJECT_ID(Scalar);

static ASN_OBJECT_ARRAY(Index) { 1 };
ASN_OBJECT_ID(Index);

size_t OidPartialMatchCountEx(AsnObjectIdentifier const * const poia1, size_t const iOffset1, AsnObjectIdentifier const * const poia2, size_t const iOffset2, size_t const cItems)
{
	size_t i = 0;
	size_t cIdsToMatch = cItems;
	size_t cIdsRemaining;

	cIdsRemaining = poia1->idLength - iOffset1;
	if (cIdsRemaining < cIdsToMatch)
		cIdsToMatch = cIdsRemaining;
	cIdsRemaining = poia2->idLength - iOffset2;
	if (cIdsRemaining < cIdsToMatch)
		cIdsToMatch = cIdsRemaining;

	for (; i < cIdsToMatch; i++)
		if (poia1->ids[iOffset1 + i] != poia2->ids[iOffset2 + i]) goto Done;

Done:

	return i;
}

size_t OidPartialMatchCount(AsnObjectIdentifier const * const poia1, size_t const iOffset1, AsnObjectIdentifier const * const poia2)
{
	return OidPartialMatchCountEx(poia1, iOffset1, poia2, 0, poia2->idLength);
}

BOOL OidPartialMatchEx(AsnObjectIdentifier const * const poia1, size_t const iOffset1, AsnObjectIdentifier const * const poia2, size_t const iOffset2, size_t const cItems)
{
	return OidPartialMatchCountEx(poia1, iOffset1, poia2, iOffset2, cItems) == cItems;
}

BOOL OidPartialMatch(AsnObjectIdentifier const * const poia1, size_t const iOffset1, AsnObjectIdentifier const * const poia2)
{
	return OidPartialMatchEx(poia1, iOffset1, poia2, 0, poia2->idLength);
}

BOOL OidRangeToValue(AsnObjectIdentifier const * const poia, size_t const iOffset, size_t const cIds, BYTE const asnType, AsnAny * const pResult)
{
	BOOL fSuccess = FALSE;

	if (cIds == 0) goto Error;
	if (iOffset + cIds > poia->idLength) goto Error;

	pResult->asnType = asnType;
	switch (asnType)
	{
		case ASN_INTEGER:
		{
			if (cIds != 1) goto Error;

			pResult->asnValue.number = poia->ids[iOffset];

			break;
		}

		case ASN_OCTETSTRING:
		{
			size_t cbData = cIds * sizeof pResult->asnValue.string.stream[0];
			BYTE * const pbStream = SnmpUtilMemAlloc(cbData);
			size_t i;

			if (pbStream == NULL) goto Error;

			for (i = 0; i < cIds; i++)
			{
				size_t iIndex = iOffset + i;
				UINT id = poia->ids[iIndex];
				 
				if (id > UCHAR_MAX)
					break;

				pbStream[i] = id;
			}

			if (i != cIds)
			{
				SnmpUtilMemFree(pbStream);
				goto Error;
			}

			pResult->asnValue.string.stream = pbStream;

			break;
		}

		default:
		{
			goto Error;
			break;
		}
	}

	fSuccess = TRUE;

Error:

	return fSuccess;
}

