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

📄 hdrutil.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
   ;   Max-Age: 0x0E, Delta-seconds-value
   ;   Path:    0x0F, Text-string
   ;   Secure:    0x10, No-value
   ; (all from encoding version 1.3)
   Also see 8.4.1.2: "Well-known field values MUST be encoded using the
   compact binary formats defined by the header syntax ..."
*/
void WSP_GetSetCookie (pHEADERELEMENT pElement, BOOL *pfError,
                       BYTE **ppbName, BYTE **ppbValue,
                       BYTE **ppbDomain, BYTE **ppbPath,
                       UINT32 **ppuiMaxAge, UINT32 *puiVersion)
{
	BYTE*  pbTemp;
	BYTE*  pbValue;
   BYTE*  pbEnd;
   BOOL   bMaxAgeFound;
   UINT32 uiParamNr;

   *pfError = TRUE;
	if ((pElement==NULL) || (pElement->bCodePage!=1))
      return;

	pbValue=pElement->pbFieldValStart;
	pbEnd=pElement->pbFieldValEnd;

	/* Pass Value-length */
	if (!isWapValueLength(&pbValue,pbEnd))
      return;

	/* Check and get Cookie-version */
	pbTemp=pbValue;
	if (!isWapShortInteger(&pbValue,pbEnd))
      return;
	*puiVersion = WSP_GetShortInt(*pbTemp) >> 4;

	/* Check and get name */
	*ppbName = WSP_GetTextString(&pbValue,pbEnd,pfError);
   if (*pfError)
      return;

	/* Check and get value */
	*ppbValue = WSP_GetTextString(&pbValue,pbEnd,pfError);
   if (*pfError)
      return;

   /* Check and get params */
   *ppbDomain = NULL;
   *ppbPath = NULL;
   bMaxAgeFound = FALSE;
   while ((pbValue<pbEnd) && (!*pfError))
   {
      pbTemp=pbValue;
		if (isWapIntegerValue(&pbValue,pbEnd))
      {
			uiParamNr=WSP_GetInteger(&pbTemp,pbEnd,pfError);
         if (*pfError) break;

         /* If Well-known Comment */
			if (uiParamNr==0x0C)
			{
            /* pass it without retrieving the value */
            if (!isWapTextString(&pbValue,pbEnd))
               *pfError = TRUE;
			}

         /* If Well-known Domain */
			else if (uiParamNr==0x0D)
			{
            *ppbDomain=WSP_GetTextString(&pbValue,pbEnd,pfError);
			}

			/* Well-known Max-Age*/
			else if (uiParamNr==0x0E)
			{
	         pbTemp=pbValue;
            if (isWapIntegerValue(&pbValue,pbEnd))
            {
               **ppuiMaxAge=WSP_GetInteger(&pbTemp,pbEnd,pfError);
               bMaxAgeFound = TRUE;
            }
            else
               *pfError=TRUE;
			}

			/* Well-known Path */
			else if (uiParamNr==0x0F)
			{
            *ppbPath=WSP_GetTextString(&pbValue,pbEnd,pfError);
			}

			/* Well-known Secure */
			else if (uiParamNr==0x10)
			{
            /* pass it without retrieving the value */
            if (!isWapNoValue(&pbValue,pbEnd))
				   *pfError=TRUE;
			}

         else
         {
            *pfError=TRUE;
			}
      }
		else if (isWapTokenText(&pbValue,pbEnd))
		{
			/* Untyped-parameter */
			if (isWapUnTypedValue(&pbValue,pbEnd))
			{
				/* Not supported here */
			}
			else
			{
				/* Error */
				*pfError=TRUE;
			}
		}
		else
		{
			/* Error */
			*pfError=TRUE;
		}

   } /* end while */

   if (!bMaxAgeFound)
      *ppuiMaxAge = NULL;

	return;
}


