2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #ifndef __NGMime_NGMimeUtilities_H__
24 #define __NGMime_NGMimeUtilities_H__
26 #import <Foundation/Foundation.h>
27 #import <NGExtensions/NGExtensions.h>
28 #include <NGMime/NGMimeDecls.h>
30 // ******************** RFC 822 ********************
32 static inline BOOL isRfc822_SpecialByte(unsigned char _byte) {
34 case '(': case ')': case '<': case '>': case '@':
35 case ',': case ';': case ':': case '"': case '\\':
36 case '.': case '[': case ']':
44 NSDictionary *parseParameters(id self, NSString *_str, unichar *cstr);
46 static inline BOOL isRfc822_CR(unsigned char _byte) {
49 static inline BOOL isRfc822_LF(unsigned char _byte) {
52 static inline BOOL isRfc822_HTAB(unsigned char _byte) {
55 static inline BOOL isRfc822_SPACE(unsigned char _byte) {
58 static inline BOOL isRfc822_QUOTE(unsigned char _byte) {
64 static inline BOOL isRfc822_CHAR(unsigned char _byte) {
68 static inline BOOL isRfc822_CTL(unsigned char _byte) {
69 return (_byte < 32) || (_byte == 127);
72 static inline BOOL isRfc822_ALPHA(unsigned char _byte) {
73 return (((_byte >= 65) && (_byte <= 90)) ||
74 ((_byte >= 97) && (_byte <= 122)));
77 static inline BOOL isRfc822_DIGIT(unsigned char _byte) {
78 return (_byte >= 48) && (_byte <= 57);
81 static inline BOOL isRfc822_LWSP(unsigned char _byte) {
82 return (isRfc822_SPACE(_byte) || isRfc822_HTAB(_byte));
86 static inline BOOL isRfc822_FieldNameChar(unsigned char _byte) {
87 return (isRfc822_CHAR(_byte) &&
88 !(isRfc822_CTL(_byte) || isRfc822_SPACE(_byte) || (_byte == ':')));
91 static inline BOOL isRfc822_AtomChar(unsigned char _byte) {
92 return (isRfc822_CHAR(_byte) &&
93 !(isRfc822_SpecialByte(_byte) || isRfc822_SPACE(_byte) ||
94 isRfc822_CTL(_byte)));
97 // ******************** MIME ***********************
99 static inline BOOL isMime_SpecialByte(unsigned char _byte) {
101 case '(': case ')': case '<': case '>': case '@':
102 case ',': case ';': case ':': case '"': case '\\':
103 case '/': case '=': case '[': case ']': case '?':
110 static inline BOOL isMime_TokenChar(unsigned char _byte) {
111 return (isRfc822_CHAR(_byte) &&
112 !(isRfc822_CTL(_byte) || isRfc822_SPACE(_byte) ||
113 isMime_SpecialByte(_byte)));
116 static inline BOOL isMime_SafeChar(unsigned char _byte) {
117 return ((_byte >= 33 && _byte <= 60) || (_byte >= 62 && _byte <= 126));
120 static inline BOOL isMime_ValidTypeXTokenChar(unsigned char _byte) {
121 return !isRfc822_SPACE(_byte);
124 static inline BOOL isMime_ValidTypeAttributeChar(unsigned char _byte) {
125 return isMime_TokenChar(_byte);
128 static inline NSData *_quotedPrintableEncoding(NSData *_data) {
129 const char *bytes = [_data bytes];
130 unsigned int length = [_data length];
131 NSData *result = nil;
133 unsigned int desLen = 0;
134 unsigned cnt = length;
135 const char *test = bytes;
139 if ((unsigned char)*test > 127) {
146 if (!doEnc) return _data;
148 des = NGMallocAtomic(sizeof(char) * desLen + 2);
150 desLen = NGEncodeQuotedPrintable(bytes, length, des, desLen);
151 if ((int)desLen != -1) {
152 result = [NSData dataWithBytesNoCopy:des length:desLen];
155 NSLog(@"WARNING(%s): An error occour during quoted-printable decoding",
156 __PRETTY_FUNCTION__);
157 if (des) NGFree(des);
163 static inline NSData *_rfc2047Decoding(char _enc, const char *_bytes,
164 unsigned int _length)
168 if ((_enc == 'b') || (_enc == 'B')) { // use BASE64 decoding
171 tmp = [[NSData alloc] initWithBytes:_bytes length:_length];
172 data = [tmp dataByDecodingBase64];
175 else if ((_enc == 'q') || (_enc == 'Q')) { // use quoted-printable decoding
181 dest = NGMallocAtomic(destSize * sizeof(char));
182 resSize = NGDecodeQuotedPrintable(_bytes, _length, dest, destSize);
183 if ((int)resSize != -1) {
184 data = [NSData dataWithBytesNoCopy:dest length:resSize];
187 NSLog(@"WARNING(%s): An error occour during quoted-printable decoding",
188 __PRETTY_FUNCTION__);
189 NGFree(dest); dest = NULL;
190 data = [NSData dataWithBytes:_bytes length:_length];
194 NSLog(@"WARNING(%s): unknown encoding type %c", __PRETTY_FUNCTION__, _enc);
195 data = [NSData dataWithBytes:_bytes length:_length];
200 #endif /* __NGMime_NGMimeUtilities_H__ */