⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 si_bpi.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		allOk = ParseAndCheckPragmaPool( bpi, BP, BPlen, &pos );
	}

	if (allOk) {
		allOk = ParseAndCheckFunctionPool( bpi, BP, BPlen, &pos );
	}

	if ( ! allOk )
	{
		if (bpi)
		{
			BPI_Delete( &bpi ); /* ... and bpi = NULL */
		}
	}

	return bpi;	
}


/*==========================================
	BPI_AccessCheck
============================================

---Purpose:
To check if we are allowed to execute and call a function in this bytecode package (BP).
if everything went ok the funcIndexResult parameter will contain the function index of 
the function that we want to call.
	
---Params:
basePath		used to resolve the path specified in the access path pragma
							to an absolute path

---Return:				

------------------------------------------------------------------------*/
enumErrorCode BPI_AccessCheck( pstructBPInfo thisx, BYTE* BP, UINT32 BPend, BYTE* funcName,
		UINT8 nbrOfArgs, WCHAR* refererUrl, WCHAR *baseUrl, UINT8* funcIndexResult )
{
	/*
  By default Access control is disabled
  (i.e. if no access pragmas exist there will be no access checks against
  domain or path)
   */

	BOOL				accessAllowed = TRUE;
	UINT8				funcIndex;
	UINT8				BPIfuncNameLen;	/* length of the funcName in the BPI */
	UINT32			paramFuncNameLen;	/* length of the funcName parameter to this function */
	UINT8				i;
	UINT32			bpPos;
	pstructVar	accessDomain = NULL;
	pstructVar	accessPath = NULL;
	UINT16			cIndex;
	UINT32			BPpos;


	/* check access pragmas */
	i = 0;
	while (i < thisx->nbrOfPragmas)
	{
		switch (thisx->pragmas[i].type)
		{
			case PRAGMA_ACCESS_DOMAIN:
				if (accessDomain != NULL)
				{	/* there can only be one access domain pragma */
					Var_Delete( &accessDomain );
					Var_Delete( &accessPath );
					return ERR_WAE_WMLS_VERIFICATION;
				}
				BPpos = thisx->pragmas[i].pos;
				ReadMBUInt16( &cIndex, BP, BPend, &BPpos );
				accessDomain = BPI_GetConstant( thisx, BP, BPend, cIndex );
				break;
			case PRAGMA_ACCESS_PATH:
				if (accessPath != NULL)
				{	/* there can only be one access path pragma */
					Var_Delete( &accessDomain );
					Var_Delete( &accessPath );
					return ERR_WAE_WMLS_VERIFICATION;
				}
				BPpos = thisx->pragmas[i].pos;
				ReadMBUInt16( &cIndex, BP, BPend, &BPpos );
				accessPath = BPI_GetConstant( thisx, BP, BPend, cIndex );
				break;
		}

		i++;
	}

  /* perform the accesscheck */
	if ((accessDomain != NULL) || (accessPath != NULL))
	{
    BOOL fSlask;
  	BYTE *pbDomain = NULL;
    BYTE *pbPath = NULL;
    BYTE *pbRefUrl = wip_wchar2byte( refererUrl, &fSlask );

		if (accessDomain != NULL)
		{
			pbDomain = wip_wchar2byte( accessDomain->val.theString, &fSlask );
    }
		else
    {
			WCHAR *pwchTemp = NULL;

			w_GetHost (baseUrl, &pwchTemp);
      pbDomain = wip_wchar2byte( pwchTemp, &fSlask );
      DEALLOC( &pwchTemp );
    }
		if (accessPath != NULL)
		{
			/* 990824 (KHN) Access supports relative paths so the
         path must be resolved against the baseURL and then do getpath */
			if (accessPath->val.theString[0] != '/')
      { /* ...then it is a relative path and must be resolved */
				WCHAR* pwchTemp = NULL;
				WCHAR* pwchTemp2 = NULL;

        if (w_Resolve (baseUrl, accessPath->val.theString, &pwchTemp) &&
        		w_GetPath(pwchTemp, &pwchTemp2))
        {
					pbPath = wip_wchar2byte( pwchTemp2, &fSlask );
        }
        else
        {	/* either the resolve or getpath failed, then just try with the path given */
					pbPath = wip_wchar2byte( accessPath->val.theString, &fSlask );
        }
	      DEALLOC( &pwchTemp );
	      DEALLOC( &pwchTemp2 );
      }
      else
      { /* absolute path */
				pbPath = wip_wchar2byte( accessPath->val.theString, &fSlask );
      }
    }

    accessAllowed = URL_CheckAccess( pbRefUrl, pbDomain, pbPath );
    DEALLOC( &pbDomain );
    DEALLOC( &pbPath );
    DEALLOC( &pbRefUrl );
	}
	Var_Delete( &accessDomain );
	Var_Delete( &accessPath );

  /* perform the check for the called function and the number of parameters */
	if (accessAllowed)
	{
		if (funcName == NULL)
		{
			return ERR_WAE_WMLS_NULL;
		}
		paramFuncNameLen = 0;
		while (funcName[paramFuncNameLen] != 0)  /* not NULL */
		{
			paramFuncNameLen++;
		}
		/* the length is now calculated */

		/* now check to see if function to be called exists and that the correct number of arguments are supplied */
		funcIndex = 0;
		while (funcIndex < thisx->nbrOfFunctions)
		{
			if (thisx->functions[funcIndex].funcNamePos != 0)
			{	/* if a named function (public), then perform name comparisson */
				/* first get length of funcName */
				bpPos = thisx->functions[funcIndex].funcNamePos;
				if (!ReadUInt8( &BPIfuncNameLen, BP, BPend, &bpPos ))
				{
					return ERR_WAE_WMLS_VERIFICATION;
				}
				if (BPIfuncNameLen == paramFuncNameLen)
				{	/* compare */
					i = 0;
					while ((i < paramFuncNameLen) && (BP[bpPos]== funcName[i]))
					{
						i++;
						bpPos++;
					}
					if (i == paramFuncNameLen)
					{	/* a match and the function is done */
						/* now check if nbrOfArgs match */
						if (nbrOfArgs == thisx->functions[funcIndex].nbrOfArgs)
						{	/* the correct function is found */
							*funcIndexResult = funcIndex;
							return ERR_WAE_WMLS_NONE;
						}
						else
						{	/* incorrect nbrOfArgs */
							return ERR_WAE_WMLS_FUNC_ARGS;
						}
					}
				}
			}

			funcIndex++;
		}

		return ERR_WAE_WMLS_EXTERNAL; /* if this line is reached, there was no matching function */
	}
	else
	{
		return ERR_WAE_WMLS_ACCESS;
	}
}


