archive_read_support_format_rar.c 83 KB

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