blob: 5d018670e5072f7e0ed06e2bf38bf3396fab6ff6 [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Victor Stinnerb306d752010-10-07 22:09:40 +00002#ifdef MS_WINDOWS
3# include <windows.h>
4#endif
Victor Stinner4e314432010-10-07 21:45:39 +00005
6#ifdef HAVE_STAT
7
8/* Decode a byte string from the locale encoding with the
9 surrogateescape error handler (undecodable bytes are decoded as characters
10 in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate
11 character, escape the bytes using the surrogateescape error handler instead
12 of decoding them.
13
14 Use _Py_wchar2char() to encode the character string back to a byte string.
15
16 Return a pointer to a newly allocated (wide) character string (use
17 PyMem_Free() to free the memory), or NULL on error (conversion error or
18 memory error). */
19wchar_t*
20_Py_char2wchar(char* arg)
21{
22 wchar_t *res;
23#ifdef HAVE_BROKEN_MBSTOWCS
24 /* Some platforms have a broken implementation of
25 * mbstowcs which does not count the characters that
26 * would result from conversion. Use an upper bound.
27 */
28 size_t argsize = strlen(arg);
29#else
30 size_t argsize = mbstowcs(NULL, arg, 0);
31#endif
32 size_t count;
33 unsigned char *in;
34 wchar_t *out;
35#ifdef HAVE_MBRTOWC
36 mbstate_t mbs;
37#endif
38 if (argsize != (size_t)-1) {
39 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
40 if (!res)
41 goto oom;
42 count = mbstowcs(res, arg, argsize+1);
43 if (count != (size_t)-1) {
44 wchar_t *tmp;
45 /* Only use the result if it contains no
46 surrogate characters. */
47 for (tmp = res; *tmp != 0 &&
48 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
49 ;
50 if (*tmp == 0)
51 return res;
52 }
53 PyMem_Free(res);
54 }
55 /* Conversion failed. Fall back to escaping with surrogateescape. */
56#ifdef HAVE_MBRTOWC
57 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
58
59 /* Overallocate; as multi-byte characters are in the argument, the
60 actual output could use less memory. */
61 argsize = strlen(arg) + 1;
62 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
63 if (!res) goto oom;
64 in = (unsigned char*)arg;
65 out = res;
66 memset(&mbs, 0, sizeof mbs);
67 while (argsize) {
68 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
69 if (converted == 0)
70 /* Reached end of string; null char stored. */
71 break;
72 if (converted == (size_t)-2) {
73 /* Incomplete character. This should never happen,
74 since we provide everything that we have -
75 unless there is a bug in the C library, or I
76 misunderstood how mbrtowc works. */
77 fprintf(stderr, "unexpected mbrtowc result -2\n");
78 return NULL;
79 }
80 if (converted == (size_t)-1) {
81 /* Conversion error. Escape as UTF-8b, and start over
82 in the initial shift state. */
83 *out++ = 0xdc00 + *in++;
84 argsize--;
85 memset(&mbs, 0, sizeof mbs);
86 continue;
87 }
88 if (*out >= 0xd800 && *out <= 0xdfff) {
89 /* Surrogate character. Escape the original
90 byte sequence with surrogateescape. */
91 argsize -= converted;
92 while (converted--)
93 *out++ = 0xdc00 + *in++;
94 continue;
95 }
96 /* successfully converted some bytes */
97 in += converted;
98 argsize -= converted;
99 out++;
100 }
101#else
102 /* Cannot use C locale for escaping; manually escape as if charset
103 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
104 correctly in the locale's charset, which must be an ASCII superset. */
105 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
106 if (!res) goto oom;
107 in = (unsigned char*)arg;
108 out = res;
109 while(*in)
110 if(*in < 128)
111 *out++ = *in++;
112 else
113 *out++ = 0xdc00 + *in++;
114 *out = 0;
115#endif
116 return res;
117oom:
118 fprintf(stderr, "out of memory\n");
119 return NULL;
120}
121
122/* Encode a (wide) character string to the locale encoding with the
123 surrogateescape error handler (characters in range U+DC80..U+DCFF are
124 converted to bytes 0x80..0xFF).
125
126 This function is the reverse of _Py_char2wchar().
127
128 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
129 the memory), or NULL on error (conversion error or memory error). */
130char*
131_Py_wchar2char(const wchar_t *text)
132{
133 const size_t len = wcslen(text);
134 char *result = NULL, *bytes = NULL;
135 size_t i, size, converted;
136 wchar_t c, buf[2];
137
138 /* The function works in two steps:
139 1. compute the length of the output buffer in bytes (size)
140 2. outputs the bytes */
141 size = 0;
142 buf[1] = 0;
143 while (1) {
144 for (i=0; i < len; i++) {
145 c = text[i];
146 if (c >= 0xdc80 && c <= 0xdcff) {
147 /* UTF-8b surrogate */
148 if (bytes != NULL) {
149 *bytes++ = c - 0xdc00;
150 size--;
151 }
152 else
153 size++;
154 continue;
155 }
156 else {
157 buf[0] = c;
158 if (bytes != NULL)
159 converted = wcstombs(bytes, buf, size);
160 else
161 converted = wcstombs(NULL, buf, 0);
162 if (converted == (size_t)-1) {
163 if (result != NULL)
164 PyMem_Free(result);
165 return NULL;
166 }
167 if (bytes != NULL) {
168 bytes += converted;
169 size -= converted;
170 }
171 else
172 size += converted;
173 }
174 }
175 if (result != NULL) {
176 *bytes = 0;
177 break;
178 }
179
180 size += 1; /* nul byte at the end */
181 result = PyMem_Malloc(size);
182 if (result == NULL)
183 return NULL;
184 bytes = result;
185 }
186 return result;
187}
188
Victor Stinner4e314432010-10-07 21:45:39 +0000189/* In principle, this should use HAVE__WSTAT, and _wstat
190 should be detected by autoconf. However, no current
191 POSIX system provides that function, so testing for
192 it is pointless.
193 Not sure whether the MS_WINDOWS guards are necessary:
194 perhaps for cygwin/mingw builds?
195*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000196#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
197int
198_Py_wstat(const wchar_t* path, struct stat *buf)
199{
Victor Stinner4e314432010-10-07 21:45:39 +0000200 int err;
201 char *fname;
202 fname = _Py_wchar2char(path);
203 if (fname == NULL) {
204 errno = EINVAL;
205 return -1;
206 }
207 err = stat(fname, buf);
208 PyMem_Free(fname);
209 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000210}
211#endif
212
213/* Call _wstat() on Windows, or stat() otherwise. Only fill st_mode
214 attribute on Windows. Return 0 on success, -1 on stat error or (if
215 PyErr_Occurred()) unicode error. */
216
217int
218_Py_stat(PyObject *unicode, struct stat *statbuf)
219{
220#ifdef MS_WINDOWS
221 wchar_t *path;
222 int err;
223 struct _stat wstatbuf;
224
225 path = PyUnicode_AsWideCharString(unicode, NULL);
226 if (path == NULL)
227 return -1;
228 err = _wstat(path, &wstatbuf);
229 PyMem_Free(path);
230 if (!err)
231 statbuf->st_mode = wstatbuf.st_mode;
232 return err;
233#else
234 int ret;
235 PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
236 if (bytes == NULL)
237 return -1;
238 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
239 Py_DECREF(bytes);
240 return ret;
241#endif
242}
243
244FILE *
245_Py_wfopen(const wchar_t *path, const wchar_t *mode)
246{
247#ifndef MS_WINDOWS
248 FILE *f;
249 char *cpath;
250 char cmode[10];
251 size_t r;
252 r = wcstombs(cmode, mode, 10);
253 if (r == (size_t)-1 || r >= 10) {
254 errno = EINVAL;
255 return NULL;
256 }
257 cpath = _Py_wchar2char(path);
258 if (cpath == NULL)
259 return NULL;
260 f = fopen(cpath, cmode);
261 PyMem_Free(cpath);
262 return f;
263#else
264 return _wfopen(path, mode);
265#endif
266}
267
268/* Call _wfopen() on Windows, or fopen() otherwise. Return the new file
269 object on success, or NULL if the file cannot be open or (if
270 PyErr_Occurred()) on unicode error */
271
272FILE*
273_Py_fopen(PyObject *unicode, const char *mode)
274{
275#ifdef MS_WINDOWS
276 wchar_t *path;
277 wchar_t wmode[10];
278 int usize;
279 FILE *f;
280
281 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
282 if (usize == 0)
283 return NULL;
284
285 path = PyUnicode_AsWideCharString(unicode, NULL);
286 if (path == NULL)
287 return NULL;
288 f = _wfopen(path, wmode);
289 PyMem_Free(path);
290 return f;
291#else
292 FILE *f;
293 PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
294 if (bytes == NULL)
295 return NULL;
296 f = fopen(PyBytes_AS_STRING(bytes), mode);
297 Py_DECREF(bytes);
298 return f;
299#endif
300}
301
302#ifdef HAVE_READLINK
303int
304_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
305{
306 char *cpath;
307 char cbuf[PATH_MAX];
308 int res;
309 size_t r1;
310
311 cpath = _Py_wchar2char(path);
312 if (cpath == NULL) {
313 errno = EINVAL;
314 return -1;
315 }
316 res = (int)readlink(cpath, cbuf, PATH_MAX);
317 PyMem_Free(cpath);
318 if (res == -1)
319 return -1;
320 if (res == PATH_MAX) {
321 errno = EINVAL;
322 return -1;
323 }
324 cbuf[res] = '\0'; /* buf will be null terminated */
325 r1 = mbstowcs(buf, cbuf, bufsiz);
326 if (r1 == -1) {
327 errno = EINVAL;
328 return -1;
329 }
330 return (int)r1;
331}
332#endif
333
334#ifdef HAVE_REALPATH
335wchar_t*
336_Py_wrealpath(const wchar_t *path, wchar_t *resolved_path)
337{
338 char *cpath;
339 char cresolved_path[PATH_MAX];
340 char *res;
341 size_t r;
342 cpath = _Py_wchar2char(path);
343 if (cpath == NULL) {
344 errno = EINVAL;
345 return NULL;
346 }
347 res = realpath(cpath, cresolved_path);
348 PyMem_Free(cpath);
349 if (res == NULL)
350 return NULL;
351 r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);
352 if (r == (size_t)-1 || r >= PATH_MAX) {
353 errno = EINVAL;
354 return NULL;
355 }
356 return resolved_path;
357}
358#endif
359
360wchar_t*
361_Py_wgetcwd(wchar_t *buf, size_t size)
362{
363#ifdef MS_WINDOWS
364 return _wgetcwd(buf, size);
365#else
366 char fname[PATH_MAX];
367 if (getcwd(fname, PATH_MAX) == NULL)
368 return NULL;
369 if (mbstowcs(buf, fname, size) >= size) {
370 errno = ERANGE;
371 return NULL;
372 }
373 return buf;
374#endif
375}
376
377#endif