3.0 Migration
[platform/core/messaging/msg-service.git] / vobject-engine / VCard.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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 "VTypes.h"
18 #include "VCard.h"
19 #include "vobject.h"
20
21 #define MAX_TYPE_NAME_LEN 50
22 #define MAX_PARAM_NAME_LEN 50
23 #define CHECK_START 1
24 #define CHECK_END 2
25
26 #define VFREE(obj) if(obj != NULL) { /*VDATA_TRACE("%p",obj);*/ free(obj); \
27         obj = NULL; \
28 }
29
30 #define TRIM(obj) if(obj != NULL) {\
31         _VRLSpace(obj);\
32         _VRTSpace(obj);\
33 }
34
35 #define UPPER(obj,start,end) if(obj != NULL) {\
36         for(start = 0; start < end; start++)\
37                 obj[start] = toupper(obj[start]);\
38 }
39
40 #define GO_NEXT_CHAR(current, rowData, addedCnt) {\
41         current = *rowData;\
42         rowData++;\
43         (*addedCnt)++;\
44 }
45
46 #define SET_PARAM_VALUE(PARAM, SZVALUE, LIST, LISTCNT, PARAMOBJ, PTYPE, ENC) {\
47         PARAM = 0;\
48         PARAM |= __VCardGetValue(SZVALUE, LIST, LISTCNT);\
49         if ( PARAM != UNKNOWN_NAME ) {\
50                 PARAMOBJ->parameter = PTYPE;\
51                 if(PTYPE == VCARD_PARAM_ENCODING)\
52                         ENC = PARAM;\
53                 break;\
54         }\
55 }
56
57 #define LENGTH_TYPE_LIST(obj, len) for(len =0; obj[len] != NULL; len++);
58
59 extern char* _VUnfoldingNoSpecNew( char *string );
60
61 /** GLOBAL VARIABLE DECLARATION AND INITIALIZATION */
62 /** vCard Types. */
63 char* pszCardTypeList[] =
64 {
65         "ADR",                  /* Delivery Address -> address*/
66         "AGENT",                        /* Agent -> assistant name, assistant number*/
67         "BDAY",                 /* Birthday -> birthday */
68         "BEGIN",                        /* BEGIN VCARD DELIMITER*/
69         "CATEGORIES",   /* Categories is a multivalued attribute */
70         "CLASS",                        /* */
71         "EMAIL",                        /* Email -> email */
72         "END",                  /* END VCARD DELIMITER*/
73         "FN",                   /* Formatted Name -> display_name */
74         "GEO",                  /* Geographic Positioning*/
75         "KEY",                  /* Public Key*/
76         "LABEL",                        /* Label Address -> address*/
77         "LOGO",                 /* Logo*/
78         "MAILER",               /* Email Program (Optional)*/
79         "N",                            /* Name -> name */
80         "NAME",                 /* Name -> name */
81         "NICKNAME",             /* Nickname -> nickname */
82         "NOTE",                 /* Note -> note */
83         "ORG",                  /* Organization Name or Organizational unit -> department*/
84         "PHOTO",                        /* Photograph -> caller id*/
85         "PRODID",               /* */
86         "PROFILE",              /* */
87         "REV",                  /* Last Revision(combination of calendar date & time)*/
88         "ROLE",                 /* Role or occupation */
89         "SORT-STRING",  /* */
90         "SOUND",                        /* Sound*/
91         "SOURCE",               /* */
92         "TEL",                  /* Telephone -> phone number */
93         "TITLE",                        /* Job Title -> job title */
94         "TZ",                   /* Time Zone*/
95         "UID",                  /* Unique Identifier*/
96         "URL",                  /* URL -> web address */
97         "VERSION",              /* Version*/
98         "X-IRMC-LUID",  /* */
99         NULL
100 };
101
102 /** Parameter */
103 char* pszCardParamList[] =
104 {
105         "CHARSET",
106         "CONTEXT",
107         "ENCODING",
108         "LANGUAGE",
109         "TYPE",
110         "VALUE"
111 };
112
113 /** Encoding value */
114 ValueObj pEncList[] =
115 {
116         {"B",                   0x00000001},
117         {"BASE64",      0x00000002},
118         {"QUOTED-PRINTABLE", 0x00000004},
119         {"7BIT",                0x00000008},
120         {"8BIT",                0x00000010}
121 };
122
123 /** Character set value */
124 ValueObj pCharsetList[] =
125 {
126         {"UTF-8",                       0x00000001},
127         {"UTF-16",              0x00000002},
128         {"ISO-8859-1",  0x00000004}
129 };
130
131 /** Value value */
132 ValueObj pValueList[] =
133 {
134         {"BINARY",                      0x00000001},
135         {"BOOLEAN",                     0x00000002},
136         {"DATE",                                0x00000004},
137         {"DATE-TIME",           0x00000008},
138         {"FLOAT",                               0x00000010},
139         {"INTEGER",                     0x00000020},
140         {"PHONE-NUMBER",        0x00000040},
141         {"TEXT",                                0x00000080},
142         {"TIME",                                0x00000100},
143         {"URI",                                 0x00000200},
144         {"URL",                                 0x00000400},
145         {"UTC-OFFSET",          0x00000800},
146         {"VCARD",                               0x00001000}
147 };
148
149 /** Type value */
150 ValueObj pTypeList[] =
151 {
152         {"AIFF",                0x00000001},
153         {"BBS",                 0x00000002},
154         {"CAR",                 0x00000004},
155         {"CELL",                0x00000008},
156         {"DOM",                 0x00000010},
157         {"WORK",                0x00000020},
158         {"FAX",                 0x00000040},
159         {"GIF",                 0x00000080},
160         {"HOME",                0x00000100},
161         {"INTL",                0x00000200},
162         {"INTERNET",    0x00000400},
163         {"ISDN",                0x00000800},
164         {"JPEG",                0x00001000},
165         {"MOBILE",      0x00002000},
166         {"MODEM",               0x00004000},
167         {"MSG",                 0x00008000},
168         {"PAGER",               0x00010000},
169         {"PARCEL",      0x00020000},
170         {"PCM",                 0x00040000},
171         {"PCS",                 0x00080000},
172         {"PNG",                 0x00100000},
173         {"POSTAL",      0x00200000},
174         {"PREF",                0x00400000},
175         {"VIDEO",               0x00800000},
176         {"VOICE",               0x01000000},
177         {"WAVE",                0x02000000},
178         {"WBMP",                0x04000000},
179         {"ETC",                 0x08000000},
180         {"X400",                0x10000000}
181 };
182
183 /** FUNCTION DECLARATION        */
184 int __VCardGetName(char*, char**, int);
185 int __VCardGetValue(char*, const ValueObj*, int);
186 int __VCardGetTypeName(char*, int*, int*);
187 int __VCardGetParamName(char*, int*, int*);
188 int __VIsVcardFile(char*, int);
189 char* __VCardGetParamVal(char*,int*, int*);
190 char* __VCardGetTypeVal(char*, int*, int*, int, VObject*);
191 char* __VCardTypeEncode(VObject*, char*);
192 char* __VCardParamEncode(VObject*, int*);
193
194 #ifdef VDATA_GROUPNAME_SUPPORTED
195 char*   gszGroupName;
196 #endif // VDATA_GROUPNAME_SUPPORTED
197
198
199 /**
200  * __VCardGetName() compares the string and vCard type, parameter name.
201  *
202  * @param       szString        Name which will compare
203  * @param               pszList[]               Name list of vCard type and param
204  * @param               size                            Number of total element of list
205  *
206  * @return      index           The index in the list
207  */
208 int
209 __VCardGetName(char* szString, char* pszList[], int size)
210 {
211         VDATA_TRACE_BEGINE
212         int high, low, i, diff;
213
214         low = 0;
215         high = size - 1;
216
217         for(; high >= low; diff<0 ? (low = i+1):(high = i-1)) {
218                 i = ( low + high ) / 2;
219                 if((diff = strcmp( pszList[i], szString )) == 0) /* success: found it */
220                         return i;
221         }
222         VDATA_TRACE_END
223         return UNKNOWN_NAME;
224 }
225
226 /**
227  * __VCardGetValue() compares the string and vCard type, parameter value.
228  *
229  * @param       szString        Value which will compare
230  * @param               list[]          Value list of vCard param
231  * @param               size                    Number of total element of list
232  *
233  * @return      flag      The value's flag.
234  */
235 int
236 __VCardGetValue( char* szString, const ValueObj list[], int size)
237 {
238         VDATA_TRACE_BEGINE
239         int i = 0, diff = -1;
240         char* szTemp = szString;
241
242         SysRequireEx(szString, UNKNOWN_NAME);
243         SysRequireEx(size > 0, UNKNOWN_NAME);
244
245         UPPER(szTemp,i,strlen(szTemp));
246
247         for(i = 0; i < size-1; i++)
248         {
249                 VDATA_TRACE(" i : %d",i);
250                 VDATA_TRACE(" for loop %d < %d, list[%d] : %p, list[%d].szName : %p",i,size,i,list[i],i,list[i].szName);
251                 VDATA_TRACE(" i : %d",i);
252                 if(list[i].szName != NULL)
253                 {
254                         VDATA_TRACE(" list[%d].szName != NULL",i);
255                         VDATA_TRACE(" before strcmp %s %s",list[i].szName,szTemp);
256                         VDATA_TRACE(" before strcmp %d",strcmp(list[i].szName, szTemp));
257                         if((diff = strcmp(list[i].szName, szTemp)) == 0) /* success: found it */
258                         {
259                                 VDATA_TRACE(" return %d",list[i].flag);
260                                 VDATA_TRACE_END
261                                 return list[i].flag;
262                         }
263                         VDATA_TRACE(" after strcmp %s %s",list[i].szName,szTemp);
264                 }
265         }
266         VDATA_TRACE(" return UNKNOWN_NAME");
267         VDATA_TRACE_END
268         return UNKNOWN_NAME;
269 }
270
271 /**
272  * __VCardGetTypeName() fine the type name and returns the index number
273  *
274  * @param               pVCardRaw       The raw data
275  * @param               pStatus         Decoder status
276  * @param               pDLen           retrived length
277  *
278  * @return      res                     The index in type list
279  */
280 int
281 __VCardGetTypeName(char* pVCardRaw, int* pStatus, int* pDLen)
282 {
283         VDATA_TRACE_BEGINE
284         int     i, index, res;
285         char    c;
286         char    name[MAX_TYPE_NAME_LEN+1]={0,};
287
288 #ifdef VDATA_GROUPNAME_SUPPORTED
289         char*   szGroupName = NULL;
290 #endif // VDATA_GROUPNAME_SUPPORTED
291
292         SysRequireEx(pVCardRaw, UNKNOWN_NAME);
293
294         i = index = 0;
295         res = UNKNOWN_NAME;
296
297         while(true) {
298
299                 GO_NEXT_CHAR(c, pVCardRaw, pDLen);
300
301                 /**
302                  * TYPE NAME's length is must be less than MAX_TYPE_NAME_LEN.
303                  * If TYPE NAME's value is over MAX_TYPE_NAME_LEN, return UNKNOWN_NAME.
304                  * And then Decoding Step shoud not be changed.
305                  */
306                 if(index >= MAX_TYPE_NAME_LEN) {
307                         *pStatus = VCARD_TYPE_NAME_STATUS;
308                         res = UNKNOWN_NAME;
309                         break;
310                 }
311
312                 /**
313                  * There is a delimeter between TYPE NAME and next element(=Param, or Type Value).
314                  * If VTYPE_TOKEN_SEMICOLON or VTYPE_TOKEN_COLON is faced with,
315                  * find TYPE NAME's value in pszCardTypeList, and then return searched result.
316                  */
317                 if ((c == VTYPE_TOKEN_SEMICOLON ) || ( c == VTYPE_TOKEN_COLON)) {
318                         name[index] = 0x00;
319                         TRIM(name);
320                         UPPER(name,i,index);
321                         res = __VCardGetName( name, (char**)pszCardTypeList, VCARD_TYPE_NUM );
322                         break;
323                 }
324                 /** current version not support grouping vcard type */
325                 else if ( c == VTYPE_TOKEN_DOT ) {
326 #ifdef VDATA_GROUPNAME_SUPPORTED
327                         name[index] = '\0';
328                         szGroupName = ( char* ) calloc(1,  index+1 );
329                         if(szGroupName != NULL){
330                                 strncpy( szGroupName, name, index );
331                                 gszGroupName = szGroupName;
332                         }
333                         index = 0;
334 #endif
335                 }
336                 /**
337                  * There is no new line in TYPE NAME.
338                  * If new line character is faced with, return UNKNOWN_NAME;
339                  */
340                 else if ( ( c == '\r' ) || ( c == '\n' ) )
341                 {
342                         (*pDLen)++;
343                         *pStatus = VCARD_TYPE_NAME_STATUS;
344                         res = UNKNOWN_NAME;
345                         return res;
346                 }
347                 else if(_VIsSpace(c));
348                 else    name[index++] = c;
349         }
350
351         /**
352          *      Set Next Step.
353          *
354          */
355         if ( c == VTYPE_TOKEN_SEMICOLON )
356                 /**
357                  * This case next token is parameter. So set VCARD_PARAM_NAME_STATUS step.
358                  */
359                 *pStatus = VCARD_PARAM_NAME_STATUS;
360         else {
361                 if(res != UNKNOWN_NAME)
362                         /**
363                          * This case next string is value. So set VCARD_TYPE_VALUE_STATUS step.
364                          */
365                         *pStatus = VCARD_TYPE_VALUE_STATUS;
366                 else
367                         /**
368                          * In current step, TYPE NAME is invalid. So Try to get TYPE NAME again from next position.
369                          */
370                         *pStatus = VCARD_TYPE_NAME_STATUS;
371         }
372         VDATA_TRACE_END
373         return res;
374 }
375
376 /**
377  * __VCardGetParamName() fine the param name and returns the index number
378  *
379  * @param               pVCardRaw       The raw data
380  * @param               pStatus         Decoder status
381  * @param               pDLen           retrived length
382  *
383  * @return      res                     The index in type list
384  */
385 int
386 __VCardGetParamName( char* pVCardRaw, int* pStatus, int* pDLen )
387 {
388         VDATA_TRACE_BEGINE
389         int     i, index, res;
390
391         char    c;
392         char    name[MAX_PARAM_NAME_LEN+1]={0,};
393         char* pTemp = pVCardRaw;
394
395         SysRequireEx( pVCardRaw, UNKNOWN_NAME );
396
397         i = index = 0;
398         res = UNKNOWN_NAME;
399
400         while ( true )
401         {
402                 GO_NEXT_CHAR(c, pVCardRaw, pDLen);
403
404                 /**
405                  * PARAM NAME's length is must be less than MAX_PARAM_NAME_LEN.
406                  * If PARAM NAME's value is over MAX_PARAM_NAME_LEN, return UNKNOWN_NAME.
407                  * And then Decoding Step shoud not be changed.
408                  */
409                 if(index >= MAX_PARAM_NAME_LEN) {
410                         *pStatus = VCARD_TYPE_NAME_STATUS;
411                         res = UNKNOWN_NAME;
412                         break;
413                 }
414
415                 /**
416                  * There is a delimeter between PARAM NAME and next element(=Param, or Param Value).
417                  * If VTYPE_TOKEN_EQUAL is faced with,
418                  * find PARAM NAME's value in pszCardParamList, and then return searched result.
419                  */
420                 if(c == VTYPE_TOKEN_EQUAL) {
421                         name[index] = '\0';
422                         TRIM(name);
423                         UPPER(name, i, index);
424                         res = __VCardGetName( name, ( char** )pszCardParamList, VCARD_PARAM_NUM );
425                         if(res==UNKNOWN_NAME){
426                                 (*pDLen) = 0;
427                         }
428                         *pStatus = VCARD_PARAM_VALUE_STATUS;
429                         break;
430                 }
431                 /**
432                  * This case, There is no parameter type. Only Parameter Value.
433                  * In VCARD_PARAM_NAME_STATUS status, VTYPE_TOKEN_COLON means that anything parameter is no more.
434                  * so set next step to VCARD_PARAM_VALUE_STATUS.
435                  *
436                  * Ex) TEL;WORK:+12341234
437                  *        ------ ":" next is TEL TYPE's value.
438                  *
439                  * VCARD_PARAM_NAME_STATUS(current) -> VCARD_PARAM_VALUE_STATUS
440                  * -> VCARD_TYPE_VALUE_STATUS -> MOVE TO NEXT TYPE
441                  */
442                 else if(c == VTYPE_TOKEN_COLON) {
443                         *pStatus = VCARD_PARAM_VALUE_STATUS;
444                         pVCardRaw = pTemp;
445                         (*pDLen) = 0;
446                         res = UNKNOWN_NAME;
447                         break;
448                 }
449                 /**
450                  * This case, There is no parameter type. Only Parameter Value.
451                  * In VCARD_PARAM_NAME_STATUS status, VTYPE_TOKEN_SEMICOLON means that there is a next parameter.
452                  * so set next step to VCARD_PARAM_NAME_STATUS.
453                  *
454                  * Ex) TEL;WORK;PREF:+12341234
455                  *        ------ ":" next is TEL TYPE's value.
456                  *
457                  * VCARD_PARAM_NAME_STATUS(current) -> VCARD_PARAM_NAME_STATUS
458                  * -> VCARD_PARAM_VALUE_STATUS -> VCARD_TYPE_VALUE_STATUS -> MOVE TO NEXT TYPE
459                  */
460                 else if(c == VTYPE_TOKEN_SEMICOLON) {
461                         *pStatus = VCARD_PARAM_NAME_STATUS;
462                         pVCardRaw = pTemp;
463                         (*pDLen) = 0;
464                         res = UNKNOWN_NAME;
465                         break;
466                 }
467                 else if((c == '\r') || (c == '\n') || (_VIsSpace(c)));
468                 else
469                         name[index++] = c;
470         }
471         VDATA_TRACE_END
472         return res;
473 }
474
475 bool
476 __VCardFreeVTreeMemory(VTree * pTree)
477 {
478         VDATA_TRACE_BEGINE
479         VObject*                pCurObj = NULL;
480         VObject*                pNextObj = NULL;
481
482         VTree*                  pCurTree = NULL;
483         VTree*                  pNextTree = NULL;
484
485         VParam* pCurParam = NULL;
486         VParam* pNextParam = NULL;
487
488         int count = 0;
489         int i = 0;
490
491         SysRequireEx( pTree->treeType == VCARD, false );
492         SysRequireEx( pTree != NULL, false );
493         VDATA_TRACE("vcard_free_vtree_memory() entered.");
494
495         if (pTree->treeType != VCARD)
496         {
497                 VDATA_TRACE_END
498                 return true;
499         }
500
501         pCurTree = pTree;
502
503         while(pCurTree)
504         {
505                 pNextTree = pCurTree->pNext;
506                 pCurObj = pCurTree->pTop;
507
508                 while ( pCurObj )
509                 {
510
511                         pNextObj = pCurObj->pSibling;
512                         count = pCurObj->valueCount;
513
514                         for ( i = 0; i < count; i++ ) {
515                                 VFREE( pCurObj->pszValue[i]);
516                         }
517
518 #ifdef VDATA_GROUPNAME_SUPPORTED
519                         if ( pCurObj->pszGroupName )
520                                 VFREE( pCurObj->pszGroupName );
521 #endif
522
523                         if ( pCurObj->pParam )
524                         {
525
526                                 pCurParam = pCurObj->pParam;
527
528                                 while(pCurParam != NULL)
529                                 {
530                                         pNextParam = pCurParam->pNext;
531                                         VDATA_TRACE("pNEXT ==> %p", pCurParam->pNext);
532                                         VDATA_TRACE("pPARAM ==> %p", pCurParam->parameter);
533                                         VDATA_TRACE("pVALUE ==> %p", pCurParam->paramValue);
534                                         VDATA_TRACE("pCurParam : %p", pCurParam);
535                                         VDATA_TRACE("pCurParam->parameter : %d", pCurParam->parameter);
536                                         VDATA_TRACE("pCurParam->paramValue : %d", pCurParam->paramValue);
537                                         if(pNextParam != NULL) {
538                                                 VDATA_TRACE("pNextParam : %p", pNextParam);
539                                                 VDATA_TRACE("pNextParam->parameter : %d", pNextParam->parameter);
540                                                 VDATA_TRACE("pNextParam->paramValue : %d", pNextParam->paramValue);
541                                         }
542                                         VFREE(pCurParam);
543                                         pCurParam = pNextParam;
544                                 }
545                         }
546
547                         VFREE( pCurObj );
548                         pCurObj = pNextObj;
549                 }
550
551                 VFREE( pCurTree );
552                 pCurTree = pNextTree;
553         }
554
555         VDATA_TRACE("exit vcard_free_vtree_memory");
556         VDATA_TRACE_END
557         return true;
558 }
559
560 /**
561  * __VCardGetParamVal() fine the param value and returns value.
562  *
563  * @param               pVCardRaw       The raw data
564  * @param               pStatus         Decoder status
565  * @param               pDLen           retrived length
566  *
567  * @return      buffer          The result value
568  */
569 char*
570 __VCardGetParamVal( char* pVCardRaw, int* pStatus, int* pDLen )
571 {
572         VDATA_TRACE_BEGINE
573         int len = 0;
574         char    c;
575         char* pBuf = NULL;
576         char* pTemp = pVCardRaw;
577
578         SysRequireEx( pVCardRaw, NULL );
579
580         while(true) {
581                 GO_NEXT_CHAR(c, pVCardRaw, pDLen);
582                 len++;
583                 switch(c) {
584                         case VTYPE_TOKEN_SEMICOLON :
585                                 *pStatus = VCARD_PARAM_NAME_STATUS;
586                                 break;
587                         case VTYPE_TOKEN_COLON :
588                                 *pStatus = VCARD_TYPE_VALUE_STATUS;
589                                 break;
590                         case VTYPE_TOKEN_COMMA :
591                                 *pStatus = VCARD_PARAM_VALUE_STATUS;
592                                 break;
593                 }
594                 if( c == VTYPE_TOKEN_SEMICOLON
595                                 || c == VTYPE_TOKEN_COLON
596                                 || c == VTYPE_TOKEN_COMMA
597                                 || c == 0x00)
598                         break;
599         }
600
601         //if (len < 1)
602                 //return NULL;
603
604         pBuf = (char *)calloc(1, len);
605         if(pBuf  == NULL)
606                 return NULL;
607
608         memset(pBuf, 0x00, len);
609         memcpy(pBuf, pTemp, len-1);
610         TRIM(pBuf);
611         VDATA_TRACE_END
612         return pBuf;
613 }
614
615
616 /**
617  * __VCardGetTypeVal() fine the type value and returns value.
618  *
619  * @param       pVCardRaw       The raw data
620  * @param               status                  Decoder status
621  * @return      buffer          The result value
622  */
623 char*
624 __VCardGetTypeVal( char* pVCardRaw, int* pStatus, int* pDLen, int enc, VObject* pType)
625 {
626         VDATA_TRACE_BEGINE
627         int num = 0;
628         int len = 0;
629         int bufferCount = 0;
630
631         bool bEscape = false;
632
633         char    c, c1, c2;
634         char* pBuf = NULL;
635         char* pTemp = pVCardRaw;
636         char* pTmpBuf = NULL;
637         int Status = 0;
638         int Len = 0;
639
640         SysRequireEx( pVCardRaw, NULL );
641
642         while(true)
643         {
644                 GO_NEXT_CHAR(c, pVCardRaw, pDLen);
645
646                 if( c == 0x00) break;
647
648                 len++;
649
650                 /** This case means that there are more type's value. */
651                 if ( c == VTYPE_TOKEN_SEMICOLON && bEscape == false ) {
652
653                         if((pBuf = (char *)calloc(1, len)) == NULL) return NULL;
654
655                         memset(pBuf, 0x00, len);
656                         memcpy(pBuf,pTemp,len-1);
657
658                         TRIM(pBuf);
659                         _VUnescape(pBuf);
660
661                         *pStatus = VCARD_TYPE_VALUE_STATUS;
662
663                         /** Base 64 Decoding */
664                         if((enc & pEncList[1].flag) || (enc & pEncList[0].flag)) {
665
666                                 bufferCount = (len * 6 / 8) + 2;
667
668                                 if((pTmpBuf = (char *)calloc(1, bufferCount)) == NULL) {
669                                         VFREE(pBuf);
670                                         return NULL;
671                                 }
672
673                                 memset(pTmpBuf, 0x00, bufferCount);
674                                 num = _VB64Decode(pTmpBuf, pBuf);
675
676                                 if(pType != NULL) pType->numOfBiData = num;
677
678                                 VFREE(pBuf);
679                                 pBuf = pTmpBuf;
680                                 pTmpBuf = NULL;
681                                 break;
682                         }
683
684                         /** Quoted Printable Decoding */
685                         if(enc & pEncList[2].flag) {
686
687                                 int i = 0, j = 0;
688
689                                 while(pBuf[i]) {
690                                         if(pBuf[i] == '\n' || pBuf[i] == '\r'){
691                                                 i++;
692                                                 if(pBuf[i] == '\n'|| pBuf[i] == '\r')
693                                                         i++;
694
695                                                 if(pBuf[j-1] == '=') j--;
696                                         }
697                                         else
698                                                 pBuf[j++] = pBuf[i++];
699                                 }
700                                 pBuf[j] = '\0';
701
702                                 _VQPDecode(pBuf);
703                                 TRIM(pBuf);
704                                 break;
705                         }
706                         break;
707                 }
708
709                 if(c == '\\')
710                         bEscape = true;
711                 else if(bEscape == true && c != VTYPE_TOKEN_SEMICOLON )
712                         bEscape = false;
713                 else if((c == '\r') || (c == '\n'))
714                 {
715                         c2 = *(pVCardRaw-2);
716
717                         if(c2 == '=' && (enc & pEncList[2].flag))
718                         {
719                                 c1 = *pVCardRaw;
720                                 if((c1 == '\r') || (c1 == '\n'))
721                                 {
722                                         pVCardRaw += 1;
723                                         (*pDLen) += 1;
724                                         len++;
725                                 }
726                         }
727                         else if(__VCardGetTypeName(pVCardRaw, &Status, &Len) != UNKNOWN_NAME)
728                         {
729                                 --len;
730                                 if((pBuf = (char *)calloc(1, len)) == NULL) return NULL;
731
732                                 memset(pBuf, 0x00, len);
733                                 memcpy(pBuf,pTemp,len-1);
734
735                                 TRIM(pBuf);
736                                 _VUnescape(pBuf);
737
738                                 *pStatus = VCARD_TYPE_NAME_STATUS;
739
740                                 c1 = *pVCardRaw;
741
742                                 if((c1 == '\r') || (c1 == '\n')) {
743                                         pVCardRaw += 1;
744                                         (*pDLen) += 1;
745                                 }
746
747                                 if((enc & pEncList[1].flag) || (enc & pEncList[0].flag)) {
748
749                                         bufferCount = (len * 6 / 8) + 5;
750
751                                         if((pTmpBuf = (char *)calloc(1, bufferCount)) == NULL) {
752                                                 VFREE(pBuf);
753                                                 return NULL;
754                                         }
755
756                                         memset(pTmpBuf, 0x00, bufferCount);
757                                         num = _VB64Decode(pTmpBuf, pBuf);
758
759                                         if(pType != NULL)
760                                                 pType->numOfBiData = num;
761
762                                         VFREE(pBuf);
763                                         pBuf = pTmpBuf;
764                                         pTmpBuf = NULL;
765                                         break;
766                                 }
767
768                                 if(enc & pEncList[2].flag) {
769
770                                         int i = 0, j = 0;
771
772                                         while(pBuf[i])
773                                         {
774                                                 if(pBuf[i] == '\n' || pBuf[i] == '\r')
775                                                 {
776                                                         i++;
777                                                         if(pBuf[i] == '\n' || pBuf[i] == '\r')
778                                                                 i++;
779
780                                                         if(pBuf[j-1] == '=') j--;
781                                                 }
782                                                 else
783                                                         pBuf[j++] = pBuf[i++];
784                                         }
785                                         pBuf[j] = '\0';
786
787                                         _VQPDecode(pBuf);
788                                         TRIM(pBuf);
789                                         break;
790                                 }
791                                 break;
792                         }
793                 }
794         }
795         VDATA_TRACE_END
796         return pBuf;
797 }
798
799
800 int
801 VCardGetTypeValue( int index )
802 {
803         VDATA_TRACE_BEGINE
804         VDATA_TRACE("VCardGetTypeValue() enter..\n");
805         VDATA_TRACE_END
806         return pTypeList[index].flag;
807 }
808
809 int
810 VCardGetValValue( int index )
811 {
812         VDATA_TRACE_BEGINE
813         VDATA_TRACE("VCardGetValValue() enter..\n");
814         VDATA_TRACE_END
815         return pValueList[index].flag;
816 }
817
818 int
819 VCardGetEncValue( int index )
820 {
821         VDATA_TRACE_BEGINE
822         VDATA_TRACE("VCardGetEncValue() enter..\n");
823         VDATA_TRACE_END
824         return pEncList[index].flag;
825 }
826
827 int
828 VCardGetCharsetValue( int index )
829 {
830         VDATA_TRACE_BEGINE
831         VDATA_TRACE("VCardGetCharsetValue() enter..\n");
832         VDATA_TRACE_END
833         return pCharsetList[index].flag;
834 }
835
836 /*
837  * vcard_decode() decode the vCard data and returns vObject struct
838  *
839  * @param       pVCardRaw            The raw data
840  * @return      vObject             The result value
841  */
842 SLPAPI VTree*
843 vcard_decode( char *pCardRaw )
844 {
845         VDATA_TRACE_BEGINE;
846         char* szValue = NULL;
847         char* szCardBegin = NULL;
848         char* pCardRawTmp = NULL;
849         VTree* pVCard = NULL;
850         VParam* pTmpParam = NULL;
851         VObject* pTemp = NULL;
852
853         char    c;
854
855         int type, param;
856         int status = VCARD_TYPE_NAME_STATUS;
857         int done = false;
858         int valueCount = 0;
859         int len;
860         int dLen = 0;
861         int param_status = false;
862         int numberedParam = 0;
863         int enc = 0;
864         //int start_status = 0;
865         char* temp = NULL;
866
867         bool vcard_ended = false;
868
869         SysRequireEx(pCardRaw != NULL, NULL);
870         len = strlen(pCardRaw);
871         VDATA_TRACE("length of pCardRaw = %d", len);
872
873         pCardRaw = _VUnfoldingNoSpecNew(pCardRaw);
874         if (pCardRaw == NULL) {
875                 return NULL;
876         }
877         pCardRawTmp = pCardRaw;
878         len = _VManySpace2Space( pCardRaw );
879
880         VDATA_TRACE("ret value of _VManySpace2Space = %d", len);
881
882         if(!__VIsVcardFile(pCardRaw, CHECK_START)) {
883                 VFREE(pCardRawTmp);
884                 VDATA_TRACE_END
885                 return NULL;
886         }
887
888
889         while(true && !done)
890         {
891                 c = *pCardRaw;
892
893                 if((c == '\0') || done)
894                         break;
895
896                 switch(status) {
897                         case VCARD_TYPE_NAME_STATUS:
898                                 dLen = 0;
899                                 type = __VCardGetTypeName(pCardRaw, &status, &dLen);
900                                 pCardRaw += dLen;
901
902                                 if(type == -1)
903                                         break;
904
905                                 switch ( type )
906                                 {
907                                         case VCARD_TYPE_BEGIN:
908                                                 if (pVCard) {
909                                                         free(pVCard);
910                                                         pVCard = NULL;
911                                                 }
912
913                                                 if ( ( pVCard = ( VTree* )calloc(1,  sizeof( VTree ) ) ) == NULL ) {
914                                                         //start_status = 1;
915                                                         goto CATCH;
916                                                 }
917
918                                                 memset(pVCard,0x00, sizeof(VTree));
919
920                                                 dLen = 0;
921                                                 szCardBegin = __VCardGetTypeVal(pCardRaw, &status, &dLen, enc, NULL);
922                                                 pCardRaw += dLen;
923                                                 VFREE(szCardBegin);
924
925                                                 pVCard->treeType = VCARD;
926                                                 pVCard->pTop = NULL;
927                                                 pVCard->pCur = NULL;
928                                                 pVCard->pNext = NULL;
929                                                 break;
930
931                                         case VCARD_TYPE_END:
932                                                 enc = 0;
933                                                 if(strstr(pCardRaw,"VCARD") != NULL) {
934                                                         pCardRaw += dLen;
935                                                         done = true;
936                                                 vcard_ended = true;
937                                                 }
938                                                 else    {
939                                                         status = VCARD_TYPE_NAME_STATUS;
940                                                         pCardRaw += dLen;
941                                                         //VFREE(etemp);
942                                                 }
943                                                 break;
944
945                                         case UNKNOWN_NAME :
946                                                 break;
947
948                                         default:
949                                                 if(UNKNOWN_NAME == type || type < 0) {
950                                                         status = VCARD_TYPE_NAME_STATUS;
951                                                         break;
952                                                 }
953
954                                                 if ( ( pTemp = ( VObject* )calloc(1,  sizeof( VObject ) ) ) == NULL ) {
955                                                         goto CATCH;
956                                                 }
957
958                                                 memset( pTemp, 0, sizeof( VObject ) );
959                                                 pTemp->property = type;
960
961                                                 if ( pVCard->pTop == NULL ) {
962                                                         pVCard->pTop = pTemp;
963                                                         pVCard->pCur = pTemp;
964                                                 }
965                                                 else {
966                                                         pVCard->pCur->pSibling = pTemp;
967                                                         pVCard->pCur = pTemp;
968                                                 }
969
970                                                 break;
971                                 }
972
973                                 numberedParam = 0;
974                                 param_status = false;
975                                 valueCount = 0;
976
977 #ifdef VDATA_GROUPNAME_SUPPORTED
978                                 if ( gszGroupName != NULL )
979                                         pVCard->pCur->pszGroupName = gszGroupName;
980 #endif
981                                 break;
982
983                         case VCARD_PARAM_NAME_STATUS:
984                         {
985                                 dLen = 0;
986                                 param = __VCardGetParamName( pCardRaw, &status, &dLen );
987                                 pCardRaw += dLen;
988
989                                 if ( param_status != true ) {
990
991                                         if ( ( pTmpParam = ( VParam* )calloc(1,  sizeof( VParam ) ) ) == NULL )
992                                                         goto CATCH;
993
994                                         param_status = true;
995                                         pVCard->pCur->pParam = pTmpParam;
996                                         memset( pTmpParam, 0x00, sizeof( VParam ) );
997                                         VDATA_TRACE("pTmpParam : %p", pTmpParam);
998                                 }
999                                 else
1000                                 {
1001                                         if ( ( pTmpParam->pNext = ( VParam* )calloc(1,  sizeof( VParam ) ) ) == NULL )
1002                                                         goto CATCH;
1003
1004                                         pTmpParam = pTmpParam->pNext;
1005                                         memset( pTmpParam, 0x00, sizeof(VParam));
1006                                         VDATA_TRACE("pTmpParam : %p", pTmpParam);
1007                                 }
1008
1009                                 pTmpParam->parameter = param;
1010                                 break;
1011                         }
1012                         case VCARD_PARAM_VALUE_STATUS:
1013                                 dLen = 0;
1014                                 numberedParam = 0;
1015                                 switch ( pTmpParam->parameter )
1016                                 {
1017                                         case VCARD_PARAM_TYPE:
1018                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1019                                                 numberedParam |= __VCardGetValue( szValue, pTypeList, VCARD_TYPE_PARAM_NUM );
1020                                                 break;
1021                                         case VCARD_PARAM_VALUE:
1022                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1023                                                 numberedParam |= __VCardGetValue( szValue, pValueList, VCARD_VALUE_PARAM_NUM );
1024                                                 break;
1025                                         case VCARD_PARAM_ENCODING:
1026                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1027                                                 numberedParam |= __VCardGetValue( szValue, pEncList, VCARD_ENCODE_PARAM_NUM );
1028                                                 enc = numberedParam;
1029                                                 break;
1030                                         case VCARD_PARAM_CHARSET:
1031                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1032                                                 numberedParam |= __VCardGetValue( szValue, pCharsetList, VCARD_CHARSET_PARAM_NUM );
1033                                                 break;
1034                                         case VCARD_PARAM_CONTEXT:
1035                                         case VCARD_PARAM_LANGUAGE:
1036                                                 // prevent 7605 08.03.13
1037                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1038                                                 numberedParam = 0;
1039                                                 break;
1040                                         default:
1041                                                 szValue = __VCardGetParamVal( pCardRaw, &status, &dLen );
1042
1043                                                 SET_PARAM_VALUE(numberedParam, szValue, pTypeList, VCARD_TYPE_PARAM_NUM, pTmpParam, VCARD_PARAM_TYPE, enc);
1044                                                 SET_PARAM_VALUE(numberedParam, szValue, pValueList, VCARD_VALUE_PARAM_NUM, pTmpParam, VCARD_PARAM_VALUE, enc);
1045                                                 SET_PARAM_VALUE(numberedParam, szValue, pEncList, VCARD_ENCODE_PARAM_NUM, pTmpParam, VCARD_PARAM_ENCODING, enc);
1046                                                 SET_PARAM_VALUE(numberedParam, szValue, pCharsetList, VCARD_CHARSET_PARAM_NUM, pTmpParam, VCARD_PARAM_CHARSET, enc);
1047
1048                                                 numberedParam = 0;
1049                                                 pCardRaw += dLen;
1050                                                 dLen = 0;
1051
1052                                                 break;
1053                                 }
1054
1055                                 VDATA_TRACE("%d, %s, %p",numberedParam, szValue, pTmpParam);
1056                                 pTmpParam->paramValue = numberedParam;
1057                                 pTmpParam->pNext = NULL;
1058                                 VFREE(szValue);
1059                                 pCardRaw += dLen;
1060                                 break;
1061                         case VCARD_TYPE_VALUE_STATUS:
1062                                 dLen = 0;
1063                                 temp = __VCardGetTypeVal( pCardRaw, &status, &dLen, enc, pVCard->pCur);
1064
1065                                 if(valueCount <= VDATA_VALUE_COUNT_MAX) {
1066                                         pVCard->pCur->pszValue[valueCount] = temp;
1067                                         valueCount++;
1068                                         pVCard->pCur->valueCount = valueCount;
1069                                 }
1070                                 else
1071                                         VFREE(temp);
1072
1073                                 pCardRaw += dLen;
1074                                 break;
1075                 }
1076         }
1077
1078         VFREE(pCardRawTmp);
1079
1080         if(pVCard->pTop == NULL)
1081                 goto CATCH;
1082
1083         if(!vcard_ended) {
1084                 goto CATCH1;
1085         }
1086         VDATA_TRACE_END
1087         return pVCard;
1088
1089 CATCH :
1090         VFREE(pTemp);
1091 CATCH1 :
1092         VFREE(pCardRawTmp);
1093         __VCardFreeVTreeMemory(pVCard);
1094         VDATA_TRACE_END
1095         return NULL;
1096 }
1097
1098 /*
1099  * vcard_encode() compares the string and vCard type, parameter value.
1100  *
1101  * @param       pVCardRaw            Data which will be encoded
1102  * @return      char *              Encoded result
1103  */
1104 SLPAPI char*
1105 vcard_encode( VTree *pVCardRaw )
1106 {
1107         VDATA_TRACE_BEGINE
1108         char*           pVCardRes = NULL;
1109         VObject *       pTmpObj =  NULL;
1110         char*           pTemp = NULL;
1111         int                     len;
1112         int                     total = 0;
1113         int             cnt = 0;
1114         int             lenTypeList = 0;
1115
1116         LENGTH_TYPE_LIST(pszCardTypeList, lenTypeList);
1117
1118         SysRequireEx(pVCardRaw != NULL, NULL);
1119         SysRequireEx(pVCardRaw->pTop != NULL, NULL);
1120         SysRequireEx(pVCardRaw->pTop->property >= 0, NULL);
1121         SysRequireEx(pVCardRaw->pTop->property < lenTypeList, NULL);
1122         SysRequireEx(pVCardRaw->treeType == VCARD, NULL);
1123         SysRequireEx(pVCardRaw->pTop->valueCount > 0, NULL);
1124
1125         //VDATA_TRACE("START %d %d", pVCardRaw->pTop->property, lenTypeList);
1126
1127         for(;cnt < pVCardRaw->pTop->valueCount;cnt++) {
1128
1129                 if(pVCardRaw->pTop->pszValue[cnt] == NULL)  {
1130                         VDATA_TRACE("pVCardRaw->pTop->valueCount : %d",pVCardRaw->pTop->valueCount);
1131                         VDATA_TRACE("pVCardRaw->pTop->pszValue[%d] : %s", cnt, pVCardRaw->pTop->pszValue[cnt]);
1132                         VDATA_TRACE_END
1133                         return NULL;
1134                 }
1135         }
1136         total += sizeof(char) * (14 + 14);
1137         if ( ( pVCardRes = ( char * )calloc(1,  total ) ) == NULL )
1138         {
1139                 VDATA_TRACE(  "vcard_encode:calloc failed\n" );
1140                 VDATA_TRACE_END
1141                 return NULL;
1142         }
1143
1144         memcpy( pVCardRes, "BEGIN:VCARD\r\n", 14 );
1145         g_strlcat( pVCardRes, "VERSION:2.1\r\n", total - strlen(pVCardRes));
1146
1147         pTmpObj = pVCardRaw->pTop;
1148
1149         while ( true )
1150         {
1151                 if(pTmpObj == NULL)
1152                         break;
1153
1154                 if ( ( pTemp = __VCardTypeEncode( pTmpObj, pszCardTypeList[pTmpObj->property] ) ) != NULL )
1155                 {
1156                         len = strlen( pTemp );
1157                         total += len + sizeof(char) * 10;
1158                         if ( ( pVCardRes = ( char* )realloc( pVCardRes, total ) ) == NULL )
1159                         {
1160                                 VDATA_TRACE(  "vcard_encode():realloc failed\n");
1161                                 VFREE( pTemp );
1162                                 pTemp = NULL;
1163                                 VDATA_TRACE_END
1164                                 return NULL;
1165                         }
1166
1167                         if( strncmp(pTemp,"VERSION", strlen("VERSION")) != 0)
1168                                 g_strlcat(pVCardRes, pTemp, total - strlen(pVCardRes));
1169
1170                         VDATA_TRACE("pTemp : %s", pTemp);
1171
1172                         VFREE( pTemp );
1173                         pTemp = NULL;
1174                 }
1175
1176                 if ( pTmpObj->pSibling != NULL )
1177                         pTmpObj = pTmpObj->pSibling;
1178                 else
1179                         break;
1180         }
1181
1182         total += sizeof(char) * 12;
1183         if ( ( pVCardRes = ( char * )realloc( pVCardRes, total ) ) == NULL )
1184         {
1185                 VDATA_TRACE(  "vcard_encode:realloc failed\n");
1186                 VDATA_TRACE_END
1187                 return NULL;
1188         }
1189         g_strlcat( pVCardRes, "END:VCARD\r\n", total - strlen(pVCardRes));
1190         VDATA_TRACE_END
1191         return pVCardRes;
1192 }
1193
1194
1195 /*
1196  * VIsVcardFile() verify VCard file.
1197  *
1198  * @param       pVCardRaw           Data which will be encoded
1199  * @return      int                 result (true or false)
1200  */
1201 int
1202 __VIsVcardFile(char *pCardRaw, int mode)
1203 {
1204         int i=0;
1205         bool rtnValue = true;
1206         char *pszVcardBegin = "BEGIN:VCARD";
1207
1208         switch(mode)
1209         {
1210                 case CHECK_START :
1211                         for(i=0; i < 11; i++)
1212                 if(*pszVcardBegin++ != *pCardRaw++)
1213                                         rtnValue = false;
1214                         break;
1215
1216                 default :
1217                         rtnValue = false;
1218         }
1219         VDATA_TRACE_END
1220         return rtnValue;
1221 }
1222
1223
1224 /*
1225  * vCardTypeEncoder() compares the string and vCard type, parameter value.
1226  *
1227  * @param               typeObj                         Data which will be encoded
1228  * @param               type                            Name of the type
1229  * @return      char *              Encoded result
1230  */
1231 char*
1232 __VCardTypeEncode( VObject *pTypeObj, char *pType )
1233 {
1234         VDATA_TRACE_BEGINE
1235         int                     len;
1236         char*           pTemp = NULL;
1237         char*           szTypeValue = NULL;
1238         int                     i;
1239         int                     enc = 0;
1240         char*           pEncode = NULL;
1241         char*           pRes = NULL;
1242         int                     total = 0;
1243         int                     biLen = 0;
1244
1245         len = strlen( pType );
1246         biLen = pTypeObj->numOfBiData;
1247
1248 #ifdef VDATA_GROUPNAME_SUPPORTED
1249         if ( pTypeObj->pszGroupName != NULL )
1250         {
1251                 len += strlen( pTypeObj->pszGroupName ) + 1;
1252         }
1253 #endif // VDATA_GROUPNAME_SUPPORTED
1254         if ( ( szTypeValue = ( char * )calloc(1,  total += ( len+1 ) ) ) == NULL )
1255         {
1256                 VDATA_TRACE(  "__VCardTypeEncode():calloc failed\n");
1257                 VDATA_TRACE_END
1258                 return NULL;
1259         }
1260         memset( szTypeValue, '\0', ( len+1 ) );
1261 #ifdef VDATA_GROUPNAME_SUPPORTED
1262         if ( pTypeObj->pszGroupName != NULL )
1263         {
1264                 g_strlcat( szTypeValue, pTypeObj->pszGroupName, total - strlen(szTypeValue));
1265                 g_strlcat( szTypeValue, ".", total - strlen(szTypeValue));
1266         }
1267 #endif // VDATA_GROUPNAME_SUPPORTED
1268         g_strlcat( szTypeValue, pType, total - strlen(szTypeValue));
1269
1270         pTemp = __VCardParamEncode( pTypeObj, &enc );
1271         if ( pTemp != NULL )
1272         {
1273                 len = strlen( pTemp );
1274                 if ( ( szTypeValue = ( char * )realloc( szTypeValue, ( total += len ) ) ) == NULL )
1275                 {
1276                         VDATA_TRACE(  "__VCardTypeEncode():realloc failed\n");
1277                         VFREE( pTemp );
1278                         pTemp = NULL
1279                         VDATA_TRACE_END;
1280                         return NULL;
1281                 }
1282                 g_strlcat( szTypeValue, pTemp, total - strlen(szTypeValue));
1283                 VFREE( pTemp );
1284                 pTemp = NULL;
1285         }
1286
1287         if ( ( szTypeValue = ( char * )realloc( szTypeValue, ( total += 2 ) ) ) == NULL )
1288         {
1289                 VDATA_TRACE_END
1290                 return NULL;
1291         }
1292
1293         g_strlcat( szTypeValue, ":", total - strlen(szTypeValue));
1294
1295         len = 0;
1296
1297         if(strcmp(pType, pszCardTypeList[19]) != 0)     {
1298                 for ( i = 0; i < pTypeObj->valueCount; i++ ) {
1299
1300                         if(pTypeObj->pszValue[i] != NULL)
1301                         len += strlen( pTypeObj->pszValue[i] );
1302                 }
1303         }
1304         else {
1305                 len += biLen;
1306         }
1307
1308         for ( i = 0; i < pTypeObj->valueCount; i++ ) {
1309
1310                 if ( i == 0 ) {
1311                         if ( ( pEncode = ( char * )calloc(1,  len+20 ) ) == NULL ) {
1312                                 VFREE(szTypeValue);
1313                                 VDATA_TRACE_END
1314                                 return NULL;
1315                         }
1316
1317                         memset( pEncode, '\0', len+20 );
1318
1319                         if(strcmp(pType, pszCardTypeList[19]) != 0)     {
1320                                 g_strlcat( pEncode, pTypeObj->pszValue[i], len+20 - strlen(pEncode));
1321                                 _VEscape(pEncode);
1322                         }
1323                         else
1324                                 memcpy(pEncode, pTypeObj->pszValue[i], biLen);
1325                         }
1326                 else {
1327                         char    buf[1000];
1328                         strncpy( buf, pTypeObj->pszValue[i], 999 );
1329                         _VEscape( buf );
1330                         g_strlcat( pEncode, ";", len+20 - strlen(pEncode));
1331                         g_strlcat( pEncode, buf, len+20 - strlen(pEncode));
1332                 }
1333         }
1334
1335         if(strcmp(pType, pszCardTypeList[19]) != 0)     {
1336                 if (pEncode) {
1337                         g_strlcat( pEncode, "\0\0", len+20 - strlen(pEncode) );
1338                         len = strlen( pEncode );
1339                 }
1340         }
1341         else {
1342                 len = biLen;
1343         }
1344
1345         if ( enc & pEncList[2].flag ) {
1346                 if((pRes = (char *)calloc(1, len * 6 + 10)) == NULL) {
1347                         VFREE(pEncode);
1348                         VFREE(szTypeValue);
1349                         VDATA_TRACE_END
1350                         return NULL;
1351                 }
1352                 if(pEncode)
1353                         _VQPEncode( pRes, pEncode );
1354                 VFREE(pEncode);
1355                         }
1356         else if(enc & pEncList[1].flag ) {
1357                 if((pRes = (char *)calloc(1, (len * 8 / 6) + 4)) == NULL){
1358                         VFREE(pEncode);
1359                         VFREE(szTypeValue);
1360                         VDATA_TRACE_END
1361                         return NULL;
1362                 }
1363
1364                 memset( pRes, '\0', ( ( len * 8 / 6 ) + 4 ) );
1365                 _VB64Encode( pRes, pEncode, biLen );
1366                 VFREE(pEncode);
1367                         }
1368         else {
1369                 if((pRes = (char *)calloc(1, len+30)) == NULL) {
1370                         VFREE(pEncode);
1371                         VFREE(szTypeValue);
1372                         VDATA_TRACE_END
1373                         return NULL;
1374                 }
1375                 memset( pRes, '\0', ( len + 30 ) );
1376                 if(pEncode)
1377                 {
1378                         memcpy( pRes, pEncode, len );
1379                         VFREE(pEncode);
1380                 }
1381         }
1382
1383         if((pRes = (char *)realloc(pRes, strlen(pRes) + 3)) == NULL)
1384         {
1385                 VFREE(pEncode);
1386                 VFREE(szTypeValue);
1387                 VDATA_TRACE_END
1388                 return NULL;
1389         }
1390         g_strlcat( pRes, "\r\n", 2);
1391
1392         len = strlen( pRes );
1393
1394         if ((szTypeValue = (char *)realloc(szTypeValue, (total += (len+3)))) == NULL) {
1395                 VFREE(pEncode);
1396                 VFREE(pRes);
1397                 VDATA_TRACE_END
1398                 return NULL;
1399         }
1400
1401         g_strlcat(szTypeValue, pRes, total - strlen(szTypeValue));
1402
1403         if(strcmp(pType, pszCardTypeList[19]) != 0) {
1404                 _VRLSpace( szTypeValue );
1405                 _VRTSpace( szTypeValue );
1406         }
1407
1408         VFREE(pRes);
1409         VDATA_TRACE_END
1410         return szTypeValue;
1411 }
1412
1413 /**
1414  * __VCardParamEncode() Parameter Encoding.
1415  *
1416  * @param               pTypeObj                Data which will be encoded
1417  * @param               pEnc                            Name of the type
1418  */
1419 char *
1420 __VCardParamEncode(VObject* pTypeObj, int* pEnc)
1421 {
1422         VDATA_TRACE_BEGINE
1423         int i = 0;
1424         int len = 0;
1425         int sNum = 0;
1426         int shift = 0;
1427         bool bSupported;
1428         char* szParam = NULL;
1429         VParam* pTemp = NULL;
1430         ValueObj*       pList = NULL;
1431
1432         /** Paramter initialize. */
1433         pTemp = pTypeObj->pParam;
1434
1435         /** Momory Allocation for parameter string. */
1436         if(pTemp != NULL) {
1437                 if ((szParam = (char*)calloc(1, len+=2)) == NULL)
1438                 {
1439                         VDATA_TRACE_END
1440                         return NULL;
1441                 }
1442                 memset(szParam, 0x00, 2);
1443         }
1444
1445         /** appending pamaters. */
1446         while(true) {
1447
1448                 if(pTemp == NULL) break;
1449
1450                 bSupported = false;
1451
1452                 /** Expand szParam string. For appending.*/
1453                 if((szParam = (char *)realloc(szParam, len += 15)) == NULL)
1454                 {
1455                         VDATA_TRACE_END
1456                         return NULL;
1457                 }
1458
1459                 /** appending paramter name. */
1460                 g_strlcat( szParam, ";", len - strlen(szParam) );
1461                 if(pTemp->parameter != VCARD_PARAM_TYPE) {
1462                         g_strlcat( szParam, pszCardParamList[pTemp->parameter], len - strlen(szParam));
1463                         g_strlcat( szParam, "=", len - strlen(szParam));
1464                 }
1465
1466                 /** Set Parameter Value name. */
1467                 switch ( pTemp->parameter )
1468                 {
1469                         case VCARD_PARAM_ENCODING:
1470                                 *pEnc = pTemp->paramValue;
1471                                 shift = VCARD_ENCODE_PARAM_NUM;
1472                                 pList = pEncList; bSupported = true;
1473                                 break;
1474                         case VCARD_PARAM_TYPE:
1475                                 shift = VCARD_TYPE_PARAM_NUM;
1476                                 pList = pTypeList; bSupported = true;
1477                                 break;
1478                         case VCARD_PARAM_VALUE:
1479                                 shift = VCARD_VALUE_PARAM_NUM;
1480                                 pList = pValueList; bSupported = true;
1481                                 break;
1482                         case VCARD_PARAM_CHARSET:
1483                                 shift = VCARD_CHARSET_PARAM_NUM;
1484                                 pList = pCharsetList; bSupported = true;
1485                                 break;
1486                         default:
1487                                 if ( ( szParam = ( char* )realloc( szParam, 5 ) ) == NULL )
1488                                 {
1489                                         VDATA_TRACE_END
1490                                         return NULL;
1491                                 }
1492                                 g_strlcat( szParam, "NONE", 5 - strlen(szParam) );
1493                 }
1494
1495                 /** exchage parameter value's to string.*/
1496                 if(bSupported == true) {
1497
1498                         for(i = 0, sNum = 0x00000001; i < shift; i++) {
1499
1500                                 if(pTemp->paramValue & sNum) {
1501                                         if((szParam = (char *)realloc(szParam, ( len += (strlen(pList[i].szName) + 2)))) == NULL)
1502                                         {
1503                                                 VDATA_TRACE_END
1504                                                 return NULL;
1505                                         }
1506
1507                                         g_strlcat( szParam, pList[i].szName, len - strlen(szParam));
1508                                         g_strlcat( szParam, "; ", len - strlen(szParam));
1509                                 }
1510
1511                                 sNum <<= 1;
1512                         }
1513                 }
1514
1515                 /** remove semicolon from tail. */
1516                 for(i = strlen( szParam ); i > 0 ; i--) {
1517
1518                         if ( szParam[i] == ' ' && szParam[i-1] == ';' ) {
1519                                 szParam[i-1] = '\0';
1520                                 break;
1521                         }
1522                 }
1523
1524                 if ( pTemp->pNext != NULL )
1525                         pTemp = pTemp->pNext;
1526                 else
1527                         break;
1528         }
1529         VDATA_TRACE_END
1530         return szParam;
1531 }
1532
1533 SLPAPI bool
1534 vcard_free_vtree_memory(VTree * pTree)
1535 {
1536         VDATA_TRACE_BEGINE
1537         if(pTree == NULL)
1538         {
1539                 VDATA_TRACE_END
1540                 return false;
1541         }
1542         VDATA_TRACE_END
1543         return __VCardFreeVTreeMemory(pTree);
1544 }
1545