Home | History | Annotate | Download | only in src
      1 /*************************************************************************/
      2 /* module:          The WBXML Encoder source file                        */
      3 /* file:            xltencwbxml.c                                        */
      4 /* target system:   All                                                  */
      5 /* target OS:       All                                                  */
      6 /*************************************************************************/
      7 
      8 /*
      9  * Copyright Notice
     10  * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication
     11  * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc.,
     12  * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001).
     13  * All Rights Reserved.
     14  * Implementation of all or part of any Specification may require
     15  * licenses under third party intellectual property rights,
     16  * including without limitation, patent rights (such a third party
     17  * may or may not be a Supporter). The Sponsors of the Specification
     18  * are not responsible and shall not be held responsible in any
     19  * manner for identifying or failing to identify any or all such
     20  * third party intellectual property rights.
     21  *
     22  * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED
     23  * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM,
     24  * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA,
     25  * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML
     26  * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
     27  * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
     28  * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
     29  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     30  * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO.,
     31  * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY
     32  * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF
     33  * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF
     34  * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL,
     35  * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH
     36  * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED
     37  * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
     38  *
     39  * The above notice and this paragraph must be included on all copies
     40  * of this document that are made.
     41  *
     42  */
     43 
     44 
     45 #include <define.h>
     46 #ifdef __SML_WBXML__
     47 /*************************************************************************/
     48 /*  Definitions                                                          */
     49 /*************************************************************************/
     50 
     51 #include "xltencwbxml.h"
     52 #include <libmem.h>
     53 #include <libstr.h>
     54 #include "xlttags.h"
     55 
     56 
     57 /**
     58  * FUNCTION: wbxmlGetGlobToken
     59  *
     60  * Converts a element type into its wbxml token
     61  *
     62  * PRE-Condition:   valid element type
     63  *
     64  * POST-Condition:  return of wbxml token
     65  *
     66  * IN:              elType, element type
     67  *
     68  * OUT:             wbxml token
     69  *
     70  * RETURN:          wbxml token
     71  *                  0, if no matching wbxml token
     72  */
     73 MemByte_t wbxmlGetGlobToken(XltElementType_t elType)
     74 {
     75 
     76   typedef struct GlobTok_s
     77   {
     78     XltElementType_t id;
     79     MemByte_t        wbxml;
     80   } GlobTok_t;
     81 
     82   // encoding of global tokens; related to the type XML_ElementType_t
     83   GlobTok_t globtoken[] =
     84   {
     85     { END,    0x01 },  //Tag End
     86     { STR_I,  0x03 },  //Inline string
     87     { OPAQUE, 0xC3 },  //Opaque Data
     88     { UNDEF,  0x00 }
     89   };
     90 
     91   int i = -1;
     92   while (globtoken[++i].id != UNDEF)
     93     if (globtoken[i].id == elType)
     94       return globtoken[i].wbxml;
     95   return 0;
     96 
     97 }
     98 
     99 /**
    100  * FUNCTION: wbxmlOpaqueSize2Buf
    101  *
    102  * Converts a Long_t opaque size to a wbxml mb_u_int32 and adds it to the buffer
    103  *
    104  * PRE-Condition:   size of the content to be written as opaque datatype
    105  *
    106  * POST-Condition:  the size is converted to the mb_u_int32 representation and added
    107  *                  to the buffer
    108  *
    109  * IN:              size, length of the opaque data
    110  *
    111  * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
    112  *
    113  * RETURN:          shows error codes of function,
    114  *                  0, if OK
    115  */
    116 Ret_t wbxmlOpaqueSize2Buf(Long_t size, BufferMgmtPtr_t pBufMgr)
    117 {
    118   Long_t _thresholdcount = 1;
    119   Long_t _bytesNeeded = 0;
    120   MemPtr_t _byteArray;
    121   MemPtr_t _tmpByteArray;
    122   int i, j;
    123   Ret_t _err = SML_ERR_OK;
    124 
    125   //j max = number of bytes of size
    126    for (j=1; j<=sizeof(size); j++)
    127   {
    128     //if the size of the content is smaller than the power of 128,j ->
    129     //one more byte is needed in the mb_u_int32 representation of WBXML
    130      _thresholdcount = _thresholdcount * 128;
    131      if(size < _thresholdcount)
    132      {
    133        _bytesNeeded = j;
    134        break;
    135      }
    136   }
    137 
    138   if (pBufMgr->spaceEvaluation == 0)
    139   {
    140     //allocate number of bytes needed by the mb_u_int32 data type
    141     if ((_byteArray = smlLibMalloc(_bytesNeeded)) == NULL) return SML_ERR_NOT_ENOUGH_SPACE;
    142 
    143     _tmpByteArray = _byteArray;
    144 
    145     //process al bytes in the mb_u_int32 data type
    146     for (i=1; i<=_bytesNeeded; i++)
    147     {
    148       // the lowest byte needs a 0 in its highest bit -> no | 0x80
    149       if ((_bytesNeeded - i) == 0)
    150       {
    151           *_tmpByteArray = ((unsigned char)(size & 0x7F));
    152       }
    153       // all the other byte needs a 1 in its highest bit -> | 0x80
    154       else
    155       {
    156         // only the seven lower bits contain the size value -> >> 7
    157           *_tmpByteArray = ((unsigned char)(((size >> (7 * (_bytesNeeded - i))) & 0x7F) | 0x80));
    158         _tmpByteArray++;
    159       }
    160     }
    161 
    162     _err = xltAddToBuffer(_byteArray, _bytesNeeded, pBufMgr);
    163 
    164     smlLibFree(_byteArray);
    165   } else {
    166     pBufMgr->smlXltWrittenBytes += _bytesNeeded;
    167     // %%% luz 2002-09-03: return value was missing here.
    168     _err=SML_ERR_OK;
    169   }
    170 
    171   return _err;
    172 }
    173 
    174 /**
    175  * FUNCTION: wbxmlGenerateTag
    176  *
    177  * Generates a tag for a given tag ID and a given tag type
    178  *
    179  * PRE-Condition:   valid parameters
    180  *
    181  * POST-Condition:  a new wbxml tag is written to the buffer
    182  *
    183  * IN:              tagId, the ID for the tag to generate (TN_ADD, ...)
    184  *                  tagType, the tag type (e.g. Begin Tag -> TT_BEG, ...)
    185  *
    186  * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
    187  *
    188  * RETURN:          shows error codes of function,
    189  *                  0, if OK
    190  */
    191 Ret_t wbxmlGenerateTag(XltTagID_t tagId, XltTagType_t tagType, BufferMgmtPtr_t pBufMgr )
    192 {
    193 
    194   Ret_t _err = SML_ERR_OK;
    195   MemByte_t _tmp = 0x00;
    196 
    197   //check if content byte has to be added to the tag
    198   switch (tagType)
    199   {
    200     //set the end tag
    201     case TT_END:
    202     {
    203       _tmp = (MemByte_t)wbxmlGetGlobToken(END);
    204       if (!_tmp) return SML_ERR_XLT_INVAL_TAG_TYPE;
    205       _err = xltAddToBuffer((&_tmp), 1, pBufMgr);
    206       // remember the number of byte that must follow for the according  end-tag
    207 	  if (_err == SML_ERR_OK) pBufMgr->endTagSize -= 1;
    208 	  return _err;
    209     }
    210     //Begin and End Tag in one
    211     case TT_ALL:
    212     {
    213       _err = (MemByte_t)getTagByte(tagId, pBufMgr->smlCurExt, &_tmp);
    214       if ((!_tmp) || (_err != SML_ERR_OK)) return _err;
    215       return xltAddToBuffer((MemPtr_t)(&_tmp), 1, pBufMgr);
    216     }
    217     //Only Begin Tag -> content follows -> content byte has to be added
    218     case TT_BEG:
    219     {
    220       _err = (MemByte_t)getTagByte(tagId, pBufMgr->smlCurExt, &_tmp);
    221       if ((!_tmp) || (_err != SML_ERR_OK)) return _err;
    222 
    223       _tmp = ((MemByte_t)(_tmp | XLT_CONTBYTE));
    224       _err = xltAddToBuffer(&_tmp, 1, pBufMgr);
    225       // remember the number of byte that must follow for the according  end-tag
    226 	  if (_err == SML_ERR_OK) pBufMgr->endTagSize += 1;
    227 	  return _err;
    228     }
    229 	default:
    230     return SML_ERR_XLT_INVAL_TAG_TYPE;
    231   }
    232 
    233  // return SML_ERR_OK;Unreachable
    234 }
    235 
    236 /**
    237  * FUNCTION: wbxmlWriteTypeToBuffer
    238  *
    239  * Write a content of a certain WBXML element type (e.g. STR_I) to the global buffer
    240  *
    241  * PRE-Condition:   valid parameters
    242  *
    243  * POST-Condition:  the content is written to the wbxml buffer with the leading
    244  *                  bytes for the opaque data type or the STR_I data type
    245  *
    246  * IN:              pContent, the character pointer referencing the content to
    247  *                            write to the buffer
    248  *                  elType, the element type to write to the buffer (e.g. STR_I)
    249  *                  size, the content length
    250  *
    251  * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
    252  *
    253  * RETURN:          shows error codes of function,
    254  *                  0, if OK
    255  */
    256 Ret_t wbxmlWriteTypeToBuffer(const MemPtr_t pContent, XltElementType_t elType, Long_t size, BufferMgmtPtr_t pBufMgr)
    257 {
    258   Ret_t _err;
    259 
    260   MemByte_t _termstr = XLT_TERMSTR;
    261   MemByte_t _tmp;
    262 
    263   switch((int)elType)
    264   {
    265     case TAG:
    266     {
    267 
    268       return (xltAddToBuffer(pContent, size, pBufMgr));
    269 
    270     }
    271     case STR_I:
    272     {
    273         _tmp = (MemByte_t)wbxmlGetGlobToken(STR_I);
    274        if (!_tmp) return SML_ERR_XLT_WBXML_UKN_TOK;
    275 
    276       //add the STR_I identifier
    277       if ((_err = xltAddToBuffer(&_tmp, 1, pBufMgr)) != SML_ERR_OK) return _err;
    278 
    279       //add the string to the buffer
    280       if ((_err = xltAddToBuffer(pContent, (!pContent) ? 0 : smlLibStrlen((String_t)pContent), pBufMgr)) != SML_ERR_OK) return _err;
    281 
    282       //add the string terminator '\0'
    283       if ((_err = xltAddToBuffer(&_termstr, 1, pBufMgr)) != SML_ERR_OK) return _err;
    284 
    285       return SML_ERR_OK;
    286     }
    287     case OPAQUE:
    288     {
    289       _tmp = (MemByte_t)wbxmlGetGlobToken(OPAQUE);
    290        if (!_tmp) return SML_ERR_XLT_WBXML_UKN_TOK;
    291 
    292 
    293 
    294       //add the OPAQUE identifier
    295       if ((_err = xltAddToBuffer(&_tmp, 1, pBufMgr)) != SML_ERR_OK) return _err;
    296 
    297       //add the pContent length
    298       if ((_err = wbxmlOpaqueSize2Buf(size, pBufMgr)) != SML_ERR_OK) return _err;
    299 
    300       //add the string buffer
    301       if ((_err = xltAddToBuffer(pContent, size, pBufMgr)) != SML_ERR_OK) return _err;
    302 
    303       return SML_ERR_OK;
    304     }
    305 	default:
    306     return SML_ERR_XLT_INVAL_PCDATA_TYPE;
    307   }
    308 
    309 //  return SML_ERR_OK;unreachable
    310 }
    311 #endif
    312