MagickCore  6.9.13-46
Convert, Edit, Or Compose Bitmap Images
module.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % M M OOO DDDD U U L EEEEE %
7 % MM MM O O D D U U L E %
8 % M M M O O D D U U L EEE %
9 % M M O O D D U U L E %
10 % M M OOO DDDD UUU LLLLL EEEEE %
11 % %
12 % %
13 % MagickCore Module Methods %
14 % %
15 % Software Design %
16 % Bob Friesenhahn %
17 % March 2000 %
18 % %
19 % %
20 % Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/license/ %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 
40 /*
41  Include declarations.
42 */
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)
69 #include "ltdl.h"
70 typedef lt_dlhandle ModuleHandle;
71 #else
72 typedef void *ModuleHandle;
73 #endif
74 
75 /*
76  Define declarations.
77 */
78 #if defined(MAGICKCORE_LTDL_DELEGATE)
79 # define ModuleGlobExpression "*.la"
80 #else
81 # if defined(_DEBUG)
82 # define ModuleGlobExpression "IM_MOD_DB_*.dll"
83 # else
84 # define ModuleGlobExpression "IM_MOD_RL_*.dll"
85 # endif
86 #endif
87 
88 /*
89  Global declarations.
90 */
91 static SemaphoreInfo
92  *module_semaphore = (SemaphoreInfo *) NULL;
93 
94 static SplayTreeInfo
95  *module_list = (SplayTreeInfo *) NULL;
96 
97 /*
98  Forward declarations.
99 */
100 static const ModuleInfo
101  *RegisterModule(const ModuleInfo *,ExceptionInfo *);
102 
103 static MagickBooleanType
104  GetMagickModulePath(const char *,MagickModuleType,char *,ExceptionInfo *),
105  IsModuleTreeInstantiated(ExceptionInfo *),
106  UnregisterModule(const ModuleInfo *,ExceptionInfo *);
107 
108 static void
109  TagToCoderModuleName(const char *,char *),
110  TagToFilterModuleName(const char *,char *),
111  TagToModuleName(const char *,const char *,char *);
112 
113 /*
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % %
116 % %
117 % %
118 % A c q u i r e M o d u l e I n f o %
119 % %
120 % %
121 % %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 %
124 % AcquireModuleInfo() allocates the ModuleInfo structure.
125 %
126 % The format of the AcquireModuleInfo method is:
127 %
128 % ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
129 %
130 % A description of each parameter follows:
131 %
132 % o path: the path associated with the tag.
133 %
134 % o tag: a character string that represents the image format we are
135 % looking for.
136 %
137 */
138 MagickExport ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
139 {
140  ModuleInfo
141  *module_info;
142 
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;
153  return(module_info);
154 }
155 
156 /*
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % %
159 % %
160 % %
161 % D e s t r o y M o d u l e L i s t %
162 % %
163 % %
164 % %
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %
167 % DestroyModuleList() unregisters any previously loaded modules and exits
168 % the module loaded environment.
169 %
170 % The format of the DestroyModuleList module is:
171 %
172 % void DestroyModuleList(void)
173 %
174 */
175 MagickExport void DestroyModuleList(void)
176 {
177  /*
178  Destroy magick modules.
179  */
180  LockSemaphoreInfo(module_semaphore);
181 #if defined(MAGICKCORE_MODULES_SUPPORT)
182  if (module_list != (SplayTreeInfo *) NULL)
183  module_list=DestroySplayTree(module_list);
184 #endif
185  UnlockSemaphoreInfo(module_semaphore);
186 }
187 
188 /*
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % %
191 % %
192 % %
193 % G e t M o d u l e I n f o %
194 % %
195 % %
196 % %
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 %
199 % GetModuleInfo() returns a pointer to a ModuleInfo structure that matches the
200 % specified tag. If tag is NULL, the head of the module list is returned. If
201 % no modules are loaded, or the requested module is not found, NULL is
202 % returned.
203 %
204 % The format of the GetModuleInfo module is:
205 %
206 % ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
207 %
208 % A description of each parameter follows:
209 %
210 % o tag: a character string that represents the image format we are
211 % looking for.
212 %
213 % o exception: return any errors or warnings in this structure.
214 %
215 */
216 MagickExport ModuleInfo *GetModuleInfo(const char *tag,ExceptionInfo *exception)
217 {
218  ModuleInfo
219  *module_info;
220 
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))
226  {
227 #if defined(MAGICKCORE_MODULES_SUPPORT)
228  if (LocaleCompare(tag,"*") == 0)
229  (void) OpenModules(exception);
230 #endif
231  module_info=(ModuleInfo *) GetNextValueInSplayTree(module_list);
232  UnlockSemaphoreInfo(module_semaphore);
233  return(module_info);
234  }
235  module_info=(ModuleInfo *) GetValueFromSplayTree(module_list,tag);
236  UnlockSemaphoreInfo(module_semaphore);
237  return(module_info);
238 }
239 
240 /*
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % %
243 % %
244 % %
245 % G e t M o d u l e I n f o L i s t %
246 % %
247 % %
248 % %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 %
251 % GetModuleInfoList() returns any modules that match the specified pattern.
252 %
253 % The format of the GetModuleInfoList function is:
254 %
255 % const ModuleInfo **GetModuleInfoList(const char *pattern,
256 % size_t *number_modules,ExceptionInfo *exception)
257 %
258 % A description of each parameter follows:
259 %
260 % o pattern: Specifies a pointer to a text string containing a pattern.
261 %
262 % o number_modules: This integer returns the number of modules in the list.
263 %
264 % o exception: return any errors or warnings in this structure.
265 %
266 */
267 
268 #if defined(__cplusplus) || defined(c_plusplus)
269 extern "C" {
270 #endif
271 
272 static int ModuleInfoCompare(const void *x,const void *y)
273 {
274  const ModuleInfo
275  **p,
276  **q;
277 
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));
283 }
284 
285 #if defined(__cplusplus) || defined(c_plusplus)
286 }
287 #endif
288 
289 MagickExport const ModuleInfo **GetModuleInfoList(const char *pattern,
290  size_t *number_modules,ExceptionInfo *exception)
291 {
292  const ModuleInfo
293  **modules;
294 
295  const ModuleInfo
296  *p;
297 
298  ssize_t
299  i;
300 
301  /*
302  Allocate module list.
303  */
304  assert(pattern != (char *) NULL);
305  assert(number_modules != (size_t *) NULL);
306  if (IsEventLogging() != MagickFalse)
307  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
308  *number_modules=0;
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);
316  /*
317  Generate module list.
318  */
319  LockSemaphoreInfo(module_semaphore);
320  ResetSplayTreeIterator(module_list);
321  p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
322  for (i=0; p != (const ModuleInfo *) NULL; )
323  {
324  if ((p->stealth == MagickFalse) &&
325  (GlobExpression(p->tag,pattern,MagickFalse) != MagickFalse))
326  modules[i++]=p;
327  p=(const ModuleInfo *) GetNextValueInSplayTree(module_list);
328  }
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;
333  return(modules);
334 }
335 
336 /*
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 % %
339 % %
340 % %
341 % G e t M o d u l e L i s t %
342 % %
343 % %
344 % %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 %
347 % GetModuleList() returns any image format modules that match the specified
348 % pattern.
349 %
350 % The format of the GetModuleList function is:
351 %
352 % char **GetModuleList(const char *pattern,const MagickModuleType type,
353 % size_t *number_modules,ExceptionInfo *exception)
354 %
355 % A description of each parameter follows:
356 %
357 % o pattern: Specifies a pointer to a text string containing a pattern.
358 %
359 % o type: choose from MagickImageCoderModule or MagickImageFilterModule.
360 %
361 % o number_modules: This integer returns the number of modules in the
362 % list.
363 %
364 % o exception: return any errors or warnings in this structure.
365 %
366 */
367 
368 #if defined(__cplusplus) || defined(c_plusplus)
369 extern "C" {
370 #endif
371 
372 static int ModuleCompare(const void *x,const void *y)
373 {
374  const char
375  **p,
376  **q;
377 
378  p=(const char **) x;
379  q=(const char **) y;
380  return(LocaleCompare(*p,*q));
381 }
382 
383 #if defined(__cplusplus) || defined(c_plusplus)
384 }
385 #endif
386 
387 MagickExport char **GetModuleList(const char *pattern,
388  const MagickModuleType type,size_t *number_modules,ExceptionInfo *exception)
389 {
390 #define MaxModules 511
391 
392  char
393  **modules,
394  filename[MaxTextExtent],
395  module_path[MaxTextExtent],
396  path[MaxTextExtent];
397 
398  DIR
399  *directory;
400 
401  MagickBooleanType
402  status;
403 
404  ssize_t
405  i;
406 
407  size_t
408  max_entries;
409 
410  struct dirent
411  *buffer,
412  *entry;
413 
414  /*
415  Locate all modules in the image coder or filter path.
416  */
417  switch (type)
418  {
419  case MagickImageCoderModule:
420  default:
421  {
422  TagToCoderModuleName("magick",filename);
423  status=GetMagickModulePath(filename,MagickImageCoderModule,module_path,
424  exception);
425  break;
426  }
427  case MagickImageFilterModule:
428  {
429  TagToFilterModuleName("analyze",filename);
430  status=GetMagickModulePath(filename,MagickImageFilterModule,module_path,
431  exception);
432  break;
433  }
434  }
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,
440  sizeof(*modules));
441  if (modules == (char **) NULL)
442  return((char **) NULL);
443  *modules=(char *) NULL;
444  directory=opendir(path);
445  if (directory == (DIR *) NULL)
446  {
447  modules=(char **) RelinquishMagickMemory(modules);
448  return((char **) NULL);
449  }
450  buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
451  if (buffer == (struct dirent *) NULL)
452  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
453  i=0;
454  while ((MagickReadDirectory(directory,buffer,&entry) == 0) &&
455  (entry != (struct dirent *) NULL))
456  {
457  status=GlobExpression(entry->d_name,ModuleGlobExpression,MagickFalse);
458  if (status == MagickFalse)
459  continue;
460  if (GlobExpression(entry->d_name,pattern,MagickFalse) == MagickFalse)
461  continue;
462  if (i >= (ssize_t) max_entries)
463  {
464  modules=(char **) NULL;
465  if (~max_entries > max_entries)
466  modules=(char **) ResizeQuantumMemory(modules,(size_t)
467  (max_entries << 1),sizeof(*modules));
468  max_entries<<=1;
469  if (modules == (char **) NULL)
470  break;
471  }
472  /*
473  Add new module name to list.
474  */
475  modules[i]=AcquireString((char *) NULL);
476  GetPathComponent(entry->d_name,BasePath,modules[i]);
477  if (LocaleNCompare("IM_MOD_",modules[i],7) == 0)
478  {
479  (void) CopyMagickString(modules[i],modules[i]+10,MaxTextExtent);
480  modules[i][strlen(modules[i])-1]='\0';
481  }
482  i++;
483  }
484  buffer=(struct dirent *) RelinquishMagickMemory(buffer);
485  (void) closedir(directory);
486  if (modules == (char **) NULL)
487  {
488  (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
489  "MemoryAllocationFailed","`%s'",pattern);
490  return((char **) NULL);
491  }
492  qsort((void *) modules,(size_t) i,sizeof(*modules),ModuleCompare);
493  modules[i]=(char *) NULL;
494  *number_modules=(size_t) i;
495  return(modules);
496 }
497 
498 /*
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % %
501 % %
502 % %
503 % G e t M a g i c k M o d u l e P a t h %
504 % %
505 % %
506 % %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 %
509 % GetMagickModulePath() finds a module with the specified module type and
510 % filename.
511 %
512 % The format of the GetMagickModulePath module is:
513 %
514 % MagickBooleanType GetMagickModulePath(const char *filename,
515 % MagickModuleType module_type,char *path,ExceptionInfo *exception)
516 %
517 % A description of each parameter follows:
518 %
519 % o filename: the module file name.
520 %
521 % o module_type: the module type: MagickImageCoderModule or
522 % MagickImageFilterModule.
523 %
524 % o path: the path associated with the filename.
525 %
526 % o exception: return any errors or warnings in this structure.
527 %
528 */
529 static MagickBooleanType GetMagickModulePath(const char *filename,
530  MagickModuleType module_type,char *path,ExceptionInfo *exception)
531 {
532  char
533  *module_path;
534 
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)
541  return(MagickFalse);
542  (void) CopyMagickString(path,filename,MaxTextExtent);
543  module_path=(char *) NULL;
544  switch (module_type)
545  {
546  case MagickImageCoderModule:
547  default:
548  {
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);
555 #endif
556  break;
557  }
558  case MagickImageFilterModule:
559  {
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);
566 #endif
567  break;
568  }
569  }
570  if (module_path != (char *) NULL)
571  {
572  char
573  *p,
574  *q;
575 
576  for (p=module_path-1; p != (char *) NULL; )
577  {
578  (void) CopyMagickString(path,p+1,MaxTextExtent);
579  q=strchr(path,DirectoryListSeparator);
580  if (q != (char *) NULL)
581  *q='\0';
582  q=path+strlen(path)-1;
583  if ((q >= path) && (*q != *DirectorySeparator))
584  (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
585  (void) ConcatenateMagickString(path,filename,MaxTextExtent);
586  {
587  char
588  *real_path = realpath_utf8(path);
589 
590  if (real_path != (char *) NULL)
591  {
592  (void) CopyMagickString(path,real_path,MagickPathExtent);
593  real_path=DestroyString(real_path);
594  }
595  }
596  if (IsPathAccessible(path) != MagickFalse)
597  {
598  module_path=DestroyString(module_path);
599  return(MagickTrue);
600  }
601  p=strchr(p+1,DirectoryListSeparator);
602  }
603  module_path=DestroyString(module_path);
604  }
605 #if defined(MAGICKCORE_INSTALLED_SUPPORT)
606  else
607 #if defined(MAGICKCORE_CODER_PATH)
608  {
609  const char
610  *directory;
611 
612  /*
613  Search hard coded paths.
614  */
615  switch (module_type)
616  {
617  case MagickImageCoderModule:
618  default:
619  {
620  directory=MAGICKCORE_CODER_PATH;
621  break;
622  }
623  case MagickImageFilterModule:
624  {
625  directory=MAGICKCORE_FILTER_PATH;
626  break;
627  }
628  }
629  (void) FormatLocaleString(path,MaxTextExtent,"%s%s",directory,filename);
630  if (IsPathAccessible(path) == MagickFalse)
631  {
632  ThrowFileException(exception,ConfigureWarning,
633  "UnableToOpenModuleFile",path);
634  return(MagickFalse);
635  }
636  return(MagickTrue);
637  }
638 #else
639 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
640  {
641  const char
642  *registry_key;
643 
644  unsigned char
645  *key_value;
646 
647  /*
648  Locate path via registry key.
649  */
650  switch (module_type)
651  {
652  case MagickImageCoderModule:
653  default:
654  {
655  registry_key="CoderModulesPath";
656  break;
657  }
658  case MagickImageFilterModule:
659  {
660  registry_key="FilterModulesPath";
661  break;
662  }
663  }
664  key_value=NTRegistryKeyLookup(registry_key);
665  if (key_value == (unsigned char *) NULL)
666  {
667  ThrowMagickException(exception,GetMagickModule(),ConfigureError,
668  "RegistryKeyLookupFailed","`%s'",registry_key);
669  return(MagickFalse);
670  }
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)
675  {
676  ThrowFileException(exception,ConfigureWarning,
677  "UnableToOpenModuleFile",path);
678  return(MagickFalse);
679  }
680  return(MagickTrue);
681  }
682 #endif
683 #endif
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
686 #endif
687 #else
688  {
689  char
690  *home;
691 
692  home=GetEnvironmentValue("MAGICK_HOME");
693  if (home != (char *) NULL)
694  {
695  /*
696  Search MAGICK_HOME.
697  */
698 #if !defined(MAGICKCORE_POSIX_SUPPORT)
699  (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",home,
700  DirectorySeparator,filename);
701 #else
702  const char
703  *directory;
704 
705  switch (module_type)
706  {
707  case MagickImageCoderModule:
708  default:
709  {
710  directory=MAGICKCORE_CODER_RELATIVE_PATH;
711  break;
712  }
713  case MagickImageFilterModule:
714  {
715  directory=MAGICKCORE_FILTER_RELATIVE_PATH;
716  break;
717  }
718  }
719  (void) FormatLocaleString(path,MaxTextExtent,"%s/lib/%s/%s",home,
720  directory,filename);
721 #endif
722  home=DestroyString(home);
723  if (IsPathAccessible(path) != MagickFalse)
724  return(MagickTrue);
725  }
726  }
727  if (*GetClientPath() != '\0')
728  {
729  /*
730  Search based on executable directory.
731  */
732 #if !defined(MAGICKCORE_POSIX_SUPPORT)
733  (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
734  DirectorySeparator,filename);
735 #else
736  char
737  prefix[MaxTextExtent];
738 
739  const char
740  *directory;
741 
742  switch (module_type)
743  {
744  case MagickImageCoderModule:
745  default:
746  {
747  directory="coders";
748  break;
749  }
750  case MagickImageFilterModule:
751  {
752  directory="filters";
753  break;
754  }
755  }
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);
760 #endif
761  if (IsPathAccessible(path) != MagickFalse)
762  return(MagickTrue);
763  }
764 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
765  {
766  /*
767  Search module path.
768  */
769  if ((NTGetModulePath("CORE_RL_magick_.dll",path) != MagickFalse) ||
770  (NTGetModulePath("CORE_DB_magick_.dll",path) != MagickFalse) ||
771  (NTGetModulePath("Magick.dll",path) != MagickFalse))
772  {
773  (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
774  (void) ConcatenateMagickString(path,filename,MaxTextExtent);
775  if (IsPathAccessible(path) != MagickFalse)
776  return(MagickTrue);
777  }
778  }
779 #endif
780  {
781  char
782  *home;
783 
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");
792 #endif
793  if (home != (char *) NULL)
794  {
795  /*
796  Search $XDG_CONFIG_HOME/ImageMagick.
797  */
798  (void) FormatLocaleString(path,MaxTextExtent,"%s%sImageMagick%s%s",
799  home,DirectorySeparator,DirectorySeparator,filename);
800  home=DestroyString(home);
801  if (IsPathAccessible(path) != MagickFalse)
802  return(MagickTrue);
803  }
804  home=GetEnvironmentValue("HOME");
805  if (home != (char *) NULL)
806  {
807  /*
808  Search $HOME/.config/ImageMagick.
809  */
810  (void) FormatLocaleString(path,MaxTextExtent,
811  "%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
812  DirectorySeparator,DirectorySeparator,filename);
813  if (IsPathAccessible(path) != MagickFalse)
814  {
815  home=DestroyString(home);
816  return(MagickTrue);
817  }
818  /*
819  Search $HOME/.magick.
820  */
821  (void) FormatLocaleString(path,MaxTextExtent,"%s%s.magick%s%s",home,
822  DirectorySeparator,DirectorySeparator,filename);
823  home=DestroyString(home);
824  if (IsPathAccessible(path) != MagickFalse)
825  return(MagickTrue);
826  }
827  }
828  /*
829  Search current directory.
830  */
831  if (IsPathAccessible(path) != MagickFalse)
832  return(MagickTrue);
833  if (exception->severity < ConfigureError)
834  ThrowFileException(exception,ConfigureWarning,"UnableToOpenModuleFile",
835  path);
836 #endif
837  return(MagickFalse);
838 }
839 
840 /*
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842 % %
843 % %
844 % %
845 % I s M o d u l e T r e e I n s t a n t i a t e d %
846 % %
847 % %
848 % %
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850 %
851 % IsModuleTreeInstantiated() determines if the module tree is instantiated.
852 % If not, it instantiates the tree and returns it.
853 %
854 % The format of the IsModuleTreeInstantiated() method is:
855 %
856 % MagickBooleanType IsModuleTreeInstantiated(Exceptioninfo *exception)
857 %
858 % A description of each parameter follows.
859 %
860 % o exception: return any errors or warnings in this structure.
861 %
862 */
863 
864 static void *DestroyModuleNode(void *module_info)
865 {
867  *exception;
868 
869  ModuleInfo
870  *p;
871 
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));
882 }
883 
884 static MagickBooleanType IsModuleTreeInstantiated(
885  ExceptionInfo *magick_unused(exception))
886 {
887  magick_unreferenced(exception);
888 
889  if (module_list == (SplayTreeInfo *) NULL)
890  {
891  if (module_semaphore == (SemaphoreInfo *) NULL)
892  ActivateSemaphoreInfo(&module_semaphore);
893  LockSemaphoreInfo(module_semaphore);
894  if (module_list == (SplayTreeInfo *) NULL)
895  {
896  MagickBooleanType
897  status;
898 
899  ModuleInfo
900  *module_info;
901 
903  *splay_tree;
904 
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");
920 #endif
921  module_list=splay_tree;
922  }
923  UnlockSemaphoreInfo(module_semaphore);
924  }
925  return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
926 }
927 
928 MagickExport MagickBooleanType InitializeModuleList(ExceptionInfo *exception)
929 {
930  return(IsModuleTreeInstantiated(exception));
931 }
932 
933 /*
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 % %
936 % %
937 % %
938 % I n v o k e D y n a m i c I m a g e F i l t e r %
939 % %
940 % %
941 % %
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 %
944 % InvokeDynamicImageFilter() invokes a dynamic image filter.
945 %
946 % The format of the InvokeDynamicImageFilter module is:
947 %
948 % MagickBooleanType InvokeDynamicImageFilter(const char *tag,Image **image,
949 % const int argc,const char **argv,ExceptionInfo *exception)
950 %
951 % A description of each parameter follows:
952 %
953 % o tag: a character string that represents the name of the particular
954 % module.
955 %
956 % o image: the image.
957 %
958 % o argc: a pointer to an integer describing the number of elements in the
959 % argument vector.
960 %
961 % o argv: a pointer to a text array containing the command line arguments.
962 %
963 % o exception: return any errors or warnings in this structure.
964 %
965 */
966 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
967  Image **images,const int argc,const char **argv,ExceptionInfo *exception)
968 {
969  char
970  name[MaxTextExtent],
971  path[MaxTextExtent];
972 
973  ImageFilterHandler
974  *image_filter;
975 
976  MagickBooleanType
977  status;
978 
979  ModuleHandle
980  handle;
981 
982  PolicyRights
983  rights;
984 
985  /*
986  Find the module.
987  */
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)
995  {
996  errno=EPERM;
997  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
998  "NotAuthorized","`%s'",tag);
999  return(MagickFalse);
1000  }
1001 #if !defined(MAGICKCORE_BUILD_MODULES)
1002  {
1003  MagickBooleanType
1004  status;
1005 
1006  status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
1007  if (status != MagickFalse)
1008  return(status);
1009  }
1010 #endif
1011  TagToFilterModuleName(tag,name);
1012  status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
1013  if (status == MagickFalse)
1014  {
1015  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1016  "UnableToLoadModule","`%s': %s",name,path);
1017  return(MagickFalse);
1018  }
1019  /*
1020  Open the module.
1021  */
1022  handle=(ModuleHandle) lt_dlopen(path);
1023  if (handle == (ModuleHandle) NULL)
1024  {
1025  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1026  "UnableToLoadModule","`%s': %s",name,lt_dlerror());
1027  return(MagickFalse);
1028  }
1029  /*
1030  Locate the module.
1031  */
1032 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1033  (void) FormatLocaleString(name,MaxTextExtent,"%sImage",tag);
1034 #else
1035  (void) FormatLocaleString(name,MaxTextExtent,"%s%sImage",
1036  MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1037 #endif
1038  /*
1039  Execute the module.
1040  */
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());
1046  else
1047  {
1048  size_t
1049  signature;
1050 
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",
1057  tag);
1058  if (signature != MagickImageFilterSignature)
1059  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1060  "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
1061  (unsigned long) signature,(unsigned long) MagickImageFilterSignature);
1062  }
1063  /*
1064  Close the module.
1065  */
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);
1070 }
1071 
1072 /*
1073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074 % %
1075 % %
1076 % %
1077 % L i s t M o d u l e I n f o %
1078 % %
1079 % %
1080 % %
1081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 %
1083 % ListModuleInfo() lists the module info to a file.
1084 %
1085 % The format of the ListModuleInfo module is:
1086 %
1087 % MagickBooleanType ListModuleInfo(FILE *file,ExceptionInfo *exception)
1088 %
1089 % A description of each parameter follows.
1090 %
1091 % o file: An pointer to a FILE.
1092 %
1093 % o exception: return any errors or warnings in this structure.
1094 %
1095 */
1096 MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1097  ExceptionInfo *exception)
1098 {
1099  char
1100  filename[MaxTextExtent],
1101  module_path[MaxTextExtent],
1102  **modules,
1103  path[MaxTextExtent];
1104 
1105  ssize_t
1106  i;
1107 
1108  size_t
1109  number_modules;
1110 
1111  if (file == (const FILE *) NULL)
1112  file=stdout;
1113  /*
1114  List image coders.
1115  */
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,
1121  exception);
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++)
1129  {
1130  (void) FormatLocaleFile(file,"%s",modules[i]);
1131  (void) FormatLocaleFile(file,"\n");
1132  }
1133  (void) fflush(file);
1134  /*
1135  Relinquish resources.
1136  */
1137  for (i=0; i < (ssize_t) number_modules; i++)
1138  modules[i]=DestroyString(modules[i]);
1139  modules=(char **) RelinquishMagickMemory(modules);
1140  /*
1141  List image filters.
1142  */
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,
1148  exception);
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++)
1156  {
1157  (void) FormatLocaleFile(file,"%s",modules[i]);
1158  (void) FormatLocaleFile(file,"\n");
1159  }
1160  (void) fflush(file);
1161  /*
1162  Relinquish resources.
1163  */
1164  for (i=0; i < (ssize_t) number_modules; i++)
1165  modules[i]=DestroyString(modules[i]);
1166  modules=(char **) RelinquishMagickMemory(modules);
1167  return(MagickTrue);
1168 }
1169 
1170 /*
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 % %
1173 % %
1174 % %
1175 + M o d u l e C o m p o n e n t G e n e s i s %
1176 % %
1177 % %
1178 % %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 %
1181 % ModuleComponentGenesis() instantiates the module component.
1182 %
1183 % The format of the ModuleComponentGenesis method is:
1184 %
1185 % MagickBooleanType ModuleComponentGenesis(void)
1186 %
1187 */
1188 MagickExport MagickBooleanType ModuleComponentGenesis(void)
1189 {
1191  *exception;
1192 
1193  MagickBooleanType
1194  status;
1195 
1196  if (module_semaphore == (SemaphoreInfo *) NULL)
1197  module_semaphore=AllocateSemaphoreInfo();
1198  exception=AcquireExceptionInfo();
1199  status=IsModuleTreeInstantiated(exception);
1200  exception=DestroyExceptionInfo(exception);
1201  return(status);
1202 }
1203 
1204 /*
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206 % %
1207 % %
1208 % %
1209 + M o d u l e C o m p o n e n t T e r m i n u s %
1210 % %
1211 % %
1212 % %
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214 %
1215 % ModuleComponentTerminus() destroys the module component.
1216 %
1217 % The format of the ModuleComponentTerminus method is:
1218 %
1219 % ModuleComponentTerminus(void)
1220 %
1221 */
1222 MagickExport void ModuleComponentTerminus(void)
1223 {
1224  if (module_semaphore == (SemaphoreInfo *) NULL)
1225  ActivateSemaphoreInfo(&module_semaphore);
1226  DestroyModuleList();
1227  DestroySemaphoreInfo(&module_semaphore);
1228 }
1229 
1230 /*
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % %
1233 % %
1234 % %
1235 % O p e n M o d u l e %
1236 % %
1237 % %
1238 % %
1239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1240 %
1241 % OpenModule() loads a module, and invokes its registration module. It
1242 % returns MagickTrue on success, and MagickFalse if there is an error.
1243 %
1244 % The format of the OpenModule module is:
1245 %
1246 % MagickBooleanType OpenModule(const char *module,ExceptionInfo *exception)
1247 %
1248 % A description of each parameter follows:
1249 %
1250 % o module: a character string that indicates the module to load.
1251 %
1252 % o exception: return any errors or warnings in this structure.
1253 %
1254 */
1255 MagickExport MagickBooleanType OpenModule(const char *module,
1256  ExceptionInfo *exception)
1257 {
1258  char
1259  module_name[MaxTextExtent],
1260  name[MaxTextExtent],
1261  path[MaxTextExtent];
1262 
1263  MagickBooleanType
1264  status;
1265 
1266  ModuleHandle
1267  handle;
1268 
1269  ModuleInfo
1270  *module_info;
1271 
1272  PolicyRights
1273  rights;
1274 
1275  const CoderInfo
1276  *p;
1277 
1278  size_t
1279  signature;
1280 
1281  /*
1282  Assign module name from alias.
1283  */
1284  assert(module != (const char *) NULL);
1285  module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1286  if (module_info != (ModuleInfo *) NULL)
1287  return(MagickTrue);
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)
1295  {
1296  errno=EPERM;
1297  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1298  "NotAuthorized","`%s'",module);
1299  return(MagickFalse);
1300  }
1301  if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
1302  return(MagickTrue); /* module already opened, return */
1303  /*
1304  Locate module.
1305  */
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);
1310  *path='\0';
1311  status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1312  if (status == MagickFalse)
1313  return(MagickFalse);
1314  /*
1315  Load module
1316  */
1317  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1318  "Opening module at path \"%s\"",path);
1319  handle=(ModuleHandle) lt_dlopen(path);
1320  if (handle == (ModuleHandle) NULL)
1321  {
1322  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1323  "UnableToLoadModule","`%s': %s",path,lt_dlerror());
1324  return(MagickFalse);
1325  }
1326  /*
1327  Register module.
1328  */
1329  module_info=AcquireModuleInfo(path,module_name);
1330  module_info->handle=handle;
1331  if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1332  return(MagickFalse);
1333  /*
1334  Define RegisterFORMATImage method.
1335  */
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)
1339  {
1340  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1341  "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1342  return(MagickFalse);
1343  }
1344  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1345  "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1346  (void *) module_info->register_module);
1347  /*
1348  Define UnregisterFORMATImage method.
1349  */
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)
1353  {
1354  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1355  "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1356  return(MagickFalse);
1357  }
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)
1363  {
1364  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1365  "ImageCoderSignatureMismatch","`%s': %8lx != %8lx",module_name,
1366  (unsigned long) signature,(unsigned long) MagickImageCoderSignature);
1367  return(MagickFalse);
1368  }
1369  return(MagickTrue);
1370 }
1371 
1372 /*
1373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % %
1375 % %
1376 % %
1377 % O p e n M o d u l e s %
1378 % %
1379 % %
1380 % %
1381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 %
1383 % OpenModules() loads all available modules.
1384 %
1385 % The format of the OpenModules module is:
1386 %
1387 % MagickBooleanType OpenModules(ExceptionInfo *exception)
1388 %
1389 % A description of each parameter follows:
1390 %
1391 % o exception: return any errors or warnings in this structure.
1392 %
1393 */
1394 MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
1395 {
1396  char
1397  **modules;
1398 
1399  ssize_t
1400  i;
1401 
1402  size_t
1403  number_modules;
1404 
1405  /*
1406  Load all modules.
1407  */
1408  (void) GetMagickInfo((char *) NULL,exception);
1409  number_modules=0;
1410  modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
1411  if ((modules == (char **) NULL) || (*modules == (char *) NULL))
1412  {
1413  if (modules != (char **) NULL)
1414  modules=(char **) RelinquishMagickMemory(modules);
1415  return(MagickFalse);
1416  }
1417  for (i=0; i < (ssize_t) number_modules; i++)
1418  (void) OpenModule(modules[i],exception);
1419  /*
1420  Relinquish resources.
1421  */
1422  for (i=0; i < (ssize_t) number_modules; i++)
1423  modules[i]=DestroyString(modules[i]);
1424  modules=(char **) RelinquishMagickMemory(modules);
1425  return(MagickTrue);
1426 }
1427 
1428 /*
1429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % %
1431 % %
1432 % %
1433 % R e g i s t e r M o d u l e %
1434 % %
1435 % %
1436 % %
1437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438 %
1439 % RegisterModule() adds an entry to the module list. It returns a pointer to
1440 % the registered entry on success.
1441 %
1442 % The format of the RegisterModule module is:
1443 %
1444 % ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1445 % ExceptionInfo *exception)
1446 %
1447 % A description of each parameter follows:
1448 %
1449 % o info: a pointer to the registered entry is returned.
1450 %
1451 % o module_info: a pointer to the ModuleInfo structure to register.
1452 %
1453 % o exception: return any errors or warnings in this structure.
1454 %
1455 */
1456 static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1457  ExceptionInfo *exception)
1458 {
1459  MagickBooleanType
1460  status;
1461 
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);
1473 }
1474 
1475 /*
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % %
1478 % %
1479 % %
1480 % T a g T o C o d e r M o d u l e N a m e %
1481 % %
1482 % %
1483 % %
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485 %
1486 % TagToCoderModuleName() munges a module tag and obtains the filename of the
1487 % corresponding module.
1488 %
1489 % The format of the TagToCoderModuleName module is:
1490 %
1491 % char *TagToCoderModuleName(const char *tag,char *name)
1492 %
1493 % A description of each parameter follows:
1494 %
1495 % o tag: a character string representing the module tag.
1496 %
1497 % o name: return the module name here.
1498 %
1499 */
1500 static void TagToCoderModuleName(const char *tag,char *name)
1501 {
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);
1509 #else
1510 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1511  if (LocaleNCompare("IM_MOD_",tag,7) == 0)
1512  (void) CopyMagickString(name,tag,MaxTextExtent);
1513  else
1514  {
1515 #if defined(_DEBUG)
1516  (void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_DB_%s_.dll",tag);
1517 #else
1518  (void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_RL_%s_.dll",tag);
1519 #endif
1520  }
1521 #endif
1522 #endif
1523 }
1524 
1525 /*
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527 % %
1528 % %
1529 % %
1530 % T a g T o F i l t e r M o d u l e N a m e %
1531 % %
1532 % %
1533 % %
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 %
1536 % TagToFilterModuleName() munges a module tag and returns the filename of the
1537 % corresponding filter module.
1538 %
1539 % The format of the TagToFilterModuleName module is:
1540 %
1541 % void TagToFilterModuleName(const char *tag,char name)
1542 %
1543 % A description of each parameter follows:
1544 %
1545 % o tag: a character string representing the module tag.
1546 %
1547 % o name: return the filter name here.
1548 %
1549 */
1550 static void TagToFilterModuleName(const char *tag,char *name)
1551 {
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);
1558 #else
1559  (void) FormatLocaleString(name,MaxTextExtent,"%s.la",tag);
1560 #endif
1561 }
1562 
1563 /*
1564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 % %
1566 % %
1567 % %
1568 % T a g T o M o d u l e N a m e %
1569 % %
1570 % %
1571 % %
1572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1573 %
1574 % TagToModuleName() munges the module tag name and returns an upper-case tag
1575 % name as the input string, and a user-provided format.
1576 %
1577 % The format of the TagToModuleName module is:
1578 %
1579 % TagToModuleName(const char *tag,const char *format,char *module)
1580 %
1581 % A description of each parameter follows:
1582 %
1583 % o tag: the module tag.
1584 %
1585 % o format: a sprintf-compatible format string containing %s where the
1586 % upper-case tag name is to be inserted.
1587 %
1588 % o module: pointer to a destination buffer for the formatted result.
1589 %
1590 */
1591 static void TagToModuleName(const char *tag,const char *format,char *module)
1592 {
1593  char
1594  name[MaxTextExtent];
1595 
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);
1602  LocaleUpper(name);
1603 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1604  (void) FormatLocaleString(module,MaxTextExtent,format,name);
1605 #else
1606  {
1607  char
1608  prefix_format[MaxTextExtent];
1609 
1610  (void) FormatLocaleString(prefix_format,MaxTextExtent,"%s%s",
1611  MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1612  (void) FormatLocaleString(module,MaxTextExtent,prefix_format,name);
1613  }
1614 #endif
1615 }
1616 
1617 /*
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 % %
1620 % %
1621 % %
1622 % U n r e g i s t e r M o d u l e %
1623 % %
1624 % %
1625 % %
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 %
1628 % UnregisterModule() unloads a module, and invokes its de-registration module.
1629 % Returns MagickTrue on success, and MagickFalse if there is an error.
1630 %
1631 % The format of the UnregisterModule module is:
1632 %
1633 % MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1634 % ExceptionInfo *exception)
1635 %
1636 % A description of each parameter follows:
1637 %
1638 % o module_info: the module info.
1639 %
1640 % o exception: return any errors or warnings in this structure.
1641 %
1642 */
1643 static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1644  ExceptionInfo *exception)
1645 {
1646  /*
1647  Locate and execute UnregisterFORMATImage module.
1648  */
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)
1654  return(MagickTrue);
1655  module_info->unregister_module();
1656  if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1657  {
1658  (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1659  "UnableToCloseModule","`%s': %s",module_info->tag,lt_dlerror());
1660  return(MagickFalse);
1661  }
1662  return(MagickTrue);
1663 }
1664 #else
1665 
1666 #if !defined(MAGICKCORE_BUILD_MODULES)
1667 extern size_t
1668  analyzeImage(Image **,const int,const char **,ExceptionInfo *);
1669 #endif
1670 
1671 MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1672  ExceptionInfo *magick_unused(exception))
1673 {
1674  magick_unreferenced(file);
1675  magick_unreferenced(exception);
1676  return(MagickTrue);
1677 }
1678 
1679 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
1680  Image **image,const int argc,const char **argv,ExceptionInfo *exception)
1681 {
1682  PolicyRights
1683  rights;
1684 
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)
1691  {
1692  errno=EPERM;
1693  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1694  "NotAuthorized","`%s'",tag);
1695  return(MagickFalse);
1696  }
1697 #if defined(MAGICKCORE_BUILD_MODULES)
1698  (void) tag;
1699  (void) argc;
1700  (void) argv;
1701  (void) exception;
1702 #else
1703  {
1704  ImageFilterHandler
1705  *image_filter;
1706 
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);
1713  else
1714  {
1715  size_t
1716  signature;
1717 
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)
1726  {
1727  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1728  "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
1729  (unsigned long) signature,(unsigned long)
1730  MagickImageFilterSignature);
1731  return(MagickFalse);
1732  }
1733  }
1734  }
1735 #endif
1736  return(MagickTrue);
1737 }
1738 #endif
Definition: mac.h:53
Definition: mac.h:41
Definition: image.h:133