/*========================================================================
	WSP_GetPushHeaderAppId
==========================================================================*/
BOOL WSP_GetPushHeaderAppId (pHEADERELEMENT pElement, BYTE** ppbURIHeaderAppId, int* piCodeHeaderAppId)
{
	BYTE* pbTemp=NULL;
	BYTE* pbValue=NULL;
	BOOL fError = FALSE;

	if (pElement!=NULL)
	{
		/* Check code page. */

		/* Just code page 1 */
		if (pElement->bCodePage==1)
		{ 
			pbValue=pElement->pbFieldValStart;
			pbTemp=pbValue;

			/* Check if integer value */
			if (isWapIntegerValue(&pbTemp,pElement->pbFieldValEnd))
			{
				*piCodeHeaderAppId = WSP_GetInteger (&pbValue,pElement->pbFieldValEnd,&fError);
			}
			else if (isWapTextString(&pbTemp,pElement->pbFieldValEnd))
			{
				*ppbURIHeaderAppId = WSP_GetTextString(&pbValue,pElement->pbFieldValEnd,&fError);
			}
			else
			{
				/* Error */
				fError = TRUE;
			}
		}
	}

	/* Return result, returns !fError so TRUE will be returned if ok  */
	return (!fError);
}



/*========================================================================
	WSP_GetMd5
==========================================================================*/
BYTE* WSP_GetMd5 (pHEADERELEMENT pElement, BOOL *pfError)
{
	BYTE *pbValue=NULL;

	if (pElement!=NULL)
	{
		/* Check code page. */

		/* Just code page 1 */
		if (pElement->bCodePage==1)
		{ 
			pbValue=pElement->pbFieldValStart;

			/* Check value size - MUST be 17 bytes */
			if ((pElement->pbFieldValEnd - pElement->pbFieldValStart) == 17 )

			/* Get length - only short length of 16 can be correct */
			if ( WSP_GetShortInt (*pbValue) == 16 )
			{
				pbValue++;

				*pfError=FALSE;
				return pbValue;
			}
		}
	}

	*pfError=TRUE;
	return NULL;
}


/* Returns the correct value in the Base64 alphabet */
BYTE WSP_GetBase64Value (WCHAR pwchChar)
{
	if ((pwchChar > 0x40) && (pwchChar < 0x5B))
	{
		/* A - Z */
		return (BYTE)(pwchChar - 0x41);
	}
	else if ((pwchChar > 0x60) && (pwchChar < 0x7B))
	{
		/* a - z */
		return (BYTE)(pwchChar - 0x47);
	}
	else if ((pwchChar > 0x2F) && (pwchChar < 0x3A))
	{
		/* 0 - 9 */
		return (BYTE)(pwchChar + 0x04);
	}
	else if (pwchChar == 0x2B)
	{
		/* + */
		return (BYTE)(0x3E);
	}
	else if (pwchChar == 0x2F)
	{
		/* / */
		return (BYTE)(0x3F);
	}
	else if (pwchChar == 0x3D)
	{
		/* = */
		return (BYTE)(0x40);
	}

	/* Not in alphabet - return FF */
	return (BYTE)(0xFF);
}


/*========================================================================
	WSP_ConvertBase64ToByte
==========================================================================*/
BYTE* WSP_ConvertBase64ToByte (WCHAR* pwchMd5, INT16* piNbrBytes)
{
	/* ASSERT: piNbrBytes!=NULL
	*/

	BYTE* pbResult=NULL;
	BYTE* pbTemp=NULL;
	INT16 iLength=-1;
	UINT8 iBitsFound=0;
	BYTE bThis=0;
	BYTE bPrev=0;
	BOOL fQuit=FALSE;

	if (pwchMd5!=NULL)
	{
		/* Calculate length */
		iLength=STRINGLENGTH(pwchMd5);
		iLength= (INT16) ((iLength*3)/4 + (iLength*3)%4);

		/* Allocate memory */
		pbResult=NEWARRAY(BYTE,iLength);
		pbTemp=pbResult;

		iLength=0;

		/* Convert */
		while ((*pwchMd5!=0) && (!fQuit))
		{
			/* Convert character to value */
			bThis=WSP_GetBase64Value (*pwchMd5);

			if (bThis < 0x40)
			{
				iBitsFound+=6;

				if (iBitsFound>=8)
				{
					iBitsFound-=8;

					/* Store one byte */
					*pbTemp++ = (BYTE)( (bPrev<<(6-iBitsFound)) | (bThis>>(iBitsFound)) );

					/* Add one to nbr of bytes */
					iLength++;
				}
			}
			else if (bThis == 0x40)
			{
				/* Padding found - quit */
				fQuit=TRUE;
			}

			/* Next char */
			bPrev=bThis;
			pwchMd5++;
		}
	}

	/* Return */
	*piNbrBytes=iLength;
	return pbResult;
}





