]> 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 +   C o d e r C o m p o n e n t G e n e s i s                                 %
244 %                                                                             %
245 %                                                                             %
246 %                                                                             %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 %
249 %  CoderComponentGenesis() instantiates the coder component.
250 %
251 %  The format of the CoderComponentGenesis method is:
252 %
253 %      MagickBooleanType CoderComponentGenesis(void)
254 %
255 */
256 MagickExport MagickBooleanType CoderComponentGenesis(void)
257 {
258   AcquireSemaphoreInfo(&coder_semaphore);
259   RelinquishSemaphoreInfo(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 MagickExport void CoderComponentTerminus(void)
282 {
283   AcquireSemaphoreInfo(&coder_semaphore);
284   if (coder_list != (SplayTreeInfo *) NULL)
285     coder_list=DestroySplayTree(coder_list);
286   instantiate_coder=MagickFalse;
287   RelinquishSemaphoreInfo(coder_semaphore);
288   DestroySemaphoreInfo(&coder_semaphore);
289 }
290 \f
291 /*
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %                                                                             %
294 %                                                                             %
295 %                                                                             %
296 +   G e t C o d e r I n f o                                                   %
297 %                                                                             %
298 %                                                                             %
299 %                                                                             %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %
302 %  GetCoderInfo searches the coder list for the specified name and if found
303 %  returns attributes for that coder.
304 %
305 %  The format of the GetCoderInfo method is:
306 %
307 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
308 %
309 %  A description of each parameter follows:
310 %
311 %    o name: the coder name.
312 %
313 %    o exception: return any errors or warnings in this structure.
314 %
315 */
316 MagickExport const CoderInfo *GetCoderInfo(const char *name,
317   ExceptionInfo *exception)
318 {
319   assert(exception != (ExceptionInfo *) NULL);
320   if ((coder_list == (SplayTreeInfo *) NULL) ||
321       (instantiate_coder == MagickFalse))
322     if (InitializeCoderList(exception) == MagickFalse)
323       return((const CoderInfo *) NULL);
324   if ((coder_list == (SplayTreeInfo *) NULL) ||
325       (GetNumberOfNodesInSplayTree(coder_list) == 0))
326     return((const CoderInfo *) NULL);
327   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
328     {
329       ResetSplayTreeIterator(coder_list);
330       return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
331     }
332   return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
333 }
334 \f
335 /*
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 %                                                                             %
338 %                                                                             %
339 %                                                                             %
340 %   G e t C o d e r I n f o L i s t                                           %
341 %                                                                             %
342 %                                                                             %
343 %                                                                             %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 %
346 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
347 %  The format of the GetCoderInfoList function is:
348 %
349 %      const CoderInfo **GetCoderInfoList(const char *pattern,
350 %        unsigned long *number_coders,ExceptionInfo *exception)
351 %
352 %  A description of each parameter follows:
353 %
354 %    o pattern: Specifies a pointer to a text string containing a pattern.
355 %
356 %    o number_coders:  This integer returns the number of coders in the list.
357 %
358 %    o exception: return any errors or warnings in this structure.
359 %
360 */
361
362 static int CoderInfoCompare(const void *x,const void *y)
363 {
364   const CoderInfo
365     **p,
366     **q;
367
368   p=(const CoderInfo **) x,
369   q=(const CoderInfo **) y;
370   if (LocaleCompare((*p)->path,(*q)->path) == 0)
371     return(LocaleCompare((*p)->name,(*q)->name));
372   return(LocaleCompare((*p)->path,(*q)->path));
373 }
374
375 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
376   unsigned long *number_coders,ExceptionInfo *exception)
377 {
378   const CoderInfo
379     **coder_map;
380
381   register const CoderInfo
382     *p;
383
384   register long
385     i;
386
387   /*
388     Allocate coder list.
389   */
390   assert(pattern != (char *) NULL);
391   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
392   assert(number_coders != (unsigned long *) NULL);
393   *number_coders=0;
394   p=GetCoderInfo("*",exception);
395   if (p == (const CoderInfo *) NULL)
396     return((const CoderInfo **) NULL);
397   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
398     GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
399   if (coder_map == (const CoderInfo **) NULL)
400     return((const CoderInfo **) NULL);
401   /*
402     Generate coder list.
403   */
404   AcquireSemaphoreInfo(&coder_semaphore);
405   ResetSplayTreeIterator(coder_list);
406   p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
407   for (i=0; p != (const CoderInfo *) NULL; )
408   {
409     if ((p->stealth == MagickFalse) &&
410         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
411       coder_map[i++]=p;
412     p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
413   }
414   RelinquishSemaphoreInfo(coder_semaphore);
415   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
416   coder_map[i]=(CoderInfo *) NULL;
417   *number_coders=(unsigned long) i;
418   return(coder_map);
419 }
420 \f
421 /*
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 %                                                                             %
424 %                                                                             %
425 %                                                                             %
426 %   G e t C o d e r L i s t                                                   %
427 %                                                                             %
428 %                                                                             %
429 %                                                                             %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 %
432 %  GetCoderList() returns any coder_map that match the specified pattern.
433 %
434 %  The format of the GetCoderList function is:
435 %
436 %      char **GetCoderList(const char *pattern,unsigned long *number_coders,
437 %        ExceptionInfo *exception)
438 %
439 %  A description of each parameter follows:
440 %
441 %    o pattern: Specifies a pointer to a text string containing a pattern.
442 %
443 %    o number_coders:  This integer returns the number of coders in the list.
444 %
445 %    o exception: return any errors or warnings in this structure.
446 %
447 */
448
449 static int CoderCompare(const void *x,const void *y)
450 {
451   register const char
452     **p,
453     **q;
454
455   p=(const char **) x;
456   q=(const char **) y;
457   return(LocaleCompare(*p,*q));
458 }
459
460 MagickExport char **GetCoderList(const char *pattern,
461   unsigned long *number_coders,ExceptionInfo *exception)
462 {
463   char
464     **coder_map;
465
466   register const CoderInfo
467     *p;
468
469   register long
470     i;
471
472   /*
473     Allocate coder list.
474   */
475   assert(pattern != (char *) NULL);
476   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
477   assert(number_coders != (unsigned long *) NULL);
478   *number_coders=0;
479   p=GetCoderInfo("*",exception);
480   if (p == (const CoderInfo *) NULL)
481     return((char **) NULL);
482   coder_map=(char **) AcquireQuantumMemory((size_t)
483     GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
484   if (coder_map == (char **) NULL)
485     return((char **) NULL);
486   /*
487     Generate coder list.
488   */
489   AcquireSemaphoreInfo(&coder_semaphore);
490   ResetSplayTreeIterator(coder_list);
491   p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
492   for (i=0; p != (const CoderInfo *) NULL; )
493   {
494     if ((p->stealth == MagickFalse) &&
495         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
496       coder_map[i++]=ConstantString(p->name);
497     p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
498   }
499   RelinquishSemaphoreInfo(coder_semaphore);
500   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
501   coder_map[i]=(char *) NULL;
502   *number_coders=(unsigned long) i;
503   return(coder_map);
504 }
505 \f
506 /*
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 %                                                                             %
509 %                                                                             %
510 %                                                                             %
511 +   I n i t i a l i z e C o d e r L i s t                                     %
512 %                                                                             %
513 %                                                                             %
514 %                                                                             %
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 %
517 %  InitializeCoderList() initializes the coder list.
518 %
519 %  The format of the InitializeCoderList method is:
520 %
521 %      MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
522 %
523 %  A description of each parameter follows.
524 %
525 %    o exception: return any errors or warnings in this structure.
526 %
527 */
528 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
529 {
530   if ((coder_list == (SplayTreeInfo *) NULL) &&
531       (instantiate_coder == MagickFalse))
532     {
533       AcquireSemaphoreInfo(&coder_semaphore);
534       if ((coder_list == (SplayTreeInfo *) NULL) &&
535           (instantiate_coder == MagickFalse))
536         {
537           (void) LoadCoderLists(MagickCoderFilename,exception);
538           instantiate_coder=MagickTrue;
539         }
540       RelinquishSemaphoreInfo(coder_semaphore);
541     }
542   return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
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 }