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/deprecate.h"
49#include "magick/exception.h"
50#include "magick/exception-private.h"
51#include "magick/log.h"
52#include "magick/hashmap.h"
53#include "magick/magic.h"
54#include "magick/magick.h"
55#include "magick/memory_.h"
56#include "magick/module.h"
57#include "magick/nt-base-private.h"
58#include "magick/policy.h"
59#include "magick/semaphore.h"
60#include "magick/splay-tree.h"
61#include "magick/static.h"
62#include "magick/string_.h"
63#include "magick/timer-private.h"
64#include "magick/token.h"
65#include "magick/utility.h"
66#include "magick/utility-private.h"
67#if defined(MAGICKCORE_MODULES_SUPPORT)
68#if defined(MAGICKCORE_LTDL_DELEGATE)
70typedef lt_dlhandle ModuleHandle;
72typedef void *ModuleHandle;
78#if defined(MAGICKCORE_LTDL_DELEGATE)
79# define ModuleGlobExpression "*.la"
82# define ModuleGlobExpression "IM_MOD_DB_*.dll"
84# define ModuleGlobExpression "IM_MOD_RL_*.dll"
95 *module_list = (SplayTreeInfo *) NULL;
100static const ModuleInfo
101 *RegisterModule(
const ModuleInfo *,ExceptionInfo *);
103static MagickBooleanType
104 GetMagickModulePath(
const char *,MagickModuleType,
char *,ExceptionInfo *),
105 IsModuleTreeInstantiated(ExceptionInfo *),
106 UnregisterModule(
const ModuleInfo *,ExceptionInfo *);
109 TagToCoderModuleName(
const char *,
char *),
110 TagToFilterModuleName(
const char *,
char *),
111 TagToModuleName(
const char *,
const char *,
char *);
138MagickExport ModuleInfo *AcquireModuleInfo(
const char *path,
const char *tag)
143 module_info=(ModuleInfo *) AcquireMagickMemory(
sizeof(*module_info));
144 if (module_info == (ModuleInfo *) NULL)
145 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
146 (void) memset(module_info,0,
sizeof(*module_info));
147 if (path != (
const char *) NULL)
148 module_info->path=ConstantString(path);
149 if (tag != (
const char *) NULL)
150 module_info->tag=ConstantString(tag);
151 module_info->timestamp=GetMagickTime();
152 module_info->signature=MagickCoreSignature;
175MagickExport
void DestroyModuleList(
void)
180 LockSemaphoreInfo(module_semaphore);
181#if defined(MAGICKCORE_MODULES_SUPPORT)
182 if (module_list != (SplayTreeInfo *) NULL)
183 module_list=DestroySplayTree(module_list);
185 UnlockSemaphoreInfo(module_semaphore);
216MagickExport ModuleInfo *GetModuleInfo(
const char *tag,ExceptionInfo *exception)
221 if (IsModuleTreeInstantiated(exception) == MagickFalse)
222 return((ModuleInfo *) NULL);
223 LockSemaphoreInfo(module_semaphore);
224 ResetSplayTreeIterator(module_list);
225 if ((tag == (
const char *) NULL) || (LocaleCompare(tag,
"*") == 0))
227#if defined(MAGICKCORE_MODULES_SUPPORT)
228 if (LocaleCompare(tag,
"*") == 0)
229 (void) OpenModules(exception);
231 module_info=(ModuleInfo *) GetNextValueInSplayTree(module_list);
232 UnlockSemaphoreInfo(module_semaphore);
235 module_info=(ModuleInfo *) GetValueFromSplayTree(module_list,tag);
236 UnlockSemaphoreInfo(module_semaphore);
268#if defined(__cplusplus) || defined(c_plusplus)
272static int ModuleInfoCompare(
const void *x,
const void *y)
278 p=(
const ModuleInfo **) x,
279 q=(
const ModuleInfo **) y;
280 if (LocaleCompare((*p)->path,(*q)->path) == 0)
281 return(LocaleCompare((*p)->tag,(*q)->tag));
282 return(LocaleCompare((*p)->path,(*q)->path));
285#if defined(__cplusplus) || defined(c_plusplus)
289MagickExport
const ModuleInfo **GetModuleInfoList(
const char *pattern,
290 size_t *number_modules,ExceptionInfo *exception)
304 assert(pattern != (
char *) NULL);
305 assert(number_modules != (
size_t *) NULL);
306 if (IsEventLogging() != MagickFalse)
307 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",pattern);
309 p=GetModuleInfo(
"*",exception);
310 if (p == (
const ModuleInfo *) NULL)
311 return((
const ModuleInfo **) NULL);
312 modules=(
const ModuleInfo **) AcquireQuantumMemory((
size_t)
313 GetNumberOfNodesInSplayTree(module_list)+1UL,
sizeof(*modules));
314 if (modules == (
const ModuleInfo **) NULL)
315 return((
const ModuleInfo **) NULL);
319 LockSemaphoreInfo(module_semaphore);
320 ResetSplayTreeIterator(module_list);
321 p=(
const ModuleInfo *) GetNextValueInSplayTree(module_list);
322 for (i=0; p != (
const ModuleInfo *) NULL; )
324 if ((p->stealth == MagickFalse) &&
325 (GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
327 p=(
const ModuleInfo *) GetNextValueInSplayTree(module_list);
329 UnlockSemaphoreInfo(module_semaphore);
330 qsort((
void *) modules,(
size_t) i,
sizeof(*modules),ModuleInfoCompare);
331 modules[i]=(ModuleInfo *) NULL;
332 *number_modules=(size_t) i;
368#if defined(__cplusplus) || defined(c_plusplus)
372static int ModuleCompare(
const void *x,
const void *y)
380 return(LocaleCompare(*p,*q));
383#if defined(__cplusplus) || defined(c_plusplus)
387MagickExport
char **GetModuleList(
const char *pattern,
388 const MagickModuleType type,
size_t *number_modules,ExceptionInfo *exception)
390#define MaxModules 511
394 filename[MaxTextExtent],
395 module_path[MaxTextExtent],
419 case MagickImageCoderModule:
422 TagToCoderModuleName(
"magick",filename);
423 status=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
427 case MagickImageFilterModule:
429 TagToFilterModuleName(
"analyze",filename);
430 status=GetMagickModulePath(filename,MagickImageFilterModule,module_path,
435 if (status == MagickFalse)
436 return((
char **) NULL);
437 GetPathComponent(module_path,HeadPath,path);
438 max_entries=MaxModules;
439 modules=(
char **) AcquireQuantumMemory((
size_t) max_entries+1UL,
441 if (modules == (
char **) NULL)
442 return((
char **) NULL);
443 *modules=(
char *) NULL;
444 directory=opendir(path);
445 if (directory == (DIR *) NULL)
447 modules=(
char **) RelinquishMagickMemory(modules);
448 return((
char **) NULL);
450 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
451 if (buffer == (
struct dirent *) NULL)
452 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
454 while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
455 (entry != (
struct dirent *) NULL))
457 status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
458 if (status == MagickFalse)
460 if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
462 if (i >= (ssize_t) max_entries)
464 modules=(
char **) NULL;
465 if (~max_entries > max_entries)
466 modules=(
char **) ResizeQuantumMemory(modules,(
size_t)
467 (max_entries << 1),
sizeof(*modules));
469 if (modules == (
char **) NULL)
475 modules[i]=AcquireString((
char *) NULL);
476 GetPathComponent(entry->d_name,BasePath,modules[i]);
477 if (LocaleNCompare(
"IM_MOD_",modules[i],7) == 0)
479 (void) CopyMagickString(modules[i],modules[i]+10,MaxTextExtent);
480 modules[i][strlen(modules[i])-1]=
'\0';
484 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
485 (void) closedir(directory);
486 if (modules == (
char **) NULL)
488 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
489 "MemoryAllocationFailed",
"`%s'",pattern);
490 return((
char **) NULL);
492 qsort((
void *) modules,(
size_t) i,
sizeof(*modules),ModuleCompare);
493 modules[i]=(
char *) NULL;
494 *number_modules=(size_t) i;
529static MagickBooleanType GetMagickModulePath(
const char *filename,
530 MagickModuleType module_type,
char *path,ExceptionInfo *exception)
535 assert(filename != (
const char *) NULL);
536 assert(path != (
char *) NULL);
537 assert(exception != (ExceptionInfo *) NULL);
538 if (IsEventLogging() != MagickFalse)
539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
540 if (strchr(filename,
'/') != (
char *) 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_FILTER_MODULE_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);
588 *real_path = realpath_utf8(path);
590 if (real_path != (
char *) NULL)
592 (void) CopyMagickString(path,real_path,MagickPathExtent);
593 real_path=DestroyString(real_path);
596 if (IsPathAccessible(path) != MagickFalse)
598 module_path=DestroyString(module_path);
601 p=strchr(p+1,DirectoryListSeparator);
603 module_path=DestroyString(module_path);
605#if defined(MAGICKCORE_INSTALLED_SUPPORT)
607#if defined(MAGICKCORE_CODER_PATH)
617 case MagickImageCoderModule:
620 directory=MAGICKCORE_CODER_PATH;
623 case MagickImageFilterModule:
625 directory=MAGICKCORE_FILTER_PATH;
629 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s",directory,filename);
630 if (IsPathAccessible(path) == MagickFalse)
632 ThrowFileException(exception,ConfigureWarning,
633 "UnableToOpenModuleFile",path);
639#if defined(MAGICKCORE_WINDOWS_SUPPORT)
652 case MagickImageCoderModule:
655 registry_key=
"CoderModulesPath";
658 case MagickImageFilterModule:
660 registry_key=
"FilterModulesPath";
664 key_value=NTRegistryKeyLookup(registry_key);
665 if (key_value == (
unsigned char *) NULL)
667 ThrowMagickException(exception,GetMagickModule(),ConfigureError,
668 "RegistryKeyLookupFailed",
"`%s'",registry_key);
671 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s%s",(
char *) key_value,
672 DirectorySeparator,filename);
673 key_value=(
unsigned char *) RelinquishMagickMemory(key_value);
674 if (IsPathAccessible(path) == MagickFalse)
676 ThrowFileException(exception,ConfigureWarning,
677 "UnableToOpenModuleFile",path);
684#if !defined(MAGICKCORE_CODER_PATH) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
685# error MAGICKCORE_CODER_PATH or MAGICKCORE_WINDOWS_SUPPORT must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
692 home=GetEnvironmentValue(
"MAGICK_HOME");
693 if (home != (
char *) NULL)
698#if !defined(MAGICKCORE_POSIX_SUPPORT)
699 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s%s",home,
700 DirectorySeparator,filename);
707 case MagickImageCoderModule:
710 directory=MAGICKCORE_CODER_RELATIVE_PATH;
713 case MagickImageFilterModule:
715 directory=MAGICKCORE_FILTER_RELATIVE_PATH;
719 (void) FormatLocaleString(path,MaxTextExtent,
"%s/lib/%s/%s",home,
722 home=DestroyString(home);
723 if (IsPathAccessible(path) != MagickFalse)
727 if (*GetClientPath() !=
'\0')
732#if !defined(MAGICKCORE_POSIX_SUPPORT)
733 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s%s",GetClientPath(),
734 DirectorySeparator,filename);
737 prefix[MaxTextExtent];
744 case MagickImageCoderModule:
750 case MagickImageFilterModule:
756 (void) CopyMagickString(prefix,GetClientPath(),MaxTextExtent);
757 ChopPathComponents(prefix,1);
758 (void) FormatLocaleString(path,MaxTextExtent,
"%s/lib/%s/%s/%s",prefix,
759 MAGICKCORE_MODULES_RELATIVE_PATH,directory,filename);
761 if (IsPathAccessible(path) != MagickFalse)
764#if defined(MAGICKCORE_WINDOWS_SUPPORT)
769 if ((NTGetModulePath(
"CORE_RL_magick_.dll",path) != MagickFalse) ||
770 (NTGetModulePath(
"CORE_DB_magick_.dll",path) != MagickFalse) ||
771 (NTGetModulePath(
"Magick.dll",path) != MagickFalse))
773 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
774 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
775 if (IsPathAccessible(path) != MagickFalse)
784 home=GetEnvironmentValue(
"XDG_CONFIG_HOME");
785#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
786 if (home == (
char *) NULL)
787 home=GetEnvironmentValue(
"LOCALAPPDATA");
788 if (home == (
char *) NULL)
789 home=GetEnvironmentValue(
"APPDATA");
790 if (home == (
char *) NULL)
791 home=GetEnvironmentValue(
"USERPROFILE");
793 if (home != (
char *) NULL)
798 (void) FormatLocaleString(path,MaxTextExtent,
"%s%sImageMagick%s%s",
799 home,DirectorySeparator,DirectorySeparator,filename);
800 home=DestroyString(home);
801 if (IsPathAccessible(path) != MagickFalse)
804 home=GetEnvironmentValue(
"HOME");
805 if (home != (
char *) NULL)
810 (void) FormatLocaleString(path,MaxTextExtent,
811 "%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
812 DirectorySeparator,DirectorySeparator,filename);
813 if (IsPathAccessible(path) != MagickFalse)
815 home=DestroyString(home);
821 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s.magick%s%s",home,
822 DirectorySeparator,DirectorySeparator,filename);
823 home=DestroyString(home);
824 if (IsPathAccessible(path) != MagickFalse)
831 if (IsPathAccessible(path) != MagickFalse)
833 if (exception->severity < ConfigureError)
834 ThrowFileException(exception,ConfigureWarning,
"UnableToOpenModuleFile",
864static void *DestroyModuleNode(
void *module_info)
872 exception=AcquireExceptionInfo();
873 p=(ModuleInfo *) module_info;
874 if (UnregisterModule(p,exception) == MagickFalse)
875 CatchException(exception);
876 if (p->tag != (
char *) NULL)
877 p->tag=DestroyString(p->tag);
878 if (p->path != (
char *) NULL)
879 p->path=DestroyString(p->path);
880 exception=DestroyExceptionInfo(exception);
881 return(RelinquishMagickMemory(p));
884static MagickBooleanType IsModuleTreeInstantiated(
885 ExceptionInfo *magick_unused(exception))
887 magick_unreferenced(exception);
889 if (module_list == (SplayTreeInfo *) NULL)
892 ActivateSemaphoreInfo(&module_semaphore);
893 LockSemaphoreInfo(module_semaphore);
894 if (module_list == (SplayTreeInfo *) NULL)
905 splay_tree=NewSplayTree(CompareSplayTreeString,
906 (
void *(*)(
void *)) NULL,DestroyModuleNode);
907 if (splay_tree == (SplayTreeInfo *) NULL)
908 ThrowFatalException(ResourceLimitFatalError,
909 "MemoryAllocationFailed");
910 module_info=AcquireModuleInfo((
const char *) NULL,
"[boot-strap]");
911 module_info->stealth=MagickTrue;
912 status=AddValueToSplayTree(splay_tree,module_info->tag,module_info);
913 if (status == MagickFalse)
914 ThrowFatalException(ResourceLimitFatalError,
915 "MemoryAllocationFailed");
916#if defined(MAGICKCORE_LTDL_DELEGATE)
917 if (lt_dlinit() != 0)
918 ThrowFatalException(ModuleFatalError,
919 "UnableToInitializeModuleLoader");
921 module_list=splay_tree;
923 UnlockSemaphoreInfo(module_semaphore);
925 return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
928MagickExport MagickBooleanType InitializeModuleList(ExceptionInfo *exception)
930 return(IsModuleTreeInstantiated(exception));
966MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
967 Image **images,
const int argc,
const char **argv,ExceptionInfo *exception)
988 assert(images != (Image **) NULL);
989 assert((*images)->signature == MagickCoreSignature);
990 if (IsEventLogging() != MagickFalse)
991 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
992 (*images)->filename);
993 rights=ReadPolicyRights;
994 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
997 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
998 "NotAuthorized",
"`%s'",tag);
1001#if !defined(MAGICKCORE_BUILD_MODULES)
1006 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
1007 if (status != MagickFalse)
1011 TagToFilterModuleName(tag,name);
1012 status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
1013 if (status == MagickFalse)
1015 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1016 "UnableToLoadModule",
"`%s': %s",name,path);
1017 return(MagickFalse);
1022 handle=(ModuleHandle) lt_dlopen(path);
1023 if (handle == (ModuleHandle) NULL)
1025 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1026 "UnableToLoadModule",
"`%s': %s",name,lt_dlerror());
1027 return(MagickFalse);
1032#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1033 (void) FormatLocaleString(name,MaxTextExtent,
"%sImage",tag);
1035 (void) FormatLocaleString(name,MaxTextExtent,
"%s%sImage",
1036 MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1041 ClearMagickException(exception);
1042 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
1043 if (image_filter == (ImageFilterHandler *) NULL)
1044 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1045 "UnableToLoadModule",
"`%s': %s",name,lt_dlerror());
1051 if (IsEventLogging() != MagickFalse)
1052 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1053 "Invoking \"%s\" dynamic image filter",tag);
1054 signature=image_filter(images,argc,argv,exception);
1055 if (IsEventLogging() != MagickFalse)
1056 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"\"%s\" completes",
1058 if (signature != MagickImageFilterSignature)
1059 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1060 "ImageFilterSignatureMismatch",
"`%s': %8lx != %8lx",tag,
1061 (
unsigned long) signature,(
unsigned long) MagickImageFilterSignature);
1066 if (lt_dlclose(handle) != 0)
1067 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1068 "UnableToCloseModule",
"`%s': %s",name,lt_dlerror());
1069 return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
1096MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1097 ExceptionInfo *exception)
1100 filename[MaxTextExtent],
1101 module_path[MaxTextExtent],
1103 path[MaxTextExtent];
1111 if (file == (
const FILE *) NULL)
1116 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1117 if (modules == (
char **) NULL)
1118 return(MagickFalse);
1119 TagToCoderModuleName(
"magick",filename);
1120 (void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
1122 GetPathComponent(module_path,HeadPath,path);
1123 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1124 (void) FormatLocaleFile(file,
"Image Coder\n");
1125 (void) FormatLocaleFile(file,
1126 "-------------------------------------------------"
1127 "------------------------------\n");
1128 for (i=0; i < (ssize_t) number_modules; i++)
1130 (void) FormatLocaleFile(file,
"%s",modules[i]);
1131 (void) FormatLocaleFile(file,
"\n");
1133 (void) fflush(file);
1137 for (i=0; i < (ssize_t) number_modules; i++)
1138 modules[i]=DestroyString(modules[i]);
1139 modules=(
char **) RelinquishMagickMemory(modules);
1143 modules=GetModuleList(
"*",MagickImageFilterModule,&number_modules,exception);
1144 if (modules == (
char **) NULL)
1145 return(MagickFalse);
1146 TagToFilterModuleName(
"analyze",filename);
1147 (void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
1149 GetPathComponent(module_path,HeadPath,path);
1150 (void) FormatLocaleFile(file,
"\nPath: %s\n\n",path);
1151 (void) FormatLocaleFile(file,
"Image Filter\n");
1152 (void) FormatLocaleFile(file,
1153 "-------------------------------------------------"
1154 "------------------------------\n");
1155 for (i=0; i < (ssize_t) number_modules; i++)
1157 (void) FormatLocaleFile(file,
"%s",modules[i]);
1158 (void) FormatLocaleFile(file,
"\n");
1160 (void) fflush(file);
1164 for (i=0; i < (ssize_t) number_modules; i++)
1165 modules[i]=DestroyString(modules[i]);
1166 modules=(
char **) RelinquishMagickMemory(modules);
1188MagickExport MagickBooleanType ModuleComponentGenesis(
void)
1197 module_semaphore=AllocateSemaphoreInfo();
1198 exception=AcquireExceptionInfo();
1199 status=IsModuleTreeInstantiated(exception);
1200 exception=DestroyExceptionInfo(exception);
1222MagickExport
void ModuleComponentTerminus(
void)
1225 ActivateSemaphoreInfo(&module_semaphore);
1226 DestroyModuleList();
1227 DestroySemaphoreInfo(&module_semaphore);
1255MagickExport MagickBooleanType OpenModule(
const char *module,
1256 ExceptionInfo *exception)
1259 module_name[MaxTextExtent],
1260 name[MaxTextExtent],
1261 path[MaxTextExtent];
1284 assert(module != (
const char *) NULL);
1285 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1286 if (module_info != (ModuleInfo *) NULL)
1288 (void) CopyMagickString(module_name,module,MaxTextExtent);
1289 p=GetCoderInfo(module,exception);
1290 if (p != (CoderInfo *) NULL)
1291 (void) CopyMagickString(module_name,p->name,MaxTextExtent);
1292 LocaleUpper(module_name);
1293 rights=(PolicyRights) (ReadPolicyRights | WritePolicyRights);
1294 if (IsRightsAuthorized(ModulePolicyDomain,rights,module_name) == MagickFalse)
1297 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1298 "NotAuthorized",
"`%s'",module);
1299 return(MagickFalse);
1301 if (GetValueFromSplayTree(module_list,module_name) != (
void *) NULL)
1306 handle=(ModuleHandle) NULL;
1307 TagToCoderModuleName(module_name,name);
1308 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1309 "Searching for module \"%s\" using filename \"%s\"",module_name,name);
1311 status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1312 if (status == MagickFalse)
1313 return(MagickFalse);
1317 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1318 "Opening module at path \"%s\"",path);
1319 handle=(ModuleHandle) lt_dlopen(path);
1320 if (handle == (ModuleHandle) NULL)
1322 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1323 "UnableToLoadModule",
"`%s': %s",path,lt_dlerror());
1324 return(MagickFalse);
1329 module_info=AcquireModuleInfo(path,module_name);
1330 module_info->handle=handle;
1331 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1332 return(MagickFalse);
1336 TagToModuleName(module_name,
"Register%sImage",name);
1337 module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
1338 if (module_info->register_module == (size_t (*)(
void)) NULL)
1340 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1341 "UnableToRegisterImageFormat",
"`%s': %s",module_name,lt_dlerror());
1342 return(MagickFalse);
1344 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1345 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1346 (
void *) module_info->register_module);
1350 TagToModuleName(module_name,
"Unregister%sImage",name);
1351 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1352 if (module_info->unregister_module == (void (*)(
void)) NULL)
1354 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1355 "UnableToRegisterImageFormat",
"`%s': %s",module_name,lt_dlerror());
1356 return(MagickFalse);
1358 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1359 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1360 (
void *) module_info->unregister_module);
1361 signature=module_info->register_module();
1362 if (signature != MagickImageCoderSignature)
1364 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1365 "ImageCoderSignatureMismatch",
"`%s': %8lx != %8lx",module_name,
1366 (
unsigned long) signature,(
unsigned long) MagickImageCoderSignature);
1367 return(MagickFalse);
1394MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
1408 (void) GetMagickInfo((
char *) NULL,exception);
1410 modules=GetModuleList(
"*",MagickImageCoderModule,&number_modules,exception);
1411 if ((modules == (
char **) NULL) || (*modules == (
char *) NULL))
1413 if (modules != (
char **) NULL)
1414 modules=(
char **) RelinquishMagickMemory(modules);
1415 return(MagickFalse);
1417 for (i=0; i < (ssize_t) number_modules; i++)
1418 (
void) OpenModule(modules[i],exception);
1422 for (i=0; i < (ssize_t) number_modules; i++)
1423 modules[i]=DestroyString(modules[i]);
1424 modules=(
char **) RelinquishMagickMemory(modules);
1456static const ModuleInfo *RegisterModule(
const ModuleInfo *module_info,
1457 ExceptionInfo *exception)
1462 assert(module_info != (ModuleInfo *) NULL);
1463 assert(module_info->signature == MagickCoreSignature);
1464 if (IsEventLogging() != MagickFalse)
1465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1466 if (module_list == (SplayTreeInfo *) NULL)
1467 return((
const ModuleInfo *) NULL);
1468 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1469 if (status == MagickFalse)
1470 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1471 "MemoryAllocationFailed",
"`%s'",module_info->tag);
1472 return(module_info);
1500static void TagToCoderModuleName(
const char *tag,
char *name)
1502 assert(tag != (
char *) NULL);
1503 assert(name != (
char *) NULL);
1504 if (IsEventLogging() != MagickFalse)
1505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1506#if defined(MAGICKCORE_LTDL_DELEGATE)
1507 (void) FormatLocaleString(name,MaxTextExtent,
"%s.la",tag);
1508 (void) LocaleLower(name);
1510#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1511 if (LocaleNCompare(
"IM_MOD_",tag,7) == 0)
1512 (void) CopyMagickString(name,tag,MaxTextExtent);
1516 (void) FormatLocaleString(name,MaxTextExtent,
"IM_MOD_DB_%s_.dll",tag);
1518 (void) FormatLocaleString(name,MaxTextExtent,
"IM_MOD_RL_%s_.dll",tag);
1550static void TagToFilterModuleName(
const char *tag,
char *name)
1552 assert(tag != (
char *) NULL);
1553 assert(name != (
char *) NULL);
1554 if (IsEventLogging() != MagickFalse)
1555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1556#if !defined(MAGICKCORE_LTDL_DELEGATE)
1557 (void) FormatLocaleString(name,MaxTextExtent,
"%s.dll",tag);
1559 (void) FormatLocaleString(name,MaxTextExtent,
"%s.la",tag);
1591static void TagToModuleName(
const char *tag,
const char *format,
char *module)
1594 name[MaxTextExtent];
1596 assert(tag != (
const char *) NULL);
1597 assert(format != (
const char *) NULL);
1598 assert(module != (
char *) NULL);
1599 if (IsEventLogging() != MagickFalse)
1600 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",tag);
1601 (void) CopyMagickString(name,tag,MaxTextExtent);
1603#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1604 (void) FormatLocaleString(module,MaxTextExtent,format,name);
1608 prefix_format[MaxTextExtent];
1610 (void) FormatLocaleString(prefix_format,MaxTextExtent,
"%s%s",
1611 MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1612 (void) FormatLocaleString(module,MaxTextExtent,prefix_format,name);
1643static MagickBooleanType UnregisterModule(
const ModuleInfo *module_info,
1644 ExceptionInfo *exception)
1649 assert(module_info != (
const ModuleInfo *) NULL);
1650 assert(exception != (ExceptionInfo *) NULL);
1651 if (IsEventLogging() != MagickFalse)
1652 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",module_info->tag);
1653 if (module_info->unregister_module == NULL)
1655 module_info->unregister_module();
1656 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1658 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1659 "UnableToCloseModule",
"`%s': %s",module_info->tag,lt_dlerror());
1660 return(MagickFalse);
1666#if !defined(MAGICKCORE_BUILD_MODULES)
1668 analyzeImage(Image **,
const int,
const char **,ExceptionInfo *);
1671MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1672 ExceptionInfo *magick_unused(exception))
1674 magick_unreferenced(file);
1675 magick_unreferenced(exception);
1679MagickExport MagickBooleanType InvokeDynamicImageFilter(
const char *tag,
1680 Image **image,
const int argc,
const char **argv,ExceptionInfo *exception)
1685 assert(image != (Image **) NULL);
1686 assert((*image)->signature == MagickCoreSignature);
1687 if (IsEventLogging() != MagickFalse)
1688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",(*image)->filename);
1689 rights=ReadPolicyRights;
1690 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
1693 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1694 "NotAuthorized",
"`%s'",tag);
1695 return(MagickFalse);
1697#if defined(MAGICKCORE_BUILD_MODULES)
1707 image_filter=(ImageFilterHandler *) NULL;
1708 if (LocaleCompare(
"analyze",tag) == 0)
1709 image_filter=(ImageFilterHandler *) analyzeImage;
1710 if (image_filter == (ImageFilterHandler *) NULL)
1711 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1712 "UnableToLoadModule",
"`%s'",tag);
1718 if ((*image)->debug != MagickFalse)
1719 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1720 "Invoking \"%s\" static image filter",tag);
1721 signature=image_filter(image,argc,argv,exception);
1722 if ((*image)->debug != MagickFalse)
1723 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1724 "\"%s\" completes",tag);
1725 if (signature != MagickImageFilterSignature)
1727 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1728 "ImageFilterSignatureMismatch",
"`%s': %8lx != %8lx",tag,
1729 (
unsigned long) signature,(
unsigned long)
1730 MagickImageFilterSignature);
1731 return(MagickFalse);