blob: ccf57e1f9003367f6e0a7bb4e62d3f1c11762fe7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC OOO DDDD EEEEE RRRR %
7% C O O D D E R R %
8% C O O D D EEE RRRR %
9% C O O D D E R R %
10% CCCC OOO DDDD EEEEE R R %
11% %
12% %
13% MagickCore Image Coder Methods %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% May 2001 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/client.h"
45#include "MagickCore/coder.h"
cristy5ff4eaf2011-09-03 01:38:02 +000046#include "MagickCore/coder-private.h"
cristy4c08aed2011-07-01 19:47:50 +000047#include "MagickCore/configure.h"
48#include "MagickCore/draw.h"
49#include "MagickCore/exception.h"
50#include "MagickCore/exception-private.h"
51#include "MagickCore/hashmap.h"
52#include "MagickCore/log.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/option.h"
55#include "MagickCore/semaphore.h"
56#include "MagickCore/string_.h"
57#include "MagickCore/splay-tree.h"
58#include "MagickCore/token.h"
59#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000060#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/xml-tree.h"
cristy3291f512014-03-16 22:16:22 +000062#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Define declarations.
66*/
67#define MagickCoderFilename "coder.xml"
68
69/*
cristy54a531d2009-10-21 17:58:01 +000070 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000071*/
cristye3e77a12009-10-16 00:47:21 +000072typedef struct _CoderMapInfo
73{
74 const char
75 *magick,
76 *name;
77} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000078
79/*
80 Static declarations.
81*/
cristye3e77a12009-10-16 00:47:21 +000082static const CoderMapInfo
83 CoderMap[] =
84 {
85 { "3FR", "DNG" },
86 { "8BIM", "META" },
87 { "8BIMTEXT", "META" },
88 { "8BIMWTEXT", "META" },
89 { "AFM", "TTF" },
90 { "A", "RAW" },
91 { "AI", "PDF" },
92 { "APP1JPEG", "META" },
93 { "APP1", "META" },
94 { "ARW", "DNG" },
cristy0fc02592010-05-24 14:12:38 +000095 { "AVI", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000096 { "BIE", "JBIG" },
97 { "BMP2", "BMP" },
98 { "BMP3", "BMP" },
99 { "B", "RAW" },
100 { "BRF", "BRAILLE" },
cristy90dbac72010-08-22 15:08:40 +0000101 { "BGRA", "BGR" },
dirk2a2d3d62015-07-27 11:09:58 +0000102 { "BGRO", "BGR" },
cristye3e77a12009-10-16 00:47:21 +0000103 { "CMYKA", "CMYK" },
104 { "C", "RAW" },
105 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000106 { "CANVAS", "XC" },
dirk2a2d3d62015-07-27 11:09:58 +0000107 { "CMYKA", "CMYK" },
cristye3e77a12009-10-16 00:47:21 +0000108 { "CR2", "DNG" },
109 { "CRW", "DNG" },
110 { "CUR", "ICON" },
111 { "DCR", "DNG" },
112 { "DCX", "PCX" },
113 { "DFONT", "TTF" },
dirk2a2d3d62015-07-27 11:09:58 +0000114 { "DXT1", "DDS" },
115 { "DXT5", "DDS" },
cristye3e77a12009-10-16 00:47:21 +0000116 { "EPDF", "PDF" },
117 { "EPI", "PS" },
118 { "EPS2", "PS2" },
119 { "EPS3", "PS3" },
120 { "EPSF", "PS" },
121 { "EPSI", "PS" },
122 { "EPS", "PS" },
123 { "EPT2", "EPT" },
124 { "EPT3", "EPT" },
125 { "ERF", "DNG" },
126 { "EXIF", "META" },
127 { "FILE", "URL" },
128 { "FRACTAL", "PLASMA" },
129 { "FTP", "URL" },
130 { "FTS", "FITS" },
131 { "G3", "FAX" },
132 { "GIF87", "GIF" },
133 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000134 { "GRANITE", "MAGICK" },
135 { "GROUP4", "TIFF" },
cristy7f47ac62013-02-10 14:16:17 +0000136 { "GV", "DOT" },
dirk2a2d3d62015-07-27 11:09:58 +0000137 { "HTM", "HTML" },
138 { "ICB", "TGA" },
139 { "ICO", "ICON" },
cristya5338bb2014-11-05 18:25:16 +0000140 { "IIQ", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000141 { "K25", "DNG" },
142 { "KDC", "DNG" },
143 { "H", "MAGICK" },
144 { "HTM", "HTML" },
145 { "HTTP", "URL" },
dirk853b77b2013-09-21 19:04:42 +0000146 { "HTTPS", "URL" },
cristye3e77a12009-10-16 00:47:21 +0000147 { "ICB", "TGA" },
148 { "ICC", "META" },
149 { "ICM", "META" },
150 { "ICO", "ICON" },
151 { "IMPLICIT", "***" },
152 { "IPTC", "META" },
153 { "IPTCTEXT", "META" },
154 { "IPTCWTEXT", "META" },
155 { "ISOBRL", "BRAILLE" },
156 { "JBG", "JBIG" },
157 { "JNG", "PNG" },
158 { "JPC", "JP2" },
cristy2267fa42014-09-12 10:27:47 +0000159 { "JPT", "JP2" },
160 { "JPM", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000161 { "J2C", "JP2" },
cristy4faac682012-05-10 12:09:45 +0000162 { "J2K", "JP2" },
dirk2a2d3d62015-07-27 11:09:58 +0000163 { "JNG", "PNG" },
cristyb338b712014-11-27 14:06:34 +0000164 { "JPE", "JPEG" },
cristye3e77a12009-10-16 00:47:21 +0000165 { "JPG", "JPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000166 { "JPM", "JP2" },
cristya663a4f2015-01-23 12:11:57 +0000167 { "JPS", "JPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000168 { "JPT", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000169 { "JPX", "JP2" },
170 { "K", "RAW" },
dirk2a2d3d62015-07-27 11:09:58 +0000171 { "K25", "DNG" },
172 { "KDC", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000173 { "LOGO", "MAGICK" },
dirk2a2d3d62015-07-27 11:09:58 +0000174 { "M", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000175 { "M2V", "MPEG" },
176 { "M4V", "MPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000177 { "MEF", "DNG" },
cristya3d81422015-04-03 13:00:33 +0000178 { "MKV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000179 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000180 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000181 { "MP4", "MPEG" },
182 { "MPG", "MPEG" },
183 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000184 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000185 { "MRW", "DNG" },
186 { "MSVG", "SVG" },
187 { "NEF", "DNG" },
188 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000189 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000190 { "O", "RAW" },
191 { "ORF", "DNG" },
192 { "OTF", "TTF" },
193 { "P7", "PNM" },
194 { "PAL", "UYVY" },
195 { "PAM", "PNM" },
196 { "PBM", "PNM" },
197 { "PCDS", "PCD" },
dirk2a2d3d62015-07-27 11:09:58 +0000198 { "PCT", "PICT" },
cristye3e77a12009-10-16 00:47:21 +0000199 { "PDFA", "PDF" },
200 { "PEF", "DNG" },
201 { "PEF", "DNG" },
202 { "PFA", "TTF" },
203 { "PFB", "TTF" },
204 { "PFM", "PNM" },
205 { "PGM", "PNM" },
206 { "PGX", "JP2" },
207 { "PICON", "XPM" },
208 { "PJPEG", "JPEG" },
209 { "PM", "XPM" },
dirk2a2d3d62015-07-27 11:09:58 +0000210 { "PNG00", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000211 { "PNG24", "PNG" },
212 { "PNG32", "PNG" },
dirk2a2d3d62015-07-27 11:09:58 +0000213 { "PNG48", "PNG" },
214 { "PNG64", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000215 { "PNG8", "PNG" },
216 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000217 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000218 { "PTIF", "TIFF" },
dirk2a2d3d62015-07-27 11:09:58 +0000219 { "R", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000220 { "RADIAL-GRADIENT", "GRADIENT" },
221 { "RAF", "DNG" },
222 { "RAS", "SUN" },
cristy1b1f0722014-06-10 10:14:59 +0000223 { "RAW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000224 { "RGBA", "RGB" },
225 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000226 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000227 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000228 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000229 { "SHTML", "HTML" },
dirka1e4ab82014-10-07 05:06:49 +0000230 { "SIX", "SIXEL" },
cristye6ee7492013-03-16 15:04:42 +0000231 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000232 { "SR2", "DNG" },
233 { "SRF", "DNG" },
234 { "SVGZ", "SVG" },
235 { "TEXT", "TXT" },
236 { "TIFF64", "TIFF" },
237 { "TIF", "TIFF" },
238 { "TTC", "TTF" },
239 { "UBRL", "BRAILLE" },
240 { "VDA", "TGA" },
241 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000242 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000243#if defined(MAGICKCORE_WINGDI32_DELEGATE)
244 { "WMF", "EMF" },
245#endif
cristye3e77a12009-10-16 00:47:21 +0000246 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000247 { "WMZ", "WMF" },
248 { "X3f", "DNG" },
249 { "XMP", "META" },
250 { "XTRNARRAY", "XTRN" },
251 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000252 { "XTRNFILE", "XTRN" },
253 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000254 { "XV", "VIFF" },
255 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000256 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000257 };
258
cristy3ed852e2009-09-05 21:47:34 +0000259static SemaphoreInfo
260 *coder_semaphore = (SemaphoreInfo *) NULL;
261
262static SplayTreeInfo
cristy86e5ac92014-03-16 19:27:39 +0000263 *coder_cache = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000264
265/*
266 Forward declarations.
267*/
268static MagickBooleanType
cristy904e5912014-03-15 19:53:14 +0000269 IsCoderTreeInstantiated(ExceptionInfo *),
cristy86e5ac92014-03-16 19:27:39 +0000270 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
271 ExceptionInfo *);
272
273/*
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275% %
276% %
277% %
cristy6c6322e2014-09-09 22:39:21 +0000278+ A c q u i r e C o d e r C a c h e %
cristy86e5ac92014-03-16 19:27:39 +0000279% %
280% %
281% %
282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283%
cristy6c6322e2014-09-09 22:39:21 +0000284% AcquireCoderCache() caches one or more coder configurations which provides a
285% mapping between coder attributes and a coder name.
cristy86e5ac92014-03-16 19:27:39 +0000286%
287% The format of the AcquireCoderCache coder is:
288%
289% SplayTreeInfo *AcquireCoderCache(const char *filename,
290% ExceptionInfo *exception)
291%
292% A description of each parameter follows:
293%
294% o filename: the font file name.
295%
296% o exception: return any errors or warnings in this structure.
297%
298*/
299
300static void *DestroyCoderNode(void *coder_info)
301{
302 register CoderInfo
303 *p;
304
305 p=(CoderInfo *) coder_info;
306 if (p->exempt == MagickFalse)
307 {
308 if (p->path != (char *) NULL)
309 p->path=DestroyString(p->path);
310 if (p->name != (char *) NULL)
311 p->name=DestroyString(p->name);
312 if (p->magick != (char *) NULL)
313 p->magick=DestroyString(p->magick);
314 }
315 return(RelinquishMagickMemory(p));
316}
317
318static SplayTreeInfo *AcquireCoderCache(const char *filename,
319 ExceptionInfo *exception)
320{
321 const StringInfo
322 *option;
323
324 LinkedListInfo
325 *options;
326
327 MagickStatusType
328 status;
329
330 register ssize_t
331 i;
332
333 SplayTreeInfo
334 *coder_cache;
335
336 /*
337 Load external coder map.
338 */
339 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
340 DestroyCoderNode);
341 if (coder_cache == (SplayTreeInfo *) NULL)
342 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
343 status=MagickTrue;
344 options=GetConfigureOptions(filename,exception);
345 option=(const StringInfo *) GetNextValueInLinkedList(options);
346 while (option != (const StringInfo *) NULL)
347 {
cristycd2cd182014-03-18 12:10:55 +0000348 status&=LoadCoderCache(coder_cache,(const char *)
349 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy86e5ac92014-03-16 19:27:39 +0000350 option=(const StringInfo *) GetNextValueInLinkedList(options);
351 }
352 options=DestroyConfigureOptions(options);
353 /*
354 Load built-in coder map.
355 */
356 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
357 {
358 CoderInfo
359 *coder_info;
360
361 register const CoderMapInfo
362 *p;
363
364 p=CoderMap+i;
365 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
366 if (coder_info == (CoderInfo *) NULL)
367 {
368 (void) ThrowMagickException(exception,GetMagickModule(),
369 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
370 continue;
371 }
372 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
373 coder_info->path=(char *) "[built-in]";
374 coder_info->magick=(char *) p->magick;
375 coder_info->name=(char *) p->name;
376 coder_info->exempt=MagickTrue;
cristye1c94d92015-06-28 12:16:33 +0000377 coder_info->signature=MagickCoreSignature;
cristy86e5ac92014-03-16 19:27:39 +0000378 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
379 coder_info);
380 if (status == MagickFalse)
381 (void) ThrowMagickException(exception,GetMagickModule(),
382 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
383 }
384 return(coder_cache);
385}
cristy3ed852e2009-09-05 21:47:34 +0000386
387/*
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389% %
390% %
391% %
cristyf34a1452009-10-24 22:29:27 +0000392+ C o d e r C o m p o n e n t G e n e s i s %
cristy3ed852e2009-09-05 21:47:34 +0000393% %
394% %
395% %
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397%
cristyf34a1452009-10-24 22:29:27 +0000398% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000399%
cristyf34a1452009-10-24 22:29:27 +0000400% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000401%
cristyf34a1452009-10-24 22:29:27 +0000402% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000403%
404*/
cristy5ff4eaf2011-09-03 01:38:02 +0000405MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000406{
cristy7c977062014-04-04 14:05:53 +0000407 if (coder_semaphore == (SemaphoreInfo *) NULL)
408 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000409 return(MagickTrue);
410}
411
412/*
413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414% %
415% %
416% %
417+ C o d e r C o m p o n e n t T e r m i n u s %
418% %
419% %
420% %
421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422%
423% CoderComponentTerminus() destroys the coder component.
424%
425% The format of the CoderComponentTerminus method is:
426%
427% CoderComponentTerminus(void)
428%
429*/
cristy5ff4eaf2011-09-03 01:38:02 +0000430MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000431{
cristy18b17442009-10-25 18:36:48 +0000432 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000433 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000434 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000435 if (coder_cache != (SplayTreeInfo *) NULL)
436 coder_cache=DestroySplayTree(coder_cache);
cristyf84a1932010-01-03 18:00:18 +0000437 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000438 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000439}
440
441/*
442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443% %
444% %
445% %
446+ G e t C o d e r I n f o %
447% %
448% %
449% %
450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451%
452% GetCoderInfo searches the coder list for the specified name and if found
453% returns attributes for that coder.
454%
455% The format of the GetCoderInfo method is:
456%
457% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
458%
459% A description of each parameter follows:
460%
461% o name: the coder name.
462%
463% o exception: return any errors or warnings in this structure.
464%
465*/
466MagickExport const CoderInfo *GetCoderInfo(const char *name,
467 ExceptionInfo *exception)
468{
cristy49d4d222014-03-16 00:37:58 +0000469 const CoderInfo
470 *coder_info;
471
cristy3ed852e2009-09-05 21:47:34 +0000472 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000473 if (IsCoderTreeInstantiated(exception) == MagickFalse)
474 return((const CoderInfo *) NULL);
cristy49d4d222014-03-16 00:37:58 +0000475 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000476 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
477 {
cristy86e5ac92014-03-16 19:27:39 +0000478 ResetSplayTreeIterator(coder_cache);
479 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy49d4d222014-03-16 00:37:58 +0000480 UnlockSemaphoreInfo(coder_semaphore);
481 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000482 }
cristy86e5ac92014-03-16 19:27:39 +0000483 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
cristy49d4d222014-03-16 00:37:58 +0000484 UnlockSemaphoreInfo(coder_semaphore);
485 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000486}
487
488/*
489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490% %
491% %
492% %
493% G e t C o d e r I n f o L i s t %
494% %
495% %
496% %
497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498%
499% GetCoderInfoList() returns any coder_map that match the specified pattern.
500% The format of the GetCoderInfoList function is:
501%
502% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000503% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000504%
505% A description of each parameter follows:
506%
507% o pattern: Specifies a pointer to a text string containing a pattern.
508%
509% o number_coders: This integer returns the number of coders in the list.
510%
511% o exception: return any errors or warnings in this structure.
512%
513*/
514
515static int CoderInfoCompare(const void *x,const void *y)
516{
517 const CoderInfo
518 **p,
519 **q;
520
521 p=(const CoderInfo **) x,
522 q=(const CoderInfo **) y;
523 if (LocaleCompare((*p)->path,(*q)->path) == 0)
524 return(LocaleCompare((*p)->name,(*q)->name));
525 return(LocaleCompare((*p)->path,(*q)->path));
526}
527
528MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000529 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000530{
531 const CoderInfo
532 **coder_map;
533
534 register const CoderInfo
535 *p;
536
cristybb503372010-05-27 20:51:26 +0000537 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000538 i;
539
540 /*
541 Allocate coder list.
542 */
543 assert(pattern != (char *) NULL);
544 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000545 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000546 *number_coders=0;
547 p=GetCoderInfo("*",exception);
548 if (p == (const CoderInfo *) NULL)
549 return((const CoderInfo **) NULL);
550 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000551 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000552 if (coder_map == (const CoderInfo **) NULL)
553 return((const CoderInfo **) NULL);
554 /*
555 Generate coder list.
556 */
cristyf84a1932010-01-03 18:00:18 +0000557 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000558 ResetSplayTreeIterator(coder_cache);
559 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000560 for (i=0; p != (const CoderInfo *) NULL; )
561 {
562 if ((p->stealth == MagickFalse) &&
563 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
564 coder_map[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000565 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000566 }
cristyf84a1932010-01-03 18:00:18 +0000567 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000568 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
569 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000570 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000571 return(coder_map);
572}
573
574/*
575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576% %
577% %
578% %
579% G e t C o d e r L i s t %
580% %
581% %
582% %
583%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584%
585% GetCoderList() returns any coder_map that match the specified pattern.
586%
587% The format of the GetCoderList function is:
588%
cristybb503372010-05-27 20:51:26 +0000589% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000590% ExceptionInfo *exception)
591%
592% A description of each parameter follows:
593%
594% o pattern: Specifies a pointer to a text string containing a pattern.
595%
596% o number_coders: This integer returns the number of coders in the list.
597%
598% o exception: return any errors or warnings in this structure.
599%
600*/
601
602static int CoderCompare(const void *x,const void *y)
603{
604 register const char
605 **p,
606 **q;
607
608 p=(const char **) x;
609 q=(const char **) y;
610 return(LocaleCompare(*p,*q));
611}
612
613MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000614 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000615{
616 char
617 **coder_map;
618
619 register const CoderInfo
620 *p;
621
cristybb503372010-05-27 20:51:26 +0000622 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000623 i;
624
625 /*
626 Allocate coder list.
627 */
628 assert(pattern != (char *) NULL);
629 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000630 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000631 *number_coders=0;
632 p=GetCoderInfo("*",exception);
633 if (p == (const CoderInfo *) NULL)
634 return((char **) NULL);
635 coder_map=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000636 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000637 if (coder_map == (char **) NULL)
638 return((char **) NULL);
639 /*
640 Generate coder list.
641 */
cristyf84a1932010-01-03 18:00:18 +0000642 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000643 ResetSplayTreeIterator(coder_cache);
644 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000645 for (i=0; p != (const CoderInfo *) NULL; )
646 {
647 if ((p->stealth == MagickFalse) &&
648 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
649 coder_map[i++]=ConstantString(p->name);
cristy86e5ac92014-03-16 19:27:39 +0000650 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000651 }
cristyf84a1932010-01-03 18:00:18 +0000652 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000653 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
654 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000655 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000656 return(coder_map);
657}
658
659/*
660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661% %
662% %
663% %
cristy904e5912014-03-15 19:53:14 +0000664+ I s C o d e r T r e e I n s t a n t i a t e d %
cristy3ed852e2009-09-05 21:47:34 +0000665% %
666% %
667% %
668%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669%
cristycd2cd182014-03-18 12:10:55 +0000670% IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
671% not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000672%
cristy904e5912014-03-15 19:53:14 +0000673% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000674%
cristy904e5912014-03-15 19:53:14 +0000675% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000676%
677% A description of each parameter follows.
678%
679% o exception: return any errors or warnings in this structure.
680%
681*/
cristy904e5912014-03-15 19:53:14 +0000682static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000683{
cristy86e5ac92014-03-16 19:27:39 +0000684 if (coder_cache == (SplayTreeInfo *) NULL)
685 {
686 if (coder_semaphore == (SemaphoreInfo *) NULL)
687 ActivateSemaphoreInfo(&coder_semaphore);
688 LockSemaphoreInfo(coder_semaphore);
689 if (coder_cache == (SplayTreeInfo *) NULL)
cristycd2cd182014-03-18 12:10:55 +0000690 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000691 UnlockSemaphoreInfo(coder_semaphore);
692 }
693 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000694}
695
696/*
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698% %
699% %
700% %
701% L i s t C o d e r I n f o %
702% %
703% %
704% %
705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706%
707% ListCoderInfo() lists the coder info to a file.
708%
709% The format of the ListCoderInfo coder is:
710%
711% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
712%
713% A description of each parameter follows.
714%
715% o file: An pointer to a FILE.
716%
717% o exception: return any errors or warnings in this structure.
718%
719*/
720MagickExport MagickBooleanType ListCoderInfo(FILE *file,
721 ExceptionInfo *exception)
722{
723 const char
724 *path;
725
726 const CoderInfo
727 **coder_info;
728
cristybb503372010-05-27 20:51:26 +0000729 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000730 i;
731
cristybb503372010-05-27 20:51:26 +0000732 size_t
cristy3ed852e2009-09-05 21:47:34 +0000733 number_coders;
734
cristy9d314ff2011-03-09 01:30:28 +0000735 ssize_t
736 j;
737
cristy3ed852e2009-09-05 21:47:34 +0000738 if (file == (const FILE *) NULL)
739 file=stdout;
740 coder_info=GetCoderInfoList("*",&number_coders,exception);
741 if (coder_info == (const CoderInfo **) NULL)
742 return(MagickFalse);
743 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000744 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000745 {
746 if (coder_info[i]->stealth != MagickFalse)
747 continue;
748 if ((path == (const char *) NULL) ||
749 (LocaleCompare(path,coder_info[i]->path) != 0))
750 {
751 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000752 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
753 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000754 (void) FormatLocaleFile(file,
755 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000756 "------------------------------\n");
757 }
758 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000759 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000760 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000761 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000762 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000763 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
764 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000765 }
766 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
767 (void) fflush(file);
768 return(MagickTrue);
769}
770
771/*
772%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773% %
774% %
775% %
776+ L o a d C o d e r L i s t %
777% %
778% %
779% %
780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781%
cristy86e5ac92014-03-16 19:27:39 +0000782% LoadCoderCache() loads the coder configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +0000783% mapping between coder attributes and a coder name.
784%
cristy86e5ac92014-03-16 19:27:39 +0000785% The format of the LoadCoderCache coder is:
cristy3ed852e2009-09-05 21:47:34 +0000786%
cristy86e5ac92014-03-16 19:27:39 +0000787% MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
788% const char *xml,const char *filename,const size_t depth,
789% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000790%
791% A description of each parameter follows:
792%
793% o xml: The coder list in XML format.
794%
795% o filename: The coder list filename.
796%
797% o depth: depth of <include /> statements.
798%
799% o exception: return any errors or warnings in this structure.
800%
801*/
cristy86e5ac92014-03-16 19:27:39 +0000802static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
803 const char *xml,const char *filename,const size_t depth,
804 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000805{
806 char
cristy151b66d2015-04-15 10:50:31 +0000807 keyword[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000808 *token;
809
810 const char
811 *q;
812
813 CoderInfo
814 *coder_info;
815
cristy759ba912014-06-26 11:59:43 +0000816 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +0000817 status;
818
819 /*
820 Load the coder map file.
821 */
822 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
823 "Loading coder configuration file \"%s\" ...",filename);
824 if (xml == (const char *) NULL)
825 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000826 status=MagickTrue;
827 coder_info=(CoderInfo *) NULL;
828 token=AcquireString(xml);
829 for (q=(char *) xml; *q != '\0'; )
830 {
831 /*
832 Interpret XML.
833 */
834 GetMagickToken(q,&q,token);
835 if (*token == '\0')
836 break;
cristy151b66d2015-04-15 10:50:31 +0000837 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000838 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
839 {
840 /*
841 Doctype element.
842 */
843 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
844 GetMagickToken(q,&q,token);
845 continue;
846 }
847 if (LocaleNCompare(keyword,"<!--",4) == 0)
848 {
849 /*
850 Comment element.
851 */
852 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
853 GetMagickToken(q,&q,token);
854 continue;
855 }
856 if (LocaleCompare(keyword,"<include") == 0)
857 {
858 /*
859 Include element.
860 */
861 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
862 {
cristy151b66d2015-04-15 10:50:31 +0000863 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000864 GetMagickToken(q,&q,token);
865 if (*token != '=')
866 continue;
867 GetMagickToken(q,&q,token);
868 if (LocaleCompare(keyword,"file") == 0)
869 {
870 if (depth > 200)
871 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000872 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000873 else
874 {
875 char
cristy151b66d2015-04-15 10:50:31 +0000876 path[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000877 *xml;
878
879 GetPathComponent(filename,HeadPath,path);
880 if (*path != '\0')
881 (void) ConcatenateMagickString(path,DirectorySeparator,
cristy151b66d2015-04-15 10:50:31 +0000882 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000883 if (*token == *DirectorySeparator)
cristy151b66d2015-04-15 10:50:31 +0000884 (void) CopyMagickString(path,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000885 else
cristy151b66d2015-04-15 10:50:31 +0000886 (void) ConcatenateMagickString(path,token,MagickPathExtent);
cristy3291f512014-03-16 22:16:22 +0000887 xml=FileToXML(path,~0UL);
cristy3ed852e2009-09-05 21:47:34 +0000888 if (xml != (char *) NULL)
889 {
cristy86e5ac92014-03-16 19:27:39 +0000890 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
891 exception);
cristy3ed852e2009-09-05 21:47:34 +0000892 xml=(char *) RelinquishMagickMemory(xml);
893 }
894 }
895 }
896 }
897 continue;
898 }
899 if (LocaleCompare(keyword,"<coder") == 0)
900 {
901 /*
902 Coder element.
903 */
cristy73bd4a52010-10-05 11:24:23 +0000904 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000905 if (coder_info == (CoderInfo *) NULL)
906 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
907 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
908 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000909 coder_info->exempt=MagickFalse;
cristye1c94d92015-06-28 12:16:33 +0000910 coder_info->signature=MagickCoreSignature;
cristy3ed852e2009-09-05 21:47:34 +0000911 continue;
912 }
913 if (coder_info == (CoderInfo *) NULL)
914 continue;
915 if (LocaleCompare(keyword,"/>") == 0)
916 {
cristy86e5ac92014-03-16 19:27:39 +0000917 status=AddValueToSplayTree(coder_cache,ConstantString(
cristy3ed852e2009-09-05 21:47:34 +0000918 coder_info->magick),coder_info);
919 if (status == MagickFalse)
920 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000921 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000922 coder_info->magick);
923 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000924 continue;
cristy3ed852e2009-09-05 21:47:34 +0000925 }
926 GetMagickToken(q,(const char **) NULL,token);
927 if (*token != '=')
928 continue;
929 GetMagickToken(q,&q,token);
930 GetMagickToken(q,&q,token);
931 switch (*keyword)
932 {
933 case 'M':
934 case 'm':
935 {
936 if (LocaleCompare((char *) keyword,"magick") == 0)
937 {
938 coder_info->magick=ConstantString(token);
939 break;
940 }
941 break;
942 }
943 case 'N':
944 case 'n':
945 {
946 if (LocaleCompare((char *) keyword,"name") == 0)
947 {
948 coder_info->name=ConstantString(token);
949 break;
950 }
951 break;
952 }
953 case 'S':
954 case 's':
955 {
956 if (LocaleCompare((char *) keyword,"stealth") == 0)
957 {
anthony6f201312012-03-30 04:08:15 +0000958 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000959 break;
960 }
961 break;
962 }
963 default:
964 break;
965 }
966 }
967 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +0000968 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000969}