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

📄 wml_uafn.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
                                                    iSendDataLen,
                                                    (UINT8)pGoElement->iMethod,
                                                    pGoElement->iEnctype,
                                                    iCharEnc);
    }
	else
	{
		DEALLOC(&pbSendData);
	}
}

 /*----------------- transcode according to characterencoding -------------- */

void TransCodeData(INT16 iCharEnc, WCHAR* pwchName, WCHAR* pwchValue, BYTE** ppNameResult, BYTE** ppValueResult )
{
   BYTE* pResStr1 = NULL;
   BYTE* pResStr2 = NULL;
   BYTE* pDummy = NULL;
   BOOL fDummy;

   switch (iCharEnc)
   {
   case IANA_CHARSET_UTF8:  /* transcode to UTF-8 */
	   {
		  UINT32 iNameLen =0;
		  UINT32 iValueLen =0;

		  if (pwchName)
		  {	  
			/* get the encoded length */   
			iNameLen = Unicode2UTF8_calcLen((WCHAR*) pwchName, STRINGLENGTH((WCHAR*)pwchName) );
			if (iNameLen)
				pResStr1 = NEWARRAY(BYTE,iNameLen+1);

			/* encode the unicode string */
		 	if (pResStr1 && Unicode2UTF8_convert( pwchName, STRINGLENGTH( pwchName ), pResStr1, iNameLen, & pDummy ) )
			{
				pResStr1[iNameLen] = 0; /* ensure 0 termination */
			}
			pDummy=NULL; 
 
		  }

		  if (pwchValue)
		  {
			/* get the encoded length */
			iValueLen = Unicode2UTF8_calcLen(  pwchValue, STRINGLENGTH( pwchValue ) );

			if (iValueLen)
			  pResStr2 = NEWARRAY(BYTE,iValueLen+1);

			/* encode the unicode string */
			if (pResStr2 && Unicode2UTF8_convert( pwchValue, STRINGLENGTH(pwchValue), pResStr2, iValueLen, & pDummy ))
			{
			   pResStr2[iValueLen] = 0;    /* ensure 0 termination */
			}
			pDummy=NULL;
		  }

	   break;
	   }
   case IANA_CHARSET_USASCII :
   case IANA_CHARSET_LATIN1 : 
 	  pResStr1 = wip_wchar2byte(pwchName, & fDummy);
	  pResStr2 = wip_wchar2byte(pwchValue, & fDummy);
	  break; 
   case IANA_CHARSET_UCS2 :
      pResStr1 = GenCopyString(2, pwchName);
      pResStr2 = GenCopyString(2, pwchValue);
	  break;
   default:  /* best effort ISO-LATIN... */
	   {
		  pResStr1 = wip_wchar2byte(pwchName, & fDummy);
		  pResStr2 = wip_wchar2byte(pwchValue, & fDummy);
		  iCharEnc = IANA_CHARSET_LATIN1;
		  break; 
	   }
   }

   *ppNameResult = pResStr1;
   *ppValueResult = pResStr2;

}		   

/*-----------------------------------------------------------------------------------------
 create a postbody according to multipart/form-data 
-------------------------------------------------------------------------------------------*/
void CreatePostBody(UA* pUserAgent, GOELEMENT* pGoElement, INT16 iCharEnc, BYTE** pbData, UINT16* piDataLen)
{
	BYTE* pPostBody = NULL;
    UINT32 iPostBodyLen = 0;
	MULTIPARTSTR* pMultiPartHandle=NULL;
	ELEMENTTYPE* pPostField = NULL;
	WCHAR* pwchName=NULL;
	WCHAR* pwchValue=NULL;
	BYTE* pbNameData = NULL;
	BYTE* pbValueData = NULL;
	UINT32 iDataLen=0;

    pPostField = SDL_GetElementContent( pGoElement );

	if (!pPostField)
	{
	   *pbData = NULL;
	   *piDataLen =0; 
	   return;
	}

	/* Changed 2000-02-09, JPR */

	while ( (pPostField) )
	{

		 if ((pPostField->iType==Type_POSTFIELD) )
		 {
#ifdef USE_KSC5601
			WCHAR   *pWCHAR=NULL;
			INT16 iBuffLength=0;
			BYTE* pResultBuffer=NULL;
#endif

			   /* evaluate name and variable */
			   pwchName =  SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pName); 	

#ifdef USE_KSC5601
			   pWCHAR = SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pValue);

			   iBuffLength = KSCStrLenOfUni(pWCHAR);
			   if (iBuffLength)
			   {
				  pResultBuffer = NEWARRAY(BYTE,iBuffLength+1);
				  iBuffLength = Uni2KSCString(pWCHAR, pResultBuffer);
			   }
			   OSConnectorFree(pWCHAR);
			   pwchValue = NULL;
#else
			   pwchValue = SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pValue);
