18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
25#if defined(__cplusplus) || defined(c_plusplus)
29#define MagickPathTemplate "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
34static inline ssize_t
MagickRead(
int fd,
void *buffer,
size_t extent)
36 unsigned char *p = (
unsigned char *) buffer;
38 while (offset < extent)
40 ssize_t count = read(fd,p+offset,extent-offset);
49 offset+=(size_t) count;
51 return((ssize_t) offset);
57#if defined(MAGICKCORE_HAVE_READDIR_R)
58 return(readdir_r(directory,entry,result));
67static inline ssize_t
MagickWrite(
int fd,
const void *buffer,
size_t extent)
69 const unsigned char *p = (
const unsigned char *) buffer;
71 while (offset < extent)
73 ssize_t count = write(fd,p+offset,extent-offset);
81 offset+=(size_t) count;
83 return((ssize_t) offset);
90#if defined(MAGICKCORE_WINDOWS_SUPPORT)
91static inline wchar_t *create_wchar_path(
const char *utf8)
99 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
100 if ((count > MAX_PATH) && (NTLongPathsEnabled() ==
MagickFalse))
112 if (longPath == (
wchar_t *) NULL)
113 return((
wchar_t *) NULL);
114 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
116 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
118 if ((count < 5) || (count >= MAX_PATH))
119 return((
wchar_t *) NULL);
121 wcscpy(wideChar,shortPath+4);
125 if (wideChar == (
wchar_t *) NULL)
126 return((
wchar_t *) NULL);
127 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
131 return((
wchar_t *) NULL);
139#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
140 return(access(path,mode));
148 path_wide=create_wchar_path(path);
149 if (path_wide == (
wchar_t *) NULL)
151 status=_waccess(path_wide,mode);
157#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
158#define close_utf8 _close
160#define close_utf8 close
163static inline FILE *
fopen_utf8(
const char *path,
const char *mode)
165#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
166 return(fopen(path,mode));
175 path_wide=create_wchar_path(path);
176 if (path_wide == (
wchar_t *) NULL)
177 return((FILE *) NULL);
178 mode_wide=create_wchar_path(mode);
179 if (mode_wide == (
wchar_t *) NULL)
182 return((FILE *) NULL);
184 file=_wfopen(path_wide,mode_wide);
193#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
197 directory=getcwd(path,extent);
204 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
208#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
213static inline int open_utf8(
const char *path,
int flags,mode_t mode)
215#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
216 return(open(path,flags,mode));
224 path_wide=create_wchar_path(path);
225 if (path_wide == (
wchar_t *) NULL)
227 status=_wopen(path_wide,flags,mode);
233static inline FILE *
popen_utf8(
const char *command,
const char *type)
235#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
236 return(popen(command,type));
249 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
252 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
256 if (command_wide == (
wchar_t *) NULL)
258 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
260 file=_wpopen(command_wide,type_wide);
268#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
269#if defined(MAGICKCORE_HAVE_REALPATH)
270 return(realpath(path,(
char *) NULL));
298 if (path == (
const char *) NULL)
299 return((
char *) NULL);
300 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
302 return((
char *) NULL);
303 wide_path=(
wchar_t *) AcquireQuantumMeory(length,
sizeof(
wchar_t));
304 if (wide_path == (
wchar_t *) NULL)
305 return((
char *) NULL);
306 MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,length);
310 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
311 if (full_path_length == 0)
314 return((
char *) NULL);
317 if (full_path == (
wchar_t *) NULL);
320 return((
char *) NULL);
322 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
327 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
328 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
329 FILE_FLAG_BACKUP_SEMANTICS,NULL);
330 if (file_handle == INVALID_HANDLE_VALUE)
333 return((
char *) NULL);
338 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
339 FILE_NAME_NORMALIZED);
340 if (final_path_length == 0)
342 CloseHandle(file_handle);
344 return((
char *) NULL);
348 if (final_path == (
wchar_t *) NULL)
350 CloseHandle(file_handle);
352 return((
char *) NULL);
354 GetFinalPathNameByHandleW(file_handle,final_path,final_path_length,
355 FILE_NAME_NORMALIZED);
356 CloseHandle(file_handle);
361 clean_path=final_path;
362 if (wcsncmp(final_path,L
"\\\\?\\",4) == 0)
363 clean_path=final_path+4;
367 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
368 if (utf8_length <= 0)
374 if (real_path == (
char *) NULL)
379 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
387#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
388 return(unlink(path));
396 path_wide=create_wchar_path(path);
397 if (path_wide == (
wchar_t *) NULL)
399 status=_wremove(path_wide);
405static inline int rename_utf8(
const char *source,
const char *destination)
407#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
408 return(rename(source,destination));
417 source_wide=create_wchar_path(source);
418 if (source_wide == (
wchar_t *) NULL)
420 destination_wide=create_wchar_path(destination);
421 if (destination_wide == (
wchar_t *) NULL)
426 status=_wrename(source_wide,destination_wide);
433static inline int stat_utf8(
const char *path,
struct stat *attributes)
435#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
436 return(stat(path,attributes));
444 path_wide=create_wchar_path(path);
445 if (path_wide == (WCHAR *) NULL)
447 status=wstat(path_wide,attributes);
455#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
456#if defined(MAGICKCORE_POSIX_SUPPORT)
460 if (lstat(path,&status) == -1)
467 return(NTIsSymlinkWide(path));
472#if defined(__cplusplus) || defined(c_plusplus)
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:498
MagickExport struct dirent * readdir(DIR *)
MagickBooleanType
Definition: magick-type.h:203
@ MagickFalse
Definition: magick-type.h:204
@ MagickTrue
Definition: magick-type.h:205
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1209
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:667
#define MagickPrivate
Definition: method-attribute.h:81
#define MaxTextExtent
Definition: method-attribute.h:89
MagickExport char * AcquireString(const char *source)
Definition: string.c:126
static int open_utf8(const char *path, int flags, mode_t mode)
Definition: utility-private.h:213
static int MagickReadDirectory(DIR *directory, struct dirent *entry, struct dirent **result)
Definition: utility-private.h:54
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: utility-private.h:163
static int remove_utf8(const char *path)
Definition: utility-private.h:385
static char * realpath_utf8(const char *path)
Definition: utility-private.h:266
static int access_utf8(const char *path, int mode)
Definition: utility-private.h:137
static int stat_utf8(const char *path, struct stat *attributes)
Definition: utility-private.h:433
static ssize_t MagickRead(int fd, void *buffer, size_t extent)
Definition: utility-private.h:34
static void getcwd_utf8(char *path, size_t extent)
Definition: utility-private.h:191
MagickPrivate MagickBooleanType ShredFile(const char *)
Definition: utility.c:1962
static int rename_utf8(const char *source, const char *destination)
Definition: utility-private.h:405
static MagickBooleanType is_symlink_utf8(const char *path)
Definition: utility-private.h:453
static FILE * popen_utf8(const char *command, const char *type)
Definition: utility-private.h:233
static ssize_t MagickWrite(int fd, const void *buffer, size_t extent)
Definition: utility-private.h:67