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