This source file includes following definitions.
- AcquireMimeCache
- LoadMimeLists
- GetMimeInfo
- MimeInfoCompare
- GetMimeInfoList
- MimeCompare
- GetMimeList
- GetMimeDescription
- GetMimeType
- IsMimeCacheInstantiated
- ListMimeInfo
- LoadMimeCache
- MagickToMime
- MimeComponentGenesis
- DestroyMimeElement
- MimeComponentTerminus
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/client.h"
#include "magick/configure.h"
#include "magick/deprecate.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/hashmap.h"
#include "magick/memory_.h"
#include "magick/mime.h"
#include "magick/mime-private.h"
#include "magick/nt-base-private.h"
#include "magick/option.h"
#include "magick/semaphore.h"
#include "magick/string_.h"
#include "magick/token.h"
#include "magick/utility.h"
#include "magick/xml-tree.h"
#include "magick/xml-tree-private.h"
#define MimeFilename "mime.xml"
struct _MimeInfo
{
char
*path,
*type,
*description,
*pattern;
ssize_t
priority;
MagickOffsetType
offset;
size_t
extent;
DataType
data_type;
ssize_t
mask,
value;
EndianType
endian;
size_t
length;
unsigned char
*magic;
MagickBooleanType
stealth;
size_t
signature;
};
static const char
*MimeMap = (char *)
"<?xml version=\"1.0\"?>"
"<mimemap>"
"</mimemap>";
static LinkedListInfo
*mime_cache = (LinkedListInfo *) NULL;
static SemaphoreInfo
*mime_semaphore = (SemaphoreInfo *) NULL;
static MagickBooleanType
IsMimeCacheInstantiated(ExceptionInfo *),
LoadMimeCache(LinkedListInfo *,const char *,const char *,const size_t,
ExceptionInfo *);
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);
}
MagickExport MagickBooleanType LoadMimeLists(const char *name,
ExceptionInfo *exception)
{
mime_cache=AcquireMimeCache(name,exception);
return(mime_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
}
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;
ssize_t
value;
unsigned long
lsb_first;
assert(exception != (ExceptionInfo *) NULL);
if (IsMimeCacheInstantiated(exception) == MagickFalse)
return((const MimeInfo *) NULL);
mime_info=(const MimeInfo *) NULL;
lsb_first=1;
LockSemaphoreInfo(mime_semaphore);
ResetLinkedListIterator(mime_cache);
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
if ((magic == (const unsigned char *) NULL) || (length == 0))
{
UnlockSemaphoreInfo(mime_semaphore);
return(p);
}
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_cache);
continue;
}
if ((p->pattern != (char *) NULL) && (filename != (char *) NULL))
{
if (GlobExpression(filename,p->pattern,MagickFalse) != MagickFalse)
mime_info=p;
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
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|=((ssize_t) *q++) << 8;
value|=((ssize_t) *q++) << 16;
value|=((ssize_t) *q++) << 24;
}
else
{
value=(ssize_t) (*q++) << 24;
value|=((ssize_t) *q++) << 16;
value|=((ssize_t) *q++) << 8;
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 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_cache);
}
if (mime_info != (const MimeInfo *) NULL)
(void) InsertValueInLinkedList(mime_cache,0,
RemoveElementByValueFromLinkedList(mime_cache,p));
UnlockSemaphoreInfo(mime_semaphore);
return(mime_info);
}
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
static int MimeInfoCompare(const void *x,const void *y)
{
const MimeInfo
**p,
**q;
p=(const MimeInfo **) x,
q=(const MimeInfo **) y;
if (strcasecmp((*p)->path,(*q)->path) == 0)
return(strcasecmp((*p)->type,(*q)->type));
return(strcasecmp((*p)->path,(*q)->path));
}
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
MagickExport const MimeInfo **GetMimeInfoList(const char *pattern,
size_t *number_aliases,ExceptionInfo *exception)
{
const MimeInfo
**aliases;
register const MimeInfo
*p;
register ssize_t
i;
assert(pattern != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
assert(number_aliases != (size_t *) NULL);
*number_aliases=0;
p=GetMimeInfo((char *) NULL,(unsigned char *) "*",0,exception);
if (p == (const MimeInfo *) NULL)
return((const MimeInfo **) NULL);
aliases=(const MimeInfo **) AcquireQuantumMemory((size_t)
GetNumberOfElementsInLinkedList(mime_cache)+1UL,sizeof(*aliases));
if (aliases == (const MimeInfo **) NULL)
return((const MimeInfo **) NULL);
LockSemaphoreInfo(mime_semaphore);
ResetLinkedListIterator(mime_cache);
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
for (i=0; p != (const MimeInfo *) NULL; )
{
if ((p->stealth == MagickFalse) &&
(GlobExpression(p->type,pattern,MagickFalse) != MagickFalse))
aliases[i++]=p;
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
}
UnlockSemaphoreInfo(mime_semaphore);
qsort((void *) aliases,(size_t) i,sizeof(*aliases),MimeInfoCompare);
aliases[i]=(MimeInfo *) NULL;
*number_aliases=(size_t) i;
return(aliases);
}
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
static int MimeCompare(const void *x,const void *y)
{
register char
*p,
*q;
p=(char *) x;
q=(char *) y;
return(strcasecmp(p,q));
}
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
MagickExport char **GetMimeList(const char *pattern,
size_t *number_aliases,ExceptionInfo *exception)
{
char
**aliases;
register const MimeInfo
*p;
register ssize_t
i;
assert(pattern != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
assert(number_aliases != (size_t *) NULL);
*number_aliases=0;
p=GetMimeInfo((char *) NULL,(unsigned char *) "*",0,exception);
if (p == (const MimeInfo *) NULL)
return((char **) NULL);
aliases=(char **) AcquireQuantumMemory((size_t)
GetNumberOfElementsInLinkedList(mime_cache)+1UL,sizeof(*aliases));
if (aliases == (char **) NULL)
return((char **) NULL);
LockSemaphoreInfo(mime_semaphore);
ResetLinkedListIterator(mime_cache);
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
for (i=0; p != (const MimeInfo *) NULL; )
{
if ((p->stealth == MagickFalse) &&
(GlobExpression(p->type,pattern,MagickFalse) != MagickFalse))
aliases[i++]=ConstantString(p->type);
p=(const MimeInfo *) GetNextValueInLinkedList(mime_cache);
}
UnlockSemaphoreInfo(mime_semaphore);
qsort((void *) aliases,(size_t) i,sizeof(*aliases),MimeCompare);
aliases[i]=(char *) NULL;
*number_aliases=(size_t) i;
return(aliases);
}
MagickExport const char *GetMimeDescription(const MimeInfo *mime_info)
{
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(mime_info != (MimeInfo *) NULL);
assert(mime_info->signature == MagickSignature);
return(mime_info->description);
}
MagickExport const char *GetMimeType(const MimeInfo *mime_info)
{
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(mime_info != (MimeInfo *) NULL);
assert(mime_info->signature == MagickSignature);
return(mime_info->type);
}
static MagickBooleanType IsMimeCacheInstantiated(ExceptionInfo *exception)
{
if (mime_cache == (LinkedListInfo *) NULL)
{
if (mime_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&mime_semaphore);
LockSemaphoreInfo(mime_semaphore);
if (mime_cache == (LinkedListInfo *) NULL)
mime_cache=AcquireMimeCache(MimeFilename,exception);
UnlockSemaphoreInfo(mime_semaphore);
}
return(mime_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
}
MagickExport MagickBooleanType ListMimeInfo(FILE *file,ExceptionInfo *exception)
{
const char
*path;
const MimeInfo
**mime_info;
register ssize_t
i;
size_t
number_aliases;
ssize_t
j;
if (file == (const FILE *) NULL)
file=stdout;
mime_info=GetMimeInfoList("*",&number_aliases,exception);
if (mime_info == (const MimeInfo **) NULL)
return(MagickFalse);
j=0;
path=(const char *) NULL;
for (i=0; i < (ssize_t) number_aliases; i++)
{
if (mime_info[i]->stealth != MagickFalse)
continue;
if ((path == (const char *) NULL) ||
(strcasecmp(path,mime_info[i]->path) != 0))
{
if (mime_info[i]->path != (char *) NULL)
(void) FormatLocaleFile(file,"\nPath: %s\n\n",mime_info[i]->path);
(void) FormatLocaleFile(file,"Type Description\n");
(void) FormatLocaleFile(file,
"-------------------------------------------------"
"------------------------------\n");
}
path=mime_info[i]->path;
(void) FormatLocaleFile(file,"%s",mime_info[i]->type);
if (strlen(mime_info[i]->type) <= 25)
{
for (j=(ssize_t) strlen(mime_info[i]->type); j <= 27; j++)
(void) FormatLocaleFile(file," ");
}
else
{
(void) FormatLocaleFile(file,"\n");
for (j=0; j <= 27; j++)
(void) FormatLocaleFile(file," ");
}
if (mime_info[i]->description != (char *) NULL)
(void) FormatLocaleFile(file,"%s",mime_info[i]->description);
(void) FormatLocaleFile(file,"\n");
}
(void) fflush(file);
mime_info=(const MimeInfo **) RelinquishMagickMemory((void *) mime_info);
return(MagickTrue);
}
static MagickBooleanType LoadMimeCache(LinkedListInfo *mime_cache,
const char *xml,const char *filename,const size_t depth,
ExceptionInfo *exception)
{
const char
*attribute;
MimeInfo
*mime_info = (MimeInfo *) NULL;
MagickStatusType
status;
XMLTreeInfo
*mime,
*mime_map,
*include;
(void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Loading mime map \"%s\" ...",filename);
if (xml == (const char *) NULL)
return(MagickFalse);
mime_map=NewXMLTree(xml,exception);
if (mime_map == (XMLTreeInfo *) NULL)
return(MagickFalse);
status=MagickTrue;
include=GetXMLTreeChild(mime_map,"include");
while (include != (XMLTreeInfo *) NULL)
{
attribute=GetXMLTreeAttribute(include,"file");
if (attribute != (const char *) NULL)
{
if (depth > 200)
(void) ThrowMagickException(exception,GetMagickModule(),
ConfigureError,"IncludeElementNestedTooDeeply","`%s'",filename);
else
{
char
path[MaxTextExtent],
*xml;
GetPathComponent(filename,HeadPath,path);
if (*path != '\0')
(void) ConcatenateMagickString(path,DirectorySeparator,
MaxTextExtent);
if (*attribute == *DirectorySeparator)
(void) CopyMagickString(path,attribute,MaxTextExtent);
else
(void) ConcatenateMagickString(path,attribute,MaxTextExtent);
xml=FileToXML(path,~0UL);
if (xml != (char *) NULL)
{
status&=LoadMimeCache(mime_cache,xml,path,depth+1,exception);
xml=DestroyString(xml);
}
}
}
include=GetNextXMLTreeTag(include);
}
mime=GetXMLTreeChild(mime_map,"mime");
while (mime != (XMLTreeInfo *) NULL)
{
const char
*attribute;
mime_info=(MimeInfo *) AcquireMagickMemory(sizeof(*mime_info));
if (mime_info == (MimeInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
(void) ResetMagickMemory(mime_info,0,sizeof(*mime_info));
mime_info->path=ConstantString(filename);
mime_info->signature=MagickSignature;
attribute=GetXMLTreeAttribute(mime,"data-type");
if (attribute != (const char *) NULL)
mime_info->data_type=(DataType) ParseCommandOption(MagickDataTypeOptions,
MagickTrue,attribute);
attribute=GetXMLTreeAttribute(mime,"description");
if (attribute != (const char *) NULL)
mime_info->description=ConstantString(attribute);
attribute=GetXMLTreeAttribute(mime,"endian");
if (attribute != (const char *) NULL)
mime_info->endian=(EndianType) ParseCommandOption(MagickEndianOptions,
MagickTrue,attribute);
attribute=GetXMLTreeAttribute(mime,"magic");
if (attribute != (const char *) NULL)
{
char
*token;
const char
*p;
register unsigned char
*q;
token=AcquireString(attribute);
(void) SubstituteString((char **) &token,"<","<");
(void) SubstituteString((char **) &token,"&","&");
(void) SubstituteString((char **) &token,""","\"");
mime_info->magic=(unsigned char *) AcquireString(token);
q=mime_info->magic;
for (p=token; *p != '\0'; )
{
if (*p == '\\')
{
p++;
if (isdigit((int) ((unsigned char) *p)) != 0)
{
char
*end;
*q++=(unsigned char) strtol(p,&end,8);
p+=(end-p);
mime_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++;
mime_info->length++;
continue;
}
*q++=(unsigned char) (*p++);
mime_info->length++;
}
token=DestroyString(token);
if (mime_info->data_type != StringData)
mime_info->value=(ssize_t) strtoul((char *) mime_info->magic,
(char **) NULL,0);
}
attribute=GetXMLTreeAttribute(mime,"mask");
if (attribute != (const char *) NULL)
mime_info->mask=(ssize_t) strtoul(attribute,(char **) NULL,0);
attribute=GetXMLTreeAttribute(mime,"offset");
if (attribute != (const char *) NULL)
{
char
*c;
mime_info->offset=(MagickOffsetType) strtol(attribute,&c,0);
if (*c == ':')
mime_info->extent=(size_t) strtol(c+1,(char **) NULL,0);
}
attribute=GetXMLTreeAttribute(mime,"pattern");
if (attribute != (const char *) NULL)
mime_info->pattern=ConstantString(attribute);
attribute=GetXMLTreeAttribute(mime,"priority");
if (attribute != (const char *) NULL)
mime_info->priority=(ssize_t) strtol(attribute,(char **) NULL,0);
attribute=GetXMLTreeAttribute(mime,"stealth");
if (attribute != (const char *) NULL)
mime_info->stealth=IsMagickTrue(attribute);
attribute=GetXMLTreeAttribute(mime,"type");
if (attribute != (const char *) NULL)
mime_info->type=ConstantString(attribute);
status=AppendValueToLinkedList(mime_cache,mime_info);
if (status == MagickFalse)
(void) ThrowMagickException(exception,GetMagickModule(),
ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
mime=GetNextXMLTreeTag(mime);
}
mime_map=DestroyXMLTree(mime_map);
return(status != 0 ? MagickTrue : MagickFalse);
}
MagickExport char *MagickToMime(const char *magick)
{
char
filename[MaxTextExtent],
media[MaxTextExtent];
const MimeInfo
*mime_info;
ExceptionInfo
*exception;
(void) FormatLocaleString(filename,MaxTextExtent,"file.%s",magick);
LocaleLower(filename);
exception=AcquireExceptionInfo();
mime_info=GetMimeInfo(filename,(unsigned char *) " ",1,exception);
exception=DestroyExceptionInfo(exception);
if (mime_info != (const MimeInfo *) NULL)
return(ConstantString(GetMimeType(mime_info)));
(void) FormatLocaleString(media,MaxTextExtent,"image/x-%s",magick);
LocaleLower(media+8);
return(ConstantString(media));
}
MagickExport MagickBooleanType MimeComponentGenesis(void)
{
if (mime_semaphore == (SemaphoreInfo *) NULL)
mime_semaphore=AllocateSemaphoreInfo();
return(MagickTrue);
}
static void *DestroyMimeElement(void *mime_info)
{
register MimeInfo
*p;
p=(MimeInfo *) mime_info;
if (p->magic != (unsigned char *) NULL)
p->magic=(unsigned char *) RelinquishMagickMemory(p->magic);
if (p->pattern != (char *) NULL)
p->pattern=DestroyString(p->pattern);
if (p->description != (char *) NULL)
p->description=DestroyString(p->description);
if (p->type != (char *) NULL)
p->type=DestroyString(p->type);
if (p->path != (char *) NULL)
p->path=DestroyString(p->path);
p=(MimeInfo *) RelinquishMagickMemory(p);
return((void *) NULL);
}
MagickExport void MimeComponentTerminus(void)
{
if (mime_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&mime_semaphore);
LockSemaphoreInfo(mime_semaphore);
if (mime_cache != (LinkedListInfo *) NULL)
mime_cache=DestroyLinkedList(mime_cache,DestroyMimeElement);
UnlockSemaphoreInfo(mime_semaphore);
DestroySemaphoreInfo(&mime_semaphore);
}