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-2012 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"
55 #include "MagickCore/utility-private.h"
60 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
61 static const unsigned char
64 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
65 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
66 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
67 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
68 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
69 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
70 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
71 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
72 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
73 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
74 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
75 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
76 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
77 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
78 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
79 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
80 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
81 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
82 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
83 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
84 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
85 0xfc, 0xfd, 0xfe, 0xff,
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % A c q u i r e S t r i n g %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % AcquireString() allocates memory for a string and copies the source string
101 % to that memory location (and returns it).
103 % The format of the AcquireString method is:
105 % char *AcquireString(const char *source)
107 % A description of each parameter follows:
109 % o source: A character string.
112 MagickExport char *AcquireString(const char *source)
121 if (source != (char *) NULL)
122 length+=strlen(source);
123 if (~length < MaxTextExtent)
124 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
125 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
126 sizeof(*destination));
127 if (destination == (char *) NULL)
128 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
130 if (source != (char *) NULL)
131 (void) memcpy(destination,source,length*sizeof(*destination));
132 destination[length]='\0';
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % A c q u i r e S t r i n g I n f o %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % AcquireStringInfo() allocates the StringInfo structure.
149 % The format of the AcquireStringInfo method is:
151 % StringInfo *AcquireStringInfo(const size_t length)
153 % A description of each parameter follows:
155 % o length: the string length.
158 MagickExport StringInfo *AcquireStringInfo(const size_t length)
163 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
164 if (string_info == (StringInfo *) NULL)
165 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
166 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
167 string_info->signature=MagickSignature;
168 string_info->length=length;
169 if (string_info->length != 0)
171 string_info->datum=(unsigned char *) NULL;
172 if (~string_info->length >= (MaxTextExtent-1))
173 string_info->datum=(unsigned char *) AcquireQuantumMemory(
174 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
175 if (string_info->datum == (unsigned char *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % B l o b T o S t r i n g I n f o %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % BlobToStringInfo() returns the contents of a blob as a string.
194 % The format of the BlobToStringInfo method is:
196 % StringInfo *BlobToStringInfo(const void *blob,const size_t length)
198 % A description of each parameter follows:
202 % o length: the length of the blob.
205 MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
210 string_info=AcquireStringInfo(0);
211 string_info->length=length;
212 if (~string_info->length >= (MaxTextExtent-1))
213 string_info->datum=(unsigned char *) AcquireQuantumMemory(
214 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
215 if (string_info->datum == (unsigned char *) NULL)
217 string_info=DestroyStringInfo(string_info);
218 return((StringInfo *) NULL);
220 if (blob != (const void *) NULL)
221 (void) memcpy(string_info->datum,blob,length);
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 % C l o n e S t r i n g %
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % CloneString() allocates memory for the destination string and copies
237 % the source string to that memory location.
239 % The format of the CloneString method is:
241 % char *CloneString(char **destination,const char *source)
243 % A description of each parameter follows:
245 % o destination: A pointer to a character string.
247 % o source: A character string.
250 MagickExport char *CloneString(char **destination,const char *source)
255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
256 assert(destination != (char **) NULL);
257 if (source == (const char *) NULL)
259 if (*destination != (char *) NULL)
260 *destination=DestroyString(*destination);
261 return(*destination);
263 if (*destination == (char *) NULL)
265 *destination=AcquireString(source);
266 return(*destination);
268 length=strlen(source);
269 if (~length < MaxTextExtent)
270 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
271 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
272 sizeof(**destination));
273 if (*destination == (char *) NULL)
274 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
276 (void) memcpy(*destination,source,length*sizeof(**destination));
277 (*destination)[length]='\0';
278 return(*destination);
282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % C l o n e S t r i n g I n f o %
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % CloneStringInfo() clones a copy of the StringInfo structure.
294 % The format of the CloneStringInfo method is:
296 % StringInfo *CloneStringInfo(const StringInfo *string_info)
298 % A description of each parameter follows:
300 % o string_info: the string info.
303 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
309 assert(string_info != (StringInfo *) NULL);
310 assert(string_info->signature == MagickSignature);
311 clone_info=AcquireStringInfo(string_info->length);
312 if (string_info->length != 0)
313 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % C o m p a r e S t r i n g I n f o %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % CompareStringInfo() compares the two datums target and source. It returns
329 % an integer less than, equal to, or greater than zero if target is found,
330 % respectively, to be less than, to match, or be greater than source.
332 % The format of the CompareStringInfo method is:
334 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
336 % A description of each parameter follows:
338 % o target: the target string.
340 % o source: the source string.
344 static inline size_t MagickMin(const size_t x,const size_t y)
351 MagickExport int CompareStringInfo(const StringInfo *target,
352 const StringInfo *source)
357 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
358 assert(target != (StringInfo *) NULL);
359 assert(target->signature == MagickSignature);
360 assert(source != (StringInfo *) NULL);
361 assert(source->signature == MagickSignature);
362 status=memcmp(target->datum,source->datum,MagickMin(target->length,
366 if (target->length == source->length)
368 return(target->length < source->length ? -1 : 1);
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % C o n c a t e n a t e M a g i c k S t r i n g %
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 % ConcatenateMagickString() concatenates the source string to the destination
383 % string. The destination buffer is always null-terminated even if the
384 % string must be truncated.
386 % The format of the ConcatenateMagickString method is:
388 % size_t ConcatenateMagickString(char *destination,const char *source,
389 % const size_t length)
391 % A description of each parameter follows:
393 % o destination: the destination string.
395 % o source: the source string.
397 % o length: the length of the destination string.
400 MagickExport size_t ConcatenateMagickString(char *destination,
401 const char *source,const size_t length)
415 assert(destination != (char *) NULL);
416 assert(source != (const char *) NULL);
421 while ((i-- != 0) && (*q != '\0'))
423 count=(size_t) (q-destination);
426 return(count+strlen(p));
437 return(count+(p-source));
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 % C o n c a t e n a t e S t r i n g %
449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 % ConcatenateString() appends a copy of string source, including the
452 % terminating null character, to the end of string destination.
454 % The format of the ConcatenateString method is:
456 % MagickBooleanType ConcatenateString(char **destination,
457 % const char *source)
459 % A description of each parameter follows:
461 % o destination: A pointer to a character string.
463 % o source: A character string.
466 MagickExport MagickBooleanType ConcatenateString(char **destination,
474 assert(destination != (char **) NULL);
475 if (source == (const char *) NULL)
477 if (*destination == (char *) NULL)
479 *destination=AcquireString(source);
482 destination_length=strlen(*destination);
483 source_length=strlen(source);
484 length=destination_length;
485 if (~length < source_length)
486 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
487 length+=source_length;
488 if (~length < MaxTextExtent)
489 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
490 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
491 sizeof(**destination));
492 if (*destination == (char *) NULL)
493 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
494 if (source_length != 0)
495 (void) memcpy((*destination)+destination_length,source,source_length);
496 (*destination)[length]='\0';
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % C o n c a t e n a t e S t r i n g I n f o %
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511 % ConcatenateStringInfo() concatenates the source string to the destination
514 % The format of the ConcatenateStringInfo method is:
516 % void ConcatenateStringInfo(StringInfo *string_info,
517 % const StringInfo *source)
519 % A description of each parameter follows:
521 % o string_info: the string info.
523 % o source: the source string.
526 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
527 const StringInfo *source)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
533 assert(string_info != (StringInfo *) NULL);
534 assert(string_info->signature == MagickSignature);
535 assert(source != (const StringInfo *) NULL);
536 length=string_info->length;
537 if (~length < source->length)
538 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
539 SetStringInfoLength(string_info,length+source->length);
540 (void) memcpy(string_info->datum+length,source->datum,source->length);
544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % 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 %
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % ConfigureFileToStringInfo() returns the contents of a configure file as a
557 % The format of the ConfigureFileToStringInfo method is:
559 % StringInfo *ConfigureFileToStringInfo(const char *filename)
560 % ExceptionInfo *exception)
562 % A description of each parameter follows:
564 % o filename: the filename.
567 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
587 assert(filename != (const char *) NULL);
588 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
590 return((StringInfo *) NULL);
591 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
592 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
595 return((StringInfo *) NULL);
597 length=(size_t) offset;
598 string=(char *) NULL;
599 if (~length >= (MaxTextExtent-1))
600 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
601 if (string == (char *) NULL)
604 return((StringInfo *) NULL);
606 map=MapBlob(file,ReadMode,0,length);
607 if (map != (void *) NULL)
609 (void) memcpy(string,map,length);
610 (void) UnmapBlob(map,length);
620 (void) lseek(file,0,SEEK_SET);
621 for (i=0; i < length; i+=count)
623 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
635 string=DestroyString(string);
636 return((StringInfo *) NULL);
641 string_info=AcquireStringInfo(0);
642 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
643 string_info->length=length;
644 string_info->datum=(unsigned char *) string;
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 % C o n s t a n t S t r i n g %
657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 % ConstantString() allocates memory for a string and copies the source string
660 % to that memory location (and returns it). Use it for strings that you do
661 % do not expect to change over its lifetime.
663 % The format of the ConstantString method is:
665 % char *ConstantString(const char *source)
667 % A description of each parameter follows:
669 % o source: A character string.
672 MagickExport char *ConstantString(const char *source)
681 if (source != (char *) NULL)
682 length+=strlen(source);
683 destination=(char *) NULL;
685 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
686 if (destination == (char *) NULL)
687 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
689 if (source != (char *) NULL)
690 (void) memcpy(destination,source,length*sizeof(*destination));
691 destination[length]='\0';
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % C o p y M a g i c k S t r i n g %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % CopyMagickString() copies the source string to the destination string. The
707 % destination buffer is always null-terminated even if the string must be
708 % truncated. The return value is the minimum of the source string length
709 % or the length parameter.
711 % The format of the CopyMagickString method is:
713 % size_t CopyMagickString(const char *destination,char *source,
714 % const size_t length)
716 % A description of each parameter follows:
718 % o destination: the destination string.
720 % o source: the source string.
722 % o length: the length of the destination string.
725 MagickExport size_t CopyMagickString(char *destination,const char *source,
737 if (source == (const char *) NULL)
741 for (n=length; n > 4; n-=4)
745 return((size_t) (p-source-1));
749 return((size_t) (p-source-1));
753 return((size_t) (p-source-1));
757 return((size_t) (p-source-1));
761 for (n--; n != 0; n--)
765 return((size_t) (p-source-1));
770 return((size_t) (p-source-1));
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 % D e s t r o y S t r i n g %
782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % DestroyString() destroys memory associated with a string.
786 % The format of the DestroyString method is:
788 % char *DestroyString(char *string)
790 % A description of each parameter follows:
792 % o string: the string.
795 MagickExport char *DestroyString(char *string)
797 return((char *) RelinquishMagickMemory(string));
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
805 % D e s t r o y S t r i n g I n f o %
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
813 % The format of the DestroyStringInfo method is:
815 % StringInfo *DestroyStringInfo(StringInfo *string_info)
817 % A description of each parameter follows:
819 % o string_info: the string info.
822 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
824 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
825 assert(string_info != (StringInfo *) NULL);
826 assert(string_info->signature == MagickSignature);
827 if (string_info->datum != (unsigned char *) NULL)
828 string_info->datum=(unsigned char *) RelinquishMagickMemory(
830 string_info->signature=(~MagickSignature);
831 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840 % D e s t r o y S t r i n g L i s t %
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 % DestroyStringList() zeros memory associated with a string list.
848 % The format of the DestroyStringList method is:
850 % char **DestroyStringList(char **list)
852 % A description of each parameter follows:
854 % o list: the string list.
857 MagickExport char **DestroyStringList(char **list)
862 assert(list != (char **) NULL);
863 for (i=0; list[i] != (char *) NULL; i++)
864 list[i]=DestroyString(list[i]);
865 list=(char **) RelinquishMagickMemory(list);
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % E s c a p e S t r i n g %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % EscapeString() allocates memory for a backslash-escaped version of a
881 % source text string, copies the escaped version of the text to that
882 % memory location while adding backslash characters, and returns the
885 % The format of the EscapeString method is:
887 % char *EscapeString(const char *source,const char escape)
889 % A description of each parameter follows:
891 % o allocate_string: Method EscapeString returns the escaped string.
893 % o source: A character string.
895 % o escape: the quoted string termination character to escape (e.g. '"').
898 MagickExport char *EscapeString(const char *source,const char escape)
912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
913 assert(source != (const char *) NULL);
914 length=strlen(source);
915 for (p=source; *p != '\0'; p++)
916 if ((*p == '\\') || (*p == escape))
919 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
922 destination=(char *) NULL;
923 if (~length >= (MaxTextExtent-1))
924 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
925 sizeof(*destination));
926 if (destination == (char *) NULL)
927 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
929 if (source != (char *) NULL)
932 for (p=source; *p != '\0'; p++)
934 if ((*p == '\\') || (*p == escape))
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 % F i l e T o S t r i n g %
952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 % FileToString() returns the contents of a file as a string.
956 % The format of the FileToString method is:
958 % char *FileToString(const char *filename,const size_t extent,
959 % ExceptionInfo *exception)
961 % A description of each parameter follows:
963 % o filename: the filename.
965 % o extent: Maximum length of the string.
967 % o exception: return any errors or warnings in this structure.
970 MagickExport char *FileToString(const char *filename,const size_t extent,
971 ExceptionInfo *exception)
976 assert(filename != (const char *) NULL);
977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
978 assert(exception != (ExceptionInfo *) NULL);
979 return((char *) FileToBlob(filename,extent,&length,exception));
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % F i l e T o S t r i n g I n f o %
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % FileToStringInfo() returns the contents of a file as a string.
995 % The format of the FileToStringInfo method is:
997 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
998 % ExceptionInfo *exception)
1000 % A description of each parameter follows:
1002 % o filename: the filename.
1004 % o extent: Maximum length of the string.
1006 % o exception: return any errors or warnings in this structure.
1009 MagickExport StringInfo *FileToStringInfo(const char *filename,
1010 const size_t extent,ExceptionInfo *exception)
1015 assert(filename != (const char *) NULL);
1016 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1017 assert(exception != (ExceptionInfo *) NULL);
1018 string_info=AcquireStringInfo(0);
1019 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
1020 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
1021 if (string_info->datum == (unsigned char *) NULL)
1023 string_info=DestroyStringInfo(string_info);
1024 return((StringInfo *) NULL);
1026 return(string_info);
1030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 % F o r m a t M a g i c k S i z e %
1038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040 % FormatMagickSize() converts a size to a human readable format, for example,
1041 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1044 % The format of the FormatMagickSize method is:
1046 % ssize_t FormatMagickSize(const MagickSizeType size,char *format)
1048 % A description of each parameter follows:
1050 % o size: convert this size to a human readable format.
1052 % o bi: use power of two rather than power of ten.
1054 % o format: human readable format.
1057 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1058 const MagickBooleanType bi,char *format)
1077 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1079 *traditional_units[] =
1081 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1085 units=traditional_units;
1086 if (bi != MagickFalse)
1091 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1092 length=(double) ((MagickOffsetType) size);
1094 length=(double) size;
1096 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1098 for (j=2; j < 12; j++)
1100 count=FormatLocaleString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
1102 if (strchr(format,'+') == (char *) NULL)
1109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1113 % F o r m a t M a g i c k T i m e %
1117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1119 % FormatMagickTime() returns the specified time in the Internet date/time
1120 % format and the length of the timestamp.
1122 % The format of the FormatMagickTime method is:
1124 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1127 % A description of each parameter follows.
1129 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1130 % measured in seconds.
1132 % o length: the maximum length of the string.
1134 % o timestamp: Return the Internet date/time here.
1137 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1150 assert(timestamp != (char *) NULL);
1151 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1152 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1153 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1154 (void) localtime_r(&time,&local_time);
1160 my_time=localtime(&time);
1161 if (my_time != (struct tm *) NULL)
1162 (void) memcpy(&local_time,my_time,sizeof(local_time));
1165 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1166 (void) gmtime_r(&time,&gm_time);
1172 my_time=gmtime(&time);
1173 if (my_time != (struct tm *) NULL)
1174 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1177 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1178 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1179 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1180 (local_time.tm_yday-gm_time.tm_yday)));
1181 count=FormatLocaleString(timestamp,length,
1182 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1183 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1184 local_time.tm_min,local_time.tm_sec,(long) timezone);
1189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193 % G e t E n v i r o n m e n t V a l u e %
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1199 % GetEnvironmentValue() returns the environment string that matches the
1202 % The format of the GetEnvironmentValue method is:
1204 % char *GetEnvironmentValue(const char *name)
1206 % A description of each parameter follows:
1208 % o name: the environment name.
1211 MagickExport char *GetEnvironmentValue(const char *name)
1216 environment=getenv(name);
1217 if (environment == (const char *) NULL)
1218 return((char *) NULL);
1219 return(ConstantString(environment));
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227 % G e t S t r i n g I n f o D a t u m %
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 % GetStringInfoDatum() returns the datum associated with the string.
1235 % The format of the GetStringInfoDatum method is:
1237 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1239 % A description of each parameter follows:
1241 % o string_info: the string info.
1244 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1246 assert(string_info != (StringInfo *) NULL);
1247 assert(string_info->signature == MagickSignature);
1248 return(string_info->datum);
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % G e t S t r i n g I n f o L e n g t h %
1260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % GetStringInfoLength() returns the string length.
1264 % The format of the GetStringInfoLength method is:
1266 % size_t GetStringInfoLength(const StringInfo *string_info)
1268 % A description of each parameter follows:
1270 % o string_info: the string info.
1273 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1275 assert(string_info != (StringInfo *) NULL);
1276 assert(string_info->signature == MagickSignature);
1277 return(string_info->length);
1281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1285 % G e t S t r i n g I n f o P a t h %
1289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1291 % GetStringInfoPath() returns the path associated with the string.
1293 % The format of the GetStringInfoPath method is:
1295 % const char *GetStringInfoPath(const StringInfo *string_info)
1297 % A description of each parameter follows:
1299 % o string_info: the string info.
1302 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1304 assert(string_info != (StringInfo *) NULL);
1305 assert(string_info->signature == MagickSignature);
1306 return(string_info->path);
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314 + I n t e r p r e t S i P r e f i x V a l u e %
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % InterpretSiPrefixValue() converts the initial portion of the string to a
1321 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1324 % The format of the InterpretSiPrefixValue method is:
1326 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1328 % A description of each parameter follows:
1330 % o value: the string value.
1332 % o sentinal: if sentinal is not NULL, return a pointer to the character
1333 % after the last character used in the conversion.
1336 MagickExport double InterpretSiPrefixValue(const char *restrict string,
1337 char **restrict sentinal)
1345 value=InterpretLocaleValue(string,&q);
1348 if ((*q >= 'E') && (*q <= 'z'))
1353 switch ((int) ((unsigned char) *q))
1355 case 'y': e=(-24.0); break;
1356 case 'z': e=(-21.0); break;
1357 case 'a': e=(-18.0); break;
1358 case 'f': e=(-15.0); break;
1359 case 'p': e=(-12.0); break;
1360 case 'n': e=(-9.0); break;
1361 case 'u': e=(-6.0); break;
1362 case 'm': e=(-3.0); break;
1363 case 'c': e=(-2.0); break;
1364 case 'd': e=(-1.0); break;
1365 case 'h': e=2.0; break;
1366 case 'k': e=3.0; break;
1367 case 'K': e=3.0; break;
1368 case 'M': e=6.0; break;
1369 case 'G': e=9.0; break;
1370 case 'T': e=12.0; break;
1371 case 'P': e=15.0; break;
1372 case 'E': e=18.0; break;
1373 case 'Z': e=21.0; break;
1374 case 'Y': e=24.0; break;
1375 default: e=0.0; break;
1377 if (e >= MagickEpsilon)
1381 value*=pow(2.0,e/0.3);
1394 if (sentinal != (char **) NULL)
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % L o c a l e C o m p a r e %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % LocaleCompare() performs a case-insensitive comparison of two strings
1411 % byte-by-byte, according to the ordering of the current locale encoding.
1412 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1413 % if the string pointed to by p is greater than, equal to, or less than the
1414 % string pointed to by q respectively. The sign of a non-zero return value
1415 % is determined by the sign of the difference between the values of the first
1416 % pair of bytes that differ in the strings being compared.
1418 % The format of the LocaleCompare method is:
1420 % int LocaleCompare(const char *p,const char *q)
1422 % A description of each parameter follows:
1424 % o p: A pointer to a character string.
1426 % o q: A pointer to a character string to compare to p.
1429 MagickExport int LocaleCompare(const char *p,const char *q)
1431 if ((p == (char *) NULL) && (q == (char *) NULL))
1433 if (p == (char *) NULL)
1435 if (q == (char *) NULL)
1437 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1438 return(strcasecmp(p,q));
1447 c=(int) *((unsigned char *) p);
1448 d=(int) *((unsigned char *) q);
1449 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1454 return(AsciiMap[c]-(int) AsciiMap[d]);
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % L o c a l e L o w e r %
1468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 % LocaleLower() transforms all of the characters in the supplied
1471 % null-terminated string, changing all uppercase letters to lowercase.
1473 % The format of the LocaleLower method is:
1475 % void LocaleLower(char *string)
1477 % A description of each parameter follows:
1479 % o string: A pointer to the string to convert to lower-case Locale.
1482 MagickExport void LocaleLower(char *string)
1487 assert(string != (char *) NULL);
1488 for (q=string; *q != '\0'; q++)
1489 *q=(char) tolower((int) *q);
1493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % L o c a l e N C o m p a r e %
1501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % LocaleNCompare() performs a case-insensitive comparison of two
1504 % strings byte-by-byte, according to the ordering of the current locale
1505 % encoding. LocaleNCompare returns an integer greater than, equal to, or
1506 % less than 0, if the string pointed to by p is greater than, equal to, or
1507 % less than the string pointed to by q respectively. The sign of a non-zero
1508 % return value is determined by the sign of the difference between the
1509 % values of the first pair of bytes that differ in the strings being
1510 % compared. The LocaleNCompare method makes the same comparison as
1511 % LocaleCompare but looks at a maximum of n bytes. Bytes following a
1512 % null byte are not compared.
1514 % The format of the LocaleNCompare method is:
1516 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1518 % A description of each parameter follows:
1520 % o p: A pointer to a character string.
1522 % o q: A pointer to a character string to compare to p.
1524 % o length: the number of characters to compare in strings p and q.
1527 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1529 if ((p == (char *) NULL) && (q == (char *) NULL))
1531 if (p == (char *) NULL)
1533 if (q == (char *) NULL)
1535 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1536 return(strncasecmp(p,q,length));
1546 for (i=length; i != 0; i--)
1548 c=(int) *((unsigned char *) p);
1549 d=(int) *((unsigned char *) q);
1550 if (AsciiMap[c] != AsciiMap[d])
1551 return(AsciiMap[c]-(int) AsciiMap[d]);
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 % L o c a l e U p p e r %
1571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1573 % LocaleUpper() transforms all of the characters in the supplied
1574 % null-terminated string, changing all lowercase letters to uppercase.
1576 % The format of the LocaleUpper method is:
1578 % void LocaleUpper(char *string)
1580 % A description of each parameter follows:
1582 % o string: A pointer to the string to convert to upper-case Locale.
1585 MagickExport void LocaleUpper(char *string)
1590 assert(string != (char *) NULL);
1591 for (q=string; *q != '\0'; q++)
1592 *q=(char) toupper((int) *q);
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 % P r i n t S t r i n g I n f o %
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % PrintStringInfo() prints the string.
1608 % The format of the PrintStringInfo method is:
1610 % void PrintStringInfo(FILE *file,const char *id,
1611 % const StringInfo *string_info)
1613 % A description of each parameter follows:
1615 % o file: the file, typically stdout.
1617 % o id: the string id.
1619 % o string_info: the string info.
1622 MagickExport void PrintStringInfo(FILE *file,const char *id,
1623 const StringInfo *string_info)
1632 assert(id != (const char *) NULL);
1633 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1634 assert(string_info != (StringInfo *) NULL);
1635 assert(string_info->signature == MagickSignature);
1636 p=(char *) string_info->datum;
1637 for (i=0; i < string_info->length; i++)
1639 if (((int) ((unsigned char) *p) < 32) &&
1640 (isspace((int) ((unsigned char) *p)) == 0))
1644 if (i == string_info->length)
1646 (void) fputs((char *) string_info->datum,file);
1647 (void) fputc('\n',file);
1651 Convert string to a HEX list.
1653 p=(char *) string_info->datum;
1654 for (i=0; i < string_info->length; i+=0x14)
1656 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
1657 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1659 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1660 if ((j % 0x04) == 0)
1661 (void) fputc(' ',file);
1663 for ( ; j <= 0x14; j++)
1665 (void) fputc(' ',file);
1666 (void) fputc(' ',file);
1667 if ((j % 0x04) == 0)
1668 (void) fputc(' ',file);
1670 (void) fputc(' ',file);
1671 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1673 if (isprint((int) ((unsigned char) *p)) != 0)
1674 (void) fputc(*p,file);
1676 (void) fputc('-',file);
1679 (void) fputc('\n',file);
1684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1688 % R e s e t S t r i n g I n f o %
1692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1694 % ResetStringInfo() reset the string to all null bytes.
1696 % The format of the ResetStringInfo method is:
1698 % void ResetStringInfo(StringInfo *string_info)
1700 % A description of each parameter follows:
1702 % o string_info: the string info.
1705 MagickExport void ResetStringInfo(StringInfo *string_info)
1707 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1708 assert(string_info != (StringInfo *) NULL);
1709 assert(string_info->signature == MagickSignature);
1710 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1718 % S e t S t r i n g I n f o %
1722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724 % SetStringInfo() copies the source string to the destination string.
1726 % The format of the SetStringInfo method is:
1728 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1730 % A description of each parameter follows:
1732 % o string_info: the string info.
1734 % o source: the source string.
1737 MagickExport void SetStringInfo(StringInfo *string_info,
1738 const StringInfo *source)
1740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1741 assert(string_info != (StringInfo *) NULL);
1742 assert(string_info->signature == MagickSignature);
1743 assert(source != (StringInfo *) NULL);
1744 assert(source->signature == MagickSignature);
1745 if (string_info->length == 0)
1747 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1748 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 % S e t S t r i n g I n f o D a t u m %
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763 % SetStringInfoDatum() copies bytes from the source string for the length of
1764 % the destination string.
1766 % The format of the SetStringInfoDatum method is:
1768 % void SetStringInfoDatum(StringInfo *string_info,
1769 % const unsigned char *source)
1771 % A description of each parameter follows:
1773 % o string_info: the string info.
1775 % o source: the source string.
1778 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1779 const unsigned char *source)
1781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1782 assert(string_info != (StringInfo *) NULL);
1783 assert(string_info->signature == MagickSignature);
1784 if (string_info->length != 0)
1785 (void) memcpy(string_info->datum,source,string_info->length);
1789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1793 % S e t S t r i n g I n f o L e n g t h %
1797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799 % SetStringInfoLength() set the string length to the specified value.
1801 % The format of the SetStringInfoLength method is:
1803 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1805 % A description of each parameter follows:
1807 % o string_info: the string info.
1809 % o length: the string length.
1812 MagickExport void SetStringInfoLength(StringInfo *string_info,
1813 const size_t length)
1815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1816 assert(string_info != (StringInfo *) NULL);
1817 assert(string_info->signature == MagickSignature);
1818 if (~length < MaxTextExtent)
1819 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1820 string_info->length=length;
1821 if (string_info->datum == (unsigned char *) NULL)
1822 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1823 MaxTextExtent,sizeof(*string_info->datum));
1825 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1826 length+MaxTextExtent,sizeof(*string_info->datum));
1827 if (string_info->datum == (unsigned char *) NULL)
1828 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836 % S e t S t r i n g I n f o D a t u m %
1840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1842 % SetStringInfoPath() sets the path associated with the string.
1844 % The format of the SetStringInfoPath method is:
1846 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1848 % A description of each parameter follows:
1850 % o string_info: the string info.
1855 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1858 assert(string_info != (StringInfo *) NULL);
1859 assert(string_info->signature == MagickSignature);
1860 assert(path != (const char *) NULL);
1861 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1869 % S p l i t S t r i n g I n f o %
1873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1875 % SplitStringInfo() splits a string into two and returns it.
1877 % The format of the SplitStringInfo method is:
1879 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1881 % A description of each parameter follows:
1883 % o string_info: the string info.
1886 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1887 const size_t offset)
1892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1893 assert(string_info != (StringInfo *) NULL);
1894 assert(string_info->signature == MagickSignature);
1895 if (offset > string_info->length)
1896 return((StringInfo *) NULL);
1897 split_info=AcquireStringInfo(offset);
1898 SetStringInfo(split_info,string_info);
1899 (void) memmove(string_info->datum,string_info->datum+offset,
1900 string_info->length-offset+MaxTextExtent);
1901 SetStringInfoLength(string_info,string_info->length-offset);
1906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1910 % S t r i n g I n f o T o S t r i n g %
1914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1916 % StringInfoToString() converts a string info string to a C string.
1918 % The format of the StringInfoToString method is:
1920 % char *StringInfoToString(const StringInfo *string_info)
1922 % A description of each parameter follows:
1924 % o string_info: the string.
1927 MagickExport char *StringInfoToString(const StringInfo *string_info)
1935 string=(char *) NULL;
1936 length=string_info->length;
1937 if (~length >= (MaxTextExtent-1))
1938 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1939 if (string == (char *) NULL)
1940 return((char *) NULL);
1941 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1942 string[length]='\0';
1947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951 % S t r i n g T o A r g v %
1955 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957 % StringToArgv() converts a text string into command line arguments.
1959 % The format of the StringToArgv method is:
1961 % char **StringToArgv(const char *text,int *argc)
1963 % A description of each parameter follows:
1965 % o argv: Method StringToArgv returns the string list unless an error
1966 % occurs, otherwise NULL.
1968 % o text: Specifies the string to segment into a list.
1970 % o argc: This integer pointer returns the number of arguments in the
1974 MagickExport char **StringToArgv(const char *text,int *argc)
1987 if (text == (char *) NULL)
1988 return((char **) NULL);
1990 Determine the number of arguments.
1992 for (p=text; *p != '\0'; )
1994 while (isspace((int) ((unsigned char) *p)) != 0)
2000 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2002 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2003 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2007 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2008 if (argv == (char **) NULL)
2009 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2011 Convert string to an ASCII list.
2013 argv[0]=AcquireString("magick");
2015 for (i=1; i < (ssize_t) *argc; i++)
2017 while (isspace((int) ((unsigned char) *p)) != 0)
2023 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2029 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2032 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2034 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2036 if (argv[i] == (char *) NULL)
2038 for (i--; i >= 0; i--)
2039 argv[i]=DestroyString(argv[i]);
2040 argv=(char **) RelinquishMagickMemory(argv);
2041 ThrowFatalException(ResourceLimitFatalError,
2042 "UnableToConvertStringToARGV");
2044 (void) memcpy(argv[i],p,(size_t) (q-p));
2047 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2050 argv[i]=(char *) NULL;
2055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2059 % S t r i n g I n f o T o H e x S t r i n g %
2063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065 % StringInfoToHexString() converts a string info string to a C string.
2067 % The format of the StringInfoToHexString method is:
2069 % char *StringInfoToHexString(const StringInfo *string_info)
2071 % A description of each parameter follows:
2073 % o string_info: the string.
2076 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
2081 register const unsigned char
2087 register unsigned char
2096 length=string_info->length;
2097 if (~length < MaxTextExtent)
2098 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2099 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2100 if (string == (char *) NULL)
2101 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2118 p=string_info->datum;
2119 q=(unsigned char *) string;
2120 for (i=0; i < (ssize_t) string_info->length; i++)
2122 *q++=hex_digits[(*p >> 4) & 0x0f];
2123 *q++=hex_digits[*p & 0x0f];
2131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2135 % S t r i n g T o k e n %
2139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2141 % StringToken() extracts a token a from the string.
2143 % The format of the StringToken method is:
2145 % char *StringToken(const char *delimiters,char **string)
2147 % A description of each parameter follows:
2149 % o delimiters: one or more delimiters.
2151 % o string: return the first token in the string. If none is found, return
2155 MagickExport char *StringToken(const char *delimiters,char **string)
2171 if (p == (char *) NULL)
2172 return((char *) NULL);
2189 } while (d != '\0');
2194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2198 % S t r i n g T o L i s t %
2202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2204 % StringToList() converts a text string into a list by segmenting the text
2205 % string at each carriage return discovered. The list is converted to HEX
2206 % characters if any control characters are discovered within the text string.
2208 % The format of the StringToList method is:
2210 % char **StringToList(const char *text)
2212 % A description of each parameter follows:
2214 % o text: Specifies the string to segment into a list.
2217 MagickExport char **StringToList(const char *text)
2231 if (text == (char *) NULL)
2232 return((char **) NULL);
2233 for (p=text; *p != '\0'; p++)
2234 if (((int) ((unsigned char) *p) < 32) &&
2235 (isspace((int) ((unsigned char) *p)) == 0))
2243 Convert string to an ASCII list.
2246 for (p=text; *p != '\0'; p++)
2249 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2251 if (textlist == (char **) NULL)
2252 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2254 for (i=0; i < (ssize_t) lines; i++)
2256 for (q=p; *q != '\0'; q++)
2257 if ((*q == '\r') || (*q == '\n'))
2259 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2260 sizeof(**textlist));
2261 if (textlist[i] == (char *) NULL)
2262 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2263 (void) memcpy(textlist[i],p,(size_t) (q-p));
2264 textlist[i][q-p]='\0';
2273 hex_string[MaxTextExtent];
2282 Convert string to a HEX list.
2284 lines=(size_t) (strlen(text)/0x14)+1;
2285 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2287 if (textlist == (char **) NULL)
2288 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2290 for (i=0; i < (ssize_t) lines; i++)
2292 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2293 sizeof(**textlist));
2294 if (textlist[i] == (char *) NULL)
2295 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2296 (void) FormatLocaleString(textlist[i],MaxTextExtent,"0x%08lx: ",
2298 q=textlist[i]+strlen(textlist[i]);
2299 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2301 (void) FormatLocaleString(hex_string,MaxTextExtent,"%02x",*(p+j));
2302 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2304 if ((j % 0x04) == 0)
2307 for ( ; j <= 0x14; j++)
2311 if ((j % 0x04) == 0)
2315 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2317 if (isprint((int) ((unsigned char) *p)) != 0)
2326 textlist[i]=(char *) NULL;
2331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2335 % S t r i n g T o S t r i n g I n f o %
2339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2341 % StringToStringInfo() converts a string to a StringInfo type.
2343 % The format of the StringToStringInfo method is:
2345 % StringInfo *StringToStringInfo(const char *string)
2347 % A description of each parameter follows:
2349 % o string: The string.
2352 MagickExport StringInfo *StringToStringInfo(const char *string)
2357 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2358 assert(string != (const char *) NULL);
2359 string_info=AcquireStringInfo(strlen(string));
2360 SetStringInfoDatum(string_info,(const unsigned char *) string);
2361 return(string_info);
2365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2369 % S t r i p S t r i n g %
2373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2375 % StripString() strips any whitespace or quotes from the beginning and end of
2376 % a string of characters.
2378 % The format of the StripString method is:
2380 % void StripString(char *message)
2382 % A description of each parameter follows:
2384 % o message: Specifies an array of characters.
2387 MagickExport void StripString(char *message)
2396 assert(message != (char *) NULL);
2397 if (*message == '\0')
2399 length=strlen(message);
2401 while (isspace((int) ((unsigned char) *p)) != 0)
2403 if ((*p == '\'') || (*p == '"'))
2406 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2409 if ((*q == '\'') || (*q == '"'))
2411 (void) memmove(message,p,(size_t) (q-p+1));
2412 message[q-p+1]='\0';
2413 for (p=message; *p != '\0'; p++)
2419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2423 % S u b s t i t u t e S t r i n g %
2427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2429 % SubstituteString() performs string substitution on a string, replacing the
2430 % string with the substituted version. Buffer must be allocated from the heap.
2431 % If the string is matched and status, MagickTrue is returned otherwise
2434 % The format of the SubstituteString method is:
2436 % MagickBooleanType SubstituteString(char **string,const char *search,
2437 % const char *replace)
2439 % A description of each parameter follows:
2441 % o string: the string to perform replacements on; replaced with new
2442 % allocation if a replacement is made.
2444 % o search: search for this string.
2446 % o replace: replace any matches with this string.
2449 MagickExport MagickBooleanType SubstituteString(char **string,
2450 const char *search,const char *replace)
2469 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2471 if (search_extent == 0)
2472 search_extent=strlen(search);
2473 if (strncmp(p,search,search_extent) != 0)
2479 if (replace_extent == 0)
2480 replace_extent=strlen(replace);
2481 if (replace_extent > search_extent)
2484 Make room for the replacement string.
2486 offset=(ssize_t) (p-(*string));
2487 extent=strlen(*string)+replace_extent-search_extent+1;
2488 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2490 if (*string == (char *) NULL)
2491 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2497 if (search_extent != replace_extent)
2498 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2499 strlen(p+search_extent)+1);
2500 (void) CopyMagickMemory(p,replace,replace_extent);
2501 p+=replace_extent-1;