/*==========================================
	BPI_SendUApragmasToTheUA
============================================

---Purpose:
Sees to it that the UA is given the information insida any UA pragmas resdiding
in the BP file.

---Params:
thisx					the BP to operate on
BP						a pointer to the compiled script
BPlen					the length (in bytes) of BP
userAgent			the struct of the user agent that invoked this script

---Return:

------------------------------------------------------------------------*/
VOID BPI_SendUApragmasToTheUA( pstructBPInfo thisx, BYTE* BP, UINT32 BPlen,
		pUA userAgent )
{
	pstructVar			stringVar = NULL;
	WCHAR						*wchProperty = NULL;
	WCHAR						*wchContent = NULL;
	WCHAR						*wchScheme = NULL;
  UINT16					pragmaIndx;
  UINT16					constantIndx;
  UINT32					BPpos;
  enumPragmaType	type;


  for (pragmaIndx = 0; pragmaIndx < thisx->nbrOfPragmas; pragmaIndx++)
  {
		type = thisx->pragmas[ pragmaIndx ].type;
		if ( (type == PRAGMA_UA_PROP_SCHEME) || (type == PRAGMA_UA_PROP) )
    {
			BPpos = thisx->pragmas[ pragmaIndx ].pos;

			ReadMBUInt16( &constantIndx, BP, BPlen, &BPpos );
      stringVar = BPI_GetConstant( thisx, BP, BPlen, constantIndx );
      wchProperty = stringVar->val.theString;
			stringVar->val.theString = NULL;
      stringVar->theStringLen = 0;
      Var_Delete( &stringVar );

			ReadMBUInt16( &constantIndx, BP, BPlen, &BPpos );
      stringVar = BPI_GetConstant( thisx, BP, BPlen, constantIndx );
      wchContent = stringVar->val.theString;
			stringVar->val.theString = NULL;
      stringVar->theStringLen = 0;
      Var_Delete( &stringVar );

			if (type == PRAGMA_UA_PROP_SCHEME)
      {
				ReadMBUInt16( &constantIndx, BP, BPlen, &BPpos );
	      stringVar = BPI_GetConstant( thisx, BP, BPlen, constantIndx );
	      wchScheme = stringVar->val.theString;
				stringVar->val.theString = NULL;
	      stringVar->theStringLen = 0;
	      Var_Delete( &stringVar );
      }

			UApragmaFromScript( userAgent, &wchProperty, &wchContent, &wchScheme );

			wchProperty = NULL;
			wchContent = NULL;
			wchScheme = NULL;
    }
  }
}


/*==========================================
	BPI_GetConstantType
============================================

---Purpose:
To retrieve the constant type of a constant with the index <constIndex>.

---Params:
thisx					the BP to operate on
constIndex		the index of the constant that we want the type of
constType			after a successfull call this parameter will hold the type of the constanr

---Return:
TRUE					the call was successful and the type is found in the constType parameter above
FALSE					the operation failed

------------------------------------------------------------------------*/
BOOL BPI_GetConstantType( pstructBPInfo thisx, UINT16 constIndex, enumConstType* constType )
{
	if (thisx != NULL) {
		if (constIndex < (thisx->nbrOfConstants)) {
			*constType = thisx->constants[constIndex].type;
			return TRUE;
		}
	}

	return FALSE; /* if this line is reached, the operation failed */
}



