MagickCore 6.9.12-98
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
utility-private.h
1/*
2 Copyright 1999 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. You may
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)
26extern "C" {
27#endif
28
29extern MagickPrivate MagickBooleanType
30 ShredFile(const char *);
31
32static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
33 struct dirent **result)
34{
35 (void) entry;
36 errno=0;
37 *result=readdir(directory);
38 return(errno);
39}
40
41/*
42 Windows UTF8 compatibility methods.
43*/
44
45#if defined(MAGICKCORE_WINDOWS_SUPPORT)
46static inline wchar_t *create_wchar_path(const char *utf8)
47{
48 int
49 count;
50
51 wchar_t
52 *wideChar;
53
54 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
55 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
56 {
57 char
58 buffer[MaxTextExtent];
59
60 wchar_t
61 shortPath[MAX_PATH],
62 *longPath;
63
64 (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
65 count+=4;
66 longPath=(wchar_t *) NTAcquireQuantumMemory((size_t) count,
67 sizeof(*longPath));
68 if (longPath == (wchar_t *) NULL)
69 return((wchar_t *) NULL);
70 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
71 if (count != 0)
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,
77 sizeof(*wideChar));
78 wcscpy(wideChar,shortPath+4);
79 return(wideChar);
80 }
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);
85 if (count == 0)
86 {
87 wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
88 return((wchar_t *) NULL);
89 }
90 return(wideChar);
91}
92#endif
93
94static inline int access_utf8(const char *path,int mode)
95{
96 if (path == (const char *) NULL)
97 return(-1);
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
116static inline FILE *fopen_utf8(const char *path,const char *mode)
117{
118#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
119 return(fopen(path,mode));
120#else
121 FILE
122 *file;
123
124 wchar_t
125 *mode_wide,
126 *path_wide;
127
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)
133 {
134 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
135 return((FILE *) NULL);
136 }
137 file=_wfopen(path_wide,mode_wide);
138 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
139 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
140 return(file);
141#endif
142}
143
144static inline void getcwd_utf8(char *path,size_t extent)
145{
146#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
147 char
148 *directory;
149
150 directory=getcwd(path,extent);
151 (void) directory;
152#else
153 wchar_t
154 wide_path[MaxTextExtent];
155
156 (void) _wgetcwd(wide_path,MaxTextExtent-1);
157 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
158#endif
159}
160
161#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
162typedef int
163 mode_t;
164#endif
165
166static inline int open_utf8(const char *path,int flags,mode_t mode)
167{
168#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
169 return(open(path,flags,mode));
170#else
171 int
172 status;
173
174 wchar_t
175 *path_wide;
176
177 path_wide=create_wchar_path(path);
178 if (path_wide == (wchar_t *) NULL)
179 return(-1);
180 status=_wopen(path_wide,flags,mode);
181 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
182 return(status);
183#endif
184}
185
186static inline FILE *popen_utf8(const char *command,const char *type)
187{
188#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
189 return(popen(command,type));
190#else
191 FILE
192 *file;
193
194 int
195 length;
196
197 wchar_t
198 *command_wide,
199 type_wide[5];
200
201 file=(FILE *) NULL;
202 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
203 if (length == 0)
204 return(file);
205 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
206 if (length == 0)
207 return(file);
208 command_wide=(wchar_t *) AcquireQuantumMemory((size_t) length,
209 sizeof(*command_wide));
210 if (command_wide == (wchar_t *) NULL)
211 return(file);
212 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
213 if (length != 0)
214 file=_wpopen(command_wide,type_wide);
215 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
216 return(file);
217#endif
218}
219
220static inline char *realpath_utf8(const char *path)
221{
222#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223#if defined(MAGICKCORE_HAVE_REALPATH)
224 return(realpath(path,(char *) NULL));
225#else
226 return(AcquireString(path));
227#endif
228#else
229 char
230 *real_path;
231
232 DWORD
233 final_path_length,
234 full_path_length;
235
236 HANDLE
237 file_handle;
238
239 int
240 length,
241 utf8_length,
242
243 wchar_t
244 *clean_path,
245 *final_path,
246 *full_path,
247 *wide_path;
248
249 /*
250 Convert UTF-8 to UTF-16.
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 *) 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);
261 /*
262 Normalize syntactically.
263 */
264 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
265 if (full_path_length == 0)
266 {
267 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
268 return((char *) NULL);
269 }
270 full_path=(wchar_t *) AcquireQuantumMemory(full_path_length,sizeof(wchar_t));
271 if (full_path == (wchar_t *) NULL);
272 {
273 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
274 return((char *) NULL);
275 }
276 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
277 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
278 /*
279 Open the file/directory to resolve symlinks.
280 */
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)
285 {
286 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
287 return((char *) NULL);
288 }
289 /*
290 Resolve final canonical path.
291 */
292 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
293 FILE_NAME_NORMALIZED);
294 if (final_path_length == 0)
295 {
296 CloseHandle(file_handle);
297 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
298 return((char *) NULL);
299 }
300 final_path=(wchar_t *) AcquireQuantumMemory(final_path_length,
301 sizeof(wchar_t));
302 if (final_path == (wchar_t *) NULL)
303 {
304 CloseHandle(file_handle);
305 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
306 return((char *) NULL);
307 }
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);
312 /*
313 Remove \\?\ prefix for POSIX-like behavior.
314 */
315 clean_path=final_path;
316 if (wcsncmp(final_path,L"\\\\?\\",4) == 0)
317 clean_path=final_path+4;
318 /*
319 Convert UTF-16 to UTF-8.
320 */
321 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
322 if (utf8_length <= 0)
323 {
324 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
325 return NULL;
326 }
327 real_path=(char *) AcquireQuantumMemory(utf8_length,sizeof(char));
328 if (real_path == (char *) NULL)
329 {
330 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
331 return NULL;
332 }
333 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
334 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
335 return(real_path);
336#endif
337}
338
339static inline int remove_utf8(const char *path)
340{
341#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
342 return(unlink(path));
343#else
344 int
345 status;
346
347 wchar_t
348 *path_wide;
349
350 path_wide=create_wchar_path(path);
351 if (path_wide == (wchar_t *) NULL)
352 return(-1);
353 status=_wremove(path_wide);
354 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
355 return(status);
356#endif
357}
358
359static inline int rename_utf8(const char *source,const char *destination)
360{
361#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
362 return(rename(source,destination));
363#else
364 int
365 status;
366
367 wchar_t
368 *destination_wide,
369 *source_wide;
370
371 source_wide=create_wchar_path(source);
372 if (source_wide == (wchar_t *) NULL)
373 return(-1);
374 destination_wide=create_wchar_path(destination);
375 if (destination_wide == (wchar_t *) NULL)
376 {
377 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
378 return(-1);
379 }
380 status=_wrename(source_wide,destination_wide);
381 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
382 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
383 return(status);
384#endif
385}
386
387static inline int stat_utf8(const char *path,struct stat *attributes)
388{
389#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
390 return(stat(path,attributes));
391#else
392 int
393 status;
394
395 wchar_t
396 *path_wide;
397
398 path_wide=create_wchar_path(path);
399 if (path_wide == (WCHAR *) NULL)
400 return(-1);
401 status=wstat(path_wide,attributes);
402 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
403 return(status);
404#endif
405}
406
407#if defined(__cplusplus) || defined(c_plusplus)
408}
409#endif
410
411#endif
Definition mac.h:42
Definition mac.h:54