530-jffs2_make_lzma_available.patch 141 KB

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