This source file includes following definitions.
- AcquireString
- AcquireStringInfo
- BlobToStringInfo
- CloneString
- CloneStringInfo
- CompareStringInfo
- ConcatenateMagickString
- ConcatenateString
- ConcatenateStringInfo
- ConfigureFileToStringInfo
- ConstantString
- CopyMagickString
- DestroyString
- DestroyStringInfo
- DestroyStringList
- EscapeString
- FileToString
- FileToStringInfo
- FormatMagickSize
- FormatMagickTime
- GetEnvironmentValue
- GetStringInfoDatum
- GetStringInfoLength
- GetStringInfoPath
- InterpretSiPrefixValue
- IsStringTrue
- IsStringNotFalse
- PrintStringInfo
- ResetStringInfo
- SetStringInfo
- SetStringInfoDatum
- SetStringInfoLength
- SetStringInfoPath
- SplitStringInfo
- StringInfoToString
- StringInfoToHexString
- StringToArgv
- StringToArrayOfDoubles
- StringToken
- StringToList
- StringToStringInfo
- StripString
- SubstituteString
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/blob-private.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/image-private.h"
#include "magick/list.h"
#include "magick/locale_.h"
#include "magick/log.h"
#include "magick/memory_.h"
#include "magick/nt-base.h"
#include "magick/nt-base-private.h"
#include "magick/property.h"
#include "magick/resource_.h"
#include "magick/signature-private.h"
#include "magick/string_.h"
#include "magick/string-private.h"
#include "magick/utility-private.h"
#if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
static const unsigned char
AsciiMap[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
0xfc, 0xfd, 0xfe, 0xff,
};
#endif
MagickExport char *AcquireString(const char *source)
{
char
*destination;
size_t
length;
length=0;
if (source != (char *) NULL)
length+=strlen(source);
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*destination));
if (destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
*destination='\0';
if (source != (char *) NULL)
(void) memcpy(destination,source,length*sizeof(*destination));
destination[length]='\0';
return(destination);
}
MagickExport StringInfo *AcquireStringInfo(const size_t length)
{
StringInfo
*string_info;
string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
if (string_info == (StringInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
(void) ResetMagickMemory(string_info,0,sizeof(*string_info));
string_info->signature=MagickSignature;
string_info->length=length;
string_info->datum=(unsigned char *) NULL;
if (~string_info->length >= (MaxTextExtent-1))
string_info->datum=(unsigned char *) AcquireQuantumMemory(
string_info->length+MaxTextExtent,sizeof(*string_info->datum));
if (string_info->datum == (unsigned char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
return(string_info);
}
MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
{
StringInfo
*string_info;
string_info=AcquireStringInfo(0);
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
string_info->length=length;
if (string_info->datum == (unsigned char *) NULL)
string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
MaxTextExtent,sizeof(*string_info->datum));
else
string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
length+MaxTextExtent,sizeof(*string_info->datum));
if (string_info->datum == (unsigned char *) NULL)
{
string_info=DestroyStringInfo(string_info);
return((StringInfo *) NULL);
}
if (blob != (const void *) NULL)
(void) memcpy(string_info->datum,blob,length);
return(string_info);
}
MagickExport char *CloneString(char **destination,const char *source)
{
size_t
length;
assert(destination != (char **) NULL);
if (source == (const char *) NULL)
{
if (*destination != (char *) NULL)
*destination=DestroyString(*destination);
return(*destination);
}
if (*destination == (char *) NULL)
{
*destination=AcquireString(source);
return(*destination);
}
length=strlen(source);
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
*destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
sizeof(**destination));
if (*destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
if (length != 0)
(void) memcpy(*destination,source,length*sizeof(**destination));
(*destination)[length]='\0';
return(*destination);
}
MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
{
StringInfo
*clone_info;
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
clone_info=AcquireStringInfo(string_info->length);
if (string_info->length != 0)
(void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
return(clone_info);
}
MagickExport int CompareStringInfo(const StringInfo *target,
const StringInfo *source)
{
int
status;
assert(target != (StringInfo *) NULL);
assert(target->signature == MagickSignature);
assert(source != (StringInfo *) NULL);
assert(source->signature == MagickSignature);
status=memcmp(target->datum,source->datum,MagickMin(target->length,
source->length));
if (status != 0)
return(status);
if (target->length == source->length)
return(0);
return(target->length < source->length ? -1 : 1);
}
MagickExport size_t ConcatenateMagickString(char *destination,
const char *source,const size_t length)
{
register char
*q;
register const char
*p;
register size_t
i;
size_t
count;
assert(destination != (char *) NULL);
assert(source != (const char *) NULL);
assert(length >= 1);
p=source;
q=destination;
i=length;
while ((i-- != 0) && (*q != '\0'))
q++;
count=(size_t) (q-destination);
i=length-count;
if (i == 0)
return(count+strlen(p));
while (*p != '\0')
{
if (i != 1)
{
*q++=(*p);
i--;
}
p++;
}
*q='\0';
return(count+(p-source));
}
MagickExport MagickBooleanType ConcatenateString(char **destination,
const char *source)
{
size_t
destination_length,
length,
source_length;
assert(destination != (char **) NULL);
if (source == (const char *) NULL)
return(MagickTrue);
if (*destination == (char *) NULL)
{
*destination=AcquireString(source);
return(MagickTrue);
}
destination_length=strlen(*destination);
source_length=strlen(source);
length=destination_length;
if (~length < source_length)
ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
length+=source_length;
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
*destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
sizeof(**destination));
if (*destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
if (source_length != 0)
(void) memcpy((*destination)+destination_length,source,source_length);
(*destination)[length]='\0';
return(MagickTrue);
}
MagickExport void ConcatenateStringInfo(StringInfo *string_info,
const StringInfo *source)
{
size_t
length;
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
assert(source != (const StringInfo *) NULL);
length=string_info->length;
if (~length < source->length)
ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
SetStringInfoLength(string_info,length+source->length);
(void) memcpy(string_info->datum+length,source->datum,source->length);
}
MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
{
char
*string;
int
file;
MagickOffsetType
offset;
size_t
length;
StringInfo
*string_info;
void
*map;
assert(filename != (const char *) NULL);
file=open_utf8(filename,O_RDONLY | O_BINARY,0);
if (file == -1)
return((StringInfo *) NULL);
offset=(MagickOffsetType) lseek(file,0,SEEK_END);
if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
{
file=close(file)-1;
return((StringInfo *) NULL);
}
length=(size_t) offset;
string=(char *) NULL;
if (~length >= (MaxTextExtent-1))
string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
if (string == (char *) NULL)
{
file=close(file)-1;
return((StringInfo *) NULL);
}
map=MapBlob(file,ReadMode,0,length);
if (map != (void *) NULL)
{
(void) memcpy(string,map,length);
(void) UnmapBlob(map,length);
}
else
{
register size_t
i;
ssize_t
count;
(void) lseek(file,0,SEEK_SET);
for (i=0; i < length; i+=count)
{
count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
SSIZE_MAX));
if (count <= 0)
{
count=0;
if (errno != EINTR)
break;
}
}
if (i < length)
{
file=close(file)-1;
string=DestroyString(string);
return((StringInfo *) NULL);
}
}
string[length]='\0';
file=close(file)-1;
string_info=AcquireStringInfo(0);
(void) CopyMagickString(string_info->path,filename,MaxTextExtent);
string_info->length=length;
if (string_info->datum != (unsigned char *) NULL)
string_info->datum=(unsigned char *) RelinquishMagickMemory(
string_info->datum);
string_info->datum=(unsigned char *) string;
return(string_info);
}
MagickExport char *ConstantString(const char *source)
{
char
*destination;
size_t
length;
length=0;
if (source != (char *) NULL)
length+=strlen(source);
destination=(char *) NULL;
if (~length >= 1UL)
destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
if (destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
*destination='\0';
if (source != (char *) NULL)
(void) memcpy(destination,source,length*sizeof(*destination));
destination[length]='\0';
return(destination);
}
MagickExport size_t CopyMagickString(char *destination,const char *source,
const size_t length)
{
register char
*q;
register const char
*p;
register size_t
n;
if (source == (const char *) NULL)
return(0);
p=source;
q=destination;
for (n=length; n > 4; n-=4)
{
*q=(*p++);
if (*q == '\0')
return((size_t) (p-source-1));
q++;
*q=(*p++);
if (*q == '\0')
return((size_t) (p-source-1));
q++;
*q=(*p++);
if (*q == '\0')
return((size_t) (p-source-1));
q++;
*q=(*p++);
if (*q == '\0')
return((size_t) (p-source-1));
q++;
}
if (n != 0)
for (n--; n != 0; n--)
{
*q=(*p++);
if (*q == '\0')
return((size_t) (p-source-1));
q++;
}
if (length != 0)
*q='\0';
return((size_t) (p-source-1));
}
MagickExport char *DestroyString(char *string)
{
return((char *) RelinquishMagickMemory(string));
}
MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
if (string_info->datum != (unsigned char *) NULL)
string_info->datum=(unsigned char *) RelinquishMagickMemory(
string_info->datum);
string_info->signature=(~MagickSignature);
string_info=(StringInfo *) RelinquishMagickMemory(string_info);
return(string_info);
}
MagickExport char **DestroyStringList(char **list)
{
register ssize_t
i;
assert(list != (char **) NULL);
for (i=0; list[i] != (char *) NULL; i++)
list[i]=DestroyString(list[i]);
list=(char **) RelinquishMagickMemory(list);
return(list);
}
MagickExport char *EscapeString(const char *source,const char escape)
{
char
*destination;
register char
*q;
register const char
*p;
size_t
length;
assert(source != (const char *) NULL);
length=strlen(source);
for (p=source; *p != '\0'; p++)
if ((*p == '\\') || (*p == escape))
{
if (~length < 1)
ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
length++;
}
destination=(char *) NULL;
if (~length >= (MaxTextExtent-1))
destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*destination));
if (destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
*destination='\0';
q=destination;
for (p=source; *p != '\0'; p++)
{
if ((*p == '\\') || (*p == escape))
*q++='\\';
*q++=(*p);
}
*q='\0';
return(destination);
}
MagickExport char *FileToString(const char *filename,const size_t extent,
ExceptionInfo *exception)
{
size_t
length;
assert(filename != (const char *) NULL);
assert(exception != (ExceptionInfo *) NULL);
return((char *) FileToBlob(filename,extent,&length,exception));
}
MagickExport StringInfo *FileToStringInfo(const char *filename,
const size_t extent,ExceptionInfo *exception)
{
StringInfo
*string_info;
assert(filename != (const char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
assert(exception != (ExceptionInfo *) NULL);
string_info=AcquireStringInfo(0);
(void) CopyMagickString(string_info->path,filename,MaxTextExtent);
if (string_info->datum != (unsigned char *) NULL)
string_info->datum=(unsigned char *) RelinquishMagickMemory(
string_info->datum);
string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
if (string_info->datum == (unsigned char *) NULL)
{
string_info=DestroyStringInfo(string_info);
return((StringInfo *) NULL);
}
return(string_info);
}
MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
const MagickBooleanType bi,char *format)
{
const char
**units;
double
bytes,
length;
register ssize_t
i,
j;
ssize_t
count;
static const char
*bi_units[] =
{
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
},
*traditional_units[] =
{
"", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
};
bytes=1000.0;
units=traditional_units;
if (bi != MagickFalse)
{
bytes=1024.0;
units=bi_units;
}
#if defined(_MSC_VER) && (_MSC_VER == 1200)
length=(double) ((MagickOffsetType) size);
#else
length=(double) size;
#endif
for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
length/=bytes;
count=0;
for (j=2; j < 12; j++)
{
count=FormatLocaleString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
units[i]);
if (strchr(format,'+') == (char *) NULL)
break;
}
return(count);
}
MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
char *timestamp)
{
ssize_t
count;
struct tm
gm_time,
local_time;
time_t
timezone;
assert(timestamp != (char *) NULL);
(void) ResetMagickMemory(&local_time,0,sizeof(local_time));
(void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
(void) localtime_r(&time,&local_time);
#else
{
struct tm
*my_time;
my_time=localtime(&time);
if (my_time != (struct tm *) NULL)
(void) memcpy(&local_time,my_time,sizeof(local_time));
}
#endif
#if defined(MAGICKCORE_HAVE_GMTIME_R)
(void) gmtime_r(&time,&gm_time);
#else
{
struct tm
*my_time;
my_time=gmtime(&time);
if (my_time != (struct tm *) NULL)
(void) memcpy(&gm_time,my_time,sizeof(gm_time));
}
#endif
timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
(local_time.tm_yday-gm_time.tm_yday)));
count=FormatLocaleString(timestamp,length,
"%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
local_time.tm_min,local_time.tm_sec,(long) timezone);
return(count);
}
MagickExport char *GetEnvironmentValue(const char *name)
{
const char
*environment;
environment=getenv(name);
if (environment == (const char *) NULL)
return((char *) NULL);
return(ConstantString(environment));
}
MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
return(string_info->datum);
}
MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
return(string_info->length);
}
MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
return(string_info->path);
}
MagickExport double InterpretSiPrefixValue(const char *magick_restrict string,
char **magick_restrict sentinal)
{
char
*q;
double
value;
value=InterpretLocaleValue(string,&q);
if (q != string)
{
if ((*q >= 'E') && (*q <= 'z'))
{
double
e;
switch ((int) ((unsigned char) *q))
{
case 'y': e=(-24.0); break;
case 'z': e=(-21.0); break;
case 'a': e=(-18.0); break;
case 'f': e=(-15.0); break;
case 'p': e=(-12.0); break;
case 'n': e=(-9.0); break;
case 'u': e=(-6.0); break;
case 'm': e=(-3.0); break;
case 'c': e=(-2.0); break;
case 'd': e=(-1.0); break;
case 'h': e=2.0; break;
case 'k': e=3.0; break;
case 'K': e=3.0; break;
case 'M': e=6.0; break;
case 'G': e=9.0; break;
case 'T': e=12.0; break;
case 'P': e=15.0; break;
case 'E': e=18.0; break;
case 'Z': e=21.0; break;
case 'Y': e=24.0; break;
default: e=0.0; break;
}
if (e >= MagickEpsilon)
{
if (q[1] == 'i')
{
value*=pow(2.0,e/0.3);
q+=2;
}
else
{
value*=pow(10.0,e);
q++;
}
}
}
if ((*q == 'B') || (*q == 'P'))
q++;
}
if (sentinal != (char **) NULL)
*sentinal=q;
return(value);
}
MagickExport MagickBooleanType IsStringTrue(const char *value)
{
if (value == (const char *) NULL)
return(MagickFalse);
if (LocaleCompare(value,"true") == 0)
return(MagickTrue);
if (LocaleCompare(value,"on") == 0)
return(MagickTrue);
if (LocaleCompare(value,"yes") == 0)
return(MagickTrue);
if (LocaleCompare(value,"1") == 0)
return(MagickTrue);
return(MagickFalse);
}
MagickExport MagickBooleanType IsStringNotFalse(const char *value)
{
if (value == (const char *) NULL)
return(MagickTrue);
if (LocaleCompare(value,"false") == 0)
return(MagickFalse);
if (LocaleCompare(value,"off") == 0)
return(MagickFalse);
if (LocaleCompare(value,"no") == 0)
return(MagickFalse);
if (LocaleCompare(value,"0") == 0)
return(MagickFalse);
return(MagickTrue);
}
MagickExport void PrintStringInfo(FILE *file,const char *id,
const StringInfo *string_info)
{
register const char
*p;
register size_t
i,
j;
assert(id != (const char *) NULL);
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
p=(char *) string_info->datum;
for (i=0; i < string_info->length; i++)
{
if (((int) ((unsigned char) *p) < 32) &&
(isspace((int) ((unsigned char) *p)) == 0))
break;
p++;
}
(void) FormatLocaleFile(file,"%s(%.20g): ",id,(double) string_info->length);
if (i == string_info->length)
{
for (i=0; i < string_info->length; i++)
(void) fputc(string_info->datum[i],file);
(void) fputc('\n',file);
return;
}
p=(char *) string_info->datum;
for (i=0; i < string_info->length; i+=0x14)
{
(void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
{
(void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
if ((j % 0x04) == 0)
(void) fputc(' ',file);
}
for ( ; j <= 0x14; j++)
{
(void) fputc(' ',file);
(void) fputc(' ',file);
if ((j % 0x04) == 0)
(void) fputc(' ',file);
}
(void) fputc(' ',file);
for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
{
if (isprint((int) ((unsigned char) *p)) != 0)
(void) fputc(*p,file);
else
(void) fputc('-',file);
p++;
}
(void) fputc('\n',file);
}
}
MagickExport void ResetStringInfo(StringInfo *string_info)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
(void) ResetMagickMemory(string_info->datum,0,string_info->length);
}
MagickExport void SetStringInfo(StringInfo *string_info,
const StringInfo *source)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
assert(source != (StringInfo *) NULL);
assert(source->signature == MagickSignature);
if (string_info->length == 0)
return;
(void) ResetMagickMemory(string_info->datum,0,string_info->length);
(void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
source->length));
}
MagickExport void SetStringInfoDatum(StringInfo *string_info,
const unsigned char *source)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
if (string_info->length != 0)
(void) memcpy(string_info->datum,source,string_info->length);
}
MagickExport void SetStringInfoLength(StringInfo *string_info,
const size_t length)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
string_info->length=length;
if (string_info->datum == (unsigned char *) NULL)
string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
MaxTextExtent,sizeof(*string_info->datum));
else
string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
length+MaxTextExtent,sizeof(*string_info->datum));
if (string_info->datum == (unsigned char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
}
MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
{
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
assert(path != (const char *) NULL);
(void) CopyMagickString(string_info->path,path,MaxTextExtent);
}
MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
const size_t offset)
{
StringInfo
*split_info;
assert(string_info != (StringInfo *) NULL);
assert(string_info->signature == MagickSignature);
if (offset > string_info->length)
return((StringInfo *) NULL);
split_info=AcquireStringInfo(offset);
SetStringInfo(split_info,string_info);
(void) memmove(string_info->datum,string_info->datum+offset,
string_info->length-offset+MaxTextExtent);
SetStringInfoLength(string_info,string_info->length-offset);
return(split_info);
}
MagickExport char *StringInfoToString(const StringInfo *string_info)
{
char
*string;
size_t
length;
string=(char *) NULL;
length=string_info->length;
if (~length >= (MaxTextExtent-1))
string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
if (string == (char *) NULL)
return((char *) NULL);
(void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
string[length]='\0';
return(string);
}
MagickExport char *StringInfoToHexString(const StringInfo *string_info)
{
char
*string;
register const unsigned char
*p;
register ssize_t
i;
register unsigned char
*q;
size_t
length;
unsigned char
hex_digits[16];
length=string_info->length;
if (~length < MaxTextExtent)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
if (string == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
hex_digits[0]='0';
hex_digits[1]='1';
hex_digits[2]='2';
hex_digits[3]='3';
hex_digits[4]='4';
hex_digits[5]='5';
hex_digits[6]='6';
hex_digits[7]='7';
hex_digits[8]='8';
hex_digits[9]='9';
hex_digits[10]='a';
hex_digits[11]='b';
hex_digits[12]='c';
hex_digits[13]='d';
hex_digits[14]='e';
hex_digits[15]='f';
p=string_info->datum;
q=(unsigned char *) string;
for (i=0; i < (ssize_t) string_info->length; i++)
{
*q++=hex_digits[(*p >> 4) & 0x0f];
*q++=hex_digits[*p & 0x0f];
p++;
}
*q='\0';
return(string);
}
MagickExport char **StringToArgv(const char *text,int *argc)
{
char
**argv;
register const char
*p,
*q;
register ssize_t
i;
*argc=0;
if (text == (char *) NULL)
return((char **) NULL);
for (p=text; *p != '\0'; )
{
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
if (*p == '\0')
break;
(*argc)++;
if (*p == '"')
for (p++; (*p != '"') && (*p != '\0'); p++) ;
if (*p == '\'')
for (p++; (*p != '\'') && (*p != '\0'); p++) ;
while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
p++;
}
(*argc)++;
argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
if (argv == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
argv[0]=AcquireString("magick");
p=text;
for (i=1; i < (ssize_t) *argc; i++)
{
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
q=p;
if (*q == '"')
{
p++;
for (q++; (*q != '"') && (*q != '\0'); q++) ;
}
else
if (*q == '\'')
{
p++;
for (q++; (*q != '\'') && (*q != '\0'); q++) ;
}
else
while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
q++;
argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
sizeof(**argv));
if (argv[i] == (char *) NULL)
{
for (i--; i >= 0; i--)
argv[i]=DestroyString(argv[i]);
argv=(char **) RelinquishMagickMemory(argv);
ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
}
(void) memcpy(argv[i],p,(size_t) (q-p));
argv[i][q-p]='\0';
p=q;
while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
p++;
}
argv[i]=(char *) NULL;
return(argv);
}
MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
ExceptionInfo *exception)
{
char
*q;
const char
*p;
double
*array;
register ssize_t
i;
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
*count=0;
i=0;
p=string;
while (*p != '\0')
{
(void) StringToDouble(p,&q);
if (p == q)
return((double *) NULL);
p=q;
i++;
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
if (*p == ',')
p++;
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
}
*count=i;
array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
if (array == (double *) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),
ResourceLimitError,"MemoryAllocationFailed","`%s'","");
return((double *) NULL);
}
i=0;
p=string;
while ((*p != '\0') && (i < *count))
{
array[i++]=StringToDouble(p,&q);
p=q;
while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
p++;
}
return(array);
}
MagickExport char *StringToken(const char *delimiters,char **string)
{
char
*q;
register char
*p;
register const char
*r;
register int
c,
d;
p=(*string);
if (p == (char *) NULL)
return((char *) NULL);
q=p;
for ( ; ; )
{
c=(*p++);
r=delimiters;
do
{
d=(*r++);
if (c == d)
{
if (c == '\0')
p=(char *) NULL;
else
p[-1]='\0';
*string=p;
return(q);
}
} while (d != '\0');
}
}
MagickExport char **StringToList(const char *text)
{
char
**textlist;
register const char
*p;
register ssize_t
i;
size_t
lines;
if (text == (char *) NULL)
return((char **) NULL);
for (p=text; *p != '\0'; p++)
if (((int) ((unsigned char) *p) < 32) &&
(isspace((int) ((unsigned char) *p)) == 0))
break;
if (*p == '\0')
{
register const char
*q;
lines=1;
for (p=text; *p != '\0'; p++)
if (*p == '\n')
lines++;
textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
sizeof(*textlist));
if (textlist == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
p=text;
for (i=0; i < (ssize_t) lines; i++)
{
for (q=p; *q != '\0'; q++)
if ((*q == '\r') || (*q == '\n'))
break;
textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
sizeof(**textlist));
if (textlist[i] == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
(void) memcpy(textlist[i],p,(size_t) (q-p));
textlist[i][q-p]='\0';
if (*q == '\r')
q++;
p=q+1;
}
}
else
{
char
hex_string[MaxTextExtent];
register char
*q;
register ssize_t
j;
lines=(size_t) (strlen(text)/0x14)+1;
textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
sizeof(*textlist));
if (textlist == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
p=text;
for (i=0; i < (ssize_t) lines; i++)
{
textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
sizeof(**textlist));
if (textlist[i] == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
(void) FormatLocaleString(textlist[i],MaxTextExtent,"0x%08lx: ",
(long) (0x14*i));
q=textlist[i]+strlen(textlist[i]);
for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
{
(void) FormatLocaleString(hex_string,MaxTextExtent,"%02x",*(p+j));
(void) CopyMagickString(q,hex_string,MaxTextExtent);
q+=2;
if ((j % 0x04) == 0)
*q++=' ';
}
for ( ; j <= 0x14; j++)
{
*q++=' ';
*q++=' ';
if ((j % 0x04) == 0)
*q++=' ';
}
*q++=' ';
for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
{
if (isprint((int) ((unsigned char) *p)) != 0)
*q++=(*p);
else
*q++='-';
p++;
}
*q='\0';
}
}
textlist[i]=(char *) NULL;
return(textlist);
}
MagickExport StringInfo *StringToStringInfo(const char *string)
{
StringInfo
*string_info;
assert(string != (const char *) NULL);
string_info=AcquireStringInfo(strlen(string));
SetStringInfoDatum(string_info,(const unsigned char *) string);
return(string_info);
}
MagickExport void StripString(char *message)
{
register char
*p,
*q;
size_t
length;
assert(message != (char *) NULL);
if (*message == '\0')
return;
length=strlen(message);
p=message;
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
if ((*p == '\'') || (*p == '"'))
p++;
q=message+length-1;
while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
q--;
if (q > p)
if ((*q == '\'') || (*q == '"'))
q--;
(void) memmove(message,p,(size_t) (q-p+1));
message[q-p+1]='\0';
for (p=message; *p != '\0'; p++)
if (*p == '\n')
*p=' ';
}
MagickExport MagickBooleanType SubstituteString(char **string,
const char *search,const char *replace)
{
MagickBooleanType
status;
register char
*p;
size_t
extent,
replace_extent,
search_extent;
ssize_t
offset;
status=MagickFalse;
search_extent=0,
replace_extent=0;
for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
{
if (search_extent == 0)
search_extent=strlen(search);
if (strncmp(p,search,search_extent) != 0)
continue;
status=MagickTrue;
if (replace_extent == 0)
replace_extent=strlen(replace);
if (replace_extent > search_extent)
{
offset=(ssize_t) (p-(*string));
extent=strlen(*string)+replace_extent-search_extent+1;
*string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
sizeof(*p));
if (*string == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
p=(*string)+offset;
}
if (search_extent != replace_extent)
(void) CopyMagickMemory(p+replace_extent,p+search_extent,
strlen(p+search_extent)+1);
(void) CopyMagickMemory(p,replace,replace_extent);
p+=replace_extent-1;
}
return(status);
}