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