• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 %                                  Cristy                                     %
16 %                               October 1998                                  %
17 %                                                                             %
18 %                                                                             %
19 %  Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization      %
20 %  dedicated to making software imaging solutions freely available.           %
21 %                                                                             %
22 %  You may not use this file except in compliance with the License.  You may  %
23 %  obtain a copy of the License at                                            %
24 %                                                                             %
25 %    https://imagemagick.org/script/license.php                               %
26 %                                                                             %
27 %  Unless required by applicable law or agreed to in writing, software        %
28 %  distributed under the License is distributed on an "AS IS" BASIS,          %
29 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
30 %  See the License for the specific language governing permissions and        %
31 %  limitations under the License.                                             %
32 %                                                                             %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %  The Delegates methods associate a set of commands with a particular
36 %  image format.  ImageMagick uses delegates for formats it does not handle
37 %  directly.
38 %
39 %  Thanks to Bob Friesenhahn for the initial inspiration and design of the
40 %  delegates methods.
41 %
42 %
43 */
44 
45 /*
46   Include declarations.
47 */
48 #include "MagickCore/studio.h"
49 #include "MagickCore/artifact.h"
50 #include "MagickCore/attribute.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/client.h"
53 #include "MagickCore/configure.h"
54 #include "MagickCore/constitute.h"
55 #include "MagickCore/delegate.h"
56 #include "MagickCore/delegate-private.h"
57 #include "MagickCore/exception.h"
58 #include "MagickCore/exception-private.h"
59 #include "MagickCore/fx-private.h"
60 #include "MagickCore/image-private.h"
61 #include "MagickCore/linked-list.h"
62 #include "MagickCore/list.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/memory-private.h"
65 #include "MagickCore/nt-base-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/policy.h"
68 #include "MagickCore/property.h"
69 #include "MagickCore/resource_.h"
70 #include "MagickCore/semaphore.h"
71 #include "MagickCore/signature.h"
72 #include "MagickCore/string_.h"
73 #include "MagickCore/token.h"
74 #include "MagickCore/token-private.h"
75 #include "MagickCore/utility.h"
76 #include "MagickCore/utility-private.h"
77 #include "MagickCore/xml-tree.h"
78 #include "MagickCore/xml-tree-private.h"
79 
80 /*
81   Define declarations.
82 */
83 #define DelegateFilename  "delegates.xml"
84 
85 /*
86   Declare delegate map.
87 */
88 static const char
89   *DelegateMap = (const char *)
90     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
91     "<delegatemap>"
92     "  <delegate decode=\"bpg\" command=\"&quot;bpgdec&quot; -b 16 -o &quot;%o.png&quot; &quot;%i&quot;; mv &quot;%o.png&quot; &quot;%o&quot;\"/>"
93     "  <delegate decode=\"png\" encode=\"bpg\" command=\"&quot;bpgenc&quot; -b 12 -q %~ -o &quot;%o&quot; &quot;%i&quot;\"/>"
94     "  <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; https://imagemagick.org/; rm &quot;%i&quot;\"/>"
95     "  <delegate decode=\"cdr\" command=\"&quot;uniconvertor&quot; &quot;%i&quot; &quot;%o.svg&quot;; mv &quot;%o.svg&quot; &quot;%o&quot;\"/>"
96     "  <delegate decode=\"cgm\" command=\"&quot;uniconvertor&quot; &quot;%i&quot; &quot;%o.svg&quot;; mv &quot;%o.svg&quot; &quot;%o&quot;\"/>"
97     "  <delegate decode=\"https\" command=\"&quot;curl&quot; -s -k -L -o &quot;%o&quot; &quot;https:%M&quot;\"/>"
98     "  <delegate decode=\"doc\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
99     "  <delegate decode=\"docx\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
100     "  <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;\"/>"
101     "  <delegate decode=\"dot\" command=\"&quot;dot&quot; -Tsvg &quot;%i&quot; -o &quot;%o&quot;\"/>"
102     "  <delegate decode=\"dvi\" command=\"&quot;dvips&quot; -sstdout=%%stderr -o &quot;%o&quot; &quot;%i&quot;\"/>"
103     "  <delegate decode=\"dxf\" command=\"&quot;uniconvertor&quot; &quot;%i&quot; &quot;%o.svg&quot;; mv &quot;%o.svg&quot; &quot;%o&quot;\"/>"
104     "  <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
105     "  <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
106     "  <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
107     "  <delegate decode=\"fig\" command=\"&quot;uniconvertor&quot; &quot;%i&quot; &quot;%o.svg&quot;; mv &quot;%o.svg&quot; &quot;%o&quot;\"/>"
108     "  <delegate decode=\"hpg\" command=\"&quot;hp2xx&quot; -sstdout=%%stderr -m eps -f `basename &quot;%o&quot;` &quot;%i&quot;;     mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
109     "  <delegate decode=\"hpgl\" command=\"&quot;hp2xx&quot; -sstdout=%%stderr -m eps -f `basename &quot;%o&quot;` &quot;%i&quot;;     mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
110     "  <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
111     "  <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
112     "  <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
113     "  <delegate decode=\"jpg\" encode=\"lep\" mode=\"encode\" command=\"&quot;lepton&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
114     "  <delegate decode=\"jxr\" command=\"mv &quot;%i&quot; &quot;%i.jxr&quot;; &quot;JxrDecApp&quot; -i &quot;%i.jxr&quot; -o &quot;%o.pnm&quot;; mv &quot;%i.jxr&quot; &quot;%i&quot;; mv &quot;%o.pnm&quot; &quot;%o&quot;\"/>"
115     "  <delegate decode=\"lep\" mode=\"decode\" command=\"&quot;lepton&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
116     "  <delegate decode=\"mpeg:decode\" command=\"&quot;avconv&quot; -v -1 -i &quot;%i&quot; -vframes %S -vcodec pam -an -f rawvideo -y &quot;%u.pam&quot; 2&gt; &quot;%u&quot;\"/>"
117     "  <delegate decode=\"odt\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
118     "  <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pamcmyk32&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
119     "  <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;\"/>"
120     "  <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;\"/>"
121     "  <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sPDFPassword=&quot;%a&quot; &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
122     "  <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; -sPDFPassword=&quot;%a&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
123     "  <delegate decode=\"png\" encode=\"webp\" command=\"&quot;cwebp&quot; -quiet -q %Q &quot;%i&quot; -o &quot;%o&quot;\"/>"
124     "  <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\"&quot;ppmtoilbm&quot; -24if &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
125     "  <delegate decode=\"bmp\" encode=\"jxr\" command=\"mv &quot;%i&quot; &quot;%i.bmp&quot;; &quot;JxrEncApp&quot; -i &quot;%i.bmp&quot; -o &quot;%o.jxr&quot;; mv &quot;%i.bmp&quot; &quot;%i&quot;; mv &quot;%o.jxr&quot; &quot;%o&quot;\"/>"
126     "  <delegate decode=\"bmp\" encode=\"wdp\" command=\"mv &quot;%i&quot; &quot;%i.bmp&quot;; &quot;JxrEncApp&quot; -i &quot;%i.bmp&quot; -o &quot;%o.jxr&quot;; mv &quot;%i.bmp&quot; &quot;%i&quot;; mv &quot;%o.jxr&quot; &quot;%o&quot;\"/>"
127     "  <delegate decode=\"ppt\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
128     "  <delegate decode=\"pptx\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
129     "  <delegate decode=\"ps\" encode=\"prt\" command=\"&quot;lpr&quot; &quot;%i&quot;\"/>"
130     "  <delegate decode=\"ps:alpha\" stealth=\"True\" command=\"&quot;gs&quot; -sstdout=%%stderr -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;\"/>"
131     "  <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pamcmyk32&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
132     "  <delegate decode=\"ps:color\" stealth=\"True\" command=\"&quot;gs&quot; -sstdout=%%stderr -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;\"/>"
133     "  <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
134     "  <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
135     "  <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr &quot;%i&quot;\"/>"
136     "  <delegate decode=\"ps:mono\" stealth=\"True\" command=\"&quot;gs&quot; -sstdout=%%stderr -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;-f%s&quot; &quot;-f%s&quot;\"/>"
137     "  <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
138     "  <delegate decode=\"sid\" command=\"&quot;mrsidgeodecode&quot; -if sid -i &quot;%i&quot; -of tif -o &quot;%o&quot; &gt; &quot;%u&quot;\"/>"
139     "  <delegate decode=\"svg\" command=\"&quot;rsvg-convert&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
140     "  <delegate decode=\"svg:decode\" stealth=\"True\" command=\"&quot;inkscape&quot; &quot;%s&quot; --export-png=&quot;%s&quot; --export-dpi=&quot;%s&quot; --export-background=&quot;%s&quot; --export-background-opacity=&quot;%s&quot; &gt; &quot;%s&quot; 2&gt;&amp;1\"/>"
141     "  <delegate decode=\"tiff\" encode=\"launch\" mode=\"encode\" command=\"&quot;gimp&quot; &quot;%i&quot;\"/>"
142     "  <delegate decode=\"wdp\" command=\"mv &quot;%i&quot; &quot;%i.jxr&quot;; &quot;JxrDecApp&quot; -i &quot;%i.jxr&quot; -o &quot;%o.bmp&quot;; mv &quot;%i.jxr&quot; &quot;%i&quot;; mv &quot;%o.bmp&quot; &quot;%o&quot;\"/>"
143     "  <delegate decode=\"webp\" command=\"&quot;dwebp&quot; -pam &quot;%i&quot; -o &quot;%o&quot;\"/>"
144     "  <delegate decode=\"xls\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
145     "  <delegate decode=\"xlsx\" command=\"&quot;soffice&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; mv &quot;%i.pdf&quot; &quot;%o&quot;\"/>"
146     "  <delegate decode=\"xps:cmyk\" stealth=\"True\" command=\"&quot;gxps&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;\"/>"
147     "  <delegate decode=\"xps:color\" stealth=\"True\" command=\"&quot;gxps&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;\"/>"
148     "  <delegate decode=\"xps:mono\" stealth=\"True\" command=\"&quot;gxps&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;\"/>"
149     "  <delegate encode=\"mpeg:encode\" stealth=\"True\" command=\"&quot;avconv&quot; -v -1 -i &quot;%M%%d.jpg&quot; &quot;%u.%m&quot; 2&gt; &quot;%u&quot;\"/>"
150     "</delegatemap>";
151 
152 /*
153   Global declaractions.
154 */
155 static LinkedListInfo
156   *delegate_cache = (LinkedListInfo *) NULL;
157 
158 static SemaphoreInfo
159   *delegate_semaphore = (SemaphoreInfo *) NULL;
160 
161 /*
162   Forward declaractions.
163 */
164 static MagickBooleanType
165   IsDelegateCacheInstantiated(ExceptionInfo *),
166   LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
167     ExceptionInfo *);
168 
169 /*
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 %                                                                             %
172 %                                                                             %
173 %                                                                             %
174 %  A c q u i r e D e l e g a t e C a c h e                                    %
175 %                                                                             %
176 %                                                                             %
177 %                                                                             %
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 %
180 %  AcquireDelegateCache() caches one or more delegate configurations which
181 %  provides a mapping between delegate attributes and a delegate name.
182 %
183 %  The format of the AcquireDelegateCache method is:
184 %
185 %      LinkedListInfo *AcquireDelegateCache(const char *filename,
186 %        ExceptionInfo *exception)
187 %
188 %  A description of each parameter follows:
189 %
190 %    o filename: the font file name.
191 %
192 %    o exception: return any errors or warnings in this structure.
193 %
194 */
AcquireDelegateCache(const char * filename,ExceptionInfo * exception)195 static LinkedListInfo *AcquireDelegateCache(const char *filename,
196   ExceptionInfo *exception)
197 {
198   LinkedListInfo
199     *cache;
200 
201   MagickStatusType
202     status;
203 
204   cache=NewLinkedList(0);
205   status=MagickTrue;
206 #if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
207   {
208     const StringInfo
209       *option;
210 
211     LinkedListInfo
212       *options;
213 
214     options=GetConfigureOptions(filename,exception);
215     option=(const StringInfo *) GetNextValueInLinkedList(options);
216     while (option != (const StringInfo *) NULL)
217     {
218       status&=LoadDelegateCache(cache,(const char *)
219         GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
220       option=(const StringInfo *) GetNextValueInLinkedList(options);
221     }
222     options=DestroyConfigureOptions(options);
223   }
224 #endif
225   if (IsLinkedListEmpty(cache) != MagickFalse)
226     status&=LoadDelegateCache(cache,DelegateMap,"built-in",0,exception);
227   return(cache);
228 }
229 
230 /*
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 %                                                                             %
233 %                                                                             %
234 %                                                                             %
235 +   D e l e g a t e C o m p o n e n t G e n e s i s                           %
236 %                                                                             %
237 %                                                                             %
238 %                                                                             %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 %
241 %  DelegateComponentGenesis() instantiates the delegate component.
242 %
243 %  The format of the DelegateComponentGenesis method is:
244 %
245 %      MagickBooleanType DelegateComponentGenesis(void)
246 %
247 */
DelegateComponentGenesis(void)248 MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
249 {
250   if (delegate_semaphore == (SemaphoreInfo *) NULL)
251     delegate_semaphore=AcquireSemaphoreInfo();
252   return(MagickTrue);
253 }
254 
255 /*
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 %                                                                             %
258 %                                                                             %
259 %                                                                             %
260 %   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                         %
261 %                                                                             %
262 %                                                                             %
263 %                                                                             %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 %
266 %  DelegateComponentTerminus() destroys the delegate component.
267 %
268 %  The format of the DelegateComponentTerminus method is:
269 %
270 %      DelegateComponentTerminus(void)
271 %
272 */
273 
DestroyDelegate(void * delegate_info)274 static void *DestroyDelegate(void *delegate_info)
275 {
276   register DelegateInfo
277     *p;
278 
279   p=(DelegateInfo *) delegate_info;
280   if (p->path != (char *) NULL)
281     p->path=DestroyString(p->path);
282   if (p->decode != (char *) NULL)
283     p->decode=DestroyString(p->decode);
284   if (p->encode != (char *) NULL)
285     p->encode=DestroyString(p->encode);
286   if (p->commands != (char *) NULL)
287     p->commands=DestroyString(p->commands);
288   if (p->semaphore != (SemaphoreInfo *) NULL)
289     RelinquishSemaphoreInfo(&p->semaphore);
290   p=(DelegateInfo *) RelinquishMagickMemory(p);
291   return((void *) NULL);
292 }
293 
DelegateComponentTerminus(void)294 MagickPrivate void DelegateComponentTerminus(void)
295 {
296   if (delegate_semaphore == (SemaphoreInfo *) NULL)
297     ActivateSemaphoreInfo(&delegate_semaphore);
298   LockSemaphoreInfo(delegate_semaphore);
299   if (delegate_cache != (LinkedListInfo *) NULL)
300     delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
301   UnlockSemaphoreInfo(delegate_semaphore);
302   RelinquishSemaphoreInfo(&delegate_semaphore);
303 }
304 
305 /*
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 %                                                                             %
308 %                                                                             %
309 %                                                                             %
310 +   E x t e r n a l D e l e g a t e C o m m a n d                             %
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %
316 %  ExternalDelegateCommand() executes the specified command and waits until it
317 %  terminates.  The returned value is the exit status of the command.
318 %
319 %  The format of the ExternalDelegateCommand method is:
320 %
321 %      int ExternalDelegateCommand(const MagickBooleanType asynchronous,
322 %        const MagickBooleanType verbose,const char *command,
323 %        char *message,ExceptionInfo *exception)
324 %
325 %  A description of each parameter follows:
326 %
327 %    o asynchronous: a value other than 0 executes the parent program
328 %      concurrently with the new child process.
329 %
330 %    o verbose: a value other than 0 prints the executed command before it is
331 %      invoked.
332 %
333 %    o command: this string is the command to execute.
334 %
335 %    o message: an option buffer to receive any message posted to stdout or
336 %      stderr.
337 %
338 %    o exception: return any errors here.
339 %
340 */
ExternalDelegateCommand(const MagickBooleanType asynchronous,const MagickBooleanType verbose,const char * command,char * message,ExceptionInfo * exception)341 MagickExport int ExternalDelegateCommand(const MagickBooleanType asynchronous,
342   const MagickBooleanType verbose,const char *command,char *message,
343   ExceptionInfo *exception)
344 {
345   char
346     **arguments,
347     *sanitize_command;
348 
349   int
350     number_arguments,
351     status;
352 
353   PolicyDomain
354     domain;
355 
356   PolicyRights
357     rights;
358 
359   register ssize_t
360     i;
361 
362   status=(-1);
363   arguments=StringToArgv(command,&number_arguments);
364   if (arguments == (char **) NULL)
365     return(status);
366   if (*arguments[1] == '\0')
367     {
368       for (i=0; i < (ssize_t) number_arguments; i++)
369         arguments[i]=DestroyString(arguments[i]);
370       arguments=(char **) RelinquishMagickMemory(arguments);
371       return(-1);
372     }
373   rights=ExecutePolicyRights;
374   domain=DelegatePolicyDomain;
375   if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
376     {
377       errno=EPERM;
378       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
379         "NotAuthorized","`%s'",arguments[1]);
380       for (i=0; i < (ssize_t) number_arguments; i++)
381         arguments[i]=DestroyString(arguments[i]);
382       arguments=(char **) RelinquishMagickMemory(arguments);
383       return(-1);
384     }
385   if (verbose != MagickFalse)
386     {
387       (void) FormatLocaleFile(stderr,"%s\n",command);
388       (void) fflush(stderr);
389     }
390   sanitize_command=SanitizeString(command);
391   if (asynchronous != MagickFalse)
392     (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
393   if (message != (char *) NULL)
394     *message='\0';
395 #if defined(MAGICKCORE_POSIX_SUPPORT)
396 #if !defined(MAGICKCORE_HAVE_EXECVP)
397   status=system(sanitize_command);
398 #else
399   if ((asynchronous != MagickFalse) ||
400       (strpbrk(sanitize_command,"&;<>|") != (char *) NULL))
401     status=system(sanitize_command);
402   else
403     {
404       pid_t
405         child_pid;
406 
407       /*
408         Call application directly rather than from a shell.
409       */
410       child_pid=(pid_t) fork();
411       if (child_pid == (pid_t) -1)
412         status=system(sanitize_command);
413       else
414         if (child_pid == 0)
415           {
416             status=execvp(arguments[1],arguments+1);
417             _exit(1);
418           }
419         else
420           {
421             int
422               child_status;
423 
424             pid_t
425               pid;
426 
427             child_status=0;
428             pid=(pid_t) waitpid(child_pid,&child_status,0);
429             if (pid == -1)
430               status=(-1);
431             else
432               {
433                 if (WIFEXITED(child_status) != 0)
434                   status=WEXITSTATUS(child_status);
435                 else
436                   if (WIFSIGNALED(child_status))
437                     status=(-1);
438               }
439           }
440     }
441 #endif
442 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
443   {
444     register char
445       *p;
446 
447     /*
448       If a command shell is executed we need to change the forward slashes in
449       files to a backslash. We need to do this to keep Windows happy when we
450       want to 'move' a file.
451 
452       TODO: This won't work if one of the delegate parameters has a forward
453             slash as aparameter.
454     */
455     p=strstr(sanitize_command,"cmd.exe /c");
456     if (p != (char*) NULL)
457       {
458         p+=10;
459         for ( ; *p != '\0'; p++)
460           if (*p == '/')
461             *p=(*DirectorySeparator);
462       }
463   }
464   status=NTSystemCommand(sanitize_command,message);
465 #elif defined(macintosh)
466   status=MACSystemCommand(sanitize_command);
467 #elif defined(vms)
468   status=system(sanitize_command);
469 #else
470 #  error No suitable system() method.
471 #endif
472   if (status < 0)
473     {
474       if ((message != (char *) NULL) && (*message != '\0'))
475         (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
476           "FailedToExecuteCommand","`%s' (%s)",sanitize_command,message);
477       else
478         (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
479           "FailedToExecuteCommand","`%s' (%d)",sanitize_command,status);
480     }
481   sanitize_command=DestroyString(sanitize_command);
482   for (i=0; i < (ssize_t) number_arguments; i++)
483     arguments[i]=DestroyString(arguments[i]);
484   arguments=(char **) RelinquishMagickMemory(arguments);
485   return(status);
486 }
487 
488 /*
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 %                                                                             %
491 %                                                                             %
492 %                                                                             %
493 %   G e t D e l e g a t e C o m m a n d                                       %
494 %                                                                             %
495 %                                                                             %
496 %                                                                             %
497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 %
499 %  GetDelegateCommand() replaces any embedded formatting characters with the
500 %  appropriate image attribute and returns the resulting command.
501 %
502 %  The format of the GetDelegateCommand method is:
503 %
504 %      char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
505 %        const char *decode,const char *encode,ExceptionInfo *exception)
506 %
507 %  A description of each parameter follows:
508 %
509 %    o command: Method GetDelegateCommand returns the command associated
510 %      with specified delegate tag.
511 %
512 %    o image_info: the image info.
513 %
514 %    o image: the image.
515 %
516 %    o decode: Specifies the decode delegate we are searching for as a
517 %      character string.
518 %
519 %    o encode: Specifies the encode delegate we are searching for as a
520 %      character string.
521 %
522 %    o exception: return any errors or warnings in this structure.
523 %
524 */
525 
SanitizeDelegateString(const char * source)526 static char *SanitizeDelegateString(const char *source)
527 {
528   char
529     *sanitize_source;
530 
531   const char
532     *q;
533 
534   register char
535     *p;
536 
537   static char
538 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
539     whitelist[] =
540       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
541       "$-_.+!;*(),{}|^~[]`\'><#%/?:@&=";
542 #else
543     whitelist[] =
544       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
545       "$-_.+!;*(),{}|\\^~[]`\"><#%/?:@&=";
546 #endif
547 
548   sanitize_source=AcquireString(source);
549   p=sanitize_source;
550   q=sanitize_source+strlen(sanitize_source);
551   for (p+=strspn(p,whitelist); p != q; p+=strspn(p,whitelist))
552     *p='_';
553   return(sanitize_source);
554 }
555 
GetMagickPropertyLetter(ImageInfo * image_info,Image * image,const char letter,ExceptionInfo * exception)556 static char *GetMagickPropertyLetter(ImageInfo *image_info,Image *image,
557   const char letter,ExceptionInfo *exception)
558 {
559 #define WarnNoImageReturn(format,letter) \
560   if (image == (Image *) NULL) \
561     { \
562       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
563         "NoImageForProperty",format,letter); \
564       break; \
565     }
566 #define WarnNoImageInfoReturn(format,letter) \
567   if (image_info == (ImageInfo *) NULL) \
568     { \
569       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
570         "NoImageInfoForProperty",format,letter); \
571       break; \
572     }
573 
574   char
575     value[MagickPathExtent];
576 
577   const char
578     *string;
579 
580   if ((image != (Image *) NULL) && (image->debug != MagickFalse))
581     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
582   else
583     if ((image_info != (ImageInfo *) NULL) &&
584         (image_info->debug != MagickFalse))
585       (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-images");
586   /*
587     Get properties that are directly defined by images.
588   */
589   *value='\0';           /* formatted string */
590   string=(const char *) value;
591   switch (letter)
592   {
593     case 'a': /* authentication passphase */
594     {
595       WarnNoImageInfoReturn("\"%%%c\"",letter);
596       string=GetImageOption(image_info,"authenticate");
597       break;
598     }
599     case 'b':  /* image size read in - in bytes */
600     {
601       WarnNoImageReturn("\"%%%c\"",letter);
602       (void) FormatMagickSize(image->extent,MagickFalse,"B",MagickPathExtent,
603         value);
604       if (image->extent == 0)
605         (void) FormatMagickSize(GetBlobSize(image),MagickFalse,"B",
606           MagickPathExtent,value);
607       break;
608     }
609     case 'd':  /* Directory component of filename */
610     {
611       WarnNoImageReturn("\"%%%c\"",letter);
612       GetPathComponent(image->magick_filename,HeadPath,value);
613       break;
614     }
615     case 'e': /* Filename extension (suffix) of image file */
616     {
617       WarnNoImageReturn("\"%%%c\"",letter);
618       GetPathComponent(image->magick_filename,ExtensionPath,value);
619       break;
620     }
621     case 'f': /* Filename without directory component */
622     {
623       WarnNoImageReturn("\"%%%c\"",letter);
624       GetPathComponent(image->magick_filename,TailPath,value);
625       break;
626     }
627     case 'g': /* Image geometry, canvas and offset  %Wx%H+%X+%Y */
628     {
629       WarnNoImageReturn("\"%%%c\"",letter);
630       (void) FormatLocaleString(value,MagickPathExtent,
631         "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
632         image->page.height,(double) image->page.x,(double) image->page.y);
633       break;
634     }
635     case 'h': /* Image height (current) */
636     {
637       WarnNoImageReturn("\"%%%c\"",letter);
638       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
639         (image->rows != 0 ? image->rows : image->magick_rows));
640       break;
641     }
642     case 'i': /* Filename last used for an image (read or write) */
643     {
644       WarnNoImageReturn("\"%%%c\"",letter);
645       string=image->filename;
646       break;
647     }
648     case 'm': /* Image format (file magick) */
649     {
650       WarnNoImageReturn("\"%%%c\"",letter);
651       string=image->magick;
652       break;
653     }
654     case 'n': /* Number of images in the list.  */
655     {
656       if (image != (Image *) NULL)
657         (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
658           GetImageListLength(image));
659       break;
660     }
661     case 'o': /* Output Filename */
662     {
663       WarnNoImageInfoReturn("\"%%%c\"",letter);
664       string=image_info->filename;
665       break;
666     }
667     case 'p': /* Image index in current image list */
668     {
669       WarnNoImageReturn("\"%%%c\"",letter);
670       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
671         GetImageIndexInList(image));
672       break;
673     }
674     case 'q': /* Quantum depth of image in memory */
675     {
676       WarnNoImageReturn("\"%%%c\"",letter);
677       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
678         MAGICKCORE_QUANTUM_DEPTH);
679       break;
680     }
681     case 'r': /* Image storage class, colorspace, and alpha enabled.  */
682     {
683       ColorspaceType
684         colorspace;
685 
686       WarnNoImageReturn("\"%%%c\"",letter);
687       colorspace=image->colorspace;
688       if (SetImageGray(image,exception) != MagickFalse)
689         colorspace=GRAYColorspace;   /* FUTURE: this is IMv6 not IMv7 */
690       (void) FormatLocaleString(value,MagickPathExtent,"%s %s %s",
691         CommandOptionToMnemonic(MagickClassOptions,(ssize_t)
692         image->storage_class),CommandOptionToMnemonic(MagickColorspaceOptions,
693         (ssize_t) colorspace),image->alpha_trait != UndefinedPixelTrait ?
694         "Alpha" : "");
695       break;
696     }
697     case 's': /* Image scene number */
698     {
699       WarnNoImageReturn("\"%%%c\"",letter);
700       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
701         image->scene);
702       break;
703     }
704     case 't': /* Base filename without directory or extention */
705     {
706       WarnNoImageReturn("\"%%%c\"",letter);
707       GetPathComponent(image->magick_filename,BasePath,value);
708       break;
709     }
710     case 'u': /* Unique filename */
711     {
712       WarnNoImageInfoReturn("\"%%%c\"",letter);
713       string=image_info->unique;
714       break;
715     }
716     case 'w': /* Image width (current) */
717     {
718       WarnNoImageReturn("\"%%%c\"",letter);
719       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
720         (image->columns != 0 ? image->columns : image->magick_columns));
721       break;
722     }
723     case 'x': /* Image horizontal resolution (with units) */
724     {
725       WarnNoImageReturn("\"%%%c\"",letter);
726       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
727         fabs(image->resolution.x) > MagickEpsilon ? image->resolution.x : 72.0);
728       break;
729     }
730     case 'y': /* Image vertical resolution (with units) */
731     {
732       WarnNoImageReturn("\"%%%c\"",letter);
733       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
734         fabs(image->resolution.y) > MagickEpsilon ? image->resolution.y : 72.0);
735       break;
736     }
737     case 'z': /* Image depth as read in */
738     {
739       WarnNoImageReturn("\"%%%c\"",letter);
740       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
741         (double) image->depth);
742       break;
743     }
744     case 'A': /* Image alpha channel  */
745     {
746       WarnNoImageReturn("\"%%%c\"",letter);
747       string=CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
748         image->alpha_trait);
749       break;
750     }
751     case 'C': /* Image compression method.  */
752     {
753       WarnNoImageReturn("\"%%%c\"",letter);
754       string=CommandOptionToMnemonic(MagickCompressOptions,
755         (ssize_t) image->compression);
756       break;
757     }
758     case 'D': /* Image dispose method.  */
759     {
760       WarnNoImageReturn("\"%%%c\"",letter);
761       string=CommandOptionToMnemonic(MagickDisposeOptions,
762         (ssize_t) image->dispose);
763       break;
764     }
765     case 'F':
766     {
767       /*
768         Magick filename - filename given incl. coder & read mods.
769       */
770       WarnNoImageReturn("\"%%%c\"",letter);
771       (void) CopyMagickString(value,image->magick_filename,MagickPathExtent);
772       break;
773     }
774     case 'G': /* Image size as geometry = "%wx%h" */
775     {
776       WarnNoImageReturn("\"%%%c\"",letter);
777       (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
778         (double) image->magick_columns,(double) image->magick_rows);
779       break;
780     }
781     case 'H': /* layer canvas height */
782     {
783       WarnNoImageReturn("\"%%%c\"",letter);
784       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
785         (double) image->page.height);
786       break;
787     }
788     case 'M': /* Magick filename - filename given incl. coder & read mods */
789     {
790       WarnNoImageReturn("\"%%%c\"",letter);
791       string=image->magick_filename;
792       break;
793     }
794     case 'O': /* layer canvas offset with sign = "+%X+%Y" */
795     {
796       WarnNoImageReturn("\"%%%c\"",letter);
797       (void) FormatLocaleString(value,MagickPathExtent,"%+ld%+ld",(long)
798         image->page.x,(long) image->page.y);
799       break;
800     }
801     case 'P': /* layer canvas page size = "%Wx%H" */
802     {
803       WarnNoImageReturn("\"%%%c\"",letter);
804       (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
805         (double) image->page.width,(double) image->page.height);
806       break;
807     }
808     case '~': /* BPG image compression quality */
809     {
810       WarnNoImageReturn("\"%%%c\"",letter);
811       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
812         (100-(image->quality == 0 ? 42 : image->quality))/2);
813       break;
814     }
815     case 'Q': /* image compression quality */
816     {
817       WarnNoImageReturn("\"%%%c\"",letter);
818       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
819         (image->quality == 0 ? 92 : image->quality));
820       break;
821     }
822     case 'S': /* Number of scenes in image list.  */
823     {
824       WarnNoImageInfoReturn("\"%%%c\"",letter);
825       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
826         (image_info->number_scenes == 0 ? 2147483647 :
827          image_info->number_scenes));
828       break;
829     }
830     case 'T': /* image time delay for animations */
831     {
832       WarnNoImageReturn("\"%%%c\"",letter);
833       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
834         image->delay);
835       break;
836     }
837     case 'U': /* Image resolution units. */
838     {
839       WarnNoImageReturn("\"%%%c\"",letter);
840       string=CommandOptionToMnemonic(MagickResolutionOptions,
841         (ssize_t) image->units);
842       break;
843     }
844     case 'W': /* layer canvas width */
845     {
846       WarnNoImageReturn("\"%%%c\"",letter);
847       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
848         image->page.width);
849       break;
850     }
851     case 'X': /* layer canvas X offset */
852     {
853       WarnNoImageReturn("\"%%%c\"",letter);
854       (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
855         image->page.x);
856       break;
857     }
858     case 'Y': /* layer canvas Y offset */
859     {
860       WarnNoImageReturn("\"%%%c\"",letter);
861       (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
862         image->page.y);
863       break;
864     }
865     case '%': /* percent escaped */
866     {
867       string="%";
868       break;
869     }
870     case '@': /* Trim bounding box, without actually trimming! */
871     {
872       RectangleInfo
873         page;
874 
875       WarnNoImageReturn("\"%%%c\"",letter);
876       page=GetImageBoundingBox(image,exception);
877       (void) FormatLocaleString(value,MagickPathExtent,
878         "%.20gx%.20g%+.20g%+.20g",(double) page.width,(double) page.height,
879         (double) page.x,(double) page.y);
880       break;
881     }
882     case '#':
883     {
884       /*
885         Image signature.
886       */
887       WarnNoImageReturn("\"%%%c\"",letter);
888       (void) SignatureImage(image,exception);
889       string=GetImageProperty(image,"signature",exception);
890       break;
891     }
892   }
893   return(SanitizeDelegateString(string));
894 }
895 
InterpretDelegateProperties(ImageInfo * image_info,Image * image,const char * embed_text,ExceptionInfo * exception)896 static char *InterpretDelegateProperties(ImageInfo *image_info,
897   Image *image,const char *embed_text,ExceptionInfo *exception)
898 {
899 #define ExtendInterpretText(string_length) \
900 DisableMSCWarning(4127) \
901 { \
902   size_t length=(string_length); \
903   if ((size_t) (q-interpret_text+length+1) >= extent) \
904     { \
905       extent+=length; \
906       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
907         MaxTextExtent,sizeof(*interpret_text)); \
908       if (interpret_text == (char *) NULL) \
909         return((char *) NULL); \
910       q=interpret_text+strlen(interpret_text); \
911    } \
912 } \
913 RestoreMSCWarning
914 
915 #define AppendKeyValue2Text(key,value)\
916 DisableMSCWarning(4127) \
917 { \
918   size_t length=strlen(key)+strlen(value)+2; \
919   if ((size_t) (q-interpret_text+length+1) >= extent) \
920     { \
921       extent+=length; \
922       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
923         MaxTextExtent,sizeof(*interpret_text)); \
924       if (interpret_text == (char *) NULL) \
925         return((char *) NULL); \
926       q=interpret_text+strlen(interpret_text); \
927      } \
928    q+=FormatLocaleString(q,extent,"%s=%s\n",(key),(value)); \
929 } \
930 RestoreMSCWarning
931 
932 #define AppendString2Text(string) \
933 DisableMSCWarning(4127) \
934 { \
935   size_t length=strlen((string)); \
936   if ((size_t) (q-interpret_text+length+1) >= extent) \
937     { \
938       extent+=length; \
939       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
940         MaxTextExtent,sizeof(*interpret_text)); \
941       if (interpret_text == (char *) NULL) \
942         return((char *) NULL); \
943       q=interpret_text+strlen(interpret_text); \
944     } \
945   (void) CopyMagickString(q,(string),extent); \
946   q+=length; \
947 } \
948 RestoreMSCWarning
949 
950   char
951     *interpret_text,
952     *string;
953 
954   register char
955     *q;  /* current position in interpret_text */
956 
957   register const char
958     *p;  /* position in embed_text string being expanded */
959 
960   size_t
961     extent;  /* allocated length of interpret_text */
962 
963   MagickBooleanType
964     number;
965 
966   assert(image == NULL || image->signature == MagickCoreSignature);
967   assert(image_info == NULL || image_info->signature == MagickCoreSignature);
968   if ((image != (Image *) NULL) && (image->debug != MagickFalse))
969     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
970   else
971    if ((image_info != (ImageInfo *) NULL) && (image_info->debug != MagickFalse))
972      (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-image");
973   if (embed_text == (const char *) NULL)
974     return(ConstantString(""));
975   p=embed_text;
976   while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
977     p++;
978   if (*p == '\0')
979     return(ConstantString(""));
980   /*
981     Translate any embedded format characters.
982   */
983   interpret_text=AcquireString(embed_text);  /* new string with extra space */
984   extent=MagickPathExtent;  /* allocated space in string */
985   number=MagickFalse;  /* is last char a number? */
986   for (q=interpret_text; *p!='\0';
987     number=isdigit((int) ((unsigned char) *p)) ? MagickTrue : MagickFalse,p++)
988   {
989     /*
990       Interpret escape characters (e.g. Filename: %M).
991     */
992     *q='\0';
993     ExtendInterpretText(MagickPathExtent);
994     switch (*p)
995     {
996       case '\\':
997       {
998         switch (*(p+1))
999         {
1000           case '\0':
1001             continue;
1002           case 'r':  /* convert to RETURN */
1003           {
1004             *q++='\r';
1005             p++;
1006             continue;
1007           }
1008           case 'n':  /* convert to NEWLINE */
1009           {
1010             *q++='\n';
1011             p++;
1012             continue;
1013           }
1014           case '\n':  /* EOL removal UNIX,MacOSX */
1015           {
1016             p++;
1017             continue;
1018           }
1019           case '\r':  /* EOL removal DOS,Windows */
1020           {
1021             p++;
1022             if (*p == '\n') /* return-newline EOL */
1023               p++;
1024             continue;
1025           }
1026           default:
1027           {
1028             p++;
1029             *q++=(*p);
1030           }
1031         }
1032         continue;
1033       }
1034       case '&':
1035       {
1036         if (LocaleNCompare("&lt;",p,4) == 0)
1037           {
1038             *q++='<';
1039             p+=3;
1040           }
1041         else
1042           if (LocaleNCompare("&gt;",p,4) == 0)
1043             {
1044               *q++='>';
1045               p+=3;
1046             }
1047           else
1048             if (LocaleNCompare("&amp;",p,5) == 0)
1049               {
1050                 *q++='&';
1051                 p+=4;
1052               }
1053             else
1054               *q++=(*p);
1055         continue;
1056       }
1057       case '%':
1058         break;  /* continue to next set of handlers */
1059       default:
1060       {
1061         *q++=(*p);  /* any thing else is 'as normal' */
1062         continue;
1063       }
1064     }
1065     p++; /* advance beyond the percent */
1066     /*
1067       Doubled Percent - or percent at end of string.
1068     */
1069     if ((*p == '\0') || (*p == '\'') || (*p == '"'))
1070       p--;
1071     if (*p == '%')
1072       {
1073         *q++='%';
1074         continue;
1075       }
1076     /*
1077       Single letter escapes %c.
1078     */
1079     if (number != MagickFalse)
1080       {
1081         /*
1082           But only if not preceeded by a number!
1083         */
1084         *q++='%'; /* do NOT substitute the percent */
1085         p--;      /* back up one */
1086         continue;
1087       }
1088     string=GetMagickPropertyLetter(image_info,image,*p,exception);
1089     if (string != (char *) NULL)
1090       {
1091         AppendString2Text(string);
1092         string=DestroyString(string);
1093         continue;
1094       }
1095     (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1096       "UnknownImageProperty","\"%%%c\"",*p);
1097   }
1098   *q='\0';
1099   return(interpret_text);
1100 }
1101 
GetDelegateCommand(const ImageInfo * image_info,Image * image,const char * decode,const char * encode,ExceptionInfo * exception)1102 MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
1103   const char *decode,const char *encode,ExceptionInfo *exception)
1104 {
1105   char
1106     *command,
1107     **commands;
1108 
1109   const DelegateInfo
1110     *delegate_info;
1111 
1112   register ssize_t
1113     i;
1114 
1115   assert(image_info != (ImageInfo *) NULL);
1116   assert(image_info->signature == MagickCoreSignature);
1117   assert(image != (Image *) NULL);
1118   assert(image->signature == MagickCoreSignature);
1119   if (image->debug != MagickFalse)
1120     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1121 
1122   delegate_info=GetDelegateInfo(decode,encode,exception);
1123   if (delegate_info == (const DelegateInfo *) NULL)
1124     {
1125       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1126         "NoTagFound","`%s'",decode ? decode : encode);
1127       return((char *) NULL);
1128     }
1129   commands=StringToList(delegate_info->commands);
1130   if (commands == (char **) NULL)
1131     {
1132       (void) ThrowMagickException(exception,GetMagickModule(),
1133         ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
1134         encode);
1135       return((char *) NULL);
1136     }
1137   command=InterpretDelegateProperties((ImageInfo *) image_info,image,
1138     commands[0],exception);
1139   if (command == (char *) NULL)
1140     (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1141       "MemoryAllocationFailed","`%s'",commands[0]);
1142   /*
1143     Relinquish resources.
1144   */
1145   for (i=0; commands[i] != (char *) NULL; i++)
1146     commands[i]=DestroyString(commands[i]);
1147   commands=(char **) RelinquishMagickMemory(commands);
1148   return(command);
1149 }
1150 
1151 /*
1152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153 %                                                                             %
1154 %                                                                             %
1155 %                                                                             %
1156 %   G e t D e l e g a t e C o m m a n d s                                     %
1157 %                                                                             %
1158 %                                                                             %
1159 %                                                                             %
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1161 %
1162 %  GetDelegateCommands() returns the commands associated with a delegate.
1163 %
1164 %  The format of the GetDelegateCommands method is:
1165 %
1166 %      const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1167 %
1168 %  A description of each parameter follows:
1169 %
1170 %    o delegate_info:  The delegate info.
1171 %
1172 */
GetDelegateCommands(const DelegateInfo * delegate_info)1173 MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1174 {
1175   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1176 
1177   assert(delegate_info != (DelegateInfo *) NULL);
1178   assert(delegate_info->signature == MagickCoreSignature);
1179   return(delegate_info->commands);
1180 }
1181 
1182 /*
1183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184 %                                                                             %
1185 %                                                                             %
1186 %                                                                             %
1187 %   G e t D e l e g a t e I n f o                                             %
1188 %                                                                             %
1189 %                                                                             %
1190 %                                                                             %
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192 %
1193 %  GetDelegateInfo() returns any delegates associated with the specified tag.
1194 %
1195 %  The format of the GetDelegateInfo method is:
1196 %
1197 %      const DelegateInfo *GetDelegateInfo(const char *decode,
1198 %        const char *encode,ExceptionInfo *exception)
1199 %
1200 %  A description of each parameter follows:
1201 %
1202 %    o decode: Specifies the decode delegate we are searching for as a
1203 %      character string.
1204 %
1205 %    o encode: Specifies the encode delegate we are searching for as a
1206 %      character string.
1207 %
1208 %    o exception: return any errors or warnings in this structure.
1209 %
1210 */
GetDelegateInfo(const char * decode,const char * encode,ExceptionInfo * exception)1211 MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
1212   const char *encode,ExceptionInfo *exception)
1213 {
1214   register const DelegateInfo
1215     *p;
1216 
1217   assert(exception != (ExceptionInfo *) NULL);
1218   if (IsDelegateCacheInstantiated(exception) == MagickFalse)
1219     return((const DelegateInfo *) NULL);
1220   /*
1221     Search for named delegate.
1222   */
1223   LockSemaphoreInfo(delegate_semaphore);
1224   ResetLinkedListIterator(delegate_cache);
1225   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1226   if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
1227     {
1228       UnlockSemaphoreInfo(delegate_semaphore);
1229       return(p);
1230     }
1231   while (p != (const DelegateInfo *) NULL)
1232   {
1233     if (p->mode > 0)
1234       {
1235         if (LocaleCompare(p->decode,decode) == 0)
1236           break;
1237         p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1238         continue;
1239       }
1240     if (p->mode < 0)
1241       {
1242         if (LocaleCompare(p->encode,encode) == 0)
1243           break;
1244         p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1245         continue;
1246       }
1247     if (LocaleCompare(decode,p->decode) == 0)
1248       if (LocaleCompare(encode,p->encode) == 0)
1249         break;
1250     if (LocaleCompare(decode,"*") == 0)
1251       if (LocaleCompare(encode,p->encode) == 0)
1252         break;
1253     if (LocaleCompare(decode,p->decode) == 0)
1254       if (LocaleCompare(encode,"*") == 0)
1255         break;
1256     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1257   }
1258   if (p != (const DelegateInfo *) NULL)
1259     (void) InsertValueInLinkedList(delegate_cache,0,
1260       RemoveElementByValueFromLinkedList(delegate_cache,p));
1261   UnlockSemaphoreInfo(delegate_semaphore);
1262   return(p);
1263 }
1264 
1265 /*
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 %                                                                             %
1268 %                                                                             %
1269 %                                                                             %
1270 %   G e t D e l e g a t e I n f o L i s t                                     %
1271 %                                                                             %
1272 %                                                                             %
1273 %                                                                             %
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 %
1276 %  GetDelegateInfoList() returns any delegates that match the specified pattern.
1277 %
1278 %  The delegate of the GetDelegateInfoList function is:
1279 %
1280 %      const DelegateInfo **GetDelegateInfoList(const char *pattern,
1281 %        size_t *number_delegates,ExceptionInfo *exception)
1282 %
1283 %  A description of each parameter follows:
1284 %
1285 %    o pattern: Specifies a pointer to a text string containing a pattern.
1286 %
1287 %    o number_delegates:  This integer returns the number of delegates in the
1288 %      list.
1289 %
1290 %    o exception: return any errors or warnings in this structure.
1291 %
1292 */
1293 
1294 #if defined(__cplusplus) || defined(c_plusplus)
1295 extern "C" {
1296 #endif
1297 
DelegateInfoCompare(const void * x,const void * y)1298 static int DelegateInfoCompare(const void *x,const void *y)
1299 {
1300   const DelegateInfo
1301     **p,
1302     **q;
1303 
1304   int
1305     cmp;
1306 
1307   p=(const DelegateInfo **) x,
1308   q=(const DelegateInfo **) y;
1309   cmp=LocaleCompare((*p)->path,(*q)->path);
1310   if (cmp == 0)
1311     {
1312       if ((*p)->decode == (char *) NULL)
1313         if (((*p)->encode != (char *) NULL) &&
1314             ((*q)->encode != (char *) NULL))
1315           return(strcmp((*p)->encode,(*q)->encode));
1316       if (((*p)->decode != (char *) NULL) &&
1317           ((*q)->decode != (char *) NULL))
1318         return(strcmp((*p)->decode,(*q)->decode));
1319     }
1320   return(cmp);
1321 }
1322 
1323 #if defined(__cplusplus) || defined(c_plusplus)
1324 }
1325 #endif
1326 
GetDelegateInfoList(const char * pattern,size_t * number_delegates,ExceptionInfo * exception)1327 MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
1328   size_t *number_delegates,ExceptionInfo *exception)
1329 {
1330   const DelegateInfo
1331     **delegates;
1332 
1333   register const DelegateInfo
1334     *p;
1335 
1336   register ssize_t
1337     i;
1338 
1339   /*
1340     Allocate delegate list.
1341   */
1342   assert(number_delegates != (size_t *) NULL);
1343   assert(pattern != (char *) NULL);
1344   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1345 
1346   *number_delegates=0;
1347   p=GetDelegateInfo("*","*",exception);
1348   if (p == (const DelegateInfo *) NULL)
1349     return((const DelegateInfo **) NULL);
1350   delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
1351     GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
1352   if (delegates == (const DelegateInfo **) NULL)
1353     return((const DelegateInfo **) NULL);
1354   /*
1355     Generate delegate list.
1356   */
1357   LockSemaphoreInfo(delegate_semaphore);
1358   ResetLinkedListIterator(delegate_cache);
1359   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1360   for (i=0; p != (const DelegateInfo *) NULL; )
1361   {
1362     if( (p->stealth == MagickFalse) &&
1363         ( GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse ||
1364           GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse) )
1365       delegates[i++]=p;
1366     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1367   }
1368   UnlockSemaphoreInfo(delegate_semaphore);
1369   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
1370   delegates[i]=(DelegateInfo *) NULL;
1371   *number_delegates=(size_t) i;
1372   return(delegates);
1373 }
1374 
1375 /*
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 %                                                                             %
1378 %                                                                             %
1379 %                                                                             %
1380 %   G e t D e l e g a t e L i s t                                             %
1381 %                                                                             %
1382 %                                                                             %
1383 %                                                                             %
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 %
1386 %  GetDelegateList() returns any image format delegates that match the
1387 %  specified  pattern.
1388 %
1389 %  The format of the GetDelegateList function is:
1390 %
1391 %      char **GetDelegateList(const char *pattern,
1392 %        size_t *number_delegates,ExceptionInfo *exception)
1393 %
1394 %  A description of each parameter follows:
1395 %
1396 %    o pattern: Specifies a pointer to a text string containing a pattern.
1397 %
1398 %    o number_delegates:  This integer returns the number of delegates
1399 %      in the list.
1400 %
1401 %    o exception: return any errors or warnings in this structure.
1402 %
1403 */
1404 
1405 #if defined(__cplusplus) || defined(c_plusplus)
1406 extern "C" {
1407 #endif
1408 
DelegateCompare(const void * x,const void * y)1409 static int DelegateCompare(const void *x,const void *y)
1410 {
1411   register const char
1412     **p,
1413     **q;
1414 
1415   p=(const char **) x;
1416   q=(const char **) y;
1417   return(LocaleCompare(*p,*q));
1418 }
1419 
1420 #if defined(__cplusplus) || defined(c_plusplus)
1421 }
1422 #endif
1423 
GetDelegateList(const char * pattern,size_t * number_delegates,ExceptionInfo * exception)1424 MagickExport char **GetDelegateList(const char *pattern,
1425   size_t *number_delegates,ExceptionInfo *exception)
1426 {
1427   char
1428     **delegates;
1429 
1430   register const DelegateInfo
1431     *p;
1432 
1433   register ssize_t
1434     i;
1435 
1436   /*
1437     Allocate delegate list.
1438   */
1439   assert(pattern != (char *) NULL);
1440   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1441 
1442   assert(number_delegates != (size_t *) NULL);
1443   *number_delegates=0;
1444   p=GetDelegateInfo("*","*",exception);
1445   if (p == (const DelegateInfo *) NULL)
1446     return((char **) NULL);
1447   delegates=(char **) AcquireQuantumMemory((size_t)
1448     GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
1449   if (delegates == (char **) NULL)
1450     return((char **) NULL);
1451   LockSemaphoreInfo(delegate_semaphore);
1452   ResetLinkedListIterator(delegate_cache);
1453   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1454   for (i=0; p != (const DelegateInfo *) NULL; )
1455   {
1456     if( (p->stealth == MagickFalse) &&
1457         GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse )
1458       delegates[i++]=ConstantString(p->decode);
1459     if( (p->stealth == MagickFalse) &&
1460         GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse )
1461       delegates[i++]=ConstantString(p->encode);
1462     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
1463   }
1464   UnlockSemaphoreInfo(delegate_semaphore);
1465   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
1466   delegates[i]=(char *) NULL;
1467   *number_delegates=(size_t) i;
1468   return(delegates);
1469 }
1470 
1471 /*
1472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 %                                                                             %
1474 %                                                                             %
1475 %                                                                             %
1476 %   G e t D e l e g a t e M o d e                                             %
1477 %                                                                             %
1478 %                                                                             %
1479 %                                                                             %
1480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1481 %
1482 %  GetDelegateMode() returns the mode of the delegate.
1483 %
1484 %  The format of the GetDelegateMode method is:
1485 %
1486 %      ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
1487 %
1488 %  A description of each parameter follows:
1489 %
1490 %    o delegate_info:  The delegate info.
1491 %
1492 */
GetDelegateMode(const DelegateInfo * delegate_info)1493 MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
1494 {
1495   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1496 
1497   assert(delegate_info != (DelegateInfo *) NULL);
1498   assert(delegate_info->signature == MagickCoreSignature);
1499   return(delegate_info->mode);
1500 }
1501 
1502 /*
1503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1504 %                                                                             %
1505 %                                                                             %
1506 %                                                                             %
1507 +   G e t D e l e g a t e T h r e a d S u p p o r t                           %
1508 %                                                                             %
1509 %                                                                             %
1510 %                                                                             %
1511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1512 %
1513 %  GetDelegateThreadSupport() returns MagickTrue if the delegate supports
1514 %  threads.
1515 %
1516 %  The format of the GetDelegateThreadSupport method is:
1517 %
1518 %      MagickBooleanType GetDelegateThreadSupport(
1519 %        const DelegateInfo *delegate_info)
1520 %
1521 %  A description of each parameter follows:
1522 %
1523 %    o delegate_info:  The delegate info.
1524 %
1525 */
GetDelegateThreadSupport(const DelegateInfo * delegate_info)1526 MagickExport MagickBooleanType GetDelegateThreadSupport(
1527   const DelegateInfo *delegate_info)
1528 {
1529   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1530 
1531   assert(delegate_info != (DelegateInfo *) NULL);
1532   assert(delegate_info->signature == MagickCoreSignature);
1533   return(delegate_info->thread_support);
1534 }
1535 
1536 /*
1537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 %                                                                             %
1539 %                                                                             %
1540 %                                                                             %
1541 +   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                     %
1542 %                                                                             %
1543 %                                                                             %
1544 %                                                                             %
1545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 %
1547 %  IsDelegateCacheInstantiated() determines if the delegate cache is
1548 %  instantiated.  If not, it instantiates the cache and returns it.
1549 %
1550 %  The format of the IsDelegateInstantiated method is:
1551 %
1552 %      MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
1553 %
1554 %  A description of each parameter follows.
1555 %
1556 %    o exception: return any errors or warnings in this structure.
1557 %
1558 */
IsDelegateCacheInstantiated(ExceptionInfo * exception)1559 static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
1560 {
1561   if (delegate_cache == (LinkedListInfo *) NULL)
1562     {
1563       if (delegate_semaphore == (SemaphoreInfo *) NULL)
1564         ActivateSemaphoreInfo(&delegate_semaphore);
1565       LockSemaphoreInfo(delegate_semaphore);
1566       if (delegate_cache == (LinkedListInfo *) NULL)
1567         delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
1568       UnlockSemaphoreInfo(delegate_semaphore);
1569     }
1570   return(delegate_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
1571 }
1572 
1573 /*
1574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575 %                                                                             %
1576 %                                                                             %
1577 %                                                                             %
1578 %   I n v o k e D e l e g a t e                                               %
1579 %                                                                             %
1580 %                                                                             %
1581 %                                                                             %
1582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1583 %
1584 %  InvokeDelegate replaces any embedded formatting characters with the
1585 %  appropriate image attribute and executes the resulting command.  MagickFalse
1586 %  is returned if the commands execute with success otherwise MagickTrue.
1587 %
1588 %  The format of the InvokeDelegate method is:
1589 %
1590 %      MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
1591 %        const char *decode,const char *encode,ExceptionInfo *exception)
1592 %
1593 %  A description of each parameter follows:
1594 %
1595 %    o image_info: the imageInfo.
1596 %
1597 %    o image: the image.
1598 %
1599 %    o exception: return any errors or warnings in this structure.
1600 %
1601 */
1602 
CopyDelegateFile(const char * source,const char * destination,const MagickBooleanType overwrite)1603 static MagickBooleanType CopyDelegateFile(const char *source,
1604   const char *destination,const MagickBooleanType overwrite)
1605 {
1606   int
1607     destination_file,
1608     source_file;
1609 
1610   MagickBooleanType
1611     status;
1612 
1613   register size_t
1614     i;
1615 
1616   size_t
1617     length,
1618     quantum;
1619 
1620   ssize_t
1621     count;
1622 
1623   struct stat
1624     attributes;
1625 
1626   unsigned char
1627     *buffer;
1628 
1629   /*
1630     Copy source file to destination.
1631   */
1632   assert(source != (const char *) NULL);
1633   assert(destination != (char *) NULL);
1634   if (overwrite == MagickFalse)
1635     {
1636       status=GetPathAttributes(destination,&attributes);
1637       if (status != MagickFalse)
1638         return(MagickTrue);
1639     }
1640   destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
1641   if (destination_file == -1)
1642     return(MagickFalse);
1643   source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
1644   if (source_file == -1)
1645     {
1646       (void) close(destination_file);
1647       return(MagickFalse);
1648     }
1649   quantum=(size_t) MagickMaxBufferExtent;
1650   if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
1651     quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1652   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1653   if (buffer == (unsigned char *) NULL)
1654     {
1655       (void) close(source_file);
1656       (void) close(destination_file);
1657       return(MagickFalse);
1658     }
1659   length=0;
1660   for (i=0; ; i+=count)
1661   {
1662     count=(ssize_t) read(source_file,buffer,quantum);
1663     if (count <= 0)
1664       break;
1665     length=(size_t) count;
1666     count=(ssize_t) write(destination_file,buffer,length);
1667     if ((size_t) count != length)
1668       break;
1669   }
1670   (void) close(destination_file);
1671   (void) close(source_file);
1672   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1673   return(i != 0 ? MagickTrue : MagickFalse);
1674 }
1675 
InvokeDelegate(ImageInfo * image_info,Image * image,const char * decode,const char * encode,ExceptionInfo * exception)1676 MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1677   Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1678 {
1679   char
1680     *command,
1681     **commands,
1682     input_filename[MagickPathExtent],
1683     output_filename[MagickPathExtent];
1684 
1685   const DelegateInfo
1686     *delegate_info;
1687 
1688   MagickBooleanType
1689     status,
1690     temporary;
1691 
1692   PolicyRights
1693     rights;
1694 
1695   register ssize_t
1696     i;
1697 
1698   /*
1699     Get delegate.
1700   */
1701   assert(image_info != (ImageInfo *) NULL);
1702   assert(image_info->signature == MagickCoreSignature);
1703   assert(image != (Image *) NULL);
1704   assert(image->signature == MagickCoreSignature);
1705   if (image->debug != MagickFalse)
1706     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1707   rights=ExecutePolicyRights;
1708   if (IsRightsAuthorized(DelegatePolicyDomain,rights,decode) == MagickFalse)
1709     {
1710       errno=EPERM;
1711       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1712         "NotAuthorized","`%s'",decode);
1713       return(MagickFalse);
1714     }
1715   if (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse)
1716     {
1717       errno=EPERM;
1718       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1719         "NotAuthorized","`%s'",encode);
1720       return(MagickFalse);
1721     }
1722   temporary=*image->filename == '\0' ? MagickTrue : MagickFalse;
1723   if ((temporary != MagickFalse) && (AcquireUniqueFilename(image->filename) ==
1724       MagickFalse))
1725     {
1726       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1727         image->filename);
1728       return(MagickFalse);
1729     }
1730   delegate_info=GetDelegateInfo(decode,encode,exception);
1731   if (delegate_info == (DelegateInfo *) NULL)
1732     {
1733       if (temporary != MagickFalse)
1734         (void) RelinquishUniqueFileResource(image->filename);
1735       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1736         "NoTagFound","`%s'",decode ? decode : encode);
1737       return(MagickFalse);
1738     }
1739   if (*image_info->filename == '\0')
1740     {
1741       if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
1742         {
1743           if (temporary != MagickFalse)
1744             (void) RelinquishUniqueFileResource(image->filename);
1745           ThrowFileException(exception,FileOpenError,
1746             "UnableToCreateTemporaryFile",image_info->filename);
1747           return(MagickFalse);
1748         }
1749       image_info->temporary=MagickTrue;
1750     }
1751   if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
1752         (delegate_info->encode != (char *) NULL)) ||
1753        ((encode != (const char *) NULL) &&
1754         (delegate_info->decode != (char *) NULL))))
1755     {
1756       char
1757         *magick;
1758 
1759       ImageInfo
1760         *clone_info;
1761 
1762       register Image
1763         *p;
1764 
1765       /*
1766         Delegate requires a particular image format.
1767       */
1768       if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1769         {
1770           ThrowFileException(exception,FileOpenError,
1771             "UnableToCreateTemporaryFile",image_info->unique);
1772           return(MagickFalse);
1773         }
1774       magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
1775         delegate_info->encode : delegate_info->decode,exception);
1776       if (magick == (char *) NULL)
1777         {
1778           (void) RelinquishUniqueFileResource(image_info->unique);
1779           if (temporary != MagickFalse)
1780             (void) RelinquishUniqueFileResource(image->filename);
1781           (void) ThrowMagickException(exception,GetMagickModule(),
1782             DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1783           return(MagickFalse);
1784         }
1785       LocaleUpper(magick);
1786       clone_info=CloneImageInfo(image_info);
1787       (void) CopyMagickString((char *) clone_info->magick,magick,
1788         MagickPathExtent);
1789       if (LocaleCompare(magick,"NULL") != 0)
1790         (void) CopyMagickString(image->magick,magick,MagickPathExtent);
1791       magick=DestroyString(magick);
1792       (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:",
1793         delegate_info->decode);
1794       (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1795         exception);
1796       (void) CopyMagickString(clone_info->filename,image_info->filename,
1797         MagickPathExtent);
1798       (void) CopyMagickString(image_info->filename,image->filename,
1799         MagickPathExtent);
1800       for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1801       {
1802         (void) FormatLocaleString(p->filename,MagickPathExtent,"%s:%s",
1803           delegate_info->decode,clone_info->filename);
1804         status=WriteImage(clone_info,p,exception);
1805         if (status == MagickFalse)
1806           {
1807             (void) RelinquishUniqueFileResource(image_info->unique);
1808             if (temporary != MagickFalse)
1809               (void) RelinquishUniqueFileResource(image->filename);
1810             clone_info=DestroyImageInfo(clone_info);
1811             (void) ThrowMagickException(exception,GetMagickModule(),
1812               DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1813             return(MagickFalse);
1814           }
1815         if (clone_info->adjoin != MagickFalse)
1816           break;
1817       }
1818       (void) RelinquishUniqueFileResource(image_info->unique);
1819       clone_info=DestroyImageInfo(clone_info);
1820     }
1821   /*
1822     Invoke delegate.
1823   */
1824   commands=StringToList(delegate_info->commands);
1825   if (commands == (char **) NULL)
1826     {
1827       if (temporary != MagickFalse)
1828         (void) RelinquishUniqueFileResource(image->filename);
1829       (void) ThrowMagickException(exception,GetMagickModule(),
1830         ResourceLimitError,"MemoryAllocationFailed","`%s'",
1831         decode ? decode : encode);
1832       return(MagickFalse);
1833     }
1834   command=(char *) NULL;
1835   status=MagickTrue;
1836   (void) CopyMagickString(output_filename,image_info->filename,
1837     MagickPathExtent);
1838   (void) CopyMagickString(input_filename,image->filename,MagickPathExtent);
1839   for (i=0; commands[i] != (char *) NULL; i++)
1840   {
1841     (void) AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1842     if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1843       {
1844         ThrowFileException(exception,FileOpenError,
1845           "UnableToCreateTemporaryFile",image_info->unique);
1846         break;
1847       }
1848     if (LocaleCompare(decode,"SCAN") != 0)
1849       {
1850         status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1851         if (status == MagickFalse)
1852           {
1853             ThrowFileException(exception,FileOpenError,
1854               "UnableToCreateTemporaryFile",input_filename);
1855             break;
1856           }
1857       }
1858     status=MagickTrue;
1859     command=InterpretDelegateProperties(image_info,image,commands[i],exception);
1860     if (command != (char *) NULL)
1861       {
1862         /*
1863           Execute delegate.
1864         */
1865         if (ExternalDelegateCommand(delegate_info->spawn,image_info->verbose,
1866           command,(char *) NULL,exception) != 0)
1867           status=MagickFalse;
1868         if (delegate_info->spawn != MagickFalse)
1869           {
1870             ssize_t
1871               count;
1872 
1873             /*
1874               Wait for input file to 'disappear', or maximum 2 seconds.
1875             */
1876             count=20;
1877             while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1878               (void) MagickDelay(100);  /* sleep 0.1 seconds */
1879           }
1880         command=DestroyString(command);
1881       }
1882     if (LocaleCompare(decode,"SCAN") != 0)
1883       {
1884         if (CopyDelegateFile(image->filename,input_filename,MagickFalse) == MagickFalse)
1885           (void) RelinquishUniqueFileResource(input_filename);
1886       }
1887     if ((strcmp(input_filename,output_filename) != 0) &&
1888         (CopyDelegateFile(image_info->filename,output_filename,MagickTrue) == MagickFalse))
1889       (void) RelinquishUniqueFileResource(output_filename);
1890     if (image_info->temporary != MagickFalse)
1891       (void) RelinquishUniqueFileResource(image_info->filename);
1892     (void) RelinquishUniqueFileResource(image_info->unique);
1893     (void) RelinquishUniqueFileResource(image_info->filename);
1894     (void) RelinquishUniqueFileResource(image->filename);
1895     if (status == MagickFalse)
1896       {
1897         (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1898           "DelegateFailed","`%s'",commands[i]);
1899         break;
1900       }
1901     commands[i]=DestroyString(commands[i]);
1902   }
1903   (void) CopyMagickString(image_info->filename,output_filename,
1904     MagickPathExtent);
1905   (void) CopyMagickString(image->filename,input_filename,MagickPathExtent);
1906   /*
1907     Relinquish resources.
1908   */
1909   for ( ; commands[i] != (char *) NULL; i++)
1910     commands[i]=DestroyString(commands[i]);
1911   commands=(char **) RelinquishMagickMemory(commands);
1912   if (temporary != MagickFalse)
1913     (void) RelinquishUniqueFileResource(image->filename);
1914   return(status);
1915 }
1916 
1917 /*
1918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1919 %                                                                             %
1920 %                                                                             %
1921 %                                                                             %
1922 %  L i s t D e l e g a t e I n f o                                            %
1923 %                                                                             %
1924 %                                                                             %
1925 %                                                                             %
1926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1927 %
1928 %  ListDelegateInfo() lists the image formats to a file.
1929 %
1930 %  The format of the ListDelegateInfo method is:
1931 %
1932 %      MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1933 %
1934 %  A description of each parameter follows.
1935 %
1936 %    o file:  An pointer to a FILE.
1937 %
1938 %    o exception: return any errors or warnings in this structure.
1939 %
1940 */
ListDelegateInfo(FILE * file,ExceptionInfo * exception)1941 MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1942   ExceptionInfo *exception)
1943 {
1944   const DelegateInfo
1945     **delegate_info;
1946 
1947   char
1948     **commands,
1949     delegate[MagickPathExtent];
1950 
1951   const char
1952     *path;
1953 
1954   register ssize_t
1955     i;
1956 
1957   size_t
1958     number_delegates;
1959 
1960   ssize_t
1961     j;
1962 
1963   if (file == (const FILE *) NULL)
1964     file=stdout;
1965   delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1966   if (delegate_info == (const DelegateInfo **) NULL)
1967     return(MagickFalse);
1968   path=(const char *) NULL;
1969   for (i=0; i < (ssize_t) number_delegates; i++)
1970   {
1971     if (delegate_info[i]->stealth != MagickFalse)
1972       continue;
1973     if ((path == (const char *) NULL) ||
1974         (LocaleCompare(path,delegate_info[i]->path) != 0))
1975       {
1976         if (delegate_info[i]->path != (char *) NULL)
1977           (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1978         (void) FormatLocaleFile(file,"Delegate                Command\n");
1979         (void) FormatLocaleFile(file,
1980           "-------------------------------------------------"
1981           "------------------------------\n");
1982       }
1983     path=delegate_info[i]->path;
1984     *delegate='\0';
1985     if (delegate_info[i]->encode != (char *) NULL)
1986       (void) CopyMagickString(delegate,delegate_info[i]->encode,
1987         MagickPathExtent);
1988     (void) ConcatenateMagickString(delegate,"        ",MagickPathExtent);
1989     delegate[8]='\0';
1990     commands=StringToList(delegate_info[i]->commands);
1991     if (commands == (char **) NULL)
1992       continue;
1993     (void) FormatLocaleFile(file,"%11s%c=%c%s  ",delegate_info[i]->decode ?
1994       delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1995       delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1996     StripString(commands[0]);
1997     (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
1998     for (j=1; commands[j] != (char *) NULL; j++)
1999     {
2000       StripString(commands[j]);
2001       (void) FormatLocaleFile(file,"                     \"%s\"\n",commands[j]);
2002     }
2003     for (j=0; commands[j] != (char *) NULL; j++)
2004       commands[j]=DestroyString(commands[j]);
2005     commands=(char **) RelinquishMagickMemory(commands);
2006   }
2007   (void) fflush(file);
2008   delegate_info=(const DelegateInfo **)
2009     RelinquishMagickMemory((void *) delegate_info);
2010   return(MagickTrue);
2011 }
2012 
2013 /*
2014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015 %                                                                             %
2016 %                                                                             %
2017 %                                                                             %
2018 +   L o a d D e l e g a t e C a c h e                                         %
2019 %                                                                             %
2020 %                                                                             %
2021 %                                                                             %
2022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023 %
2024 %  LoadDelegateCache() loads the delegate configurations which provides a
2025 %  mapping between delegate attributes and a delegate name.
2026 %
2027 %  The format of the LoadDelegateCache method is:
2028 %
2029 %      MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
2030 %        const char *xml,const char *filename,const size_t depth,
2031 %        ExceptionInfo *exception)
2032 %
2033 %  A description of each parameter follows:
2034 %
2035 %    o xml:  The delegate list in XML format.
2036 %
2037 %    o filename:  The delegate list filename.
2038 %
2039 %    o depth: depth of <include /> statements.
2040 %
2041 %    o exception: return any errors or warnings in this structure.
2042 %
2043 */
LoadDelegateCache(LinkedListInfo * cache,const char * xml,const char * filename,const size_t depth,ExceptionInfo * exception)2044 static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
2045   const char *xml,const char *filename,const size_t depth,
2046   ExceptionInfo *exception)
2047 {
2048   char
2049     keyword[MagickPathExtent],
2050     *token;
2051 
2052   const char
2053     *q;
2054 
2055   DelegateInfo
2056     *delegate_info;
2057 
2058   MagickStatusType
2059     status;
2060 
2061   size_t
2062     extent;
2063 
2064   /*
2065     Load the delegate map file.
2066   */
2067   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
2068     "Loading delegate configuration file \"%s\" ...",filename);
2069   if (xml == (const char *) NULL)
2070     return(MagickFalse);
2071   status=MagickTrue;
2072   delegate_info=(DelegateInfo *) NULL;
2073   token=AcquireString(xml);
2074   extent=strlen(token)+MagickPathExtent;
2075   for (q=(const char *) xml; *q != '\0'; )
2076   {
2077     /*
2078       Interpret XML.
2079     */
2080     (void) GetNextToken(q,&q,extent,token);
2081     if (*token == '\0')
2082       break;
2083     (void) CopyMagickString(keyword,token,MagickPathExtent);
2084     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
2085       {
2086         /*
2087           Doctype element.
2088         */
2089         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
2090           (void) GetNextToken(q,&q,extent,token);
2091         continue;
2092       }
2093     if (LocaleNCompare(keyword,"<!--",4) == 0)
2094       {
2095         /*
2096           Comment element.
2097         */
2098         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
2099           (void) GetNextToken(q,&q,extent,token);
2100         continue;
2101       }
2102     if (LocaleCompare(keyword,"<include") == 0)
2103       {
2104         /*
2105           Include element.
2106         */
2107         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
2108         {
2109           (void) CopyMagickString(keyword,token,MagickPathExtent);
2110           (void) GetNextToken(q,&q,extent,token);
2111           if (*token != '=')
2112             continue;
2113           (void) GetNextToken(q,&q,extent,token);
2114           if (LocaleCompare(keyword,"file") == 0)
2115             {
2116               if (depth > MagickMaxRecursionDepth)
2117                 (void) ThrowMagickException(exception,GetMagickModule(),
2118                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
2119               else
2120                 {
2121                   char
2122                     path[MagickPathExtent],
2123                     *file_xml;
2124 
2125                   GetPathComponent(filename,HeadPath,path);
2126                   if (*path != '\0')
2127                     (void) ConcatenateMagickString(path,DirectorySeparator,
2128                       MagickPathExtent);
2129                   if (*token == *DirectorySeparator)
2130                     (void) CopyMagickString(path,token,MagickPathExtent);
2131                   else
2132                     (void) ConcatenateMagickString(path,token,MagickPathExtent);
2133                   file_xml=FileToXML(path,~0UL);
2134                   if (file_xml != (char *) NULL)
2135                     {
2136                       status&=LoadDelegateCache(cache,file_xml,path,
2137                         depth+1,exception);
2138                       file_xml=DestroyString(file_xml);
2139                     }
2140                 }
2141             }
2142         }
2143         continue;
2144       }
2145     if (LocaleCompare(keyword,"<delegate") == 0)
2146       {
2147         /*
2148           Delegate element.
2149         */
2150         delegate_info=(DelegateInfo *) AcquireCriticalMemory(
2151           sizeof(*delegate_info));
2152         (void) memset(delegate_info,0,sizeof(*delegate_info));
2153         delegate_info->path=ConstantString(filename);
2154         delegate_info->thread_support=MagickTrue;
2155         delegate_info->signature=MagickCoreSignature;
2156         continue;
2157       }
2158     if (delegate_info == (DelegateInfo *) NULL)
2159       continue;
2160     if ((LocaleCompare(keyword,"/>") == 0) ||
2161         (LocaleCompare(keyword,"</policy>") == 0))
2162       {
2163         status=AppendValueToLinkedList(cache,delegate_info);
2164         if (status == MagickFalse)
2165           (void) ThrowMagickException(exception,GetMagickModule(),
2166             ResourceLimitError,"MemoryAllocationFailed","`%s'",
2167             delegate_info->commands);
2168         delegate_info=(DelegateInfo *) NULL;
2169         continue;
2170       }
2171     (void) GetNextToken(q,(const char **) NULL,extent,token);
2172     if (*token != '=')
2173       continue;
2174     (void) GetNextToken(q,&q,extent,token);
2175     (void) GetNextToken(q,&q,extent,token);
2176     switch (*keyword)
2177     {
2178       case 'C':
2179       case 'c':
2180       {
2181         if (LocaleCompare((char *) keyword,"command") == 0)
2182           {
2183             char
2184               *commands;
2185 
2186             commands=AcquireString(token);
2187 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
2188             if (strchr(commands,'@') != (char *) NULL)
2189               {
2190                 char
2191                   path[MagickPathExtent];
2192 
2193                 NTGhostscriptEXE(path,MagickPathExtent);
2194                 (void) SubstituteString((char **) &commands,"@PSDelegate@",
2195                   path);
2196                 (void) SubstituteString((char **) &commands,"\\","/");
2197               }
2198             (void) SubstituteString((char **) &commands,"&quot;","\"");
2199 #else
2200             (void) SubstituteString((char **) &commands,"&quot;","'");
2201 #endif
2202             (void) SubstituteString((char **) &commands,"&amp;","&");
2203             (void) SubstituteString((char **) &commands,"&gt;",">");
2204             (void) SubstituteString((char **) &commands,"&lt;","<");
2205             delegate_info->commands=commands;
2206             break;
2207           }
2208         break;
2209       }
2210       case 'D':
2211       case 'd':
2212       {
2213         if (LocaleCompare((char *) keyword,"decode") == 0)
2214           {
2215             delegate_info->decode=ConstantString(token);
2216             delegate_info->mode=1;
2217             break;
2218           }
2219         break;
2220       }
2221       case 'E':
2222       case 'e':
2223       {
2224         if (LocaleCompare((char *) keyword,"encode") == 0)
2225           {
2226             delegate_info->encode=ConstantString(token);
2227             delegate_info->mode=(-1);
2228             break;
2229           }
2230         break;
2231       }
2232       case 'M':
2233       case 'm':
2234       {
2235         if (LocaleCompare((char *) keyword,"mode") == 0)
2236           {
2237             delegate_info->mode=1;
2238             if (LocaleCompare(token,"bi") == 0)
2239               delegate_info->mode=0;
2240             else
2241               if (LocaleCompare(token,"encode") == 0)
2242                 delegate_info->mode=(-1);
2243             break;
2244           }
2245         break;
2246       }
2247       case 'S':
2248       case 's':
2249       {
2250         if (LocaleCompare((char *) keyword,"spawn") == 0)
2251           {
2252             delegate_info->spawn=IsStringTrue(token);
2253             break;
2254           }
2255         if (LocaleCompare((char *) keyword,"stealth") == 0)
2256           {
2257             delegate_info->stealth=IsStringTrue(token);
2258             break;
2259           }
2260         break;
2261       }
2262       case 'T':
2263       case 't':
2264       {
2265         if (LocaleCompare((char *) keyword,"thread-support") == 0)
2266           {
2267             delegate_info->thread_support=IsStringTrue(token);
2268             if (delegate_info->thread_support == MagickFalse)
2269               delegate_info->semaphore=AcquireSemaphoreInfo();
2270             break;
2271           }
2272         break;
2273       }
2274       default:
2275         break;
2276     }
2277   }
2278   token=(char *) RelinquishMagickMemory(token);
2279   return(status != 0 ? MagickTrue : MagickFalse);
2280 }
2281