2
0

130-lzma_jffs2.patch 140 KB

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