130-lzma_jffs2.patch 140 KB

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