MagickCore 6.9.11-60
Convert, Edit, Or Compose Bitmap Images
utility-private.h
Go to the documentation of this file.
1/*
2 Copyright 1999-2021 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"
24
25#if defined(__cplusplus) || defined(c_plusplus)
26extern "C" {
27#endif
28
29#define MagickPathTemplate "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" /* min 6 X's */
30
32 ShredFile(const char *);
33
34static inline ssize_t MagickRead(int fd,void *buffer,size_t extent)
35{
36 unsigned char *p = (unsigned char *) buffer;
37 size_t offset = 0;
38 while (offset < extent)
39 {
40 ssize_t count = read(fd,p+offset,extent-offset);
41 if (count < 0)
42 {
43 if (errno == EINTR)
44 continue;
45 return(-1);
46 }
47 if (count == 0)
48 break;
49 offset+=(size_t) count;
50 }
51 return((ssize_t) offset);
52}
53
54static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
55 struct dirent **result)
56{
57#if defined(MAGICKCORE_HAVE_READDIR_R)
58 return(readdir_r(directory,entry,result));
59#else
60 (void) entry;
61 errno=0;
62 *result=readdir(directory);
63 return(errno);
64#endif
65}
66
67static inline ssize_t MagickWrite(int fd,const void *buffer,size_t extent)
68{
69 const unsigned char *p = (const unsigned char *) buffer;
70 size_t offset = 0;
71 while (offset < extent)
72 {
73 ssize_t count = write(fd,p+offset,extent-offset);
74 if (count < 0) {
75 if (errno == EINTR)
76 continue;
77 return(-1);
78 }
79 if (count == 0)
80 break;
81 offset+=(size_t) count;
82 }
83 return((ssize_t) offset);
84}
85
86/*
87 Windows UTF8 compatibility methods.
88*/
89
90#if defined(MAGICKCORE_WINDOWS_SUPPORT)
91static inline wchar_t *create_wchar_path(const char *utf8)
92{
93 int
94 count;
95
96 wchar_t
97 *wideChar;
98
99 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
100 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
101 {
102 char
103 buffer[MaxTextExtent];
104
105 wchar_t
106 shortPath[MAX_PATH],
107 *longPath;
108
109 (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
110 count+=4;
111 longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
112 if (longPath == (wchar_t *) NULL)
113 return((wchar_t *) NULL);
114 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
115 if (count != 0)
116 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
117 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
118 if ((count < 5) || (count >= MAX_PATH))
119 return((wchar_t *) NULL);
120 wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
121 wcscpy(wideChar,shortPath+4);
122 return(wideChar);
123 }
124 wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
125 if (wideChar == (wchar_t *) NULL)
126 return((wchar_t *) NULL);
127 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
128 if (count == 0)
129 {
130 wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
131 return((wchar_t *) NULL);
132 }
133 return(wideChar);
134}
135#endif
136
137static inline int access_utf8(const char *path,int mode)
138{
139#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
140 return(access(path,mode));
141#else
142 int
143 status;
144
145 wchar_t
146 *path_wide;
147
148 path_wide=create_wchar_path(path);
149 if (path_wide == (wchar_t *) NULL)
150 return(-1);
151 status=_waccess(path_wide,mode);
152 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
153 return(status);
154#endif
155}
156
157#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
158#define close_utf8 _close
159#else
160#define close_utf8 close
161#endif
162
163static inline FILE *fopen_utf8(const char *path,const char *mode)
164{
165#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
166 return(fopen(path,mode));
167#else
168 FILE
169 *file;
170
171 wchar_t
172 *mode_wide,
173 *path_wide;
174
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)
180 {
181 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
182 return((FILE *) NULL);
183 }
184 file=_wfopen(path_wide,mode_wide);
185 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
186 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
187 return(file);
188#endif
189}
190
191static inline void getcwd_utf8(char *path,size_t extent)
192{
193#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
194 char
195 *directory;
196
197 directory=getcwd(path,extent);
198 (void) directory;
199#else
200 wchar_t
201 wide_path[MaxTextExtent];
202
203 (void) _wgetcwd(wide_path,MaxTextExtent-1);
204 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
205#endif
206}
207
208#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
209typedef int
210 mode_t;
211#endif
212
213static inline int open_utf8(const char *path,int flags,mode_t mode)
214{
215#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
216 return(open(path,flags,mode));
217#else
218 int
219 status;
220
221 wchar_t
222 *path_wide;
223
224 path_wide=create_wchar_path(path);
225 if (path_wide == (wchar_t *) NULL)
226 return(-1);
227 status=_wopen(path_wide,flags,mode);
228 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
229 return(status);
230#endif
231}
232
233static inline FILE *popen_utf8(const char *command,const char *type)
234{
235#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
236 return(popen(command,type));
237#else
238 FILE
239 *file;
240
241 int
242 length;
243
244 wchar_t
245 *command_wide,
246 type_wide[5];
247
248 file=(FILE *) NULL;
249 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
250 if (length == 0)
251 return(file);
252 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
253 if (length == 0)
254 return(file);
255 command_wide=(wchar_t *) AcquireQuantumMemory(length,sizeof(*command_wide));
256 if (command_wide == (wchar_t *) NULL)
257 return(file);
258 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
259 if (length != 0)
260 file=_wpopen(command_wide,type_wide);
261 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
262 return(file);
263#endif
264}
265
266static inline char *realpath_utf8(const char *path)
267{
268#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
269#if defined(MAGICKCORE_HAVE_REALPATH)
270 return(realpath(path,(char *) NULL));
271#else
272 return(AcquireString(path));
273#endif
274#else
275 char
276 *real_path;
277
278 DWORD
279 final_path_length,
280 full_path_length;
281
282 HANDLE
283 file_handle;
284
285 int
286 length,
287 utf8_length,
288
289 wchar_t
290 *clean_path,
291 *final_path,
292 *full_path,
293 *wide_path;
294
295 /*
296 Convert UTF-8 to UTF-16.
297 */
298 if (path == (const char *) NULL)
299 return((char *) NULL);
300 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
301 if (length <= 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);
307 /*
308 Normalize syntactically.
309 */
310 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
311 if (full_path_length == 0)
312 {
313 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
314 return((char *) NULL);
315 }
316 full_path=(wchar_t *) AcquireQuantumMemory(full_path_length,sizeof(wchar_t));
317 if (full_path == (wchar_t *) NULL);
318 {
319 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
320 return((char *) NULL);
321 }
322 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
323 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
324 /*
325 Open the file/directory to resolve symlinks.
326 */
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)
331 {
332 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
333 return((char *) NULL);
334 }
335 /*
336 Resolve final canonical path.
337 */
338 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
339 FILE_NAME_NORMALIZED);
340 if (final_path_length == 0)
341 {
342 CloseHandle(file_handle);
343 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
344 return((char *) NULL);
345 }
346 final_path=(wchar_t *) AcquireQuantumMemory(final_path_length,
347 sizeof(wchar_t));
348 if (final_path == (wchar_t *) NULL)
349 {
350 CloseHandle(file_handle);
351 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
352 return((char *) NULL);
353 }
354 GetFinalPathNameByHandleW(file_handle,final_path,final_path_length,
355 FILE_NAME_NORMALIZED);
356 CloseHandle(file_handle);
357 full_path=(wchar_t *) RelinquishMagickMemory(full_path);
358 /*
359 Remove \\?\ prefix for POSIX-like behavior.
360 */
361 clean_path=final_path;
362 if (wcsncmp(final_path,L"\\\\?\\",4) == 0)
363 clean_path=final_path+4;
364 /*
365 Convert UTF-16 to UTF-8.
366 */
367 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
368 if (utf8_length <= 0)
369 {
370 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
371 return NULL;
372 }
373 real_path=(char *) AcquireQuantumMemory(utf8_length,sizeof(char));
374 if (real_path == (char *) NULL)
375 {
376 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
377 return NULL;
378 }
379 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
380 final_path=(wchar_t *) RelinquishMagickMemory(final_path);
381 return(real_path);
382#endif
383}
384
385static inline int remove_utf8(const char *path)
386{
387#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
388 return(unlink(path));
389#else
390 int
391 status;
392
393 wchar_t
394 *path_wide;
395
396 path_wide=create_wchar_path(path);
397 if (path_wide == (wchar_t *) NULL)
398 return(-1);
399 status=_wremove(path_wide);
400 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
401 return(status);
402#endif
403}
404
405static inline int rename_utf8(const char *source,const char *destination)
406{
407#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
408 return(rename(source,destination));
409#else
410 int
411 status;
412
413 wchar_t
414 *destination_wide,
415 *source_wide;
416
417 source_wide=create_wchar_path(source);
418 if (source_wide == (wchar_t *) NULL)
419 return(-1);
420 destination_wide=create_wchar_path(destination);
421 if (destination_wide == (wchar_t *) NULL)
422 {
423 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
424 return(-1);
425 }
426 status=_wrename(source_wide,destination_wide);
427 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
428 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
429 return(status);
430#endif
431}
432
433static inline int stat_utf8(const char *path,struct stat *attributes)
434{
435#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
436 return(stat(path,attributes));
437#else
438 int
439 status;
440
441 wchar_t
442 *path_wide;
443
444 path_wide=create_wchar_path(path);
445 if (path_wide == (WCHAR *) NULL)
446 return(-1);
447 status=wstat(path_wide,attributes);
448 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
449 return(status);
450#endif
451}
452
453static inline MagickBooleanType is_symlink_utf8(const char *path)
454{
455#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
456#if defined(MAGICKCORE_POSIX_SUPPORT)
457 struct stat
458 status;
459
460 if (lstat(path,&status) == -1)
461 return(MagickFalse);
462 return(S_ISLNK(status.st_mode) != 0 ? MagickTrue : MagickFalse);
463#else
464 return(MagickFalse);
465#endif
466#else
467 return(NTIsSymlinkWide(path));
468#endif
469}
470
471
472#if defined(__cplusplus) || defined(c_plusplus)
473}
474#endif
475
476#endif
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
Definition: mac.h:42
Definition: mac.h:54
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