130-lzma_jffs2.patch 141 KB

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