archive_read_support_format_iso9660.c 99 KB

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