]> err.no Git - sope/blob - sope-core/NGMime/NGMimeUtilities.h
renamed packages as discussed in the developer list
[sope] / sope-core / NGMime / NGMimeUtilities.h
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #ifndef __NGMime_NGMimeUtilities_H__
24 #define __NGMime_NGMimeUtilities_H__
25
26 #import <Foundation/Foundation.h>
27 #import <NGExtensions/NGExtensions.h>
28 #include <NGMime/NGMimeDecls.h>
29
30 // ******************** RFC 822 ********************
31
32 static inline BOOL isRfc822_SpecialByte(unsigned char _byte) {
33   switch (_byte) {
34   case '(': case ')': case '<': case '>': case '@':
35   case ',': case ';': case ':': case '"': case '\\':
36   case '.': case '[': case ']':
37     return YES;
38   default:
39     return NO;
40   }
41 }
42
43 // single chars
44 NSDictionary *parseParameters(id self, NSString *_str, unichar *cstr);
45
46 static inline BOOL isRfc822_CR(unsigned char _byte) {
47   return (_byte == 13);
48 }
49 static inline BOOL isRfc822_LF(unsigned char _byte) {
50   return (_byte == 10);
51 }
52 static inline BOOL isRfc822_HTAB(unsigned char _byte) {
53   return (_byte == 9);
54 }
55 static inline BOOL isRfc822_SPACE(unsigned char _byte) {
56   return (_byte == 32);
57 }
58 static inline BOOL isRfc822_QUOTE(unsigned char _byte) {
59   return (_byte == 34);
60 }
61
62 // ranges
63
64 static inline BOOL isRfc822_CHAR(unsigned char _byte) {
65   return (_byte < 128);
66 }
67
68 static inline BOOL isRfc822_CTL(unsigned char _byte) {
69   return (_byte < 32) || (_byte == 127);
70 }
71
72 static inline BOOL isRfc822_ALPHA(unsigned char _byte) {
73   return (((_byte >= 65) && (_byte <= 90)) ||
74           ((_byte >= 97) && (_byte <= 122)));
75 }
76
77 static inline BOOL isRfc822_DIGIT(unsigned char _byte) {
78   return (_byte >= 48) && (_byte <= 57);
79 }
80
81 static inline BOOL isRfc822_LWSP(unsigned char _byte) {
82   return (isRfc822_SPACE(_byte) || isRfc822_HTAB(_byte));
83 }
84
85
86 static inline BOOL isRfc822_FieldNameChar(unsigned char _byte) {
87   return (isRfc822_CHAR(_byte) &&
88           !(isRfc822_CTL(_byte) || isRfc822_SPACE(_byte) || (_byte == ':')));
89 }
90
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)));
95 }
96
97 // ******************** MIME ***********************
98
99 static inline BOOL isMime_SpecialByte(unsigned char _byte) {
100   switch (_byte) {
101   case '(': case ')': case '<': case '>': case '@':
102   case ',': case ';': case ':': case '"': case '\\':
103   case '/': case '=': case '[': case ']': case '?':
104     return YES;
105   default:
106     return NO;
107   }
108 }
109
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)));
114 }
115
116 static inline BOOL isMime_SafeChar(unsigned char _byte) {
117   return ((_byte >= 33 && _byte <= 60) || (_byte >= 62 && _byte <= 126));
118 }
119
120 static inline BOOL isMime_ValidTypeXTokenChar(unsigned char _byte) {
121   return !isRfc822_SPACE(_byte);
122 }
123
124 static inline BOOL isMime_ValidTypeAttributeChar(unsigned char _byte) {
125   return isMime_TokenChar(_byte);
126 }
127
128 static inline NSData *_quotedPrintableEncoding(NSData *_data) {
129   const char   *bytes  = [_data bytes];
130   unsigned int length  = [_data length];
131   NSData       *result = nil;  
132   char         *des    = NULL;
133   unsigned int desLen  = 0;
134   unsigned     cnt     = length;
135   const char   *test   = bytes;    
136   BOOL         doEnc   = NO;
137
138   while (cnt > 0) {
139     if ((unsigned char)*test > 127) {
140       doEnc = YES;
141       break;
142     }
143     test++;
144     cnt--;
145   }
146   if (!doEnc) return _data;
147   desLen = length *3;
148   des = NGMallocAtomic(sizeof(char) * desLen + 2);
149   
150   desLen = NGEncodeQuotedPrintable(bytes, length, des, desLen);
151   if ((int)desLen != -1) {
152     result = [NSData dataWithBytesNoCopy:des length:desLen];
153   }
154   else {
155     NSLog(@"WARNING(%s): An error occour during quoted-printable decoding",
156           __PRETTY_FUNCTION__);
157     if (des) NGFree(des);
158     result = _data;
159   }
160   return result;
161 }
162
163 static inline NSData *_rfc2047Decoding(char _enc, const char *_bytes,
164                                        unsigned int _length) 
165 {
166   NSData *data = nil;
167
168   if ((_enc == 'b') || (_enc == 'B')) { // use BASE64 decoding
169     NSData *tmp;
170     
171     tmp = [[NSData alloc] initWithBytes:_bytes length:_length];
172     data = [tmp dataByDecodingBase64];
173     RELEASE(tmp);
174   }
175   else if ((_enc == 'q') || (_enc == 'Q')) { // use quoted-printable decoding
176     char   *dest    = NULL;
177     size_t destSize = 0;
178     size_t resSize  = 0;
179
180     destSize = _length;
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];
185     }
186     else {
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];
191     }
192   }
193   else {
194     NSLog(@"WARNING(%s): unknown encoding type %c", __PRETTY_FUNCTION__, _enc);
195     data = [NSData dataWithBytes:_bytes length:_length];
196   }
197   return data;
198 }
199
200 #endif /* __NGMime_NGMimeUtilities_H__ */