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