📄 xltdecwbxml.c
字号:
/*************************************************************************//* module: WBXML decoder *//* file: XLTDecWbxml.c *//* target system: all *//* target OS: all *//*************************************************************************//* * Copyright Notice * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc., * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001). * All Rights Reserved. * Implementation of all or part of any Specification may require * licenses under third party intellectual property rights, * including without limitation, patent rights (such a third party * may or may not be a Supporter). The Sponsors of the Specification * are not responsible and shall not be held responsible in any * manner for identifying or failing to identify any or all such * third party intellectual property rights. * * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM, * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA, * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO., * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL, * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE. * * The above notice and this paragraph must be included on all copies * of this document that are made. * *//** * The WBXML scanner/tokenizer. Used by the SyncML parser. */ #include "syncml_tk_prefix_file.h" // %%% luz: needed for precompiled headers in eVC++#include <define.h>#ifdef __SML_WBXML__/*************************************************************************//* Definitions *//*************************************************************************/#include "xltdevinf.h"#include "xltdeccom.h"#include "xlttags.h"#include "xltutilstack.h"#include "xltdec.h"#include <smldtd.h>#include <smldevinfdtd.h>#include <smlmetinfdtd.h>#include "mgrutil.h"#include <libmem.h>#include <libstr.h>#include <smlerr.h>#ifdef IS_END /* to avoid redefinition of this macro */#undef IS_END#endifvoidsubdtdDecodeWbxml(XltDecoderPtr_t pDecoder,SmlPcdataPtr_t *ppPcdata);/* WBXML version that this parser knows */#define _MAJOR_VERSION 1#define _MINOR_VERSION 2#define TAG_STATE 0#define ATTRIBUTE_STATE 1/* various checks about wbxml token */#define HAS_ATTRIBUTES(tag) (*tag & 0x80)#define HAS_CONTENT(tag) (*tag & 0x40)#define IDENTITY(tag) (*tag & 0x3F)#define IS_SWITCH(tok) (*(tok) == 0x00)#define IS_END(tok) (*(tok) == 0x01)#define IS_ENTITY(tok) (*(tok) == 0x02)#define IS_STR_I(tok) (*(tok) == 0x03)#define IS_LITERAL(tok) (IDENTITY(tok) == 0x04)// Note: gcc cannot parse multi-line macros when file has DOS line ends#define IS_EXT_I(tok) ((*(tok) == 0x40) || (*(tok) == 0x41) || (*(tok) == 0x42))#define IS_PI(tok) (*(tok) == 0x43)#define IS_EXT_T(tok) ((*(tok) == 0x80) || (*(tok) == 0x81) || (*(tok) == 0x82))#define IS_STR_T(tok) (*(tok) == 0x83)#define IS_EXT(tok) ((*(tok) == 0xC0) || (*(tok) == 0xC1) || (*(tok) == 0xC2))#define IS_OPAQUE(tok) (*(tok) == 0xC3)#define IS_STRING(tok) (IS_STR_I(tok) || IS_STR_T(tok))#define IS_EXTENSION(tok) (IS_EXT_I(tok) || IS_EXT_T(tok) || IS_EXT(tok))#define IS_ATTRIBUTE_VALUE(tok) (*(tok) & 0x80)#define IS_ATTRIBUTE_START(tok) (~IS_ATTRIBUTE_VALUE(tok))/** * Private Interface for the WBXML scanner. * * The private scanner interface contains some additional member attributes * that are not listed in the public interface, e.g. a copy of the string * table and some other items that do not need to be known outside the * scanner module. */typedef struct wbxmlScannerPriv_s wbxmlScannerPriv_t, *wbxmlScannerPrivPtr_t;struct wbxmlScannerPriv_s{ /* public methods */ Ret_t (*nextTok)(XltDecScannerPtr_t); Ret_t (*destroy)(XltDecScannerPtr_t); Ret_t (*pushTok)(XltDecScannerPtr_t); void (*setBuf)(XltDecScannerPtr_t pScanner, const MemPtr_t pBufStart, const MemPtr_t pBufEnd); MemPtr_t (*getPos)(XltDecScannerPtr_t pScanner); /* public attributes */ XltDecTokenPtr_t curtok; /* current token */ Long_t charset; /* character set as specified in the WBXML header */ String_t charsetStr; /* NULL */ Long_t pubID; /* document public identifier as specified in the WBXML header */ String_t pubIDStr; /* pubID as a string - valid only when pubID == 0 */ Flag_t finished; /* set when end of buffer is reached */ /* private attributes */ MemPtr_t pos; /* current buffer position */ MemPtr_t bufend; /* end of buffer */ Long_t pubIDIdx; /* strtbl index of the string version of the pubID - valid only when pubID == 0 */ XltUtilStackPtr_t tagstack; /* stack of open start tags */ MemPtr_t strtbl; /* copy of the string table */ Long_t strtbllen; /* length of the string table */ Byte_t state; /* tag state or attribute state */ SmlPcdataExtension_t cptag; /* current codepage for tags */ Byte_t cpattr; /* current codepage for attributes */ SmlPcdataExtension_t activeExt; /* the active Sub DTD */};/* typedef for multi-byte unsigned integers as specified in the WAP Binary XML Content Format specification */typedef Long_t MBINT;/** * Public methods of the scanner interface. * * Description see XLTDecCom.h. */static Ret_t _destroy(XltDecScannerPtr_t);static Ret_t _nextTok(XltDecScannerPtr_t);static Ret_t _pushTok(XltDecScannerPtr_t);static void _setBuf(XltDecScannerPtr_t, const MemPtr_t, const MemPtr_t); static MemPtr_t _getPos(XltDecScannerPtr_t); /** * FUNCTION: readBytes * * Advance the current position pointer after checking whether the end of * the buffer has been reached. If the end of the buffer has been reached * the scanner's finished flag is set. * RETURNS: 0, if end of buffer has been reached * 1 otherwise */static Boolean_t readBytes(wbxmlScannerPrivPtr_t pScanner, Long_t bytes);/** * FUNCTION: parseInt * * Decodes multi-byte integers. * * PRE-Condition: * pScanner->pos points to the first byte of the mb_int. * * POST-Condition: * pScanner->pos points to the last byte of the mb_int. */static Ret_t parseInt(wbxmlScannerPrivPtr_t pScanner, MBINT *mbi);/** * FUNCTION: wbxmlHeader, wbxmlVersion, wbxmlPublicID, wbxmlCharset * * These functions are used for decoding the WBXML document header. * wbxmlHeader is a short wrapper that calls the other four functions in * the right order to scan the header. wbxmlStrtbl makes a copy of the * string table. */static Ret_t wbxmlHeader(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlVersion(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlPublicID(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlCharset(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlStrtbl(wbxmlScannerPrivPtr_t pScanner);/** * FUNCTION: wbxmlSwitchPage * * Switch WBXML code page */static Ret_t wbxmlSwitchPage(wbxmlScannerPrivPtr_t pScanner);/** * FUNCTION: wbxmlXXXToken * * Scan the document for the next valid XML/WBXML token as defined in the * XLTDecCom header file (e.g. TOK_TAG_START). * * PRE-Condition: * pScanner->pos points to the first byte of a valid WBXML * element (String, Tag, etc.) * * POST-Condition: * pScanner->pos points to the last byte of the WBXML * element; * pScanner->curtok contains type and tagid or pcdata of * the token */static Ret_t wbxmlStringToken(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlOpaqueToken(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlTagToken(wbxmlScannerPrivPtr_t pScanner);/** * FUNCTION: wbxmlXXXToken * * WBXML extensions, entities, processing instructions and attributes are * not supported by this scanner. If one is found it is skipped and * processing continues afterwards. */static Ret_t wbxmlSkipExtension(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlSkipEntity(wbxmlScannerPrivPtr_t pScanner);static Ret_t wbxmlSkipPI(wbxmlScannerPrivPtr_t);static Ret_t wbxmlSkipAttribute(wbxmlScannerPrivPtr_t);/*************************************************************************//* External Functions *//*************************************************************************//** * FUNCTION: XltDecWbxmlInit * * Create and initialize a new WBXML scanner. Description see XLTDec.h. */Ret_txltDecWbxmlInit(const MemPtr_t pBufEnd, MemPtr_t *ppBufPos, XltDecScannerPtr_t *ppScanner){ wbxmlScannerPrivPtr_t pScanner; Ret_t rc; /* initialize new WBXML scanner */ if ((pScanner = (wbxmlScannerPrivPtr_t)smlLibMalloc(sizeof(wbxmlScannerPriv_t))) == NULL) return SML_ERR_NOT_ENOUGH_SPACE; smlLibMemset(pScanner, 0, sizeof(wbxmlScannerPriv_t)); pScanner->bufend = pBufEnd; pScanner->pos = *ppBufPos; if ((pScanner->curtok = (XltDecTokenPtr_t)smlLibMalloc(sizeof(XltDecToken_t))) == NULL) { smlLibFree(pScanner); *ppScanner = NULL; return SML_ERR_NOT_ENOUGH_SPACE; } pScanner->curtok->pcdata = NULL; if ((rc = xltUtilCreateStack(&pScanner->tagstack, 10)) != SML_ERR_OK) { smlLibFree(pScanner->curtok); smlLibFree(pScanner); *ppScanner = NULL; return rc; } pScanner->state = TAG_STATE; /* point public/private methods to the right implementation */ pScanner->nextTok = _nextTok; pScanner->destroy = _destroy; pScanner->pushTok = _pushTok; pScanner->setBuf = _setBuf; pScanner->getPos = _getPos; /* decode WBXML header */ if ((rc = wbxmlHeader(pScanner)) != SML_ERR_OK) { pScanner->destroy((XltDecScannerPtr_t)pScanner); *ppScanner = NULL; return rc; } *ppScanner = (XltDecScannerPtr_t)pScanner; return SML_ERR_OK;}/** * FUNCTION: destroy * * Free memory. Description see XltDecAll.h. */static Ret_t_destroy(XltDecScannerPtr_t pScanner){ wbxmlScannerPrivPtr_t pScannerPriv; if (pScanner == NULL) return SML_ERR_OK; pScannerPriv = (wbxmlScannerPrivPtr_t)pScanner; if (pScannerPriv->tagstack != NULL) pScannerPriv->tagstack->destroy(pScannerPriv->tagstack); smlLibFree(pScannerPriv->curtok); smlLibFree(pScannerPriv->strtbl); smlLibFree(pScannerPriv); return SML_ERR_OK;}/** * FUNCTION: nextTok * * Get next token. */static Ret_t_nextTok(XltDecScannerPtr_t pScanner){ wbxmlScannerPrivPtr_t pScannerPriv; Ret_t rc; pScannerPriv = (wbxmlScannerPrivPtr_t)pScanner; // T.K.: chanched Ptr_t to _t smlLibMemset(pScanner->curtok, 0, sizeof(XltDecToken_t)); pScannerPriv->curtok->start = pScannerPriv->pos; /* keep going until we find a "supported" element */ rc = SML_ERR_OK; while (rc == SML_ERR_OK) { /* skip PIs, extensions and entities... */ if (IS_PI(pScannerPriv->pos)) { rc = wbxmlSkipPI(pScannerPriv); } else if (IS_EXTENSION(pScannerPriv->pos)) { rc = wbxmlSkipExtension(pScannerPriv); } else if (IS_ENTITY(pScannerPriv->pos)) { rc = wbxmlSkipEntity(pScannerPriv); /* ... decode strings, opaque data and tags */ } else if (IS_STRING(pScannerPriv->pos)) { rc = wbxmlStringToken(pScannerPriv); break; } else if (IS_OPAQUE(pScannerPriv->pos)) { rc = wbxmlOpaqueToken(pScannerPriv); break; } else { rc = wbxmlTagToken(pScannerPriv); break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -