MagickCore  7.1.0
utility-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2021 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"
24 
25 #if defined(__cplusplus) || defined(c_plusplus)
26 extern "C" {
27 #endif
28 
29 extern MagickPrivate char
30  **GetPathComponents(const char *,size_t *),
31  **ListFiles(const char *,const char *,size_t *);
32 
34  GetExecutionPath(char *,const size_t),
35  ShredFile(const char *);
36 
37 extern MagickPrivate ssize_t
38  GetMagickPageSize(void);
39 
40 extern MagickPrivate void
41  ChopPathComponents(char *,const size_t),
42  ExpandFilename(char *);
43 
44 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
45  struct dirent **result)
46 {
47  (void) entry;
48  errno=0;
49  *result=readdir(directory);
50  return(errno);
51 }
52 
53 /*
54  Windows UTF8 compatibility methods.
55 */
56 
57 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
58 static inline wchar_t *create_wchar_path(const char *utf8)
59 {
60  int
61  count;
62 
63  wchar_t
64  *wideChar;
65 
66  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
67  if ((count > MAX_PATH) && (strncmp(utf8,"\\\\?\\",4) != 0) &&
68  (NTLongPathsEnabled() == MagickFalse))
69  {
70  char
71  buffer[MagickPathExtent];
72 
73  wchar_t
74  shortPath[MAX_PATH],
75  *longPath;
76 
77  (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
78  count+=4;
79  longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
80  if (longPath == (wchar_t *) NULL)
81  return((wchar_t *) NULL);
82  count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
83  if (count != 0)
84  count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
85  longPath=(wchar_t *) RelinquishMagickMemory(longPath);
86  if ((count < 5) || (count >= MAX_PATH))
87  return((wchar_t *) NULL);
88  wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
89  wcscpy(wideChar,shortPath+4);
90  return(wideChar);
91  }
92  wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
93  if (wideChar == (wchar_t *) NULL)
94  return((wchar_t *) NULL);
95  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
96  if (count == 0)
97  {
98  wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
99  return((wchar_t *) NULL);
100  }
101  return(wideChar);
102 }
103 #endif
104 
105 static inline int access_utf8(const char *path,int mode)
106 {
107 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
108  return(access(path,mode));
109 #else
110  int
111  status;
112 
113  wchar_t
114  *path_wide;
115 
116  path_wide=create_wchar_path(path);
117  if (path_wide == (wchar_t *) NULL)
118  return(-1);
119  status=_waccess(path_wide,mode);
120  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
121  return(status);
122 #endif
123 }
124 
125 static inline FILE *fopen_utf8(const char *path,const char *mode)
126 {
127 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
128  return(fopen(path,mode));
129 #else
130  FILE
131  *file;
132 
133  wchar_t
134  *mode_wide,
135  *path_wide;
136 
137  path_wide=create_wchar_path(path);
138  if (path_wide == (wchar_t *) NULL)
139  return((FILE *) NULL);
140  mode_wide=create_wchar_path(mode);
141  if (mode_wide == (wchar_t *) NULL)
142  {
143  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
144  return((FILE *) NULL);
145  }
146  file=_wfopen(path_wide,mode_wide);
147  mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
148  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
149  return(file);
150 #endif
151 }
152 
153 static inline void getcwd_utf8(char *path,size_t extent)
154 {
155 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
156  char
157  *directory;
158 
159  directory=getcwd(path,extent);
160  (void) directory;
161 #else
162  wchar_t
163  wide_path[MagickPathExtent];
164 
165  (void) _wgetcwd(wide_path,MagickPathExtent-1);
166  (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
167 #endif
168 }
169 
170 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
171 typedef int
172  mode_t;
173 #endif
174 
175 static inline int open_utf8(const char *path,int flags,mode_t mode)
176 {
177 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
178  return(open(path,flags,mode));
179 #else
180  int
181  status;
182 
183  wchar_t
184  *path_wide;
185 
186  path_wide=create_wchar_path(path);
187  if (path_wide == (wchar_t *) NULL)
188  return(-1);
189  status=_wopen(path_wide,flags,mode);
190  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
191  return(status);
192 #endif
193 }
194 
195 static inline FILE *popen_utf8(const char *command,const char *type)
196 {
197 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
198  return(popen(command,type));
199 #else
200  FILE
201  *file;
202 
203  int
204  length;
205 
206  wchar_t
207  *command_wide,
208  type_wide[5];
209 
210  file=(FILE *) NULL;
211  length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
212  if (length == 0)
213  return(file);
214  length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
215  if (length == 0)
216  return(file);
217  command_wide=(wchar_t *) AcquireQuantumMemory(length,sizeof(*command_wide));
218  if (command_wide == (wchar_t *) NULL)
219  return(file);
220  length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
221  if (length != 0)
222  file=_wpopen(command_wide,type_wide);
223  command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
224  return(file);
225 #endif
226 }
227 
228 static inline int remove_utf8(const char *path)
229 {
230 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
231  return(unlink(path));
232 #else
233  int
234  status;
235 
236  wchar_t
237  *path_wide;
238 
239  path_wide=create_wchar_path(path);
240  if (path_wide == (wchar_t *) NULL)
241  return(-1);
242  status=_wremove(path_wide);
243  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
244  return(status);
245 #endif
246 }
247 
248 static inline int rename_utf8(const char *source,const char *destination)
249 {
250 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
251  return(rename(source,destination));
252 #else
253  int
254  status;
255 
256  wchar_t
257  *destination_wide,
258  *source_wide;
259 
260  source_wide=create_wchar_path(source);
261  if (source_wide == (wchar_t *) NULL)
262  return(-1);
263  destination_wide=create_wchar_path(destination);
264  if (destination_wide == (wchar_t *) NULL)
265  {
266  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
267  return(-1);
268  }
269  status=_wrename(source_wide,destination_wide);
270  destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
271  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
272  return(status);
273 #endif
274 }
275 
276 static inline int stat_utf8(const char *path,struct stat *attributes)
277 {
278 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
279  return(stat(path,attributes));
280 #else
281  int
282  status;
283 
284  wchar_t
285  *path_wide;
286 
287  path_wide=create_wchar_path(path);
288  if (path_wide == (WCHAR *) NULL)
289  return(-1);
290  status=wstat(path_wide,attributes);
291  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
292  return(status);
293 #endif
294 }
295 
296 #if defined(__cplusplus) || defined(c_plusplus)
297 }
298 #endif
299 
300 #endif
static FILE * popen_utf8(const char *command, const char *type)
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:467
static void getcwd_utf8(char *path, size_t extent)
Definition: vms.h:941
static int stat_utf8(const char *path, struct stat *attributes)
MagickBooleanType
Definition: magick-type.h:169
MagickPrivate ssize_t GetMagickPageSize(void)
Definition: utility.c:1132
static int remove_utf8(const char *path)
MagickPrivate void ExpandFilename(char *)
Definition: utility.c:616
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:665
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: vms.h:950
struct dirent * readdir(DIR *)
#define MagickPathExtent
static int open_utf8(const char *path, int flags, mode_t mode)
MagickPrivate MagickBooleanType ShredFile(const char *)
Definition: utility.c:1845
MagickPrivate char ** GetPathComponents(const char *, size_t *)
MagickPrivate char ** ListFiles(const char *, const char *, size_t *)
Definition: utility.c:1609
MagickPrivate MagickBooleanType GetExecutionPath(char *, const size_t)
static int rename_utf8(const char *source, const char *destination)
static int access_utf8(const char *path, int mode)
static int MagickReadDirectory(DIR *directory, struct dirent *entry, struct dirent **result)
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1162
MagickPrivate void ChopPathComponents(char *, const size_t)
#define MagickPrivate