MagickCore  6.8.9
utility-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2014 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.
6  obtain a copy of the License at
7 
8  http://www.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 
30  ShredFile(const char *);
31 
32 /*
33  Windows UTF8 compatibility methods.
34 */
35 
36 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
37 static inline wchar_t *create_wchar_path(const char *utf8)
38 {
39  int
40  count;
41 
42  wchar_t
43  *wideChar;
44 
45  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
46  if (count > MAX_PATH)
47  {
48  char
49  buffer[MaxTextExtent];
50 
51  wchar_t
52  shortPath[MAX_PATH],
53  *longPath;
54 
55  (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
56  count+=4;
57  longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
58  if (longPath == (wchar_t *) NULL)
59  return((wchar_t *) NULL);
60  count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
61  if (count != 0)
62  count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
63  longPath=(wchar_t *) RelinquishMagickMemory(longPath);
64  if (count < 5)
65  return((wchar_t *) NULL);
66  wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
67  wcscpy(wideChar,shortPath+4);
68  return(wideChar);
69  }
70  wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
71  if (wideChar == (wchar_t *) NULL)
72  return((wchar_t *) NULL);
73  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
74  if (count == 0)
75  {
76  wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
77  return((wchar_t *) NULL);
78  }
79  return(wideChar);
80 }
81 #endif
82 
83 static inline int access_utf8(const char *path,int mode)
84 {
85 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
86  return(access(path,mode));
87 #else
88  int
89  status;
90 
91  wchar_t
92  *path_wide;
93 
94  path_wide=create_wchar_path(path);
95  if (path_wide == (wchar_t *) NULL)
96  return(-1);
97  status=_waccess(path_wide,mode);
98  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
99  return(status);
100 #endif
101 }
102 
103 static inline FILE *fopen_utf8(const char *path,const char *mode)
104 {
105 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
106  return(fopen(path,mode));
107 #else
108  FILE
109  *file;
110 
111  wchar_t
112  *mode_wide,
113  *path_wide;
114 
115  path_wide=create_wchar_path(path);
116  if (path_wide == (wchar_t *) NULL)
117  return((FILE *) NULL);
118  mode_wide=create_wchar_path(mode);
119  if (mode_wide == (wchar_t *) NULL)
120  {
121  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
122  return((FILE *) NULL);
123  }
124  file=_wfopen(path_wide,mode_wide);
125  mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
126  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
127  return(file);
128 #endif
129 }
130 
131 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
132 typedef int
133  mode_t;
134 #endif
135 
136 static inline int open_utf8(const char *path,int flags,mode_t mode)
137 {
138 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
139  return(open(path,flags,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=_wopen(path_wide,flags,mode);
151  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
152  return(status);
153 #endif
154 }
155 
156 static inline FILE *popen_utf8(const char *command,const char *type)
157 {
158 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
159  return(popen(command,type));
160 #else
161  FILE
162  *file;
163 
164  wchar_t
165  *type_wide,
166  *command_wide;
167 
168  command_wide=create_wchar_path(command);
169  if (command_wide == (wchar_t *) NULL)
170  return((FILE *) NULL);
171  type_wide=create_wchar_path(type);
172  if (type_wide == (wchar_t *) NULL)
173  {
174  command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
175  return((FILE *) NULL);
176  }
177  file=_wpopen(command_wide,type_wide);
178  type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
179  command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
180  return(file);
181 #endif
182 }
183 
184 static inline int remove_utf8(const char *path)
185 {
186 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
187  return(unlink(path));
188 #else
189  int
190  status;
191 
192  wchar_t
193  *path_wide;
194 
195  path_wide=create_wchar_path(path);
196  if (path_wide == (wchar_t *) NULL)
197  return(-1);
198  status=_wremove(path_wide);
199  path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
200  return(status);
201 #endif
202 }
203 
204 static inline int rename_utf8(const char *source,const char *destination)
205 {
206 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
207  return(rename(source,destination));
208 #else
209  int
210  status;
211 
212  wchar_t
213  *destination_wide,
214  *source_wide;
215 
216  source_wide=create_wchar_path(source);
217  if (source_wide == (wchar_t *) NULL)
218  return(-1);
219  destination_wide=create_wchar_path(destination);
220  if (destination_wide == (wchar_t *) NULL)
221  {
222  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
223  return(-1);
224  }
225  status=_wrename(source_wide,destination_wide);
226  destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
227  source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
228  return(status);
229 #endif
230 }
231 
232 static inline int stat_utf8(const char *path,struct stat *attributes)
233 {
234 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
235  return(stat(path,attributes));
236 #else
237  int
238  status;
239 
240  wchar_t
241  *path_wide;
242 
243  path_wide=create_wchar_path(path);
244  if (path_wide == (WCHAR *) NULL)
245  return(-1);
246  status=wstat(path_wide,attributes);
247  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
248  return(status);
249 #endif
250 }
251 
252 #if defined(__cplusplus) || defined(c_plusplus)
253 }
254 #endif
255 
256 #endif
static FILE * popen_utf8(const char *command, const char *type)
Definition: utility-private.h:156
char * path
Definition: type.h:55
static int stat_utf8(const char *path, struct stat *attributes)
Definition: utility-private.h:232
MagickBooleanType
Definition: magick-type.h:211
static int remove_utf8(const char *path)
Definition: utility-private.h:184
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:526
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: utility-private.h:103
#define MaxTextExtent
Definition: method-attribute.h:106
static int open_utf8(const char *path, int flags, mode_t mode)
Definition: utility-private.h:136
MagickPrivate MagickBooleanType ShredFile(const char *)
Definition: utility.c:1791
MagickExport ssize_t FormatLocaleString(char *restrict string, const size_t length, const char *restrict format,...)
Definition: locale.c:475
static int rename_utf8(const char *source, const char *destination)
Definition: utility-private.h:204
static int access_utf8(const char *path, int mode)
Definition: utility-private.h:83
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:985
#define MagickPrivate
Definition: method-attribute.h:99