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-2017 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 % https://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/image-private.h"
48 #include "MagickCore/list.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/memory_.h"
52 #include "MagickCore/nt-base-private.h"
53 #include "MagickCore/property.h"
54 #include "MagickCore/resource_.h"
55 #include "MagickCore/signature-private.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/utility-private.h"
64 #define asciimap AsciiMap
66 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
67 static const unsigned char
70 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
71 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
72 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
73 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
74 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
75 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
76 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
77 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
78 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
79 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
80 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
81 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
82 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
83 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
84 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
85 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
86 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
87 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
88 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
89 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
90 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
91 0xfc, 0xfd, 0xfe, 0xff,
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % A c q u i r e S t r i n g %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % AcquireString() returns an new extented string, containing a clone of the
109 % An extended string is the string length, plus an extra MagickPathExtent space
110 % to allow for the string to be actively worked on.
112 % The returned string shoud be freed using DestoryString().
114 % The format of the AcquireString method is:
116 % char *AcquireString(const char *source)
118 % A description of each parameter follows:
120 % o source: A character string.
123 MagickExport char *AcquireString(const char *source)
132 if (source != (char *) NULL)
133 length+=strlen(source);
134 if (~length < MagickPathExtent)
135 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
136 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
137 sizeof(*destination));
138 if (destination == (char *) NULL)
139 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
141 if (source != (char *) NULL)
142 (void) memcpy(destination,source,length*sizeof(*destination));
143 destination[length]='\0';
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % A c q u i r e S t r i n g I n f o %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % AcquireStringInfo() allocates the StringInfo structure.
160 % The format of the AcquireStringInfo method is:
162 % StringInfo *AcquireStringInfo(const size_t length)
164 % A description of each parameter follows:
166 % o length: the string length.
169 MagickExport StringInfo *AcquireStringInfo(const size_t length)
174 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
175 if (string_info == (StringInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
178 string_info->signature=MagickCoreSignature;
179 string_info->length=length;
180 string_info->datum=(unsigned char *) NULL;
181 if (~string_info->length >= (MagickPathExtent-1))
182 string_info->datum=(unsigned char *) AcquireQuantumMemory(
183 string_info->length+MagickPathExtent,sizeof(*string_info->datum));
184 if (string_info->datum == (unsigned char *) NULL)
185 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % B l o b T o S t r i n g I n f o %
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 % BlobToStringInfo() returns the contents of a blob as a StringInfo structure
201 % with MagickPathExtent extra space.
203 % The format of the BlobToStringInfo method is:
205 % StringInfo *BlobToStringInfo(const void *blob,const size_t length)
207 % A description of each parameter follows:
211 % o length: the length of the blob.
214 MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
219 string_info=AcquireStringInfo(0);
220 if (~length < MagickPathExtent)
221 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
222 string_info->length=length;
223 if (string_info->datum == (unsigned char *) NULL)
224 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
225 MagickPathExtent,sizeof(*string_info->datum));
227 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
228 length+MagickPathExtent,sizeof(*string_info->datum));
229 if (string_info->datum == (unsigned char *) NULL)
231 string_info=DestroyStringInfo(string_info);
232 return((StringInfo *) NULL);
234 if (blob != (const void *) NULL)
235 (void) memcpy(string_info->datum,blob,length);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % C l o n e S t r i n g %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % CloneString() replaces or frees the destination string to make it
251 % a clone of the input string plus MagickPathExtent more space so the string
254 % If source is a NULL pointer the destination string will be freed and set to
255 % a NULL pointer. A pointer to the stored in the destination is also returned.
257 % When finished the non-NULL string should be freed using DestoryString()
258 % or using CloneString() with a NULL pointed for the source.
260 % The format of the CloneString method is:
262 % char *CloneString(char **destination,const char *source)
264 % A description of each parameter follows:
266 % o destination: A pointer to a character string.
268 % o source: A character string.
271 MagickExport char *CloneString(char **destination,const char *source)
276 assert(destination != (char **) NULL);
277 if (source == (const char *) NULL)
279 if (*destination != (char *) NULL)
280 *destination=DestroyString(*destination);
281 return(*destination);
283 if (*destination == (char *) NULL)
285 *destination=AcquireString(source);
286 return(*destination);
288 length=strlen(source);
289 if (~length < MagickPathExtent)
290 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
291 *destination=(char *) ResizeQuantumMemory(*destination,length+
292 MagickPathExtent,sizeof(**destination));
293 if (*destination == (char *) NULL)
294 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
296 (void) memcpy(*destination,source,length*sizeof(**destination));
297 (*destination)[length]='\0';
298 return(*destination);
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % C l o n e S t r i n g I n f o %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % CloneStringInfo() clones a copy of the StringInfo structure.
314 % The format of the CloneStringInfo method is:
316 % StringInfo *CloneStringInfo(const StringInfo *string_info)
318 % A description of each parameter follows:
320 % o string_info: the string info.
323 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
328 assert(string_info != (StringInfo *) NULL);
329 assert(string_info->signature == MagickCoreSignature);
330 clone_info=AcquireStringInfo(string_info->length);
331 if (string_info->length != 0)
332 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % C o m p a r e S t r i n g I n f o %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % CompareStringInfo() compares the two datums target and source. It returns
348 % an integer less than, equal to, or greater than zero if target is found,
349 % respectively, to be less than, to match, or be greater than source.
351 % The format of the CompareStringInfo method is:
353 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
355 % A description of each parameter follows:
357 % o target: the target string.
359 % o source: the source string.
363 MagickExport int CompareStringInfo(const StringInfo *target,
364 const StringInfo *source)
369 assert(target != (StringInfo *) NULL);
370 assert(target->signature == MagickCoreSignature);
371 assert(source != (StringInfo *) NULL);
372 assert(source->signature == MagickCoreSignature);
373 status=memcmp(target->datum,source->datum,MagickMin(target->length,
377 if (target->length == source->length)
379 return(target->length < source->length ? -1 : 1);
383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 % C o n c a t e n a t e M a g i c k S t r i n g %
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 % ConcatenateMagickString() concatenates the source string to the destination
394 % string. The destination buffer is always null-terminated even if the
395 % string must be truncated.
397 % The format of the ConcatenateMagickString method is:
399 % size_t ConcatenateMagickString(char *destination,const char *source,
400 % const size_t length)
402 % A description of each parameter follows:
404 % o destination: the destination string.
406 % o source: the source string.
408 % o length: the length of the destination string.
411 MagickExport size_t ConcatenateMagickString(char *destination,
412 const char *source,const size_t length)
426 assert(destination != (char *) NULL);
427 assert(source != (const char *) NULL);
432 while ((i-- != 0) && (*q != '\0'))
434 count=(size_t) (q-destination);
437 return(count+strlen(p));
448 return(count+(p-source));
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 % C o n c a t e n a t e S t r i n g %
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % ConcatenateString() appends a copy of string source, including the
463 % terminating null character, to the end of string destination.
465 % The format of the ConcatenateString method is:
467 % MagickBooleanType ConcatenateString(char **destination,
468 % const char *source)
470 % A description of each parameter follows:
472 % o destination: A pointer to a character string.
474 % o source: A character string.
477 MagickExport MagickBooleanType ConcatenateString(char **destination,
485 assert(destination != (char **) NULL);
486 if (source == (const char *) NULL)
488 if (*destination == (char *) NULL)
490 *destination=AcquireString(source);
493 destination_length=strlen(*destination);
494 source_length=strlen(source);
495 length=destination_length;
496 if (~length < source_length)
497 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
498 length+=source_length;
499 if (~length < MagickPathExtent)
500 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
501 *destination=(char *) ResizeQuantumMemory(*destination,length+
502 MagickPathExtent,sizeof(**destination));
503 if (*destination == (char *) NULL)
504 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
505 if (source_length != 0)
506 (void) memcpy((*destination)+destination_length,source,source_length);
507 (*destination)[length]='\0';
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 % C o n c a t e n a t e S t r i n g I n f o %
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 % ConcatenateStringInfo() concatenates the source string to the destination
525 % The format of the ConcatenateStringInfo method is:
527 % void ConcatenateStringInfo(StringInfo *string_info,
528 % const StringInfo *source)
530 % A description of each parameter follows:
532 % o string_info: the string info.
534 % o source: the source string.
537 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
538 const StringInfo *source)
543 assert(string_info != (StringInfo *) NULL);
544 assert(string_info->signature == MagickCoreSignature);
545 assert(source != (const StringInfo *) NULL);
546 length=string_info->length;
547 if (~length < source->length)
548 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
549 SetStringInfoLength(string_info,length+source->length);
550 (void) memcpy(string_info->datum+length,source->datum,source->length);
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % 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 %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % ConfigureFileToStringInfo() returns the contents of a configure file as a
567 % The format of the ConfigureFileToStringInfo method is:
569 % StringInfo *ConfigureFileToStringInfo(const char *filename)
570 % ExceptionInfo *exception)
572 % A description of each parameter follows:
574 % o filename: the filename.
577 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
597 assert(filename != (const char *) NULL);
598 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
600 return((StringInfo *) NULL);
601 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
602 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
605 return((StringInfo *) NULL);
607 length=(size_t) offset;
608 string=(char *) NULL;
609 if (~length >= (MagickPathExtent-1))
610 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
612 if (string == (char *) NULL)
615 return((StringInfo *) NULL);
617 map=MapBlob(file,ReadMode,0,length);
618 if (map != (void *) NULL)
620 (void) memcpy(string,map,length);
621 (void) UnmapBlob(map,length);
631 (void) lseek(file,0,SEEK_SET);
632 for (i=0; i < length; i+=count)
634 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
646 string=DestroyString(string);
647 return((StringInfo *) NULL);
652 string_info=AcquireStringInfo(0);
653 string_info->path=ConstantString(filename);
654 string_info->length=length;
655 if (string_info->datum != (unsigned char *) NULL)
656 string_info->datum=(unsigned char *) RelinquishMagickMemory(
658 string_info->datum=(unsigned char *) string;
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 % C o n s t a n t S t r i n g %
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 % ConstantString() allocates exactly the needed memory for a string and
674 % copies the source string to that memory location. A NULL string pointer
675 % will allocate an empty string containing just the NUL character.
677 % When finished the string should be freed using DestoryString()
679 % The format of the ConstantString method is:
681 % char *ConstantString(const char *source)
683 % A description of each parameter follows:
685 % o source: A character string.
688 MagickExport char *ConstantString(const char *source)
697 if (source != (char *) NULL)
698 length+=strlen(source);
699 destination=(char *) NULL;
701 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
702 if (destination == (char *) NULL)
703 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
705 if (source != (char *) NULL)
706 (void) memcpy(destination,source,length*sizeof(*destination));
707 destination[length]='\0';
712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % C o p y M a g i c k S t r i n g %
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 % CopyMagickString() copies the source string to the destination string, with
723 % out exceeding the given pre-declared length.
725 % The destination buffer is always null-terminated even if the string must be
726 % truncated. The return value is the minimum of the source string length or
727 % the length parameter.
729 % The format of the CopyMagickString method is:
731 % size_t CopyMagickString(const char *destination,char *source,
732 % const size_t length)
734 % A description of each parameter follows:
736 % o destination: the destination string.
738 % o source: the source string.
740 % o length: the length of the destination string.
743 MagickExport size_t CopyMagickString(char *destination,const char *source,
757 for (n=length; n > 4; n-=4)
761 return((size_t) (p-source-1));
765 return((size_t) (p-source-1));
769 return((size_t) (p-source-1));
773 return((size_t) (p-source-1));
777 for (n--; n != 0; n--)
781 return((size_t) (p-source-1));
786 return((size_t) (p-source-1));
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % D e s t r o y S t r i n g %
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % DestroyString() destroys memory associated with a string.
802 % The format of the DestroyString method is:
804 % char *DestroyString(char *string)
806 % A description of each parameter follows:
808 % o string: the string.
811 MagickExport char *DestroyString(char *string)
813 return((char *) RelinquishMagickMemory(string));
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % D e s t r o y S t r i n g I n f o %
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
829 % The format of the DestroyStringInfo method is:
831 % StringInfo *DestroyStringInfo(StringInfo *string_info)
833 % A description of each parameter follows:
835 % o string_info: the string info.
838 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
840 assert(string_info != (StringInfo *) NULL);
841 assert(string_info->signature == MagickCoreSignature);
842 if (string_info->datum != (unsigned char *) NULL)
843 string_info->datum=(unsigned char *) RelinquishMagickMemory(
845 if (string_info->path != (char *) NULL)
846 string_info->path=DestroyString(string_info->path);
847 string_info->signature=(~MagickCoreSignature);
848 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 % D e s t r o y S t r i n g L i s t %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % DestroyStringList() zeros memory associated with a string list.
865 % The format of the DestroyStringList method is:
867 % char **DestroyStringList(char **list)
869 % A description of each parameter follows:
871 % o list: the string list.
874 MagickExport char **DestroyStringList(char **list)
879 assert(list != (char **) NULL);
880 for (i=0; list[i] != (char *) NULL; i++)
881 list[i]=DestroyString(list[i]);
882 list=(char **) RelinquishMagickMemory(list);
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 % E s c a p e S t r i n g %
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 % EscapeString() allocates memory for a backslash-escaped version of a
898 % source text string, copies the escaped version of the text to that
899 % memory location while adding backslash characters, and returns the
902 % The format of the EscapeString method is:
904 % char *EscapeString(const char *source,const char escape)
906 % A description of each parameter follows:
908 % o allocate_string: Method EscapeString returns the escaped string.
910 % o source: A character string.
912 % o escape: the quoted string termination character to escape (e.g. '"').
915 MagickExport char *EscapeString(const char *source,const char escape)
929 assert(source != (const char *) NULL);
931 for (p=source; *p != '\0'; p++)
933 if ((*p == '\\') || (*p == escape))
936 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
941 destination=(char *) NULL;
942 if (~length >= (MagickPathExtent-1))
943 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
944 sizeof(*destination));
945 if (destination == (char *) NULL)
946 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
949 for (p=source; *p != '\0'; p++)
951 if ((*p == '\\') || (*p == escape))
960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % F i l e T o S t r i n g %
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 % FileToString() returns the contents of a file as a string.
972 % The format of the FileToString method is:
974 % char *FileToString(const char *filename,const size_t extent,
975 % ExceptionInfo *exception)
977 % A description of each parameter follows:
979 % o filename: the filename.
981 % o extent: Maximum length of the string.
983 % o exception: return any errors or warnings in this structure.
986 MagickExport char *FileToString(const char *filename,const size_t extent,
987 ExceptionInfo *exception)
992 assert(filename != (const char *) NULL);
993 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
994 assert(exception != (ExceptionInfo *) NULL);
995 return((char *) FileToBlob(filename,extent,&length,exception));
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003 % F i l e T o S t r i n g I n f o %
1007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 % FileToStringInfo() returns the contents of a file as a string.
1011 % The format of the FileToStringInfo method is:
1013 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
1014 % ExceptionInfo *exception)
1016 % A description of each parameter follows:
1018 % o filename: the filename.
1020 % o extent: Maximum length of the string.
1022 % o exception: return any errors or warnings in this structure.
1025 MagickExport StringInfo *FileToStringInfo(const char *filename,
1026 const size_t extent,ExceptionInfo *exception)
1031 assert(filename != (const char *) NULL);
1032 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1033 assert(exception != (ExceptionInfo *) NULL);
1034 string_info=AcquireStringInfo(0);
1035 string_info->path=ConstantString(filename);
1036 if (string_info->datum != (unsigned char *) NULL)
1037 string_info->datum=(unsigned char *) RelinquishMagickMemory(
1038 string_info->datum);
1039 string_info->datum=(unsigned char *) FileToBlob(filename,extent,
1040 &string_info->length,exception);
1041 if (string_info->datum == (unsigned char *) NULL)
1043 string_info=DestroyStringInfo(string_info);
1044 return((StringInfo *) NULL);
1046 return(string_info);
1050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054 % F o r m a t M a g i c k S i z e %
1058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 % FormatMagickSize() converts a size to a human readable format, for example,
1061 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1064 % The format of the FormatMagickSize method is:
1066 % ssize_t FormatMagickSize(const MagickSizeType size,const char *suffix,
1067 % const size_t length,char *format)
1069 % A description of each parameter follows:
1071 % o size: convert this size to a human readable format.
1073 % o bi: use power of two rather than power of ten.
1075 % o suffix: append suffix, typically B or P.
1077 % o length: the maximum length of the string.
1079 % o format: human readable format.
1082 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1083 const MagickBooleanType bi,const char *suffix,const size_t length,
1087 p[MagickPathExtent],
1088 q[MagickPathExtent];
1106 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1108 *traditional_units[] =
1110 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1114 units=traditional_units;
1115 if (bi != MagickFalse)
1120 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1121 extent=(double) ((MagickOffsetType) size);
1123 extent=(double) size;
1125 (void) FormatLocaleString(p,MagickPathExtent,"%.*g",GetMagickPrecision(),
1127 (void) FormatLocaleString(q,MagickPathExtent,"%.20g",extent);
1128 if (strtod(p,(char **) NULL) == strtod(q,(char **) NULL))
1130 if (suffix == (const char *) NULL)
1131 count=FormatLocaleString(format,length,"%.20g%s",extent,units[0]);
1133 count=FormatLocaleString(format,length,"%.20g%s%s",extent,units[0],
1137 for (i=0; (extent >= bytes) && (units[i+1] != (const char *) NULL); i++)
1139 if (suffix == (const char *) NULL)
1140 count=FormatLocaleString(format,length,"%.*g%s",GetMagickPrecision(),
1143 count=FormatLocaleString(format,length,"%.*g%s%s",GetMagickPrecision(),
1144 extent,units[i],suffix);
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153 % F o r m a t M a g i c k T i m e %
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159 % FormatMagickTime() returns the specified time in the Internet date/time
1160 % format and the length of the timestamp.
1162 % The format of the FormatMagickTime method is:
1164 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1167 % A description of each parameter follows.
1169 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1170 % measured in seconds.
1172 % o length: the maximum length of the string.
1174 % o timestamp: Return the Internet date/time here.
1177 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1190 assert(timestamp != (char *) NULL);
1191 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1192 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1193 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1194 (void) localtime_r(&time,&local_time);
1200 my_time=localtime(&time);
1201 if (my_time != (struct tm *) NULL)
1202 (void) memcpy(&local_time,my_time,sizeof(local_time));
1205 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1206 (void) gmtime_r(&time,&gm_time);
1212 my_time=gmtime(&time);
1213 if (my_time != (struct tm *) NULL)
1214 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1217 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1218 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1219 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1220 (local_time.tm_yday-gm_time.tm_yday)));
1221 count=FormatLocaleString(timestamp,length,
1222 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1223 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1224 local_time.tm_min,local_time.tm_sec,(long) timezone);
1229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 % G e t E n v i r o n m e n t V a l u e %
1237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 % GetEnvironmentValue() returns the environment string that matches the
1242 % The format of the GetEnvironmentValue method is:
1244 % char *GetEnvironmentValue(const char *name)
1246 % A description of each parameter follows:
1248 % o name: the environment name.
1251 MagickExport char *GetEnvironmentValue(const char *name)
1256 environment=getenv(name);
1257 if (environment == (const char *) NULL)
1258 return((char *) NULL);
1259 return(ConstantString(environment));
1263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 % G e t S t r i n g I n f o D a t u m %
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273 % GetStringInfoDatum() returns the datum associated with the string.
1275 % The format of the GetStringInfoDatum method is:
1277 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1279 % A description of each parameter follows:
1281 % o string_info: the string info.
1284 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1286 assert(string_info != (StringInfo *) NULL);
1287 assert(string_info->signature == MagickCoreSignature);
1288 return(string_info->datum);
1292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1296 % G e t S t r i n g I n f o L e n g t h %
1300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302 % GetStringInfoLength() returns the string length.
1304 % The format of the GetStringInfoLength method is:
1306 % size_t GetStringInfoLength(const StringInfo *string_info)
1308 % A description of each parameter follows:
1310 % o string_info: the string info.
1313 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1315 assert(string_info != (StringInfo *) NULL);
1316 assert(string_info->signature == MagickCoreSignature);
1317 return(string_info->length);
1321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1325 % G e t S t r i n g I n f o P a t h %
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1331 % GetStringInfoPath() returns the path associated with the string.
1333 % The format of the GetStringInfoPath method is:
1335 % const char *GetStringInfoPath(const StringInfo *string_info)
1337 % A description of each parameter follows:
1339 % o string_info: the string info.
1342 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1344 assert(string_info != (StringInfo *) NULL);
1345 assert(string_info->signature == MagickCoreSignature);
1346 return(string_info->path);
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 + I n t e r p r e t S i P r e f i x V a l u e %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % InterpretSiPrefixValue() converts the initial portion of the string to a
1361 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1364 % The format of the InterpretSiPrefixValue method is:
1366 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1368 % A description of each parameter follows:
1370 % o value: the string value.
1372 % o sentinal: if sentinal is not NULL, return a pointer to the character
1373 % after the last character used in the conversion.
1376 MagickExport double InterpretSiPrefixValue(const char *magick_restrict string,
1377 char **magick_restrict sentinal)
1385 value=InterpretLocaleValue(string,&q);
1388 if ((*q >= 'E') && (*q <= 'z'))
1393 switch ((int) ((unsigned char) *q))
1395 case 'y': e=(-24.0); break;
1396 case 'z': e=(-21.0); break;
1397 case 'a': e=(-18.0); break;
1398 case 'f': e=(-15.0); break;
1399 case 'p': e=(-12.0); break;
1400 case 'n': e=(-9.0); break;
1401 case 'u': e=(-6.0); break;
1402 case 'm': e=(-3.0); break;
1403 case 'c': e=(-2.0); break;
1404 case 'd': e=(-1.0); break;
1405 case 'h': e=2.0; break;
1406 case 'k': e=3.0; break;
1407 case 'K': e=3.0; break;
1408 case 'M': e=6.0; break;
1409 case 'G': e=9.0; break;
1410 case 'T': e=12.0; break;
1411 case 'P': e=15.0; break;
1412 case 'E': e=18.0; break;
1413 case 'Z': e=21.0; break;
1414 case 'Y': e=24.0; break;
1415 default: e=0.0; break;
1417 if (e >= MagickEpsilon)
1421 value*=pow(2.0,e/0.3);
1431 if ((*q == 'B') || (*q == 'P'))
1434 if (sentinal != (char **) NULL)
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444 % I s S t r i n g T r u e %
1448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1451 % "1". Any other string or undefined returns MagickFalse.
1453 % Typically this is used to look at strings (options or artifacts) which
1454 % has a default value of "false", when not defined.
1456 % The format of the IsStringTrue method is:
1458 % MagickBooleanType IsStringTrue(const char *value)
1460 % A description of each parameter follows:
1462 % o value: Specifies a pointer to a character array.
1465 MagickExport MagickBooleanType IsStringTrue(const char *value)
1467 if (value == (const char *) NULL)
1468 return(MagickFalse);
1469 if (LocaleCompare(value,"true") == 0)
1471 if (LocaleCompare(value,"on") == 0)
1473 if (LocaleCompare(value,"yes") == 0)
1475 if (LocaleCompare(value,"1") == 0)
1477 return(MagickFalse);
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485 % I s S t r i n g F a l s e %
1489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % IsStringFalse() returns MagickTrue if the value is "false", "off", "no" or
1492 % "0". Any other string or undefined returns MagickFalse.
1494 % Typically this is used to look at strings (options or artifacts) which
1495 % has a default value of "true", when it has not been defined.
1497 % The format of the IsStringFalse method is:
1499 % MagickBooleanType IsStringFalse(const char *value)
1501 % A description of each parameter follows:
1503 % o value: Specifies a pointer to a character array.
1506 MagickExport MagickBooleanType IsStringFalse(const char *value)
1508 if (value == (const char *) NULL)
1509 return(MagickFalse);
1510 if (LocaleCompare(value,"false") == 0)
1512 if (LocaleCompare(value,"off") == 0)
1514 if (LocaleCompare(value,"no") == 0)
1516 if (LocaleCompare(value,"0") == 0)
1518 return(MagickFalse);
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1526 % P r i n t S t r i n g I n f o %
1530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532 % PrintStringInfo() prints the string.
1534 % The format of the PrintStringInfo method is:
1536 % void PrintStringInfo(FILE *file,const char *id,
1537 % const StringInfo *string_info)
1539 % A description of each parameter follows:
1541 % o file: the file, typically stdout.
1543 % o id: the string id.
1545 % o string_info: the string info.
1548 MagickExport void PrintStringInfo(FILE *file,const char *id,
1549 const StringInfo *string_info)
1558 assert(id != (const char *) NULL);
1559 assert(string_info != (StringInfo *) NULL);
1560 assert(string_info->signature == MagickCoreSignature);
1561 p=(char *) string_info->datum;
1562 for (i=0; i < string_info->length; i++)
1564 if (((int) ((unsigned char) *p) < 32) &&
1565 (isspace((int) ((unsigned char) *p)) == 0))
1569 (void) FormatLocaleFile(file,"%s(%.20g): ",id,(double) string_info->length);
1570 if (i == string_info->length)
1572 for (i=0; i < string_info->length; i++)
1573 (void) fputc(string_info->datum[i],file);
1574 (void) fputc('\n',file);
1578 Convert string to a HEX list.
1580 p=(char *) string_info->datum;
1581 for (i=0; i < string_info->length; i+=0x14)
1583 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
1584 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1586 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1587 if ((j % 0x04) == 0)
1588 (void) fputc(' ',file);
1590 for ( ; j <= 0x14; j++)
1592 (void) fputc(' ',file);
1593 (void) fputc(' ',file);
1594 if ((j % 0x04) == 0)
1595 (void) fputc(' ',file);
1597 (void) fputc(' ',file);
1598 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1600 if (isprint((int) ((unsigned char) *p)) != 0)
1601 (void) fputc(*p,file);
1603 (void) fputc('-',file);
1606 (void) fputc('\n',file);
1611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615 % R e s e t S t r i n g I n f o %
1619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % ResetStringInfo() reset the string to all null bytes.
1623 % The format of the ResetStringInfo method is:
1625 % void ResetStringInfo(StringInfo *string_info)
1627 % A description of each parameter follows:
1629 % o string_info: the string info.
1632 MagickExport void ResetStringInfo(StringInfo *string_info)
1634 assert(string_info != (StringInfo *) NULL);
1635 assert(string_info->signature == MagickCoreSignature);
1636 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % S a n t i z e S t r i n g %
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650 % SanitizeString() returns an new string removes all characters except
1651 % letters, digits and !#$%&'*+-=?^_`{|}~@.[].
1653 % The returned string shoud be freed using DestoryString().
1655 % The format of the SanitizeString method is:
1657 % char *SanitizeString(const char *source)
1659 % A description of each parameter follows:
1661 % o source: A character string.
1664 MagickExport char *SanitizeString(const char *source)
1677 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1678 "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1680 sanitize_source=AcquireString(source);
1682 q=sanitize_source+strlen(sanitize_source);
1683 for (p+=strspn(p,whitelist); p != q; p+=strspn(p,whitelist))
1685 return(sanitize_source);
1689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1693 % S e t S t r i n g I n f o %
1697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1699 % SetStringInfo() copies the source string to the destination string.
1701 % The format of the SetStringInfo method is:
1703 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1705 % A description of each parameter follows:
1707 % o string_info: the string info.
1709 % o source: the source string.
1712 MagickExport void SetStringInfo(StringInfo *string_info,
1713 const StringInfo *source)
1715 assert(string_info != (StringInfo *) NULL);
1716 assert(string_info->signature == MagickCoreSignature);
1717 assert(source != (StringInfo *) NULL);
1718 assert(source->signature == MagickCoreSignature);
1719 if (string_info->length == 0)
1721 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1722 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1731 % S e t S t r i n g I n f o D a t u m %
1735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1737 % SetStringInfoDatum() copies bytes from the source string for the length of
1738 % the destination string.
1740 % The format of the SetStringInfoDatum method is:
1742 % void SetStringInfoDatum(StringInfo *string_info,
1743 % const unsigned char *source)
1745 % A description of each parameter follows:
1747 % o string_info: the string info.
1749 % o source: the source string.
1752 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1753 const unsigned char *source)
1755 assert(string_info != (StringInfo *) NULL);
1756 assert(string_info->signature == MagickCoreSignature);
1757 if (string_info->length != 0)
1758 (void) memcpy(string_info->datum,source,string_info->length);
1762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766 % S e t S t r i n g I n f o L e n g t h %
1770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1772 % SetStringInfoLength() set the string length to the specified value.
1774 % The format of the SetStringInfoLength method is:
1776 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1778 % A description of each parameter follows:
1780 % o string_info: the string info.
1782 % o length: the string length.
1785 MagickExport void SetStringInfoLength(StringInfo *string_info,
1786 const size_t length)
1788 assert(string_info != (StringInfo *) NULL);
1789 assert(string_info->signature == MagickCoreSignature);
1790 if (string_info->length == length)
1792 if (~length < MagickPathExtent)
1793 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1794 string_info->length=length;
1795 if (string_info->datum == (unsigned char *) NULL)
1796 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1797 MagickPathExtent,sizeof(*string_info->datum));
1799 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1800 length+MagickPathExtent,sizeof(*string_info->datum));
1801 if (string_info->datum == (unsigned char *) NULL)
1802 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1810 % S e t S t r i n g I n f o D a t u m %
1814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816 % SetStringInfoPath() sets the path associated with the string.
1818 % The format of the SetStringInfoPath method is:
1820 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1822 % A description of each parameter follows:
1824 % o string_info: the string info.
1829 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1831 assert(string_info != (StringInfo *) NULL);
1832 assert(string_info->signature == MagickCoreSignature);
1833 assert(path != (const char *) NULL);
1834 string_info->path=ConstantString(path);
1838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1842 % S p l i t S t r i n g I n f o %
1846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1848 % SplitStringInfo() splits a string into two and returns it.
1850 % The format of the SplitStringInfo method is:
1852 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1854 % A description of each parameter follows:
1856 % o string_info: the string info.
1859 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1860 const size_t offset)
1865 assert(string_info != (StringInfo *) NULL);
1866 assert(string_info->signature == MagickCoreSignature);
1867 if (offset > string_info->length)
1868 return((StringInfo *) NULL);
1869 split_info=AcquireStringInfo(offset);
1870 SetStringInfo(split_info,string_info);
1871 (void) memmove(string_info->datum,string_info->datum+offset,
1872 string_info->length-offset+MagickPathExtent);
1873 SetStringInfoLength(string_info,string_info->length-offset);
1878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1882 % S t r i n g I n f o T o S t r i n g %
1886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1888 % StringInfoToString() converts a string info string to a C string.
1890 % The format of the StringInfoToString method is:
1892 % char *StringInfoToString(const StringInfo *string_info)
1894 % A description of each parameter follows:
1896 % o string_info: the string.
1899 MagickExport char *StringInfoToString(const StringInfo *string_info)
1907 string=(char *) NULL;
1908 length=string_info->length;
1909 if (~length >= (MagickPathExtent-1))
1910 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1912 if (string == (char *) NULL)
1913 return((char *) NULL);
1914 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1915 string[length]='\0';
1920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924 % S t r i n g I n f o T o H e x S t r i n g %
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1930 % StringInfoToHexString() converts a string info string to a C string.
1932 % The format of the StringInfoToHexString method is:
1934 % char *StringInfoToHexString(const StringInfo *string_info)
1936 % A description of each parameter follows:
1938 % o string_info: the string.
1941 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1946 register const unsigned char
1952 register unsigned char
1961 length=string_info->length;
1962 if (~length < MagickPathExtent)
1963 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1964 string=(char *) AcquireQuantumMemory(length+MagickPathExtent,2*
1966 if (string == (char *) NULL)
1967 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1984 p=string_info->datum;
1985 q=(unsigned char *) string;
1986 for (i=0; i < (ssize_t) string_info->length; i++)
1988 *q++=hex_digits[(*p >> 4) & 0x0f];
1989 *q++=hex_digits[*p & 0x0f];
1997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001 % S t r i n g T o A r g v %
2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2007 % StringToArgv() converts a text string into command line arguments.
2008 % The 'argv' array of arguments, is returned while the number of arguments
2009 % is returned via the provided integer variable pointer.
2011 % Simple 'word' tokenizer, which allows for each word to be optionally
2012 % quoted. However it will not allow use of partial quotes, or escape
2015 % The format of the StringToArgv method is:
2017 % char **StringToArgv(const char *text,int *argc)
2019 % A description of each parameter follows:
2021 % o argv: Method StringToArgv returns the string list unless an error
2022 % occurs, otherwise NULL.
2024 % o text: Specifies the string to segment into a list.
2026 % o argc: This integer pointer returns the number of arguments in the
2030 MagickExport char **StringToArgv(const char *text,int *argc)
2043 if (text == (char *) NULL)
2044 return((char **) NULL);
2046 Determine the number of arguments.
2048 for (p=text; *p != '\0'; )
2050 while (isspace((int) ((unsigned char) *p)) != 0)
2056 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2058 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2059 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2063 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2064 if (argv == (char **) NULL)
2065 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2067 Convert string to an ASCII list.
2069 argv[0]=AcquireString("magick");
2071 for (i=1; i < (ssize_t) *argc; i++)
2073 while (isspace((int) ((unsigned char) *p)) != 0)
2079 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2085 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2088 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2090 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
2092 if (argv[i] == (char *) NULL)
2094 for (i--; i >= 0; i--)
2095 argv[i]=DestroyString(argv[i]);
2096 argv=(char **) RelinquishMagickMemory(argv);
2097 ThrowFatalException(ResourceLimitFatalError,
2098 "UnableToConvertStringToARGV");
2100 (void) memcpy(argv[i],p,(size_t) (q-p));
2103 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2106 argv[i]=(char *) NULL;
2111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115 % S t r i n g T o A r r a y O f D o u b l e s %
2119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2121 % StringToArrayOfDoubles() converts a string of space or comma separated
2122 % numbers into array of floating point numbers (doubles). Any number that
2123 % failes to parse properly will produce a syntax error. As will two commas
2124 % without a number between them. However a final comma at the end will
2125 % not be regarded as an error so as to simplify automatic list generation.
2127 % A NULL value is returned on syntax or memory errors.
2129 % Use RelinquishMagickMemory() to free returned array when finished.
2131 % The format of the StringToArrayOfDoubles method is:
2133 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2134 % ExceptionInfo *exception)
2136 % A description of each parameter follows:
2138 % o string: the string containing the comma/space separated values.
2140 % o count: returns number of arguments in returned array
2142 % o exception: return any errors or warnings in this structure.
2145 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2146 ExceptionInfo *exception)
2161 Determine count of values, and check syntax.
2163 assert(exception != (ExceptionInfo *) NULL);
2164 assert(exception->signature == MagickCoreSignature);
2170 (void) StringToDouble(p,&q); /* get value - ignores leading space */
2172 return((double *) NULL); /* no value found */
2174 i++; /* increment value count */
2175 while (isspace((int) ((unsigned char) *p)) != 0)
2176 p++; /* skip spaces */
2178 p++; /* skip comma */
2179 while (isspace((int) ((unsigned char) *p)) != 0)
2180 p++; /* and more spaces */
2183 Allocate floating point argument list.
2186 array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2187 if (array == (double *) NULL)
2189 (void) ThrowMagickException(exception,GetMagickModule(),
2190 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2191 return((double *) NULL);
2194 Fill in the floating point values.
2198 while ((*p != '\0') && (i < *count))
2200 array[i++]=StringToDouble(p,&q);
2202 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213 + S t r i n g T o k e n %
2217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2219 % StringToken() looks for any one of given delimiters and splits the string
2220 % into two separate strings by replacing the delimiter character found with a
2223 % The given string pointer is changed to point to the string following the
2224 % delimiter character found, or NULL. A pointer to the start of the
2225 % string is returned, representing the token before the delimiter.
2227 % In may ways this is equivent to the strtok() C library function, but with
2228 % multiple delimiter characters rather than a delimiter string.
2230 % The format of the StringToken method is:
2232 % char *StringToken(const char *delimiters,char **string)
2234 % A description of each parameter follows:
2236 % o delimiters: one or more delimiters.
2238 % o string: return the first token in the string. If none is found, return
2242 MagickExport char *StringToken(const char *delimiters,char **string)
2258 if (p == (char *) NULL)
2259 return((char *) NULL);
2277 } while (d != '\0');
2282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286 % S t r i n g T o L i s t %
2290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2292 % StringToList() converts a text string into a list by segmenting the text
2293 % string at each carriage return discovered. The list is converted to HEX
2294 % characters if any control characters are discovered within the text string.
2296 % The format of the StringToList method is:
2298 % char **StringToList(const char *text)
2300 % A description of each parameter follows:
2302 % o text: Specifies the string to segment into a list.
2305 MagickExport char **StringToList(const char *text)
2319 if (text == (char *) NULL)
2320 return((char **) NULL);
2321 for (p=text; *p != '\0'; p++)
2322 if (((int) ((unsigned char) *p) < 32) &&
2323 (isspace((int) ((unsigned char) *p)) == 0))
2331 Convert string to an ASCII list.
2334 for (p=text; *p != '\0'; p++)
2337 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2339 if (textlist == (char **) NULL)
2340 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2342 for (i=0; i < (ssize_t) lines; i++)
2344 for (q=p; *q != '\0'; q++)
2345 if ((*q == '\r') || (*q == '\n'))
2347 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+
2348 MagickPathExtent,sizeof(**textlist));
2349 if (textlist[i] == (char *) NULL)
2350 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2351 (void) memcpy(textlist[i],p,(size_t) (q-p));
2352 textlist[i][q-p]='\0';
2361 hex_string[MagickPathExtent];
2370 Convert string to a HEX list.
2372 lines=(size_t) (strlen(text)/0x14)+1;
2373 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2375 if (textlist == (char **) NULL)
2376 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2378 for (i=0; i < (ssize_t) lines; i++)
2380 textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2381 sizeof(**textlist));
2382 if (textlist[i] == (char *) NULL)
2383 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2384 (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2386 q=textlist[i]+strlen(textlist[i]);
2387 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2389 (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2390 (void) CopyMagickString(q,hex_string,MagickPathExtent);
2392 if ((j % 0x04) == 0)
2395 for ( ; j <= 0x14; j++)
2399 if ((j % 0x04) == 0)
2403 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2405 if (isprint((int) ((unsigned char) *p)) != 0)
2414 textlist[i]=(char *) NULL;
2419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2423 % S t r i n g T o S t r i n g I n f o %
2427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2429 % StringToStringInfo() converts a string to a StringInfo type.
2431 % The format of the StringToStringInfo method is:
2433 % StringInfo *StringToStringInfo(const char *string)
2435 % A description of each parameter follows:
2437 % o string: The string.
2440 MagickExport StringInfo *StringToStringInfo(const char *string)
2445 assert(string != (const char *) NULL);
2446 string_info=AcquireStringInfo(strlen(string));
2447 SetStringInfoDatum(string_info,(const unsigned char *) string);
2448 return(string_info);
2452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2456 % S t r i p S t r i n g %
2460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2462 % StripString() strips any whitespace or quotes from the beginning and end of
2463 % a string of characters.
2465 % The format of the StripString method is:
2467 % void StripString(char *message)
2469 % A description of each parameter follows:
2471 % o message: Specifies an array of characters.
2474 MagickExport void StripString(char *message)
2483 assert(message != (char *) NULL);
2484 if (*message == '\0')
2486 length=strlen(message);
2488 while (isspace((int) ((unsigned char) *p)) != 0)
2490 if ((*p == '\'') || (*p == '"'))
2493 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2496 if ((*q == '\'') || (*q == '"'))
2498 (void) memmove(message,p,(size_t) (q-p+1));
2499 message[q-p+1]='\0';
2500 for (p=message; *p != '\0'; p++)
2506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2510 % S u b s t i t u t e S t r i n g %
2514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2516 % SubstituteString() performs string substitution on a string, replacing the
2517 % string with the substituted version. Buffer must be allocated from the heap.
2518 % If the string is matched and status, MagickTrue is returned otherwise
2521 % The format of the SubstituteString method is:
2523 % MagickBooleanType SubstituteString(char **string,const char *search,
2524 % const char *replace)
2526 % A description of each parameter follows:
2528 % o string: the string to perform replacements on; replaced with new
2529 % allocation if a replacement is made.
2531 % o search: search for this string.
2533 % o replace: replace any matches with this string.
2536 MagickExport MagickBooleanType SubstituteString(char **string,
2537 const char *search,const char *replace)
2556 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2558 if (search_extent == 0)
2559 search_extent=strlen(search);
2560 if (strncmp(p,search,search_extent) != 0)
2566 if (replace_extent == 0)
2567 replace_extent=strlen(replace);
2568 if (replace_extent > search_extent)
2571 Make room for the replacement string.
2573 offset=(ssize_t) (p-(*string));
2574 extent=strlen(*string)+replace_extent-search_extent+1;
2575 *string=(char *) ResizeQuantumMemory(*string,extent+MagickPathExtent,
2577 if (*string == (char *) NULL)
2578 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2584 if (search_extent != replace_extent)
2585 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2586 strlen(p+search_extent)+1);
2587 (void) CopyMagickMemory(p,replace,replace_extent);
2588 p+=replace_extent-1;