• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %           RRRR    EEEEE    GGG   IIIII  SSSSS  TTTTT  RRRR   Y   Y          %
7 %           R   R   E       G        I    SS       T    R   R   Y Y           %
8 %           RRRR    EEE     G GGG    I     SSS     T    RRRR     Y            %
9 %           R R     E       G   G    I       SS    T    R R      Y            %
10 %           R  R    EEEEE    GGG   IIIII  SSSSS    T    R  R     Y            %
11 %                                                                             %
12 %                                                                             %
13 %                       MagickCore Registry 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 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/exception.h"
45 #include "MagickCore/exception-private.h"
46 #include "MagickCore/image.h"
47 #include "MagickCore/list.h"
48 #include "MagickCore/memory_.h"
49 #include "MagickCore/memory-private.h"
50 #include "MagickCore/registry.h"
51 #include "MagickCore/registry-private.h"
52 #include "MagickCore/splay-tree.h"
53 #include "MagickCore/string_.h"
54 #include "MagickCore/utility.h"
55 
56 /*
57   Typedef declarations.
58 */
59 typedef struct _RegistryInfo
60 {
61   RegistryType
62     type;
63 
64   void
65     *value;
66 
67   size_t
68     signature;
69 } RegistryInfo;
70 
71 /*
72   Static declarations.
73 */
74 static SplayTreeInfo
75   *registry = (SplayTreeInfo *) NULL;
76 
77 static SemaphoreInfo
78   *registry_semaphore = (SemaphoreInfo *) NULL;
79 
80 /*
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %                                                                             %
83 %                                                                             %
84 %                                                                             %
85 %   D e f i n e I m a g e R e g i s t r y                                     %
86 %                                                                             %
87 %                                                                             %
88 %                                                                             %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %
91 %  DefineImageRegistry() associates a key/value pair with the image registry.
92 %
93 %  The format of the DefineImageRegistry method is:
94 %
95 %      MagickBooleanType DefineImageRegistry(const RegistryType type,
96 %        const char *option,ExceptionInfo *exception)
97 %
98 %  A description of each parameter follows:
99 %
100 %    o type: the type.
101 %
102 %    o option: the option.
103 %
104 %    o exception: the exception.
105 %
106 */
DefineImageRegistry(const RegistryType type,const char * option,ExceptionInfo * exception)107 MagickExport MagickBooleanType DefineImageRegistry(const RegistryType type,
108   const char *option,ExceptionInfo *exception)
109 {
110   char
111     key[MagickPathExtent],
112     value[MagickPathExtent];
113 
114   register char
115     *p;
116 
117   assert(option != (const char *) NULL);
118   (void) CopyMagickString(key,option,MagickPathExtent);
119   for (p=key; *p != '\0'; p++)
120     if (*p == '=')
121       break;
122   *value='\0';
123   if (*p == '=')
124     (void) CopyMagickString(value,p+1,MagickPathExtent);
125   *p='\0';
126   return(SetImageRegistry(type,key,value,exception));
127 }
128 
129 /*
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 %                                                                             %
132 %                                                                             %
133 %                                                                             %
134 %   D e l e t e I m a g e R e g i s t r y                                     %
135 %                                                                             %
136 %                                                                             %
137 %                                                                             %
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %
140 %  DeleteImageRegistry() deletes a key from the image registry.
141 %
142 %  The format of the DeleteImageRegistry method is:
143 %
144 %      MagickBooleanType DeleteImageRegistry(const char *key)
145 %
146 %  A description of each parameter follows:
147 %
148 %    o key: the registry.
149 %
150 */
DeleteImageRegistry(const char * key)151 MagickExport MagickBooleanType DeleteImageRegistry(const char *key)
152 {
153   if (IsEventLogging() != MagickFalse)
154     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
155   if (registry == (void *) NULL)
156     return(MagickFalse);
157   return(DeleteNodeFromSplayTree(registry,key));
158 }
159 
160 /*
161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 %                                                                             %
163 %                                                                             %
164 %                                                                             %
165 %   G e t I m a g e R e g i s t r y                                           %
166 %                                                                             %
167 %                                                                             %
168 %                                                                             %
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 %
171 %  GetImageRegistry() returns a value associated with an image registry key.
172 %
173 %  The format of the GetImageRegistry method is:
174 %
175 %      void *GetImageRegistry(const RegistryType type,const char *key,
176 %        ExceptionInfo *exception)
177 %
178 %  A description of each parameter follows:
179 %
180 %    o type: the type.
181 %
182 %    o key: the key.
183 %
184 %    o exception: the exception.
185 %
186 */
GetImageRegistry(const RegistryType type,const char * key,ExceptionInfo * exception)187 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
188   ExceptionInfo *exception)
189 {
190   void
191     *value;
192 
193   RegistryInfo
194     *registry_info;
195 
196   if (IsEventLogging() != MagickFalse)
197     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
198   if (registry == (void *) NULL)
199     return((void *) NULL);
200   registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
201   if (registry_info == (void *) NULL)
202     return((void *) NULL);
203   value=(void *) NULL;
204   switch (type)
205   {
206     case ImageRegistryType:
207     {
208       if (type == registry_info->type)
209         value=(void *) CloneImageList((Image *) registry_info->value,exception);
210       break;
211     }
212     case ImageInfoRegistryType:
213     {
214       if (type == registry_info->type)
215         value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
216       break;
217     }
218     case StringRegistryType:
219     {
220       switch (registry_info->type)
221       {
222         case ImageRegistryType:
223         {
224           value=(Image *) ConstantString(((Image *)
225             registry_info->value)->filename);
226           break;
227         }
228         case ImageInfoRegistryType:
229         {
230           value=(Image *) ConstantString(((ImageInfo *)
231             registry_info->value)->filename);
232           break;
233         }
234         case StringRegistryType:
235         {
236           value=(void *) ConstantString((char *) registry_info->value);
237           break;
238         }
239         default:
240           break;
241       }
242       break;
243     }
244     default:
245       break;
246   }
247   return(value);
248 }
249 
250 /*
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 %                                                                             %
253 %                                                                             %
254 %                                                                             %
255 %   G e t N e x t I m a g e R e g i s t r y                                   %
256 %                                                                             %
257 %                                                                             %
258 %                                                                             %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 %
261 %  GetNextImageRegistry() gets the next image registry value.
262 %
263 %  The format of the GetNextImageRegistry method is:
264 %
265 %      char *GetNextImageRegistry(void)
266 %
267 */
GetNextImageRegistry(void)268 MagickExport char *GetNextImageRegistry(void)
269 {
270   if (IsEventLogging() != MagickFalse)
271     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
272   if (registry == (void *) NULL)
273     return((char *) NULL);
274   return((char *) GetNextKeyInSplayTree(registry));
275 }
276 
277 /*
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 %                                                                             %
280 %                                                                             %
281 %                                                                             %
282 +   R e g i s t r y C o m p o n e n t G e n e s i s                           %
283 %                                                                             %
284 %                                                                             %
285 %                                                                             %
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 %
288 %  RegistryComponentGenesis() instantiates the registry component.
289 %
290 %  The format of the RegistryComponentGenesis method is:
291 %
292 %      MagickBooleanType RegistryComponentGenesis(void)
293 %
294 */
RegistryComponentGenesis(void)295 MagickPrivate MagickBooleanType RegistryComponentGenesis(void)
296 {
297   if (registry_semaphore == (SemaphoreInfo *) NULL)
298     registry_semaphore=AcquireSemaphoreInfo();
299   return(MagickTrue);
300 }
301 
302 /*
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 %                                                                             %
305 %                                                                             %
306 %                                                                             %
307 %   R e g i s t r y C o m p o n e n t T e r m i n u s                         %
308 %                                                                             %
309 %                                                                             %
310 %                                                                             %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 %
313 %  RegistryComponentTerminus() destroys the registry component.
314 %
315 %  The format of the DestroyDefines method is:
316 %
317 %      void RegistryComponentTerminus(void)
318 %
319 */
RegistryComponentTerminus(void)320 MagickPrivate void RegistryComponentTerminus(void)
321 {
322   if (registry_semaphore == (SemaphoreInfo *) NULL)
323     ActivateSemaphoreInfo(&registry_semaphore);
324   LockSemaphoreInfo(registry_semaphore);
325   if (IsEventLogging() != MagickFalse)
326     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
327   if (registry != (void *) NULL)
328     registry=DestroySplayTree(registry);
329   UnlockSemaphoreInfo(registry_semaphore);
330   RelinquishSemaphoreInfo(&registry_semaphore);
331 }
332 
333 /*
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 %                                                                             %
336 %                                                                             %
337 %                                                                             %
338 %   R e m o v e I m a g e R e g i s t r y                                     %
339 %                                                                             %
340 %                                                                             %
341 %                                                                             %
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 %
344 %  RemoveImageRegistry() removes a key from the image registry and returns its
345 %  value.
346 %
347 %  The format of the RemoveImageRegistry method is:
348 %
349 %      void *RemoveImageRegistry(const char *key)
350 %
351 %  A description of each parameter follows:
352 %
353 %    o key: the registry.
354 %
355 */
RemoveImageRegistry(const char * key)356 MagickExport void *RemoveImageRegistry(const char *key)
357 {
358   if (IsEventLogging() != MagickFalse)
359     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
360   if (registry == (void *) NULL)
361     return((void *) NULL);
362   return(RemoveNodeFromSplayTree(registry,key));
363 }
364 
365 /*
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 %                                                                             %
368 %                                                                             %
369 %                                                                             %
370 %   R e s e t I m a g e R e g i s t r y I t e r a t o r                       %
371 %                                                                             %
372 %                                                                             %
373 %                                                                             %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 %
376 %  ResetImageRegistryIterator() resets the registry iterator.  Use it in
377 %  conjunction with GetNextImageRegistry() to iterate over all the values
378 %  in the image registry.
379 %
380 %  The format of the ResetImageRegistryIterator method is:
381 %
382 %      ResetImageRegistryIterator(void)
383 %
384 */
ResetImageRegistryIterator(void)385 MagickExport void ResetImageRegistryIterator(void)
386 {
387   if (IsEventLogging() != MagickFalse)
388     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
389   if (registry == (void *) NULL)
390     return;
391   ResetSplayTreeIterator(registry);
392 }
393 
394 /*
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 %                                                                             %
397 %                                                                             %
398 %                                                                             %
399 %   S e t I m a g e R e g i s t r y                                           %
400 %                                                                             %
401 %                                                                             %
402 %                                                                             %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 %
405 %  SetImageRegistry() associates a value with an image registry key.
406 %
407 %  The format of the SetImageRegistry method is:
408 %
409 %      MagickBooleanType SetImageRegistry(const RegistryType type,
410 %        const char *key,const void *value,ExceptionInfo *exception)
411 %
412 %  A description of each parameter follows:
413 %
414 %    o type: the type.
415 %
416 %    o key: the key.
417 %
418 %    o value: the value.
419 %
420 %    o exception: the exception.
421 %
422 */
423 
DestroyRegistryNode(void * registry_info)424 static void *DestroyRegistryNode(void *registry_info)
425 {
426   register RegistryInfo
427     *p;
428 
429   p=(RegistryInfo *) registry_info;
430   switch (p->type)
431   {
432     case StringRegistryType:
433     default:
434     {
435       p->value=RelinquishMagickMemory(p->value);
436       break;
437     }
438     case ImageRegistryType:
439     {
440       p->value=(void *) DestroyImageList((Image *) p->value);
441       break;
442     }
443     case ImageInfoRegistryType:
444     {
445       p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
446       break;
447     }
448   }
449   return(RelinquishMagickMemory(p));
450 }
451 
SetImageRegistry(const RegistryType type,const char * key,const void * value,ExceptionInfo * exception)452 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
453   const char *key,const void *value,ExceptionInfo *exception)
454 {
455   MagickBooleanType
456     status;
457 
458   RegistryInfo
459     *registry_info;
460 
461   void
462     *clone_value;
463 
464   if (IsEventLogging() != MagickFalse)
465     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
466   if (value == (const void *) NULL)
467     return(MagickFalse);
468   clone_value=(void *) NULL;
469   switch (type)
470   {
471     case StringRegistryType:
472     default:
473     {
474       const char
475         *string;
476 
477       string=(const char *) value;
478       clone_value=(void *) ConstantString(string);
479       break;
480     }
481     case ImageRegistryType:
482     {
483       const Image
484         *image;
485 
486       image=(const Image *) value;
487       if (image->signature != MagickCoreSignature)
488         {
489           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
490             "UnableToSetRegistry","%s",key);
491           return(MagickFalse);
492         }
493       clone_value=(void *) CloneImageList(image,exception);
494       break;
495     }
496     case ImageInfoRegistryType:
497     {
498       const ImageInfo
499         *image_info;
500 
501       image_info=(const ImageInfo *) value;
502       if (image_info->signature != MagickCoreSignature)
503         {
504           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
505             "UnableToSetRegistry","%s",key);
506           return(MagickFalse);
507         }
508       clone_value=(void *) CloneImageInfo(image_info);
509       break;
510     }
511   }
512   if (clone_value == (void *) NULL)
513     return(MagickFalse);
514   registry_info=(RegistryInfo *) AcquireCriticalMemory(sizeof(*registry_info));
515   (void) memset(registry_info,0,sizeof(*registry_info));
516   registry_info->type=type;
517   registry_info->value=clone_value;
518   registry_info->signature=MagickCoreSignature;
519   if (registry == (SplayTreeInfo *) NULL)
520     {
521       if (registry_semaphore == (SemaphoreInfo *) NULL)
522         ActivateSemaphoreInfo(&registry_semaphore);
523       LockSemaphoreInfo(registry_semaphore);
524       if (registry == (SplayTreeInfo *) NULL)
525         registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
526           DestroyRegistryNode);
527       UnlockSemaphoreInfo(registry_semaphore);
528     }
529   status=AddValueToSplayTree(registry,ConstantString(key),registry_info);
530   return(status);
531 }
532