MagickCore  7.0.11
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-2021 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  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 */
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  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)
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  char
723  *magick_restrict q;
724 
725  const char
726  *magick_restrict p;
727 
728  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  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  char
888  *q;
889 
890  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  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  const char
1465  *p;
1466 
1467  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 a new string with all characters removed 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  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 D i g e s t %
1828 % %
1829 % %
1830 % %
1831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832 %
1833 % StringInfoToDigest() converts a string info string to a hex digest.
1834 %
1835 % The format of the StringInfoToString method is:
1836 %
1837 % char *StringInfoToDigest(const StringInfo *signature)
1838 %
1839 % A description of each parameter follows:
1840 %
1841 % o string_info: the string.
1842 %
1843 */
1845 {
1846  char
1847  *digest;
1848 
1850  *signature_info;
1851 
1852  signature_info=AcquireSignatureInfo();
1853  UpdateSignature(signature_info,signature);
1854  FinalizeSignature(signature_info);
1855  digest=StringInfoToHexString(GetSignatureDigest(signature_info));
1856  signature_info=DestroySignatureInfo(signature_info);
1857  return(digest);
1858 }
1859 
1860 /*
1861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1862 % %
1863 % %
1864 % %
1865 % S t r i n g I n f o T o H e x S t r i n g %
1866 % %
1867 % %
1868 % %
1869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1870 %
1871 % StringInfoToHexString() converts a string info string to a C string.
1872 %
1873 % The format of the StringInfoToHexString method is:
1874 %
1875 % char *StringInfoToHexString(const StringInfo *string_info)
1876 %
1877 % A description of each parameter follows:
1878 %
1879 % o string_info: the string.
1880 %
1881 */
1883 {
1884  char
1885  *string;
1886 
1887  const unsigned char
1888  *p;
1889 
1890  ssize_t
1891  i;
1892 
1893  unsigned char
1894  *q;
1895 
1896  size_t
1897  length;
1898 
1899  unsigned char
1900  hex_digits[16];
1901 
1902  length=string_info->length;
1903  if (~length < MagickPathExtent)
1904  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1905  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,2*
1906  sizeof(*string));
1907  if (string == (char *) NULL)
1908  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1909  hex_digits[0]='0';
1910  hex_digits[1]='1';
1911  hex_digits[2]='2';
1912  hex_digits[3]='3';
1913  hex_digits[4]='4';
1914  hex_digits[5]='5';
1915  hex_digits[6]='6';
1916  hex_digits[7]='7';
1917  hex_digits[8]='8';
1918  hex_digits[9]='9';
1919  hex_digits[10]='a';
1920  hex_digits[11]='b';
1921  hex_digits[12]='c';
1922  hex_digits[13]='d';
1923  hex_digits[14]='e';
1924  hex_digits[15]='f';
1925  p=string_info->datum;
1926  q=(unsigned char *) string;
1927  for (i=0; i < (ssize_t) string_info->length; i++)
1928  {
1929  *q++=hex_digits[(*p >> 4) & 0x0f];
1930  *q++=hex_digits[*p & 0x0f];
1931  p++;
1932  }
1933  *q='\0';
1934  return(string);
1935 }
1936 
1937 /*
1938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939 % %
1940 % %
1941 % %
1942 % S t r i n g I n f o T o S t r i n g %
1943 % %
1944 % %
1945 % %
1946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1947 %
1948 % StringInfoToString() converts a string info string to a C string.
1949 %
1950 % The format of the StringInfoToString method is:
1951 %
1952 % char *StringInfoToString(const StringInfo *string_info)
1953 %
1954 % A description of each parameter follows:
1955 %
1956 % o string_info: the string.
1957 %
1958 */
1959 MagickExport char *StringInfoToString(const StringInfo *string_info)
1960 {
1961  char
1962  *string;
1963 
1964  size_t
1965  length;
1966 
1967  string=(char *) NULL;
1968  length=string_info->length;
1969  if (~length >= (MagickPathExtent-1))
1970  string=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1971  sizeof(*string));
1972  if (string == (char *) NULL)
1973  return((char *) NULL);
1974  (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1975  string[length]='\0';
1976  return(string);
1977 }
1978 
1979 /*
1980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1981 % %
1982 % %
1983 % %
1984 % S t r i n g T o A r g v %
1985 % %
1986 % %
1987 % %
1988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1989 %
1990 % StringToArgv() converts a text string into command line arguments.
1991 % The 'argv' array of arguments, is returned while the number of arguments
1992 % is returned via the provided integer variable pointer.
1993 %
1994 % Simple 'word' tokenizer, which allows for each word to be optionally
1995 % quoted. However it will not allow use of partial quotes, or escape
1996 % characters.
1997 %
1998 % The format of the StringToArgv method is:
1999 %
2000 % char **StringToArgv(const char *text,int *argc)
2001 %
2002 % A description of each parameter follows:
2003 %
2004 % o argv: Method StringToArgv returns the string list unless an error
2005 % occurs, otherwise NULL.
2006 %
2007 % o text: Specifies the string to segment into a list.
2008 %
2009 % o argc: This integer pointer returns the number of arguments in the
2010 % list.
2011 %
2012 */
2013 MagickExport char **StringToArgv(const char *text,int *argc)
2014 {
2015  char
2016  **argv;
2017 
2018  const char
2019  *p,
2020  *q;
2021 
2022  ssize_t
2023  i;
2024 
2025  *argc=0;
2026  if (text == (char *) NULL)
2027  return((char **) NULL);
2028  /*
2029  Determine the number of arguments.
2030  */
2031  for (p=text; *p != '\0'; )
2032  {
2033  while (isspace((int) ((unsigned char) *p)) != 0)
2034  p++;
2035  if (*p == '\0')
2036  break;
2037  (*argc)++;
2038  if (*p == '"')
2039  for (p++; (*p != '"') && (*p != '\0'); p++) ;
2040  if (*p == '\'')
2041  for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2042  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2043  p++;
2044  }
2045  (*argc)++;
2046  argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2047  if (argv == (char **) NULL)
2048  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2049  /*
2050  Convert string to an ASCII list.
2051  */
2052  argv[0]=AcquireString("magick");
2053  p=text;
2054  for (i=1; i < (ssize_t) *argc; i++)
2055  {
2056  while (isspace((int) ((unsigned char) *p)) != 0)
2057  p++;
2058  q=p;
2059  if (*q == '"')
2060  {
2061  p++;
2062  for (q++; (*q != '"') && (*q != '\0'); q++) ;
2063  }
2064  else
2065  if (*q == '\'')
2066  {
2067  p++;
2068  for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2069  }
2070  else
2071  while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2072  q++;
2073  argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
2074  sizeof(**argv));
2075  if (argv[i] == (char *) NULL)
2076  {
2077  for (i--; i >= 0; i--)
2078  argv[i]=DestroyString(argv[i]);
2079  argv=(char **) RelinquishMagickMemory(argv);
2081  "UnableToConvertStringToARGV");
2082  }
2083  (void) memcpy(argv[i],p,(size_t) (q-p));
2084  argv[i][q-p]='\0';
2085  p=q;
2086  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2087  p++;
2088  }
2089  argv[i]=(char *) NULL;
2090  return(argv);
2091 }
2092 
2093 /*
2094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2095 % %
2096 % %
2097 % %
2098 % S t r i n g T o A r r a y O f D o u b l e s %
2099 % %
2100 % %
2101 % %
2102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2103 %
2104 % StringToArrayOfDoubles() converts a string of space or comma separated
2105 % numbers into array of floating point numbers (doubles). Any number that
2106 % failes to parse properly will produce a syntax error. As will two commas
2107 % without a number between them. However a final comma at the end will
2108 % not be regarded as an error so as to simplify automatic list generation.
2109 %
2110 % A NULL value is returned on syntax or memory errors.
2111 %
2112 % Use RelinquishMagickMemory() to free returned array when finished.
2113 %
2114 % The format of the StringToArrayOfDoubles method is:
2115 %
2116 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2117 % ExceptionInfo *exception)
2118 %
2119 % A description of each parameter follows:
2120 %
2121 % o string: the string containing the comma/space separated values.
2122 %
2123 % o count: returns number of arguments in returned array
2124 %
2125 % o exception: return any errors or warnings in this structure.
2126 %
2127 */
2128 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2130 {
2131  char
2132  *q;
2133 
2134  const char
2135  *p;
2136 
2137  double
2138  *array;
2139 
2140  ssize_t
2141  i;
2142 
2143  /*
2144  Determine count of values, and check syntax.
2145  */
2146  assert(exception != (ExceptionInfo *) NULL);
2148  *count=0;
2149  if (string == (char *) NULL)
2150  return((double *) NULL); /* no value found */
2151  i=0;
2152  p=string;
2153  while (*p != '\0')
2154  {
2155  (void) StringToDouble(p,&q); /* get value - ignores leading space */
2156  if (p == q)
2157  return((double *) NULL); /* no value found */
2158  p=q;
2159  i++; /* increment value count */
2160  while (isspace((int) ((unsigned char) *p)) != 0)
2161  p++; /* skip spaces */
2162  if (*p == ',')
2163  p++; /* skip comma */
2164  while (isspace((int) ((unsigned char) *p)) != 0)
2165  p++; /* and more spaces */
2166  }
2167  /*
2168  Allocate floating point argument list.
2169  */
2170  *count=i;
2171  array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2172  if (array == (double *) NULL)
2173  {
2175  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2176  return((double *) NULL);
2177  }
2178  /*
2179  Fill in the floating point values.
2180  */
2181  i=0;
2182  p=string;
2183  while ((*p != '\0') && (i < *count))
2184  {
2185  array[i++]=StringToDouble(p,&q);
2186  p=q;
2187  while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2188  p++;
2189  }
2190  return(array);
2191 }
2192 
2193 /*
2194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2195 % %
2196 % %
2197 % %
2198 + S t r i n g T o k e n %
2199 % %
2200 % %
2201 % %
2202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2203 %
2204 % StringToken() looks for any one of given delimiters and splits the string
2205 % into two separate strings by replacing the delimiter character found with a
2206 % null character.
2207 %
2208 % The given string pointer is changed to point to the string following the
2209 % delimiter character found, or NULL. A pointer to the start of the
2210 % string is returned, representing the token before the delimiter.
2211 %
2212 % StringToken() is similar to the strtok() C library method, but with
2213 % multiple delimiter characters rather than a delimiter string.
2214 %
2215 % The format of the StringToken method is:
2216 %
2217 % char *StringToken(const char *delimiters,char **string)
2218 %
2219 % A description of each parameter follows:
2220 %
2221 % o delimiters: one or more delimiters.
2222 %
2223 % o string: return the first token in the string. If none is found, return
2224 % NULL.
2225 %
2226 */
2227 MagickExport char *StringToken(const char *delimiters,char **string)
2228 {
2229  char
2230  *q;
2231 
2232  char
2233  *p;
2234 
2235  const char
2236  *r;
2237 
2238  int
2239  c,
2240  d;
2241 
2242  p=(*string);
2243  if (p == (char *) NULL)
2244  return((char *) NULL);
2245  q=p;
2246  for ( ; ; )
2247  {
2248  c=(*p++);
2249  r=delimiters;
2250  do
2251  {
2252  d=(*r++);
2253  if (c == d)
2254  {
2255  if (c == '\0')
2256  p=(char *) NULL;
2257  else
2258  p[-1]='\0';
2259  *string=p;
2260  return(q);
2261  }
2262  } while (d != '\0');
2263  }
2264 }
2265 
2266 /*
2267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2268 % %
2269 % %
2270 % %
2271 % S t r i n g T o L i s t %
2272 % %
2273 % %
2274 % %
2275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2276 %
2277 % StringToList() converts a text string into a list by segmenting the text
2278 % string at each carriage return discovered. The list is converted to HEX
2279 % characters if any control characters are discovered within the text string.
2280 %
2281 % The format of the StringToList method is:
2282 %
2283 % char **StringToList(const char *text)
2284 %
2285 % A description of each parameter follows:
2286 %
2287 % o text: Specifies the string to segment into a list.
2288 %
2289 */
2290 MagickExport char **StringToList(const char *text)
2291 {
2292  return(StringToStrings(text,(size_t *) NULL));
2293 }
2294 
2295 /*
2296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2297 % %
2298 % %
2299 % %
2300 % S t r i n g T o S t r i n g s %
2301 % %
2302 % %
2303 % %
2304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2305 %
2306 % StringToStrings() converts a text string into a list by segmenting the text
2307 % string at each carriage return discovered. The list is converted to HEX
2308 % characters if any control characters are discovered within the text string.
2309 %
2310 % The format of the StringToList method is:
2311 %
2312 % char **StringToList(const char *text,size_t *lines)
2313 %
2314 % A description of each parameter follows:
2315 %
2316 % o text: Specifies the string to segment into a list.
2317 %
2318 % o count: Return value for the number of items in the list.
2319 %
2320 */
2321 MagickExport char **StringToStrings(const char *text,size_t *count)
2322 {
2323  char
2324  **textlist;
2325 
2326  const char
2327  *p;
2328 
2329  ssize_t
2330  i;
2331 
2332  size_t
2333  lines;
2334 
2335  if (text == (char *) NULL)
2336  {
2337  if (count != (size_t *) NULL)
2338  *count=0;
2339  return((char **) NULL);
2340  }
2341  for (p=text; *p != '\0'; p++)
2342  if (((int) ((unsigned char) *p) < 32) &&
2343  (isspace((int) ((unsigned char) *p)) == 0))
2344  break;
2345  if (*p == '\0')
2346  {
2347  const char
2348  *q;
2349 
2350  /*
2351  Convert string to an ASCII list.
2352  */
2353  lines=1;
2354  for (p=text; *p != '\0'; p++)
2355  if (*p == '\n')
2356  lines++;
2357  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2358  sizeof(*textlist));
2359  if (textlist == (char **) NULL)
2360  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2361  p=text;
2362  for (i=0; i < (ssize_t) lines; i++)
2363  {
2364  for (q=p; *q != '\0'; q++)
2365  if ((*q == '\r') || (*q == '\n'))
2366  break;
2367  textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
2368  sizeof(**textlist));
2369  if (textlist[i] == (char *) NULL)
2370  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2371  (void) memcpy(textlist[i],p,(size_t) (q-p));
2372  textlist[i][q-p]='\0';
2373  if (*q == '\r')
2374  q++;
2375  p=q+1;
2376  }
2377  }
2378  else
2379  {
2380  char
2381  hex_string[MagickPathExtent];
2382 
2383  char
2384  *q;
2385 
2386  ssize_t
2387  j;
2388 
2389  /*
2390  Convert string to a HEX list.
2391  */
2392  lines=(size_t) (strlen(text)/CharsPerLine)+1;
2393  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2394  sizeof(*textlist));
2395  if (textlist == (char **) NULL)
2396  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2397  p=text;
2398  for (i=0; i < (ssize_t) lines; i++)
2399  {
2400  size_t
2401  length;
2402 
2403  textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2404  sizeof(**textlist));
2405  if (textlist[i] == (char *) NULL)
2406  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2407  (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2408  (long) (CharsPerLine*i));
2409  q=textlist[i]+strlen(textlist[i]);
2410  length=strlen(p);
2411  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2412  {
2413  (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2414  (void) CopyMagickString(q,hex_string,MagickPathExtent);
2415  q+=2;
2416  if ((j % 0x04) == 0)
2417  *q++=' ';
2418  }
2419  for ( ; j <= CharsPerLine; j++)
2420  {
2421  *q++=' ';
2422  *q++=' ';
2423  if ((j % 0x04) == 0)
2424  *q++=' ';
2425  }
2426  *q++=' ';
2427  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2428  {
2429  if (isprint((int) ((unsigned char) *p)) != 0)
2430  *q++=(*p);
2431  else
2432  *q++='-';
2433  p++;
2434  }
2435  *q='\0';
2436  textlist[i]=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
2437  textlist[i]+1),sizeof(**textlist));
2438  if (textlist[i] == (char *) NULL)
2439  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2440  }
2441  }
2442  if (count != (size_t *) NULL)
2443  *count=lines;
2444  textlist[i]=(char *) NULL;
2445  return(textlist);
2446 }
2447 
2448 /*
2449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2450 % %
2451 % %
2452 % %
2453 % S t r i n g T o S t r i n g I n f o %
2454 % %
2455 % %
2456 % %
2457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2458 %
2459 % StringToStringInfo() converts a string to a StringInfo type.
2460 %
2461 % The format of the StringToStringInfo method is:
2462 %
2463 % StringInfo *StringToStringInfo(const char *string)
2464 %
2465 % A description of each parameter follows:
2466 %
2467 % o string: The string.
2468 %
2469 */
2471 {
2472  StringInfo
2473  *string_info;
2474 
2475  assert(string != (const char *) NULL);
2476  string_info=AcquireStringInfo(strlen(string));
2477  SetStringInfoDatum(string_info,(const unsigned char *) string);
2478  return(string_info);
2479 }
2480 
2481 /*
2482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2483 % %
2484 % %
2485 % %
2486 % S t r i p S t r i n g %
2487 % %
2488 % %
2489 % %
2490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2491 %
2492 % StripString() strips any whitespace or quotes from the beginning and end of
2493 % a string of characters.
2494 %
2495 % The format of the StripString method is:
2496 %
2497 % void StripString(char *message)
2498 %
2499 % A description of each parameter follows:
2500 %
2501 % o message: Specifies an array of characters.
2502 %
2503 */
2504 MagickExport void StripString(char *message)
2505 {
2506  char
2507  *p,
2508  *q;
2509 
2510  size_t
2511  length;
2512 
2513  assert(message != (char *) NULL);
2514  if (*message == '\0')
2515  return;
2516  length=strlen(message);
2517  p=message;
2518  while (isspace((int) ((unsigned char) *p)) != 0)
2519  p++;
2520  if ((*p == '\'') || (*p == '"'))
2521  p++;
2522  q=message+length-1;
2523  while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2524  q--;
2525  if (q > p)
2526  if ((*q == '\'') || (*q == '"'))
2527  q--;
2528  (void) memmove(message,p,(size_t) (q-p+1));
2529  message[q-p+1]='\0';
2530  for (p=message; *p != '\0'; p++)
2531  if (*p == '\n')
2532  *p=' ';
2533 }
2534 
2535 /*
2536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2537 % %
2538 % %
2539 % %
2540 % S u b s t i t u t e S t r i n g %
2541 % %
2542 % %
2543 % %
2544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2545 %
2546 % SubstituteString() performs string substitution on a string, replacing the
2547 % string with the substituted version. Buffer must be allocated from the heap.
2548 % If the string is matched and status, MagickTrue is returned otherwise
2549 % MagickFalse.
2550 %
2551 % The format of the SubstituteString method is:
2552 %
2553 % MagickBooleanType SubstituteString(char **string,const char *search,
2554 % const char *replace)
2555 %
2556 % A description of each parameter follows:
2557 %
2558 % o string: the string to perform replacements on; replaced with new
2559 % allocation if a replacement is made.
2560 %
2561 % o search: search for this string.
2562 %
2563 % o replace: replace any matches with this string.
2564 %
2565 */
2567  const char *search,const char *replace)
2568 {
2570  status;
2571 
2572  char
2573  *p;
2574 
2575  size_t
2576  extent,
2577  replace_extent,
2578  search_extent;
2579 
2580  ssize_t
2581  offset;
2582 
2583  status=MagickFalse;
2584  search_extent=0,
2585  replace_extent=0;
2586  for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2587  {
2588  if (search_extent == 0)
2589  search_extent=strlen(search);
2590  if (strncmp(p,search,search_extent) != 0)
2591  continue;
2592  /*
2593  We found a match.
2594  */
2595  status=MagickTrue;
2596  if (replace_extent == 0)
2597  replace_extent=strlen(replace);
2598  if (replace_extent > search_extent)
2599  {
2600  /*
2601  Make room for the replacement string.
2602  */
2603  offset=(ssize_t) (p-(*string));
2604  extent=strlen(*string)+replace_extent-search_extent+1;
2605  *string=(char *) ResizeQuantumMemory(*string,extent+MagickPathExtent,
2606  sizeof(*p));
2607  if (*string == (char *) NULL)
2608  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2609  p=(*string)+offset;
2610  }
2611  /*
2612  Replace string.
2613  */
2614  if (search_extent != replace_extent)
2615  (void) memmove(p+replace_extent,p+search_extent,
2616  strlen(p+search_extent)+1);
2617  (void) memcpy(p,replace,replace_extent);
2618  p+=replace_extent-1;
2619  }
2620  return(status);
2621 }
#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:2470
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
MagickPrivate SignatureInfo * AcquireSignatureInfo(void)
unsigned char * datum
Definition: string_.h:33
MagickExport char * EscapeString(const char *source, const char escape)
Definition: string.c:882
#define MAGICK_SSIZE_MAX
Definition: studio.h:353
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:114
MagickExport void * ResizeQuantumMemory(void *memory, const size_t count, const size_t quantum)
Definition: memory.c:1457
#define O_BINARY
Definition: studio.h:333
MagickExport void StripString(char *message)
Definition: string.c:2504
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:133
#define MagickCoreSignature
MagickPrivate void FinalizeSignature(SignatureInfo *)
MagickPrivate SignatureInfo * DestroySignatureInfo(SignatureInfo *)
Definition: signature.c:171
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:169
MagickExport char ** StringToList(const char *text)
Definition: string.c:2290
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
size_t signature
Definition: stream.c:105
MagickExport void * AcquireCriticalMemory(const size_t size)
Definition: memory.c:626
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:665
size_t MagickSizeType
Definition: magick-type.h:134
#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:2128
MagickExport MagickBooleanType SubstituteString(char **string, const char *search, const char *replace)
Definition: string.c:2566
MagickExport char * GetEnvironmentValue(const char *name)
Definition: string.c:1135
MagickExport StringInfo * ConfigureFileToStringInfo(const char *filename)
Definition: string.c:558
MagickExport char * StringInfoToDigest(const StringInfo *signature)
Definition: string.c:1844
MagickExport StringInfo * AcquireStringInfo(const size_t length)
Definition: string.c:151
MagickPrivate void UpdateSignature(SignatureInfo *, const StringInfo *)
Definition: signature.c:766
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:2227
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:1162
MagickExport MagickBooleanType UnmapBlob(void *, const size_t)
Definition: blob.c:5553
size_t signature
Definition: string_.h:36
MagickExport char * CloneString(char **destination, const char *source)
Definition: string.c:250
MagickPrivate const StringInfo * GetSignatureDigest(const SignatureInfo *)
Definition: signature.c:327
MagickExport char ** StringToStrings(const char *text, size_t *count)
Definition: string.c:2321
#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:1959
MagickExport char * StringInfoToHexString(const StringInfo *string_info)
Definition: string.c:1882
MagickExport char ** StringToArgv(const char *text, int *argc)
Definition: string.c:2013
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