archive_read_support_format_iso9660.c 92 KB

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