MagickCore 7.1.1
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)
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 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
81 count+=4;
82 longPath=(wchar_t *) NTAcquireQuantumMemory(count,sizeof(*longPath));
83 if (longPath == (wchar_t *) NULL)
84 return((wchar_t *) NULL);
85 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
86 if (count != 0)
87 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
88 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
89 if ((count < 5) || (count >= MAX_PATH))
90 return((wchar_t *) NULL);
91 wide=(wchar_t *) NTAcquireQuantumMemory((size_t) count-3,sizeof(*wide));
92 wcscpy(wide,shortPath+4);
93 return(wide);
94 }
95 wide=(wchar_t *) NTAcquireQuantumMemory(count,sizeof(*wide));
96 if ((wide != (wchar_t *) NULL) &&
97 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
98 wide=(wchar_t *) RelinquishMagickMemory(wide);
99 return(wide);
100}
101
102static inline wchar_t *create_wchar_mode(const char *mode)
103{
104 int
105 count;
106
107 wchar_t
108 *wide;
109
110 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
111 wide=(wchar_t *) AcquireQuantumMemory((size_t) count+1,
112 sizeof(*wide));
113 if (wide == (wchar_t *) NULL)
114 return((wchar_t *) NULL);
115 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
116 {
117 wide=(wchar_t *) RelinquishMagickMemory(wide);
118 return((wchar_t *) NULL);
119 }
120 /* Specifies that the file is not inherited by child processes */
121 wide[count] = L'\0';
122 wide[count-1] = L'N';
123 return(wide);
124}
125#endif
126
127static inline int access_utf8(const char *path,int mode)
128{
129 if (path == (const char *) NULL)
130 return(-1);
131#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
132 return(access(path,mode));
133#else
134 int
135 status;
136
137 wchar_t
138 *path_wide;
139
140 path_wide=create_wchar_path(path);
141 if (path_wide == (wchar_t *) NULL)
142 return(-1);
143 status=_waccess(path_wide,mode);
144 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
145 return(status);
146#endif
147}
148
149static inline FILE *fopen_utf8(const char *path,const char *mode)
150{
151#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
152 return(fopen(path,mode));
153#else
154 FILE
155 *file;
156
157 wchar_t
158 *mode_wide,
159 *path_wide;
160
161 path_wide=create_wchar_path(path);
162 if (path_wide == (wchar_t *) NULL)
163 return((FILE *) NULL);
164 mode_wide=create_wchar_mode(mode);
165 if (mode_wide == (wchar_t *) NULL)
166 {
167 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
168 return((FILE *) NULL);
169 }
170 file=_wfopen(path_wide,mode_wide);
171 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
172 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
173 return(file);
174#endif
175}
176
177static inline void getcwd_utf8(char *path,size_t extent)
178{
179#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
180 char
181 *directory;
182
183 directory=getcwd(path,extent);
184 (void) directory;
185#else
186 wchar_t
187 wide_path[MagickPathExtent];
188
189 (void) _wgetcwd(wide_path,MagickPathExtent-1);
190 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
191#endif
192}
193
194#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
195typedef int
196 mode_t;
197#endif
198
199static inline int open_utf8(const char *path,int flags,mode_t mode)
200{
201#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
202 return(open(path,flags,mode));
203#else
204 int
205 status;
206
207 wchar_t
208 *path_wide;
209
210 path_wide=create_wchar_path(path);
211 if (path_wide == (wchar_t *) NULL)
212 return(-1);
213 /* O_NOINHERIT specifies that the file is not inherited by child processes */
214 status=_wopen(path_wide,flags | O_NOINHERIT,mode);
215 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
216 return(status);
217#endif
218}
219
220static inline FILE *popen_utf8(const char *command,const char *type)
221{
222#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223 return(popen(command,type));
224#else
225 FILE
226 *file;
227
228 int
229 length;
230
231 wchar_t
232 *command_wide,
233 type_wide[5];
234
235 file=(FILE *) NULL;
236 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
237 if (length == 0)
238 return(file);
239 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
240 if (length == 0)
241 return(file);
242 command_wide=(wchar_t *) AcquireQuantumMemory(length,sizeof(*command_wide));
243 if (command_wide == (wchar_t *) NULL)
244 return(file);
245 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
246 if (length != 0)
247 file=_wpopen(command_wide,type_wide);
248 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
249 return(file);
250#endif
251}
252
253static inline int remove_utf8(const char *path)
254{
255#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
256 return(unlink(path));
257#else
258 int
259 status;
260
261 wchar_t
262 *path_wide;
263
264 path_wide=create_wchar_path(path);
265 if (path_wide == (wchar_t *) NULL)
266 return(-1);
267 status=_wremove(path_wide);
268 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
269 return(status);
270#endif
271}
272
273static inline int rename_utf8(const char *source,const char *destination)
274{
275#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
276 return(rename(source,destination));
277#else
278 int
279 status;
280
281 wchar_t
282 *destination_wide,
283 *source_wide;
284
285 source_wide=create_wchar_path(source);
286 if (source_wide == (wchar_t *) NULL)
287 return(-1);
288 destination_wide=create_wchar_path(destination);
289 if (destination_wide == (wchar_t *) NULL)
290 {
291 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
292 return(-1);
293 }
294 status=_wrename(source_wide,destination_wide);
295 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
296 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
297 return(status);
298#endif
299}
300
301static inline int set_file_timestamp(const char *path,struct stat *attributes)
302{
303 int
304 status;
305
306#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
307#if defined(MAGICKCORE_HAVE_UTIMENSAT)
308#if defined(__APPLE__) || defined(__NetBSD__)
309#define st_atim st_atimespec
310#define st_ctim st_ctimespec
311#define st_mtim st_mtimespec
312#endif
313
314 struct timespec
315 timestamp[2];
316
317 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
318 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
319 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
320 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
321 status=utimensat(AT_FDCWD,path,timestamp,0);
322#else
323 struct utimbuf
324 timestamp;
325
326 timestamp.actime=attributes->st_atime;
327 timestamp.modtime=attributes->st_mtime;
328 status=utime(path,&timestamp);
329#endif
330#else
331 HANDLE
332 handle;
333
334 wchar_t
335 *path_wide;
336
337 status=(-1);
338 path_wide=create_wchar_path(path);
339 if (path_wide == (WCHAR *) NULL)
340 return(status);
341 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
342 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
343 if (handle != (HANDLE) NULL)
344 {
345 FILETIME
346 creation_time,
347 last_access_time,
348 last_write_time;
349
350 ULARGE_INTEGER
351 date_time;
352
353 date_time.QuadPart=(attributes->st_ctime*10000000LL)+116444736000000000LL;
354 creation_time.dwLowDateTime=date_time.LowPart;
355 creation_time.dwHighDateTime=date_time.HighPart;
356 date_time.QuadPart=(attributes->st_atime*10000000LL)+116444736000000000LL;
357 last_access_time.dwLowDateTime=date_time.LowPart;
358 last_access_time.dwHighDateTime=date_time.HighPart;
359 date_time.QuadPart=(attributes->st_mtime*10000000LL)+116444736000000000LL;
360 last_write_time.dwLowDateTime=date_time.LowPart;
361 last_write_time.dwHighDateTime=date_time.HighPart;
362 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
363 CloseHandle(handle);
364 status=0;
365 }
366 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
367#endif
368 return(status);
369}
370
371static inline int stat_utf8(const char *path,struct stat *attributes)
372{
373#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
374 return(stat(path,attributes));
375#else
376 int
377 status;
378
379 wchar_t
380 *path_wide;
381
382 path_wide=create_wchar_path(path);
383 if (path_wide == (WCHAR *) NULL)
384 return(-1);
385 status=wstat(path_wide,attributes);
386 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
387 return(status);
388#endif
389}
390
391#if defined(__cplusplus) || defined(c_plusplus)
392}
393#endif
394
395#endif
Definition: vms.h:951
Definition: vms.h:942