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