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
|
/*************************************************************************
vStrip by [maven] (maven@maven.de)
udf.c: routines for udf-parsing (because windows just doesn't cut it),
refs: udf102.pdf, udf200.pdf, ecma 167
(tabsize 2)
*************************************************************************/
#include "stdafx.h"
#include "udf.h"
static bool aspi_GetSectorInfo(const HANDLE hDrive, DWORD* sec_size, DWORD* max_sec)
{
LARGE_INTEGER size = {0, 0};
GetFileSizeEx(hDrive, &size);
*sec_size = 2048;
*max_sec = (DWORD)(size.QuadPart / *sec_size);
return true;
}
static bool aspi_ReadSectors(const HANDLE hDrive, int lba, int nSectors, DWORD sec_size, BYTE* sector)
{
DWORD nbr = 0;
LARGE_INTEGER offset;
offset.QuadPart = lba*sec_size;
SetFilePointerEx(hDrive, offset, &offset, FILE_BEGIN);
return lba*sec_size == offset.QuadPart && ReadFile(hDrive, sector, nSectors*sec_size, &nbr, nullptr);
}
static bool udf_GetLBA(const tp_udf_FileEntry fe, const DWORD sec_size, DWORD *start, DWORD *end)
{
if (fe->LengthofAllocationDescriptors == 0) {
return false;
}
switch (fe->ICBTag.Flags & udf_icbf_Mask) {
case udf_icbf_ShortAd: {
tp_udf_short_ad ad = (tp_udf_short_ad)(fe->ExtendedAttributes + fe->LengthofExtendedAttributes);
*start = ad->Location;
*end = *start + ((ad->Length & udf_LengthMask) - 1) / sec_size;
return true;
}
break;
case udf_icbf_LongAd: {
tp_udf_long_ad ad = (tp_udf_long_ad)(fe->ExtendedAttributes + fe->LengthofExtendedAttributes);
*start = ad->Location.Location; // ignore partition number
*end = *start + ((ad->Length & udf_LengthMask) - 1) / sec_size;
return true;
}
break;
case udf_icbf_ExtAd: {
tp_udf_ext_ad ad = (tp_udf_ext_ad)(fe->ExtendedAttributes + fe->LengthofExtendedAttributes);
*start = ad->Location.Location; // ignore partition number
*end = *start + ((ad->Length & udf_LengthMask) - 1) / sec_size;
return true;
}
break;
}
return false;
}
tp_udf_file udf_get_root(const HANDLE hDrive, const WORD partition_number)
{
BYTE sector[fio_SECTOR_SIZE];
tp_udf_tag tag = (tp_udf_tag)sector;
DWORD sec_size, max_sec, i, j;
DWORD MVDS_lba, MVDS_lba_end, MVDS_back_lba, MVDS_back_lba_end;
DWORD FileDescriptorSequence_lba, FileDescriptorSequence_lba_end;
DWORD partition_lba, parent_icb;
tp_udf_AnchorVolumeDescriptorPointer avd;
bool res, part_valid, vol_valid;
if (!aspi_GetSectorInfo(hDrive, &sec_size, &max_sec)) {
return nullptr;
}
if (sec_size != fio_SECTOR_SIZE || max_sec < 256) {
return nullptr;
}
// read AnchorVolumeDescriptorPointer at 256 (or MaxSec) (Tag == 2)
res = aspi_ReadSectors(hDrive, 256, 1, sec_size, sector);
if (!res || tag->TagIdentifier != udf_TAG_AnchorVolumeDescriptor) {
res = aspi_ReadSectors(hDrive, max_sec, 1, sec_size, sector);
if (!res || tag->TagIdentifier != udf_TAG_AnchorVolumeDescriptor) {
return nullptr;
}
}
// check Static Structures
// get MainVolumeDescriptorSequence Location & Length
avd = (tp_udf_AnchorVolumeDescriptorPointer)sector;
MVDS_lba = avd->MainVolumeDescriptorSequenceExtent.Location;
MVDS_lba_end = MVDS_lba + (avd->MainVolumeDescriptorSequenceExtent.Length - 1) / sec_size;
MVDS_back_lba = avd->ReserveVolumeDescriptorSequenceExtent.Location;
MVDS_back_lba_end = MVDS_back_lba + (avd->ReserveVolumeDescriptorSequenceExtent.Length - 1) / sec_size;
// read MVDS_Location..MVDS_Location + (MVDS_Length - 1) / SectorSize sectors
part_valid = vol_valid = false;
i = 1;
do {
// try twice (if we need to) for ReserveAnchor
j = MVDS_lba;
do {
res = aspi_ReadSectors(hDrive, j++, 1, sec_size, sector);
if (res) {
if (tag->TagIdentifier == udf_TAG_PartitionDescriptor && !part_valid) {
// get stuff out of partition
tp_udf_PartitionDescriptor par = (tp_udf_PartitionDescriptor )sector;
part_valid = par->PartitionNumber == partition_number;
if (part_valid) {
// extract par->PartitionStartingLocation, par->PartitionLength
partition_lba = par->PartitionStartingLocation;
}
} else if (tag->TagIdentifier == udf_TAG_LogicalVolumeDescriptor && !vol_valid) {
// get stuff out of volume
tp_udf_LogicalVolumeDescriptor vol = (tp_udf_LogicalVolumeDescriptor)sector;
// check_volume sector size
vol_valid = (vol->LogicalBlockSize == sec_size) && (partition_number == vol->FileSetDescriptorSequence.Location.PartitionNumber);
if (vol_valid) {
// extract vol->FileSetDescriptorSequence
FileDescriptorSequence_lba = vol->FileSetDescriptorSequence.Location.Location;
FileDescriptorSequence_lba_end = FileDescriptorSequence_lba + ((vol->FileSetDescriptorSequence.Length & udf_LengthMask) - 1) / sec_size;
}
}
} else {
tag->TagIdentifier = 0;
}
} while (j <= MVDS_lba_end && tag->TagIdentifier != udf_TAG_TerminatingDescriptor && ((!part_valid) || (!vol_valid)));
if ((!part_valid) || (!vol_valid)) {
// try backup
MVDS_lba = MVDS_back_lba;
MVDS_lba_end = MVDS_back_lba_end;
}
} while (i-- && ((!part_valid) || (!vol_valid)));
if (part_valid && vol_valid) {
// read FileSetDescriptor, get RootDir Location & Length, RootDir Length != 0
res = aspi_ReadSectors(hDrive, FileDescriptorSequence_lba + partition_lba, 1, sec_size, sector);
if (res && tag->TagIdentifier == udf_TAG_FileSetDescriptor) {
tp_udf_FileSetDescriptor fsd = (tp_udf_FileSetDescriptor)sector;
if (partition_number == fsd->RootDirectoryICB.Location.PartitionNumber) {
parent_icb = fsd->RootDirectoryICB.Location.Location;
res = aspi_ReadSectors(hDrive, partition_lba + parent_icb, 1, sec_size, sector);
if (res && tag->TagIdentifier == udf_TAG_FileEntry) {
tp_udf_FileEntry fe = (tp_udf_FileEntry)sector;
if (fe->ICBTag.FileType == udf_FT_Directory) {
tp_udf_file root = (tp_udf_file)malloc(sizeof *root);
root->partition_lba = partition_lba;
udf_GetLBA(fe, sec_size, &root->dir_lba, &root->dir_end_lba);
root->dir_left = (DWORD)fe->InformationLength; // don't want directories of more than 4gb
root->sector = nullptr;
root->fid = nullptr;
root->sec_size = sec_size;
strcpy_s(root->name, "/");
root->is_dir = true;
root->is_parent = false;
return root;
}
}
}
}
}
return nullptr;
}
static void udf_GetName(const BYTE *data, const DWORD len, char *target)
{
DWORD p = 1, i = 0;
if (len == 0 || !(data[0] & 0x18)) {
target[0] = '\0';
}
if (data[0] & 0x10) {
// ignore MSB of unicode16
p++;
while (p < len) {
target[i++] = data[p += 2];
}
} else {
while (p < len) {
target[i++] = data[p++];
}
}
target[i]='\0';
}
tp_udf_file udf_get_sub(const HANDLE hDrive, tp_udf_file f)
{
if (f->is_dir && !f->is_parent && f->fid) {
BYTE sector[fio_SECTOR_SIZE];
tp_udf_tag tag = (tp_udf_tag)sector;
bool res;
res = aspi_ReadSectors(hDrive, f->partition_lba + f->fid->ICB.Location.Location, 1, f->sec_size, sector);
if (res && tag->TagIdentifier == udf_TAG_FileEntry) {
tp_udf_FileEntry fe = (tp_udf_FileEntry)sector;
if (fe->ICBTag.FileType == udf_FT_Directory) {
tp_udf_file newf = (tp_udf_file)malloc(sizeof *newf);
if (newf == nullptr) {
return nullptr;
}
memset(newf, 0, sizeof(*newf));
strcpy_s(newf->name, f->name); // maybe just ""?
newf->sec_size = f->sec_size;
newf->partition_lba = f->partition_lba;
udf_GetLBA(fe, f->sec_size, &newf->dir_lba, &newf->dir_end_lba);
newf->dir_left = (DWORD)fe->InformationLength; // don't want directories of more than 4gb
newf->sector = nullptr;
newf->fid = nullptr;
newf->is_dir = true;
newf->is_parent = false;
return newf;
}
}
}
return nullptr;
}
tp_udf_file udf_get_next(const HANDLE hDrive, tp_udf_file f)
{
if (f->dir_left <= 0) {
f->fid = nullptr;
return nullptr;
}
if (f->fid) {
// advance to next FileIdentifierDescriptor
DWORD ofs = 4 * ((sizeof *(f->fid) + f->fid->LengthofImplementationUse + f->fid->LengthofFileIdentifier + 3) / 4);
f->fid = (tp_udf_FileIdentifierDescriptor)((BYTE *)f->fid + ofs);
}
if (f->fid == nullptr) {
bool res = true;
DWORD size = f->sec_size * (f->dir_end_lba - f->dir_lba + 1);
if (!f->sector) {
f->sector = (BYTE*)malloc(size);
}
res = aspi_ReadSectors(hDrive, f->partition_lba + f->dir_lba, (WORD)(f->dir_end_lba - f->dir_lba + 1), f->sec_size, f->sector);
if (res) {
f->fid = (tp_udf_FileIdentifierDescriptor)f->sector;
} else {
f->fid = nullptr;
}
}
if (f->fid && f->fid->DescriptorTag.TagIdentifier == udf_TAG_FileIdentifierDescriptor) {
DWORD ofs = 4 * ((sizeof *f->fid + f->fid->LengthofImplementationUse + f->fid->LengthofFileIdentifier + 3) / 4);
f->dir_left -= ofs;
f->is_dir = (f->fid->FileCharacteristics & udf_FID_Directory) != 0;
f->is_parent = (f->fid->FileCharacteristics & udf_FID_Parent) != 0;
udf_GetName(f->fid->ImplementationUse + f->fid->LengthofImplementationUse, f->fid->LengthofFileIdentifier, f->name);
return f;
}
return nullptr;
}
void udf_free(tp_udf_file f)
{
if (f) {
if (f->sector) {
free(f->sector);
}
free(f);
}
}
#pragma warning(push)
#pragma warning(disable:4995 4996)
#define udf_PATH_DELIMITERS "/\\"
static tp_udf_file udf_ff_traverse(const HANDLE hDrive, tp_udf_file f, char *token)
{
while (udf_get_next(hDrive, f)) {
if (_stricmp(token, f->name) == 0) {
char *next_tok = strtok(nullptr, udf_PATH_DELIMITERS);
if (!next_tok) {
return f; // found
} else if (f->is_dir) {
tp_udf_file f2 = udf_get_sub(hDrive, f);
if (f2) {
tp_udf_file f3 = udf_ff_traverse(hDrive, f2, next_tok);
if (!f3) {
udf_free(f2);
}
return f3;
}
}
}
}
return nullptr;
}
tp_udf_file udf_find_file(const HANDLE hDrive, const WORD partition, const char *name)
{
tp_udf_file f = udf_get_root(hDrive, partition), f2 = nullptr;
if (f) {
char tokenline[udf_MAX_PATHLEN];
char *token;
strcpy_s(tokenline, name);
token = strtok(tokenline, udf_PATH_DELIMITERS);
if (token) {
f2 = udf_ff_traverse(hDrive, f, token);
}
udf_free(f);
}
return f2;
}
#pragma warning(pop)
bool udf_get_lba(const HANDLE hDrive, const tp_udf_file f, DWORD *start_lba, DWORD *end_lba)
{
if (f->fid) {
BYTE sector[2048];
tp_udf_FileEntry fe = (tp_udf_FileEntry)sector;
bool res;
res = aspi_ReadSectors(hDrive, f->partition_lba + f->fid->ICB.Location.Location, 1, f->sec_size, sector);
if (res && fe->DescriptorTag.TagIdentifier == udf_TAG_FileEntry) {
return udf_GetLBA(fe, f->sec_size, start_lba, end_lba);
}
}
return false;
}
|