1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SyncML_DM_WBXMLReader.H"
18#include "SyncML_DM_WBXMLArchive.H"
19#include "SyncML_Log.H"
20#include "SyncML_PlugIn_WBXMLLog.H"
21#include "dm_tree_util.h"
22#include "xpl_Logger.h"
23#include "dmprofile.h"
24
25/*==================================================================================================
26
27Function:    SyncML_DM_WBXMLReader::readByte
28
29Description: Reads a byte of data from the file handle
30
31==================================================================================================*/
32SYNCML_DM_RET_STATUS_T
33SyncML_DM_WBXMLReader::readByte(UINT8* byte) {
34    return this->fileHandle->read(byte, 1);
35}
36
37/*==================================================================================================
38
39Function:    SyncML_DM_WBXMLReader::readHeader
40
41Description: Reads the WBXML header from the file start.
42             We expect the exact 4 bytes that we serialize back out.
43
44==================================================================================================*/
45SYNCML_DM_RET_STATUS_T
46SyncML_DM_WBXMLReader::readHeader() {
47    UINT8 bYte;
48
49    /* Read in version */
50    if (this->readByte(&bYte) != SYNCML_DM_SUCCESS
51            || bYte != SyncML_DM_WBXMLArchive::WBXML_VERSION) {
52        return SYNCML_DM_IO_FAILURE;
53    }
54
55    /* Read in public identifier */
56    if (this->readByte(&bYte) != SYNCML_DM_SUCCESS
57            || bYte != SyncML_DM_WBXMLArchive::PUBLIC_ID) {
58        return SYNCML_DM_IO_FAILURE;
59    }
60
61    /* read charset (check for UTF-8 for all files) */
62    if (this->readByte(&bYte) != SYNCML_DM_SUCCESS
63            || bYte != SyncML_DM_WBXMLArchive::CHARSET) {
64        return SYNCML_DM_IO_FAILURE;
65    }
66
67    /* Read in string table length; better be zero, since not supported */
68    if (this->readByte(&bYte) != SYNCML_DM_SUCCESS
69            || bYte != 0) {
70        return SYNCML_DM_IO_FAILURE;
71    }
72
73    return SYNCML_DM_SUCCESS;
74}
75
76
77/*==================================================================================================
78
79Function:    SyncML_DM_WBXMLReader::readLen
80
81Description:
82
83==================================================================================================*/
84SYNCML_DM_RET_STATUS_T
85SyncML_DM_WBXMLReader::readLen(UINT32 *pLen) {
86    SYNCML_DM_RET_STATUS_T ret;
87    UINT8   bYte;
88
89    /* Read and decode the data length.
90     * Note! The first byte of the length not expected to have the continue bit set (in msb).
91     * If it is, the length is >=128 and this code does not know how to decode it.
92     */
93
94    if ((ret = this->readByte(&bYte)) != SYNCML_DM_SUCCESS) {
95        return ret;
96    }
97    *pLen = bYte & 0x7F;
98    while(bYte & 0x80) {
99        if ((ret = this->readByte(&bYte)) != SYNCML_DM_SUCCESS)
100            return ret;
101        *pLen = ( (*pLen)<<7) |(bYte & 0x7F);
102    }
103
104    /* If there is data, get a buffer, read the data, append '\0' */
105    if (*pLen>127)
106    {
107        XPL_LOG_DM_TMN_Debug(("*pLen=%d\n", *pLen));
108    }
109    return ret;
110}
111
112 /*==================================================================================================
113
114Function:    SyncML_DM_WBXMLReader::readOpaque
115
116Description: Reads opaquely encoded data expected to immediately follow the OPAQUE_CODE byte in the
117             WBXML file. This function expects a data length field, encoded as mb_u_int32
118             32 bit unsigned integer, encoded in multi-byte integer format, to precede the data.
119             The length is decoded and returned. The data may or may not be a string, and so no
120             assumptions about null termination are made while reading it. However, as a courtesy to
121             the caller, a null character is appended to the data read. The returned length does not
122             include this character.
123
124             Note! The implementation only supports encoded lengths <= 127.
125             An I/O error (SYNCML_DM_IO_FAILURE) is returned otherwise, and no data buffer is allocated
126             (NULL is returned).
127
128             If the encoded length is zero, no data buffer is allocated
129             (NULL is returned). The result is SYNCML_DM_SUCCESS.
130
131             The file pointer is automatically advanced by the underlying file reader service. Any
132             underlying file I/O error code is expected to be SYNCML_DM_IO_FAILURE.
133
134             The caller must free the data buffer.
135
136             Future enhancement: If incoming *pDATA is NULL, this function should allocate memory;
137             else, the caller has provided the memory, and *pLen indicates how large it is. This
138             would reduce memory churn.
139
140==================================================================================================*/
141SYNCML_DM_RET_STATUS_T
142SyncML_DM_WBXMLReader::readOpaque(UINT8 *pBuffer, UINT8 nSize) {
143    SYNCML_DM_RET_STATUS_T ret;
144    UINT32 nLen;
145
146    *pBuffer = 0;
147    if ((ret = readLen(&nLen)) != SYNCML_DM_SUCCESS)
148        return ret;
149    if ( nLen > nSize )
150        return SYNCML_DM_FAIL;
151    if (nLen > 0)
152        ret = fileHandle->read(pBuffer, nLen);
153    return ret;
154}
155
156 /*==================================================================================================
157
158Function:    SyncML_DM_WBXMLReader::readOpaque
159
160Description: Reads opaquely encoded data expected to immediately follow the OPAQUE_CODE byte in the
161             WBXML file. This function expects a data length field, encoded as mb_u_int32
162             32 bit unsigned integer, encoded in multi-byte integer format, to precede the data.
163             The length is decoded and returned. The data may or may not be a string, and so no
164             assumptions about null termination are made while reading it. However, as a courtesy to
165             the caller, a null character is appended to the data read. The returned length does not
166             include this character.
167
168             Note! The implementation only supports encoded lengths <= 127.
169             An I/O error (SYNCML_DM_IO_FAILURE) is returned otherwise, and no data buffer is allocated
170             (NULL is returned).
171
172             If the encoded length is zero, no data buffer is allocated
173             (NULL is returned). The result is SYNCML_DM_SUCCESS.
174
175             The file pointer is automatically advanced by the underlying file reader service. Any
176             underlying file I/O error code is expected to be SYNCML_DM_IO_FAILURE.
177
178             The caller must free the data buffer.
179
180             Future enhancement: If incoming *pDATA is NULL, this function should allocate memory;
181             else, the caller has provided the memory, and *pLen indicates how large it is. This
182             would reduce memory churn.
183
184==================================================================================================*/
185SYNCML_DM_RET_STATUS_T
186SyncML_DM_WBXMLReader::readOpaque(DMBuffer *pBuffer) {
187    SYNCML_DM_RET_STATUS_T ret;
188    UINT32 nLen;
189
190    pBuffer->clear();
191    if ((ret = readLen(&nLen)) != SYNCML_DM_SUCCESS) {
192        return ret;
193    }
194    if (nLen > 0)
195    {
196        pBuffer->allocate(nLen);
197        if ( pBuffer->getBuffer() != NULL )
198        {
199           ret = fileHandle->read(pBuffer->getBuffer(), nLen);
200           pBuffer->setSize(nLen);
201        }
202        else
203           return SYNCML_DM_DEVICE_FULL;
204    }
205
206    return ret;
207}
208
209
210/*==================================================================================================
211Function:    SyncML_DM_WBXMLReader::readNode
212
213Description: Reads a node and its properties from a WBXML file.  The NODE_START_TAG
214     is assumed to already have been read.  Exits after all properties have
215     been read, signaled by an END_TAG or the NODE_START_TAG of the next node.
216
217Returns: - The byte that caused the node to stop (END_TAG or NODE_START_TAG w/content bit)
218             - A node props structure, with:
219                node->pbURI set to NULL
220                node->pbACL possibly set to an Alloc'd string
221                The caller is responsible for freeing these structs (on error or success).
222
223              SYNCML_DM_TREE_CORRUPT is returned if any I/O error occurs,
224                    or an unexpected opcode is encountered, or an invalid length is encountered.
225
226==================================================================================================*/
227SYNCML_DM_RET_STATUS_T
228SyncML_DM_WBXMLReader::readNode(DMAddNodeProp* node, UINT8* stopByte) {
229
230    UINT8 bYte;
231    SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_FAIL;
232
233
234    /* Initialize the property data */
235    node->clear();
236
237    /* While a byte is read correctly */
238    while((ret_stat = this->readByte(&bYte)) == SYNCML_DM_SUCCESS) {
239
240        /* Switch on that byte as an indicator of the data to follow
241         * and copy that data.
242         */
243        switch(bYte) {
244        case (SyncML_DM_WBXMLArchive::END_TAG):
245        case (SyncML_DM_WBXMLArchive::NODE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
246            *stopByte = bYte;
247            return SYNCML_DM_SUCCESS;
248
249#ifdef LOB_SUPPORT
250        case (SyncML_DM_WBXMLArchive::ESN_File_NAME_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
251            if((ret_stat = this->readESNFileName(node, &bYte)) != SYNCML_DM_SUCCESS)
252               break;
253            continue;   /* Skip to top of while() */
254#endif
255        case (SyncML_DM_WBXMLArchive::NAME_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
256            if((ret_stat = this->readNodeName(node, &bYte)) != SYNCML_DM_SUCCESS)
257               break;
258            continue;   /* Skip to top of while() */
259
260        case (SyncML_DM_WBXMLArchive::ACL_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
261        case (SyncML_DM_WBXMLArchive::PLURAL_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
262        case (SyncML_DM_WBXMLArchive::URI_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
263            if((ret_stat = this->skipTag(&bYte)) != SYNCML_DM_SUCCESS)
264                break;
265            continue;   /* Skip to top of while() */
266
267        case (SyncML_DM_WBXMLArchive::ACCESS_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
268            if((ret_stat = this->readAccess(node, &bYte)) != SYNCML_DM_SUCCESS)
269                break;
270            continue;   /* Skip to top of while() */
271
272        case (SyncML_DM_WBXMLArchive::SCOPE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
273            if((ret_stat = this->readScope(node, &bYte)) != SYNCML_DM_SUCCESS)
274                break;
275            continue;   /* Skip to top of while() */
276
277        case (SyncML_DM_WBXMLArchive::CLASSID_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
278            if((ret_stat = this->readClassID(node, &bYte)) != SYNCML_DM_SUCCESS)
279                break;
280            continue;   /* Skip to top of while() */
281
282        case (SyncML_DM_WBXMLArchive::FORMAT_NEW_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
283            if((ret_stat = this->readFormat(node, &bYte)) != SYNCML_DM_SUCCESS)
284                break;
285            continue;   /* Skip to top of while() */
286
287        case (SyncML_DM_WBXMLArchive::FORMAT_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
288            if((ret_stat = this->readFormatOld(node, &bYte)) != SYNCML_DM_SUCCESS)
289                break;
290            continue;   /* Skip to top of while() */
291#ifdef LOB_SUPPORT
292            /* Handles an Empty NodeName */
293        case SyncML_DM_WBXMLArchive::ESN_File_NAME_START_TAG:
294            continue;   /* Skip to top of while() */
295#endif
296            /* Handles an Empty NodeName */
297        case SyncML_DM_WBXMLArchive::NAME_START_TAG:
298            continue;   /* Skip to top of while() */
299
300        case (SyncML_DM_WBXMLArchive::TYPE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
301            if((ret_stat = this->readMime(node, &bYte)) != SYNCML_DM_SUCCESS)
302                break;
303            continue;   /* Skip to top of while() */
304
305        case (SyncML_DM_WBXMLArchive::DATA_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
306            if((ret_stat = this->readData(node, &bYte)) != SYNCML_DM_SUCCESS)
307                 break;
308            continue;   /* Skip to top of while() */
309
310        case (SyncML_DM_WBXMLArchive::TITLE_START_TAG |SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
311            if((ret_stat = this->readTitle(node, &bYte)) != SYNCML_DM_SUCCESS)
312                 break;
313            continue;   /* Skip to top of while() */
314
315#ifndef DM_IGNORE_TSTAMP_AND_VERSION
316
317        case (SyncML_DM_WBXMLArchive::VERSION_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
318            if((ret_stat = this->readVersion(node, &bYte)) != SYNCML_DM_SUCCESS)
319                break;
320            continue;   /* Skip to top of while() */
321          // dp: compact format
322        case (SyncML_DM_WBXMLArchive::TSTAMP_INT_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
323          if((ret_stat = this->readIntTStamp(node, &bYte)) != SYNCML_DM_SUCCESS)
324               break;
325           continue;
326
327#endif
328
329        case (SyncML_DM_WBXMLArchive::FLAGS_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
330            if((ret_stat = this->readFlag(node, &bYte)) != SYNCML_DM_SUCCESS)
331                break;
332
333          continue;
334
335        case (SyncML_DM_WBXMLArchive::OPI_DATA_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK):
336            if((ret_stat = this->readOPiData(node, &bYte)) != SYNCML_DM_SUCCESS)
337                break;
338
339          continue;
340
341        default:    /* Do not expect to get here */
342          ret_stat = SYNCML_DM_FAIL;
343            break;
344        }
345
346        /* If we get here, then either none of the continue statements in the switch happened,
347         * or else an unexpected byte value was read (and we exited the switch via a break).
348         * This next break will leave the while() loop, adn the fcn exits with an error.
349         */
350        break;
351    }
352
353    return ret_stat;
354}
355/*==================================================================================================
356
357Function:    SyncML_DM_WBXMLReader::readOneLogRecord
358
359Description: The DMAddNodeProp and URI fields are completed by this function
360         on successful return.  This function calls SyncML_DM_WBXMLReader::readNode() in order
361             to fill in the property data for the node.
362
363Memory policy: The caller is responsible for freeing the created objects (on error or success).
364
365Notes:  The DMAddNodeProp.pbURI field is not filled by this function
366
367==================================================================================================*/
368
369SYNCML_DM_RET_STATUS_T
370SyncML_DM_WBXMLReader::readOneLogRecord(SYNCML_DM_PLUGIN_COMMAND_T* cmdType,
371                                        DMBuffer  *cmdURI,
372                                        SYNCML_DM_PLUGIN_COMMAND_ATTRIBUTE_T * attribute,
373                                        DMAddNodeProp* props,
374                                        UINT8* stopByte)
375{
376
377    UINT8 bYte;
378    DMString  tmpStr;
379
380    /* Default values */
381    *cmdType = SYNCML_DM_PLUGIN_NO_COMMAND;
382    props->clear();
383    SYNCML_DM_RET_STATUS_T ret_code = SYNCML_DM_FAIL;
384
385    //Read command type start tag
386    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
387        || bYte != (SyncML_DM_WBXMLArchive::CMDTYPE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
388        return SYNCML_DM_IO_FAILURE;
389
390    //Read command type
391    if((ret_code = this->readByte(cmdType)) != SYNCML_DM_SUCCESS)
392      return SYNCML_DM_IO_FAILURE;
393
394    //Read END_TAG
395    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS ||
396        bYte != SyncML_DM_WBXMLArchive::END_TAG)
397        return SYNCML_DM_IO_FAILURE;
398
399    //Read command type start tag
400    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
401        || bYte != (SyncML_DM_WBXMLArchive::URI_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
402        return SYNCML_DM_IO_FAILURE;
403
404    //Read command type start tag
405    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
406        || bYte != SyncML_DM_WBXMLArchive::OPAQUE_CODE)
407        return SYNCML_DM_IO_FAILURE;
408
409    // Read URI
410    if((ret_code = this->readOpaque(cmdURI)) != SYNCML_DM_SUCCESS)
411        return SYNCML_DM_IO_FAILURE;
412
413    //Read END_TAG
414    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
415        || bYte != SyncML_DM_WBXMLArchive::END_TAG)
416        return SYNCML_DM_IO_FAILURE;
417
418    switch ( *cmdType )
419    {
420        case SYNCML_DM_PLUGIN_ADD:
421            //Read command type start tag
422            if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
423                || bYte != (SyncML_DM_WBXMLArchive::NODE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
424                return SYNCML_DM_IO_FAILURE;
425            if((ret_code = this->readNode(props, stopByte)) != SYNCML_DM_SUCCESS)
426                return SYNCML_DM_IO_FAILURE;
427            break;
428
429        case SYNCML_DM_PLUGIN_DELETE:
430        case SYNCML_DM_PLUGIN_ADD_CHILD:
431            if((ret_code = this->readByte(stopByte)) != SYNCML_DM_SUCCESS
432                || *stopByte != SyncML_DM_WBXMLArchive::END_TAG)
433                return SYNCML_DM_IO_FAILURE;
434            break;
435
436       case SYNCML_DM_REPLACE:
437            if((ret_code = this->readByte(attribute)) != SYNCML_DM_SUCCESS)
438                return SYNCML_DM_IO_FAILURE;
439            switch(*attribute)
440            {
441                case SYNCML_DM_PLUGIN_COMMAND_ON_NODE:
442                    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
443                        || bYte != (SyncML_DM_WBXMLArchive::DATA_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
444                        return SYNCML_DM_IO_FAILURE;
445
446                    if((ret_code = this->readData(props, stopByte)) != SYNCML_DM_SUCCESS)
447                        return SYNCML_DM_IO_FAILURE;
448                    break;
449
450
451                case SYNCML_DM_PLUGIN_COMMAND_ON_NAME_PROPERTY:
452                    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
453                        || bYte != (SyncML_DM_WBXMLArchive::NAME_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
454                        return SYNCML_DM_IO_FAILURE;
455
456                    if((ret_code = this->readNodeName(props, stopByte)) != SYNCML_DM_SUCCESS)
457                        return SYNCML_DM_IO_FAILURE;
458                    break;
459
460                case SYNCML_DM_PLUGIN_COMMAND_ON_TITLE_PROPERTY:
461                    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
462                        || bYte != (SyncML_DM_WBXMLArchive::TITLE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
463                        return SYNCML_DM_IO_FAILURE;
464                    if((ret_code = this->readTitle(props, stopByte)) != SYNCML_DM_SUCCESS)
465                        return SYNCML_DM_IO_FAILURE;
466                    break;
467
468#ifdef LOB_SUPPORT
469		    case SYNCML_DM_PLUGIN_COMMAND_ON_LOB_PROPERTY:
470			if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS)
471				return SYNCML_DM_IO_FAILURE;
472
473			if(bYte == (SyncML_DM_WBXMLArchive::ESN_File_NAME_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
474			{	if((ret_code = this->readESNFileName(props, stopByte)) != SYNCML_DM_SUCCESS)
475					return SYNCML_DM_IO_FAILURE;
476			}
477			else
478				if(bYte != SyncML_DM_WBXMLArchive::END_TAG)
479					return SYNCML_DM_IO_FAILURE;
480
481			break;
482#endif
483                default:
484                    return SYNCML_DM_IO_FAILURE;
485            }
486            break;
487
488
489        default:
490            return SYNCML_DM_IO_FAILURE;
491    }
492
493    return SYNCML_DM_SUCCESS;
494
495}
496/*==================================================================================================
497
498Function:    SyncML_DM_WBXMLReader::readOneCommitLogRecord
499
500Description: Read one record from commit log file
501
502==================================================================================================*/
503SYNCML_DM_RET_STATUS_T SyncML_DM_WBXMLReader::readOneCommitLogRecord(SYNCML_DM_COMMAND_T* cmdType,
504											DMBuffer  *sourceFileName,
505											DMBuffer  *targetFileName,
506											UINT8* /*stopByte */)
507{
508
509    UINT8 bYte;
510    DMString  tmpStr;
511
512    /* Default values */
513    *cmdType = SYNCML_DM_NO_COMMAND;
514
515    SYNCML_DM_RET_STATUS_T ret_code = SYNCML_DM_FAIL;
516
517    //Read command type start tag
518    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
519        || bYte != (SyncML_DM_WBXMLArchive::CMDTYPE_START_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
520		        return SYNCML_DM_IO_FAILURE;
521
522    //Read command type
523    if((ret_code = this->readByte(cmdType)) != SYNCML_DM_SUCCESS)
524      return SYNCML_DM_IO_FAILURE;
525
526    //Read END_TAG
527    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS ||
528        bYte != SyncML_DM_WBXMLArchive::END_TAG)
529        return SYNCML_DM_IO_FAILURE;
530
531    //Read command type start tag
532    if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
533        || bYte != (SyncML_DM_WBXMLArchive::TARGET_FILE_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
534        return SYNCML_DM_IO_FAILURE;
535
536    //Read target file name
537    if((ret_code = this->readOpaqueTag(&bYte, targetFileName)) != SYNCML_DM_SUCCESS)
538        return SYNCML_DM_IO_FAILURE;
539
540    if( *cmdType == SYNCML_DM_REPLACE)
541    {
542		if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS)
543			return SYNCML_DM_IO_FAILURE;
544		// Is the original file name exist?
545		if(bYte == (SyncML_DM_WBXMLArchive::SOURCE_FILE_TAG | SyncML_DM_WBXMLArchive::TAG_CONTENT_MASK))
546		{
547			// Read source  file name
548			if((ret_code = this->readOpaqueTag(&bYte, sourceFileName)) != SYNCML_DM_SUCCESS)
549					return SYNCML_DM_IO_FAILURE;
550
551			//Read END_TAG
552			if((ret_code = this->readByte(&bYte)) != SYNCML_DM_SUCCESS
553				|| bYte != SyncML_DM_WBXMLArchive::END_TAG)
554				return SYNCML_DM_IO_FAILURE;
555		}
556    }
557    return SYNCML_DM_SUCCESS;
558
559}
560
561/*==================================================================================================
562
563Function:    SyncML_DM_WBXMLReader::operator new
564
565Description: Allocate memory for this object
566
567Memory policy: The caller is responsible to delete (free) the new object (alloc'd memory)
568
569==================================================================================================*/
570void *
571SyncML_DM_WBXMLReader::operator new(size_t sz) {
572    return (DmAllocMem(sz));
573}
574
575/*==================================================================================================
576
577Function:    SyncML_DM_WBXMLReader::operator delete
578
579Description: De-allocate memory for this object
580
581==================================================================================================*/
582void
583SyncML_DM_WBXMLReader::operator delete (void *buf) {
584    DmFreeMem(buf);
585}
586
587
588SYNCML_DM_RET_STATUS_T
589SyncML_DM_WBXMLReader::readOpaqueTag(UINT8* pByte, UINT8 *pBuffer, UINT8 nSize) {
590
591    SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_SUCCESS;
592
593    if((ret_stat = this->readByte(pByte)) != SYNCML_DM_SUCCESS)
594        return ret_stat;
595
596    if(*pByte != SyncML_DM_WBXMLArchive::OPAQUE_CODE)
597        return SYNCML_DM_TREE_CORRUPT;
598
599    ret_stat = this->readOpaque(pBuffer, nSize);
600    if( ret_stat != SYNCML_DM_SUCCESS )
601        return ret_stat;
602
603    return checkNextByteIsEndTag(pByte);
604
605}
606
607SYNCML_DM_RET_STATUS_T
608SyncML_DM_WBXMLReader::readOpaqueTag(UINT8* pByte, DMBuffer *pBuffer) {
609
610    SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_SUCCESS;
611
612    if((ret_stat = this->readByte(pByte)) != SYNCML_DM_SUCCESS)
613        return ret_stat;
614
615    if(*pByte != SyncML_DM_WBXMLArchive::OPAQUE_CODE)
616        return SYNCML_DM_TREE_CORRUPT;
617
618    ret_stat = this->readOpaque(pBuffer);
619    if ( ret_stat != SYNCML_DM_SUCCESS )
620        return ret_stat;
621
622    return checkNextByteIsEndTag(pByte);
623}
624
625
626SYNCML_DM_RET_STATUS_T
627SyncML_DM_WBXMLReader::checkNextByteIsEndTag(UINT8* pByte)
628{
629    SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_SUCCESS;
630
631    if((ret_stat = this->readByte(pByte)) != SYNCML_DM_SUCCESS)
632        return ret_stat;
633    if(*pByte != SyncML_DM_WBXMLArchive::END_TAG)
634        return  SYNCML_DM_TREE_CORRUPT;
635
636    return ret_stat;
637}
638
639
640SYNCML_DM_RET_STATUS_T
641SyncML_DM_WBXMLReader::skipTag(UINT8* pByte)
642{
643    DMBuffer property;
644
645    return this->readOpaqueTag(pByte,&property);
646}
647
648#ifdef LOB_SUPPORT
649SYNCML_DM_RET_STATUS_T
650SyncML_DM_WBXMLReader::readESNFileName(DMAddNodeProp* nodeProps, UINT8* pByte)
651{
652    return this->readOpaqueTag(pByte, &nodeProps->m_oESNFileName);
653}
654#endif
655
656SYNCML_DM_RET_STATUS_T
657SyncML_DM_WBXMLReader::readNodeName(DMAddNodeProp* nodeProps, UINT8* pByte)
658{
659
660    return this->readOpaqueTag(pByte, &nodeProps->m_oName);
661
662}
663
664
665SYNCML_DM_RET_STATUS_T
666SyncML_DM_WBXMLReader::readAccess(DMAddNodeProp* /*nodeProps*/, UINT8* pByte)
667{
668    UINT8 property[2];
669
670    return this->readOpaqueTag(pByte, property,2);
671
672}
673
674SYNCML_DM_RET_STATUS_T
675SyncML_DM_WBXMLReader::readScope(DMAddNodeProp* nodeProps, UINT8* pByte)
676{
677    SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_SUCCESS;
678    UINT8 bScope = 0;
679
680    if((ret_stat = this->readOpaqueTag(pByte, &bScope,1)) != SYNCML_DM_SUCCESS)
681        return ret_stat;
682
683    if ( bScope == DMTNM_NODE_PERMANENT )
684      nodeProps->m_nFlags |= DMNode::enum_NodePermanent;
685
686    return ret_stat;
687}
688
689
690SYNCML_DM_RET_STATUS_T
691SyncML_DM_WBXMLReader::readClassID(DMAddNodeProp* /*nodeProps*/, UINT8* pByte)
692{
693    UINT8 property[2];
694
695    return this->readOpaqueTag(pByte, property,2);
696}
697
698SYNCML_DM_RET_STATUS_T
699SyncML_DM_WBXMLReader::readFormat(DMAddNodeProp* nodeProps, UINT8* pByte)
700{
701    return readOpaqueTag(pByte, &nodeProps->m_nFormat,1);
702}
703
704SYNCML_DM_RET_STATUS_T
705SyncML_DM_WBXMLReader::readFormatOld(DMAddNodeProp* nodeProps, UINT8* pByte)
706{
707    SYNCML_DM_FORMAT_T nOldFormat = 0;
708    SYNCML_DM_RET_STATUS_T nRes = readOpaqueTag(pByte, &nOldFormat,1);
709
710    static const SYNCML_DM_FORMAT_T aFormats[] = {
711        SYNCML_DM_FORMAT_BIN,
712        SYNCML_DM_FORMAT_BOOL,
713        SYNCML_DM_FORMAT_B64,
714        SYNCML_DM_FORMAT_CHR,
715        SYNCML_DM_FORMAT_INT,
716        SYNCML_DM_FORMAT_NODE,
717        SYNCML_DM_FORMAT_NULL,
718        SYNCML_DM_FORMAT_XML,
719        SYNCML_DM_FORMAT_INVALID,
720        SYNCML_DM_FORMAT_TEST,
721        SYNCML_DM_FORMAT_FLOAT,
722        SYNCML_DM_FORMAT_DATE,
723        SYNCML_DM_FORMAT_TIME
724
725      };
726
727    if ( nRes == SYNCML_DM_SUCCESS && nOldFormat < DIM(aFormats) )
728        nodeProps->m_nFormat = aFormats[nOldFormat];
729    else
730      nodeProps->m_nFormat = SYNCML_DM_FORMAT_NODE;
731
732    return nRes;
733}
734
735SYNCML_DM_RET_STATUS_T
736SyncML_DM_WBXMLReader::readMime(DMAddNodeProp* nodeProps, UINT8* pByte)
737{
738    return this->readOpaqueTag(pByte, &nodeProps->m_oMimeType);
739}
740
741SYNCML_DM_RET_STATUS_T
742SyncML_DM_WBXMLReader::readData(DMAddNodeProp* nodeProps, UINT8* pByte)
743{
744    return this->readOpaqueTag(pByte, &nodeProps->m_oData);
745}
746
747SYNCML_DM_RET_STATUS_T
748SyncML_DM_WBXMLReader::readOPiData(DMAddNodeProp* nodeProps, UINT8* pByte)
749{
750    return this->readOpaqueTag(pByte, &nodeProps->m_oOPiData);
751}
752
753SYNCML_DM_RET_STATUS_T
754SyncML_DM_WBXMLReader::readTitle(DMAddNodeProp* nodeProps, UINT8* pByte)
755{
756    return this->readOpaqueTag(pByte, &nodeProps->m_oTitle);
757}
758
759
760SYNCML_DM_RET_STATUS_T
761SyncML_DM_WBXMLReader::readFlag(DMAddNodeProp* nodeProps, UINT8* pByte)
762{
763    UINT8 property[2];
764    SYNCML_DM_RET_STATUS_T ret_stat;
765
766    if((ret_stat = this->readOpaqueTag(pByte, (UINT8*)&property,2)) != SYNCML_DM_SUCCESS)
767        return ret_stat;
768
769    nodeProps->m_nFlags = (property[0] << 8) | property[1]; /* stored in Big Endian order */
770   nodeProps->m_nFlags &= ~DMNode::enum_NodeNotPersisted;
771   return ret_stat;
772}
773
774#ifndef DM_IGNORE_TSTAMP_AND_VERSION
775
776SYNCML_DM_RET_STATUS_T
777SyncML_DM_WBXMLReader::readIntTStamp(DMAddNodeProp* nodeProps, UINT8* pByte)
778{
779    UINT8 timeStamp[ sizeof(XPL_CLK_CLOCK_T) ];
780    SYNCML_DM_RET_STATUS_T ret_stat;
781
782    if ( (ret_stat = this->readOpaqueTag(pByte,  timeStamp, sizeof(timeStamp) )) != SYNCML_DM_SUCCESS)
783        return ret_stat;
784
785    nodeProps->m_nTStamp=0;
786    for ( UINT32 i = 0; i < sizeof(XPL_CLK_CLOCK_T); i++ ) {
787      //NOT USE nodeProps->wTStamp |= ((XPL_CLK_CLOCK_T)timeStamp[sizeof(XPL_CLK_CLOCK_T) - i -1]) << (i*8);
788      nodeProps->m_nTStamp |= ((XPL_CLK_CLOCK_T)timeStamp[i]) << (i*8);
789    }
790
791    return ret_stat;
792}
793
794
795SYNCML_DM_RET_STATUS_T
796SyncML_DM_WBXMLReader::readVersion(DMAddNodeProp* nodeProps, UINT8* pByte) {
797
798    UINT8 property[2];
799    SYNCML_DM_RET_STATUS_T ret_stat;
800
801    if((ret_stat = this->readOpaqueTag(pByte, (UINT8*)&property,2)) != SYNCML_DM_SUCCESS)
802        return ret_stat;
803
804    nodeProps->m_nVerNo = (property[0] << 8) | property[1]; /* stored in Big Endian order */
805    return ret_stat;
806}
807
808#endif
809