/* header field specifics */
-- (NSData *)generateDataForHeaderField:(NSString *)_headerField
- value:(id)_value
-{
- NSData *data = nil;
+- (id)_escapeHeaderFieldValue:(NSData *)_data {
+ const char *bytes = NULL;
+ unsigned int length = 0;
+ unsigned int desLen = 0;
+ char *des = NULL;
+ unsigned int cnt;
+ BOOL doEnc;
+ NSString *str;
- data = [super generateDataForHeaderField:_headerField
- value:_value];
- {
- const char *bytes = NULL;
- unsigned int length = 0;
- unsigned int desLen = 0;
- char *des = NULL;
- unsigned int cnt = 0;
- BOOL doEnc = NO;
- NSString *str;
+ // TODO: this s***s big time!
#if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
- str = [[NSString alloc] initWithData:data
- encoding:NSISOLatin1StringEncoding];
+ str = [[NSString alloc] initWithData:_data
+ encoding:NSISOLatin1StringEncoding];
#else
- str = [[NSString alloc] initWithData:data
- encoding:NSISOLatin9StringEncoding];
+ str = [[NSString alloc] initWithData:_data
+ encoding:NSISOLatin9StringEncoding];
#endif
- str = [str autorelease];
-
- bytes = [str cString];
- length = [str cStringLength];
-
- while (cnt < length) {
- if ((unsigned char)bytes[cnt] > 127) {
- doEnc = YES;
- break;
- }
- cnt++;
+ str = [str autorelease];
+
+ bytes = [str cString];
+ length = [str cStringLength];
+
+ /* check whether we need to encode */
+
+ for (cnt = 0, doEnc = NO; cnt < length; cnt++) {
+ if ((unsigned char)bytes[cnt] > 127) {
+ doEnc = YES;
+ break;
}
- if (doEnc) {
- char iso[] = "=?iso-8859-15?q?";
- unsigned isoLen = 16;
- char isoEnd[] = "?=";
- unsigned isoEndLen = 2;
+ }
+
+ if (!doEnc)
+ return _data;
+
+ /* encode quoted printable */
+ {
+ char iso[] = "=?iso-8859-15?q?";
+ unsigned isoLen = 16;
+ char isoEnd[] = "?=";
+ unsigned isoEndLen = 2;
+
+ desLen = length * 3 + 20;
- desLen = length * 3 + 20;
+ des = calloc(desLen + 2, sizeof(char));
- des = calloc(desLen + 2, sizeof(char));
+ // memcpy(des, bytes, cnt);
+ memcpy(des, iso, isoLen);
+ desLen = NGEncodeQuotedPrintableMime(bytes, length,
+ des + isoLen, desLen - isoLen);
+ if ((int)desLen != -1) {
+ memcpy(des + isoLen + desLen, isoEnd, isoEndLen);
- // memcpy(des, bytes, cnt);
- memcpy(des, iso, isoLen);
- desLen = NGEncodeQuotedPrintableMime(bytes, length,
- des + isoLen, desLen - isoLen);
- if ((int)desLen != -1) {
- memcpy(des + isoLen + desLen, isoEnd, isoEndLen);
-
- data = [NSData dataWithBytesNoCopy:des
- length:(isoLen + desLen + isoEndLen)];
- }
- else {
- NSLog(@"WARNING: An error occour during quoted-printable decoding");
- if (des != NULL) free(des);
- }
+ return [NSData dataWithBytesNoCopy:des
+ length:(isoLen + desLen + isoEndLen)];
+ }
+ else {
+ [self logWithFormat:
+ @"WARNING: An error occour during quoted-printable decoding"];
+ if (des != NULL) free(des);
+ return _data;
}
}
- return data;
+}
+
+- (NSData *)generateDataForHeaderField:(NSString *)_hf value:(id)_value {
+ NSData *data;
+
+ // TODO: properly deal with header field values, add proper quoting
+ // prior passing the value up?
+
+ data = [super generateDataForHeaderField:_hf value:_value];
+ return [self _escapeHeaderFieldValue:data];
}