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