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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/list.h"
48 #include "MagickCore/locale_.h"
49 #include "MagickCore/log.h"
50 #include "MagickCore/memory_.h"
51 #include "MagickCore/property.h"
52 #include "MagickCore/resource_.h"
53 #include "MagickCore/signature-private.h"
54 #include "MagickCore/string_.h"
59 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
60 static const unsigned char
63 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
64 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
65 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
66 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
67 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
68 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
69 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
70 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
71 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
72 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
73 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
74 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
75 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
76 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
77 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
78 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
79 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
80 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
81 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
82 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
83 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
84 0xfc, 0xfd, 0xfe, 0xff,
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % A c q u i r e S t r i n g %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % AcquireString() allocates memory for a string and copies the source string
100 % to that memory location (and returns it).
102 % The format of the AcquireString method is:
104 % char *AcquireString(const char *source)
106 % A description of each parameter follows:
108 % o source: A character string.
111 MagickExport char *AcquireString(const char *source)
120 if (source != (char *) NULL)
121 length+=strlen(source);
122 if (~length < MaxTextExtent)
123 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
124 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
125 sizeof(*destination));
126 if (destination == (char *) NULL)
127 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
129 if (source != (char *) NULL)
130 (void) memcpy(destination,source,length*sizeof(*destination));
131 destination[length]='\0';
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 % A c q u i r e S t r i n g I n f o %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % AcquireStringInfo() allocates the StringInfo structure.
148 % The format of the AcquireStringInfo method is:
150 % StringInfo *AcquireStringInfo(const size_t length)
152 % A description of each parameter follows:
154 % o length: the string length.
157 MagickExport StringInfo *AcquireStringInfo(const size_t length)
162 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
163 if (string_info == (StringInfo *) NULL)
164 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
165 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
166 string_info->signature=MagickSignature;
167 string_info->length=length;
168 if (string_info->length != 0)
170 string_info->datum=(unsigned char *) NULL;
171 if (~string_info->length >= (MaxTextExtent-1))
172 string_info->datum=(unsigned char *) AcquireQuantumMemory(
173 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
174 if (string_info->datum == (unsigned char *) NULL)
175 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % C l o n e S t r i n g %
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % CloneString() allocates memory for the destination string and copies
192 % the source string to that memory location.
194 % The format of the CloneString method is:
196 % char *CloneString(char **destination,const char *source)
198 % A description of each parameter follows:
200 % o destination: A pointer to a character string.
202 % o source: A character string.
205 MagickExport char *CloneString(char **destination,const char *source)
210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
211 assert(destination != (char **) NULL);
212 if (source == (const char *) NULL)
214 if (*destination != (char *) NULL)
215 *destination=DestroyString(*destination);
216 return(*destination);
218 if (*destination == (char *) NULL)
220 *destination=AcquireString(source);
221 return(*destination);
223 length=strlen(source);
224 if (~length < MaxTextExtent)
225 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
226 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
227 sizeof(**destination));
228 if (*destination == (char *) NULL)
229 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
231 (void) memcpy(*destination,source,length*sizeof(**destination));
232 (*destination)[length]='\0';
233 return(*destination);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % C l o n e S t r i n g I n f o %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % CloneStringInfo() clones a copy of the StringInfo structure.
249 % The format of the CloneStringInfo method is:
251 % StringInfo *CloneStringInfo(const StringInfo *string_info)
253 % A description of each parameter follows:
255 % o string_info: the string info.
258 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
264 assert(string_info != (StringInfo *) NULL);
265 assert(string_info->signature == MagickSignature);
266 clone_info=AcquireStringInfo(string_info->length);
267 if (string_info->length != 0)
268 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % C o m p a r e S t r i n g I n f o %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % CompareStringInfo() compares the two datums target and source. It returns
284 % an integer less than, equal to, or greater than zero if target is found,
285 % respectively, to be less than, to match, or be greater than source.
287 % The format of the CompareStringInfo method is:
289 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
291 % A description of each parameter follows:
293 % o target: the target string.
295 % o source: the source string.
299 static inline size_t MagickMin(const size_t x,const size_t y)
306 MagickExport int CompareStringInfo(const StringInfo *target,
307 const StringInfo *source)
312 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
313 assert(target != (StringInfo *) NULL);
314 assert(target->signature == MagickSignature);
315 assert(source != (StringInfo *) NULL);
316 assert(source->signature == MagickSignature);
317 status=memcmp(target->datum,source->datum,MagickMin(target->length,
321 if (target->length == source->length)
323 return(target->length < source->length ? -1 : 1);
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 % C o n c a t e n a t e M a g i c k S t r i n g %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % ConcatenateMagickString() concatenates the source string to the destination
338 % string. The destination buffer is always null-terminated even if the
339 % string must be truncated.
341 % The format of the ConcatenateMagickString method is:
343 % size_t ConcatenateMagickString(char *destination,const char *source,
344 % const size_t length)
346 % A description of each parameter follows:
348 % o destination: the destination string.
350 % o source: the source string.
352 % o length: the length of the destination string.
355 MagickExport size_t ConcatenateMagickString(char *destination,
356 const char *source,const size_t length)
370 assert(destination != (char *) NULL);
371 assert(source != (const char *) NULL);
376 while ((i-- != 0) && (*q != '\0'))
378 count=(size_t) (q-destination);
381 return(count+strlen(p));
392 return(count+(p-source));
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % C o n c a t e n a t e S t r i n g %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % ConcatenateString() appends a copy of string source, including the
407 % terminating null character, to the end of string destination.
409 % The format of the ConcatenateString method is:
411 % MagickBooleanType ConcatenateString(char **destination,
412 % const char *source)
414 % A description of each parameter follows:
416 % o destination: A pointer to a character string.
418 % o source: A character string.
421 MagickExport MagickBooleanType ConcatenateString(char **destination,
429 assert(destination != (char **) NULL);
430 if (source == (const char *) NULL)
432 if (*destination == (char *) NULL)
434 *destination=AcquireString(source);
437 destination_length=strlen(*destination);
438 source_length=strlen(source);
439 length=destination_length;
440 if (~length < source_length)
441 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
442 length+=source_length;
443 if (~length < MaxTextExtent)
444 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
445 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
446 sizeof(**destination));
447 if (*destination == (char *) NULL)
448 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
449 if (source_length != 0)
450 (void) memcpy((*destination)+destination_length,source,source_length);
451 (*destination)[length]='\0';
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % C o n c a t e n a t e S t r i n g I n f o %
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % ConcatenateStringInfo() concatenates the source string to the destination
469 % The format of the ConcatenateStringInfo method is:
471 % void ConcatenateStringInfo(StringInfo *string_info,
472 % const StringInfo *source)
474 % A description of each parameter follows:
476 % o string_info: the string info.
478 % o source: the source string.
481 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
482 const StringInfo *source)
487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
488 assert(string_info != (StringInfo *) NULL);
489 assert(string_info->signature == MagickSignature);
490 assert(source != (const StringInfo *) NULL);
491 length=string_info->length;
492 if (~length < source->length)
493 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
494 SetStringInfoLength(string_info,length+source->length);
495 (void) memcpy(string_info->datum+length,source->datum,source->length);
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 % 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 %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 % ConfigureFileToStringInfo() returns the contents of a configure file as a
512 % The format of the ConfigureFileToStringInfo method is:
514 % StringInfo *ConfigureFileToStringInfo(const char *filename)
515 % ExceptionInfo *exception)
517 % A description of each parameter follows:
519 % o filename: the filename.
522 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
542 assert(filename != (const char *) NULL);
543 file=open(filename,O_RDONLY | O_BINARY);
545 return((StringInfo *) NULL);
546 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
547 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
550 return((StringInfo *) NULL);
552 length=(size_t) offset;
553 string=(char *) NULL;
554 if (~length >= (MaxTextExtent-1))
555 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
556 if (string == (char *) NULL)
559 return((StringInfo *) NULL);
561 map=MapBlob(file,ReadMode,0,length);
562 if (map != (void *) NULL)
564 (void) memcpy(string,map,length);
565 (void) UnmapBlob(map,length);
575 (void) lseek(file,0,SEEK_SET);
576 for (i=0; i < length; i+=count)
578 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
590 string=DestroyString(string);
591 return((StringInfo *) NULL);
596 string_info=AcquireStringInfo(0);
597 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
598 string_info->length=length;
599 string_info->datum=(unsigned char *) string;
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608 % C o n s t a n t S t r i n g %
612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % ConstantString() allocates memory for a string and copies the source string
615 % to that memory location (and returns it). Use it for strings that you do
616 % do not expect to change over its lifetime.
618 % The format of the ConstantString method is:
620 % char *ConstantString(const char *source)
622 % A description of each parameter follows:
624 % o source: A character string.
627 MagickExport char *ConstantString(const char *source)
636 if (source != (char *) NULL)
637 length+=strlen(source);
638 destination=(char *) NULL;
640 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
641 if (destination == (char *) NULL)
642 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
644 if (source != (char *) NULL)
645 (void) memcpy(destination,source,length*sizeof(*destination));
646 destination[length]='\0';
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % C o p y M a g i c k S t r i n g %
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661 % CopyMagickString() copies the source string to the destination string. The
662 % destination buffer is always null-terminated even if the string must be
663 % truncated. The return value is the minimum of the source string length
664 % or the length parameter.
666 % The format of the CopyMagickString method is:
668 % size_t CopyMagickString(const char *destination,char *source,
669 % const size_t length)
671 % A description of each parameter follows:
673 % o destination: the destination string.
675 % o source: the source string.
677 % o length: the length of the destination string.
680 MagickExport size_t CopyMagickString(char *destination,const char *source,
694 for (n=length; n > 4; n-=4)
698 return((size_t) (p-source-1));
702 return((size_t) (p-source-1));
706 return((size_t) (p-source-1));
710 return((size_t) (p-source-1));
714 for (n--; n != 0; n--)
718 return((size_t) (p-source-1));
723 return((size_t) (p-source-1));
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % D e s t r o y S t r i n g %
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % DestroyString() destroys memory associated with a string.
739 % The format of the DestroyString method is:
741 % char *DestroyString(char *string)
743 % A description of each parameter follows:
745 % o string: the string.
748 MagickExport char *DestroyString(char *string)
750 return((char *) RelinquishMagickMemory(string));
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % D e s t r o y S t r i n g I n f o %
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
766 % The format of the DestroyStringInfo method is:
768 % StringInfo *DestroyStringInfo(StringInfo *string_info)
770 % A description of each parameter follows:
772 % o string_info: the string info.
775 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
778 assert(string_info != (StringInfo *) NULL);
779 assert(string_info->signature == MagickSignature);
780 if (string_info->datum != (unsigned char *) NULL)
781 string_info->datum=(unsigned char *) RelinquishMagickMemory(
783 string_info->signature=(~MagickSignature);
784 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % D e s t r o y S t r i n g L i s t %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % DestroyStringList() zeros memory associated with a string list.
801 % The format of the DestroyStringList method is:
803 % char **DestroyStringList(char **list)
805 % A description of each parameter follows:
807 % o list: the string list.
810 MagickExport char **DestroyStringList(char **list)
815 assert(list != (char **) NULL);
816 for (i=0; list[i] != (char *) NULL; i++)
817 list[i]=DestroyString(list[i]);
818 list=(char **) RelinquishMagickMemory(list);
823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % E s c a p e S t r i n g %
831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % EscapeString() allocates memory for a backslash-escaped version of a
834 % source text string, copies the escaped version of the text to that
835 % memory location while adding backslash characters, and returns the
838 % The format of the EscapeString method is:
840 % char *EscapeString(const char *source,const char escape)
842 % A description of each parameter follows:
844 % o allocate_string: Method EscapeString returns the escaped string.
846 % o source: A character string.
848 % o escape: the quoted string termination character to escape (e.g. '"').
851 MagickExport char *EscapeString(const char *source,const char escape)
865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
866 assert(source != (const char *) NULL);
867 length=strlen(source);
868 for (p=source; *p != '\0'; p++)
869 if ((*p == '\\') || (*p == escape))
872 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
875 destination=(char *) NULL;
876 if (~length >= (MaxTextExtent-1))
877 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
878 sizeof(*destination));
879 if (destination == (char *) NULL)
880 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
882 if (source != (char *) NULL)
885 for (p=source; *p != '\0'; p++)
887 if ((*p == '\\') || (*p == escape))
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % F i l e T o S t r i n g %
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 % FileToString() returns the contents of a file as a string.
909 % The format of the FileToString method is:
911 % char *FileToString(const char *filename,const size_t extent,
912 % ExceptionInfo *exception)
914 % A description of each parameter follows:
916 % o filename: the filename.
918 % o extent: Maximum length of the string.
920 % o exception: return any errors or warnings in this structure.
923 MagickExport char *FileToString(const char *filename,const size_t extent,
924 ExceptionInfo *exception)
929 assert(filename != (const char *) NULL);
930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
931 assert(exception != (ExceptionInfo *) NULL);
932 return((char *) FileToBlob(filename,extent,&length,exception));
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % F i l e T o S t r i n g I n f o %
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 % FileToStringInfo() returns the contents of a file as a string.
948 % The format of the FileToStringInfo method is:
950 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
951 % ExceptionInfo *exception)
953 % A description of each parameter follows:
955 % o filename: the filename.
957 % o extent: Maximum length of the string.
959 % o exception: return any errors or warnings in this structure.
962 MagickExport StringInfo *FileToStringInfo(const char *filename,
963 const size_t extent,ExceptionInfo *exception)
968 assert(filename != (const char *) NULL);
969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
970 assert(exception != (ExceptionInfo *) NULL);
971 string_info=AcquireStringInfo(0);
972 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
973 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
974 if (string_info->datum == (unsigned char *) NULL)
976 string_info=DestroyStringInfo(string_info);
977 return((StringInfo *) NULL);
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % F o r m a t M a g i c k S i z e %
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % FormatMagickSize() converts a size to a human readable format, for example,
994 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
997 % The format of the FormatMagickSize method is:
999 % ssize_t FormatMagickSize(const MagickSizeType size,char *format)
1001 % A description of each parameter follows:
1003 % o size: convert this size to a human readable format.
1005 % o bi: use power of two rather than power of ten.
1007 % o format: human readable format.
1010 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1011 const MagickBooleanType bi,char *format)
1030 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1032 *traditional_units[] =
1034 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1038 units=traditional_units;
1039 if (bi != MagickFalse)
1044 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1045 length=(double) ((MagickOffsetType) size);
1047 length=(double) size;
1049 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1051 for (j=2; j < 12; j++)
1053 count=FormatLocaleString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
1055 if (strchr(format,'+') == (char *) NULL)
1062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % F o r m a t M a g i c k T i m e %
1070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072 % FormatMagickTime() returns the specified time in the Internet date/time
1073 % format and the length of the timestamp.
1075 % The format of the FormatMagickTime method is:
1077 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1080 % A description of each parameter follows.
1082 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1083 % measured in seconds.
1085 % o length: the maximum length of the string.
1087 % o timestamp: Return the Internet date/time here.
1090 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1103 assert(timestamp != (char *) NULL);
1104 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1105 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1106 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1107 (void) localtime_r(&time,&local_time);
1113 my_time=localtime(&time);
1114 if (my_time != (struct tm *) NULL)
1115 (void) memcpy(&local_time,my_time,sizeof(local_time));
1118 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1119 (void) gmtime_r(&time,&gm_time);
1125 my_time=gmtime(&time);
1126 if (my_time != (struct tm *) NULL)
1127 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1130 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1131 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1132 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1133 (local_time.tm_yday-gm_time.tm_yday)));
1134 count=FormatLocaleString(timestamp,length,
1135 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1136 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1137 local_time.tm_min,local_time.tm_sec,(long) timezone);
1142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1146 % G e t E n v i r o n m e n t V a l u e %
1150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152 % GetEnvironmentValue() returns the environment string that matches the
1155 % The format of the GetEnvironmentValue method is:
1157 % char *GetEnvironmentValue(const char *name)
1159 % A description of each parameter follows:
1161 % o name: the environment name.
1164 MagickExport char *GetEnvironmentValue(const char *name)
1169 environment=getenv(name);
1170 if (environment == (const char *) NULL)
1171 return((char *) NULL);
1172 return(ConstantString(environment));
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 % G e t S t r i n g I n f o D a t u m %
1184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1186 % GetStringInfoDatum() returns the datum associated with the string.
1188 % The format of the GetStringInfoDatum method is:
1190 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1192 % A description of each parameter follows:
1194 % o string_info: the string info.
1197 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1199 assert(string_info != (StringInfo *) NULL);
1200 assert(string_info->signature == MagickSignature);
1201 return(string_info->datum);
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209 % G e t S t r i n g I n f o L e n g t h %
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1215 % GetStringInfoLength() returns the string length.
1217 % The format of the GetStringInfoLength method is:
1219 % size_t GetStringInfoLength(const StringInfo *string_info)
1221 % A description of each parameter follows:
1223 % o string_info: the string info.
1226 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1228 assert(string_info != (StringInfo *) NULL);
1229 assert(string_info->signature == MagickSignature);
1230 return(string_info->length);
1234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1238 % G e t S t r i n g I n f o P a t h %
1242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244 % GetStringInfoPath() returns the path associated with the string.
1246 % The format of the GetStringInfoPath method is:
1248 % const char *GetStringInfoPath(const StringInfo *string_info)
1250 % A description of each parameter follows:
1252 % o string_info: the string info.
1255 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1257 assert(string_info != (StringInfo *) NULL);
1258 assert(string_info->signature == MagickSignature);
1259 return(string_info->path);
1263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 % L o c a l e C o m p a r e %
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273 % LocaleCompare() performs a case-insensitive comparison of two strings
1274 % byte-by-byte, according to the ordering of the current locale encoding.
1275 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1276 % if the string pointed to by p is greater than, equal to, or less than the
1277 % string pointed to by q respectively. The sign of a non-zero return value
1278 % is determined by the sign of the difference between the values of the first
1279 % pair of bytes that differ in the strings being compared.
1281 % The format of the LocaleCompare method is:
1283 % int LocaleCompare(const char *p,const char *q)
1285 % A description of each parameter follows:
1287 % o p: A pointer to a character string.
1289 % o q: A pointer to a character string to compare to p.
1292 MagickExport int LocaleCompare(const char *p,const char *q)
1294 if ((p == (char *) NULL) && (q == (char *) NULL))
1296 if (p == (char *) NULL)
1298 if (q == (char *) NULL)
1300 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1301 return(strcasecmp(p,q));
1310 c=(int) *((unsigned char *) p);
1311 d=(int) *((unsigned char *) q);
1312 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1317 return(AsciiMap[c]-(int) AsciiMap[d]);
1323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327 % L o c a l e L o w e r %
1331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1333 % LocaleLower() transforms all of the characters in the supplied
1334 % null-terminated string, changing all uppercase letters to lowercase.
1336 % The format of the LocaleLower method is:
1338 % void LocaleLower(char *string)
1340 % A description of each parameter follows:
1342 % o string: A pointer to the string to convert to lower-case Locale.
1345 MagickExport void LocaleLower(char *string)
1350 assert(string != (char *) NULL);
1351 for (q=string; *q != '\0'; q++)
1352 *q=(char) tolower((int) *q);
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % L o c a l e N C o m p a r e %
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % LocaleNCompare() performs a case-insensitive comparison of two
1367 % strings byte-by-byte, according to the ordering of the current locale
1368 % encoding. LocaleNCompare returns an integer greater than, equal to, or
1369 % less than 0, if the string pointed to by p is greater than, equal to, or
1370 % less than the string pointed to by q respectively. The sign of a non-zero
1371 % return value is determined by the sign of the difference between the
1372 % values of the first pair of bytes that differ in the strings being
1373 % compared. The LocaleNCompare method makes the same comparison as
1374 % LocaleCompare but looks at a maximum of n bytes. Bytes following a
1375 % null byte are not compared.
1377 % The format of the LocaleNCompare method is:
1379 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1381 % A description of each parameter follows:
1383 % o p: A pointer to a character string.
1385 % o q: A pointer to a character string to compare to p.
1387 % o length: the number of characters to compare in strings p and q.
1390 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1392 if (p == (char *) NULL)
1394 if (q == (char *) NULL)
1396 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1397 return(strncasecmp(p,q,length));
1407 for (i=length; i != 0; i--)
1409 c=(int) *((unsigned char *) p);
1410 d=(int) *((unsigned char *) q);
1411 if (AsciiMap[c] != AsciiMap[d])
1412 return(AsciiMap[c]-(int) AsciiMap[d]);
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 % L o c a l e U p p e r %
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % LocaleUpper() transforms all of the characters in the supplied
1435 % null-terminated string, changing all lowercase letters to uppercase.
1437 % The format of the LocaleUpper method is:
1439 % void LocaleUpper(char *string)
1441 % A description of each parameter follows:
1443 % o string: A pointer to the string to convert to upper-case Locale.
1446 MagickExport void LocaleUpper(char *string)
1451 assert(string != (char *) NULL);
1452 for (q=string; *q != '\0'; q++)
1453 *q=(char) toupper((int) *q);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % P r i n t S t r i n g I n f o %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % PrintStringInfo() prints the string.
1469 % The format of the PrintStringInfo method is:
1471 % void PrintStringInfo(FILE *file,const char *id,
1472 % const StringInfo *string_info)
1474 % A description of each parameter follows:
1476 % o file: the file, typically stdout.
1478 % o id: the string id.
1480 % o string_info: the string info.
1483 MagickExport void PrintStringInfo(FILE *file,const char *id,
1484 const StringInfo *string_info)
1493 assert(id != (const char *) NULL);
1494 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1495 assert(string_info != (StringInfo *) NULL);
1496 assert(string_info->signature == MagickSignature);
1497 p=(char *) string_info->datum;
1498 for (i=0; i < string_info->length; i++)
1500 if (((int) ((unsigned char) *p) < 32) &&
1501 (isspace((int) ((unsigned char) *p)) == 0))
1505 if (i == string_info->length)
1507 (void) fputs((char *) string_info->datum,file);
1508 (void) fputc('\n',file);
1512 Convert string to a HEX list.
1514 p=(char *) string_info->datum;
1515 for (i=0; i < string_info->length; i+=0x14)
1517 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
1518 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1520 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1521 if ((j % 0x04) == 0)
1522 (void) fputc(' ',file);
1524 for ( ; j <= 0x14; j++)
1526 (void) fputc(' ',file);
1527 (void) fputc(' ',file);
1528 if ((j % 0x04) == 0)
1529 (void) fputc(' ',file);
1531 (void) fputc(' ',file);
1532 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1534 if (isprint((int) ((unsigned char) *p)) != 0)
1535 (void) fputc(*p,file);
1537 (void) fputc('-',file);
1540 (void) fputc('\n',file);
1545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1549 % R e s e t S t r i n g I n f o %
1553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555 % ResetStringInfo() reset the string to all null bytes.
1557 % The format of the ResetStringInfo method is:
1559 % void ResetStringInfo(StringInfo *string_info)
1561 % A description of each parameter follows:
1563 % o string_info: the string info.
1566 MagickExport void ResetStringInfo(StringInfo *string_info)
1568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1569 assert(string_info != (StringInfo *) NULL);
1570 assert(string_info->signature == MagickSignature);
1571 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1579 % S e t S t r i n g I n f o %
1583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1585 % SetStringInfo() copies the source string to the destination string.
1587 % The format of the SetStringInfo method is:
1589 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1591 % A description of each parameter follows:
1593 % o string_info: the string info.
1595 % o source: the source string.
1598 MagickExport void SetStringInfo(StringInfo *string_info,
1599 const StringInfo *source)
1601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1602 assert(string_info != (StringInfo *) NULL);
1603 assert(string_info->signature == MagickSignature);
1604 assert(source != (StringInfo *) NULL);
1605 assert(source->signature == MagickSignature);
1606 if (string_info->length == 0)
1608 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1609 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1618 % S e t S t r i n g I n f o D a t u m %
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % SetStringInfoDatum() copies bytes from the source string for the length of
1625 % the destination string.
1627 % The format of the SetStringInfoDatum method is:
1629 % void SetStringInfoDatum(StringInfo *string_info,
1630 % const unsigned char *source)
1632 % A description of each parameter follows:
1634 % o string_info: the string info.
1636 % o source: the source string.
1639 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1640 const unsigned char *source)
1642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1643 assert(string_info != (StringInfo *) NULL);
1644 assert(string_info->signature == MagickSignature);
1645 if (string_info->length != 0)
1646 (void) memcpy(string_info->datum,source,string_info->length);
1650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1654 % S e t S t r i n g I n f o L e n g t h %
1658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1660 % SetStringInfoLength() set the string length to the specified value.
1662 % The format of the SetStringInfoLength method is:
1664 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1666 % A description of each parameter follows:
1668 % o string_info: the string info.
1670 % o length: the string length.
1673 MagickExport void SetStringInfoLength(StringInfo *string_info,
1674 const size_t length)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1677 assert(string_info != (StringInfo *) NULL);
1678 assert(string_info->signature == MagickSignature);
1679 if (~length < MaxTextExtent)
1680 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1681 string_info->length=length;
1682 if (string_info->datum == (unsigned char *) NULL)
1683 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1684 MaxTextExtent,sizeof(*string_info->datum));
1686 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1687 length+MaxTextExtent,sizeof(*string_info->datum));
1688 if (string_info->datum == (unsigned char *) NULL)
1689 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1697 % S e t S t r i n g I n f o D a t u m %
1701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1703 % SetStringInfoPath() sets the path associated with the string.
1705 % The format of the SetStringInfoPath method is:
1707 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1709 % A description of each parameter follows:
1711 % o string_info: the string info.
1716 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1718 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1719 assert(string_info != (StringInfo *) NULL);
1720 assert(string_info->signature == MagickSignature);
1721 assert(path != (const char *) NULL);
1722 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % S p l i t S t r i n g I n f o %
1734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736 % SplitStringInfo() splits a string into two and returns it.
1738 % The format of the SplitStringInfo method is:
1740 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1742 % A description of each parameter follows:
1744 % o string_info: the string info.
1747 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1748 const size_t offset)
1753 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1754 assert(string_info != (StringInfo *) NULL);
1755 assert(string_info->signature == MagickSignature);
1756 if (offset > string_info->length)
1757 return((StringInfo *) NULL);
1758 split_info=AcquireStringInfo(offset);
1759 SetStringInfo(split_info,string_info);
1760 (void) memmove(string_info->datum,string_info->datum+offset,
1761 string_info->length-offset+MaxTextExtent);
1762 SetStringInfoLength(string_info,string_info->length-offset);
1767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1771 % S t r i n g I n f o T o S t r i n g %
1775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1777 % StringInfoToString() converts a string info string to a C string.
1779 % The format of the StringInfoToString method is:
1781 % char *StringInfoToString(const StringInfo *string_info)
1783 % A description of each parameter follows:
1785 % o string_info: the string.
1788 MagickExport char *StringInfoToString(const StringInfo *string_info)
1796 string=(char *) NULL;
1797 length=string_info->length;
1798 if (~length >= (MaxTextExtent-1))
1799 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1800 if (string == (char *) NULL)
1801 return((char *) NULL);
1802 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1803 string[length]='\0';
1808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812 % S t r i n g T o A r g v %
1816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 % StringToArgv() converts a text string into command line arguments.
1820 % The format of the StringToArgv method is:
1822 % char **StringToArgv(const char *text,int *argc)
1824 % A description of each parameter follows:
1826 % o argv: Method StringToArgv returns the string list unless an error
1827 % occurs, otherwise NULL.
1829 % o text: Specifies the string to segment into a list.
1831 % o argc: This integer pointer returns the number of arguments in the
1835 MagickExport char **StringToArgv(const char *text,int *argc)
1848 if (text == (char *) NULL)
1849 return((char **) NULL);
1851 Determine the number of arguments.
1853 for (p=text; *p != '\0'; )
1855 while (isspace((int) ((unsigned char) *p)) != 0)
1861 for (p++; (*p != '"') && (*p != '\0'); p++) ;
1863 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
1864 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1868 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
1869 if (argv == (char **) NULL)
1870 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
1872 Convert string to an ASCII list.
1874 argv[0]=AcquireString("magick");
1876 for (i=1; i < (ssize_t) *argc; i++)
1878 while (isspace((int) ((unsigned char) *p)) != 0)
1884 for (q++; (*q != '"') && (*q != '\0'); q++) ;
1890 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
1893 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
1895 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1897 if (argv[i] == (char *) NULL)
1899 for (i--; i >= 0; i--)
1900 argv[i]=DestroyString(argv[i]);
1901 argv=(char **) RelinquishMagickMemory(argv);
1902 ThrowFatalException(ResourceLimitFatalError,
1903 "UnableToConvertStringToARGV");
1905 (void) memcpy(argv[i],p,(size_t) (q-p));
1908 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1911 argv[i]=(char *) NULL;
1916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1920 % S t r i n g I n f o T o H e x S t r i n g %
1924 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1926 % StringInfoToHexString() converts a string info string to a C string.
1928 % The format of the StringInfoToHexString method is:
1930 % char *StringInfoToHexString(const StringInfo *string_info)
1932 % A description of each parameter follows:
1934 % o string_info: the string.
1937 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1942 register const unsigned char
1948 register unsigned char
1957 length=string_info->length;
1958 if (~length < MaxTextExtent)
1959 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1960 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
1961 if (string == (char *) NULL)
1962 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1979 p=string_info->datum;
1980 q=(unsigned char *) string;
1981 for (i=0; i < (ssize_t) string_info->length; i++)
1983 *q++=hex_digits[(*p >> 4) & 0x0f];
1984 *q++=hex_digits[*p & 0x0f];
1992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1996 % S t r i n g T o k e n %
2000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2002 % StringToken() extracts a token a from the string.
2004 % The format of the StringToken method is:
2006 % char *StringToken(const char *delimiters,char **string)
2008 % A description of each parameter follows:
2010 % o delimiters: one or more delimiters.
2012 % o string: return the first token in the string. If none is found, return
2016 MagickExport char *StringToken(const char *delimiters,char **string)
2032 if (p == (char *) NULL)
2033 return((char *) NULL);
2050 } while (d != '\0');
2055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2059 % S t r i n g T o L i s t %
2063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065 % StringToList() converts a text string into a list by segmenting the text
2066 % string at each carriage return discovered. The list is converted to HEX
2067 % characters if any control characters are discovered within the text string.
2069 % The format of the StringToList method is:
2071 % char **StringToList(const char *text)
2073 % A description of each parameter follows:
2075 % o text: Specifies the string to segment into a list.
2078 MagickExport char **StringToList(const char *text)
2092 if (text == (char *) NULL)
2093 return((char **) NULL);
2094 for (p=text; *p != '\0'; p++)
2095 if (((int) ((unsigned char) *p) < 32) &&
2096 (isspace((int) ((unsigned char) *p)) == 0))
2104 Convert string to an ASCII list.
2107 for (p=text; *p != '\0'; p++)
2110 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2112 if (textlist == (char **) NULL)
2113 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2115 for (i=0; i < (ssize_t) lines; i++)
2117 for (q=p; *q != '\0'; q++)
2118 if ((*q == '\r') || (*q == '\n'))
2120 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2121 sizeof(**textlist));
2122 if (textlist[i] == (char *) NULL)
2123 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2124 (void) memcpy(textlist[i],p,(size_t) (q-p));
2125 textlist[i][q-p]='\0';
2134 hex_string[MaxTextExtent];
2143 Convert string to a HEX list.
2145 lines=(size_t) (strlen(text)/0x14)+1;
2146 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2148 if (textlist == (char **) NULL)
2149 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2151 for (i=0; i < (ssize_t) lines; i++)
2153 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2154 sizeof(**textlist));
2155 if (textlist[i] == (char *) NULL)
2156 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2157 (void) FormatLocaleString(textlist[i],MaxTextExtent,"0x%08lx: ",
2159 q=textlist[i]+strlen(textlist[i]);
2160 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2162 (void) FormatLocaleString(hex_string,MaxTextExtent,"%02x",*(p+j));
2163 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2165 if ((j % 0x04) == 0)
2168 for ( ; j <= 0x14; j++)
2172 if ((j % 0x04) == 0)
2176 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2178 if (isprint((int) ((unsigned char) *p)) != 0)
2187 textlist[i]=(char *) NULL;
2192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2196 % S t r i n g T o S t r i n g I n f o %
2200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2202 % StringToStringInfo() converts a string to a StringInfo type.
2204 % The format of the StringToStringInfo method is:
2206 % StringInfo *StringToStringInfo(const char *string)
2208 % A description of each parameter follows:
2210 % o string: The string.
2213 MagickExport StringInfo *StringToStringInfo(const char *string)
2218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2219 assert(string != (const char *) NULL);
2220 string_info=AcquireStringInfo(strlen(string));
2221 SetStringInfoDatum(string_info,(const unsigned char *) string);
2222 return(string_info);
2226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2230 % S t r i p S t r i n g %
2234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2236 % StripString() strips any whitespace or quotes from the beginning and end of
2237 % a string of characters.
2239 % The format of the StripString method is:
2241 % void StripString(char *message)
2243 % A description of each parameter follows:
2245 % o message: Specifies an array of characters.
2248 MagickExport void StripString(char *message)
2257 assert(message != (char *) NULL);
2258 if (*message == '\0')
2260 length=strlen(message);
2262 while (isspace((int) ((unsigned char) *p)) != 0)
2264 if ((*p == '\'') || (*p == '"'))
2267 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2270 if ((*q == '\'') || (*q == '"'))
2272 (void) memmove(message,p,(size_t) (q-p+1));
2273 message[q-p+1]='\0';
2274 for (p=message; *p != '\0'; p++)
2280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2284 % S u b s t i t u t e S t r i n g %
2288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2290 % SubstituteString() performs string substitution on a string, replacing the
2291 % string with the substituted version. Buffer must be allocated from the heap.
2292 % If the string is matched and status, MagickTrue is returned otherwise
2295 % The format of the SubstituteString method is:
2297 % MagickBooleanType SubstituteString(char **string,const char *search,
2298 % const char *replace)
2300 % A description of each parameter follows:
2302 % o string: the string to perform replacements on; replaced with new
2303 % allocation if a replacement is made.
2305 % o search: search for this string.
2307 % o replace: replace any matches with this string.
2310 MagickExport MagickBooleanType SubstituteString(char **string,
2311 const char *search,const char *replace)
2330 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2332 if (search_extent == 0)
2333 search_extent=strlen(search);
2334 if (strncmp(p,search,search_extent) != 0)
2340 if (replace_extent == 0)
2341 replace_extent=strlen(replace);
2342 if (replace_extent > search_extent)
2345 Make room for the replacement string.
2347 offset=(ssize_t) (p-(*string));
2348 extent=strlen(*string)+replace_extent-search_extent+1;
2349 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2351 if (*string == (char *) NULL)
2352 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2358 if (search_extent != replace_extent)
2359 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2360 strlen(p+search_extent)+1);
2361 (void) CopyMagickMemory(p,replace,replace_extent);
2362 p+=replace_extent-1;