2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M AAA GGGG IIIII CCCC %
8 % M M M AAAAA G GGG I C %
10 % M M A A GGGG IIIII CCCC %
13 % MagickCore Image Magic Methods %
20 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/configure-private.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/hashmap.h"
50 #include "MagickCore/magic.h"
51 #include "MagickCore/magic-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/semaphore.h"
54 #include "MagickCore/string_.h"
55 #include "MagickCore/string-private.h"
56 #include "MagickCore/token.h"
57 #include "MagickCore/utility.h"
58 #include "MagickCore/utility-private.h"
59 #include "MagickCore/xml-tree.h"
60 #include "MagickCore/xml-tree-private.h"
65 #define MagicFilename "magic.xml"
66 #define MagicPattern(magic) (const unsigned char *) (magic), sizeof(magic)-1
71 typedef struct _MagicMapInfo
76 const MagickOffsetType
89 static const MagicMapInfo
92 { "8BIMWTEXT", 0, MagicPattern("8\000B\000I\000M\000#") },
93 { "8BIMTEXT", 0, MagicPattern("8BIM#") },
94 { "8BIM", 0, MagicPattern("8BIM") },
95 { "BMP", 0, MagicPattern("BA") },
96 { "BMP", 0, MagicPattern("BM") },
97 { "BMP", 0, MagicPattern("CI") },
98 { "BMP", 0, MagicPattern("CP") },
99 { "BMP", 0, MagicPattern("IC") },
100 { "PICT", 0, MagicPattern("PICT") },
101 { "BMP", 0, MagicPattern("PI") },
102 { "CALS", 21, MagicPattern("version: MIL-STD-1840") },
103 { "CALS", 0, MagicPattern("srcdocid:") },
104 { "CALS", 9, MagicPattern("srcdocid:") },
105 { "CALS", 8, MagicPattern("rorient:") },
106 { "CGM", 0, MagicPattern("BEGMF") },
107 { "CIN", 0, MagicPattern("\200\052\137\327") },
108 { "CRW", 0, MagicPattern("II\x1a\x00\x00\x00HEAPCCDR") },
109 { "DCM", 128, MagicPattern("DICM") },
110 { "DCX", 0, MagicPattern("\261\150\336\72") },
111 { "DIB", 0, MagicPattern("\050\000") },
112 { "DDS", 0, MagicPattern("DDS ") },
113 { "DJVU", 0, MagicPattern("AT&TFORM") },
114 { "DOT", 0, MagicPattern("digraph") },
115 { "DPX", 0, MagicPattern("SDPX") },
116 { "DPX", 0, MagicPattern("XPDS") },
117 { "EMF", 40, MagicPattern("\040\105\115\106\000\000\001\000") },
118 { "EPT", 0, MagicPattern("\305\320\323\306") },
119 { "EXR", 0, MagicPattern("\166\057\061\001") },
120 { "FAX", 0, MagicPattern("DFAX") },
121 { "FIG", 0, MagicPattern("#FIG") },
122 { "FITS", 0, MagicPattern("IT0") },
123 { "FITS", 0, MagicPattern("SIMPLE") },
124 { "GIF", 0, MagicPattern("GIF8") },
125 { "GPLT", 0, MagicPattern("#!/usr/local/bin/gnuplot") },
126 { "HDF", 1, MagicPattern("HDF") },
127 { "HDR", 0, MagicPattern("#?RADIANCE") },
128 { "HDR", 0, MagicPattern("#?RGBE") },
129 { "HPGL", 0, MagicPattern("IN;") },
130 { "HTML", 1, MagicPattern("HTML") },
131 { "HTML", 1, MagicPattern("html") },
132 { "ILBM", 8, MagicPattern("ILBM") },
133 { "IPTCWTEXT", 0, MagicPattern("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
134 { "IPTCTEXT", 0, MagicPattern("2#0=\042�\042") },
135 { "IPTC", 0, MagicPattern("\034\002") },
136 { "JNG", 0, MagicPattern("\213JNG\r\n\032\n") },
137 { "JPEG", 0, MagicPattern("\377\330\377") },
138 { "J2K", 0, MagicPattern("\xff\x4f\xff\x51") },
139 { "JPC", 0, MagicPattern("\x0d\x0a\x87\x0a") },
140 { "JP2", 4, MagicPattern("\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a") },
141 { "MAT", 0, MagicPattern("MATLAB 5.0 MAT-file,") },
142 { "MIFF", 0, MagicPattern("Id=ImageMagick") },
143 { "MIFF", 0, MagicPattern("id=ImageMagick") },
144 { "MNG", 0, MagicPattern("\212MNG\r\n\032\n") },
145 { "MPC", 0, MagicPattern("id=MagickCache") },
146 { "MPEG", 0, MagicPattern("\000\000\001\263") },
147 { "MRW", 0, MagicPattern("\x00MRM") },
148 { "MVG", 0, MagicPattern("push graphic-context") },
149 { "ORF", 0, MagicPattern("IIRO\x08\x00\x00\x00") },
150 { "PCD", 2048, MagicPattern("PCD_") },
151 { "PCL", 0, MagicPattern("\033E\033") },
152 { "PCX", 0, MagicPattern("\012\002") },
153 { "PCX", 0, MagicPattern("\012\005") },
154 { "PDB", 60, MagicPattern("vIMGView") },
155 { "PDF", 0, MagicPattern("%PDF-") },
156 { "PES", 0, MagicPattern("#PES") },
157 { "PFA", 0, MagicPattern("%!PS-AdobeFont-1.0") },
158 { "PFB", 6, MagicPattern("%!PS-AdobeFont-1.0") },
159 { "PGX", 0, MagicPattern("\050\107\020\115\046") },
160 { "PICT", 522, MagicPattern("\000\021\002\377\014\000") },
161 { "PNG", 0, MagicPattern("\211PNG\r\n\032\n") },
162 { "PBM", 0, MagicPattern("P1") },
163 { "PGM", 0, MagicPattern("P2") },
164 { "PPM", 0, MagicPattern("P3") },
165 { "PBM", 0, MagicPattern("P4") },
166 { "PGM", 0, MagicPattern("P5") },
167 { "PPM", 0, MagicPattern("P6") },
168 { "PAM", 0, MagicPattern("P7") },
169 { "PFM", 0, MagicPattern("PF") },
170 { "PFM", 0, MagicPattern("Pf") },
171 { "PS", 0, MagicPattern("%!") },
172 { "PS", 0, MagicPattern("\004%!") },
173 { "PS", 0, MagicPattern("\305\320\323\306") },
174 { "PSB", 0, MagicPattern("8BPB") },
175 { "PSD", 0, MagicPattern("8BPS") },
176 { "PWP", 0, MagicPattern("SFW95") },
177 { "RAF", 0, MagicPattern("FUJIFILMCCD-RAW ") },
178 { "RLE", 0, MagicPattern("\122\314") },
179 { "SCT", 0, MagicPattern("CT") },
180 { "SFW", 0, MagicPattern("SFW94") },
181 { "SGI", 0, MagicPattern("\001\332") },
182 { "SUN", 0, MagicPattern("\131\246\152\225") },
183 { "SVG", 1, MagicPattern("?XML") },
184 { "SVG", 1, MagicPattern("?xml") },
185 { "TIFF", 0, MagicPattern("\115\115\000\052") },
186 { "TIFF", 0, MagicPattern("\111\111\052\000") },
187 { "TIFF64", 0, MagicPattern("\115\115\000\053\000\010\000\000") },
188 { "TIFF64", 0, MagicPattern("\111\111\053\000\010\000\000\000") },
189 { "TTF", 0, MagicPattern("\000\001\000\000\000") },
190 { "TXT", 0, MagicPattern("# ImageMagick pixel enumeration:") },
191 { "VICAR", 0, MagicPattern("LBLSIZE") },
192 { "VICAR", 0, MagicPattern("NJPL1I") },
193 { "VIFF", 0, MagicPattern("\253\001") },
194 { "WEBP", 8, MagicPattern("WEBP") },
195 { "WMF", 0, MagicPattern("\327\315\306\232") },
196 { "WMF", 0, MagicPattern("\001\000\011\000") },
197 { "WPG", 0, MagicPattern("\377WPC") },
198 { "XBM", 0, MagicPattern("#define") },
199 { "XCF", 0, MagicPattern("gimp xcf") },
200 { "XEF", 0, MagicPattern("FOVb") },
201 { "XPM", 1, MagicPattern("* XPM *") },
202 { "XWD", 4, MagicPattern("\007\000\000") },
203 { "XWD", 5, MagicPattern("\000\000\007") }
206 static LinkedListInfo
207 *magic_cache = (LinkedListInfo *) NULL;
210 *magic_semaphore = (SemaphoreInfo *) NULL;
213 Forward declarations.
215 static MagickBooleanType
216 IsMagicCacheInstantiated(ExceptionInfo *),
217 LoadMagicCache(LinkedListInfo *,const char *,const char *,const size_t,
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % A c q u i r e M a g i c L i s t s %
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 % AcquireMagicCache() caches one or more magic configurations which provides a
232 % mapping between magic attributes and a magic name.
234 % The format of the AcquireMagicCache method is:
236 % LinkedListInfo *AcquireMagicCache(const char *filename,
237 % ExceptionInfo *exception)
239 % A description of each parameter follows:
241 % o filename: the font file name.
243 % o exception: return any errors or warnings in this structure.
246 static int CompareMagickInfoSize(const void *a,const void *b)
255 if (ma->offset != mb->offset)
256 return((int) (ma->offset-mb->offset));
258 return((int) (mb->length-ma->length));
261 static LinkedListInfo *AcquireMagicCache(const char *filename,
262 ExceptionInfo *exception)
265 path[MagickPathExtent];
281 Load external magic map.
283 magic_cache=NewLinkedList(0);
284 if (magic_cache == (LinkedListInfo *) NULL)
285 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
288 options=GetConfigureOptions(filename,exception);
289 option=(const StringInfo *) GetNextValueInLinkedList(options);
290 while (option != (const StringInfo *) NULL)
292 (void) CopyMagickString(path,GetStringInfoPath(option),MagickPathExtent);
293 status&=LoadMagicCache(magic_cache,(const char *)
294 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
295 option=(const StringInfo *) GetNextValueInLinkedList(options);
297 options=DestroyConfigureOptions(options);
299 Load built-in magic map.
301 for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
306 register const MagicMapInfo
310 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
311 if (magic_info == (MagicInfo *) NULL)
313 (void) ThrowMagickException(exception,GetMagickModule(),
314 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
317 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
318 magic_info->path=(char *) "[built-in]";
319 magic_info->name=(char *) p->name;
320 magic_info->offset=p->offset;
321 magic_info->target=(char *) p->magic;
322 magic_info->magic=(unsigned char *) p->magic;
323 magic_info->length=p->length;
324 magic_info->exempt=MagickTrue;
325 magic_info->signature=MagickCoreSignature;
326 status&=InsertValueInSortedLinkedList(magic_cache,CompareMagickInfoSize,
328 if (status == MagickFalse)
329 (void) ThrowMagickException(exception,GetMagickModule(),
330 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % G e t M a g i c I n f o %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % GetMagicInfo() searches the magic list for the specified name and if found
347 % returns attributes for that magic.
349 % The format of the GetMagicInfo method is:
351 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
352 % const size_t length,ExceptionInfo *exception)
354 % A description of each parameter follows:
356 % o magic: A binary string generally representing the first few characters
357 % of the image file or blob.
359 % o length: the length of the binary signature.
361 % o exception: return any errors or warnings in this structure.
364 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
365 const size_t length,ExceptionInfo *exception)
367 register const MagicInfo
370 assert(exception != (ExceptionInfo *) NULL);
371 if (IsMagicCacheInstantiated(exception) == MagickFalse)
372 return((const MagicInfo *) NULL);
374 Search for magic tag.
376 LockSemaphoreInfo(magic_semaphore);
377 ResetLinkedListIterator(magic_cache);
378 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
379 if (magic == (const unsigned char *) NULL)
381 UnlockSemaphoreInfo(magic_semaphore);
384 while (p != (const MagicInfo *) NULL)
386 assert(p->offset >= 0);
387 if (((size_t) (p->offset+p->length) <= length) &&
388 (memcmp(magic+p->offset,p->magic,p->length) == 0))
390 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
392 if (p != (const MagicInfo *) NULL)
393 (void) InsertValueInLinkedList(magic_cache,0,
394 RemoveElementByValueFromLinkedList(magic_cache,p));
395 UnlockSemaphoreInfo(magic_semaphore);
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % G e t M a g i c P a t t e r n E x t e n t %
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 % GetMagicPatternExtent() returns the the extent of the buffer that is
411 % required to check all the MagickInfos. It returns zero if the list is empty.
413 % The format of the GetMagicPatternExtent method is:
415 % size_t GetMagicPatternExtent(ExceptionInfo *exception)
417 % A description of each parameter follows:
419 % o exception: return any errors or warnings in this structure.
422 MagickExport size_t GetMagicPatternExtent(ExceptionInfo *exception)
424 register const MagicInfo
434 assert(exception != (ExceptionInfo *) NULL);
435 if ((size != 0) || (IsMagicCacheInstantiated(exception) == MagickFalse))
437 LockSemaphoreInfo(magic_semaphore);
438 ResetLinkedListIterator(magic_cache);
440 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
441 while (p != (const MagicInfo *) NULL)
443 magickSize=(size_t) (p->offset+p->length);
444 if (magickSize > max)
446 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
449 UnlockSemaphoreInfo(magic_semaphore);
454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % G e t M a g i c I n f o L i s t %
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 % GetMagicInfoList() returns any image aliases that match the specified
467 % The magic of the GetMagicInfoList function is:
469 % const MagicInfo **GetMagicInfoList(const char *pattern,
470 % size_t *number_aliases,ExceptionInfo *exception)
472 % A description of each parameter follows:
474 % o pattern: Specifies a pointer to a text string containing a pattern.
476 % o number_aliases: This integer returns the number of aliases in the list.
478 % o exception: return any errors or warnings in this structure.
482 #if defined(__cplusplus) || defined(c_plusplus)
486 static int MagicInfoCompare(const void *x,const void *y)
492 p=(const MagicInfo **) x,
493 q=(const MagicInfo **) y;
494 if (LocaleCompare((*p)->path,(*q)->path) == 0)
495 return(LocaleCompare((*p)->name,(*q)->name));
496 return(LocaleCompare((*p)->path,(*q)->path));
499 #if defined(__cplusplus) || defined(c_plusplus)
503 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
504 size_t *number_aliases,ExceptionInfo *exception)
509 register const MagicInfo
518 assert(pattern != (char *) NULL);
519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
520 assert(number_aliases != (size_t *) NULL);
522 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
523 if (p == (const MagicInfo *) NULL)
524 return((const MagicInfo **) NULL);
525 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
526 GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
527 if (aliases == (const MagicInfo **) NULL)
528 return((const MagicInfo **) NULL);
532 LockSemaphoreInfo(magic_semaphore);
533 ResetLinkedListIterator(magic_cache);
534 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
535 for (i=0; p != (const MagicInfo *) NULL; )
537 if ((p->stealth == MagickFalse) &&
538 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
540 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
542 UnlockSemaphoreInfo(magic_semaphore);
543 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
544 aliases[i]=(MagicInfo *) NULL;
545 *number_aliases=(size_t) i;
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % G e t M a g i c L i s t %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % GetMagicList() returns any image format aliases that match the specified
563 % The format of the GetMagicList function is:
565 % char **GetMagicList(const char *pattern,size_t *number_aliases,
566 % ExceptionInfo *exception)
568 % A description of each parameter follows:
570 % o pattern: Specifies a pointer to a text string containing a pattern.
572 % o number_aliases: This integer returns the number of image format aliases
575 % o exception: return any errors or warnings in this structure.
579 #if defined(__cplusplus) || defined(c_plusplus)
583 static int MagicCompare(const void *x,const void *y)
591 return(LocaleCompare(p,q));
594 #if defined(__cplusplus) || defined(c_plusplus)
598 MagickExport char **GetMagicList(const char *pattern,size_t *number_aliases,
599 ExceptionInfo *exception)
604 register const MagicInfo
611 Allocate configure list.
613 assert(pattern != (char *) NULL);
614 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
615 assert(number_aliases != (size_t *) NULL);
617 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
618 if (p == (const MagicInfo *) NULL)
619 return((char **) NULL);
620 aliases=(char **) AcquireQuantumMemory((size_t)
621 GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
622 if (aliases == (char **) NULL)
623 return((char **) NULL);
624 LockSemaphoreInfo(magic_semaphore);
625 ResetLinkedListIterator(magic_cache);
626 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
627 for (i=0; p != (const MagicInfo *) NULL; )
629 if ((p->stealth == MagickFalse) &&
630 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
631 aliases[i++]=ConstantString(p->name);
632 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
634 UnlockSemaphoreInfo(magic_semaphore);
635 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
636 aliases[i]=(char *) NULL;
637 *number_aliases=(size_t) i;
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % G e t M a g i c N a m e %
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % GetMagicName() returns the name associated with the magic.
654 % The format of the GetMagicName method is:
656 % const char *GetMagicName(const MagicInfo *magic_info)
658 % A description of each parameter follows:
660 % o magic_info: The magic info.
663 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
666 assert(magic_info != (MagicInfo *) NULL);
667 assert(magic_info->signature == MagickCoreSignature);
668 return(magic_info->name);
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 + I s M a g i c C a c h e I n s t a n t i a t e d %
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % IsMagicCacheInstantiated() determines if the magic list is instantiated.
683 % If not, it instantiates the list and returns it.
685 % The format of the IsMagicInstantiated method is:
687 % MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
689 % A description of each parameter follows.
691 % o exception: return any errors or warnings in this structure.
694 static MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
696 if (magic_cache == (LinkedListInfo *) NULL)
698 if (magic_semaphore == (SemaphoreInfo *) NULL)
699 ActivateSemaphoreInfo(&magic_semaphore);
700 LockSemaphoreInfo(magic_semaphore);
701 if (magic_cache == (LinkedListInfo *) NULL)
702 magic_cache=AcquireMagicCache(MagicFilename,exception);
703 UnlockSemaphoreInfo(magic_semaphore);
705 return(magic_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % L i s t M a g i c I n f o %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % ListMagicInfo() lists the magic info to a file.
721 % The format of the ListMagicInfo method is:
723 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
725 % A description of each parameter follows.
727 % o file: An pointer to a FILE.
729 % o exception: return any errors or warnings in this structure.
732 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
733 ExceptionInfo *exception)
750 if (file == (const FILE *) NULL)
752 magic_info=GetMagicInfoList("*",&number_aliases,exception);
753 if (magic_info == (const MagicInfo **) NULL)
756 path=(const char *) NULL;
757 for (i=0; i < (ssize_t) number_aliases; i++)
759 if (magic_info[i]->stealth != MagickFalse)
761 if ((path == (const char *) NULL) ||
762 (LocaleCompare(path,magic_info[i]->path) != 0))
764 if (magic_info[i]->path != (char *) NULL)
765 (void) FormatLocaleFile(file,"\nPath: %s\n\n",magic_info[i]->path);
766 (void) FormatLocaleFile(file,"Name Offset Target\n");
767 (void) FormatLocaleFile(file,
768 "-------------------------------------------------"
769 "------------------------------\n");
771 path=magic_info[i]->path;
772 (void) FormatLocaleFile(file,"%s",magic_info[i]->name);
773 for (j=(ssize_t) strlen(magic_info[i]->name); j <= 9; j++)
774 (void) FormatLocaleFile(file," ");
775 (void) FormatLocaleFile(file,"%6ld ",(long) magic_info[i]->offset);
776 if (magic_info[i]->target != (char *) NULL)
781 for (j=0; magic_info[i]->target[j] != '\0'; j++)
782 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
783 (void) FormatLocaleFile(file,"%c",magic_info[i]->target[j]);
785 (void) FormatLocaleFile(file,"\\%03o",(unsigned int)
786 ((unsigned char) magic_info[i]->target[j]));
788 (void) FormatLocaleFile(file,"\n");
791 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 + L o a d M a g i c L i s t %
804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % LoadMagicCache() loads the magic configurations which provides a mapping
807 % between magic attributes and a magic name.
809 % The format of the LoadMagicCache method is:
811 % MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
812 % const char *xml,const char *filename,const size_t depth,
813 % ExceptionInfo *exception)
815 % A description of each parameter follows:
817 % o xml: The magic list in XML format.
819 % o filename: The magic list filename.
821 % o depth: depth of <include /> statements.
823 % o exception: return any errors or warnings in this structure.
826 static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
827 const char *xml,const char *filename,const size_t depth,
828 ExceptionInfo *exception)
831 keyword[MagickPathExtent],
844 Load the magic map file.
846 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
847 "Loading magic configure file \"%s\" ...",filename);
848 if (xml == (char *) NULL)
851 magic_info=(MagicInfo *) NULL;
852 token=AcquireString(xml);
853 for (q=(char *) xml; *q != '\0'; )
858 GetMagickToken(q,&q,token);
861 (void) CopyMagickString(keyword,token,MagickPathExtent);
862 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
867 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
868 GetMagickToken(q,&q,token);
871 if (LocaleNCompare(keyword,"<!--",4) == 0)
876 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
877 GetMagickToken(q,&q,token);
880 if (LocaleCompare(keyword,"<include") == 0)
885 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
887 (void) CopyMagickString(keyword,token,MagickPathExtent);
888 GetMagickToken(q,&q,token);
891 GetMagickToken(q,&q,token);
892 if (LocaleCompare(keyword,"file") == 0)
895 (void) ThrowMagickException(exception,GetMagickModule(),
896 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
900 path[MagickPathExtent],
903 GetPathComponent(filename,HeadPath,path);
905 (void) ConcatenateMagickString(path,DirectorySeparator,
907 if (*token == *DirectorySeparator)
908 (void) CopyMagickString(path,token,MagickPathExtent);
910 (void) ConcatenateMagickString(path,token,MagickPathExtent);
911 xml=FileToXML(path,~0UL);
912 if (xml != (char *) NULL)
914 status&=LoadMagicCache(magic_cache,xml,path,depth+1,
916 xml=(char *) RelinquishMagickMemory(xml);
923 if (LocaleCompare(keyword,"<magic") == 0)
928 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
929 if (magic_info == (MagicInfo *) NULL)
930 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
931 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
932 magic_info->path=ConstantString(filename);
933 magic_info->exempt=MagickFalse;
934 magic_info->signature=MagickCoreSignature;
937 if (magic_info == (MagicInfo *) NULL)
939 if (LocaleCompare(keyword,"/>") == 0)
941 status=InsertValueInSortedLinkedList(magic_cache,CompareMagickInfoSize,
943 if (status == MagickFalse)
944 (void) ThrowMagickException(exception,GetMagickModule(),
945 ResourceLimitError,"MemoryAllocationFailed","`%s'",
947 magic_info=(MagicInfo *) NULL;
950 GetMagickToken(q,(const char **) NULL,token);
953 GetMagickToken(q,&q,token);
954 GetMagickToken(q,&q,token);
960 if (LocaleCompare((char *) keyword,"name") == 0)
962 magic_info->name=ConstantString(token);
970 if (LocaleCompare((char *) keyword,"offset") == 0)
972 magic_info->offset=(MagickOffsetType) StringToLong(token);
980 if (LocaleCompare((char *) keyword,"stealth") == 0)
982 magic_info->stealth=IsStringTrue(token);
990 if (LocaleCompare((char *) keyword,"target") == 0)
995 register unsigned char
1001 length=strlen(token);
1002 magic_info->target=ConstantString(token);
1003 magic_info->magic=(unsigned char *) ConstantString(token);
1004 q=magic_info->magic;
1005 for (p=magic_info->target; *p != '\0'; )
1010 if (isdigit((int) ((unsigned char) *p)) != 0)
1015 *q++=(unsigned char) strtol(p,&end,8);
1017 magic_info->length++;
1022 case 'b': *q='\b'; break;
1023 case 'f': *q='\f'; break;
1024 case 'n': *q='\n'; break;
1025 case 'r': *q='\r'; break;
1026 case 't': *q='\t'; break;
1027 case 'v': *q='\v'; break;
1028 case 'a': *q='a'; break;
1029 case '?': *q='\?'; break;
1030 default: *q=(unsigned char) (*p); break;
1034 magic_info->length++;
1038 if (LocaleNCompare(p,"&",5) == 0)
1039 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
1040 *q++=(unsigned char) (*p++);
1041 magic_info->length++;
1051 token=(char *) RelinquishMagickMemory(token);
1052 return(status != 0 ? MagickTrue : MagickFalse);
1056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 + M a g i c C o m p o n e n t G e n e s i s %
1064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % MagicComponentGenesis() instantiates the magic component.
1068 % The format of the MagicComponentGenesis method is:
1070 % MagickBooleanType MagicComponentGenesis(void)
1073 MagickPrivate MagickBooleanType MagicComponentGenesis(void)
1075 if (magic_semaphore == (SemaphoreInfo *) NULL)
1076 magic_semaphore=AcquireSemaphoreInfo();
1081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085 + M a g i c C o m p o n e n t T e r m i n u s %
1089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091 % MagicComponentTerminus() destroys the magic component.
1093 % The format of the MagicComponentTerminus method is:
1095 % MagicComponentTerminus(void)
1099 static void *DestroyMagicElement(void *magic_info)
1104 p=(MagicInfo *) magic_info;
1105 if (p->exempt == MagickFalse)
1107 if (p->path != (char *) NULL)
1108 p->path=DestroyString(p->path);
1109 if (p->name != (char *) NULL)
1110 p->name=DestroyString(p->name);
1111 if (p->target != (char *) NULL)
1112 p->target=DestroyString(p->target);
1113 if (p->magic != (unsigned char *) NULL)
1114 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1116 p=(MagicInfo *) RelinquishMagickMemory(p);
1117 return((void *) NULL);
1120 MagickPrivate void MagicComponentTerminus(void)
1122 if (magic_semaphore == (SemaphoreInfo *) NULL)
1123 ActivateSemaphoreInfo(&magic_semaphore);
1124 LockSemaphoreInfo(magic_semaphore);
1125 if (magic_cache != (LinkedListInfo *) NULL)
1126 magic_cache=DestroyLinkedList(magic_cache,DestroyMagicElement);
1127 UnlockSemaphoreInfo(magic_semaphore);
1128 RelinquishSemaphoreInfo(&magic_semaphore);