43#include "MagickCore/studio.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/coder.h"
46#include "MagickCore/client.h"
47#include "MagickCore/configure.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/log.h"
51#include "MagickCore/linked-list.h"
52#include "MagickCore/magic.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/memory-private.h"
56#include "MagickCore/module.h"
57#include "MagickCore/module-private.h"
58#include "MagickCore/nt-base-private.h"
59#include "MagickCore/policy.h"
60#include "MagickCore/semaphore.h"
61#include "MagickCore/splay-tree.h"
62#include "MagickCore/static.h"
63#include "MagickCore/string_.h"
64#include "MagickCore/string-private.h"
65#include "MagickCore/timer-private.h"
66#include "MagickCore/token.h"
67#include "MagickCore/utility.h"
68#include "MagickCore/utility-private.h"
69#if defined(MAGICKCORE_MODULES_SUPPORT)
70#if defined(MAGICKCORE_LTDL_DELEGATE)
72typedef lt_dlhandle ModuleHandle;
74typedef void *ModuleHandle;
80#if defined(MAGICKCORE_LTDL_DELEGATE)
81# define FilterGlobExpression "*.la"
82# define ModuleGlobExpression "*.la"
85# define FilterGlobExpression "FILTER_DB_*.dll"
86# define ModuleGlobExpression "IM_MOD_DB_*.dll"
88# define FilterGlobExpression "FILTER_RL_*.dll"
89# define ModuleGlobExpression "IM_MOD_RL_*.dll"
100 *module_list = (SplayTreeInfo *) NULL;
105static const ModuleInfo
106 *RegisterModule(
const ModuleInfo *,ExceptionInfo *);
108static MagickBooleanType
109 GetMagickModulePath(
const char *,MagickModuleType,
char *,ExceptionInfo *),
110 IsModuleTreeInstantiated(
void),
111 UnregisterModule(
const ModuleInfo *,ExceptionInfo *);
114 TagToCoderModuleName(
const char *,
char *),
115 TagToFilterModuleName(
const char *,
char *),
116 TagToModuleName(
const char *,
const char *,
char *);
143MagickExport ModuleInfo *AcquireModuleInfo(
const char *path,
const char *tag)
148 module_info=(ModuleInfo *) AcquireCriticalMemory(
sizeof(*module_info));
149 (void) memset(module_info,0,
sizeof(*module_info));
150 if (path != (
const char *) NULL)
151 module_info->path=ConstantString(path);
152 if (tag != (
const char *) NULL)
153 module_info->tag=ConstantString(tag);
154 module_info->timestamp=GetMagickTime();
155 module_info->signature=MagickCoreSignature;
178MagickExport
void DestroyModuleList(
void)
183 LockSemaphoreInfo(module_semaphore);
184#if defined(MAGICKCORE_MODULES_SUPPORT)
185 if (module_list != (SplayTreeInfo *) NULL)
186 module_list=DestroySplayTree(module_list);
188 UnlockSemaphoreInfo(module_semaphore);
219MagickExport ModuleInfo *GetModuleInfo(
const char *tag,ExceptionInfo *exception)
224 if (IsModuleTreeInstantiated() == MagickFalse)
225 return((ModuleInfo *) NULL);
226 LockSemaphoreInfo(module_semaphore);
227 ResetSplayTreeIterator(module_list);
228 if ((tag == (
const char *) NULL) || (LocaleCompare(tag,
"*") == 0))
230#if defined(MAGICKCORE_MODULES_SUPPORT)
231 if (LocaleCompare(tag,
"*") == 0)
232 (void) OpenModules(exception);
234 module_info=(ModuleInfo *) GetNextValueInSplayTree(module_list);
235 UnlockSemaphoreInfo(module_semaphore);
238 module_info=(ModuleInfo *) GetValueFromSplayTree(module_list,tag);
239 UnlockSemaphoreInfo(module_semaphore);
271#if defined(__cplusplus) || defined(c_plusplus)
275static int ModuleInfoCompare(
const void *x,
const void *y)
281 p=(
const ModuleInfo **) x,
282 q=(
const ModuleInfo **) y;
283 if (LocaleCompare((*p)->path,(*q)->path) == 0)
284 return(LocaleCompare((*p)->tag,(*q)->tag));
285 return(LocaleCompare((*p)->path,(*q)->path));
288#if defined(__cplusplus) || defined(c_plusplus)
292MagickExport
const ModuleInfo **GetModuleInfoList(
const char *pattern,
293 size_t *number_modules,ExceptionInfo *exception)
307 assert(pattern != (
char *) NULL);
308 assert(number_modules != (
size_t *) NULL);
309 if (IsEventLogging() != MagickFalse)
310 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",pattern);
312 p=GetModuleInfo(
"*",exception);
313 if (p == (
const ModuleInfo *) NULL)
314 return((
const ModuleInfo **) NULL);
315 modules=(
const ModuleInfo **) AcquireQuantumMemory((
size_t)
316 GetNumberOfNodesInSplayTree(module_list)+1UL,
sizeof(*modules));
317 if (modules == (
const ModuleInfo **) NULL)
318 return((
const ModuleInfo **) NULL);
322 LockSemaphoreInfo(module_semaphore);
323 ResetSplayTreeIterator(module_list);
324 p=(
const ModuleInfo *) GetNextValueInSplayTree(module_list);
325 for (i=0; p != (
const ModuleInfo *) NULL; )
327 if ((p->stealth == MagickFalse) &&
328 (GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
330 p=(
const ModuleInfo *) GetNextValueInSplayTree(module_list);
332 UnlockSemaphoreInfo(module_semaphore);
333 qsort((
void *) modules,(
size_t) i,
sizeof(*modules),ModuleInfoCompare);
334 modules[i]=(ModuleInfo *) NULL;
335 *number_modules=(size_t) i;
371#if defined(__cplusplus) || defined(c_plusplus)
375static int ModuleCompare(
const void *x,
const void *y)
383 return(LocaleCompare(*p,*q));
386#if defined(__cplusplus) || defined(c_plusplus)
390MagickExport
char **GetModuleList(
const char *pattern,
391 const MagickModuleType type,
size_t *number_modules,ExceptionInfo *exception)
393#define MaxModules 511
397 filename[MagickPathExtent],
398 module_path[MagickPathExtent],
399 path[MagickPathExtent];
422 case MagickImageCoderModule:
425 TagToCoderModuleName(
"magick",filename);
426 status=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
430 case MagickImageFilterModule:
432 TagToFilterModuleName(
"analyze",filename);
433 status=GetMagickModulePath(filename,MagickImageFilterModule,module_path,
438 if (status == MagickFalse)
439 return((
char **) NULL);
440 GetPathComponent(module_path,HeadPath,path);
441 max_entries=MaxModules;
442 modules=(
char **) AcquireQuantumMemory((
size_t) max_entries+1UL,
444 if (modules == (
char **) NULL)
445 return((
char **) NULL);
446 *modules=(
char *) NULL;
447 directory=opendir(path);
448 if (directory == (DIR *) NULL)
450 modules=(
char **) RelinquishMagickMemory(modules);
451 return((
char **) NULL);
453 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
454 if (buffer == (
struct dirent *) NULL)
455 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
457 while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
458 (entry != (
struct dirent *) NULL))
460 if (type == MagickImageFilterModule)
461 status=GlobExpression(entry->d_name,FilterGlobExpression,MagickFalse);
463 status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
464 if (status == MagickFalse)
466 if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
468 if (i >= (ssize_t) max_entries)
470 modules=(
char **) NULL;
471 if (~max_entries > max_entries)
472 modules=(
char **) ResizeQuantumMemory(modules,(
size_t)
473 (max_entries << 1),
sizeof(*modules));
475 if (modules == (
char **) NULL)
481 modules[i]=AcquireString((
char *) NULL);
482 GetPathComponent(entry->d_name,BasePath,modules[i]);
483 if (LocaleNCompare(
"IM_MOD_",modules[i],7) == 0)
485 (void) CopyMagickString(modules[i],modules[i]+10,MagickPathExtent);
486 modules[i][strlen(modules[i])-1]=
'\0';
488 else if (LocaleNCompare(
"FILTER_",modules[i],7) == 0)
490 (void) CopyMagickString(modules[i],modules[i]+10,MagickPathExtent);
491 modules[i][strlen(modules[i])-1]=
'\0';
495 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
496 (void) closedir(directory);
497 if (modules == (
char **) NULL)
499 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
500 "MemoryAllocationFailed",
"`%s'",pattern);
501 return((
char **) NULL);
503 qsort((
void *) modules,(
size_t) i,
sizeof(*modules),ModuleCompare);
504 modules[i]=(
char *) NULL;
505 *number_modules=(size_t) i;
540static MagickBooleanType GetMagickModulePath(
const char *filename,
541 MagickModuleType module_type,
char *path,ExceptionInfo *exception)
546 assert(filename != (
const char *) NULL);
547 assert(path != (
char *) NULL);
548 assert(exception != (ExceptionInfo *) NULL);
549 if (IsEventLogging() != MagickFalse)
550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
551 if (strchr(filename,
'/') != (
char *) NULL)
553 (void) CopyMagickString(path,filename,MagickPathExtent);
554 module_path=(
char *) NULL;
557 case MagickImageCoderModule:
560 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
561 "Searching for coder module file \"%s\" ...",filename);
562 module_path=GetEnvironmentValue(
"MAGICK_CODER_MODULE_PATH");
563#if defined(MAGICKCORE_CODER_PATH)
564 if (module_path == (
char *) NULL)
565 module_path=AcquireString(MAGICKCORE_CODER_PATH);
569 case MagickImageFilterModule:
571 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
572 "Searching for filter module file \"%s\" ...",filename);
573 module_path=GetEnvironmentValue(
"MAGICK_FILTER_MODULE_PATH");
574#if defined(MAGICKCORE_FILTER_PATH)
575 if (module_path == (
char *) NULL)
576 module_path=AcquireString(MAGICKCORE_FILTER_PATH);
581 if (module_path != (
char *) NULL)
587 for (p=module_path-1; p != (
char *) NULL; )
589 (void) CopyMagickString(path,p+1,MagickPathExtent);
590 q=strchr(path,DirectoryListSeparator);
591 if (q != (
char *) NULL)
593 q=path+strlen(path)-1;
594 if ((q >= path) && (*q != *DirectorySeparator))
595 (void) ConcatenateMagickString(path,DirectorySeparator,
597 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
600 *real_path = realpath_utf8(path);
602 if (real_path != (
char *) NULL)
604 (void) CopyMagickString(path,real_path,MagickPathExtent);
605 real_path=DestroyString(real_path);
608 if (IsPathAccessible(path) != MagickFalse)
610 module_path=DestroyString(module_path);
613 p=strchr(p+1,DirectoryListSeparator);
615 module_path=DestroyString(module_path);
617#if defined(MAGICKCORE_INSTALLED_SUPPORT)
619#if defined(MAGICKCORE_CODER_PATH)
629 case MagickImageCoderModule:
632 directory=MAGICKCORE_CODER_PATH;
635 case MagickImageFilterModule:
637 directory=MAGICKCORE_FILTER_PATH;
641 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s",directory,
643 if (IsPathAccessible(path) == MagickFalse)
645 ThrowFileException(exception,ConfigureWarning,
646 "UnableToOpenModuleFile",path);
652#if defined(MAGICKCORE_WINDOWS_SUPPORT)
665 case MagickImageCoderModule:
668 registry_key=
"CoderModulesPath";
671 case MagickImageFilterModule:
673 registry_key=
"FilterModulesPath";
677 key_value=NTRegistryKeyLookup(registry_key);
678 if (key_value == (
unsigned char *) NULL)
680 ThrowMagickException(exception,GetMagickModule(),ConfigureError,
681 "RegistryKeyLookupFailed",
"`%s'",registry_key);
684 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",(
char *)
685 key_value,DirectorySeparator,filename);
686 key_value=(
unsigned char *) RelinquishMagickMemory(key_value);
687 if (IsPathAccessible(path) == MagickFalse)
689 ThrowFileException(exception,ConfigureWarning,
690 "UnableToOpenModuleFile",path);
697#if !defined(MAGICKCORE_CODER_PATH) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
698# error MAGICKCORE_CODER_PATH or MAGICKCORE_WINDOWS_SUPPORT must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
705 home=GetEnvironmentValue(
"MAGICK_HOME");
706 if (home != (
char *) NULL)
711#if !defined(MAGICKCORE_POSIX_SUPPORT)
712 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",home,
713 DirectorySeparator,filename);
720 case MagickImageCoderModule:
723 directory=MAGICKCORE_CODER_RELATIVE_PATH;
726 case MagickImageFilterModule:
728 directory=MAGICKCORE_FILTER_RELATIVE_PATH;
732 (void) FormatLocaleString(path,MagickPathExtent,
"%s/lib/%s/%s",home,
735 home=DestroyString(home);
736 if (IsPathAccessible(path) != MagickFalse)
740 if (*GetClientPath() !=
'\0')
745#if !defined(MAGICKCORE_POSIX_SUPPORT)
746 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",GetClientPath(),
747 DirectorySeparator,filename);
750 prefix[MagickPathExtent];
757 case MagickImageCoderModule:
763 case MagickImageFilterModule:
769 (void) CopyMagickString(prefix,GetClientPath(),MagickPathExtent);
770 ChopPathComponents(prefix,1);
771 (void) FormatLocaleString(path,MagickPathExtent,
"%s/lib/%s/%s/%s",prefix,
772 MAGICKCORE_MODULES_RELATIVE_PATH,directory,filename);
774 if (IsPathAccessible(path) != MagickFalse)
777#if defined(MAGICKCORE_WINDOWS_SUPPORT)
782 if ((NTGetModulePath(
"CORE_RL_MagickCore_.dll",path) != MagickFalse) ||
783 (NTGetModulePath(
"CORE_DB_MagickCore_.dll",path) != MagickFalse))
785 (void) ConcatenateMagickString(path,DirectorySeparator,
787 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
788 if (IsPathAccessible(path) != MagickFalse)
797 home=GetEnvironmentValue(
"XDG_CONFIG_HOME");
798 if (home == (
char *) NULL)
799#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
800 home=GetEnvironmentValue(
"LOCALAPPDATA");
801 if (home == (
char *) NULL)
802 home=GetEnvironmentValue(
"APPDATA");
803 if (home == (
char *) NULL)
804 home=GetEnvironmentValue(
"USERPROFILE");
806 if (home != (
char *) NULL)
811 (void) FormatLocaleString(path,MagickPathExtent,
"%s%sImageMagick%s%s",
812 home,DirectorySeparator,DirectorySeparator,filename);
813 home=DestroyString(home);
814 if (IsPathAccessible(path) != MagickFalse)
817 home=GetEnvironmentValue(
"HOME");
818 if (home != (
char *) NULL)
823 (void) FormatLocaleString(path,MagickPathExtent,
824 "%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
825 DirectorySeparator,DirectorySeparator,filename);
826 home=DestroyString(home);
827 if (IsPathAccessible(path) != MagickFalse)
834 if (IsPathAccessible(path) != MagickFalse)
836 if (exception->severity < ConfigureError)
837 ThrowFileException(exception,ConfigureWarning,
"UnableToOpenModuleFile",
863static void *DestroyModuleNode(
void *module_info)
871 exception=AcquireExceptionInfo();
872 p=(ModuleInfo *) module_info;
873 if (UnregisterModule(p,exception) == MagickFalse)
874 CatchException(exception);
875 if (p->tag != (
char *) NULL)
876 p->tag=DestroyString(p->tag);
877 if (p->path != (
char *) NULL)
878 p->path=DestroyString(p->path);
879 exception=DestroyExceptionInfo(exception);
880 return(RelinquishMagickMemory(p));
883static MagickBooleanType IsModuleTreeInstantiated(
void)
885 if (module_list == (SplayTreeInfo *) NULL)
888 ActivateSemaphoreInfo(&module_semaphore);
889 LockSemaphoreInfo(module_semaphore);
890 if (module_list == (SplayTreeInfo *) NULL)
901 splay_tree=NewSplayTree(CompareSplayTreeString,
902 (
void *(*)(
void *)) NULL,DestroyModuleNode);
903 module_info=AcquireModuleInfo((
const char *) NULL,
"[boot-strap]");
904 module_info->stealth=MagickTrue;
905 status=AddValueToSplayTree(splay_tree,module_info->tag,module_info);
906 if (status == MagickFalse)
907 ThrowFatalException(ResourceLimitFatalError,
908 "MemoryAllocationFailed");
909#if defined(MAGICKCORE_LTDL_DELEGATE)
910 if (lt_dlinit() != 0)
911 ThrowFatalException(ModuleFatalError,
912 "UnableToInitializeModuleLoader");
914 module_list=splay_tree;
916 UnlockSemaphoreInfo(module_semaphore);
918 return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
954MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
955 Image **images,
const int argc,
const char **argv,ExceptionInfo *exception)
958 name[MagickPathExtent],
959 path[MagickPathExtent];
976 assert(images != (Image **) NULL);
977 assert((*images)->signature == MagickCoreSignature);
978 if (IsEventLogging() != MagickFalse)
979 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
980 (*images)->filename);
981 rights=ReadPolicyRights;
982 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
983 ThrowPolicyException(tag,MagickFalse);
984#if !defined(MAGICKCORE_BUILD_MODULES)
989 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
990 if (status != MagickFalse)
994 TagToFilterModuleName(tag,name);
995 status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
996 if (status == MagickFalse)
998 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
999 "UnableToLoadModule",
"'%s': %s",name,path);
1000 return(MagickFalse);
1005 handle=(ModuleHandle) lt_dlopen(path);
1006 if (handle == (ModuleHandle) NULL)
1008 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1009 "UnableToLoadModule",
"'%s': %s",name,lt_dlerror());
1010 return(MagickFalse);
1015#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1016 (void) FormatLocaleString(name,MagickPathExtent,
"%sImage",tag);
1018 (void) FormatLocaleString(name,MagickPathExtent,
"%s%sImage",
1019 MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1024 ClearMagickException(exception);
1025 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
1026 if (image_filter == (ImageFilterHandler *) NULL)
1027 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1028 "UnableToLoadModule",
"'%s': %s",name,lt_dlerror());
1034 if (IsEventLogging() != MagickFalse)
1035 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1036 "Invoking \"%s\" dynamic image filter",tag);
1037 signature=image_filter(images,argc,argv,exception);
1038 if (IsEventLogging() != MagickFalse)
1039 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"\"%s\" completes",
1041 if (signature != MagickImageFilterSignature)
1042 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1043 "ImageFilterSignatureMismatch",
"'%s': %8lx != %8lx",tag,
1044 (
unsigned long) signature,(
unsigned long) MagickImageFilterSignature);
1049 if (lt_dlclose(handle) != 0)
1050 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1051 "UnableToCloseModule",
"'%s': %s",name,lt_dlerror());
1052 return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
1079MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1080 ExceptionInfo *exception)
1083 filename[MagickPathExtent],
1084 module_path[MagickPathExtent],
1086 path[MagickPathExtent];
1094 if (file == (
const FILE *) NULL)
1099 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1100 if (modules == (
char **) NULL)
1101 return(MagickFalse);
1102 TagToCoderModuleName(
"magick",filename);
1103 (void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
1105 GetPathComponent(module_path,HeadPath,path);
1106 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1107 (void) FormatLocaleFile(file,
"Image Coder\n");
1108 (void) FormatLocaleFile(file,
1109 "-------------------------------------------------"
1110 "------------------------------\n");
1111 for (i=0; i < (ssize_t) number_modules; i++)
1113 (void) FormatLocaleFile(file,
"%s",modules[i]);
1114 (void) FormatLocaleFile(file,
"\n");
1116 (void) fflush(file);
1120 for (i=0; i < (ssize_t) number_modules; i++)
1121 modules[i]=DestroyString(modules[i]);
1122 modules=(
char **) RelinquishMagickMemory(modules);
1126 modules=GetModuleList(
"*",MagickImageFilterModule,&number_modules,exception);
1127 if (modules == (
char **) NULL)
1128 return(MagickFalse);
1129 TagToFilterModuleName(
"analyze",filename);
1130 (void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
1132 GetPathComponent(module_path,HeadPath,path);
1133 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1134 (void) FormatLocaleFile(file,
"Image Filter\n");
1135 (void) FormatLocaleFile(file,
1136 "-------------------------------------------------"
1137 "------------------------------\n");
1138 for (i=0; i < (ssize_t) number_modules; i++)
1140 (void) FormatLocaleFile(file,
"%s",modules[i]);
1141 (void) FormatLocaleFile(file,
"\n");
1143 (void) fflush(file);
1147 for (i=0; i < (ssize_t) number_modules; i++)
1148 modules[i]=DestroyString(modules[i]);
1149 modules=(
char **) RelinquishMagickMemory(modules);
1171MagickPrivate MagickBooleanType ModuleComponentGenesis(
void)
1177 module_semaphore=AcquireSemaphoreInfo();
1178 status=IsModuleTreeInstantiated();
1200MagickPrivate
void ModuleComponentTerminus(
void)
1203 ActivateSemaphoreInfo(&module_semaphore);
1204 DestroyModuleList();
1205 RelinquishSemaphoreInfo(&module_semaphore);
1233MagickPrivate MagickBooleanType OpenModule(
const char *module,
1234 ExceptionInfo *exception)
1237 module_name[MagickPathExtent],
1238 name[MagickPathExtent],
1239 path[MagickPathExtent];
1262 assert(module != (
const char *) NULL);
1263 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1264 if (module_info != (ModuleInfo *) NULL)
1266 (void) CopyMagickString(module_name,module,MagickPathExtent);
1267 p=GetCoderInfo(module,exception);
1268 if (p != (CoderInfo *) NULL)
1269 (void) CopyMagickString(module_name,p->name,MagickPathExtent);
1270 LocaleUpper(module_name);
1271 rights=(PolicyRights) (ReadPolicyRights | WritePolicyRights);
1272 if (IsRightsAuthorized(ModulePolicyDomain,rights,module_name) == MagickFalse)
1273 ThrowPolicyException(module_name,MagickFalse);
1274 if (GetValueFromSplayTree(module_list,module_name) != (
void *) NULL)
1279 handle=(ModuleHandle) NULL;
1280 TagToCoderModuleName(module_name,name);
1281 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1282 "Searching for module \"%s\" using filename \"%s\"",module_name,name);
1284 status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1285 if (status == MagickFalse)
1286 return(MagickFalse);
1290 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1291 "Opening module at path \"%s\"",path);
1292 handle=(ModuleHandle) lt_dlopen(path);
1293 if (handle == (ModuleHandle) NULL)
1295 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1296 "UnableToLoadModule",
"'%s': %s",path,lt_dlerror());
1297 return(MagickFalse);
1302 module_info=AcquireModuleInfo(path,module_name);
1303 module_info->handle=handle;
1304 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1305 return(MagickFalse);
1309 TagToModuleName(module_name,
"Register%sImage",name);
1310 module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
1311 if (module_info->register_module == (size_t (*)(
void)) NULL)
1313 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1314 "UnableToRegisterImageFormat",
"'%s': %s",module_name,lt_dlerror());
1315 return(MagickFalse);
1317 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1318 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1319 (
void *) module_info->register_module);
1323 TagToModuleName(module_name,
"Unregister%sImage",name);
1324 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1325 if (module_info->unregister_module == (void (*)(
void)) NULL)
1327 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1328 "UnableToRegisterImageFormat",
"'%s': %s",module_name,lt_dlerror());
1329 return(MagickFalse);
1331 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1332 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1333 (
void *) module_info->unregister_module);
1334 signature=module_info->register_module();
1335 if (signature != MagickImageCoderSignature)
1337 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1338 "ImageCoderSignatureMismatch",
"'%s': %8lx != %8lx",module_name,
1339 (
unsigned long) signature,(
unsigned long) MagickImageCoderSignature);
1340 return(MagickFalse);
1367MagickPrivate MagickBooleanType OpenModules(ExceptionInfo *exception)
1381 (void) GetMagickInfo((
char *) NULL,exception);
1383 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1384 if ((modules == (
char **) NULL) || (*modules == (
char *) NULL))
1386 if (modules != (
char **) NULL)
1387 modules=(
char **) RelinquishMagickMemory(modules);
1388 return(MagickFalse);
1390 for (i=0; i < (ssize_t) number_modules; i++)
1391 (
void) OpenModule(modules[i],exception);
1395 for (i=0; i < (ssize_t) number_modules; i++)
1396 modules[i]=DestroyString(modules[i]);
1397 modules=(
char **) RelinquishMagickMemory(modules);
1429static const ModuleInfo *RegisterModule(
const ModuleInfo *module_info,
1430 ExceptionInfo *exception)
1435 assert(module_info != (ModuleInfo *) NULL);
1436 assert(module_info->signature == MagickCoreSignature);
1437 if (IsEventLogging() != MagickFalse)
1438 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1439 if (module_list == (SplayTreeInfo *) NULL)
1440 return((
const ModuleInfo *) NULL);
1441 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1442 if (status == MagickFalse)
1443 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1444 "MemoryAllocationFailed",
"`%s'",module_info->tag);
1445 return(module_info);
1473static void TagToCoderModuleName(
const char *tag,
char *name)
1475 assert(tag != (
char *) NULL);
1476 assert(name != (
char *) NULL);
1477 if (IsEventLogging() != MagickFalse)
1478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1479#if defined(MAGICKCORE_LTDL_DELEGATE)
1480 (void) FormatLocaleString(name,MagickPathExtent,
"%s.la",tag);
1481 (void) LocaleLower(name);
1483#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1484 if (LocaleNCompare(
"IM_MOD_",tag,7) == 0)
1485 (void) CopyMagickString(name,tag,MagickPathExtent);
1489 (void) FormatLocaleString(name,MagickPathExtent,
"IM_MOD_DB_%s_.dll",tag);
1491 (void) FormatLocaleString(name,MagickPathExtent,
"IM_MOD_RL_%s_.dll",tag);
1523static void TagToFilterModuleName(
const char *tag,
char *name)
1525 assert(tag != (
char *) NULL);
1526 assert(name != (
char *) NULL);
1527 if (IsEventLogging() != MagickFalse)
1528 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1529#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1531 (void) FormatLocaleString(name,MagickPathExtent,
"FILTER_DB_%s_.dll",tag);
1533 (void) FormatLocaleString(name,MagickPathExtent,
"FILTER_RL_%s_.dll",tag);
1535#elif !defined(MAGICKCORE_LTDL_DELEGATE)
1536 (void) FormatLocaleString(name,MagickPathExtent,
"%s.dll",tag);
1538 (void) FormatLocaleString(name,MagickPathExtent,
"%s.la",tag);
1570static void TagToModuleName(
const char *tag,
const char *format,
char *module)
1573 name[MagickPathExtent];
1575 assert(tag != (
const char *) NULL);
1576 assert(format != (
const char *) NULL);
1577 assert(module != (
char *) NULL);
1578 if (IsEventLogging() != MagickFalse)
1579 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1580 (void) CopyMagickString(name,tag,MagickPathExtent);
1582#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1583 (void) FormatLocaleString(module,MagickPathExtent,format,name);
1587 prefix_format[MagickPathExtent];
1589 (void) FormatLocaleString(prefix_format,MagickPathExtent,
"%s%s",
1590 MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1591 (void) FormatLocaleString(module,MagickPathExtent,prefix_format,name);
1622static MagickBooleanType UnregisterModule(
const ModuleInfo *module_info,
1623 ExceptionInfo *exception)
1628 assert(module_info != (
const ModuleInfo *) NULL);
1629 assert(exception != (ExceptionInfo *) NULL);
1630 if (IsEventLogging() != MagickFalse)
1631 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1632 if (module_info->unregister_module == NULL)
1634 module_info->unregister_module();
1635 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1637 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1638 "UnableToCloseModule",
"'%s': %s",module_info->tag,lt_dlerror());
1639 return(MagickFalse);
1645#if !defined(MAGICKCORE_BUILD_MODULES)
1647 analyzeImage(Image **,
const int,
const char **,ExceptionInfo *);
1650MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1651 ExceptionInfo *magick_unused(exception))
1653 magick_unreferenced(file);
1654 magick_unreferenced(exception);
1658MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
1659 Image **image,
const int argc,
const char **argv,ExceptionInfo *exception)
1664 assert(image != (Image **) NULL);
1665 assert((*image)->signature == MagickCoreSignature);
1666 if (IsEventLogging() != MagickFalse)
1667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",(*image)->filename);
1668 rights=ReadPolicyRights;
1669 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
1670 ThrowPolicyException(tag,MagickFalse);
1671#if defined(MAGICKCORE_BUILD_MODULES)
1681 image_filter=(ImageFilterHandler *) NULL;
1682 if (LocaleCompare(
"analyze",tag) == 0)
1683 image_filter=(ImageFilterHandler *) analyzeImage;
1684 if (image_filter == (ImageFilterHandler *) NULL)
1685 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1686 "UnableToLoadModule",
"`%s'",tag);
1692 if ((*image)->debug != MagickFalse)
1693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1694 "Invoking \"%s\" static image filter",tag);
1695 signature=image_filter(image,argc,argv,exception);
1696 if ((*image)->debug != MagickFalse)
1697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1698 "\"%s\" completes",tag);
1699 if (signature != MagickImageFilterSignature)
1701 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1702 "ImageFilterSignatureMismatch",
"'%s': %8lx != %8lx",tag,
1703 (
unsigned long) signature,(
unsigned long)
1704 MagickImageFilterSignature);
1705 return(MagickFalse);