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

📄 wbxmldec.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 3 页
字号:
				#endif
		
				/* Copy */
				B_COPYSTRINGN (pbData,pDecStr->pbCurByte,iLength);

				/* Create opaque element */
				pAttrPart=XML_CreateOpaque (&pbData, iLength);

				#ifndef HAS_SETJMP
				}
				else
				{
					/* Out of memory */
					pDecStr->iDecodeResult|=WBXML_Error_DataStream;
				}
				#endif
			}
			else
			{
				/* Instream error */
				pDecStr->iDecodeResult|=WBXML_Error_DataStream;
			}

			/* Step past data */
			pDecStr->pbCurByte+=iLength;
		}

		/* ATTRVALUE ( value 128 or greater ) */
		else if (bToken>127)
		{
			UINT16 iAttrVal=(UINT16)((UINT16)bToken + (UINT16)(256 * pDecStr->iAttrCodePage));
			WCHAR *pwchString=NULL;

			pDecStr->pbCurByte++;

			/* Get application specific value */
			if (pDecStr->bContentType == '\x36')		/* Connectivity doc => ok with token values instead of strings */
			{
				pAttrPart = Prov_CreateTokenValueElem(iAttrVal);	/* Motsvarande XML_CreateInlineText fkn */
			}
			
			else
			{
				pwchString=pDecStr->DTD_GetAttributeValue (iAttrVal);

				/* Add to pAttrPart */
				if (pwchString!=NULL)
				{
					pAttrPart=XML_CreateInlineText (&pwchString);
				}
				else
				{
					/* Error in encoding - unknown attribute value */
					pDecStr->iDecodeResult|=WBXML_Warning_UnknownAttributeValue;	
				}
			}
		}
		
		else
		{
			/* End of attribute value - quit */
			fCont=FALSE;
		}

		/* Add attribute part to end of attribute value */
		XML_AddElementToEnd (&pAttrVal, pAttrPart);
	}

	return pAttrVal;
}


/*========================================================================
	WBXML_DecodeAttributes
==========================================================================*/
void WBXML_DecodeAttributes (pWBXMLDECODESTR pDecStr, pELEMENTTYPE pElement)
{
	BOOL fCont = TRUE;
	BYTE bToken = 0;
	UINT16 iAttribute = 0;
	pELEMENTTYPE pAttrVal = NULL;
	UINT32 iMBUINT = 0;

	/* Decode all attributes and the values */
	while ( (pDecStr->pbCurByte < pDecStr->pbEnd) && (fCont) && pDecStr->iDecodeResult < WBXML_FatalError )
	{
		bToken=*pDecStr->pbCurByte++;

		/* Get the correct attribute value */
		iAttribute = (UINT16) (bToken + (256 * pDecStr->iAttrCodePage));

		switch (bToken)
		{

		/* End */
		case GLOBAL_END:
			fCont = FALSE;		
			break;

		/* Switch page */
		case GLOBAL_SWITCH_PAGE:

			/* Switch Attribute Code Page */
			if ( pDecStr->pbCurByte < pDecStr->pbEnd )
			{
				pDecStr->iAttrCodePage = *pDecStr->pbCurByte;
				pDecStr->pbCurByte++;
			}
			break;

		/* Literal */
		case GLOBAL_LITERAL:
			
			/* Get mbuint32 */
			iMBUINT = WBXML_GetMBUINT32 (pDecStr);
			
			/* Get string from string table and call function 
			   to convert it to the correct token */
			iAttribute = pDecStr->DTD_LiteralAttributeToToken 
								(WBXML_GetStrTabVal (pDecStr, iMBUINT));
			pDecStr->timer += 1;
			/* No break - continue into default */

		/* Other */
		default:

			/* Get attribute value */
			pAttrVal = WBXML_DecodeAttributeValue(pDecStr);

			/* Store attribute value */
			if (!pDecStr->DTD_StoreAttributeValue (pDecStr, pElement, 
													iAttribute, &pAttrVal))
			{
				/* Illegal attribute - warning */
				pDecStr->iDecodeResult |= WBXML_Warning_IllegalAttribute;	
			}
			pDecStr->timer += 1;

			/* Delete attribute value (list of elements) */
			XML_DeleteElementList(&pAttrVal,pDecStr);

			break;
		}
	}
}



