archive_read_support_format_iso9660.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_iso9660.c,v 1.30 2008/12/06 06:57:45 kientzle Exp $");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. /* #include <stdint.h> */ /* See archive_platform.h */
  32. #include <stdio.h>
  33. #ifdef HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #include <time.h>
  40. #include "archive.h"
  41. #include "archive_entry.h"
  42. #include "archive_private.h"
  43. #include "archive_read_private.h"
  44. #include "archive_string.h"
  45. /*
  46. * An overview of ISO 9660 format:
  47. *
  48. * Each disk is laid out as follows:
  49. * * 32k reserved for private use
  50. * * Volume descriptor table. Each volume descriptor
  51. * is 2k and specifies basic format information.
  52. * The "Primary Volume Descriptor" (PVD) is defined by the
  53. * standard and should always be present; other volume
  54. * descriptors include various vendor-specific extensions.
  55. * * Files and directories. Each file/dir is specified by
  56. * an "extent" (starting sector and length in bytes).
  57. * Dirs are just files with directory records packed one
  58. * after another. The PVD contains a single dir entry
  59. * specifying the location of the root directory. Everything
  60. * else follows from there.
  61. *
  62. * This module works by first reading the volume descriptors, then
  63. * building a list of directory entries, sorted by starting
  64. * sector. At each step, I look for the earliest dir entry that
  65. * hasn't yet been read, seek forward to that location and read
  66. * that entry. If it's a dir, I slurp in the new dir entries and
  67. * add them to the heap; if it's a regular file, I return the
  68. * corresponding archive_entry and wait for the client to request
  69. * the file body. This strategy allows us to read most compliant
  70. * CDs with a single pass through the data, as required by libarchive.
  71. */
  72. /* Structure of on-disk primary volume descriptor. */
  73. #define PVD_type_offset 0
  74. #define PVD_type_size 1
  75. #define PVD_id_offset (PVD_type_offset + PVD_type_size)
  76. #define PVD_id_size 5
  77. #define PVD_version_offset (PVD_id_offset + PVD_id_size)
  78. #define PVD_version_size 1
  79. #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
  80. #define PVD_reserved1_size 1
  81. #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
  82. #define PVD_system_id_size 32
  83. #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
  84. #define PVD_volume_id_size 32
  85. #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
  86. #define PVD_reserved2_size 8
  87. #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
  88. #define PVD_volume_space_size_size 8
  89. #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
  90. #define PVD_reserved3_size 32
  91. #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
  92. #define PVD_volume_set_size_size 4
  93. #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
  94. #define PVD_volume_sequence_number_size 4
  95. #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
  96. #define PVD_logical_block_size_size 4
  97. #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
  98. #define PVD_path_table_size_size 8
  99. #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
  100. #define PVD_type_1_path_table_size 4
  101. #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
  102. #define PVD_opt_type_1_path_table_size 4
  103. #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
  104. #define PVD_type_m_path_table_size 4
  105. #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
  106. #define PVD_opt_type_m_path_table_size 4
  107. #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
  108. #define PVD_root_directory_record_size 34
  109. #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
  110. #define PVD_volume_set_id_size 128
  111. #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
  112. #define PVD_publisher_id_size 128
  113. #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
  114. #define PVD_preparer_id_size 128
  115. #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
  116. #define PVD_application_id_size 128
  117. #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
  118. #define PVD_copyright_file_id_size 37
  119. #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
  120. #define PVD_abstract_file_id_size 37
  121. #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
  122. #define PVD_bibliographic_file_id_size 37
  123. #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
  124. #define PVD_creation_date_size 17
  125. #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
  126. #define PVD_modification_date_size 17
  127. #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
  128. #define PVD_expiration_date_size 17
  129. #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
  130. #define PVD_effective_date_size 17
  131. #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
  132. #define PVD_file_structure_version_size 1
  133. #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
  134. #define PVD_reserved4_size 1
  135. #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
  136. #define PVD_application_data_size 512
  137. #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
  138. #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
  139. /* TODO: It would make future maintenance easier to just hardcode the
  140. * above values. In particular, ECMA119 states the offsets as part of
  141. * the standard. That would eliminate the need for the following check.*/
  142. #if PVD_reserved5_offset != 1395
  143. #error PVD offset and size definitions are wrong.
  144. #endif
  145. /* Structure of optional on-disk supplementary volume descriptor. */
  146. #define SVD_type_offset 0
  147. #define SVD_type_size 1
  148. #define SVD_id_offset (SVD_type_offset + SVD_type_size)
  149. #define SVD_id_size 5
  150. #define SVD_version_offset (SVD_id_offset + SVD_id_size)
  151. #define SVD_version_size 1
  152. /* ... */
  153. #define SVD_volume_space_size_offset 80
  154. #define SVD_volume_space_size_size 8
  155. #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
  156. #define SVD_escape_sequences_size 32
  157. /* ... */
  158. #define SVD_logical_block_size_offset 128
  159. #define SVD_logical_block_size_size 4
  160. /* ... */
  161. #define SVD_root_directory_record_offset 156
  162. #define SVD_root_directory_record_size 34
  163. /* ... */
  164. /* FIXME: validate correctness of last SVD entry offset. */
  165. /* Structure of an on-disk directory record. */
  166. /* Note: ISO9660 stores each multi-byte integer twice, once in
  167. * each byte order. The sizes here are the size of just one
  168. * of the two integers. (This is why the offset of a field isn't
  169. * the same as the offset+size of the previous field.) */
  170. #define DR_length_offset 0
  171. #define DR_length_size 1
  172. #define DR_ext_attr_length_offset 1
  173. #define DR_ext_attr_length_size 1
  174. #define DR_extent_offset 2
  175. #define DR_extent_size 4
  176. #define DR_size_offset 10
  177. #define DR_size_size 4
  178. #define DR_date_offset 18
  179. #define DR_date_size 7
  180. #define DR_flags_offset 25
  181. #define DR_flags_size 1
  182. #define DR_file_unit_size_offset 26
  183. #define DR_file_unit_size_size 1
  184. #define DR_interleave_offset 27
  185. #define DR_interleave_size 1
  186. #define DR_volume_sequence_number_offset 28
  187. #define DR_volume_sequence_number_size 2
  188. #define DR_name_len_offset 32
  189. #define DR_name_len_size 1
  190. #define DR_name_offset 33
  191. /* In-memory storage for a directory record. */
  192. struct file_info {
  193. struct file_info *parent;
  194. int refcount;
  195. uint64_t offset; /* Offset on disk. */
  196. uint64_t size; /* File size in bytes. */
  197. uint64_t ce_offset; /* Offset of CE */
  198. uint64_t ce_size; /* Size of CE */
  199. time_t birthtime; /* File created time. */
  200. time_t mtime; /* File last modified time. */
  201. time_t atime; /* File last accessed time. */
  202. time_t ctime; /* File attribute change time. */
  203. uint64_t rdev; /* Device number */
  204. mode_t mode;
  205. uid_t uid;
  206. gid_t gid;
  207. ino_t inode;
  208. int nlinks;
  209. struct archive_string name; /* Pathname */
  210. char name_continues; /* Non-zero if name continues */
  211. struct archive_string symlink;
  212. char symlink_continues; /* Non-zero if link continues */
  213. };
  214. struct iso9660 {
  215. int magic;
  216. #define ISO9660_MAGIC 0x96609660
  217. int option_ignore_joliet;
  218. struct archive_string pathname;
  219. char seenRockridge; /* Set true if RR extensions are used. */
  220. unsigned char suspOffset;
  221. char seenJoliet;
  222. uint64_t previous_offset;
  223. uint64_t previous_size;
  224. struct archive_string previous_pathname;
  225. /* TODO: Make this a heap for fast inserts and deletions. */
  226. struct file_info **pending_files;
  227. int pending_files_allocated;
  228. int pending_files_used;
  229. uint64_t current_position;
  230. ssize_t logical_block_size;
  231. uint64_t volume_size; /* Total size of volume in bytes. */
  232. off_t entry_sparse_offset;
  233. int64_t entry_bytes_remaining;
  234. };
  235. static void add_entry(struct iso9660 *iso9660, struct file_info *file);
  236. static int archive_read_format_iso9660_bid(struct archive_read *);
  237. static int archive_read_format_iso9660_options(struct archive_read *,
  238. const char *, const char *);
  239. static int archive_read_format_iso9660_cleanup(struct archive_read *);
  240. static int archive_read_format_iso9660_read_data(struct archive_read *,
  241. const void **, size_t *, off_t *);
  242. static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
  243. static int archive_read_format_iso9660_read_header(struct archive_read *,
  244. struct archive_entry *);
  245. static const char *build_pathname(struct archive_string *, struct file_info *);
  246. #if DEBUG
  247. static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
  248. #endif
  249. static time_t time_from_tm(struct tm *);
  250. static time_t isodate17(const unsigned char *);
  251. static time_t isodate7(const unsigned char *);
  252. static int isJolietSVD(struct iso9660 *, const unsigned char *);
  253. static int isPVD(struct iso9660 *, const unsigned char *);
  254. static struct file_info *next_entry(struct iso9660 *);
  255. static int next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
  256. struct file_info **pfile);
  257. static struct file_info *
  258. parse_file_info(struct iso9660 *iso9660,
  259. struct file_info *parent, const unsigned char *isodirrec);
  260. static void parse_rockridge(struct iso9660 *iso9660,
  261. struct file_info *file, const unsigned char *start,
  262. const unsigned char *end);
  263. static void parse_rockridge_NM1(struct file_info *,
  264. const unsigned char *, int);
  265. static void parse_rockridge_SL1(struct file_info *,
  266. const unsigned char *, int);
  267. static void parse_rockridge_TF1(struct file_info *,
  268. const unsigned char *, int);
  269. static void release_file(struct iso9660 *, struct file_info *);
  270. static unsigned toi(const void *p, int n);
  271. int
  272. archive_read_support_format_iso9660(struct archive *_a)
  273. {
  274. struct archive_read *a = (struct archive_read *)_a;
  275. struct iso9660 *iso9660;
  276. int r;
  277. iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
  278. if (iso9660 == NULL) {
  279. archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
  280. return (ARCHIVE_FATAL);
  281. }
  282. memset(iso9660, 0, sizeof(*iso9660));
  283. iso9660->magic = ISO9660_MAGIC;
  284. r = __archive_read_register_format(a,
  285. iso9660,
  286. "iso9660",
  287. archive_read_format_iso9660_bid,
  288. archive_read_format_iso9660_options,
  289. archive_read_format_iso9660_read_header,
  290. archive_read_format_iso9660_read_data,
  291. NULL,
  292. NULL,
  293. archive_read_format_iso9660_read_data_skip,
  294. archive_read_format_iso9660_cleanup);
  295. if (r != ARCHIVE_OK) {
  296. free(iso9660);
  297. return (r);
  298. }
  299. return (ARCHIVE_OK);
  300. }
  301. static int
  302. archive_read_format_iso9660_bid(struct archive_read *a)
  303. {
  304. struct iso9660 *iso9660;
  305. ssize_t bytes_read, brsvd;
  306. const void *h;
  307. const unsigned char *p, *psvd;
  308. int bid;
  309. iso9660 = (struct iso9660 *)(a->format->data);
  310. /*
  311. * Skip the first 32k (reserved area) and get the first
  312. * 8 sectors of the volume descriptor table. Of course,
  313. * if the I/O layer gives us more, we'll take it.
  314. */
  315. h = __archive_read_ahead(a, 32768 + 8*2048, &bytes_read);
  316. if (h == NULL)
  317. return (-1);
  318. p = (const unsigned char *)h;
  319. /* Skip the reserved area. */
  320. bytes_read -= 32768;
  321. p += 32768;
  322. /* Check each volume descriptor to locate possible SVD with Joliet. */
  323. for (brsvd = bytes_read, psvd = p;
  324. !iso9660->option_ignore_joliet && brsvd > 2048;
  325. brsvd -= 2048, psvd += 2048) {
  326. bid = isJolietSVD(iso9660, psvd);
  327. if (bid > 0)
  328. return (bid);
  329. if (*p == '\177') /* End-of-volume-descriptor marker. */
  330. break;
  331. }
  332. /* Check each volume descriptor to locate the PVD. */
  333. for (; bytes_read > 2048; bytes_read -= 2048, p += 2048) {
  334. bid = isPVD(iso9660, p);
  335. if (bid > 0)
  336. return (bid);
  337. if (*p == '\177') /* End-of-volume-descriptor marker. */
  338. break;
  339. }
  340. /* We didn't find a valid PVD; return a bid of zero. */
  341. return (0);
  342. }
  343. static int
  344. archive_read_format_iso9660_options(struct archive_read *a,
  345. const char *key, const char *val)
  346. {
  347. struct iso9660 *iso9660;
  348. iso9660 = (struct iso9660 *)(a->format->data);
  349. if (strcmp(key, "joliet") == 0) {
  350. if (val == NULL || strcmp(val, "off") == 0 ||
  351. strcmp(val, "ignore") == 0 ||
  352. strcmp(val, "disable") == 0 ||
  353. strcmp(val, "0") == 0)
  354. iso9660->option_ignore_joliet = 1;
  355. else
  356. iso9660->option_ignore_joliet = 0;
  357. return (ARCHIVE_OK);
  358. }
  359. /* Note: The "warn" return is just to inform the options
  360. * supervisor that we didn't handle it. It will generate
  361. * a suitable error if no one used this option. */
  362. return (ARCHIVE_WARN);
  363. }
  364. static int
  365. isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
  366. {
  367. struct file_info *file;
  368. const unsigned char *p;
  369. /* Type 2 means it's a SVD. */
  370. if (h[SVD_type_offset] != 2)
  371. return (0);
  372. /* ID must be "CD001" */
  373. if (memcmp(h + SVD_id_offset, "CD001", 5) != 0)
  374. return (0);
  375. /* FIXME: do more validations according to joliet spec. */
  376. /* check if this SVD contains joliet extension! */
  377. p = h + SVD_escape_sequences_offset;
  378. /* N.B. Joliet spec says p[1] == '\\', but.... */
  379. if (p[0] == '%' && p[1] == '/') {
  380. int level = 0;
  381. if (p[2] == '@')
  382. level = 1;
  383. else if (p[2] == 'C')
  384. level = 2;
  385. else if (p[2] == 'E')
  386. level = 3;
  387. else /* not joliet */
  388. return (0);
  389. iso9660->seenJoliet = level;
  390. } else /* not joliet */
  391. return (0);
  392. iso9660->logical_block_size = toi(h + SVD_logical_block_size_offset, 2);
  393. if (iso9660->logical_block_size <= 0)
  394. return (0);
  395. iso9660->volume_size = iso9660->logical_block_size
  396. * (uint64_t)toi(h + SVD_volume_space_size_offset, 4);
  397. /* Store the root directory in the pending list. */
  398. file = parse_file_info(iso9660, NULL, h + SVD_root_directory_record_offset);
  399. add_entry(iso9660, file);
  400. return (48);
  401. }
  402. static int
  403. isPVD(struct iso9660 *iso9660, const unsigned char *h)
  404. {
  405. struct file_info *file;
  406. int i;
  407. /* Type of the Primary Volume Descriptor must be 1. */
  408. if (h[PVD_type_offset] != 1)
  409. return (0);
  410. /* ID must be "CD001" */
  411. if (memcmp(h + PVD_id_offset, "CD001", 5) != 0)
  412. return (0);
  413. /* PVD version must be 1. */
  414. if (h[PVD_version_offset] != 1)
  415. return (0);
  416. /* Reserved field must be 0. */
  417. if (h[PVD_reserved1_offset] != 0)
  418. return (0);
  419. /* Reserved field must be 0. */
  420. for (i = 0; i < PVD_reserved2_size; ++i)
  421. if (h[PVD_reserved2_offset + i] != 0)
  422. return (0);
  423. /* Reserved field must be 0. */
  424. for (i = 0; i < PVD_reserved3_size; ++i)
  425. if (h[PVD_reserved3_offset + i] != 0)
  426. return (0);
  427. /* Logical block size must be > 0. */
  428. /* I've looked at Ecma 119 and can't find any stronger
  429. * restriction on this field. */
  430. iso9660->logical_block_size = toi(h + PVD_logical_block_size_offset, 2);
  431. if (iso9660->logical_block_size <= 0)
  432. return (0);
  433. iso9660->volume_size = iso9660->logical_block_size
  434. * (uint64_t)toi(h + PVD_volume_space_size_offset, 4);
  435. /* File structure version must be 1 for ISO9660/ECMA119. */
  436. if (h[PVD_file_structure_version_offset] != 1)
  437. return (0);
  438. /* Reserved field must be 0. */
  439. for (i = 0; i < PVD_reserved4_size; ++i)
  440. if (h[PVD_reserved4_offset + i] != 0)
  441. return (0);
  442. /* Reserved field must be 0. */
  443. for (i = 0; i < PVD_reserved5_size; ++i)
  444. if (h[PVD_reserved5_offset + i] != 0)
  445. return (0);
  446. /* XXX TODO: Check other values for sanity; reject more
  447. * malformed PVDs. XXX */
  448. /* Store the root directory in the pending list. */
  449. file = parse_file_info(iso9660, NULL, h + PVD_root_directory_record_offset);
  450. add_entry(iso9660, file);
  451. return (48);
  452. }
  453. static int
  454. archive_read_format_iso9660_read_header(struct archive_read *a,
  455. struct archive_entry *entry)
  456. {
  457. struct iso9660 *iso9660;
  458. struct file_info *file;
  459. int r;
  460. iso9660 = (struct iso9660 *)(a->format->data);
  461. if (!a->archive.archive_format) {
  462. a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
  463. a->archive.archive_format_name = "ISO9660";
  464. }
  465. /* Get the next entry that appears after the current offset. */
  466. r = next_entry_seek(a, iso9660, &file);
  467. if (r != ARCHIVE_OK) {
  468. release_file(iso9660, file);
  469. return (r);
  470. }
  471. iso9660->entry_bytes_remaining = file->size;
  472. iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
  473. if (file->offset + file->size > iso9660->volume_size) {
  474. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  475. "File is beyond end-of-media: %s", file->name);
  476. iso9660->entry_bytes_remaining = 0;
  477. iso9660->entry_sparse_offset = 0;
  478. release_file(iso9660, file);
  479. return (ARCHIVE_WARN);
  480. }
  481. /* Set up the entry structure with information about this entry. */
  482. archive_entry_set_mode(entry, file->mode);
  483. archive_entry_set_uid(entry, file->uid);
  484. archive_entry_set_gid(entry, file->gid);
  485. archive_entry_set_nlink(entry, file->nlinks);
  486. archive_entry_set_ino(entry, file->inode);
  487. archive_entry_set_birthtime(entry, file->birthtime, 0);
  488. archive_entry_set_mtime(entry, file->mtime, 0);
  489. archive_entry_set_ctime(entry, file->ctime, 0);
  490. archive_entry_set_atime(entry, file->atime, 0);
  491. /* N.B.: Rock Ridge supports 64-bit device numbers. */
  492. archive_entry_set_rdev(entry, (dev_t)file->rdev);
  493. archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
  494. archive_string_empty(&iso9660->pathname);
  495. archive_entry_set_pathname(entry,
  496. build_pathname(&iso9660->pathname, file));
  497. if (file->symlink.s != NULL)
  498. archive_entry_copy_symlink(entry, file->symlink.s);
  499. /* If this entry points to the same data as the previous
  500. * entry, convert this into a hardlink to that entry.
  501. * But don't bother for zero-length files. */
  502. if (file->offset == iso9660->previous_offset
  503. && file->size == iso9660->previous_size
  504. && file->size > 0) {
  505. archive_entry_set_hardlink(entry,
  506. iso9660->previous_pathname.s);
  507. iso9660->entry_bytes_remaining = 0;
  508. iso9660->entry_sparse_offset = 0;
  509. release_file(iso9660, file);
  510. return (ARCHIVE_OK);
  511. }
  512. /* If the offset is before our current position, we can't
  513. * seek backwards to extract it, so issue a warning. */
  514. if (file->offset < iso9660->current_position) {
  515. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  516. "Ignoring out-of-order file @%x (%s) %jd < %jd",
  517. file,
  518. iso9660->pathname.s,
  519. file->offset, iso9660->current_position);
  520. iso9660->entry_bytes_remaining = 0;
  521. iso9660->entry_sparse_offset = 0;
  522. release_file(iso9660, file);
  523. return (ARCHIVE_WARN);
  524. }
  525. iso9660->previous_size = file->size;
  526. iso9660->previous_offset = file->offset;
  527. archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
  528. /* If this is a directory, read in all of the entries right now. */
  529. if (archive_entry_filetype(entry) == AE_IFDIR) {
  530. while (iso9660->entry_bytes_remaining > 0) {
  531. const void *block;
  532. const unsigned char *p;
  533. ssize_t step = iso9660->logical_block_size;
  534. if (step > iso9660->entry_bytes_remaining)
  535. step = iso9660->entry_bytes_remaining;
  536. block = __archive_read_ahead(a, step, NULL);
  537. if (block == NULL) {
  538. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  539. "Failed to read full block when scanning ISO9660 directory list");
  540. release_file(iso9660, file);
  541. return (ARCHIVE_FATAL);
  542. }
  543. __archive_read_consume(a, step);
  544. iso9660->current_position += step;
  545. iso9660->entry_bytes_remaining -= step;
  546. for (p = (const unsigned char *)block;
  547. *p != 0 && p < (const unsigned char *)block + step;
  548. p += *p) {
  549. struct file_info *child;
  550. /* N.B.: these special directory identifiers
  551. * are 8 bit "values" even on a
  552. * Joliet CD with UCS-2 (16bit) encoding.
  553. */
  554. /* Skip '.' entry. */
  555. if (*(p + DR_name_len_offset) == 1
  556. && *(p + DR_name_offset) == '\0')
  557. continue;
  558. /* Skip '..' entry. */
  559. if (*(p + DR_name_len_offset) == 1
  560. && *(p + DR_name_offset) == '\001')
  561. continue;
  562. child = parse_file_info(iso9660, file, p);
  563. add_entry(iso9660, child);
  564. if (iso9660->seenRockridge) {
  565. a->archive.archive_format =
  566. ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
  567. a->archive.archive_format_name =
  568. "ISO9660 with Rockridge extensions";
  569. }
  570. }
  571. }
  572. }
  573. release_file(iso9660, file);
  574. return (ARCHIVE_OK);
  575. }
  576. static int
  577. archive_read_format_iso9660_read_data_skip(struct archive_read *a)
  578. {
  579. /* Because read_next_header always does an explicit skip
  580. * to the next entry, we don't need to do anything here. */
  581. (void)a; /* UNUSED */
  582. return (ARCHIVE_OK);
  583. }
  584. static int
  585. archive_read_format_iso9660_read_data(struct archive_read *a,
  586. const void **buff, size_t *size, off_t *offset)
  587. {
  588. ssize_t bytes_read;
  589. struct iso9660 *iso9660;
  590. iso9660 = (struct iso9660 *)(a->format->data);
  591. if (iso9660->entry_bytes_remaining <= 0) {
  592. *buff = NULL;
  593. *size = 0;
  594. *offset = iso9660->entry_sparse_offset;
  595. return (ARCHIVE_EOF);
  596. }
  597. *buff = __archive_read_ahead(a, 1, &bytes_read);
  598. if (bytes_read == 0)
  599. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  600. "Truncated input file");
  601. if (*buff == NULL)
  602. return (ARCHIVE_FATAL);
  603. if (bytes_read > iso9660->entry_bytes_remaining)
  604. bytes_read = iso9660->entry_bytes_remaining;
  605. *size = bytes_read;
  606. *offset = iso9660->entry_sparse_offset;
  607. iso9660->entry_sparse_offset += bytes_read;
  608. iso9660->entry_bytes_remaining -= bytes_read;
  609. iso9660->current_position += bytes_read;
  610. __archive_read_consume(a, bytes_read);
  611. return (ARCHIVE_OK);
  612. }
  613. static int
  614. archive_read_format_iso9660_cleanup(struct archive_read *a)
  615. {
  616. struct iso9660 *iso9660;
  617. struct file_info *file;
  618. iso9660 = (struct iso9660 *)(a->format->data);
  619. while ((file = next_entry(iso9660)) != NULL)
  620. release_file(iso9660, file);
  621. archive_string_free(&iso9660->pathname);
  622. archive_string_free(&iso9660->previous_pathname);
  623. if (iso9660->pending_files)
  624. free(iso9660->pending_files);
  625. free(iso9660);
  626. (a->format->data) = NULL;
  627. return (ARCHIVE_OK);
  628. }
  629. /*
  630. * This routine parses a single ISO directory record, makes sense
  631. * of any extensions, and stores the result in memory.
  632. */
  633. static struct file_info *
  634. parse_file_info(struct iso9660 *iso9660, struct file_info *parent,
  635. const unsigned char *isodirrec)
  636. {
  637. struct file_info *file;
  638. size_t name_len;
  639. const unsigned char *rr_start, *rr_end;
  640. const unsigned char *p;
  641. int flags;
  642. /* TODO: Sanity check that name_len doesn't exceed length, etc. */
  643. /* Create a new file entry and copy data from the ISO dir record. */
  644. file = (struct file_info *)malloc(sizeof(*file));
  645. if (file == NULL)
  646. return (NULL);
  647. memset(file, 0, sizeof(*file));
  648. file->parent = parent;
  649. if (parent != NULL)
  650. parent->refcount++;
  651. file->offset = (uint64_t)toi(isodirrec + DR_extent_offset, DR_extent_size)
  652. * iso9660->logical_block_size;
  653. file->size = toi(isodirrec + DR_size_offset, DR_size_size);
  654. file->mtime = isodate7(isodirrec + DR_date_offset);
  655. file->ctime = file->atime = file->mtime;
  656. name_len = (size_t)isodirrec[DR_name_len_offset];
  657. p = isodirrec + DR_name_offset;
  658. /* Rockridge extensions (if any) follow name. Compute this
  659. * before fidgeting the name_len below. */
  660. rr_start = p + name_len + (name_len & 1 ? 0 : 1) + iso9660->suspOffset;
  661. rr_end = isodirrec + isodirrec[DR_length_offset];
  662. if (iso9660->seenJoliet) {
  663. /* Joliet names are max 64 chars (128 bytes) according to spec,
  664. * but genisoimage (and others?) will allow you to have more.
  665. */
  666. wchar_t wbuff[64+1], *wp;
  667. const unsigned char *c;
  668. /* TODO: warn when name_len > 128 ? */
  669. /* convert BE UTF-16 to wchar_t */
  670. for (c = p, wp = wbuff;
  671. c < (p + name_len) &&
  672. wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
  673. c += 2) {
  674. *wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
  675. }
  676. *wp = L'\0';
  677. #if 0 /* untested code, is it at all useful on Joliet? */
  678. /* trim trailing first version and dot from filename.
  679. *
  680. * Remember we were in UTF-16BE land!
  681. * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
  682. * 16 bits big endian characters on Joliet.
  683. *
  684. * TODO: sanitize filename?
  685. * Joliet allows any UCS-2 char except:
  686. * *, /, :, ;, ? and \.
  687. */
  688. /* Chop off trailing ';1' from files. */
  689. if (*(wp-2) == ';' && *(wp-1) == '1') {
  690. wp-=2;
  691. *wp = L'\0';
  692. }
  693. /* Chop off trailing '.' from filenames. */
  694. if (*(wp-1) == '.')
  695. *(--wp) = L'\0';
  696. #endif
  697. /* store the result in the file name field. */
  698. archive_strappend_w_utf8(&file->name, wbuff);
  699. } else {
  700. /* Chop off trailing ';1' from files. */
  701. if (name_len > 2 && p[name_len - 2] == ';' &&
  702. p[name_len - 1] == '1')
  703. name_len -= 2;
  704. /* Chop off trailing '.' from filenames. */
  705. if (name_len > 1 && p[name_len - 1] == '.')
  706. --name_len;
  707. archive_strncpy(&file->name, (const char *)p, name_len);
  708. }
  709. flags = isodirrec[DR_flags_offset];
  710. if (flags & 0x02)
  711. file->mode = AE_IFDIR | 0700;
  712. else
  713. file->mode = AE_IFREG | 0400;
  714. /* Rockridge extensions overwrite information from above. */
  715. parse_rockridge(iso9660, file, rr_start, rr_end);
  716. #if DEBUG
  717. /* DEBUGGING: Warn about attributes I don't yet fully support. */
  718. if ((flags & ~0x02) != 0) {
  719. fprintf(stderr, "\n ** Unrecognized flag: ");
  720. dump_isodirrec(stderr, isodirrec);
  721. fprintf(stderr, "\n");
  722. } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
  723. fprintf(stderr, "\n ** Unrecognized sequence number: ");
  724. dump_isodirrec(stderr, isodirrec);
  725. fprintf(stderr, "\n");
  726. } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
  727. fprintf(stderr, "\n ** Unexpected file unit size: ");
  728. dump_isodirrec(stderr, isodirrec);
  729. fprintf(stderr, "\n");
  730. } else if (*(isodirrec + DR_interleave_offset) != 0) {
  731. fprintf(stderr, "\n ** Unexpected interleave: ");
  732. dump_isodirrec(stderr, isodirrec);
  733. fprintf(stderr, "\n");
  734. } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
  735. fprintf(stderr, "\n ** Unexpected extended attribute length: ");
  736. dump_isodirrec(stderr, isodirrec);
  737. fprintf(stderr, "\n");
  738. }
  739. #endif
  740. return (file);
  741. }
  742. static void
  743. add_entry(struct iso9660 *iso9660, struct file_info *file)
  744. {
  745. /* Expand our pending files list as necessary. */
  746. if (iso9660->pending_files_used >= iso9660->pending_files_allocated) {
  747. struct file_info **new_pending_files;
  748. int new_size = iso9660->pending_files_allocated * 2;
  749. if (iso9660->pending_files_allocated < 1024)
  750. new_size = 1024;
  751. /* Overflow might keep us from growing the list. */
  752. if (new_size <= iso9660->pending_files_allocated)
  753. __archive_errx(1, "Out of memory");
  754. new_pending_files = (struct file_info **)malloc(new_size * sizeof(new_pending_files[0]));
  755. if (new_pending_files == NULL)
  756. __archive_errx(1, "Out of memory");
  757. memcpy(new_pending_files, iso9660->pending_files,
  758. iso9660->pending_files_allocated * sizeof(new_pending_files[0]));
  759. if (iso9660->pending_files != NULL)
  760. free(iso9660->pending_files);
  761. iso9660->pending_files = new_pending_files;
  762. iso9660->pending_files_allocated = new_size;
  763. }
  764. iso9660->pending_files[iso9660->pending_files_used++] = file;
  765. }
  766. static void
  767. parse_rockridge(struct iso9660 *iso9660, struct file_info *file,
  768. const unsigned char *p, const unsigned char *end)
  769. {
  770. (void)iso9660; /* UNUSED */
  771. file->name_continues = 0;
  772. file->symlink_continues = 0;
  773. while (p + 4 < end /* Enough space for another entry. */
  774. && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
  775. && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
  776. && p[2] >= 4 /* Sanity-check length. */
  777. && p + p[2] <= end) { /* Sanity-check length. */
  778. const unsigned char *data = p + 4;
  779. int data_length = p[2] - 4;
  780. int version = p[3];
  781. /*
  782. * Yes, each 'if' here does test p[0] again.
  783. * Otherwise, the fall-through handling to catch
  784. * unsupported extensions doesn't work.
  785. */
  786. switch(p[0]) {
  787. case 'C':
  788. if (p[0] == 'C' && p[1] == 'E') {
  789. if (version == 1 && data_length == 24) {
  790. /*
  791. * CE extension comprises:
  792. * 8 byte sector containing extension
  793. * 8 byte offset w/in above sector
  794. * 8 byte length of continuation
  795. */
  796. file->ce_offset = (uint64_t)toi(data, 4)
  797. * iso9660->logical_block_size
  798. + toi(data + 8, 4);
  799. file->ce_size = toi(data + 16, 4);
  800. /* If the result is ridiculous,
  801. * ignore it. */
  802. if (file->ce_offset + file->ce_size
  803. > iso9660->volume_size) {
  804. file->ce_offset = 0;
  805. file->ce_size = 0;
  806. }
  807. }
  808. break;
  809. }
  810. /* FALLTHROUGH */
  811. case 'N':
  812. if (p[0] == 'N' && p[1] == 'M') {
  813. if (version == 1)
  814. parse_rockridge_NM1(file,
  815. data, data_length);
  816. break;
  817. }
  818. /* FALLTHROUGH */
  819. case 'P':
  820. if (p[0] == 'P' && p[1] == 'D') {
  821. /*
  822. * PD extension is padding;
  823. * contents are always ignored.
  824. */
  825. break;
  826. }
  827. if (p[0] == 'P' && p[1] == 'N') {
  828. if (version == 1 && data_length == 16) {
  829. file->rdev = toi(data,4);
  830. file->rdev <<= 32;
  831. file->rdev |= toi(data + 8, 4);
  832. }
  833. break;
  834. }
  835. if (p[0] == 'P' && p[1] == 'X') {
  836. /*
  837. * PX extension comprises:
  838. * 8 bytes for mode,
  839. * 8 bytes for nlinks,
  840. * 8 bytes for uid,
  841. * 8 bytes for gid,
  842. * 8 bytes for inode.
  843. */
  844. if (version == 1) {
  845. if (data_length >= 8)
  846. file->mode
  847. = toi(data, 4);
  848. if (data_length >= 16)
  849. file->nlinks
  850. = toi(data + 8, 4);
  851. if (data_length >= 24)
  852. file->uid
  853. = toi(data + 16, 4);
  854. if (data_length >= 32)
  855. file->gid
  856. = toi(data + 24, 4);
  857. if (data_length >= 40)
  858. file->inode
  859. = toi(data + 32, 4);
  860. }
  861. break;
  862. }
  863. /* FALLTHROUGH */
  864. case 'R':
  865. if (p[0] == 'R' && p[1] == 'R' && version == 1) {
  866. iso9660->seenRockridge = 1;
  867. /*
  868. * RR extension comprises:
  869. * one byte flag value
  870. */
  871. /* TODO: Handle RR extension. */
  872. break;
  873. }
  874. /* FALLTHROUGH */
  875. case 'S':
  876. if (p[0] == 'S' && p[1] == 'L') {
  877. if (version == 1)
  878. parse_rockridge_SL1(file,
  879. data, data_length);
  880. break;
  881. }
  882. if (p[0] == 'S' && p[1] == 'P'
  883. && version == 1 && data_length == 3
  884. && data[0] == (unsigned char)'\xbe'
  885. && data[1] == (unsigned char)'\xef') {
  886. /*
  887. * SP extension stores the suspOffset
  888. * (Number of bytes to skip between
  889. * filename and SUSP records.)
  890. * It is mandatory by the SUSP standard
  891. * (IEEE 1281).
  892. *
  893. * It allows SUSP to coexist with
  894. * non-SUSP uses of the System
  895. * Use Area by placing non-SUSP data
  896. * before SUSP data.
  897. *
  898. * TODO: Add a check for 'SP' in
  899. * first directory entry, disable all SUSP
  900. * processing if not found.
  901. */
  902. iso9660->suspOffset = data[2];
  903. break;
  904. }
  905. if (p[0] == 'S' && p[1] == 'T'
  906. && data_length == 0 && version == 1) {
  907. /*
  908. * ST extension marks end of this
  909. * block of SUSP entries.
  910. *
  911. * It allows SUSP to coexist with
  912. * non-SUSP uses of the System
  913. * Use Area by placing non-SUSP data
  914. * after SUSP data.
  915. */
  916. return;
  917. }
  918. case 'T':
  919. if (p[0] == 'T' && p[1] == 'F') {
  920. if (version == 1)
  921. parse_rockridge_TF1(file,
  922. data, data_length);
  923. break;
  924. }
  925. /* FALLTHROUGH */
  926. default:
  927. /* The FALLTHROUGHs above leave us here for
  928. * any unsupported extension. */
  929. #if DEBUG
  930. {
  931. const unsigned char *t;
  932. fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name.s);
  933. fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length);
  934. for (t = data; t < data + data_length && t < data + 16; t++)
  935. fprintf(stderr, " %02x", *t);
  936. fprintf(stderr, "\n");
  937. }
  938. #endif
  939. break;
  940. }
  941. p += p[2];
  942. }
  943. }
  944. static void
  945. parse_rockridge_NM1(struct file_info *file,
  946. const unsigned char *data, int data_length)
  947. {
  948. if (!file->name_continues)
  949. archive_string_empty(&file->name);
  950. file->name_continues = 0;
  951. if (data_length < 1)
  952. return;
  953. /*
  954. * NM version 1 extension comprises:
  955. * 1 byte flag, value is one of:
  956. * = 0: remainder is name
  957. * = 1: remainder is name, next NM entry continues name
  958. * = 2: "."
  959. * = 4: ".."
  960. * = 32: Implementation specific
  961. * All other values are reserved.
  962. */
  963. switch(data[0]) {
  964. case 0:
  965. if (data_length < 2)
  966. return;
  967. archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
  968. break;
  969. case 1:
  970. if (data_length < 2)
  971. return;
  972. archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
  973. file->name_continues = 1;
  974. break;
  975. case 2:
  976. archive_strcat(&file->name, ".");
  977. break;
  978. case 4:
  979. archive_strcat(&file->name, "..");
  980. break;
  981. default:
  982. return;
  983. }
  984. }
  985. static void
  986. parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
  987. int data_length)
  988. {
  989. char flag;
  990. /*
  991. * TF extension comprises:
  992. * one byte flag
  993. * create time (optional)
  994. * modify time (optional)
  995. * access time (optional)
  996. * attribute time (optional)
  997. * Time format and presence of fields
  998. * is controlled by flag bits.
  999. */
  1000. if (data_length < 1)
  1001. return;
  1002. flag = data[0];
  1003. ++data;
  1004. --data_length;
  1005. if (flag & 0x80) {
  1006. /* Use 17-byte time format. */
  1007. if ((flag & 1) && data_length >= 17) {
  1008. /* Create time. */
  1009. file->birthtime = isodate17(data);
  1010. data += 17;
  1011. data_length -= 17;
  1012. }
  1013. if ((flag & 2) && data_length >= 17) {
  1014. /* Modify time. */
  1015. file->mtime = isodate17(data);
  1016. data += 17;
  1017. data_length -= 17;
  1018. }
  1019. if ((flag & 4) && data_length >= 17) {
  1020. /* Access time. */
  1021. file->atime = isodate17(data);
  1022. data += 17;
  1023. data_length -= 17;
  1024. }
  1025. if ((flag & 8) && data_length >= 17) {
  1026. /* Attribute change time. */
  1027. file->ctime = isodate17(data);
  1028. }
  1029. } else {
  1030. /* Use 7-byte time format. */
  1031. if ((flag & 1) && data_length >= 7) {
  1032. /* Create time. */
  1033. file->birthtime = isodate17(data);
  1034. data += 7;
  1035. data_length -= 7;
  1036. }
  1037. if ((flag & 2) && data_length >= 7) {
  1038. /* Modify time. */
  1039. file->mtime = isodate7(data);
  1040. data += 7;
  1041. data_length -= 7;
  1042. }
  1043. if ((flag & 4) && data_length >= 7) {
  1044. /* Access time. */
  1045. file->atime = isodate7(data);
  1046. data += 7;
  1047. data_length -= 7;
  1048. }
  1049. if ((flag & 8) && data_length >= 7) {
  1050. /* Attribute change time. */
  1051. file->ctime = isodate7(data);
  1052. }
  1053. }
  1054. }
  1055. static void
  1056. parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
  1057. int data_length)
  1058. {
  1059. int component_continues = 1;
  1060. if (!file->symlink_continues)
  1061. archive_string_empty(&file->symlink);
  1062. else
  1063. archive_strcat(&file->symlink, "/");
  1064. file->symlink_continues = 0;
  1065. /*
  1066. * Defined flag values:
  1067. * 0: This is the last SL record for this symbolic link
  1068. * 1: this symbolic link field continues in next SL entry
  1069. * All other values are reserved.
  1070. */
  1071. if (data_length < 1)
  1072. return;
  1073. switch(*data) {
  1074. case 0:
  1075. break;
  1076. case 1:
  1077. file->symlink_continues = 1;
  1078. break;
  1079. default:
  1080. return;
  1081. }
  1082. ++data; /* Skip flag byte. */
  1083. --data_length;
  1084. /*
  1085. * SL extension body stores "components".
  1086. * Basically, this is a complicated way of storing
  1087. * a POSIX path. It also interferes with using
  1088. * symlinks for storing non-path data. <sigh>
  1089. *
  1090. * Each component is 2 bytes (flag and length)
  1091. * possibly followed by name data.
  1092. */
  1093. while (data_length >= 2) {
  1094. unsigned char flag = *data++;
  1095. unsigned char nlen = *data++;
  1096. data_length -= 2;
  1097. if (!component_continues)
  1098. archive_strcat(&file->symlink, "/");
  1099. component_continues = 0;
  1100. switch(flag) {
  1101. case 0: /* Usual case, this is text. */
  1102. if (data_length < nlen)
  1103. return;
  1104. archive_strncat(&file->symlink,
  1105. (const char *)data, nlen);
  1106. break;
  1107. case 0x01: /* Text continues in next component. */
  1108. if (data_length < nlen)
  1109. return;
  1110. archive_strncat(&file->symlink,
  1111. (const char *)data, nlen);
  1112. component_continues = 1;
  1113. break;
  1114. case 0x02: /* Current dir. */
  1115. archive_strcat(&file->symlink, ".");
  1116. break;
  1117. case 0x04: /* Parent dir. */
  1118. archive_strcat(&file->symlink, "..");
  1119. break;
  1120. case 0x08: /* Root of filesystem. */
  1121. archive_string_empty(&file->symlink);
  1122. archive_strcat(&file->symlink, "/");
  1123. break;
  1124. case 0x10: /* Undefined (historically "volume root" */
  1125. archive_string_empty(&file->symlink);
  1126. archive_strcat(&file->symlink, "ROOT");
  1127. break;
  1128. case 0x20: /* Undefined (historically "hostname") */
  1129. archive_strcat(&file->symlink, "hostname");
  1130. break;
  1131. default:
  1132. /* TODO: issue a warning ? */
  1133. return;
  1134. }
  1135. data += nlen;
  1136. data_length -= nlen;
  1137. }
  1138. }
  1139. static void
  1140. release_file(struct iso9660 *iso9660, struct file_info *file)
  1141. {
  1142. struct file_info *parent;
  1143. if (file == NULL)
  1144. return;
  1145. if (file->refcount == 0) {
  1146. parent = file->parent;
  1147. archive_string_free(&file->name);
  1148. archive_string_free(&file->symlink);
  1149. free(file);
  1150. if (parent != NULL) {
  1151. parent->refcount--;
  1152. release_file(iso9660, parent);
  1153. }
  1154. }
  1155. }
  1156. static int
  1157. next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
  1158. struct file_info **pfile)
  1159. {
  1160. struct file_info *file;
  1161. uint64_t offset;
  1162. *pfile = NULL;
  1163. for (;;) {
  1164. *pfile = file = next_entry(iso9660);
  1165. if (file == NULL)
  1166. return (ARCHIVE_EOF);
  1167. /* CE area precedes actual file data? Ignore it. */
  1168. if (file->ce_offset > file->offset) {
  1169. /* fprintf(stderr, " *** Discarding CE data.\n"); */
  1170. file->ce_offset = 0;
  1171. file->ce_size = 0;
  1172. }
  1173. /* Don't waste time seeking for zero-length bodies. */
  1174. if (file->size == 0) {
  1175. file->offset = iso9660->current_position;
  1176. }
  1177. /* If CE exists, find and read it now. */
  1178. if (file->ce_offset > 0)
  1179. offset = file->ce_offset;
  1180. else
  1181. offset = file->offset;
  1182. /* Seek forward to the start of the entry. */
  1183. if (iso9660->current_position < offset) {
  1184. off_t step = offset - iso9660->current_position;
  1185. off_t bytes_read;
  1186. bytes_read = __archive_read_skip(a, step);
  1187. if (bytes_read < 0)
  1188. return (bytes_read);
  1189. iso9660->current_position = offset;
  1190. }
  1191. /* We found body of file; handle it now. */
  1192. if (offset == file->offset)
  1193. return (ARCHIVE_OK);
  1194. /* Found CE? Process it and push the file back onto list. */
  1195. if (offset == file->ce_offset) {
  1196. const void *p;
  1197. ssize_t size = file->ce_size;
  1198. const unsigned char *rr_start;
  1199. file->ce_offset = 0;
  1200. file->ce_size = 0;
  1201. p = __archive_read_ahead(a, size, NULL);
  1202. if (p == NULL)
  1203. return (ARCHIVE_FATAL);
  1204. rr_start = (const unsigned char *)p;
  1205. parse_rockridge(iso9660, file, rr_start,
  1206. rr_start + size);
  1207. __archive_read_consume(a, size);
  1208. iso9660->current_position += size;
  1209. add_entry(iso9660, file);
  1210. }
  1211. }
  1212. }
  1213. static struct file_info *
  1214. next_entry(struct iso9660 *iso9660)
  1215. {
  1216. int least_index;
  1217. uint64_t least_end_offset;
  1218. int i;
  1219. struct file_info *r;
  1220. if (iso9660->pending_files_used < 1)
  1221. return (NULL);
  1222. /* Assume the first file in the list is the earliest on disk. */
  1223. least_index = 0;
  1224. least_end_offset = iso9660->pending_files[0]->offset
  1225. + iso9660->pending_files[0]->size;
  1226. /* Now, try to find an earlier one. */
  1227. for (i = 0; i < iso9660->pending_files_used; i++) {
  1228. /* Use the position of the file *end* as our comparison. */
  1229. uint64_t end_offset = iso9660->pending_files[i]->offset
  1230. + iso9660->pending_files[i]->size;
  1231. if (iso9660->pending_files[i]->ce_offset > 0
  1232. && iso9660->pending_files[i]->ce_offset < iso9660->pending_files[i]->offset)
  1233. end_offset = iso9660->pending_files[i]->ce_offset
  1234. + iso9660->pending_files[i]->ce_size;
  1235. if (least_end_offset > end_offset) {
  1236. least_index = i;
  1237. least_end_offset = end_offset;
  1238. }
  1239. }
  1240. r = iso9660->pending_files[least_index];
  1241. iso9660->pending_files[least_index]
  1242. = iso9660->pending_files[--iso9660->pending_files_used];
  1243. return (r);
  1244. }
  1245. static unsigned int
  1246. toi(const void *p, int n)
  1247. {
  1248. const unsigned char *v = (const unsigned char *)p;
  1249. if (n > 1)
  1250. return v[0] + 256 * toi(v + 1, n - 1);
  1251. if (n == 1)
  1252. return v[0];
  1253. return (0);
  1254. }
  1255. static time_t
  1256. isodate7(const unsigned char *v)
  1257. {
  1258. struct tm tm;
  1259. int offset;
  1260. memset(&tm, 0, sizeof(tm));
  1261. tm.tm_year = v[0];
  1262. tm.tm_mon = v[1] - 1;
  1263. tm.tm_mday = v[2];
  1264. tm.tm_hour = v[3];
  1265. tm.tm_min = v[4];
  1266. tm.tm_sec = v[5];
  1267. /* v[6] is the signed timezone offset, in 1/4-hour increments. */
  1268. offset = ((const signed char *)v)[6];
  1269. if (offset > -48 && offset < 52) {
  1270. tm.tm_hour -= offset / 4;
  1271. tm.tm_min -= (offset % 4) * 15;
  1272. }
  1273. return (time_from_tm(&tm));
  1274. }
  1275. static time_t
  1276. isodate17(const unsigned char *v)
  1277. {
  1278. struct tm tm;
  1279. int offset;
  1280. memset(&tm, 0, sizeof(tm));
  1281. tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
  1282. + (v[2] - '0') * 10 + (v[3] - '0')
  1283. - 1900;
  1284. tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
  1285. tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
  1286. tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
  1287. tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
  1288. tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
  1289. /* v[16] is the signed timezone offset, in 1/4-hour increments. */
  1290. offset = ((const signed char *)v)[16];
  1291. if (offset > -48 && offset < 52) {
  1292. tm.tm_hour -= offset / 4;
  1293. tm.tm_min -= (offset % 4) * 15;
  1294. }
  1295. return (time_from_tm(&tm));
  1296. }
  1297. static time_t
  1298. time_from_tm(struct tm *t)
  1299. {
  1300. #if HAVE_TIMEGM
  1301. /* Use platform timegm() if available. */
  1302. return (timegm(t));
  1303. #else
  1304. /* Else use direct calculation using POSIX assumptions. */
  1305. /* First, fix up tm_yday based on the year/month/day. */
  1306. mktime(t);
  1307. /* Then we can compute timegm() from first principles. */
  1308. return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
  1309. + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
  1310. + ((t->tm_year - 69) / 4) * 86400 -
  1311. ((t->tm_year - 1) / 100) * 86400
  1312. + ((t->tm_year + 299) / 400) * 86400);
  1313. #endif
  1314. }
  1315. static const char *
  1316. build_pathname(struct archive_string *as, struct file_info *file)
  1317. {
  1318. if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
  1319. build_pathname(as, file->parent);
  1320. archive_strcat(as, "/");
  1321. }
  1322. if (archive_strlen(&file->name) == 0)
  1323. archive_strcat(as, ".");
  1324. else
  1325. archive_string_concat(as, &file->name);
  1326. return (as->s);
  1327. }
  1328. #if DEBUG
  1329. static void
  1330. dump_isodirrec(FILE *out, const unsigned char *isodirrec)
  1331. {
  1332. fprintf(out, " l %d,",
  1333. toi(isodirrec + DR_length_offset, DR_length_size));
  1334. fprintf(out, " a %d,",
  1335. toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
  1336. fprintf(out, " ext 0x%x,",
  1337. toi(isodirrec + DR_extent_offset, DR_extent_size));
  1338. fprintf(out, " s %d,",
  1339. toi(isodirrec + DR_size_offset, DR_extent_size));
  1340. fprintf(out, " f 0x%02x,",
  1341. toi(isodirrec + DR_flags_offset, DR_flags_size));
  1342. fprintf(out, " u %d,",
  1343. toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
  1344. fprintf(out, " ilv %d,",
  1345. toi(isodirrec + DR_interleave_offset, DR_interleave_size));
  1346. fprintf(out, " seq %d,",
  1347. toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
  1348. fprintf(out, " nl %d:",
  1349. toi(isodirrec + DR_name_len_offset, DR_name_len_size));
  1350. fprintf(out, " `%.*s'",
  1351. toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
  1352. }
  1353. #endif