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