#endif

			   /* transcode according to characterencoding  */
			   TransCodeData(iCharEnc, pwchName, pwchValue, & pbNameData, & pbValueData );
#ifdef USE_KSC5601
				pbValueData = pResultBuffer;
				pResultBuffer = NULL;
#endif
				DEALLOC( & pwchName);
				DEALLOC( & pwchValue); 


			   if (pbValueData!=NULL)
			   {
				   /* Get length of value data including termination character */
				   iDataLen=B_STRINGLENGTH((const char *)pbValueData)+1;

				   WAE_AddTextFormDataPart (&pMultiPartHandle, pbNameData, &pbValueData, iDataLen, iCharEnc );

				   /* Delete the memory for pNameData. pValueData is taken over by 
					  the function WAE_AddTextFormDataPart. */
				   DEALLOC(&pbNameData);

			   }
		 } 
	   pPostField = WML_GetNextElement( pPostField);
	}

	if (!(WSP_BuildMultipart (& pMultiPartHandle , &pPostBody, &iPostBodyLen, 0))) 
	{
		/* Multipart creation failed. */
		
		pPostBody = NULL;
		iPostBodyLen = 0;
	}
	
	WSP_DeleteMultipartListWithContent (& pMultiPartHandle );

	*pbData=pPostBody;
/*GBU 000210
     *piDataLen =iPostBodyLen;
*/
    *piDataLen = (UINT16)iPostBodyLen;
}


