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)
43#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
44#define close_utf8 _close
46#define close_utf8 close
49static inline unsigned int CRC32(
const unsigned char *message,
55 static MagickBooleanType
56 crc_initial = MagickFalse;
67 if (crc_initial == MagickFalse)
75 for (j=0; j < 256; j++)
82 alpha=(alpha & 0x01) ? (0xEDB88320 ^ (alpha >> 1)) : (alpha >> 1);
85 crc_initial=MagickTrue;
88 for (i=0; i < (ssize_t) length; i++)
89 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
90 return(crc ^ 0xFFFFFFFF);
93static inline const struct tm *GetMagickUTCTime(
const time_t *timep,
96#if defined(MAGICKCORE_HAVE_GMTIME_R)
97 (void) gmtime_r(timep,result);
103 my_time=gmtime(timep);
104 if (my_time != (
struct tm *) NULL)
105 (void) memcpy(result,my_time,
sizeof(*my_time));
111#if defined(MAGICKCORE_WINDOWS_SUPPORT)
112static inline wchar_t *CreateWidePath(
const char *path)
123 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
124 if ((count > MAX_PATH) && (strncmp(path,
"\\\\?\\",4) != 0) &&
125 (NTLongPathsEnabled() == MagickFalse))
128 buffer[MagickPathExtent];
132 short_path[MAX_PATH];
134 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",path);
136 long_path=(
wchar_t *) AcquireQuantumMemory(count,
sizeof(*long_path));
137 if (long_path == (
wchar_t *) NULL)
138 return((
wchar_t *) NULL);
139 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,long_path,count);
141 count=GetShortPathNameW(long_path,short_path,MAX_PATH);
142 long_path=(
wchar_t *) RelinquishMagickMemory(long_path);
143 if ((count < 5) || (count >= MAX_PATH))
144 return((
wchar_t *) NULL);
145 wide_path=(
wchar_t *) AcquireQuantumMemory(count-3,
sizeof(*wide_path));
146 wcscpy(wide_path,short_path+4);
149 wide_path=(
wchar_t *) AcquireQuantumMemory(count,
sizeof(*wide_path));
150 if (wide_path == (
wchar_t *) NULL)
151 return((
wchar_t *) NULL);
152 count=MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,count);
155 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
156 return((
wchar_t *) NULL);
161static inline struct dirent *NTReadDirectory(DIR *entry)
169 if (entry == (DIR *) NULL)
170 return((
struct dirent *) NULL);
171 if (!entry->firsttime)
173 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
175 return((
struct dirent *) NULL);
177 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
178 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
180 return((
struct dirent *) NULL);
181 entry->firsttime=FALSE;
182 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
183 return(&entry->file_info);
187static inline MagickBooleanType MagickCreatePath(
const char *path)
203 extent=2*strlen(path)+2;
204 directed_walk=(
char *) AcquireCriticalMemory(extent*
sizeof(*directed_walk));
207 (void) ConcatenateMagickString(directed_walk,
"/",extent);
208 directed_path=ConstantString(path);
209 for (p=strtok(directed_path,
"/"); p != (
char *) NULL; p=strtok(NULL,
"/"))
211 (void) ConcatenateMagickString(directed_walk,p,extent);
212 (void) ConcatenateMagickString(directed_walk,
"/",extent);
213 if (GetPathAttributes(directed_walk,&attributes) == MagickFalse)
215#if defined(MAGICKCORE_WINDOWS_SUPPORT)
220 wide_path=CreateWidePath(directed_walk);
221 if (wide_path == (
wchar_t *) NULL)
226 status=_wmkdir(wide_path);
227 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
230 status=mkdir(directed_walk,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
239 directed_path=DestroyString(directed_path);
240 directed_walk=DestroyString(directed_walk);
241 return(status == 0 ? MagickTrue : MagickFalse);
244static inline int open_utf8(
const char *path,
int flags,mode_t mode)
246#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
247 return(open(path,flags,mode));
255 path_wide=create_wchar_path(path);
256 if (path_wide == (
wchar_t *) NULL)
258 status=_wopen(path_wide,flags,mode);
259 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
264static inline int remove_utf8(
const char *path)
266#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
267 return(remove(path));
275 path_wide=create_wchar_path(path);
276 if (path_wide == (
wchar_t *) NULL)
278 status=_wremove(path_wide);
279 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
284#if defined(__cplusplus) || defined(c_plusplus)