-rw-r--r-- | library/backend/vobject.cpp | 104 |
1 files changed, 100 insertions, 4 deletions
diff --git a/library/backend/vobject.cpp b/library/backend/vobject.cpp index 9c2ba3b..e6f6b78 100644 --- a/library/backend/vobject.cpp +++ b/library/backend/vobject.cpp | |||
@@ -984,102 +984,181 @@ static int writeBase64(OFile *fp, unsigned char *s, long len) | |||
984 | } | 984 | } |
985 | // fill in 'quad' with the appropriate four characters | 985 | // fill in 'quad' with the appropriate four characters |
986 | for (i = 3; i >= 0; i--) { | 986 | for (i = 3; i >= 0; i--) { |
987 | b = (unsigned char)(trip & 0x3F); | 987 | b = (unsigned char)(trip & 0x3F); |
988 | trip = trip >> 6; | 988 | trip = trip >> 6; |
989 | if ((3 - i) < (cur - len)) | 989 | if ((3 - i) < (cur - len)) |
990 | quad[i] = '='; // pad char | 990 | quad[i] = '='; // pad char |
991 | else if (b < 26) quad[i] = (char)b + 'A'; | 991 | else if (b < 26) quad[i] = (char)b + 'A'; |
992 | else if (b < 52) quad[i] = (char)(b - 26) + 'a'; | 992 | else if (b < 52) quad[i] = (char)(b - 26) + 'a'; |
993 | else if (b < 62) quad[i] = (char)(b - 52) + '0'; | 993 | else if (b < 62) quad[i] = (char)(b - 52) + '0'; |
994 | else if (b == 62) quad[i] = '+'; | 994 | else if (b == 62) quad[i] = '+'; |
995 | else quad[i] = '/'; | 995 | else quad[i] = '/'; |
996 | } | 996 | } |
997 | // now output 'quad' with appropriate whitespace and line ending | 997 | // now output 'quad' with appropriate whitespace and line ending |
998 | appendsOFile(fp, (numQuads == 0 ? " " : "")); | 998 | appendsOFile(fp, (numQuads == 0 ? " " : "")); |
999 | appendsOFile(fp, quad); | 999 | appendsOFile(fp, quad); |
1000 | appendsOFile(fp, ((cur >= len)?"\n" :(numQuads==MAXQUADS-1?"\n" : ""))); | 1000 | appendsOFile(fp, ((cur >= len)?"\n" :(numQuads==MAXQUADS-1?"\n" : ""))); |
1001 | numQuads = (numQuads + 1) % MAXQUADS; | 1001 | numQuads = (numQuads + 1) % MAXQUADS; |
1002 | } | 1002 | } |
1003 | appendcOFile(fp,'\n'); | 1003 | appendcOFile(fp,'\n'); |
1004 | 1004 | ||
1005 | return 1; | 1005 | return 1; |
1006 | } | 1006 | } |
1007 | 1007 | ||
1008 | static const char *replaceChar(unsigned char c) | ||
1009 | { | ||
1010 | if (c == '\n') { | ||
1011 | return "=0A=\n"; | ||
1012 | } else if ( | ||
1013 | (c >= 'A' && c <= 'Z') | ||
1014 | || | ||
1015 | (c >= 'a' && c <= 'z') | ||
1016 | || | ||
1017 | (c >= '0' && c <= '9') | ||
1018 | || | ||
1019 | (c >= '\'' && c <= ')') | ||
1020 | || | ||
1021 | (c >= '+' && c <= '-') | ||
1022 | || | ||
1023 | (c == '/') | ||
1024 | || | ||
1025 | (c == '?') | ||
1026 | || | ||
1027 | (c == ' ')) | ||
1028 | { | ||
1029 | return 0; | ||
1030 | } | ||
1031 | |||
1032 | static char trans[4]; | ||
1033 | trans[0] = '='; | ||
1034 | trans[3] = '\0'; | ||
1035 | int rem = c % 16; | ||
1036 | int div = c / 16; | ||
1037 | |||
1038 | if (div < 10) | ||
1039 | trans[1] = '0' + div; | ||
1040 | else | ||
1041 | trans[1] = 'A' + (div - 10); | ||
1042 | |||
1043 | if (rem < 10) | ||
1044 | trans[2] = '0' + rem; | ||
1045 | else | ||
1046 | trans[2] = 'A' + (rem - 10); | ||
1047 | |||
1048 | return trans; | ||
1049 | } | ||
1050 | |||
1008 | static void writeQPString(OFile *fp, const char *s) | 1051 | static void writeQPString(OFile *fp, const char *s) |
1009 | { | 1052 | { |
1053 | /* | ||
1054 | only A-Z, 0-9 and | ||
1055 | "'" (ASCII code 39) | ||
1056 | "(" (ASCII code 40) | ||
1057 | ")" (ASCII code 41) | ||
1058 | "+" (ASCII code 43) | ||
1059 | "," (ASCII code 44) | ||
1060 | "-" (ASCII code 45) | ||
1061 | "/" (ASCII code 47) | ||
1062 | "?" (ASCII code 63) | ||
1063 | |||
1064 | should remain un-encoded. | ||
1065 | '=' needs to be encoded as it is the escape character. | ||
1066 | ';' needs to be as it is a field separator. | ||
1067 | |||
1068 | */ | ||
1010 | const char *p = s; | 1069 | const char *p = s; |
1011 | while (*p) { | 1070 | while (*p) { |
1012 | if (*p == '\n') { | 1071 | const char *rep = replaceChar(*p); |
1013 | if (p[1]) appendsOFile(fp,"=0A="); | 1072 | if (rep) |
1014 | } | 1073 | appendsOFile(fp, rep); |
1074 | else | ||
1015 | appendcOFile(fp,*p); | 1075 | appendcOFile(fp,*p); |
1016 | p++; | 1076 | p++; |
1017 | } | 1077 | } |
1018 | } | 1078 | } |
1019 | 1079 | ||
1020 | 1080 | static bool includesUnprintable(VObject *o) | |
1081 | { | ||
1082 | if (o) { | ||
1083 | if (VALUE_TYPE(o) == VCVT_STRINGZ) { | ||
1084 | const char *p = STRINGZ_VALUE_OF(o); | ||
1085 | if (p) { | ||
1086 | while (*p) { | ||
1087 | if (replaceChar(*p)) | ||
1088 | return TRUE; | ||
1089 | p++; | ||
1090 | } | ||
1091 | } | ||
1092 | } | ||
1093 | } | ||
1094 | return FALSE; | ||
1095 | } | ||
1021 | 1096 | ||
1022 | static void writeVObject_(OFile *fp, VObject *o); | 1097 | static void writeVObject_(OFile *fp, VObject *o); |
1023 | 1098 | ||
1024 | static void writeValue(OFile *fp, VObject *o, unsigned long size) | 1099 | static void writeValue(OFile *fp, VObject *o, unsigned long size) |
1025 | { | 1100 | { |
1026 | if (o == 0) return; | 1101 | if (o == 0) return; |
1027 | switch (VALUE_TYPE(o)) { | 1102 | switch (VALUE_TYPE(o)) { |
1028 | case VCVT_STRINGZ: { | 1103 | case VCVT_STRINGZ: { |
1029 | writeQPString(fp, STRINGZ_VALUE_OF(o)); | 1104 | writeQPString(fp, STRINGZ_VALUE_OF(o)); |
1030 | break; | 1105 | break; |
1031 | } | 1106 | } |
1032 | case VCVT_UINT: { | 1107 | case VCVT_UINT: { |
1033 | char buf[16]; | 1108 | char buf[16]; |
1034 | sprintf(buf,"%u", INTEGER_VALUE_OF(o)); | 1109 | sprintf(buf,"%u", INTEGER_VALUE_OF(o)); |
1035 | appendsOFile(fp,buf); | 1110 | appendsOFile(fp,buf); |
1036 | break; | 1111 | break; |
1037 | } | 1112 | } |
1038 | case VCVT_ULONG: { | 1113 | case VCVT_ULONG: { |
1039 | char buf[16]; | 1114 | char buf[16]; |
1040 | sprintf(buf,"%lu", LONG_VALUE_OF(o)); | 1115 | sprintf(buf,"%lu", LONG_VALUE_OF(o)); |
1041 | appendsOFile(fp,buf); | 1116 | appendsOFile(fp,buf); |
1042 | break; | 1117 | break; |
1043 | } | 1118 | } |
1044 | case VCVT_RAW: { | 1119 | case VCVT_RAW: { |
1045 | appendcOFile(fp,'\n'); | 1120 | appendcOFile(fp,'\n'); |
1046 | writeBase64(fp,(unsigned char*)(ANY_VALUE_OF(o)),size); | 1121 | writeBase64(fp,(unsigned char*)(ANY_VALUE_OF(o)),size); |
1047 | break; | 1122 | break; |
1048 | } | 1123 | } |
1049 | case VCVT_VOBJECT: | 1124 | case VCVT_VOBJECT: |
1050 | appendcOFile(fp,'\n'); | 1125 | appendcOFile(fp,'\n'); |
1051 | writeVObject_(fp,VOBJECT_VALUE_OF(o)); | 1126 | writeVObject_(fp,VOBJECT_VALUE_OF(o)); |
1052 | break; | 1127 | break; |
1053 | } | 1128 | } |
1054 | } | 1129 | } |
1055 | 1130 | ||
1056 | static void writeAttrValue(OFile *fp, VObject *o) | 1131 | static void writeAttrValue(OFile *fp, VObject *o) |
1057 | { | 1132 | { |
1058 | if (NAME_OF(o)) { | 1133 | if (NAME_OF(o)) { |
1059 | struct PreDefProp *pi; | 1134 | struct PreDefProp *pi; |
1060 | pi = lookupPropInfo(NAME_OF(o)); | 1135 | pi = lookupPropInfo(NAME_OF(o)); |
1061 | if (pi && ((pi->flags & PD_INTERNAL) != 0)) return; | 1136 | if (pi && ((pi->flags & PD_INTERNAL) != 0)) return; |
1137 | if ( includesUnprintable(o) ) { | ||
1138 | appendsOFile(fp, ";" VCEncodingProp "=" VCQuotedPrintableProp); | ||
1139 | appendsOFile(fp, ";" VCCharSetProp "=" "UTF-8"); | ||
1140 | } | ||
1062 | appendcOFile(fp,';'); | 1141 | appendcOFile(fp,';'); |
1063 | appendsOFile(fp,NAME_OF(o)); | 1142 | appendsOFile(fp,NAME_OF(o)); |
1064 | } | 1143 | } |
1065 | else | 1144 | else |
1066 | appendcOFile(fp,';'); | 1145 | appendcOFile(fp,';'); |
1067 | if (VALUE_TYPE(o)) { | 1146 | if (VALUE_TYPE(o)) { |
1068 | appendcOFile(fp,'='); | 1147 | appendcOFile(fp,'='); |
1069 | writeValue(fp,o,0); | 1148 | writeValue(fp,o,0); |
1070 | } | 1149 | } |
1071 | } | 1150 | } |
1072 | 1151 | ||
1073 | static void writeGroup(OFile *fp, VObject *o) | 1152 | static void writeGroup(OFile *fp, VObject *o) |
1074 | { | 1153 | { |
1075 | char buf1[256]; | 1154 | char buf1[256]; |
1076 | char buf2[256]; | 1155 | char buf2[256]; |
1077 | strcpy(buf1,NAME_OF(o)); | 1156 | strcpy(buf1,NAME_OF(o)); |
1078 | while ((o=isAPropertyOf(o,VCGroupingProp)) != 0) { | 1157 | while ((o=isAPropertyOf(o,VCGroupingProp)) != 0) { |
1079 | strcpy(buf2,STRINGZ_VALUE_OF(o)); | 1158 | strcpy(buf2,STRINGZ_VALUE_OF(o)); |
1080 | strcat(buf2,"."); | 1159 | strcat(buf2,"."); |
1081 | strcat(buf2,buf1); | 1160 | strcat(buf2,buf1); |
1082 | strcpy(buf1,buf2); | 1161 | strcpy(buf1,buf2); |
1083 | } | 1162 | } |
1084 | appendsOFile(fp,buf1); | 1163 | appendsOFile(fp,buf1); |
1085 | } | 1164 | } |
@@ -1101,65 +1180,82 @@ static void writeProp(OFile *fp, VObject *o) | |||
1101 | VObjectIterator t; | 1180 | VObjectIterator t; |
1102 | const char **fields_ = 0; | 1181 | const char **fields_ = 0; |
1103 | pi = lookupPropInfo(NAME_OF(o)); | 1182 | pi = lookupPropInfo(NAME_OF(o)); |
1104 | if (pi && ((pi->flags & PD_BEGIN) != 0)) { | 1183 | if (pi && ((pi->flags & PD_BEGIN) != 0)) { |
1105 | writeVObject_(fp,o); | 1184 | writeVObject_(fp,o); |
1106 | return; | 1185 | return; |
1107 | } | 1186 | } |
1108 | if (isAPropertyOf(o,VCGroupingProp)) | 1187 | if (isAPropertyOf(o,VCGroupingProp)) |
1109 | writeGroup(fp,o); | 1188 | writeGroup(fp,o); |
1110 | else | 1189 | else |
1111 | appendsOFile(fp,NAME_OF(o)); | 1190 | appendsOFile(fp,NAME_OF(o)); |
1112 | if (pi) fields_ = pi->fields; | 1191 | if (pi) fields_ = pi->fields; |
1113 | initPropIterator(&t,o); | 1192 | initPropIterator(&t,o); |
1114 | while (moreIteration(&t)) { | 1193 | while (moreIteration(&t)) { |
1115 | const char *s; | 1194 | const char *s; |
1116 | VObject *eachProp = nextVObject(&t); | 1195 | VObject *eachProp = nextVObject(&t); |
1117 | s = NAME_OF(eachProp); | 1196 | s = NAME_OF(eachProp); |
1118 | if (qstricmp(VCGroupingProp,s) && !inList(fields_,s)) | 1197 | if (qstricmp(VCGroupingProp,s) && !inList(fields_,s)) |
1119 | writeAttrValue(fp,eachProp); | 1198 | writeAttrValue(fp,eachProp); |
1120 | } | 1199 | } |
1121 | if (fields_) { | 1200 | if (fields_) { |
1122 | int i = 0, n = 0; | 1201 | int i = 0, n = 0; |
1123 | const char** fields = fields_; | 1202 | const char** fields = fields_; |
1124 | /* output prop as fields */ | 1203 | /* output prop as fields */ |
1204 | bool printable = TRUE; | ||
1205 | while (*fields && printable) { | ||
1206 | VObject *t = isAPropertyOf(o,*fields); | ||
1207 | if (includesUnprintable(t)) | ||
1208 | printable = FALSE; | ||
1209 | fields++; | ||
1210 | } | ||
1211 | fields = fields_; | ||
1212 | if (!printable) { | ||
1213 | appendsOFile(fp, ";" VCEncodingProp "=" VCQuotedPrintableProp); | ||
1214 | appendsOFile(fp, ";" VCCharSetProp "=" "UTF-8"); | ||
1215 | } | ||
1125 | appendcOFile(fp,':'); | 1216 | appendcOFile(fp,':'); |
1126 | while (*fields) { | 1217 | while (*fields) { |
1127 | VObject *t = isAPropertyOf(o,*fields); | 1218 | VObject *t = isAPropertyOf(o,*fields); |
1128 | i++; | 1219 | i++; |
1129 | if (t) n = i; | 1220 | if (t) n = i; |
1130 | fields++; | 1221 | fields++; |
1131 | } | 1222 | } |
1132 | fields = fields_; | 1223 | fields = fields_; |
1133 | for (i=0;i<n;i++) { | 1224 | for (i=0;i<n;i++) { |
1134 | writeValue(fp,isAPropertyOf(o,*fields),0); | 1225 | writeValue(fp,isAPropertyOf(o,*fields),0); |
1135 | fields++; | 1226 | fields++; |
1136 | if (i<(n-1)) appendcOFile(fp,';'); | 1227 | if (i<(n-1)) appendcOFile(fp,';'); |
1137 | } | 1228 | } |
1138 | } | 1229 | } |
1139 | } | 1230 | } |
1140 | 1231 | ||
1232 | |||
1141 | if (VALUE_TYPE(o)) { | 1233 | if (VALUE_TYPE(o)) { |
1234 | if ( includesUnprintable(o) ) { | ||
1235 | appendsOFile(fp, ";" VCEncodingProp "=" VCQuotedPrintableProp); | ||
1236 | appendsOFile(fp, ";" VCCharSetProp "=" "UTF-8"); | ||
1237 | } | ||
1142 | unsigned long size = 0; | 1238 | unsigned long size = 0; |
1143 | VObject *p = isAPropertyOf(o,VCDataSizeProp); | 1239 | VObject *p = isAPropertyOf(o,VCDataSizeProp); |
1144 | if (p) size = LONG_VALUE_OF(p); | 1240 | if (p) size = LONG_VALUE_OF(p); |
1145 | appendcOFile(fp,':'); | 1241 | appendcOFile(fp,':'); |
1146 | writeValue(fp,o,size); | 1242 | writeValue(fp,o,size); |
1147 | } | 1243 | } |
1148 | 1244 | ||
1149 | appendcOFile(fp,'\n'); | 1245 | appendcOFile(fp,'\n'); |
1150 | } | 1246 | } |
1151 | 1247 | ||
1152 | static void writeVObject_(OFile *fp, VObject *o) | 1248 | static void writeVObject_(OFile *fp, VObject *o) |
1153 | { | 1249 | { |
1154 | if (NAME_OF(o)) { | 1250 | if (NAME_OF(o)) { |
1155 | struct PreDefProp *pi; | 1251 | struct PreDefProp *pi; |
1156 | pi = lookupPropInfo(NAME_OF(o)); | 1252 | pi = lookupPropInfo(NAME_OF(o)); |
1157 | 1253 | ||
1158 | if (pi && ((pi->flags & PD_BEGIN) != 0)) { | 1254 | if (pi && ((pi->flags & PD_BEGIN) != 0)) { |
1159 | VObjectIterator t; | 1255 | VObjectIterator t; |
1160 | const char *begin = NAME_OF(o); | 1256 | const char *begin = NAME_OF(o); |
1161 | appendsOFile(fp,"BEGIN:"); | 1257 | appendsOFile(fp,"BEGIN:"); |
1162 | appendsOFile(fp,begin); | 1258 | appendsOFile(fp,begin); |
1163 | appendcOFile(fp,'\n'); | 1259 | appendcOFile(fp,'\n'); |
1164 | initPropIterator(&t,o); | 1260 | initPropIterator(&t,o); |
1165 | while (moreIteration(&t)) { | 1261 | while (moreIteration(&t)) { |