MagickCore  6.9.12-69
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 should be freed using DestroyString().
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 DestroyString()
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) CopyMagickString(clone_info->path,string_info->path,MagickPathExtent);
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 DestroyString()
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", "Ri", "Qi", (char *) NULL
1108  },
1109  *traditional_units[] =
1110  {
1111  "", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q", (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 **sentinel)
1309 %
1310 % A description of each parameter follows:
1311 %
1312 % o value: the string value.
1313 %
1314 % o sentinel: if sentinel 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 sentinel)
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 'q': e=(-30.0); break;
1338  case 'r': e=(-27.0); break;
1339  case 'y': e=(-24.0); break;
1340  case 'z': e=(-21.0); break;
1341  case 'a': e=(-18.0); break;
1342  case 'f': e=(-15.0); break;
1343  case 'p': e=(-12.0); break;
1344  case 'n': e=(-9.0); break;
1345  case 'u': e=(-6.0); break;
1346  case 'm': e=(-3.0); break;
1347  case 'c': e=(-2.0); break;
1348  case 'd': e=(-1.0); break;
1349  case 'h': e=2.0; break;
1350  case 'k': e=3.0; break;
1351  case 'K': e=3.0; break;
1352  case 'M': e=6.0; break;
1353  case 'G': e=9.0; break;
1354  case 'T': e=12.0; break;
1355  case 'P': e=15.0; break;
1356  case 'E': e=18.0; break;
1357  case 'Z': e=21.0; break;
1358  case 'Y': e=24.0; break;
1359  case 'R': e=27.0; break;
1360  case 'Q': e=30.0; break;
1361  default: e=0.0; break;
1362  }
1363  if (e >= MagickEpsilon)
1364  {
1365  if (q[1] == 'i')
1366  {
1367  value*=pow(2.0,e/0.3);
1368  q+=2;
1369  }
1370  else
1371  {
1372  value*=pow(10.0,e);
1373  q++;
1374  }
1375  }
1376  }
1377  if ((*q == 'B') || (*q == 'P'))
1378  q++;
1379  }
1380  if (sentinel != (char **) NULL)
1381  *sentinel=q;
1382  return(value);
1383 }
1384 
1385 /*
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % %
1388 % %
1389 % %
1390 % I s S t r i n g T r u e %
1391 % %
1392 % %
1393 % %
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 %
1396 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1397 % "1". Any other string or undefined returns MagickFalse.
1398 %
1399 % Typically this is used to look at strings (options or artifacts) which
1400 % has a default value of "false", when not defined.
1401 %
1402 % The format of the IsStringTrue method is:
1403 %
1404 % MagickBooleanType IsStringTrue(const char *value)
1405 %
1406 % A description of each parameter follows:
1407 %
1408 % o value: Specifies a pointer to a character array.
1409 %
1410 */
1411 MagickExport MagickBooleanType IsStringTrue(const char *value)
1412 {
1413  if (value == (const char *) NULL)
1414  return(MagickFalse);
1415  if (LocaleCompare(value,"true") == 0)
1416  return(MagickTrue);
1417  if (LocaleCompare(value,"on") == 0)
1418  return(MagickTrue);
1419  if (LocaleCompare(value,"yes") == 0)
1420  return(MagickTrue);
1421  if (LocaleCompare(value,"1") == 0)
1422  return(MagickTrue);
1423  return(MagickFalse);
1424 }
1425 
1426 /*
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 % %
1429 % %
1430 % %
1431 % I s S t r i n g N o t F a l s e %
1432 % %
1433 % %
1434 % %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 %
1437 % IsStringNotFalse() returns MagickTrue, unless the string specifically
1438 % has a value that makes this false. that is if it has a value of
1439 % "false", "off", "no" or "0".
1440 %
1441 % Typically this is used to look at strings (options or artifacts) which
1442 % has a default value of "true", when it has not been defined.
1443 %
1444 % The format of the IsStringNotFalse method is:
1445 %
1446 % MagickBooleanType IsStringNotFalse(const char *value)
1447 %
1448 % A description of each parameter follows:
1449 %
1450 % o value: Specifies a pointer to a character array.
1451 %
1452 */
1453 MagickExport MagickBooleanType IsStringNotFalse(const char *value)
1454 {
1455  if (value == (const char *) NULL)
1456  return(MagickTrue);
1457  if (LocaleCompare(value,"false") == 0)
1458  return(MagickFalse);
1459  if (LocaleCompare(value,"off") == 0)
1460  return(MagickFalse);
1461  if (LocaleCompare(value,"no") == 0)
1462  return(MagickFalse);
1463  if (LocaleCompare(value,"0") == 0)
1464  return(MagickFalse);
1465  return(MagickTrue);
1466 }
1467 
1468 /*
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 % %
1471 % %
1472 % %
1473 % P r i n t S t r i n g I n f o %
1474 % %
1475 % %
1476 % %
1477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 %
1479 % PrintStringInfo() prints the string.
1480 %
1481 % The format of the PrintStringInfo method is:
1482 %
1483 % void PrintStringInfo(FILE *file,const char *id,
1484 % const StringInfo *string_info)
1485 %
1486 % A description of each parameter follows:
1487 %
1488 % o file: the file, typically stdout.
1489 %
1490 % o id: the string id.
1491 %
1492 % o string_info: the string info.
1493 %
1494 */
1495 MagickExport void PrintStringInfo(FILE *file,const char *id,
1496  const StringInfo *string_info)
1497 {
1498  const char
1499  *p;
1500 
1501  size_t
1502  i,
1503  j;
1504 
1505  assert(id != (const char *) NULL);
1506  assert(string_info != (StringInfo *) NULL);
1507  assert(string_info->signature == MagickCoreSignature);
1508  p=(char *) string_info->datum;
1509  for (i=0; i < string_info->length; i++)
1510  {
1511  if (((int) ((unsigned char) *p) < 32) &&
1512  (isspace((int) ((unsigned char) *p)) == 0))
1513  break;
1514  p++;
1515  }
1516  (void) FormatLocaleFile(file,"%s(%.20g):\n",id,(double) string_info->length);
1517  if (i == string_info->length)
1518  {
1519  for (i=0; i < string_info->length; i++)
1520  (void) fputc(string_info->datum[i],file);
1521  (void) fputc('\n',file);
1522  return;
1523  }
1524  /*
1525  Convert string to a HEX list.
1526  */
1527  p=(char *) string_info->datum;
1528  for (i=0; i < string_info->length; i+=CharsPerLine)
1529  {
1530  (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (CharsPerLine*i));
1531  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1532  {
1533  (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1534  if ((j % 0x04) == 0)
1535  (void) fputc(' ',file);
1536  }
1537  for ( ; j <= CharsPerLine; j++)
1538  {
1539  (void) fputc(' ',file);
1540  (void) fputc(' ',file);
1541  if ((j % 0x04) == 0)
1542  (void) fputc(' ',file);
1543  }
1544  (void) fputc(' ',file);
1545  for (j=1; j <= MagickMin(string_info->length-i,CharsPerLine); j++)
1546  {
1547  if (isprint((int) ((unsigned char) *p)) != 0)
1548  (void) fputc(*p,file);
1549  else
1550  (void) fputc('-',file);
1551  p++;
1552  }
1553  (void) fputc('\n',file);
1554  }
1555 }
1556 
1557 /*
1558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1559 % %
1560 % %
1561 % %
1562 % R e s e t S t r i n g I n f o %
1563 % %
1564 % %
1565 % %
1566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 %
1568 % ResetStringInfo() reset the string to all null bytes.
1569 %
1570 % The format of the ResetStringInfo method is:
1571 %
1572 % void ResetStringInfo(StringInfo *string_info)
1573 %
1574 % A description of each parameter follows:
1575 %
1576 % o string_info: the string info.
1577 %
1578 */
1579 MagickExport void ResetStringInfo(StringInfo *string_info)
1580 {
1581  assert(string_info != (StringInfo *) NULL);
1582  assert(string_info->signature == MagickCoreSignature);
1583  (void) memset(string_info->datum,0,string_info->length);
1584 }
1585 
1586 /*
1587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1588 % %
1589 % %
1590 % %
1591 % S a n t i z e S t r i n g %
1592 % %
1593 % %
1594 % %
1595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596 %
1597 % SanitizeString() returns a new string with all characters removed except
1598 % letters, digits and !#$%&'*+-=?^_`{|}~@.[].
1599 %
1600 % Free the sanitized string with DestroyString().
1601 %
1602 % The format of the SanitizeString method is:
1603 %
1604 % char *SanitizeString(const char *source)
1605 %
1606 % A description of each parameter follows:
1607 %
1608 % o source: A character string.
1609 %
1610 */
1611 MagickExport char *SanitizeString(const char *source)
1612 {
1613  char
1614  *sanitize_source;
1615 
1616  const char
1617  *q;
1618 
1619  char
1620  *p;
1621 
1622  static char
1623  allowlist[] =
1624  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
1625  "$-_.+!*'(),{}|\\^~[]`\"><#%;/?:@&=";
1626 
1627  sanitize_source=AcquireString(source);
1628  p=sanitize_source;
1629  q=sanitize_source+strlen(sanitize_source);
1630  for (p+=strspn(p,allowlist); p != q; p+=strspn(p,allowlist))
1631  *p='_';
1632  return(sanitize_source);
1633 }
1634 
1635 /*
1636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637 % %
1638 % %
1639 % %
1640 % S e t S t r i n g I n f o %
1641 % %
1642 % %
1643 % %
1644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645 %
1646 % SetStringInfo() copies the source string to the destination string.
1647 %
1648 % The format of the SetStringInfo method is:
1649 %
1650 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1651 %
1652 % A description of each parameter follows:
1653 %
1654 % o string_info: the string info.
1655 %
1656 % o source: the source string.
1657 %
1658 */
1659 MagickExport void SetStringInfo(StringInfo *string_info,
1660  const StringInfo *source)
1661 {
1662  assert(string_info != (StringInfo *) NULL);
1663  assert(string_info->signature == MagickCoreSignature);
1664  assert(source != (StringInfo *) NULL);
1665  assert(source->signature == MagickCoreSignature);
1666  if (string_info->length == 0)
1667  return;
1668  (void) memset(string_info->datum,0,string_info->length);
1669  (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1670  source->length));
1671 }
1672 
1673 /*
1674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1675 % %
1676 % %
1677 % %
1678 % S e t S t r i n g I n f o D a t u m %
1679 % %
1680 % %
1681 % %
1682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1683 %
1684 % SetStringInfoDatum() copies bytes from the source string for the length of
1685 % the destination string.
1686 %
1687 % The format of the SetStringInfoDatum method is:
1688 %
1689 % void SetStringInfoDatum(StringInfo *string_info,
1690 % const unsigned char *source)
1691 %
1692 % A description of each parameter follows:
1693 %
1694 % o string_info: the string info.
1695 %
1696 % o source: the source string.
1697 %
1698 */
1699 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1700  const unsigned char *source)
1701 {
1702  assert(string_info != (StringInfo *) NULL);
1703  assert(string_info->signature == MagickCoreSignature);
1704  if (string_info->length != 0)
1705  (void) memcpy(string_info->datum,source,string_info->length);
1706 }
1707 
1708 /*
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % %
1711 % %
1712 % %
1713 % S e t S t r i n g I n f o L e n g t h %
1714 % %
1715 % %
1716 % %
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1718 %
1719 % SetStringInfoLength() set the string length to the specified value.
1720 %
1721 % The format of the SetStringInfoLength method is:
1722 %
1723 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1724 %
1725 % A description of each parameter follows:
1726 %
1727 % o string_info: the string info.
1728 %
1729 % o length: the string length.
1730 %
1731 */
1732 MagickExport void SetStringInfoLength(StringInfo *string_info,
1733  const size_t length)
1734 {
1735  assert(string_info != (StringInfo *) NULL);
1736  assert(string_info->signature == MagickCoreSignature);
1737  if (string_info->length == length)
1738  return;
1739  if (~length < MaxTextExtent)
1740  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1741  string_info->length=length;
1742  if (string_info->datum == (unsigned char *) NULL)
1743  string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1744  MaxTextExtent,sizeof(*string_info->datum));
1745  else
1746  string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1747  length+MaxTextExtent,sizeof(*string_info->datum));
1748  if (string_info->datum == (unsigned char *) NULL)
1749  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1750 }
1751 
1752 /*
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1754 % %
1755 % %
1756 % %
1757 % S e t S t r i n g I n f o N a m e %
1758 % %
1759 % %
1760 % %
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1762 %
1763 % SetStringInfoName() sets the name associated with the string.
1764 %
1765 % The format of the SetStringInfoName method is:
1766 %
1767 % void SetStringInfoName(StringInfo *string_info,const char *name)
1768 %
1769 % A description of each parameter follows:
1770 %
1771 % o string_info: the string info.
1772 %
1773 % o name: the name.
1774 %
1775 */
1776 MagickExport void SetStringInfoName(StringInfo *string_info,const char *name)
1777 {
1778  assert(string_info != (StringInfo *) NULL);
1779  assert(string_info->signature == MagickCoreSignature);
1780  assert(name != (const char *) NULL);
1781  string_info->name=ConstantString(name);
1782 }
1783 
1784 /*
1785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1786 % %
1787 % %
1788 % %
1789 % S e t S t r i n g I n f o P a t h %
1790 % %
1791 % %
1792 % %
1793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 %
1795 % SetStringInfoPath() sets the path associated with the string.
1796 %
1797 % The format of the SetStringInfoPath method is:
1798 %
1799 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1800 %
1801 % A description of each parameter follows:
1802 %
1803 % o string_info: the string info.
1804 %
1805 % o path: the path.
1806 %
1807 */
1808 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1809 {
1810  assert(string_info != (StringInfo *) NULL);
1811  assert(string_info->signature == MagickCoreSignature);
1812  assert(path != (const char *) NULL);
1813  (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1814 }
1815 
1816 /*
1817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 % %
1819 % %
1820 % %
1821 % S p l i t S t r i n g I n f o %
1822 % %
1823 % %
1824 % %
1825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826 %
1827 % SplitStringInfo() splits a string into two and returns it.
1828 %
1829 % The format of the SplitStringInfo method is:
1830 %
1831 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1832 %
1833 % A description of each parameter follows:
1834 %
1835 % o string_info: the string info.
1836 %
1837 */
1838 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1839  const size_t offset)
1840 {
1841  StringInfo
1842  *split_info;
1843 
1844  assert(string_info != (StringInfo *) NULL);
1845  assert(string_info->signature == MagickCoreSignature);
1846  if (offset > string_info->length)
1847  return((StringInfo *) NULL);
1848  split_info=AcquireStringInfo(offset);
1849  SetStringInfo(split_info,string_info);
1850  (void) memmove(string_info->datum,string_info->datum+offset,
1851  string_info->length-offset+MaxTextExtent);
1852  SetStringInfoLength(string_info,string_info->length-offset);
1853  return(split_info);
1854 }
1855 
1856 /*
1857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1858 % %
1859 % %
1860 % %
1861 % S t r i n g I n f o T o S t r i n g %
1862 % %
1863 % %
1864 % %
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866 %
1867 % StringInfoToString() converts a string info string to a C string.
1868 %
1869 % The format of the StringInfoToString method is:
1870 %
1871 % char *StringInfoToString(const StringInfo *string_info)
1872 %
1873 % A description of each parameter follows:
1874 %
1875 % o string_info: the string.
1876 %
1877 */
1878 MagickExport char *StringInfoToString(const StringInfo *string_info)
1879 {
1880  char
1881  *string;
1882 
1883  size_t
1884  length;
1885 
1886  string=(char *) NULL;
1887  length=string_info->length;
1888  if (~length >= (MaxTextExtent-1))
1889  string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
1890  if (string == (char *) NULL)
1891  return((char *) NULL);
1892  (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
1893  string[length]='\0';
1894  return(string);
1895 }
1896 
1897 /*
1898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1899 % %
1900 % %
1901 % %
1902 % S t r i n g I n f o T o H e x S t r i n g %
1903 % %
1904 % %
1905 % %
1906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1907 %
1908 % StringInfoToHexString() converts a string info string to a C string.
1909 %
1910 % The format of the StringInfoToHexString method is:
1911 %
1912 % char *StringInfoToHexString(const StringInfo *string_info)
1913 %
1914 % A description of each parameter follows:
1915 %
1916 % o string_info: the string.
1917 %
1918 */
1919 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
1920 {
1921  char
1922  *string;
1923 
1924  const unsigned char
1925  *p;
1926 
1927  ssize_t
1928  i;
1929 
1930  unsigned char
1931  *q;
1932 
1933  size_t
1934  length;
1935 
1936  unsigned char
1937  hex_digits[16];
1938 
1939  length=string_info->length;
1940  if (~length < MaxTextExtent)
1941  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1942  string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
1943  if (string == (char *) NULL)
1944  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
1945  hex_digits[0]='0';
1946  hex_digits[1]='1';
1947  hex_digits[2]='2';
1948  hex_digits[3]='3';
1949  hex_digits[4]='4';
1950  hex_digits[5]='5';
1951  hex_digits[6]='6';
1952  hex_digits[7]='7';
1953  hex_digits[8]='8';
1954  hex_digits[9]='9';
1955  hex_digits[10]='a';
1956  hex_digits[11]='b';
1957  hex_digits[12]='c';
1958  hex_digits[13]='d';
1959  hex_digits[14]='e';
1960  hex_digits[15]='f';
1961  p=string_info->datum;
1962  q=(unsigned char *) string;
1963  for (i=0; i < (ssize_t) string_info->length; i++)
1964  {
1965  *q++=hex_digits[(*p >> 4) & 0x0f];
1966  *q++=hex_digits[*p & 0x0f];
1967  p++;
1968  }
1969  *q='\0';
1970  return(string);
1971 }
1972 
1973 /*
1974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1975 % %
1976 % %
1977 % %
1978 % S t r i n g T o A r g v %
1979 % %
1980 % %
1981 % %
1982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1983 %
1984 % StringToArgv() converts a text string into command line arguments.
1985 % The 'argv' array of arguments, is returned while the number of arguments
1986 % is returned via the provided integer variable pointer.
1987 %
1988 % Simple 'word' tokenizer, which allows for each word to be optionally
1989 % quoted. However it will not allow use of partial quotes, or escape
1990 % characters.
1991 %
1992 % The format of the StringToArgv method is:
1993 %
1994 % char **StringToArgv(const char *text,int *argc)
1995 %
1996 % A description of each parameter follows:
1997 %
1998 % o argv: Method StringToArgv returns the string list unless an error
1999 % occurs, otherwise NULL.
2000 %
2001 % o text: Specifies the string to segment into a list.
2002 %
2003 % o argc: This integer pointer returns the number of arguments in the
2004 % list.
2005 %
2006 */
2007 MagickExport char **StringToArgv(const char *text,int *argc)
2008 {
2009  char
2010  **argv;
2011 
2012  const char
2013  *p,
2014  *q;
2015 
2016  ssize_t
2017  i;
2018 
2019  *argc=0;
2020  if (text == (char *) NULL)
2021  return((char **) NULL);
2022  /*
2023  Determine the number of arguments.
2024  */
2025  for (p=text; *p != '\0'; )
2026  {
2027  while (isspace((int) ((unsigned char) *p)) != 0)
2028  p++;
2029  if (*p == '\0')
2030  break;
2031  (*argc)++;
2032  if (*p == '"')
2033  for (p++; (*p != '"') && (*p != '\0'); p++) ;
2034  if (*p == '\'')
2035  for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2036  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2037  p++;
2038  }
2039  (*argc)++;
2040  argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2041  if (argv == (char **) NULL)
2042  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2043  /*
2044  Convert string to an ASCII list.
2045  */
2046  argv[0]=AcquireString("magick");
2047  p=text;
2048  for (i=1; i < (ssize_t) *argc; i++)
2049  {
2050  while (isspace((int) ((unsigned char) *p)) != 0)
2051  p++;
2052  q=p;
2053  if (*q == '"')
2054  {
2055  p++;
2056  for (q++; (*q != '"') && (*q != '\0'); q++) ;
2057  }
2058  else
2059  if (*q == '\'')
2060  {
2061  p++;
2062  for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2063  }
2064  else
2065  while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2066  q++;
2067  argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2068  sizeof(**argv));
2069  if (argv[i] == (char *) NULL)
2070  {
2071  for (i--; i >= 0; i--)
2072  argv[i]=DestroyString(argv[i]);
2073  argv=(char **) RelinquishMagickMemory(argv);
2074  ThrowFatalException(ResourceLimitFatalError,
2075  "UnableToConvertStringToARGV");
2076  }
2077  (void) memcpy(argv[i],p,(size_t) (q-p));
2078  argv[i][q-p]='\0';
2079  p=q;
2080  while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2081  p++;
2082  }
2083  argv[i]=(char *) NULL;
2084  return(argv);
2085 }
2086 
2087 /*
2088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2089 % %
2090 % %
2091 % %
2092 % S t r i n g T o A r r a y O f D o u b l e s %
2093 % %
2094 % %
2095 % %
2096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097 %
2098 % StringToArrayOfDoubles() converts a string of space or comma separated
2099 % numbers into array of floating point numbers (doubles). Any number that
2100 % fails to parse properly will produce a syntax error. As will two commas
2101 % without a number between them. However a final comma at the end will
2102 % not be regarded as an error so as to simplify automatic list generation.
2103 %
2104 % A NULL value is returned on syntax or memory errors.
2105 %
2106 % Use RelinquishMagickMemory() to free returned array when finished.
2107 %
2108 % The format of the StringToArrayOfDoubles method is:
2109 %
2110 % double *StringToArrayOfDoubles(const char *string,size_t *count,
2111 % ExceptionInfo *exception)
2112 %
2113 % A description of each parameter follows:
2114 %
2115 % o string: the string containing the comma/space separated values.
2116 %
2117 % o count: returns number of arguments in returned array
2118 %
2119 % o exception: return 'memory failure' exceptions
2120 %
2121 */
2122 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2123  ExceptionInfo *exception)
2124 {
2125  char
2126  *q;
2127 
2128  const char
2129  *p;
2130 
2131  double
2132  *array;
2133 
2134  ssize_t
2135  i;
2136 
2137  /*
2138  Determine count of values, and check syntax.
2139  */
2140  assert(exception != (ExceptionInfo *) NULL);
2141  assert(exception->signature == MagickCoreSignature);
2142  *count=0;
2143  if (string == (char *) NULL)
2144  return((double *) NULL); /* no value found */
2145  i=0;
2146  p=string;
2147  while (*p != '\0')
2148  {
2149  (void) StringToDouble(p,&q); /* get value - ignores leading space */
2150  if (p == q)
2151  return((double *) NULL); /* no value found */
2152  p=q;
2153  i++; /* increment value count */
2154  while (isspace((int) ((unsigned char) *p)) != 0)
2155  p++; /* skip spaces */
2156  if (*p == ',')
2157  p++; /* skip comma */
2158  while (isspace((int) ((unsigned char) *p)) != 0)
2159  p++; /* and more spaces */
2160  }
2161  /*
2162  Allocate floating point argument list.
2163  */
2164  *count=i;
2165  array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2166  if (array == (double *) NULL)
2167  {
2168  (void) ThrowMagickException(exception,GetMagickModule(),
2169  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2170  return((double *) NULL);
2171  }
2172  /*
2173  Fill in the floating point values.
2174  */
2175  i=0;
2176  p=string;
2177  while ((*p != '\0') && (i < *count))
2178  {
2179  array[i++]=StringToDouble(p,&q);
2180  p=q;
2181  while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2182  p++;
2183  }
2184  return(array);
2185 }
2186 
2187 /*
2188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2189 % %
2190 % %
2191 % %
2192 + S t r i n g T o k e n %
2193 % %
2194 % %
2195 % %
2196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2197 %
2198 % StringToken() looks for any one of given delimiters and splits the string
2199 % into two separate strings by replacing the delimiter character found with a
2200 % null character.
2201 %
2202 % The given string pointer is changed to point to the string following the
2203 % delimiter character found, or NULL. A pointer to the start of the
2204 % string is returned, representing the token before the delimiter.
2205 %
2206 % StringToken() is equivent to the strtok() C library method, but with
2207 % multiple delimiter characters rather than a delimiter string.
2208 %
2209 % The format of the StringToken method is:
2210 %
2211 % char *StringToken(const char *delimiters,char **string)
2212 %
2213 % A description of each parameter follows:
2214 %
2215 % o delimiters: one or more delimiters.
2216 %
2217 % o string: return the first token in the string. If none is found, return
2218 % NULL.
2219 %
2220 */
2221 MagickExport char *StringToken(const char *delimiters,char **string)
2222 {
2223  char
2224  *q;
2225 
2226  char
2227  *p;
2228 
2229  const char
2230  *r;
2231 
2232  int
2233  c,
2234  d;
2235 
2236  p=(*string);
2237  if (p == (char *) NULL)
2238  return((char *) NULL);
2239  q=p;
2240  for ( ; ; )
2241  {
2242  c=(*p++);
2243  r=delimiters;
2244  do
2245  {
2246  d=(*r++);
2247  if (c == d)
2248  {
2249  if (c == '\0')
2250  p=(char *) NULL;
2251  else
2252  p[-1]='\0';
2253  *string=p;
2254  return(q);
2255  }
2256  } while (d != '\0');
2257  }
2258 }
2259 
2260 /*
2261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2262 % %
2263 % %
2264 % %
2265 % S t r i n g T o L i s t %
2266 % %
2267 % %
2268 % %
2269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2270 %
2271 % StringToList() converts a text string into a list by segmenting the text
2272 % string at each carriage return discovered. The list is converted to HEX
2273 % characters if any control characters are discovered within the text string.
2274 %
2275 % The format of the StringToList method is:
2276 %
2277 % char **StringToList(const char *text)
2278 %
2279 % A description of each parameter follows:
2280 %
2281 % o text: Specifies the string to segment into a list.
2282 %
2283 */
2284 MagickExport char **StringToList(const char *text)
2285 {
2286  return(StringToStrings(text,(size_t *) NULL));
2287 }
2288 
2289 /*
2290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291 % %
2292 % %
2293 % %
2294 % S t r i n g T o S t r i n g s %
2295 % %
2296 % %
2297 % %
2298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2299 %
2300 % StringToStrings() converts a text string into a list by segmenting the text
2301 % string at each carriage return discovered. The list is converted to HEX
2302 % characters if any control characters are discovered within the text string.
2303 %
2304 % The format of the StringToList method is:
2305 %
2306 % char **StringToList(const char *text,size_t *lines)
2307 %
2308 % A description of each parameter follows:
2309 %
2310 % o text: Specifies the string to segment into a list.
2311 %
2312 % o count: Return value for the number of items in the list.
2313 %
2314 */
2315 MagickExport char **StringToStrings(const char *text,size_t *count)
2316 {
2317  char
2318  **textlist;
2319 
2320  const char
2321  *p;
2322 
2323  ssize_t
2324  i;
2325 
2326  size_t
2327  lines;
2328 
2329  if (text == (char *) NULL)
2330  {
2331  if (count != (size_t *) NULL)
2332  *count=0;
2333  return((char **) NULL);
2334  }
2335  for (p=text; *p != '\0'; p++)
2336  if (((int) ((unsigned char) *p) < 32) &&
2337  (isspace((int) ((unsigned char) *p)) == 0))
2338  break;
2339  if (*p == '\0')
2340  {
2341  const char
2342  *q;
2343 
2344  /*
2345  Convert string to an ASCII list.
2346  */
2347  lines=1;
2348  for (p=text; *p != '\0'; p++)
2349  if (*p == '\n')
2350  lines++;
2351  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2352  sizeof(*textlist));
2353  if (textlist == (char **) NULL)
2354  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2355  p=text;
2356  for (i=0; i < (ssize_t) lines; i++)
2357  {
2358  for (q=p; *q != '\0'; q++)
2359  if ((*q == '\r') || (*q == '\n'))
2360  break;
2361  textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
2362  sizeof(**textlist));
2363  if (textlist[i] == (char *) NULL)
2364  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2365  (void) memcpy(textlist[i],p,(size_t) (q-p));
2366  textlist[i][q-p]='\0';
2367  if (*q == '\r')
2368  q++;
2369  p=q+1;
2370  }
2371  }
2372  else
2373  {
2374  char
2375  hex_string[MagickPathExtent];
2376 
2377  char
2378  *q;
2379 
2380  ssize_t
2381  j;
2382 
2383  /*
2384  Convert string to a HEX list.
2385  */
2386  lines=(size_t) (strlen(text)/CharsPerLine)+1;
2387  textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2388  sizeof(*textlist));
2389  if (textlist == (char **) NULL)
2390  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2391  p=text;
2392  for (i=0; i < (ssize_t) lines; i++)
2393  {
2394  size_t
2395  length;
2396 
2397  textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
2398  sizeof(**textlist));
2399  if (textlist[i] == (char *) NULL)
2400  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2401  (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
2402  (long) (CharsPerLine*i));
2403  q=textlist[i]+strlen(textlist[i]);
2404  length=strlen(p);
2405  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2406  {
2407  (void) FormatLocaleString(hex_string,MagickPathExtent,"%02x",*(p+j));
2408  (void) CopyMagickString(q,hex_string,MagickPathExtent);
2409  q+=2;
2410  if ((j % 0x04) == 0)
2411  *q++=' ';
2412  }
2413  for ( ; j <= CharsPerLine; j++)
2414  {
2415  *q++=' ';
2416  *q++=' ';
2417  if ((j % 0x04) == 0)
2418  *q++=' ';
2419  }
2420  *q++=' ';
2421  for (j=1; j <= (ssize_t) MagickMin(length,CharsPerLine); j++)
2422  {
2423  if (isprint((int) ((unsigned char) *p)) != 0)
2424  *q++=(*p);
2425  else
2426  *q++='-';
2427  p++;
2428  }
2429  *q='\0';
2430  textlist[i]=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
2431  textlist[i]+1),sizeof(**textlist));
2432  if (textlist[i] == (char *) NULL)
2433  ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2434  }
2435  }
2436  if (count != (size_t *) NULL)
2437  *count=lines;
2438  textlist[i]=(char *) NULL;
2439  return(textlist);
2440 }
2441 
2442 /*
2443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2444 % %
2445 % %
2446 % %
2447 % S t r i n g T o S t r i n g I n f o %
2448 % %
2449 % %
2450 % %
2451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2452 %
2453 % StringToStringInfo() converts a string to a StringInfo type.
2454 %
2455 % The format of the StringToStringInfo method is:
2456 %
2457 % StringInfo *StringToStringInfo(const char *string)
2458 %
2459 % A description of each parameter follows:
2460 %
2461 % o string: The string.
2462 %
2463 */
2464 MagickExport StringInfo *StringToStringInfo(const char *string)
2465 {
2466  StringInfo
2467  *string_info;
2468 
2469  assert(string != (const char *) NULL);
2470  string_info=AcquireStringInfo(strlen(string));
2471  SetStringInfoDatum(string_info,(const unsigned char *) string);
2472  return(string_info);
2473 }
2474 
2475 /*
2476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2477 % %
2478 % %
2479 % %
2480 % S t r i p S t r i n g %
2481 % %
2482 % %
2483 % %
2484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2485 %
2486 % StripString() strips any whitespace or quotes from the beginning and end of
2487 % a string of characters.
2488 %
2489 % The format of the StripString method is:
2490 %
2491 % void StripString(char *message)
2492 %
2493 % A description of each parameter follows:
2494 %
2495 % o message: Specifies an array of characters.
2496 %
2497 */
2498 MagickExport void StripString(char *message)
2499 {
2500  char
2501  *p,
2502  *q;
2503 
2504  size_t
2505  length;
2506 
2507  assert(message != (char *) NULL);
2508  if (*message == '\0')
2509  return;
2510  length=strlen(message);
2511  p=message;
2512  while (isspace((int) ((unsigned char) *p)) != 0)
2513  p++;
2514  if ((*p == '\'') || (*p == '"'))
2515  p++;
2516  q=message+length-1;
2517  while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2518  q--;
2519  if (q > p)
2520  if ((*q == '\'') || (*q == '"'))
2521  q--;
2522  (void) memmove(message,p,(size_t) (q-p+1));
2523  message[q-p+1]='\0';
2524  for (p=message; *p != '\0'; p++)
2525  if (*p == '\n')
2526  *p=' ';
2527 }
2528 
2529 /*
2530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2531 % %
2532 % %
2533 % %
2534 % S u b s t i t u t e S t r i n g %
2535 % %
2536 % %
2537 % %
2538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2539 %
2540 % SubstituteString() performs string substitution on a string, replacing the
2541 % string with the substituted version. Buffer must be allocated from the heap.
2542 % If the string is matched and status, MagickTrue is returned otherwise
2543 % MagickFalse.
2544 %
2545 % The format of the SubstituteString method is:
2546 %
2547 % MagickBooleanType SubstituteString(char **string,const char *search,
2548 % const char *replace)
2549 %
2550 % A description of each parameter follows:
2551 %
2552 % o string: the string to perform replacements on; replaced with new
2553 % allocation if a replacement is made.
2554 %
2555 % o search: search for this string.
2556 %
2557 % o replace: replace any matches with this string.
2558 %
2559 */
2560 MagickExport MagickBooleanType SubstituteString(char **string,
2561  const char *search,const char *replace)
2562 {
2563  MagickBooleanType
2564  status;
2565 
2566  char
2567  *p;
2568 
2569  size_t
2570  extent,
2571  replace_extent,
2572  search_extent;
2573 
2574  ssize_t
2575  offset;
2576 
2577  status=MagickFalse;
2578  search_extent=0,
2579  replace_extent=0;
2580  for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2581  {
2582  if (search_extent == 0)
2583  search_extent=strlen(search);
2584  if (strncmp(p,search,search_extent) != 0)
2585  continue;
2586  /*
2587  We found a match.
2588  */
2589  status=MagickTrue;
2590  if (replace_extent == 0)
2591  replace_extent=strlen(replace);
2592  if (replace_extent > search_extent)
2593  {
2594  /*
2595  Make room for the replacement string.
2596  */
2597  offset=(ssize_t) (p-(*string));
2598  extent=strlen(*string)+replace_extent-search_extent+1;
2599  *string=(char *) ResizeQuantumMemory(*string,
2600  OverAllocateMemory(extent+MaxTextExtent),sizeof(*p));
2601  if (*string == (char *) NULL)
2602  ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2603  p=(*string)+offset;
2604  }
2605  /*
2606  Replace string.
2607  */
2608  if (search_extent != replace_extent)
2609  (void) memmove(p+replace_extent,p+search_extent,
2610  strlen(p+search_extent)+1);
2611  (void) memcpy(p,replace,replace_extent);
2612  p+=replace_extent-1;
2613  }
2614  return(status);
2615 }