archive_read_support_format_iso9660.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2009 Andreas Henriksson <[email protected]>
  4. * Copyright (c) 2009 Michihiro NAKAJIMA
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_iso9660.c,v 1.30 2008/12/06 06:57:45 kientzle Exp $");
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  32. /* #include <stdint.h> */ /* See archive_platform.h */
  33. #include <stdio.h>
  34. #ifdef HAVE_STDLIB_H
  35. #include <stdlib.h>
  36. #endif
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40. #include <time.h>
  41. #ifdef HAVE_ZLIB_H
  42. #include <cm_zlib.h>
  43. #endif
  44. #include "archive.h"
  45. #include "archive_endian.h"
  46. #include "archive_entry.h"
  47. #include "archive_private.h"
  48. #include "archive_read_private.h"
  49. #include "archive_string.h"
  50. /*
  51. * An overview of ISO 9660 format:
  52. *
  53. * Each disk is laid out as follows:
  54. * * 32k reserved for private use
  55. * * Volume descriptor table. Each volume descriptor
  56. * is 2k and specifies basic format information.
  57. * The "Primary Volume Descriptor" (PVD) is defined by the
  58. * standard and should always be present; other volume
  59. * descriptors include various vendor-specific extensions.
  60. * * Files and directories. Each file/dir is specified by
  61. * an "extent" (starting sector and length in bytes).
  62. * Dirs are just files with directory records packed one
  63. * after another. The PVD contains a single dir entry
  64. * specifying the location of the root directory. Everything
  65. * else follows from there.
  66. *
  67. * This module works by first reading the volume descriptors, then
  68. * building a list of directory entries, sorted by starting
  69. * sector. At each step, I look for the earliest dir entry that
  70. * hasn't yet been read, seek forward to that location and read
  71. * that entry. If it's a dir, I slurp in the new dir entries and
  72. * add them to the heap; if it's a regular file, I return the
  73. * corresponding archive_entry and wait for the client to request
  74. * the file body. This strategy allows us to read most compliant
  75. * CDs with a single pass through the data, as required by libarchive.
  76. */
  77. #define LOGICAL_BLOCK_SIZE 2048
  78. #define SYSTEM_AREA_BLOCK 16
  79. /* Structure of on-disk primary volume descriptor. */
  80. #define PVD_type_offset 0
  81. #define PVD_type_size 1
  82. #define PVD_id_offset (PVD_type_offset + PVD_type_size)
  83. #define PVD_id_size 5
  84. #define PVD_version_offset (PVD_id_offset + PVD_id_size)
  85. #define PVD_version_size 1
  86. #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
  87. #define PVD_reserved1_size 1
  88. #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
  89. #define PVD_system_id_size 32
  90. #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
  91. #define PVD_volume_id_size 32
  92. #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
  93. #define PVD_reserved2_size 8
  94. #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
  95. #define PVD_volume_space_size_size 8
  96. #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
  97. #define PVD_reserved3_size 32
  98. #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
  99. #define PVD_volume_set_size_size 4
  100. #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
  101. #define PVD_volume_sequence_number_size 4
  102. #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
  103. #define PVD_logical_block_size_size 4
  104. #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
  105. #define PVD_path_table_size_size 8
  106. #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
  107. #define PVD_type_1_path_table_size 4
  108. #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
  109. #define PVD_opt_type_1_path_table_size 4
  110. #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
  111. #define PVD_type_m_path_table_size 4
  112. #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
  113. #define PVD_opt_type_m_path_table_size 4
  114. #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
  115. #define PVD_root_directory_record_size 34
  116. #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
  117. #define PVD_volume_set_id_size 128
  118. #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
  119. #define PVD_publisher_id_size 128
  120. #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
  121. #define PVD_preparer_id_size 128
  122. #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
  123. #define PVD_application_id_size 128
  124. #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
  125. #define PVD_copyright_file_id_size 37
  126. #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
  127. #define PVD_abstract_file_id_size 37
  128. #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
  129. #define PVD_bibliographic_file_id_size 37
  130. #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
  131. #define PVD_creation_date_size 17
  132. #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
  133. #define PVD_modification_date_size 17
  134. #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
  135. #define PVD_expiration_date_size 17
  136. #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
  137. #define PVD_effective_date_size 17
  138. #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
  139. #define PVD_file_structure_version_size 1
  140. #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
  141. #define PVD_reserved4_size 1
  142. #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
  143. #define PVD_application_data_size 512
  144. #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
  145. #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
  146. /* TODO: It would make future maintenance easier to just hardcode the
  147. * above values. In particular, ECMA119 states the offsets as part of
  148. * the standard. That would eliminate the need for the following check.*/
  149. #if PVD_reserved5_offset != 1395
  150. #error PVD offset and size definitions are wrong.
  151. #endif
  152. /* Structure of optional on-disk supplementary volume descriptor. */
  153. #define SVD_type_offset 0
  154. #define SVD_type_size 1
  155. #define SVD_id_offset (SVD_type_offset + SVD_type_size)
  156. #define SVD_id_size 5
  157. #define SVD_version_offset (SVD_id_offset + SVD_id_size)
  158. #define SVD_version_size 1
  159. /* ... */
  160. #define SVD_reserved1_offset 72
  161. #define SVD_reserved1_size 8
  162. #define SVD_volume_space_size_offset 80
  163. #define SVD_volume_space_size_size 8
  164. #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
  165. #define SVD_escape_sequences_size 32
  166. /* ... */
  167. #define SVD_logical_block_size_offset 128
  168. #define SVD_logical_block_size_size 4
  169. /* ... */
  170. #define SVD_root_directory_record_offset 156
  171. #define SVD_root_directory_record_size 34
  172. #define SVD_reserved2_offset 882
  173. #define SVD_reserved2_size 1
  174. #define SVD_reserved3_offset 1395
  175. #define SVD_reserved3_size 653
  176. /* ... */
  177. /* FIXME: validate correctness of last SVD entry offset. */
  178. /* Structure of an on-disk directory record. */
  179. /* Note: ISO9660 stores each multi-byte integer twice, once in
  180. * each byte order. The sizes here are the size of just one
  181. * of the two integers. (This is why the offset of a field isn't
  182. * the same as the offset+size of the previous field.) */
  183. #define DR_length_offset 0
  184. #define DR_length_size 1
  185. #define DR_ext_attr_length_offset 1
  186. #define DR_ext_attr_length_size 1
  187. #define DR_extent_offset 2
  188. #define DR_extent_size 4
  189. #define DR_size_offset 10
  190. #define DR_size_size 4
  191. #define DR_date_offset 18
  192. #define DR_date_size 7
  193. #define DR_flags_offset 25
  194. #define DR_flags_size 1
  195. #define DR_file_unit_size_offset 26
  196. #define DR_file_unit_size_size 1
  197. #define DR_interleave_offset 27
  198. #define DR_interleave_size 1
  199. #define DR_volume_sequence_number_offset 28
  200. #define DR_volume_sequence_number_size 2
  201. #define DR_name_len_offset 32
  202. #define DR_name_len_size 1
  203. #define DR_name_offset 33
  204. #ifdef HAVE_ZLIB_H
  205. static const unsigned char zisofs_magic[8] = {
  206. 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
  207. };
  208. struct zisofs {
  209. /* Set 1 if this file compressed by paged zlib */
  210. int pz;
  211. int pz_log2_bs; /* Log2 of block size */
  212. uint64_t pz_uncompressed_size;
  213. int initialized;
  214. unsigned char *uncompressed_buffer;
  215. size_t uncompressed_buffer_size;
  216. uint32_t pz_offset;
  217. unsigned char header[16];
  218. size_t header_avail;
  219. int header_passed;
  220. unsigned char *block_pointers;
  221. size_t block_pointers_alloc;
  222. size_t block_pointers_size;
  223. size_t block_pointers_avail;
  224. size_t block_off;
  225. uint32_t block_avail;
  226. z_stream stream;
  227. int stream_valid;
  228. };
  229. #else
  230. struct zisofs {
  231. /* Set 1 if this file compressed by paged zlib */
  232. int pz;
  233. };
  234. #endif
  235. /* In-memory storage for a directory record. */
  236. struct file_info {
  237. struct file_info *parent;
  238. int refcount;
  239. uint64_t offset; /* Offset on disk. */
  240. uint64_t size; /* File size in bytes. */
  241. uint64_t ce_offset; /* Offset of CE */
  242. uint64_t ce_size; /* Size of CE */
  243. time_t birthtime; /* File created time. */
  244. time_t mtime; /* File last modified time. */
  245. time_t atime; /* File last accessed time. */
  246. time_t ctime; /* File attribute change time. */
  247. uint64_t rdev; /* Device number */
  248. mode_t mode;
  249. uid_t uid;
  250. gid_t gid;
  251. ino_t inode;
  252. int nlinks;
  253. struct archive_string name; /* Pathname */
  254. char name_continues; /* Non-zero if name continues */
  255. struct archive_string symlink;
  256. char symlink_continues; /* Non-zero if link continues */
  257. /* Set 1 if this file compressed by paged zlib(zisofs) */
  258. int pz;
  259. int pz_log2_bs; /* Log2 of block size */
  260. uint64_t pz_uncompressed_size;
  261. };
  262. struct iso9660 {
  263. int magic;
  264. #define ISO9660_MAGIC 0x96609660
  265. int option_ignore_joliet;
  266. int option_ignore_rockridge;
  267. struct archive_string pathname;
  268. char seenRockridge; /* Set true if RR extensions are used. */
  269. unsigned char suspOffset;
  270. char seenJoliet;
  271. uint64_t previous_offset;
  272. uint64_t previous_size;
  273. struct archive_string previous_pathname;
  274. /* TODO: Make this a heap for fast inserts and deletions. */
  275. struct file_info **pending_files;
  276. int pending_files_allocated;
  277. int pending_files_used;
  278. uint64_t current_position;
  279. ssize_t logical_block_size;
  280. uint64_t volume_size; /* Total size of volume in bytes. */
  281. int32_t volume_block;/* Total size of volume in logical blocks. */
  282. struct vd {
  283. int sector_number; /* Logical Sector Number. */
  284. uint32_t block_size;
  285. } primary, joliet;
  286. off_t entry_sparse_offset;
  287. int64_t entry_bytes_remaining;
  288. struct zisofs entry_zisofs;
  289. };
  290. static void add_entry(struct iso9660 *iso9660, struct file_info *file);
  291. static int archive_read_format_iso9660_bid(struct archive_read *);
  292. static int archive_read_format_iso9660_options(struct archive_read *,
  293. const char *, const char *);
  294. static int archive_read_format_iso9660_cleanup(struct archive_read *);
  295. static int archive_read_format_iso9660_read_data(struct archive_read *,
  296. const void **, size_t *, off_t *);
  297. static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
  298. static int archive_read_format_iso9660_read_header(struct archive_read *,
  299. struct archive_entry *);
  300. static const char *build_pathname(struct archive_string *, struct file_info *);
  301. #if DEBUG
  302. static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
  303. #endif
  304. static time_t time_from_tm(struct tm *);
  305. static time_t isodate17(const unsigned char *);
  306. static time_t isodate7(const unsigned char *);
  307. static int isVDSetTerminator(struct iso9660 *iso9660,
  308. const unsigned char *h);
  309. static int isJolietSVD(struct iso9660 *, const unsigned char *);
  310. static int isPVD(struct iso9660 *, const unsigned char *);
  311. static struct file_info *next_entry(struct iso9660 *);
  312. static int next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
  313. struct file_info **pfile);
  314. static struct file_info *
  315. parse_file_info(struct archive_read *a,
  316. struct file_info *parent, const unsigned char *isodirrec);
  317. static void parse_rockridge(struct iso9660 *iso9660,
  318. struct file_info *file, const unsigned char *start,
  319. const unsigned char *end);
  320. static void parse_rockridge_NM1(struct file_info *,
  321. const unsigned char *, int);
  322. static void parse_rockridge_SL1(struct file_info *,
  323. const unsigned char *, int);
  324. static void parse_rockridge_TF1(struct file_info *,
  325. const unsigned char *, int);
  326. static void parse_rockridge_ZF1(struct file_info *,
  327. const unsigned char *, int);
  328. static void release_file(struct iso9660 *, struct file_info *);
  329. static unsigned toi(const void *p, int n);
  330. int
  331. archive_read_support_format_iso9660(struct archive *_a)
  332. {
  333. struct archive_read *a = (struct archive_read *)_a;
  334. struct iso9660 *iso9660;
  335. int r;
  336. iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
  337. if (iso9660 == NULL) {
  338. archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
  339. return (ARCHIVE_FATAL);
  340. }
  341. memset(iso9660, 0, sizeof(*iso9660));
  342. iso9660->magic = ISO9660_MAGIC;
  343. r = __archive_read_register_format(a,
  344. iso9660,
  345. "iso9660",
  346. archive_read_format_iso9660_bid,
  347. archive_read_format_iso9660_options,
  348. archive_read_format_iso9660_read_header,
  349. archive_read_format_iso9660_read_data,
  350. archive_read_format_iso9660_read_data_skip,
  351. archive_read_format_iso9660_cleanup);
  352. if (r != ARCHIVE_OK) {
  353. free(iso9660);
  354. return (r);
  355. }
  356. return (ARCHIVE_OK);
  357. }
  358. static int
  359. archive_read_format_iso9660_bid(struct archive_read *a)
  360. {
  361. struct iso9660 *iso9660;
  362. ssize_t bytes_read;
  363. const void *h;
  364. const unsigned char *p;
  365. int seenTerminator;
  366. iso9660 = (struct iso9660 *)(a->format->data);
  367. /*
  368. * Skip the first 32k (reserved area) and get the first
  369. * 8 sectors of the volume descriptor table. Of course,
  370. * if the I/O layer gives us more, we'll take it.
  371. */
  372. #define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
  373. h = __archive_read_ahead(a,
  374. RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
  375. &bytes_read);
  376. if (h == NULL)
  377. return (-1);
  378. p = (const unsigned char *)h;
  379. /* Skip the reserved area. */
  380. bytes_read -= RESERVED_AREA;
  381. p += RESERVED_AREA;
  382. /* Check each volume descriptor. */
  383. seenTerminator = 0;
  384. for (; bytes_read > LOGICAL_BLOCK_SIZE;
  385. bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
  386. int bid = 0;
  387. /* Do not handle undefined Volume Descriptor Type. */
  388. if (p[0] >= 4 && p[0] <= 254)
  389. return (0);
  390. /* Standard Identifier must be "CD001" */
  391. if (memcmp(p + 1, "CD001", 5) != 0)
  392. return (0);
  393. bid += isPVD(iso9660, p);
  394. bid += isJolietSVD(iso9660, p);
  395. if (bid == 0) {
  396. if (isVDSetTerminator(iso9660, p)) {
  397. seenTerminator = 1;
  398. break;
  399. }
  400. return (0);
  401. }
  402. }
  403. /*
  404. * ISO 9660 format must have Primary Volume Descriptor and
  405. * Volume Descriptor Set Terminator.
  406. */
  407. if (seenTerminator && iso9660->primary.sector_number > 16)
  408. return (48);
  409. /* We didn't find a valid PVD; return a bid of zero. */
  410. return (0);
  411. }
  412. static int
  413. archive_read_format_iso9660_options(struct archive_read *a,
  414. const char *key, const char *val)
  415. {
  416. struct iso9660 *iso9660;
  417. iso9660 = (struct iso9660 *)(a->format->data);
  418. if (strcmp(key, "joliet") == 0) {
  419. if (val == NULL || strcmp(val, "off") == 0 ||
  420. strcmp(val, "ignore") == 0 ||
  421. strcmp(val, "disable") == 0 ||
  422. strcmp(val, "0") == 0)
  423. iso9660->option_ignore_joliet = 1;
  424. else
  425. iso9660->option_ignore_joliet = 0;
  426. return (ARCHIVE_OK);
  427. }
  428. if (strcmp(key, "rock-ridge") == 0) {
  429. iso9660->option_ignore_rockridge = val == NULL;
  430. return (ARCHIVE_OK);
  431. }
  432. /* Note: The "warn" return is just to inform the options
  433. * supervisor that we didn't handle it. It will generate
  434. * a suitable error if noone used this option. */
  435. return (ARCHIVE_WARN);
  436. }
  437. static int
  438. isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
  439. {
  440. int i;
  441. (void)iso9660;
  442. /* Type of the Volume Descriptor Set Terminator must be 255. */
  443. if (h[0] != 255)
  444. return (0);
  445. /* Volume Descriptor Version must be 1. */
  446. if (h[6] != 1)
  447. return (0);
  448. /* Reserved field must be 0. */
  449. for (i = 7; i < 2048; ++i)
  450. if (h[i] != 0)
  451. return (0);
  452. return (1);
  453. }
  454. static int
  455. isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
  456. {
  457. const unsigned char *p;
  458. int i;
  459. /* Type 2 means it's a SVD. */
  460. if (h[SVD_type_offset] != 2)
  461. return (0);
  462. /* ID must be "CD001" */
  463. if (memcmp(h + SVD_id_offset, "CD001", 5) != 0)
  464. return (0);
  465. /* Reserved field must be 0. */
  466. for (i = 0; i < SVD_reserved1_size; ++i)
  467. if (h[SVD_reserved1_offset + i] != 0)
  468. return (0);
  469. for (i = 0; i < SVD_reserved2_size; ++i)
  470. if (h[SVD_reserved2_offset + i] != 0)
  471. return (0);
  472. for (i = 0; i < SVD_reserved3_size; ++i)
  473. if (h[SVD_reserved3_offset + i] != 0)
  474. return (0);
  475. /* FIXME: do more validations according to joliet spec. */
  476. /* check if this SVD contains joliet extension! */
  477. p = h + SVD_escape_sequences_offset;
  478. /* N.B. Joliet spec says p[1] == '\\', but.... */
  479. if (p[0] == '%' && p[1] == '/') {
  480. int level = 0;
  481. if (p[2] == '@')
  482. level = 1;
  483. else if (p[2] == 'C')
  484. level = 2;
  485. else if (p[2] == 'E')
  486. level = 3;
  487. else /* not joliet */
  488. return (0);
  489. iso9660->seenJoliet = level;
  490. } else /* not joliet */
  491. return (0);
  492. iso9660->logical_block_size = toi(h + SVD_logical_block_size_offset, 2);
  493. if (iso9660->logical_block_size <= 0)
  494. return (0);
  495. iso9660->volume_block =
  496. archive_le32dec(h + SVD_volume_space_size_offset);
  497. if (iso9660->volume_block <= SYSTEM_AREA_BLOCK+4)
  498. return (0);
  499. iso9660->volume_size = iso9660->logical_block_size
  500. * (uint64_t)iso9660->volume_block;
  501. /* Read Root Directory Record in Volume Descriptor. */
  502. p = h + SVD_root_directory_record_offset;
  503. if (p[DR_length_offset] != 34)
  504. return (0);
  505. iso9660->joliet.sector_number = archive_le32dec(p + DR_extent_offset);
  506. iso9660->joliet.block_size = archive_le32dec(p + DR_size_offset);
  507. return (48);
  508. }
  509. static int
  510. isPVD(struct iso9660 *iso9660, const unsigned char *h)
  511. {
  512. const unsigned char *p;
  513. int i;
  514. /* Type of the Primary Volume Descriptor must be 1. */
  515. if (h[PVD_type_offset] != 1)
  516. return (0);
  517. /* ID must be "CD001" */
  518. if (memcmp(h + PVD_id_offset, "CD001", 5) != 0)
  519. return (0);
  520. /* PVD version must be 1. */
  521. if (h[PVD_version_offset] != 1)
  522. return (0);
  523. /* Reserved field must be 0. */
  524. if (h[PVD_reserved1_offset] != 0)
  525. return (0);
  526. /* Reserved field must be 0. */
  527. for (i = 0; i < PVD_reserved2_size; ++i)
  528. if (h[PVD_reserved2_offset + i] != 0)
  529. return (0);
  530. /* Reserved field must be 0. */
  531. for (i = 0; i < PVD_reserved3_size; ++i)
  532. if (h[PVD_reserved3_offset + i] != 0)
  533. return (0);
  534. /* Logical block size must be > 0. */
  535. /* I've looked at Ecma 119 and can't find any stronger
  536. * restriction on this field. */
  537. iso9660->logical_block_size = toi(h + PVD_logical_block_size_offset, 2);
  538. if (iso9660->logical_block_size <= 0)
  539. return (0);
  540. iso9660->volume_block =
  541. archive_le32dec(h + PVD_volume_space_size_offset);
  542. if (iso9660->volume_block <= SYSTEM_AREA_BLOCK+4)
  543. return (0);
  544. iso9660->volume_size = iso9660->logical_block_size
  545. * (uint64_t)iso9660->volume_block;
  546. /* File structure version must be 1 for ISO9660/ECMA119. */
  547. if (h[PVD_file_structure_version_offset] != 1)
  548. return (0);
  549. /* Reserved field must be 0. */
  550. for (i = 0; i < PVD_reserved4_size; ++i)
  551. if (h[PVD_reserved4_offset + i] != 0)
  552. return (0);
  553. /* Reserved field must be 0. */
  554. for (i = 0; i < PVD_reserved5_size; ++i)
  555. if (h[PVD_reserved5_offset + i] != 0)
  556. return (0);
  557. /* XXX TODO: Check other values for sanity; reject more
  558. * malformed PVDs. XXX */
  559. /* Read Root Directory Record in Volume Descriptor. */
  560. p = h + PVD_root_directory_record_offset;
  561. if (p[DR_length_offset] != 34)
  562. return (0);
  563. iso9660->primary.sector_number = archive_le32dec(p + DR_extent_offset);
  564. iso9660->primary.block_size = archive_le32dec(p + DR_size_offset);
  565. return (48);
  566. }
  567. static int
  568. archive_read_format_iso9660_read_header(struct archive_read *a,
  569. struct archive_entry *entry)
  570. {
  571. struct iso9660 *iso9660;
  572. struct file_info *file;
  573. int r;
  574. iso9660 = (struct iso9660 *)(a->format->data);
  575. if (!a->archive.archive_format) {
  576. a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
  577. a->archive.archive_format_name = "ISO9660";
  578. }
  579. if (iso9660->current_position == 0) {
  580. int64_t skipsize;
  581. struct vd *vd;
  582. const void *block;
  583. char seenJoliet;
  584. vd = &(iso9660->primary);
  585. if (iso9660->seenJoliet &&
  586. vd->sector_number > iso9660->joliet.sector_number)
  587. /* This condition is unlikely; by way of caution. */
  588. vd = &(iso9660->joliet);
  589. skipsize = LOGICAL_BLOCK_SIZE * vd->sector_number;
  590. skipsize = __archive_read_skip(a, skipsize);
  591. if (skipsize < 0)
  592. return ((int)skipsize);
  593. iso9660->current_position = skipsize;
  594. block = __archive_read_ahead(a, vd->block_size, NULL);
  595. if (block == NULL) {
  596. archive_set_error(&a->archive,
  597. ARCHIVE_ERRNO_MISC,
  598. "Failed to read full block when scanning "
  599. "ISO9660 directory list");
  600. return (ARCHIVE_FATAL);
  601. }
  602. /*
  603. * While reading Root Directory, flag seenJoliet
  604. * must be zero to avoid converting special name
  605. * 0x00(Current Directory) and next byte to UCS2.
  606. */
  607. seenJoliet = iso9660->seenJoliet;/* Save flag. */
  608. iso9660->seenJoliet = 0;
  609. file = parse_file_info(a, NULL, block);
  610. if (file == NULL)
  611. return (ARCHIVE_FATAL);
  612. iso9660->seenJoliet = seenJoliet;
  613. if (vd == &(iso9660->primary) && iso9660->seenRockridge
  614. && iso9660->seenJoliet)
  615. /*
  616. * If iso image has RockRidge and Joliet,
  617. * we use RockRidge Extensions.
  618. */
  619. iso9660->seenJoliet = 0;
  620. if (vd == &(iso9660->primary) && !iso9660->seenRockridge
  621. && iso9660->seenJoliet) {
  622. /* Switch reading data from primary to joliet. */
  623. release_file(iso9660, file);
  624. vd = &(iso9660->joliet);
  625. skipsize = LOGICAL_BLOCK_SIZE * vd->sector_number;
  626. skipsize -= LOGICAL_BLOCK_SIZE *
  627. iso9660->primary.sector_number;
  628. skipsize = __archive_read_skip(a, skipsize);
  629. if (skipsize < 0)
  630. return ((int)skipsize);
  631. iso9660->current_position += skipsize;
  632. block = __archive_read_ahead(a, vd->block_size, NULL);
  633. if (block == NULL) {
  634. archive_set_error(&a->archive,
  635. ARCHIVE_ERRNO_MISC,
  636. "Failed to read full block when scanning "
  637. "ISO9660 directory list");
  638. return (ARCHIVE_FATAL);
  639. }
  640. seenJoliet = iso9660->seenJoliet;/* Save flag. */
  641. iso9660->seenJoliet = 0;
  642. file = parse_file_info(a, NULL, block);
  643. if (file == NULL)
  644. return (ARCHIVE_FATAL);
  645. iso9660->seenJoliet = seenJoliet;
  646. }
  647. /* Store the root directory in the pending list. */
  648. add_entry(iso9660, file);
  649. if (iso9660->seenRockridge) {
  650. a->archive.archive_format =
  651. ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
  652. a->archive.archive_format_name =
  653. "ISO9660 with Rockridge extensions";
  654. }
  655. }
  656. /* Get the next entry that appears after the current offset. */
  657. r = next_entry_seek(a, iso9660, &file);
  658. if (r != ARCHIVE_OK) {
  659. release_file(iso9660, file);
  660. return (r);
  661. }
  662. iso9660->entry_bytes_remaining = file->size;
  663. iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
  664. if (file->offset + file->size > iso9660->volume_size) {
  665. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  666. "File is beyond end-of-media: %s", file->name);
  667. iso9660->entry_bytes_remaining = 0;
  668. iso9660->entry_sparse_offset = 0;
  669. release_file(iso9660, file);
  670. return (ARCHIVE_WARN);
  671. }
  672. /* Set up the entry structure with information about this entry. */
  673. archive_entry_set_mode(entry, file->mode);
  674. archive_entry_set_uid(entry, file->uid);
  675. archive_entry_set_gid(entry, file->gid);
  676. archive_entry_set_nlink(entry, file->nlinks);
  677. archive_entry_set_ino(entry, file->inode);
  678. archive_entry_set_birthtime(entry, file->birthtime, 0);
  679. archive_entry_set_mtime(entry, file->mtime, 0);
  680. archive_entry_set_ctime(entry, file->ctime, 0);
  681. archive_entry_set_atime(entry, file->atime, 0);
  682. /* N.B.: Rock Ridge supports 64-bit device numbers. */
  683. archive_entry_set_rdev(entry, (dev_t)file->rdev);
  684. archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
  685. archive_string_empty(&iso9660->pathname);
  686. archive_entry_set_pathname(entry,
  687. build_pathname(&iso9660->pathname, file));
  688. if (file->symlink.s != NULL)
  689. archive_entry_copy_symlink(entry, file->symlink.s);
  690. /* Note: If the input isn't seekable, we can't rewind to
  691. * return the same body again, so if the next entry refers to
  692. * the same data, we have to return it as a hardlink to the
  693. * original entry. */
  694. /* TODO: We have enough information here to compute an
  695. * accurate value for nlinks. We should do so and ignore
  696. * nlinks from the RR extensions. */
  697. if (file->offset == iso9660->previous_offset
  698. && file->size == iso9660->previous_size
  699. && file->size > 0) {
  700. archive_entry_set_hardlink(entry,
  701. iso9660->previous_pathname.s);
  702. archive_entry_unset_size(entry);
  703. iso9660->entry_bytes_remaining = 0;
  704. iso9660->entry_sparse_offset = 0;
  705. release_file(iso9660, file);
  706. return (ARCHIVE_OK);
  707. }
  708. /* Except for the hardlink case above, if the offset of the
  709. * next entry is before our current position, we can't seek
  710. * backwards to extract it, so issue a warning. Note that
  711. * this can only happen if this entry was added to the heap
  712. * after we passed this offset, that is, only if the directory
  713. * mentioning this entry is later than the body of the entry.
  714. * Such layouts are very unusual; most ISO9660 writers lay out
  715. * and record all directory information first, then store
  716. * all file bodies. */
  717. /* TODO: Someday, libarchive's I/O core will support optional
  718. * seeking. When that day comes, this code should attempt to
  719. * seek and only return the error if the seek fails. That
  720. * will give us support for whacky ISO images that require
  721. * seeking while retaining the ability to read almost all ISO
  722. * images in a streaming fashion. */
  723. if (file->offset < iso9660->current_position) {
  724. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  725. "Ignoring out-of-order file @%x (%s) %jd < %jd",
  726. file,
  727. iso9660->pathname.s,
  728. file->offset, iso9660->current_position);
  729. iso9660->entry_bytes_remaining = 0;
  730. iso9660->entry_sparse_offset = 0;
  731. release_file(iso9660, file);
  732. return (ARCHIVE_WARN);
  733. }
  734. /* Initialize zisofs variables. */
  735. iso9660->entry_zisofs.pz = file->pz;
  736. if (file->pz) {
  737. #ifdef HAVE_ZLIB_H
  738. struct zisofs *zisofs;
  739. zisofs = &iso9660->entry_zisofs;
  740. zisofs->initialized = 0;
  741. zisofs->pz_log2_bs = file->pz_log2_bs;
  742. zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
  743. zisofs->pz_offset = 0;
  744. zisofs->header_avail = 0;
  745. zisofs->header_passed = 0;
  746. zisofs->block_pointers_avail = 0;
  747. #endif
  748. archive_entry_set_size(entry, file->pz_uncompressed_size);
  749. }
  750. iso9660->previous_size = file->size;
  751. iso9660->previous_offset = file->offset;
  752. archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
  753. /* If this is a directory, read in all of the entries right now. */
  754. if (archive_entry_filetype(entry) == AE_IFDIR) {
  755. while (iso9660->entry_bytes_remaining > 0) {
  756. const void *block;
  757. const unsigned char *p;
  758. ssize_t step = iso9660->logical_block_size;
  759. if (step > iso9660->entry_bytes_remaining)
  760. step = iso9660->entry_bytes_remaining;
  761. block = __archive_read_ahead(a, step, NULL);
  762. if (block == NULL) {
  763. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  764. "Failed to read full block when scanning ISO9660 directory list");
  765. release_file(iso9660, file);
  766. return (ARCHIVE_FATAL);
  767. }
  768. __archive_read_consume(a, step);
  769. iso9660->current_position += step;
  770. iso9660->entry_bytes_remaining -= step;
  771. for (p = (const unsigned char *)block;
  772. *p != 0 && p < (const unsigned char *)block + step;
  773. p += *p) {
  774. struct file_info *child;
  775. /* N.B.: these special directory identifiers
  776. * are 8 bit "values" even on a
  777. * Joliet CD with UCS-2 (16bit) encoding.
  778. */
  779. /* Skip '.' entry. */
  780. if (*(p + DR_name_len_offset) == 1
  781. && *(p + DR_name_offset) == '\0')
  782. continue;
  783. /* Skip '..' entry. */
  784. if (*(p + DR_name_len_offset) == 1
  785. && *(p + DR_name_offset) == '\001')
  786. continue;
  787. child = parse_file_info(a, file, p);
  788. if (child == NULL) {
  789. release_file(iso9660, file);
  790. return (ARCHIVE_FATAL);
  791. }
  792. add_entry(iso9660, child);
  793. }
  794. }
  795. }
  796. release_file(iso9660, file);
  797. return (ARCHIVE_OK);
  798. }
  799. static int
  800. archive_read_format_iso9660_read_data_skip(struct archive_read *a)
  801. {
  802. /* Because read_next_header always does an explicit skip
  803. * to the next entry, we don't need to do anything here. */
  804. (void)a; /* UNUSED */
  805. return (ARCHIVE_OK);
  806. }
  807. #ifdef HAVE_ZLIB_H
  808. static int
  809. zisofs_read_data(struct archive_read *a,
  810. const void **buff, size_t *size, off_t *offset)
  811. {
  812. struct iso9660 *iso9660;
  813. struct zisofs *zisofs;
  814. const unsigned char *p;
  815. size_t avail;
  816. ssize_t bytes_read;
  817. size_t uncompressed_size;
  818. int r;
  819. iso9660 = (struct iso9660 *)(a->format->data);
  820. zisofs = &iso9660->entry_zisofs;
  821. p = __archive_read_ahead(a, 1, &bytes_read);
  822. if (bytes_read <= 0) {
  823. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  824. "Truncated zisofs file body");
  825. return (ARCHIVE_FATAL);
  826. }
  827. if (bytes_read > iso9660->entry_bytes_remaining)
  828. bytes_read = iso9660->entry_bytes_remaining;
  829. avail = bytes_read;
  830. uncompressed_size = 0;
  831. if (!zisofs->initialized) {
  832. size_t ceil, xsize;
  833. /* Allocate block pointers buffer. */
  834. ceil = (zisofs->pz_uncompressed_size +
  835. (1UL << zisofs->pz_log2_bs) - 1)
  836. >> zisofs->pz_log2_bs;
  837. xsize = (ceil + 1) * 4;
  838. if (zisofs->block_pointers_alloc < xsize) {
  839. size_t alloc;
  840. if (zisofs->block_pointers != NULL)
  841. free(zisofs->block_pointers);
  842. alloc = ((xsize >> 10) + 1) << 10;
  843. zisofs->block_pointers = malloc(alloc);
  844. if (zisofs->block_pointers == NULL) {
  845. archive_set_error(&a->archive, ENOMEM,
  846. "No memory for zisofs decompression");
  847. return (ARCHIVE_FATAL);
  848. }
  849. zisofs->block_pointers_alloc = alloc;
  850. }
  851. zisofs->block_pointers_size = xsize;
  852. /* Allocate uncompressed data buffer. */
  853. xsize = 1UL << zisofs->pz_log2_bs;
  854. if (zisofs->uncompressed_buffer_size < xsize) {
  855. if (zisofs->uncompressed_buffer != NULL)
  856. free(zisofs->uncompressed_buffer);
  857. zisofs->uncompressed_buffer = malloc(xsize);
  858. if (zisofs->uncompressed_buffer == NULL) {
  859. archive_set_error(&a->archive, ENOMEM,
  860. "No memory for zisofs decompression");
  861. return (ARCHIVE_FATAL);
  862. }
  863. }
  864. zisofs->uncompressed_buffer_size = xsize;
  865. /*
  866. * Read the file header, and check the magic code of zisofs.
  867. */
  868. if (zisofs->header_avail < sizeof(zisofs->header)) {
  869. xsize = sizeof(zisofs->header) - zisofs->header_avail;
  870. if (avail < xsize)
  871. xsize = avail;
  872. memcpy(zisofs->header + zisofs->header_avail, p, xsize);
  873. zisofs->header_avail += xsize;
  874. avail -= xsize;
  875. p += xsize;
  876. }
  877. if (!zisofs->header_passed &&
  878. zisofs->header_avail == sizeof(zisofs->header)) {
  879. int err = 0;
  880. if (memcmp(zisofs->header, zisofs_magic,
  881. sizeof(zisofs_magic)) != 0)
  882. err = 1;
  883. if (archive_le32dec(zisofs->header + 8)
  884. != zisofs->pz_uncompressed_size)
  885. err = 1;
  886. if (zisofs->header[12] != 4)
  887. err = 1;
  888. if (zisofs->header[13] != zisofs->pz_log2_bs)
  889. err = 1;
  890. if (err) {
  891. archive_set_error(&a->archive,
  892. ARCHIVE_ERRNO_FILE_FORMAT,
  893. "Illegal zisofs file body");
  894. return (ARCHIVE_FATAL);
  895. }
  896. zisofs->header_passed = 1;
  897. }
  898. /*
  899. * Read block pointers.
  900. */
  901. if (zisofs->header_passed &&
  902. zisofs->block_pointers_avail < zisofs->block_pointers_size) {
  903. xsize = zisofs->block_pointers_size
  904. - zisofs->block_pointers_avail;
  905. if (avail < xsize)
  906. xsize = avail;
  907. memcpy(zisofs->block_pointers
  908. + zisofs->block_pointers_avail, p, xsize);
  909. zisofs->block_pointers_avail += xsize;
  910. avail -= xsize;
  911. p += xsize;
  912. if (zisofs->block_pointers_avail
  913. == zisofs->block_pointers_size) {
  914. /* We've got all block pointers and initialize
  915. * related variables. */
  916. zisofs->block_off = 0;
  917. zisofs->block_avail = 0;
  918. /* Complete a initialization */
  919. zisofs->initialized = 1;
  920. }
  921. }
  922. if (!zisofs->initialized)
  923. goto next_data; /* We need more datas. */
  924. }
  925. /*
  926. * Get block offsets from block pointers.
  927. */
  928. if (zisofs->block_avail == 0) {
  929. uint32_t bst, bed;
  930. if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
  931. /* There isn't a pair of offsets. */
  932. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  933. "Illegal zisofs block pointers");
  934. return (ARCHIVE_FATAL);
  935. }
  936. bst = archive_le32dec(zisofs->block_pointers + zisofs->block_off);
  937. if (bst != zisofs->pz_offset + (bytes_read - avail)) {
  938. /* TODO: Should we seek offset of current file by bst ? */
  939. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  940. "Illegal zisofs block pointers(cannot seek)");
  941. return (ARCHIVE_FATAL);
  942. }
  943. bed = archive_le32dec(
  944. zisofs->block_pointers + zisofs->block_off + 4);
  945. if (bed < bst) {
  946. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  947. "Illegal zisofs block pointers");
  948. return (ARCHIVE_FATAL);
  949. }
  950. zisofs->block_avail = bed - bst;
  951. zisofs->block_off += 4;
  952. /* Initialize compression library for new block. */
  953. if (zisofs->stream_valid)
  954. r = inflateReset(&zisofs->stream);
  955. else
  956. r = inflateInit(&zisofs->stream);
  957. if (r != Z_OK) {
  958. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  959. "Can't initialize zisofs decompression.");
  960. return (ARCHIVE_FATAL);
  961. }
  962. zisofs->stream_valid = 1;
  963. zisofs->stream.total_in = 0;
  964. zisofs->stream.total_out = 0;
  965. }
  966. /*
  967. * Make uncompressed datas.
  968. */
  969. if (zisofs->block_avail == 0) {
  970. memset(zisofs->uncompressed_buffer, 0,
  971. zisofs->uncompressed_buffer_size);
  972. uncompressed_size = zisofs->uncompressed_buffer_size;
  973. } else {
  974. zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
  975. if (avail > zisofs->block_avail)
  976. zisofs->stream.avail_in = zisofs->block_avail;
  977. else
  978. zisofs->stream.avail_in = avail;
  979. zisofs->stream.next_out = zisofs->uncompressed_buffer;
  980. zisofs->stream.avail_out = zisofs->uncompressed_buffer_size;
  981. r = inflate(&zisofs->stream, 0);
  982. switch (r) {
  983. case Z_OK: /* Decompressor made some progress.*/
  984. case Z_STREAM_END: /* Found end of stream. */
  985. break;
  986. default:
  987. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  988. "zisofs decompression failed (%d)", r);
  989. return (ARCHIVE_FATAL);
  990. }
  991. uncompressed_size =
  992. zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
  993. avail -= zisofs->stream.next_in - p;
  994. zisofs->block_avail -= zisofs->stream.next_in - p;
  995. }
  996. next_data:
  997. bytes_read -= avail;
  998. *buff = zisofs->uncompressed_buffer;
  999. *size = uncompressed_size;
  1000. *offset = iso9660->entry_sparse_offset;
  1001. iso9660->entry_sparse_offset += uncompressed_size;
  1002. iso9660->entry_bytes_remaining -= bytes_read;
  1003. iso9660->current_position += bytes_read;
  1004. zisofs->pz_offset += bytes_read;
  1005. __archive_read_consume(a, bytes_read);
  1006. return (ARCHIVE_OK);
  1007. }
  1008. #else /* HAVE_ZLIB_H */
  1009. static int
  1010. zisofs_read_data(struct archive_read *a,
  1011. const void **buff, size_t *size, off_t *offset)
  1012. {
  1013. (void)buff;/* UNUSED */
  1014. (void)size;/* UNUSED */
  1015. (void)offset;/* UNUSED */
  1016. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1017. "zisofs is not supported on this platform.");
  1018. return (ARCHIVE_FATAL);
  1019. }
  1020. #endif /* HAVE_ZLIB_H */
  1021. static int
  1022. archive_read_format_iso9660_read_data(struct archive_read *a,
  1023. const void **buff, size_t *size, off_t *offset)
  1024. {
  1025. ssize_t bytes_read;
  1026. struct iso9660 *iso9660;
  1027. iso9660 = (struct iso9660 *)(a->format->data);
  1028. if (iso9660->entry_bytes_remaining <= 0) {
  1029. *buff = NULL;
  1030. *size = 0;
  1031. *offset = iso9660->entry_sparse_offset;
  1032. return (ARCHIVE_EOF);
  1033. }
  1034. if (iso9660->entry_zisofs.pz)
  1035. return (zisofs_read_data(a, buff, size, offset));
  1036. *buff = __archive_read_ahead(a, 1, &bytes_read);
  1037. if (bytes_read == 0)
  1038. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1039. "Truncated input file");
  1040. if (*buff == NULL)
  1041. return (ARCHIVE_FATAL);
  1042. if (bytes_read > iso9660->entry_bytes_remaining)
  1043. bytes_read = iso9660->entry_bytes_remaining;
  1044. *size = bytes_read;
  1045. *offset = iso9660->entry_sparse_offset;
  1046. iso9660->entry_sparse_offset += bytes_read;
  1047. iso9660->entry_bytes_remaining -= bytes_read;
  1048. iso9660->current_position += bytes_read;
  1049. __archive_read_consume(a, bytes_read);
  1050. return (ARCHIVE_OK);
  1051. }
  1052. static int
  1053. archive_read_format_iso9660_cleanup(struct archive_read *a)
  1054. {
  1055. struct iso9660 *iso9660;
  1056. struct file_info *file;
  1057. int r = ARCHIVE_OK;
  1058. iso9660 = (struct iso9660 *)(a->format->data);
  1059. while ((file = next_entry(iso9660)) != NULL)
  1060. release_file(iso9660, file);
  1061. archive_string_free(&iso9660->pathname);
  1062. archive_string_free(&iso9660->previous_pathname);
  1063. if (iso9660->pending_files)
  1064. free(iso9660->pending_files);
  1065. #ifdef HAVE_ZLIB_H
  1066. free(iso9660->entry_zisofs.uncompressed_buffer);
  1067. free(iso9660->entry_zisofs.block_pointers);
  1068. if (iso9660->entry_zisofs.stream_valid) {
  1069. if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
  1070. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1071. "Failed to clean up zlib decompressor");
  1072. r = ARCHIVE_FATAL;
  1073. }
  1074. }
  1075. #endif
  1076. free(iso9660);
  1077. (a->format->data) = NULL;
  1078. return (r);
  1079. }
  1080. /*
  1081. * This routine parses a single ISO directory record, makes sense
  1082. * of any extensions, and stores the result in memory.
  1083. */
  1084. static struct file_info *
  1085. parse_file_info(struct archive_read *a, struct file_info *parent,
  1086. const unsigned char *isodirrec)
  1087. {
  1088. struct iso9660 *iso9660;
  1089. struct file_info *file;
  1090. size_t name_len;
  1091. const unsigned char *rr_start, *rr_end;
  1092. const unsigned char *p;
  1093. size_t dr_len;
  1094. int32_t offset;
  1095. int flags;
  1096. iso9660 = (struct iso9660 *)(a->format->data);
  1097. dr_len = (size_t)isodirrec[DR_length_offset];
  1098. name_len = (size_t)isodirrec[DR_name_len_offset];
  1099. offset = archive_le32dec(isodirrec + DR_extent_offset);
  1100. /* Sanity check that dr_len needs at least 34. */
  1101. if (dr_len < 34) {
  1102. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1103. "Invalid length of directory record");
  1104. return (NULL);
  1105. }
  1106. /* Sanity check that name_len doesn't exceed dr_len. */
  1107. if (dr_len - 33 < name_len) {
  1108. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1109. "Invalid length of file identifier");
  1110. return (NULL);
  1111. }
  1112. /* Sanity check that offset doesn't exceed volume block.
  1113. * Don't check lower limit of offset; it's possibillty the offset
  1114. * has negative value when file type is symbolic link or
  1115. * file size is zero. As far as I know latest mkisofs do that.
  1116. */
  1117. if (offset >= iso9660->volume_block) {
  1118. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1119. "Invalid location of extent of file");
  1120. return (NULL);
  1121. }
  1122. /* Create a new file entry and copy data from the ISO dir record. */
  1123. file = (struct file_info *)malloc(sizeof(*file));
  1124. if (file == NULL) {
  1125. archive_set_error(&a->archive, ENOMEM,
  1126. "No memory for file entry");
  1127. return (NULL);
  1128. }
  1129. memset(file, 0, sizeof(*file));
  1130. file->parent = parent;
  1131. if (parent != NULL)
  1132. parent->refcount++;
  1133. file->offset = iso9660->logical_block_size * (uint64_t)offset;
  1134. file->size = toi(isodirrec + DR_size_offset, DR_size_size);
  1135. file->mtime = isodate7(isodirrec + DR_date_offset);
  1136. file->ctime = file->atime = file->mtime;
  1137. p = isodirrec + DR_name_offset;
  1138. /* Rockridge extensions (if any) follow name. Compute this
  1139. * before fidgeting the name_len below. */
  1140. rr_start = p + name_len + (name_len & 1 ? 0 : 1) + iso9660->suspOffset;
  1141. rr_end = isodirrec + isodirrec[DR_length_offset];
  1142. if (iso9660->seenJoliet) {
  1143. /* Joliet names are max 64 chars (128 bytes) according to spec,
  1144. * but genisoimage/mkisofs allows recording longer Joliet
  1145. * names which are 103 UCS2 characters(206 bytes) by their
  1146. * option '-joliet-long'.
  1147. */
  1148. wchar_t wbuff[103+1], *wp;
  1149. const unsigned char *c;
  1150. if (name_len > 206)
  1151. name_len = 206;
  1152. /* convert BE UTF-16 to wchar_t */
  1153. for (c = p, wp = wbuff;
  1154. c < (p + name_len) &&
  1155. wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
  1156. c += 2) {
  1157. *wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
  1158. }
  1159. *wp = L'\0';
  1160. #if 0 /* untested code, is it at all useful on Joliet? */
  1161. /* trim trailing first version and dot from filename.
  1162. *
  1163. * Remember we where in UTF-16BE land!
  1164. * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
  1165. * 16 bits big endian characters on Joliet.
  1166. *
  1167. * TODO: sanitize filename?
  1168. * Joliet allows any UCS-2 char except:
  1169. * *, /, :, ;, ? and \.
  1170. */
  1171. /* Chop off trailing ';1' from files. */
  1172. if (*(wp-2) == ';' && *(wp-1) == '1') {
  1173. wp-=2;
  1174. *wp = L'\0';
  1175. }
  1176. /* Chop off trailing '.' from filenames. */
  1177. if (*(wp-1) == '.')
  1178. *(--wp) = L'\0';
  1179. #endif
  1180. /* store the result in the file name field. */
  1181. archive_strappend_w_utf8(&file->name, wbuff);
  1182. } else {
  1183. /* Chop off trailing ';1' from files. */
  1184. if (name_len > 2 && p[name_len - 2] == ';' &&
  1185. p[name_len - 1] == '1')
  1186. name_len -= 2;
  1187. /* Chop off trailing '.' from filenames. */
  1188. if (name_len > 1 && p[name_len - 1] == '.')
  1189. --name_len;
  1190. archive_strncpy(&file->name, (const char *)p, name_len);
  1191. }
  1192. flags = isodirrec[DR_flags_offset];
  1193. if (flags & 0x02)
  1194. file->mode = AE_IFDIR | 0700;
  1195. else
  1196. file->mode = AE_IFREG | 0400;
  1197. if (!iso9660->option_ignore_rockridge)
  1198. /* Rockridge extensions overwrite information from above. */
  1199. parse_rockridge(iso9660, file, rr_start, rr_end);
  1200. #if DEBUG
  1201. /* DEBUGGING: Warn about attributes I don't yet fully support. */
  1202. if ((flags & ~0x02) != 0) {
  1203. fprintf(stderr, "\n ** Unrecognized flag: ");
  1204. dump_isodirrec(stderr, isodirrec);
  1205. fprintf(stderr, "\n");
  1206. } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
  1207. fprintf(stderr, "\n ** Unrecognized sequence number: ");
  1208. dump_isodirrec(stderr, isodirrec);
  1209. fprintf(stderr, "\n");
  1210. } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
  1211. fprintf(stderr, "\n ** Unexpected file unit size: ");
  1212. dump_isodirrec(stderr, isodirrec);
  1213. fprintf(stderr, "\n");
  1214. } else if (*(isodirrec + DR_interleave_offset) != 0) {
  1215. fprintf(stderr, "\n ** Unexpected interleave: ");
  1216. dump_isodirrec(stderr, isodirrec);
  1217. fprintf(stderr, "\n");
  1218. } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
  1219. fprintf(stderr, "\n ** Unexpected extended attribute length: ");
  1220. dump_isodirrec(stderr, isodirrec);
  1221. fprintf(stderr, "\n");
  1222. }
  1223. #endif
  1224. return (file);
  1225. }
  1226. static void
  1227. add_entry(struct iso9660 *iso9660, struct file_info *file)
  1228. {
  1229. uint64_t file_offset, parent_offset;
  1230. int hole, parent;
  1231. /* Expand our pending files list as necessary. */
  1232. if (iso9660->pending_files_used >= iso9660->pending_files_allocated) {
  1233. struct file_info **new_pending_files;
  1234. int new_size = iso9660->pending_files_allocated * 2;
  1235. if (iso9660->pending_files_allocated < 1024)
  1236. new_size = 1024;
  1237. /* Overflow might keep us from growing the list. */
  1238. if (new_size <= iso9660->pending_files_allocated)
  1239. __archive_errx(1, "Out of memory");
  1240. new_pending_files = (struct file_info **)malloc(new_size * sizeof(new_pending_files[0]));
  1241. if (new_pending_files == NULL)
  1242. __archive_errx(1, "Out of memory");
  1243. memcpy(new_pending_files, iso9660->pending_files,
  1244. iso9660->pending_files_allocated * sizeof(new_pending_files[0]));
  1245. if (iso9660->pending_files != NULL)
  1246. free(iso9660->pending_files);
  1247. iso9660->pending_files = new_pending_files;
  1248. iso9660->pending_files_allocated = new_size;
  1249. }
  1250. file_offset = file->offset + file->size;
  1251. /*
  1252. * Start with hole at end, walk it up tree to find insertion point.
  1253. */
  1254. hole = iso9660->pending_files_used++;
  1255. while (hole > 0) {
  1256. parent = (hole - 1)/2;
  1257. parent_offset = iso9660->pending_files[parent]->offset
  1258. + iso9660->pending_files[parent]->size;
  1259. if (file_offset >= parent_offset) {
  1260. iso9660->pending_files[hole] = file;
  1261. return;
  1262. }
  1263. /* Move parent into hole <==> move hole up tree. */
  1264. iso9660->pending_files[hole] = iso9660->pending_files[parent];
  1265. hole = parent;
  1266. }
  1267. iso9660->pending_files[0] = file;
  1268. }
  1269. static void
  1270. parse_rockridge(struct iso9660 *iso9660, struct file_info *file,
  1271. const unsigned char *p, const unsigned char *end)
  1272. {
  1273. (void)iso9660; /* UNUSED */
  1274. file->name_continues = 0;
  1275. file->symlink_continues = 0;
  1276. while (p + 4 < end /* Enough space for another entry. */
  1277. && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
  1278. && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
  1279. && p[2] >= 4 /* Sanity-check length. */
  1280. && p + p[2] <= end) { /* Sanity-check length. */
  1281. const unsigned char *data = p + 4;
  1282. int data_length = p[2] - 4;
  1283. int version = p[3];
  1284. /*
  1285. * Yes, each 'if' here does test p[0] again.
  1286. * Otherwise, the fall-through handling to catch
  1287. * unsupported extensions doesn't work.
  1288. */
  1289. switch(p[0]) {
  1290. case 'C':
  1291. if (p[0] == 'C' && p[1] == 'E') {
  1292. if (version == 1 && data_length == 24) {
  1293. /*
  1294. * CE extension comprises:
  1295. * 8 byte sector containing extension
  1296. * 8 byte offset w/in above sector
  1297. * 8 byte length of continuation
  1298. */
  1299. file->ce_offset = (uint64_t)toi(data, 4)
  1300. * iso9660->logical_block_size
  1301. + toi(data + 8, 4);
  1302. file->ce_size = toi(data + 16, 4);
  1303. /* If the result is rediculous,
  1304. * ignore it. */
  1305. if (file->ce_offset + file->ce_size
  1306. > iso9660->volume_size) {
  1307. file->ce_offset = 0;
  1308. file->ce_size = 0;
  1309. }
  1310. }
  1311. break;
  1312. }
  1313. /* FALLTHROUGH */
  1314. case 'N':
  1315. if (p[0] == 'N' && p[1] == 'M') {
  1316. if (version == 1)
  1317. parse_rockridge_NM1(file,
  1318. data, data_length);
  1319. break;
  1320. }
  1321. /* FALLTHROUGH */
  1322. case 'P':
  1323. if (p[0] == 'P' && p[1] == 'D') {
  1324. /*
  1325. * PD extension is padding;
  1326. * contents are always ignored.
  1327. */
  1328. break;
  1329. }
  1330. if (p[0] == 'P' && p[1] == 'N') {
  1331. if (version == 1 && data_length == 16) {
  1332. file->rdev = toi(data,4);
  1333. file->rdev <<= 32;
  1334. file->rdev |= toi(data + 8, 4);
  1335. }
  1336. break;
  1337. }
  1338. if (p[0] == 'P' && p[1] == 'X') {
  1339. /*
  1340. * PX extension comprises:
  1341. * 8 bytes for mode,
  1342. * 8 bytes for nlinks,
  1343. * 8 bytes for uid,
  1344. * 8 bytes for gid,
  1345. * 8 bytes for inode.
  1346. */
  1347. if (version == 1) {
  1348. if (data_length >= 8)
  1349. file->mode
  1350. = toi(data, 4);
  1351. if (data_length >= 16)
  1352. file->nlinks
  1353. = toi(data + 8, 4);
  1354. if (data_length >= 24)
  1355. file->uid
  1356. = toi(data + 16, 4);
  1357. if (data_length >= 32)
  1358. file->gid
  1359. = toi(data + 24, 4);
  1360. if (data_length >= 40)
  1361. file->inode
  1362. = toi(data + 32, 4);
  1363. }
  1364. break;
  1365. }
  1366. /* FALLTHROUGH */
  1367. case 'R':
  1368. if (p[0] == 'R' && p[1] == 'R' && version == 1) {
  1369. iso9660->seenRockridge = 1;
  1370. /*
  1371. * RR extension comprises:
  1372. * one byte flag value
  1373. */
  1374. /* TODO: Handle RR extension. */
  1375. break;
  1376. }
  1377. /* FALLTHROUGH */
  1378. case 'S':
  1379. if (p[0] == 'S' && p[1] == 'L') {
  1380. if (version == 1)
  1381. parse_rockridge_SL1(file,
  1382. data, data_length);
  1383. break;
  1384. }
  1385. if (p[0] == 'S' && p[1] == 'P'
  1386. && version == 1 && data_length == 3
  1387. && data[0] == (unsigned char)'\xbe'
  1388. && data[1] == (unsigned char)'\xef') {
  1389. /*
  1390. * SP extension stores the suspOffset
  1391. * (Number of bytes to skip between
  1392. * filename and SUSP records.)
  1393. * It is mandatory by the SUSP standard
  1394. * (IEEE 1281).
  1395. *
  1396. * It allows SUSP to coexist with
  1397. * non-SUSP uses of the System
  1398. * Use Area by placing non-SUSP data
  1399. * before SUSP data.
  1400. *
  1401. * TODO: Add a check for 'SP' in
  1402. * first directory entry, disable all SUSP
  1403. * processing if not found.
  1404. */
  1405. iso9660->suspOffset = data[2];
  1406. break;
  1407. }
  1408. if (p[0] == 'S' && p[1] == 'T'
  1409. && data_length == 0 && version == 1) {
  1410. /*
  1411. * ST extension marks end of this
  1412. * block of SUSP entries.
  1413. *
  1414. * It allows SUSP to coexist with
  1415. * non-SUSP uses of the System
  1416. * Use Area by placing non-SUSP data
  1417. * after SUSP data.
  1418. */
  1419. return;
  1420. }
  1421. case 'T':
  1422. if (p[0] == 'T' && p[1] == 'F') {
  1423. if (version == 1)
  1424. parse_rockridge_TF1(file,
  1425. data, data_length);
  1426. break;
  1427. }
  1428. /* FALLTHROUGH */
  1429. case 'Z':
  1430. if (p[0] == 'Z' && p[1] == 'F') {
  1431. if (version == 1)
  1432. parse_rockridge_ZF1(file,
  1433. data, data_length);
  1434. break;
  1435. }
  1436. /* FALLTHROUGH */
  1437. default:
  1438. /* The FALLTHROUGHs above leave us here for
  1439. * any unsupported extension. */
  1440. #if DEBUG
  1441. {
  1442. const unsigned char *t;
  1443. fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name.s);
  1444. fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length);
  1445. for (t = data; t < data + data_length && t < data + 16; t++)
  1446. fprintf(stderr, " %02x", *t);
  1447. fprintf(stderr, "\n");
  1448. }
  1449. #endif
  1450. break;
  1451. }
  1452. p += p[2];
  1453. }
  1454. }
  1455. static void
  1456. parse_rockridge_NM1(struct file_info *file,
  1457. const unsigned char *data, int data_length)
  1458. {
  1459. if (!file->name_continues)
  1460. archive_string_empty(&file->name);
  1461. file->name_continues = 0;
  1462. if (data_length < 1)
  1463. return;
  1464. /*
  1465. * NM version 1 extension comprises:
  1466. * 1 byte flag, value is one of:
  1467. * = 0: remainder is name
  1468. * = 1: remainder is name, next NM entry continues name
  1469. * = 2: "."
  1470. * = 4: ".."
  1471. * = 32: Implementation specific
  1472. * All other values are reserved.
  1473. */
  1474. switch(data[0]) {
  1475. case 0:
  1476. if (data_length < 2)
  1477. return;
  1478. archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
  1479. break;
  1480. case 1:
  1481. if (data_length < 2)
  1482. return;
  1483. archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
  1484. file->name_continues = 1;
  1485. break;
  1486. case 2:
  1487. archive_strcat(&file->name, ".");
  1488. break;
  1489. case 4:
  1490. archive_strcat(&file->name, "..");
  1491. break;
  1492. default:
  1493. return;
  1494. }
  1495. }
  1496. static void
  1497. parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
  1498. int data_length)
  1499. {
  1500. char flag;
  1501. /*
  1502. * TF extension comprises:
  1503. * one byte flag
  1504. * create time (optional)
  1505. * modify time (optional)
  1506. * access time (optional)
  1507. * attribute time (optional)
  1508. * Time format and presence of fields
  1509. * is controlled by flag bits.
  1510. */
  1511. if (data_length < 1)
  1512. return;
  1513. flag = data[0];
  1514. ++data;
  1515. --data_length;
  1516. if (flag & 0x80) {
  1517. /* Use 17-byte time format. */
  1518. if ((flag & 1) && data_length >= 17) {
  1519. /* Create time. */
  1520. file->birthtime = isodate17(data);
  1521. data += 17;
  1522. data_length -= 17;
  1523. }
  1524. if ((flag & 2) && data_length >= 17) {
  1525. /* Modify time. */
  1526. file->mtime = isodate17(data);
  1527. data += 17;
  1528. data_length -= 17;
  1529. }
  1530. if ((flag & 4) && data_length >= 17) {
  1531. /* Access time. */
  1532. file->atime = isodate17(data);
  1533. data += 17;
  1534. data_length -= 17;
  1535. }
  1536. if ((flag & 8) && data_length >= 17) {
  1537. /* Attribute change time. */
  1538. file->ctime = isodate17(data);
  1539. data += 17;
  1540. data_length -= 17;
  1541. }
  1542. } else {
  1543. /* Use 7-byte time format. */
  1544. if ((flag & 1) && data_length >= 7) {
  1545. /* Create time. */
  1546. file->birthtime = isodate17(data);
  1547. data += 7;
  1548. data_length -= 7;
  1549. }
  1550. if ((flag & 2) && data_length >= 7) {
  1551. /* Modify time. */
  1552. file->mtime = isodate7(data);
  1553. data += 7;
  1554. data_length -= 7;
  1555. }
  1556. if ((flag & 4) && data_length >= 7) {
  1557. /* Access time. */
  1558. file->atime = isodate7(data);
  1559. data += 7;
  1560. data_length -= 7;
  1561. }
  1562. if ((flag & 8) && data_length >= 7) {
  1563. /* Attribute change time. */
  1564. file->ctime = isodate7(data);
  1565. data += 7;
  1566. data_length -= 7;
  1567. }
  1568. }
  1569. }
  1570. static void
  1571. parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
  1572. int data_length)
  1573. {
  1574. const char *separator = "";
  1575. if (!file->symlink_continues || file->symlink.length < 1)
  1576. archive_string_empty(&file->symlink);
  1577. else if (file->symlink.s[file->symlink.length - 1] != '/')
  1578. separator = "/";
  1579. file->symlink_continues = 0;
  1580. /*
  1581. * Defined flag values:
  1582. * 0: This is the last SL record for this symbolic link
  1583. * 1: this symbolic link field continues in next SL entry
  1584. * All other values are reserved.
  1585. */
  1586. if (data_length < 1)
  1587. return;
  1588. switch(*data) {
  1589. case 0:
  1590. break;
  1591. case 1:
  1592. file->symlink_continues = 1;
  1593. break;
  1594. default:
  1595. return;
  1596. }
  1597. ++data; /* Skip flag byte. */
  1598. --data_length;
  1599. /*
  1600. * SL extension body stores "components".
  1601. * Basically, this is a complicated way of storing
  1602. * a POSIX path. It also interferes with using
  1603. * symlinks for storing non-path data. <sigh>
  1604. *
  1605. * Each component is 2 bytes (flag and length)
  1606. * possibly followed by name data.
  1607. */
  1608. while (data_length >= 2) {
  1609. unsigned char flag = *data++;
  1610. unsigned char nlen = *data++;
  1611. data_length -= 2;
  1612. archive_strcat(&file->symlink, separator);
  1613. separator = "/";
  1614. switch(flag) {
  1615. case 0: /* Usual case, this is text. */
  1616. if (data_length < nlen)
  1617. return;
  1618. archive_strncat(&file->symlink,
  1619. (const char *)data, nlen);
  1620. break;
  1621. case 0x01: /* Text continues in next component. */
  1622. if (data_length < nlen)
  1623. return;
  1624. archive_strncat(&file->symlink,
  1625. (const char *)data, nlen);
  1626. separator = "";
  1627. break;
  1628. case 0x02: /* Current dir. */
  1629. archive_strcat(&file->symlink, ".");
  1630. break;
  1631. case 0x04: /* Parent dir. */
  1632. archive_strcat(&file->symlink, "..");
  1633. break;
  1634. case 0x08: /* Root of filesystem. */
  1635. archive_strcat(&file->symlink, "/");
  1636. separator = "";
  1637. break;
  1638. case 0x10: /* Undefined (historically "volume root" */
  1639. archive_string_empty(&file->symlink);
  1640. archive_strcat(&file->symlink, "ROOT");
  1641. break;
  1642. case 0x20: /* Undefined (historically "hostname") */
  1643. archive_strcat(&file->symlink, "hostname");
  1644. break;
  1645. default:
  1646. /* TODO: issue a warning ? */
  1647. return;
  1648. }
  1649. data += nlen;
  1650. data_length -= nlen;
  1651. }
  1652. }
  1653. static void
  1654. parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
  1655. int data_length)
  1656. {
  1657. if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
  1658. /* paged zlib */
  1659. file->pz = 1;
  1660. file->pz_log2_bs = data[3];
  1661. file->pz_uncompressed_size = archive_le32dec(&data[4]);
  1662. }
  1663. }
  1664. static void
  1665. release_file(struct iso9660 *iso9660, struct file_info *file)
  1666. {
  1667. struct file_info *parent;
  1668. if (file == NULL)
  1669. return;
  1670. if (file->refcount == 0) {
  1671. parent = file->parent;
  1672. archive_string_free(&file->name);
  1673. archive_string_free(&file->symlink);
  1674. free(file);
  1675. if (parent != NULL) {
  1676. parent->refcount--;
  1677. release_file(iso9660, parent);
  1678. }
  1679. }
  1680. }
  1681. static int
  1682. next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
  1683. struct file_info **pfile)
  1684. {
  1685. struct file_info *file;
  1686. uint64_t offset;
  1687. *pfile = NULL;
  1688. for (;;) {
  1689. *pfile = file = next_entry(iso9660);
  1690. if (file == NULL)
  1691. return (ARCHIVE_EOF);
  1692. /* CE area precedes actual file data? Ignore it. */
  1693. if (file->ce_offset > file->offset) {
  1694. /* fprintf(stderr, " *** Discarding CE data.\n"); */
  1695. file->ce_offset = 0;
  1696. file->ce_size = 0;
  1697. }
  1698. /* Don't waste time seeking for zero-length bodies. */
  1699. if (file->size == 0) {
  1700. file->offset = iso9660->current_position;
  1701. }
  1702. /* If CE exists, find and read it now. */
  1703. if (file->ce_offset > 0)
  1704. offset = file->ce_offset;
  1705. else
  1706. offset = file->offset;
  1707. /* Seek forward to the start of the entry. */
  1708. if (iso9660->current_position < offset) {
  1709. off_t step = offset - iso9660->current_position;
  1710. off_t bytes_read;
  1711. bytes_read = __archive_read_skip(a, step);
  1712. if (bytes_read < 0)
  1713. return (bytes_read);
  1714. iso9660->current_position = offset;
  1715. }
  1716. /* We found body of file; handle it now. */
  1717. if (offset == file->offset)
  1718. return (ARCHIVE_OK);
  1719. /* Found CE? Process it and push the file back onto list. */
  1720. if (offset == file->ce_offset) {
  1721. const void *p;
  1722. ssize_t size = file->ce_size;
  1723. const unsigned char *rr_start;
  1724. file->ce_offset = 0;
  1725. file->ce_size = 0;
  1726. p = __archive_read_ahead(a, size, NULL);
  1727. if (p == NULL)
  1728. return (ARCHIVE_FATAL);
  1729. rr_start = (const unsigned char *)p;
  1730. parse_rockridge(iso9660, file, rr_start,
  1731. rr_start + size);
  1732. __archive_read_consume(a, size);
  1733. iso9660->current_position += size;
  1734. add_entry(iso9660, file);
  1735. }
  1736. }
  1737. }
  1738. static struct file_info *
  1739. next_entry(struct iso9660 *iso9660)
  1740. {
  1741. uint64_t a_offset, b_offset, c_offset;
  1742. int a, b, c;
  1743. struct file_info *r, *tmp;
  1744. if (iso9660->pending_files_used < 1)
  1745. return (NULL);
  1746. /*
  1747. * The first file in the list is the earliest; we'll return this.
  1748. */
  1749. r = iso9660->pending_files[0];
  1750. /*
  1751. * Move the last item in the heap to the root of the tree
  1752. */
  1753. iso9660->pending_files[0]
  1754. = iso9660->pending_files[--iso9660->pending_files_used];
  1755. /*
  1756. * Rebalance the heap.
  1757. */
  1758. a = 0; /* Starting element and its offset */
  1759. a_offset = iso9660->pending_files[a]->offset
  1760. + iso9660->pending_files[a]->size;
  1761. for (;;) {
  1762. b = a + a + 1; /* First child */
  1763. if (b >= iso9660->pending_files_used)
  1764. return (r);
  1765. b_offset = iso9660->pending_files[b]->offset
  1766. + iso9660->pending_files[b]->size;
  1767. c = b + 1; /* Use second child if it is smaller. */
  1768. if (c < iso9660->pending_files_used) {
  1769. c_offset = iso9660->pending_files[c]->offset
  1770. + iso9660->pending_files[c]->size;
  1771. if (c_offset < b_offset) {
  1772. b = c;
  1773. b_offset = c_offset;
  1774. }
  1775. }
  1776. if (a_offset <= b_offset)
  1777. return (r);
  1778. tmp = iso9660->pending_files[a];
  1779. iso9660->pending_files[a] = iso9660->pending_files[b];
  1780. iso9660->pending_files[b] = tmp;
  1781. a = b;
  1782. }
  1783. }
  1784. static unsigned int
  1785. toi(const void *p, int n)
  1786. {
  1787. const unsigned char *v = (const unsigned char *)p;
  1788. if (n > 1)
  1789. return v[0] + 256 * toi(v + 1, n - 1);
  1790. if (n == 1)
  1791. return v[0];
  1792. return (0);
  1793. }
  1794. static time_t
  1795. isodate7(const unsigned char *v)
  1796. {
  1797. struct tm tm;
  1798. int offset;
  1799. memset(&tm, 0, sizeof(tm));
  1800. tm.tm_year = v[0];
  1801. tm.tm_mon = v[1] - 1;
  1802. tm.tm_mday = v[2];
  1803. tm.tm_hour = v[3];
  1804. tm.tm_min = v[4];
  1805. tm.tm_sec = v[5];
  1806. /* v[6] is the signed timezone offset, in 1/4-hour increments. */
  1807. offset = ((const signed char *)v)[6];
  1808. if (offset > -48 && offset < 52) {
  1809. tm.tm_hour -= offset / 4;
  1810. tm.tm_min -= (offset % 4) * 15;
  1811. }
  1812. return (time_from_tm(&tm));
  1813. }
  1814. static time_t
  1815. isodate17(const unsigned char *v)
  1816. {
  1817. struct tm tm;
  1818. int offset;
  1819. memset(&tm, 0, sizeof(tm));
  1820. tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
  1821. + (v[2] - '0') * 10 + (v[3] - '0')
  1822. - 1900;
  1823. tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
  1824. tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
  1825. tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
  1826. tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
  1827. tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
  1828. /* v[16] is the signed timezone offset, in 1/4-hour increments. */
  1829. offset = ((const signed char *)v)[16];
  1830. if (offset > -48 && offset < 52) {
  1831. tm.tm_hour -= offset / 4;
  1832. tm.tm_min -= (offset % 4) * 15;
  1833. }
  1834. return (time_from_tm(&tm));
  1835. }
  1836. static time_t
  1837. time_from_tm(struct tm *t)
  1838. {
  1839. #if HAVE_TIMEGM
  1840. /* Use platform timegm() if available. */
  1841. return (timegm(t));
  1842. #else
  1843. /* Else use direct calculation using POSIX assumptions. */
  1844. /* First, fix up tm_yday based on the year/month/day. */
  1845. mktime(t);
  1846. /* Then we can compute timegm() from first principles. */
  1847. return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
  1848. + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
  1849. + ((t->tm_year - 69) / 4) * 86400 -
  1850. ((t->tm_year - 1) / 100) * 86400
  1851. + ((t->tm_year + 299) / 400) * 86400);
  1852. #endif
  1853. }
  1854. static const char *
  1855. build_pathname(struct archive_string *as, struct file_info *file)
  1856. {
  1857. if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
  1858. build_pathname(as, file->parent);
  1859. archive_strcat(as, "/");
  1860. }
  1861. if (archive_strlen(&file->name) == 0)
  1862. archive_strcat(as, ".");
  1863. else
  1864. archive_string_concat(as, &file->name);
  1865. return (as->s);
  1866. }
  1867. #if DEBUG
  1868. static void
  1869. dump_isodirrec(FILE *out, const unsigned char *isodirrec)
  1870. {
  1871. fprintf(out, " l %d,",
  1872. toi(isodirrec + DR_length_offset, DR_length_size));
  1873. fprintf(out, " a %d,",
  1874. toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
  1875. fprintf(out, " ext 0x%x,",
  1876. toi(isodirrec + DR_extent_offset, DR_extent_size));
  1877. fprintf(out, " s %d,",
  1878. toi(isodirrec + DR_size_offset, DR_extent_size));
  1879. fprintf(out, " f 0x%02x,",
  1880. toi(isodirrec + DR_flags_offset, DR_flags_size));
  1881. fprintf(out, " u %d,",
  1882. toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
  1883. fprintf(out, " ilv %d,",
  1884. toi(isodirrec + DR_interleave_offset, DR_interleave_size));
  1885. fprintf(out, " seq %d,",
  1886. toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
  1887. fprintf(out, " nl %d:",
  1888. toi(isodirrec + DR_name_len_offset, DR_name_len_size));
  1889. fprintf(out, " `%.*s'",
  1890. toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
  1891. }
  1892. #endif