]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeContentDispositionHeaderFieldGenerator.m
fixed gcc 4.0 warnings
[sope] / sope-mime / NGMime / NGMimeContentDispositionHeaderFieldGenerator.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGMimeHeaderFieldGenerator.h"
23 #include "NGMimeHeaderFields.h"
24 #include "common.h"
25
26 @implementation NGMimeContentDispositionHeaderFieldGenerator
27
28 + (int)version {
29   return 2;
30 }
31
32 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
33   value:(id)_value
34 {
35   NGMimeContentDispositionHeaderField *field;
36   NSString      *tmp;
37   NSMutableData *data;
38   
39   field = _value;
40   if (field == nil) {
41     [self logWithFormat:@"WARNING(%s): Content-Disposition field is empty",
42           __PRETTY_FUNCTION__];
43     return [NSData data];
44   }
45   
46   if ([_value isKindOfClass:[NSString class]])
47     return [_value dataUsingEncoding:NSUTF8StringEncoding];
48   
49   // TODO: move the stuff below to some NSString or NSData category?
50   
51   data = [NSMutableData dataWithCapacity:64];
52   tmp  = [field type];
53   [data appendBytes:[tmp cString] length:[tmp length]];
54   tmp = [field filename];
55   if (tmp != nil) {
56     [data appendBytes:"; " length:2];
57     [data appendBytes:"filename=\"" length:10];
58     {
59       unsigned char *ctmp;
60       int  cnt, len;
61       BOOL doEnc;
62       
63       // TODO: unicode?
64       len  = [tmp cStringLength];
65       ctmp = malloc(len + 3);
66       [tmp getCString:(char *)ctmp]; ctmp[len] = '\0';
67       cnt  = 0;
68       doEnc = NO;
69       while (cnt < len) {
70         if ((unsigned char)ctmp[cnt] > 127) {
71           doEnc = YES;
72           break;
73         }
74         cnt++;
75       }
76       if (doEnc) {
77         char        iso[]     = "=?iso-8859-15?q?";
78         unsigned    isoLen    = 16;
79         char        isoEnd[]  = "?=";
80         unsigned    isoEndLen = 2;
81         unsigned    desLen;
82         char        *des;
83       
84         if (ctmp) free(ctmp);
85         {
86           NSData *data;
87
88 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
89           data = [tmp dataUsingEncoding:NSISOLatin1StringEncoding];
90 #else
91           data = [tmp dataUsingEncoding:NSISOLatin9StringEncoding];
92 #endif
93
94           len  = [data length];
95           ctmp =  malloc(len+1);
96           [data getBytes:ctmp];  ctmp[len] = '\0';
97         }
98           
99         desLen = len * 3 + 20;
100         des    = calloc(desLen + 10, sizeof(char));
101       
102         memcpy(des, ctmp, cnt);
103         memcpy(des + cnt, iso, isoLen);
104         desLen =
105           NGEncodeQuotedPrintableMime((unsigned char *)ctmp + cnt, len - cnt,
106                                       (unsigned char *)des + cnt + isoLen,
107                                       desLen - cnt - isoLen);
108         if ((int)desLen != -1) {
109           memcpy(des + cnt + isoLen + desLen, isoEnd, isoEndLen);
110           [data appendBytes:des length:(cnt + isoLen + desLen + isoEndLen)];
111         }
112         else {
113           [self logWithFormat:@"WARNING(%s:%i): An error occour during "
114                 @"quoted-printable decoding",
115                 __PRETTY_FUNCTION__, __LINE__];
116         }
117         if (des) free(des);
118       }
119       else {
120         [data appendBytes:ctmp length:len];
121       }
122     }
123       //      [data appendBytes:[tmp cString] length:[tmp length]];
124       [data appendBytes:"\"" length:1];
125   }
126   return data;
127 }
128   
129 @end /* NGMimeContentDispositionHeaderFieldGenerator */