2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT RRRR IIIII N N GGGG %
8 % SSS T RRRR I N N N G GGG %
9 % SS T R R I N NN G G %
10 % SSSSS T R R IIIII N N GGGG %
13 % MagickCore String Methods %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the license. You may %
24 % obtain a copy of the license at %
26 % https://imagemagick.org/script/license.php %
28 % unless required by applicable law or agreed to in writing, software %
29 % distributed under the license is distributed on an "as is" basis, %
30 % without warranties or conditions of any kind, either express or implied. %
31 % See the license for the specific language governing permissions and %
32 % limitations under the license. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/image-private.h"
48 #include "MagickCore/list.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/memory_.h"
52 #include "MagickCore/memory-private.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/property.h"
55 #include "MagickCore/resource_.h"
56 #include "MagickCore/signature-private.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/utility-private.h"
64 #define CharsPerLine 0x14
70 #define asciimap AsciiMap
72 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
73 static const unsigned char
76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
77 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
78 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
79 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
80 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
81 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
82 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
83 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
84 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
85 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
86 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
87 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
88 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
89 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
90 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
91 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
92 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
93 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
94 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
95 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
96 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
97 0xfc, 0xfd, 0xfe, 0xff,
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % A c q u i r e S t r i n g %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % AcquireString() returns an new extented string, containing a clone of the
115 % An extended string is the string length, plus an extra MagickPathExtent space
116 % to allow for the string to be actively worked on.
118 % The returned string shoud be freed using DestoryString().
120 % The format of the AcquireString method is:
122 % char *AcquireString(const char *source)
124 % A description of each parameter follows:
126 % o source: A character string.
129 MagickExport char *AcquireString(const char *source)
138 if (source != (char *) NULL)
139 length+=strlen(source);
140 if (~length < MagickPathExtent)
141 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
142 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
143 sizeof(*destination));
144 if (destination == (char *) NULL)
145 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
147 if (source != (char *) NULL)
148 (void) memcpy(destination,source,length*sizeof(*destination));
149 destination[length]='\0';
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % A c q u i r e S t r i n g I n f o %
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 % AcquireStringInfo() allocates the StringInfo structure.
166 % The format of the AcquireStringInfo method is:
168 % StringInfo *AcquireStringInfo(const size_t length)
170 % A description of each parameter follows:
172 % o length: the string length.
176 static StringInfo *AcquireStringInfoContainer()
181 string_info=(StringInfo *) AcquireCriticalMemory(sizeof(*string_info));
182 (void) memset(string_info,0,sizeof(*string_info));
183 string_info->signature=MagickCoreSignature;
187 MagickExport StringInfo *AcquireStringInfo(const size_t length)
192 string_info=AcquireStringInfoContainer();
193 string_info->length=length;
194 if (~string_info->length >= (MagickPathExtent-1))
195 string_info->datum=(unsigned char *) AcquireQuantumMemory(
196 string_info->length+MagickPathExtent,sizeof(*string_info->datum));
197 if (string_info->datum == (unsigned char *) NULL)
198 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
199 (void) memset(string_info->datum,0,(length+MagickPathExtent)*
200 sizeof(*string_info->datum));
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % B l o b T o S t r i n g I n f o %
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % BlobToStringInfo() returns the contents of a blob as a StringInfo structure
216 % with MagickPathExtent extra space.
218 % The format of the BlobToStringInfo method is:
220 % StringInfo *BlobToStringInfo(const void *blob,const size_t length)
222 % A description of each parameter follows:
226 % o length: the length of the blob.
229 MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
234 if (~length < MagickPathExtent)
235 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
236 string_info=AcquireStringInfoContainer();
237 string_info->length=length;
238 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
239 MagickPathExtent,sizeof(*string_info->datum));
240 if (string_info->datum == (unsigned char *) NULL)
242 string_info=DestroyStringInfo(string_info);
243 return((StringInfo *) NULL);
245 if (blob != (const void *) NULL)
246 (void) memcpy(string_info->datum,blob,length);
248 (void) memset(string_info->datum,0,length*sizeof(*string_info->datum));
249 (void) memset(string_info->datum+length,0,MagickPathExtent*
250 sizeof(*string_info->datum));
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % C l o n e S t r i n g %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % CloneString() replaces or frees the destination string to make it
266 % a clone of the input string plus MagickPathExtent more space so the string
269 % If source is a NULL pointer the destination string will be freed and set to
270 % a NULL pointer. A pointer to the stored in the destination is also returned.
272 % When finished the non-NULL string should be freed using DestoryString()
273 % or using CloneString() with a NULL pointed for the source.
275 % The format of the CloneString method is:
277 % char *CloneString(char **destination,const char *source)
279 % A description of each parameter follows:
281 % o destination: A pointer to a character string.
283 % o source: A character string.
286 MagickExport char *CloneString(char **destination,const char *source)
291 assert(destination != (char **) NULL);
292 if (source == (const char *) NULL)
294 if (*destination != (char *) NULL)
295 *destination=DestroyString(*destination);
296 return(*destination);
298 if (*destination == (char *) NULL)
300 *destination=AcquireString(source);
301 return(*destination);
303 length=strlen(source);
304 if (~length < MagickPathExtent)
305 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
306 *destination=(char *) ResizeQuantumMemory(*destination,length+
307 MagickPathExtent,sizeof(**destination));
308 if (*destination == (char *) NULL)
309 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
311 (void) memcpy(*destination,source,length*sizeof(**destination));
312 (*destination)[length]='\0';
313 return(*destination);
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 % C l o n e S t r i n g I n f o %
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % CloneStringInfo() clones a copy of the StringInfo structure.
329 % The format of the CloneStringInfo method is:
331 % StringInfo *CloneStringInfo(const StringInfo *string_info)
333 % A description of each parameter follows:
335 % o string_info: the string info.
338 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
343 assert(string_info != (StringInfo *) NULL);
344 assert(string_info->signature == MagickCoreSignature);
345 clone_info=AcquireStringInfo(string_info->length);
346 if (string_info->length != 0)
347 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 % C o m p a r e S t r i n g I n f o %
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 % CompareStringInfo() compares the two datums target and source. It returns
363 % an integer less than, equal to, or greater than zero if target is found,
364 % respectively, to be less than, to match, or be greater than source.
366 % The format of the CompareStringInfo method is:
368 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
370 % A description of each parameter follows:
372 % o target: the target string.
374 % o source: the source string.
378 MagickExport int CompareStringInfo(const StringInfo *target,
379 const StringInfo *source)
384 assert(target != (StringInfo *) NULL);
385 assert(target->signature == MagickCoreSignature);
386 assert(source != (StringInfo *) NULL);
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);
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % C o n c a t e n a t e M a g i c k S t r i n g %
406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 % ConcatenateMagickString() concatenates the source string to the destination
409 % string. The destination buffer is always null-terminated even if the
410 % string must be truncated.
412 % The format of the ConcatenateMagickString method is:
414 % size_t ConcatenateMagickString(char *destination,const char *source,
415 % const size_t length)
417 % A description of each parameter follows:
419 % o destination: the destination string.
421 % o source: the source string.
423 % o length: the length of the destination string.
426 MagickExport size_t ConcatenateMagickString(char *destination,
427 const char *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));
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 % C o n c a t e n a t e S t r i n g %
475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % ConcatenateString() appends a copy of string source, including the
478 % terminating null character, to the end of string destination.
480 % The format of the ConcatenateString method is:
482 % MagickBooleanType ConcatenateString(char **destination,
483 % const char *source)
485 % A description of each parameter follows:
487 % o destination: A pointer to a character string.
489 % o source: A character string.
492 MagickExport MagickBooleanType ConcatenateString(char **destination,
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 < MagickPathExtent)
515 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
516 *destination=(char *) ResizeQuantumMemory(*destination,length+
517 MagickPathExtent,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';
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 % C o n c a t e n a t e S t r i n g I n f o %
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 % ConcatenateStringInfo() concatenates the source string to the destination
540 % The format of the ConcatenateStringInfo method is:
542 % void ConcatenateStringInfo(StringInfo *string_info,
543 % const StringInfo *source)
545 % A description of each parameter follows:
547 % o string_info: the string info.
549 % o source: the source string.
552 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
553 const StringInfo *source)
558 assert(string_info != (StringInfo *) NULL);
559 assert(string_info->signature == MagickCoreSignature);
560 assert(source != (const StringInfo *) NULL);
561 length=string_info->length;
562 if (~length < source->length)
563 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
564 SetStringInfoLength(string_info,length+source->length);
565 (void) memcpy(string_info->datum+length,source->datum,source->length);
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % C o n f i g u r e F i l e T o S t r i n g I n f o %
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % ConfigureFileToStringInfo() returns the contents of a configure file as a
582 % The format of the ConfigureFileToStringInfo method is:
584 % StringInfo *ConfigureFileToStringInfo(const char *filename)
585 % ExceptionInfo *exception)
587 % A description of each parameter follows:
589 % o filename: the filename.
592 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
612 assert(filename != (const char *) NULL);
613 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
615 return((StringInfo *) NULL);
616 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
617 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
620 return((StringInfo *) NULL);
622 length=(size_t) offset;
623 string=(char *) NULL;
624 if (~length >= (MagickPathExtent-1))
625 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
627 if (string == (char *) NULL)
630 return((StringInfo *) NULL);
632 map=MapBlob(file,ReadMode,0,length);
633 if (map != (void *) NULL)
635 (void) memcpy(string,map,length);
636 (void) UnmapBlob(map,length);
646 (void) lseek(file,0,SEEK_SET);
647 for (i=0; i < length; i+=count)
649 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
661 string=DestroyString(string);
662 return((StringInfo *) NULL);
667 string_info=AcquireStringInfoContainer();
668 string_info->path=ConstantString(filename);
669 string_info->length=length;
670 string_info->datum=(unsigned char *) string;
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % C o n s t a n t S t r i n g %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % ConstantString() allocates exactly the needed memory for a string and
686 % copies the source string to that memory location. A NULL string pointer
687 % will allocate an empty string containing just the NUL character.
689 % When finished the string should be freed using DestoryString()
691 % The format of the ConstantString method is:
693 % char *ConstantString(const char *source)
695 % A description of each parameter follows:
697 % o source: A character string.
700 MagickExport char *ConstantString(const char *source)
709 if (source != (char *) NULL)
710 length+=strlen(source);
711 destination=(char *) NULL;
713 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
714 if (destination == (char *) NULL)
715 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
717 if (source != (char *) NULL)
718 (void) memcpy(destination,source,length*sizeof(*destination));
719 destination[length]='\0';
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % C o p y M a g i c k S t r i n g %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % CopyMagickString() copies the source string to the destination string, with
735 % out exceeding the given pre-declared length.
737 % The destination buffer is always null-terminated even if the string must be
738 % truncated. The return value is the minimum of the source string length or
739 % the length parameter.
741 % The format of the CopyMagickString method is:
743 % size_t CopyMagickString(const char *destination,char *source,
744 % const size_t length)
746 % A description of each parameter follows:
748 % o destination: the destination string.
750 % o source: the source string.
752 % o length: the length of the destination string.
755 MagickExport size_t CopyMagickString(char *destination,const char *source,
769 for (n=length; n > 4; n-=4)
773 return((size_t) (p-source-1));
777 return((size_t) (p-source-1));
781 return((size_t) (p-source-1));
785 return((size_t) (p-source-1));
789 for (n--; n != 0; n--)
793 return((size_t) (p-source-1));
798 return((size_t) (p-source-1));
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % D e s t r o y S t r i n g %
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % DestroyString() destroys memory associated with a string.
814 % The format of the DestroyString method is:
816 % char *DestroyString(char *string)
818 % A description of each parameter follows:
820 % o string: the string.
823 MagickExport char *DestroyString(char *string)
825 return((char *) RelinquishMagickMemory(string));
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % D e s t r o y S t r i n g I n f o %
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
841 % The format of the DestroyStringInfo method is:
843 % StringInfo *DestroyStringInfo(StringInfo *string_info)
845 % A description of each parameter follows:
847 % o string_info: the string info.
850 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
852 assert(string_info != (StringInfo *) NULL);
853 assert(string_info->signature == MagickCoreSignature);
854 if (string_info->datum != (unsigned char *) NULL)
855 string_info->datum=(unsigned char *) RelinquishMagickMemory(
857 if (string_info->name != (char *) NULL)
858 string_info->name=DestroyString(string_info->name);
859 if (string_info->path != (char *) NULL)
860 string_info->path=DestroyString(string_info->path);
861 string_info->signature=(~MagickCoreSignature);
862 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 % D e s t r o y S t r i n g L i s t %
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 % DestroyStringList() zeros memory associated with a string list.
879 % The format of the DestroyStringList method is:
881 % char **DestroyStringList(char **list)
883 % A description of each parameter follows:
885 % o list: the string list.
888 MagickExport char **DestroyStringList(char **list)
893 assert(list != (char **) NULL);
894 for (i=0; list[i] != (char *) NULL; i++)
895 list[i]=DestroyString(list[i]);
896 list=(char **) RelinquishMagickMemory(list);
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % E s c a p e S t r i n g %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % EscapeString() allocates memory for a backslash-escaped version of a
912 % source text string, copies the escaped version of the text to that
913 % memory location while adding backslash characters, and returns the
916 % The format of the EscapeString method is:
918 % char *EscapeString(const char *source,const char escape)
920 % A description of each parameter follows:
922 % o allocate_string: Method EscapeString returns the escaped string.
924 % o source: A character string.
926 % o escape: the quoted string termination character to escape (e.g. '"').
929 MagickExport char *EscapeString(const char *source,const char escape)
943 assert(source != (const char *) NULL);
945 for (p=source; *p != '\0'; p++)
947 if ((*p == '\\') || (*p == escape))
950 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
955 destination=(char *) NULL;
956 if (~length >= (MagickPathExtent-1))
957 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
958 sizeof(*destination));
959 if (destination == (char *) NULL)
960 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
963 for (p=source; *p != '\0'; p++)
965 if ((*p == '\\') || (*p == escape))
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 % F i l e T o S t r i n g %
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 % FileToString() returns the contents of a file as a string.
986 % The format of the FileToString method is:
988 % char *FileToString(const char *filename,const size_t extent,
989 % ExceptionInfo *exception)
991 % A description of each parameter follows:
993 % o filename: the filename.
995 % o extent: Maximum length of the string.
997 % o exception: return any errors or warnings in this structure.
1000 MagickExport char *FileToString(const char *filename,const size_t extent,
1001 ExceptionInfo *exception)
1006 assert(filename != (const char *) NULL);
1007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1008 assert(exception != (ExceptionInfo *) NULL);
1009 return((char *) FileToBlob(filename,extent,&length,exception));
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 % F i l e T o S t r i n g I n f o %
1021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 % FileToStringInfo() returns the contents of a file as a string.
1025 % The format of the FileToStringInfo method is:
1027 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
1028 % ExceptionInfo *exception)
1030 % A description of each parameter follows:
1032 % o filename: the filename.
1034 % o extent: Maximum length of the string.
1036 % o exception: return any errors or warnings in this structure.
1039 MagickExport StringInfo *FileToStringInfo(const char *filename,
1040 const size_t extent,ExceptionInfo *exception)
1045 assert(filename != (const char *) NULL);
1046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1047 assert(exception != (ExceptionInfo *) NULL);
1048 string_info=AcquireStringInfoContainer();
1049 string_info->path=ConstantString(filename);
1050 string_info->datum=(unsigned char *) FileToBlob(filename,extent,
1051 &string_info->length,exception);
1052 if (string_info->datum == (unsigned char *) NULL)
1054 string_info=DestroyStringInfo(string_info);
1055 return((StringInfo *) NULL);
1057 return(string_info);
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 % F o r m a t M a g i c k S i z e %
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071 % FormatMagickSize() converts a size to a human readable format, for example,
1072 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1075 % The format of the FormatMagickSize method is:
1077 % ssize_t FormatMagickSize(const MagickSizeType size,const char *suffix,
1078 % const size_t length,char *format)
1080 % A description of each parameter follows:
1082 % o size: convert this size to a human readable format.
1084 % o bi: use power of two rather than power of ten.
1086 % o suffix: append suffix, typically B or P.
1088 % o length: the maximum length of the string.
1090 % o format: human readable format.
1093 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1094 const MagickBooleanType bi,const char *suffix,const size_t length,
1098 p[MagickPathExtent],
1099 q[MagickPathExtent];
1117 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1119 *traditional_units[] =
1121 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1125 units=traditional_units;
1126 if (bi != MagickFalse)
1131 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1132 extent=(double) ((MagickOffsetType) size);
1134 extent=(double) size;
1136 (void) FormatLocaleString(p,MagickPathExtent,"%.*g",GetMagickPrecision(),
1138 (void) FormatLocaleString(q,MagickPathExtent,"%.20g",extent);
1139 if (strtod(p,(char **) NULL) == strtod(q,(char **) NULL))
1141 if (suffix == (const char *) NULL)
1142 count=FormatLocaleString(format,length,"%.20g%s",extent,units[0]);
1144 count=FormatLocaleString(format,length,"%.20g%s%s",extent,units[0],
1148 for (i=0; (extent >= bytes) && (units[i+1] != (const char *) NULL); i++)
1150 if (suffix == (const char *) NULL)
1151 count=FormatLocaleString(format,length,"%.*g%s",GetMagickPrecision(),
1154 count=FormatLocaleString(format,length,"%.*g%s%s",GetMagickPrecision(),
1155 extent,units[i],suffix);
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164 % F o r m a t M a g i c k T i m e %
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1170 % FormatMagickTime() returns the specified time in the Internet date/time
1171 % format and the length of the timestamp.
1173 % The format of the FormatMagickTime method is:
1175 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1178 % A description of each parameter follows.
1180 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1181 % measured in seconds.
1183 % o length: the maximum length of the string.
1185 % o timestamp: Return the Internet date/time here.
1188 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1201 assert(timestamp != (char *) NULL);
1202 (void) memset(&local_time,0,sizeof(local_time));
1203 (void) memset(&gm_time,0,sizeof(gm_time));
1204 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1205 (void) localtime_r(&time,&local_time);
1211 my_time=localtime(&time);
1212 if (my_time != (struct tm *) NULL)
1213 (void) memcpy(&local_time,my_time,sizeof(local_time));
1216 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1217 (void) gmtime_r(&time,&gm_time);
1223 my_time=gmtime(&time);
1224 if (my_time != (struct tm *) NULL)
1225 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1228 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1229 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1230 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1231 (local_time.tm_yday-gm_time.tm_yday)));
1232 count=FormatLocaleString(timestamp,length,
1233 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1234 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1235 local_time.tm_min,local_time.tm_sec,(long) timezone);
1240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244 % G e t E n v i r o n m e n t V a l u e %
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 % GetEnvironmentValue() returns the environment string that matches the
1253 % The format of the GetEnvironmentValue method is:
1255 % char *GetEnvironmentValue(const char *name)
1257 % A description of each parameter follows:
1259 % o name: the environment name.
1262 MagickExport char *GetEnvironmentValue(const char *name)
1267 environment=getenv(name);
1268 if (environment == (const char *) NULL)
1269 return((char *) NULL);
1270 return(ConstantString(environment));
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1278 % G e t S t r i n g I n f o D a t u m %
1282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284 % GetStringInfoDatum() returns the datum associated with the string.
1286 % The format of the GetStringInfoDatum method is:
1288 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1290 % A description of each parameter follows:
1292 % o string_info: the string info.
1295 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1297 assert(string_info != (StringInfo *) NULL);
1298 assert(string_info->signature == MagickCoreSignature);
1299 return(string_info->datum);
1303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307 % G e t S t r i n g I n f o L e n g t h %
1311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1313 % GetStringInfoLength() returns the string length.
1315 % The format of the GetStringInfoLength method is:
1317 % size_t GetStringInfoLength(const StringInfo *string_info)
1319 % A description of each parameter follows:
1321 % o string_info: the string info.
1324 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1326 assert(string_info != (StringInfo *) NULL);
1327 assert(string_info->signature == MagickCoreSignature);
1328 return(string_info->length);
1332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1336 % G e t S t r i n g I n f o N a m e %
1340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342 % GetStringInfoName() returns the name associated with the string.
1344 % The format of the GetStringInfoName method is:
1346 % const char *GetStringInfoName(const StringInfo *string_info)
1348 % A description of each parameter follows:
1350 % o string_info: the string info.
1353 MagickExport const char *GetStringInfoName(const StringInfo *string_info)
1355 assert(string_info != (StringInfo *) NULL);
1356 assert(string_info->signature == MagickCoreSignature);
1357 return(string_info->name);
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 % G e t S t r i n g I n f o P a t h %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % GetStringInfoPath() returns the path associated with the string.
1373 % The format of the GetStringInfoPath method is:
1375 % const char *GetStringInfoPath(const StringInfo *string_info)
1377 % A description of each parameter follows:
1379 % o string_info: the string info.
1382 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1384 assert(string_info != (StringInfo *) NULL);
1385 assert(string_info->signature == MagickCoreSignature);
1386 return(string_info->path);
1390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1394 + I n t e r p r e t S i P r e f i x V a l u e %
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400 % InterpretSiPrefixValue() converts the initial portion of the string to a
1401 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1404 % The format of the InterpretSiPrefixValue method is:
1406 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1408 % A description of each parameter follows:
1410 % o value: the string value.
1412 % o sentinal: if sentinal is not NULL, return a pointer to the character
1413 % after the last character used in the conversion.
1416 MagickExport double InterpretSiPrefixValue(const char *magick_restrict string,
1417 char **magick_restrict sentinal)
1425 value=InterpretLocaleValue(string,&q);
1428 if ((*q >= 'E') && (*q <= 'z'))
1433 switch ((int) ((unsigned char) *q))
1435 case 'y': e=(-24.0); break;
1436 case 'z': e=(-21.0); break;
1437 case 'a': e=(-18.0); break;
1438 case 'f': e=(-15.0); break;
1439 case 'p': e=(-12.0); break;
1440 case 'n': e=(-9.0); break;
1441 case 'u': e=(-6.0); break;
1442 case 'm': e=(-3.0); break;
1443 case 'c': e=(-2.0); break;
1444 case 'd': e=(-1.0); break;
1445 case 'h': e=2.0; break;
1446 case 'k': e=3.0; break;
1447 case 'K': e=3.0; break;
1448 case 'M': e=6.0; break;
1449 case 'G': e=9.0; break;
1450 case 'T': e=12.0; break;
1451 case 'P': e=15.0; break;
1452 case 'E': e=18.0; break;
1453 case 'Z': e=21.0; break;
1454 case 'Y': e=24.0; break;
1455 default: e=0.0; break;
1457 if (e >= MagickEpsilon)
1461 value*=pow(2.0,e/0.3);
1471 if ((*q == 'B') || (*q == 'P'))
1474 if (sentinal != (char **) NULL)
1480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % I s S t r i n g T r u e %
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1491 % "1". Any other string or undefined returns MagickFalse.
1493 % Typically this is used to look at strings (options or artifacts) which
1494 % has a default value of "false", when not defined.
1496 % The format of the IsStringTrue method is:
1498 % MagickBooleanType IsStringTrue(const char *value)
1500 % A description of each parameter follows:
1502 % o value: Specifies a pointer to a character array.
1505 MagickExport MagickBooleanType IsStringTrue(const char *value)
1507 if (value == (const char *) NULL)
1508 return(MagickFalse);
1509 if (LocaleCompare(value,"true") == 0)
1511 if (LocaleCompare(value,"on") == 0)
1513 if (LocaleCompare(value,"yes") == 0)
1515 if (LocaleCompare(value,"1") == 0)
1517 return(MagickFalse);
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % I s S t r i n g F a l s e %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % IsStringFalse() returns MagickTrue if the value is "false", "off", "no" or
1532 % "0". Any other string or undefined returns MagickFalse.
1534 % Typically this is used to look at strings (options or artifacts) which
1535 % has a default value of "true", when it has not been defined.
1537 % The format of the IsStringFalse method is:
1539 % MagickBooleanType IsStringFalse(const char *value)
1541 % A description of each parameter follows:
1543 % o value: Specifies a pointer to a character array.
1546 MagickExport MagickBooleanType IsStringFalse(const char *value)
1548 if (value == (const char *) NULL)
1549 return(MagickFalse);
1550 if (LocaleCompare(value,"false") == 0)
1552 if (LocaleCompare(value,"off") == 0)
1554 if (LocaleCompare(value,"no") == 0)
1556 if (LocaleCompare(value,"0") == 0)
1558 return(MagickFalse);
1562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1566 % P r i n t S t r i n g I n f o %
1570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1572 % PrintStringInfo() prints the string.
1574 % The format of the PrintStringInfo method is:
1576 % void PrintStringInfo(FILE *file,const char *id,
1577 % const StringInfo *string_info)
1579 % A description of each parameter follows:
1581 % o file: the file, typically stdout.
1583 % o id: the string id.
1585 % o string_info: the string info.
1588 MagickExport void PrintStringInfo(FILE *file,const char *id,
1589 const StringInfo *string_info)
1598 assert(id != (const char *) NULL);
1599 assert(string_info != (StringInfo *) NULL);
1600 assert(string_info->signature == MagickCoreSignature);
1601 p=(char *) string_info->datum;
1602 for (i=0; i < string_info->length; i++)
1604 if (((int) ((unsigned char) *p) < 32) &&
1605 (isspace((int) ((unsigned char) *p)) == 0))
1609 (void) FormatLocaleFile(file,"%s(%.20g):\n",id,(double) string_info->length);
1610 if (i == string_info->length)
1612 for (i=0; i < string_info->length; i++)
1613 (void) fputc(string_info->datum[i],file);
1614 (void) fputc('\n',file);
1618 Convert string to a HEX list.
1620 p=(char *) string_info->datum;
1621 for (i=0; i < string_info->length; i+=CharsPerLine)
1623 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (CharsPerLine*i));
1624 for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1626 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1627 if ((j % 0x04) == 0)
1628 (void) fputc(' ',file);
1630 for ( ; j <= CharsPerLine; j++)
1632 (void) fputc(' ',file);
1633 (void) fputc(' ',file);
1634 if ((j % 0x04) == 0)
1635 (void) fputc(' ',file);
1637 (void) fputc(' ',file);
1638 for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1640 if (isprint((int) ((unsigned char) *p)) != 0)
1641 (void) fputc(*p,file);
1643 (void) fputc('-',file);
1646 (void) fputc('\n',file);
1651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655 % R e s e t S t r i n g I n f o %
1659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1661 % ResetStringInfo() reset the string to all null bytes.
1663 % The format of the ResetStringInfo method is:
1665 % void ResetStringInfo(StringInfo *string_info)
1667 % A description of each parameter follows:
1669 % o string_info: the string info.
1672 MagickExport void ResetStringInfo(StringInfo *string_info)
1674 assert(string_info != (StringInfo *) NULL);
1675 assert(string_info->signature == MagickCoreSignature);
1676 (void) memset(string_info->datum,0,string_info->length);
1680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1684 % S a n t i z e S t r i n g %
1688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1690 % SanitizeString() returns an new string removes all characters except
1691 % letters, digits and !#$%&'*+-=?^_`{|}~@.[].
1693 % The returned string shoud be freed using DestoryString().
1695 % The format of the SanitizeString method is:
1697 % char *SanitizeString(const char *source)
1699 % A description of each parameter follows:
1701 % o source: A character string.
1704 MagickExport char *SanitizeString(const char *source)
1717 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1718 "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1720 sanitize_source=AcquireString(source);
1722 q=sanitize_source+strlen(sanitize_source);
1723 for (p+=strspn(p,whitelist); p != q; p+=strspn(p,whitelist))
1725 return(sanitize_source);
1729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % S e t S t r i n g I n f o %
1737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739 % SetStringInfo() copies the source string to the destination string.
1741 % The format of the SetStringInfo method is:
1743 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1745 % A description of each parameter follows:
1747 % o string_info: the string info.
1749 % o source: the source string.
1752 MagickExport void SetStringInfo(StringInfo *string_info,
1753 const StringInfo *source)
1755 assert(string_info != (StringInfo *) NULL);
1756 assert(string_info->signature == MagickCoreSignature);
1757 assert(source != (StringInfo *) NULL);
1758 assert(source->signature == MagickCoreSignature);
1759 if (string_info->length == 0)
1761 (void) memset(string_info->datum,0,string_info->length);
1762 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1771 % S e t S t r i n g I n f o D a t u m %
1775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1777 % SetStringInfoDatum() copies bytes from the source string for the length of
1778 % the destination string.
1780 % The format of the SetStringInfoDatum method is:
1782 % void SetStringInfoDatum(StringInfo *string_info,
1783 % const unsigned char *source)
1785 % A description of each parameter follows:
1787 % o string_info: the string info.
1789 % o source: the source string.
1792 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1793 const unsigned char *source)
1795 assert(string_info != (StringInfo *) NULL);
1796 assert(string_info->signature == MagickCoreSignature);
1797 if (string_info->length != 0)
1798 (void) memcpy(string_info->datum,source,string_info->length);
1802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1806 % S e t S t r i n g I n f o L e n g t h %
1810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812 % SetStringInfoLength() set the string length to the specified value.
1814 % The format of the SetStringInfoLength method is:
1816 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1818 % A description of each parameter follows:
1820 % o string_info: the string info.
1822 % o length: the string length.
1825 MagickExport void SetStringInfoLength(StringInfo *string_info,
1826 const size_t length)
1828 assert(string_info != (StringInfo *) NULL);
1829 assert(string_info->signature == MagickCoreSignature);
1830 if (string_info->length == length)
1832 if (~length < MagickPathExtent)
1833 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1834 string_info->length=length;
1835 if (string_info->datum == (unsigned char *) NULL)
1836 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1837 MagickPathExtent,sizeof(*string_info->datum));
1839 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1840 length+MagickPathExtent,sizeof(*string_info->datum));
1841 if (string_info->datum == (unsigned char *) NULL)
1842 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1850 % S e t S t r i n g I n f o N a m e %
1854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1856 % SetStringInfoName() sets the name associated with the string.
1858 % The format of the SetStringInfoName method is:
1860 % void SetStringInfoName(StringInfo *string_info,const char *name)
1862 % A description of each parameter follows:
1864 % o string_info: the string info.
1869 MagickExport void SetStringInfoName(StringInfo *string_info,const char *name)
1871 assert(string_info != (StringInfo *) NULL);
1872 assert(string_info->signature == MagickCoreSignature);
1873 assert(name != (const char *) NULL);
1874 string_info->name=ConstantString(name);
1878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1882 % S e t S t r i n g I n f o P a t h %
1886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1888 % SetStringInfoPath() sets the path associated with the string.
1890 % The format of the SetStringInfoPath method is:
1892 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1894 % A description of each parameter follows:
1896 % o string_info: the string info.
1901 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1903 assert(string_info != (StringInfo *) NULL);
1904 assert(string_info->signature == MagickCoreSignature);
1905 assert(path != (const char *) NULL);
1906 string_info->path=ConstantString(path);
1910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1914 % S p l i t S t r i n g I n f o %
1918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1920 % SplitStringInfo() splits a string into two and returns it.
1922 % The format of the SplitStringInfo method is:
1924 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1926 % A description of each parameter follows:
1928 % o string_info: the string info.
1931 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1932 const size_t offset)
1937 assert(string_info != (StringInfo *) NULL);
1938 assert(string_info->signature == MagickCoreSignature);
1939 if (offset > string_info->length)
1940 return((StringInfo *) NULL);
1941 split_info=AcquireStringInfo(offset);
1942 SetStringInfo(split_info,string_info);
1943 (void) memmove(string_info->datum,string_info->datum+offset,
1944 string_info->length-offset+MagickPathExtent);
1945 SetStringInfoLength(string_info,string_info->length-offset);
1950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954 % S t r i n g I n f o T o S t r i n g %
1958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960 % StringInfoToString() converts a string info string to a C string.
1962 % The format of the StringInfoToString method is:
1964 % char *StringInfoToString(const StringInfo *string_info)
1966 % A description of each parameter follows:
1968 % o string_info: the string.
1971 MagickExport char *StringInfoToString(const StringInfo *string_info)
1979 string=(char *) NULL;
1980 length=string_info->length;
1981 if (~length >= (MagickPathExtent-1))
1982 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1984 if (string == (char *) NULL)
1985 return((char *) NULL);
1986 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1987 string[length]='\0';
1992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1996 % S t r i n g I n f o T o H e x S t r i n g %
2000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2002 % StringInfoToHexString() converts a string info string to a C string.
2004 % The format of the StringInfoToHexString method is:
2006 % char *StringInfoToHexString(const StringInfo *string_info)
2008 % A description of each parameter follows:
2010 % o string_info: the string.
2013 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
2018 register const unsigned char
2024 register unsigned char
2033 length=string_info->length;
2034 if (~length < MagickPathExtent)
2035 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2036 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,2*
2038 if (string == (char *) NULL)
2039 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2056 p=string_info->datum;
2057 q=(unsigned char *) string;
2058 for (i=0; i < (ssize_t) string_info->length; i++)
2060 *q++=hex_digits[(*p >> 4) & 0x0f];
2061 *q++=hex_digits[*p & 0x0f];
2069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2073 % S t r i n g T o A r g v %
2077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2079 % StringToArgv() converts a text string into command line arguments.
2080 % The 'argv' array of arguments, is returned while the number of arguments
2081 % is returned via the provided integer variable pointer.
2083 % Simple 'word' tokenizer, which allows for each word to be optionally
2084 % quoted. However it will not allow use of partial quotes, or escape
2087 % The format of the StringToArgv method is:
2089 % char **StringToArgv(const char *text,int *argc)
2091 % A description of each parameter follows:
2093 % o argv: Method StringToArgv returns the string list unless an error
2094 % occurs, otherwise NULL.
2096 % o text: Specifies the string to segment into a list.
2098 % o argc: This integer pointer returns the number of arguments in the
2102 MagickExport char **StringToArgv(const char *text,int *argc)
2115 if (text == (char *) NULL)
2116 return((char **) NULL);
2118 Determine the number of arguments.
2120 for (p=text; *p != '\0'; )
2122 while (isspace((int) ((unsigned char) *p)) != 0)
2128 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2130 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2131 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2135 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2136 if (argv == (char **) NULL)
2137 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2139 Convert string to an ASCII list.
2141 argv[0]=AcquireString("magick");
2143 for (i=1; i < (ssize_t) *argc; i++)
2145 while (isspace((int) ((unsigned char) *p)) != 0)
2151 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2157 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2160 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2162 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
2164 if (argv[i] == (char *) NULL)
2166 for (i--; i >= 0; i--)
2167 argv[i]=DestroyString(argv[i]);
2168 argv=(char **) RelinquishMagickMemory(argv);
2169 ThrowFatalException(ResourceLimitFatalError,
2170 "UnableToConvertStringToARGV");
2172 (void) memcpy(argv[i],p,(size_t) (q-p));
2175 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2178 argv[i]=(char *) NULL;
2183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2187 % S t r i n g T o A r r a y O f D o u b l e s %
2191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2193 % StringToArrayOfDoubles() converts a string of space or comma separated
2194 % numbers into array of floating point numbers (doubles). Any number that
2195 % failes to parse properly will produce a syntax error. As will two commas
2196 % without a number between them. However a final comma at the end will
2197 % not be regarded as an error so as to simplify automatic list generation.
2199 % A NULL value is returned on syntax or memory errors.
2201 % Use RelinquishMagickMemory() to free returned array when finished.
2203 % The format of the StringToArrayOfDoubles method is:
2205 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2206 % ExceptionInfo *exception)
2208 % A description of each parameter follows:
2210 % o string: the string containing the comma/space separated values.
2212 % o count: returns number of arguments in returned array
2214 % o exception: return any errors or warnings in this structure.
2217 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2218 ExceptionInfo *exception)
2233 Determine count of values, and check syntax.
2235 assert(exception != (ExceptionInfo *) NULL);
2236 assert(exception->signature == MagickCoreSignature);
2238 if (string == (char *) NULL)
2239 return((double *) NULL); /* no value found */
2244 (void) StringToDouble(p,&q); /* get value - ignores leading space */
2246 return((double *) NULL); /* no value found */
2248 i++; /* increment value count */
2249 while (isspace((int) ((unsigned char) *p)) != 0)
2250 p++; /* skip spaces */
2252 p++; /* skip comma */
2253 while (isspace((int) ((unsigned char) *p)) != 0)
2254 p++; /* and more spaces */
2257 Allocate floating point argument list.
2260 array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2261 if (array == (double *) NULL)
2263 (void) ThrowMagickException(exception,GetMagickModule(),
2264 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2265 return((double *) NULL);
2268 Fill in the floating point values.
2272 while ((*p != '\0') && (i < *count))
2274 array[i++]=StringToDouble(p,&q);
2276 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2287 + S t r i n g T o k e n %
2291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293 % StringToken() looks for any one of given delimiters and splits the string
2294 % into two separate strings by replacing the delimiter character found with a
2297 % The given string pointer is changed to point to the string following the
2298 % delimiter character found, or NULL. A pointer to the start of the
2299 % string is returned, representing the token before the delimiter.
2301 % In may ways this is equivent to the strtok() C library function, but with
2302 % multiple delimiter characters rather than a delimiter string.
2304 % The format of the StringToken method is:
2306 % char *StringToken(const char *delimiters,char **string)
2308 % A description of each parameter follows:
2310 % o delimiters: one or more delimiters.
2312 % o string: return the first token in the string. If none is found, return
2316 MagickExport char *StringToken(const char *delimiters,char **string)
2332 if (p == (char *) NULL)
2333 return((char *) NULL);
2351 } while (d != '\0');
2356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2360 % S t r i n g T o L i s t %
2364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2366 % StringToList() converts a text string into a list by segmenting the text
2367 % string at each carriage return discovered. The list is converted to HEX
2368 % characters if any control characters are discovered within the text string.
2370 % The format of the StringToList method is:
2372 % char **StringToList(const char *text)
2374 % A description of each parameter follows:
2376 % o text: Specifies the string to segment into a list.
2379 MagickExport char **StringToList(const char *text)
2381 return(StringToStrings(text, (size_t *) NULL));
2385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2389 % S t r i n g T o S t r i n g s %
2393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2395 % StringToList() converts a text string into a list by segmenting the text
2396 % string at each carriage return discovered. The list is converted to HEX
2397 % characters if any control characters are discovered within the text string.
2399 % The format of the StringToList method is:
2401 % char **StringToList(const char *text,size_t *lines)
2403 % A description of each parameter follows:
2405 % o text: Specifies the string to segment into a list.
2407 % o count: Return value for the number of items in the list.
2410 MagickExport char **StringToStrings(const char *text,size_t *count)
2424 if (text == (char *) NULL)
2426 if (count != (size_t *) NULL)
2428 return((char **) NULL);
2430 for (p=text; *p != '\0'; p++)
2431 if (((int) ((unsigned char) *p) < 32) &&
2432 (isspace((int) ((unsigned char) *p)) == 0))
2440 Convert string to an ASCII list.
2443 for (p=text; *p != '\0'; p++)
2446 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2448 if (textlist == (char **) NULL)
2449 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2451 for (i=0; i < (ssize_t) lines; i++)
2453 for (q=p; *q != '\0'; q++)
2454 if ((*q == '\r') || (*q == '\n'))
2456 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
2457 sizeof(**textlist));
2458 if (textlist[i] == (char *) NULL)
2459 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2460 (void) memcpy(textlist[i],p,(size_t) (q-p));
2461 textlist[i][q-p]='\0';
2470 hex_string[MagickPathExtent];
2479 Convert string to a HEX list.
2481 lines=(size_t) (strlen(text)/CharsPerLine)+1;
2482 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2484 if (textlist == (char **) NULL)
2485 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2487 for (i=0; i < (ssize_t) lines; i++)
2489 textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2490 sizeof(**textlist));
2491 if (textlist[i] == (char *) NULL)
2492 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2493 (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2494 (long) (CharsPerLine*i));
2495 q=textlist[i]+strlen(textlist[i]);
2496 for (j=1; j <= (ssize_t) MagickMin(strlen(p),CharsPerLine); j++)
2498 (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2499 (void) CopyMagickString(q,hex_string,MagickPathExtent);
2501 if ((j % 0x04) == 0)
2504 for ( ; j <= CharsPerLine; j++)
2508 if ((j % 0x04) == 0)
2512 for (j=1; j <= (ssize_t) MagickMin(strlen(p),CharsPerLine); j++)
2514 if (isprint((int) ((unsigned char) *p)) != 0)
2521 textlist[i]=(char *) ResizeQuantumMemory(textlist[i],q-textlist[i]+1,
2522 sizeof(**textlist));
2523 if (textlist[i] == (char *) NULL)
2524 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2527 if (count != (size_t *) NULL)
2529 textlist[i]=(char *) NULL;
2534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2538 % S t r i n g T o S t r i n g I n f o %
2542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2544 % StringToStringInfo() converts a string to a StringInfo type.
2546 % The format of the StringToStringInfo method is:
2548 % StringInfo *StringToStringInfo(const char *string)
2550 % A description of each parameter follows:
2552 % o string: The string.
2555 MagickExport StringInfo *StringToStringInfo(const char *string)
2560 assert(string != (const char *) NULL);
2561 string_info=AcquireStringInfo(strlen(string));
2562 SetStringInfoDatum(string_info,(const unsigned char *) string);
2563 return(string_info);
2567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2571 % S t r i p S t r i n g %
2575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2577 % StripString() strips any whitespace or quotes from the beginning and end of
2578 % a string of characters.
2580 % The format of the StripString method is:
2582 % void StripString(char *message)
2584 % A description of each parameter follows:
2586 % o message: Specifies an array of characters.
2589 MagickExport void StripString(char *message)
2598 assert(message != (char *) NULL);
2599 if (*message == '\0')
2601 length=strlen(message);
2603 while (isspace((int) ((unsigned char) *p)) != 0)
2605 if ((*p == '\'') || (*p == '"'))
2608 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2611 if ((*q == '\'') || (*q == '"'))
2613 (void) memmove(message,p,(size_t) (q-p+1));
2614 message[q-p+1]='\0';
2615 for (p=message; *p != '\0'; p++)
2621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2625 % S u b s t i t u t e S t r i n g %
2629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2631 % SubstituteString() performs string substitution on a string, replacing the
2632 % string with the substituted version. Buffer must be allocated from the heap.
2633 % If the string is matched and status, MagickTrue is returned otherwise
2636 % The format of the SubstituteString method is:
2638 % MagickBooleanType SubstituteString(char **string,const char *search,
2639 % const char *replace)
2641 % A description of each parameter follows:
2643 % o string: the string to perform replacements on; replaced with new
2644 % allocation if a replacement is made.
2646 % o search: search for this string.
2648 % o replace: replace any matches with this string.
2651 MagickExport MagickBooleanType SubstituteString(char **string,
2652 const char *search,const char *replace)
2671 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2673 if (search_extent == 0)
2674 search_extent=strlen(search);
2675 if (strncmp(p,search,search_extent) != 0)
2681 if (replace_extent == 0)
2682 replace_extent=strlen(replace);
2683 if (replace_extent > search_extent)
2686 Make room for the replacement string.
2688 offset=(ssize_t) (p-(*string));
2689 extent=strlen(*string)+replace_extent-search_extent+1;
2690 *string=(char *) ResizeQuantumMemory(*string,extent+MagickPathExtent,
2692 if (*string == (char *) NULL)
2693 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2699 if (search_extent != replace_extent)
2700 (void) memmove(p+replace_extent,p+search_extent,
2701 strlen(p+search_extent)+1);
2702 (void) memcpy(p,replace,replace_extent);
2703 p+=replace_extent-1;