1#if defined(__MINGW32__)
3# define MINGW_HAS_SECURE_API 1
8#include "internal/error.h"
14#ifndef INVALID_FILE_ATTRIBUTES
15# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
19static struct code_page_table {
24#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
25#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
27IS_ABSOLUTE_PATH_P(
const WCHAR *path,
size_t len)
37#define INVALID_CODE_PAGE 51932
38#define PATH_BUFFER_SIZE MAX_PATH * 2
40#define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
43#define system_code_page rb_w32_filecp
44#define mbstr_to_wstr rb_w32_mbstr_to_wstr
45#define wstr_to_mbstr rb_w32_wstr_to_mbstr
48replace_wchar(
wchar_t *s,
int find,
int replace)
59remove_invalid_alternative_data(
wchar_t *wfullpath,
size_t size)
61 static const wchar_t prime[] =
L":$DATA";
62 enum { prime_len = (
sizeof(prime) /
sizeof(
wchar_t)) -1 };
64 if (
size <= prime_len || _wcsnicmp(wfullpath +
size - prime_len, prime, prime_len) != 0)
69 if (wfullpath[
size - (prime_len + 1)] ==
':') {
71 size -= prime_len + 1;
72 wfullpath[
size] =
L'\0';
76 wchar_t *pos = wfullpath +
size - (prime_len + 1);
80 wfullpath[
size] =
L'\0';
94 const char *n = (
const char *)
name;
95 if (strncmp(
"CP", n, 2) == 0) {
96 int code_page = atoi(n + 2);
98 struct code_page_table *cp = (
struct code_page_table *)arg;
99 unsigned int count = cp->count;
100 USHORT *table = cp->table;
102 unsigned int i =
count;
103 count = (((idx + 4) & ~31) | 28);
110 table[idx] = (USHORT)code_page;
139 if (0 <= enc_idx && (
unsigned int)enc_idx < rb_code_page.count)
140 return rb_code_page.table[enc_idx];
145#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
152replace_to_long_name(
wchar_t **wfullpath,
size_t size,
size_t buffer_size)
154 WIN32_FIND_DATAW find_data;
166 size_t const max_short_name_size = 8 + 1 + 3;
167 size_t const max_extension_size = 3;
168 size_t path_len = 1, extension_len = 0;
169 wchar_t *pos = *wfullpath;
171 if (
size == 3 && pos[1] ==
L':' && pos[2] ==
L'\\' && pos[3] ==
L'\0') {
177 if (wcspbrk(pos,
L"*?")) {
181 pos = *wfullpath +
size - 1;
183 if (!extension_len && *pos ==
L'.') {
184 extension_len = path_len - 1;
186 if (path_len > max_short_name_size || extension_len > max_extension_size) {
193 if ((pos >= *wfullpath + 2) &&
194 (*wfullpath)[0] ==
L'\\' && (*wfullpath)[1] ==
L'\\') {
197 if (pos == *wfullpath + 2) {
201 if (!wmemchr(*wfullpath + 2,
L'\\', pos - *wfullpath - 2)) {
207 find_handle = FindFirstFileW(*wfullpath, &find_data);
210 size_t file_len = wcslen(find_data.cFileName);
211 size_t oldsize =
size;
213 FindClose(find_handle);
214 size = trail_pos + file_len;
215 if (
size > (buffer_size ? buffer_size-1 : oldsize)) {
217 wcsncpy(
buf, *wfullpath, trail_pos);
222 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
228user_length_in_path(
const wchar_t *wuser,
size_t len)
244 if (
len == -1)
len = lstrlenW(ws);
270 append_wstr(result, dir, -1,
279 size_t size = 0, whome_len = 0;
280 size_t buffer_len = 0;
281 long wpath_len = 0, wdir_len = 0;
282 wchar_t *wfullpath =
NULL, *wpath =
NULL, *wpath_pos =
NULL;
283 wchar_t *wdir =
NULL, *wdir_pos =
NULL;
284 wchar_t *whome =
NULL, *buffer =
NULL, *buffer_pos =
NULL;
286 VALUE path = fname, dir = dname;
288 wchar_t path_drive =
L'\0', dir_drive =
L'\0';
304 cp = path_cp = code_page(path_encoding);
317#if SIZEOF_INT < SIZEOF_LONG
318 if ((
long)(
int)path_len != path_len) {
329 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] ==
L'~' &&
339 whome_len = wcslen(whome);
341 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
350 cp = path_cp = code_page(path_encoding);
366 else if (wpath_len >= 2 && wpath_pos[1] ==
L':') {
373 path_drive = wpath_pos[0];
378 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] ==
L'~') {
380 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
381 path_cp, path_encoding);
390 if (!ignore_dir && !
NIL_P(dir)) {
399#if SIZEOF_INT < SIZEOF_LONG
400 if ((
long)(
int)dir_len != dir_len) {
401 if (wpath)
free(wpath);
410 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] ==
L'~' &&
421 whome_len = wcslen(whome);
423 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
440 else if (wdir_len >= 2 && wdir[1] ==
L':') {
452 while (pos < wdir_len && separators < 2) {
462 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] ==
L'~') {
464 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
465 path_cp, path_encoding);
477 if (!ignore_dir && path_drive && dir_drive) {
478 if (towupper(path_drive) != towupper(dir_drive)) {
486 if (!ignore_dir && wpath_len >= 2 &&
IS_DIR_UNC_P(wpath)) {
498 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
500 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
504 wcsncpy(buffer_pos, whome, whome_len);
505 buffer_pos += whome_len;
509 if (whome_len && wcsrchr(
L"\\/:", buffer_pos[-1]) ==
NULL) {
510 buffer_pos[0] =
L'\\';
513 else if (!dir_drive && path_drive) {
514 *buffer_pos++ = path_drive;
515 *buffer_pos++ =
L':';
523 wcsncpy(buffer_pos, wdir_pos, wdir_len);
524 buffer_pos += wdir_len;
528 if (wdir_len && wcsrchr(
L"\\/:", buffer_pos[-1]) ==
NULL) {
529 buffer_pos[0] =
L'\\';
535 wcsncpy(buffer_pos, wpath_pos, wpath_len);
536 buffer_pos += wpath_len;
540 if (wpath_len == 0) {
541 buffer_pos[0] =
L'.';
546 buffer_pos[0] =
L'\0';
549 if (!tainted && !IS_ABSOLUTE_PATH_P(buffer, buffer_len))
561 wfullpath = wfullpath_buffer;
566 wfullpath[
size - 2] !=
L':' &&
569 wfullpath[
size] =
L'\0';
573 if (wfullpath[
size - 1] ==
L'.') {
575 wfullpath[
size] =
L'\0';
579 size = remove_invalid_alternative_data(wfullpath,
size);
584 size = replace_to_long_name(&wfullpath,
size, bufsize);
588 replace_wchar(wfullpath,
L'\\',
L'/');
592 result = append_wstr(result, wfullpath,
size, path_cp, path_encoding);
611 if (wfullpath != wfullpath_buffer)
631 cp = path_cp = code_page(enc);
641 if (e == ERROR_MORE_DATA) {
655 path_cp = code_page(enc);
656 len = lstrlenW(wbuf);
671 if (!wpath)
return 0;
673 attr = GetFileAttributesW(wpath);
675 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
679 HANDLE h = CreateFileW(wpath, GENERIC_READ,
680 FILE_SHARE_READ | FILE_SHARE_WRITE,
681 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
696 WCHAR *wname, wmode[4];
700 int e = 0, n = MultiByteToWideChar(CP_ACP, 0,
mode, -1,
NULL, 0);
701 if (n >
numberof(wmode))
return EINVAL;
702 MultiByteToWideChar(CP_ACP, 0,
mode, -1, wmode,
numberof(wmode));
705 len = MultiByteToWideChar(CP_UTF8, 0,
name, n,
NULL, 0);
707 len = MultiByteToWideChar(CP_UTF8, 0,
name, n, wname,
len);
710#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
711 e = _wfreopen(wname, wmode,
file) ? 0 : errno;
715 e = _wfreopen_s(&newfp, wname, wmode,
file);
725 if (rb_code_page.count)
return;
#define rb_ascii8bit_encindex()
#define rb_usascii_encindex()
#define rb_utf8_encindex()
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
rb_encoding * rb_filesystem_encoding(void)
rb_encoding * rb_enc_get(VALUE obj)
int rb_enc_to_index(rb_encoding *enc)
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
char str[HTML_ESCAPE_MAX_LEN+1]
#define RSTRING_LEN(string)
#define RSTRING_PTR(string)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
#define ECONV_UNDEF_REPLACE
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
#define ECONV_INVALID_REPLACE
int rb_enc_str_asciionly_p(VALUE)
void rb_econv_close(rb_econv_t *ec)
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
void rb_str_set_len(VALUE, long)
void rb_str_modify_expand(VALUE, long)
#define rb_str_new_cstr(str)
#define rb_syserr_fail_path(err, path)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
#define INVALID_HANDLE_VALUE
#define RSTRING_GETMEM(str, ptrvar, lenvar)
int rb_file_load_ok(const char *path)
int rb_freopen(VALUE fname, const char *mode, FILE *file)
#define INVALID_CODE_PAGE
#define INVALID_FILE_ATTRIBUTES
#define IS_DIR_SEPARATOR_P(c)
VALUE rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
void Init_w32_codepage(void)
VALUE rb_readlink(VALUE path, rb_encoding *resultenc)
#define fix_string_encoding(str, encoding)
void rb_enc_foreach_name(int(*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
VALUE rb_default_home_dir(VALUE result)
WCHAR * rb_w32_home_dir(void)
#define rb_w32_reparse_buffer_size(n)
int rb_w32_read_reparse_point(const WCHAR *path, rb_w32_reparse_buffer_t *rp, size_t bufsize, WCHAR **result, DWORD *len)
int rb_w32_map_errno(DWORD)