busybox-1.27.0_lzip-0.patch 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918
  1. diff -urdN busybox-1.27.0.old/archival/Config.src busybox-1.27.0.new/archival/Config.src
  2. --- busybox-1.27.0.old/archival/Config.src 2017-01-10 16:31:40.000000000 +0100
  3. +++ busybox-1.27.0.new/archival/Config.src 2017-07-07 01:00:19.000000000 +0200
  4. @@ -21,6 +21,10 @@
  5. bool "Make tar, rpm, modprobe etc understand .gz data"
  6. default y
  7. +config FEATURE_SEAMLESS_LZ
  8. + bool "Make tar, rpm, modprobe etc understand .lz data"
  9. + default y
  10. +
  11. config FEATURE_SEAMLESS_Z
  12. bool "Make tar, rpm, modprobe etc understand .Z data"
  13. default n # it is ancient
  14. diff -urdN busybox-1.27.0.old/archival/bbunzip.c busybox-1.27.0.new/archival/bbunzip.c
  15. --- busybox-1.27.0.old/archival/bbunzip.c 2017-07-01 22:51:12.000000000 +0200
  16. +++ busybox-1.27.0.new/archival/bbunzip.c 2017-07-07 16:17:12.000000000 +0200
  17. @@ -202,18 +202,31 @@
  18. #if ENABLE_UNCOMPRESS \
  19. || ENABLE_FEATURE_BZIP2_DECOMPRESS \
  20. + || ENABLE_LUNZIP \
  21. || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
  22. || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
  23. static
  24. char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
  25. {
  26. char *extension = strrchr(filename, '.');
  27. - if (!extension || strcmp(extension + 1, expected_ext) != 0) {
  28. +
  29. + if (!extension)
  30. + return NULL;
  31. +
  32. + if (strcmp(extension + 1, expected_ext) == 0) {
  33. + *extension = '\0';
  34. + } else if (extension[1] == 't' && strlen(expected_ext) >= 2 &&
  35. + strcmp(extension + 2, expected_ext) == 0) {
  36. + filename = xstrdup(filename);
  37. + extension = strrchr(filename, '.');
  38. + extension[2] = 'a';
  39. + extension[3] = 'r';
  40. + extension[4] = '\0';
  41. + } else {
  42. /* Mimic GNU gunzip - "real" bunzip2 tries to */
  43. /* unpack file anyway, to file.out */
  44. return NULL;
  45. }
  46. - *extension = '\0';
  47. return filename;
  48. }
  49. #endif
  50. @@ -456,6 +469,37 @@
  51. }
  52. #endif
  53. +
  54. +//config:config LUNZIP
  55. +//config: bool "lunzip"
  56. +//config: default y
  57. +//config: help
  58. +//config: lunzip is used to decompress archives created by lzip.
  59. +//config: You can use the '-t' option to test the integrity of an archive,
  60. +//config: without decompressing it.
  61. +
  62. +//applet:IF_LUNZIP(APPLET(lunzip, BB_DIR_USR_BIN, BB_SUID_DROP))
  63. +//kbuild:lib-$(CONFIG_LUNZIP) += bbunzip.o
  64. +
  65. +//usage:#define lunzip_trivial_usage
  66. +//usage: "[-cft] [FILE]..."
  67. +//usage:#define lunzip_full_usage "\n\n"
  68. +//usage: "Decompress FILEs (or stdin)\n"
  69. +//usage: "\n -c Write to stdout"
  70. +//usage: "\n -f Force"
  71. +//usage: "\n -k Keep input files"
  72. +//usage: "\n -t Test file integrity"
  73. +#if ENABLE_LUNZIP
  74. +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  75. +int lunzip_main(int argc UNUSED_PARAM, char **argv)
  76. +{
  77. + getopt32(argv, "cfkvqdt");
  78. + argv += optind;
  79. +
  80. + return bbunpack(argv, unpack_lz_stream, make_new_name_generic, "lz");
  81. +}
  82. +#endif
  83. +
  84. /*
  85. * Small lzma deflate implementation.
  86. diff -urdN busybox-1.27.0.old/archival/dpkg.c busybox-1.27.0.new/archival/dpkg.c
  87. --- busybox-1.27.0.old/archival/dpkg.c 2017-07-01 22:51:12.000000000 +0200
  88. +++ busybox-1.27.0.new/archival/dpkg.c 2017-07-07 00:24:37.000000000 +0200
  89. @@ -1482,6 +1482,9 @@
  90. #if ENABLE_FEATURE_SEAMLESS_XZ
  91. llist_add_to(&(ar_handle->accept), (char*)"control.tar.xz");
  92. #endif
  93. +#if ENABLE_FEATURE_SEAMLESS_LZ
  94. + llist_add_to(&(ar_handle->accept), (char*)"control.tar.lz");
  95. +#endif
  96. /* Assign the tar handle as a subarchive of the ar handle */
  97. ar_handle->dpkg__sub_archive = tar_handle;
  98. @@ -1509,6 +1512,9 @@
  99. #if ENABLE_FEATURE_SEAMLESS_XZ
  100. llist_add_to(&(ar_handle->accept), (char*)"data.tar.xz");
  101. #endif
  102. +#if ENABLE_FEATURE_SEAMLESS_LZ
  103. + llist_add_to(&(ar_handle->accept), (char*)"data.tar.lz");
  104. +#endif
  105. /* Assign the tar handle as a subarchive of the ar handle */
  106. ar_handle->dpkg__sub_archive = tar_handle;
  107. diff -urdN busybox-1.27.0.old/archival/dpkg_deb.c busybox-1.27.0.new/archival/dpkg_deb.c
  108. --- busybox-1.27.0.old/archival/dpkg_deb.c 2016-12-23 13:52:46.000000000 +0100
  109. +++ busybox-1.27.0.new/archival/dpkg_deb.c 2017-07-07 00:24:37.000000000 +0200
  110. @@ -70,6 +70,10 @@
  111. llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
  112. llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
  113. #endif
  114. +#if ENABLE_FEATURE_SEAMLESS_LZ
  115. + llist_add_to(&ar_archive->accept, (char*)"data.tar.lz");
  116. + llist_add_to(&control_tar_llist, (char*)"control.tar.lz");
  117. +#endif
  118. #if ENABLE_FEATURE_SEAMLESS_LZMA
  119. llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
  120. llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
  121. diff -urdN busybox-1.27.0.old/archival/libarchive/Kbuild.src busybox-1.27.0.new/archival/libarchive/Kbuild.src
  122. --- busybox-1.27.0.old/archival/libarchive/Kbuild.src 2017-04-05 18:27:36.000000000 +0200
  123. +++ busybox-1.27.0.new/archival/libarchive/Kbuild.src 2017-07-07 01:00:19.000000000 +0200
  124. @@ -35,6 +35,7 @@
  125. get_header_tar.o \
  126. get_header_tar_gz.o \
  127. get_header_tar_bz2.o \
  128. + get_header_tar_lz.o \
  129. get_header_tar_lzma.o \
  130. get_header_tar_xz.o \
  131. @@ -54,6 +55,7 @@
  132. # 'bzip2 -d', bunzip2 or bzcat selects FEATURE_BZIP2_DECOMPRESS
  133. lib-$(CONFIG_FEATURE_BZIP2_DECOMPRESS) += open_transformer.o decompress_bunzip2.o
  134. lib-$(CONFIG_FEATURE_UNZIP_BZIP2) += open_transformer.o decompress_bunzip2.o
  135. +lib-$(CONFIG_LUNZIP) += open_transformer.o decompress_lunzip.o
  136. lib-$(CONFIG_UNLZMA) += open_transformer.o decompress_unlzma.o
  137. lib-$(CONFIG_LZCAT) += open_transformer.o decompress_unlzma.o
  138. lib-$(CONFIG_LZMA) += open_transformer.o decompress_unlzma.o
  139. @@ -70,6 +72,7 @@
  140. lib-$(CONFIG_RPM) += open_transformer.o decompress_gunzip.o get_header_cpio.o
  141. lib-$(CONFIG_GZIP) += open_transformer.o
  142. lib-$(CONFIG_BZIP2) += open_transformer.o
  143. +lib-$(CONFIG_LZIP) += open_transformer.o
  144. lib-$(CONFIG_LZOP) += open_transformer.o
  145. lib-$(CONFIG_MAN) += open_transformer.o
  146. lib-$(CONFIG_SETFONT) += open_transformer.o
  147. @@ -85,6 +88,7 @@
  148. lib-$(CONFIG_FEATURE_SEAMLESS_Z) += open_transformer.o decompress_uncompress.o
  149. lib-$(CONFIG_FEATURE_SEAMLESS_GZ) += open_transformer.o decompress_gunzip.o
  150. lib-$(CONFIG_FEATURE_SEAMLESS_BZ2) += open_transformer.o decompress_bunzip2.o
  151. +lib-$(CONFIG_FEATURE_SEAMLESS_LZ) += open_transformer.o decompress_lunzip.o
  152. lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma.o
  153. lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o
  154. lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += open_transformer.o decompress_bunzip2.o
  155. diff -urdN busybox-1.27.0.old/archival/libarchive/decompress_lunzip.c busybox-1.27.0.new/archival/libarchive/decompress_lunzip.c
  156. --- busybox-1.27.0.old/archival/libarchive/decompress_lunzip.c 1970-01-01 01:00:00.000000000 +0100
  157. +++ busybox-1.27.0.new/archival/libarchive/decompress_lunzip.c 2017-07-07 00:24:37.000000000 +0200
  158. @@ -0,0 +1,502 @@
  159. +/*
  160. + * lunzip implementation for busybox
  161. + *
  162. + * Copyright (C) 2012-2016 Antonio Diaz Diaz.
  163. + *
  164. + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  165. + */
  166. +
  167. +#include "libbb.h"
  168. +#include "bb_archive.h"
  169. +#include "lzip.h"
  170. +
  171. +/* Some functions have been marked with __always_inline because xz does
  172. + it, giving the impression that unxz is much faster than lunzip. */
  173. +#ifndef __always_inline
  174. +# ifdef __GNUC__
  175. +# define __always_inline \
  176. + inline __attribute__((__always_inline__))
  177. +# else
  178. +# define __always_inline inline
  179. +# endif
  180. +#endif
  181. +
  182. +
  183. +enum { rd_buffer_size = 16384 };
  184. +
  185. +struct Range_decoder {
  186. + unsigned long long partial_member_pos;
  187. + uint8_t *buffer; /* input buffer */
  188. + int pos; /* current pos in buffer */
  189. + int stream_pos; /* when reached, a new block must be read */
  190. + uint32_t code;
  191. + uint32_t range;
  192. + int infd; /* input file descriptor */
  193. + bool at_stream_end;
  194. +};
  195. +
  196. +
  197. +static bool Rd_read_block(struct Range_decoder *const rdec)
  198. +{
  199. + if (!rdec->at_stream_end) {
  200. + rdec->stream_pos =
  201. + full_read(rdec->infd, rdec->buffer, rd_buffer_size);
  202. + rdec->at_stream_end = (rdec->stream_pos < rd_buffer_size);
  203. + rdec->partial_member_pos += rdec->pos;
  204. + rdec->pos = 0;
  205. + }
  206. + return rdec->pos < rdec->stream_pos;
  207. +}
  208. +
  209. +
  210. +static bool Rd_init(struct Range_decoder *const rdec, const int ifd,
  211. + const bool magic_skipped)
  212. +{
  213. + rdec->partial_member_pos = (magic_skipped ? 4 : 0);
  214. + rdec->buffer = (uint8_t *) malloc(rd_buffer_size);
  215. + if (!rdec->buffer) return false;
  216. + rdec->pos = 0;
  217. + rdec->stream_pos = 0;
  218. + rdec->code = 0;
  219. + rdec->range = 0xFFFFFFFFU;
  220. + rdec->infd = ifd;
  221. + rdec->at_stream_end = false;
  222. + return true;
  223. +}
  224. +
  225. +static __always_inline bool Rd_finished(struct Range_decoder *const rdec)
  226. +{
  227. + return rdec->pos >= rdec->stream_pos && !Rd_read_block(rdec);
  228. +}
  229. +
  230. +static inline unsigned long long
  231. +Rd_member_position(const struct Range_decoder *const rdec)
  232. +{
  233. + return rdec->partial_member_pos + rdec->pos;
  234. +}
  235. +
  236. +static inline void Rd_reset_member_position(struct Range_decoder *const rdec)
  237. +{
  238. + rdec->partial_member_pos = 0; rdec->partial_member_pos -= rdec->pos;
  239. +}
  240. +
  241. +static __always_inline uint8_t Rd_get_byte(struct Range_decoder *const rdec)
  242. +{
  243. + /* 0xFF avoids decoder error if member is truncated at EOS marker */
  244. + if (Rd_finished(rdec)) return 0xFF;
  245. + return rdec->buffer[rdec->pos++];
  246. +}
  247. +
  248. +static void Rd_load(struct Range_decoder *const rdec)
  249. +{
  250. + int i;
  251. + rdec->code = 0;
  252. + for (i = 0; i < 5; ++i)
  253. + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
  254. + rdec->range = 0xFFFFFFFFU;
  255. +}
  256. +
  257. +static __always_inline void Rd_normalize(struct Range_decoder *const rdec)
  258. +{
  259. + if (rdec->range <= 0x00FFFFFFU) {
  260. + rdec->range <<= 8;
  261. + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
  262. + }
  263. +}
  264. +
  265. +static uint32_t Rd_decode(struct Range_decoder *const rdec,
  266. + const uint32_t num_bits)
  267. +{
  268. + uint32_t symbol = 0;
  269. + uint32_t i;
  270. + for (i = num_bits; i > 0; --i) {
  271. + uint32_t mask;
  272. + Rd_normalize(rdec);
  273. + rdec->range >>= 1;
  274. + /* symbol <<= 1; */
  275. + /* if(rdec->code >= rdec->range) { rdec->code -= rdec->range; symbol |= 1; } */
  276. + mask = 0U - (rdec->code < rdec->range);
  277. + rdec->code -= rdec->range;
  278. + rdec->code += rdec->range & mask;
  279. + symbol = (symbol << 1) + (mask + 1);
  280. + }
  281. + return symbol;
  282. +}
  283. +
  284. +static __always_inline uint32_t Rd_decode_bit(struct Range_decoder *const rdec,
  285. + Bit_model * const probability)
  286. +{
  287. + uint32_t bound;
  288. + Rd_normalize(rdec);
  289. + bound = (rdec->range >> bit_model_total_bits) * *probability;
  290. + if (rdec->code < bound) {
  291. + rdec->range = bound;
  292. + *probability += (bit_model_total - *probability) >> bit_model_move_bits;
  293. + return 0;
  294. + } else {
  295. + rdec->range -= bound;
  296. + rdec->code -= bound;
  297. + *probability -= *probability >> bit_model_move_bits;
  298. + return 1;
  299. + }
  300. +}
  301. +
  302. +static __always_inline uint32_t Rd_decode_tree(struct Range_decoder *const rdec,
  303. + Bit_model bm[], const uint32_t num_bits)
  304. +{
  305. + uint32_t symbol = 1;
  306. + uint32_t i;
  307. + for (i = num_bits; i > 0; --i)
  308. + symbol = (symbol << 1) | Rd_decode_bit(rdec, &bm[symbol]);
  309. + return symbol - (1 << num_bits);
  310. +}
  311. +
  312. +static __always_inline uint32_t Rd_decode_tree_reversed(struct Range_decoder *const rdec,
  313. + Bit_model bm[], const uint32_t num_bits)
  314. +{
  315. + uint32_t model = 1;
  316. + uint32_t symbol = 0;
  317. + uint32_t i;
  318. + for (i = 0; i < num_bits; ++i) {
  319. + const uint32_t bit = Rd_decode_bit(rdec, &bm[model]);
  320. + model = ( model << 1 ) + bit;
  321. + symbol |= ( bit << i );
  322. + }
  323. + return symbol;
  324. +}
  325. +
  326. +static uint32_t Rd_decode_matched(struct Range_decoder *const rdec,
  327. + Bit_model bm[], uint32_t match_byte)
  328. +{
  329. + uint32_t symbol = 1;
  330. + uint32_t mask = 0x100;
  331. + while(true) {
  332. + const uint32_t match_bit = (match_byte <<= 1) & mask;
  333. + const uint32_t bit = Rd_decode_bit(rdec, &bm[symbol+match_bit+mask]);
  334. + symbol = (symbol << 1) + bit;
  335. + if (symbol >= 0x100) return symbol & 0xFF;
  336. + mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */
  337. + }
  338. +}
  339. +
  340. +static __always_inline uint32_t Rd_decode_len(struct Range_decoder *const rdec,
  341. + struct Len_model * const lm,
  342. + const int pos_state)
  343. +{
  344. + if (Rd_decode_bit(rdec, &lm->choice1) == 0)
  345. + return Rd_decode_tree(rdec, lm->bm_low[pos_state], len_low_bits);
  346. + if (Rd_decode_bit(rdec, &lm->choice2) == 0)
  347. + return len_low_symbols +
  348. + Rd_decode_tree(rdec, lm->bm_mid[pos_state], len_mid_bits);
  349. + return len_low_symbols + len_mid_symbols +
  350. + Rd_decode_tree(rdec, lm->bm_high, len_high_bits);
  351. +}
  352. +
  353. +
  354. +struct LZ_decoder {
  355. + unsigned long long partial_data_pos;
  356. + struct Range_decoder *rdec;
  357. + uint32_t dictionary_size;
  358. + uint8_t *buffer; /* output buffer */
  359. + uint32_t pos; /* current pos in buffer */
  360. + uint32_t stream_pos; /* first byte not yet written to file */
  361. + uint32_t crc;
  362. + int outfd; /* output file descriptor */
  363. + bool pos_wrapped;
  364. + bool write_error;
  365. +};
  366. +
  367. +static void LZd_flush_data(struct LZ_decoder *const d)
  368. +{
  369. + if (d->pos > d->stream_pos) {
  370. + const int size = d->pos - d->stream_pos;
  371. + d->crc = crc32_block_endian0(d->crc, d->buffer + d->stream_pos,
  372. + size, global_crc32_table);
  373. + if (d->outfd >= 0 && full_write(d->outfd,
  374. + d->buffer + d->stream_pos, size) != size)
  375. + d->write_error = true;
  376. + if (d->pos >= d->dictionary_size) {
  377. + d->partial_data_pos += d->pos;
  378. + d->pos = 0;
  379. + d->pos_wrapped = true;
  380. + }
  381. + d->stream_pos = d->pos;
  382. + }
  383. +}
  384. +
  385. +static __always_inline uint8_t LZd_peek_prev(const struct LZ_decoder *const d)
  386. +{
  387. + const uint32_t i = ((d->pos > 0) ? d->pos : d->dictionary_size) - 1;
  388. + return d->buffer[i];
  389. +}
  390. +
  391. +static __always_inline uint8_t LZd_peek(const struct LZ_decoder *const d,
  392. + const uint32_t distance)
  393. +{
  394. + uint32_t i = d->pos - distance - 1;
  395. + if (d->pos <= distance) i += d->dictionary_size;
  396. + return d->buffer[i];
  397. +}
  398. +
  399. +static __always_inline void LZd_put_byte(struct LZ_decoder *const d,
  400. + const uint8_t b)
  401. +{
  402. + d->buffer[d->pos] = b;
  403. + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
  404. +}
  405. +
  406. +static void LZd_copy_block(struct LZ_decoder *const d,
  407. + const uint32_t distance, uint32_t len)
  408. +{
  409. + uint32_t i = d->pos - distance - 1;
  410. + bool fast;
  411. + if (d->pos <= distance) {
  412. + i += d->dictionary_size;
  413. + fast = (len <= d->dictionary_size - i && len <= i - d->pos);
  414. + }
  415. + else
  416. + fast = (len < d->dictionary_size - d->pos && len <= d->pos - i);
  417. + if( fast ) { /* no wrap, no overlap */
  418. + memcpy(d->buffer + d->pos, d->buffer + i, len);
  419. + d->pos += len;
  420. + } else for (; len > 0; --len) {
  421. + d->buffer[d->pos] = d->buffer[i];
  422. + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
  423. + if (++i >= d->dictionary_size) i = 0;
  424. + }
  425. +}
  426. +
  427. +static bool LZd_init(struct LZ_decoder *const d,
  428. + struct Range_decoder *const rde,
  429. + const uint32_t dict_size, const int ofd)
  430. +{
  431. + d->partial_data_pos = 0;
  432. + d->rdec = rde;
  433. + d->dictionary_size = dict_size;
  434. + d->buffer = (uint8_t *) malloc(d->dictionary_size);
  435. + if (!d->buffer) return false;
  436. + d->pos = 0;
  437. + d->stream_pos = 0;
  438. + d->crc = 0xFFFFFFFFU;
  439. + d->outfd = ofd;
  440. + d->pos_wrapped = false;
  441. + d->write_error = false;
  442. + d->buffer[d->dictionary_size - 1] = 0; /* prev_byte of first byte */
  443. + return true;
  444. +}
  445. +
  446. +static inline uint32_t LZd_crc(const struct LZ_decoder *const d)
  447. +{
  448. + return d->crc ^ 0xFFFFFFFFU;
  449. +}
  450. +
  451. +static __always_inline unsigned long long
  452. +LZd_data_position(const struct LZ_decoder *const d)
  453. +{
  454. + return d->partial_data_pos + d->pos;
  455. +}
  456. +
  457. +
  458. +static bool LZd_verify_trailer(struct LZ_decoder *const d)
  459. +{
  460. + File_trailer trailer;
  461. + int i = 0;
  462. + while (i < Ft_size)
  463. + trailer[i++] = Rd_get_byte(d->rdec);
  464. +
  465. + return (Ft_get_data_crc(trailer) == LZd_crc(d) &&
  466. + Ft_get_data_size(trailer) == LZd_data_position(d) &&
  467. + Ft_get_member_size(trailer) == Rd_member_position(d->rdec));
  468. +}
  469. +
  470. +
  471. +/* Return value: -1 = write error, 0 = OK, 1 = data error. */
  472. +static int LZd_decode_member(struct LZ_decoder *const d)
  473. +{
  474. + struct Range_decoder * const rdec = d->rdec;
  475. + Bit_model bm_literal[1 << literal_context_bits][0x300];
  476. + Bit_model bm_match[states][pos_states];
  477. + Bit_model bm_rep[states];
  478. + Bit_model bm_rep0[states];
  479. + Bit_model bm_rep1[states];
  480. + Bit_model bm_rep2[states];
  481. + Bit_model bm_len[states][pos_states];
  482. + Bit_model bm_dis_slot[len_states][1 << dis_slot_bits];
  483. + Bit_model bm_dis[modeled_distances-end_dis_model];
  484. + Bit_model bm_align[dis_align_size];
  485. + struct Len_model match_len_model;
  486. + struct Len_model rep_len_model;
  487. + uint32_t rep0 = 0; /* rep[0-3] latest four distances */
  488. + uint32_t rep1 = 0; /* used for efficient coding of */
  489. + uint32_t rep2 = 0; /* repeated distances */
  490. + uint32_t rep3 = 0;
  491. + State state = 0;
  492. +
  493. + Bm_array_init( bm_literal[0], (1 << literal_context_bits) * 0x300 );
  494. + Bm_array_init( bm_match[0], states * pos_states );
  495. + Bm_array_init( bm_rep, states );
  496. + Bm_array_init( bm_rep0, states );
  497. + Bm_array_init( bm_rep1, states );
  498. + Bm_array_init( bm_rep2, states );
  499. + Bm_array_init( bm_len[0], states * pos_states );
  500. + Bm_array_init( bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
  501. + Bm_array_init( bm_dis, modeled_distances - end_dis_model );
  502. + Bm_array_init( bm_align, dis_align_size );
  503. + Lm_init(&match_len_model);
  504. + Lm_init(&rep_len_model);
  505. +
  506. + Rd_load(rdec);
  507. + while (!Rd_finished(rdec)) {
  508. + const int pos_state = LZd_data_position(d) & pos_state_mask;
  509. + if (Rd_decode_bit(rdec, &bm_match[state][pos_state]) == 0) {
  510. + const uint8_t prev_byte = LZd_peek_prev(d);
  511. + if (St_is_char(state)) {
  512. + state -= (state < 4) ? state : 3;
  513. + LZd_put_byte(d, Rd_decode_tree(rdec,
  514. + bm_literal[get_lit_state(prev_byte)], 8));
  515. + } else {
  516. + state -= (state < 10) ? 3 : 6;
  517. + LZd_put_byte(d, Rd_decode_matched(rdec,
  518. + bm_literal[get_lit_state(prev_byte)],
  519. + LZd_peek(d, rep0)));
  520. + }
  521. + } else {
  522. + uint32_t len;
  523. + if (Rd_decode_bit(rdec, &bm_rep[state]) != 0) {
  524. + if (Rd_decode_bit(rdec, &bm_rep0[state]) != 0) {
  525. + uint32_t distance;
  526. + if (Rd_decode_bit(rdec, &bm_rep1[state]) == 0)
  527. + distance = rep1;
  528. + else {
  529. + if (Rd_decode_bit(rdec, &bm_rep2[state]) == 0)
  530. + distance = rep2;
  531. + else {
  532. + distance = rep3;
  533. + rep3 = rep2;
  534. + }
  535. + rep2 = rep1;
  536. + }
  537. + rep1 = rep0;
  538. + rep0 = distance;
  539. + } else {
  540. + if (Rd_decode_bit(rdec, &bm_len[state][pos_state]) == 0) {
  541. + state = St_set_short_rep(state);
  542. + LZd_put_byte(d, LZd_peek(d, rep0));
  543. + continue;
  544. + }
  545. + }
  546. + state = St_set_rep(state);
  547. + len = min_match_len + Rd_decode_len(rdec, &rep_len_model, pos_state);
  548. + } else {
  549. + const uint32_t rep0_saved = rep0;
  550. + uint32_t dis_slot;
  551. + len = min_match_len + Rd_decode_len(rdec, &match_len_model, pos_state);
  552. + dis_slot = Rd_decode_tree(rdec, bm_dis_slot[get_len_state(len)], 6);
  553. + if (dis_slot < start_dis_model) rep0 = dis_slot;
  554. + else {
  555. + const uint32_t direct_bits = (dis_slot >> 1) - 1;
  556. + rep0 = (2 | (dis_slot & 1)) << direct_bits;
  557. + if (dis_slot < end_dis_model)
  558. + rep0 += Rd_decode_tree_reversed(rdec,
  559. + bm_dis + rep0 - dis_slot - 1, direct_bits);
  560. + else {
  561. + rep0 += Rd_decode(rdec, direct_bits - dis_align_bits) << dis_align_bits;
  562. + rep0 += Rd_decode_tree_reversed(rdec, bm_align, dis_align_bits);
  563. + if (rep0 == 0xFFFFFFFFU) { /* marker found */
  564. + rep0 = rep0_saved;
  565. + Rd_normalize(rdec);
  566. + LZd_flush_data(d);
  567. + if (d->write_error) return -1;
  568. + if (len == min_match_len && /* End Of Stream marker */
  569. + LZd_verify_trailer(d))
  570. + return 0;
  571. + if (len == min_match_len + 1) { /* Sync Flush marker */
  572. + Rd_load(rdec);
  573. + continue;
  574. + }
  575. + return 1;
  576. + }
  577. + }
  578. + }
  579. + rep3 = rep2;
  580. + rep2 = rep1;
  581. + rep1 = rep0_saved;
  582. + state = St_set_match(state);
  583. + if (rep0 >= d->dictionary_size ||
  584. + (rep0 >= d->pos && !d->pos_wrapped)) {
  585. + LZd_flush_data(d);
  586. + return 1;
  587. + }
  588. + }
  589. + LZd_copy_block(d, rep0, len);
  590. + }
  591. + }
  592. + LZd_flush_data(d);
  593. + return 1;
  594. +}
  595. +
  596. +
  597. +IF_DESKTOP(long long) int FAST_FUNC
  598. +unpack_lz_stream(transformer_state_t *xstate)
  599. +{
  600. + IF_DESKTOP(long long) int total = 0;
  601. + struct Range_decoder rdec;
  602. + bool first_member;
  603. + const bool magic_skipped = (xstate->signature_skipped != 0);
  604. +
  605. + if (!global_crc32_table)
  606. + global_crc32_table = crc32_filltable(NULL, 0);
  607. +
  608. + if (!Rd_init(&rdec, xstate->src_fd, magic_skipped))
  609. + return -1;
  610. +
  611. + for (first_member = true;; first_member = false) {
  612. + int tmp = 0;
  613. + File_header header;
  614. + struct LZ_decoder decoder;
  615. +
  616. + if (first_member && magic_skipped) {
  617. + Fh_set_magic(header);
  618. + tmp = 4;
  619. + } else {
  620. + Rd_reset_member_position(&rdec);
  621. + }
  622. + while (tmp < Fh_size)
  623. + header[tmp++] = Rd_get_byte(&rdec);
  624. + if (Rd_finished(&rdec)) { /* End Of File */
  625. + if (first_member) {
  626. + bb_error_msg(bb_msg_read_error);
  627. + total = -1;
  628. + }
  629. + break;
  630. + }
  631. + tmp = Fh_get_dictionary_size(header);
  632. + if (!Fh_verify_magic(header) || tmp < min_dictionary_size ||
  633. + tmp > max_dictionary_size) {
  634. + if (!first_member)
  635. + break; /* trailing garbage */
  636. + bb_error_msg("invalid magic");
  637. + total = -1;
  638. + break;
  639. + }
  640. +
  641. + if (!LZd_init(&decoder, &rdec, tmp, xstate->dst_fd)) {
  642. + bb_error_msg(bb_msg_memory_exhausted);
  643. + total = -1;
  644. + break;
  645. + }
  646. + tmp = LZd_decode_member(&decoder);
  647. + IF_DESKTOP(total += Rd_member_position(&rdec);)
  648. + free(decoder.buffer);
  649. + if (tmp != 0) {
  650. + if (tmp < 0)
  651. + bb_perror_msg(bb_msg_write_error);
  652. + else
  653. + bb_error_msg("corrupted data");
  654. + total = -1;
  655. + break;
  656. + }
  657. + }
  658. + free(rdec.buffer);
  659. + return total;
  660. +}
  661. diff -urdN busybox-1.27.0.old/archival/libarchive/filter_accept_list_reassign.c busybox-1.27.0.new/archival/libarchive/filter_accept_list_reassign.c
  662. --- busybox-1.27.0.old/archival/libarchive/filter_accept_list_reassign.c 2016-11-27 03:55:16.000000000 +0100
  663. +++ busybox-1.27.0.new/archival/libarchive/filter_accept_list_reassign.c 2017-07-07 00:24:37.000000000 +0200
  664. @@ -44,6 +44,12 @@
  665. archive_handle->dpkg__action_data_subarchive = get_header_tar_bz2;
  666. return EXIT_SUCCESS;
  667. }
  668. + if (ENABLE_FEATURE_SEAMLESS_LZ
  669. + && strcmp(name_ptr, "lz") == 0
  670. + ) {
  671. + archive_handle->dpkg__action_data_subarchive = get_header_tar_lz;
  672. + return EXIT_SUCCESS;
  673. + }
  674. if (ENABLE_FEATURE_SEAMLESS_LZMA
  675. && strcmp(name_ptr, "lzma") == 0
  676. ) {
  677. diff -urdN busybox-1.27.0.old/archival/libarchive/get_header_tar_lz.c busybox-1.27.0.new/archival/libarchive/get_header_tar_lz.c
  678. --- busybox-1.27.0.old/archival/libarchive/get_header_tar_lz.c 1970-01-01 01:00:00.000000000 +0100
  679. +++ busybox-1.27.0.new/archival/libarchive/get_header_tar_lz.c 2017-07-07 00:24:37.000000000 +0200
  680. @@ -0,0 +1,21 @@
  681. +/* vi: set sw=4 ts=4: */
  682. +/*
  683. + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  684. + */
  685. +
  686. +#include "libbb.h"
  687. +#include "bb_archive.h"
  688. +
  689. +char FAST_FUNC get_header_tar_lz(archive_handle_t *archive_handle)
  690. +{
  691. + /* Can't lseek over pipes */
  692. + archive_handle->seek = seek_by_read;
  693. +
  694. + fork_transformer_with_sig(archive_handle->src_fd, unpack_lz_stream, "lunzip");
  695. + archive_handle->offset = 0;
  696. + while (get_header_tar(archive_handle) == EXIT_SUCCESS)
  697. + continue;
  698. +
  699. + /* Can only do one file at a time */
  700. + return EXIT_FAILURE;
  701. +}
  702. diff -urdN busybox-1.27.0.old/archival/libarchive/lzip.h busybox-1.27.0.new/archival/libarchive/lzip.h
  703. --- busybox-1.27.0.old/archival/libarchive/lzip.h 1970-01-01 01:00:00.000000000 +0100
  704. +++ busybox-1.27.0.new/archival/libarchive/lzip.h 2017-07-07 00:24:37.000000000 +0200
  705. @@ -0,0 +1,250 @@
  706. +/* Lzip - LZMA lossless data compressor
  707. + Copyright (C) 2008-2016 Antonio Diaz Diaz.
  708. +
  709. + This program is free software: you can redistribute it and/or modify
  710. + it under the terms of the GNU General Public License as published by
  711. + the Free Software Foundation, either version 2 of the License, or
  712. + (at your option) any later version.
  713. +
  714. + This program is distributed in the hope that it will be useful,
  715. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  716. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  717. + GNU General Public License for more details.
  718. +
  719. + You should have received a copy of the GNU General Public License
  720. + along with this program. If not, see <http://www.gnu.org/licenses/>.
  721. +*/
  722. +
  723. +typedef int State;
  724. +
  725. +enum { states = 12 };
  726. +
  727. +static inline bool St_is_char(const State st) { return st < 7; }
  728. +
  729. +static inline State St_set_char(const State st)
  730. +{
  731. + static const State next[states] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
  732. + return next[st];
  733. +}
  734. +
  735. +static inline State St_set_match(const State st)
  736. +{
  737. + return ((st < 7) ? 7 : 10);
  738. +}
  739. +
  740. +static inline State St_set_rep(const State st)
  741. +{
  742. + return ((st < 7) ? 8 : 11);
  743. +}
  744. +
  745. +static inline State St_set_short_rep(const State st)
  746. +{
  747. + return ((st < 7) ? 9 : 11);
  748. +}
  749. +
  750. +
  751. +enum {
  752. + min_dictionary_bits = 12,
  753. + min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
  754. + max_dictionary_bits = 29,
  755. + max_dictionary_size = 1 << max_dictionary_bits,
  756. + literal_context_bits = 3,
  757. + pos_state_bits = 2,
  758. + pos_states = 1 << pos_state_bits,
  759. + pos_state_mask = pos_states - 1,
  760. +
  761. + len_states = 4,
  762. + dis_slot_bits = 6,
  763. + start_dis_model = 4,
  764. + end_dis_model = 14,
  765. + modeled_distances = 1 << (end_dis_model / 2), /* 128 */
  766. + dis_align_bits = 4,
  767. + dis_align_size = 1 << dis_align_bits,
  768. +
  769. + len_low_bits = 3,
  770. + len_mid_bits = 3,
  771. + len_high_bits = 8,
  772. + len_low_symbols = 1 << len_low_bits,
  773. + len_mid_symbols = 1 << len_mid_bits,
  774. + len_high_symbols = 1 << len_high_bits,
  775. + max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
  776. +
  777. + min_match_len = 2, /* must be 2 */
  778. + max_match_len = min_match_len + max_len_symbols - 1, /* 273 */
  779. + min_match_len_limit = 5,
  780. +
  781. + lz_num_models =
  782. + ((1 << literal_context_bits) * 0x300) +
  783. + (2 * states * pos_states) +
  784. + (4 * states) +
  785. + (len_states * (1 << dis_slot_bits)) +
  786. + (modeled_distances - end_dis_model) +
  787. + dis_align_size,
  788. +};
  789. +
  790. +static inline int get_len_state(const int len)
  791. +{
  792. + return MIN(len - min_match_len, len_states - 1);
  793. +}
  794. +
  795. +static inline int get_lit_state(const uint8_t prev_byte)
  796. +{
  797. + return (prev_byte >> (8 - literal_context_bits));
  798. +}
  799. +
  800. +
  801. +enum { bit_model_move_bits = 5,
  802. + bit_model_total_bits = 11,
  803. + bit_model_total = 1 << bit_model_total_bits
  804. +};
  805. +
  806. +typedef int Bit_model;
  807. +
  808. +static inline void Bm_init(Bit_model * const probability)
  809. +{
  810. + *probability = bit_model_total / 2;
  811. +}
  812. +
  813. +static inline void Bm_array_init(Bit_model * const p, const int size)
  814. +{
  815. + int i = 0;
  816. + while (i < size)
  817. + p[i++] = bit_model_total / 2;
  818. +}
  819. +
  820. +struct Len_model {
  821. + Bit_model choice1;
  822. + Bit_model choice2;
  823. + Bit_model bm_low[pos_states][len_low_symbols];
  824. + Bit_model bm_mid[pos_states][len_mid_symbols];
  825. + Bit_model bm_high[len_high_symbols];
  826. +};
  827. +
  828. +static inline void Lm_init(struct Len_model * const lm)
  829. +{
  830. + Bm_init(&lm->choice1);
  831. + Bm_init(&lm->choice2);
  832. + Bm_array_init(lm->bm_low[0], pos_states * len_low_symbols);
  833. + Bm_array_init(lm->bm_mid[0], pos_states * len_mid_symbols);
  834. + Bm_array_init(lm->bm_high, len_high_symbols);
  835. +}
  836. +
  837. +
  838. +static inline int real_bits(unsigned value)
  839. +{
  840. + int bits = 0;
  841. + while(value > 0) { value >>= 1; ++bits; }
  842. + return bits;
  843. +}
  844. +
  845. +
  846. +static const uint8_t magic_string[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
  847. +
  848. +typedef uint8_t File_header[6]; /* 0-3 magic bytes */
  849. + /* 4 version */
  850. + /* 5 coded_dict_size */
  851. +enum { Fh_size = 6 };
  852. +
  853. +static inline void Fh_set_magic(File_header data)
  854. +{
  855. + memcpy(data, magic_string, 4);
  856. + data[4] = 1;
  857. +}
  858. +
  859. +static inline bool Fh_verify_magic(const File_header data)
  860. +{
  861. + return (memcmp(data, magic_string, 4) == 0 && data[4] == 1);
  862. +}
  863. +
  864. +static inline unsigned Fh_get_dictionary_size(const File_header data)
  865. +{
  866. + unsigned sz = (1 << (data[5] & 0x1F));
  867. + if (sz > min_dictionary_size)
  868. + sz -= (sz / 16) * ((data[5] >> 5) & 7);
  869. + return sz;
  870. +}
  871. +
  872. +static inline bool Fh_set_dictionary_size(File_header data, const unsigned sz)
  873. +{
  874. + if (sz < min_dictionary_size || sz > max_dictionary_size) return false;
  875. + data[5] = real_bits(sz - 1);
  876. + if (sz > min_dictionary_size) {
  877. + const unsigned base_size = 1 << data[5];
  878. + const unsigned fraction = base_size / 16;
  879. + unsigned i;
  880. + for (i = 7; i >= 1; --i)
  881. + if (base_size - (i * fraction) >= sz) {
  882. + data[5] |= (i << 5);
  883. + break;
  884. + }
  885. + }
  886. + return true;
  887. +}
  888. +
  889. +
  890. +typedef uint8_t File_trailer[20];
  891. + /* 0-3 CRC32 of the uncompressed data */
  892. + /* 4-11 size of the uncompressed data */
  893. + /* 12-19 member size including header and trailer */
  894. +
  895. +enum { Ft_size = 20 };
  896. +
  897. +static inline unsigned Ft_get_data_crc(const File_trailer data)
  898. +{
  899. + unsigned tmp = 0;
  900. + int i;
  901. + for (i = 3; i >= 0; --i) {
  902. + tmp <<= 8;
  903. + tmp += data[i];
  904. + }
  905. + return tmp;
  906. +}
  907. +
  908. +static inline void Ft_set_data_crc(File_trailer data, unsigned crc)
  909. +{
  910. + int i;
  911. + for (i = 0; i <= 3; ++i) {
  912. + data[i] = (uint8_t)crc;
  913. + crc >>= 8;
  914. + }
  915. +}
  916. +
  917. +static inline unsigned long long Ft_get_data_size(const File_trailer data)
  918. +{
  919. + unsigned long long tmp = 0;
  920. + int i;
  921. + for (i = 11; i >= 4; --i) {
  922. + tmp <<= 8;
  923. + tmp += data[i];
  924. + }
  925. + return tmp;
  926. +}
  927. +
  928. +static inline void Ft_set_data_size(File_trailer data, unsigned long long sz)
  929. +{
  930. + int i;
  931. + for (i = 4; i <= 11; ++i) {
  932. + data[i] = (uint8_t)sz;
  933. + sz >>= 8;
  934. + }
  935. +}
  936. +
  937. +static inline unsigned long long Ft_get_member_size(const File_trailer data)
  938. +{
  939. + unsigned long long tmp = 0;
  940. + int i;
  941. + for (i = 19; i >= 12; --i) {
  942. + tmp <<= 8;
  943. + tmp += data[i];
  944. + }
  945. + return tmp;
  946. +}
  947. +
  948. +static inline void Ft_set_member_size(File_trailer data, unsigned long long sz)
  949. +{
  950. + int i;
  951. + for (i = 12; i <= 19; ++i) {
  952. + data[i] = (uint8_t)sz;
  953. + sz >>= 8;
  954. + }
  955. +}
  956. diff -urdN busybox-1.27.0.old/archival/libarchive/open_transformer.c busybox-1.27.0.new/archival/libarchive/open_transformer.c
  957. --- busybox-1.27.0.old/archival/libarchive/open_transformer.c 2016-11-27 03:55:16.000000000 +0100
  958. +++ busybox-1.27.0.new/archival/libarchive/open_transformer.c 2017-07-07 00:24:37.000000000 +0200
  959. @@ -195,6 +195,17 @@
  960. USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
  961. goto found_magic;
  962. }
  963. + if (ENABLE_FEATURE_SEAMLESS_LZ
  964. + && magic.b16[0] == LZIP_MAGIC1
  965. + ) {
  966. + xstate->signature_skipped = 4;
  967. + xread(fd, magic.b16, sizeof(magic.b16[0]));
  968. + if (magic.b16[0] == LZIP_MAGIC2) {
  969. + xstate->xformer = unpack_lz_stream;
  970. + USE_FOR_NOMMU(xstate->xformer_prog = "lunzip";)
  971. + goto found_magic;
  972. + }
  973. + }
  974. if (ENABLE_FEATURE_SEAMLESS_XZ
  975. && magic.b16[0] == XZ_MAGIC1
  976. ) {
  977. @@ -211,6 +222,7 @@
  978. if (fail_if_not_compressed)
  979. bb_error_msg_and_die("no gzip"
  980. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  981. + IF_FEATURE_SEAMLESS_LZ("/lzip")
  982. IF_FEATURE_SEAMLESS_XZ("/xz")
  983. " magic");
  984. diff -urdN busybox-1.27.0.old/archival/lzip.c busybox-1.27.0.new/archival/lzip.c
  985. --- busybox-1.27.0.old/archival/lzip.c 1970-01-01 01:00:00.000000000 +0100
  986. +++ busybox-1.27.0.new/archival/lzip.c 2017-07-07 16:49:00.000000000 +0200
  987. @@ -0,0 +1,1434 @@
  988. +/*
  989. + * lzip implementation for busybox
  990. + *
  991. + * Copyright (C) 2012-2016 Antonio Diaz Diaz.
  992. + *
  993. + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  994. + */
  995. +
  996. +//config:config LZIP
  997. +//config: bool "lzip"
  998. +//config: default y
  999. +//config: help
  1000. +//config: lzip is a lossless data compressor with a user interface similar to
  1001. +//config: the one of gzip or bzip2. lzip can compress about as fast as gzip or
  1002. +//config: compress most files more than bzip2 (depending on compression level).
  1003. +//config: Decompression speed is intermediate between gzip and bzip2. lzip is
  1004. +//config: better than gzip and bzip2 from a data recovery perspective.
  1005. +
  1006. +//applet:IF_LZIP(APPLET(lzip, BB_DIR_USR_BIN, BB_SUID_DROP))
  1007. +//kbuild:lib-$(CONFIG_LZIP) += lzip.o bbunzip.o
  1008. +
  1009. +//usage:#define lzip_trivial_usage
  1010. +//usage: "[-123456789c"
  1011. +//usage: IF_LUNZIP("d") "fk"
  1012. +//usage: IF_LUNZIP("t")
  1013. +//usage: "] [-m MATCH_LENGTH] [-s DICT_SIZE] [FILE]..."
  1014. +//usage:#define lzip_full_usage "\n\n"
  1015. +//usage: "Compress FILEs (or stdin) with lzip algorithm\n"
  1016. +//usage: "\n -1..9 Compression level"
  1017. +//usage: "\n -c Write to stdout"
  1018. +//usage: IF_LUNZIP("\n -d Decompress")
  1019. +//usage: "\n -f Force"
  1020. +//usage: "\n -k Keep input files"
  1021. +//usage: "\n -m Match length limit [36]"
  1022. +//usage: "\n -s Dictionary size limit [8MiB]"
  1023. +//usage: IF_LUNZIP("\n -t Test compressed file integrity")
  1024. +
  1025. +
  1026. +#include "libbb.h"
  1027. +#include "bb_archive.h"
  1028. +#include "libarchive/lzip.h"
  1029. +
  1030. +
  1031. +#if CHAR_BIT != 8
  1032. +#error "Environments where CHAR_BIT != 8 are not supported."
  1033. +#endif
  1034. +
  1035. +
  1036. +static void CRC32_update_byte(uint32_t * crc, const uint8_t byte)
  1037. +{
  1038. + *crc = global_crc32_table[(*crc ^ byte) & 0xFF] ^ (*crc >> 8);
  1039. +}
  1040. +
  1041. +
  1042. +enum { max_num_trials = 1 << 12,
  1043. + price_shift_bits = 6
  1044. +};
  1045. +
  1046. +
  1047. +static uint8_t * dis_slots;
  1048. +
  1049. +static void Dis_slots_init(void)
  1050. +{
  1051. + int i, size, slot;
  1052. + dis_slots = xmalloc((1 << 10) * sizeof dis_slots[0]);
  1053. +
  1054. + for (slot = 0; slot < 4; ++slot) dis_slots[slot] = slot;
  1055. + for (i = 4, size = 2, slot = 4; slot < 20; slot += 2) {
  1056. + memset(&dis_slots[i], slot, size);
  1057. + memset(&dis_slots[i + size], slot + 1, size);
  1058. + size <<= 1;
  1059. + i += size;
  1060. + }
  1061. +}
  1062. +
  1063. +static uint8_t get_slot(const unsigned dis)
  1064. +{
  1065. + if (dis < (1 << 10)) return dis_slots[dis];
  1066. + if (dis < (1 << 19)) return dis_slots[dis>> 9] + 18;
  1067. + if (dis < (1 << 28)) return dis_slots[dis>>18] + 36;
  1068. + return dis_slots[dis>>27] + 54;
  1069. +}
  1070. +
  1071. +
  1072. +static int * prob_prices;
  1073. +
  1074. +static void Prob_prices_init(void)
  1075. +{
  1076. + const int num_bits = (bit_model_total_bits - 2);
  1077. + int i, j = 1, end = 2;
  1078. + prob_prices = xmalloc((bit_model_total >> 2) * sizeof prob_prices[0]);
  1079. +
  1080. + prob_prices[0] = bit_model_total_bits << price_shift_bits;
  1081. + for (i = num_bits - 1; i >= 0; --i, end <<= 1) {
  1082. + for (; j < end; ++j)
  1083. + prob_prices[j] = (i << price_shift_bits) +
  1084. + (((end - j) << price_shift_bits) >> (num_bits - i - 1));
  1085. + }
  1086. +}
  1087. +
  1088. +static inline int get_price(const int probability)
  1089. +{
  1090. + return prob_prices[probability >> 2];
  1091. +}
  1092. +
  1093. +
  1094. +static inline int price0(const Bit_model probability)
  1095. +{
  1096. + return get_price(probability);
  1097. +}
  1098. +
  1099. +static inline int price1(const Bit_model probability)
  1100. +{
  1101. + return get_price(bit_model_total - probability);
  1102. +}
  1103. +
  1104. +static int price_bit(const Bit_model bm, const int bit)
  1105. +{
  1106. + if (bit) return price1(bm);
  1107. + else return price0(bm);
  1108. +}
  1109. +
  1110. +
  1111. +static int price_symbol(const Bit_model bm[], int symbol,
  1112. + const int num_bits)
  1113. +{
  1114. + int price = 0;
  1115. + symbol |= (1 << num_bits);
  1116. + while (symbol > 1) {
  1117. + const int bit = symbol & 1;
  1118. + symbol >>= 1;
  1119. + price += price_bit(bm[symbol], bit);
  1120. + }
  1121. + return price;
  1122. +}
  1123. +
  1124. +
  1125. +static int price_symbol_reversed(const Bit_model bm[], int symbol,
  1126. + const int num_bits)
  1127. +{
  1128. + int price = 0;
  1129. + int model = 1;
  1130. + int i;
  1131. + for (i = num_bits; i > 0; --i) {
  1132. + const int bit = symbol & 1;
  1133. + price += price_bit(bm[model], bit);
  1134. + model = (model << 1) | bit;
  1135. + symbol >>= 1;
  1136. + }
  1137. + return price;
  1138. +}
  1139. +
  1140. +
  1141. +static int price_matched(const Bit_model bm[], int symbol, int match_byte)
  1142. +{
  1143. + int price = 0;
  1144. + int mask = 0x100;
  1145. + symbol |= mask;
  1146. +
  1147. + do {
  1148. + int match_bit, bit;
  1149. + match_byte <<= 1;
  1150. + match_bit = match_byte & mask;
  1151. + symbol <<= 1;
  1152. + bit = symbol & 0x100;
  1153. + price += price_bit( bm[match_bit+(symbol>>9)+mask], bit );
  1154. + mask &= ~(match_byte ^ symbol); /* if( match_bit != bit ) mask = 0; */
  1155. + }
  1156. + while( symbol < 0x10000 );
  1157. + return price;
  1158. +}
  1159. +
  1160. +
  1161. +enum { /* bytes to keep in buffer before dictionary */
  1162. + before_size = max_num_trials + 1,
  1163. + /* bytes to keep in buffer after pos */
  1164. + after_size = max_match_len,
  1165. + num_prev_positions4 = 1 << 20,
  1166. + num_prev_positions3 = 1 << 18,
  1167. + num_prev_positions2 = 1 << 16,
  1168. + num_prev_positions = num_prev_positions4 + num_prev_positions3 +
  1169. + num_prev_positions2
  1170. +};
  1171. +
  1172. +struct Matchfinder {
  1173. + unsigned long long partial_data_pos;
  1174. + uint8_t *buffer; /* input buffer */
  1175. + int32_t *prev_positions; /* last seen position of key */
  1176. + int32_t *prev_pos_tree; /* previous positions of key */
  1177. + int match_len_limit;
  1178. + int buffer_size;
  1179. + int dictionary_size; /* bytes to keep in buffer before pos */
  1180. + int pos; /* current pos in buffer */
  1181. + int cyclic_pos; /* current pos in dictionary */
  1182. + int stream_pos; /* first byte not yet read from file */
  1183. + int pos_limit; /* when reached, a new block must be read */
  1184. + int cycles;
  1185. + bool at_stream_end; /* stream_pos shows real end of file */
  1186. +};
  1187. +
  1188. +static bool Mf_read_block(struct Matchfinder *const mf)
  1189. +{
  1190. + if (!mf->at_stream_end && mf->stream_pos < mf->buffer_size) {
  1191. + const int size = mf->buffer_size - mf->stream_pos;
  1192. + const int rd = full_read(STDIN_FILENO,
  1193. + mf->buffer + mf->stream_pos, size);
  1194. + mf->stream_pos += rd;
  1195. + if (rd < size) {
  1196. + mf->at_stream_end = true;
  1197. + mf->pos_limit = mf->buffer_size;
  1198. + }
  1199. + }
  1200. + return mf->pos < mf->stream_pos;
  1201. +}
  1202. +
  1203. +static void Mf_normalize_pos(struct Matchfinder *const mf)
  1204. +{
  1205. + if (!mf->at_stream_end) {
  1206. + int i;
  1207. + const int offset = mf->pos - mf->dictionary_size - before_size;
  1208. + const int size = mf->stream_pos - offset;
  1209. + memmove(mf->buffer, mf->buffer + offset, size);
  1210. + mf->partial_data_pos += offset;
  1211. + mf->pos -= offset;
  1212. + mf->stream_pos -= offset;
  1213. + for (i = 0; i < num_prev_positions; ++i)
  1214. + if (mf->prev_positions[i] >= 0)
  1215. + mf->prev_positions[i] -= offset;
  1216. + for (i = 0; i < 2 * mf->dictionary_size; ++i)
  1217. + if (mf->prev_pos_tree[i] >= 0)
  1218. + mf->prev_pos_tree[i] -= offset;
  1219. + Mf_read_block(mf);
  1220. + }
  1221. +}
  1222. +
  1223. +static bool Mf_init(struct Matchfinder *const mf, const int dict_size,
  1224. + const int match_len_limit)
  1225. +{
  1226. + const int buffer_size_limit = (2 * dict_size) + before_size + after_size;
  1227. + int i;
  1228. +
  1229. + mf->partial_data_pos = 0;
  1230. + mf->match_len_limit = match_len_limit;
  1231. + mf->prev_positions = (int32_t *) malloc(num_prev_positions * sizeof(int32_t));
  1232. + if (!mf->prev_positions) return false;
  1233. + mf->pos = 0;
  1234. + mf->cyclic_pos = 0;
  1235. + mf->stream_pos = 0;
  1236. + mf->cycles = (match_len_limit < max_match_len) ?
  1237. + 16 + (match_len_limit / 2) : 256;
  1238. + mf->at_stream_end = false;
  1239. +
  1240. + for (i = 0; i < num_prev_positions; ++i)
  1241. + mf->prev_positions[i] = -1;
  1242. + mf->buffer_size = MAX(65536, dict_size);
  1243. + mf->buffer = (uint8_t *) malloc(mf->buffer_size);
  1244. + if (!mf->buffer) {
  1245. + free(mf->prev_positions);
  1246. + return false;
  1247. + }
  1248. + if (Mf_read_block(mf) && !mf->at_stream_end &&
  1249. + mf->buffer_size < buffer_size_limit) {
  1250. + uint8_t *tmp;
  1251. + mf->buffer_size = buffer_size_limit;
  1252. + tmp = (uint8_t *) realloc(mf->buffer, mf->buffer_size);
  1253. + if (!tmp) {
  1254. + free(mf->buffer);
  1255. + free(mf->prev_positions);
  1256. + return false;
  1257. + }
  1258. + mf->buffer = tmp;
  1259. + Mf_read_block(mf);
  1260. + }
  1261. + if (mf->at_stream_end && mf->stream_pos < dict_size)
  1262. + mf->dictionary_size = MAX(min_dictionary_size, mf->stream_pos);
  1263. + else
  1264. + mf->dictionary_size = dict_size;
  1265. + mf->pos_limit = mf->buffer_size;
  1266. + if (!mf->at_stream_end) mf->pos_limit -= after_size;
  1267. + mf->prev_pos_tree =
  1268. + (int32_t *) malloc(2 * mf->dictionary_size * sizeof(int32_t));
  1269. + if (!mf->prev_pos_tree) {
  1270. + free(mf->buffer);
  1271. + free(mf->prev_positions);
  1272. + return false;
  1273. + }
  1274. + return true;
  1275. +}
  1276. +
  1277. +static void Mf_free(struct Matchfinder *const mf)
  1278. +{
  1279. + free(mf->prev_pos_tree);
  1280. + free(mf->buffer);
  1281. + free(mf->prev_positions);
  1282. +}
  1283. +
  1284. +static inline uint8_t Mf_peek(const struct Matchfinder *const mf,
  1285. + const int distance)
  1286. +{
  1287. + return mf->buffer[mf->pos-distance];
  1288. +}
  1289. +
  1290. +static inline int Mf_available_bytes(const struct Matchfinder *const mf)
  1291. +{
  1292. + return mf->stream_pos - mf->pos;
  1293. +}
  1294. +
  1295. +static inline unsigned long long
  1296. +Mf_data_position(const struct Matchfinder *const mf)
  1297. +{
  1298. + return mf->partial_data_pos + mf->pos;
  1299. +}
  1300. +
  1301. +static inline bool Mf_finished(const struct Matchfinder *const mf)
  1302. +{
  1303. + return mf->at_stream_end && mf->pos >= mf->stream_pos;
  1304. +}
  1305. +
  1306. +static inline const uint8_t *
  1307. +Mf_ptr_to_current_pos(const struct Matchfinder *const mf)
  1308. +{
  1309. + return mf->buffer + mf->pos;
  1310. +}
  1311. +
  1312. +static int Mf_true_match_len(const struct Matchfinder *const mf,
  1313. + const int index, const int distance, int len_limit)
  1314. +{
  1315. + const uint8_t *const data = mf->buffer + mf->pos + index;
  1316. + int i = 0;
  1317. + if (index + len_limit > Mf_available_bytes(mf))
  1318. + len_limit = Mf_available_bytes(mf) - index;
  1319. + while (i < len_limit && data[i - distance] == data[i]) ++i;
  1320. + return i;
  1321. +}
  1322. +
  1323. +static void Mf_move_pos(struct Matchfinder *const mf)
  1324. +{
  1325. + if (++mf->cyclic_pos >= mf->dictionary_size) mf->cyclic_pos = 0;
  1326. + if (++mf->pos >= mf->pos_limit) Mf_normalize_pos(mf);
  1327. +}
  1328. +
  1329. +static int Mf_longest_match_len(struct Matchfinder *const mf,
  1330. + int *const distances)
  1331. +{
  1332. + int32_t *ptr0 = mf->prev_pos_tree + (mf->cyclic_pos << 1);
  1333. + int32_t *ptr1 = ptr0 + 1;
  1334. + int32_t *newptr;
  1335. + const uint8_t *newdata;
  1336. + int len = 0, len0 = 0, len1 = 0;
  1337. + int maxlen = min_match_len - 1;
  1338. + const int min_pos = (mf->pos >= mf->dictionary_size) ?
  1339. + (mf->pos - mf->dictionary_size + 1) : 0;
  1340. + const uint8_t *const data = mf->buffer + mf->pos;
  1341. + int count, delta, key2, key3, key4, newpos, tmp;
  1342. + int len_limit = mf->match_len_limit;
  1343. +
  1344. + if (len_limit > Mf_available_bytes(mf)) {
  1345. + len_limit = Mf_available_bytes(mf);
  1346. + if (len_limit < 4) return 0;
  1347. + }
  1348. +
  1349. + key2 = num_prev_positions4 + num_prev_positions3 +
  1350. + (((int) data[0] << 8) | data[1]);
  1351. + tmp = global_crc32_table[data[0]] ^ data[1] ^ ((uint32_t) data[2] << 8);
  1352. + key3 = num_prev_positions4 + (int) (tmp & (num_prev_positions3 - 1));
  1353. + key4 = (int) ((tmp ^ (global_crc32_table[data[3]] << 5)) &
  1354. + (num_prev_positions4 - 1));
  1355. +
  1356. + if (distances) {
  1357. + int np = mf->prev_positions[key2];
  1358. + if (np >= min_pos) {
  1359. + distances[2] = mf->pos - np - 1;
  1360. + maxlen = 2;
  1361. + } else
  1362. + distances[2] = 0x7FFFFFFF;
  1363. + np = mf->prev_positions[key3];
  1364. + if (np >= min_pos && mf->buffer[np] == data[0]) {
  1365. + distances[3] = mf->pos - np - 1;
  1366. + maxlen = 3;
  1367. + } else
  1368. + distances[3] = 0x7FFFFFFF;
  1369. + distances[4] = 0x7FFFFFFF;
  1370. + }
  1371. +
  1372. + mf->prev_positions[key2] = mf->pos;
  1373. + mf->prev_positions[key3] = mf->pos;
  1374. + newpos = mf->prev_positions[key4];
  1375. + mf->prev_positions[key4] = mf->pos;
  1376. +
  1377. + for (count = mf->cycles;;) {
  1378. + if (newpos < min_pos || --count < 0) {
  1379. + *ptr0 = *ptr1 = -1;
  1380. + break;
  1381. + }
  1382. + newdata = mf->buffer + newpos;
  1383. + while (len < len_limit && newdata[len] == data[len]) ++len;
  1384. +
  1385. + delta = mf->pos - newpos;
  1386. + if (distances)
  1387. + while (maxlen < len)
  1388. + distances[++maxlen] = delta - 1;
  1389. +
  1390. + newptr = mf->prev_pos_tree +
  1391. + ((mf->cyclic_pos - delta +
  1392. + ((mf->cyclic_pos >= delta) ? 0 : mf->dictionary_size)) << 1);
  1393. +
  1394. + if (len < len_limit) {
  1395. + if (newdata[len] < data[len]) {
  1396. + *ptr0 = newpos;
  1397. + ptr0 = newptr + 1;
  1398. + newpos = *ptr0;
  1399. + len0 = len;
  1400. + if (len1 < len) len = len1;
  1401. + } else {
  1402. + *ptr1 = newpos;
  1403. + ptr1 = newptr;
  1404. + newpos = *ptr1;
  1405. + len1 = len;
  1406. + if (len0 < len) len = len0;
  1407. + }
  1408. + } else {
  1409. + *ptr0 = newptr[0];
  1410. + *ptr1 = newptr[1];
  1411. + break;
  1412. + }
  1413. + }
  1414. + if (distances) {
  1415. + if (distances[3] > distances[4])
  1416. + distances[3] = distances[4];
  1417. + if (distances[2] > distances[3])
  1418. + distances[2] = distances[3];
  1419. + }
  1420. + return maxlen;
  1421. +}
  1422. +
  1423. +
  1424. +enum { re_buffer_size = 16384 };
  1425. +
  1426. +struct Range_encoder {
  1427. + uint64_t low;
  1428. + unsigned long long partial_member_pos;
  1429. + uint8_t *buffer; /* output buffer */
  1430. + int pos; /* current pos in buffer */
  1431. + uint32_t range;
  1432. + unsigned ff_count;
  1433. + uint8_t cache;
  1434. + bool write_error;
  1435. +};
  1436. +
  1437. +static void Re_flush_data(struct Range_encoder *const renc)
  1438. +{
  1439. + if (renc->pos > 0) {
  1440. + if (full_write(STDOUT_FILENO, renc->buffer, renc->pos) != renc->pos)
  1441. + renc->write_error = true;
  1442. + renc->partial_member_pos += renc->pos;
  1443. + renc->pos = 0;
  1444. + }
  1445. +}
  1446. +
  1447. +static void Re_put_byte(struct Range_encoder *const renc, const uint8_t b)
  1448. +{
  1449. + renc->buffer[renc->pos] = b;
  1450. + if (++renc->pos >= re_buffer_size) Re_flush_data(renc);
  1451. +}
  1452. +
  1453. +static void Re_shift_low(struct Range_encoder *const renc)
  1454. +{
  1455. + const bool carry = (renc->low > 0xFFFFFFFFU);
  1456. + if (carry || renc->low < 0xFF000000U) {
  1457. + Re_put_byte(renc, renc->cache + carry);
  1458. + for (; renc->ff_count > 0; --renc->ff_count)
  1459. + Re_put_byte(renc, 0xFF + carry);
  1460. + renc->cache = renc->low >> 24;
  1461. + } else
  1462. + ++renc->ff_count;
  1463. + renc->low = (renc->low & 0x00FFFFFFU) << 8;
  1464. +}
  1465. +
  1466. +static bool Re_init(struct Range_encoder *const renc)
  1467. +{
  1468. + renc->low = 0;
  1469. + renc->partial_member_pos = 0;
  1470. + renc->buffer = (uint8_t *) malloc(re_buffer_size);
  1471. + if (!renc->buffer) return false;
  1472. + renc->pos = 0;
  1473. + renc->range = 0xFFFFFFFFU;
  1474. + renc->ff_count = 0;
  1475. + renc->cache = 0;
  1476. + renc->write_error = false;
  1477. + return true;
  1478. +}
  1479. +
  1480. +static inline void Re_free(struct Range_encoder *const renc)
  1481. +{
  1482. + free(renc->buffer);
  1483. +}
  1484. +
  1485. +static inline unsigned long long
  1486. +Re_member_position(const struct Range_encoder *const renc)
  1487. +{
  1488. + return renc->partial_member_pos + renc->pos + renc->ff_count;
  1489. +}
  1490. +
  1491. +static void Re_flush(struct Range_encoder *const renc)
  1492. +{
  1493. + int i;
  1494. + for (i = 0; i < 5; ++i) Re_shift_low(renc);
  1495. +}
  1496. +
  1497. +static void Re_encode(struct Range_encoder *const renc,
  1498. + const int symbol, const int num_bits)
  1499. +{
  1500. + int i;
  1501. + for (i = num_bits - 1; i >= 0; --i) {
  1502. + renc->range >>= 1;
  1503. + if ((symbol >> i) & 1) renc->low += renc->range;
  1504. + if (renc->range <= 0x00FFFFFFU) {
  1505. + renc->range <<= 8;
  1506. + Re_shift_low(renc);
  1507. + }
  1508. + }
  1509. +}
  1510. +
  1511. +static void Re_encode_bit(struct Range_encoder *const renc,
  1512. + Bit_model * const probability, const int bit)
  1513. +{
  1514. + const uint32_t bound = (renc->range >> bit_model_total_bits) * *probability;
  1515. + if (!bit) {
  1516. + renc->range = bound;
  1517. + *probability += (bit_model_total - *probability) >> bit_model_move_bits;
  1518. + } else {
  1519. + renc->low += bound;
  1520. + renc->range -= bound;
  1521. + *probability -= *probability >> bit_model_move_bits;
  1522. + }
  1523. + if (renc->range <= 0x00FFFFFFU) {
  1524. + renc->range <<= 8;
  1525. + Re_shift_low(renc);
  1526. + }
  1527. +}
  1528. +
  1529. +static void Re_encode_tree(struct Range_encoder *const renc,
  1530. + Bit_model bm[], const int symbol,
  1531. + const int num_bits)
  1532. +{
  1533. + int mask = (1 << (num_bits - 1));
  1534. + int model = 1;
  1535. + int i;
  1536. + for (i = num_bits; i > 0; --i, mask >>= 1) {
  1537. + const int bit = (symbol & mask);
  1538. + Re_encode_bit(renc, &bm[model], bit);
  1539. + model <<= 1;
  1540. + if (bit) model |= 1;
  1541. + }
  1542. +}
  1543. +
  1544. +static void Re_encode_tree_reversed(struct Range_encoder *const renc,
  1545. + Bit_model bm[], int symbol, const int num_bits)
  1546. +{
  1547. + int model = 1;
  1548. + int i;
  1549. + for (i = num_bits; i > 0; --i) {
  1550. + const int bit = symbol & 1;
  1551. + Re_encode_bit(renc, &bm[model], bit);
  1552. + model = (model << 1) | bit;
  1553. + symbol >>= 1;
  1554. + }
  1555. +}
  1556. +
  1557. +static void Re_encode_matched(struct Range_encoder *const renc,
  1558. + Bit_model bm[], int symbol, int match_byte)
  1559. +{
  1560. + int mask = 0x100;
  1561. + symbol |= mask;
  1562. +
  1563. + do {
  1564. + int match_bit, bit;
  1565. + match_byte <<= 1;
  1566. + match_bit = match_byte & mask;
  1567. + symbol <<= 1;
  1568. + bit = symbol & 0x100;
  1569. + Re_encode_bit( renc, &bm[match_bit+(symbol>>9)+mask], bit );
  1570. + mask &= ~(match_byte ^ symbol); /* if( match_bit != bit ) mask = 0; */
  1571. + }
  1572. + while( symbol < 0x10000 );
  1573. +}
  1574. +
  1575. +static void Re_encode_len( struct Range_encoder * const renc,
  1576. + struct Len_model * const lm,
  1577. + int symbol, const int pos_state )
  1578. +{
  1579. + bool bit = ( ( symbol -= min_match_len ) >= len_low_symbols );
  1580. + Re_encode_bit( renc, &lm->choice1, bit );
  1581. + if( !bit )
  1582. + Re_encode_tree( renc, lm->bm_low[pos_state], symbol, len_low_bits );
  1583. + else {
  1584. + bit = ( symbol >= len_low_symbols + len_mid_symbols );
  1585. + Re_encode_bit( renc, &lm->choice2, bit );
  1586. + if( !bit )
  1587. + Re_encode_tree( renc, lm->bm_mid[pos_state],
  1588. + symbol - len_low_symbols, len_mid_bits );
  1589. + else
  1590. + Re_encode_tree( renc, lm->bm_high,
  1591. + symbol - len_low_symbols - len_mid_symbols, len_high_bits );
  1592. + }
  1593. +}
  1594. +
  1595. +
  1596. +struct Len_encoder {
  1597. + struct Len_model lm;
  1598. + int len_symbols;
  1599. + int prices[pos_states][max_len_symbols];
  1600. + int counters[pos_states];
  1601. +};
  1602. +
  1603. +static void Lee_update_prices(struct Len_encoder *const le, const int pos_state)
  1604. +{
  1605. + int *const pps = le->prices[pos_state];
  1606. + int tmp = price0(le->lm.choice1);
  1607. + int len = 0;
  1608. +
  1609. + for (; len < len_low_symbols && len < le->len_symbols; ++len)
  1610. + pps[len] = tmp +
  1611. + price_symbol(le->lm.bm_low[pos_state], len, len_low_bits);
  1612. + tmp = price1(le->lm.choice1);
  1613. + for (; len < len_low_symbols + len_mid_symbols && len < le->len_symbols; ++len)
  1614. + pps[len] = tmp + price0(le->lm.choice2) +
  1615. + price_symbol(le->lm.bm_mid[pos_state],
  1616. + len - len_low_symbols, len_mid_bits);
  1617. + for (; len < le->len_symbols; ++len)
  1618. + /* using 4 slots per value makes "Lee_price" faster */
  1619. + le->prices[3][len] = le->prices[2][len] =
  1620. + le->prices[1][len] = le->prices[0][len] =
  1621. + tmp + price1(le->lm.choice2) +
  1622. + price_symbol(le->lm.bm_high,
  1623. + len - len_low_symbols - len_mid_symbols,
  1624. + len_high_bits);
  1625. + le->counters[pos_state] = le->len_symbols;
  1626. +}
  1627. +
  1628. +static void Lee_init(struct Len_encoder *const le, const int len_limit)
  1629. +{
  1630. + int i;
  1631. + Lm_init(&le->lm);
  1632. + le->len_symbols = len_limit + 1 - min_match_len;
  1633. + for (i = 0; i < pos_states; ++i) Lee_update_prices(le, i);
  1634. +}
  1635. +
  1636. +static void Lee_encode(struct Len_encoder *const le,
  1637. + struct Range_encoder *const renc,
  1638. + int symbol, const int pos_state)
  1639. +{
  1640. + Re_encode_len(renc, &le->lm, symbol, pos_state);
  1641. + if (--le->counters[pos_state] <= 0)
  1642. + Lee_update_prices(le, pos_state);
  1643. +}
  1644. +
  1645. +static int Lee_price(const struct Len_encoder *const le,
  1646. + const int symbol, const int pos_state)
  1647. +{
  1648. + return le->prices[pos_state][symbol - min_match_len];
  1649. +}
  1650. +
  1651. +
  1652. +enum { infinite_price = 0x0FFFFFFF,
  1653. + num_rep_distances = 4 /* must be 4 */
  1654. +};
  1655. +
  1656. +struct Trial {
  1657. + State state;
  1658. + int price; /* dual use var; cumulative price, match length */
  1659. + int dis; /* rep index or match distance. (-1 for literal) */
  1660. + int prev_index; /* index of prev trial in trials[] */
  1661. + int reps[num_rep_distances];
  1662. +};
  1663. +
  1664. +static void Tr_update(struct Trial *const trial, const int pr,
  1665. + const int distance, const int p_i)
  1666. +{
  1667. + if (pr < trial->price) {
  1668. + trial->price = pr;
  1669. + trial->dis = distance;
  1670. + trial->prev_index = p_i;
  1671. + }
  1672. +}
  1673. +
  1674. +
  1675. +struct LZ_encoder {
  1676. + int longest_match_found;
  1677. + uint32_t crc;
  1678. +
  1679. + Bit_model bm_literal[1<<literal_context_bits][0x300];
  1680. + Bit_model bm_match[states][pos_states];
  1681. + Bit_model bm_rep[states];
  1682. + Bit_model bm_rep0[states];
  1683. + Bit_model bm_rep1[states];
  1684. + Bit_model bm_rep2[states];
  1685. + Bit_model bm_len[states][pos_states];
  1686. + Bit_model bm_dis_slot[len_states][1<<dis_slot_bits];
  1687. + Bit_model bm_dis[modeled_distances-end_dis_model];
  1688. + Bit_model bm_align[dis_align_size];
  1689. +
  1690. + struct Matchfinder *matchfinder;
  1691. + struct Range_encoder renc;
  1692. + struct Len_encoder match_len_encoder;
  1693. + struct Len_encoder rep_len_encoder;
  1694. +
  1695. + int match_distances[max_match_len+1];
  1696. + struct Trial trials[max_num_trials];
  1697. +
  1698. + int dis_slot_prices[len_states][2*max_dictionary_bits];
  1699. + int dis_prices[len_states][modeled_distances];
  1700. + int align_prices[dis_align_size];
  1701. + int align_price_count;
  1702. + int num_dis_slots;
  1703. +};
  1704. +
  1705. +static void LZe_fill_align_prices(struct LZ_encoder *const e)
  1706. +{
  1707. + int i;
  1708. + for (i = 0; i < dis_align_size; ++i)
  1709. + e->align_prices[i] =
  1710. + price_symbol_reversed(e->bm_align, i, dis_align_bits);
  1711. + e->align_price_count = dis_align_size;
  1712. +}
  1713. +
  1714. +static bool LZe_init(struct LZ_encoder *const e,
  1715. + struct Matchfinder *const mf, const File_header header)
  1716. +{
  1717. + int i;
  1718. + e->longest_match_found = 0;
  1719. + e->crc = 0xFFFFFFFFU;
  1720. + Bm_array_init(&e->bm_literal[0][0], lz_num_models);
  1721. + e->matchfinder = mf;
  1722. + if (!Re_init(&e->renc)) return false;
  1723. + Lee_init(&e->match_len_encoder, e->matchfinder->match_len_limit);
  1724. + Lee_init(&e->rep_len_encoder, e->matchfinder->match_len_limit);
  1725. + LZe_fill_align_prices(e);
  1726. + e->num_dis_slots = 2 * real_bits(e->matchfinder->dictionary_size - 1);
  1727. + for (i = 0; i < Fh_size; ++i)
  1728. + Re_put_byte(&e->renc, header[i]);
  1729. + return true;
  1730. +}
  1731. +
  1732. +static inline void LZe_free(struct LZ_encoder *const e)
  1733. +{
  1734. + Re_free(&e->renc);
  1735. +}
  1736. +
  1737. +static inline unsigned LZe_crc(const struct LZ_encoder *const e)
  1738. +{
  1739. + return e->crc ^ 0xFFFFFFFFU;
  1740. +}
  1741. +
  1742. + /* move-to-front dis in/into reps if( dis > 0 ) */
  1743. +static void mtf_reps(const int dis, int reps[num_rep_distances])
  1744. +{
  1745. + int i;
  1746. + if (dis >= num_rep_distances) {
  1747. + for (i = num_rep_distances - 1; i > 0; --i)
  1748. + reps[i] = reps[i - 1];
  1749. + reps[0] = dis - num_rep_distances;
  1750. + } else if (dis > 0) {
  1751. + const int distance = reps[dis];
  1752. + for (i = dis; i > 0; --i)
  1753. + reps[i] = reps[i - 1];
  1754. + reps[0] = distance;
  1755. + }
  1756. +}
  1757. +
  1758. +static int LZe_price_shortrep(const struct LZ_encoder *const e,
  1759. + const State state, const int pos_state)
  1760. +{
  1761. + return price0(e->bm_rep0[state]) + price0(e->bm_len[state][pos_state]);
  1762. +}
  1763. +
  1764. +static int LZe_price_rep(const struct LZ_encoder *const e, const int rep,
  1765. + const State state, const int pos_state)
  1766. +{
  1767. + int price;
  1768. + if (rep == 0)
  1769. + return price0(e->bm_rep0[state]) +
  1770. + price1(e->bm_len[state][pos_state]);
  1771. + price = price1(e->bm_rep0[state]);
  1772. + if (rep == 1)
  1773. + price += price0(e->bm_rep1[state]);
  1774. + else {
  1775. + price += price1(e->bm_rep1[state]);
  1776. + price += price_bit(e->bm_rep2[state], rep - 2);
  1777. + }
  1778. + return price;
  1779. +}
  1780. +
  1781. +static int LZe_price_dis(const struct LZ_encoder *const e,
  1782. + const int dis, const int len_state)
  1783. +{
  1784. + if (dis < modeled_distances)
  1785. + return e->dis_prices[len_state][dis];
  1786. + else
  1787. + return e->dis_slot_prices[len_state][get_slot(dis)] +
  1788. + e->align_prices[dis & (dis_align_size - 1)];
  1789. +}
  1790. +
  1791. +static int LZe_price_pair(const struct LZ_encoder *const e,
  1792. + const int dis, const int len,
  1793. + const int pos_state)
  1794. +{
  1795. + if (len <= min_match_len && dis >= modeled_distances)
  1796. + return infinite_price;
  1797. + return Lee_price(&e->match_len_encoder, len, pos_state) +
  1798. + LZe_price_dis(e, dis, get_len_state(len));
  1799. +}
  1800. +
  1801. +static int LZe_price_literal(const struct LZ_encoder *const e,
  1802. + uint8_t prev_byte, uint8_t symbol)
  1803. +{
  1804. + return price_symbol(e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
  1805. +}
  1806. +
  1807. +static int LZe_price_matched(const struct LZ_encoder *const e,
  1808. + uint8_t prev_byte, uint8_t symbol,
  1809. + uint8_t match_byte)
  1810. +{
  1811. + return price_matched(e->bm_literal[get_lit_state(prev_byte)], symbol,
  1812. + match_byte);
  1813. +}
  1814. +
  1815. +static void LZe_encode_literal(struct LZ_encoder *const e,
  1816. + uint8_t prev_byte, uint8_t symbol)
  1817. +{
  1818. + Re_encode_tree(&e->renc,
  1819. + e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
  1820. +}
  1821. +
  1822. +static void LZe_encode_matched(struct LZ_encoder *const e,
  1823. + uint8_t prev_byte, uint8_t symbol,
  1824. + uint8_t match_byte)
  1825. +{
  1826. + Re_encode_matched(&e->renc, e->bm_literal[get_lit_state(prev_byte)],
  1827. + symbol, match_byte);
  1828. +}
  1829. +
  1830. +static void LZe_encode_pair(struct LZ_encoder *const e,
  1831. + const unsigned dis, const int len,
  1832. + const int pos_state)
  1833. +{
  1834. + const int dis_slot = get_slot(dis);
  1835. + Lee_encode(&e->match_len_encoder, &e->renc, len, pos_state);
  1836. + Re_encode_tree(&e->renc, e->bm_dis_slot[get_len_state(len)], dis_slot,
  1837. + dis_slot_bits);
  1838. +
  1839. + if (dis_slot >= start_dis_model) {
  1840. + const int direct_bits = (dis_slot >> 1) - 1;
  1841. + const unsigned base = (2 | (dis_slot & 1)) << direct_bits;
  1842. + const unsigned direct_dis = dis - base;
  1843. +
  1844. + if (dis_slot < end_dis_model)
  1845. + Re_encode_tree_reversed(&e->renc,
  1846. + e->bm_dis + base - dis_slot - 1,
  1847. + direct_dis, direct_bits);
  1848. + else {
  1849. + Re_encode(&e->renc, direct_dis >> dis_align_bits,
  1850. + direct_bits - dis_align_bits);
  1851. + Re_encode_tree_reversed(&e->renc, e->bm_align,
  1852. + direct_dis, dis_align_bits);
  1853. + if (--e->align_price_count <= 0)
  1854. + LZe_fill_align_prices(e);
  1855. + }
  1856. + }
  1857. +}
  1858. +
  1859. +static int LZe_read_match_distances(struct LZ_encoder *const e)
  1860. +{
  1861. + int len = Mf_longest_match_len(e->matchfinder, e->match_distances);
  1862. + if (len == e->matchfinder->match_len_limit && len < max_match_len)
  1863. + len += Mf_true_match_len(e->matchfinder, len,
  1864. + e->match_distances[len] + 1,
  1865. + max_match_len - len);
  1866. + return len;
  1867. +}
  1868. +
  1869. +static void LZe_move_pos(struct LZ_encoder *const e, int n)
  1870. +{
  1871. + while (true) {
  1872. + Mf_move_pos(e->matchfinder);
  1873. + if( --n <= 0 ) break;
  1874. + Mf_longest_match_len(e->matchfinder, 0);
  1875. + }
  1876. +}
  1877. +
  1878. +static void LZe_backward(struct LZ_encoder *const e, int cur)
  1879. +{
  1880. + int *const dis = &e->trials[cur].dis;
  1881. + while (cur > 0) {
  1882. + const int prev_index = e->trials[cur].prev_index;
  1883. + struct Trial *const prev_trial = &e->trials[prev_index];
  1884. + prev_trial->price = cur - prev_index; /* len */
  1885. + cur = *dis;
  1886. + *dis = prev_trial->dis;
  1887. + prev_trial->dis = cur;
  1888. + cur = prev_index;
  1889. + }
  1890. +}
  1891. +
  1892. + /* End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len) */
  1893. +static void LZe_full_flush(struct LZ_encoder *const e, const State state)
  1894. +{
  1895. + int i;
  1896. + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
  1897. + File_trailer trailer;
  1898. + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], 1);
  1899. + Re_encode_bit(&e->renc, &e->bm_rep[state], 0);
  1900. + LZe_encode_pair(e, 0xFFFFFFFFU, min_match_len, pos_state);
  1901. + Re_flush(&e->renc);
  1902. + Ft_set_data_crc(trailer, LZe_crc(e));
  1903. + Ft_set_data_size(trailer, Mf_data_position(e->matchfinder));
  1904. + Ft_set_member_size(trailer, Re_member_position(&e->renc) + Ft_size);
  1905. + for (i = 0; i < Ft_size; ++i)
  1906. + Re_put_byte(&e->renc, trailer[i]);
  1907. + Re_flush_data(&e->renc);
  1908. +}
  1909. +
  1910. +
  1911. +static void LZe_update_distance_prices(struct LZ_encoder *const e)
  1912. +{
  1913. + int dis, len_state;
  1914. + for (dis = start_dis_model; dis < modeled_distances; ++dis) {
  1915. + const int dis_slot = dis_slots[dis];
  1916. + const int direct_bits = (dis_slot >> 1) - 1;
  1917. + const int base = (2 | (dis_slot & 1)) << direct_bits;
  1918. + const int price =
  1919. + price_symbol_reversed(e->bm_dis + base - dis_slot - 1,
  1920. + dis - base, direct_bits);
  1921. + for (len_state = 0; len_state < len_states; ++len_state)
  1922. + e->dis_prices[len_state][dis] = price;
  1923. + }
  1924. +
  1925. + for (len_state = 0; len_state < len_states; ++len_state) {
  1926. + int *const dsp = e->dis_slot_prices[len_state];
  1927. + int *const dp = e->dis_prices[len_state];
  1928. + const Bit_model *const bmds = e->bm_dis_slot[len_state];
  1929. + int slot = 0;
  1930. + for (; slot < end_dis_model; ++slot)
  1931. + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits);
  1932. + for (; slot < e->num_dis_slots; ++slot)
  1933. + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits) +
  1934. + ((((slot >> 1) - 1) - dis_align_bits) << price_shift_bits);
  1935. +
  1936. + for (dis = 0; dis < start_dis_model; ++dis)
  1937. + dp[dis] = dsp[dis];
  1938. + for (; dis < modeled_distances; ++dis)
  1939. + dp[dis] += dsp[dis_slots[dis]];
  1940. + }
  1941. +}
  1942. +
  1943. +
  1944. +/* Returns the number of bytes advanced (ahead).
  1945. + trials[0]..trials[ahead-1] contain the steps to encode.
  1946. + ( trials[0].dis == -1 && trials[0].price == 1 ) means literal.
  1947. + A match/rep longer or equal than match_len_limit finishes the sequence.
  1948. +*/
  1949. +static int LZe_sequence_optimizer(struct LZ_encoder *const e,
  1950. + const int reps[num_rep_distances],
  1951. + const State state)
  1952. +{
  1953. + int main_len, i, rep, cur = 0, num_trials;
  1954. + int replens[num_rep_distances];
  1955. + int rep_index = 0;
  1956. +
  1957. + if (e->longest_match_found > 0) { /* from previous call */
  1958. + main_len = e->longest_match_found;
  1959. + e->longest_match_found = 0;
  1960. + } else
  1961. + main_len = LZe_read_match_distances(e);
  1962. +
  1963. + for (i = 0; i < num_rep_distances; ++i) {
  1964. + replens[i] = Mf_true_match_len(e->matchfinder, 0, reps[i] + 1,
  1965. + max_match_len);
  1966. + if (replens[i] > replens[rep_index]) rep_index = i;
  1967. + }
  1968. + if (replens[rep_index] >= e->matchfinder->match_len_limit) {
  1969. + e->trials[0].dis = rep_index;
  1970. + e->trials[0].price = replens[rep_index];
  1971. + LZe_move_pos(e, replens[rep_index]);
  1972. + return replens[rep_index];
  1973. + }
  1974. +
  1975. + if (main_len >= e->matchfinder->match_len_limit) {
  1976. + e->trials[0].dis =
  1977. + e->match_distances[e->matchfinder->match_len_limit] +
  1978. + num_rep_distances;
  1979. + e->trials[0].price = main_len;
  1980. + LZe_move_pos(e, main_len);
  1981. + return main_len;
  1982. + }
  1983. +
  1984. + {
  1985. + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
  1986. + const int match_price = price1(e->bm_match[state][pos_state]);
  1987. + const int rep_match_price = match_price + price1(e->bm_rep[state]);
  1988. + const uint8_t prev_byte = Mf_peek(e->matchfinder, 1);
  1989. + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
  1990. + const uint8_t match_byte = Mf_peek(e->matchfinder, reps[0] + 1);
  1991. +
  1992. + e->trials[0].state = state;
  1993. + for (i = 0; i < num_rep_distances; ++i)
  1994. + e->trials[0].reps[i] = reps[i];
  1995. + e->trials[1].dis = -1; /* literal */
  1996. + e->trials[1].prev_index = 0;
  1997. + e->trials[1].price = price0(e->bm_match[state][pos_state]);
  1998. + if (St_is_char(state))
  1999. + e->trials[1].price +=
  2000. + LZe_price_literal(e, prev_byte, cur_byte);
  2001. + else
  2002. + e->trials[1].price +=
  2003. + LZe_price_matched(e, prev_byte, cur_byte, match_byte);
  2004. +
  2005. + if (match_byte == cur_byte)
  2006. + Tr_update(&e->trials[1], rep_match_price +
  2007. + LZe_price_shortrep(e, state, pos_state), 0, 0);
  2008. +
  2009. + if (main_len < min_match_len) {
  2010. + e->trials[0].dis = e->trials[1].dis;
  2011. + e->trials[0].price = 1;
  2012. + Mf_move_pos(e->matchfinder);
  2013. + return 1;
  2014. + }
  2015. +
  2016. + if (main_len <= replens[rep_index]) {
  2017. + int len;
  2018. +
  2019. + main_len = replens[rep_index];
  2020. + for (len = min_match_len; len <= main_len; ++len)
  2021. + e->trials[len].price = infinite_price;
  2022. + } else {
  2023. + int len;
  2024. + const int normal_match_price =
  2025. + match_price + price0(e->bm_rep[state]);
  2026. + for (len = min_match_len; len <= main_len; ++len) {
  2027. + e->trials[len].dis =
  2028. + e->match_distances[len] + num_rep_distances;
  2029. + e->trials[len].prev_index = 0;
  2030. + e->trials[len].price = normal_match_price +
  2031. + LZe_price_pair(e, e->match_distances[len],
  2032. + len, pos_state);
  2033. + }
  2034. + }
  2035. +
  2036. + for (rep = 0; rep < num_rep_distances; ++rep) {
  2037. + const int price = rep_match_price +
  2038. + LZe_price_rep(e, rep, state, pos_state);
  2039. + int len;
  2040. + for (len = min_match_len; len <= replens[rep]; ++len)
  2041. + Tr_update(&e->trials[len], price +
  2042. + Lee_price(&e->rep_len_encoder, len, pos_state),
  2043. + rep, 0);
  2044. + }
  2045. + }
  2046. +
  2047. + num_trials = main_len;
  2048. +
  2049. + while (true) { /* price optimization loop */
  2050. + struct Trial *cur_trial, *next_trial;
  2051. + int newlen, pos_state, prev_index, len_limit;
  2052. + int next_price, match_price, rep_match_price;
  2053. + uint8_t prev_byte, cur_byte, match_byte;
  2054. +
  2055. + Mf_move_pos(e->matchfinder);
  2056. + if (++cur >= num_trials) { /* no more initialized trials */
  2057. + LZe_backward(e, cur);
  2058. + return cur;
  2059. + }
  2060. + newlen = LZe_read_match_distances(e);
  2061. + if (newlen >= e->matchfinder->match_len_limit) {
  2062. + e->longest_match_found = newlen;
  2063. + LZe_backward(e, cur);
  2064. + return cur;
  2065. + }
  2066. +
  2067. + /* give final values to current trial */
  2068. + cur_trial = &e->trials[cur];
  2069. + prev_index = cur_trial->prev_index;
  2070. + cur_trial->state = e->trials[prev_index].state;
  2071. +
  2072. + for (i = 0; i < num_rep_distances; ++i)
  2073. + cur_trial->reps[i] = e->trials[prev_index].reps[i];
  2074. +
  2075. + if (prev_index == cur - 1) {
  2076. + if (cur_trial->dis == 0)
  2077. + cur_trial->state = St_set_short_rep(cur_trial->state);
  2078. + else
  2079. + cur_trial->state = St_set_char(cur_trial->state);
  2080. + } else {
  2081. + if (cur_trial->dis < num_rep_distances)
  2082. + cur_trial->state = St_set_rep(cur_trial->state);
  2083. + else
  2084. + cur_trial->state = St_set_match(cur_trial->state);
  2085. + mtf_reps(cur_trial->dis, cur_trial->reps);
  2086. + }
  2087. +
  2088. + pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
  2089. + prev_byte = Mf_peek(e->matchfinder, 1);
  2090. + cur_byte = Mf_peek(e->matchfinder, 0);
  2091. + match_byte = Mf_peek(e->matchfinder, cur_trial->reps[0] + 1);
  2092. +
  2093. + next_price = cur_trial->price +
  2094. + price0(e->bm_match[cur_trial->state][pos_state]);
  2095. + if (St_is_char(cur_trial->state))
  2096. + next_price += LZe_price_literal(e, prev_byte, cur_byte);
  2097. + else
  2098. + next_price += LZe_price_matched(e, prev_byte, cur_byte,
  2099. + match_byte);
  2100. + /* try last updates to next trial */
  2101. + next_trial = &e->trials[cur + 1];
  2102. +
  2103. + Tr_update(next_trial, next_price, -1, cur); /* literal */
  2104. +
  2105. + match_price = cur_trial->price +
  2106. + price1(e->bm_match[cur_trial->state][pos_state]);
  2107. + rep_match_price = match_price + price1(e->bm_rep[cur_trial->state]);
  2108. +
  2109. + if (match_byte == cur_byte && next_trial->dis != 0)
  2110. + Tr_update(next_trial, rep_match_price +
  2111. + LZe_price_shortrep(e, cur_trial->state,
  2112. + pos_state), 0, cur);
  2113. +
  2114. + len_limit = MIN(MIN(max_num_trials - 1 - cur,
  2115. + Mf_available_bytes(e->matchfinder)),
  2116. + e->matchfinder->match_len_limit);
  2117. + if (len_limit < min_match_len) continue;
  2118. +
  2119. + for (rep = 0; rep < num_rep_distances; ++rep) {
  2120. + const int dis = cur_trial->reps[rep] + 1;
  2121. + int len = 0;
  2122. + const uint8_t *const data =
  2123. + Mf_ptr_to_current_pos(e->matchfinder);
  2124. + while (len < len_limit && data[len] == data[len - dis])
  2125. + ++len;
  2126. + if (len >= min_match_len) {
  2127. + const int price = rep_match_price +
  2128. + LZe_price_rep(e, rep, cur_trial->state, pos_state);
  2129. + while (num_trials < cur + len)
  2130. + e->trials[++num_trials].price = infinite_price;
  2131. + for (; len >= min_match_len; --len)
  2132. + Tr_update(&e->trials[cur + len], price +
  2133. + Lee_price(&e->rep_len_encoder, len,
  2134. + pos_state), rep, cur);
  2135. + }
  2136. + }
  2137. +
  2138. + if (newlen <= len_limit &&
  2139. + (newlen > min_match_len ||
  2140. + (newlen == min_match_len &&
  2141. + e->match_distances[min_match_len] < modeled_distances))) {
  2142. + const int normal_match_price = match_price +
  2143. + price0(e->bm_rep[cur_trial->state]);
  2144. + int len;
  2145. + int dis = e->match_distances[min_match_len];
  2146. + int len_state = get_len_state(min_match_len);
  2147. + int dis_price = infinite_price;
  2148. +
  2149. + while (num_trials < cur + newlen)
  2150. + e->trials[++num_trials].price = infinite_price;
  2151. +
  2152. + if (dis < modeled_distances)
  2153. + Tr_update(&e->trials[cur + min_match_len],
  2154. + normal_match_price +
  2155. + e->dis_prices[len_state][dis] +
  2156. + Lee_price(&e->match_len_encoder,
  2157. + min_match_len, pos_state),
  2158. + dis + num_rep_distances, cur);
  2159. +
  2160. + for (len = min_match_len + 1; len <= newlen; ++len) {
  2161. + if (dis != e->match_distances[len] ||
  2162. + len_state < len_states - 1) {
  2163. + dis = e->match_distances[len];
  2164. + len_state = get_len_state(len);
  2165. + dis_price = LZe_price_dis(e, dis, len_state);
  2166. + }
  2167. + Tr_update(&e->trials[cur + len],
  2168. + normal_match_price + dis_price +
  2169. + Lee_price(&e->match_len_encoder, len, pos_state),
  2170. + dis + num_rep_distances, cur);
  2171. + }
  2172. + }
  2173. + }
  2174. +}
  2175. +
  2176. +
  2177. +static bool LZe_encode_member(struct LZ_encoder *const e)
  2178. +{
  2179. + const int dis_price_count =
  2180. + (e->matchfinder->match_len_limit > 12) ? 512 : 2048;
  2181. + int dis_price_counter = 0;
  2182. + int ahead, i;
  2183. + int reps[num_rep_distances];
  2184. + State state = 0;
  2185. + for (i = 0; i < num_rep_distances; ++i) reps[i] = 0;
  2186. +
  2187. + if (!Mf_finished(e->matchfinder)) { /* encode first byte */
  2188. + const uint8_t prev_byte = 0;
  2189. + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
  2190. + Re_encode_bit(&e->renc, &e->bm_match[state][0], 0);
  2191. + LZe_encode_literal(e, prev_byte, cur_byte);
  2192. + CRC32_update_byte(&e->crc, cur_byte);
  2193. + Mf_longest_match_len(e->matchfinder, 0);
  2194. + Mf_move_pos(e->matchfinder);
  2195. + }
  2196. +
  2197. + while (!Mf_finished(e->matchfinder)) {
  2198. + if (dis_price_counter <= 0) {
  2199. + LZe_update_distance_prices(e);
  2200. + dis_price_counter = dis_price_count;
  2201. + }
  2202. +
  2203. + ahead = LZe_sequence_optimizer(e, reps, state);
  2204. + dis_price_counter -= ahead;
  2205. +
  2206. + for (i = 0; ahead > 0;) {
  2207. + const int pos_state =
  2208. + (Mf_data_position(e->matchfinder) - ahead) & pos_state_mask;
  2209. + const int dis = e->trials[i].dis;
  2210. + const int len = e->trials[i].price;
  2211. +
  2212. + bool bit = (dis < 0 && len == 1);
  2213. + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], !bit);
  2214. + if (bit) { /* literal byte */
  2215. + const uint8_t prev_byte = Mf_peek(e->matchfinder, ahead + 1);
  2216. + const uint8_t cur_byte = Mf_peek(e->matchfinder, ahead);
  2217. + CRC32_update_byte(&e->crc, cur_byte);
  2218. + if (St_is_char(state))
  2219. + LZe_encode_literal(e, prev_byte, cur_byte);
  2220. + else {
  2221. + const uint8_t match_byte =
  2222. + Mf_peek(e->matchfinder, ahead + reps[0] + 1);
  2223. + LZe_encode_matched(e, prev_byte, cur_byte, match_byte);
  2224. + }
  2225. + state = St_set_char(state);
  2226. + } else { /* match or repeated match */
  2227. +
  2228. + e->crc = crc32_block_endian0(e->crc,
  2229. + Mf_ptr_to_current_pos(e->matchfinder) - ahead,
  2230. + len, global_crc32_table);
  2231. + mtf_reps(dis, reps);
  2232. + bit = (dis < num_rep_distances);
  2233. + Re_encode_bit(&e->renc, &e->bm_rep[state], bit);
  2234. + if (bit) { /* repeated match */
  2235. + bit = (dis == 0);
  2236. + Re_encode_bit(&e->renc, &e->bm_rep0[state], !bit);
  2237. + if (bit)
  2238. + Re_encode_bit(&e->renc, &e->bm_len[state][pos_state], len > 1);
  2239. + else {
  2240. + Re_encode_bit(&e->renc, &e->bm_rep1[state], dis > 1);
  2241. + if (dis > 1)
  2242. + Re_encode_bit(&e->renc, &e->bm_rep2[state], dis > 2);
  2243. + }
  2244. + if (len == 1)
  2245. + state = St_set_short_rep(state);
  2246. + else {
  2247. + Lee_encode(&e->rep_len_encoder,
  2248. + &e->renc, len, pos_state);
  2249. + state = St_set_rep(state);
  2250. + }
  2251. + } else { /* match */
  2252. + LZe_encode_pair(e, dis - num_rep_distances, len, pos_state);
  2253. + state = St_set_match(state);
  2254. + }
  2255. + }
  2256. + ahead -= len;
  2257. + i += len;
  2258. + }
  2259. + }
  2260. + LZe_full_flush(e, state);
  2261. + return !e->renc.write_error;
  2262. +}
  2263. +
  2264. +
  2265. +struct Lzma_options {
  2266. + int dictionary_size; /* 4KiB..512MiB */
  2267. + int match_len_limit; /* 5..273 */
  2268. +} encoder_options;
  2269. +
  2270. +
  2271. +static int getnum(const char *const ptr, const int llimit, const int ulimit)
  2272. +{
  2273. + long result;
  2274. + char *tail;
  2275. + errno = 0;
  2276. + result = strtol(ptr, &tail, 0);
  2277. + if (tail == ptr || errno)
  2278. + goto error;
  2279. + if (tail[0]) {
  2280. + int factor = (tail[1] == 'i') ? 1024 : 1000;
  2281. + int exponent = 0, i;
  2282. +
  2283. + switch (tail[0]) {
  2284. + case 'M':
  2285. + exponent = 2;
  2286. + break;
  2287. + case 'K':
  2288. + if (factor == 1024) {
  2289. + exponent = 1;
  2290. + break;
  2291. + }
  2292. + goto error;
  2293. + case 'k':
  2294. + if (factor == 1000) {
  2295. + exponent = 1;
  2296. + break;
  2297. + }
  2298. + default:
  2299. + goto error;
  2300. + }
  2301. + for (i = 0; i < exponent; ++i) {
  2302. + if (LONG_MAX / factor >= labs(result))
  2303. + result *= factor;
  2304. + else
  2305. + goto error;
  2306. + }
  2307. + }
  2308. + if (result >= llimit && result <= ulimit)
  2309. + return result;
  2310. + error:
  2311. + bb_error_msg_and_die("invalid number");
  2312. +}
  2313. +
  2314. +
  2315. +static int get_dict_size(const char *const arg)
  2316. +{
  2317. + char *tail;
  2318. + long bits = strtol(arg, &tail, 0);
  2319. + if (bits >= min_dictionary_bits &&
  2320. + bits <= max_dictionary_bits && *tail == 0)
  2321. + return (1 << bits);
  2322. + return getnum(arg, min_dictionary_size, max_dictionary_size);
  2323. +}
  2324. +
  2325. +
  2326. +static IF_DESKTOP(long long) int FAST_FUNC pack_lzip(transformer_state_t *xstate UNUSED_PARAM)
  2327. +{
  2328. + int retval = 0;
  2329. + File_header header;
  2330. + struct Matchfinder matchfinder;
  2331. + struct LZ_encoder * encoder;
  2332. +
  2333. + Fh_set_magic(header);
  2334. + if (!Fh_set_dictionary_size(header, encoder_options.dictionary_size) ||
  2335. + encoder_options.match_len_limit < min_match_len_limit ||
  2336. + encoder_options.match_len_limit > max_match_len)
  2337. + bb_error_msg_and_die("internal error");
  2338. +
  2339. + if (!Mf_init(&matchfinder, Fh_get_dictionary_size(header),
  2340. + encoder_options.match_len_limit)) {
  2341. + bb_error_msg(bb_msg_memory_exhausted);
  2342. + return -1;
  2343. + }
  2344. + Fh_set_dictionary_size(header, matchfinder.dictionary_size);
  2345. +
  2346. + encoder = malloc(sizeof(struct LZ_encoder));
  2347. + if (!encoder || !LZe_init(encoder, &matchfinder, header)) {
  2348. + bb_error_msg(bb_msg_memory_exhausted);
  2349. + retval = -1;
  2350. + } else {
  2351. + if (!LZe_encode_member(encoder)) {
  2352. + bb_perror_msg(bb_msg_write_error);
  2353. + retval = -1;
  2354. + }
  2355. + LZe_free(encoder);
  2356. + }
  2357. + free(encoder);
  2358. + Mf_free(&matchfinder);
  2359. + return retval;
  2360. +}
  2361. +
  2362. +
  2363. +int lzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2364. +int lzip_main(int argc UNUSED_PARAM, char **argv)
  2365. +{
  2366. + /* Mapping from gzip/bzip2 style 1..9 compression modes
  2367. + to the corresponding LZMA compression modes. */
  2368. + const struct Lzma_options option_mapping[] = {
  2369. + {1 << 20, 5}, /* -0 */
  2370. + {1 << 20, 5}, /* -1 */
  2371. + {3 << 19, 6}, /* -2 */
  2372. + {1 << 21, 8}, /* -3 */
  2373. + {3 << 20, 12}, /* -4 */
  2374. + {1 << 22, 20}, /* -5 */
  2375. + {1 << 23, 36}, /* -6 */
  2376. + {1 << 24, 68}, /* -7 */
  2377. + {3 << 23, 132}, /* -8 */
  2378. + {1 << 25, 273} /* -9 */
  2379. + };
  2380. + int i;
  2381. + char *m_arg;
  2382. + char *s_arg;
  2383. + /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
  2384. + uint32_t flags = getopt32(argv, "cfkvqdt0123456789Fm:s:", &m_arg, &s_arg);
  2385. +
  2386. + if (flags & 0x60) { // -d and/or -t
  2387. +#if ENABLE_LUNZIP /* lunzip_main may not be visible... */
  2388. + return lunzip_main(argc, argv);
  2389. +#else
  2390. + bb_error_msg("decompression is disabled");
  2391. + return 1;
  2392. +#endif
  2393. + }
  2394. + flags >>= 7; /* drop "cfkvqdt" bits */
  2395. +
  2396. + encoder_options = option_mapping[6]; /* default = "-6" */
  2397. +
  2398. + for (i = 9; i >= 7; --i)
  2399. + if (flags & (1 << i))
  2400. + encoder_options = option_mapping[i];
  2401. + for (i = 0; i <= 6; ++i)
  2402. + if (flags & (1 << i))
  2403. + encoder_options = option_mapping[i];
  2404. + if (flags & (1 << 11)) /* -m */
  2405. + encoder_options.match_len_limit =
  2406. + getnum(m_arg, min_match_len_limit, max_match_len);
  2407. + if (flags & (1 << 12)) /* -s */
  2408. + encoder_options.dictionary_size = get_dict_size(s_arg);
  2409. + /* end process options */
  2410. +
  2411. + argv += optind;
  2412. +
  2413. + if (!global_crc32_table)
  2414. + global_crc32_table = crc32_filltable(NULL, 0);
  2415. + if (!dis_slots) {
  2416. + Dis_slots_init();
  2417. + Prob_prices_init();
  2418. + }
  2419. +
  2420. + return bbunpack(argv, pack_lzip, append_ext, "lz");
  2421. +}
  2422. diff -urdN busybox-1.27.0.old/archival/tar.c busybox-1.27.0.new/archival/tar.c
  2423. --- busybox-1.27.0.old/archival/tar.c 2017-07-01 22:51:12.000000000 +0200
  2424. +++ busybox-1.27.0.new/archival/tar.c 2017-07-07 01:00:19.000000000 +0200
  2425. @@ -62,7 +62,7 @@
  2426. //config:config FEATURE_TAR_AUTODETECT
  2427. //config: bool "Autodetect compressed tarballs"
  2428. //config: default y
  2429. -//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
  2430. +//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZ || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
  2431. //config: help
  2432. //config: With this option tar can automatically detect compressed
  2433. //config: tarballs. Currently it works only on files (not pipes etc).
  2434. @@ -771,6 +771,7 @@
  2435. //usage: IF_FEATURE_SEAMLESS_GZ("z")
  2436. //usage: IF_FEATURE_SEAMLESS_XZ("J")
  2437. //usage: IF_FEATURE_SEAMLESS_BZ2("j")
  2438. +//usage: IF_FEATURE_SEAMLESS_LZ("y")
  2439. //usage: IF_FEATURE_SEAMLESS_LZMA("a")
  2440. //usage: IF_FEATURE_TAR_CREATE("h")
  2441. //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  2442. @@ -802,6 +803,9 @@
  2443. //usage: IF_FEATURE_SEAMLESS_BZ2(
  2444. //usage: "\n j (De)compress using bzip2"
  2445. //usage: )
  2446. +//usage: IF_FEATURE_SEAMLESS_LZ(
  2447. +//usage: "\n y (De)compress using lzip"
  2448. +//usage: )
  2449. //usage: IF_FEATURE_SEAMLESS_LZMA(
  2450. //usage: "\n a (De)compress using lzma"
  2451. //usage: )
  2452. @@ -846,6 +850,7 @@
  2453. IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
  2454. IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
  2455. IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,) // 16th bit
  2456. + IF_FEATURE_SEAMLESS_LZ( OPTBIT_LZIP ,)
  2457. IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,)
  2458. IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
  2459. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  2460. @@ -873,6 +878,7 @@
  2461. OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
  2462. OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
  2463. OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
  2464. + OPT_LZIP = IF_FEATURE_SEAMLESS_LZ( (1 << OPTBIT_LZIP )) + 0, // y
  2465. OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
  2466. OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
  2467. OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
  2468. @@ -882,7 +888,7 @@
  2469. OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
  2470. OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
  2471. - OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
  2472. + OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_LZIP | OPT_COMPRESS),
  2473. };
  2474. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  2475. static const char tar_longopts[] ALIGN1 =
  2476. @@ -915,6 +921,9 @@
  2477. # if ENABLE_FEATURE_SEAMLESS_GZ
  2478. "gzip\0" No_argument "z"
  2479. # endif
  2480. +# if ENABLE_FEATURE_SEAMLESS_LZ
  2481. + "lzip\0" No_argument "y"
  2482. +# endif
  2483. # if ENABLE_FEATURE_SEAMLESS_XZ
  2484. "xz\0" No_argument "J"
  2485. # endif
  2486. @@ -1026,6 +1035,7 @@
  2487. IF_FEATURE_TAR_FROM( "T:*X:*")
  2488. IF_FEATURE_SEAMLESS_GZ( "z" )
  2489. IF_FEATURE_SEAMLESS_XZ( "J" )
  2490. + IF_FEATURE_SEAMLESS_LZ( "y" )
  2491. IF_FEATURE_SEAMLESS_Z( "Z" )
  2492. IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  2493. IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components
  2494. @@ -1063,6 +1073,7 @@
  2495. showopt(OPT_EXCLUDE_FROM );
  2496. showopt(OPT_GZIP );
  2497. showopt(OPT_XZ );
  2498. + showopt(OPT_LZIP );
  2499. showopt(OPT_COMPRESS );
  2500. showopt(OPT_NOPRESERVE_TIME );
  2501. showopt(OPT_STRIP_COMPONENTS);
  2502. @@ -1198,6 +1209,8 @@
  2503. zipMode = "gzip";
  2504. if (opt & OPT_BZIP2)
  2505. zipMode = "bzip2";
  2506. + if (opt & OPT_LZIP)
  2507. + zipMode = "lzip";
  2508. if (opt & OPT_LZMA)
  2509. zipMode = "lzma";
  2510. if (opt & OPT_XZ)
  2511. @@ -1228,6 +1241,10 @@
  2512. USE_FOR_MMU(IF_FEATURE_SEAMLESS_BZ2(xformer = unpack_bz2_stream;))
  2513. USE_FOR_NOMMU(xformer_prog = "bunzip2";)
  2514. }
  2515. + if (opt & OPT_LZIP) {
  2516. + USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZ(xformer = unpack_lz_stream;))
  2517. + USE_FOR_NOMMU(xformer_prog = "lunzip";)
  2518. + }
  2519. if (opt & OPT_LZMA) {
  2520. USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZMA(xformer = unpack_lzma_stream;))
  2521. USE_FOR_NOMMU(xformer_prog = "unlzma";)
  2522. diff -urdN busybox-1.27.0.old/configs/TEST_nommu_defconfig busybox-1.27.0.new/configs/TEST_nommu_defconfig
  2523. --- busybox-1.27.0.old/configs/TEST_nommu_defconfig 2017-01-30 21:57:30.000000000 +0100
  2524. +++ busybox-1.27.0.new/configs/TEST_nommu_defconfig 2017-07-07 00:24:37.000000000 +0200
  2525. @@ -108,6 +108,7 @@
  2526. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2527. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2528. CONFIG_FEATURE_SEAMLESS_GZ=y
  2529. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2530. CONFIG_FEATURE_SEAMLESS_Z=y
  2531. CONFIG_AR=y
  2532. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2533. @@ -121,6 +122,8 @@
  2534. CONFIG_GUNZIP=y
  2535. CONFIG_GZIP=y
  2536. CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
  2537. +CONFIG_LUNZIP=y
  2538. +CONFIG_LZIP=y
  2539. CONFIG_LZOP=y
  2540. CONFIG_LZOP_COMPR_HIGH=y
  2541. CONFIG_RPM2CPIO=y
  2542. diff -urdN busybox-1.27.0.old/configs/TEST_noprintf_defconfig busybox-1.27.0.new/configs/TEST_noprintf_defconfig
  2543. --- busybox-1.27.0.old/configs/TEST_noprintf_defconfig 2017-01-15 00:18:53.000000000 +0100
  2544. +++ busybox-1.27.0.new/configs/TEST_noprintf_defconfig 2017-07-07 00:24:37.000000000 +0200
  2545. @@ -119,6 +119,7 @@
  2546. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2547. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2548. CONFIG_FEATURE_SEAMLESS_GZ=y
  2549. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2550. CONFIG_FEATURE_SEAMLESS_Z=y
  2551. # CONFIG_AR is not set
  2552. # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
  2553. @@ -133,6 +134,8 @@
  2554. # CONFIG_GUNZIP is not set
  2555. # CONFIG_GZIP is not set
  2556. # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
  2557. +# CONFIG_LUNZIP is not set
  2558. +# CONFIG_LZIP is not set
  2559. # CONFIG_LZOP is not set
  2560. # CONFIG_LZOP_COMPR_HIGH is not set
  2561. # CONFIG_RPM2CPIO is not set
  2562. diff -urdN busybox-1.27.0.old/configs/TEST_rh9_defconfig busybox-1.27.0.new/configs/TEST_rh9_defconfig
  2563. --- busybox-1.27.0.old/configs/TEST_rh9_defconfig 2017-01-30 21:57:30.000000000 +0100
  2564. +++ busybox-1.27.0.new/configs/TEST_rh9_defconfig 2017-07-07 00:24:37.000000000 +0200
  2565. @@ -117,6 +117,7 @@
  2566. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2567. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2568. CONFIG_FEATURE_SEAMLESS_GZ=y
  2569. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2570. CONFIG_FEATURE_SEAMLESS_Z=y
  2571. CONFIG_AR=y
  2572. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2573. @@ -131,6 +132,8 @@
  2574. CONFIG_GUNZIP=y
  2575. CONFIG_GZIP=y
  2576. CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
  2577. +CONFIG_LUNZIP=y
  2578. +CONFIG_LZIP=y
  2579. CONFIG_LZOP=y
  2580. # CONFIG_LZOP_COMPR_HIGH is not set
  2581. CONFIG_RPM2CPIO=y
  2582. diff -urdN busybox-1.27.0.old/configs/android2_defconfig busybox-1.27.0.new/configs/android2_defconfig
  2583. --- busybox-1.27.0.old/configs/android2_defconfig 2017-01-30 21:57:30.000000000 +0100
  2584. +++ busybox-1.27.0.new/configs/android2_defconfig 2017-07-07 00:24:37.000000000 +0200
  2585. @@ -122,6 +122,7 @@
  2586. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2587. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2588. CONFIG_FEATURE_SEAMLESS_GZ=y
  2589. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2590. CONFIG_FEATURE_SEAMLESS_Z=y
  2591. CONFIG_AR=y
  2592. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2593. @@ -136,6 +137,8 @@
  2594. CONFIG_GUNZIP=y
  2595. CONFIG_GZIP=y
  2596. # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
  2597. +CONFIG_LUNZIP=y
  2598. +CONFIG_LZIP=y
  2599. CONFIG_LZOP=y
  2600. CONFIG_LZOP_COMPR_HIGH=y
  2601. CONFIG_RPM2CPIO=y
  2602. diff -urdN busybox-1.27.0.old/configs/android_defconfig busybox-1.27.0.new/configs/android_defconfig
  2603. --- busybox-1.27.0.old/configs/android_defconfig 2017-01-30 21:57:30.000000000 +0100
  2604. +++ busybox-1.27.0.new/configs/android_defconfig 2017-07-07 00:24:37.000000000 +0200
  2605. @@ -142,6 +142,7 @@
  2606. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2607. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2608. CONFIG_FEATURE_SEAMLESS_GZ=y
  2609. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2610. CONFIG_FEATURE_SEAMLESS_Z=y
  2611. CONFIG_AR=y
  2612. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2613. @@ -157,6 +158,8 @@
  2614. CONFIG_GZIP=y
  2615. # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
  2616. CONFIG_GZIP_FAST=0
  2617. +CONFIG_LUNZIP=y
  2618. +CONFIG_LZIP=y
  2619. CONFIG_LZOP=y
  2620. CONFIG_LZOP_COMPR_HIGH=y
  2621. CONFIG_RPM2CPIO=y
  2622. diff -urdN busybox-1.27.0.old/configs/android_ndk_defconfig busybox-1.27.0.new/configs/android_ndk_defconfig
  2623. --- busybox-1.27.0.old/configs/android_ndk_defconfig 2017-01-30 21:57:30.000000000 +0100
  2624. +++ busybox-1.27.0.new/configs/android_ndk_defconfig 2017-07-07 00:24:37.000000000 +0200
  2625. @@ -132,6 +132,7 @@
  2626. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2627. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2628. CONFIG_FEATURE_SEAMLESS_GZ=y
  2629. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2630. CONFIG_FEATURE_SEAMLESS_Z=y
  2631. CONFIG_AR=y
  2632. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2633. @@ -154,6 +155,8 @@
  2634. # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
  2635. CONFIG_GZIP_FAST=0
  2636. # CONFIG_FEATURE_GZIP_LEVELS is not set
  2637. +CONFIG_LUNZIP=y
  2638. +CONFIG_LZIP=y
  2639. CONFIG_LZOP=y
  2640. CONFIG_LZOP_COMPR_HIGH=y
  2641. CONFIG_RPM=y
  2642. diff -urdN busybox-1.27.0.old/configs/cygwin_defconfig busybox-1.27.0.new/configs/cygwin_defconfig
  2643. --- busybox-1.27.0.old/configs/cygwin_defconfig 2017-01-15 00:18:53.000000000 +0100
  2644. +++ busybox-1.27.0.new/configs/cygwin_defconfig 2017-07-07 00:24:37.000000000 +0200
  2645. @@ -122,6 +122,7 @@
  2646. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2647. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2648. CONFIG_FEATURE_SEAMLESS_GZ=y
  2649. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2650. # CONFIG_FEATURE_SEAMLESS_Z is not set
  2651. # CONFIG_AR is not set
  2652. # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
  2653. @@ -136,6 +137,8 @@
  2654. CONFIG_GUNZIP=y
  2655. CONFIG_GZIP=y
  2656. CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
  2657. +CONFIG_LUNZIP=y
  2658. +CONFIG_LZIP=y
  2659. CONFIG_LZOP=y
  2660. # CONFIG_LZOP_COMPR_HIGH is not set
  2661. CONFIG_RPM2CPIO=y
  2662. diff -urdN busybox-1.27.0.old/configs/freebsd_defconfig busybox-1.27.0.new/configs/freebsd_defconfig
  2663. --- busybox-1.27.0.old/configs/freebsd_defconfig 2017-01-15 00:18:53.000000000 +0100
  2664. +++ busybox-1.27.0.new/configs/freebsd_defconfig 2017-07-07 00:24:37.000000000 +0200
  2665. @@ -120,6 +120,7 @@
  2666. CONFIG_FEATURE_SEAMLESS_LZMA=y
  2667. CONFIG_FEATURE_SEAMLESS_BZ2=y
  2668. CONFIG_FEATURE_SEAMLESS_GZ=y
  2669. +CONFIG_FEATURE_SEAMLESS_LZ=y
  2670. CONFIG_FEATURE_SEAMLESS_Z=y
  2671. CONFIG_AR=y
  2672. CONFIG_FEATURE_AR_LONG_FILENAMES=y
  2673. @@ -134,6 +135,8 @@
  2674. CONFIG_GUNZIP=y
  2675. CONFIG_GZIP=y
  2676. CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
  2677. +CONFIG_LUNZIP=y
  2678. +CONFIG_LZIP=y
  2679. CONFIG_LZOP=y
  2680. # CONFIG_LZOP_COMPR_HIGH is not set
  2681. CONFIG_RPM2CPIO=y
  2682. diff -urdN busybox-1.27.0.old/include/bb_archive.h busybox-1.27.0.new/include/bb_archive.h
  2683. --- busybox-1.27.0.old/include/bb_archive.h 2016-11-27 03:55:17.000000000 +0100
  2684. +++ busybox-1.27.0.new/include/bb_archive.h 2017-07-07 00:24:37.000000000 +0200
  2685. @@ -9,6 +9,8 @@
  2686. COMPRESS_MAGIC = 0x1f9d,
  2687. GZIP_MAGIC = 0x1f8b,
  2688. BZIP2_MAGIC = 256 * 'B' + 'Z',
  2689. + LZIP_MAGIC1 = 0x4C5A,
  2690. + LZIP_MAGIC2 = 0x4950,
  2691. /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
  2692. /* More info at: http://tukaani.org/xz/xz-file-format.txt */
  2693. XZ_MAGIC1 = 256 * 0xfd + '7',
  2694. @@ -21,6 +23,8 @@
  2695. COMPRESS_MAGIC = 0x9d1f,
  2696. GZIP_MAGIC = 0x8b1f,
  2697. BZIP2_MAGIC = 'B' + 'Z' * 256,
  2698. + LZIP_MAGIC1 = 0x5A4C,
  2699. + LZIP_MAGIC2 = 0x5049,
  2700. XZ_MAGIC1 = 0xfd + '7' * 256,
  2701. XZ_MAGIC2 = 'z' + ('X' + ('Z' + 0 * 256) * 256) * 256,
  2702. XZ_MAGIC1a = 0xfd + ('7' + ('z' + 'X' * 256) * 256) * 256,
  2703. @@ -189,6 +193,7 @@
  2704. char get_header_tar(archive_handle_t *archive_handle) FAST_FUNC;
  2705. char get_header_tar_gz(archive_handle_t *archive_handle) FAST_FUNC;
  2706. char get_header_tar_bz2(archive_handle_t *archive_handle) FAST_FUNC;
  2707. +char get_header_tar_lz(archive_handle_t *archive_handle) FAST_FUNC;
  2708. char get_header_tar_lzma(archive_handle_t *archive_handle) FAST_FUNC;
  2709. char get_header_tar_xz(archive_handle_t *archive_handle) FAST_FUNC;
  2710. @@ -239,6 +244,7 @@
  2711. IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate) FAST_FUNC;
  2712. IF_DESKTOP(long long) int unpack_gz_stream(transformer_state_t *xstate) FAST_FUNC;
  2713. IF_DESKTOP(long long) int unpack_bz2_stream(transformer_state_t *xstate) FAST_FUNC;
  2714. +IF_DESKTOP(long long) int unpack_lz_stream(transformer_state_t *xstate) FAST_FUNC;
  2715. IF_DESKTOP(long long) int unpack_lzma_stream(transformer_state_t *xstate) FAST_FUNC;
  2716. IF_DESKTOP(long long) int unpack_xz_stream(transformer_state_t *xstate) FAST_FUNC;
  2717. diff -urdN busybox-1.27.0.old/include/libbb.h busybox-1.27.0.new/include/libbb.h
  2718. --- busybox-1.27.0.old/include/libbb.h 2017-07-01 22:51:12.000000000 +0200
  2719. +++ busybox-1.27.0.new/include/libbb.h 2017-07-07 00:24:37.000000000 +0200
  2720. @@ -858,12 +858,13 @@
  2721. #define SEAMLESS_COMPRESSION (0 \
  2722. || ENABLE_FEATURE_SEAMLESS_XZ \
  2723. || ENABLE_FEATURE_SEAMLESS_LZMA \
  2724. + || ENABLE_FEATURE_SEAMLESS_LZ \
  2725. || ENABLE_FEATURE_SEAMLESS_BZ2 \
  2726. || ENABLE_FEATURE_SEAMLESS_GZ \
  2727. || ENABLE_FEATURE_SEAMLESS_Z)
  2728. #if SEAMLESS_COMPRESSION
  2729. -/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
  2730. +/* Autodetects gzip/bzip2/lzip formats. fd may be in the middle of the file! */
  2731. extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
  2732. /* Autodetects .gz etc */
  2733. extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
  2734. @@ -1288,6 +1289,7 @@
  2735. /* Don't need IF_xxx() guard for these */
  2736. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2737. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2738. +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2739. #if ENABLE_ROUTE
  2740. void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
  2741. diff -urdN busybox-1.27.0.old/miscutils/man.c busybox-1.27.0.new/miscutils/man.c
  2742. --- busybox-1.27.0.old/miscutils/man.c 2016-11-27 03:55:17.000000000 +0100
  2743. +++ busybox-1.27.0.new/miscutils/man.c 2017-07-07 00:24:37.000000000 +0200
  2744. @@ -180,6 +180,11 @@
  2745. if (run_pipe(filename_with_zext, man, level))
  2746. return 1;
  2747. #endif
  2748. +#if ENABLE_FEATURE_SEAMLESS_LZ
  2749. + strcpy(ext, "lz");
  2750. + if (run_pipe(filename_with_zext, man, level))
  2751. + return 1;
  2752. +#endif
  2753. #if ENABLE_FEATURE_SEAMLESS_BZ2
  2754. strcpy(ext, "bz2");
  2755. if (run_pipe(filename_with_zext, man, level))
  2756. diff -urdN busybox-1.27.0.old/scripts/fix_ws.sh busybox-1.27.0.new/scripts/fix_ws.sh
  2757. --- busybox-1.27.0.old/scripts/fix_ws.sh 2016-11-27 03:55:17.000000000 +0100
  2758. +++ busybox-1.27.0.new/scripts/fix_ws.sh 2017-07-07 00:24:37.000000000 +0200
  2759. @@ -19,6 +19,7 @@
  2760. | while read name; do
  2761. test "YES" = "${name/*.bz2/YES}" && continue
  2762. test "YES" = "${name/*.gz/YES}" && continue
  2763. + test "YES" = "${name/*.lz/YES}" && continue
  2764. test "YES" = "${name/*.png/YES}" && continue
  2765. test "YES" = "${name/*.gif/YES}" && continue
  2766. test "YES" = "${name/*.jpg/YES}" && continue
  2767. diff -urdN busybox-1.27.0.old/testsuite/bunzip2.tests busybox-1.27.0.new/testsuite/bunzip2.tests
  2768. --- busybox-1.27.0.old/testsuite/bunzip2.tests 2016-11-27 03:55:17.000000000 +0100
  2769. +++ busybox-1.27.0.new/testsuite/bunzip2.tests 2017-07-07 00:24:37.000000000 +0200
  2770. @@ -9,6 +9,9 @@
  2771. elif test "${0##*/}" = "bunzip2.tests"; then
  2772. unpack=bunzip2
  2773. ext=bz2
  2774. +elif test "${0##*/}" = "lunzip.tests"; then
  2775. + unpack=lunzip
  2776. + ext=lz
  2777. else
  2778. echo "WTF? argv0='$0'"
  2779. exit 1
  2780. @@ -35,6 +38,13 @@
  2781. $ECHO -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
  2782. }
  2783. +hello_lz() {
  2784. +# Lzipped "HELLO\n"
  2785. +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x24\x11\x45\xcf\x72\xcd\x3d\x3a\xdf"
  2786. +$ECHO -ne "\xff\xff\xdd\x12\x00\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00\x00\x00"
  2787. +$ECHO -ne "\x00\x00\x2a\x00\x00\x00\x00\x00\x00\x00"
  2788. +}
  2789. +
  2790. # We had bunzip2 error on this .bz2 file (fixed in rev 22521)
  2791. test1_bz2()
  2792. {
  2793. @@ -530,6 +540,12 @@
  2794. expected="ok\n"
  2795. prep; check "$unpack: delete src" "${bb}$unpack t2.$ext; test ! -f t2.$ext && echo ok"
  2796. +expected="ok\n"
  2797. +rm -f t1.*
  2798. +hello_$ext > t1.t$ext
  2799. +check "$unpack: replace .t$ext --> .tar" \
  2800. +"${bb}$unpack t1.t$ext && test -f t1.tar && test ! -f t1.t$ext && echo ok"
  2801. +
  2802. )
  2803. rm -rf testdir
  2804. diff -urdN busybox-1.27.0.old/testsuite/lunzip.tests busybox-1.27.0.new/testsuite/lunzip.tests
  2805. --- busybox-1.27.0.old/testsuite/lunzip.tests 1970-01-01 01:00:00.000000000 +0100
  2806. +++ busybox-1.27.0.new/testsuite/lunzip.tests 2017-07-07 00:24:37.000000000 +0200
  2807. @@ -0,0 +1,3 @@
  2808. +#!/bin/sh
  2809. +
  2810. +. ./bunzip2.tests
  2811. diff -urdN busybox-1.27.0.old/testsuite/lzip.tests busybox-1.27.0.new/testsuite/lzip.tests
  2812. --- busybox-1.27.0.old/testsuite/lzip.tests 1970-01-01 01:00:00.000000000 +0100
  2813. +++ busybox-1.27.0.new/testsuite/lzip.tests 2017-07-07 00:24:38.000000000 +0200
  2814. @@ -0,0 +1,58 @@
  2815. +#!/bin/sh
  2816. +# Copyright (C) 2012-2016 Antonio Diaz Diaz.
  2817. +# Licensed under GPLv2 or later, see file LICENSE in this source tree.
  2818. +
  2819. +. ./testing.sh
  2820. +
  2821. +rm -rf lzip.testdir 2>/dev/null
  2822. +mkdir lzip.testdir
  2823. +
  2824. +# Lzipped "a"
  2825. +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x30\xc1\xfb\xff\xff\xff\xe0\x00\x00\
  2826. +\x00\x43\xbe\xb7\xe8\x01\x00\x00\x00\x00\x00\x00\x00\x25\x00\x00\
  2827. +\x00\x00\x00\x00\x00" > lzip.testdir/a.lz
  2828. +
  2829. +# Lzipped zero-length file
  2830. +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x83\xff\xfb\xff\xff\xc0\x00\x00\x00\
  2831. +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\
  2832. +\x00\x00\x00\x00" > lzip.testdir/zero.lz
  2833. +
  2834. +
  2835. +# testing "test name" "commands" "expected result" "file input" "stdin"
  2836. +
  2837. +testing "lzip reads from standard input" \
  2838. +"busybox lzip | cmp lzip.testdir/a.lz -" "" "" "a"
  2839. +
  2840. +testing "lzip accepts single minus" \
  2841. +"busybox lzip - | cmp lzip.testdir/a.lz -" "" "" "a"
  2842. +
  2843. +testing "lzip compresses a zero-length file" \
  2844. +"> zero ; busybox lzip -c zero | cmp lzip.testdir/zero.lz -" "" "" ""
  2845. +
  2846. +testing "lzip replaces original file" \
  2847. +"$ECHO foo > lzip.testdir/foo
  2848. +rm -f lzip.testdir/foo.lz 2>/dev/null
  2849. +busybox lzip lzip.testdir/foo
  2850. +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok" \
  2851. +"ok\n" "" ""
  2852. +
  2853. +testing "lzip replaces multiple files" \
  2854. +"$ECHO foo > lzip.testdir/foo
  2855. +$ECHO bar > lzip.testdir/bar
  2856. +rm -f lzip.testdir/foo.lz 2>/dev/null
  2857. +rm -f lzip.testdir/bar.lz 2>/dev/null
  2858. +busybox lzip lzip.testdir/foo lzip.testdir/bar
  2859. +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok1
  2860. +test -f lzip.testdir/bar.lz && test ! -f lzip.testdir/bar && echo ok2" \
  2861. +"ok1\nok2\n" "" ""
  2862. +
  2863. +testing "lzip -c does not remove original file" \
  2864. +"$ECHO foo > lzip.testdir/foo
  2865. +busybox lzip -c lzip.testdir/foo >/dev/null
  2866. +test -f lzip.testdir/foo && echo ok" "ok\n" "" ""
  2867. +
  2868. +
  2869. +# Clean up
  2870. +rm -rf lzip.testdir 2>/dev/null
  2871. +
  2872. +exit $FAILCOUNT
  2873. diff -urdN busybox-1.27.0.old/testsuite/tar.tests busybox-1.27.0.new/testsuite/tar.tests
  2874. --- busybox-1.27.0.old/testsuite/tar.tests 2016-12-12 19:16:43.000000000 +0100
  2875. +++ busybox-1.27.0.new/testsuite/tar.tests 2017-07-07 00:24:38.000000000 +0200
  2876. @@ -218,6 +218,25 @@
  2877. "" ""
  2878. SKIP=
  2879. +# lzipped tar archive with a file named ustar containing "ustar\n"
  2880. +optional FEATURE_SEAMLESS_LZ
  2881. +testing "tar extract tlz" "\
  2882. +tar -xyvf - && echo Ok
  2883. +rm ustar || echo BAD
  2884. +" "\
  2885. +ustar
  2886. +Ok
  2887. +" \
  2888. +"" "\
  2889. +\x4c\x5a\x49\x50\x01\x0c\x00\x3a\x9c\xca\xdd\xdf\xd4\xd1\x20\x5c\
  2890. +\xd8\x09\x8b\x76\xed\xd4\xe3\x53\xc0\x91\x33\x1e\x8c\x70\xfe\x9a\
  2891. +\xb3\xd9\xab\xbf\x33\xd0\x4e\x42\x90\xee\xa0\xad\xa7\xa3\x8c\xa9\
  2892. +\xfb\x94\xce\xf9\xdc\x54\x68\x12\x1d\x1b\x42\x36\x10\x5a\xa9\x15\
  2893. +\x59\x4c\x46\xae\x50\xa5\xe7\xe2\x8d\xc0\xf5\x4b\xa3\xfa\xb7\x15\
  2894. +\x6e\x33\x92\xc0\x93\xff\xff\xf7\x6b\x7b\x00\x2e\xb1\xb8\xc0\x00\
  2895. +\x08\x00\x00\x00\x00\x00\x00\x6f\x00\x00\x00\x00\x00\x00\x00"
  2896. +SKIP=
  2897. +
  2898. # Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
  2899. # (the uuencoded hello_world.txz contains one empty file named "hello_world")
  2900. optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