archive_read_support_format_iso9660.c 92 KB

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