18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
21#if defined(__cplusplus) || defined(c_plusplus)
29#define MagickCacheSentinel ".magickcache.sentinel"
30#define MagickCacheResourceSentinel ".magickcache.resource.sentinel"
31#define MagickCacheMin(x,y) (((x) < (y)) ? (x) : (y))
37#if defined(MAGICKCORE_WINDOWS_SUPPORT)
39# define readdir(directory) NTReadDirectory(directory)
43static inline unsigned int CRC32(
const unsigned char *message,
49 static MagickBooleanType
50 crc_initial = MagickFalse;
61 if (crc_initial == MagickFalse)
69 for (j=0; j < 256; j++)
76 alpha=(alpha & 0x01) ? (0xEDB88320 ^ (alpha >> 1)) : (alpha >> 1);
79 crc_initial=MagickTrue;
82 for (i=0; i < (ssize_t) length; i++)
83 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
84 return(crc ^ 0xFFFFFFFF);
87static inline const struct tm *GetMagickUTCTime(
const time_t *timep,
90#if defined(MAGICKCORE_HAVE_GMTIME_R)
91 (void) gmtime_r(timep,result);
97 my_time=gmtime(timep);
98 if (my_time != (
struct tm *) NULL)
99 (void) memcpy(result,my_time,
sizeof(*my_time));
105#if defined(MAGICKCORE_WINDOWS_SUPPORT)
106static inline wchar_t *CreateWidePath(
const char *path)
117 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
118 if ((count > MAX_PATH) && (strncmp(path,
"\\\\?\\",4) != 0) &&
119 (NTLongPathsEnabled() == MagickFalse))
122 buffer[MagickPathExtent];
126 short_path[MAX_PATH];
128 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",path);
130 long_path=(
wchar_t *) AcquireQuantumMemory(count,
sizeof(*long_path));
131 if (long_path == (
wchar_t *) NULL)
132 return((
wchar_t *) NULL);
133 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,long_path,count);
135 count=GetShortPathNameW(long_path,short_path,MAX_PATH);
136 long_path=(
wchar_t *) RelinquishMagickMemory(long_path);
137 if ((count < 5) || (count >= MAX_PATH))
138 return((
wchar_t *) NULL);
139 wide_path=(
wchar_t *) AcquireQuantumMemory(count-3,
sizeof(*wide_path));
140 wcscpy(wide_path,short_path+4);
143 wide_path=(
wchar_t *) AcquireQuantumMemory(count,
sizeof(*wide_path));
144 if (wide_path == (
wchar_t *) NULL)
145 return((
wchar_t *) NULL);
146 count=MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,count);
149 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
150 return((
wchar_t *) NULL);
155static inline struct dirent *NTReadDirectory(DIR *entry)
163 if (entry == (DIR *) NULL)
164 return((
struct dirent *) NULL);
165 if (!entry->firsttime)
167 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
169 return((
struct dirent *) NULL);
171 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
172 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
174 return((
struct dirent *) NULL);
175 entry->firsttime=FALSE;
176 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
177 return(&entry->file_info);
181static inline MagickBooleanType MagickCreatePath(
const char *path)
197 extent=2*strlen(path)+2;
198 directed_walk=(
char *) AcquireCriticalMemory(extent*
sizeof(*directed_walk));
201 (void) ConcatenateMagickString(directed_walk,
"/",extent);
202 directed_path=ConstantString(path);
203 for (p=strtok(directed_path,
"/"); p != (
char *) NULL; p=strtok(NULL,
"/"))
205 (void) ConcatenateMagickString(directed_walk,p,extent);
206 (void) ConcatenateMagickString(directed_walk,
"/",extent);
207 if (GetPathAttributes(directed_walk,&attributes) == MagickFalse)
209#if defined(MAGICKCORE_WINDOWS_SUPPORT)
214 wide_path=CreateWidePath(directed_walk);
215 if (wide_path == (
wchar_t *) NULL)
220 status=_wmkdir(wide_path);
221 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
224 status=mkdir(directed_walk,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
233 directed_path=DestroyString(directed_path);
234 directed_walk=DestroyString(directed_walk);
235 return(status == 0 ? MagickTrue : MagickFalse);
238static inline int open_utf8(
const char *path,
int flags,mode_t mode)
240#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
241 return(open(path,flags,mode));
249 path_wide=create_wchar_path(path);
250 if (path_wide == (
wchar_t *) NULL)
252 status=_wopen(path_wide,flags,mode);
253 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
258static inline int remove_utf8(
const char *path)
260#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
261 return(remove(path));
269 path_wide=create_wchar_path(path);
270 if (path_wide == (
wchar_t *) NULL)
272 status=_wremove(path_wide);
273 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
278#if defined(__cplusplus) || defined(c_plusplus)