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/property.h"
55 #include "magick/resource_.h"
56 #include "magick/signature-private.h"
57 #include "magick/string_.h"
58 #include "magick/string-private.h"
59 #include "magick/utility-private.h"
64 #define CharsPerLine 0x14
69 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
70 static const unsigned char
73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
74 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
75 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
76 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
77 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
78 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
79 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
80 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
81 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
82 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
83 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
84 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
85 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
86 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
87 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
88 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
89 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
90 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
91 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
92 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
93 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
94 0xfc, 0xfd, 0xfe, 0xff,
126 MagickExport
char *AcquireString(
const char *source)
135 if (source != (
char *) NULL)
136 length+=strlen(source);
137 if (~length < MaxTextExtent)
138 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
139 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
140 sizeof(*destination));
141 if (destination == (
char *) NULL)
142 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
143 if (source != (
char *) NULL)
144 (
void) memcpy(destination,source,length*
sizeof(*destination));
145 destination[length]=
'\0';
172 static StringInfo *AcquireStringInfoContainer()
177 string_info=(
StringInfo *) AcquireMagickMemory(
sizeof(*string_info));
179 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
180 (void) memset(string_info,0,
sizeof(*string_info));
181 string_info->signature=MagickCoreSignature;
185 MagickExport
StringInfo *AcquireStringInfo(
const size_t length)
190 string_info=AcquireStringInfoContainer();
191 string_info->length=length;
192 if (~string_info->length >= (MaxTextExtent-1))
193 string_info->datum=(
unsigned char *) AcquireQuantumMemory(
194 string_info->length+MaxTextExtent,
sizeof(*string_info->datum));
195 if (string_info->datum == (
unsigned char *) NULL)
196 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
197 (void) memset(string_info->datum,0,(length+MagickPathExtent)*
198 sizeof(*string_info->datum));
227 MagickExport
StringInfo *BlobToStringInfo(
const void *blob,
const size_t length)
232 if (~length < MaxTextExtent)
233 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
234 string_info=AcquireStringInfoContainer();
235 string_info->length=length;
236 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
237 MaxTextExtent,
sizeof(*string_info->datum));
238 if (string_info->datum == (
unsigned char *) NULL)
240 string_info=DestroyStringInfo(string_info);
243 if (blob != (
const void *) NULL)
244 (void) memcpy(string_info->datum,blob,length);
246 (
void) memset(string_info->datum,0,length*
sizeof(*string_info->datum));
247 (void) memset(string_info->datum+length,0,MagickPathExtent*
248 sizeof(*string_info->datum));
284 MagickExport
char *CloneString(
char **destination,
const char *source)
289 assert(destination != (
char **) NULL);
290 if (source == (
const char *) NULL)
292 if (*destination != (
char *) NULL)
293 *destination=DestroyString(*destination);
294 return(*destination);
296 if (*destination == (
char *) NULL)
298 *destination=AcquireString(source);
299 return(*destination);
301 length=strlen(source);
302 if (~length < MaxTextExtent)
303 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
304 *destination=(
char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
305 sizeof(**destination));
306 if (*destination == (
char *) NULL)
307 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
309 (void) memcpy(*destination,source,length*
sizeof(**destination));
310 (*destination)[length]=
'\0';
311 return(*destination);
342 assert(string_info->signature == MagickCoreSignature);
343 clone_info=AcquireStringInfo(string_info->length);
344 (void) CopyMagickString(clone_info->path,string_info->path,MagickPathExtent);
345 (void) CloneString(&clone_info->name,string_info->name);
346 if (string_info->length != 0)
347 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
378 MagickExport
int CompareStringInfo(
const StringInfo *target,
385 assert(target->signature == MagickCoreSignature);
387 assert(source->signature == MagickCoreSignature);
388 status=memcmp(target->datum,source->datum,MagickMin(target->length,
392 if (target->length == source->length)
394 return(target->length < source->length ? -1 : 1);
426 MagickExport
size_t ConcatenateMagickString(
char *magick_restrict destination,
427 const char *magick_restrict source,
const size_t length)
441 assert(destination != (
char *) NULL);
442 assert(source != (
const char *) NULL);
447 while ((i-- != 0) && (*q !=
'\0'))
449 count=(size_t) (q-destination);
452 return(count+strlen(p));
463 return(count+(p-source));
492 MagickExport MagickBooleanType ConcatenateString(
493 char **magick_restrict destination,
const char *magick_restrict source)
500 assert(destination != (
char **) NULL);
501 if (source == (
const char *) NULL)
503 if (*destination == (
char *) NULL)
505 *destination=AcquireString(source);
508 destination_length=strlen(*destination);
509 source_length=strlen(source);
510 length=destination_length;
511 if (~length < source_length)
512 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
513 length+=source_length;
514 if (~length < MaxTextExtent)
515 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
516 *destination=(
char *) ResizeQuantumMemory(*destination,
517 OverAllocateMemory(length+MaxTextExtent),
sizeof(**destination));
518 if (*destination == (
char *) NULL)
519 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
520 if (source_length != 0)
521 (void) memcpy((*destination)+destination_length,source,source_length);
522 (*destination)[length]=
'\0';
552 MagickExport
void ConcatenateStringInfo(
StringInfo *string_info,
559 assert(string_info->signature == MagickCoreSignature);
561 length=string_info->length;
562 if (~length < source->length)
563 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
564 length+=source->length;
565 if (~length < MagickPathExtent)
566 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
567 if (string_info->datum == (
unsigned char *) NULL)
568 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
569 MagickPathExtent,
sizeof(*string_info->datum));
571 string_info->datum=(
unsigned char *) ResizeQuantumMemory(
572 string_info->datum,OverAllocateMemory(length+MagickPathExtent),
573 sizeof(*string_info->datum));
574 if (string_info->datum == (
unsigned char *) NULL)
575 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
576 (void) memcpy(string_info->datum+string_info->length,source->datum,source->length);
577 string_info->length=length;
604 MagickExport
StringInfo *ConfigureFileToStringInfo(
const char *filename)
624 assert(filename != (
const char *) NULL);
625 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
628 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
629 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
634 length=(size_t) offset;
635 string=(
char *) NULL;
636 if (~length >= (MaxTextExtent-1))
637 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*
string));
638 if (
string == (
char *) NULL)
643 map=MapBlob(file,ReadMode,0,length);
644 if (map != (
void *) NULL)
646 (void) memcpy(
string,map,length);
647 (void) UnmapBlob(map,length);
657 (void) lseek(file,0,SEEK_SET);
658 for (i=0; i < length; i+=count)
660 count=read(file,
string+i,(
size_t) MagickMin(length-i,(
size_t)
672 string=DestroyString(
string);
678 string_info=AcquireStringInfoContainer();
679 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
680 string_info->length=length;
681 string_info->datum=(
unsigned char *)
string;
711 MagickExport
char *ConstantString(
const char *source)
720 if (source != (
char *) NULL)
721 length+=strlen(source);
722 destination=(
char *) NULL;
724 destination=(
char *) AcquireQuantumMemory(length+1UL,
sizeof(*destination));
725 if (destination == (
char *) NULL)
726 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
727 if (source != (
char *) NULL)
728 (
void) memcpy(destination,source,length*
sizeof(*destination));
729 destination[length]=
'\0';
764 MagickExport
size_t CopyMagickString(
char *magick_restrict destination,
765 const char *magick_restrict source,
const size_t length)
778 for (n=length; n > 4; n-=4)
780 if (((*q++)=(*p++)) ==
'\0')
781 return((
size_t) (p-source-1));
782 if (((*q++)=(*p++)) ==
'\0')
783 return((
size_t) (p-source-1));
784 if (((*q++)=(*p++)) ==
'\0')
785 return((
size_t) (p-source-1));
786 if (((*q++)=(*p++)) ==
'\0')
787 return((
size_t) (p-source-1));
792 if (((*q++)=(*p++)) ==
'\0')
793 return((
size_t) (p-source-1));
796 return((
size_t) (p-source));
821 MagickExport
char *DestroyString(
char *
string)
823 return((
char *) RelinquishMagickMemory(
string));
851 assert(string_info->signature == MagickCoreSignature);
852 if (string_info->datum != (
unsigned char *) NULL)
853 string_info->datum=(
unsigned char *) RelinquishMagickMemory(
855 if (string_info->name != (
char *) NULL)
856 string_info->name=DestroyString(string_info->name);
857 string_info->signature=(~MagickCoreSignature);
858 string_info=(
StringInfo *) RelinquishMagickMemory(string_info);
884 MagickExport
char **DestroyStringList(
char **list)
889 assert(list != (
char **) NULL);
890 for (i=0; list[i] != (
char *) NULL; i++)
891 list[i]=DestroyString(list[i]);
892 list=(
char **) RelinquishMagickMemory(list);
925 MagickExport
char *EscapeString(
const char *source,
const char escape)
939 assert(source != (
const char *) NULL);
941 for (p=source; *p !=
'\0'; p++)
943 if ((*p ==
'\\') || (*p == escape))
946 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
951 destination=(
char *) NULL;
952 if (~length >= (MaxTextExtent-1))
953 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
954 sizeof(*destination));
955 if (destination == (
char *) NULL)
956 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
959 for (p=source; *p !=
'\0'; p++)
961 if ((*p ==
'\\') || (*p == escape))
996 MagickExport
char *FileToString(
const char *filename,
const size_t extent,
1002 assert(filename != (
const char *) NULL);
1004 return((
char *) FileToBlob(filename,extent,&length,exception));
1034 MagickExport
StringInfo *FileToStringInfo(
const char *filename,
1040 assert(filename != (
const char *) NULL);
1042 if (IsEventLogging() != MagickFalse)
1043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1044 string_info=AcquireStringInfoContainer();
1045 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
1046 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
1047 if (string_info->datum == (
unsigned char *) NULL)
1049 string_info=DestroyStringInfo(string_info);
1052 return(string_info);
1084 MagickExport ssize_t FormatMagickSize(
const MagickSizeType size,
1085 const MagickBooleanType bi,
char *format)
1107 "",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei",
"Zi",
"Yi",
"Ri",
"Qi", (
char *) NULL
1109 *traditional_units[] =
1111 "",
"K",
"M",
"G",
"T",
"P",
"E",
"Z",
"Y",
"R",
"Q", (
char *) NULL
1115 units=traditional_units;
1116 if (bi != MagickFalse)
1121 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1122 length=(double) ((MagickOffsetType) size);
1124 length=(double) size;
1126 (void) FormatLocaleString(p,MaxTextExtent,
"%.*g",GetMagickPrecision(),
1128 (void) FormatLocaleString(q,MaxTextExtent,
"%.20g",length);
1129 if (strtod(p,(
char **) NULL) == strtod(q,(
char **) NULL))
1131 count=FormatLocaleString(format,MaxTextExtent,
"%.20g%sB",length,units[0]);
1134 for (i=0; (length >= bytes) && (units[i+1] != (
const char *) NULL); i++)
1136 count=FormatLocaleString(format,MaxTextExtent,
"%.*g%sB",GetMagickPrecision(),
1164 MagickExport
char *GetEnvironmentValue(
const char *name)
1169 environment=getenv(name);
1170 if (environment == (
const char *) NULL)
1171 return((
char *) NULL);
1172 return(ConstantString(environment));
1197 MagickExport
unsigned char *GetStringInfoDatum(
const StringInfo *string_info)
1200 assert(string_info->signature == MagickCoreSignature);
1201 return(string_info->datum);
1226 MagickExport
size_t GetStringInfoLength(
const StringInfo *string_info)
1229 assert(string_info->signature == MagickCoreSignature);
1230 return(string_info->length);
1255 MagickExport
const char *GetStringInfoName(
const StringInfo *string_info)
1258 assert(string_info->signature == MagickCoreSignature);
1259 return(string_info->name);
1284 MagickExport
const char *GetStringInfoPath(
const StringInfo *string_info)
1287 assert(string_info->signature == MagickCoreSignature);
1288 return(string_info->path);
1318 MagickExport
double InterpretSiPrefixValue(
const char *magick_restrict
string,
1319 char **magick_restrict sentinel)
1327 value=InterpretLocaleValue(
string,&q);
1330 if ((*q >=
'E') && (*q <=
'z'))
1335 switch ((
int) ((
unsigned char) *q))
1337 case 'q': e=(-30.0);
break;
1338 case 'r': e=(-27.0);
break;
1339 case 'y': e=(-24.0);
break;
1340 case 'z': e=(-21.0);
break;
1341 case 'a': e=(-18.0);
break;
1342 case 'f': e=(-15.0);
break;
1343 case 'p': e=(-12.0);
break;
1344 case 'n': e=(-9.0);
break;
1345 case 'u': e=(-6.0);
break;
1346 case 'm': e=(-3.0);
break;
1347 case 'c': e=(-2.0);
break;
1348 case 'd': e=(-1.0);
break;
1349 case 'h': e=2.0;
break;
1350 case 'k': e=3.0;
break;
1351 case 'K': e=3.0;
break;
1352 case 'M': e=6.0;
break;
1353 case 'G': e=9.0;
break;
1354 case 'T': e=12.0;
break;
1355 case 'P': e=15.0;
break;
1356 case 'E': e=18.0;
break;
1357 case 'Z': e=21.0;
break;
1358 case 'Y': e=24.0;
break;
1359 case 'R': e=27.0;
break;
1360 case 'Q': e=30.0;
break;
1361 default: e=0.0;
break;
1363 if (e >= MagickEpsilon)
1367 value*=pow(2.0,e/0.3);
1377 if ((*q ==
'B') || (*q ==
'P'))
1380 if (sentinel != (
char **) NULL)
1411 MagickExport MagickBooleanType IsStringTrue(
const char *value)
1413 if (value == (
const char *) NULL)
1414 return(MagickFalse);
1415 if (LocaleCompare(value,
"true") == 0)
1417 if (LocaleCompare(value,
"on") == 0)
1419 if (LocaleCompare(value,
"yes") == 0)
1421 if (LocaleCompare(value,
"1") == 0)
1423 return(MagickFalse);
1453 MagickExport MagickBooleanType IsStringNotFalse(
const char *value)
1455 if (value == (
const char *) NULL)
1457 if (LocaleCompare(value,
"false") == 0)
1458 return(MagickFalse);
1459 if (LocaleCompare(value,
"off") == 0)
1460 return(MagickFalse);
1461 if (LocaleCompare(value,
"no") == 0)
1462 return(MagickFalse);
1463 if (LocaleCompare(value,
"0") == 0)
1464 return(MagickFalse);
1495 MagickExport
void PrintStringInfo(FILE *file,
const char *
id,
1505 assert(
id != (
const char *) NULL);
1507 assert(string_info->signature == MagickCoreSignature);
1508 p=(
char *) string_info->datum;
1509 for (i=0; i < string_info->length; i++)
1511 if (((
int) ((
unsigned char) *p) < 32) &&
1512 (isspace((
int) ((
unsigned char) *p)) == 0))
1516 (void) FormatLocaleFile(file,
"%s(%.20g):\n",
id,(
double) string_info->length);
1517 if (i == string_info->length)
1519 for (i=0; i < string_info->length; i++)
1520 (
void) fputc(string_info->datum[i],file);
1521 (void) fputc(
'\n',file);
1527 p=(
char *) string_info->datum;
1528 for (i=0; i < string_info->length; i+=CharsPerLine)
1530 (void) FormatLocaleFile(file,
"0x%08lx: ",(
unsigned long) (CharsPerLine*i));
1531 for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1533 (void) FormatLocaleFile(file,
"%02lx",(
unsigned long) (*(p+j)) & 0xff);
1534 if ((j % 0x04) == 0)
1535 (
void) fputc(
' ',file);
1537 for ( ; j <= CharsPerLine; j++)
1539 (void) fputc(
' ',file);
1540 (void) fputc(
' ',file);
1541 if ((j % 0x04) == 0)
1542 (
void) fputc(
' ',file);
1544 (void) fputc(
' ',file);
1545 for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1547 if (isprint((
int) ((
unsigned char) *p)) != 0)
1548 (
void) fputc(*p,file);
1550 (
void) fputc(
'-',file);
1553 (void) fputc(
'\n',file);
1579 MagickExport
void ResetStringInfo(
StringInfo *string_info)
1582 assert(string_info->signature == MagickCoreSignature);
1583 (void) memset(string_info->datum,0,string_info->length);
1611 MagickExport
char *SanitizeString(
const char *source)
1624 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1625 "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1627 sanitize_source=AcquireString(source);
1629 q=sanitize_source+strlen(sanitize_source);
1630 for (p+=strspn(p,allowlist); p != q; p+=strspn(p,allowlist))
1632 return(sanitize_source);
1659 MagickExport
void SetStringInfo(
StringInfo *string_info,
1663 assert(string_info->signature == MagickCoreSignature);
1665 assert(source->signature == MagickCoreSignature);
1666 if (string_info->length == 0)
1668 (void) memset(string_info->datum,0,string_info->length);
1669 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1699 MagickExport
void SetStringInfoDatum(
StringInfo *string_info,
1700 const unsigned char *source)
1703 assert(string_info->signature == MagickCoreSignature);
1704 if (string_info->length != 0)
1705 (void) memcpy(string_info->datum,source,string_info->length);
1732 MagickExport
void SetStringInfoLength(
StringInfo *string_info,
1733 const size_t length)
1736 assert(string_info->signature == MagickCoreSignature);
1737 if (string_info->length == length)
1739 if (~length < MaxTextExtent)
1740 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1741 string_info->length=length;
1742 if (string_info->datum == (
unsigned char *) NULL)
1743 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
1744 MaxTextExtent,
sizeof(*string_info->datum));
1746 string_info->datum=(
unsigned char *) ResizeQuantumMemory(string_info->datum,
1747 length+MaxTextExtent,
sizeof(*string_info->datum));
1748 if (string_info->datum == (
unsigned char *) NULL)
1749 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1776 MagickExport
void SetStringInfoName(
StringInfo *string_info,
const char *name)
1779 assert(string_info->signature == MagickCoreSignature);
1780 assert(name != (
const char *) NULL);
1781 string_info->name=ConstantString(name);
1808 MagickExport
void SetStringInfoPath(
StringInfo *string_info,
const char *path)
1811 assert(string_info->signature == MagickCoreSignature);
1812 assert(path != (
const char *) NULL);
1813 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1839 const size_t offset)
1845 assert(string_info->signature == MagickCoreSignature);
1846 if (offset > string_info->length)
1848 split_info=AcquireStringInfo(offset);
1849 SetStringInfo(split_info,string_info);
1850 (void) memmove(string_info->datum,string_info->datum+offset,
1851 string_info->length-offset+MaxTextExtent);
1852 SetStringInfoLength(string_info,string_info->length-offset);
1878 MagickExport
char *StringInfoToString(
const StringInfo *string_info)
1886 string=(
char *) NULL;
1887 length=string_info->length;
1888 if (~length >= (MaxTextExtent-1))
1889 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*
string));
1890 if (
string == (
char *) NULL)
1891 return((
char *) NULL);
1892 (void) memcpy(
string,(
char *) string_info->datum,length*
sizeof(*string));
1893 string[length]=
'\0';
1919 MagickExport
char *StringInfoToHexString(
const StringInfo *string_info)
1939 length=string_info->length;
1940 if (~length < MaxTextExtent)
1941 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1942 string=(
char *) AcquireQuantumMemory(length+MaxTextExtent,2*
sizeof(*
string));
1943 if (
string == (
char *) NULL)
1944 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1961 p=string_info->datum;
1962 q=(
unsigned char *)
string;
1963 for (i=0; i < (ssize_t) string_info->length; i++)
1965 *q++=hex_digits[(*p >> 4) & 0x0f];
1966 *q++=hex_digits[*p & 0x0f];
2007 MagickExport
char **StringToArgv(
const char *text,
int *argc)
2020 if (text == (
char *) NULL)
2021 return((
char **) NULL);
2025 for (p=text; *p !=
'\0'; )
2027 while (isspace((
int) ((
unsigned char) *p)) != 0)
2033 for (p++; (*p !=
'"') && (*p !=
'\0'); p++) ;
2035 for (p++; (*p !=
'\'') && (*p !=
'\0'); p++) ;
2036 while ((isspace((
int) ((
unsigned char) *p)) == 0) && (*p !=
'\0'))
2040 argv=(
char **) AcquireQuantumMemory((
size_t) (*argc+1UL),
sizeof(*argv));
2041 if (argv == (
char **) NULL)
2042 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
2046 argv[0]=AcquireString(
"magick");
2048 for (i=1; i < (ssize_t) *argc; i++)
2050 while (isspace((
int) ((
unsigned char) *p)) != 0)
2056 for (q++; (*q !=
'"') && (*q !=
'\0'); q++) ;
2062 for (q++; (*q !=
'\'') && (*q !=
'\0'); q++) ;
2065 while ((isspace((
int) ((
unsigned char) *q)) == 0) && (*q !=
'\0'))
2067 argv[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
2069 if (argv[i] == (
char *) NULL)
2071 for (i--; i >= 0; i--)
2072 argv[i]=DestroyString(argv[i]);
2073 argv=(
char **) RelinquishMagickMemory(argv);
2074 ThrowFatalException(ResourceLimitFatalError,
2075 "UnableToConvertStringToARGV");
2077 (void) memcpy(argv[i],p,(
size_t) (q-p));
2080 while ((isspace((
int) ((
unsigned char) *p)) == 0) && (*p !=
'\0'))
2083 argv[i]=(
char *) NULL;
2122 MagickExport
double *StringToArrayOfDoubles(
const char *
string,ssize_t *count,
2141 assert(exception->signature == MagickCoreSignature);
2143 if (
string == (
char *) NULL)
2144 return((
double *) NULL);
2149 (void) StringToDouble(p,&q);
2151 return((
double *) NULL);
2154 while (isspace((
int) ((
unsigned char) *p)) != 0)
2158 while (isspace((
int) ((
unsigned char) *p)) != 0)
2165 array=(
double *) AcquireQuantumMemory((
size_t) i,
sizeof(*array));
2166 if (array == (
double *) NULL)
2168 (void) ThrowMagickException(exception,GetMagickModule(),
2169 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2170 return((
double *) NULL);
2177 while ((*p !=
'\0') && (i < *count))
2179 array[i++]=StringToDouble(p,&q);
2181 while ((isspace((
int) ((
unsigned char) *p)) != 0) || (*p ==
','))
2221 MagickExport
char *StringToken(
const char *delimiters,
char **
string)
2237 if (p == (
char *) NULL)
2238 return((
char *) NULL);
2256 }
while (d !=
'\0');
2284 MagickExport
char **StringToList(
const char *text)
2286 return(StringToStrings(text,(
size_t *) NULL));
2315 MagickExport
char **StringToStrings(
const char *text,
size_t *count)
2329 if (text == (
char *) NULL)
2331 if (count != (
size_t *) NULL)
2333 return((
char **) NULL);
2335 for (p=text; *p !=
'\0'; p++)
2336 if (((
int) ((
unsigned char) *p) < 32) &&
2337 (isspace((
int) ((
unsigned char) *p)) == 0))
2348 for (p=text; *p !=
'\0'; p++)
2351 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2353 if (textlist == (
char **) NULL)
2354 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2356 for (i=0; i < (ssize_t) lines; i++)
2358 for (q=p; *q !=
'\0'; q++)
2359 if ((*q ==
'\r') || (*q ==
'\n'))
2361 textlist[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+1,
2362 sizeof(**textlist));
2363 if (textlist[i] == (
char *) NULL)
2364 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2365 (void) memcpy(textlist[i],p,(
size_t) (q-p));
2366 textlist[i][q-p]=
'\0';
2375 hex_string[MagickPathExtent];
2386 lines=(size_t) (strlen(text)/CharsPerLine)+1;
2387 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2389 if (textlist == (
char **) NULL)
2390 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2392 for (i=0; i < (ssize_t) lines; i++)
2397 textlist[i]=(
char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2398 sizeof(**textlist));
2399 if (textlist[i] == (
char *) NULL)
2400 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2401 (void) FormatLocaleString(textlist[i],MagickPathExtent,
"0x%08lx: ",
2402 (
long) (CharsPerLine*i));
2403 q=textlist[i]+strlen(textlist[i]);
2405 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2407 (void) FormatLocaleString(hex_string,MagickPathExtent,
"%02x",*(p+j));
2408 (void) CopyMagickString(q,hex_string,MagickPathExtent);
2410 if ((j % 0x04) == 0)
2413 for ( ; j <= CharsPerLine; j++)
2417 if ((j % 0x04) == 0)
2421 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2423 if (isprint((
int) ((
unsigned char) *p)) != 0)
2430 textlist[i]=(
char *) ResizeQuantumMemory(textlist[i],(
size_t) (q-
2431 textlist[i]+1),
sizeof(**textlist));
2432 if (textlist[i] == (
char *) NULL)
2433 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertText");
2436 if (count != (
size_t *) NULL)
2438 textlist[i]=(
char *) NULL;
2464 MagickExport
StringInfo *StringToStringInfo(
const char *
string)
2469 assert(
string != (
const char *) NULL);
2470 string_info=AcquireStringInfo(strlen(
string));
2471 SetStringInfoDatum(string_info,(
const unsigned char *)
string);
2472 return(string_info);
2498 MagickExport
void StripString(
char *message)
2507 assert(message != (
char *) NULL);
2508 if (*message ==
'\0')
2510 length=strlen(message);
2512 while (isspace((
int) ((
unsigned char) *p)) != 0)
2514 if ((*p ==
'\'') || (*p ==
'"'))
2517 while ((isspace((
int) ((
unsigned char) *q)) != 0) && (q > p))
2520 if ((*q ==
'\'') || (*q ==
'"'))
2522 (void) memmove(message,p,(
size_t) (q-p+1));
2523 message[q-p+1]=
'\0';
2524 for (p=message; *p !=
'\0'; p++)
2560 MagickExport MagickBooleanType SubstituteString(
char **
string,
2561 const char *search,
const char *replace)
2580 for (p=strchr(*
string,*search); p != (
char *) NULL; p=strchr(p+1,*search))
2582 if (search_extent == 0)
2583 search_extent=strlen(search);
2584 if (strncmp(p,search,search_extent) != 0)
2590 if (replace_extent == 0)
2591 replace_extent=strlen(replace);
2592 if (replace_extent > search_extent)
2597 offset=(ssize_t) (p-(*
string));
2598 extent=strlen(*
string)+replace_extent-search_extent+1;
2599 *
string=(
char *) ResizeQuantumMemory(*
string,
2600 OverAllocateMemory(extent+MaxTextExtent),
sizeof(*p));
2601 if (*
string == (
char *) NULL)
2602 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2608 if (search_extent != replace_extent)
2609 (void) memmove(p+replace_extent,p+search_extent,
2610 strlen(p+search_extent)+1);
2611 (void) memcpy(p,replace,replace_extent);
2612 p+=replace_extent-1;