1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: mimetype.c 955 2008-03-06 23:52:36Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2016 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/mimetype.h"
21 #include "../pith/state.h"
22 #include "../pith/conf.h"
23 #include "../pith/mailcap.h"
24 #include "../pith/util.h"
27 * We've decided not to implement the RFC1524 standard minimum path, because
28 * some of us think it is harder to debug a problem when you may be misled
29 * into looking at the wrong mailcap entry. Likewise for MIME.Types files.
31 #if defined(DOS) || defined(OS2)
32 #define MT_PATH_SEPARATOR ';'
33 #define MT_USER_FILE "MIMETYPE"
34 #define MT_STDPATH NULL
36 #define MT_PATH_SEPARATOR ':'
37 #define MT_USER_FILE NULL
39 ".mime.types:/etc/mime.types:/usr/local/lib/mime.types"
42 #define LINE_BUF_SIZE 2000
46 * Types used to pass parameters and operator functions to the
47 * mime.types searching routines.
49 #define MT_MAX_FILE_EXTENSION 3
55 int mt_browse_types_file(MT_OPERATORPROC
, MT_MAP_T
*, char *);
56 int mt_srch_by_type(MT_MAP_T
*, FILE *);
61 * Exported function that does the work of sniffing the mime.types
62 * files and filling in the body pointer if found. Returns 1 (TRUE) if
63 * extension found, and body pointer filled in, 0 (FALSE) otherwise.
66 set_mime_type_by_extension(struct mail_bodystruct
*body
, char *filename
)
70 if(mt_get_file_ext(filename
, &e2b
.from
.ext
)
71 && mt_srch_mime_type(mt_srch_by_ext
, &e2b
)){
72 body
->type
= e2b
.to
.mime
.type
;
73 body
->subtype
= e2b
.to
.mime
.subtype
; /* NOTE: subtype was malloc'd */
82 * Exported function that maps from mime types to file extensions.
85 set_mime_extension_by_type (char *ext
, char *mtype
)
89 t2e
.from
.mime_type
= mtype
;
91 return (mt_srch_mime_type (mt_srch_by_type
, &t2e
));
98 * Separate and return a pointer to the first character in the 'filename'
99 * character buffer that comes after the rightmost '.' character in the
100 * filename. (What I mean is a pointer to the filename - extension).
102 * Returns 1 if an extension is found, 0 otherwise.
105 mt_get_file_ext(char *filename
, char **extension
)
107 dprint((5, "mt_get_file_ext : filename=\"%s\", ",
108 filename
? filename
: "?"));
110 for(*extension
= NULL
; filename
&& *filename
; filename
++)
112 *extension
= filename
+ 1;
114 dprint((5, "extension=\"%s\"\n",
115 (extension
&& *extension
) ? *extension
: "?"));
117 return(*extension
? 1 : 0);
122 * Build a list of possible mime.type files. For each one that exists
123 * call the mt_operator function.
124 * Loop terminates when mt_operator returns non-zero.
127 mt_srch_mime_type(MT_OPERATORPROC mt_operator
, MT_MAP_T
*mt_map
)
129 char *s
, *pathcopy
, *path
;
132 dprint((5, "- mt_srch_mime_type -\n"));
134 pathcopy
= mc_conf_path(ps_global
->VAR_MIMETYPE_PATH
, getenv("MIMETYPES"),
135 MT_USER_FILE
, MT_PATH_SEPARATOR
, MT_STDPATH
);
137 path
= pathcopy
; /* overloaded "path" */
139 dprint((7, "mime_types: path: %s\n", path
? path
: "?"));
141 if((s
= strindex(path
, MT_PATH_SEPARATOR
)) != NULL
)
144 if((rv
= mt_browse_types_file(mt_operator
, mt_map
, path
)) != 0)
151 fs_give((void **)&pathcopy
);
153 if(!rv
&& mime_os_specific_access()){
154 if(mt_operator
== mt_srch_by_ext
){
158 if(mime_get_os_mimetype_from_ext(mt_map
->from
.ext
, buf
, 256)){
159 if((s
= strindex(buf
, '/')) != NULL
){
161 mt_map
->to
.mime
.type
= mt_translate_type(buf
);
162 mt_map
->to
.mime
.subtype
= cpystr(s
);
167 else if(mt_operator
== mt_srch_by_type
){
168 if(mime_get_os_ext_from_mimetype(mt_map
->from
.mime_type
,
169 mt_map
->to
.ext
, 32)){
170 /* the 32 comes from var ext[] in display_attachment() */
171 if(*(s
= mt_map
->to
.ext
) == '.')
172 while((*s
= *(s
+1)) != '\0')
179 alpine_panic("Unhandled mime type search");
182 /* if we still can not find the type, but it is a .docx (or alike) extension
183 set the type here. Do not use the grope function.
186 rv
= 1; /* assume success */
187 mt_map
->to
.mime
.type
= TYPEAPPLICATION
;
188 if(!strucmp(mt_map
->from
.ext
, "docx"))
189 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.WORDPROCESSINGML.DOCUMENT");
190 else if(!strucmp(mt_map
->from
.ext
, "xslx"))
191 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.SPREADSHEETML.SHEET");
192 else if(!strucmp(mt_map
->from
.ext
, "xltx"))
193 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.SPREADSHEETML.TEMPLATE");
194 else if(!strucmp(mt_map
->from
.ext
, "potx"))
195 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.PRESENTATIONML.TEMPLATE");
196 else if(!strucmp(mt_map
->from
.ext
, "ppsx"))
197 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.PRESENTATIONML.SLIDESHOW");
198 else if(!strucmp(mt_map
->from
.ext
, "pptx"))
199 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.PRESENTATIONML.PRESENTATION");
200 else if(!strucmp(mt_map
->from
.ext
, "sldx"))
201 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.PRESENTATIONML.SLIDE");
202 else if(!strucmp(mt_map
->from
.ext
, "dotx"))
203 mt_map
->to
.mime
.subtype
= cpystr("VND.OPENXMLFORMATS-OFFICEDOCUMENT.WORDPROCESSINGML.TEMPLATE");
204 else if(!strucmp(mt_map
->from
.ext
, "xlam"))
205 mt_map
->to
.mime
.subtype
= cpystr("VND.MS-EXCEL.ADDIN.MACROENABLED.12");
206 else if(!strucmp(mt_map
->from
.ext
, "xslb"))
207 mt_map
->to
.mime
.subtype
= cpystr("VND.MS-EXCEL.SHEET.BINARY.MACROENABLED.12");
208 else rv
= 0; /* else, failure */
217 * Try to match a file extension against extensions found in the file
218 * ``filename'' if that file exists. return 1 if a match
219 * was found and 0 in all other cases.
222 mt_browse_types_file(MT_OPERATORPROC mt_operator
, MT_MAP_T
*mt_map
, char *filename
)
227 dprint((7, "mt_browse_types_file(%s)\n", filename
? filename
: "?"));
228 if((file
= our_fopen(filename
, "rb")) != NULL
){
229 rv
= (*mt_operator
)(mt_map
, file
);
233 dprint((1, "mt_browse: FAILED open(%s) : %s.\n",
234 filename
? filename
: "?", error_description(errno
)));
242 * scan each line of the file. Treat each line as a mime type definition.
243 * The first word is a type/subtype specification. All following words
244 * are file extensions belonging to that type/subtype. Words are separated
245 * bij whitespace characters.
246 * If a file extension occurs more than once, then the first definition
247 * determines the file type and subtype.
250 mt_srch_by_ext(MT_MAP_T
*e2b
, FILE *file
)
252 char buffer
[LINE_BUF_SIZE
];
254 /* construct a loop reading the file line by line. Then check each
255 * line for a matching definition.
257 while(fgets(buffer
,LINE_BUF_SIZE
,file
) != NULL
){
262 continue; /* comment */
264 /* divide the input buffer into words separated by whitespace.
265 * The first words is the type and subtype. All following words
266 * are file extensions.
268 dprint((5, "traverse: buffer=\"%s\"\n", buffer
));
269 typespec
= strtok(buffer
," \t"); /* extract type,subtype */
273 dprint((5, "typespec=\"%s\"\n", typespec
? typespec
: "?"));
274 while((try_extension
= strtok(NULL
, " \t\n\r")) != NULL
){
275 /* compare the extensions, and assign the type if a match
278 dprint((5,"traverse: trying ext \"%s\"\n",try_extension
));
279 if(strucmp(try_extension
, e2b
->from
.ext
) == 0){
280 /* split the 'type/subtype' specification */
281 char *type
, *subtype
= NULL
;
283 type
= strtok(typespec
,"/");
285 subtype
= strtok(NULL
,"/");
287 dprint((5, "traverse: type=%s, subtype=%s.\n",
288 type
? type
: "<null>",
289 subtype
? subtype
: "<null>"));
290 /* The type is encoded as a small integer. we have to
291 * translate the character string naming the type into
292 * the corresponding number.
294 e2b
->to
.mime
.type
= mt_translate_type(type
);
295 e2b
->to
.mime
.subtype
= cpystr(subtype
? subtype
: "x-unknown");
296 return 1; /* a match has been found */
301 dprint((5, "traverse: search failed.\n"));
307 * scan each line of the file. Treat each line as a mime type definition.
308 * Here we are looking for a matching type. When that is found return the
309 * first extension that is three chars or less.
312 mt_srch_by_type(MT_MAP_T
*t2e
, FILE *file
)
314 char buffer
[LINE_BUF_SIZE
];
316 /* construct a loop reading the file line by line. Then check each
317 * line for a matching definition.
319 while(fgets(buffer
,LINE_BUF_SIZE
,file
) != NULL
){
324 continue; /* comment */
326 /* divide the input buffer into words separated by whitespace.
327 * The first words is the type and subtype. All following words
328 * are file extensions.
330 dprint((5, "traverse: buffer=%s.\n", buffer
));
331 typespec
= strtok(buffer
," \t"); /* extract type,subtype */
332 dprint((5, "typespec=%s.\n", typespec
? typespec
: "?"));
333 if (strucmp (typespec
, t2e
->from
.mime_type
) == 0) {
334 while((try_extension
= strtok(NULL
, " \t\n\r")) != NULL
){
335 if (strlen (try_extension
) <= MT_MAX_FILE_EXTENSION
) {
336 strncpy (t2e
->to
.ext
, try_extension
, 32);
338 * not sure of the 32, so don't write to byte 32
340 * t2e->to.ext[31] = '\0';
341 * in case that breaks something
350 dprint((5, "traverse: search failed.\n"));
356 * Translate a character string representing a content type into a short
357 * integer number, according to the coding described in c-client/mail.h
358 * List of content types taken from rfc1521, September 1993.
361 mt_translate_type(char *type
)
365 for (i
=0;(i
<=TYPEMAX
) && body_types
[i
] && strucmp(type
,body_types
[i
]);i
++)
370 else if (!body_types
[i
]) /* if empty slot, assign it to this type */
371 body_types
[i
] = cpystr (type
);