/*========================================================================
	WBXML_StepPastOnePI
==========================================================================*/
void WBXML_StepPastOnePI (pWBXMLDECODESTR decodeStruct)
{
	while (MORE_DATA && *decodeStruct->pbCurByte++ != GLOBAL_END)
		;
}



/*========================================================================
	WBXML_StepPastAllPIs
==========================================================================*/
void WBXML_StepPastAllPIs (pWBXMLDECODESTR decodeStruct)
{
	while (MORE_DATA && *decodeStruct->pbCurByte == GLOBAL_PI)
		WBXML_StepPastOnePI (decodeStruct);
}



/*========================================================================
	WBXML_InitDecode
==========================================================================*/
void WBXML_InitDecode (pWBXMLDECODESTR decodeStruct, BOOL scheduled)
{
	decodeStruct->state          = WBXML_STATE_0;
	decodeStruct->topElement     = NULL;
	decodeStruct->parentElement  = NULL;
	decodeStruct->currentElement = NULL;
	decodeStruct->deletePtr      = NULL;
	decodeStruct->scheduled      = scheduled;
}



/*========================================================================
	WBXML_AddElement
==========================================================================*/
void WBXML_AddElement(pWBXMLDECODESTR decodeStruct, pELEMENTTYPE newElement)
{
	if (newElement == NULL)
		return;

	if (decodeStruct->topElement == NULL)
		decodeStruct->topElement = newElement;
	else {
		if (decodeStruct->currentElement == decodeStruct->parentElement)
			((pCONTENTELEMENT) decodeStruct->currentElement)->pContent = newElement;
		else
			decodeStruct->currentElement->pNextElement = newElement;
	}
	while (newElement->pNextElement != NULL)
		newElement = newElement->pNextElement;
	decodeStruct->currentElement = newElement;
}



/*========================================================================
	WBXML_DeleteCurrentElement
==========================================================================*/
void WBXML_DeleteCurrentElement(pWBXMLDECODESTR decodeStruct)
{
	pELEMENTTYPE prevElement;
	pELEMENTTYPE tempElement;

	if (decodeStruct->parentElement == NULL) { /* Top level case */
		if (decodeStruct->currentElement == decodeStruct->topElement) {
			decodeStruct->topElement = decodeStruct->currentElement->pNextElement;
			prevElement = NULL;
		} else {
			tempElement = decodeStruct->topElement;
			goto traverse_list;
		}
	} else { /* Normal case */
		prevElement = decodeStruct->parentElement;
		if (((pCONTENTELEMENT) prevElement)->pContent == decodeStruct->currentElement)
			((pCONTENTELEMENT) prevElement)->pContent = decodeStruct->currentElement->pNextElement;
		else {
			tempElement = ((pCONTENTELEMENT) prevElement)->pContent;
traverse_list:
			while (tempElement != decodeStruct->currentElement) {
				prevElement = tempElement;
				tempElement = tempElement->pNextElement;
			}
			prevElement->pNextElement = decodeStruct->currentElement->pNextElement;
		}
	}

	XML_DeleteElement(&(decodeStruct->currentElement), decodeStruct);
	decodeStruct->currentElement = prevElement;
}




