Home | History | Annotate | Download | only in MagickCore
      1 /*
      2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      3 %                                                                             %
      4 %                                                                             %
      5 %                                                                             %
      6 %                  SSSSS  TTTTT   AAA   TTTTT  IIIII   CCCC                   %
      7 %                  SS       T    A   A    T      I    C                       %
      8 %                   SSS     T    AAAAA    T      I    C                       %
      9 %                     SS    T    A   A    T      I    C                       %
     10 %                  SSSSS    T    A   A    T    IIIII   CCCC                   %
     11 %                                                                             %
     12 %                                                                             %
     13 %                          MagickCore Static Methods                          %
     14 %                                                                             %
     15 %                              Software Design                                %
     16 %                                   Cristy                                    %
     17 %                                 March 2000                                  %
     18 %                                                                             %
     19 %                                                                             %
     20 %  Copyright 1999-2019 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 /*
     42   Include declarations.
     43 */
     44 #include "MagickCore/studio.h"
     45 #include "MagickCore/coder.h"
     46 #include "MagickCore/exception-private.h"
     47 #include "MagickCore/image.h"
     48 #include "MagickCore/module.h"
     49 #include "MagickCore/policy.h"
     50 #include "MagickCore/static.h"
     51 #include "MagickCore/string_.h"
     52 #include "coders/coders.h"
     53 
     54 /*
     56   Define declarations.
     57 */
     58 #define AddMagickCoder(coder)  { #coder, MagickFalse, \
     59   Register ## coder ## Image, Unregister ## coder ## Image },
     60 
     61 /*
     63   ImageMagick module stub.
     64 */
     65 ModuleExport size_t RegisterUndefinedImage(void)
     66 {
     67   return(MagickImageCoderSignature);
     68 }
     69 
     70 ModuleExport void UnregisterUndefinedImage(void)
     71 {
     72 }
     73 
     74 /*
     76   ImageMagick modules.
     77 */
     78 static struct
     79 {
     80   const char
     81     *module;
     82 
     83   MagickBooleanType
     84     registered;
     85 
     86   size_t
     87     (*register_module)(void);
     88 
     89   void
     90     (*unregister_module)(void);
     91 } MagickModules[] = {
     92 #if !defined(MAGICKCORE_BUILD_MODULES)
     93   #include "coders/coders-list.h"
     94 #endif
     95   { (const char *) NULL, MagickFalse, RegisterUndefinedImage, UnregisterUndefinedImage }
     96 };
     97 
     98 /*
    100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    101 %                                                                             %
    102 %                                                                             %
    103 %                                                                             %
    104 %   I n v o k e S t a t i c I m a g e F i l t e r                             %
    105 %                                                                             %
    106 %                                                                             %
    107 %                                                                             %
    108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    109 %
    110 %  InvokeStaticImageFilter() invokes a static image filter.
    111 %
    112 %  The format of the InvokeStaticImageFilter method is:
    113 %
    114 %      MagickBooleanType InvokeStaticImageFilter(const char *tag,Image **image,
    115 %        const int argc,const char **argv)
    116 %
    117 %  A description of each parameter follows:
    118 %
    119 %    o tag: the module tag.
    120 %
    121 %    o image: the image.
    122 %
    123 %    o argc: the number of elements in the argument vector.
    124 %
    125 %    o argv: A text array containing the command line arguments.
    126 %
    127 %    o argv: A text array containing the command line arguments.
    128 %
    129 %    o exception: return any errors or warnings in this structure.
    130 %
    131 */
    132 #if defined(MAGICKCORE_MODULES_SUPPORT)
    133 MagickExport MagickBooleanType InvokeStaticImageFilter(const char *tag,
    134   Image **image,const int argc,const char **argv,ExceptionInfo *exception)
    135 {
    136   PolicyRights
    137     rights;
    138 
    139   assert(image != (Image **) NULL);
    140   assert((*image)->signature == MagickCoreSignature);
    141   if ((*image)->debug != MagickFalse)
    142     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
    143   rights=ReadPolicyRights;
    144   if (IsRightsAuthorized(FilterPolicyDomain,rights,tag) == MagickFalse)
    145     {
    146       errno=EPERM;
    147       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
    148         "NotAuthorized","`%s'",tag);
    149       return(MagickFalse);
    150     }
    151 #if defined(MAGICKCORE_MODULES_SUPPORT)
    152   (void) tag;
    153   (void) argc;
    154   (void) argv;
    155   (void) exception;
    156 #else
    157   {
    158     extern size_t
    159       analyzeImage(Image **,const int,char **,ExceptionInfo *);
    160 
    161     ImageFilterHandler
    162       *image_filter;
    163 
    164     image_filter=(ImageFilterHandler *) NULL;
    165     if (LocaleCompare("analyze",tag) == 0)
    166       image_filter=(ImageFilterHandler *) analyzeImage;
    167     if (image_filter == (ImageFilterHandler *) NULL)
    168       (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
    169         "UnableToLoadModule","`%s'",tag);
    170     else
    171       {
    172         size_t
    173           signature;
    174 
    175         if ((*image)->debug != MagickFalse)
    176           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
    177             "Invoking \"%s\" static image filter",tag);
    178         signature=image_filter(image,argc,argv,exception);
    179         if ((*image)->debug != MagickFalse)
    180           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"\"%s\" completes",
    181             tag);
    182         if (signature != MagickImageFilterSignature)
    183           {
    184             (void) ThrowMagickException(exception,GetMagickModule(),ModuleError,
    185               "ImageFilterSignatureMismatch","'%s': %8lx != %8lx",tag,
    186               (unsigned long) signature,(unsigned long)
    187               MagickImageFilterSignature);
    188             return(MagickFalse);
    189           }
    190       }
    191   }
    192 #endif
    193   return(MagickTrue);
    194 }
    195 #endif
    196 
    197 /*
    199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    200 %                                                                             %
    201 %                                                                             %
    202 %                                                                             %
    203 %   R e g i s t e r S t a t i c M o d u l e                                   %
    204 %                                                                             %
    205 %                                                                             %
    206 %                                                                             %
    207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    208 %
    209 %  RegisterStaticModule() statically registers a module.
    210 %
    211 %  The format of the RegisterStaticModule method is:
    212 %
    213 %      MagickBooleanType RegisterStaticModule(const char module,
    214 %        ExceptionInfo *exception)
    215 %
    216 %  A description of each parameter follows:
    217 %
    218 %    o module: the want to register.
    219 %
    220 %    o exception: return any errors or warnings in this structure.
    221 %
    222 */
    223 MagickExport MagickBooleanType RegisterStaticModule(const char *module,
    224   ExceptionInfo *exception)
    225 {
    226   char
    227     module_name[MagickPathExtent];
    228 
    229   PolicyRights
    230     rights;
    231 
    232   register const CoderInfo
    233     *p;
    234 
    235   size_t
    236     extent;
    237 
    238   ssize_t
    239     i;
    240 
    241   /*
    242     Assign module name from alias.
    243   */
    244   assert(module != (const char *) NULL);
    245   rights=ReadPolicyRights;
    246   if (IsRightsAuthorized(ModulePolicyDomain,rights,module) == MagickFalse)
    247     {
    248       errno=EPERM;
    249       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
    250         "NotAuthorized","`%s'",module);
    251       return(MagickFalse);
    252     }
    253   (void) CopyMagickString(module_name,module,MagickPathExtent);
    254   p=GetCoderInfo(module,exception);
    255   if (p != (CoderInfo *) NULL)
    256     (void) CopyMagickString(module_name,p->name,MagickPathExtent);
    257   extent=sizeof(MagickModules)/sizeof(MagickModules[0]);
    258   for (i=0; i < (ssize_t) extent; i++)
    259     if (LocaleCompare(MagickModules[i].module,module_name) == 0)
    260       {
    261         if (MagickModules[i].registered == MagickFalse)
    262           {
    263             (void) (MagickModules[i].register_module)();
    264             MagickModules[i].registered=MagickTrue;
    265           }
    266         return(MagickTrue);
    267       }
    268   return(MagickFalse);
    269 }
    270 
    271 /*
    273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    274 %                                                                             %
    275 %                                                                             %
    276 %                                                                             %
    277 %   R e g i s t e r S t a t i c M o d u l e s                                 %
    278 %                                                                             %
    279 %                                                                             %
    280 %                                                                             %
    281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    282 %
    283 %  RegisterStaticModules() statically registers all the available module
    284 %  handlers.
    285 %
    286 %  The format of the RegisterStaticModules method is:
    287 %
    288 %      (void) RegisterStaticModules(void)
    289 %
    290 */
    291 MagickExport void RegisterStaticModules(void)
    292 {
    293   size_t
    294     extent;
    295 
    296   ssize_t
    297     i;
    298 
    299   extent=sizeof(MagickModules)/sizeof(MagickModules[0]);
    300   for (i=0; i < (ssize_t) extent; i++)
    301   {
    302     if (MagickModules[i].registered == MagickFalse)
    303       {
    304         (void) (MagickModules[i].register_module)();
    305         MagickModules[i].registered=MagickTrue;
    306       }
    307   }
    308 }
    309 
    310 /*
    312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    313 %                                                                             %
    314 %                                                                             %
    315 %                                                                             %
    316 %   U n r e g i s t e r S t a t i c M o d u l e                               %
    317 %                                                                             %
    318 %                                                                             %
    319 %                                                                             %
    320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    321 %
    322 %  UnregisterStaticModule() statically unregisters the named module.
    323 %
    324 %  The format of the UnregisterStaticModule method is:
    325 %
    326 %      MagickBooleanType UnregisterStaticModule(const char *module)
    327 %
    328 %  A description of each parameter follows:
    329 %
    330 %    o module: the module we want to unregister.
    331 %
    332 */
    333 MagickExport MagickBooleanType UnregisterStaticModule(const char *module)
    334 {
    335   size_t
    336     extent;
    337 
    338   ssize_t
    339     i;
    340 
    341   extent=sizeof(MagickModules)/sizeof(MagickModules[0]);
    342   for (i=0; i < (ssize_t) extent; i++)
    343     if (LocaleCompare(MagickModules[i].module,module) == 0)
    344       {
    345         if (MagickModules[i].registered != MagickFalse)
    346           {
    347             (MagickModules[i].unregister_module)();
    348             MagickModules[i].registered=MagickFalse;
    349           }
    350         return(MagickTrue);
    351       }
    352   return(MagickFalse);
    353 }
    354 
    355 /*
    357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    358 %                                                                             %
    359 %                                                                             %
    360 %                                                                             %
    361 %   U n r e g i s t e r S t a t i c M o d u l e s                             %
    362 %                                                                             %
    363 %                                                                             %
    364 %                                                                             %
    365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    366 %
    367 %  UnregisterStaticModules() statically unregisters all the available module
    368 %  handlers.
    369 %
    370 %  The format of the UnregisterStaticModules method is:
    371 %
    372 %      UnregisterStaticModules(void)
    373 %
    374 */
    375 MagickExport void UnregisterStaticModules(void)
    376 {
    377   size_t
    378     extent;
    379 
    380   ssize_t
    381     i;
    382 
    383   extent=sizeof(MagickModules)/sizeof(MagickModules[0]);
    384   for (i=0; i < (ssize_t) extent; i++)
    385   {
    386     if (MagickModules[i].registered != MagickFalse)
    387       {
    388         (MagickModules[i].unregister_module)();
    389         MagickModules[i].registered=MagickFalse;
    390       }
    391   }
    392 }
    393