42#include "MagickCore/studio.h"
43#include "MagickCore/property.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/color.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/geometry.h"
49#include "MagickCore/image-private.h"
50#include "MagickCore/list.h"
51#include "MagickCore/log.h"
52#include "MagickCore/magick-private.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/nt-base-private.h"
55#include "MagickCore/option.h"
56#include "MagickCore/policy.h"
57#include "MagickCore/policy-private.h"
58#include "MagickCore/random_.h"
59#include "MagickCore/registry.h"
60#include "MagickCore/resource_.h"
61#include "MagickCore/semaphore.h"
62#include "MagickCore/signature-private.h"
63#include "MagickCore/statistic.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/string-private.h"
66#include "MagickCore/token.h"
67#include "MagickCore/token-private.h"
68#include "MagickCore/utility.h"
69#include "MagickCore/utility-private.h"
70#if defined(MAGICKCORE_HAVE_PROCESS_H)
73#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
74#include <mach-o/dyld.h>
81 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
90static const signed char
91 Base64DecodeTable[256] =
93 -1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-1,-1,
94 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
95 -2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
96 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-3,-1,-1,
97 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
98 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
99 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
100 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
101 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
102 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
103 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
104 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
105 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
106 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
107 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
108 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
115 IsPathDirectory(
const char *);
140MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
145 file=AcquireUniqueFileResource(path);
148 (void) close_utf8(file);
182MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
208 assert(source != (
const char *) NULL);
209 assert(destination != (
char *) NULL);
210#if defined(MAGICKCORE_HAVE_SYMLINK)
215 status=IsRightsAuthorizedByName(SystemPolicyDomain,
"symlink",(PolicyRights)
216 (ReadPolicyRights | WritePolicyRights),
"follow");
218 passes=GetShredPasses();
219 if ((passes == 0) && (status != MagickFalse))
221 if (AcquireUniqueFilename(destination) == MagickFalse)
223 (void) RelinquishUniqueFileResource(destination);
224 if (*source == *DirectorySeparator)
226 if (symlink(source,destination) == 0)
232 path[MagickPathExtent];
235 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
237 if ((strlen(path)+strlen(DirectorySeparator)+strlen(source)) >=
240 (void) ConcatenateMagickString(path,DirectorySeparator,
242 (void) ConcatenateMagickString(path,source,MagickPathExtent);
243 if (symlink(path,destination) == 0)
252 destination_file=AcquireUniqueFileResource(destination);
253 if (destination_file == -1)
255 source_file=open_utf8(source,O_RDONLY | O_CLOEXEC | O_BINARY,0);
256 if (source_file == -1)
258 (void) close_utf8(destination_file);
259 (void) RelinquishUniqueFileResource(destination);
262 quantum=(size_t) MagickMaxBufferExtent;
263 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
264 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
265 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
266 if (buffer == (
unsigned char *) NULL)
268 (void) close_utf8(source_file);
269 (void) close_utf8(destination_file);
270 (void) RelinquishUniqueFileResource(destination);
276 count=(ssize_t) read(source_file,buffer,quantum);
281 (void) RelinquishUniqueFileResource(destination);
287 length=(size_t) count;
293 while (written < length)
295 count=(ssize_t) write(destination_file,buffer+written,length-written);
304 written+=(size_t) count;
306 if (written != length)
308 (void) RelinquishUniqueFileResource(destination);
314 if (close_utf8(destination_file) != 0)
316 if (status != MagickFalse)
317 (void) RelinquishUniqueFileResource(destination);
320 (void) close_utf8(source_file);
321 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
352MagickExport
void AppendImageFormat(
const char *format,
char *filename)
355 extension[MagickPathExtent],
356 root[MagickPathExtent];
358 assert(format != (
char *) NULL);
359 assert(filename != (
char *) NULL);
360 if (IsEventLogging() != MagickFalse)
361 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
362 if ((*format ==
'\0') || (*filename ==
'\0'))
364 if (LocaleCompare(filename,
"-") == 0)
367 message[MagickPathExtent];
369 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
371 (void) CopyMagickString(filename,message,MagickPathExtent);
374 GetPathComponent(filename,ExtensionPath,extension);
375 if ((LocaleCompare(extension,
"Z") == 0) ||
376 (LocaleCompare(extension,
"bz2") == 0) ||
377 (LocaleCompare(extension,
"gz") == 0) ||
378 (LocaleCompare(extension,
"wmz") == 0) ||
379 (LocaleCompare(extension,
"svgz") == 0))
381 GetPathComponent(filename,RootPath,root);
382 (void) CopyMagickString(filename,root,MagickPathExtent);
383 GetPathComponent(filename,RootPath,root);
384 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
388 GetPathComponent(filename,RootPath,root);
389 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
418MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
433 assert(source != (
char *) NULL);
434 assert(length != (
size_t *) NULL);
435 if (IsEventLogging() != MagickFalse)
436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
438 source_length=strlen(source);
439 if (source_length > (SIZE_MAX-3))
440 return((
unsigned char *) NULL);
441 decode=(
unsigned char *) AcquireQuantumMemory((source_length+3)/4,
443 if (decode == (
unsigned char *) NULL)
444 return((
unsigned char *) NULL);
447 for (p=source; *p !=
'\0'; p++)
452 value=Base64DecodeTable[(
unsigned char) *p];
459 decode=(
unsigned char *) RelinquishMagickMemory(decode);
460 return((
unsigned char *) NULL);
466 decode[i]=(
unsigned char) (value << 2);
472 decode[i++]|=(
unsigned char) (value >> 4);
473 decode[i]=(
unsigned char) ((value & 0x0f) << 4);
479 decode[i++]|=(
unsigned char) (value >> 2);
480 decode[i]=(
unsigned char) ((value & 0x03) << 6);
486 decode[i++]|=(
unsigned char) value;
499 decode=(
unsigned char *) RelinquishMagickMemory(decode);
500 return((
unsigned char *) NULL);
514 decode=(
unsigned char *) RelinquishMagickMemory(decode);
515 return((
unsigned char *) NULL);
519 for ( ; *p !=
'\0'; p++)
520 if (Base64DecodeTable[(
unsigned char) *p] != -2)
524 decode=(
unsigned char *) RelinquishMagickMemory(decode);
525 return((
unsigned char *) NULL);
532 for ( ; *p !=
'\0'; p++)
533 if (Base64DecodeTable[(
unsigned char) *p] != -2)
535 decode=(
unsigned char *) RelinquishMagickMemory(decode);
536 return((
unsigned char *) NULL);
538 if ((
int) decode[i] != 0)
540 decode=(
unsigned char *) RelinquishMagickMemory(decode);
541 return((
unsigned char *) NULL);
581MagickExport
char *Base64Encode(
const unsigned char *blob,
582 const size_t blob_length,
size_t *encode_length)
595 assert(blob != (
const unsigned char *) NULL);
596 assert(blob_length != 0);
597 assert(encode_length != (
size_t *) NULL);
598 if (IsEventLogging() != MagickFalse)
599 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
601 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
602 if (encode == (
char *) NULL)
603 return((
char *) NULL);
605 remainder=blob_length % 3;
606 end=blob+(blob_length-remainder);
607 for (p=blob; p < end; p+=(ptrdiff_t) 3)
609 encode[i++]=Base64[(int) (*p >> 2)];
610 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
611 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
612 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
625 for (j=0; j < (ssize_t) remainder; j++)
627 encode[i++]=Base64[(int) (code[0] >> 2)];
628 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
632 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
665MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
670 for (i=0; i < (ssize_t) components; i++)
671 GetPathComponent(path,HeadPath,path);
697MagickPrivate
void ExpandFilename(
char *path)
700 expand_path[MagickPathExtent];
702 if (path == (
char *) NULL)
706 (void) CopyMagickString(expand_path,path,MagickPathExtent);
707 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
715 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
716 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
717 home=GetEnvironmentValue(
"HOME");
718 if (home == (
char *) NULL)
719 home=GetEnvironmentValue(
"USERPROFILE");
720 if (home != (
char *) NULL)
722 (void) CopyMagickString(expand_path,home,MagickPathExtent);
723 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
724 home=DestroyString(home);
729#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
731#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
732 buffer[MagickPathExtent],
734 username[MagickPathExtent];
746 (void) CopyMagickString(username,path+1,MagickPathExtent);
747 p=strchr(username,
'/');
748 if (p != (
char *) NULL)
750#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
751 entry=getpwnam(username);
753 entry=(
struct passwd *) NULL;
754 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) != 0)
757 if (entry == (
struct passwd *) NULL)
759 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
760 if (p != (
char *) NULL)
762 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
763 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
767 (void) CopyMagickString(path,expand_path,MagickPathExtent);
826static inline void getcwd_utf8(
char *path,
size_t extent)
828#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
832 directory=getcwd(path,extent);
836 wide_path[MagickPathExtent];
838 (void) _wgetcwd(wide_path,MagickPathExtent-1);
839 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
843MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
847 home_directory[MagickPathExtent],
865 assert(number_arguments != (
int *) NULL);
866 assert(arguments != (
char ***) NULL);
867 if (IsEventLogging() != MagickFalse)
868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
869 if (*number_arguments < 0)
871 vector_capacity=(size_t) *number_arguments+1;
872 vector=(
char **) AcquireQuantumMemory(vector_capacity,
sizeof(*vector));
873 if (vector == (
char **) NULL)
874 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
878 *home_directory=
'\0';
880 for (i=0; i < (ssize_t) *number_arguments; i++)
884 filename[MagickPathExtent],
885 magick[MagickPathExtent],
887 path[MagickPathExtent],
888 subimage[MagickPathExtent];
893 option=(*arguments)[i];
899 if ((
size_t) (count+1) >= vector_capacity)
901 vector_capacity=(size_t) count+2;
902 vector=(
char **) ResizeQuantumMemory(vector,vector_capacity,
904 if (vector == (
char **) NULL)
905 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
907 vector[count++]=ConstantString(option);
909 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
915 for (j=0; j < parameters; j++)
918 if (i == (ssize_t) *number_arguments)
920 option=(*arguments)[i];
921 if ((
size_t) (count+1) >= vector_capacity)
923 vector_capacity=(size_t) count+2;
924 vector=(
char **) ResizeQuantumMemory(vector,vector_capacity,
926 if (vector == (
char **) NULL)
927 ThrowFatalException(ResourceLimitFatalError,
928 "MemoryAllocationFailed");
930 vector[count++]=ConstantString(option);
934 if ((*option ==
'"') || (*option ==
'\''))
936 GetPathComponent(option,TailPath,filename);
937 GetPathComponent(option,MagickPath,magick);
938 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
939 (LocaleCompare(magick,
"LABEL") == 0) ||
940 (LocaleCompare(magick,
"PANGO") == 0) ||
941 (LocaleCompare(magick,
"VID") == 0))
943 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
945 if (IsPathAccessible(option) != MagickFalse)
952 GetPathComponent(option,HeadPath,path);
953 GetPathComponent(option,SubimagePath,subimage);
954 ExpandFilename(path);
955 if (*home_directory ==
'\0')
956 getcwd_utf8(home_directory,MagickPathExtent-1);
957 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
974 exception=AcquireExceptionInfo();
975 files=FileToString(option,~0UL,exception);
976 exception=DestroyExceptionInfo(exception);
977 if (files == (
char *) NULL)
979 filelist=StringToArgv(files,&length);
980 files=DestroyString(files);
981 if (filelist == (
char **) NULL)
986 filelist[0]=DestroyString(filelist[0]);
987 filelist=(
char **) RelinquishMagickMemory(filelist);
990 filelist[0]=DestroyString(filelist[0]);
991 for (j=0; j < (ssize_t) (length-1); j++)
992 filelist[j]=filelist[j+1];
993 number_files=(size_t) (length-1);
995 if (filelist == (
char **) NULL)
997 for (j=0; j < (ssize_t) number_files; j++)
1000 candidate[MagickPathExtent];
1004 (void) CopyMagickString(candidate,path,MagickPathExtent);
1005 (void) ConcatenateMagickString(candidate,DirectorySeparator,
1007 (void) ConcatenateMagickString(candidate,filelist[j],
1011 (
void) CopyMagickString(candidate,filelist[j],MagickPathExtent);
1012 if (IsPathDirectory(candidate) <= 0)
1015 if (j == (ssize_t) number_files)
1017 for (j=0; j < (ssize_t) number_files; j++)
1018 filelist[j]=DestroyString(filelist[j]);
1019 filelist=(
char **) RelinquishMagickMemory(filelist);
1025 if (((
size_t) count+number_files+((
size_t) *number_arguments-(
size_t) i)) >=
1028 vector_capacity=(size_t) count+number_files+
1029 ((
size_t) *number_arguments-(
size_t) i)+1;
1030 vector=(
char **) ResizeQuantumMemory(vector,vector_capacity,
1032 if (vector == (
char **) NULL)
1034 for (j=0; j < (ssize_t) number_files; j++)
1035 filelist[j]=DestroyString(filelist[j]);
1036 filelist=(
char **) RelinquishMagickMemory(filelist);
1037 return(MagickFalse);
1040 for (j=0; j < (ssize_t) number_files; j++)
1043 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
1052 if (destroy != MagickFalse)
1055 vector[count]=DestroyString(vector[count]);
1056 destroy=MagickFalse;
1058 vector[count++]=option;
1059 filelist[j]=(
char *) NULL;
1060 for (k=0; k < parameters; k++)
1063 if (j == (ssize_t) number_files)
1065 vector[count++]=filelist[j];
1066 filelist[j]=(
char *) NULL;
1070 (void) CopyMagickString(filename,path,MagickPathExtent);
1072 (void) ConcatenateMagickString(filename,DirectorySeparator,
1074 if (filelist[j] != (
char *) NULL)
1075 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
1076 filelist[j]=DestroyString(filelist[j]);
1077 if (strlen(filename) >= (MagickPathExtent-1))
1078 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
1079 if (IsPathDirectory(filename) <= 0)
1082 file_path[MagickPathExtent];
1085 if (*magick !=
'\0')
1087 (void) ConcatenateMagickString(file_path,magick,
1089 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
1091 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
1092 if (*subimage !=
'\0')
1094 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
1095 (void) ConcatenateMagickString(file_path,subimage,
1097 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
1099 if (strlen(file_path) >= (MagickPathExtent-1))
1100 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
1101 if (destroy != MagickFalse)
1104 vector[count]=DestroyString(vector[count]);
1105 destroy=MagickFalse;
1107 vector[count++]=ConstantString(file_path);
1110 filelist=(
char **) RelinquishMagickMemory(filelist);
1112 vector[count]=(
char *) NULL;
1113 if (IsEventLogging() != MagickFalse)
1118 command_line=AcquireString(vector[0]);
1119 for (i=1; i < count; i++)
1121 (void) ConcatenateString(&command_line,
" {");
1122 (void) ConcatenateString(&command_line,vector[i]);
1123 (void) ConcatenateString(&command_line,
"}");
1125 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1126 "Command line: %s",command_line);
1127 command_line=DestroyString(command_line);
1129 *number_arguments=(int) count;
1159MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1165 directory=getcwd(path,(
unsigned long) extent);
1167#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1170 execution_path[PATH_MAX+1],
1171 link_path[MagickPathExtent];
1176 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.17g/exe",
1178 count=readlink(link_path,execution_path,PATH_MAX);
1181 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.17g/file",
1183 count=readlink(link_path,execution_path,PATH_MAX);
1185 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1187 execution_path[count]=
'\0';
1188 (void) CopyMagickString(path,execution_path,extent);
1192#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1195 executable_path[PATH_MAX << 1];
1200 length=
sizeof(executable_path);
1201 if (_NSGetExecutablePath(executable_path,&length) == 0)
1204 *real_path = realpath_utf8(executable_path);
1206 if (real_path != (
char *) NULL)
1208 (void) CopyMagickString(path,real_path,extent);
1209 real_path=DestroyString(real_path);
1214#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1219 execution_path=(
const char *) getexecname();
1220 if (execution_path != (
const char *) NULL)
1222 if (*execution_path != *DirectorySeparator)
1223 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1224 (void) ConcatenateMagickString(path,execution_path,extent);
1228#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1229 NTGetExecutionPath(path,extent);
1240 program_name=program_invocation_name;
1241 if (*program_invocation_name !=
'/')
1246 name_extent=strlen(directory)+strlen(program_name)+2;
1247 program_name=(
char *) AcquireQuantumMemory(name_extent,
1248 sizeof(*program_name));
1249 if (program_name == (
char *) NULL)
1250 program_name=program_invocation_name;
1252 count=FormatLocaleString(program_name,name_extent,
"%s/%s",directory,
1253 program_invocation_name);
1258 *real_path = realpath_utf8(program_name);
1260 if (real_path != (
char *) NULL)
1262 (void) CopyMagickString(path,real_path,extent);
1263 real_path=DestroyString(real_path);
1266 if (program_name != program_invocation_name)
1267 program_name=(
char *) RelinquishMagickMemory(program_name);
1270#if defined(__OpenBSD__)
1275 (void) CopyMagickString(path,__progname,extent);
1278 return(IsPathAccessible(path));
1299MagickPrivate ssize_t GetMagickPageSize(
void)
1306#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1307 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1308#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1309 page_size=(ssize_t) getpagesize();
1340MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1346 if (path == (
const char *) NULL)
1349 return(MagickFalse);
1351 (void) memset(attributes,0,
sizeof(
struct stat));
1352 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1388MagickExport
void GetPathComponent(
const char *path,PathType type,
1401 assert(path != (
const char *) NULL);
1402 assert(component != (
char *) NULL);
1403 if (IsEventLogging() != MagickFalse)
1404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1410 (void) CopyMagickString(component,path,MagickPathExtent);
1411 component_length=strlen(component);
1414 if (type != SubcanonicalPath)
1416 p=component+component_length-1;
1417 if ((component_length > 2) && (*p ==
']'))
1419 q=strrchr(component,
'[');
1420 if (q != (
char *) NULL)
1422 ExceptionInfo *exception = AcquireExceptionInfo();
1423 char *literal = (
char *) GetImageRegistry(StringRegistryType,
1424 "filename:literal",exception);
1425 exception=DestroyExceptionInfo(exception);
1426 if ((IsStringTrue(literal) == MagickFalse) ||
1427 (IsPathAccessible(path) == MagickFalse))
1433 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1434 (IsGeometry(q+1) == MagickFalse))
1438 subimage_length=(size_t) (p-q);
1439 subimage_offset=(size_t) (q-component+1);
1443 if (literal != (
char *) NULL)
1444 literal=DestroyString(literal);
1452 for (p=component; *p !=
'\0'; p++)
1454 if ((*p ==
'%') && (*(p+1) ==
'['))
1459 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1463 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1464 (IsPathAccessible(component) == MagickFalse))
1470 if (IsMagickConflict(component) != MagickFalse)
1474 magick_length=(size_t) (p-component+1);
1475 for (q=component; *(++p) !=
'\0'; q++)
1484 for (p=component+strlen(component)-1; p > component; p--)
1485 if (IsBasenameSeparator(*p) != MagickFalse)
1491 if (magick_length != 0)
1492 (void) CopyMagickString(component,path,magick_length);
1499 if (*component !=
'\0')
1501 for (p=component+(strlen(component)-1); p > component; p--)
1503 if (IsBasenameSeparator(*p) != MagickFalse)
1521 if (IsBasenameSeparator(*p) != MagickFalse)
1522 (void) CopyMagickString(component,p+1,MagickPathExtent);
1527 if (IsBasenameSeparator(*p) != MagickFalse)
1528 (void) CopyMagickString(component,p+1,MagickPathExtent);
1529 if (*component !=
'\0')
1530 for (p=component+(strlen(component)-1); p > component; p--)
1538 case BasePathSansCompressExtension:
1541 extension[MagickPathExtent];
1546 GetPathComponent(path,ExtensionPath,extension);
1547 if ((LocaleCompare(extension,
"bz2") == 0) ||
1548 (LocaleCompare(extension,
"gz") == 0) ||
1549 (LocaleCompare(extension,
"svgz") == 0) ||
1550 (LocaleCompare(extension,
"wmz") == 0) ||
1551 (LocaleCompare(extension,
"Z") == 0))
1553 if (IsBasenameSeparator(*p) != MagickFalse)
1556 if ((q != (
char *) NULL) && (q > p))
1563 if (IsBasenameSeparator(*p) != MagickFalse)
1564 (void) CopyMagickString(component,p+1,MagickPathExtent);
1565 if (*component !=
'\0')
1566 for (p=component+strlen(component)-1; p > component; p--)
1571 (void) CopyMagickString(component,p+1,MagickPathExtent);
1577 if ((subimage_length != 0) && (magick_length < subimage_offset))
1578 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1581 case SubcanonicalPath:
1613MagickPrivate
char **GetPathComponents(
const char *path,
1614 size_t *number_components)
1626 assert(number_components != (
size_t *) NULL);
1627 *number_components=0;
1628 if (path == (
char *) NULL)
1629 return((
char **) NULL);
1630 *number_components=1;
1631 for (p=path; *p !=
'\0'; p++)
1632 if (IsBasenameSeparator(*p))
1633 (*number_components)++;
1634 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1635 sizeof(*components));
1636 if (components == (
char **) NULL)
1638 *number_components=0;
1639 return((
char **) NULL);
1642 for (i=0; i < (ssize_t) *number_components; i++)
1644 for (q=p; *q !=
'\0'; q++)
1645 if (IsBasenameSeparator(*q))
1647 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1648 sizeof(**components));
1649 if (components[i] == (
char*)NULL)
1654 for (j=0; j < i; j++)
1655 components[j]=DestroyString(components[j]);
1656 components=(
char **) RelinquishMagickMemory(components);
1657 *number_components=0;
1658 return((
char **) NULL);
1660 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1663 components[i]=(
char *) NULL;
1691MagickExport MagickBooleanType GetPathTemplate(
char *path)
1709 assert(path != (
char *) NULL);
1710 (void) FormatLocaleString(path,MagickPathExtent,
"magick-"
1711 MagickPathTemplate);
1712 exception=AcquireExceptionInfo();
1713 directory=(
char *) GetImageRegistry(StringRegistryType,
"temporary-path",
1715 exception=DestroyExceptionInfo(exception);
1716 if (directory == (
char *) NULL)
1717 directory=GetEnvironmentValue(
"MAGICK_TEMPORARY_PATH");
1718 if (directory == (
char *) NULL)
1719 directory=GetEnvironmentValue(
"MAGICK_TMPDIR");
1720 if (directory == (
char *) NULL)
1721 directory=GetEnvironmentValue(
"TMPDIR");
1722#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
1723 if (directory == (
char *) NULL)
1724 directory=GetEnvironmentValue(
"TMP");
1725 if (directory == (
char *) NULL)
1726 directory=GetEnvironmentValue(
"TEMP");
1729 if (directory == (
char *) NULL)
1730 directory=GetEnvironmentValue(
"MTMPDIR");
1732#if defined(P_tmpdir)
1733 if (directory == (
char *) NULL)
1734 directory=ConstantString(P_tmpdir);
1736 if (directory == (
char *) NULL)
1737 return(MagickFalse);
1738 value=GetPolicyValue(
"resource:temporary-path");
1739 if (value != (
char *) NULL)
1741 (void) CloneString(&directory,value);
1742 value=DestroyString(value);
1744 if (directory == (
char *) NULL)
1745 return(MagickFalse);
1746 directory_length=strlen(directory);
1747 if ((directory_length == 0) || (directory_length > (MagickPathExtent-25)))
1749 directory=DestroyString(directory);
1750 return(MagickFalse);
1752 status=GetPathAttributes(directory,&attributes);
1753 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
1755 directory=DestroyString(directory);
1756 return(MagickFalse);
1758 if (directory[directory_length-1] == *DirectorySeparator)
1759 (void) FormatLocaleString(path,MagickPathExtent,
"%smagick-"
1760 MagickPathTemplate,directory);
1762 (
void) FormatLocaleString(path,MagickPathExtent,
1763 "%s%smagick-" MagickPathTemplate,directory,DirectorySeparator);
1764 directory=DestroyString(directory);
1765#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1774 for (p=(path[1] == *DirectorySeparator ? path+2 : path); *p !=
'\0'; p++)
1775 if (*p == *DirectorySeparator)
1805MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1813 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1814 return(MagickFalse);
1815 if (LocaleCompare(path,
"-") == 0)
1817 status=GetPathAttributes(path,&attributes);
1818 if (status == MagickFalse)
1820 if (S_ISREG(attributes.st_mode) == 0)
1821 return(MagickFalse);
1822 if (access_utf8(path,F_OK) != 0)
1823 return(MagickFalse);
1850static int IsPathDirectory(
const char *path)
1858 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1860 status=GetPathAttributes(path,&attributes);
1861 if (status == MagickFalse)
1863 if (S_ISDIR(attributes.st_mode) == 0)
1903#if defined(__cplusplus) || defined(c_plusplus)
1907static int FileCompare(
const void *x,
const void *y)
1913 p=(
const char **) x;
1914 q=(
const char **) y;
1915 return(LocaleCompare(*p,*q));
1918#if defined(__cplusplus) || defined(c_plusplus)
1922MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1923 size_t *number_entries)
1927 full_path[MagickPathExtent];
1943 assert(directory != (
const char *) NULL);
1944 assert(pattern != (
const char *) NULL);
1945 assert(number_entries != (
size_t *) NULL);
1946 if (IsEventLogging() != MagickFalse)
1947 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1949 current_directory=opendir(directory);
1950 if (current_directory == (DIR *) NULL)
1951 return((
char **) NULL);
1956 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1958 if (filelist == (
char **) NULL)
1960 (void) closedir(current_directory);
1961 return((
char **) NULL);
1963 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1964 if (buffer == (
struct dirent *) NULL)
1966 filelist=(
char **) RelinquishMagickMemory(filelist);
1967 (void) closedir(current_directory);
1968 return((
char **) NULL);
1970 directory_length=strlen(directory);
1971 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1972 (entry != (
struct dirent *) NULL))
1977 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1978 (LocaleCompare(entry->d_name,
"..") == 0))
1980 if ((directory_length > 0) &&
1981 (directory[directory_length-1] == *DirectorySeparator))
1982 (void) FormatLocaleString(full_path,MagickPathExtent,
"%s%s",directory,
1985 (
void) FormatLocaleString(full_path,MagickPathExtent,
"%s%s%s",directory,
1986 DirectorySeparator,entry->d_name);
1987 is_directory=IsPathDirectory(full_path);
1988 if ((is_directory > 0) ||
1989#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1990 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1992 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1995 if (*number_entries >= max_entries)
2004 resized_filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
2005 max_entries,
sizeof(*filelist));
2006 if (resized_filelist == (
char **) NULL)
2011 for (i=0; i < *number_entries; i++)
2012 filelist[i]=DestroyString(filelist[i]);
2013 filelist=(
char **) RelinquishMagickMemory(filelist);
2017 filelist=resized_filelist;
2024 p=strchr(entry->d_name,
';');
2027 if (*number_entries > 0)
2028 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
2032 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
2033 (*number_entries)++;
2036 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
2037 (void) closedir(current_directory);
2038 if (filelist == (
char **) NULL)
2039 return((
char **) NULL);
2043 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
2072MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
2074 if (milliseconds == 0)
2076#if defined(MAGICKCORE_HAVE_NANOSLEEP)
2082 timer.tv_sec=(time_t) (milliseconds/1000);
2083 timer.tv_nsec=(long) ((milliseconds % 1000)*1000*1000);
2084 while ((nanosleep(&timer,&remaining) == -1) && (errno == EINTR))
2087#elif defined(MAGICKCORE_HAVE_USLEEP)
2092 remaining=milliseconds;
2093 while (remaining > 0)
2098 chunk=(useconds_t) (remaining > 500000 ? 500000 : remaining)*1000;
2099 (void) usleep(chunk);
2100 remaining-=(MagickSizeType) (chunk/1000);
2103#elif defined(MAGICKCORE_HAVE_SELECT)
2108 timer.tv_sec=(long) (milliseconds/1000);
2109 timer.tv_usec=(long) ((milliseconds % 1000)*1000);
2110 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
2112#elif defined(MAGICKCORE_HAVE_POLL)
2113 (void) poll((
struct pollfd *) NULL,0,(
int) (milliseconds > INT_MAX ? INT_MAX :
2115#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
2120 remaining=milliseconds;
2121 while (remaining > 0)
2126 chunk=(DWORD) (remaining > (MagickSizeType) 0x7fffffff ? 0x7fffffff :
2129 remaining-=(MagickSizeType) chunk;
2137 timer=milliseconds/1000.0;
2140#elif defined(__BEOS__)
2141 snooze(1000*milliseconds);
2147 time_end=clock()+(clock_t) (milliseconds*CLOCKS_PER_SEC/1000);
2148 while (clock() < time_end)
2178MagickExport
size_t MultilineCensus(
const char *label)
2186 if (label == (
char *) NULL)
2188 for (number_lines=1; *label !=
'\0'; label++)
2191 return(number_lines);
2217MagickPrivate MagickBooleanType ShredFile(
const char *path)
2244 if ((path == (
const char *) NULL) || (*path ==
'\0'))
2245 return(MagickFalse);
2247 passes=GetShredPasses();
2253 file=open_utf8(path,O_WRONLY | O_CLOEXEC | O_EXCL | O_BINARY,S_MODE);
2255 return(MagickFalse);
2256 if ((fstat(file,&file_stats) != 0) || (file_stats.st_size < 0))
2258 (void) close_utf8(file);
2259 return(MagickFalse);
2261 if (file_stats.st_size == 0)
2263 status=close_utf8(file);
2264 return(status == -1 ? MagickFalse : MagickTrue);
2266 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
2267 length=(MagickSizeType) file_stats.st_size;
2268 random_info=AcquireRandomInfo();
2269 if (random_info == (RandomInfo *) NULL)
2271 (void) close_utf8(file);
2272 return(MagickFalse);
2274 key=GetRandomKey(random_info,quantum);
2275 if (key == (StringInfo *) NULL)
2277 random_info=DestroyRandomInfo(random_info);
2278 (void) close_utf8(file);
2279 return(MagickFalse);
2281 for (i=0; i < passes; i++)
2289 if (lseek(file,0,SEEK_SET) < 0)
2291 for (j=0; j < (MagickOffsetType) length; j+=count)
2294 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
2295 count=MagickWrite(file,GetStringInfoDatum(key),(
size_t)
2296 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
2300 if (j < (MagickOffsetType) length)
2303 key=DestroyStringInfo(key);
2304 random_info=DestroyRandomInfo(random_info);
2305 status=close_utf8(file);
2306 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);