archive_read_support_format_rar5.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103
  1. /*-
  2. * Copyright (c) 2018 Grzegorz Antoniak (http://antoniak.org)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #include "archive_endian.h"
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include <time.h>
  31. #ifdef HAVE_ZLIB_H
  32. #include <cm_zlib.h> /* crc32 */
  33. #endif
  34. #ifdef HAVE_LIMITS_H
  35. #include <limits.h>
  36. #endif
  37. #include "archive.h"
  38. #ifndef HAVE_ZLIB_H
  39. #include "archive_crc32.h"
  40. #endif
  41. #include "archive_entry.h"
  42. #include "archive_entry_locale.h"
  43. #include "archive_ppmd7_private.h"
  44. #include "archive_entry_private.h"
  45. #ifdef HAVE_BLAKE2_H
  46. #include <blake2.h>
  47. #else
  48. #include "archive_blake2.h"
  49. #endif
  50. /*#define CHECK_CRC_ON_SOLID_SKIP*/
  51. /*#define DONT_FAIL_ON_CRC_ERROR*/
  52. /*#define DEBUG*/
  53. #define rar5_min(a, b) (((a) > (b)) ? (b) : (a))
  54. #define rar5_max(a, b) (((a) > (b)) ? (a) : (b))
  55. #define rar5_countof(X) ((const ssize_t) (sizeof(X) / sizeof(*X)))
  56. #if defined DEBUG
  57. #define DEBUG_CODE if(1)
  58. #define LOG(...) do { printf("rar5: " __VA_ARGS__); puts(""); } while(0)
  59. #else
  60. #define DEBUG_CODE if(0)
  61. #endif
  62. /* Real RAR5 magic number is:
  63. *
  64. * 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00
  65. * "Rar!→•☺·\x00"
  66. *
  67. * Retrieved with `rar5_signature()` by XOR'ing it with 0xA1, because I don't
  68. * want to put this magic sequence in each binary that uses libarchive, so
  69. * applications that scan through the file for this marker won't trigger on
  70. * this "false" one.
  71. *
  72. * The array itself is decrypted in `rar5_init` function. */
  73. static unsigned char rar5_signature_xor[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
  74. static const size_t g_unpack_window_size = 0x20000;
  75. /* These could have been static const's, but they aren't, because of
  76. * Visual Studio. */
  77. #define MAX_NAME_IN_CHARS 2048
  78. #define MAX_NAME_IN_BYTES (4 * MAX_NAME_IN_CHARS)
  79. struct file_header {
  80. ssize_t bytes_remaining;
  81. ssize_t unpacked_size;
  82. int64_t last_offset; /* Used in sanity checks. */
  83. int64_t last_size; /* Used in sanity checks. */
  84. uint8_t solid : 1; /* Is this a solid stream? */
  85. uint8_t service : 1; /* Is this file a service data? */
  86. uint8_t eof : 1; /* Did we finish unpacking the file? */
  87. uint8_t dir : 1; /* Is this file entry a directory? */
  88. /* Optional time fields. */
  89. uint64_t e_mtime;
  90. uint64_t e_ctime;
  91. uint64_t e_atime;
  92. uint32_t e_unix_ns;
  93. /* Optional hash fields. */
  94. uint32_t stored_crc32;
  95. uint32_t calculated_crc32;
  96. uint8_t blake2sp[32];
  97. blake2sp_state b2state;
  98. char has_blake2;
  99. /* Optional redir fields */
  100. uint64_t redir_type;
  101. uint64_t redir_flags;
  102. ssize_t solid_window_size; /* Used in file format check. */
  103. };
  104. enum EXTRA {
  105. EX_CRYPT = 0x01,
  106. EX_HASH = 0x02,
  107. EX_HTIME = 0x03,
  108. EX_VERSION = 0x04,
  109. EX_REDIR = 0x05,
  110. EX_UOWNER = 0x06,
  111. EX_SUBDATA = 0x07
  112. };
  113. #define REDIR_SYMLINK_IS_DIR 1
  114. enum REDIR_TYPE {
  115. REDIR_TYPE_NONE = 0,
  116. REDIR_TYPE_UNIXSYMLINK = 1,
  117. REDIR_TYPE_WINSYMLINK = 2,
  118. REDIR_TYPE_JUNCTION = 3,
  119. REDIR_TYPE_HARDLINK = 4,
  120. REDIR_TYPE_FILECOPY = 5,
  121. };
  122. #define OWNER_USER_NAME 0x01
  123. #define OWNER_GROUP_NAME 0x02
  124. #define OWNER_USER_UID 0x04
  125. #define OWNER_GROUP_GID 0x08
  126. #define OWNER_MAXNAMELEN 256
  127. enum FILTER_TYPE {
  128. FILTER_DELTA = 0, /* Generic pattern. */
  129. FILTER_E8 = 1, /* Intel x86 code. */
  130. FILTER_E8E9 = 2, /* Intel x86 code. */
  131. FILTER_ARM = 3, /* ARM code. */
  132. FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
  133. FILTER_RGB = 5, /* Color palette, not used in RARv5. */
  134. FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
  135. FILTER_PPM = 7, /* Predictive pattern matching, not used in
  136. RARv5. */
  137. FILTER_NONE = 8,
  138. };
  139. struct filter_info {
  140. int type;
  141. int channels;
  142. int pos_r;
  143. int64_t block_start;
  144. ssize_t block_length;
  145. uint16_t width;
  146. };
  147. struct data_ready {
  148. char used;
  149. const uint8_t* buf;
  150. size_t size;
  151. int64_t offset;
  152. };
  153. struct cdeque {
  154. uint16_t beg_pos;
  155. uint16_t end_pos;
  156. uint16_t cap_mask;
  157. uint16_t size;
  158. size_t* arr;
  159. };
  160. struct decode_table {
  161. uint32_t size;
  162. int32_t decode_len[16];
  163. uint32_t decode_pos[16];
  164. uint32_t quick_bits;
  165. uint8_t quick_len[1 << 10];
  166. uint16_t quick_num[1 << 10];
  167. uint16_t decode_num[306];
  168. };
  169. struct comp_state {
  170. /* Flag used to specify if unpacker needs to reinitialize the
  171. uncompression context. */
  172. uint8_t initialized : 1;
  173. /* Flag used when applying filters. */
  174. uint8_t all_filters_applied : 1;
  175. /* Flag used to skip file context reinitialization, used when unpacker
  176. is skipping through different multivolume archives. */
  177. uint8_t switch_multivolume : 1;
  178. /* Flag used to specify if unpacker has processed the whole data block
  179. or just a part of it. */
  180. uint8_t block_parsing_finished : 1;
  181. signed int notused : 4;
  182. int flags; /* Uncompression flags. */
  183. int method; /* Uncompression algorithm method. */
  184. int version; /* Uncompression algorithm version. */
  185. ssize_t window_size; /* Size of window_buf. */
  186. uint8_t* window_buf; /* Circular buffer used during
  187. decompression. */
  188. uint8_t* filtered_buf; /* Buffer used when applying filters. */
  189. const uint8_t* block_buf; /* Buffer used when merging blocks. */
  190. size_t window_mask; /* Convenience field; window_size - 1. */
  191. int64_t write_ptr; /* This amount of data has been unpacked
  192. in the window buffer. */
  193. int64_t last_write_ptr; /* This amount of data has been stored in
  194. the output file. */
  195. int64_t last_unstore_ptr; /* Counter of bytes extracted during
  196. unstoring. This is separate from
  197. last_write_ptr because of how SERVICE
  198. base blocks are handled during skipping
  199. in solid multiarchive archives. */
  200. int64_t solid_offset; /* Additional offset inside the window
  201. buffer, used in unpacking solid
  202. archives. */
  203. ssize_t cur_block_size; /* Size of current data block. */
  204. int last_len; /* Flag used in lzss decompression. */
  205. /* Decode tables used during lzss uncompression. */
  206. #define HUFF_BC 20
  207. struct decode_table bd; /* huffman bit lengths */
  208. #define HUFF_NC 306
  209. struct decode_table ld; /* literals */
  210. #define HUFF_DC 64
  211. struct decode_table dd; /* distances */
  212. #define HUFF_LDC 16
  213. struct decode_table ldd; /* lower bits of distances */
  214. #define HUFF_RC 44
  215. struct decode_table rd; /* repeating distances */
  216. #define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
  217. /* Circular deque for storing filters. */
  218. struct cdeque filters;
  219. int64_t last_block_start; /* Used for sanity checking. */
  220. ssize_t last_block_length; /* Used for sanity checking. */
  221. /* Distance cache used during lzss uncompression. */
  222. int dist_cache[4];
  223. /* Data buffer stack. */
  224. struct data_ready dready[2];
  225. };
  226. /* Bit reader state. */
  227. struct bit_reader {
  228. int8_t bit_addr; /* Current bit pointer inside current byte. */
  229. int in_addr; /* Current byte pointer. */
  230. };
  231. /* RARv5 block header structure. Use bf_* functions to get values from
  232. * block_flags_u8 field. I.e. bf_byte_count, etc. */
  233. struct compressed_block_header {
  234. /* block_flags_u8 contain fields encoded in little-endian bitfield:
  235. *
  236. * - table present flag (shr 7, and 1),
  237. * - last block flag (shr 6, and 1),
  238. * - byte_count (shr 3, and 7),
  239. * - bit_size (shr 0, and 7).
  240. */
  241. uint8_t block_flags_u8;
  242. uint8_t block_cksum;
  243. };
  244. /* RARv5 main header structure. */
  245. struct main_header {
  246. /* Does the archive contain solid streams? */
  247. uint8_t solid : 1;
  248. /* If this a multi-file archive? */
  249. uint8_t volume : 1;
  250. uint8_t endarc : 1;
  251. uint8_t notused : 5;
  252. unsigned int vol_no;
  253. };
  254. struct generic_header {
  255. uint8_t split_after : 1;
  256. uint8_t split_before : 1;
  257. uint8_t padding : 6;
  258. int size;
  259. int last_header_id;
  260. };
  261. struct multivolume {
  262. unsigned int expected_vol_no;
  263. uint8_t* push_buf;
  264. };
  265. /* Main context structure. */
  266. struct rar5 {
  267. int header_initialized;
  268. /* Set to 1 if current file is positioned AFTER the magic value
  269. * of the archive file. This is used in header reading functions. */
  270. int skipped_magic;
  271. /* Set to not zero if we're in skip mode (either by calling
  272. * rar5_data_skip function or when skipping over solid streams).
  273. * Set to 0 when in * extraction mode. This is used during checksum
  274. * calculation functions. */
  275. int skip_mode;
  276. /* Set to not zero if we're in block merging mode (i.e. when switching
  277. * to another file in multivolume archive, last block from 1st archive
  278. * needs to be merged with 1st block from 2nd archive). This flag
  279. * guards against recursive use of the merging function, which doesn't
  280. * support recursive calls. */
  281. int merge_mode;
  282. /* An offset to QuickOpen list. This is not supported by this unpacker,
  283. * because we're focusing on streaming interface. QuickOpen is designed
  284. * to make things quicker for non-stream interfaces, so it's not our
  285. * use case. */
  286. uint64_t qlist_offset;
  287. /* An offset to additional Recovery data. This is not supported by this
  288. * unpacker. Recovery data are additional Reed-Solomon codes that could
  289. * be used to calculate bytes that are missing in archive or are
  290. * corrupted. */
  291. uint64_t rr_offset;
  292. /* Various context variables grouped to different structures. */
  293. struct generic_header generic;
  294. struct main_header main;
  295. struct comp_state cstate;
  296. struct file_header file;
  297. struct bit_reader bits;
  298. struct multivolume vol;
  299. /* The header of currently processed RARv5 block. Used in main
  300. * decompression logic loop. */
  301. struct compressed_block_header last_block_hdr;
  302. };
  303. /* Forward function declarations. */
  304. static void rar5_signature(char *buf);
  305. static int verify_global_checksums(struct archive_read* a);
  306. static int rar5_read_data_skip(struct archive_read *a);
  307. static int push_data_ready(struct archive_read* a, struct rar5* rar,
  308. const uint8_t* buf, size_t size, int64_t offset);
  309. /* CDE_xxx = Circular Double Ended (Queue) return values. */
  310. enum CDE_RETURN_VALUES {
  311. CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
  312. };
  313. /* Clears the contents of this circular deque. */
  314. static void cdeque_clear(struct cdeque* d) {
  315. d->size = 0;
  316. d->beg_pos = 0;
  317. d->end_pos = 0;
  318. }
  319. /* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
  320. * 64, 256, etc. When the user will add another item above current capacity,
  321. * the circular deque will overwrite the oldest entry. */
  322. static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
  323. if(d == NULL || max_capacity_power_of_2 == 0)
  324. return CDE_PARAM;
  325. d->cap_mask = max_capacity_power_of_2 - 1;
  326. d->arr = NULL;
  327. if((max_capacity_power_of_2 & d->cap_mask) != 0)
  328. return CDE_PARAM;
  329. cdeque_clear(d);
  330. d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
  331. return d->arr ? CDE_OK : CDE_ALLOC;
  332. }
  333. /* Return the current size (not capacity) of circular deque `d`. */
  334. static size_t cdeque_size(struct cdeque* d) {
  335. return d->size;
  336. }
  337. /* Returns the first element of current circular deque. Note that this function
  338. * doesn't perform any bounds checking. If you need bounds checking, use
  339. * `cdeque_front()` function instead. */
  340. static void cdeque_front_fast(struct cdeque* d, void** value) {
  341. *value = (void*) d->arr[d->beg_pos];
  342. }
  343. /* Returns the first element of current circular deque. This function
  344. * performs bounds checking. */
  345. static int cdeque_front(struct cdeque* d, void** value) {
  346. if(d->size > 0) {
  347. cdeque_front_fast(d, value);
  348. return CDE_OK;
  349. } else
  350. return CDE_OUT_OF_BOUNDS;
  351. }
  352. /* Pushes a new element into the end of this circular deque object. If current
  353. * size will exceed capacity, the oldest element will be overwritten. */
  354. static int cdeque_push_back(struct cdeque* d, void* item) {
  355. if(d == NULL)
  356. return CDE_PARAM;
  357. if(d->size == d->cap_mask + 1)
  358. return CDE_OUT_OF_BOUNDS;
  359. d->arr[d->end_pos] = (size_t) item;
  360. d->end_pos = (d->end_pos + 1) & d->cap_mask;
  361. d->size++;
  362. return CDE_OK;
  363. }
  364. /* Pops a front element of this circular deque object and returns its value.
  365. * This function doesn't perform any bounds checking. */
  366. static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
  367. *value = (void*) d->arr[d->beg_pos];
  368. d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
  369. d->size--;
  370. }
  371. /* Pops a front element of this circular deque object and returns its value.
  372. * This function performs bounds checking. */
  373. static int cdeque_pop_front(struct cdeque* d, void** value) {
  374. if(!d || !value)
  375. return CDE_PARAM;
  376. if(d->size == 0)
  377. return CDE_OUT_OF_BOUNDS;
  378. cdeque_pop_front_fast(d, value);
  379. return CDE_OK;
  380. }
  381. /* Convenience function to cast filter_info** to void **. */
  382. static void** cdeque_filter_p(struct filter_info** f) {
  383. return (void**) (size_t) f;
  384. }
  385. /* Convenience function to cast filter_info* to void *. */
  386. static void* cdeque_filter(struct filter_info* f) {
  387. return (void**) (size_t) f;
  388. }
  389. /* Destroys this circular deque object. Deallocates the memory of the
  390. * collection buffer, but doesn't deallocate the memory of any pointer passed
  391. * to this deque as a value. */
  392. static void cdeque_free(struct cdeque* d) {
  393. if(!d)
  394. return;
  395. if(!d->arr)
  396. return;
  397. free(d->arr);
  398. d->arr = NULL;
  399. d->beg_pos = -1;
  400. d->end_pos = -1;
  401. d->cap_mask = 0;
  402. }
  403. static inline
  404. uint8_t bf_bit_size(const struct compressed_block_header* hdr) {
  405. return hdr->block_flags_u8 & 7;
  406. }
  407. static inline
  408. uint8_t bf_byte_count(const struct compressed_block_header* hdr) {
  409. return (hdr->block_flags_u8 >> 3) & 7;
  410. }
  411. static inline
  412. uint8_t bf_is_table_present(const struct compressed_block_header* hdr) {
  413. return (hdr->block_flags_u8 >> 7) & 1;
  414. }
  415. static inline struct rar5* get_context(struct archive_read* a) {
  416. return (struct rar5*) a->format->data;
  417. }
  418. /* Convenience functions used by filter implementations. */
  419. static void circular_memcpy(uint8_t* dst, uint8_t* window, const uint64_t mask,
  420. int64_t start, int64_t end)
  421. {
  422. if((start & mask) > (end & mask)) {
  423. ssize_t len1 = mask + 1 - (start & mask);
  424. ssize_t len2 = end & mask;
  425. memcpy(dst, &window[start & mask], len1);
  426. memcpy(dst + len1, window, len2);
  427. } else {
  428. memcpy(dst, &window[start & mask], (size_t) (end - start));
  429. }
  430. }
  431. static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
  432. uint8_t linear_buf[4];
  433. circular_memcpy(linear_buf, rar->cstate.window_buf,
  434. rar->cstate.window_mask, offset, offset + 4);
  435. return archive_le32dec(linear_buf);
  436. }
  437. static void write_filter_data(struct rar5* rar, uint32_t offset,
  438. uint32_t value)
  439. {
  440. archive_le32enc(&rar->cstate.filtered_buf[offset], value);
  441. }
  442. /* Allocates a new filter descriptor and adds it to the filter array. */
  443. static struct filter_info* add_new_filter(struct rar5* rar) {
  444. struct filter_info* f =
  445. (struct filter_info*) calloc(1, sizeof(struct filter_info));
  446. if(!f) {
  447. return NULL;
  448. }
  449. cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
  450. return f;
  451. }
  452. static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
  453. int i;
  454. ssize_t dest_pos, src_pos = 0;
  455. for(i = 0; i < flt->channels; i++) {
  456. uint8_t prev_byte = 0;
  457. for(dest_pos = i;
  458. dest_pos < flt->block_length;
  459. dest_pos += flt->channels)
  460. {
  461. uint8_t byte;
  462. byte = rar->cstate.window_buf[
  463. (rar->cstate.solid_offset + flt->block_start +
  464. src_pos) & rar->cstate.window_mask];
  465. prev_byte -= byte;
  466. rar->cstate.filtered_buf[dest_pos] = prev_byte;
  467. src_pos++;
  468. }
  469. }
  470. return ARCHIVE_OK;
  471. }
  472. static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
  473. int extended)
  474. {
  475. const uint32_t file_size = 0x1000000;
  476. ssize_t i;
  477. circular_memcpy(rar->cstate.filtered_buf,
  478. rar->cstate.window_buf, rar->cstate.window_mask,
  479. rar->cstate.solid_offset + flt->block_start,
  480. rar->cstate.solid_offset + flt->block_start + flt->block_length);
  481. for(i = 0; i < flt->block_length - 4;) {
  482. uint8_t b = rar->cstate.window_buf[
  483. (rar->cstate.solid_offset + flt->block_start +
  484. i++) & rar->cstate.window_mask];
  485. /*
  486. * 0xE8 = x86's call <relative_addr_uint32> (function call)
  487. * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump)
  488. */
  489. if(b == 0xE8 || (extended && b == 0xE9)) {
  490. uint32_t addr;
  491. uint32_t offset = (i + flt->block_start) % file_size;
  492. addr = read_filter_data(rar,
  493. (uint32_t)(rar->cstate.solid_offset +
  494. flt->block_start + i) & rar->cstate.window_mask);
  495. if(addr & 0x80000000) {
  496. if(((addr + offset) & 0x80000000) == 0) {
  497. write_filter_data(rar, (uint32_t)i,
  498. addr + file_size);
  499. }
  500. } else {
  501. if((addr - file_size) & 0x80000000) {
  502. uint32_t naddr = addr - offset;
  503. write_filter_data(rar, (uint32_t)i,
  504. naddr);
  505. }
  506. }
  507. i += 4;
  508. }
  509. }
  510. return ARCHIVE_OK;
  511. }
  512. static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
  513. ssize_t i = 0;
  514. uint32_t offset;
  515. circular_memcpy(rar->cstate.filtered_buf,
  516. rar->cstate.window_buf, rar->cstate.window_mask,
  517. rar->cstate.solid_offset + flt->block_start,
  518. rar->cstate.solid_offset + flt->block_start + flt->block_length);
  519. for(i = 0; i < flt->block_length - 3; i += 4) {
  520. uint8_t* b = &rar->cstate.window_buf[
  521. (rar->cstate.solid_offset +
  522. flt->block_start + i + 3) & rar->cstate.window_mask];
  523. if(*b == 0xEB) {
  524. /* 0xEB = ARM's BL (branch + link) instruction. */
  525. offset = read_filter_data(rar,
  526. (rar->cstate.solid_offset + flt->block_start + i) &
  527. rar->cstate.window_mask) & 0x00ffffff;
  528. offset -= (uint32_t) ((i + flt->block_start) / 4);
  529. offset = (offset & 0x00ffffff) | 0xeb000000;
  530. write_filter_data(rar, (uint32_t)i, offset);
  531. }
  532. }
  533. return ARCHIVE_OK;
  534. }
  535. static int run_filter(struct archive_read* a, struct filter_info* flt) {
  536. int ret;
  537. struct rar5* rar = get_context(a);
  538. free(rar->cstate.filtered_buf);
  539. rar->cstate.filtered_buf = malloc(flt->block_length);
  540. if(!rar->cstate.filtered_buf) {
  541. archive_set_error(&a->archive, ENOMEM,
  542. "Can't allocate memory for filter data.");
  543. return ARCHIVE_FATAL;
  544. }
  545. switch(flt->type) {
  546. case FILTER_DELTA:
  547. ret = run_delta_filter(rar, flt);
  548. break;
  549. case FILTER_E8:
  550. /* fallthrough */
  551. case FILTER_E8E9:
  552. ret = run_e8e9_filter(rar, flt,
  553. flt->type == FILTER_E8E9);
  554. break;
  555. case FILTER_ARM:
  556. ret = run_arm_filter(rar, flt);
  557. break;
  558. default:
  559. archive_set_error(&a->archive,
  560. ARCHIVE_ERRNO_FILE_FORMAT,
  561. "Unsupported filter type: 0x%x", flt->type);
  562. return ARCHIVE_FATAL;
  563. }
  564. if(ret != ARCHIVE_OK) {
  565. /* Filter has failed. */
  566. return ret;
  567. }
  568. if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
  569. flt->block_length, rar->cstate.last_write_ptr))
  570. {
  571. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  572. "Stack overflow when submitting unpacked data");
  573. return ARCHIVE_FATAL;
  574. }
  575. rar->cstate.last_write_ptr += flt->block_length;
  576. return ARCHIVE_OK;
  577. }
  578. /* The `push_data` function submits the selected data range to the user.
  579. * Next call of `use_data` will use the pointer, size and offset arguments
  580. * that are specified here. These arguments are pushed to the FIFO stack here,
  581. * and popped from the stack by the `use_data` function. */
  582. static void push_data(struct archive_read* a, struct rar5* rar,
  583. const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
  584. {
  585. const uint64_t wmask = rar->cstate.window_mask;
  586. const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
  587. rar->cstate.last_write_ptr) & wmask;
  588. idx_begin += rar->cstate.solid_offset;
  589. idx_end += rar->cstate.solid_offset;
  590. /* Check if our unpacked data is wrapped inside the window circular
  591. * buffer. If it's not wrapped, it can be copied out by using
  592. * a single memcpy, but when it's wrapped, we need to copy the first
  593. * part with one memcpy, and the second part with another memcpy. */
  594. if((idx_begin & wmask) > (idx_end & wmask)) {
  595. /* The data is wrapped (begin offset sis bigger than end
  596. * offset). */
  597. const ssize_t frag1_size = rar->cstate.window_size -
  598. (idx_begin & wmask);
  599. const ssize_t frag2_size = idx_end & wmask;
  600. /* Copy the first part of the buffer first. */
  601. push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
  602. rar->cstate.last_write_ptr);
  603. /* Copy the second part of the buffer. */
  604. push_data_ready(a, rar, buf, frag2_size,
  605. rar->cstate.last_write_ptr + frag1_size);
  606. rar->cstate.last_write_ptr += frag1_size + frag2_size;
  607. } else {
  608. /* Data is not wrapped, so we can just use one call to copy the
  609. * data. */
  610. push_data_ready(a, rar,
  611. buf + solid_write_ptr, (idx_end - idx_begin) & wmask,
  612. rar->cstate.last_write_ptr);
  613. rar->cstate.last_write_ptr += idx_end - idx_begin;
  614. }
  615. }
  616. /* Convenience function that submits the data to the user. It uses the
  617. * unpack window buffer as a source location. */
  618. static void push_window_data(struct archive_read* a, struct rar5* rar,
  619. int64_t idx_begin, int64_t idx_end)
  620. {
  621. push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
  622. }
  623. static int apply_filters(struct archive_read* a) {
  624. struct filter_info* flt;
  625. struct rar5* rar = get_context(a);
  626. int ret;
  627. rar->cstate.all_filters_applied = 0;
  628. /* Get the first filter that can be applied to our data. The data
  629. * needs to be fully unpacked before the filter can be run. */
  630. if(CDE_OK == cdeque_front(&rar->cstate.filters,
  631. cdeque_filter_p(&flt))) {
  632. /* Check if our unpacked data fully covers this filter's
  633. * range. */
  634. if(rar->cstate.write_ptr > flt->block_start &&
  635. rar->cstate.write_ptr >= flt->block_start +
  636. flt->block_length) {
  637. /* Check if we have some data pending to be written
  638. * right before the filter's start offset. */
  639. if(rar->cstate.last_write_ptr == flt->block_start) {
  640. /* Run the filter specified by descriptor
  641. * `flt`. */
  642. ret = run_filter(a, flt);
  643. if(ret != ARCHIVE_OK) {
  644. /* Filter failure, return error. */
  645. return ret;
  646. }
  647. /* Filter descriptor won't be needed anymore
  648. * after it's used, * so remove it from the
  649. * filter list and free its memory. */
  650. (void) cdeque_pop_front(&rar->cstate.filters,
  651. cdeque_filter_p(&flt));
  652. free(flt);
  653. } else {
  654. /* We can't run filters yet, dump the memory
  655. * right before the filter. */
  656. push_window_data(a, rar,
  657. rar->cstate.last_write_ptr,
  658. flt->block_start);
  659. }
  660. /* Return 'filter applied or not needed' state to the
  661. * caller. */
  662. return ARCHIVE_RETRY;
  663. }
  664. }
  665. rar->cstate.all_filters_applied = 1;
  666. return ARCHIVE_OK;
  667. }
  668. static void dist_cache_push(struct rar5* rar, int value) {
  669. int* q = rar->cstate.dist_cache;
  670. q[3] = q[2];
  671. q[2] = q[1];
  672. q[1] = q[0];
  673. q[0] = value;
  674. }
  675. static int dist_cache_touch(struct rar5* rar, int idx) {
  676. int* q = rar->cstate.dist_cache;
  677. int i, dist = q[idx];
  678. for(i = idx; i > 0; i--)
  679. q[i] = q[i - 1];
  680. q[0] = dist;
  681. return dist;
  682. }
  683. static void free_filters(struct rar5* rar) {
  684. struct cdeque* d = &rar->cstate.filters;
  685. /* Free any remaining filters. All filters should be naturally
  686. * consumed by the unpacking function, so remaining filters after
  687. * unpacking normally mean that unpacking wasn't successful.
  688. * But still of course we shouldn't leak memory in such case. */
  689. /* cdeque_size() is a fast operation, so we can use it as a loop
  690. * expression. */
  691. while(cdeque_size(d) > 0) {
  692. struct filter_info* f = NULL;
  693. /* Pop_front will also decrease the collection's size. */
  694. if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
  695. free(f);
  696. }
  697. cdeque_clear(d);
  698. /* Also clear out the variables needed for sanity checking. */
  699. rar->cstate.last_block_start = 0;
  700. rar->cstate.last_block_length = 0;
  701. }
  702. static void reset_file_context(struct rar5* rar) {
  703. memset(&rar->file, 0, sizeof(rar->file));
  704. blake2sp_init(&rar->file.b2state, 32);
  705. if(rar->main.solid) {
  706. rar->cstate.solid_offset += rar->cstate.write_ptr;
  707. } else {
  708. rar->cstate.solid_offset = 0;
  709. }
  710. rar->cstate.write_ptr = 0;
  711. rar->cstate.last_write_ptr = 0;
  712. rar->cstate.last_unstore_ptr = 0;
  713. rar->file.redir_type = REDIR_TYPE_NONE;
  714. rar->file.redir_flags = 0;
  715. free_filters(rar);
  716. }
  717. static inline int get_archive_read(struct archive* a,
  718. struct archive_read** ar)
  719. {
  720. *ar = (struct archive_read*) a;
  721. archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  722. "archive_read_support_format_rar5");
  723. return ARCHIVE_OK;
  724. }
  725. static int read_ahead(struct archive_read* a, size_t how_many,
  726. const uint8_t** ptr)
  727. {
  728. ssize_t avail = -1;
  729. if(!ptr)
  730. return 0;
  731. *ptr = __archive_read_ahead(a, how_many, &avail);
  732. if(*ptr == NULL) {
  733. return 0;
  734. }
  735. return 1;
  736. }
  737. static int consume(struct archive_read* a, int64_t how_many) {
  738. int ret;
  739. ret = how_many == __archive_read_consume(a, how_many)
  740. ? ARCHIVE_OK
  741. : ARCHIVE_FATAL;
  742. return ret;
  743. }
  744. /**
  745. * Read a RAR5 variable sized numeric value. This value will be stored in
  746. * `pvalue`. The `pvalue_len` argument points to a variable that will receive
  747. * the byte count that was consumed in order to decode the `pvalue` value, plus
  748. * one.
  749. *
  750. * pvalue_len is optional and can be NULL.
  751. *
  752. * NOTE: if `pvalue_len` is NOT NULL, the caller needs to manually consume
  753. * the number of bytes that `pvalue_len` value contains. If the `pvalue_len`
  754. * is NULL, this consuming operation is done automatically.
  755. *
  756. * Returns 1 if *pvalue was successfully read.
  757. * Returns 0 if there was an error. In this case, *pvalue contains an
  758. * invalid value.
  759. */
  760. static int read_var(struct archive_read* a, uint64_t* pvalue,
  761. uint64_t* pvalue_len)
  762. {
  763. uint64_t result = 0;
  764. size_t shift, i;
  765. const uint8_t* p;
  766. uint8_t b;
  767. /* We will read maximum of 8 bytes. We don't have to handle the
  768. * situation to read the RAR5 variable-sized value stored at the end of
  769. * the file, because such situation will never happen. */
  770. if(!read_ahead(a, 8, &p))
  771. return 0;
  772. for(shift = 0, i = 0; i < 8; i++, shift += 7) {
  773. b = p[i];
  774. /* Strip the MSB from the input byte and add the resulting
  775. * number to the `result`. */
  776. result += (b & (uint64_t)0x7F) << shift;
  777. /* MSB set to 1 means we need to continue decoding process.
  778. * MSB set to 0 means we're done.
  779. *
  780. * This conditional checks for the second case. */
  781. if((b & 0x80) == 0) {
  782. if(pvalue) {
  783. *pvalue = result;
  784. }
  785. /* If the caller has passed the `pvalue_len` pointer,
  786. * store the number of consumed bytes in it and do NOT
  787. * consume those bytes, since the caller has all the
  788. * information it needs to perform */
  789. if(pvalue_len) {
  790. *pvalue_len = 1 + i;
  791. } else {
  792. /* If the caller did not provide the
  793. * `pvalue_len` pointer, it will not have the
  794. * possibility to advance the file pointer,
  795. * because it will not know how many bytes it
  796. * needs to consume. This is why we handle
  797. * such situation here automatically. */
  798. if(ARCHIVE_OK != consume(a, 1 + i)) {
  799. return 0;
  800. }
  801. }
  802. /* End of decoding process, return success. */
  803. return 1;
  804. }
  805. }
  806. /* The decoded value takes the maximum number of 8 bytes.
  807. * It's a maximum number of bytes, so end decoding process here
  808. * even if the first bit of last byte is 1. */
  809. if(pvalue) {
  810. *pvalue = result;
  811. }
  812. if(pvalue_len) {
  813. *pvalue_len = 9;
  814. } else {
  815. if(ARCHIVE_OK != consume(a, 9)) {
  816. return 0;
  817. }
  818. }
  819. return 1;
  820. }
  821. static int read_var_sized(struct archive_read* a, size_t* pvalue,
  822. size_t* pvalue_len)
  823. {
  824. uint64_t v;
  825. uint64_t v_size = 0;
  826. const int ret = pvalue_len ? read_var(a, &v, &v_size)
  827. : read_var(a, &v, NULL);
  828. if(ret == 1 && pvalue) {
  829. *pvalue = (size_t) v;
  830. }
  831. if(pvalue_len) {
  832. /* Possible data truncation should be safe. */
  833. *pvalue_len = (size_t) v_size;
  834. }
  835. return ret;
  836. }
  837. static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
  838. uint32_t bits = ((uint32_t) p[rar->bits.in_addr]) << 24;
  839. bits |= p[rar->bits.in_addr + 1] << 16;
  840. bits |= p[rar->bits.in_addr + 2] << 8;
  841. bits |= p[rar->bits.in_addr + 3];
  842. bits <<= rar->bits.bit_addr;
  843. bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
  844. *value = bits;
  845. return ARCHIVE_OK;
  846. }
  847. static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
  848. int bits = (int) ((uint32_t) p[rar->bits.in_addr]) << 16;
  849. bits |= (int) p[rar->bits.in_addr + 1] << 8;
  850. bits |= (int) p[rar->bits.in_addr + 2];
  851. bits >>= (8 - rar->bits.bit_addr);
  852. *value = bits & 0xffff;
  853. return ARCHIVE_OK;
  854. }
  855. static void skip_bits(struct rar5* rar, int bits) {
  856. const int new_bits = rar->bits.bit_addr + bits;
  857. rar->bits.in_addr += new_bits >> 3;
  858. rar->bits.bit_addr = new_bits & 7;
  859. }
  860. /* n = up to 16 */
  861. static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
  862. int* value)
  863. {
  864. uint16_t v;
  865. int ret, num;
  866. if(n == 0 || n > 16) {
  867. /* This is a programmer error and should never happen
  868. * in runtime. */
  869. return ARCHIVE_FATAL;
  870. }
  871. ret = read_bits_16(rar, p, &v);
  872. if(ret != ARCHIVE_OK)
  873. return ret;
  874. num = (int) v;
  875. num >>= 16 - n;
  876. skip_bits(rar, n);
  877. if(value)
  878. *value = num;
  879. return ARCHIVE_OK;
  880. }
  881. static int read_u32(struct archive_read* a, uint32_t* pvalue) {
  882. const uint8_t* p;
  883. if(!read_ahead(a, 4, &p))
  884. return 0;
  885. *pvalue = archive_le32dec(p);
  886. return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
  887. }
  888. static int read_u64(struct archive_read* a, uint64_t* pvalue) {
  889. const uint8_t* p;
  890. if(!read_ahead(a, 8, &p))
  891. return 0;
  892. *pvalue = archive_le64dec(p);
  893. return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
  894. }
  895. static int bid_standard(struct archive_read* a) {
  896. const uint8_t* p;
  897. char signature[sizeof(rar5_signature_xor)];
  898. rar5_signature(signature);
  899. if(!read_ahead(a, sizeof(rar5_signature_xor), &p))
  900. return -1;
  901. if(!memcmp(signature, p, sizeof(rar5_signature_xor)))
  902. return 30;
  903. return -1;
  904. }
  905. static int rar5_bid(struct archive_read* a, int best_bid) {
  906. int my_bid;
  907. if(best_bid > 30)
  908. return -1;
  909. my_bid = bid_standard(a);
  910. if(my_bid > -1) {
  911. return my_bid;
  912. }
  913. return -1;
  914. }
  915. static int rar5_options(struct archive_read *a, const char *key,
  916. const char *val) {
  917. (void) a;
  918. (void) key;
  919. (void) val;
  920. /* No options supported in this version. Return the ARCHIVE_WARN code
  921. * to signal the options supervisor that the unpacker didn't handle
  922. * setting this option. */
  923. return ARCHIVE_WARN;
  924. }
  925. static void init_header(struct archive_read* a) {
  926. a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
  927. a->archive.archive_format_name = "RAR5";
  928. }
  929. static void init_window_mask(struct rar5* rar) {
  930. if (rar->cstate.window_size)
  931. rar->cstate.window_mask = rar->cstate.window_size - 1;
  932. else
  933. rar->cstate.window_mask = 0;
  934. }
  935. enum HEADER_FLAGS {
  936. HFL_EXTRA_DATA = 0x0001,
  937. HFL_DATA = 0x0002,
  938. HFL_SKIP_IF_UNKNOWN = 0x0004,
  939. HFL_SPLIT_BEFORE = 0x0008,
  940. HFL_SPLIT_AFTER = 0x0010,
  941. HFL_CHILD = 0x0020,
  942. HFL_INHERITED = 0x0040
  943. };
  944. static int process_main_locator_extra_block(struct archive_read* a,
  945. struct rar5* rar)
  946. {
  947. uint64_t locator_flags;
  948. enum LOCATOR_FLAGS {
  949. QLIST = 0x01, RECOVERY = 0x02,
  950. };
  951. if(!read_var(a, &locator_flags, NULL)) {
  952. return ARCHIVE_EOF;
  953. }
  954. if(locator_flags & QLIST) {
  955. if(!read_var(a, &rar->qlist_offset, NULL)) {
  956. return ARCHIVE_EOF;
  957. }
  958. /* qlist is not used */
  959. }
  960. if(locator_flags & RECOVERY) {
  961. if(!read_var(a, &rar->rr_offset, NULL)) {
  962. return ARCHIVE_EOF;
  963. }
  964. /* rr is not used */
  965. }
  966. return ARCHIVE_OK;
  967. }
  968. static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
  969. ssize_t* extra_data_size)
  970. {
  971. size_t hash_type = 0;
  972. size_t value_len;
  973. enum HASH_TYPE {
  974. BLAKE2sp = 0x00
  975. };
  976. if(!read_var_sized(a, &hash_type, &value_len))
  977. return ARCHIVE_EOF;
  978. *extra_data_size -= value_len;
  979. if(ARCHIVE_OK != consume(a, value_len)) {
  980. return ARCHIVE_EOF;
  981. }
  982. /* The file uses BLAKE2sp checksum algorithm instead of plain old
  983. * CRC32. */
  984. if(hash_type == BLAKE2sp) {
  985. const uint8_t* p;
  986. const int hash_size = sizeof(rar->file.blake2sp);
  987. if(!read_ahead(a, hash_size, &p))
  988. return ARCHIVE_EOF;
  989. rar->file.has_blake2 = 1;
  990. memcpy(&rar->file.blake2sp, p, hash_size);
  991. if(ARCHIVE_OK != consume(a, hash_size)) {
  992. return ARCHIVE_EOF;
  993. }
  994. *extra_data_size -= hash_size;
  995. } else {
  996. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  997. "Unsupported hash type (0x%x)", (int) hash_type);
  998. return ARCHIVE_FATAL;
  999. }
  1000. return ARCHIVE_OK;
  1001. }
  1002. static uint64_t time_win_to_unix(uint64_t win_time) {
  1003. const size_t ns_in_sec = 10000000;
  1004. const uint64_t sec_to_unix = 11644473600LL;
  1005. return win_time / ns_in_sec - sec_to_unix;
  1006. }
  1007. static int parse_htime_item(struct archive_read* a, char unix_time,
  1008. uint64_t* where, ssize_t* extra_data_size)
  1009. {
  1010. if(unix_time) {
  1011. uint32_t time_val;
  1012. if(!read_u32(a, &time_val))
  1013. return ARCHIVE_EOF;
  1014. *extra_data_size -= 4;
  1015. *where = (uint64_t) time_val;
  1016. } else {
  1017. uint64_t windows_time;
  1018. if(!read_u64(a, &windows_time))
  1019. return ARCHIVE_EOF;
  1020. *where = time_win_to_unix(windows_time);
  1021. *extra_data_size -= 8;
  1022. }
  1023. return ARCHIVE_OK;
  1024. }
  1025. static int parse_file_extra_version(struct archive_read* a,
  1026. struct archive_entry* e, ssize_t* extra_data_size)
  1027. {
  1028. size_t flags = 0;
  1029. size_t version = 0;
  1030. size_t value_len = 0;
  1031. struct archive_string version_string;
  1032. struct archive_string name_utf8_string;
  1033. const char* cur_filename;
  1034. /* Flags are ignored. */
  1035. if(!read_var_sized(a, &flags, &value_len))
  1036. return ARCHIVE_EOF;
  1037. *extra_data_size -= value_len;
  1038. if(ARCHIVE_OK != consume(a, value_len))
  1039. return ARCHIVE_EOF;
  1040. if(!read_var_sized(a, &version, &value_len))
  1041. return ARCHIVE_EOF;
  1042. *extra_data_size -= value_len;
  1043. if(ARCHIVE_OK != consume(a, value_len))
  1044. return ARCHIVE_EOF;
  1045. /* extra_data_size should be zero here. */
  1046. cur_filename = archive_entry_pathname_utf8(e);
  1047. if(cur_filename == NULL) {
  1048. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1049. "Version entry without file name");
  1050. return ARCHIVE_FATAL;
  1051. }
  1052. archive_string_init(&version_string);
  1053. archive_string_init(&name_utf8_string);
  1054. /* Prepare a ;123 suffix for the filename, where '123' is the version
  1055. * value of this file. */
  1056. archive_string_sprintf(&version_string, ";%zu", version);
  1057. /* Build the new filename. */
  1058. archive_strcat(&name_utf8_string, cur_filename);
  1059. archive_strcat(&name_utf8_string, version_string.s);
  1060. /* Apply the new filename into this file's context. */
  1061. archive_entry_update_pathname_utf8(e, name_utf8_string.s);
  1062. /* Free buffers. */
  1063. archive_string_free(&version_string);
  1064. archive_string_free(&name_utf8_string);
  1065. return ARCHIVE_OK;
  1066. }
  1067. static int parse_file_extra_htime(struct archive_read* a,
  1068. struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
  1069. {
  1070. char unix_time = 0;
  1071. size_t flags = 0;
  1072. size_t value_len;
  1073. enum HTIME_FLAGS {
  1074. IS_UNIX = 0x01,
  1075. HAS_MTIME = 0x02,
  1076. HAS_CTIME = 0x04,
  1077. HAS_ATIME = 0x08,
  1078. HAS_UNIX_NS = 0x10,
  1079. };
  1080. if(!read_var_sized(a, &flags, &value_len))
  1081. return ARCHIVE_EOF;
  1082. *extra_data_size -= value_len;
  1083. if(ARCHIVE_OK != consume(a, value_len)) {
  1084. return ARCHIVE_EOF;
  1085. }
  1086. unix_time = flags & IS_UNIX;
  1087. if(flags & HAS_MTIME) {
  1088. parse_htime_item(a, unix_time, &rar->file.e_mtime,
  1089. extra_data_size);
  1090. archive_entry_set_mtime(e, rar->file.e_mtime, 0);
  1091. }
  1092. if(flags & HAS_CTIME) {
  1093. parse_htime_item(a, unix_time, &rar->file.e_ctime,
  1094. extra_data_size);
  1095. archive_entry_set_ctime(e, rar->file.e_ctime, 0);
  1096. }
  1097. if(flags & HAS_ATIME) {
  1098. parse_htime_item(a, unix_time, &rar->file.e_atime,
  1099. extra_data_size);
  1100. archive_entry_set_atime(e, rar->file.e_atime, 0);
  1101. }
  1102. if(flags & HAS_UNIX_NS) {
  1103. if(!read_u32(a, &rar->file.e_unix_ns))
  1104. return ARCHIVE_EOF;
  1105. *extra_data_size -= 4;
  1106. }
  1107. return ARCHIVE_OK;
  1108. }
  1109. static int parse_file_extra_redir(struct archive_read* a,
  1110. struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
  1111. {
  1112. uint64_t value_size = 0;
  1113. size_t target_size = 0;
  1114. char target_utf8_buf[MAX_NAME_IN_BYTES];
  1115. const uint8_t* p;
  1116. if(!read_var(a, &rar->file.redir_type, &value_size))
  1117. return ARCHIVE_EOF;
  1118. if(ARCHIVE_OK != consume(a, (int64_t)value_size))
  1119. return ARCHIVE_EOF;
  1120. *extra_data_size -= value_size;
  1121. if(!read_var(a, &rar->file.redir_flags, &value_size))
  1122. return ARCHIVE_EOF;
  1123. if(ARCHIVE_OK != consume(a, (int64_t)value_size))
  1124. return ARCHIVE_EOF;
  1125. *extra_data_size -= value_size;
  1126. if(!read_var_sized(a, &target_size, NULL))
  1127. return ARCHIVE_EOF;
  1128. *extra_data_size -= target_size + 1;
  1129. if(!read_ahead(a, target_size, &p))
  1130. return ARCHIVE_EOF;
  1131. if(target_size > (MAX_NAME_IN_CHARS - 1)) {
  1132. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1133. "Link target is too long");
  1134. return ARCHIVE_FATAL;
  1135. }
  1136. if(target_size == 0) {
  1137. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1138. "No link target specified");
  1139. return ARCHIVE_FATAL;
  1140. }
  1141. memcpy(target_utf8_buf, p, target_size);
  1142. target_utf8_buf[target_size] = 0;
  1143. if(ARCHIVE_OK != consume(a, (int64_t)target_size))
  1144. return ARCHIVE_EOF;
  1145. switch(rar->file.redir_type) {
  1146. case REDIR_TYPE_UNIXSYMLINK:
  1147. case REDIR_TYPE_WINSYMLINK:
  1148. archive_entry_set_filetype(e, AE_IFLNK);
  1149. archive_entry_update_symlink_utf8(e, target_utf8_buf);
  1150. if (rar->file.redir_flags & REDIR_SYMLINK_IS_DIR) {
  1151. archive_entry_set_symlink_type(e,
  1152. AE_SYMLINK_TYPE_DIRECTORY);
  1153. } else {
  1154. archive_entry_set_symlink_type(e,
  1155. AE_SYMLINK_TYPE_FILE);
  1156. }
  1157. break;
  1158. case REDIR_TYPE_HARDLINK:
  1159. archive_entry_set_filetype(e, AE_IFREG);
  1160. archive_entry_update_hardlink_utf8(e, target_utf8_buf);
  1161. break;
  1162. default:
  1163. /* Unknown redir type, skip it. */
  1164. break;
  1165. }
  1166. return ARCHIVE_OK;
  1167. }
  1168. static int parse_file_extra_owner(struct archive_read* a,
  1169. struct archive_entry* e, ssize_t* extra_data_size)
  1170. {
  1171. uint64_t flags = 0;
  1172. uint64_t value_size = 0;
  1173. uint64_t id = 0;
  1174. size_t name_len = 0;
  1175. size_t name_size = 0;
  1176. char namebuf[OWNER_MAXNAMELEN];
  1177. const uint8_t* p;
  1178. if(!read_var(a, &flags, &value_size))
  1179. return ARCHIVE_EOF;
  1180. if(ARCHIVE_OK != consume(a, (int64_t)value_size))
  1181. return ARCHIVE_EOF;
  1182. *extra_data_size -= value_size;
  1183. if ((flags & OWNER_USER_NAME) != 0) {
  1184. if(!read_var_sized(a, &name_size, NULL))
  1185. return ARCHIVE_EOF;
  1186. *extra_data_size -= name_size + 1;
  1187. if(!read_ahead(a, name_size, &p))
  1188. return ARCHIVE_EOF;
  1189. if (name_size >= OWNER_MAXNAMELEN) {
  1190. name_len = OWNER_MAXNAMELEN - 1;
  1191. } else {
  1192. name_len = name_size;
  1193. }
  1194. memcpy(namebuf, p, name_len);
  1195. namebuf[name_len] = 0;
  1196. if(ARCHIVE_OK != consume(a, (int64_t)name_size))
  1197. return ARCHIVE_EOF;
  1198. archive_entry_set_uname(e, namebuf);
  1199. }
  1200. if ((flags & OWNER_GROUP_NAME) != 0) {
  1201. if(!read_var_sized(a, &name_size, NULL))
  1202. return ARCHIVE_EOF;
  1203. *extra_data_size -= name_size + 1;
  1204. if(!read_ahead(a, name_size, &p))
  1205. return ARCHIVE_EOF;
  1206. if (name_size >= OWNER_MAXNAMELEN) {
  1207. name_len = OWNER_MAXNAMELEN - 1;
  1208. } else {
  1209. name_len = name_size;
  1210. }
  1211. memcpy(namebuf, p, name_len);
  1212. namebuf[name_len] = 0;
  1213. if(ARCHIVE_OK != consume(a, (int64_t)name_size))
  1214. return ARCHIVE_EOF;
  1215. archive_entry_set_gname(e, namebuf);
  1216. }
  1217. if ((flags & OWNER_USER_UID) != 0) {
  1218. if(!read_var(a, &id, &value_size))
  1219. return ARCHIVE_EOF;
  1220. if(ARCHIVE_OK != consume(a, (int64_t)value_size))
  1221. return ARCHIVE_EOF;
  1222. *extra_data_size -= value_size;
  1223. archive_entry_set_uid(e, (la_int64_t)id);
  1224. }
  1225. if ((flags & OWNER_GROUP_GID) != 0) {
  1226. if(!read_var(a, &id, &value_size))
  1227. return ARCHIVE_EOF;
  1228. if(ARCHIVE_OK != consume(a, (int64_t)value_size))
  1229. return ARCHIVE_EOF;
  1230. *extra_data_size -= value_size;
  1231. archive_entry_set_gid(e, (la_int64_t)id);
  1232. }
  1233. return ARCHIVE_OK;
  1234. }
  1235. static int process_head_file_extra(struct archive_read* a,
  1236. struct archive_entry* e, struct rar5* rar, ssize_t extra_data_size)
  1237. {
  1238. size_t extra_field_size;
  1239. size_t extra_field_id = 0;
  1240. int ret = ARCHIVE_FATAL;
  1241. size_t var_size;
  1242. while(extra_data_size > 0) {
  1243. if(!read_var_sized(a, &extra_field_size, &var_size))
  1244. return ARCHIVE_EOF;
  1245. extra_data_size -= var_size;
  1246. if(ARCHIVE_OK != consume(a, var_size)) {
  1247. return ARCHIVE_EOF;
  1248. }
  1249. if(!read_var_sized(a, &extra_field_id, &var_size))
  1250. return ARCHIVE_EOF;
  1251. extra_data_size -= var_size;
  1252. if(ARCHIVE_OK != consume(a, var_size)) {
  1253. return ARCHIVE_EOF;
  1254. }
  1255. switch(extra_field_id) {
  1256. case EX_HASH:
  1257. ret = parse_file_extra_hash(a, rar,
  1258. &extra_data_size);
  1259. break;
  1260. case EX_HTIME:
  1261. ret = parse_file_extra_htime(a, e, rar,
  1262. &extra_data_size);
  1263. break;
  1264. case EX_REDIR:
  1265. ret = parse_file_extra_redir(a, e, rar,
  1266. &extra_data_size);
  1267. break;
  1268. case EX_UOWNER:
  1269. ret = parse_file_extra_owner(a, e,
  1270. &extra_data_size);
  1271. break;
  1272. case EX_VERSION:
  1273. ret = parse_file_extra_version(a, e,
  1274. &extra_data_size);
  1275. break;
  1276. case EX_CRYPT:
  1277. /* fallthrough */
  1278. case EX_SUBDATA:
  1279. /* fallthrough */
  1280. default:
  1281. /* Skip unsupported entry. */
  1282. return consume(a, extra_data_size);
  1283. }
  1284. }
  1285. if(ret != ARCHIVE_OK) {
  1286. /* Attribute not implemented. */
  1287. return ret;
  1288. }
  1289. return ARCHIVE_OK;
  1290. }
  1291. static int process_head_file(struct archive_read* a, struct rar5* rar,
  1292. struct archive_entry* entry, size_t block_flags)
  1293. {
  1294. ssize_t extra_data_size = 0;
  1295. size_t data_size = 0;
  1296. size_t file_flags = 0;
  1297. size_t file_attr = 0;
  1298. size_t compression_info = 0;
  1299. size_t host_os = 0;
  1300. size_t name_size = 0;
  1301. uint64_t unpacked_size, window_size;
  1302. uint32_t mtime = 0, crc = 0;
  1303. int c_method = 0, c_version = 0;
  1304. char name_utf8_buf[MAX_NAME_IN_BYTES];
  1305. const uint8_t* p;
  1306. enum FILE_FLAGS {
  1307. DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
  1308. UNKNOWN_UNPACKED_SIZE = 0x0008,
  1309. };
  1310. enum FILE_ATTRS {
  1311. ATTR_READONLY = 0x1, ATTR_HIDDEN = 0x2, ATTR_SYSTEM = 0x4,
  1312. ATTR_DIRECTORY = 0x10,
  1313. };
  1314. enum COMP_INFO_FLAGS {
  1315. SOLID = 0x0040,
  1316. };
  1317. enum HOST_OS {
  1318. HOST_WINDOWS = 0,
  1319. HOST_UNIX = 1,
  1320. };
  1321. archive_entry_clear(entry);
  1322. /* Do not reset file context if we're switching archives. */
  1323. if(!rar->cstate.switch_multivolume) {
  1324. reset_file_context(rar);
  1325. }
  1326. if(block_flags & HFL_EXTRA_DATA) {
  1327. size_t edata_size = 0;
  1328. if(!read_var_sized(a, &edata_size, NULL))
  1329. return ARCHIVE_EOF;
  1330. /* Intentional type cast from unsigned to signed. */
  1331. extra_data_size = (ssize_t) edata_size;
  1332. }
  1333. if(block_flags & HFL_DATA) {
  1334. if(!read_var_sized(a, &data_size, NULL))
  1335. return ARCHIVE_EOF;
  1336. rar->file.bytes_remaining = data_size;
  1337. } else {
  1338. rar->file.bytes_remaining = 0;
  1339. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1340. "no data found in file/service block");
  1341. return ARCHIVE_FATAL;
  1342. }
  1343. if(!read_var_sized(a, &file_flags, NULL))
  1344. return ARCHIVE_EOF;
  1345. if(!read_var(a, &unpacked_size, NULL))
  1346. return ARCHIVE_EOF;
  1347. if(file_flags & UNKNOWN_UNPACKED_SIZE) {
  1348. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1349. "Files with unknown unpacked size are not supported");
  1350. return ARCHIVE_FATAL;
  1351. }
  1352. rar->file.dir = (uint8_t) ((file_flags & DIRECTORY) > 0);
  1353. if(!read_var_sized(a, &file_attr, NULL))
  1354. return ARCHIVE_EOF;
  1355. if(file_flags & UTIME) {
  1356. if(!read_u32(a, &mtime))
  1357. return ARCHIVE_EOF;
  1358. }
  1359. if(file_flags & CRC32) {
  1360. if(!read_u32(a, &crc))
  1361. return ARCHIVE_EOF;
  1362. }
  1363. if(!read_var_sized(a, &compression_info, NULL))
  1364. return ARCHIVE_EOF;
  1365. c_method = (int) (compression_info >> 7) & 0x7;
  1366. c_version = (int) (compression_info & 0x3f);
  1367. /* RAR5 seems to limit the dictionary size to 64MB. */
  1368. window_size = (rar->file.dir > 0) ?
  1369. 0 :
  1370. g_unpack_window_size << ((compression_info >> 10) & 15);
  1371. rar->cstate.method = c_method;
  1372. rar->cstate.version = c_version + 50;
  1373. rar->file.solid = (compression_info & SOLID) > 0;
  1374. /* Archives which declare solid files without initializing the window
  1375. * buffer first are invalid. */
  1376. if(rar->file.solid > 0 && rar->cstate.window_buf == NULL) {
  1377. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1378. "Declared solid file, but no window buffer "
  1379. "initialized yet.");
  1380. return ARCHIVE_FATAL;
  1381. }
  1382. /* Check if window_size is a sane value. Also, if the file is not
  1383. * declared as a directory, disallow window_size == 0. */
  1384. if(window_size > (64 * 1024 * 1024) ||
  1385. (rar->file.dir == 0 && window_size == 0))
  1386. {
  1387. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1388. "Declared dictionary size is not supported.");
  1389. return ARCHIVE_FATAL;
  1390. }
  1391. if(rar->file.solid > 0) {
  1392. /* Re-check if current window size is the same as previous
  1393. * window size (for solid files only). */
  1394. if(rar->file.solid_window_size > 0 &&
  1395. rar->file.solid_window_size != (ssize_t) window_size)
  1396. {
  1397. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1398. "Window size for this solid file doesn't match "
  1399. "the window size used in previous solid file. ");
  1400. return ARCHIVE_FATAL;
  1401. }
  1402. }
  1403. /* If we're currently switching volumes, ignore the new definition of
  1404. * window_size. */
  1405. if(rar->cstate.switch_multivolume == 0) {
  1406. /* Values up to 64M should fit into ssize_t on every
  1407. * architecture. */
  1408. rar->cstate.window_size = (ssize_t) window_size;
  1409. }
  1410. if(rar->file.solid > 0 && rar->file.solid_window_size == 0) {
  1411. /* Solid files have to have the same window_size across
  1412. whole archive. Remember the window_size parameter
  1413. for first solid file found. */
  1414. rar->file.solid_window_size = rar->cstate.window_size;
  1415. }
  1416. init_window_mask(rar);
  1417. rar->file.service = 0;
  1418. if(!read_var_sized(a, &host_os, NULL))
  1419. return ARCHIVE_EOF;
  1420. if(host_os == HOST_WINDOWS) {
  1421. /* Host OS is Windows */
  1422. __LA_MODE_T mode;
  1423. if(file_attr & ATTR_DIRECTORY) {
  1424. if (file_attr & ATTR_READONLY) {
  1425. mode = 0555 | AE_IFDIR;
  1426. } else {
  1427. mode = 0755 | AE_IFDIR;
  1428. }
  1429. } else {
  1430. if (file_attr & ATTR_READONLY) {
  1431. mode = 0444 | AE_IFREG;
  1432. } else {
  1433. mode = 0644 | AE_IFREG;
  1434. }
  1435. }
  1436. archive_entry_set_mode(entry, mode);
  1437. if (file_attr & (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM)) {
  1438. char *fflags_text, *ptr;
  1439. /* allocate for "rdonly,hidden,system," */
  1440. fflags_text = malloc(22 * sizeof(char));
  1441. if (fflags_text != NULL) {
  1442. ptr = fflags_text;
  1443. if (file_attr & ATTR_READONLY) {
  1444. strcpy(ptr, "rdonly,");
  1445. ptr = ptr + 7;
  1446. }
  1447. if (file_attr & ATTR_HIDDEN) {
  1448. strcpy(ptr, "hidden,");
  1449. ptr = ptr + 7;
  1450. }
  1451. if (file_attr & ATTR_SYSTEM) {
  1452. strcpy(ptr, "system,");
  1453. ptr = ptr + 7;
  1454. }
  1455. if (ptr > fflags_text) {
  1456. /* Delete trailing comma */
  1457. *(ptr - 1) = '\0';
  1458. archive_entry_copy_fflags_text(entry,
  1459. fflags_text);
  1460. }
  1461. free(fflags_text);
  1462. }
  1463. }
  1464. } else if(host_os == HOST_UNIX) {
  1465. /* Host OS is Unix */
  1466. archive_entry_set_mode(entry, (__LA_MODE_T) file_attr);
  1467. } else {
  1468. /* Unknown host OS */
  1469. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1470. "Unsupported Host OS: 0x%x", (int) host_os);
  1471. return ARCHIVE_FATAL;
  1472. }
  1473. if(!read_var_sized(a, &name_size, NULL))
  1474. return ARCHIVE_EOF;
  1475. if(!read_ahead(a, name_size, &p))
  1476. return ARCHIVE_EOF;
  1477. if(name_size > (MAX_NAME_IN_CHARS - 1)) {
  1478. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1479. "Filename is too long");
  1480. return ARCHIVE_FATAL;
  1481. }
  1482. if(name_size == 0) {
  1483. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1484. "No filename specified");
  1485. return ARCHIVE_FATAL;
  1486. }
  1487. memcpy(name_utf8_buf, p, name_size);
  1488. name_utf8_buf[name_size] = 0;
  1489. if(ARCHIVE_OK != consume(a, name_size)) {
  1490. return ARCHIVE_EOF;
  1491. }
  1492. archive_entry_update_pathname_utf8(entry, name_utf8_buf);
  1493. if(extra_data_size > 0) {
  1494. int ret = process_head_file_extra(a, entry, rar,
  1495. extra_data_size);
  1496. /*
  1497. * TODO: rewrite or remove useless sanity check
  1498. * as extra_data_size is not passed as a pointer
  1499. *
  1500. if(extra_data_size < 0) {
  1501. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1502. "File extra data size is not zero");
  1503. return ARCHIVE_FATAL;
  1504. }
  1505. */
  1506. if(ret != ARCHIVE_OK)
  1507. return ret;
  1508. }
  1509. if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
  1510. rar->file.unpacked_size = (ssize_t) unpacked_size;
  1511. if(rar->file.redir_type == REDIR_TYPE_NONE)
  1512. archive_entry_set_size(entry, unpacked_size);
  1513. }
  1514. if(file_flags & UTIME) {
  1515. archive_entry_set_mtime(entry, (time_t) mtime, 0);
  1516. }
  1517. if(file_flags & CRC32) {
  1518. rar->file.stored_crc32 = crc;
  1519. }
  1520. if(!rar->cstate.switch_multivolume) {
  1521. /* Do not reinitialize unpacking state if we're switching
  1522. * archives. */
  1523. rar->cstate.block_parsing_finished = 1;
  1524. rar->cstate.all_filters_applied = 1;
  1525. rar->cstate.initialized = 0;
  1526. }
  1527. if(rar->generic.split_before > 0) {
  1528. /* If now we're standing on a header that has a 'split before'
  1529. * mark, it means we're standing on a 'continuation' file
  1530. * header. Signal the caller that if it wants to move to
  1531. * another file, it must call rar5_read_header() function
  1532. * again. */
  1533. return ARCHIVE_RETRY;
  1534. } else {
  1535. return ARCHIVE_OK;
  1536. }
  1537. }
  1538. static int process_head_service(struct archive_read* a, struct rar5* rar,
  1539. struct archive_entry* entry, size_t block_flags)
  1540. {
  1541. /* Process this SERVICE block the same way as FILE blocks. */
  1542. int ret = process_head_file(a, rar, entry, block_flags);
  1543. if(ret != ARCHIVE_OK)
  1544. return ret;
  1545. rar->file.service = 1;
  1546. /* But skip the data part automatically. It's no use for the user
  1547. * anyway. It contains only service data, not even needed to
  1548. * properly unpack the file. */
  1549. ret = rar5_read_data_skip(a);
  1550. if(ret != ARCHIVE_OK)
  1551. return ret;
  1552. /* After skipping, try parsing another block automatically. */
  1553. return ARCHIVE_RETRY;
  1554. }
  1555. static int process_head_main(struct archive_read* a, struct rar5* rar,
  1556. struct archive_entry* entry, size_t block_flags)
  1557. {
  1558. int ret;
  1559. size_t extra_data_size = 0;
  1560. size_t extra_field_size = 0;
  1561. size_t extra_field_id = 0;
  1562. size_t archive_flags = 0;
  1563. enum MAIN_FLAGS {
  1564. VOLUME = 0x0001, /* multi-volume archive */
  1565. VOLUME_NUMBER = 0x0002, /* volume number, first vol doesn't
  1566. * have it */
  1567. SOLID = 0x0004, /* solid archive */
  1568. PROTECT = 0x0008, /* contains Recovery info */
  1569. LOCK = 0x0010, /* readonly flag, not used */
  1570. };
  1571. enum MAIN_EXTRA {
  1572. // Just one attribute here.
  1573. LOCATOR = 0x01,
  1574. };
  1575. (void) entry;
  1576. if(block_flags & HFL_EXTRA_DATA) {
  1577. if(!read_var_sized(a, &extra_data_size, NULL))
  1578. return ARCHIVE_EOF;
  1579. } else {
  1580. extra_data_size = 0;
  1581. }
  1582. if(!read_var_sized(a, &archive_flags, NULL)) {
  1583. return ARCHIVE_EOF;
  1584. }
  1585. rar->main.volume = (archive_flags & VOLUME) > 0;
  1586. rar->main.solid = (archive_flags & SOLID) > 0;
  1587. if(archive_flags & VOLUME_NUMBER) {
  1588. size_t v = 0;
  1589. if(!read_var_sized(a, &v, NULL)) {
  1590. return ARCHIVE_EOF;
  1591. }
  1592. if (v > UINT_MAX) {
  1593. archive_set_error(&a->archive,
  1594. ARCHIVE_ERRNO_FILE_FORMAT,
  1595. "Invalid volume number");
  1596. return ARCHIVE_FATAL;
  1597. }
  1598. rar->main.vol_no = (unsigned int) v;
  1599. } else {
  1600. rar->main.vol_no = 0;
  1601. }
  1602. if(rar->vol.expected_vol_no > 0 &&
  1603. rar->main.vol_no != rar->vol.expected_vol_no)
  1604. {
  1605. /* Returning EOF instead of FATAL because of strange
  1606. * libarchive behavior. When opening multiple files via
  1607. * archive_read_open_filenames(), after reading up the whole
  1608. * last file, the __archive_read_ahead function wraps up to
  1609. * the first archive instead of returning EOF. */
  1610. return ARCHIVE_EOF;
  1611. }
  1612. if(extra_data_size == 0) {
  1613. /* Early return. */
  1614. return ARCHIVE_OK;
  1615. }
  1616. if(!read_var_sized(a, &extra_field_size, NULL)) {
  1617. return ARCHIVE_EOF;
  1618. }
  1619. if(!read_var_sized(a, &extra_field_id, NULL)) {
  1620. return ARCHIVE_EOF;
  1621. }
  1622. if(extra_field_size == 0) {
  1623. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1624. "Invalid extra field size");
  1625. return ARCHIVE_FATAL;
  1626. }
  1627. switch(extra_field_id) {
  1628. case LOCATOR:
  1629. ret = process_main_locator_extra_block(a, rar);
  1630. if(ret != ARCHIVE_OK) {
  1631. /* Error while parsing main locator extra
  1632. * block. */
  1633. return ret;
  1634. }
  1635. break;
  1636. default:
  1637. archive_set_error(&a->archive,
  1638. ARCHIVE_ERRNO_FILE_FORMAT,
  1639. "Unsupported extra type (0x%x)",
  1640. (int) extra_field_id);
  1641. return ARCHIVE_FATAL;
  1642. }
  1643. return ARCHIVE_OK;
  1644. }
  1645. static int skip_unprocessed_bytes(struct archive_read* a) {
  1646. struct rar5* rar = get_context(a);
  1647. int ret;
  1648. if(rar->file.bytes_remaining) {
  1649. /* Use different skipping method in block merging mode than in
  1650. * normal mode. If merge mode is active, rar5_read_data_skip
  1651. * can't be used, because it could allow recursive use of
  1652. * merge_block() * function, and this function doesn't support
  1653. * recursive use. */
  1654. if(rar->merge_mode) {
  1655. /* Discard whole merged block. This is valid in solid
  1656. * mode as well, because the code will discard blocks
  1657. * only if those blocks are safe to discard (i.e.
  1658. * they're not FILE blocks). */
  1659. ret = consume(a, rar->file.bytes_remaining);
  1660. if(ret != ARCHIVE_OK) {
  1661. return ret;
  1662. }
  1663. rar->file.bytes_remaining = 0;
  1664. } else {
  1665. /* If we're not in merge mode, use safe skipping code.
  1666. * This will ensure we'll handle solid archives
  1667. * properly. */
  1668. ret = rar5_read_data_skip(a);
  1669. if(ret != ARCHIVE_OK) {
  1670. return ret;
  1671. }
  1672. }
  1673. }
  1674. return ARCHIVE_OK;
  1675. }
  1676. static int scan_for_signature(struct archive_read* a);
  1677. /* Base block processing function. A 'base block' is a RARv5 header block
  1678. * that tells the reader what kind of data is stored inside the block.
  1679. *
  1680. * From the birds-eye view a RAR file looks file this:
  1681. *
  1682. * <magic><base_block_1><base_block_2>...<base_block_n>
  1683. *
  1684. * There are a few types of base blocks. Those types are specified inside
  1685. * the 'switch' statement in this function. For example purposes, I'll write
  1686. * how a standard RARv5 file could look like here:
  1687. *
  1688. * <magic><MAIN><FILE><FILE><FILE><SERVICE><ENDARC>
  1689. *
  1690. * The structure above could describe an archive file with 3 files in it,
  1691. * one service "QuickOpen" block (that is ignored by this parser), and an
  1692. * end of file base block marker.
  1693. *
  1694. * If the file is stored in multiple archive files ("multiarchive"), it might
  1695. * look like this:
  1696. *
  1697. * .part01.rar: <magic><MAIN><FILE><ENDARC>
  1698. * .part02.rar: <magic><MAIN><FILE><ENDARC>
  1699. * .part03.rar: <magic><MAIN><FILE><ENDARC>
  1700. *
  1701. * This example could describe 3 RAR files that contain ONE archived file.
  1702. * Or it could describe 3 RAR files that contain 3 different files. Or 3
  1703. * RAR files than contain 2 files. It all depends what metadata is stored in
  1704. * the headers of <FILE> blocks.
  1705. *
  1706. * Each <FILE> block contains info about its size, the name of the file it's
  1707. * storing inside, and whether this FILE block is a continuation block of
  1708. * previous archive ('split before'), and is this FILE block should be
  1709. * continued in another archive ('split after'). By parsing the 'split before'
  1710. * and 'split after' flags, we're able to tell if multiple <FILE> base blocks
  1711. * are describing one file, or multiple files (with the same filename, for
  1712. * example).
  1713. *
  1714. * One thing to note is that if we're parsing the first <FILE> block, and
  1715. * we see 'split after' flag, then we need to jump over to another <FILE>
  1716. * block to be able to decompress rest of the data. To do this, we need
  1717. * to skip the <ENDARC> block, then switch to another file, then skip the
  1718. * <magic> block, <MAIN> block, and then we're standing on the proper
  1719. * <FILE> block.
  1720. */
  1721. static int process_base_block(struct archive_read* a,
  1722. struct archive_entry* entry)
  1723. {
  1724. const size_t SMALLEST_RAR5_BLOCK_SIZE = 3;
  1725. struct rar5* rar = get_context(a);
  1726. uint32_t hdr_crc, computed_crc;
  1727. size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
  1728. size_t header_id = 0;
  1729. size_t header_flags = 0;
  1730. const uint8_t* p;
  1731. int ret;
  1732. enum HEADER_TYPE {
  1733. HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
  1734. HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
  1735. HEAD_UNKNOWN = 0xff,
  1736. };
  1737. /* Skip any unprocessed data for this file. */
  1738. ret = skip_unprocessed_bytes(a);
  1739. if(ret != ARCHIVE_OK)
  1740. return ret;
  1741. /* Read the expected CRC32 checksum. */
  1742. if(!read_u32(a, &hdr_crc)) {
  1743. return ARCHIVE_EOF;
  1744. }
  1745. /* Read header size. */
  1746. if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
  1747. return ARCHIVE_EOF;
  1748. }
  1749. hdr_size = raw_hdr_size + hdr_size_len;
  1750. /* Sanity check, maximum header size for RAR5 is 2MB. */
  1751. if(hdr_size > (2 * 1024 * 1024)) {
  1752. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1753. "Base block header is too large");
  1754. return ARCHIVE_FATAL;
  1755. }
  1756. /* Additional sanity checks to weed out invalid files. */
  1757. if(raw_hdr_size == 0 || hdr_size_len == 0 ||
  1758. hdr_size < SMALLEST_RAR5_BLOCK_SIZE)
  1759. {
  1760. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1761. "Too small block encountered (%zu bytes)",
  1762. raw_hdr_size);
  1763. return ARCHIVE_FATAL;
  1764. }
  1765. /* Read the whole header data into memory, maximum memory use here is
  1766. * 2MB. */
  1767. if(!read_ahead(a, hdr_size, &p)) {
  1768. return ARCHIVE_EOF;
  1769. }
  1770. /* Verify the CRC32 of the header data. */
  1771. computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
  1772. if(computed_crc != hdr_crc) {
  1773. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1774. "Header CRC error");
  1775. return ARCHIVE_FATAL;
  1776. }
  1777. /* If the checksum is OK, we proceed with parsing. */
  1778. if(ARCHIVE_OK != consume(a, hdr_size_len)) {
  1779. return ARCHIVE_EOF;
  1780. }
  1781. if(!read_var_sized(a, &header_id, NULL))
  1782. return ARCHIVE_EOF;
  1783. if(!read_var_sized(a, &header_flags, NULL))
  1784. return ARCHIVE_EOF;
  1785. rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
  1786. rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
  1787. rar->generic.size = (int)hdr_size;
  1788. rar->generic.last_header_id = (int)header_id;
  1789. rar->main.endarc = 0;
  1790. /* Those are possible header ids in RARv5. */
  1791. switch(header_id) {
  1792. case HEAD_MAIN:
  1793. ret = process_head_main(a, rar, entry, header_flags);
  1794. /* Main header doesn't have any files in it, so it's
  1795. * pointless to return to the caller. Retry to next
  1796. * header, which should be HEAD_FILE/HEAD_SERVICE. */
  1797. if(ret == ARCHIVE_OK)
  1798. return ARCHIVE_RETRY;
  1799. return ret;
  1800. case HEAD_SERVICE:
  1801. ret = process_head_service(a, rar, entry, header_flags);
  1802. return ret;
  1803. case HEAD_FILE:
  1804. ret = process_head_file(a, rar, entry, header_flags);
  1805. return ret;
  1806. case HEAD_CRYPT:
  1807. archive_set_error(&a->archive,
  1808. ARCHIVE_ERRNO_FILE_FORMAT,
  1809. "Encryption is not supported");
  1810. return ARCHIVE_FATAL;
  1811. case HEAD_ENDARC:
  1812. rar->main.endarc = 1;
  1813. /* After encountering an end of file marker, we need
  1814. * to take into consideration if this archive is
  1815. * continued in another file (i.e. is it part01.rar:
  1816. * is there a part02.rar?) */
  1817. if(rar->main.volume) {
  1818. /* In case there is part02.rar, position the
  1819. * read pointer in a proper place, so we can
  1820. * resume parsing. */
  1821. ret = scan_for_signature(a);
  1822. if(ret == ARCHIVE_FATAL) {
  1823. return ARCHIVE_EOF;
  1824. } else {
  1825. if(rar->vol.expected_vol_no ==
  1826. UINT_MAX) {
  1827. archive_set_error(&a->archive,
  1828. ARCHIVE_ERRNO_FILE_FORMAT,
  1829. "Header error");
  1830. return ARCHIVE_FATAL;
  1831. }
  1832. rar->vol.expected_vol_no =
  1833. rar->main.vol_no + 1;
  1834. return ARCHIVE_OK;
  1835. }
  1836. } else {
  1837. return ARCHIVE_EOF;
  1838. }
  1839. case HEAD_MARK:
  1840. return ARCHIVE_EOF;
  1841. default:
  1842. if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
  1843. archive_set_error(&a->archive,
  1844. ARCHIVE_ERRNO_FILE_FORMAT,
  1845. "Header type error");
  1846. return ARCHIVE_FATAL;
  1847. } else {
  1848. /* If the block is marked as 'skip if unknown',
  1849. * do as the flag says: skip the block
  1850. * instead on failing on it. */
  1851. return ARCHIVE_RETRY;
  1852. }
  1853. }
  1854. #if !defined WIN32
  1855. // Not reached.
  1856. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1857. "Internal unpacker error");
  1858. return ARCHIVE_FATAL;
  1859. #endif
  1860. }
  1861. static int skip_base_block(struct archive_read* a) {
  1862. int ret;
  1863. struct rar5* rar = get_context(a);
  1864. /* Create a new local archive_entry structure that will be operated on
  1865. * by header reader; operations on this archive_entry will be discarded.
  1866. */
  1867. struct archive_entry* entry = archive_entry_new();
  1868. ret = process_base_block(a, entry);
  1869. /* Discard operations on this archive_entry structure. */
  1870. archive_entry_free(entry);
  1871. if(ret == ARCHIVE_FATAL)
  1872. return ret;
  1873. if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
  1874. return ARCHIVE_OK;
  1875. if(ret == ARCHIVE_OK)
  1876. return ARCHIVE_RETRY;
  1877. else
  1878. return ret;
  1879. }
  1880. static int rar5_read_header(struct archive_read *a,
  1881. struct archive_entry *entry)
  1882. {
  1883. struct rar5* rar = get_context(a);
  1884. int ret;
  1885. if(rar->header_initialized == 0) {
  1886. init_header(a);
  1887. rar->header_initialized = 1;
  1888. }
  1889. if(rar->skipped_magic == 0) {
  1890. if(ARCHIVE_OK != consume(a, sizeof(rar5_signature_xor))) {
  1891. return ARCHIVE_EOF;
  1892. }
  1893. rar->skipped_magic = 1;
  1894. }
  1895. do {
  1896. ret = process_base_block(a, entry);
  1897. } while(ret == ARCHIVE_RETRY ||
  1898. (rar->main.endarc > 0 && ret == ARCHIVE_OK));
  1899. return ret;
  1900. }
  1901. static void init_unpack(struct rar5* rar) {
  1902. rar->file.calculated_crc32 = 0;
  1903. init_window_mask(rar);
  1904. free(rar->cstate.window_buf);
  1905. free(rar->cstate.filtered_buf);
  1906. if(rar->cstate.window_size > 0) {
  1907. rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
  1908. rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
  1909. } else {
  1910. rar->cstate.window_buf = NULL;
  1911. rar->cstate.filtered_buf = NULL;
  1912. }
  1913. rar->cstate.write_ptr = 0;
  1914. rar->cstate.last_write_ptr = 0;
  1915. memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
  1916. memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
  1917. memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
  1918. memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
  1919. memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
  1920. }
  1921. static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
  1922. int verify_crc;
  1923. if(rar->skip_mode) {
  1924. #if defined CHECK_CRC_ON_SOLID_SKIP
  1925. verify_crc = 1;
  1926. #else
  1927. verify_crc = 0;
  1928. #endif
  1929. } else
  1930. verify_crc = 1;
  1931. if(verify_crc) {
  1932. /* Don't update CRC32 if the file doesn't have the
  1933. * `stored_crc32` info filled in. */
  1934. if(rar->file.stored_crc32 > 0) {
  1935. rar->file.calculated_crc32 =
  1936. crc32(rar->file.calculated_crc32, p, to_read);
  1937. }
  1938. /* Check if the file uses an optional BLAKE2sp checksum
  1939. * algorithm. */
  1940. if(rar->file.has_blake2 > 0) {
  1941. /* Return value of the `update` function is always 0,
  1942. * so we can explicitly ignore it here. */
  1943. (void) blake2sp_update(&rar->file.b2state, p, to_read);
  1944. }
  1945. }
  1946. }
  1947. static int create_decode_tables(uint8_t* bit_length,
  1948. struct decode_table* table, int size)
  1949. {
  1950. int code, upper_limit = 0, i, lc[16];
  1951. uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
  1952. ssize_t cur_len, quick_data_size;
  1953. memset(&lc, 0, sizeof(lc));
  1954. memset(table->decode_num, 0, sizeof(table->decode_num));
  1955. table->size = size;
  1956. table->quick_bits = size == HUFF_NC ? 10 : 7;
  1957. for(i = 0; i < size; i++) {
  1958. lc[bit_length[i] & 15]++;
  1959. }
  1960. lc[0] = 0;
  1961. table->decode_pos[0] = 0;
  1962. table->decode_len[0] = 0;
  1963. for(i = 1; i < 16; i++) {
  1964. upper_limit += lc[i];
  1965. table->decode_len[i] = upper_limit << (16 - i);
  1966. table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
  1967. upper_limit <<= 1;
  1968. }
  1969. memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
  1970. for(i = 0; i < size; i++) {
  1971. uint8_t clen = bit_length[i] & 15;
  1972. if(clen > 0) {
  1973. int last_pos = decode_pos_clone[clen];
  1974. table->decode_num[last_pos] = i;
  1975. decode_pos_clone[clen]++;
  1976. }
  1977. }
  1978. quick_data_size = (int64_t)1 << table->quick_bits;
  1979. cur_len = 1;
  1980. for(code = 0; code < quick_data_size; code++) {
  1981. int bit_field = code << (16 - table->quick_bits);
  1982. int dist, pos;
  1983. while(cur_len < rar5_countof(table->decode_len) &&
  1984. bit_field >= table->decode_len[cur_len]) {
  1985. cur_len++;
  1986. }
  1987. table->quick_len[code] = (uint8_t) cur_len;
  1988. dist = bit_field - table->decode_len[cur_len - 1];
  1989. dist >>= (16 - cur_len);
  1990. pos = table->decode_pos[cur_len & 15] + dist;
  1991. if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
  1992. table->quick_num[code] = table->decode_num[pos];
  1993. } else {
  1994. table->quick_num[code] = 0;
  1995. }
  1996. }
  1997. return ARCHIVE_OK;
  1998. }
  1999. static int decode_number(struct archive_read* a, struct decode_table* table,
  2000. const uint8_t* p, uint16_t* num)
  2001. {
  2002. int i, bits, dist;
  2003. uint16_t bitfield;
  2004. uint32_t pos;
  2005. struct rar5* rar = get_context(a);
  2006. if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
  2007. return ARCHIVE_EOF;
  2008. }
  2009. bitfield &= 0xfffe;
  2010. if(bitfield < table->decode_len[table->quick_bits]) {
  2011. int code = bitfield >> (16 - table->quick_bits);
  2012. skip_bits(rar, table->quick_len[code]);
  2013. *num = table->quick_num[code];
  2014. return ARCHIVE_OK;
  2015. }
  2016. bits = 15;
  2017. for(i = table->quick_bits + 1; i < 15; i++) {
  2018. if(bitfield < table->decode_len[i]) {
  2019. bits = i;
  2020. break;
  2021. }
  2022. }
  2023. skip_bits(rar, bits);
  2024. dist = bitfield - table->decode_len[bits - 1];
  2025. dist >>= (16 - bits);
  2026. pos = table->decode_pos[bits] + dist;
  2027. if(pos >= table->size)
  2028. pos = 0;
  2029. *num = table->decode_num[pos];
  2030. return ARCHIVE_OK;
  2031. }
  2032. /* Reads and parses Huffman tables from the beginning of the block. */
  2033. static int parse_tables(struct archive_read* a, struct rar5* rar,
  2034. const uint8_t* p)
  2035. {
  2036. int ret, value, i, w, idx = 0;
  2037. uint8_t bit_length[HUFF_BC],
  2038. table[HUFF_TABLE_SIZE],
  2039. nibble_mask = 0xF0,
  2040. nibble_shift = 4;
  2041. enum { ESCAPE = 15 };
  2042. /* The data for table generation is compressed using a simple RLE-like
  2043. * algorithm when storing zeroes, so we need to unpack it first. */
  2044. for(w = 0, i = 0; w < HUFF_BC;) {
  2045. if(i >= rar->cstate.cur_block_size) {
  2046. /* Truncated data, can't continue. */
  2047. archive_set_error(&a->archive,
  2048. ARCHIVE_ERRNO_FILE_FORMAT,
  2049. "Truncated data in huffman tables");
  2050. return ARCHIVE_FATAL;
  2051. }
  2052. value = (p[i] & nibble_mask) >> nibble_shift;
  2053. if(nibble_mask == 0x0F)
  2054. ++i;
  2055. nibble_mask ^= 0xFF;
  2056. nibble_shift ^= 4;
  2057. /* Values smaller than 15 is data, so we write it directly.
  2058. * Value 15 is a flag telling us that we need to unpack more
  2059. * bytes. */
  2060. if(value == ESCAPE) {
  2061. value = (p[i] & nibble_mask) >> nibble_shift;
  2062. if(nibble_mask == 0x0F)
  2063. ++i;
  2064. nibble_mask ^= 0xFF;
  2065. nibble_shift ^= 4;
  2066. if(value == 0) {
  2067. /* We sometimes need to write the actual value
  2068. * of 15, so this case handles that. */
  2069. bit_length[w++] = ESCAPE;
  2070. } else {
  2071. int k;
  2072. /* Fill zeroes. */
  2073. for(k = 0; (k < value + 2) && (w < HUFF_BC);
  2074. k++) {
  2075. bit_length[w++] = 0;
  2076. }
  2077. }
  2078. } else {
  2079. bit_length[w++] = value;
  2080. }
  2081. }
  2082. rar->bits.in_addr = i;
  2083. rar->bits.bit_addr = nibble_shift ^ 4;
  2084. ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
  2085. if(ret != ARCHIVE_OK) {
  2086. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2087. "Decoding huffman tables failed");
  2088. return ARCHIVE_FATAL;
  2089. }
  2090. for(i = 0; i < HUFF_TABLE_SIZE;) {
  2091. uint16_t num;
  2092. if((rar->bits.in_addr + 6) >= rar->cstate.cur_block_size) {
  2093. /* Truncated data, can't continue. */
  2094. archive_set_error(&a->archive,
  2095. ARCHIVE_ERRNO_FILE_FORMAT,
  2096. "Truncated data in huffman tables (#2)");
  2097. return ARCHIVE_FATAL;
  2098. }
  2099. ret = decode_number(a, &rar->cstate.bd, p, &num);
  2100. if(ret != ARCHIVE_OK) {
  2101. archive_set_error(&a->archive,
  2102. ARCHIVE_ERRNO_FILE_FORMAT,
  2103. "Decoding huffman tables failed");
  2104. return ARCHIVE_FATAL;
  2105. }
  2106. if(num < 16) {
  2107. /* 0..15: store directly */
  2108. table[i] = (uint8_t) num;
  2109. i++;
  2110. } else if(num < 18) {
  2111. /* 16..17: repeat previous code */
  2112. uint16_t n;
  2113. if(ARCHIVE_OK != read_bits_16(rar, p, &n))
  2114. return ARCHIVE_EOF;
  2115. if(num == 16) {
  2116. n >>= 13;
  2117. n += 3;
  2118. skip_bits(rar, 3);
  2119. } else {
  2120. n >>= 9;
  2121. n += 11;
  2122. skip_bits(rar, 7);
  2123. }
  2124. if(i > 0) {
  2125. while(n-- > 0 && i < HUFF_TABLE_SIZE) {
  2126. table[i] = table[i - 1];
  2127. i++;
  2128. }
  2129. } else {
  2130. archive_set_error(&a->archive,
  2131. ARCHIVE_ERRNO_FILE_FORMAT,
  2132. "Unexpected error when decoding "
  2133. "huffman tables");
  2134. return ARCHIVE_FATAL;
  2135. }
  2136. } else {
  2137. /* other codes: fill with zeroes `n` times */
  2138. uint16_t n;
  2139. if(ARCHIVE_OK != read_bits_16(rar, p, &n))
  2140. return ARCHIVE_EOF;
  2141. if(num == 18) {
  2142. n >>= 13;
  2143. n += 3;
  2144. skip_bits(rar, 3);
  2145. } else {
  2146. n >>= 9;
  2147. n += 11;
  2148. skip_bits(rar, 7);
  2149. }
  2150. while(n-- > 0 && i < HUFF_TABLE_SIZE)
  2151. table[i++] = 0;
  2152. }
  2153. }
  2154. ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
  2155. if(ret != ARCHIVE_OK) {
  2156. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2157. "Failed to create literal table");
  2158. return ARCHIVE_FATAL;
  2159. }
  2160. idx += HUFF_NC;
  2161. ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
  2162. if(ret != ARCHIVE_OK) {
  2163. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2164. "Failed to create distance table");
  2165. return ARCHIVE_FATAL;
  2166. }
  2167. idx += HUFF_DC;
  2168. ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
  2169. if(ret != ARCHIVE_OK) {
  2170. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2171. "Failed to create lower bits of distances table");
  2172. return ARCHIVE_FATAL;
  2173. }
  2174. idx += HUFF_LDC;
  2175. ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
  2176. if(ret != ARCHIVE_OK) {
  2177. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2178. "Failed to create repeating distances table");
  2179. return ARCHIVE_FATAL;
  2180. }
  2181. return ARCHIVE_OK;
  2182. }
  2183. /* Parses the block header, verifies its CRC byte, and saves the header
  2184. * fields inside the `hdr` pointer. */
  2185. static int parse_block_header(struct archive_read* a, const uint8_t* p,
  2186. ssize_t* block_size, struct compressed_block_header* hdr)
  2187. {
  2188. uint8_t calculated_cksum;
  2189. memcpy(hdr, p, sizeof(struct compressed_block_header));
  2190. if(bf_byte_count(hdr) > 2) {
  2191. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2192. "Unsupported block header size (was %d, max is 2)",
  2193. bf_byte_count(hdr));
  2194. return ARCHIVE_FATAL;
  2195. }
  2196. /* This should probably use bit reader interface in order to be more
  2197. * future-proof. */
  2198. *block_size = 0;
  2199. switch(bf_byte_count(hdr)) {
  2200. /* 1-byte block size */
  2201. case 0:
  2202. *block_size = *(const uint8_t*) &p[2];
  2203. break;
  2204. /* 2-byte block size */
  2205. case 1:
  2206. *block_size = archive_le16dec(&p[2]);
  2207. break;
  2208. /* 3-byte block size */
  2209. case 2:
  2210. *block_size = archive_le32dec(&p[2]);
  2211. *block_size &= 0x00FFFFFF;
  2212. break;
  2213. /* Other block sizes are not supported. This case is not
  2214. * reached, because we have an 'if' guard before the switch
  2215. * that makes sure of it. */
  2216. default:
  2217. return ARCHIVE_FATAL;
  2218. }
  2219. /* Verify the block header checksum. 0x5A is a magic value and is
  2220. * always * constant. */
  2221. calculated_cksum = 0x5A
  2222. ^ (uint8_t) hdr->block_flags_u8
  2223. ^ (uint8_t) *block_size
  2224. ^ (uint8_t) (*block_size >> 8)
  2225. ^ (uint8_t) (*block_size >> 16);
  2226. if(calculated_cksum != hdr->block_cksum) {
  2227. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2228. "Block checksum error: got 0x%x, expected 0x%x",
  2229. hdr->block_cksum, calculated_cksum);
  2230. return ARCHIVE_FATAL;
  2231. }
  2232. return ARCHIVE_OK;
  2233. }
  2234. /* Convenience function used during filter processing. */
  2235. static int parse_filter_data(struct rar5* rar, const uint8_t* p,
  2236. uint32_t* filter_data)
  2237. {
  2238. int i, bytes;
  2239. uint32_t data = 0;
  2240. if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
  2241. return ARCHIVE_EOF;
  2242. bytes++;
  2243. for(i = 0; i < bytes; i++) {
  2244. uint16_t byte;
  2245. if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
  2246. return ARCHIVE_EOF;
  2247. }
  2248. /* Cast to uint32_t will ensure the shift operation will not
  2249. * produce undefined result. */
  2250. data += ((uint32_t) byte >> 8) << (i * 8);
  2251. skip_bits(rar, 8);
  2252. }
  2253. *filter_data = data;
  2254. return ARCHIVE_OK;
  2255. }
  2256. /* Function is used during sanity checking. */
  2257. static int is_valid_filter_block_start(struct rar5* rar,
  2258. uint32_t start)
  2259. {
  2260. const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
  2261. const int64_t last_bs = rar->cstate.last_block_start;
  2262. const ssize_t last_bl = rar->cstate.last_block_length;
  2263. if(last_bs == 0 || last_bl == 0) {
  2264. /* We didn't have any filters yet, so accept this offset. */
  2265. return 1;
  2266. }
  2267. if(block_start >= last_bs + last_bl) {
  2268. /* Current offset is bigger than last block's end offset, so
  2269. * accept current offset. */
  2270. return 1;
  2271. }
  2272. /* Any other case is not a normal situation and we should fail. */
  2273. return 0;
  2274. }
  2275. /* The function will create a new filter, read its parameters from the input
  2276. * stream and add it to the filter collection. */
  2277. static int parse_filter(struct archive_read* ar, const uint8_t* p) {
  2278. uint32_t block_start, block_length;
  2279. uint16_t filter_type;
  2280. struct filter_info* filt = NULL;
  2281. struct rar5* rar = get_context(ar);
  2282. /* Read the parameters from the input stream. */
  2283. if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
  2284. return ARCHIVE_EOF;
  2285. if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
  2286. return ARCHIVE_EOF;
  2287. if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
  2288. return ARCHIVE_EOF;
  2289. filter_type >>= 13;
  2290. skip_bits(rar, 3);
  2291. /* Perform some sanity checks on this filter parameters. Note that we
  2292. * allow only DELTA, E8/E9 and ARM filters here, because rest of
  2293. * filters are not used in RARv5. */
  2294. if(block_length < 4 ||
  2295. block_length > 0x400000 ||
  2296. filter_type > FILTER_ARM ||
  2297. !is_valid_filter_block_start(rar, block_start))
  2298. {
  2299. archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2300. "Invalid filter encountered");
  2301. return ARCHIVE_FATAL;
  2302. }
  2303. /* Allocate a new filter. */
  2304. filt = add_new_filter(rar);
  2305. if(filt == NULL) {
  2306. archive_set_error(&ar->archive, ENOMEM,
  2307. "Can't allocate memory for a filter descriptor.");
  2308. return ARCHIVE_FATAL;
  2309. }
  2310. filt->type = filter_type;
  2311. filt->block_start = rar->cstate.write_ptr + block_start;
  2312. filt->block_length = block_length;
  2313. rar->cstate.last_block_start = filt->block_start;
  2314. rar->cstate.last_block_length = filt->block_length;
  2315. /* Read some more data in case this is a DELTA filter. Other filter
  2316. * types don't require any additional data over what was already
  2317. * read. */
  2318. if(filter_type == FILTER_DELTA) {
  2319. int channels;
  2320. if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
  2321. return ARCHIVE_EOF;
  2322. filt->channels = channels + 1;
  2323. }
  2324. return ARCHIVE_OK;
  2325. }
  2326. static int decode_code_length(struct rar5* rar, const uint8_t* p,
  2327. uint16_t code)
  2328. {
  2329. int lbits, length = 2;
  2330. if(code < 8) {
  2331. lbits = 0;
  2332. length += code;
  2333. } else {
  2334. lbits = code / 4 - 1;
  2335. length += (4 | (code & 3)) << lbits;
  2336. }
  2337. if(lbits > 0) {
  2338. int add;
  2339. if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
  2340. return -1;
  2341. length += add;
  2342. }
  2343. return length;
  2344. }
  2345. static int copy_string(struct archive_read* a, int len, int dist) {
  2346. struct rar5* rar = get_context(a);
  2347. const uint64_t cmask = rar->cstate.window_mask;
  2348. const uint64_t write_ptr = rar->cstate.write_ptr +
  2349. rar->cstate.solid_offset;
  2350. int i;
  2351. if (rar->cstate.window_buf == NULL)
  2352. return ARCHIVE_FATAL;
  2353. /* The unpacker spends most of the time in this function. It would be
  2354. * a good idea to introduce some optimizations here.
  2355. *
  2356. * Just remember that this loop treats buffers that overlap differently
  2357. * than buffers that do not overlap. This is why a simple memcpy(3)
  2358. * call will not be enough. */
  2359. for(i = 0; i < len; i++) {
  2360. const ssize_t write_idx = (write_ptr + i) & cmask;
  2361. const ssize_t read_idx = (write_ptr + i - dist) & cmask;
  2362. rar->cstate.window_buf[write_idx] =
  2363. rar->cstate.window_buf[read_idx];
  2364. }
  2365. rar->cstate.write_ptr += len;
  2366. return ARCHIVE_OK;
  2367. }
  2368. static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
  2369. struct rar5* rar = get_context(a);
  2370. uint16_t num;
  2371. int ret;
  2372. const uint64_t cmask = rar->cstate.window_mask;
  2373. const struct compressed_block_header* hdr = &rar->last_block_hdr;
  2374. const uint8_t bit_size = 1 + bf_bit_size(hdr);
  2375. while(1) {
  2376. if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
  2377. (rar->cstate.window_size >> 1)) {
  2378. /* Don't allow growing data by more than half of the
  2379. * window size at a time. In such case, break the loop;
  2380. * next call to this function will continue processing
  2381. * from this moment. */
  2382. break;
  2383. }
  2384. if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
  2385. (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
  2386. rar->bits.bit_addr >= bit_size))
  2387. {
  2388. /* If the program counter is here, it means the
  2389. * function has finished processing the block. */
  2390. rar->cstate.block_parsing_finished = 1;
  2391. break;
  2392. }
  2393. /* Decode the next literal. */
  2394. if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
  2395. return ARCHIVE_EOF;
  2396. }
  2397. /* Num holds a decompression literal, or 'command code'.
  2398. *
  2399. * - Values lower than 256 are just bytes. Those codes
  2400. * can be stored in the output buffer directly.
  2401. *
  2402. * - Code 256 defines a new filter, which is later used to
  2403. * ransform the data block accordingly to the filter type.
  2404. * The data block needs to be fully uncompressed first.
  2405. *
  2406. * - Code bigger than 257 and smaller than 262 define
  2407. * a repetition pattern that should be copied from
  2408. * an already uncompressed chunk of data.
  2409. */
  2410. if(num < 256) {
  2411. /* Directly store the byte. */
  2412. int64_t write_idx = rar->cstate.solid_offset +
  2413. rar->cstate.write_ptr++;
  2414. rar->cstate.window_buf[write_idx & cmask] =
  2415. (uint8_t) num;
  2416. continue;
  2417. } else if(num >= 262) {
  2418. uint16_t dist_slot;
  2419. int len = decode_code_length(rar, p, num - 262),
  2420. dbits,
  2421. dist = 1;
  2422. if(len == -1) {
  2423. archive_set_error(&a->archive,
  2424. ARCHIVE_ERRNO_PROGRAMMER,
  2425. "Failed to decode the code length");
  2426. return ARCHIVE_FATAL;
  2427. }
  2428. if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p,
  2429. &dist_slot))
  2430. {
  2431. archive_set_error(&a->archive,
  2432. ARCHIVE_ERRNO_PROGRAMMER,
  2433. "Failed to decode the distance slot");
  2434. return ARCHIVE_FATAL;
  2435. }
  2436. if(dist_slot < 4) {
  2437. dbits = 0;
  2438. dist += dist_slot;
  2439. } else {
  2440. dbits = dist_slot / 2 - 1;
  2441. /* Cast to uint32_t will make sure the shift
  2442. * left operation won't produce undefined
  2443. * result. Then, the uint32_t type will
  2444. * be implicitly casted to int. */
  2445. dist += (uint32_t) (2 |
  2446. (dist_slot & 1)) << dbits;
  2447. }
  2448. if(dbits > 0) {
  2449. if(dbits >= 4) {
  2450. uint32_t add = 0;
  2451. uint16_t low_dist;
  2452. if(dbits > 4) {
  2453. if(ARCHIVE_OK != read_bits_32(
  2454. rar, p, &add)) {
  2455. /* Return EOF if we
  2456. * can't read more
  2457. * data. */
  2458. return ARCHIVE_EOF;
  2459. }
  2460. skip_bits(rar, dbits - 4);
  2461. add = (add >> (
  2462. 36 - dbits)) << 4;
  2463. dist += add;
  2464. }
  2465. if(ARCHIVE_OK != decode_number(a,
  2466. &rar->cstate.ldd, p, &low_dist))
  2467. {
  2468. archive_set_error(&a->archive,
  2469. ARCHIVE_ERRNO_PROGRAMMER,
  2470. "Failed to decode the "
  2471. "distance slot");
  2472. return ARCHIVE_FATAL;
  2473. }
  2474. if(dist >= INT_MAX - low_dist - 1) {
  2475. /* This only happens in
  2476. * invalid archives. */
  2477. archive_set_error(&a->archive,
  2478. ARCHIVE_ERRNO_FILE_FORMAT,
  2479. "Distance pointer "
  2480. "overflow");
  2481. return ARCHIVE_FATAL;
  2482. }
  2483. dist += low_dist;
  2484. } else {
  2485. /* dbits is one of [0,1,2,3] */
  2486. int add;
  2487. if(ARCHIVE_OK != read_consume_bits(rar,
  2488. p, dbits, &add)) {
  2489. /* Return EOF if we can't read
  2490. * more data. */
  2491. return ARCHIVE_EOF;
  2492. }
  2493. dist += add;
  2494. }
  2495. }
  2496. if(dist > 0x100) {
  2497. len++;
  2498. if(dist > 0x2000) {
  2499. len++;
  2500. if(dist > 0x40000) {
  2501. len++;
  2502. }
  2503. }
  2504. }
  2505. dist_cache_push(rar, dist);
  2506. rar->cstate.last_len = len;
  2507. if(ARCHIVE_OK != copy_string(a, len, dist))
  2508. return ARCHIVE_FATAL;
  2509. continue;
  2510. } else if(num == 256) {
  2511. /* Create a filter. */
  2512. ret = parse_filter(a, p);
  2513. if(ret != ARCHIVE_OK)
  2514. return ret;
  2515. continue;
  2516. } else if(num == 257) {
  2517. if(rar->cstate.last_len != 0) {
  2518. if(ARCHIVE_OK != copy_string(a,
  2519. rar->cstate.last_len,
  2520. rar->cstate.dist_cache[0]))
  2521. {
  2522. return ARCHIVE_FATAL;
  2523. }
  2524. }
  2525. continue;
  2526. } else {
  2527. /* num < 262 */
  2528. const int idx = num - 258;
  2529. const int dist = dist_cache_touch(rar, idx);
  2530. uint16_t len_slot;
  2531. int len;
  2532. if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p,
  2533. &len_slot)) {
  2534. return ARCHIVE_FATAL;
  2535. }
  2536. len = decode_code_length(rar, p, len_slot);
  2537. rar->cstate.last_len = len;
  2538. if(ARCHIVE_OK != copy_string(a, len, dist))
  2539. return ARCHIVE_FATAL;
  2540. continue;
  2541. }
  2542. /* The program counter shouldn't reach here. */
  2543. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2544. "Unsupported block code: 0x%x", num);
  2545. return ARCHIVE_FATAL;
  2546. }
  2547. return ARCHIVE_OK;
  2548. }
  2549. /* Binary search for the RARv5 signature. */
  2550. static int scan_for_signature(struct archive_read* a) {
  2551. const uint8_t* p;
  2552. const int chunk_size = 512;
  2553. ssize_t i;
  2554. char signature[sizeof(rar5_signature_xor)];
  2555. /* If we're here, it means we're on an 'unknown territory' data.
  2556. * There's no indication what kind of data we're reading here.
  2557. * It could be some text comment, any kind of binary data,
  2558. * digital sign, dragons, etc.
  2559. *
  2560. * We want to find a valid RARv5 magic header inside this unknown
  2561. * data. */
  2562. /* Is it possible in libarchive to just skip everything until the
  2563. * end of the file? If so, it would be a better approach than the
  2564. * current implementation of this function. */
  2565. rar5_signature(signature);
  2566. while(1) {
  2567. if(!read_ahead(a, chunk_size, &p))
  2568. return ARCHIVE_EOF;
  2569. for(i = 0; i < chunk_size - (int)sizeof(rar5_signature_xor);
  2570. i++) {
  2571. if(memcmp(&p[i], signature,
  2572. sizeof(rar5_signature_xor)) == 0) {
  2573. /* Consume the number of bytes we've used to
  2574. * search for the signature, as well as the
  2575. * number of bytes used by the signature
  2576. * itself. After this we should be standing
  2577. * on a valid base block header. */
  2578. (void) consume(a,
  2579. i + sizeof(rar5_signature_xor));
  2580. return ARCHIVE_OK;
  2581. }
  2582. }
  2583. consume(a, chunk_size);
  2584. }
  2585. return ARCHIVE_FATAL;
  2586. }
  2587. /* This function will switch the multivolume archive file to another file,
  2588. * i.e. from part03 to part 04. */
  2589. static int advance_multivolume(struct archive_read* a) {
  2590. int lret;
  2591. struct rar5* rar = get_context(a);
  2592. /* A small state machine that will skip unnecessary data, needed to
  2593. * switch from one multivolume to another. Such skipping is needed if
  2594. * we want to be an stream-oriented (instead of file-oriented)
  2595. * unpacker.
  2596. *
  2597. * The state machine starts with `rar->main.endarc` == 0. It also
  2598. * assumes that current stream pointer points to some base block
  2599. * header.
  2600. *
  2601. * The `endarc` field is being set when the base block parsing
  2602. * function encounters the 'end of archive' marker.
  2603. */
  2604. while(1) {
  2605. if(rar->main.endarc == 1) {
  2606. int looping = 1;
  2607. rar->main.endarc = 0;
  2608. while(looping) {
  2609. lret = skip_base_block(a);
  2610. switch(lret) {
  2611. case ARCHIVE_RETRY:
  2612. /* Continue looping. */
  2613. break;
  2614. case ARCHIVE_OK:
  2615. /* Break loop. */
  2616. looping = 0;
  2617. break;
  2618. default:
  2619. /* Forward any errors to the
  2620. * caller. */
  2621. return lret;
  2622. }
  2623. }
  2624. break;
  2625. } else {
  2626. /* Skip current base block. In order to properly skip
  2627. * it, we really need to simply parse it and discard
  2628. * the results. */
  2629. lret = skip_base_block(a);
  2630. if(lret == ARCHIVE_FATAL || lret == ARCHIVE_FAILED)
  2631. return lret;
  2632. /* The `skip_base_block` function tells us if we
  2633. * should continue with skipping, or we should stop
  2634. * skipping. We're trying to skip everything up to
  2635. * a base FILE block. */
  2636. if(lret != ARCHIVE_RETRY) {
  2637. /* If there was an error during skipping, or we
  2638. * have just skipped a FILE base block... */
  2639. if(rar->main.endarc == 0) {
  2640. return lret;
  2641. } else {
  2642. continue;
  2643. }
  2644. }
  2645. }
  2646. }
  2647. return ARCHIVE_OK;
  2648. }
  2649. /* Merges the partial block from the first multivolume archive file, and
  2650. * partial block from the second multivolume archive file. The result is
  2651. * a chunk of memory containing the whole block, and the stream pointer
  2652. * is advanced to the next block in the second multivolume archive file. */
  2653. static int merge_block(struct archive_read* a, ssize_t block_size,
  2654. const uint8_t** p)
  2655. {
  2656. struct rar5* rar = get_context(a);
  2657. ssize_t cur_block_size, partial_offset = 0;
  2658. const uint8_t* lp;
  2659. int ret;
  2660. if(rar->merge_mode) {
  2661. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  2662. "Recursive merge is not allowed");
  2663. return ARCHIVE_FATAL;
  2664. }
  2665. /* Set a flag that we're in the switching mode. */
  2666. rar->cstate.switch_multivolume = 1;
  2667. /* Reallocate the memory which will hold the whole block. */
  2668. if(rar->vol.push_buf)
  2669. free((void*) rar->vol.push_buf);
  2670. /* Increasing the allocation block by 8 is due to bit reading functions,
  2671. * which are using additional 2 or 4 bytes. Allocating the block size
  2672. * by exact value would make bit reader perform reads from invalid
  2673. * memory block when reading the last byte from the buffer. */
  2674. rar->vol.push_buf = malloc(block_size + 8);
  2675. if(!rar->vol.push_buf) {
  2676. archive_set_error(&a->archive, ENOMEM,
  2677. "Can't allocate memory for a merge block buffer.");
  2678. return ARCHIVE_FATAL;
  2679. }
  2680. /* Valgrind complains if the extension block for bit reader is not
  2681. * initialized, so initialize it. */
  2682. memset(&rar->vol.push_buf[block_size], 0, 8);
  2683. /* A single block can span across multiple multivolume archive files,
  2684. * so we use a loop here. This loop will consume enough multivolume
  2685. * archive files until the whole block is read. */
  2686. while(1) {
  2687. /* Get the size of current block chunk in this multivolume
  2688. * archive file and read it. */
  2689. cur_block_size = rar5_min(rar->file.bytes_remaining,
  2690. block_size - partial_offset);
  2691. if(cur_block_size == 0) {
  2692. archive_set_error(&a->archive,
  2693. ARCHIVE_ERRNO_FILE_FORMAT,
  2694. "Encountered block size == 0 during block merge");
  2695. return ARCHIVE_FATAL;
  2696. }
  2697. if(!read_ahead(a, cur_block_size, &lp))
  2698. return ARCHIVE_EOF;
  2699. /* Sanity check; there should never be a situation where this
  2700. * function reads more data than the block's size. */
  2701. if(partial_offset + cur_block_size > block_size) {
  2702. archive_set_error(&a->archive,
  2703. ARCHIVE_ERRNO_PROGRAMMER,
  2704. "Consumed too much data when merging blocks.");
  2705. return ARCHIVE_FATAL;
  2706. }
  2707. /* Merge previous block chunk with current block chunk,
  2708. * or create first block chunk if this is our first
  2709. * iteration. */
  2710. memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
  2711. /* Advance the stream read pointer by this block chunk size. */
  2712. if(ARCHIVE_OK != consume(a, cur_block_size))
  2713. return ARCHIVE_EOF;
  2714. /* Update the pointers. `partial_offset` contains information
  2715. * about the sum of merged block chunks. */
  2716. partial_offset += cur_block_size;
  2717. rar->file.bytes_remaining -= cur_block_size;
  2718. /* If `partial_offset` is the same as `block_size`, this means
  2719. * we've merged all block chunks and we have a valid full
  2720. * block. */
  2721. if(partial_offset == block_size) {
  2722. break;
  2723. }
  2724. /* If we don't have any bytes to read, this means we should
  2725. * switch to another multivolume archive file. */
  2726. if(rar->file.bytes_remaining == 0) {
  2727. rar->merge_mode++;
  2728. ret = advance_multivolume(a);
  2729. rar->merge_mode--;
  2730. if(ret != ARCHIVE_OK) {
  2731. return ret;
  2732. }
  2733. }
  2734. }
  2735. *p = rar->vol.push_buf;
  2736. /* If we're here, we can resume unpacking by processing the block
  2737. * pointed to by the `*p` memory pointer. */
  2738. return ARCHIVE_OK;
  2739. }
  2740. static int process_block(struct archive_read* a) {
  2741. const uint8_t* p;
  2742. struct rar5* rar = get_context(a);
  2743. int ret;
  2744. /* If we don't have any data to be processed, this most probably means
  2745. * we need to switch to the next volume. */
  2746. if(rar->main.volume && rar->file.bytes_remaining == 0) {
  2747. ret = advance_multivolume(a);
  2748. if(ret != ARCHIVE_OK)
  2749. return ret;
  2750. }
  2751. if(rar->cstate.block_parsing_finished) {
  2752. ssize_t block_size;
  2753. ssize_t to_skip;
  2754. ssize_t cur_block_size;
  2755. /* The header size won't be bigger than 6 bytes. */
  2756. if(!read_ahead(a, 6, &p)) {
  2757. /* Failed to prefetch data block header. */
  2758. return ARCHIVE_EOF;
  2759. }
  2760. /*
  2761. * Read block_size by parsing block header. Validate the header
  2762. * by calculating CRC byte stored inside the header. Size of
  2763. * the header is not constant (block size can be stored either
  2764. * in 1 or 2 bytes), that's why block size is left out from the
  2765. * `compressed_block_header` structure and returned by
  2766. * `parse_block_header` as the second argument. */
  2767. ret = parse_block_header(a, p, &block_size,
  2768. &rar->last_block_hdr);
  2769. if(ret != ARCHIVE_OK) {
  2770. return ret;
  2771. }
  2772. /* Skip block header. Next data is huffman tables,
  2773. * if present. */
  2774. to_skip = sizeof(struct compressed_block_header) +
  2775. bf_byte_count(&rar->last_block_hdr) + 1;
  2776. if(ARCHIVE_OK != consume(a, to_skip))
  2777. return ARCHIVE_EOF;
  2778. rar->file.bytes_remaining -= to_skip;
  2779. /* The block size gives information about the whole block size,
  2780. * but the block could be stored in split form when using
  2781. * multi-volume archives. In this case, the block size will be
  2782. * bigger than the actual data stored in this file. Remaining
  2783. * part of the data will be in another file. */
  2784. cur_block_size =
  2785. rar5_min(rar->file.bytes_remaining, block_size);
  2786. if(block_size > rar->file.bytes_remaining) {
  2787. /* If current blocks' size is bigger than our data
  2788. * size, this means we have a multivolume archive.
  2789. * In this case, skip all base headers until the end
  2790. * of the file, proceed to next "partXXX.rar" volume,
  2791. * find its signature, skip all headers up to the first
  2792. * FILE base header, and continue from there.
  2793. *
  2794. * Note that `merge_block` will update the `rar`
  2795. * context structure quite extensively. */
  2796. ret = merge_block(a, block_size, &p);
  2797. if(ret != ARCHIVE_OK) {
  2798. return ret;
  2799. }
  2800. cur_block_size = block_size;
  2801. /* Current stream pointer should be now directly
  2802. * *after* the block that spanned through multiple
  2803. * archive files. `p` pointer should have the data of
  2804. * the *whole* block (merged from partial blocks
  2805. * stored in multiple archives files). */
  2806. } else {
  2807. rar->cstate.switch_multivolume = 0;
  2808. /* Read the whole block size into memory. This can take
  2809. * up to 8 megabytes of memory in theoretical cases.
  2810. * Might be worth to optimize this and use a standard
  2811. * chunk of 4kb's. */
  2812. if(!read_ahead(a, 4 + cur_block_size, &p)) {
  2813. /* Failed to prefetch block data. */
  2814. return ARCHIVE_EOF;
  2815. }
  2816. }
  2817. rar->cstate.block_buf = p;
  2818. rar->cstate.cur_block_size = cur_block_size;
  2819. rar->cstate.block_parsing_finished = 0;
  2820. rar->bits.in_addr = 0;
  2821. rar->bits.bit_addr = 0;
  2822. if(bf_is_table_present(&rar->last_block_hdr)) {
  2823. /* Load Huffman tables. */
  2824. ret = parse_tables(a, rar, p);
  2825. if(ret != ARCHIVE_OK) {
  2826. /* Error during decompression of Huffman
  2827. * tables. */
  2828. return ret;
  2829. }
  2830. }
  2831. } else {
  2832. /* Block parsing not finished, reuse previous memory buffer. */
  2833. p = rar->cstate.block_buf;
  2834. }
  2835. /* Uncompress the block, or a part of it, depending on how many bytes
  2836. * will be generated by uncompressing the block.
  2837. *
  2838. * In case too many bytes will be generated, calling this function
  2839. * again will resume the uncompression operation. */
  2840. ret = do_uncompress_block(a, p);
  2841. if(ret != ARCHIVE_OK) {
  2842. return ret;
  2843. }
  2844. if(rar->cstate.block_parsing_finished &&
  2845. rar->cstate.switch_multivolume == 0 &&
  2846. rar->cstate.cur_block_size > 0)
  2847. {
  2848. /* If we're processing a normal block, consume the whole
  2849. * block. We can do this because we've already read the whole
  2850. * block to memory. */
  2851. if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
  2852. return ARCHIVE_FATAL;
  2853. rar->file.bytes_remaining -= rar->cstate.cur_block_size;
  2854. } else if(rar->cstate.switch_multivolume) {
  2855. /* Don't consume the block if we're doing multivolume
  2856. * processing. The volume switching function will consume
  2857. * the proper count of bytes instead. */
  2858. rar->cstate.switch_multivolume = 0;
  2859. }
  2860. return ARCHIVE_OK;
  2861. }
  2862. /* Pops the `buf`, `size` and `offset` from the "data ready" stack.
  2863. *
  2864. * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
  2865. * when there is no data on the stack. */
  2866. static int use_data(struct rar5* rar, const void** buf, size_t* size,
  2867. int64_t* offset)
  2868. {
  2869. int i;
  2870. for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
  2871. struct data_ready *d = &rar->cstate.dready[i];
  2872. if(d->used) {
  2873. if(buf) *buf = d->buf;
  2874. if(size) *size = d->size;
  2875. if(offset) *offset = d->offset;
  2876. d->used = 0;
  2877. return ARCHIVE_OK;
  2878. }
  2879. }
  2880. return ARCHIVE_RETRY;
  2881. }
  2882. /* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
  2883. * FIFO stack. Those values will be popped from this stack by the `use_data`
  2884. * function. */
  2885. static int push_data_ready(struct archive_read* a, struct rar5* rar,
  2886. const uint8_t* buf, size_t size, int64_t offset)
  2887. {
  2888. int i;
  2889. /* Don't push if we're in skip mode. This is needed because solid
  2890. * streams need full processing even if we're skipping data. After
  2891. * fully processing the stream, we need to discard the generated bytes,
  2892. * because we're interested only in the side effect: building up the
  2893. * internal window circular buffer. This window buffer will be used
  2894. * later during unpacking of requested data. */
  2895. if(rar->skip_mode)
  2896. return ARCHIVE_OK;
  2897. /* Sanity check. */
  2898. if(offset != rar->file.last_offset + rar->file.last_size) {
  2899. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  2900. "Sanity check error: output stream is not continuous");
  2901. return ARCHIVE_FATAL;
  2902. }
  2903. for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
  2904. struct data_ready* d = &rar->cstate.dready[i];
  2905. if(!d->used) {
  2906. d->used = 1;
  2907. d->buf = buf;
  2908. d->size = size;
  2909. d->offset = offset;
  2910. /* These fields are used only in sanity checking. */
  2911. rar->file.last_offset = offset;
  2912. rar->file.last_size = size;
  2913. /* Calculate the checksum of this new block before
  2914. * submitting data to libarchive's engine. */
  2915. update_crc(rar, d->buf, d->size);
  2916. return ARCHIVE_OK;
  2917. }
  2918. }
  2919. /* Program counter will reach this code if the `rar->cstate.data_ready`
  2920. * stack will be filled up so that no new entries will be allowed. The
  2921. * code shouldn't allow such situation to occur. So we treat this case
  2922. * as an internal error. */
  2923. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  2924. "Error: premature end of data_ready stack");
  2925. return ARCHIVE_FATAL;
  2926. }
  2927. /* This function uncompresses the data that is stored in the <FILE> base
  2928. * block.
  2929. *
  2930. * The FILE base block looks like this:
  2931. *
  2932. * <header><huffman tables><block_1><block_2>...<block_n>
  2933. *
  2934. * The <header> is a block header, that is parsed in parse_block_header().
  2935. * It's a "compressed_block_header" structure, containing metadata needed
  2936. * to know when we should stop looking for more <block_n> blocks.
  2937. *
  2938. * <huffman tables> contain data needed to set up the huffman tables, needed
  2939. * for the actual decompression.
  2940. *
  2941. * Each <block_n> consists of series of literals:
  2942. *
  2943. * <literal><literal><literal>...<literal>
  2944. *
  2945. * Those literals generate the uncompression data. They operate on a circular
  2946. * buffer, sometimes writing raw data into it, sometimes referencing
  2947. * some previous data inside this buffer, and sometimes declaring a filter
  2948. * that will need to be executed on the data stored in the circular buffer.
  2949. * It all depends on the literal that is used.
  2950. *
  2951. * Sometimes blocks produce output data, sometimes they don't. For example, for
  2952. * some huge files that use lots of filters, sometimes a block is filled with
  2953. * only filter declaration literals. Such blocks won't produce any data in the
  2954. * circular buffer.
  2955. *
  2956. * Sometimes blocks will produce 4 bytes of data, and sometimes 1 megabyte,
  2957. * because a literal can reference previously decompressed data. For example,
  2958. * there can be a literal that says: 'append a byte 0xFE here', and after
  2959. * it another literal can say 'append 1 megabyte of data from circular buffer
  2960. * offset 0x12345'. This is how RAR format handles compressing repeated
  2961. * patterns.
  2962. *
  2963. * The RAR compressor creates those literals and the actual efficiency of
  2964. * compression depends on what those literals are. The literals can also
  2965. * be seen as a kind of a non-turing-complete virtual machine that simply
  2966. * tells the decompressor what it should do.
  2967. * */
  2968. static int do_uncompress_file(struct archive_read* a) {
  2969. struct rar5* rar = get_context(a);
  2970. int ret;
  2971. int64_t max_end_pos;
  2972. if(!rar->cstate.initialized) {
  2973. /* Don't perform full context reinitialization if we're
  2974. * processing a solid archive. */
  2975. if(!rar->main.solid || !rar->cstate.window_buf) {
  2976. init_unpack(rar);
  2977. }
  2978. rar->cstate.initialized = 1;
  2979. }
  2980. if(rar->cstate.all_filters_applied == 1) {
  2981. /* We use while(1) here, but standard case allows for just 1
  2982. * iteration. The loop will iterate if process_block() didn't
  2983. * generate any data at all. This can happen if the block
  2984. * contains only filter definitions (this is common in big
  2985. * files). */
  2986. while(1) {
  2987. ret = process_block(a);
  2988. if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
  2989. return ret;
  2990. if(rar->cstate.last_write_ptr ==
  2991. rar->cstate.write_ptr) {
  2992. /* The block didn't generate any new data,
  2993. * so just process a new block. */
  2994. continue;
  2995. }
  2996. /* The block has generated some new data, so break
  2997. * the loop. */
  2998. break;
  2999. }
  3000. }
  3001. /* Try to run filters. If filters won't be applied, it means that
  3002. * insufficient data was generated. */
  3003. ret = apply_filters(a);
  3004. if(ret == ARCHIVE_RETRY) {
  3005. return ARCHIVE_OK;
  3006. } else if(ret == ARCHIVE_FATAL) {
  3007. return ARCHIVE_FATAL;
  3008. }
  3009. /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
  3010. if(cdeque_size(&rar->cstate.filters) > 0) {
  3011. /* Check if we can write something before hitting first
  3012. * filter. */
  3013. struct filter_info* flt;
  3014. /* Get the block_start offset from the first filter. */
  3015. if(CDE_OK != cdeque_front(&rar->cstate.filters,
  3016. cdeque_filter_p(&flt)))
  3017. {
  3018. archive_set_error(&a->archive,
  3019. ARCHIVE_ERRNO_PROGRAMMER,
  3020. "Can't read first filter");
  3021. return ARCHIVE_FATAL;
  3022. }
  3023. max_end_pos = rar5_min(flt->block_start,
  3024. rar->cstate.write_ptr);
  3025. } else {
  3026. /* There are no filters defined, or all filters were applied.
  3027. * This means we can just store the data without any
  3028. * postprocessing. */
  3029. max_end_pos = rar->cstate.write_ptr;
  3030. }
  3031. if(max_end_pos == rar->cstate.last_write_ptr) {
  3032. /* We can't write anything yet. The block uncompression
  3033. * function did not generate enough data, and no filter can be
  3034. * applied. At the same time we don't have any data that can be
  3035. * stored without filter postprocessing. This means we need to
  3036. * wait for more data to be generated, so we can apply the
  3037. * filters.
  3038. *
  3039. * Signal the caller that we need more data to be able to do
  3040. * anything.
  3041. */
  3042. return ARCHIVE_RETRY;
  3043. } else {
  3044. /* We can write the data before hitting the first filter.
  3045. * So let's do it. The push_window_data() function will
  3046. * effectively return the selected data block to the user
  3047. * application. */
  3048. push_window_data(a, rar, rar->cstate.last_write_ptr,
  3049. max_end_pos);
  3050. rar->cstate.last_write_ptr = max_end_pos;
  3051. }
  3052. return ARCHIVE_OK;
  3053. }
  3054. static int uncompress_file(struct archive_read* a) {
  3055. int ret;
  3056. while(1) {
  3057. /* Sometimes the uncompression function will return a
  3058. * 'retry' signal. If this will happen, we have to retry
  3059. * the function. */
  3060. ret = do_uncompress_file(a);
  3061. if(ret != ARCHIVE_RETRY)
  3062. return ret;
  3063. }
  3064. }
  3065. static int do_unstore_file(struct archive_read* a,
  3066. struct rar5* rar, const void** buf, size_t* size, int64_t* offset)
  3067. {
  3068. size_t to_read;
  3069. const uint8_t* p;
  3070. if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
  3071. rar->generic.split_after > 0)
  3072. {
  3073. int ret;
  3074. rar->cstate.switch_multivolume = 1;
  3075. ret = advance_multivolume(a);
  3076. rar->cstate.switch_multivolume = 0;
  3077. if(ret != ARCHIVE_OK) {
  3078. /* Failed to advance to next multivolume archive
  3079. * file. */
  3080. return ret;
  3081. }
  3082. }
  3083. to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
  3084. if(to_read == 0) {
  3085. return ARCHIVE_EOF;
  3086. }
  3087. if(!read_ahead(a, to_read, &p)) {
  3088. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3089. "I/O error when unstoring file");
  3090. return ARCHIVE_FATAL;
  3091. }
  3092. if(ARCHIVE_OK != consume(a, to_read)) {
  3093. return ARCHIVE_EOF;
  3094. }
  3095. if(buf) *buf = p;
  3096. if(size) *size = to_read;
  3097. if(offset) *offset = rar->cstate.last_unstore_ptr;
  3098. rar->file.bytes_remaining -= to_read;
  3099. rar->cstate.last_unstore_ptr += to_read;
  3100. update_crc(rar, p, to_read);
  3101. return ARCHIVE_OK;
  3102. }
  3103. static int do_unpack(struct archive_read* a, struct rar5* rar,
  3104. const void** buf, size_t* size, int64_t* offset)
  3105. {
  3106. enum COMPRESSION_METHOD {
  3107. STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4,
  3108. BEST = 5
  3109. };
  3110. if(rar->file.service > 0) {
  3111. return do_unstore_file(a, rar, buf, size, offset);
  3112. } else {
  3113. switch(rar->cstate.method) {
  3114. case STORE:
  3115. return do_unstore_file(a, rar, buf, size,
  3116. offset);
  3117. case FASTEST:
  3118. /* fallthrough */
  3119. case FAST:
  3120. /* fallthrough */
  3121. case NORMAL:
  3122. /* fallthrough */
  3123. case GOOD:
  3124. /* fallthrough */
  3125. case BEST:
  3126. return uncompress_file(a);
  3127. default:
  3128. archive_set_error(&a->archive,
  3129. ARCHIVE_ERRNO_FILE_FORMAT,
  3130. "Compression method not supported: 0x%x",
  3131. rar->cstate.method);
  3132. return ARCHIVE_FATAL;
  3133. }
  3134. }
  3135. #if !defined WIN32
  3136. /* Not reached. */
  3137. return ARCHIVE_OK;
  3138. #endif
  3139. }
  3140. static int verify_checksums(struct archive_read* a) {
  3141. int verify_crc;
  3142. struct rar5* rar = get_context(a);
  3143. /* Check checksums only when actually unpacking the data. There's no
  3144. * need to calculate checksum when we're skipping data in solid archives
  3145. * (skipping in solid archives is the same thing as unpacking compressed
  3146. * data and discarding the result). */
  3147. if(!rar->skip_mode) {
  3148. /* Always check checksums if we're not in skip mode */
  3149. verify_crc = 1;
  3150. } else {
  3151. /* We can override the logic above with a compile-time option
  3152. * NO_CRC_ON_SOLID_SKIP. This option is used during debugging,
  3153. * and it will check checksums of unpacked data even when
  3154. * we're skipping it. */
  3155. #if defined CHECK_CRC_ON_SOLID_SKIP
  3156. /* Debug case */
  3157. verify_crc = 1;
  3158. #else
  3159. /* Normal case */
  3160. verify_crc = 0;
  3161. #endif
  3162. }
  3163. if(verify_crc) {
  3164. /* During unpacking, on each unpacked block we're calling the
  3165. * update_crc() function. Since we are here, the unpacking
  3166. * process is already over and we can check if calculated
  3167. * checksum (CRC32 or BLAKE2sp) is the same as what is stored
  3168. * in the archive. */
  3169. if(rar->file.stored_crc32 > 0) {
  3170. /* Check CRC32 only when the file contains a CRC32
  3171. * value for this file. */
  3172. if(rar->file.calculated_crc32 !=
  3173. rar->file.stored_crc32) {
  3174. /* Checksums do not match; the unpacked file
  3175. * is corrupted. */
  3176. DEBUG_CODE {
  3177. printf("Checksum error: CRC32 "
  3178. "(was: %08x, expected: %08x)\n",
  3179. rar->file.calculated_crc32,
  3180. rar->file.stored_crc32);
  3181. }
  3182. #ifndef DONT_FAIL_ON_CRC_ERROR
  3183. archive_set_error(&a->archive,
  3184. ARCHIVE_ERRNO_FILE_FORMAT,
  3185. "Checksum error: CRC32");
  3186. return ARCHIVE_FATAL;
  3187. #endif
  3188. } else {
  3189. DEBUG_CODE {
  3190. printf("Checksum OK: CRC32 "
  3191. "(%08x/%08x)\n",
  3192. rar->file.stored_crc32,
  3193. rar->file.calculated_crc32);
  3194. }
  3195. }
  3196. }
  3197. if(rar->file.has_blake2 > 0) {
  3198. /* BLAKE2sp is an optional checksum algorithm that is
  3199. * added to RARv5 archives when using the `-htb` switch
  3200. * during creation of archive.
  3201. *
  3202. * We now finalize the hash calculation by calling the
  3203. * `final` function. This will generate the final hash
  3204. * value we can use to compare it with the BLAKE2sp
  3205. * checksum that is stored in the archive.
  3206. *
  3207. * The return value of this `final` function is not
  3208. * very helpful, as it guards only against improper use.
  3209. * This is why we're explicitly ignoring it. */
  3210. uint8_t b2_buf[32];
  3211. (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
  3212. if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
  3213. #ifndef DONT_FAIL_ON_CRC_ERROR
  3214. archive_set_error(&a->archive,
  3215. ARCHIVE_ERRNO_FILE_FORMAT,
  3216. "Checksum error: BLAKE2");
  3217. return ARCHIVE_FATAL;
  3218. #endif
  3219. }
  3220. }
  3221. }
  3222. /* Finalization for this file has been successfully completed. */
  3223. return ARCHIVE_OK;
  3224. }
  3225. static int verify_global_checksums(struct archive_read* a) {
  3226. return verify_checksums(a);
  3227. }
  3228. /*
  3229. * Decryption function for the magic signature pattern. Check the comment near
  3230. * the `rar5_signature_xor` symbol to read the rationale behind this.
  3231. */
  3232. static void rar5_signature(char *buf) {
  3233. size_t i;
  3234. for(i = 0; i < sizeof(rar5_signature_xor); i++) {
  3235. buf[i] = rar5_signature_xor[i] ^ 0xA1;
  3236. }
  3237. }
  3238. static int rar5_read_data(struct archive_read *a, const void **buff,
  3239. size_t *size, int64_t *offset) {
  3240. int ret;
  3241. struct rar5* rar = get_context(a);
  3242. if(rar->file.dir > 0) {
  3243. /* Don't process any data if this file entry was declared
  3244. * as a directory. This is needed, because entries marked as
  3245. * directory doesn't have any dictionary buffer allocated, so
  3246. * it's impossible to perform any decompression. */
  3247. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3248. "Can't decompress an entry marked as a directory");
  3249. return ARCHIVE_FAILED;
  3250. }
  3251. if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
  3252. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  3253. "Unpacker has written too many bytes");
  3254. return ARCHIVE_FATAL;
  3255. }
  3256. ret = use_data(rar, buff, size, offset);
  3257. if(ret == ARCHIVE_OK) {
  3258. return ret;
  3259. }
  3260. if(rar->file.eof == 1) {
  3261. return ARCHIVE_EOF;
  3262. }
  3263. ret = do_unpack(a, rar, buff, size, offset);
  3264. if(ret != ARCHIVE_OK) {
  3265. return ret;
  3266. }
  3267. if(rar->file.bytes_remaining == 0 &&
  3268. rar->cstate.last_write_ptr == rar->file.unpacked_size)
  3269. {
  3270. /* If all bytes of current file were processed, run
  3271. * finalization.
  3272. *
  3273. * Finalization will check checksum against proper values. If
  3274. * some of the checksums will not match, we'll return an error
  3275. * value in the last `archive_read_data` call to signal an error
  3276. * to the user. */
  3277. rar->file.eof = 1;
  3278. return verify_global_checksums(a);
  3279. }
  3280. return ARCHIVE_OK;
  3281. }
  3282. static int rar5_read_data_skip(struct archive_read *a) {
  3283. struct rar5* rar = get_context(a);
  3284. if(rar->main.solid) {
  3285. /* In solid archives, instead of skipping the data, we need to
  3286. * extract it, and dispose the result. The side effect of this
  3287. * operation will be setting up the initial window buffer state
  3288. * needed to be able to extract the selected file. */
  3289. int ret;
  3290. /* Make sure to process all blocks in the compressed stream. */
  3291. while(rar->file.bytes_remaining > 0) {
  3292. /* Setting the "skip mode" will allow us to skip
  3293. * checksum checks during data skipping. Checking the
  3294. * checksum of skipped data isn't really necessary and
  3295. * it's only slowing things down.
  3296. *
  3297. * This is incremented instead of setting to 1 because
  3298. * this data skipping function can be called
  3299. * recursively. */
  3300. rar->skip_mode++;
  3301. /* We're disposing 1 block of data, so we use triple
  3302. * NULLs in arguments. */
  3303. ret = rar5_read_data(a, NULL, NULL, NULL);
  3304. /* Turn off "skip mode". */
  3305. rar->skip_mode--;
  3306. if(ret < 0 || ret == ARCHIVE_EOF) {
  3307. /* Propagate any potential error conditions
  3308. * to the caller. */
  3309. return ret;
  3310. }
  3311. }
  3312. } else {
  3313. /* In standard archives, we can just jump over the compressed
  3314. * stream. Each file in non-solid archives starts from an empty
  3315. * window buffer. */
  3316. if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
  3317. return ARCHIVE_FATAL;
  3318. }
  3319. rar->file.bytes_remaining = 0;
  3320. }
  3321. return ARCHIVE_OK;
  3322. }
  3323. static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
  3324. int whence)
  3325. {
  3326. (void) a;
  3327. (void) offset;
  3328. (void) whence;
  3329. /* We're a streaming unpacker, and we don't support seeking. */
  3330. return ARCHIVE_FATAL;
  3331. }
  3332. static int rar5_cleanup(struct archive_read *a) {
  3333. struct rar5* rar = get_context(a);
  3334. free(rar->cstate.window_buf);
  3335. free(rar->cstate.filtered_buf);
  3336. free(rar->vol.push_buf);
  3337. free_filters(rar);
  3338. cdeque_free(&rar->cstate.filters);
  3339. free(rar);
  3340. a->format->data = NULL;
  3341. return ARCHIVE_OK;
  3342. }
  3343. static int rar5_capabilities(struct archive_read * a) {
  3344. (void) a;
  3345. return 0;
  3346. }
  3347. static int rar5_has_encrypted_entries(struct archive_read *_a) {
  3348. (void) _a;
  3349. /* Unsupported for now. */
  3350. return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
  3351. }
  3352. static int rar5_init(struct rar5* rar) {
  3353. memset(rar, 0, sizeof(struct rar5));
  3354. if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
  3355. return ARCHIVE_FATAL;
  3356. return ARCHIVE_OK;
  3357. }
  3358. int archive_read_support_format_rar5(struct archive *_a) {
  3359. struct archive_read* ar;
  3360. int ret;
  3361. struct rar5* rar;
  3362. if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
  3363. return ret;
  3364. rar = malloc(sizeof(*rar));
  3365. if(rar == NULL) {
  3366. archive_set_error(&ar->archive, ENOMEM,
  3367. "Can't allocate rar5 data");
  3368. return ARCHIVE_FATAL;
  3369. }
  3370. if(ARCHIVE_OK != rar5_init(rar)) {
  3371. archive_set_error(&ar->archive, ENOMEM,
  3372. "Can't allocate rar5 filter buffer");
  3373. return ARCHIVE_FATAL;
  3374. }
  3375. ret = __archive_read_register_format(ar,
  3376. rar,
  3377. "rar5",
  3378. rar5_bid,
  3379. rar5_options,
  3380. rar5_read_header,
  3381. rar5_read_data,
  3382. rar5_read_data_skip,
  3383. rar5_seek_data,
  3384. rar5_cleanup,
  3385. rar5_capabilities,
  3386. rar5_has_encrypted_entries);
  3387. if(ret != ARCHIVE_OK) {
  3388. (void) rar5_cleanup(ar);
  3389. }
  3390. return ret;
  3391. }