blob: bfb4b5d8c3dacb453fe8831430c9b7a2280e73d3 [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% %
cristy7e41fe82010-12-04 23:12:08 +000019% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000020% 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*/
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/studio.h"
49#include "MagickCore/property.h"
50#include "MagickCore/blob.h"
51#include "MagickCore/client.h"
52#include "MagickCore/configure.h"
53#include "MagickCore/constitute.h"
54#include "MagickCore/delegate.h"
cristy5ff4eaf2011-09-03 01:38:02 +000055#include "MagickCore/delegate-private.h"
cristy4c08aed2011-07-01 19:47:50 +000056#include "MagickCore/exception.h"
57#include "MagickCore/exception-private.h"
58#include "MagickCore/hashmap.h"
59#include "MagickCore/list.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/policy.h"
62#include "MagickCore/resource_.h"
63#include "MagickCore/semaphore.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/token.h"
66#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000067#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000068#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71 Define declarations.
72*/
73#define DelegateFilename "delegates.xml"
74
75/*
76 Declare delegate map.
77*/
78static const char
79 *DelegateMap = (const char *)
80 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
81 "<delegatemap>"
82 " <delegate decode=\"autotrace\" stealth=\"True\" command=\"&quot;autotrace&quot; -output-format svg -output-file &quot;%o&quot; &quot;%i&quot;\"/>"
83 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\"&quot;mplayer&quot; &quot;%i&quot; -really-quiet -ao null -vo png:z=3\"/>"
84 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; http://www.imagemagick.org/\"/>"
85 " <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;\"/>"
86 " <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;\"/>"
87 " <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
88 " <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;\"/>"
89 " <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;\"/>"
90 " <delegate decode=\"fig\" command=\"&quot;fig2dev&quot; -L ps &quot;%i&quot; &quot;%o&quot;\"/>"
91 " <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;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000092 " <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;\"/>"
93 " <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\"/>"
94 " <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
95 " <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
96 " <delegate decode=\"https\" command=\"&quot;wget&quot; -q -O &quot;%o&quot; &quot;https:%M&quot;\"/>"
97 " <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
98 " <delegate decode=\"man\" command=\"&quot;groff&quot; -man -Tps &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
99 " <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;\"/>"
100 " <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;\"/>"
101 " <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;\"/>"
102 " <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;\"/>"
103 " <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;\"/>"
104 " <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;\"/>"
105 " <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;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000106 " <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;\"/>"
cristy875f9c52010-04-28 16:37:46 +0000114 " <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=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000115 " <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;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000117 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\"&quot;rawtorle&quot; -o &quot;%o&quot; -v &quot;%i&quot;\"/>"
118 " <delegate decode=\"scan\" command=\"&quot;scanimage&quot; -d &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
cristy8370e012010-06-05 13:45:00 +0000119 " <delegate encode=\"show\" 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;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000120 " <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
cristy4689cf02010-02-17 21:15:45 +0000121 " <delegate decode=\"svg\" command=\"&quot;rsvg&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000122 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\"&quot;enscript&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
123 " <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;\"/>"
124 " <delegate decode=\"wmf\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
125 "</delegatemap>";
126
127/*
128 Global declaractions.
129*/
130static LinkedListInfo
131 *delegate_list = (LinkedListInfo *) NULL;
132
133static SemaphoreInfo
134 *delegate_semaphore = (SemaphoreInfo *) NULL;
135
136static volatile MagickBooleanType
137 instantiate_delegate = MagickFalse;
138
139/*
140 Forward declaractions.
141*/
142static MagickBooleanType
143 InitializeDelegateList(ExceptionInfo *),
144 LoadDelegateLists(const char *,ExceptionInfo *);
145
146/*
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148% %
149% %
150% %
cristyf34a1452009-10-24 22:29:27 +0000151+ D e l e g a t e C o m p o n e n t T e r m i n u s %
cristy3ed852e2009-09-05 21:47:34 +0000152% %
153% %
154% %
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156%
cristyf34a1452009-10-24 22:29:27 +0000157% DelegateComponentGenesis() instantiates the delegate component.
cristy3ed852e2009-09-05 21:47:34 +0000158%
cristyf34a1452009-10-24 22:29:27 +0000159% The format of the DelegateComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000160%
cristyf34a1452009-10-24 22:29:27 +0000161% MagickBooleanType DelegateComponentGenesis(void)
162%
163*/
cristy5ff4eaf2011-09-03 01:38:02 +0000164MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000165{
cristy165b6092009-10-26 13:52:10 +0000166 AcquireSemaphoreInfo(&delegate_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000167 return(MagickTrue);
168}
169
170/*
171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172% %
173% %
174% %
175% D e l e g a t e C o m p o n e n t T e r m i n u s %
176% %
177% %
178% %
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180%
181% DelegateComponentTerminus() destroys the delegate component.
182%
183% The format of the DelegateComponentTerminus method is:
184%
185% DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000186%
187*/
188
189static void *DestroyDelegate(void *delegate_info)
190{
191 register DelegateInfo
192 *p;
193
194 p=(DelegateInfo *) delegate_info;
195 if (p->path != (char *) NULL)
196 p->path=DestroyString(p->path);
197 if (p->decode != (char *) NULL)
198 p->decode=DestroyString(p->decode);
199 if (p->encode != (char *) NULL)
200 p->encode=DestroyString(p->encode);
201 if (p->commands != (char *) NULL)
202 p->commands=DestroyString(p->commands);
203 p=(DelegateInfo *) RelinquishMagickMemory(p);
204 return((void *) NULL);
205}
206
cristy5ff4eaf2011-09-03 01:38:02 +0000207MagickPrivate void DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000208{
cristy18b17442009-10-25 18:36:48 +0000209 if (delegate_semaphore == (SemaphoreInfo *) NULL)
210 AcquireSemaphoreInfo(&delegate_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000211 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000212 if (delegate_list != (LinkedListInfo *) NULL)
213 delegate_list=DestroyLinkedList(delegate_list,DestroyDelegate);
214 instantiate_delegate=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000215 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000216 DestroySemaphoreInfo(&delegate_semaphore);
217}
218
219/*
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221% %
222% %
223% %
224% G e t D e l e g a t e C o m m a n d %
225% %
226% %
227% %
228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229%
230% GetDelegateCommand() replaces any embedded formatting characters with the
231% appropriate image attribute and returns the resulting command.
232%
233% The format of the GetDelegateCommand method is:
234%
235% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
236% const char *decode,const char *encode,ExceptionInfo *exception)
237%
238% A description of each parameter follows:
239%
240% o command: Method GetDelegateCommand returns the command associated
241% with specified delegate tag.
242%
243% o image_info: the image info.
244%
245% o image: the image.
246%
247% o decode: Specifies the decode delegate we are searching for as a
248% character string.
249%
250% o encode: Specifies the encode delegate we are searching for as a
251% character string.
252%
253% o exception: return any errors or warnings in this structure.
254%
255*/
256MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
257 const char *decode,const char *encode,ExceptionInfo *exception)
258{
259 char
260 *command,
261 **commands;
262
263 const DelegateInfo
264 *delegate_info;
265
cristybb503372010-05-27 20:51:26 +0000266 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000267 i;
268
269 assert(image_info != (ImageInfo *) NULL);
270 assert(image_info->signature == MagickSignature);
271 assert(image != (Image *) NULL);
272 assert(image->signature == MagickSignature);
273 if (image->debug != MagickFalse)
274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
275 delegate_info=GetDelegateInfo(decode,encode,exception);
276 if (delegate_info == (const DelegateInfo *) NULL)
277 {
278 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
279 "NoTagFound","`%s'",decode ? decode : encode);
280 return((char *) NULL);
281 }
282 commands=StringToList(delegate_info->commands);
283 if (commands == (char **) NULL)
284 {
285 (void) ThrowMagickException(exception,GetMagickModule(),
286 ResourceLimitError,"MemoryAllocationFailed","`%s'",
287 decode ? decode : encode);
288 return((char *) NULL);
289 }
cristy018f07f2011-09-04 21:15:19 +0000290 command=InterpretImageProperties(image_info,image,commands[0],exception);
cristy3ed852e2009-09-05 21:47:34 +0000291 if (command == (char *) NULL)
292 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
293 "MemoryAllocationFailed","`%s'",commands[0]);
294 /*
295 Relinquish resources.
296 */
297 for (i=0; commands[i] != (char *) NULL; i++)
298 commands[i]=DestroyString(commands[i]);
299 commands=(char **) RelinquishMagickMemory(commands);
300 return(command);
301}
302
303/*
304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305% %
306% %
307% %
308% G e t D e l e g a t e C o m m a n d s %
309% %
310% %
311% %
312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313%
314% GetDelegateCommands() returns the commands associated with a delegate.
315%
316% The format of the GetDelegateCommands method is:
317%
318% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
319%
320% A description of each parameter follows:
321%
322% o delegate_info: The delegate info.
323%
324*/
325MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
326{
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
328 assert(delegate_info != (DelegateInfo *) NULL);
329 assert(delegate_info->signature == MagickSignature);
330 return(delegate_info->commands);
331}
332
333/*
334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335% %
336% %
337% %
338% G e t D e l e g a t e I n f o %
339% %
340% %
341% %
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343%
344% GetDelegateInfo() returns any delegates associated with the specified tag.
345%
346% The format of the GetDelegateInfo method is:
347%
348% const DelegateInfo *GetDelegateInfo(const char *decode,
349% const char *encode,ExceptionInfo *exception)
350%
351% A description of each parameter follows:
352%
353% o decode: Specifies the decode delegate we are searching for as a
354% character string.
355%
356% o encode: Specifies the encode delegate we are searching for as a
357% character string.
358%
359% o exception: return any errors or warnings in this structure.
360%
361*/
362MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
363 const char *encode,ExceptionInfo *exception)
364{
365 register const DelegateInfo
366 *p;
367
368 assert(exception != (ExceptionInfo *) NULL);
369 if ((delegate_list == (LinkedListInfo *) NULL) ||
370 (instantiate_delegate == MagickFalse))
371 if (InitializeDelegateList(exception) == MagickFalse)
372 return((const DelegateInfo *) NULL);
373 if ((delegate_list == (LinkedListInfo *) NULL) ||
374 (IsLinkedListEmpty(delegate_list) != MagickFalse))
375 return((const DelegateInfo *) NULL);
376 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
377 return((const DelegateInfo *) GetValueFromLinkedList(delegate_list,0));
378 /*
379 Search for named delegate.
380 */
cristyf84a1932010-01-03 18:00:18 +0000381 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000382 ResetLinkedListIterator(delegate_list);
383 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
384 while (p != (const DelegateInfo *) NULL)
385 {
386 if (p->mode > 0)
387 {
388 if (LocaleCompare(p->decode,decode) == 0)
389 break;
390 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
391 continue;
392 }
393 if (p->mode < 0)
394 {
395 if (LocaleCompare(p->encode,encode) == 0)
396 break;
397 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
398 continue;
399 }
400 if (LocaleCompare(decode,p->decode) == 0)
401 if (LocaleCompare(encode,p->encode) == 0)
402 break;
403 if (LocaleCompare(decode,"*") == 0)
404 if (LocaleCompare(encode,p->encode) == 0)
405 break;
406 if (LocaleCompare(decode,p->decode) == 0)
407 if (LocaleCompare(encode,"*") == 0)
408 break;
409 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
410 }
411 if (p != (const DelegateInfo *) NULL)
412 (void) InsertValueInLinkedList(delegate_list,0,
413 RemoveElementByValueFromLinkedList(delegate_list,p));
cristyf84a1932010-01-03 18:00:18 +0000414 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000415 return(p);
416}
417
418/*
419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420% %
421% %
422% %
423% G e t D e l e g a t e I n f o L i s t %
424% %
425% %
426% %
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428%
429% GetDelegateInfoList() returns any delegates that match the specified pattern.
430%
431% The delegate of the GetDelegateInfoList function is:
432%
433% const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000434% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000435%
436% A description of each parameter follows:
437%
438% o pattern: Specifies a pointer to a text string containing a pattern.
439%
440% o number_delegates: This integer returns the number of delegates in the
441% list.
442%
443% o exception: return any errors or warnings in this structure.
444%
445*/
446
447#if defined(__cplusplus) || defined(c_plusplus)
448extern "C" {
449#endif
450
451static int DelegateInfoCompare(const void *x,const void *y)
452{
453 const DelegateInfo
454 **p,
455 **q;
456
457 p=(const DelegateInfo **) x,
458 q=(const DelegateInfo **) y;
459 if (LocaleCompare((*p)->path,(*q)->path) == 0)
460 {
461 if ((*p)->decode == (char *) NULL)
462 if (((*p)->encode != (char *) NULL) &&
463 ((*q)->encode != (char *) NULL))
464 return(strcmp((*p)->encode,(*q)->encode));
465 if (((*p)->decode != (char *) NULL) &&
466 ((*q)->decode != (char *) NULL))
467 return(strcmp((*p)->decode,(*q)->decode));
468 }
469 return(LocaleCompare((*p)->path,(*q)->path));
470}
471
472#if defined(__cplusplus) || defined(c_plusplus)
473}
474#endif
475
476MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000477 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000478{
479 const DelegateInfo
480 **delegates;
481
482 register const DelegateInfo
483 *p;
484
cristybb503372010-05-27 20:51:26 +0000485 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000486 i;
487
488 /*
489 Allocate delegate list.
490 */
491 assert(pattern != (char *) NULL);
492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000493 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000494 *number_delegates=0;
495 p=GetDelegateInfo("*","*",exception);
496 if (p == (const DelegateInfo *) NULL)
497 return((const DelegateInfo **) NULL);
498 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
499 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
500 if (delegates == (const DelegateInfo **) NULL)
501 return((const DelegateInfo **) NULL);
502 /*
503 Generate delegate list.
504 */
cristyf84a1932010-01-03 18:00:18 +0000505 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000506 ResetLinkedListIterator(delegate_list);
507 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
508 for (i=0; p != (const DelegateInfo *) NULL; )
509 {
510 if ((p->stealth == MagickFalse) &&
511 ((GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse) ||
512 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse)))
513 delegates[i++]=p;
514 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
515 }
cristyf84a1932010-01-03 18:00:18 +0000516 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000517 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
518 delegates[i]=(DelegateInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000519 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000520 return(delegates);
521}
522
523/*
524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525% %
526% %
527% %
528% G e t D e l e g a t e L i s t %
529% %
530% %
531% %
532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533%
534% GetDelegateList() returns any image format delegates that match the
535% specified pattern.
536%
537% The format of the GetDelegateList function is:
538%
539% char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000540% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000541%
542% A description of each parameter follows:
543%
544% o pattern: Specifies a pointer to a text string containing a pattern.
545%
546% o number_delegates: This integer returns the number of delegates
547% in the list.
548%
549% o exception: return any errors or warnings in this structure.
550%
551*/
552
553#if defined(__cplusplus) || defined(c_plusplus)
554extern "C" {
555#endif
556
557static int DelegateCompare(const void *x,const void *y)
558{
559 register const char
560 **p,
561 **q;
562
563 p=(const char **) x;
564 q=(const char **) y;
565 return(LocaleCompare(*p,*q));
566}
567
568#if defined(__cplusplus) || defined(c_plusplus)
569}
570#endif
571
572MagickExport char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000573 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000574{
575 char
576 **delegates;
577
578 register const DelegateInfo
579 *p;
580
cristybb503372010-05-27 20:51:26 +0000581 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000582 i;
583
584 /*
585 Allocate delegate list.
586 */
587 assert(pattern != (char *) NULL);
588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000589 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000590 *number_delegates=0;
591 p=GetDelegateInfo("*","*",exception);
592 if (p == (const DelegateInfo *) NULL)
593 return((char **) NULL);
594 delegates=(char **) AcquireQuantumMemory((size_t)
595 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
596 if (delegates == (char **) NULL)
597 return((char **) NULL);
cristyf84a1932010-01-03 18:00:18 +0000598 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000599 ResetLinkedListIterator(delegate_list);
600 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
601 for (i=0; p != (const DelegateInfo *) NULL; )
602 {
603 if ((p->stealth == MagickFalse) &&
604 (GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse))
605 delegates[i++]=ConstantString(p->decode);
606 if ((p->stealth == MagickFalse) &&
607 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse))
608 delegates[i++]=ConstantString(p->encode);
609 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
610 }
cristyf84a1932010-01-03 18:00:18 +0000611 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000612 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
613 delegates[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000614 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000615 return(delegates);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% G e t D e l e g a t e M o d e %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% GetDelegateMode() returns the mode of the delegate.
630%
631% The format of the GetDelegateMode method is:
632%
cristybb503372010-05-27 20:51:26 +0000633% ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +0000634%
635% A description of each parameter follows:
636%
637% o delegate_info: The delegate info.
638%
639*/
cristybb503372010-05-27 20:51:26 +0000640MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +0000641{
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
643 assert(delegate_info != (DelegateInfo *) NULL);
644 assert(delegate_info->signature == MagickSignature);
645 return(delegate_info->mode);
646}
647
648/*
649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650% %
651% %
652% %
653+ G e t D e l e g a t e T h r e a d S u p p o r t %
654% %
655% %
656% %
657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658%
659% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
660% threads.
661%
662% The format of the GetDelegateThreadSupport method is:
663%
664% MagickBooleanType GetDelegateThreadSupport(
665% const DelegateInfo *delegate_info)
666%
667% A description of each parameter follows:
668%
669% o delegate_info: The delegate info.
670%
671*/
672MagickExport MagickBooleanType GetDelegateThreadSupport(
673 const DelegateInfo *delegate_info)
674{
675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
676 assert(delegate_info != (DelegateInfo *) NULL);
677 assert(delegate_info->signature == MagickSignature);
678 return(delegate_info->thread_support);
679}
680
681/*
682%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683% %
684% %
685% %
686+ I n i t i a l i z e D e l e g a t e L i s t %
687% %
688% %
689% %
690%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691%
692% InitializeDelegateList() initializes the delegate list.
693%
694% The format of the InitializeDelegateList method is:
695%
696% MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
697%
698% A description of each parameter follows.
699%
700% o exception: return any errors or warnings in this structure.
701%
702*/
703static MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
704{
705 if ((delegate_list == (LinkedListInfo *) NULL) &&
706 (instantiate_delegate == MagickFalse))
707 {
cristy4e1dff62009-10-25 20:36:03 +0000708 if (delegate_semaphore == (SemaphoreInfo *) NULL)
709 AcquireSemaphoreInfo(&delegate_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000710 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000711 if ((delegate_list == (LinkedListInfo *) NULL) &&
712 (instantiate_delegate == MagickFalse))
713 {
714 (void) LoadDelegateLists(DelegateFilename,exception);
715 instantiate_delegate=MagickTrue;
716 }
cristyf84a1932010-01-03 18:00:18 +0000717 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000718 }
719 return(delegate_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% I n v o k e D e l e g a t e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% InvokeDelegate replaces any embedded formatting characters with the
734% appropriate image attribute and executes the resulting command. MagickFalse
735% is returned if the commands execute with success otherwise MagickTrue.
736%
737% The format of the InvokeDelegate method is:
738%
739% MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
740% const char *decode,const char *encode,ExceptionInfo *exception)
741%
742% A description of each parameter follows:
743%
744% o image_info: the imageInfo.
745%
746% o image: the image.
747%
748% o exception: return any errors or warnings in this structure.
749%
750*/
751
752static inline size_t MagickMin(const size_t x,const size_t y)
753{
754 if (x < y)
755 return(x);
756 return(y);
757}
758
759static MagickBooleanType CopyDelegateFile(const char *source,
760 const char *destination)
761{
762 int
763 destination_file,
764 source_file;
765
766 MagickBooleanType
767 status;
768
769 register size_t
770 i;
771
772 size_t
773 length,
774 quantum;
775
776 ssize_t
777 count;
778
779 struct stat
780 attributes;
781
782 unsigned char
783 *buffer;
784
785 /*
786 Return if destination file already exists and is not empty.
787 */
788 assert(source != (const char *) NULL);
789 assert(destination != (char *) NULL);
790 status=GetPathAttributes(destination,&attributes);
791 if ((status != MagickFalse) && (attributes.st_size != 0))
792 return(MagickTrue);
793 /*
794 Copy source file to destination.
795 */
796 destination_file=open(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
797 if (destination_file == -1)
798 return(MagickFalse);
799 source_file=open(source,O_RDONLY | O_BINARY);
800 if (source_file == -1)
801 {
802 (void) close(destination_file);
803 return(MagickFalse);
804 }
805 quantum=(size_t) MagickMaxBufferExtent;
806 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
807 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
808 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
809 if (buffer == (unsigned char *) NULL)
810 {
811 (void) close(source_file);
812 (void) close(destination_file);
813 return(MagickFalse);
814 }
815 length=0;
816 for (i=0; ; i+=count)
817 {
818 count=(ssize_t) read(source_file,buffer,quantum);
819 if (count <= 0)
820 break;
821 length=(size_t) count;
822 count=(ssize_t) write(destination_file,buffer,length);
823 if ((size_t) count != length)
824 break;
825 }
826 (void) close(destination_file);
827 (void) close(source_file);
828 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
829 return(i != 0 ? MagickTrue : MagickFalse);
830}
831
832MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
833 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
834{
835 char
836 *command,
837 **commands,
838 input_filename[MaxTextExtent],
839 output_filename[MaxTextExtent];
840
841 const DelegateInfo
842 *delegate_info;
843
844 MagickBooleanType
845 status,
846 temporary;
847
cristybb503372010-05-27 20:51:26 +0000848 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000849 i;
850
851 PolicyRights
852 rights;
853
854 /*
855 Get delegate.
856 */
857 assert(image_info != (ImageInfo *) NULL);
858 assert(image_info->signature == MagickSignature);
859 assert(image != (Image *) NULL);
860 assert(image->signature == MagickSignature);
861 if (image->debug != MagickFalse)
862 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
863 rights=ExecutePolicyRights;
864 if (IsRightsAuthorized(DelegatePolicyDomain,rights,decode) == MagickFalse)
865 {
cristya9197f62010-01-12 02:23:34 +0000866 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
868 "NotAuthorized","`%s'",decode);
869 return(MagickFalse);
870 }
871 if (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse)
872 {
cristya9197f62010-01-12 02:23:34 +0000873 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +0000874 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
875 "NotAuthorized","`%s'",encode);
876 return(MagickFalse);
877 }
878 temporary=(*image->filename == '\0') ? MagickTrue : MagickFalse;
879 if (temporary != MagickFalse)
880 if (AcquireUniqueFilename(image->filename) == MagickFalse)
881 {
882 ThrowFileException(exception,FileOpenError,
883 "UnableToCreateTemporaryFile",image->filename);
884 return(MagickFalse);
885 }
886 delegate_info=GetDelegateInfo(decode,encode,exception);
887 if (delegate_info == (DelegateInfo *) NULL)
888 {
889 if (temporary != MagickFalse)
890 (void) RelinquishUniqueFileResource(image->filename);
891 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
892 "NoTagFound","`%s'",decode ? decode : encode);
893 return(MagickFalse);
894 }
895 if (*image_info->filename == '\0')
896 {
897 if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
898 {
899 if (temporary != MagickFalse)
900 (void) RelinquishUniqueFileResource(image->filename);
901 ThrowFileException(exception,FileOpenError,
902 "UnableToCreateTemporaryFile",image_info->filename);
903 return(MagickFalse);
904 }
905 image_info->temporary=MagickTrue;
906 }
907 if ((delegate_info->mode != 0) &&
908 (((decode != (const char *) NULL) &&
909 (delegate_info->encode != (char *) NULL)) ||
910 ((encode != (const char *) NULL) &&
911 (delegate_info->decode != (char *) NULL))))
912 {
913 char
914 *magick;
915
916 ImageInfo
917 *clone_info;
918
919 register Image
920 *p;
921
922 /*
923 Delegate requires a particular image format.
924 */
925 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
926 {
927 ThrowFileException(exception,FileOpenError,
928 "UnableToCreateTemporaryFile",image_info->unique);
929 return(MagickFalse);
930 }
931 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
932 {
933 (void) RelinquishUniqueFileResource(image_info->zero);
934 ThrowFileException(exception,FileOpenError,
935 "UnableToCreateTemporaryFile",image_info->zero);
936 return(MagickFalse);
937 }
938 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
cristy018f07f2011-09-04 21:15:19 +0000939 delegate_info->encode : delegate_info->decode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000940 if (magick == (char *) NULL)
941 {
942 (void) RelinquishUniqueFileResource(image_info->unique);
943 (void) RelinquishUniqueFileResource(image_info->zero);
944 if (temporary != MagickFalse)
945 (void) RelinquishUniqueFileResource(image->filename);
946 (void) ThrowMagickException(exception,GetMagickModule(),
947 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
948 return(MagickFalse);
949 }
950 LocaleUpper(magick);
951 clone_info=CloneImageInfo(image_info);
952 (void) CopyMagickString((char *) clone_info->magick,magick,
953 MaxTextExtent);
954 if (LocaleCompare(magick,"NULL") != 0)
955 (void) CopyMagickString(image->magick,magick,MaxTextExtent);
956 magick=DestroyString(magick);
cristyb51dff52011-05-19 16:55:47 +0000957 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:",
cristy3ed852e2009-09-05 21:47:34 +0000958 delegate_info->decode);
cristyd965a422010-03-03 17:47:35 +0000959 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
960 exception);
cristy3ed852e2009-09-05 21:47:34 +0000961 (void) CopyMagickString(clone_info->filename,image_info->filename,
962 MaxTextExtent);
963 (void) CopyMagickString(image_info->filename,image->filename,
964 MaxTextExtent);
965 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
966 {
cristyb51dff52011-05-19 16:55:47 +0000967 (void) FormatLocaleString(p->filename,MaxTextExtent,"%s:%s",
cristy3ed852e2009-09-05 21:47:34 +0000968 delegate_info->decode,clone_info->filename);
cristy6f9e0d32011-08-28 16:32:09 +0000969 status=WriteImage(clone_info,p,exception);
cristy3ed852e2009-09-05 21:47:34 +0000970 if (status == MagickFalse)
971 {
972 (void) RelinquishUniqueFileResource(image_info->unique);
973 (void) RelinquishUniqueFileResource(image_info->zero);
974 if (temporary != MagickFalse)
975 (void) RelinquishUniqueFileResource(image->filename);
976 clone_info=DestroyImageInfo(clone_info);
977 (void) ThrowMagickException(exception,GetMagickModule(),
978 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
979 return(MagickFalse);
980 }
981 if (clone_info->adjoin != MagickFalse)
982 break;
983 }
984 (void) RelinquishUniqueFileResource(image_info->unique);
985 (void) RelinquishUniqueFileResource(image_info->zero);
986 clone_info=DestroyImageInfo(clone_info);
987 }
988 /*
989 Invoke delegate.
990 */
991 commands=StringToList(delegate_info->commands);
992 if (commands == (char **) NULL)
993 {
994 if (temporary != MagickFalse)
995 (void) RelinquishUniqueFileResource(image->filename);
996 (void) ThrowMagickException(exception,GetMagickModule(),
997 ResourceLimitError,"MemoryAllocationFailed","`%s'",
998 decode ? decode : encode);
999 return(MagickFalse);
1000 }
1001 command=(char *) NULL;
1002 status=MagickFalse;
1003 (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
1004 (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
1005 for (i=0; commands[i] != (char *) NULL; i++)
1006 {
1007 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1008 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1009 {
1010 ThrowFileException(exception,FileOpenError,
1011 "UnableToCreateTemporaryFile",image_info->unique);
1012 break;
1013 }
1014 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
1015 {
1016 (void) RelinquishUniqueFileResource(image_info->unique);
1017 ThrowFileException(exception,FileOpenError,
1018 "UnableToCreateTemporaryFile",image_info->zero);
1019 break;
1020 }
1021 if (LocaleCompare(decode,"SCAN") != 0)
1022 {
1023 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1024 if (status == MagickFalse)
1025 {
1026 ThrowFileException(exception,FileOpenError,
1027 "UnableToCreateTemporaryFile",input_filename);
1028 break;
1029 }
1030 }
1031 status=MagickFalse;
cristy018f07f2011-09-04 21:15:19 +00001032 command=InterpretImageProperties(image_info,image,commands[i],exception);
cristy3ed852e2009-09-05 21:47:34 +00001033 if (command != (char *) NULL)
1034 {
1035 /*
1036 Execute delegate.
1037 */
cristy6de4bc22010-01-12 17:10:35 +00001038 status=SystemCommand(delegate_info->spawn,image_info->verbose,command,
1039 exception) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001040 if (delegate_info->spawn != MagickFalse)
1041 (void) sleep(2);
1042 command=DestroyString(command);
1043 }
1044 if (LocaleCompare(decode,"SCAN") != 0)
1045 {
1046 if (CopyDelegateFile(image->filename,input_filename) == MagickFalse)
1047 (void) RelinquishUniqueFileResource(input_filename);
1048 }
1049 if (CopyDelegateFile(image_info->filename,output_filename) == MagickFalse)
1050 (void) RelinquishUniqueFileResource(output_filename);
1051 if (image_info->temporary != MagickFalse)
1052 (void) RelinquishUniqueFileResource(image_info->filename);
1053 (void) RelinquishUniqueFileResource(image_info->unique);
1054 (void) RelinquishUniqueFileResource(image_info->zero);
1055 (void) RelinquishUniqueFileResource(image_info->filename);
1056 (void) RelinquishUniqueFileResource(image->filename);
1057 if (status != MagickFalse)
1058 {
1059 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1060 "DelegateFailed","`%s'",commands[i]);
1061 break;
1062 }
1063 commands[i]=DestroyString(commands[i]);
1064 }
1065 (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1066 (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1067 /*
1068 Relinquish resources.
1069 */
1070 for ( ; commands[i] != (char *) NULL; i++)
1071 commands[i]=DestroyString(commands[i]);
1072 commands=(char **) RelinquishMagickMemory(commands);
1073 if (temporary != MagickFalse)
1074 (void) RelinquishUniqueFileResource(image->filename);
1075 return(status == MagickFalse ? MagickTrue : MagickFalse);
1076}
1077
1078/*
1079%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1080% %
1081% %
1082% %
1083% L i s t D e l e g a t e I n f o %
1084% %
1085% %
1086% %
1087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088%
1089% ListDelegateInfo() lists the image formats to a file.
1090%
1091% The format of the ListDelegateInfo method is:
1092%
1093% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1094%
1095% A description of each parameter follows.
1096%
1097% o file: An pointer to a FILE.
1098%
1099% o exception: return any errors or warnings in this structure.
1100%
1101*/
1102MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1103 ExceptionInfo *exception)
1104{
1105 const DelegateInfo
1106 **delegate_info;
1107
1108 char
1109 **commands,
1110 delegate[MaxTextExtent];
1111
1112 const char
1113 *path;
1114
cristybb503372010-05-27 20:51:26 +00001115 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001116 i;
1117
cristybb503372010-05-27 20:51:26 +00001118 size_t
cristy3ed852e2009-09-05 21:47:34 +00001119 number_delegates;
1120
cristy9d314ff2011-03-09 01:30:28 +00001121 ssize_t
1122 j;
1123
cristy3ed852e2009-09-05 21:47:34 +00001124 if (file == (const FILE *) NULL)
1125 file=stdout;
1126 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1127 if (delegate_info == (const DelegateInfo **) NULL)
1128 return(MagickFalse);
1129 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +00001130 for (i=0; i < (ssize_t) number_delegates; i++)
cristy3ed852e2009-09-05 21:47:34 +00001131 {
1132 if (delegate_info[i]->stealth != MagickFalse)
1133 continue;
1134 if ((path == (const char *) NULL) ||
1135 (LocaleCompare(path,delegate_info[i]->path) != 0))
1136 {
1137 if (delegate_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +00001138 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1139 (void) FormatLocaleFile(file,"Delegate Command\n");
cristy1e604812011-05-19 18:07:50 +00001140 (void) FormatLocaleFile(file,
1141 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +00001142 "------------------------------\n");
1143 }
1144 path=delegate_info[i]->path;
1145 *delegate='\0';
1146 if (delegate_info[i]->encode != (char *) NULL)
1147 (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1148 (void) ConcatenateMagickString(delegate," ",MaxTextExtent);
1149 delegate[8]='\0';
1150 commands=StringToList(delegate_info[i]->commands);
1151 if (commands == (char **) NULL)
1152 continue;
cristyb51dff52011-05-19 16:55:47 +00001153 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
cristy3ed852e2009-09-05 21:47:34 +00001154 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1155 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1156 StripString(commands[0]);
cristyb51dff52011-05-19 16:55:47 +00001157 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
cristy3ed852e2009-09-05 21:47:34 +00001158 for (j=1; commands[j] != (char *) NULL; j++)
1159 {
1160 StripString(commands[j]);
cristyb51dff52011-05-19 16:55:47 +00001161 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
cristy3ed852e2009-09-05 21:47:34 +00001162 }
1163 for (j=0; commands[j] != (char *) NULL; j++)
1164 commands[j]=DestroyString(commands[j]);
1165 commands=(char **) RelinquishMagickMemory(commands);
1166 }
1167 (void) fflush(file);
1168 delegate_info=(const DelegateInfo **)
1169 RelinquishMagickMemory((void *) delegate_info);
1170 return(MagickTrue);
1171}
1172
1173/*
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175% %
1176% %
1177% %
1178+ L o a d D e l e g a t e L i s t %
1179% %
1180% %
1181% %
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183%
1184% LoadDelegateList() loads the delegate configuration file which provides a
1185% mapping between delegate attributes and a delegate name.
1186%
1187% The format of the LoadDelegateList method is:
1188%
1189% MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001190% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001191%
1192% A description of each parameter follows:
1193%
1194% o xml: The delegate list in XML format.
1195%
1196% o filename: The delegate list filename.
1197%
1198% o depth: depth of <include /> statements.
1199%
1200% o exception: return any errors or warnings in this structure.
1201%
1202*/
1203static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001204 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001205{
1206 char
1207 keyword[MaxTextExtent],
1208 *token;
1209
1210 const char
1211 *q;
1212
1213 DelegateInfo
1214 *delegate_info;
1215
1216 MagickBooleanType
1217 status;
1218
1219 /*
1220 Load the delegate map file.
1221 */
1222 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1223 "Loading delegate configuration file \"%s\" ...",filename);
1224 if (xml == (const char *) NULL)
1225 return(MagickFalse);
1226 if (delegate_list == (LinkedListInfo *) NULL)
1227 {
1228 delegate_list=NewLinkedList(0);
1229 if (delegate_list == (LinkedListInfo *) NULL)
1230 {
1231 ThrowFileException(exception,ResourceLimitError,
1232 "MemoryAllocationFailed",filename);
1233 return(MagickFalse);
1234 }
1235 }
1236 status=MagickTrue;
1237 delegate_info=(DelegateInfo *) NULL;
1238 token=AcquireString(xml);
1239 for (q=(const char *) xml; *q != '\0'; )
1240 {
1241 /*
1242 Interpret XML.
1243 */
1244 GetMagickToken(q,&q,token);
1245 if (*token == '\0')
1246 break;
1247 (void) CopyMagickString(keyword,token,MaxTextExtent);
1248 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1249 {
1250 /*
1251 Doctype element.
1252 */
1253 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1254 GetMagickToken(q,&q,token);
1255 continue;
1256 }
1257 if (LocaleNCompare(keyword,"<!--",4) == 0)
1258 {
1259 /*
1260 Comment element.
1261 */
1262 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1263 GetMagickToken(q,&q,token);
1264 continue;
1265 }
1266 if (LocaleCompare(keyword,"<include") == 0)
1267 {
1268 /*
1269 Include element.
1270 */
1271 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1272 {
1273 (void) CopyMagickString(keyword,token,MaxTextExtent);
1274 GetMagickToken(q,&q,token);
1275 if (*token != '=')
1276 continue;
1277 GetMagickToken(q,&q,token);
1278 if (LocaleCompare(keyword,"file") == 0)
1279 {
1280 if (depth > 200)
1281 (void) ThrowMagickException(exception,GetMagickModule(),
1282 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1283 else
1284 {
1285 char
1286 path[MaxTextExtent],
1287 *xml;
1288
1289 GetPathComponent(filename,HeadPath,path);
1290 if (*path != '\0')
1291 (void) ConcatenateMagickString(path,DirectorySeparator,
1292 MaxTextExtent);
1293 if (*token == *DirectorySeparator)
1294 (void) CopyMagickString(path,token,MaxTextExtent);
1295 else
1296 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1297 xml=FileToString(path,~0,exception);
1298 if (xml != (char *) NULL)
1299 {
1300 status=LoadDelegateList(xml,path,depth+1,exception);
1301 xml=(char *) RelinquishMagickMemory(xml);
1302 }
1303 }
1304 }
1305 }
1306 continue;
1307 }
1308 if (LocaleCompare(keyword,"<delegate") == 0)
1309 {
1310 /*
1311 Delegate element.
1312 */
1313 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1314 sizeof(*delegate_info));
1315 if (delegate_info == (DelegateInfo *) NULL)
1316 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1317 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1318 delegate_info->path=ConstantString(filename);
1319 delegate_info->signature=MagickSignature;
1320 continue;
1321 }
1322 if (delegate_info == (DelegateInfo *) NULL)
1323 continue;
1324 if (LocaleCompare(keyword,"/>") == 0)
1325 {
1326 status=AppendValueToLinkedList(delegate_list,delegate_info);
1327 if (status == MagickFalse)
1328 (void) ThrowMagickException(exception,GetMagickModule(),
1329 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1330 delegate_info->commands);
1331 delegate_info=(DelegateInfo *) NULL;
1332 }
1333 GetMagickToken(q,(const char **) NULL,token);
1334 if (*token != '=')
1335 continue;
1336 GetMagickToken(q,&q,token);
1337 GetMagickToken(q,&q,token);
1338 switch (*keyword)
1339 {
1340 case 'C':
1341 case 'c':
1342 {
1343 if (LocaleCompare((char *) keyword,"command") == 0)
1344 {
1345 char
1346 *commands;
1347
1348 commands=AcquireString(token);
cristy0157aea2010-04-24 21:12:18 +00001349#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001350 if (strchr(commands,'@') != (char *) NULL)
1351 {
1352 char
1353 path[MaxTextExtent];
1354
1355 NTGhostscriptEXE(path,MaxTextExtent);
1356 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1357 path);
1358 (void) SubstituteString((char **) &commands,"\\","/");
1359 }
1360#endif
1361 (void) SubstituteString((char **) &commands,"&amp;","&");
1362 (void) SubstituteString((char **) &commands,"&quot;","\"");
1363 (void) SubstituteString((char **) &commands,"&gt;",">");
1364 (void) SubstituteString((char **) &commands,"&lt;","<");
1365 delegate_info->commands=commands;
1366 break;
1367 }
1368 break;
1369 }
1370 case 'D':
1371 case 'd':
1372 {
1373 if (LocaleCompare((char *) keyword,"decode") == 0)
1374 {
1375 delegate_info->decode=ConstantString(token);
1376 delegate_info->mode=1;
1377 break;
1378 }
1379 break;
1380 }
1381 case 'E':
1382 case 'e':
1383 {
1384 if (LocaleCompare((char *) keyword,"encode") == 0)
1385 {
1386 delegate_info->encode=ConstantString(token);
1387 delegate_info->mode=(-1);
1388 break;
1389 }
1390 break;
1391 }
1392 case 'M':
1393 case 'm':
1394 {
1395 if (LocaleCompare((char *) keyword,"mode") == 0)
1396 {
1397 delegate_info->mode=1;
1398 if (LocaleCompare(token,"bi") == 0)
1399 delegate_info->mode=0;
1400 else
1401 if (LocaleCompare(token,"encode") == 0)
1402 delegate_info->mode=(-1);
1403 break;
1404 }
1405 break;
1406 }
1407 case 'S':
1408 case 's':
1409 {
1410 if (LocaleCompare((char *) keyword,"spawn") == 0)
1411 {
1412 delegate_info->spawn=IsMagickTrue(token);
1413 break;
1414 }
1415 if (LocaleCompare((char *) keyword,"stealth") == 0)
1416 {
1417 delegate_info->stealth=IsMagickTrue(token);
1418 break;
1419 }
1420 break;
1421 }
1422 case 'T':
1423 case 't':
1424 {
1425 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1426 {
1427 delegate_info->thread_support=IsMagickTrue(token);
1428 break;
1429 }
1430 break;
1431 }
1432 default:
1433 break;
1434 }
1435 }
1436 token=(char *) RelinquishMagickMemory(token);
1437 return(status);
1438}
1439
1440/*
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442% %
1443% %
1444% %
1445% L o a d D e l e g a t e L i s t s %
1446% %
1447% %
1448% %
1449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450%
1451% LoadDelegateList() loads one or more delegate configuration file which
1452% provides a mapping between delegate attributes and a delegate name.
1453%
1454% The format of the LoadDelegateLists method is:
1455%
1456% MagickBooleanType LoadDelegateLists(const char *filename,
1457% ExceptionInfo *exception)
1458%
1459% A description of each parameter follows:
1460%
1461% o filename: the font file name.
1462%
1463% o exception: return any errors or warnings in this structure.
1464%
1465*/
1466static MagickBooleanType LoadDelegateLists(const char *filename,
1467 ExceptionInfo *exception)
1468{
cristy6e3607c2011-09-13 13:59:17 +00001469#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001470 return(LoadDelegateList(DelegateMap,"built-in",0,exception));
1471#else
1472 const StringInfo
1473 *option;
1474
1475 LinkedListInfo
1476 *options;
1477
1478 MagickStatusType
1479 status;
1480
1481 status=MagickFalse;
1482 options=GetConfigureOptions(filename,exception);
1483 option=(const StringInfo *) GetNextValueInLinkedList(options);
1484 while (option != (const StringInfo *) NULL)
1485 {
1486 status|=LoadDelegateList((const char *) GetStringInfoDatum(option),
1487 GetStringInfoPath(option),0,exception);
1488 option=(const StringInfo *) GetNextValueInLinkedList(options);
1489 }
1490 options=DestroyConfigureOptions(options);
1491 if ((delegate_list == (LinkedListInfo *) NULL) ||
1492 (IsLinkedListEmpty(delegate_list) != MagickFalse))
1493 status|=LoadDelegateList(DelegateMap,"built-in",0,exception);
1494 return(status != 0 ? MagickTrue : MagickFalse);
1495#endif
1496}