18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
21#include "magick/memory_.h"
22#include "magick/nt-base.h"
23#include "magick/nt-base-private.h"
25#if defined(__cplusplus) || defined(c_plusplus)
29extern MagickPrivate MagickBooleanType
30 ShredFile(
const char *);
32static inline int MagickReadDirectory(
DIR *directory,
struct dirent *entry,
37 *result=readdir(directory);
45#if defined(MAGICKCORE_WINDOWS_SUPPORT)
46static inline wchar_t *create_wchar_path(
const char *utf8)
54 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
55 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
58 buffer[MaxTextExtent];
64 (void) FormatLocaleString(buffer,MaxTextExtent,
"\\\\?\\%s",utf8);
66 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
68 if (longPath == (
wchar_t *) NULL)
69 return((
wchar_t *) NULL);
70 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
72 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
73 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
74 if ((count < 5) || (count >= MAX_PATH))
75 return((
wchar_t *) NULL);
76 wideChar=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
78 wcscpy(wideChar,shortPath+4);
81 wideChar=(
wchar_t *) NTAcquireQuantumMemory(count,
sizeof(*wideChar));
82 if (wideChar == (
wchar_t *) NULL)
83 return((
wchar_t *) NULL);
84 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
87 wideChar=(
wchar_t *) RelinquishMagickMemory(wideChar);
88 return((
wchar_t *) NULL);
94static inline int access_utf8(
const char *path,
int mode)
96 if (path == (
const char *) NULL)
98#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
99 return(access(path,mode));
107 path_wide=create_wchar_path(path);
108 if (path_wide == (
wchar_t *) NULL)
110 status=_waccess(path_wide,mode);
111 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
116static inline FILE *fopen_utf8(
const char *path,
const char *mode)
118#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
119 return(fopen(path,mode));
128 path_wide=create_wchar_path(path);
129 if (path_wide == (
wchar_t *) NULL)
130 return((FILE *) NULL);
131 mode_wide=create_wchar_path(mode);
132 if (mode_wide == (
wchar_t *) NULL)
134 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
135 return((FILE *) NULL);
137 file=_wfopen(path_wide,mode_wide);
138 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
139 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
144static inline void getcwd_utf8(
char *path,
size_t extent)
146#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
150 directory=getcwd(path,extent);
154 wide_path[MaxTextExtent];
156 (void) _wgetcwd(wide_path,MaxTextExtent-1);
157 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
161#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
166static inline int open_utf8(
const char *path,
int flags,mode_t mode)
168#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
169 return(open(path,flags,mode));
177 path_wide=create_wchar_path(path);
178 if (path_wide == (
wchar_t *) NULL)
180 status=_wopen(path_wide,flags,mode);
181 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
186static inline FILE *popen_utf8(
const char *command,
const char *type)
188#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
189 return(popen(command,type));
202 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
205 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
208 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
209 sizeof(*command_wide));
210 if (command_wide == (
wchar_t *) NULL)
212 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
214 file=_wpopen(command_wide,type_wide);
215 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
220static inline char *realpath_utf8(
const char *path)
222#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223#if defined(MAGICKCORE_HAVE_REALPATH)
224 return(realpath(path,(
char *) NULL));
226 return(AcquireString(path));
252 if (path == (
const char *) NULL)
253 return((
char *) NULL);
254 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
256 return((
char *) NULL);
257 wide_path=(
wchar_t *) AcquireQuantumMeory(length,
sizeof(
wchar_t));
258 if (wide_path == (
wchar_t *) NULL)
259 return((
char *) NULL);
260 MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,length);
264 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
265 if (full_path_length == 0)
267 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
268 return((
char *) NULL);
270 full_path=(
wchar_t *) AcquireQuantumMemory(full_path_length,
sizeof(
wchar_t));
271 if (full_path == (
wchar_t *) NULL);
273 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
274 return((
char *) NULL);
276 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
277 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
281 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
282 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
283 FILE_FLAG_BACKUP_SEMANTICS,NULL);
284 if (file_handle == INVALID_HANDLE_VALUE)
286 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
287 return((
char *) NULL);
292 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
293 FILE_NAME_NORMALIZED);
294 if (final_path_length == 0)
296 CloseHandle(file_handle);
297 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
298 return((
char *) NULL);
300 final_path=(
wchar_t *) AcquireQuantumMemory(final_path_length,
302 if (final_path == (
wchar_t *) NULL)
304 CloseHandle(file_handle);
305 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
306 return((
char *) NULL);
308 GetFinalPathNameByHandleW(file_handle,final_path,final_path_length,
309 FILE_NAME_NORMALIZED);
310 CloseHandle(file_handle);
311 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
315 clean_path=final_path;
316 if (wcsncmp(final_path,L
"\\\\?\\",4) == 0)
317 clean_path=final_path+4;
321 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
322 if (utf8_length <= 0)
324 final_path=(
wchar_t *) RelinquishMagickMemory(final_path);
327 real_path=(
char *) AcquireQuantumMemory(utf8_length,
sizeof(
char));
328 if (real_path == (
char *) NULL)
330 final_path=(
wchar_t *) RelinquishMagickMemory(final_path);
333 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
334 final_path=(
wchar_t *) RelinquishMagickMemory(final_path);
339static inline int remove_utf8(
const char *path)
341#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
342 return(unlink(path));
350 path_wide=create_wchar_path(path);
351 if (path_wide == (
wchar_t *) NULL)
353 status=_wremove(path_wide);
354 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
359static inline int rename_utf8(
const char *source,
const char *destination)
361#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
362 return(rename(source,destination));
371 source_wide=create_wchar_path(source);
372 if (source_wide == (
wchar_t *) NULL)
374 destination_wide=create_wchar_path(destination);
375 if (destination_wide == (
wchar_t *) NULL)
377 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
380 status=_wrename(source_wide,destination_wide);
381 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
382 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
387static inline int stat_utf8(
const char *path,
struct stat *attributes)
389#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
390 return(stat(path,attributes));
398 path_wide=create_wchar_path(path);
399 if (path_wide == (WCHAR *) NULL)
401 status=wstat(path_wide,attributes);
402 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
407#if defined(__cplusplus) || defined(c_plusplus)