]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/delegate.c
Added checks for exceptions.
[imagemagick] / MagickCore / delegate.c
index 8f61a2e690f3bd5e045ee0b2c1805f4f45620e07..04defce0e42d24e8471f9867faf5e74076baf9c8 100644 (file)
 %             MagickCore Methods to Read/Write/Invoke Delegates               %
 %                                                                             %
 %                             Software Design                                 %
-%                               John Cristy                                   %
+%                                  Cristy                                     %
 %                               October 1998                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
@@ -58,6 +58,7 @@
 #include "MagickCore/hashmap.h"
 #include "MagickCore/list.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/nt-base-private.h"
 #include "MagickCore/policy.h"
 #include "MagickCore/resource_.h"
 #include "MagickCore/semaphore.h"
@@ -66,6 +67,7 @@
 #include "MagickCore/utility.h"
 #include "MagickCore/utility-private.h"
 #include "MagickCore/xml-tree.h"
+#include "MagickCore/xml-tree-private.h"
 \f
 /*
   Define declarations.
@@ -128,27 +130,89 @@ static const char
   Global declaractions.
 */
 static LinkedListInfo
-  *delegate_list = (LinkedListInfo *) NULL;
+  *delegate_cache = (LinkedListInfo *) NULL;
 
 static SemaphoreInfo
   *delegate_semaphore = (SemaphoreInfo *) NULL;
-
-static volatile MagickBooleanType
-  instantiate_delegate = MagickFalse;
 \f
 /*
   Forward declaractions.
 */
 static MagickBooleanType
