130-lzma_jffs2.patch 140 KB

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