2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % M M AAA GGGG IIIII CCCC %
7 % M M M AAAAA G GGG I C %
9 % M M A A GGGG IIIII CCCC %
12 % MagickCore Image Magic Methods %
19 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % http://www.imagemagick.org/script/license.php %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "magick/studio.h"
42 #include "magick/blob.h"
43 #include "magick/client.h"
44 #include "magick/configure.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/hashmap.h"
48 #include "magick/magic.h"
49 #include "magick/memory_.h"
50 #include "magick/semaphore.h"
51 #include "magick/string_.h"
52 #include "magick/string-private.h"
53 #include "magick/token.h"
54 #include "magick/utility.h"
55 #include "magick/xml-tree.h"
60 #define MagicFilename "magic.xml"
61 #define MagickString(magic) (const unsigned char *) (magic), sizeof(magic)-1
66 typedef struct _MagicMapInfo
71 const MagickOffsetType
84 static const MagicMapInfo
87 { "AVI", 0, MagickString("RIFF") },
88 { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
89 { "8BIMTEXT", 0, MagickString("8BIM#") },
90 { "8BIM", 0, MagickString("8BIM") },
91 { "BMP", 0, MagickString("BA") },
92 { "BMP", 0, MagickString("BM") },
93 { "BMP", 0, MagickString("CI") },
94 { "BMP", 0, MagickString("CP") },
95 { "BMP", 0, MagickString("IC") },
96 { "BMP", 0, MagickString("PI") },
97 { "CALS", 21, MagickString("version: MIL-STD-1840") },
98 { "CALS", 0, MagickString("srcdocid:") },
99 { "CALS", 9, MagickString("srcdocid:") },
100 { "CALS", 8, MagickString("rorient:") },
101 { "CGM", 0, MagickString("BEGMF") },
102 { "CIN", 0, MagickString("\200\052\137\327") },
103 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
104 { "DCM", 128, MagickString("DICM") },
105 { "DCX", 0, MagickString("\261\150\336\72") },
106 { "DIB", 0, MagickString("\050\000") },
107 { "DDS", 0, MagickString("DDS ") },
108 { "DJVU", 0, MagickString("AT&TFORM") },
109 { "DOT", 0, MagickString("digraph") },
110 { "DPX", 0, MagickString("SDPX") },
111 { "DPX", 0, MagickString("XPDS") },
112 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
113 { "EPT", 0, MagickString("\305\320\323\306") },
114 { "EXR", 0, MagickString("\166\057\061\001") },
115 { "FAX", 0, MagickString("DFAX") },
116 { "FIG", 0, MagickString("#FIG") },
117 { "FITS", 0, MagickString("IT0") },
118 { "FITS", 0, MagickString("SIMPLE") },
119 { "FPX", 0, MagickString("\320\317\021\340") },
120 { "GIF", 0, MagickString("GIF8") },
121 { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
122 { "HDF", 1, MagickString("HDF") },
123 { "HPGL", 0, MagickString("IN;") },
124 { "HTML", 1, MagickString("HTML") },
125 { "HTML", 1, MagickString("html") },
126 { "ILBM", 8, MagickString("ILBM") },
127 { "IPTCWTEXT", 0, MagickString("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
128 { "IPTCTEXT", 0, MagickString("2#0=\042�\042") },
129 { "IPTC", 0, MagickString("\034\002") },
130 { "JNG", 0, MagickString("\213JNG\r\n\032\n") },
131 { "JPEG", 0, MagickString("\377\330\377") },
132 { "JPC", 0, MagickString("\377\117") },
133 { "JP2", 4, MagickString("\152\120\040\040\015") },
134 { "MAT", 0, MagickString("MATLAB 5.0 MAT-file,") },
135 { "MIFF", 0, MagickString("Id=ImageMagick") },
136 { "MIFF", 0, MagickString("id=ImageMagick") },
137 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
138 { "MPC", 0, MagickString("id=MagickCache") },
139 { "MPEG", 0, MagickString("\000\000\001\263") },
140 { "MRW", 0, MagickString("\x00MRM") },
141 { "MVG", 0, MagickString("push graphic-context") },
142 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
143 { "PCD", 2048, MagickString("PCD_") },
144 { "PCL", 0, MagickString("\033E\033") },
145 { "PCX", 0, MagickString("\012\002") },
146 { "PCX", 0, MagickString("\012\005") },
147 { "PDB", 60, MagickString("vIMGView") },
148 { "PDF", 0, MagickString("%PDF-") },
149 { "PES", 0, MagickString("#PES") },
150 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
151 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
152 { "PGX", 0, MagickString("\050\107\020\115\046") },
153 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
154 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
155 { "PNM", 0, MagickString("P1") },
156 { "PNM", 0, MagickString("P2") },
157 { "PNM", 0, MagickString("P3") },
158 { "PNM", 0, MagickString("P4") },
159 { "PNM", 0, MagickString("P5") },
160 { "PNM", 0, MagickString("P6") },
161 { "PNM", 0, MagickString("P7") },
162 { "PNM", 0, MagickString("PF") },
163 { "PNM", 0, MagickString("Pf") },
164 { "PS", 0, MagickString("%!") },
165 { "PS", 0, MagickString("\004%!") },
166 { "PS", 0, MagickString("\305\320\323\306") },
167 { "PSB", 0, MagickString("8BPB") },
168 { "PSD", 0, MagickString("8BPS") },
169 { "PWP", 0, MagickString("SFW95") },
170 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
171 { "RAD", 0, MagickString("#?RADIANCE") },
172 { "RAD", 0, MagickString("VIEW= ") },
173 { "RLE", 0, MagickString("\122\314") },
174 { "SCT", 0, MagickString("CT") },
175 { "SFW", 0, MagickString("SFW94") },
176 { "SGI", 0, MagickString("\001\332") },
177 { "SUN", 0, MagickString("\131\246\152\225") },
178 { "SVG", 1, MagickString("?XML") },
179 { "SVG", 1, MagickString("?xml") },
180 { "TIFF", 0, MagickString("\115\115\000\052") },
181 { "TIFF", 0, MagickString("\111\111\052\000") },
182 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
183 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
184 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
185 { "VICAR", 0, MagickString("LBLSIZE") },
186 { "VICAR", 0, MagickString("NJPL1I") },
187 { "VIFF", 0, MagickString("\253\001") },
188 { "WMF", 0, MagickString("\327\315\306\232") },
189 { "WMF", 0, MagickString("\001\000\011\000") },
190 { "WPG", 0, MagickString("\377WPC") },
191 { "XBM", 0, MagickString("#define") },
192 { "XCF", 0, MagickString("gimp xcf") },
193 { "XEF", 0, MagickString("FOVb") },
194 { "XPM", 1, MagickString("* XPM *") },
195 { "XWD", 4, MagickString("\007\000\000") },
196 { "XWD", 5, MagickString("\000\000\007") }
199 static LinkedListInfo
200 *magic_list = (LinkedListInfo *) NULL;
203 *magic_semaphore = (SemaphoreInfo *) NULL;
205 static volatile MagickBooleanType
206 instantiate_magic = MagickFalse;
209 Forward declarations.
211 static MagickBooleanType
212 InitializeMagicList(ExceptionInfo *),
213 LoadMagicLists(const char *,ExceptionInfo *);
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 + G e t M a g i c I n f o %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % GetMagicInfo() searches the magic list for the specified name and if found
227 % returns attributes for that magic.
229 % The format of the GetMagicInfo method is:
231 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
232 % const size_t length,ExceptionInfo *exception)
234 % A description of each parameter follows:
236 % o magic: A binary string generally representing the first few characters
237 % of the image file or blob.
239 % o length: the length of the binary signature.
241 % o exception: return any errors or warnings in this structure.
244 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
245 const size_t length,ExceptionInfo *exception)
247 register const MagicInfo
250 assert(exception != (ExceptionInfo *) NULL);
251 if ((magic_list == (LinkedListInfo *) NULL) ||
252 (instantiate_magic == MagickFalse))
253 if (InitializeMagicList(exception) == MagickFalse)
254 return((const MagicInfo *) NULL);
255 if ((magic_list == (LinkedListInfo *) NULL) ||
256 (IsLinkedListEmpty(magic_list) != MagickFalse))
257 return((const MagicInfo *) NULL);
258 if (magic == (const unsigned char *) NULL)
259 return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
261 return((const MagicInfo *) NULL);
263 Search for magic tag.
265 LockSemaphoreInfo(magic_semaphore);
266 ResetLinkedListIterator(magic_list);
267 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
268 while (p != (const MagicInfo *) NULL)
270 assert(p->offset >= 0);
271 if (((size_t) (p->offset+p->length) <= length) &&
272 (memcmp(magic+p->offset,p->magic,p->length) == 0))
274 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
276 if (p != (const MagicInfo *) NULL)
277 (void) InsertValueInLinkedList(magic_list,0,
278 RemoveElementByValueFromLinkedList(magic_list,p));
279 UnlockSemaphoreInfo(magic_semaphore);
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % G e t M a g i c I n f o L i s t %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % GetMagicInfoList() returns any image aliases that match the specified
297 % The magic of the GetMagicInfoList function is:
299 % const MagicInfo **GetMagicInfoList(const char *pattern,
300 % size_t *number_aliases,ExceptionInfo *exception)
302 % A description of each parameter follows:
304 % o pattern: Specifies a pointer to a text string containing a pattern.
306 % o number_aliases: This integer returns the number of aliases in the list.
308 % o exception: return any errors or warnings in this structure.
312 #if defined(__cplusplus) || defined(c_plusplus)
316 static int MagicInfoCompare(const void *x,const void *y)
322 p=(const MagicInfo **) x,
323 q=(const MagicInfo **) y;
324 if (LocaleCompare((*p)->path,(*q)->path) == 0)
325 return(LocaleCompare((*p)->name,(*q)->name));
326 return(LocaleCompare((*p)->path,(*q)->path));
329 #if defined(__cplusplus) || defined(c_plusplus)
333 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
334 size_t *number_aliases,ExceptionInfo *exception)
339 register const MagicInfo
348 assert(pattern != (char *) NULL);
349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
350 assert(number_aliases != (size_t *) NULL);
352 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
353 if (p == (const MagicInfo *) NULL)
354 return((const MagicInfo **) NULL);
355 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
356 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
357 if (aliases == (const MagicInfo **) NULL)
358 return((const MagicInfo **) NULL);
362 LockSemaphoreInfo(magic_semaphore);
363 ResetLinkedListIterator(magic_list);
364 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
365 for (i=0; p != (const MagicInfo *) NULL; )
367 if ((p->stealth == MagickFalse) &&
368 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
370 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
372 UnlockSemaphoreInfo(magic_semaphore);
373 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
374 aliases[i]=(MagicInfo *) NULL;
375 *number_aliases=(size_t) i;
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % G e t M a g i c L i s t %
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % GetMagicList() returns any image format aliases that match the specified
393 % The format of the GetMagicList function is:
395 % char **GetMagicList(const char *pattern,size_t *number_aliases,
396 % ExceptionInfo *exception)
398 % A description of each parameter follows:
400 % o pattern: Specifies a pointer to a text string containing a pattern.
402 % o number_aliases: This integer returns the number of image format aliases
405 % o exception: return any errors or warnings in this structure.
409 #if defined(__cplusplus) || defined(c_plusplus)
413 static int MagicCompare(const void *x,const void *y)
421 return(LocaleCompare(p,q));
424 #if defined(__cplusplus) || defined(c_plusplus)
428 MagickExport char **GetMagicList(const char *pattern,
429 size_t *number_aliases,ExceptionInfo *exception)
434 register const MagicInfo
441 Allocate configure list.
443 assert(pattern != (char *) NULL);
444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
445 assert(number_aliases != (size_t *) NULL);
447 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
448 if (p == (const MagicInfo *) NULL)
449 return((char **) NULL);
450 aliases=(char **) AcquireQuantumMemory((size_t)
451 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
452 if (aliases == (char **) NULL)
453 return((char **) NULL);
454 LockSemaphoreInfo(magic_semaphore);
455 ResetLinkedListIterator(magic_list);
456 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
457 for (i=0; p != (const MagicInfo *) NULL; )
459 if ((p->stealth == MagickFalse) &&
460 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
461 aliases[i++]=ConstantString(p->name);
462 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
464 UnlockSemaphoreInfo(magic_semaphore);
465 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
466 aliases[i]=(char *) NULL;
467 *number_aliases=(size_t) i;
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % G e t M a g i c N a m e %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % GetMagicName() returns the name associated with the magic.
484 % The format of the GetMagicName method is:
486 % const char *GetMagicName(const MagicInfo *magic_info)
488 % A description of each parameter follows:
490 % o magic_info: The magic info.
493 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
496 assert(magic_info != (MagicInfo *) NULL);
497 assert(magic_info->signature == MagickSignature);
498 return(magic_info->name);
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 + I n i t i a l i z e M a g i c L i s t %
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 % InitializeMagicList() initializes the magic list.
514 % The format of the InitializeMagicList method is:
516 % MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
518 % A description of each parameter follows.
520 % o exception: return any errors or warnings in this structure.
523 static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
525 if ((magic_list == (LinkedListInfo *) NULL) &&
526 (instantiate_magic == MagickFalse))
528 if (magic_semaphore == (SemaphoreInfo *) NULL)
529 AcquireSemaphoreInfo(&magic_semaphore);
530 LockSemaphoreInfo(magic_semaphore);
531 if ((magic_list == (LinkedListInfo *) NULL) &&
532 (instantiate_magic == MagickFalse))
534 (void) LoadMagicLists(MagicFilename,exception);
535 instantiate_magic=MagickTrue;
537 UnlockSemaphoreInfo(magic_semaphore);
539 return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % L i s t M a g i c I n f o %
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % ListMagicInfo() lists the magic info to a file.
555 % The format of the ListMagicInfo method is:
557 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
559 % A description of each parameter follows.
561 % o file: An pointer to a FILE.
563 % o exception: return any errors or warnings in this structure.
566 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
567 ExceptionInfo *exception)
584 if (file == (const FILE *) NULL)
586 magic_info=GetMagicInfoList("*",&number_aliases,exception);
587 if (magic_info == (const MagicInfo **) NULL)
590 path=(const char *) NULL;
591 for (i=0; i < (ssize_t) number_aliases; i++)
593 if (magic_info[i]->stealth != MagickFalse)
595 if ((path == (const char *) NULL) ||
596 (LocaleCompare(path,magic_info[i]->path) != 0))
598 if (magic_info[i]->path != (char *) NULL)
599 (void) fprintf(file,"\nPath: %s\n\n",magic_info[i]->path);
600 (void) fprintf(file,"Name Offset Target\n");
601 (void) fprintf(file,"-------------------------------------------------"
602 "------------------------------\n");
604 path=magic_info[i]->path;
605 (void) fprintf(file,"%s",magic_info[i]->name);
606 for (j=(ssize_t) strlen(magic_info[i]->name); j <= 9; j++)
607 (void) fprintf(file," ");
608 (void) fprintf(file,"%6ld ",(long) magic_info[i]->offset);
609 if (magic_info[i]->target != (char *) NULL)
614 for (j=0; magic_info[i]->target[j] != '\0'; j++)
615 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
616 (void) fprintf(file,"%c",magic_info[i]->target[j]);
618 (void) fprintf(file,"\\%03o",(unsigned int)
619 ((unsigned char) magic_info[i]->target[j]));
621 (void) fprintf(file,"\n");
624 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 + L o a d M a g i c L i s t %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % LoadMagicList() loads the magic configuration file which provides a mapping
640 % between magic attributes and a magic name.
642 % The format of the LoadMagicList method is:
644 % MagickBooleanType LoadMagicList(const char *xml,const char *filename,
645 % const size_t depth,ExceptionInfo *exception)
647 % A description of each parameter follows:
649 % o xml: The magic list in XML format.
651 % o filename: The magic list filename.
653 % o depth: depth of <include /> statements.
655 % o exception: return any errors or warnings in this structure.
658 static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
659 const size_t depth,ExceptionInfo *exception)
662 keyword[MaxTextExtent],
675 Load the magic map file.
677 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
678 "Loading magic configure file \"%s\" ...",filename);
679 if (xml == (char *) NULL)
681 if (magic_list == (LinkedListInfo *) NULL)
683 magic_list=NewLinkedList(0);
684 if (magic_list == (LinkedListInfo *) NULL)
686 ThrowFileException(exception,ResourceLimitError,
687 "MemoryAllocationFailed",filename);
692 magic_info=(MagicInfo *) NULL;
693 token=AcquireString(xml);
694 for (q=(char *) xml; *q != '\0'; )
699 GetMagickToken(q,&q,token);
702 (void) CopyMagickString(keyword,token,MaxTextExtent);
703 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
708 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
709 GetMagickToken(q,&q,token);
712 if (LocaleNCompare(keyword,"<!--",4) == 0)
717 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
718 GetMagickToken(q,&q,token);
721 if (LocaleCompare(keyword,"<include") == 0)
726 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
728 (void) CopyMagickString(keyword,token,MaxTextExtent);
729 GetMagickToken(q,&q,token);
732 GetMagickToken(q,&q,token);
733 if (LocaleCompare(keyword,"file") == 0)
736 (void) ThrowMagickException(exception,GetMagickModule(),
737 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
744 GetPathComponent(filename,HeadPath,path);
746 (void) ConcatenateMagickString(path,DirectorySeparator,
748 if (*token == *DirectorySeparator)
749 (void) CopyMagickString(path,token,MaxTextExtent);
751 (void) ConcatenateMagickString(path,token,MaxTextExtent);
752 xml=FileToString(path,~0,exception);
753 if (xml != (char *) NULL)
755 status=LoadMagicList(xml,path,depth+1,exception);
756 xml=(char *) RelinquishMagickMemory(xml);
763 if (LocaleCompare(keyword,"<magic") == 0)
768 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
769 if (magic_info == (MagicInfo *) NULL)
770 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
771 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
772 magic_info->path=ConstantString(filename);
773 magic_info->exempt=MagickFalse;
774 magic_info->signature=MagickSignature;
777 if (magic_info == (MagicInfo *) NULL)
779 if (LocaleCompare(keyword,"/>") == 0)
781 status=AppendValueToLinkedList(magic_list,magic_info);
782 if (status == MagickFalse)
783 (void) ThrowMagickException(exception,GetMagickModule(),
784 ResourceLimitError,"MemoryAllocationFailed","`%s'",
786 magic_info=(MagicInfo *) NULL;
788 GetMagickToken(q,(const char **) NULL,token);
791 GetMagickToken(q,&q,token);
792 GetMagickToken(q,&q,token);
798 if (LocaleCompare((char *) keyword,"name") == 0)
800 magic_info->name=ConstantString(token);
808 if (LocaleCompare((char *) keyword,"offset") == 0)
810 magic_info->offset=(MagickOffsetType) StringToLong(token);
818 if (LocaleCompare((char *) keyword,"stealth") == 0)
820 magic_info->stealth=IsMagickTrue(token);
828 if (LocaleCompare((char *) keyword,"target") == 0)
833 register unsigned char
839 length=strlen(token);
840 magic_info->target=ConstantString(token);
841 magic_info->magic=(unsigned char *) ConstantString(token);
843 for (p=magic_info->target; *p != '\0'; )
848 if (isdigit((int) ((unsigned char) *p)) != 0)
853 *q++=(unsigned char) strtol(p,&end,8);
855 magic_info->length++;
860 case 'b': *q='\b'; break;
861 case 'f': *q='\f'; break;
862 case 'n': *q='\n'; break;
863 case 'r': *q='\r'; break;
864 case 't': *q='\t'; break;
865 case 'v': *q='\v'; break;
866 case 'a': *q='a'; break;
867 case '?': *q='\?'; break;
868 default: *q=(unsigned char) (*p); break;
872 magic_info->length++;
876 if (LocaleNCompare(p,"&",5) == 0)
877 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
878 *q++=(unsigned char) (*p++);
879 magic_info->length++;
889 token=(char *) RelinquishMagickMemory(token);
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % L o a d M a g i c L i s t s %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % LoadMagicLists() loads one or more magic configuration file which provides a
905 % mapping between magic attributes and a magic name.
907 % The format of the LoadMagicLists method is:
909 % MagickBooleanType LoadMagicLists(const char *filename,
910 % ExceptionInfo *exception)
912 % A description of each parameter follows:
914 % o filename: the font file name.
916 % o exception: return any errors or warnings in this structure.
919 static MagickBooleanType LoadMagicLists(const char *filename,
920 ExceptionInfo *exception)
938 Load built-in magic map.
941 if (magic_list == (LinkedListInfo *) NULL)
943 magic_list=NewLinkedList(0);
944 if (magic_list == (LinkedListInfo *) NULL)
946 ThrowFileException(exception,ResourceLimitError,
947 "MemoryAllocationFailed",filename);
951 for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
956 register const MagicMapInfo
960 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
961 if (magic_info == (MagicInfo *) NULL)
963 (void) ThrowMagickException(exception,GetMagickModule(),
964 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
967 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
968 magic_info->path=(char *) "[built-in]";
969 magic_info->name=(char *) p->name;
970 magic_info->offset=p->offset;
971 magic_info->target=(char *) p->magic;
972 magic_info->magic=(unsigned char *) p->magic;
973 magic_info->length=p->length;
974 magic_info->exempt=MagickTrue;
975 magic_info->signature=MagickSignature;
976 status=AppendValueToLinkedList(magic_list,magic_info);
977 if (status == MagickFalse)
978 (void) ThrowMagickException(exception,GetMagickModule(),
979 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
982 Load external magic map.
985 options=GetConfigureOptions(filename,exception);
986 option=(const StringInfo *) GetNextValueInLinkedList(options);
987 while (option != (const StringInfo *) NULL)
989 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
990 status|=LoadMagicList((const char *) GetStringInfoDatum(option),
991 GetStringInfoPath(option),0,exception);
992 option=(const StringInfo *) GetNextValueInLinkedList(options);
994 options=DestroyConfigureOptions(options);
995 return(status != 0 ? MagickTrue : MagickFalse);
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003 + M a g i c C o m p o n e n t G e n e s i s %
1007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 % MagicComponentGenesis() instantiates the magic component.
1011 % The format of the MagicComponentGenesis method is:
1013 % MagickBooleanType MagicComponentGenesis(void)
1016 MagickExport MagickBooleanType MagicComponentGenesis(void)
1018 AcquireSemaphoreInfo(&magic_semaphore);
1023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 + M a g i c C o m p o n e n t T e r m i n u s %
1031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033 % MagicComponentTerminus() destroys the magic component.
1035 % The format of the MagicComponentTerminus method is:
1037 % MagicComponentTerminus(void)
1041 static void *DestroyMagicElement(void *magic_info)
1046 p=(MagicInfo *) magic_info;
1047 if (p->exempt == MagickFalse)
1049 if (p->path != (char *) NULL)
1050 p->path=DestroyString(p->path);
1051 if (p->name != (char *) NULL)
1052 p->name=DestroyString(p->name);
1053 if (p->target != (char *) NULL)
1054 p->target=DestroyString(p->target);
1055 if (p->magic != (unsigned char *) NULL)
1056 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1058 p=(MagicInfo *) RelinquishMagickMemory(p);
1059 return((void *) NULL);
1062 MagickExport void MagicComponentTerminus(void)
1064 if (magic_semaphore == (SemaphoreInfo *) NULL)
1065 AcquireSemaphoreInfo(&magic_semaphore);
1066 LockSemaphoreInfo(magic_semaphore);
1067 if (magic_list != (LinkedListInfo *) NULL)
1068 magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
1069 instantiate_magic=MagickFalse;
1070 UnlockSemaphoreInfo(magic_semaphore);
1071 DestroySemaphoreInfo(&magic_semaphore);