This source file includes following definitions.
- AcquireModuleInfo
- DestroyModuleList
- GetModuleInfo
- ModuleInfoCompare
- GetModuleInfoList
- ModuleCompare
- MagickReadDirectory
- GetModuleList
- GetMagickModulePath
- DestroyModuleNode
- IsModuleTreeInstantiated
- InitializeModuleList
- InvokeDynamicImageFilter
- ListModuleInfo
- ModuleComponentGenesis
- ModuleComponentTerminus
- OpenModule
- OpenModules
- RegisterModule
- TagToCoderModuleName
- TagToFilterModuleName
- TagToModuleName
- UnregisterModule
- ListModuleInfo
- InvokeDynamicImageFilter
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/coder.h"
#include "magick/client.h"
#include "magick/configure.h"
#include "magick/deprecate.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/log.h"
#include "magick/hashmap.h"
#include "magick/magic.h"
#include "magick/magick.h"
#include "magick/memory_.h"
#include "magick/module.h"
#include "magick/nt-base-private.h"
#include "magick/policy.h"
#include "magick/semaphore.h"
#include "magick/splay-tree.h"
#include "magick/static.h"
#include "magick/string_.h"
#include "magick/token.h"
#include "magick/utility.h"
#if defined(MAGICKCORE_MODULES_SUPPORT)
#if defined(MAGICKCORE_LTDL_DELEGATE)
#include "ltdl.h"
typedef lt_dlhandle ModuleHandle;
#else
typedef void *ModuleHandle;
#endif
#if defined(MAGICKCORE_LTDL_DELEGATE)
# define ModuleGlobExpression "*.la"
#else
# if defined(_DEBUG)
# define ModuleGlobExpression "IM_MOD_DB_*.dll"
# else
# define ModuleGlobExpression "IM_MOD_RL_*.dll"
# endif
#endif
static SemaphoreInfo
*module_semaphore = (SemaphoreInfo *) NULL;
static SplayTreeInfo
*module_list = (SplayTreeInfo *) NULL;
static const ModuleInfo
*RegisterModule(const ModuleInfo *,ExceptionInfo *);
static MagickBooleanType
GetMagickModulePath(const char *,MagickModuleType,char *,ExceptionInfo *),
IsModuleTreeInstantiated(ExceptionInfo *),
UnregisterModule(const ModuleInfo *,ExceptionInfo *);
static void
TagToCoderModuleName(const char *,char *),
TagToFilterModuleName(const char *,char *),
TagToModuleName(const char *,const char *,char *);
MagickExport ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
{
ModuleInfo
*module_info;
module_info=(ModuleInfo *) AcquireMagickMemory(sizeof(*module_info));
if (module_info == (ModuleInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
(void) ResetMagickMemory(module_info,0,sizeof(*module_info));
if (path != (const char *) NULL)
module_info->path=ConstantString(path);
if (tag != (const char *) NULL)
module_info->tag=ConstantString(tag);
module_info->timestamp=time(0);
module_info->signature=MagickSignature;
return(module_info);
}
MagickExport void DestroyModuleList(void)
{
LockSemaphoreInfo(module_semaphore);
#if defined(MAGICKCORE_MODULES_SUPPORT)
if (module_list != (SplayTreeInfo *) NULL)
module_list=DestroySplayTree(module_list);
#endif
UnlockSemaphoreInfo(module_semaphore);
}
MagickExport ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
{
ModuleInfo
*module_info;
if (IsModuleTreeInstantiated(exception) == MagickFalse)
return((ModuleInfo *) NULL);
LockSemaphoreInfo(module_semaphore);
ResetSplayTreeIterator(module_list);
if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
{
#if defined(MAGICKCORE_MODULES_SUPPORT)
if (LocaleCompare(tag,"*") == 0)
(void) OpenModules(exception);
#endif
module_info=(ModuleInfo *) GetNextValueInSplayTree(module_list);
UnlockSemaphoreInfo(module_semaphore);
return(module_info);
}
module_info=(ModuleInfo *) GetValueFromSplayTree(module_list,tag);
UnlockSemaphoreInfo(module_semaphore);
return(module_info);
}
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
static int ModuleInfoCompare(const void *x,const void *y)
{
const ModuleInfo
**p,
**q;
p=(const ModuleInfo **) x,
q=(const ModuleInfo **) y;
if (LocaleCompare((*p)->path,(*q)->path) == 0)
return(LocaleCompare((*p)->tag,(*q)->tag));
return(LocaleCompare((*p)->path,(*q)->path));
}
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
MagickExport const ModuleInfo **GetModuleInfoList(const char *pattern,
size_t *number_modules,ExceptionInfo *exception)
{
const ModuleInfo
**modules;
register const ModuleInfo
*p;
register ssize_t
i;
assert(pattern != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
assert(number_modules != (size_t *) NULL);
*number_modules=0;
p=GetModuleInfo("*",exception);
if (p == (const ModuleInfo *) NULL)
return((const ModuleInfo **) NULL);
modules=(const ModuleInfo **) AcquireQuantumMemory((size_t)
GetNumberOfNodesInSplayTree(module_list)+1UL,sizeof(*modules));
if (modules == (const ModuleInfo **) NULL)
return((const ModuleInfo **) NULL);
LockSemaphoreInfo(module_semaphore);
ResetSplayTreeIterator(module_list);
p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
for (i=0; p != (const ModuleInfo *) NULL; )
{
if ((p->stealth == MagickFalse) &&
(GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
modules[i++]=p;
p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
}
UnlockSemaphoreInfo(module_semaphore);
qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleInfoCompare);
modules[i]=(ModuleInfo *) NULL;
*number_modules=(size_t) i;
return(modules);
}
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
static int ModuleCompare(const void *x,const void *y)
{
register const char
**p,
**q;
p=(const char **) x;
q=(const char **) y;
return(LocaleCompare(*p,*q));
}
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
struct dirent **result)
{
#if defined(MAGICKCORE_HAVE_READDIR_R)
return(readdir_r(directory,entry,result));
#else
(void) entry;
errno=0;
*result=readdir(directory);
return(errno);
#endif
}
MagickExport char **GetModuleList(const char *pattern,
const MagickModuleType type,size_t *number_modules,ExceptionInfo *exception)
{
#define MaxModules 511
char
**modules,
filename[MaxTextExtent],
module_path[MaxTextExtent],
path[MaxTextExtent];
DIR
*directory;
MagickBooleanType
status;
register ssize_t
i;
size_t
max_entries;
struct dirent
*buffer,
*entry;
switch (type)
{
case MagickImageCoderModule:
default:
{
TagToCoderModuleName("magick",filename);
status=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
exception);
break;
}
case MagickImageFilterModule:
{
TagToFilterModuleName("analyze",filename);
status=GetMagickModulePath(filename,MagickImageFilterModule,module_path,
exception);
break;
}
}
if (status == MagickFalse)
return((char **) NULL);
GetPathComponent(module_path,HeadPath,path);
max_entries=MaxModules;
modules=(char **) AcquireQuantumMemory((size_t) max_entries+1UL,
sizeof(*modules));
if (modules == (char **) NULL)
return((char **) NULL);
*modules=(char *) NULL;
directory=opendir(path);
if (directory == (DIR *) NULL)
{
modules=(char **) RelinquishMagickMemory(modules);
return((char **) NULL);
}
buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
if (buffer == (struct dirent *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
i=0;
while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
(entry != (struct dirent *) NULL))
{
status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
if (status == MagickFalse)
continue;
if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
continue;
if (i >= (ssize_t) max_entries)
{
modules=(char **) NULL;
if (~max_entries > max_entries)
modules=(char **) ResizeQuantumMemory(modules,(size_t)
(max_entries << 1),sizeof(*modules));
max_entries<<=1;
if (modules == (char **) NULL)
break;
}
modules[i]=AcquireString((char *) NULL);
GetPathComponent(entry->d_name,BasePath,modules[i]);
if (LocaleNCompare("IM_MOD_",modules[i],7) == 0)
{
(void) CopyMagickString(modules[i],modules[i]+10,MaxTextExtent);
modules[i][strlen(modules[i])-1]='\0';
}
i++;
}
buffer=(struct dirent *) RelinquishMagickMemory(buffer);
(void) closedir(directory);
if (modules == (char **) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
"MemoryAllocationFailed","`%s'",pattern);
return((char **) NULL);
}
qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleCompare);
modules[i]=(char *) NULL;
*number_modules=(size_t) i;
return(modules);
}
static MagickBooleanType GetMagickModulePath(const char *filename,
MagickModuleType module_type,char *path,ExceptionInfo *exception)
{
char
*module_path;
assert(filename != (const char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
assert(path != (char *) NULL);
assert(exception != (ExceptionInfo *) NULL);
(void) CopyMagickString(path,filename,MaxTextExtent);
module_path=(char *) NULL;
switch (module_type)
{
case MagickImageCoderModule:
default:
{
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Searching for coder module file \"%s\" ...",filename);
module_path=GetEnvironmentValue("MAGICK_CODER_MODULE_PATH");
#if defined(MAGICKCORE_CODER_PATH)
if (module_path == (char *) NULL)
module_path=AcquireString(MAGICKCORE_CODER_PATH);
#endif
break;
}
case MagickImageFilterModule:
{
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Searching for filter module file \"%s\" ...",filename);
module_path=GetEnvironmentValue("MAGICK_CODER_FILTER_PATH");
#if defined(MAGICKCORE_FILTER_PATH)
if (module_path == (char *) NULL)
module_path=AcquireString(MAGICKCORE_FILTER_PATH);
#endif
break;
}
}
if (module_path != (char *) NULL)
{
register char
*p,
*q;
for (p=module_path-1; p != (char *) NULL; )
{
(void) CopyMagickString(path,p+1,MaxTextExtent);
q=strchr(path,DirectoryListSeparator);
if (q != (char *) NULL)
*q='\0';
q=path+strlen(path)-1;
if ((q >= path) && (*q != *DirectorySeparator))
(void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
(void) ConcatenateMagickString(path,filename,MaxTextExtent);
if (IsPathAccessible(path) != MagickFalse)
{
module_path=DestroyString(module_path);
return(MagickTrue);
}
p=strchr(p+1,DirectoryListSeparator);
}
module_path=DestroyString(module_path);
}
#if defined(MAGICKCORE_INSTALLED_SUPPORT)
else
#if defined(MAGICKCORE_CODER_PATH)
{
const char
*directory;
switch (module_type)
{
case MagickImageCoderModule:
default:
{
directory=MAGICKCORE_CODER_PATH;
break;
}
case MagickImageFilterModule:
{
directory=MAGICKCORE_FILTER_PATH;
break;
}
}
(void) FormatLocaleString(path,MaxTextExtent,"%s%s",directory,filename);
if (IsPathAccessible(path) == MagickFalse)
{
ThrowFileException(exception,ConfigureWarning,
"UnableToOpenModuleFile",path);
return(MagickFalse);
}
return(MagickTrue);
}
#else
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
{
const char
*registery_key;
unsigned char
*key_value;
switch (module_type)
{
case MagickImageCoderModule:
default:
{
registery_key="CoderModulesPath";
break;
}
case MagickImageFilterModule:
{
registery_key="FilterModulesPath";
break;
}
}
key_value=NTRegistryKeyLookup(registery_key);
if (key_value == (unsigned char *) NULL)
{
ThrowMagickException(exception,GetMagickModule(),ConfigureError,
"RegistryKeyLookupFailed","`%s'",registery_key);
return(MagickFalse);
}
(void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",(char *) key_value,
DirectorySeparator,filename);
key_value=(unsigned char *) RelinquishMagickMemory(key_value);
if (IsPathAccessible(path) == MagickFalse)
{
ThrowFileException(exception,ConfigureWarning,
"UnableToOpenModuleFile",path);
return(MagickFalse);
}
return(MagickTrue);
}
#endif
#endif
#if !defined(MAGICKCORE_CODER_PATH) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
# error MAGICKCORE_CODER_PATH or MAGICKCORE_WINDOWS_SUPPORT must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
#endif
#else
{
char
*home;
home=GetEnvironmentValue("MAGICK_HOME");
if (home != (char *) NULL)
{
#if !defined(MAGICKCORE_POSIX_SUPPORT)
(void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",home,
DirectorySeparator,filename);
#else
const char
*directory;
switch (module_type)
{
case MagickImageCoderModule:
default:
{
directory=MAGICKCORE_CODER_RELATIVE_PATH;
break;
}
case MagickImageFilterModule:
{
directory=MAGICKCORE_FILTER_RELATIVE_PATH;
break;
}
}
(void) FormatLocaleString(path,MaxTextExtent,"%s/lib/%s/%s",home,
directory,filename);
#endif
home=DestroyString(home);
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
}
}
if (*GetClientPath() != '\0')
{
#if !defined(MAGICKCORE_POSIX_SUPPORT)
(void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
DirectorySeparator,filename);
#else
char
prefix[MaxTextExtent];
const char
*directory;
switch (module_type)
{
case MagickImageCoderModule:
default:
{
directory="coders";
break;
}
case MagickImageFilterModule:
{
directory="filters";
break;
}
}
(void) CopyMagickString(prefix,GetClientPath(),MaxTextExtent);
ChopPathComponents(prefix,1);
(void) FormatLocaleString(path,MaxTextExtent,"%s/lib/%s/%s/%s",prefix,
MAGICKCORE_MODULES_RELATIVE_PATH,directory,filename);
#endif
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
}
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
{
if ((NTGetModulePath("CORE_RL_magick_.dll",path) != MagickFalse) ||
(NTGetModulePath("CORE_DB_magick_.dll",path) != MagickFalse) ||
(NTGetModulePath("Magick.dll",path) != MagickFalse))
{
(void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
(void) ConcatenateMagickString(path,filename,MaxTextExtent);
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
}
}
#endif
{
char
*home;
home=GetEnvironmentValue("XDG_CONFIG_HOME");
if (home == (char *) NULL)
home=GetEnvironmentValue("LOCALAPPDATA");
if (home == (char *) NULL)
home=GetEnvironmentValue("APPDATA");
if (home == (char *) NULL)
home=GetEnvironmentValue("USERPROFILE");
if (home != (char *) NULL)
{
(void) FormatLocaleString(path,MaxTextExtent,"%s%sImageMagick%s%s",
home,DirectorySeparator,DirectorySeparator,filename);
home=DestroyString(home);
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
}
home=GetEnvironmentValue("HOME");
if (home != (char *) NULL)
{
(void) FormatLocaleString(path,MaxTextExtent,
"%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
DirectorySeparator,DirectorySeparator,filename);
if (IsPathAccessible(path) != MagickFalse)
{
home=DestroyString(home);
return(MagickTrue);
}
(void) FormatLocaleString(path,MaxTextExtent,"%s%s.magick%s%s",home,
DirectorySeparator,DirectorySeparator,filename);
home=DestroyString(home);
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
}
}
if (IsPathAccessible(path) != MagickFalse)
return(MagickTrue);
if (exception->severity < ConfigureError)
ThrowFileException(exception,ConfigureWarning,"UnableToOpenModuleFile",
path);
#endif
return(MagickFalse);
}
static void *DestroyModuleNode(void *module_info)
{
ExceptionInfo
*exception;
register ModuleInfo
*p;
exception=AcquireExceptionInfo();
p=(ModuleInfo *) module_info;
if (UnregisterModule(p,exception) == MagickFalse)
CatchException(exception);
if (p->tag != (char *) NULL)
p->tag=DestroyString(p->tag);
if (p->path != (char *) NULL)
p->path=DestroyString(p->path);
exception=DestroyExceptionInfo(exception);
return(RelinquishMagickMemory(p));
}
static MagickBooleanType IsModuleTreeInstantiated(
ExceptionInfo *magick_unused(exception))
{
magick_unreferenced(exception);
if (module_list == (SplayTreeInfo *) NULL)
{
if (module_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&module_semaphore);
LockSemaphoreInfo(module_semaphore);
if (module_list == (SplayTreeInfo *) NULL)
{
MagickBooleanType
status;
ModuleInfo
*module_info;
module_list=NewSplayTree(CompareSplayTreeString,
(void *(*)(void *)) NULL,DestroyModuleNode);
if (module_list == (SplayTreeInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
module_info=AcquireModuleInfo((const char *) NULL,"[boot-strap]");
module_info->stealth=MagickTrue;
status=AddValueToSplayTree(module_list,module_info->tag,module_info);
if (status == MagickFalse)
ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
if (lt_dlinit() != 0)
ThrowFatalException(ModuleFatalError,
"UnableToInitializeModuleLoader");
}
UnlockSemaphoreInfo(module_semaphore);
}
return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
}
MagickExport MagickBooleanType InitializeModuleList(ExceptionInfo *exception)
{
return(IsModuleTreeInstantiated(exception));
}
MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
Image **images,const int argc,const char **argv,ExceptionInfo *exception)
{
char
name[MaxTextExtent],
path[MaxTextExtent];
ImageFilterHandler
*image_filter;
MagickBooleanType
status;
ModuleHandle
handle;
PolicyRights
rights;
assert(images != (Image **) NULL);
assert((*images)->signature == MagickSignature);
if ((*images)->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
(*images)->filename);
#if !defined(MAGICKCORE_BUILD_MODULES)
{
MagickBooleanType
status;
status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
if (status != MagickFalse)
return(status);
}
#endif
rights=ReadPolicyRights;
if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
{
errno=EPERM;
(void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
"NotAuthorized","`%s'",tag);
return(MagickFalse);
}
TagToFilterModuleName(tag,name);
status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
if (status == MagickFalse)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToLoadModule","`%s': %s",name,path);
return(MagickFalse);
}
handle=(ModuleHandle) lt_dlopen(path);
if (handle == (ModuleHandle) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToLoadModule","`%s': %s",name,lt_dlerror());
return(MagickFalse);
}
#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
(void) FormatLocaleString(name,MaxTextExtent,"%sImage",tag);
#else
(void) FormatLocaleString(name,MaxTextExtent,"%s%sImage",
MAGICKCORE_NAMESPACE_PREFIX,tag);
#endif
ClearMagickException(exception);
image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
if (image_filter == (ImageFilterHandler *) NULL)
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToLoadModule","`%s': %s",name,lt_dlerror());
else
{
size_t
signature;
if ((*images)->debug != MagickFalse)
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Invoking \"%s\" dynamic image filter",tag);
signature=image_filter(images,argc,argv,exception);
if ((*images)->debug != MagickFalse)
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),"\"%s\" completes",
tag);
if (signature != MagickImageFilterSignature)
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
(unsigned long) signature,(unsigned long) MagickImageFilterSignature);
}
if (lt_dlclose(handle) != 0)
(void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
"UnableToCloseModule","`%s': %s",name,lt_dlerror());
return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
}
MagickExport MagickBooleanType ListModuleInfo(FILE *file,
ExceptionInfo *exception)
{
char
filename[MaxTextExtent],
module_path[MaxTextExtent],
**modules,
path[MaxTextExtent];
register ssize_t
i;
size_t
number_modules;
if (file == (const FILE *) NULL)
file=stdout;
modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
if (modules == (char **) NULL)
return(MagickFalse);
TagToCoderModuleName("magick",filename);
(void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
exception);
GetPathComponent(module_path,HeadPath,path);
(void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
(void) FormatLocaleFile(file,"Image Coder\n");
(void) FormatLocaleFile(file,
"-------------------------------------------------"
"------------------------------\n");
for (i=0; i < (ssize_t) number_modules; i++)
{
(void) FormatLocaleFile(file,"%s",modules[i]);
(void) FormatLocaleFile(file,"\n");
}
(void) fflush(file);
for (i=0; i < (ssize_t) number_modules; i++)
modules[i]=DestroyString(modules[i]);
modules=(char **) RelinquishMagickMemory(modules);
modules=GetModuleList("*",MagickImageFilterModule,&number_modules,exception);
if (modules == (char **) NULL)
return(MagickFalse);
TagToFilterModuleName("analyze",filename);
(void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
exception);
GetPathComponent(module_path,HeadPath,path);
(void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
(void) FormatLocaleFile(file,"Image Filter\n");
(void) FormatLocaleFile(file,
"-------------------------------------------------"
"------------------------------\n");
for (i=0; i < (ssize_t) number_modules; i++)
{
(void) FormatLocaleFile(file,"%s",modules[i]);
(void) FormatLocaleFile(file,"\n");
}
(void) fflush(file);
for (i=0; i < (ssize_t) number_modules; i++)
modules[i]=DestroyString(modules[i]);
modules=(char **) RelinquishMagickMemory(modules);
return(MagickTrue);
}
MagickExport MagickBooleanType ModuleComponentGenesis(void)
{
ExceptionInfo
*exception;
MagickBooleanType
status;
if (module_semaphore == (SemaphoreInfo *) NULL)
module_semaphore=AllocateSemaphoreInfo();
exception=AcquireExceptionInfo();
status=IsModuleTreeInstantiated(exception);
exception=DestroyExceptionInfo(exception);
return(status);
}
MagickExport void ModuleComponentTerminus(void)
{
if (module_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&module_semaphore);
DestroyModuleList();
DestroySemaphoreInfo(&module_semaphore);
}
MagickExport MagickBooleanType OpenModule(const char *module,
ExceptionInfo *exception)
{
char
filename[MaxTextExtent],
module_name[MaxTextExtent],
name[MaxTextExtent],
path[MaxTextExtent];
MagickBooleanType
status;
ModuleHandle
handle;
ModuleInfo
*module_info;
register const CoderInfo
*p;
size_t
signature;
assert(module != (const char *) NULL);
module_info=(ModuleInfo *) GetModuleInfo(module,exception);
if (module_info != (ModuleInfo *) NULL)
return(MagickTrue);
(void) CopyMagickString(module_name,module,MaxTextExtent);
p=GetCoderInfo(module,exception);
if (p != (CoderInfo *) NULL)
(void) CopyMagickString(module_name,p->name,MaxTextExtent);
if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
return(MagickTrue);
handle=(ModuleHandle) NULL;
TagToCoderModuleName(module_name,filename);
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Searching for module \"%s\" using filename \"%s\"",module_name,filename);
*path='\0';
status=GetMagickModulePath(filename,MagickImageCoderModule,path,exception);
if (status == MagickFalse)
return(MagickFalse);
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Opening module at path \"%s\"",path);
handle=(ModuleHandle) lt_dlopen(path);
if (handle == (ModuleHandle) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToLoadModule","`%s': %s",path,lt_dlerror());
return(MagickFalse);
}
module_info=AcquireModuleInfo(path,module_name);
module_info->handle=handle;
if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
return(MagickFalse);
TagToModuleName(module_name,"Register%sImage",name);
module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
if (module_info->register_module == (size_t (*)(void)) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
return(MagickFalse);
}
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Method \"%s\" in module \"%s\" at address %p",name,module_name,
(void *) module_info->register_module);
TagToModuleName(module_name,"Unregister%sImage",name);
module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
if (module_info->unregister_module == (void (*)(void)) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
return(MagickFalse);
}
(void) LogMagickEvent(ModuleEvent,GetMagickModule(),
"Method \"%s\" in module \"%s\" at address %p",name,module_name,
(void *) module_info->unregister_module);
signature=module_info->register_module();
if (signature != MagickImageCoderSignature)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"ImageCoderSignatureMismatch","`%s': %8lx != %8lx",module_name,
(unsigned long) signature,(unsigned long) MagickImageCoderSignature);
return(MagickFalse);
}
return(MagickTrue);
}
MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
{
char
**modules;
register ssize_t
i;
size_t
number_modules;
(void) GetMagickInfo((char *) NULL,exception);
number_modules=0;
modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
if ((modules == (char **) NULL) || (*modules == (char *) NULL))
{
if (modules != (char **) NULL)
modules=(char **) RelinquishMagickMemory(modules);
return(MagickFalse);
}
for (i=0; i < (ssize_t) number_modules; i++)
(void) OpenModule(modules[i],exception);
for (i=0; i < (ssize_t) number_modules; i++)
modules[i]=DestroyString(modules[i]);
modules=(char **) RelinquishMagickMemory(modules);
return(MagickTrue);
}
static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
ExceptionInfo *exception)
{
MagickBooleanType
status;
assert(module_info != (ModuleInfo *) NULL);
assert(module_info->signature == MagickSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
if (module_list == (SplayTreeInfo *) NULL)
return((const ModuleInfo *) NULL);
status=AddValueToSplayTree(module_list,module_info->tag,module_info);
if (status == MagickFalse)
(void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
"MemoryAllocationFailed","`%s'",module_info->tag);
return(module_info);
}
static void TagToCoderModuleName(const char *tag,char *name)
{
assert(tag != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
assert(name != (char *) NULL);
#if defined(MAGICKCORE_LTDL_DELEGATE)
(void) FormatLocaleString(name,MaxTextExtent,"%s.la",tag);
(void) LocaleLower(name);
#else
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
if (LocaleNCompare("IM_MOD_",tag,7) == 0)
(void) CopyMagickString(name,tag,MaxTextExtent);
else
{
#if defined(_DEBUG)
(void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_DB_%s_.dll",tag);
#else
(void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_RL_%s_.dll",tag);
#endif
}
#endif
#endif
}
static void TagToFilterModuleName(const char *tag,char *name)
{
assert(tag != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
assert(name != (char *) NULL);
#if !defined(MAGICKCORE_LTDL_DELEGATE)
(void) FormatLocaleString(name,MaxTextExtent,"%s.dll",tag);
#else
(void) FormatLocaleString(name,MaxTextExtent,"%s.la",tag);
#endif
}
static void TagToModuleName(const char *tag,const char *format,char *module)
{
char
name[MaxTextExtent];
assert(tag != (const char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
assert(format != (const char *) NULL);
assert(module != (char *) NULL);
(void) CopyMagickString(name,tag,MaxTextExtent);
LocaleUpper(name);
#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
(void) FormatLocaleString(module,MaxTextExtent,format,name);
#else
{
char
prefix_format[MaxTextExtent];
(void) FormatLocaleString(prefix_format,MaxTextExtent,"%s%s",
MAGICKCORE_NAMESPACE_PREFIX,format);
(void) FormatLocaleString(module,MaxTextExtent,prefix_format,name);
}
#endif
}
static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
ExceptionInfo *exception)
{
assert(module_info != (const ModuleInfo *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
assert(exception != (ExceptionInfo *) NULL);
if (module_info->unregister_module == NULL)
return(MagickTrue);
module_info->unregister_module();
if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
"UnableToCloseModule","`%s': %s",module_info->tag,lt_dlerror());
return(MagickFalse);
}
return(MagickTrue);
}
#else
#if !defined(MAGICKCORE_BUILD_MODULES)
extern size_t
analyzeImage(Image **,const int,const char **,ExceptionInfo *);
#endif
MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
ExceptionInfo *magick_unused(exception))
{
magick_unreferenced(file);
magick_unreferenced(exception);
return(MagickTrue);
}
MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
Image **image,const int argc,const char **argv,ExceptionInfo *exception)
{
PolicyRights
rights;
assert(image != (Image **) NULL);
assert((*image)->signature == MagickSignature);
if ((*image)->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
rights=ReadPolicyRights;
if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
{
errno=EPERM;
(void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
"NotAuthorized","`%s'",tag);
return(MagickFalse);
}
#if defined(MAGICKCORE_BUILD_MODULES)
(void) tag;
(void) argc;
(void) argv;
(void) exception;
#else
{
ImageFilterHandler
*image_filter;
image_filter=(ImageFilterHandler *) NULL;
if (LocaleCompare("analyze",tag) == 0)
image_filter=(ImageFilterHandler *) analyzeImage;
if (image_filter == (ImageFilterHandler *) NULL)
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"UnableToLoadModule","`%s'",tag);
else
{
size_t
signature;
if ((*image)->debug != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
"Invoking \"%s\" static image filter",tag);
signature=image_filter(image,argc,argv,exception);
if ((*image)->debug != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),"\"%s\" completes",
tag);
if (signature != MagickImageFilterSignature)
{
(void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
"ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
(unsigned long) signature,(unsigned long)
MagickImageFilterSignature);
return(MagickFalse);
}
}
}
#endif
return(MagickTrue);
}
#endif