530-jffs2_make_lzma_available.patch 142 KB

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