MagickCore 6.9.13
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#if defined(MAGICKCORE_HAVE_UTIME_H)
25#include <utime.h>
26#endif
27
28#if defined(__cplusplus) || defined(c_plusplus)
29extern "C" {
30#endif
31
32extern MagickPrivate MagickBooleanType
33 ShredFile(const char *);
34
35static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
36 struct dirent **result)
37{
38 (void) entry;
39 errno=0;
40 *result=readdir(directory);
41 return(errno);
42}
43
44/*
45 Windows UTF8 compatibility methods.
46*/
47
48#if defined(MAGICKCORE_WINDOWS_SUPPORT)
49static inline wchar_t *create_wchar_path(const char *utf8)
50{
51 int
52 count;
53
54 wchar_t
55 *wideChar;
56
57 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
58 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
59 {
60 char
61 buffer[MaxTextExtent];
62
63 wchar_t
64 shortPath[MAX_PATH],
65 *longPath;
66
67 (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
68 count+=4;
69 longPath=(wchar_t *) NTAcquireQuantumMemory((size_t) count,
70 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=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
76 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
77 if ((count < 5) || (count >= MAX_PATH))
78 return((wchar_t *) NULL);
79 wideChar=(wchar_t *) NTAcquireQuantumMemory((size_t) count-3,
80 sizeof(*wideChar));
81 wcscpy(wideChar,shortPath+4);
82 return(wideChar);
83 }
84 wideChar=(wchar_t *) NTAcquireQuantumMemory(count,sizeof(*wideChar));
85 if (wideChar == (wchar_t *) NULL)
86 return((wchar_t *) NULL);
87 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
88 if (count == 0)
89 {
90 wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
91 return((wchar_t *) NULL);
92 }
93 return(wideChar);
94}
95#endif
96
97static inline int access_utf8(const char *path,int mode)
98{
99 if (path == (const char *) NULL)
100 return(-1);
101#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
102 return(access(path,mode));
103#else
104 int
105 status;
106
107 wchar_t
108 *path_wide;
109
110 path_wide=create_wchar_path(path);
111 if (path_wide == (wchar_t *) NULL)
112 return(-1);
113 status=_waccess(path_wide,mode);
114 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
115 return(status);
116#endif
117}
118
119static inline FILE *fopen_utf8(const char *path,const char *mode)
120{
121#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
122 return(fopen(path,mode));
123#else
124 FILE
125 *file;
126
127 wchar_t
128 *mode_wide,
129 *path_wide;
130
131 path_wide=create_wchar_path(path);
132 if (path_wide == (wchar_t *) NULL)
133 return((FILE *) NULL);
134 mode_wide=create_wchar_path(mode);
135 if (mode_wide == (wchar_t *) NULL)
136 {
137 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
138 return((FILE *) NULL);
139 }
140 file=_wfopen(path_wide,mode_wide);
141 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
142 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
143 return(file);
144#endif
145}
146
147static inline void getcwd_utf8(char *path,size_t extent)
148{
149#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
150 char
151 *directory;
152
153 directory=getcwd(path,extent);
154 (void) directory;
155#else
156 wchar_t
157 wide_path[MaxTextExtent];
158
159 (void) _wgetcwd(wide_path,MaxTextExtent-1);
160 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
161#endif
162}
163
164#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
165typedef int
166 mode_t;
167#endif
168
169static inline int open_utf8(const char *path,int flags,mode_t mode)
170{
171#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
172 return(open(path,flags,mode));
173#else
174 int
175 status;
176
177 wchar_t
178 *path_wide;
179
180 path_wide=create_wchar_path(path);
181 if (path_wide == (wchar_t *) NULL)
182 return(-1);
183 status=_wopen(path_wide,flags,mode);
184 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
185 return(status);
186#endif
187}
188
189static inline FILE *popen_utf8(const char *command,const char *type)
190{
191#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
192 return(popen(command,type));
193#else
194 FILE
195 *file;
196
197 int
198 length;
199
200 wchar_t
201 *command_wide,
202 type_wide[5];
203
204 file=(FILE *) NULL;
205 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
206 if (length == 0)
207 return(file);
208 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
209 if (length == 0)
210 return(file);
211 command_wide=(wchar_t *) AcquireQuantumMemory((size_t) length,
212 sizeof(*command_wide));
213 if (command_wide == (wchar_t *) NULL)
214 return(file);
215 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
216 if (length != 0)
217 file=_wpopen(command_wide,type_wide);
218 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
219 return(file);
220#endif
221}
222
223static inline int remove_utf8(const char *path)
224{
225#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
226 return(unlink(path));
227#else
228 int
229 status;
230
231 wchar_t
232 *path_wide;
233
234 path_wide=create_wchar_path(path);
235 if (path_wide == (wchar_t *) NULL)
236 return(-1);
237 status=_wremove(path_wide);
238 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
239 return(status);
240#endif
241}
242
243static inline int rename_utf8(const char *source,const char *destination)
244{
245#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
246 return(rename(source,destination));
247#else
248 int
249 status;
250
251 wchar_t
252 *destination_wide,
253 *source_wide;
254
255 source_wide=create_wchar_path(source);
256 if (source_wide == (wchar_t *) NULL)
257 return(-1);
258 destination_wide=create_wchar_path(destination);
259 if (destination_wide == (wchar_t *) NULL)
260 {
261 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
262 return(-1);
263 }
264 status=_wrename(source_wide,destination_wide);
265 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
266 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
267 return(status);
268#endif
269}
270
271static inline int set_file_timestamp(const char *path,struct stat *attributes)
272{
273 int
274 status;
275
276#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
277#if defined(MAGICKCORE_HAVE_UTIMENSAT)
278#if defined(__APPLE__) || defined(__NetBSD__)
279#define st_atim st_atimespec
280#define st_ctim st_ctimespec
281#define st_mtim st_mtimespec
282#endif
283
284 struct timespec
285 timestamp[2];
286
287 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
288 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
289 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
290 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
291 status=utimensat(AT_FDCWD,path,timestamp,0);
292#else
293 struct utimbuf
294 timestamp;
295
296 timestamp.actime=attributes->st_atime;
297 timestamp.modtime=attributes->st_mtime;
298 status=utime(path,&timestamp);
299#endif
300#else
301 HANDLE
302 handle;
303
304 wchar_t
305 *path_wide;
306
307 status=(-1);
308 path_wide=create_wchar_path(path);
309 if (path_wide == (WCHAR *) NULL)
310 return(status);
311 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
312 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
313 if (handle != (HANDLE) NULL)
314 {
315 FILETIME
316 creation_time,
317 last_access_time,
318 last_write_time;
319
320 ULARGE_INTEGER
321 date_time;
322
323 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
324 116444736000000000LL;
325 creation_time.dwLowDateTime=date_time.LowPart;
326 creation_time.dwHighDateTime=date_time.HighPart;
327 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
328 116444736000000000LL;
329 last_access_time.dwLowDateTime=date_time.LowPart;
330 last_access_time.dwHighDateTime=date_time.HighPart;
331 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
332 116444736000000000LL;
333 last_write_time.dwLowDateTime=date_time.LowPart;
334 last_write_time.dwHighDateTime=date_time.HighPart;
335 status=SetFileTime(handle,&creation_time,&last_access_time,
336 &last_write_time);
337 CloseHandle(handle);
338 status=0;
339 }
340 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
341#endif
342 return(status);
343}
344
345static inline int stat_utf8(const char *path,struct stat *attributes)
346{
347#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
348 return(stat(path,attributes));
349#else
350 int
351 status;
352
353 wchar_t
354 *path_wide;
355
356 path_wide=create_wchar_path(path);
357 if (path_wide == (WCHAR *) NULL)
358 return(-1);
359 status=_wstati64(path_wide,attributes);
360 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
361 return(status);
362#endif
363}
364
365#if defined(__cplusplus) || defined(c_plusplus)
366}
367#endif
368
369#endif
Definition mac.h:42
Definition mac.h:54