Skip to content

Latest commit

 

History

History
964 lines (798 loc) · 28.6 KB

archiver_iso9660.c

File metadata and controls

964 lines (798 loc) · 28.6 KB
 
1
2
3
4
5
6
7
8
/*
* ISO9660 support routines for PhysicsFS.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Christoph Nelles.
*/
Aug 21, 2010
Aug 21, 2010
9
10
/* !!! FIXME: this file needs Ryanification. */
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
* Handles CD-ROM disk images (and raw CD-ROM devices).
*
* Not supported:
* - RockRidge
* - Non 2048 Sectors
* - UDF
*
* Deviations from the standard
* - Ignores mandatory sort order
* - Allows various invalid file names
*
* Problems
* - Ambiguities in the standard
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Mar 23, 2012
Mar 23, 2012
30
31
#if PHYSFS_SUPPORTS_ISO9660
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
#include <time.h>
/* cache files smaller than this completely in memory */
#define ISO9660_FULLCACHEMAXSIZE 2048
/* !!! FIXME: this is going to cause trouble. */
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
/* This is the format as defined by the standard
typedef struct
{
PHYSFS_uint32 lsb;
PHYSFS_uint32 msb;
} ISOBB32bit; // 32byte Both Byte type, means the value first in LSB then in MSB
typedef struct
{
PHYSFS_uint16 lsb;
PHYSFS_uint16 msb;
} ISOBB16bit; // 16byte Both Byte type, means the value first in LSB then in MSB
*/
/* define better ones to simplify coding (less if's) */
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
#define ISOBB32bit(name) PHYSFS_uint32 name; PHYSFS_uint32 __dummy_##name;
#define ISOBB16bit(name) PHYSFS_uint16 name; PHYSFS_uint16 __dummy_##name;
#else
#define ISOBB32bit(name) PHYSFS_uint32 __dummy_##name; PHYSFS_uint32 name;
#define ISOBB16bit(name) PHYSFS_uint16 __dummy_##name; PHYSFS_uint16 name;
#endif
typedef struct
{
char year[4];
char month[2];
char day[2];
char hour[2];
char minute[2];
char second[2];
char centisec[2];
PHYSFS_sint8 offset; /* in 15min from GMT */
} ISO9660VolumeTimestamp;
typedef struct
{
PHYSFS_uint8 year;
PHYSFS_uint8 month;
PHYSFS_uint8 day;
PHYSFS_uint8 hour;
PHYSFS_uint8 minute;
PHYSFS_uint8 second;
PHYSFS_sint8 offset;
} ISO9660FileTimestamp;
typedef struct
{
unsigned existence:1;
unsigned directory:1;
unsigned associated_file:1;
unsigned record:1;
unsigned protection:1;
unsigned reserved:2;
unsigned multiextent:1;
} ISO9660FileFlags;
typedef struct
{
PHYSFS_uint8 length;
PHYSFS_uint8 attribute_length;
ISOBB32bit(extent_location)
ISOBB32bit(data_length)
ISO9660FileTimestamp timestamp;
ISO9660FileFlags file_flags;
PHYSFS_uint8 file_unit_size;
PHYSFS_uint8 gap_size;
ISOBB16bit(vol_seq_no)
PHYSFS_uint8 len_fi;
char unused;
} ISO9660RootDirectoryRecord;
/* this structure is combined for all Volume descriptor types */
typedef struct
{
PHYSFS_uint8 type;
char identifier[5];
PHYSFS_uint8 version;
PHYSFS_uint8 flags;
char system_identifier[32];
char volume_identifier[32];
char unused2[8];
ISOBB32bit(space_size)
PHYSFS_uint8 escape_sequences[32];
ISOBB16bit(vol_set_size)
ISOBB16bit(vol_seq_no)
ISOBB16bit(block_size)
ISOBB32bit(path_table_size)
/* PHYSFS_uint32 path_table_start_lsb; // why didn't they use both byte type?
PHYSFS_uint32 opt_path_table_start_lsb;
PHYSFS_uint32 path_table_start_msb;
PHYSFS_uint32 opt_path_table_start_msb;*/
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
PHYSFS_uint32 path_table_start;
PHYSFS_uint32 opt_path_table_start;
PHYSFS_uint32 unused6;
PHYSFS_uint32 unused7;
#else
PHYSFS_uint32 unused6;
PHYSFS_uint32 unused7;
PHYSFS_uint32 path_table_start;
PHYSFS_uint32 opt_path_table_start;
#endif
ISO9660RootDirectoryRecord rootdirectory;
char set_identifier[128];
char publisher_identifier[128];
char preparer_identifer[128];
char application_identifier[128];
char copyright_file_identifier[37];
char abstract_file_identifier[37];
char bibliographic_file_identifier[37];
ISO9660VolumeTimestamp creation_timestamp;
ISO9660VolumeTimestamp modification_timestamp;
ISO9660VolumeTimestamp expiration_timestamp;
ISO9660VolumeTimestamp effective_timestamp;
PHYSFS_uint8 file_structure_version;
char unused4;
char application_use[512];
char unused5[653];
} ISO9660VolumeDescriptor;
typedef struct
{
PHYSFS_uint8 recordlen;
PHYSFS_uint8 extattributelen;
ISOBB32bit(extentpos)
ISOBB32bit(datalen)
ISO9660FileTimestamp recordtime;
ISO9660FileFlags flags;
PHYSFS_uint8 file_unit_size;
PHYSFS_uint8 interleave_gap;
ISOBB16bit(volseqno)
PHYSFS_uint8 filenamelen;
char filename[222]; /* This is not exact, but makes reading easier */
} ISO9660FileDescriptor;
typedef struct
{
ISOBB16bit(owner)
ISOBB16bit(group)
PHYSFS_uint16 flags; /* not implemented*/
ISO9660VolumeTimestamp create_time; /* yes, not file timestamp */
ISO9660VolumeTimestamp mod_time;
ISO9660VolumeTimestamp expire_time;
ISO9660VolumeTimestamp effective_time;
PHYSFS_uint8 record_format;
PHYSFS_uint8 record_attributes;
ISOBB16bit(record_len)
char system_identifier[32];
char system_use[64];
PHYSFS_uint8 version;
ISOBB16bit(escape_len)
char reserved[64];
/** further fields not implemented */
} ISO9660ExtAttributeRec;
#pragma pack(pop) /* restore original alignment from stack */
typedef struct
{
Aug 30, 2010
Aug 30, 2010
201
PHYSFS_Io *io;
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
PHYSFS_uint32 rootdirstart;
PHYSFS_uint32 rootdirsize;
PHYSFS_uint64 currpos;
int isjoliet;
char *path;
void *mutex;
} ISO9660Handle;
typedef struct __ISO9660FileHandle
{
PHYSFS_sint64 filesize;
PHYSFS_uint64 currpos;
PHYSFS_uint64 startblock;
ISO9660Handle *isohandle;
PHYSFS_uint32 (*read) (struct __ISO9660FileHandle *filehandle, void *buffer,
Aug 21, 2010
Aug 21, 2010
218
PHYSFS_uint64 len);
219
int (*seek)(struct __ISO9660FileHandle *filehandle, PHYSFS_sint64 offset);
Aug 30, 2010
Aug 30, 2010
220
void (*close)(struct __ISO9660FileHandle *filehandle);
221
222
223
/* !!! FIXME: anonymouse union is going to cause problems. */
union
{
Aug 30, 2010
Aug 30, 2010
224
/* !!! FIXME: just use a memory PHYSFS_Io here, unify all this code. */
225
char *cacheddata; /* data of file when cached */
Aug 30, 2010
Aug 30, 2010
226
PHYSFS_Io *io; /* handle to separate opened file */
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
};
} ISO9660FileHandle;
/*******************************************************************************
* Time conversion functions
******************************************************************************/
static PHYSFS_sint64 iso_mktime(ISO9660FileTimestamp *timestamp)
{
struct tm tm;
tm.tm_year = timestamp->year;
tm.tm_mon = timestamp->month - 1;
tm.tm_mday = timestamp->day;
tm.tm_hour = timestamp->hour;
tm.tm_min = timestamp->minute;
tm.tm_sec = timestamp->second;
/* Ignore GMT offset for now... */
return mktime(&tm);
} /* iso_mktime */
static int iso_atoi2(char *text)
{
return ((text[0] - 40) * 10) + (text[1] - 40);
} /* iso_atoi2 */
static int iso_atoi4(char *text)
{
return ((text[0] - 40) * 1000) + ((text[1] - 40) * 100) +
((text[2] - 40) * 10) + (text[3] - 40);
} /* iso_atoi4 */
static PHYSFS_sint64 iso_volume_mktime(ISO9660VolumeTimestamp *timestamp)
Jul 6, 2017
Jul 6, 2017
259
260
{
PHYSFS_sint64 value;
261
262
263
264
265
266
267
268
struct tm tm;
tm.tm_year = iso_atoi4(timestamp->year);
tm.tm_mon = iso_atoi2(timestamp->month) - 1;
tm.tm_mday = iso_atoi2(timestamp->day);
tm.tm_hour = iso_atoi2(timestamp->hour);
tm.tm_min = iso_atoi2(timestamp->minute);
tm.tm_sec = iso_atoi2(timestamp->second);
/* this allows values outside the range of a unix timestamp... sanitize them */
Jul 6, 2017
Jul 6, 2017
269
value = mktime(&tm);
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
return value == -1 ? 0 : value;
} /* iso_volume_mktime */
/*******************************************************************************
* Filename extraction
******************************************************************************/
static int iso_extractfilenameISO(ISO9660FileDescriptor *descriptor,
char *filename, int *version)
{
*filename = '\0';
if (descriptor->flags.directory)
{
strncpy(filename, descriptor->filename, descriptor->filenamelen);
filename[descriptor->filenamelen] = '\0';
*version = 0;
} /* if */
else
{
/* find last SEPARATOR2 */
int pos = 0;
int lastfound = -1;
for(;pos < descriptor->filenamelen; pos++)
if (descriptor->filename[pos] == ';')
lastfound = pos;
Jul 6, 2017
Jul 6, 2017
295
296
BAIL_IF(lastfound < 1, PHYSFS_ERR_NOT_FOUND /* !!! FIXME: PHYSFS_ERR_BAD_FILENAME */, -1);
BAIL_IF(lastfound == (descriptor->filenamelen -1), PHYSFS_ERR_NOT_FOUND /* !!! PHYSFS_ERR_BAD_FILENAME */, -1);
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
strncpy(filename, descriptor->filename, lastfound);
if (filename[lastfound - 1] == '.')
filename[lastfound - 1] = '\0'; /* consume trailing ., as done in all implementations */
else
filename[lastfound] = '\0';
*version = atoi(descriptor->filename + lastfound);
} /* else */
return 0;
} /* iso_extractfilenameISO */
static int iso_extractfilenameUCS2(ISO9660FileDescriptor *descriptor,
char *filename, int *version)
{
PHYSFS_uint16 tmp[128];
PHYSFS_uint16 *src;
int len;
*filename = '\0';
*version = 1; /* Joliet does not have versions.. at least not on my images */
src = (PHYSFS_uint16*) descriptor->filename;
len = descriptor->filenamelen / 2;
tmp[len] = 0;
while(len--)
tmp[len] = PHYSFS_swapUBE16(src[len]);
PHYSFS_utf8FromUcs2(tmp, filename, 255);
return 0;
} /* iso_extractfilenameUCS2 */
static int iso_extractfilename(ISO9660Handle *handle,
ISO9660FileDescriptor *descriptor, char *filename,int *version)
{
if (handle->isjoliet)
return iso_extractfilenameUCS2(descriptor, filename, version);
else
return iso_extractfilenameISO(descriptor, filename, version);
} /* iso_extractfilename */
/*******************************************************************************
* Basic image read functions
******************************************************************************/
static int iso_readimage(ISO9660Handle *handle, PHYSFS_uint64 where,
Aug 21, 2010
Aug 21, 2010
346
void *buffer, PHYSFS_uint64 len)
Jul 6, 2017
Jul 6, 2017
347
348
{
int rc = -1;
Jul 6, 2017
Jul 6, 2017
349
BAIL_IF_ERRPASS(!__PHYSFS_platformGrabMutex(handle->mutex), -1);
350
if (where != handle->currpos)
Jul 6, 2017
Jul 6, 2017
351
GOTO_IF_ERRPASS(!handle->io->seek(handle->io,where), unlockme);
Aug 30, 2010
Aug 30, 2010
352
rc = handle->io->read(handle->io, buffer, len);
353
354
355
if (rc == -1)
{
handle->currpos = (PHYSFS_uint64) -1;
Mar 20, 2012
Mar 20, 2012
356
goto unlockme;
357
} /* if */
Aug 21, 2010
Aug 21, 2010
358
handle->currpos += rc;
359
360
361
362
363
364
365
366
367
368
369
370
unlockme:
__PHYSFS_platformReleaseMutex(handle->mutex);
return rc;
} /* iso_readimage */
static PHYSFS_sint64 iso_readfiledescriptor(ISO9660Handle *handle,
PHYSFS_uint64 where,
ISO9660FileDescriptor *descriptor)
{
PHYSFS_sint64 rc = iso_readimage(handle, where, descriptor,
Aug 21, 2010
Aug 21, 2010
371
sizeof (descriptor->recordlen));
Jul 6, 2017
Jul 6, 2017
372
373
BAIL_IF_ERRPASS(rc == -1, -1);
BAIL_IF(rc != 1, PHYSFS_ERR_CORRUPT, -1);
374
375
376
377
378
if (descriptor->recordlen == 0)
return 0; /* fill bytes at the end of a sector */
rc = iso_readimage(handle, where + 1, &descriptor->extattributelen,
Aug 21, 2010
Aug 21, 2010
379
descriptor->recordlen - sizeof(descriptor->recordlen));
Jul 6, 2017
Jul 6, 2017
380
381
BAIL_IF_ERRPASS(rc == -1, -1);
BAIL_IF(rc != 1, PHYSFS_ERR_CORRUPT, -1);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
return 0;
} /* iso_readfiledescriptor */
static void iso_extractsubpath(char *path, char **subpath)
{
*subpath = strchr(path,'/');
if (*subpath != 0)
{
**subpath = 0;
*subpath +=1;
} /* if */
} /* iso_extractsubpath */
/*
* Don't use path tables, they are not necessarily faster, but more complicated
* to implement as they store only directories and not files, so searching for
* a file needs to branch to the directory extent sooner or later.
*/
static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,
Nov 30, 2012
Nov 30, 2012
402
ISO9660FileDescriptor *descriptor)
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{
char *subpath = 0;
PHYSFS_uint64 readpos, end_of_dir;
char filename[255];
char pathcopy[256];
char *mypath;
int version = 0;
strcpy(pathcopy, path);
mypath = pathcopy;
readpos = handle->rootdirstart;
end_of_dir = handle->rootdirstart + handle->rootdirsize;
iso_extractsubpath(mypath, &subpath);
while (1)
{
Jul 6, 2017
Jul 6, 2017
419
BAIL_IF_ERRPASS(iso_readfiledescriptor(handle, readpos, descriptor), -1);
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* recordlen = 0 -> no more entries or fill entry */
if (!descriptor->recordlen)
{
/* if we are in the last sector of the directory & it's 0 -> end */
if ((end_of_dir - 2048) <= (readpos -1))
break; /* finished */
/* else skip to the next sector & continue; */
readpos = (((readpos - 1) / 2048) + 1) * 2048;
continue;
} /* if */
readpos += descriptor->recordlen;
if (descriptor->filenamelen == 1 && (descriptor->filename[0] == 0
|| descriptor->filename[0] == 1))
continue; /* special ones, ignore */
Jul 6, 2017
Jul 6, 2017
438
BAIL_IF_ERRPASS(iso_extractfilename(handle, descriptor, filename, &version), -1);
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
if (strcmp(filename, mypath) == 0)
{
if ( (subpath == 0) || (subpath[0] == 0) )
return 0; /* no subpaths left and we found the entry */
if (descriptor->flags.directory)
{
/* shorten the path to the subpath */
mypath = subpath;
iso_extractsubpath(mypath, &subpath);
/* gosub to the new directory extent */
readpos = descriptor->extentpos * 2048;
end_of_dir = readpos + descriptor->datalen;
} /* if */
else
{
Nov 30, 2012
Nov 30, 2012
456
/* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
457
458
459
460
461
462
/* we're at a file but have a remaining subpath -> no match */
return 0;
} /* else */
} /* if */
} /* while */
Nov 30, 2012
Nov 30, 2012
463
/* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
464
465
466
467
468
469
470
471
return 0;
} /* iso_find_dir_entry */
static int iso_read_ext_attributes(ISO9660Handle *handle, int block,
ISO9660ExtAttributeRec *attributes)
{
return iso_readimage(handle, block * 2048, attributes,
Aug 21, 2010
Aug 21, 2010
472
sizeof(ISO9660ExtAttributeRec));
473
474
475
} /* iso_read_ext_attributes */
Aug 30, 2010
Aug 30, 2010
476
static int ISO9660_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
Aug 30, 2010
Aug 30, 2010
478
static PHYSFS_Io *ISO9660_duplicate(PHYSFS_Io *_io)
Jul 6, 2017
Jul 6, 2017
480
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL); /* !!! FIXME: write me. */
Aug 30, 2010
Aug 30, 2010
481
} /* ISO9660_duplicate */
Aug 30, 2010
Aug 30, 2010
484
485
486
487
488
489
static void ISO9660_destroy(PHYSFS_Io *io)
{
ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
fhandle->close(fhandle);
allocator.Free(io);
} /* ISO9660_destroy */
Aug 30, 2010
Aug 30, 2010
492
493
494
495
496
497
498
499
500
static PHYSFS_sint64 ISO9660_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
{
ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
return fhandle->read(fhandle, buf, len);
} /* ISO9660_read */
static PHYSFS_sint64 ISO9660_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 l)
{
Jul 6, 2017
Jul 6, 2017
501
BAIL(PHYSFS_ERR_READ_ONLY, -1);
Aug 30, 2010
Aug 30, 2010
502
503
504
505
506
507
508
509
510
511
512
513
514
515
} /* ISO9660_write */
static PHYSFS_sint64 ISO9660_tell(PHYSFS_Io *io)
{
return ((ISO9660FileHandle*) io->opaque)->currpos;
} /* ISO9660_tell */
static int ISO9660_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
return fhandle->seek(fhandle, offset);
} /* ISO9660_seek */
Aug 30, 2010
Aug 30, 2010
518
519
520
521
522
523
524
525
static PHYSFS_sint64 ISO9660_length(PHYSFS_Io *io)
{
return ((ISO9660FileHandle*) io->opaque)->filesize;
} /* ISO9660_length */
static const PHYSFS_Io ISO9660_Io =
{
Mar 25, 2012
Mar 25, 2012
526
CURRENT_PHYSFS_IO_API_VERSION, NULL,
Aug 30, 2010
Aug 30, 2010
527
528
529
530
531
532
533
ISO9660_read,
ISO9660_write,
ISO9660_seek,
ISO9660_tell,
ISO9660_length,
ISO9660_duplicate,
ISO9660_flush,
Mar 25, 2012
Mar 25, 2012
534
ISO9660_destroy
Aug 30, 2010
Aug 30, 2010
535
536
537
538
539
540
};
/*******************************************************************************
* Archive management functions
******************************************************************************/
Aug 30, 2010
Aug 30, 2010
542
static void *ISO9660_openArchive(PHYSFS_Io *io, const char *filename, int forWriting)
Aug 30, 2010
Aug 30, 2010
544
char magicnumber[6];
545
546
547
ISO9660Handle *handle;
int founddescriptor = 0;
int foundjoliet = 0;
Aug 24, 2010
Aug 24, 2010
548
Aug 30, 2010
Aug 30, 2010
549
550
assert(io != NULL); /* shouldn't ever happen. */
Jul 6, 2017
Jul 6, 2017
551
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
Aug 30, 2010
Aug 30, 2010
552
553
/* Skip system area to magic number in Volume descriptor */
Jul 6, 2017
Jul 6, 2017
554
555
BAIL_IF_ERRPASS(!io->seek(io, 32769), NULL);
BAIL_IF_ERRPASS(io->read(io, magicnumber, 5) != 5, NULL);
Aug 30, 2010
Aug 30, 2010
556
if (memcmp(magicnumber, "CD001", 6) != 0)
Jul 6, 2017
Jul 6, 2017
557
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
558
559
handle = allocator.Malloc(sizeof(ISO9660Handle));
Jul 6, 2017
Jul 6, 2017
560
GOTO_IF(!handle, PHYSFS_ERR_OUT_OF_MEMORY, errorcleanup);
561
562
handle->path = 0;
handle->mutex= 0;
Aug 30, 2010
Aug 30, 2010
563
handle->io = NULL;
564
565
handle->path = allocator.Malloc(strlen(filename) + 1);
Jul 6, 2017
Jul 6, 2017
566
GOTO_IF(!handle->path, PHYSFS_ERR_OUT_OF_MEMORY, errorcleanup);
567
568
569
strcpy(handle->path, filename);
handle->mutex = __PHYSFS_platformCreateMutex();
Jul 6, 2017
Jul 6, 2017
570
GOTO_IF_ERRPASS(!handle->mutex, errorcleanup);
Aug 30, 2010
Aug 30, 2010
572
handle->io = io;
573
574
/* seek Primary Volume Descriptor */
Jul 6, 2017
Jul 6, 2017
575
GOTO_IF_ERRPASS(!io->seek(io, 32768), errorcleanup);
576
577
578
579
while (1)
{
ISO9660VolumeDescriptor descriptor;
Jul 6, 2017
Jul 6, 2017
580
581
GOTO_IF_ERRPASS(io->read(io, &descriptor, sizeof(ISO9660VolumeDescriptor)) != sizeof(ISO9660VolumeDescriptor), errorcleanup);
GOTO_IF(strncmp(descriptor.identifier, "CD001", 5) != 0, PHYSFS_ERR_UNSUPPORTED, errorcleanup);
582
583
584
585
586
587
588
if (descriptor.type == 255)
{
/* type 255 terminates the volume descriptor list */
if (founddescriptor)
return handle; /* ok, we've found one volume descriptor */
else
Jul 6, 2017
Jul 6, 2017
589
GOTO(PHYSFS_ERR_CORRUPT, errorcleanup);
590
591
592
} /* if */
if (descriptor.type == 1 && !founddescriptor)
{
Aug 30, 2010
Aug 30, 2010
593
handle->currpos = io->tell(io);
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
handle->rootdirstart =
descriptor.rootdirectory.extent_location * 2048;
handle->rootdirsize =
descriptor.rootdirectory.data_length;
handle->isjoliet = 0;
founddescriptor = 1; /* continue search for joliet */
} /* if */
if (descriptor.type == 2 && !foundjoliet)
{
/* check if is joliet */
PHYSFS_uint8 *s = descriptor.escape_sequences;
int joliet = !(descriptor.flags & 1)
&& (s[0] == 0x25)
&& (s[1] == 0x2F)
&& ((s[2] == 0x40) || (s[2] == 0x43) || (s[2] == 0x45));
if (!joliet)
continue;
Aug 30, 2010
Aug 30, 2010
612
handle->currpos = io->tell(io);
613
614
615
616
617
618
619
620
621
622
handle->rootdirstart =
descriptor.rootdirectory.extent_location * 2048;
handle->rootdirsize =
descriptor.rootdirectory.data_length;
handle->isjoliet = 1;
founddescriptor = 1;
foundjoliet = 1;
} /* if */
} /* while */
Jul 6, 2017
Jul 6, 2017
623
GOTO(PHYSFS_ERR_CORRUPT, errorcleanup); /* not found. */
624
625
626
627
628
629
630
631
errorcleanup:
if (handle)
{
if (handle->path)
allocator.Free(handle->path);
if (handle->mutex)
__PHYSFS_platformDestroyMutex(handle->mutex);
Aug 30, 2010
Aug 30, 2010
632
allocator.Free(handle);
633
634
635
636
637
} /* if */
return NULL;
} /* ISO9660_openArchive */
Nov 28, 2012
Nov 28, 2012
638
static void ISO9660_closeArchive(void *opaque)
639
640
{
ISO9660Handle *handle = (ISO9660Handle*) opaque;
Aug 30, 2010
Aug 30, 2010
641
handle->io->destroy(handle->io);
642
643
644
__PHYSFS_platformDestroyMutex(handle->mutex);
allocator.Free(handle->path);
allocator.Free(handle);
Mar 24, 2012
Mar 24, 2012
645
} /* ISO9660_closeArchive */
646
647
648
649
650
651
652
653
/*******************************************************************************
* Read functions
******************************************************************************/
static PHYSFS_uint32 iso_file_read_mem(ISO9660FileHandle *filehandle,
Aug 21, 2010
Aug 21, 2010
654
void *buffer, PHYSFS_uint64 len)
655
656
{
/* check remaining bytes & max obj which can be fetched */
Aug 21, 2010
Aug 21, 2010
657
658
659
const PHYSFS_sint64 bytesleft = filehandle->filesize - filehandle->currpos;
if (bytesleft < len)
len = bytesleft;
Aug 21, 2010
Aug 21, 2010
661
if (len == 0)
662
663
return 0;
Aug 21, 2010
Aug 21, 2010
664
665
666
667
memcpy(buffer, filehandle->cacheddata + filehandle->currpos, (size_t) len);
filehandle->currpos += len;
return (PHYSFS_uint32) len;
668
669
670
671
672
} /* iso_file_read_mem */
static int iso_file_seek_mem(ISO9660FileHandle *fhandle, PHYSFS_sint64 offset)
{
Jul 6, 2017
Jul 6, 2017
673
674
BAIL_IF(offset < 0, PHYSFS_ERR_INVALID_ARGUMENT, 0);
BAIL_IF(offset >= fhandle->filesize, PHYSFS_ERR_PAST_EOF, 0);
675
676
677
678
679
680
fhandle->currpos = offset;
return 0;
} /* iso_file_seek_mem */
Aug 30, 2010
Aug 30, 2010
681
static void iso_file_close_mem(ISO9660FileHandle *fhandle)
682
683
684
{
allocator.Free(fhandle->cacheddata);
allocator.Free(fhandle);
Aug 30, 2010
Aug 30, 2010
685
} /* iso_file_close_mem */
686
687
688
static PHYSFS_uint32 iso_file_read_foreign(ISO9660FileHandle *filehandle,
Aug 21, 2010
Aug 21, 2010
689
void *buffer, PHYSFS_uint64 len)
Jul 6, 2017
Jul 6, 2017
691
692
PHYSFS_sint64 rc;
693
/* check remaining bytes & max obj which can be fetched */
Aug 21, 2010
Aug 21, 2010
694
695
696
const PHYSFS_sint64 bytesleft = filehandle->filesize - filehandle->currpos;
if (bytesleft < len)
len = bytesleft;
Jul 6, 2017
Jul 6, 2017
698
rc = filehandle->io->read(filehandle->io, buffer, len);
Jul 6, 2017
Jul 6, 2017
699
BAIL_IF_ERRPASS(rc == -1, -1);
Aug 21, 2010
Aug 21, 2010
701
filehandle->currpos += rc; /* i trust my internal book keeping */
Jul 6, 2017
Jul 6, 2017
702
BAIL_IF(rc < len, PHYSFS_ERR_CORRUPT, -1);
703
704
705
706
707
708
return rc;
} /* iso_file_read_foreign */
static int iso_file_seek_foreign(ISO9660FileHandle *fhandle,
PHYSFS_sint64 offset)
Jul 6, 2017
Jul 6, 2017
709
710
711
{
PHYSFS_sint64 pos;
Jul 6, 2017
Jul 6, 2017
712
713
BAIL_IF(offset < 0, PHYSFS_ERR_INVALID_ARGUMENT, 0);
BAIL_IF(offset >= fhandle->filesize, PHYSFS_ERR_PAST_EOF, 0);
Jul 6, 2017
Jul 6, 2017
715
pos = fhandle->startblock * 2048 + offset;
Jul 6, 2017
Jul 6, 2017
716
BAIL_IF_ERRPASS(!fhandle->io->seek(fhandle->io, pos), -1);
717
718
719
720
721
722
fhandle->currpos = offset;
return 0;
} /* iso_file_seek_foreign */
Aug 30, 2010
Aug 30, 2010
723
static void iso_file_close_foreign(ISO9660FileHandle *fhandle)
Aug 30, 2010
Aug 30, 2010
725
fhandle->io->destroy(fhandle->io);
726
727
728
729
730
allocator.Free(fhandle);
} /* iso_file_close_foreign */
static int iso_file_open_mem(ISO9660Handle *handle, ISO9660FileHandle *fhandle)
Jul 6, 2017
Jul 6, 2017
731
732
{
int rc;
733
fhandle->cacheddata = allocator.Malloc(fhandle->filesize);
Jul 6, 2017
Jul 6, 2017
734
BAIL_IF(!fhandle->cacheddata, PHYSFS_ERR_OUT_OF_MEMORY, -1);
Jul 6, 2017
Jul 6, 2017
735
rc = iso_readimage(handle, fhandle->startblock * 2048,
Aug 21, 2010
Aug 21, 2010
736
fhandle->cacheddata, fhandle->filesize);
Jul 6, 2017
Jul 6, 2017
737
738
GOTO_IF_ERRPASS(rc < 0, freemem);
GOTO_IF(rc == 0, PHYSFS_ERR_CORRUPT, freemem);
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
fhandle->read = iso_file_read_mem;
fhandle->seek = iso_file_seek_mem;
fhandle->close = iso_file_close_mem;
return 0;
freemem:
allocator.Free(fhandle->cacheddata);
return -1;
} /* iso_file_open_mem */
static int iso_file_open_foreign(ISO9660Handle *handle,
ISO9660FileHandle *fhandle)
{
Aug 30, 2010
Aug 30, 2010
754
fhandle->io = __PHYSFS_createNativeIo(handle->path, 'r');
Jul 6, 2017
Jul 6, 2017
755
756
757
758
759
760
761
BAIL_IF_ERRPASS(!fhandle->io, -1);
if (!fhandle->io->seek(fhandle->io, fhandle->startblock * 2048))
{
fhandle->io->destroy(fhandle->io);
return -1;
} /* if */
762
763
764
765
766
767
768
769
fhandle->read = iso_file_read_foreign;
fhandle->seek = iso_file_seek_foreign;
fhandle->close = iso_file_close_foreign;
return 0;
} /* iso_file_open_foreign */
Nov 30, 2012
Nov 30, 2012
770
static PHYSFS_Io *ISO9660_openRead(void *opaque, const char *filename)
Aug 30, 2010
Aug 30, 2010
772
PHYSFS_Io *retval = NULL;
773
774
775
776
777
778
ISO9660Handle *handle = (ISO9660Handle*) opaque;
ISO9660FileHandle *fhandle;
ISO9660FileDescriptor descriptor;
int rc;
fhandle = allocator.Malloc(sizeof(ISO9660FileHandle));
Jul 6, 2017
Jul 6, 2017
779
BAIL_IF(fhandle == 0, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
780
781
fhandle->cacheddata = 0;
Aug 30, 2010
Aug 30, 2010
782
retval = allocator.Malloc(sizeof(PHYSFS_Io));
Jul 6, 2017
Jul 6, 2017
783
GOTO_IF(retval == 0, PHYSFS_ERR_OUT_OF_MEMORY, errorhandling);
Aug 30, 2010
Aug 30, 2010
784
785
/* find file descriptor */
Nov 30, 2012
Nov 30, 2012
786
rc = iso_find_dir_entry(handle, filename, &descriptor);
Jul 6, 2017
Jul 6, 2017
787
GOTO_IF_ERRPASS(rc, errorhandling);
788
789
790
791
792
793
fhandle->startblock = descriptor.extentpos + descriptor.extattributelen;
fhandle->filesize = descriptor.datalen;
fhandle->currpos = 0;
fhandle->isohandle = handle;
fhandle->cacheddata = NULL;
Aug 30, 2010
Aug 30, 2010
794
fhandle->io = NULL;
795
796
797
798
799
if (descriptor.datalen <= ISO9660_FULLCACHEMAXSIZE)
rc = iso_file_open_mem(handle, fhandle);
else
rc = iso_file_open_foreign(handle, fhandle);
Jul 6, 2017
Jul 6, 2017
800
GOTO_IF_ERRPASS(rc, errorhandling);
Aug 30, 2010
Aug 30, 2010
802
803
804
memcpy(retval, &ISO9660_Io, sizeof (PHYSFS_Io));
retval->opaque = fhandle;
return retval;
805
806
errorhandling:
Aug 30, 2010
Aug 30, 2010
807
808
if (retval) allocator.Free(retval);
if (fhandle) allocator.Free(fhandle);
809
810
811
812
813
814
815
816
817
return NULL;
} /* ISO9660_openRead */
/*******************************************************************************
* Information gathering functions
******************************************************************************/
Nov 28, 2012
Nov 28, 2012
818
static void ISO9660_enumerateFiles(void *opaque, const char *dname,
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
{
ISO9660Handle *handle = (ISO9660Handle*) opaque;
ISO9660FileDescriptor descriptor;
PHYSFS_uint64 readpos;
PHYSFS_uint64 end_of_dir;
char filename[130]; /* ISO allows 31, Joliet 128 -> 128 + 2 eol bytes */
int version = 0;
if (*dname == '\0')
{
readpos = handle->rootdirstart;
end_of_dir = readpos + handle->rootdirsize;
} /* if */
else
{
printf("pfad %s\n",dname);
Jul 6, 2017
Jul 6, 2017
837
838
BAIL_IF_ERRPASS(iso_find_dir_entry(handle,dname, &descriptor),);
BAIL_IF_ERRPASS(!descriptor.flags.directory,);
839
840
841
842
843
844
845
readpos = descriptor.extentpos * 2048;
end_of_dir = readpos + descriptor.datalen;
} /* else */
while (1)
{
Jul 6, 2017
Jul 6, 2017
846
BAIL_IF_ERRPASS(iso_readfiledescriptor(handle, readpos, &descriptor), );
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/* recordlen = 0 -> no more entries or fill entry */
if (!descriptor.recordlen)
{
/* if we are in the last sector of the directory & it's 0 -> end */
if ((end_of_dir - 2048) <= (readpos -1))
break; /* finished */
/* else skip to the next sector & continue; */
readpos = (((readpos - 1) / 2048) + 1) * 2048;
continue;
} /* if */
readpos += descriptor.recordlen;
if (descriptor.filenamelen == 1 && (descriptor.filename[0] == 0
|| descriptor.filename[0] == 1))
continue; /* special ones, ignore */
strncpy(filename,descriptor.filename,descriptor.filenamelen);
iso_extractfilename(handle, &descriptor, filename, &version);
cb(callbackdata, origdir,filename);
} /* while */
} /* ISO9660_enumerateFiles */
Nov 30, 2012
Nov 30, 2012
872
static int ISO9660_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
873
874
875
876
{
ISO9660Handle *handle = (ISO9660Handle*) opaque;
ISO9660FileDescriptor descriptor;
ISO9660ExtAttributeRec extattr;
Jul 6, 2017
Jul 6, 2017
877
BAIL_IF_ERRPASS(iso_find_dir_entry(handle, name, &descriptor), -1);
878
879
880
881
882
883
stat->readonly = 1;
/* try to get extended info */
if (descriptor.extattributelen)
{
Jul 6, 2017
Jul 6, 2017
884
885
BAIL_IF_ERRPASS(iso_read_ext_attributes(handle,
descriptor.extentpos, &extattr), -1);
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
stat->createtime = iso_volume_mktime(&extattr.create_time);
stat->modtime = iso_volume_mktime(&extattr.mod_time);
stat->accesstime = iso_volume_mktime(&extattr.mod_time);
} /* if */
else
{
stat->createtime = iso_mktime(&descriptor.recordtime);
stat->modtime = iso_mktime(&descriptor.recordtime);
stat->accesstime = iso_mktime(&descriptor.recordtime);
} /* else */
if (descriptor.flags.directory)
{
stat->filesize = 0;
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
} /* if */
else
{
stat->filesize = descriptor.datalen;
stat->filetype = PHYSFS_FILETYPE_REGULAR;
} /* else */
Aug 21, 2010
Aug 21, 2010
908
return 1;
909
910
911
912
913
914
915
} /* ISO9660_stat */
/*******************************************************************************
* Not supported functions
******************************************************************************/
Nov 28, 2012
Nov 28, 2012
916
static PHYSFS_Io *ISO9660_openWrite(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
918
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
919
920
921
} /* ISO9660_openWrite */
Nov 28, 2012
Nov 28, 2012
922
static PHYSFS_Io *ISO9660_openAppend(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
924
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
925
926
927
} /* ISO9660_openAppend */
Nov 28, 2012
Nov 28, 2012
928
static int ISO9660_remove(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
930
BAIL(PHYSFS_ERR_READ_ONLY, 0);
931
932
933
} /* ISO9660_remove */
Nov 28, 2012
Nov 28, 2012
934
static int ISO9660_mkdir(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
936
BAIL(PHYSFS_ERR_READ_ONLY, 0);
937
938
939
940
941
} /* ISO9660_mkdir */
const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660 =
{
Nov 28, 2012
Nov 28, 2012
942
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
Mar 25, 2012
Mar 25, 2012
943
944
945
946
{
"ISO",
"ISO9660 image file",
"Christoph Nelles <evilazrael@evilazrael.de>",
Feb 25, 2016
Feb 25, 2016
947
"https://www.evilazrael.de/",
Nov 30, 2012
Nov 30, 2012
948
0, /* supportsSymlinks */
Mar 25, 2012
Mar 25, 2012
949
},
Nov 30, 2012
Nov 30, 2012
950
951
952
953
954
955
956
ISO9660_openArchive,
ISO9660_enumerateFiles,
ISO9660_openRead,
ISO9660_openWrite,
ISO9660_openAppend,
ISO9660_remove,
ISO9660_mkdir,
Nov 30, 2012
Nov 30, 2012
957
958
ISO9660_stat,
ISO9660_closeArchive
Mar 17, 2010
Mar 17, 2010
961
962
#endif /* defined PHYSFS_SUPPORTS_ISO9660 */
963
/* end of archiver_iso9660.c ... */