2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M OOO DDDD U U L EEEEE %
7 % MM MM O O D D U U L E %
8 % M M M O O D D U U L EEE %
9 % M M O O D D U U L E %
10 % M M OOO DDDD UUU LLLLL EEEEE %
13 % MagickCore Module Methods %
20 % Copyright 1999-2010 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 % http://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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/coder.h"
46 #include "magick/client.h"
47 #include "magick/configure.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/log.h"
51 #include "magick/hashmap.h"
52 #include "magick/magic.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/module.h"
56 #include "magick/policy.h"
57 #include "magick/semaphore.h"
58 #include "magick/splay-tree.h"
59 #include "magick/static.h"
60 #include "magick/string_.h"
61 #include "magick/token.h"
62 #include "magick/utility.h"
63 #if defined(MAGICKCORE_MODULES_SUPPORT)
64 #if defined(MAGICKCORE_LTDL_DELEGATE)
66 typedef lt_dlhandle ModuleHandle;
68 typedef void *ModuleHandle;
74 #if defined(MAGICKCORE_LTDL_DELEGATE)
75 # define ModuleGlobExpression "*.la"
78 # define ModuleGlobExpression "IM_MOD_DB_*.dll"
80 # define ModuleGlobExpression "IM_MOD_RL_*.dll"
88 *module_semaphore = (SemaphoreInfo *) NULL;
91 *module_list = (SplayTreeInfo *) NULL;
93 static volatile MagickBooleanType
94 instantiate_module = MagickFalse;
99 static const ModuleInfo
100 *RegisterModule(const ModuleInfo *,ExceptionInfo *);
102 static MagickBooleanType
103 GetMagickModulePath(const char *,MagickModuleType,char *,ExceptionInfo *),
104 UnregisterModule(const ModuleInfo *,ExceptionInfo *);
107 TagToCoderModuleName(const char *,char *),
108 TagToFilterModuleName(const char *,char *),
109 TagToModuleName(const char *,const char *,char *);
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % A c q u i r e M o d u l e I n f o %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % AcquireModuleInfo() allocates the ModuleInfo structure.
124 % The format of the AcquireModuleInfo method is:
126 % ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
128 % A description of each parameter follows:
130 % o path: the path associated with the tag.
132 % o tag: a character string that represents the image format we are
136 MagickExport ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
141 module_info=(ModuleInfo *) AcquireAlignedMemory(1,sizeof(*module_info));
142 if (module_info == (ModuleInfo *) NULL)
143 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
144 (void) ResetMagickMemory(module_info,0,sizeof(*module_info));
145 if (path != (const char *) NULL)
146 module_info->path=ConstantString(path);
147 if (tag != (const char *) NULL)
148 module_info->tag=ConstantString(tag);
149 module_info->timestamp=time(0);
150 module_info->signature=MagickSignature;
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 % D e s t r o y M o d u l e L i s t %
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 % DestroyModuleList() unregisters any previously loaded modules and exits
166 % the module loaded environment.
168 % The format of the DestroyModuleList module is:
170 % void DestroyModuleList(void)
173 MagickExport void DestroyModuleList(void)
176 Destroy magick modules.
178 LockSemaphoreInfo(module_semaphore);
179 #if defined(MAGICKCORE_MODULES_SUPPORT)
180 if (module_list != (SplayTreeInfo *) NULL)
181 module_list=DestroySplayTree(module_list);
182 if (instantiate_module != MagickFalse)
184 #if !defined(MAGICKCORE_JP2_DELEGATE)
185 (void) lt_dlexit(); /* Jasper has an errant atexit() handler */
187 instantiate_module=MagickFalse;
190 UnlockSemaphoreInfo(module_semaphore);
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 % G e t M o d u l e I n f o %
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 % GetModuleInfo() returns a pointer to a ModuleInfo structure that matches the
205 % specified tag. If tag is NULL, the head of the module list is returned. If
206 % no modules are loaded, or the requested module is not found, NULL is
209 % The format of the GetModuleInfo module is:
211 % ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
213 % A description of each parameter follows:
215 % o tag: a character string that represents the image format we are
218 % o exception: return any errors or warnings in this structure.
221 MagickExport ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
223 if ((module_list == (SplayTreeInfo *) NULL) ||
224 (instantiate_module == MagickFalse))
225 if (InitializeModuleList(exception) == MagickFalse)
226 return((ModuleInfo *) NULL);
227 if ((module_list == (SplayTreeInfo *) NULL) ||
228 (GetNumberOfNodesInSplayTree(module_list) == 0))
229 return((ModuleInfo *) NULL);
230 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
235 #if defined(MAGICKCORE_MODULES_SUPPORT)
236 if (LocaleCompare(tag,"*") == 0)
237 (void) OpenModules(exception);
239 LockSemaphoreInfo(module_semaphore);
240 ResetSplayTreeIterator(module_list);
241 p=(ModuleInfo *) GetNextValueInSplayTree(module_list);
242 UnlockSemaphoreInfo(module_semaphore);
245 return((ModuleInfo *) GetValueFromSplayTree(module_list,tag));
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % G e t M o d u l e I n f o L i s t %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % GetModuleInfoList() returns any modules that match the specified pattern.
261 % The format of the GetModuleInfoList function is:
263 % const ModuleInfo **GetModuleInfoList(const char *pattern,
264 % unsigned long *number_modules,ExceptionInfo *exception)
266 % A description of each parameter follows:
268 % o pattern: Specifies a pointer to a text string containing a pattern.
270 % o number_modules: This integer returns the number of modules in the list.
272 % o exception: return any errors or warnings in this structure.
276 #if defined(__cplusplus) || defined(c_plusplus)
280 static int ModuleInfoCompare(const void *x,const void *y)
286 p=(const ModuleInfo **) x,
287 q=(const ModuleInfo **) y;
288 if (LocaleCompare((*p)->path,(*q)->path) == 0)
289 return(LocaleCompare((*p)->tag,(*q)->tag));
290 return(LocaleCompare((*p)->path,(*q)->path));
293 #if defined(__cplusplus) || defined(c_plusplus)
297 MagickExport const ModuleInfo **GetModuleInfoList(const char *pattern,
298 unsigned long *number_modules,ExceptionInfo *exception)
303 register const ModuleInfo
310 Allocate module list.
312 assert(pattern != (char *) NULL);
313 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
314 assert(number_modules != (unsigned long *) NULL);
316 p=GetModuleInfo("*",exception);
317 if (p == (const ModuleInfo *) NULL)
318 return((const ModuleInfo **) NULL);
319 modules=(const ModuleInfo **) AcquireQuantumMemory((size_t)
320 GetNumberOfNodesInSplayTree(module_list)+1UL,sizeof(*modules));
321 if (modules == (const ModuleInfo **) NULL)
322 return((const ModuleInfo **) NULL);
324 Generate module list.
326 LockSemaphoreInfo(module_semaphore);
327 ResetSplayTreeIterator(module_list);
328 p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
329 for (i=0; p != (const ModuleInfo *) NULL; )
331 if ((p->stealth == MagickFalse) &&
332 (GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
334 p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
336 UnlockSemaphoreInfo(module_semaphore);
337 qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleInfoCompare);
338 modules[i]=(ModuleInfo *) NULL;
339 *number_modules=(unsigned long) i;
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % G e t M o d u l e L i s t %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % GetModuleList() returns any image format modules that match the specified
357 % The format of the GetModuleList function is:
359 % char **GetModuleList(const char *pattern,unsigned long *number_modules,
360 % ExceptionInfo *exception)
362 % A description of each parameter follows:
364 % o pattern: Specifies a pointer to a text string containing a pattern.
366 % o number_modules: This integer returns the number of modules in the
369 % o exception: return any errors or warnings in this structure.
373 #if defined(__cplusplus) || defined(c_plusplus)
377 static int ModuleCompare(const void *x,const void *y)
385 return(LocaleCompare(*p,*q));
388 #if defined(__cplusplus) || defined(c_plusplus)
392 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
393 struct dirent **result)
395 #if defined(MAGICKCORE_HAVE_READDIR_R)
396 return(readdir_r(directory,entry,result));
400 *result=readdir(directory);
405 MagickExport char **GetModuleList(const char *pattern,
406 unsigned long *number_modules,ExceptionInfo *exception)
410 filename[MaxTextExtent],
411 module_path[MaxTextExtent],
434 Locate all modules in the coder path.
436 TagToCoderModuleName("magick",filename);
437 length=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
440 return((char **) NULL);
441 GetPathComponent(module_path,HeadPath,path);
443 modules=(char **) AcquireQuantumMemory((size_t) max_entries+1UL,
445 if (modules == (char **) NULL)
446 return((char **) NULL);
447 *modules=(char *) NULL;
448 directory=opendir(path);
449 if (directory == (DIR *) NULL)
451 modules=(char **) RelinquishMagickMemory(modules);
452 return((char **) NULL);
454 buffer=(struct dirent *) AcquireAlignedMemory(1,sizeof(*buffer)+FILENAME_MAX+1);
455 if (buffer == (struct dirent *) NULL)
456 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
458 while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
459 (entry != (struct dirent *) NULL))
461 status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
462 if (status == MagickFalse)
464 if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
466 if (i >= (long) max_entries)
468 modules=(char **) NULL;
469 if (~max_entries > max_entries)
470 modules=(char **) ResizeQuantumMemory(modules,(size_t)
471 (max_entries << 1),sizeof(*modules));
473 if (modules == (char **) NULL)
477 Add new module name to list.
479 modules[i]=AcquireString((char *) NULL);
480 GetPathComponent(entry->d_name,BasePath,modules[i]);
481 LocaleUpper(modules[i]);
482 if (LocaleNCompare("IM_MOD_",modules[i],7) == 0)
484 (void) CopyMagickString(modules[i],modules[i]+10,MaxTextExtent);
485 modules[i][strlen(modules[i])-1]='\0';
489 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
490 (void) closedir(directory);
491 if (modules == (char **) NULL)
493 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
494 "MemoryAllocationFailed","`%s'",pattern);
495 return((char **) NULL);
497 qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleCompare);
498 modules[i]=(char *) NULL;
499 *number_modules=(unsigned long) i;
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 % G e t M a g i c k M o d u l e P a t h %
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 % GetMagickModulePath() finds a module with the specified module type and
517 % The format of the GetMagickModulePath module is:
519 % MagickBooleanType GetMagickModulePath(const char *filename,
520 % MagickModuleType module_type,char *path,ExceptionInfo *exception)
522 % A description of each parameter follows:
524 % o filename: the module file name.
526 % o module_type: the module type: MagickImageCoderModule or
527 % MagickImageFilterModule.
529 % o path: the path associated with the filename.
531 % o exception: return any errors or warnings in this structure.
534 static MagickBooleanType GetMagickModulePath(const char *filename,
535 MagickModuleType module_type,char *path,ExceptionInfo *exception)
540 assert(filename != (const char *) NULL);
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
542 assert(path != (char *) NULL);
543 assert(exception != (ExceptionInfo *) NULL);
544 (void) CopyMagickString(path,filename,MaxTextExtent);
545 module_path=(char *) NULL;
548 case MagickImageCoderModule:
551 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
552 "Searching for coder module file \"%s\" ...",filename);
553 module_path=GetEnvironmentValue("MAGICK_CODER_MODULE_PATH");
554 #if defined(MAGICKCORE_CODER_PATH)
555 if (module_path == (char *) NULL)
556 module_path=AcquireString(MAGICKCORE_CODER_PATH);
560 case MagickImageFilterModule:
562 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
563 "Searching for filter module file \"%s\" ...",filename);
564 module_path=GetEnvironmentValue("MAGICK_CODER_FILTER_PATH");
565 #if defined(MAGICKCORE_FILTER_PATH)
566 if (module_path == (char *) NULL)
567 module_path=AcquireString(MAGICKCORE_FILTER_PATH);
572 if (module_path != (char *) NULL)
578 for (p=module_path-1; p != (char *) NULL; )
580 (void) CopyMagickString(path,p+1,MaxTextExtent);
581 q=strchr(path,DirectoryListSeparator);
582 if (q != (char *) NULL)
584 q=path+strlen(path)-1;
585 if ((q >= path) && (*q != *DirectorySeparator))
586 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
587 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
588 if (IsPathAccessible(path) != MagickFalse)
590 module_path=DestroyString(module_path);
593 p=strchr(p+1,DirectoryListSeparator);
595 module_path=DestroyString(module_path);
597 #if defined(MAGICKCORE_INSTALLED_SUPPORT)
599 #if defined(MAGICKCORE_CODER_PATH)
605 Search hard coded paths.
609 case MagickImageCoderModule:
612 directory=MAGICKCORE_CODER_PATH;
615 case MagickImageFilterModule:
617 directory=MAGICKCORE_FILTER_PATH;
621 (void) FormatMagickString(path,MaxTextExtent,"%s%s",directory,filename);
622 if (IsPathAccessible(path) == MagickFalse)
624 ThrowFileException(exception,ConfigureWarning,
625 "UnableToOpenModuleFile",path);
631 #if defined(__WINDOWS__)
640 Locate path via registry key.
644 case MagickImageCoderModule:
647 registery_key="CoderModulesPath";
650 case MagickImageFilterModule:
652 registery_key="FilterModulesPath";
656 key_value=NTRegistryKeyLookup(registery_key);
657 if (key_value == (unsigned char *) NULL)
659 ThrowMagickException(exception,GetMagickModule(),ConfigureError,
660 "RegistryKeyLookupFailed","`%s'",registery_key);
663 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",(char *) key_value,
664 DirectorySeparator,filename);
665 key_value=(unsigned char *) RelinquishMagickMemory(key_value);
666 if (IsPathAccessible(path) == MagickFalse)
668 ThrowFileException(exception,ConfigureWarning,
669 "UnableToOpenModuleFile",path);
676 #if !defined(MAGICKCORE_CODER_PATH) && !defined(__WINDOWS__)
677 # error MAGICKCORE_CODER_PATH or __WINDOWS__ must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
684 home=GetEnvironmentValue("MAGICK_HOME");
685 if (home != (char *) NULL)
690 #if !defined(MAGICKCORE_POSIX_SUPPORT)
691 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",home,
692 DirectorySeparator,filename);
699 case MagickImageCoderModule:
702 directory=MAGICKCORE_CODER_RELATIVE_PATH;
705 case MagickImageFilterModule:
707 directory=MAGICKCORE_FILTER_RELATIVE_PATH;
711 (void) FormatMagickString(path,MaxTextExtent,"%s/lib/%s/%s",home,
714 home=DestroyString(home);
715 if (IsPathAccessible(path) != MagickFalse)
719 if (*GetClientPath() != '\0')
722 Search based on executable directory.
724 #if !defined(MAGICKCORE_POSIX_SUPPORT)
725 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
726 DirectorySeparator,filename);
729 prefix[MaxTextExtent];
736 case MagickImageCoderModule:
742 case MagickImageFilterModule:
748 (void) CopyMagickString(prefix,GetClientPath(),MaxTextExtent);
749 ChopPathComponents(prefix,1);
750 (void) FormatMagickString(path,MaxTextExtent,
751 "%s/lib/%s/modules-Q%d/%s/%s",prefix,MAGICKCORE_LIBRARY_RELATIVE_PATH,
752 MAGICKCORE_QUANTUM_DEPTH,directory,filename);
754 if (IsPathAccessible(path) != MagickFalse)
757 #if defined(__WINDOWS__)
762 if ((NTGetModulePath("CORE_RL_magick_.dll",path) != MagickFalse) ||
763 (NTGetModulePath("CORE_DB_magick_.dll",path) != MagickFalse) ||
764 (NTGetModulePath("Magick.dll",path) != MagickFalse))
766 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
767 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
768 if (IsPathAccessible(path) != MagickFalse)
777 home=GetEnvironmentValue("HOME");
778 if (home == (char *) NULL)
779 home=GetEnvironmentValue("USERPROFILE");
780 if (home != (char *) NULL)
783 Search $HOME/.magick.
785 (void) FormatMagickString(path,MaxTextExtent,"%s%s.magick%s%s",home,
786 DirectorySeparator,DirectorySeparator,filename);
787 home=DestroyString(home);
788 if (IsPathAccessible(path) != MagickFalse)
793 Search current directory.
795 if (IsPathAccessible(path) != MagickFalse)
797 if (exception->severity < ConfigureError)
798 ThrowFileException(exception,ConfigureWarning,"UnableToOpenModuleFile",
806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % I n i t i a l i z e M o d u l e L i s t %
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 % InitializeModuleList() initializes the module loader.
818 % The format of the InitializeModuleList() method is:
820 % InitializeModuleList(Exceptioninfo *exception)
822 % A description of each parameter follows.
824 % o exception: return any errors or warnings in this structure.
828 static void *DestroyModuleNode(void *module_info)
836 exception=AcquireExceptionInfo();
837 p=(ModuleInfo *) module_info;
838 if (UnregisterModule(p,exception) == MagickFalse)
839 CatchException(exception);
840 if (p->tag != (char *) NULL)
841 p->tag=DestroyString(p->tag);
842 if (p->path != (char *) NULL)
843 p->path=DestroyString(p->path);
844 exception=DestroyExceptionInfo(exception);
845 return(RelinquishMagickMemory(p));
848 MagickExport MagickBooleanType InitializeModuleList(
849 ExceptionInfo *magick_unused(exception))
851 if ((module_list == (SplayTreeInfo *) NULL) &&
852 (instantiate_module == MagickFalse))
854 if (module_semaphore == (SemaphoreInfo *) NULL)
855 AcquireSemaphoreInfo(&module_semaphore);
856 LockSemaphoreInfo(module_semaphore);
857 if ((module_list == (SplayTreeInfo *) NULL) &&
858 (instantiate_module == MagickFalse))
866 module_list=NewSplayTree(CompareSplayTreeString,
867 (void *(*)(void *)) NULL,DestroyModuleNode);
868 if (module_list == (SplayTreeInfo *) NULL)
869 ThrowFatalException(ResourceLimitFatalError,
870 "MemoryAllocationFailed");
871 module_info=AcquireModuleInfo((const char *) NULL,"[boot-strap]");
872 module_info->stealth=MagickTrue;
873 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
874 if (status == MagickFalse)
875 ThrowFatalException(ResourceLimitFatalError,
876 "MemoryAllocationFailed");
877 if (lt_dlinit() != 0)
878 ThrowFatalException(ModuleFatalError,
879 "UnableToInitializeModuleLoader");
880 instantiate_module=MagickTrue;
882 UnlockSemaphoreInfo(module_semaphore);
884 return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 % I n v o k e D y n a m i c I m a g e F i l t e r %
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % InvokeDynamicImageFilter() invokes a dynamic image filter.
900 % The format of the InvokeDynamicImageFilter module is:
902 % MagickBooleanType InvokeDynamicImageFilter(const char *tag,Image **image,
903 % const int argc,const char **argv,ExceptionInfo *exception)
905 % A description of each parameter follows:
907 % o tag: a character string that represents the name of the particular
910 % o image: the image.
912 % o argc: a pointer to an integer describing the number of elements in the
915 % o argv: a pointer to a text array containing the command line arguments.
917 % o exception: return any errors or warnings in this structure.
920 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
921 Image **images,const int argc,const char **argv,ExceptionInfo *exception)
942 assert(images != (Image **) NULL);
943 assert((*images)->signature == MagickSignature);
944 if ((*images)->debug != MagickFalse)
945 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
946 (*images)->filename);
947 #if !defined(MAGICKCORE_BUILD_MODULES)
952 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
953 if (status != MagickFalse)
957 rights=ReadPolicyRights;
958 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
961 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
962 "NotAuthorized","`%s'",tag);
965 TagToFilterModuleName(tag,name);
966 length=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
972 handle=(ModuleHandle) lt_dlopen(path);
973 if (handle == (ModuleHandle) NULL)
975 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
976 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
982 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
983 (void) FormatMagickString(name,MaxTextExtent,"%sImage",tag);
985 (void) FormatMagickString(name,MaxTextExtent,"%s%sImage",
986 MAGICKCORE_NAMESPACE_PREFIX,tag);
991 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
992 if (image_filter == (ImageFilterHandler *) NULL)
993 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
994 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
1000 if ((*images)->debug != MagickFalse)
1001 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1002 "Invoking \"%s\" dynamic image filter",tag);
1003 signature=image_filter(images,argc,argv,exception);
1004 if ((*images)->debug != MagickFalse)
1005 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),"\"%s\" completes",
1007 if (signature != MagickImageFilterSignature)
1009 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1010 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,signature,
1011 MagickImageFilterSignature);
1012 return(MagickFalse);
1018 if (lt_dlclose(handle) != 0)
1020 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1021 "UnableToCloseModule","`%s': %s",name,lt_dlerror());
1022 return(MagickFalse);
1028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032 % L i s t M o d u l e I n f o %
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038 % ListModuleInfo() lists the module info to a file.
1040 % The format of the ListModuleInfo module is:
1042 % MagickBooleanType ListModuleInfo(FILE *file,ExceptionInfo *exception)
1044 % A description of each parameter follows.
1046 % o file: An pointer to a FILE.
1048 % o exception: return any errors or warnings in this structure.
1051 MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1052 ExceptionInfo *exception)
1063 if (file == (const FILE *) NULL)
1065 module_info=GetModuleInfoList("*",&number_modules,exception);
1066 if (module_info == (const ModuleInfo **) NULL)
1067 return(MagickFalse);
1068 if (module_info[0]->path != (char *) NULL)
1071 path[MaxTextExtent];
1073 GetPathComponent(module_info[0]->path,HeadPath,path);
1074 (void) fprintf(file,"\nPath: %s\n\n",path);
1076 (void) fprintf(file,"Module\n");
1077 (void) fprintf(file,"-------------------------------------------------"
1078 "------------------------------\n");
1079 for (i=0; i < (long) number_modules; i++)
1081 if (module_info[i]->stealth != MagickFalse)
1083 (void) fprintf(file,"%s",module_info[i]->tag);
1084 (void) fprintf(file,"\n");
1086 (void) fflush(file);
1087 module_info=(const ModuleInfo **)
1088 RelinquishMagickMemory((void *) module_info);
1093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097 + M o d u l e C o m p o n e n t G e n e s i s %
1101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 % ModuleComponentGenesis() instantiates the module component.
1105 % The format of the ModuleComponentGenesis method is:
1107 % MagickBooleanType ModuleComponentGenesis(void)
1110 MagickExport MagickBooleanType ModuleComponentGenesis(void)
1115 AcquireSemaphoreInfo(&module_semaphore);
1116 exception=AcquireExceptionInfo();
1117 InitializeModuleList(exception);
1118 exception=DestroyExceptionInfo(exception);
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 + M o d u l e C o m p o n e n t T e r m i n u s %
1131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1133 % ModuleComponentTerminus() destroys the module component.
1135 % The format of the ModuleComponentTerminus method is:
1137 % ModuleComponentTerminus(void)
1140 MagickExport void ModuleComponentTerminus(void)
1142 if (module_semaphore == (SemaphoreInfo *) NULL)
1143 AcquireSemaphoreInfo(&module_semaphore);
1144 DestroyModuleList();
1145 DestroySemaphoreInfo(&module_semaphore);
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153 % O p e n M o d u l e %
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159 % OpenModule() loads a module, and invokes its registration module. It
1160 % returns MagickTrue on success, and MagickFalse if there is an error.
1162 % The format of the OpenModule module is:
1164 % MagickBooleanType OpenModule(const char *module,ExceptionInfo *exception)
1166 % A description of each parameter follows:
1168 % o module: a character string that indicates the module to load.
1170 % o exception: return any errors or warnings in this structure.
1173 MagickExport MagickBooleanType OpenModule(const char *module,
1174 ExceptionInfo *exception)
1177 filename[MaxTextExtent],
1178 module_name[MaxTextExtent],
1179 name[MaxTextExtent],
1180 path[MaxTextExtent];
1188 register const CoderInfo
1198 Assign module name from alias.
1200 assert(module != (const char *) NULL);
1201 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1202 if (module_info != (ModuleInfo *) NULL)
1204 (void) CopyMagickString(module_name,module,MaxTextExtent);
1205 p=GetCoderInfo(module,exception);
1206 if (p != (CoderInfo *) NULL)
1207 (void) CopyMagickString(module_name,p->name,MaxTextExtent);
1208 if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
1209 return(MagickTrue); /* module already opened, return */
1213 handle=(ModuleHandle) NULL;
1214 TagToCoderModuleName(module_name,filename);
1215 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1216 "Searching for module \"%s\" using filename \"%s\"",module_name,filename);
1218 length=GetMagickModulePath(filename,MagickImageCoderModule,path,exception);
1220 return(MagickFalse);
1224 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1225 "Opening module at path \"%s\"",path);
1226 handle=(ModuleHandle) lt_dlopen(path);
1227 if (handle == (ModuleHandle) NULL)
1229 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1230 "UnableToLoadModule","`%s': %s",path,lt_dlerror());
1231 return(MagickFalse);
1236 module_info=AcquireModuleInfo(path,module_name);
1237 module_info->handle=handle;
1238 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1239 return(MagickFalse);
1241 Define RegisterFORMATImage method.
1243 TagToModuleName(module_name,"Register%sImage",name);
1244 module_info->register_module=(unsigned long (*)(void)) lt_dlsym(handle,name);
1245 if (module_info->register_module == (unsigned long (*)(void)) NULL)
1247 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1248 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1249 return(MagickFalse);
1251 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1252 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1253 (void *) module_info->register_module);
1255 Define UnregisterFORMATImage method.
1257 TagToModuleName(module_name,"Unregister%sImage",name);
1258 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1259 if (module_info->unregister_module == (void (*)(void)) NULL)
1261 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1262 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1263 return(MagickFalse);
1265 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1266 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1267 (void *) module_info->unregister_module);
1268 signature=module_info->register_module();
1269 if (signature != MagickImageCoderSignature)
1271 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1272 "ImageCoderSignatureMismatch","`%s': %8lx != %8lx",module_name,
1273 signature,MagickImageCoderSignature);
1274 return(MagickFalse);
1280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284 % O p e n M o d u l e s %
1288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1290 % OpenModules() loads all available modules.
1292 % The format of the OpenModules module is:
1294 % MagickBooleanType OpenModules(ExceptionInfo *exception)
1296 % A description of each parameter follows:
1298 % o exception: return any errors or warnings in this structure.
1301 MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
1315 (void) GetMagickInfo((char *) NULL,exception);
1317 modules=GetModuleList("*",&number_modules,exception);
1318 if (modules == (char **) NULL)
1319 return(MagickFalse);
1320 for (i=0; i < (long) number_modules; i++)
1321 (void) OpenModule(modules[i],exception);
1323 Relinquish resources.
1325 for (i=0; i < (long) number_modules; i++)
1326 modules[i]=DestroyString(modules[i]);
1327 modules=(char **) RelinquishMagickMemory(modules);
1332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1336 % R e g i s t e r M o d u l e %
1340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342 % RegisterModule() adds an entry to the module list. It returns a pointer to
1343 % the registered entry on success.
1345 % The format of the RegisterModule module is:
1347 % ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1348 % ExceptionInfo *exception)
1350 % A description of each parameter follows:
1352 % o info: a pointer to the registered entry is returned.
1354 % o module_info: a pointer to the ModuleInfo structure to register.
1356 % o exception: return any errors or warnings in this structure.
1359 static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1360 ExceptionInfo *exception)
1365 assert(module_info != (ModuleInfo *) NULL);
1366 assert(module_info->signature == MagickSignature);
1367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1368 if (module_list == (SplayTreeInfo *) NULL)
1369 return((const ModuleInfo *) NULL);
1370 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1371 if (status == MagickFalse)
1372 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1373 "MemoryAllocationFailed","`%s'",module_info->tag);
1374 return(module_info);
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % T a g T o C o d e r M o d u l e N a m e %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % TagToCoderModuleName() munges a module tag and obtains the filename of the
1389 % corresponding module.
1391 % The format of the TagToCoderModuleName module is:
1393 % char *TagToCoderModuleName(const char *tag,char *name)
1395 % A description of each parameter follows:
1397 % o tag: a character string representing the module tag.
1399 % o name: return the module name here.
1402 static void TagToCoderModuleName(const char *tag,char *name)
1404 assert(tag != (char *) NULL);
1405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1406 assert(name != (char *) NULL);
1407 #if defined(MAGICKCORE_LTDL_DELEGATE)
1408 (void) FormatMagickString(name,MaxTextExtent,"%s.la",tag);
1409 (void) LocaleLower(name);
1411 #if defined(__WINDOWS__)
1412 if (LocaleNCompare("IM_MOD_",tag,7) == 0)
1413 (void) CopyMagickString(name,tag,MaxTextExtent);
1417 (void) FormatMagickString(name,MaxTextExtent,"IM_MOD_DB_%s_.dll",tag);
1419 (void) FormatMagickString(name,MaxTextExtent,"IM_MOD_RL_%s_.dll",tag);
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 % T a g T o F i l t e r M o d u l e N a m e %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 % TagToFilterModuleName() munges a module tag and returns the filename of the
1438 % corresponding filter module.
1440 % The format of the TagToFilterModuleName module is:
1442 % void TagToFilterModuleName(const char *tag,char name)
1444 % A description of each parameter follows:
1446 % o tag: a character string representing the module tag.
1448 % o name: return the filter name here.
1451 static void TagToFilterModuleName(const char *tag,char *name)
1453 assert(tag != (char *) NULL);
1454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1455 assert(name != (char *) NULL);
1456 #if !defined(MAGICKCORE_LTDL_DELEGATE)
1457 (void) FormatMagickString(name,MaxTextExtent,"%s.dll",tag);
1459 (void) FormatMagickString(name,MaxTextExtent,"%s.la",tag);
1460 (void) LocaleLower(name);
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469 % T a g T o M o d u l e N a m e %
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475 % TagToModuleName() munges the module tag name and returns an upper-case tag
1476 % name as the input string, and a user-provided format.
1478 % The format of the TagToModuleName module is:
1480 % TagToModuleName(const char *tag,const char *format,char *module)
1482 % A description of each parameter follows:
1484 % o tag: the module tag.
1486 % o format: a sprintf-compatible format string containing %s where the
1487 % upper-case tag name is to be inserted.
1489 % o module: pointer to a destination buffer for the formatted result.
1492 static void TagToModuleName(const char *tag,const char *format,char *module)
1495 name[MaxTextExtent];
1497 assert(tag != (const char *) NULL);
1498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1499 assert(format != (const char *) NULL);
1500 assert(module != (char *) NULL);
1501 (void) CopyMagickString(name,tag,MaxTextExtent);
1503 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1504 (void) FormatMagickString(module,MaxTextExtent,format,name);
1508 prefix_format[MaxTextExtent];
1510 (void) FormatMagickString(prefix_format,MaxTextExtent,"%s%s",
1511 MAGICKCORE_NAMESPACE_PREFIX,format);
1512 (void) FormatMagickString(module,MaxTextExtent,prefix_format,name);
1518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1522 % U n r e g i s t e r M o d u l e %
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 % UnregisterModule() unloads a module, and invokes its de-registration module.
1529 % Returns MagickTrue on success, and MagickFalse if there is an error.
1531 % The format of the UnregisterModule module is:
1533 % MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1534 % ExceptionInfo *exception)
1536 % A description of each parameter follows:
1538 % o module_info: the module info.
1540 % o exception: return any errors or warnings in this structure.
1543 static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1544 ExceptionInfo *exception)
1547 Locate and execute UnregisterFORMATImage module.
1549 assert(module_info != (const ModuleInfo *) NULL);
1550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1551 assert(exception != (ExceptionInfo *) NULL);
1552 if (module_info->unregister_module == NULL)
1554 module_info->unregister_module();
1555 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1557 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1558 "UnableToCloseModule","`%s': %s",module_info->tag,lt_dlerror());
1559 return(MagickFalse);
1564 MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1565 ExceptionInfo *magick_unused(exception))
1570 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
1571 Image **image,const int argc,const char **argv,ExceptionInfo *exception)
1573 #if !defined(MAGICKCORE_BUILD_MODULES)
1575 extern unsigned long
1576 analyzeImage(Image **,const int,const char **,ExceptionInfo *),
1577 convolveImage(Image **,const int,const char **,ExceptionInfo *);
1582 image_filter=(ImageFilterHandler *) NULL;
1583 if (LocaleCompare("analyze",tag) == 0)
1584 image_filter=analyzeImage;
1585 if (LocaleCompare("convolve",tag) == 0)
1586 image_filter=convolveImage;
1587 if (image_filter != (ImageFilterHandler *) NULL)
1592 signature=image_filter(image,argc,argv,exception);
1593 if (signature != MagickImageFilterSignature)
1595 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1596 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,signature,
1597 MagickImageFilterSignature);
1598 return(MagickFalse);