-rw-r--r-- | libkcal/versit/vobject.c | 363 |
1 files changed, 192 insertions, 171 deletions
diff --git a/libkcal/versit/vobject.c b/libkcal/versit/vobject.c index 637efb2..3fac63e 100644 --- a/libkcal/versit/vobject.c +++ b/libkcal/versit/vobject.c @@ -44,14 +44,15 @@ DFARS 252.227-7013 or 48 CFR 52.227-19, as applicable. -#include <stdlib.h> +#ifdef WIN32 +#define snprintf _snprintf +#define strcasecmp stricmp +#endif #include "vobject.h" +#include <stdlib.h> #include <string.h> #include <stdio.h> -#ifdef _WIN32_ +#include <fcntl.h> - #define strcasecmp _stricmp -#endif - -#define NAME_OF(o) o->id +#define NAME_OF(o) o->id #define VALUE_TYPE(o) o->valType @@ -64,2 +65,27 @@ DFARS 252.227-7013 or 48 CFR 52.227-19, as applicable. +typedef union ValueItem { + const char *strs; + const wchar_t *ustrs; + unsigned int i; + unsigned long l; + void *any; + VObject *vobj; + } ValueItem; + +struct VObject { + VObject *next; + const char *id; + VObject *prop; + unsigned short valType; + ValueItem val; + }; + +typedef struct StrItem StrItem; + +struct StrItem { + StrItem *next; + const char *s; + unsigned int refCnt; + }; + const char** fieldedProp; @@ -78,3 +104,3 @@ const char** fieldedProp; -VObject* newVObject_(const char *id) +DLLEXPORT(VObject*) newVObject_(const char *id) { @@ -89,3 +115,3 @@ VObject* newVObject_(const char *id) -VObject* newVObject(const char *id) +DLLEXPORT(VObject*) newVObject(const char *id) { @@ -94,12 +120,9 @@ VObject* newVObject(const char *id) -void deleteVObject(VObject *p) +DLLEXPORT(void) deleteVObject(VObject *p) { - if (p->id) unUseStr(p->id); - if (p) free(p); - p = NULL; } -char* dupStr(const char *s, unsigned int size) +DLLEXPORT(char*) dupStr(const char *s, unsigned int size) { @@ -120,7 +143,5 @@ char* dupStr(const char *s, unsigned int size) -void deleteStr(const char *p) +DLLEXPORT(void) deleteStr(const char *p) { - if (p) - free((void*)p); - p = NULL; + if (p) free((void*)p); } @@ -139,5 +160,3 @@ static void deleteStrItem(StrItem *p) { - if (p) - free((void*)p); - p = NULL; + free((void*)p); } @@ -149,3 +168,3 @@ static void deleteStrItem(StrItem *p) -const char* vObjectName(VObject *o) +DLLEXPORT(const char*) vObjectName(VObject *o) { @@ -154,3 +173,3 @@ const char* vObjectName(VObject *o) -void setVObjectName(VObject *o, const char* id) +DLLEXPORT(void) setVObjectName(VObject *o, const char* id) { @@ -159,3 +178,3 @@ void setVObjectName(VObject *o, const char* id) -const char* vObjectStringZValue(VObject *o) +DLLEXPORT(const char*) vObjectStringZValue(VObject *o) { @@ -164,3 +183,3 @@ const char* vObjectStringZValue(VObject *o) -void setVObjectStringZValue(VObject *o, const char *s) +DLLEXPORT(void) setVObjectStringZValue(VObject *o, const char *s) { @@ -170,3 +189,3 @@ void setVObjectStringZValue(VObject *o, const char *s) -void setVObjectStringZValue_(VObject *o, const char *s) +DLLEXPORT(void) setVObjectStringZValue_(VObject *o, const char *s) { @@ -176,3 +195,3 @@ void setVObjectStringZValue_(VObject *o, const char *s) -const wchar_t* vObjectUStringZValue(VObject *o) +DLLEXPORT(const wchar_t*) vObjectUStringZValue(VObject *o) { @@ -181,3 +200,3 @@ const wchar_t* vObjectUStringZValue(VObject *o) -void setVObjectUStringZValue(VObject *o, const wchar_t *s) +DLLEXPORT(void) setVObjectUStringZValue(VObject *o, const wchar_t *s) { @@ -187,3 +206,3 @@ void setVObjectUStringZValue(VObject *o, const wchar_t *s) -void setVObjectUStringZValue_(VObject *o, const wchar_t *s) +DLLEXPORT(void) setVObjectUStringZValue_(VObject *o, const wchar_t *s) { @@ -193,3 +212,3 @@ void setVObjectUStringZValue_(VObject *o, const wchar_t *s) -unsigned int vObjectIntegerValue(VObject *o) +DLLEXPORT(unsigned int) vObjectIntegerValue(VObject *o) { @@ -198,3 +217,3 @@ unsigned int vObjectIntegerValue(VObject *o) -void setVObjectIntegerValue(VObject *o, unsigned int i) +DLLEXPORT(void) setVObjectIntegerValue(VObject *o, unsigned int i) { @@ -204,3 +223,3 @@ void setVObjectIntegerValue(VObject *o, unsigned int i) -unsigned long vObjectLongValue(VObject *o) +DLLEXPORT(unsigned long) vObjectLongValue(VObject *o) { @@ -209,3 +228,3 @@ unsigned long vObjectLongValue(VObject *o) -void setVObjectLongValue(VObject *o, unsigned long l) +DLLEXPORT(void) setVObjectLongValue(VObject *o, unsigned long l) { @@ -215,3 +234,3 @@ void setVObjectLongValue(VObject *o, unsigned long l) -void* vObjectAnyValue(VObject *o) +DLLEXPORT(void*) vObjectAnyValue(VObject *o) { @@ -220,3 +239,3 @@ void* vObjectAnyValue(VObject *o) -void setVObjectAnyValue(VObject *o, void *t) +DLLEXPORT(void) setVObjectAnyValue(VObject *o, void *t) { @@ -226,3 +245,3 @@ void setVObjectAnyValue(VObject *o, void *t) -VObject* vObjectVObjectValue(VObject *o) +DLLEXPORT(VObject*) vObjectVObjectValue(VObject *o) { @@ -231,3 +250,3 @@ VObject* vObjectVObjectValue(VObject *o) -void setVObjectVObjectValue(VObject *o, VObject *p) +DLLEXPORT(void) setVObjectVObjectValue(VObject *o, VObject *p) { @@ -237,3 +256,3 @@ void setVObjectVObjectValue(VObject *o, VObject *p) -int vObjectValueType(VObject *o) +DLLEXPORT(int) vObjectValueType(VObject *o) { @@ -247,3 +266,3 @@ int vObjectValueType(VObject *o) -VObject* addVObjectProp(VObject *o, VObject *p) +DLLEXPORT(VObject*) addVObjectProp(VObject *o, VObject *p) { @@ -282,3 +301,3 @@ VObject* addVObjectProp(VObject *o, VObject *p) -VObject* addProp(VObject *o, const char *id) +DLLEXPORT(VObject*) addProp(VObject *o, const char *id) { @@ -287,3 +306,3 @@ VObject* addProp(VObject *o, const char *id) -VObject* addProp_(VObject *o, const char *id) +DLLEXPORT(VObject*) addProp_(VObject *o, const char *id) { @@ -292,3 +311,3 @@ VObject* addProp_(VObject *o, const char *id) -void addList(VObject **o, VObject *p) +DLLEXPORT(void) addList(VObject **o, VObject *p) { @@ -307,3 +326,3 @@ void addList(VObject **o, VObject *p) -VObject* nextVObjectInList(VObject *o) +DLLEXPORT(VObject*) nextVObjectInList(VObject *o) { @@ -312,3 +331,3 @@ VObject* nextVObjectInList(VObject *o) -VObject* setValueWithSize_(VObject *prop, void *val, unsigned int size) +DLLEXPORT(VObject*) setValueWithSize_(VObject *prop, void *val, unsigned int size) { @@ -321,5 +340,5 @@ VObject* setValueWithSize_(VObject *prop, void *val, unsigned int size) -VObject* setValueWithSize(VObject *prop, void *val, unsigned int size) +DLLEXPORT(VObject*) setValueWithSize(VObject *prop, void *val, unsigned int size) { - void *p = dupStr(val,size); + void *p = dupStr((const char *)val,size); return setValueWithSize_(prop,p,p?size:0); @@ -327,3 +346,3 @@ VObject* setValueWithSize(VObject *prop, void *val, unsigned int size) -void initPropIterator(VObjectIterator *i, VObject *o) +DLLEXPORT(void) initPropIterator(VObjectIterator *i, VObject *o) { @@ -333,3 +352,3 @@ void initPropIterator(VObjectIterator *i, VObject *o) -void initVObjectIterator(VObjectIterator *i, VObject *o) +DLLEXPORT(void) initVObjectIterator(VObjectIterator *i, VObject *o) { @@ -339,3 +358,3 @@ void initVObjectIterator(VObjectIterator *i, VObject *o) -int moreIteration(VObjectIterator *i) +DLLEXPORT(int) moreIteration(VObjectIterator *i) { @@ -344,3 +363,3 @@ int moreIteration(VObjectIterator *i) -VObject* nextVObject(VObjectIterator *i) +DLLEXPORT(VObject*) nextVObject(VObjectIterator *i) { @@ -359,3 +378,3 @@ VObject* nextVObject(VObjectIterator *i) -VObject* isAPropertyOf(VObject *o, const char *id) +DLLEXPORT(VObject*) isAPropertyOf(VObject *o, const char *id) { @@ -365,3 +384,3 @@ VObject* isAPropertyOf(VObject *o, const char *id) VObject *each = nextVObject(&i); - if (!strcasecmp(id,each->id)) + if (!stricmp(id,each->id)) return each; @@ -371,3 +390,3 @@ VObject* isAPropertyOf(VObject *o, const char *id) -VObject* addGroup(VObject *o, const char *g) +DLLEXPORT(VObject*) addGroup(VObject *o, const char *g) { @@ -412,3 +431,3 @@ VObject* addGroup(VObject *o, const char *g) -VObject* addPropValue(VObject *o, const char *p, const char *v) +DLLEXPORT(VObject*) addPropValue(VObject *o, const char *p, const char *v) { @@ -420,3 +439,3 @@ VObject* addPropValue(VObject *o, const char *p, const char *v) -VObject* addPropSizedValue_(VObject *o, const char *p, const char *v, +DLLEXPORT(VObject*) addPropSizedValue_(VObject *o, const char *p, const char *v, unsigned int size) @@ -429,3 +448,3 @@ VObject* addPropSizedValue_(VObject *o, const char *p, const char *v, -VObject* addPropSizedValue(VObject *o, const char *p, const char *v, +DLLEXPORT(VObject*) addPropSizedValue(VObject *o, const char *p, const char *v, unsigned int size) @@ -530,3 +549,3 @@ void printVObject(FILE *fp,VObject *o) -void printVObjectToFile(char *fname,VObject *o) +DLLEXPORT(void) printVObjectToFile(char *fname,VObject *o) { @@ -539,3 +558,3 @@ void printVObjectToFile(char *fname,VObject *o) -void printVObjectsToFile(char *fname,VObject *list) +DLLEXPORT(void) printVObjectsToFile(char *fname,VObject *list) { @@ -551,3 +570,3 @@ void printVObjectsToFile(char *fname,VObject *list) -void cleanVObject(VObject *o) +DLLEXPORT(void) cleanVObject(VObject *o) { @@ -575,3 +594,3 @@ void cleanVObject(VObject *o) case VCVT_RAW: - /* assume they are all allocated by malloc. */ + /* assume they are all allocated by malloc. */ free((char*)STRINGZ_VALUE_OF(o)); @@ -585,3 +604,3 @@ void cleanVObject(VObject *o) -void cleanVObjects(VObject *list) +DLLEXPORT(void) cleanVObjects(VObject *list) { @@ -612,65 +631,61 @@ static unsigned int hashStr(const char *s) -const char* lookupStr(const char *s) +DLLEXPORT(const char*) lookupStr(const char *s) { - char *newS; - - StrItem *t; - unsigned int h = hashStr(s); - if ((t = strTbl[h]) != 0) { - do { - if (strcasecmp(t->s,s) == 0) { - t->refCnt++; - return t->s; - } - t = t->next; - } while (t); - } - newS = dupStr(s,0); - strTbl[h] = newStrItem(newS,strTbl[h]); - return newS; + StrItem *t; + unsigned int h = hashStr(s); + if ((t = strTbl[h]) != 0) { + do { + if (stricmp(t->s,s) == 0) { + t->refCnt++; + return t->s; + } + t = t->next; + } while (t); + } + s = dupStr(s,0); + strTbl[h] = newStrItem(s,strTbl[h]); + return s; } -void unUseStr(const char *s) +DLLEXPORT(void) unUseStr(const char *s) { - StrItem *cur, *prev; - + StrItem *t, *p; unsigned int h = hashStr(s); - cur = strTbl[h]; - prev = cur; - while (cur != 0) { - if (strcasecmp(cur->s,s) == 0) { - cur->refCnt--; - /* if that was the last reference to this string, kill it. */ - if (cur->refCnt == 0) { - if (cur == strTbl[h]) { - strTbl[h] = cur->next; - deleteStr(prev->s); - deleteStrItem(prev); - } else { - prev->next = cur->next; - deleteStr(cur->s); - deleteStrItem(cur); - } - return; + if ((t = strTbl[h]) != 0) { + p = t; + do { + if (stricmp(t->s,s) == 0) { + t->refCnt--; + if (t->refCnt == 0) { + if (p == strTbl[h]) { + strTbl[h] = t->next; + } + else { + p->next = t->next; + } + deleteStr(t->s); + deleteStrItem(t); + return; + } + } + p = t; + t = t->next; + } while (t); } - } - prev = cur; - cur = cur->next; - } } -void cleanStrTbl() +DLLEXPORT(void) cleanStrTbl() { - int i; - for (i=0; i<STRTBLSIZE;i++) { - StrItem *t = strTbl[i]; - while (t) { - StrItem *p; - deleteStr(t->s); - p = t; - t = t->next; - deleteStrItem(p); - } - strTbl[i] = 0; - } + int i; + for (i=0; i<STRTBLSIZE;i++) { + StrItem *t = strTbl[i]; + while (t) { + StrItem *p; + deleteStr(t->s); + p = t; + t = t->next; + deleteStrItem(p); + } while (t); + strTbl[i] = 0; + } } @@ -809,3 +824,3 @@ static struct PreDefProp propNames[] = { { VCExNumProp, 0, 0, 0 }, - { VCExDateProp, 0, 0, 0 }, + { VCExpDateProp, 0, 0, 0 }, { VCExpectProp, 0, 0, 0 }, @@ -923,3 +938,3 @@ static struct PreDefProp* lookupPropInfo(const char* str) for (i = 0; propNames[i].name; i++) - if (strcasecmp(str, propNames[i].name) == 0) { + if (stricmp(str, propNames[i].name) == 0) { return &propNames[i]; @@ -931,3 +946,3 @@ static struct PreDefProp* lookupPropInfo(const char* str) -const char* lookupProp_(const char* str) +DLLEXPORT(const char*) lookupProp_(const char* str) { @@ -936,3 +951,3 @@ const char* lookupProp_(const char* str) for (i = 0; propNames[i].name; i++) - if (strcasecmp(str, propNames[i].name) == 0) { + if (stricmp(str, propNames[i].name) == 0) { const char* s; @@ -945,3 +960,3 @@ const char* lookupProp_(const char* str) -const char* lookupProp(const char* str) +DLLEXPORT(const char*) lookupProp(const char* str) { @@ -950,3 +965,3 @@ const char* lookupProp(const char* str) for (i = 0; propNames[i].name; i++) - if (strcasecmp(str, propNames[i].name) == 0) { + if (stricmp(str, propNames[i].name) == 0) { const char *s; @@ -974,7 +989,3 @@ typedef struct OFile { - -/* vCalendar files need crlf linebreaks. The disabled functions didn't provide - that. */ #if 0 - static void appendsOFile(OFile *fp, const char *s) @@ -997,6 +1008,3 @@ stuff: if (OFILE_REALLOC_SIZE <= slen) fp->limit += slen; - if (fp->s) - fp->s = realloc(fp->s,fp->limit); - else - fp->s = malloc(fp->limit); + fp->s = (char *) realloc(fp->s,fp->limit); if (fp->s) goto stuff; @@ -1025,3 +1033,3 @@ stuff: fp->limit = fp->limit + OFILE_REALLOC_SIZE; - fp->s = realloc(fp->s,fp->limit); + fp->s = (char *) realloc(fp->s,fp->limit); if (fp->s) goto stuff; @@ -1034,5 +1042,3 @@ stuff: } - #else - static void appendcOFile_(OFile *fp, char c) @@ -1117,3 +1123,3 @@ static int writeBase64(OFile *fp, unsigned char *s, long len) while (cur < len) { - /* collect the triplet of bytes into 'trip' */ + /* collect the triplet of bytes into 'trip' */ trip = 0; @@ -1129,3 +1135,3 @@ static int writeBase64(OFile *fp, unsigned char *s, long len) if ((3 - i) < (cur - len)) - quad[i] = '='; /* pad char */ + quad[i] = '='; /* pad char */ else if (b < 26) quad[i] = (char)b + 'A'; @@ -1147,15 +1153,30 @@ static int writeBase64(OFile *fp, unsigned char *s, long len) -/* this function really sucks. Too basic. */ -static void writeQPString(OFile *fp, const char *s, int qp) +static void writeString(OFile *fp, const char *s) +{ + appendsOFile(fp,s); +} + +static void writeQPString(OFile *fp, const char *s) { + char buf[4]; + int count=0; const char *p = s; + while (*p) { - if (*p == '\n') { - if (p[1]) appendsOFile(fp,"=0A="); - } - if (*p == '=' && qp) - appendsOFile(fp,"=3D"); - else - appendcOFile(fp,*p); - p++; + /* break up lines biggger than 75 chars */ + if(count >=74){ + count=0; + appendsOFile(fp,"=\n"); + } + + /* escape any non ASCII characters and '=' as per rfc1521 */ + if (*p<= 0x1f || *p >=0x7f || *p == '=' ) { + sprintf(buf,"=%02X",(unsigned char)*p); + appendsOFile(fp,buf); + count+=3; + } else { + appendcOFile(fp,*p); + count++; + } + p++; } @@ -1163,5 +1184,7 @@ static void writeQPString(OFile *fp, const char *s, int qp) + + static void writeVObject_(OFile *fp, VObject *o); -static void writeValue(OFile *fp, VObject *o, unsigned long size) +static void writeValue(OFile *fp, VObject *o, unsigned long size,int quote) { @@ -1171,6 +1194,4 @@ static void writeValue(OFile *fp, VObject *o, unsigned long size) char *s = fakeCString(USTRINGZ_VALUE_OF(o)); - if (isAPropertyOf(o, VCQuotedPrintableProp)) - writeQPString(fp, s, 1); - else - writeQPString(fp, s, 0); + if(quote) writeQPString(fp, s); + else writeString(fp,s); deleteStr(s); @@ -1179,6 +1200,4 @@ static void writeValue(OFile *fp, VObject *o, unsigned long size) case VCVT_STRINGZ: { - if (isAPropertyOf(o, VCQuotedPrintableProp)) - writeQPString(fp, STRINGZ_VALUE_OF(o), 1); - else - writeQPString(fp, STRINGZ_VALUE_OF(o), 0); + if(quote) writeQPString(fp, STRINGZ_VALUE_OF(o)); + else writeString(fp,STRINGZ_VALUE_OF(o)); break; @@ -1222,3 +1241,3 @@ static void writeAttrValue(OFile *fp, VObject *o) appendcOFile(fp,'='); - writeValue(fp,o,0); + writeValue(fp,o,0,0); } @@ -1232,6 +1251,5 @@ static void writeGroup(OFile *fp, VObject *o) while ((o=isAPropertyOf(o,VCGroupingProp)) != 0) { - strncpy(buf2,STRINGZ_VALUE_OF(o),sizeof(buf2)); - buf2[sizeof(buf2)] = '\0'; - strncat(buf2,".",sizeof(buf2)-strlen(buf2)-1); - strncat(buf2,buf1,sizeof(buf2)-strlen(buf2)-1); + strcpy(buf2,STRINGZ_VALUE_OF(o)); + strcat(buf2,"."); + strcat(buf2,buf1); strcpy(buf1,buf2); @@ -1245,3 +1263,3 @@ static int inList(const char **list, const char *s) while (*list) { - if (strcasecmp(*list,s) == 0) return 1; + if (stricmp(*list,s) == 0) return 1; list++; @@ -1253,2 +1271,3 @@ static void writeProp(OFile *fp, VObject *o) { + int isQuoted=0; if (NAME_OF(o)) { @@ -1272,4 +1291,6 @@ static void writeProp(OFile *fp, VObject *o) s = NAME_OF(eachProp); - if (strcasecmp(VCGroupingProp,s) && !inList(fields_,s)) + if (stricmp(VCGroupingProp,s) && !inList(fields_,s)) writeAttrValue(fp,eachProp); + if (stricmp(VCQPProp,s)==0 || stricmp(VCQuotedPrintableProp,s)==0) + isQuoted=1; } @@ -1281,5 +1302,5 @@ static void writeProp(OFile *fp, VObject *o) while (*fields) { - VObject *tl = isAPropertyOf(o,*fields); + VObject *t = isAPropertyOf(o,*fields); i++; - if (tl) n = i; + if (t) n = i; fields++; @@ -1288,3 +1309,3 @@ static void writeProp(OFile *fp, VObject *o) for (i=0;i<n;i++) { - writeValue(fp,isAPropertyOf(o,*fields),0); + writeValue(fp,isAPropertyOf(o,*fields),0,isQuoted); fields++; @@ -1300,3 +1321,3 @@ static void writeProp(OFile *fp, VObject *o) appendcOFile(fp,':'); - writeValue(fp,o,size); + writeValue(fp,o,size,isQuoted); } @@ -1337,3 +1358,3 @@ void writeVObject(FILE *fp, VObject *o) -void writeVObjectToFile(char *fname, VObject *o) +DLLEXPORT(void) writeVObjectToFile(char *fname, VObject *o) { @@ -1346,3 +1367,3 @@ void writeVObjectToFile(char *fname, VObject *o) -void writeVObjectsToFile(char *fname, VObject *list) +DLLEXPORT(void) writeVObjectsToFile(char *fname, VObject *list) { @@ -1358,3 +1379,3 @@ void writeVObjectsToFile(char *fname, VObject *list) -char* writeMemVObject(char *s, int *len, VObject *o) +DLLEXPORT(char*) writeMemVObject(char *s, int *len, VObject *o) { @@ -1368,3 +1389,3 @@ char* writeMemVObject(char *s, int *len, VObject *o) -char* writeMemVObjects(char *s, int *len, VObject *list) +DLLEXPORT(char*) writeMemVObjects(char *s, int *len, VObject *list) { @@ -1384,3 +1405,3 @@ char* writeMemVObjects(char *s, int *len, VObject *list) ----------------------------------------------------------------------*/ -wchar_t* fakeUnicode(const char *ps, int *bytes) +DLLEXPORT(wchar_t*) fakeUnicode(const char *ps, int *bytes) { @@ -1407,3 +1428,3 @@ wchar_t* fakeUnicode(const char *ps, int *bytes) -int uStrLen(const wchar_t *u) +DLLEXPORT(int) uStrLen(const wchar_t *u) { @@ -1414,3 +1435,3 @@ int uStrLen(const wchar_t *u) -char* fakeCString(const wchar_t *u) +DLLEXPORT(char*) fakeCString(const wchar_t *u) { @@ -1418,3 +1439,3 @@ char* fakeCString(const wchar_t *u) int len = uStrLen(u) + 1; - t = s = (char*)malloc(len+1); + t = s = (char*)malloc(len); while (*u) { |