2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/linked-list.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 #include "MagickCore/xml-tree-private.h"
67 #define MagickCoderFilename "coder.xml"
72 typedef struct _CoderMapInfo
82 static const CoderMapInfo
88 { "8BIMTEXT", "META" },
89 { "8BIMWTEXT", "META" },
93 { "APP1JPEG", "META" },
101 { "BRF", "BRAILLE" },
112 { "DATA", "INLINE" },
130 { "FRACTAL", "PLASMA" },
137 { "GRANITE", "MAGICK" },
138 { "GROUP4", "TIFF" },
154 { "IMPLICIT", "***" },
156 { "IPTCTEXT", "META" },
157 { "IPTCWTEXT", "META" },
158 { "ISOBRL", "BRAILLE" },
159 { "ISOBRL6", "BRAILLE" },
177 { "LOGO", "MAGICK" },
192 { "NETSCAPE", "MAGICK" },
224 { "RADIAL-GRADIENT", "GRADIENT" },
231 { "ROSE", "MAGICK" },
235 { "SPARSE-COLOR", "TXT" },
240 { "TIFF64", "TIFF" },
243 { "UBRL", "BRAILLE" },
244 { "UBRL6", "BRAILLE" },
247 { "WIZARD", "MAGICK" },
248 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
255 { "XTRNARRAY", "XTRN" },
258 { "YCbCrA", "YCbCr" }
262 *coder_semaphore = (SemaphoreInfo *) NULL;
265 *coder_cache = (SplayTreeInfo *) NULL;
268 Forward declarations.
270 static MagickBooleanType
271 IsCoderTreeInstantiated(ExceptionInfo *),
272 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 + A c q u i r e C o d e r C a c h e %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % AcquireCoderCache() caches one or more coder configurations which provides a
287 % mapping between coder attributes and a coder name.
289 % The format of the AcquireCoderCache coder is:
291 % SplayTreeInfo *AcquireCoderCache(const char *filename,
292 % ExceptionInfo *exception)
294 % A description of each parameter follows:
296 % o filename: the font file name.
298 % o exception: return any errors or warnings in this structure.
302 static void *DestroyCoderNode(void *coder_info)
307 p=(CoderInfo *) coder_info;
308 if (p->exempt == MagickFalse)
310 if (p->path != (char *) NULL)
311 p->path=DestroyString(p->path);
312 if (p->name != (char *) NULL)
313 p->name=DestroyString(p->name);
314 if (p->magick != (char *) NULL)
315 p->magick=DestroyString(p->magick);
317 return(RelinquishMagickMemory(p));
320 static SplayTreeInfo *AcquireCoderCache(const char *filename,
321 ExceptionInfo *exception)
333 Load external coder map.
335 cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
337 if (cache == (SplayTreeInfo *) NULL)
338 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
340 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
348 options=GetConfigureOptions(filename,exception);
349 option=(const StringInfo *) GetNextValueInLinkedList(options);
350 while (option != (const StringInfo *) NULL)
352 status&=LoadCoderCache(cache,(const char *) GetStringInfoDatum(option),
353 GetStringInfoPath(option),0,exception);
354 option=(const StringInfo *) GetNextValueInLinkedList(options);
356 options=DestroyConfigureOptions(options);
360 Load built-in coder map.
362 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
367 register const CoderMapInfo
371 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
372 if (coder_info == (CoderInfo *) NULL)
374 (void) ThrowMagickException(exception,GetMagickModule(),
375 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
378 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
379 coder_info->path=(char *) "[built-in]";
380 coder_info->magick=(char *) p->magick;
381 coder_info->name=(char *) p->name;
382 coder_info->exempt=MagickTrue;
383 coder_info->signature=MagickCoreSignature;
384 status&=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
386 if (status == MagickFalse)
387 (void) ThrowMagickException(exception,GetMagickModule(),
388 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 + C o d e r C o m p o n e n t G e n e s i s %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % CoderComponentGenesis() instantiates the coder component.
406 % The format of the CoderComponentGenesis method is:
408 % MagickBooleanType CoderComponentGenesis(void)
411 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
413 if (coder_semaphore == (SemaphoreInfo *) NULL)
414 coder_semaphore=AcquireSemaphoreInfo();
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 + C o d e r C o m p o n e n t T e r m i n u s %
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % CoderComponentTerminus() destroys the coder component.
431 % The format of the CoderComponentTerminus method is:
433 % CoderComponentTerminus(void)
436 MagickPrivate void CoderComponentTerminus(void)
438 if (coder_semaphore == (SemaphoreInfo *) NULL)
439 ActivateSemaphoreInfo(&coder_semaphore);
440 LockSemaphoreInfo(coder_semaphore);
441 if (coder_cache != (SplayTreeInfo *) NULL)
442 coder_cache=DestroySplayTree(coder_cache);
443 UnlockSemaphoreInfo(coder_semaphore);
444 RelinquishSemaphoreInfo(&coder_semaphore);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 + G e t C o d e r I n f o %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % GetCoderInfo searches the coder list for the specified name and if found
459 % returns attributes for that coder.
461 % The format of the GetCoderInfo method is:
463 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
465 % A description of each parameter follows:
467 % o name: the coder name.
469 % o exception: return any errors or warnings in this structure.
472 MagickExport const CoderInfo *GetCoderInfo(const char *name,
473 ExceptionInfo *exception)
478 assert(exception != (ExceptionInfo *) NULL);
479 if (IsCoderTreeInstantiated(exception) == MagickFalse)
480 return((const CoderInfo *) NULL);
481 LockSemaphoreInfo(coder_semaphore);
482 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
484 ResetSplayTreeIterator(coder_cache);
485 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
486 UnlockSemaphoreInfo(coder_semaphore);
489 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
490 UnlockSemaphoreInfo(coder_semaphore);
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % G e t C o d e r I n f o L i s t %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % GetCoderInfoList() returns any coder_map that match the specified pattern.
506 % The format of the GetCoderInfoList function is:
508 % const CoderInfo **GetCoderInfoList(const char *pattern,
509 % size_t *number_coders,ExceptionInfo *exception)
511 % A description of each parameter follows:
513 % o pattern: Specifies a pointer to a text string containing a pattern.
515 % o number_coders: This integer returns the number of coders in the list.
517 % o exception: return any errors or warnings in this structure.
521 static int CoderInfoCompare(const void *x,const void *y)
527 p=(const CoderInfo **) x,
528 q=(const CoderInfo **) y;
529 if (LocaleCompare((*p)->path,(*q)->path) == 0)
530 return(LocaleCompare((*p)->name,(*q)->name));
531 return(LocaleCompare((*p)->path,(*q)->path));
534 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
535 size_t *number_coders,ExceptionInfo *exception)
540 register const CoderInfo
549 assert(pattern != (char *) NULL);
550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
551 assert(number_coders != (size_t *) NULL);
553 p=GetCoderInfo("*",exception);
554 if (p == (const CoderInfo *) NULL)
555 return((const CoderInfo **) NULL);
556 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
557 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
558 if (coder_map == (const CoderInfo **) NULL)
559 return((const CoderInfo **) NULL);
563 LockSemaphoreInfo(coder_semaphore);
564 ResetSplayTreeIterator(coder_cache);
565 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
566 for (i=0; p != (const CoderInfo *) NULL; )
568 if ((p->stealth == MagickFalse) &&
569 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
571 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
573 UnlockSemaphoreInfo(coder_semaphore);
574 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
575 coder_map[i]=(CoderInfo *) NULL;
576 *number_coders=(size_t) i;
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % G e t C o d e r L i s t %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % GetCoderList() returns any coder_map that match the specified pattern.
593 % The format of the GetCoderList function is:
595 % char **GetCoderList(const char *pattern,size_t *number_coders,
596 % ExceptionInfo *exception)
598 % A description of each parameter follows:
600 % o pattern: Specifies a pointer to a text string containing a pattern.
602 % o number_coders: This integer returns the number of coders in the list.
604 % o exception: return any errors or warnings in this structure.
608 static int CoderCompare(const void *x,const void *y)
616 return(LocaleCompare(*p,*q));
619 MagickExport char **GetCoderList(const char *pattern,
620 size_t *number_coders,ExceptionInfo *exception)
625 register const CoderInfo
634 assert(pattern != (char *) NULL);
635 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
636 assert(number_coders != (size_t *) NULL);
638 p=GetCoderInfo("*",exception);
639 if (p == (const CoderInfo *) NULL)
640 return((char **) NULL);
641 coder_map=(char **) AcquireQuantumMemory((size_t)
642 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
643 if (coder_map == (char **) NULL)
644 return((char **) NULL);
648 LockSemaphoreInfo(coder_semaphore);
649 ResetSplayTreeIterator(coder_cache);
650 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
651 for (i=0; p != (const CoderInfo *) NULL; )
653 if ((p->stealth == MagickFalse) &&
654 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
655 coder_map[i++]=ConstantString(p->name);
656 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
658 UnlockSemaphoreInfo(coder_semaphore);
659 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
660 coder_map[i]=(char *) NULL;
661 *number_coders=(size_t) i;
666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670 + I s C o d e r T r e e I n s t a n t i a t e d %
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
677 % not, it instantiates the tree and returns it.
679 % The format of the IsCoderInstantiated method is:
681 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
683 % A description of each parameter follows.
685 % o exception: return any errors or warnings in this structure.
688 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
690 if (coder_cache == (SplayTreeInfo *) NULL)
692 if (coder_semaphore == (SemaphoreInfo *) NULL)
693 ActivateSemaphoreInfo(&coder_semaphore);
694 LockSemaphoreInfo(coder_semaphore);
695 if (coder_cache == (SplayTreeInfo *) NULL)
696 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
697 UnlockSemaphoreInfo(coder_semaphore);
699 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707 % L i s t C o d e r I n f o %
711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % ListCoderInfo() lists the coder info to a file.
715 % The format of the ListCoderInfo coder is:
717 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
719 % A description of each parameter follows.
721 % o file: An pointer to a FILE.
723 % o exception: return any errors or warnings in this structure.
726 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
727 ExceptionInfo *exception)
744 if (file == (const FILE *) NULL)
746 coder_info=GetCoderInfoList("*",&number_coders,exception);
747 if (coder_info == (const CoderInfo **) NULL)
749 path=(const char *) NULL;
750 for (i=0; i < (ssize_t) number_coders; i++)
752 if (coder_info[i]->stealth != MagickFalse)
754 if ((path == (const char *) NULL) ||
755 (LocaleCompare(path,coder_info[i]->path) != 0))
757 if (coder_info[i]->path != (char *) NULL)
758 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
759 (void) FormatLocaleFile(file,"Magick Coder\n");
760 (void) FormatLocaleFile(file,
761 "-------------------------------------------------"
762 "------------------------------\n");
764 path=coder_info[i]->path;
765 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
766 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
767 (void) FormatLocaleFile(file," ");
768 if (coder_info[i]->name != (char *) NULL)
769 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
770 (void) FormatLocaleFile(file,"\n");
772 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 + L o a d C o d e r C a c h e %
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 % LoadCoderCache() loads the coder configurations which provides a
789 % mapping between coder attributes and a coder name.
791 % The format of the LoadCoderCache coder is:
793 % MagickBooleanType LoadCoderCache(SplayTreeInfo *cache,const char *xml,
794 % const char *filename,const size_t depth,ExceptionInfo *exception)
796 % A description of each parameter follows:
798 % o xml: The coder list in XML format.
800 % o filename: The coder list filename.
802 % o depth: depth of <include /> statements.
804 % o exception: return any errors or warnings in this structure.
807 static MagickBooleanType LoadCoderCache(SplayTreeInfo *cache,const char *xml,
808 const char *filename,const size_t depth,ExceptionInfo *exception)
811 keyword[MagickPathExtent],
827 Load the coder map file.
829 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
830 "Loading coder configuration file \"%s\" ...",filename);
831 if (xml == (const char *) NULL)
834 coder_info=(CoderInfo *) NULL;
835 token=AcquireString(xml);
836 extent=strlen(token)+MagickPathExtent;
837 for (q=(char *) xml; *q != '\0'; )
842 GetNextToken(q,&q,extent,token);
845 (void) CopyMagickString(keyword,token,MagickPathExtent);
846 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
851 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
852 GetNextToken(q,&q,extent,token);
855 if (LocaleNCompare(keyword,"<!--",4) == 0)
860 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
861 GetNextToken(q,&q,extent,token);
864 if (LocaleCompare(keyword,"<include") == 0)
869 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
871 (void) CopyMagickString(keyword,token,MagickPathExtent);
872 GetNextToken(q,&q,extent,token);
875 GetNextToken(q,&q,extent,token);
876 if (LocaleCompare(keyword,"file") == 0)
879 (void) ThrowMagickException(exception,GetMagickModule(),
880 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
884 path[MagickPathExtent],
887 GetPathComponent(filename,HeadPath,path);
889 (void) ConcatenateMagickString(path,DirectorySeparator,
891 if (*token == *DirectorySeparator)
892 (void) CopyMagickString(path,token,MagickPathExtent);
894 (void) ConcatenateMagickString(path,token,MagickPathExtent);
895 file_xml=FileToXML(path,~0UL);
896 if (file_xml != (char *) NULL)
898 status&=LoadCoderCache(cache,file_xml,path,depth+1,
900 file_xml=DestroyString(file_xml);
907 if (LocaleCompare(keyword,"<coder") == 0)
912 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
913 if (coder_info == (CoderInfo *) NULL)
914 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
915 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
916 coder_info->path=ConstantString(filename);
917 coder_info->exempt=MagickFalse;
918 coder_info->signature=MagickCoreSignature;
921 if (coder_info == (CoderInfo *) NULL)
923 if ((LocaleCompare(keyword,"/>") == 0) ||
924 (LocaleCompare(keyword,"</policy>") == 0))
926 status=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
928 if (status == MagickFalse)
929 (void) ThrowMagickException(exception,GetMagickModule(),
930 ResourceLimitError,"MemoryAllocationFailed","`%s'",
932 coder_info=(CoderInfo *) NULL;
935 GetNextToken(q,(const char **) NULL,extent,token);
938 GetNextToken(q,&q,extent,token);
939 GetNextToken(q,&q,extent,token);
945 if (LocaleCompare((char *) keyword,"magick") == 0)
947 coder_info->magick=ConstantString(token);
955 if (LocaleCompare((char *) keyword,"name") == 0)
957 coder_info->name=ConstantString(token);
965 if (LocaleCompare((char *) keyword,"stealth") == 0)
967 coder_info->stealth=IsStringTrue(token);
976 token=(char *) RelinquishMagickMemory(token);
977 return(status != 0 ? MagickTrue : MagickFalse);