2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #ifndef __NGMime_NGMimeUtilities_H__
23 #define __NGMime_NGMimeUtilities_H__
25 #import <Foundation/Foundation.h>
26 #import <NGExtensions/NGExtensions.h>
27 #include <NGMime/NGMimeDecls.h>
29 // ******************** RFC 822 ********************
31 static inline BOOL isRfc822_SpecialByte(unsigned char _byte) {
33 case '(': case ')': case '<': case '>': case '@':
34 case ',': case ';': case ':': case '"': case '\\':
35 case '.': case '[': case ']':
43 NSDictionary *parseParameters(id self, NSString *_str, unichar *cstr);
45 static inline BOOL isRfc822_CR(unsigned char _byte) {
48 static inline BOOL isRfc822_LF(unsigned char _byte) {
51 static inline BOOL isRfc822_HTAB(unsigned char _byte) {
54 static inline BOOL isRfc822_SPACE(unsigned char _byte) {
57 static inline BOOL isRfc822_QUOTE(unsigned char _byte) {
63 static inline BOOL isRfc822_CHAR(unsigned char _byte) {
67 static inline BOOL isRfc822_CTL(unsigned char _byte) {
68 return (_byte < 32) || (_byte == 127);
71 static inline BOOL isRfc822_ALPHA(unsigned char _byte) {
72 return (((_byte >= 65) && (_byte <= 90)) ||
73 ((_byte >= 97) && (_byte <= 122)));
76 static inline BOOL isRfc822_DIGIT(unsigned char _byte) {
77 return (_byte >= 48) && (_byte <= 57);
80 static inline BOOL isRfc822_LWSP(unsigned char _byte) {
81 return (isRfc822_SPACE(_byte) || isRfc822_HTAB(_byte));
85 static inline BOOL isRfc822_FieldNameChar(unsigned char _byte) {
86 return (isRfc822_CHAR(_byte) &&
87 !(isRfc822_CTL(_byte) || isRfc822_SPACE(_byte) || (_byte == ':')));
90 static inline BOOL isRfc822_AtomChar(unsigned char _byte) {
91 return (isRfc822_CHAR(_byte) &&
92 !(isRfc822_SpecialByte(_byte) || isRfc822_SPACE(_byte) ||
93 isRfc822_CTL(_byte)));
96 // ******************** MIME ***********************
98 static inline BOOL isMime_SpecialByte(unsigned char _byte) {
100 case '(': case ')': case '<': case '>': case '@':
101 case ',': case ';': case ':': case '"': case '\\':
102 case '/': case '=': case '[': case ']': case '?':
109 static inline BOOL isMime_TokenChar(unsigned char _byte) {
110 return (isRfc822_CHAR(_byte) &&
111 !(isRfc822_CTL(_byte) || isRfc822_SPACE(_byte) ||
112 isMime_SpecialByte(_byte)));
115 static inline BOOL isMime_SafeChar(unsigned char _byte) {
116 return ((_byte >= 33 && _byte <= 60) || (_byte >= 62 && _byte <= 126));
119 static inline BOOL isMime_ValidTypeXTokenChar(unsigned char _byte) {
120 return !isRfc822_SPACE(_byte);
123 static inline BOOL isMime_ValidTypeAttributeChar(unsigned char _byte) {
124 return isMime_TokenChar(_byte);
127 static inline NSData *_quotedPrintableEncoding(NSData *_data) {
128 const char *bytes = [_data bytes];
129 unsigned int length = [_data length];
130 NSData *result = nil;
132 unsigned int desLen = 0;
133 unsigned cnt = length;
134 const char *test = bytes;
138 if ((unsigned char)*test > 127) {
145 if (!doEnc) return _data;
147 des = NGMallocAtomic(sizeof(char) * desLen + 2);
149 desLen = NGEncodeQuotedPrintable(bytes, length, des, desLen);
150 if ((int)desLen != -1) {
151 result = [NSData dataWithBytesNoCopy:des length:desLen];
154 NSLog(@"WARNING(%s): An error occour during quoted-printable decoding",
155 __PRETTY_FUNCTION__);
156 if (des) NGFree(des);
162 static inline NSData *_rfc2047Decoding(char _enc, const char *_bytes,
163 unsigned int _length)
167 if ((_enc == 'b') || (_enc == 'B')) { // use BASE64 decoding
170 tmp = [[NSData alloc] initWithBytes:_bytes length:_length];
171 data = [tmp dataByDecodingBase64];
174 else if ((_enc == 'q') || (_enc == 'Q')) { // use quoted-printable decoding
180 dest = NGMallocAtomic(destSize * sizeof(char));
181 resSize = NGDecodeQuotedPrintable(_bytes, _length, dest, destSize);
182 if ((int)resSize != -1) {
183 data = [NSData dataWithBytesNoCopy:dest length:resSize];
186 NSLog(@"WARNING(%s): An error occour during quoted-printable decoding",
187 __PRETTY_FUNCTION__);
188 NGFree(dest); dest = NULL;
189 data = [NSData dataWithBytes:_bytes length:_length];
193 NSLog(@"WARNING(%s): unknown encoding type %c", __PRETTY_FUNCTION__, _enc);
194 data = [NSData dataWithBytes:_bytes length:_length];
199 #endif /* __NGMime_NGMimeUtilities_H__ */