/* ============ ADDRESS FUNCTIONS ==============
	Functions for handling address parsing.
   ============================================= */


BOOL WSP_ParseAddress( BYTE* pbData, INT32 iLength, pADDRESS* ppAddress )
{
	BYTE* pbTemp=pbData;
	BYTE* pbEnd=pbData+iLength;
	BYTE bFirst=0;
	BOOL fStreamOk=TRUE;
	pADDRESS pTempAddress=NULL;
	pADDRESS pLast=NULL;
	
	if (ppAddress==NULL)
	{
		return FALSE;
	}

	/* Delete old list (if any) */
	WSP_DeleteAddress( ppAddress );
	pLast=*ppAddress;

	/* Step through the byte stream - one address type at a time. */
	while ((pbTemp<pbEnd)&&(fStreamOk))
	{
		bFirst=*pbTemp;

		/* Create ADDRESS struct */
		pTempAddress=NEWSTRUCT(ADDRESS);

		if (pTempAddress!=NULL)
		{
			/* Set default values */
			pTempAddress->iBearerType=-1;
			pTempAddress->iPortNumber=-1;
			pTempAddress->pbAddress=NULL;
			pTempAddress->pNext=NULL;
			pTempAddress->uiAddressLength=0;
		}
		else
		{
			/* Out of memory */
			return FALSE;
		}

		/* Get address length (the six least significant bits) */
		pTempAddress->uiAddressLength=(BYTE) ((bFirst) & (0x3f));

		/* Get Bearer Type (if the most significant bit is set) */
		if ((bFirst)&(0x80))
		{
			/* Next byte */
			if (HdrNextByte(&pbTemp,pbEnd))
			{
				pTempAddress->iBearerType=*pbTemp;
			}
			else
			{
				fStreamOk=FALSE;
			}
		}

		/* Get Port Number (if the second most significant bit is set) */
		if ((bFirst)&(0x40))
		{
			if (HdrNextByte(&pbTemp,pbEnd))
			{
				UINT16 iA=*pbTemp;

				if (HdrNextByte(&pbTemp,pbEnd))
				{
					pTempAddress->iPortNumber=(iA<<8)|(*pbTemp);
				}
				else
				{
					fStreamOk=FALSE;
				}
			}
			else
			{
				fStreamOk=FALSE;
			}
		}
		
		if (pTempAddress->uiAddressLength>0)
		{
		
			if ( (pbTemp+(pTempAddress->uiAddressLength))<pbEnd ) 
			{
				/* Get the address */
				pTempAddress->pbAddress=pbTemp+1;

				/* Step past address */
				pbTemp+=(pTempAddress->uiAddressLength);
			}
			else
			{
				fStreamOk=FALSE;
			}
		}

		pbTemp++;

		/* Add address struct to list */
		if (pLast!=NULL)
		{
			pLast->pNext=pTempAddress;
		}
		else
		{
			/* Add to beginning of list */
			*ppAddress=pTempAddress;
		}

		pLast=pTempAddress;
	}

	if (!fStreamOk)
	{
		WSP_DeleteAddress( ppAddress );
		return FALSE;
	}

	return TRUE;
}


void WSP_DeleteAddress( pADDRESS* ppAddress )
{
	pADDRESS pTemp=NULL;

	if (ppAddress!=NULL)
	{

		while (*ppAddress!=NULL)
		{		
			pTemp=*ppAddress;
			*ppAddress=(*ppAddress)->pNext;
			DEALLOC(&pTemp);
		}		
	}
}


/* ============================================= */

#include "contypes.h"

/* Compare two strings and return the length of the common
   prefix of the two. */
static UINT32
CommonPrefix (BYTE *s, BYTE *t)
{
  UINT32 k = 0;

  while (*s && (*s++ == *t++))
    k++;

  return k;
}

