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-2009 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/token.h"
53 #include "magick/utility.h"
54 #include "magick/xml-tree.h"
59 #define MagicFilename "magic.xml"
60 #define MagickString(magic) (const unsigned char *) (magic), sizeof(magic)-1
65 typedef struct _MagicMapInfo
70 const MagickOffsetType
83 static const MagicMapInfo
86 { "AVI", 0, MagickString("RIFF") },
87 { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
88 { "8BIMTEXT", 0, MagickString("8BIM#") },
89 { "8BIM", 0, MagickString("8BIM") },
90 { "BMP", 0, MagickString("BA") },
91 { "BMP", 0, MagickString("BM") },
92 { "BMP", 0, MagickString("CI") },
93 { "BMP", 0, MagickString("CP") },
94 { "BMP", 0, MagickString("IC") },
95 { "BMP", 0, MagickString("PI") },
96 { "CALS", 21, MagickString("version: MIL-STD-1840") },
97 { "CALS", 0, MagickString("srcdocid:") },
98 { "CALS", 9, MagickString("srcdocid:") },
99 { "CALS", 8, MagickString("rorient:") },
100 { "CGM", 0, MagickString("BEGMF") },
101 { "CIN", 0, MagickString("\200\052\137\327") },
102 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
103 { "DCM", 128, MagickString("DICM") },
104 { "DCX", 0, MagickString("\261\150\336\72") },
105 { "DIB", 0, MagickString("\050\000") },
106 { "DDS", 0, MagickString("DDS ") },
107 { "DJVU", 0, MagickString("AT&TFORM") },
108 { "DOT", 0, MagickString("digraph") },
109 { "DPX", 0, MagickString("SDPX") },
110 { "DPX", 0, MagickString("XPDS") },
111 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
112 { "EPT", 0, MagickString("\305\320\323\306") },
113 { "EXR", 0, MagickString("\166\057\061\001") },
114 { "FAX", 0, MagickString("DFAX") },
115 { "FIG", 0, MagickString("#FIG") },
116 { "FITS", 0, MagickString("IT0") },
117 { "FITS", 0, MagickString("SIMPLE") },
118 { "FPX", 0, MagickString("\320\317\021\340") },
119 { "GIF", 0, MagickString("GIF8") },
120 { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
121 { "HDF", 1, MagickString("HDF") },
122 { "HPGL", 0, MagickString("IN;") },
123 { "HPGL", 0, MagickString("\033E\033") },
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 { "MIFF", 0, MagickString("Id=ImageMagick") },
135 { "MIFF", 0, MagickString("id=ImageMagick") },
136 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
137 { "MPC", 0, MagickString("id=MagickCache") },
138 { "MPEG", 0, MagickString("\000\000\001\263") },
139 { "MRW", 0, MagickString("\x00MRM") },
140 { "MVG", 0, MagickString("push graphic-context") },
141 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
142 { "PCD", 2048, MagickString("PCD_") },
143 { "PCL", 0, MagickString("\033E\033") },
144 { "PCX", 0, MagickString("\012\002") },
145 { "PCX", 0, MagickString("\012\005") },
146 { "PDB", 60, MagickString("vIMGView") },
147 { "PDF", 0, MagickString("%PDF-") },
148 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
149 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
150 { "PGX", 0, MagickString("\050\107\020\115\046") },
151 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
152 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
153 { "PNM", 0, MagickString("P1") },
154 { "PNM", 0, MagickString("P2") },
155 { "PNM", 0, MagickString("P3") },
156 { "PNM", 0, MagickString("P4") },
157 { "PNM", 0, MagickString("P5") },
158 { "PNM", 0, MagickString("P6") },
159 { "PNM", 0, MagickString("P7") },
160 { "PNM", 0, MagickString("PF") },
161 { "PNM", 0, MagickString("Pf") },
162 { "PS", 0, MagickString("%!") },
163 { "PS", 0, MagickString("\004%!") },
164 { "PS", 0, MagickString("\305\320\323\306") },
165 { "PSD", 0, MagickString("8BPS") },
166 { "PWP", 0, MagickString("SFW95") },
167 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
168 { "RAD", 0, MagickString("#?RADIANCE") },
169 { "RAD", 0, MagickString("VIEW= ") },
170 { "RLE", 0, MagickString("\122\314") },
171 { "SCT", 0, MagickString("CT") },
172 { "SFW", 0, MagickString("SFW94") },
173 { "SGI", 0, MagickString("\001\332") },
174 { "SUN", 0, MagickString("\131\246\152\225") },
175 { "SVG", 1, MagickString("?XML") },
176 { "SVG", 1, MagickString("?xml") },
177 { "TIFF", 0, MagickString("\115\115\000\052") },
178 { "TIFF", 0, MagickString("\111\111\052\000") },
179 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
180 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
181 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
182 { "VICAR", 0, MagickString("LBLSIZE") },
183 { "VICAR", 0, MagickString("NJPL1I") },
184 { "VIFF", 0, MagickString("\253\001") },
185 { "WMF", 0, MagickString("\327\315\306\232") },
186 { "WMF", 0, MagickString("\001\000\011\000") },
187 { "WPG", 0, MagickString("\377WPC") },
188 { "XBM", 0, MagickString("#define") },
189 { "XCF", 0, MagickString("gimp xcf") },
190 { "XEF", 0, MagickString("FOVb") },
191 { "XPM", 1, MagickString("* XPM *") },
192 { "XWD", 4, MagickString("\007\000\000") },
193 { "XWD", 5, MagickString("\000\000\007") }
196 static LinkedListInfo
197 *magic_list = (LinkedListInfo *) NULL;
200 *magic_semaphore = (SemaphoreInfo *) NULL;
202 static volatile MagickBooleanType
203 instantiate_magic = MagickFalse;
206 Forward declarations.
208 static MagickBooleanType
209 InitializeMagicList(ExceptionInfo *),
210 LoadMagicLists(const char *,ExceptionInfo *);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 + G e t M a g i c I n f o %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % GetMagicInfo() searches the magic list for the specified name and if found
224 % returns attributes for that magic.
226 % The format of the GetMagicInfo method is:
228 % const MagicInfo *GetMagicInfo(const unsigned char *magic,
229 % const size_t length,ExceptionInfo *exception)
231 % A description of each parameter follows:
233 % o magic: A binary string generally representing the first few characters
234 % of the image file or blob.
236 % o length: the length of the binary signature.
238 % o exception: return any errors or warnings in this structure.
241 MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
242 const size_t length,ExceptionInfo *exception)
244 register const MagicInfo
247 assert(exception != (ExceptionInfo *) NULL);
248 if ((magic_list == (LinkedListInfo *) NULL) ||
249 (instantiate_magic == MagickFalse))
250 if (InitializeMagicList(exception) == MagickFalse)
251 return((const MagicInfo *) NULL);
252 if ((magic_list == (LinkedListInfo *) NULL) ||
253 (IsLinkedListEmpty(magic_list) != MagickFalse))
254 return((const MagicInfo *) NULL);
255 if (magic == (const unsigned char *) NULL)
256 return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
258 return((const MagicInfo *) NULL);
260 Search for magic tag.
262 (void) LockSemaphoreInfo(magic_semaphore);
263 ResetLinkedListIterator(magic_list);
264 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
265 while (p != (const MagicInfo *) NULL)
267 assert(p->offset >= 0);
268 if (((size_t) (p->offset+p->length) <= length) &&
269 (memcmp(magic+p->offset,p->magic,p->length) == 0))
271 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
273 if (p != (const MagicInfo *) NULL)
274 (void) InsertValueInLinkedList(magic_list,0,
275 RemoveElementByValueFromLinkedList(magic_list,p));
276 (void) UnlockSemaphoreInfo(magic_semaphore);
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % G e t M a g i c I n f o L i s t %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % GetMagicInfoList() returns any image aliases that match the specified
294 % The magic of the GetMagicInfoList function is:
296 % const MagicInfo **GetMagicInfoList(const char *pattern,
297 % unsigned long *number_aliases,ExceptionInfo *exception)
299 % A description of each parameter follows:
301 % o pattern: Specifies a pointer to a text string containing a pattern.
303 % o number_aliases: This integer returns the number of aliases in the list.
305 % o exception: return any errors or warnings in this structure.
309 #if defined(__cplusplus) || defined(c_plusplus)
313 static int MagicInfoCompare(const void *x,const void *y)
319 p=(const MagicInfo **) x,
320 q=(const MagicInfo **) y;
321 if (LocaleCompare((*p)->path,(*q)->path) == 0)
322 return(LocaleCompare((*p)->name,(*q)->name));
323 return(LocaleCompare((*p)->path,(*q)->path));
326 #if defined(__cplusplus) || defined(c_plusplus)
330 MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
331 unsigned long *number_aliases,ExceptionInfo *exception)
336 register const MagicInfo
345 assert(pattern != (char *) NULL);
346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
347 assert(number_aliases != (unsigned long *) NULL);
349 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
350 if (p == (const MagicInfo *) NULL)
351 return((const MagicInfo **) NULL);
352 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
353 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
354 if (aliases == (const MagicInfo **) NULL)
355 return((const MagicInfo **) NULL);
359 (void) LockSemaphoreInfo(magic_semaphore);
360 ResetLinkedListIterator(magic_list);
361 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
362 for (i=0; p != (const MagicInfo *) NULL; )
364 if ((p->stealth == MagickFalse) &&
365 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
367 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
369 (void) UnlockSemaphoreInfo(magic_semaphore);
370 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
371 aliases[i]=(MagicInfo *) NULL;
372 *number_aliases=(unsigned long) i;
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % G e t M a g i c L i s t %
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 % GetMagicList() returns any image format aliases that match the specified
390 % The format of the GetMagicList function is:
392 % char **GetMagicList(const char *pattern,unsigned long *number_aliases,
393 % ExceptionInfo *exception)
395 % A description of each parameter follows:
397 % o pattern: Specifies a pointer to a text string containing a pattern.
399 % o number_aliases: This integer returns the number of image format aliases
402 % o exception: return any errors or warnings in this structure.
406 #if defined(__cplusplus) || defined(c_plusplus)
410 static int MagicCompare(const void *x,const void *y)
418 return(LocaleCompare(p,q));
421 #if defined(__cplusplus) || defined(c_plusplus)
425 MagickExport char **GetMagicList(const char *pattern,
426 unsigned long *number_aliases,ExceptionInfo *exception)
431 register const MagicInfo
438 Allocate configure list.
440 assert(pattern != (char *) NULL);
441 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
442 assert(number_aliases != (unsigned long *) NULL);
444 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
445 if (p == (const MagicInfo *) NULL)
446 return((char **) NULL);
447 aliases=(char **) AcquireQuantumMemory((size_t)
448 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
449 if (aliases == (char **) NULL)
450 return((char **) NULL);
451 (void) LockSemaphoreInfo(magic_semaphore);
452 ResetLinkedListIterator(magic_list);
453 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
454 for (i=0; p != (const MagicInfo *) NULL; )
456 if ((p->stealth == MagickFalse) &&
457 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
458 aliases[i++]=ConstantString(p->name);
459 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
461 (void) UnlockSemaphoreInfo(magic_semaphore);
462 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
463 aliases[i]=(char *) NULL;
464 *number_aliases=(unsigned long) i;
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % G e t M a g i c N a m e %
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 % GetMagicName() returns the name associated with the magic.
481 % The format of the GetMagicName method is:
483 % const char *GetMagicName(const MagicInfo *magic_info)
485 % A description of each parameter follows:
487 % o magic_info: The magic info.
490 MagickExport const char *GetMagicName(const MagicInfo *magic_info)
492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
493 assert(magic_info != (MagicInfo *) NULL);
494 assert(magic_info->signature == MagickSignature);
495 return(magic_info->name);
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 + I n i t i a l i z e M a g i c L i s t %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 % InitializeMagicList() initializes the magic list.
511 % The format of the InitializeMagicList method is:
513 % MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
515 % A description of each parameter follows.
517 % o exception: return any errors or warnings in this structure.
520 static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
522 if ((magic_list == (LinkedListInfo *) NULL) &&
523 (instantiate_magic == MagickFalse))
525 if (magic_semaphore == (SemaphoreInfo *) NULL)
526 AcquireSemaphoreInfo(&magic_semaphore);
527 (void) LockSemaphoreInfo(magic_semaphore);
528 if ((magic_list == (LinkedListInfo *) NULL) &&
529 (instantiate_magic == MagickFalse))
531 (void) LoadMagicLists(MagicFilename,exception);
532 instantiate_magic=MagickTrue;
534 (void) UnlockSemaphoreInfo(magic_semaphore);
536 return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 % L i s t M a g i c I n f o %
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % ListMagicInfo() lists the magic info to a file.
552 % The format of the ListMagicInfo method is:
554 % MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
556 % A description of each parameter follows.
558 % o file: An pointer to a FILE.
560 % o exception: return any errors or warnings in this structure.
563 MagickExport MagickBooleanType ListMagicInfo(FILE *file,
564 ExceptionInfo *exception)
581 if (file == (const FILE *) NULL)
583 magic_info=GetMagicInfoList("*",&number_aliases,exception);
584 if (magic_info == (const MagicInfo **) NULL)
587 path=(const char *) NULL;
588 for (i=0; i < (long) number_aliases; i++)
590 if (magic_info[i]->stealth != MagickFalse)
592 if ((path == (const char *) NULL) ||
593 (LocaleCompare(path,magic_info[i]->path) != 0))
595 if (magic_info[i]->path != (char *) NULL)
596 (void) fprintf(file,"\nPath: %s\n\n",magic_info[i]->path);
597 (void) fprintf(file,"Name Offset Target\n");
598 (void) fprintf(file,"-------------------------------------------------"
599 "------------------------------\n");
601 path=magic_info[i]->path;
602 (void) fprintf(file,"%s",magic_info[i]->name);
603 for (j=(long) strlen(magic_info[i]->name); j <= 9; j++)
604 (void) fprintf(file," ");
605 (void) fprintf(file,"%6ld ",(long) magic_info[i]->offset);
606 if (magic_info[i]->target != (char *) NULL)
611 for (j=0; magic_info[i]->target[j] != '\0'; j++)
612 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
613 (void) fprintf(file,"%c",magic_info[i]->target[j]);
615 (void) fprintf(file,"\\%03o",(unsigned int)
616 ((unsigned char) magic_info[i]->target[j]));
618 (void) fprintf(file,"\n");
621 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 + L o a d M a g i c L i s t %
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % LoadMagicList() loads the magic configuration file which provides a mapping
637 % between magic attributes and a magic name.
639 % The format of the LoadMagicList method is:
641 % MagickBooleanType LoadMagicList(const char *xml,const char *filename,
642 % const unsigned long depth,ExceptionInfo *exception)
644 % A description of each parameter follows:
646 % o xml: The magic list in XML format.
648 % o filename: The magic list filename.
650 % o depth: depth of <include /> statements.
652 % o exception: return any errors or warnings in this structure.
655 static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
656 const unsigned long depth,ExceptionInfo *exception)
659 keyword[MaxTextExtent],
672 Load the magic map file.
674 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
675 "Loading magic configure file \"%s\" ...",filename);
676 if (xml == (char *) NULL)
678 if (magic_list == (LinkedListInfo *) NULL)
680 magic_list=NewLinkedList(0);
681 if (magic_list == (LinkedListInfo *) NULL)
683 ThrowFileException(exception,ResourceLimitError,
684 "MemoryAllocationFailed",filename);
689 magic_info=(MagicInfo *) NULL;
690 token=AcquireString(xml);
691 for (q=(char *) xml; *q != '\0'; )
696 GetMagickToken(q,&q,token);
699 (void) CopyMagickString(keyword,token,MaxTextExtent);
700 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
705 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
706 GetMagickToken(q,&q,token);
709 if (LocaleNCompare(keyword,"<!--",4) == 0)
714 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
715 GetMagickToken(q,&q,token);
718 if (LocaleCompare(keyword,"<include") == 0)
723 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
725 (void) CopyMagickString(keyword,token,MaxTextExtent);
726 GetMagickToken(q,&q,token);
729 GetMagickToken(q,&q,token);
730 if (LocaleCompare(keyword,"file") == 0)
733 (void) ThrowMagickException(exception,GetMagickModule(),
734 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
741 GetPathComponent(filename,HeadPath,path);
743 (void) ConcatenateMagickString(path,DirectorySeparator,
745 if (*token == *DirectorySeparator)
746 (void) CopyMagickString(path,token,MaxTextExtent);
748 (void) ConcatenateMagickString(path,token,MaxTextExtent);
749 xml=FileToString(path,~0,exception);
750 if (xml != (char *) NULL)
752 status=LoadMagicList(xml,path,depth+1,exception);
753 xml=(char *) RelinquishMagickMemory(xml);
760 if (LocaleCompare(keyword,"<magic") == 0)
765 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
766 if (magic_info == (MagicInfo *) NULL)
767 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
768 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
769 magic_info->path=ConstantString(filename);
770 magic_info->exempt=MagickFalse;
771 magic_info->signature=MagickSignature;
774 if (magic_info == (MagicInfo *) NULL)
776 if (LocaleCompare(keyword,"/>") == 0)
778 status=AppendValueToLinkedList(magic_list,magic_info);
779 if (status == MagickFalse)
780 (void) ThrowMagickException(exception,GetMagickModule(),
781 ResourceLimitError,"MemoryAllocationFailed","`%s'",
783 magic_info=(MagicInfo *) NULL;
785 GetMagickToken(q,(const char **) NULL,token);
788 GetMagickToken(q,&q,token);
789 GetMagickToken(q,&q,token);
795 if (LocaleCompare((char *) keyword,"name") == 0)
797 magic_info->name=ConstantString(token);
805 if (LocaleCompare((char *) keyword,"offset") == 0)
807 magic_info->offset=(MagickOffsetType) atol(token);
815 if (LocaleCompare((char *) keyword,"stealth") == 0)
817 magic_info->stealth=IsMagickTrue(token);
825 if (LocaleCompare((char *) keyword,"target") == 0)
830 register unsigned char
836 length=strlen(token);
837 magic_info->target=ConstantString(token);
838 magic_info->magic=(unsigned char *) ConstantString(token);
840 for (p=magic_info->target; *p != '\0'; )
845 if (isdigit((int) ((unsigned char) *p)) != 0)
850 *q++=(unsigned char) strtol(p,&end,8);
852 magic_info->length++;
857 case 'b': *q='\b'; break;
858 case 'f': *q='\f'; break;
859 case 'n': *q='\n'; break;
860 case 'r': *q='\r'; break;
861 case 't': *q='\t'; break;
862 case 'v': *q='\v'; break;
863 case 'a': *q='a'; break;
864 case '?': *q='\?'; break;
865 default: *q=(unsigned char) (*p); break;
869 magic_info->length++;
873 if (LocaleNCompare(p,"&",5) == 0)
874 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
875 *q++=(unsigned char) (*p++);
876 magic_info->length++;
886 token=(char *) RelinquishMagickMemory(token);
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 % L o a d M a g i c L i s t s %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % LoadMagicLists() loads one or more magic configuration file which provides a
902 % mapping between magic attributes and a magic name.
904 % The format of the LoadMagicLists method is:
906 % MagickBooleanType LoadMagicLists(const char *filename,
907 % ExceptionInfo *exception)
909 % A description of each parameter follows:
911 % o filename: the font file name.
913 % o exception: return any errors or warnings in this structure.
916 static MagickBooleanType LoadMagicLists(const char *filename,
917 ExceptionInfo *exception)
935 Load built-in magic map.
938 if (magic_list == (LinkedListInfo *) NULL)
940 magic_list=NewLinkedList(0);
941 if (magic_list == (LinkedListInfo *) NULL)
943 ThrowFileException(exception,ResourceLimitError,
944 "MemoryAllocationFailed",filename);
948 for (i=0; i < (long) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
953 register const MagicMapInfo
957 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
958 if (magic_info == (MagicInfo *) NULL)
960 (void) ThrowMagickException(exception,GetMagickModule(),
961 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
964 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
965 magic_info->path=(char *) "[built-in]";
966 magic_info->name=(char *) p->name;
967 magic_info->offset=p->offset;
968 magic_info->target=(char *) p->magic;
969 magic_info->magic=(unsigned char *) p->magic;
970 magic_info->length=p->length;
971 magic_info->exempt=MagickTrue;
972 magic_info->signature=MagickSignature;
973 status=AppendValueToLinkedList(magic_list,magic_info);
974 if (status == MagickFalse)
975 (void) ThrowMagickException(exception,GetMagickModule(),
976 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
979 Load external magic map.
982 options=GetConfigureOptions(filename,exception);
983 option=(const StringInfo *) GetNextValueInLinkedList(options);
984 while (option != (const StringInfo *) NULL)
986 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
987 status|=LoadMagicList((const char *) GetStringInfoDatum(option),
988 GetStringInfoPath(option),0,exception);
989 option=(const StringInfo *) GetNextValueInLinkedList(options);
991 options=DestroyConfigureOptions(options);
992 return(status != 0 ? MagickTrue : MagickFalse);
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 + M a g i c C o m p o n e n t G e n e s i s %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % MagicComponentGenesis() instantiates the magic component.
1008 % The format of the MagicComponentGenesis method is:
1010 % MagickBooleanType MagicComponentGenesis(void)
1013 MagickExport MagickBooleanType MagicComponentGenesis(void)
1015 AcquireSemaphoreInfo(&magic_semaphore);
1020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1024 + M a g i c C o m p o n e n t T e r m i n u s %
1028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030 % MagicComponentTerminus() destroys the magic component.
1032 % The format of the MagicComponentTerminus method is:
1034 % MagicComponentTerminus(void)
1038 static void *DestroyMagicElement(void *magic_info)
1043 p=(MagicInfo *) magic_info;
1044 if (p->exempt == MagickFalse)
1046 if (p->path != (char *) NULL)
1047 p->path=DestroyString(p->path);
1048 if (p->name != (char *) NULL)
1049 p->name=DestroyString(p->name);
1050 if (p->target != (char *) NULL)
1051 p->target=DestroyString(p->target);
1052 if (p->magic != (unsigned char *) NULL)
1053 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
1055 p=(MagicInfo *) RelinquishMagickMemory(p);
1056 return((void *) NULL);
1059 MagickExport void MagicComponentTerminus(void)
1061 if (magic_semaphore == (SemaphoreInfo *) NULL)
1062 AcquireSemaphoreInfo(&magic_semaphore);
1063 (void) LockSemaphoreInfo(magic_semaphore);
1064 if (magic_list != (LinkedListInfo *) NULL)
1065 magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
1066 instantiate_magic=MagickFalse;
1067 (void) UnlockSemaphoreInfo(magic_semaphore);
1068 DestroySemaphoreInfo(&magic_semaphore);