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