-  InitializeDelegateList(ExceptionInfo *),
-  LoadDelegateLists(const char *,ExceptionInfo *);
+  IsDelegateCacheInstantiated(ExceptionInfo *),
+  LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
+    ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   D e l e g a t e C o m p o n e n t T e r m i n u s                         %
+%  A c q u i r e D e l e g a t e C a c h e                                    %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  AcquireDelegateCache() caches one or more delegate configurations which
+%  provides a mapping between delegate attributes and a delegate name.
+%
+%  The format of the AcquireDelegateCache method is:
+%
+%      LinkedListInfo *AcquireDelegateCache(const char *filename,
+%        ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o filename: the font file name.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+static LinkedListInfo *AcquireDelegateCache(const char *filename,
+  ExceptionInfo *exception)
+{
+  LinkedListInfo
+    *delegate_cache;
+
+  MagickStatusType
+    status;
+
+  delegate_cache=NewLinkedList(0);
+  if (delegate_cache == (LinkedListInfo *) NULL)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+  status=MagickTrue;
+#if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
+  {
+    const StringInfo
+      *option;
+
+    LinkedListInfo
+      *options;
+
+    options=GetConfigureOptions(filename,exception);
+    option=(const StringInfo *) GetNextValueInLinkedList(options);
+    while (option != (const StringInfo *) NULL)
+    {
+      status&=LoadDelegateCache(delegate_cache,(const char *)
+        GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
+      option=(const StringInfo *) GetNextValueInLinkedList(options);
+    }
+    options=DestroyConfigureOptions(options);
+  }
+#endif
+  if (IfMagickTrue(IsLinkedListEmpty(delegate_cache)))
+    status&=LoadDelegateCache(delegate_cache,DelegateMap,"built-in",0,
+      exception);
+  return(delegate_cache);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   D e l e g a t e C o m p o n e n t G e n e s i s                           %
 %                                                                             %
 %                                                                             %
 %                                                                             %
@@ -163,7 +227,8 @@ static MagickBooleanType
 */
 MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
 {
-  AcquireSemaphoreInfo(&delegate_semaphore);
+  if (delegate_semaphore == (SemaphoreInfo *) NULL)
+    delegate_semaphore=AcquireSemaphoreInfo();
   return(MagickTrue);
 }
 \f
@@ -200,6 +265,8 @@ static void *DestroyDelegate(void *delegate_info)
     p->encode=DestroyString(p->encode);
   if (p->commands != (char *) NULL)
     p->commands=DestroyString(p->commands);
+  if (p->semaphore != (SemaphoreInfo *) NULL)
+    RelinquishSemaphoreInfo(&p->semaphore);
   p=(DelegateInfo *) RelinquishMagickMemory(p);
   return((void *) NULL);
 }
@@ -207,13 +274,12 @@ static void *DestroyDelegate(void *delegate_info)
 MagickPrivate void DelegateComponentTerminus(void)
 {
   if (delegate_semaphore == (SemaphoreInfo *) NULL)
-    AcquireSemaphoreInfo(&delegate_semaphore);
+    ActivateSemaphoreInfo(&delegate_semaphore);
   LockSemaphoreInfo(delegate_semaphore);
-  if (delegate_list != (LinkedListInfo *) NULL)
-    delegate_list=DestroyLinkedList(delegate_list,DestroyDelegate);
-  instantiate_delegate=MagickFalse;
+  if (delegate_cache != (LinkedListInfo *) NULL)
+    delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
   UnlockSemaphoreInfo(delegate_semaphore);
-  DestroySemaphoreInfo(&delegate_semaphore);
+  RelinquishSemaphoreInfo(&delegate_semaphore);
 }
 \f
 /*
@@ -369,35 +435,33 @@ MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
     *p;
 
   assert(exception != (ExceptionInfo *) NULL);
-  if ((delegate_list == (LinkedListInfo *) NULL) ||
-      (IfMagickFalse(instantiate_delegate)))
-    if( IfMagickFalse(InitializeDelegateList(exception)) )
-      return((const DelegateInfo *) NULL);
-  if ((delegate_list == (LinkedListInfo *) NULL) ||
-      (IfMagickTrue(IsLinkedListEmpty(delegate_list))))
+  if (IfMagickFalse(IsDelegateCacheInstantiated(exception)))
     return((const DelegateInfo *) NULL);
-  if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
-    return((const DelegateInfo *) GetValueFromLinkedList(delegate_list,0));
   /*
     Search for named delegate.
   */
   LockSemaphoreInfo(delegate_semaphore);
-  ResetLinkedListIterator(delegate_list);
-  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+  ResetLinkedListIterator(delegate_cache);
+  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
+  if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
+    {
+      UnlockSemaphoreInfo(delegate_semaphore);
+      return(p);
+    }
   while (p != (const DelegateInfo *) NULL)
   {
     if (p->mode > 0)
       {
         if (LocaleCompare(p->decode,decode) == 0)
           break;
-        p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+        p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
         continue;
       }
     if (p->mode < 0)
       {
         if (LocaleCompare(p->encode,encode) == 0)
           break;
-        p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+        p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
         continue;
       }
     if (LocaleCompare(decode,p->decode) == 0)
@@ -409,11 +473,11 @@ MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
     if (LocaleCompare(decode,p->decode) == 0)
       if (LocaleCompare(encode,"*") == 0)
         break;
-    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
   }
   if (p != (const DelegateInfo *) NULL)
-    (void) InsertValueInLinkedList(delegate_list,0,
-      RemoveElementByValueFromLinkedList(delegate_list,p));
+    (void) InsertValueInLinkedList(delegate_cache,0,
+      RemoveElementByValueFromLinkedList(delegate_cache,p));
   UnlockSemaphoreInfo(delegate_semaphore);
   return(p);
 }
@@ -500,22 +564,22 @@ MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
   if (p == (const DelegateInfo *) NULL)
     return((const DelegateInfo **) NULL);
   delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
+    GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
   if (delegates == (const DelegateInfo **) NULL)
     return((const DelegateInfo **) NULL);
   /*
     Generate delegate list.
   */
   LockSemaphoreInfo(delegate_semaphore);
-  ResetLinkedListIterator(delegate_list);
-  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+  ResetLinkedListIterator(delegate_cache);
+  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
   for (i=0; p != (const DelegateInfo *) NULL; )
   {
     if( IfMagickFalse(p->stealth) &&
         ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
           IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
       delegates[i++]=p;
-    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
   }
   UnlockSemaphoreInfo(delegate_semaphore);
   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
@@ -597,12 +661,12 @@ MagickExport char **GetDelegateList(const char *pattern,
   if (p == (const DelegateInfo *) NULL)
     return((char **) NULL);
   delegates=(char **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
+    GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
   if (delegates == (char **) NULL)
     return((char **) NULL);
   LockSemaphoreInfo(delegate_semaphore);
-  ResetLinkedListIterator(delegate_list);
-  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+  ResetLinkedListIterator(delegate_cache);
+  p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
   for (i=0; p != (const DelegateInfo *) NULL; )
   {
     if( IfMagickFalse(p->stealth) &&
@@ -611,7 +675,7 @@ MagickExport char **GetDelegateList(const char *pattern,
     if( IfMagickFalse(p->stealth) &&
         IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
       delegates[i++]=ConstantString(p->encode);
-    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
+    p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
   }
   UnlockSemaphoreInfo(delegate_semaphore);
   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
@@ -690,40 +754,36 @@ MagickExport MagickBooleanType GetDelegateThreadSupport(
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n i t i a l i z e D e l e g a t e L i s t                               %
++   I s D e l e g a t e C a c h e I n s t a n t i a t e d                     %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  InitializeDelegateList() initializes the delegate list.
+%  IsDelegateCacheInstantiated() determines if the delegate cache is
+%  instantiated.  If not, it instantiates the cache and returns it.
 %
-%  The format of the InitializeDelegateList method is:
+%  The format of the IsDelegateInstantiated method is:
 %
-%      MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
+%      MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
+static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
 {
-  if ((delegate_list == (LinkedListInfo *) NULL) &&
-      IfMagickFalse(instantiate_delegate))
+  if (delegate_cache == (LinkedListInfo *) NULL)
     {
       if (delegate_semaphore == (SemaphoreInfo *) NULL)
-        AcquireSemaphoreInfo(&delegate_semaphore);
+        ActivateSemaphoreInfo(&delegate_semaphore);
       LockSemaphoreInfo(delegate_semaphore);
-      if ((delegate_list == (LinkedListInfo *) NULL) &&
-          IfMagickFalse(instantiate_delegate))
-        {
-          (void) LoadDelegateLists(DelegateFilename,exception);
-          instantiate_delegate=MagickTrue;
-        }
+      if (delegate_cache == (LinkedListInfo *) NULL)
+        delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
       UnlockSemaphoreInfo(delegate_semaphore);
     }
-  return(IsMagickNotNULL(delegate_list));
+  return(IsMagickNotNULL(delegate_cache));
 }
 \f
 /*
@@ -1195,13 +1255,14 @@ MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  LoadDelegateList() loads the delegate configuration file which provides a
+%  LoadDelegateCache() loads the delegate configurations which provides a
 %  mapping between delegate attributes and a delegate name.
 %
-%  The format of the LoadDelegateList method is:
+%  The format of the LoadDelegateCache method is:
 %
-%      MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
-%        const size_t depth,ExceptionInfo *exception)
+%      MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
+%        const char *xml,const char *filename,const size_t depth,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -1214,8 +1275,9 @@ MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
-  const size_t depth,ExceptionInfo *exception)
+static MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
+  const char *xml,const char *filename,const size_t depth,
+  ExceptionInfo *exception)
 {
   char
     keyword[MaxTextExtent],
@@ -1237,16 +1299,6 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
     "Loading delegate configuration file \"%s\" ...",filename);
   if (xml == (const char *) NULL)
     return(MagickFalse);
-  if (delegate_list == (LinkedListInfo *) NULL)
-    {
-      delegate_list=NewLinkedList(0);
-      if (delegate_list == (LinkedListInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
-    }
   status=MagickTrue;
   delegate_info=(DelegateInfo *) NULL;
   token=AcquireString(xml);
@@ -1308,10 +1360,11 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
                     (void) CopyMagickString(path,token,MaxTextExtent);
                   else
                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
-                  xml=FileToString(path,~0UL,exception);
+                  xml=FileToXML(path,~0UL);
                   if (xml != (char *) NULL)
                     {
-                      status=LoadDelegateList(xml,path,depth+1,exception);
+                      status&=LoadDelegateCache(delegate_cache,xml,path,
+                        depth+1,exception);
                       xml=(char *) RelinquishMagickMemory(xml);
                     }
                 }
@@ -1330,6 +1383,7 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
         (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
         delegate_info->path=ConstantString(filename);
+        delegate_info->thread_support=MagickTrue;
         delegate_info->signature=MagickSignature;
         continue;
       }
@@ -1337,12 +1391,13 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
       continue;
     if (LocaleCompare(keyword,"/>") == 0)
       {
-        status=AppendValueToLinkedList(delegate_list,delegate_info);
+        status=AppendValueToLinkedList(delegate_cache,delegate_info);
         if( IfMagickFalse(status) )
           (void) ThrowMagickException(exception,GetMagickModule(),
             ResourceLimitError,"MemoryAllocationFailed","`%s'",
             delegate_info->commands);
         delegate_info=(DelegateInfo *) NULL;
+        continue;
       }
     GetMagickToken(q,(const char **) NULL,token);
     if (*token != '=')
@@ -1439,6 +1494,8 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
         if (LocaleCompare((char *) keyword,"thread-support") == 0)
           {
             delegate_info->thread_support=IsStringTrue(token);
+            if (delegate_info->thread_support == MagickFalse)
+              delegate_info->semaphore=AcquireSemaphoreInfo();
             break;
           }
         break;
@@ -1450,61 +1507,3 @@ static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
   token=(char *) RelinquishMagickMemory(token);
   return(status);
 }
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%  L o a d D e l e g a t e L i s t s                                          %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  LoadDelegateList() loads one or more delegate configuration file which
-%  provides a mapping between delegate attributes and a delegate name.
-%
-%  The format of the LoadDelegateLists method is:
-%
-%      MagickBooleanType LoadDelegateLists(const char *filename,
-%        ExceptionInfo *exception)
-%
-%  A description of each parameter follows:
-%
-%    o filename: the font file name.
-%
-%    o exception: return any errors or warnings in this structure.
-%
-*/
-static MagickBooleanType LoadDelegateLists(const char *filename,
-  ExceptionInfo *exception)
-{
-#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
-  return(LoadDelegateList(DelegateMap,"built-in",0,exception));
-#else
-  const StringInfo
-    *option;
-
-  LinkedListInfo
-    *options;
-
-  MagickStatusType
-    status;
-
-  status=MagickFalse;
-  options=GetConfigureOptions(filename,exception);
-  option=(const StringInfo *) GetNextValueInLinkedList(options);
-  while (option != (const StringInfo *) NULL)
-  {
-    status&=LoadDelegateList((const char *) GetStringInfoDatum(option),
-      GetStringInfoPath(option),0,exception);
-    option=(const StringInfo *) GetNextValueInLinkedList(options);
-  }
-  options=DestroyConfigureOptions(options);
-  if ((delegate_list == (LinkedListInfo *) NULL) ||
-      (IfMagickTrue(IsLinkedListEmpty(delegate_list))))
-    status&=LoadDelegateList(DelegateMap,"built-in",0,exception);
-  return(IsMagickTrue(status!=0));
-#endif
-}