/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % A c q u i r e M i m e C a c h e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % AcquireMimeCache() caches one or more magic configurations which provides % a mapping between magic attributes and a magic name. % % The format of the AcquireMimeCache method is: % % LinkedListInfo *AcquireMimeCache(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. % */ MagickExport LinkedListInfo *AcquireMimeCache(const char *filename, ExceptionInfo *exception) { LinkedListInfo *cache; MagickStatusType status; cache=NewLinkedList(0); status=MagickTrue; #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT) { const StringInfo *option; LinkedListInfo *options; options=GetConfigureOptions(filename,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); while (option != (const StringInfo *) NULL) { status&=LoadMimeCache(cache,(const char *) GetStringInfoDatum(option),GetStringInfoPath(option),0,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); } options=DestroyConfigureOptions(options); } #endif if (IsLinkedListEmpty(cache) != MagickFalse) status&=LoadMimeCache(cache,MimeMap,"built-in",0,exception); return(cache); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + G e t C o n f i g u r e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % GetConfigureInfo() searches the configure list for the specified name and if % found returns attributes for that element. % % The format of the GetConfigureInfo method is: % % const ConfigureInfo *GetConfigureInfo(const char *name, % ExceptionInfo *exception) % % A description of each parameter follows: % % o configure_info: GetConfigureInfo() searches the configure list for the % specified name and if found returns attributes for that element. % % o name: the configure name. % % o exception: return any errors or warnings in this structure. % */ MagickExport const ConfigureInfo *GetConfigureInfo(const char *name, ExceptionInfo *exception) { register const ConfigureInfo *p; assert(exception != (ExceptionInfo *) NULL); if ((configure_list == (LinkedListInfo *) NULL) || (instantiate_configure == MagickFalse)) if (InitializeConfigureList(exception) == MagickFalse) return((const ConfigureInfo *) NULL); if ((configure_list == (LinkedListInfo *) NULL) || (IsLinkedListEmpty(configure_list) != MagickFalse)) return((const ConfigureInfo *) NULL); if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0)) return((const ConfigureInfo *) GetValueFromLinkedList(configure_list,0)); /* Search for configure tag. */ LockSemaphoreInfo(configure_semaphore); ResetLinkedListIterator(configure_list); p=(const ConfigureInfo *) GetNextValueInLinkedList(configure_list); while (p != (const ConfigureInfo *) NULL) { if (LocaleCompare(name,p->name) == 0) break; p=(const ConfigureInfo *) GetNextValueInLinkedList(configure_list); } if (p != (ConfigureInfo *) NULL) (void) InsertValueInLinkedList(configure_list,0, RemoveElementByValueFromLinkedList(configure_list,p)); UnlockSemaphoreInfo(configure_semaphore); return(p); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + 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) || (length == 0)) return((const MagicInfo *) GetValueFromLinkedList(magic_list,0)); if (length == 0) return((const MagicInfo *) NULL); /* Search for requested magic. */ AcquireSemaphoreInfo(&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); } RelinquishSemaphoreInfo(magic_semaphore); return((const MagicInfo *) p); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % L o a d M i m e L i s t s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % LoadMimeList() loads one or more magic configuration file which provides a % mapping between magic attributes and a magic name. % % The format of the LoadMimeLists method is: % % MagickBooleanType LoadMimeLists(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. % */ MagickExport MagickBooleanType LoadMimeLists(const char *filename, ExceptionInfo *exception) { #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT) return(LoadMimeList(MimeMap,"built-in",0,exception)); #else const StringInfo *option; LinkedListInfo *options; MagickStatusType status; status=MagickFalse; options=GetConfigureOptions(filename,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); while (option != (const StringInfo *) NULL) { status&=LoadMimeList((const char *) GetStringInfoDatum(option), GetStringInfoPath(option),0,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); } options=DestroyConfigureOptions(options); if ((mime_list == (LinkedListInfo *) NULL) || (IsLinkedListEmpty(mime_list) != MagickFalse)) status&=LoadMimeList(MimeMap,"built-in",0,exception); else ClearMagickException(exception); return(status != 0 ? MagickTrue : MagickFalse); #endif }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + G e t P o l i c y I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % GetPolicyInfo() searches the policy list for the specified name and if found % returns attributes for that policy. % % The format of the GetPolicyInfo method is: % % PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception) % % A description of each parameter follows: % % o name: the policy name. % % o exception: return any errors or warnings in this structure. % */ static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception) { char policyname[MaxTextExtent]; register PolicyInfo *p; register char *q; assert(exception != (ExceptionInfo *) NULL); if ((policy_list == (LinkedListInfo *) NULL) || (instantiate_policy == MagickFalse)) if (InitializePolicyList(exception) == MagickFalse) return((PolicyInfo *) NULL); if ((policy_list == (LinkedListInfo *) NULL) || (IsLinkedListEmpty(policy_list) != MagickFalse)) return((PolicyInfo *) NULL); if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0)) return((PolicyInfo *) GetValueFromLinkedList(policy_list,0)); /* Strip names of whitespace. */ (void) CopyMagickString(policyname,name,MaxTextExtent); for (q=policyname; *q != '\0'; q++) { if (isspace((int) ((unsigned char) *q)) == 0) continue; (void) CopyMagickString(q,q+1,MaxTextExtent); q--; } /* Search for policy tag. */ LockSemaphoreInfo(policy_semaphore); ResetLinkedListIterator(policy_list); p=(PolicyInfo *) GetNextValueInLinkedList(policy_list); while (p != (PolicyInfo *) NULL) { if (LocaleCompare(policyname,p->name) == 0) break; p=(PolicyInfo *) GetNextValueInLinkedList(policy_list); } if (p != (PolicyInfo *) NULL) (void) InsertValueInLinkedList(policy_list,0, RemoveElementByValueFromLinkedList(policy_list,p)); UnlockSemaphoreInfo(policy_semaphore); return(p); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % L o a d M a g i c L i s t s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % LoadMagicList() 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) { #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT) return(LoadMagicList(MagicMap,"built-in",0,exception)); #else char path[MaxTextExtent]; const StringInfo *option; LinkedListInfo *options; MagickStatusType status; status=MagickFalse; *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); if ((magic_list == (LinkedListInfo *) NULL) || (IsLinkedListEmpty(magic_list) != MagickFalse)) { (void) ThrowMagickException(exception,GetMagickModule(),ConfigureWarning, "UnableToOpenConfigureFile","`%s'",path); status|=LoadMagicList(MagicMap,"built-in",0,exception); } return(status != 0 ? MagickTrue : MagickFalse); #endif }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + 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); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % A c q u i r e M i m e C a c h e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % AcquireMimeCache() caches one or more magic configurations which provides % a mapping between magic attributes and a magic name. % % The format of the AcquireMimeCache method is: % % LinkedListInfo *AcquireMimeCache(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 LinkedListInfo *AcquireMimeCache(const char *filename, ExceptionInfo *exception) { LinkedListInfo *mime_cache; MagickStatusType status; mime_cache=NewLinkedList(0); if (mime_cache == (LinkedListInfo *) NULL) ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); status=MagickTrue; #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT) { const StringInfo *option; LinkedListInfo *options; options=GetConfigureOptions(filename,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); while (option != (const StringInfo *) NULL) { status&=LoadMimeCache(mime_cache,(const char *) GetStringInfoDatum(option),GetStringInfoPath(option),0,exception); option=(const StringInfo *) GetNextValueInLinkedList(options); } options=DestroyConfigureOptions(options); } #endif if (IsLinkedListEmpty(mime_cache) != MagickFalse) status&=LoadMimeCache(mime_cache,MimeMap,"built-in",0,exception); return(mime_cache); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + G e t M i m e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % GetMimeInfo() attempts to classify the content to identify which mime type % is associated with the content, if any. % % The format of the GetMimeInfo method is: % % const MimeInfo *GetMimeInfo(const char *filename, % const unsigned char *magic,const size_t length, % ExceptionInfo *exception) % % A description of each parameter follows: % % o filename: If we cannot not classify the string, we attempt to classify % based on the filename (e.g. *.pdf returns application/pdf). % % 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 MimeInfo *GetMimeInfo(const char *filename, const unsigned char *magic,const size_t length,ExceptionInfo *exception) { const MimeInfo *mime_info; EndianType endian; register const MimeInfo *p; register const unsigned char *q; register ssize_t i; size_t lsb_first; ssize_t value; assert(exception != (ExceptionInfo *) NULL); if ((mime_list == (LinkedListInfo *) NULL) || (instantiate_mime == MagickFalse)) if (InitializeMimeList(exception) == MagickFalse) return((const MimeInfo *) NULL); if ((mime_list == (LinkedListInfo *) NULL) || (IsLinkedListEmpty(mime_list) != MagickFalse)) return((const MimeInfo *) NULL); if ((magic == (const unsigned char *) NULL) || (length == 0)) return((const MimeInfo *) GetValueFromLinkedList(mime_list,0)); if (length == 0) return((const MimeInfo *) NULL); /* Search for mime tag. */ mime_info=(const MimeInfo *) NULL; lsb_first=1; LockSemaphoreInfo(mime_semaphore); ResetLinkedListIterator(mime_list); p=(const MimeInfo *) GetNextValueInLinkedList(mime_list); while (p != (const MimeInfo *) NULL) { assert(p->offset >= 0); if (mime_info != (const MimeInfo *) NULL) if (p->priority > mime_info->priority) { p=(const MimeInfo *) GetNextValueInLinkedList(mime_list); continue; } if ((p->pattern != (char *) NULL) && (filename != (char *) NULL)) { if (GlobExpression(filename,p->pattern,MagickFalse) != MagickFalse) mime_info=p; p=(const MimeInfo *) GetNextValueInLinkedList(mime_list); continue; } switch (p->data_type) { case ByteData: { if ((size_t) (p->offset+4) > length) break; q=magic+p->offset; value=(ssize_t) (*q++); if (p->mask == 0) { if (p->value == value) mime_info=p; } else { if ((p->value & p->mask) == value) mime_info=p; } break; } case ShortData: { if ((size_t) (p->offset+4) > length) break; q=magic+p->offset; endian=p->endian; if (p->endian == UndefinedEndian) endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian; if (endian == LSBEndian) { value=(ssize_t) (*q++); value|=(*q++) << 8; } else { value=(ssize_t) (*q++) << 8; value|=(*q++); } if (p->mask == 0) { if (p->value == value) mime_info=p; } else { if ((p->value & p->mask) == value) mime_info=p; } break; } case LongData: { if ((size_t) (p->offset+4) > length) break; q=magic+p->offset; endian=p->endian; if (p->endian == UndefinedEndian) endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian; if (endian == LSBEndian) { value=(ssize_t) (*q++); value|=(*q++) << 8; value|=(*q++) << 16; value|=(*q++) << 24; } else { value=(ssize_t) (*q++) << 24; value|=(*q++) << 16; value|=(*q++) << 8; value|=(*q++); } if (p->mask == 0) { if (p->value == value) mime_info=p; } else { if ((p->value & p->mask) == value) mime_info=p; } break; } case StringData: default: { for (i=0; i <= (ssize_t) p->extent; i++) { if ((size_t) (p->offset+i+p->length) > length) break; if (memcmp(magic+p->offset+i,p->magic,p->length) == 0) { mime_info=p; break; } } break; } } p=(const MimeInfo *) GetNextValueInLinkedList(mime_list); } if (p != (const MimeInfo *) NULL) (void) InsertValueInLinkedList(mime_list,0, RemoveElementByValueFromLinkedList(mime_list,p)); UnlockSemaphoreInfo(mime_semaphore); return(mime_info); }