2 Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 https://www.imagemagick.org/script/license.php
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 MagickCore private utility methods.
18 #ifndef MAGICKCORE_UTILITY_PRIVATE_H
19 #define MAGICKCORE_UTILITY_PRIVATE_H
21 #include "MagickCore/memory_.h"
22 #include "MagickCore/nt-base.h"
23 #include "MagickCore/nt-base-private.h"
25 #if defined(__cplusplus) || defined(c_plusplus)
29 extern MagickPrivate char
30 **GetPathComponents(const char *,size_t *),
31 **ListFiles(const char *,const char *,size_t *);
33 extern MagickPrivate MagickBooleanType
34 GetExecutionPath(char *,const size_t),
35 ShredFile(const char *);
37 extern MagickPrivate ssize_t
38 GetMagickPageSize(void);
40 extern MagickPrivate void
41 ChopPathComponents(char *,const size_t),
42 ExpandFilename(char *);
44 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
45 struct dirent **result)
47 #if defined(MAGICKCORE_HAVE_READDIR_R)
48 return(readdir_r(directory,entry,result));
52 *result=readdir(directory);
58 Windows UTF8 compatibility methods.
61 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
62 static inline wchar_t *create_wchar_path(const char *utf8)
70 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
74 buffer[MagickPathExtent];
80 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
82 longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
83 if (longPath == (wchar_t *) NULL)
84 return((wchar_t *) NULL);
85 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
87 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
88 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
90 return((wchar_t *) NULL);
91 wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
92 wcscpy(wideChar,shortPath+4);
95 wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
96 if (wideChar == (wchar_t *) NULL)
97 return((wchar_t *) NULL);
98 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
101 wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
102 return((wchar_t *) NULL);
108 static inline int access_utf8(const char *path,int mode)
110 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
111 return(access(path,mode));
119 path_wide=create_wchar_path(path);
120 if (path_wide == (wchar_t *) NULL)
122 status=_waccess(path_wide,mode);
123 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
128 static inline FILE *fopen_utf8(const char *path,const char *mode)
130 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
131 return(fopen(path,mode));
140 path_wide=create_wchar_path(path);
141 if (path_wide == (wchar_t *) NULL)
142 return((FILE *) NULL);
143 mode_wide=create_wchar_path(mode);
144 if (mode_wide == (wchar_t *) NULL)
146 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
147 return((FILE *) NULL);
149 file=_wfopen(path_wide,mode_wide);
150 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
151 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
156 static inline void getcwd_utf8(char *path,size_t extent)
158 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
162 directory=getcwd(path,extent);
166 wide_path[MagickPathExtent];
168 (void) _wgetcwd(wide_path,MagickPathExtent-1);
169 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
173 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
178 static inline int open_utf8(const char *path,int flags,mode_t mode)
180 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
181 return(open(path,flags,mode));
189 path_wide=create_wchar_path(path);
190 if (path_wide == (wchar_t *) NULL)
192 status=_wopen(path_wide,flags,mode);
193 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
198 static inline FILE *popen_utf8(const char *command,const char *type)
200 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
201 return(popen(command,type));
210 command_wide=create_wchar_path(command);
211 if (command_wide == (wchar_t *) NULL)
212 return((FILE *) NULL);
213 type_wide=create_wchar_path(type);
214 if (type_wide == (wchar_t *) NULL)
216 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
217 return((FILE *) NULL);
219 file=_wpopen(command_wide,type_wide);
220 type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
221 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
226 static inline int remove_utf8(const char *path)
228 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
229 return(unlink(path));
237 path_wide=create_wchar_path(path);
238 if (path_wide == (wchar_t *) NULL)
240 status=_wremove(path_wide);
241 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
246 static inline int rename_utf8(const char *source,const char *destination)
248 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
249 return(rename(source,destination));
258 source_wide=create_wchar_path(source);
259 if (source_wide == (wchar_t *) NULL)
261 destination_wide=create_wchar_path(destination);
262 if (destination_wide == (wchar_t *) NULL)
264 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
267 status=_wrename(source_wide,destination_wide);
268 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
269 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
274 static inline int stat_utf8(const char *path,struct stat *attributes)
276 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
277 return(stat(path,attributes));
285 path_wide=create_wchar_path(path);
286 if (path_wide == (WCHAR *) NULL)
288 status=wstat(path_wide,attributes);
289 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
294 #if defined(__cplusplus) || defined(c_plusplus)