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"
64 #define MagicFilename "magic.xml"
65 #define MagickString(magic) (const unsigned char *) (magic), sizeof(magic)-1
70 typedef struct _MagicMapInfo
75 const MagickOffsetType
88 static const MagicMapInfo
91 { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
92 { "8BIMTEXT", 0, MagickString("8BIM#") },
93 { "8BIM", 0, MagickString("8BIM") },
94 { "BMP", 0, MagickString("BA") },
95 { "BMP", 0, MagickString("BM") },
96 { "BMP", 0, MagickString("CI") },
97 { "BMP", 0, MagickString("CP") },
98 { "BMP", 0, MagickString("IC") },
99 { "PICT", 0, MagickString("PICT") },
100 { "BMP", 0, MagickString("PI") },
101 { "CALS", 21, MagickString("version: MIL-STD-1840") },
102 { "CALS", 0, MagickString("srcdocid:") },
103 { "CALS", 9, MagickString("srcdocid:") },
104 { "CALS", 8, MagickString("rorient:") },
105 { "CGM", 0, MagickString("BEGMF") },
106 { "CIN", 0, MagickString("\200\052\137\327") },
107 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
108 { "DCM", 128, MagickString("DICM") },
109 { "DCX", 0, MagickString("\261\150\336\72") },
110 { "DIB", 0, MagickString("\050\000") },
111 { "DDS", 0, MagickString("DDS ") },
112 { "DJVU", 0, MagickString("AT&TFORM") },
113 { "DOT", 0, MagickString("digraph") },
114 { "DPX", 0, MagickString("SDPX") },
115 { "DPX", 0, MagickString("XPDS") },
116 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
117 { "EPT", 0, MagickString("\305\320\323\306") },
118 { "EXR", 0, MagickString("\166\057\061\001") },
119 { "FAX", 0, MagickString("DFAX") },
120 { "FIG", 0, MagickString("#FIG") },
121 { "FITS", 0, MagickString("IT0") },
122 { "FITS", 0, MagickString("SIMPLE") },
123 { "FPX", 0, MagickString("\320\317\021\340") },
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 { "JPC", 0, MagickString("\377\117") },
139 { "JP2", 4, MagickString("\152\120\040\040\015") },
140 { "MAT", 0, MagickString("MATLAB 5.0 MAT-file,") },
141 { "MIFF", 0, MagickString("Id=ImageMagick") },
142 { "MIFF", 0, MagickString("id=ImageMagick") },
143 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
144 { "MPC", 0, MagickString("id=MagickCache") },
145 { "MPEG", 0, MagickString("\000\000\001\263") },
146 { "MRW", 0, MagickString("\x00MRM") },
147 { "MVG", 0, MagickString("push graphic-context") },
148 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
149 { "PCD", 2048, MagickString("PCD_") },
150 { "PCL", 0, MagickString("\033E\033") },
151 { "PCX", 0, MagickString("\012\002") },
152 { "PCX", 0, MagickString("\012\005") },
153 { "PDB", 60, MagickString("vIMGView") },
154 { "PDF", 0, MagickString("%PDF-") },
155 { "PES", 0, MagickString("#PES") },
156 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
157 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
158 { "PGX", 0, MagickString("\050\107\020\115\046") },
159 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
160 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
161 { "PBM", 0, MagickString("P1") },
162 { "PGM", 0, MagickString("P2") },
163 { "PPM", 0, MagickString("P3") },
164 { "PBM", 0, MagickString("P4") },
165 { "PGM", 0, MagickString("P5") },
166 { "PPM", 0, MagickString("P6") },
167 { "PAM", 0, MagickString("P7") },
168 { "PFM", 0, MagickString("PF") },
169 { "PFM", 0, MagickString("Pf") },
170 { "PS", 0, MagickString("%!") },
171 { "PS", 0, MagickString("\004%!") },
172 { "PS", 0, MagickString("\305\320\323\306") },
173 { "PSB", 0, MagickString("8BPB") },
174 { "PSD", 0, MagickString("8BPS") },
175 { "PWP", 0, MagickString("SFW95") },
176 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
177 { "RLE", 0, MagickString("\122\314") },
178 { "SCT", 0, MagickString("CT") },
179 { "SFW", 0, MagickString("SFW94") },
180 { "SGI", 0, MagickString("\001\332") },
181 { "SUN", 0, MagickString("\131\246\152\225") },
182 { "SVG", 1, MagickString("?XML") },
183 { "SVG", 1, MagickString("?xml") },
184 { "TIFF", 0, MagickString("\115\115\000\052") },
185 { "TIFF", 0, MagickString("\111\111\052\000") },
186 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
187 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
188 { "TTF", 0, MagickString("\000\001\000\000\000") },
189 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
190 { "VICAR", 0, MagickString("LBLSIZE") },
191 { "VICAR", 0, MagickString("NJPL1I") },
192 { "VIFF", 0, MagickString("\253\001") },
193 { "WEBP", 8, MagickString("WEBP") },
194 { "WMF", 0, MagickString("\327\315\306\232") },
195 { "WMF", 0, MagickString("\001\000\011\000") },
196 { "WPG", 0, MagickString("\377WPC") },
197 { "XBM", 0, MagickString("#define") },
198 { "XCF", 0, MagickString("gimp xcf") },
199 { "XEF", 0, MagickString("FOVb") },
200 { "XPM", 1, MagickString("* XPM *") },
201 { "XWD", 4, MagickString("\007\000\000") },
202 { "XWD", 5, MagickString("\000\000\007") }
205 static LinkedListInfo
206 *magic_list = (LinkedListInfo *) NULL;
209 *magic_semaphore = (SemaphoreInfo *) NULL;
211 static volatile MagickBooleanType
212 instantiate_magic = MagickFalse;
215 Forward declarations.
217 static MagickBooleanType
218 InitializeMagicList(ExceptionInfo *),
219 LoadMagicLists(const char *,ExceptionInfo *);
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 + G e t M a g i c I n f o %
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 % GetMagicInfo() searches the magic list for the specified name and if found
233 % returns attributes for that magic.
235 % The format of the GetMagicInfo method is:
237 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
238 % const size_t length,ExceptionInfo *exception)
240 % A description of each parameter follows:
242 % o magic: A binary string generally representing the first few characters
243 % of the image file or blob.
245 % o length: the length of the binary signature.
247 % o exception: return any errors or warnings in this structure.
250 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
251 const size_t length,ExceptionInfo *exception)
253 register const MagicInfo
256 assert(exception != (ExceptionInfo *) NULL);
257 if ((magic_list == (LinkedListInfo *) NULL) ||
258 (instantiate_magic == MagickFalse))
259 if (InitializeMagicList(exception) == MagickFalse)
260 return((const MagicInfo *) NULL);
261 if ((magic_list == (LinkedListInfo *) NULL) ||
262 (IsLinkedListEmpty(magic_list) != MagickFalse))
263 return((const MagicInfo *) NULL);
264 if (magic == (const unsigned char *) NULL)
265 return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
267 return((const MagicInfo *) NULL);
269 Search for magic tag.
271 LockSemaphoreInfo(magic_semaphore);
272 ResetLinkedListIterator(magic_list);
273 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
274 while (p != (const MagicInfo *) NULL)
276 assert(p->offset >= 0);
277 if (((size_t) (p->offset+p->length) <= length) &&
278 (memcmp(magic+p->offset,p->magic,p->length) == 0))
280 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
282 if (p != (const MagicInfo *) NULL)
283 (void) InsertValueInLinkedList(magic_list,0,
284 RemoveElementByValueFromLinkedList(magic_list,p));
285 UnlockSemaphoreInfo(magic_semaphore);
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % G e t M a g i c I n f o L i s t %
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % GetMagicInfoList() returns any image aliases that match the specified
303 % The magic of the GetMagicInfoList function is:
305 % const MagicInfo **GetMagicInfoList(const char *pattern,
306 % size_t *number_aliases,ExceptionInfo *exception)
308 % A description of each parameter follows:
310 % o pattern: Specifies a pointer to a text string containing a pattern.
312 % o number_aliases: This integer returns the number of aliases in the list.
314 % o exception: return any errors or warnings in this structure.
318 #if defined(__cplusplus) || defined(c_plusplus)
322 static int MagicInfoCompare(const void *x,const void *y)
328 p=(const MagicInfo **) x,
329 q=(const MagicInfo **) y;
330 if (LocaleCompare((*p)->path,(*q)->path) == 0)
331 return(LocaleCompare((*p)->name,(*q)->name));
332 return(LocaleCompare((*p)->path,(*q)->path));
335 #if defined(__cplusplus) || defined(c_plusplus)
339 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
340 size_t *number_aliases,ExceptionInfo *exception)
345 register const MagicInfo
354 assert(pattern != (char *) NULL);
355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
356 assert(number_aliases != (size_t *) NULL);
358 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
359 if (p == (const MagicInfo *) NULL)
360 return((const MagicInfo **) NULL);
361 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
362 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
363 if (aliases == (const MagicInfo **) NULL)
364 return((const MagicInfo **) NULL);
368 LockSemaphoreInfo(magic_semaphore);
369 ResetLinkedListIterator(magic_list);
370 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
371 for (i=0; p != (const MagicInfo *) NULL; )
373 if ((p->stealth == MagickFalse) &&
374 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
376 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
378 UnlockSemaphoreInfo(magic_semaphore);
379 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
380 aliases[i]=(MagicInfo *) NULL;
381 *number_aliases=(size_t) i;
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % G e t M a g i c L i s t %
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % GetMagicList() returns any image format aliases that match the specified
399 % The format of the GetMagicList function is:
401 % char **GetMagicList(const char *pattern,size_t *number_aliases,
402 % ExceptionInfo *exception)
404 % A description of each parameter follows:
406 % o pattern: Specifies a pointer to a text string containing a pattern.
408 % o number_aliases: This integer returns the number of image format aliases
411 % o exception: return any errors or warnings in this structure.
415 #if defined(__cplusplus) || defined(c_plusplus)
419 static int MagicCompare(const void *x,const void *y)
427 return(LocaleCompare(p,q));
430 #if defined(__cplusplus) || defined(c_plusplus)
434 MagickExport char **GetMagicList(const char *pattern,
435 size_t *number_aliases,ExceptionInfo *exception)
440 register const MagicInfo
447 Allocate configure list.
449 assert(pattern != (char *) NULL);
450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
451 assert(number_aliases != (size_t *) NULL);
453 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
454 if (p == (const MagicInfo *) NULL)
455 return((char **) NULL);
456 aliases=(char **) AcquireQuantumMemory((size_t)
457 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
458 if (aliases == (char **) NULL)
459 return((char **) NULL);
460 LockSemaphoreInfo(magic_semaphore);
461 ResetLinkedListIterator(magic_list);
462 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
463 for (i=0; p != (const MagicInfo *) NULL; )
465 if ((p->stealth == MagickFalse) &&
466 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
467 aliases[i++]=ConstantString(p->name);
468 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
470 UnlockSemaphoreInfo(magic_semaphore);
471 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
472 aliases[i]=(char *) NULL;
473 *number_aliases=(size_t) i;
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % G e t M a g i c N a m e %
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 % GetMagicName() returns the name associated with the magic.
490 % The format of the GetMagicName method is:
492 % const char *GetMagicName(const MagicInfo *magic_info)
494 % A description of each parameter follows:
496 % o magic_info: The magic info.
499 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
502 assert(magic_info != (MagicInfo *) NULL);
503 assert(magic_info->signature == MagickSignature);
504 return(magic_info->name);
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 + I n i t i a l i z e M a g i c L i s t %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % InitializeMagicList() initializes the magic list.
520 % The format of the InitializeMagicList method is:
522 % MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
524 % A description of each parameter follows.
526 % o exception: return any errors or warnings in this structure.
529 static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
531 if ((magic_list == (LinkedListInfo *) NULL) &&
532 (instantiate_magic == MagickFalse))
534 if (magic_semaphore == (SemaphoreInfo *) NULL)
535 AcquireSemaphoreInfo(&magic_semaphore);
536 LockSemaphoreInfo(magic_semaphore);
537 if ((magic_list == (LinkedListInfo *) NULL) &&
538 (instantiate_magic == MagickFalse))
540 (void) LoadMagicLists(MagicFilename,exception);
541 instantiate_magic=MagickTrue;
543 UnlockSemaphoreInfo(magic_semaphore);
545 return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % L i s t M a g i c I n f o %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % ListMagicInfo() lists the magic info to a file.
561 % The format of the ListMagicInfo method is:
563 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
565 % A description of each parameter follows.
567 % o file: An pointer to a FILE.
569 % o exception: return any errors or warnings in this structure.
572 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
573 ExceptionInfo *exception)
590 if (file == (const FILE *) NULL)
592 magic_info=GetMagicInfoList("*",&number_aliases,exception);
593 if (magic_info == (const MagicInfo **) NULL)
596 path=(const char *) NULL;
597 for (i=0; i < (ssize_t) number_aliases; i++)
599 if (magic_info[i]->stealth != MagickFalse)
601 if ((path == (const char *) NULL) ||
602 (LocaleCompare(path,magic_info[i]->path) != 0))
604 if (magic_info[i]->path != (char *) NULL)
605 (void) FormatLocaleFile(file,"\nPath: %s\n\n",magic_info[i]->path);
606 (void) FormatLocaleFile(file,"Name Offset Target\n");
607 (void) FormatLocaleFile(file,
608 "-------------------------------------------------"
609 "------------------------------\n");
611 path=magic_info[i]->path;
612 (void) FormatLocaleFile(file,"%s",magic_info[i]->name);
613 for (j=(ssize_t) strlen(magic_info[i]->name); j <= 9; j++)
614 (void) FormatLocaleFile(file," ");
615 (void) FormatLocaleFile(file,"%6ld ",(long) magic_info[i]->offset);
616 if (magic_info[i]->target != (char *) NULL)
621 for (j=0; magic_info[i]->target[j] != '\0'; j++)
622 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
623 (void) FormatLocaleFile(file,"%c",magic_info[i]->target[j]);
625 (void) FormatLocaleFile(file,"\\%03o",(unsigned int)
626 ((unsigned char) magic_info[i]->target[j]));
628 (void) FormatLocaleFile(file,"\n");
631 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 + L o a d M a g i c L i s t %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % LoadMagicList() loads the magic configuration file which provides a mapping
647 % between magic attributes and a magic name.
649 % The format of the LoadMagicList method is:
651 % MagickBooleanType LoadMagicList(const char *xml,const char *filename,
652 % const size_t depth,ExceptionInfo *exception)
654 % A description of each parameter follows:
656 % o xml: The magic list in XML format.
658 % o filename: The magic list filename.
660 % o depth: depth of <include /> statements.
662 % o exception: return any errors or warnings in this structure.
665 static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
666 const size_t depth,ExceptionInfo *exception)
669 keyword[MaxTextExtent],
682 Load the magic map file.
684 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
685 "Loading magic configure file \"%s\" ...",filename);
686 if (xml == (char *) NULL)
688 if (magic_list == (LinkedListInfo *) NULL)
690 magic_list=NewLinkedList(0);
691 if (magic_list == (LinkedListInfo *) NULL)
693 ThrowFileException(exception,ResourceLimitError,
694 "MemoryAllocationFailed",filename);
699 magic_info=(MagicInfo *) NULL;
700 token=AcquireString(xml);
701 for (q=(char *) xml; *q != '\0'; )
706 GetMagickToken(q,&q,token);
709 (void) CopyMagickString(keyword,token,MaxTextExtent);
710 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
715 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
716 GetMagickToken(q,&q,token);
719 if (LocaleNCompare(keyword,"<!--",4) == 0)
724 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
725 GetMagickToken(q,&q,token);
728 if (LocaleCompare(keyword,"<include") == 0)
733 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
735 (void) CopyMagickString(keyword,token,MaxTextExtent);
736 GetMagickToken(q,&q,token);
739 GetMagickToken(q,&q,token);
740 if (LocaleCompare(keyword,"file") == 0)
743 (void) ThrowMagickException(exception,GetMagickModule(),
744 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
751 GetPathComponent(filename,HeadPath,path);
753 (void) ConcatenateMagickString(path,DirectorySeparator,
755 if (*token == *DirectorySeparator)
756 (void) CopyMagickString(path,token,MaxTextExtent);
758 (void) ConcatenateMagickString(path,token,MaxTextExtent);
759 xml=FileToString(path,~0UL,exception);
760 if (xml != (char *) NULL)
762 status=LoadMagicList(xml,path,depth+1,exception);
763 xml=(char *) RelinquishMagickMemory(xml);
770 if (LocaleCompare(keyword,"<magic") == 0)
775 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
776 if (magic_info == (MagicInfo *) NULL)
777 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
778 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
779 magic_info->path=ConstantString(filename);
780 magic_info->exempt=MagickFalse;
781 magic_info->signature=MagickSignature;
784 if (magic_info == (MagicInfo *) NULL)
786 if (LocaleCompare(keyword,"/>") == 0)
788 status=AppendValueToLinkedList(magic_list,magic_info);
789 if (status == MagickFalse)
790 (void) ThrowMagickException(exception,GetMagickModule(),
791 ResourceLimitError,"MemoryAllocationFailed","`%s'",
793 magic_info=(MagicInfo *) NULL;
795 GetMagickToken(q,(const char **) NULL,token);
798 GetMagickToken(q,&q,token);
799 GetMagickToken(q,&q,token);
805 if (LocaleCompare((char *) keyword,"name") == 0)
807 magic_info->name=ConstantString(token);
815 if (LocaleCompare((char *) keyword,"offset") == 0)
817 magic_info->offset=(MagickOffsetType) StringToLong(token);
825 if (LocaleCompare((char *) keyword,"stealth") == 0)
827 magic_info->stealth=IsStringTrue(token);
835 if (LocaleCompare((char *) keyword,"target") == 0)
840 register unsigned char
846 length=strlen(token);
847 magic_info->target=ConstantString(token);
848 magic_info->magic=(unsigned char *) ConstantString(token);
850 for (p=magic_info->target; *p != '\0'; )
855 if (isdigit((int) ((unsigned char) *p)) != 0)
860 *q++=(unsigned char) strtol(p,&end,8);
862 magic_info->length++;
867 case 'b': *q='\b'; break;
868 case 'f': *q='\f'; break;
869 case 'n': *q='\n'; break;
870 case 'r': *q='\r'; break;
871 case 't': *q='\t'; break;
872 case 'v': *q='\v'; break;
873 case 'a': *q='a'; break;
874 case '?': *q='\?'; break;
875 default: *q=(unsigned char) (*p); break;
879 magic_info->length++;
883 if (LocaleNCompare(p,"&",5) == 0)
884 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
885 *q++=(unsigned char) (*p++);
886 magic_info->length++;
896 token=(char *) RelinquishMagickMemory(token);
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % L o a d M a g i c L i s t s %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % LoadMagicLists() loads one or more magic configuration file which provides a
912 % mapping between magic attributes and a magic name.
914 % The format of the LoadMagicLists method is:
916 % MagickBooleanType LoadMagicLists(const char *filename,
917 % ExceptionInfo *exception)
919 % A description of each parameter follows:
921 % o filename: the font file name.
923 % o exception: return any errors or warnings in this structure.
926 static MagickBooleanType LoadMagicLists(const char *filename,
927 ExceptionInfo *exception)
945 Load external magic map.
947 if (magic_list == (LinkedListInfo *) NULL)
949 magic_list=NewLinkedList(0);
950 if (magic_list == (LinkedListInfo *) NULL)
952 ThrowFileException(exception,ResourceLimitError,
953 "MemoryAllocationFailed",filename);
959 options=GetConfigureOptions(filename,exception);
960 option=(const StringInfo *) GetNextValueInLinkedList(options);
961 while (option != (const StringInfo *) NULL)
963 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
964 status&=LoadMagicList((const char *) GetStringInfoDatum(option),
965 GetStringInfoPath(option),0,exception);
966 option=(const StringInfo *) GetNextValueInLinkedList(options);
968 options=DestroyConfigureOptions(options);
970 Load built-in magic map.
972 for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
977 register const MagicMapInfo
981 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
982 if (magic_info == (MagicInfo *) NULL)
984 (void) ThrowMagickException(exception,GetMagickModule(),
985 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
988 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
989 magic_info->path=(char *) "[built-in]";
990 magic_info->name=(char *) p->name;
991 magic_info->offset=p->offset;
992 magic_info->target=(char *) p->magic;
993 magic_info->magic=(unsigned char *) p->magic;
994 magic_info->length=p->length;
995 magic_info->exempt=MagickTrue;
996 magic_info->signature=MagickSignature;
997 status&=AppendValueToLinkedList(magic_list,magic_info);
998 if (status == MagickFalse)
999 (void) ThrowMagickException(exception,GetMagickModule(),
1000 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
1002 return(status != 0 ? MagickTrue : MagickFalse);
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 + M a g i c C o m p o n e n t G e n e s i s %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016 % MagicComponentGenesis() instantiates the magic component.
1018 % The format of the MagicComponentGenesis method is:
1020 % MagickBooleanType MagicComponentGenesis(void)
1023 MagickPrivate MagickBooleanType MagicComponentGenesis(void)
1025 AcquireSemaphoreInfo(&magic_semaphore);
1030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 + M a g i c C o m p o n e n t T e r m i n u s %
1038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040 % MagicComponentTerminus() destroys the magic component.
1042 % The format of the MagicComponentTerminus method is:
1044 % MagicComponentTerminus(void)
1048 static void *DestroyMagicElement(void *magic_info)
1053 p=(MagicInfo *) magic_info;
1054 if (p->exempt == MagickFalse)
1056 if (p->path != (char *) NULL)
1057 p->path=DestroyString(p->path);
1058 if (p->name != (char *) NULL)
1059 p->name=DestroyString(p->name);
1060 if (p->target != (char *) NULL)
1061 p->target=DestroyString(p->target);
1062 if (p->magic != (unsigned char *) NULL)
1063 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1065 p=(MagicInfo *) RelinquishMagickMemory(p);
1066 return((void *) NULL);
1069 MagickPrivate void MagicComponentTerminus(void)
1071 if (magic_semaphore == (SemaphoreInfo *) NULL)
1072 AcquireSemaphoreInfo(&magic_semaphore);
1073 LockSemaphoreInfo(magic_semaphore);
1074 if (magic_list != (LinkedListInfo *) NULL)
1075 magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
1076 instantiate_magic=MagickFalse;
1077 UnlockSemaphoreInfo(magic_semaphore);
1078 DestroySemaphoreInfo(&magic_semaphore);