]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
354aadbe | 2 | * "$Id: filter.c 10996 2013-05-29 11:51:34Z msweet $" |
ef416fc2 | 3 | * |
71e16022 | 4 | * File type conversion routines for CUPS. |
ef416fc2 | 5 | * |
22c9029b | 6 | * Copyright 2007-2011 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
22c9029b MS |
17 | * mimeAddFilter() - Add a filter to the current MIME database. |
18 | * mimeFilter() - Find the fastest way to convert from one type to | |
19 | * another. | |
20 | * mimeFilter2() - Find the fastest way to convert from one type to | |
21 | * another, including the file size. | |
22 | * mimeFilterLookup() - Lookup a filter. | |
23 | * mime_compare_filters() - Compare two filters. | |
24 | * mime_compare_srcs() - Compare two filter source types. | |
25 | * mime_find_filters() - Find the filters to convert from one type to | |
26 | * another. | |
ef416fc2 | 27 | */ |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
71e16022 MS |
33 | #include <cups/string-private.h> |
34 | #include <cups/debug-private.h> | |
ef416fc2 | 35 | #include "mime.h" |
36 | ||
37 | ||
bd7854cb | 38 | /* |
39 | * Local types... | |
40 | */ | |
41 | ||
42 | typedef struct _mime_typelist_s /**** List of source types ****/ | |
43 | { | |
44 | struct _mime_typelist_s *next; /* Next source type */ | |
45 | mime_type_t *src; /* Source type */ | |
46 | } _mime_typelist_t; | |
47 | ||
48 | ||
ef416fc2 | 49 | /* |
50 | * Local functions... | |
51 | */ | |
52 | ||
22c9029b MS |
53 | static int mime_compare_filters(mime_filter_t *, mime_filter_t *); |
54 | static int mime_compare_srcs(mime_filter_t *, mime_filter_t *); | |
55 | static cups_array_t *mime_find_filters(mime_t *mime, mime_type_t *src, | |
07ed0e9a MS |
56 | size_t srcsize, mime_type_t *dst, |
57 | int *cost, _mime_typelist_t *visited); | |
ef416fc2 | 58 | |
59 | ||
60 | /* | |
61 | * 'mimeAddFilter()' - Add a filter to the current MIME database. | |
62 | */ | |
63 | ||
64 | mime_filter_t * /* O - New filter */ | |
65 | mimeAddFilter(mime_t *mime, /* I - MIME database */ | |
66 | mime_type_t *src, /* I - Source type */ | |
67 | mime_type_t *dst, /* I - Destination type */ | |
68 | int cost, /* I - Relative time/resource cost */ | |
69 | const char *filter) /* I - Filter program to run */ | |
70 | { | |
71 | mime_filter_t *temp; /* New filter */ | |
72 | ||
73 | ||
22c9029b MS |
74 | DEBUG_printf(("mimeAddFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), cost=%d, " |
75 | "filter=\"%s\")", mime, | |
76 | src, src ? src->super : "???", src ? src->type : "???", | |
77 | dst, dst ? dst->super : "???", dst ? dst->type : "???", | |
78 | cost, filter)); | |
79 | ||
ef416fc2 | 80 | /* |
81 | * Range-check the input... | |
82 | */ | |
83 | ||
fa73b229 | 84 | if (!mime || !src || !dst || !filter) |
22c9029b MS |
85 | { |
86 | DEBUG_puts("1mimeAddFilter: Returning NULL."); | |
ef416fc2 | 87 | return (NULL); |
22c9029b | 88 | } |
ef416fc2 | 89 | |
90 | /* | |
91 | * See if we already have an existing filter for the given source and | |
92 | * destination... | |
93 | */ | |
94 | ||
f7deaa1a | 95 | if ((temp = mimeFilterLookup(mime, src, dst)) != NULL) |
ef416fc2 | 96 | { |
97 | /* | |
98 | * Yup, does the existing filter have a higher cost? If so, copy the | |
99 | * filter and cost to the existing filter entry and return it... | |
100 | */ | |
101 | ||
102 | if (temp->cost > cost) | |
103 | { | |
22c9029b MS |
104 | DEBUG_printf(("1mimeAddFilter: Replacing filter \"%s\", cost %d.", |
105 | temp->filter, temp->cost)); | |
ef416fc2 | 106 | temp->cost = cost; |
107 | strlcpy(temp->filter, filter, sizeof(temp->filter)); | |
108 | } | |
109 | } | |
110 | else | |
111 | { | |
112 | /* | |
113 | * Nope, add a new one... | |
114 | */ | |
115 | ||
fa73b229 | 116 | if (!mime->filters) |
22c9029b | 117 | mime->filters = cupsArrayNew((cups_array_func_t)mime_compare_filters, NULL); |
ef416fc2 | 118 | |
fa73b229 | 119 | if (!mime->filters) |
ef416fc2 | 120 | return (NULL); |
121 | ||
fa73b229 | 122 | if ((temp = calloc(1, sizeof(mime_filter_t))) == NULL) |
123 | return (NULL); | |
ef416fc2 | 124 | |
125 | /* | |
126 | * Copy the information over and sort if necessary... | |
127 | */ | |
128 | ||
129 | temp->src = src; | |
130 | temp->dst = dst; | |
131 | temp->cost = cost; | |
132 | strlcpy(temp->filter, filter, sizeof(temp->filter)); | |
133 | ||
22c9029b | 134 | DEBUG_puts("1mimeAddFilter: Adding new filter."); |
fa73b229 | 135 | cupsArrayAdd(mime->filters, temp); |
22c9029b | 136 | cupsArrayAdd(mime->srcs, temp); |
ef416fc2 | 137 | } |
138 | ||
139 | /* | |
140 | * Return the new/updated filter... | |
141 | */ | |
142 | ||
22c9029b MS |
143 | DEBUG_printf(("1mimeAddFilter: Returning %p.", temp)); |
144 | ||
ef416fc2 | 145 | return (temp); |
146 | } | |
147 | ||
148 | ||
149 | /* | |
150 | * 'mimeFilter()' - Find the fastest way to convert from one type to another. | |
151 | */ | |
152 | ||
fa73b229 | 153 | cups_array_t * /* O - Array of filters to run */ |
ef416fc2 | 154 | mimeFilter(mime_t *mime, /* I - MIME database */ |
155 | mime_type_t *src, /* I - Source file type */ | |
156 | mime_type_t *dst, /* I - Destination file type */ | |
bd7854cb | 157 | int *cost) /* O - Cost of filters */ |
ef416fc2 | 158 | { |
fa73b229 | 159 | DEBUG_printf(("mimeFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), " |
22c9029b MS |
160 | "cost=%p(%d))", mime, |
161 | src, src ? src->super : "???", src ? src->type : "???", | |
162 | dst, dst ? dst->super : "???", dst ? dst->type : "???", | |
bd7854cb | 163 | cost, cost ? *cost : 0)); |
164 | ||
07ed0e9a MS |
165 | return (mimeFilter2(mime, src, 0, dst, cost)); |
166 | } | |
167 | ||
168 | ||
169 | /* | |
170 | * 'mimeFilter2()' - Find the fastest way to convert from one type to another, | |
171 | * including file size. | |
172 | */ | |
173 | ||
174 | cups_array_t * /* O - Array of filters to run */ | |
175 | mimeFilter2(mime_t *mime, /* I - MIME database */ | |
176 | mime_type_t *src, /* I - Source file type */ | |
177 | size_t srcsize, /* I - Size of source file */ | |
178 | mime_type_t *dst, /* I - Destination file type */ | |
179 | int *cost) /* O - Cost of filters */ | |
180 | { | |
22c9029b MS |
181 | cups_array_t *filters; /* Array of filters to run */ |
182 | ||
183 | ||
07ed0e9a MS |
184 | /* |
185 | * Range-check the input... | |
186 | */ | |
187 | ||
188 | DEBUG_printf(("mimeFilter2(mime=%p, src=%p(%s/%s), srcsize=" CUPS_LLFMT | |
22c9029b MS |
189 | ", dst=%p(%s/%s), cost=%p(%d))", mime, |
190 | src, src ? src->super : "???", src ? src->type : "???", | |
191 | CUPS_LLCAST srcsize, | |
192 | dst, dst ? dst->super : "???", dst ? dst->type : "???", | |
193 | cost, cost ? *cost : 0)); | |
ef416fc2 | 194 | |
fa73b229 | 195 | if (cost) |
196 | *cost = 0; | |
ef416fc2 | 197 | |
bd7854cb | 198 | if (!mime || !src || !dst) |
fa73b229 | 199 | return (NULL); |
ef416fc2 | 200 | |
a74454a7 | 201 | /* |
202 | * (Re)build the source lookup array as needed... | |
203 | */ | |
204 | ||
205 | if (!mime->srcs) | |
206 | { | |
207 | mime_filter_t *current; /* Current filter */ | |
208 | ||
22c9029b | 209 | mime->srcs = cupsArrayNew((cups_array_func_t)mime_compare_srcs, NULL); |
a74454a7 | 210 | |
211 | for (current = mimeFirstFilter(mime); | |
212 | current; | |
213 | current = mimeNextFilter(mime)) | |
214 | cupsArrayAdd(mime->srcs, current); | |
215 | } | |
216 | ||
bd7854cb | 217 | /* |
218 | * Find the filters... | |
219 | */ | |
220 | ||
22c9029b MS |
221 | filters = mime_find_filters(mime, src, srcsize, dst, cost, NULL); |
222 | ||
223 | DEBUG_printf(("1mimeFilter2: Returning %d filter(s), cost %d:", | |
224 | cupsArrayCount(filters), cost ? *cost : -1)); | |
225 | #ifdef DEBUG | |
226 | { | |
227 | mime_filter_t *filter; /* Current filter */ | |
228 | ||
229 | for (filter = (mime_filter_t *)cupsArrayFirst(filters); | |
230 | filter; | |
231 | filter = (mime_filter_t *)cupsArrayNext(filters)) | |
232 | DEBUG_printf(("1mimeFilter2: %s/%s %s/%s %d %s", filter->src->super, | |
233 | filter->src->type, filter->dst->super, filter->dst->type, | |
234 | filter->cost, filter->filter)); | |
235 | } | |
236 | #endif /* DEBUG */ | |
237 | ||
238 | return (filters); | |
bd7854cb | 239 | } |
240 | ||
241 | ||
f7deaa1a | 242 | /* |
07ed0e9a | 243 | * 'mimeFilterLookup()' - Lookup a filter. |
f7deaa1a | 244 | */ |
245 | ||
246 | mime_filter_t * /* O - Filter for src->dst */ | |
247 | mimeFilterLookup(mime_t *mime, /* I - MIME database */ | |
248 | mime_type_t *src, /* I - Source type */ | |
249 | mime_type_t *dst) /* I - Destination type */ | |
250 | { | |
22c9029b MS |
251 | mime_filter_t key, /* Key record for filter search */ |
252 | *filter; /* Matching filter */ | |
253 | ||
f7deaa1a | 254 | |
22c9029b MS |
255 | DEBUG_printf(("2mimeFilterLookup(mime=%p, src=%p(%s/%s), dst=%p(%s/%s))", mime, |
256 | src, src ? src->super : "???", src ? src->type : "???", | |
257 | dst, dst ? dst->super : "???", dst ? dst->type : "???")); | |
f7deaa1a | 258 | |
259 | key.src = src; | |
260 | key.dst = dst; | |
261 | ||
22c9029b MS |
262 | filter = (mime_filter_t *)cupsArrayFind(mime->filters, &key); |
263 | DEBUG_printf(("3mimeFilterLookup: Returning %p(%s).", filter, | |
264 | filter ? filter->filter : "???")); | |
265 | return (filter); | |
f7deaa1a | 266 | } |
267 | ||
268 | ||
bd7854cb | 269 | /* |
22c9029b | 270 | * 'mime_compare_filters()' - Compare two filters. |
bd7854cb | 271 | */ |
272 | ||
273 | static int /* O - Comparison result */ | |
22c9029b MS |
274 | mime_compare_filters(mime_filter_t *f0, /* I - First filter */ |
275 | mime_filter_t *f1) /* I - Second filter */ | |
bd7854cb | 276 | { |
277 | int i; /* Result of comparison */ | |
278 | ||
279 | ||
280 | if ((i = strcmp(f0->src->super, f1->src->super)) == 0) | |
281 | if ((i = strcmp(f0->src->type, f1->src->type)) == 0) | |
282 | if ((i = strcmp(f0->dst->super, f1->dst->super)) == 0) | |
283 | i = strcmp(f0->dst->type, f1->dst->type); | |
284 | ||
285 | return (i); | |
286 | } | |
287 | ||
288 | ||
a74454a7 | 289 | /* |
22c9029b | 290 | * 'mime_compare_srcs()' - Compare two filter source types. |
a74454a7 | 291 | */ |
292 | ||
293 | static int /* O - Comparison result */ | |
22c9029b MS |
294 | mime_compare_srcs(mime_filter_t *f0, /* I - First filter */ |
295 | mime_filter_t *f1) /* I - Second filter */ | |
a74454a7 | 296 | { |
297 | int i; /* Result of comparison */ | |
298 | ||
299 | ||
300 | if ((i = strcmp(f0->src->super, f1->src->super)) == 0) | |
301 | i = strcmp(f0->src->type, f1->src->type); | |
302 | ||
303 | return (i); | |
304 | } | |
305 | ||
306 | ||
bd7854cb | 307 | /* |
22c9029b | 308 | * 'mime_find_filters()' - Find the filters to convert from one type to another. |
bd7854cb | 309 | */ |
310 | ||
b423cd4c | 311 | static cups_array_t * /* O - Array of filters to run */ |
22c9029b MS |
312 | mime_find_filters( |
313 | mime_t *mime, /* I - MIME database */ | |
314 | mime_type_t *src, /* I - Source file type */ | |
315 | size_t srcsize, /* I - Size of source file */ | |
316 | mime_type_t *dst, /* I - Destination file type */ | |
317 | int *cost, /* O - Cost of filters */ | |
318 | _mime_typelist_t *list) /* I - Source types we've used */ | |
bd7854cb | 319 | { |
320 | int tempcost, /* Temporary cost */ | |
321 | mincost; /* Current minimum */ | |
322 | cups_array_t *temp, /* Temporary filter */ | |
323 | *mintemp; /* Current minimum */ | |
a74454a7 | 324 | mime_filter_t *current, /* Current filter */ |
325 | srckey; /* Source type key */ | |
bd7854cb | 326 | _mime_typelist_t listnode, /* New list node */ |
327 | *listptr; /* Pointer in list */ | |
328 | ||
329 | ||
22c9029b | 330 | DEBUG_printf(("2mime_find_filters(mime=%p, src=%p(%s/%s), srcsize=" CUPS_LLFMT |
07ed0e9a MS |
331 | ", dst=%p(%s/%s), cost=%p, list=%p)", mime, src, src->super, |
332 | src->type, CUPS_LLCAST srcsize, dst, dst->super, dst->type, | |
333 | cost, list)); | |
bd7854cb | 334 | |
ef416fc2 | 335 | /* |
336 | * See if there is a filter that can convert the files directly... | |
337 | */ | |
338 | ||
07ed0e9a MS |
339 | if ((current = mimeFilterLookup(mime, src, dst)) != NULL && |
340 | (current->maxsize == 0 || srcsize <= current->maxsize)) | |
ef416fc2 | 341 | { |
342 | /* | |
343 | * Got a direct filter! | |
344 | */ | |
345 | ||
22c9029b | 346 | DEBUG_puts("3mime_find_filters: Direct filter found."); |
bd7854cb | 347 | |
fa73b229 | 348 | if ((mintemp = cupsArrayNew(NULL, NULL)) == NULL) |
22c9029b MS |
349 | { |
350 | DEBUG_puts("3mime_find_filters: Returning NULL (out of memory)."); | |
ef416fc2 | 351 | return (NULL); |
22c9029b | 352 | } |
ef416fc2 | 353 | |
fa73b229 | 354 | cupsArrayAdd(mintemp, current); |
355 | ||
356 | mincost = current->cost; | |
ef416fc2 | 357 | |
a74454a7 | 358 | if (!cost) |
22c9029b MS |
359 | { |
360 | DEBUG_printf(("3mime_find_filters: Returning 1 filter, cost %d:", | |
361 | mincost)); | |
362 | DEBUG_printf(("3mime_find_filters: %s/%s %s/%s %d %s", | |
363 | current->src->super, current->src->type, | |
364 | current->dst->super, current->dst->type, | |
365 | current->cost, current->filter)); | |
a74454a7 | 366 | return (mintemp); |
22c9029b | 367 | } |
ef416fc2 | 368 | } |
369 | else | |
370 | { | |
371 | /* | |
372 | * No direct filter... | |
373 | */ | |
374 | ||
fa73b229 | 375 | mintemp = NULL; |
376 | mincost = 9999999; | |
ef416fc2 | 377 | } |
378 | ||
bd7854cb | 379 | /* |
380 | * Initialize this node in the type list... | |
381 | */ | |
382 | ||
383 | listnode.next = list; | |
384 | ||
ef416fc2 | 385 | /* |
386 | * OK, now look for filters from the source type to any other type... | |
387 | */ | |
388 | ||
a74454a7 | 389 | srckey.src = src; |
bd7854cb | 390 | |
a74454a7 | 391 | for (current = (mime_filter_t *)cupsArrayFind(mime->srcs, &srckey); |
392 | current && current->src == src; | |
393 | current = (mime_filter_t *)cupsArrayNext(mime->srcs)) | |
394 | { | |
395 | /* | |
396 | * See if we have already tried the destination type as a source | |
397 | * type (this avoids extra filter looping...) | |
398 | */ | |
bd7854cb | 399 | |
a74454a7 | 400 | mime_type_t *current_dst; /* Current destination type */ |
bd7854cb | 401 | |
07ed0e9a MS |
402 | if (current->maxsize > 0 && srcsize > current->maxsize) |
403 | continue; | |
ef416fc2 | 404 | |
a74454a7 | 405 | for (listptr = list, current_dst = current->dst; |
406 | listptr; | |
407 | listptr = listptr->next) | |
408 | if (current_dst == listptr->src) | |
409 | break; | |
410 | ||
411 | if (listptr) | |
412 | continue; | |
bd7854cb | 413 | |
a74454a7 | 414 | /* |
415 | * See if we have any filters that can convert from the destination type | |
416 | * of this filter to the final type... | |
417 | */ | |
418 | ||
419 | listnode.src = current->src; | |
fa73b229 | 420 | |
a74454a7 | 421 | cupsArraySave(mime->srcs); |
22c9029b MS |
422 | temp = mime_find_filters(mime, current->dst, srcsize, dst, &tempcost, |
423 | &listnode); | |
a74454a7 | 424 | cupsArrayRestore(mime->srcs); |
425 | ||
426 | if (!temp) | |
427 | continue; | |
428 | ||
429 | if (!cost) | |
22c9029b MS |
430 | { |
431 | DEBUG_printf(("3mime_find_filters: Returning %d filter(s), cost %d:", | |
432 | cupsArrayCount(temp), tempcost)); | |
433 | ||
434 | #ifdef DEBUG | |
435 | for (current = (mime_filter_t *)cupsArrayFirst(temp); | |
436 | current; | |
437 | current = (mime_filter_t *)cupsArrayNext(temp)) | |
438 | DEBUG_printf(("3mime_find_filters: %s/%s %s/%s %d %s", | |
439 | current->src->super, current->src->type, | |
440 | current->dst->super, current->dst->type, | |
441 | current->cost, current->filter)); | |
442 | #endif /* DEBUG */ | |
443 | ||
a74454a7 | 444 | return (temp); |
22c9029b | 445 | } |
a74454a7 | 446 | |
447 | /* | |
448 | * Found a match; see if this one is less costly than the last (if | |
449 | * any...) | |
450 | */ | |
451 | ||
ed486911 | 452 | tempcost += current->cost; |
453 | ||
a74454a7 | 454 | if (tempcost < mincost) |
455 | { | |
456 | cupsArrayDelete(mintemp); | |
ef416fc2 | 457 | |
458 | /* | |
a74454a7 | 459 | * Hey, we got a match! Add the current filter to the beginning of the |
460 | * filter list... | |
ef416fc2 | 461 | */ |
462 | ||
a74454a7 | 463 | mintemp = temp; |
ed486911 | 464 | mincost = tempcost; |
a74454a7 | 465 | cupsArrayInsert(mintemp, current); |
ef416fc2 | 466 | } |
a74454a7 | 467 | else |
468 | cupsArrayDelete(temp); | |
469 | } | |
ef416fc2 | 470 | |
fa73b229 | 471 | if (mintemp) |
ef416fc2 | 472 | { |
473 | /* | |
474 | * Hey, we got a match! | |
475 | */ | |
476 | ||
22c9029b MS |
477 | DEBUG_printf(("3mime_find_filters: Returning %d filter(s), cost %d:", |
478 | cupsArrayCount(mintemp), mincost)); | |
ae71f5de | 479 | |
22c9029b | 480 | #ifdef DEBUG |
fa73b229 | 481 | for (current = (mime_filter_t *)cupsArrayFirst(mintemp); |
482 | current; | |
483 | current = (mime_filter_t *)cupsArrayNext(mintemp)) | |
22c9029b MS |
484 | DEBUG_printf(("3mime_find_filters: %s/%s %s/%s %d %s", |
485 | current->src->super, current->src->type, | |
486 | current->dst->super, current->dst->type, | |
487 | current->cost, current->filter)); | |
ef416fc2 | 488 | #endif /* DEBUG */ |
489 | ||
bd7854cb | 490 | if (cost) |
491 | *cost = mincost; | |
fa73b229 | 492 | |
ef416fc2 | 493 | return (mintemp); |
494 | } | |
495 | ||
22c9029b | 496 | DEBUG_puts("3mime_find_filters: Returning NULL (no matches)."); |
ef416fc2 | 497 | |
498 | return (NULL); | |
499 | } | |
500 | ||
501 | ||
ef416fc2 | 502 | /* |
354aadbe | 503 | * End of "$Id: filter.c 10996 2013-05-29 11:51:34Z msweet $". |
ef416fc2 | 504 | */ |