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