/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
/*************************************************************************/
/* module:          Encoder source file                                  */
/* file:            xltenc.c                                             */
/* target system:   All                                                  */
/* target OS:       All                                                  */   
/*************************************************************************/



/*************************************************************************/
/*  Definitions                                                          */
/*************************************************************************/
#include <xltenc.h>
#ifdef __SML_WBXML__
  #include "xltencwbxml.h"
#endif
#ifdef __SML_XML__
  #include "xltencxml.h"
#endif
#include "xlttags.h"
#include "xltmetinf.h"
#include "xltdevinf.h"
#include "smlmetinfdtd.h"
#include "smldevinfdtd.h"
#include <libstr.h>
#include <libmem.h>
#include <smldtd.h>

Ret_t getTNbyPE(SmlProtoElement_t, XltTagID_t*);
Ret_t xltEncBlock(XltTagID_t, XltRO_t, const VoidPtr_t, SmlEncoding_t, BufferMgmtPtr_t, SmlPcdataExtension_t);
Ret_t xltEncList(XltListType_t, XltRO_t, VoidPtr_t, SmlEncoding_t, BufferMgmtPtr_t, SmlPcdataExtension_t);
#ifndef __SML_LITE__
Ret_t xltBuildExtention(SmlPcdataExtension_t, XltRO_t, VoidPtr_t, SmlEncoding_t, BufferMgmtPtr_t);
#endif

typedef struct PEEnc_s
{
    XltTagID_t     tagid;
    SmlProtoElement_t type;
} PEEnc_t, *PEEncPtr_t;

PEEncPtr_t getPEEncTable(void);

PEEncPtr_t getPEEncTable(void)
{ 
  PEEncPtr_t _tmpPEEncPtr;
  PEEnc_t const PE[] =
  {
    { TN_ADD,          SML_PE_ADD            },
    { TN_ALERT,        SML_PE_ALERT          },
    { TN_ATOMIC,       SML_PE_ATOMIC_START   },
    { TN_ATOMIC_END,   SML_PE_ATOMIC_END     },
    { TN_COPY,         SML_PE_COPY           },
    { TN_DELETE,       SML_PE_DELETE         },
    { TN_EXEC,         SML_PE_EXEC           },
    { TN_GET,          SML_PE_GET            },
    { TN_MAP,          SML_PE_MAP            },
    { TN_PUT,          SML_PE_PUT            },
    { TN_RESULTS,      SML_PE_RESULTS        },
    { TN_SEARCH,       SML_PE_SEARCH         },    
    { TN_SEQUENCE,     SML_PE_SEQUENCE_START },
    { TN_SEQUENCE_END, SML_PE_SEQUENCE_END   },    
    { TN_STATUS,       SML_PE_STATUS         },
    { TN_SYNC,         SML_PE_SYNC_START     },
    { TN_SYNC_END,     SML_PE_SYNC_END       },    
    { TN_REPLACE,      SML_PE_REPLACE        },
    { TN_UNDEF,        SML_PE_UNDEF          }
  };
  _tmpPEEncPtr = (PEEncPtr_t)smlLibMalloc(sizeof(PE));
  if (_tmpPEEncPtr == NULL) return NULL;
  smlLibMemcpy(_tmpPEEncPtr, &PE, sizeof(PE));
  return _tmpPEEncPtr;  
}

Ret_t getTNbyPE(SmlProtoElement_t pE, XltTagID_t *tagID)
{    
    int i = 0; 
    PEEncPtr_t pPETbl = getPEEncTable();
    if (pPETbl == NULL)
    {
      smlLibFree(pPETbl);
      return SML_ERR_NOT_ENOUGH_SPACE;
    }
    while (((pPETbl+i)->type) != SML_PE_UNDEF)
    {
      if (((pPETbl+i)->type) == pE)
      {
      	*tagID = (pPETbl+i)->tagid;
      	smlLibFree(pPETbl);
        return SML_ERR_OK;
      }    
      i++;
    }        
   	smlLibFree(pPETbl);
    *tagID = TN_UNDEF;
    return SML_ERR_XLT_INVAL_PROTO_ELEM;
}


/**
 * FUNCTION: smlXltEncInit
 *
 * Initializes an XML buffer; Creates XML code for the SyncHdr
 * and appends it to the buffer.
 * Returns 0 if operation was successful.
 *
 * PRE-Condition:   no memory should be allocated for ppEncoder (should be NULL)
 *                  pHeader has to contain a valid SyncHdr structure
 *                  pBufEnd must point to the end of the (WB)XML buffer
 *                  ppBufPos has to be initialized to the start point of the
 *                  (WB)XML buffer.
 *                  
 *
 * POST-Condition:  After the function call ppBufPos points to the
 *                  first free byte in the buffer behind the (WB)XML document
 *
 * IN:              enc, the encoding constant (SML_WBXML or SML_XML)
 *                  pHeader, the SyncML header structure
 *                  pBufEnd, pointer to the end of the buffer to write on
 * 
 * IN/OUT:          ppBufPos, current position of the bufferpointer
 *                  ppEncoder, the encoder object       
 *
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 *                  Possible Error Codes:
 *                  SML_ERR_XLT_MISSING_CONT            
 *                  SML_ERR_XLT_BUF_ERR                 
 *                  SML_ERR_XLT_INVAL_ELEM_TYPE         
 *                  SML_ERR_XLT_INVAL_LIST_TYPE         
 *                  SML_ERR_XLT_INVAL_TAG_TYPE          
 *                  SML_ERR_XLT_ENC_UNK	               
 *                  SML_ERR_XLT_INVAL_PROTO_ELEM
 */
Ret_t xltEncInit(SmlEncoding_t enc,
                 const SmlSyncHdrPtr_t pHeader,
                 const MemPtr_t pBufEnd,
                 MemPtr_t *ppBufPos,
                 XltEncoderPtr_t *ppEncoder)
{
  // Return variable
  Ret_t _err;

  XltEncoderPtr_t _pEncoder;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

#ifdef __SML_WBXML__
    MemByte_t _stablen     =  0x1D; //XLT_STABLEN;
    MemByte_t _wbxmlver    = XLT_WBXMLVER;
    MemByte_t _charset     = XLT_CHARSET;
    MemByte_t _pubident1   = XLT_PUBIDENT1;
    MemByte_t _pubident2   = XLT_PUBIDENT2;
    MemByte_t _syncmldtd[] = XLT_DTD_ID;
#endif

#ifdef __SML_XML__
  MemPtr_t _tmpStr;
  MemPtr_t _xmlver = (MemPtr_t)XML_VERSION;
  MemPtr_t _xmlenc = (MemPtr_t)XML_ENCODING;
  MemByte_t _begpar = XML_BEGPAR;
  MemByte_t _endpar = XML_ENDPAR;
#endif

  //MemByte_t _tmp = 0x00;CURRENTLY NOT USED

  if ((_pEncoder = (XltEncoderPtr_t)smlLibMalloc(sizeof(XltEncoder_t))) == NULL) return SML_ERR_NOT_ENOUGH_SPACE;
  if ((_pBufMgr = (BufferMgmtPtr_t)smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) {
      smlLibFree(_pEncoder);
      return SML_ERR_NOT_ENOUGH_SPACE; 
  }

  //set the encoding
  _pEncoder->enc = enc;

  _pEncoder->cur_ext = (SmlPcdataExtension_t)SML_EXT_UNDEFINED;
  _pEncoder->last_ext = (SmlPcdataExtension_t)SML_EXT_UNDEFINED;
  _pEncoder->end_tag_size = 0;
  _pEncoder->space_evaluation = NULL;
  

  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltWrittenBytes = 0;  
  _pBufMgr->smlCurExt = _pEncoder->cur_ext;
  _pBufMgr->smlLastExt = _pEncoder->last_ext;
  _pBufMgr->smlActiveExt = SML_EXT_UNDEFINED;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = 0;
  _pBufMgr->endTagSize = 0;

  switch (enc)
  {

#ifdef __SML_WBXML__
    case SML_WBXML:
    {

      // Set the WBXML Header Values
      // WBXML Version
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_wbxmlver), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Public Idetifier - default unknown
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_pubident1), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_pubident2), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Character set - not yet implemented
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_charset), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Sting table length -  not yet implemented
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_stablen), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;

      if ((_err = xltAddToBuffer((MemPtr_t) (&_syncmldtd) , smlLibStrlen((String_t)_syncmldtd), _pBufMgr)) != SML_ERR_OK) break;      

      break;
    }