/* Search a prefix tree pn for the string t.
   On successful search, return the index associated with the string,
   otherwise return -1. */
static INT8
SearchPrefixTree (const node_t *pn, BYTE *t)
{
  INT8 p = 0;
  node_t *q;

  while (p >= 0) {
    q = (node_t *)pn + p;
    if (*(q->s) != *t) {
      p = q->sibling;
    }
    else {
      if (CommonPrefix (q->s, t) < q->len) { break; }
      t += q->len;
      if (*t) {
				p = q->child;
      }
      else
				return q->idx;
    }
  }
  /* Not found */
  return -1;
}

/* Return a copy of the string with index idx.
   Return NULL if index is not in the table. */
static BYTE *
ReverseLookup (const node_t *pn, INT8 idx)
{
  INT32 i;

  if (idx < 0 || idx >= NUM_NODES)
    return NULL;

  for (i = idx; i < NUM_NODES; i++) {
    if (pn[i].idx == idx) {
      /* Compute length of string */
      INT32 j, l;
      BYTE *s, *t;

      for (l = 0, j = i; j >= 0; j = pn[j].parent) {
				l += pn[j].len;
      }
      s = NEWARRAY (BYTE, l + 1);
      t = s + l;
      *t = '\0';
      for (j = i; j >= 0; j = pn[j].parent) {
				t -= pn[j].len;
				B_COPYSTRINGN (t, pn[j].s, pn[j].len);
      }
      return s;
    }
  }
  return NULL;
}

BOOL WSP_ConvertContentStringByte (BYTE *pbValue, BYTE **ppbText)
{
	if (*ppbText != NULL)	{
		BYTE buf[128];
		INT8 k;

		/* Convert string to lowercase */
		DowncaseString (buf, *ppbText);
		
		/* Convert from text to byte */
		k = SearchPrefixTree (treeNodes, buf);

		if (k >= 0) {
			*pbValue = k;
			return TRUE;
		}
		return FALSE;
	}
	else {
		/* Convert from byte to text */
		*ppbText = ReverseLookup (treeNodes, *pbValue);
		return *ppbText != NULL;
	}
}

/*========================================================================
	WSP_PreParseMultipart
==========================================================================*/
pMULTIPARTSTR WSP_PreParseMultipart (BYTE *pbInstream, UINT32 iLength)
{
	pMULTIPARTSTR pMultiList=NULL;
	pMULTIPARTSTR pMultiTemp=NULL;
	pMULTIPARTSTR pMultiLast=NULL;
	BYTE* pbEnd=pbInstream+iLength;
	
	BOOL fError=FALSE;
	UINT32 iCount=0;
	UINT32 iNbrOfEntries=0;
	UINT32 iPos=0;

	UINT32 iLen=0;

	/* Check if within bounds */
	if (pbInstream<pbEnd)
	{
		/* Get number of entries */
		if (ReadMBUInt32(&iNbrOfEntries,pbInstream,(UINT32)(pbEnd-pbInstream-1),&iPos))
		{
			/* Increase pbInstream with the number of bytes indicated 
			   by the number of bytes for the MBUInt32. */
			pbInstream+=iPos;

			/* Split the multipart data */
			while ((iCount<iNbrOfEntries)&&(!fError))
			{
				/* Create MULTIPARTSTR */
				pMultiTemp=NEWSTRUCT(MULTIPARTSTR);
			
				if (pMultiTemp!=NULL)
				{
					pMultiTemp->pNext=NULL;
				
					/* Check if within bounds */
					if (pbInstream<pbEnd)
					{
						iPos=0;
	
						/* Get header length */
						if (ReadMBUInt32(&iLen,pbInstream,(UINT32)(pbEnd-pbInstream-1),&iPos))
						{
							pbInstream+=iPos;

							/* Store headerlen */
							pMultiTemp->iHdrLen=iLen;
						}
						else 
						{
							fError=TRUE;
						}
					}
					else 
					{
						fError=TRUE;
					}
	
					/* Check if within bounds */
					if ((pbInstream<pbEnd)&&(!fError))

⌨️ 快捷键说明

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