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