#endif

#ifdef __SML_XML__
    case SML_XML:
    {    
           
      if ((_err = xltAddToBuffer((MemPtr_t)(&_begpar), 1, _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"?xml version=\"";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = _xmlver;
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"\" encoding=\"";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = _xmlenc;
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"\"?";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      if ((_err = xltAddToBuffer((MemPtr_t)(&_endpar), 1, _pBufMgr)) != SML_ERR_OK) break;

      break;
    }
#endif

    default:
    {
      _err = SML_ERR_XLT_ENC_UNK;
    }
  }
  if (_err != SML_ERR_OK)
  {
     smlLibFree(_pBufMgr);
     smlLibFree(_pEncoder);
     return _err;
  }

  // SyncML Tag   
  if ((_err = xltGenerateTag(TN_SYNCML, TT_BEG, enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    smlLibFree(_pEncoder);
    return _err;
  }

  // Generate SmlSyncHdr

  if ((_err = xltEncBlock(TN_SYNCHDR, REQUIRED, pHeader, enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    smlLibFree(_pEncoder);
    return _err;
  }

  // SyncBody Tag 
  if ((_err = xltGenerateTag(TN_SYNCBODY, TT_BEG, enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    smlLibFree(_pEncoder);
    return _err;
  }

  _pEncoder->cur_ext = _pBufMgr->smlCurExt;
  _pEncoder->last_ext = _pBufMgr->smlLastExt;
  _pEncoder->end_tag_size = _pBufMgr->endTagSize;

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  _pEncoder->final = 0;

  *ppEncoder = (XltEncoderPtr_t)_pEncoder;

  return SML_ERR_OK;
}

/**
 * FUNCTION: smlXltEncAppend
 *
 * Generates XML code and appends it to the XML buffer.
 *
 * PRE-Condition:   pEncoder holds the initialized encoder structure.
 *                  the initialization takes place in the xltEncAppend function
 *                  pContent has to contain a valid content structure structure
 *                  pBufEnd must point to the end of the (WB)XML buffer
 *                  ppBufPos has to be initialized to the start point of the
 *                  (WB)XML buffer.
 *                  
 *
 * POST-Condition:  After the function call ppBufPos points to the
 *                  first free byte in the buffer behind the (WB)XML document
 *
 * IN:              pEncoder, the encoder object
 *                  pe, the protocol element (PE_ADD, ...)    
 *                  pBufEnd, pointer to the end of the buffer to write on
 *                  pContent, the content to append to the SyncML document
 * 
 * IN/OUT:          ppBufPos, current position of the bufferpointer
 * 
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 *                  Possible Error Codes:
 *                  SML_ERR_XLT_MISSING_CONT            
 *                  SML_ERR_XLT_BUF_ERR                 
 *                  SML_ERR_XLT_INVAL_ELEM_TYPE         
 *                  SML_ERR_XLT_INVAL_LIST_TYPE         
 *                  SML_ERR_XLT_INVAL_TAG_TYPE          
 *                  SML_ERR_XLT_ENC_UNK	               
 *                  SML_ERR_XLT_INVAL_PROTO_ELEM
 */
Ret_t xltEncAppend(const XltEncoderPtr_t pEncoder, 
                   SmlProtoElement_t pe, 
                   const MemPtr_t pBufEnd,
                   const VoidPtr_t pContent,
                   MemPtr_t *ppBufPos)
{
  // Return variable
  Ret_t _err;

  XltTagID_t tagID = TN_UNDEF;

  // encoding type
  SmlEncoding_t _enc;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

  if ((_pBufMgr = (BufferMgmtPtr_t)smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) return SML_ERR_NOT_ENOUGH_SPACE;
  smlLibMemset(_pBufMgr, 0, sizeof(BufferMgmt_t));

  //get the encoding type
  _enc = pEncoder->enc;
  
  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltWrittenBytes = 0;      
  _pBufMgr->smlCurExt = pEncoder->cur_ext;
  _pBufMgr->smlLastExt = pEncoder->last_ext;
  _pBufMgr->smlActiveExt = SML_EXT_UNDEFINED;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = ((pEncoder->space_evaluation == NULL) ? 0 : 1);
  _pBufMgr->endTagSize =0;
	  
  _err = getTNbyPE(pe, &tagID);

  _err = xltEncBlock(tagID, REQUIRED, pContent, _enc, _pBufMgr, SML_EXT_UNDEFINED);
  if (_err != SML_ERR_OK)
  {
     smlLibFree(_pBufMgr);
     return _err;
  }  

  pEncoder->cur_ext = _pBufMgr->smlCurExt;
  pEncoder->last_ext = _pBufMgr->smlLastExt;

  if (pEncoder->space_evaluation != NULL) {
	pEncoder->space_evaluation->written_bytes += _pBufMgr->smlXltWrittenBytes;
	pEncoder->space_evaluation->end_tag_size += _pBufMgr->endTagSize;
  } else {
	pEncoder->end_tag_size += _pBufMgr->endTagSize;
  }

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  return SML_ERR_OK;
}

/**
 * FUNCTION: smlXltEncTerminate
 *
 * Filnalizes the (WB)XML document and returns the size of written bytes to 
 * the workspace module
 *
 * PRE-Condition:   pEncoder holds the initialized encoder structure.
 *                  the initialization takes place in the xltEncAppend function
 *                  pBufEnd must point to the end of the (WB)XML buffer
 *                  ppBufPos has to be initialized to the start point of the
 *                  (WB)XML buffer.
 *                  
 * POST-Condition:  After the function call ppBufPos points to the
 *                  first free byte in the buffer behind the (WB)XML document
 *
 * IN:              pEncoder, the encoder object
 *                  pBufEnd, pointer to the end of the buffer to write on
 * 
 * IN/OUT:          ppBufPos, current position of the bufferpointer
 * 
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 *                  Possible Error Codes:
 *                  SML_ERR_XLT_BUF_ERR      
 *                  SML_ERR_XLT_MISSING_CONT
 *                  SML_ERR_XLT_INVAL_ELEM_TYPE         
 *                  SML_ERR_XLT_INVAL_LIST_TYPE         
 *                  SML_ERR_XLT_INVAL_TAG_TYPE          
 *                  SML_ERR_XLT_ENC_UNK	               
 *                  SML_ERR_XLT_INVAL_PROTO_ELEM
 */
Ret_t xltEncTerminate(const XltEncoderPtr_t pEncoder,
                      const MemPtr_t pBufEnd,
                      MemPtr_t *ppBufPos)
{
  // Return variable      
  Ret_t _err;

  // encoding type
  SmlEncoding_t _enc;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

  //get the encoding type
  _enc = pEncoder->enc;

  //Initialize buffer variables
  if ((_pBufMgr = smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) {
 	  smlLibFree(pEncoder);
 	  return SML_ERR_NOT_ENOUGH_SPACE;
   }

  _pBufMgr->smlXltWrittenBytes = 0;      
  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlCurExt = pEncoder->cur_ext;
  _pBufMgr->smlLastExt = pEncoder->last_ext;
  _pBufMgr->smlActiveExt = pEncoder->cur_ext;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = ((pEncoder->space_evaluation == NULL) ? 0 : 1);
  _pBufMgr->endTagSize =0;

  if (pEncoder->final == 1)
  {
    // Final Flag     
    if ((_err = xltGenerateTag(TN_FINAL, TT_ALL, _enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) 
    {
      smlLibFree(_pBufMgr);
      xltEncReset(pEncoder);
      return _err;
    }
  }

  // SyncBody End Tag     
  if ((_err = xltGenerateTag(TN_SYNCBODY, TT_END, _enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) 
  {
    smlLibFree(_pBufMgr);
    xltEncReset(pEncoder);
    return _err;
  }

  // SyncML End Tag       
  if ((_err = xltGenerateTag(TN_SYNCML, TT_END, _enc, _pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    xltEncReset(pEncoder);
    return _err;
  }

  pEncoder->cur_ext = _pBufMgr->smlCurExt;
  pEncoder->last_ext = _pBufMgr->smlLastExt;

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  xltEncReset(pEncoder);

  return SML_ERR_OK;
}

Ret_t xltEncReset(XltEncoderPtr_t pEncoder)
{
  if ((pEncoder) && (pEncoder->space_evaluation)) {
    smlLibFree(pEncoder->space_evaluation);
    pEncoder->space_evaluation = NULL;
  }
  smlLibFree(pEncoder);
  return SML_ERR_OK;
}


/**
 * FUNCTION: smlXltStartEvaluation
 *
 * Starts an evaluation run which prevents further API-Calls to write tags - 
 * just the tag-sizes are calculated. Must be sopped via smlEndEvaluation
 *
 * IN:              XltEncoderPtr_t
 *                  the encoder object
 *
 *
 * RETURN:          Ret_t
 *                  Return Code
 */
SML_API Ret_t xltStartEvaluation(XltEncoderPtr_t pEncoder)
{
  XltSpaceEvaluationPtr_t _pSpaceEvaluation;

  if (pEncoder->space_evaluation != NULL)
	  return SML_ERR_WRONG_USAGE;

  if ((_pSpaceEvaluation = (XltSpaceEvaluationPtr_t)smlLibMalloc(sizeof(XltSpaceEvaluation_t))) == NULL) 
	  return SML_ERR_NOT_ENOUGH_SPACE;
  smlLibMemset(_pSpaceEvaluation, 0, sizeof(XltSpaceEvaluation_t));

  pEncoder->space_evaluation = _pSpaceEvaluation;
  return SML_ERR_OK;
}


/**
 * FUNCTION: smlXltEndEvaluation
 *
 * Stops an evaluation run which prevents further API-Calls to write tags - 
 * the remaining free buffer size after all Tags are written is returned
 *
 * IN:              XltEncoderPtr_t
 *                  the encoder object
 *
 * IN/OUT:          MemSize_t              
 *					Size of free buffer for data after all tags are written
 *
 * RETURN:          Ret_t
 *                  Return Code
 */
SML_API Ret_t xltEndEvaluation(InstanceID_t id, XltEncoderPtr_t pEncoder, MemSize_t *freemem)
{
  
  MemSize_t _free;
  XltSpaceEvaluationPtr_t _evaluation;
  
  _evaluation = pEncoder->space_evaluation;

  if (_evaluation == NULL)
	  return SML_ERR_WRONG_USAGE;

  _free = smlGetFreeBuffer(id);
  _free -= _evaluation->written_bytes;
  _free -= _evaluation->end_tag_size;
  _free -= pEncoder->end_tag_size;
  
  *freemem = _free;
  
  smlLibFree(_evaluation);
  pEncoder->space_evaluation = NULL;

  return SML_ERR_OK;
}


/**
 * FUNCTION: xltEncBlock
 *
 * Generates a (WB)XML Block for a given tag ID and a given content
 *
 * PRE-Condition:   pContent holds a valid content structure
 *                  tagId contains a valid SyncML tag ID                  
 *
 * POST-Condition:  the (WB)XML buffer in the pBufMgr structure contains the
 *                  encoded (WB)XML block
 *
 * IN:              tagId, the ID for the tag to generate (TN_ADD, ...)
 *                  reqOptFlag, flag if the block is required or optional
 *                  pContent, the content structure of the block
 *                  enc, the encoding constant (SML_WBXML or SML_XML)
 *                  attFlag, indicates if the encoded tag contain Attributes in namespace extensions
 * 
 * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
 * 
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 */
Ret_t xltEncBlock(XltTagID_t tagId, XltRO_t reqOptFlag, const VoidPtr_t pContent, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr, SmlPcdataExtension_t attFlag)
{

  //Return variable
  Ret_t _err;


  //Check if pContent of a required field is missing
  if ((reqOptFlag == REQUIRED) && (pContent == NULL)) {
    switch ((int)tagId) {
      case TN_ATOMIC_END:
      case TN_SYNC_END:
      case TN_SEQUENCE_END:      
        break;
      default:  
        return SML_ERR_XLT_MISSING_CONT;
    }    
  }  
  //Check if pContent of a optional field is missing
  else if ((pContent == NULL) && (tagId != TN_SYNC_END) && (tagId != TN_ATOMIC_END) && (tagId != TN_SEQUENCE_END))
    return SML_ERR_OK;
  
  //Generate the commands -> see DTD
  switch ((int)tagId){
    case TN_SYNCHDR:
      // SyncHdr Begin Tag
      if ((_err = xltGenerateTag(TN_SYNCHDR, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Version
      if ((_err = xltEncBlock(TN_VERSION, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->version, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Proto
      if ((_err = xltEncBlock(TN_PROTO, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->proto, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // SessionID
      if ((_err = xltEncBlock(TN_SESSIONID, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->sessionID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // MsgID
      if ((_err = xltEncBlock(TN_MSGID, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->msgID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Target
      if ((_err = xltEncBlock(TN_TARGET, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Source
      if ((_err = xltEncBlock(TN_SOURCE, REQUIRED, ((SmlSyncHdrPtr_t) pContent)->source, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // RespURI?
      if ((_err = xltEncBlock(TN_RESPURI, OPTIONAL, ((SmlSyncHdrPtr_t) pContent)->respURI, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlSyncHdrPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlSyncHdrPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlSyncHdrPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // SyncHdr End Tag
      if ((_err = xltGenerateTag(TN_SYNCHDR, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_CRED:
      // Begin tag
      if ((_err = xltGenerateTag(TN_CRED, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlCredPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Data
      if ((_err = xltEncBlock(TN_DATA, REQUIRED, ((SmlCredPtr_t) pContent)->data, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // End tag
      if ((_err = xltGenerateTag(TN_CRED, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_SOURCE:
    case TN_TARGET:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // LocURI
      if ((_err = xltEncBlock(TN_LOCURI, REQUIRED, ((SmlSourcePtr_t) pContent)->locURI, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                 
      // LocName?
      if ((_err = xltEncBlock(TN_LOCNAME, OPTIONAL, ((SmlSourcePtr_t) pContent)->locName, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                       
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_ITEM:
      // Begin tag
      if ((_err = xltGenerateTag(TN_ITEM, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Target?
      if ((_err = xltEncBlock(TN_TARGET, OPTIONAL, ((SmlItemPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Source?
      if ((_err = xltEncBlock(TN_SOURCE, OPTIONAL, ((SmlItemPtr_t) pContent)->source, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlItemPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Data?
      if ((_err = xltEncBlock(TN_DATA, OPTIONAL, ((SmlItemPtr_t) pContent)->data, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
	  // MoreData?
      if ((_err = xltEncBlock(TN_MOREDATA, OPTIONAL, &(((SmlItemPtr_t) pContent)->flags), enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // End tag
      if ((_err = xltGenerateTag(TN_ITEM, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#if (defined ADD_SEND || defined COPY_SEND)    
    case TN_ADD:
    case TN_COPY:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlGenericCmdPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlGenericCmdPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlGenericCmdPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                     
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlGenericCmdPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                     
      // Item+
      if ((_err = xltEncList(ITEM_LIST, REQUIRED, ((SmlGenericCmdPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#endif
    case TN_ALERT:
      // Begin tag
      if ((_err = xltGenerateTag(TN_ALERT, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlAlertPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlAlertPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlAlertPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // Data?
      if ((_err = xltEncBlock(TN_DATA, OPTIONAL, ((SmlAlertPtr_t) pContent)->data, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // Item*
      if ((_err = xltEncList(ITEM_LIST, OPTIONAL, ((SmlAlertPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      if ((_err = xltEncBlock(TN_CORRELATOR, OPTIONAL, ((SmlAlertPtr_t) pContent)->correlator, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // End tag
      if ((_err = xltGenerateTag(TN_ALERT, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#if (defined ATOMIC_SEND || defined SEQUENCE_SEND)    
    case TN_ATOMIC:
    case TN_SEQUENCE:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlAtomicPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlAtomicPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlAtomicPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      //End tag in TN_ATOMIC_END

      break;
    case TN_ATOMIC_END:
      // End tag
      if ((_err = xltGenerateTag(TN_ATOMIC, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      break;
    case TN_SEQUENCE_END:
      // End tag
      if ((_err = xltGenerateTag(TN_SEQUENCE, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      break;
#endif    
    case TN_DELETE:
      // Begin tag
      if ((_err = xltGenerateTag(TN_DELETE, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlDeletePtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlDeletePtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Archive?
      if ((_err = xltEncBlock(TN_ARCHIVE, OPTIONAL, &(((SmlDeletePtr_t) pContent)->flags), enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // SftDel?
      if ((_err = xltEncBlock(TN_SFTDEL, OPTIONAL, &(((SmlDeletePtr_t) pContent)->flags), enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlDeletePtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlDeletePtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Item+
      if ((_err = xltEncList(ITEM_LIST, REQUIRED, ((SmlDeletePtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(TN_DELETE, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#ifdef EXEC_SEND    
    case TN_EXEC:
      // Begin tag
      if ((_err = xltGenerateTag(TN_EXEC, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlExecPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlExecPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlExecPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlExecPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Item
      if ((_err = xltEncBlock(TN_ITEM, REQUIRED, ((SmlExecPtr_t) pContent)->item, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Correlator?
      if ((_err = xltEncBlock(TN_CORRELATOR, OPTIONAL, ((SmlExecPtr_t) pContent)->correlator, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // End tag
      if ((_err = xltGenerateTag(TN_EXEC, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#endif
    case TN_GET:
    case TN_PUT:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlGetPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlGetPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // Lang?
      if ((_err = xltEncBlock(TN_LANG, OPTIONAL, ((SmlGetPtr_t) pContent)->lang, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlGetPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlGetPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Item+
      if ((_err = xltEncList(ITEM_LIST, REQUIRED, ((SmlGetPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                    
      
      break;
    case TN_MAP:
      // Begin tag
      if ((_err = xltGenerateTag(TN_MAP, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlMapPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // Target
      if ((_err = xltEncBlock(TN_TARGET, REQUIRED, ((SmlMapPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // Source
      if ((_err = xltEncBlock(TN_SOURCE, REQUIRED, ((SmlMapPtr_t) pContent)->source, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlMapPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlMapPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Mapitemlist
      if ((_err = xltEncList(MAPITEM_LIST, REQUIRED, ((SmlMapPtr_t) pContent)->mapItemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // End tag
      if ((_err = xltGenerateTag(TN_MAP, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_MAPITEM:
      // Begin tag
      if ((_err = xltGenerateTag(TN_MAPITEM, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Target
      if ((_err = xltEncBlock(TN_TARGET, REQUIRED, ((SmlMapItemPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Source
      if ((_err = xltEncBlock(TN_SOURCE, REQUIRED, ((SmlMapItemPtr_t) pContent)->source, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // End tag
      if ((_err = xltGenerateTag(TN_MAPITEM, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_RESULTS:
      // Begin tag
      if ((_err = xltGenerateTag(TN_RESULTS, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlResultsPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // MsgRef?
      if ((_err = xltEncBlock(TN_MSGREF, OPTIONAL, ((SmlResultsPtr_t) pContent)->msgRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // CmdRef
      if ((_err = xltEncBlock(TN_CMDREF, REQUIRED, ((SmlResultsPtr_t) pContent)->cmdRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                            
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlResultsPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // TargetRef?
      if ((_err = xltEncBlock(TN_TARGETREF, OPTIONAL, ((SmlResultsPtr_t) pContent)->targetRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // SourceRef?
      if ((_err = xltEncBlock(TN_SOURCEREF, OPTIONAL, ((SmlResultsPtr_t) pContent)->sourceRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // Item+
      if ((_err = xltEncList(ITEM_LIST, REQUIRED, ((SmlResultsPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(TN_RESULTS, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_CHAL:
      // Begin tag
      if ((_err = xltGenerateTag(TN_CHAL, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // Meta
      if ((_err = xltEncBlock(TN_META, REQUIRED, ((SmlChalPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // End tag
      if ((_err = xltGenerateTag(TN_CHAL, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#ifdef SEARCH_SEND
    case TN_SEARCH:
      // Begin tag
      if ((_err = xltGenerateTag(TN_SEARCH, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlSearchPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlSearchPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // NoResults?
      if ((_err = xltEncBlock(TN_NORESULTS, OPTIONAL, &((SmlSearchPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlSearchPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Target?
      if ((_err = xltEncBlock(TN_TARGET, OPTIONAL, ((SmlSearchPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                     
      // Source List
      if ((_err = xltEncList(SOURCE_LIST, REQUIRED, ((SmlSearchPtr_t) pContent)->sourceList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                        
      // Lang?
      if ((_err = xltEncBlock(TN_LANG, OPTIONAL, ((SmlSearchPtr_t) pContent)->lang, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Meta
      if ((_err = xltEncBlock(TN_META, REQUIRED, ((SmlSearchPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Dsta
      if ((_err = xltEncBlock(TN_DATA, REQUIRED, ((SmlSearchPtr_t) pContent)->data, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // End tag
      if ((_err = xltGenerateTag(TN_SEARCH, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
#endif    
    case TN_STATUS:
      // Begin tag
      if ((_err = xltGenerateTag(TN_STATUS, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlStatusPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // MsgRef?
      if ((_err = xltEncBlock(TN_MSGREF, REQUIRED, ((SmlStatusPtr_t) pContent)->msgRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // CmdRef
      if ((_err = xltEncBlock(TN_CMDREF, REQUIRED, ((SmlStatusPtr_t) pContent)->cmdRef, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Cmd
      if ((_err = xltEncBlock(TN_CMD, REQUIRED, ((SmlStatusPtr_t) pContent)->cmd, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // TargetRefList?
      if ((_err = xltEncList(TARGETREF_LIST, OPTIONAL, ((SmlStatusPtr_t) pContent)->targetRefList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // SourceRefList?
      if ((_err = xltEncList(SOURCEREF_LIST, OPTIONAL, ((SmlStatusPtr_t) pContent)->sourceRefList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlStatusPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Chal?
      if ((_err = xltEncBlock(TN_CHAL, OPTIONAL, ((SmlStatusPtr_t) pContent)->chal, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Data
      if ((_err = xltEncBlock(TN_DATA, REQUIRED, ((SmlStatusPtr_t) pContent)->data, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                          
      // Item*
      if ((_err = xltEncList(ITEM_LIST, OPTIONAL, ((SmlStatusPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(TN_STATUS, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_SYNC:
      // Begin tag
      if ((_err = xltGenerateTag(TN_SYNC, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlSyncPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlSyncPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                         
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlSyncPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Target?
      if ((_err = xltEncBlock(TN_TARGET, OPTIONAL, ((SmlSyncPtr_t) pContent)->target, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Source?
      if ((_err = xltEncBlock(TN_SOURCE, OPTIONAL, ((SmlSyncPtr_t) pContent)->source, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                       
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlSyncPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
	  // NumberOfChanges?
      if ((_err = xltEncBlock(TN_NUMBEROFCHANGES, OPTIONAL, ((SmlSyncPtr_t) pContent)->noc, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // End tag in TN_SYNC_END

      break;
    case TN_SYNC_END:
      //End tag
      if ((_err = xltGenerateTag(TN_SYNC, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_REPLACE:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // CmdID
      if ((_err = xltEncBlock(TN_CMDID, REQUIRED, ((SmlGenericCmdPtr_t) pContent)->cmdID, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // NoResp?
      if ((_err = xltEncBlock(TN_NORESP, OPTIONAL, &((SmlGenericCmdPtr_t) pContent)->flags, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                           
      // Cred?
      if ((_err = xltEncBlock(TN_CRED, OPTIONAL, ((SmlGenericCmdPtr_t) pContent)->cred, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                     
      // Meta?
      if ((_err = xltEncBlock(TN_META, OPTIONAL, ((SmlGenericCmdPtr_t) pContent)->meta, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                     
      // Item+     
      if ((_err = xltEncList(ITEM_LIST, REQUIRED, ((SmlGenericCmdPtr_t) pContent)->itemList, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;                                   
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_ARCHIVE:                        
      //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t*)pContent)) & (SmlArchive_f)) {
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      }                       
      break;
    case TN_SFTDEL:                 
      //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t *) pContent)) & (SmlSftDel_f)) {
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      break;
	case TN_MOREDATA:
	  //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t *) pContent)) & (SmlMoreData_f)) {
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      }
      break;
    case TN_NORESULTS:
      //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t *) pContent)) & (SmlNoResults_f)) {
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      }
      break;
    case TN_NORESP:                                 
      //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t *) pContent)) & (SmlNoResp_f)){
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      }
      break;
    case TN_FINAL:                  
      //set the flag in the (WB)XML document if the flag is in the pContent
      if ((*((Flag_t *) pContent)) & (SmlFinal_f)) {
        if ((_err = xltGenerateTag(tagId, TT_ALL, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      }
      break;

    /*
     * Change to support TNDS. 
     */
    case TN_TNDS_TREE:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // VerDTD
      if ((_err = xltEncBlock(TN_TNDS_VERSION, REQUIRED, ((SmlSubTreePtr_t) pContent)->version, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Man
      if ((_err = xltEncBlock(TN_TNDS_MAN, OPTIONAL, ((SmlSubTreePtr_t) pContent)->man, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Mode
      if ((_err = xltEncBlock(TN_TNDS_MODE, OPTIONAL, ((SmlSubTreePtr_t) pContent)->mode, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // NODE
      if ((_err = xltEncBlock(TN_TNDS_NODE, REQUIRED, ((SmlSubTreePtr_t) pContent)->node, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;

      break;
    case TN_TNDS_NODE:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Name
      if ((_err = xltEncBlock(TN_TNDS_NODENAME, REQUIRED, ((SmlSubTreeNodePtr_t) pContent)->nodeName, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Path
      if ((_err = xltEncBlock(TN_TNDS_NODEPATH, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->path, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Title
      if ((_err = xltEncBlock(TN_TNDS_NODETITLE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->title, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Begin RTProperties tag
      if ((_err = xltGenerateTag(TN_TNDS_NODERTPROPERTIES, TT_BEG, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // ACL
      if ((_err = xltEncBlock(TN_TNDS_RTACL, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->ACL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Format
      if ((_err = xltEncBlock(TN_TNDS_RTFORMAT, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->format, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // TimeStamp
      if ((_err = xltEncBlock(TN_TNDS_RTTSTAMP, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->timeStamp, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // VerNO
      if ((_err = xltEncBlock(TN_TNDS_RTVERNO, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->verNo, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Type
      if ((_err = xltEncBlock(TN_TNDS_RTTYPE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->mimeType, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // End RTProperties tag
      if ((_err = xltGenerateTag(TN_TNDS_NODERTPROPERTIES, TT_END, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Size
      if ((_err = xltEncBlock(TN_TNDS_NODESIZE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->size, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Value
      if ((_err = xltEncBlock(TN_TNDS_NODEVALUE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->data, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Child Node
      if ((_err = xltEncBlock(TN_TNDS_NODE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->child, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      // Sibling Node
      if ((_err = xltEncBlock(TN_TNDS_NODE, OPTIONAL, ((SmlSubTreeNodePtr_t) pContent)->nextSibling, enc, pBufMgr, SML_EXT_UNDEFINED)) != SML_ERR_OK) return _err;

      break;
    case TN_TNDS_RTFORMAT:
      // Begin tag
      if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
      {
          SmlPcdataPtr_t format = (SmlPcdataPtr_t)pContent;
          if (!strcmp((char *)format->content, "chr"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTCHR, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "node"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTNODE, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "null"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTNULL, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "bin"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTBIN, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "bool"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTBOOL, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "b64"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTB64, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "int"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTINT, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "xml"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTXML, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "date"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTDATE, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "time"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTTIME, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
          else if (!strcmp((char *)format->content, "float"))
          {
              if ((_err = xltGenerateTag(TN_TNDS_FMTFLOAT, TT_ALL, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;
          }
      }
      // End tag
      if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK) return _err;

      break;

    default:  // all leaf nodes (PCDATA#)
        return xltEncPcdata(tagId, reqOptFlag, pContent, enc, pBufMgr, attFlag);
    }
    }
    return SML_ERR_OK;
}
      
Ret_t xltEncPcdata(XltTagID_t tagId, XltRO_t reqOptFlag, const VoidPtr_t pContent, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr, SmlPcdataExtension_t attFlag)   {
    //Return variable
    Ret_t _err;

    //generate PCDATA begin tag
    if ((_err = xltGenerateTag(tagId, TT_BEG, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;

    //write the pContent to the buffer according the encoding type
    switch ((int)enc) {
#ifdef __SML_WBXML__
    case SML_WBXML:
        switch (((SmlPcdataPtr_t)pContent)->contentType) {
        case SML_PCDATA_STRING:
            if ((_err = wbxmlWriteTypeToBuffer(((SmlPcdataPtr_t)pContent)->content, STR_I, ((SmlPcdataPtr_t)pContent)->length, pBufMgr)) != SML_ERR_OK) return _err;
            break;
        case SML_PCDATA_OPAQUE:
            if ((_err = wbxmlWriteTypeToBuffer(((SmlPcdataPtr_t)pContent)->content, OPAQUE, ((SmlPcdataPtr_t)pContent)->length, pBufMgr)) != SML_ERR_OK) return _err;
            break;
#ifdef __USE_EXTENSIONS__
        case SML_PCDATA_EXTENSION:
            if ((_err = xltBuildExtention(((SmlPcdataPtr_t)pContent)->extension, reqOptFlag, ((SmlPcdataPtr_t)pContent)->content, enc, pBufMgr)) != SML_ERR_OK) return _err;
            break;
#endif
        /* SCTSTK - 21/01/2003 : If the content type is unknown just build the ending tag
        default:
            return SML_ERR_XLT_INVAL_PCDATA_TYPE;
		*/
        }; // eof switch(contenttype)
        break;
#endif  // eof WBXML

#ifdef __SML_XML__
    case SML_XML:
        switch (((SmlPcdataPtr_t)pContent)->contentType) {
            case SML_PCDATA_CDATA: {
			    MemPtr_t _tmpStr;        
                _tmpStr = (MemPtr_t) "<![CDATA[";
                if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), pBufMgr)) != SML_ERR_OK) return _err;
                if ((_err = xltAddToBuffer(((SmlPcdataPtr_t)pContent)->content, ((SmlPcdataPtr_t)pContent)->length, pBufMgr)) != SML_ERR_OK) return _err;
                _tmpStr = (MemPtr_t) "]]>";
                if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), pBufMgr)) != SML_ERR_OK) return _err;
                break;
                }
            case SML_PCDATA_STRING:
                if ((_err = xltAddToBuffer(((SmlPcdataPtr_t)pContent)->content, ((SmlPcdataPtr_t)pContent)->length, pBufMgr)) != SML_ERR_OK) return _err;
                break;
            /* SCTSTK - 16/03/2002 : Converts raw data into a byte stream string, used during WBXML conversion */
			case SML_PCDATA_OPAQUE:
				{
					extern int sprintf(char *, const char *, ...);
					int flag=1;
					int len=((SmlPcdataPtr_t)pContent)->length;
					char *byteStream=(char *)smlLibMalloc(len*3+2);
					char *_byteStr=byteStream;
					char *_tmpStr=((SmlPcdataPtr_t)pContent)->content;
					unsigned char ch;
					while (len)
					{
						ch=*_tmpStr;
						if (flag && ch!=9 && ch!=10 && ch!=13 && (ch<32 || ch>126)) flag=0;
						sprintf(_byteStr,"%02X ",ch);
						_byteStr+=3;
						++_tmpStr;
						--len;
					}
					len=((SmlPcdataPtr_t)pContent)->length;
					if (flag)
					{
						if ((_err = xltAddToBuffer("<![CDATA[",9,pBufMgr)) != SML_ERR_OK)
						{
							smlLibFree(byteStream);
							return _err;
						}
						_tmpStr=((SmlPcdataPtr_t)pContent)->content;
					}
					else
					{
						if ((_err = xltAddToBuffer("<!-- Binary data converted to byte stream during WBXML conversion -->",69,pBufMgr)) != SML_ERR_OK)
						{
							smlLibFree(byteStream);
							return _err;
						}
						_tmpStr=byteStream;
						len=len<<1;
					}
					_err = xltAddToBuffer(_tmpStr, len, pBufMgr);
					smlLibFree(byteStream);
					if (flag && _err==SML_ERR_OK) _err = xltAddToBuffer("]]>",3,pBufMgr);
					if (_err!=SML_ERR_OK) return _err;
					break;
				}
#ifdef __USE_EXTENSIONS__
            case SML_PCDATA_EXTENSION:
                if ((_err = xltBuildExtention(((SmlPcdataPtr_t)pContent)->extension, reqOptFlag, ((SmlPcdataPtr_t)pContent)->content, enc, pBufMgr)) != SML_ERR_OK) return _err;
                break;
#endif
            /* SCTSTK - 21/01/2003 : If the content type is unknown just build the ending tag
            default:
                return SML_ERR_XLT_INVAL_PCDATA_TYPE;
			*/
        }
        break;         
#endif // eof XML

        default:
            return SML_ERR_XLT_ENC_UNK;
    } // eof switch(enc)

    //generate PCDATA END tag
    if ((_err = xltGenerateTag(tagId, TT_END, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
    return SML_ERR_OK;
}

/**
 * FUNCTION: xltEncList
 *
 * Generates a list element which is not directly related to a tag
 *
 * PRE-Condition:   pList holds a valid list structure
 *                  listId contains a valid SyncML list ID                  
 *
 * POST-Condition:  the (WB)XML buffer in the pBufMgr structure contains the
 *                  encoded (WB)XML list
 *
 * IN:              listId, the ID of the list to generate (e.g. TARGET_LIST, ...)
 *                  pList, reference to the list to process
 *                  enc, the encoding constant (SML_WBXML or SML_XML)
 *                  attFlag, indicates if the encoded tag contain Attributes in namespace extensions
 * 
 * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
 * 
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 */
Ret_t xltEncList(XltListType_t listId, XltRO_t reqOptFlag, VoidPtr_t pList, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr, SmlPcdataExtension_t attFlag)
{
  //Return variable
  Ret_t _err;

  //check if list is required or not
  if ((reqOptFlag == REQUIRED) && (pList == NULL))
    return SML_ERR_XLT_MISSING_CONT;
  else if (pList == NULL)
    return SML_ERR_OK;
  
  //encode the different list types
  switch ((int)listId)
  {
    case ITEM_LIST:
    {
      do
      {        
        if ((_err = xltEncBlock(TN_ITEM, OPTIONAL, ((SmlItemListPtr_t)pList)->item, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
        pList = ((SmlItemListPtr_t)pList)->next;
      } while ((SmlItemListPtr_t)pList != NULL);

      break;
    }
    case SOURCE_LIST:
    {
      do
      {        
        if ((_err = xltEncBlock(TN_SOURCE, OPTIONAL, ((SmlSourceListPtr_t)pList)->source, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
        pList = ((SmlSourceListPtr_t)pList)->next;
      } while ((SmlSourceListPtr_t)pList != NULL);

      break;
    }
		case TARGETREF_LIST:
    {
      do
      {        
        if ((_err = xltEncBlock(TN_TARGETREF, OPTIONAL, ((SmlTargetRefListPtr_t)pList)->targetRef, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
        pList = ((SmlTargetRefListPtr_t)pList)->next;
      } while ((SmlTargetRefListPtr_t)pList != NULL);

      break;
    }
    case SOURCEREF_LIST:
    {
      do
      {        
        if ((_err = xltEncBlock(TN_SOURCEREF, OPTIONAL, ((SmlSourceRefListPtr_t)pList)->sourceRef, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
        pList = ((SmlSourceRefListPtr_t)pList)->next;
      } while ((SmlSourceRefListPtr_t)pList != NULL);

      break;
    }
    case MAPITEM_LIST:
    {
      do
      {        
        if ((_err = xltEncBlock(TN_MAPITEM, OPTIONAL, ((SmlMapItemListPtr_t)pList)->mapItem, enc, pBufMgr, attFlag)) != SML_ERR_OK) return _err;
        pList = ((SmlMapItemListPtr_t)pList)->next;
      } while ((SmlMapItemListPtr_t)pList != NULL);

      break;
    }
    default:
    return SML_ERR_XLT_INVAL_LIST_TYPE;
    }

  return SML_ERR_OK;
}

/**
 * FUNCTION: xltGenerateTag
 *
 * Generates a (WB)XML tag
 *
 * PRE-Condition:   valis parameters
 *
 * POST-Condition:  the buffer contains a new tag
 *
 * IN:              tagId, the tag ID
 *                  TagType, the tag type (begin tag, end tag, ...)
 *                  enc, the encoding constant (SML_WBXML or SML_XML)                  
 *                  attFlag, indicates if the encoded tag contain Attributes in namespace extensions
 * 
 * IN/OUT:          pBufMgr, pointer to a structure containing buffer management elements
 * 
 * RETURN:          shows error codes of function, 
 *                  0, if OK
 */
Ret_t xltGenerateTag(XltTagID_t tagId, XltTagType_t TagType, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr, SmlPcdataExtension_t attFlag)
{

  Ret_t _err;
#ifdef __SML_WBXML__
  MemByte_t _switchpage = XLT_SWITCHPAGE;
#endif
  
  switch ((int)enc) {
#ifdef __SML_WBXML__
    case SML_WBXML:
        /* in WBXML codepage switches are done for starting tags only */
        if (TagType != TT_END) {
      //codepage switching with wbxml instead of namespace
      if (getCodePage(attFlag) != getCodePage(pBufMgr->smlCurExt)) {
        MemByte_t _newcp      = getCodePage(attFlag);
        if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_switchpage), TAG, 1, pBufMgr)) != SML_ERR_OK) return _err;
        if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_newcp), TAG, 1, pBufMgr)) != SML_ERR_OK) return _err;
      }
      
      if (attFlag != pBufMgr->smlCurExt) {
                pBufMgr->switchExtTag = tagId;
                pBufMgr->smlLastExt = pBufMgr->smlCurExt;
                pBufMgr->smlCurExt = attFlag;
            }
		} // for TagType
      return wbxmlGenerateTag(tagId, TagType, pBufMgr);    
#endif
#ifdef __SML_XML__
    case SML_XML:

        if (attFlag != pBufMgr->smlCurExt) {
			pBufMgr->switchExtTag = tagId;
			pBufMgr->smlLastExt = pBufMgr->smlCurExt;
            pBufMgr->smlCurExt = attFlag;
        }
		return xmlGenerateTag(tagId, TagType, pBufMgr, attFlag);
#endif
    default:
      return SML_ERR_XLT_ENC_UNK;
  }

  //return SML_ERR_XLT_ENC_UNK;NOT NEEDED
}

#ifdef __USE_EXTENSIONS__
/* Entrypoint for SubDTD's. If we reached this point we already know 
 * a) we have data fora sub-DTD to encode and
 * b) we know which sub-DTD should be encoded.
 * So just call the appropriate sub-DTD encoder and thats it.
 */
Ret_t xltBuildExtention(SmlPcdataExtension_t extId, XltRO_t reqOptFlag, VoidPtr_t pContent, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr) {

	switch (extId) {
#ifdef __USE_METINF__
	case SML_EXT_METINF:
		/* a metaInf DTD always starts with this token */
		return metinfEncBlock(TN_METINF_METINF,reqOptFlag,pContent,enc,pBufMgr,SML_EXT_METINF);
		break;
#endif 
#ifdef __USE_DEVINF__
	case SML_EXT_DEVINF:
		/* a deviceInf DTD always starts with this token */
        /* we have to choose, wether we have to encode the DevInf as XML or WBXML */
        /* in the latter case, we need a special treatment of this sub-dtd, as we have */
        /* to put it into a SML_PCDATA_OPAQUE field ... */
        if (enc == SML_XML)
		    return devinfEncBlock(TN_DEVINF_DEVINF,reqOptFlag,pContent,enc,pBufMgr,SML_EXT_DEVINF);
        else
            return subdtdEncWBXML(TN_DEVINF_DEVINF,reqOptFlag,pContent,SML_WBXML,pBufMgr,SML_EXT_DEVINF);
		break;
#endif 
	/* oops - we don not know about that extension -> bail out */
	default:
		return SML_ERR_XLT_INVAL_EXT;
  }
  //return SML_ERR_OK;CAN NOT BE REACHED
}


/* Sub DTD's need a special treatment when used together with WBXML.
 * We need to eoncode them as a complete WBXML message including headers and stuff
 * and store the result within an SML_PCDATA_OPAQUE datafield.
 * To archieve this we create a new encoder, encode the message and finally
 * copy the result into the allready existing encoder.
 */
#ifdef __SML_WBXML__
Ret_t subdtdEncWBXML(XltTagID_t tagId, XltRO_t reqOptFlag, const VoidPtr_t pContent, SmlEncoding_t enc, BufferMgmtPtr_t pBufMgr, SmlPcdataExtension_t attFlag)
{
    #ifdef __USE_DEVINF__
       Ret_t             _err         = SML_ERR_OK;
    #endif

    Short_t           SubBufSize   = 12000; // for starters we use 12kB for each sub DTD to encode in WBXML
    BufferMgmtPtr_t   pSubBufMgr   = NULL;

    // first create a sub buffer
    pSubBufMgr = (BufferMgmtPtr_t)smlLibMalloc(sizeof(BufferMgmt_t));
    if (pSubBufMgr == NULL) {
 		if (enc && pContent && reqOptFlag && tagId) {
 		}
 		return SML_ERR_NOT_ENOUGH_SPACE;}

    smlLibMemset(pSubBufMgr, 0,sizeof(BufferMgmt_t));
    pSubBufMgr->smlXltBufferLen     = SubBufSize;
    pSubBufMgr->smlXltBufferP       = (MemPtr_t)smlLibMalloc(SubBufSize);
    if (pSubBufMgr->smlXltBufferP == NULL) {
 		smlLibFree(pSubBufMgr);
 		return SML_ERR_NOT_ENOUGH_SPACE;
 	}

    smlLibMemset(pSubBufMgr->smlXltBufferP, 0, SubBufSize);
    pSubBufMgr->smlXltStoreBufP     = pSubBufMgr->smlXltBufferP;
    pSubBufMgr->smlXltWrittenBytes  = 0;
    pSubBufMgr->smlActiveExt        = pBufMgr->smlActiveExt;
    pSubBufMgr->smlCurExt           = pBufMgr->smlCurExt;
    pSubBufMgr->smlLastExt          = pBufMgr->smlLastExt;
	pSubBufMgr->spaceEvaluation     = pBufMgr->spaceEvaluation;
    
	// in case of space evaluation, just count the number of written bytes
	if (pSubBufMgr->spaceEvaluation == 0) {
      // create the WBXML header
      pSubBufMgr->smlXltBufferP[0]    = 0x02; // WBXML Version 1.2
      pSubBufMgr->smlXltBufferP[1]    = 0x00; // use Stringtable for ID
      pSubBufMgr->smlXltBufferP[2]    = 0x00; // empty/unknown public ID
      pSubBufMgr->smlXltBufferP[3]    = 0x6A; // charset encoding UTF-8
      pSubBufMgr->smlXltBufferP[4]    = 0x1D; // lenght of stringtable
      pSubBufMgr->smlXltBufferP      += 5;
      smlLibMemmove(pSubBufMgr->smlXltBufferP, "-//SYNCML//DTD DevInf 1.0//EN", 0x1d);
      pSubBufMgr->smlXltBufferP      += 0x1d;
	}
	pSubBufMgr->smlXltWrittenBytes  = 5 + 0x1d;

    // do the encoding
	switch (attFlag) {
#ifdef __USE_DEVINF__
	case SML_EXT_DEVINF:
		if ((_err = devinfEncBlock(TN_DEVINF_DEVINF,reqOptFlag,pContent,enc,pSubBufMgr,SML_EXT_DEVINF)) != SML_ERR_OK) {
            smlLibFree(pSubBufMgr->smlXltStoreBufP);
            smlLibFree(pSubBufMgr);
            return _err;
        }
		break;
#endif 
	/* oops - we don not know about that extension -> bail out */
	default:
		smlLibFree(pSubBufMgr->smlXltStoreBufP);
        smlLibFree(pSubBufMgr);
		return SML_ERR_XLT_INVAL_EXT;
    }
    
#ifdef __USE_DEVINF__
    // move it to the 'real' encoder buffer
    // now set up the OPAQUE field
	if (pBufMgr->spaceEvaluation == 0) {
      pBufMgr->smlXltBufferP[0]     = 0xC3; // OPAQUE data identifier
      pBufMgr->smlXltBufferP       += 1;

	  wbxmlOpaqueSize2Buf(pSubBufMgr->smlXltWrittenBytes, pBufMgr);

      smlLibMemmove(pBufMgr->smlXltBufferP, pSubBufMgr->smlXltStoreBufP, pSubBufMgr->smlXltWrittenBytes);
	  pBufMgr->smlXltBufferP      += pSubBufMgr->smlXltWrittenBytes;
      pBufMgr->smlXltWrittenBytes += pSubBufMgr->smlXltWrittenBytes;
	} else {
	  pBufMgr->smlXltWrittenBytes++;
	  wbxmlOpaqueSize2Buf(pSubBufMgr->smlXltWrittenBytes, pBufMgr);
      pBufMgr->smlXltWrittenBytes += pSubBufMgr->smlXltWrittenBytes;
	}
    
    // clean up the temporary stuff
    smlLibFree(pSubBufMgr->smlXltStoreBufP);
    smlLibFree(pSubBufMgr);

    return _err;
#endif
}
#endif

/*
 * Encode SyncML TNDS. 
 */
Ret_t xltEncDDFInit(SmlEncoding_t enc,
                    const MemPtr_t pBufEnd,
                    MemPtr_t *ppBufPos,
                    XltEncoderPtr_t *ppEncoder)
{
  // Return variable
  Ret_t _err;

  XltEncoderPtr_t _pEncoder;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

#ifdef __SML_WBXML__
    MemByte_t _stablen     =  0x1A; //XLT_STABLEN;
    MemByte_t _wbxmlver    = XLT_WBXMLVER;
    MemByte_t _charset     = XLT_CHARSET;
    MemByte_t _pubident1   = XLT_PUBIDENT1;
    MemByte_t _pubident2   = XLT_PUBIDENT2;
    //MemByte_t _syncmldtd[] = "-//OMA//DTD-DM-DDF 1.1//EN";
    MemByte_t _syncmldtd[] = "-//OMA//DTD-DM-DDF 1.2//EN";
#endif

#ifdef __SML_XML__
    MemPtr_t _tmpStr;
    MemPtr_t _xmlver = (MemPtr_t)XML_VERSION;
    MemPtr_t _xmlenc = (MemPtr_t)XML_ENCODING;
    MemByte_t _begpar = XML_BEGPAR;
    MemByte_t _endpar = XML_ENDPAR;
#endif

  //MemByte_t _tmp = 0x00;CURRENTLY NOT USED

  if ((_pEncoder = (XltEncoderPtr_t)smlLibMalloc(sizeof(XltEncoder_t))) == NULL) return SML_ERR_NOT_ENOUGH_SPACE;
  if ((_pBufMgr = (BufferMgmtPtr_t)smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) {
      smlLibFree(_pEncoder);
      return SML_ERR_NOT_ENOUGH_SPACE; 
  }

  //set the encoding
  _pEncoder->enc = enc;

  _pEncoder->cur_ext = (SmlPcdataExtension_t)SML_EXT_UNDEFINED;
  _pEncoder->last_ext = (SmlPcdataExtension_t)SML_EXT_UNDEFINED;
  _pEncoder->end_tag_size = 0;
  _pEncoder->space_evaluation = NULL;
  

  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltWrittenBytes = 0;  
  _pBufMgr->smlCurExt = _pEncoder->cur_ext;
  _pBufMgr->smlLastExt = _pEncoder->last_ext;
  _pBufMgr->smlActiveExt = SML_EXT_UNDEFINED;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = 0;
  _pBufMgr->endTagSize = 0;

  switch (enc)
  {

#ifdef __SML_WBXML__
    case SML_WBXML:
    {

      // Set the WBXML Header Values
      // WBXML Version
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_wbxmlver), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Public Idetifier - default unknown
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_pubident1), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_pubident2), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Character set - not yet implemented
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_charset), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;
      // Sting table length -  not yet implemented
      if ((_err = wbxmlWriteTypeToBuffer((MemPtr_t)(&_stablen), TAG, 1, _pBufMgr)) != SML_ERR_OK) break;

      if ((_err = xltAddToBuffer((MemPtr_t) (&_syncmldtd) , smlLibStrlen((String_t)_syncmldtd), _pBufMgr)) != SML_ERR_OK) break;      
      _err = SML_ERR_OK;
      break;
    }
#endif

#ifdef __SML_XML__
    case SML_XML:
    {    
      if ((_err = xltAddToBuffer((MemPtr_t)(&_begpar), 1, _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"?xml version=\"";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = _xmlver;
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"\" encoding=\"";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = _xmlenc;
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      _tmpStr = (MemPtr_t)"\"?";
      if ((_err = xltAddToBuffer(_tmpStr, smlLibStrlen((String_t)_tmpStr), _pBufMgr)) != SML_ERR_OK) break;
      if ((_err = xltAddToBuffer((MemPtr_t)(&_endpar), 1, _pBufMgr)) != SML_ERR_OK) break;
      _err = SML_ERR_OK;
      break;
    }
#endif

    default:
    {
      _err = SML_ERR_XLT_ENC_UNK;
    }
  }
  if (_err != SML_ERR_OK)
  {
     smlLibFree(_pBufMgr);
     smlLibFree(_pEncoder);
     return _err;
  }

  // SyncML Tag   
  if ((_err = xltGenerateTag(TN_TNDS_SYNCML, TT_BEG, enc, _pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    smlLibFree(_pEncoder);
    return _err;
  }

  _pEncoder->cur_ext = _pBufMgr->smlCurExt;
  _pEncoder->last_ext = _pBufMgr->smlLastExt;
  _pEncoder->end_tag_size = _pBufMgr->endTagSize;

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  _pEncoder->final = 0;

  *ppEncoder = (XltEncoderPtr_t)_pEncoder;

  return SML_ERR_OK;
}

Ret_t xltEncDDFTerminate(const XltEncoderPtr_t pEncoder,
                         const MemPtr_t pBufEnd,
                         MemPtr_t *ppBufPos)
{
  // Return variable      
  Ret_t _err;

  // encoding type
  SmlEncoding_t _enc;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

  //get the encoding type
  _enc = pEncoder->enc;

  //Initialize buffer variables
  if ((_pBufMgr = smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) {
 	  smlLibFree(pEncoder);
 	  return SML_ERR_NOT_ENOUGH_SPACE;
   }

  _pBufMgr->smlXltWrittenBytes = 0;      
  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlCurExt = pEncoder->cur_ext;
  _pBufMgr->smlLastExt = pEncoder->last_ext;
  _pBufMgr->smlActiveExt = pEncoder->cur_ext;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = ((pEncoder->space_evaluation == NULL) ? 0 : 1);
  _pBufMgr->endTagSize =0;

  // SyncML End Tag       
  if ((_err = xltGenerateTag(TN_TNDS_SYNCML, TT_END, _enc, _pBufMgr, SML_EXT_TNDS)) != SML_ERR_OK)
  {
    smlLibFree(_pBufMgr);
    xltEncReset(pEncoder);
    return _err;
  }

  pEncoder->cur_ext = _pBufMgr->smlCurExt;
  pEncoder->last_ext = _pBufMgr->smlLastExt;

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  xltEncReset(pEncoder);

  return SML_ERR_OK;
}

Ret_t xltEncDDFAppend(const XltEncoderPtr_t pEncoder, 
                      const MemPtr_t pBufEnd,
                      const VoidPtr_t pContent,
                      MemPtr_t *ppBufPos)
{
  // Return variable
  Ret_t _err;

  XltTagID_t tagID = TN_TNDS_TREE;

  // encoding type
  SmlEncoding_t _enc;

  //Structure containing buffer pointers, length and written bytes
  BufferMgmtPtr_t _pBufMgr;

  if ((_pBufMgr = (BufferMgmtPtr_t)smlLibMalloc(sizeof(BufferMgmt_t))) == NULL) return SML_ERR_NOT_ENOUGH_SPACE;
  smlLibMemset(_pBufMgr, 0, sizeof(BufferMgmt_t));

  //get the encoding type
  _enc = pEncoder->enc;
  
  _pBufMgr->smlXltBufferP = *ppBufPos;
  _pBufMgr->smlXltBufferLen = pBufEnd - *ppBufPos;
  _pBufMgr->smlXltStoreBufP = _pBufMgr->smlXltBufferP;
  _pBufMgr->smlXltWrittenBytes = 0;      
  _pBufMgr->smlCurExt = pEncoder->cur_ext;
  _pBufMgr->smlLastExt = pEncoder->last_ext;
  _pBufMgr->smlActiveExt = SML_EXT_TNDS;
  _pBufMgr->switchExtTag = TN_UNDEF;
  _pBufMgr->spaceEvaluation = ((pEncoder->space_evaluation == NULL) ? 0 : 1);
  _pBufMgr->endTagSize =0;
	  
  _err = xltEncBlock(tagID, REQUIRED, pContent, _enc, _pBufMgr, SML_EXT_TNDS);
  if (_err != SML_ERR_OK)
  {
     smlLibFree(_pBufMgr);
     return _err;
  }  

  pEncoder->cur_ext = _pBufMgr->smlCurExt;
  pEncoder->last_ext = _pBufMgr->smlLastExt;

  if (pEncoder->space_evaluation != NULL) {
	pEncoder->space_evaluation->written_bytes += _pBufMgr->smlXltWrittenBytes;
	pEncoder->space_evaluation->end_tag_size += _pBufMgr->endTagSize;
  } else {
	pEncoder->end_tag_size += _pBufMgr->endTagSize;
  }

  *ppBufPos = _pBufMgr->smlXltBufferP;

  smlLibFree(_pBufMgr);

  return SML_ERR_OK;
}

#endif
