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