This source file includes following definitions.
- DestroyCoderNode
- AcquireCoderCache
- CoderComponentGenesis
- CoderComponentTerminus
- GetCoderInfo
- CoderInfoCompare
- GetCoderInfoList
- CoderCompare
- GetCoderList
- IsCoderTreeInstantiated
- ListCoderInfo
- LoadCoderCache
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/client.h"
#include "magick/coder.h"
#include "magick/configure.h"
#include "magick/draw.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/hashmap.h"
#include "magick/log.h"
#include "magick/memory_.h"
#include "magick/option.h"
#include "magick/semaphore.h"
#include "magick/string_.h"
#include "magick/splay-tree.h"
#include "magick/token.h"
#include "magick/utility.h"
#include "magick/xml-tree.h"
#include "magick/xml-tree-private.h"
#define MagickCoderFilename "coder.xml"
typedef struct _CoderMapInfo
{
const char
*magick,
*name;
} CoderMapInfo;
static const CoderMapInfo
CoderMap[] =
{
{ "3FR", "DNG" },
{ "8BIM", "META" },
{ "8BIMTEXT", "META" },
{ "8BIMWTEXT", "META" },
{ "AFM", "TTF" },
{ "A", "RAW" },
{ "AI", "PDF" },
{ "APP1JPEG", "META" },
{ "APP1", "META" },
{ "ARW", "DNG" },
{ "AVI", "MPEG" },
{ "BIE", "JBIG" },
{ "BMP2", "BMP" },
{ "BMP3", "BMP" },
{ "B", "RAW" },
{ "BRF", "BRAILLE" },
{ "BGRA", "BGR" },
{ "BGRO", "BGR" },
{ "CMYKA", "CMYK" },
{ "C", "RAW" },
{ "CAL", "CALS" },
{ "CANVAS", "XC" },
{ "CMYKA", "CMYK" },
{ "CR2", "DNG" },
{ "CRW", "DNG" },
{ "CUR", "ICON" },
{ "DATA", "INLINE" },
{ "DCR", "DNG" },
{ "DCX", "PCX" },
{ "DFONT", "TTF" },
{ "DXT1", "DDS" },
{ "DXT5", "DDS" },
{ "EPDF", "PDF" },
{ "EPI", "PS" },
{ "EPS2", "PS2" },
{ "EPS3", "PS3" },
{ "EPSF", "PS" },
{ "EPSI", "PS" },
{ "EPS", "PS" },
{ "EPT2", "EPT" },
{ "EPT3", "EPT" },
{ "ERF", "DNG" },
{ "EXIF", "META" },
{ "FILE", "URL" },
{ "FRACTAL", "PLASMA" },
{ "FTP", "URL" },
{ "FTS", "FITS" },
{ "G3", "FAX" },
{ "GIF87", "GIF" },
{ "G", "RAW" },
{ "GRANITE", "MAGICK" },
{ "GROUP4", "TIFF" },
{ "GV", "DOT" },
{ "HTM", "HTML" },
{ "ICB", "TGA" },
{ "ICO", "ICON" },
{ "IIQ", "DNG" },
{ "K25", "DNG" },
{ "KDC", "DNG" },
{ "H", "MAGICK" },
{ "HTM", "HTML" },
{ "HTTP", "URL" },
{ "HTTPS", "URL" },
{ "ICB", "TGA" },
{ "ICC", "META" },
{ "ICM", "META" },
{ "ICO", "ICON" },
{ "IMPLICIT", "***" },
{ "IPTC", "META" },
{ "IPTCTEXT", "META" },
{ "IPTCWTEXT", "META" },
{ "ISOBRL", "BRAILLE" },
{ "ISOBRL6", "BRAILLE" },
{ "JBG", "JBIG" },
{ "JNG", "PNG" },
{ "JPC", "JP2" },
{ "JPT", "JP2" },
{ "JPM", "JP2" },
{ "J2C", "JP2" },
{ "J2K", "JP2" },
{ "JNG", "PNG" },
{ "JPE", "JPEG" },
{ "JPG", "JPEG" },
{ "JPM", "JP2" },
{ "JPS", "JPEG" },
{ "JPT", "JP2" },
{ "JPX", "JP2" },
{ "K", "RAW" },
{ "K25", "DNG" },
{ "KDC", "DNG" },
{ "LOGO", "MAGICK" },
{ "M", "RAW" },
{ "M2V", "MPEG" },
{ "M4V", "MPEG" },
{ "MEF", "DNG" },
{ "MKV", "MPEG" },
{ "MNG", "PNG" },
{ "MOV", "MPEG" },
{ "MP4", "MPEG" },
{ "MPG", "MPEG" },
{ "MPRI", "MPR" },
{ "MEF", "DNG" },
{ "MRW", "DNG" },
{ "MSVG", "SVG" },
{ "NEF", "DNG" },
{ "NETSCAPE", "MAGICK" },
{ "NRW", "DNG" },
{ "O", "RAW" },
{ "ORF", "DNG" },
{ "OTF", "TTF" },
{ "P7", "PNM" },
{ "PAL", "UYVY" },
{ "PAM", "PNM" },
{ "PBM", "PNM" },
{ "PCDS", "PCD" },
{ "PCT", "PICT" },
{ "PDFA", "PDF" },
{ "PEF", "DNG" },
{ "PEF", "DNG" },
{ "PFA", "TTF" },
{ "PFB", "TTF" },
{ "PFM", "PNM" },
{ "PGM", "PNM" },
{ "PGX", "JP2" },
{ "PICON", "XPM" },
{ "PJPEG", "JPEG" },
{ "PM", "XPM" },
{ "PNG00", "PNG" },
{ "PNG24", "PNG" },
{ "PNG32", "PNG" },
{ "PNG48", "PNG" },
{ "PNG64", "PNG" },
{ "PNG8", "PNG" },
{ "PPM", "PNM" },
{ "PSB", "PSD" },
{ "PTIF", "TIFF" },
{ "R", "RAW" },
{ "RADIAL-GRADIENT", "GRADIENT" },
{ "RAF", "DNG" },
{ "RAS", "SUN" },
{ "RAW", "DNG" },
{ "RGBA", "RGB" },
{ "RGBO", "RGB" },
{ "RMF", "DNG" },
{ "ROSE", "MAGICK" },
{ "RW2", "DNG" },
{ "SHTML", "HTML" },
{ "SIX", "SIXEL" },
{ "SPARSE-COLOR", "TXT" },
{ "SR2", "DNG" },
{ "SRF", "DNG" },
{ "SVGZ", "SVG" },
{ "TEXT", "TXT" },
{ "TIFF64", "TIFF" },
{ "TIF", "TIFF" },
{ "TTC", "TTF" },
{ "UBRL", "BRAILLE" },
{ "UBRL6", "BRAILLE" },
{ "VDA", "TGA" },
{ "VST", "TGA" },
{ "WIZARD", "MAGICK" },
#if defined(MAGICKCORE_WINGDI32_DELEGATE)
{ "WMF", "EMF" },
#endif
{ "WMV", "MPEG" },
{ "WMZ", "WMF" },
{ "X3f", "DNG" },
{ "XMP", "META" },
{ "XTRNARRAY", "XTRN" },
{ "XTRNBLOB", "XTRN" },
{ "XTRNFILE", "XTRN" },
{ "XTRNIMAGE", "XTRN" },
{ "XV", "VIFF" },
{ "Y", "RAW" },
{ "YCbCrA", "YCbCr" }
};
static SemaphoreInfo
*coder_semaphore = (SemaphoreInfo *) NULL;
static SplayTreeInfo
*coder_cache = (SplayTreeInfo *) NULL;
static MagickBooleanType
IsCoderTreeInstantiated(ExceptionInfo *),
LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
ExceptionInfo *);
static void *DestroyCoderNode(void *coder_info)
{
register CoderInfo
*p;
p=(CoderInfo *) coder_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->magick != (char *) NULL)
p->magick=DestroyString(p->magick);
}
return(RelinquishMagickMemory(p));
}
static SplayTreeInfo *AcquireCoderCache(const char *filename,
ExceptionInfo *exception)
{
const StringInfo
*option;
LinkedListInfo
*options;
MagickStatusType
status;
register ssize_t
i;
SplayTreeInfo
*coder_cache;
coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
DestroyCoderNode);
if (coder_cache == (SplayTreeInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
status=MagickTrue;
options=GetConfigureOptions(filename,exception);
option=(const StringInfo *) GetNextValueInLinkedList(options);
while (option != (const StringInfo *) NULL)
{
status&=LoadCoderCache(coder_cache,(const char *)
GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
option=(const StringInfo *) GetNextValueInLinkedList(options);
}
options=DestroyConfigureOptions(options);
for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
{
CoderInfo
*coder_info;
register const CoderMapInfo
*p;
p=CoderMap+i;
coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
if (coder_info == (CoderInfo *) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),
ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
continue;
}
(void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
coder_info->path=(char *) "[built-in]";
coder_info->magick=(char *) p->magick;
coder_info->name=(char *) p->name;
coder_info->exempt=MagickTrue;
coder_info->signature=MagickSignature;
status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
coder_info);
if (status == MagickFalse)
(void) ThrowMagickException(exception,GetMagickModule(),
ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
}
return(coder_cache);
}
MagickExport MagickBooleanType CoderComponentGenesis(void)
{
if (coder_semaphore == (SemaphoreInfo *) NULL)
coder_semaphore=AllocateSemaphoreInfo();
return(MagickTrue);
}
MagickExport void CoderComponentTerminus(void)
{
if (coder_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&coder_semaphore);
LockSemaphoreInfo(coder_semaphore);
if (coder_cache != (SplayTreeInfo *) NULL)
coder_cache=DestroySplayTree(coder_cache);
UnlockSemaphoreInfo(coder_semaphore);
DestroySemaphoreInfo(&coder_semaphore);
}
MagickExport const CoderInfo *GetCoderInfo(const char *name,
ExceptionInfo *exception)
{
const CoderInfo
*coder_info;
assert(exception != (ExceptionInfo *) NULL);
if (IsCoderTreeInstantiated(exception) == MagickFalse)
return((const CoderInfo *) NULL);
LockSemaphoreInfo(coder_semaphore);
if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
{
ResetSplayTreeIterator(coder_cache);
coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
UnlockSemaphoreInfo(coder_semaphore);
return(coder_info);
}
coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
UnlockSemaphoreInfo(coder_semaphore);
return(coder_info);
}
static int CoderInfoCompare(const void *x,const void *y)
{
const CoderInfo
**p,
**q;
p=(const CoderInfo **) x,
q=(const CoderInfo **) y;
if (LocaleCompare((*p)->path,(*q)->path) == 0)
return(LocaleCompare((*p)->name,(*q)->name));
return(LocaleCompare((*p)->path,(*q)->path));
}
MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
size_t *number_coders,ExceptionInfo *exception)
{
const CoderInfo
**coder_map;
register const CoderInfo
*p;
register ssize_t
i;
assert(pattern != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
assert(number_coders != (size_t *) NULL);
*number_coders=0;
p=GetCoderInfo("*",exception);
if (p == (const CoderInfo *) NULL)
return((const CoderInfo **) NULL);
coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
if (coder_map == (const CoderInfo **) NULL)
return((const CoderInfo **) NULL);
LockSemaphoreInfo(coder_semaphore);
ResetSplayTreeIterator(coder_cache);
p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
for (i=0; p != (const CoderInfo *) NULL; )
{
if ((p->stealth == MagickFalse) &&
(GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
coder_map[i++]=p;
p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
}
UnlockSemaphoreInfo(coder_semaphore);
qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
coder_map[i]=(CoderInfo *) NULL;
*number_coders=(size_t) i;
return(coder_map);
}
static int CoderCompare(const void *x,const void *y)
{
register const char
**p,
**q;
p=(const char **) x;
q=(const char **) y;
return(LocaleCompare(*p,*q));
}
MagickExport char **GetCoderList(const char *pattern,
size_t *number_coders,ExceptionInfo *exception)
{
char
**coder_map;
register const CoderInfo
*p;
register ssize_t
i;
assert(pattern != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
assert(number_coders != (size_t *) NULL);
*number_coders=0;
p=GetCoderInfo("*",exception);
if (p == (const CoderInfo *) NULL)
return((char **) NULL);
coder_map=(char **) AcquireQuantumMemory((size_t)
GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
if (coder_map == (char **) NULL)
return((char **) NULL);
LockSemaphoreInfo(coder_semaphore);
ResetSplayTreeIterator(coder_cache);
p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
for (i=0; p != (const CoderInfo *) NULL; )
{
if ((p->stealth == MagickFalse) &&
(GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
coder_map[i++]=ConstantString(p->name);
p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
}
UnlockSemaphoreInfo(coder_semaphore);
qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
coder_map[i]=(char *) NULL;
*number_coders=(size_t) i;
return(coder_map);
}
static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
{
if (coder_cache == (SplayTreeInfo *) NULL)
{
if (coder_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&coder_semaphore);
LockSemaphoreInfo(coder_semaphore);
if (coder_cache == (SplayTreeInfo *) NULL)
coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
UnlockSemaphoreInfo(coder_semaphore);
}
return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
}
MagickExport MagickBooleanType ListCoderInfo(FILE *file,
ExceptionInfo *exception)
{
const char
*path;
const CoderInfo
**coder_info;
register ssize_t
i;
size_t
number_coders;
ssize_t
j;
if (file == (const FILE *) NULL)
file=stdout;
coder_info=GetCoderInfoList("*",&number_coders,exception);
if (coder_info == (const CoderInfo **) NULL)
return(MagickFalse);
path=(const char *) NULL;
for (i=0; i < (ssize_t) number_coders; i++)
{
if (coder_info[i]->stealth != MagickFalse)
continue;
if ((path == (const char *) NULL) ||
(LocaleCompare(path,coder_info[i]->path) != 0))
{
if (coder_info[i]->path != (char *) NULL)
(void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
(void) FormatLocaleFile(file,"Magick Coder\n");
(void) FormatLocaleFile(file,
"-------------------------------------------------"
"------------------------------\n");
}
path=coder_info[i]->path;
(void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
(void) FormatLocaleFile(file," ");
if (coder_info[i]->name != (char *) NULL)
(void) FormatLocaleFile(file,"%s",coder_info[i]->name);
(void) FormatLocaleFile(file,"\n");
}
coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
(void) fflush(file);
return(MagickTrue);
}
static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
const char *xml,const char *filename,const size_t depth,
ExceptionInfo *exception)
{
char
keyword[MaxTextExtent],
*token;
const char
*q;
CoderInfo
*coder_info;
MagickStatusType
status;
(void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Loading coder configuration file \"%s\" ...",filename);
if (xml == (const char *) NULL)
return(MagickFalse);
status=MagickTrue;
coder_info=(CoderInfo *) NULL;
token=AcquireString(xml);
for (q=(char *) xml; *q != '\0'; )
{
GetMagickToken(q,&q,token);
if (*token == '\0')
break;
(void) CopyMagickString(keyword,token,MaxTextExtent);
if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
{
while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
GetMagickToken(q,&q,token);
continue;
}
if (LocaleNCompare(keyword,"<!--",4) == 0)
{
while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
GetMagickToken(q,&q,token);
continue;
}
if (LocaleCompare(keyword,"<include") == 0)
{
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,"IncludeNodeNestedTooDeeply","`%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=FileToXML(path,~0UL);
if (xml != (char *) NULL)
{
status&=LoadCoderCache(coder_cache,xml,path,depth+1,
exception);
xml=(char *) RelinquishMagickMemory(xml);
}
}
}
}
continue;
}
if (LocaleCompare(keyword,"<coder") == 0)
{
coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
if (coder_info == (CoderInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
(void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
coder_info->path=ConstantString(filename);
coder_info->exempt=MagickFalse;
coder_info->signature=MagickSignature;
continue;
}
if (coder_info == (CoderInfo *) NULL)
continue;
if (LocaleCompare(keyword,"/>") == 0)
{
status=AddValueToSplayTree(coder_cache,ConstantString(
coder_info->magick),coder_info);
if (status == MagickFalse)
(void) ThrowMagickException(exception,GetMagickModule(),
ResourceLimitError,"MemoryAllocationFailed","`%s'",
coder_info->magick);
coder_info=(CoderInfo *) NULL;
continue;
}
GetMagickToken(q,(const char **) NULL,token);
if (*token != '=')
continue;
GetMagickToken(q,&q,token);
GetMagickToken(q,&q,token);
switch (*keyword)
{
case 'M':
case 'm':
{
if (LocaleCompare((char *) keyword,"magick") == 0)
{
coder_info->magick=ConstantString(token);
break;
}
break;
}
case 'N':
case 'n':
{
if (LocaleCompare((char *) keyword,"name") == 0)
{
coder_info->name=ConstantString(token);
break;
}
break;
}
case 'S':
case 's':
{
if (LocaleCompare((char *) keyword,"stealth") == 0)
{
coder_info->stealth=IsMagickTrue(token);
break;
}
break;
}
default:
break;
}
}
token=(char *) RelinquishMagickMemory(token);
return(status != 0 ? MagickTrue : MagickFalse);
}