42#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/client.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/image-private.h"
49#include "MagickCore/list.h"
50#include "MagickCore/locale_.h"
51#include "MagickCore/log.h"
52#include "MagickCore/memory_.h"
53#include "MagickCore/memory-private.h"
54#include "MagickCore/nt-base-private.h"
55#include "MagickCore/property.h"
56#include "MagickCore/policy.h"
57#include "MagickCore/resource_.h"
58#include "MagickCore/resource-private.h"
59#include "MagickCore/signature-private.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/string-private.h"
62#include "MagickCore/utility-private.h"
67#define CharsPerLine 0x14
97MagickExport
char *AcquireString(
const char *source)
106 if (source != (
char *) NULL)
107 length+=strlen(source);
108 if (~length < MagickPathExtent)
109 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
110 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
111 sizeof(*destination));
112 if (destination == (
char *) NULL)
113 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
114 if (source != (
char *) NULL)
115 (void) memcpy(destination,source,length*
sizeof(*destination));
116 destination[length]=
'\0';
143static StringInfo *AcquireStringInfoContainer(
void)
148 string_info=(StringInfo *) AcquireCriticalMemory(
sizeof(*string_info));
149 (void) memset(string_info,0,
sizeof(*string_info));
150 string_info->signature=MagickCoreSignature;
154MagickExport StringInfo *AcquireStringInfo(
const size_t length)
159 string_info=AcquireStringInfoContainer();
160 string_info->length=length;
161 if (~string_info->length >= (MagickPathExtent-1))
162 string_info->datum=(
unsigned char *) AcquireQuantumMemory(
163 string_info->length+MagickPathExtent,
sizeof(*string_info->datum));
164 if (string_info->datum == (
unsigned char *) NULL)
165 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
166 (void) memset(string_info->datum,0,(length+MagickPathExtent)*
167 sizeof(*string_info->datum));
196MagickExport StringInfo *BlobToStringInfo(
const void *blob,
const size_t length)
201 if (~length < MagickPathExtent)
202 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
203 string_info=AcquireStringInfoContainer();
204 string_info->length=length;
205 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
206 MagickPathExtent,
sizeof(*string_info->datum));
207 if (string_info->datum == (
unsigned char *) NULL)
209 string_info=DestroyStringInfo(string_info);
210 return((StringInfo *) NULL);
212 if (blob != (
const void *) NULL)
213 (void) memcpy(string_info->datum,blob,length);
215 (
void) memset(string_info->datum,0,length*
sizeof(*string_info->datum));
216 (void) memset(string_info->datum+length,0,MagickPathExtent*
217 sizeof(*string_info->datum));
253MagickExport
char *CloneString(
char **destination,
const char *source)
258 assert(destination != (
char **) NULL);
259 if (source == (
const char *) NULL)
261 if (*destination != (
char *) NULL)
262 *destination=DestroyString(*destination);
263 return(*destination);
265 if (*destination == (
char *) NULL)
267 *destination=AcquireString(source);
268 return(*destination);
270 length=strlen(source);
271 if (~length < MagickPathExtent)
272 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
273 *destination=(
char *) ResizeQuantumMemory(*destination,length+
274 MagickPathExtent,
sizeof(**destination));
275 if (*destination == (
char *) NULL)
276 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
278 (void) memcpy(*destination,source,length*
sizeof(**destination));
279 (*destination)[length]=
'\0';
280 return(*destination);
305MagickExport StringInfo *CloneStringInfo(
const StringInfo *string_info)
310 assert(string_info != (StringInfo *) NULL);
311 assert(string_info->signature == MagickCoreSignature);
312 clone_info=AcquireStringInfo(string_info->length);
313 (void) CloneString(&clone_info->path,string_info->path);
314 (void) CloneString(&clone_info->name,string_info->name);
315 if (string_info->length != 0)
316 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
347MagickExport
int CompareStringInfo(
const StringInfo *target,
348 const StringInfo *source)
353 assert(target != (StringInfo *) NULL);
354 assert(target->signature == MagickCoreSignature);
355 assert(source != (StringInfo *) NULL);
356 assert(source->signature == MagickCoreSignature);
357 status=memcmp(target->datum,source->datum,MagickMin(target->length,
361 if (target->length == source->length)
363 return(target->length < source->length ? -1 : 1);
395MagickExport
size_t ConcatenateMagickString(
char *magick_restrict destination,
396 const char *magick_restrict source,
const size_t length)
408 assert(destination != (
char *) NULL);
409 assert(source != (
const char *) NULL);
414 while ((i-- != 0) && (*q !=
'\0'))
416 count=(size_t) (q-destination);
419 return(count+strlen(p));
430 return(count+(
size_t) (p-source));
459MagickExport MagickBooleanType ConcatenateString(
460 char **magick_restrict destination,
const char *magick_restrict source)
467 assert(destination != (
char **) NULL);
468 if (source == (
const char *) NULL)
470 if (*destination == (
char *) NULL)
472 *destination=AcquireString(source);
475 destination_length=strlen(*destination);
476 source_length=strlen(source);
477 length=destination_length;
478 if (~length < source_length)
479 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
480 length+=source_length;
481 if (~length < MagickPathExtent)
482 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
483 *destination=(
char *) ResizeQuantumMemory(*destination,
484 OverAllocateMemory(length+MagickPathExtent),
sizeof(**destination));
485 if (*destination == (
char *) NULL)
486 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
487 if (source_length != 0)
488 (void) memcpy((*destination)+destination_length,source,source_length);
489 (*destination)[length]=
'\0';
519MagickExport
void ConcatenateStringInfo(StringInfo *string_info,
520 const StringInfo *source)
525 assert(string_info != (StringInfo *) NULL);
526 assert(string_info->signature == MagickCoreSignature);
527 assert(source != (
const StringInfo *) NULL);
528 length=string_info->length;
529 if (~length < source->length)
530 ThrowFatalException(ResourceLimitFatalError,
"UnableToConcatenateString");
531 length+=source->length;
532 if (~length < MagickPathExtent)
533 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
534 if (string_info->datum == (
unsigned char *) NULL)
535 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
536 MagickPathExtent,
sizeof(*string_info->datum));
538 string_info->datum=(
unsigned char *) ResizeQuantumMemory(
539 string_info->datum,OverAllocateMemory(length+MagickPathExtent),
540 sizeof(*string_info->datum));
541 if (string_info->datum == (
unsigned char *) NULL)
542 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
543 (void) memcpy(string_info->datum+string_info->length,source->datum,
545 string_info->length=length;
572MagickExport StringInfo *ConfigureFileToStringInfo(
const char *filename)
592 assert(filename != (
const char *) NULL);
593 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
595 return((StringInfo *) NULL);
596 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
597 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
599 file=close_utf8(file)-1;
600 return((StringInfo *) NULL);
602 length=(size_t) offset;
603 string=(
char *) NULL;
604 if (~length >= (MagickPathExtent-1))
605 string=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
607 if (
string == (
char *) NULL)
609 file=close_utf8(file)-1;
610 return((StringInfo *) NULL);
612 map=MapBlob(file,ReadMode,0,length);
613 if (map != (
void *) NULL)
615 (void) memcpy(
string,map,length);
616 (void) UnmapBlob(map,length);
626 (void) lseek(file,0,SEEK_SET);
627 for (i=0; i < length; i+=(size_t) count)
629 count=MagickRead(file,
string+i,(
size_t) MagickMin(length-i,(
size_t)
630 MagickMaxBufferExtent));
636 file=close_utf8(file)-1;
637 string=DestroyString(
string);
638 return((StringInfo *) NULL);
642 file=close_utf8(file)-1;
643 string_info=AcquireStringInfoContainer();
644 string_info->path=ConstantString(filename);
645 string_info->length=length;
646 string_info->datum=(
unsigned char *)
string;
676MagickExport
char *ConstantString(
const char *source)
685 if (source != (
char *) NULL)
686 length+=strlen(source);
687 destination=(
char *) NULL;
689 destination=(
char *) AcquireQuantumMemory(length+1UL,
sizeof(*destination));
690 if (destination == (
char *) NULL)
691 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
692 if (source != (
char *) NULL)
693 (void) memcpy(destination,source,length*
sizeof(*destination));
694 destination[length]=
'\0';
729MagickExport
size_t CopyMagickString(
char *magick_restrict destination,
730 const char *magick_restrict source,
const size_t length)
743 for (n=length; n > 4; n-=4)
745 if (((*q++)=(*p++)) ==
'\0')
746 return((
size_t) (p-source-1));
747 if (((*q++)=(*p++)) ==
'\0')
748 return((
size_t) (p-source-1));
749 if (((*q++)=(*p++)) ==
'\0')
750 return((
size_t) (p-source-1));
751 if (((*q++)=(*p++)) ==
'\0')
752 return((
size_t) (p-source-1));
757 if (((*q++)=(*p++)) ==
'\0')
758 return((
size_t) (p-source-1));
761 return((
size_t) (p-source));
786MagickExport
char *DestroyString(
char *
string)
788 return((
char *) RelinquishMagickMemory(
string));
813MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
815 assert(string_info != (StringInfo *) NULL);
816 assert(string_info->signature == MagickCoreSignature);
817 if (string_info->datum != (
unsigned char *) NULL)
818 string_info->datum=(
unsigned char *) RelinquishMagickMemory(
820 if (string_info->name != (
char *) NULL)
821 string_info->name=DestroyString(string_info->name);
822 if (string_info->path != (
char *) NULL)
823 string_info->path=DestroyString(string_info->path);
824 string_info->signature=(~MagickCoreSignature);
825 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
851MagickExport
char **DestroyStringList(
char **list)
856 assert(list != (
char **) NULL);
857 for (i=0; list[i] != (
char *) NULL; i++)
858 list[i]=DestroyString(list[i]);
859 list=(
char **) RelinquishMagickMemory(list);
892MagickExport
char *EscapeString(
const char *source,
const char escape)
906 assert(source != (
const char *) NULL);
908 for (p=source; *p !=
'\0'; p++)
910 if ((*p ==
'\\') || (*p == escape))
913 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
918 destination=(
char *) NULL;
919 if (~length >= (MagickPathExtent-1))
920 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
921 sizeof(*destination));
922 if (destination == (
char *) NULL)
923 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
926 for (p=source; *p !=
'\0'; p++)
928 if ((*p ==
'\\') || (*p == escape))
963MagickExport
char *FileToString(
const char *filename,
const size_t extent,
964 ExceptionInfo *exception)
972 assert(filename != (
const char *) NULL);
973 assert(exception != (ExceptionInfo *) NULL);
974 if (IsEventLogging() != MagickFalse)
975 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
977 if ((*filename ==
'@') && (strlen(filename) > 1))
982 status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
983 if (status == MagickFalse)
984 ThrowPolicyException(filename,(
char *) NULL);
987 return((
char *) FileToBlob(p,extent,&length,exception));
1017MagickExport StringInfo *FileToStringInfo(
const char *filename,
1018 const size_t extent,ExceptionInfo *exception)
1023 assert(filename != (
const char *) NULL);
1024 assert(exception != (ExceptionInfo *) NULL);
1025 if (IsEventLogging() != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
1027 string_info=AcquireStringInfoContainer();
1028 string_info->path=ConstantString(filename);
1029 string_info->datum=(
unsigned char *) FileToBlob(filename,extent,
1030 &string_info->length,exception);
1031 if (string_info->datum == (
unsigned char *) NULL)
1033 string_info=DestroyStringInfo(string_info);
1034 return((StringInfo *) NULL);
1036 return(string_info);
1072MagickExport ssize_t FormatMagickSize(
const MagickSizeType size,
1073 const MagickBooleanType bi,
const char *suffix,
const size_t length,
1092 "",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei",
"Zi",
"Yi",
"Ri",
"Qi", (
char *) NULL
1094 *traditional_units[] =
1096 "",
"K",
"M",
"G",
"T",
"P",
"E",
"Z",
"Y",
"R",
"Q", (
char *) NULL
1100 units=traditional_units;
1101 if (bi != MagickFalse)
1106 extent=(double) size;
1107 (void) FormatLocaleString(format,MagickFormatExtent,
"%.*g",
1108 GetMagickPrecision(),extent);
1109 if (strstr(format,
"e+") == (
char *) NULL)
1111 if (suffix == (
const char *) NULL)
1112 count=FormatLocaleString(format,length,
"%.17g%s",extent,units[0]);
1114 count=FormatLocaleString(format,length,
"%.17g%s%s",extent,units[0],
1118 for (i=0; (extent >= bytes) && (units[i+1] != (
const char *) NULL); i++)
1120 if (suffix == (
const char *) NULL)
1121 count=FormatLocaleString(format,length,
"%.*g%s",GetMagickPrecision(),
1124 count=FormatLocaleString(format,length,
"%.*g%s%s",GetMagickPrecision(),
1125 extent,units[i],suffix);
1152MagickExport
char *GetEnvironmentValue(
const char *name)
1154#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1155 return(NTGetEnvironmentValue(name));
1160 environment=getenv(name);
1161 if (environment == (
const char *) NULL)
1162 return((
char *) NULL);
1163 return(ConstantString(environment));
1189MagickExport
unsigned char *GetStringInfoDatum(
const StringInfo *string_info)
1191 assert(string_info != (StringInfo *) NULL);
1192 assert(string_info->signature == MagickCoreSignature);
1193 return(string_info->datum);
1218MagickExport
size_t GetStringInfoLength(
const StringInfo *string_info)
1220 assert(string_info != (StringInfo *) NULL);
1221 assert(string_info->signature == MagickCoreSignature);
1222 return(string_info->length);
1247MagickExport
const char *GetStringInfoName(
const StringInfo *string_info)
1249 assert(string_info != (StringInfo *) NULL);
1250 assert(string_info->signature == MagickCoreSignature);
1251 return(string_info->name);
1276MagickExport
const char *GetStringInfoPath(
const StringInfo *string_info)
1278 assert(string_info != (StringInfo *) NULL);
1279 assert(string_info->signature == MagickCoreSignature);
1280 return(string_info->path);
1310MagickExport
double InterpretSiPrefixValue(
const char *magick_restrict
string,
1311 char **magick_restrict sentinel)
1319 value=InterpretLocaleValue(
string,&q);
1322 if ((*q >=
'E') && (*q <=
'z'))
1327 switch ((
int) ((
unsigned char) *q))
1329 case 'q': e=(-30.0);
break;
1330 case 'r': e=(-27.0);
break;
1331 case 'y': e=(-24.0);
break;
1332 case 'z': e=(-21.0);
break;
1333 case 'a': e=(-18.0);
break;
1334 case 'f': e=(-15.0);
break;
1335 case 'p': e=(-12.0);
break;
1336 case 'n': e=(-9.0);
break;
1337 case 'u': e=(-6.0);
break;
1338 case 'm': e=(-3.0);
break;
1339 case 'c': e=(-2.0);
break;
1340 case 'd': e=(-1.0);
break;
1341 case 'h': e=2.0;
break;
1342 case 'k': e=3.0;
break;
1343 case 'K': e=3.0;
break;
1344 case 'M': e=6.0;
break;
1345 case 'G': e=9.0;
break;
1346 case 'T': e=12.0;
break;
1347 case 'P': e=15.0;
break;
1348 case 'E': e=18.0;
break;
1349 case 'Z': e=21.0;
break;
1350 case 'Y': e=24.0;
break;
1351 case 'R': e=27.0;
break;
1352 case 'Q': e=30.0;
break;
1353 default: e=0.0;
break;
1355 if (e >= MagickEpsilon)
1359 value*=pow(2.0,e/0.3);
1369 if ((*q ==
'B') || (*q ==
'P'))
1372 if (sentinel != (
char **) NULL)
1403MagickExport MagickBooleanType IsStringTrue(
const char *value)
1405 if (value == (
const char *) NULL)
1406 return(MagickFalse);
1407 if (LocaleCompare(value,
"true") == 0)
1409 if (LocaleCompare(value,
"on") == 0)
1411 if (LocaleCompare(value,
"yes") == 0)
1413 if (LocaleCompare(value,
"1") == 0)
1415 return(MagickFalse);
1444MagickExport MagickBooleanType IsStringFalse(
const char *value)
1446 if (value == (
const char *) NULL)
1447 return(MagickFalse);
1448 if (LocaleCompare(value,
"false") == 0)
1450 if (LocaleCompare(value,
"off") == 0)
1452 if (LocaleCompare(value,
"no") == 0)
1454 if (LocaleCompare(value,
"0") == 0)
1456 return(MagickFalse);
1486MagickExport
void PrintStringInfo(FILE *file,
const char *
id,
1487 const StringInfo *string_info)
1499 assert(
id != (
const char *) NULL);
1500 assert(string_info != (StringInfo *) NULL);
1501 assert(string_info->signature == MagickCoreSignature);
1502 p=(
const unsigned char *) string_info->datum;
1503 for (i=0; i < string_info->length; i++)
1504 if ((p[i] < 32) && (isspace((
int)p[i]) == 0))
1506 (void) FormatLocaleFile(file,
"%s(%.17g):\n",
id,(
double) string_info->length);
1507 if (i == string_info->length)
1509 for (i = 0; i < string_info->length; i++)
1510 (
void) fputc(p[i],file);
1511 (void) fputc(
'\n',file);
1517 for (i=0; i < string_info->length; i+=CharsPerLine)
1519 (void) FormatLocaleFile(file,
"0x%08lx: ",(
unsigned long) i);
1520 for (j = 0; j < MagickMin(string_info->length-i, CharsPerLine); j++)
1522 (void) FormatLocaleFile(file,
"%02lx",(
unsigned long) (p[i+j]) & 0xff);
1523 if (((j+1) % 0x04) == 0)
1524 (void) fputc(
' ',file);
1529 for ( ; j < CharsPerLine; j++)
1531 (void) fputc(
' ',file);
1532 (void) fputc(
' ',file);
1533 if (((j+1) % 0x04) == 0)
1534 (void) fputc(
' ',file);
1536 (void) fputc(
' ',file);
1540 for (j=0; j < MagickMin(string_info->length-i,CharsPerLine); j++)
1542 unsigned char c = p[i+j];
1543 if (isprint((
int) c) != 0)
1544 (void) fputc(c,file);
1546 (
void) fputc(
'-',file);
1548 (void) fputc(
'\n',file);
1574MagickExport
void ResetStringInfo(StringInfo *string_info)
1576 assert(string_info != (StringInfo *) NULL);
1577 assert(string_info->signature == MagickCoreSignature);
1578 (void) memset(string_info->datum,0,string_info->length);
1606MagickExport
char *SanitizeString(
const char *source)
1619 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1620 "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1622 sanitize_source=AcquireString(source);
1624 q=sanitize_source+strlen(sanitize_source);
1625 for (p+=strspn(p,allowlist); p != q; p+=(ptrdiff_t) strspn(p,allowlist))
1627 return(sanitize_source);
1654MagickExport
void SetStringInfo(StringInfo *string_info,
1655 const StringInfo *source)
1657 assert(string_info != (StringInfo *) NULL);
1658 assert(string_info->signature == MagickCoreSignature);
1659 assert(source != (StringInfo *) NULL);
1660 assert(source->signature == MagickCoreSignature);
1661 if (string_info->length == 0)
1663 (void) memset(string_info->datum,0,string_info->length);
1664 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1694MagickExport
void SetStringInfoDatum(StringInfo *string_info,
1695 const unsigned char *source)
1697 assert(string_info != (StringInfo *) NULL);
1698 assert(string_info->signature == MagickCoreSignature);
1699 if (string_info->length != 0)
1700 (void) memcpy(string_info->datum,source,string_info->length);
1727MagickExport
void SetStringInfoLength(StringInfo *string_info,
1728 const size_t length)
1730 assert(string_info != (StringInfo *) NULL);
1731 assert(string_info->signature == MagickCoreSignature);
1732 if (string_info->length == length)
1734 if (~length < MagickPathExtent)
1735 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1736 string_info->length=length;
1737 if (string_info->datum == (
unsigned char *) NULL)
1738 string_info->datum=(
unsigned char *) AcquireQuantumMemory(length+
1739 MagickPathExtent,
sizeof(*string_info->datum));
1741 string_info->datum=(
unsigned char *) ResizeQuantumMemory(string_info->datum,
1742 length+MagickPathExtent,
sizeof(*string_info->datum));
1743 if (string_info->datum == (
unsigned char *) NULL)
1744 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1771MagickExport
void SetStringInfoName(StringInfo *string_info,
const char *name)
1773 assert(string_info != (StringInfo *) NULL);
1774 assert(string_info->signature == MagickCoreSignature);
1775 assert(name != (
const char *) NULL);
1776 string_info->name=ConstantString(name);
1803MagickExport
void SetStringInfoPath(StringInfo *string_info,
const char *path)
1805 assert(string_info != (StringInfo *) NULL);
1806 assert(string_info->signature == MagickCoreSignature);
1807 assert(path != (
const char *) NULL);
1808 string_info->path=ConstantString(path);
1833MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1834 const size_t offset)
1839 assert(string_info != (StringInfo *) NULL);
1840 assert(string_info->signature == MagickCoreSignature);
1841 if (offset > string_info->length)
1842 return((StringInfo *) NULL);
1843 split_info=AcquireStringInfo(offset);
1844 SetStringInfo(split_info,string_info);
1845 (void) memmove(string_info->datum,string_info->datum+offset,
1846 string_info->length-offset+MagickPathExtent);
1847 SetStringInfoLength(string_info,string_info->length-offset);
1873MagickExport
char *StringInfoToDigest(
const StringInfo *signature)
1881 signature_info=AcquireSignatureInfo();
1882 UpdateSignature(signature_info,signature);
1883 FinalizeSignature(signature_info);
1884 digest=StringInfoToHexString(GetSignatureDigest(signature_info));
1885 signature_info=DestroySignatureInfo(signature_info);
1911MagickExport
char *StringInfoToHexString(
const StringInfo *string_info)
1931 length=string_info->length;
1932 if (~length < MagickPathExtent)
1933 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1934 string=(
char *) AcquireQuantumMemory(length+MagickPathExtent,2*
1936 if (
string == (
char *) NULL)
1937 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
1954 p=string_info->datum;
1955 q=(
unsigned char *)
string;
1956 for (i=0; i < (ssize_t) string_info->length; i++)
1958 *q++=hex_digits[(*p >> 4) & 0x0f];
1959 *q++=hex_digits[*p & 0x0f];
1988MagickExport
char *StringInfoToString(
const StringInfo *string_info)
1996 string=(
char *) NULL;
1997 length=string_info->length;
1998 if (~length >= (MagickPathExtent-1))
1999 string=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
2001 if (
string == (
char *) NULL)
2002 return((
char *) NULL);
2003 (void) memcpy(
string,(
char *) string_info->datum,length*
sizeof(*
string));
2004 string[length]=
'\0';
2042MagickExport
char **StringToArgv(
const char *text,
int *argc)
2057 if (text == (
const char *) NULL)
2058 return((
char **) NULL);
2064 while ((isspace((
int) ((
unsigned char) *p)) != 0) || (*p ==
'`'))
2072 if ((*p ==
'&') && (*(p+1) ==
'&'))
2077 if ((*p ==
'|') && (*(p+1) ==
'|'))
2082 if ((*p ==
';') || (*p ==
'&') || (*p ==
'|'))
2090 while ((*p !=
'\0') &&
2091 (isspace((
int) ((
unsigned char) *p)) == 0) &&
2092 (*p !=
'`') && (*p !=
';') && (*p !=
'&') && (*p !=
'|'))
2101 if ((*p ==
'"') || (*p ==
'\''))
2103 const char quote = (*p++);
2104 while ((*p != quote) && (*p !=
'\0'))
2106 if ((quote ==
'"') && (*p ==
'\\'))
2126 argv=(
char **) AcquireQuantumMemory((
size_t) *argc+1UL,
sizeof(*argv));
2127 if (argv == (
char **) NULL)
2128 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
2132 argv[0]=AcquireString(GetClientName());
2134 for (i=1; i < (ssize_t) *argc; i++)
2145 while ((isspace((
int) ((
unsigned char) *p)) != 0) || (*p ==
'`'))
2148 if ((*q ==
'&') && (*(q+1) ==
'&'))
2151 if ((*q ==
'|') && (*(q+1) ==
'|'))
2154 if ((*q ==
';') || (*q ==
'&') || (*q ==
'|'))
2158 while ((*q !=
'\0') &&
2159 (isspace((
int) ((
unsigned char) *q)) == 0) &&
2160 (*q !=
'`') && (*q !=
';') && (*q !=
'&') && (*q !=
'|'))
2169 if ((*q ==
'"') || (*q ==
'\''))
2171 const char quote = (*q++);
2172 while ((*q != quote) && (*q !=
'\0'))
2174 if ((quote ==
'"') && (*q ==
'\\'))
2193 argv[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
2195 if (argv[i] == (
char *) NULL)
2197 for (i--; i >= 0; i--)
2198 argv[i]=DestroyString(argv[i]);
2199 argv=(
char **) RelinquishMagickMemory(argv);
2200 ThrowFatalException(ResourceLimitFatalError,
2201 "UnableToConvertStringToARGV");
2206 if (((p != q) && ((*p ==
';') || (*p ==
'&') || (*p ==
'|'))))
2209 argv[i][length++]=(*p++);
2219 argv[i][length++]=(*p++);
2222 if ((*p ==
'"') || (*p ==
'\''))
2224 const char quote = *p++;
2225 while ((p < q) && (*p != quote))
2227 if ((quote ==
'"') && (*p ==
'\\'))
2231 argv[i][length++]=(*p++);
2234 argv[i][length++]=(*p++);
2236 if ((p < q) && (*p == quote))
2240 argv[i][length++]=(*p++);
2243 argv[i][length]=
'\0';
2245 argv[i]=(
char *) NULL;
2284MagickExport
double *StringToArrayOfDoubles(
const char *
string,ssize_t *count,
2285 ExceptionInfo *exception)
2302 assert(exception != (ExceptionInfo *) NULL);
2303 assert(exception->signature == MagickCoreSignature);
2305 if (
string == (
char *) NULL)
2306 return((
double *) NULL);
2311 (void) StringToDouble(p,&q);
2313 return((
double *) NULL);
2316 while (isspace((
int) ((
unsigned char) *p)) != 0)
2320 while (isspace((
int) ((
unsigned char) *p)) != 0)
2327 array=(
double *) AcquireQuantumMemory((
size_t) i,
sizeof(*array));
2328 if (array == (
double *) NULL)
2330 (void) ThrowMagickException(exception,GetMagickModule(),
2331 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2332 return((
double *) NULL);
2339 while ((*p !=
'\0') && (i < *count))
2341 array[i++]=StringToDouble(p,&q);
2343 while ((isspace((
int) ((
unsigned char) *p)) != 0) || (*p ==
','))
2383MagickExport
char *StringToken(
const char *delimiters,
char **
string)
2399 if (p == (
char *) NULL)
2400 return((
char *) NULL);
2418 }
while (d !=
'\0');
2446MagickExport
char **StringToList(
const char *text)
2448 return(StringToStrings(text,(
size_t *) NULL));
2477MagickExport
char **StringToStrings(
const char *text,
size_t *count)
2491 if (text == (
char *) NULL)
2493 if (count != (
size_t *) NULL)
2495 return((
char **) NULL);
2497 for (p=text; *p !=
'\0'; p++)
2498 if (((
int) ((
unsigned char) *p) < 32) &&
2499 (isspace((
int) ((
unsigned char) *p)) == 0))
2510 for (p=text; *p !=
'\0'; p++)
2513 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2515 if (textlist == (
char **) NULL)
2516 return((
char **) NULL);
2518 for (i=0; i < (ssize_t) lines; i++)
2520 for (q=p; *q !=
'\0'; q++)
2521 if ((*q ==
'\r') || (*q ==
'\n'))
2523 textlist[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+1,
2524 sizeof(**textlist));
2525 if (textlist[i] == (
char *) NULL)
2530 for (j=0; j < i; j++)
2531 textlist[j]=DestroyString(textlist[j]);
2532 textlist=(
char **) RelinquishMagickMemory(textlist);
2533 return((
char **) NULL);
2535 (void) memcpy(textlist[i],p,(
size_t) (q-p));
2536 textlist[i][q-p]=
'\0';
2545 hex_string[MagickPathExtent];
2556 lines=(size_t) (strlen(text)/CharsPerLine)+1;
2557 textlist=(
char **) AcquireQuantumMemory((
size_t) lines+1UL,
2559 if (textlist == (
char **) NULL)
2560 return((
char **) NULL);
2562 for (i=0; i < (ssize_t) lines; i++)
2567 textlist[i]=(
char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2568 sizeof(**textlist));
2569 if (textlist[i] == (
char *) NULL)
2571 for (j=0; j < i; j++)
2572 textlist[j]=DestroyString(textlist[j]);
2573 textlist=(
char **) RelinquishMagickMemory(textlist);
2574 return((
char **) NULL);
2576 (void) FormatLocaleString(textlist[i],MagickPathExtent,
"0x%08lx: ",
2577 (
long) (CharsPerLine*i));
2578 q=textlist[i]+strlen(textlist[i]);
2580 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2582 (void) FormatLocaleString(hex_string,MagickPathExtent,
"%02x",*(p+j));
2583 (void) CopyMagickString(q,hex_string,MagickPathExtent);
2585 if ((j % 0x04) == 0)
2588 for ( ; j <= CharsPerLine; j++)
2592 if ((j % 0x04) == 0)
2596 for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2598 if (isprint((
int) ((
unsigned char) *p)) != 0)
2609 resized=(
char *) ResizeQuantumMemory(textlist[i],(
size_t) (q-
2610 textlist[i]+1),
sizeof(**textlist));
2611 if (resized == (
char *) NULL)
2613 textlist[i]=(
char *) NULL;
2614 for (j=0; j < i; j++)
2615 textlist[j]=DestroyString(textlist[j]);
2616 textlist=(
char **) RelinquishMagickMemory(textlist);
2617 return((
char **) NULL);
2619 textlist[i]=resized;
2623 if (count != (
size_t *) NULL)
2625 textlist[i]=(
char *) NULL;
2651MagickExport StringInfo *StringToStringInfo(
const char *
string)
2656 assert(
string != (
const char *) NULL);
2657 string_info=AcquireStringInfo(strlen(
string));
2658 SetStringInfoDatum(string_info,(
const unsigned char *)
string);
2659 return(string_info);
2686MagickExport
void StripString(
char *message)
2688 (void) StripMagickString(message);
2691MagickExport
size_t StripMagickString(
char *message)
2700 assert(message != (
char *) NULL);
2701 if (*message ==
'\0')
2703 length=strlen(message);
2707 while (isspace((
int) ((
unsigned char) *p)) != 0)
2709 if ((*p ==
'\'') || (*p ==
'"'))
2712 while ((isspace((
int) ((
unsigned char) *q)) != 0) && (q > p))
2715 if ((*q ==
'\'') || (*q ==
'"'))
2717 (void) memmove(message,p,(
size_t) (q-p+1));
2718 message[q-p+1]=
'\0';
2719 for (p=message; *p !=
'\0'; p++)
2722 return((
size_t) (q-p+1));
2756MagickExport MagickBooleanType SubstituteString(
char **
string,
2757 const char *search,
const char *replace)
2776 for (p=strchr(*
string,*search); p != (
char *) NULL; p=strchr(p+1,*search))
2778 if (search_extent == 0)
2779 search_extent=strlen(search);
2780 if (strncmp(p,search,search_extent) != 0)
2786 if (replace_extent == 0)
2787 replace_extent=strlen(replace);
2788 if (replace_extent > search_extent)
2793 offset=(ssize_t) (p-(*
string));
2794 extent=strlen(*
string)+replace_extent-search_extent+1;
2795 *
string=(
char *) ResizeQuantumMemory(*
string,
2796 OverAllocateMemory(extent+MagickPathExtent),
sizeof(*p));
2797 if (*
string == (
char *) NULL)
2798 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2804 if (search_extent != replace_extent)
2805 (void) memmove(p+replace_extent,p+search_extent,
2806 strlen(p+search_extent)+1);
2807 (void) memcpy(p,replace,replace_extent);
2808 p+=(ptrdiff_t) replace_extent-1;