Enum.cpp
00001 /* 00002 libvcard - vCard parsing library for vCard version 3.0 00003 00004 Copyright (C) 1998 Rik Hemsley rik@kde.org 00005 00006 Permission is hereby granted, free of charge, to any person obtaining a copy 00007 of this software and associated documentation files (the "Software"), to 00008 deal in the Software without restriction, including without limitation the 00009 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 00010 sell copies of the Software, and to permit persons to whom the Software is 00011 furnished to do so, subject to the following conditions: 00012 00013 The above copyright notice and this permission notice shall be included in 00014 all copies or substantial portions of the Software. 00015 00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 00020 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00021 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00022 */ 00023 00024 #include <tqcstring.h> 00025 #include <ctype.h> 00026 00027 #include <VCardEnum.h> 00028 00029 using namespace VCARD; 00030 00031 // There are 31 possible types, not including extensions. 00032 // URI is a custom field designed to store the upstream URI for each contact 00033 // in order to handle certain limited CardDAV systems such as Zimbra 00034 const TQCString 00035 VCARD::paramNames [] = 00036 { 00037 "NAME", 00038 "PROFILE", 00039 "SOURCE", 00040 "FN", 00041 "N", 00042 "NICKNAME", 00043 "PHOTO", 00044 "BDAY", 00045 "ADR", 00046 "LABEL", 00047 "TEL", 00048 "EMAIL", 00049 "MAILER", 00050 "TZ", 00051 "GEO", 00052 "TITLE", 00053 "ROLE", 00054 "LOGO", 00055 "AGENT", 00056 "ORG", 00057 "CATEGORIES", 00058 "NOTE", 00059 "PRODID", 00060 "REV", 00061 "SORT-STRING", 00062 "SOUND", 00063 "UID", 00064 "URL", 00065 "VERSION", 00066 "CLASS", 00067 "KEY", 00068 "URI" 00069 }; 00070 00071 const ParamType 00072 VCARD::paramTypesTable[] = { 00073 ParamNone, // NAME 00074 ParamNone, // PROFILE 00075 ParamSource, // SOURCE 00076 ParamText, // FN 00077 ParamText, // N 00078 ParamText, // NICKNAME 00079 ParamImage, // PHOTO (inline/refer) 00080 ParamDate, // BDAY ("VALUE = "date-time/date) 00081 ParamAddrText, // ADR (adr-param/text-param) 00082 ParamAddrText, // LABEL (adr-param/text-param) 00083 ParamTel, // TEL 00084 ParamEmail, // EMAIL 00085 ParamText, // MAILER 00086 ParamNone, // TZ 00087 ParamNone, // GEO 00088 ParamText, // TITLE 00089 ParamText, // ROLE 00090 ParamImage, // LOGO 00091 ParamAgent, // AGENT 00092 ParamText, // ORG 00093 ParamText, // CATEGORIES 00094 ParamText, // NOTE 00095 ParamNone, // PRODID 00096 ParamDate, // REV 00097 ParamText, // SORT-STRING 00098 ParamSound, // SOUND 00099 ParamNone, // UID 00100 ParamNone, // URL 00101 ParamNone, // VERSION 00102 ParamNone, // CLASS 00103 ParamTextBin, // KEY 00104 ParamTextNS, // X 00105 ParamNone // URI 00106 }; 00107 00108 ParamType 00109 VCARD::EntityTypeToParamType(EntityType e) 00110 { 00111 ParamType t(ParamUnknown); 00112 00113 switch (e) { 00114 00115 //---------------------------------------------------------------// 00116 case EntityAgent: t = ParamAgent; break; 00117 //---------------------------------------------------------------// 00118 case EntitySound: t = ParamSound; break; 00119 //---------------------------------------------------------------// 00120 case EntitySource: t = ParamSource; break; 00121 //---------------------------------------------------------------// 00122 case EntityTelephone: t = ParamTel; break; 00123 //---------------------------------------------------------------// 00124 case EntityEmail: t = ParamEmail; break; 00125 //---------------------------------------------------------------// 00126 case EntityKey: t = ParamTextBin; break; 00127 //---------------------------------------------------------------// 00128 case EntityExtension: t = ParamTextNS; break; 00129 //---------------------------------------------------------------// 00130 case EntityAddress: 00131 case EntityLabel: t = ParamAddrText; break; 00132 //---------------------------------------------------------------// 00133 case EntityBirthday: 00134 case EntityRevision: t = ParamDate; break; 00135 //---------------------------------------------------------------// 00136 case EntityPhoto: 00137 case EntityLogo: t = ParamImage; break; 00138 //---------------------------------------------------------------// 00139 case EntityOrganisation: 00140 case EntityTitle: 00141 case EntityRole: 00142 case EntityFullName: 00143 case EntityMailer: 00144 case EntityN: 00145 case EntitySortString: 00146 case EntityNickname: 00147 case EntityCategories: 00148 case EntityNote: t = ParamText; break; 00149 //---------------------------------------------------------------// 00150 case EntityProductID: 00151 case EntityTimeZone: 00152 case EntityUID: 00153 case EntityURL: 00154 case EntityClass: 00155 case EntityGeo: 00156 case EntityName: 00157 case EntityVersion: 00158 case EntityProfile: 00159 case EntityURI: 00160 default: t = ParamNone; break; 00161 //---------------------------------------------------------------// 00162 00163 } 00164 00165 return t; 00166 } 00167 00168 ValueType 00169 VCARD::EntityTypeToValueType(EntityType e) 00170 { 00171 ValueType t(ValueUnknown); 00172 00173 switch (e) { 00174 00175 //---------------------------------------------------------------// 00176 case EntitySound: t = ValueSound; break; 00177 //---------------------------------------------------------------// 00178 case EntityAgent: t = ValueAgent; break; 00179 //---------------------------------------------------------------// 00180 case EntityAddress: t = ValueAddress; break; 00181 //---------------------------------------------------------------// 00182 case EntityTelephone: t = ValueTel; break; 00183 //---------------------------------------------------------------// 00184 case EntityKey: t = ValueTextBin; break; 00185 //---------------------------------------------------------------// 00186 case EntityOrganisation: t = ValueOrg; break; 00187 //---------------------------------------------------------------// 00188 case EntityN: t = ValueN; break; 00189 //---------------------------------------------------------------// 00190 case EntityTimeZone: t = ValueUTC; break; 00191 //---------------------------------------------------------------// 00192 case EntityClass: t = ValueClass; break; 00193 //---------------------------------------------------------------// 00194 case EntityGeo: t = ValueGeo; break; 00195 //---------------------------------------------------------------// 00196 case EntitySource: 00197 case EntityURL: t = ValueURI; break; 00198 //---------------------------------------------------------------// 00199 case EntityPhoto: 00200 case EntityLogo: t = ValueImage; break; 00201 //---------------------------------------------------------------// 00202 case EntityBirthday: 00203 case EntityRevision: t = ValueDate; break; 00204 //---------------------------------------------------------------// 00205 case EntityCategories: 00206 case EntityNickname: t = ValueTextList; break; 00207 //---------------------------------------------------------------// 00208 case EntityLabel: 00209 case EntityExtension: 00210 case EntityEmail: 00211 case EntityTitle: 00212 case EntityRole: 00213 case EntityFullName: 00214 case EntityMailer: 00215 case EntityProductID: 00216 case EntityName: 00217 case EntitySortString: 00218 case EntityVersion: 00219 case EntityProfile: 00220 case EntityUID: 00221 case EntityNote: 00222 case EntityURI: 00223 default: t = ValueText; break; 00224 //---------------------------------------------------------------// 00225 00226 } 00227 00228 return t; 00229 } 00230 00231 TQCString 00232 VCARD::EntityTypeToParamName(EntityType e) 00233 { 00234 if ( e > EntityUnknown ) e = EntityUnknown; 00235 return paramNames[ int( e ) ]; 00236 } 00237 00238 EntityType 00239 VCARD::EntityNameToEntityType(const TQCString & s) 00240 { 00241 if (s.isEmpty()) return EntityUnknown; 00242 00243 EntityType t(EntityUnknown); 00244 00245 switch (s[0]) { 00246 00247 case 'A': 00248 if (s == "ADR") 00249 t = EntityAddress; 00250 else if (s == "AGENT") 00251 t = EntityAgent; 00252 break; 00253 00254 case 'B': 00255 if (s == "BDAY") 00256 t = EntityBirthday; 00257 break; 00258 00259 case 'C': 00260 if (s == "CATEGORIES") 00261 t = EntityCategories; 00262 else if (s == "CLASS") 00263 t = EntityClass; 00264 break; 00265 00266 case 'E': 00267 if (s == "EMAIL") 00268 t = EntityEmail; 00269 break; 00270 00271 case 'F': 00272 if (s == "FN") 00273 t = EntityFullName; 00274 break; 00275 00276 case 'G': 00277 if (s == "GEO") 00278 t = EntityGeo; 00279 break; 00280 00281 case 'K': 00282 if (s == "KEY") 00283 t = EntityKey; 00284 break; 00285 00286 case 'L': 00287 if (s == "LABEL") 00288 t = EntityLabel; 00289 else if (s == "LOGO") 00290 t = EntityLogo; 00291 break; 00292 00293 case 'M': 00294 if (s == "MAILER") 00295 t = EntityMailer; 00296 break; 00297 00298 case 'N': 00299 if (s == "N") 00300 t = EntityN; 00301 else if (s == "NAME") 00302 t = EntityName; 00303 else if (s == "NICKNAME") 00304 t = EntityNickname; 00305 else if (s == "NOTE") 00306 t = EntityNote; 00307 break; 00308 00309 case 'O': 00310 if (s == "ORG") 00311 t = EntityOrganisation; 00312 break; 00313 00314 case 'P': 00315 if (s == "PHOTO") 00316 t = EntityPhoto; 00317 else if (s == "PRODID") 00318 t = EntityProductID; 00319 else if (s == "PROFILE") 00320 t = EntityProfile; 00321 break; 00322 00323 case 'R': 00324 if (s == "REV") 00325 t = EntityRevision; 00326 else if (s == "ROLE") 00327 t = EntityRole; 00328 break; 00329 00330 case 'S': 00331 if (s == "SORT-STRING") 00332 t = EntitySortString; 00333 else if (s == "SOUND") 00334 t = EntitySound; 00335 else if (s == "SOURCE") 00336 t = EntitySource; 00337 break; 00338 00339 case 'T': 00340 if (s == "TEL") 00341 t = EntityTelephone; 00342 else if (s == "TITLE") 00343 t = EntityTitle; 00344 else if (s == "TZ") 00345 t = EntityTimeZone; 00346 break; 00347 00348 case 'U': 00349 if (s == "UID") 00350 t = EntityUID; 00351 else if (s == "URL") 00352 t = EntityURL; 00353 else if (s == "URI") 00354 t = EntityURI; 00355 case 'V': 00356 if (s == "VERSION") 00357 t = EntityVersion; 00358 break; 00359 00360 case 'X': 00361 if (s.left(2) == "X-") 00362 t = EntityExtension; 00363 break; 00364 00365 default: 00366 00367 t = EntityUnknown; 00368 } 00369 00370 return t; 00371 } 00372 00373 // The copyright notice below refers to the base64 codec functions used below, 00374 // which are modified from the original sources. 00375 00376 /* 00377 * Original version Copyright 1988 by The Leland Stanford Junior University 00378 * Copyright 1998 by the University of Washington 00379 * 00380 * Permission to use, copy, modify, and distribute this software and its 00381 * documentation for any purpose and without fee is hereby granted, provided 00382 * that the above copyright notices appear in all copies and that both the 00383 * above copyright notices and this permission notice appear in supporting 00384 * documentation, and that the name of the University of Washington or The 00385 * Leland Stanford Junior University not be used in advertising or publicity 00386 * pertaining to distribution of the software without specific, written prior 00387 * permission. This software is made available "as is", and 00388 * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY 00389 * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE, 00390 * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 00391 * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF 00392 * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY 00393 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 00394 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 00395 * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF 00396 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00397 * 00398 */ 00399 00400 static char B64[] = 00401 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00402 00403 // the mime base64 disctionary used for decoding 00404 static signed char b64dec[] = { 00405 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 00406 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10 00407 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20 00408 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 30 00409 -1, -1, -1,-19, -1, -1, -1,-16, -4, -4, // 40 -19 == '+' -16 == '/' 00410 -4, -4, -4, -4, -4, -4, -4, -4, -1, -1, // 50 -4 == '0' 00411 -1, 0, -1, -1, -1, 65, 65, 65, 65, 65, // 60 0 == '=' 65 == 'A' 00412 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, // 70 00413 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, // 80 00414 65, -1, -1, -1, -1, -1, -1, 71, 71, 71, // 90 71 == 'a' 00415 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, // 100 00416 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, // 110 00417 71, 71, 71, -1, -1, -1, -1, -1, -1, -1, // 120 00418 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 130 00419 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 140 00420 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 150 00421 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160 00422 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 170 00423 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 180 00424 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 190 00425 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 200 00426 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 210 00427 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 220 00428 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 230 00429 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 240 00430 -1, -1, -1, -1, -1, -1, -1 // 250 00431 }; 00432 00433 char * 00434 VCARD::decodeBase64(const char * s, unsigned long srcl, unsigned long & len) 00435 { 00436 unsigned char c; 00437 unsigned long e(0); 00438 len = 0; 00439 unsigned const char * src = (unsigned const char *)s; 00440 char * ret = new char[srcl + (srcl / 4 + 1)]; 00441 char *d = ret; 00442 while (srcl--) { // Critical loop 00443 c = *src++; 00444 int dec = b64dec[c]; 00445 if (dec == -1) continue; 00446 if (c == '=') { 00447 switch (e++) { 00448 case 3: e = 0; break; 00449 case 2: if (*src == '=') break; 00450 default: delete [] ret; ret = 0; return 0; break; 00451 } 00452 continue; 00453 } 00454 c -= dec; 00455 if (e == 0) { *d = c << 2; ++e; continue; } 00456 switch (e) { 00457 case 1: *d |= c >> 4; *++d = c << 4; break; 00458 case 2: *d |= c >> 2; *++d = c << 6; break; 00459 case 3: *d++ |= c; e = 0; continue; break; 00460 } 00461 ++e; 00462 } 00463 len = d - (char *)ret; 00464 return ret; 00465 } 00466 00467 00468 char * 00469 VCARD::encodeBase64(const char * src, unsigned long srcl, unsigned long & destl) 00470 { 00471 const unsigned char *s = (unsigned char *)src; 00472 unsigned long i = ((srcl + 2) / 3) * 4; 00473 destl = i += 2 * ((i / 60) + 1); 00474 i = 0; 00475 char * ret = new char[destl]; 00476 unsigned char *d((unsigned char *)ret); 00477 while (srcl != 0) { // Critical loop 00478 *d++ = B64[s[0] >> 2]; 00479 *d++ = B64[((s[0] << 4) + (--srcl == 0 ? 0 : s[1] >> 4)) & 0x3f]; 00480 *d++ = srcl == 0 ? '=' : 00481 B64[((s[1] << 2) + (--srcl == 0 ? 0 : s[2] >> 6)) & 0x3f]; 00482 *d++ = srcl == 0 ? '=' : B64[s[2] & 0x3f]; 00483 if (srcl != 0) srcl--; 00484 if (++i == 15) { i = 0; *d++ = '\r'; *d++ = '\n'; } 00485 s += 3; 00486 } 00487 *d = '\r'; *++d = '\n'; *++d = '\0'; 00488 return ret; 00489 } 00490