]> granicus.if.org Git - imagemagick/blob - MagickCore/coder.c
d09ce863238d45f8aac4e1226f306415607fdd27
[imagemagick] / MagickCore / coder.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                   CCCC   OOO   DDDD    EEEEE  RRRR                          %
7 %                  C      O   O  D   D   E      R   R                         %
8 %                  C      O   O  D   D   EEE    RRRR                          %
9 %                  C      O   O  D   D   E      R R                           %
10 %                   CCCC   OOO   DDDD    EEEEE  R  R                          %
11 %                                                                             %
12 %                                                                             %
13 %                     MagickCore Image Coder Methods                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 May 2001                                    %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2018 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 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/coder-private.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/memory-private.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/splay-tree.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/utility.h"
60 #include "MagickCore/utility-private.h"
61 #include "coders/coders.h"
62 \f
63 /*
64   Define declarations.
65 */
66 #define AddMagickCoder(coder) Magick ## coder ## Aliases
67 \f
68 /*
69   Typedef declarations.
70 */
71 typedef struct _CoderMapInfo
72 {
73   const char
74     *magick,
75     *name;
76 } CoderMapInfo;
77 \f
78 /*
79   Static declarations.
80 */
81 static const CoderMapInfo
82   CoderMap[] =
83   {
84     #include "coders/coders-list.h"
85   };
86
87 static SemaphoreInfo
88   *coder_semaphore = (SemaphoreInfo *) NULL;
89
90 static SplayTreeInfo
91   *coder_cache = (SplayTreeInfo *) NULL;
92 \f
93 /*
94   Forward declarations.
95 */
96 static MagickBooleanType
97   IsCoderTreeInstantiated(ExceptionInfo *);
98 \f
99 /*
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 +  A c q u i r e C o d e r C a c h e                                          %
105 %                                                                             %
106 %                                                                             %
107 %                                                                             %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %
110 %  AcquireCoderCache() caches one or more coder configurations which provides a
111 %  mapping between coder attributes and a coder name.
112 %
113 %  The format of the AcquireCoderCache coder is:
114 %
115 %      SplayTreeInfo *AcquireCoderCache(const char *filename,
116 %        ExceptionInfo *exception)
117 %
118 %  A description of each parameter follows:
119 %
120 %    o filename: the font file name.
121 %
122 %    o exception: return any errors or warnings in this structure.
123 %
124 */
125
126 static void *DestroyCoderNode(void *coder_info)
127 {
128   register CoderInfo
129     *p;
130
131   p=(CoderInfo *) coder_info;
132   if (p->exempt == MagickFalse)
133     {
134       if (p->path != (char *) NULL)
135         p->path=DestroyString(p->path);
136       if (p->name != (char *) NULL)
137         p->name=DestroyString(p->name);
138       if (p->magick != (char *) NULL)
139         p->magick=DestroyString(p->magick);
140     }
141   return(RelinquishMagickMemory(p));
142 }
143
144 static SplayTreeInfo *AcquireCoderCache(ExceptionInfo *exception)
145 {
146   MagickStatusType
147     status;
148
149   register ssize_t
150     i;
151
152   SplayTreeInfo
153     *cache;
154
155   /*
156     Load built-in coder map.
157   */
158   cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
159     DestroyCoderNode);
160   status=MagickTrue;
161   for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
162   {
163     CoderInfo
164       *coder_info;
165
166     register const CoderMapInfo
167       *p;
168
169     p=CoderMap+i;
170     coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
171     if (coder_info == (CoderInfo *) NULL)
172       {
173         (void) ThrowMagickException(exception,GetMagickModule(),
174           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
175         continue;
176       }
177     (void) memset(coder_info,0,sizeof(*coder_info));
178     coder_info->path=(char *) "[built-in]";
179     coder_info->magick=(char *) p->magick;
180     coder_info->name=(char *) p->name;
181     coder_info->exempt=MagickTrue;
182     coder_info->signature=MagickCoreSignature;
183     status&=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
184       coder_info);
185     if (status == MagickFalse)
186       (void) ThrowMagickException(exception,GetMagickModule(),
187         ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
188   }
189   return(cache);
190 }
191 \f
192 /*
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %                                                                             %
195 %                                                                             %
196 %                                                                             %
197 +   C o d e r C o m p o n e n t G e n e s i s                                 %
198 %                                                                             %
199 %                                                                             %
200 %                                                                             %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %
203 %  CoderComponentGenesis() instantiates the coder component.
204 %
205 %  The format of the CoderComponentGenesis method is:
206 %
207 %      MagickBooleanType CoderComponentGenesis(void)
208 %
209 */
210 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
211 {
212   if (coder_semaphore == (SemaphoreInfo *) NULL)
213     coder_semaphore=AcquireSemaphoreInfo();
214   return(MagickTrue);
215 }
216 \f
217 /*
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 +   C o d e r C o m p o n e n t T e r m i n u s                               %
223 %                                                                             %
224 %                                                                             %
225 %                                                                             %
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 %
228 %  CoderComponentTerminus() destroys the coder component.
229 %
230 %  The format of the CoderComponentTerminus method is:
231 %
232 %      CoderComponentTerminus(void)
233 %
234 */
235 MagickPrivate void CoderComponentTerminus(void)
236 {
237   if (coder_semaphore == (SemaphoreInfo *) NULL)
238     ActivateSemaphoreInfo(&coder_semaphore);
239   LockSemaphoreInfo(coder_semaphore);
240   if (coder_cache != (SplayTreeInfo *) NULL)
241     coder_cache=DestroySplayTree(coder_cache);
242   UnlockSemaphoreInfo(coder_semaphore);
243   RelinquishSemaphoreInfo(&coder_semaphore);
244 }
245 \f
246 /*
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 %                                                                             %
249 %                                                                             %
250 %                                                                             %
251 +   G e t C o d e r I n f o                                                   %
252 %                                                                             %
253 %                                                                             %
254 %                                                                             %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %
257 %  GetCoderInfo searches the coder list for the specified name and if found
258 %  returns attributes for that coder.
259 %
260 %  The format of the GetCoderInfo method is:
261 %
262 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
263 %
264 %  A description of each parameter follows:
265 %
266 %    o name: the coder name.
267 %
268 %    o exception: return any errors or warnings in this structure.
269 %
270 */
271 MagickExport const CoderInfo *GetCoderInfo(const char *name,
272   ExceptionInfo *exception)
273 {
274   assert(exception != (ExceptionInfo *) NULL);
275   if (IsCoderTreeInstantiated(exception) == MagickFalse)
276     return((const CoderInfo *) NULL);
277   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
278     return((const CoderInfo *) GetRootValueFromSplayTree(coder_cache));
279   return((const CoderInfo *) GetValueFromSplayTree(coder_cache,name));
280 }
281 \f
282 /*
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 %                                                                             %
285 %                                                                             %
286 %                                                                             %
287 %   G e t C o d e r I n f o L i s t                                           %
288 %                                                                             %
289 %                                                                             %
290 %                                                                             %
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 %
293 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
294 %  The format of the GetCoderInfoList function is:
295 %
296 %      const CoderInfo **GetCoderInfoList(const char *pattern,
297 %        size_t *number_coders,ExceptionInfo *exception)
298 %
299 %  A description of each parameter follows:
300 %
301 %    o pattern: Specifies a pointer to a text string containing a pattern.
302 %
303 %    o number_coders:  This integer returns the number of coders in the list.
304 %
305 %    o exception: return any errors or warnings in this structure.
306 %
307 */
308
309 static int CoderInfoCompare(const void *x,const void *y)
310 {
311   const CoderInfo
312     **p,
313     **q;
314
315   p=(const CoderInfo **) x,
316   q=(const CoderInfo **) y;
317   if (LocaleCompare((*p)->path,(*q)->path) == 0)
318     return(LocaleCompare((*p)->name,(*q)->name));
319   return(LocaleCompare((*p)->path,(*q)->path));
320 }
321
322 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
323   size_t *number_coders,ExceptionInfo *exception)
324 {
325   const CoderInfo
326     **coder_map;
327
328   register const CoderInfo
329     *p;
330
331   register ssize_t
332     i;
333
334   /*
335     Allocate coder list.
336   */
337   assert(pattern != (char *) NULL);
338   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
339   assert(number_coders != (size_t *) NULL);
340   *number_coders=0;
341   p=GetCoderInfo("*",exception);
342   if (p == (const CoderInfo *) NULL)
343     return((const CoderInfo **) NULL);
344   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
345     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
346   if (coder_map == (const CoderInfo **) NULL)
347     return((const CoderInfo **) NULL);
348   /*
349     Generate coder list.
350   */
351   LockSemaphoreInfo(coder_semaphore);
352   ResetSplayTreeIterator(coder_cache);
353   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
354   for (i=0; p != (const CoderInfo *) NULL; )
355   {
356     if ((p->stealth == MagickFalse) &&
357         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
358       coder_map[i++]=p;
359     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
360   }
361   UnlockSemaphoreInfo(coder_semaphore);
362   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
363   coder_map[i]=(CoderInfo *) NULL;
364   *number_coders=(size_t) i;
365   return(coder_map);
366 }
367 \f
368 /*
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 %                                                                             %
371 %                                                                             %
372 %                                                                             %
373 %   G e t C o d e r L i s t                                                   %
374 %                                                                             %
375 %                                                                             %
376 %                                                                             %
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 %
379 %  GetCoderList() returns any coder_map that match the specified pattern.
380 %
381 %  The format of the GetCoderList function is:
382 %
383 %      char **GetCoderList(const char *pattern,size_t *number_coders,
384 %        ExceptionInfo *exception)
385 %
386 %  A description of each parameter follows:
387 %
388 %    o pattern: Specifies a pointer to a text string containing a pattern.
389 %
390 %    o number_coders:  This integer returns the number of coders in the list.
391 %
392 %    o exception: return any errors or warnings in this structure.
393 %
394 */
395
396 static int CoderCompare(const void *x,const void *y)
397 {
398   register const char
399     **p,
400     **q;
401
402   p=(const char **) x;
403   q=(const char **) y;
404   return(LocaleCompare(*p,*q));
405 }
406
407 MagickExport char **GetCoderList(const char *pattern,
408   size_t *number_coders,ExceptionInfo *exception)
409 {
410   char
411     **coder_map;
412
413   register const CoderInfo
414     *p;
415
416   register ssize_t
417     i;
418
419   /*
420     Allocate coder list.
421   */
422   assert(pattern != (char *) NULL);
423   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
424   assert(number_coders != (size_t *) NULL);
425   *number_coders=0;
426   p=GetCoderInfo("*",exception);
427   if (p == (const CoderInfo *) NULL)
428     return((char **) NULL);
429   coder_map=(char **) AcquireQuantumMemory((size_t)
430     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
431   if (coder_map == (char **) NULL)
432     return((char **) NULL);
433   /*
434     Generate coder list.
435   */
436   LockSemaphoreInfo(coder_semaphore);
437   ResetSplayTreeIterator(coder_cache);
438   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
439   for (i=0; p != (const CoderInfo *) NULL; )
440   {
441     if ((p->stealth == MagickFalse) &&
442         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
443       coder_map[i++]=ConstantString(p->name);
444     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
445   }
446   UnlockSemaphoreInfo(coder_semaphore);
447   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
448   coder_map[i]=(char *) NULL;
449   *number_coders=(size_t) i;
450   return(coder_map);
451 }
452 \f
453 /*
454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455 %                                                                             %
456 %                                                                             %
457 %                                                                             %
458 +   I s C o d e r T r e e I n s t a n t i a t e d                             %
459 %                                                                             %
460 %                                                                             %
461 %                                                                             %
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 %
464 %  IsCoderTreeInstantiated() determines if the coder tree is instantiated.  If
465 %  not, it instantiates the tree and returns it.
466 %
467 %  The format of the IsCoderInstantiated method is:
468 %
469 %      MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
470 %
471 %  A description of each parameter follows.
472 %
473 %    o exception: return any errors or warnings in this structure.
474 %
475 */
476 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
477 {
478   if (coder_cache == (SplayTreeInfo *) NULL)
479     {
480       if (coder_semaphore == (SemaphoreInfo *) NULL)
481         ActivateSemaphoreInfo(&coder_semaphore);
482       LockSemaphoreInfo(coder_semaphore);
483       if (coder_cache == (SplayTreeInfo *) NULL)
484         coder_cache=AcquireCoderCache(exception);
485       UnlockSemaphoreInfo(coder_semaphore);
486     }
487   return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
488 }
489 \f
490 /*
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 %                                                                             %
493 %                                                                             %
494 %                                                                             %
495 %  L i s t C o d e r I n f o                                                  %
496 %                                                                             %
497 %                                                                             %
498 %                                                                             %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 %
501 %  ListCoderInfo() lists the coder info to a file.
502 %
503 %  The format of the ListCoderInfo coder is:
504 %
505 %      MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
506 %
507 %  A description of each parameter follows.
508 %
509 %    o file:  An pointer to a FILE.
510 %
511 %    o exception: return any errors or warnings in this structure.
512 %
513 */
514 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
515   ExceptionInfo *exception)
516 {
517   const char
518     *path;
519
520   const CoderInfo
521     **coder_info;
522
523   register ssize_t
524     i;
525
526   size_t
527     number_coders;
528
529   ssize_t
530     j;
531
532   if (file == (const FILE *) NULL)
533     file=stdout;
534   coder_info=GetCoderInfoList("*",&number_coders,exception);
535   if (coder_info == (const CoderInfo **) NULL)
536     return(MagickFalse);
537   path=(const char *) NULL;
538   for (i=0; i < (ssize_t) number_coders; i++)
539   {
540     if (coder_info[i]->stealth != MagickFalse)
541       continue;
542     if ((path == (const char *) NULL) ||
543         (LocaleCompare(path,coder_info[i]->path) != 0))
544       {
545         if (coder_info[i]->path != (char *) NULL)
546           (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
547         (void) FormatLocaleFile(file,"Magick      Coder\n");
548         (void) FormatLocaleFile(file,
549           "-------------------------------------------------"
550           "------------------------------\n");
551       }
552     path=coder_info[i]->path;
553     (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
554     for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
555       (void) FormatLocaleFile(file," ");
556     if (coder_info[i]->name != (char *) NULL)
557       (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
558     (void) FormatLocaleFile(file,"\n");
559   }
560   coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
561   (void) fflush(file);
562   return(MagickTrue);
563 }