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