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