archive_read_support_format_rar.c 80 KB

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