2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
21 // $Id: NGMimeContentTypeHeaderFieldGenerator.m 1 2004-08-20 10:08:27Z znek $
23 #include "NGMimeHeaderFieldGenerator.h"
24 #include "NGMimeHeaderFields.h"
27 @implementation NGMimeContentTypeHeaderFieldGenerator
33 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
36 NGMimeType *type = nil; // only one content-type field
38 NSMutableData *data = nil;
39 unsigned char *ctmp = NULL;
45 NSLog(@"WARNING(%s): empty content type field", __PRETTY_FUNCTION__);
46 return [NSData dataWithBytes:"application/octet" length:17];
48 if ([_value isKindOfClass:[NSString class]]) {
49 return [_value dataUsingEncoding:NSUTF8StringEncoding];
52 if (![type isKindOfClass:[NGMimeType class]]) {
53 NSLog(@"WARNING(%s): invalid MIME type value (%@) !", __PRETTY_FUNCTION__,
55 return [NSData dataWithBytes:"application/octet" length:17];
58 data = [NSMutableData dataWithCapacity:64];
61 NSAssert(tmp, @"type should not be nil");
63 ctmp = malloc(len + 1);
64 [tmp getCString:ctmp]; ctmp[len] = '\0';
65 [data appendBytes:ctmp length:len];
68 [data appendBytes:"//" length:1];
73 ctmp = malloc(len + 1);
74 [tmp getCString:ctmp]; ctmp[len] = '\0';
75 [data appendBytes:ctmp length:len];
79 [data appendBytes:"*" length:1];
82 NSEnumerator *enumerator = [type parameterNames];
84 NSString *value = nil;
86 while ((name = [enumerator nextObject])) {
87 value = [type valueOfParameter:name];
88 if (![value isKindOfClass:[NSString class]]) {
89 NSLog(@"ERROR[%s]: parameter should be a NSString headerField: %@ "
90 @"value %@", __PRETTY_FUNCTION__, _headerField, _value);
93 [data appendBytes:"; " length:2];
95 len = [name cStringLength];
96 ctmp = malloc(len + 1);
97 [name getCString:ctmp]; ctmp[len] = '\0';
98 [data appendBytes:ctmp length:len];
102 this confuses GroupWise: "= \"" (a space)
104 [data appendBytes:"=\"" length:2];
106 /* check for encoding */
111 len = [value cStringLength];
112 ctmp = malloc(len + 1);
113 [value getCString:ctmp]; ctmp[len] = '\0';
117 if ((unsigned char)ctmp[cnt] > 127) {
124 unsigned char iso[] = "=?iso-8859-15?q?";
125 unsigned isoLen = 16;
126 unsigned char isoEnd[] = "?=";
127 unsigned isoEndLen = 2;
131 if (ctmp) free(ctmp);
135 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
136 data = [value dataUsingEncoding:NSISOLatin1StringEncoding];
138 data = [value dataUsingEncoding:NSISOLatin9StringEncoding];
142 ctmp = malloc(len + 10);
143 [data getBytes:ctmp]; ctmp[len] = '\0';
146 desLen = len * 3 + 20;
147 des = calloc(desLen + 10, sizeof(char));
149 memcpy(des, ctmp, cnt);
150 memcpy(des + cnt, iso, isoLen);
152 NGEncodeQuotedPrintableMime(ctmp + cnt, len - cnt,
154 desLen - cnt - isoLen);
155 if ((int)desLen != -1) {
156 memcpy(des + cnt + isoLen + desLen, isoEnd, isoEndLen);
157 [data appendBytes:des length:(cnt + isoLen + desLen + isoEndLen)];
160 NSLog(@"WARNING: An error occour during quoted-printable decoding");
165 [data appendBytes:ctmp length:len];
169 [data appendBytes:"\"" length:1];
175 @end /* NGMimeContentTypeHeaderFieldGenerator */