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-2012 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 { "BMP", 0, MagickString("PI") },
100 { "CALS", 21, MagickString("version: MIL-STD-1840") },
101 { "CALS", 0, MagickString("srcdocid:") },
102 { "CALS", 9, MagickString("srcdocid:") },
103 { "CALS", 8, MagickString("rorient:") },
104 { "CGM", 0, MagickString("BEGMF") },
105 { "CIN", 0, MagickString("\200\052\137\327") },
106 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
107 { "DCM", 128, MagickString("DICM") },
108 { "DCX", 0, MagickString("\261\150\336\72") },
109 { "DIB", 0, MagickString("\050\000") },
110 { "DDS", 0, MagickString("DDS ") },
111 { "DJVU", 0, MagickString("AT&TFORM") },
112 { "DOT", 0, MagickString("digraph") },
113 { "DPX", 0, MagickString("SDPX") },
114 { "DPX", 0, MagickString("XPDS") },
115 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
116 { "EPT", 0, MagickString("\305\320\323\306") },
117 { "EXR", 0, MagickString("\166\057\061\001") },
118 { "FAX", 0, MagickString("DFAX") },
119 { "FIG", 0, MagickString("#FIG") },
120 { "FITS", 0, MagickString("IT0") },
121 { "FITS", 0, MagickString("SIMPLE") },
122 { "FPX", 0, MagickString("\320\317\021\340") },
123 { "GIF", 0, MagickString("GIF8") },
124 { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
125 { "HDF", 1, MagickString("HDF") },
126 { "HDR", 0, MagickString("#?RADIANCE") },
127 { "HDR", 0, MagickString("#?RGBE") },
128 { "HPGL", 0, MagickString("IN;") },
129 { "HTML", 1, MagickString("HTML") },
130 { "HTML", 1, MagickString("html") },
131 { "ILBM", 8, MagickString("ILBM") },
132 { "IPTCWTEXT", 0, MagickString("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
133 { "IPTCTEXT", 0, MagickString("2#0=\042�\042") },
134 { "IPTC", 0, MagickString("\034\002") },
135 { "JNG", 0, MagickString("\213JNG\r\n\032\n") },
136 { "JPEG", 0, MagickString("\377\330\377") },
137 { "JPC", 0, MagickString("\377\117") },
138 { "JP2", 4, MagickString("\152\120\040\040\015") },
139 { "MAT", 0, MagickString("MATLAB 5.0 MAT-file,") },
140 { "MIFF", 0, MagickString("Id=ImageMagick") },
141 { "MIFF", 0, MagickString("id=ImageMagick") },
142 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
143 { "MPC", 0, MagickString("id=MagickCache") },
144 { "MPEG", 0, MagickString("\000\000\001\263") },
145 { "MRW", 0, MagickString("\x00MRM") },
146 { "MVG", 0, MagickString("push graphic-context") },
147 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
148 { "PCD", 2048, MagickString("PCD_") },
149 { "PCL", 0, MagickString("\033E\033") },
150 { "PCX", 0, MagickString("\012\002") },
151 { "PCX", 0, MagickString("\012\005") },
152 { "PDB", 60, MagickString("vIMGView") },
153 { "PDF", 0, MagickString("%PDF-") },
154 { "PES", 0, MagickString("#PES") },
155 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
156 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
157 { "PGX", 0, MagickString("\050\107\020\115\046") },
158 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
159 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
160 { "PBM", 0, MagickString("P1") },
161 { "PGM", 0, MagickString("P2") },
162 { "PPM", 0, MagickString("P3") },
163 { "PBM", 0, MagickString("P4") },
164 { "PGM", 0, MagickString("P5") },
165 { "PPM", 0, MagickString("P6") },
166 { "PAM", 0, MagickString("P7") },
167 { "PFM", 0, MagickString("PF") },
168 { "PFM", 0, MagickString("Pf") },
169 { "PS", 0, MagickString("%!") },
170 { "PS", 0, MagickString("\004%!") },
171 { "PS", 0, MagickString("\305\320\323\306") },
172 { "PSB", 0, MagickString("8BPB") },
173 { "PSD", 0, MagickString("8BPS") },
174 { "PWP", 0, MagickString("SFW95") },
175 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
176 { "RLE", 0, MagickString("\122\314") },
177 { "SCT", 0, MagickString("CT") },
178 { "SFW", 0, MagickString("SFW94") },
179 { "SGI", 0, MagickString("\001\332") },
180 { "SUN", 0, MagickString("\131\246\152\225") },
181 { "SVG", 1, MagickString("?XML") },
182 { "SVG", 1, MagickString("?xml") },
183 { "TIFF", 0, MagickString("\115\115\000\052") },
184 { "TIFF", 0, MagickString("\111\111\052\000") },
185 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
186 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
187 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
188 { "VICAR", 0, MagickString("LBLSIZE") },
189 { "VICAR", 0, MagickString("NJPL1I") },
190 { "VIFF", 0, MagickString("\253\001") },
191 { "WEBP", 8, MagickString("WEBP") },
192 { "WMF", 0, MagickString("\327\315\306\232") },
193 { "WMF", 0, MagickString("\001\000\011\000") },
194 { "WPG", 0, MagickString("\377WPC") },
195 { "XBM", 0, MagickString("#define") },
196 { "XCF", 0, MagickString("gimp xcf") },
197 { "XEF", 0, MagickString("FOVb") },
198 { "XPM", 1, MagickString("* XPM *") },
199 { "XWD", 4, MagickString("\007\000\000") },
200 { "XWD", 5, MagickString("\000\000\007") }
203 static LinkedListInfo
204 *magic_list = (LinkedListInfo *) NULL;
207 *magic_semaphore = (SemaphoreInfo *) NULL;
209 static volatile MagickBooleanType
210 instantiate_magic = MagickFalse;
213 Forward declarations.
215 static MagickBooleanType
216 InitializeMagicList(ExceptionInfo *),
217 LoadMagicLists(const char *,ExceptionInfo *);
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 + G e t M a g i c I n f o %
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 % GetMagicInfo() searches the magic list for the specified name and if found
231 % returns attributes for that magic.
233 % The format of the GetMagicInfo method is:
235 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
236 % const size_t length,ExceptionInfo *exception)
238 % A description of each parameter follows:
240 % o magic: A binary string generally representing the first few characters
241 % of the image file or blob.
243 % o length: the length of the binary signature.
245 % o exception: return any errors or warnings in this structure.
248 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
249 const size_t length,ExceptionInfo *exception)
251 register const MagicInfo
254 assert(exception != (ExceptionInfo *) NULL);
255 if ((magic_list == (LinkedListInfo *) NULL) ||
256 (instantiate_magic == MagickFalse))
257 if (InitializeMagicList(exception) == MagickFalse)
258 return((const MagicInfo *) NULL);
259 if ((magic_list == (LinkedListInfo *) NULL) ||
260 (IsLinkedListEmpty(magic_list) != MagickFalse))
261 return((const MagicInfo *) NULL);
262 if (magic == (const unsigned char *) NULL)
263 return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
265 return((const MagicInfo *) NULL);
267 Search for magic tag.
269 LockSemaphoreInfo(magic_semaphore);
270 ResetLinkedListIterator(magic_list);
271 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
272 while (p != (const MagicInfo *) NULL)
274 assert(p->offset >= 0);
275 if (((size_t) (p->offset+p->length) <= length) &&
276 (memcmp(magic+p->offset,p->magic,p->length) == 0))
278 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
280 if (p != (const MagicInfo *) NULL)
281 (void) InsertValueInLinkedList(magic_list,0,
282 RemoveElementByValueFromLinkedList(magic_list,p));
283 UnlockSemaphoreInfo(magic_semaphore);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % G e t M a g i c I n f o L i s t %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % GetMagicInfoList() returns any image aliases that match the specified
301 % The magic of the GetMagicInfoList function is:
303 % const MagicInfo **GetMagicInfoList(const char *pattern,
304 % size_t *number_aliases,ExceptionInfo *exception)
306 % A description of each parameter follows:
308 % o pattern: Specifies a pointer to a text string containing a pattern.
310 % o number_aliases: This integer returns the number of aliases in the list.
312 % o exception: return any errors or warnings in this structure.
316 #if defined(__cplusplus) || defined(c_plusplus)
320 static int MagicInfoCompare(const void *x,const void *y)
326 p=(const MagicInfo **) x,
327 q=(const MagicInfo **) y;
328 if (LocaleCompare((*p)->path,(*q)->path) == 0)
329 return(LocaleCompare((*p)->name,(*q)->name));
330 return(LocaleCompare((*p)->path,(*q)->path));
333 #if defined(__cplusplus) || defined(c_plusplus)
337 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
338 size_t *number_aliases,ExceptionInfo *exception)
343 register const MagicInfo
352 assert(pattern != (char *) NULL);
353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
354 assert(number_aliases != (size_t *) NULL);
356 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
357 if (p == (const MagicInfo *) NULL)
358 return((const MagicInfo **) NULL);
359 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
360 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
361 if (aliases == (const MagicInfo **) NULL)
362 return((const MagicInfo **) NULL);
366 LockSemaphoreInfo(magic_semaphore);
367 ResetLinkedListIterator(magic_list);
368 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
369 for (i=0; p != (const MagicInfo *) NULL; )
371 if ((p->stealth == MagickFalse) &&
372 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
374 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
376 UnlockSemaphoreInfo(magic_semaphore);
377 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
378 aliases[i]=(MagicInfo *) NULL;
379 *number_aliases=(size_t) i;
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % G e t M a g i c L i s t %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % GetMagicList() returns any image format aliases that match the specified
397 % The format of the GetMagicList function is:
399 % char **GetMagicList(const char *pattern,size_t *number_aliases,
400 % 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 image format aliases
409 % o exception: return any errors or warnings in this structure.
413 #if defined(__cplusplus) || defined(c_plusplus)
417 static int MagicCompare(const void *x,const void *y)
425 return(LocaleCompare(p,q));
428 #if defined(__cplusplus) || defined(c_plusplus)
432 MagickExport char **GetMagicList(const char *pattern,
433 size_t *number_aliases,ExceptionInfo *exception)
438 register const MagicInfo
445 Allocate configure list.
447 assert(pattern != (char *) NULL);
448 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
449 assert(number_aliases != (size_t *) NULL);
451 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
452 if (p == (const MagicInfo *) NULL)
453 return((char **) NULL);
454 aliases=(char **) AcquireQuantumMemory((size_t)
455 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
456 if (aliases == (char **) NULL)
457 return((char **) NULL);
458 LockSemaphoreInfo(magic_semaphore);
459 ResetLinkedListIterator(magic_list);
460 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
461 for (i=0; p != (const MagicInfo *) NULL; )
463 if ((p->stealth == MagickFalse) &&
464 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
465 aliases[i++]=ConstantString(p->name);
466 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
468 UnlockSemaphoreInfo(magic_semaphore);
469 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
470 aliases[i]=(char *) NULL;
471 *number_aliases=(size_t) i;
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % G e t M a g i c N a m e %
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 % GetMagicName() returns the name associated with the magic.
488 % The format of the GetMagicName method is:
490 % const char *GetMagicName(const MagicInfo *magic_info)
492 % A description of each parameter follows:
494 % o magic_info: The magic info.
497 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
500 assert(magic_info != (MagicInfo *) NULL);
501 assert(magic_info->signature == MagickSignature);
502 return(magic_info->name);
506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510 + I n i t i a l i z e M a g i c L i s t %
514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 % InitializeMagicList() initializes the magic list.
518 % The format of the InitializeMagicList method is:
520 % MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
522 % A description of each parameter follows.
524 % o exception: return any errors or warnings in this structure.
527 static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
529 if ((magic_list == (LinkedListInfo *) NULL) &&
530 (instantiate_magic == MagickFalse))
532 if (magic_semaphore == (SemaphoreInfo *) NULL)
533 AcquireSemaphoreInfo(&magic_semaphore);
534 LockSemaphoreInfo(magic_semaphore);
535 if ((magic_list == (LinkedListInfo *) NULL) &&
536 (instantiate_magic == MagickFalse))
538 (void) LoadMagicLists(MagicFilename,exception);
539 instantiate_magic=MagickTrue;
541 UnlockSemaphoreInfo(magic_semaphore);
543 return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 % L i s t M a g i c I n f o %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % ListMagicInfo() lists the magic info to a file.
559 % The format of the ListMagicInfo method is:
561 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
563 % A description of each parameter follows.
565 % o file: An pointer to a FILE.
567 % o exception: return any errors or warnings in this structure.
570 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
571 ExceptionInfo *exception)
588 if (file == (const FILE *) NULL)
590 magic_info=GetMagicInfoList("*",&number_aliases,exception);
591 if (magic_info == (const MagicInfo **) NULL)
594 path=(const char *) NULL;
595 for (i=0; i < (ssize_t) number_aliases; i++)
597 if (magic_info[i]->stealth != MagickFalse)
599 if ((path == (const char *) NULL) ||
600 (LocaleCompare(path,magic_info[i]->path) != 0))
602 if (magic_info[i]->path != (char *) NULL)
603 (void) FormatLocaleFile(file,"\nPath: %s\n\n",magic_info[i]->path);
604 (void) FormatLocaleFile(file,"Name Offset Target\n");
605 (void) FormatLocaleFile(file,
606 "-------------------------------------------------"
607 "------------------------------\n");
609 path=magic_info[i]->path;
610 (void) FormatLocaleFile(file,"%s",magic_info[i]->name);
611 for (j=(ssize_t) strlen(magic_info[i]->name); j <= 9; j++)
612 (void) FormatLocaleFile(file," ");
613 (void) FormatLocaleFile(file,"%6ld ",(long) magic_info[i]->offset);
614 if (magic_info[i]->target != (char *) NULL)
619 for (j=0; magic_info[i]->target[j] != '\0'; j++)
620 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
621 (void) FormatLocaleFile(file,"%c",magic_info[i]->target[j]);
623 (void) FormatLocaleFile(file,"\\%03o",(unsigned int)
624 ((unsigned char) magic_info[i]->target[j]));
626 (void) FormatLocaleFile(file,"\n");
629 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 + L o a d M a g i c L i s t %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % LoadMagicList() loads the magic configuration file which provides a mapping
645 % between magic attributes and a magic name.
647 % The format of the LoadMagicList method is:
649 % MagickBooleanType LoadMagicList(const char *xml,const char *filename,
650 % const size_t depth,ExceptionInfo *exception)
652 % A description of each parameter follows:
654 % o xml: The magic list in XML format.
656 % o filename: The magic list filename.
658 % o depth: depth of <include /> statements.
660 % o exception: return any errors or warnings in this structure.
663 static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
664 const size_t depth,ExceptionInfo *exception)
667 keyword[MaxTextExtent],
680 Load the magic map file.
682 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
683 "Loading magic configure file \"%s\" ...",filename);
684 if (xml == (char *) NULL)
686 if (magic_list == (LinkedListInfo *) NULL)
688 magic_list=NewLinkedList(0);
689 if (magic_list == (LinkedListInfo *) NULL)
691 ThrowFileException(exception,ResourceLimitError,
692 "MemoryAllocationFailed",filename);
697 magic_info=(MagicInfo *) NULL;
698 token=AcquireString(xml);
699 for (q=(char *) xml; *q != '\0'; )
704 GetMagickToken(q,&q,token);
707 (void) CopyMagickString(keyword,token,MaxTextExtent);
708 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
713 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
714 GetMagickToken(q,&q,token);
717 if (LocaleNCompare(keyword,"<!--",4) == 0)
722 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
723 GetMagickToken(q,&q,token);
726 if (LocaleCompare(keyword,"<include") == 0)
731 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
733 (void) CopyMagickString(keyword,token,MaxTextExtent);
734 GetMagickToken(q,&q,token);
737 GetMagickToken(q,&q,token);
738 if (LocaleCompare(keyword,"file") == 0)
741 (void) ThrowMagickException(exception,GetMagickModule(),
742 ConfigureError,"IncludeElementNestedTooDeeply","'%s'",token);
749 GetPathComponent(filename,HeadPath,path);
751 (void) ConcatenateMagickString(path,DirectorySeparator,
753 if (*token == *DirectorySeparator)
754 (void) CopyMagickString(path,token,MaxTextExtent);
756 (void) ConcatenateMagickString(path,token,MaxTextExtent);
757 xml=FileToString(path,~0,exception);
758 if (xml != (char *) NULL)
760 status=LoadMagicList(xml,path,depth+1,exception);
761 xml=(char *) RelinquishMagickMemory(xml);
768 if (LocaleCompare(keyword,"<magic") == 0)
773 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
774 if (magic_info == (MagicInfo *) NULL)
775 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
776 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
777 magic_info->path=ConstantString(filename);
778 magic_info->exempt=MagickFalse;
779 magic_info->signature=MagickSignature;
782 if (magic_info == (MagicInfo *) NULL)
784 if (LocaleCompare(keyword,"/>") == 0)
786 status=AppendValueToLinkedList(magic_list,magic_info);
787 if (status == MagickFalse)
788 (void) ThrowMagickException(exception,GetMagickModule(),
789 ResourceLimitError,"MemoryAllocationFailed","'%s'",
791 magic_info=(MagicInfo *) NULL;
793 GetMagickToken(q,(const char **) NULL,token);
796 GetMagickToken(q,&q,token);
797 GetMagickToken(q,&q,token);
803 if (LocaleCompare((char *) keyword,"name") == 0)
805 magic_info->name=ConstantString(token);
813 if (LocaleCompare((char *) keyword,"offset") == 0)
815 magic_info->offset=(MagickOffsetType) StringToLong(token);
823 if (LocaleCompare((char *) keyword,"stealth") == 0)
825 magic_info->stealth=IsStringTrue(token);
833 if (LocaleCompare((char *) keyword,"target") == 0)
838 register unsigned char
844 length=strlen(token);
845 magic_info->target=ConstantString(token);
846 magic_info->magic=(unsigned char *) ConstantString(token);
848 for (p=magic_info->target; *p != '\0'; )
853 if (isdigit((int) ((unsigned char) *p)) != 0)
858 *q++=(unsigned char) strtol(p,&end,8);
860 magic_info->length++;
865 case 'b': *q='\b'; break;
866 case 'f': *q='\f'; break;
867 case 'n': *q='\n'; break;
868 case 'r': *q='\r'; break;
869 case 't': *q='\t'; break;
870 case 'v': *q='\v'; break;
871 case 'a': *q='a'; break;
872 case '?': *q='\?'; break;
873 default: *q=(unsigned char) (*p); break;
877 magic_info->length++;
881 if (LocaleNCompare(p,"&",5) == 0)
882 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
883 *q++=(unsigned char) (*p++);
884 magic_info->length++;
894 token=(char *) RelinquishMagickMemory(token);
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 % L o a d M a g i c L i s t s %
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 % LoadMagicLists() loads one or more magic configuration file which provides a
910 % mapping between magic attributes and a magic name.
912 % The format of the LoadMagicLists method is:
914 % MagickBooleanType LoadMagicLists(const char *filename,
915 % ExceptionInfo *exception)
917 % A description of each parameter follows:
919 % o filename: the font file name.
921 % o exception: return any errors or warnings in this structure.
924 static MagickBooleanType LoadMagicLists(const char *filename,
925 ExceptionInfo *exception)
943 Load built-in magic map.
946 if (magic_list == (LinkedListInfo *) NULL)
948 magic_list=NewLinkedList(0);
949 if (magic_list == (LinkedListInfo *) NULL)
951 ThrowFileException(exception,ResourceLimitError,
952 "MemoryAllocationFailed",filename);
956 for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
961 register const MagicMapInfo
965 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
966 if (magic_info == (MagicInfo *) NULL)
968 (void) ThrowMagickException(exception,GetMagickModule(),
969 ResourceLimitError,"MemoryAllocationFailed","'%s'",magic_info->name);
972 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
973 magic_info->path=(char *) "[built-in]";
974 magic_info->name=(char *) p->name;
975 magic_info->offset=p->offset;
976 magic_info->target=(char *) p->magic;
977 magic_info->magic=(unsigned char *) p->magic;
978 magic_info->length=p->length;
979 magic_info->exempt=MagickTrue;
980 magic_info->signature=MagickSignature;
981 status=AppendValueToLinkedList(magic_list,magic_info);
982 if (status == MagickFalse)
983 (void) ThrowMagickException(exception,GetMagickModule(),
984 ResourceLimitError,"MemoryAllocationFailed","'%s'",magic_info->name);
987 Load external magic map.
990 options=GetConfigureOptions(filename,exception);
991 option=(const StringInfo *) GetNextValueInLinkedList(options);
992 while (option != (const StringInfo *) NULL)
994 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
995 status|=LoadMagicList((const char *) GetStringInfoDatum(option),
996 GetStringInfoPath(option),0,exception);
997 option=(const StringInfo *) GetNextValueInLinkedList(options);
999 options=DestroyConfigureOptions(options);
1000 return(status != 0 ? MagickTrue : MagickFalse);
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1008 + M a g i c C o m p o n e n t G e n e s i s %
1012 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 % MagicComponentGenesis() instantiates the magic component.
1016 % The format of the MagicComponentGenesis method is:
1018 % MagickBooleanType MagicComponentGenesis(void)
1021 MagickPrivate MagickBooleanType MagicComponentGenesis(void)
1023 AcquireSemaphoreInfo(&magic_semaphore);
1028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032 + M a g i c C o m p o n e n t T e r m i n u s %
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038 % MagicComponentTerminus() destroys the magic component.
1040 % The format of the MagicComponentTerminus method is:
1042 % MagicComponentTerminus(void)
1046 static void *DestroyMagicElement(void *magic_info)
1051 p=(MagicInfo *) magic_info;
1052 if (p->exempt == MagickFalse)
1054 if (p->path != (char *) NULL)
1055 p->path=DestroyString(p->path);
1056 if (p->name != (char *) NULL)
1057 p->name=DestroyString(p->name);
1058 if (p->target != (char *) NULL)
1059 p->target=DestroyString(p->target);
1060 if (p->magic != (unsigned char *) NULL)
1061 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1063 p=(MagicInfo *) RelinquishMagickMemory(p);
1064 return((void *) NULL);
1067 MagickPrivate void MagicComponentTerminus(void)
1069 if (magic_semaphore == (SemaphoreInfo *) NULL)
1070 AcquireSemaphoreInfo(&magic_semaphore);
1071 LockSemaphoreInfo(magic_semaphore);
1072 if (magic_list != (LinkedListInfo *) NULL)
1073 magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
1074 instantiate_magic=MagickFalse;
1075 UnlockSemaphoreInfo(magic_semaphore);
1076 DestroySemaphoreInfo(&magic_semaphore);