/*
- Copyright (C) 2000-2005 SKYRIX Software AG
+ Copyright (C) 2000-2006 SKYRIX Software AG
+ Copyright (C) 2006 Helge Hess
This file is part of SOPE.
/*
Quoted Printable encoder/decoder
- As specified in RFC 822.
+ As specified in RFC 822 / 2045 / 2047. Note that 2045 and 2047 specify
+ different variants (Q vs content-transfer-encoding)
TODO: explain what it does. It doesn't seem to decode a full line like
"=?iso-8859-1?q?Yannick=20DAmboise?="
but only turns "=20D" style encodings to their charcode.
+
Note: apparently sope-mime contains a category on NSData which provides a
method to decode the full value:
-decodeQuotedPrintableValueOfMIMEHeaderField:
(NGMimeMessageParser)
*/
-@interface NSString(QuotedPrintableCoding)
-
-- (NSString *)stringByDecodingQuotedPrintable;
-- (NSString *)stringByEncodingQuotedPrintable;
-
-@end
-
@interface NSData(QuotedPrintableCoding)
/*
- Decode a quoted printable encoded data. Returns nil if decoding failed.
+ Decode a quoted printable encoded data. Returns nil if decoding failed. The
+ first method does the RFC 2047 variant, the second RFC 2045 (w/o _ replacing)
*/
- (NSData *)dataByDecodingQuotedPrintable;
+- (NSData *)dataByDecodingQuotedPrintableTransferEncoding;
/*
Decode data in quoted printable encoding. Returns nil if encoding failed.
@end
+
+/* Note: you should avoid NSString methods for QP, its defined on byte level */
+@interface NSString(QuotedPrintableCoding)
+
+- (NSString *)stringByDecodingQuotedPrintable;
+- (NSString *)stringByEncodingQuotedPrintable;
+
+@end
+
+
NGExtensions_EXPORT int
NGEncodeQuotedPrintable(const char *_src, unsigned _srcLen,
char *_dest, unsigned _destLen);
NGExtensions_EXPORT int
NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
char *_dest, unsigned _destLen);
+NGExtensions_EXPORT int
+NGDecodeQuotedPrintableX(const char *_src, unsigned _srcLen,
+ char *_dest, unsigned _destLen,
+ BOOL _replaceUnderline);
#endif /* __NGExtensions_NGQuotedPrintableCoding_H__ */
/*
- Copyright (C) 2000-2005 SKYRIX Software AG
+ Copyright (C) 2000-2006 SKYRIX Software AG
+ Copyright (C) 2006 Helge Hess
This file is part of SOPE.
#include "common.h"
#include "NGMemoryAllocation.h"
+
@implementation NSString(QuotedPrintableCoding)
- (NSString *)stringByDecodingQuotedPrintable {
buf = malloc(len + 10);
[self getCString:buf];
data = [NSData dataWithBytes:buf length:len];
- if (buf) free(buf);
+ if (buf != NULL) free(buf);
}
else
data = [NSData data];
data = [data dataByDecodingQuotedPrintable];
+
+ // TODO: should we default to some specific charset instead? (either
+ // Latin1 or UTF-8
return [NSString stringWithCString:[data bytes] length:[data length]];
}
+
- (NSString *)stringByEncodingQuotedPrintable {
NSData *data;
unsigned len;
data = [NSData data];
data = [data dataByEncodingQuotedPrintable];
+
return [NSString stringWithCString:[data bytes] length:[data length]];
}
-@end
+@end /* NSString(QuotedPrintableCoding) */
+
@implementation NSData(QuotedPrintableCoding)
- (NSData *)dataByDecodingQuotedPrintable {
- char *dest = NULL;
- size_t destSize = 0;
- size_t resSize = 0;
+ char *dest;
+ size_t destSize;
+ size_t resSize;
+
+ destSize = [self length];
+ dest = malloc(destSize * sizeof(char) + 2);
+
+ resSize =
+ NGDecodeQuotedPrintableX([self bytes], [self length], dest, destSize, YES);
+
+ return ((int)resSize != -1)
+ ? [NSData dataWithBytesNoCopy:dest length:resSize]
+ : nil;
+}
+- (NSData *)dataByDecodingQuotedPrintableTransferEncoding {
+ char *dest;
+ size_t destSize;
+ size_t resSize;
destSize = [self length];
- dest = NGMallocAtomic(destSize * sizeof(char));
+ dest = malloc(destSize * sizeof(char) + 2);
- resSize = NGDecodeQuotedPrintable([self bytes],[self length],dest,destSize);
+ resSize =
+ NGDecodeQuotedPrintableX([self bytes], [self length], dest, destSize, NO);
return ((int)resSize != -1)
? [NSData dataWithBytesNoCopy:dest length:resSize]
: nil;
}
-@end
+@end /* NSData(QuotedPrintableCoding) */
+
// implementation
return -1;
}
-int NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
- char *_dest, unsigned _destLen)
+int NGDecodeQuotedPrintableX(const char *_src, unsigned _srcLen,
+ char *_dest, unsigned _destLen,
+ BOOL _replaceUnderline)
{
/*
Eg: "Hello=20World" => "Hello World"
for (cnt = 0; ((cnt < _srcLen) && (destCnt < _destLen)); cnt++) {
if (_src[cnt] != '=') {
- _dest[destCnt] = _src[cnt] == '_' ? 0x20 : _src[cnt];
+ _dest[destCnt] =
+ (_replaceUnderline && _src[cnt] == '_') ? 0x20 : _src[cnt];
destCnt++;
}
else {
return -1;
return destCnt;
}
+int NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
+ char *_dest, unsigned _destLen)
+{
+ // should we deprecated that?
+ return NGDecodeQuotedPrintableX(_src, _srcLen, _dest, _destLen, YES);
+}
/*
From RFC 2045 Multipurpose Internet Mail Extensions