MagickCore  7.1.0
Convert, Edit, Or Compose Bitmap Images
string.c
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 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"
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/memory-private.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/property.h"
55 #include "MagickCore/resource_.h"
56 #include "MagickCore/signature-private.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/utility-private.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 
140 static StringInfo *AcquireStringInfoContainer()
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 
151 MagickExport StringInfo *AcquireStringInfo(const size_t length)
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 */
302 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
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 
344 MagickExport int CompareStringInfo(const StringInfo *target,
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 */
392 MagickExport size_t ConcatenateMagickString(char *magick_restrict destination,
393  const char *magick_restrict source,const size_t length)
394 {
395  char
396  *magick_restrict q;
397 
398  const char
399  *magick_restrict p;
400 
401  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 */
458 MagickExport MagickBooleanType ConcatenateString(
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,
483  OverAllocateMemory(length+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 */
518 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
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  length+=source->length;
531  if (~length < MagickPathExtent)
532  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
533  if (string_info->datum == (unsigned char *) NULL)
534  string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
535  MagickPathExtent,sizeof(*string_info->datum));
536  else
537  string_info->datum=(unsigned char *) ResizeQuantumMemory(
538  string_info->datum,OverAllocateMemory(length+MagickPathExtent),
539  sizeof(*string_info->datum));
540  if (string_info->datum == (unsigned char *) NULL)
541  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
542  (void) memcpy(string_info->datum+string_info->length,source->datum,source->length);
543  string_info->length=length;
544 }
545 ␌
546 /*
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % %
549 % %
550 % %
551 % C o n f i g u r e F i l e T o S t r i n g I n f o %
552 % %
553 % %
554 % %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 %
557 % ConfigureFileToStringInfo() returns the contents of a configure file as a
558 % string.
559 %
560 % The format of the ConfigureFileToStringInfo method is:
561 %
562 % StringInfo *ConfigureFileToStringInfo(const char *filename)
563 % ExceptionInfo *exception)
564 %
565 % A description of each parameter follows:
566 %
567 % o filename: the filename.
568 %
569 */
570 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
571 {
572  char
573  *string;
574 
575  int
576  file;
577 
578  MagickOffsetType
579  offset;
580 
581  size_t
582  length;
583 
584  StringInfo
585  *string_info;
586 
587  void
588  *map;
589 
590  assert(filename != (const char *) NULL);
591  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
592  if (file == -1)
593  return((StringInfo *) NULL);
594  offset=(MagickOffsetType) lseek(file,0,SEEK_END);
595  if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
596  {
597  file=close(file)-1;
598  return((StringInfo *) NULL);
599  }
600  length=(size_t) offset;
601  string=(char *) NULL;
602  if (~length >= (MagickPathExtent-1))
603  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
604  sizeof(*string));
605  if (string == (char *) NULL)
606  {
607  file=close(file)-1;
608  return((StringInfo *) NULL);
609  }
610  map=MapBlob(file,ReadMode,0,length);
611  if (map != (void *) NULL)
612  {
613  (void) memcpy(string,map,length);
614  (void) UnmapBlob(map,length);
615  }
616  else
617  {
618  size_t
619  i;
620 
621  ssize_t
622  count;
623 
624  (void) lseek(file,0,SEEK_SET);
625  for (i=0; i < length; i+=count)
626  {
627  count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
628  MAGICK_SSIZE_MAX));
629  if (count <= 0)
630  {
631  count=0;
632  if (errno != EINTR)
633  break;
634  }
635  }
636  if (i < length)
637  {
638  file=close(file)-1;
639  string=DestroyString(string);
640  return((StringInfo *) NULL);
641  }
642  }
643  string[length]='\0';
644  file=close(file)-1;
645  string_info=AcquireStringInfoContainer();
646  string_info->path=ConstantString(filename);
647  string_info->length=length;
648  string_info->datum=(unsigned char *) string;
649  return(string_info);
650 }
651 ␌
652 /*
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % %
655 % %
656 % %
657 % C o n s t a n t S t r i n g %
658 % %
659 % %
660 % %
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662 %
663 % ConstantString() allocates exactly the needed memory for a string and
664 % copies the source string to that memory location. A NULL string pointer
665 % will allocate an empty string containing just the NUL character.
666 %
667 % When finished the string should be freed using DestoryString()
668 %
669 % The format of the ConstantString method is:
670 %
671 % char *ConstantString(const char *source)
672 %
673 % A description of each parameter follows:
674 %
675 % o source: A character string.
676 %
677 */
678 MagickExport char *ConstantString(const char *source)
679 {
680  char
681  *destination;
682 
683  size_t
684  length;
685 
686  length=0;
687  if (source != (char *) NULL)
688  length+=strlen(source);
689  destination=(char *) NULL;
690  if (~length >= 1UL)
691  destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
692  if (destination == (char *) NULL)
693  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
694  if (source != (char *) NULL)
695  (void) memcpy(destination,source,length*sizeof(*destination));
696  destination[length]='\0';
697  return(destination);
698 }
699 ␌
700 /*
701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % %
703 % %
704 % %
705 % C o p y M a g i c k S t r i n g %
706 % %
707 % %
708 % %
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 %
711 % CopyMagickString() copies the source string to the destination string, with
712 % out exceeding the given pre-declared length.
713 %
714 % The destination buffer is always null-terminated even if the string must be
715 % truncated. The return value is the length of the string.
716 %
717 % The format of the CopyMagickString method is:
718 %
719 % size_t CopyMagickString(const char *magick_restrict destination,
720 % char *magick_restrict source,const size_t length)
721 %
722 % A description of each parameter follows:
723 %
724 % o destination: the destination string.
725 %
726 % o source: the source string.
727 %
728 % o length: the length of the destination string.
729 %
730 */
731 MagickExport size_t CopyMagickString(char *magick_restrict destination,
732  const char *magick_restrict source,const size_t length)
733 {
734  char
735  *magick_restrict q;
736 
737  const char
738  *magick_restrict p;
739 
740  size_t
741  n;
742 
743  p=source;
744  q=destination;
745  for (n=length; n > 4; n-=4)
746  {
747  if (((*q++)=(*p++)) == '\0')
748  return((size_t) (p-source-1));
749  if (((*q++)=(*p++)) == '\0')
750  return((size_t) (p-source-1));
751  if (((*q++)=(*p++)) == '\0')
752  return((size_t) (p-source-1));
753  if (((*q++)=(*p++)) == '\0')
754  return((size_t) (p-source-1));
755  }
756  if (length != 0)
757  {
758  while (--n != 0)
759  if (((*q++)=(*p++)) == '\0')
760  return((size_t) (p-source-1));
761  *q='\0';
762  }
763  return((size_t) (p-source));
764 }
765 ␌
766 /*
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % %
769 % %
770 % %
771 % D e s t r o y S t r i n g %
772 % %
773 % %
774 % %
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 %
777 % DestroyString() destroys memory associated with a string.
778 %
779 % The format of the DestroyString method is:
780 %
781 % char *DestroyString(char *string)
782 %
783 % A description of each parameter follows:
784 %
785 % o string: the string.
786 %
787 */
788 MagickExport char *DestroyString(char *string)
789 {
790  return((char *) RelinquishMagickMemory(string));
791 }
792 ␌
793 /*
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 % %
796 % %
797 % %
798 % D e s t r o y S t r i n g I n f o %
799 % %
800 % %
801 % %
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803 %
804 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
805 %
806 % The format of the DestroyStringInfo method is:
807 %
808 % StringInfo *DestroyStringInfo(StringInfo *string_info)
809 %
810 % A description of each parameter follows:
811 %
812 % o string_info: the string info.
813 %
814 */
815 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
816 {
817  assert(string_info != (StringInfo *) NULL);
818  assert(string_info->signature == MagickCoreSignature);
819  if (string_info->datum != (unsigned char *) NULL)
820  string_info->datum=(unsigned char *) RelinquishMagickMemory(
821  string_info->datum);
822  if (string_info->name != (char *) NULL)
823  string_info->name=DestroyString(string_info->name);
824  if (string_info->path != (char *) NULL)
825  string_info->path=DestroyString(string_info->path);
826  string_info->signature=(~MagickCoreSignature);
827  string_info=(StringInfo *) RelinquishMagickMemory(string_info);
828  return(string_info);
829 }
830 ␌
831 /*
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % %
834 % %
835 % %
836 % D e s t r o y S t r i n g L i s t %
837 % %
838 % %
839 % %
840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 %
842 % DestroyStringList() zeros memory associated with a string list.
843 %
844 % The format of the DestroyStringList method is:
845 %
846 % char **DestroyStringList(char **list)
847 %
848 % A description of each parameter follows:
849 %
850 % o list: the string list.
851 %
852 */
853 MagickExport char **DestroyStringList(char **list)
854 {
855  ssize_t
856  i;
857 
858  assert(list != (char **) NULL);
859  for (i=0; list[i] != (char *) NULL; i++)
860  list[i]=DestroyString(list[i]);
861  list=(char **) RelinquishMagickMemory(list);
862  return(list);
863 }
864 ␌
865 /*
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % %
868 % %
869 % %
870 % E s c a p e S t r i n g %
871 % %
872 % %
873 % %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 %
876 % EscapeString() allocates memory for a backslash-escaped version of a
877 % source text string, copies the escaped version of the text to that
878 % memory location while adding backslash characters, and returns the
879 % escaped string.
880 %
881 % The format of the EscapeString method is:
882 %
883 % char *EscapeString(const char *source,const char escape)
884 %
885 % A description of each parameter follows:
886 %
887 % o allocate_string: Method EscapeString returns the escaped string.
888 %
889 % o source: A character string.
890 %
891 % o escape: the quoted string termination character to escape (e.g. '"').
892 %
893 */
894 MagickExport char *EscapeString(const char *source,const char escape)
895 {
896  char
897  *destination;
898 
899  char
900  *q;
901 
902  const char
903  *p;
904 
905  size_t
906  length;
907 
908  assert(source != (const char *) NULL);
909  length=0;
910  for (p=source; *p != '\0'; p++)
911  {
912  if ((*p == '\\') || (*p == escape))
913  {
914  if (~length < 1)
915  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
916  length++;
917  }
918  length++;
919  }
920  destination=(char *) NULL;
921  if (~length >= (MagickPathExtent-1))
922  destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
923  sizeof(*destination));
924  if (destination == (char *) NULL)
925  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
926  *destination='\0';
927  q=destination;
928  for (p=source; *p != '\0'; p++)
929  {
930  if ((*p == '\\') || (*p == escape))
931  *q++='\\';
932  *q++=(*p);
933  }
934  *q='\0';
935  return(destination);
936 }
937 ␌
938 /*
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % %
941 % %
942 % %
943 % F i l e T o S t r i n g %
944 % %
945 % %
946 % %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 %
949 % FileToString() returns the contents of a file as a string.
950 %
951 % The format of the FileToString method is:
952 %
953 % char *FileToString(const char *filename,const size_t extent,
954 % ExceptionInfo *exception)
955 %
956 % A description of each parameter follows:
957 %
958 % o filename: the filename.
959 %
960 % o extent: Maximum length of the string.
961 %
962 % o exception: return any errors or warnings in this structure.
963 %
964 */
965 MagickExport char *FileToString(const char *filename,const size_t extent,
966  ExceptionInfo *exception)
967 {
968  size_t
969  length;
970 
971  assert(filename != (const char *) NULL);
972  assert(exception != (ExceptionInfo *) NULL);
973  if (IsEventLogging() != MagickFalse)
974  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
975  return((char *) FileToBlob(filename,extent,&length,exception));
976 }
977 ␌
978 /*
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 % %
981 % %
982 % %
983 % F i l e T o S t r i n g I n f o %
984 % %
985 % %
986 % %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 %
989 % FileToStringInfo() returns the contents of a file as a string.
990 %
991 % The format of the FileToStringInfo method is:
992 %
993 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
994 % ExceptionInfo *exception)
995 %
996 % A description of each parameter follows:
997 %
998 % o filename: the filename.
999 %
1000 % o extent: Maximum length of the string.
1001 %
1002 % o exception: return any errors or warnings in this structure.
1003 %
1004 */
1005 MagickExport StringInfo *FileToStringInfo(const char *filename,
1006  const size_t extent,ExceptionInfo *exception)
1007 {
1008  StringInfo
1009  *string_info;
1010 
1011  assert(filename != (const char *) NULL);
1012  assert(exception != (ExceptionInfo *) NULL);
1013  if (IsEventLogging() != MagickFalse)
1014  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1015  string_info=AcquireStringInfoContainer();
1016  string_info->path=ConstantString(filename);
1017  string_info->datum=(unsigned char *) FileToBlob(filename,extent,
1018  &string_info->length,exception);
1019  if (string_info->datum == (unsigned char *) NULL)
1020  {
1021  string_info=DestroyStringInfo(string_info);
1022  return((StringInfo *) NULL);
1023  }
1024  return(string_info);
1025 }
1026 ␌
1027 /*
1028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029 % %
1030 % %
1031 % %
1032 % F o r m a t M a g i c k S i z e %
1033 % %
1034 % %
1035 % %
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037 %
1038 % FormatMagickSize() converts a size to a human readable format, for example,
1039 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1040 % 1000.
1041 %
1042 % The format of the FormatMagickSize method is:
1043 %
1044 % ssize_t FormatMagickSize(const MagickSizeType size,const char *suffix,
1045 % const size_t length,char *format)
1046 %
1047 % A description of each parameter follows:
1048 %
1049 % o size: convert this size to a human readable format.
1050 %
1051 % o bi: use power of two rather than power of ten.
1052 %
1053 % o suffix: append suffix, typically B or P.
1054 %
1055 % o length: the maximum length of the string.
1056 %
1057 % o format: human readable format.
1058 %
1059 */
1060 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1061  const MagickBooleanType bi,const char *suffix,const size_t length,
1062  char *format)
1063 {
1064  char
1065  p[MagickPathExtent],
1066  q[MagickPathExtent];
1067 
1068  const char
1069  **units;
1070 
1071  double
1072  bytes,
1073  extent;
1074 
1075  ssize_t
1076  i;
1077 
1078  ssize_t
1079  count;
1080 
1081  static const char
1082  *bi_units[] =
1083  {
1084  "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1085  },
1086  *traditional_units[] =
1087  {
1088  "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1089  };
1090 
1091  bytes=1000.0;
1092  units=traditional_units;
1093  if (bi != MagickFalse)
1094  {
1095  bytes=1024.0;
1096  units=bi_units;
1097  }
1098  extent=(double) size;
1099  (void) FormatLocaleString(p,MagickPathExtent,"%.*g",GetMagickPrecision(),
1100  extent);
1101  (void) FormatLocaleString(q,MagickPathExtent,"%.20g",extent);
1102  if (strtod(p,(char **) NULL) == strtod(q,(char **) NULL))
1103  {
1104  if (suffix == (const char *) NULL)
1105  count=FormatLocaleString(format,length,"%.20g%s",extent,units[0]);
1106  else
1107  count=FormatLocaleString(format,length,"%.20g%s%s",extent,units[0],
1108  suffix);
1109  return(count);
1110  }
1111  for (i=0; (extent >= bytes) && (units[i+1] != (const char *) NULL); i++)
1112  extent/=bytes;
1113  if (suffix == (const char *) NULL)
1114  count=FormatLocaleString(format,length,"%.*g%s",GetMagickPrecision(),
1115  extent,units[i]);
1116  else
1117  count=FormatLocaleString(format,length,"%.*g%s%s",GetMagickPrecision(),
1118  extent,units[i],suffix);
1119  return(count);
1120 }
1121 ␌
1122 /*
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 % %
1125 % %
1126 % %
1127 % G e t E n v i r o n m e n t V a l u e %
1128 % %
1129 % %
1130 % %
1131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1132 %
1133 % GetEnvironmentValue() returns the environment string that matches the
1134 % specified name.
1135 %
1136 % The format of the GetEnvironmentValue method is:
1137 %
1138 % char *GetEnvironmentValue(const char *name)
1139 %
1140 % A description of each parameter follows:
1141 %
1142 % o name: the environment name.
1143 %
1144 */
1145 MagickExport char *GetEnvironmentValue(const char *name)
1146 {
1147  const char
1148  *environment;
1149 
1150  environment=getenv(name);
1151  if (environment == (const char *) NULL)
1152  return((char *) NULL);
1153  return(ConstantString(environment));
1154 }
1155 ␌
1156 /*
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1158 % %
1159 % %
1160 % %
1161 % G e t S t r i n g I n f o D a t u m %
1162 % %
1163 % %
1164 % %
1165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166 %
1167 % GetStringInfoDatum() returns the datum associated with the string.
1168 %
1169 % The format of the GetStringInfoDatum method is:
1170 %
1171 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1172 %
1173 % A description of each parameter follows:
1174 %
1175 % o string_info: the string info.
1176 %
1177 */
1178 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1179 {
1180  assert(string_info != (StringInfo *) NULL);
1181  assert(string_info->signature == MagickCoreSignature);
1182  return(string_info->datum);
1183 }
1184 ␌
1185 /*
1186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1187 % %
1188 % %
1189 % %
1190 % G e t S t r i n g I n f o L e n g t h %
1191 % %
1192 % %
1193 % %
1194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195 %
1196 % GetStringInfoLength() returns the string length.
1197 %
1198 % The format of the GetStringInfoLength method is:
1199 %
1200 % size_t GetStringInfoLength(const StringInfo *string_info)
1201 %
1202 % A description of each parameter follows:
1203 %
1204 % o string_info: the string info.
1205 %
1206 */
1207 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1208 {
1209  assert(string_info != (StringInfo *) NULL);
1210  assert(string_info->signature == MagickCoreSignature);
1211  return(string_info->length);
1212 }
1213 ␌
1214 /*
1215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216 % %
1217 % %
1218 % %
1219 % G e t S t r i n g I n f o N a m e %
1220 % %
1221 % %
1222 % %
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1224 %
1225 % GetStringInfoName() returns the name associated with the string.
1226 %
1227 % The format of the GetStringInfoName method is:
1228 %
1229 % const char *GetStringInfoName(const StringInfo *string_info)
1230 %
1231 % A description of each parameter follows:
1232 %
1233 % o string_info: the string info.
1234 %
1235 */
1236 MagickExport const char *GetStringInfoName(const StringInfo *string_info)
1237 {
1238  assert(string_info != (StringInfo *) NULL);
1239  assert(string_info->signature == MagickCoreSignature);
1240  return(string_info->name);
1241 }
1242 ␌
1243 /*
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245 % %
1246 % %
1247 % %
1248 % G e t S t r i n g I n f o P a t h %
1249 % %
1250 % %
1251 % %
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 %
1254 % GetStringInfoPath() returns the path associated with the string.
1255 %
1256 % The format of the GetStringInfoPath method is:
1257 %
1258 % const char *GetStringInfoPath(const StringInfo *string_info)
1259 %
1260 % A description of each parameter follows:
1261 %
1262 % o string_info: the string info.
1263 %
1264 */
1265 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1266 {
1267  assert(string_info != (StringInfo *) NULL);
1268  assert(string_info->signature == MagickCoreSignature);
1269  return(string_info->path);
1270 }
1271 ␌
1272 /*
1273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1274 % %
1275 % %
1276 % %
1277 + I n t e r p r e t S i P r e f i x V a l u e %
1278 % %
1279 % %
1280 % %
1281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1282 %
1283 % InterpretSiPrefixValue() converts the initial portion of the string to a
1284 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1285 % etc.).
1286 %
1287 % The format of the InterpretSiPrefixValue method is:
1288 %
1289 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1290 %
1291 % A description of each parameter follows:
1292 %
1293 % o value: the string value.
1294 %
1295 % o sentinal: if sentinal is not NULL, return a pointer to the character
1296 % after the last character used in the conversion.
1297 %
1298 */
1299 MagickExport double InterpretSiPrefixValue(const char *magick_restrict string,
1300  char **magick_restrict sentinal)
1301 {
1302  char
1303  *q;
1304 
1305  double
1306  value;
1307 
1308  value=InterpretLocaleValue(string,&q);
1309  if (q != string)
1310  {
1311  if ((*q >= 'E') && (*q <= 'z'))
1312  {
1313  double
1314  e;
1315 
1316  switch ((int) ((unsigned char) *q))
1317  {
1318  case 'y': e=(-24.0); break;
1319  case 'z': e=(-21.0); break;
1320  case 'a': e=(-18.0); break;
1321  case 'f': e=(-15.0); break;
1322  case 'p': e=(-12.0); break;
1323  case 'n': e=(-9.0); break;
1324  case 'u': e=(-6.0); break;
1325  case 'm': e=(-3.0); break;
1326  case 'c': e=(-2.0); break;
1327  case 'd': e=(-1.0); break;
1328  case 'h': e=2.0; break;
1329  case 'k': e=3.0; break;
1330  case 'K': e=3.0; break;
1331  case 'M': e=6.0; break;
1332  case 'G': e=9.0; break;
1333  case 'T': e=12.0; break;
1334  case 'P': e=15.0; break;
1335  case 'E': e=18.0; break;
1336  case 'Z': e=21.0; break;
1337  case 'Y': e=24.0; break;
1338  default: e=0.0; break;
1339  }
1340  if (e >= MagickEpsilon)
1341  {
1342  if (q[1] == 'i')
1343  {
1344  value*=pow(2.0,e/0.3);
1345  q+=2;
1346  }
1347  else
1348  {
1349  value*=pow(10.0,e);
1350  q++;
1351  }
1352  }
1353  }
1354  if ((*q == 'B') || (*q == 'P'))
1355  q++;
1356  }
1357  if (sentinal != (char **) NULL)
1358  *sentinal=q;
1359  return(value);
1360 }
1361 ␌
1362 /*
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 % %
1365 % %
1366 % %
1367 % I s S t r i n g T r u e %
1368 % %
1369 % %
1370 % %
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372 %
1373 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1374 % "1". Any other string or undefined returns MagickFalse.
1375 %
1376 % Typically this is used to look at strings (options or artifacts) which
1377 % has a default value of "false", when not defined.
1378 %
1379 % The format of the IsStringTrue method is:
1380 %
1381 % MagickBooleanType IsStringTrue(const char *value)
1382 %
1383 % A description of each parameter follows:
1384 %
1385 % o value: Specifies a pointer to a character array.
1386 %
1387 */
1388 MagickExport MagickBooleanType IsStringTrue(const char *value)
1389 {
1390  if (value == (const char *) NULL)
1391  return(MagickFalse);
1392  if (LocaleCompare(value,"true") == 0)
1393  return(MagickTrue);
1394  if (LocaleCompare(value,"on") == 0)
1395  return(MagickTrue);
1396  if (LocaleCompare(value,"yes") == 0)
1397  return(MagickTrue);
1398  if (LocaleCompare(value,"1") == 0)
1399  return(MagickTrue);
1400  return(MagickFalse);
1401 }
1402 ␌
1403 /*
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1405 % %
1406 % %
1407 % %
1408 % I s S t r i n g F a l s e %
1409 % %
1410 % %
1411 % %
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 %
1414 % IsStringFalse() returns MagickTrue if the value is "false", "off", "no" or
1415 % "0". Any other string or undefined returns MagickFalse.
1416 %
1417 % Typically this is used to look at strings (options or artifacts) which
1418 % has a default value of "true", when it has not been defined.
1419 %
1420 % The format of the IsStringFalse method is:
1421 %
1422 % MagickBooleanType IsStringFalse(const char *value)
1423 %
1424 % A description of each parameter follows:
1425 %
1426 % o value: Specifies a pointer to a character array.
1427 %
1428 */
1429 MagickExport MagickBooleanType IsStringFalse(const char *value)
1430 {
1431  if (value == (const char *) NULL)
1432  return(MagickFalse);
1433  if (LocaleCompare(value,"false") == 0)
1434  return(MagickTrue);
1435  if (LocaleCompare(value,"off") == 0)
1436  return(MagickTrue);
1437  if (LocaleCompare(value,"no") == 0)
1438  return(MagickTrue);
1439  if (LocaleCompare(value,"0") == 0)
1440  return(MagickTrue);
1441  return(MagickFalse);
1442 }
1443 ␌
1444 /*
1445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446 % %
1447 % %
1448 % %
1449 % P r i n t S t r i n g I n f o %
1450 % %
1451 % %
1452 % %
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 %
1455 % PrintStringInfo() prints the string.
1456 %
1457 % The format of the PrintStringInfo method is:
1458 %
1459 % void PrintStringInfo(FILE *file,const char *id,
1460 % const StringInfo *string_info)
1461 %
1462 % A description of each parameter follows:
1463 %
1464 % o file: the file, typically stdout.
1465 %
1466 % o id: the string id.
1467 %
1468 % o string_info: the string info.
1469 %
1470 */
1471 MagickExport void PrintStringInfo(FILE *file,const char *id,
1472  const StringInfo *string_info)
1473 {
1474  const char
1475  *p;
1476 
1477  size_t
1478  i,
1479  j;
1480 
1481  assert(id != (const char *) NULL);
1482  assert(string_info != (StringInfo *) NULL);
1483  assert(string_info->signature == MagickCoreSignature);
1484  p=(char *) string_info->datum;
1485  for (i=0; i < string_info->length; i++)
1486  {
1487  if (((int) ((unsigned char) *p) < 32) &&
1488  (isspace((int) ((unsigned char) *p)) == 0))
1489  break;
1490  p++;
1491  }
1492  (void) FormatLocaleFile(file,"%s(%.20g):\n",id,(double) string_info->length);
1493  if (i == string_info->length)
1494  {
1495  for (i=0; i < string_info->length; i++)
1496  (void) fputc(string_info->datum[i],file);
1497  (void) fputc('\n',file);
1498  return;
1499  }
1500  /*
1501  Convert string to a HEX list.
1502  */
1503  p=(char *) string_info->datum;
1504  for (i=0; i < string_info->length; i+=CharsPerLine)
1505  {
1506  (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (CharsPerLine*i));
1507  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1508  {
1509  (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1510  if ((j % 0x04) == 0)
1511  (void) fputc(' ',file);
1512  }
1513  for ( ; j <= CharsPerLine; j++)
1514  {
1515  (void) fputc(' ',file);
1516  (void) fputc(' ',file);
1517  if ((j % 0x04) == 0)
1518  (void) fputc(' ',file);
1519  }
1520  (void) fputc(' ',file);
1521  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1522  {
1523  if (isprint((int) ((unsigned char) *p)) != 0)
1524  (void) fputc(*p,file);
1525  else
1526  (void) fputc('-',file);
1527  p++;
1528  }
1529  (void) fputc('\n',file);
1530  }
1531 }
1532 ␌
1533 /*
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 % %
1536 % %
1537 % %
1538 % R e s e t S t r i n g I n f o %
1539 % %
1540 % %
1541 % %
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543 %
1544 % ResetStringInfo() reset the string to all null bytes.
1545 %
1546 % The format of the ResetStringInfo method is:
1547 %
1548 % void ResetStringInfo(StringInfo *string_info)
1549 %
1550 % A description of each parameter follows:
1551 %
1552 % o string_info: the string info.
1553 %
1554 */
1555 MagickExport void ResetStringInfo(StringInfo *string_info)
1556 {
1557  assert(string_info != (StringInfo *) NULL);
1558  assert(string_info->signature == MagickCoreSignature);
1559  (void) memset(string_info->datum,0,string_info->length);
1560 }
1561 ␌
1562 /*
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1564 % %
1565 % %
1566 % %
1567 % S a n t i z e S t r i n g %
1568 % %
1569 % %
1570 % %
1571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1572 %
1573 % SanitizeString() returns a new string with all characters removed except
1574 % letters, digits and !#$%&'*+-=?^_`{|}~@.[].
1575 %
1576 % Free the sanitized string with DestroyString().
1577 %
1578 % The format of the SanitizeString method is:
1579 %
1580 % char *SanitizeString(const char *source)
1581 %
1582 % A description of each parameter follows:
1583 %
1584 % o source: A character string.
1585 %
1586 */
1587 MagickExport char *SanitizeString(const char *source)
1588 {
1589  char
1590  *sanitize_source;
1591 
1592  const char
1593  *q;
1594 
1595  char
1596  *p;
1597 
1598  static char
1599  allowlist[] =
1600  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1601  "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1602 
1603  sanitize_source=AcquireString(source);
1604  p=sanitize_source;
1605  q=sanitize_source+strlen(sanitize_source);
1606  for (p+=strspn(p,allowlist); p != q; p+=strspn(p,allowlist))
1607  *p='_';
1608  return(sanitize_source);
1609 }
1610 ␌
1611 /*
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 % %
1614 % %
1615 % %
1616 % S e t S t r i n g I n f o %
1617 % %
1618 % %
1619 % %
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 %
1622 % SetStringInfo() copies the source string to the destination string.
1623 %
1624 % The format of the SetStringInfo method is:
1625 %
1626 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1627 %
1628 % A description of each parameter follows:
1629 %
1630 % o string_info: the string info.
1631 %
1632 % o source: the source string.
1633 %
1634 */
1635 MagickExport void SetStringInfo(StringInfo *string_info,
1636  const StringInfo *source)
1637 {
1638  assert(string_info != (StringInfo *) NULL);
1639  assert(string_info->signature == MagickCoreSignature);
1640  assert(source != (StringInfo *) NULL);
1641  assert(source->signature == MagickCoreSignature);
1642  if (string_info->length == 0)
1643  return;
1644  (void) memset(string_info->datum,0,string_info->length);
1645  (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1646  source->length));
1647 }
1648 ␌
1649 /*
1650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1651 % %
1652 % %
1653 % %
1654 % S e t S t r i n g I n f o D a t u m %
1655 % %
1656 % %
1657 % %
1658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1659 %
1660 % SetStringInfoDatum() copies bytes from the source string for the length of
1661 % the destination string.
1662 %
1663 % The format of the SetStringInfoDatum method is:
1664 %
1665 % void SetStringInfoDatum(StringInfo *string_info,
1666 % const unsigned char *source)
1667 %
1668 % A description of each parameter follows:
1669 %
1670 % o string_info: the string info.
1671 %
1672 % o source: the source string.
1673 %
1674 */
1675 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1676  const unsigned char *source)
1677 {
1678  assert(string_info != (StringInfo *) NULL);
1679  assert(string_info->signature == MagickCoreSignature);
1680  if (string_info->length != 0)
1681  (void) memcpy(string_info->datum,source,string_info->length);
1682 }
1683 ␌
1684 /*
1685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % %
1687 % %
1688 % %
1689 % S e t S t r i n g I n f o L e n g t h %
1690 % %
1691 % %
1692 % %
1693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1694 %
1695 % SetStringInfoLength() set the string length to the specified value.
1696 %
1697 % The format of the SetStringInfoLength method is:
1698 %
1699 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1700 %
1701 % A description of each parameter follows:
1702 %
1703 % o string_info: the string info.
1704 %
1705 % o length: the string length.
1706 %
1707 */
1708 MagickExport void SetStringInfoLength(StringInfo *string_info,
1709  const size_t length)
1710 {
1711  assert(string_info != (StringInfo *) NULL);
1712  assert(string_info->signature == MagickCoreSignature);
1713  if (string_info->length == length)
1714  return;
1715  if (~length < MagickPathExtent)
1716  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1717  string_info->length=length;
1718  if (string_info->datum == (unsigned char *) NULL)
1719  string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1720  MagickPathExtent,sizeof(*string_info->datum));
1721  else
1722  string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1723  length+MagickPathExtent,sizeof(*string_info->datum));
1724  if (string_info->datum == (unsigned char *) NULL)
1725  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1726 }
1727 ␌
1728 /*
1729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % %
1731 % %
1732 % %
1733 % S e t S t r i n g I n f o N a m e %
1734 % %
1735 % %
1736 % %
1737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1738 %
1739 % SetStringInfoName() sets the name associated with the string.
1740 %
1741 % The format of the SetStringInfoName method is:
1742 %
1743 % void SetStringInfoName(StringInfo *string_info,const char *name)
1744 %
1745 % A description of each parameter follows:
1746 %
1747 % o string_info: the string info.
1748 %
1749 % o name: the name.
1750 %
1751 */
1752 MagickExport void SetStringInfoName(StringInfo *string_info,const char *name)
1753 {
1754  assert(string_info != (StringInfo *) NULL);
1755  assert(string_info->signature == MagickCoreSignature);
1756  assert(name != (const char *) NULL);
1757  string_info->name=ConstantString(name);
1758 }
1759 ␌
1760 /*
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1762 % %
1763 % %
1764 % %
1765 % S e t S t r i n g I n f o P a t h %
1766 % %
1767 % %
1768 % %
1769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1770 %
1771 % SetStringInfoPath() sets the path associated with the string.
1772 %
1773 % The format of the SetStringInfoPath method is:
1774 %
1775 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1776 %
1777 % A description of each parameter follows:
1778 %
1779 % o string_info: the string info.
1780 %
1781 % o path: the path.
1782 %
1783 */
1784 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1785 {
1786  assert(string_info != (StringInfo *) NULL);
1787  assert(string_info->signature == MagickCoreSignature);
1788  assert(path != (const char *) NULL);
1789  string_info->path=ConstantString(path);
1790 }
1791 ␌
1792 /*
1793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 % %
1795 % %
1796 % %
1797 % S p l i t S t r i n g I n f o %
1798 % %
1799 % %
1800 % %
1801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1802 %
1803 % SplitStringInfo() splits a string into two and returns it.
1804 %
1805 % The format of the SplitStringInfo method is:
1806 %
1807 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1808 %
1809 % A description of each parameter follows:
1810 %
1811 % o string_info: the string info.
1812 %
1813 */
1814 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1815  const size_t offset)
1816 {
1817  StringInfo
1818  *split_info;
1819 
1820  assert(string_info != (StringInfo *) NULL);
1821  assert(string_info->signature == MagickCoreSignature);
1822  if (offset > string_info->length)
1823  return((StringInfo *) NULL);
1824  split_info=AcquireStringInfo(offset);
1825  SetStringInfo(split_info,string_info);
1826  (void) memmove(string_info->datum,string_info->datum+offset,
1827  string_info->length-offset+MagickPathExtent);
1828  SetStringInfoLength(string_info,string_info->length-offset);
1829  return(split_info);
1830 }
1831 ␌
1832 /*
1833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834 % %
1835 % %
1836 % %
1837 % S t r i n g I n f o T o D i g e s t %
1838 % %
1839 % %
1840 % %
1841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1842 %
1843 % StringInfoToDigest() converts a string info string to a hex digest.
1844 %
1845 % The format of the StringInfoToString method is:
1846 %
1847 % char *StringInfoToDigest(const StringInfo *signature)
1848 %
1849 % A description of each parameter follows:
1850 %
1851 % o string_info: the string.
1852 %
1853 */
1854 MagickExport char *StringInfoToDigest(const StringInfo *signature)
1855 {
1856  char
1857  *digest;
1858 
1860  *signature_info;
1861 
1862  signature_info=AcquireSignatureInfo();
1863  UpdateSignature(signature_info,signature);
1864  FinalizeSignature(signature_info);
1865  digest=StringInfoToHexString(GetSignatureDigest(signature_info));
1866  signature_info=DestroySignatureInfo(signature_info);
1867  return(digest);
1868 }
1869 ␌
1870 /*
1871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872 % %
1873 % %
1874 % %
1875 % S t r i n g I n f o T o H e x S t r i n g %
1876 % %
1877 % %
1878 % %
1879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1880 %
1881 % StringInfoToHexString() converts a string info string to a C string.
1882 %
1883 % The format of the StringInfoToHexString method is:
1884 %
1885 % char *StringInfoToHexString(const StringInfo *string_info)
1886 %
1887 % A description of each parameter follows:
1888 %
1889 % o string_info: the string.
1890 %
1891 */
1892 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1893 {
1894  char
1895  *string;
1896 
1897  const unsigned char
1898  *p;
1899 
1900  ssize_t
1901  i;
1902 
1903  unsigned char
1904  *q;
1905 
1906  size_t
1907  length;
1908 
1909  unsigned char
1910  hex_digits[16];
1911 
1912  length=string_info->length;
1913  if (~length < MagickPathExtent)
1914  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1915  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,2*
1916  sizeof(*string));
1917  if (string == (char *) NULL)
1918  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1919  hex_digits[0]='0';
1920  hex_digits[1]='1';
1921  hex_digits[2]='2';
1922  hex_digits[3]='3';
1923  hex_digits[4]='4';
1924  hex_digits[5]='5';
1925  hex_digits[6]='6';
1926  hex_digits[7]='7';
1927  hex_digits[8]='8';
1928  hex_digits[9]='9';
1929  hex_digits[10]='a';
1930  hex_digits[11]='b';
1931  hex_digits[12]='c';
1932  hex_digits[13]='d';
1933  hex_digits[14]='e';
1934  hex_digits[15]='f';
1935  p=string_info->datum;
1936  q=(unsigned char *) string;
1937  for (i=0; i < (ssize_t) string_info->length; i++)
1938  {
1939  *q++=hex_digits[(*p >> 4) & 0x0f];
1940  *q++=hex_digits[*p & 0x0f];
1941  p++;
1942  }
1943  *q='\0';
1944  return(string);
1945 }
1946 ␌
1947 /*
1948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1949 % %
1950 % %
1951 % %
1952 % S t r i n g I n f o T o S t r i n g %
1953 % %
1954 % %
1955 % %
1956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957 %
1958 % StringInfoToString() converts a string info string to a C string.
1959 %
1960 % The format of the StringInfoToString method is:
1961 %
1962 % char *StringInfoToString(const StringInfo *string_info)
1963 %
1964 % A description of each parameter follows:
1965 %
1966 % o string_info: the string.
1967 %
1968 */
1969 MagickExport char *StringInfoToString(const StringInfo *string_info)
1970 {
1971  char
1972  *string;
1973 
1974  size_t
1975  length;
1976 
1977  string=(char *) NULL;
1978  length=string_info->length;
1979  if (~length >= (MagickPathExtent-1))
1980  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1981  sizeof(*string));
1982  if (string == (char *) NULL)
1983  return((char *) NULL);
1984  (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1985  string[length]='\0';
1986  return(string);
1987 }
1988 ␌
1989 /*
1990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1991 % %
1992 % %
1993 % %
1994 % S t r i n g T o A r g v %
1995 % %
1996 % %
1997 % %
1998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1999 %
2000 % StringToArgv() converts a text string into command line arguments.
2001 % The 'argv' array of arguments, is returned while the number of arguments
2002 % is returned via the provided integer variable pointer.
2003 %
2004 % Simple 'word' tokenizer, which allows for each word to be optionally
2005 % quoted. However it will not allow use of partial quotes, or escape
2006 % characters.
2007 %
2008 % The format of the StringToArgv method is:
2009 %
2010 % char **StringToArgv(const char *text,int *argc)
2011 %
2012 % A description of each parameter follows:
2013 %
2014 % o argv: Method StringToArgv returns the string list unless an error
2015 % occurs, otherwise NULL.
2016 %
2017 % o text: Specifies the string to segment into a list.
2018 %
2019 % o argc: This integer pointer returns the number of arguments in the
2020 % list.
2021 %
2022 */
2023 MagickExport char **StringToArgv(const char *text,int *argc)
2024 {
2025  char
2026  **argv;
2027 
2028  const char
2029  *p,
2030  *q;
2031 
2032  ssize_t
2033  i;
2034 
2035  *argc=0;
2036  if (text == (char *) NULL)
2037  return((char **) NULL);
2038  /*
2039  Determine the number of arguments.
2040  */
2041  for (p=text; *p != '\0'; )
2042  {
2043  while (isspace((int) ((unsigned char) *p)) != 0)
2044  p++;
2045  if (*p == '\0')
2046  break;
2047  (*argc)++;
2048  if (*p == '"')
2049  for (p++; (*p != '"') && (*p != '\0'); p++) ;
2050  if (*p == '\'')
2051  for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2052  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2053  p++;
2054  }
2055  (*argc)++;
2056  argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2057  if (argv == (char **) NULL)
2058  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2059  /*
2060  Convert string to an ASCII list.
2061  */
2062  argv[0]=AcquireString("magick");
2063  p=text;
2064  for (i=1; i < (ssize_t) *argc; i++)
2065  {
2066  while (isspace((int) ((unsigned char) *p)) != 0)
2067  p++;
2068  q=p;
2069  if (*q == '"')
2070  {
2071  p++;
2072  for (q++; (*q != '"') && (*q != '\0'); q++) ;
2073  }
2074  else
2075  if (*q == '\'')
2076  {
2077  p++;
2078  for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2079  }
2080  else
2081  while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2082  q++;
2083  argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
2084  sizeof(**argv));
2085  if (argv[i] == (char *) NULL)
2086  {
2087  for (i--; i >= 0; i--)
2088  argv[i]=DestroyString(argv[i]);
2089  argv=(char **) RelinquishMagickMemory(argv);
2090  ThrowFatalException(ResourceLimitFatalError,
2091  "UnableToConvertStringToARGV");
2092  }
2093  (void) memcpy(argv[i],p,(size_t) (q-p));
2094  argv[i][q-p]='\0';
2095  p=q;
2096  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2097  p++;
2098  }
2099  argv[i]=(char *) NULL;
2100  return(argv);
2101 }
2102 ␌
2103 /*
2104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105 % %
2106 % %
2107 % %
2108 % S t r i n g T o A r r a y O f D o u b l e s %
2109 % %
2110 % %
2111 % %
2112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113 %
2114 % StringToArrayOfDoubles() converts a string of space or comma separated
2115 % numbers into array of floating point numbers (doubles). Any number that
2116 % failes to parse properly will produce a syntax error. As will two commas
2117 % without a number between them. However a final comma at the end will
2118 % not be regarded as an error so as to simplify automatic list generation.
2119 %
2120 % A NULL value is returned on syntax or memory errors.
2121 %
2122 % Use RelinquishMagickMemory() to free returned array when finished.
2123 %
2124 % The format of the StringToArrayOfDoubles method is:
2125 %
2126 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2127 % ExceptionInfo *exception)
2128 %
2129 % A description of each parameter follows:
2130 %
2131 % o string: the string containing the comma/space separated values.
2132 %
2133 % o count: returns number of arguments in returned array
2134 %
2135 % o exception: return any errors or warnings in this structure.
2136 %
2137 */
2138 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2139  ExceptionInfo *exception)
2140 {
2141  char
2142  *q;
2143 
2144  const char
2145  *p;
2146 
2147  double
2148  *array;
2149 
2150  ssize_t
2151  i;
2152 
2153  /*
2154  Determine count of values, and check syntax.
2155  */
2156  assert(exception != (ExceptionInfo *) NULL);
2157  assert(exception->signature == MagickCoreSignature);
2158  *count=0;
2159  if (string == (char *) NULL)
2160  return((double *) NULL); /* no value found */
2161  i=0;
2162  p=string;
2163  while (*p != '\0')
2164  {
2165  (void) StringToDouble(p,&q); /* get value - ignores leading space */
2166  if (p == q)
2167  return((double *) NULL); /* no value found */
2168  p=q;
2169  i++; /* increment value count */
2170  while (isspace((int) ((unsigned char) *p)) != 0)
2171  p++; /* skip spaces */
2172  if (*p == ',')
2173  p++; /* skip comma */
2174  while (isspace((int) ((unsigned char) *p)) != 0)
2175  p++; /* and more spaces */
2176  }
2177  /*
2178  Allocate floating point argument list.
2179  */
2180  *count=i;
2181  array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2182  if (array == (double *) NULL)
2183  {
2184  (void) ThrowMagickException(exception,GetMagickModule(),
2185  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2186  return((double *) NULL);
2187  }
2188  /*
2189  Fill in the floating point values.
2190  */
2191  i=0;
2192  p=string;
2193  while ((*p != '\0') && (i < *count))
2194  {
2195  array[i++]=StringToDouble(p,&q);
2196  p=q;
2197  while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2198  p++;
2199  }
2200  return(array);
2201 }
2202 ␌
2203 /*
2204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2205 % %
2206 % %
2207 % %
2208 + S t r i n g T o k e n %
2209 % %
2210 % %
2211 % %
2212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213 %
2214 % StringToken() looks for any one of given delimiters and splits the string
2215 % into two separate strings by replacing the delimiter character found with a
2216 % null character.
2217 %
2218 % The given string pointer is changed to point to the string following the
2219 % delimiter character found, or NULL. A pointer to the start of the
2220 % string is returned, representing the token before the delimiter.
2221 %
2222 % StringToken() is similar to the strtok() C library method, but with
2223 % multiple delimiter characters rather than a delimiter string.
2224 %
2225 % The format of the StringToken method is:
2226 %
2227 % char *StringToken(const char *delimiters,char **string)
2228 %
2229 % A description of each parameter follows:
2230 %
2231 % o delimiters: one or more delimiters.
2232 %
2233 % o string: return the first token in the string. If none is found, return
2234 % NULL.
2235 %
2236 */
2237 MagickExport char *StringToken(const char *delimiters,char **string)
2238 {
2239  char
2240  *q;
2241 
2242  char
2243  *p;
2244 
2245  const char
2246  *r;
2247 
2248  int
2249  c,
2250  d;
2251 
2252  p=(*string);
2253  if (p == (char *) NULL)
2254  return((char *) NULL);
2255  q=p;
2256  for ( ; ; )
2257  {
2258  c=(*p++);
2259  r=delimiters;
2260  do
2261  {
2262  d=(*r++);
2263  if (c == d)
2264  {
2265  if (c == '\0')
2266  p=(char *) NULL;
2267  else
2268  p[-1]='\0';
2269  *string=p;
2270  return(q);
2271  }
2272  } while (d != '\0');
2273  }
2274 }
2275 ␌
2276 /*
2277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2278 % %
2279 % %
2280 % %
2281 % S t r i n g T o L i s t %
2282 % %
2283 % %
2284 % %
2285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286 %
2287 % StringToList() converts a text string into a list by segmenting the text
2288 % string at each carriage return discovered. The list is converted to HEX
2289 % characters if any control characters are discovered within the text string.
2290 %
2291 % The format of the StringToList method is:
2292 %
2293 % char **StringToList(const char *text)
2294 %
2295 % A description of each parameter follows:
2296 %
2297 % o text: Specifies the string to segment into a list.
2298 %
2299 */
2300 MagickExport char **StringToList(const char *text)
2301 {
2302  return(StringToStrings(text,(size_t *) NULL));
2303 }
2304 ␌
2305 /*
2306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2307 % %
2308 % %
2309 % %
2310 % S t r i n g T o S t r i n g s %
2311 % %
2312 % %
2313 % %
2314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2315 %
2316 % StringToStrings() converts a text string into a list by segmenting the text
2317 % string at each carriage return discovered. The list is converted to HEX
2318 % characters if any control characters are discovered within the text string.
2319 %
2320 % The format of the StringToList method is:
2321 %
2322 % char **StringToList(const char *text,size_t *lines)
2323 %
2324 % A description of each parameter follows:
2325 %
2326 % o text: Specifies the string to segment into a list.
2327 %
2328 % o count: Return value for the number of items in the list.
2329 %
2330 */
2331 MagickExport char **StringToStrings(const char *text,size_t *count)
2332 {
2333  char
2334  **textlist;
2335 
2336  const char
2337  *p;
2338 
2339  ssize_t
2340  i;
2341 
2342  size_t
2343  lines;
2344 
2345  if (text == (char *) NULL)
2346  {
2347  if (count != (size_t *) NULL)
2348  *count=0;
2349  return((char **) NULL);
2350  }
2351  for (p=text; *p != '\0'; p++)
2352  if (((int) ((unsigned char) *p) < 32) &&
2353  (isspace((int) ((unsigned char) *p)) == 0))
2354  break;
2355  if (*p == '\0')
2356  {
2357  const char
2358  *q;
2359 
2360  /*
2361  Convert string to an ASCII list.
2362  */
2363  lines=1;
2364  for (p=text; *p != '\0'; p++)
2365  if (*p == '\n')
2366  lines++;
2367  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2368  sizeof(*textlist));
2369  if (textlist == (char **) NULL)
2370  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2371  p=text;
2372  for (i=0; i < (ssize_t) lines; i++)
2373  {
2374  for (q=p; *q != '\0'; q++)
2375  if ((*q == '\r') || (*q == '\n'))
2376  break;
2377  textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
2378  sizeof(**textlist));
2379  if (textlist[i] == (char *) NULL)
2380  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2381  (void) memcpy(textlist[i],p,(size_t) (q-p));
2382  textlist[i][q-p]='\0';
2383  if (*q == '\r')
2384  q++;
2385  p=q+1;
2386  }
2387  }
2388  else
2389  {
2390  char
2391  hex_string[MagickPathExtent];
2392 
2393  char
2394  *q;
2395 
2396  ssize_t
2397  j;
2398 
2399  /*
2400  Convert string to a HEX list.
2401  */
2402  lines=(size_t) (strlen(text)/CharsPerLine)+1;
2403  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2404  sizeof(*textlist));
2405  if (textlist == (char **) NULL)
2406  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2407  p=text;
2408  for (i=0; i < (ssize_t) lines; i++)
2409  {
2410  size_t
2411  length;
2412 
2413  textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2414  sizeof(**textlist));
2415  if (textlist[i] == (char *) NULL)
2416  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2417  (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2418  (long) (CharsPerLine*i));
2419  q=textlist[i]+strlen(textlist[i]);
2420  length=strlen(p);
2421  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2422  {
2423  (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2424  (void) CopyMagickString(q,hex_string,MagickPathExtent);
2425  q+=2;
2426  if ((j % 0x04) == 0)
2427  *q++=' ';
2428  }
2429  for ( ; j <= CharsPerLine; j++)
2430  {
2431  *q++=' ';
2432  *q++=' ';
2433  if ((j % 0x04) == 0)
2434  *q++=' ';
2435  }
2436  *q++=' ';
2437  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2438  {
2439  if (isprint((int) ((unsigned char) *p)) != 0)
2440  *q++=(*p);
2441  else
2442  *q++='-';
2443  p++;
2444  }
2445  *q='\0';
2446  textlist[i]=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
2447  textlist[i]+1),sizeof(**textlist));
2448  if (textlist[i] == (char *) NULL)
2449  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2450  }
2451  }
2452  if (count != (size_t *) NULL)
2453  *count=lines;
2454  textlist[i]=(char *) NULL;
2455  return(textlist);
2456 }
2457 ␌
2458 /*
2459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2460 % %
2461 % %
2462 % %
2463 % S t r i n g T o S t r i n g I n f o %
2464 % %
2465 % %
2466 % %
2467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2468 %
2469 % StringToStringInfo() converts a string to a StringInfo type.
2470 %
2471 % The format of the StringToStringInfo method is:
2472 %
2473 % StringInfo *StringToStringInfo(const char *string)
2474 %
2475 % A description of each parameter follows:
2476 %
2477 % o string: The string.
2478 %
2479 */
2480 MagickExport StringInfo *StringToStringInfo(const char *string)
2481 {
2482  StringInfo
2483  *string_info;
2484 
2485  assert(string != (const char *) NULL);
2486  string_info=AcquireStringInfo(strlen(string));
2487  SetStringInfoDatum(string_info,(const unsigned char *) string);
2488  return(string_info);
2489 }
2490 ␌
2491 /*
2492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2493 % %
2494 % %
2495 % %
2496 % S t r i p M a g i c k S t r i n g %
2497 % %
2498 % %
2499 % %
2500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2501 %
2502 % StripMagickString() strips any whitespace or quotes from the beginning and
2503 % end of a string of characters. It returns the stripped string length.
2504 %
2505 % The format of the StripMagickString method is:
2506 %
2507 % size_t StripMagickString(char *message)
2508 %
2509 % A description of each parameter follows:
2510 %
2511 % o message: Specifies an array of characters.
2512 %
2513 */
2514 
2515 MagickExport void StripString(char *message)
2516 {
2517  (void) StripMagickString(message);
2518 }
2519 
2520 MagickExport size_t StripMagickString(char *message)
2521 {
2522  char
2523  *p,
2524  *q;
2525 
2526  size_t
2527  length;
2528 
2529  assert(message != (char *) NULL);
2530  if (*message == '\0')
2531  return(0);
2532  length=strlen(message);
2533  if (length == 1)
2534  return(1);
2535  p=message;
2536  while (isspace((int) ((unsigned char) *p)) != 0)
2537  p++;
2538  if ((*p == '\'') || (*p == '"'))
2539  p++;
2540  q=message+length-1;
2541  while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2542  q--;
2543  if (q > p)
2544  if ((*q == '\'') || (*q == '"'))
2545  q--;
2546  (void) memmove(message,p,(size_t) (q-p+1));
2547  message[q-p+1]='\0';
2548  for (p=message; *p != '\0'; p++)
2549  if (*p == '\n')
2550  *p=' ';
2551  return((size_t) (q-p+1));
2552 }
2553 ␌
2554 /*
2555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2556 % %
2557 % %
2558 % %
2559 % S u b s t i t u t e S t r i n g %
2560 % %
2561 % %
2562 % %
2563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2564 %
2565 % SubstituteString() performs string substitution on a string, replacing the
2566 % string with the substituted version. Buffer must be allocated from the heap.
2567 % If the string is matched and status, MagickTrue is returned otherwise
2568 % MagickFalse.
2569 %
2570 % The format of the SubstituteString method is:
2571 %
2572 % MagickBooleanType SubstituteString(char **string,const char *search,
2573 % const char *replace)
2574 %
2575 % A description of each parameter follows:
2576 %
2577 % o string: the string to perform replacements on; replaced with new
2578 % allocation if a replacement is made.
2579 %
2580 % o search: search for this string.
2581 %
2582 % o replace: replace any matches with this string.
2583 %
2584 */
2585 MagickExport MagickBooleanType SubstituteString(char **string,
2586  const char *search,const char *replace)
2587 {
2588  MagickBooleanType
2589  status;
2590 
2591  char
2592  *p;
2593 
2594  size_t
2595  extent,
2596  replace_extent,
2597  search_extent;
2598 
2599  ssize_t
2600  offset;
2601 
2602  status=MagickFalse;
2603  search_extent=0,
2604  replace_extent=0;
2605  for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2606  {
2607  if (search_extent == 0)
2608  search_extent=strlen(search);
2609  if (strncmp(p,search,search_extent) != 0)
2610  continue;
2611  /*
2612  We found a match.
2613  */
2614  status=MagickTrue;
2615  if (replace_extent == 0)
2616  replace_extent=strlen(replace);
2617  if (replace_extent > search_extent)
2618  {
2619  /*
2620  Make room for the replacement string.
2621  */
2622  offset=(ssize_t) (p-(*string));
2623  extent=strlen(*string)+replace_extent-search_extent+1;
2624  *string=(char *) ResizeQuantumMemory(*string,
2625  OverAllocateMemory(extent+MagickPathExtent),sizeof(*p));
2626  if (*string == (char *) NULL)
2627  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2628  p=(*string)+offset;
2629  }
2630  /*
2631  Replace string.
2632  */
2633  if (search_extent != replace_extent)
2634  (void) memmove(p+replace_extent,p+search_extent,
2635  strlen(p+search_extent)+1);
2636  (void) memcpy(p,replace,replace_extent);
2637  p+=replace_extent-1;
2638  }
2639  return(status);
2640 }