2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2018 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/memory-private.h"
55 #include "MagickCore/option.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/splay-tree.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/utility.h"
61 #include "MagickCore/utility-private.h"
62 #include "MagickCore/xml-tree.h"
63 #include "MagickCore/xml-tree-private.h"
68 #define MagickCoderFilename "coder.xml"
73 typedef struct _CoderMapInfo
83 static const CoderMapInfo
89 { "8BIMTEXT", "META" },
90 { "8BIMWTEXT", "META" },
94 { "APP1JPEG", "META" },
102 { "BRF", "BRAILLE" },
113 { "DATA", "INLINE" },
131 { "FRACTAL", "PLASMA" },
138 { "GRANITE", "MAGICK" },
140 { "GROUP4", "TIFF" },
157 { "IMPLICIT", "***" },
159 { "IPTCTEXT", "META" },
160 { "IPTCWTEXT", "META" },
161 { "ISOBRL", "BRAILLE" },
162 { "ISOBRL6", "BRAILLE" },
180 { "LOGO", "MAGICK" },
195 { "NETSCAPE", "MAGICK" },
226 { "RADIAL-GRADIENT", "GRADIENT" },
233 { "ROSE", "MAGICK" },
237 { "SPARSE-COLOR", "TXT" },
242 { "TIFF64", "TIFF" },
245 { "UBRL", "BRAILLE" },
246 { "UBRL6", "BRAILLE" },
249 { "WIZARD", "MAGICK" },
250 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
257 { "XTRNARRAY", "XTRN" },
260 { "YCbCrA", "YCbCr" }
264 *coder_semaphore = (SemaphoreInfo *) NULL;
267 *coder_cache = (SplayTreeInfo *) NULL;
270 Forward declarations.
272 static MagickBooleanType
273 IsCoderTreeInstantiated(ExceptionInfo *),
274 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 + A c q u i r e C o d e r C a c h e %
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % AcquireCoderCache() caches one or more coder configurations which provides a
289 % mapping between coder attributes and a coder name.
291 % The format of the AcquireCoderCache coder is:
293 % SplayTreeInfo *AcquireCoderCache(const char *filename,
294 % ExceptionInfo *exception)
296 % A description of each parameter follows:
298 % o filename: the font file name.
300 % o exception: return any errors or warnings in this structure.
304 static void *DestroyCoderNode(void *coder_info)
309 p=(CoderInfo *) coder_info;
310 if (p->exempt == MagickFalse)
312 if (p->path != (char *) NULL)
313 p->path=DestroyString(p->path);
314 if (p->name != (char *) NULL)
315 p->name=DestroyString(p->name);
316 if (p->magick != (char *) NULL)
317 p->magick=DestroyString(p->magick);
319 return(RelinquishMagickMemory(p));
322 static SplayTreeInfo *AcquireCoderCache(const char *filename,
323 ExceptionInfo *exception)
335 Load external coder map.
337 cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
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) memset(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)
475 assert(exception != (ExceptionInfo *) NULL);
476 if (IsCoderTreeInstantiated(exception) == MagickFalse)
477 return((const CoderInfo *) NULL);
478 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
479 return((const CoderInfo *) GetRootValueFromSplayTree(coder_cache));
480 return((const CoderInfo *) GetValueFromSplayTree(coder_cache,name));
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 % G e t C o d e r I n f o L i s t %
492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 % GetCoderInfoList() returns any coder_map that match the specified pattern.
495 % The format of the GetCoderInfoList function is:
497 % const CoderInfo **GetCoderInfoList(const char *pattern,
498 % size_t *number_coders,ExceptionInfo *exception)
500 % A description of each parameter follows:
502 % o pattern: Specifies a pointer to a text string containing a pattern.
504 % o number_coders: This integer returns the number of coders in the list.
506 % o exception: return any errors or warnings in this structure.
510 static int CoderInfoCompare(const void *x,const void *y)
516 p=(const CoderInfo **) x,
517 q=(const CoderInfo **) y;
518 if (LocaleCompare((*p)->path,(*q)->path) == 0)
519 return(LocaleCompare((*p)->name,(*q)->name));
520 return(LocaleCompare((*p)->path,(*q)->path));
523 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
524 size_t *number_coders,ExceptionInfo *exception)
529 register const CoderInfo
538 assert(pattern != (char *) NULL);
539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
540 assert(number_coders != (size_t *) NULL);
542 p=GetCoderInfo("*",exception);
543 if (p == (const CoderInfo *) NULL)
544 return((const CoderInfo **) NULL);
545 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
546 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
547 if (coder_map == (const CoderInfo **) NULL)
548 return((const CoderInfo **) NULL);
552 LockSemaphoreInfo(coder_semaphore);
553 ResetSplayTreeIterator(coder_cache);
554 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
555 for (i=0; p != (const CoderInfo *) NULL; )
557 if ((p->stealth == MagickFalse) &&
558 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
560 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
562 UnlockSemaphoreInfo(coder_semaphore);
563 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
564 coder_map[i]=(CoderInfo *) NULL;
565 *number_coders=(size_t) i;
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 % G e t C o d e r L i s t %
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 % GetCoderList() returns any coder_map that match the specified pattern.
582 % The format of the GetCoderList function is:
584 % char **GetCoderList(const char *pattern,size_t *number_coders,
585 % ExceptionInfo *exception)
587 % A description of each parameter follows:
589 % o pattern: Specifies a pointer to a text string containing a pattern.
591 % o number_coders: This integer returns the number of coders in the list.
593 % o exception: return any errors or warnings in this structure.
597 static int CoderCompare(const void *x,const void *y)
605 return(LocaleCompare(*p,*q));
608 MagickExport char **GetCoderList(const char *pattern,
609 size_t *number_coders,ExceptionInfo *exception)
614 register const CoderInfo
623 assert(pattern != (char *) NULL);
624 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
625 assert(number_coders != (size_t *) NULL);
627 p=GetCoderInfo("*",exception);
628 if (p == (const CoderInfo *) NULL)
629 return((char **) NULL);
630 coder_map=(char **) AcquireQuantumMemory((size_t)
631 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
632 if (coder_map == (char **) NULL)
633 return((char **) NULL);
637 LockSemaphoreInfo(coder_semaphore);
638 ResetSplayTreeIterator(coder_cache);
639 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
640 for (i=0; p != (const CoderInfo *) NULL; )
642 if ((p->stealth == MagickFalse) &&
643 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
644 coder_map[i++]=ConstantString(p->name);
645 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
647 UnlockSemaphoreInfo(coder_semaphore);
648 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
649 coder_map[i]=(char *) NULL;
650 *number_coders=(size_t) i;
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 + I s C o d e r T r e e I n s t a n t i a t e d %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
666 % not, it instantiates the tree and returns it.
668 % The format of the IsCoderInstantiated method is:
670 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
672 % A description of each parameter follows.
674 % o exception: return any errors or warnings in this structure.
677 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
679 if (coder_cache == (SplayTreeInfo *) NULL)
681 if (coder_semaphore == (SemaphoreInfo *) NULL)
682 ActivateSemaphoreInfo(&coder_semaphore);
683 LockSemaphoreInfo(coder_semaphore);
684 if (coder_cache == (SplayTreeInfo *) NULL)
685 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
686 UnlockSemaphoreInfo(coder_semaphore);
688 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 % L i s t C o d e r I n f o %
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % ListCoderInfo() lists the coder info to a file.
704 % The format of the ListCoderInfo coder is:
706 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
708 % A description of each parameter follows.
710 % o file: An pointer to a FILE.
712 % o exception: return any errors or warnings in this structure.
715 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
716 ExceptionInfo *exception)
733 if (file == (const FILE *) NULL)
735 coder_info=GetCoderInfoList("*",&number_coders,exception);
736 if (coder_info == (const CoderInfo **) NULL)
738 path=(const char *) NULL;
739 for (i=0; i < (ssize_t) number_coders; i++)
741 if (coder_info[i]->stealth != MagickFalse)
743 if ((path == (const char *) NULL) ||
744 (LocaleCompare(path,coder_info[i]->path) != 0))
746 if (coder_info[i]->path != (char *) NULL)
747 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
748 (void) FormatLocaleFile(file,"Magick Coder\n");
749 (void) FormatLocaleFile(file,
750 "-------------------------------------------------"
751 "------------------------------\n");
753 path=coder_info[i]->path;
754 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
755 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
756 (void) FormatLocaleFile(file," ");
757 if (coder_info[i]->name != (char *) NULL)
758 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
759 (void) FormatLocaleFile(file,"\n");
761 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771 + L o a d C o d e r C a c h e %
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 % LoadCoderCache() loads the coder configurations which provides a
778 % mapping between coder attributes and a coder name.
780 % The format of the LoadCoderCache coder is:
782 % MagickBooleanType LoadCoderCache(SplayTreeInfo *cache,const char *xml,
783 % const char *filename,const size_t depth,ExceptionInfo *exception)
785 % A description of each parameter follows:
787 % o xml: The coder list in XML format.
789 % o filename: The coder list filename.
791 % o depth: depth of <include /> statements.
793 % o exception: return any errors or warnings in this structure.
796 static MagickBooleanType LoadCoderCache(SplayTreeInfo *cache,const char *xml,
797 const char *filename,const size_t depth,ExceptionInfo *exception)
800 keyword[MagickPathExtent],
816 Load the coder map file.
818 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
819 "Loading coder configuration file \"%s\" ...",filename);
820 if (xml == (const char *) NULL)
823 coder_info=(CoderInfo *) NULL;
824 token=AcquireString(xml);
825 extent=strlen(token)+MagickPathExtent;
826 for (q=(char *) xml; *q != '\0'; )
831 GetNextToken(q,&q,extent,token);
834 (void) CopyMagickString(keyword,token,MagickPathExtent);
835 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
840 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
841 GetNextToken(q,&q,extent,token);
844 if (LocaleNCompare(keyword,"<!--",4) == 0)
849 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
850 GetNextToken(q,&q,extent,token);
853 if (LocaleCompare(keyword,"<include") == 0)
858 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
860 (void) CopyMagickString(keyword,token,MagickPathExtent);
861 GetNextToken(q,&q,extent,token);
864 GetNextToken(q,&q,extent,token);
865 if (LocaleCompare(keyword,"file") == 0)
867 if (depth > MagickMaxRecursionDepth)
868 (void) ThrowMagickException(exception,GetMagickModule(),
869 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
873 path[MagickPathExtent],
876 GetPathComponent(filename,HeadPath,path);
878 (void) ConcatenateMagickString(path,DirectorySeparator,
880 if (*token == *DirectorySeparator)
881 (void) CopyMagickString(path,token,MagickPathExtent);
883 (void) ConcatenateMagickString(path,token,MagickPathExtent);
884 file_xml=FileToXML(path,~0UL);
885 if (file_xml != (char *) NULL)
887 status&=LoadCoderCache(cache,file_xml,path,depth+1,
889 file_xml=DestroyString(file_xml);
896 if (LocaleCompare(keyword,"<coder") == 0)
901 coder_info=(CoderInfo *) AcquireCriticalMemory(sizeof(*coder_info));
902 (void) memset(coder_info,0,sizeof(*coder_info));
903 coder_info->path=ConstantString(filename);
904 coder_info->exempt=MagickFalse;
905 coder_info->signature=MagickCoreSignature;
908 if (coder_info == (CoderInfo *) NULL)
910 if ((LocaleCompare(keyword,"/>") == 0) ||
911 (LocaleCompare(keyword,"</policy>") == 0))
913 status=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
915 if (status == MagickFalse)
916 (void) ThrowMagickException(exception,GetMagickModule(),
917 ResourceLimitError,"MemoryAllocationFailed","`%s'",
919 coder_info=(CoderInfo *) NULL;
922 GetNextToken(q,(const char **) NULL,extent,token);
925 GetNextToken(q,&q,extent,token);
926 GetNextToken(q,&q,extent,token);
932 if (LocaleCompare((char *) keyword,"magick") == 0)
934 coder_info->magick=ConstantString(token);
942 if (LocaleCompare((char *) keyword,"name") == 0)
944 coder_info->name=ConstantString(token);
952 if (LocaleCompare((char *) keyword,"stealth") == 0)
954 coder_info->stealth=IsStringTrue(token);
963 token=(char *) RelinquishMagickMemory(token);
964 return(status != 0 ? MagickTrue : MagickFalse);