2 * MIME database file routines for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/string-private.h>
16 #include "mime-private.h"
20 * Debug macros that used to be private API...
24 #define DEBUG_printf(...)
31 typedef struct _mime_fcache_s /**** Filter cache structure ****/
33 char *name, /* Filter name */
34 *path; /* Full path to filter if available */
42 static const char *mime_add_fcache(cups_array_t *filtercache, const char *name,
43 const char *filterpath);
44 static int mime_compare_fcache(_mime_fcache_t *a, _mime_fcache_t *b);
45 static void mime_delete_fcache(cups_array_t *filtercache);
46 static void mime_delete_rules(mime_magic_t *rules);
47 static void mime_load_convs(mime_t *mime, const char *filename,
48 const char *filterpath,
49 cups_array_t *filtercache);
50 static void mime_load_types(mime_t *mime, const char *filename);
54 * 'mimeDelete()' - Delete (free) a MIME database.
58 mimeDelete(mime_t *mime) /* I - MIME database */
60 mime_type_t *type; /* Current type */
61 mime_filter_t *filter; /* Current filter */
64 DEBUG_printf(("mimeDelete(mime=%p)", mime));
70 * Loop through filters and free them...
73 for (filter = (mime_filter_t *)cupsArrayFirst(mime->filters);
75 filter = (mime_filter_t *)cupsArrayNext(mime->filters))
76 mimeDeleteFilter(mime, filter);
79 * Loop through the file types and delete any rules...
82 for (type = (mime_type_t *)cupsArrayFirst(mime->types);
84 type = (mime_type_t *)cupsArrayNext(mime->types))
85 mimeDeleteType(mime, type);
88 * Free the types and filters arrays, and then the MIME database structure.
91 cupsArrayDelete(mime->types);
92 cupsArrayDelete(mime->filters);
93 cupsArrayDelete(mime->srcs);
99 * 'mimeDeleteFilter()' - Delete a filter from the MIME database.
103 mimeDeleteFilter(mime_t *mime, /* I - MIME database */
104 mime_filter_t *filter) /* I - Filter */
106 DEBUG_printf(("mimeDeleteFilter(mime=%p, filter=%p(%s/%s->%s/%s, cost=%d, "
107 "maxsize=" CUPS_LLFMT "))", mime, filter,
108 filter ? filter->src->super : "???",
109 filter ? filter->src->type : "???",
110 filter ? filter->dst->super : "???",
111 filter ? filter->dst->super : "???",
112 filter ? filter->cost : -1,
113 filter ? CUPS_LLCAST filter->maxsize : CUPS_LLCAST -1));
115 if (!mime || !filter)
119 if (!cupsArrayFind(mime->filters, filter))
120 DEBUG_puts("1mimeDeleteFilter: Filter not in MIME database.");
123 cupsArrayRemove(mime->filters, filter);
127 * Deleting a filter invalidates the source lookup cache used by
133 DEBUG_puts("1mimeDeleteFilter: Deleting source lookup cache.");
134 cupsArrayDelete(mime->srcs);
141 * 'mimeDeleteType()' - Delete a type from the MIME database.
145 mimeDeleteType(mime_t *mime, /* I - MIME database */
146 mime_type_t *mt) /* I - Type */
148 DEBUG_printf(("mimeDeleteType(mime=%p, mt=%p(%s/%s))", mime, mt,
149 mt ? mt->super : "???", mt ? mt->type : "???"));
155 if (!cupsArrayFind(mime->types, mt))
156 DEBUG_puts("1mimeDeleteFilter: Type not in MIME database.");
159 cupsArrayRemove(mime->types, mt);
161 mime_delete_rules(mt->rules);
167 * '_mimeError()' - Show an error message.
171 _mimeError(mime_t *mime, /* I - MIME database */
172 const char *message, /* I - Printf-style message string */
173 ...) /* I - Additional arguments as needed */
175 va_list ap; /* Argument pointer */
176 char buffer[8192]; /* Message buffer */
181 va_start(ap, message);
182 vsnprintf(buffer, sizeof(buffer), message, ap);
185 (*mime->error_cb)(mime->error_ctx, buffer);
191 * 'mimeFirstFilter()' - Get the first filter in the MIME database.
194 mime_filter_t * /* O - Filter or NULL */
195 mimeFirstFilter(mime_t *mime) /* I - MIME database */
197 DEBUG_printf(("6mimeFirstFilter(mime=%p)", mime));
201 DEBUG_puts("7mimeFirstFilter: Returning NULL.");
206 mime_filter_t *first = (mime_filter_t *)cupsArrayFirst(mime->filters);
209 DEBUG_printf(("7mimeFirstFilter: Returning %p.", first));
216 * 'mimeFirstType()' - Get the first type in the MIME database.
219 mime_type_t * /* O - Type or NULL */
220 mimeFirstType(mime_t *mime) /* I - MIME database */
222 DEBUG_printf(("6mimeFirstType(mime=%p)", mime));
226 DEBUG_puts("7mimeFirstType: Returning NULL.");
231 mime_type_t *first = (mime_type_t *)cupsArrayFirst(mime->types);
234 DEBUG_printf(("7mimeFirstType: Returning %p.", first));
241 * 'mimeLoad()' - Create a new MIME database from disk.
243 * This function uses @link mimeLoadFilters@ and @link mimeLoadTypes@ to
244 * create a MIME database from a single directory.
247 mime_t * /* O - New MIME database */
248 mimeLoad(const char *pathname, /* I - Directory to load */
249 const char *filterpath) /* I - Directory to load */
251 mime_t *mime; /* New MIME database */
253 DEBUG_printf(("mimeLoad(pathname=\"%s\", filterpath=\"%s\")", pathname,
256 mime = mimeLoadFilters(mimeLoadTypes(NULL, pathname), pathname, filterpath);
257 DEBUG_printf(("1mimeLoad: Returning %p.", mime));
264 * 'mimeLoadFilters()' - Load filter definitions from disk.
266 * This function loads all of the .convs files from the specified directory.
267 * Use @link mimeLoadTypes@ to load all types before you load the filters.
270 mime_t * /* O - MIME database */
271 mimeLoadFilters(mime_t *mime, /* I - MIME database */
272 const char *pathname, /* I - Directory to load from */
273 const char *filterpath) /* I - Default filter program directory */
275 cups_dir_t *dir; /* Directory */
276 cups_dentry_t *dent; /* Directory entry */
277 char filename[1024]; /* Full filename of .convs file */
278 cups_array_t *filtercache; /* Filter cache */
281 DEBUG_printf(("mimeLoadFilters(mime=%p, pathname=\"%s\", filterpath=\"%s\")",
282 mime, pathname, filterpath));
285 * Range check input...
288 if (!mime || !pathname || !filterpath)
290 DEBUG_puts("1mimeLoadFilters: Bad arguments.");
295 * Then open the directory specified by pathname...
298 if ((dir = cupsDirOpen(pathname)) == NULL)
300 DEBUG_printf(("1mimeLoadFilters: Unable to open \"%s\": %s", pathname,
302 _mimeError(mime, "Unable to open \"%s\": %s", pathname, strerror(errno));
307 * Read all the .convs files...
310 filtercache = cupsArrayNew((cups_array_func_t)mime_compare_fcache, NULL);
312 while ((dent = cupsDirRead(dir)) != NULL)
314 if (strlen(dent->filename) > 6 &&
315 !strcmp(dent->filename + strlen(dent->filename) - 6, ".convs"))
318 * Load a mime.convs file...
321 snprintf(filename, sizeof(filename), "%s/%s", pathname, dent->filename);
322 DEBUG_printf(("1mimeLoadFilters: Loading \"%s\".", filename));
323 mime_load_convs(mime, filename, filterpath, filtercache);
327 mime_delete_fcache(filtercache);
336 * 'mimeLoadTypes()' - Load type definitions from disk.
338 * This function loads all of the .types files from the specified directory.
339 * Use @link mimeLoadFilters@ to load all filters after you load the types.
342 mime_t * /* O - MIME database */
343 mimeLoadTypes(mime_t *mime, /* I - MIME database or @code NULL@ to create a new one */
344 const char *pathname) /* I - Directory to load from */
346 cups_dir_t *dir; /* Directory */
347 cups_dentry_t *dent; /* Directory entry */
348 char filename[1024]; /* Full filename of .types file */
351 DEBUG_printf(("mimeLoadTypes(mime=%p, pathname=\"%s\")", mime, pathname));
354 * First open the directory specified by pathname...
357 if ((dir = cupsDirOpen(pathname)) == NULL)
359 DEBUG_printf(("1mimeLoadTypes: Unable to open \"%s\": %s", pathname,
361 DEBUG_printf(("1mimeLoadTypes: Returning %p.", mime));
362 _mimeError(mime, "Unable to open \"%s\": %s", pathname, strerror(errno));
367 * If "mime" is NULL, make a new, empty database...
376 DEBUG_puts("1mimeLoadTypes: Returning NULL.");
381 * Read all the .types files...
384 while ((dent = cupsDirRead(dir)) != NULL)
386 if (strlen(dent->filename) > 6 &&
387 !strcmp(dent->filename + strlen(dent->filename) - 6, ".types"))
390 * Load a mime.types file...
393 snprintf(filename, sizeof(filename), "%s/%s", pathname, dent->filename);
394 DEBUG_printf(("1mimeLoadTypes: Loading \"%s\".", filename));
395 mime_load_types(mime, filename);
401 DEBUG_printf(("1mimeLoadTypes: Returning %p.", mime));
408 * 'mimeNew()' - Create a new, empty MIME database.
411 mime_t * /* O - MIME database */
414 return ((mime_t *)calloc(1, sizeof(mime_t)));
419 * 'mimeNextFilter()' - Get the next filter in the MIME database.
422 mime_filter_t * /* O - Filter or NULL */
423 mimeNextFilter(mime_t *mime) /* I - MIME database */
425 DEBUG_printf(("6mimeNextFilter(mime=%p)", mime));
429 DEBUG_puts("7mimeNextFilter: Returning NULL.");
434 mime_filter_t *next = (mime_filter_t *)cupsArrayNext(mime->filters);
437 DEBUG_printf(("7mimeNextFilter: Returning %p.", next));
444 * 'mimeNextType()' - Get the next type in the MIME database.
447 mime_type_t * /* O - Type or NULL */
448 mimeNextType(mime_t *mime) /* I - MIME database */
450 DEBUG_printf(("6mimeNextType(mime=%p)", mime));
454 DEBUG_puts("7mimeNextType: Returning NULL.");
459 mime_type_t *next = (mime_type_t *)cupsArrayNext(mime->types);
462 DEBUG_printf(("7mimeNextType: Returning %p.", next));
469 * 'mimeNumFilters()' - Get the number of filters in a MIME database.
473 mimeNumFilters(mime_t *mime) /* I - MIME database */
475 DEBUG_printf(("mimeNumFilters(mime=%p)", mime));
479 DEBUG_puts("1mimeNumFilters: Returning 0.");
484 DEBUG_printf(("1mimeNumFilters: Returning %d.",
485 cupsArrayCount(mime->filters)));
486 return (cupsArrayCount(mime->filters));
492 * 'mimeNumTypes()' - Get the number of types in a MIME database.
496 mimeNumTypes(mime_t *mime) /* I - MIME database */
498 DEBUG_printf(("mimeNumTypes(mime=%p)", mime));
502 DEBUG_puts("1mimeNumTypes: Returning 0.");
507 DEBUG_printf(("1mimeNumTypes: Returning %d.",
508 cupsArrayCount(mime->types)));
509 return (cupsArrayCount(mime->types));
515 * 'mimeSetErrorCallback()' - Set the callback for error messages.
519 mimeSetErrorCallback(
520 mime_t *mime, /* I - MIME database */
521 mime_error_cb_t cb, /* I - Callback function */
522 void *ctx) /* I - Context pointer for callback */
527 mime->error_ctx = ctx;
533 * 'mime_add_fcache()' - Add a filter to the filter cache.
536 static const char * /* O - Full path to filter or NULL */
538 cups_array_t *filtercache, /* I - Filter cache */
539 const char *name, /* I - Filter name */
540 const char *filterpath) /* I - Filter path */
542 _mime_fcache_t key, /* Search key */
543 *temp; /* New filter cache */
544 char path[1024]; /* Full path to filter */
547 DEBUG_printf(("2mime_add_fcache(filtercache=%p, name=\"%s\", "
548 "filterpath=\"%s\")", filtercache, name, filterpath));
550 key.name = (char *)name;
551 if ((temp = (_mime_fcache_t *)cupsArrayFind(filtercache, &key)) != NULL)
553 DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp->path));
557 if ((temp = calloc(1, sizeof(_mime_fcache_t))) == NULL)
559 DEBUG_puts("3mime_add_fcache: Returning NULL.");
563 temp->name = strdup(name);
565 if (cupsFileFind(name, filterpath, 1, path, sizeof(path)))
566 temp->path = strdup(path);
568 cupsArrayAdd(filtercache, temp);
570 DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp->path));
576 * 'mime_compare_fcache()' - Compare two filter cache entries.
579 static int /* O - Result of comparison */
580 mime_compare_fcache(_mime_fcache_t *a, /* I - First entry */
581 _mime_fcache_t *b) /* I - Second entry */
583 return (strcmp(a->name, b->name));
588 * 'mime_delete_fcache()' - Free all memory used by the filter cache.
593 cups_array_t *filtercache) /* I - Filter cache */
595 _mime_fcache_t *current; /* Current cache entry */
598 DEBUG_printf(("2mime_delete_fcache(filtercache=%p)", filtercache));
600 for (current = (_mime_fcache_t *)cupsArrayFirst(filtercache);
602 current = (_mime_fcache_t *)cupsArrayNext(filtercache))
612 cupsArrayDelete(filtercache);
617 * 'mime_delete_rules()' - Free all memory for the given rule tree.
621 mime_delete_rules(mime_magic_t *rules) /* I - Rules to free */
623 mime_magic_t *next; /* Next rule to free */
626 DEBUG_printf(("2mime_delete_rules(rules=%p)", rules));
629 * Free the rules list, descending recursively to free any child rules.
632 while (rules != NULL)
636 if (rules->child != NULL)
637 mime_delete_rules(rules->child);
639 if (rules->op == MIME_MAGIC_REGEX)
640 regfree(&(rules->value.rev));
649 * 'mime_load_convs()' - Load a xyz.convs file.
654 mime_t *mime, /* I - MIME database */
655 const char *filename, /* I - Convs file to load */
656 const char *filterpath, /* I - Path for filters */
657 cups_array_t *filtercache) /* I - Filter program cache */
659 cups_file_t *fp; /* Convs file */
660 char line[1024], /* Input line from file */
661 *lineptr, /* Current position in line */
662 super[MIME_MAX_SUPER], /* Super-type name */
663 type[MIME_MAX_TYPE], /* Type name */
664 *temp, /* Temporary pointer */
665 *filter; /* Filter program */
666 mime_type_t *temptype, /* MIME type looping var */
667 *dsttype; /* Destination MIME type */
668 int cost; /* Cost of filter */
671 DEBUG_printf(("2mime_load_convs(mime=%p, filename=\"%s\", filterpath=\"%s\", "
672 "filtercache=%p)", mime, filename, filterpath, filtercache));
675 * First try to open the file...
678 if ((fp = cupsFileOpen(filename, "r")) == NULL)
680 DEBUG_printf(("3mime_load_convs: Unable to open \"%s\": %s", filename,
682 _mimeError(mime, "Unable to open \"%s\": %s", filename, strerror(errno));
687 * Then read each line from the file, skipping any comments in the file...
690 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
693 * Skip blank lines and lines starting with a #...
696 if (!line[0] || line[0] == '#')
700 * Strip trailing whitespace...
703 for (lineptr = line + strlen(line) - 1;
704 lineptr >= line && isspace(*lineptr & 255);
709 * Extract the destination super-type and type names from the middle of
714 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\0')
717 while (*lineptr == ' ' || *lineptr == '\t')
722 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
723 (temp - super + 1) < MIME_MAX_SUPER)
724 *temp++ = (char)tolower(*lineptr++ & 255);
734 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
735 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
736 *temp++ = (char)tolower(*lineptr++ & 255);
740 if (*lineptr == '\0' || *lineptr == '\n')
743 if ((dsttype = mimeType(mime, super, type)) == NULL)
745 DEBUG_printf(("3mime_load_convs: Destination type %s/%s not found.",
751 * Then get the cost and filter program...
754 while (*lineptr == ' ' || *lineptr == '\t')
757 if (*lineptr < '0' || *lineptr > '9')
760 cost = atoi(lineptr);
762 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\0')
764 while (*lineptr == ' ' || *lineptr == '\t')
767 if (*lineptr == '\0' || *lineptr == '\n')
772 if (strcmp(filter, "-"))
775 * Verify that the filter exists and is executable...
778 if (!mime_add_fcache(filtercache, filter, filterpath))
780 DEBUG_printf(("mime_load_convs: Filter %s not found in %s.", filter,
782 _mimeError(mime, "Filter \"%s\" not found.", filter);
788 * Finally, get the source super-type and type names from the beginning of
789 * the line. We do it here so we can support wildcards...
795 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
796 (temp - super + 1) < MIME_MAX_SUPER)
797 *temp++ = (char)tolower(*lineptr++ & 255);
807 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
808 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
809 *temp++ = (char)tolower(*lineptr++ & 255);
813 if (!strcmp(super, "*") && !strcmp(type, "*"))
816 * Force * / * to be "application/octet-stream"...
819 strlcpy(super, "application", sizeof(super));
820 strlcpy(type, "octet-stream", sizeof(type));
824 * Add the filter to the MIME database, supporting wildcards as needed...
827 for (temptype = (mime_type_t *)cupsArrayFirst(mime->types);
829 temptype = (mime_type_t *)cupsArrayNext(mime->types))
830 if ((super[0] == '*' || !strcmp(temptype->super, super)) &&
831 (type[0] == '*' || !strcmp(temptype->type, type)))
832 mimeAddFilter(mime, temptype, dsttype, cost, filter);
840 * 'mime_load_types()' - Load a xyz.types file.
844 mime_load_types(mime_t *mime, /* I - MIME database */
845 const char *filename) /* I - Types file to load */
847 cups_file_t *fp; /* Types file */
848 size_t linelen; /* Length of line */
849 char line[32768], /* Input line from file */
850 *lineptr, /* Current position in line */
851 super[MIME_MAX_SUPER], /* Super-type name */
852 type[MIME_MAX_TYPE], /* Type name */
853 *temp; /* Temporary pointer */
854 mime_type_t *typeptr; /* New MIME type */
857 DEBUG_printf(("2mime_load_types(mime=%p, filename=\"%s\")", mime, filename));
860 * First try to open the file...
863 if ((fp = cupsFileOpen(filename, "r")) == NULL)
865 DEBUG_printf(("3mime_load_types: Unable to open \"%s\": %s", filename,
867 _mimeError(mime, "Unable to open \"%s\": %s", filename, strerror(errno));
872 * Then read each line from the file, skipping any comments in the file...
875 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
878 * Skip blank lines and lines starting with a #...
881 if (!line[0] || line[0] == '#')
885 * While the last character in the line is a backslash, continue on to the
886 * next line (and the next, etc.)
889 linelen = strlen(line);
891 while (line[linelen - 1] == '\\')
895 if (cupsFileGets(fp, line + linelen, sizeof(line) - linelen) == NULL)
896 line[linelen] = '\0';
898 linelen += strlen(line + linelen);
902 * Extract the super-type and type names from the beginning of the line.
908 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
909 (temp - super + 1) < MIME_MAX_SUPER)
910 *temp++ = (char)tolower(*lineptr++ & 255);
920 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
921 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
922 *temp++ = (char)tolower(*lineptr++ & 255);
927 * Add the type and rules to the MIME database...
930 typeptr = mimeAddType(mime, super, type);
931 mimeAddTypeRule(typeptr, lineptr);