/*----------------------------------------------------------------------------------------- 
Create a application/x-url-encoded string to be attached to either the URL or the post body 
-------------------------------------------------------------------------------------------*/
void CreatePostString(UA* pUserAgent,  GOELEMENT* pGoElement, INT16 iCharEnc, BYTE** ppString, UINT16* piDataLen ) 
{

    void    *pNameData;
    void    *pValueData;
    WCHAR   *pWValue, *pWName;
    BYTE* pResStr1=NULL;
    BYTE* pResStr2=NULL;
    BYTE* pResult=NULL;
    UINT32 iTotalLength =0;
	UINT32 iNameLen =0;
	UINT32 iValueLen =0;
    ELEMENTTYPE* pPostField = NULL;
    POSTSTRINGPART* pListStart=NULL;
    POSTSTRINGPART* pNewStringPart=NULL;
    POSTSTRINGPART* pPrevStringPart=NULL;

    pPostField = SDL_GetElementContent( pGoElement );

	if (!pPostField)
	{
	   *ppString = NULL;
	   *piDataLen =0; 
	   return;
	}

	while (pPostField)
	{
	  if (pPostField->iType == Type_POSTFIELD)
      {
        pNewStringPart=NEWSTRUCT(POSTSTRINGPART);
	    if (pNewStringPart)
        {
#ifdef USE_KSC5601
			WCHAR   *pWCHAR=NULL;
			INT16 iBuffLength=0;
			BYTE* pResultBuffer=NULL;
#endif
           if (!pListStart)
             pListStart=pNewStringPart;

           pNewStringPart->pData=NULL;
		   
		   /* evaluate name and variable */
           pWName = SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pName); 	

#ifdef USE_KSC5601
           pWCHAR = SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pValue);

           iBuffLength = KSCStrLenOfUni(pWCHAR);
		   if (iBuffLength)
		   {
	          pResultBuffer = NEWARRAY(BYTE,iBuffLength+1);
		      iBuffLength = Uni2KSCString(pWCHAR, pResultBuffer);
		   }
           OSConnectorFree(pWCHAR);
		   pWValue = NULL;
#else
           pWValue = SDL_GetText(pUserAgent, ((POSTFIELDELEMENT*)(pPostField))->pValue);
#endif

           /* transcode according to characterencoding  */
           TransCodeData(iCharEnc, pWName, pWValue, (BYTE**)&pNameData, (BYTE**)&pValueData);
#ifdef USE_KSC5601
			pValueData = (void*)pResultBuffer;
			pResultBuffer = NULL;
#endif
		   DEALLOC(&pWName);
		   DEALLOC(&pWValue);
		   

           /* ------------------ escape result ---------------------- */

		   if (iCharEnc==IANA_CHARSET_UCS2)
		   {
             pResStr1 = b_UnicodeEscape((WCHAR*)pNameData);
             pResStr2 = b_UnicodeEscape((WCHAR*)pValueData);		   
		   }
		   else
		   {
               pResStr1 = b_EscapeString((const BYTE *)pNameData);
               pResStr2 = b_EscapeString((const BYTE *)pValueData);
           }

		   DEALLOC(&pNameData);
		   DEALLOC(&pValueData);

		   if (pResStr1)
			  iNameLen = B_STRINGLENGTH((const char *)pResStr1);
		   if (pResStr2)
			  iValueLen = B_STRINGLENGTH((const char *)pResStr2);

		   /* concatenate the strings ignore if variable name is empty*/
		   
		   if (pResStr1)
           {
		      pResult = NEWARRAY(BYTE,iNameLen+iValueLen+2);
			  if (pResult)
              {
				  B_COPYSTRING((CHAR*)pResult,(CHAR*)pResStr1);
                  B_COPYSTRING((CHAR*)(&pResult[iNameLen]),"=");
				  if (pResStr2)
				     B_COPYSTRING((CHAR*)(&pResult[iNameLen+1]),(CHAR*)pResStr2);
			  }
           }

   		   DEALLOC(&pResStr1);
		   DEALLOC(&pResStr2);
		   
		   /* put result in linked list */ 
           
		   pNewStringPart->pData=pResult;
		   if (pPrevStringPart)
              pPrevStringPart->pNextField=pNewStringPart;
		   pNewStringPart->pNextField=NULL;
		   pPrevStringPart = pNewStringPart;
		   
		   iTotalLength = iTotalLength+iNameLen+iValueLen+2; /*eg testname=testvalue */
		}
	  }
      pPostField = WML_GetNextElement( pPostField);
	}

/* ------------------ assemble the list ----------------------- */

	pResult=NULL;
    pNewStringPart=pListStart;
    if (iTotalLength)
	{
	  pResult = NEWARRAY(BYTE,iTotalLength);
      iTotalLength=0;
	  if (pResult)
        while (pNewStringPart)
		{
          if (pNewStringPart->pData)
		  {
  	        B_COPYSTRING((CHAR*)(&pResult[iTotalLength]),(CHAR*)pNewStringPart->pData);
		    iTotalLength = iTotalLength + B_STRINGLENGTH(pNewStringPart->pData);
		    DEALLOC(&pNewStringPart->pData);
		  }
		  pPrevStringPart=pNewStringPart;
		  pNewStringPart=pNewStringPart->pNextField;
		  DEALLOC(&pPrevStringPart);
		  if (pNewStringPart)
          {
		     B_COPYSTRING((CHAR*)(&pResult[iTotalLength]),(CHAR*)"&");
		     iTotalLength++;
		  }
		}
	}

	*ppString = pResult;
	*piDataLen = (UINT16)iTotalLength;
}

