blob: 0a28c7bf9e26ad30b8733fbb7f467910bdddf108 [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% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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" },
cristye3e77a12009-10-16 00:47:21 +0000156 { "JPG", "JPEG" },
157 { "JPX", "JP2" },
158 { "K", "RAW" },
159 { "LOGO", "MAGICK" },
160 { "M2V", "MPEG" },
161 { "M4V", "MPEG" },
162 { "M", "RAW" },
163 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000164 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000165 { "MP4", "MPEG" },
166 { "MPG", "MPEG" },
167 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000168 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000169 { "MRW", "DNG" },
170 { "MSVG", "SVG" },
171 { "NEF", "DNG" },
172 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000173 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000174 { "O", "RAW" },
175 { "ORF", "DNG" },
176 { "OTF", "TTF" },
177 { "P7", "PNM" },
178 { "PAL", "UYVY" },
179 { "PAM", "PNM" },
180 { "PBM", "PNM" },
181 { "PCDS", "PCD" },
182 { "PDFA", "PDF" },
183 { "PEF", "DNG" },
184 { "PEF", "DNG" },
185 { "PFA", "TTF" },
186 { "PFB", "TTF" },
187 { "PFM", "PNM" },
188 { "PGM", "PNM" },
189 { "PGX", "JP2" },
190 { "PICON", "XPM" },
191 { "PJPEG", "JPEG" },
192 { "PM", "XPM" },
193 { "PNG24", "PNG" },
194 { "PNG32", "PNG" },
195 { "PNG8", "PNG" },
196 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000197 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000198 { "PTIF", "TIFF" },
199 { "RADIAL-GRADIENT", "GRADIENT" },
200 { "RAF", "DNG" },
201 { "RAS", "SUN" },
cristy1b1f0722014-06-10 10:14:59 +0000202 { "RAW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000203 { "RGBA", "RGB" },
204 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000205 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000206 { "R", "RAW" },
207 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000208 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000209 { "SHTML", "HTML" },
dirka1e4ab82014-10-07 05:06:49 +0000210 { "SIX", "SIXEL" },
cristye6ee7492013-03-16 15:04:42 +0000211 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000212 { "SR2", "DNG" },
213 { "SRF", "DNG" },
214 { "SVGZ", "SVG" },
215 { "TEXT", "TXT" },
216 { "TIFF64", "TIFF" },
217 { "TIF", "TIFF" },
218 { "TTC", "TTF" },
219 { "UBRL", "BRAILLE" },
220 { "VDA", "TGA" },
221 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000222 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000223#if defined(MAGICKCORE_WINGDI32_DELEGATE)
224 { "WMF", "EMF" },
225#endif
cristye3e77a12009-10-16 00:47:21 +0000226 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000227 { "WMZ", "WMF" },
228 { "X3f", "DNG" },
229 { "XMP", "META" },
230 { "XTRNARRAY", "XTRN" },
231 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000232 { "XTRNFILE", "XTRN" },
233 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000234 { "XV", "VIFF" },
235 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000236 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000237 };
238
cristy3ed852e2009-09-05 21:47:34 +0000239static SemaphoreInfo
240 *coder_semaphore = (SemaphoreInfo *) NULL;
241
242static SplayTreeInfo
cristy86e5ac92014-03-16 19:27:39 +0000243 *coder_cache = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000244
245/*
246 Forward declarations.
247*/
248static MagickBooleanType
cristy904e5912014-03-15 19:53:14 +0000249 IsCoderTreeInstantiated(ExceptionInfo *),
cristy86e5ac92014-03-16 19:27:39 +0000250 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
251 ExceptionInfo *);
252
253/*
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255% %
256% %
257% %
cristy6c6322e2014-09-09 22:39:21 +0000258+ A c q u i r e C o d e r C a c h e %
cristy86e5ac92014-03-16 19:27:39 +0000259% %
260% %
261% %
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263%
cristy6c6322e2014-09-09 22:39:21 +0000264% AcquireCoderCache() caches one or more coder configurations which provides a
265% mapping between coder attributes and a coder name.
cristy86e5ac92014-03-16 19:27:39 +0000266%
267% The format of the AcquireCoderCache coder is:
268%
269% SplayTreeInfo *AcquireCoderCache(const char *filename,
270% ExceptionInfo *exception)
271%
272% A description of each parameter follows:
273%
274% o filename: the font file name.
275%
276% o exception: return any errors or warnings in this structure.
277%
278*/
279
280static void *DestroyCoderNode(void *coder_info)
281{
282 register CoderInfo
283 *p;
284
285 p=(CoderInfo *) coder_info;
286 if (p->exempt == MagickFalse)
287 {
288 if (p->path != (char *) NULL)
289 p->path=DestroyString(p->path);
290 if (p->name != (char *) NULL)
291 p->name=DestroyString(p->name);
292 if (p->magick != (char *) NULL)
293 p->magick=DestroyString(p->magick);
294 }
295 return(RelinquishMagickMemory(p));
296}
297
298static SplayTreeInfo *AcquireCoderCache(const char *filename,
299 ExceptionInfo *exception)
300{
301 const StringInfo
302 *option;
303
304 LinkedListInfo
305 *options;
306
307 MagickStatusType
308 status;
309
310 register ssize_t
311 i;
312
313 SplayTreeInfo
314 *coder_cache;
315
316 /*
317 Load external coder map.
318 */
319 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
320 DestroyCoderNode);
321 if (coder_cache == (SplayTreeInfo *) NULL)
322 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
323 status=MagickTrue;
324 options=GetConfigureOptions(filename,exception);
325 option=(const StringInfo *) GetNextValueInLinkedList(options);
326 while (option != (const StringInfo *) NULL)
327 {
cristycd2cd182014-03-18 12:10:55 +0000328 status&=LoadCoderCache(coder_cache,(const char *)
329 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy86e5ac92014-03-16 19:27:39 +0000330 option=(const StringInfo *) GetNextValueInLinkedList(options);
331 }
332 options=DestroyConfigureOptions(options);
333 /*
334 Load built-in coder map.
335 */
336 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
337 {
338 CoderInfo
339 *coder_info;
340
341 register const CoderMapInfo
342 *p;
343
344 p=CoderMap+i;
345 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
346 if (coder_info == (CoderInfo *) NULL)
347 {
348 (void) ThrowMagickException(exception,GetMagickModule(),
349 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
350 continue;
351 }
352 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
353 coder_info->path=(char *) "[built-in]";
354 coder_info->magick=(char *) p->magick;
355 coder_info->name=(char *) p->name;
356 coder_info->exempt=MagickTrue;
357 coder_info->signature=MagickSignature;
358 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
359 coder_info);
360 if (status == MagickFalse)
361 (void) ThrowMagickException(exception,GetMagickModule(),
362 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
363 }
364 return(coder_cache);
365}
cristy3ed852e2009-09-05 21:47:34 +0000366
367/*
368%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369% %
370% %
371% %
cristyf34a1452009-10-24 22:29:27 +0000372+ 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 +0000373% %
374% %
375% %
376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377%
cristyf34a1452009-10-24 22:29:27 +0000378% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000379%
cristyf34a1452009-10-24 22:29:27 +0000380% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000381%
cristyf34a1452009-10-24 22:29:27 +0000382% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000383%
384*/
cristy5ff4eaf2011-09-03 01:38:02 +0000385MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000386{
cristy7c977062014-04-04 14:05:53 +0000387 if (coder_semaphore == (SemaphoreInfo *) NULL)
388 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000389 return(MagickTrue);
390}
391
392/*
393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394% %
395% %
396% %
397+ C o d e r C o m p o n e n t T e r m i n u s %
398% %
399% %
400% %
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402%
403% CoderComponentTerminus() destroys the coder component.
404%
405% The format of the CoderComponentTerminus method is:
406%
407% CoderComponentTerminus(void)
408%
409*/
cristy5ff4eaf2011-09-03 01:38:02 +0000410MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000411{
cristy18b17442009-10-25 18:36:48 +0000412 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000413 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000414 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000415 if (coder_cache != (SplayTreeInfo *) NULL)
416 coder_cache=DestroySplayTree(coder_cache);
cristyf84a1932010-01-03 18:00:18 +0000417 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000418 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000419}
420
421/*
422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423% %
424% %
425% %
426+ G e t C o d e r I n f o %
427% %
428% %
429% %
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431%
432% GetCoderInfo searches the coder list for the specified name and if found
433% returns attributes for that coder.
434%
435% The format of the GetCoderInfo method is:
436%
437% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
438%
439% A description of each parameter follows:
440%
441% o name: the coder name.
442%
443% o exception: return any errors or warnings in this structure.
444%
445*/
446MagickExport const CoderInfo *GetCoderInfo(const char *name,
447 ExceptionInfo *exception)
448{
cristy49d4d222014-03-16 00:37:58 +0000449 const CoderInfo
450 *coder_info;
451
cristy3ed852e2009-09-05 21:47:34 +0000452 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000453 if (IsCoderTreeInstantiated(exception) == MagickFalse)
454 return((const CoderInfo *) NULL);
cristy49d4d222014-03-16 00:37:58 +0000455 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000456 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
457 {
cristy86e5ac92014-03-16 19:27:39 +0000458 ResetSplayTreeIterator(coder_cache);
459 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy49d4d222014-03-16 00:37:58 +0000460 UnlockSemaphoreInfo(coder_semaphore);
461 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000462 }
cristy86e5ac92014-03-16 19:27:39 +0000463 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
cristy49d4d222014-03-16 00:37:58 +0000464 UnlockSemaphoreInfo(coder_semaphore);
465 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000466}
467
468/*
469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470% %
471% %
472% %
473% G e t C o d e r I n f o L i s t %
474% %
475% %
476% %
477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478%
479% GetCoderInfoList() returns any coder_map that match the specified pattern.
480% The format of the GetCoderInfoList function is:
481%
482% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000483% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000484%
485% A description of each parameter follows:
486%
487% o pattern: Specifies a pointer to a text string containing a pattern.
488%
489% o number_coders: This integer returns the number of coders in the list.
490%
491% o exception: return any errors or warnings in this structure.
492%
493*/
494
495static int CoderInfoCompare(const void *x,const void *y)
496{
497 const CoderInfo
498 **p,
499 **q;
500
501 p=(const CoderInfo **) x,
502 q=(const CoderInfo **) y;
503 if (LocaleCompare((*p)->path,(*q)->path) == 0)
504 return(LocaleCompare((*p)->name,(*q)->name));
505 return(LocaleCompare((*p)->path,(*q)->path));
506}
507
508MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000509 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000510{
511 const CoderInfo
512 **coder_map;
513
514 register const CoderInfo
515 *p;
516
cristybb503372010-05-27 20:51:26 +0000517 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000518 i;
519
520 /*
521 Allocate coder list.
522 */
523 assert(pattern != (char *) NULL);
524 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000525 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000526 *number_coders=0;
527 p=GetCoderInfo("*",exception);
528 if (p == (const CoderInfo *) NULL)
529 return((const CoderInfo **) NULL);
530 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000531 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000532 if (coder_map == (const CoderInfo **) NULL)
533 return((const CoderInfo **) NULL);
534 /*
535 Generate coder list.
536 */
cristyf84a1932010-01-03 18:00:18 +0000537 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000538 ResetSplayTreeIterator(coder_cache);
539 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000540 for (i=0; p != (const CoderInfo *) NULL; )
541 {
542 if ((p->stealth == MagickFalse) &&
543 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
544 coder_map[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000545 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000546 }
cristyf84a1932010-01-03 18:00:18 +0000547 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000548 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
549 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000550 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000551 return(coder_map);
552}
553
554/*
555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556% %
557% %
558% %
559% G e t C o d e r L i s t %
560% %
561% %
562% %
563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564%
565% GetCoderList() returns any coder_map that match the specified pattern.
566%
567% The format of the GetCoderList function is:
568%
cristybb503372010-05-27 20:51:26 +0000569% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000570% ExceptionInfo *exception)
571%
572% A description of each parameter follows:
573%
574% o pattern: Specifies a pointer to a text string containing a pattern.
575%
576% o number_coders: This integer returns the number of coders in the list.
577%
578% o exception: return any errors or warnings in this structure.
579%
580*/
581
582static int CoderCompare(const void *x,const void *y)
583{
584 register const char
585 **p,
586 **q;
587
588 p=(const char **) x;
589 q=(const char **) y;
590 return(LocaleCompare(*p,*q));
591}
592
593MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000594 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000595{
596 char
597 **coder_map;
598
599 register const CoderInfo
600 *p;
601
cristybb503372010-05-27 20:51:26 +0000602 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000603 i;
604
605 /*
606 Allocate coder list.
607 */
608 assert(pattern != (char *) NULL);
609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000610 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000611 *number_coders=0;
612 p=GetCoderInfo("*",exception);
613 if (p == (const CoderInfo *) NULL)
614 return((char **) NULL);
615 coder_map=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000616 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000617 if (coder_map == (char **) NULL)
618 return((char **) NULL);
619 /*
620 Generate coder list.
621 */
cristyf84a1932010-01-03 18:00:18 +0000622 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000623 ResetSplayTreeIterator(coder_cache);
624 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000625 for (i=0; p != (const CoderInfo *) NULL; )
626 {
627 if ((p->stealth == MagickFalse) &&
628 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
629 coder_map[i++]=ConstantString(p->name);
cristy86e5ac92014-03-16 19:27:39 +0000630 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000631 }
cristyf84a1932010-01-03 18:00:18 +0000632 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000633 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
634 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000635 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000636 return(coder_map);
637}
638
639/*
640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641% %
642% %
643% %
cristy904e5912014-03-15 19:53:14 +0000644+ 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 +0000645% %
646% %
647% %
648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649%
cristycd2cd182014-03-18 12:10:55 +0000650% IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
651% not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000652%
cristy904e5912014-03-15 19:53:14 +0000653% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000654%
cristy904e5912014-03-15 19:53:14 +0000655% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000656%
657% A description of each parameter follows.
658%
659% o exception: return any errors or warnings in this structure.
660%
661*/
cristy904e5912014-03-15 19:53:14 +0000662static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000663{
cristy86e5ac92014-03-16 19:27:39 +0000664 if (coder_cache == (SplayTreeInfo *) NULL)
665 {
666 if (coder_semaphore == (SemaphoreInfo *) NULL)
667 ActivateSemaphoreInfo(&coder_semaphore);
668 LockSemaphoreInfo(coder_semaphore);
669 if (coder_cache == (SplayTreeInfo *) NULL)
cristycd2cd182014-03-18 12:10:55 +0000670 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000671 UnlockSemaphoreInfo(coder_semaphore);
672 }
673 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000674}
675
676/*
677%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678% %
679% %
680% %
681% L i s t C o d e r I n f o %
682% %
683% %
684% %
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686%
687% ListCoderInfo() lists the coder info to a file.
688%
689% The format of the ListCoderInfo coder is:
690%
691% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
692%
693% A description of each parameter follows.
694%
695% o file: An pointer to a FILE.
696%
697% o exception: return any errors or warnings in this structure.
698%
699*/
700MagickExport MagickBooleanType ListCoderInfo(FILE *file,
701 ExceptionInfo *exception)
702{
703 const char
704 *path;
705
706 const CoderInfo
707 **coder_info;
708
cristybb503372010-05-27 20:51:26 +0000709 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000710 i;
711
cristybb503372010-05-27 20:51:26 +0000712 size_t
cristy3ed852e2009-09-05 21:47:34 +0000713 number_coders;
714
cristy9d314ff2011-03-09 01:30:28 +0000715 ssize_t
716 j;
717
cristy3ed852e2009-09-05 21:47:34 +0000718 if (file == (const FILE *) NULL)
719 file=stdout;
720 coder_info=GetCoderInfoList("*",&number_coders,exception);
721 if (coder_info == (const CoderInfo **) NULL)
722 return(MagickFalse);
723 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000724 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000725 {
726 if (coder_info[i]->stealth != MagickFalse)
727 continue;
728 if ((path == (const char *) NULL) ||
729 (LocaleCompare(path,coder_info[i]->path) != 0))
730 {
731 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000732 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
733 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000734 (void) FormatLocaleFile(file,
735 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000736 "------------------------------\n");
737 }
738 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000739 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000740 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000741 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000742 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000743 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
744 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000745 }
746 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
747 (void) fflush(file);
748 return(MagickTrue);
749}
750
751/*
752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753% %
754% %
755% %
756+ L o a d C o d e r L i s t %
757% %
758% %
759% %
760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761%
cristy86e5ac92014-03-16 19:27:39 +0000762% LoadCoderCache() loads the coder configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +0000763% mapping between coder attributes and a coder name.
764%
cristy86e5ac92014-03-16 19:27:39 +0000765% The format of the LoadCoderCache coder is:
cristy3ed852e2009-09-05 21:47:34 +0000766%
cristy86e5ac92014-03-16 19:27:39 +0000767% MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
768% const char *xml,const char *filename,const size_t depth,
769% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000770%
771% A description of each parameter follows:
772%
773% o xml: The coder list in XML format.
774%
775% o filename: The coder list filename.
776%
777% o depth: depth of <include /> statements.
778%
779% o exception: return any errors or warnings in this structure.
780%
781*/
cristy86e5ac92014-03-16 19:27:39 +0000782static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
783 const char *xml,const char *filename,const size_t depth,
784 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000785{
786 char
787 keyword[MaxTextExtent],
788 *token;
789
790 const char
791 *q;
792
793 CoderInfo
794 *coder_info;
795
cristy759ba912014-06-26 11:59:43 +0000796 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +0000797 status;
798
799 /*
800 Load the coder map file.
801 */
802 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
803 "Loading coder configuration file \"%s\" ...",filename);
804 if (xml == (const char *) NULL)
805 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000806 status=MagickTrue;
807 coder_info=(CoderInfo *) NULL;
808 token=AcquireString(xml);
809 for (q=(char *) xml; *q != '\0'; )
810 {
811 /*
812 Interpret XML.
813 */
814 GetMagickToken(q,&q,token);
815 if (*token == '\0')
816 break;
817 (void) CopyMagickString(keyword,token,MaxTextExtent);
818 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
819 {
820 /*
821 Doctype element.
822 */
823 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
824 GetMagickToken(q,&q,token);
825 continue;
826 }
827 if (LocaleNCompare(keyword,"<!--",4) == 0)
828 {
829 /*
830 Comment element.
831 */
832 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
833 GetMagickToken(q,&q,token);
834 continue;
835 }
836 if (LocaleCompare(keyword,"<include") == 0)
837 {
838 /*
839 Include element.
840 */
841 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
842 {
843 (void) CopyMagickString(keyword,token,MaxTextExtent);
844 GetMagickToken(q,&q,token);
845 if (*token != '=')
846 continue;
847 GetMagickToken(q,&q,token);
848 if (LocaleCompare(keyword,"file") == 0)
849 {
850 if (depth > 200)
851 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000852 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000853 else
854 {
855 char
856 path[MaxTextExtent],
857 *xml;
858
859 GetPathComponent(filename,HeadPath,path);
860 if (*path != '\0')
861 (void) ConcatenateMagickString(path,DirectorySeparator,
862 MaxTextExtent);
863 if (*token == *DirectorySeparator)
864 (void) CopyMagickString(path,token,MaxTextExtent);
865 else
866 (void) ConcatenateMagickString(path,token,MaxTextExtent);
cristy3291f512014-03-16 22:16:22 +0000867 xml=FileToXML(path,~0UL);
cristy3ed852e2009-09-05 21:47:34 +0000868 if (xml != (char *) NULL)
869 {
cristy86e5ac92014-03-16 19:27:39 +0000870 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
871 exception);
cristy3ed852e2009-09-05 21:47:34 +0000872 xml=(char *) RelinquishMagickMemory(xml);
873 }
874 }
875 }
876 }
877 continue;
878 }
879 if (LocaleCompare(keyword,"<coder") == 0)
880 {
881 /*
882 Coder element.
883 */
cristy73bd4a52010-10-05 11:24:23 +0000884 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000885 if (coder_info == (CoderInfo *) NULL)
886 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
887 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
888 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000889 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000890 coder_info->signature=MagickSignature;
891 continue;
892 }
893 if (coder_info == (CoderInfo *) NULL)
894 continue;
895 if (LocaleCompare(keyword,"/>") == 0)
896 {
cristy86e5ac92014-03-16 19:27:39 +0000897 status=AddValueToSplayTree(coder_cache,ConstantString(
cristy3ed852e2009-09-05 21:47:34 +0000898 coder_info->magick),coder_info);
899 if (status == MagickFalse)
900 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000901 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000902 coder_info->magick);
903 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000904 continue;
cristy3ed852e2009-09-05 21:47:34 +0000905 }
906 GetMagickToken(q,(const char **) NULL,token);
907 if (*token != '=')
908 continue;
909 GetMagickToken(q,&q,token);
910 GetMagickToken(q,&q,token);
911 switch (*keyword)
912 {
913 case 'M':
914 case 'm':
915 {
916 if (LocaleCompare((char *) keyword,"magick") == 0)
917 {
918 coder_info->magick=ConstantString(token);
919 break;
920 }
921 break;
922 }
923 case 'N':
924 case 'n':
925 {
926 if (LocaleCompare((char *) keyword,"name") == 0)
927 {
928 coder_info->name=ConstantString(token);
929 break;
930 }
931 break;
932 }
933 case 'S':
934 case 's':
935 {
936 if (LocaleCompare((char *) keyword,"stealth") == 0)
937 {
anthony6f201312012-03-30 04:08:15 +0000938 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000939 break;
940 }
941 break;
942 }
943 default:
944 break;
945 }
946 }
947 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +0000948 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000949}