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