MagickCore  7.0.10
string.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % SSSSS TTTTT RRRR IIIII N N GGGG %
7 % SS T R R I NN N G %
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 %
11 % %
12 % %
13 % MagickCore String Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % August 2003 %
18 % %
19 % %
20 % Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the license. You may %
24 % obtain a copy of the license at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
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. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40  include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
45 #include "MagickCore/exception.h"
48 #include "MagickCore/list.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/memory_.h"
54 #include "MagickCore/property.h"
55 #include "MagickCore/resource_.h"
57 #include "MagickCore/string_.h"
60 
61 /*
62  Define declarations.
63 */
64 #define CharsPerLine 0x14
65 
66 /*
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 % %
69 % %
70 % %
71 % A c q u i r e S t r i n g %
72 % %
73 % %
74 % %
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %
77 % AcquireString() returns an new extended string, containing a clone of the
78 % given string.
79 %
80 % An extended string is the string length, plus an extra MagickPathExtent space
81 % to allow for the string to be actively worked on.
82 %
83 % The returned string shoud be freed using DestoryString().
84 %
85 % The format of the AcquireString method is:
86 %
87 % char *AcquireString(const char *source)
88 %
89 % A description of each parameter follows:
90 %
91 % o source: A character string.
92 %
93 */
94 MagickExport char *AcquireString(const char *source)
95 {
96  char
97  *destination;
98 
99  size_t
100  length;
101 
102  length=0;
103  if (source != (char *) NULL)
104  length+=strlen(source);
105  if (~length < MagickPathExtent)
106  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
107  destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
108  sizeof(*destination));
109  if (destination == (char *) NULL)
110  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
111  if (source != (char *) NULL)
112  (void) memcpy(destination,source,length*sizeof(*destination));
113  destination[length]='\0';
114  return(destination);
115 }
116 
117 /*
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % %
120 % %
121 % %
122 % A c q u i r e S t r i n g I n f o %
123 % %
124 % %
125 % %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 %
128 % AcquireStringInfo() allocates the StringInfo structure.
129 %
130 % The format of the AcquireStringInfo method is:
131 %
132 % StringInfo *AcquireStringInfo(const size_t length)
133 %
134 % A description of each parameter follows:
135 %
136 % o length: the string length.
137 %
138 */
139 
141 {
142  StringInfo
143  *string_info;
144 
145  string_info=(StringInfo *) AcquireCriticalMemory(sizeof(*string_info));
146  (void) memset(string_info,0,sizeof(*string_info));
147  string_info->signature=MagickCoreSignature;
148  return(string_info);
149 }
150 
152 {
153  StringInfo
154  *string_info;
155 
156  string_info=AcquireStringInfoContainer();
157  string_info->length=length;
158  if (~string_info->length >= (MagickPathExtent-1))
159  string_info->datum=(unsigned char *) AcquireQuantumMemory(
160  string_info->length+MagickPathExtent,sizeof(*string_info->datum));
161  if (string_info->datum == (unsigned char *) NULL)
162  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
163  (void) memset(string_info->datum,0,(length+MagickPathExtent)*
164  sizeof(*string_info->datum));
165  return(string_info);
166 }
167 
168 /*
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 % %
171 % %
172 % %
173 % B l o b T o S t r i n g I n f o %
174 % %
175 % %
176 % %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 %
179 % BlobToStringInfo() returns the contents of a blob as a StringInfo structure
180 % with MagickPathExtent extra space.
181 %
182 % The format of the BlobToStringInfo method is:
183 %
184 % StringInfo *BlobToStringInfo(const void *blob,const size_t length)
185 %
186 % A description of each parameter follows:
187 %
188 % o blob: the blob.
189 %
190 % o length: the length of the blob.
191 %
192 */
193 MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
194 {
195  StringInfo
196  *string_info;
197 
198  if (~length < MagickPathExtent)
199  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
200  string_info=AcquireStringInfoContainer();
201  string_info->length=length;
202  string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
203  MagickPathExtent,sizeof(*string_info->datum));
204  if (string_info->datum == (unsigned char *) NULL)
205  {
206  string_info=DestroyStringInfo(string_info);
207  return((StringInfo *) NULL);
208  }
209  if (blob != (const void *) NULL)
210  (void) memcpy(string_info->datum,blob,length);
211  else
212  (void) memset(string_info->datum,0,length*sizeof(*string_info->datum));
213  (void) memset(string_info->datum+length,0,MagickPathExtent*
214  sizeof(*string_info->datum));
215  return(string_info);
216 }
217 
218 /*
219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 % %
221 % %
222 % %
223 % C l o n e S t r i n g %
224 % %
225 % %
226 % %
227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 %
229 % CloneString() replaces or frees the destination string to make it
230 % a clone of the input string plus MagickPathExtent more space so the string
231 % may be worked on.
232 %
233 % If source is a NULL pointer the destination string will be freed and set to
234 % a NULL pointer. A pointer to the stored in the destination is also returned.
235 %
236 % When finished the non-NULL string should be freed using DestoryString()
237 % or using CloneString() with a NULL pointed for the source.
238 %
239 % The format of the CloneString method is:
240 %
241 % char *CloneString(char **destination,const char *source)
242 %
243 % A description of each parameter follows:
244 %
245 % o destination: A pointer to a character string.
246 %
247 % o source: A character string.
248 %
249 */
250 MagickExport char *CloneString(char **destination,const char *source)
251 {
252  size_t
253  length;
254 
255  assert(destination != (char **) NULL);
256  if (source == (const char *) NULL)
257  {
258  if (*destination != (char *) NULL)
259  *destination=DestroyString(*destination);
260  return(*destination);
261  }
262  if (*destination == (char *) NULL)
263  {
264  *destination=AcquireString(source);
265  return(*destination);
266  }
267  length=strlen(source);
268  if (~length < MagickPathExtent)
269  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
270  *destination=(char *) ResizeQuantumMemory(*destination,length+
271  MagickPathExtent,sizeof(**destination));
272  if (*destination == (char *) NULL)
273  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
274  if (length != 0)
275  (void) memcpy(*destination,source,length*sizeof(**destination));
276  (*destination)[length]='\0';
277  return(*destination);
278 }
279 
280 /*
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % %
283 % %
284 % %
285 % C l o n e S t r i n g I n f o %
286 % %
287 % %
288 % %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 %
291 % CloneStringInfo() clones a copy of the StringInfo structure.
292 %
293 % The format of the CloneStringInfo method is:
294 %
295 % StringInfo *CloneStringInfo(const StringInfo *string_info)
296 %
297 % A description of each parameter follows:
298 %
299 % o string_info: the string info.
300 %
301 */
303 {
304  StringInfo
305  *clone_info;
306 
307  assert(string_info != (StringInfo *) NULL);
308  assert(string_info->signature == MagickCoreSignature);
309  clone_info=AcquireStringInfo(string_info->length);
310  (void) CloneString(&clone_info->path,string_info->path);
311  (void) CloneString(&clone_info->name,string_info->name);
312  if (string_info->length != 0)
313  (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
314  return(clone_info);
315 }
316 
317 /*
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % %
320 % %
321 % %
322 % C o m p a r e S t r i n g I n f o %
323 % %
324 % %
325 % %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 %
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.
331 %
332 % The format of the CompareStringInfo method is:
333 %
334 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
335 %
336 % A description of each parameter follows:
337 %
338 % o target: the target string.
339 %
340 % o source: the source string.
341 %
342 */
343 
345  const StringInfo *source)
346 {
347  int
348  status;
349 
350  assert(target != (StringInfo *) NULL);
351  assert(target->signature == MagickCoreSignature);
352  assert(source != (StringInfo *) NULL);
353  assert(source->signature == MagickCoreSignature);
354  status=memcmp(target->datum,source->datum,MagickMin(target->length,
355  source->length));
356  if (status != 0)
357  return(status);
358  if (target->length == source->length)
359  return(0);
360  return(target->length < source->length ? -1 : 1);
361 }
362 
363 /*
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 % %
366 % %
367 % %
368 % C o n c a t e n a t e M a g i c k S t r i n g %
369 % %
370 % %
371 % %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 %
374 % ConcatenateMagickString() concatenates the source string to the destination
375 % string. The destination buffer is always null-terminated even if the
376 % string must be truncated.
377 %
378 % The format of the ConcatenateMagickString method is:
379 %
380 % size_t ConcatenateMagickString(char *magick_restrict destination,
381 % const char *magick_restrict source,const size_t length)
382 %
383 % A description of each parameter follows:
384 %
385 % o destination: the destination string.
386 %
387 % o source: the source string.
388 %
389 % o length: the length of the destination string.
390 %
391 */
393  const char *magick_restrict source,const size_t length)
394 {
395  register char
396  *magick_restrict q;
397 
398  register const char
399  *magick_restrict p;
400 
401  register size_t
402  i;
403 
404  size_t
405  count;
406 
407  assert(destination != (char *) NULL);
408  assert(source != (const char *) NULL);
409  assert(length >= 1);
410  p=source;
411  q=destination;
412  i=length;
413  while ((i-- != 0) && (*q != '\0'))
414  q++;
415  count=(size_t) (q-destination);
416  i=length-count;
417  if (i == 0)
418  return(count+strlen(p));
419  while (*p != '\0')
420  {
421  if (i != 1)
422  {
423  *q++=(*p);
424  i--;
425  }
426  p++;
427  }
428  *q='\0';
429  return(count+(p-source));
430 }
431 
432 /*
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 % %
435 % %
436 % %
437 % C o n c a t e n a t e S t r i n g %
438 % %
439 % %
440 % %
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 %
443 % ConcatenateString() appends a copy of string source, including the
444 % terminating null character, to the end of string destination.
445 %
446 % The format of the ConcatenateString method is:
447 %
448 % MagickBooleanType ConcatenateString(char **magick_restrict destination,
449 % const char *magick_restrict source)
450 %
451 % A description of each parameter follows:
452 %
453 % o destination: A pointer to a character string.
454 %
455 % o source: A character string.
456 %
457 */
459  char **magick_restrict destination,const char *magick_restrict source)
460 {
461  size_t
462  destination_length,
463  length,
464  source_length;
465 
466  assert(destination != (char **) NULL);
467  if (source == (const char *) NULL)
468  return(MagickTrue);
469  if (*destination == (char *) NULL)
470  {
471  *destination=AcquireString(source);
472  return(MagickTrue);
473  }
474  destination_length=strlen(*destination);
475  source_length=strlen(source);
476  length=destination_length;
477  if (~length < source_length)
478  ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
479  length+=source_length;
480  if (~length < MagickPathExtent)
481  ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
482  *destination=(char *) ResizeQuantumMemory(*destination,length+
483  MagickPathExtent,sizeof(**destination));
484  if (*destination == (char *) NULL)
485  ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
486  if (source_length != 0)
487  (void) memcpy((*destination)+destination_length,source,source_length);
488  (*destination)[length]='\0';
489  return(MagickTrue);
490 }
491 
492 /*
493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 % %
495 % %
496 % %
497 % C o n c a t e n a t e S t r i n g I n f o %
498 % %
499 % %
500 % %
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 %
503 % ConcatenateStringInfo() concatenates the source string to the destination
504 % string.
505 %
506 % The format of the ConcatenateStringInfo method is:
507 %
508 % void ConcatenateStringInfo(StringInfo *string_info,
509 % const StringInfo *source)
510 %
511 % A description of each parameter follows:
512 %
513 % o string_info: the string info.
514 %
515 % o source: the source string.
516 %
517 */
519  const StringInfo *source)
520 {
521  size_t
522  length;
523 
524  assert(string_info != (StringInfo *) NULL);
525  assert(string_info->signature == MagickCoreSignature);
526  assert(source != (const StringInfo *) NULL);
527  length=string_info->length;
528  if (~length < source->length)
529  ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
530  SetStringInfoLength(string_info,length+source->length);
531  (void) memcpy(string_info->datum+length,source->datum,source->length);
532 }
533 
534 /*
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 % %
537 % %
538 % %
539 % 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 %
540 % %
541 % %
542 % %
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 %
545 % ConfigureFileToStringInfo() returns the contents of a configure file as a
546 % string.
547 %
548 % The format of the ConfigureFileToStringInfo method is:
549 %
550 % StringInfo *ConfigureFileToStringInfo(const char *filename)
551 % ExceptionInfo *exception)
552 %
553 % A description of each parameter follows:
554 %
555 % o filename: the filename.
556 %
557 */
559 {
560  char
561  *string;
562 
563  int
564  file;
565 
567  offset;
568 
569  size_t
570  length;
571 
572  StringInfo
573  *string_info;
574 
575  void
576  *map;
577 
578  assert(filename != (const char *) NULL);
579  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
580  if (file == -1)
581  return((StringInfo *) NULL);
582  offset=(MagickOffsetType) lseek(file,0,SEEK_END);
583  if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
584  {
585  file=close(file)-1;
586  return((StringInfo *) NULL);
587  }
588  length=(size_t) offset;
589  string=(char *) NULL;
590  if (~length >= (MagickPathExtent-1))
591  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
592  sizeof(*string));
593  if (string == (char *) NULL)
594  {
595  file=close(file)-1;
596  return((StringInfo *) NULL);
597  }
598  map=MapBlob(file,ReadMode,0,length);
599  if (map != (void *) NULL)
600  {
601  (void) memcpy(string,map,length);
602  (void) UnmapBlob(map,length);
603  }
604  else
605  {
606  register size_t
607  i;
608 
609  ssize_t
610  count;
611 
612  (void) lseek(file,0,SEEK_SET);
613  for (i=0; i < length; i+=count)
614  {
615  count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
616  SSIZE_MAX));
617  if (count <= 0)
618  {
619  count=0;
620  if (errno != EINTR)
621  break;
622  }
623  }
624  if (i < length)
625  {
626  file=close(file)-1;
627  string=DestroyString(string);
628  return((StringInfo *) NULL);
629  }
630  }
631  string[length]='\0';
632  file=close(file)-1;
633  string_info=AcquireStringInfoContainer();
634  string_info->path=ConstantString(filename);
635  string_info->length=length;
636  string_info->datum=(unsigned char *) string;
637  return(string_info);
638 }
639 
640 /*
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % %
643 % %
644 % %
645 % C o n s t a n t S t r i n g %
646 % %
647 % %
648 % %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650 %
651 % ConstantString() allocates exactly the needed memory for a string and
652 % copies the source string to that memory location. A NULL string pointer
653 % will allocate an empty string containing just the NUL character.
654 %
655 % When finished the string should be freed using DestoryString()
656 %
657 % The format of the ConstantString method is:
658 %
659 % char *ConstantString(const char *source)
660 %
661 % A description of each parameter follows:
662 %
663 % o source: A character string.
664 %
665 */
666 MagickExport char *ConstantString(const char *source)
667 {
668  char
669  *destination;
670 
671  size_t
672  length;
673 
674  length=0;
675  if (source != (char *) NULL)
676  length+=strlen(source);
677  destination=(char *) NULL;
678  if (~length >= 1UL)
679  destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
680  if (destination == (char *) NULL)
681  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
682  if (source != (char *) NULL)
683  (void) memcpy(destination,source,length*sizeof(*destination));
684  destination[length]='\0';
685  return(destination);
686 }
687 
688 /*
689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 % %
691 % %
692 % %
693 % C o p y M a g i c k S t r i n g %
694 % %
695 % %
696 % %
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 %
699 % CopyMagickString() copies the source string to the destination string, with
700 % out exceeding the given pre-declared length.
701 %
702 % The destination buffer is always null-terminated even if the string must be
703 % truncated. The return value is the length of the string.
704 %
705 % The format of the CopyMagickString method is:
706 %
707 % size_t CopyMagickString(const char *magick_restrict destination,
708 % char *magick_restrict source,const size_t length)
709 %
710 % A description of each parameter follows:
711 %
712 % o destination: the destination string.
713 %
714 % o source: the source string.
715 %
716 % o length: the length of the destination string.
717 %
718 */
720  const char *magick_restrict source,const size_t length)
721 {
722  register char
723  *magick_restrict q;
724 
725  register const char
726  *magick_restrict p;
727 
728  register size_t
729  n;
730 
731  p=source;
732  q=destination;
733  for (n=length; n > 4; n-=4)
734  {
735  if (((*q++)=(*p++)) == '\0')
736  return((size_t) (p-source-1));
737  if (((*q++)=(*p++)) == '\0')
738  return((size_t) (p-source-1));
739  if (((*q++)=(*p++)) == '\0')
740  return((size_t) (p-source-1));
741  if (((*q++)=(*p++)) == '\0')
742  return((size_t) (p-source-1));
743  }
744  if (length != 0)
745  {
746  while (--n != 0)
747  if (((*q++)=(*p++)) == '\0')
748  return((size_t) (p-source-1));
749  *q='\0';
750  }
751  return((size_t) (p-source));
752 }
753 
754 /*
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % %
757 % %
758 % %
759 % D e s t r o y S t r i n g %
760 % %
761 % %
762 % %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 %
765 % DestroyString() destroys memory associated with a string.
766 %
767 % The format of the DestroyString method is:
768 %
769 % char *DestroyString(char *string)
770 %
771 % A description of each parameter follows:
772 %
773 % o string: the string.
774 %
775 */
776 MagickExport char *DestroyString(char *string)
777 {
778  return((char *) RelinquishMagickMemory(string));
779 }
780 
781 /*
782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783 % %
784 % %
785 % %
786 % D e s t r o y S t r i n g I n f o %
787 % %
788 % %
789 % %
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 %
792 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
793 %
794 % The format of the DestroyStringInfo method is:
795 %
796 % StringInfo *DestroyStringInfo(StringInfo *string_info)
797 %
798 % A description of each parameter follows:
799 %
800 % o string_info: the string info.
801 %
802 */
804 {
805  assert(string_info != (StringInfo *) NULL);
806  assert(string_info->signature == MagickCoreSignature);
807  if (string_info->datum != (unsigned char *) NULL)
808  string_info->datum=(unsigned char *) RelinquishMagickMemory(
809  string_info->datum);
810  if (string_info->name != (char *) NULL)
811  string_info->name=DestroyString(string_info->name);
812  if (string_info->path != (char *) NULL)
813  string_info->path=DestroyString(string_info->path);
814  string_info->signature=(~MagickCoreSignature);
815  string_info=(StringInfo *) RelinquishMagickMemory(string_info);
816  return(string_info);
817 }
818 
819 /*
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % %
822 % %
823 % %
824 % D e s t r o y S t r i n g L i s t %
825 % %
826 % %
827 % %
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 %
830 % DestroyStringList() zeros memory associated with a string list.
831 %
832 % The format of the DestroyStringList method is:
833 %
834 % char **DestroyStringList(char **list)
835 %
836 % A description of each parameter follows:
837 %
838 % o list: the string list.
839 %
840 */
841 MagickExport char **DestroyStringList(char **list)
842 {
843  register ssize_t
844  i;
845 
846  assert(list != (char **) NULL);
847  for (i=0; list[i] != (char *) NULL; i++)
848  list[i]=DestroyString(list[i]);
849  list=(char **) RelinquishMagickMemory(list);
850  return(list);
851 }
852 
853 /*
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % %
856 % %
857 % %
858 % E s c a p e S t r i n g %
859 % %
860 % %
861 % %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 %
864 % EscapeString() allocates memory for a backslash-escaped version of a
865 % source text string, copies the escaped version of the text to that
866 % memory location while adding backslash characters, and returns the
867 % escaped string.
868 %
869 % The format of the EscapeString method is:
870 %
871 % char *EscapeString(const char *source,const char escape)
872 %
873 % A description of each parameter follows:
874 %
875 % o allocate_string: Method EscapeString returns the escaped string.
876 %
877 % o source: A character string.
878 %
879 % o escape: the quoted string termination character to escape (e.g. '"').
880 %
881 */
882 MagickExport char *EscapeString(const char *source,const char escape)
883 {
884  char
885  *destination;
886 
887  register char
888  *q;
889 
890  register const char
891  *p;
892 
893  size_t
894  length;
895 
896  assert(source != (const char *) NULL);
897  length=0;
898  for (p=source; *p != '\0'; p++)
899  {
900  if ((*p == '\\') || (*p == escape))
901  {
902  if (~length < 1)
903  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
904  length++;
905  }
906  length++;
907  }
908  destination=(char *) NULL;
909  if (~length >= (MagickPathExtent-1))
910  destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
911  sizeof(*destination));
912  if (destination == (char *) NULL)
913  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
914  *destination='\0';
915  q=destination;
916  for (p=source; *p != '\0'; p++)
917  {
918  if ((*p == '\\') || (*p == escape))
919  *q++='\\';
920  *q++=(*p);
921  }
922  *q='\0';
923  return(destination);
924 }
925 
926 /*
927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
928 % %
929 % %
930 % %
931 % F i l e T o S t r i n g %
932 % %
933 % %
934 % %
935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 %
937 % FileToString() returns the contents of a file as a string.
938 %
939 % The format of the FileToString method is:
940 %
941 % char *FileToString(const char *filename,const size_t extent,
942 % ExceptionInfo *exception)
943 %
944 % A description of each parameter follows:
945 %
946 % o filename: the filename.
947 %
948 % o extent: Maximum length of the string.
949 %
950 % o exception: return any errors or warnings in this structure.
951 %
952 */
953 MagickExport char *FileToString(const char *filename,const size_t extent,
955 {
956  size_t
957  length;
958 
959  assert(filename != (const char *) NULL);
960  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
961  assert(exception != (ExceptionInfo *) NULL);
962  return((char *) FileToBlob(filename,extent,&length,exception));
963 }
964 
965 /*
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967 % %
968 % %
969 % %
970 % F i l e T o S t r i n g I n f o %
971 % %
972 % %
973 % %
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 %
976 % FileToStringInfo() returns the contents of a file as a string.
977 %
978 % The format of the FileToStringInfo method is:
979 %
980 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
981 % ExceptionInfo *exception)
982 %
983 % A description of each parameter follows:
984 %
985 % o filename: the filename.
986 %
987 % o extent: Maximum length of the string.
988 %
989 % o exception: return any errors or warnings in this structure.
990 %
991 */
993  const size_t extent,ExceptionInfo *exception)
994 {
995  StringInfo
996  *string_info;
997 
998  assert(filename != (const char *) NULL);
999  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1000  assert(exception != (ExceptionInfo *) NULL);
1001  string_info=AcquireStringInfoContainer();
1002  string_info->path=ConstantString(filename);
1003  string_info->datum=(unsigned char *) FileToBlob(filename,extent,
1004  &string_info->length,exception);
1005  if (string_info->datum == (unsigned char *) NULL)
1006  {
1007  string_info=DestroyStringInfo(string_info);
1008  return((StringInfo *) NULL);
1009  }
1010  return(string_info);
1011 }
1012 
1013 /*
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 % %
1016 % %
1017 % %
1018 % F o r m a t M a g i c k S i z e %
1019 % %
1020 % %
1021 % %
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 %
1024 % FormatMagickSize() converts a size to a human readable format, for example,
1025 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1026 % 1000.
1027 %
1028 % The format of the FormatMagickSize method is:
1029 %
1030 % ssize_t FormatMagickSize(const MagickSizeType size,const char *suffix,
1031 % const size_t length,char *format)
1032 %
1033 % A description of each parameter follows:
1034 %
1035 % o size: convert this size to a human readable format.
1036 %
1037 % o bi: use power of two rather than power of ten.
1038 %
1039 % o suffix: append suffix, typically B or P.
1040 %
1041 % o length: the maximum length of the string.
1042 %
1043 % o format: human readable format.
1044 %
1045 */
1047  const MagickBooleanType bi,const char *suffix,const size_t length,
1048  char *format)
1049 {
1050  char
1051  p[MagickPathExtent],
1052  q[MagickPathExtent];
1053 
1054  const char
1055  **units;
1056 
1057  double
1058  bytes,
1059  extent;
1060 
1061  register ssize_t
1062  i;
1063 
1064  ssize_t
1065  count;
1066 
1067  static const char
1068  *bi_units[] =
1069  {
1070  "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1071  },
1072  *traditional_units[] =
1073  {
1074  "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1075  };
1076 
1077  bytes=1000.0;
1078  units=traditional_units;
1079  if (bi != MagickFalse)
1080  {
1081  bytes=1024.0;
1082  units=bi_units;
1083  }
1084 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1085  extent=(double) ((MagickOffsetType) size);
1086 #else
1087  extent=(double) size;
1088 #endif
1090  extent);
1091  (void) FormatLocaleString(q,MagickPathExtent,"%.20g",extent);
1092  if (strtod(p,(char **) NULL) == strtod(q,(char **) NULL))
1093  {
1094  if (suffix == (const char *) NULL)
1095  count=FormatLocaleString(format,length,"%.20g%s",extent,units[0]);
1096  else
1097  count=FormatLocaleString(format,length,"%.20g%s%s",extent,units[0],
1098  suffix);
1099  return(count);
1100  }
1101  for (i=0; (extent >= bytes) && (units[i+1] != (const char *) NULL); i++)
1102  extent/=bytes;
1103  if (suffix == (const char *) NULL)
1104  count=FormatLocaleString(format,length,"%.*g%s",GetMagickPrecision(),
1105  extent,units[i]);
1106  else
1107  count=FormatLocaleString(format,length,"%.*g%s%s",GetMagickPrecision(),
1108  extent,units[i],suffix);
1109  return(count);
1110 }
1111 
1112 /*
1113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1114 % %
1115 % %
1116 % %
1117 % G e t E n v i r o n m e n t V a l u e %
1118 % %
1119 % %
1120 % %
1121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1122 %
1123 % GetEnvironmentValue() returns the environment string that matches the
1124 % specified name.
1125 %
1126 % The format of the GetEnvironmentValue method is:
1127 %
1128 % char *GetEnvironmentValue(const char *name)
1129 %
1130 % A description of each parameter follows:
1131 %
1132 % o name: the environment name.
1133 %
1134 */
1135 MagickExport char *GetEnvironmentValue(const char *name)
1136 {
1137  const char
1138  *environment;
1139 
1140  environment=getenv(name);
1141  if (environment == (const char *) NULL)
1142  return((char *) NULL);
1143  return(ConstantString(environment));
1144 }
1145 
1146 /*
1147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148 % %
1149 % %
1150 % %
1151 % G e t S t r i n g I n f o D a t u m %
1152 % %
1153 % %
1154 % %
1155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156 %
1157 % GetStringInfoDatum() returns the datum associated with the string.
1158 %
1159 % The format of the GetStringInfoDatum method is:
1160 %
1161 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1162 %
1163 % A description of each parameter follows:
1164 %
1165 % o string_info: the string info.
1166 %
1167 */
1168 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1169 {
1170  assert(string_info != (StringInfo *) NULL);
1171  assert(string_info->signature == MagickCoreSignature);
1172  return(string_info->datum);
1173 }
1174 
1175 /*
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177 % %
1178 % %
1179 % %
1180 % G e t S t r i n g I n f o L e n g t h %
1181 % %
1182 % %
1183 % %
1184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185 %
1186 % GetStringInfoLength() returns the string length.
1187 %
1188 % The format of the GetStringInfoLength method is:
1189 %
1190 % size_t GetStringInfoLength(const StringInfo *string_info)
1191 %
1192 % A description of each parameter follows:
1193 %
1194 % o string_info: the string info.
1195 %
1196 */
1197 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1198 {
1199  assert(string_info != (StringInfo *) NULL);
1200  assert(string_info->signature == MagickCoreSignature);
1201  return(string_info->length);
1202 }
1203 
1204 /*
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206 % %
1207 % %
1208 % %
1209 % G e t S t r i n g I n f o N a m e %
1210 % %
1211 % %
1212 % %
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214 %
1215 % GetStringInfoName() returns the name associated with the string.
1216 %
1217 % The format of the GetStringInfoName method is:
1218 %
1219 % const char *GetStringInfoName(const StringInfo *string_info)
1220 %
1221 % A description of each parameter follows:
1222 %
1223 % o string_info: the string info.
1224 %
1225 */
1226 MagickExport const char *GetStringInfoName(const StringInfo *string_info)
1227 {
1228  assert(string_info != (StringInfo *) NULL);
1229  assert(string_info->signature == MagickCoreSignature);
1230  return(string_info->name);
1231 }
1232 
1233 /*
1234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235 % %
1236 % %
1237 % %
1238 % G e t S t r i n g I n f o P a t h %
1239 % %
1240 % %
1241 % %
1242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1243 %
1244 % GetStringInfoPath() returns the path associated with the string.
1245 %
1246 % The format of the GetStringInfoPath method is:
1247 %
1248 % const char *GetStringInfoPath(const StringInfo *string_info)
1249 %
1250 % A description of each parameter follows:
1251 %
1252 % o string_info: the string info.
1253 %
1254 */
1255 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1256 {
1257  assert(string_info != (StringInfo *) NULL);
1258  assert(string_info->signature == MagickCoreSignature);
1259  return(string_info->path);
1260 }
1261 
1262 /*
1263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 % %
1265 % %
1266 % %
1267 + I n t e r p r e t S i P r e f i x V a l u e %
1268 % %
1269 % %
1270 % %
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1272 %
1273 % InterpretSiPrefixValue() converts the initial portion of the string to a
1274 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1275 % etc.).
1276 %
1277 % The format of the InterpretSiPrefixValue method is:
1278 %
1279 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1280 %
1281 % A description of each parameter follows:
1282 %
1283 % o value: the string value.
1284 %
1285 % o sentinal: if sentinal is not NULL, return a pointer to the character
1286 % after the last character used in the conversion.
1287 %
1288 */
1290  char **magick_restrict sentinal)
1291 {
1292  char
1293  *q;
1294 
1295  double
1296  value;
1297 
1298  value=InterpretLocaleValue(string,&q);
1299  if (q != string)
1300  {
1301  if ((*q >= 'E') && (*q <= 'z'))
1302  {
1303  double
1304  e;
1305 
1306  switch ((int) ((unsigned char) *q))
1307  {
1308  case 'y': e=(-24.0); break;
1309  case 'z': e=(-21.0); break;
1310  case 'a': e=(-18.0); break;
1311  case 'f': e=(-15.0); break;
1312  case 'p': e=(-12.0); break;
1313  case 'n': e=(-9.0); break;
1314  case 'u': e=(-6.0); break;
1315  case 'm': e=(-3.0); break;
1316  case 'c': e=(-2.0); break;
1317  case 'd': e=(-1.0); break;
1318  case 'h': e=2.0; break;
1319  case 'k': e=3.0; break;
1320  case 'K': e=3.0; break;
1321  case 'M': e=6.0; break;
1322  case 'G': e=9.0; break;
1323  case 'T': e=12.0; break;
1324  case 'P': e=15.0; break;
1325  case 'E': e=18.0; break;
1326  case 'Z': e=21.0; break;
1327  case 'Y': e=24.0; break;
1328  default: e=0.0; break;
1329  }
1330  if (e >= MagickEpsilon)
1331  {
1332  if (q[1] == 'i')
1333  {
1334  value*=pow(2.0,e/0.3);
1335  q+=2;
1336  }
1337  else
1338  {
1339  value*=pow(10.0,e);
1340  q++;
1341  }
1342  }
1343  }
1344  if ((*q == 'B') || (*q == 'P'))
1345  q++;
1346  }
1347  if (sentinal != (char **) NULL)
1348  *sentinal=q;
1349  return(value);
1350 }
1351 
1352 /*
1353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % %
1355 % %
1356 % %
1357 % I s S t r i n g T r u e %
1358 % %
1359 % %
1360 % %
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362 %
1363 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1364 % "1". Any other string or undefined returns MagickFalse.
1365 %
1366 % Typically this is used to look at strings (options or artifacts) which
1367 % has a default value of "false", when not defined.
1368 %
1369 % The format of the IsStringTrue method is:
1370 %
1371 % MagickBooleanType IsStringTrue(const char *value)
1372 %
1373 % A description of each parameter follows:
1374 %
1375 % o value: Specifies a pointer to a character array.
1376 %
1377 */
1379 {
1380  if (value == (const char *) NULL)
1381  return(MagickFalse);
1382  if (LocaleCompare(value,"true") == 0)
1383  return(MagickTrue);
1384  if (LocaleCompare(value,"on") == 0)
1385  return(MagickTrue);
1386  if (LocaleCompare(value,"yes") == 0)
1387  return(MagickTrue);
1388  if (LocaleCompare(value,"1") == 0)
1389  return(MagickTrue);
1390  return(MagickFalse);
1391 }
1392 
1393 /*
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % %
1396 % %
1397 % %
1398 % I s S t r i n g F a l s e %
1399 % %
1400 % %
1401 % %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403 %
1404 % IsStringFalse() returns MagickTrue if the value is "false", "off", "no" or
1405 % "0". Any other string or undefined returns MagickFalse.
1406 %
1407 % Typically this is used to look at strings (options or artifacts) which
1408 % has a default value of "true", when it has not been defined.
1409 %
1410 % The format of the IsStringFalse method is:
1411 %
1412 % MagickBooleanType IsStringFalse(const char *value)
1413 %
1414 % A description of each parameter follows:
1415 %
1416 % o value: Specifies a pointer to a character array.
1417 %
1418 */
1420 {
1421  if (value == (const char *) NULL)
1422  return(MagickFalse);
1423  if (LocaleCompare(value,"false") == 0)
1424  return(MagickTrue);
1425  if (LocaleCompare(value,"off") == 0)
1426  return(MagickTrue);
1427  if (LocaleCompare(value,"no") == 0)
1428  return(MagickTrue);
1429  if (LocaleCompare(value,"0") == 0)
1430  return(MagickTrue);
1431  return(MagickFalse);
1432 }
1433 
1434 /*
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % %
1437 % %
1438 % %
1439 % P r i n t S t r i n g I n f o %
1440 % %
1441 % %
1442 % %
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444 %
1445 % PrintStringInfo() prints the string.
1446 %
1447 % The format of the PrintStringInfo method is:
1448 %
1449 % void PrintStringInfo(FILE *file,const char *id,
1450 % const StringInfo *string_info)
1451 %
1452 % A description of each parameter follows:
1453 %
1454 % o file: the file, typically stdout.
1455 %
1456 % o id: the string id.
1457 %
1458 % o string_info: the string info.
1459 %
1460 */
1461 MagickExport void PrintStringInfo(FILE *file,const char *id,
1462  const StringInfo *string_info)
1463 {
1464  register const char
1465  *p;
1466 
1467  register size_t
1468  i,
1469  j;
1470 
1471  assert(id != (const char *) NULL);
1472  assert(string_info != (StringInfo *) NULL);
1473  assert(string_info->signature == MagickCoreSignature);
1474  p=(char *) string_info->datum;
1475  for (i=0; i < string_info->length; i++)
1476  {
1477  if (((int) ((unsigned char) *p) < 32) &&
1478  (isspace((int) ((unsigned char) *p)) == 0))
1479  break;
1480  p++;
1481  }
1482  (void) FormatLocaleFile(file,"%s(%.20g):\n",id,(double) string_info->length);
1483  if (i == string_info->length)
1484  {
1485  for (i=0; i < string_info->length; i++)
1486  (void) fputc(string_info->datum[i],file);
1487  (void) fputc('\n',file);
1488  return;
1489  }
1490  /*
1491  Convert string to a HEX list.
1492  */
1493  p=(char *) string_info->datum;
1494  for (i=0; i < string_info->length; i+=CharsPerLine)
1495  {
1496  (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (CharsPerLine*i));
1497  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1498  {
1499  (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1500  if ((j % 0x04) == 0)
1501  (void) fputc(' ',file);
1502  }
1503  for ( ; j <= CharsPerLine; j++)
1504  {
1505  (void) fputc(' ',file);
1506  (void) fputc(' ',file);
1507  if ((j % 0x04) == 0)
1508  (void) fputc(' ',file);
1509  }
1510  (void) fputc(' ',file);
1511  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1512  {
1513  if (isprint((int) ((unsigned char) *p)) != 0)
1514  (void) fputc(*p,file);
1515  else
1516  (void) fputc('-',file);
1517  p++;
1518  }
1519  (void) fputc('\n',file);
1520  }
1521 }
1522 
1523 /*
1524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % %
1526 % %
1527 % %
1528 % R e s e t S t r i n g I n f o %
1529 % %
1530 % %
1531 % %
1532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1533 %
1534 % ResetStringInfo() reset the string to all null bytes.
1535 %
1536 % The format of the ResetStringInfo method is:
1537 %
1538 % void ResetStringInfo(StringInfo *string_info)
1539 %
1540 % A description of each parameter follows:
1541 %
1542 % o string_info: the string info.
1543 %
1544 */
1546 {
1547  assert(string_info != (StringInfo *) NULL);
1548  assert(string_info->signature == MagickCoreSignature);
1549  (void) memset(string_info->datum,0,string_info->length);
1550 }
1551 
1552 /*
1553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1554 % %
1555 % %
1556 % %
1557 % S a n t i z e S t r i n g %
1558 % %
1559 % %
1560 % %
1561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1562 %
1563 % SanitizeString() returns n new string removes all characters except
1564 % letters, digits and !#$%&'*+-=?^_`{|}~@.[].
1565 %
1566 % Free the sanitized string with DestroyString().
1567 %
1568 % The format of the SanitizeString method is:
1569 %
1570 % char *SanitizeString(const char *source)
1571 %
1572 % A description of each parameter follows:
1573 %
1574 % o source: A character string.
1575 %
1576 */
1577 MagickExport char *SanitizeString(const char *source)
1578 {
1579  char
1580  *sanitize_source;
1581 
1582  const char
1583  *q;
1584 
1585  register char
1586  *p;
1587 
1588  static char
1589  allowlist[] =
1590  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1591  "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1592 
1593  sanitize_source=AcquireString(source);
1594  p=sanitize_source;
1595  q=sanitize_source+strlen(sanitize_source);
1596  for (p+=strspn(p,allowlist); p != q; p+=strspn(p,allowlist))
1597  *p='_';
1598  return(sanitize_source);
1599 }
1600 
1601 /*
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 % %
1604 % %
1605 % %
1606 % S e t S t r i n g I n f o %
1607 % %
1608 % %
1609 % %
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 %
1612 % SetStringInfo() copies the source string to the destination string.
1613 %
1614 % The format of the SetStringInfo method is:
1615 %
1616 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1617 %
1618 % A description of each parameter follows:
1619 %
1620 % o string_info: the string info.
1621 %
1622 % o source: the source string.
1623 %
1624 */
1626  const StringInfo *source)
1627 {
1628  assert(string_info != (StringInfo *) NULL);
1629  assert(string_info->signature == MagickCoreSignature);
1630  assert(source != (StringInfo *) NULL);
1631  assert(source->signature == MagickCoreSignature);
1632  if (string_info->length == 0)
1633  return;
1634  (void) memset(string_info->datum,0,string_info->length);
1635  (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1636  source->length));
1637 }
1638 
1639 /*
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1641 % %
1642 % %
1643 % %
1644 % S e t S t r i n g I n f o D a t u m %
1645 % %
1646 % %
1647 % %
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1649 %
1650 % SetStringInfoDatum() copies bytes from the source string for the length of
1651 % the destination string.
1652 %
1653 % The format of the SetStringInfoDatum method is:
1654 %
1655 % void SetStringInfoDatum(StringInfo *string_info,
1656 % const unsigned char *source)
1657 %
1658 % A description of each parameter follows:
1659 %
1660 % o string_info: the string info.
1661 %
1662 % o source: the source string.
1663 %
1664 */
1666  const unsigned char *source)
1667 {
1668  assert(string_info != (StringInfo *) NULL);
1669  assert(string_info->signature == MagickCoreSignature);
1670  if (string_info->length != 0)
1671  (void) memcpy(string_info->datum,source,string_info->length);
1672 }
1673 
1674 /*
1675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % %
1677 % %
1678 % %
1679 % S e t S t r i n g I n f o L e n g t h %
1680 % %
1681 % %
1682 % %
1683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1684 %
1685 % SetStringInfoLength() set the string length to the specified value.
1686 %
1687 % The format of the SetStringInfoLength method is:
1688 %
1689 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1690 %
1691 % A description of each parameter follows:
1692 %
1693 % o string_info: the string info.
1694 %
1695 % o length: the string length.
1696 %
1697 */
1699  const size_t length)
1700 {
1701  assert(string_info != (StringInfo *) NULL);
1702  assert(string_info->signature == MagickCoreSignature);
1703  if (string_info->length == length)
1704  return;
1705  if (~length < MagickPathExtent)
1706  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1707  string_info->length=length;
1708  if (string_info->datum == (unsigned char *) NULL)
1709  string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1710  MagickPathExtent,sizeof(*string_info->datum));
1711  else
1712  string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1713  length+MagickPathExtent,sizeof(*string_info->datum));
1714  if (string_info->datum == (unsigned char *) NULL)
1715  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1716 }
1717 
1718 /*
1719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1720 % %
1721 % %
1722 % %
1723 % S e t S t r i n g I n f o N a m e %
1724 % %
1725 % %
1726 % %
1727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728 %
1729 % SetStringInfoName() sets the name associated with the string.
1730 %
1731 % The format of the SetStringInfoName method is:
1732 %
1733 % void SetStringInfoName(StringInfo *string_info,const char *name)
1734 %
1735 % A description of each parameter follows:
1736 %
1737 % o string_info: the string info.
1738 %
1739 % o name: the name.
1740 %
1741 */
1742 MagickExport void SetStringInfoName(StringInfo *string_info,const char *name)
1743 {
1744  assert(string_info != (StringInfo *) NULL);
1745  assert(string_info->signature == MagickCoreSignature);
1746  assert(name != (const char *) NULL);
1747  string_info->name=ConstantString(name);
1748 }
1749 
1750 /*
1751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1752 % %
1753 % %
1754 % %
1755 % S e t S t r i n g I n f o P a t h %
1756 % %
1757 % %
1758 % %
1759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1760 %
1761 % SetStringInfoPath() sets the path associated with the string.
1762 %
1763 % The format of the SetStringInfoPath method is:
1764 %
1765 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1766 %
1767 % A description of each parameter follows:
1768 %
1769 % o string_info: the string info.
1770 %
1771 % o path: the path.
1772 %
1773 */
1774 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1775 {
1776  assert(string_info != (StringInfo *) NULL);
1777  assert(string_info->signature == MagickCoreSignature);
1778  assert(path != (const char *) NULL);
1779  string_info->path=ConstantString(path);
1780 }
1781 
1782 /*
1783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1784 % %
1785 % %
1786 % %
1787 % S p l i t S t r i n g I n f o %
1788 % %
1789 % %
1790 % %
1791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1792 %
1793 % SplitStringInfo() splits a string into two and returns it.
1794 %
1795 % The format of the SplitStringInfo method is:
1796 %
1797 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1798 %
1799 % A description of each parameter follows:
1800 %
1801 % o string_info: the string info.
1802 %
1803 */
1805  const size_t offset)
1806 {
1807  StringInfo
1808  *split_info;
1809 
1810  assert(string_info != (StringInfo *) NULL);
1811  assert(string_info->signature == MagickCoreSignature);
1812  if (offset > string_info->length)
1813  return((StringInfo *) NULL);
1814  split_info=AcquireStringInfo(offset);
1815  SetStringInfo(split_info,string_info);
1816  (void) memmove(string_info->datum,string_info->datum+offset,
1817  string_info->length-offset+MagickPathExtent);
1818  SetStringInfoLength(string_info,string_info->length-offset);
1819  return(split_info);
1820 }
1821 
1822 /*
1823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1824 % %
1825 % %
1826 % %
1827 % S t r i n g I n f o T o S t r i n g %
1828 % %
1829 % %
1830 % %
1831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832 %
1833 % StringInfoToString() converts a string info string to a C string.
1834 %
1835 % The format of the StringInfoToString method is:
1836 %
1837 % char *StringInfoToString(const StringInfo *string_info)
1838 %
1839 % A description of each parameter follows:
1840 %
1841 % o string_info: the string.
1842 %
1843 */
1844 MagickExport char *StringInfoToString(const StringInfo *string_info)
1845 {
1846  char
1847  *string;
1848 
1849  size_t
1850  length;
1851 
1852  string=(char *) NULL;
1853  length=string_info->length;
1854  if (~length >= (MagickPathExtent-1))
1855  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1856  sizeof(*string));
1857  if (string == (char *) NULL)
1858  return((char *) NULL);
1859  (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1860  string[length]='\0';
1861  return(string);
1862 }
1863 
1864 /*
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866 % %
1867 % %
1868 % %
1869 % S t r i n g I n f o T o H e x S t r i n g %
1870 % %
1871 % %
1872 % %
1873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1874 %
1875 % StringInfoToHexString() converts a string info string to a C string.
1876 %
1877 % The format of the StringInfoToHexString method is:
1878 %
1879 % char *StringInfoToHexString(const StringInfo *string_info)
1880 %
1881 % A description of each parameter follows:
1882 %
1883 % o string_info: the string.
1884 %
1885 */
1887 {
1888  char
1889  *string;
1890 
1891  register const unsigned char
1892  *p;
1893 
1894  register ssize_t
1895  i;
1896 
1897  register unsigned char
1898  *q;
1899 
1900  size_t
1901  length;
1902 
1903  unsigned char
1904  hex_digits[16];
1905 
1906  length=string_info->length;
1907  if (~length < MagickPathExtent)
1908  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1909  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,2*
1910  sizeof(*string));
1911  if (string == (char *) NULL)
1912  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1913  hex_digits[0]='0';
1914  hex_digits[1]='1';
1915  hex_digits[2]='2';
1916  hex_digits[3]='3';
1917  hex_digits[4]='4';
1918  hex_digits[5]='5';
1919  hex_digits[6]='6';
1920  hex_digits[7]='7';
1921  hex_digits[8]='8';
1922  hex_digits[9]='9';
1923  hex_digits[10]='a';
1924  hex_digits[11]='b';
1925  hex_digits[12]='c';
1926  hex_digits[13]='d';
1927  hex_digits[14]='e';
1928  hex_digits[15]='f';
1929  p=string_info->datum;
1930  q=(unsigned char *) string;
1931  for (i=0; i < (ssize_t) string_info->length; i++)
1932  {
1933  *q++=hex_digits[(*p >> 4) & 0x0f];
1934  *q++=hex_digits[*p & 0x0f];
1935  p++;
1936  }
1937  *q='\0';
1938  return(string);
1939 }
1940 
1941 /*
1942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1943 % %
1944 % %
1945 % %
1946 % S t r i n g T o A r g v %
1947 % %
1948 % %
1949 % %
1950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951 %
1952 % StringToArgv() converts a text string into command line arguments.
1953 % The 'argv' array of arguments, is returned while the number of arguments
1954 % is returned via the provided integer variable pointer.
1955 %
1956 % Simple 'word' tokenizer, which allows for each word to be optionally
1957 % quoted. However it will not allow use of partial quotes, or escape
1958 % characters.
1959 %
1960 % The format of the StringToArgv method is:
1961 %
1962 % char **StringToArgv(const char *text,int *argc)
1963 %
1964 % A description of each parameter follows:
1965 %
1966 % o argv: Method StringToArgv returns the string list unless an error
1967 % occurs, otherwise NULL.
1968 %
1969 % o text: Specifies the string to segment into a list.
1970 %
1971 % o argc: This integer pointer returns the number of arguments in the
1972 % list.
1973 %
1974 */
1975 MagickExport char **StringToArgv(const char *text,int *argc)
1976 {
1977  char
1978  **argv;
1979 
1980  register const char
1981  *p,
1982  *q;
1983 
1984  register ssize_t
1985  i;
1986 
1987  *argc=0;
1988  if (text == (char *) NULL)
1989  return((char **) NULL);
1990  /*
1991  Determine the number of arguments.
1992  */
1993  for (p=text; *p != '\0'; )
1994  {
1995  while (isspace((int) ((unsigned char) *p)) != 0)
1996  p++;
1997  if (*p == '\0')
1998  break;
1999  (*argc)++;
2000  if (*p == '"')
2001  for (p++; (*p != '"') && (*p != '\0'); p++) ;
2002  if (*p == '\'')
2003  for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2004  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2005  p++;
2006  }
2007  (*argc)++;
2008  argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2009  if (argv == (char **) NULL)
2010  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2011  /*
2012  Convert string to an ASCII list.
2013  */
2014  argv[0]=AcquireString("magick");
2015  p=text;
2016  for (i=1; i < (ssize_t) *argc; i++)
2017  {
2018  while (isspace((int) ((unsigned char) *p)) != 0)
2019  p++;
2020  q=p;
2021  if (*q == '"')
2022  {
2023  p++;
2024  for (q++; (*q != '"') && (*q != '\0'); q++) ;
2025  }
2026  else
2027  if (*q == '\'')
2028  {
2029  p++;
2030  for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2031  }
2032  else
2033  while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2034  q++;
2035  argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
2036  sizeof(**argv));
2037  if (argv[i] == (char *) NULL)
2038  {
2039  for (i--; i >= 0; i--)
2040  argv[i]=DestroyString(argv[i]);
2041  argv=(char **) RelinquishMagickMemory(argv);
2043  "UnableToConvertStringToARGV");
2044  }
2045  (void) memcpy(argv[i],p,(size_t) (q-p));
2046  argv[i][q-p]='\0';
2047  p=q;
2048  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2049  p++;
2050  }
2051  argv[i]=(char *) NULL;
2052  return(argv);
2053 }
2054 
2055 /*
2056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2057 % %
2058 % %
2059 % %
2060 % S t r i n g T o A r r a y O f D o u b l e s %
2061 % %
2062 % %
2063 % %
2064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065 %
2066 % StringToArrayOfDoubles() converts a string of space or comma separated
2067 % numbers into array of floating point numbers (doubles). Any number that
2068 % failes to parse properly will produce a syntax error. As will two commas
2069 % without a number between them. However a final comma at the end will
2070 % not be regarded as an error so as to simplify automatic list generation.
2071 %
2072 % A NULL value is returned on syntax or memory errors.
2073 %
2074 % Use RelinquishMagickMemory() to free returned array when finished.
2075 %
2076 % The format of the StringToArrayOfDoubles method is:
2077 %
2078 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2079 % ExceptionInfo *exception)
2080 %
2081 % A description of each parameter follows:
2082 %
2083 % o string: the string containing the comma/space separated values.
2084 %
2085 % o count: returns number of arguments in returned array
2086 %
2087 % o exception: return any errors or warnings in this structure.
2088 %
2089 */
2090 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2092 {
2093  char
2094  *q;
2095 
2096  const char
2097  *p;
2098 
2099  double
2100  *array;
2101 
2102  register ssize_t
2103  i;
2104 
2105  /*
2106  Determine count of values, and check syntax.
2107  */
2108  assert(exception != (ExceptionInfo *) NULL);
2110  *count=0;
2111  if (string == (char *) NULL)
2112  return((double *) NULL); /* no value found */
2113  i=0;
2114  p=string;
2115  while (*p != '\0')
2116  {
2117  (void) StringToDouble(p,&q); /* get value - ignores leading space */
2118  if (p == q)
2119  return((double *) NULL); /* no value found */
2120  p=q;
2121  i++; /* increment value count */
2122  while (isspace((int) ((unsigned char) *p)) != 0)
2123  p++; /* skip spaces */
2124  if (*p == ',')
2125  p++; /* skip comma */
2126  while (isspace((int) ((unsigned char) *p)) != 0)
2127  p++; /* and more spaces */
2128  }
2129  /*
2130  Allocate floating point argument list.
2131  */
2132  *count=i;
2133  array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2134  if (array == (double *) NULL)
2135  {
2137  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2138  return((double *) NULL);
2139  }
2140  /*
2141  Fill in the floating point values.
2142  */
2143  i=0;
2144  p=string;
2145  while ((*p != '\0') && (i < *count))
2146  {
2147  array[i++]=StringToDouble(p,&q);
2148  p=q;
2149  while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2150  p++;
2151  }
2152  return(array);
2153 }
2154 
2155 /*
2156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2157 % %
2158 % %
2159 % %
2160 + S t r i n g T o k e n %
2161 % %
2162 % %
2163 % %
2164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2165 %
2166 % StringToken() looks for any one of given delimiters and splits the string
2167 % into two separate strings by replacing the delimiter character found with a
2168 % null character.
2169 %
2170 % The given string pointer is changed to point to the string following the
2171 % delimiter character found, or NULL. A pointer to the start of the
2172 % string is returned, representing the token before the delimiter.
2173 %
2174 % In may ways this is equivent to the strtok() C library function, but with
2175 % multiple delimiter characters rather than a delimiter string.
2176 %
2177 % The format of the StringToken method is:
2178 %
2179 % char *StringToken(const char *delimiters,char **string)
2180 %
2181 % A description of each parameter follows:
2182 %
2183 % o delimiters: one or more delimiters.
2184 %
2185 % o string: return the first token in the string. If none is found, return
2186 % NULL.
2187 %
2188 */
2189 MagickExport char *StringToken(const char *delimiters,char **string)
2190 {
2191  char
2192  *q;
2193 
2194  register char
2195  *p;
2196 
2197  register const char
2198  *r;
2199 
2200  register int
2201  c,
2202  d;
2203 
2204  p=(*string);
2205  if (p == (char *) NULL)
2206  return((char *) NULL);
2207  q=p;
2208  for ( ; ; )
2209  {
2210  c=(*p++);
2211  r=delimiters;
2212  do
2213  {
2214  d=(*r++);
2215  if (c == d)
2216  {
2217  if (c == '\0')
2218  p=(char *) NULL;
2219  else
2220  p[-1]='\0';
2221  *string=p;
2222  return(q);
2223  }
2224  } while (d != '\0');
2225  }
2226 }
2227 
2228 /*
2229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2230 % %
2231 % %
2232 % %
2233 % S t r i n g T o L i s t %
2234 % %
2235 % %
2236 % %
2237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2238 %
2239 % StringToList() converts a text string into a list by segmenting the text
2240 % string at each carriage return discovered. The list is converted to HEX
2241 % characters if any control characters are discovered within the text string.
2242 %
2243 % The format of the StringToList method is:
2244 %
2245 % char **StringToList(const char *text)
2246 %
2247 % A description of each parameter follows:
2248 %
2249 % o text: Specifies the string to segment into a list.
2250 %
2251 */
2252 MagickExport char **StringToList(const char *text)
2253 {
2254  return(StringToStrings(text,(size_t *) NULL));
2255 }
2256 
2257 /*
2258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2259 % %
2260 % %
2261 % %
2262 % S t r i n g T o S t r i n g s %
2263 % %
2264 % %
2265 % %
2266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2267 %
2268 % StringToStrings() converts a text string into a list by segmenting the text
2269 % string at each carriage return discovered. The list is converted to HEX
2270 % characters if any control characters are discovered within the text string.
2271 %
2272 % The format of the StringToList method is:
2273 %
2274 % char **StringToList(const char *text,size_t *lines)
2275 %
2276 % A description of each parameter follows:
2277 %
2278 % o text: Specifies the string to segment into a list.
2279 %
2280 % o count: Return value for the number of items in the list.
2281 %
2282 */
2283 MagickExport char **StringToStrings(const char *text,size_t *count)
2284 {
2285  char
2286  **textlist;
2287 
2288  register const char
2289  *p;
2290 
2291  register ssize_t
2292  i;
2293 
2294  size_t
2295  lines;
2296 
2297  if (text == (char *) NULL)
2298  {
2299  if (count != (size_t *) NULL)
2300  *count=0;
2301  return((char **) NULL);
2302  }
2303  for (p=text; *p != '\0'; p++)
2304  if (((int) ((unsigned char) *p) < 32) &&
2305  (isspace((int) ((unsigned char) *p)) == 0))
2306  break;
2307  if (*p == '\0')
2308  {
2309  register const char
2310  *q;
2311 
2312  /*
2313  Convert string to an ASCII list.
2314  */
2315  lines=1;
2316  for (p=text; *p != '\0'; p++)
2317  if (*p == '\n')
2318  lines++;
2319  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2320  sizeof(*textlist));
2321  if (textlist == (char **) NULL)
2322  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2323  p=text;
2324  for (i=0; i < (ssize_t) lines; i++)
2325  {
2326  for (q=p; *q != '\0'; q++)
2327  if ((*q == '\r') || (*q == '\n'))
2328  break;
2329  textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
2330  sizeof(**textlist));
2331  if (textlist[i] == (char *) NULL)
2332  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2333  (void) memcpy(textlist[i],p,(size_t) (q-p));
2334  textlist[i][q-p]='\0';
2335  if (*q == '\r')
2336  q++;
2337  p=q+1;
2338  }
2339  }
2340  else
2341  {
2342  char
2343  hex_string[MagickPathExtent];
2344 
2345  register char
2346  *q;
2347 
2348  register ssize_t
2349  j;
2350 
2351  /*
2352  Convert string to a HEX list.
2353  */
2354  lines=(size_t) (strlen(text)/CharsPerLine)+1;
2355  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2356  sizeof(*textlist));
2357  if (textlist == (char **) NULL)
2358  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2359  p=text;
2360  for (i=0; i < (ssize_t) lines; i++)
2361  {
2362  size_t
2363  length;
2364 
2365  textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2366  sizeof(**textlist));
2367  if (textlist[i] == (char *) NULL)
2368  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2369  (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2370  (long) (CharsPerLine*i));
2371  q=textlist[i]+strlen(textlist[i]);
2372  length=strlen(p);
2373  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2374  {
2375  (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2376  (void) CopyMagickString(q,hex_string,MagickPathExtent);
2377  q+=2;
2378  if ((j % 0x04) == 0)
2379  *q++=' ';
2380  }
2381  for ( ; j <= CharsPerLine; j++)
2382  {
2383  *q++=' ';
2384  *q++=' ';
2385  if ((j % 0x04) == 0)
2386  *q++=' ';
2387  }
2388  *q++=' ';
2389  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2390  {
2391  if (isprint((int) ((unsigned char) *p)) != 0)
2392  *q++=(*p);
2393  else
2394  *q++='-';
2395  p++;
2396  }
2397  *q='\0';
2398  textlist[i]=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
2399  textlist[i]+1),sizeof(**textlist));
2400  if (textlist[i] == (char *) NULL)
2401  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2402  }
2403  }
2404  if (count != (size_t *) NULL)
2405  *count=lines;
2406  textlist[i]=(char *) NULL;
2407  return(textlist);
2408 }
2409 
2410 /*
2411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2412 % %
2413 % %
2414 % %
2415 % S t r i n g T o S t r i n g I n f o %
2416 % %
2417 % %
2418 % %
2419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2420 %
2421 % StringToStringInfo() converts a string to a StringInfo type.
2422 %
2423 % The format of the StringToStringInfo method is:
2424 %
2425 % StringInfo *StringToStringInfo(const char *string)
2426 %
2427 % A description of each parameter follows:
2428 %
2429 % o string: The string.
2430 %
2431 */
2433 {
2434  StringInfo
2435  *string_info;
2436 
2437  assert(string != (const char *) NULL);
2438  string_info=AcquireStringInfo(strlen(string));
2439  SetStringInfoDatum(string_info,(const unsigned char *) string);
2440  return(string_info);
2441 }
2442 
2443 /*
2444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2445 % %
2446 % %
2447 % %
2448 % S t r i p S t r i n g %
2449 % %
2450 % %
2451 % %
2452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2453 %
2454 % StripString() strips any whitespace or quotes from the beginning and end of
2455 % a string of characters.
2456 %
2457 % The format of the StripString method is:
2458 %
2459 % void StripString(char *message)
2460 %
2461 % A description of each parameter follows:
2462 %
2463 % o message: Specifies an array of characters.
2464 %
2465 */
2466 MagickExport void StripString(char *message)
2467 {
2468  register char
2469  *p,
2470  *q;
2471 
2472  size_t
2473  length;
2474 
2475  assert(message != (char *) NULL);
2476  if (*message == '\0')
2477  return;
2478  length=strlen(message);
2479  p=message;
2480  while (isspace((int) ((unsigned char) *p)) != 0)
2481  p++;
2482  if ((*p == '\'') || (*p == '"'))
2483  p++;
2484  q=message+length-1;
2485  while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2486  q--;
2487  if (q > p)
2488  if ((*q == '\'') || (*q == '"'))
2489  q--;
2490  (void) memmove(message,p,(size_t) (q-p+1));
2491  message[q-p+1]='\0';
2492  for (p=message; *p != '\0'; p++)
2493  if (*p == '\n')
2494  *p=' ';
2495 }
2496 
2497 /*
2498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2499 % %
2500 % %
2501 % %
2502 % S u b s t i t u t e S t r i n g %
2503 % %
2504 % %
2505 % %
2506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2507 %
2508 % SubstituteString() performs string substitution on a string, replacing the
2509 % string with the substituted version. Buffer must be allocated from the heap.
2510 % If the string is matched and status, MagickTrue is returned otherwise
2511 % MagickFalse.
2512 %
2513 % The format of the SubstituteString method is:
2514 %
2515 % MagickBooleanType SubstituteString(char **string,const char *search,
2516 % const char *replace)
2517 %
2518 % A description of each parameter follows:
2519 %
2520 % o string: the string to perform replacements on; replaced with new
2521 % allocation if a replacement is made.
2522 %
2523 % o search: search for this string.
2524 %
2525 % o replace: replace any matches with this string.
2526 %
2527 */
2529  const char *search,const char *replace)
2530 {
2532  status;
2533 
2534  register char
2535  *p;
2536 
2537  size_t
2538  extent,
2539  replace_extent,
2540  search_extent;
2541 
2542  ssize_t
2543  offset;
2544 
2545  status=MagickFalse;
2546  search_extent=0,
2547  replace_extent=0;
2548  for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2549  {
2550  if (search_extent == 0)
2551  search_extent=strlen(search);
2552  if (strncmp(p,search,search_extent) != 0)
2553  continue;
2554  /*
2555  We found a match.
2556  */
2557  status=MagickTrue;
2558  if (replace_extent == 0)
2559  replace_extent=strlen(replace);
2560  if (replace_extent > search_extent)
2561  {
2562  /*
2563  Make room for the replacement string.
2564  */
2565  offset=(ssize_t) (p-(*string));
2566  extent=strlen(*string)+replace_extent-search_extent+1;
2567  *string=(char *) ResizeQuantumMemory(*string,extent+MagickPathExtent,
2568  sizeof(*p));
2569  if (*string == (char *) NULL)
2570  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2571  p=(*string)+offset;
2572  }
2573  /*
2574  Replace string.
2575  */
2576  if (search_extent != replace_extent)
2577  (void) memmove(p+replace_extent,p+search_extent,
2578  strlen(p+search_extent)+1);
2579  (void) memcpy(p,replace,replace_extent);
2580  p+=replace_extent-1;
2581  }
2582  return(status);
2583 }
#define magick_restrict
Definition: MagickCore.h:41
MagickExport double InterpretSiPrefixValue(const char *magick_restrict string, char **magick_restrict sentinal)
Definition: string.c:1289
MagickExport ssize_t FormatMagickSize(const MagickSizeType size, const MagickBooleanType bi, const char *suffix, const size_t length, char *format)
Definition: string.c:1046
Definition: blob.h:30
MagickExport MagickBooleanType IsStringFalse(const char *value)
Definition: string.c:1419
MagickExport int CompareStringInfo(const StringInfo *target, const StringInfo *source)
Definition: string.c:344
MagickExport StringInfo * StringToStringInfo(const char *string)
Definition: string.c:2432
MagickExport size_t ConcatenateMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:392
MagickExport void SetStringInfoPath(StringInfo *string_info, const char *path)
Definition: string.c:1774
static StringInfo * AcquireStringInfoContainer()
Definition: string.c:140
char * map
Definition: stream.c:84
#define ThrowFatalException(severity, tag)
size_t signature
Definition: exception.h:123
unsigned char * datum
Definition: string_.h:33
MagickExport char * EscapeString(const char *source, const char escape)
Definition: string.c:882
static double StringToDouble(const char *magick_restrict string, char **magick_restrict sentinal)
MagickExport char * SanitizeString(const char *source)
Definition: string.c:1577
MagickExport void ConcatenateStringInfo(StringInfo *string_info, const StringInfo *source)
Definition: string.c:518
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:467
MagickExport size_t CopyMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:719
#define MagickEpsilon
Definition: magick-type.h:118
MagickExport void * ResizeQuantumMemory(void *memory, const size_t count, const size_t quantum)
Definition: memory.c:1420
#define O_BINARY
Definition: studio.h:333
MagickExport void StripString(char *message)
Definition: string.c:2466
Definition: log.h:52
MagickExport char * FileToString(const char *filename, const size_t extent, ExceptionInfo *exception)
Definition: string.c:953
ssize_t MagickOffsetType
Definition: magick-type.h:137
#define MagickCoreSignature
MagickExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition: string.c:1168
MagickExport ssize_t FormatLocaleFile(FILE *file, const char *magick_restrict format,...)
Definition: locale.c:372
MagickBooleanType
Definition: magick-type.h:173
MagickExport char ** StringToList(const char *text)
Definition: string.c:2252
MagickExport char * AcquireString(const char *source)
Definition: string.c:94
MagickExport void * FileToBlob(const char *filename, const size_t extent, size_t *length, ExceptionInfo *exception)
Definition: blob.c:1393
MagickExport void * AcquireCriticalMemory(const size_t size)
Definition: memory.c:599
MagickExport StringInfo * FileToStringInfo(const char *filename, const size_t extent, ExceptionInfo *exception)
Definition: string.c:992
MagickExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition: string.c:803
MagickExport void ResetStringInfo(StringInfo *string_info)
Definition: string.c:1545
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:638
size_t MagickSizeType
Definition: magick-type.h:138
#define MagickPathExtent
MagickExport void PrintStringInfo(FILE *file, const char *id, const StringInfo *string_info)
Definition: string.c:1461
MagickExport MagickBooleanType IsStringTrue(const char *value)
Definition: string.c:1378
MagickExport int GetMagickPrecision(void)
Definition: magick.c:942
MagickExport StringInfo * BlobToStringInfo(const void *blob, const size_t length)
Definition: string.c:193
MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *tag, const char *format,...)
Definition: exception.c:1145
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1660
static int open_utf8(const char *path, int flags, mode_t mode)
ExceptionInfo * exception
Definition: stream.c:99
MagickExport double * StringToArrayOfDoubles(const char *string, ssize_t *count, ExceptionInfo *exception)
Definition: string.c:2090
MagickExport MagickBooleanType SubstituteString(char **string, const char *search, const char *replace)
Definition: string.c:2528
MagickExport char * GetEnvironmentValue(const char *name)
Definition: string.c:1135
MagickExport StringInfo * ConfigureFileToStringInfo(const char *filename)
Definition: string.c:558
MagickExport StringInfo * AcquireStringInfo(const size_t length)
Definition: string.c:151
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1403
#define GetMagickModule()
Definition: log.h:28
MagickExport const char * GetStringInfoPath(const StringInfo *string_info)
Definition: string.c:1255
MagickExport void SetStringInfoName(StringInfo *string_info, const char *name)
Definition: string.c:1742
MagickExport char * StringToken(const char *delimiters, char **string)
Definition: string.c:2189
MagickExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition: string.c:1698
MagickExport char * DestroyString(char *string)
Definition: string.c:776
char * name
Definition: string_.h:40
MagickExport char ** DestroyStringList(char **list)
Definition: string.c:841
#define MagickMin(x, y)
Definition: image-private.h:37
MagickExport MagickBooleanType ConcatenateString(char **magick_restrict destination, const char *magick_restrict source)
Definition: string.c:458
char * path
Definition: string_.h:30
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1135
MagickExport MagickBooleanType UnmapBlob(void *, const size_t)
Definition: blob.c:5552
size_t signature
Definition: string_.h:36
MagickExport char * CloneString(char **destination, const char *source)
Definition: string.c:250
MagickExport char ** StringToStrings(const char *text, size_t *count)
Definition: string.c:2283
#define CharsPerLine
Definition: string.c:64
size_t length
Definition: string_.h:36
#define MagickExport
MagickExport StringInfo * SplitStringInfo(StringInfo *string_info, const size_t offset)
Definition: string.c:1804
MagickExport char * StringInfoToString(const StringInfo *string_info)
Definition: string.c:1844
MagickExport char * StringInfoToHexString(const StringInfo *string_info)
Definition: string.c:1886
MagickExport char ** StringToArgv(const char *text, int *argc)
Definition: string.c:1975
MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition: string.c:1197
MagickExport const char * GetStringInfoName(const StringInfo *string_info)
Definition: string.c:1226
MagickExport char * ConstantString(const char *source)
Definition: string.c:666
MagickExport StringInfo * CloneStringInfo(const StringInfo *string_info)
Definition: string.c:302
MagickExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
Definition: string.c:1665
MagickExport void * MapBlob(int, const MapMode, const MagickOffsetType, const size_t)
MagickExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition: string.c:1625
MagickExport double InterpretLocaleValue(const char *magick_restrict string, char **magick_restrict sentinal)
Definition: locale.c:971