static HRESULT Interop_Marshal_NUMERIC_ARRAY
( const CLR_RT_StackFrame &stackFrame, UINT32 paramIndex, T *&pByteParam, UINT32 &arraySize, UINT32 elementSize )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array *pHeapBlockArray = NULL;

    if ( stackFrame.ArgN( paramIndex ).DataType() != DATATYPE_OBJECT )
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
    }

    pHeapBlockArray = stackFrame.ArgN( paramIndex ).DereferenceArray();
    FAULT_ON_NULL(pHeapBlockArray);

    if ( pHeapBlockArray->m_sizeOfElement != elementSize )
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
    }

    arraySize  = pHeapBlockArray->m_numOfElements;
    pByteParam = (T *)pHeapBlockArray->GetFirstElement();

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_usb_native_Microsoft_SPOT_Hardware_UsbClient_UsbStream::nativeWrite___I4__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_INT32               retVal = 0;
    CLR_INT32               controllerIndex, streamIndex, stream;
    CLR_UINT32              offset, count;
    CLR_RT_HeapBlock*       pThis;
    CLR_RT_HeapBlock_Array* array;

    pThis = stack.This(); FAULT_ON_NULL(pThis);

    controllerIndex = pThis[ Library_spot_hardware_usb_native_Microsoft_SPOT_Hardware_UsbClient_UsbStream::FIELD__m_controllerIndex ].NumericByRef().s4;
    streamIndex     = pThis[ Library_spot_hardware_usb_native_Microsoft_SPOT_Hardware_UsbClient_UsbStream::FIELD__m_streamIndex     ].NumericByRef().s4;
    array           = stack.Arg1().DereferenceArray(); FAULT_ON_NULL( array );
    offset          = stack.Arg2().NumericByRef().u4;
    count           = stack.Arg3().NumericByRef().u4;

    stream = (controllerIndex << USB_CONTROLLER_SHIFT) + streamIndex;
    if( array->m_numOfElements < (offset + count) )
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
    }
    else
    {
        retVal = USB_Write( stream, (char*)array->GetFirstElement()+offset, count );
    }
    stack.SetResult_I4( retVal );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::SignUpdateInternal___VOID__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis      = stack.This();
    CLR_RT_HeapBlock_Array* pData      = stack.Arg1().DereferenceArray();
    CLR_INT32               offset     = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len        = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CLR_INT32               sigLen     = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::FIELD__m_signatureLength].NumericByRef().s4;
    CK_SESSION_HANDLE       hSession;

    FAULT_ON_NULL_ARG(pData);
    FAULT_ON_NULL(pSession);

    if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    // TODO: add code for processing chunks at a time if size is too big
    //maxProcessingBytes = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_maxProcessingBytes].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    CRYPTOKI_CHECK_RESULT(stack, C_SignUpdate(hSession, pData->GetElement(offset), len));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_ConstructorInfo::Invoke___OBJECT__SZARRAY_OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*           thisRef = stack.ThisRef().Dereference();
    CLR_RT_MethodDef_Instance   md;
    CLR_RT_HeapBlock_Array*     pArray  = stack.Arg1().DereferenceArray();
    CLR_RT_HeapBlock*           args    = NULL;
    int                         numArgs = 0;

    if(md.InitializeFromIndex( thisRef->ReflectionDataConst().m_data.m_method ) == false) TINYCLR_SET_AND_LEAVE(CLR_E_NULL_REFERENCE);

    if(stack.m_customState == 0)
    {
        stack.m_customState = 1;

        if(pArray)
        {
            args = (CLR_RT_HeapBlock*)pArray->GetFirstElement();
            numArgs = pArray->m_numOfElements;
        }

        TINYCLR_CHECK_HRESULT(stack.MakeCall( md, NULL, args, numArgs ));
    }   

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiVerify::VerifyInternal___BOOLEAN__SZARRAY_U1__I4__I4__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis    = stack.This();
    CLR_RT_HeapBlock_Array* pData    = stack.Arg1().DereferenceArray(); 
    CLR_INT32               offset   = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len      = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock_Array* pSig     = stack.Arg4().DereferenceArray(); 
    CLR_INT32               sigOff   = stack.Arg5().NumericByRef().s4;
    CLR_INT32               sigLen   = stack.Arg6().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE       hSession;
    bool                    retVal   = false;
    CK_RV                   result;

    FAULT_ON_NULL_ARG(pData);
    FAULT_ON_NULL_ARG(pSig);

    if((offset + len   ) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
    if((sigOff + sigLen) > (CLR_INT32)pSig->m_numOfElements ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    result = C_Verify(hSession, pData->GetElement(offset), len, pSig->GetElement(sigOff), sigLen);

    retVal = CKR_OK == result;

    stack.SetResult_Boolean(retVal);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
	NATIVE_PROFILE_CLR_CORE();
	TINYCLR_HEADER();

	CLR_RT_HeapBlock_Array* pArray = stack.Arg0().DereferenceArray();
	FAULT_ON_NULL_ARG(pArray);

	int index;
	index = stack.Arg1().NumericByRefConst().s4;
	int length;
	length = stack.Arg2().NumericByRefConst().s4;
	if (pArray->m_numOfElements == 0 && index == 0 && length == 0)
	{
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(""));
	}
	else
	{
		if (index < 0 || length < 0 || (unsigned int)index >= pArray->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
		if ((unsigned int)index + length > pArray->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);

		BYTE* p;
		p  = pArray->GetFirstElement();
		char* pOutput = ByteArrayToHex(p, index, length);
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(pOutput));
		delete[] pOutput;
	}

	TINYCLR_NOCLEANUP();
}
HRESULT CLR_RT_HeapBlock_MemoryStream::ToArray( CLR_RT_HeapBlock& ref )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_UINT32              tot;
    CLR_RT_HeapBlock_Array* array;
    CLR_UINT8*              buf;

    tot = 0;
    TINYCLR_FOREACH_NODE(Buffer,node,m_buffers)
    {
        tot += node->m_length;
    }
    TINYCLR_FOREACH_NODE_END();

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( ref, tot, g_CLR_RT_WellKnownTypes.m_UInt8 ));

    array = ref.DereferenceArray();
    buf   = array->GetFirstElement();

    TINYCLR_FOREACH_NODE(Buffer,node,m_buffers)
    {
        memcpy( buf, node->m_payload, node->m_length ); buf += node->m_length;
    }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_FindObjectEnum::FindObjectsInit___VOID__SZARRAY_MicrosoftSPOTCryptokiCryptokiAttribute( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
    CLR_RT_HeapBlock*       pThis      = stack.This();
    CLR_RT_HeapBlock_Array* pAttribs   = stack.Arg1().DereferenceArray();
    CLR_RT_HeapBlock*       pAttrib;
    CLR_RT_HeapBlock*       pSession   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_FindObjectEnum::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE       hSession;
    CK_ATTRIBUTE            attribs[20];
    CLR_UINT32              i;

    FAULT_ON_NULL_ARG(pAttribs);
    FAULT_ON_NULL(pSession);

    if(pAttribs->m_numOfElements > ARRAYSIZE(attribs)) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().u4;
    pAttrib  = (CLR_RT_HeapBlock*)pAttribs->GetFirstElement();

    for(i=0; i<pAttribs->m_numOfElements; i++)
    {
        CLR_RT_HeapBlock*       pElement    = pAttrib->Dereference();  FAULT_ON_NULL(pElement);
        CLR_RT_HeapBlock_Array* pValueArray = pElement[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiAttribute::FIELD__Value].DereferenceArray();  FAULT_ON_NULL(pValueArray);

        attribs[i].type       = pElement[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiAttribute::FIELD__Type].NumericByRef().u4;
        attribs[i].pValue     = pValueArray->GetFirstElement();
        attribs[i].ulValueLen = pValueArray->m_numOfElements;

        pAttrib++;
    }

    CRYPTOKI_CHECK_RESULT(stack, C_FindObjectsInit(hSession, attribs, pAttribs->m_numOfElements));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::MarshalSockAddress( CLR_RT_HeapBlock& blkDst, const struct SOCK_sockaddr* addrSrc, CLR_UINT32 addrLenSrc )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* arr = NULL;

    CLR_RT_HeapBlock blkArr; blkArr.SetObjectReference( NULL );
    CLR_RT_ProtectFromGC gc( blkArr );
    SOCK_sockaddr_in* dst;
    SOCK_sockaddr_in* src = (SOCK_sockaddr_in*)addrSrc;
        
    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( blkArr, addrLenSrc, g_CLR_RT_WellKnownTypes.m_UInt8 ));
    
    arr = blkArr.DereferenceArray();

    _ASSERTE(arr);

    dst = (SOCK_sockaddr_in*)arr->GetFirstElement();

    dst->sin_family           = SwapEndianIfBEc16(src->sin_family);
    dst->sin_port             = src->sin_port;
    dst->sin_addr.S_un.S_addr = src->sin_addr.S_un.S_addr;

    memcpy(dst->sin_zero, src->sin_zero, sizeof(dst->sin_zero));

    _ASSERTE(blkDst.DataType() == DATATYPE_BYREF || blkDst.DataType() == DATATYPE_ARRAY_BYREF);

    TINYCLR_CHECK_HRESULT(blkArr.StoreToReference( blkDst, 0 ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::MarshalSockAddress( struct SOCK_sockaddr* addrDst, CLR_UINT32& addrLen, const CLR_RT_HeapBlock& blkSockAddress )
{        
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* ptrSockAddress;
    SOCK_sockaddr_in* dst = (SOCK_sockaddr_in*)addrDst;
    SOCK_sockaddr_in* src;    

    ptrSockAddress = blkSockAddress.DereferenceArray();                    
    FAULT_ON_NULL(ptrSockAddress);

    if(ptrSockAddress->m_numOfElements > addrLen) TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);

    src = (SOCK_sockaddr_in*)ptrSockAddress->GetFirstElement();

    dst->sin_family           = SwapEndianIfBEc16(src->sin_family);
    dst->sin_port             = src->sin_port;
    dst->sin_addr.S_un.S_addr = src->sin_addr.S_un.S_addr; //already in network byte order

    memcpy(dst->sin_zero, src->sin_zero, sizeof(dst->sin_zero));

    addrLen = ptrSockAddress->m_numOfElements;

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_SPI::InternalWriteRead___VOID__SZARRAY_U2__I4__I4__SZARRAY_U2__I4__I4__I4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_HARDWARE();
    TINYCLR_HEADER();
    {
        CLR_RT_HeapBlock*       pThis           = stack.This();                    FAULT_ON_NULL(pThis);
        CLR_RT_HeapBlock_Array* writeBuffer     = stack.Arg1().DereferenceArray(); FAULT_ON_NULL(writeBuffer);
        CLR_INT32               writeOffset     = stack.Arg2().NumericByRef().s4;
        CLR_INT32               writeCount      = stack.Arg3().NumericByRef().s4;
        CLR_RT_HeapBlock_Array* readBuffer      = stack.Arg4().DereferenceArray(); 
        CLR_INT32               readOffset      = stack.Arg5().NumericByRef().s4;
        CLR_INT32               readCount       = stack.Arg6().NumericByRef().s4;
        CLR_UINT32              startReadOffset = stack.Arg7().NumericByRef().s4;
        
        SPI_CONFIGURATION       config;
        TINYCLR_CHECK_HRESULT(Library_spot_hardware_native_Microsoft_SPOT_Hardware_SPI__Configuration::GetInitialConfig( pThis[ FIELD__m_config ], config ));

        config.MD_16bits = TRUE;

        CPU_SPI_Initialize();
        
        if(!::CPU_SPI_nWrite16_nRead16(
                                    config,
                                    (CLR_UINT16*)writeBuffer->GetElement(writeOffset), 
                                    writeCount,
                                    readBuffer == NULL ? NULL : (CLR_UINT16*)readBuffer ->GetElement(readOffset), 
                                    readBuffer == NULL ? 0    : readCount,
                                    startReadOffset
                                  ))
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION);
        }
    }
    TINYCLR_NOCLEANUP();
}
// TODO: Make common functions for transformBlock for encrypt/decrypt when async logic is in place
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Decryptor::TransformBlockInternal___I4__SZARRAY_U1__I4__I4__SZARRAY_U1__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis      = stack.This();
    CLR_RT_HeapBlock_Array* pData      = stack.Arg1().DereferenceArray(); 
    CLR_INT32               dataOffset = stack.Arg2().NumericByRef().s4;
    CLR_INT32               dataLen    = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock_Array* pOutput    = stack.Arg4().DereferenceArray(); 
    CLR_INT32               outOffset  = stack.Arg5().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE       hSession;
    CLR_UINT32              decrSize;   

    FAULT_ON_NULL_ARG(pData);
    FAULT_ON_NULL_ARG(pOutput);
    FAULT_ON_NULL_ARG(pSession);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    if((dataOffset + dataLen) > (CLR_INT32)pData->m_numOfElements  ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
    if((outOffset           ) > (CLR_INT32)pOutput->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    decrSize = pOutput->m_numOfElements - outOffset;

    CRYPTOKI_CHECK_RESULT(stack, C_DecryptUpdate(hSession, pData->GetElement(dataOffset), dataLen, pOutput->GetElement(outOffset), (CK_ULONG_PTR)&decrSize));

    stack.SetResult_I4(decrSize);

    TINYCLR_NOCLEANUP();
}
HRESULT CLR_RT_HeapBlock_Queue::Clear()
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* array = GetArray();
    CLR_INT32               size  = GetSize();
    CLR_INT32               head  =    Head();
    CLR_INT32               tail  = GetTail();

    if(size > 0)
    {
        if(head < tail)
        {
            TINYCLR_CHECK_HRESULT(array->ClearElements( head, size ));
        }
        else
        {
            TINYCLR_CHECK_HRESULT(array->ClearElements( head, array->m_numOfElements - head ));
            TINYCLR_CHECK_HRESULT(array->ClearElements( 0   , tail                          ));
        }
        
        SetSize( 0 );
    }

    SetHead( 0 );
    SetTail( 0 );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Text_UTF8Encoding::GetBytes___I4__STRING__I4__I4__SZARRAY_U1__I4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    size_t                  cMaxBytes;
    LPCSTR                  str         = stack.Arg1().RecoverString();
    CLR_INT32               strIdx      = stack.Arg2().NumericByRef().s4;
    CLR_INT32               strCnt      = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock_Array* pArrayBytes = stack.Arg4().DereferenceArray();
    CLR_INT32               byteIdx     = stack.Arg5().NumericByRef().s4;

    FAULT_ON_NULL(str);
    FAULT_ON_NULL(pArrayBytes);

    cMaxBytes = hal_strlen_s(str);

    if((strIdx  + strCnt) > (CLR_INT32)cMaxBytes                   ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
    if((byteIdx + strCnt) > (CLR_INT32)pArrayBytes->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    memcpy(pArrayBytes->GetElement(byteIdx), &str[strIdx], strCnt);

    stack.SetResult_I4(strCnt);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_update_native_Microsoft_SPOT_MFUpdate_MFNativeUpdate::AddPacket___STATIC__BOOLEAN__I4__I4__SZARRAY_U1__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_INT32  handle       = stack.Arg0().NumericByRef().s4;
    CLR_INT32  pktIndex     = stack.Arg1().NumericByRef().s4;
    CLR_RT_HeapBlock_Array* pPacket     = stack.Arg2().DereferenceArray();
    CLR_RT_HeapBlock_Array* pValidation = stack.Arg3().DereferenceArray();
    CLR_UINT8* pValidData;
    CLR_INT32 validLen;

    FAULT_ON_NULL(pPacket);

    if(pValidation == NULL)
    {
        pValidData = NULL;
        validLen = 0;
    }
    else
    {
        pValidData = pValidation->GetFirstElement();
        validLen = pValidation->m_numOfElements;
    }

    stack.SetResult_Boolean(TRUE == MFUpdate_AddPacket(handle, pktIndex, pPacket->GetFirstElement(), pPacket->m_numOfElements, pValidData, validLen));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_NetworkInformation_NetworkInterface::UpdateConfiguration___VOID__I4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    SOCK_NetworkConfiguration config; 
    CLR_RT_HeapBlock* pConfig           = stack.Arg0().Dereference();  _ASSERTE(pConfig != NULL);
    CLR_UINT32 interfaceIndex           = pConfig[ FIELD___interfaceIndex ].NumericByRefConst().u4;
    CLR_UINT32 updateFlags              = stack.Arg1().NumericByRef().u4;
    CLR_RT_HeapBlock_Array* pMACAddress = pConfig[ FIELD___macAddress ].DereferenceArray();

    TINYCLR_CLEAR(config);

    config.flags                = pConfig[ FIELD___flags                ].NumericByRef().u4;
    config.ipaddr               = pConfig[ FIELD___ipAddress            ].NumericByRef().u4;
    config.gateway              = pConfig[ FIELD___gatewayAddress       ].NumericByRef().u4;
    config.subnetmask           = pConfig[ FIELD___subnetMask           ].NumericByRef().u4;
    config.dnsServer1           = pConfig[ FIELD___dnsAddress1          ].NumericByRef().u4;
    config.dnsServer2           = pConfig[ FIELD___dnsAddress2          ].NumericByRef().u4;
    config.networkInterfaceType = pConfig[ FIELD___networkInterfaceType ].NumericByRef().u4;
    
    if(pMACAddress != NULL)
    {
        config.macAddressLen = pMACAddress->m_numOfElements;
        
        if(config.macAddressLen > sizeof(config.macAddressBuffer)) TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
        
        memcpy( &config.macAddressBuffer, pMACAddress->GetFirstElement(), config.macAddressLen ); 
    }

    TINYCLR_CHECK_HRESULT(SOCK_CONFIGURATION_UpdateAdapterConfiguration( interfaceIndex, updateFlags, &config ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiDigest::DigestInternal___SZARRAY_U1__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis      = stack.This();
    CLR_RT_HeapBlock_Array* pData      = stack.Arg1().DereferenceArray(); 
    CLR_INT32               offset     = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len        = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CLR_INT32               digestSize = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiDigest::FIELD__m_hashSize].NumericByRef().s4;
    CK_SESSION_HANDLE       hSession;
    //CLR_INT32                     maxProcessingBytes;

    FAULT_ON_NULL_ARG(pData);
    FAULT_ON_NULL(pSession);

    if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    // TODO: add code for processing chunks at a time if size is too big
    //maxProcessingBytes = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_maxProcessingBytes].NumericByRef().s4;

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValue(), digestSize, g_CLR_RT_WellKnownTypes.m_UInt8));

    CRYPTOKI_CHECK_RESULT(stack, C_Digest(hSession, pData->GetElement(offset), len, stack.TopValue().DereferenceArray()->GetFirstElement(), (CK_ULONG_PTR)&digestSize));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_Assembly::Load___STATIC__SystemReflectionAssembly__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* array = NULL;
    CLR_RT_Assembly*        assm  = NULL;
    CLR_RT_HeapBlock&       top   = stack.PushValueAndClear();
    CLR_RT_HeapBlock*       hbObj;
    CLR_RECORD_ASSEMBLY*    header;

    array = stack.Arg0().DereferenceArray(); FAULT_ON_NULL(array);

    header = (CLR_RECORD_ASSEMBLY*)array->GetFirstElement();
    
    TINYCLR_CHECK_HRESULT(CLR_RT_Assembly::VerifyEndian(header));

    if(header->GoodAssembly())
    {
        //
        // Sorry, you'll have to reboot to load this assembly.
        //
        if(header->flags & CLR_RECORD_ASSEMBLY::c_Flags_NeedReboot)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_BUSY);
        }

        TINYCLR_CHECK_HRESULT(CLR_RT_Assembly::CreateInstance( header, assm ));

        assm->m_pFile = array;

        g_CLR_RT_TypeSystem.Link( assm );

        TINYCLR_CHECK_HRESULT(g_CLR_RT_TypeSystem.ResolveAll         ());
        TINYCLR_CHECK_HRESULT(g_CLR_RT_TypeSystem.PrepareForExecution());
    }

    if(assm)
    {
        CLR_RT_Assembly_Index idx; idx.Set( assm->m_idx );

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex(top, g_CLR_RT_WellKnownTypes.m_Assembly));
        
        hbObj = top.Dereference();
        hbObj->SetReflection( idx );
    }

    TINYCLR_CLEANUP();

    if(FAILED(hr))
    {
        if(assm)
        {
            assm->DestroyInstance();
        }
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_spot_native_Microsoft_SPOT_Reflection::GetAssemblyInfo___STATIC__BOOLEAN__SZARRAY_U1__MicrosoftSPOTReflectionAssemblyInfo( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* array = NULL;
    CLR_RT_Assembly*        assm  = NULL;
    CLR_RECORD_ASSEMBLY*    header;

    array = stack.Arg0().DereferenceArray(); FAULT_ON_NULL(array);

    header = (CLR_RECORD_ASSEMBLY*)array->GetFirstElement();

    if(header->GoodAssembly())
    {
        CLR_RT_HeapBlock* dst = stack.Arg1().Dereference(); FAULT_ON_NULL(dst);

        TINYCLR_CHECK_HRESULT(CLR_RT_Assembly::CreateInstance( header, assm ));

        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( dst[ Library_spot_native_Microsoft_SPOT_Reflection__AssemblyInfo::FIELD__m_name ], assm->m_szName ));

        {
            CLR_RT_HeapBlock& refs   = dst[ Library_spot_native_Microsoft_SPOT_Reflection__AssemblyInfo::FIELD__m_refs ];
            CLR_UINT32        numRef = assm->m_pTablesSize[ TBL_AssemblyRef ];


            TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( refs, numRef, g_CLR_RT_WellKnownTypes.m_UInt32 ));

            {
                const CLR_RECORD_ASSEMBLYREF* ar  =              assm->GetAssemblyRef( 0 );
                CLR_UINT32*                   dst = (CLR_UINT32*)refs.DereferenceArray()->GetFirstElement();

                while(numRef--)
                {
                    *dst++ = assm->ComputeAssemblyHash( ar++ );
                }
            }
        }

        dst[ Library_spot_native_Microsoft_SPOT_Reflection__AssemblyInfo::FIELD__m_flags ].SetInteger(                 assm->m_header->flags                  , DATATYPE_U4 );
        dst[ Library_spot_native_Microsoft_SPOT_Reflection__AssemblyInfo::FIELD__m_size  ].SetInteger( ROUNDTOMULTIPLE(assm->m_header->TotalSize(), CLR_INT32), DATATYPE_I4 );
        dst[ Library_spot_native_Microsoft_SPOT_Reflection__AssemblyInfo::FIELD__m_hash  ].SetInteger(                 assm->ComputeAssemblyHash()            , DATATYPE_U4 );

        stack.SetResult_Boolean( true );
    }
    else
    {
        stack.SetResult_Boolean( false );
    }

    TINYCLR_CLEANUP();

    if(assm)
    {
        assm->DestroyInstance();
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_spot_update_native_Microsoft_SPOT_MFUpdate_MFNativeUpdate::Authenticate___STATIC__BOOLEAN__I4__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_INT32               handle  = stack.Arg0().NumericByRef().s4;
    CLR_RT_HeapBlock_Array* paArgs  = stack.Arg1().DereferenceArray();
    CLR_UINT8*              pArgs   = paArgs == NULL ? NULL : paArgs->GetFirstElement();
    CLR_INT32               argsLen = paArgs == NULL ? 0    : paArgs->m_numOfElements;

    stack.SetResult_Boolean(TRUE == MFUpdate_Authenticate(handle, pArgs, argsLen));

    TINYCLR_NOCLEANUP_NOLABEL();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Decryptor::TransformFinalBlockInternal___SZARRAY_U1__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis     = stack.This();
    CLR_RT_HeapBlock_Array* pData     = stack.Arg1().DereferenceArray(); 
    CLR_INT32               offset    = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len       = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession  = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CLR_UINT32              decrSize  = 0;
    CK_SESSION_HANDLE       hSession;
    CLR_RT_HeapBlock        hbRef;
    CLR_RT_HeapBlock*       pRet;
    CLR_UINT32              maxOut;

    FAULT_ON_NULL_ARG(pData);
    FAULT_ON_NULL_ARG(pSession);

    if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession  = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    CRYPTOKI_CHECK_RESULT(stack, C_Decrypt(hSession, pData->GetElement(offset), len, NULL, (CK_ULONG_PTR)&decrSize));

    maxOut = decrSize;

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(hbRef, decrSize, g_CLR_RT_WellKnownTypes.m_UInt8));

    CRYPTOKI_CHECK_RESULT(stack, C_Decrypt(hSession, pData->GetElement(offset), len, hbRef.DereferenceArray()->GetFirstElement(), (CK_ULONG_PTR)&decrSize));

    if(decrSize < maxOut)
    {
        CLR_RT_HeapBlock refSmall;
        
        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(refSmall, decrSize, g_CLR_RT_WellKnownTypes.m_UInt8));

        memcpy(refSmall.DereferenceArray()->GetFirstElement(), hbRef.DereferenceArray()->GetFirstElement(), decrSize);

        pRet = refSmall.Dereference();
    }
    else
    {
        pRet = hbRef.Dereference();
    }

    stack.SetResult_Object(pRet);

    TINYCLR_NOCLEANUP();
}
HRESULT CLR_RT_HeapBlock_Array::CreateInstance( CLR_RT_HeapBlock& reference, CLR_UINT32 length, const CLR_RT_ReflectionDef_Index& reflex )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* pArray;
    CLR_RT_TypeDef_Index    cls;
    CLR_RT_TypeDef_Instance inst;

    reference.SetObjectReference( NULL );

    if((CLR_INT32)length < 0) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    if(reflex.m_kind != REFLECTION_TYPE)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
    }

    if(reflex.m_levels == 1)
    {
        cls = reflex.m_data.m_type;
    }
    else
    {
        cls = g_CLR_RT_WellKnownTypes.m_Array;
    }

    if(inst.InitializeFromIndex( cls ) == false)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
    }
    else
    {
        CLR_DataType                 dt  = (CLR_DataType)inst.m_target->dataType;
        const CLR_RT_DataTypeLookup& dtl = c_CLR_RT_DataTypeLookup[ dt ];

        if(dtl.m_sizeInBytes == CLR_RT_DataTypeLookup::c_NA)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
        }

        pArray    = (CLR_RT_HeapBlock_Array*)g_CLR_RT_ExecutionEngine.ExtractHeapBlocksForArray( inst, length, reflex ); CHECK_ALLOCATION(pArray);

        reference.SetObjectReference( pArray );

        TINYCLR_SET_AND_LEAVE(pArray->ClearElements( 0, length ));
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Random::NextBytes___VOID__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
    
    CLR_RT_Random* rand;
    CLR_RT_HeapBlock_Array* buffer;

    TINYCLR_CHECK_HRESULT(GetRandom( stack, rand ));

    buffer = stack.Arg1().DereferenceArray(); FAULT_ON_NULL(buffer);

    rand->NextBytes( buffer->GetFirstElement(), buffer->m_numOfElements );
        
    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_update_native_Microsoft_SPOT_MFUpdate_MFNativeUpdate::DeserializeParameter___STATIC__BOOLEAN__SZARRAY_U1__OBJECT( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_INT32 offset = 0;

    CLR_RT_HeapBlock_Array* array = stack.Arg0().DereferenceArray();
    CLR_RT_HeapBlock&         obj = stack.Arg1();

    if(!DeserializeObject( &obj, array->GetFirstElement(), offset, array->m_numOfElements )) TINYCLR_SET_AND_LEAVE(CLR_E_FAIL);

    stack.SetResult_Boolean(true);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_MethodBase::Invoke___OBJECT__OBJECT__SZARRAY_OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock&           obj     = stack.Arg1();
    CLR_RT_MethodDef_Instance   md;
    const CLR_RECORD_METHODDEF* mdR;
    CLR_RT_HeapBlock_Array*     pArray  = stack.Arg2().DereferenceArray();
    CLR_RT_HeapBlock*           args    = NULL;
    int                         numArgs = 0;
    CLR_RT_HeapBlock*           hbMeth  = stack.Arg0().Dereference();

    TINYCLR_CHECK_HRESULT(GetMethodDescriptor( stack, *hbMeth, md ));

    mdR = md.m_target;

    if(stack.m_customState == 0)
    {
        stack.m_customState = 1;

        if(pArray)
        {
            args    = (CLR_RT_HeapBlock*)pArray->GetFirstElement();
            numArgs =                    pArray->m_numOfElements;
        }

        TINYCLR_CHECK_HRESULT(stack.MakeCall( md, &obj, args, numArgs ));
    }
    else
    {                
        if(mdR->retVal != DATATYPE_VOID)
        {                    
            if(mdR->retVal < DATATYPE_I4)
            {
                stack.TopValue().ChangeDataType( mdR->retVal );
            }

            TINYCLR_CHECK_HRESULT(stack.TopValue().PerformBoxingIfNeeded());
        }
        else
        {               
            stack.SetResult_Object( NULL );
        }
    }

    TINYCLR_NOCLEANUP();
}
HRESULT CLR_RT_HeapBlock_XmlNameTable::Grow()
{
    TINYCLR_HEADER();
    
    CLR_RT_HeapBlock_Array*              oldEntries;
    CLR_RT_HeapBlock                     newEntriesHB;
    CLR_RT_HeapBlock_Array*              newEntries;
    CLR_RT_HeapBlock_XmlNameTable_Entry* entry;
    CLR_RT_HeapBlock_XmlNameTable_Entry* next;
    CLR_RT_HeapBlock*                    newEntryHB;

    CLR_UINT32                           i;
    CLR_INT32                            newIndex;
    CLR_INT32                            newMask;

    newMask = GetMask() * 2 + 1;

    // allocate a new instance of Entry array
    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( newEntriesHB, newMask + 1, g_CLR_RT_WellKnownTypes.m_XmlNameTable_Entry ));

    newEntries = newEntriesHB.DereferenceArray();
    oldEntries = GetEntries();

    // Go through the old buckets, and resort them
    for(i = 0; i < oldEntries->m_numOfElements; i++)
    {
        entry = (CLR_RT_HeapBlock_XmlNameTable_Entry*)((CLR_RT_HeapBlock*)oldEntries->GetElement( i ))->Dereference();
    
        while(entry != NULL)
        {
            newIndex = entry->GetHashCode() & newMask;
            next = entry->GetNext();
    
            newEntryHB = (CLR_RT_HeapBlock*)newEntries->GetElement( newIndex );
    
            entry->SetNext( (CLR_RT_HeapBlock_XmlNameTable_Entry*)newEntryHB->Dereference() );
    
            newEntryHB->SetObjectReference( entry );
    
            entry = next;
        }
    }

    SetEntries( newEntries );
    SetMask   ( newMask    );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_HardwareProvider::NativeGetPinsMap___VOID__SZARRAY_MicrosoftSPOTHardwareCpuPinUsage( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* map;
    CLR_INT32               mapLength;
    CLR_UINT8 *             ptr;
    map = stack.Arg1().DereferenceArray();  FAULT_ON_NULL(map);

    mapLength   = map->m_numOfElements;
    ptr         = map->GetFirstElement();

   ::CPU_GPIO_GetPinsMap( ptr, mapLength );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_net_security_native_Microsoft_SPOT_Net_Security_SslNative::UpdateCertificates___STATIC__VOID__I4__MicrosoftSPOTNativeSystemSecurityCryptographyX509CertificatesX509Certificate__SZARRAY_MicrosoftSPOTNativeSystemSecurityCryptographyX509CertificatesX509Certificate( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_INT32               sslContext = stack.Arg0().NumericByRef().s4;
    CLR_RT_HeapBlock*       hbCert     = stack.Arg1().Dereference(); 
    CLR_RT_HeapBlock_Array* arrCA      = stack.Arg2().DereferenceArray(); 
    CLR_RT_HeapBlock_Array* arrCert;
    CLR_UINT8* sslCert;
    int        i;
    CLR_RT_HeapBlock*       hbPwd;
    LPCSTR  szPwd;

    FAULT_ON_NULL(hbCert);
    FAULT_ON_NULL(arrCA);

    arrCert    = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray(); FAULT_ON_NULL(arrCert);

    sslCert    = arrCert->GetFirstElement();

    hbPwd      = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference(); FAULT_ON_NULL(hbPwd);

    szPwd      = hbPwd->StringText();
    
    SSL_ClearCertificateAuthority( sslContext );

    if(!SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, szPwd )) TINYCLR_SET_AND_LEAVE(CLR_E_FAIL);

    for(i=0; i<(int)arrCA->m_numOfElements; i++)
    {
        hbCert = (CLR_RT_HeapBlock*)arrCA->GetElement( i ); FAULT_ON_NULL(arrCert);

        arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray();

        sslCert = arrCert->GetFirstElement();

        hbPwd      = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference();
        
        szPwd      = hbPwd->StringText();
        
        if(!SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, szPwd )) TINYCLR_SET_AND_LEAVE(CLR_E_FAIL);
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiRNG::GenerateRandom___VOID__SZARRAY_U1__I4__I4__BOOLEAN( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis    = stack.This();
    CLR_RT_HeapBlock*       pSession;
    CLR_RT_HeapBlock_Array* pData    = stack.Arg1().DereferenceArray();
    CLR_INT32               offset   = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len      = stack.Arg3().NumericByRef().s4;
    bool                    fNonZero = stack.Arg4().NumericByRef().s4 == 1;
    CK_SESSION_HANDLE       hSession;
    CLR_UINT8*              pDataElem;

    FAULT_ON_NULL_ARG(pData);

    if(len+offset > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); FAULT_ON_NULL(pSession);
    hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    pDataElem = pData->GetElement(offset);
    
    CRYPTOKI_CHECK_RESULT(stack, C_GenerateRandom(hSession, pDataElem, len));

    if(fNonZero)
    {
        int i,idx = -1;
        CLR_UINT8 replacements[20];

        for(i=0; i<len; i++)
        {
            if(*pDataElem == 0)
            {
                if(idx == -1 || idx >= ARRAYSIZE(replacements))
                {
                    CRYPTOKI_CHECK_RESULT(stack, C_GenerateRandom(hSession, replacements, ARRAYSIZE(replacements)));
                    idx = 0;
                }

                *pDataElem = replacements[idx++];
            }
        }
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_Hardware_Utility::InsertOrExtractValueFromArray( CLR_RT_StackFrame& stack, bool fInsert )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* array;
    CLR_INT32               offset;
    CLR_INT32               size;
    CLR_UINT32              res;

    array  = stack.Arg0().DereferenceArray(); FAULT_ON_NULL(array);
    offset = stack.Arg1().NumericByRefConst().s4;
    size   = stack.Arg2().NumericByRefConst().s4;
    res    = 0;

    switch(size)
    {
    case 1:
    case 2:
    case 4:
        if(offset >= 0 && (CLR_UINT32)(offset + size) <= array->m_numOfElements)
        {
            CLR_UINT8* ptr = array->GetElement( offset );

            if(fInsert)
            {
                res = stack.Arg3().NumericByRef().u4;

                memcpy( ptr, &res, size );
            }
            else
            {
                memcpy( &res, ptr, size );

                stack.SetResult( res, DATATYPE_U4 );
            }

            TINYCLR_SET_AND_LEAVE(S_OK);
        }
        break;
    }

    TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    TINYCLR_NOCLEANUP();
}