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-2010 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 % http://www.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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/list.h"
48 #include "magick/log.h"
49 #include "magick/memory_.h"
50 #include "magick/property.h"
51 #include "magick/resource_.h"
52 #include "magick/signature-private.h"
53 #include "magick/string_.h"
58 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
59 static const unsigned char
62 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
63 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
64 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
65 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
66 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
67 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
68 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
69 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
70 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
71 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
72 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
73 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
74 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
75 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
76 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
77 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
78 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
79 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
80 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
81 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
82 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
83 0xfc, 0xfd, 0xfe, 0xff,
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % A c q u i r e S t r i n g %
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % AcquireString() allocates memory for a string and copies the source string
99 % to that memory location (and returns it).
101 % The format of the AcquireString method is:
103 % char *AcquireString(const char *source)
105 % A description of each parameter follows:
107 % o source: A character string.
110 MagickExport char *AcquireString(const char *source)
119 if (source != (char *) NULL)
120 length+=strlen(source);
121 if (~length < MaxTextExtent)
122 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
123 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
124 sizeof(*destination));
125 if (destination == (char *) NULL)
126 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
128 if (source != (char *) NULL)
129 (void) memcpy(destination,source,length*sizeof(*destination));
130 destination[length]='\0';
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % A c q u i r e S t r i n g I n f o %
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % AcquireStringInfo() allocates the StringInfo structure.
147 % The format of the AcquireStringInfo method is:
149 % StringInfo *AcquireStringInfo(const size_t length)
151 % A description of each parameter follows:
153 % o length: the string length.
156 MagickExport StringInfo *AcquireStringInfo(const size_t length)
161 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
162 if (string_info == (StringInfo *) NULL)
163 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
164 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
165 string_info->signature=MagickSignature;
166 string_info->length=length;
167 if (string_info->length != 0)
169 string_info->datum=(unsigned char *) NULL;
170 if (~string_info->length >= MaxTextExtent)
171 string_info->datum=(unsigned char *) AcquireQuantumMemory(
172 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
173 if (string_info->datum == (unsigned char *) NULL)
174 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % C l o n e S t r i n g %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % CloneString() allocates memory for the destination string and copies
191 % the source string to that memory location.
193 % The format of the CloneString method is:
195 % char *CloneString(char **destination,const char *source)
197 % A description of each parameter follows:
199 % o destination: A pointer to a character string.
201 % o source: A character string.
204 MagickExport char *CloneString(char **destination,const char *source)
209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
210 assert(destination != (char **) NULL);
211 if (source == (const char *) NULL)
213 if (*destination != (char *) NULL)
214 *destination=DestroyString(*destination);
215 return(*destination);
217 if (*destination == (char *) NULL)
219 *destination=AcquireString(source);
220 return(*destination);
222 length=strlen(source);
223 if (~length < MaxTextExtent)
224 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
225 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
226 sizeof(*destination));
227 if (*destination == (char *) NULL)
228 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
230 (void) memcpy(*destination,source,length*sizeof(*destination));
231 (*destination)[length]='\0';
232 return(*destination);
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % C l o n e S t r i n g I n f o %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % CloneStringInfo() clones a copy of the StringInfo structure.
248 % The format of the CloneStringInfo method is:
250 % StringInfo *CloneStringInfo(const StringInfo *string_info)
252 % A description of each parameter follows:
254 % o string_info: the string info.
257 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
263 assert(string_info != (StringInfo *) NULL);
264 assert(string_info->signature == MagickSignature);
265 clone_info=AcquireStringInfo(string_info->length);
266 if (string_info->length != 0)
267 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % C o m p a r e S t r i n g I n f o %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % CompareStringInfo() compares the two datums target and source. It returns
283 % an integer less than, equal to, or greater than zero if target is found,
284 % respectively, to be less than, to match, or be greater than source.
286 % The format of the CompareStringInfo method is:
288 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
290 % A description of each parameter follows:
292 % o target: the target string.
294 % o source: the source string.
298 static inline size_t MagickMin(const size_t x,const size_t y)
305 MagickExport int CompareStringInfo(const StringInfo *target,
306 const StringInfo *source)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
312 assert(target != (StringInfo *) NULL);
313 assert(target->signature == MagickSignature);
314 assert(source != (StringInfo *) NULL);
315 assert(source->signature == MagickSignature);
316 status=memcmp(target->datum,source->datum,MagickMin(target->length,
320 if (target->length == source->length)
322 return(target->length < source->length ? -1 : 1);
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % C o n c a t e n a t e M a g i c k S t r i n g %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % ConcatenateMagickString() concatenates the source string to the destination
337 % string. The destination buffer is always null-terminated even if the
338 % string must be truncated.
340 % The format of the ConcatenateMagickString method is:
342 % size_t ConcatenateMagickString(char *destination,const char *source,
343 % const size_t length)
345 % A description of each parameter follows:
347 % o destination: the destination string.
349 % o source: the source string.
351 % o length: the length of the destination string.
354 MagickExport size_t ConcatenateMagickString(char *destination,
355 const char *source,const size_t length)
369 assert(destination != (char *) NULL);
370 assert(source != (const char *) NULL);
375 while ((i-- != 0) && (*q != '\0'))
377 count=(size_t) (q-destination);
380 return(count+strlen(p));
391 return(count+(p-source));
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % C o n c a t e n a t e S t r i n g %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % ConcatenateString() appends a copy of string source, including the
406 % terminating null character, to the end of string destination.
408 % The format of the ConcatenateString method is:
410 % MagickBooleanType ConcatenateString(char **destination,
411 % const char *source)
413 % A description of each parameter follows:
415 % o destination: A pointer to a character string.
417 % o source: A character string.
420 MagickExport MagickBooleanType ConcatenateString(char **destination,
428 assert(destination != (char **) NULL);
429 if (source == (const char *) NULL)
431 if (*destination == (char *) NULL)
433 *destination=AcquireString(source);
436 destination_length=strlen(*destination);
437 source_length=strlen(source);
438 length=destination_length;
439 if (~length < source_length)
440 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
441 length+=source_length;
442 if (~length < MaxTextExtent)
443 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
444 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
445 sizeof(*destination));
446 if (*destination == (char *) NULL)
447 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
448 if (source_length != 0)
449 (void) memcpy((*destination)+destination_length,source,source_length);
450 (*destination)[length]='\0';
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 % C o n c a t e n a t e S t r i n g I n f o %
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 % ConcatenateStringInfo() concatenates the source string to the destination
468 % The format of the ConcatenateStringInfo method is:
470 % void ConcatenateStringInfo(StringInfo *string_info,
471 % const StringInfo *source)
473 % A description of each parameter follows:
475 % o string_info: the string info.
477 % o source: the source string.
480 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
481 const StringInfo *source)
486 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
487 assert(string_info != (StringInfo *) NULL);
488 assert(string_info->signature == MagickSignature);
489 assert(source != (const StringInfo *) NULL);
490 length=string_info->length;
491 if (~length < source->length)
492 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
493 SetStringInfoLength(string_info,length+source->length);
494 (void) memcpy(string_info->datum+length,source->datum,source->length);
498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 % 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 %
506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 % ConfigureFileToStringInfo() returns the contents of a configure file as a
511 % The format of the ConfigureFileToStringInfo method is:
513 % StringInfo *ConfigureFileToStringInfo(const char *filename)
514 % ExceptionInfo *exception)
516 % A description of each parameter follows:
518 % o filename: the filename.
521 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
541 assert(filename != (const char *) NULL);
542 file=open(filename,O_RDONLY | O_BINARY);
544 return((StringInfo *) NULL);
545 offset=(MagickOffsetType) MagickSeek(file,0,SEEK_END);
546 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
549 return((StringInfo *) NULL);
551 length=(size_t) offset;
552 string=(char *) NULL;
553 if (~length > MaxTextExtent)
554 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
555 if (string == (char *) NULL)
558 return((StringInfo *) NULL);
560 map=MapBlob(file,ReadMode,0,length);
561 if (map != (void *) NULL)
563 (void) memcpy(string,map,length);
564 (void) UnmapBlob(map,length);
574 (void) MagickSeek(file,0,SEEK_SET);
575 for (i=0; i < length; i+=count)
577 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
589 string=DestroyString(string);
590 return((StringInfo *) NULL);
595 string_info=AcquireStringInfo(0);
596 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
597 string_info->length=length;
598 string_info->datum=(unsigned char *) string;
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % C o n s t a n t S t r i n g %
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % ConstantString() allocates memory for a string and copies the source string
614 % to that memory location (and returns it). Use it for strings that you do
615 % do not expect to change over its lifetime.
617 % The format of the ConstantString method is:
619 % char *ConstantString(const char *source)
621 % A description of each parameter follows:
623 % o source: A character string.
626 MagickExport char *ConstantString(const char *source)
635 if (source != (char *) NULL)
636 length+=strlen(source);
637 destination=(char *) NULL;
639 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
640 if (destination == (char *) NULL)
641 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
643 if (source != (char *) NULL)
644 (void) memcpy(destination,source,length*sizeof(*destination));
645 destination[length]='\0';
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % C o p y M a g i c k S t r i n g %
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 % CopyMagickString() copies the source string to the destination string. The
661 % destination buffer is always null-terminated even if the string must be
662 % truncated. The return value is the minimum of the source string length
663 % or the length parameter.
665 % The format of the CopyMagickString method is:
667 % size_t CopyMagickString(const char *destination,char *source,
668 % const size_t length)
670 % A description of each parameter follows:
672 % o destination: the destination string.
674 % o source: the source string.
676 % o length: the length of the destination string.
679 MagickExport size_t CopyMagickString(char *destination,const char *source,
693 for (n=length; n > 4; n-=4)
697 return((size_t) (p-source-1));
701 return((size_t) (p-source-1));
705 return((size_t) (p-source-1));
709 return((size_t) (p-source-1));
713 for (n--; n != 0; n--)
717 return((size_t) (p-source-1));
722 return((size_t) (p-source-1));
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % D e s t r o y S t r i n g %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % DestroyString() destroys memory associated with a string.
738 % The format of the DestroyString method is:
740 % char *DestroyString(char *string)
742 % A description of each parameter follows:
744 % o string: the string.
747 MagickExport char *DestroyString(char *string)
749 return((char *) RelinquishMagickMemory(string));
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % D e s t r o y S t r i n g I n f o %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
765 % The format of the DestroyStringInfo method is:
767 % StringInfo *DestroyStringInfo(StringInfo *string_info)
769 % A description of each parameter follows:
771 % o string_info: the string info.
774 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
776 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
777 assert(string_info != (StringInfo *) NULL);
778 assert(string_info->signature == MagickSignature);
779 if (string_info->datum != (unsigned char *) NULL)
780 string_info->datum=(unsigned char *) RelinquishMagickMemory(
782 string_info->signature=(~MagickSignature);
783 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 % D e s t r o y S t r i n g L i s t %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % DestroyStringList() zeros memory associated with a string list.
800 % The format of the DestroyStringList method is:
802 % char **DestroyStringList(char **list)
804 % A description of each parameter follows:
806 % o list: the string list.
809 MagickExport char **DestroyStringList(char **list)
814 assert(list != (char **) NULL);
815 for (i=0; list[i] != (char *) NULL; i++)
816 list[i]=DestroyString(list[i]);
817 list=(char **) RelinquishMagickMemory(list);
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % E s c a p e S t r i n g %
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 % EscapeString() allocates memory for a backslash-escaped version of a
833 % source text string, copies the escaped version of the text to that
834 % memory location while adding backslash characters, and returns the
837 % The format of the EscapeString method is:
839 % char *EscapeString(const char *source,const char escape)
841 % A description of each parameter follows:
843 % o allocate_string: Method EscapeString returns the escaped string.
845 % o source: A character string.
847 % o escape: the quoted string termination character to escape (e.g. '"').
850 MagickExport char *EscapeString(const char *source,const char escape)
864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
865 assert(source != (const char *) NULL);
866 length=strlen(source);
867 for (p=source; *p != '\0'; p++)
868 if ((*p == '\\') || (*p == escape))
871 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
874 destination=(char *) NULL;
875 if (~length >= MaxTextExtent)
876 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
877 sizeof(*destination));
878 if (destination == (char *) NULL)
879 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
881 if (source != (char *) NULL)
884 for (p=source; *p != '\0'; p++)
886 if ((*p == '\\') || (*p == escape))
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 % F i l e T o S t r i n g %
904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
906 % FileToString() returns the contents of a file as a string.
908 % The format of the FileToString method is:
910 % char *FileToString(const char *filename,const size_t extent,
911 % ExceptionInfo *exception)
913 % A description of each parameter follows:
915 % o filename: the filename.
917 % o extent: Maximum length of the string.
919 % o exception: return any errors or warnings in this structure.
922 MagickExport char *FileToString(const char *filename,const size_t extent,
923 ExceptionInfo *exception)
928 assert(filename != (const char *) NULL);
929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
930 assert(exception != (ExceptionInfo *) NULL);
931 return((char *) FileToBlob(filename,extent,&length,exception));
935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939 % F i l e T o S t r i n g I n f o %
943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
945 % FileToStringInfo() returns the contents of a file as a string.
947 % The format of the FileToStringInfo method is:
949 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
950 % ExceptionInfo *exception)
952 % A description of each parameter follows:
954 % o filename: the filename.
956 % o extent: Maximum length of the string.
958 % o exception: return any errors or warnings in this structure.
961 MagickExport StringInfo *FileToStringInfo(const char *filename,
962 const size_t extent,ExceptionInfo *exception)
967 assert(filename != (const char *) NULL);
968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
969 assert(exception != (ExceptionInfo *) NULL);
970 string_info=AcquireStringInfo(0);
971 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
972 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
973 if (string_info->datum == (unsigned char *) NULL)
975 string_info=DestroyStringInfo(string_info);
976 return((StringInfo *) NULL);
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 % F o r m a t M a g i c k S i z e %
990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % FormatMagickSize() converts a size to a human readable format, for example,
993 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
996 % The format of the FormatMagickSize method is:
998 % ssize_t FormatMagickSize(const MagickSizeType size,char *format)
1000 % A description of each parameter follows:
1002 % o size: convert this size to a human readable format.
1004 % o bi: use power of two rather than power of ten.
1006 % o format: human readable format.
1009 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1010 const MagickBooleanType bi,char *format)
1029 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1031 *traditional_units[] =
1033 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1037 units=traditional_units;
1038 if (bi != MagickFalse)
1043 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1044 length=(double) ((MagickOffsetType) size);
1046 length=(double) size;
1048 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1050 for (j=2; j < 12; j++)
1052 count=FormatMagickString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
1054 if (strchr(format,'+') == (char *) NULL)
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 % F o r m a t M a g i c k S t r i n g %
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071 % FormatMagickString() prints formatted output of a variable argument list.
1073 % The format of the FormatMagickString method is:
1075 % ssize_t FormatMagickString(char *string,const size_t length,
1076 % const char *format,...)
1078 % A description of each parameter follows.
1080 % o string: FormatMagickString() returns the formatted string in this
1083 % o length: the maximum length of the string.
1085 % o format: A string describing the format to use to write the remaining
1090 MagickExport ssize_t FormatMagickStringList(char *string,const size_t length,
1091 const char *format,va_list operands)
1096 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1097 n=vsnprintf(string,length,format,operands);
1099 n=vsprintf(string,format,operands);
1102 string[length-1]='\0';
1103 return((ssize_t) n);
1106 MagickExport ssize_t FormatMagickString(char *string,const size_t length,
1107 const char *format,...)
1115 va_start(operands,format);
1116 n=(ssize_t) FormatMagickStringList(string,length,format,operands);
1122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1126 % F o r m a t M a g i c k T i m e %
1130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1132 % FormatMagickTime() returns the specified time in the Internet date/time
1133 % format and the length of the timestamp.
1135 % The format of the FormatMagickTime method is:
1137 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1140 % A description of each parameter follows.
1142 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1143 % measured in seconds.
1145 % o length: the maximum length of the string.
1147 % o timestamp: Return the Internet date/time here.
1150 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1163 assert(timestamp != (char *) NULL);
1164 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1165 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1166 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1167 (void) localtime_r(&time,&local_time);
1173 my_time=localtime(&time);
1174 if (my_time != (struct tm *) NULL)
1175 (void) memcpy(&local_time,my_time,sizeof(local_time));
1178 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1179 (void) gmtime_r(&time,&gm_time);
1185 my_time=gmtime(&time);
1186 if (my_time != (struct tm *) NULL)
1187 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1190 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1191 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1192 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1193 (local_time.tm_yday-gm_time.tm_yday)));
1194 count=FormatMagickString(timestamp,length,
1195 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1196 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1197 local_time.tm_min,local_time.tm_sec,(long) timezone);
1202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206 % G e t E n v i r o n m e n t V a l u e %
1210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1212 % GetEnvironmentValue() returns the environment string that matches the
1215 % The format of the GetEnvironmentValue method is:
1217 % char *GetEnvironmentValue(const char *name)
1219 % A description of each parameter follows:
1221 % o name: the environment name.
1224 MagickExport char *GetEnvironmentValue(const char *name)
1229 environment=getenv(name);
1230 if (environment == (const char *) NULL)
1231 return((char *) NULL);
1232 return(ConstantString(environment));
1236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1240 % G e t S t r i n g I n f o D a t u m %
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246 % GetStringInfoDatum() returns the datum associated with the string.
1248 % The format of the GetStringInfoDatum method is:
1250 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1252 % A description of each parameter follows:
1254 % o string_info: the string info.
1257 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1259 assert(string_info != (StringInfo *) NULL);
1260 assert(string_info->signature == MagickSignature);
1261 return(string_info->datum);
1265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269 % G e t S t r i n g I n f o L e n g t h %
1273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 % GetStringInfoLength() returns the string length.
1277 % The format of the GetStringInfoLength method is:
1279 % size_t GetStringInfoLength(const StringInfo *string_info)
1281 % A description of each parameter follows:
1283 % o string_info: the string info.
1286 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1288 assert(string_info != (StringInfo *) NULL);
1289 assert(string_info->signature == MagickSignature);
1290 return(string_info->length);
1294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298 % G e t S t r i n g I n f o P a t h %
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1304 % GetStringInfoPath() returns the path associated with the string.
1306 % The format of the GetStringInfoPath method is:
1308 % const char *GetStringInfoPath(const StringInfo *string_info)
1310 % A description of each parameter follows:
1312 % o string_info: the string info.
1315 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1317 assert(string_info != (StringInfo *) NULL);
1318 assert(string_info->signature == MagickSignature);
1319 return(string_info->path);
1323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327 % L o c a l e C o m p a r e %
1331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1333 % LocaleCompare() performs a case-insensitive comparison of two strings
1334 % byte-by-byte, according to the ordering of the current locale encoding.
1335 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1336 % if the string pointed to by p is greater than, equal to, or less than the
1337 % string pointed to by q respectively. The sign of a non-zero return value
1338 % is determined by the sign of the difference between the values of the first< % pair of bytes that differ in the strings being compared.
1340 % The format of the LocaleCompare method is:
1342 % int LocaleCompare(const char *p,const char *q)
1344 % A description of each parameter follows:
1346 % o p: A pointer to a character string.
1348 % o q: A pointer to a character string to compare to p.
1351 MagickExport int LocaleCompare(const char *p,const char *q)
1353 if ((p == (char *) NULL) && (q == (char *) NULL))
1355 if (p == (char *) NULL)
1357 if (q == (char *) NULL)
1359 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1360 return(strcasecmp(p,q));
1369 c=(int) *((unsigned char *) p);
1370 d=(int) *((unsigned char *) q);
1371 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1376 return(AsciiMap[c]-(int) AsciiMap[d]);
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 % L o c a l e L o w e r %
1390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392 % LocaleLower() transforms all of the characters in the supplied
1393 % null-terminated string, changing all uppercase letters to lowercase.
1395 % The format of the LocaleLower method is:
1397 % void LocaleLower(char *string)
1399 % A description of each parameter follows:
1401 % o string: A pointer to the string to convert to lower-case Locale.
1404 MagickExport void LocaleLower(char *string)
1409 assert(string != (char *) NULL);
1410 for (q=string; *q != '\0'; q++)
1411 *q=(char) tolower((int) *q);
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419 % L o c a l e N C o m p a r e %
1423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1425 % LocaleNCompare() performs a case-insensitive comparison of two
1426 % strings byte-by-byte, according to the ordering of the current locale
1427 % encoding. LocaleNCompare returns an integer greater than, equal to, or
1428 % less than 0, if the string pointed to by p is greater than, equal to, or
1429 % less than the string pointed to by q respectively. The sign of a non-zero
1430 % return value is determined by the sign of the difference between the
1431 % values of the first pair of bytes that differ in the strings being
1432 % compared. The LocaleNCompare method makes the same comparison as
1433 % LocaleCompare but looks at a maximum of n bytes. Bytes following a
1434 % null byte are not compared.
1436 % The format of the LocaleNCompare method is:
1438 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1440 % A description of each parameter follows:
1442 % o p: A pointer to a character string.
1444 % o q: A pointer to a character string to compare to p.
1446 % o length: the number of characters to compare in strings p & q.
1449 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1451 if (p == (char *) NULL)
1453 if (q == (char *) NULL)
1455 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1456 return(strncasecmp(p,q,length));
1466 for (i=length; i != 0; i--)
1468 c=(int) *((unsigned char *) p);
1469 d=(int) *((unsigned char *) q);
1470 if (AsciiMap[c] != AsciiMap[d])
1471 return(AsciiMap[c]-(int) AsciiMap[d]);
1483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1487 % L o c a l e U p p e r %
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493 % LocaleUpper() transforms all of the characters in the supplied
1494 % null-terminated string, changing all lowercase letters to uppercase.
1496 % The format of the LocaleUpper method is:
1498 % void LocaleUpper(char *string)
1500 % A description of each parameter follows:
1502 % o string: A pointer to the string to convert to upper-case Locale.
1505 MagickExport void LocaleUpper(char *string)
1510 assert(string != (char *) NULL);
1511 for (q=string; *q != '\0'; q++)
1512 *q=(char) toupper((int) *q);
1516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1520 % P r i n t S t r i n g I n f o %
1524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1526 % PrintStringInfo() prints the string.
1528 % The format of the PrintStringInfo method is:
1530 % void PrintStringInfo(FILE *file,const char *id,
1531 % const StringInfo *string_info)
1533 % A description of each parameter follows:
1535 % o file: the file, typically stdout.
1537 % o id: the string id.
1539 % o string_info: the string info.
1542 MagickExport void PrintStringInfo(FILE *file,const char *id,
1543 const StringInfo *string_info)
1552 assert(id != (const char *) NULL);
1553 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1554 assert(string_info != (StringInfo *) NULL);
1555 assert(string_info->signature == MagickSignature);
1556 p=(char *) string_info->datum;
1557 for (i=0; i < string_info->length; i++)
1559 if (((int) ((unsigned char) *p) < 32) &&
1560 (isspace((int) ((unsigned char) *p)) == 0))
1564 if (i == string_info->length)
1566 (void) fputs((char *) string_info->datum,file);
1567 (void) fputc('\n',file);
1571 Convert string to a HEX list.
1573 p=(char *) string_info->datum;
1574 for (i=0; i < string_info->length; i+=0x14)
1576 (void) fprintf(file,"0x%08lx: ",(unsigned long) (0x14*i));
1577 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1579 (void) fprintf(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1580 if ((j % 0x04) == 0)
1581 (void) fputc(' ',file);
1583 for ( ; j <= 0x14; j++)
1585 (void) fputc(' ',file);
1586 (void) fputc(' ',file);
1587 if ((j % 0x04) == 0)
1588 (void) fputc(' ',file);
1590 (void) fputc(' ',file);
1591 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1593 if (isprint((int) ((unsigned char) *p)) != 0)
1594 (void) fputc(*p,file);
1596 (void) fputc('-',file);
1599 (void) fputc('\n',file);
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 % R e s e t S t r i n g I n f o %
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1614 % ResetStringInfo() reset the string to all null bytes.
1616 % The format of the ResetStringInfo method is:
1618 % void ResetStringInfo(StringInfo *string_info)
1620 % A description of each parameter follows:
1622 % o string_info: the string info.
1625 MagickExport void ResetStringInfo(StringInfo *string_info)
1627 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1628 assert(string_info != (StringInfo *) NULL);
1629 assert(string_info->signature == MagickSignature);
1630 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638 % S e t S t r i n g I n f o %
1642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % SetStringInfo() copies the source string to the destination string.
1646 % The format of the SetStringInfo method is:
1648 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1650 % A description of each parameter follows:
1652 % o string_info: the string info.
1654 % o source: the source string.
1657 MagickExport void SetStringInfo(StringInfo *string_info,
1658 const StringInfo *source)
1660 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1661 assert(string_info != (StringInfo *) NULL);
1662 assert(string_info->signature == MagickSignature);
1663 assert(source != (StringInfo *) NULL);
1664 assert(source->signature == MagickSignature);
1665 if (string_info->length == 0)
1667 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1668 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1677 % S e t S t r i n g I n f o D a t u m %
1681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1683 % SetStringInfoDatum() copies bytes from the source string for the length of
1684 % the destination string.
1686 % The format of the SetStringInfoDatum method is:
1688 % void SetStringInfoDatum(StringInfo *string_info,
1689 % const unsigned char *source)
1691 % A description of each parameter follows:
1693 % o string_info: the string info.
1695 % o source: the source string.
1698 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1699 const unsigned char *source)
1701 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1702 assert(string_info != (StringInfo *) NULL);
1703 assert(string_info->signature == MagickSignature);
1704 if (string_info->length != 0)
1705 (void) memcpy(string_info->datum,source,string_info->length);
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713 % S e t S t r i n g I n f o L e n g t h %
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1719 % SetStringInfoLength() set the string length to the specified value.
1721 % The format of the SetStringInfoLength method is:
1723 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1725 % A description of each parameter follows:
1727 % o string_info: the string info.
1729 % o length: the string length.
1732 MagickExport void SetStringInfoLength(StringInfo *string_info,
1733 const size_t length)
1735 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1736 assert(string_info != (StringInfo *) NULL);
1737 assert(string_info->signature == MagickSignature);
1738 if (~length < MaxTextExtent)
1739 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1740 string_info->length=length;
1741 if (string_info->datum == (unsigned char *) NULL)
1742 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1743 MaxTextExtent,sizeof(*string_info->datum));
1745 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1746 length+MaxTextExtent,sizeof(*string_info->datum));
1747 if (string_info->datum == (unsigned char *) NULL)
1748 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1756 % S e t S t r i n g I n f o D a t u m %
1760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1762 % SetStringInfoPath() sets the path associated with the string.
1764 % The format of the SetStringInfoPath method is:
1766 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1768 % A description of each parameter follows:
1770 % o string_info: the string info.
1775 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1778 assert(string_info != (StringInfo *) NULL);
1779 assert(string_info->signature == MagickSignature);
1780 assert(path != (const char *) NULL);
1781 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789 % S p l i t S t r i n g I n f o %
1793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795 % SplitStringInfo() splits a string into two and returns it.
1797 % The format of the SplitStringInfo method is:
1799 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1801 % A description of each parameter follows:
1803 % o string_info: the string info.
1806 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1807 const size_t offset)
1812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1813 assert(string_info != (StringInfo *) NULL);
1814 assert(string_info->signature == MagickSignature);
1815 if (offset > string_info->length)
1816 return((StringInfo *) NULL);
1817 split_info=AcquireStringInfo(offset);
1818 SetStringInfo(split_info,string_info);
1819 (void) memcpy(string_info->datum,string_info->datum+offset,
1820 string_info->length-offset+MaxTextExtent);
1821 SetStringInfoLength(string_info,string_info->length-offset);
1826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 % S t r i n g I n f o T o S t r i n g %
1834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836 % StringInfoToString() converts a string info string to a C string.
1838 % The format of the StringInfoToString method is:
1840 % char *StringInfoToString(const StringInfo *string_info)
1842 % A description of each parameter follows:
1844 % o string_info: the string.
1847 MagickExport char *StringInfoToString(const StringInfo *string_info)
1855 string=(char *) NULL;
1856 length=string_info->length;
1857 if (~length >= MaxTextExtent)
1858 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1859 if (string == (char *) NULL)
1860 return((char *) NULL);
1861 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1862 string[length]='\0';
1867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871 % S t r i n g T o A r g v %
1875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1877 % StringToArgv() converts a text string into command line arguments.
1879 % The format of the StringToArgv method is:
1881 % char **StringToArgv(const char *text,int *argc)
1883 % A description of each parameter follows:
1885 % o argv: Method StringToArgv returns the string list unless an error
1886 % occurs, otherwise NULL.
1888 % o text: Specifies the string to segment into a list.
1890 % o argc: This integer pointer returns the number of arguments in the
1894 MagickExport char **StringToArgv(const char *text,int *argc)
1907 if (text == (char *) NULL)
1908 return((char **) NULL);
1910 Determine the number of arguments.
1912 for (p=text; *p != '\0'; )
1914 while (isspace((int) ((unsigned char) *p)) != 0)
1918 for (p++; (*p != '"') && (*p != '\0'); p++) ;
1920 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
1921 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1925 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
1926 if (argv == (char **) NULL)
1927 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
1929 Convert string to an ASCII list.
1931 argv[0]=AcquireString("magick");
1933 for (i=1; i < (ssize_t) *argc; i++)
1935 while (isspace((int) ((unsigned char) *p)) != 0)
1941 for (q++; (*q != '"') && (*q != '\0'); q++) ;
1946 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
1950 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
1952 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1954 if (argv[i] == (char *) NULL)
1956 for (i--; i >= 0; i--)
1957 argv[i]=DestroyString(argv[i]);
1958 argv=(char **) RelinquishMagickMemory(argv);
1959 ThrowFatalException(ResourceLimitFatalError,
1960 "UnableToConvertStringToARGV");
1962 (void) memcpy(argv[i],p,(size_t) (q-p));
1965 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1968 argv[i]=(char *) NULL;
1973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1977 % S t r i n g I n f o T o H e x S t r i n g %
1981 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1983 % StringInfoToHexString() converts a string info string to a C string.
1985 % The format of the StringInfoToHexString method is:
1987 % char *StringInfoToHexString(const StringInfo *string_info)
1989 % A description of each parameter follows:
1991 % o string_info: the string.
1994 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1999 register const unsigned char
2005 register unsigned char
2014 length=string_info->length;
2015 if (~length < MaxTextExtent)
2016 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2017 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2018 if (string == (char *) NULL)
2019 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2036 p=string_info->datum;
2037 q=(unsigned char *) string;
2038 for (i=0; i < (ssize_t) string_info->length; i++)
2040 *q++=hex_digits[(*p >> 4) & 0x0f];
2041 *q++=hex_digits[*p & 0x0f];
2049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2053 % S t r i n g T o k e n %
2057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2059 % StringToken() extracts a token a from the string.
2061 % The format of the StringToken method is:
2063 % char *StringToken(const char *delimiters,char **string)
2065 % A description of each parameter follows:
2067 % o delimiters: one or more delimiters.
2069 % o string: return the first token in the string. If none is found, return
2073 MagickExport char *StringToken(const char *delimiters,char **string)
2089 if (p == (char *) NULL)
2090 return((char *) NULL);
2107 } while (d != '\0');
2112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2116 % S t r i n g T o L i s t %
2120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2122 % StringToList() converts a text string into a list by segmenting the text
2123 % string at each carriage return discovered. The list is converted to HEX
2124 % characters if any control characters are discovered within the text string.
2126 % The format of the StringToList method is:
2128 % char **StringToList(const char *text)
2130 % A description of each parameter follows:
2132 % o text: Specifies the string to segment into a list.
2135 MagickExport char **StringToList(const char *text)
2149 if (text == (char *) NULL)
2150 return((char **) NULL);
2151 for (p=text; *p != '\0'; p++)
2152 if (((int) ((unsigned char) *p) < 32) &&
2153 (isspace((int) ((unsigned char) *p)) == 0))
2161 Convert string to an ASCII list.
2164 for (p=text; *p != '\0'; p++)
2167 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2169 if (textlist == (char **) NULL)
2170 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2172 for (i=0; i < (ssize_t) lines; i++)
2174 for (q=p; *q != '\0'; q++)
2175 if ((*q == '\r') || (*q == '\n'))
2177 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2179 if (textlist[i] == (char *) NULL)
2180 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2181 (void) memcpy(textlist[i],p,(size_t) (q-p));
2182 textlist[i][q-p]='\0';
2191 hex_string[MaxTextExtent];
2200 Convert string to a HEX list.
2202 lines=(size_t) (strlen(text)/0x14)+1;
2203 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2205 if (textlist == (char **) NULL)
2206 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2208 for (i=0; i < (ssize_t) lines; i++)
2210 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2212 if (textlist[i] == (char *) NULL)
2213 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2214 (void) FormatMagickString(textlist[i],MaxTextExtent,"0x%08lx: ",
2216 q=textlist[i]+strlen(textlist[i]);
2217 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2219 (void) FormatMagickString(hex_string,MaxTextExtent,"%02x",*(p+j));
2220 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2222 if ((j % 0x04) == 0)
2225 for ( ; j <= 0x14; j++)
2229 if ((j % 0x04) == 0)
2233 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2235 if (isprint((int) ((unsigned char) *p)) != 0)
2244 textlist[i]=(char *) NULL;
2249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2253 % S t r i n g T o S t r i n g I n f o %
2257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2259 % StringToStringInfo() returns the contents of a file as a string.
2261 % The format of the StringToStringInfo method is:
2263 % StringInfo *StringToStringInfo(const char *string)
2265 % A description of each parameter follows:
2267 % o string: The string.
2270 MagickExport StringInfo *StringToStringInfo(const char *string)
2275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2276 assert(string != (const char *) NULL);
2277 string_info=AcquireStringInfo(strlen(string)+1);
2278 SetStringInfoDatum(string_info,(const unsigned char *) string);
2279 return(string_info);
2283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2287 % S t r i p S t r i n g %
2291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293 % StripString() strips any whitespace or quotes from the beginning and end of
2294 % a string of characters.
2296 % The format of the StripString method is:
2298 % void StripString(char *message)
2300 % A description of each parameter follows:
2302 % o message: Specifies an array of characters.
2305 MagickExport void StripString(char *message)
2314 assert(message != (char *) NULL);
2315 if (*message == '\0')
2317 length=strlen(message);
2319 while (isspace((int) ((unsigned char) *p)) != 0)
2321 if ((*p == '\'') || (*p == '"'))
2324 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2327 if ((*q == '\'') || (*q == '"'))
2329 (void) memcpy(message,p,(size_t) (q-p+1));
2330 message[q-p+1]='\0';
2331 for (p=message; *p != '\0'; p++)
2337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2341 % S u b s t i t u t e S t r i n g %
2345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2347 % SubstituteString() performs string substitution on a string, replacing the
2348 % string with the substituted version. Buffer must be allocated from the heap.
2349 % If the string is matched and status, MagickTrue is returned otherwise
2352 % The format of the SubstituteString method is:
2354 % MagickBooleanType SubstituteString(char **string,const char *search,
2355 % const char *replace)
2357 % A description of each parameter follows:
2359 % o string: the string to perform replacements on; replaced with new
2360 % allocation if a replacement is made.
2362 % o search: search for this string.
2364 % o replace: replace any matches with this string.
2367 MagickExport MagickBooleanType SubstituteString(char **string,
2368 const char *search,const char *replace)
2387 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2389 if (search_extent == 0)
2390 search_extent=strlen(search);
2391 if (strncmp(p,search,search_extent) != 0)
2397 if (replace_extent == 0)
2398 replace_extent=strlen(replace);
2399 if (replace_extent > search_extent)
2402 Make room for the replacement string.
2404 offset=(ssize_t) (p-(*string));
2405 extent=strlen(*string)+replace_extent-search_extent+1;
2406 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2408 if (*string == (char *) NULL)
2409 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2415 if (search_extent != replace_extent)
2416 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2417 strlen(p+search_extent)+1);
2418 (void) CopyMagickMemory(p,replace,replace_extent);
2419 p+=replace_extent-1;