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