530-jffs2_make_lzma_available.patch 142 KB

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