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

📄 xltdecwbxml.c

📁 SyncML ToolKits,学习syncml的参考工具包.非常好用.
💻 C
📖 第 1 页 / 共 3 页
字号:
    XltTagID_t tagid;    Boolean_t has_cont, has_attr;    Ret_t rc;    if (IS_SWITCH(pScanner->pos)) {        if ((rc = wbxmlSwitchPage(pScanner)) != SML_ERR_OK)            return rc;    }    /* we have to look at the top of the tagstack to see which       start tag an end tag belongs to */    if (IS_END(pScanner->pos)) {        if (!readBytes(pScanner, 1))            return SML_ERR_XLT_END_OF_BUFFER;        pScanner->curtok->type = TOK_TAG_END;        rc = pScanner->tagstack->pop(pScanner->tagstack, &tagid);        if (rc == SML_ERR_WRONG_USAGE)            return SML_ERR_XLT_INVAL_WBXML_DOC;        else if (rc)            return rc;        pScanner->curtok->tagid = tagid;        return SML_ERR_OK;    }     /* look at the two MSB: does this tag have content or attributes? */         has_cont = ((Boolean_t)(HAS_CONTENT(pScanner->pos)));     has_attr = ((Boolean_t)(HAS_ATTRIBUTES(pScanner->pos)));    /* look up tag ID either by string or by number */    if (IS_LITERAL(pScanner->pos)) {        MBINT offset; /* offset into the string table */        if (!readBytes(pScanner, 1))            return SML_ERR_XLT_END_OF_BUFFER;        if ((rc = parseInt(pScanner, &offset)) != SML_ERR_OK)            return rc;        if (offset > pScanner->strtbllen)            return SML_ERR_XLT_INVAL_WBXML_DOC;        rc = (Ret_t)getTagIDByStringAndExt((String_t)(pScanner->strtbl + offset), pScanner->activeExt, &tagid);        if ((tagid == TN_UNDEF) || (rc != SML_ERR_OK)) return rc;    } else {        rc = (Ret_t)getTagIDByByteAndExt((Byte_t)IDENTITY(pScanner->pos), pScanner->activeExt, &tagid);        if ((tagid == TN_UNDEF) || (rc != SML_ERR_OK)) return rc;    }    /* we know everything we need to know */    pScanner->curtok->tagid = tagid;    pScanner->curtok->type = has_cont ? TOK_TAG_START : TOK_TAG_EMPTY;#ifdef __USE_METINF__        pScanner->curtok->ext = pScanner->cptag == 0 ? SML_EXT_UNDEFINED : SML_EXT_METINF;#else	pScanner->curtok->ext = SML_EXT_UNDEFINED;#endif    if (!readBytes(pScanner, 1))        return SML_ERR_XLT_END_OF_BUFFER;    /* push tag onto tagstack unless this tag is empty */    if (has_cont) {        if ((rc = pScanner->tagstack->push(pScanner->tagstack, tagid)) != SML_ERR_OK)            return rc;    }    /* skip attributes */    if (has_attr) {        pScanner->state = ATTRIBUTE_STATE;        if ((rc = wbxmlSkipAttribute(pScanner)) != SML_ERR_OK)            return rc;        pScanner->state = TAG_STATE;    }    return SML_ERR_OK;}/** * FUNCTION: wbxmlSwitchPage * * Switch WBXML code page. *//* T.K. 06.02.01 * We need to enhance this as soon as we introduce  * Sub DTD's with more than one WBXML codepage. But till then * there is only one case where WBXML codepages can occure, and  * this is the MetInf Sub DTD. So in case we find a codepage switch * to something other than codepage zero, we set the active extension  * to metinf. * In future versions the pScanner needs to be enhanced, to translate * codepageswitches context sensitive to the active extension. */static Ret_twbxmlSwitchPage(wbxmlScannerPrivPtr_t pScanner){    if (!readBytes(pScanner, 1))        return SML_ERR_XLT_END_OF_BUFFER;    if (pScanner->state == TAG_STATE)        pScanner->cptag = (SmlPcdataExtension_t)*pScanner->pos;    else         pScanner->cpattr = *pScanner->pos;    readBytes(pScanner, 1);    /* T.K. this needs to be adjusted as described above */    if (pScanner->cpattr != 0 || pScanner->cptag != 0)        pScanner->activeExt = SML_EXT_METINF;    else        pScanner->activeExt = SML_EXT_UNDEFINED;    return SML_ERR_OK;}/******************************//* Unsupported WBXML elements *//******************************//** * FUNCTION: wbxmlSkipEntity * * Skips entities but doesn't do anything useful yet. */static Ret_twbxmlSkipEntity(wbxmlScannerPrivPtr_t pScanner){    MBINT tmp;    Ret_t rc;    if (!readBytes(pScanner, 1))        return SML_ERR_XLT_END_OF_BUFFER;    if ((rc = parseInt(pScanner, &tmp)) != SML_ERR_OK)        return rc;    if (!readBytes(pScanner, 1))        return SML_ERR_XLT_END_OF_BUFFER;    return SML_ERR_OK;}/** * FUNCTION: wbxmlSkipExtension * * Decode WBXML extensions. Skips the extension but doesn't do anything * useful with it. */static Ret_twbxmlSkipExtension(wbxmlScannerPrivPtr_t pScanner){    MBINT tmp;    Ret_t rc;    if (IS_EXT(pScanner->pos)) {        /* single byte extension token */        if (!readBytes(pScanner, 1))          return SML_ERR_XLT_END_OF_BUFFER;    } else if (IS_EXT_I(pScanner->pos)) {        /* inline string extension token */        if (!readBytes(pScanner, 1))            return SML_ERR_XLT_END_OF_BUFFER;        if (!readBytes(pScanner, smlLibStrlen((String_t)pScanner->pos) + 1))            return SML_ERR_XLT_END_OF_BUFFER;    } else {        /* inline integer extension token */        if (!readBytes(pScanner, 1))            return SML_ERR_XLT_END_OF_BUFFER;        if ((rc = parseInt(pScanner, &tmp)) != SML_ERR_OK)            return rc;        if (!readBytes(pScanner, tmp + 1))            return SML_ERR_XLT_END_OF_BUFFER;    }    return SML_ERR_OK;}/** * FUNCTION: wbxmlSkipPI * * Handle XML processing instructions. PIs are not supported but the * scanner recognizes and skips over them. */static Ret_twbxmlSkipPI(wbxmlScannerPrivPtr_t pScanner){    /* PIs are just like tag attributes with a special PI token instead     * of the attribute start token */    return wbxmlSkipAttribute(pScanner);}/** * FUNCTION: wbxmlSkipAttribute * * Handle attributes. Attributes are not supported but the * scanner recognizes and skips over them. */static Ret_twbxmlSkipAttribute(wbxmlScannerPrivPtr_t pScanner){    XltDecTokenPtr_t oldtok;    MBINT tmp;    Ret_t rc = 0;    /* skipping attributes shouldn't change the current token so we       make a copy... */    if ((oldtok = (XltDecTokenPtr_t)smlLibMalloc(sizeof(XltDecToken_t))) == NULL)        return SML_ERR_NOT_ENOUGH_SPACE;    smlLibMemcpy(oldtok, pScanner->curtok, sizeof(XltDecToken_t));    /* ... skip until attribute end tag... */    while (!IS_END(pScanner->pos)) {        if (IS_STRING(pScanner->pos)) {            rc = wbxmlStringToken(pScanner);            /* avoid memory leak due to this ugly workaround of               skipping attributes */            smlLibFree(pScanner->curtok->pcdata);        } else if (IS_EXTENSION(pScanner->pos)) {            rc = wbxmlSkipExtension(pScanner);        } else if (IS_ENTITY(pScanner->pos)) {            rc = wbxmlSkipEntity(pScanner);        } else if (IS_OPAQUE(pScanner->pos)) {            rc = wbxmlOpaqueToken(pScanner);            /* avoid memory leak due to this ugly workaround of               skipping attributes */            smlLibFree(pScanner->curtok->pcdata);        } else if (IS_LITERAL(pScanner->pos)) {            if (!readBytes(pScanner, 1))                return SML_ERR_XLT_END_OF_BUFFER;            rc = parseInt(pScanner, &tmp);            if (!readBytes(pScanner, 1))                return SML_ERR_XLT_END_OF_BUFFER;        } else if (IS_SWITCH(pScanner->pos)) {            rc = wbxmlSwitchPage(pScanner);        } else {            if (!readBytes(pScanner, 1))                return SML_ERR_XLT_END_OF_BUFFER;        }        if (rc != SML_ERR_OK) {            smlLibFree(oldtok);            return rc;        }    }    /* ... then skip the end tag itself... */    readBytes(pScanner, 1);    /* ... and finaly restore our copy of curtok */    smlLibMemcpy(pScanner->curtok, oldtok, sizeof(XltDecToken_t));    smlLibFree(oldtok);    return SML_ERR_OK;}#ifdef __USE_EXTENSIONS__/* * This function tries to decode an inlined WBXML document inside * an PCDATA element. * In case of failing to decode it the PCDATA element isn't changed * at all. */ voidsubdtdDecodeWbxml(XltDecoderPtr_t pDecoder,SmlPcdataPtr_t *ppPcdata) {    Ret_t                 _err         = SML_ERR_OK;    MemPtr_t              pSubBuf      = NULL;    SmlPcdataPtr_t        pSubPcdata   = NULL;    XltDecoderPtr_t       pSubDecoder  = NULL;#ifdef __USE_DEVINF__     wbxmlScannerPrivPtr_t pScannerPriv = NULL;#endif       /* some sanity checks at first */           if (*ppPcdata == NULL) { 		if (pDecoder) /* use this rare case to remove warning */ 		{ 		} 		return; 	}    if ((*ppPcdata)->contentType != SML_PCDATA_OPAQUE) return;    // now create a sub buffer    pSubBuf = (MemPtr_t)smlLibMalloc((*ppPcdata)->length);    if (pSubBuf == NULL) return;    smlLibMemset(pSubBuf, 0x00, (*ppPcdata)->length);    smlLibMemmove(pSubBuf, (*ppPcdata)->content, (*ppPcdata)->length);        /* ok looks fine sofar - now lets decode the rest */    /* now lets create a decoder, but without parsing the SyncML     * start tags (because it's not there) and skip the XML     * part as we don't need it.     */    pSubDecoder = (XltDecoderPtr_t)smlLibMalloc(sizeof(XltDecoder_t));    if (pSubDecoder == NULL) {        smlLibFree(pSubBuf);        return;    }    pSubDecoder->finished = 0;    pSubDecoder->final    = 0;    pSubDecoder->scanner  = NULL;    if (xltUtilCreateStack(&pSubDecoder->tagstack, 10) != SML_ERR_OK) {        smlLibFree(pSubDecoder);        smlLibFree(pSubBuf);        return;    }    if (xltDecWbxmlInit(pSubBuf+(*ppPcdata)->length,&pSubBuf, &pSubDecoder->scanner) != SML_ERR_OK) {        xltDecTerminate(pSubDecoder);        smlLibFree(pSubBuf);        return;    }    pSubDecoder->charset    = pSubDecoder->scanner->charset;    pSubDecoder->charsetStr = NULL;    pSubPcdata = (SmlPcdataPtr_t)smlLibMalloc(sizeof(SmlPcdata_t));    if (pSubPcdata == NULL) {        xltDecTerminate(pSubDecoder);        smlLibFree(pSubPcdata);        smlLibFree(pSubBuf);        return;    }    /* T.K.     * In the future we need to check the WBXML stringtable and     * switch into the right Sub DTD. But sofar only DevInf is     * supported so we can save time and space     */    /* T.K.     * To prevent buffer corruption when __USE_DEVINF__ is not used     * we initialize _err with any errorcode != OK, and this way     * force the function to exit without modifying the ppPcdata     */    _err = SML_ERR_UNSPECIFIC;#ifdef __USE_DEVINF__        pSubPcdata->contentType   = SML_PCDATA_EXTENSION;    pSubPcdata->extension     = SML_EXT_DEVINF;    pSubPcdata->length        = 0;    pSubPcdata->content       = NULL;    pScannerPriv = (wbxmlScannerPrivPtr_t)pSubDecoder->scanner;    pScannerPriv->activeExt  = SML_EXT_DEVINF;    pScannerPriv->cpattr     = 0;    pScannerPriv->cptag      = (SmlPcdataExtension_t)0;    smlLibMemset(pScannerPriv->curtok, 0,sizeof(XltDecToken_t));     _err = buildDevInfDevInfCmd(pSubDecoder, (VoidPtr_t)&pSubPcdata->content);#endif    if (_err != SML_ERR_OK) {        xltDecTerminate(pSubDecoder);        smlLibFree(pSubPcdata);        smlLibFree(pSubBuf);        return;    }        /* parsing is done, now lets anchor it within the original PCDATA element */    smlFreePcdata(*ppPcdata);    *ppPcdata = pSubPcdata;    /* we are done */    xltDecTerminate(pSubDecoder);    smlLibFree(pSubBuf);    return;}    #endif#endif

⌨️ 快捷键说明

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