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-2011 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) lseek(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) lseek(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
1339 % pair of bytes that differ in the strings being compared.
1341 % The format of the LocaleCompare method is:
1343 % int LocaleCompare(const char *p,const char *q)
1345 % A description of each parameter follows:
1347 % o p: A pointer to a character string.
1349 % o q: A pointer to a character string to compare to p.
1352 MagickExport int LocaleCompare(const char *p,const char *q)
1354 if ((p == (char *) NULL) && (q == (char *) NULL))
1356 if (p == (char *) NULL)
1358 if (q == (char *) NULL)
1360 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1361 return(strcasecmp(p,q));
1370 c=(int) *((unsigned char *) p);
1371 d=(int) *((unsigned char *) q);
1372 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1377 return(AsciiMap[c]-(int) AsciiMap[d]);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % L o c a l e L o w e r %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % LocaleLower() transforms all of the characters in the supplied
1394 % null-terminated string, changing all uppercase letters to lowercase.
1396 % The format of the LocaleLower method is:
1398 % void LocaleLower(char *string)
1400 % A description of each parameter follows:
1402 % o string: A pointer to the string to convert to lower-case Locale.
1405 MagickExport void LocaleLower(char *string)
1410 assert(string != (char *) NULL);
1411 for (q=string; *q != '\0'; q++)
1412 *q=(char) tolower((int) *q);
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 % L o c a l e N C o m p a r e %
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 % LocaleNCompare() performs a case-insensitive comparison of two
1427 % strings byte-by-byte, according to the ordering of the current locale
1428 % encoding. LocaleNCompare returns an integer greater than, equal to, or
1429 % less than 0, if the string pointed to by p is greater than, equal to, or
1430 % less than the string pointed to by q respectively. The sign of a non-zero
1431 % return value is determined by the sign of the difference between the
1432 % values of the first pair of bytes that differ in the strings being
1433 % compared. The LocaleNCompare method makes the same comparison as
1434 % LocaleCompare but looks at a maximum of n bytes. Bytes following a
1435 % null byte are not compared.
1437 % The format of the LocaleNCompare method is:
1439 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1441 % A description of each parameter follows:
1443 % o p: A pointer to a character string.
1445 % o q: A pointer to a character string to compare to p.
1447 % o length: the number of characters to compare in strings p and q.
1450 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1452 if (p == (char *) NULL)
1454 if (q == (char *) NULL)
1456 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1457 return(strncasecmp(p,q,length));
1467 for (i=length; i != 0; i--)
1469 c=(int) *((unsigned char *) p);
1470 d=(int) *((unsigned char *) q);
1471 if (AsciiMap[c] != AsciiMap[d])
1472 return(AsciiMap[c]-(int) AsciiMap[d]);
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488 % L o c a l e U p p e r %
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1494 % LocaleUpper() transforms all of the characters in the supplied
1495 % null-terminated string, changing all lowercase letters to uppercase.
1497 % The format of the LocaleUpper method is:
1499 % void LocaleUpper(char *string)
1501 % A description of each parameter follows:
1503 % o string: A pointer to the string to convert to upper-case Locale.
1506 MagickExport void LocaleUpper(char *string)
1511 assert(string != (char *) NULL);
1512 for (q=string; *q != '\0'; q++)
1513 *q=(char) toupper((int) *q);
1517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1521 % P r i n t S t r i n g I n f o %
1525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527 % PrintStringInfo() prints the string.
1529 % The format of the PrintStringInfo method is:
1531 % void PrintStringInfo(FILE *file,const char *id,
1532 % const StringInfo *string_info)
1534 % A description of each parameter follows:
1536 % o file: the file, typically stdout.
1538 % o id: the string id.
1540 % o string_info: the string info.
1543 MagickExport void PrintStringInfo(FILE *file,const char *id,
1544 const StringInfo *string_info)
1553 assert(id != (const char *) NULL);
1554 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1555 assert(string_info != (StringInfo *) NULL);
1556 assert(string_info->signature == MagickSignature);
1557 p=(char *) string_info->datum;
1558 for (i=0; i < string_info->length; i++)
1560 if (((int) ((unsigned char) *p) < 32) &&
1561 (isspace((int) ((unsigned char) *p)) == 0))
1565 if (i == string_info->length)
1567 (void) fputs((char *) string_info->datum,file);
1568 (void) fputc('\n',file);
1572 Convert string to a HEX list.
1574 p=(char *) string_info->datum;
1575 for (i=0; i < string_info->length; i+=0x14)
1577 (void) fprintf(file,"0x%08lx: ",(unsigned long) (0x14*i));
1578 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1580 (void) fprintf(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1581 if ((j % 0x04) == 0)
1582 (void) fputc(' ',file);
1584 for ( ; j <= 0x14; j++)
1586 (void) fputc(' ',file);
1587 (void) fputc(' ',file);
1588 if ((j % 0x04) == 0)
1589 (void) fputc(' ',file);
1591 (void) fputc(' ',file);
1592 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1594 if (isprint((int) ((unsigned char) *p)) != 0)
1595 (void) fputc(*p,file);
1597 (void) fputc('-',file);
1600 (void) fputc('\n',file);
1605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609 % R e s e t S t r i n g I n f o %
1613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615 % ResetStringInfo() reset the string to all null bytes.
1617 % The format of the ResetStringInfo method is:
1619 % void ResetStringInfo(StringInfo *string_info)
1621 % A description of each parameter follows:
1623 % o string_info: the string info.
1626 MagickExport void ResetStringInfo(StringInfo *string_info)
1628 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1629 assert(string_info != (StringInfo *) NULL);
1630 assert(string_info->signature == MagickSignature);
1631 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639 % S e t S t r i n g I n f o %
1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645 % SetStringInfo() copies the source string to the destination string.
1647 % The format of the SetStringInfo method is:
1649 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1651 % A description of each parameter follows:
1653 % o string_info: the string info.
1655 % o source: the source string.
1658 MagickExport void SetStringInfo(StringInfo *string_info,
1659 const StringInfo *source)
1661 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1662 assert(string_info != (StringInfo *) NULL);
1663 assert(string_info->signature == MagickSignature);
1664 assert(source != (StringInfo *) NULL);
1665 assert(source->signature == MagickSignature);
1666 if (string_info->length == 0)
1668 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1669 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678 % S e t S t r i n g I n f o D a t u m %
1682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1684 % SetStringInfoDatum() copies bytes from the source string for the length of
1685 % the destination string.
1687 % The format of the SetStringInfoDatum method is:
1689 % void SetStringInfoDatum(StringInfo *string_info,
1690 % const unsigned char *source)
1692 % A description of each parameter follows:
1694 % o string_info: the string info.
1696 % o source: the source string.
1699 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1700 const unsigned char *source)
1702 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1703 assert(string_info != (StringInfo *) NULL);
1704 assert(string_info->signature == MagickSignature);
1705 if (string_info->length != 0)
1706 (void) memcpy(string_info->datum,source,string_info->length);
1710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714 % S e t S t r i n g I n f o L e n g t h %
1718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1720 % SetStringInfoLength() set the string length to the specified value.
1722 % The format of the SetStringInfoLength method is:
1724 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1726 % A description of each parameter follows:
1728 % o string_info: the string info.
1730 % o length: the string length.
1733 MagickExport void SetStringInfoLength(StringInfo *string_info,
1734 const size_t length)
1736 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1737 assert(string_info != (StringInfo *) NULL);
1738 assert(string_info->signature == MagickSignature);
1739 if (~length < MaxTextExtent)
1740 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1741 string_info->length=length;
1742 if (string_info->datum == (unsigned char *) NULL)
1743 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1744 MaxTextExtent,sizeof(*string_info->datum));
1746 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1747 length+MaxTextExtent,sizeof(*string_info->datum));
1748 if (string_info->datum == (unsigned char *) NULL)
1749 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 % S e t S t r i n g I n f o D a t u m %
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763 % SetStringInfoPath() sets the path associated with the string.
1765 % The format of the SetStringInfoPath method is:
1767 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1769 % A description of each parameter follows:
1771 % o string_info: the string info.
1776 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1779 assert(string_info != (StringInfo *) NULL);
1780 assert(string_info->signature == MagickSignature);
1781 assert(path != (const char *) NULL);
1782 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790 % S p l i t S t r i n g I n f o %
1794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1796 % SplitStringInfo() splits a string into two and returns it.
1798 % The format of the SplitStringInfo method is:
1800 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1802 % A description of each parameter follows:
1804 % o string_info: the string info.
1807 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1808 const size_t offset)
1813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1814 assert(string_info != (StringInfo *) NULL);
1815 assert(string_info->signature == MagickSignature);
1816 if (offset > string_info->length)
1817 return((StringInfo *) NULL);
1818 split_info=AcquireStringInfo(offset);
1819 SetStringInfo(split_info,string_info);
1820 (void) memcpy(string_info->datum,string_info->datum+offset,
1821 string_info->length-offset+MaxTextExtent);
1822 SetStringInfoLength(string_info,string_info->length-offset);
1827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1831 % S t r i n g I n f o T o S t r i n g %
1835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837 % StringInfoToString() converts a string info string to a C string.
1839 % The format of the StringInfoToString method is:
1841 % char *StringInfoToString(const StringInfo *string_info)
1843 % A description of each parameter follows:
1845 % o string_info: the string.
1848 MagickExport char *StringInfoToString(const StringInfo *string_info)
1856 string=(char *) NULL;
1857 length=string_info->length;
1858 if (~length >= MaxTextExtent)
1859 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1860 if (string == (char *) NULL)
1861 return((char *) NULL);
1862 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1863 string[length]='\0';
1868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872 % S t r i n g T o A r g v %
1876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1878 % StringToArgv() converts a text string into command line arguments.
1880 % The format of the StringToArgv method is:
1882 % char **StringToArgv(const char *text,int *argc)
1884 % A description of each parameter follows:
1886 % o argv: Method StringToArgv returns the string list unless an error
1887 % occurs, otherwise NULL.
1889 % o text: Specifies the string to segment into a list.
1891 % o argc: This integer pointer returns the number of arguments in the
1895 MagickExport char **StringToArgv(const char *text,int *argc)
1908 if (text == (char *) NULL)
1909 return((char **) NULL);
1911 Determine the number of arguments.
1913 for (p=text; *p != '\0'; )
1915 while (isspace((int) ((unsigned char) *p)) != 0)
1921 for (p++; (*p != '"') && (*p != '\0'); p++) ;
1923 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
1924 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1928 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
1929 if (argv == (char **) NULL)
1930 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
1932 Convert string to an ASCII list.
1934 argv[0]=AcquireString("magick");
1936 for (i=1; i < (ssize_t) *argc; i++)
1938 while (isspace((int) ((unsigned char) *p)) != 0)
1944 for (q++; (*q != '"') && (*q != '\0'); q++) ;
1950 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
1953 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
1955 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1957 if (argv[i] == (char *) NULL)
1959 for (i--; i >= 0; i--)
1960 argv[i]=DestroyString(argv[i]);
1961 argv=(char **) RelinquishMagickMemory(argv);
1962 ThrowFatalException(ResourceLimitFatalError,
1963 "UnableToConvertStringToARGV");
1965 (void) memcpy(argv[i],p,(size_t) (q-p));
1968 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1971 argv[i]=(char *) NULL;
1976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1980 % S t r i n g I n f o T o H e x S t r i n g %
1984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1986 % StringInfoToHexString() converts a string info string to a C string.
1988 % The format of the StringInfoToHexString method is:
1990 % char *StringInfoToHexString(const StringInfo *string_info)
1992 % A description of each parameter follows:
1994 % o string_info: the string.
1997 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
2002 register const unsigned char
2008 register unsigned char
2017 length=string_info->length;
2018 if (~length < MaxTextExtent)
2019 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2020 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2021 if (string == (char *) NULL)
2022 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2039 p=string_info->datum;
2040 q=(unsigned char *) string;
2041 for (i=0; i < (ssize_t) string_info->length; i++)
2043 *q++=hex_digits[(*p >> 4) & 0x0f];
2044 *q++=hex_digits[*p & 0x0f];
2052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2056 % S t r i n g T o k e n %
2060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2062 % StringToken() extracts a token a from the string.
2064 % The format of the StringToken method is:
2066 % char *StringToken(const char *delimiters,char **string)
2068 % A description of each parameter follows:
2070 % o delimiters: one or more delimiters.
2072 % o string: return the first token in the string. If none is found, return
2076 MagickExport char *StringToken(const char *delimiters,char **string)
2092 if (p == (char *) NULL)
2093 return((char *) NULL);
2110 } while (d != '\0');
2115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119 % S t r i n g T o L i s t %
2123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2125 % StringToList() converts a text string into a list by segmenting the text
2126 % string at each carriage return discovered. The list is converted to HEX
2127 % characters if any control characters are discovered within the text string.
2129 % The format of the StringToList method is:
2131 % char **StringToList(const char *text)
2133 % A description of each parameter follows:
2135 % o text: Specifies the string to segment into a list.
2138 MagickExport char **StringToList(const char *text)
2152 if (text == (char *) NULL)
2153 return((char **) NULL);
2154 for (p=text; *p != '\0'; p++)
2155 if (((int) ((unsigned char) *p) < 32) &&
2156 (isspace((int) ((unsigned char) *p)) == 0))
2164 Convert string to an ASCII list.
2167 for (p=text; *p != '\0'; p++)
2170 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2172 if (textlist == (char **) NULL)
2173 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2175 for (i=0; i < (ssize_t) lines; i++)
2177 for (q=p; *q != '\0'; q++)
2178 if ((*q == '\r') || (*q == '\n'))
2180 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2182 if (textlist[i] == (char *) NULL)
2183 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2184 (void) memcpy(textlist[i],p,(size_t) (q-p));
2185 textlist[i][q-p]='\0';
2194 hex_string[MaxTextExtent];
2203 Convert string to a HEX list.
2205 lines=(size_t) (strlen(text)/0x14)+1;
2206 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2208 if (textlist == (char **) NULL)
2209 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2211 for (i=0; i < (ssize_t) lines; i++)
2213 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2215 if (textlist[i] == (char *) NULL)
2216 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2217 (void) FormatMagickString(textlist[i],MaxTextExtent,"0x%08lx: ",
2219 q=textlist[i]+strlen(textlist[i]);
2220 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2222 (void) FormatMagickString(hex_string,MaxTextExtent,"%02x",*(p+j));
2223 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2225 if ((j % 0x04) == 0)
2228 for ( ; j <= 0x14; j++)
2232 if ((j % 0x04) == 0)
2236 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2238 if (isprint((int) ((unsigned char) *p)) != 0)
2247 textlist[i]=(char *) NULL;
2252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2256 % S t r i n g T o S t r i n g I n f o %
2260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262 % StringToStringInfo() returns the contents of a file as a string.
2264 % The format of the StringToStringInfo method is:
2266 % StringInfo *StringToStringInfo(const char *string)
2268 % A description of each parameter follows:
2270 % o string: The string.
2273 MagickExport StringInfo *StringToStringInfo(const char *string)
2278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2279 assert(string != (const char *) NULL);
2280 string_info=AcquireStringInfo(strlen(string)+1);
2281 SetStringInfoDatum(string_info,(const unsigned char *) string);
2282 return(string_info);
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2290 % S t r i p S t r i n g %
2294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2296 % StripString() strips any whitespace or quotes from the beginning and end of
2297 % a string of characters.
2299 % The format of the StripString method is:
2301 % void StripString(char *message)
2303 % A description of each parameter follows:
2305 % o message: Specifies an array of characters.
2308 MagickExport void StripString(char *message)
2317 assert(message != (char *) NULL);
2318 if (*message == '\0')
2320 length=strlen(message);
2322 while (isspace((int) ((unsigned char) *p)) != 0)
2324 if ((*p == '\'') || (*p == '"'))
2327 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2330 if ((*q == '\'') || (*q == '"'))
2332 (void) memcpy(message,p,(size_t) (q-p+1));
2333 message[q-p+1]='\0';
2334 for (p=message; *p != '\0'; p++)
2340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2344 % S u b s t i t u t e S t r i n g %
2348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2350 % SubstituteString() performs string substitution on a string, replacing the
2351 % string with the substituted version. Buffer must be allocated from the heap.
2352 % If the string is matched and status, MagickTrue is returned otherwise
2355 % The format of the SubstituteString method is:
2357 % MagickBooleanType SubstituteString(char **string,const char *search,
2358 % const char *replace)
2360 % A description of each parameter follows:
2362 % o string: the string to perform replacements on; replaced with new
2363 % allocation if a replacement is made.
2365 % o search: search for this string.
2367 % o replace: replace any matches with this string.
2370 MagickExport MagickBooleanType SubstituteString(char **string,
2371 const char *search,const char *replace)
2390 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2392 if (search_extent == 0)
2393 search_extent=strlen(search);
2394 if (strncmp(p,search,search_extent) != 0)
2400 if (replace_extent == 0)
2401 replace_extent=strlen(replace);
2402 if (replace_extent > search_extent)
2405 Make room for the replacement string.
2407 offset=(ssize_t) (p-(*string));
2408 extent=strlen(*string)+replace_extent-search_extent+1;
2409 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2411 if (*string == (char *) NULL)
2412 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2418 if (search_extent != replace_extent)
2419 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2420 strlen(p+search_extent)+1);
2421 (void) CopyMagickMemory(p,replace,replace_extent);
2422 p+=replace_extent-1;