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