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_CODER_FILTER_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);
598#if defined(MAGICKCORE_HAVE_REALPATH)
601 resolved_path[PATH_MAX+1];
603 if (realpath(path,resolved_path) != (
char *) NULL)
604 (void) CopyMagickString(path,resolved_path,MagickPathExtent);
607 if (IsPathAccessible(path) != MagickFalse)
609 module_path=DestroyString(module_path);
612 p=strchr(p+1,DirectoryListSeparator);
614 module_path=DestroyString(module_path);
616#if defined(MAGICKCORE_INSTALLED_SUPPORT)
618#if defined(MAGICKCORE_CODER_PATH)
628 case MagickImageCoderModule:
631 directory=MAGICKCORE_CODER_PATH;
634 case MagickImageFilterModule:
636 directory=MAGICKCORE_FILTER_PATH;
640 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s",directory,
642 if (IsPathAccessible(path) == MagickFalse)
644 ThrowFileException(exception,ConfigureWarning,
645 "UnableToOpenModuleFile",path);
651#if defined(MAGICKCORE_WINDOWS_SUPPORT)
664 case MagickImageCoderModule:
667 registry_key=
"CoderModulesPath";
670 case MagickImageFilterModule:
672 registry_key=
"FilterModulesPath";
676 key_value=NTRegistryKeyLookup(registry_key);
677 if (key_value == (
unsigned char *) NULL)
679 ThrowMagickException(exception,GetMagickModule(),ConfigureError,
680 "RegistryKeyLookupFailed",
"`%s'",registry_key);
683 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",(
char *)
684 key_value,DirectorySeparator,filename);
685 key_value=(
unsigned char *) RelinquishMagickMemory(key_value);
686 if (IsPathAccessible(path) == MagickFalse)
688 ThrowFileException(exception,ConfigureWarning,
689 "UnableToOpenModuleFile",path);
696#if !defined(MAGICKCORE_CODER_PATH) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
697# error MAGICKCORE_CODER_PATH or MAGICKCORE_WINDOWS_SUPPORT must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
704 home=GetEnvironmentValue(
"MAGICK_HOME");
705 if (home != (
char *) NULL)
710#if !defined(MAGICKCORE_POSIX_SUPPORT)
711 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",home,
712 DirectorySeparator,filename);
719 case MagickImageCoderModule:
722 directory=MAGICKCORE_CODER_RELATIVE_PATH;
725 case MagickImageFilterModule:
727 directory=MAGICKCORE_FILTER_RELATIVE_PATH;
731 (void) FormatLocaleString(path,MagickPathExtent,
"%s/lib/%s/%s",home,
734 home=DestroyString(home);
735 if (IsPathAccessible(path) != MagickFalse)
739 if (*GetClientPath() !=
'\0')
744#if !defined(MAGICKCORE_POSIX_SUPPORT)
745 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",GetClientPath(),
746 DirectorySeparator,filename);
749 prefix[MagickPathExtent];
756 case MagickImageCoderModule:
762 case MagickImageFilterModule:
768 (void) CopyMagickString(prefix,GetClientPath(),MagickPathExtent);
769 ChopPathComponents(prefix,1);
770 (void) FormatLocaleString(path,MagickPathExtent,
"%s/lib/%s/%s/%s",prefix,
771 MAGICKCORE_MODULES_RELATIVE_PATH,directory,filename);
773 if (IsPathAccessible(path) != MagickFalse)
776#if defined(MAGICKCORE_WINDOWS_SUPPORT)
781 if ((NTGetModulePath(
"CORE_RL_MagickCore_.dll",path) != MagickFalse) ||
782 (NTGetModulePath(
"CORE_DB_MagickCore_.dll",path) != MagickFalse))
784 (void) ConcatenateMagickString(path,DirectorySeparator,
786 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
787 if (IsPathAccessible(path) != MagickFalse)
796 home=GetEnvironmentValue(
"XDG_CONFIG_HOME");
797 if (home == (
char *) NULL)
798#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
799 home=GetEnvironmentValue(
"LOCALAPPDATA");
800 if (home == (
char *) NULL)
801 home=GetEnvironmentValue(
"APPDATA");
802 if (home == (
char *) NULL)
803 home=GetEnvironmentValue(
"USERPROFILE");
805 if (home != (
char *) NULL)
810 (void) FormatLocaleString(path,MagickPathExtent,
"%s%sImageMagick%s%s",
811 home,DirectorySeparator,DirectorySeparator,filename);
812 home=DestroyString(home);
813 if (IsPathAccessible(path) != MagickFalse)
816 home=GetEnvironmentValue(
"HOME");
817 if (home != (
char *) NULL)
822 (void) FormatLocaleString(path,MagickPathExtent,
823 "%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
824 DirectorySeparator,DirectorySeparator,filename);
825 home=DestroyString(home);
826 if (IsPathAccessible(path) != MagickFalse)
833 if (IsPathAccessible(path) != MagickFalse)
835 if (exception->severity < ConfigureError)
836 ThrowFileException(exception,ConfigureWarning,
"UnableToOpenModuleFile",
862static void *DestroyModuleNode(
void *module_info)
870 exception=AcquireExceptionInfo();
871 p=(ModuleInfo *) module_info;
872 if (UnregisterModule(p,exception) == MagickFalse)
873 CatchException(exception);
874 if (p->tag != (
char *) NULL)
875 p->tag=DestroyString(p->tag);
876 if (p->path != (
char *) NULL)
877 p->path=DestroyString(p->path);
878 exception=DestroyExceptionInfo(exception);
879 return(RelinquishMagickMemory(p));
882static MagickBooleanType IsModuleTreeInstantiated(
void)
884 if (module_list == (SplayTreeInfo *) NULL)
887 ActivateSemaphoreInfo(&module_semaphore);
888 LockSemaphoreInfo(module_semaphore);
889 if (module_list == (SplayTreeInfo *) NULL)
900 splay_tree=NewSplayTree(CompareSplayTreeString,
901 (
void *(*)(
void *)) NULL,DestroyModuleNode);
902 module_info=AcquireModuleInfo((
const char *) NULL,
"[boot-strap]");
903 module_info->stealth=MagickTrue;
904 status=AddValueToSplayTree(splay_tree,module_info->tag,module_info);
905 if (status == MagickFalse)
906 ThrowFatalException(ResourceLimitFatalError,
907 "MemoryAllocationFailed");
908#if defined(MAGICKCORE_LTDL_DELEGATE)
909 if (lt_dlinit() != 0)
910 ThrowFatalException(ModuleFatalError,
911 "UnableToInitializeModuleLoader");
913 module_list=splay_tree;
915 UnlockSemaphoreInfo(module_semaphore);
917 return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
953MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
954 Image **images,
const int argc,
const char **argv,ExceptionInfo *exception)
957 name[MagickPathExtent],
958 path[MagickPathExtent];
975 assert(images != (Image **) NULL);
976 assert((*images)->signature == MagickCoreSignature);
977 if (IsEventLogging() != MagickFalse)
978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
979 (*images)->filename);
980 rights=ReadPolicyRights;
981 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
984 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
985 "NotAuthorized",
"`%s'",tag);
988#if !defined(MAGICKCORE_BUILD_MODULES)
993 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
994 if (status != MagickFalse)
998 TagToFilterModuleName(tag,name);
999 status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
1000 if (status == MagickFalse)
1002 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1003 "UnableToLoadModule",
"'%s': %s",name,path);
1004 return(MagickFalse);
1009 handle=(ModuleHandle) lt_dlopen(path);
1010 if (handle == (ModuleHandle) NULL)
1012 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1013 "UnableToLoadModule",
"'%s': %s",name,lt_dlerror());
1014 return(MagickFalse);
1019#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1020 (void) FormatLocaleString(name,MagickPathExtent,
"%sImage",tag);
1022 (void) FormatLocaleString(name,MagickPathExtent,
"%s%sImage",
1023 MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1028 ClearMagickException(exception);
1029 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
1030 if (image_filter == (ImageFilterHandler *) NULL)
1031 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1032 "UnableToLoadModule",
"'%s': %s",name,lt_dlerror());
1038 if (IsEventLogging() != MagickFalse)
1039 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1040 "Invoking \"%s\" dynamic image filter",tag);
1041 signature=image_filter(images,argc,argv,exception);
1042 if (IsEventLogging() != MagickFalse)
1043 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"\"%s\" completes",
1045 if (signature != MagickImageFilterSignature)
1046 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1047 "ImageFilterSignatureMismatch",
"'%s': %8lx != %8lx",tag,
1048 (
unsigned long) signature,(
unsigned long) MagickImageFilterSignature);
1053 if (lt_dlclose(handle) != 0)
1054 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1055 "UnableToCloseModule",
"'%s': %s",name,lt_dlerror());
1056 return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
1083MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1084 ExceptionInfo *exception)
1087 filename[MagickPathExtent],
1088 module_path[MagickPathExtent],
1090 path[MagickPathExtent];
1098 if (file == (
const FILE *) NULL)
1103 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1104 if (modules == (
char **) NULL)
1105 return(MagickFalse);
1106 TagToCoderModuleName(
"magick",filename);
1107 (void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
1109 GetPathComponent(module_path,HeadPath,path);
1110 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1111 (void) FormatLocaleFile(file,
"Image Coder\n");
1112 (void) FormatLocaleFile(file,
1113 "-------------------------------------------------"
1114 "------------------------------\n");
1115 for (i=0; i < (ssize_t) number_modules; i++)
1117 (void) FormatLocaleFile(file,
"%s",modules[i]);
1118 (void) FormatLocaleFile(file,
"\n");
1120 (void) fflush(file);
1124 for (i=0; i < (ssize_t) number_modules; i++)
1125 modules[i]=DestroyString(modules[i]);
1126 modules=(
char **) RelinquishMagickMemory(modules);
1130 modules=GetModuleList(
"*",MagickImageFilterModule,&number_modules,exception);
1131 if (modules == (
char **) NULL)
1132 return(MagickFalse);
1133 TagToFilterModuleName(
"analyze",filename);
1134 (void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
1136 GetPathComponent(module_path,HeadPath,path);
1137 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1138 (void) FormatLocaleFile(file,
"Image Filter\n");
1139 (void) FormatLocaleFile(file,
1140 "-------------------------------------------------"
1141 "------------------------------\n");
1142 for (i=0; i < (ssize_t) number_modules; i++)
1144 (void) FormatLocaleFile(file,
"%s",modules[i]);
1145 (void) FormatLocaleFile(file,
"\n");
1147 (void) fflush(file);
1151 for (i=0; i < (ssize_t) number_modules; i++)
1152 modules[i]=DestroyString(modules[i]);
1153 modules=(
char **) RelinquishMagickMemory(modules);
1175MagickPrivate MagickBooleanType ModuleComponentGenesis(
void)
1181 module_semaphore=AcquireSemaphoreInfo();
1182 status=IsModuleTreeInstantiated();
1204MagickPrivate
void ModuleComponentTerminus(
void)
1207 ActivateSemaphoreInfo(&module_semaphore);
1208 DestroyModuleList();
1209 RelinquishSemaphoreInfo(&module_semaphore);
1237MagickPrivate MagickBooleanType OpenModule(
const char *module,
1238 ExceptionInfo *exception)
1241 module_name[MagickPathExtent],
1242 name[MagickPathExtent],
1243 path[MagickPathExtent];
1266 assert(module != (
const char *) NULL);
1267 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1268 if (module_info != (ModuleInfo *) NULL)
1270 (void) CopyMagickString(module_name,module,MagickPathExtent);
1271 p=GetCoderInfo(module,exception);
1272 if (p != (CoderInfo *) NULL)
1273 (void) CopyMagickString(module_name,p->name,MagickPathExtent);
1274 LocaleUpper(module_name);
1275 rights=(PolicyRights) (ReadPolicyRights | WritePolicyRights);
1276 if (IsRightsAuthorized(ModulePolicyDomain,rights,module_name) == MagickFalse)
1279 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1280 "NotAuthorized",
"`%s'",module);
1281 return(MagickFalse);
1283 if (GetValueFromSplayTree(module_list,module_name) != (
void *) NULL)
1288 handle=(ModuleHandle) NULL;
1289 TagToCoderModuleName(module_name,name);
1290 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1291 "Searching for module \"%s\" using filename \"%s\"",module_name,name);
1293 status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1294 if (status == MagickFalse)
1295 return(MagickFalse);
1299 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1300 "Opening module at path \"%s\"",path);
1301 handle=(ModuleHandle) lt_dlopen(path);
1302 if (handle == (ModuleHandle) NULL)
1304 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1305 "UnableToLoadModule",
"'%s': %s",path,lt_dlerror());
1306 return(MagickFalse);
1311 module_info=AcquireModuleInfo(path,module_name);
1312 module_info->handle=handle;
1313 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1314 return(MagickFalse);
1318 TagToModuleName(module_name,
"Register%sImage",name);
1319 module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
1320 if (module_info->register_module == (size_t (*)(
void)) NULL)
1322 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1323 "UnableToRegisterImageFormat",
"'%s': %s",module_name,lt_dlerror());
1324 return(MagickFalse);
1326 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1327 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1328 (
void *) module_info->register_module);
1332 TagToModuleName(module_name,
"Unregister%sImage",name);
1333 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1334 if (module_info->unregister_module == (void (*)(
void)) NULL)
1336 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1337 "UnableToRegisterImageFormat",
"'%s': %s",module_name,lt_dlerror());
1338 return(MagickFalse);
1340 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1341 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1342 (
void *) module_info->unregister_module);
1343 signature=module_info->register_module();
1344 if (signature != MagickImageCoderSignature)
1346 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1347 "ImageCoderSignatureMismatch",
"'%s': %8lx != %8lx",module_name,
1348 (
unsigned long) signature,(
unsigned long) MagickImageCoderSignature);
1349 return(MagickFalse);
1376MagickPrivate MagickBooleanType OpenModules(ExceptionInfo *exception)
1390 (void) GetMagickInfo((
char *) NULL,exception);
1392 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1393 if ((modules == (
char **) NULL) || (*modules == (
char *) NULL))
1395 if (modules != (
char **) NULL)
1396 modules=(
char **) RelinquishMagickMemory(modules);
1397 return(MagickFalse);
1399 for (i=0; i < (ssize_t) number_modules; i++)
1400 (
void) OpenModule(modules[i],exception);
1404 for (i=0; i < (ssize_t) number_modules; i++)
1405 modules[i]=DestroyString(modules[i]);
1406 modules=(
char **) RelinquishMagickMemory(modules);
1438static const ModuleInfo *RegisterModule(
const ModuleInfo *module_info,
1439 ExceptionInfo *exception)
1444 assert(module_info != (ModuleInfo *) NULL);
1445 assert(module_info->signature == MagickCoreSignature);
1446 if (IsEventLogging() != MagickFalse)
1447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1448 if (module_list == (SplayTreeInfo *) NULL)
1449 return((
const ModuleInfo *) NULL);
1450 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1451 if (status == MagickFalse)
1452 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1453 "MemoryAllocationFailed",
"`%s'",module_info->tag);
1454 return(module_info);
1482static void TagToCoderModuleName(
const char *tag,
char *name)
1484 assert(tag != (
char *) NULL);
1485 assert(name != (
char *) NULL);
1486 if (IsEventLogging() != MagickFalse)
1487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1488#if defined(MAGICKCORE_LTDL_DELEGATE)
1489 (void) FormatLocaleString(name,MagickPathExtent,
"%s.la",tag);
1490 (void) LocaleLower(name);
1492#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1493 if (LocaleNCompare(
"IM_MOD_",tag,7) == 0)
1494 (void) CopyMagickString(name,tag,MagickPathExtent);
1498 (void) FormatLocaleString(name,MagickPathExtent,
"IM_MOD_DB_%s_.dll",tag);
1500 (void) FormatLocaleString(name,MagickPathExtent,
"IM_MOD_RL_%s_.dll",tag);
1532static void TagToFilterModuleName(
const char *tag,
char *name)
1534 assert(tag != (
char *) NULL);
1535 assert(name != (
char *) NULL);
1536 if (IsEventLogging() != MagickFalse)
1537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1538#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1540 (void) FormatLocaleString(name,MagickPathExtent,
"FILTER_DB_%s_.dll",tag);
1542 (void) FormatLocaleString(name,MagickPathExtent,
"FILTER_RL_%s_.dll",tag);
1544#elif !defined(MAGICKCORE_LTDL_DELEGATE)
1545 (void) FormatLocaleString(name,MagickPathExtent,
"%s.dll",tag);
1547 (void) FormatLocaleString(name,MagickPathExtent,
"%s.la",tag);
1579static void TagToModuleName(
const char *tag,
const char *format,
char *module)
1582 name[MagickPathExtent];
1584 assert(tag != (
const char *) NULL);
1585 assert(format != (
const char *) NULL);
1586 assert(module != (
char *) NULL);
1587 if (IsEventLogging() != MagickFalse)
1588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1589 (void) CopyMagickString(name,tag,MagickPathExtent);
1591#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1592 (void) FormatLocaleString(module,MagickPathExtent,format,name);
1596 prefix_format[MagickPathExtent];
1598 (void) FormatLocaleString(prefix_format,MagickPathExtent,
"%s%s",
1599 MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1600 (void) FormatLocaleString(module,MagickPathExtent,prefix_format,name);
1631static MagickBooleanType UnregisterModule(
const ModuleInfo *module_info,
1632 ExceptionInfo *exception)
1637 assert(module_info != (
const ModuleInfo *) NULL);
1638 assert(exception != (ExceptionInfo *) NULL);
1639 if (IsEventLogging() != MagickFalse)
1640 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1641 if (module_info->unregister_module == NULL)
1643 module_info->unregister_module();
1644 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1646 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1647 "UnableToCloseModule",
"'%s': %s",module_info->tag,lt_dlerror());
1648 return(MagickFalse);
1654#if !defined(MAGICKCORE_BUILD_MODULES)
1656 analyzeImage(Image **,
const int,
const char **,ExceptionInfo *);
1659MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1660 ExceptionInfo *magick_unused(exception))
1662 magick_unreferenced(file);
1663 magick_unreferenced(exception);
1667MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
1668 Image **image,
const int argc,
const char **argv,ExceptionInfo *exception)
1673 assert(image != (Image **) NULL);
1674 assert((*image)->signature == MagickCoreSignature);
1675 if (IsEventLogging() != MagickFalse)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",(*image)->filename);
1677 rights=ReadPolicyRights;
1678 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
1681 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1682 "NotAuthorized",
"`%s'",tag);
1683 return(MagickFalse);
1685#if defined(MAGICKCORE_BUILD_MODULES)
1695 image_filter=(ImageFilterHandler *) NULL;
1696 if (LocaleCompare(
"analyze",tag) == 0)
1697 image_filter=(ImageFilterHandler *) analyzeImage;
1698 if (image_filter == (ImageFilterHandler *) NULL)
1699 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1700 "UnableToLoadModule",
"`%s'",tag);
1706 if ((*image)->debug != MagickFalse)
1707 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1708 "Invoking \"%s\" static image filter",tag);
1709 signature=image_filter(image,argc,argv,exception);
1710 if ((*image)->debug != MagickFalse)
1711 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1712 "\"%s\" completes",tag);
1713 if (signature != MagickImageFilterSignature)
1715 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1716 "ImageFilterSignatureMismatch",
"'%s': %8lx != %8lx",tag,
1717 (
unsigned long) signature,(
unsigned long)
1718 MagickImageFilterSignature);
1719 return(MagickFalse);