-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathfileio.c
169 lines (154 loc) · 4.09 KB
/
fileio.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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2004 Craig Edwards
// SPDX-FileCopyrightText: 2006 Richard Osborne
// SPDX-FileCopyrightText: 2017-2020 Stefan Schmidt
// SPDX-FileCopyrightText: 2019 Lucas Jansson
// SPDX-FileCopyrightText: 2019 Jannik Vogel
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hal/fileio.h>
#include <xboxkrnl/xboxkrnl.h>
#include <hal/debug.h>
#include <stdbool.h>
// #define DEBUG
static char *currentDirString = NULL;
static char *partitions[] =
{
"\\??\\D:\\",
"\\Device\\Harddisk0\\Partition1\\",
"\\Device\\Harddisk0\\Partition2\\",
"\\Device\\Harddisk0\\Partition3\\",
"\\Device\\Harddisk0\\Partition4\\",
"\\Device\\Harddisk0\\Partition5\\",
"\\Device\\Harddisk0\\Partition6\\",
"\\Device\\Harddisk0\\Partition7\\",
};
static int getPartitionIndex(char c)
{
switch(c)
{
case 'c': case 'C': return 2;
case 'd': case 'D': return 0;
case 'e': case 'E': return 1;
case 'f': case 'F': return 6;
case 'g': case 'G': return 7;
case 'x': case 'X': return 3;
case 'y': case 'Y': return 4;
case 'z': case 'Z': return 5;
default: return -1;
}
}
static char *getPartitionString(char c)
{
int i = getPartitionIndex(c);
if (i == -1)
return NULL;
else
return partitions[i];
}
static void setPartitionString(char c, char *string)
{
int i = getPartitionIndex(c);
if (i != -1)
partitions[i] = string;
}
static char *getCurrentDirString()
{
char *tmp;
if (currentDirString) {
return currentDirString;
}
currentDirString = malloc(XeImageFileName->Length + 1);
memcpy(currentDirString, XeImageFileName->Buffer, XeImageFileName->Length);
currentDirString[XeImageFileName->Length] = '\0';
// Remove XBE name, leaving the path
tmp = strrchr(currentDirString, '\\');
if (tmp) {
*(tmp + 1) = '\0';
} else {
free(currentDirString);
currentDirString = NULL;
}
return currentDirString;
}
/**
* Converts a DOS-style path (eg. "c:/foo/bar.txt") to a XBOX-style
* path (eg. "\Device\Harddisk0\Partition2\foo\bar.txt").
* Returns early if the path is alread in XBOX-style - will use the
* input path as-is.
*
* We handle the following scenarios (back/forward slashes are handled
* in all cases):
* c:\foo\bar.txt
* d:\foo\bar.txt
* .\foo\bar.txt ==> d:\foo\bar.txt
* \foo\bar.txt ==> d:\foo\bar.txt
* foo\bar.txt ==> d:\foo\bar.txt
* \\.\D:\foo\bar.txt ==> d:\foo\bar.txt
* \??\c:\foo\bar.txt ==> c:\foo\bar.txt
*/
int XConvertDOSFilenameToXBOX(const char *dosFilename, char *xboxFilename)
{
// Allow Xbox filenames directly
if (!memcmp(dosFilename, "\\Device\\", 8)) {
strcpy(xboxFilename, dosFilename);
return STATUS_SUCCESS;
}
// path contains the qualified pathname from the root
// directory without the leading slash. eg. "foo\bar.txt"
const char *path;
// partition points to a literal string representing
// the fully qualified device name
char *partition = NULL;
if (dosFilename[0] == '.' && (dosFilename[1] == '\\' || dosFilename[1] == '/'))
{
// .\foo\bar.txt
path = dosFilename+2;
partition = getCurrentDirString();
}
else if (dosFilename[0] == '\\' || dosFilename[0] == '/')
{
if (dosFilename[1] == '\\' && dosFilename[2] == '.' && dosFilename[3] == '\\')
{
// \\.\x:\foo\bar.txt
path = dosFilename+7;
partition = getPartitionString(dosFilename[4]);
}
else if (dosFilename[1] == '?' && dosFilename[2] == '?' && dosFilename[3] == '\\')
{
// \??\x:\foo\bar.txt
path = dosFilename+7;
partition = getPartitionString(dosFilename[4]);
}
else
{
// \foo\bar.txt
path = dosFilename+1;
partition = getCurrentDirString();
}
}
else if (dosFilename[1] == ':')
{
// x:\foo\bar.txt
path = dosFilename+3;
partition = getPartitionString(dosFilename[0]);
}
else
{
// foo\bar.txt
path = dosFilename;
partition = getCurrentDirString();
}
if (partition == NULL)
return ERROR_INVALID_DRIVE;
strcpy(xboxFilename, partition);
strcat(xboxFilename, path);
char *c = xboxFilename;
while (*c++)
{
if (*c == '/')
*c = '\\';
}
return STATUS_SUCCESS;
}