blob: b9f9a9446f6d2c839b9c56ab6904d294ac0faad5 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% DDDD EEEEE L EEEEE GGGG AAA TTTTT EEEEE %
6% D D E L E G A A T E %
7% D D EEE L EEE G GG AAAAA T EEE %
8% D D E L E G G A A T E %
9% DDDD EEEEE LLLLL EEEEE GGG A A T EEEEE %
10% %
11% %
12% MagickCore Methods to Read/Write/Invoke Delegates %
13% %
14% Software Design %
15% John Cristy %
16% October 1998 %
17% %
18% %
19% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% http://www.imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35% The Delegates methods associate a set of commands with a particular
36% image format. ImageMagick uses delegates for formats it does not handle
37% directly.
38%
39% Thanks to Bob Friesenhahn for the initial inspiration and design of the
40% delegates methods.
41%
42%
43*/
44
45/*
46 Include declarations.
47*/
48#include "magick/studio.h"
49#include "magick/property.h"
50#include "magick/blob.h"
51#include "magick/client.h"
52#include "magick/configure.h"
53#include "magick/constitute.h"
54#include "magick/delegate.h"
55#include "magick/exception.h"
56#include "magick/exception-private.h"
57#include "magick/hashmap.h"
58#include "magick/list.h"
59#include "magick/memory_.h"
60#include "magick/policy.h"
61#include "magick/resource_.h"
62#include "magick/semaphore.h"
63#include "magick/string_.h"
64#include "magick/token.h"
65#include "magick/utility.h"
66#include "magick/xml-tree.h"
67
68/*
69 Define declarations.
70*/
71#define DelegateFilename "delegates.xml"
72
73/*
74 Declare delegate map.
75*/
76static const char
77 *DelegateMap = (const char *)
78 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
79 "<delegatemap>"
80 " <delegate decode=\"autotrace\" stealth=\"True\" command=\"&quot;autotrace&quot; -output-format svg -output-file &quot;%o&quot; &quot;%i&quot;\"/>"
81 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\"&quot;mplayer&quot; &quot;%i&quot; -really-quiet -ao null -vo png:z=3\"/>"
82 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; http://www.imagemagick.org/\"/>"
83 " <delegate decode=\"cgm\" thread-support=\"False\" command=\"&quot;ralcgm&quot; -d ps -oC &lt; &quot;%i&quot; &gt; &quot;%o&quot; 2&gt; &quot;%u&quot;\"/>"
84 " <delegate decode=\"dng:decode\" command=\"&quot;/usr/bin/ufraw-batch&quot; --silent --wb=camera --black-point=auto --exposure=auto --create-id=also --out-type=ppm16 &quot;--output=%u.pnm&quot; &quot;%i&quot;\"/>"
85 " <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
86 " <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
87 " <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
88 " <delegate decode=\"fig\" command=\"&quot;fig2dev&quot; -L ps &quot;%i&quot; &quot;%o&quot;\"/>"
89 " <delegate decode=\"gplt\" command=\"&quot;echo&quot; &quot;set size 1.25,0.62 set terminal postscript portrait color solid; set output &quot;%o&quot;; load &quot;%i&quot;&quot; &gt; &quot;%u&quot;;&quot;gnuplot&quot; &quot;%u&quot;\"/>"
90 " <delegate decode=\"hdr\" command=\"&quot;ra_pfm&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
91 " <delegate decode=\"hpg\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot; mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
92 " <delegate decode=\"hpgl\" command=\"if [ -e hp2xx -o -e /usr/bin/hp2xx ]; then hp2xx -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot; mv -f `basename &quot;%o&quot;` &quot;%o else echo &quot;You need to install hp2xx to use HPGL files with ImageMagick.&quot; exit 1 fi\"/>"
93 " <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
94 " <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
95 " <delegate decode=\"https\" command=\"&quot;wget&quot; -q -O &quot;%o&quot; &quot;https:%M&quot;\"/>"
96 " <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
97 " <delegate decode=\"man\" command=\"&quot;groff&quot; -man -Tps &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
98 " <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; --i &quot;%i&quot; -vcodec pam -an -f rawvideo -y &quot;%u0.pam&quot; 2;&gt; &quot;%Z&quot;\"/>"
99 " <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; &quot;%M%%d.jpg&quot; &quot;%u.%m&quot; 2;&gt; &quot;%Z&quot;\"/>"
100 " <delegate decode=\"pcl:color\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=ppmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
101 " <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
102 " <delegate decode=\"pcl:mono\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
103 " <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
104 " <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
105 " <delegate decode=\"pic\" command=\"&quot;ra_pfm&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
106 " <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\"&quot;ppmtoilbm&quot; -24if &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
107 " <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\"&quot;gimp&quot; &quot;%i&quot;\"/>"
108 " <delegate decode=\"pov\" command=\"&quot;povray&quot; &quot;+i&quot;%i&quot;&quot; -D0 +o&quot;%o&quot; +fn%q +w%w +h%h +a -q9 -kfi&quot;%s&quot; -kff&quot;%n&quot; &quot;convert&quot; -concatenate &quot;%o*.png&quot; &quot;%o&quot;\"/>"
109 " <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
110 " <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
111 " <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr &quot;%i&quot;\"/>"
112 " <delegate decode=\"ps:alpha\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
113 " <delegate decode=\"ps:bbox\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=bbox&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
114 " <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pam&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
115 " <delegate decode=\"ps:color\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
116 " <delegate decode=\"ps:mono\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
117 " <delegate decode=\"rad\" command=\"&quot;ra_pfm&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
118 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\"&quot;rawtorle&quot; -o &quot;%o&quot; -v &quot;%i&quot;\"/>"
119 " <delegate decode=\"scan\" command=\"&quot;scanimage&quot; -d &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
120 " <delegate encode=\"show\" stealth=\"True\" spawn=\"True\" command=\"&quot;/usr/local/bin/display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
121 " <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
122 " <delegate decode=\"svg\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
123 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\"&quot;enscript&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
124 " <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\"&quot;/usr/local/bin/display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
125 " <delegate decode=\"wmf\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
126 "</delegatemap>";
127
128/*
129 Global declaractions.
130*/
131static LinkedListInfo
132 *delegate_list = (LinkedListInfo *) NULL;
133
134static SemaphoreInfo
135 *delegate_semaphore = (SemaphoreInfo *) NULL;
136
137static volatile MagickBooleanType
138 instantiate_delegate = MagickFalse;
139
140/*
141 Forward declaractions.
142*/
143static MagickBooleanType
144 InitializeDelegateList(ExceptionInfo *),
145 LoadDelegateLists(const char *,ExceptionInfo *);
146
147/*
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149% %
150% %
151% %
152% D e s t r o y D e l e g a t e L i s t %
153% %
154% %
155% %
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157%
158% DestroyDelegateList() deallocates memory associated with the delegates list.
159%
160% The format of the DestroyDelegateList method is:
161%
162% DestroyDelegateList(void)
163%
164*/
165
166static void *DestroyDelegate(void *delegate_info)
167{
168 register DelegateInfo
169 *p;
170
171 p=(DelegateInfo *) delegate_info;
172 if (p->path != (char *) NULL)
173 p->path=DestroyString(p->path);
174 if (p->decode != (char *) NULL)
175 p->decode=DestroyString(p->decode);
176 if (p->encode != (char *) NULL)
177 p->encode=DestroyString(p->encode);
178 if (p->commands != (char *) NULL)
179 p->commands=DestroyString(p->commands);
180 p=(DelegateInfo *) RelinquishMagickMemory(p);
181 return((void *) NULL);
182}
183
184
185MagickExport void DestroyDelegateList(void)
186{
187 AcquireSemaphoreInfo(&delegate_semaphore);
188 if (delegate_list != (LinkedListInfo *) NULL)
189 delegate_list=DestroyLinkedList(delegate_list,DestroyDelegate);
190 instantiate_delegate=MagickFalse;
191 RelinquishSemaphoreInfo(delegate_semaphore);
192 DestroySemaphoreInfo(&delegate_semaphore);
193}
194
195/*
196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197% %
198% %
199% %
200% G e t D e l e g a t e C o m m a n d %
201% %
202% %
203% %
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205%
206% GetDelegateCommand() replaces any embedded formatting characters with the
207% appropriate image attribute and returns the resulting command.
208%
209% The format of the GetDelegateCommand method is:
210%
211% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
212% const char *decode,const char *encode,ExceptionInfo *exception)
213%
214% A description of each parameter follows:
215%
216% o command: Method GetDelegateCommand returns the command associated
217% with specified delegate tag.
218%
219% o image_info: the image info.
220%
221% o image: the image.
222%
223% o decode: Specifies the decode delegate we are searching for as a
224% character string.
225%
226% o encode: Specifies the encode delegate we are searching for as a
227% character string.
228%
229% o exception: return any errors or warnings in this structure.
230%
231*/
232MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
233 const char *decode,const char *encode,ExceptionInfo *exception)
234{
235 char
236 *command,
237 **commands;
238
239 const DelegateInfo
240 *delegate_info;
241
242 register long
243 i;
244
245 assert(image_info != (ImageInfo *) NULL);
246 assert(image_info->signature == MagickSignature);
247 assert(image != (Image *) NULL);
248 assert(image->signature == MagickSignature);
249 if (image->debug != MagickFalse)
250 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
251 delegate_info=GetDelegateInfo(decode,encode,exception);
252 if (delegate_info == (const DelegateInfo *) NULL)
253 {
254 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
255 "NoTagFound","`%s'",decode ? decode : encode);
256 return((char *) NULL);
257 }
258 commands=StringToList(delegate_info->commands);
259 if (commands == (char **) NULL)
260 {
261 (void) ThrowMagickException(exception,GetMagickModule(),
262 ResourceLimitError,"MemoryAllocationFailed","`%s'",
263 decode ? decode : encode);
264 return((char *) NULL);
265 }
266 command=InterpretImageProperties(image_info,image,commands[0]);
267 if (command == (char *) NULL)
268 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
269 "MemoryAllocationFailed","`%s'",commands[0]);
270 /*
271 Relinquish resources.
272 */
273 for (i=0; commands[i] != (char *) NULL; i++)
274 commands[i]=DestroyString(commands[i]);
275 commands=(char **) RelinquishMagickMemory(commands);
276 return(command);
277}
278
279/*
280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281% %
282% %
283% %
284% G e t D e l e g a t e C o m m a n d s %
285% %
286% %
287% %
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289%
290% GetDelegateCommands() returns the commands associated with a delegate.
291%
292% The format of the GetDelegateCommands method is:
293%
294% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
295%
296% A description of each parameter follows:
297%
298% o delegate_info: The delegate info.
299%
300*/
301MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
302{
303 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
304 assert(delegate_info != (DelegateInfo *) NULL);
305 assert(delegate_info->signature == MagickSignature);
306 return(delegate_info->commands);
307}
308
309/*
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311% %
312% %
313% %
314% G e t D e l e g a t e I n f o %
315% %
316% %
317% %
318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319%
320% GetDelegateInfo() returns any delegates associated with the specified tag.
321%
322% The format of the GetDelegateInfo method is:
323%
324% const DelegateInfo *GetDelegateInfo(const char *decode,
325% const char *encode,ExceptionInfo *exception)
326%
327% A description of each parameter follows:
328%
329% o decode: Specifies the decode delegate we are searching for as a
330% character string.
331%
332% o encode: Specifies the encode delegate we are searching for as a
333% character string.
334%
335% o exception: return any errors or warnings in this structure.
336%
337*/
338MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
339 const char *encode,ExceptionInfo *exception)
340{
341 register const DelegateInfo
342 *p;
343
344 assert(exception != (ExceptionInfo *) NULL);
345 if ((delegate_list == (LinkedListInfo *) NULL) ||
346 (instantiate_delegate == MagickFalse))
347 if (InitializeDelegateList(exception) == MagickFalse)
348 return((const DelegateInfo *) NULL);
349 if ((delegate_list == (LinkedListInfo *) NULL) ||
350 (IsLinkedListEmpty(delegate_list) != MagickFalse))
351 return((const DelegateInfo *) NULL);
352 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
353 return((const DelegateInfo *) GetValueFromLinkedList(delegate_list,0));
354 /*
355 Search for named delegate.
356 */
357 AcquireSemaphoreInfo(&delegate_semaphore);
358 ResetLinkedListIterator(delegate_list);
359 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
360 while (p != (const DelegateInfo *) NULL)
361 {
362 if (p->mode > 0)
363 {
364 if (LocaleCompare(p->decode,decode) == 0)
365 break;
366 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
367 continue;
368 }
369 if (p->mode < 0)
370 {
371 if (LocaleCompare(p->encode,encode) == 0)
372 break;
373 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
374 continue;
375 }
376 if (LocaleCompare(decode,p->decode) == 0)
377 if (LocaleCompare(encode,p->encode) == 0)
378 break;
379 if (LocaleCompare(decode,"*") == 0)
380 if (LocaleCompare(encode,p->encode) == 0)
381 break;
382 if (LocaleCompare(decode,p->decode) == 0)
383 if (LocaleCompare(encode,"*") == 0)
384 break;
385 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
386 }
387 if (p != (const DelegateInfo *) NULL)
388 (void) InsertValueInLinkedList(delegate_list,0,
389 RemoveElementByValueFromLinkedList(delegate_list,p));
390 RelinquishSemaphoreInfo(delegate_semaphore);
391 return(p);
392}
393
394/*
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396% %
397% %
398% %
399% G e t D e l e g a t e I n f o L i s t %
400% %
401% %
402% %
403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404%
405% GetDelegateInfoList() returns any delegates that match the specified pattern.
406%
407% The delegate of the GetDelegateInfoList function is:
408%
409% const DelegateInfo **GetDelegateInfoList(const char *pattern,
410% unsigned long *number_delegates,ExceptionInfo *exception)
411%
412% A description of each parameter follows:
413%
414% o pattern: Specifies a pointer to a text string containing a pattern.
415%
416% o number_delegates: This integer returns the number of delegates in the
417% list.
418%
419% o exception: return any errors or warnings in this structure.
420%
421*/
422
423#if defined(__cplusplus) || defined(c_plusplus)
424extern "C" {
425#endif
426
427static int DelegateInfoCompare(const void *x,const void *y)
428{
429 const DelegateInfo
430 **p,
431 **q;
432
433 p=(const DelegateInfo **) x,
434 q=(const DelegateInfo **) y;
435 if (LocaleCompare((*p)->path,(*q)->path) == 0)
436 {
437 if ((*p)->decode == (char *) NULL)
438 if (((*p)->encode != (char *) NULL) &&
439 ((*q)->encode != (char *) NULL))
440 return(strcmp((*p)->encode,(*q)->encode));
441 if (((*p)->decode != (char *) NULL) &&
442 ((*q)->decode != (char *) NULL))
443 return(strcmp((*p)->decode,(*q)->decode));
444 }
445 return(LocaleCompare((*p)->path,(*q)->path));
446}
447
448#if defined(__cplusplus) || defined(c_plusplus)
449}
450#endif
451
452MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
453 unsigned long *number_delegates,ExceptionInfo *exception)
454{
455 const DelegateInfo
456 **delegates;
457
458 register const DelegateInfo
459 *p;
460
461 register long
462 i;
463
464 /*
465 Allocate delegate list.
466 */
467 assert(pattern != (char *) NULL);
468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
469 assert(number_delegates != (unsigned long *) NULL);
470 *number_delegates=0;
471 p=GetDelegateInfo("*","*",exception);
472 if (p == (const DelegateInfo *) NULL)
473 return((const DelegateInfo **) NULL);
474 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
475 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
476 if (delegates == (const DelegateInfo **) NULL)
477 return((const DelegateInfo **) NULL);
478 /*
479 Generate delegate list.
480 */
481 AcquireSemaphoreInfo(&delegate_semaphore);
482 ResetLinkedListIterator(delegate_list);
483 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
484 for (i=0; p != (const DelegateInfo *) NULL; )
485 {
486 if ((p->stealth == MagickFalse) &&
487 ((GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse) ||
488 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse)))
489 delegates[i++]=p;
490 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
491 }
492 RelinquishSemaphoreInfo(delegate_semaphore);
493 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
494 delegates[i]=(DelegateInfo *) NULL;
495 *number_delegates=(unsigned long) i;
496 return(delegates);
497}
498
499/*
500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501% %
502% %
503% %
504% G e t D e l e g a t e L i s t %
505% %
506% %
507% %
508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509%
510% GetDelegateList() returns any image format delegates that match the
511% specified pattern.
512%
513% The format of the GetDelegateList function is:
514%
515% char **GetDelegateList(const char *pattern,
516% unsigned long *number_delegates,ExceptionInfo *exception)
517%
518% A description of each parameter follows:
519%
520% o pattern: Specifies a pointer to a text string containing a pattern.
521%
522% o number_delegates: This integer returns the number of delegates
523% in the list.
524%
525% o exception: return any errors or warnings in this structure.
526%
527*/
528
529#if defined(__cplusplus) || defined(c_plusplus)
530extern "C" {
531#endif
532
533static int DelegateCompare(const void *x,const void *y)
534{
535 register const char
536 **p,
537 **q;
538
539 p=(const char **) x;
540 q=(const char **) y;
541 return(LocaleCompare(*p,*q));
542}
543
544#if defined(__cplusplus) || defined(c_plusplus)
545}
546#endif
547
548MagickExport char **GetDelegateList(const char *pattern,
549 unsigned long *number_delegates,ExceptionInfo *exception)
550{
551 char
552 **delegates;
553
554 register const DelegateInfo
555 *p;
556
557 register long
558 i;
559
560 /*
561 Allocate delegate list.
562 */
563 assert(pattern != (char *) NULL);
564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
565 assert(number_delegates != (unsigned long *) NULL);
566 *number_delegates=0;
567 p=GetDelegateInfo("*","*",exception);
568 if (p == (const DelegateInfo *) NULL)
569 return((char **) NULL);
570 delegates=(char **) AcquireQuantumMemory((size_t)
571 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
572 if (delegates == (char **) NULL)
573 return((char **) NULL);
574 AcquireSemaphoreInfo(&delegate_semaphore);
575 ResetLinkedListIterator(delegate_list);
576 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
577 for (i=0; p != (const DelegateInfo *) NULL; )
578 {
579 if ((p->stealth == MagickFalse) &&
580 (GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse))
581 delegates[i++]=ConstantString(p->decode);
582 if ((p->stealth == MagickFalse) &&
583 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse))
584 delegates[i++]=ConstantString(p->encode);
585 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
586 }
587 RelinquishSemaphoreInfo(delegate_semaphore);
588 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
589 delegates[i]=(char *) NULL;
590 *number_delegates=(unsigned long) i;
591 return(delegates);
592}
593
594/*
595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596% %
597% %
598% %
599% G e t D e l e g a t e M o d e %
600% %
601% %
602% %
603%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604%
605% GetDelegateMode() returns the mode of the delegate.
606%
607% The format of the GetDelegateMode method is:
608%
609% long GetDelegateMode(const DelegateInfo *delegate_info)
610%
611% A description of each parameter follows:
612%
613% o delegate_info: The delegate info.
614%
615*/
616MagickExport long GetDelegateMode(const DelegateInfo *delegate_info)
617{
618 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
619 assert(delegate_info != (DelegateInfo *) NULL);
620 assert(delegate_info->signature == MagickSignature);
621 return(delegate_info->mode);
622}
623
624/*
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626% %
627% %
628% %
629+ G e t D e l e g a t e T h r e a d S u p p o r t %
630% %
631% %
632% %
633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634%
635% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
636% threads.
637%
638% The format of the GetDelegateThreadSupport method is:
639%
640% MagickBooleanType GetDelegateThreadSupport(
641% const DelegateInfo *delegate_info)
642%
643% A description of each parameter follows:
644%
645% o delegate_info: The delegate info.
646%
647*/
648MagickExport MagickBooleanType GetDelegateThreadSupport(
649 const DelegateInfo *delegate_info)
650{
651 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
652 assert(delegate_info != (DelegateInfo *) NULL);
653 assert(delegate_info->signature == MagickSignature);
654 return(delegate_info->thread_support);
655}
656
657/*
658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659% %
660% %
661% %
662+ I n i t i a l i z e D e l e g a t e L i s t %
663% %
664% %
665% %
666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667%
668% InitializeDelegateList() initializes the delegate list.
669%
670% The format of the InitializeDelegateList method is:
671%
672% MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
673%
674% A description of each parameter follows.
675%
676% o exception: return any errors or warnings in this structure.
677%
678*/
679static MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
680{
681 if ((delegate_list == (LinkedListInfo *) NULL) &&
682 (instantiate_delegate == MagickFalse))
683 {
684 AcquireSemaphoreInfo(&delegate_semaphore);
685 if ((delegate_list == (LinkedListInfo *) NULL) &&
686 (instantiate_delegate == MagickFalse))
687 {
688 (void) LoadDelegateLists(DelegateFilename,exception);
689 instantiate_delegate=MagickTrue;
690 }
691 RelinquishSemaphoreInfo(delegate_semaphore);
692 }
693 return(delegate_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
694}
695
696/*
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698% %
699% %
700% %
701% I n v o k e D e l e g a t e %
702% %
703% %
704% %
705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706%
707% InvokeDelegate replaces any embedded formatting characters with the
708% appropriate image attribute and executes the resulting command. MagickFalse
709% is returned if the commands execute with success otherwise MagickTrue.
710%
711% The format of the InvokeDelegate method is:
712%
713% MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
714% const char *decode,const char *encode,ExceptionInfo *exception)
715%
716% A description of each parameter follows:
717%
718% o image_info: the imageInfo.
719%
720% o image: the image.
721%
722% o exception: return any errors or warnings in this structure.
723%
724*/
725
726static inline size_t MagickMin(const size_t x,const size_t y)
727{
728 if (x < y)
729 return(x);
730 return(y);
731}
732
733static MagickBooleanType CopyDelegateFile(const char *source,
734 const char *destination)
735{
736 int
737 destination_file,
738 source_file;
739
740 MagickBooleanType
741 status;
742
743 register size_t
744 i;
745
746 size_t
747 length,
748 quantum;
749
750 ssize_t
751 count;
752
753 struct stat
754 attributes;
755
756 unsigned char
757 *buffer;
758
759 /*
760 Return if destination file already exists and is not empty.
761 */
762 assert(source != (const char *) NULL);
763 assert(destination != (char *) NULL);
764 status=GetPathAttributes(destination,&attributes);
765 if ((status != MagickFalse) && (attributes.st_size != 0))
766 return(MagickTrue);
767 /*
768 Copy source file to destination.
769 */
770 destination_file=open(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
771 if (destination_file == -1)
772 return(MagickFalse);
773 source_file=open(source,O_RDONLY | O_BINARY);
774 if (source_file == -1)
775 {
776 (void) close(destination_file);
777 return(MagickFalse);
778 }
779 quantum=(size_t) MagickMaxBufferExtent;
780 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
781 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
782 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
783 if (buffer == (unsigned char *) NULL)
784 {
785 (void) close(source_file);
786 (void) close(destination_file);
787 return(MagickFalse);
788 }
789 length=0;
790 for (i=0; ; i+=count)
791 {
792 count=(ssize_t) read(source_file,buffer,quantum);
793 if (count <= 0)
794 break;
795 length=(size_t) count;
796 count=(ssize_t) write(destination_file,buffer,length);
797 if ((size_t) count != length)
798 break;
799 }
800 (void) close(destination_file);
801 (void) close(source_file);
802 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
803 return(i != 0 ? MagickTrue : MagickFalse);
804}
805
806MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
807 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
808{
809 char
810 *command,
811 **commands,
812 input_filename[MaxTextExtent],
813 output_filename[MaxTextExtent];
814
815 const DelegateInfo
816 *delegate_info;
817
818 MagickBooleanType
819 status,
820 temporary;
821
822 register long
823 i;
824
825 PolicyRights
826 rights;
827
828 /*
829 Get delegate.
830 */
831 assert(image_info != (ImageInfo *) NULL);
832 assert(image_info->signature == MagickSignature);
833 assert(image != (Image *) NULL);
834 assert(image->signature == MagickSignature);
835 if (image->debug != MagickFalse)
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
837 rights=ExecutePolicyRights;
838 if (IsRightsAuthorized(DelegatePolicyDomain,rights,decode) == MagickFalse)
839 {
840 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
841 "NotAuthorized","`%s'",decode);
842 return(MagickFalse);
843 }
844 if (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse)
845 {
846 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
847 "NotAuthorized","`%s'",encode);
848 return(MagickFalse);
849 }
850 temporary=(*image->filename == '\0') ? MagickTrue : MagickFalse;
851 if (temporary != MagickFalse)
852 if (AcquireUniqueFilename(image->filename) == MagickFalse)
853 {
854 ThrowFileException(exception,FileOpenError,
855 "UnableToCreateTemporaryFile",image->filename);
856 return(MagickFalse);
857 }
858 delegate_info=GetDelegateInfo(decode,encode,exception);
859 if (delegate_info == (DelegateInfo *) NULL)
860 {
861 if (temporary != MagickFalse)
862 (void) RelinquishUniqueFileResource(image->filename);
863 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
864 "NoTagFound","`%s'",decode ? decode : encode);
865 return(MagickFalse);
866 }
867 if (*image_info->filename == '\0')
868 {
869 if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
870 {
871 if (temporary != MagickFalse)
872 (void) RelinquishUniqueFileResource(image->filename);
873 ThrowFileException(exception,FileOpenError,
874 "UnableToCreateTemporaryFile",image_info->filename);
875 return(MagickFalse);
876 }
877 image_info->temporary=MagickTrue;
878 }
879 if ((delegate_info->mode != 0) &&
880 (((decode != (const char *) NULL) &&
881 (delegate_info->encode != (char *) NULL)) ||
882 ((encode != (const char *) NULL) &&
883 (delegate_info->decode != (char *) NULL))))
884 {
885 char
886 *magick;
887
888 ImageInfo
889 *clone_info;
890
891 register Image
892 *p;
893
894 /*
895 Delegate requires a particular image format.
896 */
897 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
898 {
899 ThrowFileException(exception,FileOpenError,
900 "UnableToCreateTemporaryFile",image_info->unique);
901 return(MagickFalse);
902 }
903 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
904 {
905 (void) RelinquishUniqueFileResource(image_info->zero);
906 ThrowFileException(exception,FileOpenError,
907 "UnableToCreateTemporaryFile",image_info->zero);
908 return(MagickFalse);
909 }
910 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
911 delegate_info->encode : delegate_info->decode);
912 if (magick == (char *) NULL)
913 {
914 (void) RelinquishUniqueFileResource(image_info->unique);
915 (void) RelinquishUniqueFileResource(image_info->zero);
916 if (temporary != MagickFalse)
917 (void) RelinquishUniqueFileResource(image->filename);
918 (void) ThrowMagickException(exception,GetMagickModule(),
919 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
920 return(MagickFalse);
921 }
922 LocaleUpper(magick);
923 clone_info=CloneImageInfo(image_info);
924 (void) CopyMagickString((char *) clone_info->magick,magick,
925 MaxTextExtent);
926 if (LocaleCompare(magick,"NULL") != 0)
927 (void) CopyMagickString(image->magick,magick,MaxTextExtent);
928 magick=DestroyString(magick);
929 (void) FormatMagickString(clone_info->filename,MaxTextExtent,"%s:",
930 delegate_info->decode);
931 (void) SetImageInfo(clone_info,MagickTrue,exception);
932 (void) CopyMagickString(clone_info->filename,image_info->filename,
933 MaxTextExtent);
934 (void) CopyMagickString(image_info->filename,image->filename,
935 MaxTextExtent);
936 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
937 {
938 (void) FormatMagickString(p->filename,MaxTextExtent,"%s:%s",
939 delegate_info->decode,clone_info->filename);
940 status=WriteImage(clone_info,p);
941 if (status == MagickFalse)
942 {
943 (void) RelinquishUniqueFileResource(image_info->unique);
944 (void) RelinquishUniqueFileResource(image_info->zero);
945 if (temporary != MagickFalse)
946 (void) RelinquishUniqueFileResource(image->filename);
947 clone_info=DestroyImageInfo(clone_info);
948 (void) ThrowMagickException(exception,GetMagickModule(),
949 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
950 return(MagickFalse);
951 }
952 if (clone_info->adjoin != MagickFalse)
953 break;
954 }
955 (void) RelinquishUniqueFileResource(image_info->unique);
956 (void) RelinquishUniqueFileResource(image_info->zero);
957 clone_info=DestroyImageInfo(clone_info);
958 }
959 /*
960 Invoke delegate.
961 */
962 commands=StringToList(delegate_info->commands);
963 if (commands == (char **) NULL)
964 {
965 if (temporary != MagickFalse)
966 (void) RelinquishUniqueFileResource(image->filename);
967 (void) ThrowMagickException(exception,GetMagickModule(),
968 ResourceLimitError,"MemoryAllocationFailed","`%s'",
969 decode ? decode : encode);
970 return(MagickFalse);
971 }
972 command=(char *) NULL;
973 status=MagickFalse;
974 (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
975 (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
976 for (i=0; commands[i] != (char *) NULL; i++)
977 {
978 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
979 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
980 {
981 ThrowFileException(exception,FileOpenError,
982 "UnableToCreateTemporaryFile",image_info->unique);
983 break;
984 }
985 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
986 {
987 (void) RelinquishUniqueFileResource(image_info->unique);
988 ThrowFileException(exception,FileOpenError,
989 "UnableToCreateTemporaryFile",image_info->zero);
990 break;
991 }
992 if (LocaleCompare(decode,"SCAN") != 0)
993 {
994 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
995 if (status == MagickFalse)
996 {
997 ThrowFileException(exception,FileOpenError,
998 "UnableToCreateTemporaryFile",input_filename);
999 break;
1000 }
1001 }
1002 status=MagickFalse;
1003 command=InterpretImageProperties(image_info,image,commands[i]);
1004 if (command != (char *) NULL)
1005 {
1006 /*
1007 Execute delegate.
1008 */
1009 if (delegate_info->spawn != MagickFalse)
1010 (void) ConcatenateString(&command," &");
1011 status=SystemCommand(image_info->verbose,command) != 0 ? MagickTrue :
1012 MagickFalse;
1013 if (delegate_info->spawn != MagickFalse)
1014 (void) sleep(2);
1015 command=DestroyString(command);
1016 }
1017 if (LocaleCompare(decode,"SCAN") != 0)
1018 {
1019 if (CopyDelegateFile(image->filename,input_filename) == MagickFalse)
1020 (void) RelinquishUniqueFileResource(input_filename);
1021 }
1022 if (CopyDelegateFile(image_info->filename,output_filename) == MagickFalse)
1023 (void) RelinquishUniqueFileResource(output_filename);
1024 if (image_info->temporary != MagickFalse)
1025 (void) RelinquishUniqueFileResource(image_info->filename);
1026 (void) RelinquishUniqueFileResource(image_info->unique);
1027 (void) RelinquishUniqueFileResource(image_info->zero);
1028 (void) RelinquishUniqueFileResource(image_info->filename);
1029 (void) RelinquishUniqueFileResource(image->filename);
1030 if (status != MagickFalse)
1031 {
1032 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1033 "DelegateFailed","`%s'",commands[i]);
1034 break;
1035 }
1036 commands[i]=DestroyString(commands[i]);
1037 }
1038 (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1039 (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1040 /*
1041 Relinquish resources.
1042 */
1043 for ( ; commands[i] != (char *) NULL; i++)
1044 commands[i]=DestroyString(commands[i]);
1045 commands=(char **) RelinquishMagickMemory(commands);
1046 if (temporary != MagickFalse)
1047 (void) RelinquishUniqueFileResource(image->filename);
1048 return(status == MagickFalse ? MagickTrue : MagickFalse);
1049}
1050
1051/*
1052%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053% %
1054% %
1055% %
1056% L i s t D e l e g a t e I n f o %
1057% %
1058% %
1059% %
1060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061%
1062% ListDelegateInfo() lists the image formats to a file.
1063%
1064% The format of the ListDelegateInfo method is:
1065%
1066% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1067%
1068% A description of each parameter follows.
1069%
1070% o file: An pointer to a FILE.
1071%
1072% o exception: return any errors or warnings in this structure.
1073%
1074*/
1075MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1076 ExceptionInfo *exception)
1077{
1078 const DelegateInfo
1079 **delegate_info;
1080
1081 char
1082 **commands,
1083 delegate[MaxTextExtent];
1084
1085 const char
1086 *path;
1087
1088 long
1089 j;
1090
1091 register long
1092 i;
1093
1094 unsigned long
1095 number_delegates;
1096
1097 if (file == (const FILE *) NULL)
1098 file=stdout;
1099 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1100 if (delegate_info == (const DelegateInfo **) NULL)
1101 return(MagickFalse);
1102 path=(const char *) NULL;
1103 for (i=0; i < (long) number_delegates; i++)
1104 {
1105 if (delegate_info[i]->stealth != MagickFalse)
1106 continue;
1107 if ((path == (const char *) NULL) ||
1108 (LocaleCompare(path,delegate_info[i]->path) != 0))
1109 {
1110 if (delegate_info[i]->path != (char *) NULL)
1111 (void) fprintf(file,"\nPath: %s\n\n",delegate_info[i]->path);
1112 (void) fprintf(file,"Delegate Command\n");
1113 (void) fprintf(file,"-------------------------------------------------"
1114 "------------------------------\n");
1115 }
1116 path=delegate_info[i]->path;
1117 *delegate='\0';
1118 if (delegate_info[i]->encode != (char *) NULL)
1119 (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1120 (void) ConcatenateMagickString(delegate," ",MaxTextExtent);
1121 delegate[8]='\0';
1122 commands=StringToList(delegate_info[i]->commands);
1123 if (commands == (char **) NULL)
1124 continue;
1125 (void) fprintf(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
1126 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1127 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1128 StripString(commands[0]);
1129 (void) fprintf(file,"\"%s\"\n",commands[0]);
1130 for (j=1; commands[j] != (char *) NULL; j++)
1131 {
1132 StripString(commands[j]);
1133 (void) fprintf(file," \"%s\"\n",commands[j]);
1134 }
1135 for (j=0; commands[j] != (char *) NULL; j++)
1136 commands[j]=DestroyString(commands[j]);
1137 commands=(char **) RelinquishMagickMemory(commands);
1138 }
1139 (void) fflush(file);
1140 delegate_info=(const DelegateInfo **)
1141 RelinquishMagickMemory((void *) delegate_info);
1142 return(MagickTrue);
1143}
1144
1145/*
1146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1147% %
1148% %
1149% %
1150+ L o a d D e l e g a t e L i s t %
1151% %
1152% %
1153% %
1154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1155%
1156% LoadDelegateList() loads the delegate configuration file which provides a
1157% mapping between delegate attributes and a delegate name.
1158%
1159% The format of the LoadDelegateList method is:
1160%
1161% MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1162% const unsigned long depth,ExceptionInfo *exception)
1163%
1164% A description of each parameter follows:
1165%
1166% o xml: The delegate list in XML format.
1167%
1168% o filename: The delegate list filename.
1169%
1170% o depth: depth of <include /> statements.
1171%
1172% o exception: return any errors or warnings in this structure.
1173%
1174*/
1175static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1176 const unsigned long depth,ExceptionInfo *exception)
1177{
1178 char
1179 keyword[MaxTextExtent],
1180 *token;
1181
1182 const char
1183 *q;
1184
1185 DelegateInfo
1186 *delegate_info;
1187
1188 MagickBooleanType
1189 status;
1190
1191 /*
1192 Load the delegate map file.
1193 */
1194 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1195 "Loading delegate configuration file \"%s\" ...",filename);
1196 if (xml == (const char *) NULL)
1197 return(MagickFalse);
1198 if (delegate_list == (LinkedListInfo *) NULL)
1199 {
1200 delegate_list=NewLinkedList(0);
1201 if (delegate_list == (LinkedListInfo *) NULL)
1202 {
1203 ThrowFileException(exception,ResourceLimitError,
1204 "MemoryAllocationFailed",filename);
1205 return(MagickFalse);
1206 }
1207 }
1208 status=MagickTrue;
1209 delegate_info=(DelegateInfo *) NULL;
1210 token=AcquireString(xml);
1211 for (q=(const char *) xml; *q != '\0'; )
1212 {
1213 /*
1214 Interpret XML.
1215 */
1216 GetMagickToken(q,&q,token);
1217 if (*token == '\0')
1218 break;
1219 (void) CopyMagickString(keyword,token,MaxTextExtent);
1220 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1221 {
1222 /*
1223 Doctype element.
1224 */
1225 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1226 GetMagickToken(q,&q,token);
1227 continue;
1228 }
1229 if (LocaleNCompare(keyword,"<!--",4) == 0)
1230 {
1231 /*
1232 Comment element.
1233 */
1234 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1235 GetMagickToken(q,&q,token);
1236 continue;
1237 }
1238 if (LocaleCompare(keyword,"<include") == 0)
1239 {
1240 /*
1241 Include element.
1242 */
1243 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1244 {
1245 (void) CopyMagickString(keyword,token,MaxTextExtent);
1246 GetMagickToken(q,&q,token);
1247 if (*token != '=')
1248 continue;
1249 GetMagickToken(q,&q,token);
1250 if (LocaleCompare(keyword,"file") == 0)
1251 {
1252 if (depth > 200)
1253 (void) ThrowMagickException(exception,GetMagickModule(),
1254 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1255 else
1256 {
1257 char
1258 path[MaxTextExtent],
1259 *xml;
1260
1261 GetPathComponent(filename,HeadPath,path);
1262 if (*path != '\0')
1263 (void) ConcatenateMagickString(path,DirectorySeparator,
1264 MaxTextExtent);
1265 if (*token == *DirectorySeparator)
1266 (void) CopyMagickString(path,token,MaxTextExtent);
1267 else
1268 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1269 xml=FileToString(path,~0,exception);
1270 if (xml != (char *) NULL)
1271 {
1272 status=LoadDelegateList(xml,path,depth+1,exception);
1273 xml=(char *) RelinquishMagickMemory(xml);
1274 }
1275 }
1276 }
1277 }
1278 continue;
1279 }
1280 if (LocaleCompare(keyword,"<delegate") == 0)
1281 {
1282 /*
1283 Delegate element.
1284 */
1285 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1286 sizeof(*delegate_info));
1287 if (delegate_info == (DelegateInfo *) NULL)
1288 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1289 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1290 delegate_info->path=ConstantString(filename);
1291 delegate_info->signature=MagickSignature;
1292 continue;
1293 }
1294 if (delegate_info == (DelegateInfo *) NULL)
1295 continue;
1296 if (LocaleCompare(keyword,"/>") == 0)
1297 {
1298 status=AppendValueToLinkedList(delegate_list,delegate_info);
1299 if (status == MagickFalse)
1300 (void) ThrowMagickException(exception,GetMagickModule(),
1301 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1302 delegate_info->commands);
1303 delegate_info=(DelegateInfo *) NULL;
1304 }
1305 GetMagickToken(q,(const char **) NULL,token);
1306 if (*token != '=')
1307 continue;
1308 GetMagickToken(q,&q,token);
1309 GetMagickToken(q,&q,token);
1310 switch (*keyword)
1311 {
1312 case 'C':
1313 case 'c':
1314 {
1315 if (LocaleCompare((char *) keyword,"command") == 0)
1316 {
1317 char
1318 *commands;
1319
1320 commands=AcquireString(token);
1321#if defined(__WINDOWS__)
1322 if (strchr(commands,'@') != (char *) NULL)
1323 {
1324 char
1325 path[MaxTextExtent];
1326
1327 NTGhostscriptEXE(path,MaxTextExtent);
1328 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1329 path);
1330 (void) SubstituteString((char **) &commands,"\\","/");
1331 }
1332#endif
1333 (void) SubstituteString((char **) &commands,"&amp;","&");
1334 (void) SubstituteString((char **) &commands,"&quot;","\"");
1335 (void) SubstituteString((char **) &commands,"&gt;",">");
1336 (void) SubstituteString((char **) &commands,"&lt;","<");
1337 delegate_info->commands=commands;
1338 break;
1339 }
1340 break;
1341 }
1342 case 'D':
1343 case 'd':
1344 {
1345 if (LocaleCompare((char *) keyword,"decode") == 0)
1346 {
1347 delegate_info->decode=ConstantString(token);
1348 delegate_info->mode=1;
1349 break;
1350 }
1351 break;
1352 }
1353 case 'E':
1354 case 'e':
1355 {
1356 if (LocaleCompare((char *) keyword,"encode") == 0)
1357 {
1358 delegate_info->encode=ConstantString(token);
1359 delegate_info->mode=(-1);
1360 break;
1361 }
1362 break;
1363 }
1364 case 'M':
1365 case 'm':
1366 {
1367 if (LocaleCompare((char *) keyword,"mode") == 0)
1368 {
1369 delegate_info->mode=1;
1370 if (LocaleCompare(token,"bi") == 0)
1371 delegate_info->mode=0;
1372 else
1373 if (LocaleCompare(token,"encode") == 0)
1374 delegate_info->mode=(-1);
1375 break;
1376 }
1377 break;
1378 }
1379 case 'S':
1380 case 's':
1381 {
1382 if (LocaleCompare((char *) keyword,"spawn") == 0)
1383 {
1384 delegate_info->spawn=IsMagickTrue(token);
1385 break;
1386 }
1387 if (LocaleCompare((char *) keyword,"stealth") == 0)
1388 {
1389 delegate_info->stealth=IsMagickTrue(token);
1390 break;
1391 }
1392 break;
1393 }
1394 case 'T':
1395 case 't':
1396 {
1397 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1398 {
1399 delegate_info->thread_support=IsMagickTrue(token);
1400 break;
1401 }
1402 break;
1403 }
1404 default:
1405 break;
1406 }
1407 }
1408 token=(char *) RelinquishMagickMemory(token);
1409 return(status);
1410}
1411
1412/*
1413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414% %
1415% %
1416% %
1417% L o a d D e l e g a t e L i s t s %
1418% %
1419% %
1420% %
1421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422%
1423% LoadDelegateList() loads one or more delegate configuration file which
1424% provides a mapping between delegate attributes and a delegate name.
1425%
1426% The format of the LoadDelegateLists method is:
1427%
1428% MagickBooleanType LoadDelegateLists(const char *filename,
1429% ExceptionInfo *exception)
1430%
1431% A description of each parameter follows:
1432%
1433% o filename: the font file name.
1434%
1435% o exception: return any errors or warnings in this structure.
1436%
1437*/
1438static MagickBooleanType LoadDelegateLists(const char *filename,
1439 ExceptionInfo *exception)
1440{
1441#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1442 return(LoadDelegateList(DelegateMap,"built-in",0,exception));
1443#else
1444 const StringInfo
1445 *option;
1446
1447 LinkedListInfo
1448 *options;
1449
1450 MagickStatusType
1451 status;
1452
1453 status=MagickFalse;
1454 options=GetConfigureOptions(filename,exception);
1455 option=(const StringInfo *) GetNextValueInLinkedList(options);
1456 while (option != (const StringInfo *) NULL)
1457 {
1458 status|=LoadDelegateList((const char *) GetStringInfoDatum(option),
1459 GetStringInfoPath(option),0,exception);
1460 option=(const StringInfo *) GetNextValueInLinkedList(options);
1461 }
1462 options=DestroyConfigureOptions(options);
1463 if ((delegate_list == (LinkedListInfo *) NULL) ||
1464 (IsLinkedListEmpty(delegate_list) != MagickFalse))
1465 status|=LoadDelegateList(DelegateMap,"built-in",0,exception);
1466 return(status != 0 ? MagickTrue : MagickFalse);
1467#endif
1468}