530-jffs2_make_lzma_available.patch 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180
  1. From: Alexandros C. Couloumbis <alex@ozo.com>
  2. Subject: fs: add jffs2/lzma support (not activated by default yet)
  3. lede-commit: c2c88d315fa0e881f8b19da07b62859b915b11b2
  4. Signed-off-by: Alexandros C. Couloumbis <alex@ozo.com>
  5. ---
  6. fs/jffs2/Kconfig | 9 +
  7. fs/jffs2/Makefile | 3 +
  8. fs/jffs2/compr.c | 6 +
  9. fs/jffs2/compr.h | 10 +-
  10. fs/jffs2/compr_lzma.c | 128 +++
  11. fs/jffs2/super.c | 33 +-
  12. include/linux/lzma.h | 62 ++
  13. include/linux/lzma/LzFind.h | 115 +++
  14. include/linux/lzma/LzHash.h | 54 +
  15. include/linux/lzma/LzmaDec.h | 231 +++++
  16. include/linux/lzma/LzmaEnc.h | 80 ++
  17. include/linux/lzma/Types.h | 226 +++++
  18. include/uapi/linux/jffs2.h | 1 +
  19. lib/Kconfig | 6 +
  20. lib/Makefile | 12 +
  21. lib/lzma/LzFind.c | 761 ++++++++++++++
  22. lib/lzma/LzmaDec.c | 999 +++++++++++++++++++
  23. lib/lzma/LzmaEnc.c | 2271 ++++++++++++++++++++++++++++++++++++++++++
  24. lib/lzma/Makefile | 7 +
  25. 19 files changed, 5008 insertions(+), 6 deletions(-)
  26. create mode 100644 fs/jffs2/compr_lzma.c
  27. create mode 100644 include/linux/lzma.h
  28. create mode 100644 include/linux/lzma/LzFind.h
  29. create mode 100644 include/linux/lzma/LzHash.h
  30. create mode 100644 include/linux/lzma/LzmaDec.h
  31. create mode 100644 include/linux/lzma/LzmaEnc.h
  32. create mode 100644 include/linux/lzma/Types.h
  33. create mode 100644 lib/lzma/LzFind.c
  34. create mode 100644 lib/lzma/LzmaDec.c
  35. create mode 100644 lib/lzma/LzmaEnc.c
  36. create mode 100644 lib/lzma/Makefile
  37. --- a/fs/jffs2/Kconfig
  38. +++ b/fs/jffs2/Kconfig
  39. @@ -139,6 +139,15 @@ config JFFS2_LZO
  40. This feature was added in July, 2007. Say 'N' if you need
  41. compatibility with older bootloaders or kernels.
  42. +config JFFS2_LZMA
  43. + bool "JFFS2 LZMA compression support" if JFFS2_COMPRESSION_OPTIONS
  44. + select LZMA_COMPRESS
  45. + select LZMA_DECOMPRESS
  46. + depends on JFFS2_FS
  47. + default n
  48. + help
  49. + JFFS2 wrapper to the LZMA C SDK
  50. +
  51. config JFFS2_RTIME
  52. bool "JFFS2 RTIME compression support" if JFFS2_COMPRESSION_OPTIONS
  53. depends on JFFS2_FS
  54. --- a/fs/jffs2/Makefile
  55. +++ b/fs/jffs2/Makefile
  56. @@ -19,4 +19,7 @@ jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rub
  57. jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o
  58. jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o
  59. jffs2-$(CONFIG_JFFS2_LZO) += compr_lzo.o
  60. +jffs2-$(CONFIG_JFFS2_LZMA) += compr_lzma.o
  61. jffs2-$(CONFIG_JFFS2_SUMMARY) += summary.o
  62. +
  63. +CFLAGS_compr_lzma.o += -Iinclude/linux -Ilib/lzma
  64. --- a/fs/jffs2/compr.c
  65. +++ b/fs/jffs2/compr.c
  66. @@ -378,6 +378,9 @@ int __init jffs2_compressors_init(void)
  67. #ifdef CONFIG_JFFS2_LZO
  68. jffs2_lzo_init();
  69. #endif
  70. +#ifdef CONFIG_JFFS2_LZMA
  71. + jffs2_lzma_init();
  72. +#endif
  73. /* Setting default compression mode */
  74. #ifdef CONFIG_JFFS2_CMODE_NONE
  75. jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
  76. @@ -401,6 +404,9 @@ int __init jffs2_compressors_init(void)
  77. int jffs2_compressors_exit(void)
  78. {
  79. /* Unregistering compressors */
  80. +#ifdef CONFIG_JFFS2_LZMA
  81. + jffs2_lzma_exit();
  82. +#endif
  83. #ifdef CONFIG_JFFS2_LZO
  84. jffs2_lzo_exit();
  85. #endif
  86. --- a/fs/jffs2/compr.h
  87. +++ b/fs/jffs2/compr.h
  88. @@ -29,9 +29,9 @@
  89. #define JFFS2_DYNRUBIN_PRIORITY 20
  90. #define JFFS2_LZARI_PRIORITY 30
  91. #define JFFS2_RTIME_PRIORITY 50
  92. -#define JFFS2_ZLIB_PRIORITY 60
  93. -#define JFFS2_LZO_PRIORITY 80
  94. -
  95. +#define JFFS2_LZMA_PRIORITY 70
  96. +#define JFFS2_ZLIB_PRIORITY 80
  97. +#define JFFS2_LZO_PRIORITY 90
  98. #define JFFS2_RUBINMIPS_DISABLED /* RUBINs will be used only */
  99. #define JFFS2_DYNRUBIN_DISABLED /* for decompression */
  100. @@ -101,5 +101,9 @@ void jffs2_zlib_exit(void);
  101. int jffs2_lzo_init(void);
  102. void jffs2_lzo_exit(void);
  103. #endif
  104. +#ifdef CONFIG_JFFS2_LZMA
  105. +int jffs2_lzma_init(void);
  106. +void jffs2_lzma_exit(void);
  107. +#endif
  108. #endif /* __JFFS2_COMPR_H__ */
  109. --- /dev/null
  110. +++ b/fs/jffs2/compr_lzma.c
  111. @@ -0,0 +1,128 @@
  112. +/*
  113. + * JFFS2 -- Journalling Flash File System, Version 2.
  114. + *
  115. + * For licensing information, see the file 'LICENCE' in this directory.
  116. + *
  117. + * JFFS2 wrapper to the LZMA C SDK
  118. + *
  119. + */
  120. +
  121. +#include <linux/lzma.h>
  122. +#include "compr.h"
  123. +
  124. +#ifdef __KERNEL__
  125. + static DEFINE_MUTEX(deflate_mutex);
  126. +#endif
  127. +
  128. +CLzmaEncHandle *p;
  129. +Byte propsEncoded[LZMA_PROPS_SIZE];
  130. +SizeT propsSize = sizeof(propsEncoded);
  131. +
  132. +STATIC void lzma_free_workspace(void)
  133. +{
  134. + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
  135. +}
  136. +
  137. +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
  138. +{
  139. + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
  140. + {
  141. + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
  142. + return -ENOMEM;
  143. + }
  144. +
  145. + if (LzmaEnc_SetProps(p, props) != SZ_OK)
  146. + {
  147. + lzma_free_workspace();
  148. + return -1;
  149. + }
  150. +
  151. + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
  152. + {
  153. + lzma_free_workspace();
  154. + return -1;
  155. + }
  156. +
  157. + return 0;
  158. +}
  159. +
  160. +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
  161. + uint32_t *sourcelen, uint32_t *dstlen)
  162. +{
  163. + SizeT compress_size = (SizeT)(*dstlen);
  164. + int ret;
  165. +
  166. + #ifdef __KERNEL__
  167. + mutex_lock(&deflate_mutex);
  168. + #endif
  169. +
  170. + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
  171. + 0, NULL, &lzma_alloc, &lzma_alloc);
  172. +
  173. + #ifdef __KERNEL__
  174. + mutex_unlock(&deflate_mutex);
  175. + #endif
  176. +
  177. + if (ret != SZ_OK)
  178. + return -1;
  179. +
  180. + *dstlen = (uint32_t)compress_size;
  181. +
  182. + return 0;
  183. +}
  184. +
  185. +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
  186. + uint32_t srclen, uint32_t destlen)
  187. +{
  188. + int ret;
  189. + SizeT dl = (SizeT)destlen;
  190. + SizeT sl = (SizeT)srclen;
  191. + ELzmaStatus status;
  192. +
  193. + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
  194. + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
  195. +
  196. + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
  197. + return -1;
  198. +
  199. + return 0;
  200. +}
  201. +
  202. +static struct jffs2_compressor jffs2_lzma_comp = {
  203. + .priority = JFFS2_LZMA_PRIORITY,
  204. + .name = "lzma",
  205. + .compr = JFFS2_COMPR_LZMA,
  206. + .compress = &jffs2_lzma_compress,
  207. + .decompress = &jffs2_lzma_decompress,
  208. + .disabled = 0,
  209. +};
  210. +
  211. +int INIT jffs2_lzma_init(void)
  212. +{
  213. + int ret;
  214. + CLzmaEncProps props;
  215. + LzmaEncProps_Init(&props);
  216. +
  217. + props.dictSize = LZMA_BEST_DICT(0x2000);
  218. + props.level = LZMA_BEST_LEVEL;
  219. + props.lc = LZMA_BEST_LC;
  220. + props.lp = LZMA_BEST_LP;
  221. + props.pb = LZMA_BEST_PB;
  222. + props.fb = LZMA_BEST_FB;
  223. +
  224. + ret = lzma_alloc_workspace(&props);
  225. + if (ret < 0)
  226. + return ret;
  227. +
  228. + ret = jffs2_register_compressor(&jffs2_lzma_comp);
  229. + if (ret)
  230. + lzma_free_workspace();
  231. +
  232. + return ret;
  233. +}
  234. +
  235. +void jffs2_lzma_exit(void)
  236. +{
  237. + jffs2_unregister_compressor(&jffs2_lzma_comp);
  238. + lzma_free_workspace();
  239. +}
  240. --- a/fs/jffs2/super.c
  241. +++ b/fs/jffs2/super.c
  242. @@ -372,14 +372,41 @@ static int __init init_jffs2_fs(void)
  243. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  244. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  245. - pr_info("version 2.2."
  246. + pr_info("version 2.2"
  247. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  248. " (NAND)"
  249. #endif
  250. #ifdef CONFIG_JFFS2_SUMMARY
  251. - " (SUMMARY) "
  252. + " (SUMMARY)"
  253. #endif
  254. - " © 2001-2006 Red Hat, Inc.\n");
  255. +#ifdef CONFIG_JFFS2_ZLIB
  256. + " (ZLIB)"
  257. +#endif
  258. +#ifdef CONFIG_JFFS2_LZO
  259. + " (LZO)"
  260. +#endif
  261. +#ifdef CONFIG_JFFS2_LZMA
  262. + " (LZMA)"
  263. +#endif
  264. +#ifdef CONFIG_JFFS2_RTIME
  265. + " (RTIME)"
  266. +#endif
  267. +#ifdef CONFIG_JFFS2_RUBIN
  268. + " (RUBIN)"
  269. +#endif
  270. +#ifdef CONFIG_JFFS2_CMODE_NONE
  271. + " (CMODE_NONE)"
  272. +#endif
  273. +#ifdef CONFIG_JFFS2_CMODE_PRIORITY
  274. + " (CMODE_PRIORITY)"
  275. +#endif
  276. +#ifdef CONFIG_JFFS2_CMODE_SIZE
  277. + " (CMODE_SIZE)"
  278. +#endif
  279. +#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
  280. + " (CMODE_FAVOURLZO)"
  281. +#endif
  282. + " (c) 2001-2006 Red Hat, Inc.\n");
  283. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  284. sizeof(struct jffs2_inode_info),
  285. --- /dev/null
  286. +++ b/include/linux/lzma.h
  287. @@ -0,0 +1,62 @@
  288. +#ifndef __LZMA_H__
  289. +#define __LZMA_H__
  290. +
  291. +#ifdef __KERNEL__
  292. + #include <linux/kernel.h>
  293. + #include <linux/sched.h>
  294. + #include <linux/slab.h>
  295. + #include <linux/vmalloc.h>
  296. + #include <linux/init.h>
  297. + #define LZMA_MALLOC vmalloc
  298. + #define LZMA_FREE vfree
  299. + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
  300. + #define INIT __init
  301. + #define STATIC static
  302. +#else
  303. + #include <stdint.h>
  304. + #include <stdlib.h>
  305. + #include <stdio.h>
  306. + #include <unistd.h>
  307. + #include <string.h>
  308. + #include <asm/types.h>
  309. + #include <errno.h>
  310. + #include <linux/jffs2.h>
  311. + #ifndef PAGE_SIZE
  312. + extern int page_size;
  313. + #define PAGE_SIZE page_size
  314. + #endif
  315. + #define LZMA_MALLOC malloc
  316. + #define LZMA_FREE free
  317. + #define PRINT_ERROR(msg) fprintf(stderr, msg)
  318. + #define INIT
  319. + #define STATIC
  320. +#endif
  321. +
  322. +#include "lzma/LzmaDec.h"
  323. +#include "lzma/LzmaEnc.h"
  324. +
  325. +#define LZMA_BEST_LEVEL (9)
  326. +#define LZMA_BEST_LC (0)
  327. +#define LZMA_BEST_LP (0)
  328. +#define LZMA_BEST_PB (0)
  329. +#define LZMA_BEST_FB (273)
  330. +
  331. +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
  332. +
  333. +static void *p_lzma_malloc(void *p, size_t size)
  334. +{
  335. + if (size == 0)
  336. + return NULL;
  337. +
  338. + return LZMA_MALLOC(size);
  339. +}
  340. +
  341. +static void p_lzma_free(void *p, void *address)
  342. +{
  343. + if (address != NULL)
  344. + LZMA_FREE(address);
  345. +}
  346. +
  347. +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
  348. +
  349. +#endif
  350. --- /dev/null
  351. +++ b/include/linux/lzma/LzFind.h
  352. @@ -0,0 +1,115 @@
  353. +/* LzFind.h -- Match finder for LZ algorithms
  354. +2009-04-22 : Igor Pavlov : Public domain */
  355. +
  356. +#ifndef __LZ_FIND_H
  357. +#define __LZ_FIND_H
  358. +
  359. +#include "Types.h"
  360. +
  361. +#ifdef __cplusplus
  362. +extern "C" {
  363. +#endif
  364. +
  365. +typedef UInt32 CLzRef;
  366. +
  367. +typedef struct _CMatchFinder
  368. +{
  369. + Byte *buffer;
  370. + UInt32 pos;
  371. + UInt32 posLimit;
  372. + UInt32 streamPos;
  373. + UInt32 lenLimit;
  374. +
  375. + UInt32 cyclicBufferPos;
  376. + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
  377. +
  378. + UInt32 matchMaxLen;
  379. + CLzRef *hash;
  380. + CLzRef *son;
  381. + UInt32 hashMask;
  382. + UInt32 cutValue;
  383. +
  384. + Byte *bufferBase;
  385. + ISeqInStream *stream;
  386. + int streamEndWasReached;
  387. +
  388. + UInt32 blockSize;
  389. + UInt32 keepSizeBefore;
  390. + UInt32 keepSizeAfter;
  391. +
  392. + UInt32 numHashBytes;
  393. + int directInput;
  394. + size_t directInputRem;
  395. + int btMode;
  396. + int bigHash;
  397. + UInt32 historySize;
  398. + UInt32 fixedHashSize;
  399. + UInt32 hashSizeSum;
  400. + UInt32 numSons;
  401. + SRes result;
  402. + UInt32 crc[256];
  403. +} CMatchFinder;
  404. +
  405. +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
  406. +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
  407. +
  408. +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
  409. +
  410. +int MatchFinder_NeedMove(CMatchFinder *p);
  411. +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
  412. +void MatchFinder_MoveBlock(CMatchFinder *p);
  413. +void MatchFinder_ReadIfRequired(CMatchFinder *p);
  414. +
  415. +void MatchFinder_Construct(CMatchFinder *p);
  416. +
  417. +/* Conditions:
  418. + historySize <= 3 GB
  419. + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
  420. +*/
  421. +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
  422. + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
  423. + ISzAlloc *alloc);
  424. +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
  425. +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems);
  426. +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
  427. +
  428. +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son,
  429. + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
  430. + UInt32 *distances, UInt32 maxLen);
  431. +
  432. +/*
  433. +Conditions:
  434. + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
  435. + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
  436. +*/
  437. +
  438. +typedef void (*Mf_Init_Func)(void *object);
  439. +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
  440. +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
  441. +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
  442. +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
  443. +typedef void (*Mf_Skip_Func)(void *object, UInt32);
  444. +
  445. +typedef struct _IMatchFinder
  446. +{
  447. + Mf_Init_Func Init;
  448. + Mf_GetIndexByte_Func GetIndexByte;
  449. + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
  450. + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
  451. + Mf_GetMatches_Func GetMatches;
  452. + Mf_Skip_Func Skip;
  453. +} IMatchFinder;
  454. +
  455. +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
  456. +
  457. +void MatchFinder_Init(CMatchFinder *p);
  458. +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
  459. +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
  460. +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
  461. +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
  462. +
  463. +#ifdef __cplusplus
  464. +}
  465. +#endif
  466. +
  467. +#endif
  468. --- /dev/null
  469. +++ b/include/linux/lzma/LzHash.h
  470. @@ -0,0 +1,54 @@
  471. +/* LzHash.h -- HASH functions for LZ algorithms
  472. +2009-02-07 : Igor Pavlov : Public domain */
  473. +
  474. +#ifndef __LZ_HASH_H
  475. +#define __LZ_HASH_H
  476. +
  477. +#define kHash2Size (1 << 10)
  478. +#define kHash3Size (1 << 16)
  479. +#define kHash4Size (1 << 20)
  480. +
  481. +#define kFix3HashSize (kHash2Size)
  482. +#define kFix4HashSize (kHash2Size + kHash3Size)
  483. +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
  484. +
  485. +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
  486. +
  487. +#define HASH3_CALC { \
  488. + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
  489. + hash2Value = temp & (kHash2Size - 1); \
  490. + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
  491. +
  492. +#define HASH4_CALC { \
  493. + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
  494. + hash2Value = temp & (kHash2Size - 1); \
  495. + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
  496. + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
  497. +
  498. +#define HASH5_CALC { \
  499. + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
  500. + hash2Value = temp & (kHash2Size - 1); \
  501. + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
  502. + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
  503. + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
  504. + hash4Value &= (kHash4Size - 1); }
  505. +
  506. +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
  507. +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
  508. +
  509. +
  510. +#define MT_HASH2_CALC \
  511. + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
  512. +
  513. +#define MT_HASH3_CALC { \
  514. + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
  515. + hash2Value = temp & (kHash2Size - 1); \
  516. + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
  517. +
  518. +#define MT_HASH4_CALC { \
  519. + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
  520. + hash2Value = temp & (kHash2Size - 1); \
  521. + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
  522. + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
  523. +
  524. +#endif
  525. --- /dev/null
  526. +++ b/include/linux/lzma/LzmaDec.h
  527. @@ -0,0 +1,231 @@
  528. +/* LzmaDec.h -- LZMA Decoder
  529. +2009-02-07 : Igor Pavlov : Public domain */
  530. +
  531. +#ifndef __LZMA_DEC_H
  532. +#define __LZMA_DEC_H
  533. +
  534. +#include "Types.h"
  535. +
  536. +#ifdef __cplusplus
  537. +extern "C" {
  538. +#endif
  539. +
  540. +/* #define _LZMA_PROB32 */
  541. +/* _LZMA_PROB32 can increase the speed on some CPUs,
  542. + but memory usage for CLzmaDec::probs will be doubled in that case */
  543. +
  544. +#ifdef _LZMA_PROB32
  545. +#define CLzmaProb UInt32
  546. +#else
  547. +#define CLzmaProb UInt16
  548. +#endif
  549. +
  550. +
  551. +/* ---------- LZMA Properties ---------- */
  552. +
  553. +#define LZMA_PROPS_SIZE 5
  554. +
  555. +typedef struct _CLzmaProps
  556. +{
  557. + unsigned lc, lp, pb;
  558. + UInt32 dicSize;
  559. +} CLzmaProps;
  560. +
  561. +/* LzmaProps_Decode - decodes properties
  562. +Returns:
  563. + SZ_OK
  564. + SZ_ERROR_UNSUPPORTED - Unsupported properties
  565. +*/
  566. +
  567. +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
  568. +
  569. +
  570. +/* ---------- LZMA Decoder state ---------- */
  571. +
  572. +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
  573. + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
  574. +
  575. +#define LZMA_REQUIRED_INPUT_MAX 20
  576. +
  577. +typedef struct
  578. +{
  579. + CLzmaProps prop;
  580. + CLzmaProb *probs;
  581. + Byte *dic;
  582. + const Byte *buf;
  583. + UInt32 range, code;
  584. + SizeT dicPos;
  585. + SizeT dicBufSize;
  586. + UInt32 processedPos;
  587. + UInt32 checkDicSize;
  588. + unsigned state;
  589. + UInt32 reps[4];
  590. + unsigned remainLen;
  591. + int needFlush;
  592. + int needInitState;
  593. + UInt32 numProbs;
  594. + unsigned tempBufSize;
  595. + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
  596. +} CLzmaDec;
  597. +
  598. +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
  599. +
  600. +void LzmaDec_Init(CLzmaDec *p);
  601. +
  602. +/* There are two types of LZMA streams:
  603. + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
  604. + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
  605. +
  606. +typedef enum
  607. +{
  608. + LZMA_FINISH_ANY, /* finish at any point */
  609. + LZMA_FINISH_END /* block must be finished at the end */
  610. +} ELzmaFinishMode;
  611. +
  612. +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
  613. +
  614. + You must use LZMA_FINISH_END, when you know that current output buffer
  615. + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
  616. +
  617. + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
  618. + and output value of destLen will be less than output buffer size limit.
  619. + You can check status result also.
  620. +
  621. + You can use multiple checks to test data integrity after full decompression:
  622. + 1) Check Result and "status" variable.
  623. + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
  624. + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
  625. + You must use correct finish mode in that case. */
  626. +
  627. +typedef enum
  628. +{
  629. + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
  630. + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
  631. + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
  632. + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
  633. + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
  634. +} ELzmaStatus;
  635. +
  636. +/* ELzmaStatus is used only as output value for function call */
  637. +
  638. +
  639. +/* ---------- Interfaces ---------- */
  640. +
  641. +/* There are 3 levels of interfaces:
  642. + 1) Dictionary Interface
  643. + 2) Buffer Interface
  644. + 3) One Call Interface
  645. + You can select any of these interfaces, but don't mix functions from different
  646. + groups for same object. */
  647. +
  648. +
  649. +/* There are two variants to allocate state for Dictionary Interface:
  650. + 1) LzmaDec_Allocate / LzmaDec_Free
  651. + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
  652. + You can use variant 2, if you set dictionary buffer manually.
  653. + For Buffer Interface you must always use variant 1.
  654. +
  655. +LzmaDec_Allocate* can return:
  656. + SZ_OK
  657. + SZ_ERROR_MEM - Memory allocation error
  658. + SZ_ERROR_UNSUPPORTED - Unsupported properties
  659. +*/
  660. +
  661. +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
  662. +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
  663. +
  664. +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
  665. +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
  666. +
  667. +/* ---------- Dictionary Interface ---------- */
  668. +
  669. +/* You can use it, if you want to eliminate the overhead for data copying from
  670. + dictionary to some other external buffer.
  671. + You must work with CLzmaDec variables directly in this interface.
  672. +
  673. + STEPS:
  674. + LzmaDec_Constr()
  675. + LzmaDec_Allocate()
  676. + for (each new stream)
  677. + {
  678. + LzmaDec_Init()
  679. + while (it needs more decompression)
  680. + {
  681. + LzmaDec_DecodeToDic()
  682. + use data from CLzmaDec::dic and update CLzmaDec::dicPos
  683. + }
  684. + }
  685. + LzmaDec_Free()
  686. +*/
  687. +
  688. +/* LzmaDec_DecodeToDic
  689. +
  690. + The decoding to internal dictionary buffer (CLzmaDec::dic).
  691. + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
  692. +
  693. +finishMode:
  694. + It has meaning only if the decoding reaches output limit (dicLimit).
  695. + LZMA_FINISH_ANY - Decode just dicLimit bytes.
  696. + LZMA_FINISH_END - Stream must be finished after dicLimit.
  697. +
  698. +Returns:
  699. + SZ_OK
  700. + status:
  701. + LZMA_STATUS_FINISHED_WITH_MARK
  702. + LZMA_STATUS_NOT_FINISHED
  703. + LZMA_STATUS_NEEDS_MORE_INPUT
  704. + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
  705. + SZ_ERROR_DATA - Data error
  706. +*/
  707. +
  708. +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
  709. + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
  710. +
  711. +
  712. +/* ---------- Buffer Interface ---------- */
  713. +
  714. +/* It's zlib-like interface.
  715. + See LzmaDec_DecodeToDic description for information about STEPS and return results,
  716. + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
  717. + to work with CLzmaDec variables manually.
  718. +
  719. +finishMode:
  720. + It has meaning only if the decoding reaches output limit (*destLen).
  721. + LZMA_FINISH_ANY - Decode just destLen bytes.
  722. + LZMA_FINISH_END - Stream must be finished after (*destLen).
  723. +*/
  724. +
  725. +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
  726. + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
  727. +
  728. +
  729. +/* ---------- One Call Interface ---------- */
  730. +
  731. +/* LzmaDecode
  732. +
  733. +finishMode:
  734. + It has meaning only if the decoding reaches output limit (*destLen).
  735. + LZMA_FINISH_ANY - Decode just destLen bytes.
  736. + LZMA_FINISH_END - Stream must be finished after (*destLen).
  737. +
  738. +Returns:
  739. + SZ_OK
  740. + status:
  741. + LZMA_STATUS_FINISHED_WITH_MARK
  742. + LZMA_STATUS_NOT_FINISHED
  743. + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
  744. + SZ_ERROR_DATA - Data error
  745. + SZ_ERROR_MEM - Memory allocation error
  746. + SZ_ERROR_UNSUPPORTED - Unsupported properties
  747. + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
  748. +*/
  749. +
  750. +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  751. + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
  752. + ELzmaStatus *status, ISzAlloc *alloc);
  753. +
  754. +#ifdef __cplusplus
  755. +}
  756. +#endif
  757. +
  758. +#endif
  759. --- /dev/null
  760. +++ b/include/linux/lzma/LzmaEnc.h
  761. @@ -0,0 +1,80 @@
  762. +/* LzmaEnc.h -- LZMA Encoder
  763. +2009-02-07 : Igor Pavlov : Public domain */
  764. +
  765. +#ifndef __LZMA_ENC_H
  766. +#define __LZMA_ENC_H
  767. +
  768. +#include "Types.h"
  769. +
  770. +#ifdef __cplusplus
  771. +extern "C" {
  772. +#endif
  773. +
  774. +#define LZMA_PROPS_SIZE 5
  775. +
  776. +typedef struct _CLzmaEncProps
  777. +{
  778. + int level; /* 0 <= level <= 9 */
  779. + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
  780. + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
  781. + default = (1 << 24) */
  782. + int lc; /* 0 <= lc <= 8, default = 3 */
  783. + int lp; /* 0 <= lp <= 4, default = 0 */
  784. + int pb; /* 0 <= pb <= 4, default = 2 */
  785. + int algo; /* 0 - fast, 1 - normal, default = 1 */
  786. + int fb; /* 5 <= fb <= 273, default = 32 */
  787. + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
  788. + int numHashBytes; /* 2, 3 or 4, default = 4 */
  789. + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
  790. + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
  791. + int numThreads; /* 1 or 2, default = 2 */
  792. +} CLzmaEncProps;
  793. +
  794. +void LzmaEncProps_Init(CLzmaEncProps *p);
  795. +void LzmaEncProps_Normalize(CLzmaEncProps *p);
  796. +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
  797. +
  798. +
  799. +/* ---------- CLzmaEncHandle Interface ---------- */
  800. +
  801. +/* LzmaEnc_* functions can return the following exit codes:
  802. +Returns:
  803. + SZ_OK - OK
  804. + SZ_ERROR_MEM - Memory allocation error
  805. + SZ_ERROR_PARAM - Incorrect paramater in props
  806. + SZ_ERROR_WRITE - Write callback error.
  807. + SZ_ERROR_PROGRESS - some break from progress callback
  808. + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
  809. +*/
  810. +
  811. +typedef void * CLzmaEncHandle;
  812. +
  813. +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
  814. +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
  815. +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
  816. +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
  817. +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
  818. + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
  819. +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  820. + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
  821. +
  822. +/* ---------- One Call Interface ---------- */
  823. +
  824. +/* LzmaEncode
  825. +Return code:
  826. + SZ_OK - OK
  827. + SZ_ERROR_MEM - Memory allocation error
  828. + SZ_ERROR_PARAM - Incorrect paramater
  829. + SZ_ERROR_OUTPUT_EOF - output buffer overflow
  830. + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
  831. +*/
  832. +
  833. +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  834. + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
  835. + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
  836. +
  837. +#ifdef __cplusplus
  838. +}
  839. +#endif
  840. +
  841. +#endif
  842. --- /dev/null
  843. +++ b/include/linux/lzma/Types.h
  844. @@ -0,0 +1,226 @@
  845. +/* Types.h -- Basic types
  846. +2009-11-23 : Igor Pavlov : Public domain */
  847. +
  848. +#ifndef __7Z_TYPES_H
  849. +#define __7Z_TYPES_H
  850. +
  851. +#include <stddef.h>
  852. +
  853. +#ifdef _WIN32
  854. +#include <windows.h>
  855. +#endif
  856. +
  857. +#ifndef EXTERN_C_BEGIN
  858. +#ifdef __cplusplus
  859. +#define EXTERN_C_BEGIN extern "C" {
  860. +#define EXTERN_C_END }
  861. +#else
  862. +#define EXTERN_C_BEGIN
  863. +#define EXTERN_C_END
  864. +#endif
  865. +#endif
  866. +
  867. +EXTERN_C_BEGIN
  868. +
  869. +#define SZ_OK 0
  870. +
  871. +#define SZ_ERROR_DATA 1
  872. +#define SZ_ERROR_MEM 2
  873. +#define SZ_ERROR_CRC 3
  874. +#define SZ_ERROR_UNSUPPORTED 4
  875. +#define SZ_ERROR_PARAM 5
  876. +#define SZ_ERROR_INPUT_EOF 6
  877. +#define SZ_ERROR_OUTPUT_EOF 7
  878. +#define SZ_ERROR_READ 8
  879. +#define SZ_ERROR_WRITE 9
  880. +#define SZ_ERROR_PROGRESS 10
  881. +#define SZ_ERROR_FAIL 11
  882. +#define SZ_ERROR_THREAD 12
  883. +
  884. +#define SZ_ERROR_ARCHIVE 16
  885. +#define SZ_ERROR_NO_ARCHIVE 17
  886. +
  887. +typedef int SRes;
  888. +
  889. +#ifdef _WIN32
  890. +typedef DWORD WRes;
  891. +#else
  892. +typedef int WRes;
  893. +#endif
  894. +
  895. +#ifndef RINOK
  896. +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
  897. +#endif
  898. +
  899. +typedef unsigned char Byte;
  900. +typedef short Int16;
  901. +typedef unsigned short UInt16;
  902. +
  903. +#ifdef _LZMA_UINT32_IS_ULONG
  904. +typedef long Int32;
  905. +typedef unsigned long UInt32;
  906. +#else
  907. +typedef int Int32;
  908. +typedef unsigned int UInt32;
  909. +#endif
  910. +
  911. +#ifdef _SZ_NO_INT_64
  912. +
  913. +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
  914. + NOTES: Some code will work incorrectly in that case! */
  915. +
  916. +typedef long Int64;
  917. +typedef unsigned long UInt64;
  918. +
  919. +#else
  920. +
  921. +#if defined(_MSC_VER) || defined(__BORLANDC__)
  922. +typedef __int64 Int64;
  923. +typedef unsigned __int64 UInt64;
  924. +#else
  925. +typedef long long int Int64;
  926. +typedef unsigned long long int UInt64;
  927. +#endif
  928. +
  929. +#endif
  930. +
  931. +#ifdef _LZMA_NO_SYSTEM_SIZE_T
  932. +typedef UInt32 SizeT;
  933. +#else
  934. +typedef size_t SizeT;
  935. +#endif
  936. +
  937. +typedef int Bool;
  938. +#define True 1
  939. +#define False 0
  940. +
  941. +
  942. +#ifdef _WIN32
  943. +#define MY_STD_CALL __stdcall
  944. +#else
  945. +#define MY_STD_CALL
  946. +#endif
  947. +
  948. +#ifdef _MSC_VER
  949. +
  950. +#if _MSC_VER >= 1300
  951. +#define MY_NO_INLINE __declspec(noinline)
  952. +#else
  953. +#define MY_NO_INLINE
  954. +#endif
  955. +
  956. +#define MY_CDECL __cdecl
  957. +#define MY_FAST_CALL __fastcall
  958. +
  959. +#else
  960. +
  961. +#define MY_CDECL
  962. +#define MY_FAST_CALL
  963. +
  964. +#endif
  965. +
  966. +
  967. +/* The following interfaces use first parameter as pointer to structure */
  968. +
  969. +typedef struct
  970. +{
  971. + SRes (*Read)(void *p, void *buf, size_t *size);
  972. + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
  973. + (output(*size) < input(*size)) is allowed */
  974. +} ISeqInStream;
  975. +
  976. +/* it can return SZ_ERROR_INPUT_EOF */
  977. +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);
  978. +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);
  979. +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);
  980. +
  981. +typedef struct
  982. +{
  983. + size_t (*Write)(void *p, const void *buf, size_t size);
  984. + /* Returns: result - the number of actually written bytes.
  985. + (result < size) means error */
  986. +} ISeqOutStream;
  987. +
  988. +typedef enum
  989. +{
  990. + SZ_SEEK_SET = 0,
  991. + SZ_SEEK_CUR = 1,
  992. + SZ_SEEK_END = 2
  993. +} ESzSeek;
  994. +
  995. +typedef struct
  996. +{
  997. + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
  998. + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
  999. +} ISeekInStream;
  1000. +
  1001. +typedef struct
  1002. +{
  1003. + SRes (*Look)(void *p, void **buf, size_t *size);
  1004. + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
  1005. + (output(*size) > input(*size)) is not allowed
  1006. + (output(*size) < input(*size)) is allowed */
  1007. + SRes (*Skip)(void *p, size_t offset);
  1008. + /* offset must be <= output(*size) of Look */
  1009. +
  1010. + SRes (*Read)(void *p, void *buf, size_t *size);
  1011. + /* reads directly (without buffer). It's same as ISeqInStream::Read */
  1012. + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
  1013. +} ILookInStream;
  1014. +
  1015. +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);
  1016. +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
  1017. +
  1018. +/* reads via ILookInStream::Read */
  1019. +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
  1020. +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
  1021. +
  1022. +#define LookToRead_BUF_SIZE (1 << 14)
  1023. +
  1024. +typedef struct
  1025. +{
  1026. + ILookInStream s;
  1027. + ISeekInStream *realStream;
  1028. + size_t pos;
  1029. + size_t size;
  1030. + Byte buf[LookToRead_BUF_SIZE];
  1031. +} CLookToRead;
  1032. +
  1033. +void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
  1034. +void LookToRead_Init(CLookToRead *p);
  1035. +
  1036. +typedef struct
  1037. +{
  1038. + ISeqInStream s;
  1039. + ILookInStream *realStream;
  1040. +} CSecToLook;
  1041. +
  1042. +void SecToLook_CreateVTable(CSecToLook *p);
  1043. +
  1044. +typedef struct
  1045. +{
  1046. + ISeqInStream s;
  1047. + ILookInStream *realStream;
  1048. +} CSecToRead;
  1049. +
  1050. +void SecToRead_CreateVTable(CSecToRead *p);
  1051. +
  1052. +typedef struct
  1053. +{
  1054. + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
  1055. + /* Returns: result. (result != SZ_OK) means break.
  1056. + Value (UInt64)(Int64)-1 for size means unknown value. */
  1057. +} ICompressProgress;
  1058. +
  1059. +typedef struct
  1060. +{
  1061. + void *(*Alloc)(void *p, size_t size);
  1062. + void (*Free)(void *p, void *address); /* address can be 0 */
  1063. +} ISzAlloc;
  1064. +
  1065. +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
  1066. +#define IAlloc_Free(p, a) (p)->Free((p), a)
  1067. +
  1068. +EXTERN_C_END
  1069. +
  1070. +#endif
  1071. --- a/include/uapi/linux/jffs2.h
  1072. +++ b/include/uapi/linux/jffs2.h
  1073. @@ -46,6 +46,7 @@
  1074. #define JFFS2_COMPR_DYNRUBIN 0x05
  1075. #define JFFS2_COMPR_ZLIB 0x06
  1076. #define JFFS2_COMPR_LZO 0x07
  1077. +#define JFFS2_COMPR_LZMA 0x08
  1078. /* Compatibility flags. */
  1079. #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
  1080. #define JFFS2_NODE_ACCURATE 0x2000
  1081. --- a/lib/Kconfig
  1082. +++ b/lib/Kconfig
  1083. @@ -259,6 +259,12 @@ config ZSTD_DECOMPRESS
  1084. source "lib/xz/Kconfig"
  1085. +config LZMA_COMPRESS
  1086. + tristate
  1087. +
  1088. +config LZMA_DECOMPRESS
  1089. + tristate
  1090. +
  1091. #
  1092. # These all provide a common interface (hence the apparent duplication with
  1093. # ZLIB_INFLATE; DECOMPRESS_GZIP is just a wrapper.)
  1094. --- a/lib/Makefile
  1095. +++ b/lib/Makefile
  1096. @@ -3,6 +3,16 @@
  1097. # Makefile for some libs needed in the kernel.
  1098. #
  1099. +ifdef CONFIG_JFFS2_ZLIB
  1100. + CONFIG_ZLIB_INFLATE:=y
  1101. + CONFIG_ZLIB_DEFLATE:=y
  1102. +endif
  1103. +
  1104. +ifdef CONFIG_JFFS2_LZMA
  1105. + CONFIG_LZMA_DECOMPRESS:=y
  1106. + CONFIG_LZMA_COMPRESS:=y
  1107. +endif
  1108. +
  1109. ifdef CONFIG_FUNCTION_TRACER
  1110. ORIG_CFLAGS := $(KBUILD_CFLAGS)
  1111. KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
  1112. @@ -122,6 +132,8 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd/
  1113. obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd/
  1114. obj-$(CONFIG_XZ_DEC) += xz/
  1115. obj-$(CONFIG_RAID6_PQ) += raid6/
  1116. +obj-$(CONFIG_LZMA_COMPRESS) += lzma/
  1117. +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma/
  1118. lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
  1119. lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
  1120. --- /dev/null
  1121. +++ b/lib/lzma/LzFind.c
  1122. @@ -0,0 +1,761 @@
  1123. +/* LzFind.c -- Match finder for LZ algorithms
  1124. +2009-04-22 : Igor Pavlov : Public domain */
  1125. +
  1126. +#include <string.h>
  1127. +
  1128. +#include "LzFind.h"
  1129. +#include "LzHash.h"
  1130. +
  1131. +#define kEmptyHashValue 0
  1132. +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
  1133. +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
  1134. +#define kNormalizeMask (~(kNormalizeStepMin - 1))
  1135. +#define kMaxHistorySize ((UInt32)3 << 30)
  1136. +
  1137. +#define kStartMaxLen 3
  1138. +
  1139. +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
  1140. +{
  1141. + if (!p->directInput)
  1142. + {
  1143. + alloc->Free(alloc, p->bufferBase);
  1144. + p->bufferBase = 0;
  1145. + }
  1146. +}
  1147. +
  1148. +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
  1149. +
  1150. +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
  1151. +{
  1152. + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
  1153. + if (p->directInput)
  1154. + {
  1155. + p->blockSize = blockSize;
  1156. + return 1;
  1157. + }
  1158. + if (p->bufferBase == 0 || p->blockSize != blockSize)
  1159. + {
  1160. + LzInWindow_Free(p, alloc);
  1161. + p->blockSize = blockSize;
  1162. + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
  1163. + }
  1164. + return (p->bufferBase != 0);
  1165. +}
  1166. +
  1167. +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
  1168. +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
  1169. +
  1170. +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
  1171. +
  1172. +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
  1173. +{
  1174. + p->posLimit -= subValue;
  1175. + p->pos -= subValue;
  1176. + p->streamPos -= subValue;
  1177. +}
  1178. +
  1179. +static void MatchFinder_ReadBlock(CMatchFinder *p)
  1180. +{
  1181. + if (p->streamEndWasReached || p->result != SZ_OK)
  1182. + return;
  1183. + if (p->directInput)
  1184. + {
  1185. + UInt32 curSize = 0xFFFFFFFF - p->streamPos;
  1186. + if (curSize > p->directInputRem)
  1187. + curSize = (UInt32)p->directInputRem;
  1188. + p->directInputRem -= curSize;
  1189. + p->streamPos += curSize;
  1190. + if (p->directInputRem == 0)
  1191. + p->streamEndWasReached = 1;
  1192. + return;
  1193. + }
  1194. + for (;;)
  1195. + {
  1196. + Byte *dest = p->buffer + (p->streamPos - p->pos);
  1197. + size_t size = (p->bufferBase + p->blockSize - dest);
  1198. + if (size == 0)
  1199. + return;
  1200. + p->result = p->stream->Read(p->stream, dest, &size);
  1201. + if (p->result != SZ_OK)
  1202. + return;
  1203. + if (size == 0)
  1204. + {
  1205. + p->streamEndWasReached = 1;
  1206. + return;
  1207. + }
  1208. + p->streamPos += (UInt32)size;
  1209. + if (p->streamPos - p->pos > p->keepSizeAfter)
  1210. + return;
  1211. + }
  1212. +}
  1213. +
  1214. +void MatchFinder_MoveBlock(CMatchFinder *p)
  1215. +{
  1216. + memmove(p->bufferBase,
  1217. + p->buffer - p->keepSizeBefore,
  1218. + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
  1219. + p->buffer = p->bufferBase + p->keepSizeBefore;
  1220. +}
  1221. +
  1222. +int MatchFinder_NeedMove(CMatchFinder *p)
  1223. +{
  1224. + if (p->directInput)
  1225. + return 0;
  1226. + /* if (p->streamEndWasReached) return 0; */
  1227. + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
  1228. +}
  1229. +
  1230. +void MatchFinder_ReadIfRequired(CMatchFinder *p)
  1231. +{
  1232. + if (p->streamEndWasReached)
  1233. + return;
  1234. + if (p->keepSizeAfter >= p->streamPos - p->pos)
  1235. + MatchFinder_ReadBlock(p);
  1236. +}
  1237. +
  1238. +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
  1239. +{
  1240. + if (MatchFinder_NeedMove(p))
  1241. + MatchFinder_MoveBlock(p);
  1242. + MatchFinder_ReadBlock(p);
  1243. +}
  1244. +
  1245. +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
  1246. +{
  1247. + p->cutValue = 32;
  1248. + p->btMode = 1;
  1249. + p->numHashBytes = 4;
  1250. + p->bigHash = 0;
  1251. +}
  1252. +
  1253. +#define kCrcPoly 0xEDB88320
  1254. +
  1255. +void MatchFinder_Construct(CMatchFinder *p)
  1256. +{
  1257. + UInt32 i;
  1258. + p->bufferBase = 0;
  1259. + p->directInput = 0;
  1260. + p->hash = 0;
  1261. + MatchFinder_SetDefaultSettings(p);
  1262. +
  1263. + for (i = 0; i < 256; i++)
  1264. + {
  1265. + UInt32 r = i;
  1266. + int j;
  1267. + for (j = 0; j < 8; j++)
  1268. + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
  1269. + p->crc[i] = r;
  1270. + }
  1271. +}
  1272. +
  1273. +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
  1274. +{
  1275. + alloc->Free(alloc, p->hash);
  1276. + p->hash = 0;
  1277. +}
  1278. +
  1279. +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
  1280. +{
  1281. + MatchFinder_FreeThisClassMemory(p, alloc);
  1282. + LzInWindow_Free(p, alloc);
  1283. +}
  1284. +
  1285. +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
  1286. +{
  1287. + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
  1288. + if (sizeInBytes / sizeof(CLzRef) != num)
  1289. + return 0;
  1290. + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
  1291. +}
  1292. +
  1293. +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
  1294. + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
  1295. + ISzAlloc *alloc)
  1296. +{
  1297. + UInt32 sizeReserv;
  1298. + if (historySize > kMaxHistorySize)
  1299. + {
  1300. + MatchFinder_Free(p, alloc);
  1301. + return 0;
  1302. + }
  1303. + sizeReserv = historySize >> 1;
  1304. + if (historySize > ((UInt32)2 << 30))
  1305. + sizeReserv = historySize >> 2;
  1306. + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
  1307. +
  1308. + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
  1309. + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
  1310. + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
  1311. + if (LzInWindow_Create(p, sizeReserv, alloc))
  1312. + {
  1313. + UInt32 newCyclicBufferSize = historySize + 1;
  1314. + UInt32 hs;
  1315. + p->matchMaxLen = matchMaxLen;
  1316. + {
  1317. + p->fixedHashSize = 0;
  1318. + if (p->numHashBytes == 2)
  1319. + hs = (1 << 16) - 1;
  1320. + else
  1321. + {
  1322. + hs = historySize - 1;
  1323. + hs |= (hs >> 1);
  1324. + hs |= (hs >> 2);
  1325. + hs |= (hs >> 4);
  1326. + hs |= (hs >> 8);
  1327. + hs >>= 1;
  1328. + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
  1329. + if (hs > (1 << 24))
  1330. + {
  1331. + if (p->numHashBytes == 3)
  1332. + hs = (1 << 24) - 1;
  1333. + else
  1334. + hs >>= 1;
  1335. + }
  1336. + }
  1337. + p->hashMask = hs;
  1338. + hs++;
  1339. + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
  1340. + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
  1341. + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
  1342. + hs += p->fixedHashSize;
  1343. + }
  1344. +
  1345. + {
  1346. + UInt32 prevSize = p->hashSizeSum + p->numSons;
  1347. + UInt32 newSize;
  1348. + p->historySize = historySize;
  1349. + p->hashSizeSum = hs;
  1350. + p->cyclicBufferSize = newCyclicBufferSize;
  1351. + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
  1352. + newSize = p->hashSizeSum + p->numSons;
  1353. + if (p->hash != 0 && prevSize == newSize)
  1354. + return 1;
  1355. + MatchFinder_FreeThisClassMemory(p, alloc);
  1356. + p->hash = AllocRefs(newSize, alloc);
  1357. + if (p->hash != 0)
  1358. + {
  1359. + p->son = p->hash + p->hashSizeSum;
  1360. + return 1;
  1361. + }
  1362. + }
  1363. + }
  1364. + MatchFinder_Free(p, alloc);
  1365. + return 0;
  1366. +}
  1367. +
  1368. +static void MatchFinder_SetLimits(CMatchFinder *p)
  1369. +{
  1370. + UInt32 limit = kMaxValForNormalize - p->pos;
  1371. + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
  1372. + if (limit2 < limit)
  1373. + limit = limit2;
  1374. + limit2 = p->streamPos - p->pos;
  1375. + if (limit2 <= p->keepSizeAfter)
  1376. + {
  1377. + if (limit2 > 0)
  1378. + limit2 = 1;
  1379. + }
  1380. + else
  1381. + limit2 -= p->keepSizeAfter;
  1382. + if (limit2 < limit)
  1383. + limit = limit2;
  1384. + {
  1385. + UInt32 lenLimit = p->streamPos - p->pos;
  1386. + if (lenLimit > p->matchMaxLen)
  1387. + lenLimit = p->matchMaxLen;
  1388. + p->lenLimit = lenLimit;
  1389. + }
  1390. + p->posLimit = p->pos + limit;
  1391. +}
  1392. +
  1393. +void MatchFinder_Init(CMatchFinder *p)
  1394. +{
  1395. + UInt32 i;
  1396. + for (i = 0; i < p->hashSizeSum; i++)
  1397. + p->hash[i] = kEmptyHashValue;
  1398. + p->cyclicBufferPos = 0;
  1399. + p->buffer = p->bufferBase;
  1400. + p->pos = p->streamPos = p->cyclicBufferSize;
  1401. + p->result = SZ_OK;
  1402. + p->streamEndWasReached = 0;
  1403. + MatchFinder_ReadBlock(p);
  1404. + MatchFinder_SetLimits(p);
  1405. +}
  1406. +
  1407. +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
  1408. +{
  1409. + return (p->pos - p->historySize - 1) & kNormalizeMask;
  1410. +}
  1411. +
  1412. +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
  1413. +{
  1414. + UInt32 i;
  1415. + for (i = 0; i < numItems; i++)
  1416. + {
  1417. + UInt32 value = items[i];
  1418. + if (value <= subValue)
  1419. + value = kEmptyHashValue;
  1420. + else
  1421. + value -= subValue;
  1422. + items[i] = value;
  1423. + }
  1424. +}
  1425. +
  1426. +static void MatchFinder_Normalize(CMatchFinder *p)
  1427. +{
  1428. + UInt32 subValue = MatchFinder_GetSubValue(p);
  1429. + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
  1430. + MatchFinder_ReduceOffsets(p, subValue);
  1431. +}
  1432. +
  1433. +static void MatchFinder_CheckLimits(CMatchFinder *p)
  1434. +{
  1435. + if (p->pos == kMaxValForNormalize)
  1436. + MatchFinder_Normalize(p);
  1437. + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
  1438. + MatchFinder_CheckAndMoveAndRead(p);
  1439. + if (p->cyclicBufferPos == p->cyclicBufferSize)
  1440. + p->cyclicBufferPos = 0;
  1441. + MatchFinder_SetLimits(p);
  1442. +}
  1443. +
  1444. +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  1445. + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  1446. + UInt32 *distances, UInt32 maxLen)
  1447. +{
  1448. + son[_cyclicBufferPos] = curMatch;
  1449. + for (;;)
  1450. + {
  1451. + UInt32 delta = pos - curMatch;
  1452. + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  1453. + return distances;
  1454. + {
  1455. + const Byte *pb = cur - delta;
  1456. + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
  1457. + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
  1458. + {
  1459. + UInt32 len = 0;
  1460. + while (++len != lenLimit)
  1461. + if (pb[len] != cur[len])
  1462. + break;
  1463. + if (maxLen < len)
  1464. + {
  1465. + *distances++ = maxLen = len;
  1466. + *distances++ = delta - 1;
  1467. + if (len == lenLimit)
  1468. + return distances;
  1469. + }
  1470. + }
  1471. + }
  1472. + }
  1473. +}
  1474. +
  1475. +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  1476. + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  1477. + UInt32 *distances, UInt32 maxLen)
  1478. +{
  1479. + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
  1480. + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
  1481. + UInt32 len0 = 0, len1 = 0;
  1482. + for (;;)
  1483. + {
  1484. + UInt32 delta = pos - curMatch;
  1485. + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  1486. + {
  1487. + *ptr0 = *ptr1 = kEmptyHashValue;
  1488. + return distances;
  1489. + }
  1490. + {
  1491. + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  1492. + const Byte *pb = cur - delta;
  1493. + UInt32 len = (len0 < len1 ? len0 : len1);
  1494. + if (pb[len] == cur[len])
  1495. + {
  1496. + if (++len != lenLimit && pb[len] == cur[len])
  1497. + while (++len != lenLimit)
  1498. + if (pb[len] != cur[len])
  1499. + break;
  1500. + if (maxLen < len)
  1501. + {
  1502. + *distances++ = maxLen = len;
  1503. + *distances++ = delta - 1;
  1504. + if (len == lenLimit)
  1505. + {
  1506. + *ptr1 = pair[0];
  1507. + *ptr0 = pair[1];
  1508. + return distances;
  1509. + }
  1510. + }
  1511. + }
  1512. + if (pb[len] < cur[len])
  1513. + {
  1514. + *ptr1 = curMatch;
  1515. + ptr1 = pair + 1;
  1516. + curMatch = *ptr1;
  1517. + len1 = len;
  1518. + }
  1519. + else
  1520. + {
  1521. + *ptr0 = curMatch;
  1522. + ptr0 = pair;
  1523. + curMatch = *ptr0;
  1524. + len0 = len;
  1525. + }
  1526. + }
  1527. + }
  1528. +}
  1529. +
  1530. +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  1531. + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
  1532. +{
  1533. + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
  1534. + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
  1535. + UInt32 len0 = 0, len1 = 0;
  1536. + for (;;)
  1537. + {
  1538. + UInt32 delta = pos - curMatch;
  1539. + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  1540. + {
  1541. + *ptr0 = *ptr1 = kEmptyHashValue;
  1542. + return;
  1543. + }
  1544. + {
  1545. + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  1546. + const Byte *pb = cur - delta;
  1547. + UInt32 len = (len0 < len1 ? len0 : len1);
  1548. + if (pb[len] == cur[len])
  1549. + {
  1550. + while (++len != lenLimit)
  1551. + if (pb[len] != cur[len])
  1552. + break;
  1553. + {
  1554. + if (len == lenLimit)
  1555. + {
  1556. + *ptr1 = pair[0];
  1557. + *ptr0 = pair[1];
  1558. + return;
  1559. + }
  1560. + }
  1561. + }
  1562. + if (pb[len] < cur[len])
  1563. + {
  1564. + *ptr1 = curMatch;
  1565. + ptr1 = pair + 1;
  1566. + curMatch = *ptr1;
  1567. + len1 = len;
  1568. + }
  1569. + else
  1570. + {
  1571. + *ptr0 = curMatch;
  1572. + ptr0 = pair;
  1573. + curMatch = *ptr0;
  1574. + len0 = len;
  1575. + }
  1576. + }
  1577. + }
  1578. +}
  1579. +
  1580. +#define MOVE_POS \
  1581. + ++p->cyclicBufferPos; \
  1582. + p->buffer++; \
  1583. + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
  1584. +
  1585. +#define MOVE_POS_RET MOVE_POS return offset;
  1586. +
  1587. +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
  1588. +
  1589. +#define GET_MATCHES_HEADER2(minLen, ret_op) \
  1590. + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
  1591. + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
  1592. + cur = p->buffer;
  1593. +
  1594. +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
  1595. +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
  1596. +
  1597. +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
  1598. +
  1599. +#define GET_MATCHES_FOOTER(offset, maxLen) \
  1600. + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
  1601. + distances + offset, maxLen) - distances); MOVE_POS_RET;
  1602. +
  1603. +#define SKIP_FOOTER \
  1604. + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
  1605. +
  1606. +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1607. +{
  1608. + UInt32 offset;
  1609. + GET_MATCHES_HEADER(2)
  1610. + HASH2_CALC;
  1611. + curMatch = p->hash[hashValue];
  1612. + p->hash[hashValue] = p->pos;
  1613. + offset = 0;
  1614. + GET_MATCHES_FOOTER(offset, 1)
  1615. +}
  1616. +
  1617. +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1618. +{
  1619. + UInt32 offset;
  1620. + GET_MATCHES_HEADER(3)
  1621. + HASH_ZIP_CALC;
  1622. + curMatch = p->hash[hashValue];
  1623. + p->hash[hashValue] = p->pos;
  1624. + offset = 0;
  1625. + GET_MATCHES_FOOTER(offset, 2)
  1626. +}
  1627. +
  1628. +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1629. +{
  1630. + UInt32 hash2Value, delta2, maxLen, offset;
  1631. + GET_MATCHES_HEADER(3)
  1632. +
  1633. + HASH3_CALC;
  1634. +
  1635. + delta2 = p->pos - p->hash[hash2Value];
  1636. + curMatch = p->hash[kFix3HashSize + hashValue];
  1637. +
  1638. + p->hash[hash2Value] =
  1639. + p->hash[kFix3HashSize + hashValue] = p->pos;
  1640. +
  1641. +
  1642. + maxLen = 2;
  1643. + offset = 0;
  1644. + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  1645. + {
  1646. + for (; maxLen != lenLimit; maxLen++)
  1647. + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  1648. + break;
  1649. + distances[0] = maxLen;
  1650. + distances[1] = delta2 - 1;
  1651. + offset = 2;
  1652. + if (maxLen == lenLimit)
  1653. + {
  1654. + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
  1655. + MOVE_POS_RET;
  1656. + }
  1657. + }
  1658. + GET_MATCHES_FOOTER(offset, maxLen)
  1659. +}
  1660. +
  1661. +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1662. +{
  1663. + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
  1664. + GET_MATCHES_HEADER(4)
  1665. +
  1666. + HASH4_CALC;
  1667. +
  1668. + delta2 = p->pos - p->hash[ hash2Value];
  1669. + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
  1670. + curMatch = p->hash[kFix4HashSize + hashValue];
  1671. +
  1672. + p->hash[ hash2Value] =
  1673. + p->hash[kFix3HashSize + hash3Value] =
  1674. + p->hash[kFix4HashSize + hashValue] = p->pos;
  1675. +
  1676. + maxLen = 1;
  1677. + offset = 0;
  1678. + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  1679. + {
  1680. + distances[0] = maxLen = 2;
  1681. + distances[1] = delta2 - 1;
  1682. + offset = 2;
  1683. + }
  1684. + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
  1685. + {
  1686. + maxLen = 3;
  1687. + distances[offset + 1] = delta3 - 1;
  1688. + offset += 2;
  1689. + delta2 = delta3;
  1690. + }
  1691. + if (offset != 0)
  1692. + {
  1693. + for (; maxLen != lenLimit; maxLen++)
  1694. + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  1695. + break;
  1696. + distances[offset - 2] = maxLen;
  1697. + if (maxLen == lenLimit)
  1698. + {
  1699. + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
  1700. + MOVE_POS_RET;
  1701. + }
  1702. + }
  1703. + if (maxLen < 3)
  1704. + maxLen = 3;
  1705. + GET_MATCHES_FOOTER(offset, maxLen)
  1706. +}
  1707. +
  1708. +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1709. +{
  1710. + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
  1711. + GET_MATCHES_HEADER(4)
  1712. +
  1713. + HASH4_CALC;
  1714. +
  1715. + delta2 = p->pos - p->hash[ hash2Value];
  1716. + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
  1717. + curMatch = p->hash[kFix4HashSize + hashValue];
  1718. +
  1719. + p->hash[ hash2Value] =
  1720. + p->hash[kFix3HashSize + hash3Value] =
  1721. + p->hash[kFix4HashSize + hashValue] = p->pos;
  1722. +
  1723. + maxLen = 1;
  1724. + offset = 0;
  1725. + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  1726. + {
  1727. + distances[0] = maxLen = 2;
  1728. + distances[1] = delta2 - 1;
  1729. + offset = 2;
  1730. + }
  1731. + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
  1732. + {
  1733. + maxLen = 3;
  1734. + distances[offset + 1] = delta3 - 1;
  1735. + offset += 2;
  1736. + delta2 = delta3;
  1737. + }
  1738. + if (offset != 0)
  1739. + {
  1740. + for (; maxLen != lenLimit; maxLen++)
  1741. + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  1742. + break;
  1743. + distances[offset - 2] = maxLen;
  1744. + if (maxLen == lenLimit)
  1745. + {
  1746. + p->son[p->cyclicBufferPos] = curMatch;
  1747. + MOVE_POS_RET;
  1748. + }
  1749. + }
  1750. + if (maxLen < 3)
  1751. + maxLen = 3;
  1752. + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  1753. + distances + offset, maxLen) - (distances));
  1754. + MOVE_POS_RET
  1755. +}
  1756. +
  1757. +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  1758. +{
  1759. + UInt32 offset;
  1760. + GET_MATCHES_HEADER(3)
  1761. + HASH_ZIP_CALC;
  1762. + curMatch = p->hash[hashValue];
  1763. + p->hash[hashValue] = p->pos;
  1764. + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  1765. + distances, 2) - (distances));
  1766. + MOVE_POS_RET
  1767. +}
  1768. +
  1769. +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1770. +{
  1771. + do
  1772. + {
  1773. + SKIP_HEADER(2)
  1774. + HASH2_CALC;
  1775. + curMatch = p->hash[hashValue];
  1776. + p->hash[hashValue] = p->pos;
  1777. + SKIP_FOOTER
  1778. + }
  1779. + while (--num != 0);
  1780. +}
  1781. +
  1782. +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1783. +{
  1784. + do
  1785. + {
  1786. + SKIP_HEADER(3)
  1787. + HASH_ZIP_CALC;
  1788. + curMatch = p->hash[hashValue];
  1789. + p->hash[hashValue] = p->pos;
  1790. + SKIP_FOOTER
  1791. + }
  1792. + while (--num != 0);
  1793. +}
  1794. +
  1795. +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1796. +{
  1797. + do
  1798. + {
  1799. + UInt32 hash2Value;
  1800. + SKIP_HEADER(3)
  1801. + HASH3_CALC;
  1802. + curMatch = p->hash[kFix3HashSize + hashValue];
  1803. + p->hash[hash2Value] =
  1804. + p->hash[kFix3HashSize + hashValue] = p->pos;
  1805. + SKIP_FOOTER
  1806. + }
  1807. + while (--num != 0);
  1808. +}
  1809. +
  1810. +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1811. +{
  1812. + do
  1813. + {
  1814. + UInt32 hash2Value, hash3Value;
  1815. + SKIP_HEADER(4)
  1816. + HASH4_CALC;
  1817. + curMatch = p->hash[kFix4HashSize + hashValue];
  1818. + p->hash[ hash2Value] =
  1819. + p->hash[kFix3HashSize + hash3Value] = p->pos;
  1820. + p->hash[kFix4HashSize + hashValue] = p->pos;
  1821. + SKIP_FOOTER
  1822. + }
  1823. + while (--num != 0);
  1824. +}
  1825. +
  1826. +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1827. +{
  1828. + do
  1829. + {
  1830. + UInt32 hash2Value, hash3Value;
  1831. + SKIP_HEADER(4)
  1832. + HASH4_CALC;
  1833. + curMatch = p->hash[kFix4HashSize + hashValue];
  1834. + p->hash[ hash2Value] =
  1835. + p->hash[kFix3HashSize + hash3Value] =
  1836. + p->hash[kFix4HashSize + hashValue] = p->pos;
  1837. + p->son[p->cyclicBufferPos] = curMatch;
  1838. + MOVE_POS
  1839. + }
  1840. + while (--num != 0);
  1841. +}
  1842. +
  1843. +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  1844. +{
  1845. + do
  1846. + {
  1847. + SKIP_HEADER(3)
  1848. + HASH_ZIP_CALC;
  1849. + curMatch = p->hash[hashValue];
  1850. + p->hash[hashValue] = p->pos;
  1851. + p->son[p->cyclicBufferPos] = curMatch;
  1852. + MOVE_POS
  1853. + }
  1854. + while (--num != 0);
  1855. +}
  1856. +
  1857. +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
  1858. +{
  1859. + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
  1860. + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
  1861. + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
  1862. + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
  1863. + if (!p->btMode)
  1864. + {
  1865. + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
  1866. + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
  1867. + }
  1868. + else if (p->numHashBytes == 2)
  1869. + {
  1870. + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
  1871. + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
  1872. + }
  1873. + else if (p->numHashBytes == 3)
  1874. + {
  1875. + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
  1876. + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
  1877. + }
  1878. + else
  1879. + {
  1880. + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
  1881. + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
  1882. + }
  1883. +}
  1884. --- /dev/null
  1885. +++ b/lib/lzma/LzmaDec.c
  1886. @@ -0,0 +1,999 @@
  1887. +/* LzmaDec.c -- LZMA Decoder
  1888. +2009-09-20 : Igor Pavlov : Public domain */
  1889. +
  1890. +#include "LzmaDec.h"
  1891. +
  1892. +#include <string.h>
  1893. +
  1894. +#define kNumTopBits 24
  1895. +#define kTopValue ((UInt32)1 << kNumTopBits)
  1896. +
  1897. +#define kNumBitModelTotalBits 11
  1898. +#define kBitModelTotal (1 << kNumBitModelTotalBits)
  1899. +#define kNumMoveBits 5
  1900. +
  1901. +#define RC_INIT_SIZE 5
  1902. +
  1903. +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
  1904. +
  1905. +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  1906. +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
  1907. +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
  1908. +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
  1909. + { UPDATE_0(p); i = (i + i); A0; } else \
  1910. + { UPDATE_1(p); i = (i + i) + 1; A1; }
  1911. +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
  1912. +
  1913. +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
  1914. +#define TREE_DECODE(probs, limit, i) \
  1915. + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
  1916. +
  1917. +/* #define _LZMA_SIZE_OPT */
  1918. +
  1919. +#ifdef _LZMA_SIZE_OPT
  1920. +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
  1921. +#else
  1922. +#define TREE_6_DECODE(probs, i) \
  1923. + { i = 1; \
  1924. + TREE_GET_BIT(probs, i); \
  1925. + TREE_GET_BIT(probs, i); \
  1926. + TREE_GET_BIT(probs, i); \
  1927. + TREE_GET_BIT(probs, i); \
  1928. + TREE_GET_BIT(probs, i); \
  1929. + TREE_GET_BIT(probs, i); \
  1930. + i -= 0x40; }
  1931. +#endif
  1932. +
  1933. +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
  1934. +
  1935. +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  1936. +#define UPDATE_0_CHECK range = bound;
  1937. +#define UPDATE_1_CHECK range -= bound; code -= bound;
  1938. +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
  1939. + { UPDATE_0_CHECK; i = (i + i); A0; } else \
  1940. + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
  1941. +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
  1942. +#define TREE_DECODE_CHECK(probs, limit, i) \
  1943. + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
  1944. +
  1945. +
  1946. +#define kNumPosBitsMax 4
  1947. +#define kNumPosStatesMax (1 << kNumPosBitsMax)
  1948. +
  1949. +#define kLenNumLowBits 3
  1950. +#define kLenNumLowSymbols (1 << kLenNumLowBits)
  1951. +#define kLenNumMidBits 3
  1952. +#define kLenNumMidSymbols (1 << kLenNumMidBits)
  1953. +#define kLenNumHighBits 8
  1954. +#define kLenNumHighSymbols (1 << kLenNumHighBits)
  1955. +
  1956. +#define LenChoice 0
  1957. +#define LenChoice2 (LenChoice + 1)
  1958. +#define LenLow (LenChoice2 + 1)
  1959. +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  1960. +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  1961. +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  1962. +
  1963. +
  1964. +#define kNumStates 12
  1965. +#define kNumLitStates 7
  1966. +
  1967. +#define kStartPosModelIndex 4
  1968. +#define kEndPosModelIndex 14
  1969. +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  1970. +
  1971. +#define kNumPosSlotBits 6
  1972. +#define kNumLenToPosStates 4
  1973. +
  1974. +#define kNumAlignBits 4
  1975. +#define kAlignTableSize (1 << kNumAlignBits)
  1976. +
  1977. +#define kMatchMinLen 2
  1978. +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  1979. +
  1980. +#define IsMatch 0
  1981. +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  1982. +#define IsRepG0 (IsRep + kNumStates)
  1983. +#define IsRepG1 (IsRepG0 + kNumStates)
  1984. +#define IsRepG2 (IsRepG1 + kNumStates)
  1985. +#define IsRep0Long (IsRepG2 + kNumStates)
  1986. +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  1987. +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  1988. +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  1989. +#define LenCoder (Align + kAlignTableSize)
  1990. +#define RepLenCoder (LenCoder + kNumLenProbs)
  1991. +#define Literal (RepLenCoder + kNumLenProbs)
  1992. +
  1993. +#define LZMA_BASE_SIZE 1846
  1994. +#define LZMA_LIT_SIZE 768
  1995. +
  1996. +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
  1997. +
  1998. +#if Literal != LZMA_BASE_SIZE
  1999. +StopCompilingDueBUG
  2000. +#endif
  2001. +
  2002. +#define LZMA_DIC_MIN (1 << 12)
  2003. +
  2004. +/* First LZMA-symbol is always decoded.
  2005. +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
  2006. +Out:
  2007. + Result:
  2008. + SZ_OK - OK
  2009. + SZ_ERROR_DATA - Error
  2010. + p->remainLen:
  2011. + < kMatchSpecLenStart : normal remain
  2012. + = kMatchSpecLenStart : finished
  2013. + = kMatchSpecLenStart + 1 : Flush marker
  2014. + = kMatchSpecLenStart + 2 : State Init Marker
  2015. +*/
  2016. +
  2017. +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  2018. +{
  2019. + CLzmaProb *probs = p->probs;
  2020. +
  2021. + unsigned state = p->state;
  2022. + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
  2023. + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
  2024. + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
  2025. + unsigned lc = p->prop.lc;
  2026. +
  2027. + Byte *dic = p->dic;
  2028. + SizeT dicBufSize = p->dicBufSize;
  2029. + SizeT dicPos = p->dicPos;
  2030. +
  2031. + UInt32 processedPos = p->processedPos;
  2032. + UInt32 checkDicSize = p->checkDicSize;
  2033. + unsigned len = 0;
  2034. +
  2035. + const Byte *buf = p->buf;
  2036. + UInt32 range = p->range;
  2037. + UInt32 code = p->code;
  2038. +
  2039. + do
  2040. + {
  2041. + CLzmaProb *prob;
  2042. + UInt32 bound;
  2043. + unsigned ttt;
  2044. + unsigned posState = processedPos & pbMask;
  2045. +
  2046. + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  2047. + IF_BIT_0(prob)
  2048. + {
  2049. + unsigned symbol;
  2050. + UPDATE_0(prob);
  2051. + prob = probs + Literal;
  2052. + if (checkDicSize != 0 || processedPos != 0)
  2053. + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
  2054. + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
  2055. +
  2056. + if (state < kNumLitStates)
  2057. + {
  2058. + state -= (state < 4) ? state : 3;
  2059. + symbol = 1;
  2060. + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
  2061. + }
  2062. + else
  2063. + {
  2064. + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  2065. + unsigned offs = 0x100;
  2066. + state -= (state < 10) ? 3 : 6;
  2067. + symbol = 1;
  2068. + do
  2069. + {
  2070. + unsigned bit;
  2071. + CLzmaProb *probLit;
  2072. + matchByte <<= 1;
  2073. + bit = (matchByte & offs);
  2074. + probLit = prob + offs + bit + symbol;
  2075. + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
  2076. + }
  2077. + while (symbol < 0x100);
  2078. + }
  2079. + dic[dicPos++] = (Byte)symbol;
  2080. + processedPos++;
  2081. + continue;
  2082. + }
  2083. + else
  2084. + {
  2085. + UPDATE_1(prob);
  2086. + prob = probs + IsRep + state;
  2087. + IF_BIT_0(prob)
  2088. + {
  2089. + UPDATE_0(prob);
  2090. + state += kNumStates;
  2091. + prob = probs + LenCoder;
  2092. + }
  2093. + else
  2094. + {
  2095. + UPDATE_1(prob);
  2096. + if (checkDicSize == 0 && processedPos == 0)
  2097. + return SZ_ERROR_DATA;
  2098. + prob = probs + IsRepG0 + state;
  2099. + IF_BIT_0(prob)
  2100. + {
  2101. + UPDATE_0(prob);
  2102. + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  2103. + IF_BIT_0(prob)
  2104. + {
  2105. + UPDATE_0(prob);
  2106. + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  2107. + dicPos++;
  2108. + processedPos++;
  2109. + state = state < kNumLitStates ? 9 : 11;
  2110. + continue;
  2111. + }
  2112. + UPDATE_1(prob);
  2113. + }
  2114. + else
  2115. + {
  2116. + UInt32 distance;
  2117. + UPDATE_1(prob);
  2118. + prob = probs + IsRepG1 + state;
  2119. + IF_BIT_0(prob)
  2120. + {
  2121. + UPDATE_0(prob);
  2122. + distance = rep1;
  2123. + }
  2124. + else
  2125. + {
  2126. + UPDATE_1(prob);
  2127. + prob = probs + IsRepG2 + state;
  2128. + IF_BIT_0(prob)
  2129. + {
  2130. + UPDATE_0(prob);
  2131. + distance = rep2;
  2132. + }
  2133. + else
  2134. + {
  2135. + UPDATE_1(prob);
  2136. + distance = rep3;
  2137. + rep3 = rep2;
  2138. + }
  2139. + rep2 = rep1;
  2140. + }
  2141. + rep1 = rep0;
  2142. + rep0 = distance;
  2143. + }
  2144. + state = state < kNumLitStates ? 8 : 11;
  2145. + prob = probs + RepLenCoder;
  2146. + }
  2147. + {
  2148. + unsigned limit, offset;
  2149. + CLzmaProb *probLen = prob + LenChoice;
  2150. + IF_BIT_0(probLen)
  2151. + {
  2152. + UPDATE_0(probLen);
  2153. + probLen = prob + LenLow + (posState << kLenNumLowBits);
  2154. + offset = 0;
  2155. + limit = (1 << kLenNumLowBits);
  2156. + }
  2157. + else
  2158. + {
  2159. + UPDATE_1(probLen);
  2160. + probLen = prob + LenChoice2;
  2161. + IF_BIT_0(probLen)
  2162. + {
  2163. + UPDATE_0(probLen);
  2164. + probLen = prob + LenMid + (posState << kLenNumMidBits);
  2165. + offset = kLenNumLowSymbols;
  2166. + limit = (1 << kLenNumMidBits);
  2167. + }
  2168. + else
  2169. + {
  2170. + UPDATE_1(probLen);
  2171. + probLen = prob + LenHigh;
  2172. + offset = kLenNumLowSymbols + kLenNumMidSymbols;
  2173. + limit = (1 << kLenNumHighBits);
  2174. + }
  2175. + }
  2176. + TREE_DECODE(probLen, limit, len);
  2177. + len += offset;
  2178. + }
  2179. +
  2180. + if (state >= kNumStates)
  2181. + {
  2182. + UInt32 distance;
  2183. + prob = probs + PosSlot +
  2184. + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
  2185. + TREE_6_DECODE(prob, distance);
  2186. + if (distance >= kStartPosModelIndex)
  2187. + {
  2188. + unsigned posSlot = (unsigned)distance;
  2189. + int numDirectBits = (int)(((distance >> 1) - 1));
  2190. + distance = (2 | (distance & 1));
  2191. + if (posSlot < kEndPosModelIndex)
  2192. + {
  2193. + distance <<= numDirectBits;
  2194. + prob = probs + SpecPos + distance - posSlot - 1;
  2195. + {
  2196. + UInt32 mask = 1;
  2197. + unsigned i = 1;
  2198. + do
  2199. + {
  2200. + GET_BIT2(prob + i, i, ; , distance |= mask);
  2201. + mask <<= 1;
  2202. + }
  2203. + while (--numDirectBits != 0);
  2204. + }
  2205. + }
  2206. + else
  2207. + {
  2208. + numDirectBits -= kNumAlignBits;
  2209. + do
  2210. + {
  2211. + NORMALIZE
  2212. + range >>= 1;
  2213. +
  2214. + {
  2215. + UInt32 t;
  2216. + code -= range;
  2217. + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
  2218. + distance = (distance << 1) + (t + 1);
  2219. + code += range & t;
  2220. + }
  2221. + /*
  2222. + distance <<= 1;
  2223. + if (code >= range)
  2224. + {
  2225. + code -= range;
  2226. + distance |= 1;
  2227. + }
  2228. + */
  2229. + }
  2230. + while (--numDirectBits != 0);
  2231. + prob = probs + Align;
  2232. + distance <<= kNumAlignBits;
  2233. + {
  2234. + unsigned i = 1;
  2235. + GET_BIT2(prob + i, i, ; , distance |= 1);
  2236. + GET_BIT2(prob + i, i, ; , distance |= 2);
  2237. + GET_BIT2(prob + i, i, ; , distance |= 4);
  2238. + GET_BIT2(prob + i, i, ; , distance |= 8);
  2239. + }
  2240. + if (distance == (UInt32)0xFFFFFFFF)
  2241. + {
  2242. + len += kMatchSpecLenStart;
  2243. + state -= kNumStates;
  2244. + break;
  2245. + }
  2246. + }
  2247. + }
  2248. + rep3 = rep2;
  2249. + rep2 = rep1;
  2250. + rep1 = rep0;
  2251. + rep0 = distance + 1;
  2252. + if (checkDicSize == 0)
  2253. + {
  2254. + if (distance >= processedPos)
  2255. + return SZ_ERROR_DATA;
  2256. + }
  2257. + else if (distance >= checkDicSize)
  2258. + return SZ_ERROR_DATA;
  2259. + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
  2260. + }
  2261. +
  2262. + len += kMatchMinLen;
  2263. +
  2264. + if (limit == dicPos)
  2265. + return SZ_ERROR_DATA;
  2266. + {
  2267. + SizeT rem = limit - dicPos;
  2268. + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
  2269. + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
  2270. +
  2271. + processedPos += curLen;
  2272. +
  2273. + len -= curLen;
  2274. + if (pos + curLen <= dicBufSize)
  2275. + {
  2276. + Byte *dest = dic + dicPos;
  2277. + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
  2278. + const Byte *lim = dest + curLen;
  2279. + dicPos += curLen;
  2280. + do
  2281. + *(dest) = (Byte)*(dest + src);
  2282. + while (++dest != lim);
  2283. + }
  2284. + else
  2285. + {
  2286. + do
  2287. + {
  2288. + dic[dicPos++] = dic[pos];
  2289. + if (++pos == dicBufSize)
  2290. + pos = 0;
  2291. + }
  2292. + while (--curLen != 0);
  2293. + }
  2294. + }
  2295. + }
  2296. + }
  2297. + while (dicPos < limit && buf < bufLimit);
  2298. + NORMALIZE;
  2299. + p->buf = buf;
  2300. + p->range = range;
  2301. + p->code = code;
  2302. + p->remainLen = len;
  2303. + p->dicPos = dicPos;
  2304. + p->processedPos = processedPos;
  2305. + p->reps[0] = rep0;
  2306. + p->reps[1] = rep1;
  2307. + p->reps[2] = rep2;
  2308. + p->reps[3] = rep3;
  2309. + p->state = state;
  2310. +
  2311. + return SZ_OK;
  2312. +}
  2313. +
  2314. +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
  2315. +{
  2316. + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
  2317. + {
  2318. + Byte *dic = p->dic;
  2319. + SizeT dicPos = p->dicPos;
  2320. + SizeT dicBufSize = p->dicBufSize;
  2321. + unsigned len = p->remainLen;
  2322. + UInt32 rep0 = p->reps[0];
  2323. + if (limit - dicPos < len)
  2324. + len = (unsigned)(limit - dicPos);
  2325. +
  2326. + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
  2327. + p->checkDicSize = p->prop.dicSize;
  2328. +
  2329. + p->processedPos += len;
  2330. + p->remainLen -= len;
  2331. + while (len-- != 0)
  2332. + {
  2333. + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  2334. + dicPos++;
  2335. + }
  2336. + p->dicPos = dicPos;
  2337. + }
  2338. +}
  2339. +
  2340. +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  2341. +{
  2342. + do
  2343. + {
  2344. + SizeT limit2 = limit;
  2345. + if (p->checkDicSize == 0)
  2346. + {
  2347. + UInt32 rem = p->prop.dicSize - p->processedPos;
  2348. + if (limit - p->dicPos > rem)
  2349. + limit2 = p->dicPos + rem;
  2350. + }
  2351. + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
  2352. + if (p->processedPos >= p->prop.dicSize)
  2353. + p->checkDicSize = p->prop.dicSize;
  2354. + LzmaDec_WriteRem(p, limit);
  2355. + }
  2356. + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
  2357. +
  2358. + if (p->remainLen > kMatchSpecLenStart)
  2359. + {
  2360. + p->remainLen = kMatchSpecLenStart;
  2361. + }
  2362. + return 0;
  2363. +}
  2364. +
  2365. +typedef enum
  2366. +{
  2367. + DUMMY_ERROR, /* unexpected end of input stream */
  2368. + DUMMY_LIT,
  2369. + DUMMY_MATCH,
  2370. + DUMMY_REP
  2371. +} ELzmaDummy;
  2372. +
  2373. +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
  2374. +{
  2375. + UInt32 range = p->range;
  2376. + UInt32 code = p->code;
  2377. + const Byte *bufLimit = buf + inSize;
  2378. + CLzmaProb *probs = p->probs;
  2379. + unsigned state = p->state;
  2380. + ELzmaDummy res;
  2381. +
  2382. + {
  2383. + CLzmaProb *prob;
  2384. + UInt32 bound;
  2385. + unsigned ttt;
  2386. + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
  2387. +
  2388. + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  2389. + IF_BIT_0_CHECK(prob)
  2390. + {
  2391. + UPDATE_0_CHECK
  2392. +
  2393. + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
  2394. +
  2395. + prob = probs + Literal;
  2396. + if (p->checkDicSize != 0 || p->processedPos != 0)
  2397. + prob += (LZMA_LIT_SIZE *
  2398. + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
  2399. + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
  2400. +
  2401. + if (state < kNumLitStates)
  2402. + {
  2403. + unsigned symbol = 1;
  2404. + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
  2405. + }
  2406. + else
  2407. + {
  2408. + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
  2409. + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
  2410. + unsigned offs = 0x100;
  2411. + unsigned symbol = 1;
  2412. + do
  2413. + {
  2414. + unsigned bit;
  2415. + CLzmaProb *probLit;
  2416. + matchByte <<= 1;
  2417. + bit = (matchByte & offs);
  2418. + probLit = prob + offs + bit + symbol;
  2419. + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
  2420. + }
  2421. + while (symbol < 0x100);
  2422. + }
  2423. + res = DUMMY_LIT;
  2424. + }
  2425. + else
  2426. + {
  2427. + unsigned len;
  2428. + UPDATE_1_CHECK;
  2429. +
  2430. + prob = probs + IsRep + state;
  2431. + IF_BIT_0_CHECK(prob)
  2432. + {
  2433. + UPDATE_0_CHECK;
  2434. + state = 0;
  2435. + prob = probs + LenCoder;
  2436. + res = DUMMY_MATCH;
  2437. + }
  2438. + else
  2439. + {
  2440. + UPDATE_1_CHECK;
  2441. + res = DUMMY_REP;
  2442. + prob = probs + IsRepG0 + state;
  2443. + IF_BIT_0_CHECK(prob)
  2444. + {
  2445. + UPDATE_0_CHECK;
  2446. + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  2447. + IF_BIT_0_CHECK(prob)
  2448. + {
  2449. + UPDATE_0_CHECK;
  2450. + NORMALIZE_CHECK;
  2451. + return DUMMY_REP;
  2452. + }
  2453. + else
  2454. + {
  2455. + UPDATE_1_CHECK;
  2456. + }
  2457. + }
  2458. + else
  2459. + {
  2460. + UPDATE_1_CHECK;
  2461. + prob = probs + IsRepG1 + state;
  2462. + IF_BIT_0_CHECK(prob)
  2463. + {
  2464. + UPDATE_0_CHECK;
  2465. + }
  2466. + else
  2467. + {
  2468. + UPDATE_1_CHECK;
  2469. + prob = probs + IsRepG2 + state;
  2470. + IF_BIT_0_CHECK(prob)
  2471. + {
  2472. + UPDATE_0_CHECK;
  2473. + }
  2474. + else
  2475. + {
  2476. + UPDATE_1_CHECK;
  2477. + }
  2478. + }
  2479. + }
  2480. + state = kNumStates;
  2481. + prob = probs + RepLenCoder;
  2482. + }
  2483. + {
  2484. + unsigned limit, offset;
  2485. + CLzmaProb *probLen = prob + LenChoice;
  2486. + IF_BIT_0_CHECK(probLen)
  2487. + {
  2488. + UPDATE_0_CHECK;
  2489. + probLen = prob + LenLow + (posState << kLenNumLowBits);
  2490. + offset = 0;
  2491. + limit = 1 << kLenNumLowBits;
  2492. + }
  2493. + else
  2494. + {
  2495. + UPDATE_1_CHECK;
  2496. + probLen = prob + LenChoice2;
  2497. + IF_BIT_0_CHECK(probLen)
  2498. + {
  2499. + UPDATE_0_CHECK;
  2500. + probLen = prob + LenMid + (posState << kLenNumMidBits);
  2501. + offset = kLenNumLowSymbols;
  2502. + limit = 1 << kLenNumMidBits;
  2503. + }
  2504. + else
  2505. + {
  2506. + UPDATE_1_CHECK;
  2507. + probLen = prob + LenHigh;
  2508. + offset = kLenNumLowSymbols + kLenNumMidSymbols;
  2509. + limit = 1 << kLenNumHighBits;
  2510. + }
  2511. + }
  2512. + TREE_DECODE_CHECK(probLen, limit, len);
  2513. + len += offset;
  2514. + }
  2515. +
  2516. + if (state < 4)
  2517. + {
  2518. + unsigned posSlot;
  2519. + prob = probs + PosSlot +
  2520. + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  2521. + kNumPosSlotBits);
  2522. + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
  2523. + if (posSlot >= kStartPosModelIndex)
  2524. + {
  2525. + int numDirectBits = ((posSlot >> 1) - 1);
  2526. +
  2527. + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
  2528. +
  2529. + if (posSlot < kEndPosModelIndex)
  2530. + {
  2531. + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
  2532. + }
  2533. + else
  2534. + {
  2535. + numDirectBits -= kNumAlignBits;
  2536. + do
  2537. + {
  2538. + NORMALIZE_CHECK
  2539. + range >>= 1;
  2540. + code -= range & (((code - range) >> 31) - 1);
  2541. + /* if (code >= range) code -= range; */
  2542. + }
  2543. + while (--numDirectBits != 0);
  2544. + prob = probs + Align;
  2545. + numDirectBits = kNumAlignBits;
  2546. + }
  2547. + {
  2548. + unsigned i = 1;
  2549. + do
  2550. + {
  2551. + GET_BIT_CHECK(prob + i, i);
  2552. + }
  2553. + while (--numDirectBits != 0);
  2554. + }
  2555. + }
  2556. + }
  2557. + }
  2558. + }
  2559. + NORMALIZE_CHECK;
  2560. + return res;
  2561. +}
  2562. +
  2563. +
  2564. +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
  2565. +{
  2566. + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
  2567. + p->range = 0xFFFFFFFF;
  2568. + p->needFlush = 0;
  2569. +}
  2570. +
  2571. +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
  2572. +{
  2573. + p->needFlush = 1;
  2574. + p->remainLen = 0;
  2575. + p->tempBufSize = 0;
  2576. +
  2577. + if (initDic)
  2578. + {
  2579. + p->processedPos = 0;
  2580. + p->checkDicSize = 0;
  2581. + p->needInitState = 1;
  2582. + }
  2583. + if (initState)
  2584. + p->needInitState = 1;
  2585. +}
  2586. +
  2587. +void LzmaDec_Init(CLzmaDec *p)
  2588. +{
  2589. + p->dicPos = 0;
  2590. + LzmaDec_InitDicAndState(p, True, True);
  2591. +}
  2592. +
  2593. +static void LzmaDec_InitStateReal(CLzmaDec *p)
  2594. +{
  2595. + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
  2596. + UInt32 i;
  2597. + CLzmaProb *probs = p->probs;
  2598. + for (i = 0; i < numProbs; i++)
  2599. + probs[i] = kBitModelTotal >> 1;
  2600. + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
  2601. + p->state = 0;
  2602. + p->needInitState = 0;
  2603. +}
  2604. +
  2605. +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
  2606. + ELzmaFinishMode finishMode, ELzmaStatus *status)
  2607. +{
  2608. + SizeT inSize = *srcLen;
  2609. + (*srcLen) = 0;
  2610. + LzmaDec_WriteRem(p, dicLimit);
  2611. +
  2612. + *status = LZMA_STATUS_NOT_SPECIFIED;
  2613. +
  2614. + while (p->remainLen != kMatchSpecLenStart)
  2615. + {
  2616. + int checkEndMarkNow;
  2617. +
  2618. + if (p->needFlush != 0)
  2619. + {
  2620. + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
  2621. + p->tempBuf[p->tempBufSize++] = *src++;
  2622. + if (p->tempBufSize < RC_INIT_SIZE)
  2623. + {
  2624. + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  2625. + return SZ_OK;
  2626. + }
  2627. + if (p->tempBuf[0] != 0)
  2628. + return SZ_ERROR_DATA;
  2629. +
  2630. + LzmaDec_InitRc(p, p->tempBuf);
  2631. + p->tempBufSize = 0;
  2632. + }
  2633. +
  2634. + checkEndMarkNow = 0;
  2635. + if (p->dicPos >= dicLimit)
  2636. + {
  2637. + if (p->remainLen == 0 && p->code == 0)
  2638. + {
  2639. + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
  2640. + return SZ_OK;
  2641. + }
  2642. + if (finishMode == LZMA_FINISH_ANY)
  2643. + {
  2644. + *status = LZMA_STATUS_NOT_FINISHED;
  2645. + return SZ_OK;
  2646. + }
  2647. + if (p->remainLen != 0)
  2648. + {
  2649. + *status = LZMA_STATUS_NOT_FINISHED;
  2650. + return SZ_ERROR_DATA;
  2651. + }
  2652. + checkEndMarkNow = 1;
  2653. + }
  2654. +
  2655. + if (p->needInitState)
  2656. + LzmaDec_InitStateReal(p);
  2657. +
  2658. + if (p->tempBufSize == 0)
  2659. + {
  2660. + SizeT processed;
  2661. + const Byte *bufLimit;
  2662. + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  2663. + {
  2664. + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
  2665. + if (dummyRes == DUMMY_ERROR)
  2666. + {
  2667. + memcpy(p->tempBuf, src, inSize);
  2668. + p->tempBufSize = (unsigned)inSize;
  2669. + (*srcLen) += inSize;
  2670. + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  2671. + return SZ_OK;
  2672. + }
  2673. + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  2674. + {
  2675. + *status = LZMA_STATUS_NOT_FINISHED;
  2676. + return SZ_ERROR_DATA;
  2677. + }
  2678. + bufLimit = src;
  2679. + }
  2680. + else
  2681. + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
  2682. + p->buf = src;
  2683. + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
  2684. + return SZ_ERROR_DATA;
  2685. + processed = (SizeT)(p->buf - src);
  2686. + (*srcLen) += processed;
  2687. + src += processed;
  2688. + inSize -= processed;
  2689. + }
  2690. + else
  2691. + {
  2692. + unsigned rem = p->tempBufSize, lookAhead = 0;
  2693. + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
  2694. + p->tempBuf[rem++] = src[lookAhead++];
  2695. + p->tempBufSize = rem;
  2696. + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  2697. + {
  2698. + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
  2699. + if (dummyRes == DUMMY_ERROR)
  2700. + {
  2701. + (*srcLen) += lookAhead;
  2702. + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  2703. + return SZ_OK;
  2704. + }
  2705. + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  2706. + {
  2707. + *status = LZMA_STATUS_NOT_FINISHED;
  2708. + return SZ_ERROR_DATA;
  2709. + }
  2710. + }
  2711. + p->buf = p->tempBuf;
  2712. + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
  2713. + return SZ_ERROR_DATA;
  2714. + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
  2715. + (*srcLen) += lookAhead;
  2716. + src += lookAhead;
  2717. + inSize -= lookAhead;
  2718. + p->tempBufSize = 0;
  2719. + }
  2720. + }
  2721. + if (p->code == 0)
  2722. + *status = LZMA_STATUS_FINISHED_WITH_MARK;
  2723. + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
  2724. +}
  2725. +
  2726. +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  2727. +{
  2728. + SizeT outSize = *destLen;
  2729. + SizeT inSize = *srcLen;
  2730. + *srcLen = *destLen = 0;
  2731. + for (;;)
  2732. + {
  2733. + SizeT inSizeCur = inSize, outSizeCur, dicPos;
  2734. + ELzmaFinishMode curFinishMode;
  2735. + SRes res;
  2736. + if (p->dicPos == p->dicBufSize)
  2737. + p->dicPos = 0;
  2738. + dicPos = p->dicPos;
  2739. + if (outSize > p->dicBufSize - dicPos)
  2740. + {
  2741. + outSizeCur = p->dicBufSize;
  2742. + curFinishMode = LZMA_FINISH_ANY;
  2743. + }
  2744. + else
  2745. + {
  2746. + outSizeCur = dicPos + outSize;
  2747. + curFinishMode = finishMode;
  2748. + }
  2749. +
  2750. + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
  2751. + src += inSizeCur;
  2752. + inSize -= inSizeCur;
  2753. + *srcLen += inSizeCur;
  2754. + outSizeCur = p->dicPos - dicPos;
  2755. + memcpy(dest, p->dic + dicPos, outSizeCur);
  2756. + dest += outSizeCur;
  2757. + outSize -= outSizeCur;
  2758. + *destLen += outSizeCur;
  2759. + if (res != 0)
  2760. + return res;
  2761. + if (outSizeCur == 0 || outSize == 0)
  2762. + return SZ_OK;
  2763. + }
  2764. +}
  2765. +
  2766. +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
  2767. +{
  2768. + alloc->Free(alloc, p->probs);
  2769. + p->probs = 0;
  2770. +}
  2771. +
  2772. +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
  2773. +{
  2774. + alloc->Free(alloc, p->dic);
  2775. + p->dic = 0;
  2776. +}
  2777. +
  2778. +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
  2779. +{
  2780. + LzmaDec_FreeProbs(p, alloc);
  2781. + LzmaDec_FreeDict(p, alloc);
  2782. +}
  2783. +
  2784. +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
  2785. +{
  2786. + UInt32 dicSize;
  2787. + Byte d;
  2788. +
  2789. + if (size < LZMA_PROPS_SIZE)
  2790. + return SZ_ERROR_UNSUPPORTED;
  2791. + else
  2792. + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
  2793. +
  2794. + if (dicSize < LZMA_DIC_MIN)
  2795. + dicSize = LZMA_DIC_MIN;
  2796. + p->dicSize = dicSize;
  2797. +
  2798. + d = data[0];
  2799. + if (d >= (9 * 5 * 5))
  2800. + return SZ_ERROR_UNSUPPORTED;
  2801. +
  2802. + p->lc = d % 9;
  2803. + d /= 9;
  2804. + p->pb = d / 5;
  2805. + p->lp = d % 5;
  2806. +
  2807. + return SZ_OK;
  2808. +}
  2809. +
  2810. +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
  2811. +{
  2812. + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
  2813. + if (p->probs == 0 || numProbs != p->numProbs)
  2814. + {
  2815. + LzmaDec_FreeProbs(p, alloc);
  2816. + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
  2817. + p->numProbs = numProbs;
  2818. + if (p->probs == 0)
  2819. + return SZ_ERROR_MEM;
  2820. + }
  2821. + return SZ_OK;
  2822. +}
  2823. +
  2824. +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
  2825. +{
  2826. + CLzmaProps propNew;
  2827. + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  2828. + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  2829. + p->prop = propNew;
  2830. + return SZ_OK;
  2831. +}
  2832. +
  2833. +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
  2834. +{
  2835. + CLzmaProps propNew;
  2836. + SizeT dicBufSize;
  2837. + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  2838. + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  2839. + dicBufSize = propNew.dicSize;
  2840. + if (p->dic == 0 || dicBufSize != p->dicBufSize)
  2841. + {
  2842. + LzmaDec_FreeDict(p, alloc);
  2843. + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
  2844. + if (p->dic == 0)
  2845. + {
  2846. + LzmaDec_FreeProbs(p, alloc);
  2847. + return SZ_ERROR_MEM;
  2848. + }
  2849. + }
  2850. + p->dicBufSize = dicBufSize;
  2851. + p->prop = propNew;
  2852. + return SZ_OK;
  2853. +}
  2854. +
  2855. +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  2856. + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
  2857. + ELzmaStatus *status, ISzAlloc *alloc)
  2858. +{
  2859. + CLzmaDec p;
  2860. + SRes res;
  2861. + SizeT inSize = *srcLen;
  2862. + SizeT outSize = *destLen;
  2863. + *srcLen = *destLen = 0;
  2864. + if (inSize < RC_INIT_SIZE)
  2865. + return SZ_ERROR_INPUT_EOF;
  2866. +
  2867. + LzmaDec_Construct(&p);
  2868. + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
  2869. + if (res != 0)
  2870. + return res;
  2871. + p.dic = dest;
  2872. + p.dicBufSize = outSize;
  2873. +
  2874. + LzmaDec_Init(&p);
  2875. +
  2876. + *srcLen = inSize;
  2877. + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
  2878. +
  2879. + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
  2880. + res = SZ_ERROR_INPUT_EOF;
  2881. +
  2882. + (*destLen) = p.dicPos;
  2883. + LzmaDec_FreeProbs(&p, alloc);
  2884. + return res;
  2885. +}
  2886. --- /dev/null
  2887. +++ b/lib/lzma/LzmaEnc.c
  2888. @@ -0,0 +1,2271 @@
  2889. +/* LzmaEnc.c -- LZMA Encoder
  2890. +2009-11-24 : Igor Pavlov : Public domain */
  2891. +
  2892. +#include <string.h>
  2893. +
  2894. +/* #define SHOW_STAT */
  2895. +/* #define SHOW_STAT2 */
  2896. +
  2897. +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
  2898. +#include <stdio.h>
  2899. +#endif
  2900. +
  2901. +#include "LzmaEnc.h"
  2902. +
  2903. +/* disable MT */
  2904. +#define _7ZIP_ST
  2905. +
  2906. +#include "LzFind.h"
  2907. +#ifndef _7ZIP_ST
  2908. +#include "LzFindMt.h"
  2909. +#endif
  2910. +
  2911. +#ifdef SHOW_STAT
  2912. +static int ttt = 0;
  2913. +#endif
  2914. +
  2915. +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
  2916. +
  2917. +#define kBlockSize (9 << 10)
  2918. +#define kUnpackBlockSize (1 << 18)
  2919. +#define kMatchArraySize (1 << 21)
  2920. +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
  2921. +
  2922. +#define kNumMaxDirectBits (31)
  2923. +
  2924. +#define kNumTopBits 24
  2925. +#define kTopValue ((UInt32)1 << kNumTopBits)
  2926. +
  2927. +#define kNumBitModelTotalBits 11
  2928. +#define kBitModelTotal (1 << kNumBitModelTotalBits)
  2929. +#define kNumMoveBits 5
  2930. +#define kProbInitValue (kBitModelTotal >> 1)
  2931. +
  2932. +#define kNumMoveReducingBits 4
  2933. +#define kNumBitPriceShiftBits 4
  2934. +#define kBitPrice (1 << kNumBitPriceShiftBits)
  2935. +
  2936. +void LzmaEncProps_Init(CLzmaEncProps *p)
  2937. +{
  2938. + p->level = 5;
  2939. + p->dictSize = p->mc = 0;
  2940. + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
  2941. + p->writeEndMark = 0;
  2942. +}
  2943. +
  2944. +void LzmaEncProps_Normalize(CLzmaEncProps *p)
  2945. +{
  2946. + int level = p->level;
  2947. + if (level < 0) level = 5;
  2948. + p->level = level;
  2949. + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
  2950. + if (p->lc < 0) p->lc = 3;
  2951. + if (p->lp < 0) p->lp = 0;
  2952. + if (p->pb < 0) p->pb = 2;
  2953. + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
  2954. + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
  2955. + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
  2956. + if (p->numHashBytes < 0) p->numHashBytes = 4;
  2957. + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
  2958. + if (p->numThreads < 0)
  2959. + p->numThreads =
  2960. + #ifndef _7ZIP_ST
  2961. + ((p->btMode && p->algo) ? 2 : 1);
  2962. + #else
  2963. + 1;
  2964. + #endif
  2965. +}
  2966. +
  2967. +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
  2968. +{
  2969. + CLzmaEncProps props = *props2;
  2970. + LzmaEncProps_Normalize(&props);
  2971. + return props.dictSize;
  2972. +}
  2973. +
  2974. +/* #define LZMA_LOG_BSR */
  2975. +/* Define it for Intel's CPU */
  2976. +
  2977. +
  2978. +#ifdef LZMA_LOG_BSR
  2979. +
  2980. +#define kDicLogSizeMaxCompress 30
  2981. +
  2982. +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
  2983. +
  2984. +UInt32 GetPosSlot1(UInt32 pos)
  2985. +{
  2986. + UInt32 res;
  2987. + BSR2_RET(pos, res);
  2988. + return res;
  2989. +}
  2990. +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  2991. +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
  2992. +
  2993. +#else
  2994. +
  2995. +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
  2996. +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
  2997. +
  2998. +void LzmaEnc_FastPosInit(Byte *g_FastPos)
  2999. +{
  3000. + int c = 2, slotFast;
  3001. + g_FastPos[0] = 0;
  3002. + g_FastPos[1] = 1;
  3003. +
  3004. + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
  3005. + {
  3006. + UInt32 k = (1 << ((slotFast >> 1) - 1));
  3007. + UInt32 j;
  3008. + for (j = 0; j < k; j++, c++)
  3009. + g_FastPos[c] = (Byte)slotFast;
  3010. + }
  3011. +}
  3012. +
  3013. +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
  3014. + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
  3015. + res = p->g_FastPos[pos >> i] + (i * 2); }
  3016. +/*
  3017. +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
  3018. + p->g_FastPos[pos >> 6] + 12 : \
  3019. + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
  3020. +*/
  3021. +
  3022. +#define GetPosSlot1(pos) p->g_FastPos[pos]
  3023. +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  3024. +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
  3025. +
  3026. +#endif
  3027. +
  3028. +
  3029. +#define LZMA_NUM_REPS 4
  3030. +
  3031. +typedef unsigned CState;
  3032. +
  3033. +typedef struct
  3034. +{
  3035. + UInt32 price;
  3036. +
  3037. + CState state;
  3038. + int prev1IsChar;
  3039. + int prev2;
  3040. +
  3041. + UInt32 posPrev2;
  3042. + UInt32 backPrev2;
  3043. +
  3044. + UInt32 posPrev;
  3045. + UInt32 backPrev;
  3046. + UInt32 backs[LZMA_NUM_REPS];
  3047. +} COptimal;
  3048. +
  3049. +#define kNumOpts (1 << 12)
  3050. +
  3051. +#define kNumLenToPosStates 4
  3052. +#define kNumPosSlotBits 6
  3053. +#define kDicLogSizeMin 0
  3054. +#define kDicLogSizeMax 32
  3055. +#define kDistTableSizeMax (kDicLogSizeMax * 2)
  3056. +
  3057. +
  3058. +#define kNumAlignBits 4
  3059. +#define kAlignTableSize (1 << kNumAlignBits)
  3060. +#define kAlignMask (kAlignTableSize - 1)
  3061. +
  3062. +#define kStartPosModelIndex 4
  3063. +#define kEndPosModelIndex 14
  3064. +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
  3065. +
  3066. +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  3067. +
  3068. +#ifdef _LZMA_PROB32
  3069. +#define CLzmaProb UInt32
  3070. +#else
  3071. +#define CLzmaProb UInt16
  3072. +#endif
  3073. +
  3074. +#define LZMA_PB_MAX 4
  3075. +#define LZMA_LC_MAX 8
  3076. +#define LZMA_LP_MAX 4
  3077. +
  3078. +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
  3079. +
  3080. +
  3081. +#define kLenNumLowBits 3
  3082. +#define kLenNumLowSymbols (1 << kLenNumLowBits)
  3083. +#define kLenNumMidBits 3
  3084. +#define kLenNumMidSymbols (1 << kLenNumMidBits)
  3085. +#define kLenNumHighBits 8
  3086. +#define kLenNumHighSymbols (1 << kLenNumHighBits)
  3087. +
  3088. +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  3089. +
  3090. +#define LZMA_MATCH_LEN_MIN 2
  3091. +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
  3092. +
  3093. +#define kNumStates 12
  3094. +
  3095. +typedef struct
  3096. +{
  3097. + CLzmaProb choice;
  3098. + CLzmaProb choice2;
  3099. + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
  3100. + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
  3101. + CLzmaProb high[kLenNumHighSymbols];
  3102. +} CLenEnc;
  3103. +
  3104. +typedef struct
  3105. +{
  3106. + CLenEnc p;
  3107. + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
  3108. + UInt32 tableSize;
  3109. + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
  3110. +} CLenPriceEnc;
  3111. +
  3112. +typedef struct
  3113. +{
  3114. + UInt32 range;
  3115. + Byte cache;
  3116. + UInt64 low;
  3117. + UInt64 cacheSize;
  3118. + Byte *buf;
  3119. + Byte *bufLim;
  3120. + Byte *bufBase;
  3121. + ISeqOutStream *outStream;
  3122. + UInt64 processed;
  3123. + SRes res;
  3124. +} CRangeEnc;
  3125. +
  3126. +typedef struct
  3127. +{
  3128. + CLzmaProb *litProbs;
  3129. +
  3130. + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  3131. + CLzmaProb isRep[kNumStates];
  3132. + CLzmaProb isRepG0[kNumStates];
  3133. + CLzmaProb isRepG1[kNumStates];
  3134. + CLzmaProb isRepG2[kNumStates];
  3135. + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  3136. +
  3137. + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  3138. + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  3139. + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  3140. +
  3141. + CLenPriceEnc lenEnc;
  3142. + CLenPriceEnc repLenEnc;
  3143. +
  3144. + UInt32 reps[LZMA_NUM_REPS];
  3145. + UInt32 state;
  3146. +} CSaveState;
  3147. +
  3148. +typedef struct
  3149. +{
  3150. + IMatchFinder matchFinder;
  3151. + void *matchFinderObj;
  3152. +
  3153. + #ifndef _7ZIP_ST
  3154. + Bool mtMode;
  3155. + CMatchFinderMt matchFinderMt;
  3156. + #endif
  3157. +
  3158. + CMatchFinder matchFinderBase;
  3159. +
  3160. + #ifndef _7ZIP_ST
  3161. + Byte pad[128];
  3162. + #endif
  3163. +
  3164. + UInt32 optimumEndIndex;
  3165. + UInt32 optimumCurrentIndex;
  3166. +
  3167. + UInt32 longestMatchLength;
  3168. + UInt32 numPairs;
  3169. + UInt32 numAvail;
  3170. + COptimal opt[kNumOpts];
  3171. +
  3172. + #ifndef LZMA_LOG_BSR
  3173. + Byte g_FastPos[1 << kNumLogBits];
  3174. + #endif
  3175. +
  3176. + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
  3177. + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
  3178. + UInt32 numFastBytes;
  3179. + UInt32 additionalOffset;
  3180. + UInt32 reps[LZMA_NUM_REPS];
  3181. + UInt32 state;
  3182. +
  3183. + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
  3184. + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
  3185. + UInt32 alignPrices[kAlignTableSize];
  3186. + UInt32 alignPriceCount;
  3187. +
  3188. + UInt32 distTableSize;
  3189. +
  3190. + unsigned lc, lp, pb;
  3191. + unsigned lpMask, pbMask;
  3192. +
  3193. + CLzmaProb *litProbs;
  3194. +
  3195. + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  3196. + CLzmaProb isRep[kNumStates];
  3197. + CLzmaProb isRepG0[kNumStates];
  3198. + CLzmaProb isRepG1[kNumStates];
  3199. + CLzmaProb isRepG2[kNumStates];
  3200. + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  3201. +
  3202. + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  3203. + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  3204. + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  3205. +
  3206. + CLenPriceEnc lenEnc;
  3207. + CLenPriceEnc repLenEnc;
  3208. +
  3209. + unsigned lclp;
  3210. +
  3211. + Bool fastMode;
  3212. +
  3213. + CRangeEnc rc;
  3214. +
  3215. + Bool writeEndMark;
  3216. + UInt64 nowPos64;
  3217. + UInt32 matchPriceCount;
  3218. + Bool finished;
  3219. + Bool multiThread;
  3220. +
  3221. + SRes result;
  3222. + UInt32 dictSize;
  3223. + UInt32 matchFinderCycles;
  3224. +
  3225. + int needInit;
  3226. +
  3227. + CSaveState saveState;
  3228. +} CLzmaEnc;
  3229. +
  3230. +void LzmaEnc_SaveState(CLzmaEncHandle pp)
  3231. +{
  3232. + CLzmaEnc *p = (CLzmaEnc *)pp;
  3233. + CSaveState *dest = &p->saveState;
  3234. + int i;
  3235. + dest->lenEnc = p->lenEnc;
  3236. + dest->repLenEnc = p->repLenEnc;
  3237. + dest->state = p->state;
  3238. +
  3239. + for (i = 0; i < kNumStates; i++)
  3240. + {
  3241. + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
  3242. + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
  3243. + }
  3244. + for (i = 0; i < kNumLenToPosStates; i++)
  3245. + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
  3246. + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
  3247. + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
  3248. + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
  3249. + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
  3250. + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
  3251. + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
  3252. + memcpy(dest->reps, p->reps, sizeof(p->reps));
  3253. + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
  3254. +}
  3255. +
  3256. +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
  3257. +{
  3258. + CLzmaEnc *dest = (CLzmaEnc *)pp;
  3259. + const CSaveState *p = &dest->saveState;
  3260. + int i;
  3261. + dest->lenEnc = p->lenEnc;
  3262. + dest->repLenEnc = p->repLenEnc;
  3263. + dest->state = p->state;
  3264. +
  3265. + for (i = 0; i < kNumStates; i++)
  3266. + {
  3267. + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
  3268. + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
  3269. + }
  3270. + for (i = 0; i < kNumLenToPosStates; i++)
  3271. + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
  3272. + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
  3273. + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
  3274. + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
  3275. + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
  3276. + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
  3277. + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
  3278. + memcpy(dest->reps, p->reps, sizeof(p->reps));
  3279. + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
  3280. +}
  3281. +
  3282. +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
  3283. +{
  3284. + CLzmaEnc *p = (CLzmaEnc *)pp;
  3285. + CLzmaEncProps props = *props2;
  3286. + LzmaEncProps_Normalize(&props);
  3287. +
  3288. + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
  3289. + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
  3290. + return SZ_ERROR_PARAM;
  3291. + p->dictSize = props.dictSize;
  3292. + p->matchFinderCycles = props.mc;
  3293. + {
  3294. + unsigned fb = props.fb;
  3295. + if (fb < 5)
  3296. + fb = 5;
  3297. + if (fb > LZMA_MATCH_LEN_MAX)
  3298. + fb = LZMA_MATCH_LEN_MAX;
  3299. + p->numFastBytes = fb;
  3300. + }
  3301. + p->lc = props.lc;
  3302. + p->lp = props.lp;
  3303. + p->pb = props.pb;
  3304. + p->fastMode = (props.algo == 0);
  3305. + p->matchFinderBase.btMode = props.btMode;
  3306. + {
  3307. + UInt32 numHashBytes = 4;
  3308. + if (props.btMode)
  3309. + {
  3310. + if (props.numHashBytes < 2)
  3311. + numHashBytes = 2;
  3312. + else if (props.numHashBytes < 4)
  3313. + numHashBytes = props.numHashBytes;
  3314. + }
  3315. + p->matchFinderBase.numHashBytes = numHashBytes;
  3316. + }
  3317. +
  3318. + p->matchFinderBase.cutValue = props.mc;
  3319. +
  3320. + p->writeEndMark = props.writeEndMark;
  3321. +
  3322. + #ifndef _7ZIP_ST
  3323. + /*
  3324. + if (newMultiThread != _multiThread)
  3325. + {
  3326. + ReleaseMatchFinder();
  3327. + _multiThread = newMultiThread;
  3328. + }
  3329. + */
  3330. + p->multiThread = (props.numThreads > 1);
  3331. + #endif
  3332. +
  3333. + return SZ_OK;
  3334. +}
  3335. +
  3336. +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
  3337. +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
  3338. +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
  3339. +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
  3340. +
  3341. +#define IsCharState(s) ((s) < 7)
  3342. +
  3343. +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
  3344. +
  3345. +#define kInfinityPrice (1 << 30)
  3346. +
  3347. +static void RangeEnc_Construct(CRangeEnc *p)
  3348. +{
  3349. + p->outStream = 0;
  3350. + p->bufBase = 0;
  3351. +}
  3352. +
  3353. +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
  3354. +
  3355. +#define RC_BUF_SIZE (1 << 16)
  3356. +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
  3357. +{
  3358. + if (p->bufBase == 0)
  3359. + {
  3360. + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
  3361. + if (p->bufBase == 0)
  3362. + return 0;
  3363. + p->bufLim = p->bufBase + RC_BUF_SIZE;
  3364. + }
  3365. + return 1;
  3366. +}
  3367. +
  3368. +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
  3369. +{
  3370. + alloc->Free(alloc, p->bufBase);
  3371. + p->bufBase = 0;
  3372. +}
  3373. +
  3374. +static void RangeEnc_Init(CRangeEnc *p)
  3375. +{
  3376. + /* Stream.Init(); */
  3377. + p->low = 0;
  3378. + p->range = 0xFFFFFFFF;
  3379. + p->cacheSize = 1;
  3380. + p->cache = 0;
  3381. +
  3382. + p->buf = p->bufBase;
  3383. +
  3384. + p->processed = 0;
  3385. + p->res = SZ_OK;
  3386. +}
  3387. +
  3388. +static void RangeEnc_FlushStream(CRangeEnc *p)
  3389. +{
  3390. + size_t num;
  3391. + if (p->res != SZ_OK)
  3392. + return;
  3393. + num = p->buf - p->bufBase;
  3394. + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
  3395. + p->res = SZ_ERROR_WRITE;
  3396. + p->processed += num;
  3397. + p->buf = p->bufBase;
  3398. +}
  3399. +
  3400. +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
  3401. +{
  3402. + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
  3403. + {
  3404. + Byte temp = p->cache;
  3405. + do
  3406. + {
  3407. + Byte *buf = p->buf;
  3408. + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
  3409. + p->buf = buf;
  3410. + if (buf == p->bufLim)
  3411. + RangeEnc_FlushStream(p);
  3412. + temp = 0xFF;
  3413. + }
  3414. + while (--p->cacheSize != 0);
  3415. + p->cache = (Byte)((UInt32)p->low >> 24);
  3416. + }
  3417. + p->cacheSize++;
  3418. + p->low = (UInt32)p->low << 8;
  3419. +}
  3420. +
  3421. +static void RangeEnc_FlushData(CRangeEnc *p)
  3422. +{
  3423. + int i;
  3424. + for (i = 0; i < 5; i++)
  3425. + RangeEnc_ShiftLow(p);
  3426. +}
  3427. +
  3428. +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
  3429. +{
  3430. + do
  3431. + {
  3432. + p->range >>= 1;
  3433. + p->low += p->range & (0 - ((value >> --numBits) & 1));
  3434. + if (p->range < kTopValue)
  3435. + {
  3436. + p->range <<= 8;
  3437. + RangeEnc_ShiftLow(p);
  3438. + }
  3439. + }
  3440. + while (numBits != 0);
  3441. +}
  3442. +
  3443. +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
  3444. +{
  3445. + UInt32 ttt = *prob;
  3446. + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
  3447. + if (symbol == 0)
  3448. + {
  3449. + p->range = newBound;
  3450. + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
  3451. + }
  3452. + else
  3453. + {
  3454. + p->low += newBound;
  3455. + p->range -= newBound;
  3456. + ttt -= ttt >> kNumMoveBits;
  3457. + }
  3458. + *prob = (CLzmaProb)ttt;
  3459. + if (p->range < kTopValue)
  3460. + {
  3461. + p->range <<= 8;
  3462. + RangeEnc_ShiftLow(p);
  3463. + }
  3464. +}
  3465. +
  3466. +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
  3467. +{
  3468. + symbol |= 0x100;
  3469. + do
  3470. + {
  3471. + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
  3472. + symbol <<= 1;
  3473. + }
  3474. + while (symbol < 0x10000);
  3475. +}
  3476. +
  3477. +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
  3478. +{
  3479. + UInt32 offs = 0x100;
  3480. + symbol |= 0x100;
  3481. + do
  3482. + {
  3483. + matchByte <<= 1;
  3484. + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
  3485. + symbol <<= 1;
  3486. + offs &= ~(matchByte ^ symbol);
  3487. + }
  3488. + while (symbol < 0x10000);
  3489. +}
  3490. +
  3491. +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
  3492. +{
  3493. + UInt32 i;
  3494. + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
  3495. + {
  3496. + const int kCyclesBits = kNumBitPriceShiftBits;
  3497. + UInt32 w = i;
  3498. + UInt32 bitCount = 0;
  3499. + int j;
  3500. + for (j = 0; j < kCyclesBits; j++)
  3501. + {
  3502. + w = w * w;
  3503. + bitCount <<= 1;
  3504. + while (w >= ((UInt32)1 << 16))
  3505. + {
  3506. + w >>= 1;
  3507. + bitCount++;
  3508. + }
  3509. + }
  3510. + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
  3511. + }
  3512. +}
  3513. +
  3514. +
  3515. +#define GET_PRICE(prob, symbol) \
  3516. + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  3517. +
  3518. +#define GET_PRICEa(prob, symbol) \
  3519. + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  3520. +
  3521. +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
  3522. +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  3523. +
  3524. +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
  3525. +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  3526. +
  3527. +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
  3528. +{
  3529. + UInt32 price = 0;
  3530. + symbol |= 0x100;
  3531. + do
  3532. + {
  3533. + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
  3534. + symbol <<= 1;
  3535. + }
  3536. + while (symbol < 0x10000);
  3537. + return price;
  3538. +}
  3539. +
  3540. +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
  3541. +{
  3542. + UInt32 price = 0;
  3543. + UInt32 offs = 0x100;
  3544. + symbol |= 0x100;
  3545. + do
  3546. + {
  3547. + matchByte <<= 1;
  3548. + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
  3549. + symbol <<= 1;
  3550. + offs &= ~(matchByte ^ symbol);
  3551. + }
  3552. + while (symbol < 0x10000);
  3553. + return price;
  3554. +}
  3555. +
  3556. +
  3557. +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  3558. +{
  3559. + UInt32 m = 1;
  3560. + int i;
  3561. + for (i = numBitLevels; i != 0;)
  3562. + {
  3563. + UInt32 bit;
  3564. + i--;
  3565. + bit = (symbol >> i) & 1;
  3566. + RangeEnc_EncodeBit(rc, probs + m, bit);
  3567. + m = (m << 1) | bit;
  3568. + }
  3569. +}
  3570. +
  3571. +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  3572. +{
  3573. + UInt32 m = 1;
  3574. + int i;
  3575. + for (i = 0; i < numBitLevels; i++)
  3576. + {
  3577. + UInt32 bit = symbol & 1;
  3578. + RangeEnc_EncodeBit(rc, probs + m, bit);
  3579. + m = (m << 1) | bit;
  3580. + symbol >>= 1;
  3581. + }
  3582. +}
  3583. +
  3584. +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  3585. +{
  3586. + UInt32 price = 0;
  3587. + symbol |= (1 << numBitLevels);
  3588. + while (symbol != 1)
  3589. + {
  3590. + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
  3591. + symbol >>= 1;
  3592. + }
  3593. + return price;
  3594. +}
  3595. +
  3596. +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  3597. +{
  3598. + UInt32 price = 0;
  3599. + UInt32 m = 1;
  3600. + int i;
  3601. + for (i = numBitLevels; i != 0; i--)
  3602. + {
  3603. + UInt32 bit = symbol & 1;
  3604. + symbol >>= 1;
  3605. + price += GET_PRICEa(probs[m], bit);
  3606. + m = (m << 1) | bit;
  3607. + }
  3608. + return price;
  3609. +}
  3610. +
  3611. +
  3612. +static void LenEnc_Init(CLenEnc *p)
  3613. +{
  3614. + unsigned i;
  3615. + p->choice = p->choice2 = kProbInitValue;
  3616. + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
  3617. + p->low[i] = kProbInitValue;
  3618. + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
  3619. + p->mid[i] = kProbInitValue;
  3620. + for (i = 0; i < kLenNumHighSymbols; i++)
  3621. + p->high[i] = kProbInitValue;
  3622. +}
  3623. +
  3624. +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
  3625. +{
  3626. + if (symbol < kLenNumLowSymbols)
  3627. + {
  3628. + RangeEnc_EncodeBit(rc, &p->choice, 0);
  3629. + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
  3630. + }
  3631. + else
  3632. + {
  3633. + RangeEnc_EncodeBit(rc, &p->choice, 1);
  3634. + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
  3635. + {
  3636. + RangeEnc_EncodeBit(rc, &p->choice2, 0);
  3637. + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
  3638. + }
  3639. + else
  3640. + {
  3641. + RangeEnc_EncodeBit(rc, &p->choice2, 1);
  3642. + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
  3643. + }
  3644. + }
  3645. +}
  3646. +
  3647. +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
  3648. +{
  3649. + UInt32 a0 = GET_PRICE_0a(p->choice);
  3650. + UInt32 a1 = GET_PRICE_1a(p->choice);
  3651. + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
  3652. + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
  3653. + UInt32 i = 0;
  3654. + for (i = 0; i < kLenNumLowSymbols; i++)
  3655. + {
  3656. + if (i >= numSymbols)
  3657. + return;
  3658. + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
  3659. + }
  3660. + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
  3661. + {
  3662. + if (i >= numSymbols)
  3663. + return;
  3664. + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
  3665. + }
  3666. + for (; i < numSymbols; i++)
  3667. + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
  3668. +}
  3669. +
  3670. +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
  3671. +{
  3672. + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
  3673. + p->counters[posState] = p->tableSize;
  3674. +}
  3675. +
  3676. +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
  3677. +{
  3678. + UInt32 posState;
  3679. + for (posState = 0; posState < numPosStates; posState++)
  3680. + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  3681. +}
  3682. +
  3683. +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
  3684. +{
  3685. + LenEnc_Encode(&p->p, rc, symbol, posState);
  3686. + if (updatePrice)
  3687. + if (--p->counters[posState] == 0)
  3688. + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  3689. +}
  3690. +
  3691. +
  3692. +
  3693. +
  3694. +static void MovePos(CLzmaEnc *p, UInt32 num)
  3695. +{
  3696. + #ifdef SHOW_STAT
  3697. + ttt += num;
  3698. + printf("\n MovePos %d", num);
  3699. + #endif
  3700. + if (num != 0)
  3701. + {
  3702. + p->additionalOffset += num;
  3703. + p->matchFinder.Skip(p->matchFinderObj, num);
  3704. + }
  3705. +}
  3706. +
  3707. +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
  3708. +{
  3709. + UInt32 lenRes = 0, numPairs;
  3710. + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  3711. + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
  3712. + #ifdef SHOW_STAT
  3713. + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
  3714. + ttt++;
  3715. + {
  3716. + UInt32 i;
  3717. + for (i = 0; i < numPairs; i += 2)
  3718. + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
  3719. + }
  3720. + #endif
  3721. + if (numPairs > 0)
  3722. + {
  3723. + lenRes = p->matches[numPairs - 2];
  3724. + if (lenRes == p->numFastBytes)
  3725. + {
  3726. + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  3727. + UInt32 distance = p->matches[numPairs - 1] + 1;
  3728. + UInt32 numAvail = p->numAvail;
  3729. + if (numAvail > LZMA_MATCH_LEN_MAX)
  3730. + numAvail = LZMA_MATCH_LEN_MAX;
  3731. + {
  3732. + const Byte *pby2 = pby - distance;
  3733. + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
  3734. + }
  3735. + }
  3736. + }
  3737. + p->additionalOffset++;
  3738. + *numDistancePairsRes = numPairs;
  3739. + return lenRes;
  3740. +}
  3741. +
  3742. +
  3743. +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
  3744. +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
  3745. +#define IsShortRep(p) ((p)->backPrev == 0)
  3746. +
  3747. +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
  3748. +{
  3749. + return
  3750. + GET_PRICE_0(p->isRepG0[state]) +
  3751. + GET_PRICE_0(p->isRep0Long[state][posState]);
  3752. +}
  3753. +
  3754. +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
  3755. +{
  3756. + UInt32 price;
  3757. + if (repIndex == 0)
  3758. + {
  3759. + price = GET_PRICE_0(p->isRepG0[state]);
  3760. + price += GET_PRICE_1(p->isRep0Long[state][posState]);
  3761. + }
  3762. + else
  3763. + {
  3764. + price = GET_PRICE_1(p->isRepG0[state]);
  3765. + if (repIndex == 1)
  3766. + price += GET_PRICE_0(p->isRepG1[state]);
  3767. + else
  3768. + {
  3769. + price += GET_PRICE_1(p->isRepG1[state]);
  3770. + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
  3771. + }
  3772. + }
  3773. + return price;
  3774. +}
  3775. +
  3776. +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
  3777. +{
  3778. + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
  3779. + GetPureRepPrice(p, repIndex, state, posState);
  3780. +}
  3781. +
  3782. +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
  3783. +{
  3784. + UInt32 posMem = p->opt[cur].posPrev;
  3785. + UInt32 backMem = p->opt[cur].backPrev;
  3786. + p->optimumEndIndex = cur;
  3787. + do
  3788. + {
  3789. + if (p->opt[cur].prev1IsChar)
  3790. + {
  3791. + MakeAsChar(&p->opt[posMem])
  3792. + p->opt[posMem].posPrev = posMem - 1;
  3793. + if (p->opt[cur].prev2)
  3794. + {
  3795. + p->opt[posMem - 1].prev1IsChar = False;
  3796. + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
  3797. + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
  3798. + }
  3799. + }
  3800. + {
  3801. + UInt32 posPrev = posMem;
  3802. + UInt32 backCur = backMem;
  3803. +
  3804. + backMem = p->opt[posPrev].backPrev;
  3805. + posMem = p->opt[posPrev].posPrev;
  3806. +
  3807. + p->opt[posPrev].backPrev = backCur;
  3808. + p->opt[posPrev].posPrev = cur;
  3809. + cur = posPrev;
  3810. + }
  3811. + }
  3812. + while (cur != 0);
  3813. + *backRes = p->opt[0].backPrev;
  3814. + p->optimumCurrentIndex = p->opt[0].posPrev;
  3815. + return p->optimumCurrentIndex;
  3816. +}
  3817. +
  3818. +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
  3819. +
  3820. +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
  3821. +{
  3822. + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
  3823. + UInt32 matchPrice, repMatchPrice, normalMatchPrice;
  3824. + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
  3825. + UInt32 *matches;
  3826. + const Byte *data;
  3827. + Byte curByte, matchByte;
  3828. + if (p->optimumEndIndex != p->optimumCurrentIndex)
  3829. + {
  3830. + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
  3831. + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
  3832. + *backRes = opt->backPrev;
  3833. + p->optimumCurrentIndex = opt->posPrev;
  3834. + return lenRes;
  3835. + }
  3836. + p->optimumCurrentIndex = p->optimumEndIndex = 0;
  3837. +
  3838. + if (p->additionalOffset == 0)
  3839. + mainLen = ReadMatchDistances(p, &numPairs);
  3840. + else
  3841. + {
  3842. + mainLen = p->longestMatchLength;
  3843. + numPairs = p->numPairs;
  3844. + }
  3845. +
  3846. + numAvail = p->numAvail;
  3847. + if (numAvail < 2)
  3848. + {
  3849. + *backRes = (UInt32)(-1);
  3850. + return 1;
  3851. + }
  3852. + if (numAvail > LZMA_MATCH_LEN_MAX)
  3853. + numAvail = LZMA_MATCH_LEN_MAX;
  3854. +
  3855. + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  3856. + repMaxIndex = 0;
  3857. + for (i = 0; i < LZMA_NUM_REPS; i++)
  3858. + {
  3859. + UInt32 lenTest;
  3860. + const Byte *data2;
  3861. + reps[i] = p->reps[i];
  3862. + data2 = data - (reps[i] + 1);
  3863. + if (data[0] != data2[0] || data[1] != data2[1])
  3864. + {
  3865. + repLens[i] = 0;
  3866. + continue;
  3867. + }
  3868. + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  3869. + repLens[i] = lenTest;
  3870. + if (lenTest > repLens[repMaxIndex])
  3871. + repMaxIndex = i;
  3872. + }
  3873. + if (repLens[repMaxIndex] >= p->numFastBytes)
  3874. + {
  3875. + UInt32 lenRes;
  3876. + *backRes = repMaxIndex;
  3877. + lenRes = repLens[repMaxIndex];
  3878. + MovePos(p, lenRes - 1);
  3879. + return lenRes;
  3880. + }
  3881. +
  3882. + matches = p->matches;
  3883. + if (mainLen >= p->numFastBytes)
  3884. + {
  3885. + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  3886. + MovePos(p, mainLen - 1);
  3887. + return mainLen;
  3888. + }
  3889. + curByte = *data;
  3890. + matchByte = *(data - (reps[0] + 1));
  3891. +
  3892. + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
  3893. + {
  3894. + *backRes = (UInt32)-1;
  3895. + return 1;
  3896. + }
  3897. +
  3898. + p->opt[0].state = (CState)p->state;
  3899. +
  3900. + posState = (position & p->pbMask);
  3901. +
  3902. + {
  3903. + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  3904. + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
  3905. + (!IsCharState(p->state) ?
  3906. + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  3907. + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  3908. + }
  3909. +
  3910. + MakeAsChar(&p->opt[1]);
  3911. +
  3912. + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
  3913. + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
  3914. +
  3915. + if (matchByte == curByte)
  3916. + {
  3917. + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
  3918. + if (shortRepPrice < p->opt[1].price)
  3919. + {
  3920. + p->opt[1].price = shortRepPrice;
  3921. + MakeAsShortRep(&p->opt[1]);
  3922. + }
  3923. + }
  3924. + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
  3925. +
  3926. + if (lenEnd < 2)
  3927. + {
  3928. + *backRes = p->opt[1].backPrev;
  3929. + return 1;
  3930. + }
  3931. +
  3932. + p->opt[1].posPrev = 0;
  3933. + for (i = 0; i < LZMA_NUM_REPS; i++)
  3934. + p->opt[0].backs[i] = reps[i];
  3935. +
  3936. + len = lenEnd;
  3937. + do
  3938. + p->opt[len--].price = kInfinityPrice;
  3939. + while (len >= 2);
  3940. +
  3941. + for (i = 0; i < LZMA_NUM_REPS; i++)
  3942. + {
  3943. + UInt32 repLen = repLens[i];
  3944. + UInt32 price;
  3945. + if (repLen < 2)
  3946. + continue;
  3947. + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
  3948. + do
  3949. + {
  3950. + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
  3951. + COptimal *opt = &p->opt[repLen];
  3952. + if (curAndLenPrice < opt->price)
  3953. + {
  3954. + opt->price = curAndLenPrice;
  3955. + opt->posPrev = 0;
  3956. + opt->backPrev = i;
  3957. + opt->prev1IsChar = False;
  3958. + }
  3959. + }
  3960. + while (--repLen >= 2);
  3961. + }
  3962. +
  3963. + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
  3964. +
  3965. + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
  3966. + if (len <= mainLen)
  3967. + {
  3968. + UInt32 offs = 0;
  3969. + while (len > matches[offs])
  3970. + offs += 2;
  3971. + for (; ; len++)
  3972. + {
  3973. + COptimal *opt;
  3974. + UInt32 distance = matches[offs + 1];
  3975. +
  3976. + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
  3977. + UInt32 lenToPosState = GetLenToPosState(len);
  3978. + if (distance < kNumFullDistances)
  3979. + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
  3980. + else
  3981. + {
  3982. + UInt32 slot;
  3983. + GetPosSlot2(distance, slot);
  3984. + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
  3985. + }
  3986. + opt = &p->opt[len];
  3987. + if (curAndLenPrice < opt->price)
  3988. + {
  3989. + opt->price = curAndLenPrice;
  3990. + opt->posPrev = 0;
  3991. + opt->backPrev = distance + LZMA_NUM_REPS;
  3992. + opt->prev1IsChar = False;
  3993. + }
  3994. + if (len == matches[offs])
  3995. + {
  3996. + offs += 2;
  3997. + if (offs == numPairs)
  3998. + break;
  3999. + }
  4000. + }
  4001. + }
  4002. +
  4003. + cur = 0;
  4004. +
  4005. + #ifdef SHOW_STAT2
  4006. + if (position >= 0)
  4007. + {
  4008. + unsigned i;
  4009. + printf("\n pos = %4X", position);
  4010. + for (i = cur; i <= lenEnd; i++)
  4011. + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
  4012. + }
  4013. + #endif
  4014. +
  4015. + for (;;)
  4016. + {
  4017. + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
  4018. + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
  4019. + Bool nextIsChar;
  4020. + Byte curByte, matchByte;
  4021. + const Byte *data;
  4022. + COptimal *curOpt;
  4023. + COptimal *nextOpt;
  4024. +
  4025. + cur++;
  4026. + if (cur == lenEnd)
  4027. + return Backward(p, backRes, cur);
  4028. +
  4029. + newLen = ReadMatchDistances(p, &numPairs);
  4030. + if (newLen >= p->numFastBytes)
  4031. + {
  4032. + p->numPairs = numPairs;
  4033. + p->longestMatchLength = newLen;
  4034. + return Backward(p, backRes, cur);
  4035. + }
  4036. + position++;
  4037. + curOpt = &p->opt[cur];
  4038. + posPrev = curOpt->posPrev;
  4039. + if (curOpt->prev1IsChar)
  4040. + {
  4041. + posPrev--;
  4042. + if (curOpt->prev2)
  4043. + {
  4044. + state = p->opt[curOpt->posPrev2].state;
  4045. + if (curOpt->backPrev2 < LZMA_NUM_REPS)
  4046. + state = kRepNextStates[state];
  4047. + else
  4048. + state = kMatchNextStates[state];
  4049. + }
  4050. + else
  4051. + state = p->opt[posPrev].state;
  4052. + state = kLiteralNextStates[state];
  4053. + }
  4054. + else
  4055. + state = p->opt[posPrev].state;
  4056. + if (posPrev == cur - 1)
  4057. + {
  4058. + if (IsShortRep(curOpt))
  4059. + state = kShortRepNextStates[state];
  4060. + else
  4061. + state = kLiteralNextStates[state];
  4062. + }
  4063. + else
  4064. + {
  4065. + UInt32 pos;
  4066. + const COptimal *prevOpt;
  4067. + if (curOpt->prev1IsChar && curOpt->prev2)
  4068. + {
  4069. + posPrev = curOpt->posPrev2;
  4070. + pos = curOpt->backPrev2;
  4071. + state = kRepNextStates[state];
  4072. + }
  4073. + else
  4074. + {
  4075. + pos = curOpt->backPrev;
  4076. + if (pos < LZMA_NUM_REPS)
  4077. + state = kRepNextStates[state];
  4078. + else
  4079. + state = kMatchNextStates[state];
  4080. + }
  4081. + prevOpt = &p->opt[posPrev];
  4082. + if (pos < LZMA_NUM_REPS)
  4083. + {
  4084. + UInt32 i;
  4085. + reps[0] = prevOpt->backs[pos];
  4086. + for (i = 1; i <= pos; i++)
  4087. + reps[i] = prevOpt->backs[i - 1];
  4088. + for (; i < LZMA_NUM_REPS; i++)
  4089. + reps[i] = prevOpt->backs[i];
  4090. + }
  4091. + else
  4092. + {
  4093. + UInt32 i;
  4094. + reps[0] = (pos - LZMA_NUM_REPS);
  4095. + for (i = 1; i < LZMA_NUM_REPS; i++)
  4096. + reps[i] = prevOpt->backs[i - 1];
  4097. + }
  4098. + }
  4099. + curOpt->state = (CState)state;
  4100. +
  4101. + curOpt->backs[0] = reps[0];
  4102. + curOpt->backs[1] = reps[1];
  4103. + curOpt->backs[2] = reps[2];
  4104. + curOpt->backs[3] = reps[3];
  4105. +
  4106. + curPrice = curOpt->price;
  4107. + nextIsChar = False;
  4108. + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  4109. + curByte = *data;
  4110. + matchByte = *(data - (reps[0] + 1));
  4111. +
  4112. + posState = (position & p->pbMask);
  4113. +
  4114. + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
  4115. + {
  4116. + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  4117. + curAnd1Price +=
  4118. + (!IsCharState(state) ?
  4119. + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  4120. + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  4121. + }
  4122. +
  4123. + nextOpt = &p->opt[cur + 1];
  4124. +
  4125. + if (curAnd1Price < nextOpt->price)
  4126. + {
  4127. + nextOpt->price = curAnd1Price;
  4128. + nextOpt->posPrev = cur;
  4129. + MakeAsChar(nextOpt);
  4130. + nextIsChar = True;
  4131. + }
  4132. +
  4133. + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
  4134. + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
  4135. +
  4136. + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
  4137. + {
  4138. + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
  4139. + if (shortRepPrice <= nextOpt->price)
  4140. + {
  4141. + nextOpt->price = shortRepPrice;
  4142. + nextOpt->posPrev = cur;
  4143. + MakeAsShortRep(nextOpt);
  4144. + nextIsChar = True;
  4145. + }
  4146. + }
  4147. + numAvailFull = p->numAvail;
  4148. + {
  4149. + UInt32 temp = kNumOpts - 1 - cur;
  4150. + if (temp < numAvailFull)
  4151. + numAvailFull = temp;
  4152. + }
  4153. +
  4154. + if (numAvailFull < 2)
  4155. + continue;
  4156. + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
  4157. +
  4158. + if (!nextIsChar && matchByte != curByte) /* speed optimization */
  4159. + {
  4160. + /* try Literal + rep0 */
  4161. + UInt32 temp;
  4162. + UInt32 lenTest2;
  4163. + const Byte *data2 = data - (reps[0] + 1);
  4164. + UInt32 limit = p->numFastBytes + 1;
  4165. + if (limit > numAvailFull)
  4166. + limit = numAvailFull;
  4167. +
  4168. + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
  4169. + lenTest2 = temp - 1;
  4170. + if (lenTest2 >= 2)
  4171. + {
  4172. + UInt32 state2 = kLiteralNextStates[state];
  4173. + UInt32 posStateNext = (position + 1) & p->pbMask;
  4174. + UInt32 nextRepMatchPrice = curAnd1Price +
  4175. + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  4176. + GET_PRICE_1(p->isRep[state2]);
  4177. + /* for (; lenTest2 >= 2; lenTest2--) */
  4178. + {
  4179. + UInt32 curAndLenPrice;
  4180. + COptimal *opt;
  4181. + UInt32 offset = cur + 1 + lenTest2;
  4182. + while (lenEnd < offset)
  4183. + p->opt[++lenEnd].price = kInfinityPrice;
  4184. + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  4185. + opt = &p->opt[offset];
  4186. + if (curAndLenPrice < opt->price)
  4187. + {
  4188. + opt->price = curAndLenPrice;
  4189. + opt->posPrev = cur + 1;
  4190. + opt->backPrev = 0;
  4191. + opt->prev1IsChar = True;
  4192. + opt->prev2 = False;
  4193. + }
  4194. + }
  4195. + }
  4196. + }
  4197. +
  4198. + startLen = 2; /* speed optimization */
  4199. + {
  4200. + UInt32 repIndex;
  4201. + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
  4202. + {
  4203. + UInt32 lenTest;
  4204. + UInt32 lenTestTemp;
  4205. + UInt32 price;
  4206. + const Byte *data2 = data - (reps[repIndex] + 1);
  4207. + if (data[0] != data2[0] || data[1] != data2[1])
  4208. + continue;
  4209. + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  4210. + while (lenEnd < cur + lenTest)
  4211. + p->opt[++lenEnd].price = kInfinityPrice;
  4212. + lenTestTemp = lenTest;
  4213. + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
  4214. + do
  4215. + {
  4216. + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
  4217. + COptimal *opt = &p->opt[cur + lenTest];
  4218. + if (curAndLenPrice < opt->price)
  4219. + {
  4220. + opt->price = curAndLenPrice;
  4221. + opt->posPrev = cur;
  4222. + opt->backPrev = repIndex;
  4223. + opt->prev1IsChar = False;
  4224. + }
  4225. + }
  4226. + while (--lenTest >= 2);
  4227. + lenTest = lenTestTemp;
  4228. +
  4229. + if (repIndex == 0)
  4230. + startLen = lenTest + 1;
  4231. +
  4232. + /* if (_maxMode) */
  4233. + {
  4234. + UInt32 lenTest2 = lenTest + 1;
  4235. + UInt32 limit = lenTest2 + p->numFastBytes;
  4236. + UInt32 nextRepMatchPrice;
  4237. + if (limit > numAvailFull)
  4238. + limit = numAvailFull;
  4239. + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  4240. + lenTest2 -= lenTest + 1;
  4241. + if (lenTest2 >= 2)
  4242. + {
  4243. + UInt32 state2 = kRepNextStates[state];
  4244. + UInt32 posStateNext = (position + lenTest) & p->pbMask;
  4245. + UInt32 curAndLenCharPrice =
  4246. + price + p->repLenEnc.prices[posState][lenTest - 2] +
  4247. + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  4248. + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  4249. + data[lenTest], data2[lenTest], p->ProbPrices);
  4250. + state2 = kLiteralNextStates[state2];
  4251. + posStateNext = (position + lenTest + 1) & p->pbMask;
  4252. + nextRepMatchPrice = curAndLenCharPrice +
  4253. + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  4254. + GET_PRICE_1(p->isRep[state2]);
  4255. +
  4256. + /* for (; lenTest2 >= 2; lenTest2--) */
  4257. + {
  4258. + UInt32 curAndLenPrice;
  4259. + COptimal *opt;
  4260. + UInt32 offset = cur + lenTest + 1 + lenTest2;
  4261. + while (lenEnd < offset)
  4262. + p->opt[++lenEnd].price = kInfinityPrice;
  4263. + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  4264. + opt = &p->opt[offset];
  4265. + if (curAndLenPrice < opt->price)
  4266. + {
  4267. + opt->price = curAndLenPrice;
  4268. + opt->posPrev = cur + lenTest + 1;
  4269. + opt->backPrev = 0;
  4270. + opt->prev1IsChar = True;
  4271. + opt->prev2 = True;
  4272. + opt->posPrev2 = cur;
  4273. + opt->backPrev2 = repIndex;
  4274. + }
  4275. + }
  4276. + }
  4277. + }
  4278. + }
  4279. + }
  4280. + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
  4281. + if (newLen > numAvail)
  4282. + {
  4283. + newLen = numAvail;
  4284. + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
  4285. + matches[numPairs] = newLen;
  4286. + numPairs += 2;
  4287. + }
  4288. + if (newLen >= startLen)
  4289. + {
  4290. + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
  4291. + UInt32 offs, curBack, posSlot;
  4292. + UInt32 lenTest;
  4293. + while (lenEnd < cur + newLen)
  4294. + p->opt[++lenEnd].price = kInfinityPrice;
  4295. +
  4296. + offs = 0;
  4297. + while (startLen > matches[offs])
  4298. + offs += 2;
  4299. + curBack = matches[offs + 1];
  4300. + GetPosSlot2(curBack, posSlot);
  4301. + for (lenTest = /*2*/ startLen; ; lenTest++)
  4302. + {
  4303. + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
  4304. + UInt32 lenToPosState = GetLenToPosState(lenTest);
  4305. + COptimal *opt;
  4306. + if (curBack < kNumFullDistances)
  4307. + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
  4308. + else
  4309. + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
  4310. +
  4311. + opt = &p->opt[cur + lenTest];
  4312. + if (curAndLenPrice < opt->price)
  4313. + {
  4314. + opt->price = curAndLenPrice;
  4315. + opt->posPrev = cur;
  4316. + opt->backPrev = curBack + LZMA_NUM_REPS;
  4317. + opt->prev1IsChar = False;
  4318. + }
  4319. +
  4320. + if (/*_maxMode && */lenTest == matches[offs])
  4321. + {
  4322. + /* Try Match + Literal + Rep0 */
  4323. + const Byte *data2 = data - (curBack + 1);
  4324. + UInt32 lenTest2 = lenTest + 1;
  4325. + UInt32 limit = lenTest2 + p->numFastBytes;
  4326. + UInt32 nextRepMatchPrice;
  4327. + if (limit > numAvailFull)
  4328. + limit = numAvailFull;
  4329. + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  4330. + lenTest2 -= lenTest + 1;
  4331. + if (lenTest2 >= 2)
  4332. + {
  4333. + UInt32 state2 = kMatchNextStates[state];
  4334. + UInt32 posStateNext = (position + lenTest) & p->pbMask;
  4335. + UInt32 curAndLenCharPrice = curAndLenPrice +
  4336. + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  4337. + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  4338. + data[lenTest], data2[lenTest], p->ProbPrices);
  4339. + state2 = kLiteralNextStates[state2];
  4340. + posStateNext = (posStateNext + 1) & p->pbMask;
  4341. + nextRepMatchPrice = curAndLenCharPrice +
  4342. + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  4343. + GET_PRICE_1(p->isRep[state2]);
  4344. +
  4345. + /* for (; lenTest2 >= 2; lenTest2--) */
  4346. + {
  4347. + UInt32 offset = cur + lenTest + 1 + lenTest2;
  4348. + UInt32 curAndLenPrice;
  4349. + COptimal *opt;
  4350. + while (lenEnd < offset)
  4351. + p->opt[++lenEnd].price = kInfinityPrice;
  4352. + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  4353. + opt = &p->opt[offset];
  4354. + if (curAndLenPrice < opt->price)
  4355. + {
  4356. + opt->price = curAndLenPrice;
  4357. + opt->posPrev = cur + lenTest + 1;
  4358. + opt->backPrev = 0;
  4359. + opt->prev1IsChar = True;
  4360. + opt->prev2 = True;
  4361. + opt->posPrev2 = cur;
  4362. + opt->backPrev2 = curBack + LZMA_NUM_REPS;
  4363. + }
  4364. + }
  4365. + }
  4366. + offs += 2;
  4367. + if (offs == numPairs)
  4368. + break;
  4369. + curBack = matches[offs + 1];
  4370. + if (curBack >= kNumFullDistances)
  4371. + GetPosSlot2(curBack, posSlot);
  4372. + }
  4373. + }
  4374. + }
  4375. + }
  4376. +}
  4377. +
  4378. +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
  4379. +
  4380. +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
  4381. +{
  4382. + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
  4383. + const Byte *data;
  4384. + const UInt32 *matches;
  4385. +
  4386. + if (p->additionalOffset == 0)
  4387. + mainLen = ReadMatchDistances(p, &numPairs);
  4388. + else
  4389. + {
  4390. + mainLen = p->longestMatchLength;
  4391. + numPairs = p->numPairs;
  4392. + }
  4393. +
  4394. + numAvail = p->numAvail;
  4395. + *backRes = (UInt32)-1;
  4396. + if (numAvail < 2)
  4397. + return 1;
  4398. + if (numAvail > LZMA_MATCH_LEN_MAX)
  4399. + numAvail = LZMA_MATCH_LEN_MAX;
  4400. + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  4401. +
  4402. + repLen = repIndex = 0;
  4403. + for (i = 0; i < LZMA_NUM_REPS; i++)
  4404. + {
  4405. + UInt32 len;
  4406. + const Byte *data2 = data - (p->reps[i] + 1);
  4407. + if (data[0] != data2[0] || data[1] != data2[1])
  4408. + continue;
  4409. + for (len = 2; len < numAvail && data[len] == data2[len]; len++);
  4410. + if (len >= p->numFastBytes)
  4411. + {
  4412. + *backRes = i;
  4413. + MovePos(p, len - 1);
  4414. + return len;
  4415. + }
  4416. + if (len > repLen)
  4417. + {
  4418. + repIndex = i;
  4419. + repLen = len;
  4420. + }
  4421. + }
  4422. +
  4423. + matches = p->matches;
  4424. + if (mainLen >= p->numFastBytes)
  4425. + {
  4426. + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  4427. + MovePos(p, mainLen - 1);
  4428. + return mainLen;
  4429. + }
  4430. +
  4431. + mainDist = 0; /* for GCC */
  4432. + if (mainLen >= 2)
  4433. + {
  4434. + mainDist = matches[numPairs - 1];
  4435. + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
  4436. + {
  4437. + if (!ChangePair(matches[numPairs - 3], mainDist))
  4438. + break;
  4439. + numPairs -= 2;
  4440. + mainLen = matches[numPairs - 2];
  4441. + mainDist = matches[numPairs - 1];
  4442. + }
  4443. + if (mainLen == 2 && mainDist >= 0x80)
  4444. + mainLen = 1;
  4445. + }
  4446. +
  4447. + if (repLen >= 2 && (
  4448. + (repLen + 1 >= mainLen) ||
  4449. + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
  4450. + (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
  4451. + {
  4452. + *backRes = repIndex;
  4453. + MovePos(p, repLen - 1);
  4454. + return repLen;
  4455. + }
  4456. +
  4457. + if (mainLen < 2 || numAvail <= 2)
  4458. + return 1;
  4459. +
  4460. + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
  4461. + if (p->longestMatchLength >= 2)
  4462. + {
  4463. + UInt32 newDistance = matches[p->numPairs - 1];
  4464. + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
  4465. + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
  4466. + (p->longestMatchLength > mainLen + 1) ||
  4467. + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
  4468. + return 1;
  4469. + }
  4470. +
  4471. + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  4472. + for (i = 0; i < LZMA_NUM_REPS; i++)
  4473. + {
  4474. + UInt32 len, limit;
  4475. + const Byte *data2 = data - (p->reps[i] + 1);
  4476. + if (data[0] != data2[0] || data[1] != data2[1])
  4477. + continue;
  4478. + limit = mainLen - 1;
  4479. + for (len = 2; len < limit && data[len] == data2[len]; len++);
  4480. + if (len >= limit)
  4481. + return 1;
  4482. + }
  4483. + *backRes = mainDist + LZMA_NUM_REPS;
  4484. + MovePos(p, mainLen - 2);
  4485. + return mainLen;
  4486. +}
  4487. +
  4488. +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
  4489. +{
  4490. + UInt32 len;
  4491. + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  4492. + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  4493. + p->state = kMatchNextStates[p->state];
  4494. + len = LZMA_MATCH_LEN_MIN;
  4495. + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  4496. + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
  4497. + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
  4498. + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
  4499. +}
  4500. +
  4501. +static SRes CheckErrors(CLzmaEnc *p)
  4502. +{
  4503. + if (p->result != SZ_OK)
  4504. + return p->result;
  4505. + if (p->rc.res != SZ_OK)
  4506. + p->result = SZ_ERROR_WRITE;
  4507. + if (p->matchFinderBase.result != SZ_OK)
  4508. + p->result = SZ_ERROR_READ;
  4509. + if (p->result != SZ_OK)
  4510. + p->finished = True;
  4511. + return p->result;
  4512. +}
  4513. +
  4514. +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
  4515. +{
  4516. + /* ReleaseMFStream(); */
  4517. + p->finished = True;
  4518. + if (p->writeEndMark)
  4519. + WriteEndMarker(p, nowPos & p->pbMask);
  4520. + RangeEnc_FlushData(&p->rc);
  4521. + RangeEnc_FlushStream(&p->rc);
  4522. + return CheckErrors(p);
  4523. +}
  4524. +
  4525. +static void FillAlignPrices(CLzmaEnc *p)
  4526. +{
  4527. + UInt32 i;
  4528. + for (i = 0; i < kAlignTableSize; i++)
  4529. + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
  4530. + p->alignPriceCount = 0;
  4531. +}
  4532. +
  4533. +static void FillDistancesPrices(CLzmaEnc *p)
  4534. +{
  4535. + UInt32 tempPrices[kNumFullDistances];
  4536. + UInt32 i, lenToPosState;
  4537. + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
  4538. + {
  4539. + UInt32 posSlot = GetPosSlot1(i);
  4540. + UInt32 footerBits = ((posSlot >> 1) - 1);
  4541. + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  4542. + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
  4543. + }
  4544. +
  4545. + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
  4546. + {
  4547. + UInt32 posSlot;
  4548. + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
  4549. + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
  4550. + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
  4551. + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
  4552. + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
  4553. + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
  4554. +
  4555. + {
  4556. + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
  4557. + UInt32 i;
  4558. + for (i = 0; i < kStartPosModelIndex; i++)
  4559. + distancesPrices[i] = posSlotPrices[i];
  4560. + for (; i < kNumFullDistances; i++)
  4561. + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
  4562. + }
  4563. + }
  4564. + p->matchPriceCount = 0;
  4565. +}
  4566. +
  4567. +void LzmaEnc_Construct(CLzmaEnc *p)
  4568. +{
  4569. + RangeEnc_Construct(&p->rc);
  4570. + MatchFinder_Construct(&p->matchFinderBase);
  4571. + #ifndef _7ZIP_ST
  4572. + MatchFinderMt_Construct(&p->matchFinderMt);
  4573. + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
  4574. + #endif
  4575. +
  4576. + {
  4577. + CLzmaEncProps props;
  4578. + LzmaEncProps_Init(&props);
  4579. + LzmaEnc_SetProps(p, &props);
  4580. + }
  4581. +
  4582. + #ifndef LZMA_LOG_BSR
  4583. + LzmaEnc_FastPosInit(p->g_FastPos);
  4584. + #endif
  4585. +
  4586. + LzmaEnc_InitPriceTables(p->ProbPrices);
  4587. + p->litProbs = 0;
  4588. + p->saveState.litProbs = 0;
  4589. +}
  4590. +
  4591. +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
  4592. +{
  4593. + void *p;
  4594. + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
  4595. + if (p != 0)
  4596. + LzmaEnc_Construct((CLzmaEnc *)p);
  4597. + return p;
  4598. +}
  4599. +
  4600. +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
  4601. +{
  4602. + alloc->Free(alloc, p->litProbs);
  4603. + alloc->Free(alloc, p->saveState.litProbs);
  4604. + p->litProbs = 0;
  4605. + p->saveState.litProbs = 0;
  4606. +}
  4607. +
  4608. +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
  4609. +{
  4610. + #ifndef _7ZIP_ST
  4611. + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
  4612. + #endif
  4613. + MatchFinder_Free(&p->matchFinderBase, allocBig);
  4614. + LzmaEnc_FreeLits(p, alloc);
  4615. + RangeEnc_Free(&p->rc, alloc);
  4616. +}
  4617. +
  4618. +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
  4619. +{
  4620. + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
  4621. + alloc->Free(alloc, p);
  4622. +}
  4623. +
  4624. +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
  4625. +{
  4626. + UInt32 nowPos32, startPos32;
  4627. + if (p->needInit)
  4628. + {
  4629. + p->matchFinder.Init(p->matchFinderObj);
  4630. + p->needInit = 0;
  4631. + }
  4632. +
  4633. + if (p->finished)
  4634. + return p->result;
  4635. + RINOK(CheckErrors(p));
  4636. +
  4637. + nowPos32 = (UInt32)p->nowPos64;
  4638. + startPos32 = nowPos32;
  4639. +
  4640. + if (p->nowPos64 == 0)
  4641. + {
  4642. + UInt32 numPairs;
  4643. + Byte curByte;
  4644. + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  4645. + return Flush(p, nowPos32);
  4646. + ReadMatchDistances(p, &numPairs);
  4647. + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
  4648. + p->state = kLiteralNextStates[p->state];
  4649. + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
  4650. + LitEnc_Encode(&p->rc, p->litProbs, curByte);
  4651. + p->additionalOffset--;
  4652. + nowPos32++;
  4653. + }
  4654. +
  4655. + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
  4656. + for (;;)
  4657. + {
  4658. + UInt32 pos, len, posState;
  4659. +
  4660. + if (p->fastMode)
  4661. + len = GetOptimumFast(p, &pos);
  4662. + else
  4663. + len = GetOptimum(p, nowPos32, &pos);
  4664. +
  4665. + #ifdef SHOW_STAT2
  4666. + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
  4667. + #endif
  4668. +
  4669. + posState = nowPos32 & p->pbMask;
  4670. + if (len == 1 && pos == (UInt32)-1)
  4671. + {
  4672. + Byte curByte;
  4673. + CLzmaProb *probs;
  4674. + const Byte *data;
  4675. +
  4676. + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
  4677. + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  4678. + curByte = *data;
  4679. + probs = LIT_PROBS(nowPos32, *(data - 1));
  4680. + if (IsCharState(p->state))
  4681. + LitEnc_Encode(&p->rc, probs, curByte);
  4682. + else
  4683. + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
  4684. + p->state = kLiteralNextStates[p->state];
  4685. + }
  4686. + else
  4687. + {
  4688. + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  4689. + if (pos < LZMA_NUM_REPS)
  4690. + {
  4691. + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
  4692. + if (pos == 0)
  4693. + {
  4694. + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
  4695. + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
  4696. + }
  4697. + else
  4698. + {
  4699. + UInt32 distance = p->reps[pos];
  4700. + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
  4701. + if (pos == 1)
  4702. + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
  4703. + else
  4704. + {
  4705. + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
  4706. + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
  4707. + if (pos == 3)
  4708. + p->reps[3] = p->reps[2];
  4709. + p->reps[2] = p->reps[1];
  4710. + }
  4711. + p->reps[1] = p->reps[0];
  4712. + p->reps[0] = distance;
  4713. + }
  4714. + if (len == 1)
  4715. + p->state = kShortRepNextStates[p->state];
  4716. + else
  4717. + {
  4718. + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  4719. + p->state = kRepNextStates[p->state];
  4720. + }
  4721. + }
  4722. + else
  4723. + {
  4724. + UInt32 posSlot;
  4725. + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  4726. + p->state = kMatchNextStates[p->state];
  4727. + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  4728. + pos -= LZMA_NUM_REPS;
  4729. + GetPosSlot(pos, posSlot);
  4730. + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
  4731. +
  4732. + if (posSlot >= kStartPosModelIndex)
  4733. + {
  4734. + UInt32 footerBits = ((posSlot >> 1) - 1);
  4735. + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  4736. + UInt32 posReduced = pos - base;
  4737. +
  4738. + if (posSlot < kEndPosModelIndex)
  4739. + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
  4740. + else
  4741. + {
  4742. + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
  4743. + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
  4744. + p->alignPriceCount++;
  4745. + }
  4746. + }
  4747. + p->reps[3] = p->reps[2];
  4748. + p->reps[2] = p->reps[1];
  4749. + p->reps[1] = p->reps[0];
  4750. + p->reps[0] = pos;
  4751. + p->matchPriceCount++;
  4752. + }
  4753. + }
  4754. + p->additionalOffset -= len;
  4755. + nowPos32 += len;
  4756. + if (p->additionalOffset == 0)
  4757. + {
  4758. + UInt32 processed;
  4759. + if (!p->fastMode)
  4760. + {
  4761. + if (p->matchPriceCount >= (1 << 7))
  4762. + FillDistancesPrices(p);
  4763. + if (p->alignPriceCount >= kAlignTableSize)
  4764. + FillAlignPrices(p);
  4765. + }
  4766. + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  4767. + break;
  4768. + processed = nowPos32 - startPos32;
  4769. + if (useLimits)
  4770. + {
  4771. + if (processed + kNumOpts + 300 >= maxUnpackSize ||
  4772. + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
  4773. + break;
  4774. + }
  4775. + else if (processed >= (1 << 15))
  4776. + {
  4777. + p->nowPos64 += nowPos32 - startPos32;
  4778. + return CheckErrors(p);
  4779. + }
  4780. + }
  4781. + }
  4782. + p->nowPos64 += nowPos32 - startPos32;
  4783. + return Flush(p, nowPos32);
  4784. +}
  4785. +
  4786. +#define kBigHashDicLimit ((UInt32)1 << 24)
  4787. +
  4788. +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  4789. +{
  4790. + UInt32 beforeSize = kNumOpts;
  4791. + Bool btMode;
  4792. + if (!RangeEnc_Alloc(&p->rc, alloc))
  4793. + return SZ_ERROR_MEM;
  4794. + btMode = (p->matchFinderBase.btMode != 0);
  4795. + #ifndef _7ZIP_ST
  4796. + p->mtMode = (p->multiThread && !p->fastMode && btMode);
  4797. + #endif
  4798. +
  4799. + {
  4800. + unsigned lclp = p->lc + p->lp;
  4801. + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
  4802. + {
  4803. + LzmaEnc_FreeLits(p, alloc);
  4804. + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  4805. + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  4806. + if (p->litProbs == 0 || p->saveState.litProbs == 0)
  4807. + {
  4808. + LzmaEnc_FreeLits(p, alloc);
  4809. + return SZ_ERROR_MEM;
  4810. + }
  4811. + p->lclp = lclp;
  4812. + }
  4813. + }
  4814. +
  4815. + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
  4816. +
  4817. + if (beforeSize + p->dictSize < keepWindowSize)
  4818. + beforeSize = keepWindowSize - p->dictSize;
  4819. +
  4820. + #ifndef _7ZIP_ST
  4821. + if (p->mtMode)
  4822. + {
  4823. + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
  4824. + p->matchFinderObj = &p->matchFinderMt;
  4825. + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
  4826. + }
  4827. + else
  4828. + #endif
  4829. + {
  4830. + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
  4831. + return SZ_ERROR_MEM;
  4832. + p->matchFinderObj = &p->matchFinderBase;
  4833. + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
  4834. + }
  4835. + return SZ_OK;
  4836. +}
  4837. +
  4838. +void LzmaEnc_Init(CLzmaEnc *p)
  4839. +{
  4840. + UInt32 i;
  4841. + p->state = 0;
  4842. + for (i = 0 ; i < LZMA_NUM_REPS; i++)
  4843. + p->reps[i] = 0;
  4844. +
  4845. + RangeEnc_Init(&p->rc);
  4846. +
  4847. +
  4848. + for (i = 0; i < kNumStates; i++)
  4849. + {
  4850. + UInt32 j;
  4851. + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
  4852. + {
  4853. + p->isMatch[i][j] = kProbInitValue;
  4854. + p->isRep0Long[i][j] = kProbInitValue;
  4855. + }
  4856. + p->isRep[i] = kProbInitValue;
  4857. + p->isRepG0[i] = kProbInitValue;
  4858. + p->isRepG1[i] = kProbInitValue;
  4859. + p->isRepG2[i] = kProbInitValue;
  4860. + }
  4861. +
  4862. + {
  4863. + UInt32 num = 0x300 << (p->lp + p->lc);
  4864. + for (i = 0; i < num; i++)
  4865. + p->litProbs[i] = kProbInitValue;
  4866. + }
  4867. +
  4868. + {
  4869. + for (i = 0; i < kNumLenToPosStates; i++)
  4870. + {
  4871. + CLzmaProb *probs = p->posSlotEncoder[i];
  4872. + UInt32 j;
  4873. + for (j = 0; j < (1 << kNumPosSlotBits); j++)
  4874. + probs[j] = kProbInitValue;
  4875. + }
  4876. + }
  4877. + {
  4878. + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
  4879. + p->posEncoders[i] = kProbInitValue;
  4880. + }
  4881. +
  4882. + LenEnc_Init(&p->lenEnc.p);
  4883. + LenEnc_Init(&p->repLenEnc.p);
  4884. +
  4885. + for (i = 0; i < (1 << kNumAlignBits); i++)
  4886. + p->posAlignEncoder[i] = kProbInitValue;
  4887. +
  4888. + p->optimumEndIndex = 0;
  4889. + p->optimumCurrentIndex = 0;
  4890. + p->additionalOffset = 0;
  4891. +
  4892. + p->pbMask = (1 << p->pb) - 1;
  4893. + p->lpMask = (1 << p->lp) - 1;
  4894. +}
  4895. +
  4896. +void LzmaEnc_InitPrices(CLzmaEnc *p)
  4897. +{
  4898. + if (!p->fastMode)
  4899. + {
  4900. + FillDistancesPrices(p);
  4901. + FillAlignPrices(p);
  4902. + }
  4903. +
  4904. + p->lenEnc.tableSize =
  4905. + p->repLenEnc.tableSize =
  4906. + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
  4907. + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
  4908. + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
  4909. +}
  4910. +
  4911. +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  4912. +{
  4913. + UInt32 i;
  4914. + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
  4915. + if (p->dictSize <= ((UInt32)1 << i))
  4916. + break;
  4917. + p->distTableSize = i * 2;
  4918. +
  4919. + p->finished = False;
  4920. + p->result = SZ_OK;
  4921. + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
  4922. + LzmaEnc_Init(p);
  4923. + LzmaEnc_InitPrices(p);
  4924. + p->nowPos64 = 0;
  4925. + return SZ_OK;
  4926. +}
  4927. +
  4928. +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
  4929. + ISzAlloc *alloc, ISzAlloc *allocBig)
  4930. +{
  4931. + CLzmaEnc *p = (CLzmaEnc *)pp;
  4932. + p->matchFinderBase.stream = inStream;
  4933. + p->needInit = 1;
  4934. + p->rc.outStream = outStream;
  4935. + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
  4936. +}
  4937. +
  4938. +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
  4939. + ISeqInStream *inStream, UInt32 keepWindowSize,
  4940. + ISzAlloc *alloc, ISzAlloc *allocBig)
  4941. +{
  4942. + CLzmaEnc *p = (CLzmaEnc *)pp;
  4943. + p->matchFinderBase.stream = inStream;
  4944. + p->needInit = 1;
  4945. + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  4946. +}
  4947. +
  4948. +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
  4949. +{
  4950. + p->matchFinderBase.directInput = 1;
  4951. + p->matchFinderBase.bufferBase = (Byte *)src;
  4952. + p->matchFinderBase.directInputRem = srcLen;
  4953. +}
  4954. +
  4955. +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
  4956. + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  4957. +{
  4958. + CLzmaEnc *p = (CLzmaEnc *)pp;
  4959. + LzmaEnc_SetInputBuf(p, src, srcLen);
  4960. + p->needInit = 1;
  4961. +
  4962. + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  4963. +}
  4964. +
  4965. +void LzmaEnc_Finish(CLzmaEncHandle pp)
  4966. +{
  4967. + #ifndef _7ZIP_ST
  4968. + CLzmaEnc *p = (CLzmaEnc *)pp;
  4969. + if (p->mtMode)
  4970. + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
  4971. + #else
  4972. + pp = pp;
  4973. + #endif
  4974. +}
  4975. +
  4976. +typedef struct
  4977. +{
  4978. + ISeqOutStream funcTable;
  4979. + Byte *data;
  4980. + SizeT rem;
  4981. + Bool overflow;
  4982. +} CSeqOutStreamBuf;
  4983. +
  4984. +static size_t MyWrite(void *pp, const void *data, size_t size)
  4985. +{
  4986. + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
  4987. + if (p->rem < size)
  4988. + {
  4989. + size = p->rem;
  4990. + p->overflow = True;
  4991. + }
  4992. + memcpy(p->data, data, size);
  4993. + p->rem -= size;
  4994. + p->data += size;
  4995. + return size;
  4996. +}
  4997. +
  4998. +
  4999. +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
  5000. +{
  5001. + const CLzmaEnc *p = (CLzmaEnc *)pp;
  5002. + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  5003. +}
  5004. +
  5005. +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
  5006. +{
  5007. + const CLzmaEnc *p = (CLzmaEnc *)pp;
  5008. + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  5009. +}
  5010. +
  5011. +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
  5012. + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
  5013. +{
  5014. + CLzmaEnc *p = (CLzmaEnc *)pp;
  5015. + UInt64 nowPos64;
  5016. + SRes res;
  5017. + CSeqOutStreamBuf outStream;
  5018. +
  5019. + outStream.funcTable.Write = MyWrite;
  5020. + outStream.data = dest;
  5021. + outStream.rem = *destLen;
  5022. + outStream.overflow = False;
  5023. +
  5024. + p->writeEndMark = False;
  5025. + p->finished = False;
  5026. + p->result = SZ_OK;
  5027. +
  5028. + if (reInit)
  5029. + LzmaEnc_Init(p);
  5030. + LzmaEnc_InitPrices(p);
  5031. + nowPos64 = p->nowPos64;
  5032. + RangeEnc_Init(&p->rc);
  5033. + p->rc.outStream = &outStream.funcTable;
  5034. +
  5035. + res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
  5036. +
  5037. + *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
  5038. + *destLen -= outStream.rem;
  5039. + if (outStream.overflow)
  5040. + return SZ_ERROR_OUTPUT_EOF;
  5041. +
  5042. + return res;
  5043. +}
  5044. +
  5045. +static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
  5046. +{
  5047. + SRes res = SZ_OK;
  5048. +
  5049. + #ifndef _7ZIP_ST
  5050. + Byte allocaDummy[0x300];
  5051. + int i = 0;
  5052. + for (i = 0; i < 16; i++)
  5053. + allocaDummy[i] = (Byte)i;
  5054. + #endif
  5055. +
  5056. + for (;;)
  5057. + {
  5058. + res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
  5059. + if (res != SZ_OK || p->finished != 0)
  5060. + break;
  5061. + if (progress != 0)
  5062. + {
  5063. + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
  5064. + if (res != SZ_OK)
  5065. + {
  5066. + res = SZ_ERROR_PROGRESS;
  5067. + break;
  5068. + }
  5069. + }
  5070. + }
  5071. + LzmaEnc_Finish(p);
  5072. + return res;
  5073. +}
  5074. +
  5075. +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
  5076. + ISzAlloc *alloc, ISzAlloc *allocBig)
  5077. +{
  5078. + RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
  5079. + return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
  5080. +}
  5081. +
  5082. +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
  5083. +{
  5084. + CLzmaEnc *p = (CLzmaEnc *)pp;
  5085. + int i;
  5086. + UInt32 dictSize = p->dictSize;
  5087. + if (*size < LZMA_PROPS_SIZE)
  5088. + return SZ_ERROR_PARAM;
  5089. + *size = LZMA_PROPS_SIZE;
  5090. + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
  5091. +
  5092. + for (i = 11; i <= 30; i++)
  5093. + {
  5094. + if (dictSize <= ((UInt32)2 << i))
  5095. + {
  5096. + dictSize = (2 << i);
  5097. + break;
  5098. + }
  5099. + if (dictSize <= ((UInt32)3 << i))
  5100. + {
  5101. + dictSize = (3 << i);
  5102. + break;
  5103. + }
  5104. + }
  5105. +
  5106. + for (i = 0; i < 4; i++)
  5107. + props[1 + i] = (Byte)(dictSize >> (8 * i));
  5108. + return SZ_OK;
  5109. +}
  5110. +
  5111. +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  5112. + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  5113. +{
  5114. + SRes res;
  5115. + CLzmaEnc *p = (CLzmaEnc *)pp;
  5116. +
  5117. + CSeqOutStreamBuf outStream;
  5118. +
  5119. + LzmaEnc_SetInputBuf(p, src, srcLen);
  5120. +
  5121. + outStream.funcTable.Write = MyWrite;
  5122. + outStream.data = dest;
  5123. + outStream.rem = *destLen;
  5124. + outStream.overflow = False;
  5125. +
  5126. + p->writeEndMark = writeEndMark;
  5127. +
  5128. + p->rc.outStream = &outStream.funcTable;
  5129. + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
  5130. + if (res == SZ_OK)
  5131. + res = LzmaEnc_Encode2(p, progress);
  5132. +
  5133. + *destLen -= outStream.rem;
  5134. + if (outStream.overflow)
  5135. + return SZ_ERROR_OUTPUT_EOF;
  5136. + return res;
  5137. +}
  5138. +
  5139. +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  5140. + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
  5141. + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  5142. +{
  5143. + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
  5144. + SRes res;
  5145. + if (p == 0)
  5146. + return SZ_ERROR_MEM;
  5147. +
  5148. + res = LzmaEnc_SetProps(p, props);
  5149. + if (res == SZ_OK)
  5150. + {
  5151. + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
  5152. + if (res == SZ_OK)
  5153. + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
  5154. + writeEndMark, progress, alloc, allocBig);
  5155. + }
  5156. +
  5157. + LzmaEnc_Destroy(p, alloc, allocBig);
  5158. + return res;
  5159. +}
  5160. --- /dev/null
  5161. +++ b/lib/lzma/Makefile
  5162. @@ -0,0 +1,7 @@
  5163. +lzma_compress-objs := LzFind.o LzmaEnc.o
  5164. +lzma_decompress-objs := LzmaDec.o
  5165. +
  5166. +obj-$(CONFIG_LZMA_COMPRESS) += lzma_compress.o
  5167. +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma_decompress.o
  5168. +
  5169. +EXTRA_CFLAGS += -Iinclude/linux -Iinclude/linux/lzma -include types.h