blob: 156bfba729449b0106d63c946fef636fc487278b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% DDDD EEEEE L EEEEE GGGG AAA TTTTT EEEEE %
6% D D E L E G A A T E %
7% D D EEE L EEE G GG AAAAA T EEE %
8% D D E L E G G A A T E %
9% DDDD EEEEE LLLLL EEEEE GGG A A T EEEEE %
10% %
11% %
12% MagickCore Methods to Read/Write/Invoke Delegates %
13% %
14% Software Design %
cristyde984cd2013-12-01 14:49:27 +000015% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000016% October 1998 %
17% %
18% %
Cristy93b707b2017-12-06 07:05:51 -050019% Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000020% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
Cristyf19d4142017-04-24 11:34:30 -040025% https://www.imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000026% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35% The Delegates methods associate a set of commands with a particular
36% image format. ImageMagick uses delegates for formats it does not handle
37% directly.
38%
39% Thanks to Bob Friesenhahn for the initial inspiration and design of the
40% delegates methods.
41%
42%
43*/
44
45/*
46 Include declarations.
47*/
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/studio.h"
Cristy8be0b1e2016-05-13 20:45:40 -040049#include "MagickCore/artifact.h"
50#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/blob.h"
52#include "MagickCore/client.h"
53#include "MagickCore/configure.h"
54#include "MagickCore/constitute.h"
55#include "MagickCore/delegate.h"
cristy5ff4eaf2011-09-03 01:38:02 +000056#include "MagickCore/delegate-private.h"
cristy4c08aed2011-07-01 19:47:50 +000057#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
Cristy8be0b1e2016-05-13 20:45:40 -040059#include "MagickCore/fx-private.h"
dirk007e9252015-01-15 21:02:57 +000060#include "MagickCore/image-private.h"
dirkabed7e22016-01-31 17:10:21 +010061#include "MagickCore/linked-list.h"
cristy4c08aed2011-07-01 19:47:50 +000062#include "MagickCore/list.h"
63#include "MagickCore/memory_.h"
Dirk Lemstra06344a02017-10-15 10:10:01 +020064#include "MagickCore/memory-private.h"
cristy1e37e8f2014-02-21 17:05:37 +000065#include "MagickCore/nt-base-private.h"
Cristy8be0b1e2016-05-13 20:45:40 -040066#include "MagickCore/option.h"
cristy4c08aed2011-07-01 19:47:50 +000067#include "MagickCore/policy.h"
Cristy8be0b1e2016-05-13 20:45:40 -040068#include "MagickCore/property.h"
cristy4c08aed2011-07-01 19:47:50 +000069#include "MagickCore/resource_.h"
70#include "MagickCore/semaphore.h"
Cristy8be0b1e2016-05-13 20:45:40 -040071#include "MagickCore/signature.h"
cristy4c08aed2011-07-01 19:47:50 +000072#include "MagickCore/string_.h"
73#include "MagickCore/token.h"
Cristy8be0b1e2016-05-13 20:45:40 -040074#include "MagickCore/token-private.h"
cristy4c08aed2011-07-01 19:47:50 +000075#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000076#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000077#include "MagickCore/xml-tree.h"
cristy3291f512014-03-16 22:16:22 +000078#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000079
80/*
81 Define declarations.
82*/
83#define DelegateFilename "delegates.xml"
84
85/*
86 Declare delegate map.
87*/
88static const char
89 *DelegateMap = (const char *)
90 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
91 "<delegatemap>"
92 " <delegate decode=\"autotrace\" stealth=\"True\" command=\"&quot;autotrace&quot; -output-format svg -output-file &quot;%o&quot; &quot;%i&quot;\"/>"
93 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\"&quot;mplayer&quot; &quot;%i&quot; -really-quiet -ao null -vo png:z=3\"/>"
anthonyb8071362012-05-23 03:56:25 +000094 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; http://www.imagemagick.org/; rm &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000095 " <delegate decode=\"cgm\" thread-support=\"False\" command=\"&quot;ralcgm&quot; -d ps -oC &lt; &quot;%i&quot; &gt; &quot;%o&quot; 2&gt; &quot;%u&quot;\"/>"
cristy12e4d2e2015-01-20 17:00:03 +000096 " <delegate decode=\"dng:decode\" command=\"&quot;ufraw-batch&quot; --silent --create-id=also --out-type=png --out-depth=16 &quot;--output=%u.png&quot; &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +000097 " <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +000098 " <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristya9c90a52015-01-05 23:23:35 +000099 " <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000100 " <delegate decode=\"fig\" command=\"&quot;fig2dev&quot; -L ps &quot;%i&quot; &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000101 " <delegate decode=\"hpg\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot; mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
cristy7c278052012-11-18 01:09:35 +0000102 " <delegate decode=\"hpgl\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot; mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000103 " <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
104 " <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
Cristy5f478ca2016-05-14 18:43:20 -0400105 " <delegate decode=\"https\" command=\"&quot;curl&quot; -s -k -L -o &quot;%o&quot; &quot;https:%M&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000106 " <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
107 " <delegate decode=\"man\" command=\"&quot;groff&quot; -man -Tps &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
cristy53798ff2015-05-08 18:28:24 +0000108 " <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -nostdin -v -1 -vframes %S -i &quot;%i&quot; -vcodec pam -an -f rawvideo -y &quot;%u.pam&quot; 2&gt; &quot;%Z&quot;\"/>"
Cristy5f478ca2016-05-14 18:43:20 -0400109 " <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -nostdin -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i &quot;%M%%d.jpg&quot; &quot;%u.%m&quot; 2&gt; &quot;%Z&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000110 " <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;\"/>"
111 " <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
112 " <delegate decode=\"pcl:mono\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
cristya9c90a52015-01-05 23:23:35 +0000113 " <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
114 " <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000115 " <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\"&quot;ppmtoilbm&quot; -24if &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
116 " <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\"&quot;gimp&quot; &quot;%i&quot;\"/>"
117 " <delegate decode=\"pov\" command=\"&quot;povray&quot; &quot;+i&quot;%i&quot;&quot; -D0 +o&quot;%o&quot; +fn%q +w%w +h%h +a -q9 -kfi&quot;%s&quot; -kff&quot;%n&quot; &quot;convert&quot; -concatenate &quot;%o*.png&quot; &quot;%o&quot;\"/>"
cristya9c90a52015-01-05 23:23:35 +0000118 " <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=eps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000119 " <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000120 " <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr &quot;%i&quot;\"/>"
cristya7bb1c42012-11-18 00:27:49 +0000121 " <delegate decode=\"ps:alpha\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
122 " <delegate decode=\"ps:bbox\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bbox&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
123 " <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
124 " <delegate decode=\"ps:color\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
125 " <delegate decode=\"ps:mono\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000126 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\"&quot;rawtorle&quot; -o &quot;%o&quot; -v &quot;%i&quot;\"/>"
127 " <delegate decode=\"scan\" command=\"&quot;scanimage&quot; -d &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
Cristy5f478ca2016-05-14 18:43:20 -0400128 " <delegate encode=\"show\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -title &quot;%M&quot; &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000129 " <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
cristy4689cf02010-02-17 21:15:45 +0000130 " <delegate decode=\"svg\" command=\"&quot;rsvg&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000131 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\"&quot;enscript&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
Cristy5f478ca2016-05-14 18:43:20 -0400132 " <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -title &quot;%M&quot; &quot;%i&quot;\"/>"
cristy3ed852e2009-09-05 21:47:34 +0000133 " <delegate decode=\"wmf\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
134 "</delegatemap>";
135
136/*
137 Global declaractions.
138*/
139static LinkedListInfo
cristy86e5ac92014-03-16 19:27:39 +0000140 *delegate_cache = (LinkedListInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000141
142static SemaphoreInfo
143 *delegate_semaphore = (SemaphoreInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000144
145/*
146 Forward declaractions.
147*/
148static MagickBooleanType
cristy86e5ac92014-03-16 19:27:39 +0000149 IsDelegateCacheInstantiated(ExceptionInfo *),
cristycd2cd182014-03-18 12:10:55 +0000150 LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
151 ExceptionInfo *);
cristy86e5ac92014-03-16 19:27:39 +0000152
153/*
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155% %
156% %
157% %
158% A c q u i r e D e l e g a t e C a c h e %
159% %
160% %
161% %
162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163%
164% AcquireDelegateCache() caches one or more delegate configurations which
165% provides a mapping between delegate attributes and a delegate name.
166%
167% The format of the AcquireDelegateCache method is:
168%
169% LinkedListInfo *AcquireDelegateCache(const char *filename,
170% ExceptionInfo *exception)
171%
172% A description of each parameter follows:
173%
174% o filename: the font file name.
175%
176% o exception: return any errors or warnings in this structure.
177%
178*/
179static LinkedListInfo *AcquireDelegateCache(const char *filename,
180 ExceptionInfo *exception)
181{
cristy86e5ac92014-03-16 19:27:39 +0000182 LinkedListInfo
dirkc9f5cc82016-07-17 18:59:02 +0200183 *cache;
cristy86e5ac92014-03-16 19:27:39 +0000184
185 MagickStatusType
186 status;
187
dirkc9f5cc82016-07-17 18:59:02 +0200188 cache=NewLinkedList(0);
cristy86e5ac92014-03-16 19:27:39 +0000189 status=MagickTrue;
cristy191ba5c2014-03-16 21:26:40 +0000190#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
cristy86e5ac92014-03-16 19:27:39 +0000191 {
cristy66f8c5f2014-03-17 00:12:14 +0000192 const StringInfo
193 *option;
194
195 LinkedListInfo
196 *options;
197
198 options=GetConfigureOptions(filename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000199 option=(const StringInfo *) GetNextValueInLinkedList(options);
cristy66f8c5f2014-03-17 00:12:14 +0000200 while (option != (const StringInfo *) NULL)
201 {
dirkc9f5cc82016-07-17 18:59:02 +0200202 status&=LoadDelegateCache(cache,(const char *)
cristycd2cd182014-03-18 12:10:55 +0000203 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy66f8c5f2014-03-17 00:12:14 +0000204 option=(const StringInfo *) GetNextValueInLinkedList(options);
205 }
206 options=DestroyConfigureOptions(options);
cristy86e5ac92014-03-16 19:27:39 +0000207 }
cristy191ba5c2014-03-16 21:26:40 +0000208#endif
dirkc9f5cc82016-07-17 18:59:02 +0200209 if (IsLinkedListEmpty(cache) != MagickFalse)
210 status&=LoadDelegateCache(cache,DelegateMap,"built-in",0,exception);
211 return(cache);
cristy86e5ac92014-03-16 19:27:39 +0000212}
cristy3ed852e2009-09-05 21:47:34 +0000213
214/*
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216% %
217% %
218% %
cristy0d8101e2014-04-10 00:13:05 +0000219+ D e l e g a t e C o m p o n e n t G e n e s i s %
cristy3ed852e2009-09-05 21:47:34 +0000220% %
221% %
222% %
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224%
cristyf34a1452009-10-24 22:29:27 +0000225% DelegateComponentGenesis() instantiates the delegate component.
cristy3ed852e2009-09-05 21:47:34 +0000226%
cristyf34a1452009-10-24 22:29:27 +0000227% The format of the DelegateComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000228%
cristyf34a1452009-10-24 22:29:27 +0000229% MagickBooleanType DelegateComponentGenesis(void)
230%
231*/
cristy5ff4eaf2011-09-03 01:38:02 +0000232MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000233{
cristy7c977062014-04-04 14:05:53 +0000234 if (delegate_semaphore == (SemaphoreInfo *) NULL)
235 delegate_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000236 return(MagickTrue);
237}
238
239/*
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241% %
242% %
243% %
244% 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 %
245% %
246% %
247% %
248%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249%
250% DelegateComponentTerminus() destroys the delegate component.
251%
252% The format of the DelegateComponentTerminus method is:
253%
254% DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000255%
256*/
257
258static void *DestroyDelegate(void *delegate_info)
259{
260 register DelegateInfo
261 *p;
262
263 p=(DelegateInfo *) delegate_info;
264 if (p->path != (char *) NULL)
265 p->path=DestroyString(p->path);
266 if (p->decode != (char *) NULL)
267 p->decode=DestroyString(p->decode);
268 if (p->encode != (char *) NULL)
269 p->encode=DestroyString(p->encode);
270 if (p->commands != (char *) NULL)
271 p->commands=DestroyString(p->commands);
cristy191ba5c2014-03-16 21:26:40 +0000272 if (p->semaphore != (SemaphoreInfo *) NULL)
cristy2f62da42014-03-19 21:57:49 +0000273 RelinquishSemaphoreInfo(&p->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000274 p=(DelegateInfo *) RelinquishMagickMemory(p);
275 return((void *) NULL);
276}
277
cristy5ff4eaf2011-09-03 01:38:02 +0000278MagickPrivate void DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000279{
cristy18b17442009-10-25 18:36:48 +0000280 if (delegate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000281 ActivateSemaphoreInfo(&delegate_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000282 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000283 if (delegate_cache != (LinkedListInfo *) NULL)
284 delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
cristyf84a1932010-01-03 18:00:18 +0000285 UnlockSemaphoreInfo(delegate_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000286 RelinquishSemaphoreInfo(&delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000287}
288
289/*
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291% %
292% %
293% %
cristydfc19b62014-10-17 22:52:24 +0000294+ E x t e r n a l D e l e g a t e C o m m a n d %
295% %
296% %
297% %
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299%
300% ExternalDelegateCommand() executes the specified command and waits until it
301% terminates. The returned value is the exit status of the command.
302%
303% The format of the ExternalDelegateCommand method is:
304%
305% int ExternalDelegateCommand(const MagickBooleanType asynchronous,
306% const MagickBooleanType verbose,const char *command,
307% char *message,ExceptionInfo *exception)
308%
309% A description of each parameter follows:
310%
311% o asynchronous: a value other than 0 executes the parent program
312% concurrently with the new child process.
313%
314% o verbose: a value other than 0 prints the executed command before it is
315% invoked.
316%
317% o command: this string is the command to execute.
318%
319% o message: an option buffer to receive any message posted to stdout or
320% stderr.
321%
322% o exception: return any errors here.
323%
324*/
dirk457d9762014-10-20 19:58:07 +0000325MagickExport int ExternalDelegateCommand(const MagickBooleanType asynchronous,
cristydfc19b62014-10-17 22:52:24 +0000326 const MagickBooleanType verbose,const char *command,char *message,
327 ExceptionInfo *exception)
328{
329 char
330 **arguments,
331 *sanitize_command;
332
333 int
334 number_arguments,
335 status;
336
337 PolicyDomain
338 domain;
339
340 PolicyRights
341 rights;
342
343 register ssize_t
344 i;
345
346 status=(-1);
347 arguments=StringToArgv(command,&number_arguments);
348 if (arguments == (char **) NULL)
349 return(status);
350 if (*arguments[1] == '\0')
351 {
352 for (i=0; i < (ssize_t) number_arguments; i++)
353 arguments[i]=DestroyString(arguments[i]);
354 arguments=(char **) RelinquishMagickMemory(arguments);
355 return(-1);
356 }
357 rights=ExecutePolicyRights;
358 domain=DelegatePolicyDomain;
359 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
360 {
361 errno=EPERM;
362 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
363 "NotAuthorized","`%s'",arguments[1]);
364 for (i=0; i < (ssize_t) number_arguments; i++)
365 arguments[i]=DestroyString(arguments[i]);
366 arguments=(char **) RelinquishMagickMemory(arguments);
367 return(-1);
368 }
369 if (verbose != MagickFalse)
370 {
371 (void) FormatLocaleFile(stderr,"%s\n",command);
372 (void) fflush(stderr);
373 }
Cristy40639d12016-05-29 19:54:23 -0400374 sanitize_command=SanitizeString(command);
cristydfc19b62014-10-17 22:52:24 +0000375 if (asynchronous != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000376 (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
cristydfc19b62014-10-17 22:52:24 +0000377 if (message != (char *) NULL)
378 *message='\0';
379#if defined(MAGICKCORE_POSIX_SUPPORT)
380#if !defined(MAGICKCORE_HAVE_EXECVP)
381 status=system(sanitize_command);
382#else
383 if ((asynchronous != MagickFalse) ||
384 (strpbrk(sanitize_command,"&;<>|") != (char *) NULL))
385 status=system(sanitize_command);
386 else
387 {
388 pid_t
389 child_pid;
390
391 /*
392 Call application directly rather than from a shell.
393 */
394 child_pid=(pid_t) fork();
395 if (child_pid == (pid_t) -1)
396 status=system(sanitize_command);
397 else
398 if (child_pid == 0)
399 {
400 status=execvp(arguments[1],arguments+1);
401 _exit(1);
402 }
403 else
404 {
405 int
406 child_status;
407
408 pid_t
409 pid;
410
411 child_status=0;
412 pid=(pid_t) waitpid(child_pid,&child_status,0);
413 if (pid == -1)
414 status=(-1);
415 else
416 {
417 if (WIFEXITED(child_status) != 0)
418 status=WEXITSTATUS(child_status);
419 else
420 if (WIFSIGNALED(child_status))
421 status=(-1);
422 }
423 }
424 }
425#endif
426#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
dirkeb0db702015-02-12 22:17:21 +0000427 {
428 register char
429 *p;
430
431 /*
432 If a command shell is executed we need to change the forward slashes in
433 files to a backslash. We need to do this to keep Windows happy when we
434 want to 'move' a file.
435
436 TODO: This won't work if one of the delegate parameters has a forward
437 slash as aparameter.
438 */
439 p=strstr(sanitize_command, "cmd.exe /c");
440 if (p != (char*) NULL)
441 {
442 p+=10;
443 for (; *p != '\0'; p++)
444 if (*p == '/')
445 *p=*DirectorySeparator;
446 }
447 }
cristydfc19b62014-10-17 22:52:24 +0000448 status=NTSystemCommand(sanitize_command,message);
449#elif defined(macintosh)
450 status=MACSystemCommand(sanitize_command);
451#elif defined(vms)
452 status=system(sanitize_command);
453#else
454# error No suitable system() method.
455#endif
456 if (status < 0)
457 {
458 if ((message != (char *) NULL) && (*message != '\0'))
459 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
dirkeb0db702015-02-12 22:17:21 +0000460 "FailedToExecuteCommand","`%s' (%s)",sanitize_command,message);
cristydfc19b62014-10-17 22:52:24 +0000461 else
462 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
dirkeb0db702015-02-12 22:17:21 +0000463 "FailedToExecuteCommand","`%s' (%d)",sanitize_command,status);
cristydfc19b62014-10-17 22:52:24 +0000464 }
465 sanitize_command=DestroyString(sanitize_command);
466 for (i=0; i < (ssize_t) number_arguments; i++)
467 arguments[i]=DestroyString(arguments[i]);
468 arguments=(char **) RelinquishMagickMemory(arguments);
469 return(status);
470}
471
472/*
473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474% %
475% %
476% %
cristy3ed852e2009-09-05 21:47:34 +0000477% G e t D e l e g a t e C o m m a n d %
478% %
479% %
480% %
481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482%
483% GetDelegateCommand() replaces any embedded formatting characters with the
484% appropriate image attribute and returns the resulting command.
485%
486% The format of the GetDelegateCommand method is:
487%
488% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
489% const char *decode,const char *encode,ExceptionInfo *exception)
490%
491% A description of each parameter follows:
492%
493% o command: Method GetDelegateCommand returns the command associated
494% with specified delegate tag.
495%
496% o image_info: the image info.
497%
498% o image: the image.
499%
500% o decode: Specifies the decode delegate we are searching for as a
501% character string.
502%
503% o encode: Specifies the encode delegate we are searching for as a
504% character string.
505%
506% o exception: return any errors or warnings in this structure.
507%
508*/
Cristy8be0b1e2016-05-13 20:45:40 -0400509
Cristy8ef5a072016-05-14 11:49:10 -0400510static char *GetMagickPropertyLetter(ImageInfo *image_info,Image *image,
511 const char letter,ExceptionInfo *exception)
Cristy8be0b1e2016-05-13 20:45:40 -0400512{
Cristy8ef5a072016-05-14 11:49:10 -0400513#define WarnNoImageReturn(format,letter) \
514 if (image == (Image *) NULL) \
515 { \
516 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
517 "NoImageForProperty",format,letter); \
518 break; \
519 }
520#define WarnNoImageInfoReturn(format,letter) \
521 if (image_info == (ImageInfo *) NULL) \
522 { \
523 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
524 "NoImageInfoForProperty",format,letter); \
525 break; \
526 }
Cristy8be0b1e2016-05-13 20:45:40 -0400527
528 char
Cristy8ef5a072016-05-14 11:49:10 -0400529 value[MagickPathExtent];
Cristy8be0b1e2016-05-13 20:45:40 -0400530
531 const char
Cristy8ef5a072016-05-14 11:49:10 -0400532 *string;
533
Cristy8be0b1e2016-05-13 20:45:40 -0400534 if ((image != (Image *) NULL) && (image->debug != MagickFalse))
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 else
537 if ((image_info != (ImageInfo *) NULL) &&
538 (image_info->debug != MagickFalse))
Cristy8ef5a072016-05-14 11:49:10 -0400539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-images");
Cristy8be0b1e2016-05-13 20:45:40 -0400540 /*
Cristy8ef5a072016-05-14 11:49:10 -0400541 Get properties that are directly defined by images.
Cristy8be0b1e2016-05-13 20:45:40 -0400542 */
Cristy8ef5a072016-05-14 11:49:10 -0400543 *value='\0'; /* formatted string */
544 string=(const char *) value;
Cristy8be0b1e2016-05-13 20:45:40 -0400545 switch (letter)
546 {
Cristy8ef5a072016-05-14 11:49:10 -0400547 case 'a': /* authentication passphase */
548 {
549 WarnNoImageInfoReturn("\"%%%c\"",letter);
550 string=GetImageOption(image_info,"authenticate");
551 break;
552 }
Cristy8be0b1e2016-05-13 20:45:40 -0400553 case 'b': /* image size read in - in bytes */
554 {
555 WarnNoImageReturn("\"%%%c\"",letter);
556 (void) FormatMagickSize(image->extent,MagickFalse,"B",MagickPathExtent,
557 value);
558 if (image->extent == 0)
559 (void) FormatMagickSize(GetBlobSize(image),MagickFalse,"B",
560 MagickPathExtent,value);
561 break;
562 }
Cristy8be0b1e2016-05-13 20:45:40 -0400563 case 'd': /* Directory component of filename */
564 {
565 WarnNoImageReturn("\"%%%c\"",letter);
566 GetPathComponent(image->magick_filename,HeadPath,value);
Cristy8be0b1e2016-05-13 20:45:40 -0400567 break;
568 }
569 case 'e': /* Filename extension (suffix) of image file */
570 {
571 WarnNoImageReturn("\"%%%c\"",letter);
572 GetPathComponent(image->magick_filename,ExtensionPath,value);
Cristy8be0b1e2016-05-13 20:45:40 -0400573 break;
574 }
575 case 'f': /* Filename without directory component */
576 {
577 WarnNoImageReturn("\"%%%c\"",letter);
578 GetPathComponent(image->magick_filename,TailPath,value);
Cristy8be0b1e2016-05-13 20:45:40 -0400579 break;
580 }
581 case 'g': /* Image geometry, canvas and offset %Wx%H+%X+%Y */
582 {
583 WarnNoImageReturn("\"%%%c\"",letter);
584 (void) FormatLocaleString(value,MagickPathExtent,
585 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
586 image->page.height,(double) image->page.x,(double) image->page.y);
587 break;
588 }
589 case 'h': /* Image height (current) */
590 {
591 WarnNoImageReturn("\"%%%c\"",letter);
592 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
593 (image->rows != 0 ? image->rows : image->magick_rows));
594 break;
595 }
596 case 'i': /* Filename last used for an image (read or write) */
597 {
598 WarnNoImageReturn("\"%%%c\"",letter);
599 string=image->filename;
600 break;
601 }
Cristy8be0b1e2016-05-13 20:45:40 -0400602 case 'm': /* Image format (file magick) */
603 {
604 WarnNoImageReturn("\"%%%c\"",letter);
605 string=image->magick;
606 break;
607 }
608 case 'n': /* Number of images in the list. */
609 {
Cristy8ef5a072016-05-14 11:49:10 -0400610 if (image != (Image *) NULL)
Cristy8be0b1e2016-05-13 20:45:40 -0400611 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
612 GetImageListLength(image));
Cristy8be0b1e2016-05-13 20:45:40 -0400613 break;
614 }
Cristy8ef5a072016-05-14 11:49:10 -0400615 case 'o': /* Output Filename */
616 {
Cristy8be0b1e2016-05-13 20:45:40 -0400617 WarnNoImageInfoReturn("\"%%%c\"",letter);
618 string=image_info->filename;
619 break;
Cristy8ef5a072016-05-14 11:49:10 -0400620 }
Cristy8be0b1e2016-05-13 20:45:40 -0400621 case 'p': /* Image index in current image list */
622 {
623 WarnNoImageReturn("\"%%%c\"",letter);
624 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
625 GetImageIndexInList(image));
626 break;
627 }
628 case 'q': /* Quantum depth of image in memory */
629 {
630 WarnNoImageReturn("\"%%%c\"",letter);
631 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
632 MAGICKCORE_QUANTUM_DEPTH);
633 break;
634 }
635 case 'r': /* Image storage class, colorspace, and alpha enabled. */
636 {
637 ColorspaceType
638 colorspace;
639
640 WarnNoImageReturn("\"%%%c\"",letter);
641 colorspace=image->colorspace;
642 if (SetImageGray(image,exception) != MagickFalse)
Cristybeb4c2b2017-12-26 19:43:17 -0500643 colorspace=GRAYColorspace; /* FUTURE: this is IMv6 not IMv7 */
Cristy8be0b1e2016-05-13 20:45:40 -0400644 (void) FormatLocaleString(value,MagickPathExtent,"%s %s %s",
645 CommandOptionToMnemonic(MagickClassOptions,(ssize_t)
646 image->storage_class),CommandOptionToMnemonic(MagickColorspaceOptions,
647 (ssize_t) colorspace),image->alpha_trait != UndefinedPixelTrait ?
648 "Alpha" : "");
649 break;
650 }
651 case 's': /* Image scene number */
652 {
Cristy8be0b1e2016-05-13 20:45:40 -0400653 WarnNoImageReturn("\"%%%c\"",letter);
654 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
Cristy8ef5a072016-05-14 11:49:10 -0400655 image->scene);
Cristy8be0b1e2016-05-13 20:45:40 -0400656 break;
657 }
658 case 't': /* Base filename without directory or extention */
659 {
660 WarnNoImageReturn("\"%%%c\"",letter);
661 GetPathComponent(image->magick_filename,BasePath,value);
Cristy8be0b1e2016-05-13 20:45:40 -0400662 break;
663 }
664 case 'u': /* Unique filename */
Cristy8ef5a072016-05-14 11:49:10 -0400665 {
Cristy8be0b1e2016-05-13 20:45:40 -0400666 WarnNoImageInfoReturn("\"%%%c\"",letter);
667 string=image_info->unique;
668 break;
Cristy8ef5a072016-05-14 11:49:10 -0400669 }
Cristy8be0b1e2016-05-13 20:45:40 -0400670 case 'w': /* Image width (current) */
671 {
672 WarnNoImageReturn("\"%%%c\"",letter);
673 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
674 (image->columns != 0 ? image->columns : image->magick_columns));
675 break;
676 }
677 case 'x': /* Image horizontal resolution (with units) */
678 {
679 WarnNoImageReturn("\"%%%c\"",letter);
680 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
681 fabs(image->resolution.x) > MagickEpsilon ? image->resolution.x : 72.0);
682 break;
683 }
684 case 'y': /* Image vertical resolution (with units) */
685 {
686 WarnNoImageReturn("\"%%%c\"",letter);
687 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
688 fabs(image->resolution.y) > MagickEpsilon ? image->resolution.y : 72.0);
689 break;
690 }
691 case 'z': /* Image depth as read in */
692 {
693 WarnNoImageReturn("\"%%%c\"",letter);
694 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
695 (double) image->depth);
696 break;
697 }
698 case 'A': /* Image alpha channel */
699 {
700 WarnNoImageReturn("\"%%%c\"",letter);
701 string=CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
702 image->alpha_trait);
703 break;
704 }
705 case 'C': /* Image compression method. */
706 {
707 WarnNoImageReturn("\"%%%c\"",letter);
708 string=CommandOptionToMnemonic(MagickCompressOptions,
709 (ssize_t) image->compression);
710 break;
711 }
712 case 'D': /* Image dispose method. */
713 {
714 WarnNoImageReturn("\"%%%c\"",letter);
715 string=CommandOptionToMnemonic(MagickDisposeOptions,
716 (ssize_t) image->dispose);
717 break;
718 }
719 case 'F':
720 {
Cristy8be0b1e2016-05-13 20:45:40 -0400721 /*
Cristy8ef5a072016-05-14 11:49:10 -0400722 Magick filename - filename given incl. coder & read mods.
Cristy8be0b1e2016-05-13 20:45:40 -0400723 */
724 WarnNoImageReturn("\"%%%c\"",letter);
725 (void) CopyMagickString(value,image->magick_filename,MagickPathExtent);
Cristy8be0b1e2016-05-13 20:45:40 -0400726 break;
727 }
728 case 'G': /* Image size as geometry = "%wx%h" */
729 {
730 WarnNoImageReturn("\"%%%c\"",letter);
731 (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
Cristy8ef5a072016-05-14 11:49:10 -0400732 (double) image->magick_columns,(double) image->magick_rows);
Cristy8be0b1e2016-05-13 20:45:40 -0400733 break;
734 }
735 case 'H': /* layer canvas height */
736 {
737 WarnNoImageReturn("\"%%%c\"",letter);
738 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
739 (double) image->page.height);
740 break;
741 }
742 case 'M': /* Magick filename - filename given incl. coder & read mods */
743 {
744 WarnNoImageReturn("\"%%%c\"",letter);
745 string=image->magick_filename;
746 break;
747 }
748 case 'O': /* layer canvas offset with sign = "+%X+%Y" */
749 {
750 WarnNoImageReturn("\"%%%c\"",letter);
751 (void) FormatLocaleString(value,MagickPathExtent,"%+ld%+ld",(long)
752 image->page.x,(long) image->page.y);
753 break;
754 }
755 case 'P': /* layer canvas page size = "%Wx%H" */
756 {
757 WarnNoImageReturn("\"%%%c\"",letter);
758 (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
759 (double) image->page.width,(double) image->page.height);
760 break;
761 }
762 case 'Q': /* image compression quality */
763 {
764 WarnNoImageReturn("\"%%%c\"",letter);
765 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
766 (image->quality == 0 ? 92 : image->quality));
767 break;
768 }
769 case 'S': /* Number of scenes in image list. */
770 {
771 WarnNoImageInfoReturn("\"%%%c\"",letter);
Cristy8be0b1e2016-05-13 20:45:40 -0400772 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
773 (image_info->number_scenes == 0 ? 2147483647 :
774 image_info->number_scenes));
Cristy8be0b1e2016-05-13 20:45:40 -0400775 break;
776 }
777 case 'T': /* image time delay for animations */
778 {
779 WarnNoImageReturn("\"%%%c\"",letter);
780 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
781 image->delay);
782 break;
783 }
784 case 'U': /* Image resolution units. */
785 {
786 WarnNoImageReturn("\"%%%c\"",letter);
787 string=CommandOptionToMnemonic(MagickResolutionOptions,
788 (ssize_t) image->units);
789 break;
790 }
791 case 'W': /* layer canvas width */
792 {
793 WarnNoImageReturn("\"%%%c\"",letter);
794 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
795 image->page.width);
796 break;
797 }
798 case 'X': /* layer canvas X offset */
799 {
800 WarnNoImageReturn("\"%%%c\"",letter);
801 (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
802 image->page.x);
803 break;
804 }
805 case 'Y': /* layer canvas Y offset */
806 {
807 WarnNoImageReturn("\"%%%c\"",letter);
808 (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
809 image->page.y);
810 break;
811 }
812 case '%': /* percent escaped */
Cristy8ef5a072016-05-14 11:49:10 -0400813 {
Cristy8be0b1e2016-05-13 20:45:40 -0400814 string="%";
815 break;
Cristy8ef5a072016-05-14 11:49:10 -0400816 }
817 case '@': /* Trim bounding box, without actually trimming! */
Cristy8be0b1e2016-05-13 20:45:40 -0400818 {
819 RectangleInfo
820 page;
821
822 WarnNoImageReturn("\"%%%c\"",letter);
823 page=GetImageBoundingBox(image,exception);
824 (void) FormatLocaleString(value,MagickPathExtent,
825 "%.20gx%.20g%+.20g%+.20g",(double) page.width,(double) page.height,
Cristy8ef5a072016-05-14 11:49:10 -0400826 (double) page.x,(double) page.y);
Cristy8be0b1e2016-05-13 20:45:40 -0400827 break;
828 }
829 case '#':
830 {
831 /*
832 Image signature.
833 */
834 WarnNoImageReturn("\"%%%c\"",letter);
835 (void) SignatureImage(image,exception);
836 string=GetImageProperty(image,"signature",exception);
837 break;
838 }
839 }
Cristy40639d12016-05-29 19:54:23 -0400840 return(SanitizeString(string));
Cristy8be0b1e2016-05-13 20:45:40 -0400841}
842
843static char *InterpretDelegateProperties(ImageInfo *image_info,
844 Image *image,const char *embed_text,ExceptionInfo *exception)
845{
846#define ExtendInterpretText(string_length) \
847DisableMSCWarning(4127) \
848{ \
849 size_t length=(string_length); \
850 if ((size_t) (q-interpret_text+length+1) >= extent) \
851 { \
852 extent+=length; \
853 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
854 MaxTextExtent,sizeof(*interpret_text)); \
855 if (interpret_text == (char *) NULL) \
856 return((char *) NULL); \
857 q=interpret_text+strlen(interpret_text); \
858 } \
859} \
860RestoreMSCWarning
861
862#define AppendKeyValue2Text(key,value)\
863DisableMSCWarning(4127) \
864{ \
865 size_t length=strlen(key)+strlen(value)+2; \
866 if ((size_t) (q-interpret_text+length+1) >= extent) \
867 { \
868 extent+=length; \
869 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
870 MaxTextExtent,sizeof(*interpret_text)); \
871 if (interpret_text == (char *) NULL) \
872 return((char *) NULL); \
873 q=interpret_text+strlen(interpret_text); \
874 } \
875 q+=FormatLocaleString(q,extent,"%s=%s\n",(key),(value)); \
876} \
877RestoreMSCWarning
878
879#define AppendString2Text(string) \
880DisableMSCWarning(4127) \
881{ \
882 size_t length=strlen((string)); \
883 if ((size_t) (q-interpret_text+length+1) >= extent) \
884 { \
885 extent+=length; \
886 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
887 MaxTextExtent,sizeof(*interpret_text)); \
888 if (interpret_text == (char *) NULL) \
889 return((char *) NULL); \
890 q=interpret_text+strlen(interpret_text); \
891 } \
892 (void) CopyMagickString(q,(string),extent); \
893 q+=length; \
894} \
895RestoreMSCWarning
896
897 char
Cristy8ef5a072016-05-14 11:49:10 -0400898 *interpret_text,
899 *string;
Cristy8be0b1e2016-05-13 20:45:40 -0400900
901 register char
902 *q; /* current position in interpret_text */
903
904 register const char
905 *p; /* position in embed_text string being expanded */
906
907 size_t
908 extent; /* allocated length of interpret_text */
909
910 MagickBooleanType
911 number;
912
913 assert(image == NULL || image->signature == MagickCoreSignature);
914 assert(image_info == NULL || image_info->signature == MagickCoreSignature);
915 if ((image != (Image *) NULL) && (image->debug != MagickFalse))
916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
917 else
918 if ((image_info != (ImageInfo *) NULL) && (image_info->debug != MagickFalse))
919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-image");
920 if (embed_text == (const char *) NULL)
921 return(ConstantString(""));
922 p=embed_text;
923 while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
924 p++;
925 if (*p == '\0')
926 return(ConstantString(""));
Cristy8be0b1e2016-05-13 20:45:40 -0400927 /*
928 Translate any embedded format characters.
929 */
Cristy8ef5a072016-05-14 11:49:10 -0400930 interpret_text=AcquireString(embed_text); /* new string with extra space */
931 extent=MagickPathExtent; /* allocated space in string */
932 number=MagickFalse; /* is last char a number? */
Cristy8be0b1e2016-05-13 20:45:40 -0400933 for (q=interpret_text; *p!='\0';
Roland Illig50c202d2018-02-18 17:52:42 +0100934 number=isdigit((int) ((unsigned char) *p)) ? MagickTrue : MagickFalse,p++)
Cristy8be0b1e2016-05-13 20:45:40 -0400935 {
936 /*
Cristy8ef5a072016-05-14 11:49:10 -0400937 Interpret escape characters (e.g. Filename: %M).
Cristy8be0b1e2016-05-13 20:45:40 -0400938 */
939 *q='\0';
940 ExtendInterpretText(MagickPathExtent);
941 switch (*p)
942 {
943 case '\\':
944 {
945 switch (*(p+1))
946 {
947 case '\0':
948 continue;
949 case 'r': /* convert to RETURN */
950 {
951 *q++='\r';
952 p++;
953 continue;
954 }
955 case 'n': /* convert to NEWLINE */
956 {
957 *q++='\n';
958 p++;
959 continue;
960 }
961 case '\n': /* EOL removal UNIX,MacOSX */
962 {
963 p++;
964 continue;
965 }
966 case '\r': /* EOL removal DOS,Windows */
967 {
968 p++;
969 if (*p == '\n') /* return-newline EOL */
970 p++;
971 continue;
972 }
973 default:
974 {
975 p++;
976 *q++=(*p);
977 }
978 }
979 continue;
980 }
981 case '&':
982 {
983 if (LocaleNCompare("&lt;",p,4) == 0)
984 {
985 *q++='<';
986 p+=3;
987 }
988 else
989 if (LocaleNCompare("&gt;",p,4) == 0)
990 {
991 *q++='>';
992 p+=3;
993 }
994 else
995 if (LocaleNCompare("&amp;",p,5) == 0)
996 {
997 *q++='&';
998 p+=4;
999 }
1000 else
1001 *q++=(*p);
1002 continue;
1003 }
1004 case '%':
1005 break; /* continue to next set of handlers */
1006 default:
1007 {
1008 *q++=(*p); /* any thing else is 'as normal' */
1009 continue;
1010 }
1011 }
1012 p++; /* advance beyond the percent */
1013 /*
1014 Doubled Percent - or percent at end of string.
1015 */
1016 if ((*p == '\0') || (*p == '\'') || (*p == '"'))
1017 p--;
1018 if (*p == '%')
1019 {
1020 *q++='%';
1021 continue;
1022 }
1023 /*
1024 Single letter escapes %c.
1025 */
Cristy8ef5a072016-05-14 11:49:10 -04001026 if (number != MagickFalse)
Cristy8be0b1e2016-05-13 20:45:40 -04001027 {
Cristy8ef5a072016-05-14 11:49:10 -04001028 /*
1029 But only if not preceeded by a number!
1030 */
1031 *q++='%'; /* do NOT substitute the percent */
1032 p--; /* back up one */
Cristy8be0b1e2016-05-13 20:45:40 -04001033 continue;
1034 }
Cristy8ef5a072016-05-14 11:49:10 -04001035 string=GetMagickPropertyLetter(image_info,image,*p, exception);
1036 if (string != (char *) NULL)
Cristy8be0b1e2016-05-13 20:45:40 -04001037 {
Cristy8ef5a072016-05-14 11:49:10 -04001038 AppendString2Text(string);
1039 string=DestroyString(string);
1040 continue;
Cristy8be0b1e2016-05-13 20:45:40 -04001041 }
Cristy8ef5a072016-05-14 11:49:10 -04001042 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1043 "UnknownImageProperty","\"%%%c\"",*p);
Cristy8be0b1e2016-05-13 20:45:40 -04001044 }
1045 *q='\0';
1046 return(interpret_text);
1047}
1048
cristy3ed852e2009-09-05 21:47:34 +00001049MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
1050 const char *decode,const char *encode,ExceptionInfo *exception)
1051{
1052 char
1053 *command,
1054 **commands;
1055
1056 const DelegateInfo
1057 *delegate_info;
1058
cristybb503372010-05-27 20:51:26 +00001059 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001060 i;
1061
1062 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001063 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001064 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001065 assert(image->signature == MagickCoreSignature);
cristycb190b72014-08-31 20:04:42 +00001066 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
anthony59c44432012-05-23 04:41:25 +00001068
cristy3ed852e2009-09-05 21:47:34 +00001069 delegate_info=GetDelegateInfo(decode,encode,exception);
1070 if (delegate_info == (const DelegateInfo *) NULL)
1071 {
1072 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +00001073 "NoTagFound","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001074 return((char *) NULL);
1075 }
1076 commands=StringToList(delegate_info->commands);
1077 if (commands == (char **) NULL)
1078 {
1079 (void) ThrowMagickException(exception,GetMagickModule(),
cristy014ead12013-04-29 08:47:36 +00001080 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
1081 encode);
cristy3ed852e2009-09-05 21:47:34 +00001082 return((char *) NULL);
1083 }
Cristy8be0b1e2016-05-13 20:45:40 -04001084 command=InterpretDelegateProperties((ImageInfo *) image_info,image,
1085 commands[0],exception);
cristy3ed852e2009-09-05 21:47:34 +00001086 if (command == (char *) NULL)
1087 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
cristyefe601c2013-01-05 17:51:12 +00001088 "MemoryAllocationFailed","`%s'",commands[0]);
cristy3ed852e2009-09-05 21:47:34 +00001089 /*
1090 Relinquish resources.
1091 */
1092 for (i=0; commands[i] != (char *) NULL; i++)
1093 commands[i]=DestroyString(commands[i]);
1094 commands=(char **) RelinquishMagickMemory(commands);
1095 return(command);
1096}
1097
1098/*
1099%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100% %
1101% %
1102% %
1103% G e t D e l e g a t e C o m m a n d s %
1104% %
1105% %
1106% %
1107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108%
1109% GetDelegateCommands() returns the commands associated with a delegate.
1110%
1111% The format of the GetDelegateCommands method is:
1112%
1113% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1114%
1115% A description of each parameter follows:
1116%
1117% o delegate_info: The delegate info.
1118%
1119*/
1120MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1121{
1122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +00001123
cristy3ed852e2009-09-05 21:47:34 +00001124 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001125 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001126 return(delegate_info->commands);
1127}
1128
1129/*
1130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1131% %
1132% %
1133% %
1134% G e t D e l e g a t e I n f o %
1135% %
1136% %
1137% %
1138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1139%
1140% GetDelegateInfo() returns any delegates associated with the specified tag.
1141%
1142% The format of the GetDelegateInfo method is:
1143%
1144% const DelegateInfo *GetDelegateInfo(const char *decode,
1145% const char *encode,ExceptionInfo *exception)
1146%
1147% A description of each parameter follows:
1148%
1149% o decode: Specifies the decode delegate we are searching for as a
1150% character string.
1151%
1152% o encode: Specifies the encode delegate we are searching for as a
1153% character string.
1154%
1155% o exception: return any errors or warnings in this structure.
1156%
1157*/
1158MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
1159 const char *encode,ExceptionInfo *exception)
1160{
1161 register const DelegateInfo
1162 *p;
1163
1164 assert(exception != (ExceptionInfo *) NULL);
dirk5d0a4412016-01-05 22:28:51 +01001165 if (IsDelegateCacheInstantiated(exception) == MagickFalse)
cristycd2cd182014-03-18 12:10:55 +00001166 return((const DelegateInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001167 /*
1168 Search for named delegate.
1169 */
cristyf84a1932010-01-03 18:00:18 +00001170 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +00001171 ResetLinkedListIterator(delegate_cache);
1172 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy49d4d222014-03-16 00:37:58 +00001173 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
1174 {
1175 UnlockSemaphoreInfo(delegate_semaphore);
1176 return(p);
1177 }
cristy3ed852e2009-09-05 21:47:34 +00001178 while (p != (const DelegateInfo *) NULL)
1179 {
1180 if (p->mode > 0)
1181 {
1182 if (LocaleCompare(p->decode,decode) == 0)
1183 break;
cristy86e5ac92014-03-16 19:27:39 +00001184 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001185 continue;
1186 }
1187 if (p->mode < 0)
1188 {
1189 if (LocaleCompare(p->encode,encode) == 0)
1190 break;
cristy86e5ac92014-03-16 19:27:39 +00001191 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001192 continue;
1193 }
1194 if (LocaleCompare(decode,p->decode) == 0)
1195 if (LocaleCompare(encode,p->encode) == 0)
1196 break;
1197 if (LocaleCompare(decode,"*") == 0)
1198 if (LocaleCompare(encode,p->encode) == 0)
1199 break;
1200 if (LocaleCompare(decode,p->decode) == 0)
1201 if (LocaleCompare(encode,"*") == 0)
1202 break;
cristy86e5ac92014-03-16 19:27:39 +00001203 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001204 }
1205 if (p != (const DelegateInfo *) NULL)
cristy86e5ac92014-03-16 19:27:39 +00001206 (void) InsertValueInLinkedList(delegate_cache,0,
1207 RemoveElementByValueFromLinkedList(delegate_cache,p));
cristyf84a1932010-01-03 18:00:18 +00001208 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001209 return(p);
1210}
1211
1212/*
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214% %
1215% %
1216% %
1217% G e t D e l e g a t e I n f o L i s t %
1218% %
1219% %
1220% %
1221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222%
1223% GetDelegateInfoList() returns any delegates that match the specified pattern.
1224%
1225% The delegate of the GetDelegateInfoList function is:
1226%
1227% const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +00001228% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001229%
1230% A description of each parameter follows:
1231%
1232% o pattern: Specifies a pointer to a text string containing a pattern.
1233%
1234% o number_delegates: This integer returns the number of delegates in the
1235% list.
1236%
1237% o exception: return any errors or warnings in this structure.
1238%
1239*/
1240
1241#if defined(__cplusplus) || defined(c_plusplus)
1242extern "C" {
1243#endif
1244
1245static int DelegateInfoCompare(const void *x,const void *y)
1246{
1247 const DelegateInfo
1248 **p,
1249 **q;
1250
dirkf0dedd82014-12-16 19:56:13 +00001251 int
1252 cmp;
1253
cristy3ed852e2009-09-05 21:47:34 +00001254 p=(const DelegateInfo **) x,
1255 q=(const DelegateInfo **) y;
dirkf0dedd82014-12-16 19:56:13 +00001256 cmp=LocaleCompare((*p)->path,(*q)->path);
1257 if (cmp == 0)
cristy3ed852e2009-09-05 21:47:34 +00001258 {
1259 if ((*p)->decode == (char *) NULL)
1260 if (((*p)->encode != (char *) NULL) &&
1261 ((*q)->encode != (char *) NULL))
1262 return(strcmp((*p)->encode,(*q)->encode));
1263 if (((*p)->decode != (char *) NULL) &&
1264 ((*q)->decode != (char *) NULL))
1265 return(strcmp((*p)->decode,(*q)->decode));
1266 }
dirkf0dedd82014-12-16 19:56:13 +00001267 return(cmp);
cristy3ed852e2009-09-05 21:47:34 +00001268}
1269
1270#if defined(__cplusplus) || defined(c_plusplus)
1271}
1272#endif
1273
1274MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +00001275 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001276{
1277 const DelegateInfo
1278 **delegates;
1279
1280 register const DelegateInfo
1281 *p;
1282
cristybb503372010-05-27 20:51:26 +00001283 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001284 i;
1285
1286 /*
1287 Allocate delegate list.
1288 */
anthony59c44432012-05-23 04:41:25 +00001289 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001290 assert(pattern != (char *) NULL);
1291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
anthony59c44432012-05-23 04:41:25 +00001292
cristy3ed852e2009-09-05 21:47:34 +00001293 *number_delegates=0;
1294 p=GetDelegateInfo("*","*",exception);
1295 if (p == (const DelegateInfo *) NULL)
1296 return((const DelegateInfo **) NULL);
1297 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +00001298 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
cristy3ed852e2009-09-05 21:47:34 +00001299 if (delegates == (const DelegateInfo **) NULL)
1300 return((const DelegateInfo **) NULL);
1301 /*
1302 Generate delegate list.
1303 */
cristyf84a1932010-01-03 18:00:18 +00001304 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +00001305 ResetLinkedListIterator(delegate_cache);
1306 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001307 for (i=0; p != (const DelegateInfo *) NULL; )
1308 {
dirk5d0a4412016-01-05 22:28:51 +01001309 if( (p->stealth == MagickFalse) &&
dirk11a7cb72016-01-05 14:00:04 +01001310 ( GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse ||
1311 GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse) )
cristy3ed852e2009-09-05 21:47:34 +00001312 delegates[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +00001313 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001314 }
cristyf84a1932010-01-03 18:00:18 +00001315 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001316 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
1317 delegates[i]=(DelegateInfo *) NULL;
cristybb503372010-05-27 20:51:26 +00001318 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00001319 return(delegates);
1320}
1321
1322/*
1323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1324% %
1325% %
1326% %
1327% G e t D e l e g a t e L i s t %
1328% %
1329% %
1330% %
1331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1332%
1333% GetDelegateList() returns any image format delegates that match the
1334% specified pattern.
1335%
1336% The format of the GetDelegateList function is:
1337%
1338% char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +00001339% size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001340%
1341% A description of each parameter follows:
1342%
1343% o pattern: Specifies a pointer to a text string containing a pattern.
1344%
1345% o number_delegates: This integer returns the number of delegates
1346% in the list.
1347%
1348% o exception: return any errors or warnings in this structure.
1349%
1350*/
1351
1352#if defined(__cplusplus) || defined(c_plusplus)
1353extern "C" {
1354#endif
1355
1356static int DelegateCompare(const void *x,const void *y)
1357{
1358 register const char
1359 **p,
1360 **q;
1361
1362 p=(const char **) x;
1363 q=(const char **) y;
1364 return(LocaleCompare(*p,*q));
1365}
1366
1367#if defined(__cplusplus) || defined(c_plusplus)
1368}
1369#endif
1370
1371MagickExport char **GetDelegateList(const char *pattern,
cristybb503372010-05-27 20:51:26 +00001372 size_t *number_delegates,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001373{
1374 char
1375 **delegates;
1376
1377 register const DelegateInfo
1378 *p;
1379
cristybb503372010-05-27 20:51:26 +00001380 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001381 i;
1382
1383 /*
1384 Allocate delegate list.
1385 */
1386 assert(pattern != (char *) NULL);
1387 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
anthony59c44432012-05-23 04:41:25 +00001388
cristybb503372010-05-27 20:51:26 +00001389 assert(number_delegates != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001390 *number_delegates=0;
1391 p=GetDelegateInfo("*","*",exception);
1392 if (p == (const DelegateInfo *) NULL)
1393 return((char **) NULL);
1394 delegates=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +00001395 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
cristy3ed852e2009-09-05 21:47:34 +00001396 if (delegates == (char **) NULL)
1397 return((char **) NULL);
cristyf84a1932010-01-03 18:00:18 +00001398 LockSemaphoreInfo(delegate_semaphore);
cristy86e5ac92014-03-16 19:27:39 +00001399 ResetLinkedListIterator(delegate_cache);
1400 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001401 for (i=0; p != (const DelegateInfo *) NULL; )
1402 {
dirk5d0a4412016-01-05 22:28:51 +01001403 if( (p->stealth == MagickFalse) &&
dirk11a7cb72016-01-05 14:00:04 +01001404 GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse )
cristy3ed852e2009-09-05 21:47:34 +00001405 delegates[i++]=ConstantString(p->decode);
dirk5d0a4412016-01-05 22:28:51 +01001406 if( (p->stealth == MagickFalse) &&
dirk11a7cb72016-01-05 14:00:04 +01001407 GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse )
cristy3ed852e2009-09-05 21:47:34 +00001408 delegates[i++]=ConstantString(p->encode);
cristy86e5ac92014-03-16 19:27:39 +00001409 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
cristy3ed852e2009-09-05 21:47:34 +00001410 }
cristyf84a1932010-01-03 18:00:18 +00001411 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001412 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
1413 delegates[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +00001414 *number_delegates=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00001415 return(delegates);
1416}
1417
1418/*
1419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420% %
1421% %
1422% %
1423% G e t D e l e g a t e M o d e %
1424% %
1425% %
1426% %
1427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428%
1429% GetDelegateMode() returns the mode of the delegate.
1430%
1431% The format of the GetDelegateMode method is:
1432%
cristybb503372010-05-27 20:51:26 +00001433% ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +00001434%
1435% A description of each parameter follows:
1436%
1437% o delegate_info: The delegate info.
1438%
1439*/
cristybb503372010-05-27 20:51:26 +00001440MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
cristy3ed852e2009-09-05 21:47:34 +00001441{
1442 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +00001443
cristy3ed852e2009-09-05 21:47:34 +00001444 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001445 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001446 return(delegate_info->mode);
1447}
1448
1449/*
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451% %
1452% %
1453% %
1454+ G e t D e l e g a t e T h r e a d S u p p o r t %
1455% %
1456% %
1457% %
1458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459%
1460% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
1461% threads.
1462%
1463% The format of the GetDelegateThreadSupport method is:
1464%
1465% MagickBooleanType GetDelegateThreadSupport(
1466% const DelegateInfo *delegate_info)
1467%
1468% A description of each parameter follows:
1469%
1470% o delegate_info: The delegate info.
1471%
1472*/
1473MagickExport MagickBooleanType GetDelegateThreadSupport(
1474 const DelegateInfo *delegate_info)
1475{
1476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
anthony59c44432012-05-23 04:41:25 +00001477
cristy3ed852e2009-09-05 21:47:34 +00001478 assert(delegate_info != (DelegateInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001479 assert(delegate_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001480 return(delegate_info->thread_support);
1481}
1482
1483/*
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485% %
1486% %
1487% %
cristy86e5ac92014-03-16 19:27:39 +00001488+ I s D e l e g a t e C a c h e I n s t a n t i a t e d %
cristy3ed852e2009-09-05 21:47:34 +00001489% %
1490% %
1491% %
1492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493%
cristy10e530d2014-03-19 13:39:13 +00001494% IsDelegateCacheInstantiated() determines if the delegate cache is
1495% instantiated. If not, it instantiates the cache and returns it.
cristy3ed852e2009-09-05 21:47:34 +00001496%
cristy904e5912014-03-15 19:53:14 +00001497% The format of the IsDelegateInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +00001498%
cristy86e5ac92014-03-16 19:27:39 +00001499% MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001500%
1501% A description of each parameter follows.
1502%
1503% o exception: return any errors or warnings in this structure.
1504%
1505*/
cristy86e5ac92014-03-16 19:27:39 +00001506static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001507{
cristy86e5ac92014-03-16 19:27:39 +00001508 if (delegate_cache == (LinkedListInfo *) NULL)
1509 {
1510 if (delegate_semaphore == (SemaphoreInfo *) NULL)
1511 ActivateSemaphoreInfo(&delegate_semaphore);
1512 LockSemaphoreInfo(delegate_semaphore);
1513 if (delegate_cache == (LinkedListInfo *) NULL)
1514 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
1515 UnlockSemaphoreInfo(delegate_semaphore);
1516 }
dirk972c2e42015-07-25 19:23:33 +00001517 return(delegate_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001518}
1519
1520/*
1521%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1522% %
1523% %
1524% %
1525% I n v o k e D e l e g a t e %
1526% %
1527% %
1528% %
1529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1530%
1531% InvokeDelegate replaces any embedded formatting characters with the
1532% appropriate image attribute and executes the resulting command. MagickFalse
1533% is returned if the commands execute with success otherwise MagickTrue.
1534%
1535% The format of the InvokeDelegate method is:
1536%
1537% MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
1538% const char *decode,const char *encode,ExceptionInfo *exception)
1539%
1540% A description of each parameter follows:
1541%
1542% o image_info: the imageInfo.
1543%
1544% o image: the image.
1545%
1546% o exception: return any errors or warnings in this structure.
1547%
1548*/
1549
cristy3ed852e2009-09-05 21:47:34 +00001550static MagickBooleanType CopyDelegateFile(const char *source,
cristy4886c4f2015-06-06 15:26:09 +00001551 const char *destination,const MagickBooleanType overwrite)
cristy3ed852e2009-09-05 21:47:34 +00001552{
1553 int
1554 destination_file,
1555 source_file;
1556
cristy53ac66f2012-03-11 02:56:37 +00001557 MagickBooleanType
1558 status;
1559
cristy3ed852e2009-09-05 21:47:34 +00001560 register size_t
1561 i;
1562
1563 size_t
1564 length,
1565 quantum;
1566
1567 ssize_t
1568 count;
1569
1570 struct stat
1571 attributes;
1572
1573 unsigned char
1574 *buffer;
1575
1576 /*
cristy2dfb1df2012-02-25 14:16:09 +00001577 Copy source file to destination.
cristy3ed852e2009-09-05 21:47:34 +00001578 */
1579 assert(source != (const char *) NULL);
1580 assert(destination != (char *) NULL);
cristy4886c4f2015-06-06 15:26:09 +00001581 if (overwrite == MagickFalse)
1582 {
1583 status=GetPathAttributes(destination,&attributes);
1584 if (status != MagickFalse)
1585 return(MagickTrue);
1586 }
cristy18c6c272011-09-23 14:40:37 +00001587 destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
cristy3ed852e2009-09-05 21:47:34 +00001588 if (destination_file == -1)
1589 return(MagickFalse);
cristy18c6c272011-09-23 14:40:37 +00001590 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
cristy3ed852e2009-09-05 21:47:34 +00001591 if (source_file == -1)
1592 {
1593 (void) close(destination_file);
1594 return(MagickFalse);
1595 }
1596 quantum=(size_t) MagickMaxBufferExtent;
cristyf201ba62015-07-05 13:54:28 +00001597 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
cristy3ed852e2009-09-05 21:47:34 +00001598 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1599 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1600 if (buffer == (unsigned char *) NULL)
1601 {
1602 (void) close(source_file);
1603 (void) close(destination_file);
1604 return(MagickFalse);
1605 }
1606 length=0;
1607 for (i=0; ; i+=count)
1608 {
1609 count=(ssize_t) read(source_file,buffer,quantum);
1610 if (count <= 0)
1611 break;
1612 length=(size_t) count;
1613 count=(ssize_t) write(destination_file,buffer,length);
1614 if ((size_t) count != length)
1615 break;
1616 }
1617 (void) close(destination_file);
1618 (void) close(source_file);
1619 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
dirkb9dbc292015-07-26 09:50:00 +00001620 return(i != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001621}
1622
1623MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1624 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1625{
1626 char
1627 *command,
1628 **commands,
cristy151b66d2015-04-15 10:50:31 +00001629 input_filename[MagickPathExtent],
1630 output_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001631
1632 const DelegateInfo
1633 *delegate_info;
1634
1635 MagickBooleanType
1636 status,
1637 temporary;
1638
cristy3ed852e2009-09-05 21:47:34 +00001639 PolicyRights
1640 rights;
1641
Cristyf82a2e02016-02-13 08:35:17 -05001642 register ssize_t
1643 i;
1644
cristy3ed852e2009-09-05 21:47:34 +00001645 /*
1646 Get delegate.
1647 */
1648 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001649 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001650 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001651 assert(image->signature == MagickCoreSignature);
cristycb190b72014-08-31 20:04:42 +00001652 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001653 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1654 rights=ExecutePolicyRights;
dirkb9dbc292015-07-26 09:50:00 +00001655 if (IsRightsAuthorized(DelegatePolicyDomain,rights,decode) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001656 {
cristya9197f62010-01-12 02:23:34 +00001657 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001658 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
cristyefe601c2013-01-05 17:51:12 +00001659 "NotAuthorized","`%s'",decode);
cristy3ed852e2009-09-05 21:47:34 +00001660 return(MagickFalse);
1661 }
dirkb9dbc292015-07-26 09:50:00 +00001662 if (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001663 {
cristya9197f62010-01-12 02:23:34 +00001664 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001665 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
cristyefe601c2013-01-05 17:51:12 +00001666 "NotAuthorized","`%s'",encode);
cristy3ed852e2009-09-05 21:47:34 +00001667 return(MagickFalse);
1668 }
dirkb9dbc292015-07-26 09:50:00 +00001669 temporary=*image->filename == '\0' ? MagickTrue : MagickFalse;
1670 if ((temporary != MagickFalse) && (AcquireUniqueFilename(image->filename) ==
1671 MagickFalse))
1672 {
1673 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1674 image->filename);
1675 return(MagickFalse);
1676 }
cristy3ed852e2009-09-05 21:47:34 +00001677 delegate_info=GetDelegateInfo(decode,encode,exception);
1678 if (delegate_info == (DelegateInfo *) NULL)
1679 {
dirkb9dbc292015-07-26 09:50:00 +00001680 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001681 (void) RelinquishUniqueFileResource(image->filename);
1682 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +00001683 "NoTagFound","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001684 return(MagickFalse);
1685 }
1686 if (*image_info->filename == '\0')
1687 {
dirkb9dbc292015-07-26 09:50:00 +00001688 if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001689 {
dirkb9dbc292015-07-26 09:50:00 +00001690 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001691 (void) RelinquishUniqueFileResource(image->filename);
1692 ThrowFileException(exception,FileOpenError,
1693 "UnableToCreateTemporaryFile",image_info->filename);
1694 return(MagickFalse);
1695 }
1696 image_info->temporary=MagickTrue;
1697 }
cristyfc7f59b2011-09-16 00:41:11 +00001698 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
cristy3ed852e2009-09-05 21:47:34 +00001699 (delegate_info->encode != (char *) NULL)) ||
1700 ((encode != (const char *) NULL) &&
1701 (delegate_info->decode != (char *) NULL))))
1702 {
1703 char
1704 *magick;
1705
1706 ImageInfo
1707 *clone_info;
1708
1709 register Image
1710 *p;
1711
1712 /*
1713 Delegate requires a particular image format.
1714 */
dirkb9dbc292015-07-26 09:50:00 +00001715 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001716 {
1717 ThrowFileException(exception,FileOpenError,
1718 "UnableToCreateTemporaryFile",image_info->unique);
1719 return(MagickFalse);
1720 }
cristy3ed852e2009-09-05 21:47:34 +00001721 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
cristy018f07f2011-09-04 21:15:19 +00001722 delegate_info->encode : delegate_info->decode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001723 if (magick == (char *) NULL)
1724 {
1725 (void) RelinquishUniqueFileResource(image_info->unique);
dirkb9dbc292015-07-26 09:50:00 +00001726 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001727 (void) RelinquishUniqueFileResource(image->filename);
1728 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001729 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001730 return(MagickFalse);
1731 }
1732 LocaleUpper(magick);
1733 clone_info=CloneImageInfo(image_info);
1734 (void) CopyMagickString((char *) clone_info->magick,magick,
cristy151b66d2015-04-15 10:50:31 +00001735 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001736 if (LocaleCompare(magick,"NULL") != 0)
cristy151b66d2015-04-15 10:50:31 +00001737 (void) CopyMagickString(image->magick,magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001738 magick=DestroyString(magick);
cristy151b66d2015-04-15 10:50:31 +00001739 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:",
cristy3ed852e2009-09-05 21:47:34 +00001740 delegate_info->decode);
cristyd965a422010-03-03 17:47:35 +00001741 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1742 exception);
cristy3ed852e2009-09-05 21:47:34 +00001743 (void) CopyMagickString(clone_info->filename,image_info->filename,
cristy151b66d2015-04-15 10:50:31 +00001744 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001745 (void) CopyMagickString(image_info->filename,image->filename,
cristy151b66d2015-04-15 10:50:31 +00001746 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001747 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1748 {
cristy151b66d2015-04-15 10:50:31 +00001749 (void) FormatLocaleString(p->filename,MagickPathExtent,"%s:%s",
cristy3ed852e2009-09-05 21:47:34 +00001750 delegate_info->decode,clone_info->filename);
cristy6f9e0d32011-08-28 16:32:09 +00001751 status=WriteImage(clone_info,p,exception);
dirkb9dbc292015-07-26 09:50:00 +00001752 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001753 {
1754 (void) RelinquishUniqueFileResource(image_info->unique);
dirkb9dbc292015-07-26 09:50:00 +00001755 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001756 (void) RelinquishUniqueFileResource(image->filename);
1757 clone_info=DestroyImageInfo(clone_info);
1758 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001759 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
cristy3ed852e2009-09-05 21:47:34 +00001760 return(MagickFalse);
1761 }
dirkb9dbc292015-07-26 09:50:00 +00001762 if (clone_info->adjoin != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001763 break;
1764 }
1765 (void) RelinquishUniqueFileResource(image_info->unique);
cristy3ed852e2009-09-05 21:47:34 +00001766 clone_info=DestroyImageInfo(clone_info);
1767 }
1768 /*
1769 Invoke delegate.
1770 */
1771 commands=StringToList(delegate_info->commands);
1772 if (commands == (char **) NULL)
1773 {
dirk11a7cb72016-01-05 14:00:04 +01001774 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001775 (void) RelinquishUniqueFileResource(image->filename);
1776 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001777 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001778 decode ? decode : encode);
1779 return(MagickFalse);
1780 }
1781 command=(char *) NULL;
dirkb9dbc292015-07-26 09:50:00 +00001782 status=MagickTrue;
1783 (void) CopyMagickString(output_filename,image_info->filename,
1784 MagickPathExtent);
cristy151b66d2015-04-15 10:50:31 +00001785 (void) CopyMagickString(input_filename,image->filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001786 for (i=0; commands[i] != (char *) NULL; i++)
1787 {
dirkb9dbc292015-07-26 09:50:00 +00001788 (void) AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1789 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001790 {
1791 ThrowFileException(exception,FileOpenError,
1792 "UnableToCreateTemporaryFile",image_info->unique);
1793 break;
1794 }
cristy3ed852e2009-09-05 21:47:34 +00001795 if (LocaleCompare(decode,"SCAN") != 0)
1796 {
1797 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
dirkb9dbc292015-07-26 09:50:00 +00001798 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001799 {
1800 ThrowFileException(exception,FileOpenError,
1801 "UnableToCreateTemporaryFile",input_filename);
1802 break;
1803 }
1804 }
dirkb9dbc292015-07-26 09:50:00 +00001805 status=MagickTrue;
Cristy8be0b1e2016-05-13 20:45:40 -04001806 command=InterpretDelegateProperties(image_info,image,commands[i],exception);
cristy3ed852e2009-09-05 21:47:34 +00001807 if (command != (char *) NULL)
1808 {
1809 /*
1810 Execute delegate.
1811 */
dirkb9dbc292015-07-26 09:50:00 +00001812 if (ExternalDelegateCommand(delegate_info->spawn,image_info->verbose,
1813 command,(char *) NULL,exception) != 0)
1814 status=MagickFalse;
1815 if (delegate_info->spawn != MagickFalse)
cristy77f69392012-05-26 01:15:25 +00001816 {
1817 ssize_t
1818 count;
1819
1820 /*
Cristyc7107ee2016-07-23 19:08:45 -04001821 Wait for input file to 'disappear', or maximum 2 seconds.
cristy77f69392012-05-26 01:15:25 +00001822 */
Cristyc7107ee2016-07-23 19:08:45 -04001823 count=20;
cristy3fe49df2012-05-26 01:17:56 +00001824 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
cristy58399382012-05-26 12:44:41 +00001825 (void) MagickDelay(100); /* sleep 0.1 seconds */
anthony2ec9bd92012-05-20 05:43:24 +00001826 }
cristy3ed852e2009-09-05 21:47:34 +00001827 command=DestroyString(command);
1828 }
1829 if (LocaleCompare(decode,"SCAN") != 0)
1830 {
cristy4886c4f2015-06-06 15:26:09 +00001831 if (CopyDelegateFile(image->filename,input_filename,MagickFalse) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001832 (void) RelinquishUniqueFileResource(input_filename);
1833 }
Cristyf82a2e02016-02-13 08:35:17 -05001834 if ((strcmp(input_filename,output_filename) != 0) &&
1835 (CopyDelegateFile(image_info->filename,output_filename,MagickTrue) == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001836 (void) RelinquishUniqueFileResource(output_filename);
dirkb9dbc292015-07-26 09:50:00 +00001837 if (image_info->temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001838 (void) RelinquishUniqueFileResource(image_info->filename);
1839 (void) RelinquishUniqueFileResource(image_info->unique);
cristy3ed852e2009-09-05 21:47:34 +00001840 (void) RelinquishUniqueFileResource(image_info->filename);
1841 (void) RelinquishUniqueFileResource(image->filename);
dirkb9dbc292015-07-26 09:50:00 +00001842 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001843 {
1844 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
cristyefe601c2013-01-05 17:51:12 +00001845 "DelegateFailed","`%s'",commands[i]);
cristy3ed852e2009-09-05 21:47:34 +00001846 break;
1847 }
1848 commands[i]=DestroyString(commands[i]);
1849 }
Cristy8ef5a072016-05-14 11:49:10 -04001850 (void) CopyMagickString(image_info->filename,output_filename,
1851 MagickPathExtent);
cristy151b66d2015-04-15 10:50:31 +00001852 (void) CopyMagickString(image->filename,input_filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001853 /*
1854 Relinquish resources.
1855 */
1856 for ( ; commands[i] != (char *) NULL; i++)
1857 commands[i]=DestroyString(commands[i]);
1858 commands=(char **) RelinquishMagickMemory(commands);
dirkb9dbc292015-07-26 09:50:00 +00001859 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001860 (void) RelinquishUniqueFileResource(image->filename);
dirkb9dbc292015-07-26 09:50:00 +00001861 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001862}
1863
1864/*
1865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866% %
1867% %
1868% %
1869% L i s t D e l e g a t e I n f o %
1870% %
1871% %
1872% %
1873%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1874%
1875% ListDelegateInfo() lists the image formats to a file.
1876%
1877% The format of the ListDelegateInfo method is:
1878%
1879% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1880%
1881% A description of each parameter follows.
1882%
1883% o file: An pointer to a FILE.
1884%
1885% o exception: return any errors or warnings in this structure.
1886%
1887*/
1888MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1889 ExceptionInfo *exception)
1890{
1891 const DelegateInfo
1892 **delegate_info;
1893
1894 char
1895 **commands,
cristy151b66d2015-04-15 10:50:31 +00001896 delegate[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001897
1898 const char
1899 *path;
1900
cristybb503372010-05-27 20:51:26 +00001901 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001902 i;
1903
cristybb503372010-05-27 20:51:26 +00001904 size_t
cristy3ed852e2009-09-05 21:47:34 +00001905 number_delegates;
1906
cristy9d314ff2011-03-09 01:30:28 +00001907 ssize_t
1908 j;
1909
cristy3ed852e2009-09-05 21:47:34 +00001910 if (file == (const FILE *) NULL)
1911 file=stdout;
1912 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1913 if (delegate_info == (const DelegateInfo **) NULL)
1914 return(MagickFalse);
1915 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +00001916 for (i=0; i < (ssize_t) number_delegates; i++)
cristy3ed852e2009-09-05 21:47:34 +00001917 {
dirk11a7cb72016-01-05 14:00:04 +01001918 if (delegate_info[i]->stealth != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001919 continue;
1920 if ((path == (const char *) NULL) ||
1921 (LocaleCompare(path,delegate_info[i]->path) != 0))
1922 {
1923 if (delegate_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +00001924 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1925 (void) FormatLocaleFile(file,"Delegate Command\n");
cristy1e604812011-05-19 18:07:50 +00001926 (void) FormatLocaleFile(file,
1927 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +00001928 "------------------------------\n");
1929 }
1930 path=delegate_info[i]->path;
1931 *delegate='\0';
1932 if (delegate_info[i]->encode != (char *) NULL)
Cristy5f478ca2016-05-14 18:43:20 -04001933 (void) CopyMagickString(delegate,delegate_info[i]->encode,
1934 MagickPathExtent);
cristy151b66d2015-04-15 10:50:31 +00001935 (void) ConcatenateMagickString(delegate," ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001936 delegate[8]='\0';
1937 commands=StringToList(delegate_info[i]->commands);
1938 if (commands == (char **) NULL)
1939 continue;
cristyb51dff52011-05-19 16:55:47 +00001940 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
cristy3ed852e2009-09-05 21:47:34 +00001941 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1942 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1943 StripString(commands[0]);
cristyb51dff52011-05-19 16:55:47 +00001944 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
cristy3ed852e2009-09-05 21:47:34 +00001945 for (j=1; commands[j] != (char *) NULL; j++)
1946 {
1947 StripString(commands[j]);
cristyb51dff52011-05-19 16:55:47 +00001948 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
cristy3ed852e2009-09-05 21:47:34 +00001949 }
1950 for (j=0; commands[j] != (char *) NULL; j++)
1951 commands[j]=DestroyString(commands[j]);
1952 commands=(char **) RelinquishMagickMemory(commands);
1953 }
1954 (void) fflush(file);
1955 delegate_info=(const DelegateInfo **)
1956 RelinquishMagickMemory((void *) delegate_info);
1957 return(MagickTrue);
1958}
1959
1960/*
1961%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1962% %
1963% %
1964% %
dirkfc0f1242016-03-26 00:36:39 +01001965+ L o a d D e l e g a t e C a c h e %
cristy3ed852e2009-09-05 21:47:34 +00001966% %
1967% %
1968% %
1969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970%
cristy86e5ac92014-03-16 19:27:39 +00001971% LoadDelegateCache() loads the delegate configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +00001972% mapping between delegate attributes and a delegate name.
1973%
cristy86e5ac92014-03-16 19:27:39 +00001974% The format of the LoadDelegateCache method is:
cristy3ed852e2009-09-05 21:47:34 +00001975%
dirkc9f5cc82016-07-17 18:59:02 +02001976% MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
cristycd2cd182014-03-18 12:10:55 +00001977% const char *xml,const char *filename,const size_t depth,
1978% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001979%
1980% A description of each parameter follows:
1981%
1982% o xml: The delegate list in XML format.
1983%
1984% o filename: The delegate list filename.
1985%
1986% o depth: depth of <include /> statements.
1987%
1988% o exception: return any errors or warnings in this structure.
1989%
1990*/
dirkc9f5cc82016-07-17 18:59:02 +02001991static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
cristycd2cd182014-03-18 12:10:55 +00001992 const char *xml,const char *filename,const size_t depth,
1993 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001994{
1995 char
cristy151b66d2015-04-15 10:50:31 +00001996 keyword[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001997 *token;
1998
1999 const char
2000 *q;
2001
2002 DelegateInfo
2003 *delegate_info;
2004
cristy759ba912014-06-26 11:59:43 +00002005 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +00002006 status;
2007
dirkfc0f1242016-03-26 00:36:39 +01002008 size_t
2009 extent;
2010
cristy3ed852e2009-09-05 21:47:34 +00002011 /*
2012 Load the delegate map file.
2013 */
2014 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
2015 "Loading delegate configuration file \"%s\" ...",filename);
2016 if (xml == (const char *) NULL)
2017 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002018 status=MagickTrue;
2019 delegate_info=(DelegateInfo *) NULL;
2020 token=AcquireString(xml);
dirkfc0f1242016-03-26 00:36:39 +01002021 extent=strlen(token)+MagickPathExtent;
cristy3ed852e2009-09-05 21:47:34 +00002022 for (q=(const char *) xml; *q != '\0'; )
2023 {
2024 /*
2025 Interpret XML.
2026 */
Cristy8bedb4e2016-03-25 19:54:25 -04002027 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002028 if (*token == '\0')
2029 break;
cristy151b66d2015-04-15 10:50:31 +00002030 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002031 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
2032 {
2033 /*
2034 Doctype element.
2035 */
2036 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
Cristy8bedb4e2016-03-25 19:54:25 -04002037 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002038 continue;
2039 }
2040 if (LocaleNCompare(keyword,"<!--",4) == 0)
2041 {
2042 /*
2043 Comment element.
2044 */
2045 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
Cristy8bedb4e2016-03-25 19:54:25 -04002046 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002047 continue;
2048 }
2049 if (LocaleCompare(keyword,"<include") == 0)
2050 {
2051 /*
2052 Include element.
2053 */
2054 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
2055 {
cristy151b66d2015-04-15 10:50:31 +00002056 (void) CopyMagickString(keyword,token,MagickPathExtent);
Cristy8bedb4e2016-03-25 19:54:25 -04002057 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002058 if (*token != '=')
2059 continue;
Cristy8bedb4e2016-03-25 19:54:25 -04002060 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002061 if (LocaleCompare(keyword,"file") == 0)
2062 {
Cristy8a4029f2018-02-05 07:35:07 -05002063 if (depth > MagickMaxRecursionDepth)
cristy3ed852e2009-09-05 21:47:34 +00002064 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002065 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00002066 else
2067 {
2068 char
cristy151b66d2015-04-15 10:50:31 +00002069 path[MagickPathExtent],
dirkaa6a11c2015-10-18 12:21:23 +02002070 *file_xml;
cristy3ed852e2009-09-05 21:47:34 +00002071
2072 GetPathComponent(filename,HeadPath,path);
2073 if (*path != '\0')
2074 (void) ConcatenateMagickString(path,DirectorySeparator,
cristy151b66d2015-04-15 10:50:31 +00002075 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002076 if (*token == *DirectorySeparator)
cristy151b66d2015-04-15 10:50:31 +00002077 (void) CopyMagickString(path,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002078 else
cristy151b66d2015-04-15 10:50:31 +00002079 (void) ConcatenateMagickString(path,token,MagickPathExtent);
dirkaa6a11c2015-10-18 12:21:23 +02002080 file_xml=FileToXML(path,~0UL);
2081 if (file_xml != (char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002082 {
dirkc9f5cc82016-07-17 18:59:02 +02002083 status&=LoadDelegateCache(cache,file_xml,path,
cristycd2cd182014-03-18 12:10:55 +00002084 depth+1,exception);
dirkb81f0ab2015-10-20 23:01:53 +02002085 file_xml=DestroyString(file_xml);
cristy3ed852e2009-09-05 21:47:34 +00002086 }
2087 }
2088 }
2089 }
2090 continue;
2091 }
2092 if (LocaleCompare(keyword,"<delegate") == 0)
2093 {
2094 /*
2095 Delegate element.
2096 */
Dirk Lemstra06344a02017-10-15 10:10:01 +02002097 delegate_info=(DelegateInfo *) AcquireCriticalMemory(
cristy3ed852e2009-09-05 21:47:34 +00002098 sizeof(*delegate_info));
Cristy81bfff22018-03-10 07:58:31 -05002099 (void) memset(delegate_info,0,sizeof(*delegate_info));
cristy3ed852e2009-09-05 21:47:34 +00002100 delegate_info->path=ConstantString(filename);
cristy571ce682014-03-19 13:52:13 +00002101 delegate_info->thread_support=MagickTrue;
cristye1c94d92015-06-28 12:16:33 +00002102 delegate_info->signature=MagickCoreSignature;
cristy3ed852e2009-09-05 21:47:34 +00002103 continue;
2104 }
2105 if (delegate_info == (DelegateInfo *) NULL)
2106 continue;
Cristyfed111e2017-01-09 07:22:16 -05002107 if ((LocaleCompare(keyword,"/>") == 0) ||
2108 (LocaleCompare(keyword,"</policy>") == 0))
cristy3ed852e2009-09-05 21:47:34 +00002109 {
dirkc9f5cc82016-07-17 18:59:02 +02002110 status=AppendValueToLinkedList(cache,delegate_info);
dirk5d0a4412016-01-05 22:28:51 +01002111 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002112 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002113 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00002114 delegate_info->commands);
2115 delegate_info=(DelegateInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +00002116 continue;
cristy3ed852e2009-09-05 21:47:34 +00002117 }
Cristy8bedb4e2016-03-25 19:54:25 -04002118 GetNextToken(q,(const char **) NULL,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002119 if (*token != '=')
2120 continue;
Cristy8bedb4e2016-03-25 19:54:25 -04002121 GetNextToken(q,&q,extent,token);
2122 GetNextToken(q,&q,extent,token);
cristy3ed852e2009-09-05 21:47:34 +00002123 switch (*keyword)
2124 {
2125 case 'C':
2126 case 'c':
2127 {
2128 if (LocaleCompare((char *) keyword,"command") == 0)
2129 {
2130 char
2131 *commands;
2132
2133 commands=AcquireString(token);
cristy0157aea2010-04-24 21:12:18 +00002134#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00002135 if (strchr(commands,'@') != (char *) NULL)
2136 {
2137 char
cristy151b66d2015-04-15 10:50:31 +00002138 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00002139
cristy151b66d2015-04-15 10:50:31 +00002140 NTGhostscriptEXE(path,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002141 (void) SubstituteString((char **) &commands,"@PSDelegate@",
2142 path);
2143 (void) SubstituteString((char **) &commands,"\\","/");
2144 }
dirk04451442016-05-15 08:33:28 +02002145 (void) SubstituteString((char **) &commands,"&quot;","\"");
2146#else
2147 (void) SubstituteString((char **) &commands,"&quot;","'");
cristy3ed852e2009-09-05 21:47:34 +00002148#endif
2149 (void) SubstituteString((char **) &commands,"&amp;","&");
cristy3ed852e2009-09-05 21:47:34 +00002150 (void) SubstituteString((char **) &commands,"&gt;",">");
2151 (void) SubstituteString((char **) &commands,"&lt;","<");
2152 delegate_info->commands=commands;
2153 break;
2154 }
2155 break;
2156 }
2157 case 'D':
2158 case 'd':
2159 {
2160 if (LocaleCompare((char *) keyword,"decode") == 0)
2161 {
2162 delegate_info->decode=ConstantString(token);
2163 delegate_info->mode=1;
2164 break;
2165 }
2166 break;
2167 }
2168 case 'E':
2169 case 'e':
2170 {
2171 if (LocaleCompare((char *) keyword,"encode") == 0)
2172 {
2173 delegate_info->encode=ConstantString(token);
2174 delegate_info->mode=(-1);
2175 break;
2176 }
2177 break;
2178 }
2179 case 'M':
2180 case 'm':
2181 {
2182 if (LocaleCompare((char *) keyword,"mode") == 0)
2183 {
2184 delegate_info->mode=1;
2185 if (LocaleCompare(token,"bi") == 0)
2186 delegate_info->mode=0;
2187 else
2188 if (LocaleCompare(token,"encode") == 0)
2189 delegate_info->mode=(-1);
2190 break;
2191 }
2192 break;
2193 }
2194 case 'S':
2195 case 's':
2196 {
2197 if (LocaleCompare((char *) keyword,"spawn") == 0)
2198 {
anthony6f201312012-03-30 04:08:15 +00002199 delegate_info->spawn=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +00002200 break;
2201 }
2202 if (LocaleCompare((char *) keyword,"stealth") == 0)
2203 {
anthony6f201312012-03-30 04:08:15 +00002204 delegate_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +00002205 break;
2206 }
2207 break;
2208 }
2209 case 'T':
2210 case 't':
2211 {
2212 if (LocaleCompare((char *) keyword,"thread-support") == 0)
2213 {
anthony6f201312012-03-30 04:08:15 +00002214 delegate_info->thread_support=IsStringTrue(token);
cristy571ce682014-03-19 13:52:13 +00002215 if (delegate_info->thread_support == MagickFalse)
2216 delegate_info->semaphore=AcquireSemaphoreInfo();
cristy3ed852e2009-09-05 21:47:34 +00002217 break;
2218 }
2219 break;
2220 }
2221 default:
2222 break;
2223 }
2224 }
2225 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +00002226 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002227}