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