]> granicus.if.org Git - imagemagick/blob - MagickCore/coder.c
Fixed detection of Ghostscript location.
[imagemagick] / MagickCore / coder.c
1 /*
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                                %
16 %                                   Cristy                                    %
17 %                                 May 2001                                    %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/coder-private.h"
47 #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"
60 #include "MagickCore/utility-private.h"
61 #include "MagickCore/xml-tree.h"
62 #include "MagickCore/xml-tree-private.h"
63 \f
64 /*
65   Define declarations.
66 */
67 #define MagickCoderFilename  "coder.xml"
68 \f
69 /*
70   Typedef declarations.
71 */
72 typedef struct _CoderMapInfo
73 {
74   const char
75     *magick,
76     *name;
77 } CoderMapInfo;
78 \f
79 /*
80   Static declarations.
81 */
82 static 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" },
95     { "AVI", "MPEG" },
96     { "BIE", "JBIG" },
97     { "BMP2", "BMP" },
98     { "BMP3", "BMP" },
99     { "B", "RAW" },
100     { "BRF", "BRAILLE" },
101     { "BGRA", "BGR" },
102     { "CMYKA", "CMYK" },
103     { "C", "RAW" },
104     { "CAL", "CALS" },
105     { "CANVAS", "XC" },
106     { "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" },
130     { "GRANITE", "MAGICK" },
131     { "GROUP4", "TIFF" },
132     { "GV", "DOT" },
133     { "K25", "DNG" },
134     { "KDC", "DNG" },
135     { "H", "MAGICK" },
136     { "HTM", "HTML" },
137     { "HTTP", "URL" },
138     { "HTTPS", "URL" },
139     { "ICB", "TGA" },
140     { "ICC", "META" },
141     { "ICM", "META" },
142     { "ICO", "ICON" },
143     { "IMPLICIT", "***" },
144     { "IPTC", "META" },
145     { "IPTCTEXT", "META" },
146     { "IPTCWTEXT", "META" },
147     { "ISOBRL", "BRAILLE" },
148     { "JBG", "JBIG" },
149     { "JNG", "PNG" },
150     { "JPC", "JP2" },
151     { "J2C", "JP2" },
152     { "J2K", "JP2" },
153     { "JPG", "JPEG" },
154     { "JPX", "JP2" },
155     { "K", "RAW" },
156     { "LOGO", "MAGICK" },
157     { "M2V", "MPEG" },
158     { "M4V", "MPEG" },
159     { "M", "RAW" },
160     { "MNG", "PNG" },
161     { "MOV", "MPEG" },
162     { "MP4", "MPEG" },
163     { "MPG", "MPEG" },
164     { "MPRI", "MPR" },
165     { "MEF", "DNG" },
166     { "MRW", "DNG" },
167     { "MSVG", "SVG" },
168     { "NEF", "DNG" },
169     { "NETSCAPE", "MAGICK" },
170     { "NRW", "DNG" },
171     { "O", "RAW" },
172     { "ORF", "DNG" },
173     { "OTF", "TTF" },
174     { "P7", "PNM" },
175     { "PAL", "UYVY" },
176     { "PAM", "PNM" },
177     { "PBM", "PNM" },
178     { "PCDS", "PCD" },
179     { "PDFA", "PDF" },
180     { "PEF", "DNG" },
181     { "PEF", "DNG" },
182     { "PFA", "TTF" },
183     { "PFB", "TTF" },
184     { "PFM", "PNM" },
185     { "PGM", "PNM" },
186     { "PGX", "JP2" },
187     { "PICON", "XPM" },
188     { "PJPEG", "JPEG" },
189     { "PM", "XPM" },
190     { "PNG24", "PNG" },
191     { "PNG32", "PNG" },
192     { "PNG8", "PNG" },
193     { "PPM", "PNM" },
194     { "PSB", "PSD" },
195     { "PTIF", "TIFF" },
196     { "RADIAL-GRADIENT", "GRADIENT" },
197     { "RAF", "DNG" },
198     { "RAS", "SUN" },
199     { "RGBA", "RGB" },
200     { "RGBO", "RGB" },
201     { "RMF", "DNG" },
202     { "R", "RAW" },
203     { "ROSE", "MAGICK" },
204     { "RW2", "DNG" },
205     { "SHTML", "HTML" },
206     { "SPARSE-COLOR", "TXT" },
207     { "SR2", "DNG" },
208     { "SRF", "DNG" },
209     { "SVGZ", "SVG" },
210     { "TEXT", "TXT" },
211     { "TIFF64", "TIFF" },
212     { "TIF", "TIFF" },
213     { "TTC", "TTF" },
214     { "UBRL", "BRAILLE" },
215     { "VDA", "TGA" },
216     { "VST", "TGA" },
217     { "WIZARD", "MAGICK" },
218 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
219     { "WMF", "EMF" },
220 #endif
221     { "WMV", "MPEG" },
222     { "WMZ", "WMF" },
223     { "X3f", "DNG" },
224     { "XMP", "META" },
225     { "XTRNARRAY", "XTRN" },
226     { "XTRNBLOB", "XTRN" },
227     { "XTRNFILE", "XTRN" },
228     { "XTRNIMAGE", "XTRN" },
229     { "XV", "VIFF" },
230     { "Y", "RAW" },
231     { "YCbCrA", "YCbCr" }
232  };
233
234 static SemaphoreInfo
235   *coder_semaphore = (SemaphoreInfo *) NULL;
236
237 static SplayTreeInfo
238   *coder_cache = (SplayTreeInfo *) NULL;
239 \f
240 /*
241   Forward declarations.
242 */
243 static MagickBooleanType
244   IsCoderTreeInstantiated(ExceptionInfo *),
245   LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
246     ExceptionInfo *);
247 \f
248 /*
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 %                                                                             %
251 %                                                                             %
252 %                                                                             %
253 %  A c q u i r e C o d e r S p l a y T r e e                                  %
254 %                                                                             %
255 %                                                                             %
256 %                                                                             %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 %
259 %  AcquireCoderCache() caches one or more coder configurations which
260 %  provides a mapping between coder attributes and a coder name.
261 %
262 %  The format of the AcquireCoderCache coder is:
263 %
264 %      SplayTreeInfo *AcquireCoderCache(const char *filename,
265 %        ExceptionInfo *exception)
266 %
267 %  A description of each parameter follows:
268 %
269 %    o filename: the font file name.
270 %
271 %    o exception: return any errors or warnings in this structure.
272 %
273 */
274
275 static void *DestroyCoderNode(void *coder_info)
276 {
277   register CoderInfo
278     *p;
279
280   p=(CoderInfo *) coder_info;
281   if (p->exempt == MagickFalse)
282     {
283       if (p->path != (char *) NULL)
284         p->path=DestroyString(p->path);
285       if (p->name != (char *) NULL)
286         p->name=DestroyString(p->name);
287       if (p->magick != (char *) NULL)
288         p->magick=DestroyString(p->magick);
289     }
290   return(RelinquishMagickMemory(p));
291 }
292
293 static SplayTreeInfo *AcquireCoderCache(const char *filename,
294   ExceptionInfo *exception)
295 {
296   const StringInfo
297     *option;
298
299   LinkedListInfo
300     *options;
301
302   MagickStatusType
303     status;
304
305   register ssize_t
306     i;
307
308   SplayTreeInfo
309     *coder_cache;
310
311   /*
312     Load external coder map.
313   */
314   coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
315     DestroyCoderNode);
316   if (coder_cache == (SplayTreeInfo *) NULL)
317     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
318   status=MagickTrue;
319   options=GetConfigureOptions(filename,exception);
320   option=(const StringInfo *) GetNextValueInLinkedList(options);
321   while (option != (const StringInfo *) NULL)
322   {
323     status&=LoadCoderCache(coder_cache,(const char *)
324       GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
325     option=(const StringInfo *) GetNextValueInLinkedList(options);
326   }
327   options=DestroyConfigureOptions(options);
328   /*
329     Load built-in coder map.
330   */
331   for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
332   {
333     CoderInfo
334       *coder_info;
335
336     register const CoderMapInfo
337       *p;
338
339     p=CoderMap+i;
340     coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
341     if (coder_info == (CoderInfo *) NULL)
342       {
343         (void) ThrowMagickException(exception,GetMagickModule(),
344           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
345         continue;
346       }
347     (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
348     coder_info->path=(char *) "[built-in]";
349     coder_info->magick=(char *) p->magick;
350     coder_info->name=(char *) p->name;
351     coder_info->exempt=MagickTrue;
352     coder_info->signature=MagickSignature;
353     status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
354       coder_info);
355     if (status == MagickFalse)
356       (void) ThrowMagickException(exception,GetMagickModule(),
357         ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
358   }
359   return(coder_cache);
360 }
361 \f
362 /*
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 %                                                                             %
365 %                                                                             %
366 %                                                                             %
367 +   C o d e r C o m p o n e n t G e n e s i s                                 %
368 %                                                                             %
369 %                                                                             %
370 %                                                                             %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 %
373 %  CoderComponentGenesis() instantiates the coder component.
374 %
375 %  The format of the CoderComponentGenesis method is:
376 %
377 %      MagickBooleanType CoderComponentGenesis(void)
378 %
379 */
380 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
381 {
382   if (coder_semaphore == (SemaphoreInfo *) NULL)
383     coder_semaphore=AcquireSemaphoreInfo();
384   return(MagickTrue);
385 }
386 \f
387 /*
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 %                                                                             %
390 %                                                                             %
391 %                                                                             %
392 +   C o d e r C o m p o n e n t T e r m i n u s                               %
393 %                                                                             %
394 %                                                                             %
395 %                                                                             %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %
398 %  CoderComponentTerminus() destroys the coder component.
399 %
400 %  The format of the CoderComponentTerminus method is:
401 %
402 %      CoderComponentTerminus(void)
403 %
404 */
405 MagickPrivate void CoderComponentTerminus(void)
406 {
407   if (coder_semaphore == (SemaphoreInfo *) NULL)
408     ActivateSemaphoreInfo(&coder_semaphore);
409   LockSemaphoreInfo(coder_semaphore);
410   if (coder_cache != (SplayTreeInfo *) NULL)
411     coder_cache=DestroySplayTree(coder_cache);
412   UnlockSemaphoreInfo(coder_semaphore);
413   RelinquishSemaphoreInfo(&coder_semaphore);
414 }
415 \f
416 /*
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 %                                                                             %
419 %                                                                             %
420 %                                                                             %
421 +   G e t C o d e r I n f o                                                   %
422 %                                                                             %
423 %                                                                             %
424 %                                                                             %
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 %
427 %  GetCoderInfo searches the coder list for the specified name and if found
428 %  returns attributes for that coder.
429 %
430 %  The format of the GetCoderInfo method is:
431 %
432 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
433 %
434 %  A description of each parameter follows:
435 %
436 %    o name: the coder name.
437 %
438 %    o exception: return any errors or warnings in this structure.
439 %
440 */
441 MagickExport const CoderInfo *GetCoderInfo(const char *name,
442   ExceptionInfo *exception)
443 {
444   const CoderInfo
445     *coder_info;
446
447   assert(exception != (ExceptionInfo *) NULL);
448   if (IsCoderTreeInstantiated(exception) == MagickFalse)
449     return((const CoderInfo *) NULL);
450   LockSemaphoreInfo(coder_semaphore);
451   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
452     {
453       ResetSplayTreeIterator(coder_cache);
454       coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
455       UnlockSemaphoreInfo(coder_semaphore);
456       return(coder_info);
457     }
458   coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
459   UnlockSemaphoreInfo(coder_semaphore);
460   return(coder_info);
461 }
462 \f
463 /*
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 %                                                                             %
466 %                                                                             %
467 %                                                                             %
468 %   G e t C o d e r I n f o L i s t                                           %
469 %                                                                             %
470 %                                                                             %
471 %                                                                             %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 %
474 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
475 %  The format of the GetCoderInfoList function is:
476 %
477 %      const CoderInfo **GetCoderInfoList(const char *pattern,
478 %        size_t *number_coders,ExceptionInfo *exception)
479 %
480 %  A description of each parameter follows:
481 %
482 %    o pattern: Specifies a pointer to a text string containing a pattern.
483 %
484 %    o number_coders:  This integer returns the number of coders in the list.
485 %
486 %    o exception: return any errors or warnings in this structure.
487 %
488 */
489
490 static int CoderInfoCompare(const void *x,const void *y)
491 {
492   const CoderInfo
493     **p,
494     **q;
495
496   p=(const CoderInfo **) x,
497   q=(const CoderInfo **) y;
498   if (LocaleCompare((*p)->path,(*q)->path) == 0)
499     return(LocaleCompare((*p)->name,(*q)->name));
500   return(LocaleCompare((*p)->path,(*q)->path));
501 }
502
503 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
504   size_t *number_coders,ExceptionInfo *exception)
505 {
506   const CoderInfo
507     **coder_map;
508
509   register const CoderInfo
510     *p;
511
512   register ssize_t
513     i;
514
515   /*
516     Allocate coder list.
517   */
518   assert(pattern != (char *) NULL);
519   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
520   assert(number_coders != (size_t *) NULL);
521   *number_coders=0;
522   p=GetCoderInfo("*",exception);
523   if (p == (const CoderInfo *) NULL)
524     return((const CoderInfo **) NULL);
525   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
526     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
527   if (coder_map == (const CoderInfo **) NULL)
528     return((const CoderInfo **) NULL);
529   /*
530     Generate coder list.
531   */
532   LockSemaphoreInfo(coder_semaphore);
533   ResetSplayTreeIterator(coder_cache);
534   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
535   for (i=0; p != (const CoderInfo *) NULL; )
536   {
537     if ((p->stealth == MagickFalse) &&
538         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
539       coder_map[i++]=p;
540     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
541   }
542   UnlockSemaphoreInfo(coder_semaphore);
543   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
544   coder_map[i]=(CoderInfo *) NULL;
545   *number_coders=(size_t) i;
546   return(coder_map);
547 }
548 \f
549 /*
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 %                                                                             %
552 %                                                                             %
553 %                                                                             %
554 %   G e t C o d e r L i s t                                                   %
555 %                                                                             %
556 %                                                                             %
557 %                                                                             %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 %
560 %  GetCoderList() returns any coder_map that match the specified pattern.
561 %
562 %  The format of the GetCoderList function is:
563 %
564 %      char **GetCoderList(const char *pattern,size_t *number_coders,
565 %        ExceptionInfo *exception)
566 %
567 %  A description of each parameter follows:
568 %
569 %    o pattern: Specifies a pointer to a text string containing a pattern.
570 %
571 %    o number_coders:  This integer returns the number of coders in the list.
572 %
573 %    o exception: return any errors or warnings in this structure.
574 %
575 */
576
577 static int CoderCompare(const void *x,const void *y)
578 {
579   register const char
580     **p,
581     **q;
582
583   p=(const char **) x;
584   q=(const char **) y;
585   return(LocaleCompare(*p,*q));
586 }
587
588 MagickExport char **GetCoderList(const char *pattern,
589   size_t *number_coders,ExceptionInfo *exception)
590 {
591   char
592     **coder_map;
593
594   register const CoderInfo
595     *p;
596
597   register ssize_t
598     i;
599
600   /*
601     Allocate coder list.
602   */
603   assert(pattern != (char *) NULL);
604   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
605   assert(number_coders != (size_t *) NULL);
606   *number_coders=0;
607   p=GetCoderInfo("*",exception);
608   if (p == (const CoderInfo *) NULL)
609     return((char **) NULL);
610   coder_map=(char **) AcquireQuantumMemory((size_t)
611     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
612   if (coder_map == (char **) NULL)
613     return((char **) NULL);
614   /*
615     Generate coder list.
616   */
617   LockSemaphoreInfo(coder_semaphore);
618   ResetSplayTreeIterator(coder_cache);
619   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
620   for (i=0; p != (const CoderInfo *) NULL; )
621   {
622     if ((p->stealth == MagickFalse) &&
623         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
624       coder_map[i++]=ConstantString(p->name);
625     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
626   }
627   UnlockSemaphoreInfo(coder_semaphore);
628   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
629   coder_map[i]=(char *) NULL;
630   *number_coders=(size_t) i;
631   return(coder_map);
632 }
633 \f
634 /*
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 %                                                                             %
637 %                                                                             %
638 %                                                                             %
639 +   I s C o d e r T r e e I n s t a n t i a t e d                             %
640 %                                                                             %
641 %                                                                             %
642 %                                                                             %
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 %
645 %  IsCoderTreeInstantiated() determines if the coder tree is instantiated.  If
646 %  not, it instantiates the tree and returns it.
647 %
648 %  The format of the IsCoderInstantiated method is:
649 %
650 %      MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
651 %
652 %  A description of each parameter follows.
653 %
654 %    o exception: return any errors or warnings in this structure.
655 %
656 */
657 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
658 {
659   if (coder_cache == (SplayTreeInfo *) NULL)
660     {
661       if (coder_semaphore == (SemaphoreInfo *) NULL)
662         ActivateSemaphoreInfo(&coder_semaphore);
663       LockSemaphoreInfo(coder_semaphore);
664       if (coder_cache == (SplayTreeInfo *) NULL)
665         coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
666       UnlockSemaphoreInfo(coder_semaphore);
667     }
668   return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
669 }
670 \f
671 /*
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 %                                                                             %
674 %                                                                             %
675 %                                                                             %
676 %  L i s t C o d e r I n f o                                                  %
677 %                                                                             %
678 %                                                                             %
679 %                                                                             %
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 %
682 %  ListCoderInfo() lists the coder info to a file.
683 %
684 %  The format of the ListCoderInfo coder is:
685 %
686 %      MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
687 %
688 %  A description of each parameter follows.
689 %
690 %    o file:  An pointer to a FILE.
691 %
692 %    o exception: return any errors or warnings in this structure.
693 %
694 */
695 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
696   ExceptionInfo *exception)
697 {
698   const char
699     *path;
700
701   const CoderInfo
702     **coder_info;
703
704   register ssize_t
705     i;
706
707   size_t
708     number_coders;
709
710   ssize_t
711     j;
712
713   if (file == (const FILE *) NULL)
714     file=stdout;
715   coder_info=GetCoderInfoList("*",&number_coders,exception);
716   if (coder_info == (const CoderInfo **) NULL)
717     return(MagickFalse);
718   path=(const char *) NULL;
719   for (i=0; i < (ssize_t) number_coders; i++)
720   {
721     if (coder_info[i]->stealth != MagickFalse)
722       continue;
723     if ((path == (const char *) NULL) ||
724         (LocaleCompare(path,coder_info[i]->path) != 0))
725       {
726         if (coder_info[i]->path != (char *) NULL)
727           (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
728         (void) FormatLocaleFile(file,"Magick      Coder\n");
729         (void) FormatLocaleFile(file,
730           "-------------------------------------------------"
731           "------------------------------\n");
732       }
733     path=coder_info[i]->path;
734     (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
735     for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
736       (void) FormatLocaleFile(file," ");
737     if (coder_info[i]->name != (char *) NULL)
738       (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
739     (void) FormatLocaleFile(file,"\n");
740   }
741   coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
742   (void) fflush(file);
743   return(MagickTrue);
744 }
745 \f
746 /*
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748 %                                                                             %
749 %                                                                             %
750 %                                                                             %
751 +   L o a d C o d e r L i s t                                                 %
752 %                                                                             %
753 %                                                                             %
754 %                                                                             %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 %
757 %  LoadCoderCache() loads the coder configurations which provides a
758 %  mapping between coder attributes and a coder name.
759 %
760 %  The format of the LoadCoderCache coder is:
761 %
762 %      MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
763 %        const char *xml,const char *filename,const size_t depth,
764 %        ExceptionInfo *exception)
765 %
766 %  A description of each parameter follows:
767 %
768 %    o xml:  The coder list in XML format.
769 %
770 %    o filename:  The coder list filename.
771 %
772 %    o depth: depth of <include /> statements.
773 %
774 %    o exception: return any errors or warnings in this structure.
775 %
776 */
777 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
778   const char *xml,const char *filename,const size_t depth,
779   ExceptionInfo *exception)
780 {
781   char
782     keyword[MaxTextExtent],
783     *token;
784
785   const char
786     *q;
787
788   CoderInfo
789     *coder_info;
790
791   MagickBooleanType
792     status;
793
794   /*
795     Load the coder map file.
796   */
797   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
798     "Loading coder configuration file \"%s\" ...",filename);
799   if (xml == (const char *) NULL)
800     return(MagickFalse);
801   status=MagickTrue;
802   coder_info=(CoderInfo *) NULL;
803   token=AcquireString(xml);
804   for (q=(char *) xml; *q != '\0'; )
805   {
806     /*
807       Interpret XML.
808     */
809     GetMagickToken(q,&q,token);
810     if (*token == '\0')
811       break;
812     (void) CopyMagickString(keyword,token,MaxTextExtent);
813     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
814       {
815         /*
816           Doctype element.
817         */
818         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
819           GetMagickToken(q,&q,token);
820         continue;
821       }
822     if (LocaleNCompare(keyword,"<!--",4) == 0)
823       {
824         /*
825           Comment element.
826         */
827         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
828           GetMagickToken(q,&q,token);
829         continue;
830       }
831     if (LocaleCompare(keyword,"<include") == 0)
832       {
833         /*
834           Include element.
835         */
836         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
837         {
838           (void) CopyMagickString(keyword,token,MaxTextExtent);
839           GetMagickToken(q,&q,token);
840           if (*token != '=')
841             continue;
842           GetMagickToken(q,&q,token);
843           if (LocaleCompare(keyword,"file") == 0)
844             {
845               if (depth > 200)
846                 (void) ThrowMagickException(exception,GetMagickModule(),
847                   ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
848               else
849                 {
850                   char
851                     path[MaxTextExtent],
852                     *xml;
853
854                   GetPathComponent(filename,HeadPath,path);
855                   if (*path != '\0')
856                     (void) ConcatenateMagickString(path,DirectorySeparator,
857                       MaxTextExtent);
858                   if (*token == *DirectorySeparator)
859                     (void) CopyMagickString(path,token,MaxTextExtent);
860                   else
861                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
862                   xml=FileToXML(path,~0UL);
863                   if (xml != (char *) NULL)
864                     {
865                       status&=LoadCoderCache(coder_cache,xml,path,depth+1,
866                         exception);
867                       xml=(char *) RelinquishMagickMemory(xml);
868                     }
869                 }
870             }
871         }
872         continue;
873       }
874     if (LocaleCompare(keyword,"<coder") == 0)
875       {
876         /*
877           Coder element.
878         */
879         coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
880         if (coder_info == (CoderInfo *) NULL)
881           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
882         (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
883         coder_info->path=ConstantString(filename);
884         coder_info->exempt=MagickFalse;
885         coder_info->signature=MagickSignature;
886         continue;
887       }
888     if (coder_info == (CoderInfo *) NULL)
889       continue;
890     if (LocaleCompare(keyword,"/>") == 0)
891       {
892         status=AddValueToSplayTree(coder_cache,ConstantString(
893           coder_info->magick),coder_info);
894         if (status == MagickFalse)
895           (void) ThrowMagickException(exception,GetMagickModule(),
896             ResourceLimitError,"MemoryAllocationFailed","`%s'",
897             coder_info->magick);
898         coder_info=(CoderInfo *) NULL;
899         continue;
900       }
901     GetMagickToken(q,(const char **) NULL,token);
902     if (*token != '=')
903       continue;
904     GetMagickToken(q,&q,token);
905     GetMagickToken(q,&q,token);
906     switch (*keyword)
907     {
908       case 'M':
909       case 'm':
910       {
911         if (LocaleCompare((char *) keyword,"magick") == 0)
912           {
913             coder_info->magick=ConstantString(token);
914             break;
915           }
916         break;
917       }
918       case 'N':
919       case 'n':
920       {
921         if (LocaleCompare((char *) keyword,"name") == 0)
922           {
923             coder_info->name=ConstantString(token);
924             break;
925           }
926         break;
927       }
928       case 'S':
929       case 's':
930       {
931         if (LocaleCompare((char *) keyword,"stealth") == 0)
932           {
933             coder_info->stealth=IsStringTrue(token);
934             break;
935           }
936         break;
937       }
938       default:
939         break;
940     }
941   }
942   token=(char *) RelinquishMagickMemory(token);
943   return(status);
944 }