archive_read_support_format_lha.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. /*-
  2. * Copyright (c) 2008-2012 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #ifdef HAVE_LIMITS_H
  30. #include <limits.h>
  31. #endif
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_read_private.h"
  43. #include "archive_endian.h"
  44. #define MAXMATCH 256 /* Maximum match length. */
  45. #define MINMATCH 3 /* Minimum match length. */
  46. /*
  47. * Literal table format:
  48. * +0 +256 +510
  49. * +---------------+-------------------------+
  50. * | literal code | match length |
  51. * | 0 ... 255 | MINMATCH ... MAXMATCH |
  52. * +---------------+-------------------------+
  53. * <--- LT_BITLEN_SIZE --->
  54. */
  55. /* Literal table size. */
  56. #define LT_BITLEN_SIZE (UCHAR_MAX + 1 + MAXMATCH - MINMATCH + 1)
  57. /* Position table size.
  58. * Note: this used for both position table and pre literal table.*/
  59. #define PT_BITLEN_SIZE (3 + 16)
  60. struct lzh_dec {
  61. /* Decoding status. */
  62. int state;
  63. /*
  64. * Window to see last 8Ki(lh5),32Ki(lh6),64Ki(lh7) bytes of decoded
  65. * data.
  66. */
  67. int w_size;
  68. int w_mask;
  69. /* Window buffer, which is a loop buffer. */
  70. unsigned char *w_buff;
  71. /* The insert position to the window. */
  72. int w_pos;
  73. /* The position where we can copy decoded code from the window. */
  74. int copy_pos;
  75. /* The length how many bytes we can copy decoded code from
  76. * the window. */
  77. int copy_len;
  78. /* The remaining bytes that we have not copied decoded data from
  79. * the window to an output buffer. */
  80. int w_remaining;
  81. /*
  82. * Bit stream reader.
  83. */
  84. struct lzh_br {
  85. #define CACHE_TYPE uint64_t
  86. #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
  87. /* Cache buffer. */
  88. CACHE_TYPE cache_buffer;
  89. /* Indicates how many bits avail in cache_buffer. */
  90. int cache_avail;
  91. } br;
  92. /*
  93. * Huffman coding.
  94. */
  95. struct huffman {
  96. int len_size;
  97. int len_avail;
  98. int len_bits;
  99. int freq[17];
  100. unsigned char *bitlen;
  101. /*
  102. * Use a index table. It's faster than searching a huffman
  103. * coding tree, which is a binary tree. But a use of a large
  104. * index table causes L1 cache read miss many times.
  105. */
  106. #define HTBL_BITS 10
  107. int max_bits;
  108. int shift_bits;
  109. int tbl_bits;
  110. int tree_used;
  111. int tree_avail;
  112. /* Direct access table. */
  113. uint16_t *tbl;
  114. /* Binary tree table for extra bits over the direct access. */
  115. struct htree_t {
  116. uint16_t left;
  117. uint16_t right;
  118. } *tree;
  119. } lt, pt;
  120. int blocks_avail;
  121. int pos_pt_len_size;
  122. int pos_pt_len_bits;
  123. int literal_pt_len_size;
  124. int literal_pt_len_bits;
  125. int reading_position;
  126. int loop;
  127. int error;
  128. };
  129. struct lzh_stream {
  130. const unsigned char *next_in;
  131. int64_t avail_in;
  132. int64_t total_in;
  133. unsigned char *next_out;
  134. int64_t avail_out;
  135. int64_t total_out;
  136. struct lzh_dec *ds;
  137. };
  138. struct lha {
  139. /* entry_bytes_remaining is the number of bytes we expect. */
  140. int64_t entry_offset;
  141. int64_t entry_bytes_remaining;
  142. int64_t entry_unconsumed;
  143. uint16_t entry_crc_calculated;
  144. size_t header_size; /* header size */
  145. unsigned char level; /* header level */
  146. char method[3]; /* compress type */
  147. int64_t compsize; /* compressed data size */
  148. int64_t origsize; /* original file size */
  149. int setflag;
  150. #define BIRTHTIME_IS_SET 1
  151. #define ATIME_IS_SET 2
  152. #define UNIX_MODE_IS_SET 4
  153. #define CRC_IS_SET 8
  154. time_t birthtime;
  155. long birthtime_tv_nsec;
  156. time_t mtime;
  157. long mtime_tv_nsec;
  158. time_t atime;
  159. long atime_tv_nsec;
  160. mode_t mode;
  161. int64_t uid;
  162. int64_t gid;
  163. struct archive_string uname;
  164. struct archive_string gname;
  165. uint16_t header_crc;
  166. uint16_t crc;
  167. struct archive_string_conv *sconv;
  168. struct archive_string_conv *opt_sconv;
  169. struct archive_string dirname;
  170. struct archive_string filename;
  171. struct archive_wstring ws;
  172. unsigned char dos_attr;
  173. /* Flag to mark progress that an archive was read their first header.*/
  174. char found_first_header;
  175. /* Flag to mark that indicates an empty directory. */
  176. char directory;
  177. /* Flags to mark progress of decompression. */
  178. char decompress_init;
  179. char end_of_entry;
  180. char end_of_entry_cleanup;
  181. char entry_is_compressed;
  182. unsigned char *uncompressed_buffer;
  183. size_t uncompressed_buffer_size;
  184. char format_name[64];
  185. struct lzh_stream strm;
  186. };
  187. /*
  188. * LHA header common member offset.
  189. */
  190. #define H_METHOD_OFFSET 2 /* Compress type. */
  191. #define H_ATTR_OFFSET 19 /* DOS attribute. */
  192. #define H_LEVEL_OFFSET 20 /* Header Level. */
  193. #define H_SIZE 22 /* Minimum header size. */
  194. static const uint16_t crc16tbl[256] = {
  195. 0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,
  196. 0xC601,0x06C0,0x0780,0xC741,0x0500,0xC5C1,0xC481,0x0440,
  197. 0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,
  198. 0x0A00,0xCAC1,0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,
  199. 0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,0x1A40,
  200. 0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,
  201. 0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
  202. 0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
  203. 0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
  204. 0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
  205. 0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,
  206. 0xFA01,0x3AC0,0x3B80,0xFB41,0x3900,0xF9C1,0xF881,0x3840,
  207. 0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
  208. 0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
  209. 0xE401,0x24C0,0x2580,0xE541,0x2700,0xE7C1,0xE681,0x2640,
  210. 0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,
  211. 0xA001,0x60C0,0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,
  212. 0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,0xA441,
  213. 0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,
  214. 0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
  215. 0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,
  216. 0xBE01,0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,
  217. 0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,0xB681,0x7640,
  218. 0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,
  219. 0x5000,0x90C1,0x9181,0x5140,0x9301,0x53C0,0x5280,0x9241,
  220. 0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
  221. 0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,
  222. 0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,0x59C0,0x5880,0x9841,
  223. 0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,
  224. 0x4E00,0x8EC1,0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,
  225. 0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,0x8641,
  226. 0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040
  227. };
  228. static int archive_read_format_lha_bid(struct archive_read *, int);
  229. static int archive_read_format_lha_options(struct archive_read *,
  230. const char *, const char *);
  231. static int archive_read_format_lha_read_header(struct archive_read *,
  232. struct archive_entry *);
  233. static int archive_read_format_lha_read_data(struct archive_read *,
  234. const void **, size_t *, int64_t *);
  235. static int archive_read_format_lha_read_data_skip(struct archive_read *);
  236. static int archive_read_format_lha_cleanup(struct archive_read *);
  237. static void lha_replace_path_separator(struct lha *,
  238. struct archive_entry *);
  239. static int lha_read_file_header_0(struct archive_read *, struct lha *);
  240. static int lha_read_file_header_1(struct archive_read *, struct lha *);
  241. static int lha_read_file_header_2(struct archive_read *, struct lha *);
  242. static int lha_read_file_header_3(struct archive_read *, struct lha *);
  243. static int lha_read_file_extended_header(struct archive_read *,
  244. struct lha *, uint16_t *, int, size_t, size_t *);
  245. static size_t lha_check_header_format(const void *);
  246. static int lha_skip_sfx(struct archive_read *);
  247. static time_t lha_dos_time(const unsigned char *);
  248. static time_t lha_win_time(uint64_t, long *);
  249. static unsigned char lha_calcsum(unsigned char, const void *,
  250. int, size_t);
  251. static int lha_parse_linkname(struct archive_string *,
  252. struct archive_string *);
  253. static int lha_read_data_none(struct archive_read *, const void **,
  254. size_t *, int64_t *);
  255. static int lha_read_data_lzh(struct archive_read *, const void **,
  256. size_t *, int64_t *);
  257. static uint16_t lha_crc16(uint16_t, const void *, size_t);
  258. static int lzh_decode_init(struct lzh_stream *, const char *);
  259. static void lzh_decode_free(struct lzh_stream *);
  260. static int lzh_decode(struct lzh_stream *, int);
  261. static int lzh_br_fillup(struct lzh_stream *, struct lzh_br *);
  262. static int lzh_huffman_init(struct huffman *, size_t, int);
  263. static void lzh_huffman_free(struct huffman *);
  264. static int lzh_read_pt_bitlen(struct lzh_stream *, int start, int end);
  265. static int lzh_make_fake_table(struct huffman *, uint16_t);
  266. static int lzh_make_huffman_table(struct huffman *);
  267. static inline int lzh_decode_huffman(struct huffman *, unsigned);
  268. static int lzh_decode_huffman_tree(struct huffman *, unsigned, int);
  269. int
  270. archive_read_support_format_lha(struct archive *_a)
  271. {
  272. struct archive_read *a = (struct archive_read *)_a;
  273. struct lha *lha;
  274. int r;
  275. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  276. ARCHIVE_STATE_NEW, "archive_read_support_format_lha");
  277. lha = (struct lha *)calloc(1, sizeof(*lha));
  278. if (lha == NULL) {
  279. archive_set_error(&a->archive, ENOMEM,
  280. "Can't allocate lha data");
  281. return (ARCHIVE_FATAL);
  282. }
  283. archive_string_init(&lha->ws);
  284. r = __archive_read_register_format(a,
  285. lha,
  286. "lha",
  287. archive_read_format_lha_bid,
  288. archive_read_format_lha_options,
  289. archive_read_format_lha_read_header,
  290. archive_read_format_lha_read_data,
  291. archive_read_format_lha_read_data_skip,
  292. NULL,
  293. archive_read_format_lha_cleanup,
  294. NULL,
  295. NULL);
  296. if (r != ARCHIVE_OK)
  297. free(lha);
  298. return (ARCHIVE_OK);
  299. }
  300. static size_t
  301. lha_check_header_format(const void *h)
  302. {
  303. const unsigned char *p = h;
  304. size_t next_skip_bytes;
  305. switch (p[H_METHOD_OFFSET+3]) {
  306. /*
  307. * "-lh0-" ... "-lh7-" "-lhd-"
  308. * "-lzs-" "-lz5-"
  309. */
  310. case '0': case '1': case '2': case '3':
  311. case '4': case '5': case '6': case '7':
  312. case 'd':
  313. case 's':
  314. next_skip_bytes = 4;
  315. /* b0 == 0 means the end of an LHa archive file. */
  316. if (p[0] == 0)
  317. break;
  318. if (p[H_METHOD_OFFSET] != '-' || p[H_METHOD_OFFSET+1] != 'l'
  319. || p[H_METHOD_OFFSET+4] != '-')
  320. break;
  321. if (p[H_METHOD_OFFSET+2] == 'h') {
  322. /* "-lh?-" */
  323. if (p[H_METHOD_OFFSET+3] == 's')
  324. break;
  325. if (p[H_LEVEL_OFFSET] == 0)
  326. return (0);
  327. if (p[H_LEVEL_OFFSET] <= 3 && p[H_ATTR_OFFSET] == 0x20)
  328. return (0);
  329. }
  330. if (p[H_METHOD_OFFSET+2] == 'z') {
  331. /* LArc extensions: -lzs-,-lz4- and -lz5- */
  332. if (p[H_LEVEL_OFFSET] != 0)
  333. break;
  334. if (p[H_METHOD_OFFSET+3] == 's'
  335. || p[H_METHOD_OFFSET+3] == '4'
  336. || p[H_METHOD_OFFSET+3] == '5')
  337. return (0);
  338. }
  339. break;
  340. case 'h': next_skip_bytes = 1; break;
  341. case 'z': next_skip_bytes = 1; break;
  342. case 'l': next_skip_bytes = 2; break;
  343. case '-': next_skip_bytes = 3; break;
  344. default : next_skip_bytes = 4; break;
  345. }
  346. return (next_skip_bytes);
  347. }
  348. static int
  349. archive_read_format_lha_bid(struct archive_read *a, int best_bid)
  350. {
  351. const char *p;
  352. const void *buff;
  353. ssize_t bytes_avail, offset, window;
  354. size_t next;
  355. /* If there's already a better bid than we can ever
  356. make, don't bother testing. */
  357. if (best_bid > 30)
  358. return (-1);
  359. if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL)
  360. return (-1);
  361. if (lha_check_header_format(p) == 0)
  362. return (30);
  363. if (p[0] == 'M' && p[1] == 'Z') {
  364. /* PE file */
  365. offset = 0;
  366. window = 4096;
  367. while (offset < (1024 * 20)) {
  368. buff = __archive_read_ahead(a, offset + window,
  369. &bytes_avail);
  370. if (buff == NULL) {
  371. /* Remaining bytes are less than window. */
  372. window >>= 1;
  373. if (window < (H_SIZE + 3))
  374. return (0);
  375. continue;
  376. }
  377. p = (const char *)buff + offset;
  378. while (p + H_SIZE < (const char *)buff + bytes_avail) {
  379. if ((next = lha_check_header_format(p)) == 0)
  380. return (30);
  381. p += next;
  382. }
  383. offset = p - (const char *)buff;
  384. }
  385. }
  386. return (0);
  387. }
  388. static int
  389. archive_read_format_lha_options(struct archive_read *a,
  390. const char *key, const char *val)
  391. {
  392. struct lha *lha;
  393. int ret = ARCHIVE_FAILED;
  394. lha = (struct lha *)(a->format->data);
  395. if (strcmp(key, "hdrcharset") == 0) {
  396. if (val == NULL || val[0] == 0)
  397. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  398. "lha: hdrcharset option needs a character-set name");
  399. else {
  400. lha->opt_sconv =
  401. archive_string_conversion_from_charset(
  402. &a->archive, val, 0);
  403. if (lha->opt_sconv != NULL)
  404. ret = ARCHIVE_OK;
  405. else
  406. ret = ARCHIVE_FATAL;
  407. }
  408. return (ret);
  409. }
  410. /* Note: The "warn" return is just to inform the options
  411. * supervisor that we didn't handle it. It will generate
  412. * a suitable error if no one used this option. */
  413. return (ARCHIVE_WARN);
  414. }
  415. static int
  416. lha_skip_sfx(struct archive_read *a)
  417. {
  418. const void *h;
  419. const char *p, *q;
  420. size_t next, skip;
  421. ssize_t bytes, window;
  422. window = 4096;
  423. for (;;) {
  424. h = __archive_read_ahead(a, window, &bytes);
  425. if (h == NULL) {
  426. /* Remaining bytes are less than window. */
  427. window >>= 1;
  428. if (window < (H_SIZE + 3))
  429. goto fatal;
  430. continue;
  431. }
  432. if (bytes < H_SIZE)
  433. goto fatal;
  434. p = h;
  435. q = p + bytes;
  436. /*
  437. * Scan ahead until we find something that looks
  438. * like the lha header.
  439. */
  440. while (p + H_SIZE < q) {
  441. if ((next = lha_check_header_format(p)) == 0) {
  442. skip = p - (const char *)h;
  443. __archive_read_consume(a, skip);
  444. return (ARCHIVE_OK);
  445. }
  446. p += next;
  447. }
  448. skip = p - (const char *)h;
  449. __archive_read_consume(a, skip);
  450. }
  451. fatal:
  452. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  453. "Couldn't find out LHa header");
  454. return (ARCHIVE_FATAL);
  455. }
  456. static int
  457. truncated_error(struct archive_read *a)
  458. {
  459. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  460. "Truncated LHa header");
  461. return (ARCHIVE_FATAL);
  462. }
  463. static int
  464. archive_read_format_lha_read_header(struct archive_read *a,
  465. struct archive_entry *entry)
  466. {
  467. struct archive_string linkname;
  468. struct archive_string pathname;
  469. struct lha *lha;
  470. const unsigned char *p;
  471. const char *signature;
  472. int err;
  473. a->archive.archive_format = ARCHIVE_FORMAT_LHA;
  474. if (a->archive.archive_format_name == NULL)
  475. a->archive.archive_format_name = "lha";
  476. lha = (struct lha *)(a->format->data);
  477. lha->decompress_init = 0;
  478. lha->end_of_entry = 0;
  479. lha->end_of_entry_cleanup = 0;
  480. lha->entry_unconsumed = 0;
  481. if ((p = __archive_read_ahead(a, H_SIZE, NULL)) == NULL) {
  482. /*
  483. * LHa archiver added 0 to the tail of its archive file as
  484. * the mark of the end of the archive.
  485. */
  486. signature = __archive_read_ahead(a, sizeof(signature[0]), NULL);
  487. if (signature == NULL || signature[0] == 0)
  488. return (ARCHIVE_EOF);
  489. return (truncated_error(a));
  490. }
  491. signature = (const char *)p;
  492. if (lha->found_first_header == 0 &&
  493. signature[0] == 'M' && signature[1] == 'Z') {
  494. /* This is an executable? Must be self-extracting... */
  495. err = lha_skip_sfx(a);
  496. if (err < ARCHIVE_WARN)
  497. return (err);
  498. if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
  499. return (truncated_error(a));
  500. signature = (const char *)p;
  501. }
  502. /* signature[0] == 0 means the end of an LHa archive file. */
  503. if (signature[0] == 0)
  504. return (ARCHIVE_EOF);
  505. /*
  506. * Check the header format and method type.
  507. */
  508. if (lha_check_header_format(p) != 0) {
  509. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  510. "Bad LHa file");
  511. return (ARCHIVE_FATAL);
  512. }
  513. /* We've found the first header. */
  514. lha->found_first_header = 1;
  515. /* Set a default value and common data */
  516. lha->header_size = 0;
  517. lha->level = p[H_LEVEL_OFFSET];
  518. lha->method[0] = p[H_METHOD_OFFSET+1];
  519. lha->method[1] = p[H_METHOD_OFFSET+2];
  520. lha->method[2] = p[H_METHOD_OFFSET+3];
  521. if (memcmp(lha->method, "lhd", 3) == 0)
  522. lha->directory = 1;
  523. else
  524. lha->directory = 0;
  525. if (memcmp(lha->method, "lh0", 3) == 0 ||
  526. memcmp(lha->method, "lz4", 3) == 0)
  527. lha->entry_is_compressed = 0;
  528. else
  529. lha->entry_is_compressed = 1;
  530. lha->compsize = 0;
  531. lha->origsize = 0;
  532. lha->setflag = 0;
  533. lha->birthtime = 0;
  534. lha->birthtime_tv_nsec = 0;
  535. lha->mtime = 0;
  536. lha->mtime_tv_nsec = 0;
  537. lha->atime = 0;
  538. lha->atime_tv_nsec = 0;
  539. lha->mode = (lha->directory)? 0777 : 0666;
  540. lha->uid = 0;
  541. lha->gid = 0;
  542. archive_string_empty(&lha->dirname);
  543. archive_string_empty(&lha->filename);
  544. lha->dos_attr = 0;
  545. if (lha->opt_sconv != NULL)
  546. lha->sconv = lha->opt_sconv;
  547. else
  548. lha->sconv = NULL;
  549. switch (p[H_LEVEL_OFFSET]) {
  550. case 0:
  551. err = lha_read_file_header_0(a, lha);
  552. break;
  553. case 1:
  554. err = lha_read_file_header_1(a, lha);
  555. break;
  556. case 2:
  557. err = lha_read_file_header_2(a, lha);
  558. break;
  559. case 3:
  560. err = lha_read_file_header_3(a, lha);
  561. break;
  562. default:
  563. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  564. "Unsupported LHa header level %d", p[H_LEVEL_OFFSET]);
  565. err = ARCHIVE_FATAL;
  566. break;
  567. }
  568. if (err < ARCHIVE_WARN)
  569. return (err);
  570. if (!lha->directory && archive_strlen(&lha->filename) == 0)
  571. /* The filename has not been set */
  572. return (truncated_error(a));
  573. /*
  574. * Make a pathname from a dirname and a filename.
  575. */
  576. archive_string_concat(&lha->dirname, &lha->filename);
  577. archive_string_init(&pathname);
  578. archive_string_init(&linkname);
  579. archive_string_copy(&pathname, &lha->dirname);
  580. if ((lha->mode & AE_IFMT) == AE_IFLNK) {
  581. /*
  582. * Extract the symlink-name if it's included in the pathname.
  583. */
  584. if (!lha_parse_linkname(&linkname, &pathname)) {
  585. /* We couldn't get the symlink-name. */
  586. archive_set_error(&a->archive,
  587. ARCHIVE_ERRNO_FILE_FORMAT,
  588. "Unknown symlink-name");
  589. archive_string_free(&pathname);
  590. archive_string_free(&linkname);
  591. return (ARCHIVE_FAILED);
  592. }
  593. } else {
  594. /*
  595. * Make sure a file-type is set.
  596. * The mode has been overridden if it is in the extended data.
  597. */
  598. lha->mode = (lha->mode & ~AE_IFMT) |
  599. ((lha->directory)? AE_IFDIR: AE_IFREG);
  600. }
  601. if ((lha->setflag & UNIX_MODE_IS_SET) == 0 &&
  602. (lha->dos_attr & 1) != 0)
  603. lha->mode &= ~(0222);/* read only. */
  604. /*
  605. * Set basic file parameters.
  606. */
  607. if (archive_entry_copy_pathname_l(entry, pathname.s,
  608. pathname.length, lha->sconv) != 0) {
  609. if (errno == ENOMEM) {
  610. archive_set_error(&a->archive, ENOMEM,
  611. "Can't allocate memory for Pathname");
  612. return (ARCHIVE_FATAL);
  613. }
  614. archive_set_error(&a->archive,
  615. ARCHIVE_ERRNO_FILE_FORMAT,
  616. "Pathname cannot be converted "
  617. "from %s to current locale.",
  618. archive_string_conversion_charset_name(lha->sconv));
  619. err = ARCHIVE_WARN;
  620. }
  621. archive_string_free(&pathname);
  622. if (archive_strlen(&linkname) > 0) {
  623. if (archive_entry_copy_symlink_l(entry, linkname.s,
  624. linkname.length, lha->sconv) != 0) {
  625. if (errno == ENOMEM) {
  626. archive_set_error(&a->archive, ENOMEM,
  627. "Can't allocate memory for Linkname");
  628. return (ARCHIVE_FATAL);
  629. }
  630. archive_set_error(&a->archive,
  631. ARCHIVE_ERRNO_FILE_FORMAT,
  632. "Linkname cannot be converted "
  633. "from %s to current locale.",
  634. archive_string_conversion_charset_name(lha->sconv));
  635. err = ARCHIVE_WARN;
  636. }
  637. } else
  638. archive_entry_set_symlink(entry, NULL);
  639. archive_string_free(&linkname);
  640. /*
  641. * When a header level is 0, there is a possibility that
  642. * a pathname and a symlink has '\' character, a directory
  643. * separator in DOS/Windows. So we should convert it to '/'.
  644. */
  645. if (p[H_LEVEL_OFFSET] == 0)
  646. lha_replace_path_separator(lha, entry);
  647. archive_entry_set_mode(entry, lha->mode);
  648. archive_entry_set_uid(entry, lha->uid);
  649. archive_entry_set_gid(entry, lha->gid);
  650. if (archive_strlen(&lha->uname) > 0)
  651. archive_entry_set_uname(entry, lha->uname.s);
  652. if (archive_strlen(&lha->gname) > 0)
  653. archive_entry_set_gname(entry, lha->gname.s);
  654. if (lha->setflag & BIRTHTIME_IS_SET) {
  655. archive_entry_set_birthtime(entry, lha->birthtime,
  656. lha->birthtime_tv_nsec);
  657. archive_entry_set_ctime(entry, lha->birthtime,
  658. lha->birthtime_tv_nsec);
  659. } else {
  660. archive_entry_unset_birthtime(entry);
  661. archive_entry_unset_ctime(entry);
  662. }
  663. archive_entry_set_mtime(entry, lha->mtime, lha->mtime_tv_nsec);
  664. if (lha->setflag & ATIME_IS_SET)
  665. archive_entry_set_atime(entry, lha->atime,
  666. lha->atime_tv_nsec);
  667. else
  668. archive_entry_unset_atime(entry);
  669. if (lha->directory || archive_entry_symlink(entry) != NULL)
  670. archive_entry_unset_size(entry);
  671. else
  672. archive_entry_set_size(entry, lha->origsize);
  673. /*
  674. * Prepare variables used to read a file content.
  675. */
  676. lha->entry_bytes_remaining = lha->compsize;
  677. lha->entry_offset = 0;
  678. lha->entry_crc_calculated = 0;
  679. /*
  680. * This file does not have a content.
  681. */
  682. if (lha->directory || lha->compsize == 0)
  683. lha->end_of_entry = 1;
  684. sprintf(lha->format_name, "lha -%c%c%c-",
  685. lha->method[0], lha->method[1], lha->method[2]);
  686. a->archive.archive_format_name = lha->format_name;
  687. return (err);
  688. }
  689. /*
  690. * Replace a DOS path separator '\' by a character '/'.
  691. * Some multi-byte character set have a character '\' in its second byte.
  692. */
  693. static void
  694. lha_replace_path_separator(struct lha *lha, struct archive_entry *entry)
  695. {
  696. const wchar_t *wp;
  697. size_t i;
  698. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  699. archive_wstrcpy(&(lha->ws), wp);
  700. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  701. if (lha->ws.s[i] == L'\\')
  702. lha->ws.s[i] = L'/';
  703. }
  704. archive_entry_copy_pathname_w(entry, lha->ws.s);
  705. }
  706. if ((wp = archive_entry_symlink_w(entry)) != NULL) {
  707. archive_wstrcpy(&(lha->ws), wp);
  708. for (i = 0; i < archive_strlen(&(lha->ws)); i++) {
  709. if (lha->ws.s[i] == L'\\')
  710. lha->ws.s[i] = L'/';
  711. }
  712. archive_entry_copy_symlink_w(entry, lha->ws.s);
  713. }
  714. }
  715. /*
  716. * Header 0 format
  717. *
  718. * +0 +1 +2 +7 +11
  719. * +---------------+----------+----------------+-------------------+
  720. * |header size(*1)|header sum|compression type|compressed size(*2)|
  721. * +---------------+----------+----------------+-------------------+
  722. * <---------------------(*1)----------*
  723. *
  724. * +11 +15 +17 +19 +20 +21
  725. * +-----------------+---------+---------+--------------+----------------+
  726. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=0)|
  727. * +-----------------+---------+---------+--------------+----------------+
  728. * *--------------------------------(*1)---------------------------------*
  729. *
  730. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+2+(*4)
  731. * +---------------+---------+----------+----------------+------------------+
  732. * |name length(*3)|file name|file CRC16|extra header(*4)| compressed data |
  733. * +---------------+---------+----------+----------------+------------------+
  734. * <--(*3)-> <------(*2)------>
  735. * *----------------------(*1)-------------------------->
  736. *
  737. */
  738. #define H0_HEADER_SIZE_OFFSET 0
  739. #define H0_HEADER_SUM_OFFSET 1
  740. #define H0_COMP_SIZE_OFFSET 7
  741. #define H0_ORIG_SIZE_OFFSET 11
  742. #define H0_DOS_TIME_OFFSET 15
  743. #define H0_NAME_LEN_OFFSET 21
  744. #define H0_FILE_NAME_OFFSET 22
  745. #define H0_FIXED_SIZE 24
  746. static int
  747. lha_read_file_header_0(struct archive_read *a, struct lha *lha)
  748. {
  749. const unsigned char *p;
  750. int extdsize, namelen;
  751. unsigned char headersum, sum_calculated;
  752. if ((p = __archive_read_ahead(a, H0_FIXED_SIZE, NULL)) == NULL)
  753. return (truncated_error(a));
  754. lha->header_size = p[H0_HEADER_SIZE_OFFSET] + 2;
  755. headersum = p[H0_HEADER_SUM_OFFSET];
  756. lha->compsize = archive_le32dec(p + H0_COMP_SIZE_OFFSET);
  757. lha->origsize = archive_le32dec(p + H0_ORIG_SIZE_OFFSET);
  758. lha->mtime = lha_dos_time(p + H0_DOS_TIME_OFFSET);
  759. namelen = p[H0_NAME_LEN_OFFSET];
  760. extdsize = (int)lha->header_size - H0_FIXED_SIZE - namelen;
  761. if ((namelen > 221 || extdsize < 0) && extdsize != -2) {
  762. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  763. "Invalid LHa header");
  764. return (ARCHIVE_FATAL);
  765. }
  766. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  767. return (truncated_error(a));
  768. archive_strncpy(&lha->filename, p + H0_FILE_NAME_OFFSET, namelen);
  769. /* When extdsize == -2, A CRC16 value is not present in the header. */
  770. if (extdsize >= 0) {
  771. lha->crc = archive_le16dec(p + H0_FILE_NAME_OFFSET + namelen);
  772. lha->setflag |= CRC_IS_SET;
  773. }
  774. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  775. /* Read an extended header */
  776. if (extdsize > 0) {
  777. /* This extended data is set by 'LHa for UNIX' only.
  778. * Maybe fixed size.
  779. */
  780. p += H0_FILE_NAME_OFFSET + namelen + 2;
  781. if (p[0] == 'U' && extdsize == 12) {
  782. /* p[1] is a minor version. */
  783. lha->mtime = archive_le32dec(&p[2]);
  784. lha->mode = archive_le16dec(&p[6]);
  785. lha->uid = archive_le16dec(&p[8]);
  786. lha->gid = archive_le16dec(&p[10]);
  787. lha->setflag |= UNIX_MODE_IS_SET;
  788. }
  789. }
  790. __archive_read_consume(a, lha->header_size);
  791. if (sum_calculated != headersum) {
  792. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  793. "LHa header sum error");
  794. return (ARCHIVE_FATAL);
  795. }
  796. return (ARCHIVE_OK);
  797. }
  798. /*
  799. * Header 1 format
  800. *
  801. * +0 +1 +2 +7 +11
  802. * +---------------+----------+----------------+-------------+
  803. * |header size(*1)|header sum|compression type|skip size(*2)|
  804. * +---------------+----------+----------------+-------------+
  805. * <---------------(*1)----------*
  806. *
  807. * +11 +15 +17 +19 +20 +21
  808. * +-----------------+---------+---------+--------------+----------------+
  809. * |uncompressed size|time(DOS)|date(DOS)|attribute(DOS)|header level(=1)|
  810. * +-----------------+---------+---------+--------------+----------------+
  811. * *-------------------------------(*1)----------------------------------*
  812. *
  813. * +21 +22 +22+(*3) +22+(*3)+2 +22+(*3)+3 +22+(*3)+3+(*4)
  814. * +---------------+---------+----------+-----------+-----------+
  815. * |name length(*3)|file name|file CRC16| creator |padding(*4)|
  816. * +---------------+---------+----------+-----------+-----------+
  817. * <--(*3)->
  818. * *----------------------------(*1)----------------------------*
  819. *
  820. * +22+(*3)+3+(*4) +22+(*3)+3+(*4)+2 +22+(*3)+3+(*4)+2+(*5)
  821. * +----------------+---------------------+------------------------+
  822. * |next header size| extended header(*5) | compressed data |
  823. * +----------------+---------------------+------------------------+
  824. * *------(*1)-----> <--------------------(*2)-------------------->
  825. */
  826. #define H1_HEADER_SIZE_OFFSET 0
  827. #define H1_HEADER_SUM_OFFSET 1
  828. #define H1_COMP_SIZE_OFFSET 7
  829. #define H1_ORIG_SIZE_OFFSET 11
  830. #define H1_DOS_TIME_OFFSET 15
  831. #define H1_NAME_LEN_OFFSET 21
  832. #define H1_FILE_NAME_OFFSET 22
  833. #define H1_FIXED_SIZE 27
  834. static int
  835. lha_read_file_header_1(struct archive_read *a, struct lha *lha)
  836. {
  837. const unsigned char *p;
  838. size_t extdsize;
  839. int i, err, err2;
  840. int namelen, padding;
  841. unsigned char headersum, sum_calculated;
  842. err = ARCHIVE_OK;
  843. if ((p = __archive_read_ahead(a, H1_FIXED_SIZE, NULL)) == NULL)
  844. return (truncated_error(a));
  845. lha->header_size = p[H1_HEADER_SIZE_OFFSET] + 2;
  846. headersum = p[H1_HEADER_SUM_OFFSET];
  847. /* Note: An extended header size is included in a compsize. */
  848. lha->compsize = archive_le32dec(p + H1_COMP_SIZE_OFFSET);
  849. lha->origsize = archive_le32dec(p + H1_ORIG_SIZE_OFFSET);
  850. lha->mtime = lha_dos_time(p + H1_DOS_TIME_OFFSET);
  851. namelen = p[H1_NAME_LEN_OFFSET];
  852. /* Calculate a padding size. The result will be normally 0 only(?) */
  853. padding = ((int)lha->header_size) - H1_FIXED_SIZE - namelen;
  854. if (namelen > 230 || padding < 0)
  855. goto invalid;
  856. if ((p = __archive_read_ahead(a, lha->header_size, NULL)) == NULL)
  857. return (truncated_error(a));
  858. for (i = 0; i < namelen; i++) {
  859. if (p[i + H1_FILE_NAME_OFFSET] == 0xff)
  860. goto invalid;/* Invalid filename. */
  861. }
  862. archive_strncpy(&lha->filename, p + H1_FILE_NAME_OFFSET, namelen);
  863. lha->crc = archive_le16dec(p + H1_FILE_NAME_OFFSET + namelen);
  864. lha->setflag |= CRC_IS_SET;
  865. sum_calculated = lha_calcsum(0, p, 2, lha->header_size - 2);
  866. /* Consume used bytes but not include `next header size' data
  867. * since it will be consumed in lha_read_file_extended_header(). */
  868. __archive_read_consume(a, lha->header_size - 2);
  869. /* Read extended headers */
  870. err2 = lha_read_file_extended_header(a, lha, NULL, 2,
  871. (size_t)(lha->compsize + 2), &extdsize);
  872. if (err2 < ARCHIVE_WARN)
  873. return (err2);
  874. if (err2 < err)
  875. err = err2;
  876. /* Get a real compressed file size. */
  877. lha->compsize -= extdsize - 2;
  878. if (sum_calculated != headersum) {
  879. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  880. "LHa header sum error");
  881. return (ARCHIVE_FATAL);
  882. }
  883. return (err);
  884. invalid:
  885. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  886. "Invalid LHa header");
  887. return (ARCHIVE_FATAL);
  888. }
  889. /*
  890. * Header 2 format
  891. *
  892. * +0 +2 +7 +11 +15
  893. * +---------------+----------------+-------------------+-----------------+
  894. * |header size(*1)|compression type|compressed size(*2)|uncompressed size|
  895. * +---------------+----------------+-------------------+-----------------+
  896. * <--------------------------------(*1)---------------------------------*
  897. *
  898. * +15 +19 +20 +21 +23 +24
  899. * +-----------------+------------+----------------+----------+-----------+
  900. * |data/time(time_t)| 0x20 fixed |header level(=2)|file CRC16| creator |
  901. * +-----------------+------------+----------------+----------+-----------+
  902. * *---------------------------------(*1)---------------------------------*
  903. *
  904. * +24 +26 +26+(*3) +26+(*3)+(*4)
  905. * +----------------+-------------------+-------------+-------------------+
  906. * |next header size|extended header(*3)| padding(*4) | compressed data |
  907. * +----------------+-------------------+-------------+-------------------+
  908. * *--------------------------(*1)-------------------> <------(*2)------->
  909. *
  910. */
  911. #define H2_HEADER_SIZE_OFFSET 0
  912. #define H2_COMP_SIZE_OFFSET 7
  913. #define H2_ORIG_SIZE_OFFSET 11
  914. #define H2_TIME_OFFSET 15
  915. #define H2_CRC_OFFSET 21
  916. #define H2_FIXED_SIZE 24
  917. static int
  918. lha_read_file_header_2(struct archive_read *a, struct lha *lha)
  919. {
  920. const unsigned char *p;
  921. size_t extdsize;
  922. int err, padding;
  923. uint16_t header_crc;
  924. if ((p = __archive_read_ahead(a, H2_FIXED_SIZE, NULL)) == NULL)
  925. return (truncated_error(a));
  926. lha->header_size =archive_le16dec(p + H2_HEADER_SIZE_OFFSET);
  927. lha->compsize = archive_le32dec(p + H2_COMP_SIZE_OFFSET);
  928. lha->origsize = archive_le32dec(p + H2_ORIG_SIZE_OFFSET);
  929. lha->mtime = archive_le32dec(p + H2_TIME_OFFSET);
  930. lha->crc = archive_le16dec(p + H2_CRC_OFFSET);
  931. lha->setflag |= CRC_IS_SET;
  932. if (lha->header_size < H2_FIXED_SIZE) {
  933. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  934. "Invalid LHa header size");
  935. return (ARCHIVE_FATAL);
  936. }
  937. header_crc = lha_crc16(0, p, H2_FIXED_SIZE);
  938. __archive_read_consume(a, H2_FIXED_SIZE);
  939. /* Read extended headers */
  940. err = lha_read_file_extended_header(a, lha, &header_crc, 2,
  941. lha->header_size - H2_FIXED_SIZE, &extdsize);
  942. if (err < ARCHIVE_WARN)
  943. return (err);
  944. /* Calculate a padding size. The result will be normally 0 or 1. */
  945. padding = (int)lha->header_size - (int)(H2_FIXED_SIZE + extdsize);
  946. if (padding > 0) {
  947. if ((p = __archive_read_ahead(a, padding, NULL)) == NULL)
  948. return (truncated_error(a));
  949. header_crc = lha_crc16(header_crc, p, padding);
  950. __archive_read_consume(a, padding);
  951. }
  952. if (header_crc != lha->header_crc) {
  953. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  954. "LHa header CRC error");
  955. return (ARCHIVE_FATAL);
  956. }
  957. return (err);
  958. }
  959. /*
  960. * Header 3 format
  961. *
  962. * +0 +2 +7 +11 +15
  963. * +------------+----------------+-------------------+-----------------+
  964. * | 0x04 fixed |compression type|compressed size(*2)|uncompressed size|
  965. * +------------+----------------+-------------------+-----------------+
  966. * <-------------------------------(*1)-------------------------------*
  967. *
  968. * +15 +19 +20 +21 +23 +24
  969. * +-----------------+------------+----------------+----------+-----------+
  970. * |date/time(time_t)| 0x20 fixed |header level(=3)|file CRC16| creator |
  971. * +-----------------+------------+----------------+----------+-----------+
  972. * *--------------------------------(*1)----------------------------------*
  973. *
  974. * +24 +28 +32 +32+(*3)
  975. * +---------------+----------------+-------------------+-----------------+
  976. * |header size(*1)|next header size|extended header(*3)| compressed data |
  977. * +---------------+----------------+-------------------+-----------------+
  978. * *------------------------(*1)-----------------------> <------(*2)----->
  979. *
  980. */
  981. #define H3_FIELD_LEN_OFFSET 0
  982. #define H3_COMP_SIZE_OFFSET 7
  983. #define H3_ORIG_SIZE_OFFSET 11
  984. #define H3_TIME_OFFSET 15
  985. #define H3_CRC_OFFSET 21
  986. #define H3_HEADER_SIZE_OFFSET 24
  987. #define H3_FIXED_SIZE 28
  988. static int
  989. lha_read_file_header_3(struct archive_read *a, struct lha *lha)
  990. {
  991. const unsigned char *p;
  992. size_t extdsize;
  993. int err;
  994. uint16_t header_crc;
  995. if ((p = __archive_read_ahead(a, H3_FIXED_SIZE, NULL)) == NULL)
  996. return (truncated_error(a));
  997. if (archive_le16dec(p + H3_FIELD_LEN_OFFSET) != 4)
  998. goto invalid;
  999. lha->header_size =archive_le32dec(p + H3_HEADER_SIZE_OFFSET);
  1000. lha->compsize = archive_le32dec(p + H3_COMP_SIZE_OFFSET);
  1001. lha->origsize = archive_le32dec(p + H3_ORIG_SIZE_OFFSET);
  1002. lha->mtime = archive_le32dec(p + H3_TIME_OFFSET);
  1003. lha->crc = archive_le16dec(p + H3_CRC_OFFSET);
  1004. lha->setflag |= CRC_IS_SET;
  1005. if (lha->header_size < H3_FIXED_SIZE + 4)
  1006. goto invalid;
  1007. header_crc = lha_crc16(0, p, H3_FIXED_SIZE);
  1008. __archive_read_consume(a, H3_FIXED_SIZE);
  1009. /* Read extended headers */
  1010. err = lha_read_file_extended_header(a, lha, &header_crc, 4,
  1011. lha->header_size - H3_FIXED_SIZE, &extdsize);
  1012. if (err < ARCHIVE_WARN)
  1013. return (err);
  1014. if (header_crc != lha->header_crc) {
  1015. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1016. "LHa header CRC error");
  1017. return (ARCHIVE_FATAL);
  1018. }
  1019. return (err);
  1020. invalid:
  1021. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1022. "Invalid LHa header");
  1023. return (ARCHIVE_FATAL);
  1024. }
  1025. /*
  1026. * Extended header format
  1027. *
  1028. * +0 +2 +3 -- used in header 1 and 2
  1029. * +0 +4 +5 -- used in header 3
  1030. * +--------------+---------+-------------------+--------------+--
  1031. * |ex-header size|header id| data |ex-header size| .......
  1032. * +--------------+---------+-------------------+--------------+--
  1033. * <-------------( ex-header size)------------> <-- next extended header --*
  1034. *
  1035. * If the ex-header size is zero, it is the make of the end of extended
  1036. * headers.
  1037. *
  1038. */
  1039. static int
  1040. lha_read_file_extended_header(struct archive_read *a, struct lha *lha,
  1041. uint16_t *crc, int sizefield_length, size_t limitsize, size_t *total_size)
  1042. {
  1043. const void *h;
  1044. const unsigned char *extdheader;
  1045. size_t extdsize;
  1046. size_t datasize;
  1047. unsigned int i;
  1048. unsigned char extdtype;
  1049. #define EXT_HEADER_CRC 0x00 /* Header CRC and information*/
  1050. #define EXT_FILENAME 0x01 /* Filename */
  1051. #define EXT_DIRECTORY 0x02 /* Directory name */
  1052. #define EXT_DOS_ATTR 0x40 /* MS-DOS attribute */
  1053. #define EXT_TIMESTAMP 0x41 /* Windows time stamp */
  1054. #define EXT_FILESIZE 0x42 /* Large file size */
  1055. #define EXT_TIMEZONE 0x43 /* Time zone */
  1056. #define EXT_UTF16_FILENAME 0x44 /* UTF-16 filename */
  1057. #define EXT_UTF16_DIRECTORY 0x45 /* UTF-16 directory name */
  1058. #define EXT_CODEPAGE 0x46 /* Codepage */
  1059. #define EXT_UNIX_MODE 0x50 /* File permission */
  1060. #define EXT_UNIX_GID_UID 0x51 /* gid,uid */
  1061. #define EXT_UNIX_GNAME 0x52 /* Group name */
  1062. #define EXT_UNIX_UNAME 0x53 /* User name */
  1063. #define EXT_UNIX_MTIME 0x54 /* Modified time */
  1064. #define EXT_OS2_NEW_ATTR 0x7f /* new attribute(OS/2 only) */
  1065. #define EXT_NEW_ATTR 0xff /* new attribute */
  1066. *total_size = sizefield_length;
  1067. for (;;) {
  1068. /* Read an extended header size. */
  1069. if ((h =
  1070. __archive_read_ahead(a, sizefield_length, NULL)) == NULL)
  1071. return (truncated_error(a));
  1072. /* Check if the size is the zero indicates the end of the
  1073. * extended header. */
  1074. if (sizefield_length == sizeof(uint16_t))
  1075. extdsize = archive_le16dec(h);
  1076. else
  1077. extdsize = archive_le32dec(h);
  1078. if (extdsize == 0) {
  1079. /* End of extended header */
  1080. if (crc != NULL)
  1081. *crc = lha_crc16(*crc, h, sizefield_length);
  1082. __archive_read_consume(a, sizefield_length);
  1083. return (ARCHIVE_OK);
  1084. }
  1085. /* Sanity check to the extended header size. */
  1086. if (((uint64_t)*total_size + extdsize) >
  1087. (uint64_t)limitsize ||
  1088. extdsize <= (size_t)sizefield_length)
  1089. goto invalid;
  1090. /* Read the extended header. */
  1091. if ((h = __archive_read_ahead(a, extdsize, NULL)) == NULL)
  1092. return (truncated_error(a));
  1093. *total_size += extdsize;
  1094. extdheader = (const unsigned char *)h;
  1095. /* Get the extended header type. */
  1096. extdtype = extdheader[sizefield_length];
  1097. /* Calculate an extended data size. */
  1098. datasize = extdsize - (1 + sizefield_length);
  1099. /* Skip an extended header size field and type field. */
  1100. extdheader += sizefield_length + 1;
  1101. if (crc != NULL && extdtype != EXT_HEADER_CRC)
  1102. *crc = lha_crc16(*crc, h, extdsize);
  1103. switch (extdtype) {
  1104. case EXT_HEADER_CRC:
  1105. /* We only use a header CRC. Following data will not
  1106. * be used. */
  1107. if (datasize >= 2) {
  1108. lha->header_crc = archive_le16dec(extdheader);
  1109. if (crc != NULL) {
  1110. static const char zeros[2] = {0, 0};
  1111. *crc = lha_crc16(*crc, h,
  1112. extdsize - datasize);
  1113. /* CRC value itself as zero */
  1114. *crc = lha_crc16(*crc, zeros, 2);
  1115. *crc = lha_crc16(*crc,
  1116. extdheader+2, datasize - 2);
  1117. }
  1118. }
  1119. break;
  1120. case EXT_FILENAME:
  1121. if (datasize == 0) {
  1122. /* maybe directory header */
  1123. archive_string_empty(&lha->filename);
  1124. break;
  1125. }
  1126. archive_strncpy(&lha->filename,
  1127. (const char *)extdheader, datasize);
  1128. break;
  1129. case EXT_DIRECTORY:
  1130. if (datasize == 0)
  1131. /* no directory name data. exit this case. */
  1132. break;
  1133. archive_strncpy(&lha->dirname,
  1134. (const char *)extdheader, datasize);
  1135. /*
  1136. * Convert directory delimiter from 0xFF
  1137. * to '/' for local system.
  1138. */
  1139. for (i = 0; i < lha->dirname.length; i++) {
  1140. if ((unsigned char)lha->dirname.s[i] == 0xFF)
  1141. lha->dirname.s[i] = '/';
  1142. }
  1143. /* Is last character directory separator? */
  1144. if (lha->dirname.s[lha->dirname.length-1] != '/')
  1145. /* invalid directory data */
  1146. goto invalid;
  1147. break;
  1148. case EXT_DOS_ATTR:
  1149. if (datasize == 2)
  1150. lha->dos_attr = (unsigned char)
  1151. (archive_le16dec(extdheader) & 0xff);
  1152. break;
  1153. case EXT_TIMESTAMP:
  1154. if (datasize == (sizeof(uint64_t) * 3)) {
  1155. lha->birthtime = lha_win_time(
  1156. archive_le64dec(extdheader),
  1157. &lha->birthtime_tv_nsec);
  1158. extdheader += sizeof(uint64_t);
  1159. lha->mtime = lha_win_time(
  1160. archive_le64dec(extdheader),
  1161. &lha->mtime_tv_nsec);
  1162. extdheader += sizeof(uint64_t);
  1163. lha->atime = lha_win_time(
  1164. archive_le64dec(extdheader),
  1165. &lha->atime_tv_nsec);
  1166. lha->setflag |= BIRTHTIME_IS_SET |
  1167. ATIME_IS_SET;
  1168. }
  1169. break;
  1170. case EXT_FILESIZE:
  1171. if (datasize == sizeof(uint64_t) * 2) {
  1172. lha->compsize = archive_le64dec(extdheader);
  1173. extdheader += sizeof(uint64_t);
  1174. lha->origsize = archive_le64dec(extdheader);
  1175. }
  1176. break;
  1177. case EXT_CODEPAGE:
  1178. /* Get an archived filename charset from codepage.
  1179. * This overwrites the charset specified by
  1180. * hdrcharset option. */
  1181. if (datasize == sizeof(uint32_t)) {
  1182. struct archive_string cp;
  1183. const char *charset;
  1184. archive_string_init(&cp);
  1185. switch (archive_le32dec(extdheader)) {
  1186. case 65001: /* UTF-8 */
  1187. charset = "UTF-8";
  1188. break;
  1189. default:
  1190. archive_string_sprintf(&cp, "CP%d",
  1191. (int)archive_le32dec(extdheader));
  1192. charset = cp.s;
  1193. break;
  1194. }
  1195. lha->sconv =
  1196. archive_string_conversion_from_charset(
  1197. &(a->archive), charset, 1);
  1198. archive_string_free(&cp);
  1199. if (lha->sconv == NULL)
  1200. return (ARCHIVE_FATAL);
  1201. }
  1202. break;
  1203. case EXT_UNIX_MODE:
  1204. if (datasize == sizeof(uint16_t)) {
  1205. lha->mode = archive_le16dec(extdheader);
  1206. lha->setflag |= UNIX_MODE_IS_SET;
  1207. }
  1208. break;
  1209. case EXT_UNIX_GID_UID:
  1210. if (datasize == (sizeof(uint16_t) * 2)) {
  1211. lha->gid = archive_le16dec(extdheader);
  1212. lha->uid = archive_le16dec(extdheader+2);
  1213. }
  1214. break;
  1215. case EXT_UNIX_GNAME:
  1216. if (datasize > 0)
  1217. archive_strncpy(&lha->gname,
  1218. (const char *)extdheader, datasize);
  1219. break;
  1220. case EXT_UNIX_UNAME:
  1221. if (datasize > 0)
  1222. archive_strncpy(&lha->uname,
  1223. (const char *)extdheader, datasize);
  1224. break;
  1225. case EXT_UNIX_MTIME:
  1226. if (datasize == sizeof(uint32_t))
  1227. lha->mtime = archive_le32dec(extdheader);
  1228. break;
  1229. case EXT_OS2_NEW_ATTR:
  1230. /* This extended header is OS/2 depend. */
  1231. if (datasize == 16) {
  1232. lha->dos_attr = (unsigned char)
  1233. (archive_le16dec(extdheader) & 0xff);
  1234. lha->mode = archive_le16dec(extdheader+2);
  1235. lha->gid = archive_le16dec(extdheader+4);
  1236. lha->uid = archive_le16dec(extdheader+6);
  1237. lha->birthtime = archive_le32dec(extdheader+8);
  1238. lha->atime = archive_le32dec(extdheader+12);
  1239. lha->setflag |= UNIX_MODE_IS_SET
  1240. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1241. }
  1242. break;
  1243. case EXT_NEW_ATTR:
  1244. if (datasize == 20) {
  1245. lha->mode = (mode_t)archive_le32dec(extdheader);
  1246. lha->gid = archive_le32dec(extdheader+4);
  1247. lha->uid = archive_le32dec(extdheader+8);
  1248. lha->birthtime = archive_le32dec(extdheader+12);
  1249. lha->atime = archive_le32dec(extdheader+16);
  1250. lha->setflag |= UNIX_MODE_IS_SET
  1251. | BIRTHTIME_IS_SET | ATIME_IS_SET;
  1252. }
  1253. break;
  1254. case EXT_TIMEZONE: /* Not supported */
  1255. case EXT_UTF16_FILENAME: /* Not supported */
  1256. case EXT_UTF16_DIRECTORY: /* Not supported */
  1257. default:
  1258. break;
  1259. }
  1260. __archive_read_consume(a, extdsize);
  1261. }
  1262. invalid:
  1263. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1264. "Invalid extended LHa header");
  1265. return (ARCHIVE_FATAL);
  1266. }
  1267. static int
  1268. archive_read_format_lha_read_data(struct archive_read *a,
  1269. const void **buff, size_t *size, int64_t *offset)
  1270. {
  1271. struct lha *lha = (struct lha *)(a->format->data);
  1272. int r;
  1273. if (lha->entry_unconsumed) {
  1274. /* Consume as much as the decompressor actually used. */
  1275. __archive_read_consume(a, lha->entry_unconsumed);
  1276. lha->entry_unconsumed = 0;
  1277. }
  1278. if (lha->end_of_entry) {
  1279. if (!lha->end_of_entry_cleanup) {
  1280. if ((lha->setflag & CRC_IS_SET) &&
  1281. lha->crc != lha->entry_crc_calculated) {
  1282. archive_set_error(&a->archive,
  1283. ARCHIVE_ERRNO_MISC,
  1284. "LHa data CRC error");
  1285. return (ARCHIVE_WARN);
  1286. }
  1287. /* End-of-entry cleanup done. */
  1288. lha->end_of_entry_cleanup = 1;
  1289. }
  1290. *offset = lha->entry_offset;
  1291. *size = 0;
  1292. *buff = NULL;
  1293. return (ARCHIVE_EOF);
  1294. }
  1295. if (lha->entry_is_compressed)
  1296. r = lha_read_data_lzh(a, buff, size, offset);
  1297. else
  1298. /* No compression. */
  1299. r = lha_read_data_none(a, buff, size, offset);
  1300. return (r);
  1301. }
  1302. /*
  1303. * Read a file content in no compression.
  1304. *
  1305. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1306. * lha->end_of_entry if it consumes all of the data.
  1307. */
  1308. static int
  1309. lha_read_data_none(struct archive_read *a, const void **buff,
  1310. size_t *size, int64_t *offset)
  1311. {
  1312. struct lha *lha = (struct lha *)(a->format->data);
  1313. ssize_t bytes_avail;
  1314. if (lha->entry_bytes_remaining == 0) {
  1315. *buff = NULL;
  1316. *size = 0;
  1317. *offset = lha->entry_offset;
  1318. lha->end_of_entry = 1;
  1319. return (ARCHIVE_OK);
  1320. }
  1321. /*
  1322. * Note: '1' here is a performance optimization.
  1323. * Recall that the decompression layer returns a count of
  1324. * available bytes; asking for more than that forces the
  1325. * decompressor to combine reads by copying data.
  1326. */
  1327. *buff = __archive_read_ahead(a, 1, &bytes_avail);
  1328. if (bytes_avail <= 0) {
  1329. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1330. "Truncated LHa file data");
  1331. return (ARCHIVE_FATAL);
  1332. }
  1333. if (bytes_avail > lha->entry_bytes_remaining)
  1334. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1335. lha->entry_crc_calculated =
  1336. lha_crc16(lha->entry_crc_calculated, *buff, bytes_avail);
  1337. *size = bytes_avail;
  1338. *offset = lha->entry_offset;
  1339. lha->entry_offset += bytes_avail;
  1340. lha->entry_bytes_remaining -= bytes_avail;
  1341. if (lha->entry_bytes_remaining == 0)
  1342. lha->end_of_entry = 1;
  1343. lha->entry_unconsumed = bytes_avail;
  1344. return (ARCHIVE_OK);
  1345. }
  1346. /*
  1347. * Read a file content in LZHUFF encoding.
  1348. *
  1349. * Returns ARCHIVE_OK if successful, returns ARCHIVE_WARN if compression is
  1350. * unsupported, ARCHIVE_FATAL otherwise, sets lha->end_of_entry if it consumes
  1351. * all of the data.
  1352. */
  1353. static int
  1354. lha_read_data_lzh(struct archive_read *a, const void **buff,
  1355. size_t *size, int64_t *offset)
  1356. {
  1357. struct lha *lha = (struct lha *)(a->format->data);
  1358. ssize_t bytes_avail;
  1359. int r;
  1360. /* If the buffer hasn't been allocated, allocate it now. */
  1361. if (lha->uncompressed_buffer == NULL) {
  1362. lha->uncompressed_buffer_size = 64 * 1024;
  1363. lha->uncompressed_buffer
  1364. = (unsigned char *)malloc(lha->uncompressed_buffer_size);
  1365. if (lha->uncompressed_buffer == NULL) {
  1366. archive_set_error(&a->archive, ENOMEM,
  1367. "No memory for lzh decompression");
  1368. return (ARCHIVE_FATAL);
  1369. }
  1370. }
  1371. /* If we haven't yet read any data, initialize the decompressor. */
  1372. if (!lha->decompress_init) {
  1373. r = lzh_decode_init(&(lha->strm), lha->method);
  1374. switch (r) {
  1375. case ARCHIVE_OK:
  1376. break;
  1377. case ARCHIVE_FAILED:
  1378. /* Unsupported compression. */
  1379. *buff = NULL;
  1380. *size = 0;
  1381. *offset = 0;
  1382. archive_set_error(&a->archive,
  1383. ARCHIVE_ERRNO_FILE_FORMAT,
  1384. "Unsupported lzh compression method -%c%c%c-",
  1385. lha->method[0], lha->method[1], lha->method[2]);
  1386. /* We know compressed size; just skip it. */
  1387. archive_read_format_lha_read_data_skip(a);
  1388. return (ARCHIVE_WARN);
  1389. default:
  1390. archive_set_error(&a->archive, ENOMEM,
  1391. "Couldn't allocate memory "
  1392. "for lzh decompression");
  1393. return (ARCHIVE_FATAL);
  1394. }
  1395. /* We've initialized decompression for this stream. */
  1396. lha->decompress_init = 1;
  1397. lha->strm.avail_out = 0;
  1398. lha->strm.total_out = 0;
  1399. }
  1400. /*
  1401. * Note: '1' here is a performance optimization.
  1402. * Recall that the decompression layer returns a count of
  1403. * available bytes; asking for more than that forces the
  1404. * decompressor to combine reads by copying data.
  1405. */
  1406. lha->strm.next_in = __archive_read_ahead(a, 1, &bytes_avail);
  1407. if (bytes_avail <= 0) {
  1408. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1409. "Truncated LHa file body");
  1410. return (ARCHIVE_FATAL);
  1411. }
  1412. if (bytes_avail > lha->entry_bytes_remaining)
  1413. bytes_avail = (ssize_t)lha->entry_bytes_remaining;
  1414. lha->strm.avail_in = bytes_avail;
  1415. lha->strm.total_in = 0;
  1416. if (lha->strm.avail_out == 0) {
  1417. lha->strm.next_out = lha->uncompressed_buffer;
  1418. lha->strm.avail_out = lha->uncompressed_buffer_size;
  1419. }
  1420. r = lzh_decode(&(lha->strm), bytes_avail == lha->entry_bytes_remaining);
  1421. switch (r) {
  1422. case ARCHIVE_OK:
  1423. break;
  1424. case ARCHIVE_EOF:
  1425. lha->end_of_entry = 1;
  1426. break;
  1427. default:
  1428. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1429. "Bad lzh data");
  1430. return (ARCHIVE_FAILED);
  1431. }
  1432. lha->entry_unconsumed = lha->strm.total_in;
  1433. lha->entry_bytes_remaining -= lha->strm.total_in;
  1434. if (lha->strm.avail_out == 0 || lha->end_of_entry) {
  1435. *offset = lha->entry_offset;
  1436. *size = lha->strm.next_out - lha->uncompressed_buffer;
  1437. *buff = lha->uncompressed_buffer;
  1438. lha->entry_crc_calculated =
  1439. lha_crc16(lha->entry_crc_calculated, *buff, *size);
  1440. lha->entry_offset += *size;
  1441. } else {
  1442. *offset = lha->entry_offset;
  1443. *size = 0;
  1444. *buff = NULL;
  1445. }
  1446. return (ARCHIVE_OK);
  1447. }
  1448. /*
  1449. * Skip a file content.
  1450. */
  1451. static int
  1452. archive_read_format_lha_read_data_skip(struct archive_read *a)
  1453. {
  1454. struct lha *lha;
  1455. int64_t bytes_skipped;
  1456. lha = (struct lha *)(a->format->data);
  1457. if (lha->entry_unconsumed) {
  1458. /* Consume as much as the decompressor actually used. */
  1459. __archive_read_consume(a, lha->entry_unconsumed);
  1460. lha->entry_unconsumed = 0;
  1461. }
  1462. /* if we've already read to end of data, we're done. */
  1463. if (lha->end_of_entry_cleanup)
  1464. return (ARCHIVE_OK);
  1465. /*
  1466. * If the length is at the beginning, we can skip the
  1467. * compressed data much more quickly.
  1468. */
  1469. bytes_skipped = __archive_read_consume(a, lha->entry_bytes_remaining);
  1470. if (bytes_skipped < 0)
  1471. return (ARCHIVE_FATAL);
  1472. /* This entry is finished and done. */
  1473. lha->end_of_entry_cleanup = lha->end_of_entry = 1;
  1474. return (ARCHIVE_OK);
  1475. }
  1476. static int
  1477. archive_read_format_lha_cleanup(struct archive_read *a)
  1478. {
  1479. struct lha *lha = (struct lha *)(a->format->data);
  1480. lzh_decode_free(&(lha->strm));
  1481. free(lha->uncompressed_buffer);
  1482. archive_string_free(&(lha->dirname));
  1483. archive_string_free(&(lha->filename));
  1484. archive_string_free(&(lha->uname));
  1485. archive_string_free(&(lha->gname));
  1486. archive_wstring_free(&(lha->ws));
  1487. free(lha);
  1488. (a->format->data) = NULL;
  1489. return (ARCHIVE_OK);
  1490. }
  1491. /*
  1492. * 'LHa for UNIX' utility has archived a symbolic-link name after
  1493. * a pathname with '|' character.
  1494. * This function extracts the symbolic-link name from the pathname.
  1495. *
  1496. * example.
  1497. * 1. a symbolic-name is 'aaa/bb/cc'
  1498. * 2. a filename is 'xxx/bbb'
  1499. * then a archived pathname is 'xxx/bbb|aaa/bb/cc'
  1500. */
  1501. static int
  1502. lha_parse_linkname(struct archive_string *linkname,
  1503. struct archive_string *pathname)
  1504. {
  1505. char * linkptr;
  1506. size_t symlen;
  1507. linkptr = strchr(pathname->s, '|');
  1508. if (linkptr != NULL) {
  1509. symlen = strlen(linkptr + 1);
  1510. archive_strncpy(linkname, linkptr+1, symlen);
  1511. *linkptr = 0;
  1512. pathname->length = strlen(pathname->s);
  1513. return (1);
  1514. }
  1515. return (0);
  1516. }
  1517. /* Convert an MSDOS-style date/time into Unix-style time. */
  1518. static time_t
  1519. lha_dos_time(const unsigned char *p)
  1520. {
  1521. int msTime, msDate;
  1522. struct tm ts;
  1523. msTime = archive_le16dec(p);
  1524. msDate = archive_le16dec(p+2);
  1525. memset(&ts, 0, sizeof(ts));
  1526. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  1527. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  1528. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  1529. ts.tm_hour = (msTime >> 11) & 0x1f;
  1530. ts.tm_min = (msTime >> 5) & 0x3f;
  1531. ts.tm_sec = (msTime << 1) & 0x3e;
  1532. ts.tm_isdst = -1;
  1533. return (mktime(&ts));
  1534. }
  1535. /* Convert an MS-Windows-style date/time into Unix-style time. */
  1536. static time_t
  1537. lha_win_time(uint64_t wintime, long *ns)
  1538. {
  1539. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  1540. if (wintime >= EPOC_TIME) {
  1541. wintime -= EPOC_TIME; /* 1970-01-01 00:00:00 (UTC) */
  1542. if (ns != NULL)
  1543. *ns = (long)(wintime % 10000000) * 100;
  1544. return (wintime / 10000000);
  1545. } else {
  1546. if (ns != NULL)
  1547. *ns = 0;
  1548. return (0);
  1549. }
  1550. }
  1551. static unsigned char
  1552. lha_calcsum(unsigned char sum, const void *pp, int offset, size_t size)
  1553. {
  1554. unsigned char const *p = (unsigned char const *)pp;
  1555. p += offset;
  1556. for (;size > 0; --size)
  1557. sum += *p++;
  1558. return (sum);
  1559. }
  1560. #define CRC16(crc, v) do { \
  1561. (crc) = crc16tbl[((crc) ^ v) & 0xFF] ^ ((crc) >> 8); \
  1562. } while (0)
  1563. static uint16_t
  1564. lha_crc16(uint16_t crc, const void *pp, size_t len)
  1565. {
  1566. const unsigned char *buff = (const unsigned char *)pp;
  1567. while (len >= 8) {
  1568. CRC16(crc, *buff++); CRC16(crc, *buff++);
  1569. CRC16(crc, *buff++); CRC16(crc, *buff++);
  1570. CRC16(crc, *buff++); CRC16(crc, *buff++);
  1571. CRC16(crc, *buff++); CRC16(crc, *buff++);
  1572. len -= 8;
  1573. }
  1574. switch (len) {
  1575. case 7:
  1576. CRC16(crc, *buff++);
  1577. /* FALL THROUGH */
  1578. case 6:
  1579. CRC16(crc, *buff++);
  1580. /* FALL THROUGH */
  1581. case 5:
  1582. CRC16(crc, *buff++);
  1583. /* FALL THROUGH */
  1584. case 4:
  1585. CRC16(crc, *buff++);
  1586. /* FALL THROUGH */
  1587. case 3:
  1588. CRC16(crc, *buff++);
  1589. /* FALL THROUGH */
  1590. case 2:
  1591. CRC16(crc, *buff++);
  1592. /* FALL THROUGH */
  1593. case 1:
  1594. CRC16(crc, *buff);
  1595. /* FALL THROUGH */
  1596. case 0:
  1597. break;
  1598. }
  1599. return (crc);
  1600. }
  1601. /*
  1602. * Initialize LZHUF decoder.
  1603. *
  1604. * Returns ARCHIVE_OK if initialization was successful.
  1605. * Returns ARCHIVE_FAILED if method is unsupported.
  1606. * Returns ARCHIVE_FATAL if initialization failed; memory allocation
  1607. * error occurred.
  1608. */
  1609. static int
  1610. lzh_decode_init(struct lzh_stream *strm, const char *method)
  1611. {
  1612. struct lzh_dec *ds;
  1613. int w_bits, w_size;
  1614. if (strm->ds == NULL) {
  1615. strm->ds = calloc(1, sizeof(*strm->ds));
  1616. if (strm->ds == NULL)
  1617. return (ARCHIVE_FATAL);
  1618. }
  1619. ds = strm->ds;
  1620. ds->error = ARCHIVE_FAILED;
  1621. if (method == NULL || method[0] != 'l' || method[1] != 'h')
  1622. return (ARCHIVE_FAILED);
  1623. switch (method[2]) {
  1624. case '5':
  1625. w_bits = 13;/* 8KiB for window */
  1626. break;
  1627. case '6':
  1628. w_bits = 15;/* 32KiB for window */
  1629. break;
  1630. case '7':
  1631. w_bits = 16;/* 64KiB for window */
  1632. break;
  1633. default:
  1634. return (ARCHIVE_FAILED);/* Not supported. */
  1635. }
  1636. ds->error = ARCHIVE_FATAL;
  1637. w_size = ds->w_size;
  1638. ds->w_size = 1U << w_bits;
  1639. ds->w_mask = ds->w_size -1;
  1640. if (ds->w_buff == NULL || w_size != ds->w_size) {
  1641. free(ds->w_buff);
  1642. ds->w_buff = malloc(ds->w_size);
  1643. if (ds->w_buff == NULL)
  1644. return (ARCHIVE_FATAL);
  1645. }
  1646. memset(ds->w_buff, 0x20, ds->w_size);
  1647. ds->w_pos = 0;
  1648. ds->w_remaining = 0;
  1649. ds->state = 0;
  1650. ds->pos_pt_len_size = w_bits + 1;
  1651. ds->pos_pt_len_bits = (w_bits == 15 || w_bits == 16)? 5: 4;
  1652. ds->literal_pt_len_size = PT_BITLEN_SIZE;
  1653. ds->literal_pt_len_bits = 5;
  1654. ds->br.cache_buffer = 0;
  1655. ds->br.cache_avail = 0;
  1656. if (lzh_huffman_init(&(ds->lt), LT_BITLEN_SIZE, 16)
  1657. != ARCHIVE_OK)
  1658. return (ARCHIVE_FATAL);
  1659. ds->lt.len_bits = 9;
  1660. if (lzh_huffman_init(&(ds->pt), PT_BITLEN_SIZE, 16)
  1661. != ARCHIVE_OK)
  1662. return (ARCHIVE_FATAL);
  1663. ds->error = 0;
  1664. return (ARCHIVE_OK);
  1665. }
  1666. /*
  1667. * Release LZHUF decoder.
  1668. */
  1669. static void
  1670. lzh_decode_free(struct lzh_stream *strm)
  1671. {
  1672. if (strm->ds == NULL)
  1673. return;
  1674. free(strm->ds->w_buff);
  1675. lzh_huffman_free(&(strm->ds->lt));
  1676. lzh_huffman_free(&(strm->ds->pt));
  1677. free(strm->ds);
  1678. strm->ds = NULL;
  1679. }
  1680. /*
  1681. * Bit stream reader.
  1682. */
  1683. /* Check that the cache buffer has enough bits. */
  1684. #define lzh_br_has(br, n) ((br)->cache_avail >= n)
  1685. /* Get compressed data by bit. */
  1686. #define lzh_br_bits(br, n) \
  1687. (((uint16_t)((br)->cache_buffer >> \
  1688. ((br)->cache_avail - (n)))) & cache_masks[n])
  1689. #define lzh_br_bits_forced(br, n) \
  1690. (((uint16_t)((br)->cache_buffer << \
  1691. ((n) - (br)->cache_avail))) & cache_masks[n])
  1692. /* Read ahead to make sure the cache buffer has enough compressed data we
  1693. * will use.
  1694. * True : completed, there is enough data in the cache buffer.
  1695. * False : we met that strm->next_in is empty, we have to get following
  1696. * bytes. */
  1697. #define lzh_br_read_ahead_0(strm, br, n) \
  1698. (lzh_br_has(br, (n)) || lzh_br_fillup(strm, br))
  1699. /* True : the cache buffer has some bits as much as we need.
  1700. * False : there are no enough bits in the cache buffer to be used,
  1701. * we have to get following bytes if we could. */
  1702. #define lzh_br_read_ahead(strm, br, n) \
  1703. (lzh_br_read_ahead_0((strm), (br), (n)) || lzh_br_has((br), (n)))
  1704. /* Notify how many bits we consumed. */
  1705. #define lzh_br_consume(br, n) ((br)->cache_avail -= (n))
  1706. #define lzh_br_unconsume(br, n) ((br)->cache_avail += (n))
  1707. static const uint16_t cache_masks[] = {
  1708. 0x0000, 0x0001, 0x0003, 0x0007,
  1709. 0x000F, 0x001F, 0x003F, 0x007F,
  1710. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  1711. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
  1712. 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF
  1713. };
  1714. /*
  1715. * Shift away used bits in the cache data and fill it up with following bits.
  1716. * Call this when cache buffer does not have enough bits you need.
  1717. *
  1718. * Returns 1 if the cache buffer is full.
  1719. * Returns 0 if the cache buffer is not full; input buffer is empty.
  1720. */
  1721. static int
  1722. lzh_br_fillup(struct lzh_stream *strm, struct lzh_br *br)
  1723. {
  1724. int n = CACHE_BITS - br->cache_avail;
  1725. for (;;) {
  1726. switch (n >> 3) {
  1727. case 8:
  1728. if (strm->avail_in >= 8) {
  1729. br->cache_buffer =
  1730. ((uint64_t)strm->next_in[0]) << 56 |
  1731. ((uint64_t)strm->next_in[1]) << 48 |
  1732. ((uint64_t)strm->next_in[2]) << 40 |
  1733. ((uint64_t)strm->next_in[3]) << 32 |
  1734. ((uint32_t)strm->next_in[4]) << 24 |
  1735. ((uint32_t)strm->next_in[5]) << 16 |
  1736. ((uint32_t)strm->next_in[6]) << 8 |
  1737. (uint32_t)strm->next_in[7];
  1738. strm->next_in += 8;
  1739. strm->avail_in -= 8;
  1740. br->cache_avail += 8 * 8;
  1741. return (1);
  1742. }
  1743. break;
  1744. case 7:
  1745. if (strm->avail_in >= 7) {
  1746. br->cache_buffer =
  1747. (br->cache_buffer << 56) |
  1748. ((uint64_t)strm->next_in[0]) << 48 |
  1749. ((uint64_t)strm->next_in[1]) << 40 |
  1750. ((uint64_t)strm->next_in[2]) << 32 |
  1751. ((uint32_t)strm->next_in[3]) << 24 |
  1752. ((uint32_t)strm->next_in[4]) << 16 |
  1753. ((uint32_t)strm->next_in[5]) << 8 |
  1754. (uint32_t)strm->next_in[6];
  1755. strm->next_in += 7;
  1756. strm->avail_in -= 7;
  1757. br->cache_avail += 7 * 8;
  1758. return (1);
  1759. }
  1760. break;
  1761. case 6:
  1762. if (strm->avail_in >= 6) {
  1763. br->cache_buffer =
  1764. (br->cache_buffer << 48) |
  1765. ((uint64_t)strm->next_in[0]) << 40 |
  1766. ((uint64_t)strm->next_in[1]) << 32 |
  1767. ((uint32_t)strm->next_in[2]) << 24 |
  1768. ((uint32_t)strm->next_in[3]) << 16 |
  1769. ((uint32_t)strm->next_in[4]) << 8 |
  1770. (uint32_t)strm->next_in[5];
  1771. strm->next_in += 6;
  1772. strm->avail_in -= 6;
  1773. br->cache_avail += 6 * 8;
  1774. return (1);
  1775. }
  1776. break;
  1777. case 0:
  1778. /* We have enough compressed data in
  1779. * the cache buffer.*/
  1780. return (1);
  1781. default:
  1782. break;
  1783. }
  1784. if (strm->avail_in == 0) {
  1785. /* There is not enough compressed data to fill up the
  1786. * cache buffer. */
  1787. return (0);
  1788. }
  1789. br->cache_buffer =
  1790. (br->cache_buffer << 8) | *strm->next_in++;
  1791. strm->avail_in--;
  1792. br->cache_avail += 8;
  1793. n -= 8;
  1794. }
  1795. }
  1796. /*
  1797. * Decode LZHUF.
  1798. *
  1799. * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
  1800. * Please set available buffer and call this function again.
  1801. * 2. Returns ARCHIVE_EOF if decompression has been completed.
  1802. * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
  1803. * is broken or you do not set 'last' flag properly.
  1804. * 4. 'last' flag is very important, you must set 1 to the flag if there
  1805. * is no input data. The lha compressed data format does not provide how
  1806. * to know the compressed data is really finished.
  1807. * Note: lha command utility check if the total size of output bytes is
  1808. * reached the uncompressed size recorded in its header. it does not mind
  1809. * that the decoding process is properly finished.
  1810. * GNU ZIP can decompress another compressed file made by SCO LZH compress.
  1811. * it handles EOF as null to fill read buffer with zero until the decoding
  1812. * process meet 2 bytes of zeros at reading a size of a next chunk, so the
  1813. * zeros are treated as the mark of the end of the data although the zeros
  1814. * is dummy, not the file data.
  1815. */
  1816. static int lzh_read_blocks(struct lzh_stream *, int);
  1817. static int lzh_decode_blocks(struct lzh_stream *, int);
  1818. #define ST_RD_BLOCK 0
  1819. #define ST_RD_PT_1 1
  1820. #define ST_RD_PT_2 2
  1821. #define ST_RD_PT_3 3
  1822. #define ST_RD_PT_4 4
  1823. #define ST_RD_LITERAL_1 5
  1824. #define ST_RD_LITERAL_2 6
  1825. #define ST_RD_LITERAL_3 7
  1826. #define ST_RD_POS_DATA_1 8
  1827. #define ST_GET_LITERAL 9
  1828. #define ST_GET_POS_1 10
  1829. #define ST_GET_POS_2 11
  1830. #define ST_COPY_DATA 12
  1831. static int
  1832. lzh_decode(struct lzh_stream *strm, int last)
  1833. {
  1834. struct lzh_dec *ds = strm->ds;
  1835. int64_t avail_in;
  1836. int r;
  1837. if (ds->error)
  1838. return (ds->error);
  1839. avail_in = strm->avail_in;
  1840. do {
  1841. if (ds->state < ST_GET_LITERAL)
  1842. r = lzh_read_blocks(strm, last);
  1843. else
  1844. r = lzh_decode_blocks(strm, last);
  1845. } while (r == 100);
  1846. strm->total_in += avail_in - strm->avail_in;
  1847. return (r);
  1848. }
  1849. static int
  1850. lzh_copy_from_window(struct lzh_stream *strm, struct lzh_dec *ds)
  1851. {
  1852. size_t copy_bytes;
  1853. if (ds->w_remaining == 0 && ds->w_pos > 0) {
  1854. if (ds->w_pos - ds->copy_pos <= strm->avail_out)
  1855. copy_bytes = ds->w_pos - ds->copy_pos;
  1856. else
  1857. copy_bytes = (size_t)strm->avail_out;
  1858. memcpy(strm->next_out,
  1859. ds->w_buff + ds->copy_pos, copy_bytes);
  1860. ds->copy_pos += (int)copy_bytes;
  1861. } else {
  1862. if (ds->w_remaining <= strm->avail_out)
  1863. copy_bytes = ds->w_remaining;
  1864. else
  1865. copy_bytes = (size_t)strm->avail_out;
  1866. memcpy(strm->next_out,
  1867. ds->w_buff + ds->w_size - ds->w_remaining, copy_bytes);
  1868. ds->w_remaining -= (int)copy_bytes;
  1869. }
  1870. strm->next_out += copy_bytes;
  1871. strm->avail_out -= copy_bytes;
  1872. strm->total_out += copy_bytes;
  1873. if (strm->avail_out == 0)
  1874. return (0);
  1875. else
  1876. return (1);
  1877. }
  1878. static int
  1879. lzh_read_blocks(struct lzh_stream *strm, int last)
  1880. {
  1881. struct lzh_dec *ds = strm->ds;
  1882. struct lzh_br *br = &(ds->br);
  1883. int c = 0, i;
  1884. unsigned rbits;
  1885. for (;;) {
  1886. switch (ds->state) {
  1887. case ST_RD_BLOCK:
  1888. /*
  1889. * Read a block number indicates how many blocks
  1890. * we will handle. The block is composed of a
  1891. * literal and a match, sometimes a literal only
  1892. * in particular, there are no reference data at
  1893. * the beginning of the decompression.
  1894. */
  1895. if (!lzh_br_read_ahead_0(strm, br, 16)) {
  1896. if (!last)
  1897. /* We need following data. */
  1898. return (ARCHIVE_OK);
  1899. if (lzh_br_has(br, 8)) {
  1900. /*
  1901. * It seems there are extra bits.
  1902. * 1. Compressed data is broken.
  1903. * 2. `last' flag does not properly
  1904. * set.
  1905. */
  1906. goto failed;
  1907. }
  1908. if (ds->w_pos > 0) {
  1909. if (!lzh_copy_from_window(strm, ds))
  1910. return (ARCHIVE_OK);
  1911. }
  1912. /* End of compressed data; we have completely
  1913. * handled all compressed data. */
  1914. return (ARCHIVE_EOF);
  1915. }
  1916. ds->blocks_avail = lzh_br_bits(br, 16);
  1917. if (ds->blocks_avail == 0)
  1918. goto failed;
  1919. lzh_br_consume(br, 16);
  1920. /*
  1921. * Read a literal table compressed in huffman
  1922. * coding.
  1923. */
  1924. ds->pt.len_size = ds->literal_pt_len_size;
  1925. ds->pt.len_bits = ds->literal_pt_len_bits;
  1926. ds->reading_position = 0;
  1927. /* FALL THROUGH */
  1928. case ST_RD_PT_1:
  1929. /* Note: ST_RD_PT_1, ST_RD_PT_2 and ST_RD_PT_4 are
  1930. * used in reading both a literal table and a
  1931. * position table. */
  1932. if (!lzh_br_read_ahead(strm, br, ds->pt.len_bits)) {
  1933. if (last)
  1934. goto failed;/* Truncated data. */
  1935. ds->state = ST_RD_PT_1;
  1936. return (ARCHIVE_OK);
  1937. }
  1938. ds->pt.len_avail = lzh_br_bits(br, ds->pt.len_bits);
  1939. lzh_br_consume(br, ds->pt.len_bits);
  1940. /* FALL THROUGH */
  1941. case ST_RD_PT_2:
  1942. if (ds->pt.len_avail == 0) {
  1943. /* There is no bitlen. */
  1944. if (!lzh_br_read_ahead(strm, br,
  1945. ds->pt.len_bits)) {
  1946. if (last)
  1947. goto failed;/* Truncated data.*/
  1948. ds->state = ST_RD_PT_2;
  1949. return (ARCHIVE_OK);
  1950. }
  1951. if (!lzh_make_fake_table(&(ds->pt),
  1952. lzh_br_bits(br, ds->pt.len_bits)))
  1953. goto failed;/* Invalid data. */
  1954. lzh_br_consume(br, ds->pt.len_bits);
  1955. if (ds->reading_position)
  1956. ds->state = ST_GET_LITERAL;
  1957. else
  1958. ds->state = ST_RD_LITERAL_1;
  1959. break;
  1960. } else if (ds->pt.len_avail > ds->pt.len_size)
  1961. goto failed;/* Invalid data. */
  1962. ds->loop = 0;
  1963. memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
  1964. if (ds->pt.len_avail < 3 ||
  1965. ds->pt.len_size == ds->pos_pt_len_size) {
  1966. ds->state = ST_RD_PT_4;
  1967. break;
  1968. }
  1969. /* FALL THROUGH */
  1970. case ST_RD_PT_3:
  1971. ds->loop = lzh_read_pt_bitlen(strm, ds->loop, 3);
  1972. if (ds->loop < 3) {
  1973. if (ds->loop < 0 || last)
  1974. goto failed;/* Invalid data. */
  1975. /* Not completed, get following data. */
  1976. ds->state = ST_RD_PT_3;
  1977. return (ARCHIVE_OK);
  1978. }
  1979. /* There are some null in bitlen of the literal. */
  1980. if (!lzh_br_read_ahead(strm, br, 2)) {
  1981. if (last)
  1982. goto failed;/* Truncated data. */
  1983. ds->state = ST_RD_PT_3;
  1984. return (ARCHIVE_OK);
  1985. }
  1986. c = lzh_br_bits(br, 2);
  1987. lzh_br_consume(br, 2);
  1988. if (c > ds->pt.len_avail - 3)
  1989. goto failed;/* Invalid data. */
  1990. for (i = 3; c-- > 0 ;)
  1991. ds->pt.bitlen[i++] = 0;
  1992. ds->loop = i;
  1993. /* FALL THROUGH */
  1994. case ST_RD_PT_4:
  1995. ds->loop = lzh_read_pt_bitlen(strm, ds->loop,
  1996. ds->pt.len_avail);
  1997. if (ds->loop < ds->pt.len_avail) {
  1998. if (ds->loop < 0 || last)
  1999. goto failed;/* Invalid data. */
  2000. /* Not completed, get following data. */
  2001. ds->state = ST_RD_PT_4;
  2002. return (ARCHIVE_OK);
  2003. }
  2004. if (!lzh_make_huffman_table(&(ds->pt)))
  2005. goto failed;/* Invalid data */
  2006. if (ds->reading_position) {
  2007. ds->state = ST_GET_LITERAL;
  2008. break;
  2009. }
  2010. /* FALL THROUGH */
  2011. case ST_RD_LITERAL_1:
  2012. if (!lzh_br_read_ahead(strm, br, ds->lt.len_bits)) {
  2013. if (last)
  2014. goto failed;/* Truncated data. */
  2015. ds->state = ST_RD_LITERAL_1;
  2016. return (ARCHIVE_OK);
  2017. }
  2018. ds->lt.len_avail = lzh_br_bits(br, ds->lt.len_bits);
  2019. lzh_br_consume(br, ds->lt.len_bits);
  2020. /* FALL THROUGH */
  2021. case ST_RD_LITERAL_2:
  2022. if (ds->lt.len_avail == 0) {
  2023. /* There is no bitlen. */
  2024. if (!lzh_br_read_ahead(strm, br,
  2025. ds->lt.len_bits)) {
  2026. if (last)
  2027. goto failed;/* Truncated data.*/
  2028. ds->state = ST_RD_LITERAL_2;
  2029. return (ARCHIVE_OK);
  2030. }
  2031. if (!lzh_make_fake_table(&(ds->lt),
  2032. lzh_br_bits(br, ds->lt.len_bits)))
  2033. goto failed;/* Invalid data */
  2034. lzh_br_consume(br, ds->lt.len_bits);
  2035. ds->state = ST_RD_POS_DATA_1;
  2036. break;
  2037. } else if (ds->lt.len_avail > ds->lt.len_size)
  2038. goto failed;/* Invalid data */
  2039. ds->loop = 0;
  2040. memset(ds->lt.freq, 0, sizeof(ds->lt.freq));
  2041. /* FALL THROUGH */
  2042. case ST_RD_LITERAL_3:
  2043. i = ds->loop;
  2044. while (i < ds->lt.len_avail) {
  2045. if (!lzh_br_read_ahead(strm, br,
  2046. ds->pt.max_bits)) {
  2047. if (last)
  2048. goto failed;/* Truncated data.*/
  2049. ds->loop = i;
  2050. ds->state = ST_RD_LITERAL_3;
  2051. return (ARCHIVE_OK);
  2052. }
  2053. rbits = lzh_br_bits(br, ds->pt.max_bits);
  2054. c = lzh_decode_huffman(&(ds->pt), rbits);
  2055. if (c > 2) {
  2056. /* Note: 'c' will never be more than
  2057. * eighteen since it's limited by
  2058. * PT_BITLEN_SIZE, which is being set
  2059. * to ds->pt.len_size through
  2060. * ds->literal_pt_len_size. */
  2061. lzh_br_consume(br, ds->pt.bitlen[c]);
  2062. c -= 2;
  2063. ds->lt.freq[c]++;
  2064. ds->lt.bitlen[i++] = c;
  2065. } else if (c == 0) {
  2066. lzh_br_consume(br, ds->pt.bitlen[c]);
  2067. ds->lt.bitlen[i++] = 0;
  2068. } else {
  2069. /* c == 1 or c == 2 */
  2070. int n = (c == 1)?4:9;
  2071. if (!lzh_br_read_ahead(strm, br,
  2072. ds->pt.bitlen[c] + n)) {
  2073. if (last) /* Truncated data. */
  2074. goto failed;
  2075. ds->loop = i;
  2076. ds->state = ST_RD_LITERAL_3;
  2077. return (ARCHIVE_OK);
  2078. }
  2079. lzh_br_consume(br, ds->pt.bitlen[c]);
  2080. c = lzh_br_bits(br, n);
  2081. lzh_br_consume(br, n);
  2082. c += (n == 4)?3:20;
  2083. if (i + c > ds->lt.len_avail)
  2084. goto failed;/* Invalid data */
  2085. memset(&(ds->lt.bitlen[i]), 0, c);
  2086. i += c;
  2087. }
  2088. }
  2089. if (i > ds->lt.len_avail ||
  2090. !lzh_make_huffman_table(&(ds->lt)))
  2091. goto failed;/* Invalid data */
  2092. /* FALL THROUGH */
  2093. case ST_RD_POS_DATA_1:
  2094. /*
  2095. * Read a position table compressed in huffman
  2096. * coding.
  2097. */
  2098. ds->pt.len_size = ds->pos_pt_len_size;
  2099. ds->pt.len_bits = ds->pos_pt_len_bits;
  2100. ds->reading_position = 1;
  2101. ds->state = ST_RD_PT_1;
  2102. break;
  2103. case ST_GET_LITERAL:
  2104. return (100);
  2105. }
  2106. }
  2107. failed:
  2108. return (ds->error = ARCHIVE_FAILED);
  2109. }
  2110. static int
  2111. lzh_decode_blocks(struct lzh_stream *strm, int last)
  2112. {
  2113. struct lzh_dec *ds = strm->ds;
  2114. struct lzh_br bre = ds->br;
  2115. struct huffman *lt = &(ds->lt);
  2116. struct huffman *pt = &(ds->pt);
  2117. unsigned char *w_buff = ds->w_buff;
  2118. unsigned char *lt_bitlen = lt->bitlen;
  2119. unsigned char *pt_bitlen = pt->bitlen;
  2120. int blocks_avail = ds->blocks_avail, c = 0;
  2121. int copy_len = ds->copy_len, copy_pos = ds->copy_pos;
  2122. int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
  2123. int lt_max_bits = lt->max_bits, pt_max_bits = pt->max_bits;
  2124. int state = ds->state;
  2125. if (ds->w_remaining > 0) {
  2126. if (!lzh_copy_from_window(strm, ds))
  2127. goto next_data;
  2128. }
  2129. for (;;) {
  2130. switch (state) {
  2131. case ST_GET_LITERAL:
  2132. for (;;) {
  2133. if (blocks_avail == 0) {
  2134. /* We have decoded all blocks.
  2135. * Let's handle next blocks. */
  2136. ds->state = ST_RD_BLOCK;
  2137. ds->br = bre;
  2138. ds->blocks_avail = 0;
  2139. ds->w_pos = w_pos;
  2140. ds->copy_pos = 0;
  2141. return (100);
  2142. }
  2143. /* lzh_br_read_ahead() always try to fill the
  2144. * cache buffer up. In specific situation we
  2145. * are close to the end of the data, the cache
  2146. * buffer will not be full and thus we have to
  2147. * determine if the cache buffer has some bits
  2148. * as much as we need after lzh_br_read_ahead()
  2149. * failed. */
  2150. if (!lzh_br_read_ahead(strm, &bre,
  2151. lt_max_bits)) {
  2152. if (!last)
  2153. goto next_data;
  2154. /* Remaining bits are less than
  2155. * maximum bits(lt.max_bits) but maybe
  2156. * it still remains as much as we need,
  2157. * so we should try to use it with
  2158. * dummy bits. */
  2159. c = lzh_decode_huffman(lt,
  2160. lzh_br_bits_forced(&bre,
  2161. lt_max_bits));
  2162. lzh_br_consume(&bre, lt_bitlen[c]);
  2163. if (!lzh_br_has(&bre, 0))
  2164. goto failed;/* Over read. */
  2165. } else {
  2166. c = lzh_decode_huffman(lt,
  2167. lzh_br_bits(&bre, lt_max_bits));
  2168. lzh_br_consume(&bre, lt_bitlen[c]);
  2169. }
  2170. blocks_avail--;
  2171. if (c > UCHAR_MAX)
  2172. /* Current block is a match data. */
  2173. break;
  2174. /*
  2175. * 'c' is exactly a literal code.
  2176. */
  2177. /* Save a decoded code to reference it
  2178. * afterward. */
  2179. w_buff[w_pos] = c;
  2180. if (++w_pos >= w_size) {
  2181. w_pos = 0;
  2182. ds->w_remaining = w_size;
  2183. if (!lzh_copy_from_window(strm, ds))
  2184. goto next_data;
  2185. }
  2186. }
  2187. /* 'c' is the length of a match pattern we have
  2188. * already extracted, which has be stored in
  2189. * window(ds->w_buff). */
  2190. copy_len = c - (UCHAR_MAX + 1) + MINMATCH;
  2191. /* FALL THROUGH */
  2192. case ST_GET_POS_1:
  2193. /*
  2194. * Get a reference position.
  2195. */
  2196. if (!lzh_br_read_ahead(strm, &bre, pt_max_bits)) {
  2197. if (!last) {
  2198. state = ST_GET_POS_1;
  2199. ds->copy_len = copy_len;
  2200. goto next_data;
  2201. }
  2202. copy_pos = lzh_decode_huffman(pt,
  2203. lzh_br_bits_forced(&bre, pt_max_bits));
  2204. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2205. if (!lzh_br_has(&bre, 0))
  2206. goto failed;/* Over read. */
  2207. } else {
  2208. copy_pos = lzh_decode_huffman(pt,
  2209. lzh_br_bits(&bre, pt_max_bits));
  2210. lzh_br_consume(&bre, pt_bitlen[copy_pos]);
  2211. }
  2212. /* FALL THROUGH */
  2213. case ST_GET_POS_2:
  2214. if (copy_pos > 1) {
  2215. /* We need an additional adjustment number to
  2216. * the position. */
  2217. int p = copy_pos - 1;
  2218. if (!lzh_br_read_ahead(strm, &bre, p)) {
  2219. if (last)
  2220. goto failed;/* Truncated data.*/
  2221. state = ST_GET_POS_2;
  2222. ds->copy_len = copy_len;
  2223. ds->copy_pos = copy_pos;
  2224. goto next_data;
  2225. }
  2226. copy_pos = (1 << p) + lzh_br_bits(&bre, p);
  2227. lzh_br_consume(&bre, p);
  2228. }
  2229. /* The position is actually a distance from the last
  2230. * code we had extracted and thus we have to convert
  2231. * it to a position of the window. */
  2232. copy_pos = (w_pos - copy_pos - 1) & w_mask;
  2233. /* FALL THROUGH */
  2234. case ST_COPY_DATA:
  2235. /*
  2236. * Copy `copy_len' bytes as extracted data from
  2237. * the window into the output buffer.
  2238. */
  2239. for (;;) {
  2240. int l;
  2241. l = copy_len;
  2242. if (copy_pos > w_pos) {
  2243. if (l > w_size - copy_pos)
  2244. l = w_size - copy_pos;
  2245. } else {
  2246. if (l > w_size - w_pos)
  2247. l = w_size - w_pos;
  2248. }
  2249. if ((copy_pos + l < w_pos)
  2250. || (w_pos + l < copy_pos)) {
  2251. /* No overlap. */
  2252. memcpy(w_buff + w_pos,
  2253. w_buff + copy_pos, l);
  2254. } else {
  2255. const unsigned char *s;
  2256. unsigned char *d;
  2257. int li;
  2258. d = w_buff + w_pos;
  2259. s = w_buff + copy_pos;
  2260. for (li = 0; li < l; li++)
  2261. d[li] = s[li];
  2262. }
  2263. w_pos = (w_pos + l) & w_mask;
  2264. if (w_pos == 0) {
  2265. ds->w_remaining = w_size;
  2266. if (!lzh_copy_from_window(strm, ds)) {
  2267. if (copy_len <= l)
  2268. state = ST_GET_LITERAL;
  2269. else {
  2270. state = ST_COPY_DATA;
  2271. ds->copy_len =
  2272. copy_len - l;
  2273. ds->copy_pos =
  2274. (copy_pos + l)
  2275. & w_mask;
  2276. }
  2277. goto next_data;
  2278. }
  2279. }
  2280. if (copy_len <= l)
  2281. /* A copy of current pattern ended. */
  2282. break;
  2283. copy_len -= l;
  2284. copy_pos = (copy_pos + l) & w_mask;
  2285. }
  2286. state = ST_GET_LITERAL;
  2287. break;
  2288. }
  2289. }
  2290. failed:
  2291. return (ds->error = ARCHIVE_FAILED);
  2292. next_data:
  2293. ds->br = bre;
  2294. ds->blocks_avail = blocks_avail;
  2295. ds->state = state;
  2296. ds->w_pos = w_pos;
  2297. return (ARCHIVE_OK);
  2298. }
  2299. static int
  2300. lzh_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
  2301. {
  2302. int bits;
  2303. if (hf->bitlen == NULL) {
  2304. hf->bitlen = malloc(len_size * sizeof(hf->bitlen[0]));
  2305. if (hf->bitlen == NULL)
  2306. return (ARCHIVE_FATAL);
  2307. }
  2308. if (hf->tbl == NULL) {
  2309. if (tbl_bits < HTBL_BITS)
  2310. bits = tbl_bits;
  2311. else
  2312. bits = HTBL_BITS;
  2313. hf->tbl = malloc(((size_t)1 << bits) * sizeof(hf->tbl[0]));
  2314. if (hf->tbl == NULL)
  2315. return (ARCHIVE_FATAL);
  2316. }
  2317. if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
  2318. hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
  2319. hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
  2320. if (hf->tree == NULL)
  2321. return (ARCHIVE_FATAL);
  2322. }
  2323. hf->len_size = (int)len_size;
  2324. hf->tbl_bits = tbl_bits;
  2325. return (ARCHIVE_OK);
  2326. }
  2327. static void
  2328. lzh_huffman_free(struct huffman *hf)
  2329. {
  2330. free(hf->bitlen);
  2331. free(hf->tbl);
  2332. free(hf->tree);
  2333. }
  2334. static int
  2335. lzh_read_pt_bitlen(struct lzh_stream *strm, int start, int end)
  2336. {
  2337. struct lzh_dec *ds = strm->ds;
  2338. struct lzh_br * br = &(ds->br);
  2339. int c, i;
  2340. for (i = start; i < end;) {
  2341. /*
  2342. * bit pattern the number we need
  2343. * 000 -> 0
  2344. * 001 -> 1
  2345. * 010 -> 2
  2346. * ...
  2347. * 110 -> 6
  2348. * 1110 -> 7
  2349. * 11110 -> 8
  2350. * ...
  2351. * 1111111111110 -> 16
  2352. */
  2353. if (!lzh_br_read_ahead(strm, br, 3))
  2354. return (i);
  2355. if ((c = lzh_br_bits(br, 3)) == 7) {
  2356. int d;
  2357. if (!lzh_br_read_ahead(strm, br, 13))
  2358. return (i);
  2359. d = lzh_br_bits(br, 13);
  2360. while (d & 0x200) {
  2361. c++;
  2362. d <<= 1;
  2363. }
  2364. if (c > 16)
  2365. return (-1);/* Invalid data. */
  2366. lzh_br_consume(br, c - 3);
  2367. } else
  2368. lzh_br_consume(br, 3);
  2369. ds->pt.bitlen[i++] = c;
  2370. ds->pt.freq[c]++;
  2371. }
  2372. return (i);
  2373. }
  2374. static int
  2375. lzh_make_fake_table(struct huffman *hf, uint16_t c)
  2376. {
  2377. if (c >= hf->len_size)
  2378. return (0);
  2379. hf->tbl[0] = c;
  2380. hf->max_bits = 0;
  2381. hf->shift_bits = 0;
  2382. hf->bitlen[hf->tbl[0]] = 0;
  2383. return (1);
  2384. }
  2385. /*
  2386. * Make a huffman coding table.
  2387. */
  2388. static int
  2389. lzh_make_huffman_table(struct huffman *hf)
  2390. {
  2391. uint16_t *tbl;
  2392. const unsigned char *bitlen;
  2393. int bitptn[17], weight[17];
  2394. int i, maxbits = 0, ptn, tbl_size, w;
  2395. int diffbits, len_avail;
  2396. /*
  2397. * Initialize bit patterns.
  2398. */
  2399. ptn = 0;
  2400. for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
  2401. bitptn[i] = ptn;
  2402. weight[i] = w;
  2403. if (hf->freq[i]) {
  2404. ptn += hf->freq[i] * w;
  2405. maxbits = i;
  2406. }
  2407. }
  2408. if (ptn != 0x10000 || maxbits > hf->tbl_bits)
  2409. return (0);/* Invalid */
  2410. hf->max_bits = maxbits;
  2411. /*
  2412. * Cut out extra bits which we won't house in the table.
  2413. * This preparation reduces the same calculation in the for-loop
  2414. * making the table.
  2415. */
  2416. if (maxbits < 16) {
  2417. int ebits = 16 - maxbits;
  2418. for (i = 1; i <= maxbits; i++) {
  2419. bitptn[i] >>= ebits;
  2420. weight[i] >>= ebits;
  2421. }
  2422. }
  2423. if (maxbits > HTBL_BITS) {
  2424. int htbl_max;
  2425. uint16_t *p;
  2426. diffbits = maxbits - HTBL_BITS;
  2427. for (i = 1; i <= HTBL_BITS; i++) {
  2428. bitptn[i] >>= diffbits;
  2429. weight[i] >>= diffbits;
  2430. }
  2431. htbl_max = bitptn[HTBL_BITS] +
  2432. weight[HTBL_BITS] * hf->freq[HTBL_BITS];
  2433. p = &(hf->tbl[htbl_max]);
  2434. while (p < &hf->tbl[1U<<HTBL_BITS])
  2435. *p++ = 0;
  2436. } else
  2437. diffbits = 0;
  2438. hf->shift_bits = diffbits;
  2439. /*
  2440. * Make the table.
  2441. */
  2442. tbl_size = 1 << HTBL_BITS;
  2443. tbl = hf->tbl;
  2444. bitlen = hf->bitlen;
  2445. len_avail = hf->len_avail;
  2446. hf->tree_used = 0;
  2447. for (i = 0; i < len_avail; i++) {
  2448. uint16_t *p;
  2449. int len, cnt;
  2450. uint16_t bit;
  2451. int extlen;
  2452. struct htree_t *ht;
  2453. if (bitlen[i] == 0)
  2454. continue;
  2455. /* Get a bit pattern */
  2456. len = bitlen[i];
  2457. ptn = bitptn[len];
  2458. cnt = weight[len];
  2459. if (len <= HTBL_BITS) {
  2460. /* Calculate next bit pattern */
  2461. if ((bitptn[len] = ptn + cnt) > tbl_size)
  2462. return (0);/* Invalid */
  2463. /* Update the table */
  2464. p = &(tbl[ptn]);
  2465. while (--cnt >= 0)
  2466. p[cnt] = (uint16_t)i;
  2467. continue;
  2468. }
  2469. /*
  2470. * A bit length is too big to be housed to a direct table,
  2471. * so we use a tree model for its extra bits.
  2472. */
  2473. bitptn[len] = ptn + cnt;
  2474. bit = 1U << (diffbits -1);
  2475. extlen = len - HTBL_BITS;
  2476. p = &(tbl[ptn >> diffbits]);
  2477. if (*p == 0) {
  2478. *p = len_avail + hf->tree_used;
  2479. ht = &(hf->tree[hf->tree_used++]);
  2480. if (hf->tree_used > hf->tree_avail)
  2481. return (0);/* Invalid */
  2482. ht->left = 0;
  2483. ht->right = 0;
  2484. } else {
  2485. if (*p < len_avail ||
  2486. *p >= (len_avail + hf->tree_used))
  2487. return (0);/* Invalid */
  2488. ht = &(hf->tree[*p - len_avail]);
  2489. }
  2490. while (--extlen > 0) {
  2491. if (ptn & bit) {
  2492. if (ht->left < len_avail) {
  2493. ht->left = len_avail + hf->tree_used;
  2494. ht = &(hf->tree[hf->tree_used++]);
  2495. if (hf->tree_used > hf->tree_avail)
  2496. return (0);/* Invalid */
  2497. ht->left = 0;
  2498. ht->right = 0;
  2499. } else {
  2500. ht = &(hf->tree[ht->left - len_avail]);
  2501. }
  2502. } else {
  2503. if (ht->right < len_avail) {
  2504. ht->right = len_avail + hf->tree_used;
  2505. ht = &(hf->tree[hf->tree_used++]);
  2506. if (hf->tree_used > hf->tree_avail)
  2507. return (0);/* Invalid */
  2508. ht->left = 0;
  2509. ht->right = 0;
  2510. } else {
  2511. ht = &(hf->tree[ht->right - len_avail]);
  2512. }
  2513. }
  2514. bit >>= 1;
  2515. }
  2516. if (ptn & bit) {
  2517. if (ht->left != 0)
  2518. return (0);/* Invalid */
  2519. ht->left = (uint16_t)i;
  2520. } else {
  2521. if (ht->right != 0)
  2522. return (0);/* Invalid */
  2523. ht->right = (uint16_t)i;
  2524. }
  2525. }
  2526. return (1);
  2527. }
  2528. static int
  2529. lzh_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
  2530. {
  2531. struct htree_t *ht;
  2532. int extlen;
  2533. ht = hf->tree;
  2534. extlen = hf->shift_bits;
  2535. while (c >= hf->len_avail) {
  2536. c -= hf->len_avail;
  2537. if (extlen-- <= 0 || c >= hf->tree_used)
  2538. return (0);
  2539. if (rbits & (1U << extlen))
  2540. c = ht[c].left;
  2541. else
  2542. c = ht[c].right;
  2543. }
  2544. return (c);
  2545. }
  2546. static inline int
  2547. lzh_decode_huffman(struct huffman *hf, unsigned rbits)
  2548. {
  2549. int c;
  2550. /*
  2551. * At first search an index table for a bit pattern.
  2552. * If it fails, search a huffman tree for.
  2553. */
  2554. c = hf->tbl[rbits >> hf->shift_bits];
  2555. if (c < hf->len_avail)
  2556. return (c);
  2557. /* This bit pattern needs to be found out at a huffman tree. */
  2558. return (lzh_decode_huffman_tree(hf, rbits, c));
  2559. }