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