-
Notifications
You must be signed in to change notification settings - Fork 256
/
searchfile.c
250 lines (198 loc) · 7.86 KB
/
searchfile.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
/*
Copyright 2009-2010, jimmikaelkael
Licenced under Academic Free License version 3.0
Review Open PS2 Loader README & LICENSE files for further details.
*/
#include "internal.h"
static void cdvdman_trimspaces(char *str);
static struct dirTocEntry *cdvdman_locatefile(char *name, u32 tocLBA, int tocLength, int layer);
static int cdvdman_findfile(sceCdlFILE *pcd_file, const char *name, int layer);
typedef struct
{
u32 rootDirtocLBA;
u32 rootDirtocLength;
} layer_info_t;
static layer_info_t layer_info[2];
//-------------------------------------------------------------------------
static void cdvdman_trimspaces(char *str)
{
int i, len;
char *p;
len = strlen(str);
if (len == 0)
return;
for (i = len - 1; i != -1; i--) {
p = &str[i];
if ((*p != 0x20) && (*p != 0x2e))
break;
*p = 0;
}
}
//-------------------------------------------------------------------------
static struct dirTocEntry *cdvdman_locatefile(char *name, u32 tocLBA, int tocLength, int layer)
{
char cdvdman_dirname[32]; /* Like below, but follow the original SCE limitation of 32-characters.
Some games specify filenames like "\\FILEFILE.EXT;1", which result in a length longer than just 14.
SCE does not perform bounds-checking on this buffer. */
char cdvdman_curdir[15]; /* Maximum 14 characters: the filename (8) + '.' + extension (3) + ';' + '1'.
Unlike the SCE original which used a 32-character buffer,
we'll assume that ISO9660 disc images are all _strictly_ compliant with ISO9660 level 1. */
char *p = (char *)name;
char *slash;
int r, len, filename_len;
int tocPos;
struct dirTocEntry *tocEntryPointer;
lbl_startlocate:
DPRINTF("cdvdman_locatefile start locating, layer=%d\n", layer);
while (*p == '/')
p++;
while (*p == '\\')
p++;
slash = strchr(p, '/');
// if the path doesn't contain a '/' then look for a '\'
if (!slash)
slash = strchr(p, '\\');
len = (u32)slash - (u32)p;
// if a slash was found
if (slash != NULL) {
#ifdef __IOPCORE_DEBUG
if (len >= sizeof(cdvdman_dirname)) {
DPRINTF("cdvdman_locatefile: segment too long: (%d chars) \"%s\"\n", len, p);
asm volatile("break\n");
}
#endif
// copy the path into main 'dir' var
strncpy(cdvdman_dirname, p, len);
cdvdman_dirname[len] = 0;
} else {
#ifdef __IOPCORE_DEBUG
len = strlen(p);
if (len >= sizeof(cdvdman_dirname)) {
DPRINTF("cdvdman_locatefile: filename too long: (%d chars) \"%s\"\n", len, p);
asm volatile("break\n");
}
#endif
strcpy(cdvdman_dirname, p);
}
while (tocLength > 0) {
if (sceCdRead(tocLBA, 1, cdvdman_buf, NULL) == 0)
return NULL;
sceCdSync(0);
DPRINTF("cdvdman_locatefile tocLBA read done\n");
tocLength -= 2048;
tocLBA++;
tocPos = 0;
do {
tocEntryPointer = (struct dirTocEntry *)&cdvdman_buf[tocPos];
if (tocEntryPointer->length == 0)
break;
filename_len = tocEntryPointer->filenameLength;
if (filename_len) {
strncpy(cdvdman_curdir, tocEntryPointer->filename, filename_len); // copy filename
cdvdman_curdir[filename_len] = 0;
#ifdef __IOPCORE_DEBUG
DPRINTF("cdvdman_locatefile strcmp %s %s\n", cdvdman_dirname, cdvdman_curdir);
#endif
r = strncmp(cdvdman_dirname, cdvdman_curdir, 12);
if ((!r) && (!slash)) { // we searched a file so it's found
DPRINTF("cdvdman_locatefile found file! LBA=%d size=%d\n", (int)tocEntryPointer->fileLBA, (int)tocEntryPointer->fileSize);
return tocEntryPointer;
} else if ((!r) && (tocEntryPointer->fileProperties & 2)) { // we found it but it's a directory
tocLBA = tocEntryPointer->fileLBA;
tocLength = tocEntryPointer->fileSize;
p = &slash[1];
if (!(cdvdman_settings.common.flags & IOPCORE_COMPAT_EMU_DVDDL)) {
int on_dual;
u32 layer1_start;
sceCdReadDvdDualInfo(&on_dual, &layer1_start);
if (layer)
tocLBA += layer1_start;
}
goto lbl_startlocate;
} else {
tocPos += (tocEntryPointer->length << 16) >> 16;
}
}
} while (tocPos < 2016);
}
DPRINTF("cdvdman_locatefile file not found!!!\n");
return NULL;
}
//-------------------------------------------------------------------------
static int cdvdman_findfile(sceCdlFILE *pcdfile, const char *name, int layer)
{
static char cdvdman_filepath[256];
u32 lsn;
struct dirTocEntry *tocEntryPointer;
layer_info_t *pLayerInfo;
cdvdman_init();
if (cdvdman_settings.common.flags & IOPCORE_COMPAT_EMU_DVDDL)
layer = 0;
pLayerInfo = (layer != 0) ? &layer_info[1] : &layer_info[0]; //SCE CDVDMAN simply treats a non-zero value as a signal for the 2nd layer.
WaitSema(cdvdman_searchfilesema);
DPRINTF("cdvdman_findfile %s layer%d\n", name, layer);
strncpy(cdvdman_filepath, name, sizeof(cdvdman_filepath));
cdvdman_filepath[sizeof(cdvdman_filepath) - 1] = '\0';
cdvdman_trimspaces(cdvdman_filepath);
DPRINTF("cdvdman_findfile cdvdman_filepath=%s\n", cdvdman_filepath);
if (pLayerInfo->rootDirtocLBA == 0) {
SignalSema(cdvdman_searchfilesema);
return 0;
}
tocEntryPointer = cdvdman_locatefile(cdvdman_filepath, pLayerInfo->rootDirtocLBA, pLayerInfo->rootDirtocLength, layer);
if (tocEntryPointer == NULL) {
SignalSema(cdvdman_searchfilesema);
return 0;
}
lsn = tocEntryPointer->fileLBA;
if (layer) {
sceCdReadDvdDualInfo((int *)&pcdfile->lsn, &pcdfile->size);
lsn += pcdfile->size;
}
pcdfile->lsn = lsn;
// Skip Videos: Apply 0 (zero) filesize to PSS videos
if ((cdvdman_settings.common.flags & IOPCORE_COMPAT_0_SKIP_VIDEOS) &&
((!strncmp(&cdvdman_filepath[strlen(cdvdman_filepath) - 6], ".PSS", 4)) ||
(!strncmp(&cdvdman_filepath[strlen(cdvdman_filepath) - 6], ".pss", 4))))
pcdfile->size = 0;
else
pcdfile->size = tocEntryPointer->fileSize;
strcpy(pcdfile->name, strrchr(name, '\\') + 1);
DPRINTF("cdvdman_findfile found %s\n", name);
SignalSema(cdvdman_searchfilesema);
return 1;
}
//-------------------------------------------------------------------------
int sceCdSearchFile(sceCdlFILE *pcd_file, const char *name)
{
DPRINTF("sceCdSearchFile %s\n", name);
return cdvdman_findfile(pcd_file, name, 0);
}
//-------------------------------------------------------------------------
int sceCdLayerSearchFile(sceCdlFILE *fp, const char *name, int layer)
{
DPRINTF("sceCdLayerSearchFile %s\n", name);
return cdvdman_findfile(fp, name, layer);
}
void cdvdman_searchfile_init(void)
{
// Read the volume descriptor
sceCdRead(16, 1, cdvdman_buf, NULL);
sceCdSync(0);
struct dirTocEntry *tocEntryPointer = (struct dirTocEntry *)&cdvdman_buf[0x9c];
layer_info[0].rootDirtocLBA = tocEntryPointer->fileLBA;
layer_info[0].rootDirtocLength = tocEntryPointer->length;
// DVD DL support
if (!(cdvdman_settings.common.flags & IOPCORE_COMPAT_EMU_DVDDL)) {
int on_dual;
u32 layer1_start;
sceCdReadDvdDualInfo(&on_dual, &layer1_start);
if (on_dual) {
sceCdRead(layer1_start + 16, 1, cdvdman_buf, NULL);
sceCdSync(0);
tocEntryPointer = (struct dirTocEntry *)&cdvdman_buf[0x9c];
layer_info[1].rootDirtocLBA = layer1_start + tocEntryPointer->fileLBA;
layer_info[1].rootDirtocLength = tocEntryPointer->length;
}
}
}