blob: 7f47f2245eae9dcff210df7989021ce1d8ac264b [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 %
cristyde984cd2013-12-01 14:49:27 +000015% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000016% October 1998 %
17% %
18% %
cristyb56bb242014-11-25 17:12:48 +000019% Copyright 1999-2015 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"
dirk007e9252015-01-15 21:02:57 +000059#include "MagickCore/image-private.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/list.h"
61#include "MagickCore/memory_.h"
cristy1e37e8f2014-02-21 17:05:37 +000062#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000063#include "MagickCore/policy.h"
64#include "MagickCore/resource_.h"
65#include "MagickCore/semaphore.h"
66#include "MagickCore/string_.h"
67#include "MagickCore/token.h"
68#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000069#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000070#include "MagickCore/xml-tree.h"
cristy3291f512014-03-16 22:16:22 +000071#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000072
73/*
74 Define declarations.
75*/
76#define DelegateFilename "delegates.xml"
77
78/*
79 Declare delegate map.
80*/
81static const char
82 *DelegateMap = (const char *)
83 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
84 "<delegatemap>"
85 " <delegate decode=\"autotrace\" stealth=\"True\" command=\"&quot;autotrace&quot; -output-format svg -output-file &quot;%o&quot; &quot;%i&quot;\"/>"
86 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\"&quot;mplayer&quot; &quot;%i&quot; -really-quiet -ao null -vo png:z=3\"/>"
anthonyb8071362012-05-23 03:56:25 +000087 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; http://www.imagemagick.org/; rm &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000088 " <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;\"/>"
cristy12e4d2e2015-01-20 17:00:03 +000089 " <delegate decode=\"dng:decode\" command=\"&quot;ufraw-batch&quot; --silent --create-id=also --out-type=png --out-depth=16 &quot;--output=%u.png&quot; &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000090 " <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +000091 " <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristya9c90a52015-01-05 23:23:35 +000092 " <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000093 " <delegate decode=\"fig\" command=\"&quot;fig2dev&quot; -L ps &quot;%i&quot; &quot;%o&quot;\"/>"
94 " <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 +000095 " <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;\"/>"
cristy7c278052012-11-18 01:09:35 +000096 " <delegate decode=\"hpgl\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot; mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000097 " <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
98 " <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
99 " <delegate decode=\"https\" command=\"&quot;wget&quot; -q -O &quot;%o&quot; &quot;https:%M&quot;\"/>"
100 " <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
101 " <delegate decode=\"man\" command=\"&quot;groff&quot; -man -Tps &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
cristy53798ff2015-05-08 18:28:24 +0000102 " <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -nostdin -v -1 -vframes %S -i &quot;%i&quot; -vcodec pam -an -f rawvideo -y &quot;%u.pam&quot; 2&gt; &quot;%Z&quot;\"/>"
103 " <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -nostdin -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i &quot;%M%%d.jpg&quot; &quot;%u.%m&quot; 2&gt; &quot;%Z&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000104 " <delegate decode=\"pcl:color\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ppmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
105 " <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
106 " <delegate decode=\"pcl:mono\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
cristya9c90a52015-01-05 23:23:35 +0000107 " <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
108 " <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000109 " <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\"&quot;ppmtoilbm&quot; -24if &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
110 " <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\"&quot;gimp&quot; &quot;%i&quot;\"/>"
111 " <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;\"/>"
cristya9c90a52015-01-05 23:23:35 +0000112 " <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000113 " <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000114 " <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr &quot;%i&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000115 " <delegate decode=\"ps:alpha\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &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;\"/>"
116 " <delegate decode=\"ps:bbox\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &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;\"/>"
117 " <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &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;\"/>"
118 " <delegate decode=\"ps:color\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &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;\"/>"
119 " <delegate decode=\"ps:mono\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &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 +0000120 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\"&quot;rawtorle&quot; -o &quot;%o&quot; -v &quot;%i&quot;\"/>"
121 " <delegate decode=\"scan\" command=\"&quot;scanimage&quot; -d &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
anthonyb8071362012-05-23 03:56:25 +0000122 " <delegate encode=\"show\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000123 " <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
cristy4689cf02010-02-17 21:15:45 +0000124 " <delegate decode=\"svg\" command=\"&quot;rsvg&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000125 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\"&quot;enscript&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
anthonyb8071362012-05-23 03:56:25 +0000126 " <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000127 " <delegate decode=\"wmf\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
128 "</delegatemap>";
129
130/*
131 Global declaractions.
132*/
133static LinkedListInfo
cristy86e5ac92014-03-16 19:27:39 +0000134 *delegate_cache = (LinkedListInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000135
136static SemaphoreInfo
137 *delegate_semaphore = (SemaphoreInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000138
139/*
140 Forward declaractions.
141*/
142static MagickBooleanType
cristy86e5ac92014-03-16 19:27:39 +0000143 IsDelegateCacheInstantiated(ExceptionInfo *),
cristycd2cd182014-03-18 12:10:55 +0000144 LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
145 ExceptionInfo *);
cristy86e5ac92014-03-16 19:27:39 +0000146
147/*
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149% %
150% %
151% %
152% A c q u i r e D e l e g a t e C a c h e %
153% %
154% %
155% %
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157%
158% AcquireDelegateCache() caches one or more delegate configurations which
159% provides a mapping between delegate attributes and a delegate name.
160%
161% The format of the AcquireDelegateCache method is:
162%
163% LinkedListInfo *AcquireDelegateCache(const char *filename,
164% ExceptionInfo *exception)
165%
166% A description of each parameter follows:
167%
168% o filename: the font file name.
169%
170% o exception: return any errors or warnings in this structure.
171%
172*/
173static LinkedListInfo *AcquireDelegateCache(const char *filename,
174 ExceptionInfo *exception)
175{
cristy86e5ac92014-03-16 19:27:39 +0000176 LinkedListInfo
cristy66f8c5f2014-03-17 00:12:14 +0000177 *delegate_cache;
cristy86e5ac92014-03-16 19:27:39 +0000178
179 MagickStatusType
180 status;
181
182 delegate_cache=NewLinkedList(0);
183 if (delegate_cache == (LinkedListInfo *) NULL)
184 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
185 status=MagickTrue;
cristy191ba5c2014-03-16 21:26:40 +0000186#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
cristy86e5ac92014-03-16 19:27:39 +0000187 {
cristy66f8c5f2014-03-17 00:12:14 +0000188 const StringInfo
189 *option;
190
191 LinkedListInfo
192 *options;
193
194 options=GetConfigureOptions(filename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000195 option=(const StringInfo *) GetNextValueInLinkedList(options);
cristy66f8c5f2014-03-17 00:12:14 +0000196 while (option != (const StringInfo *) NULL)
197 {
cristycd2cd182014-03-18 12:10:55 +0000198 status&=LoadDelegateCache(delegate_cache,(const char *)
199 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy66f8c5f2014-03-17 00:12:14 +0000200 option=(const StringInfo *) GetNextValueInLinkedList(options);
201 }
202 options=DestroyConfigureOptions(options);
cristy86e5ac92014-03-16 19:27:39 +0000203 }
cristy191ba5c2014-03-16 21:26:40 +0000204#endif
cristy191ba5c2014-03-16 21:26:40 +0000205 if (IfMagickTrue(IsLinkedListEmpty(delegate_cache)))
cristycd2cd182014-03-18 12:10:55 +0000206 status&=LoadDelegateCache(delegate_cache,DelegateMap,"built-in",0,
207 exception);
cristy86e5ac92014-03-16 19:27:39 +0000208 return(delegate_cache);
cristy86e5ac92014-03-16 19:27:39 +0000209}
cristy3ed852e2009-09-05 21:47:34 +0000210
211/*
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% %
214% %
215% %
cristy0d8101e2014-04-10 00:13:05 +0000216+ D e l e g a t e C o m p o n e n t G e n e s i s %
cristy3ed852e2009-09-05 21:47:34 +0000217% %
218% %
219% %
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%
cristyf34a1452009-10-24 22:29:27 +0000222% DelegateComponentGenesis() instantiates the delegate component.
cristy3ed852e2009-09-05 21:47:34 +0000223%
cristyf34a1452009-10-24 22:29:27 +0000224% The format of the DelegateComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000225%
cristyf34a1452009-10-24 22:29:27 +0000226% MagickBooleanType DelegateComponentGenesis(void)
227%
228*/
cristy5ff4eaf2011-09-03 01:38:02 +0000229MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000230{
cristy7c977062014-04-04 14:05:53 +0000231 if (delegate_semaphore == (SemaphoreInfo *) NULL)
232 delegate_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000233 return(MagickTrue);
234}
235
236/*
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238% %
239% %
240% %
241% 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 %
242% %
243% %
244% %
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246%
247% DelegateComponentTerminus() destroys the delegate component.
248%
249% The format of the DelegateComponentTerminus method is:
250%
251% DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000252%
253*/
254
255static void *DestroyDelegate(void *delegate_info)
256{
257 register DelegateInfo
258 *p;
259
260 p=(DelegateInfo *) delegate_info;
261 if (p->path != (char *) NULL)
262 p->path=DestroyString(p->path);
263 if (p->decode != (char *) NULL)
264 p->decode=DestroyString(p->decode);
265 if (p->encode != (char *) NULL)
266 p->encode=DestroyString(p->encode);
267 if (p->commands != (char *) NULL)
268 p->commands=DestroyString(p->commands);
cristy191ba5c2014-03-16 21:26:40 +0000269 if (p->semaphore != (SemaphoreInfo *) NULL)
cristy2f62da42014-03-19 21:57:49 +0000270 RelinquishSemaphoreInfo(&p->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000271 p=(DelegateInfo *) RelinquishMagickMemory(p);
272 return((void *) NULL);
273}
274
cristy5ff4eaf2011-09-03 01:38:02 +0000275MagickPrivate void DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000276{
cristy18b17442009-10-25 18:36:48 +0000277 if (delegate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000278 ActivateSemaphoreInfo(&delegate_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000279 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000280 if (delegate_cache != (LinkedListInfo *) NULL)
281 delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
cristyf84a1932010-01-03 18:00:18 +0000282 UnlockSemaphoreInfo(delegate_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000283 RelinquishSemaphoreInfo(&delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000284}
285
286/*
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288% %
289% %
290% %
cristydfc19b62014-10-17 22:52:24 +0000291+ E x t e r n a l D e l e g a t e C o m m a n d %
292% %
293% %
294% %
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%
297% ExternalDelegateCommand() executes the specified command and waits until it
298% terminates. The returned value is the exit status of the command.
299%
300% The format of the ExternalDelegateCommand method is:
301%
302% int ExternalDelegateCommand(const MagickBooleanType asynchronous,
303% const MagickBooleanType verbose,const char *command,
304% char *message,ExceptionInfo *exception)
305%
306% A description of each parameter follows:
307%
308% o asynchronous: a value other than 0 executes the parent program
309% concurrently with the new child process.
310%
311% o verbose: a value other than 0 prints the executed command before it is
312% invoked.
313%
314% o command: this string is the command to execute.
315%
316% o message: an option buffer to receive any message posted to stdout or
317% stderr.
318%
319% o exception: return any errors here.
320%
321*/
322
323static char *SanitizeDelegateCommand(const char *command)
324{
325 char
326 *sanitize_command;
327
328 const char
329 *q;
330
331 register char
332 *p;
333
334 static char
335 whitelist[] =
336 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_- "
cristyd9fc2322015-06-06 00:13:14 +0000337 ".@&;<>()|/\\\'\":%=~`";
cristydfc19b62014-10-17 22:52:24 +0000338
339 sanitize_command=AcquireString(command);
340 p=sanitize_command;
341 q=sanitize_command+strlen(sanitize_command);
342 for (p+=strspn(p,whitelist); p != q; p+=strspn(p,whitelist))
343 *p='_';
344 return(sanitize_command);
345}
346
dirk457d9762014-10-20 19:58:07 +0000347MagickExport int ExternalDelegateCommand(const MagickBooleanType asynchronous,
cristydfc19b62014-10-17 22:52:24 +0000348 const MagickBooleanType verbose,const char *command,char *message,
349 ExceptionInfo *exception)
350{
351 char
352 **arguments,
353 *sanitize_command;
354
355 int
356 number_arguments,
357 status;
358
359 PolicyDomain
360 domain;
361
362 PolicyRights
363 rights;
364
365 register ssize_t
366 i;
367
368 status=(-1);
369 arguments=StringToArgv(command,&number_arguments);
370 if (arguments == (char **) NULL)
371 return(status);
372 if (*arguments[1] == '\0')
373 {
374 for (i=0; i < (ssize_t) number_arguments; i++)
375 arguments[i]=DestroyString(arguments[i]);
376 arguments=(char **) RelinquishMagickMemory(arguments);
377 return(-1);
378 }
379 rights=ExecutePolicyRights;
380 domain=DelegatePolicyDomain;
381 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
382 {
383 errno=EPERM;
384 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
385 "NotAuthorized","`%s'",arguments[1]);
386 for (i=0; i < (ssize_t) number_arguments; i++)
387 arguments[i]=DestroyString(arguments[i]);
388 arguments=(char **) RelinquishMagickMemory(arguments);
389 return(-1);
390 }
391 if (verbose != MagickFalse)
392 {
393 (void) FormatLocaleFile(stderr,"%s\n",command);
394 (void) fflush(stderr);
395 }
396 sanitize_command=SanitizeDelegateCommand(command);
397 if (asynchronous != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000398 (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
cristydfc19b62014-10-17 22:52:24 +0000399 if (message != (char *) NULL)
400 *message='\0';
401#if defined(MAGICKCORE_POSIX_SUPPORT)
402#if !defined(MAGICKCORE_HAVE_EXECVP)
403 status=system(sanitize_command);
404#else
405 if ((asynchronous != MagickFalse) ||
406 (strpbrk(sanitize_command,"&;<>|") != (char *) NULL))
407 status=system(sanitize_command);
408 else
409 {
410 pid_t
411 child_pid;
412
413 /*
414 Call application directly rather than from a shell.
415 */
416 child_pid=(pid_t) fork();
417 if (child_pid == (pid_t) -1)
418 status=system(sanitize_command);
419 else
420 if (child_pid == 0)
421 {
422 status=execvp(arguments[1],arguments+1);
423 _exit(1);
424 }
425 else
426 {
427 int
428 child_status;
429
430 pid_t
431 pid;
432
433 child_status=0;
434 pid=(pid_t) waitpid(child_pid,&child_status,0);
435 if (pid == -1)
436 status=(-1);
437 else
438 {
439 if (WIFEXITED(child_status) != 0)
440 status=WEXITSTATUS(child_status);
441 else
442 if (WIFSIGNALED(child_status))
443 status=(-1);
444 }
445 }
446 }
447#endif
448#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
dirkeb0db702015-02-12 22:17:21 +0000449 {
450 register char
451 *p;
452
453 /*
454 If a command shell is executed we need to change the forward slashes in
455 files to a backslash. We need to do this to keep Windows happy when we
456 want to 'move' a file.
457
458 TODO: This won't work if one of the delegate parameters has a forward
459 slash as aparameter.
460 */
461 p=strstr(sanitize_command, "cmd.exe /c");
462 if (p != (char*) NULL)
463 {
464 p+=10;
465 for (; *p != '\0'; p++)
466 if (*p == '/')
467 *p=*DirectorySeparator;
468 }
469 }
cristydfc19b62014-10-17 22:52:24 +0000470 status=NTSystemCommand(sanitize_command,message);
471#elif defined(macintosh)
472 status=MACSystemCommand(sanitize_command);
473#elif defined(vms)
474 status=system(sanitize_command);
475#else
476# error No suitable system() method.
477#endif
478 if (status < 0)
479 {
480 if ((message != (char *) NULL) && (*message != '\0'))
481 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
dirkeb0db702015-02-12 22:17:21 +0000482 "FailedToExecuteCommand","`%s' (%s)",sanitize_command,message);
cristydfc19b62014-10-17 22:52:24 +0000483 else
484 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
dirkeb0db702015-02-12 22:17:21 +0000485 "FailedToExecuteCommand","`%s' (%d)",sanitize_command,status);
cristydfc19b62014-10-17 22:52:24 +0000486 }
487 sanitize_command=DestroyString(sanitize_command);
488 for (i=0; i < (ssize_t) number_arguments; i++)
489 arguments[i]=DestroyString(arguments[i]);
490 arguments=(char **) RelinquishMagickMemory(arguments);
491 return(status);
492}
493
494/*
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496% %
497% %
498% %
cristy3ed852e2009-09-05 21:47:34 +0000499% G e t D e l e g a t e C o m m a n d %
500% %
501% %
502% %
503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504%
505% GetDelegateCommand() replaces any embedded formatting characters with the
506% appropriate image attribute and returns the resulting command.
507%
508% The format of the GetDelegateCommand method is:
509%
510% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
511% const char *decode,const char *encode,ExceptionInfo *exception)
512%
513% A description of each parameter follows:
514%
515% o command: Method GetDelegateCommand returns the command associated
516% with specified delegate tag.
517%
518% o image_info: the image info.
519%
520% o image: the image.
521%
522% o decode: Specifies the decode delegate we are searching for as a
523% character string.
524%
525% o encode: Specifies the encode delegate we are searching for as a
526% character string.
527%
528% o exception: return any errors or warnings in this structure.
529%
530*/
531MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
532 const char *decode,const char *encode,ExceptionInfo *exception)
533{
534 char
535 *command,
536 **commands;
537
538 const DelegateInfo
539 *delegate_info;
540
cristybb503372010-05-27 20:51:26 +0000541 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000542 i;
543
544 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000545 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000546 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000547 assert(image->signature == MagickCoreSignature);
cristycb190b72014-08-31 20:04:42 +0000548 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000549 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
anthony59c44432012-05-23 04:41:25 +0000550
cristy3ed852e2009-09-05 21:47:34 +0000551 delegate_info=GetDelegateInfo(decode,encode,exception);
552 if (delegate_info == (const DelegateInfo *) NULL)
553 {
554 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +0000555 "NoTagFound","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +0000556 return((char *) NULL);
557 }
558 commands=StringToList(delegate_info->commands);
559 if (commands == (char **) NULL)
560 {
561 (void) ThrowMagickException(exception,GetMagickModule(),
cristy014ead12013-04-29 08:47:36 +0000562 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
563 encode);
cristy3ed852e2009-09-05 21:47:34 +0000564 return((char *) NULL);
565 }
cristy014ead12013-04-29 08:47:36 +0000566 command=InterpretImageProperties((ImageInfo *) image_info,image,commands[0],
567 exception);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (command == (char *) NULL)
569 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
cristyefe601c2013-01-05 17:51:12 +0000570 "MemoryAllocationFailed","`%s'",commands[0]);
cristy3ed852e2009-09-05 21:47:34 +0000571 /*
572 Relinquish resources.
573 */
574 for (i=0; commands[i] != (char *) NULL; i++)
575 commands[i]=DestroyString(commands[i]);
576 commands=(char **) RelinquishMagickMemory(commands);
577 return(command);
578}
579
580/*
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582% %
583% %
584% %
585% G e t D e l e g a t e C o m m a n d s %
586% %
587% %
588% %
589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590%
591% GetDelegateCommands() returns the commands associated with a delegate.
592%
593% The format of the GetDelegateCommands method is:
594%
595% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
596%
597% A description of each parameter follows:
598%
599% o delegate_info: The delegate info.
600%
601*/
602MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
603{
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +0000605
cristy3ed852e2009-09-05 21:47:34 +0000606 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000607 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000608 return(delegate_info->commands);
609}
610
611/*
612%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613% %
614% %
615% %
616% G e t D e l e g a t e I n f o %
617% %
618% %
619% %
620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621%
622% GetDelegateInfo() returns any delegates associated with the specified tag.
623%
624% The format of the GetDelegateInfo method is:
625%
626% const DelegateInfo *GetDelegateInfo(const char *decode,
627% const char *encode,ExceptionInfo *exception)
628%
629% A description of each parameter follows:
630%
631% o decode: Specifies the decode delegate we are searching for as a
632% character string.
633%
634% o encode: Specifies the encode delegate we are searching for as a
635% character string.
636%
637% o exception: return any errors or warnings in this structure.
638%
639*/
640MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
641 const char *encode,ExceptionInfo *exception)
642{
643 register const DelegateInfo
644 *p;
645
646 assert(exception != (ExceptionInfo *) NULL);
cristycd2cd182014-03-18 12:10:55 +0000647 if (IfMagickFalse(IsDelegateCacheInstantiated(exception)))
648 return((const DelegateInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000649 /*
650 Search for named delegate.
651 */
cristyf84a1932010-01-03 18:00:18 +0000652 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000653 ResetLinkedListIterator(delegate_cache);
654 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy49d4d222014-03-16 00:37:58 +0000655 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
656 {
657 UnlockSemaphoreInfo(delegate_semaphore);
658 return(p);
659 }
cristy3ed852e2009-09-05 21:47:34 +0000660 while (p != (const DelegateInfo *) NULL)
661 {
662 if (p->mode > 0)
663 {
664 if (LocaleCompare(p->decode,decode) == 0)
665 break;
cristy86e5ac92014-03-16 19:27:39 +0000666 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000667 continue;
668 }
669 if (p->mode < 0)
670 {
671 if (LocaleCompare(p->encode,encode) == 0)
672 break;
cristy86e5ac92014-03-16 19:27:39 +0000673 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000674 continue;
675 }
676 if (LocaleCompare(decode,p->decode) == 0)
677 if (LocaleCompare(encode,p->encode) == 0)
678 break;
679 if (LocaleCompare(decode,"*") == 0)
680 if (LocaleCompare(encode,p->encode) == 0)
681 break;
682 if (LocaleCompare(decode,p->decode) == 0)
683 if (LocaleCompare(encode,"*") == 0)
684 break;
cristy86e5ac92014-03-16 19:27:39 +0000685 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000686 }
687 if (p != (const DelegateInfo *) NULL)
cristy86e5ac92014-03-16 19:27:39 +0000688 (void) InsertValueInLinkedList(delegate_cache,0,
689 RemoveElementByValueFromLinkedList(delegate_cache,p));
cristyf84a1932010-01-03 18:00:18 +0000690 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000691 return(p);
692}
693
694/*
695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696% %
697% %
698% %
699% G e t D e l e g a t e I n f o L i s t %
700% %
701% %
702% %
703%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704%
705% GetDelegateInfoList() returns any delegates that match the specified pattern.
706%
707% The delegate of the GetDelegateInfoList function is:
708%
709% const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000710% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000711%
712% A description of each parameter follows:
713%
714% o pattern: Specifies a pointer to a text string containing a pattern.
715%
716% o number_delegates: This integer returns the number of delegates in the
717% list.
718%
719% o exception: return any errors or warnings in this structure.
720%
721*/
722
723#if defined(__cplusplus) || defined(c_plusplus)
724extern "C" {
725#endif
726
727static int DelegateInfoCompare(const void *x,const void *y)
728{
729 const DelegateInfo
730 **p,
731 **q;
732
dirkf0dedd82014-12-16 19:56:13 +0000733 int
734 cmp;
735
cristy3ed852e2009-09-05 21:47:34 +0000736 p=(const DelegateInfo **) x,
737 q=(const DelegateInfo **) y;
dirkf0dedd82014-12-16 19:56:13 +0000738 cmp=LocaleCompare((*p)->path,(*q)->path);
739 if (cmp == 0)
cristy3ed852e2009-09-05 21:47:34 +0000740 {
741 if ((*p)->decode == (char *) NULL)
742 if (((*p)->encode != (char *) NULL) &&
743 ((*q)->encode != (char *) NULL))
744 return(strcmp((*p)->encode,(*q)->encode));
745 if (((*p)->decode != (char *) NULL) &&
746 ((*q)->decode != (char *) NULL))
747 return(strcmp((*p)->decode,(*q)->decode));
748 }
dirkf0dedd82014-12-16 19:56:13 +0000749 return(cmp);
cristy3ed852e2009-09-05 21:47:34 +0000750}
751
752#if defined(__cplusplus) || defined(c_plusplus)
753}
754#endif
755
756MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000757 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000758{
759 const DelegateInfo
760 **delegates;
761
762 register const DelegateInfo
763 *p;
764
cristybb503372010-05-27 20:51:26 +0000765 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000766 i;
767
768 /*
769 Allocate delegate list.
770 */
anthony59c44432012-05-23 04:41:25 +0000771 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000772 assert(pattern != (char *) NULL);
773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
anthony59c44432012-05-23 04:41:25 +0000774
cristy3ed852e2009-09-05 21:47:34 +0000775 *number_delegates=0;
776 p=GetDelegateInfo("*","*",exception);
777 if (p == (const DelegateInfo *) NULL)
778 return((const DelegateInfo **) NULL);
779 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000780 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
cristy3ed852e2009-09-05 21:47:34 +0000781 if (delegates == (const DelegateInfo **) NULL)
782 return((const DelegateInfo **) NULL);
783 /*
784 Generate delegate list.
785 */
cristyf84a1932010-01-03 18:00:18 +0000786 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000787 ResetLinkedListIterator(delegate_cache);
788 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000789 for (i=0; p != (const DelegateInfo *) NULL; )
790 {
anthony59c44432012-05-23 04:41:25 +0000791 if( IfMagickFalse(p->stealth) &&
792 ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
793 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
cristy3ed852e2009-09-05 21:47:34 +0000794 delegates[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000795 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000796 }
cristyf84a1932010-01-03 18:00:18 +0000797 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000798 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
799 delegates[i]=(DelegateInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000800 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000801 return(delegates);
802}
803
804/*
805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806% %
807% %
808% %
809% G e t D e l e g a t e L i s t %
810% %
811% %
812% %
813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814%
815% GetDelegateList() returns any image format delegates that match the
816% specified pattern.
817%
818% The format of the GetDelegateList function is:
819%
820% char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000821% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000822%
823% A description of each parameter follows:
824%
825% o pattern: Specifies a pointer to a text string containing a pattern.
826%
827% o number_delegates: This integer returns the number of delegates
828% in the list.
829%
830% o exception: return any errors or warnings in this structure.
831%
832*/
833
834#if defined(__cplusplus) || defined(c_plusplus)
835extern "C" {
836#endif
837
838static int DelegateCompare(const void *x,const void *y)
839{
840 register const char
841 **p,
842 **q;
843
844 p=(const char **) x;
845 q=(const char **) y;
846 return(LocaleCompare(*p,*q));
847}
848
849#if defined(__cplusplus) || defined(c_plusplus)
850}
851#endif
852
853MagickExport char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000854 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000855{
856 char
857 **delegates;
858
859 register const DelegateInfo
860 *p;
861
cristybb503372010-05-27 20:51:26 +0000862 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000863 i;
864
865 /*
866 Allocate delegate list.
867 */
868 assert(pattern != (char *) NULL);
869 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
anthony59c44432012-05-23 04:41:25 +0000870
cristybb503372010-05-27 20:51:26 +0000871 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000872 *number_delegates=0;
873 p=GetDelegateInfo("*","*",exception);
874 if (p == (const DelegateInfo *) NULL)
875 return((char **) NULL);
876 delegates=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000877 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
cristy3ed852e2009-09-05 21:47:34 +0000878 if (delegates == (char **) NULL)
879 return((char **) NULL);
cristyf84a1932010-01-03 18:00:18 +0000880 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000881 ResetLinkedListIterator(delegate_cache);
882 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000883 for (i=0; p != (const DelegateInfo *) NULL; )
884 {
anthony59c44432012-05-23 04:41:25 +0000885 if( IfMagickFalse(p->stealth) &&
886 IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) )
cristy3ed852e2009-09-05 21:47:34 +0000887 delegates[i++]=ConstantString(p->decode);
anthony59c44432012-05-23 04:41:25 +0000888 if( IfMagickFalse(p->stealth) &&
889 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
cristy3ed852e2009-09-05 21:47:34 +0000890 delegates[i++]=ConstantString(p->encode);
cristy86e5ac92014-03-16 19:27:39 +0000891 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +0000892 }
cristyf84a1932010-01-03 18:00:18 +0000893 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000894 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
895 delegates[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000896 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000897 return(delegates);
898}
899
900/*
901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902% %
903% %
904% %
905% G e t D e l e g a t e M o d e %
906% %
907% %
908% %
909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910%
911% GetDelegateMode() returns the mode of the delegate.
912%
913% The format of the GetDelegateMode method is:
914%
cristybb503372010-05-27 20:51:26 +0000915% ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +0000916%
917% A description of each parameter follows:
918%
919% o delegate_info: The delegate info.
920%
921*/
cristybb503372010-05-27 20:51:26 +0000922MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +0000923{
924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +0000925
cristy3ed852e2009-09-05 21:47:34 +0000926 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000927 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000928 return(delegate_info->mode);
929}
930
931/*
932%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933% %
934% %
935% %
936+ G e t D e l e g a t e T h r e a d S u p p o r t %
937% %
938% %
939% %
940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941%
942% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
943% threads.
944%
945% The format of the GetDelegateThreadSupport method is:
946%
947% MagickBooleanType GetDelegateThreadSupport(
948% const DelegateInfo *delegate_info)
949%
950% A description of each parameter follows:
951%
952% o delegate_info: The delegate info.
953%
954*/
955MagickExport MagickBooleanType GetDelegateThreadSupport(
956 const DelegateInfo *delegate_info)
957{
958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +0000959
cristy3ed852e2009-09-05 21:47:34 +0000960 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000961 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000962 return(delegate_info->thread_support);
963}
964
965/*
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967% %
968% %
969% %
cristy86e5ac92014-03-16 19:27:39 +0000970+ I s D e l e g a t e C a c h e I n s t a n t i a t e d %
cristy3ed852e2009-09-05 21:47:34 +0000971% %
972% %
973% %
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975%
cristy10e530d2014-03-19 13:39:13 +0000976% IsDelegateCacheInstantiated() determines if the delegate cache is
977% instantiated. If not, it instantiates the cache and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000978%
cristy904e5912014-03-15 19:53:14 +0000979% The format of the IsDelegateInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000980%
cristy86e5ac92014-03-16 19:27:39 +0000981% MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000982%
983% A description of each parameter follows.
984%
985% o exception: return any errors or warnings in this structure.
986%
987*/
cristy86e5ac92014-03-16 19:27:39 +0000988static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000989{
cristy86e5ac92014-03-16 19:27:39 +0000990 if (delegate_cache == (LinkedListInfo *) NULL)
991 {
992 if (delegate_semaphore == (SemaphoreInfo *) NULL)
993 ActivateSemaphoreInfo(&delegate_semaphore);
994 LockSemaphoreInfo(delegate_semaphore);
995 if (delegate_cache == (LinkedListInfo *) NULL)
996 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
997 UnlockSemaphoreInfo(delegate_semaphore);
998 }
dirk972c2e42015-07-25 19:23:33 +0000999 return(delegate_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001000}
1001
1002/*
1003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004% %
1005% %
1006% %
1007% I n v o k e D e l e g a t e %
1008% %
1009% %
1010% %
1011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012%
1013% InvokeDelegate replaces any embedded formatting characters with the
1014% appropriate image attribute and executes the resulting command. MagickFalse
1015% is returned if the commands execute with success otherwise MagickTrue.
1016%
1017% The format of the InvokeDelegate method is:
1018%
1019% MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
1020% const char *decode,const char *encode,ExceptionInfo *exception)
1021%
1022% A description of each parameter follows:
1023%
1024% o image_info: the imageInfo.
1025%
1026% o image: the image.
1027%
1028% o exception: return any errors or warnings in this structure.
1029%
1030*/
1031
cristy3ed852e2009-09-05 21:47:34 +00001032static MagickBooleanType CopyDelegateFile(const char *source,
cristy4886c4f2015-06-06 15:26:09 +00001033 const char *destination,const MagickBooleanType overwrite)
cristy3ed852e2009-09-05 21:47:34 +00001034{
1035 int
1036 destination_file,
1037 source_file;
1038
cristy53ac66f2012-03-11 02:56:37 +00001039 MagickBooleanType
1040 status;
1041
cristy3ed852e2009-09-05 21:47:34 +00001042 register size_t
1043 i;
1044
1045 size_t
1046 length,
1047 quantum;
1048
1049 ssize_t
1050 count;
1051
1052 struct stat
1053 attributes;
1054
1055 unsigned char
1056 *buffer;
1057
1058 /*
cristy2dfb1df2012-02-25 14:16:09 +00001059 Copy source file to destination.
cristy3ed852e2009-09-05 21:47:34 +00001060 */
1061 assert(source != (const char *) NULL);
1062 assert(destination != (char *) NULL);
cristy4886c4f2015-06-06 15:26:09 +00001063 if (overwrite == MagickFalse)
1064 {
1065 status=GetPathAttributes(destination,&attributes);
1066 if (status != MagickFalse)
1067 return(MagickTrue);
1068 }
cristy18c6c272011-09-23 14:40:37 +00001069 destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
cristy3ed852e2009-09-05 21:47:34 +00001070 if (destination_file == -1)
1071 return(MagickFalse);
cristy18c6c272011-09-23 14:40:37 +00001072 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
cristy3ed852e2009-09-05 21:47:34 +00001073 if (source_file == -1)
1074 {
1075 (void) close(destination_file);
1076 return(MagickFalse);
1077 }
1078 quantum=(size_t) MagickMaxBufferExtent;
cristyf201ba62015-07-05 13:54:28 +00001079 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
cristy3ed852e2009-09-05 21:47:34 +00001080 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1081 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1082 if (buffer == (unsigned char *) NULL)
1083 {
1084 (void) close(source_file);
1085 (void) close(destination_file);
1086 return(MagickFalse);
1087 }
1088 length=0;
1089 for (i=0; ; i+=count)
1090 {
1091 count=(ssize_t) read(source_file,buffer,quantum);
1092 if (count <= 0)
1093 break;
1094 length=(size_t) count;
1095 count=(ssize_t) write(destination_file,buffer,length);
1096 if ((size_t) count != length)
1097 break;
1098 }
1099 (void) close(destination_file);
1100 (void) close(source_file);
1101 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
anthony59c44432012-05-23 04:41:25 +00001102 return(IsMagickTrue(i!=0));
cristy3ed852e2009-09-05 21:47:34 +00001103}
1104
1105MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1106 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1107{
1108 char
1109 *command,
1110 **commands,
cristy151b66d2015-04-15 10:50:31 +00001111 input_filename[MagickPathExtent],
1112 output_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001113
1114 const DelegateInfo
1115 *delegate_info;
1116
1117 MagickBooleanType
1118 status,
1119 temporary;
1120
cristybb503372010-05-27 20:51:26 +00001121 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001122 i;
1123
1124 PolicyRights
1125 rights;
1126
1127 /*
1128 Get delegate.
1129 */
1130 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001131 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001132 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001133 assert(image->signature == MagickCoreSignature);
cristycb190b72014-08-31 20:04:42 +00001134 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
anthony59c44432012-05-23 04:41:25 +00001136
cristy3ed852e2009-09-05 21:47:34 +00001137 rights=ExecutePolicyRights;
anthony59c44432012-05-23 04:41:25 +00001138 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,decode)) )
cristy3ed852e2009-09-05 21:47:34 +00001139 {
cristya9197f62010-01-12 02:23:34 +00001140 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001141 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
cristyefe601c2013-01-05 17:51:12 +00001142 "NotAuthorized","`%s'",decode);
cristy3ed852e2009-09-05 21:47:34 +00001143 return(MagickFalse);
1144 }
anthony59c44432012-05-23 04:41:25 +00001145 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,encode)) )
cristy3ed852e2009-09-05 21:47:34 +00001146 {
cristya9197f62010-01-12 02:23:34 +00001147 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001148 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
cristyefe601c2013-01-05 17:51:12 +00001149 "NotAuthorized","`%s'",encode);
cristy3ed852e2009-09-05 21:47:34 +00001150 return(MagickFalse);
1151 }
anthony59c44432012-05-23 04:41:25 +00001152 temporary=IsMagickTrue(*image->filename == '\0');
1153 if( IfMagickTrue(temporary) )
1154 if( IfMagickFalse(AcquireUniqueFilename(image->filename)) )
cristy3ed852e2009-09-05 21:47:34 +00001155 {
1156 ThrowFileException(exception,FileOpenError,
1157 "UnableToCreateTemporaryFile",image->filename);
1158 return(MagickFalse);
1159 }
1160 delegate_info=GetDelegateInfo(decode,encode,exception);
1161 if (delegate_info == (DelegateInfo *) NULL)
1162 {
anthony59c44432012-05-23 04:41:25 +00001163 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001164 (void) RelinquishUniqueFileResource(image->filename);
1165 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +00001166 "NoTagFound","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001167 return(MagickFalse);
1168 }
1169 if (*image_info->filename == '\0')
1170 {
anthony59c44432012-05-23 04:41:25 +00001171 if( IfMagickFalse(AcquireUniqueFilename(image_info->filename)) )
cristy3ed852e2009-09-05 21:47:34 +00001172 {
anthony59c44432012-05-23 04:41:25 +00001173 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001174 (void) RelinquishUniqueFileResource(image->filename);
1175 ThrowFileException(exception,FileOpenError,
1176 "UnableToCreateTemporaryFile",image_info->filename);
1177 return(MagickFalse);
1178 }
1179 image_info->temporary=MagickTrue;
1180 }
cristyfc7f59b2011-09-16 00:41:11 +00001181 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
cristy3ed852e2009-09-05 21:47:34 +00001182 (delegate_info->encode != (char *) NULL)) ||
1183 ((encode != (const char *) NULL) &&
1184 (delegate_info->decode != (char *) NULL))))
1185 {
1186 char
1187 *magick;
1188
1189 ImageInfo
1190 *clone_info;
1191
1192 register Image
1193 *p;
1194
1195 /*
1196 Delegate requires a particular image format.
1197 */
anthony59c44432012-05-23 04:41:25 +00001198 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
cristy3ed852e2009-09-05 21:47:34 +00001199 {
1200 ThrowFileException(exception,FileOpenError,
1201 "UnableToCreateTemporaryFile",image_info->unique);
1202 return(MagickFalse);
1203 }
anthony59c44432012-05-23 04:41:25 +00001204 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
cristy3ed852e2009-09-05 21:47:34 +00001205 {
anthony2ec9bd92012-05-20 05:43:24 +00001206 (void) RelinquishUniqueFileResource(image_info->unique);
cristy3ed852e2009-09-05 21:47:34 +00001207 ThrowFileException(exception,FileOpenError,
1208 "UnableToCreateTemporaryFile",image_info->zero);
1209 return(MagickFalse);
1210 }
1211 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
cristy018f07f2011-09-04 21:15:19 +00001212 delegate_info->encode : delegate_info->decode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001213 if (magick == (char *) NULL)
1214 {
1215 (void) RelinquishUniqueFileResource(image_info->unique);
1216 (void) RelinquishUniqueFileResource(image_info->zero);
anthony59c44432012-05-23 04:41:25 +00001217 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001218 (void) RelinquishUniqueFileResource(image->filename);
1219 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001220 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001221 return(MagickFalse);
1222 }
1223 LocaleUpper(magick);
1224 clone_info=CloneImageInfo(image_info);
1225 (void) CopyMagickString((char *) clone_info->magick,magick,
cristy151b66d2015-04-15 10:50:31 +00001226 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001227 if (LocaleCompare(magick,"NULL") != 0)
cristy151b66d2015-04-15 10:50:31 +00001228 (void) CopyMagickString(image->magick,magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001229 magick=DestroyString(magick);
cristy151b66d2015-04-15 10:50:31 +00001230 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:",
cristy3ed852e2009-09-05 21:47:34 +00001231 delegate_info->decode);
cristyd965a422010-03-03 17:47:35 +00001232 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1233 exception);
cristy3ed852e2009-09-05 21:47:34 +00001234 (void) CopyMagickString(clone_info->filename,image_info->filename,
cristy151b66d2015-04-15 10:50:31 +00001235 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001236 (void) CopyMagickString(image_info->filename,image->filename,
cristy151b66d2015-04-15 10:50:31 +00001237 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001238 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1239 {
cristy151b66d2015-04-15 10:50:31 +00001240 (void) FormatLocaleString(p->filename,MagickPathExtent,"%s:%s",
cristy3ed852e2009-09-05 21:47:34 +00001241 delegate_info->decode,clone_info->filename);
cristy6f9e0d32011-08-28 16:32:09 +00001242 status=WriteImage(clone_info,p,exception);
anthony59c44432012-05-23 04:41:25 +00001243 if( IfMagickFalse(status) )
cristy3ed852e2009-09-05 21:47:34 +00001244 {
1245 (void) RelinquishUniqueFileResource(image_info->unique);
1246 (void) RelinquishUniqueFileResource(image_info->zero);
anthony59c44432012-05-23 04:41:25 +00001247 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001248 (void) RelinquishUniqueFileResource(image->filename);
1249 clone_info=DestroyImageInfo(clone_info);
1250 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001251 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001252 return(MagickFalse);
1253 }
anthony59c44432012-05-23 04:41:25 +00001254 if( IfMagickTrue(clone_info->adjoin) )
cristy3ed852e2009-09-05 21:47:34 +00001255 break;
1256 }
1257 (void) RelinquishUniqueFileResource(image_info->unique);
1258 (void) RelinquishUniqueFileResource(image_info->zero);
1259 clone_info=DestroyImageInfo(clone_info);
1260 }
1261 /*
1262 Invoke delegate.
1263 */
1264 commands=StringToList(delegate_info->commands);
1265 if (commands == (char **) NULL)
1266 {
anthony59c44432012-05-23 04:41:25 +00001267 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001268 (void) RelinquishUniqueFileResource(image->filename);
1269 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001270 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001271 decode ? decode : encode);
1272 return(MagickFalse);
1273 }
1274 command=(char *) NULL;
1275 status=MagickFalse;
cristy151b66d2015-04-15 10:50:31 +00001276 (void) CopyMagickString(output_filename,image_info->filename,MagickPathExtent);
1277 (void) CopyMagickString(input_filename,image->filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001278 for (i=0; commands[i] != (char *) NULL; i++)
1279 {
1280 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
anthony59c44432012-05-23 04:41:25 +00001281 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
cristy3ed852e2009-09-05 21:47:34 +00001282 {
1283 ThrowFileException(exception,FileOpenError,
1284 "UnableToCreateTemporaryFile",image_info->unique);
1285 break;
1286 }
anthony59c44432012-05-23 04:41:25 +00001287 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
cristy3ed852e2009-09-05 21:47:34 +00001288 {
1289 (void) RelinquishUniqueFileResource(image_info->unique);
1290 ThrowFileException(exception,FileOpenError,
1291 "UnableToCreateTemporaryFile",image_info->zero);
1292 break;
1293 }
1294 if (LocaleCompare(decode,"SCAN") != 0)
1295 {
1296 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
anthony59c44432012-05-23 04:41:25 +00001297 if( IfMagickFalse(status) )
cristy3ed852e2009-09-05 21:47:34 +00001298 {
1299 ThrowFileException(exception,FileOpenError,
1300 "UnableToCreateTemporaryFile",input_filename);
1301 break;
1302 }
1303 }
1304 status=MagickFalse;
cristy018f07f2011-09-04 21:15:19 +00001305 command=InterpretImageProperties(image_info,image,commands[i],exception);
cristy3ed852e2009-09-05 21:47:34 +00001306 if (command != (char *) NULL)
1307 {
1308 /*
1309 Execute delegate.
1310 */
cristydfc19b62014-10-17 22:52:24 +00001311 status=IsMagickTrue(ExternalDelegateCommand(delegate_info->spawn,
dirk4dd75672014-10-12 12:52:42 +00001312 image_info->verbose,command,(char *) NULL,exception) != 0);
cristy77f69392012-05-26 01:15:25 +00001313 if (IfMagickTrue(delegate_info->spawn))
1314 {
1315 ssize_t
1316 count;
1317
1318 /*
cristy47ef5e92013-03-20 23:29:45 +00001319 Wait for input file to 'disappear', or maximum 10 seconds.
cristy77f69392012-05-26 01:15:25 +00001320 */
cristy47ef5e92013-03-20 23:29:45 +00001321 count=100;
cristy3fe49df2012-05-26 01:17:56 +00001322 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
cristy58399382012-05-26 12:44:41 +00001323 (void) MagickDelay(100); /* sleep 0.1 seconds */
anthony2ec9bd92012-05-20 05:43:24 +00001324 }
cristy3ed852e2009-09-05 21:47:34 +00001325 command=DestroyString(command);
1326 }
1327 if (LocaleCompare(decode,"SCAN") != 0)
1328 {
cristy4886c4f2015-06-06 15:26:09 +00001329 if (CopyDelegateFile(image->filename,input_filename,MagickFalse) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001330 (void) RelinquishUniqueFileResource(input_filename);
1331 }
cristy4886c4f2015-06-06 15:26:09 +00001332 if (CopyDelegateFile(image_info->filename,output_filename,MagickTrue) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001333 (void) RelinquishUniqueFileResource(output_filename);
anthony59c44432012-05-23 04:41:25 +00001334 if( IfMagickTrue(image_info->temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001335 (void) RelinquishUniqueFileResource(image_info->filename);
1336 (void) RelinquishUniqueFileResource(image_info->unique);
1337 (void) RelinquishUniqueFileResource(image_info->zero);
1338 (void) RelinquishUniqueFileResource(image_info->filename);
1339 (void) RelinquishUniqueFileResource(image->filename);
anthony59c44432012-05-23 04:41:25 +00001340 if( IfMagickTrue(status) )
cristy3ed852e2009-09-05 21:47:34 +00001341 {
1342 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +00001343 "DelegateFailed","`%s'",commands[i]);
cristy3ed852e2009-09-05 21:47:34 +00001344 break;
1345 }
1346 commands[i]=DestroyString(commands[i]);
1347 }
cristy151b66d2015-04-15 10:50:31 +00001348 (void) CopyMagickString(image_info->filename,output_filename,MagickPathExtent);
1349 (void) CopyMagickString(image->filename,input_filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001350 /*
1351 Relinquish resources.
1352 */
1353 for ( ; commands[i] != (char *) NULL; i++)
1354 commands[i]=DestroyString(commands[i]);
1355 commands=(char **) RelinquishMagickMemory(commands);
anthony59c44432012-05-23 04:41:25 +00001356 if( IfMagickTrue(temporary) )
cristy3ed852e2009-09-05 21:47:34 +00001357 (void) RelinquishUniqueFileResource(image->filename);
anthony59c44432012-05-23 04:41:25 +00001358 return(IsMagickFalse(status));
cristy3ed852e2009-09-05 21:47:34 +00001359}
1360
1361/*
1362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363% %
1364% %
1365% %
1366% L i s t D e l e g a t e I n f o %
1367% %
1368% %
1369% %
1370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371%
1372% ListDelegateInfo() lists the image formats to a file.
1373%
1374% The format of the ListDelegateInfo method is:
1375%
1376% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1377%
1378% A description of each parameter follows.
1379%
1380% o file: An pointer to a FILE.
1381%
1382% o exception: return any errors or warnings in this structure.
1383%
1384*/
1385MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1386 ExceptionInfo *exception)
1387{
1388 const DelegateInfo
1389 **delegate_info;
1390
1391 char
1392 **commands,
cristy151b66d2015-04-15 10:50:31 +00001393 delegate[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001394
1395 const char
1396 *path;
1397
cristybb503372010-05-27 20:51:26 +00001398 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001399 i;
1400
cristybb503372010-05-27 20:51:26 +00001401 size_t
cristy3ed852e2009-09-05 21:47:34 +00001402 number_delegates;
1403
cristy9d314ff2011-03-09 01:30:28 +00001404 ssize_t
1405 j;
1406
cristy3ed852e2009-09-05 21:47:34 +00001407 if (file == (const FILE *) NULL)
1408 file=stdout;
1409 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1410 if (delegate_info == (const DelegateInfo **) NULL)
1411 return(MagickFalse);
1412 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +00001413 for (i=0; i < (ssize_t) number_delegates; i++)
cristy3ed852e2009-09-05 21:47:34 +00001414 {
anthony59c44432012-05-23 04:41:25 +00001415 if( IfMagickTrue(delegate_info[i]->stealth) )
cristy3ed852e2009-09-05 21:47:34 +00001416 continue;
1417 if ((path == (const char *) NULL) ||
1418 (LocaleCompare(path,delegate_info[i]->path) != 0))
1419 {
1420 if (delegate_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +00001421 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1422 (void) FormatLocaleFile(file,"Delegate Command\n");
cristy1e604812011-05-19 18:07:50 +00001423 (void) FormatLocaleFile(file,
1424 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +00001425 "------------------------------\n");
1426 }
1427 path=delegate_info[i]->path;
1428 *delegate='\0';
1429 if (delegate_info[i]->encode != (char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001430 (void) CopyMagickString(delegate,delegate_info[i]->encode,MagickPathExtent);
1431 (void) ConcatenateMagickString(delegate," ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001432 delegate[8]='\0';
1433 commands=StringToList(delegate_info[i]->commands);
1434 if (commands == (char **) NULL)
1435 continue;
cristyb51dff52011-05-19 16:55:47 +00001436 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
cristy3ed852e2009-09-05 21:47:34 +00001437 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1438 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1439 StripString(commands[0]);
cristyb51dff52011-05-19 16:55:47 +00001440 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
cristy3ed852e2009-09-05 21:47:34 +00001441 for (j=1; commands[j] != (char *) NULL; j++)
1442 {
1443 StripString(commands[j]);
cristyb51dff52011-05-19 16:55:47 +00001444 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
cristy3ed852e2009-09-05 21:47:34 +00001445 }
1446 for (j=0; commands[j] != (char *) NULL; j++)
1447 commands[j]=DestroyString(commands[j]);
1448 commands=(char **) RelinquishMagickMemory(commands);
1449 }
1450 (void) fflush(file);
1451 delegate_info=(const DelegateInfo **)
1452 RelinquishMagickMemory((void *) delegate_info);
1453 return(MagickTrue);
1454}
1455
1456/*
1457%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458% %
1459% %
1460% %
1461+ L o a d D e l e g a t e L i s t %
1462% %
1463% %
1464% %
1465%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466%
cristy86e5ac92014-03-16 19:27:39 +00001467% LoadDelegateCache() loads the delegate configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +00001468% mapping between delegate attributes and a delegate name.
1469%
cristy86e5ac92014-03-16 19:27:39 +00001470% The format of the LoadDelegateCache method is:
cristy3ed852e2009-09-05 21:47:34 +00001471%
cristycd2cd182014-03-18 12:10:55 +00001472% MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1473% const char *xml,const char *filename,const size_t depth,
1474% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001475%
1476% A description of each parameter follows:
1477%
1478% o xml: The delegate list in XML format.
1479%
1480% o filename: The delegate list filename.
1481%
1482% o depth: depth of <include /> statements.
1483%
1484% o exception: return any errors or warnings in this structure.
1485%
1486*/
cristycd2cd182014-03-18 12:10:55 +00001487static MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1488 const char *xml,const char *filename,const size_t depth,
1489 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001490{
1491 char
cristy151b66d2015-04-15 10:50:31 +00001492 keyword[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001493 *token;
1494
1495 const char
1496 *q;
1497
1498 DelegateInfo
1499 *delegate_info;
1500
cristy759ba912014-06-26 11:59:43 +00001501 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +00001502 status;
1503
1504 /*
1505 Load the delegate map file.
1506 */
1507 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1508 "Loading delegate configuration file \"%s\" ...",filename);
1509 if (xml == (const char *) NULL)
1510 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001511 status=MagickTrue;
1512 delegate_info=(DelegateInfo *) NULL;
1513 token=AcquireString(xml);
1514 for (q=(const char *) xml; *q != '\0'; )
1515 {
1516 /*
1517 Interpret XML.
1518 */
1519 GetMagickToken(q,&q,token);
1520 if (*token == '\0')
1521 break;
cristy151b66d2015-04-15 10:50:31 +00001522 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001523 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1524 {
1525 /*
1526 Doctype element.
1527 */
1528 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1529 GetMagickToken(q,&q,token);
1530 continue;
1531 }
1532 if (LocaleNCompare(keyword,"<!--",4) == 0)
1533 {
1534 /*
1535 Comment element.
1536 */
1537 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1538 GetMagickToken(q,&q,token);
1539 continue;
1540 }
1541 if (LocaleCompare(keyword,"<include") == 0)
1542 {
1543 /*
1544 Include element.
1545 */
1546 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1547 {
cristy151b66d2015-04-15 10:50:31 +00001548 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001549 GetMagickToken(q,&q,token);
1550 if (*token != '=')
1551 continue;
1552 GetMagickToken(q,&q,token);
1553 if (LocaleCompare(keyword,"file") == 0)
1554 {
1555 if (depth > 200)
1556 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001557 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00001558 else
1559 {
1560 char
cristy151b66d2015-04-15 10:50:31 +00001561 path[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001562 *xml;
1563
1564 GetPathComponent(filename,HeadPath,path);
1565 if (*path != '\0')
1566 (void) ConcatenateMagickString(path,DirectorySeparator,
cristy151b66d2015-04-15 10:50:31 +00001567 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001568 if (*token == *DirectorySeparator)
cristy151b66d2015-04-15 10:50:31 +00001569 (void) CopyMagickString(path,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001570 else
cristy151b66d2015-04-15 10:50:31 +00001571 (void) ConcatenateMagickString(path,token,MagickPathExtent);
cristy3291f512014-03-16 22:16:22 +00001572 xml=FileToXML(path,~0UL);
cristy3ed852e2009-09-05 21:47:34 +00001573 if (xml != (char *) NULL)
1574 {
cristycd2cd182014-03-18 12:10:55 +00001575 status&=LoadDelegateCache(delegate_cache,xml,path,
1576 depth+1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001577 xml=(char *) RelinquishMagickMemory(xml);
1578 }
1579 }
1580 }
1581 }
1582 continue;
1583 }
1584 if (LocaleCompare(keyword,"<delegate") == 0)
1585 {
1586 /*
1587 Delegate element.
1588 */
1589 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1590 sizeof(*delegate_info));
1591 if (delegate_info == (DelegateInfo *) NULL)
1592 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1593 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1594 delegate_info->path=ConstantString(filename);
cristy571ce682014-03-19 13:52:13 +00001595 delegate_info->thread_support=MagickTrue;
cristye1c94d92015-06-28 12:16:33 +00001596 delegate_info->signature=MagickCoreSignature;
cristy3ed852e2009-09-05 21:47:34 +00001597 continue;
1598 }
1599 if (delegate_info == (DelegateInfo *) NULL)
1600 continue;
1601 if (LocaleCompare(keyword,"/>") == 0)
1602 {
cristy86e5ac92014-03-16 19:27:39 +00001603 status=AppendValueToLinkedList(delegate_cache,delegate_info);
anthony59c44432012-05-23 04:41:25 +00001604 if( IfMagickFalse(status) )
cristy3ed852e2009-09-05 21:47:34 +00001605 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001606 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001607 delegate_info->commands);
1608 delegate_info=(DelegateInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +00001609 continue;
cristy3ed852e2009-09-05 21:47:34 +00001610 }
1611 GetMagickToken(q,(const char **) NULL,token);
1612 if (*token != '=')
1613 continue;
1614 GetMagickToken(q,&q,token);
1615 GetMagickToken(q,&q,token);
1616 switch (*keyword)
1617 {
1618 case 'C':
1619 case 'c':
1620 {
1621 if (LocaleCompare((char *) keyword,"command") == 0)
1622 {
1623 char
1624 *commands;
1625
1626 commands=AcquireString(token);
cristy0157aea2010-04-24 21:12:18 +00001627#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001628 if (strchr(commands,'@') != (char *) NULL)
1629 {
1630 char
cristy151b66d2015-04-15 10:50:31 +00001631 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001632
cristy151b66d2015-04-15 10:50:31 +00001633 NTGhostscriptEXE(path,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001634 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1635 path);
1636 (void) SubstituteString((char **) &commands,"\\","/");
1637 }
1638#endif
1639 (void) SubstituteString((char **) &commands,"&amp;","&");
1640 (void) SubstituteString((char **) &commands,"&quot;","\"");
1641 (void) SubstituteString((char **) &commands,"&gt;",">");
1642 (void) SubstituteString((char **) &commands,"&lt;","<");
1643 delegate_info->commands=commands;
1644 break;
1645 }
1646 break;
1647 }
1648 case 'D':
1649 case 'd':
1650 {
1651 if (LocaleCompare((char *) keyword,"decode") == 0)
1652 {
1653 delegate_info->decode=ConstantString(token);
1654 delegate_info->mode=1;
1655 break;
1656 }
1657 break;
1658 }
1659 case 'E':
1660 case 'e':
1661 {
1662 if (LocaleCompare((char *) keyword,"encode") == 0)
1663 {
1664 delegate_info->encode=ConstantString(token);
1665 delegate_info->mode=(-1);
1666 break;
1667 }
1668 break;
1669 }
1670 case 'M':
1671 case 'm':
1672 {
1673 if (LocaleCompare((char *) keyword,"mode") == 0)
1674 {
1675 delegate_info->mode=1;
1676 if (LocaleCompare(token,"bi") == 0)
1677 delegate_info->mode=0;
1678 else
1679 if (LocaleCompare(token,"encode") == 0)
1680 delegate_info->mode=(-1);
1681 break;
1682 }
1683 break;
1684 }
1685 case 'S':
1686 case 's':
1687 {
1688 if (LocaleCompare((char *) keyword,"spawn") == 0)
1689 {
anthony6f201312012-03-30 04:08:15 +00001690 delegate_info->spawn=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +00001691 break;
1692 }
1693 if (LocaleCompare((char *) keyword,"stealth") == 0)
1694 {
anthony6f201312012-03-30 04:08:15 +00001695 delegate_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +00001696 break;
1697 }
1698 break;
1699 }
1700 case 'T':
1701 case 't':
1702 {
1703 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1704 {
anthony6f201312012-03-30 04:08:15 +00001705 delegate_info->thread_support=IsStringTrue(token);
cristy571ce682014-03-19 13:52:13 +00001706 if (delegate_info->thread_support == MagickFalse)
1707 delegate_info->semaphore=AcquireSemaphoreInfo();
cristy3ed852e2009-09-05 21:47:34 +00001708 break;
1709 }
1710 break;
1711 }
1712 default:
1713 break;
1714 }
1715 }
1716 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +00001717 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001718}