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