2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT RRRR IIIII N N GGGG %
8 % SSS T RRRR I N N N G GGG %
9 % SS T R R I N NN G G %
10 % SSSSS T R R IIIII N N GGGG %
13 % MagickCore String Methods %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/list.h"
48 #include "magick/log.h"
49 #include "magick/memory_.h"
50 #include "magick/property.h"
51 #include "magick/resource_.h"
52 #include "magick/signature-private.h"
53 #include "magick/string_.h"
58 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
59 static const unsigned char
62 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
63 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
64 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
65 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
66 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
67 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
68 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
69 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
70 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
71 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
72 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
73 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
74 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
75 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
76 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
77 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
78 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
79 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
80 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
81 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
82 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
83 0xfc, 0xfd, 0xfe, 0xff,
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % A c q u i r e S t r i n g %
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % AcquireString() allocates memory for a string and copies the source string
99 % to that memory location (and returns it).
101 % The format of the AcquireString method is:
103 % char *AcquireString(const char *source)
105 % A description of each parameter follows:
107 % o source: A character string.
110 MagickExport char *AcquireString(const char *source)
119 if (source != (char *) NULL)
120 length+=strlen(source);
121 destination=(char *) NULL;
122 if (~length >= MaxTextExtent)
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) CopyMagickString(destination,source,(length+1)*sizeof(*destination));
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % A c q u i r e S t r i n g I n f o %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % AcquireStringInfo() allocates the StringInfo structure.
146 % The format of the AcquireStringInfo method is:
148 % StringInfo *AcquireStringInfo(const size_t length)
150 % A description of each parameter follows:
152 % o length: the string length.
155 MagickExport StringInfo *AcquireStringInfo(const size_t length)
160 string_info=(StringInfo *) AcquireAlignedMemory(1,sizeof(*string_info));
161 if (string_info == (StringInfo *) NULL)
162 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
163 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
164 string_info->signature=MagickSignature;
165 string_info->length=length;
166 if (string_info->length != 0)
168 string_info->datum=(unsigned char *) NULL;
169 if (~string_info->length >= MaxTextExtent)
170 string_info->datum=(unsigned char *) AcquireQuantumMemory(
171 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
172 if (string_info->datum == (unsigned char *) NULL)
173 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % C l o n e S t r i n g %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % CloneString() allocates memory for the destination string and copies
190 % the source string to that memory location.
192 % The format of the CloneString method is:
194 % char *CloneString(char **destination,const char *source)
196 % A description of each parameter follows:
198 % o destination: A pointer to a character string.
200 % o source: A character string.
203 MagickExport char *CloneString(char **destination,const char *source)
208 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
209 assert(destination != (char **) NULL);
210 if (source == (const char *) NULL)
212 if (*destination != (char *) NULL)
213 *destination=DestroyString(*destination);
214 return(*destination);
216 if (*destination == (char *) NULL)
218 *destination=AcquireString(source);
219 return(*destination);
221 length=strlen(source);
222 if (~length < MaxTextExtent)
223 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
224 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
225 sizeof(*destination));
226 if (*destination == (char *) NULL)
227 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
228 (void) CopyMagickString(*destination,source,(length+1)*sizeof(*destination));
229 return(*destination);
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % C l o n e S t r i n g I n f o %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % CloneStringInfo() clones a copy of the StringInfo structure.
245 % The format of the CloneStringInfo method is:
247 % StringInfo *CloneStringInfo(const StringInfo *string_info)
249 % A description of each parameter follows:
251 % o string_info: the string info.
254 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
259 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
260 assert(string_info != (StringInfo *) NULL);
261 assert(string_info->signature == MagickSignature);
262 clone_info=AcquireStringInfo(string_info->length);
263 if (string_info->length != 0)
264 (void) CopyMagickMemory(clone_info->datum,string_info->datum,
265 string_info->length+1);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % C o m p a r e S t r i n g I n f o %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % CompareStringInfo() compares the two datums target and source. It returns
281 % an integer less than, equal to, or greater than zero if target is found,
282 % respectively, to be less than, to match, or be greater than source.
284 % The format of the CompareStringInfo method is:
286 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
288 % A description of each parameter follows:
290 % o target: the target string.
292 % o source: the source string.
296 static inline size_t MagickMin(const size_t x,const size_t y)
303 MagickExport int CompareStringInfo(const StringInfo *target,
304 const StringInfo *source)
309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
310 assert(target != (StringInfo *) NULL);
311 assert(target->signature == MagickSignature);
312 assert(source != (StringInfo *) NULL);
313 assert(source->signature == MagickSignature);
314 status=memcmp(target->datum,source->datum,MagickMin(target->length,
318 if (target->length == source->length)
320 return(target->length < source->length ? -1 : 1);
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % C o n c a t e n a t e M a g i c k S t r i n g %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % ConcatenateMagickString() concatenates the source string to the destination
335 % string. The destination buffer is always null-terminated even if the
336 % string must be truncated.
338 % The format of the ConcatenateMagickString method is:
340 % size_t ConcatenateMagickString(char *destination,const char *source,
341 % const size_t length)
343 % A description of each parameter follows:
345 % o destination: the destination string.
347 % o source: the source string.
349 % o length: the length of the destination string.
352 MagickExport size_t ConcatenateMagickString(char *destination,
353 const char *source,const size_t length)
367 assert(destination != (char *) NULL);
368 assert(source != (const char *) NULL);
373 while ((i-- != 0) && (*q != '\0'))
375 count=(size_t) (q-destination);
378 return(count+strlen(p));
389 return(count+(p-source));
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % C o n c a t e n a t e S t r i n g %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % ConcatenateString() appends a copy of string source, including the
404 % terminating null character, to the end of string destination.
406 % The format of the ConcatenateString method is:
408 % MagickBooleanType ConcatenateString(char **destination,
409 % const char *source)
411 % A description of each parameter follows:
413 % o destination: A pointer to a character string.
415 % o source: A character string.
418 MagickExport MagickBooleanType ConcatenateString(char **destination,
425 assert(destination != (char **) NULL);
426 if (source == (const char *) NULL)
428 if (*destination == (char *) NULL)
430 *destination=AcquireString(source);
433 length=strlen(*destination);
434 source_length=strlen(source);
435 if (~length < source_length)
436 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
437 length+=source_length;
438 if (~length < MaxTextExtent)
439 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
440 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
441 sizeof(*destination));
442 if (*destination == (char *) NULL)
443 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
444 (void) ConcatenateMagickString(*destination,source,(length+1)*
445 sizeof(*destination));
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % C o n c a t e n a t e S t r i n g I n f o %
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % ConcatenateStringInfo() concatenates the source string to the destination
463 % The format of the ConcatenateStringInfo method is:
465 % void ConcatenateStringInfo(StringInfo *string_info,
466 % const StringInfo *source)
468 % A description of each parameter follows:
470 % o string_info: the string info.
472 % o source: the source string.
475 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
476 const StringInfo *source)
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
482 assert(string_info != (StringInfo *) NULL);
483 assert(string_info->signature == MagickSignature);
484 assert(source != (const StringInfo *) NULL);
485 length=string_info->length;
486 if (~length < source->length)
487 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
488 SetStringInfoLength(string_info,length+source->length);
489 (void) CopyMagickMemory(string_info->datum+length,source->datum,
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % 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 %
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % ConfigureFileToStringInfo() returns the contents of a configure file as a
507 % The format of the ConfigureFileToStringInfo method is:
509 % StringInfo *ConfigureFileToStringInfo(const char *filename)
510 % ExceptionInfo *exception)
512 % A description of each parameter follows:
514 % o filename: the filename.
517 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
537 assert(filename != (const char *) NULL);
538 file=open(filename,O_RDONLY | O_BINARY);
540 return((StringInfo *) NULL);
541 offset=(MagickOffsetType) MagickSeek(file,0,SEEK_END);
542 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
545 return((StringInfo *) NULL);
547 length=(size_t) offset;
548 string=(char *) NULL;
549 if (~length > MaxTextExtent)
550 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
551 if (string == (char *) NULL)
554 return((StringInfo *) NULL);
556 map=MapBlob(file,ReadMode,0,length);
557 if (map != (void *) NULL)
559 (void) CopyMagickMemory(string,map,length);
560 (void) UnmapBlob(map,length);
570 (void) MagickSeek(file,0,SEEK_SET);
571 for (i=0; i < length; i+=count)
573 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
585 string=DestroyString(string);
586 return((StringInfo *) NULL);
591 string_info=AcquireStringInfo(0);
592 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
593 string_info->length=length;
594 string_info->datum=(unsigned char *) string;
599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
603 % C o n s t a n t S t r i n g %
607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 % ConstantString() allocates memory for a string and copies the source string
610 % to that memory location (and returns it). Use it for strings that you do
611 % do not expect to change over its lifetime.
613 % The format of the ConstantString method is:
615 % char *ConstantString(const char *source)
617 % A description of each parameter follows:
619 % o source: A character string.
622 MagickExport char *ConstantString(const char *source)
631 if (source != (char *) NULL)
632 length+=strlen(source);
633 destination=(char *) NULL;
635 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
636 if (destination == (char *) NULL)
637 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
639 if (source != (char *) NULL)
640 (void) CopyMagickString(destination,source,(length+1)*sizeof(*destination));
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % C o p y M a g i c k S t r i n g %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % CopyMagickString() copies the source string to the destination string. The
656 % destination buffer is always null-terminated even if the string must be
657 % truncated. The return value is the minimum of the source string length
658 % or the length parameter.
660 % The format of the CopyMagickString method is:
662 % size_t CopyMagickString(const char *destination,char *source,
663 % const size_t length)
665 % A description of each parameter follows:
667 % o destination: the destination string.
669 % o source: the source string.
671 % o length: the length of the destination string.
674 MagickExport size_t CopyMagickString(char *destination,const char *source,
688 for (n=length; n > 4; n-=4)
692 return((size_t) (p-source-1));
696 return((size_t) (p-source-1));
700 return((size_t) (p-source-1));
704 return((size_t) (p-source-1));
708 for (n--; n != 0; n--)
712 return((size_t) (p-source-1));
717 return((size_t) (p-source-1));
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 % D e s t r o y S t r i n g %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % DestroyString() destroys memory associated with a string.
733 % The format of the DestroyString method is:
735 % char *DestroyString(char *string)
737 % A description of each parameter follows:
739 % o string: the string.
742 MagickExport char *DestroyString(char *string)
744 return((char *) RelinquishMagickMemory(string));
748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 % D e s t r o y S t r i n g I n f o %
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
760 % The format of the DestroyStringInfo method is:
762 % StringInfo *DestroyStringInfo(StringInfo *string_info)
764 % A description of each parameter follows:
766 % o string_info: the string info.
769 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
772 assert(string_info != (StringInfo *) NULL);
773 assert(string_info->signature == MagickSignature);
774 if (string_info->datum != (unsigned char *) NULL)
775 string_info->datum=(unsigned char *) RelinquishMagickMemory(
777 string_info->signature=(~MagickSignature);
778 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 % D e s t r o y S t r i n g L i s t %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % DestroyStringList() zeros memory associated with a string list.
795 % The format of the DestroyStringList method is:
797 % char **DestroyStringList(char **list)
799 % A description of each parameter follows:
801 % o list: the string list.
804 MagickExport char **DestroyStringList(char **list)
809 assert(list != (char **) NULL);
810 for (i=0; list[i] != (char *) NULL; i++)
811 list[i]=DestroyString(list[i]);
812 list=(char **) RelinquishMagickMemory(list);
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % E s c a p e S t r i n g %
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % EscapeString() allocates memory for a backslash-escaped version of a
828 % source text string, copies the escaped version of the text to that
829 % memory location while adding backslash characters, and returns the
832 % The format of the EscapeString method is:
834 % char *EscapeString(const char *source,const char escape)
836 % A description of each parameter follows:
838 % o allocate_string: Method EscapeString returns the escaped string.
840 % o source: A character string.
842 % o escape: the quoted string termination character to escape (e.g. '"').
845 MagickExport char *EscapeString(const char *source,const char escape)
859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
860 assert(source != (const char *) NULL);
861 length=strlen(source);
862 for (p=source; *p != '\0'; p++)
863 if ((*p == '\\') || (*p == escape))
866 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
869 destination=(char *) NULL;
870 if (~length >= MaxTextExtent)
871 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
872 sizeof(*destination));
873 if (destination == (char *) NULL)
874 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
876 if (source != (char *) NULL)
879 for (p=source; *p != '\0'; p++)
881 if ((*p == '\\') || (*p == escape))
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 % F i l e T o S t r i n g %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % FileToString() returns the contents of a file as a string.
903 % The format of the FileToString method is:
905 % char *FileToString(const char *filename,const size_t extent,
906 % ExceptionInfo *exception)
908 % A description of each parameter follows:
910 % o filename: the filename.
912 % o extent: Maximum length of the string.
914 % o exception: return any errors or warnings in this structure.
917 MagickExport char *FileToString(const char *filename,const size_t extent,
918 ExceptionInfo *exception)
923 assert(filename != (const char *) NULL);
924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
925 assert(exception != (ExceptionInfo *) NULL);
926 return((char *) FileToBlob(filename,extent,&length,exception));
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934 % F i l e T o S t r i n g I n f o %
938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % FileToStringInfo() returns the contents of a file as a string.
942 % The format of the FileToStringInfo method is:
944 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
945 % ExceptionInfo *exception)
947 % A description of each parameter follows:
949 % o filename: the filename.
951 % o extent: Maximum length of the string.
953 % o exception: return any errors or warnings in this structure.
956 MagickExport StringInfo *FileToStringInfo(const char *filename,
957 const size_t extent,ExceptionInfo *exception)
962 assert(filename != (const char *) NULL);
963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
964 assert(exception != (ExceptionInfo *) NULL);
965 string_info=AcquireStringInfo(0);
966 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
967 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
968 if (string_info->datum == (unsigned char *) NULL)
970 string_info=DestroyStringInfo(string_info);
971 return((StringInfo *) NULL);
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 % F o r m a t M a g i c k S i z e %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % FormatMagickSize() converts a size to a human readable format, for example,
988 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
991 % The format of the FormatMagickSize method is:
993 % ssize_t FormatMagickSize(const MagickSizeType size,char *format)
995 % A description of each parameter follows:
997 % o size: convert this size to a human readable format.
999 % o bi: use power of two rather than power of ten.
1001 % o format: human readable format.
1004 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1005 const MagickBooleanType bi,char *format)
1024 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1026 *traditional_units[] =
1028 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1032 units=traditional_units;
1033 if (bi != MagickFalse)
1038 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1039 length=(double) ((MagickOffsetType) size);
1041 length=(double) size;
1043 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1045 for (j=2; j < 12; j++)
1047 count=FormatMagickString(format,MaxTextExtent,"%.*g%s",(int) (i+j),length,
1049 if (strchr(format,'+') == (char *) NULL)
1056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 % F o r m a t M a g i c k S t r i n g %
1064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % FormatMagickString() prints formatted output of a variable argument list.
1068 % The format of the FormatMagickString method is:
1070 % ssize_t FormatMagickString(char *string,const size_t length,
1071 % const char *format,...)
1073 % A description of each parameter follows.
1075 % o string: FormatMagickString() returns the formatted string in this
1078 % o length: the maximum length of the string.
1080 % o format: A string describing the format to use to write the remaining
1085 MagickExport ssize_t FormatMagickStringList(char *string,const size_t length,
1086 const char *format,va_list operands)
1091 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1092 n=vsnprintf(string,length,format,operands);
1094 n=vsprintf(string,format,operands);
1097 string[length-1]='\0';
1098 return((ssize_t) n);
1101 MagickExport ssize_t FormatMagickString(char *string,const size_t length,
1102 const char *format,...)
1110 va_start(operands,format);
1111 n=(ssize_t) FormatMagickStringList(string,length,format,operands);
1117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1121 % F o r m a t M a g i c k T i m e %
1125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 % FormatMagickTime() returns the specified time in the Internet date/time
1128 % format and the length of the timestamp.
1130 % The format of the FormatMagickTime method is:
1132 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1135 % A description of each parameter follows.
1137 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1138 % measured in seconds.
1140 % o length: the maximum length of the string.
1142 % o timestamp: Return the Internet date/time here.
1145 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1158 assert(timestamp != (char *) NULL);
1159 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1160 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1161 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1162 (void) localtime_r(&time,&local_time);
1168 my_time=localtime(&time);
1169 if (my_time != (struct tm *) NULL)
1170 (void) memcpy(&local_time,my_time,sizeof(local_time));
1173 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1174 (void) gmtime_r(&time,&gm_time);
1180 my_time=gmtime(&time);
1181 if (my_time != (struct tm *) NULL)
1182 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1185 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1186 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1187 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1188 (local_time.tm_yday-gm_time.tm_yday)));
1189 count=FormatMagickString(timestamp,length,
1190 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1191 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1192 local_time.tm_min,local_time.tm_sec,(long) timezone);
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201 % G e t E n v i r o n m e n t V a l u e %
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207 % GetEnvironmentValue() returns the environment string that matches the
1210 % The format of the GetEnvironmentValue method is:
1212 % char *GetEnvironmentValue(const char *name)
1214 % A description of each parameter follows:
1216 % o name: the environment name.
1219 MagickExport char *GetEnvironmentValue(const char *name)
1224 environment=getenv(name);
1225 if (environment == (const char *) NULL)
1226 return((char *) NULL);
1227 return(ConstantString(environment));
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235 % G e t S t r i n g I n f o D a t u m %
1239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1241 % GetStringInfoDatum() returns the datum associated with the string.
1243 % The format of the GetStringInfoDatum method is:
1245 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1247 % A description of each parameter follows:
1249 % o string_info: the string info.
1252 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1254 assert(string_info != (StringInfo *) NULL);
1255 assert(string_info->signature == MagickSignature);
1256 return(string_info->datum);
1260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 % G e t S t r i n g I n f o L e n g t h %
1268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1270 % GetStringInfoLength() returns the string length.
1272 % The format of the GetStringInfoLength method is:
1274 % size_t GetStringInfoLength(const StringInfo *string_info)
1276 % A description of each parameter follows:
1278 % o string_info: the string info.
1281 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1283 assert(string_info != (StringInfo *) NULL);
1284 assert(string_info->signature == MagickSignature);
1285 return(string_info->length);
1289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1293 % G e t S t r i n g I n f o P a t h %
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1299 % GetStringInfoPath() returns the path associated with the string.
1301 % The format of the GetStringInfoPath method is:
1303 % const char *GetStringInfoPath(const StringInfo *string_info)
1305 % A description of each parameter follows:
1307 % o string_info: the string info.
1310 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1312 assert(string_info != (StringInfo *) NULL);
1313 assert(string_info->signature == MagickSignature);
1314 return(string_info->path);
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1322 % L o c a l e C o m p a r e %
1326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328 % LocaleCompare() performs a case-insensitive comparison of two strings
1329 % byte-by-byte, according to the ordering of the current locale encoding.
1330 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1331 % if the string pointed to by p is greater than, equal to, or less than the
1332 % string pointed to by q respectively. The sign of a non-zero return value
1333 % is determined by the sign of the difference between the values of the first< % pair of bytes that differ in the strings being compared.
1335 % The format of the LocaleCompare method is:
1337 % int LocaleCompare(const char *p,const char *q)
1339 % A description of each parameter follows:
1341 % o p: A pointer to a character string.
1343 % o q: A pointer to a character string to compare to p.
1346 MagickExport int LocaleCompare(const char *p,const char *q)
1348 if ((p == (char *) NULL) && (q == (char *) NULL))
1350 if (p == (char *) NULL)
1352 if (q == (char *) NULL)
1354 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1355 return(strcasecmp(p,q));
1364 c=(int) *((unsigned char *) p);
1365 d=(int) *((unsigned char *) q);
1366 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1371 return(AsciiMap[c]-(int) AsciiMap[d]);
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 % L o c a l e L o w e r %
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % LocaleLower() transforms all of the characters in the supplied
1388 % null-terminated string, changing all uppercase letters to lowercase.
1390 % The format of the LocaleLower method is:
1392 % void LocaleLower(char *string)
1394 % A description of each parameter follows:
1396 % o string: A pointer to the string to convert to lower-case Locale.
1399 MagickExport void LocaleLower(char *string)
1404 assert(string != (char *) NULL);
1405 for (q=string; *q != '\0'; q++)
1406 *q=(char) tolower((int) *q);
1410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 % L o c a l e N C o m p a r e %
1418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 % LocaleNCompare() performs a case-insensitive comparison of two
1421 % strings byte-by-byte, according to the ordering of the current locale
1422 % encoding. LocaleNCompare returns an integer greater than, equal to, or
1423 % less than 0, if the string pointed to by p is greater than, equal to, or
1424 % less than the string pointed to by q respectively. The sign of a non-zero
1425 % return value is determined by the sign of the difference between the
1426 % values of the first pair of bytes that differ in the strings being
1427 % compared. The LocaleNCompare method makes the same comparison as
1428 % LocaleCompare but looks at a maximum of n bytes. Bytes following a
1429 % null byte are not compared.
1431 % The format of the LocaleNCompare method is:
1433 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1435 % A description of each parameter follows:
1437 % o p: A pointer to a character string.
1439 % o q: A pointer to a character string to compare to p.
1441 % o length: the number of characters to compare in strings p & q.
1444 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1446 if (p == (char *) NULL)
1448 if (q == (char *) NULL)
1450 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1451 return(strncasecmp(p,q,length));
1461 for (i=length; i != 0; i--)
1463 c=(int) *((unsigned char *) p);
1464 d=(int) *((unsigned char *) q);
1465 if (AsciiMap[c] != AsciiMap[d])
1466 return(AsciiMap[c]-(int) AsciiMap[d]);
1478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1482 % L o c a l e U p p e r %
1486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488 % LocaleUpper() transforms all of the characters in the supplied
1489 % null-terminated string, changing all lowercase letters to uppercase.
1491 % The format of the LocaleUpper method is:
1493 % void LocaleUpper(char *string)
1495 % A description of each parameter follows:
1497 % o string: A pointer to the string to convert to upper-case Locale.
1500 MagickExport void LocaleUpper(char *string)
1505 assert(string != (char *) NULL);
1506 for (q=string; *q != '\0'; q++)
1507 *q=(char) toupper((int) *q);
1511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515 % P r i n t S t r i n g I n f o %
1519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1521 % PrintStringInfo() prints the string.
1523 % The format of the PrintStringInfo method is:
1525 % void PrintStringInfo(FILE *file,const char *id,
1526 % const StringInfo *string_info)
1528 % A description of each parameter follows:
1530 % o file: the file, typically stdout.
1532 % o id: the string id.
1534 % o string_info: the string info.
1537 MagickExport void PrintStringInfo(FILE *file,const char *id,
1538 const StringInfo *string_info)
1547 assert(id != (const char *) NULL);
1548 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1549 assert(string_info != (StringInfo *) NULL);
1550 assert(string_info->signature == MagickSignature);
1551 p=(char *) string_info->datum;
1552 for (i=0; i < string_info->length; i++)
1554 if (((int) ((unsigned char) *p) < 32) &&
1555 (isspace((int) ((unsigned char) *p)) == 0))
1559 if (i == string_info->length)
1561 (void) fputs((char *) string_info->datum,file);
1562 (void) fputc('\n',file);
1566 Convert string to a HEX list.
1568 p=(char *) string_info->datum;
1569 for (i=0; i < string_info->length; i+=0x14)
1571 (void) fprintf(file,"0x%08lx: ",(unsigned long) (0x14*i));
1572 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1574 (void) fprintf(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1575 if ((j % 0x04) == 0)
1576 (void) fputc(' ',file);
1578 for ( ; j <= 0x14; j++)
1580 (void) fputc(' ',file);
1581 (void) fputc(' ',file);
1582 if ((j % 0x04) == 0)
1583 (void) fputc(' ',file);
1585 (void) fputc(' ',file);
1586 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1588 if (isprint((int) ((unsigned char) *p)) != 0)
1589 (void) fputc(*p,file);
1591 (void) fputc('-',file);
1594 (void) fputc('\n',file);
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 % R e s e t S t r i n g I n f o %
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609 % ResetStringInfo() reset the string to all null bytes.
1611 % The format of the ResetStringInfo method is:
1613 % void ResetStringInfo(StringInfo *string_info)
1615 % A description of each parameter follows:
1617 % o string_info: the string info.
1620 MagickExport void ResetStringInfo(StringInfo *string_info)
1622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1623 assert(string_info != (StringInfo *) NULL);
1624 assert(string_info->signature == MagickSignature);
1625 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1633 % S e t S t r i n g I n f o %
1637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639 % SetStringInfo() copies the source string to the destination string.
1641 % The format of the SetStringInfo method is:
1643 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1645 % A description of each parameter follows:
1647 % o string_info: the string info.
1649 % o source: the source string.
1652 MagickExport void SetStringInfo(StringInfo *string_info,
1653 const StringInfo *source)
1655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1656 assert(string_info != (StringInfo *) NULL);
1657 assert(string_info->signature == MagickSignature);
1658 assert(source != (StringInfo *) NULL);
1659 assert(source->signature == MagickSignature);
1660 if (string_info->length == 0)
1662 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1663 (void) CopyMagickMemory(string_info->datum,source->datum,MagickMin(
1664 string_info->length,source->length));
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1672 % S e t S t r i n g I n f o D a t u m %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678 % SetStringInfoDatum() copies bytes from the source string for the length of
1679 % the destination string.
1681 % The format of the SetStringInfoDatum method is:
1683 % void SetStringInfoDatum(StringInfo *string_info,
1684 % const unsigned char *source)
1686 % A description of each parameter follows:
1688 % o string_info: the string info.
1690 % o source: the source string.
1693 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1694 const unsigned char *source)
1696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1697 assert(string_info != (StringInfo *) NULL);
1698 assert(string_info->signature == MagickSignature);
1699 if (string_info->length != 0)
1700 (void) CopyMagickMemory(string_info->datum,source,string_info->length);
1704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % S e t S t r i n g I n f o L e n g t h %
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714 % SetStringInfoLength() set the string length to the specified value.
1716 % The format of the SetStringInfoLength method is:
1718 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1720 % A description of each parameter follows:
1722 % o string_info: the string info.
1724 % o length: the string length.
1727 MagickExport void SetStringInfoLength(StringInfo *string_info,
1728 const size_t length)
1730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1731 assert(string_info != (StringInfo *) NULL);
1732 assert(string_info->signature == MagickSignature);
1733 if (~length < MaxTextExtent)
1734 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1735 string_info->length=length;
1736 if (string_info->datum == (unsigned char *) NULL)
1737 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1738 MaxTextExtent,sizeof(*string_info->datum));
1740 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1741 length+MaxTextExtent,sizeof(*string_info->datum));
1742 if (string_info->datum == (unsigned char *) NULL)
1743 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1751 % S e t S t r i n g I n f o D a t u m %
1755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 % SetStringInfoPath() sets the path associated with the string.
1759 % The format of the SetStringInfoPath method is:
1761 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1763 % A description of each parameter follows:
1765 % o string_info: the string info.
1770 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1773 assert(string_info != (StringInfo *) NULL);
1774 assert(string_info->signature == MagickSignature);
1775 assert(path != (const char *) NULL);
1776 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1784 % S p l i t S t r i n g I n f o %
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790 % SplitStringInfo() splits a string into two and returns it.
1792 % The format of the SplitStringInfo method is:
1794 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1796 % A description of each parameter follows:
1798 % o string_info: the string info.
1801 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1802 const size_t offset)
1807 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1808 assert(string_info != (StringInfo *) NULL);
1809 assert(string_info->signature == MagickSignature);
1810 if (offset > string_info->length)
1811 return((StringInfo *) NULL);
1812 split_info=AcquireStringInfo(offset);
1813 SetStringInfo(split_info,string_info);
1814 (void) CopyMagickMemory(string_info->datum,string_info->datum+offset,
1815 string_info->length-offset+MaxTextExtent);
1816 SetStringInfoLength(string_info,string_info->length-offset);
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1825 % S t r i n g I n f o T o S t r i n g %
1829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1831 % StringInfoToString() converts a string info string to a C string.
1833 % The format of the StringInfoToString method is:
1835 % char *StringInfoToString(const StringInfo *string_info)
1837 % A description of each parameter follows:
1839 % o string_info: the string.
1842 MagickExport char *StringInfoToString(const StringInfo *string_info)
1850 string=(char *) NULL;
1851 length=string_info->length;
1852 if (~length >= MaxTextExtent)
1853 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1854 if (string != (char *) NULL)
1855 (void) CopyMagickString(string,(char *) string_info->datum,
1856 (length+1)*sizeof(*string));
1861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1865 % S t r i n g T o A r g v %
1869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871 % StringToArgv() converts a text string into command line arguments.
1873 % The format of the StringToArgv method is:
1875 % char **StringToArgv(const char *text,int *argc)
1877 % A description of each parameter follows:
1879 % o argv: Method StringToArgv returns the string list unless an error
1880 % occurs, otherwise NULL.
1882 % o text: Specifies the string to segment into a list.
1884 % o argc: This integer pointer returns the number of arguments in the
1888 MagickExport char **StringToArgv(const char *text,int *argc)
1901 if (text == (char *) NULL)
1902 return((char **) NULL);
1904 Determine the number of arguments.
1906 for (p=text; *p != '\0'; )
1908 while (isspace((int) ((unsigned char) *p)) != 0)
1912 for (p++; (*p != '"') && (*p != '\0'); p++) ;
1914 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
1915 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1919 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
1920 if (argv == (char **) NULL)
1921 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
1923 Convert string to an ASCII list.
1925 argv[0]=AcquireString("magick");
1927 for (i=1; i < (ssize_t) *argc; i++)
1929 while (isspace((int) ((unsigned char) *p)) != 0)
1935 for (q++; (*q != '"') && (*q != '\0'); q++) ;
1940 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
1944 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
1946 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1948 if (argv[i] == (char *) NULL)
1950 for (i--; i >= 0; i--)
1951 argv[i]=DestroyString(argv[i]);
1952 argv=(char **) RelinquishMagickMemory(argv);
1953 ThrowFatalException(ResourceLimitFatalError,
1954 "UnableToConvertStringToARGV");
1956 (void) CopyMagickString(argv[i],p,(size_t) (q-p+1));
1958 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
1961 argv[i]=(char *) NULL;
1966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970 % S t r i n g I n f o T o H e x S t r i n g %
1974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1976 % StringInfoToHexString() converts a string info string to a C string.
1978 % The format of the StringInfoToHexString method is:
1980 % char *StringInfoToHexString(const StringInfo *string_info)
1982 % A description of each parameter follows:
1984 % o string_info: the string.
1987 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1992 register const unsigned char
1998 register unsigned char
2007 length=string_info->length;
2008 if (~length < MaxTextExtent)
2009 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2010 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2011 if (string == (char *) NULL)
2012 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2029 p=string_info->datum;
2030 q=(unsigned char *) string;
2031 for (i=0; i < (ssize_t) string_info->length; i++)
2033 *q++=hex_digits[(*p >> 4) & 0x0f];
2034 *q++=hex_digits[*p & 0x0f];
2042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2046 % S t r i n g T o k e n %
2050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2052 % StringToken() extracts a token a from the string.
2054 % The format of the StringToken method is:
2056 % char *StringToken(const char *delimiters,char **string)
2058 % A description of each parameter follows:
2060 % o delimiters: one or more delimiters.
2062 % o string: return the first token in the string. If none is found, return
2066 MagickExport char *StringToken(const char *delimiters,char **string)
2082 if (p == (char *) NULL)
2083 return((char *) NULL);
2100 } while (d != '\0');
2105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2109 % S t r i n g T o L i s t %
2113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115 % StringToList() converts a text string into a list by segmenting the text
2116 % string at each carriage return discovered. The list is converted to HEX
2117 % characters if any control characters are discovered within the text string.
2119 % The format of the StringToList method is:
2121 % char **StringToList(const char *text)
2123 % A description of each parameter follows:
2125 % o list: Method StringToList returns the string list unless an error
2126 % occurs, otherwise NULL.
2128 % o text: Specifies the string to segment into a list.
2131 MagickExport char **StringToList(const char *text)
2145 if (text == (char *) NULL)
2146 return((char **) NULL);
2147 for (p=text; *p != '\0'; p++)
2148 if (((int) ((unsigned char) *p) < 32) &&
2149 (isspace((int) ((unsigned char) *p)) == 0))
2157 Convert string to an ASCII list.
2160 for (p=text; *p != '\0'; p++)
2163 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2165 if (textlist == (char **) NULL)
2166 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2168 for (i=0; i < (ssize_t) lines; i++)
2170 for (q=p; *q != '\0'; q++)
2171 if ((*q == '\r') || (*q == '\n'))
2173 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2175 if (textlist[i] == (char *) NULL)
2176 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2177 (void) CopyMagickString(textlist[i],p,(size_t) (q-p+1));
2186 hex_string[MaxTextExtent];
2195 Convert string to a HEX list.
2197 lines=(size_t) (strlen(text)/0x14)+1;
2198 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2200 if (textlist == (char **) NULL)
2201 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2203 for (i=0; i < (ssize_t) lines; i++)
2205 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2207 if (textlist[i] == (char *) NULL)
2208 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2209 (void) FormatMagickString(textlist[i],MaxTextExtent,"0x%08lx: ",
2211 q=textlist[i]+strlen(textlist[i]);
2212 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2214 (void) FormatMagickString(hex_string,MaxTextExtent,"%02x",*(p+j));
2215 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2217 if ((j % 0x04) == 0)
2220 for ( ; j <= 0x14; j++)
2224 if ((j % 0x04) == 0)
2228 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2230 if (isprint((int) ((unsigned char) *p)) != 0)
2239 textlist[i]=(char *) NULL;
2244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2248 % S t r i n g T o S t r i n g I n f o %
2252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2254 % StringToStringInfo() returns the contents of a file as a string.
2256 % The format of the StringToStringInfo method is:
2258 % StringInfo *StringToStringInfo(const char *string)
2260 % A description of each parameter follows:
2262 % o string: The string.
2265 MagickExport StringInfo *StringToStringInfo(const char *string)
2270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2271 assert(string != (const char *) NULL);
2272 string_info=AcquireStringInfo(strlen(string)+1);
2273 SetStringInfoDatum(string_info,(const unsigned char *) string);
2274 return(string_info);
2278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2282 % S t r i p S t r i n g %
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288 % StripString() strips any whitespace or quotes from the beginning and end of
2289 % a string of characters.
2291 % The format of the StripString method is:
2293 % void StripString(char *message)
2295 % A description of each parameter follows:
2297 % o message: Specifies an array of characters.
2300 MagickExport void StripString(char *message)
2309 assert(message != (char *) NULL);
2310 if (*message == '\0')
2312 length=strlen(message);
2314 while (isspace((int) ((unsigned char) *p)) != 0)
2316 if ((*p == '\'') || (*p == '"'))
2319 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2322 if ((*q == '\'') || (*q == '"'))
2324 (void) CopyMagickMemory(message,p,(size_t) (q-p+1));
2325 message[q-p+1]='\0';
2326 for (p=message; *p != '\0'; p++)
2332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2336 % S u b s t i t u t e S t r i n g %
2340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2342 % SubstituteString() performs string substitution on a string, replacing the
2343 % string with the substituted version. Buffer must be allocated from the heap.
2344 % If the string is matched and status, MagickTrue is returned otherwise
2347 % The format of the SubstituteString method is:
2349 % MagickBooleanType SubstituteString(char **string,const char *search,
2350 % const char *replace)
2352 % A description of each parameter follows:
2354 % o string: the string to perform replacements on; replaced with new
2355 % allocation if a replacement is made.
2357 % o search: search for this string.
2359 % o replace: replace any matches with this string.
2362 MagickExport MagickBooleanType SubstituteString(char **string,
2363 const char *search,const char *replace)
2382 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2384 if (search_extent == 0)
2385 search_extent=strlen(search);
2386 if (strncmp(p,search,search_extent) != 0)
2392 if (replace_extent == 0)
2393 replace_extent=strlen(replace);
2394 if (replace_extent > search_extent)
2397 Make room for the replacement string.
2399 offset=(ssize_t) (p-(*string));
2400 extent=strlen(*string)+replace_extent-search_extent+1;
2401 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2403 if (*string == (char *) NULL)
2404 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2410 if (search_extent != replace_extent)
2411 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2412 strlen(p+search_extent)+1);
2413 (void) CopyMagickMemory(p,replace,replace_extent);
2414 p+=replace_extent-1;