/*========================================================================
	WBXML_Decode
==========================================================================*/
pELEMENTTYPE WBXML_Decode (pWBXMLDECODESTR decodeStruct, BOOL *finished)
{	
	pELEMENTTYPE newElement = NULL;
	BYTE*        data;
	UINT32       length;
	UINT32       location;
	UINT16       tag;
	BYTE         token;

	decodeStruct->timer = 0;
	*finished = FALSE;

	while (!decodeStruct->scheduled || decodeStruct->timer < WBXML_TIMESLOT)
	switch (decodeStruct->state) {

	case WBXML_STATE_0: /*************************************************  Decode element */
		decodeStruct->timer += 1;

		/* Get tag */
		if (MORE_DATA) {
			token = *decodeStruct->pbCurByte++;

			if (token == GLOBAL_SWITCH_PAGE) /* --------------------------  SWITCH PAGE */ 
				if (MORE_DATA)
					/* Switch Tag Code Page */
					decodeStruct->iTagCodePage = *decodeStruct->pbCurByte++;
				else
					goto Decode_error;
			
			if ((token & MASK) < 4) { /* ---------------------------------  ILLEGAL */
				/* GLOBAL_SWITCH_PAGE, GLOBAL_END, GLOBAL_ENTITY,
				   GLOBAL_STR_I, GLOBAL_EXT_I_0, GLOBAL_EXT_I_1,
				   GLOBAL_EXT_I_2, GLOBAL_PI, GLOBAL_EXT_T_0,
				   GLOBAL_EXT_T_1, GLOBAL_EXT_T_2, GLOBAL_STR_T,
				   GLOBAL_EXT_0, GLOBAL_EXT_1, GLOBAL_EXT_2, GLOBAL_OPAQUE */
				decodeStruct->iDecodeResult |= WBXML_Error_Encoding;

			} else if ((token & MASK) == 4) { /* -------------------------  LITERAL */
				/* GLOBAL_LITERAL, GLOBAL_LITERAL_C,
				   GLOBAL_LITERAL_A, GLOBAL_LITERAL_AC */
				location = WBXML_GetMBUINT32(decodeStruct);
				tag = decodeStruct->DTD_LiteralTagToToken(WBXML_GetStrTabVal(decodeStruct, location));
				decodeStruct->timer += 1;

			} else /* ----------------------------------------------------  ELEMENT */
				tag = (UINT16) ((token & MASK) + (decodeStruct->iTagCodePage << 8));
		} else 
Decode_error: decodeStruct->iDecodeResult |= WBXML_Error_DataStream;	

		/* Decode element */
		if (decodeStruct->iDecodeResult <= WBXML_FatalError) /* No fatal errors encountered */
			newElement = decodeStruct->DTD_CreateElement(decodeStruct, tag);

		if (decodeStruct->iDecodeResult <= WBXML_FatalError) {
			WBXML_AddElement(decodeStruct, newElement);

			if (token & ATTR)
				WBXML_DecodeAttributes(decodeStruct, newElement);

			if (token & CONT) { /* Decode content (if any) */
				if (! decodeStruct->DTD_ValidContent (decodeStruct, newElement)) {
					/* An element contains content, even though it is not permitted.
					 * Its content is added to a dummy element, then deleted in WBXML_STATE_1.
					 * This goes for unknown elements as well. */
					newElement = (pELEMENTTYPE) NEWSTRUCT(CONTENTELEMENT);
					#ifndef HAS_SETJMP
					if (newElement == NULL) {
						/* Out of memory; restart system. */ 
						decodeStruct->iDecodeResult |= WBXML_Error_DataStream;
						return NULL;
					}
					#endif
					
					newElement->iType = Type_ContentElement;
					newElement->pNextElement = NULL;
					((pCONTENTELEMENT) newElement)->pContent = NULL;

					WBXML_AddElement(decodeStruct, newElement);
					if (decodeStruct->deletePtr == NULL)
						decodeStruct->deletePtr = newElement;
				}
				
				newElement->pNextElement = decodeStruct->parentElement;
				decodeStruct->parentElement = newElement;
				decodeStruct->state = WBXML_STATE_2; 
				break;
			}
		}

		decodeStruct->state = WBXML_STATE_1;
		break;

	
	case WBXML_STATE_1: /*************************************************  Finish element */
		decodeStruct->timer += 1;
		
		if (decodeStruct->deletePtr == decodeStruct->currentElement &&
		    decodeStruct->deletePtr != NULL) {
			WBXML_DeleteCurrentElement(decodeStruct);
			decodeStruct->deletePtr = NULL;
			decodeStruct->iDecodeResult |= WBXML_Warning_IllegalElement;
			decodeStruct->timer += 1;
		}	else 
			if (decodeStruct->currentElement != NULL) /* Application specific modifications */
				decodeStruct->DTD_AppSpecParseMod(decodeStruct);

		if (decodeStruct->parentElement == NULL) { /* Document done */
			*finished = TRUE;
			if (decodeStruct->iDecodeResult >= WBXML_FatalError && 
			    decodeStruct->topElement != NULL)
				/* Fatal error; delete entire document tree */
				WBXML_DeleteCurrentElement(decodeStruct);
			return decodeStruct->currentElement;
		}

		decodeStruct->state = WBXML_STATE_2;
		break;
		
	case WBXML_STATE_2: /**************************************************  Decode content */
		decodeStruct->timer += 1;
		
		if (!MORE_DATA || decodeStruct->iDecodeResult >= WBXML_FatalError) {
			/* Fatal error, abort decoding */
			decodeStruct->pbCurByte--; /* Compensate for an unwanted increment in END section */
			token = GLOBAL_END;
		} else
			token = *decodeStruct->pbCurByte;
			
		newElement = NULL;

		if (token == GLOBAL_END) { /* -------------------------------------  END */
			decodeStruct->pbCurByte++;
			decodeStruct->currentElement = decodeStruct->parentElement;
			decodeStruct->parentElement = decodeStruct->currentElement->pNextElement;
			decodeStruct->currentElement->pNextElement = NULL;
			decodeStruct->state = WBXML_STATE_1;
			break;

		} else if (WBXML_CheckIfGlobalText (token)) /* --------------------  GLOBAL TEXT */ 
			/* entity | string | extension */
			newElement=WBXML_DecodeGlobalText (decodeStruct);

		else if (token == GLOBAL_PI) /* -----------------------------------  PI */
			WBXML_StepPastOnePI(decodeStruct);
		
		else if (token == GLOBAL_OPAQUE) { /* -----------------------------  OPAQUE */
			decodeStruct->pbCurByte++;
			length = WBXML_GetMBUINT32(decodeStruct);

			/* Copy data */
			if (decodeStruct->pbCurByte+length < decodeStruct->pbEnd) {
				data = NEWARRAY (BYTE, length);

				#ifndef HAS_SETJMP
				if (data == NULL) {
					/* Out of memory; restart system. */ 
					decodeStruct->iDecodeResult |= WBXML_Error_DataStream;
					return NULL;
				}
				#endif

				B_COPYSTRINGN(data, decodeStruct->pbCurByte, length);
				newElement = XML_CreateOpaque(&data, length);
			} else
				decodeStruct->iDecodeResult |= WBXML_Error_DataStream;

			decodeStruct->pbCurByte += length;

		} else { /* --------------------------------------------------------  ELEMENT */
			decodeStruct->state = WBXML_STATE_0; 
			break;
		}

		WBXML_AddElement(decodeStruct, newElement);

		decodeStruct->state = WBXML_STATE_2;
		break;
	}

	return NULL;
}



/*========================================================================
==========================================================================*/
/* Set decode charset */
void WBXML_SetDecodeCharset (pWBXMLDECODESTR pDecStr, INT16 iCharset)
{
	/* Set charset : -1 indicates unknown charset. If the charset is
	   already set (e.g., WSP-level) and differ from the value
	   specified in the content, the definition in the content will 
	   be used. If none is defined, UTF-8 will be set as default.	
	*/
	if (iCharset!=IANA_CHARSET_INVALID)
	{
		pDecStr->iCharset=iCharset;
		return;
	}

	/* Check if charset already defined */
	if (pDecStr->iCharset!=IANA_CHARSET_INVALID)
	{
		/* Keep pre-defined charset */

⌨️ 快捷键说明

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