1
0

130-lzma_jffs2.patch 140 KB

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