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-2014 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 MagickString(magic) (const unsigned char *) (magic), sizeof(magic)-1
71 typedef struct _MagicMapInfo
76 const MagickOffsetType
89 static const MagicMapInfo
92 { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
93 { "8BIMTEXT", 0, MagickString("8BIM#") },
94 { "8BIM", 0, MagickString("8BIM") },
95 { "BMP", 0, MagickString("BA") },
96 { "BMP", 0, MagickString("BM") },
97 { "BMP", 0, MagickString("CI") },
98 { "BMP", 0, MagickString("CP") },
99 { "BMP", 0, MagickString("IC") },
100 { "PICT", 0, MagickString("PICT") },
101 { "BMP", 0, MagickString("PI") },
102 { "CALS", 21, MagickString("version: MIL-STD-1840") },
103 { "CALS", 0, MagickString("srcdocid:") },
104 { "CALS", 9, MagickString("srcdocid:") },
105 { "CALS", 8, MagickString("rorient:") },
106 { "CGM", 0, MagickString("BEGMF") },
107 { "CIN", 0, MagickString("\200\052\137\327") },
108 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
109 { "DCM", 128, MagickString("DICM") },
110 { "DCX", 0, MagickString("\261\150\336\72") },
111 { "DIB", 0, MagickString("\050\000") },
112 { "DDS", 0, MagickString("DDS ") },
113 { "DJVU", 0, MagickString("AT&TFORM") },
114 { "DOT", 0, MagickString("digraph") },
115 { "DPX", 0, MagickString("SDPX") },
116 { "DPX", 0, MagickString("XPDS") },
117 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
118 { "EPT", 0, MagickString("\305\320\323\306") },
119 { "EXR", 0, MagickString("\166\057\061\001") },
120 { "FAX", 0, MagickString("DFAX") },
121 { "FIG", 0, MagickString("#FIG") },
122 { "FITS", 0, MagickString("IT0") },
123 { "FITS", 0, MagickString("SIMPLE") },
124 { "GIF", 0, MagickString("GIF8") },
125 { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
126 { "HDF", 1, MagickString("HDF") },
127 { "HDR", 0, MagickString("#?RADIANCE") },
128 { "HDR", 0, MagickString("#?RGBE") },
129 { "HPGL", 0, MagickString("IN;") },
130 { "HTML", 1, MagickString("HTML") },
131 { "HTML", 1, MagickString("html") },
132 { "ILBM", 8, MagickString("ILBM") },
133 { "IPTCWTEXT", 0, MagickString("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
134 { "IPTCTEXT", 0, MagickString("2#0=\042�\042") },
135 { "IPTC", 0, MagickString("\034\002") },
136 { "JNG", 0, MagickString("\213JNG\r\n\032\n") },
137 { "JPEG", 0, MagickString("\377\330\377") },
138 { "J2K", 0, MagickString("\xff\x4f\xff\x51") },
139 { "JPC", 0, MagickString("\x0d\x0a\x87\x0a") },
140 { "JP2", 4, MagickString("\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a") },
141 { "MAT", 0, MagickString("MATLAB 5.0 MAT-file,") },
142 { "MIFF", 0, MagickString("Id=ImageMagick") },
143 { "MIFF", 0, MagickString("id=ImageMagick") },
144 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
145 { "MPC", 0, MagickString("id=MagickCache") },
146 { "MPEG", 0, MagickString("\000\000\001\263") },
147 { "MRW", 0, MagickString("\x00MRM") },
148 { "MVG", 0, MagickString("push graphic-context") },
149 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
150 { "PCD", 2048, MagickString("PCD_") },
151 { "PCL", 0, MagickString("\033E\033") },
152 { "PCX", 0, MagickString("\012\002") },
153 { "PCX", 0, MagickString("\012\005") },
154 { "PDB", 60, MagickString("vIMGView") },
155 { "PDF", 0, MagickString("%PDF-") },
156 { "PES", 0, MagickString("#PES") },
157 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
158 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
159 { "PGX", 0, MagickString("\050\107\020\115\046") },
160 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
161 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
162 { "PBM", 0, MagickString("P1") },
163 { "PGM", 0, MagickString("P2") },
164 { "PPM", 0, MagickString("P3") },
165 { "PBM", 0, MagickString("P4") },
166 { "PGM", 0, MagickString("P5") },
167 { "PPM", 0, MagickString("P6") },
168 { "PAM", 0, MagickString("P7") },
169 { "PFM", 0, MagickString("PF") },
170 { "PFM", 0, MagickString("Pf") },
171 { "PS", 0, MagickString("%!") },
172 { "PS", 0, MagickString("\004%!") },
173 { "PS", 0, MagickString("\305\320\323\306") },
174 { "PSB", 0, MagickString("8BPB") },
175 { "PSD", 0, MagickString("8BPS") },
176 { "PWP", 0, MagickString("SFW95") },
177 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
178 { "RLE", 0, MagickString("\122\314") },
179 { "SCT", 0, MagickString("CT") },
180 { "SFW", 0, MagickString("SFW94") },
181 { "SGI", 0, MagickString("\001\332") },
182 { "SUN", 0, MagickString("\131\246\152\225") },
183 { "SVG", 1, MagickString("?XML") },
184 { "SVG", 1, MagickString("?xml") },
185 { "TIFF", 0, MagickString("\115\115\000\052") },
186 { "TIFF", 0, MagickString("\111\111\052\000") },
187 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
188 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
189 { "TTF", 0, MagickString("\000\001\000\000\000") },
190 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
191 { "VICAR", 0, MagickString("LBLSIZE") },
192 { "VICAR", 0, MagickString("NJPL1I") },
193 { "VIFF", 0, MagickString("\253\001") },
194 { "WEBP", 8, MagickString("WEBP") },
195 { "WMF", 0, MagickString("\327\315\306\232") },
196 { "WMF", 0, MagickString("\001\000\011\000") },
197 { "WPG", 0, MagickString("\377WPC") },
198 { "XBM", 0, MagickString("#define") },
199 { "XCF", 0, MagickString("gimp xcf") },
200 { "XEF", 0, MagickString("FOVb") },
201 { "XPM", 1, MagickString("* XPM *") },
202 { "XWD", 4, MagickString("\007\000\000") },
203 { "XWD", 5, MagickString("\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 LinkedListInfo *AcquireMagicCache(const char *filename,
247 ExceptionInfo *exception)
266 Load external magic map.
268 magic_cache=NewLinkedList(0);
269 if (magic_cache == (LinkedListInfo *) NULL)
270 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
273 options=GetConfigureOptions(filename,exception);
274 option=(const StringInfo *) GetNextValueInLinkedList(options);
275 while (option != (const StringInfo *) NULL)
277 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
278 status&=LoadMagicCache(magic_cache,(const char *)
279 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
280 option=(const StringInfo *) GetNextValueInLinkedList(options);
282 options=DestroyConfigureOptions(options);
284 Load built-in magic map.
286 for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
291 register const MagicMapInfo
295 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
296 if (magic_info == (MagicInfo *) NULL)
298 (void) ThrowMagickException(exception,GetMagickModule(),
299 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
302 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
303 magic_info->path=(char *) "[built-in]";
304 magic_info->name=(char *) p->name;
305 magic_info->offset=p->offset;
306 magic_info->target=(char *) p->magic;
307 magic_info->magic=(unsigned char *) p->magic;
308 magic_info->length=p->length;
309 magic_info->exempt=MagickTrue;
310 magic_info->signature=MagickSignature;
311 status&=AppendValueToLinkedList(magic_cache,magic_info);
312 if (status == MagickFalse)
313 (void) ThrowMagickException(exception,GetMagickModule(),
314 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 + G e t M a g i c I n f o %
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % GetMagicInfo() searches the magic list for the specified name and if found
331 % returns attributes for that magic.
333 % The format of the GetMagicInfo method is:
335 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
336 % const size_t length,ExceptionInfo *exception)
338 % A description of each parameter follows:
340 % o magic: A binary string generally representing the first few characters
341 % of the image file or blob.
343 % o length: the length of the binary signature.
345 % o exception: return any errors or warnings in this structure.
348 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
349 const size_t length,ExceptionInfo *exception)
351 register const MagicInfo
354 assert(exception != (ExceptionInfo *) NULL);
355 if (IsMagicCacheInstantiated(exception) == MagickFalse)
356 return((const MagicInfo *) NULL);
358 Search for magic tag.
360 LockSemaphoreInfo(magic_semaphore);
361 ResetLinkedListIterator(magic_cache);
362 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
363 if (magic == (const unsigned char *) NULL)
365 UnlockSemaphoreInfo(magic_semaphore);
368 while (p != (const MagicInfo *) NULL)
370 assert(p->offset >= 0);
371 if (((size_t) (p->offset+p->length) <= length) &&
372 (memcmp(magic+p->offset,p->magic,p->length) == 0))
374 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
376 if (p != (const MagicInfo *) NULL)
377 (void) InsertValueInLinkedList(magic_cache,0,
378 RemoveElementByValueFromLinkedList(magic_cache,p));
379 UnlockSemaphoreInfo(magic_semaphore);
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % G e t M a g i c I n f o L i s t %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % GetMagicInfoList() returns any image aliases that match the specified
397 % The magic of the GetMagicInfoList function is:
399 % const MagicInfo **GetMagicInfoList(const char *pattern,
400 % size_t *number_aliases,ExceptionInfo *exception)
402 % A description of each parameter follows:
404 % o pattern: Specifies a pointer to a text string containing a pattern.
406 % o number_aliases: This integer returns the number of aliases in the list.
408 % o exception: return any errors or warnings in this structure.
412 #if defined(__cplusplus) || defined(c_plusplus)
416 static int MagicInfoCompare(const void *x,const void *y)
422 p=(const MagicInfo **) x,
423 q=(const MagicInfo **) y;
424 if (LocaleCompare((*p)->path,(*q)->path) == 0)
425 return(LocaleCompare((*p)->name,(*q)->name));
426 return(LocaleCompare((*p)->path,(*q)->path));
429 #if defined(__cplusplus) || defined(c_plusplus)
433 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
434 size_t *number_aliases,ExceptionInfo *exception)
439 register const MagicInfo
448 assert(pattern != (char *) NULL);
449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
450 assert(number_aliases != (size_t *) NULL);
452 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
453 if (p == (const MagicInfo *) NULL)
454 return((const MagicInfo **) NULL);
455 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
456 GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
457 if (aliases == (const MagicInfo **) NULL)
458 return((const MagicInfo **) NULL);
462 LockSemaphoreInfo(magic_semaphore);
463 ResetLinkedListIterator(magic_cache);
464 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
465 for (i=0; p != (const MagicInfo *) NULL; )
467 if ((p->stealth == MagickFalse) &&
468 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
470 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
472 UnlockSemaphoreInfo(magic_semaphore);
473 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
474 aliases[i]=(MagicInfo *) NULL;
475 *number_aliases=(size_t) i;
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % G e t M a g i c L i s t %
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % GetMagicList() returns any image format aliases that match the specified
493 % The format of the GetMagicList function is:
495 % char **GetMagicList(const char *pattern,size_t *number_aliases,
496 % ExceptionInfo *exception)
498 % A description of each parameter follows:
500 % o pattern: Specifies a pointer to a text string containing a pattern.
502 % o number_aliases: This integer returns the number of image format aliases
505 % o exception: return any errors or warnings in this structure.
509 #if defined(__cplusplus) || defined(c_plusplus)
513 static int MagicCompare(const void *x,const void *y)
521 return(LocaleCompare(p,q));
524 #if defined(__cplusplus) || defined(c_plusplus)
528 MagickExport char **GetMagicList(const char *pattern,size_t *number_aliases,
529 ExceptionInfo *exception)
534 register const MagicInfo
541 Allocate configure list.
543 assert(pattern != (char *) NULL);
544 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
545 assert(number_aliases != (size_t *) NULL);
547 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
548 if (p == (const MagicInfo *) NULL)
549 return((char **) NULL);
550 aliases=(char **) AcquireQuantumMemory((size_t)
551 GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
552 if (aliases == (char **) NULL)
553 return((char **) NULL);
554 LockSemaphoreInfo(magic_semaphore);
555 ResetLinkedListIterator(magic_cache);
556 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
557 for (i=0; p != (const MagicInfo *) NULL; )
559 if ((p->stealth == MagickFalse) &&
560 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
561 aliases[i++]=ConstantString(p->name);
562 p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
564 UnlockSemaphoreInfo(magic_semaphore);
565 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
566 aliases[i]=(char *) NULL;
567 *number_aliases=(size_t) i;
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % G e t M a g i c N a m e %
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % GetMagicName() returns the name associated with the magic.
584 % The format of the GetMagicName method is:
586 % const char *GetMagicName(const MagicInfo *magic_info)
588 % A description of each parameter follows:
590 % o magic_info: The magic info.
593 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
596 assert(magic_info != (MagicInfo *) NULL);
597 assert(magic_info->signature == MagickSignature);
598 return(magic_info->name);
602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 + I s M a g i c C a c h e I n s t a n t i a t e d %
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612 % IsMagicCacheInstantiated() determines if the magic list is instantiated.
613 % If not, it instantiates the list and returns it.
615 % The format of the IsMagicInstantiated method is:
617 % MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
619 % A description of each parameter follows.
621 % o exception: return any errors or warnings in this structure.
624 static MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
626 if (magic_cache == (LinkedListInfo *) NULL)
628 if (magic_semaphore == (SemaphoreInfo *) NULL)
629 ActivateSemaphoreInfo(&magic_semaphore);
630 LockSemaphoreInfo(magic_semaphore);
631 if (magic_cache == (LinkedListInfo *) NULL)
632 magic_cache=AcquireMagicCache(MagicFilename,exception);
633 UnlockSemaphoreInfo(magic_semaphore);
635 return(magic_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % L i s t M a g i c I n f o %
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % ListMagicInfo() lists the magic info to a file.
651 % The format of the ListMagicInfo method is:
653 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
655 % A description of each parameter follows.
657 % o file: An pointer to a FILE.
659 % o exception: return any errors or warnings in this structure.
662 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
663 ExceptionInfo *exception)
680 if (file == (const FILE *) NULL)
682 magic_info=GetMagicInfoList("*",&number_aliases,exception);
683 if (magic_info == (const MagicInfo **) NULL)
686 path=(const char *) NULL;
687 for (i=0; i < (ssize_t) number_aliases; i++)
689 if (magic_info[i]->stealth != MagickFalse)
691 if ((path == (const char *) NULL) ||
692 (LocaleCompare(path,magic_info[i]->path) != 0))
694 if (magic_info[i]->path != (char *) NULL)
695 (void) FormatLocaleFile(file,"\nPath: %s\n\n",magic_info[i]->path);
696 (void) FormatLocaleFile(file,"Name Offset Target\n");
697 (void) FormatLocaleFile(file,
698 "-------------------------------------------------"
699 "------------------------------\n");
701 path=magic_info[i]->path;
702 (void) FormatLocaleFile(file,"%s",magic_info[i]->name);
703 for (j=(ssize_t) strlen(magic_info[i]->name); j <= 9; j++)
704 (void) FormatLocaleFile(file," ");
705 (void) FormatLocaleFile(file,"%6ld ",(long) magic_info[i]->offset);
706 if (magic_info[i]->target != (char *) NULL)
711 for (j=0; magic_info[i]->target[j] != '\0'; j++)
712 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
713 (void) FormatLocaleFile(file,"%c",magic_info[i]->target[j]);
715 (void) FormatLocaleFile(file,"\\%03o",(unsigned int)
716 ((unsigned char) magic_info[i]->target[j]));
718 (void) FormatLocaleFile(file,"\n");
721 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 + L o a d M a g i c L i s t %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % LoadMagicCache() loads the magic configurations which provides a mapping
737 % between magic attributes and a magic name.
739 % The format of the LoadMagicCache method is:
741 % MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
742 % const char *xml,const char *filename,const size_t depth,
743 % ExceptionInfo *exception)
745 % A description of each parameter follows:
747 % o xml: The magic list in XML format.
749 % o filename: The magic list filename.
751 % o depth: depth of <include /> statements.
753 % o exception: return any errors or warnings in this structure.
756 static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
757 const char *xml,const char *filename,const size_t depth,
758 ExceptionInfo *exception)
761 keyword[MaxTextExtent],
774 Load the magic map file.
776 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
777 "Loading magic configure file \"%s\" ...",filename);
778 if (xml == (char *) NULL)
781 magic_info=(MagicInfo *) NULL;
782 token=AcquireString(xml);
783 for (q=(char *) xml; *q != '\0'; )
788 GetMagickToken(q,&q,token);
791 (void) CopyMagickString(keyword,token,MaxTextExtent);
792 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
797 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
798 GetMagickToken(q,&q,token);
801 if (LocaleNCompare(keyword,"<!--",4) == 0)
806 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
807 GetMagickToken(q,&q,token);
810 if (LocaleCompare(keyword,"<include") == 0)
815 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
817 (void) CopyMagickString(keyword,token,MaxTextExtent);
818 GetMagickToken(q,&q,token);
821 GetMagickToken(q,&q,token);
822 if (LocaleCompare(keyword,"file") == 0)
825 (void) ThrowMagickException(exception,GetMagickModule(),
826 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
833 GetPathComponent(filename,HeadPath,path);
835 (void) ConcatenateMagickString(path,DirectorySeparator,
837 if (*token == *DirectorySeparator)
838 (void) CopyMagickString(path,token,MaxTextExtent);
840 (void) ConcatenateMagickString(path,token,MaxTextExtent);
841 xml=FileToXML(path,~0UL);
842 if (xml != (char *) NULL)
844 status&=LoadMagicCache(magic_cache,xml,path,depth+1,
846 xml=(char *) RelinquishMagickMemory(xml);
853 if (LocaleCompare(keyword,"<magic") == 0)
858 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
859 if (magic_info == (MagicInfo *) NULL)
860 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
861 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
862 magic_info->path=ConstantString(filename);
863 magic_info->exempt=MagickFalse;
864 magic_info->signature=MagickSignature;
867 if (magic_info == (MagicInfo *) NULL)
869 if (LocaleCompare(keyword,"/>") == 0)
871 status=AppendValueToLinkedList(magic_cache,magic_info);
872 if (status == MagickFalse)
873 (void) ThrowMagickException(exception,GetMagickModule(),
874 ResourceLimitError,"MemoryAllocationFailed","`%s'",
876 magic_info=(MagicInfo *) NULL;
879 GetMagickToken(q,(const char **) NULL,token);
882 GetMagickToken(q,&q,token);
883 GetMagickToken(q,&q,token);
889 if (LocaleCompare((char *) keyword,"name") == 0)
891 magic_info->name=ConstantString(token);
899 if (LocaleCompare((char *) keyword,"offset") == 0)
901 magic_info->offset=(MagickOffsetType) StringToLong(token);
909 if (LocaleCompare((char *) keyword,"stealth") == 0)
911 magic_info->stealth=IsStringTrue(token);
919 if (LocaleCompare((char *) keyword,"target") == 0)
924 register unsigned char
930 length=strlen(token);
931 magic_info->target=ConstantString(token);
932 magic_info->magic=(unsigned char *) ConstantString(token);
934 for (p=magic_info->target; *p != '\0'; )
939 if (isdigit((int) ((unsigned char) *p)) != 0)
944 *q++=(unsigned char) strtol(p,&end,8);
946 magic_info->length++;
951 case 'b': *q='\b'; break;
952 case 'f': *q='\f'; break;
953 case 'n': *q='\n'; break;
954 case 'r': *q='\r'; break;
955 case 't': *q='\t'; break;
956 case 'v': *q='\v'; break;
957 case 'a': *q='a'; break;
958 case '?': *q='\?'; break;
959 default: *q=(unsigned char) (*p); break;
963 magic_info->length++;
967 if (LocaleNCompare(p,"&",5) == 0)
968 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
969 *q++=(unsigned char) (*p++);
970 magic_info->length++;
980 token=(char *) RelinquishMagickMemory(token);
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 + M a g i c C o m p o n e n t G e n e s i s %
993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % MagicComponentGenesis() instantiates the magic component.
997 % The format of the MagicComponentGenesis method is:
999 % MagickBooleanType MagicComponentGenesis(void)
1002 MagickPrivate MagickBooleanType MagicComponentGenesis(void)
1004 if (magic_semaphore == (SemaphoreInfo *) NULL)
1005 magic_semaphore=AcquireSemaphoreInfo();
1010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 + M a g i c C o m p o n e n t T e r m i n u s %
1018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020 % MagicComponentTerminus() destroys the magic component.
1022 % The format of the MagicComponentTerminus method is:
1024 % MagicComponentTerminus(void)
1028 static void *DestroyMagicElement(void *magic_info)
1033 p=(MagicInfo *) magic_info;
1034 if (p->exempt == MagickFalse)
1036 if (p->path != (char *) NULL)
1037 p->path=DestroyString(p->path);
1038 if (p->name != (char *) NULL)
1039 p->name=DestroyString(p->name);
1040 if (p->target != (char *) NULL)
1041 p->target=DestroyString(p->target);
1042 if (p->magic != (unsigned char *) NULL)
1043 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1045 p=(MagicInfo *) RelinquishMagickMemory(p);
1046 return((void *) NULL);
1049 MagickPrivate void MagicComponentTerminus(void)
1051 if (magic_semaphore == (SemaphoreInfo *) NULL)
1052 ActivateSemaphoreInfo(&magic_semaphore);
1053 LockSemaphoreInfo(magic_semaphore);
1054 if (magic_cache != (LinkedListInfo *) NULL)
1055 magic_cache=DestroyLinkedList(magic_cache,DestroyMagicElement);
1056 UnlockSemaphoreInfo(magic_semaphore);
1057 RelinquishSemaphoreInfo(&magic_semaphore);