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 #include <NGMime/NGMimeType.h>
23 #include <NGMime/NGMimeUtilities.h>
32 } NGMimeParameterConstants;
34 static NGMimeParameterConstants *MimeParameterConstants = NULL;
35 static Class NSStringClass = Nil;
36 static int MimeLogEnabled = -1;
38 static NSString *_stringForParameterName(char *_type, int _len) {
39 if (NSStringClass == Nil)
40 NSStringClass = [NSString class];
42 if (MimeLogEnabled == -1) {
43 MimeLogEnabled = [[NSUserDefaults standardUserDefaults]
44 boolForKey:@"MimeLogEnabled"]?1:0;
47 if (MimeParameterConstants == NULL) {
48 MimeParameterConstants = malloc(sizeof(NGMimeParameterConstants));
50 MimeParameterConstants->charset = NGMimeParameterTextCharset;
51 MimeParameterConstants->q = @"q";
52 MimeParameterConstants->name = @"name";
53 MimeParameterConstants->boundary = @"boundary";
54 MimeParameterConstants->fileName = @"filename";
55 MimeParameterConstants->reportType = @"report-type";
62 return MimeParameterConstants->q;
65 if (strncmp(_type, "name", 4) == 0)
66 return MimeParameterConstants->name;
69 if (strncmp(_type, "charset", 7) == 0)
70 return MimeParameterConstants->charset;
73 if (strncmp(_type, "boundary", 8) == 0)
74 return MimeParameterConstants->boundary;
75 if (strncmp(_type, "filename", 8) == 0)
76 return MimeParameterConstants->fileName;
79 if (strncmp(_type, "report-type", 11) == 0)
80 return MimeParameterConstants->reportType;
83 return [NSStringClass stringWithCString:_type length:_len];
86 NSDictionary *parseParameters(id self, NSString *_str, unichar *cstr) {
88 NSMutableDictionary *paras;
90 paras = [[NSMutableDictionary alloc] initWithCapacity:8];
94 NSString *attrName, *attrValue;
99 // consume end of previous entry (spaces and ';')
100 while ((*cstr == ';') || isRfc822_LWSP(*cstr))
106 while ((*cstr != '\0') && (isMime_ValidTypeAttributeChar(*cstr))) {
113 unsigned char buf[len + 1];
117 for (i = 0; i < len; i++) buf[i] = tolower(tmp[i]);
119 attrName = _stringForParameterName((char *)buf, len);
122 while ((*cstr != '\0') && (isRfc822_LWSP(*cstr))) {
125 // no value was given for attribute
128 [self logWithFormat:@"WARNING(%s): attribute '%@' has no value in "
129 @"MimeType '%@'", __PRETTY_FUNCTION__, attrName, _str];
136 [self logWithFormat:@"WARNING(%s): attribute '%@', missing '=' "
137 @"in MimeType '%@'", __PRETTY_FUNCTION__, attrName, _str];
143 while ((*cstr != '\0') && (isRfc822_LWSP(*cstr)))
146 /* no value was given for parameter */
149 [self logWithFormat:@"WARNING(%s): attribute '%@' has no value "
150 @"in MimeType '%@'", __PRETTY_FUNCTION__, attrName, _str];
154 /* now parameter read value */
155 if (isRfc822_QUOTE(*cstr)) { // quoted value
159 while (!isRfc822_QUOTE(*cstr) && (*cstr != '\0')) {
163 attrValue = [[[NSString alloc] initWithCharacters:tmp length:len]
166 if (*cstr == '\0') { // quote was not closed
168 [self logWithFormat:@"WARNING(%s): value-quotes in attribute "
169 @"'%@' were not closed in MimeType '%@'",
170 __PRETTY_FUNCTION__, attrName, _str];
173 cstr++; // skip closing quote
175 else { /* value without quotes */
179 while (*cstr != '\0') {
180 if (isRfc822_SPACE(*cstr)) break;
181 if (isRfc822_CTL(*cstr)) break;
183 if (*cstr == ';') break; /* parameter separator */
188 attrValue = [[[NSString alloc] initWithCharacters:tmp length:len]
191 /* store attr/value pair in dictionary */
192 [paras setObject:attrValue forKey:attrName];
197 while ((*cstr != '\0') && (isRfc822_LWSP(*cstr)))
200 if (*cstr == ';') // skip ';' (attribute separator)
202 else if (*cstr == '\0') /* parsing is finished, exit loop */
206 [self logWithFormat:@"WARNING(%s): expected end of string or ';', "
207 @"got '%c'%i (str='%@')", __PRETTY_FUNCTION__, *cstr,
212 while ((*cstr != '\0') && (isRfc822_LWSP(*cstr)))
216 return [paras autorelease];