/* used by postcontent */
void AddPostData(URLREQSTRUCT* pRequestStruct, int iSendMode, BYTE* pPostData, UINT32 iPostDataLen, BYTE* pContentType, BYTE* pContentDispositionFileName, UINT32 iTotalSize) 
{
	MULTIPARTSTR* pMultiPartHandle=NULL;
	BYTE* pTemp=NULL;

    pRequestStruct->iContentCharSet = IANA_CHARSET_INVALID;
	pRequestStruct->iMethod = M_Post;
	
	if (iSendMode == SENDMODE_MULTIPART_FORMDATA)
	{
		WAE_AddGenericFormDataPart (& pMultiPartHandle, & pPostData, (UINT16)iPostDataLen, pContentType, pContentDispositionFileName);
		if (!(WSP_BuildMultipart (& pMultiPartHandle, & pRequestStruct->pbBody, & pRequestStruct->iBodyLength, iTotalSize)))
		{
			/* Multipart creation failed. */

			pRequestStruct->pbBody = NULL;
			pRequestStruct->iBodyLength = 0;
		}
		pRequestStruct->iContentType=0x24;
		WSP_DeleteMultipartListWithContent (&pMultiPartHandle);
		DEALLOC( &pContentType );
		DEALLOC( &pPostData);
	}
	else if (iSendMode == SENDMODE_BINARY)
	{
		BYTE	bValue = NULL;

		pRequestStruct->iBodyLength = iPostDataLen;
		pRequestStruct->pbBody = pPostData;

		if( pContentType != NULL )
			if( WSP_ConvertContentStringByte( &bValue, &pContentType ) ){
				pRequestStruct->iContentType = bValue;
				DEALLOC(&pContentType);
			}
			else{
				pRequestStruct->iContentType = CONTENTTYPE_INVALID;
				pRequestStruct->pbContentType = pContentType;
			}
		else
			pRequestStruct->iContentType=0;
	}
	else	/* iSendMode == SENDMODE_URL_ENCODED */
	{
	   pTemp=EscapeALLString(pPostData, (UINT16)iPostDataLen);
       if (pTemp)
	   {
		   pRequestStruct->iBodyLength = B_STRINGLENGTH((const char*)pTemp)+1;
           pRequestStruct->pbBody = pTemp;
	   }
       pRequestStruct->iContentType=0x12;
	   DEALLOC( &pContentType );
	   DEALLOC( &pPostData);
	}

	DEALLOC( &pContentDispositionFileName );
}

void* AddMorePostData( void* vsPostData, int PostDataLen, int iSendMode )
{
   URLREQSTRUCT*	pURLStruct=NULL;
   BYTE*			pTemp = NULL;

   pURLStruct = New_URLREQSTRUCT();

   if (pURLStruct)
   {
		pURLStruct->iMethod = M_Post;

		if (iSendMode == SENDMODE_MULTIPART_FORMDATA || iSendMode == SENDMODE_BINARY)
		{
			pURLStruct->pbBody = (BYTE*)vsPostData;
			pURLStruct->iBodyLength = PostDataLen;
		}
		else	/* iSendMode == SENDMODE_URL_ENCODED */
		{
			pTemp=EscapeALLString( (BYTE*)vsPostData, (UINT16)PostDataLen);
			if (pTemp)
			{
				pURLStruct->iBodyLength = B_STRINGLENGTH((const char*)pTemp)+1;
				pURLStruct->pbBody = pTemp;
			}
			DEALLOC( &vsPostData);
		}
   }

  return pURLStruct;
}


/*-----------------------------------------------------------
Constructs a new requeststruct to be sent to wsp.
This function is to be used by common User Agents and the MMI agent, i.e when no specfic task is related to the request
-------------------------------------------------------------*/
/*
void* SDL_CreateRequestStruct( void* pWAEInObject, void* pDeprecated1, void* pDeprecated2, void* pbURL, void* pAccept)
*/
void* SDL_CreateRequestStruct( void* pbURL, void* pAccept)

{
   URLREQSTRUCT* pURLStruct=NULL;   

   pURLStruct = New_URLREQSTRUCT();

   if (pURLStruct)
   {
	  /* the URL that represents this request */
      pURLStruct->pbURL = (BYTE*)GenCopyString(1, pbURL);
       /* the content-types that are accepted by this request */
      pURLStruct->pbAccept= (BYTE*)GenCopyString(1,pAccept);

	  pURLStruct->iMethod = M_Get;   /*default method */

      #ifdef WAE_DEBUG
 	    PrintChar("New Request");
	    PrintChar((CHAR*)pURLStruct->pbURL);
	    PrintChar((CHAR*)pURLStruct->pbAccept);
	  #endif
   }

  return pURLStruct;
}

/*-----------------------------------------------------------
Constructs a new EXTENDED requeststruct to be sent to wsp.
This function is to be used by WML/WTA User Agents

⌨️ 快捷键说明

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