41#include "magick/studio.h"
42#include "magick/blob.h"
43#include "magick/blob-private.h"
44#include "magick/exception.h"
45#include "magick/exception-private.h"
46#include "magick/image-private.h"
47#include "magick/list.h"
48#include "magick/locale_.h"
49#include "magick/log.h"
50#include "magick/memory_.h"
51#include "magick/memory-private.h"
52#include "magick/nt-base.h"
53#include "magick/nt-base-private.h"
54#include "magick/policy.h"
55#include "magick/property.h"
56#include "magick/resource_.h"
57#include "magick/signature-private.h"
58#include "magick/string_.h"
59#include "magick/string-private.h"
60#include "magick/utility-private.h"
65#define CharsPerLine 0x14
70#if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
71static const unsigned char
74 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
75 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
76 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
77 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
78 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
79 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
80 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
81 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
82 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
83 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
84 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
85 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
86 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
87 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
88 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
89 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
90 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
91 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
92 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
93 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
94 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
95 0xfc, 0xfd, 0xfe, 0xff,
127MagickExport
char *AcquireString(
const char *source)
136 if (source != (
char *) NULL)
137 length+=strlen(source);
138 if (~length < MaxTextExtent)
139 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
140 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
141 sizeof(*destination));
142 if (destination == (
char *) NULL)
143 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
144 if (source != (
char *) NULL)
145 (void) memcpy(destination,source,length*
sizeof(*destination));
146 destination[length]=
'\0';
173static StringInfo *AcquireStringInfoContainer(
void)
178 string_info=(StringInfo *) AcquireMagickMemory(
sizeof(*string_info));
179 if (string_info == (StringInfo *) NULL)
180 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
181 (void) memset(string_info,0,
sizeof(*string_info));
182 string_info->signature=MagickCoreSignature;
186MagickExport StringInfo *AcquireStringInfo(
const size_t length)
191 string_info=AcquireStringInfoContainer();
192 string_info->length=length;
193 if (~string_info->length >= (MaxTextExtent-1))
194 string_info->datum=(
unsigned char *) AcquireQuantumMemory(
195 string_info->length+MaxTextExtent,
sizeof(*string_info->datum));
196 if (string_info->datum == (
unsigned char *) NULL)
197 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
198 (void) memset(string_info->datum,0,(length+MagickPathExtent)*
199 sizeof(*string_info->datum));
228MagickExport StringInfo *BlobToStringInfo(
const void *blob,
const size_t length)
233 if (~length < MaxTextExtent)
234 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
235 string_info=AcquireStringInfoContainer();
236 string_info->length=length;
237 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
238 MaxTextExtent,
sizeof(*string_info->datum));
239 if (string_info->datum == (
unsigned char *) NULL)
241 string_info=DestroyStringInfo(string_info);
242 return((StringInfo *) NULL);
244 if (blob != (
const void *) NULL)
245 (void) memcpy(string_info->datum,blob,length);
247 (
void) memset(string_info->datum,0,length*
sizeof(*string_info->datum));
248 (void) memset(string_info->datum+length,0,MagickPathExtent*
249 sizeof(*string_info->datum));
285MagickExport
char *CloneString(
char **destination,
const char *source)
290 assert(destination != (
char **) NULL);
291 if (source == (
const char *) NULL)
293 if (*destination != (
char *) NULL)
294 *destination=DestroyString(*destination);
295 return(*destination);
297 if (*destination == (
char *) NULL)
299 *destination=AcquireString(source);
300 return(*destination);
302 length=strlen(source);
303 if (~length < MaxTextExtent)
304 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
305 *destination=(
char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
306 sizeof(**destination));
307 if (*destination == (
char *) NULL)
308 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
310 (void) memcpy(*destination,source,length*
sizeof(**destination));
311 (*destination)[length]=
'\0';
312 return(*destination);
337MagickExport StringInfo *CloneStringInfo(
const StringInfo *string_info)
342 assert(string_info != (StringInfo *) NULL);
343 assert(string_info->signature == MagickCoreSignature);
344 clone_info=AcquireStringInfo(string_info->length);
345 (void) CopyMagickString(clone_info->path,string_info->path,MagickPathExtent);
346 (void) CloneString(&clone_info->name,string_info->name);
347 if (string_info->length != 0)
348 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
379MagickExport
int CompareStringInfo(
const StringInfo *target,
380 const StringInfo *source)
385 assert(target != (StringInfo *) NULL);
386 assert(target->signature == MagickCoreSignature);
387 assert(source != (StringInfo *) NULL);
388 assert(source->signature == MagickCoreSignature);
389 status=memcmp(target->datum,source->datum,MagickMin(target->length,
393 if (target->length == source->length)
395 return(target->length < source->length ? -1 : 1);
427MagickExport
size_t ConcatenateMagickString(
char *magick_restrict destination,
428 const char *magick_restrict source,
const size_t length)
442 assert(destination != (
char *) NULL);
443 assert(source != (
const char *) NULL);
448 while ((i-- != 0) && (*q !=
'\0'))
450 count=(size_t) (q-destination);
453 return(count+strlen(p));
464 return(count+(p-source));
493MagickExport MagickBooleanType ConcatenateString(
494 char **magick_restrict destination,
const char *magick_restrict source)
501 assert(destination != (
char **) NULL);
502 if (source == (
const char *) NULL)
504 if (*destination == (
char *) NULL)
506 *destination=AcquireString(source);
509 destination_length=strlen(*destination);
510 source_length=strlen(source);
511 length=destination_length;
512 if (~length < source_length)
513 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
514 length+=source_length;
515 if (~length < MaxTextExtent)
516 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
517 *destination=(
char *) ResizeQuantumMemory(*destination,
518 OverAllocateMemory(length+MaxTextExtent),
sizeof(**destination));
519 if (*destination == (
char *) NULL)
520 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
521 if (source_length != 0)
522 (void) memcpy((*destination)+destination_length,source,source_length);
523 (*destination)[length]=
'\0';
553MagickExport
void ConcatenateStringInfo(StringInfo *string_info,
554 const StringInfo *source)
559 assert(string_info != (StringInfo *) NULL);
560 assert(string_info->signature == MagickCoreSignature);
561 assert(source != (
const StringInfo *) NULL);
562 length=string_info->length;
563 if (~length < source->length)
564 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
565 length+=source->length;
566 if (~length < MagickPathExtent)
567 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
568 if (string_info->datum == (
unsigned char *) NULL)
569 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
570 MagickPathExtent,
sizeof(*string_info->datum));
572 string_info->datum=(
unsigned char *) ResizeQuantumMemory(
573 string_info->datum,OverAllocateMemory(length+MagickPathExtent),
574 sizeof(*string_info->datum));
575 if (string_info->datum == (
unsigned char *) NULL)
576 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
577 (void) memcpy(string_info->datum+string_info->length,source->datum,source->length);
578 string_info->length=length;
605MagickExport StringInfo *ConfigureFileToStringInfo(
const char *filename)
625 assert(filename != (
const char *) NULL);
626 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
628 return((StringInfo *) NULL);
629 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
630 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
632 file=close_utf8(file)-1;
633 return((StringInfo *) NULL);
635 length=(size_t) offset;
636 string=(
char *) NULL;
637 if (~length >= (MaxTextExtent-1))
638 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*
string));
639 if (
string == (
char *) NULL)
641 file=close_utf8(file)-1;
642 return((StringInfo *) NULL);
644 map=MapBlob(file,ReadMode,0,length);
645 if (map != (
void *) NULL)
647 (void) memcpy(
string,map,length);
648 (void) UnmapBlob(map,length);
658 (void) lseek(file,0,SEEK_SET);
659 for (i=0; i < length; i+=count)
661 count=read(file,
string+i,(
size_t) MagickMin(length-i,(
size_t)
662 MagickMaxBufferExtent));
672 file=close_utf8(file)-1;
673 string=DestroyString(
string);
674 return((StringInfo *) NULL);
678 file=close_utf8(file)-1;
679 string_info=AcquireStringInfoContainer();
680 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
681 string_info->length=length;
682 string_info->datum=(
unsigned char *)
string;
712MagickExport
char *ConstantString(
const char *source)
721 if (source != (
char *) NULL)
722 length+=strlen(source);
723 destination=(
char *) NULL;
725 destination=(
char *) AcquireQuantumMemory(length+1UL,
sizeof(*destination));
726 if (destination == (
char *) NULL)
727 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
728 if (source != (
char *) NULL)
729 (void) memcpy(destination,source,length*
sizeof(*destination));
730 destination[length]=
'\0';
765MagickExport
size_t CopyMagickString(
char *magick_restrict destination,
766 const char *magick_restrict source,
const size_t length)
779 for (n=length; n > 4; n-=4)
781 if (((*q++)=(*p++)) ==
'\0')
782 return((
size_t) (p-source-1));
783 if (((*q++)=(*p++)) ==
'\0')
784 return((
size_t) (p-source-1));
785 if (((*q++)=(*p++)) ==
'\0')
786 return((
size_t) (p-source-1));
787 if (((*q++)=(*p++)) ==
'\0')
788 return((
size_t) (p-source-1));
793 if (((*q++)=(*p++)) ==
'\0')
794 return((
size_t) (p-source-1));
797 return((
size_t) (p-source));
822MagickExport
char *DestroyString(
char *
string)
824 return((
char *) RelinquishMagickMemory(
string));
849MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
851 assert(string_info != (StringInfo *) NULL);
852 assert(string_info->signature == MagickCoreSignature);
853 if (string_info->datum != (
unsigned char *) NULL)
854 string_info->datum=(
unsigned char *) RelinquishMagickMemory(
856 if (string_info->name != (
char *) NULL)
857 string_info->name=DestroyString(string_info->name);
858 string_info->signature=(~MagickCoreSignature);
859 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
885MagickExport
char **DestroyStringList(
char **list)
890 assert(list != (
char **) NULL);
891 for (i=0; list[i] != (
char *) NULL; i++)
892 list[i]=DestroyString(list[i]);
893 list=(
char **) RelinquishMagickMemory(list);
926MagickExport
char *EscapeString(
const char *source,
const char escape)
940 assert(source != (
const char *) NULL);
942 for (p=source; *p !=
'\0'; p++)
944 if ((*p ==
'\\') || (*p == escape))
947 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
952 destination=(
char *) NULL;
953 if (~length >= (MaxTextExtent-1))
954 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
955 sizeof(*destination));
956 if (destination == (
char *) NULL)
957 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
960 for (p=source; *p !=
'\0'; p++)
962 if ((*p ==
'\\') || (*p == escape))
997MagickExport
char *FileToString(
const char *filename,
const size_t extent,
998 ExceptionInfo *exception)
1006 assert(filename != (
const char *) NULL);
1007 assert(exception != (ExceptionInfo *) NULL);
1008 if (IsEventLogging() != MagickFalse)
1009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1011 if ((*filename ==
'@') && (strlen(filename) > 1))
1016 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1017 if (status == MagickFalse)
1020 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1021 "NotAuthorized",
"`%s'",filename);
1022 return((
char *) NULL);
1026 return((
char *) FileToBlob(p,extent,&length,exception));
1056MagickExport StringInfo *FileToStringInfo(
const char *filename,
1057 const size_t extent,ExceptionInfo *exception)
1062 assert(filename != (
const char *) NULL);
1063 assert(exception != (ExceptionInfo *) NULL);
1064 if (IsEventLogging() != MagickFalse)
1065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1066 string_info=AcquireStringInfoContainer();
1067 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
1068 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
1069 if (string_info->datum == (
unsigned char *) NULL)
1071 string_info=DestroyStringInfo(string_info);
1072 return((StringInfo *) NULL);
1074 return(string_info);
1106MagickExport ssize_t FormatMagickSize(
const MagickSizeType size,
1107 const MagickBooleanType bi,
char *format)
1125 "",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei",
"Zi",
"Yi",
"Ri",
"Qi", (
char *) NULL
1127 *traditional_units[] =
1129 "",
"K",
"M",
"G",
"T",
"P",
"E",
"Z",
"Y",
"R",
"Q", (
char *) NULL
1133 units=traditional_units;
1134 if (bi != MagickFalse)
1139#if defined(_MSC_VER) && (_MSC_VER == 1200)
1140 length=(double) ((MagickOffsetType) size);
1142 length=(double) size;
1144 (void) FormatLocaleString(format,MaxTextExtent,
"%.*g",GetMagickPrecision(),
1146 if (strstr(format,
"e+") == (
char *) NULL)
1148 count=FormatLocaleString(format,MaxTextExtent,
"%.20g%sB",length,units[0]);
1151 for (i=0; (length >= bytes) && (units[i+1] != (
const char *) NULL); i++)
1153 count=FormatLocaleString(format,MaxTextExtent,
"%.*g%sB",GetMagickPrecision(),
1181MagickExport
char *GetEnvironmentValue(
const char *name)
1183#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1184 return(NTGetEnvironmentValue(name));
1189 environment=getenv(name);
1190 if (environment == (
const char *) NULL)
1191 return((
char *) NULL);
1192 return(ConstantString(environment));
1218MagickExport
unsigned char *GetStringInfoDatum(
const StringInfo *string_info)
1220 assert(string_info != (StringInfo *) NULL);
1221 assert(string_info->signature == MagickCoreSignature);
1222 return(string_info->datum);
1247MagickExport
size_t GetStringInfoLength(
const StringInfo *string_info)
1249 assert(string_info != (StringInfo *) NULL);
1250 assert(string_info->signature == MagickCoreSignature);
1251 return(string_info->length);
1276MagickExport
const char *GetStringInfoName(
const StringInfo *string_info)
1278 assert(string_info != (StringInfo *) NULL);
1279 assert(string_info->signature == MagickCoreSignature);
1280 return(string_info->name);
1305MagickExport
const char *GetStringInfoPath(
const StringInfo *string_info)
1307 assert(string_info != (StringInfo *) NULL);
1308 assert(string_info->signature == MagickCoreSignature);
1309 return(string_info->path);
1339MagickExport
double InterpretSiPrefixValue(
const char *magick_restrict
string,
1340 char **magick_restrict sentinel)
1348 value=InterpretLocaleValue(
string,&q);
1351 if ((*q >=
'E') && (*q <=
'z'))
1356 switch ((
int) ((
unsigned char) *q))
1358 case 'q': e=(-30.0);
break;
1359 case 'r': e=(-27.0);
break;
1360 case 'y': e=(-24.0);
break;
1361 case 'z': e=(-21.0);
break;
1362 case 'a': e=(-18.0);
break;
1363 case 'f': e=(-15.0);
break;
1364 case 'p': e=(-12.0);
break;
1365 case 'n': e=(-9.0);
break;
1366 case 'u': e=(-6.0);
break;
1367 case 'm': e=(-3.0);
break;
1368 case 'c': e=(-2.0);
break;
1369 case 'd': e=(-1.0);
break;
1370 case 'h': e=2.0;
break;
1371 case 'k': e=3.0;
break;
1372 case 'K': e=3.0;
break;
1373 case 'M': e=6.0;
break;
1374 case 'G': e=9.0;
break;
1375 case 'T': e=12.0;
break;
1376 case 'P': e=15.0;
break;
1377 case 'E': e=18.0;
break;
1378 case 'Z': e=21.0;
break;
1379 case 'Y': e=24.0;
break;
1380 case 'R': e=27.0;
break;
1381 case 'Q': e=30.0;
break;
1382 default: e=0.0;
break;
1384 if (e >= MagickEpsilon)
1388 value*=pow(2.0,e/0.3);
1398 if ((*q ==
'B') || (*q ==
'P'))
1401 if (sentinel != (
char **) NULL)
1432MagickExport MagickBooleanType IsStringTrue(
const char *value)
1434 if (value == (
const char *) NULL)
1435 return(MagickFalse);
1436 if (LocaleCompare(value,
"true") == 0)
1438 if (LocaleCompare(value,
"on") == 0)
1440 if (LocaleCompare(value,
"yes") == 0)
1442 if (LocaleCompare(value,
"1") == 0)
1444 return(MagickFalse);
1474MagickExport MagickBooleanType IsStringNotFalse(
const char *value)
1476 if (value == (
const char *) NULL)
1478 if (LocaleCompare(value,
"false") == 0)
1479 return(MagickFalse);
1480 if (LocaleCompare(value,
"off") == 0)
1481 return(MagickFalse);
1482 if (LocaleCompare(value,
"no") == 0)
1483 return(MagickFalse);
1484 if (LocaleCompare(value,
"0") == 0)
1485 return(MagickFalse);
1516MagickExport
void PrintStringInfo(FILE *file,
const char *
id,
1517 const StringInfo *string_info)
1529 assert(
id != (
const char *) NULL);
1530 assert(string_info != (StringInfo *) NULL);
1531 assert(string_info->signature == MagickCoreSignature);
1532 p=(
const unsigned char *) string_info->datum;
1533 for (i=0; i < string_info->length; i++)
1534 if ((p[i] < 32) && (isspace((
int)p[i]) == 0))
1536 (void) FormatLocaleFile(file,
"%s(%.20g):\n",
id,(
double) string_info->length);
1537 if (i == string_info->length)
1539 for (i = 0; i < string_info->length; i++)
1540 (
void) fputc(p[i],file);
1541 (void) fputc(
'\n',file);
1547 for (i=0; i < string_info->length; i+=CharsPerLine)
1549 (void) FormatLocaleFile(file,
"0x%08lx: ",(
unsigned long) i);
1550 for (j = 0; j < MagickMin(string_info->length-i, CharsPerLine); j++)
1552 (void) FormatLocaleFile(file,
"%02lx",(
unsigned long) (p[i+j]) & 0xff);
1553 if (((j+1) % 0x04) == 0)
1554 (void) fputc(
' ',file);
1559 for ( ; j < CharsPerLine; j++)
1561 (void) fputc(
' ',file);
1562 (void) fputc(
' ',file);
1563 if (((j+1) % 0x04) == 0)
1564 (void) fputc(
' ',file);
1566 (void) fputc(
' ',file);
1570 for (j=0; j < MagickMin(string_info->length-i,CharsPerLine); j++)
1572 unsigned char c = p[i+j];
1573 if (isprint((
int) c) != 0)
1574 (void) fputc(c,file);
1576 (
void) fputc(
'-',file);
1578 (void) fputc(
'\n',file);
1604MagickExport
void ResetStringInfo(StringInfo *string_info)
1606 assert(string_info != (StringInfo *) NULL);
1607 assert(string_info->signature == MagickCoreSignature);
1608 (void) memset(string_info->datum,0,string_info->length);
1636MagickExport
char *SanitizeString(
const char *source)
1649 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1650 "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1652 sanitize_source=AcquireString(source);
1654 q=sanitize_source+strlen(sanitize_source);
1655 for (p+=strspn(p,allowlist); p != q; p+=(ptrdiff_t) strspn(p,allowlist))
1657 return(sanitize_source);
1684MagickExport
void SetStringInfo(StringInfo *string_info,
1685 const StringInfo *source)
1687 assert(string_info != (StringInfo *) NULL);
1688 assert(string_info->signature == MagickCoreSignature);
1689 assert(source != (StringInfo *) NULL);
1690 assert(source->signature == MagickCoreSignature);
1691 if (string_info->length == 0)
1693 (void) memset(string_info->datum,0,string_info->length);
1694 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1724MagickExport
void SetStringInfoDatum(StringInfo *string_info,
1725 const unsigned char *source)
1727 assert(string_info != (StringInfo *) NULL);
1728 assert(string_info->signature == MagickCoreSignature);
1729 if (string_info->length != 0)
1730 (void) memcpy(string_info->datum,source,string_info->length);
1757MagickExport
void SetStringInfoLength(StringInfo *string_info,
1758 const size_t length)
1760 assert(string_info != (StringInfo *) NULL);
1761 assert(string_info->signature == MagickCoreSignature);
1762 if (string_info->length == length)
1764 if (~length < MaxTextExtent)
1765 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1766 string_info->length=length;
1767 if (string_info->datum == (
unsigned char *) NULL)
1768 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
1769 MaxTextExtent,
sizeof(*string_info->datum));
1771 string_info->datum=(
unsigned char *) ResizeQuantumMemory(string_info->datum,
1772 length+MaxTextExtent,
sizeof(*string_info->datum));
1773 if (string_info->datum == (
unsigned char *) NULL)
1774 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1801MagickExport
void SetStringInfoName(StringInfo *string_info,
const char *name)
1803 assert(string_info != (StringInfo *) NULL);
1804 assert(string_info->signature == MagickCoreSignature);
1805 assert(name != (
const char *) NULL);
1806 string_info->name=ConstantString(name);
1833MagickExport
void SetStringInfoPath(StringInfo *string_info,
const char *path)
1835 assert(string_info != (StringInfo *) NULL);
1836 assert(string_info->signature == MagickCoreSignature);
1837 assert(path != (
const char *) NULL);
1838 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1863MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1864 const size_t offset)
1869 assert(string_info != (StringInfo *) NULL);
1870 assert(string_info->signature == MagickCoreSignature);
1871 if (offset > string_info->length)
1872 return((StringInfo *) NULL);
1873 split_info=AcquireStringInfo(offset);
1874 SetStringInfo(split_info,string_info);
1875 (void) memmove(string_info->datum,string_info->datum+offset,
1876 string_info->length-offset+MaxTextExtent);
1877 SetStringInfoLength(string_info,string_info->length-offset);
1903MagickExport
char *StringInfoToString(
const StringInfo *string_info)
1911 string=(
char *) NULL;
1912 length=string_info->length;
1913 if (~length >= (MaxTextExtent-1))
1914 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*
string));
1915 if (
string == (
char *) NULL)
1916 return((
char *) NULL);
1917 (void) memcpy(
string,(
char *) string_info->datum,length*
sizeof(*
string));
1918 string[length]=
'\0';
1944MagickExport
char *StringInfoToHexString(
const StringInfo *string_info)
1964 length=string_info->length;
1965 if (~length < MaxTextExtent)
1966 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1967 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,2*
sizeof(*
string));
1968 if (
string == (
char *) NULL)
1969 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1986 p=string_info->datum;
1987 q=(
unsigned char *)
string;
1988 for (i=0; i < (ssize_t) string_info->length; i++)
1990 *q++=hex_digits[(*p >> 4) & 0x0f];
1991 *q++=hex_digits[*p & 0x0f];
2032MagickExport
char **StringToArgv(
const char *text,
int *argc)
2045 if (text == (
char *) NULL)
2046 return((
char **) NULL);
2050 for (p=text; *p !=
'\0'; )
2052 while (isspace((
int) ((
unsigned char) *p)) != 0)
2058 for (p++; (*p !=
'"') && (*p !=
'\0'); p++) ;
2060 for (p++; (*p !=
'\'') && (*p !=
'\0'); p++) ;
2061 while ((isspace((
int) ((
unsigned char) *p)) == 0) && (*p !=
'\0'))
2065 argv=(
char **) AcquireQuantumMemory((
size_t) (*argc+1UL),
sizeof(*argv));
2066 if (argv == (
char **) NULL)
2067 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
2071 argv[0]=AcquireString(
"magick");
2073 for (i=1; i < (ssize_t) *argc; i++)
2075 while (isspace((
int) ((
unsigned char) *p)) != 0)
2081 for (q++; (*q !=
'"') && (*q !=
'\0'); q++) ;
2087 for (q++; (*q !=
'\'') && (*q !=
'\0'); q++) ;
2090 while ((isspace((
int) ((
unsigned char) *q)) == 0) && (*q !=
'\0'))
2092 argv[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
2094 if (argv[i] == (
char *) NULL)
2096 for (i--; i >= 0; i--)
2097 argv[i]=DestroyString(argv[i]);
2098 argv=(
char **) RelinquishMagickMemory(argv);
2099 ThrowFatalException(ResourceLimitFatalError,
2100 "UnableToConvertStringToARGV");
2102 (void) memcpy(argv[i],p,(
size_t) (q-p));
2105 while ((isspace((
int) ((
unsigned char) *p)) == 0) && (*p !=
'\0'))
2108 argv[i]=(
char *) NULL;
2147MagickExport
double *StringToArrayOfDoubles(
const char *
string,ssize_t *count,
2148 ExceptionInfo *exception)
2165 assert(exception != (ExceptionInfo *) NULL);
2166 assert(exception->signature == MagickCoreSignature);
2168 if (
string == (
char *) NULL)
2169 return((
double *) NULL);
2174 (void) StringToDouble(p,&q);
2176 return((
double *) NULL);
2179 while (isspace((
int) ((
unsigned char) *p)) != 0)
2183 while (isspace((
int) ((
unsigned char) *p)) != 0)
2190 array=(
double *) AcquireQuantumMemory((
size_t) i,
sizeof(*array));
2191 if (array == (
double *) NULL)
2193 (void) ThrowMagickException(exception,GetMagickModule(),
2194 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2195 return((
double *) NULL);
2202 while ((*p !=
'\0') && (i < *count))
2204 array[i++]=StringToDouble(p,&q);
2206 while ((isspace((
int) ((
unsigned char) *p)) != 0) || (*p ==
','))
2246MagickExport
char *StringToken(
const char *delimiters,
char **
string)
2262 if (p == (
char *) NULL)
2263 return((
char *) NULL);
2281 }
while (d !=
'\0');
2309MagickExport
char **StringToList(
const char *text)
2311 return(StringToStrings(text,(
size_t *) NULL));
2340MagickExport
char **StringToStrings(
const char *text,
size_t *count)
2354 if (text == (
char *) NULL)
2356 if (count != (
size_t *) NULL)
2358 return((
char **) NULL);
2360 for (p=text; *p !=
'\0'; p++)
2361 if (((
int) ((
unsigned char) *p) < 32) &&
2362 (isspace((
int) ((
unsigned char) *p)) == 0))
2373 for (p=text; *p !=
'\0'; p++)
2376 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2378 if (textlist == (
char **) NULL)
2379 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2381 for (i=0; i < (ssize_t) lines; i++)
2383 for (q=p; *q !=
'\0'; q++)
2384 if ((*q ==
'\r') || (*q ==
'\n'))
2386 textlist[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+1,
2387 sizeof(**textlist));
2388 if (textlist[i] == (
char *) NULL)
2389 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2390 (void) memcpy(textlist[i],p,(
size_t) (q-p));
2391 textlist[i][q-p]=
'\0';
2400 hex_string[MagickPathExtent];
2411 lines=(size_t) (strlen(text)/CharsPerLine)+1;
2412 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2414 if (textlist == (
char **) NULL)
2415 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2417 for (i=0; i < (ssize_t) lines; i++)
2422 textlist[i]=(
char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2423 sizeof(**textlist));
2424 if (textlist[i] == (
char *) NULL)
2425 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2426 (void) FormatLocaleString(textlist[i],MagickPathExtent,
"0x%08lx: ",
2427 (
long) (CharsPerLine*i));
2428 q=textlist[i]+strlen(textlist[i]);
2430 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2432 (void) FormatLocaleString(hex_string,MagickPathExtent,
"%02x",*(p+j));
2433 (void) CopyMagickString(q,hex_string,MagickPathExtent);
2435 if ((j % 0x04) == 0)
2438 for ( ; j <= CharsPerLine; j++)
2442 if ((j % 0x04) == 0)
2446 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2448 if (isprint((
int) ((
unsigned char) *p)) != 0)
2455 textlist[i]=(
char *) ResizeQuantumMemory(textlist[i],(
size_t) (q-
2456 textlist[i]+1),
sizeof(**textlist));
2457 if (textlist[i] == (
char *) NULL)
2458 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2461 if (count != (
size_t *) NULL)
2463 textlist[i]=(
char *) NULL;
2489MagickExport StringInfo *StringToStringInfo(
const char *
string)
2494 assert(
string != (
const char *) NULL);
2495 string_info=AcquireStringInfo(strlen(
string));
2496 SetStringInfoDatum(string_info,(
const unsigned char *)
string);
2497 return(string_info);
2523MagickExport
void StripString(
char *message)
2532 assert(message != (
char *) NULL);
2533 if (*message ==
'\0')
2535 length=strlen(message);
2537 while (isspace((
int) ((
unsigned char) *p)) != 0)
2539 if ((*p ==
'\'') || (*p ==
'"'))
2542 while ((isspace((
int) ((
unsigned char) *q)) != 0) && (q > p))
2545 if ((*q ==
'\'') || (*q ==
'"'))
2547 (void) memmove(message,p,(
size_t) (q-p+1));
2548 message[q-p+1]=
'\0';
2549 for (p=message; *p !=
'\0'; p++)
2585MagickExport MagickBooleanType SubstituteString(
char **
string,
2586 const char *search,
const char *replace)
2605 for (p=strchr(*
string,*search); p != (
char *) NULL; p=strchr(p+1,*search))
2607 if (search_extent == 0)
2608 search_extent=strlen(search);
2609 if (strncmp(p,search,search_extent) != 0)
2615 if (replace_extent == 0)
2616 replace_extent=strlen(replace);
2617 if (replace_extent > search_extent)
2622 offset=(ssize_t) (p-(*
string));
2623 extent=strlen(*
string)+replace_extent-search_extent+1;
2624 *
string=(
char *) ResizeQuantumMemory(*
string,
2625 OverAllocateMemory(extent+MaxTextExtent),
sizeof(*p));
2626 if (*
string == (
char *) NULL)
2627 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2633 if (search_extent != replace_extent)
2634 (void) memmove(p+replace_extent,p+search_extent,
2635 strlen(p+search_extent)+1);
2636 (void) memcpy(p,replace,replace_extent);
2637 p+=(ptrdiff_t) replace_extent-1;