archiver_iso9660.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * ISO9660 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Christoph Nelles.
  7. */
  8. /* !!! FIXME: this file needs Ryanification. */
  9. /*
  10. * Handles CD-ROM disk images (and raw CD-ROM devices).
  11. *
  12. * Not supported:
  13. * - RockRidge
  14. * - Non 2048 Sectors
  15. * - UDF
  16. *
  17. * Deviations from the standard
  18. * - Ignores mandatory sort order
  19. * - Allows various invalid file names
  20. *
  21. * Problems
  22. * - Ambiguities in the standard
  23. */
  24. #define __PHYSICSFS_INTERNAL__
  25. #include "physfs_internal.h"
  26. #if PHYSFS_SUPPORTS_ISO9660
  27. #include <time.h>
  28. /* cache files smaller than this completely in memory */
  29. #define ISO9660_FULLCACHEMAXSIZE 2048
  30. /* !!! FIXME: this is going to cause trouble. */
  31. #pragma pack(push) /* push current alignment to stack */
  32. #pragma pack(1) /* set alignment to 1 byte boundary */
  33. /* This is the format as defined by the standard
  34. typedef struct
  35. {
  36. PHYSFS_uint32 lsb;
  37. PHYSFS_uint32 msb;
  38. } ISOBB32bit; // 32byte Both Byte type, means the value first in LSB then in MSB
  39. typedef struct
  40. {
  41. PHYSFS_uint16 lsb;
  42. PHYSFS_uint16 msb;
  43. } ISOBB16bit; // 16byte Both Byte type, means the value first in LSB then in MSB
  44. */
  45. /* define better ones to simplify coding (less if's) */
  46. #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
  47. #define ISOBB32bit(name) PHYSFS_uint32 name; PHYSFS_uint32 __dummy_##name;
  48. #define ISOBB16bit(name) PHYSFS_uint16 name; PHYSFS_uint16 __dummy_##name;
  49. #else
  50. #define ISOBB32bit(name) PHYSFS_uint32 __dummy_##name; PHYSFS_uint32 name;
  51. #define ISOBB16bit(name) PHYSFS_uint16 __dummy_##name; PHYSFS_uint16 name;
  52. #endif
  53. typedef struct
  54. {
  55. char year[4];
  56. char month[2];
  57. char day[2];
  58. char hour[2];
  59. char minute[2];
  60. char second[2];
  61. char centisec[2];
  62. PHYSFS_sint8 offset; /* in 15min from GMT */
  63. } ISO9660VolumeTimestamp;
  64. typedef struct
  65. {
  66. PHYSFS_uint8 year;
  67. PHYSFS_uint8 month;
  68. PHYSFS_uint8 day;
  69. PHYSFS_uint8 hour;
  70. PHYSFS_uint8 minute;
  71. PHYSFS_uint8 second;
  72. PHYSFS_sint8 offset;
  73. } ISO9660FileTimestamp;
  74. typedef struct
  75. {
  76. unsigned existence:1;
  77. unsigned directory:1;
  78. unsigned associated_file:1;
  79. unsigned record:1;
  80. unsigned protection:1;
  81. unsigned reserved:2;
  82. unsigned multiextent:1;
  83. } ISO9660FileFlags;
  84. typedef struct
  85. {
  86. PHYSFS_uint8 length;
  87. PHYSFS_uint8 attribute_length;
  88. ISOBB32bit(extent_location)
  89. ISOBB32bit(data_length)
  90. ISO9660FileTimestamp timestamp;
  91. ISO9660FileFlags file_flags;
  92. PHYSFS_uint8 file_unit_size;
  93. PHYSFS_uint8 gap_size;
  94. ISOBB16bit(vol_seq_no)
  95. PHYSFS_uint8 len_fi;
  96. char unused;
  97. } ISO9660RootDirectoryRecord;
  98. /* this structure is combined for all Volume descriptor types */
  99. typedef struct
  100. {
  101. PHYSFS_uint8 type;
  102. char identifier[5];
  103. PHYSFS_uint8 version;
  104. PHYSFS_uint8 flags;
  105. char system_identifier[32];
  106. char volume_identifier[32];
  107. char unused2[8];
  108. ISOBB32bit(space_size)
  109. PHYSFS_uint8 escape_sequences[32];
  110. ISOBB16bit(vol_set_size)
  111. ISOBB16bit(vol_seq_no)
  112. ISOBB16bit(block_size)
  113. ISOBB32bit(path_table_size)
  114. /* PHYSFS_uint32 path_table_start_lsb; // why didn't they use both byte type?
  115. PHYSFS_uint32 opt_path_table_start_lsb;
  116. PHYSFS_uint32 path_table_start_msb;
  117. PHYSFS_uint32 opt_path_table_start_msb;*/
  118. #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
  119. PHYSFS_uint32 path_table_start;
  120. PHYSFS_uint32 opt_path_table_start;
  121. PHYSFS_uint32 unused6;
  122. PHYSFS_uint32 unused7;
  123. #else
  124. PHYSFS_uint32 unused6;
  125. PHYSFS_uint32 unused7;
  126. PHYSFS_uint32 path_table_start;
  127. PHYSFS_uint32 opt_path_table_start;
  128. #endif
  129. ISO9660RootDirectoryRecord rootdirectory;
  130. char set_identifier[128];
  131. char publisher_identifier[128];
  132. char preparer_identifer[128];
  133. char application_identifier[128];
  134. char copyright_file_identifier[37];
  135. char abstract_file_identifier[37];
  136. char bibliographic_file_identifier[37];
  137. ISO9660VolumeTimestamp creation_timestamp;
  138. ISO9660VolumeTimestamp modification_timestamp;
  139. ISO9660VolumeTimestamp expiration_timestamp;
  140. ISO9660VolumeTimestamp effective_timestamp;
  141. PHYSFS_uint8 file_structure_version;
  142. char unused4;
  143. char application_use[512];
  144. char unused5[653];
  145. } ISO9660VolumeDescriptor;
  146. typedef struct
  147. {
  148. PHYSFS_uint8 recordlen;
  149. PHYSFS_uint8 extattributelen;
  150. ISOBB32bit(extentpos)
  151. ISOBB32bit(datalen)
  152. ISO9660FileTimestamp recordtime;
  153. ISO9660FileFlags flags;
  154. PHYSFS_uint8 file_unit_size;
  155. PHYSFS_uint8 interleave_gap;
  156. ISOBB16bit(volseqno)
  157. PHYSFS_uint8 filenamelen;
  158. char filename[222]; /* This is not exact, but makes reading easier */
  159. } ISO9660FileDescriptor;
  160. typedef struct
  161. {
  162. ISOBB16bit(owner)
  163. ISOBB16bit(group)
  164. PHYSFS_uint16 flags; /* not implemented*/
  165. ISO9660VolumeTimestamp create_time; /* yes, not file timestamp */
  166. ISO9660VolumeTimestamp mod_time;
  167. ISO9660VolumeTimestamp expire_time;
  168. ISO9660VolumeTimestamp effective_time;
  169. PHYSFS_uint8 record_format;
  170. PHYSFS_uint8 record_attributes;
  171. ISOBB16bit(record_len)
  172. char system_identifier[32];
  173. char system_use[64];
  174. PHYSFS_uint8 version;
  175. ISOBB16bit(escape_len)
  176. char reserved[64];
  177. /** further fields not implemented */
  178. } ISO9660ExtAttributeRec;
  179. #pragma pack(pop) /* restore original alignment from stack */
  180. typedef struct
  181. {
  182. PHYSFS_Io *io;
  183. PHYSFS_uint32 rootdirstart;
  184. PHYSFS_uint32 rootdirsize;
  185. PHYSFS_uint64 currpos;
  186. int isjoliet;
  187. char *path;
  188. void *mutex;
  189. } ISO9660Handle;
  190. typedef struct __ISO9660FileHandle
  191. {
  192. PHYSFS_sint64 filesize;
  193. PHYSFS_uint64 currpos;
  194. PHYSFS_uint64 startblock;
  195. ISO9660Handle *isohandle;
  196. PHYSFS_uint32 (*read) (struct __ISO9660FileHandle *filehandle, void *buffer,
  197. PHYSFS_uint64 len);
  198. int (*seek)(struct __ISO9660FileHandle *filehandle, PHYSFS_sint64 offset);
  199. void (*close)(struct __ISO9660FileHandle *filehandle);
  200. /* !!! FIXME: anonymouse union is going to cause problems. */
  201. union
  202. {
  203. /* !!! FIXME: just use a memory PHYSFS_Io here, unify all this code. */
  204. char *cacheddata; /* data of file when cached */
  205. PHYSFS_Io *io; /* handle to separate opened file */
  206. };
  207. } ISO9660FileHandle;
  208. /*******************************************************************************
  209. * Time conversion functions
  210. ******************************************************************************/
  211. static PHYSFS_sint64 iso_mktime(ISO9660FileTimestamp *timestamp)
  212. {
  213. struct tm tm;
  214. tm.tm_year = timestamp->year;
  215. tm.tm_mon = timestamp->month - 1;
  216. tm.tm_mday = timestamp->day;
  217. tm.tm_hour = timestamp->hour;
  218. tm.tm_min = timestamp->minute;
  219. tm.tm_sec = timestamp->second;
  220. /* Ignore GMT offset for now... */
  221. return mktime(&tm);
  222. } /* iso_mktime */
  223. static int iso_atoi2(char *text)
  224. {
  225. return ((text[0] - 40) * 10) + (text[1] - 40);
  226. } /* iso_atoi2 */
  227. static int iso_atoi4(char *text)
  228. {
  229. return ((text[0] - 40) * 1000) + ((text[1] - 40) * 100) +
  230. ((text[2] - 40) * 10) + (text[3] - 40);
  231. } /* iso_atoi4 */
  232. static PHYSFS_sint64 iso_volume_mktime(ISO9660VolumeTimestamp *timestamp)
  233. {
  234. struct tm tm;
  235. tm.tm_year = iso_atoi4(timestamp->year);
  236. tm.tm_mon = iso_atoi2(timestamp->month) - 1;
  237. tm.tm_mday = iso_atoi2(timestamp->day);
  238. tm.tm_hour = iso_atoi2(timestamp->hour);
  239. tm.tm_min = iso_atoi2(timestamp->minute);
  240. tm.tm_sec = iso_atoi2(timestamp->second);
  241. /* this allows values outside the range of a unix timestamp... sanitize them */
  242. PHYSFS_sint64 value = mktime(&tm);
  243. return value == -1 ? 0 : value;
  244. } /* iso_volume_mktime */
  245. /*******************************************************************************
  246. * Filename extraction
  247. ******************************************************************************/
  248. static int iso_extractfilenameISO(ISO9660FileDescriptor *descriptor,
  249. char *filename, int *version)
  250. {
  251. *filename = '\0';
  252. if (descriptor->flags.directory)
  253. {
  254. strncpy(filename, descriptor->filename, descriptor->filenamelen);
  255. filename[descriptor->filenamelen] = '\0';
  256. *version = 0;
  257. } /* if */
  258. else
  259. {
  260. /* find last SEPARATOR2 */
  261. int pos = 0;
  262. int lastfound = -1;
  263. for(;pos < descriptor->filenamelen; pos++)
  264. if (descriptor->filename[pos] == ';')
  265. lastfound = pos;
  266. BAIL_IF_MACRO(lastfound < 1, PHYSFS_ERR_NOT_FOUND /* !!! FIXME: PHYSFS_ERR_BAD_FILENAME */, -1);
  267. BAIL_IF_MACRO(lastfound == (descriptor->filenamelen -1), PHYSFS_ERR_NOT_FOUND /* !!! PHYSFS_ERR_BAD_FILENAME */, -1);
  268. strncpy(filename, descriptor->filename, lastfound);
  269. if (filename[lastfound - 1] == '.')
  270. filename[lastfound - 1] = '\0'; /* consume trailing ., as done in all implementations */
  271. else
  272. filename[lastfound] = '\0';
  273. *version = atoi(descriptor->filename + lastfound);
  274. } /* else */
  275. return 0;
  276. } /* iso_extractfilenameISO */
  277. static int iso_extractfilenameUCS2(ISO9660FileDescriptor *descriptor,
  278. char *filename, int *version)
  279. {
  280. PHYSFS_uint16 tmp[128];
  281. PHYSFS_uint16 *src;
  282. int len;
  283. *filename = '\0';
  284. *version = 1; /* Joliet does not have versions.. at least not on my images */
  285. src = (PHYSFS_uint16*) descriptor->filename;
  286. len = descriptor->filenamelen / 2;
  287. tmp[len] = 0;
  288. while(len--)
  289. tmp[len] = PHYSFS_swapUBE16(src[len]);
  290. PHYSFS_utf8FromUcs2(tmp, filename, 255);
  291. return 0;
  292. } /* iso_extractfilenameUCS2 */
  293. static int iso_extractfilename(ISO9660Handle *handle,
  294. ISO9660FileDescriptor *descriptor, char *filename,int *version)
  295. {
  296. if (handle->isjoliet)
  297. return iso_extractfilenameUCS2(descriptor, filename, version);
  298. else
  299. return iso_extractfilenameISO(descriptor, filename, version);
  300. } /* iso_extractfilename */
  301. /*******************************************************************************
  302. * Basic image read functions
  303. ******************************************************************************/
  304. static int iso_readimage(ISO9660Handle *handle, PHYSFS_uint64 where,
  305. void *buffer, PHYSFS_uint64 len)
  306. {
  307. BAIL_IF_MACRO(!__PHYSFS_platformGrabMutex(handle->mutex), ERRPASS, -1);
  308. int rc = -1;
  309. if (where != handle->currpos)
  310. GOTO_IF_MACRO(!handle->io->seek(handle->io,where), ERRPASS, unlockme);
  311. rc = handle->io->read(handle->io, buffer, len);
  312. if (rc == -1)
  313. {
  314. handle->currpos = (PHYSFS_uint64) -1;
  315. goto unlockme;
  316. } /* if */
  317. handle->currpos += rc;
  318. unlockme:
  319. __PHYSFS_platformReleaseMutex(handle->mutex);
  320. return rc;
  321. } /* iso_readimage */
  322. static PHYSFS_sint64 iso_readfiledescriptor(ISO9660Handle *handle,
  323. PHYSFS_uint64 where,
  324. ISO9660FileDescriptor *descriptor)
  325. {
  326. PHYSFS_sint64 rc = iso_readimage(handle, where, descriptor,
  327. sizeof (descriptor->recordlen));
  328. BAIL_IF_MACRO(rc == -1, ERRPASS, -1);
  329. BAIL_IF_MACRO(rc != 1, PHYSFS_ERR_CORRUPT, -1);
  330. if (descriptor->recordlen == 0)
  331. return 0; /* fill bytes at the end of a sector */
  332. rc = iso_readimage(handle, where + 1, &descriptor->extattributelen,
  333. descriptor->recordlen - sizeof(descriptor->recordlen));
  334. BAIL_IF_MACRO(rc == -1, ERRPASS, -1);
  335. BAIL_IF_MACRO(rc != 1, PHYSFS_ERR_CORRUPT, -1);
  336. return 0;
  337. } /* iso_readfiledescriptor */
  338. static void iso_extractsubpath(char *path, char **subpath)
  339. {
  340. *subpath = strchr(path,'/');
  341. if (*subpath != 0)
  342. {
  343. **subpath = 0;
  344. *subpath +=1;
  345. } /* if */
  346. } /* iso_extractsubpath */
  347. /*
  348. * Don't use path tables, they are not necessarily faster, but more complicated
  349. * to implement as they store only directories and not files, so searching for
  350. * a file needs to branch to the directory extent sooner or later.
  351. */
  352. static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,
  353. ISO9660FileDescriptor *descriptor)
  354. {
  355. char *subpath = 0;
  356. PHYSFS_uint64 readpos, end_of_dir;
  357. char filename[255];
  358. char pathcopy[256];
  359. char *mypath;
  360. int version = 0;
  361. strcpy(pathcopy, path);
  362. mypath = pathcopy;
  363. readpos = handle->rootdirstart;
  364. end_of_dir = handle->rootdirstart + handle->rootdirsize;
  365. iso_extractsubpath(mypath, &subpath);
  366. while (1)
  367. {
  368. BAIL_IF_MACRO(iso_readfiledescriptor(handle, readpos, descriptor), ERRPASS, -1);
  369. /* recordlen = 0 -> no more entries or fill entry */
  370. if (!descriptor->recordlen)
  371. {
  372. /* if we are in the last sector of the directory & it's 0 -> end */
  373. if ((end_of_dir - 2048) <= (readpos -1))
  374. break; /* finished */
  375. /* else skip to the next sector & continue; */
  376. readpos = (((readpos - 1) / 2048) + 1) * 2048;
  377. continue;
  378. } /* if */
  379. readpos += descriptor->recordlen;
  380. if (descriptor->filenamelen == 1 && (descriptor->filename[0] == 0
  381. || descriptor->filename[0] == 1))
  382. continue; /* special ones, ignore */
  383. BAIL_IF_MACRO(
  384. iso_extractfilename(handle, descriptor, filename, &version),
  385. ERRPASS, -1);
  386. if (strcmp(filename, mypath) == 0)
  387. {
  388. if ( (subpath == 0) || (subpath[0] == 0) )
  389. return 0; /* no subpaths left and we found the entry */
  390. if (descriptor->flags.directory)
  391. {
  392. /* shorten the path to the subpath */
  393. mypath = subpath;
  394. iso_extractsubpath(mypath, &subpath);
  395. /* gosub to the new directory extent */
  396. readpos = descriptor->extentpos * 2048;
  397. end_of_dir = readpos + descriptor->datalen;
  398. } /* if */
  399. else
  400. {
  401. /* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
  402. /* we're at a file but have a remaining subpath -> no match */
  403. return 0;
  404. } /* else */
  405. } /* if */
  406. } /* while */
  407. /* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
  408. return 0;
  409. } /* iso_find_dir_entry */
  410. static int iso_read_ext_attributes(ISO9660Handle *handle, int block,
  411. ISO9660ExtAttributeRec *attributes)
  412. {
  413. return iso_readimage(handle, block * 2048, attributes,
  414. sizeof(ISO9660ExtAttributeRec));
  415. } /* iso_read_ext_attributes */
  416. static int ISO9660_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
  417. static PHYSFS_Io *ISO9660_duplicate(PHYSFS_Io *_io)
  418. {
  419. BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL); /* !!! FIXME: write me. */
  420. } /* ISO9660_duplicate */
  421. static void ISO9660_destroy(PHYSFS_Io *io)
  422. {
  423. ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
  424. fhandle->close(fhandle);
  425. allocator.Free(io);
  426. } /* ISO9660_destroy */
  427. static PHYSFS_sint64 ISO9660_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  428. {
  429. ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
  430. return fhandle->read(fhandle, buf, len);
  431. } /* ISO9660_read */
  432. static PHYSFS_sint64 ISO9660_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 l)
  433. {
  434. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
  435. } /* ISO9660_write */
  436. static PHYSFS_sint64 ISO9660_tell(PHYSFS_Io *io)
  437. {
  438. return ((ISO9660FileHandle*) io->opaque)->currpos;
  439. } /* ISO9660_tell */
  440. static int ISO9660_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  441. {
  442. ISO9660FileHandle *fhandle = (ISO9660FileHandle*) io->opaque;
  443. return fhandle->seek(fhandle, offset);
  444. } /* ISO9660_seek */
  445. static PHYSFS_sint64 ISO9660_length(PHYSFS_Io *io)
  446. {
  447. return ((ISO9660FileHandle*) io->opaque)->filesize;
  448. } /* ISO9660_length */
  449. static const PHYSFS_Io ISO9660_Io =
  450. {
  451. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  452. ISO9660_read,
  453. ISO9660_write,
  454. ISO9660_seek,
  455. ISO9660_tell,
  456. ISO9660_length,
  457. ISO9660_duplicate,
  458. ISO9660_flush,
  459. ISO9660_destroy
  460. };
  461. /*******************************************************************************
  462. * Archive management functions
  463. ******************************************************************************/
  464. static void *ISO9660_openArchive(PHYSFS_Io *io, const char *filename, int forWriting)
  465. {
  466. char magicnumber[6];
  467. ISO9660Handle *handle;
  468. int founddescriptor = 0;
  469. int foundjoliet = 0;
  470. assert(io != NULL); /* shouldn't ever happen. */
  471. BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  472. /* Skip system area to magic number in Volume descriptor */
  473. BAIL_IF_MACRO(!io->seek(io, 32769), ERRPASS, NULL);
  474. BAIL_IF_MACRO(!io->read(io, magicnumber, 5) != 5, ERRPASS, NULL);
  475. if (memcmp(magicnumber, "CD001", 6) != 0)
  476. BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
  477. handle = allocator.Malloc(sizeof(ISO9660Handle));
  478. GOTO_IF_MACRO(!handle, PHYSFS_ERR_OUT_OF_MEMORY, errorcleanup);
  479. handle->path = 0;
  480. handle->mutex= 0;
  481. handle->io = NULL;
  482. handle->path = allocator.Malloc(strlen(filename) + 1);
  483. GOTO_IF_MACRO(!handle->path, PHYSFS_ERR_OUT_OF_MEMORY, errorcleanup);
  484. strcpy(handle->path, filename);
  485. handle->mutex = __PHYSFS_platformCreateMutex();
  486. GOTO_IF_MACRO(!handle->mutex, ERRPASS, errorcleanup);
  487. handle->io = io;
  488. /* seek Primary Volume Descriptor */
  489. GOTO_IF_MACRO(!io->seek(io, 32768), PHYSFS_ERR_IO, errorcleanup);
  490. while (1)
  491. {
  492. ISO9660VolumeDescriptor descriptor;
  493. GOTO_IF_MACRO(io->read(io, &descriptor, sizeof(ISO9660VolumeDescriptor)) != sizeof(ISO9660VolumeDescriptor), PHYSFS_ERR_IO, errorcleanup);
  494. GOTO_IF_MACRO(strncmp(descriptor.identifier, "CD001", 5) != 0, PHYSFS_ERR_UNSUPPORTED, errorcleanup);
  495. if (descriptor.type == 255)
  496. {
  497. /* type 255 terminates the volume descriptor list */
  498. if (founddescriptor)
  499. return handle; /* ok, we've found one volume descriptor */
  500. else
  501. GOTO_MACRO(PHYSFS_ERR_CORRUPT, errorcleanup);
  502. } /* if */
  503. if (descriptor.type == 1 && !founddescriptor)
  504. {
  505. handle->currpos = io->tell(io);
  506. handle->rootdirstart =
  507. descriptor.rootdirectory.extent_location * 2048;
  508. handle->rootdirsize =
  509. descriptor.rootdirectory.data_length;
  510. handle->isjoliet = 0;
  511. founddescriptor = 1; /* continue search for joliet */
  512. } /* if */
  513. if (descriptor.type == 2 && !foundjoliet)
  514. {
  515. /* check if is joliet */
  516. PHYSFS_uint8 *s = descriptor.escape_sequences;
  517. int joliet = !(descriptor.flags & 1)
  518. && (s[0] == 0x25)
  519. && (s[1] == 0x2F)
  520. && ((s[2] == 0x40) || (s[2] == 0x43) || (s[2] == 0x45));
  521. if (!joliet)
  522. continue;
  523. handle->currpos = io->tell(io);
  524. handle->rootdirstart =
  525. descriptor.rootdirectory.extent_location * 2048;
  526. handle->rootdirsize =
  527. descriptor.rootdirectory.data_length;
  528. handle->isjoliet = 1;
  529. founddescriptor = 1;
  530. foundjoliet = 1;
  531. } /* if */
  532. } /* while */
  533. GOTO_MACRO(PHYSFS_ERR_CORRUPT, errorcleanup); /* not found. */
  534. errorcleanup:
  535. if (handle)
  536. {
  537. if (handle->path)
  538. allocator.Free(handle->path);
  539. if (handle->mutex)
  540. __PHYSFS_platformDestroyMutex(handle->mutex);
  541. allocator.Free(handle);
  542. } /* if */
  543. return NULL;
  544. } /* ISO9660_openArchive */
  545. static void ISO9660_closeArchive(void *opaque)
  546. {
  547. ISO9660Handle *handle = (ISO9660Handle*) opaque;
  548. handle->io->destroy(handle->io);
  549. __PHYSFS_platformDestroyMutex(handle->mutex);
  550. allocator.Free(handle->path);
  551. allocator.Free(handle);
  552. } /* ISO9660_closeArchive */
  553. /*******************************************************************************
  554. * Read functions
  555. ******************************************************************************/
  556. static PHYSFS_uint32 iso_file_read_mem(ISO9660FileHandle *filehandle,
  557. void *buffer, PHYSFS_uint64 len)
  558. {
  559. /* check remaining bytes & max obj which can be fetched */
  560. const PHYSFS_sint64 bytesleft = filehandle->filesize - filehandle->currpos;
  561. if (bytesleft < len)
  562. len = bytesleft;
  563. if (len == 0)
  564. return 0;
  565. memcpy(buffer, filehandle->cacheddata + filehandle->currpos, (size_t) len);
  566. filehandle->currpos += len;
  567. return (PHYSFS_uint32) len;
  568. } /* iso_file_read_mem */
  569. static int iso_file_seek_mem(ISO9660FileHandle *fhandle, PHYSFS_sint64 offset)
  570. {
  571. BAIL_IF_MACRO(offset < 0, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  572. BAIL_IF_MACRO(offset >= fhandle->filesize, PHYSFS_ERR_PAST_EOF, 0);
  573. fhandle->currpos = offset;
  574. return 0;
  575. } /* iso_file_seek_mem */
  576. static void iso_file_close_mem(ISO9660FileHandle *fhandle)
  577. {
  578. allocator.Free(fhandle->cacheddata);
  579. allocator.Free(fhandle);
  580. } /* iso_file_close_mem */
  581. static PHYSFS_uint32 iso_file_read_foreign(ISO9660FileHandle *filehandle,
  582. void *buffer, PHYSFS_uint64 len)
  583. {
  584. /* check remaining bytes & max obj which can be fetched */
  585. const PHYSFS_sint64 bytesleft = filehandle->filesize - filehandle->currpos;
  586. if (bytesleft < len)
  587. len = bytesleft;
  588. const PHYSFS_sint64 rc = filehandle->io->read(filehandle->io, buffer, len);
  589. BAIL_IF_MACRO(rc == -1, ERRPASS, -1);
  590. filehandle->currpos += rc; /* i trust my internal book keeping */
  591. BAIL_IF_MACRO(rc < len, PHYSFS_ERR_CORRUPT, -1);
  592. return rc;
  593. } /* iso_file_read_foreign */
  594. static int iso_file_seek_foreign(ISO9660FileHandle *fhandle,
  595. PHYSFS_sint64 offset)
  596. {
  597. BAIL_IF_MACRO(offset < 0, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  598. BAIL_IF_MACRO(offset >= fhandle->filesize, PHYSFS_ERR_PAST_EOF, 0);
  599. PHYSFS_sint64 pos = fhandle->startblock * 2048 + offset;
  600. BAIL_IF_MACRO(!fhandle->io->seek(fhandle->io, pos), ERRPASS, -1);
  601. fhandle->currpos = offset;
  602. return 0;
  603. } /* iso_file_seek_foreign */
  604. static void iso_file_close_foreign(ISO9660FileHandle *fhandle)
  605. {
  606. fhandle->io->destroy(fhandle->io);
  607. allocator.Free(fhandle);
  608. } /* iso_file_close_foreign */
  609. static int iso_file_open_mem(ISO9660Handle *handle, ISO9660FileHandle *fhandle)
  610. {
  611. fhandle->cacheddata = allocator.Malloc(fhandle->filesize);
  612. BAIL_IF_MACRO(!fhandle->cacheddata, PHYSFS_ERR_OUT_OF_MEMORY, -1);
  613. int rc = iso_readimage(handle, fhandle->startblock * 2048,
  614. fhandle->cacheddata, fhandle->filesize);
  615. GOTO_IF_MACRO(rc < 0, ERRPASS, freemem);
  616. GOTO_IF_MACRO(rc == 0, PHYSFS_ERR_CORRUPT, freemem);
  617. fhandle->read = iso_file_read_mem;
  618. fhandle->seek = iso_file_seek_mem;
  619. fhandle->close = iso_file_close_mem;
  620. return 0;
  621. freemem:
  622. allocator.Free(fhandle->cacheddata);
  623. return -1;
  624. } /* iso_file_open_mem */
  625. static int iso_file_open_foreign(ISO9660Handle *handle,
  626. ISO9660FileHandle *fhandle)
  627. {
  628. int rc;
  629. fhandle->io = __PHYSFS_createNativeIo(handle->path, 'r');
  630. BAIL_IF_MACRO(!fhandle->io, ERRPASS, -1);
  631. rc = fhandle->io->seek(fhandle->io, fhandle->startblock * 2048);
  632. GOTO_IF_MACRO(!rc, ERRPASS, closefile);
  633. fhandle->read = iso_file_read_foreign;
  634. fhandle->seek = iso_file_seek_foreign;
  635. fhandle->close = iso_file_close_foreign;
  636. return 0;
  637. closefile:
  638. fhandle->io->destroy(fhandle->io);
  639. return -1;
  640. } /* iso_file_open_foreign */
  641. static PHYSFS_Io *ISO9660_openRead(void *opaque, const char *filename)
  642. {
  643. PHYSFS_Io *retval = NULL;
  644. ISO9660Handle *handle = (ISO9660Handle*) opaque;
  645. ISO9660FileHandle *fhandle;
  646. ISO9660FileDescriptor descriptor;
  647. int rc;
  648. fhandle = allocator.Malloc(sizeof(ISO9660FileHandle));
  649. BAIL_IF_MACRO(fhandle == 0, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  650. fhandle->cacheddata = 0;
  651. retval = allocator.Malloc(sizeof(PHYSFS_Io));
  652. GOTO_IF_MACRO(retval == 0, PHYSFS_ERR_OUT_OF_MEMORY, errorhandling);
  653. /* find file descriptor */
  654. rc = iso_find_dir_entry(handle, filename, &descriptor);
  655. GOTO_IF_MACRO(rc, ERRPASS, errorhandling);
  656. fhandle->startblock = descriptor.extentpos + descriptor.extattributelen;
  657. fhandle->filesize = descriptor.datalen;
  658. fhandle->currpos = 0;
  659. fhandle->isohandle = handle;
  660. fhandle->cacheddata = NULL;
  661. fhandle->io = NULL;
  662. if (descriptor.datalen <= ISO9660_FULLCACHEMAXSIZE)
  663. rc = iso_file_open_mem(handle, fhandle);
  664. else
  665. rc = iso_file_open_foreign(handle, fhandle);
  666. GOTO_IF_MACRO(rc, ERRPASS, errorhandling);
  667. memcpy(retval, &ISO9660_Io, sizeof (PHYSFS_Io));
  668. retval->opaque = fhandle;
  669. return retval;
  670. errorhandling:
  671. if (retval) allocator.Free(retval);
  672. if (fhandle) allocator.Free(fhandle);
  673. return NULL;
  674. } /* ISO9660_openRead */
  675. /*******************************************************************************
  676. * Information gathering functions
  677. ******************************************************************************/
  678. static void ISO9660_enumerateFiles(void *opaque, const char *dname,
  679. PHYSFS_EnumFilesCallback cb,
  680. const char *origdir, void *callbackdata)
  681. {
  682. ISO9660Handle *handle = (ISO9660Handle*) opaque;
  683. ISO9660FileDescriptor descriptor;
  684. PHYSFS_uint64 readpos;
  685. PHYSFS_uint64 end_of_dir;
  686. char filename[130]; /* ISO allows 31, Joliet 128 -> 128 + 2 eol bytes */
  687. int version = 0;
  688. if (*dname == '\0')
  689. {
  690. readpos = handle->rootdirstart;
  691. end_of_dir = readpos + handle->rootdirsize;
  692. } /* if */
  693. else
  694. {
  695. printf("pfad %s\n",dname);
  696. BAIL_IF_MACRO(iso_find_dir_entry(handle,dname, &descriptor), ERRPASS,);
  697. BAIL_IF_MACRO(!descriptor.flags.directory, ERRPASS,);
  698. readpos = descriptor.extentpos * 2048;
  699. end_of_dir = readpos + descriptor.datalen;
  700. } /* else */
  701. while (1)
  702. {
  703. BAIL_IF_MACRO(iso_readfiledescriptor(handle, readpos, &descriptor), ERRPASS, );
  704. /* recordlen = 0 -> no more entries or fill entry */
  705. if (!descriptor.recordlen)
  706. {
  707. /* if we are in the last sector of the directory & it's 0 -> end */
  708. if ((end_of_dir - 2048) <= (readpos -1))
  709. break; /* finished */
  710. /* else skip to the next sector & continue; */
  711. readpos = (((readpos - 1) / 2048) + 1) * 2048;
  712. continue;
  713. } /* if */
  714. readpos += descriptor.recordlen;
  715. if (descriptor.filenamelen == 1 && (descriptor.filename[0] == 0
  716. || descriptor.filename[0] == 1))
  717. continue; /* special ones, ignore */
  718. strncpy(filename,descriptor.filename,descriptor.filenamelen);
  719. iso_extractfilename(handle, &descriptor, filename, &version);
  720. cb(callbackdata, origdir,filename);
  721. } /* while */
  722. } /* ISO9660_enumerateFiles */
  723. static int ISO9660_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
  724. {
  725. ISO9660Handle *handle = (ISO9660Handle*) opaque;
  726. ISO9660FileDescriptor descriptor;
  727. ISO9660ExtAttributeRec extattr;
  728. BAIL_IF_MACRO(iso_find_dir_entry(handle, name, &descriptor), ERRPASS, -1);
  729. stat->readonly = 1;
  730. /* try to get extended info */
  731. if (descriptor.extattributelen)
  732. {
  733. BAIL_IF_MACRO(iso_read_ext_attributes(handle,
  734. descriptor.extentpos, &extattr), ERRPASS, -1);
  735. stat->createtime = iso_volume_mktime(&extattr.create_time);
  736. stat->modtime = iso_volume_mktime(&extattr.mod_time);
  737. stat->accesstime = iso_volume_mktime(&extattr.mod_time);
  738. } /* if */
  739. else
  740. {
  741. stat->createtime = iso_mktime(&descriptor.recordtime);
  742. stat->modtime = iso_mktime(&descriptor.recordtime);
  743. stat->accesstime = iso_mktime(&descriptor.recordtime);
  744. } /* else */
  745. if (descriptor.flags.directory)
  746. {
  747. stat->filesize = 0;
  748. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  749. } /* if */
  750. else
  751. {
  752. stat->filesize = descriptor.datalen;
  753. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  754. } /* else */
  755. return 1;
  756. } /* ISO9660_stat */
  757. /*******************************************************************************
  758. * Not supported functions
  759. ******************************************************************************/
  760. static PHYSFS_Io *ISO9660_openWrite(void *opaque, const char *name)
  761. {
  762. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
  763. } /* ISO9660_openWrite */
  764. static PHYSFS_Io *ISO9660_openAppend(void *opaque, const char *name)
  765. {
  766. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
  767. } /* ISO9660_openAppend */
  768. static int ISO9660_remove(void *opaque, const char *name)
  769. {
  770. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
  771. } /* ISO9660_remove */
  772. static int ISO9660_mkdir(void *opaque, const char *name)
  773. {
  774. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
  775. } /* ISO9660_mkdir */
  776. const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660 =
  777. {
  778. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  779. {
  780. "ISO",
  781. "ISO9660 image file",
  782. "Christoph Nelles <evilazrael@evilazrael.de>",
  783. "http://www.evilazrael.de/",
  784. 0, /* supportsSymlinks */
  785. },
  786. ISO9660_openArchive,
  787. ISO9660_enumerateFiles,
  788. ISO9660_openRead,
  789. ISO9660_openWrite,
  790. ISO9660_openAppend,
  791. ISO9660_remove,
  792. ISO9660_mkdir,
  793. ISO9660_stat,
  794. ISO9660_closeArchive
  795. };
  796. #endif /* defined PHYSFS_SUPPORTS_ISO9660 */
  797. /* end of archiver_iso9660.c ... */