blob: c674c563db896ec9a4e304e97dc7c6b030e4b455 [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" },
cristye3e77a12009-10-16 00:47:21 +0000102 { "CMYKA", "CMYK" },
103 { "C", "RAW" },
104 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000105 { "CANVAS", "XC" },
cristye3e77a12009-10-16 00:47:21 +0000106 { "CR2", "DNG" },
107 { "CRW", "DNG" },
108 { "CUR", "ICON" },
109 { "DCR", "DNG" },
110 { "DCX", "PCX" },
111 { "DFONT", "TTF" },
112 { "EPDF", "PDF" },
113 { "EPI", "PS" },
114 { "EPS2", "PS2" },
115 { "EPS3", "PS3" },
116 { "EPSF", "PS" },
117 { "EPSI", "PS" },
118 { "EPS", "PS" },
119 { "EPT2", "EPT" },
120 { "EPT3", "EPT" },
121 { "ERF", "DNG" },
122 { "EXIF", "META" },
123 { "FILE", "URL" },
124 { "FRACTAL", "PLASMA" },
125 { "FTP", "URL" },
126 { "FTS", "FITS" },
127 { "G3", "FAX" },
128 { "GIF87", "GIF" },
129 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000130 { "GRANITE", "MAGICK" },
131 { "GROUP4", "TIFF" },
cristy7f47ac62013-02-10 14:16:17 +0000132 { "GV", "DOT" },
cristya5338bb2014-11-05 18:25:16 +0000133 { "IIQ", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000134 { "K25", "DNG" },
135 { "KDC", "DNG" },
136 { "H", "MAGICK" },
137 { "HTM", "HTML" },
138 { "HTTP", "URL" },
dirk853b77b2013-09-21 19:04:42 +0000139 { "HTTPS", "URL" },
cristye3e77a12009-10-16 00:47:21 +0000140 { "ICB", "TGA" },
141 { "ICC", "META" },
142 { "ICM", "META" },
143 { "ICO", "ICON" },
144 { "IMPLICIT", "***" },
145 { "IPTC", "META" },
146 { "IPTCTEXT", "META" },
147 { "IPTCWTEXT", "META" },
148 { "ISOBRL", "BRAILLE" },
149 { "JBG", "JBIG" },
150 { "JNG", "PNG" },
151 { "JPC", "JP2" },
cristy2267fa42014-09-12 10:27:47 +0000152 { "JPT", "JP2" },
153 { "JPM", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000154 { "J2C", "JP2" },
cristy4faac682012-05-10 12:09:45 +0000155 { "J2K", "JP2" },
cristyb338b712014-11-27 14:06:34 +0000156 { "JPE", "JPEG" },
cristye3e77a12009-10-16 00:47:21 +0000157 { "JPG", "JPEG" },
cristya663a4f2015-01-23 12:11:57 +0000158 { "JPS", "JPEG" },
cristye3e77a12009-10-16 00:47:21 +0000159 { "JPX", "JP2" },
160 { "K", "RAW" },
161 { "LOGO", "MAGICK" },
162 { "M2V", "MPEG" },
163 { "M4V", "MPEG" },
164 { "M", "RAW" },
cristya3d81422015-04-03 13:00:33 +0000165 { "MKV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000166 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000167 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000168 { "MP4", "MPEG" },
169 { "MPG", "MPEG" },
170 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000171 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000172 { "MRW", "DNG" },
173 { "MSVG", "SVG" },
174 { "NEF", "DNG" },
175 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000176 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000177 { "O", "RAW" },
178 { "ORF", "DNG" },
179 { "OTF", "TTF" },
180 { "P7", "PNM" },
181 { "PAL", "UYVY" },
182 { "PAM", "PNM" },
183 { "PBM", "PNM" },
184 { "PCDS", "PCD" },
185 { "PDFA", "PDF" },
186 { "PEF", "DNG" },
187 { "PEF", "DNG" },
188 { "PFA", "TTF" },
189 { "PFB", "TTF" },
190 { "PFM", "PNM" },
191 { "PGM", "PNM" },
192 { "PGX", "JP2" },
193 { "PICON", "XPM" },
194 { "PJPEG", "JPEG" },
195 { "PM", "XPM" },
196 { "PNG24", "PNG" },
197 { "PNG32", "PNG" },
198 { "PNG8", "PNG" },
199 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000200 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000201 { "PTIF", "TIFF" },
202 { "RADIAL-GRADIENT", "GRADIENT" },
203 { "RAF", "DNG" },
204 { "RAS", "SUN" },
cristy1b1f0722014-06-10 10:14:59 +0000205 { "RAW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000206 { "RGBA", "RGB" },
207 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000208 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000209 { "R", "RAW" },
210 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000211 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000212 { "SHTML", "HTML" },
dirka1e4ab82014-10-07 05:06:49 +0000213 { "SIX", "SIXEL" },
cristye6ee7492013-03-16 15:04:42 +0000214 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000215 { "SR2", "DNG" },
216 { "SRF", "DNG" },
217 { "SVGZ", "SVG" },
218 { "TEXT", "TXT" },
219 { "TIFF64", "TIFF" },
220 { "TIF", "TIFF" },
221 { "TTC", "TTF" },
222 { "UBRL", "BRAILLE" },
223 { "VDA", "TGA" },
224 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000225 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000226#if defined(MAGICKCORE_WINGDI32_DELEGATE)
227 { "WMF", "EMF" },
228#endif
cristye3e77a12009-10-16 00:47:21 +0000229 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000230 { "WMZ", "WMF" },
231 { "X3f", "DNG" },
232 { "XMP", "META" },
233 { "XTRNARRAY", "XTRN" },
234 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000235 { "XTRNFILE", "XTRN" },
236 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000237 { "XV", "VIFF" },
238 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000239 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000240 };
241
cristy3ed852e2009-09-05 21:47:34 +0000242static SemaphoreInfo
243 *coder_semaphore = (SemaphoreInfo *) NULL;
244
245static SplayTreeInfo
cristy86e5ac92014-03-16 19:27:39 +0000246 *coder_cache = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000247
248/*
249 Forward declarations.
250*/
251static MagickBooleanType
cristy904e5912014-03-15 19:53:14 +0000252 IsCoderTreeInstantiated(ExceptionInfo *),
cristy86e5ac92014-03-16 19:27:39 +0000253 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
254 ExceptionInfo *);
255
256/*
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258% %
259% %
260% %
cristy6c6322e2014-09-09 22:39:21 +0000261+ A c q u i r e C o d e r C a c h e %
cristy86e5ac92014-03-16 19:27:39 +0000262% %
263% %
264% %
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266%
cristy6c6322e2014-09-09 22:39:21 +0000267% AcquireCoderCache() caches one or more coder configurations which provides a
268% mapping between coder attributes and a coder name.
cristy86e5ac92014-03-16 19:27:39 +0000269%
270% The format of the AcquireCoderCache coder is:
271%
272% SplayTreeInfo *AcquireCoderCache(const char *filename,
273% ExceptionInfo *exception)
274%
275% A description of each parameter follows:
276%
277% o filename: the font file name.
278%
279% o exception: return any errors or warnings in this structure.
280%
281*/
282
283static void *DestroyCoderNode(void *coder_info)
284{
285 register CoderInfo
286 *p;
287
288 p=(CoderInfo *) coder_info;
289 if (p->exempt == MagickFalse)
290 {
291 if (p->path != (char *) NULL)
292 p->path=DestroyString(p->path);
293 if (p->name != (char *) NULL)
294 p->name=DestroyString(p->name);
295 if (p->magick != (char *) NULL)
296 p->magick=DestroyString(p->magick);
297 }
298 return(RelinquishMagickMemory(p));
299}
300
301static SplayTreeInfo *AcquireCoderCache(const char *filename,
302 ExceptionInfo *exception)
303{
304 const StringInfo
305 *option;
306
307 LinkedListInfo
308 *options;
309
310 MagickStatusType
311 status;
312
313 register ssize_t
314 i;
315
316 SplayTreeInfo
317 *coder_cache;
318
319 /*
320 Load external coder map.
321 */
322 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
323 DestroyCoderNode);
324 if (coder_cache == (SplayTreeInfo *) NULL)
325 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
326 status=MagickTrue;
327 options=GetConfigureOptions(filename,exception);
328 option=(const StringInfo *) GetNextValueInLinkedList(options);
329 while (option != (const StringInfo *) NULL)
330 {
cristycd2cd182014-03-18 12:10:55 +0000331 status&=LoadCoderCache(coder_cache,(const char *)
332 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy86e5ac92014-03-16 19:27:39 +0000333 option=(const StringInfo *) GetNextValueInLinkedList(options);
334 }
335 options=DestroyConfigureOptions(options);
336 /*
337 Load built-in coder map.
338 */
339 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
340 {
341 CoderInfo
342 *coder_info;
343
344 register const CoderMapInfo
345 *p;
346
347 p=CoderMap+i;
348 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
349 if (coder_info == (CoderInfo *) NULL)
350 {
351 (void) ThrowMagickException(exception,GetMagickModule(),
352 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
353 continue;
354 }
355 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
356 coder_info->path=(char *) "[built-in]";
357 coder_info->magick=(char *) p->magick;
358 coder_info->name=(char *) p->name;
359 coder_info->exempt=MagickTrue;
360 coder_info->signature=MagickSignature;
361 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
362 coder_info);
363 if (status == MagickFalse)
364 (void) ThrowMagickException(exception,GetMagickModule(),
365 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
366 }
367 return(coder_cache);
368}
cristy3ed852e2009-09-05 21:47:34 +0000369
370/*
371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372% %
373% %
374% %
cristyf34a1452009-10-24 22:29:27 +0000375+ 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 +0000376% %
377% %
378% %
379%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380%
cristyf34a1452009-10-24 22:29:27 +0000381% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000382%
cristyf34a1452009-10-24 22:29:27 +0000383% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000384%
cristyf34a1452009-10-24 22:29:27 +0000385% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000386%
387*/
cristy5ff4eaf2011-09-03 01:38:02 +0000388MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000389{
cristy7c977062014-04-04 14:05:53 +0000390 if (coder_semaphore == (SemaphoreInfo *) NULL)
391 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000392 return(MagickTrue);
393}
394
395/*
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397% %
398% %
399% %
400+ C o d e r C o m p o n e n t T e r m i n u s %
401% %
402% %
403% %
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405%
406% CoderComponentTerminus() destroys the coder component.
407%
408% The format of the CoderComponentTerminus method is:
409%
410% CoderComponentTerminus(void)
411%
412*/
cristy5ff4eaf2011-09-03 01:38:02 +0000413MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000414{
cristy18b17442009-10-25 18:36:48 +0000415 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000416 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000417 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000418 if (coder_cache != (SplayTreeInfo *) NULL)
419 coder_cache=DestroySplayTree(coder_cache);
cristyf84a1932010-01-03 18:00:18 +0000420 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000421 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000422}
423
424/*
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426% %
427% %
428% %
429+ G e t C o d e r I n f o %
430% %
431% %
432% %
433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434%
435% GetCoderInfo searches the coder list for the specified name and if found
436% returns attributes for that coder.
437%
438% The format of the GetCoderInfo method is:
439%
440% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
441%
442% A description of each parameter follows:
443%
444% o name: the coder name.
445%
446% o exception: return any errors or warnings in this structure.
447%
448*/
449MagickExport const CoderInfo *GetCoderInfo(const char *name,
450 ExceptionInfo *exception)
451{
cristy49d4d222014-03-16 00:37:58 +0000452 const CoderInfo
453 *coder_info;
454
cristy3ed852e2009-09-05 21:47:34 +0000455 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000456 if (IsCoderTreeInstantiated(exception) == MagickFalse)
457 return((const CoderInfo *) NULL);
cristy49d4d222014-03-16 00:37:58 +0000458 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000459 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
460 {
cristy86e5ac92014-03-16 19:27:39 +0000461 ResetSplayTreeIterator(coder_cache);
462 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy49d4d222014-03-16 00:37:58 +0000463 UnlockSemaphoreInfo(coder_semaphore);
464 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000465 }
cristy86e5ac92014-03-16 19:27:39 +0000466 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
cristy49d4d222014-03-16 00:37:58 +0000467 UnlockSemaphoreInfo(coder_semaphore);
468 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000469}
470
471/*
472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473% %
474% %
475% %
476% G e t C o d e r I n f o L i s t %
477% %
478% %
479% %
480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481%
482% GetCoderInfoList() returns any coder_map that match the specified pattern.
483% The format of the GetCoderInfoList function is:
484%
485% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000486% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000487%
488% A description of each parameter follows:
489%
490% o pattern: Specifies a pointer to a text string containing a pattern.
491%
492% o number_coders: This integer returns the number of coders in the list.
493%
494% o exception: return any errors or warnings in this structure.
495%
496*/
497
498static int CoderInfoCompare(const void *x,const void *y)
499{
500 const CoderInfo
501 **p,
502 **q;
503
504 p=(const CoderInfo **) x,
505 q=(const CoderInfo **) y;
506 if (LocaleCompare((*p)->path,(*q)->path) == 0)
507 return(LocaleCompare((*p)->name,(*q)->name));
508 return(LocaleCompare((*p)->path,(*q)->path));
509}
510
511MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000512 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000513{
514 const CoderInfo
515 **coder_map;
516
517 register const CoderInfo
518 *p;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000521 i;
522
523 /*
524 Allocate coder list.
525 */
526 assert(pattern != (char *) NULL);
527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000528 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000529 *number_coders=0;
530 p=GetCoderInfo("*",exception);
531 if (p == (const CoderInfo *) NULL)
532 return((const CoderInfo **) NULL);
533 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000534 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000535 if (coder_map == (const CoderInfo **) NULL)
536 return((const CoderInfo **) NULL);
537 /*
538 Generate coder list.
539 */
cristyf84a1932010-01-03 18:00:18 +0000540 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000541 ResetSplayTreeIterator(coder_cache);
542 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000543 for (i=0; p != (const CoderInfo *) NULL; )
544 {
545 if ((p->stealth == MagickFalse) &&
546 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
547 coder_map[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000548 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000549 }
cristyf84a1932010-01-03 18:00:18 +0000550 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000551 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
552 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000553 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000554 return(coder_map);
555}
556
557/*
558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559% %
560% %
561% %
562% G e t C o d e r L i s t %
563% %
564% %
565% %
566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567%
568% GetCoderList() returns any coder_map that match the specified pattern.
569%
570% The format of the GetCoderList function is:
571%
cristybb503372010-05-27 20:51:26 +0000572% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000573% ExceptionInfo *exception)
574%
575% A description of each parameter follows:
576%
577% o pattern: Specifies a pointer to a text string containing a pattern.
578%
579% o number_coders: This integer returns the number of coders in the list.
580%
581% o exception: return any errors or warnings in this structure.
582%
583*/
584
585static int CoderCompare(const void *x,const void *y)
586{
587 register const char
588 **p,
589 **q;
590
591 p=(const char **) x;
592 q=(const char **) y;
593 return(LocaleCompare(*p,*q));
594}
595
596MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000597 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000598{
599 char
600 **coder_map;
601
602 register const CoderInfo
603 *p;
604
cristybb503372010-05-27 20:51:26 +0000605 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000606 i;
607
608 /*
609 Allocate coder list.
610 */
611 assert(pattern != (char *) NULL);
612 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000613 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000614 *number_coders=0;
615 p=GetCoderInfo("*",exception);
616 if (p == (const CoderInfo *) NULL)
617 return((char **) NULL);
618 coder_map=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000619 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000620 if (coder_map == (char **) NULL)
621 return((char **) NULL);
622 /*
623 Generate coder list.
624 */
cristyf84a1932010-01-03 18:00:18 +0000625 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000626 ResetSplayTreeIterator(coder_cache);
627 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000628 for (i=0; p != (const CoderInfo *) NULL; )
629 {
630 if ((p->stealth == MagickFalse) &&
631 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
632 coder_map[i++]=ConstantString(p->name);
cristy86e5ac92014-03-16 19:27:39 +0000633 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000634 }
cristyf84a1932010-01-03 18:00:18 +0000635 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000636 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
637 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000638 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000639 return(coder_map);
640}
641
642/*
643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644% %
645% %
646% %
cristy904e5912014-03-15 19:53:14 +0000647+ 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 +0000648% %
649% %
650% %
651%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652%
cristycd2cd182014-03-18 12:10:55 +0000653% IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
654% not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000655%
cristy904e5912014-03-15 19:53:14 +0000656% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000657%
cristy904e5912014-03-15 19:53:14 +0000658% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000659%
660% A description of each parameter follows.
661%
662% o exception: return any errors or warnings in this structure.
663%
664*/
cristy904e5912014-03-15 19:53:14 +0000665static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000666{
cristy86e5ac92014-03-16 19:27:39 +0000667 if (coder_cache == (SplayTreeInfo *) NULL)
668 {
669 if (coder_semaphore == (SemaphoreInfo *) NULL)
670 ActivateSemaphoreInfo(&coder_semaphore);
671 LockSemaphoreInfo(coder_semaphore);
672 if (coder_cache == (SplayTreeInfo *) NULL)
cristycd2cd182014-03-18 12:10:55 +0000673 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000674 UnlockSemaphoreInfo(coder_semaphore);
675 }
676 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000677}
678
679/*
680%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681% %
682% %
683% %
684% L i s t C o d e r I n f o %
685% %
686% %
687% %
688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689%
690% ListCoderInfo() lists the coder info to a file.
691%
692% The format of the ListCoderInfo coder is:
693%
694% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
695%
696% A description of each parameter follows.
697%
698% o file: An pointer to a FILE.
699%
700% o exception: return any errors or warnings in this structure.
701%
702*/
703MagickExport MagickBooleanType ListCoderInfo(FILE *file,
704 ExceptionInfo *exception)
705{
706 const char
707 *path;
708
709 const CoderInfo
710 **coder_info;
711
cristybb503372010-05-27 20:51:26 +0000712 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000713 i;
714
cristybb503372010-05-27 20:51:26 +0000715 size_t
cristy3ed852e2009-09-05 21:47:34 +0000716 number_coders;
717
cristy9d314ff2011-03-09 01:30:28 +0000718 ssize_t
719 j;
720
cristy3ed852e2009-09-05 21:47:34 +0000721 if (file == (const FILE *) NULL)
722 file=stdout;
723 coder_info=GetCoderInfoList("*",&number_coders,exception);
724 if (coder_info == (const CoderInfo **) NULL)
725 return(MagickFalse);
726 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000727 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000728 {
729 if (coder_info[i]->stealth != MagickFalse)
730 continue;
731 if ((path == (const char *) NULL) ||
732 (LocaleCompare(path,coder_info[i]->path) != 0))
733 {
734 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000735 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
736 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000737 (void) FormatLocaleFile(file,
738 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000739 "------------------------------\n");
740 }
741 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000742 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000743 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000744 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000745 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000746 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
747 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000748 }
749 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
750 (void) fflush(file);
751 return(MagickTrue);
752}
753
754/*
755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756% %
757% %
758% %
759+ L o a d C o d e r L i s t %
760% %
761% %
762% %
763%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764%
cristy86e5ac92014-03-16 19:27:39 +0000765% LoadCoderCache() loads the coder configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +0000766% mapping between coder attributes and a coder name.
767%
cristy86e5ac92014-03-16 19:27:39 +0000768% The format of the LoadCoderCache coder is:
cristy3ed852e2009-09-05 21:47:34 +0000769%
cristy86e5ac92014-03-16 19:27:39 +0000770% MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
771% const char *xml,const char *filename,const size_t depth,
772% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000773%
774% A description of each parameter follows:
775%
776% o xml: The coder list in XML format.
777%
778% o filename: The coder list filename.
779%
780% o depth: depth of <include /> statements.
781%
782% o exception: return any errors or warnings in this structure.
783%
784*/
cristy86e5ac92014-03-16 19:27:39 +0000785static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
786 const char *xml,const char *filename,const size_t depth,
787 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000788{
789 char
790 keyword[MaxTextExtent],
791 *token;
792
793 const char
794 *q;
795
796 CoderInfo
797 *coder_info;
798
cristy759ba912014-06-26 11:59:43 +0000799 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +0000800 status;
801
802 /*
803 Load the coder map file.
804 */
805 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
806 "Loading coder configuration file \"%s\" ...",filename);
807 if (xml == (const char *) NULL)
808 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000809 status=MagickTrue;
810 coder_info=(CoderInfo *) NULL;
811 token=AcquireString(xml);
812 for (q=(char *) xml; *q != '\0'; )
813 {
814 /*
815 Interpret XML.
816 */
817 GetMagickToken(q,&q,token);
818 if (*token == '\0')
819 break;
820 (void) CopyMagickString(keyword,token,MaxTextExtent);
821 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
822 {
823 /*
824 Doctype element.
825 */
826 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
827 GetMagickToken(q,&q,token);
828 continue;
829 }
830 if (LocaleNCompare(keyword,"<!--",4) == 0)
831 {
832 /*
833 Comment element.
834 */
835 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
836 GetMagickToken(q,&q,token);
837 continue;
838 }
839 if (LocaleCompare(keyword,"<include") == 0)
840 {
841 /*
842 Include element.
843 */
844 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
845 {
846 (void) CopyMagickString(keyword,token,MaxTextExtent);
847 GetMagickToken(q,&q,token);
848 if (*token != '=')
849 continue;
850 GetMagickToken(q,&q,token);
851 if (LocaleCompare(keyword,"file") == 0)
852 {
853 if (depth > 200)
854 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000855 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000856 else
857 {
858 char
859 path[MaxTextExtent],
860 *xml;
861
862 GetPathComponent(filename,HeadPath,path);
863 if (*path != '\0')
864 (void) ConcatenateMagickString(path,DirectorySeparator,
865 MaxTextExtent);
866 if (*token == *DirectorySeparator)
867 (void) CopyMagickString(path,token,MaxTextExtent);
868 else
869 (void) ConcatenateMagickString(path,token,MaxTextExtent);
cristy3291f512014-03-16 22:16:22 +0000870 xml=FileToXML(path,~0UL);
cristy3ed852e2009-09-05 21:47:34 +0000871 if (xml != (char *) NULL)
872 {
cristy86e5ac92014-03-16 19:27:39 +0000873 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
874 exception);
cristy3ed852e2009-09-05 21:47:34 +0000875 xml=(char *) RelinquishMagickMemory(xml);
876 }
877 }
878 }
879 }
880 continue;
881 }
882 if (LocaleCompare(keyword,"<coder") == 0)
883 {
884 /*
885 Coder element.
886 */
cristy73bd4a52010-10-05 11:24:23 +0000887 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000888 if (coder_info == (CoderInfo *) NULL)
889 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
890 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
891 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000892 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000893 coder_info->signature=MagickSignature;
894 continue;
895 }
896 if (coder_info == (CoderInfo *) NULL)
897 continue;
898 if (LocaleCompare(keyword,"/>") == 0)
899 {
cristy86e5ac92014-03-16 19:27:39 +0000900 status=AddValueToSplayTree(coder_cache,ConstantString(
cristy3ed852e2009-09-05 21:47:34 +0000901 coder_info->magick),coder_info);
902 if (status == MagickFalse)
903 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000904 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000905 coder_info->magick);
906 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000907 continue;
cristy3ed852e2009-09-05 21:47:34 +0000908 }
909 GetMagickToken(q,(const char **) NULL,token);
910 if (*token != '=')
911 continue;
912 GetMagickToken(q,&q,token);
913 GetMagickToken(q,&q,token);
914 switch (*keyword)
915 {
916 case 'M':
917 case 'm':
918 {
919 if (LocaleCompare((char *) keyword,"magick") == 0)
920 {
921 coder_info->magick=ConstantString(token);
922 break;
923 }
924 break;
925 }
926 case 'N':
927 case 'n':
928 {
929 if (LocaleCompare((char *) keyword,"name") == 0)
930 {
931 coder_info->name=ConstantString(token);
932 break;
933 }
934 break;
935 }
936 case 'S':
937 case 's':
938 {
939 if (LocaleCompare((char *) keyword,"stealth") == 0)
940 {
anthony6f201312012-03-30 04:08:15 +0000941 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000942 break;
943 }
944 break;
945 }
946 default:
947 break;
948 }
949 }
950 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +0000951 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000952}