MagickCore  7.1.1-43
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/script/license.php %
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 "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/client.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/linked-list.h"
52 #include "MagickCore/magic.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/memory-private.h"
56 #include "MagickCore/module.h"
57 #include "MagickCore/module-private.h"
58 #include "MagickCore/nt-base-private.h"
59 #include "MagickCore/policy.h"
60 #include "MagickCore/semaphore.h"
61 #include "MagickCore/splay-tree.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
65 #include "MagickCore/timer-private.h"
66 #include "MagickCore/token.h"
67 #include "MagickCore/utility.h"
68 #include "MagickCore/utility-private.h"
69 #if defined(MAGICKCORE_MODULES_SUPPORT)
70 #if defined(MAGICKCORE_LTDL_DELEGATE)
71 #include "ltdl.h"
72 typedef lt_dlhandle ModuleHandle;
73 #else
74 typedef void *ModuleHandle;
75 #endif
76 
77 /*
78  Define declarations.
79 */
80 #if defined(MAGICKCORE_LTDL_DELEGATE)
81 # define ModuleGlobExpression "*.la"
82 #else
83 # if defined(_DEBUG)
84 # define ModuleGlobExpression "IM_MOD_DB_*.dll"
85 # else
86 # define ModuleGlobExpression "IM_MOD_RL_*.dll"
87 # endif
88 #endif
89 
90 /*
91  Global declarations.
92 */
93 static SemaphoreInfo
94  *module_semaphore = (SemaphoreInfo *) NULL;
95 
96 static SplayTreeInfo
97  *module_list = (SplayTreeInfo *) NULL;
98 
99 /*
100  Forward declarations.
101 */
102 static const ModuleInfo
103  *RegisterModule(const ModuleInfo *,ExceptionInfo *);
104 
105 static MagickBooleanType
106  GetMagickModulePath(const char *,MagickModuleType,char *,ExceptionInfo *),
107  IsModuleTreeInstantiated(void),
108  UnregisterModule(const ModuleInfo *,ExceptionInfo *);
109 
110 static void
111  TagToCoderModuleName(const char *,char *),
112  TagToFilterModuleName(const char *,char *),
113  TagToModuleName(const char *,const char *,char *);
114 
115 /*
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % %
118 % %
119 % %
120 % A c q u i r e M o d u l e I n f o %
121 % %
122 % %
123 % %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 %
126 % AcquireModuleInfo() allocates the ModuleInfo structure.
127 %
128 % The format of the AcquireModuleInfo method is:
129 %
130 % ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
131 %
132 % A description of each parameter follows:
133 %
134 % o path: the path associated with the tag.
135 %
136 % o tag: a character string that represents the image format we are
137 % looking for.
138 %
139 */
140 MagickExport ModuleInfo *AcquireModuleInfo(const char *path,const char *tag)
141 {
142  ModuleInfo
143  *module_info;
144 
145  module_info=(ModuleInfo *) AcquireCriticalMemory(sizeof(*module_info));
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() == 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[MagickPathExtent],
395  module_path[MagickPathExtent],
396  path[MagickPathExtent];
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,MagickPathExtent);
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,MagickPathExtent);
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_CODER_FILTER_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,MagickPathExtent);
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,
585  MagickPathExtent);
586  (void) ConcatenateMagickString(path,filename,MagickPathExtent);
587 #if defined(MAGICKCORE_HAVE_REALPATH)
588  {
589  char
590  resolved_path[PATH_MAX+1];
591 
592  if (realpath(path,resolved_path) != (char *) NULL)
593  (void) CopyMagickString(path,resolved_path,MagickPathExtent);
594  }
595 #endif
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,MagickPathExtent,"%s%s",directory,
630  filename);
631  if (IsPathAccessible(path) == MagickFalse)
632  {
633  ThrowFileException(exception,ConfigureWarning,
634  "UnableToOpenModuleFile",path);
635  return(MagickFalse);
636  }
637  return(MagickTrue);
638  }
639 #else
640 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
641  {
642  const char
643  *registry_key;
644 
645  unsigned char
646  *key_value;
647 
648  /*
649  Locate path via registry key.
650  */
651  switch (module_type)
652  {
653  case MagickImageCoderModule:
654  default:
655  {
656  registry_key="CoderModulesPath";
657  break;
658  }
659  case MagickImageFilterModule:
660  {
661  registry_key="FilterModulesPath";
662  break;
663  }
664  }
665  key_value=NTRegistryKeyLookup(registry_key);
666  if (key_value == (unsigned char *) NULL)
667  {
668  ThrowMagickException(exception,GetMagickModule(),ConfigureError,
669  "RegistryKeyLookupFailed","`%s'",registry_key);
670  return(MagickFalse);
671  }
672  (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s",(char *)
673  key_value,DirectorySeparator,filename);
674  key_value=(unsigned char *) RelinquishMagickMemory(key_value);
675  if (IsPathAccessible(path) == MagickFalse)
676  {
677  ThrowFileException(exception,ConfigureWarning,
678  "UnableToOpenModuleFile",path);
679  return(MagickFalse);
680  }
681  return(MagickTrue);
682  }
683 #endif
684 #endif
685 #if !defined(MAGICKCORE_CODER_PATH) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
686 # error MAGICKCORE_CODER_PATH or MAGICKCORE_WINDOWS_SUPPORT must be defined when MAGICKCORE_INSTALLED_SUPPORT is defined
687 #endif
688 #else
689  {
690  char
691  *home;
692 
693  home=GetEnvironmentValue("MAGICK_HOME");
694  if (home != (char *) NULL)
695  {
696  /*
697  Search MAGICK_HOME.
698  */
699 #if !defined(MAGICKCORE_POSIX_SUPPORT)
700  (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s",home,
701  DirectorySeparator,filename);
702 #else
703  const char
704  *directory;
705 
706  switch (module_type)
707  {
708  case MagickImageCoderModule:
709  default:
710  {
711  directory=MAGICKCORE_CODER_RELATIVE_PATH;
712  break;
713  }
714  case MagickImageFilterModule:
715  {
716  directory=MAGICKCORE_FILTER_RELATIVE_PATH;
717  break;
718  }
719  }
720  (void) FormatLocaleString(path,MagickPathExtent,"%s/lib/%s/%s",home,
721  directory,filename);
722 #endif
723  home=DestroyString(home);
724  if (IsPathAccessible(path) != MagickFalse)
725  return(MagickTrue);
726  }
727  }
728  if (*GetClientPath() != '\0')
729  {
730  /*
731  Search based on executable directory.
732  */
733 #if !defined(MAGICKCORE_POSIX_SUPPORT)
734  (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s",GetClientPath(),
735  DirectorySeparator,filename);
736 #else
737  char
738  prefix[MagickPathExtent];
739 
740  const char
741  *directory;
742 
743  switch (module_type)
744  {
745  case MagickImageCoderModule:
746  default:
747  {
748  directory="coders";
749  break;
750  }
751  case MagickImageFilterModule:
752  {
753  directory="filters";
754  break;
755  }
756  }
757  (void) CopyMagickString(prefix,GetClientPath(),MagickPathExtent);
758  ChopPathComponents(prefix,1);
759  (void) FormatLocaleString(path,MagickPathExtent,"%s/lib/%s/%s/%s",prefix,
760  MAGICKCORE_MODULES_RELATIVE_PATH,directory,filename);
761 #endif
762  if (IsPathAccessible(path) != MagickFalse)
763  return(MagickTrue);
764  }
765 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
766  {
767  /*
768  Search module path.
769  */
770  if ((NTGetModulePath("CORE_RL_MagickCore_.dll",path) != MagickFalse) ||
771  (NTGetModulePath("CORE_DB_MagickCore_.dll",path) != MagickFalse))
772  {
773  (void) ConcatenateMagickString(path,DirectorySeparator,
774  MagickPathExtent);
775  (void) ConcatenateMagickString(path,filename,MagickPathExtent);
776  if (IsPathAccessible(path) != MagickFalse)
777  return(MagickTrue);
778  }
779  }
780 #endif
781  {
782  char
783  *home;
784 
785  home=GetEnvironmentValue("XDG_CONFIG_HOME");
786  if (home == (char *) NULL)
787 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
788  home=GetEnvironmentValue("LOCALAPPDATA");
789  if (home == (char *) NULL)
790  home=GetEnvironmentValue("APPDATA");
791  if (home == (char *) NULL)
792  home=GetEnvironmentValue("USERPROFILE");
793 #endif
794  if (home != (char *) NULL)
795  {
796  /*
797  Search $XDG_CONFIG_HOME/ImageMagick.
798  */
799  (void) FormatLocaleString(path,MagickPathExtent,"%s%sImageMagick%s%s",
800  home,DirectorySeparator,DirectorySeparator,filename);
801  home=DestroyString(home);
802  if (IsPathAccessible(path) != MagickFalse)
803  return(MagickTrue);
804  }
805  home=GetEnvironmentValue("HOME");
806  if (home != (char *) NULL)
807  {
808  /*
809  Search $HOME/.config/ImageMagick.
810  */
811  (void) FormatLocaleString(path,MagickPathExtent,
812  "%s%s.config%sImageMagick%s%s",home,DirectorySeparator,
813  DirectorySeparator,DirectorySeparator,filename);
814  home=DestroyString(home);
815  if (IsPathAccessible(path) != MagickFalse)
816  return(MagickTrue);
817  }
818  }
819  /*
820  Search current directory.
821  */
822  if (IsPathAccessible(path) != MagickFalse)
823  return(MagickTrue);
824  if (exception->severity < ConfigureError)
825  ThrowFileException(exception,ConfigureWarning,"UnableToOpenModuleFile",
826  path);
827 #endif
828  return(MagickFalse);
829 }
830 
831 /*
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % %
834 % %
835 % %
836 % I s M o d u l e T r e e I n s t a n t i a t e d %
837 % %
838 % %
839 % %
840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 %
842 % IsModuleTreeInstantiated() determines if the module tree is instantiated.
843 % If not, it instantiates the tree and returns it.
844 %
845 % The format of the IsModuleTreeInstantiated() method is:
846 %
847 % IsModuleTreeInstantiated()
848 %
849 */
850 
851 static void *DestroyModuleNode(void *module_info)
852 {
854  *exception;
855 
856  ModuleInfo
857  *p;
858 
859  exception=AcquireExceptionInfo();
860  p=(ModuleInfo *) module_info;
861  if (UnregisterModule(p,exception) == MagickFalse)
862  CatchException(exception);
863  if (p->tag != (char *) NULL)
864  p->tag=DestroyString(p->tag);
865  if (p->path != (char *) NULL)
866  p->path=DestroyString(p->path);
867  exception=DestroyExceptionInfo(exception);
868  return(RelinquishMagickMemory(p));
869 }
870 
871 static MagickBooleanType IsModuleTreeInstantiated(void)
872 {
873  if (module_list == (SplayTreeInfo *) NULL)
874  {
875  if (module_semaphore == (SemaphoreInfo *) NULL)
876  ActivateSemaphoreInfo(&module_semaphore);
877  LockSemaphoreInfo(module_semaphore);
878  if (module_list == (SplayTreeInfo *) NULL)
879  {
880  MagickBooleanType
881  status;
882 
883  ModuleInfo
884  *module_info;
885 
887  *splay_tree;
888 
889  splay_tree=NewSplayTree(CompareSplayTreeString,
890  (void *(*)(void *)) NULL,DestroyModuleNode);
891  module_info=AcquireModuleInfo((const char *) NULL,"[boot-strap]");
892  module_info->stealth=MagickTrue;
893  status=AddValueToSplayTree(splay_tree,module_info->tag,module_info);
894  if (status == MagickFalse)
895  ThrowFatalException(ResourceLimitFatalError,
896  "MemoryAllocationFailed");
897 #if defined(MAGICKCORE_LTDL_DELEGATE)
898  if (lt_dlinit() != 0)
899  ThrowFatalException(ModuleFatalError,
900  "UnableToInitializeModuleLoader");
901 #endif
902  module_list=splay_tree;
903  }
904  UnlockSemaphoreInfo(module_semaphore);
905  }
906  return(module_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
907 }
908 
909 /*
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % %
912 % %
913 % %
914 % I n v o k e D y n a m i c I m a g e F i l t e r %
915 % %
916 % %
917 % %
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 %
920 % InvokeDynamicImageFilter() invokes a dynamic image filter.
921 %
922 % The format of the InvokeDynamicImageFilter module is:
923 %
924 % MagickBooleanType InvokeDynamicImageFilter(const char *tag,Image **image,
925 % const int argc,const char **argv,ExceptionInfo *exception)
926 %
927 % A description of each parameter follows:
928 %
929 % o tag: a character string that represents the name of the particular
930 % module.
931 %
932 % o image: the image.
933 %
934 % o argc: a pointer to an integer describing the number of elements in the
935 % argument vector.
936 %
937 % o argv: a pointer to a text array containing the command line arguments.
938 %
939 % o exception: return any errors or warnings in this structure.
940 %
941 */
942 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
943  Image **images,const int argc,const char **argv,ExceptionInfo *exception)
944 {
945  char
946  name[MagickPathExtent],
947  path[MagickPathExtent];
948 
949  ImageFilterHandler
950  *image_filter;
951 
952  MagickBooleanType
953  status;
954 
955  ModuleHandle
956  handle;
957 
958  PolicyRights
959  rights;
960 
961  /*
962  Find the module.
963  */
964  assert(images != (Image **) NULL);
965  assert((*images)->signature == MagickCoreSignature);
966  if (IsEventLogging() != MagickFalse)
967  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
968  (*images)->filename);
969  rights=ReadPolicyRights;
970  if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
971  {
972  errno=EPERM;
973  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
974  "NotAuthorized","`%s'",tag);
975  return(MagickFalse);
976  }
977 #if !defined(MAGICKCORE_BUILD_MODULES)
978  {
979  MagickBooleanType
980  status;
981 
982  status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
983  if (status != MagickFalse)
984  return(status);
985  }
986 #endif
987  TagToFilterModuleName(tag,name);
988  status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
989  if (status == MagickFalse)
990  {
991  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
992  "UnableToLoadModule","'%s': %s",name,path);
993  return(MagickFalse);
994  }
995  /*
996  Open the module.
997  */
998  handle=(ModuleHandle) lt_dlopen(path);
999  if (handle == (ModuleHandle) NULL)
1000  {
1001  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1002  "UnableToLoadModule","'%s': %s",name,lt_dlerror());
1003  return(MagickFalse);
1004  }
1005  /*
1006  Locate the module.
1007  */
1008 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1009  (void) FormatLocaleString(name,MagickPathExtent,"%sImage",tag);
1010 #else
1011  (void) FormatLocaleString(name,MagickPathExtent,"%s%sImage",
1012  MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1013 #endif
1014  /*
1015  Execute the module.
1016  */
1017  ClearMagickException(exception);
1018  image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
1019  if (image_filter == (ImageFilterHandler *) NULL)
1020  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1021  "UnableToLoadModule","'%s': %s",name,lt_dlerror());
1022  else
1023  {
1024  size_t
1025  signature;
1026 
1027  if (IsEventLogging() != MagickFalse)
1028  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1029  "Invoking \"%s\" dynamic image filter",tag);
1030  signature=image_filter(images,argc,argv,exception);
1031  if (IsEventLogging() != MagickFalse)
1032  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),"\"%s\" completes",
1033  tag);
1034  if (signature != MagickImageFilterSignature)
1035  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1036  "ImageFilterSignatureMismatch","'%s': %8lx != %8lx",tag,
1037  (unsigned long) signature,(unsigned long) MagickImageFilterSignature);
1038  }
1039  /*
1040  Close the module.
1041  */
1042  if (lt_dlclose(handle) != 0)
1043  (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1044  "UnableToCloseModule","'%s': %s",name,lt_dlerror());
1045  return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
1046 }
1047 
1048 /*
1049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050 % %
1051 % %
1052 % %
1053 % L i s t M o d u l e I n f o %
1054 % %
1055 % %
1056 % %
1057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058 %
1059 % ListModuleInfo() lists the module info to a file.
1060 %
1061 % The format of the ListModuleInfo module is:
1062 %
1063 % MagickBooleanType ListModuleInfo(FILE *file,ExceptionInfo *exception)
1064 %
1065 % A description of each parameter follows.
1066 %
1067 % o file: An pointer to a FILE.
1068 %
1069 % o exception: return any errors or warnings in this structure.
1070 %
1071 */
1072 MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1073  ExceptionInfo *exception)
1074 {
1075  char
1076  filename[MagickPathExtent],
1077  module_path[MagickPathExtent],
1078  **modules,
1079  path[MagickPathExtent];
1080 
1081  ssize_t
1082  i;
1083 
1084  size_t
1085  number_modules;
1086 
1087  if (file == (const FILE *) NULL)
1088  file=stdout;
1089  /*
1090  List image coders.
1091  */
1092  modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
1093  if (modules == (char **) NULL)
1094  return(MagickFalse);
1095  TagToCoderModuleName("magick",filename);
1096  (void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
1097  exception);
1098  GetPathComponent(module_path,HeadPath,path);
1099  (void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
1100  (void) FormatLocaleFile(file,"Image Coder\n");
1101  (void) FormatLocaleFile(file,
1102  "-------------------------------------------------"
1103  "------------------------------\n");
1104  for (i=0; i < (ssize_t) number_modules; i++)
1105  {
1106  (void) FormatLocaleFile(file,"%s",modules[i]);
1107  (void) FormatLocaleFile(file,"\n");
1108  }
1109  (void) fflush(file);
1110  /*
1111  Relinquish resources.
1112  */
1113  for (i=0; i < (ssize_t) number_modules; i++)
1114  modules[i]=DestroyString(modules[i]);
1115  modules=(char **) RelinquishMagickMemory(modules);
1116  /*
1117  List image filters.
1118  */
1119  modules=GetModuleList("*",MagickImageFilterModule,&number_modules,exception);
1120  if (modules == (char **) NULL)
1121  return(MagickFalse);
1122  TagToFilterModuleName("analyze",filename);
1123  (void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
1124  exception);
1125  GetPathComponent(module_path,HeadPath,path);
1126  (void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
1127  (void) FormatLocaleFile(file,"Image Filter\n");
1128  (void) FormatLocaleFile(file,
1129  "-------------------------------------------------"
1130  "------------------------------\n");
1131  for (i=0; i < (ssize_t) number_modules; i++)
1132  {
1133  (void) FormatLocaleFile(file,"%s",modules[i]);
1134  (void) FormatLocaleFile(file,"\n");
1135  }
1136  (void) fflush(file);
1137  /*
1138  Relinquish resources.
1139  */
1140  for (i=0; i < (ssize_t) number_modules; i++)
1141  modules[i]=DestroyString(modules[i]);
1142  modules=(char **) RelinquishMagickMemory(modules);
1143  return(MagickTrue);
1144 }
1145 
1146 /*
1147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148 % %
1149 % %
1150 % %
1151 + M o d u l e C o m p o n e n t G e n e s i s %
1152 % %
1153 % %
1154 % %
1155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156 %
1157 % ModuleComponentGenesis() instantiates the module component.
1158 %
1159 % The format of the ModuleComponentGenesis method is:
1160 %
1161 % MagickBooleanType ModuleComponentGenesis(void)
1162 %
1163 */
1164 MagickPrivate MagickBooleanType ModuleComponentGenesis(void)
1165 {
1166  MagickBooleanType
1167  status;
1168 
1169  if (module_semaphore == (SemaphoreInfo *) NULL)
1170  module_semaphore=AcquireSemaphoreInfo();
1171  status=IsModuleTreeInstantiated();
1172  return(status);
1173 }
1174 
1175 /*
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177 % %
1178 % %
1179 % %
1180 + M o d u l e C o m p o n e n t T e r m i n u s %
1181 % %
1182 % %
1183 % %
1184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185 %
1186 % ModuleComponentTerminus() destroys the module component.
1187 %
1188 % The format of the ModuleComponentTerminus method is:
1189 %
1190 % ModuleComponentTerminus(void)
1191 %
1192 */
1193 MagickPrivate void ModuleComponentTerminus(void)
1194 {
1195  if (module_semaphore == (SemaphoreInfo *) NULL)
1196  ActivateSemaphoreInfo(&module_semaphore);
1197  DestroyModuleList();
1198  RelinquishSemaphoreInfo(&module_semaphore);
1199 }
1200 
1201 /*
1202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203 % %
1204 % %
1205 % %
1206 % O p e n M o d u l e %
1207 % %
1208 % %
1209 % %
1210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1211 %
1212 % OpenModule() loads a module, and invokes its registration module. It
1213 % returns MagickTrue on success, and MagickFalse if there is an error.
1214 %
1215 % The format of the OpenModule module is:
1216 %
1217 % MagickBooleanType OpenModule(const char *module,ExceptionInfo *exception)
1218 %
1219 % A description of each parameter follows:
1220 %
1221 % o module: a character string that indicates the module to load.
1222 %
1223 % o exception: return any errors or warnings in this structure.
1224 %
1225 */
1226 MagickPrivate MagickBooleanType OpenModule(const char *module,
1227  ExceptionInfo *exception)
1228 {
1229  char
1230  module_name[MagickPathExtent],
1231  name[MagickPathExtent],
1232  path[MagickPathExtent];
1233 
1234  MagickBooleanType
1235  status;
1236 
1237  ModuleHandle
1238  handle;
1239 
1240  ModuleInfo
1241  *module_info;
1242 
1243  PolicyRights
1244  rights;
1245 
1246  const CoderInfo
1247  *p;
1248 
1249  size_t
1250  signature;
1251 
1252  /*
1253  Assign module name from alias.
1254  */
1255  assert(module != (const char *) NULL);
1256  module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1257  if (module_info != (ModuleInfo *) NULL)
1258  return(MagickTrue);
1259  (void) CopyMagickString(module_name,module,MagickPathExtent);
1260  p=GetCoderInfo(module,exception);
1261  if (p != (CoderInfo *) NULL)
1262  (void) CopyMagickString(module_name,p->name,MagickPathExtent);
1263  LocaleUpper(module_name);
1264  rights=(PolicyRights) (ReadPolicyRights | WritePolicyRights);
1265  if (IsRightsAuthorized(ModulePolicyDomain,rights,module_name) == MagickFalse)
1266  {
1267  errno=EPERM;
1268  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1269  "NotAuthorized","`%s'",module);
1270  return(MagickFalse);
1271  }
1272  if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
1273  return(MagickTrue); /* module already opened, return */
1274  /*
1275  Locate module.
1276  */
1277  handle=(ModuleHandle) NULL;
1278  TagToCoderModuleName(module_name,name);
1279  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1280  "Searching for module \"%s\" using filename \"%s\"",module_name,name);
1281  *path='\0';
1282  status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1283  if (status == MagickFalse)
1284  return(MagickFalse);
1285  /*
1286  Load module
1287  */
1288  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1289  "Opening module at path \"%s\"",path);
1290  handle=(ModuleHandle) lt_dlopen(path);
1291  if (handle == (ModuleHandle) NULL)
1292  {
1293  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1294  "UnableToLoadModule","'%s': %s",path,lt_dlerror());
1295  return(MagickFalse);
1296  }
1297  /*
1298  Register module.
1299  */
1300  module_info=AcquireModuleInfo(path,module_name);
1301  module_info->handle=handle;
1302  if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1303  return(MagickFalse);
1304  /*
1305  Define RegisterFORMATImage method.
1306  */
1307  TagToModuleName(module_name,"Register%sImage",name);
1308  module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
1309  if (module_info->register_module == (size_t (*)(void)) NULL)
1310  {
1311  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1312  "UnableToRegisterImageFormat","'%s': %s",module_name,lt_dlerror());
1313  return(MagickFalse);
1314  }
1315  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1316  "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1317  (void *) module_info->register_module);
1318  /*
1319  Define UnregisterFORMATImage method.
1320  */
1321  TagToModuleName(module_name,"Unregister%sImage",name);
1322  module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1323  if (module_info->unregister_module == (void (*)(void)) NULL)
1324  {
1325  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1326  "UnableToRegisterImageFormat","'%s': %s",module_name,lt_dlerror());
1327  return(MagickFalse);
1328  }
1329  (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1330  "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1331  (void *) module_info->unregister_module);
1332  signature=module_info->register_module();
1333  if (signature != MagickImageCoderSignature)
1334  {
1335  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1336  "ImageCoderSignatureMismatch","'%s': %8lx != %8lx",module_name,
1337  (unsigned long) signature,(unsigned long) MagickImageCoderSignature);
1338  return(MagickFalse);
1339  }
1340  return(MagickTrue);
1341 }
1342 
1343 /*
1344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % %
1346 % %
1347 % %
1348 % O p e n M o d u l e s %
1349 % %
1350 % %
1351 % %
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353 %
1354 % OpenModules() loads all available modules.
1355 %
1356 % The format of the OpenModules module is:
1357 %
1358 % MagickBooleanType OpenModules(ExceptionInfo *exception)
1359 %
1360 % A description of each parameter follows:
1361 %
1362 % o exception: return any errors or warnings in this structure.
1363 %
1364 */
1365 MagickPrivate MagickBooleanType OpenModules(ExceptionInfo *exception)
1366 {
1367  char
1368  **modules;
1369 
1370  ssize_t
1371  i;
1372 
1373  size_t
1374  number_modules;
1375 
1376  /*
1377  Load all modules.
1378  */
1379  (void) GetMagickInfo((char *) NULL,exception);
1380  number_modules=0;
1381  modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
1382  if ((modules == (char **) NULL) || (*modules == (char *) NULL))
1383  {
1384  if (modules != (char **) NULL)
1385  modules=(char **) RelinquishMagickMemory(modules);
1386  return(MagickFalse);
1387  }
1388  for (i=0; i < (ssize_t) number_modules; i++)
1389  (void) OpenModule(modules[i],exception);
1390  /*
1391  Relinquish resources.
1392  */
1393  for (i=0; i < (ssize_t) number_modules; i++)
1394  modules[i]=DestroyString(modules[i]);
1395  modules=(char **) RelinquishMagickMemory(modules);
1396  return(MagickTrue);
1397 }
1398 
1399 /*
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 % %
1402 % %
1403 % %
1404 % R e g i s t e r M o d u l e %
1405 % %
1406 % %
1407 % %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 %
1410 % RegisterModule() adds an entry to the module list. It returns a pointer to
1411 % the registered entry on success.
1412 %
1413 % The format of the RegisterModule module is:
1414 %
1415 % ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1416 % ExceptionInfo *exception)
1417 %
1418 % A description of each parameter follows:
1419 %
1420 % o info: a pointer to the registered entry is returned.
1421 %
1422 % o module_info: a pointer to the ModuleInfo structure to register.
1423 %
1424 % o exception: return any errors or warnings in this structure.
1425 %
1426 */
1427 static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1428  ExceptionInfo *exception)
1429 {
1430  MagickBooleanType
1431  status;
1432 
1433  assert(module_info != (ModuleInfo *) NULL);
1434  assert(module_info->signature == MagickCoreSignature);
1435  if (IsEventLogging() != MagickFalse)
1436  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1437  if (module_list == (SplayTreeInfo *) NULL)
1438  return((const ModuleInfo *) NULL);
1439  status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1440  if (status == MagickFalse)
1441  (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1442  "MemoryAllocationFailed","`%s'",module_info->tag);
1443  return(module_info);
1444 }
1445 
1446 /*
1447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 % %
1449 % %
1450 % %
1451 % T a g T o C o d e r M o d u l e N a m e %
1452 % %
1453 % %
1454 % %
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 %
1457 % TagToCoderModuleName() munges a module tag and obtains the filename of the
1458 % corresponding module.
1459 %
1460 % The format of the TagToCoderModuleName module is:
1461 %
1462 % char *TagToCoderModuleName(const char *tag,char *name)
1463 %
1464 % A description of each parameter follows:
1465 %
1466 % o tag: a character string representing the module tag.
1467 %
1468 % o name: return the module name here.
1469 %
1470 */
1471 static void TagToCoderModuleName(const char *tag,char *name)
1472 {
1473  assert(tag != (char *) NULL);
1474  assert(name != (char *) NULL);
1475  if (IsEventLogging() != MagickFalse)
1476  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1477 #if defined(MAGICKCORE_LTDL_DELEGATE)
1478  (void) FormatLocaleString(name,MagickPathExtent,"%s.la",tag);
1479  (void) LocaleLower(name);
1480 #else
1481 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1482  if (LocaleNCompare("IM_MOD_",tag,7) == 0)
1483  (void) CopyMagickString(name,tag,MagickPathExtent);
1484  else
1485  {
1486 #if defined(_DEBUG)
1487  (void) FormatLocaleString(name,MagickPathExtent,"IM_MOD_DB_%s_.dll",tag);
1488 #else
1489  (void) FormatLocaleString(name,MagickPathExtent,"IM_MOD_RL_%s_.dll",tag);
1490 #endif
1491  }
1492 #endif
1493 #endif
1494 }
1495 
1496 /*
1497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % %
1499 % %
1500 % %
1501 % T a g T o F i l t e r M o d u l e N a m e %
1502 % %
1503 % %
1504 % %
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506 %
1507 % TagToFilterModuleName() munges a module tag and returns the filename of the
1508 % corresponding filter module.
1509 %
1510 % The format of the TagToFilterModuleName module is:
1511 %
1512 % void TagToFilterModuleName(const char *tag,char name)
1513 %
1514 % A description of each parameter follows:
1515 %
1516 % o tag: a character string representing the module tag.
1517 %
1518 % o name: return the filter name here.
1519 %
1520 */
1521 static void TagToFilterModuleName(const char *tag,char *name)
1522 {
1523  assert(tag != (char *) NULL);
1524  assert(name != (char *) NULL);
1525  if (IsEventLogging() != MagickFalse)
1526  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1527 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1528  (void) FormatLocaleString(name,MagickPathExtent,"FILTER_%s_.dll",tag);
1529 #elif !defined(MAGICKCORE_LTDL_DELEGATE)
1530  (void) FormatLocaleString(name,MagickPathExtent,"%s.dll",tag);
1531 #else
1532  (void) FormatLocaleString(name,MagickPathExtent,"%s.la",tag);
1533 #endif
1534 }
1535 
1536 /*
1537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 % %
1539 % %
1540 % %
1541 % T a g T o M o d u l e N a m e %
1542 % %
1543 % %
1544 % %
1545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 %
1547 % TagToModuleName() munges the module tag name and returns an upper-case tag
1548 % name as the input string, and a user-provided format.
1549 %
1550 % The format of the TagToModuleName module is:
1551 %
1552 % TagToModuleName(const char *tag,const char *format,char *module)
1553 %
1554 % A description of each parameter follows:
1555 %
1556 % o tag: the module tag.
1557 %
1558 % o format: a sprintf-compatible format string containing %s where the
1559 % upper-case tag name is to be inserted.
1560 %
1561 % o module: pointer to a destination buffer for the formatted result.
1562 %
1563 */
1564 static void TagToModuleName(const char *tag,const char *format,char *module)
1565 {
1566  char
1567  name[MagickPathExtent];
1568 
1569  assert(tag != (const char *) NULL);
1570  assert(format != (const char *) NULL);
1571  assert(module != (char *) NULL);
1572  if (IsEventLogging() != MagickFalse)
1573  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1574  (void) CopyMagickString(name,tag,MagickPathExtent);
1575  LocaleUpper(name);
1576 #if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1577  (void) FormatLocaleString(module,MagickPathExtent,format,name);
1578 #else
1579  {
1580  char
1581  prefix_format[MagickPathExtent];
1582 
1583  (void) FormatLocaleString(prefix_format,MagickPathExtent,"%s%s",
1584  MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1585  (void) FormatLocaleString(module,MagickPathExtent,prefix_format,name);
1586  }
1587 #endif
1588 }
1589 
1590 /*
1591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1592 % %
1593 % %
1594 % %
1595 % U n r e g i s t e r M o d u l e %
1596 % %
1597 % %
1598 % %
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 %
1601 % UnregisterModule() unloads a module, and invokes its de-registration module.
1602 % Returns MagickTrue on success, and MagickFalse if there is an error.
1603 %
1604 % The format of the UnregisterModule module is:
1605 %
1606 % MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1607 % ExceptionInfo *exception)
1608 %
1609 % A description of each parameter follows:
1610 %
1611 % o module_info: the module info.
1612 %
1613 % o exception: return any errors or warnings in this structure.
1614 %
1615 */
1616 static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1617  ExceptionInfo *exception)
1618 {
1619  /*
1620  Locate and execute UnregisterFORMATImage module.
1621  */
1622  assert(module_info != (const ModuleInfo *) NULL);
1623  assert(exception != (ExceptionInfo *) NULL);
1624  if (IsEventLogging() != MagickFalse)
1625  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1626  if (module_info->unregister_module == NULL)
1627  return(MagickTrue);
1628  module_info->unregister_module();
1629  if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1630  {
1631  (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1632  "UnableToCloseModule","'%s': %s",module_info->tag,lt_dlerror());
1633  return(MagickFalse);
1634  }
1635  return(MagickTrue);
1636 }
1637 #else
1638 
1639 #if !defined(MAGICKCORE_BUILD_MODULES)
1640 extern size_t
1641  analyzeImage(Image **,const int,const char **,ExceptionInfo *);
1642 #endif
1643 
1644 MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1645  ExceptionInfo *magick_unused(exception))
1646 {
1647  magick_unreferenced(file);
1648  magick_unreferenced(exception);
1649  return(MagickTrue);
1650 }
1651 
1652 MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
1653  Image **image,const int argc,const char **argv,ExceptionInfo *exception)
1654 {
1655  PolicyRights
1656  rights;
1657 
1658  assert(image != (Image **) NULL);
1659  assert((*image)->signature == MagickCoreSignature);
1660  if (IsEventLogging() != MagickFalse)
1661  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
1662  rights=ReadPolicyRights;
1663  if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
1664  {
1665  errno=EPERM;
1666  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1667  "NotAuthorized","`%s'",tag);
1668  return(MagickFalse);
1669  }
1670 #if defined(MAGICKCORE_BUILD_MODULES)
1671  (void) tag;
1672  (void) argc;
1673  (void) argv;
1674  (void) exception;
1675 #else
1676  {
1677  ImageFilterHandler
1678  *image_filter;
1679 
1680  image_filter=(ImageFilterHandler *) NULL;
1681  if (LocaleCompare("analyze",tag) == 0)
1682  image_filter=(ImageFilterHandler *) analyzeImage;
1683  if (image_filter == (ImageFilterHandler *) NULL)
1684  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1685  "UnableToLoadModule","`%s'",tag);
1686  else
1687  {
1688  size_t
1689  signature;
1690 
1691  if ((*image)->debug != MagickFalse)
1692  (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1693  "Invoking \"%s\" static image filter",tag);
1694  signature=image_filter(image,argc,argv,exception);
1695  if ((*image)->debug != MagickFalse)
1696  (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1697  "\"%s\" completes",tag);
1698  if (signature != MagickImageFilterSignature)
1699  {
1700  (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1701  "ImageFilterSignatureMismatch","'%s': %8lx != %8lx",tag,
1702  (unsigned long) signature,(unsigned long)
1703  MagickImageFilterSignature);
1704  return(MagickFalse);
1705  }
1706  }
1707  }
1708 #endif
1709  return(MagickTrue);
1710 }
1711 #endif
_SplayTreeInfo
Definition: splay-tree.c:83
_ModuleInfo
Definition: module.h:38
SemaphoreInfo
Definition: semaphore.c:60
_Image
Definition: image.h:131
_CoderInfo
Definition: coder.h:25
_ExceptionInfo
Definition: exception.h:101