MagickCore 7.1.2
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 "MagickCore/memory_.h"
22#include "MagickCore/nt-base.h"
23#include "MagickCore/nt-base-private.h"
24#if defined(MAGICKCORE_HAVE_UTIME_H)
25#include <utime.h>
26#endif
27#if defined(__MINGW32__)
28#include <share.h>
29#endif
30
31#if defined(__cplusplus) || defined(c_plusplus)
32extern "C" {
33#endif
34
35extern MagickPrivate char
36 **GetPathComponents(const char *,size_t *),
37 **ListFiles(const char *,const char *,size_t *);
38
39extern MagickPrivate MagickBooleanType
40 GetExecutionPath(char *,const size_t),
41 ShredFile(const char *);
42
43extern MagickPrivate ssize_t
44 GetMagickPageSize(void);
45
46extern MagickPrivate void
47 ChopPathComponents(char *,const size_t),
48 ExpandFilename(char *);
49
50static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
51 struct dirent **result)
52{
53 (void) entry;
54 errno=0;
55 *result=readdir(directory);
56 return(errno);
57}
58
59/*
60 Windows UTF8 compatibility methods.
61*/
62
63#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
64static inline wchar_t *create_wchar_path(const char *utf8)
65{
66 int
67 count;
68
69 wchar_t
70 *wide;
71
72 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
73 if ((count > MAX_PATH) && (strncmp(utf8,"\\\\?\\",4) != 0) &&
74 (NTLongPathsEnabled() == MagickFalse))
75 {
76 char
77 buffer[MagickPathExtent];
78
79 wchar_t
80 shortPath[MAX_PATH],
81 *longPath;
82
83 size_t
84 length;
85
86 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
87 count+=4;
88 longPath=(wchar_t *) NTAcquireQuantumMemory((size_t) count,
89 sizeof(*longPath));
90 if (longPath == (wchar_t *) NULL)
91 return((wchar_t *) NULL);
92 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
93 if (count != 0)
94 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
95 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
96 if ((count < 5) || (count >= MAX_PATH))
97 return((wchar_t *) NULL);
98 length=(size_t) count-3;
99 wide=(wchar_t *) NTAcquireQuantumMemory(length,sizeof(*wide));
100 wcscpy_s(wide,length,shortPath+4);
101 return(wide);
102 }
103 wide=(wchar_t *) NTAcquireQuantumMemory((size_t) count,sizeof(*wide));
104 if ((wide != (wchar_t *) NULL) &&
105 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
106 wide=(wchar_t *) RelinquishMagickMemory(wide);
107 return(wide);
108}
109
110static inline wchar_t *create_wchar_mode(const char *mode)
111{
112 int
113 count;
114
115 wchar_t
116 *wide;
117
118 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
119 wide=(wchar_t *) AcquireQuantumMemory((size_t) count+1,
120 sizeof(*wide));
121 if (wide == (wchar_t *) NULL)
122 return((wchar_t *) NULL);
123 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
124 {
125 wide=(wchar_t *) RelinquishMagickMemory(wide);
126 return((wchar_t *) NULL);
127 }
128 /* Specifies that the file is not inherited by child processes */
129 wide[count] = L'\0';
130 wide[count-1] = L'N';
131 return(wide);
132}
133#endif
134
135static inline int access_utf8(const char *path,int mode)
136{
137 if (path == (const char *) NULL)
138 return(-1);
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_mode(mode);
179 if (mode_wide == (wchar_t *) NULL)
180 {
181 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
182 return((FILE *) NULL);
183 }
184 if (_wfopen_s(&file,path_wide,mode_wide) != 0)
185 file=(FILE *) NULL;
186 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
187 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
188 return(file);
189#endif
190}
191
192static inline void getcwd_utf8(char *path,size_t extent)
193{
194#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
195 char
196 *directory;
197
198 directory=getcwd(path,extent);
199 (void) directory;
200#else
201 wchar_t
202 wide_path[MagickPathExtent];
203
204 (void) _wgetcwd(wide_path,MagickPathExtent-1);
205 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
206#endif
207}
208
209#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
210typedef int
211 mode_t;
212#endif
213
214static inline int open_utf8(const char *path,int flags,mode_t mode)
215{
216#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
217 return(open(path,flags,mode));
218#else
219 int
220 file_handle,
221 status;
222
223 wchar_t
224 *path_wide;
225
226 path_wide=create_wchar_path(path);
227 if (path_wide == (wchar_t *) NULL)
228 return(-1);
229 /* O_NOINHERIT specifies that the file is not inherited by child processes */
230 status=_wsopen_s(&file_handle,path_wide,flags | O_NOINHERIT,_SH_DENYNO,mode);
231 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
232 return(status == 0 ? file_handle : -1);
233#endif
234}
235
236static inline FILE *popen_utf8(const char *command,const char *type)
237{
238#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
239 return(popen(command,type));
240#else
241 FILE
242 *file;
243
244 int
245 length;
246
247 wchar_t
248 *command_wide,
249 type_wide[5];
250
251 file=(FILE *) NULL;
252 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
253 if (length == 0)
254 return(file);
255 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
256 if (length == 0)
257 return(file);
258 command_wide=(wchar_t *) AcquireQuantumMemory((size_t) length,
259 sizeof(*command_wide));
260 if (command_wide == (wchar_t *) NULL)
261 return(file);
262 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
263 if (length != 0)
264 file=_wpopen(command_wide,type_wide);
265 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
266 return(file);
267#endif
268}
269
270static inline int remove_utf8(const char *path)
271{
272#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
273 return(unlink(path));
274#else
275 int
276 status;
277
278 wchar_t
279 *path_wide;
280
281 path_wide=create_wchar_path(path);
282 if (path_wide == (wchar_t *) NULL)
283 return(-1);
284 status=_wremove(path_wide);
285 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
286 return(status);
287#endif
288}
289
290static inline int rename_utf8(const char *source,const char *destination)
291{
292#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
293 return(rename(source,destination));
294#else
295 int
296 status;
297
298 wchar_t
299 *destination_wide,
300 *source_wide;
301
302 source_wide=create_wchar_path(source);
303 if (source_wide == (wchar_t *) NULL)
304 return(-1);
305 destination_wide=create_wchar_path(destination);
306 if (destination_wide == (wchar_t *) NULL)
307 {
308 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
309 return(-1);
310 }
311 status=_wrename(source_wide,destination_wide);
312 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
313 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
314 return(status);
315#endif
316}
317
318static inline int set_file_timestamp(const char *path,struct stat *attributes)
319{
320 int
321 status;
322
323#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
324#if defined(MAGICKCORE_HAVE_UTIMENSAT)
325#if defined(__APPLE__) || defined(__NetBSD__)
326#define st_atim st_atimespec
327#define st_ctim st_ctimespec
328#define st_mtim st_mtimespec
329#endif
330
331 struct timespec
332 timestamp[2];
333
334 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
335 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
336 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
337 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
338 status=utimensat(AT_FDCWD,path,timestamp,0);
339#else
340 struct utimbuf
341 timestamp;
342
343 timestamp.actime=attributes->st_atime;
344 timestamp.modtime=attributes->st_mtime;
345 status=utime(path,&timestamp);
346#endif
347#else
348 HANDLE
349 handle;
350
351 wchar_t
352 *path_wide;
353
354 status=(-1);
355 path_wide=create_wchar_path(path);
356 if (path_wide == (WCHAR *) NULL)
357 return(status);
358 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
359 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
360 if (handle != (HANDLE) NULL)
361 {
362 FILETIME
363 creation_time,
364 last_access_time,
365 last_write_time;
366
367 ULARGE_INTEGER
368 date_time;
369
370 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
371 116444736000000000LL;
372 creation_time.dwLowDateTime=date_time.LowPart;
373 creation_time.dwHighDateTime=date_time.HighPart;
374 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
375 116444736000000000LL;
376 last_access_time.dwLowDateTime=date_time.LowPart;
377 last_access_time.dwHighDateTime=date_time.HighPart;
378 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
379 116444736000000000LL;
380 last_write_time.dwLowDateTime=date_time.LowPart;
381 last_write_time.dwHighDateTime=date_time.HighPart;
382 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
383 CloseHandle(handle);
384 status=0;
385 }
386 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
387#endif
388 return(status);
389}
390
391static inline int stat_utf8(const char *path,struct stat *attributes)
392{
393#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
394 return(stat(path,attributes));
395#else
396 int
397 status;
398
399 wchar_t
400 *path_wide;
401
402 path_wide=create_wchar_path(path);
403 if (path_wide == (WCHAR *) NULL)
404 return(-1);
405 status=wstat(path_wide,attributes);
406 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
407 return(status);
408#endif
409}
410
411#if defined(__cplusplus) || defined(c_plusplus)
412}
413#endif
414
415#endif
Definition vms.h:942