/*==========================================
	BPI_GetConstant
============================================

---Purpose:
To retrieve the constant and return its value as a var.
	
---Params:
thisx					the BP to operate on
constIndex		the index of the constant that we want to get the value of
	
---Return:				
pstructVar		the call was successful
NULL					the operation failed

------------------------------------------------------------------------*/
pstructVar BPI_GetConstant( pstructBPInfo thisx, BYTE* BP, UINT32 BPend, UINT16 constIndex )
{
	pstructVar			result = NULL;
	enumConstType		constType;
	UINT32					constPos;
	INT8						val_i8;
	INT16						val_i16;
	INT32						val_i32;
#ifdef HAS_FLOAT
	FLOAT32					val_f32;
#endif

	UINT32					strByteLen;	/* the length of the string in number of bytes  */
	INT32						strLetterLen;	/* the number of letters/characters in the string */
	BYTE*						strStart = NULL;
	WCHAR*					ucs2Start;

	if ( BPI_GetConstantType( thisx, constIndex, &constType )	) {
		/* a valid constant */

		constPos = thisx->constants[constIndex].pos;

		switch (constType) {

			case CONST_TYPE_INT8:
				result = Var_New();
				ReadInt8( &val_i8, BP, BPend, &constPos );
				Var_AssignInt( result, val_i8 );
				break;

			case CONST_TYPE_INT16:
				result = Var_New();
				ReadInt16( &val_i16, BP, BPend, &constPos );
				Var_AssignInt( result, val_i16 );
				break;

			case CONST_TYPE_INT32:
				result = Var_New();
				ReadInt32( &val_i32, BP, BPend, &constPos );
				Var_AssignInt( result, val_i32 );
				break;


			case CONST_TYPE_FLOAT32:
				result = Var_New();
#ifdef HAS_FLOAT
				ReadFloat32( &val_f32, BP, BPend, &constPos );
				Var_AssignFloat( result, val_f32 );
#else
				Var_AssignInvalid( result );
#endif
				break;


			case CONST_TYPE_EXT_CHARSET:	/* string encoding determined by the BPI->characterSet entry */
			case CONST_TYPE_UTF8:		/* variable length string encoding */
				result = Var_New();
				ReadMBUInt32( &strByteLen, BP, BPend, &constPos );
				strStart	= BP + constPos;
				if (constType == CONST_TYPE_EXT_CHARSET)
        {
					strLetterLen = Iana2Unicode_calcLen( strStart, thisx->characterSet,
          		FALSE, strByteLen, &strByteLen );
        }
        else
        {	/* UTF8 */
					strLetterLen = Iana2Unicode_calcLen( strStart, IANA_CHARSET_UTF8,
          		FALSE, strByteLen, &strByteLen );
        }
				if (strLetterLen >= 0) {
					if (strLetterLen > 0)
          {
						Var_NewString( result, strLetterLen );
						ucs2Start = result->val.theString;
						if (constType == CONST_TYPE_EXT_CHARSET)
	          {
							if ( ! Iana2Unicode_convert( strStart, thisx->characterSet,
	            		strByteLen, ucs2Start, strLetterLen) )
		          {
								Var_AssignInvalid( result );
							}
						}
	          else
	          {	/* UTF8 */
							if ( ! Iana2Unicode_convert( strStart, IANA_CHARSET_UTF8, strByteLen,
	            		ucs2Start, strLetterLen) )
		          {
								Var_AssignInvalid( result );
							}
	          }
          }
          /* else the empty string will be the result */
				}
				else { /* incorrect encoded string, how and which error code should be given??? */
					Var_AssignInvalid( result );
				}
				break;


			case CONST_TYPE_ES:
				result = Var_New();
				break;

		}
	}

	return result;
}


/*==========================================
	BPI_GetRawUTF8Constant
============================================

---Purpose:
To retrieve an UTF8 string constant and return it unconverted 
as an null terminated string
	
---Params:
thisx					the BP to operate on
constIndex		the index of the constant that we want to get the value of
	
---Return:				
BYTE*					a pointer to the UTF8 string constant
NULL					not an UTF8 string constant

------------------------------------------------------------------------*/
BYTE* BPI_GetRawUTF8Constant( pstructBPInfo thisx, BYTE* BP, UINT32 BPend, UINT16 constIndex ) 
{
	enumConstType		constType;
	UINT32					constPos;
	UINT32					strLen;
	BYTE*						result = NULL;
	BYTE*						utf8Pos;


	if ( BPI_GetConstantType( thisx, constIndex, &constType ) && (constType == CONST_TYPE_UTF8)	) {
		/* a valid UTF8 string constant */

		constPos = thisx->constants[constIndex].pos;
		ReadMBUInt32( &strLen, BP, BPend, &constPos );
		utf8Pos	= BP + constPos;

		result = NEWARRAY( BYTE, strLen+1 ); /* +1 to include a NULL */
		result[strLen] = 0; /* now NULL terminated */
		memcpy( result, utf8Pos , strLen );

		return result;
	}
	else {
		return NULL;
	}

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -