MagickCore 6.9.13-51
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
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"
70typedef lt_dlhandle ModuleHandle;
71#else
72typedef 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*/
91static SemaphoreInfo
92 *module_semaphore = (SemaphoreInfo *) NULL;
93
94static SplayTreeInfo
95 *module_list = (SplayTreeInfo *) NULL;
96
97/*
98 Forward declarations.
99*/
100static const ModuleInfo
101 *RegisterModule(const ModuleInfo *,ExceptionInfo *);
102
103static MagickBooleanType
104 GetMagickModulePath(const char *,MagickModuleType,char *,ExceptionInfo *),
105 IsModuleTreeInstantiated(ExceptionInfo *),
106 UnregisterModule(const ModuleInfo *,ExceptionInfo *);
107
108static 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*/
138MagickExport 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*/
175MagickExport 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*/
216MagickExport 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)
269extern "C" {
270#endif
271
272static 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
289MagickExport 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)
369extern "C" {
370#endif
371
372static 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
387MagickExport 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*/
529static 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
864static void *DestroyModuleNode(void *module_info)
865{
866 ExceptionInfo
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
884static 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
902 SplayTreeInfo
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
928MagickExport 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*/
966MagickExport 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 ThrowPolicyException(tag,MagickFalse);
996#if !defined(MAGICKCORE_BUILD_MODULES)
997 {
998 MagickBooleanType
999 status;
1000
1001 status=InvokeStaticImageFilter(tag,images,argc,argv,exception);
1002 if (status != MagickFalse)
1003 return(status);
1004 }
1005#endif
1006 TagToFilterModuleName(tag,name);
1007 status=GetMagickModulePath(name,MagickImageFilterModule,path,exception);
1008 if (status == MagickFalse)
1009 {
1010 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1011 "UnableToLoadModule","`%s': %s",name,path);
1012 return(MagickFalse);
1013 }
1014 /*
1015 Open the module.
1016 */
1017 handle=(ModuleHandle) lt_dlopen(path);
1018 if (handle == (ModuleHandle) NULL)
1019 {
1020 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1021 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
1022 return(MagickFalse);
1023 }
1024 /*
1025 Locate the module.
1026 */
1027#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1028 (void) FormatLocaleString(name,MaxTextExtent,"%sImage",tag);
1029#else
1030 (void) FormatLocaleString(name,MaxTextExtent,"%s%sImage",
1031 MAGICKCORE_NAMESPACE_PREFIX_TAG,tag);
1032#endif
1033 /*
1034 Execute the module.
1035 */
1036 ClearMagickException(exception);
1037 image_filter=(ImageFilterHandler *) lt_dlsym(handle,name);
1038 if (image_filter == (ImageFilterHandler *) NULL)
1039 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1040 "UnableToLoadModule","`%s': %s",name,lt_dlerror());
1041 else
1042 {
1043 size_t
1044 signature;
1045
1046 if (IsEventLogging() != MagickFalse)
1047 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1048 "Invoking \"%s\" dynamic image filter",tag);
1049 signature=image_filter(images,argc,argv,exception);
1050 if (IsEventLogging() != MagickFalse)
1051 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),"\"%s\" completes",
1052 tag);
1053 if (signature != MagickImageFilterSignature)
1054 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1055 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
1056 (unsigned long) signature,(unsigned long) MagickImageFilterSignature);
1057 }
1058 /*
1059 Close the module.
1060 */
1061 if (lt_dlclose(handle) != 0)
1062 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1063 "UnableToCloseModule","`%s': %s",name,lt_dlerror());
1064 return(exception->severity < ErrorException ? MagickTrue : MagickFalse);
1065}
1066
1067/*
1068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1069% %
1070% %
1071% %
1072% L i s t M o d u l e I n f o %
1073% %
1074% %
1075% %
1076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1077%
1078% ListModuleInfo() lists the module info to a file.
1079%
1080% The format of the ListModuleInfo module is:
1081%
1082% MagickBooleanType ListModuleInfo(FILE *file,ExceptionInfo *exception)
1083%
1084% A description of each parameter follows.
1085%
1086% o file: An pointer to a FILE.
1087%
1088% o exception: return any errors or warnings in this structure.
1089%
1090*/
1091MagickExport MagickBooleanType ListModuleInfo(FILE *file,
1092 ExceptionInfo *exception)
1093{
1094 char
1095 filename[MaxTextExtent],
1096 module_path[MaxTextExtent],
1097 **modules,
1098 path[MaxTextExtent];
1099
1100 ssize_t
1101 i;
1102
1103 size_t
1104 number_modules;
1105
1106 if (file == (const FILE *) NULL)
1107 file=stdout;
1108 /*
1109 List image coders.
1110 */
1111 modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
1112 if (modules == (char **) NULL)
1113 return(MagickFalse);
1114 TagToCoderModuleName("magick",filename);
1115 (void) GetMagickModulePath(filename,MagickImageCoderModule,module_path,
1116 exception);
1117 GetPathComponent(module_path,HeadPath,path);
1118 (void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
1119 (void) FormatLocaleFile(file,"Image Coder\n");
1120 (void) FormatLocaleFile(file,
1121 "-------------------------------------------------"
1122 "------------------------------\n");
1123 for (i=0; i < (ssize_t) number_modules; i++)
1124 {
1125 (void) FormatLocaleFile(file,"%s",modules[i]);
1126 (void) FormatLocaleFile(file,"\n");
1127 }
1128 (void) fflush(file);
1129 /*
1130 Relinquish resources.
1131 */
1132 for (i=0; i < (ssize_t) number_modules; i++)
1133 modules[i]=DestroyString(modules[i]);
1134 modules=(char **) RelinquishMagickMemory(modules);
1135 /*
1136 List image filters.
1137 */
1138 modules=GetModuleList("*",MagickImageFilterModule,&number_modules,exception);
1139 if (modules == (char **) NULL)
1140 return(MagickFalse);
1141 TagToFilterModuleName("analyze",filename);
1142 (void) GetMagickModulePath(filename,MagickImageFilterModule,module_path,
1143 exception);
1144 GetPathComponent(module_path,HeadPath,path);
1145 (void) FormatLocaleFile(file,"\nPath: %s\n\n",path);
1146 (void) FormatLocaleFile(file,"Image Filter\n");
1147 (void) FormatLocaleFile(file,
1148 "-------------------------------------------------"
1149 "------------------------------\n");
1150 for (i=0; i < (ssize_t) number_modules; i++)
1151 {
1152 (void) FormatLocaleFile(file,"%s",modules[i]);
1153 (void) FormatLocaleFile(file,"\n");
1154 }
1155 (void) fflush(file);
1156 /*
1157 Relinquish resources.
1158 */
1159 for (i=0; i < (ssize_t) number_modules; i++)
1160 modules[i]=DestroyString(modules[i]);
1161 modules=(char **) RelinquishMagickMemory(modules);
1162 return(MagickTrue);
1163}
1164
1165/*
1166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167% %
1168% %
1169% %
1170+ M o d u l e C o m p o n e n t G e n e s i s %
1171% %
1172% %
1173% %
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175%
1176% ModuleComponentGenesis() instantiates the module component.
1177%
1178% The format of the ModuleComponentGenesis method is:
1179%
1180% MagickBooleanType ModuleComponentGenesis(void)
1181%
1182*/
1183MagickExport MagickBooleanType ModuleComponentGenesis(void)
1184{
1185 ExceptionInfo
1186 *exception;
1187
1188 MagickBooleanType
1189 status;
1190
1191 if (module_semaphore == (SemaphoreInfo *) NULL)
1192 module_semaphore=AllocateSemaphoreInfo();
1193 exception=AcquireExceptionInfo();
1194 status=IsModuleTreeInstantiated(exception);
1195 exception=DestroyExceptionInfo(exception);
1196 return(status);
1197}
1198
1199/*
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201% %
1202% %
1203% %
1204+ M o d u l e C o m p o n e n t T e r m i n u s %
1205% %
1206% %
1207% %
1208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209%
1210% ModuleComponentTerminus() destroys the module component.
1211%
1212% The format of the ModuleComponentTerminus method is:
1213%
1214% ModuleComponentTerminus(void)
1215%
1216*/
1217MagickExport void ModuleComponentTerminus(void)
1218{
1219 if (module_semaphore == (SemaphoreInfo *) NULL)
1220 ActivateSemaphoreInfo(&module_semaphore);
1221 DestroyModuleList();
1222 DestroySemaphoreInfo(&module_semaphore);
1223}
1224
1225/*
1226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227% %
1228% %
1229% %
1230% O p e n M o d u l e %
1231% %
1232% %
1233% %
1234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235%
1236% OpenModule() loads a module, and invokes its registration module. It
1237% returns MagickTrue on success, and MagickFalse if there is an error.
1238%
1239% The format of the OpenModule module is:
1240%
1241% MagickBooleanType OpenModule(const char *module,ExceptionInfo *exception)
1242%
1243% A description of each parameter follows:
1244%
1245% o module: a character string that indicates the module to load.
1246%
1247% o exception: return any errors or warnings in this structure.
1248%
1249*/
1250MagickExport MagickBooleanType OpenModule(const char *module,
1251 ExceptionInfo *exception)
1252{
1253 char
1254 module_name[MaxTextExtent],
1255 name[MaxTextExtent],
1256 path[MaxTextExtent];
1257
1258 MagickBooleanType
1259 status;
1260
1261 ModuleHandle
1262 handle;
1263
1264 ModuleInfo
1265 *module_info;
1266
1267 PolicyRights
1268 rights;
1269
1270 const CoderInfo
1271 *p;
1272
1273 size_t
1274 signature;
1275
1276 /*
1277 Assign module name from alias.
1278 */
1279 assert(module != (const char *) NULL);
1280 module_info=(ModuleInfo *) GetModuleInfo(module,exception);
1281 if (module_info != (ModuleInfo *) NULL)
1282 return(MagickTrue);
1283 (void) CopyMagickString(module_name,module,MaxTextExtent);
1284 p=GetCoderInfo(module,exception);
1285 if (p != (CoderInfo *) NULL)
1286 (void) CopyMagickString(module_name,p->name,MaxTextExtent);
1287 LocaleUpper(module_name);
1288 rights=(PolicyRights) (ReadPolicyRights | WritePolicyRights);
1289 if (IsRightsAuthorized(ModulePolicyDomain,rights,module_name) == MagickFalse)
1290 ThrowPolicyException(module_name,MagickFalse);
1291 if (GetValueFromSplayTree(module_list,module_name) != (void *) NULL)
1292 return(MagickTrue); /* module already opened, return */
1293 /*
1294 Locate module.
1295 */
1296 handle=(ModuleHandle) NULL;
1297 TagToCoderModuleName(module_name,name);
1298 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1299 "Searching for module \"%s\" using filename \"%s\"",module_name,name);
1300 *path='\0';
1301 status=GetMagickModulePath(name,MagickImageCoderModule,path,exception);
1302 if (status == MagickFalse)
1303 return(MagickFalse);
1304 /*
1305 Load module
1306 */
1307 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1308 "Opening module at path \"%s\"",path);
1309 handle=(ModuleHandle) lt_dlopen(path);
1310 if (handle == (ModuleHandle) NULL)
1311 {
1312 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1313 "UnableToLoadModule","`%s': %s",path,lt_dlerror());
1314 return(MagickFalse);
1315 }
1316 /*
1317 Register module.
1318 */
1319 module_info=AcquireModuleInfo(path,module_name);
1320 module_info->handle=handle;
1321 if (RegisterModule(module_info,exception) == (ModuleInfo *) NULL)
1322 return(MagickFalse);
1323 /*
1324 Define RegisterFORMATImage method.
1325 */
1326 TagToModuleName(module_name,"Register%sImage",name);
1327 module_info->register_module=(size_t (*)(void)) lt_dlsym(handle,name);
1328 if (module_info->register_module == (size_t (*)(void)) NULL)
1329 {
1330 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1331 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1332 return(MagickFalse);
1333 }
1334 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1335 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1336 (void *) module_info->register_module);
1337 /*
1338 Define UnregisterFORMATImage method.
1339 */
1340 TagToModuleName(module_name,"Unregister%sImage",name);
1341 module_info->unregister_module=(void (*)(void)) lt_dlsym(handle,name);
1342 if (module_info->unregister_module == (void (*)(void)) NULL)
1343 {
1344 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1345 "UnableToRegisterImageFormat","`%s': %s",module_name,lt_dlerror());
1346 return(MagickFalse);
1347 }
1348 (void) LogMagickEvent(ModuleEvent,GetMagickModule(),
1349 "Method \"%s\" in module \"%s\" at address %p",name,module_name,
1350 (void *) module_info->unregister_module);
1351 signature=module_info->register_module();
1352 if (signature != MagickImageCoderSignature)
1353 {
1354 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1355 "ImageCoderSignatureMismatch","`%s': %8lx != %8lx",module_name,
1356 (unsigned long) signature,(unsigned long) MagickImageCoderSignature);
1357 return(MagickFalse);
1358 }
1359 return(MagickTrue);
1360}
1361
1362/*
1363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364% %
1365% %
1366% %
1367% O p e n M o d u l e s %
1368% %
1369% %
1370% %
1371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372%
1373% OpenModules() loads all available modules.
1374%
1375% The format of the OpenModules module is:
1376%
1377% MagickBooleanType OpenModules(ExceptionInfo *exception)
1378%
1379% A description of each parameter follows:
1380%
1381% o exception: return any errors or warnings in this structure.
1382%
1383*/
1384MagickExport MagickBooleanType OpenModules(ExceptionInfo *exception)
1385{
1386 char
1387 **modules;
1388
1389 ssize_t
1390 i;
1391
1392 size_t
1393 number_modules;
1394
1395 /*
1396 Load all modules.
1397 */
1398 (void) GetMagickInfo((char *) NULL,exception);
1399 number_modules=0;
1400 modules=GetModuleList("*",MagickImageCoderModule,&number_modules,exception);
1401 if ((modules == (char **) NULL) || (*modules == (char *) NULL))
1402 {
1403 if (modules != (char **) NULL)
1404 modules=(char **) RelinquishMagickMemory(modules);
1405 return(MagickFalse);
1406 }
1407 for (i=0; i < (ssize_t) number_modules; i++)
1408 (void) OpenModule(modules[i],exception);
1409 /*
1410 Relinquish resources.
1411 */
1412 for (i=0; i < (ssize_t) number_modules; i++)
1413 modules[i]=DestroyString(modules[i]);
1414 modules=(char **) RelinquishMagickMemory(modules);
1415 return(MagickTrue);
1416}
1417
1418/*
1419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420% %
1421% %
1422% %
1423% R e g i s t e r M o d u l e %
1424% %
1425% %
1426% %
1427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428%
1429% RegisterModule() adds an entry to the module list. It returns a pointer to
1430% the registered entry on success.
1431%
1432% The format of the RegisterModule module is:
1433%
1434% ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1435% ExceptionInfo *exception)
1436%
1437% A description of each parameter follows:
1438%
1439% o info: a pointer to the registered entry is returned.
1440%
1441% o module_info: a pointer to the ModuleInfo structure to register.
1442%
1443% o exception: return any errors or warnings in this structure.
1444%
1445*/
1446static const ModuleInfo *RegisterModule(const ModuleInfo *module_info,
1447 ExceptionInfo *exception)
1448{
1449 MagickBooleanType
1450 status;
1451
1452 assert(module_info != (ModuleInfo *) NULL);
1453 assert(module_info->signature == MagickCoreSignature);
1454 if (IsEventLogging() != MagickFalse)
1455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1456 if (module_list == (SplayTreeInfo *) NULL)
1457 return((const ModuleInfo *) NULL);
1458 status=AddValueToSplayTree(module_list,module_info->tag,module_info);
1459 if (status == MagickFalse)
1460 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
1461 "MemoryAllocationFailed","`%s'",module_info->tag);
1462 return(module_info);
1463}
1464
1465/*
1466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467% %
1468% %
1469% %
1470% T a g T o C o d e r M o d u l e N a m e %
1471% %
1472% %
1473% %
1474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475%
1476% TagToCoderModuleName() munges a module tag and obtains the filename of the
1477% corresponding module.
1478%
1479% The format of the TagToCoderModuleName module is:
1480%
1481% char *TagToCoderModuleName(const char *tag,char *name)
1482%
1483% A description of each parameter follows:
1484%
1485% o tag: a character string representing the module tag.
1486%
1487% o name: return the module name here.
1488%
1489*/
1490static void TagToCoderModuleName(const char *tag,char *name)
1491{
1492 assert(tag != (char *) NULL);
1493 assert(name != (char *) NULL);
1494 if (IsEventLogging() != MagickFalse)
1495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1496#if defined(MAGICKCORE_LTDL_DELEGATE)
1497 (void) FormatLocaleString(name,MaxTextExtent,"%s.la",tag);
1498 (void) LocaleLower(name);
1499#else
1500#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1501 if (LocaleNCompare("IM_MOD_",tag,7) == 0)
1502 (void) CopyMagickString(name,tag,MaxTextExtent);
1503 else
1504 {
1505#if defined(_DEBUG)
1506 (void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_DB_%s_.dll",tag);
1507#else
1508 (void) FormatLocaleString(name,MaxTextExtent,"IM_MOD_RL_%s_.dll",tag);
1509#endif
1510 }
1511#endif
1512#endif
1513}
1514
1515/*
1516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1517% %
1518% %
1519% %
1520% T a g T o F i l t e r M o d u l e N a m e %
1521% %
1522% %
1523% %
1524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525%
1526% TagToFilterModuleName() munges a module tag and returns the filename of the
1527% corresponding filter module.
1528%
1529% The format of the TagToFilterModuleName module is:
1530%
1531% void TagToFilterModuleName(const char *tag,char name)
1532%
1533% A description of each parameter follows:
1534%
1535% o tag: a character string representing the module tag.
1536%
1537% o name: return the filter name here.
1538%
1539*/
1540static void TagToFilterModuleName(const char *tag,char *name)
1541{
1542 assert(tag != (char *) NULL);
1543 assert(name != (char *) NULL);
1544 if (IsEventLogging() != MagickFalse)
1545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1546#if !defined(MAGICKCORE_LTDL_DELEGATE)
1547 (void) FormatLocaleString(name,MaxTextExtent,"%s.dll",tag);
1548#else
1549 (void) FormatLocaleString(name,MaxTextExtent,"%s.la",tag);
1550#endif
1551}
1552
1553/*
1554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555% %
1556% %
1557% %
1558% T a g T o M o d u l e N a m e %
1559% %
1560% %
1561% %
1562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1563%
1564% TagToModuleName() munges the module tag name and returns an upper-case tag
1565% name as the input string, and a user-provided format.
1566%
1567% The format of the TagToModuleName module is:
1568%
1569% TagToModuleName(const char *tag,const char *format,char *module)
1570%
1571% A description of each parameter follows:
1572%
1573% o tag: the module tag.
1574%
1575% o format: a sprintf-compatible format string containing %s where the
1576% upper-case tag name is to be inserted.
1577%
1578% o module: pointer to a destination buffer for the formatted result.
1579%
1580*/
1581static void TagToModuleName(const char *tag,const char *format,char *module)
1582{
1583 char
1584 name[MaxTextExtent];
1585
1586 assert(tag != (const char *) NULL);
1587 assert(format != (const char *) NULL);
1588 assert(module != (char *) NULL);
1589 if (IsEventLogging() != MagickFalse)
1590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tag);
1591 (void) CopyMagickString(name,tag,MaxTextExtent);
1592 LocaleUpper(name);
1593#if !defined(MAGICKCORE_NAMESPACE_PREFIX)
1594 (void) FormatLocaleString(module,MaxTextExtent,format,name);
1595#else
1596 {
1597 char
1598 prefix_format[MaxTextExtent];
1599
1600 (void) FormatLocaleString(prefix_format,MaxTextExtent,"%s%s",
1601 MAGICKCORE_NAMESPACE_PREFIX_TAG,format);
1602 (void) FormatLocaleString(module,MaxTextExtent,prefix_format,name);
1603 }
1604#endif
1605}
1606
1607/*
1608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609% %
1610% %
1611% %
1612% U n r e g i s t e r M o d u l e %
1613% %
1614% %
1615% %
1616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617%
1618% UnregisterModule() unloads a module, and invokes its de-registration module.
1619% Returns MagickTrue on success, and MagickFalse if there is an error.
1620%
1621% The format of the UnregisterModule module is:
1622%
1623% MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1624% ExceptionInfo *exception)
1625%
1626% A description of each parameter follows:
1627%
1628% o module_info: the module info.
1629%
1630% o exception: return any errors or warnings in this structure.
1631%
1632*/
1633static MagickBooleanType UnregisterModule(const ModuleInfo *module_info,
1634 ExceptionInfo *exception)
1635{
1636 /*
1637 Locate and execute UnregisterFORMATImage module.
1638 */
1639 assert(module_info != (const ModuleInfo *) NULL);
1640 assert(exception != (ExceptionInfo *) NULL);
1641 if (IsEventLogging() != MagickFalse)
1642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",module_info->tag);
1643 if (module_info->unregister_module == NULL)
1644 return(MagickTrue);
1645 module_info->unregister_module();
1646 if (lt_dlclose((ModuleHandle) module_info->handle) != 0)
1647 {
1648 (void) ThrowMagickException(exception,GetMagickModule(),ModuleWarning,
1649 "UnableToCloseModule","`%s': %s",module_info->tag,lt_dlerror());
1650 return(MagickFalse);
1651 }
1652 return(MagickTrue);
1653}
1654#else
1655
1656#if !defined(MAGICKCORE_BUILD_MODULES)
1657extern size_t
1658 analyzeImage(Image **,const int,const char **,ExceptionInfo *);
1659#endif
1660
1661MagickExport MagickBooleanType ListModuleInfo(FILE *magick_unused(file),
1662 ExceptionInfo *magick_unused(exception))
1663{
1664 magick_unreferenced(file);
1665 magick_unreferenced(exception);
1666 return(MagickTrue);
1667}
1668
1669MagickExport MagickBooleanType InvokeDynamicImageFilter(const char *tag,
1670 Image **image,const int argc,const char **argv,ExceptionInfo *exception)
1671{
1672 PolicyRights
1673 rights;
1674
1675 assert(image != (Image **) NULL);
1676 assert((*image)->signature == MagickCoreSignature);
1677 if (IsEventLogging() != MagickFalse)
1678 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
1679 rights=ReadPolicyRights;
1680 if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
1681 ThrowPolicyException(tag,MagickFalse);
1682#if defined(MAGICKCORE_BUILD_MODULES)
1683 (void) tag;
1684 (void) argc;
1685 (void) argv;
1686 (void) exception;
1687#else
1688 {
1689 ImageFilterHandler
1690 *image_filter;
1691
1692 image_filter=(ImageFilterHandler *) NULL;
1693 if (LocaleCompare("analyze",tag) == 0)
1694 image_filter=(ImageFilterHandler *) analyzeImage;
1695 if (image_filter == (ImageFilterHandler *) NULL)
1696 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1697 "UnableToLoadModule","`%s'",tag);
1698 else
1699 {
1700 size_t
1701 signature;
1702
1703 if ((*image)->debug != MagickFalse)
1704 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1705 "Invoking \"%s\" static image filter",tag);
1706 signature=image_filter(image,argc,argv,exception);
1707 if ((*image)->debug != MagickFalse)
1708 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1709 "\"%s\" completes",tag);
1710 if (signature != MagickImageFilterSignature)
1711 {
1712 (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
1713 "ImageFilterSignatureMismatch","`%s': %8lx != %8lx",tag,
1714 (unsigned long) signature,(unsigned long)
1715 MagickImageFilterSignature);
1716 return(MagickFalse);
1717 }
1718 }
1719 }
1720#endif
1721 return(MagickTrue);
1722}
1723#endif
Definition mac.h:54