archive_read_support_format_iso9660.c 92 KB

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