forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
266 lines (241 loc) · 9.84 KB
/
File.cpp
File metadata and controls
266 lines (241 loc) · 9.84 KB
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
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <Windows.h>
#include "File.h"
#include "D2BS.h"
#include "Helpers.h"
using namespace std;
char* readLine(FILE* fptr, bool locking) {
if (feof(fptr))
return NULL;
string buffer;
char c = 0;
// grab all the characters in this line
do {
if (locking)
c = (char)fgetc(fptr);
else
c = (char)_fgetc_nolock(fptr);
// append the new character unless it's a carriage return
if (c != '\r' && c != '\n' && !feof(fptr))
buffer.append(1, c);
} while (!feof(fptr) && c != '\n');
return _strdup(buffer.c_str());
}
bool writeValue(FILE* fptr, JSContext* cx, jsval value, bool isBinary, bool locking) {
char* str;
int len = 0, result;
int32 ival = 0;
jsdouble dval = 0;
bool bval;
switch (JS_TypeOfValue(cx, value)) {
case JSTYPE_VOID:
case JSTYPE_NULL:
if (locking)
result = fwrite(&ival, sizeof(int), 1, fptr);
else
result = _fwrite_nolock(&ival, sizeof(int), 1, fptr);
if (result == 1)
return true;
break;
case JSTYPE_STRING:
str = JS_EncodeStringToUTF8(cx, JSVAL_TO_STRING(value));
if (locking)
result = fwrite(str, sizeof(char), strlen(str), fptr);
else
result = _fwrite_nolock(str, sizeof(char), strlen(str), fptr);
JS_free(cx, str);
return (int)strlen(str) == result;
break;
case JSTYPE_NUMBER:
if (isBinary) {
if (JSVAL_IS_DOUBLE(value)) {
if (JS_ValueToNumber(cx, value, &dval)) {
if (locking)
result = fwrite(&dval, sizeof(jsdouble), 1, fptr);
else
result = _fwrite_nolock(&dval, sizeof(jsdouble), 1, fptr);
return result == 1;
} else
return false;
} else if (JSVAL_IS_INT(value)) {
if (JS_ValueToInt32(cx, value, &ival)) {
if (locking)
result = fwrite(&ival, sizeof(int32), 1, fptr);
else
result = _fwrite_nolock(&dval, sizeof(int32), 1, fptr);
return result == 1;
} else
return false;
}
} else {
if (JSVAL_IS_DOUBLE(value)) {
if (JS_ValueToNumber(cx, value, &dval) == JS_FALSE)
return false;
// jsdouble will never be a 64-char string, but I'd rather be safe than sorry
str = new char[64];
sprintf_s(str, 64, "%.16f", dval);
len = strlen(str);
if (locking)
result = fwrite(str, sizeof(char), len, fptr);
else
result = _fwrite_nolock(str, sizeof(char), len, fptr);
delete[] str;
if (result == len)
return true;
} else if (JSVAL_IS_INT(value)) {
if (JS_ValueToInt32(cx, value, &ival) == JS_FALSE)
return false;
str = new char[16];
_itoa_s(ival, str, 16, 10);
len = strlen(str);
if (locking)
result = fwrite(str, sizeof(char), len, fptr);
else
result = _fwrite_nolock(str, sizeof(char), len, fptr);
delete[] str;
if (result == len)
return true;
}
}
break;
case JSTYPE_BOOLEAN:
if (!isBinary) {
bval = !!JSVAL_TO_BOOLEAN(value);
str = bval ? "true" : "false";
if (locking)
result = fwrite(str, sizeof(char), strlen(str), fptr);
else
result = _fwrite_nolock(str, sizeof(char), strlen(str), fptr);
return (int)strlen(str) == result;
} else {
bval = !!JSVAL_TO_BOOLEAN(value);
if (locking)
result = fwrite(&bval, sizeof(bool), 1, fptr);
else
result = _fwrite_nolock(&bval, sizeof(bool), 1, fptr);
return result == 1;
}
break;
/* case JSTYPE_OBJECT:
JSObject *arr = JSVAL_TO_OBJECT(value);
if(JS_IsArrayObject(cx, arr)) {
JS_GetArrayLength(cx, arr, &uival);
for(jsuint i = 0; i < uival; i++)
{
jsval val;
JS_GetElement(cx, arr, i, &val);
if(!writeValue(fptr, cx, val, isBinary))
return false;
}
return true;
}
else
{
JSString* jsstr = JS_ValueToString(cx, value);
str = JS_EncodeString(cx,jsstr);
if(locking)
result = fwrite(str, sizeof(char), strlen(str), fptr);
else
result = _fwrite_nolock(str, sizeof(char), strlen(str), fptr);
return strlen(str) == result;
}
break;
*/
}
return false;
}
/** Safely open a file relative to the script dir.
*
* In theory this is the only function used to open files that is exposed to
* javascript.
*
* \param filename Name of the file to open relative to the script folder
*
* \param mode Mode to open in. See fopen_s.
*
* \param cx JSContext that is running. Used to throw errors.
*
* \return The file pointer.
*/
FILE* fileOpenRelScript(const wchar_t* filename, const wchar_t* mode, JSContext* cx) {
FILE* f;
wchar_t fullPath[_MAX_PATH + _MAX_FNAME];
// Get the relative path
if (getPathRelScript(filename, _MAX_PATH + _MAX_FNAME, fullPath) == NULL) {
JS_ReportError(cx, "Invalid file name");
return NULL;
}
// Open the file
if (_wfopen_s(&f, fullPath, mode) != 0 || f == NULL) {
char message[128];
_strerror_s(message, 128, NULL);
JS_ReportError(cx, "Couldn't open file %ls: %s", filename, message);
return NULL;
}
return f;
}
/** Get the full path relative to the script path. Does the validation check.
*
* \param filename Name of the file to open relative to the script folder.
*
* \param bufLen Length of the output buffer.
*
* \param fullPath Buffer where the full path will be stored. Empty string if
* location is invalid.
*
* \return fullPath on success or NULL on failure.
*/
wchar_t* getPathRelScript(const wchar_t* filename, int bufLen, wchar_t* fullPath) {
wchar_t fullScriptPath[_MAX_PATH + _MAX_FNAME];
wchar_t* relPath;
int strLenScript;
DWORD scrPathLen;
strLenScript = wcslen(Vars.szScriptPath);
// Make the filename relative to the script path
relPath = (wchar_t*)_alloca((strLenScript + wcslen(filename) + 2) * 2); // *2 for wide chars
wcscpy_s(relPath, strLenScript + wcslen(filename) + 2, Vars.szScriptPath);
relPath[strLenScript] = L'\\';
wcscpy_s(relPath + strLenScript + 1, wcslen(filename) + 1, filename);
// Transform to the full pathname
GetFullPathNameW(relPath, bufLen, fullPath, NULL);
// Get the full path of the script path, check it is the prefix of fullPath
scrPathLen = GetFullPathNameW(Vars.szScriptPath, _MAX_PATH + _MAX_FNAME, fullScriptPath, NULL);
// Check that fullScriptPath is the prefix of fullPath
// As GetFullPathName seems to not add a trailing \, if there is not a
// trailing \ in fullScriptPath check for it in fullPath
if (wcsncmp(fullPath, fullScriptPath, scrPathLen) != 0 || (fullScriptPath[scrPathLen - 1] != '\\' && fullPath[scrPathLen] != '\\')) {
fullPath[0] = '\0';
return NULL;
}
return fullPath;
}
/** Check that the full path of the script path is the prefix of the fullpath
* of name. Also checks that there is no ..\ or ../ sequences or ":?*<>| chars.
*
* \param name The file/path to validate.
*
* \return true if path is valid, false otherwise.
*/
bool isValidPath(const wchar_t* name) {
wchar_t fullPath[_MAX_PATH + _MAX_FNAME];
// Use getPathRelScript to validate based on full paths
if (getPathRelScript(name, _MAX_PATH + _MAX_FNAME, fullPath) == NULL)
return false;
return (!wcsstr(name, L"..\\") && !wcsstr(name, L"../") && (wcscspn(name, L"\":?*<>|") == wcslen(name)));
}