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-2009 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 InitializeModuleList(ExceptionInfo *),
105 UnregisterModule(const ModuleInfo *,ExceptionInfo *);
108 TagToCoderModuleName(const char *,char *),
109 TagToFilterModuleName(const char *,char *),
110 TagToModuleName(const char *,const char *,char *);
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % A c q u i r e M o d u l e I n f o %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % AcquireModuleInfo() allocates the ModuleInfo structure.
125 % The format of the AcquireModuleInfo method is:
127 % ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
129 % A description of each parameter follows:
131 % o path: the path associated with the tag.
133 % o tag: a character string that represents the image format we are
137 MagickExport ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
142 module_info=(ModuleInfo *) AcquireMagickMemory(sizeof(*module_info));
143 if (module_info == (ModuleInfo *) NULL)
144 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
145 (void) ResetMagickMemory(module_info,0,sizeof(*module_info));
146 if (path != (const char *) NULL)
147 module_info->path=ConstantString(path);
148 if (tag != (const char *) NULL)
149 module_info->tag=ConstantString(tag);
150 module_info->timestamp=time(0);
151 module_info->signature=MagickSignature;
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % D e s t r o y M o d u l e L i s t %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % DestroyModuleList() unregisters any previously loaded modules and exits
167 % the module loaded environment.
169 % The format of the DestroyModuleList module is:
171 % void DestroyModuleList(void)
174 MagickExport void DestroyModuleList(void)
177 Destroy magick modules.
179 AcquireSemaphoreInfo(&module_semaphore);
180 #if defined(MAGICKCORE_MODULES_SUPPORT)
181 if (module_list != (SplayTreeInfo *) NULL)
182 module_list=DestroySplayTree(module_list);
183 if (instantiate_module != MagickFalse)
186 instantiate_module=MagickFalse;
187 RelinquishSemaphoreInfo(module_semaphore);
188 DestroySemaphoreInfo(&module_semaphore);
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 % G e t M o d u l e I n f o %
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 % GetModuleInfo() returns a pointer to a ModuleInfo structure that matches the
203 % specified tag. If tag is NULL, the head of the module list is returned. If
204 % no modules are loaded, or the requested module is not found, NULL is
207 % The format of the GetModuleInfo module is:
209 % ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
211 % A description of each parameter follows:
213 % o tag: a character string that represents the image format we are
216 % o exception: return any errors or warnings in this structure.
219 MagickExport ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
221 if ((module_list == (SplayTreeInfo *) NULL) ||
222 (instantiate_module == MagickFalse))
223 if (InitializeModuleList(exception) == MagickFalse)
224 return((ModuleInfo *) NULL);
225 if ((module_list == (SplayTreeInfo *) NULL) ||
226 (GetNumberOfNodesInSplayTree(module_list) == 0))
227 return((ModuleInfo *) NULL);
228 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
233 #if defined(MAGICKCORE_MODULES_SUPPORT)
234 if (LocaleCompare(tag,"*") == 0)
235 (void) OpenModules(exception);
237 AcquireSemaphoreInfo(&module_semaphore);
238 ResetSplayTreeIterator(module_list);
239 p=(ModuleInfo *) GetNextValueInSplayTree(module_list);
240 RelinquishSemaphoreInfo(module_semaphore);
243 return((ModuleInfo *) GetValueFromSplayTree(module_list,tag));
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % G e t M o d u l e I n f o L i s t %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % GetModuleInfoList() returns any modules that match the specified pattern.
259 % The format of the GetModuleInfoList function is:
261 % const ModuleInfo **GetModuleInfoList(const char *pattern,
262 % unsigned long *number_modules,ExceptionInfo *exception)
264 % A description of each parameter follows:
266 % o pattern: Specifies a pointer to a text string containing a pattern.
268 % o number_modules: This integer returns the number of modules in the list.
270 % o exception: return any errors or warnings in this structure.
274 #if defined(__cplusplus) || defined(c_plusplus)
278 static int ModuleInfoCompare(const void *x,const void *y)
284 p=(const ModuleInfo **) x,
285 q=(const ModuleInfo **) y;
286 if (LocaleCompare((*p)->path,(*q)->path) == 0)
287 return(LocaleCompare((*p)->tag,(*q)->tag));
288 return(LocaleCompare((*p)->path,(*q)->path));
291 #if defined(__cplusplus) || defined(c_plusplus)
295 MagickExport const ModuleInfo **GetModuleInfoList(const char *pattern,
296 unsigned long *number_modules,ExceptionInfo *exception)
301 register const ModuleInfo
308 Allocate module list.
310 assert(pattern != (char *) NULL);
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
312 assert(number_modules != (unsigned long *) NULL);
314 p=GetModuleInfo("*",exception);
315 if (p == (const ModuleInfo *) NULL)
316 return((const ModuleInfo **) NULL);
317 modules=(const ModuleInfo **) AcquireQuantumMemory((size_t)
318 GetNumberOfNodesInSplayTree(module_list)+1UL,sizeof(*modules));
319 if (modules == (const ModuleInfo **) NULL)
320 return((const ModuleInfo **) NULL);
322 Generate module list.
324 AcquireSemaphoreInfo(&module_semaphore);
325 ResetSplayTreeIterator(module_list);
326 p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
327 for (i=0; p != (const ModuleInfo *) NULL; )
329 if ((p->stealth == MagickFalse) &&
330 (GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
332 p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
334 RelinquishSemaphoreInfo(module_semaphore);
335 qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleInfoCompare);
336 modules[i]=(ModuleInfo *) NULL;
337 *number_modules=(unsigned long) i;
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % G e t M o d u l e L i s t %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % GetModuleList() returns any image format modules that match the specified
355 % The format of the GetModuleList function is:
357 % char **GetModuleList(const char *pattern,unsigned long *number_modules,
358 % ExceptionInfo *exception)
360 % A description of each parameter follows:
362 % o pattern: Specifies a pointer to a text string containing a pattern.
364 % o number_modules: This integer returns the number of modules in the
367 % o exception: return any errors or warnings in this structure.
371 #if defined(__cplusplus) || defined(c_plusplus)
375 static int ModuleCompare(const void *x,const void *y)
383 return(LocaleCompare(*p,*q));
386 #if defined(__cplusplus) || defined(c_plusplus)
390 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
391 struct dirent **result)
393 #if defined(MAGICKCORE_HAVE_READDIR_R)
394 return(readdir_r(directory,entry,result));
398 *result=readdir(directory);
403 MagickExport char **GetModuleList(const char *pattern,
404 unsigned long *number_modules,ExceptionInfo *exception)
408 filename[MaxTextExtent],
409 module_path[MaxTextExtent],
432 Locate all modules in the coder path.
434 TagToCoderModuleName("magick",filename);
435 length=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
438 return((char **) NULL);
439 GetPathComponent(module_path,HeadPath,path);
441 modules=(char **) AcquireQuantumMemory((size_t) max_entries+1UL,
443 if (modules == (char **) NULL)
444 return((char **) NULL);
445 *modules=(char *) NULL;
446 directory=opendir(path);
447 if (directory == (DIR *) NULL)
449 modules=(char **) RelinquishMagickMemory(modules);
450 return((char **) NULL);
452 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
453 if (buffer == (struct dirent *) NULL)
454 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
456 while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
457 (entry != (struct dirent *) NULL))
459 status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
460 if (status == MagickFalse)
462 if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
464 if (i >= (long) max_entries)
466 modules=(char **) NULL;
467 if (~max_entries > max_entries)
468 modules=(char **) ResizeQuantumMemory(modules,(size_t)
469 (max_entries << 1),sizeof(*modules));
471 if (modules == (char **) NULL)
475 Add new module name to list.
477 modules[i]=AcquireString((char *) NULL);
478 GetPathComponent(entry->d_name,BasePath,modules[i]);
479 LocaleUpper(modules[i]);
480 if (LocaleNCompare("IM_MOD_",modules[i],7) == 0)
482 (void) CopyMagickString(modules[i],modules[i]+10,MaxTextExtent);
483 modules[i][strlen(modules[i])-1]='\0';
487 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
488 (void) closedir(directory);
489 if (modules == (char **) NULL)
491 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
492 "MemoryAllocationFailed","`%s'",pattern);
493 return((char **) NULL);
495 qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleCompare);
496 modules[i]=(char *) NULL;
497 *number_modules=(unsigned long) i;
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % G e t M a g i c k M o d u l e P a t h %
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 % GetMagickModulePath() finds a module with the specified module type and
515 % The format of the GetMagickModulePath module is:
517 % MagickBooleanType GetMagickModulePath(const char *filename,
518 % MagickModuleType module_type,char *path,ExceptionInfo *exception)
520 % A description of each parameter follows:
522 % o filename: the module file name.
524 % o module_type: the module type: MagickImageCoderModule or
525 % MagickImageFilterModule.
527 % o path: the path associated with the filename.
529 % o exception: return any errors or warnings in this structure.
532 static MagickBooleanType GetMagickModulePath(const char *filename,
533 MagickModuleType module_type,char *path,ExceptionInfo *exception)
538 assert(filename != (const char *) NULL);
539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
540 assert(path != (char *) NULL);
541 assert(exception != (ExceptionInfo *) NULL);
542 (void) CopyMagickString(path,filename,MaxTextExtent);
543 module_path=(char *) NULL;
546 case MagickImageCoderModule:
549 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
550 "Searching for coder module file \"%s\" ...",filename);
551 module_path=GetEnvironmentValue("MAGICK_CODER_MODULE_PATH");
552 #if defined(MAGICKCORE_CODER_PATH)
553 if (module_path == (char *) NULL)
554 module_path=AcquireString(MAGICKCORE_CODER_PATH);
558 case MagickImageFilterModule:
560 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
561 "Searching for filter module file \"%s\" ...",filename);
562 module_path=GetEnvironmentValue("MAGICK_CODER_FILTER_PATH");
563 #if defined(MAGICKCORE_FILTER_PATH)
564 if (module_path == (char *) NULL)
565 module_path=AcquireString(MAGICKCORE_FILTER_PATH);
570 if (module_path != (char *) NULL)
576 for (p=module_path-1; p != (char *) NULL; )
578 (void) CopyMagickString(path,p+1,MaxTextExtent);
579 q=strchr(path,DirectoryListSeparator);
580 if (q != (char *) NULL)
582 q=path+strlen(path)-1;
583 if ((q >= path) && (*q != *DirectorySeparator))
584 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
585 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
586 if (IsPathAccessible(path) != MagickFalse)
588 module_path=DestroyString(module_path);
591 p=strchr(p+1,DirectoryListSeparator);
593 module_path=DestroyString(module_path);
595 #if defined(MAGICKCORE_INSTALLED_SUPPORT)
597 #if defined(MAGICKCORE_CODER_PATH)
603 Search hard coded paths.
607 case MagickImageCoderModule:
610 directory=MAGICKCORE_CODER_PATH;
613 case MagickImageFilterModule:
615 directory=MAGICKCORE_FILTER_PATH;
619 (void) FormatMagickString(path,MaxTextExtent,"%s%s",directory,filename);
620 if (IsPathAccessible(path) == MagickFalse)
622 ThrowFileException(exception,ConfigureWarning,
623 "UnableToOpenModuleFile",path);
629 #if defined(__WINDOWS__)
638 Locate path via registry key.
642 case MagickImageCoderModule:
645 registery_key="CoderModulesPath";
648 case MagickImageFilterModule:
650 registery_key="FilterModulesPath";
654 key_value=NTRegistryKeyLookup(registery_key);
655 if (key_value == (unsigned char *) NULL)
657 ThrowMagickException(exception,GetMagickModule(),ConfigureError,
658 "RegistryKeyLookupFailed","`%s'",registery_key);
661 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",(char *) key_value,
662 DirectorySeparator,filename);
663 key_value=(unsigned char *) RelinquishMagickMemory(key_value);
664 if (IsPathAccessible(path) == MagickFalse)
666 ThrowFileException(exception,ConfigureWarning,
667 "UnableToOpenModuleFile",path);
674 #if !defined(MAGICKCORE_CODER_PATH) && !defined(__WINDOWS__)
675 # error MAGICKCORE_CODER_PATH or __WINDOWS__ must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
682 home=GetEnvironmentValue("MAGICK_HOME");
683 if (home != (char *) NULL)
688 #if !defined(MAGICKCORE_POSIX_SUPPORT)
689 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",home,
690 DirectorySeparator,filename);
697 case MagickImageCoderModule:
700 directory=MAGICKCORE_CODER_RELATIVE_PATH;
703 case MagickImageFilterModule:
705 directory=MAGICKCORE_FILTER_RELATIVE_PATH;
709 (void) FormatMagickString(path,MaxTextExtent,"%s/lib/%s/%s",home,
712 home=DestroyString(home);
713 if (IsPathAccessible(path) != MagickFalse)
717 if (*GetClientPath() != '\0')
720 Search based on executable directory.
722 #if !defined(MAGICKCORE_POSIX_SUPPORT)
723 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
724 DirectorySeparator,filename);
727 prefix[MaxTextExtent];
734 case MagickImageCoderModule:
740 case MagickImageFilterModule:
746 (void) CopyMagickString(prefix,GetClientPath(),MaxTextExtent);
747 ChopPathComponents(prefix,1);
748 (void) FormatMagickString(path,MaxTextExtent,
749 "%s/lib/%s/modules-Q%d/%s/%s",prefix,MAGICKCORE_LIBRARY_RELATIVE_PATH,
750 MAGICKCORE_QUANTUM_DEPTH,directory,filename);
752 if (IsPathAccessible(path) != MagickFalse)
755 #if defined(__WINDOWS__)
760 if ((NTGetModulePath("CORE_RL_magick_.dll",path) != MagickFalse) ||
761 (NTGetModulePath("CORE_DB_magick_.dll",path) != MagickFalse) ||
762 (NTGetModulePath("Magick.dll",path) != MagickFalse))
764 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
765 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
766 if (IsPathAccessible(path) != MagickFalse)
775 home=GetEnvironmentValue("HOME");
776 if (home == (char *) NULL)
777 home=GetEnvironmentValue("USERPROFILE");
778 if (home != (char *) NULL)
781 Search $HOME/.magick.
783 (void) FormatMagickString(path,MaxTextExtent,"%s%s.magick%s%s",home,
784 DirectorySeparator,DirectorySeparator,filename);
785 home=DestroyString(home);
786 if (IsPathAccessible(path) != MagickFalse)
791 Search current directory.
793 if (IsPathAccessible(path) != MagickFalse)
795 if (exception->severity < ConfigureError)
796 ThrowFileException(exception,ConfigureWarning,"UnableToOpenModuleFile",
804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808 + I n i t i a l i z e M o d u l e L i s t %
812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814 % InitializeModuleList() initializes the module loader.
816 % The format of the InitializeModuleList() method is:
818 % InitializeModuleList(Exceptioninfo *exception)
820 % A description of each parameter follows.
822 % o exception: return any errors or warnings in this structure.
826 static void *DestroyModuleNode(void *module_info)
834 exception=AcquireExceptionInfo();
835 p=(ModuleInfo *) module_info;
836 if (UnregisterModule(p,exception) == MagickFalse)
837 CatchException(exception);
838 if (p->tag != (char *) NULL)
839 p->tag=DestroyString(p->tag);
840 if (p->path != (char *) NULL)
841 p->path=DestroyString(p->path);
842 exception=DestroyExceptionInfo(exception);
843 return(RelinquishMagickMemory(p));
846 static MagickBooleanType InitializeModuleList(
847 ExceptionInfo *magick_unused(exception))
849 if ((module_list == (SplayTreeInfo *) NULL) &&
850 (instantiate_module == MagickFalse))
852 AcquireSemaphoreInfo(&module_semaphore);
853 if ((module_list == (SplayTreeInfo *) NULL) &&
854 (instantiate_module == MagickFalse))
862 module_list=NewSplayTree(CompareSplayTreeString,
863 (void *(*)(void *)) NULL,DestroyModuleNode);
864 if (module_list == (SplayTreeInfo *) NULL)
865 ThrowFatalException(ResourceLimitFatalError,
866 "MemoryAllocationFailed");
867 module_info=AcquireModuleInfo((const char *) NULL,"[boot-strap]");
868 module_info->stealth=MagickTrue;
869 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
870 if (status == MagickFalse)
871 ThrowFatalException(ResourceLimitFatalError,
872 "MemoryAllocationFailed");
873 if (lt_dlinit() != 0)
874 ThrowFatalException(ModuleFatalError,
875 "UnableToInitializeModuleLoader");
876 instantiate_module=MagickTrue;
878 RelinquishSemaphoreInfo(module_semaphore);
880 return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888 % I n v o k e D y n a m i c I m a g e F i l t e r %
892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 % InvokeDynamicImageFilter() invokes a dynamic image filter.
896 % The format of the InvokeDynamicImageFilter module is:
898 % MagickBooleanType InvokeDynamicImageFilter(const char *tag,Image **image,
899 % const int argc,const char **argv,ExceptionInfo *exception)
901 % A description of each parameter follows:
903 % o tag: a character string that represents the name of the particular
906 % o image: the image.
908 % o argc: a pointer to an integer describing the number of elements in the
911 % o argv: a pointer to a text array containing the command line arguments.
913 % o exception: return any errors or warnings in this structure.
916 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
917 Image **images,const int argc,const char **argv,ExceptionInfo *exception)
938 assert(images != (Image **) NULL);
939 assert((*images)->signature == MagickSignature);
940 if ((*images)->debug != MagickFalse)
941 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
942 (*images)->filename);
943 #if !defined(MAGICKCORE_BUILD_MODULES)
948 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
949 if (status != MagickFalse)
953 rights=ReadPolicyRights;
954 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
956 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
957 "NotAuthorized","`%s'",tag);
960 TagToFilterModuleName(tag,name);
961 length=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
967 handle=(ModuleHandle) lt_dlopen(path);
968 if (handle == (ModuleHandle) NULL)
970 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
971 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
977 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
978 (void) FormatMagickString(name,MaxTextExtent,"%sImage",tag);
980 (void) FormatMagickString(name,MaxTextExtent,"%s%sImage",
981 MAGICKCORE_NAMESPACE_PREFIX,tag);
986 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
987 if (image_filter == (ImageFilterHandler *) NULL)
988 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
989 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
995 if ((*images)->debug != MagickFalse)
996 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
997 "Invoking \"%s\" dynamic image filter",tag);
998 signature=image_filter(images,argc,argv,exception);
999 if ((*images)->debug != MagickFalse)
1000 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),"\"%s\" completes",
1002 if (signature != MagickImageFilterSignature)
1004 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1005 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,signature,
1006 MagickImageFilterSignature);
1007 return(MagickFalse);
1013 if (lt_dlclose(handle) != 0)
1015 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1016 "UnableToCloseModule","`%s': %s",name,lt_dlerror());
1017 return(MagickFalse);
1023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 % L i s t M o d u l e I n f o %
1031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033 % ListModuleInfo() lists the module info to a file.
1035 % The format of the ListModuleInfo module is:
1037 % MagickBooleanType ListModuleInfo(FILE *file,ExceptionInfo *exception)
1039 % A description of each parameter follows.
1041 % o file: An pointer to a FILE.
1043 % o exception: return any errors or warnings in this structure.
1046 MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1047 ExceptionInfo *exception)
1058 if (file == (const FILE *) NULL)
1060 module_info=GetModuleInfoList("*",&number_modules,exception);
1061 if (module_info == (const ModuleInfo **) NULL)
1062 return(MagickFalse);
1063 if (module_info[0]->path != (char *) NULL)
1066 path[MaxTextExtent];
1068 GetPathComponent(module_info[0]->path,HeadPath,path);
1069 (void) fprintf(file,"\nPath: %s\n\n",path);
1071 (void) fprintf(file,"Module\n");
1072 (void) fprintf(file,"-------------------------------------------------"
1073 "------------------------------\n");
1074 for (i=0; i < (long) number_modules; i++)
1076 if (module_info[i]->stealth != MagickFalse)
1078 (void) fprintf(file,"%s",module_info[i]->tag);
1079 (void) fprintf(file,"\n");
1081 (void) fflush(file);
1082 module_info=(const ModuleInfo **)
1083 RelinquishMagickMemory((void *) module_info);
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 % O p e n M o d u l e %
1096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1098 % OpenModule() loads a module, and invokes its registration module. It
1099 % returns MagickTrue on success, and MagickFalse if there is an error.
1101 % The format of the OpenModule module is:
1103 % MagickBooleanType OpenModule(const char *module,ExceptionInfo *exception)
1105 % A description of each parameter follows:
1107 % o module: a character string that indicates the module to load.
1109 % o exception: return any errors or warnings in this structure.
1112 MagickExport MagickBooleanType OpenModule(const char *module,
1113 ExceptionInfo *exception)
1116 filename[MaxTextExtent],
1117 module_name[MaxTextExtent],
1118 name[MaxTextExtent],
1119 path[MaxTextExtent];
1127 register const CoderInfo
1137 Assign module name from alias.
1139 assert(module != (const char *) NULL);
1140 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1141 if (module_info != (ModuleInfo *) NULL)
1143 (void) CopyMagickString(module_name,module,MaxTextExtent);
1144 p=GetCoderInfo(module,exception);
1145 if (p != (CoderInfo *) NULL)
1146 (void) CopyMagickString(module_name,p->name,MaxTextExtent);
1147 if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
1148 return(MagickTrue); /* module already opened, return */
1152 handle=(ModuleHandle) NULL;
1153 TagToCoderModuleName(module_name,filename);
1154 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1155 "Searching for module \"%s\" using filename \"%s\"",module_name,filename);
1157 length=GetMagickModulePath(filename,MagickImageCoderModule,path,exception);
1159 return(MagickFalse);
1163 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1164 "Opening module at path \"%s\"",path);
1165 handle=(ModuleHandle) lt_dlopen(path);
1166 if (handle == (ModuleHandle) NULL)
1168 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1169 "UnableToLoadModule","`%s': %s",path,lt_dlerror());
1170 return(MagickFalse);
1175 module_info=AcquireModuleInfo(path,module_name);
1176 module_info->handle=handle;
1177 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1178 return(MagickFalse);
1180 Define RegisterFORMATImage method.
1182 TagToModuleName(module_name,"Register%sImage",name);
1183 module_info->register_module=(unsigned long (*)(void)) lt_dlsym(handle,name);
1184 if (module_info->register_module == (unsigned long (*)(void)) NULL)
1186 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1187 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1188 return(MagickFalse);
1190 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1191 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1192 (void *) module_info->register_module);
1194 Define UnregisterFORMATImage method.
1196 TagToModuleName(module_name,"Unregister%sImage",name);
1197 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1198 if (module_info->unregister_module == (void (*)(void)) NULL)
1200 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1201 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1202 return(MagickFalse);
1204 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1205 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1206 (void *) module_info->unregister_module);
1207 signature=module_info->register_module();
1208 if (signature != MagickImageCoderSignature)
1210 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1211 "ImageCoderSignatureMismatch","`%s': %8lx != %8lx",module_name,
1212 signature,MagickImageCoderSignature);
1213 return(MagickFalse);
1219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223 % O p e n M o d u l e s %
1227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1229 % OpenModules() loads all available modules.
1231 % The format of the OpenModules module is:
1233 % MagickBooleanType OpenModules(ExceptionInfo *exception)
1235 % A description of each parameter follows:
1237 % o exception: return any errors or warnings in this structure.
1240 MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
1254 (void) GetMagickInfo((char *) NULL,exception);
1256 modules=GetModuleList("*",&number_modules,exception);
1257 if (modules == (char **) NULL)
1258 return(MagickFalse);
1259 for (i=0; i < (long) number_modules; i++)
1260 (void) OpenModule(modules[i],exception);
1262 Relinquish resources.
1264 for (i=0; i < (long) number_modules; i++)
1265 modules[i]=DestroyString(modules[i]);
1266 modules=(char **) RelinquishMagickMemory(modules);
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 % R e g i s t e r M o d u l e %
1279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1281 % RegisterModule() adds an entry to the module list. It returns a pointer to
1282 % the registered entry on success.
1284 % The format of the RegisterModule module is:
1286 % ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1287 % ExceptionInfo *exception)
1289 % A description of each parameter follows:
1291 % o info: a pointer to the registered entry is returned.
1293 % o module_info: a pointer to the ModuleInfo structure to register.
1295 % o exception: return any errors or warnings in this structure.
1298 static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1299 ExceptionInfo *exception)
1304 assert(module_info != (ModuleInfo *) NULL);
1305 assert(module_info->signature == MagickSignature);
1306 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1307 if (module_list == (SplayTreeInfo *) NULL)
1308 return((const ModuleInfo *) NULL);
1309 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1310 if (status == MagickFalse)
1311 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1312 "MemoryAllocationFailed","`%s'",module_info->tag);
1313 return(module_info);
1317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1321 % T a g T o C o d e r M o d u l e N a m e %
1325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327 % TagToCoderModuleName() munges a module tag and obtains the filename of the
1328 % corresponding module.
1330 % The format of the TagToCoderModuleName module is:
1332 % char *TagToCoderModuleName(const char *tag,char *name)
1334 % A description of each parameter follows:
1336 % o tag: a character string representing the module tag.
1338 % o name: return the module name here.
1341 static void TagToCoderModuleName(const char *tag,char *name)
1343 assert(tag != (char *) NULL);
1344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1345 assert(name != (char *) NULL);
1346 #if defined(MAGICKCORE_LTDL_DELEGATE)
1347 (void) FormatMagickString(name,MaxTextExtent,"%s.la",tag);
1348 (void) LocaleLower(name);
1350 #if defined(__WINDOWS__)
1351 if (LocaleNCompare("IM_MOD_",tag,7) == 0)
1352 (void) CopyMagickString(name,tag,MaxTextExtent);
1356 (void) FormatMagickString(name,MaxTextExtent,"IM_MOD_DB_%s_.dll",tag);
1358 (void) FormatMagickString(name,MaxTextExtent,"IM_MOD_RL_%s_.dll",tag);
1366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % T a g T o F i l t e r M o d u l e N a m e %
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % TagToFilterModuleName() munges a module tag and returns the filename of the
1377 % corresponding filter module.
1379 % The format of the TagToFilterModuleName module is:
1381 % void TagToFilterModuleName(const char *tag,char name)
1383 % A description of each parameter follows:
1385 % o tag: a character string representing the module tag.
1387 % o name: return the filter name here.
1390 static void TagToFilterModuleName(const char *tag,char *name)
1392 assert(tag != (char *) NULL);
1393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1394 assert(name != (char *) NULL);
1395 #if !defined(MAGICKCORE_LTDL_DELEGATE)
1396 (void) FormatMagickString(name,MaxTextExtent,"%s.dll",tag);
1398 (void) FormatMagickString(name,MaxTextExtent,"%s.la",tag);
1399 (void) LocaleLower(name);
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408 % T a g T o M o d u l e N a m e %
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 % TagToModuleName() munges the module tag name and returns an upper-case tag
1415 % name as the input string, and a user-provided format.
1417 % The format of the TagToModuleName module is:
1419 % TagToModuleName(const char *tag,const char *format,char *module)
1421 % A description of each parameter follows:
1423 % o tag: the module tag.
1425 % o format: a sprintf-compatible format string containing %s where the
1426 % upper-case tag name is to be inserted.
1428 % o module: pointer to a destination buffer for the formatted result.
1431 static void TagToModuleName(const char *tag,const char *format,char *module)
1434 name[MaxTextExtent];
1436 assert(tag != (const char *) NULL);
1437 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1438 assert(format != (const char *) NULL);
1439 assert(module != (char *) NULL);
1440 (void) CopyMagickString(name,tag,MaxTextExtent);
1442 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1443 (void) FormatMagickString(module,MaxTextExtent,format,name);
1447 prefix_format[MaxTextExtent];
1449 (void) FormatMagickString(prefix_format,MaxTextExtent,"%s%s",
1450 MAGICKCORE_NAMESPACE_PREFIX,format);
1451 (void) FormatMagickString(module,MaxTextExtent,prefix_format,name);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % U n r e g i s t e r M o d u l e %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % UnregisterModule() unloads a module, and invokes its de-registration module.
1468 % Returns MagickTrue on success, and MagickFalse if there is an error.
1470 % The format of the UnregisterModule module is:
1472 % MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1473 % ExceptionInfo *exception)
1475 % A description of each parameter follows:
1477 % o module_info: the module info.
1479 % o exception: return any errors or warnings in this structure.
1482 static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1483 ExceptionInfo *exception)
1486 Locate and execute UnregisterFORMATImage module.
1488 assert(module_info != (const ModuleInfo *) NULL);
1489 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1490 assert(exception != (ExceptionInfo *) NULL);
1491 if (module_info->unregister_module == NULL)
1493 module_info->unregister_module();
1494 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1496 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1497 "UnableToCloseModule","`%s': %s",module_info->tag,lt_dlerror());
1498 return(MagickFalse);
1503 MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1504 ExceptionInfo *magick_unused(exception))
1509 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
1510 Image **image,const int argc,const char **argv,ExceptionInfo *exception)
1512 #if !defined(MAGICKCORE_BUILD_MODULES)
1514 extern unsigned long
1515 analyzeImage(Image **,const int,const char **,ExceptionInfo *);
1520 image_filter=(ImageFilterHandler *) NULL;
1521 if (LocaleCompare("analyze",tag) == 0)
1522 image_filter=analyzeImage;
1523 if (image_filter != (ImageFilterHandler *) NULL)
1528 signature=image_filter(image,argc,argv,exception);
1529 if (signature != MagickImageFilterSignature)
1531 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1532 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,signature,
1533 MagickImageFilterSignature);
1534 return(MagickFalse);