MagickCore 7.1.2-32
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 (*arguments[1] == '\0')
419 {
420 for (i=0; i < (ssize_t) number_arguments; i++)
421 arguments[i]=DestroyString(arguments[i]);
422 arguments=(char **) RelinquishMagickMemory(arguments);
423 return(-1);
424 }
425 rights=ExecutePolicyRights;
426 domain=DelegatePolicyDomain;
427 for (i=1; i < (ssize_t) number_arguments; i++)
428 {
429 if ((i != 1) && (IsExecutableToken(arguments[i]) == MagickFalse))
430 continue;
431 if (IsRightsAuthorized(domain,rights,arguments[i]) == MagickFalse)
432 {
433 ssize_t
434 j;
435
436 errno=EPERM;
437 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
438 "NotAuthorized","`%s'",arguments[i]);
439 for (j=0; j < (ssize_t) number_arguments; j++)
440 arguments[j]=DestroyString(arguments[j]);
441 arguments=(char **) RelinquishMagickMemory(arguments);
442 return(-1);
443 }
444 }
445 if (verbose != MagickFalse)
446 {
447 (void) FormatLocaleFile(stderr,"%s\n",command);
448 (void) fflush(stderr);
449 }
450 sanitize_command=SanitizeString(command);
451 if (asynchronous != MagickFalse)
452 (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
453 if (message != (char *) NULL)
454 *message='\0';
455#if defined(MAGICKCORE_POSIX_SUPPORT)
456#if defined(MAGICKCORE_HAVE_POPEN)
457 if ((asynchronous == MagickFalse) && (message != (char *) NULL))
458 {
459 FILE
460 *file;
461
462 file=popen_utf8(sanitize_command,"r");
463 if (file == (FILE *) NULL)
464 status=system(sanitize_command);
465 else
466 {
467 size_t
468 offset = 0;
469
470 while ((offset < MagickPathExtent) &&
471 (fgets(message+offset,(int) (MagickPathExtent-offset),file) != NULL))
472 offset+=strlen(message);
473 status=pclose(file);
474 }
475 }
476 else
477#endif
478 {
479#if !defined(MAGICKCORE_HAVE_EXECVP)
480 status=system(sanitize_command);
481#else
482 if ((asynchronous != MagickFalse) ||
483 (strpbrk(sanitize_command,"&;<>|") != (char *) NULL))
484 status=system(sanitize_command);
485 else
486 {
487 pid_t
488 child_pid;
489
490 /*
491 Call application directly rather than from a shell.
492 */
493 child_pid=(pid_t) fork();
494 if (child_pid == (pid_t) -1)
495 status=system(sanitize_command);
496 else
497 if (child_pid == 0)
498 {
499 status=execvp(arguments[1],arguments+1);
500 _exit(1);
501 }
502 else
503 {
504 int
505 child_status;
506
507 pid_t
508 pid;
509
510 child_status=0;
511 pid=(pid_t) waitpid(child_pid,&child_status,0);
512 if (pid == -1)
513 status=(-1);
514 else
515 {
516 if (WIFEXITED(child_status) != 0)
517 status=WEXITSTATUS(child_status);
518 else
519 if (WIFSIGNALED(child_status))
520 status=(-1);
521 }
522 }
523 }
524#endif
525 }
526#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
527 {
528 char
529 *p;
530
531 /*
532 If a command shell is executed we need to change the forward slashes in
533 files to a backslash. We need to do this to keep Windows happy when we
534 want to 'move' a file.
535
536 TODO: This won't work if one of the delegate parameters has a forward
537 slash as a parameter.
538 */
539 p=strstr(sanitize_command,"cmd.exe /c");
540 if (p != (char*) NULL)
541 {
542 p+=(ptrdiff_t) 10;
543 for ( ; *p != '\0'; p++)
544 if (*p == '/')
545 *p=(*DirectorySeparator);
546 }
547 }
548 status=NTSystemCommand(sanitize_command,message);
549#elif defined(vms)
550 status=system(sanitize_command);
551#else
552# error No suitable system() method.
553#endif
554 if (status < 0)
555 {
556 if ((message != (char *) NULL) && (*message != '\0'))
557 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
558 "FailedToExecuteCommand","`%s' (%s)",sanitize_command,message);
559 else
560 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
561 "FailedToExecuteCommand","`%s' (%d)",sanitize_command,status);
562 }
563 sanitize_command=DestroyString(sanitize_command);
564 for (i=0; i < (ssize_t) number_arguments; i++)
565 arguments[i]=DestroyString(arguments[i]);
566 arguments=(char **) RelinquishMagickMemory(arguments);
567 return(status);
568}
569
570/*
571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572% %
573% %
574% %
575% G e t D e l e g a t e C o m m a n d %
576% %
577% %
578% %
579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580%
581% GetDelegateCommand() replaces any embedded formatting characters with the
582% appropriate image attribute and returns the resulting command.
583%
584% The format of the GetDelegateCommand method is:
585%
586% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
587% const char *decode,const char *encode,ExceptionInfo *exception)
588%
589% A description of each parameter follows:
590%
591% o command: Method GetDelegateCommand returns the command associated
592% with specified delegate tag.
593%
594% o image_info: the image info.
595%
596% o image: the image.
597%
598% o decode: Specifies the decode delegate we are searching for as a
599% character string.
600%
601% o encode: Specifies the encode delegate we are searching for as a
602% character string.
603%
604% o exception: return any errors or warnings in this structure.
605%
606*/
607
608static char *GetMagickPropertyLetter(ImageInfo *image_info,Image *image,
609 const char letter,ExceptionInfo *exception)
610{
611#define WarnNoImageReturn(format,letter) \
612 if (image == (Image *) NULL) \
613 { \
614 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
615 "NoImageForProperty",format,letter); \
616 break; \
617 }
618#define WarnNoImageInfoReturn(format,letter) \
619 if (image_info == (ImageInfo *) NULL) \
620 { \
621 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
622 "NoImageInfoForProperty",format,letter); \
623 break; \
624 }
625
626 char
627 value[MagickPathExtent];
628
629 const char
630 *string;
631
632 if ((image != (Image *) NULL) && (IsEventLogging() != MagickFalse))
633 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
634 else
635 if ((image_info != (ImageInfo *) NULL) && (IsEventLogging() != MagickFalse))
636 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-images");
637 /*
638 Get properties that are directly defined by images.
639 */
640 *value='\0'; /* formatted string */
641 string=(const char *) value;
642 switch (letter)
643 {
644 case 'a': /* authentication passphrase */
645 {
646 WarnNoImageInfoReturn("\"%%%c\"",letter);
647 string=GetImageOption(image_info,"authenticate");
648 break;
649 }
650 case 'b': /* image size read in - in bytes */
651 {
652 WarnNoImageReturn("\"%%%c\"",letter);
653 (void) FormatMagickSize(image->extent,MagickFalse,"B",MagickPathExtent,
654 value);
655 if (image->extent == 0)
656 (void) FormatMagickSize(GetBlobSize(image),MagickFalse,"B",
657 MagickPathExtent,value);
658 break;
659 }
660 case 'd': /* Directory component of filename */
661 {
662 WarnNoImageReturn("\"%%%c\"",letter);
663 GetPathComponent(image->magick_filename,HeadPath,value);
664 break;
665 }
666 case 'e': /* Filename extension (suffix) of image file */
667 {
668 WarnNoImageReturn("\"%%%c\"",letter);
669 GetPathComponent(image->magick_filename,ExtensionPath,value);
670 break;
671 }
672 case 'f': /* Filename without directory component */
673 {
674 WarnNoImageReturn("\"%%%c\"",letter);
675 GetPathComponent(image->magick_filename,TailPath,value);
676 break;
677 }
678 case 'g': /* Image geometry, canvas and offset %Wx%H+%X+%Y */
679 {
680 WarnNoImageReturn("\"%%%c\"",letter);
681 (void) FormatLocaleString(value,MagickPathExtent,
682 "%.17gx%.17g%+.20g%+.20g",(double) image->page.width,(double)
683 image->page.height,(double) image->page.x,(double) image->page.y);
684 break;
685 }
686 case 'h': /* Image height (current) */
687 {
688 WarnNoImageReturn("\"%%%c\"",letter);
689 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
690 (image->rows != 0 ? image->rows : image->magick_rows));
691 break;
692 }
693 case 'i': /* Filename last used for an image (read or write) */
694 {
695 WarnNoImageReturn("\"%%%c\"",letter);
696 string=image->filename;
697 break;
698 }
699 case 'm': /* Image format (file magick) */
700 {
701 WarnNoImageReturn("\"%%%c\"",letter);
702 string=image->magick;
703 break;
704 }
705 case 'n': /* Number of images in the list. */
706 {
707 if (image != (Image *) NULL)
708 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
709 GetImageListLength(image));
710 break;
711 }
712 case 'o': /* Output Filename */
713 {
714 WarnNoImageInfoReturn("\"%%%c\"",letter);
715 string=image_info->filename;
716 break;
717 }
718 case 'p': /* Image index in current image list */
719 {
720 WarnNoImageReturn("\"%%%c\"",letter);
721 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
722 GetImageIndexInList(image));
723 break;
724 }
725 case 'q': /* Quantum depth of image in memory */
726 {
727 WarnNoImageReturn("\"%%%c\"",letter);
728 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
729 MAGICKCORE_QUANTUM_DEPTH);
730 break;
731 }
732 case 'r': /* Image storage class, colorspace, and alpha enabled. */
733 {
734 ColorspaceType
735 colorspace;
736
737 WarnNoImageReturn("\"%%%c\"",letter);
738 colorspace=image->colorspace;
739 (void) FormatLocaleString(value,MagickPathExtent,"%s %s %s",
740 CommandOptionToMnemonic(MagickClassOptions,(ssize_t)
741 image->storage_class),CommandOptionToMnemonic(MagickColorspaceOptions,
742 (ssize_t) colorspace),image->alpha_trait != UndefinedPixelTrait ?
743 "Alpha" : "");
744 break;
745 }
746 case 's': /* Image scene number */
747 {
748 WarnNoImageReturn("\"%%%c\"",letter);
749 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
750 image->scene);
751 break;
752 }
753 case 't': /* Base filename without directory or extension */
754 {
755 WarnNoImageReturn("\"%%%c\"",letter);
756 GetPathComponent(image->magick_filename,BasePath,value);
757 break;
758 }
759 case 'u': /* Unique filename */
760 {
761 WarnNoImageInfoReturn("\"%%%c\"",letter);
762 string=image_info->unique;
763 break;
764 }
765 case 'w': /* Image width (current) */
766 {
767 WarnNoImageReturn("\"%%%c\"",letter);
768 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
769 (image->columns != 0 ? image->columns : image->magick_columns));
770 break;
771 }
772 case 'x': /* Image horizontal resolution (with units) */
773 {
774 WarnNoImageReturn("\"%%%c\"",letter);
775 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",
776 fabs(image->resolution.x) > MagickEpsilon ? image->resolution.x :
777 image->units == PixelsPerCentimeterResolution ? DefaultResolution/2.54 :
778 DefaultResolution);
779 break;
780 }
781 case 'y': /* Image vertical resolution (with units) */
782 {
783 WarnNoImageReturn("\"%%%c\"",letter);
784 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",
785 fabs(image->resolution.y) > MagickEpsilon ? image->resolution.y :
786 image->units == PixelsPerCentimeterResolution ? DefaultResolution/2.54 :
787 DefaultResolution);
788 break;
789 }
790 case 'z': /* Image depth as read in */
791 {
792 WarnNoImageReturn("\"%%%c\"",letter);
793 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",
794 (double) image->depth);
795 break;
796 }
797 case 'A': /* Image alpha channel */
798 {
799 WarnNoImageReturn("\"%%%c\"",letter);
800 string=CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
801 image->alpha_trait);
802 break;
803 }
804 case 'C': /* Image compression method. */
805 {
806 WarnNoImageReturn("\"%%%c\"",letter);
807 string=CommandOptionToMnemonic(MagickCompressOptions,
808 (ssize_t) image->compression);
809 break;
810 }
811 case 'D': /* Image dispose method. */
812 {
813 WarnNoImageReturn("\"%%%c\"",letter);
814 string=CommandOptionToMnemonic(MagickDisposeOptions,
815 (ssize_t) image->dispose);
816 break;
817 }
818 case 'F':
819 {
820 /*
821 Magick filename - filename given incl. coder & read mods.
822 */
823 WarnNoImageReturn("\"%%%c\"",letter);
824 (void) CopyMagickString(value,image->magick_filename,MagickPathExtent);
825 break;
826 }
827 case 'G': /* Image size as geometry = "%wx%h" */
828 {
829 WarnNoImageReturn("\"%%%c\"",letter);
830 (void) FormatLocaleString(value,MagickPathExtent,"%.17gx%.17g",
831 (double) image->magick_columns,(double) image->magick_rows);
832 break;
833 }
834 case 'H': /* layer canvas height */
835 {
836 WarnNoImageReturn("\"%%%c\"",letter);
837 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",
838 (double) image->page.height);
839 break;
840 }
841 case 'I': /* image iterations for animations */
842 {
843 WarnNoImageReturn("\"%%%c\"",letter);
844 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
845 image->iterations);
846 break;
847 }
848 case 'M': /* Magick filename - filename given incl. coder & read mods */
849 {
850 WarnNoImageReturn("\"%%%c\"",letter);
851 string=image->magick_filename;
852 break;
853 }
854 case 'O': /* layer canvas offset with sign = "+%X+%Y" */
855 {
856 WarnNoImageReturn("\"%%%c\"",letter);
857 (void) FormatLocaleString(value,MagickPathExtent,"%+ld%+ld",(long)
858 image->page.x,(long) image->page.y);
859 break;
860 }
861 case 'P': /* layer canvas page size = "%Wx%H" */
862 {
863 WarnNoImageReturn("\"%%%c\"",letter);
864 (void) FormatLocaleString(value,MagickPathExtent,"%.17gx%.17g",
865 (double) image->page.width,(double) image->page.height);
866 break;
867 }
868 case '~': /* BPG image compression quality */
869 {
870 WarnNoImageReturn("\"%%%c\"",letter);
871 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
872 (100-(image->quality == 0 ? 42 : image->quality))/2);
873 break;
874 }
875 case 'Q': /* image compression quality */
876 {
877 WarnNoImageReturn("\"%%%c\"",letter);
878 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
879 (image->quality == 0 ? 92 : image->quality));
880 break;
881 }
882 case 'S': /* Number of scenes in image list. */
883 {
884 WarnNoImageInfoReturn("\"%%%c\"",letter);
885 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
886 (image_info->number_scenes == 0 ? 2147483647 :
887 image_info->number_scenes));
888 break;
889 }
890 case 'T': /* image time delay for animations */
891 {
892 WarnNoImageReturn("\"%%%c\"",letter);
893 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
894 image->delay);
895 break;
896 }
897 case 'U': /* Image resolution units. */
898 {
899 WarnNoImageReturn("\"%%%c\"",letter);
900 string=CommandOptionToMnemonic(MagickResolutionOptions,
901 (ssize_t) image->units);
902 break;
903 }
904 case 'W': /* layer canvas width */
905 {
906 WarnNoImageReturn("\"%%%c\"",letter);
907 (void) FormatLocaleString(value,MagickPathExtent,"%.17g",(double)
908 image->page.width);
909 break;
910 }
911 case 'X': /* layer canvas X offset */
912 {
913 WarnNoImageReturn("\"%%%c\"",letter);
914 (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
915 image->page.x);
916 break;
917 }
918 case 'Y': /* layer canvas Y offset */
919 {
920 WarnNoImageReturn("\"%%%c\"",letter);
921 (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
922 image->page.y);
923 break;
924 }
925 case '%': /* percent escaped */
926 {
927 string="%";
928 break;
929 }
930 case '@': /* Trim bounding box, without actually trimming! */
931 {
932 RectangleInfo
933 page;
934
935 WarnNoImageReturn("\"%%%c\"",letter);
936 page=GetImageBoundingBox(image,exception);
937 (void) FormatLocaleString(value,MagickPathExtent,
938 "%.17gx%.17g%+.20g%+.20g",(double) page.width,(double) page.height,
939 (double) page.x,(double) page.y);
940 break;
941 }
942 case '#':
943 {
944 /*
945 Image signature.
946 */
947 WarnNoImageReturn("\"%%%c\"",letter);
948 (void) SignatureImage(image,exception);
949 string=GetImageProperty(image,"signature",exception);
950 break;
951 }
952 }
953 return(SanitizeDelegateString(string));
954}
955
956static char *InterpretDelegateProperties(ImageInfo *image_info,
957 Image *image,const char *embed_text,ExceptionInfo *exception)
958{
959#define ExtendInterpretText(string_length) \
960{ \
961 size_t length=(string_length); \
962 if ((size_t) (q-interpret_text+(ssize_t) length+1) >= extent) \
963 { \
964 extent+=length; \
965 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
966 MagickPathExtent,sizeof(*interpret_text)); \
967 if (interpret_text == (char *) NULL) \
968 return((char *) NULL); \
969 q=interpret_text+strlen(interpret_text); \
970 } \
971}
972
973#define AppendKeyValue2Text(key,value)\
974{ \
975 size_t length=strlen(key)+strlen(value)+2; \
976 if ((size_t) (q-interpret_text+length+1) >= extent) \
977 { \
978 extent+=length; \
979 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
980 MagickPathExtent,sizeof(*interpret_text)); \
981 if (interpret_text == (char *) NULL) \
982 return((char *) NULL); \
983 q=interpret_text+strlen(interpret_text); \
984 } \
985 q+=(ptrdiff_t) FormatLocaleString(q,extent,"%s=%s\n",(key),(value)); \
986}
987
988#define AppendString2Text(string) \
989{ \
990 size_t length=strlen((string)); \
991 if ((size_t) (q-interpret_text+(ssize_t) length+1) >= extent) \
992 { \
993 extent+=length; \
994 interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
995 MagickPathExtent,sizeof(*interpret_text)); \
996 if (interpret_text == (char *) NULL) \
997 return((char *) NULL); \
998 q=interpret_text+strlen(interpret_text); \
999 } \
1000 (void) CopyMagickString(q,(string),extent); \
1001 q+=(ptrdiff_t) length; \
1002}
1003
1004 char
1005 *interpret_text,
1006 *string;
1007
1008 char
1009 *q; /* current position in interpret_text */
1010
1011 const char
1012 *p; /* position in embed_text string being expanded */
1013
1014 size_t
1015 extent; /* allocated length of interpret_text */
1016
1017 MagickBooleanType
1018 number;
1019
1020 assert(image == NULL || image->signature == MagickCoreSignature);
1021 assert(image_info == NULL || image_info->signature == MagickCoreSignature);
1022 if ((image != (Image *) NULL) && (IsEventLogging() != MagickFalse))
1023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1024 else
1025 if ((image_info != (ImageInfo *) NULL) && (IsEventLogging() != MagickFalse))
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-image");
1027 if (embed_text == (const char *) NULL)
1028 return(ConstantString(""));
1029 p=embed_text;
1030 while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
1031 p++;
1032 if (*p == '\0')
1033 return(ConstantString(""));
1034 /*
1035 Translate any embedded format characters.
1036 */
1037 interpret_text=AcquireString(embed_text); /* new string with extra space */
1038 extent=MagickPathExtent; /* allocated space in string */
1039 number=MagickFalse; /* is last char a number? */
1040 for (q=interpret_text; *p!='\0';
1041 number=isdigit((int) ((unsigned char) *p)) ? MagickTrue : MagickFalse,p++)
1042 {
1043 /*
1044 Interpret escape characters (e.g. Filename: %M).
1045 */
1046 *q='\0';
1047 ExtendInterpretText(MagickPathExtent);
1048 switch (*p)
1049 {
1050 case '\\':
1051 {
1052 switch (*(p+1))
1053 {
1054 case '\0':
1055 continue;
1056 case 'r': /* convert to RETURN */
1057 {
1058 *q++='\r';
1059 p++;
1060 continue;
1061 }
1062 case 'n': /* convert to NEWLINE */
1063 {
1064 *q++='\n';
1065 p++;
1066 continue;
1067 }
1068 case '\n': /* EOL removal UNIX,MacOSX */
1069 {
1070 p++;
1071 continue;
1072 }
1073 case '\r': /* EOL removal DOS,Windows */
1074 {
1075 p++;
1076 if (*p == '\n') /* return-newline EOL */
1077 p++;
1078 continue;
1079 }
1080 default:
1081 {
1082 p++;
1083 *q++=(*p);
1084 }
1085 }
1086 continue;
1087 }
1088 case '&':
1089 {
1090 if (LocaleNCompare("&lt;",p,4) == 0)
1091 {
1092 *q++='<';
1093 p+=(ptrdiff_t) 3;
1094 }
1095 else
1096 if (LocaleNCompare("&gt;",p,4) == 0)
1097 {
1098 *q++='>';
1099 p+=(ptrdiff_t) 3;
1100 }
1101 else
1102 if (LocaleNCompare("&amp;",p,5) == 0)
1103 {
1104 *q++='&';
1105 p+=(ptrdiff_t) 4;
1106 }
1107 else
1108 *q++=(*p);
1109 continue;
1110 }
1111 case '%':
1112 break; /* continue to next set of handlers */
1113 default:
1114 {
1115 *q++=(*p); /* any thing else is 'as normal' */
1116 continue;
1117 }
1118 }
1119 p++; /* advance beyond the percent */
1120 /*
1121 Doubled Percent - or percent at end of string.
1122 */
1123 if ((*p == '\0') || (*p == '\'') || (*p == '"'))
1124 p--;
1125 if (*p == '%')
1126 {
1127 *q++='%';
1128 continue;
1129 }
1130 /*
1131 Single letter escapes %c.
1132 */
1133 if (number != MagickFalse)
1134 {
1135 /*
1136 But only if not preceded by a number!
1137 */
1138 *q++='%'; /* do NOT substitute the percent */
1139 p--; /* back up one */
1140 continue;
1141 }
1142 string=GetMagickPropertyLetter(image_info,image,*p,exception);
1143 if (string != (char *) NULL)
1144 {
1145 AppendString2Text(string);
1146 string=DestroyString(string);
1147 continue;
1148 }
1149 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1150 "UnknownImageProperty","\"%%%c\"",*p);
1151 }
1152 *q='\0';
1153 return(interpret_text);
1154}
1155
1156MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
1157 const char *decode,const char *encode,ExceptionInfo *exception)
1158{
1159 char
1160 *command,
1161 **commands;
1162
1163 const DelegateInfo
1164 *delegate_info;
1165
1166 ssize_t
1167 i;
1168
1169 assert(image_info != (ImageInfo *) NULL);
1170 assert(image_info->signature == MagickCoreSignature);
1171 assert(image != (Image *) NULL);
1172 assert(image->signature == MagickCoreSignature);
1173 if (IsEventLogging() != MagickFalse)
1174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1175 delegate_info=GetDelegateInfo(decode,encode,exception);
1176 if (delegate_info == (const DelegateInfo *) NULL)
1177 {
1178 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1179 "NoTagFound","`%s'",decode ? decode : encode);
1180 return((char *) NULL);
1181 }
1182 commands=StringToList(delegate_info->commands);
1183 if (commands == (char **) NULL)
1184 {
1185 (void) ThrowMagickException(exception,GetMagickModule(),
1186 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
1187 encode);
1188 return((char *) NULL);
1189 }
1190 command=InterpretDelegateProperties((ImageInfo *) image_info,image,
1191 commands[0],exception);
1192 if (command == (char *) NULL)
1193 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1194 "MemoryAllocationFailed","`%s'",commands[0]);
1195 /*
1196 Relinquish resources.
1197 */
1198 for (i=0; commands[i] != (char *) NULL; i++)
1199 commands[i]=DestroyString(commands[i]);
1200 commands=(char **) RelinquishMagickMemory(commands);
1201 return(command);
1202}
1203
1204/*
1205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206% %
1207% %
1208% %
1209% G e t D e l e g a t e C o m m a n d s %
1210% %
1211% %
1212% %
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214%
1215% GetDelegateCommands() returns the commands associated with a delegate.
1216%
1217% The format of the GetDelegateCommands method is:
1218%
1219% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1220%
1221% A description of each parameter follows:
1222%
1223% o delegate_info: The delegate info.
1224%
1225*/
1226MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
1227{
1228 if (IsEventLogging() != MagickFalse)
1229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1230 assert(delegate_info != (DelegateInfo *) NULL);
1231 assert(delegate_info->signature == MagickCoreSignature);
1232 return(delegate_info->commands);
1233}
1234
1235/*
1236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237% %
1238% %
1239% %
1240% G e t D e l e g a t e I n f o %
1241% %
1242% %
1243% %
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245%
1246% GetDelegateInfo() returns any delegates associated with the specified tag.
1247%
1248% The format of the GetDelegateInfo method is:
1249%
1250% const DelegateInfo *GetDelegateInfo(const char *decode,
1251% const char *encode,ExceptionInfo *exception)
1252%
1253% A description of each parameter follows:
1254%
1255% o decode: Specifies the decode delegate we are searching for as a
1256% character string.
1257%
1258% o encode: Specifies the encode delegate we are searching for as a
1259% character string.
1260%
1261% o exception: return any errors or warnings in this structure.
1262%
1263*/
1264MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
1265 const char *encode,ExceptionInfo *exception)
1266{
1267 const DelegateInfo
1268 *delegate_info;
1269
1270 ElementInfo
1271 *p;
1272
1273 assert(exception != (ExceptionInfo *) NULL);
1274 if (IsDelegateCacheInstantiated(exception) == MagickFalse)
1275 return((const DelegateInfo *) NULL);
1276 /*
1277 Search for named delegate.
1278 */
1279 delegate_info=(const DelegateInfo *) NULL;
1280 LockSemaphoreInfo(delegate_semaphore);
1281 p=GetHeadElementInLinkedList(delegate_cache);
1282 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
1283 {
1284 UnlockSemaphoreInfo(delegate_semaphore);
1285 if (p != (ElementInfo *) NULL)
1286 delegate_info=(const DelegateInfo* ) p->value;
1287 return(delegate_info);
1288 }
1289 while (p != (ElementInfo *) NULL)
1290 {
1291 delegate_info=(const DelegateInfo* ) p->value;
1292 if (delegate_info->mode > 0)
1293 {
1294 if (LocaleCompare(delegate_info->decode,decode) == 0)
1295 break;
1296 p=p->next;
1297 continue;
1298 }
1299 if (delegate_info->mode < 0)
1300 {
1301 if (LocaleCompare(delegate_info->encode,encode) == 0)
1302 break;
1303 p=p->next;
1304 continue;
1305 }
1306 if (LocaleCompare(decode,delegate_info->decode) == 0)
1307 if (LocaleCompare(encode,delegate_info->encode) == 0)
1308 break;
1309 if (LocaleCompare(decode,"*") == 0)
1310 if (LocaleCompare(encode,delegate_info->encode) == 0)
1311 break;
1312 if (LocaleCompare(decode,delegate_info->decode) == 0)
1313 if (LocaleCompare(encode,"*") == 0)
1314 break;
1315 p=p->next;
1316 }
1317 if (p == (ElementInfo *) NULL)
1318 delegate_info=(const DelegateInfo *) NULL;
1319 else
1320 SetHeadElementInLinkedList(delegate_cache,p);
1321 UnlockSemaphoreInfo(delegate_semaphore);
1322 return(delegate_info);
1323}
1324
1325/*
1326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327% %
1328% %
1329% %
1330% G e t D e l e g a t e I n f o L i s t %
1331% %
1332% %
1333% %
1334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335%
1336% GetDelegateInfoList() returns any delegates that match the specified pattern.
1337%
1338% The delegate of the GetDelegateInfoList function is:
1339%
1340% const DelegateInfo **GetDelegateInfoList(const char *pattern,
1341% size_t *number_delegates,ExceptionInfo *exception)
1342%
1343% A description of each parameter follows:
1344%
1345% o pattern: Specifies a pointer to a text string containing a pattern.
1346%
1347% o number_delegates: This integer returns the number of delegates in the
1348% list.
1349%
1350% o exception: return any errors or warnings in this structure.
1351%
1352*/
1353
1354#if defined(__cplusplus) || defined(c_plusplus)
1355extern "C" {
1356#endif
1357
1358static int DelegateInfoCompare(const void *x,const void *y)
1359{
1360 const DelegateInfo
1361 **p,
1362 **q;
1363
1364 int
1365 cmp;
1366
1367 p=(const DelegateInfo **) x,
1368 q=(const DelegateInfo **) y;
1369 cmp=LocaleCompare((*p)->path,(*q)->path);
1370 if (cmp == 0)
1371 {
1372 if ((*p)->decode == (char *) NULL)
1373 if (((*p)->encode != (char *) NULL) &&
1374 ((*q)->encode != (char *) NULL))
1375 return(strcmp((*p)->encode,(*q)->encode));
1376 if (((*p)->decode != (char *) NULL) &&
1377 ((*q)->decode != (char *) NULL))
1378 return(strcmp((*p)->decode,(*q)->decode));
1379 }
1380 return(cmp);
1381}
1382
1383#if defined(__cplusplus) || defined(c_plusplus)
1384}
1385#endif
1386
1387MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
1388 size_t *number_delegates,ExceptionInfo *exception)
1389{
1390 const DelegateInfo
1391 **delegates;
1392
1393 ElementInfo
1394 *p;
1395
1396 ssize_t
1397 i;
1398
1399 assert(number_delegates != (size_t *) NULL);
1400 assert(pattern != (char *) NULL);
1401 if (IsEventLogging() != MagickFalse)
1402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1403 *number_delegates=0;
1404 if (IsDelegateCacheInstantiated(exception) == MagickFalse)
1405 return((const DelegateInfo **) NULL);
1406 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
1407 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
1408 if (delegates == (const DelegateInfo **) NULL)
1409 return((const DelegateInfo **) NULL);
1410 LockSemaphoreInfo(delegate_semaphore);
1411 p=GetHeadElementInLinkedList(delegate_cache);
1412 for (i=0; p != (ElementInfo *) NULL; )
1413 {
1414 const DelegateInfo
1415 *delegate_info;
1416
1417 delegate_info=(const DelegateInfo *) p->value;
1418 if( (delegate_info->stealth == MagickFalse) &&
1419 (GlobExpression(delegate_info->decode,pattern,MagickFalse) != MagickFalse ||
1420 GlobExpression(delegate_info->encode,pattern,MagickFalse) != MagickFalse))
1421 delegates[i++]=delegate_info;
1422 p=p->next;
1423 }
1424 UnlockSemaphoreInfo(delegate_semaphore);
1425 if (i == 0)
1426 delegates=(const DelegateInfo **) RelinquishMagickMemory((void*) delegates);
1427 else
1428 {
1429 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
1430 delegates[i]=(DelegateInfo *) NULL;
1431 }
1432 *number_delegates=(size_t) i;
1433 return(delegates);
1434}
1435
1436/*
1437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438% %
1439% %
1440% %
1441% G e t D e l e g a t e L i s t %
1442% %
1443% %
1444% %
1445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446%
1447% GetDelegateList() returns any image format delegates that match the
1448% specified pattern.
1449%
1450% The format of the GetDelegateList function is:
1451%
1452% char **GetDelegateList(const char *pattern,
1453% size_t *number_delegates,ExceptionInfo *exception)
1454%
1455% A description of each parameter follows:
1456%
1457% o pattern: Specifies a pointer to a text string containing a pattern.
1458%
1459% o number_delegates: This integer returns the number of delegates
1460% in the list.
1461%
1462% o exception: return any errors or warnings in this structure.
1463%
1464*/
1465
1466#if defined(__cplusplus) || defined(c_plusplus)
1467extern "C" {
1468#endif
1469
1470static int DelegateCompare(const void *x,const void *y)
1471{
1472 const char
1473 **p,
1474 **q;
1475
1476 p=(const char **) x;
1477 q=(const char **) y;
1478 return(LocaleCompare(*p,*q));
1479}
1480
1481#if defined(__cplusplus) || defined(c_plusplus)
1482}
1483#endif
1484
1485MagickExport char **GetDelegateList(const char *pattern,
1486 size_t *number_delegates,ExceptionInfo *exception)
1487{
1488 char
1489 **delegates;
1490
1491 ElementInfo
1492 *p;
1493
1494 ssize_t
1495 i;
1496
1497 assert(pattern != (char *) NULL);
1498 if (IsEventLogging() != MagickFalse)
1499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1500 assert(number_delegates != (size_t *) NULL);
1501 *number_delegates=0;
1502 if (IsDelegateCacheInstantiated(exception) == MagickFalse)
1503 return((char **) NULL);
1504 delegates=(char **) AcquireQuantumMemory((size_t)
1505 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
1506 if (delegates == (char **) NULL)
1507 return((char **) NULL);
1508 LockSemaphoreInfo(delegate_semaphore);
1509 p=GetHeadElementInLinkedList(delegate_cache);
1510 for (i=0; p != (ElementInfo *) NULL; )
1511 {
1512 const DelegateInfo
1513 *delegate_info;
1514
1515 delegate_info=(const DelegateInfo *) p->value;
1516 if ((delegate_info->stealth == MagickFalse) &&
1517 (GlobExpression(delegate_info->decode,pattern,MagickFalse) != MagickFalse))
1518 delegates[i++]=ConstantString(delegate_info->decode);
1519 if ((delegate_info->stealth == MagickFalse) &&
1520 (GlobExpression(delegate_info->encode,pattern,MagickFalse) != MagickFalse))
1521 delegates[i++]=ConstantString(delegate_info->encode);
1522 p=p->next;
1523 }
1524 UnlockSemaphoreInfo(delegate_semaphore);
1525 if (i == 0)
1526 delegates=(char **) RelinquishMagickMemory(delegates);
1527 else
1528 {
1529 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
1530 delegates[i]=(char *) NULL;
1531 }
1532 *number_delegates=(size_t) i;
1533 return(delegates);
1534}
1535
1536/*
1537%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538% %
1539% %
1540% %
1541% G e t D e l e g a t e M o d e %
1542% %
1543% %
1544% %
1545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546%
1547% GetDelegateMode() returns the mode of the delegate.
1548%
1549% The format of the GetDelegateMode method is:
1550%
1551% ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
1552%
1553% A description of each parameter follows:
1554%
1555% o delegate_info: The delegate info.
1556%
1557*/
1558MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
1559{
1560 if (IsEventLogging() != MagickFalse)
1561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1562 assert(delegate_info != (DelegateInfo *) NULL);
1563 assert(delegate_info->signature == MagickCoreSignature);
1564 return(delegate_info->mode);
1565}
1566
1567/*
1568%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569% %
1570% %
1571% %
1572+ G e t D e l e g a t e T h r e a d S u p p o r t %
1573% %
1574% %
1575% %
1576%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577%
1578% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
1579% threads.
1580%
1581% The format of the GetDelegateThreadSupport method is:
1582%
1583% MagickBooleanType GetDelegateThreadSupport(
1584% const DelegateInfo *delegate_info)
1585%
1586% A description of each parameter follows:
1587%
1588% o delegate_info: The delegate info.
1589%
1590*/
1591MagickExport MagickBooleanType GetDelegateThreadSupport(
1592 const DelegateInfo *delegate_info)
1593{
1594 assert(delegate_info != (DelegateInfo *) NULL);
1595 assert(delegate_info->signature == MagickCoreSignature);
1596 if (IsEventLogging() != MagickFalse)
1597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1598 return(delegate_info->thread_support);
1599}
1600
1601/*
1602%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603% %
1604% %
1605% %
1606+ 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 %
1607% %
1608% %
1609% %
1610%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611%
1612% IsDelegateCacheInstantiated() determines if the delegate cache is
1613% instantiated. If not, it instantiates the cache and returns it.
1614%
1615% The format of the IsDelegateInstantiated method is:
1616%
1617% MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
1618%
1619% A description of each parameter follows.
1620%
1621% o exception: return any errors or warnings in this structure.
1622%
1623*/
1624static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
1625{
1626 if (delegate_cache == (LinkedListInfo *) NULL)
1627 {
1628 if (delegate_semaphore == (SemaphoreInfo *) NULL)
1629 ActivateSemaphoreInfo(&delegate_semaphore);
1630 LockSemaphoreInfo(delegate_semaphore);
1631 if (delegate_cache == (LinkedListInfo *) NULL)
1632 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
1633 UnlockSemaphoreInfo(delegate_semaphore);
1634 }
1635 return(delegate_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
1636}
1637
1638/*
1639%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1640% %
1641% %
1642% %
1643% I n v o k e D e l e g a t e %
1644% %
1645% %
1646% %
1647%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1648%
1649% InvokeDelegate replaces any embedded formatting characters with the
1650% appropriate image attribute and executes the resulting command. MagickFalse
1651% is returned if the commands execute with success otherwise MagickTrue.
1652%
1653% The format of the InvokeDelegate method is:
1654%
1655% MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
1656% const char *decode,const char *encode,ExceptionInfo *exception)
1657%
1658% A description of each parameter follows:
1659%
1660% o image_info: the imageInfo.
1661%
1662% o image: the image.
1663%
1664% o exception: return any errors or warnings in this structure.
1665%
1666*/
1667
1668static MagickBooleanType CopyDelegateFile(const char *source,
1669 const char *destination,const MagickBooleanType overwrite,
1670 ExceptionInfo *exception)
1671{
1672 int
1673 destination_file = -1,
1674 source_file = -1;
1675
1676 MagickBooleanType
1677 status = MagickTrue;
1678
1679 ssize_t
1680 count = 0;
1681
1682 size_t
1683 quantum = 0;
1684
1685 struct stat
1686 attributes = { 0 };
1687
1688 unsigned char
1689 *buffer;
1690
1691 /*
1692 Copy source file to destination.
1693 */
1694 assert(source != (const char *) NULL);
1695 assert(destination != (char *) NULL);
1696 if ((overwrite == MagickFalse) &&
1697 (GetPathAttributes(destination,&attributes) != MagickFalse))
1698 return(MagickTrue);
1699 if (IsPathAuthorized(WritePolicyRights,destination) == MagickFalse)
1700 ThrowPolicyException(source,MagickFalse);
1701 destination_file=open_utf8(destination,O_WRONLY | O_CLOEXEC | O_BINARY | O_CREAT |
1702 O_NOFOLLOW,S_MODE);
1703 if (destination_file == -1)
1704 return(MagickFalse);
1705 if (IsPathAuthorized(WritePolicyRights,destination) == MagickFalse)
1706 ThrowPolicyException(destination,MagickFalse);
1707 if (IsPathAuthorized(ReadPolicyRights,source) == MagickFalse)
1708 {
1709 (void) close_utf8(destination_file);
1710 ThrowPolicyException(source,MagickFalse);
1711 }
1712 source_file=open_utf8(source,O_RDONLY | O_CLOEXEC | O_BINARY,0);
1713 if (source_file == -1)
1714 {
1715 (void) close_utf8(destination_file);
1716 return(MagickFalse);
1717 }
1718 quantum=(size_t) MagickMaxBufferExtent;
1719 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
1720 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1721 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1722 if (buffer == (unsigned char *) NULL)
1723 {
1724 (void) close_utf8(source_file);
1725 (void) close_utf8(destination_file);
1726 return(MagickFalse);
1727 }
1728 while ((count=read(source_file,buffer,quantum)) > 0)
1729 {
1730 ssize_t written = write(destination_file,buffer,(size_t) count);
1731 if (written != count)
1732 {
1733 status=MagickFalse;
1734 break;
1735 }
1736 }
1737 if (count < 0)
1738 status=MagickFalse;
1739 (void) close_utf8(destination_file);
1740 (void) close_utf8(source_file);
1741 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1742 return(status);
1743}
1744
1745MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1746 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1747{
1748 char
1749 *command,
1750 **commands,
1751 input_filename[MagickPathExtent],
1752 output_filename[MagickPathExtent];
1753
1754 const DelegateInfo
1755 *delegate_info;
1756
1757 MagickBooleanType
1758 status,
1759 temporary;
1760
1761 PolicyRights
1762 rights;
1763
1764 ssize_t
1765 i;
1766
1767 /*
1768 Get delegate.
1769 */
1770 assert(image_info != (ImageInfo *) NULL);
1771 assert(image_info->signature == MagickCoreSignature);
1772 assert(image != (Image *) NULL);
1773 assert(image->signature == MagickCoreSignature);
1774 if (IsEventLogging() != MagickFalse)
1775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1776 rights=ExecutePolicyRights;
1777 if ((decode != (const char *) NULL) &&
1778 (IsRightsAuthorized(DelegatePolicyDomain,rights,decode) == MagickFalse))
1779 ThrowPolicyException(decode,MagickFalse);
1780 if ((encode != (const char *) NULL) &&
1781 (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse))
1782 ThrowPolicyException(encode,MagickFalse);
1783 temporary=(*image->filename == '\0') ? MagickTrue : MagickFalse;
1784 if ((temporary != MagickFalse) && (AcquireUniqueFilename(image->filename) ==
1785 MagickFalse))
1786 {
1787 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1788 image->filename);
1789 return(MagickFalse);
1790 }
1791 delegate_info=GetDelegateInfo(decode,encode,exception);
1792 if (delegate_info == (DelegateInfo *) NULL)
1793 {
1794 if (temporary != MagickFalse)
1795 (void) RelinquishUniqueFileResource(image->filename);
1796 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1797 "NoTagFound","`%s'",decode ? decode : encode);
1798 return(MagickFalse);
1799 }
1800 if (*image_info->filename == '\0')
1801 {
1802 if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
1803 {
1804 if (temporary != MagickFalse)
1805 (void) RelinquishUniqueFileResource(image->filename);
1806 ThrowFileException(exception,FileOpenError,
1807 "UnableToCreateTemporaryFile",image_info->filename);
1808 return(MagickFalse);
1809 }
1810 image_info->temporary=MagickTrue;
1811 }
1812 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
1813 (delegate_info->encode != (char *) NULL)) ||
1814 ((encode != (const char *) NULL) &&
1815 (delegate_info->decode != (char *) NULL))))
1816 {
1817 char
1818 *magick;
1819
1820 ImageInfo
1821 *clone_info;
1822
1823 Image
1824 *p;
1825
1826 /*
1827 Delegate requires a particular image format.
1828 */
1829 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1830 {
1831 ThrowFileException(exception,FileOpenError,
1832 "UnableToCreateTemporaryFile",image_info->unique);
1833 return(MagickFalse);
1834 }
1835 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
1836 delegate_info->encode : delegate_info->decode,exception);
1837 if (magick == (char *) NULL)
1838 {
1839 (void) RelinquishUniqueFileResource(image_info->unique);
1840 if (temporary != MagickFalse)
1841 (void) RelinquishUniqueFileResource(image->filename);
1842 (void) ThrowMagickException(exception,GetMagickModule(),
1843 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1844 return(MagickFalse);
1845 }
1846 LocaleUpper(magick);
1847 clone_info=CloneImageInfo(image_info);
1848 (void) CopyMagickString((char *) clone_info->magick,magick,
1849 MagickPathExtent);
1850 if (LocaleCompare(magick,"NULL") != 0)
1851 (void) CopyMagickString(image->magick,magick,MagickPathExtent);
1852 magick=DestroyString(magick);
1853 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:",
1854 delegate_info->decode);
1855 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1856 exception);
1857 (void) CopyMagickString(clone_info->filename,image_info->filename,
1858 MagickPathExtent);
1859 (void) CopyMagickString(image_info->filename,image->filename,
1860 MagickPathExtent);
1861 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1862 {
1863 (void) FormatLocaleString(p->filename,MagickPathExtent,"%s:%s",
1864 delegate_info->decode,clone_info->filename);
1865 status=WriteImage(clone_info,p,exception);
1866 if (status == MagickFalse)
1867 {
1868 (void) RelinquishUniqueFileResource(image_info->unique);
1869 if (temporary != MagickFalse)
1870 (void) RelinquishUniqueFileResource(image->filename);
1871 clone_info=DestroyImageInfo(clone_info);
1872 (void) ThrowMagickException(exception,GetMagickModule(),
1873 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1874 return(MagickFalse);
1875 }
1876 if (clone_info->adjoin != MagickFalse)
1877 break;
1878 }
1879 (void) RelinquishUniqueFileResource(image_info->unique);
1880 clone_info=DestroyImageInfo(clone_info);
1881 }
1882 /*
1883 Invoke delegate.
1884 */
1885 commands=StringToList(delegate_info->commands);
1886 if (commands == (char **) NULL)
1887 {
1888 if (temporary != MagickFalse)
1889 (void) RelinquishUniqueFileResource(image->filename);
1890 (void) ThrowMagickException(exception,GetMagickModule(),
1891 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1892 decode ? decode : encode);
1893 return(MagickFalse);
1894 }
1895 command=(char *) NULL;
1896 status=MagickTrue;
1897 (void) CopyMagickString(output_filename,image_info->filename,
1898 MagickPathExtent);
1899 (void) CopyMagickString(input_filename,image->filename,MagickPathExtent);
1900 for (i=0; commands[i] != (char *) NULL; i++)
1901 {
1902 (void) AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1903 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1904 {
1905 ThrowFileException(exception,FileOpenError,
1906 "UnableToCreateTemporaryFile",image_info->unique);
1907 break;
1908 }
1909 if (IsPathAccessible(input_filename) != MagickFalse)
1910 {
1911 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1912 if (status == MagickFalse)
1913 {
1914 ThrowFileException(exception,FileOpenError,
1915 "UnableToCreateTemporaryFile",input_filename);
1916 break;
1917 }
1918 }
1919 status=MagickTrue;
1920 command=InterpretDelegateProperties(image_info,image,commands[i],exception);
1921 if (command != (char *) NULL)
1922 {
1923 /*
1924 Execute delegate.
1925 */
1926 if (ExternalDelegateCommand(delegate_info->spawn,image_info->verbose,command,(char *) NULL,exception) != 0)
1927 status=MagickFalse;
1928 if (delegate_info->spawn != MagickFalse)
1929 {
1930 ssize_t
1931 count;
1932
1933 /*
1934 Wait for input file to 'disappear', or maximum 2 seconds.
1935 */
1936 count=20;
1937 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1938 (void) MagickDelay(100); /* sleep 0.1 seconds */
1939 }
1940 command=DestroyString(command);
1941 }
1942 if (IsPathAccessible(input_filename) != MagickFalse)
1943 {
1944 if (CopyDelegateFile(image->filename,input_filename,MagickFalse,exception) == MagickFalse)
1945 {
1946 (void) RelinquishUniqueFileResource(input_filename);
1947 status=MagickFalse;
1948 }
1949 }
1950 if ((strcmp(input_filename,output_filename) != 0) &&
1951 (CopyDelegateFile(image_info->filename,output_filename,MagickTrue,exception) == MagickFalse))
1952 {
1953 (void) RelinquishUniqueFileResource(output_filename);
1954 status=MagickFalse;
1955 }
1956 if (image_info->temporary != MagickFalse)
1957 (void) RelinquishUniqueFileResource(image_info->filename);
1958 (void) RelinquishUniqueFileResource(image_info->unique);
1959 (void) RelinquishUniqueFileResource(image_info->filename);
1960 (void) RelinquishUniqueFileResource(image->filename);
1961 if (status == MagickFalse)
1962 {
1963 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1964 "DelegateFailed","`%s'",commands[i]);
1965 break;
1966 }
1967 commands[i]=DestroyString(commands[i]);
1968 }
1969 (void) CopyMagickString(image_info->filename,output_filename,
1970 MagickPathExtent);
1971 (void) CopyMagickString(image->filename,input_filename,MagickPathExtent);
1972 /*
1973 Relinquish resources.
1974 */
1975 for ( ; commands[i] != (char *) NULL; i++)
1976 commands[i]=DestroyString(commands[i]);
1977 commands=(char **) RelinquishMagickMemory(commands);
1978 if (temporary != MagickFalse)
1979 (void) RelinquishUniqueFileResource(image->filename);
1980 return(status);
1981}
1982
1983/*
1984%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1985% %
1986% %
1987% %
1988% L i s t D e l e g a t e I n f o %
1989% %
1990% %
1991% %
1992%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1993%
1994% ListDelegateInfo() lists the image formats to a file.
1995%
1996% The format of the ListDelegateInfo method is:
1997%
1998% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1999%
2000% A description of each parameter follows.
2001%
2002% o file: An pointer to a FILE.
2003%
2004% o exception: return any errors or warnings in this structure.
2005%
2006*/
2007MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
2008 ExceptionInfo *exception)
2009{
2010 const DelegateInfo
2011 **delegate_info;
2012
2013 char
2014 **commands,
2015 delegate[MagickPathExtent];
2016
2017 const char
2018 *path;
2019
2020 ssize_t
2021 i;
2022
2023 size_t
2024 number_delegates;
2025
2026 ssize_t
2027 j;
2028
2029 if (file == (const FILE *) NULL)
2030 file=stdout;
2031 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
2032 if (delegate_info == (const DelegateInfo **) NULL)
2033 return(MagickFalse);
2034 path=(const char *) NULL;
2035 for (i=0; i < (ssize_t) number_delegates; i++)
2036 {
2037 if (delegate_info[i]->stealth != MagickFalse)
2038 continue;
2039 if ((path == (const char *) NULL) ||
2040 (LocaleCompare(path,delegate_info[i]->path) != 0))
2041 {
2042 if (delegate_info[i]->path != (char *) NULL)
2043 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
2044 (void) FormatLocaleFile(file,"Delegate Command\n");
2045 (void) FormatLocaleFile(file,
2046 "-------------------------------------------------"
2047 "------------------------------\n");
2048 }
2049 path=delegate_info[i]->path;
2050 *delegate='\0';
2051 if (delegate_info[i]->encode != (char *) NULL)
2052 (void) CopyMagickString(delegate,delegate_info[i]->encode,
2053 MagickPathExtent);
2054 (void) ConcatenateMagickString(delegate," ",MagickPathExtent);
2055 delegate[9]='\0';
2056 commands=StringToList(delegate_info[i]->commands);
2057 if (commands == (char **) NULL)
2058 continue;
2059 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
2060 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
2061 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
2062 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
2063 for (j=1; commands[j] != (char *) NULL; j++)
2064 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
2065 for (j=0; commands[j] != (char *) NULL; j++)
2066 commands[j]=DestroyString(commands[j]);
2067 commands=(char **) RelinquishMagickMemory(commands);
2068 }
2069 (void) fflush(file);
2070 delegate_info=(const DelegateInfo **)
2071 RelinquishMagickMemory((void *) delegate_info);
2072 return(MagickTrue);
2073}
2074
2075/*
2076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2077% %
2078% %
2079% %
2080+ L o a d D e l e g a t e C a c h e %
2081% %
2082% %
2083% %
2084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2085%
2086% LoadDelegateCache() loads the delegate configurations which provides a
2087% mapping between delegate attributes and a delegate name.
2088%
2089% The format of the LoadDelegateCache method is:
2090%
2091% MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
2092% const char *xml,const char *filename,const size_t depth,
2093% ExceptionInfo *exception)
2094%
2095% A description of each parameter follows:
2096%
2097% o xml: The delegate list in XML format.
2098%
2099% o filename: The delegate list filename.
2100%
2101% o depth: depth of <include /> statements.
2102%
2103% o exception: return any errors or warnings in this structure.
2104%
2105*/
2106static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
2107 const char *xml,const char *filename,const size_t depth,
2108 ExceptionInfo *exception)
2109{
2110 char
2111 keyword[MagickPathExtent],
2112 *token;
2113
2114 const char
2115 *q;
2116
2117 DelegateInfo
2118 *delegate_info;
2119
2120 MagickStatusType
2121 status;
2122
2123 size_t
2124 extent;
2125
2126 /*
2127 Load the delegate map file.
2128 */
2129 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
2130 "Loading delegate configuration file \"%s\" ...",filename);
2131 if (xml == (const char *) NULL)
2132 return(MagickFalse);
2133 status=MagickTrue;
2134 delegate_info=(DelegateInfo *) NULL;
2135 token=AcquireString(xml);
2136 extent=strlen(token)+MagickPathExtent;
2137 for (q=(const char *) xml; *q != '\0'; )
2138 {
2139 /*
2140 Interpret XML.
2141 */
2142 (void) GetNextToken(q,&q,extent,token);
2143 if (*token == '\0')
2144 break;
2145 (void) CopyMagickString(keyword,token,MagickPathExtent);
2146 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
2147 {
2148 int
2149 bracket_depth = 0,
2150 quote = 0;
2151
2152 /*
2153 DOCTYPE element.
2154 */
2155 for ( ; *q != '\0'; q++)
2156 {
2157 if (quote != 0)
2158 {
2159 if (*q == quote)
2160 quote=0;
2161 }
2162 else
2163 {
2164 if ((*q == '"') || (*q == '\''))
2165 quote=(*q);
2166 else
2167 if (*q == '[')
2168 bracket_depth++;
2169 else
2170 if (*q == ']')
2171 {
2172 if (bracket_depth > 0)
2173 bracket_depth--;
2174 }
2175 else
2176 if ((*q == '>') && (bracket_depth == 0))
2177 {
2178 q++; /* consume final '>' */
2179 break;
2180 }
2181 }
2182 }
2183 }
2184 if (LocaleNCompare(keyword,"<!--",4) == 0)
2185 {
2186 /*
2187 Comment element.
2188 */
2189 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
2190 (void) GetNextToken(q,&q,extent,token);
2191 continue;
2192 }
2193 if (LocaleCompare(keyword,"<include") == 0)
2194 {
2195 /*
2196 Include element.
2197 */
2198 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
2199 {
2200 (void) CopyMagickString(keyword,token,MagickPathExtent);
2201 (void) GetNextToken(q,&q,extent,token);
2202 if (*token != '=')
2203 continue;
2204 (void) GetNextToken(q,&q,extent,token);
2205 if (LocaleCompare(keyword,"file") == 0)
2206 {
2207 if (depth > MagickMaxRecursionDepth)
2208 (void) ThrowMagickException(exception,GetMagickModule(),
2209 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
2210 else
2211 {
2212 char
2213 path[MagickPathExtent],
2214 *file_xml;
2215
2216 GetPathComponent(filename,HeadPath,path);
2217 if (*path != '\0')
2218 (void) ConcatenateMagickString(path,DirectorySeparator,
2219 MagickPathExtent);
2220 if (*token == *DirectorySeparator)
2221 (void) CopyMagickString(path,token,MagickPathExtent);
2222 else
2223 (void) ConcatenateMagickString(path,token,MagickPathExtent);
2224 file_xml=FileToXML(path,~0UL);
2225 if (file_xml != (char *) NULL)
2226 {
2227 status&=(MagickStatusType) LoadDelegateCache(cache,
2228 file_xml,path,depth+1,exception);
2229 file_xml=DestroyString(file_xml);
2230 }
2231 }
2232 }
2233 }
2234 continue;
2235 }
2236 if (LocaleCompare(keyword,"<delegate") == 0)
2237 {
2238 /*
2239 Delegate element.
2240 */
2241 delegate_info=(DelegateInfo *) AcquireCriticalMemory(
2242 sizeof(*delegate_info));
2243 (void) memset(delegate_info,0,sizeof(*delegate_info));
2244 delegate_info->path=ConstantString(filename);
2245 delegate_info->thread_support=MagickTrue;
2246 delegate_info->signature=MagickCoreSignature;
2247 continue;
2248 }
2249 if (delegate_info == (DelegateInfo *) NULL)
2250 continue;
2251 if ((LocaleCompare(keyword,"/>") == 0) ||
2252 (LocaleCompare(keyword,"</policy>") == 0))
2253 {
2254 status=AppendValueToLinkedList(cache,delegate_info);
2255 if (status == MagickFalse)
2256 (void) ThrowMagickException(exception,GetMagickModule(),
2257 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2258 delegate_info->commands);
2259 delegate_info=(DelegateInfo *) NULL;
2260 continue;
2261 }
2262 (void) GetNextToken(q,(const char **) NULL,extent,token);
2263 if (*token != '=')
2264 continue;
2265 (void) GetNextToken(q,&q,extent,token);
2266 (void) GetNextToken(q,&q,extent,token);
2267 switch (*keyword)
2268 {
2269 case 'C':
2270 case 'c':
2271 {
2272 if (LocaleCompare((char *) keyword,"command") == 0)
2273 {
2274 char
2275 *commands;
2276
2277 commands=AcquireString(token);
2278#if defined(MAGICKCORE_WINDOWS_SUPPORT)
2279 if (strchr(commands,'@') != (char *) NULL)
2280 {
2281 char
2282 path[MagickPathExtent];
2283
2284 NTGhostscriptEXE(path,MagickPathExtent);
2285 (void) SubstituteString((char **) &commands,"@PSDelegate@",
2286 path);
2287 (void) SubstituteString((char **) &commands,"\\","/");
2288 }
2289#endif
2290 (void) SubstituteString((char **) &commands,"&quot;","\"");
2291 (void) SubstituteString((char **) &commands,"&apos;","'");
2292 (void) SubstituteString((char **) &commands,"&amp;","&");
2293 (void) SubstituteString((char **) &commands,"&gt;",">");
2294 (void) SubstituteString((char **) &commands,"&lt;","<");
2295 if (delegate_info->commands != (char *) NULL)
2296 delegate_info->commands=DestroyString(delegate_info->commands);
2297 delegate_info->commands=commands;
2298 break;
2299 }
2300 break;
2301 }
2302 case 'D':
2303 case 'd':
2304 {
2305 if (LocaleCompare((char *) keyword,"decode") == 0)
2306 {
2307 delegate_info->decode=ConstantString(token);
2308 delegate_info->mode=1;
2309 break;
2310 }
2311 break;
2312 }
2313 case 'E':
2314 case 'e':
2315 {
2316 if (LocaleCompare((char *) keyword,"encode") == 0)
2317 {
2318 delegate_info->encode=ConstantString(token);
2319 delegate_info->mode=(-1);
2320 break;
2321 }
2322 break;
2323 }
2324 case 'M':
2325 case 'm':
2326 {
2327 if (LocaleCompare((char *) keyword,"mode") == 0)
2328 {
2329 delegate_info->mode=1;
2330 if (LocaleCompare(token,"bi") == 0)
2331 delegate_info->mode=0;
2332 else
2333 if (LocaleCompare(token,"encode") == 0)
2334 delegate_info->mode=(-1);
2335 break;
2336 }
2337 break;
2338 }
2339 case 'S':
2340 case 's':
2341 {
2342 if (LocaleCompare((char *) keyword,"spawn") == 0)
2343 {
2344 delegate_info->spawn=IsStringTrue(token);
2345 break;
2346 }
2347 if (LocaleCompare((char *) keyword,"stealth") == 0)
2348 {
2349 delegate_info->stealth=IsStringTrue(token);
2350 break;
2351 }
2352 break;
2353 }
2354 case 'T':
2355 case 't':
2356 {
2357 if (LocaleCompare((char *) keyword,"thread-support") == 0)
2358 {
2359 delegate_info->thread_support=IsStringTrue(token);
2360 if (delegate_info->thread_support == MagickFalse)
2361 delegate_info->semaphore=AcquireSemaphoreInfo();
2362 break;
2363 }
2364 break;
2365 }
2366 default:
2367 break;
2368 }
2369 }
2370 token=(char *) RelinquishMagickMemory(token);
2371 return(status != 0 ? MagickTrue : MagickFalse);
2372}