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