00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include <qcstring.h>
00025 #include <ctype.h>
00026 
00027 #include <VCardEnum.h>
00028 
00029 using namespace VCARD;
00030 
00031 
00032     const QCString
00033 VCARD::paramNames [] =
00034 {
00035     "NAME",
00036     "PROFILE",
00037     "SOURCE",
00038     "FN",
00039     "N",
00040     "NICKNAME",
00041     "PHOTO",
00042     "BDAY",
00043     "ADR",
00044     "LABEL",
00045     "TEL",
00046     "EMAIL",
00047     "MAILER",
00048     "TZ",
00049     "GEO",
00050     "TITLE",
00051     "ROLE",
00052     "LOGO",
00053     "AGENT",
00054     "ORG",
00055     "CATEGORIES",
00056     "NOTE",
00057     "PRODID",
00058     "REV",
00059     "SORT-STRING",
00060     "SOUND",
00061     "UID",
00062     "URL",
00063     "VERSION",
00064     "CLASS",
00065     "KEY"
00066 };
00067 
00068     const ParamType
00069 VCARD::paramTypesTable[] = {
00070     ParamNone,      
00071     ParamNone,      
00072     ParamSource,        
00073     ParamText,      
00074     ParamText,      
00075     ParamText,      
00076     ParamImage,     
00077     ParamDate,      
00078     ParamAddrText,      
00079     ParamAddrText,      
00080     ParamTel,       
00081     ParamEmail,     
00082     ParamText,      
00083     ParamNone,      
00084     ParamNone,      
00085     ParamText,      
00086     ParamText,      
00087     ParamImage,     
00088     ParamAgent,     
00089     ParamText,      
00090     ParamText,      
00091     ParamText,      
00092     ParamNone,      
00093     ParamDate,      
00094     ParamText,      
00095     ParamSound,     
00096     ParamNone,      
00097     ParamNone,      
00098     ParamNone,      
00099     ParamNone,      
00100     ParamTextBin,       
00101     ParamTextNS     
00102 };
00103 
00104     ParamType
00105 VCARD::EntityTypeToParamType(EntityType e)
00106 {
00107     ParamType t(ParamUnknown);
00108 
00109     switch (e) {
00110     
00111     
00112         case EntityAgent:       t = ParamAgent;     break;
00113     
00114         case EntitySound:       t = ParamSound;     break;
00115     
00116         case EntitySource:      t = ParamSource;    break;
00117     
00118         case EntityTelephone:       t = ParamTel;       break;
00119     
00120         case EntityEmail:       t = ParamEmail;     break;
00121     
00122         case EntityKey:         t = ParamTextBin;   break;
00123     
00124         case EntityExtension:       t = ParamTextNS;    break;
00125     
00126         case EntityAddress:
00127         case EntityLabel:       t = ParamAddrText;  break;
00128     
00129         case EntityBirthday:
00130         case EntityRevision:        t = ParamDate;      break;
00131     
00132         case EntityPhoto:
00133         case EntityLogo:        t = ParamImage;     break;
00134     
00135         case EntityOrganisation:
00136         case EntityTitle:
00137         case EntityRole:
00138         case EntityFullName:
00139         case EntityMailer:
00140         case EntityN:
00141         case EntitySortString:
00142         case EntityNickname:
00143         case EntityCategories:
00144         case EntityNote:        t = ParamText;      break;
00145     
00146         case EntityProductID:
00147         case EntityTimeZone:
00148         case EntityUID:
00149         case EntityURL:
00150         case EntityClass:
00151         case EntityGeo:
00152         case EntityName:
00153         case EntityVersion:
00154         case EntityProfile:
00155         default:            t = ParamNone;      break;
00156     
00157 
00158     }
00159     
00160     return t;
00161 }
00162 
00163     ValueType
00164 VCARD::EntityTypeToValueType(EntityType e)
00165 {
00166     ValueType t(ValueUnknown);
00167 
00168     switch (e) {
00169     
00170     
00171         case EntitySound:       t = ValueSound;     break;
00172     
00173         case EntityAgent:       t = ValueAgent;     break;
00174     
00175         case EntityAddress:     t = ValueAddress;   break;
00176     
00177         case EntityTelephone:       t = ValueTel;       break;
00178     
00179         case EntityKey:         t = ValueTextBin;   break;
00180     
00181         case EntityOrganisation:    t = ValueOrg;       break;
00182     
00183         case EntityN:           t = ValueN;     break;
00184     
00185         case EntityTimeZone:        t = ValueUTC;       break;
00186     
00187         case EntityClass:       t = ValueClass;     break;
00188     
00189         case EntityGeo:         t = ValueGeo;       break;
00190     
00191         case EntitySource:
00192         case EntityURL:         t = ValueURI;       break;
00193     
00194         case EntityPhoto:
00195         case EntityLogo:        t = ValueImage;     break;
00196     
00197         case EntityBirthday:
00198         case EntityRevision:        t = ValueDate;      break;
00199     
00200         case EntityCategories:
00201         case EntityNickname:        t = ValueTextList;  break;
00202     
00203         case EntityLabel:
00204         case EntityExtension:
00205         case EntityEmail:
00206         case EntityTitle:
00207         case EntityRole:
00208         case EntityFullName:
00209         case EntityMailer:
00210         case EntityProductID:
00211         case EntityName:
00212         case EntitySortString:
00213         case EntityVersion:
00214         case EntityProfile:
00215         case EntityUID:
00216         case EntityNote:
00217         default:            t = ValueText;      break;
00218     
00219 
00220     }
00221     
00222     return t;
00223 }
00224 
00225     QCString
00226 VCARD::EntityTypeToParamName(EntityType e)
00227 {
00228     if ( e > EntityUnknown ) e = EntityUnknown;
00229     return paramNames[ int( e ) ];
00230 }
00231 
00232     EntityType
00233 VCARD::EntityNameToEntityType(const QCString & s)
00234 {
00235     if (s.isEmpty()) return EntityUnknown;
00236     
00237     EntityType t(EntityUnknown);
00238     
00239     switch (s[0]) {
00240 
00241         case 'A':
00242             if (s == "ADR")
00243                 t = EntityAddress;
00244             else if (s == "AGENT")
00245                 t = EntityAgent;
00246             break;
00247 
00248         case 'B':
00249             if (s == "BDAY")
00250                 t = EntityBirthday;
00251             break;
00252 
00253         case 'C':
00254             if (s == "CATEGORIES")
00255                 t = EntityCategories;
00256             else if (s == "CLASS")
00257                 t = EntityClass;
00258             break;
00259 
00260         case 'E':
00261             if (s == "EMAIL")
00262                 t = EntityEmail;
00263             break;
00264 
00265         case 'F':
00266             if (s == "FN")
00267                 t = EntityFullName;
00268             break;
00269 
00270         case 'G':
00271             if (s == "GEO")
00272                 t = EntityGeo;
00273             break;
00274 
00275         case 'K':
00276             if (s == "KEY")
00277                 t = EntityKey;
00278             break;
00279 
00280         case 'L':
00281             if (s == "LABEL")
00282                 t = EntityLabel;
00283             else if (s == "LOGO")
00284                 t = EntityLogo;
00285             break;
00286 
00287         case 'M':
00288             if (s == "MAILER")
00289                 t = EntityMailer;
00290             break;
00291             
00292         case 'N':
00293             if (s == "N")
00294                 t = EntityN;
00295             else if (s == "NAME")
00296                 t = EntityName;
00297             else if (s == "NICKNAME")
00298                 t = EntityNickname;
00299             else if (s == "NOTE")
00300                 t = EntityNote;
00301             break;
00302 
00303         case 'O':
00304             if (s == "ORG")
00305                 t = EntityOrganisation;
00306             break;
00307 
00308         case 'P':
00309             if (s == "PHOTO")
00310                 t = EntityPhoto;
00311             else if (s == "PRODID")
00312                 t = EntityProductID;
00313             else if (s == "PROFILE")
00314                 t = EntityProfile;
00315             break;
00316         
00317         case 'R':
00318             if (s == "REV")
00319                 t = EntityRevision;
00320             else if (s == "ROLE")
00321                 t = EntityRole;
00322             break;
00323             
00324         case 'S':
00325             if (s == "SORT-STRING")
00326                 t = EntitySortString;
00327             else if (s == "SOUND")
00328                 t = EntitySound;
00329             else if (s == "SOURCE")
00330                 t = EntitySource;
00331             break;
00332 
00333         case 'T':
00334             if (s == "TEL")
00335                 t = EntityTelephone;
00336             else if (s == "TITLE")
00337                 t = EntityTitle;
00338             else if (s == "TZ")
00339                 t = EntityTimeZone;
00340             break;
00341 
00342         case 'U':
00343             if (s == "UID")
00344                 t = EntityUID;
00345             else if (s == "URL")
00346                 t = EntityURL;
00347         case 'V':
00348             if (s == "VERSION")
00349                 t = EntityVersion;
00350             break;
00351 
00352         case 'X':
00353             if (s.left(2) == "X-")
00354                 t = EntityExtension;
00355             break;
00356             
00357         default:
00358             
00359             t = EntityUnknown;
00360     }
00361     
00362     return t;
00363 }
00364 
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00389 
00390 
00391 
00392 static char B64[] = 
00393     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00394 
00395 
00396 static signed char b64dec[] = {
00397     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00398     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00399     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00400     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00401     -1, -1, -1,-19, -1, -1, -1,-16, -4, -4, 
00402     -4, -4, -4, -4, -4, -4, -4, -4, -1, -1, 
00403     -1,  0, -1, -1, -1, 65, 65, 65, 65, 65, 
00404     65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 
00405     65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 
00406     65, -1, -1, -1, -1, -1, -1, 71, 71, 71, 
00407     71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
00408     71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
00409     71, 71, 71, -1, -1, -1, -1, -1, -1, -1, 
00410     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00411     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00412     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00413     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00414     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00415     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00416     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00417     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00418     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00419     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00420     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00421     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
00422     -1, -1, -1, -1, -1, -1, -1              
00423 };
00424 
00425     char *
00426 VCARD::decodeBase64(const char * s, unsigned long srcl, unsigned long & len)
00427 {
00428     register unsigned char c;
00429     register unsigned long e(0);
00430     len = 0;
00431     unsigned const char * src = (unsigned const char *)s;
00432     char * ret = new char[srcl + (srcl / 4 + 1)];
00433     register char *d = ret;
00434     while (srcl--) { 
00435         c = *src++;
00436         int dec = b64dec[c];
00437         if (dec == -1) continue;
00438         if (c == '=') {
00439             switch (e++) {
00440                 case 3: e = 0;                              break;
00441                 case 2: if (*src == '=')                    break;
00442                 default: delete [] ret; ret = 0; return 0;  break;
00443             }
00444             continue;
00445         }
00446         c -= dec;
00447         if (e == 0) { *d = c << 2; ++e; continue; }
00448         switch (e) {
00449             case 1: *d |= c >> 4; *++d = c << 4;    break;
00450             case 2: *d |= c >> 2; *++d = c << 6;    break;
00451             case 3: *d++ |= c; e = 0; continue;     break;
00452         }
00453         ++e;
00454     }
00455     len = d - (char *)ret;
00456     return ret;
00457 }
00458 
00459 
00460     char *
00461 VCARD::encodeBase64(const char * src, unsigned long srcl, unsigned long & destl)
00462 {
00463     register const unsigned char *s = (unsigned char *)src;
00464     register unsigned long i = ((srcl + 2) / 3) * 4;
00465     destl = i += 2 * ((i / 60) + 1);
00466     i = 0;
00467     char * ret = new char[destl];
00468     register unsigned char *d((unsigned char *)ret);
00469     while (srcl != 0) { 
00470         *d++ = B64[s[0] >> 2];
00471         *d++ = B64[((s[0] << 4) + (--srcl == 0 ? 0 : s[1] >> 4)) & 0x3f];
00472         *d++ = srcl == 0 ? '=' :
00473             B64[((s[1] << 2) + (--srcl == 0 ? 0 : s[2] >> 6)) & 0x3f];
00474         *d++ = srcl == 0 ?  '=' : B64[s[2] & 0x3f];
00475         if (srcl != 0) srcl--;
00476         if (++i == 15) { i = 0; *d++ = '\r'; *d++ = '\n'; }
00477         s += 3;
00478     }
00479     *d = '\r'; *++d = '\n'; *++d = '\0';
00480     return ret;
00481 }
00482