2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
21 #define VCARD_TYPE_NUM 34
22 #define VCAL_TYPE_NUM 66
23 #define VMSG_TYPE_NUM 12
24 extern char *pszCardTypeList[];
27 char Base64Table[65] = { 'A',
28 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
29 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
30 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e',
31 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
32 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
33 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
37 /* Function Declaration */
38 int __VFindBase64( char );
39 int __VBase64Check( char * );
40 int __VRepChar2Space( char *, char );
41 char __VHexaDecoder( char * );
42 void __VHexaEncoder( char * );
43 int __VIsPrintable( char );
47 * vCardIsSpace() returns one if char is either a space, tab, or newline.
49 * @param s1 [in] pointer to first string.
50 * @param s2 [in] pointer to second string.
51 * @return 1 'in' is a space character.
52 * @return 0 'in' is not a space.
57 if ( ( in == TAB ) || ( in == WSP ) )
69 * vRemLeadSpace() removes leading space in string 'in'.
71 * @param in [inout] pointer to string.
72 * @return 0 if success.
83 while ( !done && in[i] )
85 if ( _VIsSpace( in[i] ) )
108 * vRemTermSpace() removes terminating space.
110 * @param in [inout] pointer to string.
111 * @return 0 if success.
114 _VRTSpace( char *in )
122 while ( !done && !( i < 0 ) )
124 if ( _VIsSpace( in[i] ) )
139 * VUnescape() unescapes escaped character.
141 * @param in [inout] pointer to string.
142 * @return 0 if success.
145 _VUnescape( char *in )
155 for ( i = 0, index = 0; i < len; i++ )
162 if ( c2 == ';' ) continue;
173 * VEscape() escapes character.
175 * @param in [inout] pointer to string.
176 * @return 0 if success.
188 buf = (char*) calloc(1, len*2+1);
190 for ( i = 0, index = 0; i < len; i++ ){
198 strncpy( in, buf, len*2+1 );
207 * vManySpace2Space() converts multiple spaces to single space in 'in'.
209 * @param in [inout] pointer to string.
210 * @return int length of converted string.
213 _VManySpace2Space( char *in )
219 for ( i = 0; in[i]; i++ )
221 if ( _VIsSpace( in[i] ) )
246 * vFindBase64() returns the integer repesentation of the location in base64 table.
248 * @param in a character
249 * @return int The base64 table location of input character
252 __VFindBase64( char in )
256 for ( i = 0; i < 65; i++ )
258 if ( Base64Table[i] == in )
267 * vBase64Check() returns the total length of input except non-base64 value.
269 * @param in char values which are base64 or non-base64
270 * @return int the total length of input except non-base64
273 __VBase64Check( char *in )
280 base = __VFindBase64( in[i] );
298 * vBase64Decoder() decodes the base64 encoded input.
300 * @param Src Base64 encoded input
301 * @param Dest The destination buffer of decoded value
302 * @return int The total length decoded value
305 _VB64Decode( char *Dest, char *Src )
312 char Debuffer[4] = {0x00, 0x00, 0x00, '\0'};
316 len = __VBase64Check( Src );
320 for ( i = 0; i < 3; i++ )
323 for ( i = 0; i < 4; i++, Encoded++, j++ )
325 if(*Encoded == 0x00) break;
326 if((res = __VFindBase64(*Encoded)) < 0) continue;
339 DecodeTemp = Base << 2;
340 Debuffer[0] |= DecodeTemp;
343 DecodeTemp = Base >> 4;
344 Debuffer[0] |= DecodeTemp;
345 DecodeTemp = Base << 4;
346 Debuffer[1] |= DecodeTemp;
349 DecodeTemp = Base >> 2;
350 Debuffer[1] |= DecodeTemp;
351 DecodeTemp = Base << 6;
352 Debuffer[2] |= DecodeTemp;
356 Debuffer[2] |= DecodeTemp;
368 Dest[index] = Debuffer[0];
372 Dest[index++] = Debuffer[0];
373 Dest[index++] = Debuffer[1];
379 Dest[index++] = Debuffer[0];
380 Dest[index++] = Debuffer[1];
381 Dest[index++] = Debuffer[2];
390 * vBase64Encoder() encode the input to base64.
392 * @param Src non-base64 char input
393 * @param Dest The destination buffer of encoded value
397 _VB64Encode( char *Dest, char *Src, int len )
399 char* Encoded = Dest;
404 int base1 = 0, base2 = 0;
409 for ( i = 0; i < 4; i++ )
412 for ( i = 0; i < len; i++ )
419 Debuffer[0] = *Decoded;
422 Debuffer[1] = *Decoded;
425 Debuffer[2] = *Decoded;
426 index = ( int )( ( Debuffer[0] & 0xFC ) >> 2 );
427 Enbuffer[0] = Base64Table[index];
428 base1 = ( int )( ( Debuffer[0] & 0x03 ) << 4 );
429 base2 = ( int )( ( Debuffer[1] & 0xF0 ) >> 4 );
430 index = ( int )( base1 | base2 );
431 Enbuffer[1] = Base64Table[index];
432 base1 = ( int )( ( Debuffer[1] & 0x0F ) << 2 );
433 base2 = ( int )( ( Debuffer[2] & 0xC0 ) >> 6 );
434 index = ( int )( base1 | base2 );
435 Enbuffer[2] = Base64Table[index];
436 index = ( int )( Debuffer[2] & 0x3F );
437 Enbuffer[3] = Base64Table[index];
439 Encoded[length++] = Enbuffer[0];
440 Encoded[length++] = Enbuffer[1];
441 Encoded[length++] = Enbuffer[2];
442 Encoded[length++] = Enbuffer[3];
444 for ( j = 0; j < 3; j++ )
460 index = ( int )( ( Debuffer[0] & 0xFC ) >> 2 );
461 Enbuffer[0] = Base64Table[index];
462 base1 = ( int )( ( Debuffer[0] & 0x03 ) << 4 );
463 base2 = ( int )( ( Debuffer[1] & 0xF0 ) >> 4 );
464 index = ( int )( base1 | base2 );
465 Enbuffer[1] = Base64Table[index];
466 Enbuffer[2] = Base64Table[64];
467 Enbuffer[3] = Base64Table[64];
469 Encoded[length++] = Enbuffer[0];
470 Encoded[length++] = Enbuffer[1];
471 Encoded[length++] = Enbuffer[2];
472 Encoded[length++] = Enbuffer[3];
476 index = ( int )( ( Debuffer[0] & 0xFC ) >> 2 );
477 Enbuffer[0] = Base64Table[index];
478 base1 = ( int )( ( Debuffer[0] & 0x03 ) << 4 );
479 base2 = ( int )( ( Debuffer[1] & 0xF0 ) >> 4 );
480 index = ( int )( base1 | base2 );
481 Enbuffer[1] = Base64Table[index];
482 base1 = ( int )( ( Debuffer[1] & 0x0F ) << 2 );
483 base2 = ( int )( ( Debuffer[2] & 0xC0 ) >> 6 );
484 index = ( int )( base1 | base2 );
485 Enbuffer[2] = Base64Table[index];
486 Enbuffer[3] = Base64Table[64];
488 Encoded[length++] = Enbuffer[0];
489 Encoded[length++] = Enbuffer[1];
490 Encoded[length++] = Enbuffer[2];
491 Encoded[length++] = Enbuffer[3];
496 Encoded[length] = '\0';
504 __VRepChar2Space( char *vRaw, char replaced )
510 if ( *vRaw == replaced )
522 * vUnfolding() unfold the folded line.
524 * @param string The folded line input
525 * @return int Total length of unfolded output
528 _VUnfolding( char *string )
533 len = strlen( string );
535 for ( i = 0, j = 0; i < len; i++, j++ )
537 string[j] = string[i];
539 // 12.03.2004 Process garbage character at the end of vcard/vcal
540 if ( _VIsSpace( string[i] ) && ( i < len-5 ) )
542 if ( string[i-1] == LF || string[i-1] == CR )
551 if ( string[i-2] == LF || string[i-2] == CR )
570 __VIsNewType( char *pCardRaw )
572 int count=0, i = 0, low=0, high=0, diff=0;
573 char strTypeName[50]={0};
577 if(*pCardRaw == CR || *pCardRaw == LF)
581 if(*pCardRaw == ';' || *pCardRaw == ':' || count >= 50)
586 strTypeName[count++] = *pCardRaw++;
592 for ( low = 0, high = VCARD_TYPE_NUM - 1; high >= low; diff < 0 ? ( low = i+1 ) : ( high = i-1 ) )
594 i = ( low + high ) / 2;
595 diff = strcmp( pszCardTypeList[i], strTypeName );
596 if ( diff == 0 ) /* success: found it */
603 //res = __VCardGetName( strTypeName, (char**)pszCardTypeList, VCARD_TYPE_NUM );
608 __VIsNewTypeforOrg( char *pCardRaw, int vType )
610 int count=0, i = 0, low=0, high=0, diff=0,vTypeNum;
611 char strTypeName[50]={0};
612 extern char* pszCardTypeList[];
613 extern char* pszMsgTypeList[];
617 if(*pCardRaw == CR || *pCardRaw == LF)
621 if(*pCardRaw == ';' || *pCardRaw == ':' || count >= 50)
626 strTypeName[count++] = *pCardRaw++;
631 vTypeNum = VCARD_TYPE_NUM;
632 else if(vType == VMESSAGE)
633 vTypeNum = VMSG_TYPE_NUM;
637 for ( low = 0, high = vTypeNum - 1; high >= low; diff < 0 ? ( low = i+1 ) : ( high = i-1 ) )
639 i = ( low + high ) / 2;
642 diff = strcmp( pszCardTypeList[i], strTypeName );
643 else if(vType == VMESSAGE)
644 diff = strcmp( pszMsgTypeList[i], strTypeName );
646 if ( diff == 0 ) /* success: found it */
648 else if( !strncmp( strTypeName, "X-", 2 ))
652 //if(count <= 50) return TRUE;
656 //res = __VCardGetName( strTypeName, (char**)pszCardTypeList, VCARD_TYPE_NUM );
660 _VUnfoldingNoSpecNew(char *string)
667 len = strlen( string );
669 if( !(newString = (char*) malloc(len+1) )) {
674 for ( i = 0, j = 0; i < len; i++, j++ ) {
676 newString[j] = string[i];
680 if ( string[i] == '=' ) {
682 if(string[i+1] == CR && string[i+2] == LF) {
684 if(__VIsNewType(string) == false){
689 else if(string[i+1] == CR || string[i+1] == LF) {
690 if(__VIsNewType(string) == false){
696 else if(string[i] == ' ') {
698 if(string[i-2] == CR && string[i-1] == LF) {
699 if(__VIsNewType(string) == false)
704 else if(string[i-1] == CR || string[i-1] == LF) {
709 else if((string[i] == CR || string[i] == LF) && __VIsNewType(string) == false) {
711 if(string[i+1] == LF) {
725 * vUnfolding() unfold the folded line.
727 * @param string The folded line input
728 * @return int Total length of unfolded output
731 _VUnfoldingNoSpec( char *string, int vType )
736 len = strlen( string );
738 for ( i = 0, j = 0; i < len; i++, j++ )
740 string[j] = string[i];
744 if ( string[i] == '=' )
746 if(string[i+1] == CR && string[i+2] == LF && string[i+3] =='=' )
755 else if(string[i+1] == CR && string[i+2] == LF && __VIsNewTypeforOrg(&string[i+3], vType) == false)
765 else if(string[i] ==WSP
768 if(string[i-2] == CR && string[i-1] == LF)
778 else if(string[i-1] == CR || string[i-1] == LF)
799 * vFolding() decodes the base64 encoded input.
801 * @param contentline Original line (unfolded)
802 * @param Dest The destination buffer of folded result
805 _VFolding( char *result, char *contentline )
809 while ( *contentline )
819 *result++ = *contentline++;
828 * vFolding() decodes the base64 encoded input.
830 * @param contentline Original line (unfolded)
831 * @param Dest The destination buffer of folded result
834 _VFoldingQP( char *result, char *contentline )
838 while ( *contentline )
848 *result++ = *contentline++;
857 * vFolding() decodes the base64 encoded input.
859 * @param contentline Original line (unfolded)
860 * @param Dest The destination buffer of folded result
863 _VFoldingNoSpace( char *result, char *contentline )
867 while ( *contentline )
876 *result++ = *contentline++;
885 * vQuotedPrintalbeDecoder() decodes the quoted-printable encoded input.
887 * @param Src Quoted-printable encoded input
888 * @return int The total length decoded value
891 _VQPDecode( char *src )
901 if ( !( _VIsSpace( src[i + 1] ) || ( src[i + 1] == '\r' ) || ( src[i+1] == '\n' ) ) )
903 if ( src[i + 1] == '0' && ( src[i + 2] == 'D' || src[i +2] == 'd' ) && src[i + 3] == '='
904 && src[i + 4] == '0' && ( src[i + 5] == 'A' || src[i + 5] == 'a' ) )
914 decodedNum = __VHexaDecoder( qp );
933 j = _VManySpace2Space( src );
941 * vQuotedPrintableEncoder() decodes the quoted-printalbe encoded input.
943 * @param Src Quoted-printable encoded input
944 * @param Dest The destination buffer of decoded value
945 * @return int The total length decoded value
948 _VQPEncode( char *dest, char *src )
950 int i = 0, j = 0, k = 0;
951 char encoded[2] = {0x0f, 0x0f};
953 while ( src[i] /*&& ( src[i] > 0 )*/ )
955 if ( k == 73 && _VIsSpace( src[i] ) )
959 dest[j++] = '='; dest[j++] = '2'; dest[j++] = '0';
962 else if ( src[i] == TAB )
964 dest[j++] = '='; dest[j++] = '0'; dest[j++] = '9';
968 /* else if ( k == 76 )
970 dest[j++] = '='; dest[j++] = WSP;
973 else if ( !__VIsPrintable( src[i] ) )
976 encoded[0] &= (src[i] >> 4);
977 encoded[1] &= (src[i]);
978 __VHexaEncoder( encoded );
979 dest[j++] = encoded[0]; encoded[0] = 0x0f;
980 dest[j++] = encoded[1]; encoded[1] = 0x0f;
983 else if ( src[i] == '\r' || src[i] == '\n' )
985 dest[j++] = '='; dest[j++] = '0'; dest[j++] = 'D'; k += 3;
986 dest[j++] = '='; dest[j++] = '0'; dest[j++] = 'A'; k += 3;
990 dest[j++] = src[i]; k++;
1002 * vIsPrintable() check whether the input is printable.
1005 * @return true/false if input is printable :true else : false
1008 __VIsPrintable( char in )
1010 if ( in >= 33 && in <= 60 ) return true;
1011 else if ( in >= 62 && in <= 126 ) return true;
1012 else if ( in == WSP || in == TAB ) return true;
1013 else if ( in == '\r' || in == '\n' ) return true;
1020 * vHexaDecoder() output the character value of inputed hexadecimal value.
1022 * @param qp Hexadecimal input value
1023 * @return char Character representation of input hexadecimal value
1026 __VHexaDecoder( char *qp )
1029 char decoded[2] = {0x00, 0x00};
1032 for ( i = 0; i < 2; i++ )
1093 res = ( char )( ( decoded[0] << 4 ) + decoded[1] );
1101 * vHexaEncoder() output the hexadecimal value of input character value.
1103 * @return qp Character representation of input hexadecimal value
1106 __VHexaEncoder( char *qp )
1110 for ( i = 0; i < 2; i++ )
1168 * _VIsCrLf() returns one if char is either a space, tab, or newline.
1170 * @param s1 [in] pointer to first string.
1171 * @param s2 [in] pointer to second string.
1172 * @return 1 'in' is a space character.
1173 * @return 0 'in' is not a space.
1178 if ( ( in == CR ) || ( in == LF ) )
1189 * vManySpace2Space() converts multiple spaces to single space in 'in'.
1191 * @param in [inout] pointer to string.
1192 * @return int length of converted string.
1195 _VManyCRLF2CRLF(char *pIn)
1198 bool bCrLf = false, bFirstCrLf = true;
1201 for ( i = 0; pIn[i]; i++ )
1203 if ( _VIsCrLf( pIn[i] ) && _VIsCrLf( pIn[i+1] ))
1205 if( bFirstCrLf && !bCrLf)
1209 else if( !bFirstCrLf )