archive_read_support_format_rar.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011 Andres Mejia
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include <time.h>
  31. #include <limits.h>
  32. #ifdef HAVE_ZLIB_H
  33. #include <cm_zlib.h> /* crc32 */
  34. #endif
  35. #include "archive.h"
  36. #ifndef HAVE_ZLIB_H
  37. #include "archive_crc32.h"
  38. #endif
  39. #include "archive_endian.h"
  40. #include "archive_entry.h"
  41. #include "archive_entry_locale.h"
  42. #include "archive_ppmd7_private.h"
  43. #include "archive_private.h"
  44. #include "archive_read_private.h"
  45. /* RAR signature, also known as the mark header */
  46. #define RAR_SIGNATURE "\x52\x61\x72\x21\x1A\x07\x00"
  47. /* Header types */
  48. #define MARK_HEAD 0x72
  49. #define MAIN_HEAD 0x73
  50. #define FILE_HEAD 0x74
  51. #define COMM_HEAD 0x75
  52. #define AV_HEAD 0x76
  53. #define SUB_HEAD 0x77
  54. #define PROTECT_HEAD 0x78
  55. #define SIGN_HEAD 0x79
  56. #define NEWSUB_HEAD 0x7a
  57. #define ENDARC_HEAD 0x7b
  58. /* Main Header Flags */
  59. #define MHD_VOLUME 0x0001
  60. #define MHD_COMMENT 0x0002
  61. #define MHD_LOCK 0x0004
  62. #define MHD_SOLID 0x0008
  63. #define MHD_NEWNUMBERING 0x0010
  64. #define MHD_AV 0x0020
  65. #define MHD_PROTECT 0x0040
  66. #define MHD_PASSWORD 0x0080
  67. #define MHD_FIRSTVOLUME 0x0100
  68. #define MHD_ENCRYPTVER 0x0200
  69. /* Flags common to all headers */
  70. #define HD_MARKDELETION 0x4000
  71. #define HD_ADD_SIZE_PRESENT 0x8000
  72. /* File Header Flags */
  73. #define FHD_SPLIT_BEFORE 0x0001
  74. #define FHD_SPLIT_AFTER 0x0002
  75. #define FHD_PASSWORD 0x0004
  76. #define FHD_COMMENT 0x0008
  77. #define FHD_SOLID 0x0010
  78. #define FHD_LARGE 0x0100
  79. #define FHD_UNICODE 0x0200
  80. #define FHD_SALT 0x0400
  81. #define FHD_VERSION 0x0800
  82. #define FHD_EXTTIME 0x1000
  83. #define FHD_EXTFLAGS 0x2000
  84. /* File dictionary sizes */
  85. #define DICTIONARY_SIZE_64 0x00
  86. #define DICTIONARY_SIZE_128 0x20
  87. #define DICTIONARY_SIZE_256 0x40
  88. #define DICTIONARY_SIZE_512 0x60
  89. #define DICTIONARY_SIZE_1024 0x80
  90. #define DICTIONARY_SIZE_2048 0xA0
  91. #define DICTIONARY_SIZE_4096 0xC0
  92. #define FILE_IS_DIRECTORY 0xE0
  93. #define DICTIONARY_MASK FILE_IS_DIRECTORY
  94. /* OS Flags */
  95. #define OS_MSDOS 0
  96. #define OS_OS2 1
  97. #define OS_WIN32 2
  98. #define OS_UNIX 3
  99. #define OS_MAC_OS 4
  100. #define OS_BEOS 5
  101. /* Compression Methods */
  102. #define COMPRESS_METHOD_STORE 0x30
  103. /* LZSS */
  104. #define COMPRESS_METHOD_FASTEST 0x31
  105. #define COMPRESS_METHOD_FAST 0x32
  106. #define COMPRESS_METHOD_NORMAL 0x33
  107. /* PPMd Variant H */
  108. #define COMPRESS_METHOD_GOOD 0x34
  109. #define COMPRESS_METHOD_BEST 0x35
  110. #define CRC_POLYNOMIAL 0xEDB88320
  111. #define NS_UNIT 10000000
  112. #define DICTIONARY_MAX_SIZE 0x400000
  113. #define MAINCODE_SIZE 299
  114. #define OFFSETCODE_SIZE 60
  115. #define LOWOFFSETCODE_SIZE 17
  116. #define LENGTHCODE_SIZE 28
  117. #define HUFFMAN_TABLE_SIZE \
  118. MAINCODE_SIZE + OFFSETCODE_SIZE + LOWOFFSETCODE_SIZE + LENGTHCODE_SIZE
  119. #define MAX_SYMBOL_LENGTH 0xF
  120. #define MAX_SYMBOLS 20
  121. /*
  122. * Considering L1,L2 cache miss and a calling of write system-call,
  123. * the best size of the output buffer(uncompressed buffer) is 128K.
  124. * If the structure of extracting process is changed, this value
  125. * might be researched again.
  126. */
  127. #define UNP_BUFFER_SIZE (128 * 1024)
  128. /* Define this here for non-Windows platforms */
  129. #if !((defined(__WIN32__) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__))
  130. #define FILE_ATTRIBUTE_DIRECTORY 0x10
  131. #endif
  132. #undef minimum
  133. #define minimum(a, b) ((a)<(b)?(a):(b))
  134. /* Fields common to all headers */
  135. struct rar_header
  136. {
  137. char crc[2];
  138. char type;
  139. char flags[2];
  140. char size[2];
  141. };
  142. /* Fields common to all file headers */
  143. struct rar_file_header
  144. {
  145. char pack_size[4];
  146. char unp_size[4];
  147. char host_os;
  148. char file_crc[4];
  149. char file_time[4];
  150. char unp_ver;
  151. char method;
  152. char name_size[2];
  153. char file_attr[4];
  154. };
  155. struct huffman_tree_node
  156. {
  157. int branches[2];
  158. };
  159. struct huffman_table_entry
  160. {
  161. unsigned int length;
  162. int value;
  163. };
  164. struct huffman_code
  165. {
  166. struct huffman_tree_node *tree;
  167. int numentries;
  168. int numallocatedentries;
  169. int minlength;
  170. int maxlength;
  171. int tablesize;
  172. struct huffman_table_entry *table;
  173. };
  174. struct lzss
  175. {
  176. unsigned char *window;
  177. int mask;
  178. int64_t position;
  179. };
  180. struct data_block_offsets
  181. {
  182. int64_t header_size;
  183. int64_t start_offset;
  184. int64_t end_offset;
  185. };
  186. struct rar
  187. {
  188. /* Entries from main RAR header */
  189. unsigned main_flags;
  190. unsigned long file_crc;
  191. char reserved1[2];
  192. char reserved2[4];
  193. char encryptver;
  194. /* File header entries */
  195. char compression_method;
  196. unsigned file_flags;
  197. int64_t packed_size;
  198. int64_t unp_size;
  199. time_t mtime;
  200. long mnsec;
  201. mode_t mode;
  202. char *filename;
  203. char *filename_save;
  204. size_t filename_save_size;
  205. size_t filename_allocated;
  206. /* File header optional entries */
  207. char salt[8];
  208. time_t atime;
  209. long ansec;
  210. time_t ctime;
  211. long cnsec;
  212. time_t arctime;
  213. long arcnsec;
  214. /* Fields to help with tracking decompression of files. */
  215. int64_t bytes_unconsumed;
  216. int64_t bytes_remaining;
  217. int64_t bytes_uncopied;
  218. int64_t offset;
  219. int64_t offset_outgoing;
  220. int64_t offset_seek;
  221. char valid;
  222. unsigned int unp_offset;
  223. unsigned int unp_buffer_size;
  224. unsigned char *unp_buffer;
  225. unsigned int dictionary_size;
  226. char start_new_block;
  227. char entry_eof;
  228. unsigned long crc_calculated;
  229. int found_first_header;
  230. char has_endarc_header;
  231. struct data_block_offsets *dbo;
  232. unsigned int cursor;
  233. unsigned int nodes;
  234. char filename_must_match;
  235. /* LZSS members */
  236. struct huffman_code maincode;
  237. struct huffman_code offsetcode;
  238. struct huffman_code lowoffsetcode;
  239. struct huffman_code lengthcode;
  240. unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
  241. struct lzss lzss;
  242. char output_last_match;
  243. unsigned int lastlength;
  244. unsigned int lastoffset;
  245. unsigned int oldoffset[4];
  246. unsigned int lastlowoffset;
  247. unsigned int numlowoffsetrepeats;
  248. int64_t filterstart;
  249. char start_new_table;
  250. /* PPMd Variant H members */
  251. char ppmd_valid;
  252. char ppmd_eod;
  253. char is_ppmd_block;
  254. int ppmd_escape;
  255. CPpmd7 ppmd7_context;
  256. CPpmd7z_RangeDec range_dec;
  257. IByteIn bytein;
  258. /*
  259. * String conversion object.
  260. */
  261. int init_default_conversion;
  262. struct archive_string_conv *sconv_default;
  263. struct archive_string_conv *opt_sconv;
  264. struct archive_string_conv *sconv_utf8;
  265. struct archive_string_conv *sconv_utf16be;
  266. /*
  267. * Bit stream reader.
  268. */
  269. struct rar_br {
  270. #define CACHE_TYPE uint64_t
  271. #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
  272. /* Cache buffer. */
  273. CACHE_TYPE cache_buffer;
  274. /* Indicates how many bits avail in cache_buffer. */
  275. int cache_avail;
  276. ssize_t avail_in;
  277. const unsigned char *next_in;
  278. } br;
  279. /*
  280. * Custom field to denote that this archive contains encrypted entries
  281. */
  282. int has_encrypted_entries;
  283. };
  284. static int archive_read_support_format_rar_capabilities(struct archive_read *);
  285. static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
  286. static int archive_read_format_rar_bid(struct archive_read *, int);
  287. static int archive_read_format_rar_options(struct archive_read *,
  288. const char *, const char *);
  289. static int archive_read_format_rar_read_header(struct archive_read *,
  290. struct archive_entry *);
  291. static int archive_read_format_rar_read_data(struct archive_read *,
  292. const void **, size_t *, int64_t *);
  293. static int archive_read_format_rar_read_data_skip(struct archive_read *a);
  294. static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
  295. int);
  296. static int archive_read_format_rar_cleanup(struct archive_read *);
  297. /* Support functions */
  298. static int read_header(struct archive_read *, struct archive_entry *, char);
  299. static time_t get_time(int);
  300. static int read_exttime(const char *, struct rar *, const char *);
  301. static int read_symlink_stored(struct archive_read *, struct archive_entry *,
  302. struct archive_string_conv *);
  303. static int read_data_stored(struct archive_read *, const void **, size_t *,
  304. int64_t *);
  305. static int read_data_compressed(struct archive_read *, const void **, size_t *,
  306. int64_t *);
  307. static int rar_br_preparation(struct archive_read *, struct rar_br *);
  308. static int parse_codes(struct archive_read *);
  309. static void free_codes(struct archive_read *);
  310. static int read_next_symbol(struct archive_read *, struct huffman_code *);
  311. static int create_code(struct archive_read *, struct huffman_code *,
  312. unsigned char *, int, char);
  313. static int add_value(struct archive_read *, struct huffman_code *, int, int,
  314. int);
  315. static int new_node(struct huffman_code *);
  316. static int make_table(struct archive_read *, struct huffman_code *);
  317. static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
  318. struct huffman_table_entry *, int, int);
  319. static int64_t expand(struct archive_read *, int64_t);
  320. static int copy_from_lzss_window(struct archive_read *, const void **,
  321. int64_t, int);
  322. static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
  323. /*
  324. * Bit stream reader.
  325. */
  326. /* Check that the cache buffer has enough bits. */
  327. #define rar_br_has(br, n) ((br)->cache_avail >= n)
  328. /* Get compressed data by bit. */
  329. #define rar_br_bits(br, n) \
  330. (((uint32_t)((br)->cache_buffer >> \
  331. ((br)->cache_avail - (n)))) & cache_masks[n])
  332. #define rar_br_bits_forced(br, n) \
  333. (((uint32_t)((br)->cache_buffer << \
  334. ((n) - (br)->cache_avail))) & cache_masks[n])
  335. /* Read ahead to make sure the cache buffer has enough compressed data we
  336. * will use.
  337. * True : completed, there is enough data in the cache buffer.
  338. * False : there is no data in the stream. */
  339. #define rar_br_read_ahead(a, br, n) \
  340. ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
  341. /* Notify how many bits we consumed. */
  342. #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
  343. #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
  344. static const uint32_t cache_masks[] = {
  345. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  346. 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
  347. 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
  348. 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
  349. 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
  350. 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
  351. 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
  352. 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
  353. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  354. };
  355. /*
  356. * Shift away used bits in the cache data and fill it up with following bits.
  357. * Call this when cache buffer does not have enough bits you need.
  358. *
  359. * Returns 1 if the cache buffer is full.
  360. * Returns 0 if the cache buffer is not full; input buffer is empty.
  361. */
  362. static int
  363. rar_br_fillup(struct archive_read *a, struct rar_br *br)
  364. {
  365. struct rar *rar = (struct rar *)(a->format->data);
  366. int n = CACHE_BITS - br->cache_avail;
  367. for (;;) {
  368. switch (n >> 3) {
  369. case 8:
  370. if (br->avail_in >= 8) {
  371. br->cache_buffer =
  372. ((uint64_t)br->next_in[0]) << 56 |
  373. ((uint64_t)br->next_in[1]) << 48 |
  374. ((uint64_t)br->next_in[2]) << 40 |
  375. ((uint64_t)br->next_in[3]) << 32 |
  376. ((uint32_t)br->next_in[4]) << 24 |
  377. ((uint32_t)br->next_in[5]) << 16 |
  378. ((uint32_t)br->next_in[6]) << 8 |
  379. (uint32_t)br->next_in[7];
  380. br->next_in += 8;
  381. br->avail_in -= 8;
  382. br->cache_avail += 8 * 8;
  383. rar->bytes_unconsumed += 8;
  384. rar->bytes_remaining -= 8;
  385. return (1);
  386. }
  387. break;
  388. case 7:
  389. if (br->avail_in >= 7) {
  390. br->cache_buffer =
  391. (br->cache_buffer << 56) |
  392. ((uint64_t)br->next_in[0]) << 48 |
  393. ((uint64_t)br->next_in[1]) << 40 |
  394. ((uint64_t)br->next_in[2]) << 32 |
  395. ((uint32_t)br->next_in[3]) << 24 |
  396. ((uint32_t)br->next_in[4]) << 16 |
  397. ((uint32_t)br->next_in[5]) << 8 |
  398. (uint32_t)br->next_in[6];
  399. br->next_in += 7;
  400. br->avail_in -= 7;
  401. br->cache_avail += 7 * 8;
  402. rar->bytes_unconsumed += 7;
  403. rar->bytes_remaining -= 7;
  404. return (1);
  405. }
  406. break;
  407. case 6:
  408. if (br->avail_in >= 6) {
  409. br->cache_buffer =
  410. (br->cache_buffer << 48) |
  411. ((uint64_t)br->next_in[0]) << 40 |
  412. ((uint64_t)br->next_in[1]) << 32 |
  413. ((uint32_t)br->next_in[2]) << 24 |
  414. ((uint32_t)br->next_in[3]) << 16 |
  415. ((uint32_t)br->next_in[4]) << 8 |
  416. (uint32_t)br->next_in[5];
  417. br->next_in += 6;
  418. br->avail_in -= 6;
  419. br->cache_avail += 6 * 8;
  420. rar->bytes_unconsumed += 6;
  421. rar->bytes_remaining -= 6;
  422. return (1);
  423. }
  424. break;
  425. case 0:
  426. /* We have enough compressed data in
  427. * the cache buffer.*/
  428. return (1);
  429. default:
  430. break;
  431. }
  432. if (br->avail_in <= 0) {
  433. if (rar->bytes_unconsumed > 0) {
  434. /* Consume as much as the decompressor
  435. * actually used. */
  436. __archive_read_consume(a, rar->bytes_unconsumed);
  437. rar->bytes_unconsumed = 0;
  438. }
  439. br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
  440. if (br->next_in == NULL)
  441. return (0);
  442. if (br->avail_in == 0)
  443. return (0);
  444. }
  445. br->cache_buffer =
  446. (br->cache_buffer << 8) | *br->next_in++;
  447. br->avail_in--;
  448. br->cache_avail += 8;
  449. n -= 8;
  450. rar->bytes_unconsumed++;
  451. rar->bytes_remaining--;
  452. }
  453. }
  454. static int
  455. rar_br_preparation(struct archive_read *a, struct rar_br *br)
  456. {
  457. struct rar *rar = (struct rar *)(a->format->data);
  458. if (rar->bytes_remaining > 0) {
  459. br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
  460. if (br->next_in == NULL) {
  461. archive_set_error(&a->archive,
  462. ARCHIVE_ERRNO_FILE_FORMAT,
  463. "Truncated RAR file data");
  464. return (ARCHIVE_FATAL);
  465. }
  466. if (br->cache_avail == 0)
  467. (void)rar_br_fillup(a, br);
  468. }
  469. return (ARCHIVE_OK);
  470. }
  471. /* Find last bit set */
  472. static inline int
  473. rar_fls(unsigned int word)
  474. {
  475. word |= (word >> 1);
  476. word |= (word >> 2);
  477. word |= (word >> 4);
  478. word |= (word >> 8);
  479. word |= (word >> 16);
  480. return word - (word >> 1);
  481. }
  482. /* LZSS functions */
  483. static inline int64_t
  484. lzss_position(struct lzss *lzss)
  485. {
  486. return lzss->position;
  487. }
  488. static inline int
  489. lzss_mask(struct lzss *lzss)
  490. {
  491. return lzss->mask;
  492. }
  493. static inline int
  494. lzss_size(struct lzss *lzss)
  495. {
  496. return lzss->mask + 1;
  497. }
  498. static inline int
  499. lzss_offset_for_position(struct lzss *lzss, int64_t pos)
  500. {
  501. return (int)(pos & lzss->mask);
  502. }
  503. static inline unsigned char *
  504. lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
  505. {
  506. return &lzss->window[lzss_offset_for_position(lzss, pos)];
  507. }
  508. static inline int
  509. lzss_current_offset(struct lzss *lzss)
  510. {
  511. return lzss_offset_for_position(lzss, lzss->position);
  512. }
  513. static inline uint8_t *
  514. lzss_current_pointer(struct lzss *lzss)
  515. {
  516. return lzss_pointer_for_position(lzss, lzss->position);
  517. }
  518. static inline void
  519. lzss_emit_literal(struct rar *rar, uint8_t literal)
  520. {
  521. *lzss_current_pointer(&rar->lzss) = literal;
  522. rar->lzss.position++;
  523. }
  524. static inline void
  525. lzss_emit_match(struct rar *rar, int offset, int length)
  526. {
  527. int dstoffs = lzss_current_offset(&rar->lzss);
  528. int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
  529. int l, li, remaining;
  530. unsigned char *d, *s;
  531. remaining = length;
  532. while (remaining > 0) {
  533. l = remaining;
  534. if (dstoffs > srcoffs) {
  535. if (l > lzss_size(&rar->lzss) - dstoffs)
  536. l = lzss_size(&rar->lzss) - dstoffs;
  537. } else {
  538. if (l > lzss_size(&rar->lzss) - srcoffs)
  539. l = lzss_size(&rar->lzss) - srcoffs;
  540. }
  541. d = &(rar->lzss.window[dstoffs]);
  542. s = &(rar->lzss.window[srcoffs]);
  543. if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
  544. memcpy(d, s, l);
  545. else {
  546. for (li = 0; li < l; li++)
  547. d[li] = s[li];
  548. }
  549. remaining -= l;
  550. dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
  551. srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
  552. }
  553. rar->lzss.position += length;
  554. }
  555. static Byte
  556. ppmd_read(void *p)
  557. {
  558. struct archive_read *a = ((IByteIn*)p)->a;
  559. struct rar *rar = (struct rar *)(a->format->data);
  560. struct rar_br *br = &(rar->br);
  561. Byte b;
  562. if (!rar_br_read_ahead(a, br, 8))
  563. {
  564. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  565. "Truncated RAR file data");
  566. rar->valid = 0;
  567. return 0;
  568. }
  569. b = rar_br_bits(br, 8);
  570. rar_br_consume(br, 8);
  571. return b;
  572. }
  573. int
  574. archive_read_support_format_rar(struct archive *_a)
  575. {
  576. struct archive_read *a = (struct archive_read *)_a;
  577. struct rar *rar;
  578. int r;
  579. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  580. "archive_read_support_format_rar");
  581. rar = (struct rar *)calloc(sizeof(*rar), 1);
  582. if (rar == NULL)
  583. {
  584. archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
  585. return (ARCHIVE_FATAL);
  586. }
  587. /*
  588. * Until enough data has been read, we cannot tell about
  589. * any encrypted entries yet.
  590. */
  591. rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  592. r = __archive_read_register_format(a,
  593. rar,
  594. "rar",
  595. archive_read_format_rar_bid,
  596. archive_read_format_rar_options,
  597. archive_read_format_rar_read_header,
  598. archive_read_format_rar_read_data,
  599. archive_read_format_rar_read_data_skip,
  600. archive_read_format_rar_seek_data,
  601. archive_read_format_rar_cleanup,
  602. archive_read_support_format_rar_capabilities,
  603. archive_read_format_rar_has_encrypted_entries);
  604. if (r != ARCHIVE_OK)
  605. free(rar);
  606. return (r);
  607. }
  608. static int
  609. archive_read_support_format_rar_capabilities(struct archive_read * a)
  610. {
  611. (void)a; /* UNUSED */
  612. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
  613. | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  614. }
  615. static int
  616. archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
  617. {
  618. if (_a && _a->format) {
  619. struct rar * rar = (struct rar *)_a->format->data;
  620. if (rar) {
  621. return rar->has_encrypted_entries;
  622. }
  623. }
  624. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  625. }
  626. static int
  627. archive_read_format_rar_bid(struct archive_read *a, int best_bid)
  628. {
  629. const char *p;
  630. /* If there's already a bid > 30, we'll never win. */
  631. if (best_bid > 30)
  632. return (-1);
  633. if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
  634. return (-1);
  635. if (memcmp(p, RAR_SIGNATURE, 7) == 0)
  636. return (30);
  637. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  638. /* This is a PE file */
  639. ssize_t offset = 0x10000;
  640. ssize_t window = 4096;
  641. ssize_t bytes_avail;
  642. while (offset + window <= (1024 * 128)) {
  643. const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
  644. if (buff == NULL) {
  645. /* Remaining bytes are less than window. */
  646. window >>= 1;
  647. if (window < 0x40)
  648. return (0);
  649. continue;
  650. }
  651. p = buff + offset;
  652. while (p + 7 < buff + bytes_avail) {
  653. if (memcmp(p, RAR_SIGNATURE, 7) == 0)
  654. return (30);
  655. p += 0x10;
  656. }
  657. offset = p - buff;
  658. }
  659. }
  660. return (0);
  661. }
  662. static int
  663. skip_sfx(struct archive_read *a)
  664. {
  665. const void *h;
  666. const char *p, *q;
  667. size_t skip, total;
  668. ssize_t bytes, window;
  669. total = 0;
  670. window = 4096;
  671. while (total + window <= (1024 * 128)) {
  672. h = __archive_read_ahead(a, window, &bytes);
  673. if (h == NULL) {
  674. /* Remaining bytes are less than window. */
  675. window >>= 1;
  676. if (window < 0x40)
  677. goto fatal;
  678. continue;
  679. }
  680. if (bytes < 0x40)
  681. goto fatal;
  682. p = h;
  683. q = p + bytes;
  684. /*
  685. * Scan ahead until we find something that looks
  686. * like the RAR header.
  687. */
  688. while (p + 7 < q) {
  689. if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
  690. skip = p - (const char *)h;
  691. __archive_read_consume(a, skip);
  692. return (ARCHIVE_OK);
  693. }
  694. p += 0x10;
  695. }
  696. skip = p - (const char *)h;
  697. __archive_read_consume(a, skip);
  698. total += skip;
  699. }
  700. fatal:
  701. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  702. "Couldn't find out RAR header");
  703. return (ARCHIVE_FATAL);
  704. }
  705. static int
  706. archive_read_format_rar_options(struct archive_read *a,
  707. const char *key, const char *val)
  708. {
  709. struct rar *rar;
  710. int ret = ARCHIVE_FAILED;
  711. rar = (struct rar *)(a->format->data);
  712. if (strcmp(key, "hdrcharset") == 0) {
  713. if (val == NULL || val[0] == 0)
  714. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  715. "rar: hdrcharset option needs a character-set name");
  716. else {
  717. rar->opt_sconv =
  718. archive_string_conversion_from_charset(
  719. &a->archive, val, 0);
  720. if (rar->opt_sconv != NULL)
  721. ret = ARCHIVE_OK;
  722. else
  723. ret = ARCHIVE_FATAL;
  724. }
  725. return (ret);
  726. }
  727. /* Note: The "warn" return is just to inform the options
  728. * supervisor that we didn't handle it. It will generate
  729. * a suitable error if no one used this option. */
  730. return (ARCHIVE_WARN);
  731. }
  732. static int
  733. archive_read_format_rar_read_header(struct archive_read *a,
  734. struct archive_entry *entry)
  735. {
  736. const void *h;
  737. const char *p;
  738. struct rar *rar;
  739. size_t skip;
  740. char head_type;
  741. int ret;
  742. unsigned flags;
  743. unsigned long crc32_expected;
  744. a->archive.archive_format = ARCHIVE_FORMAT_RAR;
  745. if (a->archive.archive_format_name == NULL)
  746. a->archive.archive_format_name = "RAR";
  747. rar = (struct rar *)(a->format->data);
  748. /*
  749. * It should be sufficient to call archive_read_next_header() for
  750. * a reader to determine if an entry is encrypted or not. If the
  751. * encryption of an entry is only detectable when calling
  752. * archive_read_data(), so be it. We'll do the same check there
  753. * as well.
  754. */
  755. if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  756. rar->has_encrypted_entries = 0;
  757. }
  758. /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
  759. * this fails.
  760. */
  761. if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
  762. return (ARCHIVE_EOF);
  763. p = h;
  764. if (rar->found_first_header == 0 &&
  765. ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
  766. /* This is an executable ? Must be self-extracting... */
  767. ret = skip_sfx(a);
  768. if (ret < ARCHIVE_WARN)
  769. return (ret);
  770. }
  771. rar->found_first_header = 1;
  772. while (1)
  773. {
  774. unsigned long crc32_val;
  775. if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
  776. return (ARCHIVE_FATAL);
  777. p = h;
  778. head_type = p[2];
  779. switch(head_type)
  780. {
  781. case MARK_HEAD:
  782. if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
  783. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  784. "Invalid marker header");
  785. return (ARCHIVE_FATAL);
  786. }
  787. __archive_read_consume(a, 7);
  788. break;
  789. case MAIN_HEAD:
  790. rar->main_flags = archive_le16dec(p + 3);
  791. skip = archive_le16dec(p + 5);
  792. if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
  793. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  794. "Invalid header size");
  795. return (ARCHIVE_FATAL);
  796. }
  797. if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
  798. return (ARCHIVE_FATAL);
  799. p = h;
  800. memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
  801. memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
  802. sizeof(rar->reserved2));
  803. if (rar->main_flags & MHD_ENCRYPTVER) {
  804. if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
  805. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  806. "Invalid header size");
  807. return (ARCHIVE_FATAL);
  808. }
  809. rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
  810. sizeof(rar->reserved2));
  811. }
  812. /* Main header is password encrypted, so we cannot read any
  813. file names or any other info about files from the header. */
  814. if (rar->main_flags & MHD_PASSWORD)
  815. {
  816. archive_entry_set_is_metadata_encrypted(entry, 1);
  817. archive_entry_set_is_data_encrypted(entry, 1);
  818. rar->has_encrypted_entries = 1;
  819. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  820. "RAR encryption support unavailable.");
  821. return (ARCHIVE_FATAL);
  822. }
  823. crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
  824. if ((crc32_val & 0xffff) != archive_le16dec(p)) {
  825. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  826. "Header CRC error");
  827. return (ARCHIVE_FATAL);
  828. }
  829. __archive_read_consume(a, skip);
  830. break;
  831. case FILE_HEAD:
  832. return read_header(a, entry, head_type);
  833. case COMM_HEAD:
  834. case AV_HEAD:
  835. case SUB_HEAD:
  836. case PROTECT_HEAD:
  837. case SIGN_HEAD:
  838. case ENDARC_HEAD:
  839. flags = archive_le16dec(p + 3);
  840. skip = archive_le16dec(p + 5);
  841. if (skip < 7) {
  842. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  843. "Invalid header size too small");
  844. return (ARCHIVE_FATAL);
  845. }
  846. if (flags & HD_ADD_SIZE_PRESENT)
  847. {
  848. if (skip < 7 + 4) {
  849. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  850. "Invalid header size too small");
  851. return (ARCHIVE_FATAL);
  852. }
  853. if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
  854. return (ARCHIVE_FATAL);
  855. p = h;
  856. skip += archive_le32dec(p + 7);
  857. }
  858. /* Skip over the 2-byte CRC at the beginning of the header. */
  859. crc32_expected = archive_le16dec(p);
  860. __archive_read_consume(a, 2);
  861. skip -= 2;
  862. /* Skim the entire header and compute the CRC. */
  863. crc32_val = 0;
  864. while (skip > 0) {
  865. size_t to_read = skip;
  866. ssize_t did_read;
  867. if (to_read > 32 * 1024) {
  868. to_read = 32 * 1024;
  869. }
  870. if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
  871. return (ARCHIVE_FATAL);
  872. }
  873. p = h;
  874. crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
  875. __archive_read_consume(a, did_read);
  876. skip -= did_read;
  877. }
  878. if ((crc32_val & 0xffff) != crc32_expected) {
  879. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  880. "Header CRC error");
  881. return (ARCHIVE_FATAL);
  882. }
  883. if (head_type == ENDARC_HEAD)
  884. return (ARCHIVE_EOF);
  885. break;
  886. case NEWSUB_HEAD:
  887. if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
  888. return ret;
  889. break;
  890. default:
  891. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  892. "Bad RAR file");
  893. return (ARCHIVE_FATAL);
  894. }
  895. }
  896. }
  897. static int
  898. archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
  899. size_t *size, int64_t *offset)
  900. {
  901. struct rar *rar = (struct rar *)(a->format->data);
  902. int ret;
  903. if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  904. rar->has_encrypted_entries = 0;
  905. }
  906. if (rar->bytes_unconsumed > 0) {
  907. /* Consume as much as the decompressor actually used. */
  908. __archive_read_consume(a, rar->bytes_unconsumed);
  909. rar->bytes_unconsumed = 0;
  910. }
  911. *buff = NULL;
  912. if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
  913. *size = 0;
  914. *offset = rar->offset;
  915. if (*offset < rar->unp_size)
  916. *offset = rar->unp_size;
  917. return (ARCHIVE_EOF);
  918. }
  919. switch (rar->compression_method)
  920. {
  921. case COMPRESS_METHOD_STORE:
  922. ret = read_data_stored(a, buff, size, offset);
  923. break;
  924. case COMPRESS_METHOD_FASTEST:
  925. case COMPRESS_METHOD_FAST:
  926. case COMPRESS_METHOD_NORMAL:
  927. case COMPRESS_METHOD_GOOD:
  928. case COMPRESS_METHOD_BEST:
  929. ret = read_data_compressed(a, buff, size, offset);
  930. if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN) {
  931. __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
  932. rar->start_new_table = 1;
  933. rar->ppmd_valid = 0;
  934. }
  935. break;
  936. default:
  937. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  938. "Unsupported compression method for RAR file.");
  939. ret = ARCHIVE_FATAL;
  940. break;
  941. }
  942. return (ret);
  943. }
  944. static int
  945. archive_read_format_rar_read_data_skip(struct archive_read *a)
  946. {
  947. struct rar *rar;
  948. int64_t bytes_skipped;
  949. int ret;
  950. rar = (struct rar *)(a->format->data);
  951. if (rar->bytes_unconsumed > 0) {
  952. /* Consume as much as the decompressor actually used. */
  953. __archive_read_consume(a, rar->bytes_unconsumed);
  954. rar->bytes_unconsumed = 0;
  955. }
  956. if (rar->bytes_remaining > 0) {
  957. bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
  958. if (bytes_skipped < 0)
  959. return (ARCHIVE_FATAL);
  960. }
  961. /* Compressed data to skip must be read from each header in a multivolume
  962. * archive.
  963. */
  964. if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
  965. {
  966. ret = archive_read_format_rar_read_header(a, a->entry);
  967. if (ret == (ARCHIVE_EOF))
  968. ret = archive_read_format_rar_read_header(a, a->entry);
  969. if (ret != (ARCHIVE_OK))
  970. return ret;
  971. return archive_read_format_rar_read_data_skip(a);
  972. }
  973. return (ARCHIVE_OK);
  974. }
  975. static int64_t
  976. archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
  977. int whence)
  978. {
  979. int64_t client_offset, ret;
  980. unsigned int i;
  981. struct rar *rar = (struct rar *)(a->format->data);
  982. if (rar->compression_method == COMPRESS_METHOD_STORE)
  983. {
  984. /* Modify the offset for use with SEEK_SET */
  985. switch (whence)
  986. {
  987. case SEEK_CUR:
  988. client_offset = rar->offset_seek;
  989. break;
  990. case SEEK_END:
  991. client_offset = rar->unp_size;
  992. break;
  993. case SEEK_SET:
  994. default:
  995. client_offset = 0;
  996. }
  997. client_offset += offset;
  998. if (client_offset < 0)
  999. {
  1000. /* Can't seek past beginning of data block */
  1001. return -1;
  1002. }
  1003. else if (client_offset > rar->unp_size)
  1004. {
  1005. /*
  1006. * Set the returned offset but only seek to the end of
  1007. * the data block.
  1008. */
  1009. rar->offset_seek = client_offset;
  1010. client_offset = rar->unp_size;
  1011. }
  1012. client_offset += rar->dbo[0].start_offset;
  1013. i = 0;
  1014. while (i < rar->cursor)
  1015. {
  1016. i++;
  1017. client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
  1018. }
  1019. if (rar->main_flags & MHD_VOLUME)
  1020. {
  1021. /* Find the appropriate offset among the multivolume archive */
  1022. while (1)
  1023. {
  1024. if (client_offset < rar->dbo[rar->cursor].start_offset &&
  1025. rar->file_flags & FHD_SPLIT_BEFORE)
  1026. {
  1027. /* Search backwards for the correct data block */
  1028. if (rar->cursor == 0)
  1029. {
  1030. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1031. "Attempt to seek past beginning of RAR data block");
  1032. return (ARCHIVE_FAILED);
  1033. }
  1034. rar->cursor--;
  1035. client_offset -= rar->dbo[rar->cursor+1].start_offset -
  1036. rar->dbo[rar->cursor].end_offset;
  1037. if (client_offset < rar->dbo[rar->cursor].start_offset)
  1038. continue;
  1039. ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
  1040. rar->dbo[rar->cursor].header_size, SEEK_SET);
  1041. if (ret < (ARCHIVE_OK))
  1042. return ret;
  1043. ret = archive_read_format_rar_read_header(a, a->entry);
  1044. if (ret != (ARCHIVE_OK))
  1045. {
  1046. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1047. "Error during seek of RAR file");
  1048. return (ARCHIVE_FAILED);
  1049. }
  1050. rar->cursor--;
  1051. break;
  1052. }
  1053. else if (client_offset > rar->dbo[rar->cursor].end_offset &&
  1054. rar->file_flags & FHD_SPLIT_AFTER)
  1055. {
  1056. /* Search forward for the correct data block */
  1057. rar->cursor++;
  1058. if (rar->cursor < rar->nodes &&
  1059. client_offset > rar->dbo[rar->cursor].end_offset)
  1060. {
  1061. client_offset += rar->dbo[rar->cursor].start_offset -
  1062. rar->dbo[rar->cursor-1].end_offset;
  1063. continue;
  1064. }
  1065. rar->cursor--;
  1066. ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
  1067. SEEK_SET);
  1068. if (ret < (ARCHIVE_OK))
  1069. return ret;
  1070. ret = archive_read_format_rar_read_header(a, a->entry);
  1071. if (ret == (ARCHIVE_EOF))
  1072. {
  1073. rar->has_endarc_header = 1;
  1074. ret = archive_read_format_rar_read_header(a, a->entry);
  1075. }
  1076. if (ret != (ARCHIVE_OK))
  1077. {
  1078. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1079. "Error during seek of RAR file");
  1080. return (ARCHIVE_FAILED);
  1081. }
  1082. client_offset += rar->dbo[rar->cursor].start_offset -
  1083. rar->dbo[rar->cursor-1].end_offset;
  1084. continue;
  1085. }
  1086. break;
  1087. }
  1088. }
  1089. ret = __archive_read_seek(a, client_offset, SEEK_SET);
  1090. if (ret < (ARCHIVE_OK))
  1091. return ret;
  1092. rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
  1093. i = rar->cursor;
  1094. while (i > 0)
  1095. {
  1096. i--;
  1097. ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
  1098. }
  1099. ret -= rar->dbo[0].start_offset;
  1100. /* Always restart reading the file after a seek */
  1101. __archive_reset_read_data(&a->archive);
  1102. rar->bytes_unconsumed = 0;
  1103. rar->offset = 0;
  1104. /*
  1105. * If a seek past the end of file was requested, return the requested
  1106. * offset.
  1107. */
  1108. if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
  1109. return rar->offset_seek;
  1110. /* Return the new offset */
  1111. rar->offset_seek = ret;
  1112. return rar->offset_seek;
  1113. }
  1114. else
  1115. {
  1116. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1117. "Seeking of compressed RAR files is unsupported");
  1118. }
  1119. return (ARCHIVE_FAILED);
  1120. }
  1121. static int
  1122. archive_read_format_rar_cleanup(struct archive_read *a)
  1123. {
  1124. struct rar *rar;
  1125. rar = (struct rar *)(a->format->data);
  1126. free_codes(a);
  1127. free(rar->filename);
  1128. free(rar->filename_save);
  1129. free(rar->dbo);
  1130. free(rar->unp_buffer);
  1131. free(rar->lzss.window);
  1132. __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
  1133. free(rar);
  1134. (a->format->data) = NULL;
  1135. return (ARCHIVE_OK);
  1136. }
  1137. static int
  1138. read_header(struct archive_read *a, struct archive_entry *entry,
  1139. char head_type)
  1140. {
  1141. const void *h;
  1142. const char *p, *endp;
  1143. struct rar *rar;
  1144. struct rar_header rar_header;
  1145. struct rar_file_header file_header;
  1146. int64_t header_size;
  1147. unsigned filename_size, end;
  1148. char *filename;
  1149. char *strp;
  1150. char packed_size[8];
  1151. char unp_size[8];
  1152. int ttime;
  1153. struct archive_string_conv *sconv, *fn_sconv;
  1154. unsigned long crc32_val;
  1155. int ret = (ARCHIVE_OK), ret2;
  1156. rar = (struct rar *)(a->format->data);
  1157. /* Setup a string conversion object for non-rar-unicode filenames. */
  1158. sconv = rar->opt_sconv;
  1159. if (sconv == NULL) {
  1160. if (!rar->init_default_conversion) {
  1161. rar->sconv_default =
  1162. archive_string_default_conversion_for_read(
  1163. &(a->archive));
  1164. rar->init_default_conversion = 1;
  1165. }
  1166. sconv = rar->sconv_default;
  1167. }
  1168. if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
  1169. return (ARCHIVE_FATAL);
  1170. p = h;
  1171. memcpy(&rar_header, p, sizeof(rar_header));
  1172. rar->file_flags = archive_le16dec(rar_header.flags);
  1173. header_size = archive_le16dec(rar_header.size);
  1174. if (header_size < (int64_t)sizeof(file_header) + 7) {
  1175. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1176. "Invalid header size");
  1177. return (ARCHIVE_FATAL);
  1178. }
  1179. crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
  1180. __archive_read_consume(a, 7);
  1181. if (!(rar->file_flags & FHD_SOLID))
  1182. {
  1183. rar->compression_method = 0;
  1184. rar->packed_size = 0;
  1185. rar->unp_size = 0;
  1186. rar->mtime = 0;
  1187. rar->ctime = 0;
  1188. rar->atime = 0;
  1189. rar->arctime = 0;
  1190. rar->mode = 0;
  1191. memset(&rar->salt, 0, sizeof(rar->salt));
  1192. rar->atime = 0;
  1193. rar->ansec = 0;
  1194. rar->ctime = 0;
  1195. rar->cnsec = 0;
  1196. rar->mtime = 0;
  1197. rar->mnsec = 0;
  1198. rar->arctime = 0;
  1199. rar->arcnsec = 0;
  1200. }
  1201. else
  1202. {
  1203. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1204. "RAR solid archive support unavailable.");
  1205. return (ARCHIVE_FATAL);
  1206. }
  1207. if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
  1208. return (ARCHIVE_FATAL);
  1209. /* File Header CRC check. */
  1210. crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
  1211. if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
  1212. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1213. "Header CRC error");
  1214. return (ARCHIVE_FATAL);
  1215. }
  1216. /* If no CRC error, Go on parsing File Header. */
  1217. p = h;
  1218. endp = p + header_size - 7;
  1219. memcpy(&file_header, p, sizeof(file_header));
  1220. p += sizeof(file_header);
  1221. rar->compression_method = file_header.method;
  1222. ttime = archive_le32dec(file_header.file_time);
  1223. rar->mtime = get_time(ttime);
  1224. rar->file_crc = archive_le32dec(file_header.file_crc);
  1225. if (rar->file_flags & FHD_PASSWORD)
  1226. {
  1227. archive_entry_set_is_data_encrypted(entry, 1);
  1228. rar->has_encrypted_entries = 1;
  1229. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1230. "RAR encryption support unavailable.");
  1231. /* Since it is only the data part itself that is encrypted we can at least
  1232. extract information about the currently processed entry and don't need
  1233. to return ARCHIVE_FATAL here. */
  1234. /*return (ARCHIVE_FATAL);*/
  1235. }
  1236. if (rar->file_flags & FHD_LARGE)
  1237. {
  1238. memcpy(packed_size, file_header.pack_size, 4);
  1239. memcpy(packed_size + 4, p, 4); /* High pack size */
  1240. p += 4;
  1241. memcpy(unp_size, file_header.unp_size, 4);
  1242. memcpy(unp_size + 4, p, 4); /* High unpack size */
  1243. p += 4;
  1244. rar->packed_size = archive_le64dec(&packed_size);
  1245. rar->unp_size = archive_le64dec(&unp_size);
  1246. }
  1247. else
  1248. {
  1249. rar->packed_size = archive_le32dec(file_header.pack_size);
  1250. rar->unp_size = archive_le32dec(file_header.unp_size);
  1251. }
  1252. if (rar->packed_size < 0 || rar->unp_size < 0)
  1253. {
  1254. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1255. "Invalid sizes specified.");
  1256. return (ARCHIVE_FATAL);
  1257. }
  1258. rar->bytes_remaining = rar->packed_size;
  1259. /* TODO: RARv3 subblocks contain comments. For now the complete block is
  1260. * consumed at the end.
  1261. */
  1262. if (head_type == NEWSUB_HEAD) {
  1263. size_t distance = p - (const char *)h;
  1264. header_size += rar->packed_size;
  1265. /* Make sure we have the extended data. */
  1266. if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
  1267. return (ARCHIVE_FATAL);
  1268. p = h;
  1269. endp = p + header_size - 7;
  1270. p += distance;
  1271. }
  1272. filename_size = archive_le16dec(file_header.name_size);
  1273. if (p + filename_size > endp) {
  1274. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1275. "Invalid filename size");
  1276. return (ARCHIVE_FATAL);
  1277. }
  1278. if (rar->filename_allocated < filename_size * 2 + 2) {
  1279. char *newptr;
  1280. size_t newsize = filename_size * 2 + 2;
  1281. newptr = realloc(rar->filename, newsize);
  1282. if (newptr == NULL) {
  1283. archive_set_error(&a->archive, ENOMEM,
  1284. "Couldn't allocate memory.");
  1285. return (ARCHIVE_FATAL);
  1286. }
  1287. rar->filename = newptr;
  1288. rar->filename_allocated = newsize;
  1289. }
  1290. filename = rar->filename;
  1291. memcpy(filename, p, filename_size);
  1292. filename[filename_size] = '\0';
  1293. if (rar->file_flags & FHD_UNICODE)
  1294. {
  1295. if (filename_size != strlen(filename))
  1296. {
  1297. unsigned char highbyte, flagbits, flagbyte;
  1298. unsigned fn_end, offset;
  1299. end = filename_size;
  1300. fn_end = filename_size * 2;
  1301. filename_size = 0;
  1302. offset = (unsigned)strlen(filename) + 1;
  1303. highbyte = *(p + offset++);
  1304. flagbits = 0;
  1305. flagbyte = 0;
  1306. while (offset < end && filename_size < fn_end)
  1307. {
  1308. if (!flagbits)
  1309. {
  1310. flagbyte = *(p + offset++);
  1311. flagbits = 8;
  1312. }
  1313. flagbits -= 2;
  1314. switch((flagbyte >> flagbits) & 3)
  1315. {
  1316. case 0:
  1317. filename[filename_size++] = '\0';
  1318. filename[filename_size++] = *(p + offset++);
  1319. break;
  1320. case 1:
  1321. filename[filename_size++] = highbyte;
  1322. filename[filename_size++] = *(p + offset++);
  1323. break;
  1324. case 2:
  1325. filename[filename_size++] = *(p + offset + 1);
  1326. filename[filename_size++] = *(p + offset);
  1327. offset += 2;
  1328. break;
  1329. case 3:
  1330. {
  1331. char extra, high;
  1332. uint8_t length = *(p + offset++);
  1333. if (length & 0x80) {
  1334. extra = *(p + offset++);
  1335. high = (char)highbyte;
  1336. } else
  1337. extra = high = 0;
  1338. length = (length & 0x7f) + 2;
  1339. while (length && filename_size < fn_end) {
  1340. unsigned cp = filename_size >> 1;
  1341. filename[filename_size++] = high;
  1342. filename[filename_size++] = p[cp] + extra;
  1343. length--;
  1344. }
  1345. }
  1346. break;
  1347. }
  1348. }
  1349. if (filename_size > fn_end) {
  1350. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1351. "Invalid filename");
  1352. return (ARCHIVE_FATAL);
  1353. }
  1354. filename[filename_size++] = '\0';
  1355. /*
  1356. * Do not increment filename_size here as the computations below
  1357. * add the space for the terminating NUL explicitly.
  1358. */
  1359. filename[filename_size] = '\0';
  1360. /* Decoded unicode form is UTF-16BE, so we have to update a string
  1361. * conversion object for it. */
  1362. if (rar->sconv_utf16be == NULL) {
  1363. rar->sconv_utf16be = archive_string_conversion_from_charset(
  1364. &a->archive, "UTF-16BE", 1);
  1365. if (rar->sconv_utf16be == NULL)
  1366. return (ARCHIVE_FATAL);
  1367. }
  1368. fn_sconv = rar->sconv_utf16be;
  1369. strp = filename;
  1370. while (memcmp(strp, "\x00\x00", 2))
  1371. {
  1372. if (!memcmp(strp, "\x00\\", 2))
  1373. *(strp + 1) = '/';
  1374. strp += 2;
  1375. }
  1376. p += offset;
  1377. } else {
  1378. /*
  1379. * If FHD_UNICODE is set but no unicode data, this file name form
  1380. * is UTF-8, so we have to update a string conversion object for
  1381. * it accordingly.
  1382. */
  1383. if (rar->sconv_utf8 == NULL) {
  1384. rar->sconv_utf8 = archive_string_conversion_from_charset(
  1385. &a->archive, "UTF-8", 1);
  1386. if (rar->sconv_utf8 == NULL)
  1387. return (ARCHIVE_FATAL);
  1388. }
  1389. fn_sconv = rar->sconv_utf8;
  1390. while ((strp = strchr(filename, '\\')) != NULL)
  1391. *strp = '/';
  1392. p += filename_size;
  1393. }
  1394. }
  1395. else
  1396. {
  1397. fn_sconv = sconv;
  1398. while ((strp = strchr(filename, '\\')) != NULL)
  1399. *strp = '/';
  1400. p += filename_size;
  1401. }
  1402. /* Split file in multivolume RAR. No more need to process header. */
  1403. if (rar->filename_save &&
  1404. filename_size == rar->filename_save_size &&
  1405. !memcmp(rar->filename, rar->filename_save, filename_size + 1))
  1406. {
  1407. __archive_read_consume(a, header_size - 7);
  1408. rar->cursor++;
  1409. if (rar->cursor >= rar->nodes)
  1410. {
  1411. rar->nodes++;
  1412. if ((rar->dbo =
  1413. realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
  1414. {
  1415. archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
  1416. return (ARCHIVE_FATAL);
  1417. }
  1418. rar->dbo[rar->cursor].header_size = header_size;
  1419. rar->dbo[rar->cursor].start_offset = -1;
  1420. rar->dbo[rar->cursor].end_offset = -1;
  1421. }
  1422. if (rar->dbo[rar->cursor].start_offset < 0)
  1423. {
  1424. rar->dbo[rar->cursor].start_offset = a->filter->position;
  1425. rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
  1426. rar->packed_size;
  1427. }
  1428. return ret;
  1429. }
  1430. else if (rar->filename_must_match)
  1431. {
  1432. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1433. "Mismatch of file parts split across multi-volume archive");
  1434. return (ARCHIVE_FATAL);
  1435. }
  1436. rar->filename_save = (char*)realloc(rar->filename_save,
  1437. filename_size + 1);
  1438. memcpy(rar->filename_save, rar->filename, filename_size + 1);
  1439. rar->filename_save_size = filename_size;
  1440. /* Set info for seeking */
  1441. free(rar->dbo);
  1442. if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
  1443. {
  1444. archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
  1445. return (ARCHIVE_FATAL);
  1446. }
  1447. rar->dbo[0].header_size = header_size;
  1448. rar->dbo[0].start_offset = -1;
  1449. rar->dbo[0].end_offset = -1;
  1450. rar->cursor = 0;
  1451. rar->nodes = 1;
  1452. if (rar->file_flags & FHD_SALT)
  1453. {
  1454. if (p + 8 > endp) {
  1455. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1456. "Invalid header size");
  1457. return (ARCHIVE_FATAL);
  1458. }
  1459. memcpy(rar->salt, p, 8);
  1460. p += 8;
  1461. }
  1462. if (rar->file_flags & FHD_EXTTIME) {
  1463. if (read_exttime(p, rar, endp) < 0) {
  1464. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1465. "Invalid header size");
  1466. return (ARCHIVE_FATAL);
  1467. }
  1468. }
  1469. __archive_read_consume(a, header_size - 7);
  1470. rar->dbo[0].start_offset = a->filter->position;
  1471. rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
  1472. switch(file_header.host_os)
  1473. {
  1474. case OS_MSDOS:
  1475. case OS_OS2:
  1476. case OS_WIN32:
  1477. rar->mode = archive_le32dec(file_header.file_attr);
  1478. if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
  1479. rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  1480. else
  1481. rar->mode = AE_IFREG;
  1482. rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  1483. break;
  1484. case OS_UNIX:
  1485. case OS_MAC_OS:
  1486. case OS_BEOS:
  1487. rar->mode = archive_le32dec(file_header.file_attr);
  1488. break;
  1489. default:
  1490. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1491. "Unknown file attributes from RAR file's host OS");
  1492. return (ARCHIVE_FATAL);
  1493. }
  1494. rar->bytes_uncopied = rar->bytes_unconsumed = 0;
  1495. rar->lzss.position = rar->offset = 0;
  1496. rar->offset_seek = 0;
  1497. rar->dictionary_size = 0;
  1498. rar->offset_outgoing = 0;
  1499. rar->br.cache_avail = 0;
  1500. rar->br.avail_in = 0;
  1501. rar->crc_calculated = 0;
  1502. rar->entry_eof = 0;
  1503. rar->valid = 1;
  1504. rar->is_ppmd_block = 0;
  1505. rar->start_new_table = 1;
  1506. free(rar->unp_buffer);
  1507. rar->unp_buffer = NULL;
  1508. rar->unp_offset = 0;
  1509. rar->unp_buffer_size = UNP_BUFFER_SIZE;
  1510. memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
  1511. __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
  1512. rar->ppmd_valid = rar->ppmd_eod = 0;
  1513. /* Don't set any archive entries for non-file header types */
  1514. if (head_type == NEWSUB_HEAD)
  1515. return ret;
  1516. archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
  1517. archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
  1518. archive_entry_set_atime(entry, rar->atime, rar->ansec);
  1519. archive_entry_set_size(entry, rar->unp_size);
  1520. archive_entry_set_mode(entry, rar->mode);
  1521. if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
  1522. {
  1523. if (errno == ENOMEM)
  1524. {
  1525. archive_set_error(&a->archive, ENOMEM,
  1526. "Can't allocate memory for Pathname");
  1527. return (ARCHIVE_FATAL);
  1528. }
  1529. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1530. "Pathname cannot be converted from %s to current locale.",
  1531. archive_string_conversion_charset_name(fn_sconv));
  1532. ret = (ARCHIVE_WARN);
  1533. }
  1534. if (((rar->mode) & AE_IFMT) == AE_IFLNK)
  1535. {
  1536. /* Make sure a symbolic-link file does not have its body. */
  1537. rar->bytes_remaining = 0;
  1538. archive_entry_set_size(entry, 0);
  1539. /* Read a symbolic-link name. */
  1540. if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
  1541. return ret2;
  1542. if (ret > ret2)
  1543. ret = ret2;
  1544. }
  1545. if (rar->bytes_remaining == 0)
  1546. rar->entry_eof = 1;
  1547. return ret;
  1548. }
  1549. static time_t
  1550. get_time(int ttime)
  1551. {
  1552. struct tm tm;
  1553. tm.tm_sec = 2 * (ttime & 0x1f);
  1554. tm.tm_min = (ttime >> 5) & 0x3f;
  1555. tm.tm_hour = (ttime >> 11) & 0x1f;
  1556. tm.tm_mday = (ttime >> 16) & 0x1f;
  1557. tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
  1558. tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
  1559. tm.tm_isdst = -1;
  1560. return mktime(&tm);
  1561. }
  1562. static int
  1563. read_exttime(const char *p, struct rar *rar, const char *endp)
  1564. {
  1565. unsigned rmode, flags, rem, j, count;
  1566. int ttime, i;
  1567. struct tm *tm;
  1568. time_t t;
  1569. long nsec;
  1570. #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
  1571. struct tm tmbuf;
  1572. #endif
  1573. #if defined(HAVE__LOCALTIME64_S)
  1574. errno_t terr;
  1575. __time64_t tmptime;
  1576. #endif
  1577. if (p + 2 > endp)
  1578. return (-1);
  1579. flags = archive_le16dec(p);
  1580. p += 2;
  1581. for (i = 3; i >= 0; i--)
  1582. {
  1583. t = 0;
  1584. if (i == 3)
  1585. t = rar->mtime;
  1586. rmode = flags >> i * 4;
  1587. if (rmode & 8)
  1588. {
  1589. if (!t)
  1590. {
  1591. if (p + 4 > endp)
  1592. return (-1);
  1593. ttime = archive_le32dec(p);
  1594. t = get_time(ttime);
  1595. p += 4;
  1596. }
  1597. rem = 0;
  1598. count = rmode & 3;
  1599. if (p + count > endp)
  1600. return (-1);
  1601. for (j = 0; j < count; j++)
  1602. {
  1603. rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
  1604. p++;
  1605. }
  1606. #if defined(HAVE_LOCALTIME_R)
  1607. tm = localtime_r(&t, &tmbuf);
  1608. #elif defined(HAVE__LOCALTIME64_S)
  1609. tmptime = t;
  1610. terr = _localtime64_s(&tmbuf, &tmptime);
  1611. if (terr)
  1612. tm = NULL;
  1613. else
  1614. tm = &tmbuf;
  1615. #else
  1616. tm = localtime(&t);
  1617. #endif
  1618. nsec = tm->tm_sec + rem / NS_UNIT;
  1619. if (rmode & 4)
  1620. {
  1621. tm->tm_sec++;
  1622. t = mktime(tm);
  1623. }
  1624. if (i == 3)
  1625. {
  1626. rar->mtime = t;
  1627. rar->mnsec = nsec;
  1628. }
  1629. else if (i == 2)
  1630. {
  1631. rar->ctime = t;
  1632. rar->cnsec = nsec;
  1633. }
  1634. else if (i == 1)
  1635. {
  1636. rar->atime = t;
  1637. rar->ansec = nsec;
  1638. }
  1639. else
  1640. {
  1641. rar->arctime = t;
  1642. rar->arcnsec = nsec;
  1643. }
  1644. }
  1645. }
  1646. return (0);
  1647. }
  1648. static int
  1649. read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
  1650. struct archive_string_conv *sconv)
  1651. {
  1652. const void *h;
  1653. const char *p;
  1654. struct rar *rar;
  1655. int ret = (ARCHIVE_OK);
  1656. rar = (struct rar *)(a->format->data);
  1657. if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
  1658. return (ARCHIVE_FATAL);
  1659. p = h;
  1660. if (archive_entry_copy_symlink_l(entry,
  1661. p, (size_t)rar->packed_size, sconv))
  1662. {
  1663. if (errno == ENOMEM)
  1664. {
  1665. archive_set_error(&a->archive, ENOMEM,
  1666. "Can't allocate memory for link");
  1667. return (ARCHIVE_FATAL);
  1668. }
  1669. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1670. "link cannot be converted from %s to current locale.",
  1671. archive_string_conversion_charset_name(sconv));
  1672. ret = (ARCHIVE_WARN);
  1673. }
  1674. __archive_read_consume(a, rar->packed_size);
  1675. return ret;
  1676. }
  1677. static int
  1678. read_data_stored(struct archive_read *a, const void **buff, size_t *size,
  1679. int64_t *offset)
  1680. {
  1681. struct rar *rar;
  1682. ssize_t bytes_avail;
  1683. rar = (struct rar *)(a->format->data);
  1684. if (rar->bytes_remaining == 0 &&
  1685. !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
  1686. {
  1687. *buff = NULL;
  1688. *size = 0;
  1689. *offset = rar->offset;
  1690. if (rar->file_crc != rar->crc_calculated) {
  1691. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1692. "File CRC error");
  1693. return (ARCHIVE_FATAL);
  1694. }
  1695. rar->entry_eof = 1;
  1696. return (ARCHIVE_EOF);
  1697. }
  1698. *buff = rar_read_ahead(a, 1, &bytes_avail);
  1699. if (bytes_avail <= 0)
  1700. {
  1701. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1702. "Truncated RAR file data");
  1703. return (ARCHIVE_FATAL);
  1704. }
  1705. *size = bytes_avail;
  1706. *offset = rar->offset;
  1707. rar->offset += bytes_avail;
  1708. rar->offset_seek += bytes_avail;
  1709. rar->bytes_remaining -= bytes_avail;
  1710. rar->bytes_unconsumed = bytes_avail;
  1711. /* Calculate File CRC. */
  1712. rar->crc_calculated = crc32(rar->crc_calculated, *buff,
  1713. (unsigned)bytes_avail);
  1714. return (ARCHIVE_OK);
  1715. }
  1716. static int
  1717. read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
  1718. int64_t *offset)
  1719. {
  1720. struct rar *rar;
  1721. int64_t start, end, actualend;
  1722. size_t bs;
  1723. int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
  1724. rar = (struct rar *)(a->format->data);
  1725. do {
  1726. if (!rar->valid)
  1727. return (ARCHIVE_FATAL);
  1728. if (rar->ppmd_eod ||
  1729. (rar->dictionary_size && rar->offset >= rar->unp_size))
  1730. {
  1731. if (rar->unp_offset > 0) {
  1732. /*
  1733. * We have unprocessed extracted data. write it out.
  1734. */
  1735. *buff = rar->unp_buffer;
  1736. *size = rar->unp_offset;
  1737. *offset = rar->offset_outgoing;
  1738. rar->offset_outgoing += *size;
  1739. /* Calculate File CRC. */
  1740. rar->crc_calculated = crc32(rar->crc_calculated, *buff,
  1741. (unsigned)*size);
  1742. rar->unp_offset = 0;
  1743. return (ARCHIVE_OK);
  1744. }
  1745. *buff = NULL;
  1746. *size = 0;
  1747. *offset = rar->offset;
  1748. if (rar->file_crc != rar->crc_calculated) {
  1749. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1750. "File CRC error");
  1751. return (ARCHIVE_FATAL);
  1752. }
  1753. rar->entry_eof = 1;
  1754. return (ARCHIVE_EOF);
  1755. }
  1756. if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
  1757. {
  1758. if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
  1759. bs = rar->unp_buffer_size - rar->unp_offset;
  1760. else
  1761. bs = (size_t)rar->bytes_uncopied;
  1762. ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
  1763. if (ret != ARCHIVE_OK)
  1764. return (ret);
  1765. rar->offset += bs;
  1766. rar->bytes_uncopied -= bs;
  1767. if (*buff != NULL) {
  1768. rar->unp_offset = 0;
  1769. *size = rar->unp_buffer_size;
  1770. *offset = rar->offset_outgoing;
  1771. rar->offset_outgoing += *size;
  1772. /* Calculate File CRC. */
  1773. rar->crc_calculated = crc32(rar->crc_calculated, *buff,
  1774. (unsigned)*size);
  1775. return (ret);
  1776. }
  1777. continue;
  1778. }
  1779. if (!rar->br.next_in &&
  1780. (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
  1781. return (ret);
  1782. if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
  1783. return (ret);
  1784. if (rar->is_ppmd_block)
  1785. {
  1786. if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1787. &rar->ppmd7_context, &rar->range_dec.p)) < 0)
  1788. {
  1789. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1790. "Invalid symbol");
  1791. return (ARCHIVE_FATAL);
  1792. }
  1793. if(sym != rar->ppmd_escape)
  1794. {
  1795. lzss_emit_literal(rar, sym);
  1796. rar->bytes_uncopied++;
  1797. }
  1798. else
  1799. {
  1800. if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1801. &rar->ppmd7_context, &rar->range_dec.p)) < 0)
  1802. {
  1803. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1804. "Invalid symbol");
  1805. return (ARCHIVE_FATAL);
  1806. }
  1807. switch(code)
  1808. {
  1809. case 0:
  1810. rar->start_new_table = 1;
  1811. return read_data_compressed(a, buff, size, offset);
  1812. case 2:
  1813. rar->ppmd_eod = 1;/* End Of ppmd Data. */
  1814. continue;
  1815. case 3:
  1816. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1817. "Parsing filters is unsupported.");
  1818. return (ARCHIVE_FAILED);
  1819. case 4:
  1820. lzss_offset = 0;
  1821. for (i = 2; i >= 0; i--)
  1822. {
  1823. if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1824. &rar->ppmd7_context, &rar->range_dec.p)) < 0)
  1825. {
  1826. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1827. "Invalid symbol");
  1828. return (ARCHIVE_FATAL);
  1829. }
  1830. lzss_offset |= code << (i * 8);
  1831. }
  1832. if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1833. &rar->ppmd7_context, &rar->range_dec.p)) < 0)
  1834. {
  1835. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1836. "Invalid symbol");
  1837. return (ARCHIVE_FATAL);
  1838. }
  1839. lzss_emit_match(rar, lzss_offset + 2, length + 32);
  1840. rar->bytes_uncopied += length + 32;
  1841. break;
  1842. case 5:
  1843. if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1844. &rar->ppmd7_context, &rar->range_dec.p)) < 0)
  1845. {
  1846. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1847. "Invalid symbol");
  1848. return (ARCHIVE_FATAL);
  1849. }
  1850. lzss_emit_match(rar, 1, length + 4);
  1851. rar->bytes_uncopied += length + 4;
  1852. break;
  1853. default:
  1854. lzss_emit_literal(rar, sym);
  1855. rar->bytes_uncopied++;
  1856. }
  1857. }
  1858. }
  1859. else
  1860. {
  1861. start = rar->offset;
  1862. end = start + rar->dictionary_size;
  1863. rar->filterstart = INT64_MAX;
  1864. if ((actualend = expand(a, end)) < 0)
  1865. return ((int)actualend);
  1866. rar->bytes_uncopied = actualend - start;
  1867. if (rar->bytes_uncopied == 0) {
  1868. /* Broken RAR files cause this case.
  1869. * NOTE: If this case were possible on a normal RAR file
  1870. * we would find out where it was actually bad and
  1871. * what we would do to solve it. */
  1872. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1873. "Internal error extracting RAR file");
  1874. return (ARCHIVE_FATAL);
  1875. }
  1876. }
  1877. if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
  1878. bs = rar->unp_buffer_size - rar->unp_offset;
  1879. else
  1880. bs = (size_t)rar->bytes_uncopied;
  1881. ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
  1882. if (ret != ARCHIVE_OK)
  1883. return (ret);
  1884. rar->offset += bs;
  1885. rar->bytes_uncopied -= bs;
  1886. /*
  1887. * If *buff is NULL, it means unp_buffer is not full.
  1888. * So we have to continue extracting a RAR file.
  1889. */
  1890. } while (*buff == NULL);
  1891. rar->unp_offset = 0;
  1892. *size = rar->unp_buffer_size;
  1893. *offset = rar->offset_outgoing;
  1894. rar->offset_outgoing += *size;
  1895. /* Calculate File CRC. */
  1896. rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
  1897. return ret;
  1898. }
  1899. static int
  1900. parse_codes(struct archive_read *a)
  1901. {
  1902. int i, j, val, n, r;
  1903. unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
  1904. unsigned int maxorder;
  1905. struct huffman_code precode;
  1906. struct rar *rar = (struct rar *)(a->format->data);
  1907. struct rar_br *br = &(rar->br);
  1908. free_codes(a);
  1909. /* Skip to the next byte */
  1910. rar_br_consume_unalined_bits(br);
  1911. /* PPMd block flag */
  1912. if (!rar_br_read_ahead(a, br, 1))
  1913. goto truncated_data;
  1914. if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
  1915. {
  1916. rar_br_consume(br, 1);
  1917. if (!rar_br_read_ahead(a, br, 7))
  1918. goto truncated_data;
  1919. ppmd_flags = rar_br_bits(br, 7);
  1920. rar_br_consume(br, 7);
  1921. /* Memory is allocated in MB */
  1922. if (ppmd_flags & 0x20)
  1923. {
  1924. if (!rar_br_read_ahead(a, br, 8))
  1925. goto truncated_data;
  1926. rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
  1927. rar_br_consume(br, 8);
  1928. }
  1929. if (ppmd_flags & 0x40)
  1930. {
  1931. if (!rar_br_read_ahead(a, br, 8))
  1932. goto truncated_data;
  1933. rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
  1934. rar_br_consume(br, 8);
  1935. }
  1936. else
  1937. rar->ppmd_escape = 2;
  1938. if (ppmd_flags & 0x20)
  1939. {
  1940. maxorder = (ppmd_flags & 0x1F) + 1;
  1941. if(maxorder > 16)
  1942. maxorder = 16 + (maxorder - 16) * 3;
  1943. if (maxorder == 1)
  1944. {
  1945. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1946. "Truncated RAR file data");
  1947. return (ARCHIVE_FATAL);
  1948. }
  1949. /* Make sure ppmd7_contest is freed before Ppmd7_Construct
  1950. * because reading a broken file cause this abnormal sequence. */
  1951. __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
  1952. rar->bytein.a = a;
  1953. rar->bytein.Read = &ppmd_read;
  1954. __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
  1955. rar->range_dec.Stream = &rar->bytein;
  1956. __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
  1957. if (rar->dictionary_size == 0) {
  1958. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1959. "Invalid zero dictionary size");
  1960. return (ARCHIVE_FATAL);
  1961. }
  1962. if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
  1963. rar->dictionary_size))
  1964. {
  1965. archive_set_error(&a->archive, ENOMEM,
  1966. "Out of memory");
  1967. return (ARCHIVE_FATAL);
  1968. }
  1969. if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
  1970. {
  1971. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1972. "Unable to initialize PPMd range decoder");
  1973. return (ARCHIVE_FATAL);
  1974. }
  1975. __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
  1976. rar->ppmd_valid = 1;
  1977. }
  1978. else
  1979. {
  1980. if (!rar->ppmd_valid) {
  1981. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1982. "Invalid PPMd sequence");
  1983. return (ARCHIVE_FATAL);
  1984. }
  1985. if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
  1986. {
  1987. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1988. "Unable to initialize PPMd range decoder");
  1989. return (ARCHIVE_FATAL);
  1990. }
  1991. }
  1992. }
  1993. else
  1994. {
  1995. rar_br_consume(br, 1);
  1996. /* Keep existing table flag */
  1997. if (!rar_br_read_ahead(a, br, 1))
  1998. goto truncated_data;
  1999. if (!rar_br_bits(br, 1))
  2000. memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
  2001. rar_br_consume(br, 1);
  2002. memset(&bitlengths, 0, sizeof(bitlengths));
  2003. for (i = 0; i < MAX_SYMBOLS;)
  2004. {
  2005. if (!rar_br_read_ahead(a, br, 4))
  2006. goto truncated_data;
  2007. bitlengths[i++] = rar_br_bits(br, 4);
  2008. rar_br_consume(br, 4);
  2009. if (bitlengths[i-1] == 0xF)
  2010. {
  2011. if (!rar_br_read_ahead(a, br, 4))
  2012. goto truncated_data;
  2013. zerocount = rar_br_bits(br, 4);
  2014. rar_br_consume(br, 4);
  2015. if (zerocount)
  2016. {
  2017. i--;
  2018. for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
  2019. bitlengths[i++] = 0;
  2020. }
  2021. }
  2022. }
  2023. memset(&precode, 0, sizeof(precode));
  2024. r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
  2025. if (r != ARCHIVE_OK) {
  2026. free(precode.tree);
  2027. free(precode.table);
  2028. return (r);
  2029. }
  2030. for (i = 0; i < HUFFMAN_TABLE_SIZE;)
  2031. {
  2032. if ((val = read_next_symbol(a, &precode)) < 0) {
  2033. free(precode.tree);
  2034. free(precode.table);
  2035. return (ARCHIVE_FATAL);
  2036. }
  2037. if (val < 16)
  2038. {
  2039. rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
  2040. i++;
  2041. }
  2042. else if (val < 18)
  2043. {
  2044. if (i == 0)
  2045. {
  2046. free(precode.tree);
  2047. free(precode.table);
  2048. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2049. "Internal error extracting RAR file.");
  2050. return (ARCHIVE_FATAL);
  2051. }
  2052. if(val == 16) {
  2053. if (!rar_br_read_ahead(a, br, 3)) {
  2054. free(precode.tree);
  2055. free(precode.table);
  2056. goto truncated_data;
  2057. }
  2058. n = rar_br_bits(br, 3) + 3;
  2059. rar_br_consume(br, 3);
  2060. } else {
  2061. if (!rar_br_read_ahead(a, br, 7)) {
  2062. free(precode.tree);
  2063. free(precode.table);
  2064. goto truncated_data;
  2065. }
  2066. n = rar_br_bits(br, 7) + 11;
  2067. rar_br_consume(br, 7);
  2068. }
  2069. for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
  2070. {
  2071. rar->lengthtable[i] = rar->lengthtable[i-1];
  2072. i++;
  2073. }
  2074. }
  2075. else
  2076. {
  2077. if(val == 18) {
  2078. if (!rar_br_read_ahead(a, br, 3)) {
  2079. free(precode.tree);
  2080. free(precode.table);
  2081. goto truncated_data;
  2082. }
  2083. n = rar_br_bits(br, 3) + 3;
  2084. rar_br_consume(br, 3);
  2085. } else {
  2086. if (!rar_br_read_ahead(a, br, 7)) {
  2087. free(precode.tree);
  2088. free(precode.table);
  2089. goto truncated_data;
  2090. }
  2091. n = rar_br_bits(br, 7) + 11;
  2092. rar_br_consume(br, 7);
  2093. }
  2094. for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
  2095. rar->lengthtable[i++] = 0;
  2096. }
  2097. }
  2098. free(precode.tree);
  2099. free(precode.table);
  2100. r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
  2101. MAX_SYMBOL_LENGTH);
  2102. if (r != ARCHIVE_OK)
  2103. return (r);
  2104. r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
  2105. OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
  2106. if (r != ARCHIVE_OK)
  2107. return (r);
  2108. r = create_code(a, &rar->lowoffsetcode,
  2109. &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
  2110. LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
  2111. if (r != ARCHIVE_OK)
  2112. return (r);
  2113. r = create_code(a, &rar->lengthcode,
  2114. &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
  2115. LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
  2116. if (r != ARCHIVE_OK)
  2117. return (r);
  2118. }
  2119. if (!rar->dictionary_size || !rar->lzss.window)
  2120. {
  2121. /* Seems as though dictionary sizes are not used. Even so, minimize
  2122. * memory usage as much as possible.
  2123. */
  2124. void *new_window;
  2125. unsigned int new_size;
  2126. if (rar->unp_size >= DICTIONARY_MAX_SIZE)
  2127. new_size = DICTIONARY_MAX_SIZE;
  2128. else
  2129. new_size = rar_fls((unsigned int)rar->unp_size) << 1;
  2130. if (new_size == 0) {
  2131. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2132. "Zero window size is invalid.");
  2133. return (ARCHIVE_FATAL);
  2134. }
  2135. new_window = realloc(rar->lzss.window, new_size);
  2136. if (new_window == NULL) {
  2137. archive_set_error(&a->archive, ENOMEM,
  2138. "Unable to allocate memory for uncompressed data.");
  2139. return (ARCHIVE_FATAL);
  2140. }
  2141. rar->lzss.window = (unsigned char *)new_window;
  2142. rar->dictionary_size = new_size;
  2143. memset(rar->lzss.window, 0, rar->dictionary_size);
  2144. rar->lzss.mask = rar->dictionary_size - 1;
  2145. }
  2146. rar->start_new_table = 0;
  2147. return (ARCHIVE_OK);
  2148. truncated_data:
  2149. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2150. "Truncated RAR file data");
  2151. rar->valid = 0;
  2152. return (ARCHIVE_FATAL);
  2153. }
  2154. static void
  2155. free_codes(struct archive_read *a)
  2156. {
  2157. struct rar *rar = (struct rar *)(a->format->data);
  2158. free(rar->maincode.tree);
  2159. free(rar->offsetcode.tree);
  2160. free(rar->lowoffsetcode.tree);
  2161. free(rar->lengthcode.tree);
  2162. free(rar->maincode.table);
  2163. free(rar->offsetcode.table);
  2164. free(rar->lowoffsetcode.table);
  2165. free(rar->lengthcode.table);
  2166. memset(&rar->maincode, 0, sizeof(rar->maincode));
  2167. memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
  2168. memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
  2169. memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
  2170. }
  2171. static int
  2172. read_next_symbol(struct archive_read *a, struct huffman_code *code)
  2173. {
  2174. unsigned char bit;
  2175. unsigned int bits;
  2176. int length, value, node;
  2177. struct rar *rar;
  2178. struct rar_br *br;
  2179. if (!code->table)
  2180. {
  2181. if (make_table(a, code) != (ARCHIVE_OK))
  2182. return -1;
  2183. }
  2184. rar = (struct rar *)(a->format->data);
  2185. br = &(rar->br);
  2186. /* Look ahead (peek) at bits */
  2187. if (!rar_br_read_ahead(a, br, code->tablesize)) {
  2188. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2189. "Truncated RAR file data");
  2190. rar->valid = 0;
  2191. return -1;
  2192. }
  2193. bits = rar_br_bits(br, code->tablesize);
  2194. length = code->table[bits].length;
  2195. value = code->table[bits].value;
  2196. if (length < 0)
  2197. {
  2198. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2199. "Invalid prefix code in bitstream");
  2200. return -1;
  2201. }
  2202. if (length <= code->tablesize)
  2203. {
  2204. /* Skip length bits */
  2205. rar_br_consume(br, length);
  2206. return value;
  2207. }
  2208. /* Skip tablesize bits */
  2209. rar_br_consume(br, code->tablesize);
  2210. node = value;
  2211. while (!(code->tree[node].branches[0] ==
  2212. code->tree[node].branches[1]))
  2213. {
  2214. if (!rar_br_read_ahead(a, br, 1)) {
  2215. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2216. "Truncated RAR file data");
  2217. rar->valid = 0;
  2218. return -1;
  2219. }
  2220. bit = rar_br_bits(br, 1);
  2221. rar_br_consume(br, 1);
  2222. if (code->tree[node].branches[bit] < 0)
  2223. {
  2224. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2225. "Invalid prefix code in bitstream");
  2226. return -1;
  2227. }
  2228. node = code->tree[node].branches[bit];
  2229. }
  2230. return code->tree[node].branches[0];
  2231. }
  2232. static int
  2233. create_code(struct archive_read *a, struct huffman_code *code,
  2234. unsigned char *lengths, int numsymbols, char maxlength)
  2235. {
  2236. int i, j, codebits = 0, symbolsleft = numsymbols;
  2237. code->numentries = 0;
  2238. code->numallocatedentries = 0;
  2239. if (new_node(code) < 0) {
  2240. archive_set_error(&a->archive, ENOMEM,
  2241. "Unable to allocate memory for node data.");
  2242. return (ARCHIVE_FATAL);
  2243. }
  2244. code->numentries = 1;
  2245. code->minlength = INT_MAX;
  2246. code->maxlength = INT_MIN;
  2247. codebits = 0;
  2248. for(i = 1; i <= maxlength; i++)
  2249. {
  2250. for(j = 0; j < numsymbols; j++)
  2251. {
  2252. if (lengths[j] != i) continue;
  2253. if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
  2254. return (ARCHIVE_FATAL);
  2255. codebits++;
  2256. if (--symbolsleft <= 0)
  2257. break;
  2258. }
  2259. if (symbolsleft <= 0)
  2260. break;
  2261. codebits <<= 1;
  2262. }
  2263. return (ARCHIVE_OK);
  2264. }
  2265. static int
  2266. add_value(struct archive_read *a, struct huffman_code *code, int value,
  2267. int codebits, int length)
  2268. {
  2269. int lastnode, bitpos, bit;
  2270. /* int repeatpos, repeatnode, nextnode; */
  2271. free(code->table);
  2272. code->table = NULL;
  2273. if(length > code->maxlength)
  2274. code->maxlength = length;
  2275. if(length < code->minlength)
  2276. code->minlength = length;
  2277. /*
  2278. * Dead code, repeatpos was is -1
  2279. *
  2280. repeatpos = -1;
  2281. if (repeatpos == 0 || (repeatpos >= 0
  2282. && (((codebits >> (repeatpos - 1)) & 3) == 0
  2283. || ((codebits >> (repeatpos - 1)) & 3) == 3)))
  2284. {
  2285. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2286. "Invalid repeat position");
  2287. return (ARCHIVE_FATAL);
  2288. }
  2289. */
  2290. lastnode = 0;
  2291. for (bitpos = length - 1; bitpos >= 0; bitpos--)
  2292. {
  2293. bit = (codebits >> bitpos) & 1;
  2294. /* Leaf node check */
  2295. if (code->tree[lastnode].branches[0] ==
  2296. code->tree[lastnode].branches[1])
  2297. {
  2298. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2299. "Prefix found");
  2300. return (ARCHIVE_FATAL);
  2301. }
  2302. /*
  2303. * Dead code, repeatpos was -1, bitpos >=0
  2304. *
  2305. if (bitpos == repeatpos)
  2306. {
  2307. * Open branch check *
  2308. if (!(code->tree[lastnode].branches[bit] < 0))
  2309. {
  2310. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2311. "Invalid repeating code");
  2312. return (ARCHIVE_FATAL);
  2313. }
  2314. if ((repeatnode = new_node(code)) < 0) {
  2315. archive_set_error(&a->archive, ENOMEM,
  2316. "Unable to allocate memory for node data.");
  2317. return (ARCHIVE_FATAL);
  2318. }
  2319. if ((nextnode = new_node(code)) < 0) {
  2320. archive_set_error(&a->archive, ENOMEM,
  2321. "Unable to allocate memory for node data.");
  2322. return (ARCHIVE_FATAL);
  2323. }
  2324. * Set branches *
  2325. code->tree[lastnode].branches[bit] = repeatnode;
  2326. code->tree[repeatnode].branches[bit] = repeatnode;
  2327. code->tree[repeatnode].branches[bit^1] = nextnode;
  2328. lastnode = nextnode;
  2329. bitpos++; * terminating bit already handled, skip it *
  2330. }
  2331. else
  2332. {
  2333. */
  2334. /* Open branch check */
  2335. if (code->tree[lastnode].branches[bit] < 0)
  2336. {
  2337. if (new_node(code) < 0) {
  2338. archive_set_error(&a->archive, ENOMEM,
  2339. "Unable to allocate memory for node data.");
  2340. return (ARCHIVE_FATAL);
  2341. }
  2342. code->tree[lastnode].branches[bit] = code->numentries++;
  2343. }
  2344. /* set to branch */
  2345. lastnode = code->tree[lastnode].branches[bit];
  2346. /* } */
  2347. }
  2348. if (!(code->tree[lastnode].branches[0] == -1
  2349. && code->tree[lastnode].branches[1] == -2))
  2350. {
  2351. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2352. "Prefix found");
  2353. return (ARCHIVE_FATAL);
  2354. }
  2355. /* Set leaf value */
  2356. code->tree[lastnode].branches[0] = value;
  2357. code->tree[lastnode].branches[1] = value;
  2358. return (ARCHIVE_OK);
  2359. }
  2360. static int
  2361. new_node(struct huffman_code *code)
  2362. {
  2363. void *new_tree;
  2364. if (code->numallocatedentries == code->numentries) {
  2365. int new_num_entries = 256;
  2366. if (code->numentries > 0) {
  2367. new_num_entries = code->numentries * 2;
  2368. }
  2369. new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
  2370. if (new_tree == NULL)
  2371. return (-1);
  2372. code->tree = (struct huffman_tree_node *)new_tree;
  2373. code->numallocatedentries = new_num_entries;
  2374. }
  2375. code->tree[code->numentries].branches[0] = -1;
  2376. code->tree[code->numentries].branches[1] = -2;
  2377. return 1;
  2378. }
  2379. static int
  2380. make_table(struct archive_read *a, struct huffman_code *code)
  2381. {
  2382. if (code->maxlength < code->minlength || code->maxlength > 10)
  2383. code->tablesize = 10;
  2384. else
  2385. code->tablesize = code->maxlength;
  2386. code->table =
  2387. (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
  2388. * ((size_t)1 << code->tablesize));
  2389. return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
  2390. }
  2391. static int
  2392. make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
  2393. struct huffman_table_entry *table, int depth,
  2394. int maxdepth)
  2395. {
  2396. int currtablesize, i, ret = (ARCHIVE_OK);
  2397. if (!code->tree)
  2398. {
  2399. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2400. "Huffman tree was not created.");
  2401. return (ARCHIVE_FATAL);
  2402. }
  2403. if (node < 0 || node >= code->numentries)
  2404. {
  2405. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2406. "Invalid location to Huffman tree specified.");
  2407. return (ARCHIVE_FATAL);
  2408. }
  2409. currtablesize = 1 << (maxdepth - depth);
  2410. if (code->tree[node].branches[0] ==
  2411. code->tree[node].branches[1])
  2412. {
  2413. for(i = 0; i < currtablesize; i++)
  2414. {
  2415. table[i].length = depth;
  2416. table[i].value = code->tree[node].branches[0];
  2417. }
  2418. }
  2419. /*
  2420. * Dead code, node >= 0
  2421. *
  2422. else if (node < 0)
  2423. {
  2424. for(i = 0; i < currtablesize; i++)
  2425. table[i].length = -1;
  2426. }
  2427. */
  2428. else
  2429. {
  2430. if(depth == maxdepth)
  2431. {
  2432. table[0].length = maxdepth + 1;
  2433. table[0].value = node;
  2434. }
  2435. else
  2436. {
  2437. ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
  2438. depth + 1, maxdepth);
  2439. ret |= make_table_recurse(a, code, code->tree[node].branches[1],
  2440. table + currtablesize / 2, depth + 1, maxdepth);
  2441. }
  2442. }
  2443. return ret;
  2444. }
  2445. static int64_t
  2446. expand(struct archive_read *a, int64_t end)
  2447. {
  2448. static const unsigned char lengthbases[] =
  2449. { 0, 1, 2, 3, 4, 5, 6,
  2450. 7, 8, 10, 12, 14, 16, 20,
  2451. 24, 28, 32, 40, 48, 56, 64,
  2452. 80, 96, 112, 128, 160, 192, 224 };
  2453. static const unsigned char lengthbits[] =
  2454. { 0, 0, 0, 0, 0, 0, 0,
  2455. 0, 1, 1, 1, 1, 2, 2,
  2456. 2, 2, 3, 3, 3, 3, 4,
  2457. 4, 4, 4, 5, 5, 5, 5 };
  2458. static const int lengthb_min = minimum(
  2459. (int)(sizeof(lengthbases)/sizeof(lengthbases[0])),
  2460. (int)(sizeof(lengthbits)/sizeof(lengthbits[0]))
  2461. );
  2462. static const unsigned int offsetbases[] =
  2463. { 0, 1, 2, 3, 4, 6,
  2464. 8, 12, 16, 24, 32, 48,
  2465. 64, 96, 128, 192, 256, 384,
  2466. 512, 768, 1024, 1536, 2048, 3072,
  2467. 4096, 6144, 8192, 12288, 16384, 24576,
  2468. 32768, 49152, 65536, 98304, 131072, 196608,
  2469. 262144, 327680, 393216, 458752, 524288, 589824,
  2470. 655360, 720896, 786432, 851968, 917504, 983040,
  2471. 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
  2472. 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
  2473. static const unsigned char offsetbits[] =
  2474. { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
  2475. 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
  2476. 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
  2477. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2478. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
  2479. static const int offsetb_min = minimum(
  2480. (int)(sizeof(offsetbases)/sizeof(offsetbases[0])),
  2481. (int)(sizeof(offsetbits)/sizeof(offsetbits[0]))
  2482. );
  2483. static const unsigned char shortbases[] =
  2484. { 0, 4, 8, 16, 32, 64, 128, 192 };
  2485. static const unsigned char shortbits[] =
  2486. { 2, 2, 3, 4, 5, 6, 6, 6 };
  2487. int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
  2488. unsigned char newfile;
  2489. struct rar *rar = (struct rar *)(a->format->data);
  2490. struct rar_br *br = &(rar->br);
  2491. if (rar->filterstart < end)
  2492. end = rar->filterstart;
  2493. while (1)
  2494. {
  2495. if (rar->output_last_match &&
  2496. lzss_position(&rar->lzss) + rar->lastlength <= end)
  2497. {
  2498. lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
  2499. rar->output_last_match = 0;
  2500. }
  2501. if(rar->is_ppmd_block || rar->output_last_match ||
  2502. lzss_position(&rar->lzss) >= end)
  2503. return lzss_position(&rar->lzss);
  2504. if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
  2505. return (ARCHIVE_FATAL);
  2506. rar->output_last_match = 0;
  2507. if (symbol < 256)
  2508. {
  2509. lzss_emit_literal(rar, symbol);
  2510. continue;
  2511. }
  2512. else if (symbol == 256)
  2513. {
  2514. if (!rar_br_read_ahead(a, br, 1))
  2515. goto truncated_data;
  2516. newfile = !rar_br_bits(br, 1);
  2517. rar_br_consume(br, 1);
  2518. if(newfile)
  2519. {
  2520. rar->start_new_block = 1;
  2521. if (!rar_br_read_ahead(a, br, 1))
  2522. goto truncated_data;
  2523. rar->start_new_table = rar_br_bits(br, 1);
  2524. rar_br_consume(br, 1);
  2525. return lzss_position(&rar->lzss);
  2526. }
  2527. else
  2528. {
  2529. if (parse_codes(a) != ARCHIVE_OK)
  2530. return (ARCHIVE_FATAL);
  2531. continue;
  2532. }
  2533. }
  2534. else if(symbol==257)
  2535. {
  2536. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2537. "Parsing filters is unsupported.");
  2538. return (ARCHIVE_FAILED);
  2539. }
  2540. else if(symbol==258)
  2541. {
  2542. if(rar->lastlength == 0)
  2543. continue;
  2544. offs = rar->lastoffset;
  2545. len = rar->lastlength;
  2546. }
  2547. else if (symbol <= 262)
  2548. {
  2549. offsindex = symbol - 259;
  2550. offs = rar->oldoffset[offsindex];
  2551. if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
  2552. goto bad_data;
  2553. if (lensymbol > lengthb_min)
  2554. goto bad_data;
  2555. len = lengthbases[lensymbol] + 2;
  2556. if (lengthbits[lensymbol] > 0) {
  2557. if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
  2558. goto truncated_data;
  2559. len += rar_br_bits(br, lengthbits[lensymbol]);
  2560. rar_br_consume(br, lengthbits[lensymbol]);
  2561. }
  2562. for (i = offsindex; i > 0; i--)
  2563. rar->oldoffset[i] = rar->oldoffset[i-1];
  2564. rar->oldoffset[0] = offs;
  2565. }
  2566. else if(symbol<=270)
  2567. {
  2568. offs = shortbases[symbol-263] + 1;
  2569. if(shortbits[symbol-263] > 0) {
  2570. if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
  2571. goto truncated_data;
  2572. offs += rar_br_bits(br, shortbits[symbol-263]);
  2573. rar_br_consume(br, shortbits[symbol-263]);
  2574. }
  2575. len = 2;
  2576. for(i = 3; i > 0; i--)
  2577. rar->oldoffset[i] = rar->oldoffset[i-1];
  2578. rar->oldoffset[0] = offs;
  2579. }
  2580. else
  2581. {
  2582. if (symbol-271 > lengthb_min)
  2583. goto bad_data;
  2584. len = lengthbases[symbol-271]+3;
  2585. if(lengthbits[symbol-271] > 0) {
  2586. if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
  2587. goto truncated_data;
  2588. len += rar_br_bits(br, lengthbits[symbol-271]);
  2589. rar_br_consume(br, lengthbits[symbol-271]);
  2590. }
  2591. if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
  2592. goto bad_data;
  2593. if (offssymbol > offsetb_min)
  2594. goto bad_data;
  2595. offs = offsetbases[offssymbol]+1;
  2596. if(offsetbits[offssymbol] > 0)
  2597. {
  2598. if(offssymbol > 9)
  2599. {
  2600. if(offsetbits[offssymbol] > 4) {
  2601. if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
  2602. goto truncated_data;
  2603. offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
  2604. rar_br_consume(br, offsetbits[offssymbol] - 4);
  2605. }
  2606. if(rar->numlowoffsetrepeats > 0)
  2607. {
  2608. rar->numlowoffsetrepeats--;
  2609. offs += rar->lastlowoffset;
  2610. }
  2611. else
  2612. {
  2613. if ((lowoffsetsymbol =
  2614. read_next_symbol(a, &rar->lowoffsetcode)) < 0)
  2615. return (ARCHIVE_FATAL);
  2616. if(lowoffsetsymbol == 16)
  2617. {
  2618. rar->numlowoffsetrepeats = 15;
  2619. offs += rar->lastlowoffset;
  2620. }
  2621. else
  2622. {
  2623. offs += lowoffsetsymbol;
  2624. rar->lastlowoffset = lowoffsetsymbol;
  2625. }
  2626. }
  2627. }
  2628. else {
  2629. if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
  2630. goto truncated_data;
  2631. offs += rar_br_bits(br, offsetbits[offssymbol]);
  2632. rar_br_consume(br, offsetbits[offssymbol]);
  2633. }
  2634. }
  2635. if (offs >= 0x40000)
  2636. len++;
  2637. if (offs >= 0x2000)
  2638. len++;
  2639. for(i = 3; i > 0; i--)
  2640. rar->oldoffset[i] = rar->oldoffset[i-1];
  2641. rar->oldoffset[0] = offs;
  2642. }
  2643. rar->lastoffset = offs;
  2644. rar->lastlength = len;
  2645. rar->output_last_match = 1;
  2646. }
  2647. truncated_data:
  2648. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2649. "Truncated RAR file data");
  2650. rar->valid = 0;
  2651. return (ARCHIVE_FATAL);
  2652. bad_data:
  2653. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2654. "Bad RAR file data");
  2655. return (ARCHIVE_FATAL);
  2656. }
  2657. static int
  2658. copy_from_lzss_window(struct archive_read *a, const void **buffer,
  2659. int64_t startpos, int length)
  2660. {
  2661. int windowoffs, firstpart;
  2662. struct rar *rar = (struct rar *)(a->format->data);
  2663. if (!rar->unp_buffer)
  2664. {
  2665. if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
  2666. {
  2667. archive_set_error(&a->archive, ENOMEM,
  2668. "Unable to allocate memory for uncompressed data.");
  2669. return (ARCHIVE_FATAL);
  2670. }
  2671. }
  2672. windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
  2673. if(windowoffs + length <= lzss_size(&rar->lzss)) {
  2674. memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
  2675. length);
  2676. } else if (length <= lzss_size(&rar->lzss)) {
  2677. firstpart = lzss_size(&rar->lzss) - windowoffs;
  2678. if (firstpart < 0) {
  2679. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2680. "Bad RAR file data");
  2681. return (ARCHIVE_FATAL);
  2682. }
  2683. if (firstpart < length) {
  2684. memcpy(&rar->unp_buffer[rar->unp_offset],
  2685. &rar->lzss.window[windowoffs], firstpart);
  2686. memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
  2687. &rar->lzss.window[0], length - firstpart);
  2688. } else {
  2689. memcpy(&rar->unp_buffer[rar->unp_offset],
  2690. &rar->lzss.window[windowoffs], length);
  2691. }
  2692. } else {
  2693. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2694. "Bad RAR file data");
  2695. return (ARCHIVE_FATAL);
  2696. }
  2697. rar->unp_offset += length;
  2698. if (rar->unp_offset >= rar->unp_buffer_size)
  2699. *buffer = rar->unp_buffer;
  2700. else
  2701. *buffer = NULL;
  2702. return (ARCHIVE_OK);
  2703. }
  2704. static const void *
  2705. rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
  2706. {
  2707. struct rar *rar = (struct rar *)(a->format->data);
  2708. const void *h = __archive_read_ahead(a, min, avail);
  2709. int ret;
  2710. if (avail)
  2711. {
  2712. if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
  2713. *avail = a->archive.read_data_requested;
  2714. if (*avail > rar->bytes_remaining)
  2715. *avail = (ssize_t)rar->bytes_remaining;
  2716. if (*avail < 0)
  2717. return NULL;
  2718. else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
  2719. rar->file_flags & FHD_SPLIT_AFTER)
  2720. {
  2721. rar->filename_must_match = 1;
  2722. ret = archive_read_format_rar_read_header(a, a->entry);
  2723. if (ret == (ARCHIVE_EOF))
  2724. {
  2725. rar->has_endarc_header = 1;
  2726. ret = archive_read_format_rar_read_header(a, a->entry);
  2727. }
  2728. rar->filename_must_match = 0;
  2729. if (ret != (ARCHIVE_OK))
  2730. return NULL;
  2731. return rar_read_ahead(a, min, avail);
  2732. }
  2733. }
  2734. return h;
  2735. }