blob: c521ff0b1ccb91425d29c7101e0ea0f4445a3ff4 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% M M AAA GGGG IIIII CCCC %
6% MM MM A A G I C %
7% M M M AAAAA G GGG I C %
8% M M A A G G I C %
9% M M A A GGGG IIIII CCCC %
10% %
11% %
12% MagickCore Image Magic Methods %
13% %
14% Software Design %
15% Bob Friesenhahn %
16% July 2000 %
17% %
18% %
19% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% http://www.imagemagick.org/script/license.php %
26% %
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. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36*/
37
38/*
39 Include declarations.
40*/
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"
55
56/*
57 Define declarations.
58*/
59#define MagicFilename "magic.xml"
60
61/*
62 Static declarations.
63*/
cristy1ca3eb32009-10-15 18:41:54 +000064typedef struct _MagicMapInfo
65{
66 const char
67 *name;
68
cristye3e77a12009-10-16 00:47:21 +000069 const MagickOffsetType
70 offset;
71
cristybcdce3d2009-10-15 20:04:49 +000072 const unsigned char
cristy1ca3eb32009-10-15 18:41:54 +000073 *magic;
74
cristye3e77a12009-10-16 00:47:21 +000075 const size_t
cristy1ca3eb32009-10-15 18:41:54 +000076 length;
cristy1ca3eb32009-10-15 18:41:54 +000077} MagicMapInfo;
78
cristye3e77a12009-10-16 00:47:21 +000079#define MagickString(magic) (const unsigned char *) (magic), sizeof(magic)-1
cristy1ca3eb32009-10-15 18:41:54 +000080
81static const MagicMapInfo
82 MagicMap[] =
83 {
cristye3e77a12009-10-16 00:47:21 +000084 { "AVI", 0, MagickString("RIFF") },
85 { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
86 { "8BIMTEXT", 0, MagickString("8BIM#") },
87 { "8BIM", 0, MagickString("8BIM") },
88 { "BMP", 0, MagickString("BA") },
89 { "BMP", 0, MagickString("BM") },
90 { "BMP", 0, MagickString("CI") },
91 { "BMP", 0, MagickString("CP") },
92 { "BMP", 0, MagickString("IC") },
93 { "BMP", 0, MagickString("PI") },
94 { "CALS", 21, MagickString("version: MIL-STD-1840") },
95 { "CALS", 0, MagickString("srcdocid:") },
96 { "CALS", 9, MagickString("srcdocid:") },
97 { "CALS", 8, MagickString("rorient:") },
98 { "CGM", 0, MagickString("BEGMF") },
99 { "CIN", 0, MagickString("\200\052\137\327") },
100 { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
101 { "DCM", 128, MagickString("DICM") },
102 { "DCX", 0, MagickString("\261\150\336\72") },
103 { "DIB", 0, MagickString("\050\000") },
104 { "DDS", 0, MagickString("DDS ") },
105 { "DJVU", 0, MagickString("AT&TFORM") },
106 { "DOT", 0, MagickString("digraph") },
107 { "DPX", 0, MagickString("SDPX") },
108 { "DPX", 0, MagickString("XPDS") },
109 { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
110 { "EPT", 0, MagickString("\305\320\323\306") },
111 { "EXR", 0, MagickString("\166\057\061\001") },
112 { "FAX", 0, MagickString("DFAX") },
113 { "FIG", 0, MagickString("#FIG") },
114 { "FITS", 0, MagickString("IT0") },
115 { "FITS", 0, MagickString("SIMPLE") },
116 { "FPX", 0, MagickString("\320\317\021\340") },
117 { "GIF", 0, MagickString("GIF8") },
118 { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
119 { "HDF", 1, MagickString("HDF") },
120 { "HPGL", 0, MagickString("IN;") },
121 { "HPGL", 0, MagickString("\033E\033") },
122 { "HTML", 1, MagickString("HTML") },
123 { "HTML", 1, MagickString("html") },
124 { "ILBM", 8, MagickString("ILBM") },
125 { "IPTCWTEXT", 0, MagickString("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
126 { "IPTCTEXT", 0, MagickString("2#0=\042�\042") },
127 { "IPTC", 0, MagickString("\034\002") },
128 { "JNG", 0, MagickString("\213JNG\r\n\032\n") },
129 { "JPEG", 0, MagickString("\377\330\377") },
130 { "JPC", 0, MagickString("\377\117") },
131 { "JP2", 4, MagickString("\152\120\040\040\015") },
132 { "MIFF", 0, MagickString("Id=ImageMagick") },
133 { "MIFF", 0, MagickString("id=ImageMagick") },
134 { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
135 { "MPC", 0, MagickString("id=MagickCache") },
136 { "MPEG", 0, MagickString("\000\000\001\263") },
137 { "MRW", 0, MagickString("\x00MRM") },
138 { "MVG", 0, MagickString("push graphic-context") },
139 { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
140 { "PCD", 2048, MagickString("PCD_") },
141 { "PCL", 0, MagickString("\033E\033") },
142 { "PCX", 0, MagickString("\012\002") },
143 { "PCX", 0, MagickString("\012\005") },
144 { "PDB", 60, MagickString("vIMGView") },
145 { "PDF", 0, MagickString("%PDF-") },
146 { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
147 { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
148 { "PGX", 0, MagickString("\050\107\020\115\046") },
149 { "PICT", 522, MagickString("\000\021\002\377\014\000") },
150 { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
151 { "PNM", 0, MagickString("P1") },
152 { "PNM", 0, MagickString("P2") },
153 { "PNM", 0, MagickString("P3") },
154 { "PNM", 0, MagickString("P4") },
155 { "PNM", 0, MagickString("P5") },
156 { "PNM", 0, MagickString("P6") },
157 { "PNM", 0, MagickString("P7") },
158 { "PNM", 0, MagickString("PF") },
159 { "PNM", 0, MagickString("Pf") },
160 { "PS", 0, MagickString("%!") },
161 { "PS", 0, MagickString("\004%!") },
162 { "PS", 0, MagickString("\305\320\323\306") },
163 { "PSD", 0, MagickString("8BPS") },
164 { "PWP", 0, MagickString("SFW95") },
165 { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
166 { "RAD", 0, MagickString("#?RADIANCE") },
167 { "RAD", 0, MagickString("VIEW= ") },
168 { "RLE", 0, MagickString("\122\314") },
169 { "SCT", 0, MagickString("CT") },
170 { "SFW", 0, MagickString("SFW94") },
171 { "SGI", 0, MagickString("\001\332") },
172 { "SUN", 0, MagickString("\131\246\152\225") },
173 { "SVG", 1, MagickString("?XML") },
174 { "SVG", 1, MagickString("?xml") },
175 { "TIFF", 0, MagickString("\115\115\000\052") },
176 { "TIFF", 0, MagickString("\111\111\052\000") },
177 { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
178 { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
179 { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
180 { "VICAR", 0, MagickString("LBLSIZE") },
181 { "VICAR", 0, MagickString("NJPL1I") },
182 { "VIFF", 0, MagickString("\253\001") },
183 { "WMF", 0, MagickString("\327\315\306\232") },
184 { "WMF", 0, MagickString("\001\000\011\000") },
185 { "WPG", 0, MagickString("\377WPC") },
186 { "XBM", 0, MagickString("#define") },
187 { "XCF", 0, MagickString("gimp xcf") },
188 { "XEF", 0, MagickString("FOVb") },
189 { "XPM", 1, MagickString("* XPM *") },
190 { "XWD", 4, MagickString("\007\000\000") },
191 { "XWD", 5, MagickString("\000\000\007") },
192 { (const char *) NULL, 0, MagickString((const char *) NULL) }
cristy1ca3eb32009-10-15 18:41:54 +0000193 };
cristy3ed852e2009-09-05 21:47:34 +0000194
195static LinkedListInfo
196 *magic_list = (LinkedListInfo *) NULL;
197
198static SemaphoreInfo
199 *magic_semaphore = (SemaphoreInfo *) NULL;
200
201static volatile MagickBooleanType
202 instantiate_magic = MagickFalse;
203
204/*
205 Forward declarations.
206*/
207static MagickBooleanType
208 InitializeMagicList(ExceptionInfo *),
209 LoadMagicLists(const char *,ExceptionInfo *);
210
211/*
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% %
214% %
215% %
216+ D e s t r o y M a g i c L i s t %
217% %
218% %
219% %
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%
222% DestroyMagicList() deallocates memory associated with the magic list.
223%
224% The format of the DestroyMagicList method is:
225%
226% DestroyMagicList(void)
227%
228*/
229
230static void *DestroyMagicElement(void *magic_info)
231{
232 register MagicInfo
233 *p;
234
235 p=(MagicInfo *) magic_info;
cristy1ca3eb32009-10-15 18:41:54 +0000236 if (p->exempt == MagickFalse)
237 {
238 if (p->path != (char *) NULL)
239 p->path=DestroyString(p->path);
240 if (p->name != (char *) NULL)
241 p->name=DestroyString(p->name);
242 if (p->target != (char *) NULL)
243 p->target=DestroyString(p->target);
244 if (p->magic != (unsigned char *) NULL)
245 p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
246 }
cristy3ed852e2009-09-05 21:47:34 +0000247 p=(MagicInfo *) RelinquishMagickMemory(p);
248 return((void *) NULL);
249}
250
251MagickExport void DestroyMagicList(void)
252{
253 AcquireSemaphoreInfo(&magic_semaphore);
254 if (magic_list != (LinkedListInfo *) NULL)
255 magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
256 instantiate_magic=MagickFalse;
257 RelinquishSemaphoreInfo(magic_semaphore);
258 DestroySemaphoreInfo(&magic_semaphore);
259}
260
261/*
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% %
264% %
265% %
266+ G e t M a g i c I n f o %
267% %
268% %
269% %
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%
272% GetMagicInfo() searches the magic list for the specified name and if found
273% returns attributes for that magic.
274%
275% The format of the GetMagicInfo method is:
276%
277% const MagicInfo *GetMagicInfo(const unsigned char *magic,
278% const size_t length,ExceptionInfo *exception)
279%
280% A description of each parameter follows:
281%
282% o magic: A binary string generally representing the first few characters
283% of the image file or blob.
284%
285% o length: the length of the binary signature.
286%
287% o exception: return any errors or warnings in this structure.
288%
289*/
290MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
291 const size_t length,ExceptionInfo *exception)
292{
293 register const MagicInfo
294 *p;
295
296 assert(exception != (ExceptionInfo *) NULL);
297 if ((magic_list == (LinkedListInfo *) NULL) ||
298 (instantiate_magic == MagickFalse))
299 if (InitializeMagicList(exception) == MagickFalse)
300 return((const MagicInfo *) NULL);
301 if ((magic_list == (LinkedListInfo *) NULL) ||
302 (IsLinkedListEmpty(magic_list) != MagickFalse))
303 return((const MagicInfo *) NULL);
304 if (magic == (const unsigned char *) NULL)
305 return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
306 if (length == 0)
307 return((const MagicInfo *) NULL);
308 /*
309 Search for magic tag.
310 */
311 AcquireSemaphoreInfo(&magic_semaphore);
312 ResetLinkedListIterator(magic_list);
313 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
314 while (p != (const MagicInfo *) NULL)
315 {
316 assert(p->offset >= 0);
317 if (((size_t) (p->offset+p->length) <= length) &&
318 (memcmp(magic+p->offset,p->magic,p->length) == 0))
319 break;
320 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
321 }
322 if (p != (const MagicInfo *) NULL)
323 (void) InsertValueInLinkedList(magic_list,0,
324 RemoveElementByValueFromLinkedList(magic_list,p));
325 RelinquishSemaphoreInfo(magic_semaphore);
326 return(p);
327}
328
329/*
330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331% %
332% %
333% %
334% G e t M a g i c I n f o L i s t %
335% %
336% %
337% %
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339%
340% GetMagicInfoList() returns any image aliases that match the specified
341% pattern.
342%
343% The magic of the GetMagicInfoList function is:
344%
345% const MagicInfo **GetMagicInfoList(const char *pattern,
346% unsigned long *number_aliases,ExceptionInfo *exception)
347%
348% A description of each parameter follows:
349%
350% o pattern: Specifies a pointer to a text string containing a pattern.
351%
cristyfc0d1122009-10-17 20:22:37 +0000352% o number_aliases: This integer returns the number of aliases in the list.
cristy3ed852e2009-09-05 21:47:34 +0000353%
354% o exception: return any errors or warnings in this structure.
355%
356*/
357
358#if defined(__cplusplus) || defined(c_plusplus)
359extern "C" {
360#endif
361
362static int MagicInfoCompare(const void *x,const void *y)
363{
364 const MagicInfo
365 **p,
366 **q;
367
368 p=(const MagicInfo **) x,
369 q=(const MagicInfo **) y;
370 if (LocaleCompare((*p)->path,(*q)->path) == 0)
371 return(LocaleCompare((*p)->name,(*q)->name));
372 return(LocaleCompare((*p)->path,(*q)->path));
373}
374
375#if defined(__cplusplus) || defined(c_plusplus)
376}
377#endif
378
379MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
380 unsigned long *number_aliases,ExceptionInfo *exception)
381{
382 const MagicInfo
383 **aliases;
384
385 register const MagicInfo
386 *p;
387
388 register long
389 i;
390
391 /*
392 Allocate magic list.
393 */
394 assert(pattern != (char *) NULL);
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
396 assert(number_aliases != (unsigned long *) NULL);
397 *number_aliases=0;
cristy1ca3eb32009-10-15 18:41:54 +0000398 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000399 if (p == (const MagicInfo *) NULL)
400 return((const MagicInfo **) NULL);
401 aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
402 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
403 if (aliases == (const MagicInfo **) NULL)
404 return((const MagicInfo **) NULL);
405 /*
406 Generate magic list.
407 */
408 AcquireSemaphoreInfo(&magic_semaphore);
409 ResetLinkedListIterator(magic_list);
410 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
411 for (i=0; p != (const MagicInfo *) NULL; )
412 {
413 if ((p->stealth == MagickFalse) &&
414 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
415 aliases[i++]=p;
416 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
417 }
418 RelinquishSemaphoreInfo(magic_semaphore);
419 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
420 aliases[i]=(MagicInfo *) NULL;
421 *number_aliases=(unsigned long) i;
422 return(aliases);
423}
424
425/*
426%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427% %
428% %
429% %
430% G e t M a g i c L i s t %
431% %
432% %
433% %
434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435%
436% GetMagicList() returns any image format aliases that match the specified
437% pattern.
438%
439% The format of the GetMagicList function is:
440%
441% char **GetMagicList(const char *pattern,unsigned long *number_aliases,
442% ExceptionInfo *exception)
443%
444% A description of each parameter follows:
445%
446% o pattern: Specifies a pointer to a text string containing a pattern.
447%
448% o number_aliases: This integer returns the number of image format aliases
449% in the list.
450%
451% o exception: return any errors or warnings in this structure.
452%
453*/
454
455#if defined(__cplusplus) || defined(c_plusplus)
456extern "C" {
457#endif
458
459static int MagicCompare(const void *x,const void *y)
460{
461 register const char
462 *p,
463 *q;
464
465 p=(const char *) x;
466 q=(const char *) y;
467 return(LocaleCompare(p,q));
468}
469
470#if defined(__cplusplus) || defined(c_plusplus)
471}
472#endif
473
474MagickExport char **GetMagicList(const char *pattern,
475 unsigned long *number_aliases,ExceptionInfo *exception)
476{
477 char
478 **aliases;
479
480 register const MagicInfo
481 *p;
482
483 register long
484 i;
485
486 /*
487 Allocate configure list.
488 */
489 assert(pattern != (char *) NULL);
490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
491 assert(number_aliases != (unsigned long *) NULL);
492 *number_aliases=0;
cristy1ca3eb32009-10-15 18:41:54 +0000493 p=GetMagicInfo((const unsigned char *) NULL,0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000494 if (p == (const MagicInfo *) NULL)
495 return((char **) NULL);
496 aliases=(char **) AcquireQuantumMemory((size_t)
497 GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
498 if (aliases == (char **) NULL)
499 return((char **) NULL);
500 AcquireSemaphoreInfo(&magic_semaphore);
501 ResetLinkedListIterator(magic_list);
502 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
503 for (i=0; p != (const MagicInfo *) NULL; )
504 {
505 if ((p->stealth == MagickFalse) &&
506 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
507 aliases[i++]=ConstantString(p->name);
508 p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
509 }
510 RelinquishSemaphoreInfo(magic_semaphore);
511 qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
512 aliases[i]=(char *) NULL;
513 *number_aliases=(unsigned long) i;
514 return(aliases);
515}
516
517/*
518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519% %
520% %
521% %
522% G e t M a g i c N a m e %
523% %
524% %
525% %
526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527%
528% GetMagicName() returns the name associated with the magic.
529%
530% The format of the GetMagicName method is:
531%
532% const char *GetMagicName(const MagicInfo *magic_info)
533%
534% A description of each parameter follows:
535%
536% o magic_info: The magic info.
537%
538*/
539MagickExport const char *GetMagicName(const MagicInfo *magic_info)
540{
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
542 assert(magic_info != (MagicInfo *) NULL);
543 assert(magic_info->signature == MagickSignature);
544 return(magic_info->name);
545}
546
547/*
548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549% %
550% %
551% %
552+ I n i t i a l i z e M a g i c L i s t %
553% %
554% %
555% %
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557%
558% InitializeMagicList() initializes the magic list.
559%
560% The format of the InitializeMagicList method is:
561%
562% MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
563%
564% A description of each parameter follows.
565%
566% o exception: return any errors or warnings in this structure.
567%
568*/
569static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
570{
571 if ((magic_list == (LinkedListInfo *) NULL) &&
572 (instantiate_magic == MagickFalse))
573 {
574 AcquireSemaphoreInfo(&magic_semaphore);
575 if ((magic_list == (LinkedListInfo *) NULL) &&
576 (instantiate_magic == MagickFalse))
577 {
578 (void) LoadMagicLists(MagicFilename,exception);
579 instantiate_magic=MagickTrue;
580 }
581 RelinquishSemaphoreInfo(magic_semaphore);
582 }
583 return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
584}
585
586/*
587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588% %
589% %
590% %
591% L i s t M a g i c I n f o %
592% %
593% %
594% %
595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596%
597% ListMagicInfo() lists the magic info to a file.
598%
599% The format of the ListMagicInfo method is:
600%
601% MagickBooleanType ListMagicInfo(FILE *file,ExceptionInfo *exception)
602%
603% A description of each parameter follows.
604%
605% o file: An pointer to a FILE.
606%
607% o exception: return any errors or warnings in this structure.
608%
609*/
610MagickExport MagickBooleanType ListMagicInfo(FILE *file,
611 ExceptionInfo *exception)
612{
613 const char
614 *path;
615
616 const MagicInfo
617 **magic_info;
618
619 long
620 j;
621
622 register long
623 i;
624
625 unsigned long
626 number_aliases;
627
628 if (file == (const FILE *) NULL)
629 file=stdout;
630 magic_info=GetMagicInfoList("*",&number_aliases,exception);
631 if (magic_info == (const MagicInfo **) NULL)
632 return(MagickFalse);
633 j=0;
634 path=(const char *) NULL;
635 for (i=0; i < (long) number_aliases; i++)
636 {
637 if (magic_info[i]->stealth != MagickFalse)
638 continue;
639 if ((path == (const char *) NULL) ||
640 (LocaleCompare(path,magic_info[i]->path) != 0))
641 {
642 if (magic_info[i]->path != (char *) NULL)
643 (void) fprintf(file,"\nPath: %s\n\n",magic_info[i]->path);
644 (void) fprintf(file,"Name Offset Target\n");
645 (void) fprintf(file,"-------------------------------------------------"
646 "------------------------------\n");
647 }
648 path=magic_info[i]->path;
649 (void) fprintf(file,"%s",magic_info[i]->name);
650 for (j=(long) strlen(magic_info[i]->name); j <= 9; j++)
651 (void) fprintf(file," ");
652 (void) fprintf(file,"%6ld ",(long) magic_info[i]->offset);
653 if (magic_info[i]->target != (char *) NULL)
cristy1ca3eb32009-10-15 18:41:54 +0000654 {
655 register long
656 j;
657
658 for (j=0; magic_info[i]->target[j] != '\0'; j++)
659 if (isprint((int) ((unsigned char) magic_info[i]->target[j])) != 0)
660 (void) fprintf(file,"%c",magic_info[i]->target[j]);
661 else
cristy50931922009-10-15 18:59:59 +0000662 (void) fprintf(file,"\\%03o",(unsigned int)
663 ((unsigned char) magic_info[i]->target[j]));
cristy1ca3eb32009-10-15 18:41:54 +0000664 }
cristy3ed852e2009-09-05 21:47:34 +0000665 (void) fprintf(file,"\n");
666 }
667 (void) fflush(file);
668 magic_info=(const MagicInfo **) RelinquishMagickMemory((void *) magic_info);
669 return(MagickTrue);
670}
671
672/*
673%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674% %
675% %
676% %
677+ L o a d M a g i c L i s t %
678% %
679% %
680% %
681%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682%
683% LoadMagicList() loads the magic configuration file which provides a mapping
684% between magic attributes and a magic name.
685%
686% The format of the LoadMagicList method is:
687%
688% MagickBooleanType LoadMagicList(const char *xml,const char *filename,
689% const unsigned long depth,ExceptionInfo *exception)
690%
691% A description of each parameter follows:
692%
cristy1ca3eb32009-10-15 18:41:54 +0000693% o xml: The magic list in XML format.
cristy3ed852e2009-09-05 21:47:34 +0000694%
cristy1ca3eb32009-10-15 18:41:54 +0000695% o filename: The magic list filename.
cristy3ed852e2009-09-05 21:47:34 +0000696%
697% o depth: depth of <include /> statements.
698%
699% o exception: return any errors or warnings in this structure.
700%
701*/
702static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
703 const unsigned long depth,ExceptionInfo *exception)
704{
705 char
706 keyword[MaxTextExtent],
707 *token;
708
709 const char
710 *q;
711
712 MagickBooleanType
713 status;
714
715 MagicInfo
716 *magic_info;
717
718 /*
719 Load the magic map file.
720 */
721 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
722 "Loading magic configure file \"%s\" ...",filename);
723 if (xml == (char *) NULL)
724 return(MagickFalse);
725 if (magic_list == (LinkedListInfo *) NULL)
cristye3e77a12009-10-16 00:47:21 +0000726 {
727 magic_list=NewLinkedList(0);
728 if (magic_list == (LinkedListInfo *) NULL)
729 {
730 ThrowFileException(exception,ResourceLimitError,
731 "MemoryAllocationFailed",filename);
732 return(MagickFalse);
733 }
734 }
cristy3ed852e2009-09-05 21:47:34 +0000735 status=MagickTrue;
736 magic_info=(MagicInfo *) NULL;
737 token=AcquireString(xml);
738 for (q=(char *) xml; *q != '\0'; )
739 {
740 /*
741 Interpret XML.
742 */
743 GetMagickToken(q,&q,token);
744 if (*token == '\0')
745 break;
746 (void) CopyMagickString(keyword,token,MaxTextExtent);
747 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
748 {
749 /*
750 Doctype element.
751 */
752 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
753 GetMagickToken(q,&q,token);
754 continue;
755 }
756 if (LocaleNCompare(keyword,"<!--",4) == 0)
757 {
758 /*
759 Comment element.
760 */
761 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
762 GetMagickToken(q,&q,token);
763 continue;
764 }
765 if (LocaleCompare(keyword,"<include") == 0)
766 {
767 /*
768 Include element.
769 */
770 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
771 {
772 (void) CopyMagickString(keyword,token,MaxTextExtent);
773 GetMagickToken(q,&q,token);
774 if (*token != '=')
775 continue;
776 GetMagickToken(q,&q,token);
777 if (LocaleCompare(keyword,"file") == 0)
778 {
779 if (depth > 200)
780 (void) ThrowMagickException(exception,GetMagickModule(),
781 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
782 else
783 {
784 char
785 path[MaxTextExtent],
786 *xml;
787
788 GetPathComponent(filename,HeadPath,path);
789 if (*path != '\0')
790 (void) ConcatenateMagickString(path,DirectorySeparator,
791 MaxTextExtent);
792 if (*token == *DirectorySeparator)
793 (void) CopyMagickString(path,token,MaxTextExtent);
794 else
795 (void) ConcatenateMagickString(path,token,MaxTextExtent);
796 xml=FileToString(path,~0,exception);
797 if (xml != (char *) NULL)
798 {
799 status=LoadMagicList(xml,path,depth+1,exception);
800 xml=(char *) RelinquishMagickMemory(xml);
801 }
802 }
803 }
804 }
805 continue;
806 }
807 if (LocaleCompare(keyword,"<magic") == 0)
808 {
809 /*
810 Magic element.
811 */
812 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
813 if (magic_info == (MagicInfo *) NULL)
814 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
815 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
816 magic_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000817 magic_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000818 magic_info->signature=MagickSignature;
819 continue;
820 }
821 if (magic_info == (MagicInfo *) NULL)
822 continue;
823 if (LocaleCompare(keyword,"/>") == 0)
824 {
825 status=AppendValueToLinkedList(magic_list,magic_info);
826 if (status == MagickFalse)
827 (void) ThrowMagickException(exception,GetMagickModule(),
828 ResourceLimitError,"MemoryAllocationFailed","`%s'",
829 magic_info->name);
830 magic_info=(MagicInfo *) NULL;
831 }
832 GetMagickToken(q,(const char **) NULL,token);
833 if (*token != '=')
834 continue;
835 GetMagickToken(q,&q,token);
836 GetMagickToken(q,&q,token);
837 switch (*keyword)
838 {
839 case 'N':
840 case 'n':
841 {
842 if (LocaleCompare((char *) keyword,"name") == 0)
843 {
844 magic_info->name=ConstantString(token);
845 break;
846 }
847 break;
848 }
849 case 'O':
850 case 'o':
851 {
852 if (LocaleCompare((char *) keyword,"offset") == 0)
853 {
854 magic_info->offset=(MagickOffsetType) atol(token);
855 break;
856 }
857 break;
858 }
859 case 'S':
860 case 's':
861 {
862 if (LocaleCompare((char *) keyword,"stealth") == 0)
863 {
864 magic_info->stealth=IsMagickTrue(token);
865 break;
866 }
867 break;
868 }
869 case 'T':
870 case 't':
871 {
872 if (LocaleCompare((char *) keyword,"target") == 0)
873 {
874 char
875 *p;
876
877 register unsigned char
878 *q;
879
880 size_t
881 length;
882
883 length=strlen(token);
884 magic_info->target=ConstantString(token);
885 magic_info->magic=(unsigned char *) ConstantString(token);
886 q=magic_info->magic;
887 for (p=magic_info->target; *p != '\0'; )
888 {
889 if (*p == '\\')
890 {
891 p++;
892 if (isdigit((int) ((unsigned char) *p)) != 0)
893 {
894 char
895 *end;
896
897 *q++=(unsigned char) strtol(p,&end,8);
898 p+=(end-p);
899 magic_info->length++;
900 continue;
901 }
902 switch (*p)
903 {
904 case 'b': *q='\b'; break;
905 case 'f': *q='\f'; break;
906 case 'n': *q='\n'; break;
907 case 'r': *q='\r'; break;
908 case 't': *q='\t'; break;
909 case 'v': *q='\v'; break;
910 case 'a': *q='a'; break;
911 case '?': *q='\?'; break;
912 default: *q=(unsigned char) (*p); break;
913 }
914 p++;
915 q++;
916 magic_info->length++;
917 continue;
918 }
919 else
920 if (LocaleNCompare(p,"&amp;",5) == 0)
921 (void) CopyMagickString(p+1,p+5,length-magic_info->length);
922 *q++=(unsigned char) (*p++);
923 magic_info->length++;
924 }
925 break;
926 }
927 break;
928 }
929 default:
930 break;
931 }
932 }
933 token=(char *) RelinquishMagickMemory(token);
934 return(status);
935}
936
937/*
938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939% %
940% %
941% %
942% L o a d M a g i c L i s t s %
943% %
944% %
945% %
946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947%
948% LoadMagicLists() loads one or more magic configuration file which provides a
949% mapping between magic attributes and a magic name.
950%
951% The format of the LoadMagicLists method is:
952%
953% MagickBooleanType LoadMagicLists(const char *filename,
954% ExceptionInfo *exception)
955%
956% A description of each parameter follows:
957%
958% o filename: the font file name.
959%
960% o exception: return any errors or warnings in this structure.
961%
962*/
963static MagickBooleanType LoadMagicLists(const char *filename,
964 ExceptionInfo *exception)
965{
cristy3ed852e2009-09-05 21:47:34 +0000966 char
967 path[MaxTextExtent];
968
969 const StringInfo
970 *option;
971
972 LinkedListInfo
973 *options;
974
975 MagickStatusType
976 status;
977
cristy1ca3eb32009-10-15 18:41:54 +0000978 register const MagicMapInfo
979 *p;
980
981 /*
982 Load built-in magic map.
983 */
cristy3ed852e2009-09-05 21:47:34 +0000984 status=MagickFalse;
cristy1ca3eb32009-10-15 18:41:54 +0000985 if (magic_list == (LinkedListInfo *) NULL)
cristye3e77a12009-10-16 00:47:21 +0000986 {
987 magic_list=NewLinkedList(0);
988 if (magic_list == (LinkedListInfo *) NULL)
989 {
990 ThrowFileException(exception,ResourceLimitError,
991 "MemoryAllocationFailed",filename);
992 return(MagickFalse);
993 }
994 }
cristy1ca3eb32009-10-15 18:41:54 +0000995 for (p=MagicMap; p->name != (const char *) NULL; p++)
996 {
997 MagicInfo
998 *magic_info;
999
1000 magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
1001 if (magic_info == (MagicInfo *) NULL)
cristye3e77a12009-10-16 00:47:21 +00001002 {
1003 (void) ThrowMagickException(exception,GetMagickModule(),
1004 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
1005 continue;
1006 }
cristy1ca3eb32009-10-15 18:41:54 +00001007 (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
cristy38b8ed92009-10-16 01:20:21 +00001008 magic_info->path=(char *) "[built-in]";
cristy1ca3eb32009-10-15 18:41:54 +00001009 magic_info->name=(char *) p->name;
1010 magic_info->offset=p->offset;
1011 magic_info->target=(char *) p->magic;
cristye3e77a12009-10-16 00:47:21 +00001012 magic_info->magic=(unsigned char *) p->magic;
cristy1ca3eb32009-10-15 18:41:54 +00001013 magic_info->length=p->length;
1014 magic_info->exempt=MagickTrue;
1015 magic_info->signature=MagickSignature;
1016 status=AppendValueToLinkedList(magic_list,magic_info);
1017 if (status == MagickFalse)
1018 (void) ThrowMagickException(exception,GetMagickModule(),
1019 ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
1020 }
1021 /*
1022 Load external magic map.
1023 */
cristy3ed852e2009-09-05 21:47:34 +00001024 *path='\0';
1025 options=GetConfigureOptions(filename,exception);
1026 option=(const StringInfo *) GetNextValueInLinkedList(options);
1027 while (option != (const StringInfo *) NULL)
1028 {
1029 (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
1030 status|=LoadMagicList((const char *) GetStringInfoDatum(option),
1031 GetStringInfoPath(option),0,exception);
1032 option=(const StringInfo *) GetNextValueInLinkedList(options);
1033 }
1034 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +00001035 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001036}