physfs_lzmasdk.h 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031
  1. #ifndef _INCLUDE_PHYSFS_LZMASDK_H_
  2. #define _INCLUDE_PHYSFS_LZMASDK_H_
  3. /* This is just a bunch of the LZMA SDK mushed together into one header.
  4. This code is all public domain, and mostly (if not entirely) written by
  5. Igor Pavlov. http://www.7-zip.org/sdk.html
  6. --ryan. */
  7. /* 7zTypes.h -- Basic types
  8. 2013-11-12 : Igor Pavlov : Public domain */
  9. #ifndef __7Z_TYPES_H
  10. #define __7Z_TYPES_H
  11. #ifdef _WIN32
  12. /* #include <windows.h> */
  13. #endif
  14. #include <stddef.h>
  15. #ifndef EXTERN_C_BEGIN
  16. #ifdef __cplusplus
  17. #define EXTERN_C_BEGIN extern "C" {
  18. #define EXTERN_C_END }
  19. #else
  20. #define EXTERN_C_BEGIN
  21. #define EXTERN_C_END
  22. #endif
  23. #endif
  24. EXTERN_C_BEGIN
  25. #define SZ_OK 0
  26. #define SZ_ERROR_DATA 1
  27. #define SZ_ERROR_MEM 2
  28. #define SZ_ERROR_CRC 3
  29. #define SZ_ERROR_UNSUPPORTED 4
  30. #define SZ_ERROR_PARAM 5
  31. #define SZ_ERROR_INPUT_EOF 6
  32. #define SZ_ERROR_OUTPUT_EOF 7
  33. #define SZ_ERROR_READ 8
  34. #define SZ_ERROR_WRITE 9
  35. #define SZ_ERROR_PROGRESS 10
  36. #define SZ_ERROR_FAIL 11
  37. #define SZ_ERROR_THREAD 12
  38. #define SZ_ERROR_ARCHIVE 16
  39. #define SZ_ERROR_NO_ARCHIVE 17
  40. typedef int SRes;
  41. #ifdef _WIN32
  42. /* typedef DWORD WRes; */
  43. typedef unsigned WRes;
  44. #else
  45. typedef int WRes;
  46. #endif
  47. #ifndef RINOK
  48. #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
  49. #endif
  50. typedef unsigned char Byte;
  51. typedef short Int16;
  52. typedef unsigned short UInt16;
  53. #ifdef _LZMA_UINT32_IS_ULONG
  54. typedef long Int32;
  55. typedef unsigned long UInt32;
  56. #else
  57. typedef int Int32;
  58. typedef unsigned int UInt32;
  59. #endif
  60. #ifdef _SZ_NO_INT_64
  61. /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
  62. NOTES: Some code will work incorrectly in that case! */
  63. typedef long Int64;
  64. typedef unsigned long UInt64;
  65. #else
  66. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
  67. typedef __int64 Int64;
  68. typedef unsigned __int64 UInt64;
  69. #define UINT64_CONST(n) n ## ui64
  70. #else
  71. typedef long long int Int64;
  72. typedef unsigned long long int UInt64;
  73. #define UINT64_CONST(n) n ## ULL
  74. #endif
  75. #endif
  76. #ifdef _LZMA_NO_SYSTEM_SIZE_T
  77. typedef UInt32 SizeT;
  78. #else
  79. typedef size_t SizeT;
  80. #endif
  81. typedef int Bool;
  82. #define True 1
  83. #define False 0
  84. #ifdef _WIN32
  85. #define MY_STD_CALL __stdcall
  86. #else
  87. #define MY_STD_CALL
  88. #endif
  89. #ifdef _MSC_VER
  90. #if _MSC_VER >= 1300
  91. #define MY_NO_INLINE __declspec(noinline)
  92. #else
  93. #define MY_NO_INLINE
  94. #endif
  95. #define MY_CDECL __cdecl
  96. #define MY_FAST_CALL __fastcall
  97. #else
  98. #define MY_NO_INLINE
  99. #define MY_CDECL
  100. #define MY_FAST_CALL
  101. #endif
  102. /* The following interfaces use first parameter as pointer to structure */
  103. typedef struct
  104. {
  105. Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */
  106. } IByteIn;
  107. typedef struct
  108. {
  109. void (*Write)(void *p, Byte b);
  110. } IByteOut;
  111. typedef struct
  112. {
  113. SRes (*Read)(void *p, void *buf, size_t *size);
  114. /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
  115. (output(*size) < input(*size)) is allowed */
  116. } ISeqInStream;
  117. typedef struct
  118. {
  119. size_t (*Write)(void *p, const void *buf, size_t size);
  120. /* Returns: result - the number of actually written bytes.
  121. (result < size) means error */
  122. } ISeqOutStream;
  123. typedef enum
  124. {
  125. SZ_SEEK_SET = 0,
  126. SZ_SEEK_CUR = 1,
  127. SZ_SEEK_END = 2
  128. } ESzSeek;
  129. typedef struct
  130. {
  131. SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
  132. SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
  133. } ISeekInStream;
  134. typedef struct
  135. {
  136. SRes (*Look)(void *p, const void **buf, size_t *size);
  137. /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
  138. (output(*size) > input(*size)) is not allowed
  139. (output(*size) < input(*size)) is allowed */
  140. SRes (*Skip)(void *p, size_t offset);
  141. /* offset must be <= output(*size) of Look */
  142. SRes (*Read)(void *p, void *buf, size_t *size);
  143. /* reads directly (without buffer). It's same as ISeqInStream::Read */
  144. SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
  145. } ILookInStream;
  146. static SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
  147. /* reads via ILookInStream::Read */
  148. static SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
  149. static SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
  150. #define LookToRead_BUF_SIZE (1 << 14)
  151. typedef struct
  152. {
  153. ILookInStream s;
  154. ISeekInStream *realStream;
  155. size_t pos;
  156. size_t size;
  157. Byte buf[LookToRead_BUF_SIZE];
  158. } CLookToRead;
  159. static void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
  160. static void LookToRead_Init(CLookToRead *p);
  161. typedef struct
  162. {
  163. ISeqInStream s;
  164. ILookInStream *realStream;
  165. } CSecToLook;
  166. typedef struct
  167. {
  168. ISeqInStream s;
  169. ILookInStream *realStream;
  170. } CSecToRead;
  171. typedef struct
  172. {
  173. SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
  174. /* Returns: result. (result != SZ_OK) means break.
  175. Value (UInt64)(Int64)-1 for size means unknown value. */
  176. } ICompressProgress;
  177. typedef struct
  178. {
  179. void *(*Alloc)(void *p, size_t size);
  180. void (*Free)(void *p, void *address); /* address can be 0 */
  181. } ISzAlloc;
  182. #define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
  183. #define IAlloc_Free(p, a) (p)->Free((p), a)
  184. #ifdef _WIN32
  185. #define CHAR_PATH_SEPARATOR '\\'
  186. #define WCHAR_PATH_SEPARATOR L'\\'
  187. #define STRING_PATH_SEPARATOR "\\"
  188. #define WSTRING_PATH_SEPARATOR L"\\"
  189. #else
  190. #define CHAR_PATH_SEPARATOR '/'
  191. #define WCHAR_PATH_SEPARATOR L'/'
  192. #define STRING_PATH_SEPARATOR "/"
  193. #define WSTRING_PATH_SEPARATOR L"/"
  194. #endif
  195. EXTERN_C_END
  196. #endif
  197. /* 7z.h -- 7z interface
  198. 2015-11-18 : Igor Pavlov : Public domain */
  199. #ifndef __7Z_H
  200. #define __7Z_H
  201. /*#include "7zTypes.h"*/
  202. EXTERN_C_BEGIN
  203. #define k7zStartHeaderSize 0x20
  204. #define k7zSignatureSize 6
  205. static const Byte k7zSignature[k7zSignatureSize];
  206. typedef struct
  207. {
  208. const Byte *Data;
  209. size_t Size;
  210. } CSzData;
  211. /* CSzCoderInfo & CSzFolder support only default methods */
  212. typedef struct
  213. {
  214. size_t PropsOffset;
  215. UInt32 MethodID;
  216. Byte NumStreams;
  217. Byte PropsSize;
  218. } CSzCoderInfo;
  219. typedef struct
  220. {
  221. UInt32 InIndex;
  222. UInt32 OutIndex;
  223. } CSzBond;
  224. #define SZ_NUM_CODERS_IN_FOLDER_MAX 4
  225. #define SZ_NUM_BONDS_IN_FOLDER_MAX 3
  226. #define SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX 4
  227. typedef struct
  228. {
  229. UInt32 NumCoders;
  230. UInt32 NumBonds;
  231. UInt32 NumPackStreams;
  232. UInt32 UnpackStream;
  233. UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX];
  234. CSzBond Bonds[SZ_NUM_BONDS_IN_FOLDER_MAX];
  235. CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX];
  236. } CSzFolder;
  237. static SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd);
  238. typedef struct
  239. {
  240. UInt32 Low;
  241. UInt32 High;
  242. } CNtfsFileTime;
  243. typedef struct
  244. {
  245. Byte *Defs; /* MSB 0 bit numbering */
  246. UInt32 *Vals;
  247. } CSzBitUi32s;
  248. typedef struct
  249. {
  250. Byte *Defs; /* MSB 0 bit numbering */
  251. /* UInt64 *Vals; */
  252. CNtfsFileTime *Vals;
  253. } CSzBitUi64s;
  254. #define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0)
  255. #define SzBitWithVals_Check(p, i) ((p)->Defs && ((p)->Defs[(i) >> 3] & (0x80 >> ((i) & 7))) != 0)
  256. typedef struct
  257. {
  258. UInt32 NumPackStreams;
  259. UInt32 NumFolders;
  260. UInt64 *PackPositions; /* NumPackStreams + 1 */
  261. CSzBitUi32s FolderCRCs; /* NumFolders */
  262. size_t *FoCodersOffsets; /* NumFolders + 1 */
  263. UInt32 *FoStartPackStreamIndex; /* NumFolders + 1 */
  264. UInt32 *FoToCoderUnpackSizes; /* NumFolders + 1 */
  265. Byte *FoToMainUnpackSizeIndex; /* NumFolders */
  266. UInt64 *CoderUnpackSizes; /* for all coders in all folders */
  267. Byte *CodersData;
  268. } CSzAr;
  269. static UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex);
  270. static SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex,
  271. ILookInStream *stream, UInt64 startPos,
  272. Byte *outBuffer, size_t outSize,
  273. ISzAlloc *allocMain);
  274. typedef struct
  275. {
  276. CSzAr db;
  277. UInt64 startPosAfterHeader;
  278. UInt64 dataPos;
  279. UInt32 NumFiles;
  280. UInt64 *UnpackPositions; /* NumFiles + 1 */
  281. /* Byte *IsEmptyFiles; */
  282. Byte *IsDirs;
  283. CSzBitUi32s CRCs;
  284. CSzBitUi32s Attribs;
  285. /* CSzBitUi32s Parents; */
  286. CSzBitUi64s MTime;
  287. CSzBitUi64s CTime;
  288. UInt32 *FolderToFile; /* NumFolders + 1 */
  289. UInt32 *FileToFolder; /* NumFiles */
  290. size_t *FileNameOffsets; /* in 2-byte steps */
  291. Byte *FileNames; /* UTF-16-LE */
  292. } CSzArEx;
  293. #define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i))
  294. #define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i])
  295. static void SzArEx_Init(CSzArEx *p);
  296. static void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc);
  297. /*
  298. if dest == NULL, the return value specifies the required size of the buffer,
  299. in 16-bit characters, including the null-terminating character.
  300. if dest != NULL, the return value specifies the number of 16-bit characters that
  301. are written to the dest, including the null-terminating character. */
  302. static size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest);
  303. /*
  304. size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex);
  305. UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest);
  306. */
  307. /*
  308. SzArEx_Extract extracts file from archive
  309. *outBuffer must be 0 before first call for each new archive.
  310. Extracting cache:
  311. If you need to decompress more than one file, you can send
  312. these values from previous call:
  313. *blockIndex,
  314. *outBuffer,
  315. *outBufferSize
  316. You can consider "*outBuffer" as cache of solid block. If your archive is solid,
  317. it will increase decompression speed.
  318. If you use external function, you can declare these 3 cache variables
  319. (blockIndex, outBuffer, outBufferSize) as static in that external function.
  320. Free *outBuffer and set *outBuffer to 0, if you want to flush cache.
  321. */
  322. static SRes SzArEx_Extract(
  323. const CSzArEx *db,
  324. ILookInStream *inStream,
  325. UInt32 fileIndex, /* index of file */
  326. UInt32 *blockIndex, /* index of solid block */
  327. Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */
  328. size_t *outBufferSize, /* buffer size for output buffer */
  329. size_t *offset, /* offset of stream for required file in *outBuffer */
  330. size_t *outSizeProcessed, /* size of file in *outBuffer */
  331. ISzAlloc *allocMain,
  332. ISzAlloc *allocTemp);
  333. /*
  334. SzArEx_Open Errors:
  335. SZ_ERROR_NO_ARCHIVE
  336. SZ_ERROR_ARCHIVE
  337. SZ_ERROR_UNSUPPORTED
  338. SZ_ERROR_MEM
  339. SZ_ERROR_CRC
  340. SZ_ERROR_INPUT_EOF
  341. SZ_ERROR_FAIL
  342. */
  343. static SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream,
  344. ISzAlloc *allocMain, ISzAlloc *allocTemp);
  345. EXTERN_C_END
  346. #endif
  347. /* 7zCrc.h -- CRC32 calculation
  348. 2013-01-18 : Igor Pavlov : Public domain */
  349. #ifndef __7Z_CRC_H
  350. #define __7Z_CRC_H
  351. /*#include "7zTypes.h" */
  352. EXTERN_C_BEGIN
  353. /* Call CrcGenerateTable one time before other CRC functions */
  354. static void MY_FAST_CALL CrcGenerateTable(void);
  355. #define CRC_INIT_VAL 0xFFFFFFFF
  356. #define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL)
  357. #define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
  358. static UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size);
  359. EXTERN_C_END
  360. #endif
  361. /* CpuArch.h -- CPU specific code
  362. 2016-06-09: Igor Pavlov : Public domain */
  363. #ifndef __CPU_ARCH_H
  364. #define __CPU_ARCH_H
  365. /*#include "7zTypes.h"*/
  366. EXTERN_C_BEGIN
  367. /*
  368. MY_CPU_LE means that CPU is LITTLE ENDIAN.
  369. MY_CPU_BE means that CPU is BIG ENDIAN.
  370. If MY_CPU_LE and MY_CPU_BE are not defined, we don't know about ENDIANNESS of platform.
  371. MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses.
  372. */
  373. #if defined(_M_X64) \
  374. || defined(_M_AMD64) \
  375. || defined(__x86_64__) \
  376. || defined(__AMD64__) \
  377. || defined(__amd64__)
  378. #define MY_CPU_AMD64
  379. #endif
  380. #if defined(MY_CPU_AMD64) \
  381. || defined(_M_ARM64) \
  382. || defined(_M_IA64) \
  383. || defined(__AARCH64EL__) \
  384. || defined(__AARCH64EB__)
  385. #define MY_CPU_64BIT
  386. #endif
  387. #if defined(_M_IX86) || defined(__i386__)
  388. #define MY_CPU_X86
  389. #endif
  390. #if defined(MY_CPU_X86) || defined(MY_CPU_AMD64)
  391. #define MY_CPU_X86_OR_AMD64
  392. #endif
  393. #if defined(MY_CPU_X86) \
  394. || defined(_M_ARM) \
  395. || defined(__ARMEL__) \
  396. || defined(__THUMBEL__) \
  397. || defined(__ARMEB__) \
  398. || defined(__THUMBEB__)
  399. #define MY_CPU_32BIT
  400. #endif
  401. #if defined(_WIN32) && defined(_M_ARM)
  402. #define MY_CPU_ARM_LE
  403. #elif defined(_WIN64) && defined(_M_ARM64)
  404. #define MY_CPU_ARM_LE
  405. #endif
  406. #if defined(_WIN32) && defined(_M_IA64)
  407. #define MY_CPU_IA64_LE
  408. #endif
  409. #if defined(MY_CPU_X86_OR_AMD64) \
  410. || defined(MY_CPU_ARM_LE) \
  411. || defined(MY_CPU_IA64_LE) \
  412. || defined(__LITTLE_ENDIAN__) \
  413. || defined(__ARMEL__) \
  414. || defined(__THUMBEL__) \
  415. || defined(__AARCH64EL__) \
  416. || defined(__MIPSEL__) \
  417. || defined(__MIPSEL) \
  418. || defined(_MIPSEL) \
  419. || defined(__BFIN__) \
  420. || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  421. #define MY_CPU_LE
  422. #endif
  423. #if defined(__BIG_ENDIAN__) \
  424. || defined(__ARMEB__) \
  425. || defined(__THUMBEB__) \
  426. || defined(__AARCH64EB__) \
  427. || defined(__MIPSEB__) \
  428. || defined(__MIPSEB) \
  429. || defined(_MIPSEB) \
  430. || defined(__m68k__) \
  431. || defined(__s390__) \
  432. || defined(__s390x__) \
  433. || defined(__zarch__) \
  434. || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  435. #define MY_CPU_BE
  436. #endif
  437. #if defined(MY_CPU_LE) && defined(MY_CPU_BE)
  438. Stop_Compiling_Bad_Endian
  439. #endif
  440. #ifdef MY_CPU_LE
  441. #if defined(MY_CPU_X86_OR_AMD64) \
  442. /* || defined(__AARCH64EL__) */
  443. /*#define MY_CPU_LE_UNALIGN*/
  444. #endif
  445. #endif
  446. #ifdef MY_CPU_LE_UNALIGN
  447. #define GetUi16(p) (*(const UInt16 *)(const void *)(p))
  448. #define GetUi32(p) (*(const UInt32 *)(const void *)(p))
  449. #define GetUi64(p) (*(const UInt64 *)(const void *)(p))
  450. #define SetUi16(p, v) { *(UInt16 *)(p) = (v); }
  451. #define SetUi32(p, v) { *(UInt32 *)(p) = (v); }
  452. #define SetUi64(p, v) { *(UInt64 *)(p) = (v); }
  453. #else
  454. #define GetUi16(p) ( (UInt16) ( \
  455. ((const Byte *)(p))[0] | \
  456. ((UInt16)((const Byte *)(p))[1] << 8) ))
  457. #define GetUi32(p) ( \
  458. ((const Byte *)(p))[0] | \
  459. ((UInt32)((const Byte *)(p))[1] << 8) | \
  460. ((UInt32)((const Byte *)(p))[2] << 16) | \
  461. ((UInt32)((const Byte *)(p))[3] << 24))
  462. #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
  463. #define SetUi16(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  464. _ppp_[0] = (Byte)_vvv_; \
  465. _ppp_[1] = (Byte)(_vvv_ >> 8); }
  466. #define SetUi32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  467. _ppp_[0] = (Byte)_vvv_; \
  468. _ppp_[1] = (Byte)(_vvv_ >> 8); \
  469. _ppp_[2] = (Byte)(_vvv_ >> 16); \
  470. _ppp_[3] = (Byte)(_vvv_ >> 24); }
  471. #define SetUi64(p, v) { Byte *_ppp2_ = (Byte *)(p); UInt64 _vvv2_ = (v); \
  472. SetUi32(_ppp2_ , (UInt32)_vvv2_); \
  473. SetUi32(_ppp2_ + 4, (UInt32)(_vvv2_ >> 32)); }
  474. #endif
  475. #if defined(MY_CPU_LE_UNALIGN) && /* defined(_WIN64) && */ (_MSC_VER >= 1300)
  476. /* Note: we use bswap instruction, that is unsupported in 386 cpu */
  477. #include <stdlib.h>
  478. #pragma intrinsic(_byteswap_ulong)
  479. #pragma intrinsic(_byteswap_uint64)
  480. #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p))
  481. #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p))
  482. #define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = _byteswap_ulong(v)
  483. #elif defined(MY_CPU_LE_UNALIGN) && defined (__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  484. #define GetBe32(p) __builtin_bswap32(*(const UInt32 *)(const Byte *)(p))
  485. #define GetBe64(p) __builtin_bswap64(*(const UInt64 *)(const Byte *)(p))
  486. #define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = __builtin_bswap32(v)
  487. #else
  488. #define GetBe32(p) ( \
  489. ((UInt32)((const Byte *)(p))[0] << 24) | \
  490. ((UInt32)((const Byte *)(p))[1] << 16) | \
  491. ((UInt32)((const Byte *)(p))[2] << 8) | \
  492. ((const Byte *)(p))[3] )
  493. #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
  494. #define SetBe32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  495. _ppp_[0] = (Byte)(_vvv_ >> 24); \
  496. _ppp_[1] = (Byte)(_vvv_ >> 16); \
  497. _ppp_[2] = (Byte)(_vvv_ >> 8); \
  498. _ppp_[3] = (Byte)_vvv_; }
  499. #endif
  500. #define GetBe16(p) ( (UInt16) ( \
  501. ((UInt16)((const Byte *)(p))[0] << 8) | \
  502. ((const Byte *)(p))[1] ))
  503. #ifdef MY_CPU_X86_OR_AMD64
  504. typedef struct
  505. {
  506. UInt32 maxFunc;
  507. UInt32 vendor[3];
  508. UInt32 ver;
  509. UInt32 b;
  510. UInt32 c;
  511. UInt32 d;
  512. } Cx86cpuid;
  513. enum
  514. {
  515. CPU_FIRM_INTEL,
  516. CPU_FIRM_AMD,
  517. CPU_FIRM_VIA
  518. };
  519. static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d);
  520. static Bool x86cpuid_CheckAndRead(Cx86cpuid *p);
  521. static int x86cpuid_GetFirm(const Cx86cpuid *p);
  522. #define x86cpuid_GetFamily(ver) (((ver >> 16) & 0xFF0) | ((ver >> 8) & 0xF))
  523. #define x86cpuid_GetModel(ver) (((ver >> 12) & 0xF0) | ((ver >> 4) & 0xF))
  524. #define x86cpuid_GetStepping(ver) (ver & 0xF)
  525. static Bool CPU_Is_InOrder();
  526. #endif
  527. EXTERN_C_END
  528. #endif
  529. /* 7zBuf.h -- Byte Buffer
  530. 2013-01-18 : Igor Pavlov : Public domain */
  531. #ifndef __7Z_BUF_H
  532. #define __7Z_BUF_H
  533. /*#include "7zTypes.h" */
  534. EXTERN_C_BEGIN
  535. typedef struct
  536. {
  537. Byte *data;
  538. size_t size;
  539. } CBuf;
  540. static void Buf_Init(CBuf *p);
  541. static int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc);
  542. static void Buf_Free(CBuf *p, ISzAlloc *alloc);
  543. EXTERN_C_END
  544. #endif
  545. /* Bcj2.h -- BCJ2 Converter for x86 code
  546. 2014-11-10 : Igor Pavlov : Public domain */
  547. #ifndef __BCJ2_H
  548. #define __BCJ2_H
  549. /*#include "7zTypes.h" */
  550. EXTERN_C_BEGIN
  551. #define BCJ2_NUM_STREAMS 4
  552. enum
  553. {
  554. BCJ2_STREAM_MAIN,
  555. BCJ2_STREAM_CALL,
  556. BCJ2_STREAM_JUMP,
  557. BCJ2_STREAM_RC
  558. };
  559. enum
  560. {
  561. BCJ2_DEC_STATE_ORIG_0 = BCJ2_NUM_STREAMS,
  562. BCJ2_DEC_STATE_ORIG_1,
  563. BCJ2_DEC_STATE_ORIG_2,
  564. BCJ2_DEC_STATE_ORIG_3,
  565. BCJ2_DEC_STATE_ORIG,
  566. BCJ2_DEC_STATE_OK
  567. };
  568. enum
  569. {
  570. BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS,
  571. BCJ2_ENC_STATE_OK
  572. };
  573. #define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP)
  574. /*
  575. CBcj2Dec / CBcj2Enc
  576. bufs sizes:
  577. BUF_SIZE(n) = lims[n] - bufs[n]
  578. bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be mutliply of 4:
  579. (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0
  580. (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0
  581. */
  582. /*
  583. CBcj2Dec:
  584. dest is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions:
  585. bufs[BCJ2_STREAM_MAIN] >= dest &&
  586. bufs[BCJ2_STREAM_MAIN] - dest >= tempReserv +
  587. BUF_SIZE(BCJ2_STREAM_CALL) +
  588. BUF_SIZE(BCJ2_STREAM_JUMP)
  589. tempReserv = 0 : for first call of Bcj2Dec_Decode
  590. tempReserv = 4 : for any other calls of Bcj2Dec_Decode
  591. overlap with offset = 1 is not allowed
  592. */
  593. typedef struct
  594. {
  595. const Byte *bufs[BCJ2_NUM_STREAMS];
  596. const Byte *lims[BCJ2_NUM_STREAMS];
  597. Byte *dest;
  598. const Byte *destLim;
  599. unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
  600. UInt32 ip;
  601. Byte temp[4];
  602. UInt32 range;
  603. UInt32 code;
  604. UInt16 probs[2 + 256];
  605. } CBcj2Dec;
  606. static void Bcj2Dec_Init(CBcj2Dec *p);
  607. /* Returns: SZ_OK or SZ_ERROR_DATA */
  608. static SRes Bcj2Dec_Decode(CBcj2Dec *p);
  609. #define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0)
  610. #define BCJ2_RELAT_LIMIT_NUM_BITS 26
  611. #define BCJ2_RELAT_LIMIT ((UInt32)1 << BCJ2_RELAT_LIMIT_NUM_BITS)
  612. /* limit for CBcj2Enc::fileSize variable */
  613. #define BCJ2_FileSize_MAX ((UInt32)1 << 31)
  614. EXTERN_C_END
  615. #endif
  616. /* Bra.h -- Branch converters for executables
  617. 2013-01-18 : Igor Pavlov : Public domain */
  618. #ifndef __BRA_H
  619. #define __BRA_H
  620. /*#include "7zTypes.h"*/
  621. EXTERN_C_BEGIN
  622. /*
  623. These functions convert relative addresses to absolute addresses
  624. in CALL instructions to increase the compression ratio.
  625. In:
  626. data - data buffer
  627. size - size of data
  628. ip - current virtual Instruction Pinter (IP) value
  629. state - state variable for x86 converter
  630. encoding - 0 (for decoding), 1 (for encoding)
  631. Out:
  632. state - state variable for x86 converter
  633. Returns:
  634. The number of processed bytes. If you call these functions with multiple calls,
  635. you must start next call with first byte after block of processed bytes.
  636. Type Endian Alignment LookAhead
  637. x86 little 1 4
  638. ARMT little 2 2
  639. ARM little 4 0
  640. PPC big 4 0
  641. SPARC big 4 0
  642. IA64 little 16 0
  643. size must be >= Alignment + LookAhead, if it's not last block.
  644. If (size < Alignment + LookAhead), converter returns 0.
  645. Example:
  646. UInt32 ip = 0;
  647. for ()
  648. {
  649. ; size must be >= Alignment + LookAhead, if it's not last block
  650. SizeT processed = Convert(data, size, ip, 1);
  651. data += processed;
  652. size -= processed;
  653. ip += processed;
  654. }
  655. */
  656. #define x86_Convert_Init(state) { state = 0; }
  657. static SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding);
  658. static SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
  659. static SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
  660. static SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
  661. static SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
  662. static SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
  663. EXTERN_C_END
  664. #endif
  665. /* Delta.h -- Delta converter
  666. 2013-01-18 : Igor Pavlov : Public domain */
  667. #ifndef __DELTA_H
  668. #define __DELTA_H
  669. /*#include "7zTypes.h" */
  670. EXTERN_C_BEGIN
  671. #define DELTA_STATE_SIZE 256
  672. static void Delta_Init(Byte *state);
  673. static void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size);
  674. EXTERN_C_END
  675. #endif
  676. /* LzmaDec.h -- LZMA Decoder
  677. 2013-01-18 : Igor Pavlov : Public domain */
  678. #ifndef __LZMA_DEC_H
  679. #define __LZMA_DEC_H
  680. /*#include "7zTypes.h"*/
  681. EXTERN_C_BEGIN
  682. /* #define _LZMA_PROB32 */
  683. /* _LZMA_PROB32 can increase the speed on some CPUs,
  684. but memory usage for CLzmaDec::probs will be doubled in that case */
  685. #ifdef _LZMA_PROB32
  686. #define CLzmaProb UInt32
  687. #else
  688. #define CLzmaProb UInt16
  689. #endif
  690. /* ---------- LZMA Properties ---------- */
  691. #define LZMA_PROPS_SIZE 5
  692. typedef struct _CLzmaProps
  693. {
  694. unsigned lc, lp, pb;
  695. UInt32 dicSize;
  696. } CLzmaProps;
  697. /* LzmaProps_Decode - decodes properties
  698. Returns:
  699. SZ_OK
  700. SZ_ERROR_UNSUPPORTED - Unsupported properties
  701. */
  702. static SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
  703. /* ---------- LZMA Decoder state ---------- */
  704. /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
  705. Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
  706. #define LZMA_REQUIRED_INPUT_MAX 20
  707. typedef struct
  708. {
  709. CLzmaProps prop;
  710. CLzmaProb *probs;
  711. Byte *dic;
  712. const Byte *buf;
  713. UInt32 range, code;
  714. SizeT dicPos;
  715. SizeT dicBufSize;
  716. UInt32 processedPos;
  717. UInt32 checkDicSize;
  718. unsigned state;
  719. UInt32 reps[4];
  720. unsigned remainLen;
  721. int needFlush;
  722. int needInitState;
  723. UInt32 numProbs;
  724. unsigned tempBufSize;
  725. Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
  726. } CLzmaDec;
  727. #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
  728. static void LzmaDec_Init(CLzmaDec *p);
  729. /* There are two types of LZMA streams:
  730. 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
  731. 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
  732. typedef enum
  733. {
  734. LZMA_FINISH_ANY, /* finish at any point */
  735. LZMA_FINISH_END /* block must be finished at the end */
  736. } ELzmaFinishMode;
  737. /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
  738. You must use LZMA_FINISH_END, when you know that current output buffer
  739. covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
  740. If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
  741. and output value of destLen will be less than output buffer size limit.
  742. You can check status result also.
  743. You can use multiple checks to test data integrity after full decompression:
  744. 1) Check Result and "status" variable.
  745. 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
  746. 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
  747. You must use correct finish mode in that case. */
  748. typedef enum
  749. {
  750. LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
  751. LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
  752. LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
  753. LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
  754. LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
  755. } ELzmaStatus;
  756. /* ELzmaStatus is used only as output value for function call */
  757. /* ---------- Interfaces ---------- */
  758. /* There are 3 levels of interfaces:
  759. 1) Dictionary Interface
  760. 2) Buffer Interface
  761. 3) One Call Interface
  762. You can select any of these interfaces, but don't mix functions from different
  763. groups for same object. */
  764. /* There are two variants to allocate state for Dictionary Interface:
  765. 1) LzmaDec_Allocate / LzmaDec_Free
  766. 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
  767. You can use variant 2, if you set dictionary buffer manually.
  768. For Buffer Interface you must always use variant 1.
  769. LzmaDec_Allocate* can return:
  770. SZ_OK
  771. SZ_ERROR_MEM - Memory allocation error
  772. SZ_ERROR_UNSUPPORTED - Unsupported properties
  773. */
  774. static SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
  775. static void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
  776. /* ---------- Dictionary Interface ---------- */
  777. /* You can use it, if you want to eliminate the overhead for data copying from
  778. dictionary to some other external buffer.
  779. You must work with CLzmaDec variables directly in this interface.
  780. STEPS:
  781. LzmaDec_Constr()
  782. LzmaDec_Allocate()
  783. for (each new stream)
  784. {
  785. LzmaDec_Init()
  786. while (it needs more decompression)
  787. {
  788. LzmaDec_DecodeToDic()
  789. use data from CLzmaDec::dic and update CLzmaDec::dicPos
  790. }
  791. }
  792. LzmaDec_Free()
  793. */
  794. /* LzmaDec_DecodeToDic
  795. The decoding to internal dictionary buffer (CLzmaDec::dic).
  796. You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
  797. finishMode:
  798. It has meaning only if the decoding reaches output limit (dicLimit).
  799. LZMA_FINISH_ANY - Decode just dicLimit bytes.
  800. LZMA_FINISH_END - Stream must be finished after dicLimit.
  801. Returns:
  802. SZ_OK
  803. status:
  804. LZMA_STATUS_FINISHED_WITH_MARK
  805. LZMA_STATUS_NOT_FINISHED
  806. LZMA_STATUS_NEEDS_MORE_INPUT
  807. LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
  808. SZ_ERROR_DATA - Data error
  809. */
  810. static SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
  811. const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
  812. EXTERN_C_END
  813. #endif
  814. /* Lzma2Dec.h -- LZMA2 Decoder
  815. 2015-05-13 : Igor Pavlov : Public domain */
  816. #ifndef __LZMA2_DEC_H
  817. #define __LZMA2_DEC_H
  818. /*#include "LzmaDec.h"*/
  819. EXTERN_C_BEGIN
  820. /* ---------- State Interface ---------- */
  821. typedef struct
  822. {
  823. CLzmaDec decoder;
  824. UInt32 packSize;
  825. UInt32 unpackSize;
  826. unsigned state;
  827. Byte control;
  828. Bool needInitDic;
  829. Bool needInitState;
  830. Bool needInitProp;
  831. } CLzma2Dec;
  832. #define Lzma2Dec_Construct(p) LzmaDec_Construct(&(p)->decoder)
  833. #define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc);
  834. #define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc);
  835. static SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAlloc *alloc);
  836. static void Lzma2Dec_Init(CLzma2Dec *p);
  837. /*
  838. finishMode:
  839. It has meaning only if the decoding reaches output limit (*destLen or dicLimit).
  840. LZMA_FINISH_ANY - use smallest number of input bytes
  841. LZMA_FINISH_END - read EndOfStream marker after decoding
  842. Returns:
  843. SZ_OK
  844. status:
  845. LZMA_STATUS_FINISHED_WITH_MARK
  846. LZMA_STATUS_NOT_FINISHED
  847. LZMA_STATUS_NEEDS_MORE_INPUT
  848. SZ_ERROR_DATA - Data error
  849. */
  850. static SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
  851. const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
  852. EXTERN_C_END
  853. #endif
  854. /* END HEADERS */
  855. /* 7zCrc.c -- CRC32 init
  856. 2015-03-10 : Igor Pavlov : Public domain */
  857. /*
  858. #include "Precomp.h"
  859. #include "7zCrc.h"
  860. #include "CpuArch.h"
  861. */
  862. #define UNUSED_VAR(x) (void)x;
  863. #define kCrcPoly 0xEDB88320
  864. #ifdef MY_CPU_LE
  865. #define CRC_NUM_TABLES 8
  866. #else
  867. #define CRC_NUM_TABLES 9
  868. #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
  869. static UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  870. static UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
  871. #endif
  872. #ifndef MY_CPU_BE
  873. static UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  874. static UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
  875. #endif
  876. typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table);
  877. static CRC_FUNC g_CrcUpdateT4;
  878. static CRC_FUNC g_CrcUpdateT8;
  879. static CRC_FUNC g_CrcUpdate;
  880. static UInt32 g_CrcTable[256 * CRC_NUM_TABLES];
  881. static UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
  882. {
  883. return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL;
  884. }
  885. #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
  886. #if CRC_NUM_TABLES < 4
  887. static UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table)
  888. {
  889. const Byte *p = (const Byte *)data;
  890. const Byte *pEnd = p + size;
  891. for (; p != pEnd; p++)
  892. v = CRC_UPDATE_BYTE_2(v, *p);
  893. return v;
  894. }
  895. #endif
  896. static void MY_FAST_CALL CrcGenerateTable()
  897. {
  898. UInt32 i;
  899. for (i = 0; i < 256; i++)
  900. {
  901. UInt32 r = i;
  902. unsigned j;
  903. for (j = 0; j < 8; j++)
  904. r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
  905. g_CrcTable[i] = r;
  906. }
  907. for (; i < 256 * CRC_NUM_TABLES; i++)
  908. {
  909. UInt32 r = g_CrcTable[i - 256];
  910. g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8);
  911. }
  912. #if CRC_NUM_TABLES < 4
  913. g_CrcUpdate = CrcUpdateT1;
  914. #else
  915. #ifdef MY_CPU_LE
  916. g_CrcUpdateT4 = CrcUpdateT4;
  917. g_CrcUpdate = CrcUpdateT4;
  918. #if CRC_NUM_TABLES >= 8
  919. g_CrcUpdateT8 = CrcUpdateT8;
  920. #ifdef MY_CPU_X86_OR_AMD64
  921. if (!CPU_Is_InOrder())
  922. g_CrcUpdate = CrcUpdateT8;
  923. #endif
  924. #endif
  925. #else
  926. {
  927. #ifndef MY_CPU_BE
  928. UInt32 k = 0x01020304;
  929. const Byte *p = (const Byte *)&k;
  930. if (p[0] == 4 && p[1] == 3)
  931. {
  932. g_CrcUpdateT4 = CrcUpdateT4;
  933. g_CrcUpdate = CrcUpdateT4;
  934. #if CRC_NUM_TABLES >= 8
  935. g_CrcUpdateT8 = CrcUpdateT8;
  936. /* g_CrcUpdate = CrcUpdateT8; */
  937. #endif
  938. }
  939. else if (p[0] != 1 || p[1] != 2)
  940. g_CrcUpdate = CrcUpdateT1;
  941. else
  942. #endif
  943. {
  944. for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
  945. {
  946. UInt32 x = g_CrcTable[i - 256];
  947. g_CrcTable[i] = CRC_UINT32_SWAP(x);
  948. }
  949. g_CrcUpdateT4 = CrcUpdateT1_BeT4;
  950. g_CrcUpdate = CrcUpdateT1_BeT4;
  951. #if CRC_NUM_TABLES >= 8
  952. g_CrcUpdateT8 = CrcUpdateT1_BeT8;
  953. /* g_CrcUpdate = CrcUpdateT1_BeT8; */
  954. #endif
  955. }
  956. }
  957. #endif
  958. #endif
  959. }
  960. /* 7zCrcOpt.c -- CRC32 calculation
  961. 2015-03-01 : Igor Pavlov : Public domain */
  962. /*
  963. #include "Precomp.h"
  964. #include "CpuArch.h"
  965. */
  966. #ifndef MY_CPU_BE
  967. #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
  968. static UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
  969. {
  970. const Byte *p = (const Byte *)data;
  971. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
  972. v = CRC_UPDATE_BYTE_2(v, *p);
  973. for (; size >= 4; size -= 4, p += 4)
  974. {
  975. v ^= *(const UInt32 *)p;
  976. v =
  977. table[0x300 + ((v ) & 0xFF)]
  978. ^ table[0x200 + ((v >> 8) & 0xFF)]
  979. ^ table[0x100 + ((v >> 16) & 0xFF)]
  980. ^ table[0x000 + ((v >> 24))];
  981. }
  982. for (; size > 0; size--, p++)
  983. v = CRC_UPDATE_BYTE_2(v, *p);
  984. return v;
  985. }
  986. static UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
  987. {
  988. const Byte *p = (const Byte *)data;
  989. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
  990. v = CRC_UPDATE_BYTE_2(v, *p);
  991. for (; size >= 8; size -= 8, p += 8)
  992. {
  993. UInt32 d;
  994. v ^= *(const UInt32 *)p;
  995. v =
  996. table[0x700 + ((v ) & 0xFF)]
  997. ^ table[0x600 + ((v >> 8) & 0xFF)]
  998. ^ table[0x500 + ((v >> 16) & 0xFF)]
  999. ^ table[0x400 + ((v >> 24))];
  1000. d = *((const UInt32 *)p + 1);
  1001. v ^=
  1002. table[0x300 + ((d ) & 0xFF)]
  1003. ^ table[0x200 + ((d >> 8) & 0xFF)]
  1004. ^ table[0x100 + ((d >> 16) & 0xFF)]
  1005. ^ table[0x000 + ((d >> 24))];
  1006. }
  1007. for (; size > 0; size--, p++)
  1008. v = CRC_UPDATE_BYTE_2(v, *p);
  1009. return v;
  1010. }
  1011. #endif
  1012. #ifndef MY_CPU_LE
  1013. #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
  1014. #define CRC_UPDATE_BYTE_2_BE(crc, b) (table[(((crc) >> 24) ^ (b))] ^ ((crc) << 8))
  1015. static UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
  1016. {
  1017. const Byte *p = (const Byte *)data;
  1018. table += 0x100;
  1019. v = CRC_UINT32_SWAP(v);
  1020. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
  1021. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  1022. for (; size >= 4; size -= 4, p += 4)
  1023. {
  1024. v ^= *(const UInt32 *)p;
  1025. v =
  1026. table[0x000 + ((v ) & 0xFF)]
  1027. ^ table[0x100 + ((v >> 8) & 0xFF)]
  1028. ^ table[0x200 + ((v >> 16) & 0xFF)]
  1029. ^ table[0x300 + ((v >> 24))];
  1030. }
  1031. for (; size > 0; size--, p++)
  1032. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  1033. return CRC_UINT32_SWAP(v);
  1034. }
  1035. static UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
  1036. {
  1037. const Byte *p = (const Byte *)data;
  1038. table += 0x100;
  1039. v = CRC_UINT32_SWAP(v);
  1040. for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
  1041. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  1042. for (; size >= 8; size -= 8, p += 8)
  1043. {
  1044. UInt32 d;
  1045. v ^= *(const UInt32 *)p;
  1046. v =
  1047. table[0x400 + ((v ) & 0xFF)]
  1048. ^ table[0x500 + ((v >> 8) & 0xFF)]
  1049. ^ table[0x600 + ((v >> 16) & 0xFF)]
  1050. ^ table[0x700 + ((v >> 24))];
  1051. d = *((const UInt32 *)p + 1);
  1052. v ^=
  1053. table[0x000 + ((d ) & 0xFF)]
  1054. ^ table[0x100 + ((d >> 8) & 0xFF)]
  1055. ^ table[0x200 + ((d >> 16) & 0xFF)]
  1056. ^ table[0x300 + ((d >> 24))];
  1057. }
  1058. for (; size > 0; size--, p++)
  1059. v = CRC_UPDATE_BYTE_2_BE(v, *p);
  1060. return CRC_UINT32_SWAP(v);
  1061. }
  1062. #endif
  1063. /* CpuArch.c -- CPU specific code
  1064. 2016-02-25: Igor Pavlov : Public domain */
  1065. /*
  1066. #include "Precomp.h"
  1067. #include "CpuArch.h"
  1068. */
  1069. #ifdef MY_CPU_X86_OR_AMD64
  1070. #if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
  1071. #define USE_ASM
  1072. #endif
  1073. #if !defined(USE_ASM) && _MSC_VER >= 1500
  1074. #include <intrin.h>
  1075. #endif
  1076. #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
  1077. static UInt32 CheckFlag(UInt32 flag)
  1078. {
  1079. #ifdef _MSC_VER
  1080. __asm pushfd;
  1081. __asm pop EAX;
  1082. __asm mov EDX, EAX;
  1083. __asm xor EAX, flag;
  1084. __asm push EAX;
  1085. __asm popfd;
  1086. __asm pushfd;
  1087. __asm pop EAX;
  1088. __asm xor EAX, EDX;
  1089. __asm push EDX;
  1090. __asm popfd;
  1091. __asm and flag, EAX;
  1092. #else
  1093. __asm__ __volatile__ (
  1094. "pushf\n\t"
  1095. "pop %%EAX\n\t"
  1096. "movl %%EAX,%%EDX\n\t"
  1097. "xorl %0,%%EAX\n\t"
  1098. "push %%EAX\n\t"
  1099. "popf\n\t"
  1100. "pushf\n\t"
  1101. "pop %%EAX\n\t"
  1102. "xorl %%EDX,%%EAX\n\t"
  1103. "push %%EDX\n\t"
  1104. "popf\n\t"
  1105. "andl %%EAX, %0\n\t":
  1106. "=c" (flag) : "c" (flag) :
  1107. "%eax", "%edx");
  1108. #endif
  1109. return flag;
  1110. }
  1111. #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
  1112. #else
  1113. #define CHECK_CPUID_IS_SUPPORTED
  1114. #endif
  1115. #if defined(__WATCOMC__)
  1116. static void __cpuid(int *cpuinfo, const UInt32 infotype);
  1117. #pragma aux __cpuid = \
  1118. ".586" \
  1119. "cpuid" \
  1120. "mov [esi+0],eax" \
  1121. "mov [esi+4],ebx" \
  1122. "mov [esi+8],ecx" \
  1123. "mov [esi+12],edx" \
  1124. parm [esi] [eax] modify [ebx ecx edx];
  1125. #endif
  1126. static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
  1127. {
  1128. #ifdef USE_ASM
  1129. #ifdef _MSC_VER
  1130. UInt32 a2, b2, c2, d2;
  1131. __asm xor EBX, EBX;
  1132. __asm xor ECX, ECX;
  1133. __asm xor EDX, EDX;
  1134. __asm mov EAX, function;
  1135. __asm cpuid;
  1136. __asm mov a2, EAX;
  1137. __asm mov b2, EBX;
  1138. __asm mov c2, ECX;
  1139. __asm mov d2, EDX;
  1140. *a = a2;
  1141. *b = b2;
  1142. *c = c2;
  1143. *d = d2;
  1144. #else
  1145. __asm__ __volatile__ (
  1146. #if defined(MY_CPU_AMD64) && defined(__PIC__)
  1147. "mov %%rbx, %%rdi;"
  1148. "cpuid;"
  1149. "xchg %%rbx, %%rdi;"
  1150. : "=a" (*a) ,
  1151. "=D" (*b) ,
  1152. #elif defined(MY_CPU_X86) && defined(__PIC__)
  1153. "mov %%ebx, %%edi;"
  1154. "cpuid;"
  1155. "xchgl %%ebx, %%edi;"
  1156. : "=a" (*a) ,
  1157. "=D" (*b) ,
  1158. #else
  1159. "cpuid"
  1160. : "=a" (*a) ,
  1161. "=b" (*b) ,
  1162. #endif
  1163. "=c" (*c) ,
  1164. "=d" (*d)
  1165. : "0" (function)) ;
  1166. #endif
  1167. #else
  1168. int CPUInfo[4];
  1169. __cpuid(CPUInfo, function);
  1170. *a = CPUInfo[0];
  1171. *b = CPUInfo[1];
  1172. *c = CPUInfo[2];
  1173. *d = CPUInfo[3];
  1174. #endif
  1175. }
  1176. static Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
  1177. {
  1178. CHECK_CPUID_IS_SUPPORTED
  1179. MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
  1180. MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
  1181. return True;
  1182. }
  1183. static const UInt32 kVendors[][3] =
  1184. {
  1185. { 0x756E6547, 0x49656E69, 0x6C65746E},
  1186. { 0x68747541, 0x69746E65, 0x444D4163},
  1187. { 0x746E6543, 0x48727561, 0x736C7561}
  1188. };
  1189. static int x86cpuid_GetFirm(const Cx86cpuid *p)
  1190. {
  1191. unsigned i;
  1192. for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
  1193. {
  1194. const UInt32 *v = kVendors[i];
  1195. if (v[0] == p->vendor[0] &&
  1196. v[1] == p->vendor[1] &&
  1197. v[2] == p->vendor[2])
  1198. return (int)i;
  1199. }
  1200. return -1;
  1201. }
  1202. static Bool CPU_Is_InOrder()
  1203. {
  1204. Cx86cpuid p;
  1205. int firm;
  1206. UInt32 family, model;
  1207. if (!x86cpuid_CheckAndRead(&p))
  1208. return True;
  1209. family = x86cpuid_GetFamily(p.ver);
  1210. model = x86cpuid_GetModel(p.ver);
  1211. firm = x86cpuid_GetFirm(&p);
  1212. switch (firm)
  1213. {
  1214. case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && (
  1215. /* In-Order Atom CPU */
  1216. model == 0x1C /* 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330 */
  1217. || model == 0x26 /* 45 nm, Z6xx */
  1218. || model == 0x27 /* 32 nm, Z2460 */
  1219. || model == 0x35 /* 32 nm, Z2760 */
  1220. || model == 0x36 /* 32 nm, N2xxx, D2xxx */
  1221. )));
  1222. case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
  1223. case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
  1224. }
  1225. return True;
  1226. }
  1227. #endif
  1228. /* 7zStream.c -- 7z Stream functions
  1229. 2013-11-12 : Igor Pavlov : Public domain */
  1230. /*#include "Precomp.h"*/
  1231. #include <string.h>
  1232. /*#include "7zTypes.h"*/
  1233. static SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
  1234. {
  1235. Int64 t = offset;
  1236. return stream->Seek(stream, &t, SZ_SEEK_SET);
  1237. }
  1238. static SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
  1239. {
  1240. while (size != 0)
  1241. {
  1242. size_t processed = size;
  1243. RINOK(stream->Read(stream, buf, &processed));
  1244. if (processed == 0)
  1245. return errorType;
  1246. buf = (void *)((Byte *)buf + processed);
  1247. size -= processed;
  1248. }
  1249. return SZ_OK;
  1250. }
  1251. static SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size)
  1252. {
  1253. return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
  1254. }
  1255. static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size)
  1256. {
  1257. SRes res = SZ_OK;
  1258. CLookToRead *p = (CLookToRead *)pp;
  1259. size_t size2 = p->size - p->pos;
  1260. if (size2 == 0 && *size > 0)
  1261. {
  1262. p->pos = 0;
  1263. size2 = LookToRead_BUF_SIZE;
  1264. res = p->realStream->Read(p->realStream, p->buf, &size2);
  1265. p->size = size2;
  1266. }
  1267. if (size2 < *size)
  1268. *size = size2;
  1269. *buf = p->buf + p->pos;
  1270. return res;
  1271. }
  1272. static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size)
  1273. {
  1274. SRes res = SZ_OK;
  1275. CLookToRead *p = (CLookToRead *)pp;
  1276. size_t size2 = p->size - p->pos;
  1277. if (size2 == 0 && *size > 0)
  1278. {
  1279. p->pos = 0;
  1280. if (*size > LookToRead_BUF_SIZE)
  1281. *size = LookToRead_BUF_SIZE;
  1282. res = p->realStream->Read(p->realStream, p->buf, size);
  1283. size2 = p->size = *size;
  1284. }
  1285. if (size2 < *size)
  1286. *size = size2;
  1287. *buf = p->buf + p->pos;
  1288. return res;
  1289. }
  1290. static SRes LookToRead_Skip(void *pp, size_t offset)
  1291. {
  1292. CLookToRead *p = (CLookToRead *)pp;
  1293. p->pos += offset;
  1294. return SZ_OK;
  1295. }
  1296. static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
  1297. {
  1298. CLookToRead *p = (CLookToRead *)pp;
  1299. size_t rem = p->size - p->pos;
  1300. if (rem == 0)
  1301. return p->realStream->Read(p->realStream, buf, size);
  1302. if (rem > *size)
  1303. rem = *size;
  1304. memcpy(buf, p->buf + p->pos, rem);
  1305. p->pos += rem;
  1306. *size = rem;
  1307. return SZ_OK;
  1308. }
  1309. static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
  1310. {
  1311. CLookToRead *p = (CLookToRead *)pp;
  1312. p->pos = p->size = 0;
  1313. return p->realStream->Seek(p->realStream, pos, origin);
  1314. }
  1315. static void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
  1316. {
  1317. p->s.Look = lookahead ?
  1318. LookToRead_Look_Lookahead :
  1319. LookToRead_Look_Exact;
  1320. p->s.Skip = LookToRead_Skip;
  1321. p->s.Read = LookToRead_Read;
  1322. p->s.Seek = LookToRead_Seek;
  1323. }
  1324. static void LookToRead_Init(CLookToRead *p)
  1325. {
  1326. p->pos = p->size = 0;
  1327. }
  1328. /* 7zArcIn.c -- 7z Input functions
  1329. 2016-05-16 : Igor Pavlov : Public domain */
  1330. /*
  1331. #include "Precomp.h"
  1332. #include <string.h>
  1333. #include "7z.h"
  1334. #include "7zBuf.h"
  1335. #include "7zCrc.h"
  1336. #include "CpuArch.h"
  1337. */
  1338. #define MY_ALLOC(T, p, size, alloc) { \
  1339. if ((p = (T *)IAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; }
  1340. #define MY_ALLOC_ZE(T, p, size, alloc) { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) }
  1341. #define MY_ALLOC_AND_CPY(to, size, from, alloc) \
  1342. { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); }
  1343. #define MY_ALLOC_ZE_AND_CPY(to, size, from, alloc) \
  1344. { if ((size) == 0) p = NULL; else { MY_ALLOC_AND_CPY(to, size, from, alloc) } }
  1345. #define k7zMajorVersion 0
  1346. enum EIdEnum
  1347. {
  1348. k7zIdEnd,
  1349. k7zIdHeader,
  1350. k7zIdArchiveProperties,
  1351. k7zIdAdditionalStreamsInfo,
  1352. k7zIdMainStreamsInfo,
  1353. k7zIdFilesInfo,
  1354. k7zIdPackInfo,
  1355. k7zIdUnpackInfo,
  1356. k7zIdSubStreamsInfo,
  1357. k7zIdSize,
  1358. k7zIdCRC,
  1359. k7zIdFolder,
  1360. k7zIdCodersUnpackSize,
  1361. k7zIdNumUnpackStream,
  1362. k7zIdEmptyStream,
  1363. k7zIdEmptyFile,
  1364. k7zIdAnti,
  1365. k7zIdName,
  1366. k7zIdCTime,
  1367. k7zIdATime,
  1368. k7zIdMTime,
  1369. k7zIdWinAttrib,
  1370. k7zIdComment,
  1371. k7zIdEncodedHeader,
  1372. k7zIdStartPos,
  1373. k7zIdDummy
  1374. /* k7zNtSecure, */
  1375. /* k7zParent, */
  1376. /* k7zIsReal */
  1377. };
  1378. static const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  1379. #define SzBitUi32s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
  1380. static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAlloc *alloc)
  1381. {
  1382. if (num == 0)
  1383. {
  1384. p->Defs = NULL;
  1385. p->Vals = NULL;
  1386. }
  1387. else
  1388. {
  1389. MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc);
  1390. MY_ALLOC(UInt32, p->Vals, num, alloc);
  1391. }
  1392. return SZ_OK;
  1393. }
  1394. static void SzBitUi32s_Free(CSzBitUi32s *p, ISzAlloc *alloc)
  1395. {
  1396. IAlloc_Free(alloc, p->Defs); p->Defs = NULL;
  1397. IAlloc_Free(alloc, p->Vals); p->Vals = NULL;
  1398. }
  1399. #define SzBitUi64s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
  1400. static void SzBitUi64s_Free(CSzBitUi64s *p, ISzAlloc *alloc)
  1401. {
  1402. IAlloc_Free(alloc, p->Defs); p->Defs = NULL;
  1403. IAlloc_Free(alloc, p->Vals); p->Vals = NULL;
  1404. }
  1405. static void SzAr_Init(CSzAr *p)
  1406. {
  1407. p->NumPackStreams = 0;
  1408. p->NumFolders = 0;
  1409. p->PackPositions = NULL;
  1410. SzBitUi32s_Init(&p->FolderCRCs);
  1411. p->FoCodersOffsets = NULL;
  1412. p->FoStartPackStreamIndex = NULL;
  1413. p->FoToCoderUnpackSizes = NULL;
  1414. p->FoToMainUnpackSizeIndex = NULL;
  1415. p->CoderUnpackSizes = NULL;
  1416. p->CodersData = NULL;
  1417. }
  1418. static void SzAr_Free(CSzAr *p, ISzAlloc *alloc)
  1419. {
  1420. IAlloc_Free(alloc, p->PackPositions);
  1421. SzBitUi32s_Free(&p->FolderCRCs, alloc);
  1422. IAlloc_Free(alloc, p->FoCodersOffsets);
  1423. IAlloc_Free(alloc, p->FoStartPackStreamIndex);
  1424. IAlloc_Free(alloc, p->FoToCoderUnpackSizes);
  1425. IAlloc_Free(alloc, p->FoToMainUnpackSizeIndex);
  1426. IAlloc_Free(alloc, p->CoderUnpackSizes);
  1427. IAlloc_Free(alloc, p->CodersData);
  1428. SzAr_Init(p);
  1429. }
  1430. static void SzArEx_Init(CSzArEx *p)
  1431. {
  1432. SzAr_Init(&p->db);
  1433. p->NumFiles = 0;
  1434. p->dataPos = 0;
  1435. p->UnpackPositions = NULL;
  1436. p->IsDirs = NULL;
  1437. p->FolderToFile = NULL;
  1438. p->FileToFolder = NULL;
  1439. p->FileNameOffsets = NULL;
  1440. p->FileNames = NULL;
  1441. SzBitUi32s_Init(&p->CRCs);
  1442. SzBitUi32s_Init(&p->Attribs);
  1443. /* SzBitUi32s_Init(&p->Parents); */
  1444. SzBitUi64s_Init(&p->MTime);
  1445. SzBitUi64s_Init(&p->CTime);
  1446. }
  1447. static void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc)
  1448. {
  1449. IAlloc_Free(alloc, p->UnpackPositions);
  1450. IAlloc_Free(alloc, p->IsDirs);
  1451. IAlloc_Free(alloc, p->FolderToFile);
  1452. IAlloc_Free(alloc, p->FileToFolder);
  1453. IAlloc_Free(alloc, p->FileNameOffsets);
  1454. IAlloc_Free(alloc, p->FileNames);
  1455. SzBitUi32s_Free(&p->CRCs, alloc);
  1456. SzBitUi32s_Free(&p->Attribs, alloc);
  1457. /* SzBitUi32s_Free(&p->Parents, alloc); */
  1458. SzBitUi64s_Free(&p->MTime, alloc);
  1459. SzBitUi64s_Free(&p->CTime, alloc);
  1460. SzAr_Free(&p->db, alloc);
  1461. SzArEx_Init(p);
  1462. }
  1463. static int TestSignatureCandidate(const Byte *testBytes)
  1464. {
  1465. unsigned i;
  1466. for (i = 0; i < k7zSignatureSize; i++)
  1467. if (testBytes[i] != k7zSignature[i])
  1468. return 0;
  1469. return 1;
  1470. }
  1471. #define SzData_Clear(p) { (p)->Data = NULL; (p)->Size = 0; }
  1472. #define SZ_READ_BYTE_SD(_sd_, dest) if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; (_sd_)->Size--; dest = *(_sd_)->Data++;
  1473. #define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest)
  1474. #define SZ_READ_BYTE_2(dest) if (sd.Size == 0) return SZ_ERROR_ARCHIVE; sd.Size--; dest = *sd.Data++;
  1475. #define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); }
  1476. #define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); }
  1477. #define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \
  1478. dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4);
  1479. static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value)
  1480. {
  1481. Byte firstByte, mask;
  1482. unsigned i;
  1483. UInt32 v;
  1484. SZ_READ_BYTE(firstByte);
  1485. if ((firstByte & 0x80) == 0)
  1486. {
  1487. *value = firstByte;
  1488. return SZ_OK;
  1489. }
  1490. SZ_READ_BYTE(v);
  1491. if ((firstByte & 0x40) == 0)
  1492. {
  1493. *value = (((UInt32)firstByte & 0x3F) << 8) | v;
  1494. return SZ_OK;
  1495. }
  1496. SZ_READ_BYTE(mask);
  1497. *value = v | ((UInt32)mask << 8);
  1498. mask = 0x20;
  1499. for (i = 2; i < 8; i++)
  1500. {
  1501. Byte b;
  1502. if ((firstByte & mask) == 0)
  1503. {
  1504. UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1);
  1505. *value |= (highPart << (8 * i));
  1506. return SZ_OK;
  1507. }
  1508. SZ_READ_BYTE(b);
  1509. *value |= ((UInt64)b << (8 * i));
  1510. mask >>= 1;
  1511. }
  1512. return SZ_OK;
  1513. }
  1514. static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value)
  1515. {
  1516. Byte firstByte;
  1517. UInt64 value64;
  1518. if (sd->Size == 0)
  1519. return SZ_ERROR_ARCHIVE;
  1520. firstByte = *sd->Data;
  1521. if ((firstByte & 0x80) == 0)
  1522. {
  1523. *value = firstByte;
  1524. sd->Data++;
  1525. sd->Size--;
  1526. return SZ_OK;
  1527. }
  1528. RINOK(ReadNumber(sd, &value64));
  1529. if (value64 >= (UInt32)0x80000000 - 1)
  1530. return SZ_ERROR_UNSUPPORTED;
  1531. if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4)))
  1532. return SZ_ERROR_UNSUPPORTED;
  1533. *value = (UInt32)value64;
  1534. return SZ_OK;
  1535. }
  1536. #define ReadID(sd, value) ReadNumber(sd, value)
  1537. static SRes SkipData(CSzData *sd)
  1538. {
  1539. UInt64 size;
  1540. RINOK(ReadNumber(sd, &size));
  1541. if (size > sd->Size)
  1542. return SZ_ERROR_ARCHIVE;
  1543. SKIP_DATA(sd, size);
  1544. return SZ_OK;
  1545. }
  1546. static SRes WaitId(CSzData *sd, UInt32 id)
  1547. {
  1548. for (;;)
  1549. {
  1550. UInt64 type;
  1551. RINOK(ReadID(sd, &type));
  1552. if (type == id)
  1553. return SZ_OK;
  1554. if (type == k7zIdEnd)
  1555. return SZ_ERROR_ARCHIVE;
  1556. RINOK(SkipData(sd));
  1557. }
  1558. }
  1559. static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v)
  1560. {
  1561. UInt32 numBytes = (numItems + 7) >> 3;
  1562. if (numBytes > sd->Size)
  1563. return SZ_ERROR_ARCHIVE;
  1564. *v = sd->Data;
  1565. SKIP_DATA(sd, numBytes);
  1566. return SZ_OK;
  1567. }
  1568. static UInt32 CountDefinedBits(const Byte *bits, UInt32 numItems)
  1569. {
  1570. Byte b = 0;
  1571. unsigned m = 0;
  1572. UInt32 sum = 0;
  1573. for (; numItems != 0; numItems--)
  1574. {
  1575. if (m == 0)
  1576. {
  1577. b = *bits++;
  1578. m = 8;
  1579. }
  1580. m--;
  1581. sum += ((b >> m) & 1);
  1582. }
  1583. return sum;
  1584. }
  1585. static MY_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAlloc *alloc)
  1586. {
  1587. Byte allAreDefined;
  1588. Byte *v2;
  1589. UInt32 numBytes = (numItems + 7) >> 3;
  1590. *v = NULL;
  1591. SZ_READ_BYTE(allAreDefined);
  1592. if (numBytes == 0)
  1593. return SZ_OK;
  1594. if (allAreDefined == 0)
  1595. {
  1596. if (numBytes > sd->Size)
  1597. return SZ_ERROR_ARCHIVE;
  1598. MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc);
  1599. SKIP_DATA(sd, numBytes);
  1600. return SZ_OK;
  1601. }
  1602. MY_ALLOC(Byte, *v, numBytes, alloc);
  1603. v2 = *v;
  1604. memset(v2, 0xFF, (size_t)numBytes);
  1605. {
  1606. unsigned numBits = (unsigned)numItems & 7;
  1607. if (numBits != 0)
  1608. v2[numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits));
  1609. }
  1610. return SZ_OK;
  1611. }
  1612. static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAlloc *alloc)
  1613. {
  1614. UInt32 i;
  1615. CSzData sd;
  1616. UInt32 *vals;
  1617. const Byte *defs;
  1618. MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc);
  1619. sd = *sd2;
  1620. defs = crcs->Defs;
  1621. vals = crcs->Vals;
  1622. for (i = 0; i < numItems; i++)
  1623. if (SzBitArray_Check(defs, i))
  1624. {
  1625. SZ_READ_32(vals[i]);
  1626. }
  1627. else
  1628. vals[i] = 0;
  1629. *sd2 = sd;
  1630. return SZ_OK;
  1631. }
  1632. static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAlloc *alloc)
  1633. {
  1634. SzBitUi32s_Free(crcs, alloc);
  1635. RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc));
  1636. return ReadUi32s(sd, numItems, crcs, alloc);
  1637. }
  1638. static SRes SkipBitUi32s(CSzData *sd, UInt32 numItems)
  1639. {
  1640. Byte allAreDefined;
  1641. UInt32 numDefined = numItems;
  1642. SZ_READ_BYTE(allAreDefined);
  1643. if (!allAreDefined)
  1644. {
  1645. size_t numBytes = (numItems + 7) >> 3;
  1646. if (numBytes > sd->Size)
  1647. return SZ_ERROR_ARCHIVE;
  1648. numDefined = CountDefinedBits(sd->Data, numItems);
  1649. SKIP_DATA(sd, numBytes);
  1650. }
  1651. if (numDefined > (sd->Size >> 2))
  1652. return SZ_ERROR_ARCHIVE;
  1653. SKIP_DATA(sd, (size_t)numDefined * 4);
  1654. return SZ_OK;
  1655. }
  1656. static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAlloc *alloc)
  1657. {
  1658. RINOK(SzReadNumber32(sd, &p->NumPackStreams));
  1659. RINOK(WaitId(sd, k7zIdSize));
  1660. MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc);
  1661. {
  1662. UInt64 sum = 0;
  1663. UInt32 i;
  1664. UInt32 numPackStreams = p->NumPackStreams;
  1665. for (i = 0; i < numPackStreams; i++)
  1666. {
  1667. UInt64 packSize;
  1668. p->PackPositions[i] = sum;
  1669. RINOK(ReadNumber(sd, &packSize));
  1670. sum += packSize;
  1671. if (sum < packSize)
  1672. return SZ_ERROR_ARCHIVE;
  1673. }
  1674. p->PackPositions[i] = sum;
  1675. }
  1676. for (;;)
  1677. {
  1678. UInt64 type;
  1679. RINOK(ReadID(sd, &type));
  1680. if (type == k7zIdEnd)
  1681. return SZ_OK;
  1682. if (type == k7zIdCRC)
  1683. {
  1684. /* CRC of packed streams is unused now */
  1685. RINOK(SkipBitUi32s(sd, p->NumPackStreams));
  1686. continue;
  1687. }
  1688. RINOK(SkipData(sd));
  1689. }
  1690. }
  1691. /*
  1692. static SRes SzReadSwitch(CSzData *sd)
  1693. {
  1694. Byte external;
  1695. RINOK(SzReadByte(sd, &external));
  1696. return (external == 0) ? SZ_OK: SZ_ERROR_UNSUPPORTED;
  1697. }
  1698. */
  1699. #define k_NumCodersStreams_in_Folder_MAX (SZ_NUM_BONDS_IN_FOLDER_MAX + SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX)
  1700. static SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
  1701. {
  1702. UInt32 numCoders, i;
  1703. UInt32 numInStreams = 0;
  1704. const Byte *dataStart = sd->Data;
  1705. f->NumCoders = 0;
  1706. f->NumBonds = 0;
  1707. f->NumPackStreams = 0;
  1708. f->UnpackStream = 0;
  1709. RINOK(SzReadNumber32(sd, &numCoders));
  1710. if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX)
  1711. return SZ_ERROR_UNSUPPORTED;
  1712. for (i = 0; i < numCoders; i++)
  1713. {
  1714. Byte mainByte;
  1715. CSzCoderInfo *coder = f->Coders + i;
  1716. unsigned idSize, j;
  1717. UInt64 id;
  1718. SZ_READ_BYTE(mainByte);
  1719. if ((mainByte & 0xC0) != 0)
  1720. return SZ_ERROR_UNSUPPORTED;
  1721. idSize = (unsigned)(mainByte & 0xF);
  1722. if (idSize > sizeof(id))
  1723. return SZ_ERROR_UNSUPPORTED;
  1724. if (idSize > sd->Size)
  1725. return SZ_ERROR_ARCHIVE;
  1726. id = 0;
  1727. for (j = 0; j < idSize; j++)
  1728. {
  1729. id = ((id << 8) | *sd->Data);
  1730. sd->Data++;
  1731. sd->Size--;
  1732. }
  1733. if (id > UINT64_CONST(0xFFFFFFFF))
  1734. return SZ_ERROR_UNSUPPORTED;
  1735. coder->MethodID = (UInt32)id;
  1736. coder->NumStreams = 1;
  1737. coder->PropsOffset = 0;
  1738. coder->PropsSize = 0;
  1739. if ((mainByte & 0x10) != 0)
  1740. {
  1741. UInt32 numStreams;
  1742. RINOK(SzReadNumber32(sd, &numStreams));
  1743. if (numStreams > k_NumCodersStreams_in_Folder_MAX)
  1744. return SZ_ERROR_UNSUPPORTED;
  1745. coder->NumStreams = (Byte)numStreams;
  1746. RINOK(SzReadNumber32(sd, &numStreams));
  1747. if (numStreams != 1)
  1748. return SZ_ERROR_UNSUPPORTED;
  1749. }
  1750. numInStreams += coder->NumStreams;
  1751. if (numInStreams > k_NumCodersStreams_in_Folder_MAX)
  1752. return SZ_ERROR_UNSUPPORTED;
  1753. if ((mainByte & 0x20) != 0)
  1754. {
  1755. UInt32 propsSize = 0;
  1756. RINOK(SzReadNumber32(sd, &propsSize));
  1757. if (propsSize > sd->Size)
  1758. return SZ_ERROR_ARCHIVE;
  1759. if (propsSize >= 0x80)
  1760. return SZ_ERROR_UNSUPPORTED;
  1761. coder->PropsOffset = sd->Data - dataStart;
  1762. coder->PropsSize = (Byte)propsSize;
  1763. sd->Data += (size_t)propsSize;
  1764. sd->Size -= (size_t)propsSize;
  1765. }
  1766. }
  1767. /*
  1768. if (numInStreams == 1 && numCoders == 1)
  1769. {
  1770. f->NumPackStreams = 1;
  1771. f->PackStreams[0] = 0;
  1772. }
  1773. else
  1774. */
  1775. {
  1776. Byte streamUsed[k_NumCodersStreams_in_Folder_MAX];
  1777. UInt32 numBonds, numPackStreams;
  1778. numBonds = numCoders - 1;
  1779. if (numInStreams < numBonds)
  1780. return SZ_ERROR_ARCHIVE;
  1781. if (numBonds > SZ_NUM_BONDS_IN_FOLDER_MAX)
  1782. return SZ_ERROR_UNSUPPORTED;
  1783. f->NumBonds = numBonds;
  1784. numPackStreams = numInStreams - numBonds;
  1785. if (numPackStreams > SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX)
  1786. return SZ_ERROR_UNSUPPORTED;
  1787. f->NumPackStreams = numPackStreams;
  1788. for (i = 0; i < numInStreams; i++)
  1789. streamUsed[i] = False;
  1790. if (numBonds != 0)
  1791. {
  1792. Byte coderUsed[SZ_NUM_CODERS_IN_FOLDER_MAX];
  1793. for (i = 0; i < numCoders; i++)
  1794. coderUsed[i] = False;
  1795. for (i = 0; i < numBonds; i++)
  1796. {
  1797. CSzBond *bp = f->Bonds + i;
  1798. RINOK(SzReadNumber32(sd, &bp->InIndex));
  1799. if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex])
  1800. return SZ_ERROR_ARCHIVE;
  1801. streamUsed[bp->InIndex] = True;
  1802. RINOK(SzReadNumber32(sd, &bp->OutIndex));
  1803. if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex])
  1804. return SZ_ERROR_ARCHIVE;
  1805. coderUsed[bp->OutIndex] = True;
  1806. }
  1807. for (i = 0; i < numCoders; i++)
  1808. if (!coderUsed[i])
  1809. {
  1810. f->UnpackStream = i;
  1811. break;
  1812. }
  1813. if (i == numCoders)
  1814. return SZ_ERROR_ARCHIVE;
  1815. }
  1816. if (numPackStreams == 1)
  1817. {
  1818. for (i = 0; i < numInStreams; i++)
  1819. if (!streamUsed[i])
  1820. break;
  1821. if (i == numInStreams)
  1822. return SZ_ERROR_ARCHIVE;
  1823. f->PackStreams[0] = i;
  1824. }
  1825. else
  1826. for (i = 0; i < numPackStreams; i++)
  1827. {
  1828. UInt32 index;
  1829. RINOK(SzReadNumber32(sd, &index));
  1830. if (index >= numInStreams || streamUsed[index])
  1831. return SZ_ERROR_ARCHIVE;
  1832. streamUsed[index] = True;
  1833. f->PackStreams[i] = index;
  1834. }
  1835. }
  1836. f->NumCoders = numCoders;
  1837. return SZ_OK;
  1838. }
  1839. static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num)
  1840. {
  1841. CSzData sd;
  1842. sd = *sd2;
  1843. for (; num != 0; num--)
  1844. {
  1845. Byte firstByte, mask;
  1846. unsigned i;
  1847. SZ_READ_BYTE_2(firstByte);
  1848. if ((firstByte & 0x80) == 0)
  1849. continue;
  1850. if ((firstByte & 0x40) == 0)
  1851. {
  1852. if (sd.Size == 0)
  1853. return SZ_ERROR_ARCHIVE;
  1854. sd.Size--;
  1855. sd.Data++;
  1856. continue;
  1857. }
  1858. mask = 0x20;
  1859. for (i = 2; i < 8 && (firstByte & mask) != 0; i++)
  1860. mask >>= 1;
  1861. if (i > sd.Size)
  1862. return SZ_ERROR_ARCHIVE;
  1863. SKIP_DATA2(sd, i);
  1864. }
  1865. *sd2 = sd;
  1866. return SZ_OK;
  1867. }
  1868. #define k_Scan_NumCoders_MAX 64
  1869. #define k_Scan_NumCodersStreams_in_Folder_MAX 64
  1870. static SRes ReadUnpackInfo(CSzAr *p,
  1871. CSzData *sd2,
  1872. UInt32 numFoldersMax,
  1873. const CBuf *tempBufs, UInt32 numTempBufs,
  1874. ISzAlloc *alloc)
  1875. {
  1876. CSzData sd;
  1877. UInt32 fo, numFolders, numCodersOutStreams, packStreamIndex;
  1878. const Byte *startBufPtr;
  1879. Byte external;
  1880. RINOK(WaitId(sd2, k7zIdFolder));
  1881. RINOK(SzReadNumber32(sd2, &numFolders));
  1882. if (numFolders > numFoldersMax)
  1883. return SZ_ERROR_UNSUPPORTED;
  1884. p->NumFolders = numFolders;
  1885. SZ_READ_BYTE_SD(sd2, external);
  1886. if (external == 0)
  1887. sd = *sd2;
  1888. else
  1889. {
  1890. UInt32 index;
  1891. RINOK(SzReadNumber32(sd2, &index));
  1892. if (index >= numTempBufs)
  1893. return SZ_ERROR_ARCHIVE;
  1894. sd.Data = tempBufs[index].data;
  1895. sd.Size = tempBufs[index].size;
  1896. }
  1897. MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc);
  1898. MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc);
  1899. MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc);
  1900. MY_ALLOC(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc);
  1901. startBufPtr = sd.Data;
  1902. packStreamIndex = 0;
  1903. numCodersOutStreams = 0;
  1904. for (fo = 0; fo < numFolders; fo++)
  1905. {
  1906. UInt32 numCoders, ci, numInStreams = 0;
  1907. p->FoCodersOffsets[fo] = sd.Data - startBufPtr;
  1908. RINOK(SzReadNumber32(&sd, &numCoders));
  1909. if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX)
  1910. return SZ_ERROR_UNSUPPORTED;
  1911. for (ci = 0; ci < numCoders; ci++)
  1912. {
  1913. Byte mainByte;
  1914. unsigned idSize;
  1915. UInt32 coderInStreams;
  1916. SZ_READ_BYTE_2(mainByte);
  1917. if ((mainByte & 0xC0) != 0)
  1918. return SZ_ERROR_UNSUPPORTED;
  1919. idSize = (mainByte & 0xF);
  1920. if (idSize > 8)
  1921. return SZ_ERROR_UNSUPPORTED;
  1922. if (idSize > sd.Size)
  1923. return SZ_ERROR_ARCHIVE;
  1924. SKIP_DATA2(sd, idSize);
  1925. coderInStreams = 1;
  1926. if ((mainByte & 0x10) != 0)
  1927. {
  1928. UInt32 coderOutStreams;
  1929. RINOK(SzReadNumber32(&sd, &coderInStreams));
  1930. RINOK(SzReadNumber32(&sd, &coderOutStreams));
  1931. if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1)
  1932. return SZ_ERROR_UNSUPPORTED;
  1933. }
  1934. numInStreams += coderInStreams;
  1935. if ((mainByte & 0x20) != 0)
  1936. {
  1937. UInt32 propsSize;
  1938. RINOK(SzReadNumber32(&sd, &propsSize));
  1939. if (propsSize > sd.Size)
  1940. return SZ_ERROR_ARCHIVE;
  1941. SKIP_DATA2(sd, propsSize);
  1942. }
  1943. }
  1944. {
  1945. UInt32 indexOfMainStream = 0;
  1946. UInt32 numPackStreams = 1;
  1947. if (numCoders != 1 || numInStreams != 1)
  1948. {
  1949. Byte streamUsed[k_Scan_NumCodersStreams_in_Folder_MAX];
  1950. Byte coderUsed[k_Scan_NumCoders_MAX];
  1951. UInt32 i;
  1952. UInt32 numBonds = numCoders - 1;
  1953. if (numInStreams < numBonds)
  1954. return SZ_ERROR_ARCHIVE;
  1955. if (numInStreams > k_Scan_NumCodersStreams_in_Folder_MAX)
  1956. return SZ_ERROR_UNSUPPORTED;
  1957. for (i = 0; i < numInStreams; i++)
  1958. streamUsed[i] = False;
  1959. for (i = 0; i < numCoders; i++)
  1960. coderUsed[i] = False;
  1961. for (i = 0; i < numBonds; i++)
  1962. {
  1963. UInt32 index;
  1964. RINOK(SzReadNumber32(&sd, &index));
  1965. if (index >= numInStreams || streamUsed[index])
  1966. return SZ_ERROR_ARCHIVE;
  1967. streamUsed[index] = True;
  1968. RINOK(SzReadNumber32(&sd, &index));
  1969. if (index >= numCoders || coderUsed[index])
  1970. return SZ_ERROR_ARCHIVE;
  1971. coderUsed[index] = True;
  1972. }
  1973. numPackStreams = numInStreams - numBonds;
  1974. if (numPackStreams != 1)
  1975. for (i = 0; i < numPackStreams; i++)
  1976. {
  1977. UInt32 index;
  1978. RINOK(SzReadNumber32(&sd, &index));
  1979. if (index >= numInStreams || streamUsed[index])
  1980. return SZ_ERROR_ARCHIVE;
  1981. streamUsed[index] = True;
  1982. }
  1983. for (i = 0; i < numCoders; i++)
  1984. if (!coderUsed[i])
  1985. {
  1986. indexOfMainStream = i;
  1987. break;
  1988. }
  1989. if (i == numCoders)
  1990. return SZ_ERROR_ARCHIVE;
  1991. }
  1992. p->FoStartPackStreamIndex[fo] = packStreamIndex;
  1993. p->FoToCoderUnpackSizes[fo] = numCodersOutStreams;
  1994. p->FoToMainUnpackSizeIndex[fo] = (Byte)indexOfMainStream;
  1995. numCodersOutStreams += numCoders;
  1996. if (numCodersOutStreams < numCoders)
  1997. return SZ_ERROR_UNSUPPORTED;
  1998. if (numPackStreams > p->NumPackStreams - packStreamIndex)
  1999. return SZ_ERROR_ARCHIVE;
  2000. packStreamIndex += numPackStreams;
  2001. }
  2002. }
  2003. p->FoToCoderUnpackSizes[fo] = numCodersOutStreams;
  2004. {
  2005. size_t dataSize = sd.Data - startBufPtr;
  2006. p->FoStartPackStreamIndex[fo] = packStreamIndex;
  2007. p->FoCodersOffsets[fo] = dataSize;
  2008. MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc);
  2009. }
  2010. if (external != 0)
  2011. {
  2012. if (sd.Size != 0)
  2013. return SZ_ERROR_ARCHIVE;
  2014. sd = *sd2;
  2015. }
  2016. RINOK(WaitId(&sd, k7zIdCodersUnpackSize));
  2017. MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc);
  2018. {
  2019. UInt32 i;
  2020. for (i = 0; i < numCodersOutStreams; i++)
  2021. {
  2022. RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i));
  2023. }
  2024. }
  2025. for (;;)
  2026. {
  2027. UInt64 type;
  2028. RINOK(ReadID(&sd, &type));
  2029. if (type == k7zIdEnd)
  2030. {
  2031. *sd2 = sd;
  2032. return SZ_OK;
  2033. }
  2034. if (type == k7zIdCRC)
  2035. {
  2036. RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc));
  2037. continue;
  2038. }
  2039. RINOK(SkipData(&sd));
  2040. }
  2041. }
  2042. static UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex)
  2043. {
  2044. return p->CoderUnpackSizes[p->FoToCoderUnpackSizes[folderIndex] + p->FoToMainUnpackSizeIndex[folderIndex]];
  2045. }
  2046. typedef struct
  2047. {
  2048. UInt32 NumTotalSubStreams;
  2049. UInt32 NumSubDigests;
  2050. CSzData sdNumSubStreams;
  2051. CSzData sdSizes;
  2052. CSzData sdCRCs;
  2053. } CSubStreamInfo;
  2054. static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
  2055. {
  2056. UInt64 type = 0;
  2057. UInt32 numSubDigests = 0;
  2058. UInt32 numFolders = p->NumFolders;
  2059. UInt32 numUnpackStreams = numFolders;
  2060. UInt32 numUnpackSizesInData = 0;
  2061. for (;;)
  2062. {
  2063. RINOK(ReadID(sd, &type));
  2064. if (type == k7zIdNumUnpackStream)
  2065. {
  2066. UInt32 i;
  2067. ssi->sdNumSubStreams.Data = sd->Data;
  2068. numUnpackStreams = 0;
  2069. numSubDigests = 0;
  2070. for (i = 0; i < numFolders; i++)
  2071. {
  2072. UInt32 numStreams;
  2073. RINOK(SzReadNumber32(sd, &numStreams));
  2074. if (numUnpackStreams > numUnpackStreams + numStreams)
  2075. return SZ_ERROR_UNSUPPORTED;
  2076. numUnpackStreams += numStreams;
  2077. if (numStreams != 0)
  2078. numUnpackSizesInData += (numStreams - 1);
  2079. if (numStreams != 1 || !SzBitWithVals_Check(&p->FolderCRCs, i))
  2080. numSubDigests += numStreams;
  2081. }
  2082. ssi->sdNumSubStreams.Size = sd->Data - ssi->sdNumSubStreams.Data;
  2083. continue;
  2084. }
  2085. if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd)
  2086. break;
  2087. RINOK(SkipData(sd));
  2088. }
  2089. if (!ssi->sdNumSubStreams.Data)
  2090. {
  2091. numSubDigests = numFolders;
  2092. if (p->FolderCRCs.Defs)
  2093. numSubDigests = numFolders - CountDefinedBits(p->FolderCRCs.Defs, numFolders);
  2094. }
  2095. ssi->NumTotalSubStreams = numUnpackStreams;
  2096. ssi->NumSubDigests = numSubDigests;
  2097. if (type == k7zIdSize)
  2098. {
  2099. ssi->sdSizes.Data = sd->Data;
  2100. RINOK(SkipNumbers(sd, numUnpackSizesInData));
  2101. ssi->sdSizes.Size = sd->Data - ssi->sdSizes.Data;
  2102. RINOK(ReadID(sd, &type));
  2103. }
  2104. for (;;)
  2105. {
  2106. if (type == k7zIdEnd)
  2107. return SZ_OK;
  2108. if (type == k7zIdCRC)
  2109. {
  2110. ssi->sdCRCs.Data = sd->Data;
  2111. RINOK(SkipBitUi32s(sd, numSubDigests));
  2112. ssi->sdCRCs.Size = sd->Data - ssi->sdCRCs.Data;
  2113. }
  2114. else
  2115. {
  2116. RINOK(SkipData(sd));
  2117. }
  2118. RINOK(ReadID(sd, &type));
  2119. }
  2120. }
  2121. static SRes SzReadStreamsInfo(CSzAr *p,
  2122. CSzData *sd,
  2123. UInt32 numFoldersMax, const CBuf *tempBufs, UInt32 numTempBufs,
  2124. UInt64 *dataOffset,
  2125. CSubStreamInfo *ssi,
  2126. ISzAlloc *alloc)
  2127. {
  2128. UInt64 type;
  2129. SzData_Clear(&ssi->sdSizes);
  2130. SzData_Clear(&ssi->sdCRCs);
  2131. SzData_Clear(&ssi->sdNumSubStreams);
  2132. *dataOffset = 0;
  2133. RINOK(ReadID(sd, &type));
  2134. if (type == k7zIdPackInfo)
  2135. {
  2136. RINOK(ReadNumber(sd, dataOffset));
  2137. RINOK(ReadPackInfo(p, sd, alloc));
  2138. RINOK(ReadID(sd, &type));
  2139. }
  2140. if (type == k7zIdUnpackInfo)
  2141. {
  2142. RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc));
  2143. RINOK(ReadID(sd, &type));
  2144. }
  2145. if (type == k7zIdSubStreamsInfo)
  2146. {
  2147. RINOK(ReadSubStreamsInfo(p, sd, ssi));
  2148. RINOK(ReadID(sd, &type));
  2149. }
  2150. else
  2151. {
  2152. ssi->NumTotalSubStreams = p->NumFolders;
  2153. /* ssi->NumSubDigests = 0; */
  2154. }
  2155. return (type == k7zIdEnd ? SZ_OK : SZ_ERROR_UNSUPPORTED);
  2156. }
  2157. static SRes SzReadAndDecodePackedStreams(
  2158. ILookInStream *inStream,
  2159. CSzData *sd,
  2160. CBuf *tempBufs,
  2161. UInt32 numFoldersMax,
  2162. UInt64 baseOffset,
  2163. CSzAr *p,
  2164. ISzAlloc *allocTemp)
  2165. {
  2166. UInt64 dataStartPos = 0;
  2167. UInt32 fo;
  2168. CSubStreamInfo ssi;
  2169. UInt32 numFolders;
  2170. RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp));
  2171. numFolders = p->NumFolders;
  2172. if (numFolders == 0)
  2173. return SZ_ERROR_ARCHIVE;
  2174. else if (numFolders > numFoldersMax)
  2175. return SZ_ERROR_UNSUPPORTED;
  2176. dataStartPos += baseOffset;
  2177. for (fo = 0; fo < numFolders; fo++)
  2178. Buf_Init(tempBufs + fo);
  2179. for (fo = 0; fo < numFolders; fo++)
  2180. {
  2181. CBuf *tempBuf = tempBufs + fo;
  2182. UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo);
  2183. if ((size_t)unpackSize != unpackSize)
  2184. return SZ_ERROR_MEM;
  2185. if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp))
  2186. return SZ_ERROR_MEM;
  2187. }
  2188. for (fo = 0; fo < numFolders; fo++)
  2189. {
  2190. const CBuf *tempBuf = tempBufs + fo;
  2191. RINOK(LookInStream_SeekTo(inStream, dataStartPos));
  2192. RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp));
  2193. }
  2194. return SZ_OK;
  2195. }
  2196. static SRes SzReadFileNames(const Byte *data, size_t size, UInt32 numFiles, size_t *offsets)
  2197. {
  2198. size_t pos = 0;
  2199. *offsets++ = 0;
  2200. if (numFiles == 0)
  2201. return (size == 0) ? SZ_OK : SZ_ERROR_ARCHIVE;
  2202. if (size < 2)
  2203. return SZ_ERROR_ARCHIVE;
  2204. if (data[size - 2] != 0 || data[size - 1] != 0)
  2205. return SZ_ERROR_ARCHIVE;
  2206. do
  2207. {
  2208. const Byte *p;
  2209. if (pos == size)
  2210. return SZ_ERROR_ARCHIVE;
  2211. for (p = data + pos;
  2212. #ifdef _WIN32
  2213. *(const UInt16 *)p != 0
  2214. #else
  2215. p[0] != 0 || p[1] != 0
  2216. #endif
  2217. ; p += 2);
  2218. pos = p - data + 2;
  2219. *offsets++ = (pos >> 1);
  2220. }
  2221. while (--numFiles);
  2222. return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
  2223. }
  2224. static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
  2225. CSzData *sd2,
  2226. const CBuf *tempBufs, UInt32 numTempBufs,
  2227. ISzAlloc *alloc)
  2228. {
  2229. CSzData sd;
  2230. UInt32 i;
  2231. CNtfsFileTime *vals;
  2232. Byte *defs;
  2233. Byte external;
  2234. RINOK(ReadBitVector(sd2, num, &p->Defs, alloc));
  2235. SZ_READ_BYTE_SD(sd2, external);
  2236. if (external == 0)
  2237. sd = *sd2;
  2238. else
  2239. {
  2240. UInt32 index;
  2241. RINOK(SzReadNumber32(sd2, &index));
  2242. if (index >= numTempBufs)
  2243. return SZ_ERROR_ARCHIVE;
  2244. sd.Data = tempBufs[index].data;
  2245. sd.Size = tempBufs[index].size;
  2246. }
  2247. MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc);
  2248. vals = p->Vals;
  2249. defs = p->Defs;
  2250. for (i = 0; i < num; i++)
  2251. if (SzBitArray_Check(defs, i))
  2252. {
  2253. if (sd.Size < 8)
  2254. return SZ_ERROR_ARCHIVE;
  2255. vals[i].Low = GetUi32(sd.Data);
  2256. vals[i].High = GetUi32(sd.Data + 4);
  2257. SKIP_DATA2(sd, 8);
  2258. }
  2259. else
  2260. vals[i].High = vals[i].Low = 0;
  2261. if (external == 0)
  2262. *sd2 = sd;
  2263. return SZ_OK;
  2264. }
  2265. #define NUM_ADDITIONAL_STREAMS_MAX 8
  2266. static SRes SzReadHeader2(
  2267. CSzArEx *p, /* allocMain */
  2268. CSzData *sd,
  2269. ILookInStream *inStream,
  2270. CBuf *tempBufs, UInt32 *numTempBufs,
  2271. ISzAlloc *allocMain,
  2272. ISzAlloc *allocTemp
  2273. )
  2274. {
  2275. CSubStreamInfo ssi;
  2276. {
  2277. UInt64 type;
  2278. SzData_Clear(&ssi.sdSizes);
  2279. SzData_Clear(&ssi.sdCRCs);
  2280. SzData_Clear(&ssi.sdNumSubStreams);
  2281. ssi.NumSubDigests = 0;
  2282. ssi.NumTotalSubStreams = 0;
  2283. RINOK(ReadID(sd, &type));
  2284. if (type == k7zIdArchiveProperties)
  2285. {
  2286. for (;;)
  2287. {
  2288. UInt64 type2;
  2289. RINOK(ReadID(sd, &type2));
  2290. if (type2 == k7zIdEnd)
  2291. break;
  2292. RINOK(SkipData(sd));
  2293. }
  2294. RINOK(ReadID(sd, &type));
  2295. }
  2296. if (type == k7zIdAdditionalStreamsInfo)
  2297. {
  2298. CSzAr tempAr;
  2299. SRes res;
  2300. SzAr_Init(&tempAr);
  2301. res = SzReadAndDecodePackedStreams(inStream, sd, tempBufs, NUM_ADDITIONAL_STREAMS_MAX,
  2302. p->startPosAfterHeader, &tempAr, allocTemp);
  2303. *numTempBufs = tempAr.NumFolders;
  2304. SzAr_Free(&tempAr, allocTemp);
  2305. if (res != SZ_OK)
  2306. return res;
  2307. RINOK(ReadID(sd, &type));
  2308. }
  2309. if (type == k7zIdMainStreamsInfo)
  2310. {
  2311. RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs,
  2312. &p->dataPos, &ssi, allocMain));
  2313. p->dataPos += p->startPosAfterHeader;
  2314. RINOK(ReadID(sd, &type));
  2315. }
  2316. if (type == k7zIdEnd)
  2317. {
  2318. return SZ_OK;
  2319. }
  2320. if (type != k7zIdFilesInfo)
  2321. return SZ_ERROR_ARCHIVE;
  2322. }
  2323. {
  2324. UInt32 numFiles = 0;
  2325. UInt32 numEmptyStreams = 0;
  2326. const Byte *emptyStreams = NULL;
  2327. const Byte *emptyFiles = NULL;
  2328. RINOK(SzReadNumber32(sd, &numFiles));
  2329. p->NumFiles = numFiles;
  2330. for (;;)
  2331. {
  2332. UInt64 type;
  2333. UInt64 size;
  2334. RINOK(ReadID(sd, &type));
  2335. if (type == k7zIdEnd)
  2336. break;
  2337. RINOK(ReadNumber(sd, &size));
  2338. if (size > sd->Size)
  2339. return SZ_ERROR_ARCHIVE;
  2340. if (type >= ((UInt32)1 << 8))
  2341. {
  2342. SKIP_DATA(sd, size);
  2343. }
  2344. else switch ((unsigned)type)
  2345. {
  2346. case k7zIdName:
  2347. {
  2348. size_t namesSize;
  2349. const Byte *namesData;
  2350. Byte external;
  2351. SZ_READ_BYTE(external);
  2352. if (external == 0)
  2353. {
  2354. namesSize = (size_t)size - 1;
  2355. namesData = sd->Data;
  2356. }
  2357. else
  2358. {
  2359. UInt32 index;
  2360. RINOK(SzReadNumber32(sd, &index));
  2361. if (index >= *numTempBufs)
  2362. return SZ_ERROR_ARCHIVE;
  2363. namesData = (tempBufs)[index].data;
  2364. namesSize = (tempBufs)[index].size;
  2365. }
  2366. if ((namesSize & 1) != 0)
  2367. return SZ_ERROR_ARCHIVE;
  2368. MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain);
  2369. MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain);
  2370. RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets))
  2371. if (external == 0)
  2372. {
  2373. SKIP_DATA(sd, namesSize);
  2374. }
  2375. break;
  2376. }
  2377. case k7zIdEmptyStream:
  2378. {
  2379. RINOK(RememberBitVector(sd, numFiles, &emptyStreams));
  2380. numEmptyStreams = CountDefinedBits(emptyStreams, numFiles);
  2381. emptyFiles = NULL;
  2382. break;
  2383. }
  2384. case k7zIdEmptyFile:
  2385. {
  2386. RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles));
  2387. break;
  2388. }
  2389. case k7zIdWinAttrib:
  2390. {
  2391. Byte external;
  2392. CSzData sdSwitch;
  2393. CSzData *sdPtr;
  2394. SzBitUi32s_Free(&p->Attribs, allocMain);
  2395. RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain));
  2396. SZ_READ_BYTE(external);
  2397. if (external == 0)
  2398. sdPtr = sd;
  2399. else
  2400. {
  2401. UInt32 index;
  2402. RINOK(SzReadNumber32(sd, &index));
  2403. if (index >= *numTempBufs)
  2404. return SZ_ERROR_ARCHIVE;
  2405. sdSwitch.Data = (tempBufs)[index].data;
  2406. sdSwitch.Size = (tempBufs)[index].size;
  2407. sdPtr = &sdSwitch;
  2408. }
  2409. RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain));
  2410. break;
  2411. }
  2412. /*
  2413. case k7zParent:
  2414. {
  2415. SzBitUi32s_Free(&p->Parents, allocMain);
  2416. RINOK(ReadBitVector(sd, numFiles, &p->Parents.Defs, allocMain));
  2417. RINOK(SzReadSwitch(sd));
  2418. RINOK(ReadUi32s(sd, numFiles, &p->Parents, allocMain));
  2419. break;
  2420. }
  2421. */
  2422. case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break;
  2423. case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break;
  2424. default:
  2425. {
  2426. SKIP_DATA(sd, size);
  2427. }
  2428. }
  2429. }
  2430. if (numFiles - numEmptyStreams != ssi.NumTotalSubStreams)
  2431. return SZ_ERROR_ARCHIVE;
  2432. for (;;)
  2433. {
  2434. UInt64 type;
  2435. RINOK(ReadID(sd, &type));
  2436. if (type == k7zIdEnd)
  2437. break;
  2438. RINOK(SkipData(sd));
  2439. }
  2440. {
  2441. UInt32 i;
  2442. UInt32 emptyFileIndex = 0;
  2443. UInt32 folderIndex = 0;
  2444. UInt32 remSubStreams = 0;
  2445. UInt32 numSubStreams = 0;
  2446. UInt64 unpackPos = 0;
  2447. const Byte *digestsDefs = NULL;
  2448. const Byte *digestsVals = NULL;
  2449. UInt32 digestsValsIndex = 0;
  2450. UInt32 digestIndex;
  2451. Byte allDigestsDefined = 0;
  2452. Byte isDirMask = 0;
  2453. Byte crcMask = 0;
  2454. Byte mask = 0x80;
  2455. MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain);
  2456. MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain);
  2457. MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain);
  2458. MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain);
  2459. RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain));
  2460. if (ssi.sdCRCs.Size != 0)
  2461. {
  2462. SZ_READ_BYTE_SD(&ssi.sdCRCs, allDigestsDefined);
  2463. if (allDigestsDefined)
  2464. digestsVals = ssi.sdCRCs.Data;
  2465. else
  2466. {
  2467. size_t numBytes = (ssi.NumSubDigests + 7) >> 3;
  2468. digestsDefs = ssi.sdCRCs.Data;
  2469. digestsVals = digestsDefs + numBytes;
  2470. }
  2471. }
  2472. digestIndex = 0;
  2473. for (i = 0; i < numFiles; i++, mask >>= 1)
  2474. {
  2475. if (mask == 0)
  2476. {
  2477. UInt32 byteIndex = (i - 1) >> 3;
  2478. p->IsDirs[byteIndex] = isDirMask;
  2479. p->CRCs.Defs[byteIndex] = crcMask;
  2480. isDirMask = 0;
  2481. crcMask = 0;
  2482. mask = 0x80;
  2483. }
  2484. p->UnpackPositions[i] = unpackPos;
  2485. p->CRCs.Vals[i] = 0;
  2486. if (emptyStreams && SzBitArray_Check(emptyStreams, i))
  2487. {
  2488. if (emptyFiles)
  2489. {
  2490. if (!SzBitArray_Check(emptyFiles, emptyFileIndex))
  2491. isDirMask |= mask;
  2492. emptyFileIndex++;
  2493. }
  2494. else
  2495. isDirMask |= mask;
  2496. if (remSubStreams == 0)
  2497. {
  2498. p->FileToFolder[i] = (UInt32)-1;
  2499. continue;
  2500. }
  2501. }
  2502. if (remSubStreams == 0)
  2503. {
  2504. for (;;)
  2505. {
  2506. if (folderIndex >= p->db.NumFolders)
  2507. return SZ_ERROR_ARCHIVE;
  2508. p->FolderToFile[folderIndex] = i;
  2509. numSubStreams = 1;
  2510. if (ssi.sdNumSubStreams.Data)
  2511. {
  2512. RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams));
  2513. }
  2514. remSubStreams = numSubStreams;
  2515. if (numSubStreams != 0)
  2516. break;
  2517. {
  2518. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  2519. unpackPos += folderUnpackSize;
  2520. if (unpackPos < folderUnpackSize)
  2521. return SZ_ERROR_ARCHIVE;
  2522. }
  2523. folderIndex++;
  2524. }
  2525. }
  2526. p->FileToFolder[i] = folderIndex;
  2527. if (emptyStreams && SzBitArray_Check(emptyStreams, i))
  2528. continue;
  2529. if (--remSubStreams == 0)
  2530. {
  2531. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  2532. UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]];
  2533. if (folderUnpackSize < unpackPos - startFolderUnpackPos)
  2534. return SZ_ERROR_ARCHIVE;
  2535. unpackPos = startFolderUnpackPos + folderUnpackSize;
  2536. if (unpackPos < folderUnpackSize)
  2537. return SZ_ERROR_ARCHIVE;
  2538. if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, i))
  2539. {
  2540. p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex];
  2541. crcMask |= mask;
  2542. }
  2543. else if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex)))
  2544. {
  2545. p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4);
  2546. digestsValsIndex++;
  2547. crcMask |= mask;
  2548. }
  2549. folderIndex++;
  2550. }
  2551. else
  2552. {
  2553. UInt64 v;
  2554. RINOK(ReadNumber(&ssi.sdSizes, &v));
  2555. unpackPos += v;
  2556. if (unpackPos < v)
  2557. return SZ_ERROR_ARCHIVE;
  2558. if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex)))
  2559. {
  2560. p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4);
  2561. digestsValsIndex++;
  2562. crcMask |= mask;
  2563. }
  2564. }
  2565. }
  2566. if (mask != 0x80)
  2567. {
  2568. UInt32 byteIndex = (i - 1) >> 3;
  2569. p->IsDirs[byteIndex] = isDirMask;
  2570. p->CRCs.Defs[byteIndex] = crcMask;
  2571. }
  2572. p->UnpackPositions[i] = unpackPos;
  2573. if (remSubStreams != 0)
  2574. return SZ_ERROR_ARCHIVE;
  2575. for (;;)
  2576. {
  2577. p->FolderToFile[folderIndex] = i;
  2578. if (folderIndex >= p->db.NumFolders)
  2579. break;
  2580. if (!ssi.sdNumSubStreams.Data)
  2581. return SZ_ERROR_ARCHIVE;
  2582. RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams));
  2583. if (numSubStreams != 0)
  2584. return SZ_ERROR_ARCHIVE;
  2585. /*
  2586. {
  2587. UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  2588. unpackPos += folderUnpackSize;
  2589. if (unpackPos < folderUnpackSize)
  2590. return SZ_ERROR_ARCHIVE;
  2591. }
  2592. */
  2593. folderIndex++;
  2594. }
  2595. if (ssi.sdNumSubStreams.Data && ssi.sdNumSubStreams.Size != 0)
  2596. return SZ_ERROR_ARCHIVE;
  2597. }
  2598. }
  2599. return SZ_OK;
  2600. }
  2601. static SRes SzReadHeader(
  2602. CSzArEx *p,
  2603. CSzData *sd,
  2604. ILookInStream *inStream,
  2605. ISzAlloc *allocMain,
  2606. ISzAlloc *allocTemp)
  2607. {
  2608. UInt32 i;
  2609. UInt32 numTempBufs = 0;
  2610. SRes res;
  2611. CBuf tempBufs[NUM_ADDITIONAL_STREAMS_MAX];
  2612. for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++)
  2613. Buf_Init(tempBufs + i);
  2614. res = SzReadHeader2(p, sd, inStream,
  2615. tempBufs, &numTempBufs,
  2616. allocMain, allocTemp);
  2617. for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++)
  2618. Buf_Free(tempBufs + i, allocTemp);
  2619. RINOK(res);
  2620. if (sd->Size != 0)
  2621. return SZ_ERROR_FAIL;
  2622. return res;
  2623. }
  2624. static SRes SzArEx_Open2(
  2625. CSzArEx *p,
  2626. ILookInStream *inStream,
  2627. ISzAlloc *allocMain,
  2628. ISzAlloc *allocTemp)
  2629. {
  2630. Byte header[k7zStartHeaderSize];
  2631. Int64 startArcPos;
  2632. UInt64 nextHeaderOffset, nextHeaderSize;
  2633. size_t nextHeaderSizeT;
  2634. UInt32 nextHeaderCRC;
  2635. CBuf buf;
  2636. SRes res;
  2637. startArcPos = 0;
  2638. RINOK(inStream->Seek(inStream, &startArcPos, SZ_SEEK_CUR));
  2639. RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE));
  2640. if (!TestSignatureCandidate(header))
  2641. return SZ_ERROR_NO_ARCHIVE;
  2642. if (header[6] != k7zMajorVersion)
  2643. return SZ_ERROR_UNSUPPORTED;
  2644. nextHeaderOffset = GetUi64(header + 12);
  2645. nextHeaderSize = GetUi64(header + 20);
  2646. nextHeaderCRC = GetUi32(header + 28);
  2647. p->startPosAfterHeader = startArcPos + k7zStartHeaderSize;
  2648. if (CrcCalc(header + 12, 20) != GetUi32(header + 8))
  2649. return SZ_ERROR_CRC;
  2650. nextHeaderSizeT = (size_t)nextHeaderSize;
  2651. if (nextHeaderSizeT != nextHeaderSize)
  2652. return SZ_ERROR_MEM;
  2653. if (nextHeaderSizeT == 0)
  2654. return SZ_OK;
  2655. if (nextHeaderOffset > nextHeaderOffset + nextHeaderSize ||
  2656. nextHeaderOffset > nextHeaderOffset + nextHeaderSize + k7zStartHeaderSize)
  2657. return SZ_ERROR_NO_ARCHIVE;
  2658. {
  2659. Int64 pos = 0;
  2660. RINOK(inStream->Seek(inStream, &pos, SZ_SEEK_END));
  2661. if ((UInt64)pos < startArcPos + nextHeaderOffset ||
  2662. (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset ||
  2663. (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize)
  2664. return SZ_ERROR_INPUT_EOF;
  2665. }
  2666. RINOK(LookInStream_SeekTo(inStream, startArcPos + k7zStartHeaderSize + nextHeaderOffset));
  2667. if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp))
  2668. return SZ_ERROR_MEM;
  2669. res = LookInStream_Read(inStream, buf.data, nextHeaderSizeT);
  2670. if (res == SZ_OK)
  2671. {
  2672. res = SZ_ERROR_ARCHIVE;
  2673. if (CrcCalc(buf.data, nextHeaderSizeT) == nextHeaderCRC)
  2674. {
  2675. CSzData sd;
  2676. UInt64 type;
  2677. sd.Data = buf.data;
  2678. sd.Size = buf.size;
  2679. res = ReadID(&sd, &type);
  2680. if (res == SZ_OK && type == k7zIdEncodedHeader)
  2681. {
  2682. CSzAr tempAr;
  2683. CBuf tempBuf;
  2684. Buf_Init(&tempBuf);
  2685. SzAr_Init(&tempAr);
  2686. res = SzReadAndDecodePackedStreams(inStream, &sd, &tempBuf, 1, p->startPosAfterHeader, &tempAr, allocTemp);
  2687. SzAr_Free(&tempAr, allocTemp);
  2688. if (res != SZ_OK)
  2689. {
  2690. Buf_Free(&tempBuf, allocTemp);
  2691. }
  2692. else
  2693. {
  2694. Buf_Free(&buf, allocTemp);
  2695. buf.data = tempBuf.data;
  2696. buf.size = tempBuf.size;
  2697. sd.Data = buf.data;
  2698. sd.Size = buf.size;
  2699. res = ReadID(&sd, &type);
  2700. }
  2701. }
  2702. if (res == SZ_OK)
  2703. {
  2704. if (type == k7zIdHeader)
  2705. {
  2706. /*
  2707. CSzData sd2;
  2708. unsigned ttt;
  2709. for (ttt = 0; ttt < 40000; ttt++)
  2710. {
  2711. SzArEx_Free(p, allocMain);
  2712. sd2 = sd;
  2713. res = SzReadHeader(p, &sd2, inStream, allocMain, allocTemp);
  2714. if (res != SZ_OK)
  2715. break;
  2716. }
  2717. */
  2718. res = SzReadHeader(p, &sd, inStream, allocMain, allocTemp);
  2719. }
  2720. else
  2721. res = SZ_ERROR_UNSUPPORTED;
  2722. }
  2723. }
  2724. }
  2725. Buf_Free(&buf, allocTemp);
  2726. return res;
  2727. }
  2728. static SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream,
  2729. ISzAlloc *allocMain, ISzAlloc *allocTemp)
  2730. {
  2731. SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp);
  2732. if (res != SZ_OK)
  2733. SzArEx_Free(p, allocMain);
  2734. return res;
  2735. }
  2736. static SRes SzArEx_Extract(
  2737. const CSzArEx *p,
  2738. ILookInStream *inStream,
  2739. UInt32 fileIndex,
  2740. UInt32 *blockIndex,
  2741. Byte **tempBuf,
  2742. size_t *outBufferSize,
  2743. size_t *offset,
  2744. size_t *outSizeProcessed,
  2745. ISzAlloc *allocMain,
  2746. ISzAlloc *allocTemp)
  2747. {
  2748. UInt32 folderIndex = p->FileToFolder[fileIndex];
  2749. SRes res = SZ_OK;
  2750. *offset = 0;
  2751. *outSizeProcessed = 0;
  2752. if (folderIndex == (UInt32)-1)
  2753. {
  2754. IAlloc_Free(allocMain, *tempBuf);
  2755. *blockIndex = folderIndex;
  2756. *tempBuf = NULL;
  2757. *outBufferSize = 0;
  2758. return SZ_OK;
  2759. }
  2760. if (*tempBuf == NULL || *blockIndex != folderIndex)
  2761. {
  2762. UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
  2763. /*
  2764. UInt64 unpackSizeSpec =
  2765. p->UnpackPositions[p->FolderToFile[folderIndex + 1]] -
  2766. p->UnpackPositions[p->FolderToFile[folderIndex]];
  2767. */
  2768. size_t unpackSize = (size_t)unpackSizeSpec;
  2769. if (unpackSize != unpackSizeSpec)
  2770. return SZ_ERROR_MEM;
  2771. *blockIndex = folderIndex;
  2772. IAlloc_Free(allocMain, *tempBuf);
  2773. *tempBuf = NULL;
  2774. if (res == SZ_OK)
  2775. {
  2776. *outBufferSize = unpackSize;
  2777. if (unpackSize != 0)
  2778. {
  2779. *tempBuf = (Byte *)IAlloc_Alloc(allocMain, unpackSize);
  2780. if (*tempBuf == NULL)
  2781. res = SZ_ERROR_MEM;
  2782. }
  2783. if (res == SZ_OK)
  2784. {
  2785. res = SzAr_DecodeFolder(&p->db, folderIndex,
  2786. inStream, p->dataPos, *tempBuf, unpackSize, allocTemp);
  2787. }
  2788. }
  2789. }
  2790. if (res == SZ_OK)
  2791. {
  2792. UInt64 unpackPos = p->UnpackPositions[fileIndex];
  2793. *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]);
  2794. *outSizeProcessed = (size_t)(p->UnpackPositions[fileIndex + 1] - unpackPos);
  2795. if (*offset + *outSizeProcessed > *outBufferSize)
  2796. return SZ_ERROR_FAIL;
  2797. if (SzBitWithVals_Check(&p->CRCs, fileIndex))
  2798. if (CrcCalc(*tempBuf + *offset, *outSizeProcessed) != p->CRCs.Vals[fileIndex])
  2799. res = SZ_ERROR_CRC;
  2800. }
  2801. return res;
  2802. }
  2803. static size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
  2804. {
  2805. size_t offs = p->FileNameOffsets[fileIndex];
  2806. size_t len = p->FileNameOffsets[fileIndex + 1] - offs;
  2807. if (dest != 0)
  2808. {
  2809. size_t i;
  2810. const Byte *src = p->FileNames + offs * 2;
  2811. for (i = 0; i < len; i++)
  2812. dest[i] = GetUi16(src + i * 2);
  2813. }
  2814. return len;
  2815. }
  2816. /*
  2817. static size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex)
  2818. {
  2819. size_t len;
  2820. if (!p->FileNameOffsets)
  2821. return 1;
  2822. len = 0;
  2823. for (;;)
  2824. {
  2825. UInt32 parent = (UInt32)(Int32)-1;
  2826. len += p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
  2827. if SzBitWithVals_Check(&p->Parents, fileIndex)
  2828. parent = p->Parents.Vals[fileIndex];
  2829. if (parent == (UInt32)(Int32)-1)
  2830. return len;
  2831. fileIndex = parent;
  2832. }
  2833. }
  2834. static UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
  2835. {
  2836. Bool needSlash;
  2837. if (!p->FileNameOffsets)
  2838. {
  2839. *(--dest) = 0;
  2840. return dest;
  2841. }
  2842. needSlash = False;
  2843. for (;;)
  2844. {
  2845. UInt32 parent = (UInt32)(Int32)-1;
  2846. size_t curLen = p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
  2847. SzArEx_GetFileNameUtf16(p, fileIndex, dest - curLen);
  2848. if (needSlash)
  2849. *(dest - 1) = '/';
  2850. needSlash = True;
  2851. dest -= curLen;
  2852. if SzBitWithVals_Check(&p->Parents, fileIndex)
  2853. parent = p->Parents.Vals[fileIndex];
  2854. if (parent == (UInt32)(Int32)-1)
  2855. return dest;
  2856. fileIndex = parent;
  2857. }
  2858. }
  2859. */
  2860. /* 7zBuf.c -- Byte Buffer
  2861. 2013-01-21 : Igor Pavlov : Public domain */
  2862. /*
  2863. #include "Precomp.h"
  2864. #include "7zBuf.h"
  2865. */
  2866. static void Buf_Init(CBuf *p)
  2867. {
  2868. p->data = 0;
  2869. p->size = 0;
  2870. }
  2871. static int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc)
  2872. {
  2873. p->size = 0;
  2874. if (size == 0)
  2875. {
  2876. p->data = 0;
  2877. return 1;
  2878. }
  2879. p->data = (Byte *)alloc->Alloc(alloc, size);
  2880. if (p->data != 0)
  2881. {
  2882. p->size = size;
  2883. return 1;
  2884. }
  2885. return 0;
  2886. }
  2887. static void Buf_Free(CBuf *p, ISzAlloc *alloc)
  2888. {
  2889. alloc->Free(alloc, p->data);
  2890. p->data = 0;
  2891. p->size = 0;
  2892. }
  2893. /* 7zDec.c -- Decoding from 7z folder
  2894. 2015-11-18 : Igor Pavlov : Public domain */
  2895. /* #define _7ZIP_PPMD_SUPPPORT */
  2896. /*
  2897. #include "Precomp.h"
  2898. #include <string.h>
  2899. #include "7z.h"
  2900. #include "7zCrc.h"
  2901. #include "Bcj2.h"
  2902. #include "Bra.h"
  2903. #include "CpuArch.h"
  2904. #include "Delta.h"
  2905. #include "LzmaDec.h"
  2906. #include "Lzma2Dec.h"
  2907. #ifdef _7ZIP_PPMD_SUPPPORT
  2908. #include "Ppmd7.h"
  2909. #endif
  2910. */
  2911. #define k_Copy 0
  2912. #define k_Delta 3
  2913. #define k_LZMA2 0x21
  2914. #define k_LZMA 0x30101
  2915. #define k_BCJ 0x3030103
  2916. #define k_BCJ2 0x303011B
  2917. #define k_PPC 0x3030205
  2918. #define k_IA64 0x3030401
  2919. #define k_ARM 0x3030501
  2920. #define k_ARMT 0x3030701
  2921. #define k_SPARC 0x3030805
  2922. #ifdef _7ZIP_PPMD_SUPPPORT
  2923. #define k_PPMD 0x30401
  2924. typedef struct
  2925. {
  2926. IByteIn p;
  2927. const Byte *cur;
  2928. const Byte *end;
  2929. const Byte *begin;
  2930. UInt64 processed;
  2931. Bool extra;
  2932. SRes res;
  2933. ILookInStream *inStream;
  2934. } CByteInToLook;
  2935. static Byte ReadByte(void *pp)
  2936. {
  2937. CByteInToLook *p = (CByteInToLook *)pp;
  2938. if (p->cur != p->end)
  2939. return *p->cur++;
  2940. if (p->res == SZ_OK)
  2941. {
  2942. size_t size = p->cur - p->begin;
  2943. p->processed += size;
  2944. p->res = p->inStream->Skip(p->inStream, size);
  2945. size = (1 << 25);
  2946. p->res = p->inStream->Look(p->inStream, (const void **)&p->begin, &size);
  2947. p->cur = p->begin;
  2948. p->end = p->begin + size;
  2949. if (size != 0)
  2950. return *p->cur++;;
  2951. }
  2952. p->extra = True;
  2953. return 0;
  2954. }
  2955. static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream,
  2956. Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain)
  2957. {
  2958. CPpmd7 ppmd;
  2959. CByteInToLook s;
  2960. SRes res = SZ_OK;
  2961. s.p.Read = ReadByte;
  2962. s.inStream = inStream;
  2963. s.begin = s.end = s.cur = NULL;
  2964. s.extra = False;
  2965. s.res = SZ_OK;
  2966. s.processed = 0;
  2967. if (propsSize != 5)
  2968. return SZ_ERROR_UNSUPPORTED;
  2969. {
  2970. unsigned order = props[0];
  2971. UInt32 memSize = GetUi32(props + 1);
  2972. if (order < PPMD7_MIN_ORDER ||
  2973. order > PPMD7_MAX_ORDER ||
  2974. memSize < PPMD7_MIN_MEM_SIZE ||
  2975. memSize > PPMD7_MAX_MEM_SIZE)
  2976. return SZ_ERROR_UNSUPPORTED;
  2977. Ppmd7_Construct(&ppmd);
  2978. if (!Ppmd7_Alloc(&ppmd, memSize, allocMain))
  2979. return SZ_ERROR_MEM;
  2980. Ppmd7_Init(&ppmd, order);
  2981. }
  2982. {
  2983. CPpmd7z_RangeDec rc;
  2984. Ppmd7z_RangeDec_CreateVTable(&rc);
  2985. rc.Stream = &s.p;
  2986. if (!Ppmd7z_RangeDec_Init(&rc))
  2987. res = SZ_ERROR_DATA;
  2988. else if (s.extra)
  2989. res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA);
  2990. else
  2991. {
  2992. SizeT i;
  2993. for (i = 0; i < outSize; i++)
  2994. {
  2995. int sym = Ppmd7_DecodeSymbol(&ppmd, &rc.p);
  2996. if (s.extra || sym < 0)
  2997. break;
  2998. outBuffer[i] = (Byte)sym;
  2999. }
  3000. if (i != outSize)
  3001. res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA);
  3002. else if (s.processed + (s.cur - s.begin) != inSize || !Ppmd7z_RangeDec_IsFinishedOK(&rc))
  3003. res = SZ_ERROR_DATA;
  3004. }
  3005. }
  3006. Ppmd7_Free(&ppmd, allocMain);
  3007. return res;
  3008. }
  3009. #endif
  3010. static SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream,
  3011. Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain)
  3012. {
  3013. CLzmaDec state;
  3014. SRes res = SZ_OK;
  3015. LzmaDec_Construct(&state);
  3016. RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain));
  3017. state.dic = outBuffer;
  3018. state.dicBufSize = outSize;
  3019. LzmaDec_Init(&state);
  3020. for (;;)
  3021. {
  3022. const void *inBuf = NULL;
  3023. size_t lookahead = (1 << 18);
  3024. if (lookahead > inSize)
  3025. lookahead = (size_t)inSize;
  3026. res = inStream->Look(inStream, &inBuf, &lookahead);
  3027. if (res != SZ_OK)
  3028. break;
  3029. {
  3030. SizeT inProcessed = (SizeT)lookahead, dicPos = state.dicPos;
  3031. ELzmaStatus status;
  3032. res = LzmaDec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
  3033. lookahead -= inProcessed;
  3034. inSize -= inProcessed;
  3035. if (res != SZ_OK)
  3036. break;
  3037. if (status == LZMA_STATUS_FINISHED_WITH_MARK)
  3038. {
  3039. if (outSize != state.dicPos || inSize != 0)
  3040. res = SZ_ERROR_DATA;
  3041. break;
  3042. }
  3043. if (outSize == state.dicPos && inSize == 0 && status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
  3044. break;
  3045. if (inProcessed == 0 && dicPos == state.dicPos)
  3046. {
  3047. res = SZ_ERROR_DATA;
  3048. break;
  3049. }
  3050. res = inStream->Skip((void *)inStream, inProcessed);
  3051. if (res != SZ_OK)
  3052. break;
  3053. }
  3054. }
  3055. LzmaDec_FreeProbs(&state, allocMain);
  3056. return res;
  3057. }
  3058. #ifndef _7Z_NO_METHOD_LZMA2
  3059. static SRes SzDecodeLzma2(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream,
  3060. Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain)
  3061. {
  3062. CLzma2Dec state;
  3063. SRes res = SZ_OK;
  3064. Lzma2Dec_Construct(&state);
  3065. if (propsSize != 1)
  3066. return SZ_ERROR_DATA;
  3067. RINOK(Lzma2Dec_AllocateProbs(&state, props[0], allocMain));
  3068. state.decoder.dic = outBuffer;
  3069. state.decoder.dicBufSize = outSize;
  3070. Lzma2Dec_Init(&state);
  3071. for (;;)
  3072. {
  3073. const void *inBuf = NULL;
  3074. size_t lookahead = (1 << 18);
  3075. if (lookahead > inSize)
  3076. lookahead = (size_t)inSize;
  3077. res = inStream->Look(inStream, &inBuf, &lookahead);
  3078. if (res != SZ_OK)
  3079. break;
  3080. {
  3081. SizeT inProcessed = (SizeT)lookahead, dicPos = state.decoder.dicPos;
  3082. ELzmaStatus status;
  3083. res = Lzma2Dec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
  3084. lookahead -= inProcessed;
  3085. inSize -= inProcessed;
  3086. if (res != SZ_OK)
  3087. break;
  3088. if (status == LZMA_STATUS_FINISHED_WITH_MARK)
  3089. {
  3090. if (outSize != state.decoder.dicPos || inSize != 0)
  3091. res = SZ_ERROR_DATA;
  3092. break;
  3093. }
  3094. if (inProcessed == 0 && dicPos == state.decoder.dicPos)
  3095. {
  3096. res = SZ_ERROR_DATA;
  3097. break;
  3098. }
  3099. res = inStream->Skip((void *)inStream, inProcessed);
  3100. if (res != SZ_OK)
  3101. break;
  3102. }
  3103. }
  3104. Lzma2Dec_FreeProbs(&state, allocMain);
  3105. return res;
  3106. }
  3107. #endif
  3108. static SRes SzDecodeCopy(UInt64 inSize, ILookInStream *inStream, Byte *outBuffer)
  3109. {
  3110. while (inSize > 0)
  3111. {
  3112. const void *inBuf;
  3113. size_t curSize = (1 << 18);
  3114. if (curSize > inSize)
  3115. curSize = (size_t)inSize;
  3116. RINOK(inStream->Look(inStream, &inBuf, &curSize));
  3117. if (curSize == 0)
  3118. return SZ_ERROR_INPUT_EOF;
  3119. memcpy(outBuffer, inBuf, curSize);
  3120. outBuffer += curSize;
  3121. inSize -= curSize;
  3122. RINOK(inStream->Skip((void *)inStream, curSize));
  3123. }
  3124. return SZ_OK;
  3125. }
  3126. static Bool IS_MAIN_METHOD(UInt32 m)
  3127. {
  3128. switch (m)
  3129. {
  3130. case k_Copy:
  3131. case k_LZMA:
  3132. #ifndef _7Z_NO_METHOD_LZMA2
  3133. case k_LZMA2:
  3134. #endif
  3135. #ifdef _7ZIP_PPMD_SUPPPORT
  3136. case k_PPMD:
  3137. #endif
  3138. return True;
  3139. }
  3140. return False;
  3141. }
  3142. static Bool IS_SUPPORTED_CODER(const CSzCoderInfo *c)
  3143. {
  3144. return
  3145. c->NumStreams == 1
  3146. /* && c->MethodID <= (UInt32)0xFFFFFFFF */
  3147. && IS_MAIN_METHOD((UInt32)c->MethodID);
  3148. }
  3149. #define IS_BCJ2(c) ((c)->MethodID == k_BCJ2 && (c)->NumStreams == 4)
  3150. static SRes CheckSupportedFolder(const CSzFolder *f)
  3151. {
  3152. if (f->NumCoders < 1 || f->NumCoders > 4)
  3153. return SZ_ERROR_UNSUPPORTED;
  3154. if (!IS_SUPPORTED_CODER(&f->Coders[0]))
  3155. return SZ_ERROR_UNSUPPORTED;
  3156. if (f->NumCoders == 1)
  3157. {
  3158. if (f->NumPackStreams != 1 || f->PackStreams[0] != 0 || f->NumBonds != 0)
  3159. return SZ_ERROR_UNSUPPORTED;
  3160. return SZ_OK;
  3161. }
  3162. #ifndef _7Z_NO_METHODS_FILTERS
  3163. if (f->NumCoders == 2)
  3164. {
  3165. const CSzCoderInfo *c = &f->Coders[1];
  3166. if (
  3167. /* c->MethodID > (UInt32)0xFFFFFFFF || */
  3168. c->NumStreams != 1
  3169. || f->NumPackStreams != 1
  3170. || f->PackStreams[0] != 0
  3171. || f->NumBonds != 1
  3172. || f->Bonds[0].InIndex != 1
  3173. || f->Bonds[0].OutIndex != 0)
  3174. return SZ_ERROR_UNSUPPORTED;
  3175. switch ((UInt32)c->MethodID)
  3176. {
  3177. case k_Delta:
  3178. case k_BCJ:
  3179. case k_PPC:
  3180. case k_IA64:
  3181. case k_SPARC:
  3182. case k_ARM:
  3183. case k_ARMT:
  3184. break;
  3185. default:
  3186. return SZ_ERROR_UNSUPPORTED;
  3187. }
  3188. return SZ_OK;
  3189. }
  3190. #endif
  3191. if (f->NumCoders == 4)
  3192. {
  3193. if (!IS_SUPPORTED_CODER(&f->Coders[1])
  3194. || !IS_SUPPORTED_CODER(&f->Coders[2])
  3195. || !IS_BCJ2(&f->Coders[3]))
  3196. return SZ_ERROR_UNSUPPORTED;
  3197. if (f->NumPackStreams != 4
  3198. || f->PackStreams[0] != 2
  3199. || f->PackStreams[1] != 6
  3200. || f->PackStreams[2] != 1
  3201. || f->PackStreams[3] != 0
  3202. || f->NumBonds != 3
  3203. || f->Bonds[0].InIndex != 5 || f->Bonds[0].OutIndex != 0
  3204. || f->Bonds[1].InIndex != 4 || f->Bonds[1].OutIndex != 1
  3205. || f->Bonds[2].InIndex != 3 || f->Bonds[2].OutIndex != 2)
  3206. return SZ_ERROR_UNSUPPORTED;
  3207. return SZ_OK;
  3208. }
  3209. return SZ_ERROR_UNSUPPORTED;
  3210. }
  3211. #define CASE_BRA_CONV(isa) case k_ ## isa: isa ## _Convert(outBuffer, outSize, 0, 0); break;
  3212. static SRes SzFolder_Decode2(const CSzFolder *folder,
  3213. const Byte *propsData,
  3214. const UInt64 *unpackSizes,
  3215. const UInt64 *packPositions,
  3216. ILookInStream *inStream, UInt64 startPos,
  3217. Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain,
  3218. Byte *tempBuf[])
  3219. {
  3220. UInt32 ci;
  3221. SizeT tempSizes[3] = { 0, 0, 0};
  3222. SizeT tempSize3 = 0;
  3223. Byte *tempBuf3 = 0;
  3224. RINOK(CheckSupportedFolder(folder));
  3225. for (ci = 0; ci < folder->NumCoders; ci++)
  3226. {
  3227. const CSzCoderInfo *coder = &folder->Coders[ci];
  3228. if (IS_MAIN_METHOD((UInt32)coder->MethodID))
  3229. {
  3230. UInt32 si = 0;
  3231. UInt64 offset;
  3232. UInt64 inSize;
  3233. Byte *outBufCur = outBuffer;
  3234. SizeT outSizeCur = outSize;
  3235. if (folder->NumCoders == 4)
  3236. {
  3237. UInt32 indices[] = { 3, 2, 0 };
  3238. UInt64 unpackSize = unpackSizes[ci];
  3239. si = indices[ci];
  3240. if (ci < 2)
  3241. {
  3242. Byte *temp;
  3243. outSizeCur = (SizeT)unpackSize;
  3244. if (outSizeCur != unpackSize)
  3245. return SZ_ERROR_MEM;
  3246. temp = (Byte *)IAlloc_Alloc(allocMain, outSizeCur);
  3247. if (!temp && outSizeCur != 0)
  3248. return SZ_ERROR_MEM;
  3249. outBufCur = tempBuf[1 - ci] = temp;
  3250. tempSizes[1 - ci] = outSizeCur;
  3251. }
  3252. else if (ci == 2)
  3253. {
  3254. if (unpackSize > outSize) /* check it */
  3255. return SZ_ERROR_PARAM;
  3256. tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize);
  3257. tempSize3 = outSizeCur = (SizeT)unpackSize;
  3258. }
  3259. else
  3260. return SZ_ERROR_UNSUPPORTED;
  3261. }
  3262. offset = packPositions[si];
  3263. inSize = packPositions[si + 1] - offset;
  3264. RINOK(LookInStream_SeekTo(inStream, startPos + offset));
  3265. if (coder->MethodID == k_Copy)
  3266. {
  3267. if (inSize != outSizeCur) /* check it */
  3268. return SZ_ERROR_DATA;
  3269. RINOK(SzDecodeCopy(inSize, inStream, outBufCur));
  3270. }
  3271. else if (coder->MethodID == k_LZMA)
  3272. {
  3273. RINOK(SzDecodeLzma(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain));
  3274. }
  3275. #ifndef _7Z_NO_METHOD_LZMA2
  3276. else if (coder->MethodID == k_LZMA2)
  3277. {
  3278. RINOK(SzDecodeLzma2(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain));
  3279. }
  3280. #endif
  3281. #ifdef _7ZIP_PPMD_SUPPPORT
  3282. else if (coder->MethodID == k_PPMD)
  3283. {
  3284. RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain));
  3285. }
  3286. #endif
  3287. else
  3288. return SZ_ERROR_UNSUPPORTED;
  3289. }
  3290. else if (coder->MethodID == k_BCJ2)
  3291. {
  3292. UInt64 offset = packPositions[1];
  3293. UInt64 s3Size = packPositions[2] - offset;
  3294. if (ci != 3)
  3295. return SZ_ERROR_UNSUPPORTED;
  3296. tempSizes[2] = (SizeT)s3Size;
  3297. if (tempSizes[2] != s3Size)
  3298. return SZ_ERROR_MEM;
  3299. tempBuf[2] = (Byte *)IAlloc_Alloc(allocMain, tempSizes[2]);
  3300. if (!tempBuf[2] && tempSizes[2] != 0)
  3301. return SZ_ERROR_MEM;
  3302. RINOK(LookInStream_SeekTo(inStream, startPos + offset));
  3303. RINOK(SzDecodeCopy(s3Size, inStream, tempBuf[2]));
  3304. if ((tempSizes[0] & 3) != 0 ||
  3305. (tempSizes[1] & 3) != 0 ||
  3306. tempSize3 + tempSizes[0] + tempSizes[1] != outSize)
  3307. return SZ_ERROR_DATA;
  3308. {
  3309. CBcj2Dec p;
  3310. p.bufs[0] = tempBuf3; p.lims[0] = tempBuf3 + tempSize3;
  3311. p.bufs[1] = tempBuf[0]; p.lims[1] = tempBuf[0] + tempSizes[0];
  3312. p.bufs[2] = tempBuf[1]; p.lims[2] = tempBuf[1] + tempSizes[1];
  3313. p.bufs[3] = tempBuf[2]; p.lims[3] = tempBuf[2] + tempSizes[2];
  3314. p.dest = outBuffer;
  3315. p.destLim = outBuffer + outSize;
  3316. Bcj2Dec_Init(&p);
  3317. RINOK(Bcj2Dec_Decode(&p));
  3318. {
  3319. unsigned i;
  3320. for (i = 0; i < 4; i++)
  3321. if (p.bufs[i] != p.lims[i])
  3322. return SZ_ERROR_DATA;
  3323. if (!Bcj2Dec_IsFinished(&p))
  3324. return SZ_ERROR_DATA;
  3325. if (p.dest != p.destLim
  3326. || p.state != BCJ2_STREAM_MAIN)
  3327. return SZ_ERROR_DATA;
  3328. }
  3329. }
  3330. }
  3331. #ifndef _7Z_NO_METHODS_FILTERS
  3332. else if (ci == 1)
  3333. {
  3334. if (coder->MethodID == k_Delta)
  3335. {
  3336. if (coder->PropsSize != 1)
  3337. return SZ_ERROR_UNSUPPORTED;
  3338. {
  3339. Byte state[DELTA_STATE_SIZE];
  3340. Delta_Init(state);
  3341. Delta_Decode(state, (unsigned)(propsData[coder->PropsOffset]) + 1, outBuffer, outSize);
  3342. }
  3343. }
  3344. else
  3345. {
  3346. if (coder->PropsSize != 0)
  3347. return SZ_ERROR_UNSUPPORTED;
  3348. switch (coder->MethodID)
  3349. {
  3350. case k_BCJ:
  3351. {
  3352. UInt32 state;
  3353. x86_Convert_Init(state);
  3354. x86_Convert(outBuffer, outSize, 0, &state, 0);
  3355. break;
  3356. }
  3357. CASE_BRA_CONV(PPC)
  3358. CASE_BRA_CONV(IA64)
  3359. CASE_BRA_CONV(SPARC)
  3360. CASE_BRA_CONV(ARM)
  3361. CASE_BRA_CONV(ARMT)
  3362. default:
  3363. return SZ_ERROR_UNSUPPORTED;
  3364. }
  3365. }
  3366. }
  3367. #endif
  3368. else
  3369. return SZ_ERROR_UNSUPPORTED;
  3370. }
  3371. return SZ_OK;
  3372. }
  3373. static SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex,
  3374. ILookInStream *inStream, UInt64 startPos,
  3375. Byte *outBuffer, size_t outSize,
  3376. ISzAlloc *allocMain)
  3377. {
  3378. SRes res;
  3379. CSzFolder folder;
  3380. CSzData sd;
  3381. const Byte *data = p->CodersData + p->FoCodersOffsets[folderIndex];
  3382. sd.Data = data;
  3383. sd.Size = p->FoCodersOffsets[folderIndex + 1] - p->FoCodersOffsets[folderIndex];
  3384. res = SzGetNextFolderItem(&folder, &sd);
  3385. if (res != SZ_OK)
  3386. return res;
  3387. if (sd.Size != 0
  3388. || folder.UnpackStream != p->FoToMainUnpackSizeIndex[folderIndex]
  3389. || outSize != SzAr_GetFolderUnpackSize(p, folderIndex))
  3390. return SZ_ERROR_FAIL;
  3391. {
  3392. unsigned i;
  3393. Byte *tempBuf[3] = { 0, 0, 0};
  3394. res = SzFolder_Decode2(&folder, data,
  3395. &p->CoderUnpackSizes[p->FoToCoderUnpackSizes[folderIndex]],
  3396. p->PackPositions + p->FoStartPackStreamIndex[folderIndex],
  3397. inStream, startPos,
  3398. outBuffer, (SizeT)outSize, allocMain, tempBuf);
  3399. for (i = 0; i < 3; i++)
  3400. IAlloc_Free(allocMain, tempBuf[i]);
  3401. if (res == SZ_OK)
  3402. if (SzBitWithVals_Check(&p->FolderCRCs, folderIndex))
  3403. if (CrcCalc(outBuffer, outSize) != p->FolderCRCs.Vals[folderIndex])
  3404. res = SZ_ERROR_CRC;
  3405. return res;
  3406. }
  3407. }
  3408. /* Bcj2.c -- BCJ2 Decoder (Converter for x86 code)
  3409. 2015-08-01 : Igor Pavlov : Public domain */
  3410. /*
  3411. #include "Precomp.h"
  3412. #include "Bcj2.h"
  3413. #include "CpuArch.h"
  3414. */
  3415. #define CProb UInt16
  3416. #define kTopValue ((UInt32)1 << 24)
  3417. #define kNumModelBits 11
  3418. #define kBitModelTotal (1 << kNumModelBits)
  3419. #define kNumMoveBits 5
  3420. #define _IF_BIT_0 ttt = *prob; bound = (p->range >> kNumModelBits) * ttt; if (p->code < bound)
  3421. #define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
  3422. #define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
  3423. static void Bcj2Dec_Init(CBcj2Dec *p)
  3424. {
  3425. unsigned i;
  3426. p->state = BCJ2_DEC_STATE_OK;
  3427. p->ip = 0;
  3428. p->temp[3] = 0;
  3429. p->range = 0;
  3430. p->code = 0;
  3431. for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
  3432. p->probs[i] = kBitModelTotal >> 1;
  3433. }
  3434. static SRes Bcj2Dec_Decode(CBcj2Dec *p)
  3435. {
  3436. if (p->range <= 5)
  3437. {
  3438. p->state = BCJ2_DEC_STATE_OK;
  3439. for (; p->range != 5; p->range++)
  3440. {
  3441. if (p->range == 1 && p->code != 0)
  3442. return SZ_ERROR_DATA;
  3443. if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
  3444. {
  3445. p->state = BCJ2_STREAM_RC;
  3446. return SZ_OK;
  3447. }
  3448. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  3449. }
  3450. if (p->code == 0xFFFFFFFF)
  3451. return SZ_ERROR_DATA;
  3452. p->range = 0xFFFFFFFF;
  3453. }
  3454. else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
  3455. {
  3456. while (p->state <= BCJ2_DEC_STATE_ORIG_3)
  3457. {
  3458. Byte *dest = p->dest;
  3459. if (dest == p->destLim)
  3460. return SZ_OK;
  3461. *dest = p->temp[p->state++ - BCJ2_DEC_STATE_ORIG_0];
  3462. p->dest = dest + 1;
  3463. }
  3464. }
  3465. /*
  3466. if (BCJ2_IS_32BIT_STREAM(p->state))
  3467. {
  3468. const Byte *cur = p->bufs[p->state];
  3469. if (cur == p->lims[p->state])
  3470. return SZ_OK;
  3471. p->bufs[p->state] = cur + 4;
  3472. {
  3473. UInt32 val;
  3474. Byte *dest;
  3475. SizeT rem;
  3476. p->ip += 4;
  3477. val = GetBe32(cur) - p->ip;
  3478. dest = p->dest;
  3479. rem = p->destLim - dest;
  3480. if (rem < 4)
  3481. {
  3482. SizeT i;
  3483. SetUi32(p->temp, val);
  3484. for (i = 0; i < rem; i++)
  3485. dest[i] = p->temp[i];
  3486. p->dest = dest + rem;
  3487. p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
  3488. return SZ_OK;
  3489. }
  3490. SetUi32(dest, val);
  3491. p->temp[3] = (Byte)(val >> 24);
  3492. p->dest = dest + 4;
  3493. p->state = BCJ2_DEC_STATE_OK;
  3494. }
  3495. }
  3496. */
  3497. for (;;)
  3498. {
  3499. if (BCJ2_IS_32BIT_STREAM(p->state))
  3500. p->state = BCJ2_DEC_STATE_OK;
  3501. else
  3502. {
  3503. if (p->range < kTopValue)
  3504. {
  3505. if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
  3506. {
  3507. p->state = BCJ2_STREAM_RC;
  3508. return SZ_OK;
  3509. }
  3510. p->range <<= 8;
  3511. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  3512. }
  3513. {
  3514. const Byte *src = p->bufs[BCJ2_STREAM_MAIN];
  3515. const Byte *srcLim;
  3516. Byte *dest;
  3517. SizeT num = p->lims[BCJ2_STREAM_MAIN] - src;
  3518. if (num == 0)
  3519. {
  3520. p->state = BCJ2_STREAM_MAIN;
  3521. return SZ_OK;
  3522. }
  3523. dest = p->dest;
  3524. if (num > (SizeT)(p->destLim - dest))
  3525. {
  3526. num = p->destLim - dest;
  3527. if (num == 0)
  3528. {
  3529. p->state = BCJ2_DEC_STATE_ORIG;
  3530. return SZ_OK;
  3531. }
  3532. }
  3533. srcLim = src + num;
  3534. if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80)
  3535. *dest = src[0];
  3536. else for (;;)
  3537. {
  3538. Byte b = *src;
  3539. *dest = b;
  3540. if (b != 0x0F)
  3541. {
  3542. if ((b & 0xFE) == 0xE8)
  3543. break;
  3544. dest++;
  3545. if (++src != srcLim)
  3546. continue;
  3547. break;
  3548. }
  3549. dest++;
  3550. if (++src == srcLim)
  3551. break;
  3552. if ((*src & 0xF0) != 0x80)
  3553. continue;
  3554. *dest = *src;
  3555. break;
  3556. }
  3557. num = src - p->bufs[BCJ2_STREAM_MAIN];
  3558. if (src == srcLim)
  3559. {
  3560. p->temp[3] = src[-1];
  3561. p->bufs[BCJ2_STREAM_MAIN] = src;
  3562. p->ip += (UInt32)num;
  3563. p->dest += num;
  3564. p->state =
  3565. p->bufs[BCJ2_STREAM_MAIN] ==
  3566. p->lims[BCJ2_STREAM_MAIN] ?
  3567. (unsigned)BCJ2_STREAM_MAIN :
  3568. (unsigned)BCJ2_DEC_STATE_ORIG;
  3569. return SZ_OK;
  3570. }
  3571. {
  3572. UInt32 bound, ttt;
  3573. CProb *prob;
  3574. Byte b = src[0];
  3575. Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
  3576. p->temp[3] = b;
  3577. p->bufs[BCJ2_STREAM_MAIN] = src + 1;
  3578. num++;
  3579. p->ip += (UInt32)num;
  3580. p->dest += num;
  3581. prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
  3582. _IF_BIT_0
  3583. {
  3584. _UPDATE_0
  3585. continue;
  3586. }
  3587. _UPDATE_1
  3588. }
  3589. }
  3590. }
  3591. {
  3592. UInt32 val;
  3593. unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
  3594. const Byte *cur = p->bufs[cj];
  3595. Byte *dest;
  3596. SizeT rem;
  3597. if (cur == p->lims[cj])
  3598. {
  3599. p->state = cj;
  3600. break;
  3601. }
  3602. val = GetBe32(cur);
  3603. p->bufs[cj] = cur + 4;
  3604. p->ip += 4;
  3605. val -= p->ip;
  3606. dest = p->dest;
  3607. rem = p->destLim - dest;
  3608. if (rem < 4)
  3609. {
  3610. SizeT i;
  3611. SetUi32(p->temp, val);
  3612. for (i = 0; i < rem; i++)
  3613. dest[i] = p->temp[i];
  3614. p->dest = dest + rem;
  3615. p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
  3616. break;
  3617. }
  3618. SetUi32(dest, val);
  3619. p->temp[3] = (Byte)(val >> 24);
  3620. p->dest = dest + 4;
  3621. }
  3622. }
  3623. if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC])
  3624. {
  3625. p->range <<= 8;
  3626. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  3627. }
  3628. return SZ_OK;
  3629. }
  3630. #undef kTopValue /* reused later. --ryan. */
  3631. #undef kBitModelTotal /* reused later. --ryan. */
  3632. /* Bra.c -- Converters for RISC code
  3633. 2010-04-16 : Igor Pavlov : Public domain */
  3634. /*
  3635. #include "Precomp.h"
  3636. #include "Bra.h"
  3637. */
  3638. static SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  3639. {
  3640. SizeT i;
  3641. if (size < 4)
  3642. return 0;
  3643. size -= 4;
  3644. ip += 8;
  3645. for (i = 0; i <= size; i += 4)
  3646. {
  3647. if (data[i + 3] == 0xEB)
  3648. {
  3649. UInt32 dest;
  3650. UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
  3651. src <<= 2;
  3652. if (encoding)
  3653. dest = ip + (UInt32)i + src;
  3654. else
  3655. dest = src - (ip + (UInt32)i);
  3656. dest >>= 2;
  3657. data[i + 2] = (Byte)(dest >> 16);
  3658. data[i + 1] = (Byte)(dest >> 8);
  3659. data[i + 0] = (Byte)dest;
  3660. }
  3661. }
  3662. return i;
  3663. }
  3664. static SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  3665. {
  3666. SizeT i;
  3667. if (size < 4)
  3668. return 0;
  3669. size -= 4;
  3670. ip += 4;
  3671. for (i = 0; i <= size; i += 2)
  3672. {
  3673. if ((data[i + 1] & 0xF8) == 0xF0 &&
  3674. (data[i + 3] & 0xF8) == 0xF8)
  3675. {
  3676. UInt32 dest;
  3677. UInt32 src =
  3678. (((UInt32)data[i + 1] & 0x7) << 19) |
  3679. ((UInt32)data[i + 0] << 11) |
  3680. (((UInt32)data[i + 3] & 0x7) << 8) |
  3681. (data[i + 2]);
  3682. src <<= 1;
  3683. if (encoding)
  3684. dest = ip + (UInt32)i + src;
  3685. else
  3686. dest = src - (ip + (UInt32)i);
  3687. dest >>= 1;
  3688. data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
  3689. data[i + 0] = (Byte)(dest >> 11);
  3690. data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
  3691. data[i + 2] = (Byte)dest;
  3692. i += 2;
  3693. }
  3694. }
  3695. return i;
  3696. }
  3697. static SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  3698. {
  3699. SizeT i;
  3700. if (size < 4)
  3701. return 0;
  3702. size -= 4;
  3703. for (i = 0; i <= size; i += 4)
  3704. {
  3705. if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
  3706. {
  3707. UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
  3708. ((UInt32)data[i + 1] << 16) |
  3709. ((UInt32)data[i + 2] << 8) |
  3710. ((UInt32)data[i + 3] & (~3));
  3711. UInt32 dest;
  3712. if (encoding)
  3713. dest = ip + (UInt32)i + src;
  3714. else
  3715. dest = src - (ip + (UInt32)i);
  3716. data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
  3717. data[i + 1] = (Byte)(dest >> 16);
  3718. data[i + 2] = (Byte)(dest >> 8);
  3719. data[i + 3] &= 0x3;
  3720. data[i + 3] |= dest;
  3721. }
  3722. }
  3723. return i;
  3724. }
  3725. static SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  3726. {
  3727. UInt32 i;
  3728. if (size < 4)
  3729. return 0;
  3730. size -= 4;
  3731. for (i = 0; i <= size; i += 4)
  3732. {
  3733. if ((data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00) ||
  3734. (data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0))
  3735. {
  3736. UInt32 src =
  3737. ((UInt32)data[i + 0] << 24) |
  3738. ((UInt32)data[i + 1] << 16) |
  3739. ((UInt32)data[i + 2] << 8) |
  3740. ((UInt32)data[i + 3]);
  3741. UInt32 dest;
  3742. src <<= 2;
  3743. if (encoding)
  3744. dest = ip + i + src;
  3745. else
  3746. dest = src - (ip + i);
  3747. dest >>= 2;
  3748. dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
  3749. data[i + 0] = (Byte)(dest >> 24);
  3750. data[i + 1] = (Byte)(dest >> 16);
  3751. data[i + 2] = (Byte)(dest >> 8);
  3752. data[i + 3] = (Byte)dest;
  3753. }
  3754. }
  3755. return i;
  3756. }
  3757. /* Bra86.c -- Converter for x86 code (BCJ)
  3758. 2013-11-12 : Igor Pavlov : Public domain */
  3759. /*
  3760. #include "Precomp.h"
  3761. #include "Bra.h"
  3762. */
  3763. #define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
  3764. static SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
  3765. {
  3766. SizeT pos = 0;
  3767. UInt32 mask = *state & 7;
  3768. if (size < 5)
  3769. return 0;
  3770. size -= 4;
  3771. ip += 5;
  3772. for (;;)
  3773. {
  3774. Byte *p = data + pos;
  3775. const Byte *limit = data + size;
  3776. for (; p < limit; p++)
  3777. if ((*p & 0xFE) == 0xE8)
  3778. break;
  3779. {
  3780. SizeT d = (SizeT)(p - data - pos);
  3781. pos = (SizeT)(p - data);
  3782. if (p >= limit)
  3783. {
  3784. *state = (d > 2 ? 0 : mask >> (unsigned)d);
  3785. return pos;
  3786. }
  3787. if (d > 2)
  3788. mask = 0;
  3789. else
  3790. {
  3791. mask >>= (unsigned)d;
  3792. if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(mask >> 1) + 1])))
  3793. {
  3794. mask = (mask >> 1) | 4;
  3795. pos++;
  3796. continue;
  3797. }
  3798. }
  3799. }
  3800. if (Test86MSByte(p[4]))
  3801. {
  3802. UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
  3803. UInt32 cur = ip + (UInt32)pos;
  3804. pos += 5;
  3805. if (encoding)
  3806. v += cur;
  3807. else
  3808. v -= cur;
  3809. if (mask != 0)
  3810. {
  3811. unsigned sh = (mask & 6) << 2;
  3812. if (Test86MSByte((Byte)(v >> sh)))
  3813. {
  3814. v ^= (((UInt32)0x100 << sh) - 1);
  3815. if (encoding)
  3816. v += cur;
  3817. else
  3818. v -= cur;
  3819. }
  3820. mask = 0;
  3821. }
  3822. p[1] = (Byte)v;
  3823. p[2] = (Byte)(v >> 8);
  3824. p[3] = (Byte)(v >> 16);
  3825. p[4] = (Byte)(0 - ((v >> 24) & 1));
  3826. }
  3827. else
  3828. {
  3829. mask = (mask >> 1) | 4;
  3830. pos++;
  3831. }
  3832. }
  3833. }
  3834. /* BraIA64.c -- Converter for IA-64 code
  3835. 2013-11-12 : Igor Pavlov : Public domain */
  3836. /*
  3837. #include "Precomp.h"
  3838. #include "Bra.h"
  3839. */
  3840. static const Byte kBranchTable[32] =
  3841. {
  3842. 0, 0, 0, 0, 0, 0, 0, 0,
  3843. 0, 0, 0, 0, 0, 0, 0, 0,
  3844. 4, 4, 6, 6, 0, 0, 7, 7,
  3845. 4, 4, 0, 0, 4, 4, 0, 0
  3846. };
  3847. static SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  3848. {
  3849. SizeT i;
  3850. if (size < 16)
  3851. return 0;
  3852. size -= 16;
  3853. for (i = 0; i <= size; i += 16)
  3854. {
  3855. UInt32 instrTemplate = data[i] & 0x1F;
  3856. UInt32 mask = kBranchTable[instrTemplate];
  3857. UInt32 bitPos = 5;
  3858. int slot;
  3859. for (slot = 0; slot < 3; slot++, bitPos += 41)
  3860. {
  3861. UInt32 bytePos, bitRes;
  3862. UInt64 instruction, instNorm;
  3863. int j;
  3864. if (((mask >> slot) & 1) == 0)
  3865. continue;
  3866. bytePos = (bitPos >> 3);
  3867. bitRes = bitPos & 0x7;
  3868. instruction = 0;
  3869. for (j = 0; j < 6; j++)
  3870. instruction += (UInt64)data[i + j + bytePos] << (8 * j);
  3871. instNorm = instruction >> bitRes;
  3872. if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
  3873. {
  3874. UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
  3875. UInt32 dest;
  3876. src |= ((UInt32)(instNorm >> 36) & 1) << 20;
  3877. src <<= 4;
  3878. if (encoding)
  3879. dest = ip + (UInt32)i + src;
  3880. else
  3881. dest = src - (ip + (UInt32)i);
  3882. dest >>= 4;
  3883. instNorm &= ~((UInt64)(0x8FFFFF) << 13);
  3884. instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
  3885. instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
  3886. instruction &= (1 << bitRes) - 1;
  3887. instruction |= (instNorm << bitRes);
  3888. for (j = 0; j < 6; j++)
  3889. data[i + j + bytePos] = (Byte)(instruction >> (8 * j));
  3890. }
  3891. }
  3892. }
  3893. return i;
  3894. }
  3895. /* Delta.c -- Delta converter
  3896. 2009-05-26 : Igor Pavlov : Public domain */
  3897. /*
  3898. #include "Precomp.h"
  3899. #include "Delta.h"
  3900. */
  3901. static void Delta_Init(Byte *state)
  3902. {
  3903. unsigned i;
  3904. for (i = 0; i < DELTA_STATE_SIZE; i++)
  3905. state[i] = 0;
  3906. }
  3907. static void MyMemCpy(Byte *dest, const Byte *src, unsigned size)
  3908. {
  3909. unsigned i;
  3910. for (i = 0; i < size; i++)
  3911. dest[i] = src[i];
  3912. }
  3913. static void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)
  3914. {
  3915. Byte buf[DELTA_STATE_SIZE];
  3916. unsigned j = 0;
  3917. MyMemCpy(buf, state, delta);
  3918. {
  3919. SizeT i;
  3920. for (i = 0; i < size;)
  3921. {
  3922. for (j = 0; j < delta && i < size; i++, j++)
  3923. {
  3924. buf[j] = data[i] = (Byte)(buf[j] + data[i]);
  3925. }
  3926. }
  3927. }
  3928. if (j == delta)
  3929. j = 0;
  3930. MyMemCpy(state, buf + j, delta - j);
  3931. MyMemCpy(state + delta - j, buf, j);
  3932. }
  3933. /* LzmaDec.c -- LZMA Decoder
  3934. 2016-05-16 : Igor Pavlov : Public domain */
  3935. /*
  3936. #include "Precomp.h"
  3937. #include "LzmaDec.h"
  3938. #include <string.h>
  3939. */
  3940. #define kNumTopBits 24
  3941. #define kTopValue ((UInt32)1 << kNumTopBits)
  3942. #define kNumBitModelTotalBits 11
  3943. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  3944. #define kNumMoveBits 5
  3945. #define RC_INIT_SIZE 5
  3946. #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
  3947. #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  3948. #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
  3949. #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
  3950. #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
  3951. { UPDATE_0(p); i = (i + i); A0; } else \
  3952. { UPDATE_1(p); i = (i + i) + 1; A1; }
  3953. #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
  3954. #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
  3955. #define TREE_DECODE(probs, limit, i) \
  3956. { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
  3957. /* #define _LZMA_SIZE_OPT */
  3958. #ifdef _LZMA_SIZE_OPT
  3959. #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
  3960. #else
  3961. #define TREE_6_DECODE(probs, i) \
  3962. { i = 1; \
  3963. TREE_GET_BIT(probs, i); \
  3964. TREE_GET_BIT(probs, i); \
  3965. TREE_GET_BIT(probs, i); \
  3966. TREE_GET_BIT(probs, i); \
  3967. TREE_GET_BIT(probs, i); \
  3968. TREE_GET_BIT(probs, i); \
  3969. i -= 0x40; }
  3970. #endif
  3971. #define NORMAL_LITER_DEC GET_BIT(prob + symbol, symbol)
  3972. #define MATCHED_LITER_DEC \
  3973. matchByte <<= 1; \
  3974. bit = (matchByte & offs); \
  3975. probLit = prob + offs + bit + symbol; \
  3976. GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
  3977. #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
  3978. #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  3979. #define UPDATE_0_CHECK range = bound;
  3980. #define UPDATE_1_CHECK range -= bound; code -= bound;
  3981. #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
  3982. { UPDATE_0_CHECK; i = (i + i); A0; } else \
  3983. { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
  3984. #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
  3985. #define TREE_DECODE_CHECK(probs, limit, i) \
  3986. { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
  3987. #define kNumPosBitsMax 4
  3988. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  3989. #define kLenNumLowBits 3
  3990. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  3991. #define kLenNumMidBits 3
  3992. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  3993. #define kLenNumHighBits 8
  3994. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  3995. #define LenChoice 0
  3996. #define LenChoice2 (LenChoice + 1)
  3997. #define LenLow (LenChoice2 + 1)
  3998. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  3999. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  4000. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  4001. #define kNumStates 12
  4002. #define kNumLitStates 7
  4003. #define kStartPosModelIndex 4
  4004. #define kEndPosModelIndex 14
  4005. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  4006. #define kNumPosSlotBits 6
  4007. #define kNumLenToPosStates 4
  4008. #define kNumAlignBits 4
  4009. #define kAlignTableSize (1 << kNumAlignBits)
  4010. #define kMatchMinLen 2
  4011. #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  4012. #define IsMatch 0
  4013. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  4014. #define IsRepG0 (IsRep + kNumStates)
  4015. #define IsRepG1 (IsRepG0 + kNumStates)
  4016. #define IsRepG2 (IsRepG1 + kNumStates)
  4017. #define IsRep0Long (IsRepG2 + kNumStates)
  4018. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  4019. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  4020. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  4021. #define LenCoder (Align + kAlignTableSize)
  4022. #define RepLenCoder (LenCoder + kNumLenProbs)
  4023. #define Literal (RepLenCoder + kNumLenProbs)
  4024. #define LZMA_BASE_SIZE 1846
  4025. #define LZMA_LIT_SIZE 0x300
  4026. #if Literal != LZMA_BASE_SIZE
  4027. StopCompilingDueBUG
  4028. #endif
  4029. #define LzmaProps_GetNumProbs(p) (Literal + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
  4030. #define LZMA_DIC_MIN (1 << 12)
  4031. /* First LZMA-symbol is always decoded.
  4032. And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
  4033. Out:
  4034. Result:
  4035. SZ_OK - OK
  4036. SZ_ERROR_DATA - Error
  4037. p->remainLen:
  4038. < kMatchSpecLenStart : normal remain
  4039. = kMatchSpecLenStart : finished
  4040. = kMatchSpecLenStart + 1 : Flush marker (unused now)
  4041. = kMatchSpecLenStart + 2 : State Init Marker (unused now)
  4042. */
  4043. static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  4044. {
  4045. CLzmaProb *probs = p->probs;
  4046. unsigned state = p->state;
  4047. UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
  4048. unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
  4049. unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
  4050. unsigned lc = p->prop.lc;
  4051. Byte *dic = p->dic;
  4052. SizeT dicBufSize = p->dicBufSize;
  4053. SizeT dicPos = p->dicPos;
  4054. UInt32 processedPos = p->processedPos;
  4055. UInt32 checkDicSize = p->checkDicSize;
  4056. unsigned len = 0;
  4057. const Byte *buf = p->buf;
  4058. UInt32 range = p->range;
  4059. UInt32 code = p->code;
  4060. do
  4061. {
  4062. CLzmaProb *prob;
  4063. UInt32 bound;
  4064. unsigned ttt;
  4065. unsigned posState = processedPos & pbMask;
  4066. prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  4067. IF_BIT_0(prob)
  4068. {
  4069. unsigned symbol;
  4070. UPDATE_0(prob);
  4071. prob = probs + Literal;
  4072. if (processedPos != 0 || checkDicSize != 0)
  4073. prob += ((UInt32)LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
  4074. (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
  4075. processedPos++;
  4076. if (state < kNumLitStates)
  4077. {
  4078. state -= (state < 4) ? state : 3;
  4079. symbol = 1;
  4080. #ifdef _LZMA_SIZE_OPT
  4081. do { NORMAL_LITER_DEC } while (symbol < 0x100);
  4082. #else
  4083. NORMAL_LITER_DEC
  4084. NORMAL_LITER_DEC
  4085. NORMAL_LITER_DEC
  4086. NORMAL_LITER_DEC
  4087. NORMAL_LITER_DEC
  4088. NORMAL_LITER_DEC
  4089. NORMAL_LITER_DEC
  4090. NORMAL_LITER_DEC
  4091. #endif
  4092. }
  4093. else
  4094. {
  4095. unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  4096. unsigned offs = 0x100;
  4097. state -= (state < 10) ? 3 : 6;
  4098. symbol = 1;
  4099. #ifdef _LZMA_SIZE_OPT
  4100. do
  4101. {
  4102. unsigned bit;
  4103. CLzmaProb *probLit;
  4104. MATCHED_LITER_DEC
  4105. }
  4106. while (symbol < 0x100);
  4107. #else
  4108. {
  4109. unsigned bit;
  4110. CLzmaProb *probLit;
  4111. MATCHED_LITER_DEC
  4112. MATCHED_LITER_DEC
  4113. MATCHED_LITER_DEC
  4114. MATCHED_LITER_DEC
  4115. MATCHED_LITER_DEC
  4116. MATCHED_LITER_DEC
  4117. MATCHED_LITER_DEC
  4118. MATCHED_LITER_DEC
  4119. }
  4120. #endif
  4121. }
  4122. dic[dicPos++] = (Byte)symbol;
  4123. continue;
  4124. }
  4125. {
  4126. UPDATE_1(prob);
  4127. prob = probs + IsRep + state;
  4128. IF_BIT_0(prob)
  4129. {
  4130. UPDATE_0(prob);
  4131. state += kNumStates;
  4132. prob = probs + LenCoder;
  4133. }
  4134. else
  4135. {
  4136. UPDATE_1(prob);
  4137. if (checkDicSize == 0 && processedPos == 0)
  4138. return SZ_ERROR_DATA;
  4139. prob = probs + IsRepG0 + state;
  4140. IF_BIT_0(prob)
  4141. {
  4142. UPDATE_0(prob);
  4143. prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  4144. IF_BIT_0(prob)
  4145. {
  4146. UPDATE_0(prob);
  4147. dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  4148. dicPos++;
  4149. processedPos++;
  4150. state = state < kNumLitStates ? 9 : 11;
  4151. continue;
  4152. }
  4153. UPDATE_1(prob);
  4154. }
  4155. else
  4156. {
  4157. UInt32 distance;
  4158. UPDATE_1(prob);
  4159. prob = probs + IsRepG1 + state;
  4160. IF_BIT_0(prob)
  4161. {
  4162. UPDATE_0(prob);
  4163. distance = rep1;
  4164. }
  4165. else
  4166. {
  4167. UPDATE_1(prob);
  4168. prob = probs + IsRepG2 + state;
  4169. IF_BIT_0(prob)
  4170. {
  4171. UPDATE_0(prob);
  4172. distance = rep2;
  4173. }
  4174. else
  4175. {
  4176. UPDATE_1(prob);
  4177. distance = rep3;
  4178. rep3 = rep2;
  4179. }
  4180. rep2 = rep1;
  4181. }
  4182. rep1 = rep0;
  4183. rep0 = distance;
  4184. }
  4185. state = state < kNumLitStates ? 8 : 11;
  4186. prob = probs + RepLenCoder;
  4187. }
  4188. #ifdef _LZMA_SIZE_OPT
  4189. {
  4190. unsigned lim, offset;
  4191. CLzmaProb *probLen = prob + LenChoice;
  4192. IF_BIT_0(probLen)
  4193. {
  4194. UPDATE_0(probLen);
  4195. probLen = prob + LenLow + (posState << kLenNumLowBits);
  4196. offset = 0;
  4197. lim = (1 << kLenNumLowBits);
  4198. }
  4199. else
  4200. {
  4201. UPDATE_1(probLen);
  4202. probLen = prob + LenChoice2;
  4203. IF_BIT_0(probLen)
  4204. {
  4205. UPDATE_0(probLen);
  4206. probLen = prob + LenMid + (posState << kLenNumMidBits);
  4207. offset = kLenNumLowSymbols;
  4208. lim = (1 << kLenNumMidBits);
  4209. }
  4210. else
  4211. {
  4212. UPDATE_1(probLen);
  4213. probLen = prob + LenHigh;
  4214. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  4215. lim = (1 << kLenNumHighBits);
  4216. }
  4217. }
  4218. TREE_DECODE(probLen, lim, len);
  4219. len += offset;
  4220. }
  4221. #else
  4222. {
  4223. CLzmaProb *probLen = prob + LenChoice;
  4224. IF_BIT_0(probLen)
  4225. {
  4226. UPDATE_0(probLen);
  4227. probLen = prob + LenLow + (posState << kLenNumLowBits);
  4228. len = 1;
  4229. TREE_GET_BIT(probLen, len);
  4230. TREE_GET_BIT(probLen, len);
  4231. TREE_GET_BIT(probLen, len);
  4232. len -= 8;
  4233. }
  4234. else
  4235. {
  4236. UPDATE_1(probLen);
  4237. probLen = prob + LenChoice2;
  4238. IF_BIT_0(probLen)
  4239. {
  4240. UPDATE_0(probLen);
  4241. probLen = prob + LenMid + (posState << kLenNumMidBits);
  4242. len = 1;
  4243. TREE_GET_BIT(probLen, len);
  4244. TREE_GET_BIT(probLen, len);
  4245. TREE_GET_BIT(probLen, len);
  4246. }
  4247. else
  4248. {
  4249. UPDATE_1(probLen);
  4250. probLen = prob + LenHigh;
  4251. TREE_DECODE(probLen, (1 << kLenNumHighBits), len);
  4252. len += kLenNumLowSymbols + kLenNumMidSymbols;
  4253. }
  4254. }
  4255. }
  4256. #endif
  4257. if (state >= kNumStates)
  4258. {
  4259. UInt32 distance;
  4260. prob = probs + PosSlot +
  4261. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
  4262. TREE_6_DECODE(prob, distance);
  4263. if (distance >= kStartPosModelIndex)
  4264. {
  4265. unsigned posSlot = (unsigned)distance;
  4266. unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
  4267. distance = (2 | (distance & 1));
  4268. if (posSlot < kEndPosModelIndex)
  4269. {
  4270. distance <<= numDirectBits;
  4271. prob = probs + SpecPos + distance - posSlot - 1;
  4272. {
  4273. UInt32 mask = 1;
  4274. unsigned i = 1;
  4275. do
  4276. {
  4277. GET_BIT2(prob + i, i, ; , distance |= mask);
  4278. mask <<= 1;
  4279. }
  4280. while (--numDirectBits != 0);
  4281. }
  4282. }
  4283. else
  4284. {
  4285. numDirectBits -= kNumAlignBits;
  4286. do
  4287. {
  4288. NORMALIZE
  4289. range >>= 1;
  4290. {
  4291. UInt32 t;
  4292. code -= range;
  4293. t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
  4294. distance = (distance << 1) + (t + 1);
  4295. code += range & t;
  4296. }
  4297. /*
  4298. distance <<= 1;
  4299. if (code >= range)
  4300. {
  4301. code -= range;
  4302. distance |= 1;
  4303. }
  4304. */
  4305. }
  4306. while (--numDirectBits != 0);
  4307. prob = probs + Align;
  4308. distance <<= kNumAlignBits;
  4309. {
  4310. unsigned i = 1;
  4311. GET_BIT2(prob + i, i, ; , distance |= 1);
  4312. GET_BIT2(prob + i, i, ; , distance |= 2);
  4313. GET_BIT2(prob + i, i, ; , distance |= 4);
  4314. GET_BIT2(prob + i, i, ; , distance |= 8);
  4315. }
  4316. if (distance == (UInt32)0xFFFFFFFF)
  4317. {
  4318. len += kMatchSpecLenStart;
  4319. state -= kNumStates;
  4320. break;
  4321. }
  4322. }
  4323. }
  4324. rep3 = rep2;
  4325. rep2 = rep1;
  4326. rep1 = rep0;
  4327. rep0 = distance + 1;
  4328. if (checkDicSize == 0)
  4329. {
  4330. if (distance >= processedPos)
  4331. {
  4332. p->dicPos = dicPos;
  4333. return SZ_ERROR_DATA;
  4334. }
  4335. }
  4336. else if (distance >= checkDicSize)
  4337. {
  4338. p->dicPos = dicPos;
  4339. return SZ_ERROR_DATA;
  4340. }
  4341. state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
  4342. }
  4343. len += kMatchMinLen;
  4344. {
  4345. SizeT rem;
  4346. unsigned curLen;
  4347. SizeT pos;
  4348. if ((rem = limit - dicPos) == 0)
  4349. {
  4350. p->dicPos = dicPos;
  4351. return SZ_ERROR_DATA;
  4352. }
  4353. curLen = ((rem < len) ? (unsigned)rem : len);
  4354. pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
  4355. processedPos += curLen;
  4356. len -= curLen;
  4357. if (curLen <= dicBufSize - pos)
  4358. {
  4359. Byte *dest = dic + dicPos;
  4360. ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
  4361. const Byte *lim = dest + curLen;
  4362. dicPos += curLen;
  4363. do
  4364. *(dest) = (Byte)*(dest + src);
  4365. while (++dest != lim);
  4366. }
  4367. else
  4368. {
  4369. do
  4370. {
  4371. dic[dicPos++] = dic[pos];
  4372. if (++pos == dicBufSize)
  4373. pos = 0;
  4374. }
  4375. while (--curLen != 0);
  4376. }
  4377. }
  4378. }
  4379. }
  4380. while (dicPos < limit && buf < bufLimit);
  4381. NORMALIZE;
  4382. p->buf = buf;
  4383. p->range = range;
  4384. p->code = code;
  4385. p->remainLen = len;
  4386. p->dicPos = dicPos;
  4387. p->processedPos = processedPos;
  4388. p->reps[0] = rep0;
  4389. p->reps[1] = rep1;
  4390. p->reps[2] = rep2;
  4391. p->reps[3] = rep3;
  4392. p->state = state;
  4393. return SZ_OK;
  4394. }
  4395. static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
  4396. {
  4397. if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
  4398. {
  4399. Byte *dic = p->dic;
  4400. SizeT dicPos = p->dicPos;
  4401. SizeT dicBufSize = p->dicBufSize;
  4402. unsigned len = p->remainLen;
  4403. SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */
  4404. SizeT rem = limit - dicPos;
  4405. if (rem < len)
  4406. len = (unsigned)(rem);
  4407. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
  4408. p->checkDicSize = p->prop.dicSize;
  4409. p->processedPos += len;
  4410. p->remainLen -= len;
  4411. while (len != 0)
  4412. {
  4413. len--;
  4414. dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  4415. dicPos++;
  4416. }
  4417. p->dicPos = dicPos;
  4418. }
  4419. }
  4420. static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  4421. {
  4422. do
  4423. {
  4424. SizeT limit2 = limit;
  4425. if (p->checkDicSize == 0)
  4426. {
  4427. UInt32 rem = p->prop.dicSize - p->processedPos;
  4428. if (limit - p->dicPos > rem)
  4429. limit2 = p->dicPos + rem;
  4430. }
  4431. RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
  4432. if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize)
  4433. p->checkDicSize = p->prop.dicSize;
  4434. LzmaDec_WriteRem(p, limit);
  4435. }
  4436. while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
  4437. if (p->remainLen > kMatchSpecLenStart)
  4438. p->remainLen = kMatchSpecLenStart;
  4439. return 0;
  4440. }
  4441. typedef enum
  4442. {
  4443. DUMMY_ERROR, /* unexpected end of input stream */
  4444. DUMMY_LIT,
  4445. DUMMY_MATCH,
  4446. DUMMY_REP
  4447. } ELzmaDummy;
  4448. static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
  4449. {
  4450. UInt32 range = p->range;
  4451. UInt32 code = p->code;
  4452. const Byte *bufLimit = buf + inSize;
  4453. const CLzmaProb *probs = p->probs;
  4454. unsigned state = p->state;
  4455. ELzmaDummy res;
  4456. {
  4457. const CLzmaProb *prob;
  4458. UInt32 bound;
  4459. unsigned ttt;
  4460. unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
  4461. prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  4462. IF_BIT_0_CHECK(prob)
  4463. {
  4464. UPDATE_0_CHECK
  4465. /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
  4466. prob = probs + Literal;
  4467. if (p->checkDicSize != 0 || p->processedPos != 0)
  4468. prob += ((UInt32)LZMA_LIT_SIZE *
  4469. ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
  4470. (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
  4471. if (state < kNumLitStates)
  4472. {
  4473. unsigned symbol = 1;
  4474. do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
  4475. }
  4476. else
  4477. {
  4478. unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
  4479. (p->dicPos < p->reps[0] ? p->dicBufSize : 0)];
  4480. unsigned offs = 0x100;
  4481. unsigned symbol = 1;
  4482. do
  4483. {
  4484. unsigned bit;
  4485. const CLzmaProb *probLit;
  4486. matchByte <<= 1;
  4487. bit = (matchByte & offs);
  4488. probLit = prob + offs + bit + symbol;
  4489. GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
  4490. }
  4491. while (symbol < 0x100);
  4492. }
  4493. res = DUMMY_LIT;
  4494. }
  4495. else
  4496. {
  4497. unsigned len;
  4498. UPDATE_1_CHECK;
  4499. prob = probs + IsRep + state;
  4500. IF_BIT_0_CHECK(prob)
  4501. {
  4502. UPDATE_0_CHECK;
  4503. state = 0;
  4504. prob = probs + LenCoder;
  4505. res = DUMMY_MATCH;
  4506. }
  4507. else
  4508. {
  4509. UPDATE_1_CHECK;
  4510. res = DUMMY_REP;
  4511. prob = probs + IsRepG0 + state;
  4512. IF_BIT_0_CHECK(prob)
  4513. {
  4514. UPDATE_0_CHECK;
  4515. prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  4516. IF_BIT_0_CHECK(prob)
  4517. {
  4518. UPDATE_0_CHECK;
  4519. NORMALIZE_CHECK;
  4520. return DUMMY_REP;
  4521. }
  4522. else
  4523. {
  4524. UPDATE_1_CHECK;
  4525. }
  4526. }
  4527. else
  4528. {
  4529. UPDATE_1_CHECK;
  4530. prob = probs + IsRepG1 + state;
  4531. IF_BIT_0_CHECK(prob)
  4532. {
  4533. UPDATE_0_CHECK;
  4534. }
  4535. else
  4536. {
  4537. UPDATE_1_CHECK;
  4538. prob = probs + IsRepG2 + state;
  4539. IF_BIT_0_CHECK(prob)
  4540. {
  4541. UPDATE_0_CHECK;
  4542. }
  4543. else
  4544. {
  4545. UPDATE_1_CHECK;
  4546. }
  4547. }
  4548. }
  4549. state = kNumStates;
  4550. prob = probs + RepLenCoder;
  4551. }
  4552. {
  4553. unsigned limit, offset;
  4554. const CLzmaProb *probLen = prob + LenChoice;
  4555. IF_BIT_0_CHECK(probLen)
  4556. {
  4557. UPDATE_0_CHECK;
  4558. probLen = prob + LenLow + (posState << kLenNumLowBits);
  4559. offset = 0;
  4560. limit = 1 << kLenNumLowBits;
  4561. }
  4562. else
  4563. {
  4564. UPDATE_1_CHECK;
  4565. probLen = prob + LenChoice2;
  4566. IF_BIT_0_CHECK(probLen)
  4567. {
  4568. UPDATE_0_CHECK;
  4569. probLen = prob + LenMid + (posState << kLenNumMidBits);
  4570. offset = kLenNumLowSymbols;
  4571. limit = 1 << kLenNumMidBits;
  4572. }
  4573. else
  4574. {
  4575. UPDATE_1_CHECK;
  4576. probLen = prob + LenHigh;
  4577. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  4578. limit = 1 << kLenNumHighBits;
  4579. }
  4580. }
  4581. TREE_DECODE_CHECK(probLen, limit, len);
  4582. len += offset;
  4583. }
  4584. if (state < 4)
  4585. {
  4586. unsigned posSlot;
  4587. prob = probs + PosSlot +
  4588. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  4589. kNumPosSlotBits);
  4590. TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
  4591. if (posSlot >= kStartPosModelIndex)
  4592. {
  4593. unsigned numDirectBits = ((posSlot >> 1) - 1);
  4594. /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
  4595. if (posSlot < kEndPosModelIndex)
  4596. {
  4597. prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
  4598. }
  4599. else
  4600. {
  4601. numDirectBits -= kNumAlignBits;
  4602. do
  4603. {
  4604. NORMALIZE_CHECK
  4605. range >>= 1;
  4606. code -= range & (((code - range) >> 31) - 1);
  4607. /* if (code >= range) code -= range; */
  4608. }
  4609. while (--numDirectBits != 0);
  4610. prob = probs + Align;
  4611. numDirectBits = kNumAlignBits;
  4612. }
  4613. {
  4614. unsigned i = 1;
  4615. do
  4616. {
  4617. GET_BIT_CHECK(prob + i, i);
  4618. }
  4619. while (--numDirectBits != 0);
  4620. }
  4621. }
  4622. }
  4623. }
  4624. }
  4625. NORMALIZE_CHECK;
  4626. return res;
  4627. }
  4628. static void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
  4629. {
  4630. p->needFlush = 1;
  4631. p->remainLen = 0;
  4632. p->tempBufSize = 0;
  4633. if (initDic)
  4634. {
  4635. p->processedPos = 0;
  4636. p->checkDicSize = 0;
  4637. p->needInitState = 1;
  4638. }
  4639. if (initState)
  4640. p->needInitState = 1;
  4641. }
  4642. static void LzmaDec_Init(CLzmaDec *p)
  4643. {
  4644. p->dicPos = 0;
  4645. LzmaDec_InitDicAndState(p, True, True);
  4646. }
  4647. static void LzmaDec_InitStateReal(CLzmaDec *p)
  4648. {
  4649. SizeT numProbs = LzmaProps_GetNumProbs(&p->prop);
  4650. SizeT i;
  4651. CLzmaProb *probs = p->probs;
  4652. for (i = 0; i < numProbs; i++)
  4653. probs[i] = kBitModelTotal >> 1;
  4654. p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
  4655. p->state = 0;
  4656. p->needInitState = 0;
  4657. }
  4658. static SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
  4659. ELzmaFinishMode finishMode, ELzmaStatus *status)
  4660. {
  4661. SizeT inSize = *srcLen;
  4662. (*srcLen) = 0;
  4663. LzmaDec_WriteRem(p, dicLimit);
  4664. *status = LZMA_STATUS_NOT_SPECIFIED;
  4665. while (p->remainLen != kMatchSpecLenStart)
  4666. {
  4667. int checkEndMarkNow;
  4668. if (p->needFlush)
  4669. {
  4670. for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
  4671. p->tempBuf[p->tempBufSize++] = *src++;
  4672. if (p->tempBufSize < RC_INIT_SIZE)
  4673. {
  4674. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  4675. return SZ_OK;
  4676. }
  4677. if (p->tempBuf[0] != 0)
  4678. return SZ_ERROR_DATA;
  4679. p->code =
  4680. ((UInt32)p->tempBuf[1] << 24)
  4681. | ((UInt32)p->tempBuf[2] << 16)
  4682. | ((UInt32)p->tempBuf[3] << 8)
  4683. | ((UInt32)p->tempBuf[4]);
  4684. p->range = 0xFFFFFFFF;
  4685. p->needFlush = 0;
  4686. p->tempBufSize = 0;
  4687. }
  4688. checkEndMarkNow = 0;
  4689. if (p->dicPos >= dicLimit)
  4690. {
  4691. if (p->remainLen == 0 && p->code == 0)
  4692. {
  4693. *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
  4694. return SZ_OK;
  4695. }
  4696. if (finishMode == LZMA_FINISH_ANY)
  4697. {
  4698. *status = LZMA_STATUS_NOT_FINISHED;
  4699. return SZ_OK;
  4700. }
  4701. if (p->remainLen != 0)
  4702. {
  4703. *status = LZMA_STATUS_NOT_FINISHED;
  4704. return SZ_ERROR_DATA;
  4705. }
  4706. checkEndMarkNow = 1;
  4707. }
  4708. if (p->needInitState)
  4709. LzmaDec_InitStateReal(p);
  4710. if (p->tempBufSize == 0)
  4711. {
  4712. SizeT processed;
  4713. const Byte *bufLimit;
  4714. if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  4715. {
  4716. int dummyRes = LzmaDec_TryDummy(p, src, inSize);
  4717. if (dummyRes == DUMMY_ERROR)
  4718. {
  4719. memcpy(p->tempBuf, src, inSize);
  4720. p->tempBufSize = (unsigned)inSize;
  4721. (*srcLen) += inSize;
  4722. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  4723. return SZ_OK;
  4724. }
  4725. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  4726. {
  4727. *status = LZMA_STATUS_NOT_FINISHED;
  4728. return SZ_ERROR_DATA;
  4729. }
  4730. bufLimit = src;
  4731. }
  4732. else
  4733. bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
  4734. p->buf = src;
  4735. if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
  4736. return SZ_ERROR_DATA;
  4737. processed = (SizeT)(p->buf - src);
  4738. (*srcLen) += processed;
  4739. src += processed;
  4740. inSize -= processed;
  4741. }
  4742. else
  4743. {
  4744. unsigned rem = p->tempBufSize, lookAhead = 0;
  4745. while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
  4746. p->tempBuf[rem++] = src[lookAhead++];
  4747. p->tempBufSize = rem;
  4748. if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  4749. {
  4750. int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
  4751. if (dummyRes == DUMMY_ERROR)
  4752. {
  4753. (*srcLen) += lookAhead;
  4754. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  4755. return SZ_OK;
  4756. }
  4757. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  4758. {
  4759. *status = LZMA_STATUS_NOT_FINISHED;
  4760. return SZ_ERROR_DATA;
  4761. }
  4762. }
  4763. p->buf = p->tempBuf;
  4764. if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
  4765. return SZ_ERROR_DATA;
  4766. {
  4767. unsigned kkk = (unsigned)(p->buf - p->tempBuf);
  4768. if (rem < kkk)
  4769. return SZ_ERROR_FAIL; /* some internal error */
  4770. rem -= kkk;
  4771. if (lookAhead < rem)
  4772. return SZ_ERROR_FAIL; /* some internal error */
  4773. lookAhead -= rem;
  4774. }
  4775. (*srcLen) += lookAhead;
  4776. src += lookAhead;
  4777. inSize -= lookAhead;
  4778. p->tempBufSize = 0;
  4779. }
  4780. }
  4781. if (p->code == 0)
  4782. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  4783. return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
  4784. }
  4785. static void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
  4786. {
  4787. alloc->Free(alloc, p->probs);
  4788. p->probs = NULL;
  4789. }
  4790. static SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
  4791. {
  4792. UInt32 dicSize;
  4793. Byte d;
  4794. if (size < LZMA_PROPS_SIZE)
  4795. return SZ_ERROR_UNSUPPORTED;
  4796. else
  4797. dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
  4798. if (dicSize < LZMA_DIC_MIN)
  4799. dicSize = LZMA_DIC_MIN;
  4800. p->dicSize = dicSize;
  4801. d = data[0];
  4802. if (d >= (9 * 5 * 5))
  4803. return SZ_ERROR_UNSUPPORTED;
  4804. p->lc = d % 9;
  4805. d /= 9;
  4806. p->pb = d / 5;
  4807. p->lp = d % 5;
  4808. return SZ_OK;
  4809. }
  4810. static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
  4811. {
  4812. UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
  4813. if (!p->probs || numProbs != p->numProbs)
  4814. {
  4815. LzmaDec_FreeProbs(p, alloc);
  4816. p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
  4817. p->numProbs = numProbs;
  4818. if (!p->probs)
  4819. return SZ_ERROR_MEM;
  4820. }
  4821. return SZ_OK;
  4822. }
  4823. static SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
  4824. {
  4825. CLzmaProps propNew;
  4826. RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  4827. RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  4828. p->prop = propNew;
  4829. return SZ_OK;
  4830. }
  4831. /* Lzma2Dec.c -- LZMA2 Decoder
  4832. 2015-11-09 : Igor Pavlov : Public domain */
  4833. /* #define SHOW_DEBUG_INFO */
  4834. /*
  4835. #include "Precomp.h"
  4836. #ifdef SHOW_DEBUG_INFO
  4837. #include <stdio.h>
  4838. #endif
  4839. #include <string.h>
  4840. #include "Lzma2Dec.h"
  4841. */
  4842. /*
  4843. 00000000 - EOS
  4844. 00000001 U U - Uncompressed Reset Dic
  4845. 00000010 U U - Uncompressed No Reset
  4846. 100uuuuu U U P P - LZMA no reset
  4847. 101uuuuu U U P P - LZMA reset state
  4848. 110uuuuu U U P P S - LZMA reset state + new prop
  4849. 111uuuuu U U P P S - LZMA reset state + new prop + reset dic
  4850. u, U - Unpack Size
  4851. P - Pack Size
  4852. S - Props
  4853. */
  4854. #define LZMA2_CONTROL_LZMA (1 << 7)
  4855. #define LZMA2_CONTROL_COPY_NO_RESET 2
  4856. #define LZMA2_CONTROL_COPY_RESET_DIC 1
  4857. #define LZMA2_CONTROL_EOF 0
  4858. #define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & LZMA2_CONTROL_LZMA) == 0)
  4859. #define LZMA2_GET_LZMA_MODE(p) (((p)->control >> 5) & 3)
  4860. #define LZMA2_IS_THERE_PROP(mode) ((mode) >= 2)
  4861. #define LZMA2_LCLP_MAX 4
  4862. #define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
  4863. #ifdef SHOW_DEBUG_INFO
  4864. #define PRF(x) x
  4865. #else
  4866. #define PRF(x)
  4867. #endif
  4868. typedef enum
  4869. {
  4870. LZMA2_STATE_CONTROL,
  4871. LZMA2_STATE_UNPACK0,
  4872. LZMA2_STATE_UNPACK1,
  4873. LZMA2_STATE_PACK0,
  4874. LZMA2_STATE_PACK1,
  4875. LZMA2_STATE_PROP,
  4876. LZMA2_STATE_DATA,
  4877. LZMA2_STATE_DATA_CONT,
  4878. LZMA2_STATE_FINISHED,
  4879. LZMA2_STATE_ERROR
  4880. } ELzma2State;
  4881. static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
  4882. {
  4883. UInt32 dicSize;
  4884. if (prop > 40)
  4885. return SZ_ERROR_UNSUPPORTED;
  4886. dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
  4887. props[0] = (Byte)LZMA2_LCLP_MAX;
  4888. props[1] = (Byte)(dicSize);
  4889. props[2] = (Byte)(dicSize >> 8);
  4890. props[3] = (Byte)(dicSize >> 16);
  4891. props[4] = (Byte)(dicSize >> 24);
  4892. return SZ_OK;
  4893. }
  4894. static SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAlloc *alloc)
  4895. {
  4896. Byte props[LZMA_PROPS_SIZE];
  4897. RINOK(Lzma2Dec_GetOldProps(prop, props));
  4898. return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
  4899. }
  4900. static void Lzma2Dec_Init(CLzma2Dec *p)
  4901. {
  4902. p->state = LZMA2_STATE_CONTROL;
  4903. p->needInitDic = True;
  4904. p->needInitState = True;
  4905. p->needInitProp = True;
  4906. LzmaDec_Init(&p->decoder);
  4907. }
  4908. static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
  4909. {
  4910. switch (p->state)
  4911. {
  4912. case LZMA2_STATE_CONTROL:
  4913. p->control = b;
  4914. PRF(printf("\n %4X ", (unsigned)p->decoder.dicPos));
  4915. PRF(printf(" %2X", (unsigned)b));
  4916. if (p->control == 0)
  4917. return LZMA2_STATE_FINISHED;
  4918. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  4919. {
  4920. if ((p->control & 0x7F) > 2)
  4921. return LZMA2_STATE_ERROR;
  4922. p->unpackSize = 0;
  4923. }
  4924. else
  4925. p->unpackSize = (UInt32)(p->control & 0x1F) << 16;
  4926. return LZMA2_STATE_UNPACK0;
  4927. case LZMA2_STATE_UNPACK0:
  4928. p->unpackSize |= (UInt32)b << 8;
  4929. return LZMA2_STATE_UNPACK1;
  4930. case LZMA2_STATE_UNPACK1:
  4931. p->unpackSize |= (UInt32)b;
  4932. p->unpackSize++;
  4933. PRF(printf(" %8u", (unsigned)p->unpackSize));
  4934. return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
  4935. case LZMA2_STATE_PACK0:
  4936. p->packSize = (UInt32)b << 8;
  4937. return LZMA2_STATE_PACK1;
  4938. case LZMA2_STATE_PACK1:
  4939. p->packSize |= (UInt32)b;
  4940. p->packSize++;
  4941. PRF(printf(" %8u", (unsigned)p->packSize));
  4942. return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p)) ? LZMA2_STATE_PROP:
  4943. (p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA);
  4944. case LZMA2_STATE_PROP:
  4945. {
  4946. unsigned lc, lp;
  4947. if (b >= (9 * 5 * 5))
  4948. return LZMA2_STATE_ERROR;
  4949. lc = b % 9;
  4950. b /= 9;
  4951. p->decoder.prop.pb = b / 5;
  4952. lp = b % 5;
  4953. if (lc + lp > LZMA2_LCLP_MAX)
  4954. return LZMA2_STATE_ERROR;
  4955. p->decoder.prop.lc = lc;
  4956. p->decoder.prop.lp = lp;
  4957. p->needInitProp = False;
  4958. return LZMA2_STATE_DATA;
  4959. }
  4960. }
  4961. return LZMA2_STATE_ERROR;
  4962. }
  4963. static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size)
  4964. {
  4965. memcpy(p->dic + p->dicPos, src, size);
  4966. p->dicPos += size;
  4967. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
  4968. p->checkDicSize = p->prop.dicSize;
  4969. p->processedPos += (UInt32)size;
  4970. }
  4971. static void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState);
  4972. static SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
  4973. const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  4974. {
  4975. SizeT inSize = *srcLen;
  4976. *srcLen = 0;
  4977. *status = LZMA_STATUS_NOT_SPECIFIED;
  4978. while (p->state != LZMA2_STATE_FINISHED)
  4979. {
  4980. SizeT dicPos = p->decoder.dicPos;
  4981. if (p->state == LZMA2_STATE_ERROR)
  4982. return SZ_ERROR_DATA;
  4983. if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
  4984. {
  4985. *status = LZMA_STATUS_NOT_FINISHED;
  4986. return SZ_OK;
  4987. }
  4988. if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
  4989. {
  4990. if (*srcLen == inSize)
  4991. {
  4992. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  4993. return SZ_OK;
  4994. }
  4995. (*srcLen)++;
  4996. p->state = Lzma2Dec_UpdateState(p, *src++);
  4997. if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
  4998. {
  4999. p->state = LZMA2_STATE_ERROR;
  5000. return SZ_ERROR_DATA;
  5001. }
  5002. continue;
  5003. }
  5004. {
  5005. SizeT destSizeCur = dicLimit - dicPos;
  5006. SizeT srcSizeCur = inSize - *srcLen;
  5007. ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
  5008. if (p->unpackSize <= destSizeCur)
  5009. {
  5010. destSizeCur = (SizeT)p->unpackSize;
  5011. curFinishMode = LZMA_FINISH_END;
  5012. }
  5013. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  5014. {
  5015. if (*srcLen == inSize)
  5016. {
  5017. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  5018. return SZ_OK;
  5019. }
  5020. if (p->state == LZMA2_STATE_DATA)
  5021. {
  5022. Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
  5023. if (initDic)
  5024. p->needInitProp = p->needInitState = True;
  5025. else if (p->needInitDic)
  5026. {
  5027. p->state = LZMA2_STATE_ERROR;
  5028. return SZ_ERROR_DATA;
  5029. }
  5030. p->needInitDic = False;
  5031. LzmaDec_InitDicAndState(&p->decoder, initDic, False);
  5032. }
  5033. if (srcSizeCur > destSizeCur)
  5034. srcSizeCur = destSizeCur;
  5035. if (srcSizeCur == 0)
  5036. {
  5037. p->state = LZMA2_STATE_ERROR;
  5038. return SZ_ERROR_DATA;
  5039. }
  5040. LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur);
  5041. src += srcSizeCur;
  5042. *srcLen += srcSizeCur;
  5043. p->unpackSize -= (UInt32)srcSizeCur;
  5044. p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
  5045. }
  5046. else
  5047. {
  5048. SizeT outSizeProcessed;
  5049. SRes res;
  5050. if (p->state == LZMA2_STATE_DATA)
  5051. {
  5052. unsigned mode = LZMA2_GET_LZMA_MODE(p);
  5053. Bool initDic = (mode == 3);
  5054. Bool initState = (mode != 0);
  5055. if ((!initDic && p->needInitDic) || (!initState && p->needInitState))
  5056. {
  5057. p->state = LZMA2_STATE_ERROR;
  5058. return SZ_ERROR_DATA;
  5059. }
  5060. LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
  5061. p->needInitDic = False;
  5062. p->needInitState = False;
  5063. p->state = LZMA2_STATE_DATA_CONT;
  5064. }
  5065. if (srcSizeCur > p->packSize)
  5066. srcSizeCur = (SizeT)p->packSize;
  5067. res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSizeCur, curFinishMode, status);
  5068. src += srcSizeCur;
  5069. *srcLen += srcSizeCur;
  5070. p->packSize -= (UInt32)srcSizeCur;
  5071. outSizeProcessed = p->decoder.dicPos - dicPos;
  5072. p->unpackSize -= (UInt32)outSizeProcessed;
  5073. RINOK(res);
  5074. if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
  5075. return res;
  5076. if (srcSizeCur == 0 && outSizeProcessed == 0)
  5077. {
  5078. if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
  5079. || p->unpackSize != 0
  5080. || p->packSize != 0)
  5081. {
  5082. p->state = LZMA2_STATE_ERROR;
  5083. return SZ_ERROR_DATA;
  5084. }
  5085. p->state = LZMA2_STATE_CONTROL;
  5086. }
  5087. if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
  5088. *status = LZMA_STATUS_NOT_FINISHED;
  5089. }
  5090. }
  5091. }
  5092. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  5093. return SZ_OK;
  5094. }
  5095. #endif /* _INCLUDE_PHYSFS_LZMASDK_H_ */
  5096. /* end of physfs_lzmasdk.h ... */