MagickCore 6.9.12-20
Convert, Edit, Or Compose Bitmap Images
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 "magick/memory_.h"
22#include "magick/nt-base.h"
24
25#if defined(__cplusplus) || defined(c_plusplus)
26extern "C" {
27#endif
28
30 ShredFile(const char *);
31
32static 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)
46static 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 *) AcquireQuantumMemory(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 *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
76 wcscpy(wideChar,shortPath+4);
77 return(wideChar);
78 }
79 wideChar=(wchar_t *) AcquireQuantumMemory(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
92static inline int access_utf8(const char *path,int mode)
93{
94#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
95 return(access(path,mode));
96#else
97 int
98 status;
99
100 wchar_t
101 *path_wide;
102
103 path_wide=create_wchar_path(path);
104 if (path_wide == (wchar_t *) NULL)
105 return(-1);
106 status=_waccess(path_wide,mode);
107 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
108 return(status);
109#endif
110}
111
112static inline FILE *fopen_utf8(const char *path,const char *mode)
113{
114#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
115 return(fopen(path,mode));
116#else
117 FILE
118 *file;
119
120 wchar_t
121 *mode_wide,
122 *path_wide;
123
124 path_wide=create_wchar_path(path);
125 if (path_wide == (wchar_t *) NULL)
126 return((FILE *) NULL);
127 mode_wide=create_wchar_path(mode);
128 if (mode_wide == (wchar_t *) NULL)
129 {
130 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
131 return((FILE *) NULL);
132 }
133 file=_wfopen(path_wide,mode_wide);
134 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
135 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
136 return(file);
137#endif
138}
139
140static inline void getcwd_utf8(char *path,size_t extent)
141{
142#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
143 char
144 *directory;
145
146 directory=getcwd(path,extent);
147 (void) directory;
148#else
149 wchar_t
150 wide_path[MaxTextExtent];
151
152 (void) _wgetcwd(wide_path,MaxTextExtent-1);
153 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
154#endif
155}
156
157#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
158typedef int
159 mode_t;
160#endif
161
162static inline int open_utf8(const char *path,int flags,mode_t mode)
163{
164#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
165 return(open(path,flags,mode));
166#else
167 int
168 status;
169
170 wchar_t
171 *path_wide;
172
173 path_wide=create_wchar_path(path);
174 if (path_wide == (wchar_t *) NULL)
175 return(-1);
176 status=_wopen(path_wide,flags,mode);
177 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
178 return(status);
179#endif
180}
181
182static inline FILE *popen_utf8(const char *command,const char *type)
183{
184#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
185 return(popen(command,type));
186#else
187 FILE
188 *file;
189
190 int
191 length;
192
193 wchar_t
194 *command_wide,
195 type_wide[5];
196
197 file=(FILE *) NULL;
198 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
199 if (length == 0)
200 return(file);
201 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
202 if (length == 0)
203 return(file);
204 command_wide=(wchar_t *) AcquireQuantumMemory(length,sizeof(*command_wide));
205 if (command_wide == (wchar_t *) NULL)
206 return(file);
207 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
208 if (length != 0)
209 file=_wpopen(command_wide,type_wide);
210 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
211 return(file);
212#endif
213}
214
215static inline int remove_utf8(const char *path)
216{
217#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
218 return(unlink(path));
219#else
220 int
221 status;
222
223 wchar_t
224 *path_wide;
225
226 path_wide=create_wchar_path(path);
227 if (path_wide == (wchar_t *) NULL)
228 return(-1);
229 status=_wremove(path_wide);
230 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
231 return(status);
232#endif
233}
234
235static inline int rename_utf8(const char *source,const char *destination)
236{
237#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
238 return(rename(source,destination));
239#else
240 int
241 status;
242
243 wchar_t
244 *destination_wide,
245 *source_wide;
246
247 source_wide=create_wchar_path(source);
248 if (source_wide == (wchar_t *) NULL)
249 return(-1);
250 destination_wide=create_wchar_path(destination);
251 if (destination_wide == (wchar_t *) NULL)
252 {
253 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
254 return(-1);
255 }
256 status=_wrename(source_wide,destination_wide);
257 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
258 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
259 return(status);
260#endif
261}
262
263static inline int stat_utf8(const char *path,struct stat *attributes)
264{
265#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
266 return(stat(path,attributes));
267#else
268 int
269 status;
270
271 wchar_t
272 *path_wide;
273
274 path_wide=create_wchar_path(path);
275 if (path_wide == (WCHAR *) NULL)
276 return(-1);
277 status=wstat(path_wide,attributes);
278 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
279 return(status);
280#endif
281}
282
283#if defined(__cplusplus) || defined(c_plusplus)
284}
285#endif
286
287#endif
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:497
MagickExport struct dirent * readdir(DIR *)
MagickBooleanType
Definition: magick-type.h:199
@ MagickFalse
Definition: magick-type.h:200
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1162
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:665
#define MagickPrivate
Definition: method-attribute.h:81
#define MaxTextExtent
Definition: method-attribute.h:89
Definition: mac.h:42
Definition: mac.h:54
static int open_utf8(const char *path, int flags, mode_t mode)
Definition: utility-private.h:162
static int MagickReadDirectory(DIR *directory, struct dirent *entry, struct dirent **result)
Definition: utility-private.h:32
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: utility-private.h:112
static int remove_utf8(const char *path)
Definition: utility-private.h:215
static int access_utf8(const char *path, int mode)
Definition: utility-private.h:92
static int stat_utf8(const char *path, struct stat *attributes)
Definition: utility-private.h:263
static void getcwd_utf8(char *path, size_t extent)
Definition: utility-private.h:140
MagickPrivate MagickBooleanType ShredFile(const char *)
Definition: utility.c:1843
static int rename_utf8(const char *source, const char *destination)
Definition: utility-private.h:235
static FILE * popen_utf8(const char *command, const char *type)
Definition: utility-private.h:182