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