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