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