530-jffs2_make_lzma_available.patch 125 KB

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