]> 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     { "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   coder_semaphore=AcquireSemaphoreInfo();
383   return(MagickTrue);
384 }
385 \f
386 /*
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 %                                                                             %
389 %                                                                             %
390 %                                                                             %
391 +   C o d e r C o m p o n e n t T e r m i n u s                               %
392 %                                                                             %
393 %                                                                             %
394 %                                                                             %
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 %
397 %  CoderComponentTerminus() destroys the coder component.
398 %
399 %  The format of the CoderComponentTerminus method is:
400 %
401 %      CoderComponentTerminus(void)
402 %
403 */
404 MagickPrivate void CoderComponentTerminus(void)
405 {
406   if (coder_semaphore == (SemaphoreInfo *) NULL)
407     ActivateSemaphoreInfo(&coder_semaphore);
408   LockSemaphoreInfo(coder_semaphore);
409   if (coder_cache != (SplayTreeInfo *) NULL)
410     coder_cache=DestroySplayTree(coder_cache);
411   UnlockSemaphoreInfo(coder_semaphore);
412   RelinquishSemaphoreInfo(&coder_semaphore);
413 }
414 \f
415 /*
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 %                                                                             %
418 %                                                                             %
419 %                                                                             %
420 +   G e t C o d e r I n f o                                                   %
421 %                                                                             %
422 %                                                                             %
423 %                                                                             %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 %
426 %  GetCoderInfo searches the coder list for the specified name and if found
427 %  returns attributes for that coder.
428 %
429 %  The format of the GetCoderInfo method is:
430 %
431 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
432 %
433 %  A description of each parameter follows:
434 %
435 %    o name: the coder name.
436 %
437 %    o exception: return any errors or warnings in this structure.
438 %
439 */
440 MagickExport const CoderInfo *GetCoderInfo(const char *name,
441   ExceptionInfo *exception)
442 {
443   const CoderInfo
444     *coder_info;
445
446   assert(exception != (ExceptionInfo *) NULL);
447   if (IsCoderTreeInstantiated(exception) == MagickFalse)
448     return((const CoderInfo *) NULL);
449   LockSemaphoreInfo(coder_semaphore);
450   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
451     {
452       ResetSplayTreeIterator(coder_cache);
453       coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
454       UnlockSemaphoreInfo(coder_semaphore);
455       return(coder_info);
456     }
457   coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
458   UnlockSemaphoreInfo(coder_semaphore);
459   return(coder_info);
460 }
461 \f
462 /*
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 %                                                                             %
465 %                                                                             %
466 %                                                                             %
467 %   G e t C o d e r I n f o L i s t                                           %
468 %                                                                             %
469 %                                                                             %
470 %                                                                             %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 %
473 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
474 %  The format of the GetCoderInfoList function is:
475 %
476 %      const CoderInfo **GetCoderInfoList(const char *pattern,
477 %        size_t *number_coders,ExceptionInfo *exception)
478 %
479 %  A description of each parameter follows:
480 %
481 %    o pattern: Specifies a pointer to a text string containing a pattern.
482 %
483 %    o number_coders:  This integer returns the number of coders in the list.
484 %
485 %    o exception: return any errors or warnings in this structure.
486 %
487 */
488
489 static int CoderInfoCompare(const void *x,const void *y)
490 {
491   const CoderInfo
492     **p,
493     **q;
494
495   p=(const CoderInfo **) x,
496   q=(const CoderInfo **) y;
497   if (LocaleCompare((*p)->path,(*q)->path) == 0)
498     return(LocaleCompare((*p)->name,(*q)->name));
499   return(LocaleCompare((*p)->path,(*q)->path));
500 }
501
502 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
503   size_t *number_coders,ExceptionInfo *exception)
504 {
505   const CoderInfo
506     **coder_map;
507
508   register const CoderInfo
509     *p;
510
511   register ssize_t
512     i;
513
514   /*
515     Allocate coder list.
516   */
517   assert(pattern != (char *) NULL);
518   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
519   assert(number_coders != (size_t *) NULL);
520   *number_coders=0;
521   p=GetCoderInfo("*",exception);
522   if (p == (const CoderInfo *) NULL)
523     return((const CoderInfo **) NULL);
524   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
525     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
526   if (coder_map == (const CoderInfo **) NULL)
527     return((const CoderInfo **) NULL);
528   /*
529     Generate coder list.
530   */
531   LockSemaphoreInfo(coder_semaphore);
532   ResetSplayTreeIterator(coder_cache);
533   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
534   for (i=0; p != (const CoderInfo *) NULL; )
535   {
536     if ((p->stealth == MagickFalse) &&
537         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
538       coder_map[i++]=p;
539     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
540   }
541   UnlockSemaphoreInfo(coder_semaphore);
542   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
543   coder_map[i]=(CoderInfo *) NULL;
544   *number_coders=(size_t) i;
545   return(coder_map);
546 }
547 \f
548 /*
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 %                                                                             %
551 %                                                                             %
552 %                                                                             %
553 %   G e t C o d e r L i s t                                                   %
554 %                                                                             %
555 %                                                                             %
556 %                                                                             %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 %
559 %  GetCoderList() returns any coder_map that match the specified pattern.
560 %
561 %  The format of the GetCoderList function is:
562 %
563 %      char **GetCoderList(const char *pattern,size_t *number_coders,
564 %        ExceptionInfo *exception)
565 %
566 %  A description of each parameter follows:
567 %
568 %    o pattern: Specifies a pointer to a text string containing a pattern.
569 %
570 %    o number_coders:  This integer returns the number of coders in the list.
571 %
572 %    o exception: return any errors or warnings in this structure.
573 %
574 */
575
576 static int CoderCompare(const void *x,const void *y)
577 {
578   register const char
579     **p,
580     **q;
581
582   p=(const char **) x;
583   q=(const char **) y;
584   return(LocaleCompare(*p,*q));
585 }
586
587 MagickExport char **GetCoderList(const char *pattern,
588   size_t *number_coders,ExceptionInfo *exception)
589 {
590   char
591     **coder_map;
592
593   register const CoderInfo
594     *p;
595
596   register ssize_t
597     i;
598
599   /*
600     Allocate coder list.
601   */
602   assert(pattern != (char *) NULL);
603   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
604   assert(number_coders != (size_t *) NULL);
605   *number_coders=0;
606   p=GetCoderInfo("*",exception);
607   if (p == (const CoderInfo *) NULL)
608     return((char **) NULL);
609   coder_map=(char **) AcquireQuantumMemory((size_t)
610     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
611   if (coder_map == (char **) NULL)
612     return((char **) NULL);
613   /*
614     Generate coder list.
615   */
616   LockSemaphoreInfo(coder_semaphore);
617   ResetSplayTreeIterator(coder_cache);
618   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
619   for (i=0; p != (const CoderInfo *) NULL; )
620   {
621     if ((p->stealth == MagickFalse) &&
622         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
623       coder_map[i++]=ConstantString(p->name);
624     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
625   }
626   UnlockSemaphoreInfo(coder_semaphore);
627   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
628   coder_map[i]=(char *) NULL;
629   *number_coders=(size_t) i;
630   return(coder_map);
631 }
632 \f
633 /*
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 %                                                                             %
636 %                                                                             %
637 %                                                                             %
638 +   I s C o d e r T r e e I n s t a n t i a t e d                             %
639 %                                                                             %
640 %                                                                             %
641 %                                                                             %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 %
644 %  IsCoderTreeInstantiated() determines if the coder tree is instantiated.  If
645 %  not, it instantiates the tree and returns it.
646 %
647 %  The format of the IsCoderInstantiated method is:
648 %
649 %      MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
650 %
651 %  A description of each parameter follows.
652 %
653 %    o exception: return any errors or warnings in this structure.
654 %
655 */
656 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
657 {
658   if (coder_cache == (SplayTreeInfo *) NULL)
659     {
660       if (coder_semaphore == (SemaphoreInfo *) NULL)
661         ActivateSemaphoreInfo(&coder_semaphore);
662       LockSemaphoreInfo(coder_semaphore);
663       if (coder_cache == (SplayTreeInfo *) NULL)
664         coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
665       UnlockSemaphoreInfo(coder_semaphore);
666     }
667   return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
668 }
669 \f
670 /*
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 %                                                                             %
673 %                                                                             %
674 %                                                                             %
675 %  L i s t C o d e r I n f o                                                  %
676 %                                                                             %
677 %                                                                             %
678 %                                                                             %
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 %
681 %  ListCoderInfo() lists the coder info to a file.
682 %
683 %  The format of the ListCoderInfo coder is:
684 %
685 %      MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
686 %
687 %  A description of each parameter follows.
688 %
689 %    o file:  An pointer to a FILE.
690 %
691 %    o exception: return any errors or warnings in this structure.
692 %
693 */
694 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
695   ExceptionInfo *exception)
696 {
697   const char
698     *path;
699
700   const CoderInfo
701     **coder_info;
702
703   register ssize_t
704     i;
705
706   size_t
707     number_coders;
708
709   ssize_t
710     j;
711
712   if (file == (const FILE *) NULL)
713     file=stdout;
714   coder_info=GetCoderInfoList("*",&number_coders,exception);
715   if (coder_info == (const CoderInfo **) NULL)
716     return(MagickFalse);
717   path=(const char *) NULL;
718   for (i=0; i < (ssize_t) number_coders; i++)
719   {
720     if (coder_info[i]->stealth != MagickFalse)
721       continue;
722     if ((path == (const char *) NULL) ||
723         (LocaleCompare(path,coder_info[i]->path) != 0))
724       {
725         if (coder_info[i]->path != (char *) NULL)
726           (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
727         (void) FormatLocaleFile(file,"Magick      Coder\n");
728         (void) FormatLocaleFile(file,
729           "-------------------------------------------------"
730           "------------------------------\n");
731       }
732     path=coder_info[i]->path;
733     (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
734     for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
735       (void) FormatLocaleFile(file," ");
736     if (coder_info[i]->name != (char *) NULL)
737       (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
738     (void) FormatLocaleFile(file,"\n");
739   }
740   coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
741   (void) fflush(file);
742   return(MagickTrue);
743 }
744 \f
745 /*
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
747 %                                                                             %
748 %                                                                             %
749 %                                                                             %
750 +   L o a d C o d e r L i s t                                                 %
751 %                                                                             %
752 %                                                                             %
753 %                                                                             %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 %
756 %  LoadCoderCache() loads the coder configurations which provides a
757 %  mapping between coder attributes and a coder name.
758 %
759 %  The format of the LoadCoderCache coder is:
760 %
761 %      MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
762 %        const char *xml,const char *filename,const size_t depth,
763 %        ExceptionInfo *exception)
764 %
765 %  A description of each parameter follows:
766 %
767 %    o xml:  The coder list in XML format.
768 %
769 %    o filename:  The coder list filename.
770 %
771 %    o depth: depth of <include /> statements.
772 %
773 %    o exception: return any errors or warnings in this structure.
774 %
775 */
776 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
777   const char *xml,const char *filename,const size_t depth,
778   ExceptionInfo *exception)
779 {
780   char
781     keyword[MaxTextExtent],
782     *token;
783
784   const char
785     *q;
786
787   CoderInfo
788     *coder_info;
789
790   MagickBooleanType
791     status;
792
793   /*
794     Load the coder map file.
795   */
796   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
797     "Loading coder configuration file \"%s\" ...",filename);
798   if (xml == (const char *) NULL)
799     return(MagickFalse);
800   status=MagickTrue;
801   coder_info=(CoderInfo *) NULL;
802   token=AcquireString(xml);
803   for (q=(char *) xml; *q != '\0'; )
804   {
805     /*
806       Interpret XML.
807     */
808     GetMagickToken(q,&q,token);
809     if (*token == '\0')
810       break;
811     (void) CopyMagickString(keyword,token,MaxTextExtent);
812     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
813       {
814         /*
815           Doctype element.
816         */
817         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
818           GetMagickToken(q,&q,token);
819         continue;
820       }
821     if (LocaleNCompare(keyword,"<!--",4) == 0)
822       {
823         /*
824           Comment element.
825         */
826         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
827           GetMagickToken(q,&q,token);
828         continue;
829       }
830     if (LocaleCompare(keyword,"<include") == 0)
831       {
832         /*
833           Include element.
834         */
835         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
836         {
837           (void) CopyMagickString(keyword,token,MaxTextExtent);
838           GetMagickToken(q,&q,token);
839           if (*token != '=')
840             continue;
841           GetMagickToken(q,&q,token);
842           if (LocaleCompare(keyword,"file") == 0)
843             {
844               if (depth > 200)
845                 (void) ThrowMagickException(exception,GetMagickModule(),
846                   ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
847               else
848                 {
849                   char
850                     path[MaxTextExtent],
851                     *xml;
852
853                   GetPathComponent(filename,HeadPath,path);
854                   if (*path != '\0')
855                     (void) ConcatenateMagickString(path,DirectorySeparator,
856                       MaxTextExtent);
857                   if (*token == *DirectorySeparator)
858                     (void) CopyMagickString(path,token,MaxTextExtent);
859                   else
860                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
861                   xml=FileToXML(path,~0UL);
862                   if (xml != (char *) NULL)
863                     {
864                       status&=LoadCoderCache(coder_cache,xml,path,depth+1,
865                         exception);
866                       xml=(char *) RelinquishMagickMemory(xml);
867                     }
868                 }
869             }
870         }
871         continue;
872       }
873     if (LocaleCompare(keyword,"<coder") == 0)
874       {
875         /*
876           Coder element.
877         */
878         coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
879         if (coder_info == (CoderInfo *) NULL)
880           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
881         (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
882         coder_info->path=ConstantString(filename);
883         coder_info->exempt=MagickFalse;
884         coder_info->signature=MagickSignature;
885         continue;
886       }
887     if (coder_info == (CoderInfo *) NULL)
888       continue;
889     if (LocaleCompare(keyword,"/>") == 0)
890       {
891         status=AddValueToSplayTree(coder_cache,ConstantString(
892           coder_info->magick),coder_info);
893         if (status == MagickFalse)
894           (void) ThrowMagickException(exception,GetMagickModule(),
895             ResourceLimitError,"MemoryAllocationFailed","`%s'",
896             coder_info->magick);
897         coder_info=(CoderInfo *) NULL;
898         continue;
899       }
900     GetMagickToken(q,(const char **) NULL,token);
901     if (*token != '=')
902       continue;
903     GetMagickToken(q,&q,token);
904     GetMagickToken(q,&q,token);
905     switch (*keyword)
906     {
907       case 'M':
908       case 'm':
909       {
910         if (LocaleCompare((char *) keyword,"magick") == 0)
911           {
912             coder_info->magick=ConstantString(token);
913             break;
914           }
915         break;
916       }
917       case 'N':
918       case 'n':
919       {
920         if (LocaleCompare((char *) keyword,"name") == 0)
921           {
922             coder_info->name=ConstantString(token);
923             break;
924           }
925         break;
926       }
927       case 'S':
928       case 's':
929       {
930         if (LocaleCompare((char *) keyword,"stealth") == 0)
931           {
932             coder_info->stealth=IsStringTrue(token);
933             break;
934           }
935         break;
936       }
937       default:
938         break;
939     }
940   }
941   token=(char *) RelinquishMagickMemory(token);
942   return(status);
943 }