-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpath.c
458 lines (367 loc) · 12.2 KB
/
xpath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <ruby.h>
#include <ruby/encoding.h>
#include <windows.h>
#include <shlwapi.h>
#include <sddl.h>
#ifdef __MINGW32__
#define swprintf _snwprintf
#endif
#ifdef HAVE_PATHCCH_H
#include <pathcch.h>
#define MAX_WPATH PATHCCH_MAX_CCH
#else
#define MAX_WPATH MAX_PATH * sizeof(wchar_t)
#endif
// Equivalent to raise SystemCallError.new(string, errnum)
void rb_raise_syserr(const char* msg, DWORD errnum){
VALUE v_sys = rb_funcall(rb_eSystemCallError, rb_intern("new"), 2, rb_str_new2(msg), LONG2FIX(errnum));
rb_funcall(rb_mKernel, rb_intern("raise"), 1, v_sys);
}
// Helper function to find user's home directory
wchar_t* find_user(wchar_t* str){
SID* sid;
DWORD cbSid, cbDom, cbData, lpType;
SID_NAME_USE peUse;
LPWSTR str_sid;
LONG rv;
HKEY phkResult;
wchar_t subkey[MAX_WPATH];
wchar_t* lpData;
wchar_t* dom;
wchar_t* ptr;
const wchar_t* key_base = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\";
// Read up until first backslash, and preserve the rest for later
if (ptr = wcschr(str, L'\\')){
ptr++;
str[wcscspn(str, L"\\")] = 0;
}
sid = (SID*)ruby_xmalloc(MAX_WPATH);
dom = (wchar_t*)ruby_xmalloc(MAX_WPATH);
cbSid = MAX_PATH;
cbDom = MAX_PATH;
// Get the user's SID
if (!LookupAccountNameW(NULL, str, sid, &cbSid, dom, &cbDom, &peUse)){
char* mstr;
DWORD length;
ruby_xfree(sid);
ruby_xfree(dom);
length = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
mstr = (char*)ruby_xmalloc(length);
length = WideCharToMultiByte(CP_UTF8, 0, str, -1, mstr, length, NULL, NULL);
if (!length){
ruby_xfree(str);
rb_raise_syserr("WideCharToMultiByte", GetLastError());
}
rb_raise(rb_eArgError, "can't find user '%s'", mstr);
}
ruby_xfree(dom); // Don't need this any more
// Get the stringy version of the SID
if (!ConvertSidToStringSidW(sid, &str_sid)){
ruby_xfree(sid);
rb_raise_syserr("ConvertSidToStringSid", GetLastError());
}
ruby_xfree(sid); // Don't need this any more
// Mash the stringified SID onto our base key
if(swprintf(subkey, MAX_WPATH, L"%s%s", key_base, str_sid) < 0)
rb_raise_syserr("swprintf", GetLastError());
// Get the key handle we need
rv = RegOpenKeyExW(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &phkResult);
if (rv != ERROR_SUCCESS)
rb_raise_syserr("RegOpenKeyEx", rv);
lpData = (wchar_t*)malloc(MAX_WPATH);
cbData = MAX_WPATH;
lpType = REG_EXPAND_SZ;
// Finally, get the user's home directory
rv = RegQueryValueExW(phkResult, L"ProfileImagePath", NULL, &lpType, (LPBYTE)lpData, &cbData);
if (rv != ERROR_SUCCESS){
ruby_xfree(lpData);
rb_raise(rb_eArgError, "can't find home directory for user %ls", str);
}
// Append any remaining path data that was originally present
if (ptr){
if (swprintf(lpData, MAX_WPATH, L"%s/%s", lpData, ptr) < 0)
rb_raise_syserr("swprintf", GetLastError());
}
return lpData;
}
/* Helper function to expand tilde into full path. Note that I don't use the
* PathCchXXX functions here because it's extremely unlikely that a person's
* home directory exceeds MAX_PATH. In the unlikely even that it does exceed
* MAX_PATH, an error will be raised.
*/
wchar_t* expand_tilde(){
DWORD size = 0;
wchar_t* home = NULL;
const wchar_t* env = L"HOME";
// First, try to get HOME environment variable
size = GetEnvironmentVariableW(env, NULL, 0);
// If that isn't found then try USERPROFILE
if(!size){
env = L"USERPROFILE";
size = GetEnvironmentVariableW(env, home, size);
}
// If that isn't found the try HOMEDRIVE + HOMEPATH
if(!size){
DWORD size2;
wchar_t* temp;
const wchar_t* env2 = L"HOMEPATH";
env = L"HOMEDRIVE";
#ifdef HAVE_PATHCCH_H
HRESULT hr;
#endif
// If neither are found then raise an error
size = GetEnvironmentVariableW(env, NULL, 0);
size2 = GetEnvironmentVariableW(env2, NULL, 0);
if(!size || !size2){
if (GetLastError() != ERROR_ENVVAR_NOT_FOUND)
rb_raise_syserr("GetEnvironmentVariable", GetLastError());
else
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding '~'");
}
home = (wchar_t*)ruby_xmalloc(MAX_WPATH);
temp = (wchar_t*)ruby_xmalloc(MAX_WPATH);
if(!GetEnvironmentVariableW(env, home, size) || !GetEnvironmentVariableW(env2, temp, size2)){
ruby_xfree(home);
ruby_xfree(temp);
rb_raise_syserr("GetEnvironmentVariable", GetLastError());
}
#ifdef HAVE_PATHCCH_H
hr = PathCchAppendEx(home, MAX_WPATH, temp, 1);
if(hr != S_OK){
ruby_xfree(home);
ruby_xfree(temp);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
if(!PathAppendW(home, temp)){
ruby_xfree(home);
ruby_xfree(temp);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
}
else{
home = (wchar_t*)ruby_xmalloc(MAX_WPATH);
size = GetEnvironmentVariableW(env, home, size);
if(!size){
ruby_xfree(home);
rb_raise_syserr("GetEnvironmentVariable", GetLastError());
}
}
while(wcsstr(home, L"/"))
home[wcscspn(home, L"/")] = L'\\';
if (PathIsRelativeW(home))
rb_raise(rb_eArgError, "non-absolute home");
return home;
}
/*
* File.expand_path(path, dir = nil)
*
* Converts +path+ to an absolute path.
*
* This is a custom version of the File.expand_path method for Windows
* that is much faster than the MRI core method. It also supports "~user"
* expansion on Windows while the MRI core method does not.
*/
static VALUE rb_xpath(int argc, VALUE* argv, VALUE self){
VALUE v_path, v_path_orig, v_dir_orig;
wchar_t* buffer = NULL;
wchar_t* ptr = NULL;
wchar_t* path = NULL;
char* final_path;
int length;
rb_encoding* path_encoding;
rb_econv_t* ec;
const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
rb_scan_args(argc, argv, "11", &v_path_orig, &v_dir_orig);
if (rb_respond_to(v_path_orig, rb_intern("to_path")))
v_path_orig = rb_funcall2(v_path_orig, rb_intern("to_path"), 0, NULL);
SafeStringValue(v_path_orig);
if (!NIL_P(v_dir_orig)){
if (rb_respond_to(v_dir_orig, rb_intern("to_path")))
v_dir_orig = rb_funcall2(v_dir_orig, rb_intern("to_path"), 0, NULL);
SafeStringValue(v_dir_orig);
}
// Dup and prep string for modification
path_encoding = rb_enc_get(v_path_orig);
if (rb_enc_to_index(path_encoding) == rb_utf8_encindex()){
v_path = rb_str_dup(v_path_orig);
}
else{
ec = rb_econv_open(rb_enc_name(path_encoding), "UTF-8", replaceflags);
v_path = rb_econv_str_convert(ec, v_path_orig, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
rb_str_modify_expand(v_path, MAX_WPATH);
// Make our path a wide string for later functions
length = MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_path), -1, NULL, 0);
path = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
if(!MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_path), -1, path, length)){
ruby_xfree(path);
rb_raise_syserr("MultibyteToWideChar", GetLastError());
}
// Convert all forward slashes to backslashes to Windows API functions work properly
while(wcsstr(path, L"/"))
path[wcscspn(path, L"/")] = L'\\';
// Handle ~ expansion if first character.
if ( (ptr = wcschr(path, L'~')) && ((int)(ptr - path) == 0) ){
wchar_t* home;
// Handle both ~/user and ~user syntax
if (ptr[1] && ptr[1] != L'\\'){
home = find_user(++ptr);
}
else{
#ifdef HAVE_PATHCCHAPPENDEX
HRESULT hr;
home = expand_tilde(path);
hr = PathCchAppendEx(home, MAX_WPATH, ++ptr, 1);
if(hr != S_OK){
ruby_xfree(home);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
home = expand_tilde(path);
if (!PathAppendW(home, ++ptr)){
ruby_xfree(home);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
}
path = home;
}
// Directory argument is present
if (!NIL_P(v_dir_orig)){
wchar_t* dir;
VALUE v_dir;
rb_encoding* dir_encoding;
#ifdef HAVE_PATHCCH_H
HRESULT hr;
#endif
dir_encoding = rb_enc_get(v_dir_orig);
// Hooray for encodings
if (rb_enc_to_index(dir_encoding) == rb_utf8_encindex()){
v_dir = rb_str_dup(v_dir_orig);
}
else{
ec = rb_econv_open(rb_enc_name(dir_encoding), "UTF-8", replaceflags);
v_dir = rb_econv_str_convert(ec, v_dir_orig, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
// Prep string for modification
rb_str_modify_expand(v_dir, MAX_WPATH);
length = MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_dir), -1, NULL, 0);
dir = (wchar_t*)ruby_xmalloc(MAX_WPATH * sizeof(wchar_t));
if (!MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_dir), -1, dir, length)){
ruby_xfree(dir);
rb_raise_syserr("MultibyteToWideChar", GetLastError());
}
while (wcsstr(dir, L"/"))
dir[wcscspn(dir, L"/")] = L'\\';
// Check for tilde in first character
if ( (ptr = wcschr(dir, L'~')) && ((int)(ptr - dir) == 0) ){
if (ptr[1] && ptr[1] != L'\\'){
dir = find_user(++ptr);
}
else{
dir = expand_tilde();
#ifdef HAVE_PATHCCH_H
hr = PathCchAppendEx(dir, MAX_WPATH, ++ptr, 1);
if(hr != S_OK){
ruby_xfree(dir);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
if (!PathAppendW(dir, ++ptr)){
ruby_xfree(dir);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
}
}
if (!wcslen(path))
path = dir;
if (PathIsRelativeW(path)){
#ifdef HAVE_PATHCCH_H
hr = PathCchAppendEx(dir, MAX_WPATH, path, 1);
if(hr != S_OK){
ruby_xfree(dir);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
if(!PathAppendW(dir, path)){
ruby_xfree(dir);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
// Remove leading slashes from relative paths
if (dir[0] == L'\\')
++dir;
path = dir;
}
}
else{
if (!wcslen(path)){
char* pwd;
wchar_t* wpwd;
// First call, get the length
length = GetCurrentDirectoryW(0, NULL);
wpwd = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
length = GetCurrentDirectoryW(length, wpwd);
if(!length){
ruby_xfree(wpwd);
rb_raise_syserr("GetCurrentDirectory", GetLastError());
}
// Convert backslashes into forward slashes
while(wcsstr(wpwd, L"\\"))
wpwd[wcscspn(wpwd, L"\\")] = L'/';
// Convert string back to multibyte string before returning Ruby object
length = WideCharToMultiByte(CP_UTF8, 0, wpwd, -1, NULL, 0, NULL, NULL);
pwd = (char*)ruby_xmalloc(length);
length = WideCharToMultiByte(CP_UTF8, 0, wpwd, -1, pwd, length, NULL, NULL);
if (!length){
ruby_xfree(pwd);
rb_raise_syserr("WideCharToMultiByte", GetLastError());
}
return rb_str_new2(pwd);
}
}
// Strip all trailing backslashes
#ifdef HAVE_PATHCCH_H
while (PathCchRemoveBackslash(path, wcslen(path)+1) == S_OK);
#else
while (!*PathRemoveBackslashW(path));
#endif
// First call, get the length
length = GetFullPathNameW(path, 0, buffer, NULL);
buffer = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
// Now get the path
length = GetFullPathNameW(path, length, buffer, NULL);
if (!length){
ruby_xfree(buffer);
rb_raise_syserr("GetFullPathName", GetLastError());
}
// Convert backslashes into forward slashes
while(wcsstr(buffer, L"\\"))
buffer[wcscspn(buffer, L"\\")] = L'/';
length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
final_path = (char*)ruby_xmalloc(length);
length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, final_path, length, NULL, NULL);
ruby_xfree(buffer);
if (!length){
ruby_xfree(final_path);
rb_raise_syserr("WideCharToMultiByte", GetLastError());
}
v_path = rb_str_new(final_path, length - 1); // Don't count null terminator
if (rb_enc_to_index(path_encoding) != rb_utf8_encindex()){
ec = rb_econv_open("UTF-8", rb_enc_name(path_encoding), replaceflags);
v_path = rb_econv_str_convert(ec, v_path, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
rb_enc_associate(v_path, path_encoding);
if (OBJ_TAINTED(v_path_orig) || rb_equal(v_path, v_path_orig) == Qfalse)
OBJ_TAINT(v_path);
return v_path;
}
void Init_xpath(){
rb_define_singleton_method(rb_cFile, "expand_path", rb_xpath, -1);
}