archive_read_support_format_iso9660.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2009 Andreas Henriksson <[email protected]>
  4. * Copyright (c) 2009-2012 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: head/lib/libarchive/archive_read_support_format_iso9660.c 201246 2009-12-30 05:30:35Z kientzle $");
  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_entry_locale.h"
  48. #include "archive_private.h"
  49. #include "archive_read_private.h"
  50. #include "archive_string.h"
  51. /*
  52. * An overview of ISO 9660 format:
  53. *
  54. * Each disk is laid out as follows:
  55. * * 32k reserved for private use
  56. * * Volume descriptor table. Each volume descriptor
  57. * is 2k and specifies basic format information.
  58. * The "Primary Volume Descriptor" (PVD) is defined by the
  59. * standard and should always be present; other volume
  60. * descriptors include various vendor-specific extensions.
  61. * * Files and directories. Each file/dir is specified by
  62. * an "extent" (starting sector and length in bytes).
  63. * Dirs are just files with directory records packed one
  64. * after another. The PVD contains a single dir entry
  65. * specifying the location of the root directory. Everything
  66. * else follows from there.
  67. *
  68. * This module works by first reading the volume descriptors, then
  69. * building a list of directory entries, sorted by starting
  70. * sector. At each step, I look for the earliest dir entry that
  71. * hasn't yet been read, seek forward to that location and read
  72. * that entry. If it's a dir, I slurp in the new dir entries and
  73. * add them to the heap; if it's a regular file, I return the
  74. * corresponding archive_entry and wait for the client to request
  75. * the file body. This strategy allows us to read most compliant
  76. * CDs with a single pass through the data, as required by libarchive.
  77. */
  78. #define LOGICAL_BLOCK_SIZE 2048
  79. #define SYSTEM_AREA_BLOCK 16
  80. /* Structure of on-disk primary volume descriptor. */
  81. #define PVD_type_offset 0
  82. #define PVD_type_size 1
  83. #define PVD_id_offset (PVD_type_offset + PVD_type_size)
  84. #define PVD_id_size 5
  85. #define PVD_version_offset (PVD_id_offset + PVD_id_size)
  86. #define PVD_version_size 1
  87. #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
  88. #define PVD_reserved1_size 1
  89. #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
  90. #define PVD_system_id_size 32
  91. #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
  92. #define PVD_volume_id_size 32
  93. #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
  94. #define PVD_reserved2_size 8
  95. #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
  96. #define PVD_volume_space_size_size 8
  97. #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
  98. #define PVD_reserved3_size 32
  99. #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
  100. #define PVD_volume_set_size_size 4
  101. #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
  102. #define PVD_volume_sequence_number_size 4
  103. #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
  104. #define PVD_logical_block_size_size 4
  105. #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
  106. #define PVD_path_table_size_size 8
  107. #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
  108. #define PVD_type_1_path_table_size 4
  109. #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
  110. #define PVD_opt_type_1_path_table_size 4
  111. #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
  112. #define PVD_type_m_path_table_size 4
  113. #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
  114. #define PVD_opt_type_m_path_table_size 4
  115. #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
  116. #define PVD_root_directory_record_size 34
  117. #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
  118. #define PVD_volume_set_id_size 128
  119. #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
  120. #define PVD_publisher_id_size 128
  121. #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
  122. #define PVD_preparer_id_size 128
  123. #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
  124. #define PVD_application_id_size 128
  125. #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
  126. #define PVD_copyright_file_id_size 37
  127. #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
  128. #define PVD_abstract_file_id_size 37
  129. #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
  130. #define PVD_bibliographic_file_id_size 37
  131. #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
  132. #define PVD_creation_date_size 17
  133. #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
  134. #define PVD_modification_date_size 17
  135. #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
  136. #define PVD_expiration_date_size 17
  137. #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
  138. #define PVD_effective_date_size 17
  139. #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
  140. #define PVD_file_structure_version_size 1
  141. #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
  142. #define PVD_reserved4_size 1
  143. #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
  144. #define PVD_application_data_size 512
  145. #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
  146. #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
  147. /* TODO: It would make future maintenance easier to just hardcode the
  148. * above values. In particular, ECMA119 states the offsets as part of
  149. * the standard. That would eliminate the need for the following check.*/
  150. #if PVD_reserved5_offset != 1395
  151. #error PVD offset and size definitions are wrong.
  152. #endif
  153. /* Structure of optional on-disk supplementary volume descriptor. */
  154. #define SVD_type_offset 0
  155. #define SVD_type_size 1
  156. #define SVD_id_offset (SVD_type_offset + SVD_type_size)
  157. #define SVD_id_size 5
  158. #define SVD_version_offset (SVD_id_offset + SVD_id_size)
  159. #define SVD_version_size 1
  160. /* ... */
  161. #define SVD_reserved1_offset 72
  162. #define SVD_reserved1_size 8
  163. #define SVD_volume_space_size_offset 80
  164. #define SVD_volume_space_size_size 8
  165. #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
  166. #define SVD_escape_sequences_size 32
  167. /* ... */
  168. #define SVD_logical_block_size_offset 128
  169. #define SVD_logical_block_size_size 4
  170. #define SVD_type_L_path_table_offset 140
  171. #define SVD_type_M_path_table_offset 148
  172. /* ... */
  173. #define SVD_root_directory_record_offset 156
  174. #define SVD_root_directory_record_size 34
  175. #define SVD_file_structure_version_offset 881
  176. #define SVD_reserved2_offset 882
  177. #define SVD_reserved2_size 1
  178. #define SVD_reserved3_offset 1395
  179. #define SVD_reserved3_size 653
  180. /* ... */
  181. /* FIXME: validate correctness of last SVD entry offset. */
  182. /* Structure of an on-disk directory record. */
  183. /* Note: ISO9660 stores each multi-byte integer twice, once in
  184. * each byte order. The sizes here are the size of just one
  185. * of the two integers. (This is why the offset of a field isn't
  186. * the same as the offset+size of the previous field.) */
  187. #define DR_length_offset 0
  188. #define DR_length_size 1
  189. #define DR_ext_attr_length_offset 1
  190. #define DR_ext_attr_length_size 1
  191. #define DR_extent_offset 2
  192. #define DR_extent_size 4
  193. #define DR_size_offset 10
  194. #define DR_size_size 4
  195. #define DR_date_offset 18
  196. #define DR_date_size 7
  197. #define DR_flags_offset 25
  198. #define DR_flags_size 1
  199. #define DR_file_unit_size_offset 26
  200. #define DR_file_unit_size_size 1
  201. #define DR_interleave_offset 27
  202. #define DR_interleave_size 1
  203. #define DR_volume_sequence_number_offset 28
  204. #define DR_volume_sequence_number_size 2
  205. #define DR_name_len_offset 32
  206. #define DR_name_len_size 1
  207. #define DR_name_offset 33
  208. #ifdef HAVE_ZLIB_H
  209. static const unsigned char zisofs_magic[8] = {
  210. 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
  211. };
  212. struct zisofs {
  213. /* Set 1 if this file compressed by paged zlib */
  214. int pz;
  215. int pz_log2_bs; /* Log2 of block size */
  216. uint64_t pz_uncompressed_size;
  217. int initialized;
  218. unsigned char *uncompressed_buffer;
  219. size_t uncompressed_buffer_size;
  220. uint32_t pz_offset;
  221. unsigned char header[16];
  222. size_t header_avail;
  223. int header_passed;
  224. unsigned char *block_pointers;
  225. size_t block_pointers_alloc;
  226. size_t block_pointers_size;
  227. size_t block_pointers_avail;
  228. size_t block_off;
  229. uint32_t block_avail;
  230. z_stream stream;
  231. int stream_valid;
  232. };
  233. #else
  234. struct zisofs {
  235. /* Set 1 if this file compressed by paged zlib */
  236. int pz;
  237. };
  238. #endif
  239. struct content {
  240. uint64_t offset;/* Offset on disk. */
  241. uint64_t size; /* File size in bytes. */
  242. struct content *next;
  243. };
  244. /* In-memory storage for a directory record. */
  245. struct file_info {
  246. struct file_info *use_next;
  247. struct file_info *parent;
  248. struct file_info *next;
  249. struct file_info *re_next;
  250. int subdirs;
  251. uint64_t key; /* Heap Key. */
  252. uint64_t offset; /* Offset on disk. */
  253. uint64_t size; /* File size in bytes. */
  254. uint32_t ce_offset; /* Offset of CE. */
  255. uint32_t ce_size; /* Size of CE. */
  256. char rr_moved; /* Flag to rr_moved. */
  257. char rr_moved_has_re_only;
  258. char re; /* Having RRIP "RE" extension. */
  259. char re_descendant;
  260. uint64_t cl_offset; /* Having RRIP "CL" extension. */
  261. int birthtime_is_set;
  262. time_t birthtime; /* File created time. */
  263. time_t mtime; /* File last modified time. */
  264. time_t atime; /* File last accessed time. */
  265. time_t ctime; /* File attribute change time. */
  266. uint64_t rdev; /* Device number. */
  267. mode_t mode;
  268. uid_t uid;
  269. gid_t gid;
  270. int64_t number;
  271. int nlinks;
  272. struct archive_string name; /* Pathname */
  273. unsigned char *utf16be_name;
  274. size_t utf16be_bytes;
  275. char name_continues; /* Non-zero if name continues */
  276. struct archive_string symlink;
  277. char symlink_continues; /* Non-zero if link continues */
  278. /* Set 1 if this file compressed by paged zlib(zisofs) */
  279. int pz;
  280. int pz_log2_bs; /* Log2 of block size */
  281. uint64_t pz_uncompressed_size;
  282. /* Set 1 if this file is multi extent. */
  283. int multi_extent;
  284. struct {
  285. struct content *first;
  286. struct content **last;
  287. } contents;
  288. struct {
  289. struct file_info *first;
  290. struct file_info **last;
  291. } rede_files;
  292. };
  293. struct heap_queue {
  294. struct file_info **files;
  295. int allocated;
  296. int used;
  297. };
  298. struct iso9660 {
  299. int magic;
  300. #define ISO9660_MAGIC 0x96609660
  301. int opt_support_joliet;
  302. int opt_support_rockridge;
  303. struct archive_string pathname;
  304. char seenRockridge; /* Set true if RR extensions are used. */
  305. char seenSUSP; /* Set true if SUSP is beging used. */
  306. char seenJoliet;
  307. unsigned char suspOffset;
  308. struct file_info *rr_moved;
  309. struct read_ce_queue {
  310. struct read_ce_req {
  311. uint64_t offset;/* Offset of CE on disk. */
  312. struct file_info *file;
  313. } *reqs;
  314. int cnt;
  315. int allocated;
  316. } read_ce_req;
  317. int64_t previous_number;
  318. struct archive_string previous_pathname;
  319. struct file_info *use_files;
  320. struct heap_queue pending_files;
  321. struct {
  322. struct file_info *first;
  323. struct file_info **last;
  324. } cache_files;
  325. struct {
  326. struct file_info *first;
  327. struct file_info **last;
  328. } re_files;
  329. uint64_t current_position;
  330. ssize_t logical_block_size;
  331. uint64_t volume_size; /* Total size of volume in bytes. */
  332. int32_t volume_block;/* Total size of volume in logical blocks. */
  333. struct vd {
  334. int location; /* Location of Extent. */
  335. uint32_t size;
  336. } primary, joliet;
  337. int64_t entry_sparse_offset;
  338. int64_t entry_bytes_remaining;
  339. size_t entry_bytes_unconsumed;
  340. struct zisofs entry_zisofs;
  341. struct content *entry_content;
  342. struct archive_string_conv *sconv_utf16be;
  343. /*
  344. * Buffers for a full pathname in UTF-16BE in Joliet extensions.
  345. */
  346. #define UTF16_NAME_MAX 1024
  347. unsigned char *utf16be_path;
  348. size_t utf16be_path_len;
  349. unsigned char *utf16be_previous_path;
  350. size_t utf16be_previous_path_len;
  351. /* Null buufer used in bidder to improve its performance. */
  352. unsigned char null[2048];
  353. };
  354. static int archive_read_format_iso9660_bid(struct archive_read *, int);
  355. static int archive_read_format_iso9660_options(struct archive_read *,
  356. const char *, const char *);
  357. static int archive_read_format_iso9660_cleanup(struct archive_read *);
  358. static int archive_read_format_iso9660_read_data(struct archive_read *,
  359. const void **, size_t *, int64_t *);
  360. static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
  361. static int archive_read_format_iso9660_read_header(struct archive_read *,
  362. struct archive_entry *);
  363. static const char *build_pathname(struct archive_string *, struct file_info *);
  364. static int build_pathname_utf16be(unsigned char *, size_t, size_t *,
  365. struct file_info *);
  366. #if DEBUG
  367. static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
  368. #endif
  369. static time_t time_from_tm(struct tm *);
  370. static time_t isodate17(const unsigned char *);
  371. static time_t isodate7(const unsigned char *);
  372. static int isBootRecord(struct iso9660 *, const unsigned char *);
  373. static int isVolumePartition(struct iso9660 *, const unsigned char *);
  374. static int isVDSetTerminator(struct iso9660 *, const unsigned char *);
  375. static int isJolietSVD(struct iso9660 *, const unsigned char *);
  376. static int isSVD(struct iso9660 *, const unsigned char *);
  377. static int isEVD(struct iso9660 *, const unsigned char *);
  378. static int isPVD(struct iso9660 *, const unsigned char *);
  379. static int next_cache_entry(struct archive_read *, struct iso9660 *,
  380. struct file_info **);
  381. static int next_entry_seek(struct archive_read *, struct iso9660 *,
  382. struct file_info **);
  383. static struct file_info *
  384. parse_file_info(struct archive_read *a,
  385. struct file_info *parent, const unsigned char *isodirrec);
  386. static int parse_rockridge(struct archive_read *a,
  387. struct file_info *file, const unsigned char *start,
  388. const unsigned char *end);
  389. static int register_CE(struct archive_read *a, int32_t location,
  390. struct file_info *file);
  391. static int read_CE(struct archive_read *a, struct iso9660 *iso9660);
  392. static void parse_rockridge_NM1(struct file_info *,
  393. const unsigned char *, int);
  394. static void parse_rockridge_SL1(struct file_info *,
  395. const unsigned char *, int);
  396. static void parse_rockridge_TF1(struct file_info *,
  397. const unsigned char *, int);
  398. static void parse_rockridge_ZF1(struct file_info *,
  399. const unsigned char *, int);
  400. static void register_file(struct iso9660 *, struct file_info *);
  401. static void release_files(struct iso9660 *);
  402. static unsigned toi(const void *p, int n);
  403. static inline void re_add_entry(struct iso9660 *, struct file_info *);
  404. static inline struct file_info * re_get_entry(struct iso9660 *);
  405. static inline int rede_add_entry(struct file_info *);
  406. static inline struct file_info * rede_get_entry(struct file_info *);
  407. static inline void cache_add_entry(struct iso9660 *iso9660,
  408. struct file_info *file);
  409. static inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
  410. static int heap_add_entry(struct archive_read *a, struct heap_queue *heap,
  411. struct file_info *file, uint64_t key);
  412. static struct file_info *heap_get_entry(struct heap_queue *heap);
  413. #define add_entry(arch, iso9660, file) \
  414. heap_add_entry(arch, &((iso9660)->pending_files), file, file->offset)
  415. #define next_entry(iso9660) \
  416. heap_get_entry(&((iso9660)->pending_files))
  417. int
  418. archive_read_support_format_iso9660(struct archive *_a)
  419. {
  420. struct archive_read *a = (struct archive_read *)_a;
  421. struct iso9660 *iso9660;
  422. int r;
  423. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  424. ARCHIVE_STATE_NEW, "archive_read_support_format_iso9660");
  425. iso9660 = (struct iso9660 *)calloc(1, sizeof(*iso9660));
  426. if (iso9660 == NULL) {
  427. archive_set_error(&a->archive, ENOMEM,
  428. "Can't allocate iso9660 data");
  429. return (ARCHIVE_FATAL);
  430. }
  431. iso9660->magic = ISO9660_MAGIC;
  432. iso9660->cache_files.first = NULL;
  433. iso9660->cache_files.last = &(iso9660->cache_files.first);
  434. iso9660->re_files.first = NULL;
  435. iso9660->re_files.last = &(iso9660->re_files.first);
  436. /* Enable to support Joliet extensions by default. */
  437. iso9660->opt_support_joliet = 1;
  438. /* Enable to support Rock Ridge extensions by default. */
  439. iso9660->opt_support_rockridge = 1;
  440. r = __archive_read_register_format(a,
  441. iso9660,
  442. "iso9660",
  443. archive_read_format_iso9660_bid,
  444. archive_read_format_iso9660_options,
  445. archive_read_format_iso9660_read_header,
  446. archive_read_format_iso9660_read_data,
  447. archive_read_format_iso9660_read_data_skip,
  448. NULL,
  449. archive_read_format_iso9660_cleanup,
  450. NULL,
  451. NULL);
  452. if (r != ARCHIVE_OK) {
  453. free(iso9660);
  454. return (r);
  455. }
  456. return (ARCHIVE_OK);
  457. }
  458. static int
  459. archive_read_format_iso9660_bid(struct archive_read *a, int best_bid)
  460. {
  461. struct iso9660 *iso9660;
  462. ssize_t bytes_read;
  463. const unsigned char *p;
  464. int seenTerminator;
  465. /* If there's already a better bid than we can ever
  466. make, don't bother testing. */
  467. if (best_bid > 48)
  468. return (-1);
  469. iso9660 = (struct iso9660 *)(a->format->data);
  470. /*
  471. * Skip the first 32k (reserved area) and get the first
  472. * 8 sectors of the volume descriptor table. Of course,
  473. * if the I/O layer gives us more, we'll take it.
  474. */
  475. #define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
  476. p = __archive_read_ahead(a,
  477. RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
  478. &bytes_read);
  479. if (p == NULL)
  480. return (-1);
  481. /* Skip the reserved area. */
  482. bytes_read -= RESERVED_AREA;
  483. p += RESERVED_AREA;
  484. /* Check each volume descriptor. */
  485. seenTerminator = 0;
  486. for (; bytes_read > LOGICAL_BLOCK_SIZE;
  487. bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
  488. /* Do not handle undefined Volume Descriptor Type. */
  489. if (p[0] >= 4 && p[0] <= 254)
  490. return (0);
  491. /* Standard Identifier must be "CD001" */
  492. if (memcmp(p + 1, "CD001", 5) != 0)
  493. return (0);
  494. if (isPVD(iso9660, p))
  495. continue;
  496. if (!iso9660->joliet.location) {
  497. if (isJolietSVD(iso9660, p))
  498. continue;
  499. }
  500. if (isBootRecord(iso9660, p))
  501. continue;
  502. if (isEVD(iso9660, p))
  503. continue;
  504. if (isSVD(iso9660, p))
  505. continue;
  506. if (isVolumePartition(iso9660, p))
  507. continue;
  508. if (isVDSetTerminator(iso9660, p)) {
  509. seenTerminator = 1;
  510. break;
  511. }
  512. return (0);
  513. }
  514. /*
  515. * ISO 9660 format must have Primary Volume Descriptor and
  516. * Volume Descriptor Set Terminator.
  517. */
  518. if (seenTerminator && iso9660->primary.location > 16)
  519. return (48);
  520. /* We didn't find a valid PVD; return a bid of zero. */
  521. return (0);
  522. }
  523. static int
  524. archive_read_format_iso9660_options(struct archive_read *a,
  525. const char *key, const char *val)
  526. {
  527. struct iso9660 *iso9660;
  528. iso9660 = (struct iso9660 *)(a->format->data);
  529. if (strcmp(key, "joliet") == 0) {
  530. if (val == NULL || strcmp(val, "off") == 0 ||
  531. strcmp(val, "ignore") == 0 ||
  532. strcmp(val, "disable") == 0 ||
  533. strcmp(val, "0") == 0)
  534. iso9660->opt_support_joliet = 0;
  535. else
  536. iso9660->opt_support_joliet = 1;
  537. return (ARCHIVE_OK);
  538. }
  539. if (strcmp(key, "rockridge") == 0 ||
  540. strcmp(key, "Rockridge") == 0) {
  541. iso9660->opt_support_rockridge = val != NULL;
  542. return (ARCHIVE_OK);
  543. }
  544. /* Note: The "warn" return is just to inform the options
  545. * supervisor that we didn't handle it. It will generate
  546. * a suitable error if no one used this option. */
  547. return (ARCHIVE_WARN);
  548. }
  549. static int
  550. isNull(struct iso9660 *iso9660, const unsigned char *h, unsigned offset,
  551. unsigned bytes)
  552. {
  553. while (bytes >= sizeof(iso9660->null)) {
  554. if (!memcmp(iso9660->null, h + offset, sizeof(iso9660->null)))
  555. return (0);
  556. offset += sizeof(iso9660->null);
  557. bytes -= sizeof(iso9660->null);
  558. }
  559. if (bytes)
  560. return memcmp(iso9660->null, h + offset, bytes) == 0;
  561. else
  562. return (1);
  563. }
  564. static int
  565. isBootRecord(struct iso9660 *iso9660, const unsigned char *h)
  566. {
  567. (void)iso9660; /* UNUSED */
  568. /* Type of the Volume Descriptor Boot Record must be 0. */
  569. if (h[0] != 0)
  570. return (0);
  571. /* Volume Descriptor Version must be 1. */
  572. if (h[6] != 1)
  573. return (0);
  574. return (1);
  575. }
  576. static int
  577. isVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
  578. {
  579. int32_t location;
  580. /* Type of the Volume Partition Descriptor must be 3. */
  581. if (h[0] != 3)
  582. return (0);
  583. /* Volume Descriptor Version must be 1. */
  584. if (h[6] != 1)
  585. return (0);
  586. /* Unused Field */
  587. if (h[7] != 0)
  588. return (0);
  589. location = archive_le32dec(h + 72);
  590. if (location <= SYSTEM_AREA_BLOCK ||
  591. location >= iso9660->volume_block)
  592. return (0);
  593. if ((uint32_t)location != archive_be32dec(h + 76))
  594. return (0);
  595. return (1);
  596. }
  597. static int
  598. isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
  599. {
  600. (void)iso9660; /* UNUSED */
  601. /* Type of the Volume Descriptor Set Terminator must be 255. */
  602. if (h[0] != 255)
  603. return (0);
  604. /* Volume Descriptor Version must be 1. */
  605. if (h[6] != 1)
  606. return (0);
  607. /* Reserved field must be 0. */
  608. if (!isNull(iso9660, h, 7, 2048-7))
  609. return (0);
  610. return (1);
  611. }
  612. static int
  613. isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
  614. {
  615. const unsigned char *p;
  616. ssize_t logical_block_size;
  617. int32_t volume_block;
  618. /* Check if current sector is a kind of Supplementary Volume
  619. * Descriptor. */
  620. if (!isSVD(iso9660, h))
  621. return (0);
  622. /* FIXME: do more validations according to joliet spec. */
  623. /* check if this SVD contains joliet extension! */
  624. p = h + SVD_escape_sequences_offset;
  625. /* N.B. Joliet spec says p[1] == '\\', but.... */
  626. if (p[0] == '%' && p[1] == '/') {
  627. int level = 0;
  628. if (p[2] == '@')
  629. level = 1;
  630. else if (p[2] == 'C')
  631. level = 2;
  632. else if (p[2] == 'E')
  633. level = 3;
  634. else /* not joliet */
  635. return (0);
  636. iso9660->seenJoliet = level;
  637. } else /* not joliet */
  638. return (0);
  639. logical_block_size =
  640. archive_le16dec(h + SVD_logical_block_size_offset);
  641. volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
  642. iso9660->logical_block_size = logical_block_size;
  643. iso9660->volume_block = volume_block;
  644. iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
  645. /* Read Root Directory Record in Volume Descriptor. */
  646. p = h + SVD_root_directory_record_offset;
  647. iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
  648. iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
  649. return (48);
  650. }
  651. static int
  652. isSVD(struct iso9660 *iso9660, const unsigned char *h)
  653. {
  654. const unsigned char *p;
  655. ssize_t logical_block_size;
  656. int32_t volume_block;
  657. int32_t location;
  658. (void)iso9660; /* UNUSED */
  659. /* Type 2 means it's a SVD. */
  660. if (h[SVD_type_offset] != 2)
  661. return (0);
  662. /* Reserved field must be 0. */
  663. if (!isNull(iso9660, h, SVD_reserved1_offset, SVD_reserved1_size))
  664. return (0);
  665. if (!isNull(iso9660, h, SVD_reserved2_offset, SVD_reserved2_size))
  666. return (0);
  667. if (!isNull(iso9660, h, SVD_reserved3_offset, SVD_reserved3_size))
  668. return (0);
  669. /* File structure version must be 1 for ISO9660/ECMA119. */
  670. if (h[SVD_file_structure_version_offset] != 1)
  671. return (0);
  672. logical_block_size =
  673. archive_le16dec(h + SVD_logical_block_size_offset);
  674. if (logical_block_size <= 0)
  675. return (0);
  676. volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
  677. if (volume_block <= SYSTEM_AREA_BLOCK+4)
  678. return (0);
  679. /* Location of Occurrence of Type L Path Table must be
  680. * available location,
  681. * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  682. location = archive_le32dec(h+SVD_type_L_path_table_offset);
  683. if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
  684. return (0);
  685. /* The Type M Path Table must be at a valid location (WinISO
  686. * and probably other programs omit this, so we allow zero)
  687. *
  688. * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  689. location = archive_be32dec(h+SVD_type_M_path_table_offset);
  690. if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
  691. || location >= volume_block)
  692. return (0);
  693. /* Read Root Directory Record in Volume Descriptor. */
  694. p = h + SVD_root_directory_record_offset;
  695. if (p[DR_length_offset] != 34)
  696. return (0);
  697. return (48);
  698. }
  699. static int
  700. isEVD(struct iso9660 *iso9660, const unsigned char *h)
  701. {
  702. const unsigned char *p;
  703. ssize_t logical_block_size;
  704. int32_t volume_block;
  705. int32_t location;
  706. (void)iso9660; /* UNUSED */
  707. /* Type of the Enhanced Volume Descriptor must be 2. */
  708. if (h[PVD_type_offset] != 2)
  709. return (0);
  710. /* EVD version must be 2. */
  711. if (h[PVD_version_offset] != 2)
  712. return (0);
  713. /* Reserved field must be 0. */
  714. if (h[PVD_reserved1_offset] != 0)
  715. return (0);
  716. /* Reserved field must be 0. */
  717. if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
  718. return (0);
  719. /* Reserved field must be 0. */
  720. if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
  721. return (0);
  722. /* Logical block size must be > 0. */
  723. /* I've looked at Ecma 119 and can't find any stronger
  724. * restriction on this field. */
  725. logical_block_size =
  726. archive_le16dec(h + PVD_logical_block_size_offset);
  727. if (logical_block_size <= 0)
  728. return (0);
  729. volume_block =
  730. archive_le32dec(h + PVD_volume_space_size_offset);
  731. if (volume_block <= SYSTEM_AREA_BLOCK+4)
  732. return (0);
  733. /* File structure version must be 2 for ISO9660:1999. */
  734. if (h[PVD_file_structure_version_offset] != 2)
  735. return (0);
  736. /* Location of Occurrence of Type L Path Table must be
  737. * available location,
  738. * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  739. location = archive_le32dec(h+PVD_type_1_path_table_offset);
  740. if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
  741. return (0);
  742. /* Location of Occurrence of Type M Path Table must be
  743. * available location,
  744. * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  745. location = archive_be32dec(h+PVD_type_m_path_table_offset);
  746. if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
  747. || location >= volume_block)
  748. return (0);
  749. /* Reserved field must be 0. */
  750. if (!isNull(iso9660, h, PVD_reserved4_offset, PVD_reserved4_size))
  751. return (0);
  752. /* Reserved field must be 0. */
  753. if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
  754. return (0);
  755. /* Read Root Directory Record in Volume Descriptor. */
  756. p = h + PVD_root_directory_record_offset;
  757. if (p[DR_length_offset] != 34)
  758. return (0);
  759. return (48);
  760. }
  761. static int
  762. isPVD(struct iso9660 *iso9660, const unsigned char *h)
  763. {
  764. const unsigned char *p;
  765. ssize_t logical_block_size;
  766. int32_t volume_block;
  767. int32_t location;
  768. int i;
  769. /* Type of the Primary Volume Descriptor must be 1. */
  770. if (h[PVD_type_offset] != 1)
  771. return (0);
  772. /* PVD version must be 1. */
  773. if (h[PVD_version_offset] != 1)
  774. return (0);
  775. /* Reserved field must be 0. */
  776. if (h[PVD_reserved1_offset] != 0)
  777. return (0);
  778. /* Reserved field must be 0. */
  779. if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
  780. return (0);
  781. /* Reserved field must be 0. */
  782. if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
  783. return (0);
  784. /* Logical block size must be > 0. */
  785. /* I've looked at Ecma 119 and can't find any stronger
  786. * restriction on this field. */
  787. logical_block_size =
  788. archive_le16dec(h + PVD_logical_block_size_offset);
  789. if (logical_block_size <= 0)
  790. return (0);
  791. volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
  792. if (volume_block <= SYSTEM_AREA_BLOCK+4)
  793. return (0);
  794. /* File structure version must be 1 for ISO9660/ECMA119. */
  795. if (h[PVD_file_structure_version_offset] != 1)
  796. return (0);
  797. /* Location of Occurrence of Type L Path Table must be
  798. * available location,
  799. * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  800. location = archive_le32dec(h+PVD_type_1_path_table_offset);
  801. if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
  802. return (0);
  803. /* The Type M Path Table must also be at a valid location
  804. * (although ECMA 119 requires a Type M Path Table, WinISO and
  805. * probably other programs omit it, so we permit a zero here)
  806. *
  807. * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
  808. location = archive_be32dec(h+PVD_type_m_path_table_offset);
  809. if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
  810. || location >= volume_block)
  811. return (0);
  812. /* Reserved field must be 0. */
  813. /* But accept NetBSD/FreeBSD "makefs" images with 0x20 here. */
  814. for (i = 0; i < PVD_reserved4_size; ++i)
  815. if (h[PVD_reserved4_offset + i] != 0
  816. && h[PVD_reserved4_offset + i] != 0x20)
  817. return (0);
  818. /* Reserved field must be 0. */
  819. if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
  820. return (0);
  821. /* XXX TODO: Check other values for sanity; reject more
  822. * malformed PVDs. XXX */
  823. /* Read Root Directory Record in Volume Descriptor. */
  824. p = h + PVD_root_directory_record_offset;
  825. if (p[DR_length_offset] != 34)
  826. return (0);
  827. if (!iso9660->primary.location) {
  828. iso9660->logical_block_size = logical_block_size;
  829. iso9660->volume_block = volume_block;
  830. iso9660->volume_size =
  831. logical_block_size * (uint64_t)volume_block;
  832. iso9660->primary.location =
  833. archive_le32dec(p + DR_extent_offset);
  834. iso9660->primary.size = archive_le32dec(p + DR_size_offset);
  835. }
  836. return (48);
  837. }
  838. static int
  839. read_children(struct archive_read *a, struct file_info *parent)
  840. {
  841. struct iso9660 *iso9660;
  842. const unsigned char *b, *p;
  843. struct file_info *multi;
  844. size_t step, skip_size;
  845. iso9660 = (struct iso9660 *)(a->format->data);
  846. /* flush any remaining bytes from the last round to ensure
  847. * we're positioned */
  848. if (iso9660->entry_bytes_unconsumed) {
  849. __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
  850. iso9660->entry_bytes_unconsumed = 0;
  851. }
  852. if (iso9660->current_position > parent->offset) {
  853. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  854. "Ignoring out-of-order directory (%s) %jd > %jd",
  855. parent->name.s,
  856. (intmax_t)iso9660->current_position,
  857. (intmax_t)parent->offset);
  858. return (ARCHIVE_WARN);
  859. }
  860. if (parent->offset + parent->size > iso9660->volume_size) {
  861. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  862. "Directory is beyond end-of-media: %s",
  863. parent->name.s);
  864. return (ARCHIVE_WARN);
  865. }
  866. if (iso9660->current_position < parent->offset) {
  867. int64_t skipsize;
  868. skipsize = parent->offset - iso9660->current_position;
  869. skipsize = __archive_read_consume(a, skipsize);
  870. if (skipsize < 0)
  871. return ((int)skipsize);
  872. iso9660->current_position = parent->offset;
  873. }
  874. step = (size_t)(((parent->size + iso9660->logical_block_size -1) /
  875. iso9660->logical_block_size) * iso9660->logical_block_size);
  876. b = __archive_read_ahead(a, step, NULL);
  877. if (b == NULL) {
  878. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  879. "Failed to read full block when scanning "
  880. "ISO9660 directory list");
  881. return (ARCHIVE_FATAL);
  882. }
  883. iso9660->current_position += step;
  884. multi = NULL;
  885. skip_size = step;
  886. while (step) {
  887. p = b;
  888. b += iso9660->logical_block_size;
  889. step -= iso9660->logical_block_size;
  890. for (; *p != 0 && p < b && p + *p <= b; p += *p) {
  891. struct file_info *child;
  892. /* N.B.: these special directory identifiers
  893. * are 8 bit "values" even on a
  894. * Joliet CD with UCS-2 (16bit) encoding.
  895. */
  896. /* Skip '.' entry. */
  897. if (*(p + DR_name_len_offset) == 1
  898. && *(p + DR_name_offset) == '\0')
  899. continue;
  900. /* Skip '..' entry. */
  901. if (*(p + DR_name_len_offset) == 1
  902. && *(p + DR_name_offset) == '\001')
  903. continue;
  904. child = parse_file_info(a, parent, p);
  905. if (child == NULL) {
  906. __archive_read_consume(a, skip_size);
  907. return (ARCHIVE_FATAL);
  908. }
  909. if (child->cl_offset == 0 &&
  910. (child->multi_extent || multi != NULL)) {
  911. struct content *con;
  912. if (multi == NULL) {
  913. multi = child;
  914. multi->contents.first = NULL;
  915. multi->contents.last =
  916. &(multi->contents.first);
  917. }
  918. con = malloc(sizeof(struct content));
  919. if (con == NULL) {
  920. archive_set_error(
  921. &a->archive, ENOMEM,
  922. "No memory for multi extent");
  923. __archive_read_consume(a, skip_size);
  924. return (ARCHIVE_FATAL);
  925. }
  926. con->offset = child->offset;
  927. con->size = child->size;
  928. con->next = NULL;
  929. *multi->contents.last = con;
  930. multi->contents.last = &(con->next);
  931. if (multi == child) {
  932. if (add_entry(a, iso9660, child)
  933. != ARCHIVE_OK)
  934. return (ARCHIVE_FATAL);
  935. } else {
  936. multi->size += child->size;
  937. if (!child->multi_extent)
  938. multi = NULL;
  939. }
  940. } else
  941. if (add_entry(a, iso9660, child) != ARCHIVE_OK)
  942. return (ARCHIVE_FATAL);
  943. }
  944. }
  945. __archive_read_consume(a, skip_size);
  946. /* Read data which recorded by RRIP "CE" extension. */
  947. if (read_CE(a, iso9660) != ARCHIVE_OK)
  948. return (ARCHIVE_FATAL);
  949. return (ARCHIVE_OK);
  950. }
  951. static int
  952. choose_volume(struct archive_read *a, struct iso9660 *iso9660)
  953. {
  954. struct file_info *file;
  955. int64_t skipsize;
  956. struct vd *vd;
  957. const void *block;
  958. char seenJoliet;
  959. vd = &(iso9660->primary);
  960. if (!iso9660->opt_support_joliet)
  961. iso9660->seenJoliet = 0;
  962. if (iso9660->seenJoliet &&
  963. vd->location > iso9660->joliet.location)
  964. /* This condition is unlikely; by way of caution. */
  965. vd = &(iso9660->joliet);
  966. skipsize = LOGICAL_BLOCK_SIZE * vd->location;
  967. skipsize = __archive_read_consume(a, skipsize);
  968. if (skipsize < 0)
  969. return ((int)skipsize);
  970. iso9660->current_position = skipsize;
  971. block = __archive_read_ahead(a, vd->size, NULL);
  972. if (block == NULL) {
  973. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  974. "Failed to read full block when scanning "
  975. "ISO9660 directory list");
  976. return (ARCHIVE_FATAL);
  977. }
  978. /*
  979. * While reading Root Directory, flag seenJoliet must be zero to
  980. * avoid converting special name 0x00(Current Directory) and
  981. * next byte to UCS2.
  982. */
  983. seenJoliet = iso9660->seenJoliet;/* Save flag. */
  984. iso9660->seenJoliet = 0;
  985. file = parse_file_info(a, NULL, block);
  986. if (file == NULL)
  987. return (ARCHIVE_FATAL);
  988. iso9660->seenJoliet = seenJoliet;
  989. /*
  990. * If the iso image has both RockRidge and Joliet, we preferentially
  991. * use RockRidge Extensions rather than Joliet ones.
  992. */
  993. if (vd == &(iso9660->primary) && iso9660->seenRockridge
  994. && iso9660->seenJoliet)
  995. iso9660->seenJoliet = 0;
  996. if (vd == &(iso9660->primary) && !iso9660->seenRockridge
  997. && iso9660->seenJoliet) {
  998. /* Switch reading data from primary to joliet. */
  999. vd = &(iso9660->joliet);
  1000. skipsize = LOGICAL_BLOCK_SIZE * vd->location;
  1001. skipsize -= iso9660->current_position;
  1002. skipsize = __archive_read_consume(a, skipsize);
  1003. if (skipsize < 0)
  1004. return ((int)skipsize);
  1005. iso9660->current_position += skipsize;
  1006. block = __archive_read_ahead(a, vd->size, NULL);
  1007. if (block == NULL) {
  1008. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1009. "Failed to read full block when scanning "
  1010. "ISO9660 directory list");
  1011. return (ARCHIVE_FATAL);
  1012. }
  1013. iso9660->seenJoliet = 0;
  1014. file = parse_file_info(a, NULL, block);
  1015. if (file == NULL)
  1016. return (ARCHIVE_FATAL);
  1017. iso9660->seenJoliet = seenJoliet;
  1018. }
  1019. /* Store the root directory in the pending list. */
  1020. if (add_entry(a, iso9660, file) != ARCHIVE_OK)
  1021. return (ARCHIVE_FATAL);
  1022. if (iso9660->seenRockridge) {
  1023. a->archive.archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
  1024. a->archive.archive_format_name =
  1025. "ISO9660 with Rockridge extensions";
  1026. }
  1027. return (ARCHIVE_OK);
  1028. }
  1029. static int
  1030. archive_read_format_iso9660_read_header(struct archive_read *a,
  1031. struct archive_entry *entry)
  1032. {
  1033. struct iso9660 *iso9660;
  1034. struct file_info *file;
  1035. int r, rd_r = ARCHIVE_OK;
  1036. iso9660 = (struct iso9660 *)(a->format->data);
  1037. if (!a->archive.archive_format) {
  1038. a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
  1039. a->archive.archive_format_name = "ISO9660";
  1040. }
  1041. if (iso9660->current_position == 0) {
  1042. r = choose_volume(a, iso9660);
  1043. if (r != ARCHIVE_OK)
  1044. return (r);
  1045. }
  1046. file = NULL;/* Eliminate a warning. */
  1047. /* Get the next entry that appears after the current offset. */
  1048. r = next_entry_seek(a, iso9660, &file);
  1049. if (r != ARCHIVE_OK)
  1050. return (r);
  1051. if (iso9660->seenJoliet) {
  1052. /*
  1053. * Convert UTF-16BE of a filename to local locale MBS
  1054. * and store the result into a filename field.
  1055. */
  1056. if (iso9660->sconv_utf16be == NULL) {
  1057. iso9660->sconv_utf16be =
  1058. archive_string_conversion_from_charset(
  1059. &(a->archive), "UTF-16BE", 1);
  1060. if (iso9660->sconv_utf16be == NULL)
  1061. /* Coundn't allocate memory */
  1062. return (ARCHIVE_FATAL);
  1063. }
  1064. if (iso9660->utf16be_path == NULL) {
  1065. iso9660->utf16be_path = malloc(UTF16_NAME_MAX);
  1066. if (iso9660->utf16be_path == NULL) {
  1067. archive_set_error(&a->archive, ENOMEM,
  1068. "No memory");
  1069. return (ARCHIVE_FATAL);
  1070. }
  1071. }
  1072. if (iso9660->utf16be_previous_path == NULL) {
  1073. iso9660->utf16be_previous_path = malloc(UTF16_NAME_MAX);
  1074. if (iso9660->utf16be_previous_path == NULL) {
  1075. archive_set_error(&a->archive, ENOMEM,
  1076. "No memory");
  1077. return (ARCHIVE_FATAL);
  1078. }
  1079. }
  1080. iso9660->utf16be_path_len = 0;
  1081. if (build_pathname_utf16be(iso9660->utf16be_path,
  1082. UTF16_NAME_MAX, &(iso9660->utf16be_path_len), file) != 0) {
  1083. archive_set_error(&a->archive,
  1084. ARCHIVE_ERRNO_FILE_FORMAT,
  1085. "Pathname is too long");
  1086. }
  1087. r = archive_entry_copy_pathname_l(entry,
  1088. (const char *)iso9660->utf16be_path,
  1089. iso9660->utf16be_path_len,
  1090. iso9660->sconv_utf16be);
  1091. if (r != 0) {
  1092. if (errno == ENOMEM) {
  1093. archive_set_error(&a->archive, ENOMEM,
  1094. "No memory for Pathname");
  1095. return (ARCHIVE_FATAL);
  1096. }
  1097. archive_set_error(&a->archive,
  1098. ARCHIVE_ERRNO_FILE_FORMAT,
  1099. "Pathname cannot be converted "
  1100. "from %s to current locale.",
  1101. archive_string_conversion_charset_name(
  1102. iso9660->sconv_utf16be));
  1103. rd_r = ARCHIVE_WARN;
  1104. }
  1105. } else {
  1106. archive_string_empty(&iso9660->pathname);
  1107. archive_entry_set_pathname(entry,
  1108. build_pathname(&iso9660->pathname, file));
  1109. }
  1110. iso9660->entry_bytes_remaining = file->size;
  1111. /* Offset for sparse-file-aware clients. */
  1112. iso9660->entry_sparse_offset = 0;
  1113. if (file->offset + file->size > iso9660->volume_size) {
  1114. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1115. "File is beyond end-of-media: %s",
  1116. archive_entry_pathname(entry));
  1117. iso9660->entry_bytes_remaining = 0;
  1118. return (ARCHIVE_WARN);
  1119. }
  1120. /* Set up the entry structure with information about this entry. */
  1121. archive_entry_set_mode(entry, file->mode);
  1122. archive_entry_set_uid(entry, file->uid);
  1123. archive_entry_set_gid(entry, file->gid);
  1124. archive_entry_set_nlink(entry, file->nlinks);
  1125. if (file->birthtime_is_set)
  1126. archive_entry_set_birthtime(entry, file->birthtime, 0);
  1127. else
  1128. archive_entry_unset_birthtime(entry);
  1129. archive_entry_set_mtime(entry, file->mtime, 0);
  1130. archive_entry_set_ctime(entry, file->ctime, 0);
  1131. archive_entry_set_atime(entry, file->atime, 0);
  1132. /* N.B.: Rock Ridge supports 64-bit device numbers. */
  1133. archive_entry_set_rdev(entry, (dev_t)file->rdev);
  1134. archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
  1135. if (file->symlink.s != NULL)
  1136. archive_entry_copy_symlink(entry, file->symlink.s);
  1137. /* Note: If the input isn't seekable, we can't rewind to
  1138. * return the same body again, so if the next entry refers to
  1139. * the same data, we have to return it as a hardlink to the
  1140. * original entry. */
  1141. if (file->number != -1 &&
  1142. file->number == iso9660->previous_number) {
  1143. if (iso9660->seenJoliet) {
  1144. r = archive_entry_copy_hardlink_l(entry,
  1145. (const char *)iso9660->utf16be_previous_path,
  1146. iso9660->utf16be_previous_path_len,
  1147. iso9660->sconv_utf16be);
  1148. if (r != 0) {
  1149. if (errno == ENOMEM) {
  1150. archive_set_error(&a->archive, ENOMEM,
  1151. "No memory for Linkname");
  1152. return (ARCHIVE_FATAL);
  1153. }
  1154. archive_set_error(&a->archive,
  1155. ARCHIVE_ERRNO_FILE_FORMAT,
  1156. "Linkname cannot be converted "
  1157. "from %s to current locale.",
  1158. archive_string_conversion_charset_name(
  1159. iso9660->sconv_utf16be));
  1160. rd_r = ARCHIVE_WARN;
  1161. }
  1162. } else
  1163. archive_entry_set_hardlink(entry,
  1164. iso9660->previous_pathname.s);
  1165. archive_entry_unset_size(entry);
  1166. iso9660->entry_bytes_remaining = 0;
  1167. return (rd_r);
  1168. }
  1169. if ((file->mode & AE_IFMT) != AE_IFDIR &&
  1170. file->offset < iso9660->current_position) {
  1171. int64_t r64;
  1172. r64 = __archive_read_seek(a, file->offset, SEEK_SET);
  1173. if (r64 != (int64_t)file->offset) {
  1174. /* We can't seek backwards to extract it, so issue
  1175. * a warning. Note that this can only happen if
  1176. * this entry was added to the heap after we passed
  1177. * this offset, that is, only if the directory
  1178. * mentioning this entry is later than the body of
  1179. * the entry. Such layouts are very unusual; most
  1180. * ISO9660 writers lay out and record all directory
  1181. * information first, then store all file bodies. */
  1182. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1183. "Ignoring out-of-order file @%jx (%s) %jd < %jd",
  1184. (intmax_t)file->number,
  1185. iso9660->pathname.s,
  1186. (intmax_t)file->offset,
  1187. (intmax_t)iso9660->current_position);
  1188. iso9660->entry_bytes_remaining = 0;
  1189. return (ARCHIVE_WARN);
  1190. }
  1191. iso9660->current_position = (uint64_t)r64;
  1192. }
  1193. /* Initialize zisofs variables. */
  1194. iso9660->entry_zisofs.pz = file->pz;
  1195. if (file->pz) {
  1196. #ifdef HAVE_ZLIB_H
  1197. struct zisofs *zisofs;
  1198. zisofs = &iso9660->entry_zisofs;
  1199. zisofs->initialized = 0;
  1200. zisofs->pz_log2_bs = file->pz_log2_bs;
  1201. zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
  1202. zisofs->pz_offset = 0;
  1203. zisofs->header_avail = 0;
  1204. zisofs->header_passed = 0;
  1205. zisofs->block_pointers_avail = 0;
  1206. #endif
  1207. archive_entry_set_size(entry, file->pz_uncompressed_size);
  1208. }
  1209. iso9660->previous_number = file->number;
  1210. if (iso9660->seenJoliet) {
  1211. memcpy(iso9660->utf16be_previous_path, iso9660->utf16be_path,
  1212. iso9660->utf16be_path_len);
  1213. iso9660->utf16be_previous_path_len = iso9660->utf16be_path_len;
  1214. } else
  1215. archive_strcpy(
  1216. &iso9660->previous_pathname, iso9660->pathname.s);
  1217. /* Reset entry_bytes_remaining if the file is multi extent. */
  1218. iso9660->entry_content = file->contents.first;
  1219. if (iso9660->entry_content != NULL)
  1220. iso9660->entry_bytes_remaining = iso9660->entry_content->size;
  1221. if (archive_entry_filetype(entry) == AE_IFDIR) {
  1222. /* Overwrite nlinks by proper link number which is
  1223. * calculated from number of sub directories. */
  1224. archive_entry_set_nlink(entry, 2 + file->subdirs);
  1225. /* Directory data has been read completely. */
  1226. iso9660->entry_bytes_remaining = 0;
  1227. }
  1228. if (rd_r != ARCHIVE_OK)
  1229. return (rd_r);
  1230. return (ARCHIVE_OK);
  1231. }
  1232. static int
  1233. archive_read_format_iso9660_read_data_skip(struct archive_read *a)
  1234. {
  1235. /* Because read_next_header always does an explicit skip
  1236. * to the next entry, we don't need to do anything here. */
  1237. (void)a; /* UNUSED */
  1238. return (ARCHIVE_OK);
  1239. }
  1240. #ifdef HAVE_ZLIB_H
  1241. static int
  1242. zisofs_read_data(struct archive_read *a,
  1243. const void **buff, size_t *size, int64_t *offset)
  1244. {
  1245. struct iso9660 *iso9660;
  1246. struct zisofs *zisofs;
  1247. const unsigned char *p;
  1248. size_t avail;
  1249. ssize_t bytes_read;
  1250. size_t uncompressed_size;
  1251. int r;
  1252. iso9660 = (struct iso9660 *)(a->format->data);
  1253. zisofs = &iso9660->entry_zisofs;
  1254. p = __archive_read_ahead(a, 1, &bytes_read);
  1255. if (bytes_read <= 0) {
  1256. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1257. "Truncated zisofs file body");
  1258. return (ARCHIVE_FATAL);
  1259. }
  1260. if (bytes_read > iso9660->entry_bytes_remaining)
  1261. bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
  1262. avail = bytes_read;
  1263. uncompressed_size = 0;
  1264. if (!zisofs->initialized) {
  1265. size_t ceil, xsize;
  1266. /* Allocate block pointers buffer. */
  1267. ceil = (size_t)((zisofs->pz_uncompressed_size +
  1268. (((int64_t)1) << zisofs->pz_log2_bs) - 1)
  1269. >> zisofs->pz_log2_bs);
  1270. xsize = (ceil + 1) * 4;
  1271. if (zisofs->block_pointers_alloc < xsize) {
  1272. size_t alloc;
  1273. if (zisofs->block_pointers != NULL)
  1274. free(zisofs->block_pointers);
  1275. alloc = ((xsize >> 10) + 1) << 10;
  1276. zisofs->block_pointers = malloc(alloc);
  1277. if (zisofs->block_pointers == NULL) {
  1278. archive_set_error(&a->archive, ENOMEM,
  1279. "No memory for zisofs decompression");
  1280. return (ARCHIVE_FATAL);
  1281. }
  1282. zisofs->block_pointers_alloc = alloc;
  1283. }
  1284. zisofs->block_pointers_size = xsize;
  1285. /* Allocate uncompressed data buffer. */
  1286. xsize = (size_t)1UL << zisofs->pz_log2_bs;
  1287. if (zisofs->uncompressed_buffer_size < xsize) {
  1288. if (zisofs->uncompressed_buffer != NULL)
  1289. free(zisofs->uncompressed_buffer);
  1290. zisofs->uncompressed_buffer = malloc(xsize);
  1291. if (zisofs->uncompressed_buffer == NULL) {
  1292. archive_set_error(&a->archive, ENOMEM,
  1293. "No memory for zisofs decompression");
  1294. return (ARCHIVE_FATAL);
  1295. }
  1296. }
  1297. zisofs->uncompressed_buffer_size = xsize;
  1298. /*
  1299. * Read the file header, and check the magic code of zisofs.
  1300. */
  1301. if (zisofs->header_avail < sizeof(zisofs->header)) {
  1302. xsize = sizeof(zisofs->header) - zisofs->header_avail;
  1303. if (avail < xsize)
  1304. xsize = avail;
  1305. memcpy(zisofs->header + zisofs->header_avail, p, xsize);
  1306. zisofs->header_avail += xsize;
  1307. avail -= xsize;
  1308. p += xsize;
  1309. }
  1310. if (!zisofs->header_passed &&
  1311. zisofs->header_avail == sizeof(zisofs->header)) {
  1312. int err = 0;
  1313. if (memcmp(zisofs->header, zisofs_magic,
  1314. sizeof(zisofs_magic)) != 0)
  1315. err = 1;
  1316. if (archive_le32dec(zisofs->header + 8)
  1317. != zisofs->pz_uncompressed_size)
  1318. err = 1;
  1319. if (zisofs->header[12] != 4)
  1320. err = 1;
  1321. if (zisofs->header[13] != zisofs->pz_log2_bs)
  1322. err = 1;
  1323. if (err) {
  1324. archive_set_error(&a->archive,
  1325. ARCHIVE_ERRNO_FILE_FORMAT,
  1326. "Illegal zisofs file body");
  1327. return (ARCHIVE_FATAL);
  1328. }
  1329. zisofs->header_passed = 1;
  1330. }
  1331. /*
  1332. * Read block pointers.
  1333. */
  1334. if (zisofs->header_passed &&
  1335. zisofs->block_pointers_avail < zisofs->block_pointers_size) {
  1336. xsize = zisofs->block_pointers_size
  1337. - zisofs->block_pointers_avail;
  1338. if (avail < xsize)
  1339. xsize = avail;
  1340. memcpy(zisofs->block_pointers
  1341. + zisofs->block_pointers_avail, p, xsize);
  1342. zisofs->block_pointers_avail += xsize;
  1343. avail -= xsize;
  1344. p += xsize;
  1345. if (zisofs->block_pointers_avail
  1346. == zisofs->block_pointers_size) {
  1347. /* We've got all block pointers and initialize
  1348. * related variables. */
  1349. zisofs->block_off = 0;
  1350. zisofs->block_avail = 0;
  1351. /* Complete a initialization */
  1352. zisofs->initialized = 1;
  1353. }
  1354. }
  1355. if (!zisofs->initialized)
  1356. goto next_data; /* We need more data. */
  1357. }
  1358. /*
  1359. * Get block offsets from block pointers.
  1360. */
  1361. if (zisofs->block_avail == 0) {
  1362. uint32_t bst, bed;
  1363. if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
  1364. /* There isn't a pair of offsets. */
  1365. archive_set_error(&a->archive,
  1366. ARCHIVE_ERRNO_FILE_FORMAT,
  1367. "Illegal zisofs block pointers");
  1368. return (ARCHIVE_FATAL);
  1369. }
  1370. bst = archive_le32dec(
  1371. zisofs->block_pointers + zisofs->block_off);
  1372. if (bst != zisofs->pz_offset + (bytes_read - avail)) {
  1373. /* TODO: Should we seek offset of current file
  1374. * by bst ? */
  1375. archive_set_error(&a->archive,
  1376. ARCHIVE_ERRNO_FILE_FORMAT,
  1377. "Illegal zisofs block pointers(cannot seek)");
  1378. return (ARCHIVE_FATAL);
  1379. }
  1380. bed = archive_le32dec(
  1381. zisofs->block_pointers + zisofs->block_off + 4);
  1382. if (bed < bst) {
  1383. archive_set_error(&a->archive,
  1384. ARCHIVE_ERRNO_FILE_FORMAT,
  1385. "Illegal zisofs block pointers");
  1386. return (ARCHIVE_FATAL);
  1387. }
  1388. zisofs->block_avail = bed - bst;
  1389. zisofs->block_off += 4;
  1390. /* Initialize compression library for new block. */
  1391. if (zisofs->stream_valid)
  1392. r = inflateReset(&zisofs->stream);
  1393. else
  1394. r = inflateInit(&zisofs->stream);
  1395. if (r != Z_OK) {
  1396. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1397. "Can't initialize zisofs decompression.");
  1398. return (ARCHIVE_FATAL);
  1399. }
  1400. zisofs->stream_valid = 1;
  1401. zisofs->stream.total_in = 0;
  1402. zisofs->stream.total_out = 0;
  1403. }
  1404. /*
  1405. * Make uncompressed data.
  1406. */
  1407. if (zisofs->block_avail == 0) {
  1408. memset(zisofs->uncompressed_buffer, 0,
  1409. zisofs->uncompressed_buffer_size);
  1410. uncompressed_size = zisofs->uncompressed_buffer_size;
  1411. } else {
  1412. zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
  1413. if (avail > zisofs->block_avail)
  1414. zisofs->stream.avail_in = zisofs->block_avail;
  1415. else
  1416. zisofs->stream.avail_in = (uInt)avail;
  1417. zisofs->stream.next_out = zisofs->uncompressed_buffer;
  1418. zisofs->stream.avail_out =
  1419. (uInt)zisofs->uncompressed_buffer_size;
  1420. r = inflate(&zisofs->stream, 0);
  1421. switch (r) {
  1422. case Z_OK: /* Decompressor made some progress.*/
  1423. case Z_STREAM_END: /* Found end of stream. */
  1424. break;
  1425. default:
  1426. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1427. "zisofs decompression failed (%d)", r);
  1428. return (ARCHIVE_FATAL);
  1429. }
  1430. uncompressed_size =
  1431. zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
  1432. avail -= zisofs->stream.next_in - p;
  1433. zisofs->block_avail -= (uint32_t)(zisofs->stream.next_in - p);
  1434. }
  1435. next_data:
  1436. bytes_read -= avail;
  1437. *buff = zisofs->uncompressed_buffer;
  1438. *size = uncompressed_size;
  1439. *offset = iso9660->entry_sparse_offset;
  1440. iso9660->entry_sparse_offset += uncompressed_size;
  1441. iso9660->entry_bytes_remaining -= bytes_read;
  1442. iso9660->current_position += bytes_read;
  1443. zisofs->pz_offset += (uint32_t)bytes_read;
  1444. iso9660->entry_bytes_unconsumed += bytes_read;
  1445. return (ARCHIVE_OK);
  1446. }
  1447. #else /* HAVE_ZLIB_H */
  1448. static int
  1449. zisofs_read_data(struct archive_read *a,
  1450. const void **buff, size_t *size, int64_t *offset)
  1451. {
  1452. (void)buff;/* UNUSED */
  1453. (void)size;/* UNUSED */
  1454. (void)offset;/* UNUSED */
  1455. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1456. "zisofs is not supported on this platform.");
  1457. return (ARCHIVE_FAILED);
  1458. }
  1459. #endif /* HAVE_ZLIB_H */
  1460. static int
  1461. archive_read_format_iso9660_read_data(struct archive_read *a,
  1462. const void **buff, size_t *size, int64_t *offset)
  1463. {
  1464. ssize_t bytes_read;
  1465. struct iso9660 *iso9660;
  1466. iso9660 = (struct iso9660 *)(a->format->data);
  1467. if (iso9660->entry_bytes_unconsumed) {
  1468. __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
  1469. iso9660->entry_bytes_unconsumed = 0;
  1470. }
  1471. if (iso9660->entry_bytes_remaining <= 0) {
  1472. if (iso9660->entry_content != NULL)
  1473. iso9660->entry_content = iso9660->entry_content->next;
  1474. if (iso9660->entry_content == NULL) {
  1475. *buff = NULL;
  1476. *size = 0;
  1477. *offset = iso9660->entry_sparse_offset;
  1478. return (ARCHIVE_EOF);
  1479. }
  1480. /* Seek forward to the start of the entry. */
  1481. if (iso9660->current_position < iso9660->entry_content->offset) {
  1482. int64_t step;
  1483. step = iso9660->entry_content->offset -
  1484. iso9660->current_position;
  1485. step = __archive_read_consume(a, step);
  1486. if (step < 0)
  1487. return ((int)step);
  1488. iso9660->current_position =
  1489. iso9660->entry_content->offset;
  1490. }
  1491. if (iso9660->entry_content->offset < iso9660->current_position) {
  1492. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1493. "Ignoring out-of-order file (%s) %jd < %jd",
  1494. iso9660->pathname.s,
  1495. (intmax_t)iso9660->entry_content->offset,
  1496. (intmax_t)iso9660->current_position);
  1497. *buff = NULL;
  1498. *size = 0;
  1499. *offset = iso9660->entry_sparse_offset;
  1500. return (ARCHIVE_WARN);
  1501. }
  1502. iso9660->entry_bytes_remaining = iso9660->entry_content->size;
  1503. }
  1504. if (iso9660->entry_zisofs.pz)
  1505. return (zisofs_read_data(a, buff, size, offset));
  1506. *buff = __archive_read_ahead(a, 1, &bytes_read);
  1507. if (bytes_read == 0)
  1508. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1509. "Truncated input file");
  1510. if (*buff == NULL)
  1511. return (ARCHIVE_FATAL);
  1512. if (bytes_read > iso9660->entry_bytes_remaining)
  1513. bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
  1514. *size = bytes_read;
  1515. *offset = iso9660->entry_sparse_offset;
  1516. iso9660->entry_sparse_offset += bytes_read;
  1517. iso9660->entry_bytes_remaining -= bytes_read;
  1518. iso9660->entry_bytes_unconsumed = bytes_read;
  1519. iso9660->current_position += bytes_read;
  1520. return (ARCHIVE_OK);
  1521. }
  1522. static int
  1523. archive_read_format_iso9660_cleanup(struct archive_read *a)
  1524. {
  1525. struct iso9660 *iso9660;
  1526. int r = ARCHIVE_OK;
  1527. iso9660 = (struct iso9660 *)(a->format->data);
  1528. release_files(iso9660);
  1529. free(iso9660->read_ce_req.reqs);
  1530. archive_string_free(&iso9660->pathname);
  1531. archive_string_free(&iso9660->previous_pathname);
  1532. if (iso9660->pending_files.files)
  1533. free(iso9660->pending_files.files);
  1534. #ifdef HAVE_ZLIB_H
  1535. free(iso9660->entry_zisofs.uncompressed_buffer);
  1536. free(iso9660->entry_zisofs.block_pointers);
  1537. if (iso9660->entry_zisofs.stream_valid) {
  1538. if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
  1539. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1540. "Failed to clean up zlib decompressor");
  1541. r = ARCHIVE_FATAL;
  1542. }
  1543. }
  1544. #endif
  1545. free(iso9660->utf16be_path);
  1546. free(iso9660->utf16be_previous_path);
  1547. free(iso9660);
  1548. (a->format->data) = NULL;
  1549. return (r);
  1550. }
  1551. /*
  1552. * This routine parses a single ISO directory record, makes sense
  1553. * of any extensions, and stores the result in memory.
  1554. */
  1555. static struct file_info *
  1556. parse_file_info(struct archive_read *a, struct file_info *parent,
  1557. const unsigned char *isodirrec)
  1558. {
  1559. struct iso9660 *iso9660;
  1560. struct file_info *file;
  1561. size_t name_len;
  1562. const unsigned char *rr_start, *rr_end;
  1563. const unsigned char *p;
  1564. size_t dr_len;
  1565. uint64_t fsize;
  1566. int32_t location;
  1567. int flags;
  1568. iso9660 = (struct iso9660 *)(a->format->data);
  1569. dr_len = (size_t)isodirrec[DR_length_offset];
  1570. name_len = (size_t)isodirrec[DR_name_len_offset];
  1571. location = archive_le32dec(isodirrec + DR_extent_offset);
  1572. fsize = toi(isodirrec + DR_size_offset, DR_size_size);
  1573. /* Sanity check that dr_len needs at least 34. */
  1574. if (dr_len < 34) {
  1575. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1576. "Invalid length of directory record");
  1577. return (NULL);
  1578. }
  1579. /* Sanity check that name_len doesn't exceed dr_len. */
  1580. if (dr_len - 33 < name_len || name_len == 0) {
  1581. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1582. "Invalid length of file identifier");
  1583. return (NULL);
  1584. }
  1585. /* Sanity check that location doesn't exceed volume block.
  1586. * Don't check lower limit of location; it's possibility
  1587. * the location has negative value when file type is symbolic
  1588. * link or file size is zero. As far as I know latest mkisofs
  1589. * do that.
  1590. */
  1591. if (location > 0 &&
  1592. (location + ((fsize + iso9660->logical_block_size -1)
  1593. / iso9660->logical_block_size))
  1594. > (uint32_t)iso9660->volume_block) {
  1595. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1596. "Invalid location of extent of file");
  1597. return (NULL);
  1598. }
  1599. /* Sanity check that location doesn't have a negative value
  1600. * when the file is not empty. it's too large. */
  1601. if (fsize != 0 && location < 0) {
  1602. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1603. "Invalid location of extent of file");
  1604. return (NULL);
  1605. }
  1606. /* Create a new file entry and copy data from the ISO dir record. */
  1607. file = (struct file_info *)calloc(1, sizeof(*file));
  1608. if (file == NULL) {
  1609. archive_set_error(&a->archive, ENOMEM,
  1610. "No memory for file entry");
  1611. return (NULL);
  1612. }
  1613. file->parent = parent;
  1614. file->offset = iso9660->logical_block_size * (uint64_t)location;
  1615. file->size = fsize;
  1616. file->mtime = isodate7(isodirrec + DR_date_offset);
  1617. file->ctime = file->atime = file->mtime;
  1618. file->rede_files.first = NULL;
  1619. file->rede_files.last = &(file->rede_files.first);
  1620. p = isodirrec + DR_name_offset;
  1621. /* Rockridge extensions (if any) follow name. Compute this
  1622. * before fidgeting the name_len below. */
  1623. rr_start = p + name_len + (name_len & 1 ? 0 : 1);
  1624. rr_end = isodirrec + dr_len;
  1625. if (iso9660->seenJoliet) {
  1626. /* Joliet names are max 64 chars (128 bytes) according to spec,
  1627. * but genisoimage/mkisofs allows recording longer Joliet
  1628. * names which are 103 UCS2 characters(206 bytes) by their
  1629. * option '-joliet-long'.
  1630. */
  1631. if (name_len > 206)
  1632. name_len = 206;
  1633. name_len &= ~1;
  1634. /* trim trailing first version and dot from filename.
  1635. *
  1636. * Remember we were in UTF-16BE land!
  1637. * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
  1638. * 16 bits big endian characters on Joliet.
  1639. *
  1640. * TODO: sanitize filename?
  1641. * Joliet allows any UCS-2 char except:
  1642. * *, /, :, ;, ? and \.
  1643. */
  1644. /* Chop off trailing ';1' from files. */
  1645. if (name_len > 4 && p[name_len-4] == 0 && p[name_len-3] == ';'
  1646. && p[name_len-2] == 0 && p[name_len-1] == '1')
  1647. name_len -= 4;
  1648. #if 0 /* XXX: this somehow manages to strip of single-character file extensions, like '.c'. */
  1649. /* Chop off trailing '.' from filenames. */
  1650. if (name_len > 2 && p[name_len-2] == 0 && p[name_len-1] == '.')
  1651. name_len -= 2;
  1652. #endif
  1653. if ((file->utf16be_name = malloc(name_len)) == NULL) {
  1654. archive_set_error(&a->archive, ENOMEM,
  1655. "No memory for file name");
  1656. return (NULL);
  1657. }
  1658. memcpy(file->utf16be_name, p, name_len);
  1659. file->utf16be_bytes = name_len;
  1660. } else {
  1661. /* Chop off trailing ';1' from files. */
  1662. if (name_len > 2 && p[name_len - 2] == ';' &&
  1663. p[name_len - 1] == '1')
  1664. name_len -= 2;
  1665. /* Chop off trailing '.' from filenames. */
  1666. if (name_len > 1 && p[name_len - 1] == '.')
  1667. --name_len;
  1668. archive_strncpy(&file->name, (const char *)p, name_len);
  1669. }
  1670. flags = isodirrec[DR_flags_offset];
  1671. if (flags & 0x02)
  1672. file->mode = AE_IFDIR | 0700;
  1673. else
  1674. file->mode = AE_IFREG | 0400;
  1675. if (flags & 0x80)
  1676. file->multi_extent = 1;
  1677. else
  1678. file->multi_extent = 0;
  1679. /*
  1680. * Use a location for the file number, which is treated as an inode
  1681. * number to find out hardlink target. If Rockridge extensions is
  1682. * being used, the file number will be overwritten by FILE SERIAL
  1683. * NUMBER of RRIP "PX" extension.
  1684. * Note: Old mkisofs did not record that FILE SERIAL NUMBER
  1685. * in ISO images.
  1686. * Note2: xorriso set 0 to the location of a symlink file.
  1687. */
  1688. if (file->size == 0 && location >= 0) {
  1689. /* If file->size is zero, its location points wrong place,
  1690. * and so we should not use it for the file number.
  1691. * When the location has negative value, it can be used
  1692. * for the file number.
  1693. */
  1694. file->number = -1;
  1695. /* Do not appear before any directory entries. */
  1696. file->offset = -1;
  1697. } else
  1698. file->number = (int64_t)(uint32_t)location;
  1699. /* Rockridge extensions overwrite information from above. */
  1700. if (iso9660->opt_support_rockridge) {
  1701. if (parent == NULL && rr_end - rr_start >= 7) {
  1702. p = rr_start;
  1703. if (memcmp(p, "SP\x07\x01\xbe\xef", 6) == 0) {
  1704. /*
  1705. * SP extension stores the suspOffset
  1706. * (Number of bytes to skip between
  1707. * filename and SUSP records.)
  1708. * It is mandatory by the SUSP standard
  1709. * (IEEE 1281).
  1710. *
  1711. * It allows SUSP to coexist with
  1712. * non-SUSP uses of the System
  1713. * Use Area by placing non-SUSP data
  1714. * before SUSP data.
  1715. *
  1716. * SP extension must be in the root
  1717. * directory entry, disable all SUSP
  1718. * processing if not found.
  1719. */
  1720. iso9660->suspOffset = p[6];
  1721. iso9660->seenSUSP = 1;
  1722. rr_start += 7;
  1723. }
  1724. }
  1725. if (iso9660->seenSUSP) {
  1726. int r;
  1727. file->name_continues = 0;
  1728. file->symlink_continues = 0;
  1729. rr_start += iso9660->suspOffset;
  1730. r = parse_rockridge(a, file, rr_start, rr_end);
  1731. if (r != ARCHIVE_OK) {
  1732. free(file);
  1733. return (NULL);
  1734. }
  1735. /*
  1736. * A file size of symbolic link files in ISO images
  1737. * made by makefs is not zero and its location is
  1738. * the same as those of next regular file. That is
  1739. * the same as hard like file and it causes unexpected
  1740. * error.
  1741. */
  1742. if (file->size > 0 &&
  1743. (file->mode & AE_IFMT) == AE_IFLNK) {
  1744. file->size = 0;
  1745. file->number = -1;
  1746. file->offset = -1;
  1747. }
  1748. } else
  1749. /* If there isn't SUSP, disable parsing
  1750. * rock ridge extensions. */
  1751. iso9660->opt_support_rockridge = 0;
  1752. }
  1753. file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
  1754. /* Tell file's parent how many children that parent has. */
  1755. if (parent != NULL && (flags & 0x02))
  1756. parent->subdirs++;
  1757. if (iso9660->seenRockridge) {
  1758. if (parent != NULL && parent->parent == NULL &&
  1759. (flags & 0x02) && iso9660->rr_moved == NULL &&
  1760. file->name.s &&
  1761. (strcmp(file->name.s, "rr_moved") == 0 ||
  1762. strcmp(file->name.s, ".rr_moved") == 0)) {
  1763. iso9660->rr_moved = file;
  1764. file->rr_moved = 1;
  1765. file->rr_moved_has_re_only = 1;
  1766. file->re = 0;
  1767. parent->subdirs--;
  1768. } else if (file->re) {
  1769. /*
  1770. * Sanity check: file's parent is rr_moved.
  1771. */
  1772. if (parent == NULL || parent->rr_moved == 0) {
  1773. archive_set_error(&a->archive,
  1774. ARCHIVE_ERRNO_MISC,
  1775. "Invalid Rockridge RE");
  1776. return (NULL);
  1777. }
  1778. /*
  1779. * Sanity check: file does not have "CL" extension.
  1780. */
  1781. if (file->cl_offset) {
  1782. archive_set_error(&a->archive,
  1783. ARCHIVE_ERRNO_MISC,
  1784. "Invalid Rockridge RE and CL");
  1785. return (NULL);
  1786. }
  1787. /*
  1788. * Sanity check: The file type must be a directory.
  1789. */
  1790. if ((flags & 0x02) == 0) {
  1791. archive_set_error(&a->archive,
  1792. ARCHIVE_ERRNO_MISC,
  1793. "Invalid Rockridge RE");
  1794. return (NULL);
  1795. }
  1796. } else if (parent != NULL && parent->rr_moved)
  1797. file->rr_moved_has_re_only = 0;
  1798. else if (parent != NULL && (flags & 0x02) &&
  1799. (parent->re || parent->re_descendant))
  1800. file->re_descendant = 1;
  1801. if (file->cl_offset) {
  1802. struct file_info *r;
  1803. if (parent == NULL || parent->parent == NULL) {
  1804. archive_set_error(&a->archive,
  1805. ARCHIVE_ERRNO_MISC,
  1806. "Invalid Rockridge CL");
  1807. return (NULL);
  1808. }
  1809. /*
  1810. * Sanity check: The file type must be a regular file.
  1811. */
  1812. if ((flags & 0x02) != 0) {
  1813. archive_set_error(&a->archive,
  1814. ARCHIVE_ERRNO_MISC,
  1815. "Invalid Rockridge CL");
  1816. return (NULL);
  1817. }
  1818. parent->subdirs++;
  1819. /* Overwrite an offset and a number of this "CL" entry
  1820. * to appear before other dirs. "+1" to those is to
  1821. * make sure to appear after "RE" entry which this
  1822. * "CL" entry should be connected with. */
  1823. file->offset = file->number = file->cl_offset + 1;
  1824. /*
  1825. * Sanity check: cl_offset does not point at its
  1826. * the parents or itself.
  1827. */
  1828. for (r = parent; r; r = r->parent) {
  1829. if (r->offset == file->cl_offset) {
  1830. archive_set_error(&a->archive,
  1831. ARCHIVE_ERRNO_MISC,
  1832. "Invalid Rockridge CL");
  1833. return (NULL);
  1834. }
  1835. }
  1836. if (file->cl_offset == file->offset ||
  1837. parent->rr_moved) {
  1838. archive_set_error(&a->archive,
  1839. ARCHIVE_ERRNO_MISC,
  1840. "Invalid Rockridge CL");
  1841. return (NULL);
  1842. }
  1843. }
  1844. }
  1845. #if DEBUG
  1846. /* DEBUGGING: Warn about attributes I don't yet fully support. */
  1847. if ((flags & ~0x02) != 0) {
  1848. fprintf(stderr, "\n ** Unrecognized flag: ");
  1849. dump_isodirrec(stderr, isodirrec);
  1850. fprintf(stderr, "\n");
  1851. } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
  1852. fprintf(stderr, "\n ** Unrecognized sequence number: ");
  1853. dump_isodirrec(stderr, isodirrec);
  1854. fprintf(stderr, "\n");
  1855. } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
  1856. fprintf(stderr, "\n ** Unexpected file unit size: ");
  1857. dump_isodirrec(stderr, isodirrec);
  1858. fprintf(stderr, "\n");
  1859. } else if (*(isodirrec + DR_interleave_offset) != 0) {
  1860. fprintf(stderr, "\n ** Unexpected interleave: ");
  1861. dump_isodirrec(stderr, isodirrec);
  1862. fprintf(stderr, "\n");
  1863. } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
  1864. fprintf(stderr, "\n ** Unexpected extended attribute length: ");
  1865. dump_isodirrec(stderr, isodirrec);
  1866. fprintf(stderr, "\n");
  1867. }
  1868. #endif
  1869. register_file(iso9660, file);
  1870. return (file);
  1871. }
  1872. static int
  1873. parse_rockridge(struct archive_read *a, struct file_info *file,
  1874. const unsigned char *p, const unsigned char *end)
  1875. {
  1876. struct iso9660 *iso9660;
  1877. iso9660 = (struct iso9660 *)(a->format->data);
  1878. while (p + 4 <= end /* Enough space for another entry. */
  1879. && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
  1880. && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
  1881. && p[2] >= 4 /* Sanity-check length. */
  1882. && p + p[2] <= end) { /* Sanity-check length. */
  1883. const unsigned char *data = p + 4;
  1884. int data_length = p[2] - 4;
  1885. int version = p[3];
  1886. switch(p[0]) {
  1887. case 'C':
  1888. if (p[1] == 'E') {
  1889. if (version == 1 && data_length == 24) {
  1890. /*
  1891. * CE extension comprises:
  1892. * 8 byte sector containing extension
  1893. * 8 byte offset w/in above sector
  1894. * 8 byte length of continuation
  1895. */
  1896. int32_t location =
  1897. archive_le32dec(data);
  1898. file->ce_offset =
  1899. archive_le32dec(data+8);
  1900. file->ce_size =
  1901. archive_le32dec(data+16);
  1902. if (register_CE(a, location, file)
  1903. != ARCHIVE_OK)
  1904. return (ARCHIVE_FATAL);
  1905. }
  1906. }
  1907. else if (p[1] == 'L') {
  1908. if (version == 1 && data_length == 8) {
  1909. file->cl_offset = (uint64_t)
  1910. iso9660->logical_block_size *
  1911. (uint64_t)archive_le32dec(data);
  1912. iso9660->seenRockridge = 1;
  1913. }
  1914. }
  1915. break;
  1916. case 'N':
  1917. if (p[1] == 'M') {
  1918. if (version == 1) {
  1919. parse_rockridge_NM1(file,
  1920. data, data_length);
  1921. iso9660->seenRockridge = 1;
  1922. }
  1923. }
  1924. break;
  1925. case 'P':
  1926. /*
  1927. * PD extension is padding;
  1928. * contents are always ignored.
  1929. *
  1930. * PL extension won't appear;
  1931. * contents are always ignored.
  1932. */
  1933. if (p[1] == 'N') {
  1934. if (version == 1 && data_length == 16) {
  1935. file->rdev = toi(data,4);
  1936. file->rdev <<= 32;
  1937. file->rdev |= toi(data + 8, 4);
  1938. iso9660->seenRockridge = 1;
  1939. }
  1940. }
  1941. else if (p[1] == 'X') {
  1942. /*
  1943. * PX extension comprises:
  1944. * 8 bytes for mode,
  1945. * 8 bytes for nlinks,
  1946. * 8 bytes for uid,
  1947. * 8 bytes for gid,
  1948. * 8 bytes for inode.
  1949. */
  1950. if (version == 1) {
  1951. if (data_length >= 8)
  1952. file->mode
  1953. = toi(data, 4);
  1954. if (data_length >= 16)
  1955. file->nlinks
  1956. = toi(data + 8, 4);
  1957. if (data_length >= 24)
  1958. file->uid
  1959. = toi(data + 16, 4);
  1960. if (data_length >= 32)
  1961. file->gid
  1962. = toi(data + 24, 4);
  1963. if (data_length >= 40)
  1964. file->number
  1965. = toi(data + 32, 4);
  1966. iso9660->seenRockridge = 1;
  1967. }
  1968. }
  1969. break;
  1970. case 'R':
  1971. if (p[1] == 'E' && version == 1) {
  1972. file->re = 1;
  1973. iso9660->seenRockridge = 1;
  1974. }
  1975. else if (p[1] == 'R' && version == 1) {
  1976. /*
  1977. * RR extension comprises:
  1978. * one byte flag value
  1979. * This extension is obsolete,
  1980. * so contents are always ignored.
  1981. */
  1982. }
  1983. break;
  1984. case 'S':
  1985. if (p[1] == 'L') {
  1986. if (version == 1) {
  1987. parse_rockridge_SL1(file,
  1988. data, data_length);
  1989. iso9660->seenRockridge = 1;
  1990. }
  1991. }
  1992. else if (p[1] == 'T'
  1993. && data_length == 0 && version == 1) {
  1994. /*
  1995. * ST extension marks end of this
  1996. * block of SUSP entries.
  1997. *
  1998. * It allows SUSP to coexist with
  1999. * non-SUSP uses of the System
  2000. * Use Area by placing non-SUSP data
  2001. * after SUSP data.
  2002. */
  2003. iso9660->seenSUSP = 0;
  2004. iso9660->seenRockridge = 0;
  2005. return (ARCHIVE_OK);
  2006. }
  2007. break;
  2008. case 'T':
  2009. if (p[1] == 'F') {
  2010. if (version == 1) {
  2011. parse_rockridge_TF1(file,
  2012. data, data_length);
  2013. iso9660->seenRockridge = 1;
  2014. }
  2015. }
  2016. break;
  2017. case 'Z':
  2018. if (p[1] == 'F') {
  2019. if (version == 1)
  2020. parse_rockridge_ZF1(file,
  2021. data, data_length);
  2022. }
  2023. break;
  2024. default:
  2025. break;
  2026. }
  2027. p += p[2];
  2028. }
  2029. return (ARCHIVE_OK);
  2030. }
  2031. static int
  2032. register_CE(struct archive_read *a, int32_t location,
  2033. struct file_info *file)
  2034. {
  2035. struct iso9660 *iso9660;
  2036. struct read_ce_queue *heap;
  2037. struct read_ce_req *p;
  2038. uint64_t offset, parent_offset;
  2039. int hole, parent;
  2040. iso9660 = (struct iso9660 *)(a->format->data);
  2041. offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
  2042. if (((file->mode & AE_IFMT) == AE_IFREG &&
  2043. offset >= file->offset) ||
  2044. offset < iso9660->current_position ||
  2045. (((uint64_t)file->ce_offset) + file->ce_size)
  2046. > (uint64_t)iso9660->logical_block_size ||
  2047. offset + file->ce_offset + file->ce_size
  2048. > iso9660->volume_size) {
  2049. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2050. "Invalid parameter in SUSP \"CE\" extension");
  2051. return (ARCHIVE_FATAL);
  2052. }
  2053. /* Expand our CE list as necessary. */
  2054. heap = &(iso9660->read_ce_req);
  2055. if (heap->cnt >= heap->allocated) {
  2056. int new_size;
  2057. if (heap->allocated < 16)
  2058. new_size = 16;
  2059. else
  2060. new_size = heap->allocated * 2;
  2061. /* Overflow might keep us from growing the list. */
  2062. if (new_size <= heap->allocated) {
  2063. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2064. return (ARCHIVE_FATAL);
  2065. }
  2066. p = calloc(new_size, sizeof(p[0]));
  2067. if (p == NULL) {
  2068. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2069. return (ARCHIVE_FATAL);
  2070. }
  2071. if (heap->reqs != NULL) {
  2072. memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
  2073. free(heap->reqs);
  2074. }
  2075. heap->reqs = p;
  2076. heap->allocated = new_size;
  2077. }
  2078. /*
  2079. * Start with hole at end, walk it up tree to find insertion point.
  2080. */
  2081. hole = heap->cnt++;
  2082. while (hole > 0) {
  2083. parent = (hole - 1)/2;
  2084. parent_offset = heap->reqs[parent].offset;
  2085. if (offset >= parent_offset) {
  2086. heap->reqs[hole].offset = offset;
  2087. heap->reqs[hole].file = file;
  2088. return (ARCHIVE_OK);
  2089. }
  2090. /* Move parent into hole <==> move hole up tree. */
  2091. heap->reqs[hole] = heap->reqs[parent];
  2092. hole = parent;
  2093. }
  2094. heap->reqs[0].offset = offset;
  2095. heap->reqs[0].file = file;
  2096. return (ARCHIVE_OK);
  2097. }
  2098. static void
  2099. next_CE(struct read_ce_queue *heap)
  2100. {
  2101. uint64_t a_offset, b_offset, c_offset;
  2102. int a, b, c;
  2103. struct read_ce_req tmp;
  2104. if (heap->cnt < 1)
  2105. return;
  2106. /*
  2107. * Move the last item in the heap to the root of the tree
  2108. */
  2109. heap->reqs[0] = heap->reqs[--(heap->cnt)];
  2110. /*
  2111. * Rebalance the heap.
  2112. */
  2113. a = 0; /* Starting element and its offset */
  2114. a_offset = heap->reqs[a].offset;
  2115. for (;;) {
  2116. b = a + a + 1; /* First child */
  2117. if (b >= heap->cnt)
  2118. return;
  2119. b_offset = heap->reqs[b].offset;
  2120. c = b + 1; /* Use second child if it is smaller. */
  2121. if (c < heap->cnt) {
  2122. c_offset = heap->reqs[c].offset;
  2123. if (c_offset < b_offset) {
  2124. b = c;
  2125. b_offset = c_offset;
  2126. }
  2127. }
  2128. if (a_offset <= b_offset)
  2129. return;
  2130. tmp = heap->reqs[a];
  2131. heap->reqs[a] = heap->reqs[b];
  2132. heap->reqs[b] = tmp;
  2133. a = b;
  2134. }
  2135. }
  2136. static int
  2137. read_CE(struct archive_read *a, struct iso9660 *iso9660)
  2138. {
  2139. struct read_ce_queue *heap;
  2140. const unsigned char *b, *p, *end;
  2141. struct file_info *file;
  2142. size_t step;
  2143. int r;
  2144. /* Read data which RRIP "CE" extension points. */
  2145. heap = &(iso9660->read_ce_req);
  2146. step = iso9660->logical_block_size;
  2147. while (heap->cnt &&
  2148. heap->reqs[0].offset == iso9660->current_position) {
  2149. b = __archive_read_ahead(a, step, NULL);
  2150. if (b == NULL) {
  2151. archive_set_error(&a->archive,
  2152. ARCHIVE_ERRNO_MISC,
  2153. "Failed to read full block when scanning "
  2154. "ISO9660 directory list");
  2155. return (ARCHIVE_FATAL);
  2156. }
  2157. do {
  2158. file = heap->reqs[0].file;
  2159. if (file->ce_offset + file->ce_size > step) {
  2160. archive_set_error(&a->archive,
  2161. ARCHIVE_ERRNO_FILE_FORMAT,
  2162. "Malformed CE information");
  2163. return (ARCHIVE_FATAL);
  2164. }
  2165. p = b + file->ce_offset;
  2166. end = p + file->ce_size;
  2167. next_CE(heap);
  2168. r = parse_rockridge(a, file, p, end);
  2169. if (r != ARCHIVE_OK)
  2170. return (ARCHIVE_FATAL);
  2171. } while (heap->cnt &&
  2172. heap->reqs[0].offset == iso9660->current_position);
  2173. /* NOTE: Do not move this consume's code to fron of
  2174. * do-while loop. Registration of nested CE extension
  2175. * might cause error because of current position. */
  2176. __archive_read_consume(a, step);
  2177. iso9660->current_position += step;
  2178. }
  2179. return (ARCHIVE_OK);
  2180. }
  2181. static void
  2182. parse_rockridge_NM1(struct file_info *file,
  2183. const unsigned char *data, int data_length)
  2184. {
  2185. if (!file->name_continues)
  2186. archive_string_empty(&file->name);
  2187. file->name_continues = 0;
  2188. if (data_length < 1)
  2189. return;
  2190. /*
  2191. * NM version 1 extension comprises:
  2192. * 1 byte flag, value is one of:
  2193. * = 0: remainder is name
  2194. * = 1: remainder is name, next NM entry continues name
  2195. * = 2: "."
  2196. * = 4: ".."
  2197. * = 32: Implementation specific
  2198. * All other values are reserved.
  2199. */
  2200. switch(data[0]) {
  2201. case 0:
  2202. if (data_length < 2)
  2203. return;
  2204. archive_strncat(&file->name,
  2205. (const char *)data + 1, data_length - 1);
  2206. break;
  2207. case 1:
  2208. if (data_length < 2)
  2209. return;
  2210. archive_strncat(&file->name,
  2211. (const char *)data + 1, data_length - 1);
  2212. file->name_continues = 1;
  2213. break;
  2214. case 2:
  2215. archive_strcat(&file->name, ".");
  2216. break;
  2217. case 4:
  2218. archive_strcat(&file->name, "..");
  2219. break;
  2220. default:
  2221. return;
  2222. }
  2223. }
  2224. static void
  2225. parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
  2226. int data_length)
  2227. {
  2228. char flag;
  2229. /*
  2230. * TF extension comprises:
  2231. * one byte flag
  2232. * create time (optional)
  2233. * modify time (optional)
  2234. * access time (optional)
  2235. * attribute time (optional)
  2236. * Time format and presence of fields
  2237. * is controlled by flag bits.
  2238. */
  2239. if (data_length < 1)
  2240. return;
  2241. flag = data[0];
  2242. ++data;
  2243. --data_length;
  2244. if (flag & 0x80) {
  2245. /* Use 17-byte time format. */
  2246. if ((flag & 1) && data_length >= 17) {
  2247. /* Create time. */
  2248. file->birthtime_is_set = 1;
  2249. file->birthtime = isodate17(data);
  2250. data += 17;
  2251. data_length -= 17;
  2252. }
  2253. if ((flag & 2) && data_length >= 17) {
  2254. /* Modify time. */
  2255. file->mtime = isodate17(data);
  2256. data += 17;
  2257. data_length -= 17;
  2258. }
  2259. if ((flag & 4) && data_length >= 17) {
  2260. /* Access time. */
  2261. file->atime = isodate17(data);
  2262. data += 17;
  2263. data_length -= 17;
  2264. }
  2265. if ((flag & 8) && data_length >= 17) {
  2266. /* Attribute change time. */
  2267. file->ctime = isodate17(data);
  2268. }
  2269. } else {
  2270. /* Use 7-byte time format. */
  2271. if ((flag & 1) && data_length >= 7) {
  2272. /* Create time. */
  2273. file->birthtime_is_set = 1;
  2274. file->birthtime = isodate7(data);
  2275. data += 7;
  2276. data_length -= 7;
  2277. }
  2278. if ((flag & 2) && data_length >= 7) {
  2279. /* Modify time. */
  2280. file->mtime = isodate7(data);
  2281. data += 7;
  2282. data_length -= 7;
  2283. }
  2284. if ((flag & 4) && data_length >= 7) {
  2285. /* Access time. */
  2286. file->atime = isodate7(data);
  2287. data += 7;
  2288. data_length -= 7;
  2289. }
  2290. if ((flag & 8) && data_length >= 7) {
  2291. /* Attribute change time. */
  2292. file->ctime = isodate7(data);
  2293. }
  2294. }
  2295. }
  2296. static void
  2297. parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
  2298. int data_length)
  2299. {
  2300. const char *separator = "";
  2301. if (!file->symlink_continues || file->symlink.length < 1)
  2302. archive_string_empty(&file->symlink);
  2303. file->symlink_continues = 0;
  2304. /*
  2305. * Defined flag values:
  2306. * 0: This is the last SL record for this symbolic link
  2307. * 1: this symbolic link field continues in next SL entry
  2308. * All other values are reserved.
  2309. */
  2310. if (data_length < 1)
  2311. return;
  2312. switch(*data) {
  2313. case 0:
  2314. break;
  2315. case 1:
  2316. file->symlink_continues = 1;
  2317. break;
  2318. default:
  2319. return;
  2320. }
  2321. ++data; /* Skip flag byte. */
  2322. --data_length;
  2323. /*
  2324. * SL extension body stores "components".
  2325. * Basically, this is a complicated way of storing
  2326. * a POSIX path. It also interferes with using
  2327. * symlinks for storing non-path data. <sigh>
  2328. *
  2329. * Each component is 2 bytes (flag and length)
  2330. * possibly followed by name data.
  2331. */
  2332. while (data_length >= 2) {
  2333. unsigned char flag = *data++;
  2334. unsigned char nlen = *data++;
  2335. data_length -= 2;
  2336. archive_strcat(&file->symlink, separator);
  2337. separator = "/";
  2338. switch(flag) {
  2339. case 0: /* Usual case, this is text. */
  2340. if (data_length < nlen)
  2341. return;
  2342. archive_strncat(&file->symlink,
  2343. (const char *)data, nlen);
  2344. break;
  2345. case 0x01: /* Text continues in next component. */
  2346. if (data_length < nlen)
  2347. return;
  2348. archive_strncat(&file->symlink,
  2349. (const char *)data, nlen);
  2350. separator = "";
  2351. break;
  2352. case 0x02: /* Current dir. */
  2353. archive_strcat(&file->symlink, ".");
  2354. break;
  2355. case 0x04: /* Parent dir. */
  2356. archive_strcat(&file->symlink, "..");
  2357. break;
  2358. case 0x08: /* Root of filesystem. */
  2359. archive_strcat(&file->symlink, "/");
  2360. separator = "";
  2361. break;
  2362. case 0x10: /* Undefined (historically "volume root" */
  2363. archive_string_empty(&file->symlink);
  2364. archive_strcat(&file->symlink, "ROOT");
  2365. break;
  2366. case 0x20: /* Undefined (historically "hostname") */
  2367. archive_strcat(&file->symlink, "hostname");
  2368. break;
  2369. default:
  2370. /* TODO: issue a warning ? */
  2371. return;
  2372. }
  2373. data += nlen;
  2374. data_length -= nlen;
  2375. }
  2376. }
  2377. static void
  2378. parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
  2379. int data_length)
  2380. {
  2381. if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
  2382. /* paged zlib */
  2383. file->pz = 1;
  2384. file->pz_log2_bs = data[3];
  2385. file->pz_uncompressed_size = archive_le32dec(&data[4]);
  2386. }
  2387. }
  2388. static void
  2389. register_file(struct iso9660 *iso9660, struct file_info *file)
  2390. {
  2391. file->use_next = iso9660->use_files;
  2392. iso9660->use_files = file;
  2393. }
  2394. static void
  2395. release_files(struct iso9660 *iso9660)
  2396. {
  2397. struct content *con, *connext;
  2398. struct file_info *file;
  2399. file = iso9660->use_files;
  2400. while (file != NULL) {
  2401. struct file_info *next = file->use_next;
  2402. archive_string_free(&file->name);
  2403. archive_string_free(&file->symlink);
  2404. free(file->utf16be_name);
  2405. con = file->contents.first;
  2406. while (con != NULL) {
  2407. connext = con->next;
  2408. free(con);
  2409. con = connext;
  2410. }
  2411. free(file);
  2412. file = next;
  2413. }
  2414. }
  2415. static int
  2416. next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
  2417. struct file_info **pfile)
  2418. {
  2419. struct file_info *file;
  2420. int r;
  2421. r = next_cache_entry(a, iso9660, pfile);
  2422. if (r != ARCHIVE_OK)
  2423. return (r);
  2424. file = *pfile;
  2425. /* Don't waste time seeking for zero-length bodies. */
  2426. if (file->size == 0)
  2427. file->offset = iso9660->current_position;
  2428. /* flush any remaining bytes from the last round to ensure
  2429. * we're positioned */
  2430. if (iso9660->entry_bytes_unconsumed) {
  2431. __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
  2432. iso9660->entry_bytes_unconsumed = 0;
  2433. }
  2434. /* Seek forward to the start of the entry. */
  2435. if (iso9660->current_position < file->offset) {
  2436. int64_t step;
  2437. step = file->offset - iso9660->current_position;
  2438. step = __archive_read_consume(a, step);
  2439. if (step < 0)
  2440. return ((int)step);
  2441. iso9660->current_position = file->offset;
  2442. }
  2443. /* We found body of file; handle it now. */
  2444. return (ARCHIVE_OK);
  2445. }
  2446. static int
  2447. next_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
  2448. struct file_info **pfile)
  2449. {
  2450. struct file_info *file;
  2451. struct {
  2452. struct file_info *first;
  2453. struct file_info **last;
  2454. } empty_files;
  2455. int64_t number;
  2456. int count;
  2457. file = cache_get_entry(iso9660);
  2458. if (file != NULL) {
  2459. *pfile = file;
  2460. return (ARCHIVE_OK);
  2461. }
  2462. for (;;) {
  2463. struct file_info *re, *d;
  2464. *pfile = file = next_entry(iso9660);
  2465. if (file == NULL) {
  2466. /*
  2467. * If directory entries all which are descendant of
  2468. * rr_moved are stil remaning, expose their.
  2469. */
  2470. if (iso9660->re_files.first != NULL &&
  2471. iso9660->rr_moved != NULL &&
  2472. iso9660->rr_moved->rr_moved_has_re_only)
  2473. /* Expose "rr_moved" entry. */
  2474. cache_add_entry(iso9660, iso9660->rr_moved);
  2475. while ((re = re_get_entry(iso9660)) != NULL) {
  2476. /* Expose its descendant dirs. */
  2477. while ((d = rede_get_entry(re)) != NULL)
  2478. cache_add_entry(iso9660, d);
  2479. }
  2480. if (iso9660->cache_files.first != NULL)
  2481. return (next_cache_entry(a, iso9660, pfile));
  2482. return (ARCHIVE_EOF);
  2483. }
  2484. if (file->cl_offset) {
  2485. struct file_info *first_re = NULL;
  2486. int nexted_re = 0;
  2487. /*
  2488. * Find "RE" dir for the current file, which
  2489. * has "CL" flag.
  2490. */
  2491. while ((re = re_get_entry(iso9660))
  2492. != first_re) {
  2493. if (first_re == NULL)
  2494. first_re = re;
  2495. if (re->offset == file->cl_offset) {
  2496. re->parent->subdirs--;
  2497. re->parent = file->parent;
  2498. re->re = 0;
  2499. if (re->parent->re_descendant) {
  2500. nexted_re = 1;
  2501. re->re_descendant = 1;
  2502. if (rede_add_entry(re) < 0)
  2503. goto fatal_rr;
  2504. /* Move a list of descendants
  2505. * to a new ancestor. */
  2506. while ((d = rede_get_entry(
  2507. re)) != NULL)
  2508. if (rede_add_entry(d)
  2509. < 0)
  2510. goto fatal_rr;
  2511. break;
  2512. }
  2513. /* Replace the current file
  2514. * with "RE" dir */
  2515. *pfile = file = re;
  2516. /* Expose its descendant */
  2517. while ((d = rede_get_entry(
  2518. file)) != NULL)
  2519. cache_add_entry(
  2520. iso9660, d);
  2521. break;
  2522. } else
  2523. re_add_entry(iso9660, re);
  2524. }
  2525. if (nexted_re) {
  2526. /*
  2527. * Do not expose this at this time
  2528. * because we have not gotten its full-path
  2529. * name yet.
  2530. */
  2531. continue;
  2532. }
  2533. } else if ((file->mode & AE_IFMT) == AE_IFDIR) {
  2534. int r;
  2535. /* Read file entries in this dir. */
  2536. r = read_children(a, file);
  2537. if (r != ARCHIVE_OK)
  2538. return (r);
  2539. /*
  2540. * Handle a special dir of Rockridge extensions,
  2541. * "rr_moved".
  2542. */
  2543. if (file->rr_moved) {
  2544. /*
  2545. * If this has only the subdirectories which
  2546. * have "RE" flags, do not expose at this time.
  2547. */
  2548. if (file->rr_moved_has_re_only)
  2549. continue;
  2550. /* Otherwise expose "rr_moved" entry. */
  2551. } else if (file->re) {
  2552. /*
  2553. * Do not expose this at this time
  2554. * because we have not gotten its full-path
  2555. * name yet.
  2556. */
  2557. re_add_entry(iso9660, file);
  2558. continue;
  2559. } else if (file->re_descendant) {
  2560. /*
  2561. * If the top level "RE" entry of this entry
  2562. * is not exposed, we, accordingly, should not
  2563. * expose this entry at this time because
  2564. * we cannot make its proper full-path name.
  2565. */
  2566. if (rede_add_entry(file) == 0)
  2567. continue;
  2568. /* Otherwise we can expose this entry because
  2569. * it seems its top level "RE" has already been
  2570. * exposed. */
  2571. }
  2572. }
  2573. break;
  2574. }
  2575. if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
  2576. return (ARCHIVE_OK);
  2577. count = 0;
  2578. number = file->number;
  2579. iso9660->cache_files.first = NULL;
  2580. iso9660->cache_files.last = &(iso9660->cache_files.first);
  2581. empty_files.first = NULL;
  2582. empty_files.last = &empty_files.first;
  2583. /* Collect files which has the same file serial number.
  2584. * Peek pending_files so that file which number is different
  2585. * is not put bak. */
  2586. while (iso9660->pending_files.used > 0 &&
  2587. (iso9660->pending_files.files[0]->number == -1 ||
  2588. iso9660->pending_files.files[0]->number == number)) {
  2589. if (file->number == -1) {
  2590. /* This file has the same offset
  2591. * but it's wrong offset which empty files
  2592. * and symlink files have.
  2593. * NOTE: This wrong offse was recorded by
  2594. * old mkisofs utility. If ISO images is
  2595. * created by latest mkisofs, this does not
  2596. * happen.
  2597. */
  2598. file->next = NULL;
  2599. *empty_files.last = file;
  2600. empty_files.last = &(file->next);
  2601. } else {
  2602. count++;
  2603. cache_add_entry(iso9660, file);
  2604. }
  2605. file = next_entry(iso9660);
  2606. }
  2607. if (count == 0) {
  2608. *pfile = file;
  2609. return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
  2610. }
  2611. if (file->number == -1) {
  2612. file->next = NULL;
  2613. *empty_files.last = file;
  2614. empty_files.last = &(file->next);
  2615. } else {
  2616. count++;
  2617. cache_add_entry(iso9660, file);
  2618. }
  2619. if (count > 1) {
  2620. /* The count is the same as number of hardlink,
  2621. * so much so that each nlinks of files in cache_file
  2622. * is overwritten by value of the count.
  2623. */
  2624. for (file = iso9660->cache_files.first;
  2625. file != NULL; file = file->next)
  2626. file->nlinks = count;
  2627. }
  2628. /* If there are empty files, that files are added
  2629. * to the tail of the cache_files. */
  2630. if (empty_files.first != NULL) {
  2631. *iso9660->cache_files.last = empty_files.first;
  2632. iso9660->cache_files.last = empty_files.last;
  2633. }
  2634. *pfile = cache_get_entry(iso9660);
  2635. return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
  2636. fatal_rr:
  2637. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2638. "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of "
  2639. "Rockridge extensions: current position = %jd, CL offset = %jd",
  2640. (intmax_t)iso9660->current_position, (intmax_t)file->cl_offset);
  2641. return (ARCHIVE_FATAL);
  2642. }
  2643. static inline void
  2644. re_add_entry(struct iso9660 *iso9660, struct file_info *file)
  2645. {
  2646. file->re_next = NULL;
  2647. *iso9660->re_files.last = file;
  2648. iso9660->re_files.last = &(file->re_next);
  2649. }
  2650. static inline struct file_info *
  2651. re_get_entry(struct iso9660 *iso9660)
  2652. {
  2653. struct file_info *file;
  2654. if ((file = iso9660->re_files.first) != NULL) {
  2655. iso9660->re_files.first = file->re_next;
  2656. if (iso9660->re_files.first == NULL)
  2657. iso9660->re_files.last =
  2658. &(iso9660->re_files.first);
  2659. }
  2660. return (file);
  2661. }
  2662. static inline int
  2663. rede_add_entry(struct file_info *file)
  2664. {
  2665. struct file_info *re;
  2666. /*
  2667. * Find "RE" entry.
  2668. */
  2669. re = file->parent;
  2670. while (re != NULL && !re->re)
  2671. re = re->parent;
  2672. if (re == NULL)
  2673. return (-1);
  2674. file->re_next = NULL;
  2675. *re->rede_files.last = file;
  2676. re->rede_files.last = &(file->re_next);
  2677. return (0);
  2678. }
  2679. static inline struct file_info *
  2680. rede_get_entry(struct file_info *re)
  2681. {
  2682. struct file_info *file;
  2683. if ((file = re->rede_files.first) != NULL) {
  2684. re->rede_files.first = file->re_next;
  2685. if (re->rede_files.first == NULL)
  2686. re->rede_files.last =
  2687. &(re->rede_files.first);
  2688. }
  2689. return (file);
  2690. }
  2691. static inline void
  2692. cache_add_entry(struct iso9660 *iso9660, struct file_info *file)
  2693. {
  2694. file->next = NULL;
  2695. *iso9660->cache_files.last = file;
  2696. iso9660->cache_files.last = &(file->next);
  2697. }
  2698. static inline struct file_info *
  2699. cache_get_entry(struct iso9660 *iso9660)
  2700. {
  2701. struct file_info *file;
  2702. if ((file = iso9660->cache_files.first) != NULL) {
  2703. iso9660->cache_files.first = file->next;
  2704. if (iso9660->cache_files.first == NULL)
  2705. iso9660->cache_files.last =
  2706. &(iso9660->cache_files.first);
  2707. }
  2708. return (file);
  2709. }
  2710. static int
  2711. heap_add_entry(struct archive_read *a, struct heap_queue *heap,
  2712. struct file_info *file, uint64_t key)
  2713. {
  2714. uint64_t file_key, parent_key;
  2715. int hole, parent;
  2716. /* Expand our pending files list as necessary. */
  2717. if (heap->used >= heap->allocated) {
  2718. struct file_info **new_pending_files;
  2719. int new_size = heap->allocated * 2;
  2720. if (heap->allocated < 1024)
  2721. new_size = 1024;
  2722. /* Overflow might keep us from growing the list. */
  2723. if (new_size <= heap->allocated) {
  2724. archive_set_error(&a->archive,
  2725. ENOMEM, "Out of memory");
  2726. return (ARCHIVE_FATAL);
  2727. }
  2728. new_pending_files = (struct file_info **)
  2729. malloc(new_size * sizeof(new_pending_files[0]));
  2730. if (new_pending_files == NULL) {
  2731. archive_set_error(&a->archive,
  2732. ENOMEM, "Out of memory");
  2733. return (ARCHIVE_FATAL);
  2734. }
  2735. memcpy(new_pending_files, heap->files,
  2736. heap->allocated * sizeof(new_pending_files[0]));
  2737. if (heap->files != NULL)
  2738. free(heap->files);
  2739. heap->files = new_pending_files;
  2740. heap->allocated = new_size;
  2741. }
  2742. file_key = file->key = key;
  2743. /*
  2744. * Start with hole at end, walk it up tree to find insertion point.
  2745. */
  2746. hole = heap->used++;
  2747. while (hole > 0) {
  2748. parent = (hole - 1)/2;
  2749. parent_key = heap->files[parent]->key;
  2750. if (file_key >= parent_key) {
  2751. heap->files[hole] = file;
  2752. return (ARCHIVE_OK);
  2753. }
  2754. /* Move parent into hole <==> move hole up tree. */
  2755. heap->files[hole] = heap->files[parent];
  2756. hole = parent;
  2757. }
  2758. heap->files[0] = file;
  2759. return (ARCHIVE_OK);
  2760. }
  2761. static struct file_info *
  2762. heap_get_entry(struct heap_queue *heap)
  2763. {
  2764. uint64_t a_key, b_key, c_key;
  2765. int a, b, c;
  2766. struct file_info *r, *tmp;
  2767. if (heap->used < 1)
  2768. return (NULL);
  2769. /*
  2770. * The first file in the list is the earliest; we'll return this.
  2771. */
  2772. r = heap->files[0];
  2773. /*
  2774. * Move the last item in the heap to the root of the tree
  2775. */
  2776. heap->files[0] = heap->files[--(heap->used)];
  2777. /*
  2778. * Rebalance the heap.
  2779. */
  2780. a = 0; /* Starting element and its heap key */
  2781. a_key = heap->files[a]->key;
  2782. for (;;) {
  2783. b = a + a + 1; /* First child */
  2784. if (b >= heap->used)
  2785. return (r);
  2786. b_key = heap->files[b]->key;
  2787. c = b + 1; /* Use second child if it is smaller. */
  2788. if (c < heap->used) {
  2789. c_key = heap->files[c]->key;
  2790. if (c_key < b_key) {
  2791. b = c;
  2792. b_key = c_key;
  2793. }
  2794. }
  2795. if (a_key <= b_key)
  2796. return (r);
  2797. tmp = heap->files[a];
  2798. heap->files[a] = heap->files[b];
  2799. heap->files[b] = tmp;
  2800. a = b;
  2801. }
  2802. }
  2803. static unsigned int
  2804. toi(const void *p, int n)
  2805. {
  2806. const unsigned char *v = (const unsigned char *)p;
  2807. if (n > 1)
  2808. return v[0] + 256 * toi(v + 1, n - 1);
  2809. if (n == 1)
  2810. return v[0];
  2811. return (0);
  2812. }
  2813. static time_t
  2814. isodate7(const unsigned char *v)
  2815. {
  2816. struct tm tm;
  2817. int offset;
  2818. time_t t;
  2819. memset(&tm, 0, sizeof(tm));
  2820. tm.tm_year = v[0];
  2821. tm.tm_mon = v[1] - 1;
  2822. tm.tm_mday = v[2];
  2823. tm.tm_hour = v[3];
  2824. tm.tm_min = v[4];
  2825. tm.tm_sec = v[5];
  2826. /* v[6] is the signed timezone offset, in 1/4-hour increments. */
  2827. offset = ((const signed char *)v)[6];
  2828. if (offset > -48 && offset < 52) {
  2829. tm.tm_hour -= offset / 4;
  2830. tm.tm_min -= (offset % 4) * 15;
  2831. }
  2832. t = time_from_tm(&tm);
  2833. if (t == (time_t)-1)
  2834. return ((time_t)0);
  2835. return (t);
  2836. }
  2837. static time_t
  2838. isodate17(const unsigned char *v)
  2839. {
  2840. struct tm tm;
  2841. int offset;
  2842. time_t t;
  2843. memset(&tm, 0, sizeof(tm));
  2844. tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
  2845. + (v[2] - '0') * 10 + (v[3] - '0')
  2846. - 1900;
  2847. tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
  2848. tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
  2849. tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
  2850. tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
  2851. tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
  2852. /* v[16] is the signed timezone offset, in 1/4-hour increments. */
  2853. offset = ((const signed char *)v)[16];
  2854. if (offset > -48 && offset < 52) {
  2855. tm.tm_hour -= offset / 4;
  2856. tm.tm_min -= (offset % 4) * 15;
  2857. }
  2858. t = time_from_tm(&tm);
  2859. if (t == (time_t)-1)
  2860. return ((time_t)0);
  2861. return (t);
  2862. }
  2863. static time_t
  2864. time_from_tm(struct tm *t)
  2865. {
  2866. #if HAVE_TIMEGM
  2867. /* Use platform timegm() if available. */
  2868. return (timegm(t));
  2869. #elif HAVE__MKGMTIME64
  2870. return (_mkgmtime64(t));
  2871. #else
  2872. /* Else use direct calculation using POSIX assumptions. */
  2873. /* First, fix up tm_yday based on the year/month/day. */
  2874. if (mktime(t) == (time_t)-1)
  2875. return ((time_t)-1);
  2876. /* Then we can compute timegm() from first principles. */
  2877. return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
  2878. + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
  2879. + ((t->tm_year - 69) / 4) * 86400 -
  2880. ((t->tm_year - 1) / 100) * 86400
  2881. + ((t->tm_year + 299) / 400) * 86400);
  2882. #endif
  2883. }
  2884. static const char *
  2885. build_pathname(struct archive_string *as, struct file_info *file)
  2886. {
  2887. if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
  2888. build_pathname(as, file->parent);
  2889. archive_strcat(as, "/");
  2890. }
  2891. if (archive_strlen(&file->name) == 0)
  2892. archive_strcat(as, ".");
  2893. else
  2894. archive_string_concat(as, &file->name);
  2895. return (as->s);
  2896. }
  2897. static int
  2898. build_pathname_utf16be(unsigned char *p, size_t max, size_t *len,
  2899. struct file_info *file)
  2900. {
  2901. if (file->parent != NULL && file->parent->utf16be_bytes > 0) {
  2902. if (build_pathname_utf16be(p, max, len, file->parent) != 0)
  2903. return (-1);
  2904. p[*len] = 0;
  2905. p[*len + 1] = '/';
  2906. *len += 2;
  2907. }
  2908. if (file->utf16be_bytes == 0) {
  2909. if (*len + 2 > max)
  2910. return (-1);/* Path is too long! */
  2911. p[*len] = 0;
  2912. p[*len + 1] = '.';
  2913. *len += 2;
  2914. } else {
  2915. if (*len + file->utf16be_bytes > max)
  2916. return (-1);/* Path is too long! */
  2917. memcpy(p + *len, file->utf16be_name, file->utf16be_bytes);
  2918. *len += file->utf16be_bytes;
  2919. }
  2920. return (0);
  2921. }
  2922. #if DEBUG
  2923. static void
  2924. dump_isodirrec(FILE *out, const unsigned char *isodirrec)
  2925. {
  2926. fprintf(out, " l %d,",
  2927. toi(isodirrec + DR_length_offset, DR_length_size));
  2928. fprintf(out, " a %d,",
  2929. toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
  2930. fprintf(out, " ext 0x%x,",
  2931. toi(isodirrec + DR_extent_offset, DR_extent_size));
  2932. fprintf(out, " s %d,",
  2933. toi(isodirrec + DR_size_offset, DR_extent_size));
  2934. fprintf(out, " f 0x%x,",
  2935. toi(isodirrec + DR_flags_offset, DR_flags_size));
  2936. fprintf(out, " u %d,",
  2937. toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
  2938. fprintf(out, " ilv %d,",
  2939. toi(isodirrec + DR_interleave_offset, DR_interleave_size));
  2940. fprintf(out, " seq %d,",
  2941. toi(isodirrec + DR_volume_sequence_number_offset,
  2942. DR_volume_sequence_number_size));
  2943. fprintf(out, " nl %d:",
  2944. toi(isodirrec + DR_name_len_offset, DR_name_len_size));
  2945. fprintf(out, " `%.*s'",
  2946. toi(isodirrec + DR_name_len_offset, DR_name_len_size),
  2947. isodirrec + DR_name_offset);
  2948. }
  2949. #endif