🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
14 KiB

  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 originally written by Christoph Nelles, but was largely
  7. * rewritten by Ryan C. Gordon (so please harass Ryan about bugs and not
  8. * Christoph).
  9. */
  10. /*
  11. * Handles CD-ROM disk images (and raw CD-ROM devices).
  12. *
  13. * Not supported:
  14. * - Rock Ridge (needed for sparse files, device nodes and symlinks, etc).
  15. * - Non 2048 Sectors
  16. * - TRANS.TBL (maps 8.3 filenames on old discs to long filenames).
  17. * - Multiextents (4gb max file size without it).
  18. * - UDF
  19. *
  20. * Deviations from the standard
  21. * - Ignores mandatory sort order
  22. * - Allows various invalid file names
  23. *
  24. * Problems
  25. * - Ambiguities in the standard
  26. */
  27. #define __PHYSICSFS_INTERNAL__
  28. #include "physfs_internal.h"
  29. #if PHYSFS_SUPPORTS_ISO9660
  30. #include <time.h>
  31. /* ISO9660 often stores values in both big and little endian formats: little
  32. first, followed by big. While technically there might be different values
  33. in each, we just always use the littleendian ones and swap ourselves. The
  34. fields aren't aligned anyhow, so you have to serialize them in any case
  35. to avoid crashes on many CPU archs in any case. */
  36. static int iso9660LoadEntries(PHYSFS_Io *io, const int joliet,
  37. const char *base, const PHYSFS_uint64 dirstart,
  38. const PHYSFS_uint64 dirend, void *unpkarc);
  39. static int iso9660AddEntry(PHYSFS_Io *io, const int joliet, const int isdir,
  40. const char *base, PHYSFS_uint8 *fname,
  41. const int fnamelen, const PHYSFS_sint64 ts,
  42. const PHYSFS_uint64 pos, const PHYSFS_uint64 len,
  43. void *unpkarc)
  44. {
  45. char *fullpath;
  46. char *fnamecpy;
  47. size_t baselen;
  48. size_t fullpathlen;
  49. void *entry;
  50. int i;
  51. if (fnamelen == 1 && ((fname[0] == 0) || (fname[0] == 1)))
  52. return 1; /* Magic that represents "." and "..", ignore */
  53. BAIL_IF(fnamelen == 0, PHYSFS_ERR_CORRUPT, 0);
  54. assert(fnamelen > 0);
  55. assert(fnamelen <= 255);
  56. BAIL_IF(joliet && (fnamelen % 2), PHYSFS_ERR_CORRUPT, 0);
  57. /* Joliet is UCS-2, so at most UTF-8 will double the byte size */
  58. baselen = strlen(base);
  59. fullpathlen = baselen + (fnamelen * (joliet ? 2 : 1)) + 2;
  60. fullpath = (char *) __PHYSFS_smallAlloc(fullpathlen);
  61. BAIL_IF(!fullpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  62. fnamecpy = fullpath;
  63. if (baselen > 0)
  64. {
  65. snprintf(fullpath, fullpathlen, "%s/", base);
  66. fnamecpy += baselen + 1;
  67. fullpathlen -= baselen - 1;
  68. } /* if */
  69. if (joliet)
  70. {
  71. PHYSFS_uint16 *ucs2 = (PHYSFS_uint16 *) fname;
  72. int total = fnamelen / 2;
  73. for (i = 0; i < total; i++)
  74. ucs2[i] = PHYSFS_swapUBE16(ucs2[i]);
  75. ucs2[total] = '\0';
  76. PHYSFS_utf8FromUcs2(ucs2, fnamecpy, fullpathlen);
  77. } /* if */
  78. else
  79. {
  80. for (i = 0; i < fnamelen; i++)
  81. {
  82. /* We assume the filenames are low-ASCII; consider the archive
  83. corrupt if we see something above 127, since we don't know the
  84. encoding. (We can change this later if we find out these exist
  85. and are intended to be, say, latin-1 or UTF-8 encoding). */
  86. BAIL_IF(fname[i] > 127, PHYSFS_ERR_CORRUPT, 0);
  87. fnamecpy[i] = fname[i];
  88. } /* for */
  89. fnamecpy[fnamelen] = '\0';
  90. if (!isdir)
  91. {
  92. /* find last SEPARATOR2 */
  93. char *ptr = strrchr(fnamecpy, ';');
  94. if (ptr && (ptr != fnamecpy))
  95. *(ptr--) = '\0';
  96. else
  97. ptr = fnamecpy + (fnamelen - 1);
  98. /* chop out any trailing '.', as done in all implementations */
  99. if (*ptr == '.')
  100. *ptr = '\0';
  101. } /* if */
  102. } /* else */
  103. entry = UNPK_addEntry(unpkarc, fullpath, isdir, ts, ts, pos, len);
  104. if ((entry) && (isdir))
  105. {
  106. if (!iso9660LoadEntries(io, joliet, fullpath, pos, pos + len, unpkarc))
  107. entry = NULL; /* so we report a failure later. */
  108. } /* if */
  109. __PHYSFS_smallFree(fullpath);
  110. return entry != NULL;
  111. } /* iso9660AddEntry */
  112. static int iso9660LoadEntries(PHYSFS_Io *io, const int joliet,
  113. const char *base, const PHYSFS_uint64 dirstart,
  114. const PHYSFS_uint64 dirend, void *unpkarc)
  115. {
  116. PHYSFS_uint64 readpos = dirstart;
  117. while (1)
  118. {
  119. PHYSFS_uint8 recordlen;
  120. PHYSFS_uint8 extattrlen;
  121. PHYSFS_uint32 extent;
  122. PHYSFS_uint32 datalen;
  123. PHYSFS_uint8 ignore[4];
  124. PHYSFS_uint8 year, month, day, hour, minute, second, offset;
  125. PHYSFS_uint8 flags;
  126. PHYSFS_uint8 fnamelen;
  127. PHYSFS_uint8 fname[256];
  128. PHYSFS_sint64 timestamp;
  129. struct tm t;
  130. int isdir;
  131. int multiextent;
  132. BAIL_IF_ERRPASS(!io->seek(io, readpos), 0);
  133. /* recordlen = 0 -> no more entries or fill entry */
  134. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &recordlen, 1), 0);
  135. if (recordlen > 0)
  136. readpos += recordlen; /* ready to seek to next record. */
  137. else
  138. {
  139. PHYSFS_uint64 nextpos;
  140. /* if we are in the last sector of the directory & it's 0 -> end */
  141. if ((dirend - 2048) <= (readpos - 1))
  142. break; /* finished */
  143. /* else skip to the next sector & continue; */
  144. nextpos = (((readpos - 1) / 2048) + 1) * 2048;
  145. /* whoops, can't make forward progress! */
  146. BAIL_IF(nextpos == readpos, PHYSFS_ERR_CORRUPT, 0);
  147. readpos = nextpos;
  148. continue; /* start back at upper loop. */
  149. } /* else */
  150. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &extattrlen, 1), 0);
  151. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &extent, 4), 0);
  152. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* extent be */
  153. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &datalen, 4), 0);
  154. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* datalen be */
  155. /* record timestamp */
  156. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &year, 1), 0);
  157. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &month, 1), 0);
  158. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &day, 1), 0);
  159. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &hour, 1), 0);
  160. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &minute, 1), 0);
  161. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &second, 1), 0);
  162. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &offset, 1), 0);
  163. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &flags, 1), 0);
  164. isdir = (flags & (1 << 1)) != 0;
  165. multiextent = (flags & (1 << 7)) != 0;
  166. BAIL_IF(multiextent, PHYSFS_ERR_UNSUPPORTED, 0); /* !!! FIXME */
  167. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 1), 0); /* unit size */
  168. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 1), 0); /* interleave gap */
  169. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* seqnum le */
  170. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* seqnum be */
  171. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &fnamelen, 1), 0);
  172. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, fname, fnamelen), 0);
  173. t.tm_sec = second;
  174. t.tm_min = minute;
  175. t.tm_hour = hour;
  176. t.tm_mday = day;
  177. t.tm_mon = month - 1;
  178. t.tm_year = year;
  179. t.tm_wday = 0;
  180. t.tm_yday = 0;
  181. t.tm_isdst = -1;
  182. timestamp = (PHYSFS_sint64) mktime(&t);
  183. extent += extattrlen; /* skip extended attribute record. */
  184. /* infinite loop, corrupt file? */
  185. BAIL_IF((extent * 2048) == dirstart, PHYSFS_ERR_CORRUPT, 0);
  186. if (!iso9660AddEntry(io, joliet, isdir, base, fname, fnamelen,
  187. timestamp, extent * 2048, datalen, unpkarc))
  188. {
  189. return 0;
  190. } /* if */
  191. } /* while */
  192. return 1;
  193. } /* iso9660LoadEntries */
  194. static int parseVolumeDescriptor(PHYSFS_Io *io, PHYSFS_uint64 *_rootpos,
  195. PHYSFS_uint64 *_rootlen, int *_joliet,
  196. int *_claimed)
  197. {
  198. PHYSFS_uint64 pos = 32768; /* start at the Primary Volume Descriptor */
  199. int found = 0;
  200. int done = 0;
  201. *_joliet = 0;
  202. while (!done)
  203. {
  204. PHYSFS_uint8 type;
  205. PHYSFS_uint8 identifier[5];
  206. PHYSFS_uint8 version;
  207. PHYSFS_uint8 flags;
  208. PHYSFS_uint8 escapeseqs[32];
  209. PHYSFS_uint8 ignore[32];
  210. PHYSFS_uint16 blocksize;
  211. PHYSFS_uint32 extent;
  212. PHYSFS_uint32 datalen;
  213. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  214. pos += 2048; /* each volume descriptor is 2048 bytes */
  215. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &type, 1), 0);
  216. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &identifier, 5), 0);
  217. if (memcmp(identifier, "CD001", 5) != 0) /* maybe not an iso? */
  218. {
  219. BAIL_IF(!*_claimed, PHYSFS_ERR_UNSUPPORTED, 0);
  220. continue; /* just skip this one */
  221. } /* if */
  222. *_claimed = 1; /* okay, this is probably an iso. */
  223. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, 1), 0); /* version */
  224. BAIL_IF(version != 1, PHYSFS_ERR_UNSUPPORTED, 0);
  225. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &flags, 1), 0);
  226. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 32), 0); /* system id */
  227. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 32), 0); /* volume id */
  228. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 8), 0); /* reserved */
  229. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* space le */
  230. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* space be */
  231. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, escapeseqs, 32), 0);
  232. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* setsize le */
  233. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* setsize be */
  234. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* seq num le */
  235. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* seq num be */
  236. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &blocksize, 2), 0);
  237. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 2), 0); /* blocklen be */
  238. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* pthtablen le */
  239. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* pthtablen be */
  240. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* pthtabpos le */
  241. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* optpthtabpos le */
  242. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* pthtabpos be */
  243. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* optpthtabpos be */
  244. /* root directory record... */
  245. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 1), 0); /* len */
  246. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 1), 0); /* attr len */
  247. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &extent, 4), 0);
  248. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* extent be */
  249. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &datalen, 4), 0);
  250. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), 0); /* datalen be */
  251. /* !!! FIXME: deal with this properly. */
  252. blocksize = PHYSFS_swapULE32(blocksize);
  253. BAIL_IF(blocksize && (blocksize != 2048), PHYSFS_ERR_UNSUPPORTED, 0);
  254. switch (type)
  255. {
  256. case 1: /* Primary Volume Descriptor */
  257. case 2: /* Supplementary Volume Descriptor */
  258. if (found < type)
  259. {
  260. *_rootpos = PHYSFS_swapULE32(extent) * 2048;
  261. *_rootlen = PHYSFS_swapULE32(datalen);
  262. found = type;
  263. if (found == 2) /* possible Joliet volume */
  264. {
  265. const PHYSFS_uint8 *s = escapeseqs;
  266. *_joliet = !(flags & 1) &&
  267. (s[0] == 0x25) && (s[1] == 0x2F) &&
  268. ((s[2] == 0x40) || (s[2] == 0x43) || (s[2] == 0x45));
  269. } /* if */
  270. } /* if */
  271. break;
  272. case 255: /* type 255 terminates the volume descriptor list */
  273. done = 1;
  274. break;
  275. default:
  276. break; /* skip unknown types. */
  277. } /* switch */
  278. } /* while */
  279. BAIL_IF(!found, PHYSFS_ERR_CORRUPT, 0);
  280. return 1;
  281. } /* parseVolumeDescriptor */
  282. static void *ISO9660_openArchive(PHYSFS_Io *io, const char *filename,
  283. int forWriting, int *claimed)
  284. {
  285. PHYSFS_uint64 rootpos = 0;
  286. PHYSFS_uint64 len = 0;
  287. int joliet = 0;
  288. void *unpkarc = NULL;
  289. assert(io != NULL); /* shouldn't ever happen. */
  290. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  291. if (!parseVolumeDescriptor(io, &rootpos, &len, &joliet, claimed))
  292. return NULL;
  293. unpkarc = UNPK_openArchive(io);
  294. BAIL_IF_ERRPASS(!unpkarc, NULL);
  295. if (!iso9660LoadEntries(io, joliet, "", rootpos, rootpos + len, unpkarc))
  296. {
  297. UNPK_abandonArchive(unpkarc);
  298. return NULL;
  299. } /* if */
  300. return unpkarc;
  301. } /* ISO9660_openArchive */
  302. const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660 =
  303. {
  304. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  305. {
  306. "ISO",
  307. "ISO9660 image file",
  308. "Ryan C. Gordon <icculus@icculus.org>",
  309. "https://icculus.org/physfs/",
  310. 0, /* supportsSymlinks */
  311. },
  312. ISO9660_openArchive,
  313. UNPK_enumerate,
  314. UNPK_openRead,
  315. UNPK_openWrite,
  316. UNPK_openAppend,
  317. UNPK_remove,
  318. UNPK_mkdir,
  319. UNPK_stat,
  320. UNPK_closeArchive
  321. };
  322. #endif /* defined PHYSFS_SUPPORTS_ISO9660 */
  323. /* end of physfs_archiver_iso9660.c ... */