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