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