MagickCore  6.9.10
Convert, Edit, Or Compose Bitmap Images
utility-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License.
6  obtain a copy of the License at
7 
8  https://imagemagick.org/script/license.php
9 
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.
15 
16  MagickCore private utility methods.
17 */
18 #ifndef MAGICKCORE_UTILITY_PRIVATE_H
19 #define MAGICKCORE_UTILITY_PRIVATE_H
20 
21 #include "magick/memory_.h"
22 #include "magick/nt-base.h"
23 #include "magick/nt-base-private.h"
24 
25 #if defined(__cplusplus) || defined(c_plusplus)
26 extern "C" {
27 #endif
28 
30  ShredFile(const char *);
31 
32 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
33  struct dirent **result)
34 {
35 #if defined(MAGICKCORE_HAVE_READDIR_R)
36  return(readdir_r(directory,entry,result));
37 #else
38  (void) entry;
39  errno=0;
40  *result=readdir(directory);
41  return(errno);
42 #endif
43 }
44 
45 /*
46  Windows UTF8 compatibility methods.
47 */
48 
49 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
50 static inline wchar_t *create_wchar_path(const char *utf8)
51 {
52  int
53  count;
54 
55  wchar_t
56  *wideChar;
57 
58  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
59  if (count > MAX_PATH)
60  {
61  char
62  buffer[MaxTextExtent];
63 
64  wchar_t
65  shortPath[MAX_PATH],
66  *longPath;
67 
68  (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
69  count+=4;
70  longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
71  if (longPath == (wchar_t *) NULL)
72  return((wchar_t *) NULL);
73  count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
74  if (count != 0)
75  count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
76  longPath=(wchar_t *) RelinquishMagickMemory(longPath);
77  if (count < 5)
78  return((wchar_t *) NULL);
79  wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
80  wcscpy(wideChar,shortPath+4);
81  return(wideChar);
82  }
83  wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
84  if (wideChar == (wchar_t *) NULL)
85  return((wchar_t *) NULL);
86  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
87  if (count == 0)
88  {
89  wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
90  return((wchar_t *) NULL);
91  }
92  return(wideChar);
93 }
94 #endif
95 
96 static inline int access_utf8(const char *path,int mode)
97 {
98 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
99  return(access(path,mode));
100 #else
101  int
102  status;
103 
104  wchar_t
105  *path_wide;
106 
107  path_wide=create_wchar_path(path);
108  if (path_wide == (wchar_t *) NULL)
109  return(-1);
110  status=_waccess(path_wide,mode);
111  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
112  return(status);
113 #endif
114 }
115 
116 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
117 #define close_utf8 _close
118 #else
119 #define close_utf8 close
120 #endif
121 
122 static inline FILE *fopen_utf8(const char *path,const char *mode)
123 {
124 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
125  return(fopen(path,mode));
126 #else
127  FILE
128  *file;
129 
130  wchar_t
131  *mode_wide,
132  *path_wide;
133 
134  path_wide=create_wchar_path(path);
135  if (path_wide == (wchar_t *) NULL)
136  return((FILE *) NULL);
137  mode_wide=create_wchar_path(mode);
138  if (mode_wide == (wchar_t *) NULL)
139  {
140  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
141  return((FILE *) NULL);
142  }
143  file=_wfopen(path_wide,mode_wide);
144  mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
145  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
146  return(file);
147 #endif
148 }
149 
150 static inline void getcwd_utf8(char *path,size_t extent)
151 {
152 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
153  char
154  *directory;
155 
156  directory=getcwd(path,extent);
157  (void) directory;
158 #else
159  wchar_t
160  wide_path[MaxTextExtent];
161 
162  (void) _wgetcwd(wide_path,MaxTextExtent-1);
163  (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
164 #endif
165 }
166 
167 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
168 typedef int
169  mode_t;
170 #endif
171 
172 static inline int open_utf8(const char *path,int flags,mode_t mode)
173 {
174 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
175  return(open(path,flags,mode));
176 #else
177  int
178  status;
179 
180  wchar_t
181  *path_wide;
182 
183  path_wide=create_wchar_path(path);
184  if (path_wide == (wchar_t *) NULL)
185  return(-1);
186  status=_wopen(path_wide,flags,mode);
187  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
188  return(status);
189 #endif
190 }
191 
192 static inline FILE *popen_utf8(const char *command,const char *type)
193 {
194 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
195  return(popen(command,type));
196 #else
197  FILE
198  *file;
199 
200  wchar_t
201  *type_wide,
202  *command_wide;
203 
204  command_wide=create_wchar_path(command);
205  if (command_wide == (wchar_t *) NULL)
206  return((FILE *) NULL);
207  type_wide=create_wchar_path(type);
208  if (type_wide == (wchar_t *) NULL)
209  {
210  command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
211  return((FILE *) NULL);
212  }
213  file=_wpopen(command_wide,type_wide);
214  type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
215  command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
216  return(file);
217 #endif
218 }
219 
220 static inline char *realpath_utf8(const char *path)
221 {
222 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223 #if defined(MAGICKCORE_HAVE_REALPATH)
224  if (path == (const char *) NULL)
225  return((char *) NULL);
226  return(realpath(path,(char *) NULL));
227 #else
228  if (path == (const char *) NULL)
229  return((char *) NULL);
230  return(AcquireString(path));
231 #endif
232 #else
233  char
234  *real_path;
235 
236  DWORD
237  final_path_length,
238  full_path_length;
239 
240  HANDLE
241  file_handle;
242 
243  int
244  length,
245  utf8_length;
246 
247  wchar_t
248  *clean_path,
249  *full_path,
250  *wide_path;
251 
252  if (path == (const char *) NULL)
253  return((char *) NULL);
254  length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
255  if (length <= 0)
256  return((char *) NULL);
257  wide_path=(wchar_t *) AcquireQuantumMemory(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);
261  full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
262  if (full_path_length == 0)
263  {
264  wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
265  return((char *) NULL);
266  }
267  full_path=(wchar_t *) AcquireQuantumMemory(full_path_length,sizeof(wchar_t));
268  if (full_path == (wchar_t *) NULL)
269  {
270  wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
271  return((char *) NULL);
272  }
273  GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
274  wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
275  file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
276  FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
277  FILE_FLAG_BACKUP_SEMANTICS,NULL);
278  if (file_handle != INVALID_HANDLE_VALUE)
279  {
280  final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
281  FILE_NAME_NORMALIZED);
282  if (final_path_length == 0)
283  {
284  CloseHandle(file_handle);
285  full_path=(wchar_t *) RelinquishMagickMemory(full_path);
286  return((char *) NULL);
287  }
288  full_path=(wchar_t *) RelinquishMagickMemory(full_path);
289  full_path=(wchar_t *) AcquireQuantumMemory(final_path_length,
290  sizeof(wchar_t));
291  if (full_path == (wchar_t *) NULL)
292  {
293  CloseHandle(file_handle);
294  return((char *) NULL);
295  }
296  GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
297  FILE_NAME_NORMALIZED);
298  CloseHandle(file_handle);
299  }
300  clean_path=full_path;
301  if (wcsncmp(full_path,L"\\\\?\\",4) == 0)
302  clean_path=full_path+4;
303  utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
304  if (utf8_length <= 0)
305  {
306  full_path=(wchar_t *) RelinquishMagickMemory(full_path);
307  return((char *) NULL);
308  }
309  real_path=(char *) AcquireQuantumMemory(utf8_length,sizeof(char));
310  if (real_path == (char *) NULL)
311  {
312  full_path=(wchar_t *) RelinquishMagickMemory(full_path);
313  return((char *) NULL);
314  }
315  WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
316  full_path=(wchar_t *) RelinquishMagickMemory(full_path);
317  return(real_path);
318 #endif
319 }
320 
321 static inline int remove_utf8(const char *path)
322 {
323 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
324  return(unlink(path));
325 #else
326  int
327  status;
328 
329  wchar_t
330  *path_wide;
331 
332  path_wide=create_wchar_path(path);
333  if (path_wide == (wchar_t *) NULL)
334  return(-1);
335  status=_wremove(path_wide);
336  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
337  return(status);
338 #endif
339 }
340 
341 static inline int rename_utf8(const char *source,const char *destination)
342 {
343 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
344  return(rename(source,destination));
345 #else
346  int
347  status;
348 
349  wchar_t
350  *destination_wide,
351  *source_wide;
352 
353  source_wide=create_wchar_path(source);
354  if (source_wide == (wchar_t *) NULL)
355  return(-1);
356  destination_wide=create_wchar_path(destination);
357  if (destination_wide == (wchar_t *) NULL)
358  {
359  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
360  return(-1);
361  }
362  status=_wrename(source_wide,destination_wide);
363  destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
364  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
365  return(status);
366 #endif
367 }
368 
369 static inline int stat_utf8(const char *path,struct stat *attributes)
370 {
371 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
372  return(stat(path,attributes));
373 #else
374  int
375  status;
376 
377  wchar_t
378  *path_wide;
379 
380  path_wide=create_wchar_path(path);
381  if (path_wide == (WCHAR *) NULL)
382  return(-1);
383  status=wstat(path_wide,attributes);
384  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
385  return(status);
386 #endif
387 }
388 
389 #if defined(__cplusplus) || defined(c_plusplus)
390 }
391 #endif
392 
393 #endif
_DIR
Definition: mac.h:42
nt-base-private.h
FormatLocaleString
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:502
MagickReadDirectory
static int MagickReadDirectory(DIR *directory, struct dirent *entry, struct dirent **result)
Definition: utility-private.h:32
getcwd_utf8
static void getcwd_utf8(char *path, size_t extent)
Definition: utility-private.h:150
readdir
MagickExport struct dirent * readdir(DIR *)
_TypeInfo::path
char * path
Definition: type.h:56
nt-base.h
dirent
Definition: mac.h:54
remove_utf8
static int remove_utf8(const char *path)
Definition: utility-private.h:321
MagickPrivate
#define MagickPrivate
Definition: method-attribute.h:81
popen_utf8
static FILE * popen_utf8(const char *command, const char *type)
Definition: utility-private.h:192
fopen_utf8
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: utility-private.h:122
MagickBooleanType
MagickBooleanType
Definition: magick-type.h:192
stat_utf8
static int stat_utf8(const char *path, struct stat *attributes)
Definition: utility-private.h:369
RelinquishMagickMemory
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1077
ShredFile
MagickPrivate MagickBooleanType ShredFile(const char *)
Definition: utility.c:1825
access_utf8
static int access_utf8(const char *path, int mode)
Definition: utility-private.h:96
realpath_utf8
static char * realpath_utf8(const char *path)
Definition: utility-private.h:220
memory_.h
MaxTextExtent
#define MaxTextExtent
Definition: method-attribute.h:89
AcquireString
MagickExport char * AcquireString(const char *source)
Definition: string.c:125
open_utf8
static int open_utf8(const char *path, int flags, mode_t mode)
Definition: utility-private.h:172
rename_utf8
static int rename_utf8(const char *source, const char *destination)
Definition: utility-private.h:341
AcquireQuantumMemory
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:544