1
0

zstddeclib.c 565 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520
  1. #define malloc v86_malloc
  2. #define free v86_free
  3. #include <stddef.h>
  4. void *calloc(size_t nmemb, size_t size);
  5. void *memset(void *s, int c, size_t n);
  6. void *memcpy(void *dest, const void *src, size_t n);
  7. void *memmove(void *dest, const void *src, size_t n);
  8. void *malloc(size_t size);
  9. void free(void *ptr);
  10. /**
  11. * \file zstddeclib.c
  12. * Single-file Zstandard decompressor.
  13. *
  14. * Generate using:
  15. * \code
  16. * combine.sh -r ../../lib -o zstddeclib.c zstddeclib-in.c
  17. * \endcode
  18. */
  19. /*
  20. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  21. * All rights reserved.
  22. *
  23. * This source code is licensed under both the BSD-style license (found in the
  24. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  25. * in the COPYING file in the root directory of this source tree).
  26. * You may select, at your option, one of the above-listed licenses.
  27. */
  28. /*
  29. * Settings to bake for the standalone decompressor.
  30. *
  31. * Note: It's important that none of these affects 'zstd.h' (only the
  32. * implementation files we're amalgamating).
  33. *
  34. * Note: MEM_MODULE stops xxhash redefining BYTE, U16, etc., which are also
  35. * defined in mem.h (breaking C99 compatibility).
  36. *
  37. * Note: the undefs for xxHash allow Zstd's implementation to coinside with with
  38. * standalone xxHash usage (with global defines).
  39. */
  40. #define DEBUGLEVEL 0
  41. #define MEM_MODULE
  42. #undef XXH_NAMESPACE
  43. #define XXH_NAMESPACE ZSTD_
  44. #undef XXH_PRIVATE_API
  45. #define XXH_PRIVATE_API
  46. #undef XXH_INLINE_ALL
  47. #define XXH_INLINE_ALL
  48. #define ZSTD_LEGACY_SUPPORT 0
  49. #define ZSTD_LIB_COMPRESSION 0
  50. #define ZSTD_LIB_DEPRECATED 0
  51. #define ZSTD_NOBENCH
  52. #define ZSTD_STRIP_ERROR_STRINGS
  53. /**** start inlining common/debug.c ****/
  54. /* ******************************************************************
  55. * debug
  56. * Part of FSE library
  57. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  58. *
  59. * You can contact the author at :
  60. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  61. *
  62. * This source code is licensed under both the BSD-style license (found in the
  63. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  64. * in the COPYING file in the root directory of this source tree).
  65. * You may select, at your option, one of the above-listed licenses.
  66. ****************************************************************** */
  67. /*
  68. * This module only hosts one global variable
  69. * which can be used to dynamically influence the verbosity of traces,
  70. * such as DEBUGLOG and RAWLOG
  71. */
  72. /**** start inlining debug.h ****/
  73. /* ******************************************************************
  74. * debug
  75. * Part of FSE library
  76. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  77. *
  78. * You can contact the author at :
  79. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  80. *
  81. * This source code is licensed under both the BSD-style license (found in the
  82. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  83. * in the COPYING file in the root directory of this source tree).
  84. * You may select, at your option, one of the above-listed licenses.
  85. ****************************************************************** */
  86. /*
  87. * The purpose of this header is to enable debug functions.
  88. * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,
  89. * and DEBUG_STATIC_ASSERT() for compile-time.
  90. *
  91. * By default, DEBUGLEVEL==0, which means run-time debug is disabled.
  92. *
  93. * Level 1 enables assert() only.
  94. * Starting level 2, traces can be generated and pushed to stderr.
  95. * The higher the level, the more verbose the traces.
  96. *
  97. * It's possible to dynamically adjust level using variable g_debug_level,
  98. * which is only declared if DEBUGLEVEL>=2,
  99. * and is a global variable, not multi-thread protected (use with care)
  100. */
  101. #ifndef DEBUG_H_12987983217
  102. #define DEBUG_H_12987983217
  103. #if defined (__cplusplus)
  104. extern "C" {
  105. #endif
  106. /* static assert is triggered at compile time, leaving no runtime artefact.
  107. * static assert only works with compile-time constants.
  108. * Also, this variant can only be used inside a function. */
  109. #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
  110. /* DEBUGLEVEL is expected to be defined externally,
  111. * typically through compiler command line.
  112. * Value must be a number. */
  113. #ifndef DEBUGLEVEL
  114. # define DEBUGLEVEL 0
  115. #endif
  116. /* DEBUGFILE can be defined externally,
  117. * typically through compiler command line.
  118. * note : currently useless.
  119. * Value must be stderr or stdout */
  120. #ifndef DEBUGFILE
  121. # define DEBUGFILE stderr
  122. #endif
  123. /* recommended values for DEBUGLEVEL :
  124. * 0 : release mode, no debug, all run-time checks disabled
  125. * 1 : enables assert() only, no display
  126. * 2 : reserved, for currently active debug path
  127. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  128. * 4 : events once per frame
  129. * 5 : events once per block
  130. * 6 : events once per sequence (verbose)
  131. * 7+: events at every position (*very* verbose)
  132. *
  133. * It's generally inconvenient to output traces > 5.
  134. * In which case, it's possible to selectively trigger high verbosity levels
  135. * by modifying g_debug_level.
  136. */
  137. #if (DEBUGLEVEL>=1)
  138. # include <assert.h>
  139. #else
  140. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  141. # define assert(condition) ((void)0) /* disable assert (default) */
  142. # endif
  143. #endif
  144. #if (DEBUGLEVEL>=2)
  145. # include <stdio.h>
  146. extern int g_debuglevel; /* the variable is only declared,
  147. it actually lives in debug.c,
  148. and is shared by the whole process.
  149. It's not thread-safe.
  150. It's useful when enabling very verbose levels
  151. on selective conditions (such as position in src) */
  152. # define RAWLOG(l, ...) { \
  153. if (l<=g_debuglevel) { \
  154. fprintf(stderr, __VA_ARGS__); \
  155. } }
  156. # define DEBUGLOG(l, ...) { \
  157. if (l<=g_debuglevel) { \
  158. fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
  159. fprintf(stderr, " \n"); \
  160. } }
  161. #else
  162. # define RAWLOG(l, ...) {} /* disabled */
  163. # define DEBUGLOG(l, ...) {} /* disabled */
  164. #endif
  165. #if defined (__cplusplus)
  166. }
  167. #endif
  168. #endif /* DEBUG_H_12987983217 */
  169. /**** ended inlining debug.h ****/
  170. int g_debuglevel = DEBUGLEVEL;
  171. /**** ended inlining common/debug.c ****/
  172. /**** start inlining common/entropy_common.c ****/
  173. /* ******************************************************************
  174. * Common functions of New Generation Entropy library
  175. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  176. *
  177. * You can contact the author at :
  178. * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
  179. * - Public forum : https://groups.google.com/forum/#!forum/lz4c
  180. *
  181. * This source code is licensed under both the BSD-style license (found in the
  182. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  183. * in the COPYING file in the root directory of this source tree).
  184. * You may select, at your option, one of the above-listed licenses.
  185. ****************************************************************** */
  186. /* *************************************
  187. * Dependencies
  188. ***************************************/
  189. /**** start inlining mem.h ****/
  190. /*
  191. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  192. * All rights reserved.
  193. *
  194. * This source code is licensed under both the BSD-style license (found in the
  195. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  196. * in the COPYING file in the root directory of this source tree).
  197. * You may select, at your option, one of the above-listed licenses.
  198. */
  199. #ifndef MEM_H_MODULE
  200. #define MEM_H_MODULE
  201. #if defined (__cplusplus)
  202. extern "C" {
  203. #endif
  204. /*-****************************************
  205. * Dependencies
  206. ******************************************/
  207. #include <stddef.h> /* size_t, ptrdiff_t */
  208. /*-****************************************
  209. * Compiler specifics
  210. ******************************************/
  211. #if defined(_MSC_VER) /* Visual Studio */
  212. # include <intrin.h> /* _byteswap_* */
  213. #endif
  214. #if defined(__GNUC__)
  215. # define MEM_STATIC static __inline __attribute__((unused))
  216. #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  217. # define MEM_STATIC static inline
  218. #elif defined(_MSC_VER)
  219. # define MEM_STATIC static __inline
  220. #else
  221. # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
  222. #endif
  223. #ifndef __has_builtin
  224. # define __has_builtin(x) 0 /* compat. with non-clang compilers */
  225. #endif
  226. /* code only tested on 32 and 64 bits systems */
  227. #define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; }
  228. MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
  229. /* detects whether we are being compiled under msan */
  230. #if defined (__has_feature)
  231. # if __has_feature(memory_sanitizer)
  232. # define MEMORY_SANITIZER 1
  233. # endif
  234. #endif
  235. #if defined (MEMORY_SANITIZER)
  236. /* Not all platforms that support msan provide sanitizers/msan_interface.h.
  237. * We therefore declare the functions we need ourselves, rather than trying to
  238. * include the header file... */
  239. #include <stdint.h> /* intptr_t */
  240. /* Make memory region fully initialized (without changing its contents). */
  241. void __msan_unpoison(const volatile void *a, size_t size);
  242. /* Make memory region fully uninitialized (without changing its contents).
  243. This is a legacy interface that does not update origin information. Use
  244. __msan_allocated_memory() instead. */
  245. void __msan_poison(const volatile void *a, size_t size);
  246. /* Returns the offset of the first (at least partially) poisoned byte in the
  247. memory range, or -1 if the whole range is good. */
  248. intptr_t __msan_test_shadow(const volatile void *x, size_t size);
  249. #endif
  250. /* detects whether we are being compiled under asan */
  251. #if defined (__has_feature)
  252. # if __has_feature(address_sanitizer)
  253. # define ADDRESS_SANITIZER 1
  254. # endif
  255. #elif defined(__SANITIZE_ADDRESS__)
  256. # define ADDRESS_SANITIZER 1
  257. #endif
  258. #if defined (ADDRESS_SANITIZER)
  259. /* Not all platforms that support asan provide sanitizers/asan_interface.h.
  260. * We therefore declare the functions we need ourselves, rather than trying to
  261. * include the header file... */
  262. /**
  263. * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
  264. *
  265. * This memory must be previously allocated by your program. Instrumented
  266. * code is forbidden from accessing addresses in this region until it is
  267. * unpoisoned. This function is not guaranteed to poison the entire region -
  268. * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
  269. * alignment restrictions.
  270. *
  271. * \note This function is not thread-safe because no two threads can poison or
  272. * unpoison memory in the same memory region simultaneously.
  273. *
  274. * \param addr Start of memory region.
  275. * \param size Size of memory region. */
  276. void __asan_poison_memory_region(void const volatile *addr, size_t size);
  277. /**
  278. * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
  279. *
  280. * This memory must be previously allocated by your program. Accessing
  281. * addresses in this region is allowed until this region is poisoned again.
  282. * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
  283. * to ASan alignment restrictions.
  284. *
  285. * \note This function is not thread-safe because no two threads can
  286. * poison or unpoison memory in the same memory region simultaneously.
  287. *
  288. * \param addr Start of memory region.
  289. * \param size Size of memory region. */
  290. void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  291. #endif
  292. /*-**************************************************************
  293. * Basic Types
  294. *****************************************************************/
  295. #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  296. # include <stdint.h>
  297. typedef uint8_t BYTE;
  298. typedef uint16_t U16;
  299. typedef int16_t S16;
  300. typedef uint32_t U32;
  301. typedef int32_t S32;
  302. typedef uint64_t U64;
  303. typedef int64_t S64;
  304. #else
  305. # include <limits.h>
  306. #if CHAR_BIT != 8
  307. # error "this implementation requires char to be exactly 8-bit type"
  308. #endif
  309. typedef unsigned char BYTE;
  310. #if USHRT_MAX != 65535
  311. # error "this implementation requires short to be exactly 16-bit type"
  312. #endif
  313. typedef unsigned short U16;
  314. typedef signed short S16;
  315. #if UINT_MAX != 4294967295
  316. # error "this implementation requires int to be exactly 32-bit type"
  317. #endif
  318. typedef unsigned int U32;
  319. typedef signed int S32;
  320. /* note : there are no limits defined for long long type in C90.
  321. * limits exist in C99, however, in such case, <stdint.h> is preferred */
  322. typedef unsigned long long U64;
  323. typedef signed long long S64;
  324. #endif
  325. /*-**************************************************************
  326. * Memory I/O
  327. *****************************************************************/
  328. /* MEM_FORCE_MEMORY_ACCESS :
  329. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  330. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  331. * The below switch allow to select different access method for improved performance.
  332. * Method 0 (default) : use `memcpy()`. Safe and portable.
  333. * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
  334. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  335. * Method 2 : direct access. This method is portable but violate C standard.
  336. * It can generate buggy code on targets depending on alignment.
  337. * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
  338. * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
  339. * Prefer these methods in priority order (0 > 1 > 2)
  340. */
  341. #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  342. # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  343. # define MEM_FORCE_MEMORY_ACCESS 2
  344. # elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)
  345. # define MEM_FORCE_MEMORY_ACCESS 1
  346. # endif
  347. #endif
  348. MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
  349. MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
  350. MEM_STATIC unsigned MEM_isLittleEndian(void)
  351. {
  352. const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  353. return one.c[0];
  354. }
  355. #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
  356. /* violates C standard, by lying on structure alignment.
  357. Only use if no other choice to achieve best performance on target platform */
  358. MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
  359. MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
  360. MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
  361. MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
  362. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
  363. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
  364. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
  365. #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
  366. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  367. /* currently only defined for gcc and icc */
  368. #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
  369. __pragma( pack(push, 1) )
  370. typedef struct { U16 v; } unalign16;
  371. typedef struct { U32 v; } unalign32;
  372. typedef struct { U64 v; } unalign64;
  373. typedef struct { size_t v; } unalignArch;
  374. __pragma( pack(pop) )
  375. #else
  376. typedef struct { U16 v; } __attribute__((packed)) unalign16;
  377. typedef struct { U32 v; } __attribute__((packed)) unalign32;
  378. typedef struct { U64 v; } __attribute__((packed)) unalign64;
  379. typedef struct { size_t v; } __attribute__((packed)) unalignArch;
  380. #endif
  381. MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign16*)ptr)->v; }
  382. MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; }
  383. MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; }
  384. MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; }
  385. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; }
  386. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; }
  387. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; }
  388. #else
  389. /* default method, safe and standard.
  390. can sometimes prove slower */
  391. MEM_STATIC U16 MEM_read16(const void* memPtr)
  392. {
  393. U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
  394. }
  395. MEM_STATIC U32 MEM_read32(const void* memPtr)
  396. {
  397. U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
  398. }
  399. MEM_STATIC U64 MEM_read64(const void* memPtr)
  400. {
  401. U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
  402. }
  403. MEM_STATIC size_t MEM_readST(const void* memPtr)
  404. {
  405. size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
  406. }
  407. MEM_STATIC void MEM_write16(void* memPtr, U16 value)
  408. {
  409. memcpy(memPtr, &value, sizeof(value));
  410. }
  411. MEM_STATIC void MEM_write32(void* memPtr, U32 value)
  412. {
  413. memcpy(memPtr, &value, sizeof(value));
  414. }
  415. MEM_STATIC void MEM_write64(void* memPtr, U64 value)
  416. {
  417. memcpy(memPtr, &value, sizeof(value));
  418. }
  419. #endif /* MEM_FORCE_MEMORY_ACCESS */
  420. MEM_STATIC U32 MEM_swap32(U32 in)
  421. {
  422. #if defined(_MSC_VER) /* Visual Studio */
  423. return _byteswap_ulong(in);
  424. #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
  425. || (defined(__clang__) && __has_builtin(__builtin_bswap32))
  426. return __builtin_bswap32(in);
  427. #else
  428. return ((in << 24) & 0xff000000 ) |
  429. ((in << 8) & 0x00ff0000 ) |
  430. ((in >> 8) & 0x0000ff00 ) |
  431. ((in >> 24) & 0x000000ff );
  432. #endif
  433. }
  434. MEM_STATIC U64 MEM_swap64(U64 in)
  435. {
  436. #if defined(_MSC_VER) /* Visual Studio */
  437. return _byteswap_uint64(in);
  438. #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
  439. || (defined(__clang__) && __has_builtin(__builtin_bswap64))
  440. return __builtin_bswap64(in);
  441. #else
  442. return ((in << 56) & 0xff00000000000000ULL) |
  443. ((in << 40) & 0x00ff000000000000ULL) |
  444. ((in << 24) & 0x0000ff0000000000ULL) |
  445. ((in << 8) & 0x000000ff00000000ULL) |
  446. ((in >> 8) & 0x00000000ff000000ULL) |
  447. ((in >> 24) & 0x0000000000ff0000ULL) |
  448. ((in >> 40) & 0x000000000000ff00ULL) |
  449. ((in >> 56) & 0x00000000000000ffULL);
  450. #endif
  451. }
  452. MEM_STATIC size_t MEM_swapST(size_t in)
  453. {
  454. if (MEM_32bits())
  455. return (size_t)MEM_swap32((U32)in);
  456. else
  457. return (size_t)MEM_swap64((U64)in);
  458. }
  459. /*=== Little endian r/w ===*/
  460. MEM_STATIC U16 MEM_readLE16(const void* memPtr)
  461. {
  462. if (MEM_isLittleEndian())
  463. return MEM_read16(memPtr);
  464. else {
  465. const BYTE* p = (const BYTE*)memPtr;
  466. return (U16)(p[0] + (p[1]<<8));
  467. }
  468. }
  469. MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
  470. {
  471. if (MEM_isLittleEndian()) {
  472. MEM_write16(memPtr, val);
  473. } else {
  474. BYTE* p = (BYTE*)memPtr;
  475. p[0] = (BYTE)val;
  476. p[1] = (BYTE)(val>>8);
  477. }
  478. }
  479. MEM_STATIC U32 MEM_readLE24(const void* memPtr)
  480. {
  481. return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
  482. }
  483. MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
  484. {
  485. MEM_writeLE16(memPtr, (U16)val);
  486. ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
  487. }
  488. MEM_STATIC U32 MEM_readLE32(const void* memPtr)
  489. {
  490. if (MEM_isLittleEndian())
  491. return MEM_read32(memPtr);
  492. else
  493. return MEM_swap32(MEM_read32(memPtr));
  494. }
  495. MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
  496. {
  497. if (MEM_isLittleEndian())
  498. MEM_write32(memPtr, val32);
  499. else
  500. MEM_write32(memPtr, MEM_swap32(val32));
  501. }
  502. MEM_STATIC U64 MEM_readLE64(const void* memPtr)
  503. {
  504. if (MEM_isLittleEndian())
  505. return MEM_read64(memPtr);
  506. else
  507. return MEM_swap64(MEM_read64(memPtr));
  508. }
  509. MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
  510. {
  511. if (MEM_isLittleEndian())
  512. MEM_write64(memPtr, val64);
  513. else
  514. MEM_write64(memPtr, MEM_swap64(val64));
  515. }
  516. MEM_STATIC size_t MEM_readLEST(const void* memPtr)
  517. {
  518. if (MEM_32bits())
  519. return (size_t)MEM_readLE32(memPtr);
  520. else
  521. return (size_t)MEM_readLE64(memPtr);
  522. }
  523. MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
  524. {
  525. if (MEM_32bits())
  526. MEM_writeLE32(memPtr, (U32)val);
  527. else
  528. MEM_writeLE64(memPtr, (U64)val);
  529. }
  530. /*=== Big endian r/w ===*/
  531. MEM_STATIC U32 MEM_readBE32(const void* memPtr)
  532. {
  533. if (MEM_isLittleEndian())
  534. return MEM_swap32(MEM_read32(memPtr));
  535. else
  536. return MEM_read32(memPtr);
  537. }
  538. MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
  539. {
  540. if (MEM_isLittleEndian())
  541. MEM_write32(memPtr, MEM_swap32(val32));
  542. else
  543. MEM_write32(memPtr, val32);
  544. }
  545. MEM_STATIC U64 MEM_readBE64(const void* memPtr)
  546. {
  547. if (MEM_isLittleEndian())
  548. return MEM_swap64(MEM_read64(memPtr));
  549. else
  550. return MEM_read64(memPtr);
  551. }
  552. MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
  553. {
  554. if (MEM_isLittleEndian())
  555. MEM_write64(memPtr, MEM_swap64(val64));
  556. else
  557. MEM_write64(memPtr, val64);
  558. }
  559. MEM_STATIC size_t MEM_readBEST(const void* memPtr)
  560. {
  561. if (MEM_32bits())
  562. return (size_t)MEM_readBE32(memPtr);
  563. else
  564. return (size_t)MEM_readBE64(memPtr);
  565. }
  566. MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
  567. {
  568. if (MEM_32bits())
  569. MEM_writeBE32(memPtr, (U32)val);
  570. else
  571. MEM_writeBE64(memPtr, (U64)val);
  572. }
  573. #if defined (__cplusplus)
  574. }
  575. #endif
  576. #endif /* MEM_H_MODULE */
  577. /**** ended inlining mem.h ****/
  578. /**** start inlining error_private.h ****/
  579. /*
  580. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  581. * All rights reserved.
  582. *
  583. * This source code is licensed under both the BSD-style license (found in the
  584. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  585. * in the COPYING file in the root directory of this source tree).
  586. * You may select, at your option, one of the above-listed licenses.
  587. */
  588. /* Note : this module is expected to remain private, do not expose it */
  589. #ifndef ERROR_H_MODULE
  590. #define ERROR_H_MODULE
  591. #if defined (__cplusplus)
  592. extern "C" {
  593. #endif
  594. /* ****************************************
  595. * Dependencies
  596. ******************************************/
  597. #include <stddef.h> /* size_t */
  598. /**** start inlining zstd_errors.h ****/
  599. /*
  600. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  601. * All rights reserved.
  602. *
  603. * This source code is licensed under both the BSD-style license (found in the
  604. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  605. * in the COPYING file in the root directory of this source tree).
  606. * You may select, at your option, one of the above-listed licenses.
  607. */
  608. #ifndef ZSTD_ERRORS_H_398273423
  609. #define ZSTD_ERRORS_H_398273423
  610. #if defined (__cplusplus)
  611. extern "C" {
  612. #endif
  613. /*===== dependency =====*/
  614. #include <stddef.h> /* size_t */
  615. /* ===== ZSTDERRORLIB_API : control library symbols visibility ===== */
  616. #ifndef ZSTDERRORLIB_VISIBILITY
  617. # if defined(__GNUC__) && (__GNUC__ >= 4)
  618. # define ZSTDERRORLIB_VISIBILITY __attribute__ ((visibility ("default")))
  619. # else
  620. # define ZSTDERRORLIB_VISIBILITY
  621. # endif
  622. #endif
  623. #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
  624. # define ZSTDERRORLIB_API __declspec(dllexport) ZSTDERRORLIB_VISIBILITY
  625. #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
  626. # define ZSTDERRORLIB_API __declspec(dllimport) ZSTDERRORLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  627. #else
  628. # define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBILITY
  629. #endif
  630. /*-*********************************************
  631. * Error codes list
  632. *-*********************************************
  633. * Error codes _values_ are pinned down since v1.3.1 only.
  634. * Therefore, don't rely on values if you may link to any version < v1.3.1.
  635. *
  636. * Only values < 100 are considered stable.
  637. *
  638. * note 1 : this API shall be used with static linking only.
  639. * dynamic linking is not yet officially supported.
  640. * note 2 : Prefer relying on the enum than on its value whenever possible
  641. * This is the only supported way to use the error list < v1.3.1
  642. * note 3 : ZSTD_isError() is always correct, whatever the library version.
  643. **********************************************/
  644. typedef enum {
  645. ZSTD_error_no_error = 0,
  646. ZSTD_error_GENERIC = 1,
  647. ZSTD_error_prefix_unknown = 10,
  648. ZSTD_error_version_unsupported = 12,
  649. ZSTD_error_frameParameter_unsupported = 14,
  650. ZSTD_error_frameParameter_windowTooLarge = 16,
  651. ZSTD_error_corruption_detected = 20,
  652. ZSTD_error_checksum_wrong = 22,
  653. ZSTD_error_dictionary_corrupted = 30,
  654. ZSTD_error_dictionary_wrong = 32,
  655. ZSTD_error_dictionaryCreation_failed = 34,
  656. ZSTD_error_parameter_unsupported = 40,
  657. ZSTD_error_parameter_outOfBound = 42,
  658. ZSTD_error_tableLog_tooLarge = 44,
  659. ZSTD_error_maxSymbolValue_tooLarge = 46,
  660. ZSTD_error_maxSymbolValue_tooSmall = 48,
  661. ZSTD_error_stage_wrong = 60,
  662. ZSTD_error_init_missing = 62,
  663. ZSTD_error_memory_allocation = 64,
  664. ZSTD_error_workSpace_tooSmall= 66,
  665. ZSTD_error_dstSize_tooSmall = 70,
  666. ZSTD_error_srcSize_wrong = 72,
  667. ZSTD_error_dstBuffer_null = 74,
  668. /* following error codes are __NOT STABLE__, they can be removed or changed in future versions */
  669. ZSTD_error_frameIndex_tooLarge = 100,
  670. ZSTD_error_seekableIO = 102,
  671. ZSTD_error_dstBuffer_wrong = 104,
  672. ZSTD_error_maxCode = 120 /* never EVER use this value directly, it can change in future versions! Use ZSTD_isError() instead */
  673. } ZSTD_ErrorCode;
  674. /*! ZSTD_getErrorCode() :
  675. convert a `size_t` function result into a `ZSTD_ErrorCode` enum type,
  676. which can be used to compare with enum list published above */
  677. ZSTDERRORLIB_API ZSTD_ErrorCode ZSTD_getErrorCode(size_t functionResult);
  678. ZSTDERRORLIB_API const char* ZSTD_getErrorString(ZSTD_ErrorCode code); /**< Same as ZSTD_getErrorName, but using a `ZSTD_ErrorCode` enum argument */
  679. #if defined (__cplusplus)
  680. }
  681. #endif
  682. #endif /* ZSTD_ERRORS_H_398273423 */
  683. /**** ended inlining zstd_errors.h ****/
  684. /* ****************************************
  685. * Compiler-specific
  686. ******************************************/
  687. #if defined(__GNUC__)
  688. # define ERR_STATIC static __attribute__((unused))
  689. #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  690. # define ERR_STATIC static inline
  691. #elif defined(_MSC_VER)
  692. # define ERR_STATIC static __inline
  693. #else
  694. # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
  695. #endif
  696. /*-****************************************
  697. * Customization (error_public.h)
  698. ******************************************/
  699. typedef ZSTD_ErrorCode ERR_enum;
  700. #define PREFIX(name) ZSTD_error_##name
  701. /*-****************************************
  702. * Error codes handling
  703. ******************************************/
  704. #undef ERROR /* already defined on Visual Studio */
  705. #define ERROR(name) ZSTD_ERROR(name)
  706. #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
  707. ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
  708. ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
  709. /* check and forward error code */
  710. #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
  711. #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
  712. /*-****************************************
  713. * Error Strings
  714. ******************************************/
  715. const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
  716. ERR_STATIC const char* ERR_getErrorName(size_t code)
  717. {
  718. return ERR_getErrorString(ERR_getErrorCode(code));
  719. }
  720. #if defined (__cplusplus)
  721. }
  722. #endif
  723. #endif /* ERROR_H_MODULE */
  724. /**** ended inlining error_private.h ****/
  725. #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
  726. /**** start inlining fse.h ****/
  727. /* ******************************************************************
  728. * FSE : Finite State Entropy codec
  729. * Public Prototypes declaration
  730. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  731. *
  732. * You can contact the author at :
  733. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  734. *
  735. * This source code is licensed under both the BSD-style license (found in the
  736. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  737. * in the COPYING file in the root directory of this source tree).
  738. * You may select, at your option, one of the above-listed licenses.
  739. ****************************************************************** */
  740. #if defined (__cplusplus)
  741. extern "C" {
  742. #endif
  743. #ifndef FSE_H
  744. #define FSE_H
  745. /*-*****************************************
  746. * Dependencies
  747. ******************************************/
  748. #include <stddef.h> /* size_t, ptrdiff_t */
  749. /*-*****************************************
  750. * FSE_PUBLIC_API : control library symbols visibility
  751. ******************************************/
  752. #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
  753. # define FSE_PUBLIC_API __attribute__ ((visibility ("default")))
  754. #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
  755. # define FSE_PUBLIC_API __declspec(dllexport)
  756. #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
  757. # define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  758. #else
  759. # define FSE_PUBLIC_API
  760. #endif
  761. /*------ Version ------*/
  762. #define FSE_VERSION_MAJOR 0
  763. #define FSE_VERSION_MINOR 9
  764. #define FSE_VERSION_RELEASE 0
  765. #define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE
  766. #define FSE_QUOTE(str) #str
  767. #define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)
  768. #define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)
  769. #define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE)
  770. FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */
  771. /*-****************************************
  772. * FSE simple functions
  773. ******************************************/
  774. /*! FSE_compress() :
  775. Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'.
  776. 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize).
  777. @return : size of compressed data (<= dstCapacity).
  778. Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
  779. if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.
  780. if FSE_isError(return), compression failed (more details using FSE_getErrorName())
  781. */
  782. FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity,
  783. const void* src, size_t srcSize);
  784. /*! FSE_decompress():
  785. Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',
  786. into already allocated destination buffer 'dst', of size 'dstCapacity'.
  787. @return : size of regenerated data (<= maxDstSize),
  788. or an error code, which can be tested using FSE_isError() .
  789. ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!!
  790. Why ? : making this distinction requires a header.
  791. Header management is intentionally delegated to the user layer, which can better manage special cases.
  792. */
  793. FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity,
  794. const void* cSrc, size_t cSrcSize);
  795. /*-*****************************************
  796. * Tool functions
  797. ******************************************/
  798. FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */
  799. /* Error Management */
  800. FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */
  801. FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */
  802. /*-*****************************************
  803. * FSE advanced functions
  804. ******************************************/
  805. /*! FSE_compress2() :
  806. Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog'
  807. Both parameters can be defined as '0' to mean : use default value
  808. @return : size of compressed data
  809. Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!!
  810. if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression.
  811. if FSE_isError(return), it's an error code.
  812. */
  813. FSE_PUBLIC_API size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
  814. /*-*****************************************
  815. * FSE detailed API
  816. ******************************************/
  817. /*!
  818. FSE_compress() does the following:
  819. 1. count symbol occurrence from source[] into table count[] (see hist.h)
  820. 2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog)
  821. 3. save normalized counters to memory buffer using writeNCount()
  822. 4. build encoding table 'CTable' from normalized counters
  823. 5. encode the data stream using encoding table 'CTable'
  824. FSE_decompress() does the following:
  825. 1. read normalized counters with readNCount()
  826. 2. build decoding table 'DTable' from normalized counters
  827. 3. decode the data stream using decoding table 'DTable'
  828. The following API allows targeting specific sub-functions for advanced tasks.
  829. For example, it's possible to compress several blocks using the same 'CTable',
  830. or to save and provide normalized distribution using external method.
  831. */
  832. /* *** COMPRESSION *** */
  833. /*! FSE_optimalTableLog():
  834. dynamically downsize 'tableLog' when conditions are met.
  835. It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.
  836. @return : recommended tableLog (necessarily <= 'maxTableLog') */
  837. FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
  838. /*! FSE_normalizeCount():
  839. normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
  840. 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
  841. @return : tableLog,
  842. or an errorCode, which can be tested using FSE_isError() */
  843. FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog,
  844. const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
  845. /*! FSE_NCountWriteBound():
  846. Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
  847. Typically useful for allocation purpose. */
  848. FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
  849. /*! FSE_writeNCount():
  850. Compactly save 'normalizedCounter' into 'buffer'.
  851. @return : size of the compressed table,
  852. or an errorCode, which can be tested using FSE_isError(). */
  853. FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize,
  854. const short* normalizedCounter,
  855. unsigned maxSymbolValue, unsigned tableLog);
  856. /*! Constructor and Destructor of FSE_CTable.
  857. Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */
  858. typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */
  859. FSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog);
  860. FSE_PUBLIC_API void FSE_freeCTable (FSE_CTable* ct);
  861. /*! FSE_buildCTable():
  862. Builds `ct`, which must be already allocated, using FSE_createCTable().
  863. @return : 0, or an errorCode, which can be tested using FSE_isError() */
  864. FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
  865. /*! FSE_compress_usingCTable():
  866. Compress `src` using `ct` into `dst` which must be already allocated.
  867. @return : size of compressed data (<= `dstCapacity`),
  868. or 0 if compressed data could not fit into `dst`,
  869. or an errorCode, which can be tested using FSE_isError() */
  870. FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);
  871. /*!
  872. Tutorial :
  873. ----------
  874. The first step is to count all symbols. FSE_count() does this job very fast.
  875. Result will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells.
  876. 'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0]
  877. maxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value)
  878. FSE_count() will return the number of occurrence of the most frequent symbol.
  879. This can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility.
  880. If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
  881. The next step is to normalize the frequencies.
  882. FSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'.
  883. It also guarantees a minimum of 1 to any Symbol with frequency >= 1.
  884. You can use 'tableLog'==0 to mean "use default tableLog value".
  885. If you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(),
  886. which will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means "default").
  887. The result of FSE_normalizeCount() will be saved into a table,
  888. called 'normalizedCounter', which is a table of signed short.
  889. 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells.
  890. The return value is tableLog if everything proceeded as expected.
  891. It is 0 if there is a single symbol within distribution.
  892. If there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()).
  893. 'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount().
  894. 'buffer' must be already allocated.
  895. For guaranteed success, buffer size must be at least FSE_headerBound().
  896. The result of the function is the number of bytes written into 'buffer'.
  897. If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small).
  898. 'normalizedCounter' can then be used to create the compression table 'CTable'.
  899. The space required by 'CTable' must be already allocated, using FSE_createCTable().
  900. You can then use FSE_buildCTable() to fill 'CTable'.
  901. If there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()).
  902. 'CTable' can then be used to compress 'src', with FSE_compress_usingCTable().
  903. Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize'
  904. The function returns the size of compressed data (without header), necessarily <= `dstCapacity`.
  905. If it returns '0', compressed data could not fit into 'dst'.
  906. If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
  907. */
  908. /* *** DECOMPRESSION *** */
  909. /*! FSE_readNCount():
  910. Read compactly saved 'normalizedCounter' from 'rBuffer'.
  911. @return : size read from 'rBuffer',
  912. or an errorCode, which can be tested using FSE_isError().
  913. maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
  914. FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter,
  915. unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
  916. const void* rBuffer, size_t rBuffSize);
  917. /*! Constructor and Destructor of FSE_DTable.
  918. Note that its size depends on 'tableLog' */
  919. typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
  920. FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog);
  921. FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt);
  922. /*! FSE_buildDTable():
  923. Builds 'dt', which must be already allocated, using FSE_createDTable().
  924. return : 0, or an errorCode, which can be tested using FSE_isError() */
  925. FSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
  926. /*! FSE_decompress_usingDTable():
  927. Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
  928. into `dst` which must be already allocated.
  929. @return : size of regenerated data (necessarily <= `dstCapacity`),
  930. or an errorCode, which can be tested using FSE_isError() */
  931. FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
  932. /*!
  933. Tutorial :
  934. ----------
  935. (Note : these functions only decompress FSE-compressed blocks.
  936. If block is uncompressed, use memcpy() instead
  937. If block is a single repeated byte, use memset() instead )
  938. The first step is to obtain the normalized frequencies of symbols.
  939. This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount().
  940. 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short.
  941. In practice, that means it's necessary to know 'maxSymbolValue' beforehand,
  942. or size the table to handle worst case situations (typically 256).
  943. FSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'.
  944. The result of FSE_readNCount() is the number of bytes read from 'rBuffer'.
  945. Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that.
  946. If there is an error, the function will return an error code, which can be tested using FSE_isError().
  947. The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'.
  948. This is performed by the function FSE_buildDTable().
  949. The space required by 'FSE_DTable' must be already allocated using FSE_createDTable().
  950. If there is an error, the function will return an error code, which can be tested using FSE_isError().
  951. `FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable().
  952. `cSrcSize` must be strictly correct, otherwise decompression will fail.
  953. FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`).
  954. If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small)
  955. */
  956. #endif /* FSE_H */
  957. #if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY)
  958. #define FSE_H_FSE_STATIC_LINKING_ONLY
  959. /* *** Dependency *** */
  960. /**** start inlining bitstream.h ****/
  961. /* ******************************************************************
  962. * bitstream
  963. * Part of FSE library
  964. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  965. *
  966. * You can contact the author at :
  967. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  968. *
  969. * This source code is licensed under both the BSD-style license (found in the
  970. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  971. * in the COPYING file in the root directory of this source tree).
  972. * You may select, at your option, one of the above-listed licenses.
  973. ****************************************************************** */
  974. #ifndef BITSTREAM_H_MODULE
  975. #define BITSTREAM_H_MODULE
  976. #if defined (__cplusplus)
  977. extern "C" {
  978. #endif
  979. /*
  980. * This API consists of small unitary functions, which must be inlined for best performance.
  981. * Since link-time-optimization is not available for all compilers,
  982. * these functions are defined into a .h to be included.
  983. */
  984. /*-****************************************
  985. * Dependencies
  986. ******************************************/
  987. /**** skipping file: mem.h ****/
  988. /**** start inlining compiler.h ****/
  989. /*
  990. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  991. * All rights reserved.
  992. *
  993. * This source code is licensed under both the BSD-style license (found in the
  994. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  995. * in the COPYING file in the root directory of this source tree).
  996. * You may select, at your option, one of the above-listed licenses.
  997. */
  998. #ifndef ZSTD_COMPILER_H
  999. #define ZSTD_COMPILER_H
  1000. /*-*******************************************************
  1001. * Compiler specifics
  1002. *********************************************************/
  1003. /* force inlining */
  1004. #if !defined(ZSTD_NO_INLINE)
  1005. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  1006. # define INLINE_KEYWORD inline
  1007. #else
  1008. # define INLINE_KEYWORD
  1009. #endif
  1010. #if defined(__GNUC__) || defined(__ICCARM__)
  1011. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  1012. #elif defined(_MSC_VER)
  1013. # define FORCE_INLINE_ATTR __forceinline
  1014. #else
  1015. # define FORCE_INLINE_ATTR
  1016. #endif
  1017. #else
  1018. #define INLINE_KEYWORD
  1019. #define FORCE_INLINE_ATTR
  1020. #endif
  1021. /**
  1022. On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
  1023. This explictly marks such functions as __cdecl so that the code will still compile
  1024. if a CC other than __cdecl has been made the default.
  1025. */
  1026. #if defined(_MSC_VER)
  1027. # define WIN_CDECL __cdecl
  1028. #else
  1029. # define WIN_CDECL
  1030. #endif
  1031. /**
  1032. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  1033. * parameters. They must be inlined for the compiler to eliminate the constant
  1034. * branches.
  1035. */
  1036. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  1037. /**
  1038. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  1039. * used for "templates", so it can be tweaked based on the compilers
  1040. * performance.
  1041. *
  1042. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  1043. * always_inline attribute.
  1044. *
  1045. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  1046. * attribute.
  1047. */
  1048. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  1049. # define HINT_INLINE static INLINE_KEYWORD
  1050. #else
  1051. # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
  1052. #endif
  1053. /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
  1054. #if defined(__GNUC__)
  1055. # define UNUSED_ATTR __attribute__((unused))
  1056. #else
  1057. # define UNUSED_ATTR
  1058. #endif
  1059. /* force no inlining */
  1060. #ifdef _MSC_VER
  1061. # define FORCE_NOINLINE static __declspec(noinline)
  1062. #else
  1063. # if defined(__GNUC__) || defined(__ICCARM__)
  1064. # define FORCE_NOINLINE static __attribute__((__noinline__))
  1065. # else
  1066. # define FORCE_NOINLINE static
  1067. # endif
  1068. #endif
  1069. /* target attribute */
  1070. #ifndef __has_attribute
  1071. #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
  1072. #endif
  1073. #if defined(__GNUC__) || defined(__ICCARM__)
  1074. # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  1075. #else
  1076. # define TARGET_ATTRIBUTE(target)
  1077. #endif
  1078. /* Enable runtime BMI2 dispatch based on the CPU.
  1079. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  1080. */
  1081. #ifndef DYNAMIC_BMI2
  1082. #if ((defined(__clang__) && __has_attribute(__target__)) \
  1083. || (defined(__GNUC__) \
  1084. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  1085. && (defined(__x86_64__) || defined(_M_X86)) \
  1086. && !defined(__BMI2__)
  1087. # define DYNAMIC_BMI2 1
  1088. #else
  1089. # define DYNAMIC_BMI2 0
  1090. #endif
  1091. #endif
  1092. /* prefetch
  1093. * can be disabled, by declaring NO_PREFETCH build macro */
  1094. #if defined(NO_PREFETCH)
  1095. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  1096. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  1097. #else
  1098. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  1099. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  1100. # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  1101. # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
  1102. # elif defined(__aarch64__)
  1103. # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
  1104. # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
  1105. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  1106. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  1107. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  1108. # else
  1109. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  1110. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  1111. # endif
  1112. #endif /* NO_PREFETCH */
  1113. #define CACHELINE_SIZE 64
  1114. #define PREFETCH_AREA(p, s) { \
  1115. const char* const _ptr = (const char*)(p); \
  1116. size_t const _size = (size_t)(s); \
  1117. size_t _pos; \
  1118. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  1119. PREFETCH_L2(_ptr + _pos); \
  1120. } \
  1121. }
  1122. /* vectorization
  1123. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
  1124. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
  1125. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  1126. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  1127. # else
  1128. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  1129. # endif
  1130. #else
  1131. # define DONT_VECTORIZE
  1132. #endif
  1133. /* Tell the compiler that a branch is likely or unlikely.
  1134. * Only use these macros if it causes the compiler to generate better code.
  1135. * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
  1136. * and clang, please do.
  1137. */
  1138. #if defined(__GNUC__)
  1139. #define LIKELY(x) (__builtin_expect((x), 1))
  1140. #define UNLIKELY(x) (__builtin_expect((x), 0))
  1141. #else
  1142. #define LIKELY(x) (x)
  1143. #define UNLIKELY(x) (x)
  1144. #endif
  1145. /* disable warnings */
  1146. #ifdef _MSC_VER /* Visual Studio */
  1147. # include <intrin.h> /* For Visual 2005 */
  1148. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  1149. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  1150. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  1151. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  1152. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  1153. #endif
  1154. #endif /* ZSTD_COMPILER_H */
  1155. /**** ended inlining compiler.h ****/
  1156. /**** skipping file: debug.h ****/
  1157. /**** skipping file: error_private.h ****/
  1158. /*=========================================
  1159. * Target specific
  1160. =========================================*/
  1161. #if defined(__BMI__) && defined(__GNUC__)
  1162. # include <immintrin.h> /* support for bextr (experimental) */
  1163. #elif defined(__ICCARM__)
  1164. # include <intrinsics.h>
  1165. #endif
  1166. #define STREAM_ACCUMULATOR_MIN_32 25
  1167. #define STREAM_ACCUMULATOR_MIN_64 57
  1168. #define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
  1169. /*-******************************************
  1170. * bitStream encoding API (write forward)
  1171. ********************************************/
  1172. /* bitStream can mix input from multiple sources.
  1173. * A critical property of these streams is that they encode and decode in **reverse** direction.
  1174. * So the first bit sequence you add will be the last to be read, like a LIFO stack.
  1175. */
  1176. typedef struct {
  1177. size_t bitContainer;
  1178. unsigned bitPos;
  1179. char* startPtr;
  1180. char* ptr;
  1181. char* endPtr;
  1182. } BIT_CStream_t;
  1183. MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
  1184. MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
  1185. MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
  1186. MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
  1187. /* Start with initCStream, providing the size of buffer to write into.
  1188. * bitStream will never write outside of this buffer.
  1189. * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
  1190. *
  1191. * bits are first added to a local register.
  1192. * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
  1193. * Writing data into memory is an explicit operation, performed by the flushBits function.
  1194. * Hence keep track how many bits are potentially stored into local register to avoid register overflow.
  1195. * After a flushBits, a maximum of 7 bits might still be stored into local register.
  1196. *
  1197. * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
  1198. *
  1199. * Last operation is to close the bitStream.
  1200. * The function returns the final size of CStream in bytes.
  1201. * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
  1202. */
  1203. /*-********************************************
  1204. * bitStream decoding API (read backward)
  1205. **********************************************/
  1206. typedef struct {
  1207. size_t bitContainer;
  1208. unsigned bitsConsumed;
  1209. const char* ptr;
  1210. const char* start;
  1211. const char* limitPtr;
  1212. } BIT_DStream_t;
  1213. typedef enum { BIT_DStream_unfinished = 0,
  1214. BIT_DStream_endOfBuffer = 1,
  1215. BIT_DStream_completed = 2,
  1216. BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
  1217. /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
  1218. MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
  1219. MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
  1220. MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
  1221. MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
  1222. /* Start by invoking BIT_initDStream().
  1223. * A chunk of the bitStream is then stored into a local register.
  1224. * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
  1225. * You can then retrieve bitFields stored into the local register, **in reverse order**.
  1226. * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
  1227. * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
  1228. * Otherwise, it can be less than that, so proceed accordingly.
  1229. * Checking if DStream has reached its end can be performed with BIT_endOfDStream().
  1230. */
  1231. /*-****************************************
  1232. * unsafe API
  1233. ******************************************/
  1234. MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
  1235. /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
  1236. MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
  1237. /* unsafe version; does not check buffer overflow */
  1238. MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
  1239. /* faster, but works only if nbBits >= 1 */
  1240. /*-**************************************************************
  1241. * Internal functions
  1242. ****************************************************************/
  1243. MEM_STATIC unsigned BIT_highbit32 (U32 val)
  1244. {
  1245. assert(val != 0);
  1246. {
  1247. # if defined(_MSC_VER) /* Visual */
  1248. unsigned long r=0;
  1249. return _BitScanReverse ( &r, val ) ? (unsigned)r : 0;
  1250. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
  1251. return __builtin_clz (val) ^ 31;
  1252. # elif defined(__ICCARM__) /* IAR Intrinsic */
  1253. return 31 - __CLZ(val);
  1254. # else /* Software version */
  1255. static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
  1256. 11, 14, 16, 18, 22, 25, 3, 30,
  1257. 8, 12, 20, 28, 15, 17, 24, 7,
  1258. 19, 27, 23, 6, 26, 5, 4, 31 };
  1259. U32 v = val;
  1260. v |= v >> 1;
  1261. v |= v >> 2;
  1262. v |= v >> 4;
  1263. v |= v >> 8;
  1264. v |= v >> 16;
  1265. return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
  1266. # endif
  1267. }
  1268. }
  1269. /*===== Local Constants =====*/
  1270. static const unsigned BIT_mask[] = {
  1271. 0, 1, 3, 7, 0xF, 0x1F,
  1272. 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
  1273. 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF,
  1274. 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
  1275. 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,
  1276. 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */
  1277. #define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))
  1278. /*-**************************************************************
  1279. * bitStream encoding
  1280. ****************************************************************/
  1281. /*! BIT_initCStream() :
  1282. * `dstCapacity` must be > sizeof(size_t)
  1283. * @return : 0 if success,
  1284. * otherwise an error code (can be tested using ERR_isError()) */
  1285. MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
  1286. void* startPtr, size_t dstCapacity)
  1287. {
  1288. bitC->bitContainer = 0;
  1289. bitC->bitPos = 0;
  1290. bitC->startPtr = (char*)startPtr;
  1291. bitC->ptr = bitC->startPtr;
  1292. bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer);
  1293. if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall);
  1294. return 0;
  1295. }
  1296. /*! BIT_addBits() :
  1297. * can add up to 31 bits into `bitC`.
  1298. * Note : does not check for register overflow ! */
  1299. MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
  1300. size_t value, unsigned nbBits)
  1301. {
  1302. MEM_STATIC_ASSERT(BIT_MASK_SIZE == 32);
  1303. assert(nbBits < BIT_MASK_SIZE);
  1304. assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
  1305. bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
  1306. bitC->bitPos += nbBits;
  1307. }
  1308. /*! BIT_addBitsFast() :
  1309. * works only if `value` is _clean_,
  1310. * meaning all high bits above nbBits are 0 */
  1311. MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
  1312. size_t value, unsigned nbBits)
  1313. {
  1314. assert((value>>nbBits) == 0);
  1315. assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
  1316. bitC->bitContainer |= value << bitC->bitPos;
  1317. bitC->bitPos += nbBits;
  1318. }
  1319. /*! BIT_flushBitsFast() :
  1320. * assumption : bitContainer has not overflowed
  1321. * unsafe version; does not check buffer overflow */
  1322. MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
  1323. {
  1324. size_t const nbBytes = bitC->bitPos >> 3;
  1325. assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
  1326. assert(bitC->ptr <= bitC->endPtr);
  1327. MEM_writeLEST(bitC->ptr, bitC->bitContainer);
  1328. bitC->ptr += nbBytes;
  1329. bitC->bitPos &= 7;
  1330. bitC->bitContainer >>= nbBytes*8;
  1331. }
  1332. /*! BIT_flushBits() :
  1333. * assumption : bitContainer has not overflowed
  1334. * safe version; check for buffer overflow, and prevents it.
  1335. * note : does not signal buffer overflow.
  1336. * overflow will be revealed later on using BIT_closeCStream() */
  1337. MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
  1338. {
  1339. size_t const nbBytes = bitC->bitPos >> 3;
  1340. assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
  1341. assert(bitC->ptr <= bitC->endPtr);
  1342. MEM_writeLEST(bitC->ptr, bitC->bitContainer);
  1343. bitC->ptr += nbBytes;
  1344. if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
  1345. bitC->bitPos &= 7;
  1346. bitC->bitContainer >>= nbBytes*8;
  1347. }
  1348. /*! BIT_closeCStream() :
  1349. * @return : size of CStream, in bytes,
  1350. * or 0 if it could not fit into dstBuffer */
  1351. MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
  1352. {
  1353. BIT_addBitsFast(bitC, 1, 1); /* endMark */
  1354. BIT_flushBits(bitC);
  1355. if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */
  1356. return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
  1357. }
  1358. /*-********************************************************
  1359. * bitStream decoding
  1360. **********************************************************/
  1361. /*! BIT_initDStream() :
  1362. * Initialize a BIT_DStream_t.
  1363. * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
  1364. * `srcSize` must be the *exact* size of the bitStream, in bytes.
  1365. * @return : size of stream (== srcSize), or an errorCode if a problem is detected
  1366. */
  1367. MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
  1368. {
  1369. if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
  1370. bitD->start = (const char*)srcBuffer;
  1371. bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
  1372. if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
  1373. bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
  1374. bitD->bitContainer = MEM_readLEST(bitD->ptr);
  1375. { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
  1376. bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
  1377. if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
  1378. } else {
  1379. bitD->ptr = bitD->start;
  1380. bitD->bitContainer = *(const BYTE*)(bitD->start);
  1381. switch(srcSize)
  1382. {
  1383. case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
  1384. /* fall-through */
  1385. case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
  1386. /* fall-through */
  1387. case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
  1388. /* fall-through */
  1389. case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
  1390. /* fall-through */
  1391. case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
  1392. /* fall-through */
  1393. case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
  1394. /* fall-through */
  1395. default: break;
  1396. }
  1397. { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
  1398. bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
  1399. if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
  1400. }
  1401. bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
  1402. }
  1403. return srcSize;
  1404. }
  1405. MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
  1406. {
  1407. return bitContainer >> start;
  1408. }
  1409. MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
  1410. {
  1411. U32 const regMask = sizeof(bitContainer)*8 - 1;
  1412. /* if start > regMask, bitstream is corrupted, and result is undefined */
  1413. assert(nbBits < BIT_MASK_SIZE);
  1414. return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
  1415. }
  1416. MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
  1417. {
  1418. assert(nbBits < BIT_MASK_SIZE);
  1419. return bitContainer & BIT_mask[nbBits];
  1420. }
  1421. /*! BIT_lookBits() :
  1422. * Provides next n bits from local register.
  1423. * local register is not modified.
  1424. * On 32-bits, maxNbBits==24.
  1425. * On 64-bits, maxNbBits==56.
  1426. * @return : value extracted */
  1427. MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
  1428. {
  1429. /* arbitrate between double-shift and shift+mask */
  1430. #if 1
  1431. /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8,
  1432. * bitstream is likely corrupted, and result is undefined */
  1433. return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
  1434. #else
  1435. /* this code path is slower on my os-x laptop */
  1436. U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
  1437. return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask);
  1438. #endif
  1439. }
  1440. /*! BIT_lookBitsFast() :
  1441. * unsafe version; only works if nbBits >= 1 */
  1442. MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
  1443. {
  1444. U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
  1445. assert(nbBits >= 1);
  1446. return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
  1447. }
  1448. MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
  1449. {
  1450. bitD->bitsConsumed += nbBits;
  1451. }
  1452. /*! BIT_readBits() :
  1453. * Read (consume) next n bits from local register and update.
  1454. * Pay attention to not read more than nbBits contained into local register.
  1455. * @return : extracted value. */
  1456. MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
  1457. {
  1458. size_t const value = BIT_lookBits(bitD, nbBits);
  1459. BIT_skipBits(bitD, nbBits);
  1460. return value;
  1461. }
  1462. /*! BIT_readBitsFast() :
  1463. * unsafe version; only works only if nbBits >= 1 */
  1464. MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
  1465. {
  1466. size_t const value = BIT_lookBitsFast(bitD, nbBits);
  1467. assert(nbBits >= 1);
  1468. BIT_skipBits(bitD, nbBits);
  1469. return value;
  1470. }
  1471. /*! BIT_reloadDStreamFast() :
  1472. * Similar to BIT_reloadDStream(), but with two differences:
  1473. * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
  1474. * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this
  1475. * point you must use BIT_reloadDStream() to reload.
  1476. */
  1477. MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
  1478. {
  1479. if (UNLIKELY(bitD->ptr < bitD->limitPtr))
  1480. return BIT_DStream_overflow;
  1481. assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
  1482. bitD->ptr -= bitD->bitsConsumed >> 3;
  1483. bitD->bitsConsumed &= 7;
  1484. bitD->bitContainer = MEM_readLEST(bitD->ptr);
  1485. return BIT_DStream_unfinished;
  1486. }
  1487. /*! BIT_reloadDStream() :
  1488. * Refill `bitD` from buffer previously set in BIT_initDStream() .
  1489. * This function is safe, it guarantees it will not read beyond src buffer.
  1490. * @return : status of `BIT_DStream_t` internal register.
  1491. * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
  1492. MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
  1493. {
  1494. if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
  1495. return BIT_DStream_overflow;
  1496. if (bitD->ptr >= bitD->limitPtr) {
  1497. return BIT_reloadDStreamFast(bitD);
  1498. }
  1499. if (bitD->ptr == bitD->start) {
  1500. if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
  1501. return BIT_DStream_completed;
  1502. }
  1503. /* start < ptr < limitPtr */
  1504. { U32 nbBytes = bitD->bitsConsumed >> 3;
  1505. BIT_DStream_status result = BIT_DStream_unfinished;
  1506. if (bitD->ptr - nbBytes < bitD->start) {
  1507. nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
  1508. result = BIT_DStream_endOfBuffer;
  1509. }
  1510. bitD->ptr -= nbBytes;
  1511. bitD->bitsConsumed -= nbBytes*8;
  1512. bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */
  1513. return result;
  1514. }
  1515. }
  1516. /*! BIT_endOfDStream() :
  1517. * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).
  1518. */
  1519. MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
  1520. {
  1521. return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
  1522. }
  1523. #if defined (__cplusplus)
  1524. }
  1525. #endif
  1526. #endif /* BITSTREAM_H_MODULE */
  1527. /**** ended inlining bitstream.h ****/
  1528. /* *****************************************
  1529. * Static allocation
  1530. *******************************************/
  1531. /* FSE buffer bounds */
  1532. #define FSE_NCOUNTBOUND 512
  1533. #define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
  1534. #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  1535. /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
  1536. #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
  1537. #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
  1538. /* or use the size to malloc() space directly. Pay attention to alignment restrictions though */
  1539. #define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))
  1540. #define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable))
  1541. /* *****************************************
  1542. * FSE advanced API
  1543. ***************************************** */
  1544. unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);
  1545. /**< same as FSE_optimalTableLog(), which used `minus==2` */
  1546. /* FSE_compress_wksp() :
  1547. * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
  1548. * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
  1549. */
  1550. #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
  1551. size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
  1552. size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);
  1553. /**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */
  1554. size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
  1555. /**< build a fake FSE_CTable, designed to compress always the same symbolValue */
  1556. /* FSE_buildCTable_wksp() :
  1557. * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
  1558. * `wkspSize` must be >= `(1<<tableLog)`.
  1559. */
  1560. size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
  1561. size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
  1562. /**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
  1563. size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
  1564. /**< build a fake FSE_DTable, designed to always generate the same symbolValue */
  1565. size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);
  1566. /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
  1567. typedef enum {
  1568. FSE_repeat_none, /**< Cannot use the previous table */
  1569. FSE_repeat_check, /**< Can use the previous table but it must be checked */
  1570. FSE_repeat_valid /**< Can use the previous table and it is assumed to be valid */
  1571. } FSE_repeat;
  1572. /* *****************************************
  1573. * FSE symbol compression API
  1574. *******************************************/
  1575. /*!
  1576. This API consists of small unitary functions, which highly benefit from being inlined.
  1577. Hence their body are included in next section.
  1578. */
  1579. typedef struct {
  1580. ptrdiff_t value;
  1581. const void* stateTable;
  1582. const void* symbolTT;
  1583. unsigned stateLog;
  1584. } FSE_CState_t;
  1585. static void FSE_initCState(FSE_CState_t* CStatePtr, const FSE_CTable* ct);
  1586. static void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* CStatePtr, unsigned symbol);
  1587. static void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* CStatePtr);
  1588. /**<
  1589. These functions are inner components of FSE_compress_usingCTable().
  1590. They allow the creation of custom streams, mixing multiple tables and bit sources.
  1591. A key property to keep in mind is that encoding and decoding are done **in reverse direction**.
  1592. So the first symbol you will encode is the last you will decode, like a LIFO stack.
  1593. You will need a few variables to track your CStream. They are :
  1594. FSE_CTable ct; // Provided by FSE_buildCTable()
  1595. BIT_CStream_t bitStream; // bitStream tracking structure
  1596. FSE_CState_t state; // State tracking structure (can have several)
  1597. The first thing to do is to init bitStream and state.
  1598. size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize);
  1599. FSE_initCState(&state, ct);
  1600. Note that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError();
  1601. You can then encode your input data, byte after byte.
  1602. FSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time.
  1603. Remember decoding will be done in reverse direction.
  1604. FSE_encodeByte(&bitStream, &state, symbol);
  1605. At any time, you can also add any bit sequence.
  1606. Note : maximum allowed nbBits is 25, for compatibility with 32-bits decoders
  1607. BIT_addBits(&bitStream, bitField, nbBits);
  1608. The above methods don't commit data to memory, they just store it into local register, for speed.
  1609. Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
  1610. Writing data to memory is a manual operation, performed by the flushBits function.
  1611. BIT_flushBits(&bitStream);
  1612. Your last FSE encoding operation shall be to flush your last state value(s).
  1613. FSE_flushState(&bitStream, &state);
  1614. Finally, you must close the bitStream.
  1615. The function returns the size of CStream in bytes.
  1616. If data couldn't fit into dstBuffer, it will return a 0 ( == not compressible)
  1617. If there is an error, it returns an errorCode (which can be tested using FSE_isError()).
  1618. size_t size = BIT_closeCStream(&bitStream);
  1619. */
  1620. /* *****************************************
  1621. * FSE symbol decompression API
  1622. *******************************************/
  1623. typedef struct {
  1624. size_t state;
  1625. const void* table; /* precise table may vary, depending on U16 */
  1626. } FSE_DState_t;
  1627. static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
  1628. static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
  1629. static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
  1630. /**<
  1631. Let's now decompose FSE_decompress_usingDTable() into its unitary components.
  1632. You will decode FSE-encoded symbols from the bitStream,
  1633. and also any other bitFields you put in, **in reverse order**.
  1634. You will need a few variables to track your bitStream. They are :
  1635. BIT_DStream_t DStream; // Stream context
  1636. FSE_DState_t DState; // State context. Multiple ones are possible
  1637. FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
  1638. The first thing to do is to init the bitStream.
  1639. errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
  1640. You should then retrieve your initial state(s)
  1641. (in reverse flushing order if you have several ones) :
  1642. errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
  1643. You can then decode your data, symbol after symbol.
  1644. For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
  1645. Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
  1646. unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
  1647. You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
  1648. Note : maximum allowed nbBits is 25, for 32-bits compatibility
  1649. size_t bitField = BIT_readBits(&DStream, nbBits);
  1650. All above operations only read from local register (which size depends on size_t).
  1651. Refueling the register from memory is manually performed by the reload method.
  1652. endSignal = FSE_reloadDStream(&DStream);
  1653. BIT_reloadDStream() result tells if there is still some more data to read from DStream.
  1654. BIT_DStream_unfinished : there is still some data left into the DStream.
  1655. BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
  1656. BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
  1657. BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
  1658. When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
  1659. to properly detect the exact end of stream.
  1660. After each decoded symbol, check if DStream is fully consumed using this simple test :
  1661. BIT_reloadDStream(&DStream) >= BIT_DStream_completed
  1662. When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
  1663. Checking if DStream has reached its end is performed by :
  1664. BIT_endOfDStream(&DStream);
  1665. Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
  1666. FSE_endOfDState(&DState);
  1667. */
  1668. /* *****************************************
  1669. * FSE unsafe API
  1670. *******************************************/
  1671. static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
  1672. /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
  1673. /* *****************************************
  1674. * Implementation of inlined functions
  1675. *******************************************/
  1676. typedef struct {
  1677. int deltaFindState;
  1678. U32 deltaNbBits;
  1679. } FSE_symbolCompressionTransform; /* total 8 bytes */
  1680. MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct)
  1681. {
  1682. const void* ptr = ct;
  1683. const U16* u16ptr = (const U16*) ptr;
  1684. const U32 tableLog = MEM_read16(ptr);
  1685. statePtr->value = (ptrdiff_t)1<<tableLog;
  1686. statePtr->stateTable = u16ptr+2;
  1687. statePtr->symbolTT = ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1);
  1688. statePtr->stateLog = tableLog;
  1689. }
  1690. /*! FSE_initCState2() :
  1691. * Same as FSE_initCState(), but the first symbol to include (which will be the last to be read)
  1692. * uses the smallest state value possible, saving the cost of this symbol */
  1693. MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol)
  1694. {
  1695. FSE_initCState(statePtr, ct);
  1696. { const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
  1697. const U16* stateTable = (const U16*)(statePtr->stateTable);
  1698. U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1<<15)) >> 16);
  1699. statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits;
  1700. statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
  1701. }
  1702. }
  1703. MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, unsigned symbol)
  1704. {
  1705. FSE_symbolCompressionTransform const symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
  1706. const U16* const stateTable = (const U16*)(statePtr->stateTable);
  1707. U32 const nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
  1708. BIT_addBits(bitC, statePtr->value, nbBitsOut);
  1709. statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
  1710. }
  1711. MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr)
  1712. {
  1713. BIT_addBits(bitC, statePtr->value, statePtr->stateLog);
  1714. BIT_flushBits(bitC);
  1715. }
  1716. /* FSE_getMaxNbBits() :
  1717. * Approximate maximum cost of a symbol, in bits.
  1718. * Fractional get rounded up (i.e : a symbol with a normalized frequency of 3 gives the same result as a frequency of 2)
  1719. * note 1 : assume symbolValue is valid (<= maxSymbolValue)
  1720. * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */
  1721. MEM_STATIC U32 FSE_getMaxNbBits(const void* symbolTTPtr, U32 symbolValue)
  1722. {
  1723. const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;
  1724. return (symbolTT[symbolValue].deltaNbBits + ((1<<16)-1)) >> 16;
  1725. }
  1726. /* FSE_bitCost() :
  1727. * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits)
  1728. * note 1 : assume symbolValue is valid (<= maxSymbolValue)
  1729. * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */
  1730. MEM_STATIC U32 FSE_bitCost(const void* symbolTTPtr, U32 tableLog, U32 symbolValue, U32 accuracyLog)
  1731. {
  1732. const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;
  1733. U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16;
  1734. U32 const threshold = (minNbBits+1) << 16;
  1735. assert(tableLog < 16);
  1736. assert(accuracyLog < 31-tableLog); /* ensure enough room for renormalization double shift */
  1737. { U32 const tableSize = 1 << tableLog;
  1738. U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize);
  1739. U32 const normalizedDeltaFromThreshold = (deltaFromThreshold << accuracyLog) >> tableLog; /* linear interpolation (very approximate) */
  1740. U32 const bitMultiplier = 1 << accuracyLog;
  1741. assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold);
  1742. assert(normalizedDeltaFromThreshold <= bitMultiplier);
  1743. return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold;
  1744. }
  1745. }
  1746. /* ====== Decompression ====== */
  1747. typedef struct {
  1748. U16 tableLog;
  1749. U16 fastMode;
  1750. } FSE_DTableHeader; /* sizeof U32 */
  1751. typedef struct
  1752. {
  1753. unsigned short newState;
  1754. unsigned char symbol;
  1755. unsigned char nbBits;
  1756. } FSE_decode_t; /* size == U32 */
  1757. MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
  1758. {
  1759. const void* ptr = dt;
  1760. const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr;
  1761. DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
  1762. BIT_reloadDStream(bitD);
  1763. DStatePtr->table = dt + 1;
  1764. }
  1765. MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr)
  1766. {
  1767. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  1768. return DInfo.symbol;
  1769. }
  1770. MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  1771. {
  1772. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  1773. U32 const nbBits = DInfo.nbBits;
  1774. size_t const lowBits = BIT_readBits(bitD, nbBits);
  1775. DStatePtr->state = DInfo.newState + lowBits;
  1776. }
  1777. MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  1778. {
  1779. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  1780. U32 const nbBits = DInfo.nbBits;
  1781. BYTE const symbol = DInfo.symbol;
  1782. size_t const lowBits = BIT_readBits(bitD, nbBits);
  1783. DStatePtr->state = DInfo.newState + lowBits;
  1784. return symbol;
  1785. }
  1786. /*! FSE_decodeSymbolFast() :
  1787. unsafe, only works if no symbol has a probability > 50% */
  1788. MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  1789. {
  1790. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  1791. U32 const nbBits = DInfo.nbBits;
  1792. BYTE const symbol = DInfo.symbol;
  1793. size_t const lowBits = BIT_readBitsFast(bitD, nbBits);
  1794. DStatePtr->state = DInfo.newState + lowBits;
  1795. return symbol;
  1796. }
  1797. MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
  1798. {
  1799. return DStatePtr->state == 0;
  1800. }
  1801. #ifndef FSE_COMMONDEFS_ONLY
  1802. /* **************************************************************
  1803. * Tuning parameters
  1804. ****************************************************************/
  1805. /*!MEMORY_USAGE :
  1806. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  1807. * Increasing memory usage improves compression ratio
  1808. * Reduced memory usage can improve speed, due to cache effect
  1809. * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
  1810. #ifndef FSE_MAX_MEMORY_USAGE
  1811. # define FSE_MAX_MEMORY_USAGE 14
  1812. #endif
  1813. #ifndef FSE_DEFAULT_MEMORY_USAGE
  1814. # define FSE_DEFAULT_MEMORY_USAGE 13
  1815. #endif
  1816. /*!FSE_MAX_SYMBOL_VALUE :
  1817. * Maximum symbol value authorized.
  1818. * Required for proper stack allocation */
  1819. #ifndef FSE_MAX_SYMBOL_VALUE
  1820. # define FSE_MAX_SYMBOL_VALUE 255
  1821. #endif
  1822. /* **************************************************************
  1823. * template functions type & suffix
  1824. ****************************************************************/
  1825. #define FSE_FUNCTION_TYPE BYTE
  1826. #define FSE_FUNCTION_EXTENSION
  1827. #define FSE_DECODE_TYPE FSE_decode_t
  1828. #endif /* !FSE_COMMONDEFS_ONLY */
  1829. /* ***************************************************************
  1830. * Constants
  1831. *****************************************************************/
  1832. #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
  1833. #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
  1834. #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
  1835. #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
  1836. #define FSE_MIN_TABLELOG 5
  1837. #define FSE_TABLELOG_ABSOLUTE_MAX 15
  1838. #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
  1839. # error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
  1840. #endif
  1841. #define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)
  1842. #endif /* FSE_STATIC_LINKING_ONLY */
  1843. #if defined (__cplusplus)
  1844. }
  1845. #endif
  1846. /**** ended inlining fse.h ****/
  1847. #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
  1848. /**** start inlining huf.h ****/
  1849. /* ******************************************************************
  1850. * huff0 huffman codec,
  1851. * part of Finite State Entropy library
  1852. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  1853. *
  1854. * You can contact the author at :
  1855. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  1856. *
  1857. * This source code is licensed under both the BSD-style license (found in the
  1858. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  1859. * in the COPYING file in the root directory of this source tree).
  1860. * You may select, at your option, one of the above-listed licenses.
  1861. ****************************************************************** */
  1862. #if defined (__cplusplus)
  1863. extern "C" {
  1864. #endif
  1865. #ifndef HUF_H_298734234
  1866. #define HUF_H_298734234
  1867. /* *** Dependencies *** */
  1868. #include <stddef.h> /* size_t */
  1869. /* *** library symbols visibility *** */
  1870. /* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,
  1871. * HUF symbols remain "private" (internal symbols for library only).
  1872. * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */
  1873. #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
  1874. # define HUF_PUBLIC_API __attribute__ ((visibility ("default")))
  1875. #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
  1876. # define HUF_PUBLIC_API __declspec(dllexport)
  1877. #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
  1878. # define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */
  1879. #else
  1880. # define HUF_PUBLIC_API
  1881. #endif
  1882. /* ========================== */
  1883. /* *** simple functions *** */
  1884. /* ========================== */
  1885. /** HUF_compress() :
  1886. * Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
  1887. * 'dst' buffer must be already allocated.
  1888. * Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
  1889. * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
  1890. * @return : size of compressed data (<= `dstCapacity`).
  1891. * Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
  1892. * if HUF_isError(return), compression failed (more details using HUF_getErrorName())
  1893. */
  1894. HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
  1895. const void* src, size_t srcSize);
  1896. /** HUF_decompress() :
  1897. * Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
  1898. * into already allocated buffer 'dst', of minimum size 'dstSize'.
  1899. * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
  1900. * Note : in contrast with FSE, HUF_decompress can regenerate
  1901. * RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
  1902. * because it knows size to regenerate (originalSize).
  1903. * @return : size of regenerated data (== originalSize),
  1904. * or an error code, which can be tested using HUF_isError()
  1905. */
  1906. HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,
  1907. const void* cSrc, size_t cSrcSize);
  1908. /* *** Tool functions *** */
  1909. #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
  1910. HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
  1911. /* Error Management */
  1912. HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
  1913. HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
  1914. /* *** Advanced function *** */
  1915. /** HUF_compress2() :
  1916. * Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
  1917. * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
  1918. * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
  1919. HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
  1920. const void* src, size_t srcSize,
  1921. unsigned maxSymbolValue, unsigned tableLog);
  1922. /** HUF_compress4X_wksp() :
  1923. * Same as HUF_compress2(), but uses externally allocated `workSpace`.
  1924. * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
  1925. #define HUF_WORKSPACE_SIZE ((6 << 10) + 256)
  1926. #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
  1927. HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
  1928. const void* src, size_t srcSize,
  1929. unsigned maxSymbolValue, unsigned tableLog,
  1930. void* workSpace, size_t wkspSize);
  1931. #endif /* HUF_H_298734234 */
  1932. /* ******************************************************************
  1933. * WARNING !!
  1934. * The following section contains advanced and experimental definitions
  1935. * which shall never be used in the context of a dynamic library,
  1936. * because they are not guaranteed to remain stable in the future.
  1937. * Only consider them in association with static linking.
  1938. * *****************************************************************/
  1939. #if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
  1940. #define HUF_H_HUF_STATIC_LINKING_ONLY
  1941. /* *** Dependencies *** */
  1942. /**** skipping file: mem.h ****/
  1943. /* *** Constants *** */
  1944. #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
  1945. #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
  1946. #define HUF_SYMBOLVALUE_MAX 255
  1947. #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
  1948. #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
  1949. # error "HUF_TABLELOG_MAX is too large !"
  1950. #endif
  1951. /* ****************************************
  1952. * Static allocation
  1953. ******************************************/
  1954. /* HUF buffer bounds */
  1955. #define HUF_CTABLEBOUND 129
  1956. #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */
  1957. #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  1958. /* static allocation of HUF's Compression Table */
  1959. #define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */
  1960. #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
  1961. #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
  1962. U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \
  1963. void* name##hv = &(name##hb); \
  1964. HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
  1965. /* static allocation of HUF's DTable */
  1966. typedef U32 HUF_DTable;
  1967. #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
  1968. #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
  1969. HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
  1970. #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
  1971. HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
  1972. /* ****************************************
  1973. * Advanced decompression functions
  1974. ******************************************/
  1975. size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  1976. #ifndef HUF_FORCE_DECOMPRESS_X1
  1977. size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  1978. #endif
  1979. size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
  1980. size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
  1981. size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */
  1982. size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  1983. size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
  1984. #ifndef HUF_FORCE_DECOMPRESS_X1
  1985. size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  1986. size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
  1987. #endif
  1988. /* ****************************************
  1989. * HUF detailed API
  1990. * ****************************************/
  1991. /*! HUF_compress() does the following:
  1992. * 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
  1993. * 2. (optional) refine tableLog using HUF_optimalTableLog()
  1994. * 3. build Huffman table from count using HUF_buildCTable()
  1995. * 4. save Huffman table to memory buffer using HUF_writeCTable()
  1996. * 5. encode the data stream using HUF_compress4X_usingCTable()
  1997. *
  1998. * The following API allows targeting specific sub-functions for advanced tasks.
  1999. * For example, it's possible to compress several blocks using the same 'CTable',
  2000. * or to save and regenerate 'CTable' using external methods.
  2001. */
  2002. unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
  2003. typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
  2004. size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
  2005. size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
  2006. size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  2007. size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
  2008. int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
  2009. typedef enum {
  2010. HUF_repeat_none, /**< Cannot use the previous table */
  2011. HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */
  2012. HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */
  2013. } HUF_repeat;
  2014. /** HUF_compress4X_repeat() :
  2015. * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  2016. * If it uses hufTable it does not modify hufTable or repeat.
  2017. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  2018. * If preferRepeat then the old table will always be used if valid. */
  2019. size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
  2020. const void* src, size_t srcSize,
  2021. unsigned maxSymbolValue, unsigned tableLog,
  2022. void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
  2023. HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
  2024. /** HUF_buildCTable_wksp() :
  2025. * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
  2026. * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
  2027. */
  2028. #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
  2029. #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
  2030. size_t HUF_buildCTable_wksp (HUF_CElt* tree,
  2031. const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
  2032. void* workSpace, size_t wkspSize);
  2033. /*! HUF_readStats() :
  2034. * Read compact Huffman tree, saved by HUF_writeCTable().
  2035. * `huffWeight` is destination buffer.
  2036. * @return : size read from `src` , or an error Code .
  2037. * Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
  2038. size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
  2039. U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
  2040. const void* src, size_t srcSize);
  2041. /** HUF_readCTable() :
  2042. * Loading a CTable saved with HUF_writeCTable() */
  2043. size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
  2044. /** HUF_getNbBits() :
  2045. * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
  2046. * Note 1 : is not inlined, as HUF_CElt definition is private
  2047. * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
  2048. U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
  2049. /*
  2050. * HUF_decompress() does the following:
  2051. * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
  2052. * 2. build Huffman table from save, using HUF_readDTableX?()
  2053. * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
  2054. */
  2055. /** HUF_selectDecoder() :
  2056. * Tells which decoder is likely to decode faster,
  2057. * based on a set of pre-computed metrics.
  2058. * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
  2059. * Assumption : 0 < dstSize <= 128 KB */
  2060. U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
  2061. /**
  2062. * The minimum workspace size for the `workSpace` used in
  2063. * HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().
  2064. *
  2065. * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
  2066. * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
  2067. * Buffer overflow errors may potentially occur if code modifications result in
  2068. * a required workspace size greater than that specified in the following
  2069. * macro.
  2070. */
  2071. #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
  2072. #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
  2073. #ifndef HUF_FORCE_DECOMPRESS_X2
  2074. size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);
  2075. size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
  2076. #endif
  2077. #ifndef HUF_FORCE_DECOMPRESS_X1
  2078. size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
  2079. size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
  2080. #endif
  2081. size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  2082. #ifndef HUF_FORCE_DECOMPRESS_X2
  2083. size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  2084. #endif
  2085. #ifndef HUF_FORCE_DECOMPRESS_X1
  2086. size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  2087. #endif
  2088. /* ====================== */
  2089. /* single stream variants */
  2090. /* ====================== */
  2091. size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
  2092. size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
  2093. size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
  2094. /** HUF_compress1X_repeat() :
  2095. * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  2096. * If it uses hufTable it does not modify hufTable or repeat.
  2097. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  2098. * If preferRepeat then the old table will always be used if valid. */
  2099. size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
  2100. const void* src, size_t srcSize,
  2101. unsigned maxSymbolValue, unsigned tableLog,
  2102. void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
  2103. HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
  2104. size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
  2105. #ifndef HUF_FORCE_DECOMPRESS_X1
  2106. size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
  2107. #endif
  2108. size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
  2109. size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);
  2110. #ifndef HUF_FORCE_DECOMPRESS_X2
  2111. size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
  2112. size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
  2113. #endif
  2114. #ifndef HUF_FORCE_DECOMPRESS_X1
  2115. size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
  2116. size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
  2117. #endif
  2118. size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */
  2119. #ifndef HUF_FORCE_DECOMPRESS_X2
  2120. size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  2121. #endif
  2122. #ifndef HUF_FORCE_DECOMPRESS_X1
  2123. size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
  2124. #endif
  2125. /* BMI2 variants.
  2126. * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
  2127. */
  2128. size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
  2129. #ifndef HUF_FORCE_DECOMPRESS_X2
  2130. size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
  2131. #endif
  2132. size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
  2133. size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
  2134. #endif /* HUF_STATIC_LINKING_ONLY */
  2135. #if defined (__cplusplus)
  2136. }
  2137. #endif
  2138. /**** ended inlining huf.h ****/
  2139. /*=== Version ===*/
  2140. unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
  2141. /*=== Error Management ===*/
  2142. unsigned FSE_isError(size_t code) { return ERR_isError(code); }
  2143. const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
  2144. unsigned HUF_isError(size_t code) { return ERR_isError(code); }
  2145. const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
  2146. /*-**************************************************************
  2147. * FSE NCount encoding-decoding
  2148. ****************************************************************/
  2149. size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
  2150. const void* headerBuffer, size_t hbSize)
  2151. {
  2152. const BYTE* const istart = (const BYTE*) headerBuffer;
  2153. const BYTE* const iend = istart + hbSize;
  2154. const BYTE* ip = istart;
  2155. int nbBits;
  2156. int remaining;
  2157. int threshold;
  2158. U32 bitStream;
  2159. int bitCount;
  2160. unsigned charnum = 0;
  2161. int previous0 = 0;
  2162. if (hbSize < 4) {
  2163. /* This function only works when hbSize >= 4 */
  2164. char buffer[4];
  2165. memset(buffer, 0, sizeof(buffer));
  2166. memcpy(buffer, headerBuffer, hbSize);
  2167. { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
  2168. buffer, sizeof(buffer));
  2169. if (FSE_isError(countSize)) return countSize;
  2170. if (countSize > hbSize) return ERROR(corruption_detected);
  2171. return countSize;
  2172. } }
  2173. assert(hbSize >= 4);
  2174. /* init */
  2175. memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
  2176. bitStream = MEM_readLE32(ip);
  2177. nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
  2178. if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
  2179. bitStream >>= 4;
  2180. bitCount = 4;
  2181. *tableLogPtr = nbBits;
  2182. remaining = (1<<nbBits)+1;
  2183. threshold = 1<<nbBits;
  2184. nbBits++;
  2185. while ((remaining>1) & (charnum<=*maxSVPtr)) {
  2186. if (previous0) {
  2187. unsigned n0 = charnum;
  2188. while ((bitStream & 0xFFFF) == 0xFFFF) {
  2189. n0 += 24;
  2190. if (ip < iend-5) {
  2191. ip += 2;
  2192. bitStream = MEM_readLE32(ip) >> bitCount;
  2193. } else {
  2194. bitStream >>= 16;
  2195. bitCount += 16;
  2196. } }
  2197. while ((bitStream & 3) == 3) {
  2198. n0 += 3;
  2199. bitStream >>= 2;
  2200. bitCount += 2;
  2201. }
  2202. n0 += bitStream & 3;
  2203. bitCount += 2;
  2204. if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
  2205. while (charnum < n0) normalizedCounter[charnum++] = 0;
  2206. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  2207. assert((bitCount >> 3) <= 3); /* For first condition to work */
  2208. ip += bitCount>>3;
  2209. bitCount &= 7;
  2210. bitStream = MEM_readLE32(ip) >> bitCount;
  2211. } else {
  2212. bitStream >>= 2;
  2213. } }
  2214. { int const max = (2*threshold-1) - remaining;
  2215. int count;
  2216. if ((bitStream & (threshold-1)) < (U32)max) {
  2217. count = bitStream & (threshold-1);
  2218. bitCount += nbBits-1;
  2219. } else {
  2220. count = bitStream & (2*threshold-1);
  2221. if (count >= threshold) count -= max;
  2222. bitCount += nbBits;
  2223. }
  2224. count--; /* extra accuracy */
  2225. remaining -= count < 0 ? -count : count; /* -1 means +1 */
  2226. normalizedCounter[charnum++] = (short)count;
  2227. previous0 = !count;
  2228. while (remaining < threshold) {
  2229. nbBits--;
  2230. threshold >>= 1;
  2231. }
  2232. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  2233. ip += bitCount>>3;
  2234. bitCount &= 7;
  2235. } else {
  2236. bitCount -= (int)(8 * (iend - 4 - ip));
  2237. ip = iend - 4;
  2238. }
  2239. bitStream = MEM_readLE32(ip) >> (bitCount & 31);
  2240. } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
  2241. if (remaining != 1) return ERROR(corruption_detected);
  2242. if (bitCount > 32) return ERROR(corruption_detected);
  2243. *maxSVPtr = charnum-1;
  2244. ip += (bitCount+7)>>3;
  2245. return ip-istart;
  2246. }
  2247. /*! HUF_readStats() :
  2248. Read compact Huffman tree, saved by HUF_writeCTable().
  2249. `huffWeight` is destination buffer.
  2250. `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
  2251. @return : size read from `src` , or an error Code .
  2252. Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
  2253. */
  2254. size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
  2255. U32* nbSymbolsPtr, U32* tableLogPtr,
  2256. const void* src, size_t srcSize)
  2257. {
  2258. U32 weightTotal;
  2259. const BYTE* ip = (const BYTE*) src;
  2260. size_t iSize;
  2261. size_t oSize;
  2262. if (!srcSize) return ERROR(srcSize_wrong);
  2263. iSize = ip[0];
  2264. /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
  2265. if (iSize >= 128) { /* special header */
  2266. oSize = iSize - 127;
  2267. iSize = ((oSize+1)/2);
  2268. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  2269. if (oSize >= hwSize) return ERROR(corruption_detected);
  2270. ip += 1;
  2271. { U32 n;
  2272. for (n=0; n<oSize; n+=2) {
  2273. huffWeight[n] = ip[n/2] >> 4;
  2274. huffWeight[n+1] = ip[n/2] & 15;
  2275. } } }
  2276. else { /* header compressed with FSE (normal case) */
  2277. FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
  2278. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  2279. oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
  2280. if (FSE_isError(oSize)) return oSize;
  2281. }
  2282. /* collect weight stats */
  2283. memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
  2284. weightTotal = 0;
  2285. { U32 n; for (n=0; n<oSize; n++) {
  2286. if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
  2287. rankStats[huffWeight[n]]++;
  2288. weightTotal += (1 << huffWeight[n]) >> 1;
  2289. } }
  2290. if (weightTotal == 0) return ERROR(corruption_detected);
  2291. /* get last non-null symbol weight (implied, total must be 2^n) */
  2292. { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
  2293. if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
  2294. *tableLogPtr = tableLog;
  2295. /* determine last weight */
  2296. { U32 const total = 1 << tableLog;
  2297. U32 const rest = total - weightTotal;
  2298. U32 const verif = 1 << BIT_highbit32(rest);
  2299. U32 const lastWeight = BIT_highbit32(rest) + 1;
  2300. if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
  2301. huffWeight[oSize] = (BYTE)lastWeight;
  2302. rankStats[lastWeight]++;
  2303. } }
  2304. /* check tree construction validity */
  2305. if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
  2306. /* results */
  2307. *nbSymbolsPtr = (U32)(oSize+1);
  2308. return iSize+1;
  2309. }
  2310. /**** ended inlining common/entropy_common.c ****/
  2311. /**** start inlining common/error_private.c ****/
  2312. /*
  2313. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  2314. * All rights reserved.
  2315. *
  2316. * This source code is licensed under both the BSD-style license (found in the
  2317. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  2318. * in the COPYING file in the root directory of this source tree).
  2319. * You may select, at your option, one of the above-listed licenses.
  2320. */
  2321. /* The purpose of this file is to have a single list of error strings embedded in binary */
  2322. /**** skipping file: error_private.h ****/
  2323. const char* ERR_getErrorString(ERR_enum code)
  2324. {
  2325. #ifdef ZSTD_STRIP_ERROR_STRINGS
  2326. (void)code;
  2327. return "Error strings stripped";
  2328. #else
  2329. static const char* const notErrorCode = "Unspecified error code";
  2330. switch( code )
  2331. {
  2332. case PREFIX(no_error): return "No error detected";
  2333. case PREFIX(GENERIC): return "Error (generic)";
  2334. case PREFIX(prefix_unknown): return "Unknown frame descriptor";
  2335. case PREFIX(version_unsupported): return "Version not supported";
  2336. case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
  2337. case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding";
  2338. case PREFIX(corruption_detected): return "Corrupted block detected";
  2339. case PREFIX(checksum_wrong): return "Restored data doesn't match checksum";
  2340. case PREFIX(parameter_unsupported): return "Unsupported parameter";
  2341. case PREFIX(parameter_outOfBound): return "Parameter is out of bound";
  2342. case PREFIX(init_missing): return "Context should be init first";
  2343. case PREFIX(memory_allocation): return "Allocation error : not enough memory";
  2344. case PREFIX(workSpace_tooSmall): return "workSpace buffer is not large enough";
  2345. case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
  2346. case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
  2347. case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
  2348. case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
  2349. case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
  2350. case PREFIX(dictionary_wrong): return "Dictionary mismatch";
  2351. case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples";
  2352. case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
  2353. case PREFIX(srcSize_wrong): return "Src size is incorrect";
  2354. case PREFIX(dstBuffer_null): return "Operation on NULL destination buffer";
  2355. /* following error codes are not stable and may be removed or changed in a future version */
  2356. case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
  2357. case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
  2358. case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
  2359. case PREFIX(maxCode):
  2360. default: return notErrorCode;
  2361. }
  2362. #endif
  2363. }
  2364. /**** ended inlining common/error_private.c ****/
  2365. /**** start inlining common/fse_decompress.c ****/
  2366. /* ******************************************************************
  2367. * FSE : Finite State Entropy decoder
  2368. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  2369. *
  2370. * You can contact the author at :
  2371. * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
  2372. * - Public forum : https://groups.google.com/forum/#!forum/lz4c
  2373. *
  2374. * This source code is licensed under both the BSD-style license (found in the
  2375. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  2376. * in the COPYING file in the root directory of this source tree).
  2377. * You may select, at your option, one of the above-listed licenses.
  2378. ****************************************************************** */
  2379. /* **************************************************************
  2380. * Includes
  2381. ****************************************************************/
  2382. /**** skipping file: bitstream.h ****/
  2383. /**** skipping file: compiler.h ****/
  2384. #define FSE_STATIC_LINKING_ONLY
  2385. /**** skipping file: fse.h ****/
  2386. /**** skipping file: error_private.h ****/
  2387. /* **************************************************************
  2388. * Error Management
  2389. ****************************************************************/
  2390. #define FSE_isError ERR_isError
  2391. #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
  2392. /* **************************************************************
  2393. * Templates
  2394. ****************************************************************/
  2395. /*
  2396. designed to be included
  2397. for type-specific functions (template emulation in C)
  2398. Objective is to write these functions only once, for improved maintenance
  2399. */
  2400. /* safety checks */
  2401. #ifndef FSE_FUNCTION_EXTENSION
  2402. # error "FSE_FUNCTION_EXTENSION must be defined"
  2403. #endif
  2404. #ifndef FSE_FUNCTION_TYPE
  2405. # error "FSE_FUNCTION_TYPE must be defined"
  2406. #endif
  2407. /* Function names */
  2408. #define FSE_CAT(X,Y) X##Y
  2409. #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
  2410. #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
  2411. /* Function templates */
  2412. FSE_DTable* FSE_createDTable (unsigned tableLog)
  2413. {
  2414. if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
  2415. return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );
  2416. }
  2417. void FSE_freeDTable (FSE_DTable* dt)
  2418. {
  2419. free(dt);
  2420. }
  2421. size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
  2422. {
  2423. void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
  2424. FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
  2425. U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
  2426. U32 const maxSV1 = maxSymbolValue + 1;
  2427. U32 const tableSize = 1 << tableLog;
  2428. U32 highThreshold = tableSize-1;
  2429. /* Sanity Checks */
  2430. if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
  2431. if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
  2432. /* Init, lay down lowprob symbols */
  2433. { FSE_DTableHeader DTableH;
  2434. DTableH.tableLog = (U16)tableLog;
  2435. DTableH.fastMode = 1;
  2436. { S16 const largeLimit= (S16)(1 << (tableLog-1));
  2437. U32 s;
  2438. for (s=0; s<maxSV1; s++) {
  2439. if (normalizedCounter[s]==-1) {
  2440. tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
  2441. symbolNext[s] = 1;
  2442. } else {
  2443. if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
  2444. symbolNext[s] = normalizedCounter[s];
  2445. } } }
  2446. memcpy(dt, &DTableH, sizeof(DTableH));
  2447. }
  2448. /* Spread symbols */
  2449. { U32 const tableMask = tableSize-1;
  2450. U32 const step = FSE_TABLESTEP(tableSize);
  2451. U32 s, position = 0;
  2452. for (s=0; s<maxSV1; s++) {
  2453. int i;
  2454. for (i=0; i<normalizedCounter[s]; i++) {
  2455. tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
  2456. position = (position + step) & tableMask;
  2457. while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
  2458. } }
  2459. if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  2460. }
  2461. /* Build Decoding table */
  2462. { U32 u;
  2463. for (u=0; u<tableSize; u++) {
  2464. FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
  2465. U32 const nextState = symbolNext[symbol]++;
  2466. tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
  2467. tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
  2468. } }
  2469. return 0;
  2470. }
  2471. #ifndef FSE_COMMONDEFS_ONLY
  2472. /*-*******************************************************
  2473. * Decompression (Byte symbols)
  2474. *********************************************************/
  2475. size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
  2476. {
  2477. void* ptr = dt;
  2478. FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
  2479. void* dPtr = dt + 1;
  2480. FSE_decode_t* const cell = (FSE_decode_t*)dPtr;
  2481. DTableH->tableLog = 0;
  2482. DTableH->fastMode = 0;
  2483. cell->newState = 0;
  2484. cell->symbol = symbolValue;
  2485. cell->nbBits = 0;
  2486. return 0;
  2487. }
  2488. size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
  2489. {
  2490. void* ptr = dt;
  2491. FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
  2492. void* dPtr = dt + 1;
  2493. FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
  2494. const unsigned tableSize = 1 << nbBits;
  2495. const unsigned tableMask = tableSize - 1;
  2496. const unsigned maxSV1 = tableMask+1;
  2497. unsigned s;
  2498. /* Sanity checks */
  2499. if (nbBits < 1) return ERROR(GENERIC); /* min size */
  2500. /* Build Decoding Table */
  2501. DTableH->tableLog = (U16)nbBits;
  2502. DTableH->fastMode = 1;
  2503. for (s=0; s<maxSV1; s++) {
  2504. dinfo[s].newState = 0;
  2505. dinfo[s].symbol = (BYTE)s;
  2506. dinfo[s].nbBits = (BYTE)nbBits;
  2507. }
  2508. return 0;
  2509. }
  2510. FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
  2511. void* dst, size_t maxDstSize,
  2512. const void* cSrc, size_t cSrcSize,
  2513. const FSE_DTable* dt, const unsigned fast)
  2514. {
  2515. BYTE* const ostart = (BYTE*) dst;
  2516. BYTE* op = ostart;
  2517. BYTE* const omax = op + maxDstSize;
  2518. BYTE* const olimit = omax-3;
  2519. BIT_DStream_t bitD;
  2520. FSE_DState_t state1;
  2521. FSE_DState_t state2;
  2522. /* Init */
  2523. CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
  2524. FSE_initDState(&state1, &bitD, dt);
  2525. FSE_initDState(&state2, &bitD, dt);
  2526. #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
  2527. /* 4 symbols per loop */
  2528. for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
  2529. op[0] = FSE_GETSYMBOL(&state1);
  2530. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  2531. BIT_reloadDStream(&bitD);
  2532. op[1] = FSE_GETSYMBOL(&state2);
  2533. if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  2534. { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
  2535. op[2] = FSE_GETSYMBOL(&state1);
  2536. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  2537. BIT_reloadDStream(&bitD);
  2538. op[3] = FSE_GETSYMBOL(&state2);
  2539. }
  2540. /* tail */
  2541. /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
  2542. while (1) {
  2543. if (op>(omax-2)) return ERROR(dstSize_tooSmall);
  2544. *op++ = FSE_GETSYMBOL(&state1);
  2545. if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
  2546. *op++ = FSE_GETSYMBOL(&state2);
  2547. break;
  2548. }
  2549. if (op>(omax-2)) return ERROR(dstSize_tooSmall);
  2550. *op++ = FSE_GETSYMBOL(&state2);
  2551. if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
  2552. *op++ = FSE_GETSYMBOL(&state1);
  2553. break;
  2554. } }
  2555. return op-ostart;
  2556. }
  2557. size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
  2558. const void* cSrc, size_t cSrcSize,
  2559. const FSE_DTable* dt)
  2560. {
  2561. const void* ptr = dt;
  2562. const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;
  2563. const U32 fastMode = DTableH->fastMode;
  2564. /* select fast mode (static) */
  2565. if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
  2566. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
  2567. }
  2568. size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)
  2569. {
  2570. const BYTE* const istart = (const BYTE*)cSrc;
  2571. const BYTE* ip = istart;
  2572. short counting[FSE_MAX_SYMBOL_VALUE+1];
  2573. unsigned tableLog;
  2574. unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
  2575. /* normal FSE decoding mode */
  2576. size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
  2577. if (FSE_isError(NCountLength)) return NCountLength;
  2578. /* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */ /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */
  2579. if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
  2580. ip += NCountLength;
  2581. cSrcSize -= NCountLength;
  2582. CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );
  2583. return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */
  2584. }
  2585. typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
  2586. size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)
  2587. {
  2588. DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
  2589. return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);
  2590. }
  2591. #endif /* FSE_COMMONDEFS_ONLY */
  2592. /**** ended inlining common/fse_decompress.c ****/
  2593. /**** start inlining common/zstd_common.c ****/
  2594. /*
  2595. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  2596. * All rights reserved.
  2597. *
  2598. * This source code is licensed under both the BSD-style license (found in the
  2599. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  2600. * in the COPYING file in the root directory of this source tree).
  2601. * You may select, at your option, one of the above-listed licenses.
  2602. */
  2603. /*-*************************************
  2604. * Dependencies
  2605. ***************************************/
  2606. /**** skipping file: error_private.h ****/
  2607. /**** start inlining zstd_internal.h ****/
  2608. /*
  2609. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  2610. * All rights reserved.
  2611. *
  2612. * This source code is licensed under both the BSD-style license (found in the
  2613. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  2614. * in the COPYING file in the root directory of this source tree).
  2615. * You may select, at your option, one of the above-listed licenses.
  2616. */
  2617. #ifndef ZSTD_CCOMMON_H_MODULE
  2618. #define ZSTD_CCOMMON_H_MODULE
  2619. /* this module contains definitions which must be identical
  2620. * across compression, decompression and dictBuilder.
  2621. * It also contains a few functions useful to at least 2 of them
  2622. * and which benefit from being inlined */
  2623. /*-*************************************
  2624. * Dependencies
  2625. ***************************************/
  2626. #ifdef __aarch64__
  2627. #include <arm_neon.h>
  2628. #endif
  2629. /**** skipping file: compiler.h ****/
  2630. /**** skipping file: mem.h ****/
  2631. /**** skipping file: debug.h ****/
  2632. /**** skipping file: error_private.h ****/
  2633. #define ZSTD_STATIC_LINKING_ONLY
  2634. /**** start inlining ../zstd.h ****/
  2635. /*
  2636. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  2637. * All rights reserved.
  2638. *
  2639. * This source code is licensed under both the BSD-style license (found in the
  2640. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  2641. * in the COPYING file in the root directory of this source tree).
  2642. * You may select, at your option, one of the above-listed licenses.
  2643. */
  2644. #if defined (__cplusplus)
  2645. extern "C" {
  2646. #endif
  2647. #ifndef ZSTD_H_235446
  2648. #define ZSTD_H_235446
  2649. /* ====== Dependency ======*/
  2650. #include <limits.h> /* INT_MAX */
  2651. #include <stddef.h> /* size_t */
  2652. /* ===== ZSTDLIB_API : control library symbols visibility ===== */
  2653. #ifndef ZSTDLIB_VISIBILITY
  2654. # if defined(__GNUC__) && (__GNUC__ >= 4)
  2655. # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default")))
  2656. # else
  2657. # define ZSTDLIB_VISIBILITY
  2658. # endif
  2659. #endif
  2660. #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
  2661. # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY
  2662. #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
  2663. # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  2664. #else
  2665. # define ZSTDLIB_API ZSTDLIB_VISIBILITY
  2666. #endif
  2667. /*******************************************************************************
  2668. Introduction
  2669. zstd, short for Zstandard, is a fast lossless compression algorithm, targeting
  2670. real-time compression scenarios at zlib-level and better compression ratios.
  2671. The zstd compression library provides in-memory compression and decompression
  2672. functions.
  2673. The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
  2674. which is currently 22. Levels >= 20, labeled `--ultra`, should be used with
  2675. caution, as they require more memory. The library also offers negative
  2676. compression levels, which extend the range of speed vs. ratio preferences.
  2677. The lower the level, the faster the speed (at the cost of compression).
  2678. Compression can be done in:
  2679. - a single step (described as Simple API)
  2680. - a single step, reusing a context (described as Explicit context)
  2681. - unbounded multiple steps (described as Streaming compression)
  2682. The compression ratio achievable on small data can be highly improved using
  2683. a dictionary. Dictionary compression can be performed in:
  2684. - a single step (described as Simple dictionary API)
  2685. - a single step, reusing a dictionary (described as Bulk-processing
  2686. dictionary API)
  2687. Advanced experimental functions can be accessed using
  2688. `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h.
  2689. Advanced experimental APIs should never be used with a dynamically-linked
  2690. library. They are not "stable"; their definitions or signatures may change in
  2691. the future. Only static linking is allowed.
  2692. *******************************************************************************/
  2693. /*------ Version ------*/
  2694. #define ZSTD_VERSION_MAJOR 1
  2695. #define ZSTD_VERSION_MINOR 4
  2696. #define ZSTD_VERSION_RELEASE 5
  2697. #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
  2698. ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */
  2699. #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
  2700. #define ZSTD_QUOTE(str) #str
  2701. #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
  2702. #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
  2703. ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */
  2704. /* *************************************
  2705. * Default constant
  2706. ***************************************/
  2707. #ifndef ZSTD_CLEVEL_DEFAULT
  2708. # define ZSTD_CLEVEL_DEFAULT 3
  2709. #endif
  2710. /* *************************************
  2711. * Constants
  2712. ***************************************/
  2713. /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */
  2714. #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */
  2715. #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */
  2716. #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
  2717. #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0
  2718. #define ZSTD_BLOCKSIZELOG_MAX 17
  2719. #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX)
  2720. /***************************************
  2721. * Simple API
  2722. ***************************************/
  2723. /*! ZSTD_compress() :
  2724. * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
  2725. * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
  2726. * @return : compressed size written into `dst` (<= `dstCapacity),
  2727. * or an error code if it fails (which can be tested using ZSTD_isError()). */
  2728. ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
  2729. const void* src, size_t srcSize,
  2730. int compressionLevel);
  2731. /*! ZSTD_decompress() :
  2732. * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
  2733. * `dstCapacity` is an upper bound of originalSize to regenerate.
  2734. * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
  2735. * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  2736. * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
  2737. ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
  2738. const void* src, size_t compressedSize);
  2739. /*! ZSTD_getFrameContentSize() : requires v1.3.0+
  2740. * `src` should point to the start of a ZSTD encoded frame.
  2741. * `srcSize` must be at least as large as the frame header.
  2742. * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.
  2743. * @return : - decompressed size of `src` frame content, if known
  2744. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  2745. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
  2746. * note 1 : a 0 return value means the frame is valid but "empty".
  2747. * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.
  2748. * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
  2749. * In which case, it's necessary to use streaming mode to decompress data.
  2750. * Optionally, application can rely on some implicit limit,
  2751. * as ZSTD_decompress() only needs an upper bound of decompressed size.
  2752. * (For example, data could be necessarily cut into blocks <= 16 KB).
  2753. * note 3 : decompressed size is always present when compression is completed using single-pass functions,
  2754. * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().
  2755. * note 4 : decompressed size can be very large (64-bits value),
  2756. * potentially larger than what local system can handle as a single memory segment.
  2757. * In which case, it's necessary to use streaming mode to decompress data.
  2758. * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.
  2759. * Always ensure return value fits within application's authorized limits.
  2760. * Each application can set its own limits.
  2761. * note 6 : This function replaces ZSTD_getDecompressedSize() */
  2762. #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
  2763. #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
  2764. ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
  2765. /*! ZSTD_getDecompressedSize() :
  2766. * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().
  2767. * Both functions work the same way, but ZSTD_getDecompressedSize() blends
  2768. * "empty", "unknown" and "error" results to the same return value (0),
  2769. * while ZSTD_getFrameContentSize() gives them separate return values.
  2770. * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
  2771. ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
  2772. /*! ZSTD_findFrameCompressedSize() :
  2773. * `src` should point to the start of a ZSTD frame or skippable frame.
  2774. * `srcSize` must be >= first frame size
  2775. * @return : the compressed size of the first frame starting at `src`,
  2776. * suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
  2777. * or an error code if input is invalid */
  2778. ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
  2779. /*====== Helper functions ======*/
  2780. #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */
  2781. ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
  2782. ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  2783. ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
  2784. ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
  2785. ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
  2786. /***************************************
  2787. * Explicit context
  2788. ***************************************/
  2789. /*= Compression context
  2790. * When compressing many times,
  2791. * it is recommended to allocate a context just once,
  2792. * and re-use it for each successive compression operation.
  2793. * This will make workload friendlier for system's memory.
  2794. * Note : re-using context is just a speed / resource optimization.
  2795. * It doesn't change the compression ratio, which remains identical.
  2796. * Note 2 : In multi-threaded environments,
  2797. * use one different context per thread for parallel execution.
  2798. */
  2799. typedef struct ZSTD_CCtx_s ZSTD_CCtx;
  2800. ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
  2801. ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
  2802. /*! ZSTD_compressCCtx() :
  2803. * Same as ZSTD_compress(), using an explicit ZSTD_CCtx.
  2804. * Important : in order to behave similarly to `ZSTD_compress()`,
  2805. * this function compresses at requested compression level,
  2806. * __ignoring any other parameter__ .
  2807. * If any advanced parameter was set using the advanced API,
  2808. * they will all be reset. Only `compressionLevel` remains.
  2809. */
  2810. ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
  2811. void* dst, size_t dstCapacity,
  2812. const void* src, size_t srcSize,
  2813. int compressionLevel);
  2814. /*= Decompression context
  2815. * When decompressing many times,
  2816. * it is recommended to allocate a context only once,
  2817. * and re-use it for each successive compression operation.
  2818. * This will make workload friendlier for system's memory.
  2819. * Use one context per thread for parallel execution. */
  2820. typedef struct ZSTD_DCtx_s ZSTD_DCtx;
  2821. ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
  2822. ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
  2823. /*! ZSTD_decompressDCtx() :
  2824. * Same as ZSTD_decompress(),
  2825. * requires an allocated ZSTD_DCtx.
  2826. * Compatible with sticky parameters.
  2827. */
  2828. ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
  2829. void* dst, size_t dstCapacity,
  2830. const void* src, size_t srcSize);
  2831. /***************************************
  2832. * Advanced compression API
  2833. ***************************************/
  2834. /* API design :
  2835. * Parameters are pushed one by one into an existing context,
  2836. * using ZSTD_CCtx_set*() functions.
  2837. * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.
  2838. * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !
  2839. * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ .
  2840. *
  2841. * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset().
  2842. *
  2843. * This API supercedes all other "advanced" API entry points in the experimental section.
  2844. * In the future, we expect to remove from experimental API entry points which are redundant with this API.
  2845. */
  2846. /* Compression strategies, listed from fastest to strongest */
  2847. typedef enum { ZSTD_fast=1,
  2848. ZSTD_dfast=2,
  2849. ZSTD_greedy=3,
  2850. ZSTD_lazy=4,
  2851. ZSTD_lazy2=5,
  2852. ZSTD_btlazy2=6,
  2853. ZSTD_btopt=7,
  2854. ZSTD_btultra=8,
  2855. ZSTD_btultra2=9
  2856. /* note : new strategies _might_ be added in the future.
  2857. Only the order (from fast to strong) is guaranteed */
  2858. } ZSTD_strategy;
  2859. typedef enum {
  2860. /* compression parameters
  2861. * Note: When compressing with a ZSTD_CDict these parameters are superseded
  2862. * by the parameters used to construct the ZSTD_CDict.
  2863. * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */
  2864. ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table.
  2865. * Note that exact compression parameters are dynamically determined,
  2866. * depending on both compression level and srcSize (when known).
  2867. * Default level is ZSTD_CLEVEL_DEFAULT==3.
  2868. * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
  2869. * Note 1 : it's possible to pass a negative compression level.
  2870. * Note 2 : setting a level does not automatically set all other compression parameters
  2871. * to default. Setting this will however eventually dynamically impact the compression
  2872. * parameters which have not been manually set. The manually set
  2873. * ones will 'stick'. */
  2874. /* Advanced compression parameters :
  2875. * It's possible to pin down compression parameters to some specific values.
  2876. * In which case, these values are no longer dynamically selected by the compressor */
  2877. ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2.
  2878. * This will set a memory budget for streaming decompression,
  2879. * with larger values requiring more memory
  2880. * and typically compressing more.
  2881. * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
  2882. * Special: value 0 means "use default windowLog".
  2883. * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT
  2884. * requires explicitly allowing such size at streaming decompression stage. */
  2885. ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2.
  2886. * Resulting memory usage is (1 << (hashLog+2)).
  2887. * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
  2888. * Larger tables improve compression ratio of strategies <= dFast,
  2889. * and improve speed of strategies > dFast.
  2890. * Special: value 0 means "use default hashLog". */
  2891. ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2.
  2892. * Resulting memory usage is (1 << (chainLog+2)).
  2893. * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
  2894. * Larger tables result in better and slower compression.
  2895. * This parameter is useless for "fast" strategy.
  2896. * It's still useful when using "dfast" strategy,
  2897. * in which case it defines a secondary probe table.
  2898. * Special: value 0 means "use default chainLog". */
  2899. ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2.
  2900. * More attempts result in better and slower compression.
  2901. * This parameter is useless for "fast" and "dFast" strategies.
  2902. * Special: value 0 means "use default searchLog". */
  2903. ZSTD_c_minMatch=105, /* Minimum size of searched matches.
  2904. * Note that Zstandard can still find matches of smaller size,
  2905. * it just tweaks its search algorithm to look for this size and larger.
  2906. * Larger values increase compression and decompression speed, but decrease ratio.
  2907. * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX.
  2908. * Note that currently, for all strategies < btopt, effective minimum is 4.
  2909. * , for all strategies > fast, effective maximum is 6.
  2910. * Special: value 0 means "use default minMatchLength". */
  2911. ZSTD_c_targetLength=106, /* Impact of this field depends on strategy.
  2912. * For strategies btopt, btultra & btultra2:
  2913. * Length of Match considered "good enough" to stop search.
  2914. * Larger values make compression stronger, and slower.
  2915. * For strategy fast:
  2916. * Distance between match sampling.
  2917. * Larger values make compression faster, and weaker.
  2918. * Special: value 0 means "use default targetLength". */
  2919. ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition.
  2920. * The higher the value of selected strategy, the more complex it is,
  2921. * resulting in stronger and slower compression.
  2922. * Special: value 0 means "use default strategy". */
  2923. /* LDM mode parameters */
  2924. ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.
  2925. * This parameter is designed to improve compression ratio
  2926. * for large inputs, by finding large matches at long distance.
  2927. * It increases memory usage and window size.
  2928. * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB
  2929. * except when expressly set to a different value. */
  2930. ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2.
  2931. * Larger values increase memory usage and compression ratio,
  2932. * but decrease compression speed.
  2933. * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
  2934. * default: windowlog - 7.
  2935. * Special: value 0 means "automatically determine hashlog". */
  2936. ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher.
  2937. * Larger/too small values usually decrease compression ratio.
  2938. * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
  2939. * Special: value 0 means "use default value" (default: 64). */
  2940. ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution.
  2941. * Larger values improve collision resolution but decrease compression speed.
  2942. * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX.
  2943. * Special: value 0 means "use default value" (default: 3). */
  2944. ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table.
  2945. * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
  2946. * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
  2947. * Larger values improve compression speed.
  2948. * Deviating far from default value will likely result in a compression ratio decrease.
  2949. * Special: value 0 means "automatically determine hashRateLog". */
  2950. /* frame parameters */
  2951. ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
  2952. * Content size must be known at the beginning of compression.
  2953. * This is automatically the case when using ZSTD_compress2(),
  2954. * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */
  2955. ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */
  2956. ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */
  2957. /* multi-threading parameters */
  2958. /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD).
  2959. * They return an error otherwise. */
  2960. ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
  2961. * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() :
  2962. * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller,
  2963. * while compression work is performed in parallel, within worker threads.
  2964. * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end :
  2965. * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call).
  2966. * More workers improve speed, but also increase memory usage.
  2967. * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
  2968. ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1.
  2969. * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads.
  2970. * 0 means default, which is dynamically determined based on compression parameters.
  2971. * Job size must be a minimum of overlap size, or 1 MB, whichever is largest.
  2972. * The minimum size is automatically and transparently enforced. */
  2973. ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size.
  2974. * The overlap size is an amount of data reloaded from previous job at the beginning of a new job.
  2975. * It helps preserve compression ratio, while each job is compressed in parallel.
  2976. * This value is enforced only when nbWorkers >= 1.
  2977. * Larger values increase compression ratio, but decrease speed.
  2978. * Possible values range from 0 to 9 :
  2979. * - 0 means "default" : value will be determined by the library, depending on strategy
  2980. * - 1 means "no overlap"
  2981. * - 9 means "full overlap", using a full window size.
  2982. * Each intermediate rank increases/decreases load size by a factor 2 :
  2983. * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default
  2984. * default value varies between 6 and 9, depending on strategy */
  2985. /* note : additional experimental parameters are also available
  2986. * within the experimental section of the API.
  2987. * At the time of this writing, they include :
  2988. * ZSTD_c_rsyncable
  2989. * ZSTD_c_format
  2990. * ZSTD_c_forceMaxWindow
  2991. * ZSTD_c_forceAttachDict
  2992. * ZSTD_c_literalCompressionMode
  2993. * ZSTD_c_targetCBlockSize
  2994. * ZSTD_c_srcSizeHint
  2995. * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
  2996. * note : never ever use experimentalParam? names directly;
  2997. * also, the enums values themselves are unstable and can still change.
  2998. */
  2999. ZSTD_c_experimentalParam1=500,
  3000. ZSTD_c_experimentalParam2=10,
  3001. ZSTD_c_experimentalParam3=1000,
  3002. ZSTD_c_experimentalParam4=1001,
  3003. ZSTD_c_experimentalParam5=1002,
  3004. ZSTD_c_experimentalParam6=1003,
  3005. ZSTD_c_experimentalParam7=1004
  3006. } ZSTD_cParameter;
  3007. typedef struct {
  3008. size_t error;
  3009. int lowerBound;
  3010. int upperBound;
  3011. } ZSTD_bounds;
  3012. /*! ZSTD_cParam_getBounds() :
  3013. * All parameters must belong to an interval with lower and upper bounds,
  3014. * otherwise they will either trigger an error or be automatically clamped.
  3015. * @return : a structure, ZSTD_bounds, which contains
  3016. * - an error status field, which must be tested using ZSTD_isError()
  3017. * - lower and upper bounds, both inclusive
  3018. */
  3019. ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam);
  3020. /*! ZSTD_CCtx_setParameter() :
  3021. * Set one compression parameter, selected by enum ZSTD_cParameter.
  3022. * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds().
  3023. * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
  3024. * Setting a parameter is generally only possible during frame initialization (before starting compression).
  3025. * Exception : when using multi-threading mode (nbWorkers >= 1),
  3026. * the following parameters can be updated _during_ compression (within same frame):
  3027. * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
  3028. * new parameters will be active for next job only (after a flush()).
  3029. * @return : an error code (which can be tested using ZSTD_isError()).
  3030. */
  3031. ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value);
  3032. /*! ZSTD_CCtx_setPledgedSrcSize() :
  3033. * Total input data size to be compressed as a single frame.
  3034. * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag.
  3035. * This value will also be controlled at end of frame, and trigger an error if not respected.
  3036. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3037. * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame.
  3038. * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
  3039. * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame.
  3040. * Note 2 : pledgedSrcSize is only valid once, for the next frame.
  3041. * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
  3042. * Note 3 : Whenever all input data is provided and consumed in a single round,
  3043. * for example with ZSTD_compress2(),
  3044. * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
  3045. * this value is automatically overridden by srcSize instead.
  3046. */
  3047. ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
  3048. typedef enum {
  3049. ZSTD_reset_session_only = 1,
  3050. ZSTD_reset_parameters = 2,
  3051. ZSTD_reset_session_and_parameters = 3
  3052. } ZSTD_ResetDirective;
  3053. /*! ZSTD_CCtx_reset() :
  3054. * There are 2 different things that can be reset, independently or jointly :
  3055. * - The session : will stop compressing current frame, and make CCtx ready to start a new one.
  3056. * Useful after an error, or to interrupt any ongoing compression.
  3057. * Any internal data not yet flushed is cancelled.
  3058. * Compression parameters and dictionary remain unchanged.
  3059. * They will be used to compress next frame.
  3060. * Resetting session never fails.
  3061. * - The parameters : changes all parameters back to "default".
  3062. * This removes any reference to any dictionary too.
  3063. * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
  3064. * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
  3065. * - Both : similar to resetting the session, followed by resetting parameters.
  3066. */
  3067. ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
  3068. /*! ZSTD_compress2() :
  3069. * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
  3070. * ZSTD_compress2() always starts a new frame.
  3071. * Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
  3072. * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
  3073. * - The function is always blocking, returns when compression is completed.
  3074. * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
  3075. * @return : compressed size written into `dst` (<= `dstCapacity),
  3076. * or an error code if it fails (which can be tested using ZSTD_isError()).
  3077. */
  3078. ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
  3079. void* dst, size_t dstCapacity,
  3080. const void* src, size_t srcSize);
  3081. /***************************************
  3082. * Advanced decompression API
  3083. ***************************************/
  3084. /* The advanced API pushes parameters one by one into an existing DCtx context.
  3085. * Parameters are sticky, and remain valid for all following frames
  3086. * using the same DCtx context.
  3087. * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
  3088. * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
  3089. * Therefore, no new decompression function is necessary.
  3090. */
  3091. typedef enum {
  3092. ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
  3093. * the streaming API will refuse to allocate memory buffer
  3094. * in order to protect the host from unreasonable memory requirements.
  3095. * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
  3096. * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT).
  3097. * Special: value 0 means "use default maximum windowLog". */
  3098. /* note : additional experimental parameters are also available
  3099. * within the experimental section of the API.
  3100. * At the time of this writing, they include :
  3101. * ZSTD_d_format
  3102. * ZSTD_d_stableOutBuffer
  3103. * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
  3104. * note : never ever use experimentalParam? names directly
  3105. */
  3106. ZSTD_d_experimentalParam1=1000,
  3107. ZSTD_d_experimentalParam2=1001
  3108. } ZSTD_dParameter;
  3109. /*! ZSTD_dParam_getBounds() :
  3110. * All parameters must belong to an interval with lower and upper bounds,
  3111. * otherwise they will either trigger an error or be automatically clamped.
  3112. * @return : a structure, ZSTD_bounds, which contains
  3113. * - an error status field, which must be tested using ZSTD_isError()
  3114. * - both lower and upper bounds, inclusive
  3115. */
  3116. ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
  3117. /*! ZSTD_DCtx_setParameter() :
  3118. * Set one compression parameter, selected by enum ZSTD_dParameter.
  3119. * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
  3120. * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
  3121. * Setting a parameter is only possible during frame initialization (before starting decompression).
  3122. * @return : 0, or an error code (which can be tested using ZSTD_isError()).
  3123. */
  3124. ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
  3125. /*! ZSTD_DCtx_reset() :
  3126. * Return a DCtx to clean state.
  3127. * Session and parameters can be reset jointly or separately.
  3128. * Parameters can only be reset when no active frame is being decompressed.
  3129. * @return : 0, or an error code, which can be tested with ZSTD_isError()
  3130. */
  3131. ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
  3132. /****************************
  3133. * Streaming
  3134. ****************************/
  3135. typedef struct ZSTD_inBuffer_s {
  3136. const void* src; /**< start of input buffer */
  3137. size_t size; /**< size of input buffer */
  3138. size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
  3139. } ZSTD_inBuffer;
  3140. typedef struct ZSTD_outBuffer_s {
  3141. void* dst; /**< start of output buffer */
  3142. size_t size; /**< size of output buffer */
  3143. size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
  3144. } ZSTD_outBuffer;
  3145. /*-***********************************************************************
  3146. * Streaming compression - HowTo
  3147. *
  3148. * A ZSTD_CStream object is required to track streaming operation.
  3149. * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
  3150. * ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
  3151. * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.
  3152. *
  3153. * For parallel execution, use one separate ZSTD_CStream per thread.
  3154. *
  3155. * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
  3156. *
  3157. * Parameters are sticky : when starting a new compression on the same context,
  3158. * it will re-use the same sticky parameters as previous compression session.
  3159. * When in doubt, it's recommended to fully initialize the context before usage.
  3160. * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(),
  3161. * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to
  3162. * set more specific parameters, the pledged source size, or load a dictionary.
  3163. *
  3164. * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to
  3165. * consume input stream. The function will automatically update both `pos`
  3166. * fields within `input` and `output`.
  3167. * Note that the function may not consume the entire input, for example, because
  3168. * the output buffer is already full, in which case `input.pos < input.size`.
  3169. * The caller must check if input has been entirely consumed.
  3170. * If not, the caller must make some room to receive more compressed data,
  3171. * and then present again remaining input data.
  3172. * note: ZSTD_e_continue is guaranteed to make some forward progress when called,
  3173. * but doesn't guarantee maximal forward progress. This is especially relevant
  3174. * when compressing with multiple threads. The call won't block if it can
  3175. * consume some input, but if it can't it will wait for some, but not all,
  3176. * output to be flushed.
  3177. * @return : provides a minimum amount of data remaining to be flushed from internal buffers
  3178. * or an error code, which can be tested using ZSTD_isError().
  3179. *
  3180. * At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
  3181. * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated.
  3182. * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0).
  3183. * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush.
  3184. * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the
  3185. * operation.
  3186. * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will
  3187. * block until the flush is complete or the output buffer is full.
  3188. * @return : 0 if internal buffers are entirely flushed,
  3189. * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
  3190. * or an error code, which can be tested using ZSTD_isError().
  3191. *
  3192. * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame.
  3193. * It will perform a flush and write frame epilogue.
  3194. * The epilogue is required for decoders to consider a frame completed.
  3195. * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush.
  3196. * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to
  3197. * start a new frame.
  3198. * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will
  3199. * block until the flush is complete or the output buffer is full.
  3200. * @return : 0 if frame fully completed and fully flushed,
  3201. * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
  3202. * or an error code, which can be tested using ZSTD_isError().
  3203. *
  3204. * *******************************************************************/
  3205. typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
  3206. /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
  3207. /*===== ZSTD_CStream management functions =====*/
  3208. ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
  3209. ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
  3210. /*===== Streaming compression functions =====*/
  3211. typedef enum {
  3212. ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
  3213. ZSTD_e_flush=1, /* flush any data provided so far,
  3214. * it creates (at least) one new block, that can be decoded immediately on reception;
  3215. * frame will continue: any future data can still reference previously compressed data, improving compression.
  3216. * note : multithreaded compression will block to flush as much output as possible. */
  3217. ZSTD_e_end=2 /* flush any remaining data _and_ close current frame.
  3218. * note that frame is only closed after compressed data is fully flushed (return value == 0).
  3219. * After that point, any additional data starts a new frame.
  3220. * note : each frame is independent (does not reference any content from previous frame).
  3221. : note : multithreaded compression will block to flush as much output as possible. */
  3222. } ZSTD_EndDirective;
  3223. /*! ZSTD_compressStream2() :
  3224. * Behaves about the same as ZSTD_compressStream, with additional control on end directive.
  3225. * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
  3226. * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
  3227. * - output->pos must be <= dstCapacity, input->pos must be <= srcSize
  3228. * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
  3229. * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
  3230. * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
  3231. * and then immediately returns, just indicating that there is some data remaining to be flushed.
  3232. * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
  3233. * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.
  3234. * - @return provides a minimum amount of data remaining to be flushed from internal buffers
  3235. * or an error code, which can be tested using ZSTD_isError().
  3236. * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
  3237. * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
  3238. * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
  3239. * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
  3240. * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
  3241. * Before starting a new compression job, or changing compression parameters,
  3242. * it is required to fully flush internal buffers.
  3243. */
  3244. ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
  3245. ZSTD_outBuffer* output,
  3246. ZSTD_inBuffer* input,
  3247. ZSTD_EndDirective endOp);
  3248. /* These buffer sizes are softly recommended.
  3249. * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output.
  3250. * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(),
  3251. * reducing the amount of memory shuffling and buffering, resulting in minor performance savings.
  3252. *
  3253. * However, note that these recommendations are from the perspective of a C caller program.
  3254. * If the streaming interface is invoked from some other language,
  3255. * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo,
  3256. * a major performance rule is to reduce crossing such interface to an absolute minimum.
  3257. * It's not rare that performance ends being spent more into the interface, rather than compression itself.
  3258. * In which cases, prefer using large buffers, as large as practical,
  3259. * for both input and output, to reduce the nb of roundtrips.
  3260. */
  3261. ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
  3262. ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */
  3263. /* *****************************************************************************
  3264. * This following is a legacy streaming API.
  3265. * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2().
  3266. * It is redundant, but remains fully supported.
  3267. * Advanced parameters and dictionary compression can only be used through the
  3268. * new API.
  3269. ******************************************************************************/
  3270. /*!
  3271. * Equivalent to:
  3272. *
  3273. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  3274. * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
  3275. * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
  3276. */
  3277. ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
  3278. /*!
  3279. * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue).
  3280. * NOTE: The return value is different. ZSTD_compressStream() returns a hint for
  3281. * the next read size (if non-zero and not an error). ZSTD_compressStream2()
  3282. * returns the minimum nb of bytes left to flush (if non-zero and not an error).
  3283. */
  3284. ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
  3285. /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */
  3286. ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
  3287. /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */
  3288. ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
  3289. /*-***************************************************************************
  3290. * Streaming decompression - HowTo
  3291. *
  3292. * A ZSTD_DStream object is required to track streaming operations.
  3293. * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
  3294. * ZSTD_DStream objects can be re-used multiple times.
  3295. *
  3296. * Use ZSTD_initDStream() to start a new decompression operation.
  3297. * @return : recommended first input size
  3298. * Alternatively, use advanced API to set specific properties.
  3299. *
  3300. * Use ZSTD_decompressStream() repetitively to consume your input.
  3301. * The function will update both `pos` fields.
  3302. * If `input.pos < input.size`, some input has not been consumed.
  3303. * It's up to the caller to present again remaining data.
  3304. * The function tries to flush all data decoded immediately, respecting output buffer size.
  3305. * If `output.pos < output.size`, decoder has flushed everything it could.
  3306. * But if `output.pos == output.size`, there might be some data left within internal buffers.,
  3307. * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
  3308. * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
  3309. * @return : 0 when a frame is completely decoded and fully flushed,
  3310. * or an error code, which can be tested using ZSTD_isError(),
  3311. * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
  3312. * the return value is a suggested next input size (just a hint for better latency)
  3313. * that will never request more than the remaining frame size.
  3314. * *******************************************************************************/
  3315. typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
  3316. /* For compatibility with versions <= v1.2.0, prefer differentiating them. */
  3317. /*===== ZSTD_DStream management functions =====*/
  3318. ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
  3319. ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
  3320. /*===== Streaming decompression functions =====*/
  3321. /* This function is redundant with the advanced API and equivalent to:
  3322. *
  3323. * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
  3324. * ZSTD_DCtx_refDDict(zds, NULL);
  3325. */
  3326. ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
  3327. ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
  3328. ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
  3329. ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
  3330. /**************************
  3331. * Simple dictionary API
  3332. ***************************/
  3333. /*! ZSTD_compress_usingDict() :
  3334. * Compression at an explicit compression level using a Dictionary.
  3335. * A dictionary can be any arbitrary data segment (also called a prefix),
  3336. * or a buffer with specified information (see dictBuilder/zdict.h).
  3337. * Note : This function loads the dictionary, resulting in significant startup delay.
  3338. * It's intended for a dictionary used only once.
  3339. * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
  3340. ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
  3341. void* dst, size_t dstCapacity,
  3342. const void* src, size_t srcSize,
  3343. const void* dict,size_t dictSize,
  3344. int compressionLevel);
  3345. /*! ZSTD_decompress_usingDict() :
  3346. * Decompression using a known Dictionary.
  3347. * Dictionary must be identical to the one used during compression.
  3348. * Note : This function loads the dictionary, resulting in significant startup delay.
  3349. * It's intended for a dictionary used only once.
  3350. * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
  3351. ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  3352. void* dst, size_t dstCapacity,
  3353. const void* src, size_t srcSize,
  3354. const void* dict,size_t dictSize);
  3355. /***********************************
  3356. * Bulk processing dictionary API
  3357. **********************************/
  3358. typedef struct ZSTD_CDict_s ZSTD_CDict;
  3359. /*! ZSTD_createCDict() :
  3360. * When compressing multiple messages or blocks using the same dictionary,
  3361. * it's recommended to digest the dictionary only once, since it's a costly operation.
  3362. * ZSTD_createCDict() will create a state from digesting a dictionary.
  3363. * The resulting state can be used for future compression operations with very limited startup cost.
  3364. * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
  3365. * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict.
  3366. * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content.
  3367. * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer,
  3368. * in which case the only thing that it transports is the @compressionLevel.
  3369. * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively,
  3370. * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */
  3371. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
  3372. int compressionLevel);
  3373. /*! ZSTD_freeCDict() :
  3374. * Function frees memory allocated by ZSTD_createCDict(). */
  3375. ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
  3376. /*! ZSTD_compress_usingCDict() :
  3377. * Compression using a digested Dictionary.
  3378. * Recommended when same dictionary is used multiple times.
  3379. * Note : compression level is _decided at dictionary creation time_,
  3380. * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
  3381. ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
  3382. void* dst, size_t dstCapacity,
  3383. const void* src, size_t srcSize,
  3384. const ZSTD_CDict* cdict);
  3385. typedef struct ZSTD_DDict_s ZSTD_DDict;
  3386. /*! ZSTD_createDDict() :
  3387. * Create a digested dictionary, ready to start decompression operation without startup delay.
  3388. * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
  3389. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
  3390. /*! ZSTD_freeDDict() :
  3391. * Function frees memory allocated with ZSTD_createDDict() */
  3392. ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
  3393. /*! ZSTD_decompress_usingDDict() :
  3394. * Decompression using a digested Dictionary.
  3395. * Recommended when same dictionary is used multiple times. */
  3396. ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  3397. void* dst, size_t dstCapacity,
  3398. const void* src, size_t srcSize,
  3399. const ZSTD_DDict* ddict);
  3400. /********************************
  3401. * Dictionary helper functions
  3402. *******************************/
  3403. /*! ZSTD_getDictID_fromDict() :
  3404. * Provides the dictID stored within dictionary.
  3405. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  3406. * It can still be loaded, but as a content-only dictionary. */
  3407. ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
  3408. /*! ZSTD_getDictID_fromDDict() :
  3409. * Provides the dictID of the dictionary loaded into `ddict`.
  3410. * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
  3411. * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
  3412. ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
  3413. /*! ZSTD_getDictID_fromFrame() :
  3414. * Provides the dictID required to decompressed the frame stored within `src`.
  3415. * If @return == 0, the dictID could not be decoded.
  3416. * This could for one of the following reasons :
  3417. * - The frame does not require a dictionary to be decoded (most common case).
  3418. * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
  3419. * Note : this use case also happens when using a non-conformant dictionary.
  3420. * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
  3421. * - This is not a Zstandard frame.
  3422. * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
  3423. ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
  3424. /*******************************************************************************
  3425. * Advanced dictionary and prefix API
  3426. *
  3427. * This API allows dictionaries to be used with ZSTD_compress2(),
  3428. * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and
  3429. * only reset with the context is reset with ZSTD_reset_parameters or
  3430. * ZSTD_reset_session_and_parameters. Prefixes are single-use.
  3431. ******************************************************************************/
  3432. /*! ZSTD_CCtx_loadDictionary() :
  3433. * Create an internal CDict from `dict` buffer.
  3434. * Decompression will have to use same dictionary.
  3435. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3436. * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary,
  3437. * meaning "return to no-dictionary mode".
  3438. * Note 1 : Dictionary is sticky, it will be used for all future compressed frames.
  3439. * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters).
  3440. * Note 2 : Loading a dictionary involves building tables.
  3441. * It's also a CPU consuming operation, with non-negligible impact on latency.
  3442. * Tables are dependent on compression parameters, and for this reason,
  3443. * compression parameters can no longer be changed after loading a dictionary.
  3444. * Note 3 :`dict` content will be copied internally.
  3445. * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead.
  3446. * In such a case, dictionary buffer must outlive its users.
  3447. * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
  3448. * to precisely select how dictionary content must be interpreted. */
  3449. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
  3450. /*! ZSTD_CCtx_refCDict() :
  3451. * Reference a prepared dictionary, to be used for all next compressed frames.
  3452. * Note that compression parameters are enforced from within CDict,
  3453. * and supersede any compression parameter previously set within CCtx.
  3454. * The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs.
  3455. * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode.
  3456. * The dictionary will remain valid for future compressed frames using same CCtx.
  3457. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3458. * Special : Referencing a NULL CDict means "return to no-dictionary mode".
  3459. * Note 1 : Currently, only one dictionary can be managed.
  3460. * Referencing a new dictionary effectively "discards" any previous one.
  3461. * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */
  3462. ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
  3463. /*! ZSTD_CCtx_refPrefix() :
  3464. * Reference a prefix (single-usage dictionary) for next compressed frame.
  3465. * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end).
  3466. * Decompression will need same prefix to properly regenerate data.
  3467. * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
  3468. * but performs much faster, especially during decompression (compression speed is tunable with compression level).
  3469. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3470. * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
  3471. * Note 1 : Prefix buffer is referenced. It **must** outlive compression.
  3472. * Its content must remain unmodified during compression.
  3473. * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
  3474. * ensure that the window size is large enough to contain the entire source.
  3475. * See ZSTD_c_windowLog.
  3476. * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
  3477. * It's a CPU consuming operation, with non-negligible impact on latency.
  3478. * If there is a need to use the same prefix multiple times, consider loadDictionary instead.
  3479. * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent).
  3480. * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
  3481. ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
  3482. const void* prefix, size_t prefixSize);
  3483. /*! ZSTD_DCtx_loadDictionary() :
  3484. * Create an internal DDict from dict buffer,
  3485. * to be used to decompress next frames.
  3486. * The dictionary remains valid for all future frames, until explicitly invalidated.
  3487. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3488. * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
  3489. * meaning "return to no-dictionary mode".
  3490. * Note 1 : Loading a dictionary involves building tables,
  3491. * which has a non-negligible impact on CPU usage and latency.
  3492. * It's recommended to "load once, use many times", to amortize the cost
  3493. * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading.
  3494. * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead.
  3495. * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of
  3496. * how dictionary content is loaded and interpreted.
  3497. */
  3498. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  3499. /*! ZSTD_DCtx_refDDict() :
  3500. * Reference a prepared dictionary, to be used to decompress next frames.
  3501. * The dictionary remains active for decompression of future frames using same DCtx.
  3502. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3503. * Note 1 : Currently, only one dictionary can be managed.
  3504. * Referencing a new dictionary effectively "discards" any previous one.
  3505. * Special: referencing a NULL DDict means "return to no-dictionary mode".
  3506. * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
  3507. */
  3508. ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
  3509. /*! ZSTD_DCtx_refPrefix() :
  3510. * Reference a prefix (single-usage dictionary) to decompress next frame.
  3511. * This is the reverse operation of ZSTD_CCtx_refPrefix(),
  3512. * and must use the same prefix as the one used during compression.
  3513. * Prefix is **only used once**. Reference is discarded at end of frame.
  3514. * End of frame is reached when ZSTD_decompressStream() returns 0.
  3515. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  3516. * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
  3517. * Note 2 : Prefix buffer is referenced. It **must** outlive decompression.
  3518. * Prefix buffer must remain unmodified up to the end of frame,
  3519. * reached when ZSTD_decompressStream() returns 0.
  3520. * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent).
  3521. * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section)
  3522. * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
  3523. * A full dictionary is more costly, as it requires building tables.
  3524. */
  3525. ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
  3526. const void* prefix, size_t prefixSize);
  3527. /* === Memory management === */
  3528. /*! ZSTD_sizeof_*() :
  3529. * These functions give the _current_ memory usage of selected object.
  3530. * Note that object memory usage can evolve (increase or decrease) over time. */
  3531. ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
  3532. ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
  3533. ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
  3534. ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
  3535. ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
  3536. ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
  3537. #endif /* ZSTD_H_235446 */
  3538. /* **************************************************************************************
  3539. * ADVANCED AND EXPERIMENTAL FUNCTIONS
  3540. ****************************************************************************************
  3541. * The definitions in the following section are considered experimental.
  3542. * They are provided for advanced scenarios.
  3543. * They should never be used with a dynamic library, as prototypes may change in the future.
  3544. * Use them only in association with static linking.
  3545. * ***************************************************************************************/
  3546. #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
  3547. #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
  3548. /****************************************************************************************
  3549. * experimental API (static linking only)
  3550. ****************************************************************************************
  3551. * The following symbols and constants
  3552. * are not planned to join "stable API" status in the near future.
  3553. * They can still change in future versions.
  3554. * Some of them are planned to remain in the static_only section indefinitely.
  3555. * Some of them might be removed in the future (especially when redundant with existing stable functions)
  3556. * ***************************************************************************************/
  3557. #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */
  3558. #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2)
  3559. #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */
  3560. #define ZSTD_SKIPPABLEHEADERSIZE 8
  3561. /* compression parameter bounds */
  3562. #define ZSTD_WINDOWLOG_MAX_32 30
  3563. #define ZSTD_WINDOWLOG_MAX_64 31
  3564. #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
  3565. #define ZSTD_WINDOWLOG_MIN 10
  3566. #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
  3567. #define ZSTD_HASHLOG_MIN 6
  3568. #define ZSTD_CHAINLOG_MAX_32 29
  3569. #define ZSTD_CHAINLOG_MAX_64 30
  3570. #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
  3571. #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
  3572. #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
  3573. #define ZSTD_SEARCHLOG_MIN 1
  3574. #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
  3575. #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */
  3576. #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
  3577. #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
  3578. #define ZSTD_STRATEGY_MIN ZSTD_fast
  3579. #define ZSTD_STRATEGY_MAX ZSTD_btultra2
  3580. #define ZSTD_OVERLAPLOG_MIN 0
  3581. #define ZSTD_OVERLAPLOG_MAX 9
  3582. #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame
  3583. * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
  3584. * to preserve host's memory from unreasonable requirements.
  3585. * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
  3586. * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
  3587. /* LDM parameter bounds */
  3588. #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN
  3589. #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX
  3590. #define ZSTD_LDM_MINMATCH_MIN 4
  3591. #define ZSTD_LDM_MINMATCH_MAX 4096
  3592. #define ZSTD_LDM_BUCKETSIZELOG_MIN 1
  3593. #define ZSTD_LDM_BUCKETSIZELOG_MAX 8
  3594. #define ZSTD_LDM_HASHRATELOG_MIN 0
  3595. #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
  3596. /* Advanced parameter bounds */
  3597. #define ZSTD_TARGETCBLOCKSIZE_MIN 64
  3598. #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX
  3599. #define ZSTD_SRCSIZEHINT_MIN 0
  3600. #define ZSTD_SRCSIZEHINT_MAX INT_MAX
  3601. /* internal */
  3602. #define ZSTD_HASHLOG3_MAX 17
  3603. /* --- Advanced types --- */
  3604. typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
  3605. typedef struct {
  3606. unsigned int matchPos; /* Match pos in dst */
  3607. /* If seqDef.offset > 3, then this is seqDef.offset - 3
  3608. * If seqDef.offset < 3, then this is the corresponding repeat offset
  3609. * But if seqDef.offset < 3 and litLength == 0, this is the
  3610. * repeat offset before the corresponding repeat offset
  3611. * And if seqDef.offset == 3 and litLength == 0, this is the
  3612. * most recent repeat offset - 1
  3613. */
  3614. unsigned int offset;
  3615. unsigned int litLength; /* Literal length */
  3616. unsigned int matchLength; /* Match length */
  3617. /* 0 when seq not rep and seqDef.offset otherwise
  3618. * when litLength == 0 this will be <= 4, otherwise <= 3 like normal
  3619. */
  3620. unsigned int rep;
  3621. } ZSTD_Sequence;
  3622. typedef struct {
  3623. unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
  3624. unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
  3625. unsigned hashLog; /**< dispatch table : larger == faster, more memory */
  3626. unsigned searchLog; /**< nb of searches : larger == more compression, slower */
  3627. unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */
  3628. unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
  3629. ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */
  3630. } ZSTD_compressionParameters;
  3631. typedef struct {
  3632. int contentSizeFlag; /**< 1: content size will be in frame header (when known) */
  3633. int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */
  3634. int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */
  3635. } ZSTD_frameParameters;
  3636. typedef struct {
  3637. ZSTD_compressionParameters cParams;
  3638. ZSTD_frameParameters fParams;
  3639. } ZSTD_parameters;
  3640. typedef enum {
  3641. ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
  3642. ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
  3643. ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */
  3644. } ZSTD_dictContentType_e;
  3645. typedef enum {
  3646. ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
  3647. ZSTD_dlm_byRef = 1 /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
  3648. } ZSTD_dictLoadMethod_e;
  3649. typedef enum {
  3650. ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
  3651. ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number.
  3652. * Useful to save 4 bytes per generated frame.
  3653. * Decoder cannot recognise automatically this format, requiring this instruction. */
  3654. } ZSTD_format_e;
  3655. typedef enum {
  3656. /* Note: this enum and the behavior it controls are effectively internal
  3657. * implementation details of the compressor. They are expected to continue
  3658. * to evolve and should be considered only in the context of extremely
  3659. * advanced performance tuning.
  3660. *
  3661. * Zstd currently supports the use of a CDict in three ways:
  3662. *
  3663. * - The contents of the CDict can be copied into the working context. This
  3664. * means that the compression can search both the dictionary and input
  3665. * while operating on a single set of internal tables. This makes
  3666. * the compression faster per-byte of input. However, the initial copy of
  3667. * the CDict's tables incurs a fixed cost at the beginning of the
  3668. * compression. For small compressions (< 8 KB), that copy can dominate
  3669. * the cost of the compression.
  3670. *
  3671. * - The CDict's tables can be used in-place. In this model, compression is
  3672. * slower per input byte, because the compressor has to search two sets of
  3673. * tables. However, this model incurs no start-up cost (as long as the
  3674. * working context's tables can be reused). For small inputs, this can be
  3675. * faster than copying the CDict's tables.
  3676. *
  3677. * - The CDict's tables are not used at all, and instead we use the working
  3678. * context alone to reload the dictionary and use params based on the source
  3679. * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict().
  3680. * This method is effective when the dictionary sizes are very small relative
  3681. * to the input size, and the input size is fairly large to begin with.
  3682. *
  3683. * Zstd has a simple internal heuristic that selects which strategy to use
  3684. * at the beginning of a compression. However, if experimentation shows that
  3685. * Zstd is making poor choices, it is possible to override that choice with
  3686. * this enum.
  3687. */
  3688. ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
  3689. ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */
  3690. ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */
  3691. ZSTD_dictForceLoad = 3 /* Always reload the dictionary */
  3692. } ZSTD_dictAttachPref_e;
  3693. typedef enum {
  3694. ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level.
  3695. * Negative compression levels will be uncompressed, and positive compression
  3696. * levels will be compressed. */
  3697. ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be
  3698. * emitted if Huffman compression is not profitable. */
  3699. ZSTD_lcm_uncompressed = 2 /**< Always emit uncompressed literals. */
  3700. } ZSTD_literalCompressionMode_e;
  3701. /***************************************
  3702. * Frame size functions
  3703. ***************************************/
  3704. /*! ZSTD_findDecompressedSize() :
  3705. * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
  3706. * `srcSize` must be the _exact_ size of this series
  3707. * (i.e. there should be a frame boundary at `src + srcSize`)
  3708. * @return : - decompressed size of all data in all successive frames
  3709. * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
  3710. * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
  3711. *
  3712. * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
  3713. * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
  3714. * In which case, it's necessary to use streaming mode to decompress data.
  3715. * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
  3716. * note 3 : decompressed size can be very large (64-bits value),
  3717. * potentially larger than what local system can handle as a single memory segment.
  3718. * In which case, it's necessary to use streaming mode to decompress data.
  3719. * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
  3720. * Always ensure result fits within application's authorized limits.
  3721. * Each application can set its own limits.
  3722. * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
  3723. * read each contained frame header. This is fast as most of the data is skipped,
  3724. * however it does mean that all frame data must be present and valid. */
  3725. ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
  3726. /*! ZSTD_decompressBound() :
  3727. * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
  3728. * `srcSize` must be the _exact_ size of this series
  3729. * (i.e. there should be a frame boundary at `src + srcSize`)
  3730. * @return : - upper-bound for the decompressed size of all data in all successive frames
  3731. * - if an error occured: ZSTD_CONTENTSIZE_ERROR
  3732. *
  3733. * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame.
  3734. * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`.
  3735. * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value.
  3736. * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by:
  3737. * upper-bound = # blocks * min(128 KB, Window_Size)
  3738. */
  3739. ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize);
  3740. /*! ZSTD_frameHeaderSize() :
  3741. * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
  3742. * @return : size of the Frame Header,
  3743. * or an error code (if srcSize is too small) */
  3744. ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
  3745. /*! ZSTD_getSequences() :
  3746. * Extract sequences from the sequence store
  3747. * zc can be used to insert custom compression params.
  3748. * This function invokes ZSTD_compress2
  3749. * @return : number of sequences extracted
  3750. */
  3751. ZSTDLIB_API size_t ZSTD_getSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs,
  3752. size_t outSeqsSize, const void* src, size_t srcSize);
  3753. /***************************************
  3754. * Memory management
  3755. ***************************************/
  3756. /*! ZSTD_estimate*() :
  3757. * These functions make it possible to estimate memory usage
  3758. * of a future {D,C}Ctx, before its creation.
  3759. *
  3760. * ZSTD_estimateCCtxSize() will provide a memory budget large enough
  3761. * for any compression level up to selected one.
  3762. * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate
  3763. * does not include space for a window buffer.
  3764. * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming.
  3765. * The estimate will assume the input may be arbitrarily large,
  3766. * which is the worst case.
  3767. *
  3768. * When srcSize can be bound by a known and rather "small" value,
  3769. * this fact can be used to provide a tighter estimation
  3770. * because the CCtx compression context will need less memory.
  3771. * This tighter estimation can be provided by more advanced functions
  3772. * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(),
  3773. * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter().
  3774. * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits.
  3775. *
  3776. * Note 2 : only single-threaded compression is supported.
  3777. * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1.
  3778. */
  3779. ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
  3780. ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
  3781. ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  3782. ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
  3783. /*! ZSTD_estimateCStreamSize() :
  3784. * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
  3785. * It will also consider src size to be arbitrarily "large", which is worst case.
  3786. * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
  3787. * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  3788. * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
  3789. * Note : CStream size estimation is only correct for single-threaded compression.
  3790. * ZSTD_DStream memory budget depends on window Size.
  3791. * This information can be passed manually, using ZSTD_estimateDStreamSize,
  3792. * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
  3793. * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
  3794. * an internal ?Dict will be created, which additional size is not estimated here.
  3795. * In this case, get total size by adding ZSTD_estimate?DictSize */
  3796. ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
  3797. ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
  3798. ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  3799. ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
  3800. ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
  3801. /*! ZSTD_estimate?DictSize() :
  3802. * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
  3803. * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
  3804. * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
  3805. */
  3806. ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
  3807. ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
  3808. ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
  3809. /*! ZSTD_initStatic*() :
  3810. * Initialize an object using a pre-allocated fixed-size buffer.
  3811. * workspace: The memory area to emplace the object into.
  3812. * Provided pointer *must be 8-bytes aligned*.
  3813. * Buffer must outlive object.
  3814. * workspaceSize: Use ZSTD_estimate*Size() to determine
  3815. * how large workspace must be to support target scenario.
  3816. * @return : pointer to object (same address as workspace, just different type),
  3817. * or NULL if error (size too small, incorrect alignment, etc.)
  3818. * Note : zstd will never resize nor malloc() when using a static buffer.
  3819. * If the object requires more memory than available,
  3820. * zstd will just error out (typically ZSTD_error_memory_allocation).
  3821. * Note 2 : there is no corresponding "free" function.
  3822. * Since workspace is allocated externally, it must be freed externally too.
  3823. * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
  3824. * into its associated cParams.
  3825. * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
  3826. * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
  3827. * Limitation 2 : static cctx currently not compatible with multi-threading.
  3828. * Limitation 3 : static dctx is incompatible with legacy support.
  3829. */
  3830. ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
  3831. ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
  3832. ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
  3833. ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
  3834. ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
  3835. void* workspace, size_t workspaceSize,
  3836. const void* dict, size_t dictSize,
  3837. ZSTD_dictLoadMethod_e dictLoadMethod,
  3838. ZSTD_dictContentType_e dictContentType,
  3839. ZSTD_compressionParameters cParams);
  3840. ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
  3841. void* workspace, size_t workspaceSize,
  3842. const void* dict, size_t dictSize,
  3843. ZSTD_dictLoadMethod_e dictLoadMethod,
  3844. ZSTD_dictContentType_e dictContentType);
  3845. /*! Custom memory allocation :
  3846. * These prototypes make it possible to pass your own allocation/free functions.
  3847. * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
  3848. * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
  3849. */
  3850. typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
  3851. typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
  3852. typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
  3853. static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
  3854. ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
  3855. ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
  3856. ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
  3857. ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
  3858. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
  3859. ZSTD_dictLoadMethod_e dictLoadMethod,
  3860. ZSTD_dictContentType_e dictContentType,
  3861. ZSTD_compressionParameters cParams,
  3862. ZSTD_customMem customMem);
  3863. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
  3864. ZSTD_dictLoadMethod_e dictLoadMethod,
  3865. ZSTD_dictContentType_e dictContentType,
  3866. ZSTD_customMem customMem);
  3867. /***************************************
  3868. * Advanced compression functions
  3869. ***************************************/
  3870. /*! ZSTD_createCDict_byReference() :
  3871. * Create a digested dictionary for compression
  3872. * Dictionary content is just referenced, not duplicated.
  3873. * As a consequence, `dictBuffer` **must** outlive CDict,
  3874. * and its content must remain unmodified throughout the lifetime of CDict.
  3875. * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */
  3876. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
  3877. /*! ZSTD_getCParams() :
  3878. * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
  3879. * `estimatedSrcSize` value is optional, select 0 if not known */
  3880. ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  3881. /*! ZSTD_getParams() :
  3882. * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
  3883. * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
  3884. ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  3885. /*! ZSTD_checkCParams() :
  3886. * Ensure param values remain within authorized range.
  3887. * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */
  3888. ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
  3889. /*! ZSTD_adjustCParams() :
  3890. * optimize params for a given `srcSize` and `dictSize`.
  3891. * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN.
  3892. * `dictSize` must be `0` when there is no dictionary.
  3893. * cPar can be invalid : all parameters will be clamped within valid range in the @return struct.
  3894. * This function never fails (wide contract) */
  3895. ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
  3896. /*! ZSTD_compress_advanced() :
  3897. * Note : this function is now DEPRECATED.
  3898. * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters.
  3899. * This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */
  3900. ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
  3901. void* dst, size_t dstCapacity,
  3902. const void* src, size_t srcSize,
  3903. const void* dict,size_t dictSize,
  3904. ZSTD_parameters params);
  3905. /*! ZSTD_compress_usingCDict_advanced() :
  3906. * Note : this function is now REDUNDANT.
  3907. * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters.
  3908. * This prototype will be marked as deprecated and generate compilation warning in some future version */
  3909. ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
  3910. void* dst, size_t dstCapacity,
  3911. const void* src, size_t srcSize,
  3912. const ZSTD_CDict* cdict,
  3913. ZSTD_frameParameters fParams);
  3914. /*! ZSTD_CCtx_loadDictionary_byReference() :
  3915. * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx.
  3916. * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */
  3917. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
  3918. /*! ZSTD_CCtx_loadDictionary_advanced() :
  3919. * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over
  3920. * how to load the dictionary (by copy ? by reference ?)
  3921. * and how to interpret it (automatic ? force raw mode ? full mode only ?) */
  3922. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
  3923. /*! ZSTD_CCtx_refPrefix_advanced() :
  3924. * Same as ZSTD_CCtx_refPrefix(), but gives finer control over
  3925. * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
  3926. ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
  3927. /* === experimental parameters === */
  3928. /* these parameters can be used with ZSTD_setParameter()
  3929. * they are not guaranteed to remain supported in the future */
  3930. /* Enables rsyncable mode,
  3931. * which makes compressed files more rsync friendly
  3932. * by adding periodic synchronization points to the compressed data.
  3933. * The target average block size is ZSTD_c_jobSize / 2.
  3934. * It's possible to modify the job size to increase or decrease
  3935. * the granularity of the synchronization point.
  3936. * Once the jobSize is smaller than the window size,
  3937. * it will result in compression ratio degradation.
  3938. * NOTE 1: rsyncable mode only works when multithreading is enabled.
  3939. * NOTE 2: rsyncable performs poorly in combination with long range mode,
  3940. * since it will decrease the effectiveness of synchronization points,
  3941. * though mileage may vary.
  3942. * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s.
  3943. * If the selected compression level is already running significantly slower,
  3944. * the overall speed won't be significantly impacted.
  3945. */
  3946. #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1
  3947. /* Select a compression format.
  3948. * The value must be of type ZSTD_format_e.
  3949. * See ZSTD_format_e enum definition for details */
  3950. #define ZSTD_c_format ZSTD_c_experimentalParam2
  3951. /* Force back-reference distances to remain < windowSize,
  3952. * even when referencing into Dictionary content (default:0) */
  3953. #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3
  3954. /* Controls whether the contents of a CDict
  3955. * are used in place, or copied into the working context.
  3956. * Accepts values from the ZSTD_dictAttachPref_e enum.
  3957. * See the comments on that enum for an explanation of the feature. */
  3958. #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
  3959. /* Controls how the literals are compressed (default is auto).
  3960. * The value must be of type ZSTD_literalCompressionMode_e.
  3961. * See ZSTD_literalCompressionMode_t enum definition for details.
  3962. */
  3963. #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5
  3964. /* Tries to fit compressed block size to be around targetCBlockSize.
  3965. * No target when targetCBlockSize == 0.
  3966. * There is no guarantee on compressed block size (default:0) */
  3967. #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6
  3968. /* User's best guess of source size.
  3969. * Hint is not valid when srcSizeHint == 0.
  3970. * There is no guarantee that hint is close to actual source size,
  3971. * but compression ratio may regress significantly if guess considerably underestimates */
  3972. #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7
  3973. /*! ZSTD_CCtx_getParameter() :
  3974. * Get the requested compression parameter value, selected by enum ZSTD_cParameter,
  3975. * and store it into int* value.
  3976. * @return : 0, or an error code (which can be tested with ZSTD_isError()).
  3977. */
  3978. ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value);
  3979. /*! ZSTD_CCtx_params :
  3980. * Quick howto :
  3981. * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
  3982. * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into
  3983. * an existing ZSTD_CCtx_params structure.
  3984. * This is similar to
  3985. * ZSTD_CCtx_setParameter().
  3986. * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
  3987. * an existing CCtx.
  3988. * These parameters will be applied to
  3989. * all subsequent frames.
  3990. * - ZSTD_compressStream2() : Do compression using the CCtx.
  3991. * - ZSTD_freeCCtxParams() : Free the memory.
  3992. *
  3993. * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
  3994. * for static allocation of CCtx for single-threaded compression.
  3995. */
  3996. ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
  3997. ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
  3998. /*! ZSTD_CCtxParams_reset() :
  3999. * Reset params to default values.
  4000. */
  4001. ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
  4002. /*! ZSTD_CCtxParams_init() :
  4003. * Initializes the compression parameters of cctxParams according to
  4004. * compression level. All other parameters are reset to their default values.
  4005. */
  4006. ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
  4007. /*! ZSTD_CCtxParams_init_advanced() :
  4008. * Initializes the compression and frame parameters of cctxParams according to
  4009. * params. All other parameters are reset to their default values.
  4010. */
  4011. ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
  4012. /*! ZSTD_CCtxParams_setParameter() :
  4013. * Similar to ZSTD_CCtx_setParameter.
  4014. * Set one compression parameter, selected by enum ZSTD_cParameter.
  4015. * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
  4016. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  4017. */
  4018. ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
  4019. /*! ZSTD_CCtxParams_getParameter() :
  4020. * Similar to ZSTD_CCtx_getParameter.
  4021. * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
  4022. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  4023. */
  4024. ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
  4025. /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
  4026. * Apply a set of ZSTD_CCtx_params to the compression context.
  4027. * This can be done even after compression is started,
  4028. * if nbWorkers==0, this will have no impact until a new compression is started.
  4029. * if nbWorkers>=1, new parameters will be picked up at next job,
  4030. * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
  4031. */
  4032. ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
  4033. ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
  4034. /*! ZSTD_compressStream2_simpleArgs() :
  4035. * Same as ZSTD_compressStream2(),
  4036. * but using only integral types as arguments.
  4037. * This variant might be helpful for binders from dynamic languages
  4038. * which have troubles handling structures containing memory pointers.
  4039. */
  4040. ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs (
  4041. ZSTD_CCtx* cctx,
  4042. void* dst, size_t dstCapacity, size_t* dstPos,
  4043. const void* src, size_t srcSize, size_t* srcPos,
  4044. ZSTD_EndDirective endOp);
  4045. /***************************************
  4046. * Advanced decompression functions
  4047. ***************************************/
  4048. /*! ZSTD_isFrame() :
  4049. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  4050. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  4051. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  4052. * Note 3 : Skippable Frame Identifiers are considered valid. */
  4053. ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
  4054. /*! ZSTD_createDDict_byReference() :
  4055. * Create a digested dictionary, ready to start decompression operation without startup delay.
  4056. * Dictionary content is referenced, and therefore stays in dictBuffer.
  4057. * It is important that dictBuffer outlives DDict,
  4058. * it must remain read accessible throughout the lifetime of DDict */
  4059. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
  4060. /*! ZSTD_DCtx_loadDictionary_byReference() :
  4061. * Same as ZSTD_DCtx_loadDictionary(),
  4062. * but references `dict` content instead of copying it into `dctx`.
  4063. * This saves memory if `dict` remains around.,
  4064. * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */
  4065. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  4066. /*! ZSTD_DCtx_loadDictionary_advanced() :
  4067. * Same as ZSTD_DCtx_loadDictionary(),
  4068. * but gives direct control over
  4069. * how to load the dictionary (by copy ? by reference ?)
  4070. * and how to interpret it (automatic ? force raw mode ? full mode only ?). */
  4071. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
  4072. /*! ZSTD_DCtx_refPrefix_advanced() :
  4073. * Same as ZSTD_DCtx_refPrefix(), but gives finer control over
  4074. * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
  4075. ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
  4076. /*! ZSTD_DCtx_setMaxWindowSize() :
  4077. * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
  4078. * This protects a decoder context from reserving too much memory for itself (potential attack scenario).
  4079. * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
  4080. * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT)
  4081. * @return : 0, or an error code (which can be tested using ZSTD_isError()).
  4082. */
  4083. ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
  4084. /* ZSTD_d_format
  4085. * experimental parameter,
  4086. * allowing selection between ZSTD_format_e input compression formats
  4087. */
  4088. #define ZSTD_d_format ZSTD_d_experimentalParam1
  4089. /* ZSTD_d_stableOutBuffer
  4090. * Experimental parameter.
  4091. * Default is 0 == disabled. Set to 1 to enable.
  4092. *
  4093. * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same
  4094. * between calls, except for the modifications that zstd makes to pos (the
  4095. * caller must not modify pos). This is checked by the decompressor, and
  4096. * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer
  4097. * MUST be large enough to fit the entire decompressed frame. This will be
  4098. * checked when the frame content size is known. The data in the ZSTD_outBuffer
  4099. * in the range [dst, dst + pos) MUST not be modified during decompression
  4100. * or you will get data corruption.
  4101. *
  4102. * When this flags is enabled zstd won't allocate an output buffer, because
  4103. * it can write directly to the ZSTD_outBuffer, but it will still allocate
  4104. * an input buffer large enough to fit any compressed block. This will also
  4105. * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer.
  4106. * If you need to avoid the input buffer allocation use the buffer-less
  4107. * streaming API.
  4108. *
  4109. * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using
  4110. * this flag is ALWAYS memory safe, and will never access out-of-bounds
  4111. * memory. However, decompression WILL fail if you violate the preconditions.
  4112. *
  4113. * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST
  4114. * not be modified during decompression or you will get data corruption. This
  4115. * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate
  4116. * matches. Normally zstd maintains its own buffer for this purpose, but passing
  4117. * this flag tells zstd to use the user provided buffer.
  4118. */
  4119. #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2
  4120. /*! ZSTD_DCtx_setFormat() :
  4121. * Instruct the decoder context about what kind of data to decode next.
  4122. * This instruction is mandatory to decode data without a fully-formed header,
  4123. * such ZSTD_f_zstd1_magicless for example.
  4124. * @return : 0, or an error code (which can be tested using ZSTD_isError()). */
  4125. ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
  4126. /*! ZSTD_decompressStream_simpleArgs() :
  4127. * Same as ZSTD_decompressStream(),
  4128. * but using only integral types as arguments.
  4129. * This can be helpful for binders from dynamic languages
  4130. * which have troubles handling structures containing memory pointers.
  4131. */
  4132. ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs (
  4133. ZSTD_DCtx* dctx,
  4134. void* dst, size_t dstCapacity, size_t* dstPos,
  4135. const void* src, size_t srcSize, size_t* srcPos);
  4136. /********************************************************************
  4137. * Advanced streaming functions
  4138. * Warning : most of these functions are now redundant with the Advanced API.
  4139. * Once Advanced API reaches "stable" status,
  4140. * redundant functions will be deprecated, and then at some point removed.
  4141. ********************************************************************/
  4142. /*===== Advanced Streaming compression functions =====*/
  4143. /**! ZSTD_initCStream_srcSize() :
  4144. * This function is deprecated, and equivalent to:
  4145. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4146. * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
  4147. * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
  4148. * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
  4149. *
  4150. * pledgedSrcSize must be correct. If it is not known at init time, use
  4151. * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs,
  4152. * "0" also disables frame content size field. It may be enabled in the future.
  4153. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4154. */
  4155. ZSTDLIB_API size_t
  4156. ZSTD_initCStream_srcSize(ZSTD_CStream* zcs,
  4157. int compressionLevel,
  4158. unsigned long long pledgedSrcSize);
  4159. /**! ZSTD_initCStream_usingDict() :
  4160. * This function is deprecated, and is equivalent to:
  4161. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4162. * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
  4163. * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
  4164. *
  4165. * Creates of an internal CDict (incompatible with static CCtx), except if
  4166. * dict == NULL or dictSize < 8, in which case no dict is used.
  4167. * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if
  4168. * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.
  4169. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4170. */
  4171. ZSTDLIB_API size_t
  4172. ZSTD_initCStream_usingDict(ZSTD_CStream* zcs,
  4173. const void* dict, size_t dictSize,
  4174. int compressionLevel);
  4175. /**! ZSTD_initCStream_advanced() :
  4176. * This function is deprecated, and is approximately equivalent to:
  4177. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4178. * // Pseudocode: Set each zstd parameter and leave the rest as-is.
  4179. * for ((param, value) : params) {
  4180. * ZSTD_CCtx_setParameter(zcs, param, value);
  4181. * }
  4182. * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
  4183. * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
  4184. *
  4185. * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy.
  4186. * pledgedSrcSize must be correct.
  4187. * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN.
  4188. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4189. */
  4190. ZSTDLIB_API size_t
  4191. ZSTD_initCStream_advanced(ZSTD_CStream* zcs,
  4192. const void* dict, size_t dictSize,
  4193. ZSTD_parameters params,
  4194. unsigned long long pledgedSrcSize);
  4195. /**! ZSTD_initCStream_usingCDict() :
  4196. * This function is deprecated, and equivalent to:
  4197. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4198. * ZSTD_CCtx_refCDict(zcs, cdict);
  4199. *
  4200. * note : cdict will just be referenced, and must outlive compression session
  4201. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4202. */
  4203. ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);
  4204. /**! ZSTD_initCStream_usingCDict_advanced() :
  4205. * This function is DEPRECATED, and is approximately equivalent to:
  4206. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4207. * // Pseudocode: Set each zstd frame parameter and leave the rest as-is.
  4208. * for ((fParam, value) : fParams) {
  4209. * ZSTD_CCtx_setParameter(zcs, fParam, value);
  4210. * }
  4211. * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
  4212. * ZSTD_CCtx_refCDict(zcs, cdict);
  4213. *
  4214. * same as ZSTD_initCStream_usingCDict(), with control over frame parameters.
  4215. * pledgedSrcSize must be correct. If srcSize is not known at init time, use
  4216. * value ZSTD_CONTENTSIZE_UNKNOWN.
  4217. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4218. */
  4219. ZSTDLIB_API size_t
  4220. ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs,
  4221. const ZSTD_CDict* cdict,
  4222. ZSTD_frameParameters fParams,
  4223. unsigned long long pledgedSrcSize);
  4224. /*! ZSTD_resetCStream() :
  4225. * This function is deprecated, and is equivalent to:
  4226. * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
  4227. * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
  4228. *
  4229. * start a new frame, using same parameters from previous frame.
  4230. * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
  4231. * Note that zcs must be init at least once before using ZSTD_resetCStream().
  4232. * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
  4233. * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
  4234. * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
  4235. * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
  4236. * @return : 0, or an error code (which can be tested using ZSTD_isError())
  4237. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4238. */
  4239. ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
  4240. typedef struct {
  4241. unsigned long long ingested; /* nb input bytes read and buffered */
  4242. unsigned long long consumed; /* nb input bytes actually compressed */
  4243. unsigned long long produced; /* nb of compressed bytes generated and buffered */
  4244. unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
  4245. unsigned currentJobID; /* MT only : latest started job nb */
  4246. unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
  4247. } ZSTD_frameProgression;
  4248. /* ZSTD_getFrameProgression() :
  4249. * tells how much data has been ingested (read from input)
  4250. * consumed (input actually compressed) and produced (output) for current frame.
  4251. * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
  4252. * Aggregates progression inside active worker threads.
  4253. */
  4254. ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
  4255. /*! ZSTD_toFlushNow() :
  4256. * Tell how many bytes are ready to be flushed immediately.
  4257. * Useful for multithreading scenarios (nbWorkers >= 1).
  4258. * Probe the oldest active job, defined as oldest job not yet entirely flushed,
  4259. * and check its output buffer.
  4260. * @return : amount of data stored in oldest job and ready to be flushed immediately.
  4261. * if @return == 0, it means either :
  4262. * + there is no active job (could be checked with ZSTD_frameProgression()), or
  4263. * + oldest job is still actively compressing data,
  4264. * but everything it has produced has also been flushed so far,
  4265. * therefore flush speed is limited by production speed of oldest job
  4266. * irrespective of the speed of concurrent (and newer) jobs.
  4267. */
  4268. ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
  4269. /*===== Advanced Streaming decompression functions =====*/
  4270. /**
  4271. * This function is deprecated, and is equivalent to:
  4272. *
  4273. * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
  4274. * ZSTD_DCtx_loadDictionary(zds, dict, dictSize);
  4275. *
  4276. * note: no dictionary will be used if dict == NULL or dictSize < 8
  4277. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4278. */
  4279. ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
  4280. /**
  4281. * This function is deprecated, and is equivalent to:
  4282. *
  4283. * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
  4284. * ZSTD_DCtx_refDDict(zds, ddict);
  4285. *
  4286. * note : ddict is referenced, it must outlive decompression session
  4287. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4288. */
  4289. ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);
  4290. /**
  4291. * This function is deprecated, and is equivalent to:
  4292. *
  4293. * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
  4294. *
  4295. * re-use decompression parameters from previous init; saves dictionary loading
  4296. * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
  4297. */
  4298. ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);
  4299. /*********************************************************************
  4300. * Buffer-less and synchronous inner streaming functions
  4301. *
  4302. * This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
  4303. * But it's also a complex one, with several restrictions, documented below.
  4304. * Prefer normal streaming API for an easier experience.
  4305. ********************************************************************* */
  4306. /**
  4307. Buffer-less streaming compression (synchronous mode)
  4308. A ZSTD_CCtx object is required to track streaming operations.
  4309. Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
  4310. ZSTD_CCtx object can be re-used multiple times within successive compression operations.
  4311. Start by initializing a context.
  4312. Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
  4313. or ZSTD_compressBegin_advanced(), for finer parameter control.
  4314. It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
  4315. Then, consume your input using ZSTD_compressContinue().
  4316. There are some important considerations to keep in mind when using this advanced function :
  4317. - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
  4318. - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
  4319. - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
  4320. Worst case evaluation is provided by ZSTD_compressBound().
  4321. ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
  4322. - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
  4323. It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
  4324. - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
  4325. In which case, it will "discard" the relevant memory section from its history.
  4326. Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
  4327. It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
  4328. Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
  4329. `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
  4330. */
  4331. /*===== Buffer-less streaming compression functions =====*/
  4332. ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
  4333. ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
  4334. ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */
  4335. ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
  4336. ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */
  4337. ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */
  4338. ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  4339. ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  4340. /*-
  4341. Buffer-less streaming decompression (synchronous mode)
  4342. A ZSTD_DCtx object is required to track streaming operations.
  4343. Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
  4344. A ZSTD_DCtx object can be re-used multiple times.
  4345. First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
  4346. Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
  4347. Data fragment must be large enough to ensure successful decoding.
  4348. `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
  4349. @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
  4350. >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
  4351. errorCode, which can be tested using ZSTD_isError().
  4352. It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
  4353. such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
  4354. Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
  4355. As a consequence, check that values remain within valid application range.
  4356. For example, do not allocate memory blindly, check that `windowSize` is within expectation.
  4357. Each application can set its own limits, depending on local restrictions.
  4358. For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
  4359. ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
  4360. ZSTD_decompressContinue() is very sensitive to contiguity,
  4361. if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
  4362. or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
  4363. There are multiple ways to guarantee this condition.
  4364. The most memory efficient way is to use a round buffer of sufficient size.
  4365. Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
  4366. which can @return an error code if required value is too large for current system (in 32-bits mode).
  4367. In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
  4368. up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
  4369. which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
  4370. At which point, decoding can resume from the beginning of the buffer.
  4371. Note that already decoded data stored in the buffer should be flushed before being overwritten.
  4372. There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
  4373. Finally, if you control the compression process, you can also ignore all buffer size rules,
  4374. as long as the encoder and decoder progress in "lock-step",
  4375. aka use exactly the same buffer sizes, break contiguity at the same place, etc.
  4376. Once buffers are setup, start decompression, with ZSTD_decompressBegin().
  4377. If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
  4378. Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
  4379. ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  4380. ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
  4381. @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
  4382. It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
  4383. It can also be an error code, which can be tested with ZSTD_isError().
  4384. A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
  4385. Context can then be reset to start a new decompression.
  4386. Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
  4387. This information is not required to properly decode a frame.
  4388. == Special case : skippable frames ==
  4389. Skippable frames allow integration of user-defined data into a flow of concatenated frames.
  4390. Skippable frames will be ignored (skipped) by decompressor.
  4391. The format of skippable frames is as follows :
  4392. a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
  4393. b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
  4394. c) Frame Content - any content (User Data) of length equal to Frame Size
  4395. For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
  4396. For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
  4397. */
  4398. /*===== Buffer-less streaming decompression functions =====*/
  4399. typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
  4400. typedef struct {
  4401. unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
  4402. unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
  4403. unsigned blockSizeMax;
  4404. ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
  4405. unsigned headerSize;
  4406. unsigned dictID;
  4407. unsigned checksumFlag;
  4408. } ZSTD_frameHeader;
  4409. /*! ZSTD_getFrameHeader() :
  4410. * decode Frame Header, or requires larger `srcSize`.
  4411. * @return : 0, `zfhPtr` is correctly filled,
  4412. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  4413. * or an error code, which can be tested using ZSTD_isError() */
  4414. ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  4415. /*! ZSTD_getFrameHeader_advanced() :
  4416. * same as ZSTD_getFrameHeader(),
  4417. * with added capability to select a format (like ZSTD_f_zstd1_magicless) */
  4418. ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format);
  4419. ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */
  4420. ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
  4421. ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  4422. ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
  4423. ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
  4424. ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  4425. /* misc */
  4426. ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
  4427. typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
  4428. ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
  4429. /* ============================ */
  4430. /** Block level API */
  4431. /* ============================ */
  4432. /*!
  4433. Block functions produce and decode raw zstd blocks, without frame metadata.
  4434. Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes).
  4435. But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes.
  4436. A few rules to respect :
  4437. - Compressing and decompressing require a context structure
  4438. + Use ZSTD_createCCtx() and ZSTD_createDCtx()
  4439. - It is necessary to init context before starting
  4440. + compression : any ZSTD_compressBegin*() variant, including with dictionary
  4441. + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
  4442. + copyCCtx() and copyDCtx() can be used too
  4443. - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
  4444. + If input is larger than a block size, it's necessary to split input data into multiple blocks
  4445. + For inputs larger than a single block, consider using regular ZSTD_compress() instead.
  4446. Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block.
  4447. - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) !
  4448. ===> In which case, nothing is produced into `dst` !
  4449. + User __must__ test for such outcome and deal directly with uncompressed data
  4450. + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0.
  4451. Doing so would mess up with statistics history, leading to potential data corruption.
  4452. + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !!
  4453. + In case of multiple successive blocks, should some of them be uncompressed,
  4454. decoder must be informed of their existence in order to follow proper history.
  4455. Use ZSTD_insertBlock() for such a case.
  4456. */
  4457. /*===== Raw zstd block functions =====*/
  4458. ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx);
  4459. ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  4460. ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  4461. ZSTDLIB_API size_t ZSTD_insertBlock (ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression. */
  4462. #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
  4463. #if defined (__cplusplus)
  4464. }
  4465. #endif
  4466. /**** ended inlining ../zstd.h ****/
  4467. #define FSE_STATIC_LINKING_ONLY
  4468. /**** skipping file: fse.h ****/
  4469. #define HUF_STATIC_LINKING_ONLY
  4470. /**** skipping file: huf.h ****/
  4471. #ifndef XXH_STATIC_LINKING_ONLY
  4472. # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  4473. #endif
  4474. /**** start inlining xxhash.h ****/
  4475. /*
  4476. * xxHash - Extremely Fast Hash algorithm
  4477. * Header File
  4478. * Copyright (c) 2012-2020, Yann Collet, Facebook, Inc.
  4479. *
  4480. * You can contact the author at :
  4481. * - xxHash source repository : https://github.com/Cyan4973/xxHash
  4482. *
  4483. * This source code is licensed under both the BSD-style license (found in the
  4484. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  4485. * in the COPYING file in the root directory of this source tree).
  4486. * You may select, at your option, one of the above-listed licenses.
  4487. */
  4488. /* Notice extracted from xxHash homepage :
  4489. xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
  4490. It also successfully passes all tests from the SMHasher suite.
  4491. Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
  4492. Name Speed Q.Score Author
  4493. xxHash 5.4 GB/s 10
  4494. CrapWow 3.2 GB/s 2 Andrew
  4495. MumurHash 3a 2.7 GB/s 10 Austin Appleby
  4496. SpookyHash 2.0 GB/s 10 Bob Jenkins
  4497. SBox 1.4 GB/s 9 Bret Mulvey
  4498. Lookup3 1.2 GB/s 9 Bob Jenkins
  4499. SuperFastHash 1.2 GB/s 1 Paul Hsieh
  4500. CityHash64 1.05 GB/s 10 Pike & Alakuijala
  4501. FNV 0.55 GB/s 5 Fowler, Noll, Vo
  4502. CRC32 0.43 GB/s 9
  4503. MD5-32 0.33 GB/s 10 Ronald L. Rivest
  4504. SHA1-32 0.28 GB/s 10
  4505. Q.Score is a measure of quality of the hash function.
  4506. It depends on successfully passing SMHasher test set.
  4507. 10 is a perfect score.
  4508. A 64-bits version, named XXH64, is available since r35.
  4509. It offers much better speed, but for 64-bits applications only.
  4510. Name Speed on 64 bits Speed on 32 bits
  4511. XXH64 13.8 GB/s 1.9 GB/s
  4512. XXH32 6.8 GB/s 6.0 GB/s
  4513. */
  4514. #if defined (__cplusplus)
  4515. extern "C" {
  4516. #endif
  4517. #ifndef XXHASH_H_5627135585666179
  4518. #define XXHASH_H_5627135585666179 1
  4519. /* ****************************
  4520. * Definitions
  4521. ******************************/
  4522. #include <stddef.h> /* size_t */
  4523. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  4524. /* ****************************
  4525. * API modifier
  4526. ******************************/
  4527. /** XXH_PRIVATE_API
  4528. * This is useful if you want to include xxhash functions in `static` mode
  4529. * in order to inline them, and remove their symbol from the public list.
  4530. * Methodology :
  4531. * #define XXH_PRIVATE_API
  4532. * #include "xxhash.h"
  4533. * `xxhash.c` is automatically included.
  4534. * It's not useful to compile and link it as a separate module anymore.
  4535. */
  4536. #ifdef XXH_PRIVATE_API
  4537. # ifndef XXH_STATIC_LINKING_ONLY
  4538. # define XXH_STATIC_LINKING_ONLY
  4539. # endif
  4540. # if defined(__GNUC__)
  4541. # define XXH_PUBLIC_API static __inline __attribute__((unused))
  4542. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  4543. # define XXH_PUBLIC_API static inline
  4544. # elif defined(_MSC_VER)
  4545. # define XXH_PUBLIC_API static __inline
  4546. # else
  4547. # define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */
  4548. # endif
  4549. #else
  4550. # define XXH_PUBLIC_API /* do nothing */
  4551. #endif /* XXH_PRIVATE_API */
  4552. /*!XXH_NAMESPACE, aka Namespace Emulation :
  4553. If you want to include _and expose_ xxHash functions from within your own library,
  4554. but also want to avoid symbol collisions with another library which also includes xxHash,
  4555. you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
  4556. with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).
  4557. Note that no change is required within the calling program as long as it includes `xxhash.h` :
  4558. regular symbol name will be automatically translated by this header.
  4559. */
  4560. #ifdef XXH_NAMESPACE
  4561. # define XXH_CAT(A,B) A##B
  4562. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  4563. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  4564. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  4565. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  4566. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  4567. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  4568. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  4569. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  4570. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  4571. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  4572. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  4573. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  4574. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  4575. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  4576. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  4577. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  4578. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  4579. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  4580. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  4581. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  4582. #endif
  4583. /* *************************************
  4584. * Version
  4585. ***************************************/
  4586. #define XXH_VERSION_MAJOR 0
  4587. #define XXH_VERSION_MINOR 6
  4588. #define XXH_VERSION_RELEASE 2
  4589. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  4590. XXH_PUBLIC_API unsigned XXH_versionNumber (void);
  4591. /* ****************************
  4592. * Simple Hash Functions
  4593. ******************************/
  4594. typedef unsigned int XXH32_hash_t;
  4595. typedef unsigned long long XXH64_hash_t;
  4596. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
  4597. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
  4598. /*!
  4599. XXH32() :
  4600. Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
  4601. The memory between input & input+length must be valid (allocated and read-accessible).
  4602. "seed" can be used to alter the result predictably.
  4603. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
  4604. XXH64() :
  4605. Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
  4606. "seed" can be used to alter the result predictably.
  4607. This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
  4608. */
  4609. /* ****************************
  4610. * Streaming Hash Functions
  4611. ******************************/
  4612. typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
  4613. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  4614. /*! State allocation, compatible with dynamic libraries */
  4615. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
  4616. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  4617. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
  4618. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  4619. /* hash streaming */
  4620. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);
  4621. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  4622. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  4623. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);
  4624. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
  4625. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
  4626. /*
  4627. These functions generate the xxHash of an input provided in multiple segments.
  4628. Note that, for small input, they are slower than single-call functions, due to state management.
  4629. For small input, prefer `XXH32()` and `XXH64()` .
  4630. XXH state must first be allocated, using XXH*_createState() .
  4631. Start a new hash by initializing state with a seed, using XXH*_reset().
  4632. Then, feed the hash state by calling XXH*_update() as many times as necessary.
  4633. Obviously, input must be allocated and read accessible.
  4634. The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
  4635. Finally, a hash value can be produced anytime, by using XXH*_digest().
  4636. This function returns the nn-bits hash as an int or long long.
  4637. It's still possible to continue inserting input into the hash state after a digest,
  4638. and generate some new hashes later on, by calling again XXH*_digest().
  4639. When done, free XXH state space if it was allocated dynamically.
  4640. */
  4641. /* **************************
  4642. * Utils
  4643. ****************************/
  4644. #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */
  4645. # define restrict /* disable restrict */
  4646. #endif
  4647. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state);
  4648. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state);
  4649. /* **************************
  4650. * Canonical representation
  4651. ****************************/
  4652. /* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
  4653. * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
  4654. * These functions allow transformation of hash result into and from its canonical format.
  4655. * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
  4656. */
  4657. typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
  4658. typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
  4659. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  4660. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
  4661. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  4662. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
  4663. #endif /* XXHASH_H_5627135585666179 */
  4664. /* ================================================================================================
  4665. This section contains definitions which are not guaranteed to remain stable.
  4666. They may change in future versions, becoming incompatible with a different version of the library.
  4667. They shall only be used with static linking.
  4668. Never use these definitions in association with dynamic linking !
  4669. =================================================================================================== */
  4670. #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345)
  4671. #define XXH_STATIC_H_3543687687345
  4672. /* These definitions are only meant to allow allocation of XXH state
  4673. statically, on stack, or in a struct for example.
  4674. Do not use members directly. */
  4675. struct XXH32_state_s {
  4676. unsigned total_len_32;
  4677. unsigned large_len;
  4678. unsigned v1;
  4679. unsigned v2;
  4680. unsigned v3;
  4681. unsigned v4;
  4682. unsigned mem32[4]; /* buffer defined as U32 for alignment */
  4683. unsigned memsize;
  4684. unsigned reserved; /* never read nor write, will be removed in a future version */
  4685. }; /* typedef'd to XXH32_state_t */
  4686. struct XXH64_state_s {
  4687. unsigned long long total_len;
  4688. unsigned long long v1;
  4689. unsigned long long v2;
  4690. unsigned long long v3;
  4691. unsigned long long v4;
  4692. unsigned long long mem64[4]; /* buffer defined as U64 for alignment */
  4693. unsigned memsize;
  4694. unsigned reserved[2]; /* never read nor write, will be removed in a future version */
  4695. }; /* typedef'd to XXH64_state_t */
  4696. # ifdef XXH_PRIVATE_API
  4697. /**** start inlining xxhash.c ****/
  4698. /*
  4699. * xxHash - Fast Hash algorithm
  4700. * Copyright (c) 2012-2020, Yann Collet, Facebook, Inc.
  4701. *
  4702. * You can contact the author at :
  4703. * - xxHash homepage: http://www.xxhash.com
  4704. * - xxHash source repository : https://github.com/Cyan4973/xxHash
  4705. *
  4706. * This source code is licensed under both the BSD-style license (found in the
  4707. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  4708. * in the COPYING file in the root directory of this source tree).
  4709. * You may select, at your option, one of the above-listed licenses.
  4710. */
  4711. /* *************************************
  4712. * Tuning parameters
  4713. ***************************************/
  4714. /*!XXH_FORCE_MEMORY_ACCESS :
  4715. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  4716. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  4717. * The below switch allow to select different access method for improved performance.
  4718. * Method 0 (default) : use `memcpy()`. Safe and portable.
  4719. * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
  4720. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  4721. * Method 2 : direct access. This method doesn't depend on compiler but violate C standard.
  4722. * It can generate buggy code on targets which do not support unaligned memory accesses.
  4723. * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
  4724. * See http://stackoverflow.com/a/32095106/646947 for details.
  4725. * Prefer these methods in priority order (0 > 1 > 2)
  4726. */
  4727. #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  4728. # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  4729. # define XXH_FORCE_MEMORY_ACCESS 2
  4730. # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
  4731. (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) )) || \
  4732. defined(__ICCARM__)
  4733. # define XXH_FORCE_MEMORY_ACCESS 1
  4734. # endif
  4735. #endif
  4736. /*!XXH_ACCEPT_NULL_INPUT_POINTER :
  4737. * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  4738. * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  4739. * By default, this option is disabled. To enable it, uncomment below define :
  4740. */
  4741. /* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */
  4742. /*!XXH_FORCE_NATIVE_FORMAT :
  4743. * By default, xxHash library provides endian-independent Hash values, based on little-endian convention.
  4744. * Results are therefore identical for little-endian and big-endian CPU.
  4745. * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  4746. * Should endian-independence be of no importance for your application, you may set the #define below to 1,
  4747. * to improve speed for Big-endian CPU.
  4748. * This option has no impact on Little_Endian CPU.
  4749. */
  4750. #ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */
  4751. # define XXH_FORCE_NATIVE_FORMAT 0
  4752. #endif
  4753. /*!XXH_FORCE_ALIGN_CHECK :
  4754. * This is a minor performance trick, only useful with lots of very small keys.
  4755. * It means : check for aligned/unaligned input.
  4756. * The check costs one initial branch per hash; set to 0 when the input data
  4757. * is guaranteed to be aligned.
  4758. */
  4759. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  4760. # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  4761. # define XXH_FORCE_ALIGN_CHECK 0
  4762. # else
  4763. # define XXH_FORCE_ALIGN_CHECK 1
  4764. # endif
  4765. #endif
  4766. /* *************************************
  4767. * Includes & Memory related functions
  4768. ***************************************/
  4769. /* Modify the local functions below should you wish to use some other memory routines */
  4770. /* for malloc(), free() */
  4771. #include <stddef.h> /* size_t */
  4772. static void* XXH_malloc(size_t s) { return malloc(s); }
  4773. static void XXH_free (void* p) { free(p); }
  4774. /* for memcpy() */
  4775. static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  4776. #ifndef XXH_STATIC_LINKING_ONLY
  4777. # define XXH_STATIC_LINKING_ONLY
  4778. #endif
  4779. /**** skipping file: xxhash.h ****/
  4780. /* *************************************
  4781. * Compiler Specific Options
  4782. ***************************************/
  4783. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  4784. # define INLINE_KEYWORD inline
  4785. #else
  4786. # define INLINE_KEYWORD
  4787. #endif
  4788. #if defined(__GNUC__) || defined(__ICCARM__)
  4789. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  4790. #elif defined(_MSC_VER)
  4791. # define FORCE_INLINE_ATTR __forceinline
  4792. #else
  4793. # define FORCE_INLINE_ATTR
  4794. #endif
  4795. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  4796. #ifdef _MSC_VER
  4797. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  4798. #endif
  4799. /* *************************************
  4800. * Basic Types
  4801. ***************************************/
  4802. #ifndef MEM_MODULE
  4803. # define MEM_MODULE
  4804. # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  4805. # include <stdint.h>
  4806. typedef uint8_t BYTE;
  4807. typedef uint16_t U16;
  4808. typedef uint32_t U32;
  4809. typedef int32_t S32;
  4810. typedef uint64_t U64;
  4811. # else
  4812. typedef unsigned char BYTE;
  4813. typedef unsigned short U16;
  4814. typedef unsigned int U32;
  4815. typedef signed int S32;
  4816. typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */
  4817. # endif
  4818. #endif
  4819. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  4820. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  4821. static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
  4822. static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
  4823. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  4824. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  4825. /* currently only defined for gcc and icc */
  4826. typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign;
  4827. static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  4828. static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
  4829. #else
  4830. /* portable and safe solution. Generally efficient.
  4831. * see : http://stackoverflow.com/a/32095106/646947
  4832. */
  4833. static U32 XXH_read32(const void* memPtr)
  4834. {
  4835. U32 val;
  4836. memcpy(&val, memPtr, sizeof(val));
  4837. return val;
  4838. }
  4839. static U64 XXH_read64(const void* memPtr)
  4840. {
  4841. U64 val;
  4842. memcpy(&val, memPtr, sizeof(val));
  4843. return val;
  4844. }
  4845. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  4846. /* ****************************************
  4847. * Compiler-specific Functions and Macros
  4848. ******************************************/
  4849. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  4850. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  4851. #if defined(_MSC_VER)
  4852. # define XXH_rotl32(x,r) _rotl(x,r)
  4853. # define XXH_rotl64(x,r) _rotl64(x,r)
  4854. #else
  4855. #if defined(__ICCARM__)
  4856. # include <intrinsics.h>
  4857. # define XXH_rotl32(x,r) __ROR(x,(32 - r))
  4858. #else
  4859. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  4860. #endif
  4861. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  4862. #endif
  4863. #if defined(_MSC_VER) /* Visual Studio */
  4864. # define XXH_swap32 _byteswap_ulong
  4865. # define XXH_swap64 _byteswap_uint64
  4866. #elif GCC_VERSION >= 403
  4867. # define XXH_swap32 __builtin_bswap32
  4868. # define XXH_swap64 __builtin_bswap64
  4869. #else
  4870. static U32 XXH_swap32 (U32 x)
  4871. {
  4872. return ((x << 24) & 0xff000000 ) |
  4873. ((x << 8) & 0x00ff0000 ) |
  4874. ((x >> 8) & 0x0000ff00 ) |
  4875. ((x >> 24) & 0x000000ff );
  4876. }
  4877. static U64 XXH_swap64 (U64 x)
  4878. {
  4879. return ((x << 56) & 0xff00000000000000ULL) |
  4880. ((x << 40) & 0x00ff000000000000ULL) |
  4881. ((x << 24) & 0x0000ff0000000000ULL) |
  4882. ((x << 8) & 0x000000ff00000000ULL) |
  4883. ((x >> 8) & 0x00000000ff000000ULL) |
  4884. ((x >> 24) & 0x0000000000ff0000ULL) |
  4885. ((x >> 40) & 0x000000000000ff00ULL) |
  4886. ((x >> 56) & 0x00000000000000ffULL);
  4887. }
  4888. #endif
  4889. /* *************************************
  4890. * Architecture Macros
  4891. ***************************************/
  4892. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  4893. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  4894. #ifndef XXH_CPU_LITTLE_ENDIAN
  4895. static const int g_one = 1;
  4896. # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
  4897. #endif
  4898. /* ***************************
  4899. * Memory reads
  4900. *****************************/
  4901. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  4902. FORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  4903. {
  4904. if (align==XXH_unaligned)
  4905. return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  4906. else
  4907. return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
  4908. }
  4909. FORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
  4910. {
  4911. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  4912. }
  4913. static U32 XXH_readBE32(const void* ptr)
  4914. {
  4915. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  4916. }
  4917. FORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  4918. {
  4919. if (align==XXH_unaligned)
  4920. return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  4921. else
  4922. return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
  4923. }
  4924. FORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
  4925. {
  4926. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  4927. }
  4928. static U64 XXH_readBE64(const void* ptr)
  4929. {
  4930. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  4931. }
  4932. /* *************************************
  4933. * Macros
  4934. ***************************************/
  4935. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  4936. /* *************************************
  4937. * Constants
  4938. ***************************************/
  4939. static const U32 PRIME32_1 = 2654435761U;
  4940. static const U32 PRIME32_2 = 2246822519U;
  4941. static const U32 PRIME32_3 = 3266489917U;
  4942. static const U32 PRIME32_4 = 668265263U;
  4943. static const U32 PRIME32_5 = 374761393U;
  4944. static const U64 PRIME64_1 = 11400714785074694791ULL;
  4945. static const U64 PRIME64_2 = 14029467366897019727ULL;
  4946. static const U64 PRIME64_3 = 1609587929392839161ULL;
  4947. static const U64 PRIME64_4 = 9650029242287828579ULL;
  4948. static const U64 PRIME64_5 = 2870177450012600261ULL;
  4949. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  4950. /* **************************
  4951. * Utils
  4952. ****************************/
  4953. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)
  4954. {
  4955. memcpy(dstState, srcState, sizeof(*dstState));
  4956. }
  4957. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
  4958. {
  4959. memcpy(dstState, srcState, sizeof(*dstState));
  4960. }
  4961. /* ***************************
  4962. * Simple Hash Functions
  4963. *****************************/
  4964. static U32 XXH32_round(U32 seed, U32 input)
  4965. {
  4966. seed += input * PRIME32_2;
  4967. seed = XXH_rotl32(seed, 13);
  4968. seed *= PRIME32_1;
  4969. return seed;
  4970. }
  4971. FORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
  4972. {
  4973. const BYTE* p = (const BYTE*)input;
  4974. const BYTE* bEnd = p + len;
  4975. U32 h32;
  4976. #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
  4977. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  4978. if (p==NULL) {
  4979. len=0;
  4980. bEnd=p=(const BYTE*)(size_t)16;
  4981. }
  4982. #endif
  4983. if (len>=16) {
  4984. const BYTE* const limit = bEnd - 16;
  4985. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  4986. U32 v2 = seed + PRIME32_2;
  4987. U32 v3 = seed + 0;
  4988. U32 v4 = seed - PRIME32_1;
  4989. do {
  4990. v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
  4991. v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
  4992. v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
  4993. v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
  4994. } while (p<=limit);
  4995. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  4996. } else {
  4997. h32 = seed + PRIME32_5;
  4998. }
  4999. h32 += (U32) len;
  5000. while (p+4<=bEnd) {
  5001. h32 += XXH_get32bits(p) * PRIME32_3;
  5002. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  5003. p+=4;
  5004. }
  5005. while (p<bEnd) {
  5006. h32 += (*p) * PRIME32_5;
  5007. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  5008. p++;
  5009. }
  5010. h32 ^= h32 >> 15;
  5011. h32 *= PRIME32_2;
  5012. h32 ^= h32 >> 13;
  5013. h32 *= PRIME32_3;
  5014. h32 ^= h32 >> 16;
  5015. return h32;
  5016. }
  5017. XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)
  5018. {
  5019. #if 0
  5020. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  5021. XXH32_CREATESTATE_STATIC(state);
  5022. XXH32_reset(state, seed);
  5023. XXH32_update(state, input, len);
  5024. return XXH32_digest(state);
  5025. #else
  5026. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5027. if (XXH_FORCE_ALIGN_CHECK) {
  5028. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  5029. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5030. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  5031. else
  5032. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  5033. } }
  5034. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5035. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  5036. else
  5037. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  5038. #endif
  5039. }
  5040. static U64 XXH64_round(U64 acc, U64 input)
  5041. {
  5042. acc += input * PRIME64_2;
  5043. acc = XXH_rotl64(acc, 31);
  5044. acc *= PRIME64_1;
  5045. return acc;
  5046. }
  5047. static U64 XXH64_mergeRound(U64 acc, U64 val)
  5048. {
  5049. val = XXH64_round(0, val);
  5050. acc ^= val;
  5051. acc = acc * PRIME64_1 + PRIME64_4;
  5052. return acc;
  5053. }
  5054. FORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  5055. {
  5056. const BYTE* p = (const BYTE*)input;
  5057. const BYTE* const bEnd = p + len;
  5058. U64 h64;
  5059. #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
  5060. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  5061. if (p==NULL) {
  5062. len=0;
  5063. bEnd=p=(const BYTE*)(size_t)32;
  5064. }
  5065. #endif
  5066. if (len>=32) {
  5067. const BYTE* const limit = bEnd - 32;
  5068. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  5069. U64 v2 = seed + PRIME64_2;
  5070. U64 v3 = seed + 0;
  5071. U64 v4 = seed - PRIME64_1;
  5072. do {
  5073. v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
  5074. v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
  5075. v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
  5076. v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
  5077. } while (p<=limit);
  5078. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  5079. h64 = XXH64_mergeRound(h64, v1);
  5080. h64 = XXH64_mergeRound(h64, v2);
  5081. h64 = XXH64_mergeRound(h64, v3);
  5082. h64 = XXH64_mergeRound(h64, v4);
  5083. } else {
  5084. h64 = seed + PRIME64_5;
  5085. }
  5086. h64 += (U64) len;
  5087. while (p+8<=bEnd) {
  5088. U64 const k1 = XXH64_round(0, XXH_get64bits(p));
  5089. h64 ^= k1;
  5090. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  5091. p+=8;
  5092. }
  5093. if (p+4<=bEnd) {
  5094. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  5095. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  5096. p+=4;
  5097. }
  5098. while (p<bEnd) {
  5099. h64 ^= (*p) * PRIME64_5;
  5100. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  5101. p++;
  5102. }
  5103. h64 ^= h64 >> 33;
  5104. h64 *= PRIME64_2;
  5105. h64 ^= h64 >> 29;
  5106. h64 *= PRIME64_3;
  5107. h64 ^= h64 >> 32;
  5108. return h64;
  5109. }
  5110. XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  5111. {
  5112. #if 0
  5113. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  5114. XXH64_CREATESTATE_STATIC(state);
  5115. XXH64_reset(state, seed);
  5116. XXH64_update(state, input, len);
  5117. return XXH64_digest(state);
  5118. #else
  5119. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5120. if (XXH_FORCE_ALIGN_CHECK) {
  5121. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  5122. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5123. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  5124. else
  5125. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  5126. } }
  5127. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5128. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  5129. else
  5130. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  5131. #endif
  5132. }
  5133. /* **************************************************
  5134. * Advanced Hash Functions
  5135. ****************************************************/
  5136. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  5137. {
  5138. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  5139. }
  5140. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  5141. {
  5142. XXH_free(statePtr);
  5143. return XXH_OK;
  5144. }
  5145. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  5146. {
  5147. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  5148. }
  5149. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  5150. {
  5151. XXH_free(statePtr);
  5152. return XXH_OK;
  5153. }
  5154. /*** Hash feed ***/
  5155. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
  5156. {
  5157. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  5158. memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
  5159. state.v1 = seed + PRIME32_1 + PRIME32_2;
  5160. state.v2 = seed + PRIME32_2;
  5161. state.v3 = seed + 0;
  5162. state.v4 = seed - PRIME32_1;
  5163. memcpy(statePtr, &state, sizeof(state));
  5164. return XXH_OK;
  5165. }
  5166. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
  5167. {
  5168. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  5169. memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
  5170. state.v1 = seed + PRIME64_1 + PRIME64_2;
  5171. state.v2 = seed + PRIME64_2;
  5172. state.v3 = seed + 0;
  5173. state.v4 = seed - PRIME64_1;
  5174. memcpy(statePtr, &state, sizeof(state));
  5175. return XXH_OK;
  5176. }
  5177. FORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)
  5178. {
  5179. const BYTE* p = (const BYTE*)input;
  5180. const BYTE* const bEnd = p + len;
  5181. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  5182. if (input==NULL) return XXH_ERROR;
  5183. #endif
  5184. state->total_len_32 += (unsigned)len;
  5185. state->large_len |= (len>=16) | (state->total_len_32>=16);
  5186. if (state->memsize + len < 16) { /* fill in tmp buffer */
  5187. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
  5188. state->memsize += (unsigned)len;
  5189. return XXH_OK;
  5190. }
  5191. if (state->memsize) { /* some data left from previous update */
  5192. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
  5193. { const U32* p32 = state->mem32;
  5194. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
  5195. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
  5196. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
  5197. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;
  5198. }
  5199. p += 16-state->memsize;
  5200. state->memsize = 0;
  5201. }
  5202. if (p <= bEnd-16) {
  5203. const BYTE* const limit = bEnd - 16;
  5204. U32 v1 = state->v1;
  5205. U32 v2 = state->v2;
  5206. U32 v3 = state->v3;
  5207. U32 v4 = state->v4;
  5208. do {
  5209. v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
  5210. v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
  5211. v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
  5212. v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
  5213. } while (p<=limit);
  5214. state->v1 = v1;
  5215. state->v2 = v2;
  5216. state->v3 = v3;
  5217. state->v4 = v4;
  5218. }
  5219. if (p < bEnd) {
  5220. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  5221. state->memsize = (unsigned)(bEnd-p);
  5222. }
  5223. return XXH_OK;
  5224. }
  5225. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  5226. {
  5227. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5228. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5229. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  5230. else
  5231. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  5232. }
  5233. FORCE_INLINE_TEMPLATE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)
  5234. {
  5235. const BYTE * p = (const BYTE*)state->mem32;
  5236. const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;
  5237. U32 h32;
  5238. if (state->large_len) {
  5239. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  5240. } else {
  5241. h32 = state->v3 /* == seed */ + PRIME32_5;
  5242. }
  5243. h32 += state->total_len_32;
  5244. while (p+4<=bEnd) {
  5245. h32 += XXH_readLE32(p, endian) * PRIME32_3;
  5246. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  5247. p+=4;
  5248. }
  5249. while (p<bEnd) {
  5250. h32 += (*p) * PRIME32_5;
  5251. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  5252. p++;
  5253. }
  5254. h32 ^= h32 >> 15;
  5255. h32 *= PRIME32_2;
  5256. h32 ^= h32 >> 13;
  5257. h32 *= PRIME32_3;
  5258. h32 ^= h32 >> 16;
  5259. return h32;
  5260. }
  5261. XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
  5262. {
  5263. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5264. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5265. return XXH32_digest_endian(state_in, XXH_littleEndian);
  5266. else
  5267. return XXH32_digest_endian(state_in, XXH_bigEndian);
  5268. }
  5269. /* **** XXH64 **** */
  5270. FORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
  5271. {
  5272. const BYTE* p = (const BYTE*)input;
  5273. const BYTE* const bEnd = p + len;
  5274. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  5275. if (input==NULL) return XXH_ERROR;
  5276. #endif
  5277. state->total_len += len;
  5278. if (state->memsize + len < 32) { /* fill in tmp buffer */
  5279. if (input != NULL) {
  5280. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
  5281. }
  5282. state->memsize += (U32)len;
  5283. return XXH_OK;
  5284. }
  5285. if (state->memsize) { /* tmp buffer is full */
  5286. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
  5287. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
  5288. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
  5289. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
  5290. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
  5291. p += 32-state->memsize;
  5292. state->memsize = 0;
  5293. }
  5294. if (p+32 <= bEnd) {
  5295. const BYTE* const limit = bEnd - 32;
  5296. U64 v1 = state->v1;
  5297. U64 v2 = state->v2;
  5298. U64 v3 = state->v3;
  5299. U64 v4 = state->v4;
  5300. do {
  5301. v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
  5302. v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
  5303. v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
  5304. v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
  5305. } while (p<=limit);
  5306. state->v1 = v1;
  5307. state->v2 = v2;
  5308. state->v3 = v3;
  5309. state->v4 = v4;
  5310. }
  5311. if (p < bEnd) {
  5312. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  5313. state->memsize = (unsigned)(bEnd-p);
  5314. }
  5315. return XXH_OK;
  5316. }
  5317. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  5318. {
  5319. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5320. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5321. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  5322. else
  5323. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  5324. }
  5325. FORCE_INLINE_TEMPLATE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
  5326. {
  5327. const BYTE * p = (const BYTE*)state->mem64;
  5328. const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
  5329. U64 h64;
  5330. if (state->total_len >= 32) {
  5331. U64 const v1 = state->v1;
  5332. U64 const v2 = state->v2;
  5333. U64 const v3 = state->v3;
  5334. U64 const v4 = state->v4;
  5335. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  5336. h64 = XXH64_mergeRound(h64, v1);
  5337. h64 = XXH64_mergeRound(h64, v2);
  5338. h64 = XXH64_mergeRound(h64, v3);
  5339. h64 = XXH64_mergeRound(h64, v4);
  5340. } else {
  5341. h64 = state->v3 + PRIME64_5;
  5342. }
  5343. h64 += (U64) state->total_len;
  5344. while (p+8<=bEnd) {
  5345. U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
  5346. h64 ^= k1;
  5347. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  5348. p+=8;
  5349. }
  5350. if (p+4<=bEnd) {
  5351. h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
  5352. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  5353. p+=4;
  5354. }
  5355. while (p<bEnd) {
  5356. h64 ^= (*p) * PRIME64_5;
  5357. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  5358. p++;
  5359. }
  5360. h64 ^= h64 >> 33;
  5361. h64 *= PRIME64_2;
  5362. h64 ^= h64 >> 29;
  5363. h64 *= PRIME64_3;
  5364. h64 ^= h64 >> 32;
  5365. return h64;
  5366. }
  5367. XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  5368. {
  5369. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  5370. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  5371. return XXH64_digest_endian(state_in, XXH_littleEndian);
  5372. else
  5373. return XXH64_digest_endian(state_in, XXH_bigEndian);
  5374. }
  5375. /* **************************
  5376. * Canonical representation
  5377. ****************************/
  5378. /*! Default XXH result types are basic unsigned 32 and 64 bits.
  5379. * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
  5380. * These functions allow transformation of hash result into and from its canonical format.
  5381. * This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs.
  5382. */
  5383. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  5384. {
  5385. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  5386. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  5387. memcpy(dst, &hash, sizeof(*dst));
  5388. }
  5389. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  5390. {
  5391. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  5392. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  5393. memcpy(dst, &hash, sizeof(*dst));
  5394. }
  5395. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  5396. {
  5397. return XXH_readBE32(src);
  5398. }
  5399. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  5400. {
  5401. return XXH_readBE64(src);
  5402. }
  5403. /**** ended inlining xxhash.c ****/
  5404. # endif
  5405. #endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */
  5406. #if defined (__cplusplus)
  5407. }
  5408. #endif
  5409. /**** ended inlining xxhash.h ****/
  5410. #if defined (__cplusplus)
  5411. extern "C" {
  5412. #endif
  5413. /* ---- static assert (debug) --- */
  5414. #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
  5415. #define ZSTD_isError ERR_isError /* for inlining */
  5416. #define FSE_isError ERR_isError
  5417. #define HUF_isError ERR_isError
  5418. /*-*************************************
  5419. * shared macros
  5420. ***************************************/
  5421. #undef MIN
  5422. #undef MAX
  5423. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  5424. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  5425. /**
  5426. * Ignore: this is an internal helper.
  5427. *
  5428. * This is a helper function to help force C99-correctness during compilation.
  5429. * Under strict compilation modes, variadic macro arguments can't be empty.
  5430. * However, variadic function arguments can be. Using a function therefore lets
  5431. * us statically check that at least one (string) argument was passed,
  5432. * independent of the compilation flags.
  5433. */
  5434. static INLINE_KEYWORD UNUSED_ATTR
  5435. void _force_has_format_string(const char *format, ...) {
  5436. (void)format;
  5437. }
  5438. /**
  5439. * Ignore: this is an internal helper.
  5440. *
  5441. * We want to force this function invocation to be syntactically correct, but
  5442. * we don't want to force runtime evaluation of its arguments.
  5443. */
  5444. #define _FORCE_HAS_FORMAT_STRING(...) \
  5445. if (0) { \
  5446. _force_has_format_string(__VA_ARGS__); \
  5447. }
  5448. /**
  5449. * Return the specified error if the condition evaluates to true.
  5450. *
  5451. * In debug modes, prints additional information.
  5452. * In order to do that (particularly, printing the conditional that failed),
  5453. * this can't just wrap RETURN_ERROR().
  5454. */
  5455. #define RETURN_ERROR_IF(cond, err, ...) \
  5456. if (cond) { \
  5457. RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
  5458. __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
  5459. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  5460. RAWLOG(3, ": " __VA_ARGS__); \
  5461. RAWLOG(3, "\n"); \
  5462. return ERROR(err); \
  5463. }
  5464. /**
  5465. * Unconditionally return the specified error.
  5466. *
  5467. * In debug modes, prints additional information.
  5468. */
  5469. #define RETURN_ERROR(err, ...) \
  5470. do { \
  5471. RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
  5472. __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
  5473. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  5474. RAWLOG(3, ": " __VA_ARGS__); \
  5475. RAWLOG(3, "\n"); \
  5476. return ERROR(err); \
  5477. } while(0);
  5478. /**
  5479. * If the provided expression evaluates to an error code, returns that error code.
  5480. *
  5481. * In debug modes, prints additional information.
  5482. */
  5483. #define FORWARD_IF_ERROR(err, ...) \
  5484. do { \
  5485. size_t const err_code = (err); \
  5486. if (ERR_isError(err_code)) { \
  5487. RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
  5488. __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
  5489. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  5490. RAWLOG(3, ": " __VA_ARGS__); \
  5491. RAWLOG(3, "\n"); \
  5492. return err_code; \
  5493. } \
  5494. } while(0);
  5495. /*-*************************************
  5496. * Common constants
  5497. ***************************************/
  5498. #define ZSTD_OPT_NUM (1<<12)
  5499. #define ZSTD_REP_NUM 3 /* number of repcodes */
  5500. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  5501. static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  5502. #define KB *(1 <<10)
  5503. #define MB *(1 <<20)
  5504. #define GB *(1U<<30)
  5505. #define BIT7 128
  5506. #define BIT6 64
  5507. #define BIT5 32
  5508. #define BIT4 16
  5509. #define BIT1 2
  5510. #define BIT0 1
  5511. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  5512. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  5513. static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  5514. #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
  5515. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  5516. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  5517. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  5518. #define ZSTD_FRAMECHECKSUMSIZE 4
  5519. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  5520. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  5521. #define HufLog 12
  5522. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  5523. #define LONGNBSEQ 0x7F00
  5524. #define MINMATCH 3
  5525. #define Litbits 8
  5526. #define MaxLit ((1<<Litbits) - 1)
  5527. #define MaxML 52
  5528. #define MaxLL 35
  5529. #define DefaultMaxOff 28
  5530. #define MaxOff 31
  5531. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  5532. #define MLFSELog 9
  5533. #define LLFSELog 9
  5534. #define OffFSELog 8
  5535. #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
  5536. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  5537. 0, 0, 0, 0, 0, 0, 0, 0,
  5538. 1, 1, 1, 1, 2, 2, 3, 3,
  5539. 4, 6, 7, 8, 9,10,11,12,
  5540. 13,14,15,16 };
  5541. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
  5542. 2, 2, 2, 2, 2, 1, 1, 1,
  5543. 2, 2, 2, 2, 2, 2, 2, 2,
  5544. 2, 3, 2, 1, 1, 1, 1, 1,
  5545. -1,-1,-1,-1 };
  5546. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  5547. static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  5548. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  5549. 0, 0, 0, 0, 0, 0, 0, 0,
  5550. 0, 0, 0, 0, 0, 0, 0, 0,
  5551. 0, 0, 0, 0, 0, 0, 0, 0,
  5552. 1, 1, 1, 1, 2, 2, 3, 3,
  5553. 4, 4, 5, 7, 8, 9,10,11,
  5554. 12,13,14,15,16 };
  5555. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
  5556. 2, 1, 1, 1, 1, 1, 1, 1,
  5557. 1, 1, 1, 1, 1, 1, 1, 1,
  5558. 1, 1, 1, 1, 1, 1, 1, 1,
  5559. 1, 1, 1, 1, 1, 1, 1, 1,
  5560. 1, 1, 1, 1, 1, 1,-1,-1,
  5561. -1,-1,-1,-1,-1 };
  5562. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  5563. static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  5564. static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
  5565. 2, 1, 1, 1, 1, 1, 1, 1,
  5566. 1, 1, 1, 1, 1, 1, 1, 1,
  5567. -1,-1,-1,-1,-1 };
  5568. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  5569. static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  5570. /*-*******************************************
  5571. * Shared functions to include for inlining
  5572. *********************************************/
  5573. static void ZSTD_copy8(void* dst, const void* src) {
  5574. #ifdef __aarch64__
  5575. vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
  5576. #else
  5577. memcpy(dst, src, 8);
  5578. #endif
  5579. }
  5580. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  5581. static void ZSTD_copy16(void* dst, const void* src) {
  5582. #ifdef __aarch64__
  5583. vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
  5584. #else
  5585. memcpy(dst, src, 16);
  5586. #endif
  5587. }
  5588. #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
  5589. #define WILDCOPY_OVERLENGTH 32
  5590. #define WILDCOPY_VECLEN 16
  5591. typedef enum {
  5592. ZSTD_no_overlap,
  5593. ZSTD_overlap_src_before_dst
  5594. /* ZSTD_overlap_dst_before_src, */
  5595. } ZSTD_overlap_e;
  5596. /*! ZSTD_wildcopy() :
  5597. * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
  5598. * @param ovtype controls the overlap detection
  5599. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  5600. * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
  5601. * The src buffer must be before the dst buffer.
  5602. */
  5603. MEM_STATIC FORCE_INLINE_ATTR
  5604. void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
  5605. {
  5606. ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
  5607. const BYTE* ip = (const BYTE*)src;
  5608. BYTE* op = (BYTE*)dst;
  5609. BYTE* const oend = op + length;
  5610. assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
  5611. if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
  5612. /* Handle short offset copies. */
  5613. do {
  5614. COPY8(op, ip)
  5615. } while (op < oend);
  5616. } else {
  5617. assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
  5618. /* Separate out the first COPY16() call because the copy length is
  5619. * almost certain to be short, so the branches have different
  5620. * probabilities. Since it is almost certain to be short, only do
  5621. * one COPY16() in the first call. Then, do two calls per loop since
  5622. * at that point it is more likely to have a high trip count.
  5623. */
  5624. #ifndef __aarch64__
  5625. do {
  5626. COPY16(op, ip);
  5627. }
  5628. while (op < oend);
  5629. #else
  5630. COPY16(op, ip);
  5631. if (op >= oend) return;
  5632. do {
  5633. COPY16(op, ip);
  5634. COPY16(op, ip);
  5635. }
  5636. while (op < oend);
  5637. #endif
  5638. }
  5639. }
  5640. MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  5641. {
  5642. size_t const length = MIN(dstCapacity, srcSize);
  5643. if (length > 0) {
  5644. memcpy(dst, src, length);
  5645. }
  5646. return length;
  5647. }
  5648. /* define "workspace is too large" as this number of times larger than needed */
  5649. #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
  5650. /* when workspace is continuously too large
  5651. * during at least this number of times,
  5652. * context's memory usage is considered wasteful,
  5653. * because it's sized to handle a worst case scenario which rarely happens.
  5654. * In which case, resize it down to free some memory */
  5655. #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
  5656. /*-*******************************************
  5657. * Private declarations
  5658. *********************************************/
  5659. typedef struct seqDef_s {
  5660. U32 offset;
  5661. U16 litLength;
  5662. U16 matchLength;
  5663. } seqDef;
  5664. typedef struct {
  5665. seqDef* sequencesStart;
  5666. seqDef* sequences;
  5667. BYTE* litStart;
  5668. BYTE* lit;
  5669. BYTE* llCode;
  5670. BYTE* mlCode;
  5671. BYTE* ofCode;
  5672. size_t maxNbSeq;
  5673. size_t maxNbLit;
  5674. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  5675. U32 longLengthPos;
  5676. } seqStore_t;
  5677. typedef struct {
  5678. U32 litLength;
  5679. U32 matchLength;
  5680. } ZSTD_sequenceLength;
  5681. /**
  5682. * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
  5683. * indicated by longLengthPos and longLengthID, and adds MINMATCH back to matchLength.
  5684. */
  5685. MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
  5686. {
  5687. ZSTD_sequenceLength seqLen;
  5688. seqLen.litLength = seq->litLength;
  5689. seqLen.matchLength = seq->matchLength + MINMATCH;
  5690. if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
  5691. if (seqStore->longLengthID == 1) {
  5692. seqLen.litLength += 0xFFFF;
  5693. }
  5694. if (seqStore->longLengthID == 2) {
  5695. seqLen.matchLength += 0xFFFF;
  5696. }
  5697. }
  5698. return seqLen;
  5699. }
  5700. /**
  5701. * Contains the compressed frame size and an upper-bound for the decompressed frame size.
  5702. * Note: before using `compressedSize`, check for errors using ZSTD_isError().
  5703. * similarly, before using `decompressedBound`, check for errors using:
  5704. * `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
  5705. */
  5706. typedef struct {
  5707. size_t compressedSize;
  5708. unsigned long long decompressedBound;
  5709. } ZSTD_frameSizeInfo; /* decompress & legacy */
  5710. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
  5711. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
  5712. /* custom memory allocation functions */
  5713. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  5714. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
  5715. void ZSTD_free(void* ptr, ZSTD_customMem customMem);
  5716. MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
  5717. {
  5718. assert(val != 0);
  5719. {
  5720. # if defined(_MSC_VER) /* Visual */
  5721. unsigned long r=0;
  5722. return _BitScanReverse(&r, val) ? (unsigned)r : 0;
  5723. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  5724. return __builtin_clz (val) ^ 31;
  5725. # elif defined(__ICCARM__) /* IAR Intrinsic */
  5726. return 31 - __CLZ(val);
  5727. # else /* Software version */
  5728. static const U32 DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
  5729. U32 v = val;
  5730. v |= v >> 1;
  5731. v |= v >> 2;
  5732. v |= v >> 4;
  5733. v |= v >> 8;
  5734. v |= v >> 16;
  5735. return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
  5736. # endif
  5737. }
  5738. }
  5739. /* ZSTD_invalidateRepCodes() :
  5740. * ensures next compression will not use repcodes from previous block.
  5741. * Note : only works with regular variant;
  5742. * do not use with extDict variant ! */
  5743. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
  5744. typedef struct {
  5745. blockType_e blockType;
  5746. U32 lastBlock;
  5747. U32 origSize;
  5748. } blockProperties_t; /* declared here for decompress and fullbench */
  5749. /*! ZSTD_getcBlockSize() :
  5750. * Provides the size of compressed block from block header `src` */
  5751. /* Used by: decompress, fullbench (does not get its definition from here) */
  5752. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  5753. blockProperties_t* bpPtr);
  5754. /*! ZSTD_decodeSeqHeaders() :
  5755. * decode sequence header from src */
  5756. /* Used by: decompress, fullbench (does not get its definition from here) */
  5757. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  5758. const void* src, size_t srcSize);
  5759. #if defined (__cplusplus)
  5760. }
  5761. #endif
  5762. #endif /* ZSTD_CCOMMON_H_MODULE */
  5763. /**** ended inlining zstd_internal.h ****/
  5764. /*-****************************************
  5765. * Version
  5766. ******************************************/
  5767. unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
  5768. const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
  5769. /*-****************************************
  5770. * ZSTD Error Management
  5771. ******************************************/
  5772. #undef ZSTD_isError /* defined within zstd_internal.h */
  5773. /*! ZSTD_isError() :
  5774. * tells if a return value is an error code
  5775. * symbol is required for external callers */
  5776. unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
  5777. /*! ZSTD_getErrorName() :
  5778. * provides error code string from function result (useful for debugging) */
  5779. const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
  5780. /*! ZSTD_getError() :
  5781. * convert a `size_t` function result into a proper ZSTD_errorCode enum */
  5782. ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
  5783. /*! ZSTD_getErrorString() :
  5784. * provides error code string from enum */
  5785. const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
  5786. /*=**************************************************************
  5787. * Custom allocator
  5788. ****************************************************************/
  5789. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
  5790. {
  5791. if (customMem.customAlloc)
  5792. return customMem.customAlloc(customMem.opaque, size);
  5793. return malloc(size);
  5794. }
  5795. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
  5796. {
  5797. if (customMem.customAlloc) {
  5798. /* calloc implemented as malloc+memset;
  5799. * not as efficient as calloc, but next best guess for custom malloc */
  5800. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  5801. memset(ptr, 0, size);
  5802. return ptr;
  5803. }
  5804. return calloc(1, size);
  5805. }
  5806. void ZSTD_free(void* ptr, ZSTD_customMem customMem)
  5807. {
  5808. if (ptr!=NULL) {
  5809. if (customMem.customFree)
  5810. customMem.customFree(customMem.opaque, ptr);
  5811. else
  5812. free(ptr);
  5813. }
  5814. }
  5815. /**** ended inlining common/zstd_common.c ****/
  5816. /**** start inlining decompress/huf_decompress.c ****/
  5817. /* ******************************************************************
  5818. * huff0 huffman decoder,
  5819. * part of Finite State Entropy library
  5820. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  5821. *
  5822. * You can contact the author at :
  5823. * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
  5824. *
  5825. * This source code is licensed under both the BSD-style license (found in the
  5826. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  5827. * in the COPYING file in the root directory of this source tree).
  5828. * You may select, at your option, one of the above-listed licenses.
  5829. ****************************************************************** */
  5830. /* **************************************************************
  5831. * Dependencies
  5832. ****************************************************************/
  5833. /**** skipping file: ../common/compiler.h ****/
  5834. /**** skipping file: ../common/bitstream.h ****/
  5835. /**** skipping file: ../common/fse.h ****/
  5836. #define HUF_STATIC_LINKING_ONLY
  5837. /**** skipping file: ../common/huf.h ****/
  5838. /**** skipping file: ../common/error_private.h ****/
  5839. /* **************************************************************
  5840. * Macros
  5841. ****************************************************************/
  5842. /* These two optional macros force the use one way or another of the two
  5843. * Huffman decompression implementations. You can't force in both directions
  5844. * at the same time.
  5845. */
  5846. #if defined(HUF_FORCE_DECOMPRESS_X1) && \
  5847. defined(HUF_FORCE_DECOMPRESS_X2)
  5848. #error "Cannot force the use of the X1 and X2 decoders at the same time!"
  5849. #endif
  5850. /* **************************************************************
  5851. * Error Management
  5852. ****************************************************************/
  5853. #define HUF_isError ERR_isError
  5854. /* **************************************************************
  5855. * Byte alignment for workSpace management
  5856. ****************************************************************/
  5857. #define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1)
  5858. #define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  5859. /* **************************************************************
  5860. * BMI2 Variant Wrappers
  5861. ****************************************************************/
  5862. #if DYNAMIC_BMI2
  5863. #define HUF_DGEN(fn) \
  5864. \
  5865. static size_t fn##_default( \
  5866. void* dst, size_t dstSize, \
  5867. const void* cSrc, size_t cSrcSize, \
  5868. const HUF_DTable* DTable) \
  5869. { \
  5870. return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
  5871. } \
  5872. \
  5873. static TARGET_ATTRIBUTE("bmi2") size_t fn##_bmi2( \
  5874. void* dst, size_t dstSize, \
  5875. const void* cSrc, size_t cSrcSize, \
  5876. const HUF_DTable* DTable) \
  5877. { \
  5878. return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
  5879. } \
  5880. \
  5881. static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
  5882. size_t cSrcSize, HUF_DTable const* DTable, int bmi2) \
  5883. { \
  5884. if (bmi2) { \
  5885. return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); \
  5886. } \
  5887. return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \
  5888. }
  5889. #else
  5890. #define HUF_DGEN(fn) \
  5891. static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
  5892. size_t cSrcSize, HUF_DTable const* DTable, int bmi2) \
  5893. { \
  5894. (void)bmi2; \
  5895. return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
  5896. }
  5897. #endif
  5898. /*-***************************/
  5899. /* generic DTableDesc */
  5900. /*-***************************/
  5901. typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;
  5902. static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
  5903. {
  5904. DTableDesc dtd;
  5905. memcpy(&dtd, table, sizeof(dtd));
  5906. return dtd;
  5907. }
  5908. #ifndef HUF_FORCE_DECOMPRESS_X2
  5909. /*-***************************/
  5910. /* single-symbol decoding */
  5911. /*-***************************/
  5912. typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX1; /* single-symbol decoding */
  5913. size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)
  5914. {
  5915. U32 tableLog = 0;
  5916. U32 nbSymbols = 0;
  5917. size_t iSize;
  5918. void* const dtPtr = DTable + 1;
  5919. HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;
  5920. U32* rankVal;
  5921. BYTE* huffWeight;
  5922. size_t spaceUsed32 = 0;
  5923. rankVal = (U32 *)workSpace + spaceUsed32;
  5924. spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
  5925. huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);
  5926. spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
  5927. if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
  5928. DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
  5929. /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
  5930. iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
  5931. if (HUF_isError(iSize)) return iSize;
  5932. /* Table header */
  5933. { DTableDesc dtd = HUF_getDTableDesc(DTable);
  5934. if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
  5935. dtd.tableType = 0;
  5936. dtd.tableLog = (BYTE)tableLog;
  5937. memcpy(DTable, &dtd, sizeof(dtd));
  5938. }
  5939. /* Calculate starting value for each rank */
  5940. { U32 n, nextRankStart = 0;
  5941. for (n=1; n<tableLog+1; n++) {
  5942. U32 const current = nextRankStart;
  5943. nextRankStart += (rankVal[n] << (n-1));
  5944. rankVal[n] = current;
  5945. } }
  5946. /* fill DTable */
  5947. { U32 n;
  5948. size_t const nEnd = nbSymbols;
  5949. for (n=0; n<nEnd; n++) {
  5950. size_t const w = huffWeight[n];
  5951. size_t const length = (1 << w) >> 1;
  5952. size_t const uStart = rankVal[w];
  5953. size_t const uEnd = uStart + length;
  5954. size_t u;
  5955. HUF_DEltX1 D;
  5956. D.byte = (BYTE)n;
  5957. D.nbBits = (BYTE)(tableLog + 1 - w);
  5958. rankVal[w] = (U32)uEnd;
  5959. if (length < 4) {
  5960. /* Use length in the loop bound so the compiler knows it is short. */
  5961. for (u = 0; u < length; ++u)
  5962. dt[uStart + u] = D;
  5963. } else {
  5964. /* Unroll the loop 4 times, we know it is a power of 2. */
  5965. for (u = uStart; u < uEnd; u += 4) {
  5966. dt[u + 0] = D;
  5967. dt[u + 1] = D;
  5968. dt[u + 2] = D;
  5969. dt[u + 3] = D;
  5970. } } } }
  5971. return iSize;
  5972. }
  5973. size_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize)
  5974. {
  5975. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  5976. return HUF_readDTableX1_wksp(DTable, src, srcSize,
  5977. workSpace, sizeof(workSpace));
  5978. }
  5979. FORCE_INLINE_TEMPLATE BYTE
  5980. HUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, const U32 dtLog)
  5981. {
  5982. size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
  5983. BYTE const c = dt[val].byte;
  5984. BIT_skipBits(Dstream, dt[val].nbBits);
  5985. return c;
  5986. }
  5987. #define HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) \
  5988. *ptr++ = HUF_decodeSymbolX1(DStreamPtr, dt, dtLog)
  5989. #define HUF_DECODE_SYMBOLX1_1(ptr, DStreamPtr) \
  5990. if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
  5991. HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)
  5992. #define HUF_DECODE_SYMBOLX1_2(ptr, DStreamPtr) \
  5993. if (MEM_64bits()) \
  5994. HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)
  5995. HINT_INLINE size_t
  5996. HUF_decodeStreamX1(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX1* const dt, const U32 dtLog)
  5997. {
  5998. BYTE* const pStart = p;
  5999. /* up to 4 symbols at a time */
  6000. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-3)) {
  6001. HUF_DECODE_SYMBOLX1_2(p, bitDPtr);
  6002. HUF_DECODE_SYMBOLX1_1(p, bitDPtr);
  6003. HUF_DECODE_SYMBOLX1_2(p, bitDPtr);
  6004. HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
  6005. }
  6006. /* [0-3] symbols remaining */
  6007. if (MEM_32bits())
  6008. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd))
  6009. HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
  6010. /* no more data to retrieve from bitstream, no need to reload */
  6011. while (p < pEnd)
  6012. HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
  6013. return pEnd-pStart;
  6014. }
  6015. FORCE_INLINE_TEMPLATE size_t
  6016. HUF_decompress1X1_usingDTable_internal_body(
  6017. void* dst, size_t dstSize,
  6018. const void* cSrc, size_t cSrcSize,
  6019. const HUF_DTable* DTable)
  6020. {
  6021. BYTE* op = (BYTE*)dst;
  6022. BYTE* const oend = op + dstSize;
  6023. const void* dtPtr = DTable + 1;
  6024. const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;
  6025. BIT_DStream_t bitD;
  6026. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6027. U32 const dtLog = dtd.tableLog;
  6028. CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
  6029. HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog);
  6030. if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
  6031. return dstSize;
  6032. }
  6033. FORCE_INLINE_TEMPLATE size_t
  6034. HUF_decompress4X1_usingDTable_internal_body(
  6035. void* dst, size_t dstSize,
  6036. const void* cSrc, size_t cSrcSize,
  6037. const HUF_DTable* DTable)
  6038. {
  6039. /* Check */
  6040. if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
  6041. { const BYTE* const istart = (const BYTE*) cSrc;
  6042. BYTE* const ostart = (BYTE*) dst;
  6043. BYTE* const oend = ostart + dstSize;
  6044. BYTE* const olimit = oend - 3;
  6045. const void* const dtPtr = DTable + 1;
  6046. const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;
  6047. /* Init */
  6048. BIT_DStream_t bitD1;
  6049. BIT_DStream_t bitD2;
  6050. BIT_DStream_t bitD3;
  6051. BIT_DStream_t bitD4;
  6052. size_t const length1 = MEM_readLE16(istart);
  6053. size_t const length2 = MEM_readLE16(istart+2);
  6054. size_t const length3 = MEM_readLE16(istart+4);
  6055. size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
  6056. const BYTE* const istart1 = istart + 6; /* jumpTable */
  6057. const BYTE* const istart2 = istart1 + length1;
  6058. const BYTE* const istart3 = istart2 + length2;
  6059. const BYTE* const istart4 = istart3 + length3;
  6060. const size_t segmentSize = (dstSize+3) / 4;
  6061. BYTE* const opStart2 = ostart + segmentSize;
  6062. BYTE* const opStart3 = opStart2 + segmentSize;
  6063. BYTE* const opStart4 = opStart3 + segmentSize;
  6064. BYTE* op1 = ostart;
  6065. BYTE* op2 = opStart2;
  6066. BYTE* op3 = opStart3;
  6067. BYTE* op4 = opStart4;
  6068. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6069. U32 const dtLog = dtd.tableLog;
  6070. U32 endSignal = 1;
  6071. if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
  6072. CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
  6073. CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
  6074. CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
  6075. CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
  6076. /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */
  6077. for ( ; (endSignal) & (op4 < olimit) ; ) {
  6078. HUF_DECODE_SYMBOLX1_2(op1, &bitD1);
  6079. HUF_DECODE_SYMBOLX1_2(op2, &bitD2);
  6080. HUF_DECODE_SYMBOLX1_2(op3, &bitD3);
  6081. HUF_DECODE_SYMBOLX1_2(op4, &bitD4);
  6082. HUF_DECODE_SYMBOLX1_1(op1, &bitD1);
  6083. HUF_DECODE_SYMBOLX1_1(op2, &bitD2);
  6084. HUF_DECODE_SYMBOLX1_1(op3, &bitD3);
  6085. HUF_DECODE_SYMBOLX1_1(op4, &bitD4);
  6086. HUF_DECODE_SYMBOLX1_2(op1, &bitD1);
  6087. HUF_DECODE_SYMBOLX1_2(op2, &bitD2);
  6088. HUF_DECODE_SYMBOLX1_2(op3, &bitD3);
  6089. HUF_DECODE_SYMBOLX1_2(op4, &bitD4);
  6090. HUF_DECODE_SYMBOLX1_0(op1, &bitD1);
  6091. HUF_DECODE_SYMBOLX1_0(op2, &bitD2);
  6092. HUF_DECODE_SYMBOLX1_0(op3, &bitD3);
  6093. HUF_DECODE_SYMBOLX1_0(op4, &bitD4);
  6094. endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;
  6095. endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;
  6096. endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;
  6097. endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;
  6098. }
  6099. /* check corruption */
  6100. /* note : should not be necessary : op# advance in lock step, and we control op4.
  6101. * but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 test is present */
  6102. if (op1 > opStart2) return ERROR(corruption_detected);
  6103. if (op2 > opStart3) return ERROR(corruption_detected);
  6104. if (op3 > opStart4) return ERROR(corruption_detected);
  6105. /* note : op4 supposed already verified within main loop */
  6106. /* finish bitStreams one by one */
  6107. HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog);
  6108. HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog);
  6109. HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog);
  6110. HUF_decodeStreamX1(op4, &bitD4, oend, dt, dtLog);
  6111. /* check */
  6112. { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
  6113. if (!endCheck) return ERROR(corruption_detected); }
  6114. /* decoded size */
  6115. return dstSize;
  6116. }
  6117. }
  6118. typedef size_t (*HUF_decompress_usingDTable_t)(void *dst, size_t dstSize,
  6119. const void *cSrc,
  6120. size_t cSrcSize,
  6121. const HUF_DTable *DTable);
  6122. HUF_DGEN(HUF_decompress1X1_usingDTable_internal)
  6123. HUF_DGEN(HUF_decompress4X1_usingDTable_internal)
  6124. size_t HUF_decompress1X1_usingDTable(
  6125. void* dst, size_t dstSize,
  6126. const void* cSrc, size_t cSrcSize,
  6127. const HUF_DTable* DTable)
  6128. {
  6129. DTableDesc dtd = HUF_getDTableDesc(DTable);
  6130. if (dtd.tableType != 0) return ERROR(GENERIC);
  6131. return HUF_decompress1X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6132. }
  6133. size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
  6134. const void* cSrc, size_t cSrcSize,
  6135. void* workSpace, size_t wkspSize)
  6136. {
  6137. const BYTE* ip = (const BYTE*) cSrc;
  6138. size_t const hSize = HUF_readDTableX1_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize);
  6139. if (HUF_isError(hSize)) return hSize;
  6140. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  6141. ip += hSize; cSrcSize -= hSize;
  6142. return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);
  6143. }
  6144. size_t HUF_decompress1X1_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
  6145. const void* cSrc, size_t cSrcSize)
  6146. {
  6147. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6148. return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
  6149. workSpace, sizeof(workSpace));
  6150. }
  6151. size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6152. {
  6153. HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
  6154. return HUF_decompress1X1_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
  6155. }
  6156. size_t HUF_decompress4X1_usingDTable(
  6157. void* dst, size_t dstSize,
  6158. const void* cSrc, size_t cSrcSize,
  6159. const HUF_DTable* DTable)
  6160. {
  6161. DTableDesc dtd = HUF_getDTableDesc(DTable);
  6162. if (dtd.tableType != 0) return ERROR(GENERIC);
  6163. return HUF_decompress4X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6164. }
  6165. static size_t HUF_decompress4X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,
  6166. const void* cSrc, size_t cSrcSize,
  6167. void* workSpace, size_t wkspSize, int bmi2)
  6168. {
  6169. const BYTE* ip = (const BYTE*) cSrc;
  6170. size_t const hSize = HUF_readDTableX1_wksp (dctx, cSrc, cSrcSize,
  6171. workSpace, wkspSize);
  6172. if (HUF_isError(hSize)) return hSize;
  6173. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  6174. ip += hSize; cSrcSize -= hSize;
  6175. return HUF_decompress4X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
  6176. }
  6177. size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  6178. const void* cSrc, size_t cSrcSize,
  6179. void* workSpace, size_t wkspSize)
  6180. {
  6181. return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, 0);
  6182. }
  6183. size_t HUF_decompress4X1_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6184. {
  6185. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6186. return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  6187. workSpace, sizeof(workSpace));
  6188. }
  6189. size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6190. {
  6191. HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
  6192. return HUF_decompress4X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  6193. }
  6194. #endif /* HUF_FORCE_DECOMPRESS_X2 */
  6195. #ifndef HUF_FORCE_DECOMPRESS_X1
  6196. /* *************************/
  6197. /* double-symbols decoding */
  6198. /* *************************/
  6199. typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX2; /* double-symbols decoding */
  6200. typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
  6201. typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
  6202. typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];
  6203. /* HUF_fillDTableX2Level2() :
  6204. * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
  6205. static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 sizeLog, const U32 consumed,
  6206. const U32* rankValOrigin, const int minWeight,
  6207. const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
  6208. U32 nbBitsBaseline, U16 baseSeq)
  6209. {
  6210. HUF_DEltX2 DElt;
  6211. U32 rankVal[HUF_TABLELOG_MAX + 1];
  6212. /* get pre-calculated rankVal */
  6213. memcpy(rankVal, rankValOrigin, sizeof(rankVal));
  6214. /* fill skipped values */
  6215. if (minWeight>1) {
  6216. U32 i, skipSize = rankVal[minWeight];
  6217. MEM_writeLE16(&(DElt.sequence), baseSeq);
  6218. DElt.nbBits = (BYTE)(consumed);
  6219. DElt.length = 1;
  6220. for (i = 0; i < skipSize; i++)
  6221. DTable[i] = DElt;
  6222. }
  6223. /* fill DTable */
  6224. { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
  6225. const U32 symbol = sortedSymbols[s].symbol;
  6226. const U32 weight = sortedSymbols[s].weight;
  6227. const U32 nbBits = nbBitsBaseline - weight;
  6228. const U32 length = 1 << (sizeLog-nbBits);
  6229. const U32 start = rankVal[weight];
  6230. U32 i = start;
  6231. const U32 end = start + length;
  6232. MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
  6233. DElt.nbBits = (BYTE)(nbBits + consumed);
  6234. DElt.length = 2;
  6235. do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
  6236. rankVal[weight] += length;
  6237. } }
  6238. }
  6239. static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,
  6240. const sortedSymbol_t* sortedList, const U32 sortedListSize,
  6241. const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
  6242. const U32 nbBitsBaseline)
  6243. {
  6244. U32 rankVal[HUF_TABLELOG_MAX + 1];
  6245. const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
  6246. const U32 minBits = nbBitsBaseline - maxWeight;
  6247. U32 s;
  6248. memcpy(rankVal, rankValOrigin, sizeof(rankVal));
  6249. /* fill DTable */
  6250. for (s=0; s<sortedListSize; s++) {
  6251. const U16 symbol = sortedList[s].symbol;
  6252. const U32 weight = sortedList[s].weight;
  6253. const U32 nbBits = nbBitsBaseline - weight;
  6254. const U32 start = rankVal[weight];
  6255. const U32 length = 1 << (targetLog-nbBits);
  6256. if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */
  6257. U32 sortedRank;
  6258. int minWeight = nbBits + scaleLog;
  6259. if (minWeight < 1) minWeight = 1;
  6260. sortedRank = rankStart[minWeight];
  6261. HUF_fillDTableX2Level2(DTable+start, targetLog-nbBits, nbBits,
  6262. rankValOrigin[nbBits], minWeight,
  6263. sortedList+sortedRank, sortedListSize-sortedRank,
  6264. nbBitsBaseline, symbol);
  6265. } else {
  6266. HUF_DEltX2 DElt;
  6267. MEM_writeLE16(&(DElt.sequence), symbol);
  6268. DElt.nbBits = (BYTE)(nbBits);
  6269. DElt.length = 1;
  6270. { U32 const end = start + length;
  6271. U32 u;
  6272. for (u = start; u < end; u++) DTable[u] = DElt;
  6273. } }
  6274. rankVal[weight] += length;
  6275. }
  6276. }
  6277. size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
  6278. const void* src, size_t srcSize,
  6279. void* workSpace, size_t wkspSize)
  6280. {
  6281. U32 tableLog, maxW, sizeOfSort, nbSymbols;
  6282. DTableDesc dtd = HUF_getDTableDesc(DTable);
  6283. U32 const maxTableLog = dtd.maxTableLog;
  6284. size_t iSize;
  6285. void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */
  6286. HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
  6287. U32 *rankStart;
  6288. rankValCol_t* rankVal;
  6289. U32* rankStats;
  6290. U32* rankStart0;
  6291. sortedSymbol_t* sortedSymbol;
  6292. BYTE* weightList;
  6293. size_t spaceUsed32 = 0;
  6294. rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);
  6295. spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
  6296. rankStats = (U32 *)workSpace + spaceUsed32;
  6297. spaceUsed32 += HUF_TABLELOG_MAX + 1;
  6298. rankStart0 = (U32 *)workSpace + spaceUsed32;
  6299. spaceUsed32 += HUF_TABLELOG_MAX + 2;
  6300. sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);
  6301. spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
  6302. weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);
  6303. spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
  6304. if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
  6305. rankStart = rankStart0 + 1;
  6306. memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
  6307. DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
  6308. if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
  6309. /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
  6310. iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
  6311. if (HUF_isError(iSize)) return iSize;
  6312. /* check result */
  6313. if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
  6314. /* find maxWeight */
  6315. for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
  6316. /* Get start index of each weight */
  6317. { U32 w, nextRankStart = 0;
  6318. for (w=1; w<maxW+1; w++) {
  6319. U32 current = nextRankStart;
  6320. nextRankStart += rankStats[w];
  6321. rankStart[w] = current;
  6322. }
  6323. rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
  6324. sizeOfSort = nextRankStart;
  6325. }
  6326. /* sort symbols by weight */
  6327. { U32 s;
  6328. for (s=0; s<nbSymbols; s++) {
  6329. U32 const w = weightList[s];
  6330. U32 const r = rankStart[w]++;
  6331. sortedSymbol[r].symbol = (BYTE)s;
  6332. sortedSymbol[r].weight = (BYTE)w;
  6333. }
  6334. rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
  6335. }
  6336. /* Build rankVal */
  6337. { U32* const rankVal0 = rankVal[0];
  6338. { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
  6339. U32 nextRankVal = 0;
  6340. U32 w;
  6341. for (w=1; w<maxW+1; w++) {
  6342. U32 current = nextRankVal;
  6343. nextRankVal += rankStats[w] << (w+rescale);
  6344. rankVal0[w] = current;
  6345. } }
  6346. { U32 const minBits = tableLog+1 - maxW;
  6347. U32 consumed;
  6348. for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
  6349. U32* const rankValPtr = rankVal[consumed];
  6350. U32 w;
  6351. for (w = 1; w < maxW+1; w++) {
  6352. rankValPtr[w] = rankVal0[w] >> consumed;
  6353. } } } }
  6354. HUF_fillDTableX2(dt, maxTableLog,
  6355. sortedSymbol, sizeOfSort,
  6356. rankStart0, rankVal, maxW,
  6357. tableLog+1);
  6358. dtd.tableLog = (BYTE)maxTableLog;
  6359. dtd.tableType = 1;
  6360. memcpy(DTable, &dtd, sizeof(dtd));
  6361. return iSize;
  6362. }
  6363. size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)
  6364. {
  6365. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6366. return HUF_readDTableX2_wksp(DTable, src, srcSize,
  6367. workSpace, sizeof(workSpace));
  6368. }
  6369. FORCE_INLINE_TEMPLATE U32
  6370. HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
  6371. {
  6372. size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
  6373. memcpy(op, dt+val, 2);
  6374. BIT_skipBits(DStream, dt[val].nbBits);
  6375. return dt[val].length;
  6376. }
  6377. FORCE_INLINE_TEMPLATE U32
  6378. HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
  6379. {
  6380. size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
  6381. memcpy(op, dt+val, 1);
  6382. if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
  6383. else {
  6384. if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
  6385. BIT_skipBits(DStream, dt[val].nbBits);
  6386. if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
  6387. /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
  6388. DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);
  6389. } }
  6390. return 1;
  6391. }
  6392. #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
  6393. ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)
  6394. #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
  6395. if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
  6396. ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)
  6397. #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
  6398. if (MEM_64bits()) \
  6399. ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)
  6400. HINT_INLINE size_t
  6401. HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,
  6402. const HUF_DEltX2* const dt, const U32 dtLog)
  6403. {
  6404. BYTE* const pStart = p;
  6405. /* up to 8 symbols at a time */
  6406. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {
  6407. HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
  6408. HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
  6409. HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
  6410. HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
  6411. }
  6412. /* closer to end : up to 2 symbols at a time */
  6413. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))
  6414. HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
  6415. while (p <= pEnd-2)
  6416. HUF_DECODE_SYMBOLX2_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
  6417. if (p < pEnd)
  6418. p += HUF_decodeLastSymbolX2(p, bitDPtr, dt, dtLog);
  6419. return p-pStart;
  6420. }
  6421. FORCE_INLINE_TEMPLATE size_t
  6422. HUF_decompress1X2_usingDTable_internal_body(
  6423. void* dst, size_t dstSize,
  6424. const void* cSrc, size_t cSrcSize,
  6425. const HUF_DTable* DTable)
  6426. {
  6427. BIT_DStream_t bitD;
  6428. /* Init */
  6429. CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
  6430. /* decode */
  6431. { BYTE* const ostart = (BYTE*) dst;
  6432. BYTE* const oend = ostart + dstSize;
  6433. const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
  6434. const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
  6435. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6436. HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog);
  6437. }
  6438. /* check */
  6439. if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
  6440. /* decoded size */
  6441. return dstSize;
  6442. }
  6443. FORCE_INLINE_TEMPLATE size_t
  6444. HUF_decompress4X2_usingDTable_internal_body(
  6445. void* dst, size_t dstSize,
  6446. const void* cSrc, size_t cSrcSize,
  6447. const HUF_DTable* DTable)
  6448. {
  6449. if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
  6450. { const BYTE* const istart = (const BYTE*) cSrc;
  6451. BYTE* const ostart = (BYTE*) dst;
  6452. BYTE* const oend = ostart + dstSize;
  6453. BYTE* const olimit = oend - (sizeof(size_t)-1);
  6454. const void* const dtPtr = DTable+1;
  6455. const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
  6456. /* Init */
  6457. BIT_DStream_t bitD1;
  6458. BIT_DStream_t bitD2;
  6459. BIT_DStream_t bitD3;
  6460. BIT_DStream_t bitD4;
  6461. size_t const length1 = MEM_readLE16(istart);
  6462. size_t const length2 = MEM_readLE16(istart+2);
  6463. size_t const length3 = MEM_readLE16(istart+4);
  6464. size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
  6465. const BYTE* const istart1 = istart + 6; /* jumpTable */
  6466. const BYTE* const istart2 = istart1 + length1;
  6467. const BYTE* const istart3 = istart2 + length2;
  6468. const BYTE* const istart4 = istart3 + length3;
  6469. size_t const segmentSize = (dstSize+3) / 4;
  6470. BYTE* const opStart2 = ostart + segmentSize;
  6471. BYTE* const opStart3 = opStart2 + segmentSize;
  6472. BYTE* const opStart4 = opStart3 + segmentSize;
  6473. BYTE* op1 = ostart;
  6474. BYTE* op2 = opStart2;
  6475. BYTE* op3 = opStart3;
  6476. BYTE* op4 = opStart4;
  6477. U32 endSignal = 1;
  6478. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6479. U32 const dtLog = dtd.tableLog;
  6480. if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
  6481. CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
  6482. CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
  6483. CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
  6484. CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
  6485. /* 16-32 symbols per loop (4-8 symbols per stream) */
  6486. for ( ; (endSignal) & (op4 < olimit); ) {
  6487. #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
  6488. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  6489. HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
  6490. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  6491. HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
  6492. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  6493. HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
  6494. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  6495. HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
  6496. endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;
  6497. endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;
  6498. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  6499. HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
  6500. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  6501. HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
  6502. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  6503. HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
  6504. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  6505. HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
  6506. endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;
  6507. endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;
  6508. #else
  6509. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  6510. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  6511. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  6512. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  6513. HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
  6514. HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
  6515. HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
  6516. HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
  6517. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  6518. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  6519. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  6520. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  6521. HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
  6522. HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
  6523. HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
  6524. HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
  6525. endSignal = (U32)LIKELY(
  6526. (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished)
  6527. & (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished)
  6528. & (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished)
  6529. & (BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished));
  6530. #endif
  6531. }
  6532. /* check corruption */
  6533. if (op1 > opStart2) return ERROR(corruption_detected);
  6534. if (op2 > opStart3) return ERROR(corruption_detected);
  6535. if (op3 > opStart4) return ERROR(corruption_detected);
  6536. /* note : op4 already verified within main loop */
  6537. /* finish bitStreams one by one */
  6538. HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
  6539. HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
  6540. HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
  6541. HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
  6542. /* check */
  6543. { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
  6544. if (!endCheck) return ERROR(corruption_detected); }
  6545. /* decoded size */
  6546. return dstSize;
  6547. }
  6548. }
  6549. HUF_DGEN(HUF_decompress1X2_usingDTable_internal)
  6550. HUF_DGEN(HUF_decompress4X2_usingDTable_internal)
  6551. size_t HUF_decompress1X2_usingDTable(
  6552. void* dst, size_t dstSize,
  6553. const void* cSrc, size_t cSrcSize,
  6554. const HUF_DTable* DTable)
  6555. {
  6556. DTableDesc dtd = HUF_getDTableDesc(DTable);
  6557. if (dtd.tableType != 1) return ERROR(GENERIC);
  6558. return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6559. }
  6560. size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
  6561. const void* cSrc, size_t cSrcSize,
  6562. void* workSpace, size_t wkspSize)
  6563. {
  6564. const BYTE* ip = (const BYTE*) cSrc;
  6565. size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize,
  6566. workSpace, wkspSize);
  6567. if (HUF_isError(hSize)) return hSize;
  6568. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  6569. ip += hSize; cSrcSize -= hSize;
  6570. return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);
  6571. }
  6572. size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
  6573. const void* cSrc, size_t cSrcSize)
  6574. {
  6575. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6576. return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
  6577. workSpace, sizeof(workSpace));
  6578. }
  6579. size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6580. {
  6581. HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
  6582. return HUF_decompress1X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  6583. }
  6584. size_t HUF_decompress4X2_usingDTable(
  6585. void* dst, size_t dstSize,
  6586. const void* cSrc, size_t cSrcSize,
  6587. const HUF_DTable* DTable)
  6588. {
  6589. DTableDesc dtd = HUF_getDTableDesc(DTable);
  6590. if (dtd.tableType != 1) return ERROR(GENERIC);
  6591. return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6592. }
  6593. static size_t HUF_decompress4X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,
  6594. const void* cSrc, size_t cSrcSize,
  6595. void* workSpace, size_t wkspSize, int bmi2)
  6596. {
  6597. const BYTE* ip = (const BYTE*) cSrc;
  6598. size_t hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize,
  6599. workSpace, wkspSize);
  6600. if (HUF_isError(hSize)) return hSize;
  6601. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  6602. ip += hSize; cSrcSize -= hSize;
  6603. return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
  6604. }
  6605. size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  6606. const void* cSrc, size_t cSrcSize,
  6607. void* workSpace, size_t wkspSize)
  6608. {
  6609. return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, /* bmi2 */ 0);
  6610. }
  6611. size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
  6612. const void* cSrc, size_t cSrcSize)
  6613. {
  6614. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6615. return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  6616. workSpace, sizeof(workSpace));
  6617. }
  6618. size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6619. {
  6620. HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
  6621. return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  6622. }
  6623. #endif /* HUF_FORCE_DECOMPRESS_X1 */
  6624. /* ***********************************/
  6625. /* Universal decompression selectors */
  6626. /* ***********************************/
  6627. size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize,
  6628. const void* cSrc, size_t cSrcSize,
  6629. const HUF_DTable* DTable)
  6630. {
  6631. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6632. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6633. (void)dtd;
  6634. assert(dtd.tableType == 0);
  6635. return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6636. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6637. (void)dtd;
  6638. assert(dtd.tableType == 1);
  6639. return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6640. #else
  6641. return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :
  6642. HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6643. #endif
  6644. }
  6645. size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize,
  6646. const void* cSrc, size_t cSrcSize,
  6647. const HUF_DTable* DTable)
  6648. {
  6649. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6650. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6651. (void)dtd;
  6652. assert(dtd.tableType == 0);
  6653. return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6654. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6655. (void)dtd;
  6656. assert(dtd.tableType == 1);
  6657. return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6658. #else
  6659. return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :
  6660. HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
  6661. #endif
  6662. }
  6663. #if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)
  6664. typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
  6665. static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
  6666. {
  6667. /* single, double, quad */
  6668. {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
  6669. {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
  6670. {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
  6671. {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
  6672. {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
  6673. {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
  6674. {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
  6675. {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
  6676. {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
  6677. {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
  6678. {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
  6679. {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
  6680. {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
  6681. {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
  6682. {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
  6683. {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
  6684. };
  6685. #endif
  6686. /** HUF_selectDecoder() :
  6687. * Tells which decoder is likely to decode faster,
  6688. * based on a set of pre-computed metrics.
  6689. * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
  6690. * Assumption : 0 < dstSize <= 128 KB */
  6691. U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)
  6692. {
  6693. assert(dstSize > 0);
  6694. assert(dstSize <= 128*1024);
  6695. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6696. (void)dstSize;
  6697. (void)cSrcSize;
  6698. return 0;
  6699. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6700. (void)dstSize;
  6701. (void)cSrcSize;
  6702. return 1;
  6703. #else
  6704. /* decoder timing evaluation */
  6705. { U32 const Q = (cSrcSize >= dstSize) ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */
  6706. U32 const D256 = (U32)(dstSize >> 8);
  6707. U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
  6708. U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
  6709. DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, to reduce cache eviction */
  6710. return DTime1 < DTime0;
  6711. }
  6712. #endif
  6713. }
  6714. typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
  6715. size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6716. {
  6717. #if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)
  6718. static const decompressionAlgo decompress[2] = { HUF_decompress4X1, HUF_decompress4X2 };
  6719. #endif
  6720. /* validation checks */
  6721. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  6722. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  6723. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  6724. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  6725. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  6726. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6727. (void)algoNb;
  6728. assert(algoNb == 0);
  6729. return HUF_decompress4X1(dst, dstSize, cSrc, cSrcSize);
  6730. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6731. (void)algoNb;
  6732. assert(algoNb == 1);
  6733. return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize);
  6734. #else
  6735. return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
  6736. #endif
  6737. }
  6738. }
  6739. size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6740. {
  6741. /* validation checks */
  6742. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  6743. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  6744. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  6745. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  6746. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  6747. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6748. (void)algoNb;
  6749. assert(algoNb == 0);
  6750. return HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
  6751. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6752. (void)algoNb;
  6753. assert(algoNb == 1);
  6754. return HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
  6755. #else
  6756. return algoNb ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
  6757. HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
  6758. #endif
  6759. }
  6760. }
  6761. size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  6762. {
  6763. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6764. return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  6765. workSpace, sizeof(workSpace));
  6766. }
  6767. size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst,
  6768. size_t dstSize, const void* cSrc,
  6769. size_t cSrcSize, void* workSpace,
  6770. size_t wkspSize)
  6771. {
  6772. /* validation checks */
  6773. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  6774. if (cSrcSize == 0) return ERROR(corruption_detected);
  6775. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  6776. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6777. (void)algoNb;
  6778. assert(algoNb == 0);
  6779. return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);
  6780. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6781. (void)algoNb;
  6782. assert(algoNb == 1);
  6783. return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);
  6784. #else
  6785. return algoNb ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
  6786. cSrcSize, workSpace, wkspSize):
  6787. HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);
  6788. #endif
  6789. }
  6790. }
  6791. size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  6792. const void* cSrc, size_t cSrcSize,
  6793. void* workSpace, size_t wkspSize)
  6794. {
  6795. /* validation checks */
  6796. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  6797. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  6798. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  6799. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  6800. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  6801. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6802. (void)algoNb;
  6803. assert(algoNb == 0);
  6804. return HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,
  6805. cSrcSize, workSpace, wkspSize);
  6806. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6807. (void)algoNb;
  6808. assert(algoNb == 1);
  6809. return HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
  6810. cSrcSize, workSpace, wkspSize);
  6811. #else
  6812. return algoNb ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
  6813. cSrcSize, workSpace, wkspSize):
  6814. HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,
  6815. cSrcSize, workSpace, wkspSize);
  6816. #endif
  6817. }
  6818. }
  6819. size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
  6820. const void* cSrc, size_t cSrcSize)
  6821. {
  6822. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  6823. return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  6824. workSpace, sizeof(workSpace));
  6825. }
  6826. size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)
  6827. {
  6828. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6829. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6830. (void)dtd;
  6831. assert(dtd.tableType == 0);
  6832. return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6833. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6834. (void)dtd;
  6835. assert(dtd.tableType == 1);
  6836. return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6837. #else
  6838. return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :
  6839. HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6840. #endif
  6841. }
  6842. #ifndef HUF_FORCE_DECOMPRESS_X2
  6843. size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)
  6844. {
  6845. const BYTE* ip = (const BYTE*) cSrc;
  6846. size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize);
  6847. if (HUF_isError(hSize)) return hSize;
  6848. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  6849. ip += hSize; cSrcSize -= hSize;
  6850. return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
  6851. }
  6852. #endif
  6853. size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)
  6854. {
  6855. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  6856. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6857. (void)dtd;
  6858. assert(dtd.tableType == 0);
  6859. return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6860. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6861. (void)dtd;
  6862. assert(dtd.tableType == 1);
  6863. return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6864. #else
  6865. return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :
  6866. HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
  6867. #endif
  6868. }
  6869. size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)
  6870. {
  6871. /* validation checks */
  6872. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  6873. if (cSrcSize == 0) return ERROR(corruption_detected);
  6874. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  6875. #if defined(HUF_FORCE_DECOMPRESS_X1)
  6876. (void)algoNb;
  6877. assert(algoNb == 0);
  6878. return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
  6879. #elif defined(HUF_FORCE_DECOMPRESS_X2)
  6880. (void)algoNb;
  6881. assert(algoNb == 1);
  6882. return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
  6883. #else
  6884. return algoNb ? HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2) :
  6885. HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
  6886. #endif
  6887. }
  6888. }
  6889. /**** ended inlining decompress/huf_decompress.c ****/
  6890. /**** start inlining decompress/zstd_ddict.c ****/
  6891. /*
  6892. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  6893. * All rights reserved.
  6894. *
  6895. * This source code is licensed under both the BSD-style license (found in the
  6896. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  6897. * in the COPYING file in the root directory of this source tree).
  6898. * You may select, at your option, one of the above-listed licenses.
  6899. */
  6900. /* zstd_ddict.c :
  6901. * concentrates all logic that needs to know the internals of ZSTD_DDict object */
  6902. /*-*******************************************************
  6903. * Dependencies
  6904. *********************************************************/
  6905. /**** start inlining ../common/cpu.h ****/
  6906. /*
  6907. * Copyright (c) 2018-2020, Facebook, Inc.
  6908. * All rights reserved.
  6909. *
  6910. * This source code is licensed under both the BSD-style license (found in the
  6911. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  6912. * in the COPYING file in the root directory of this source tree).
  6913. * You may select, at your option, one of the above-listed licenses.
  6914. */
  6915. #ifndef ZSTD_COMMON_CPU_H
  6916. #define ZSTD_COMMON_CPU_H
  6917. /**
  6918. * Implementation taken from folly/CpuId.h
  6919. * https://github.com/facebook/folly/blob/master/folly/CpuId.h
  6920. */
  6921. /**** skipping file: mem.h ****/
  6922. #ifdef _MSC_VER
  6923. #include <intrin.h>
  6924. #endif
  6925. typedef struct {
  6926. U32 f1c;
  6927. U32 f1d;
  6928. U32 f7b;
  6929. U32 f7c;
  6930. } ZSTD_cpuid_t;
  6931. MEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) {
  6932. U32 f1c = 0;
  6933. U32 f1d = 0;
  6934. U32 f7b = 0;
  6935. U32 f7c = 0;
  6936. #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
  6937. int reg[4];
  6938. __cpuid((int*)reg, 0);
  6939. {
  6940. int const n = reg[0];
  6941. if (n >= 1) {
  6942. __cpuid((int*)reg, 1);
  6943. f1c = (U32)reg[2];
  6944. f1d = (U32)reg[3];
  6945. }
  6946. if (n >= 7) {
  6947. __cpuidex((int*)reg, 7, 0);
  6948. f7b = (U32)reg[1];
  6949. f7c = (U32)reg[2];
  6950. }
  6951. }
  6952. #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)
  6953. /* The following block like the normal cpuid branch below, but gcc
  6954. * reserves ebx for use of its pic register so we must specially
  6955. * handle the save and restore to avoid clobbering the register
  6956. */
  6957. U32 n;
  6958. __asm__(
  6959. "pushl %%ebx\n\t"
  6960. "cpuid\n\t"
  6961. "popl %%ebx\n\t"
  6962. : "=a"(n)
  6963. : "a"(0)
  6964. : "ecx", "edx");
  6965. if (n >= 1) {
  6966. U32 f1a;
  6967. __asm__(
  6968. "pushl %%ebx\n\t"
  6969. "cpuid\n\t"
  6970. "popl %%ebx\n\t"
  6971. : "=a"(f1a), "=c"(f1c), "=d"(f1d)
  6972. : "a"(1));
  6973. }
  6974. if (n >= 7) {
  6975. __asm__(
  6976. "pushl %%ebx\n\t"
  6977. "cpuid\n\t"
  6978. "movl %%ebx, %%eax\n\t"
  6979. "popl %%ebx"
  6980. : "=a"(f7b), "=c"(f7c)
  6981. : "a"(7), "c"(0)
  6982. : "edx");
  6983. }
  6984. #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)
  6985. U32 n;
  6986. __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");
  6987. if (n >= 1) {
  6988. U32 f1a;
  6989. __asm__("cpuid" : "=a"(f1a), "=c"(f1c), "=d"(f1d) : "a"(1) : "ebx");
  6990. }
  6991. if (n >= 7) {
  6992. U32 f7a;
  6993. __asm__("cpuid"
  6994. : "=a"(f7a), "=b"(f7b), "=c"(f7c)
  6995. : "a"(7), "c"(0)
  6996. : "edx");
  6997. }
  6998. #endif
  6999. {
  7000. ZSTD_cpuid_t cpuid;
  7001. cpuid.f1c = f1c;
  7002. cpuid.f1d = f1d;
  7003. cpuid.f7b = f7b;
  7004. cpuid.f7c = f7c;
  7005. return cpuid;
  7006. }
  7007. }
  7008. #define X(name, r, bit) \
  7009. MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \
  7010. return ((cpuid.r) & (1U << bit)) != 0; \
  7011. }
  7012. /* cpuid(1): Processor Info and Feature Bits. */
  7013. #define C(name, bit) X(name, f1c, bit)
  7014. C(sse3, 0)
  7015. C(pclmuldq, 1)
  7016. C(dtes64, 2)
  7017. C(monitor, 3)
  7018. C(dscpl, 4)
  7019. C(vmx, 5)
  7020. C(smx, 6)
  7021. C(eist, 7)
  7022. C(tm2, 8)
  7023. C(ssse3, 9)
  7024. C(cnxtid, 10)
  7025. C(fma, 12)
  7026. C(cx16, 13)
  7027. C(xtpr, 14)
  7028. C(pdcm, 15)
  7029. C(pcid, 17)
  7030. C(dca, 18)
  7031. C(sse41, 19)
  7032. C(sse42, 20)
  7033. C(x2apic, 21)
  7034. C(movbe, 22)
  7035. C(popcnt, 23)
  7036. C(tscdeadline, 24)
  7037. C(aes, 25)
  7038. C(xsave, 26)
  7039. C(osxsave, 27)
  7040. C(avx, 28)
  7041. C(f16c, 29)
  7042. C(rdrand, 30)
  7043. #undef C
  7044. #define D(name, bit) X(name, f1d, bit)
  7045. D(fpu, 0)
  7046. D(vme, 1)
  7047. D(de, 2)
  7048. D(pse, 3)
  7049. D(tsc, 4)
  7050. D(msr, 5)
  7051. D(pae, 6)
  7052. D(mce, 7)
  7053. D(cx8, 8)
  7054. D(apic, 9)
  7055. D(sep, 11)
  7056. D(mtrr, 12)
  7057. D(pge, 13)
  7058. D(mca, 14)
  7059. D(cmov, 15)
  7060. D(pat, 16)
  7061. D(pse36, 17)
  7062. D(psn, 18)
  7063. D(clfsh, 19)
  7064. D(ds, 21)
  7065. D(acpi, 22)
  7066. D(mmx, 23)
  7067. D(fxsr, 24)
  7068. D(sse, 25)
  7069. D(sse2, 26)
  7070. D(ss, 27)
  7071. D(htt, 28)
  7072. D(tm, 29)
  7073. D(pbe, 31)
  7074. #undef D
  7075. /* cpuid(7): Extended Features. */
  7076. #define B(name, bit) X(name, f7b, bit)
  7077. B(bmi1, 3)
  7078. B(hle, 4)
  7079. B(avx2, 5)
  7080. B(smep, 7)
  7081. B(bmi2, 8)
  7082. B(erms, 9)
  7083. B(invpcid, 10)
  7084. B(rtm, 11)
  7085. B(mpx, 14)
  7086. B(avx512f, 16)
  7087. B(avx512dq, 17)
  7088. B(rdseed, 18)
  7089. B(adx, 19)
  7090. B(smap, 20)
  7091. B(avx512ifma, 21)
  7092. B(pcommit, 22)
  7093. B(clflushopt, 23)
  7094. B(clwb, 24)
  7095. B(avx512pf, 26)
  7096. B(avx512er, 27)
  7097. B(avx512cd, 28)
  7098. B(sha, 29)
  7099. B(avx512bw, 30)
  7100. B(avx512vl, 31)
  7101. #undef B
  7102. #define C(name, bit) X(name, f7c, bit)
  7103. C(prefetchwt1, 0)
  7104. C(avx512vbmi, 1)
  7105. #undef C
  7106. #undef X
  7107. #endif /* ZSTD_COMMON_CPU_H */
  7108. /**** ended inlining ../common/cpu.h ****/
  7109. /**** skipping file: ../common/mem.h ****/
  7110. #define FSE_STATIC_LINKING_ONLY
  7111. /**** skipping file: ../common/fse.h ****/
  7112. #define HUF_STATIC_LINKING_ONLY
  7113. /**** skipping file: ../common/huf.h ****/
  7114. /**** start inlining zstd_decompress_internal.h ****/
  7115. /*
  7116. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7117. * All rights reserved.
  7118. *
  7119. * This source code is licensed under both the BSD-style license (found in the
  7120. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7121. * in the COPYING file in the root directory of this source tree).
  7122. * You may select, at your option, one of the above-listed licenses.
  7123. */
  7124. /* zstd_decompress_internal:
  7125. * objects and definitions shared within lib/decompress modules */
  7126. #ifndef ZSTD_DECOMPRESS_INTERNAL_H
  7127. #define ZSTD_DECOMPRESS_INTERNAL_H
  7128. /*-*******************************************************
  7129. * Dependencies
  7130. *********************************************************/
  7131. /**** skipping file: ../common/mem.h ****/
  7132. /**** skipping file: ../common/zstd_internal.h ****/
  7133. /*-*******************************************************
  7134. * Constants
  7135. *********************************************************/
  7136. static const U32 LL_base[MaxLL+1] = {
  7137. 0, 1, 2, 3, 4, 5, 6, 7,
  7138. 8, 9, 10, 11, 12, 13, 14, 15,
  7139. 16, 18, 20, 22, 24, 28, 32, 40,
  7140. 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
  7141. 0x2000, 0x4000, 0x8000, 0x10000 };
  7142. static const U32 OF_base[MaxOff+1] = {
  7143. 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
  7144. 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
  7145. 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
  7146. 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };
  7147. static const U32 OF_bits[MaxOff+1] = {
  7148. 0, 1, 2, 3, 4, 5, 6, 7,
  7149. 8, 9, 10, 11, 12, 13, 14, 15,
  7150. 16, 17, 18, 19, 20, 21, 22, 23,
  7151. 24, 25, 26, 27, 28, 29, 30, 31 };
  7152. static const U32 ML_base[MaxML+1] = {
  7153. 3, 4, 5, 6, 7, 8, 9, 10,
  7154. 11, 12, 13, 14, 15, 16, 17, 18,
  7155. 19, 20, 21, 22, 23, 24, 25, 26,
  7156. 27, 28, 29, 30, 31, 32, 33, 34,
  7157. 35, 37, 39, 41, 43, 47, 51, 59,
  7158. 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
  7159. 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
  7160. /*-*******************************************************
  7161. * Decompression types
  7162. *********************************************************/
  7163. typedef struct {
  7164. U32 fastMode;
  7165. U32 tableLog;
  7166. } ZSTD_seqSymbol_header;
  7167. typedef struct {
  7168. U16 nextState;
  7169. BYTE nbAdditionalBits;
  7170. BYTE nbBits;
  7171. U32 baseValue;
  7172. } ZSTD_seqSymbol;
  7173. #define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log)))
  7174. typedef struct {
  7175. ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */
  7176. ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */
  7177. ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */
  7178. HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
  7179. U32 rep[ZSTD_REP_NUM];
  7180. } ZSTD_entropyDTables_t;
  7181. typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
  7182. ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
  7183. ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,
  7184. ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;
  7185. typedef enum { zdss_init=0, zdss_loadHeader,
  7186. zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
  7187. typedef enum {
  7188. ZSTD_use_indefinitely = -1, /* Use the dictionary indefinitely */
  7189. ZSTD_dont_use = 0, /* Do not use the dictionary (if one exists free it) */
  7190. ZSTD_use_once = 1 /* Use the dictionary once and set to ZSTD_dont_use */
  7191. } ZSTD_dictUses_e;
  7192. typedef enum {
  7193. ZSTD_obm_buffered = 0, /* Buffer the output */
  7194. ZSTD_obm_stable = 1 /* ZSTD_outBuffer is stable */
  7195. } ZSTD_outBufferMode_e;
  7196. struct ZSTD_DCtx_s
  7197. {
  7198. const ZSTD_seqSymbol* LLTptr;
  7199. const ZSTD_seqSymbol* MLTptr;
  7200. const ZSTD_seqSymbol* OFTptr;
  7201. const HUF_DTable* HUFptr;
  7202. ZSTD_entropyDTables_t entropy;
  7203. U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */
  7204. const void* previousDstEnd; /* detect continuity */
  7205. const void* prefixStart; /* start of current segment */
  7206. const void* virtualStart; /* virtual start of previous segment if it was just before current one */
  7207. const void* dictEnd; /* end of previous segment */
  7208. size_t expected;
  7209. ZSTD_frameHeader fParams;
  7210. U64 decodedSize;
  7211. blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */
  7212. ZSTD_dStage stage;
  7213. U32 litEntropy;
  7214. U32 fseEntropy;
  7215. XXH64_state_t xxhState;
  7216. size_t headerSize;
  7217. ZSTD_format_e format;
  7218. const BYTE* litPtr;
  7219. ZSTD_customMem customMem;
  7220. size_t litSize;
  7221. size_t rleSize;
  7222. size_t staticSize;
  7223. int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
  7224. /* dictionary */
  7225. ZSTD_DDict* ddictLocal;
  7226. const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */
  7227. U32 dictID;
  7228. int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
  7229. ZSTD_dictUses_e dictUses;
  7230. /* streaming */
  7231. ZSTD_dStreamStage streamStage;
  7232. char* inBuff;
  7233. size_t inBuffSize;
  7234. size_t inPos;
  7235. size_t maxWindowSize;
  7236. char* outBuff;
  7237. size_t outBuffSize;
  7238. size_t outStart;
  7239. size_t outEnd;
  7240. size_t lhSize;
  7241. void* legacyContext;
  7242. U32 previousLegacyVersion;
  7243. U32 legacyVersion;
  7244. U32 hostageByte;
  7245. int noForwardProgress;
  7246. ZSTD_outBufferMode_e outBufferMode;
  7247. ZSTD_outBuffer expectedOutBuffer;
  7248. /* workspace */
  7249. BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
  7250. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  7251. size_t oversizedDuration;
  7252. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  7253. void const* dictContentBeginForFuzzing;
  7254. void const* dictContentEndForFuzzing;
  7255. #endif
  7256. }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
  7257. /*-*******************************************************
  7258. * Shared internal functions
  7259. *********************************************************/
  7260. /*! ZSTD_loadDEntropy() :
  7261. * dict : must point at beginning of a valid zstd dictionary.
  7262. * @return : size of dictionary header (size of magic number + dict ID + entropy tables) */
  7263. size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  7264. const void* const dict, size_t const dictSize);
  7265. /*! ZSTD_checkContinuity() :
  7266. * check if next `dst` follows previous position, where decompression ended.
  7267. * If yes, do nothing (continue on current segment).
  7268. * If not, classify previous segment as "external dictionary", and start a new segment.
  7269. * This function cannot fail. */
  7270. void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst);
  7271. #endif /* ZSTD_DECOMPRESS_INTERNAL_H */
  7272. /**** ended inlining zstd_decompress_internal.h ****/
  7273. /**** start inlining zstd_ddict.h ****/
  7274. /*
  7275. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7276. * All rights reserved.
  7277. *
  7278. * This source code is licensed under both the BSD-style license (found in the
  7279. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7280. * in the COPYING file in the root directory of this source tree).
  7281. * You may select, at your option, one of the above-listed licenses.
  7282. */
  7283. #ifndef ZSTD_DDICT_H
  7284. #define ZSTD_DDICT_H
  7285. /*-*******************************************************
  7286. * Dependencies
  7287. *********************************************************/
  7288. #include <stddef.h> /* size_t */
  7289. /**** skipping file: ../zstd.h ****/
  7290. /*-*******************************************************
  7291. * Interface
  7292. *********************************************************/
  7293. /* note: several prototypes are already published in `zstd.h` :
  7294. * ZSTD_createDDict()
  7295. * ZSTD_createDDict_byReference()
  7296. * ZSTD_createDDict_advanced()
  7297. * ZSTD_freeDDict()
  7298. * ZSTD_initStaticDDict()
  7299. * ZSTD_sizeof_DDict()
  7300. * ZSTD_estimateDDictSize()
  7301. * ZSTD_getDictID_fromDict()
  7302. */
  7303. const void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict);
  7304. size_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict);
  7305. void ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
  7306. #endif /* ZSTD_DDICT_H */
  7307. /**** ended inlining zstd_ddict.h ****/
  7308. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  7309. /**** start inlining ../legacy/zstd_legacy.h ****/
  7310. /*
  7311. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7312. * All rights reserved.
  7313. *
  7314. * This source code is licensed under both the BSD-style license (found in the
  7315. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7316. * in the COPYING file in the root directory of this source tree).
  7317. * You may select, at your option, one of the above-listed licenses.
  7318. */
  7319. #ifndef ZSTD_LEGACY_H
  7320. #define ZSTD_LEGACY_H
  7321. #if defined (__cplusplus)
  7322. extern "C" {
  7323. #endif
  7324. /* *************************************
  7325. * Includes
  7326. ***************************************/
  7327. /**** skipping file: ../common/mem.h ****/
  7328. /**** skipping file: ../common/error_private.h ****/
  7329. /**** skipping file: ../common/zstd_internal.h ****/
  7330. #if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
  7331. # undef ZSTD_LEGACY_SUPPORT
  7332. # define ZSTD_LEGACY_SUPPORT 8
  7333. #endif
  7334. #if (ZSTD_LEGACY_SUPPORT <= 1)
  7335. /**** start inlining zstd_v01.h ****/
  7336. /*
  7337. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7338. * All rights reserved.
  7339. *
  7340. * This source code is licensed under both the BSD-style license (found in the
  7341. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7342. * in the COPYING file in the root directory of this source tree).
  7343. * You may select, at your option, one of the above-listed licenses.
  7344. */
  7345. #ifndef ZSTD_V01_H_28739879432
  7346. #define ZSTD_V01_H_28739879432
  7347. #if defined (__cplusplus)
  7348. extern "C" {
  7349. #endif
  7350. /* *************************************
  7351. * Includes
  7352. ***************************************/
  7353. #include <stddef.h> /* size_t */
  7354. /* *************************************
  7355. * Simple one-step function
  7356. ***************************************/
  7357. /**
  7358. ZSTDv01_decompress() : decompress ZSTD frames compliant with v0.1.x format
  7359. compressedSize : is the exact source size
  7360. maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
  7361. It must be equal or larger than originalSize, otherwise decompression will fail.
  7362. return : the number of bytes decompressed into destination buffer (originalSize)
  7363. or an errorCode if it fails (which can be tested using ZSTDv01_isError())
  7364. */
  7365. size_t ZSTDv01_decompress( void* dst, size_t maxOriginalSize,
  7366. const void* src, size_t compressedSize);
  7367. /**
  7368. ZSTDv01_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.1.x format
  7369. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7370. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7371. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7372. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7373. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7374. note : assumes `cSize` and `dBound` are _not_ NULL.
  7375. */
  7376. void ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7377. size_t* cSize, unsigned long long* dBound);
  7378. /**
  7379. ZSTDv01_isError() : tells if the result of ZSTDv01_decompress() is an error
  7380. */
  7381. unsigned ZSTDv01_isError(size_t code);
  7382. /* *************************************
  7383. * Advanced functions
  7384. ***************************************/
  7385. typedef struct ZSTDv01_Dctx_s ZSTDv01_Dctx;
  7386. ZSTDv01_Dctx* ZSTDv01_createDCtx(void);
  7387. size_t ZSTDv01_freeDCtx(ZSTDv01_Dctx* dctx);
  7388. size_t ZSTDv01_decompressDCtx(void* ctx,
  7389. void* dst, size_t maxOriginalSize,
  7390. const void* src, size_t compressedSize);
  7391. /* *************************************
  7392. * Streaming functions
  7393. ***************************************/
  7394. size_t ZSTDv01_resetDCtx(ZSTDv01_Dctx* dctx);
  7395. size_t ZSTDv01_nextSrcSizeToDecompress(ZSTDv01_Dctx* dctx);
  7396. size_t ZSTDv01_decompressContinue(ZSTDv01_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
  7397. /**
  7398. Use above functions alternatively.
  7399. ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  7400. ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
  7401. Result is the number of bytes regenerated within 'dst'.
  7402. It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
  7403. */
  7404. /* *************************************
  7405. * Prefix - version detection
  7406. ***************************************/
  7407. #define ZSTDv01_magicNumber 0xFD2FB51E /* Big Endian version */
  7408. #define ZSTDv01_magicNumberLE 0x1EB52FFD /* Little Endian version */
  7409. #if defined (__cplusplus)
  7410. }
  7411. #endif
  7412. #endif /* ZSTD_V01_H_28739879432 */
  7413. /**** ended inlining zstd_v01.h ****/
  7414. #endif
  7415. #if (ZSTD_LEGACY_SUPPORT <= 2)
  7416. /**** start inlining zstd_v02.h ****/
  7417. /*
  7418. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7419. * All rights reserved.
  7420. *
  7421. * This source code is licensed under both the BSD-style license (found in the
  7422. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7423. * in the COPYING file in the root directory of this source tree).
  7424. * You may select, at your option, one of the above-listed licenses.
  7425. */
  7426. #ifndef ZSTD_V02_H_4174539423
  7427. #define ZSTD_V02_H_4174539423
  7428. #if defined (__cplusplus)
  7429. extern "C" {
  7430. #endif
  7431. /* *************************************
  7432. * Includes
  7433. ***************************************/
  7434. #include <stddef.h> /* size_t */
  7435. /* *************************************
  7436. * Simple one-step function
  7437. ***************************************/
  7438. /**
  7439. ZSTDv02_decompress() : decompress ZSTD frames compliant with v0.2.x format
  7440. compressedSize : is the exact source size
  7441. maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
  7442. It must be equal or larger than originalSize, otherwise decompression will fail.
  7443. return : the number of bytes decompressed into destination buffer (originalSize)
  7444. or an errorCode if it fails (which can be tested using ZSTDv01_isError())
  7445. */
  7446. size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
  7447. const void* src, size_t compressedSize);
  7448. /**
  7449. ZSTDv02_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.2.x format
  7450. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7451. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7452. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7453. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7454. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7455. note : assumes `cSize` and `dBound` are _not_ NULL.
  7456. */
  7457. void ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7458. size_t* cSize, unsigned long long* dBound);
  7459. /**
  7460. ZSTDv02_isError() : tells if the result of ZSTDv02_decompress() is an error
  7461. */
  7462. unsigned ZSTDv02_isError(size_t code);
  7463. /* *************************************
  7464. * Advanced functions
  7465. ***************************************/
  7466. typedef struct ZSTDv02_Dctx_s ZSTDv02_Dctx;
  7467. ZSTDv02_Dctx* ZSTDv02_createDCtx(void);
  7468. size_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx);
  7469. size_t ZSTDv02_decompressDCtx(void* ctx,
  7470. void* dst, size_t maxOriginalSize,
  7471. const void* src, size_t compressedSize);
  7472. /* *************************************
  7473. * Streaming functions
  7474. ***************************************/
  7475. size_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx);
  7476. size_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx);
  7477. size_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
  7478. /**
  7479. Use above functions alternatively.
  7480. ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  7481. ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
  7482. Result is the number of bytes regenerated within 'dst'.
  7483. It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
  7484. */
  7485. /* *************************************
  7486. * Prefix - version detection
  7487. ***************************************/
  7488. #define ZSTDv02_magicNumber 0xFD2FB522 /* v0.2 */
  7489. #if defined (__cplusplus)
  7490. }
  7491. #endif
  7492. #endif /* ZSTD_V02_H_4174539423 */
  7493. /**** ended inlining zstd_v02.h ****/
  7494. #endif
  7495. #if (ZSTD_LEGACY_SUPPORT <= 3)
  7496. /**** start inlining zstd_v03.h ****/
  7497. /*
  7498. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7499. * All rights reserved.
  7500. *
  7501. * This source code is licensed under both the BSD-style license (found in the
  7502. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7503. * in the COPYING file in the root directory of this source tree).
  7504. * You may select, at your option, one of the above-listed licenses.
  7505. */
  7506. #ifndef ZSTD_V03_H_298734209782
  7507. #define ZSTD_V03_H_298734209782
  7508. #if defined (__cplusplus)
  7509. extern "C" {
  7510. #endif
  7511. /* *************************************
  7512. * Includes
  7513. ***************************************/
  7514. #include <stddef.h> /* size_t */
  7515. /* *************************************
  7516. * Simple one-step function
  7517. ***************************************/
  7518. /**
  7519. ZSTDv03_decompress() : decompress ZSTD frames compliant with v0.3.x format
  7520. compressedSize : is the exact source size
  7521. maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
  7522. It must be equal or larger than originalSize, otherwise decompression will fail.
  7523. return : the number of bytes decompressed into destination buffer (originalSize)
  7524. or an errorCode if it fails (which can be tested using ZSTDv01_isError())
  7525. */
  7526. size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
  7527. const void* src, size_t compressedSize);
  7528. /**
  7529. ZSTDv03_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.3.x format
  7530. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7531. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7532. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7533. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7534. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7535. note : assumes `cSize` and `dBound` are _not_ NULL.
  7536. */
  7537. void ZSTDv03_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7538. size_t* cSize, unsigned long long* dBound);
  7539. /**
  7540. ZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error
  7541. */
  7542. unsigned ZSTDv03_isError(size_t code);
  7543. /* *************************************
  7544. * Advanced functions
  7545. ***************************************/
  7546. typedef struct ZSTDv03_Dctx_s ZSTDv03_Dctx;
  7547. ZSTDv03_Dctx* ZSTDv03_createDCtx(void);
  7548. size_t ZSTDv03_freeDCtx(ZSTDv03_Dctx* dctx);
  7549. size_t ZSTDv03_decompressDCtx(void* ctx,
  7550. void* dst, size_t maxOriginalSize,
  7551. const void* src, size_t compressedSize);
  7552. /* *************************************
  7553. * Streaming functions
  7554. ***************************************/
  7555. size_t ZSTDv03_resetDCtx(ZSTDv03_Dctx* dctx);
  7556. size_t ZSTDv03_nextSrcSizeToDecompress(ZSTDv03_Dctx* dctx);
  7557. size_t ZSTDv03_decompressContinue(ZSTDv03_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
  7558. /**
  7559. Use above functions alternatively.
  7560. ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  7561. ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
  7562. Result is the number of bytes regenerated within 'dst'.
  7563. It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
  7564. */
  7565. /* *************************************
  7566. * Prefix - version detection
  7567. ***************************************/
  7568. #define ZSTDv03_magicNumber 0xFD2FB523 /* v0.3 */
  7569. #if defined (__cplusplus)
  7570. }
  7571. #endif
  7572. #endif /* ZSTD_V03_H_298734209782 */
  7573. /**** ended inlining zstd_v03.h ****/
  7574. #endif
  7575. #if (ZSTD_LEGACY_SUPPORT <= 4)
  7576. /**** start inlining zstd_v04.h ****/
  7577. /*
  7578. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7579. * All rights reserved.
  7580. *
  7581. * This source code is licensed under both the BSD-style license (found in the
  7582. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7583. * in the COPYING file in the root directory of this source tree).
  7584. * You may select, at your option, one of the above-listed licenses.
  7585. */
  7586. #ifndef ZSTD_V04_H_91868324769238
  7587. #define ZSTD_V04_H_91868324769238
  7588. #if defined (__cplusplus)
  7589. extern "C" {
  7590. #endif
  7591. /* *************************************
  7592. * Includes
  7593. ***************************************/
  7594. #include <stddef.h> /* size_t */
  7595. /* *************************************
  7596. * Simple one-step function
  7597. ***************************************/
  7598. /**
  7599. ZSTDv04_decompress() : decompress ZSTD frames compliant with v0.4.x format
  7600. compressedSize : is the exact source size
  7601. maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
  7602. It must be equal or larger than originalSize, otherwise decompression will fail.
  7603. return : the number of bytes decompressed into destination buffer (originalSize)
  7604. or an errorCode if it fails (which can be tested using ZSTDv01_isError())
  7605. */
  7606. size_t ZSTDv04_decompress( void* dst, size_t maxOriginalSize,
  7607. const void* src, size_t compressedSize);
  7608. /**
  7609. ZSTDv04_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.4.x format
  7610. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7611. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7612. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7613. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7614. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7615. note : assumes `cSize` and `dBound` are _not_ NULL.
  7616. */
  7617. void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7618. size_t* cSize, unsigned long long* dBound);
  7619. /**
  7620. ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error
  7621. */
  7622. unsigned ZSTDv04_isError(size_t code);
  7623. /* *************************************
  7624. * Advanced functions
  7625. ***************************************/
  7626. typedef struct ZSTDv04_Dctx_s ZSTDv04_Dctx;
  7627. ZSTDv04_Dctx* ZSTDv04_createDCtx(void);
  7628. size_t ZSTDv04_freeDCtx(ZSTDv04_Dctx* dctx);
  7629. size_t ZSTDv04_decompressDCtx(ZSTDv04_Dctx* dctx,
  7630. void* dst, size_t maxOriginalSize,
  7631. const void* src, size_t compressedSize);
  7632. /* *************************************
  7633. * Direct Streaming
  7634. ***************************************/
  7635. size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx);
  7636. size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx);
  7637. size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
  7638. /**
  7639. Use above functions alternatively.
  7640. ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  7641. ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
  7642. Result is the number of bytes regenerated within 'dst'.
  7643. It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
  7644. */
  7645. /* *************************************
  7646. * Buffered Streaming
  7647. ***************************************/
  7648. typedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx;
  7649. ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void);
  7650. size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx);
  7651. size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx);
  7652. size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize);
  7653. size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr);
  7654. /** ************************************************
  7655. * Streaming decompression
  7656. *
  7657. * A ZBUFF_DCtx object is required to track streaming operation.
  7658. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
  7659. * Use ZBUFF_decompressInit() to start a new decompression operation.
  7660. * ZBUFF_DCtx objects can be reused multiple times.
  7661. *
  7662. * Optionally, a reference to a static dictionary can be set, using ZBUFF_decompressWithDictionary()
  7663. * It must be the same content as the one set during compression phase.
  7664. * Dictionary content must remain accessible during the decompression process.
  7665. *
  7666. * Use ZBUFF_decompressContinue() repetitively to consume your input.
  7667. * *srcSizePtr and *maxDstSizePtr can be any size.
  7668. * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
  7669. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  7670. * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst.
  7671. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
  7672. * or 0 when a frame is completely decoded
  7673. * or an error code, which can be tested using ZBUFF_isError().
  7674. *
  7675. * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize / ZBUFF_recommendedDOutSize
  7676. * output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded.
  7677. * input : ZBUFF_recommendedDInSize==128Kb+3; just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  7678. * **************************************************/
  7679. unsigned ZBUFFv04_isError(size_t errorCode);
  7680. const char* ZBUFFv04_getErrorName(size_t errorCode);
  7681. /** The below functions provide recommended buffer sizes for Compression or Decompression operations.
  7682. * These sizes are not compulsory, they just tend to offer better latency */
  7683. size_t ZBUFFv04_recommendedDInSize(void);
  7684. size_t ZBUFFv04_recommendedDOutSize(void);
  7685. /* *************************************
  7686. * Prefix - version detection
  7687. ***************************************/
  7688. #define ZSTDv04_magicNumber 0xFD2FB524 /* v0.4 */
  7689. #if defined (__cplusplus)
  7690. }
  7691. #endif
  7692. #endif /* ZSTD_V04_H_91868324769238 */
  7693. /**** ended inlining zstd_v04.h ****/
  7694. #endif
  7695. #if (ZSTD_LEGACY_SUPPORT <= 5)
  7696. /**** start inlining zstd_v05.h ****/
  7697. /*
  7698. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7699. * All rights reserved.
  7700. *
  7701. * This source code is licensed under both the BSD-style license (found in the
  7702. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7703. * in the COPYING file in the root directory of this source tree).
  7704. * You may select, at your option, one of the above-listed licenses.
  7705. */
  7706. #ifndef ZSTDv05_H
  7707. #define ZSTDv05_H
  7708. #if defined (__cplusplus)
  7709. extern "C" {
  7710. #endif
  7711. /*-*************************************
  7712. * Dependencies
  7713. ***************************************/
  7714. #include <stddef.h> /* size_t */
  7715. /**** skipping file: ../common/mem.h ****/
  7716. /* *************************************
  7717. * Simple functions
  7718. ***************************************/
  7719. /*! ZSTDv05_decompress() :
  7720. `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
  7721. `dstCapacity` must be large enough, equal or larger than originalSize.
  7722. @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  7723. or an errorCode if it fails (which can be tested using ZSTDv05_isError()) */
  7724. size_t ZSTDv05_decompress( void* dst, size_t dstCapacity,
  7725. const void* src, size_t compressedSize);
  7726. /**
  7727. ZSTDv05_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.5.x format
  7728. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7729. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7730. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7731. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7732. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7733. note : assumes `cSize` and `dBound` are _not_ NULL.
  7734. */
  7735. void ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7736. size_t* cSize, unsigned long long* dBound);
  7737. /* *************************************
  7738. * Helper functions
  7739. ***************************************/
  7740. /* Error Management */
  7741. unsigned ZSTDv05_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  7742. const char* ZSTDv05_getErrorName(size_t code); /*!< provides readable string for an error code */
  7743. /* *************************************
  7744. * Explicit memory management
  7745. ***************************************/
  7746. /** Decompression context */
  7747. typedef struct ZSTDv05_DCtx_s ZSTDv05_DCtx;
  7748. ZSTDv05_DCtx* ZSTDv05_createDCtx(void);
  7749. size_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx); /*!< @return : errorCode */
  7750. /** ZSTDv05_decompressDCtx() :
  7751. * Same as ZSTDv05_decompress(), but requires an already allocated ZSTDv05_DCtx (see ZSTDv05_createDCtx()) */
  7752. size_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  7753. /*-***********************
  7754. * Simple Dictionary API
  7755. *************************/
  7756. /*! ZSTDv05_decompress_usingDict() :
  7757. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  7758. * Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
  7759. * Note : dict can be NULL, in which case, it's equivalent to ZSTDv05_decompressDCtx() */
  7760. size_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,
  7761. void* dst, size_t dstCapacity,
  7762. const void* src, size_t srcSize,
  7763. const void* dict,size_t dictSize);
  7764. /*-************************
  7765. * Advanced Streaming API
  7766. ***************************/
  7767. typedef enum { ZSTDv05_fast, ZSTDv05_greedy, ZSTDv05_lazy, ZSTDv05_lazy2, ZSTDv05_btlazy2, ZSTDv05_opt, ZSTDv05_btopt } ZSTDv05_strategy;
  7768. typedef struct {
  7769. U64 srcSize;
  7770. U32 windowLog; /* the only useful information to retrieve */
  7771. U32 contentLog; U32 hashLog; U32 searchLog; U32 searchLength; U32 targetLength; ZSTDv05_strategy strategy;
  7772. } ZSTDv05_parameters;
  7773. size_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize);
  7774. size_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize);
  7775. void ZSTDv05_copyDCtx(ZSTDv05_DCtx* dstDCtx, const ZSTDv05_DCtx* srcDCtx);
  7776. size_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx);
  7777. size_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  7778. /*-***********************
  7779. * ZBUFF API
  7780. *************************/
  7781. typedef struct ZBUFFv05_DCtx_s ZBUFFv05_DCtx;
  7782. ZBUFFv05_DCtx* ZBUFFv05_createDCtx(void);
  7783. size_t ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* dctx);
  7784. size_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* dctx);
  7785. size_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* dctx, const void* dict, size_t dictSize);
  7786. size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* dctx,
  7787. void* dst, size_t* dstCapacityPtr,
  7788. const void* src, size_t* srcSizePtr);
  7789. /*-***************************************************************************
  7790. * Streaming decompression
  7791. *
  7792. * A ZBUFFv05_DCtx object is required to track streaming operations.
  7793. * Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.
  7794. * Use ZBUFFv05_decompressInit() to start a new decompression operation,
  7795. * or ZBUFFv05_decompressInitDictionary() if decompression requires a dictionary.
  7796. * Note that ZBUFFv05_DCtx objects can be reused multiple times.
  7797. *
  7798. * Use ZBUFFv05_decompressContinue() repetitively to consume your input.
  7799. * *srcSizePtr and *dstCapacityPtr can be any size.
  7800. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  7801. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  7802. * The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change @dst.
  7803. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency)
  7804. * or 0 when a frame is completely decoded
  7805. * or an error code, which can be tested using ZBUFFv05_isError().
  7806. *
  7807. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv05_recommendedDInSize() / ZBUFFv05_recommendedDOutSize()
  7808. * output : ZBUFFv05_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  7809. * input : ZBUFFv05_recommendedDInSize==128Kb+3; just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  7810. * *******************************************************************************/
  7811. /* *************************************
  7812. * Tool functions
  7813. ***************************************/
  7814. unsigned ZBUFFv05_isError(size_t errorCode);
  7815. const char* ZBUFFv05_getErrorName(size_t errorCode);
  7816. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  7817. * These sizes are just hints, and tend to offer better latency */
  7818. size_t ZBUFFv05_recommendedDInSize(void);
  7819. size_t ZBUFFv05_recommendedDOutSize(void);
  7820. /*-*************************************
  7821. * Constants
  7822. ***************************************/
  7823. #define ZSTDv05_MAGICNUMBER 0xFD2FB525 /* v0.5 */
  7824. #if defined (__cplusplus)
  7825. }
  7826. #endif
  7827. #endif /* ZSTDv0505_H */
  7828. /**** ended inlining zstd_v05.h ****/
  7829. #endif
  7830. #if (ZSTD_LEGACY_SUPPORT <= 6)
  7831. /**** start inlining zstd_v06.h ****/
  7832. /*
  7833. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7834. * All rights reserved.
  7835. *
  7836. * This source code is licensed under both the BSD-style license (found in the
  7837. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7838. * in the COPYING file in the root directory of this source tree).
  7839. * You may select, at your option, one of the above-listed licenses.
  7840. */
  7841. #ifndef ZSTDv06_H
  7842. #define ZSTDv06_H
  7843. #if defined (__cplusplus)
  7844. extern "C" {
  7845. #endif
  7846. /*====== Dependency ======*/
  7847. #include <stddef.h> /* size_t */
  7848. /*====== Export for Windows ======*/
  7849. /*!
  7850. * ZSTDv06_DLL_EXPORT :
  7851. * Enable exporting of functions when building a Windows DLL
  7852. */
  7853. #if defined(_WIN32) && defined(ZSTDv06_DLL_EXPORT) && (ZSTDv06_DLL_EXPORT==1)
  7854. # define ZSTDLIBv06_API __declspec(dllexport)
  7855. #else
  7856. # define ZSTDLIBv06_API
  7857. #endif
  7858. /* *************************************
  7859. * Simple functions
  7860. ***************************************/
  7861. /*! ZSTDv06_decompress() :
  7862. `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
  7863. `dstCapacity` must be large enough, equal or larger than originalSize.
  7864. @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  7865. or an errorCode if it fails (which can be tested using ZSTDv06_isError()) */
  7866. ZSTDLIBv06_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity,
  7867. const void* src, size_t compressedSize);
  7868. /**
  7869. ZSTDv06_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.6.x format
  7870. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  7871. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  7872. or an error code if it fails (which can be tested using ZSTDv01_isError())
  7873. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  7874. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  7875. note : assumes `cSize` and `dBound` are _not_ NULL.
  7876. */
  7877. void ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  7878. size_t* cSize, unsigned long long* dBound);
  7879. /* *************************************
  7880. * Helper functions
  7881. ***************************************/
  7882. ZSTDLIBv06_API size_t ZSTDv06_compressBound(size_t srcSize); /*!< maximum compressed size (worst case scenario) */
  7883. /* Error Management */
  7884. ZSTDLIBv06_API unsigned ZSTDv06_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  7885. ZSTDLIBv06_API const char* ZSTDv06_getErrorName(size_t code); /*!< provides readable string for an error code */
  7886. /* *************************************
  7887. * Explicit memory management
  7888. ***************************************/
  7889. /** Decompression context */
  7890. typedef struct ZSTDv06_DCtx_s ZSTDv06_DCtx;
  7891. ZSTDLIBv06_API ZSTDv06_DCtx* ZSTDv06_createDCtx(void);
  7892. ZSTDLIBv06_API size_t ZSTDv06_freeDCtx(ZSTDv06_DCtx* dctx); /*!< @return : errorCode */
  7893. /** ZSTDv06_decompressDCtx() :
  7894. * Same as ZSTDv06_decompress(), but requires an already allocated ZSTDv06_DCtx (see ZSTDv06_createDCtx()) */
  7895. ZSTDLIBv06_API size_t ZSTDv06_decompressDCtx(ZSTDv06_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  7896. /*-***********************
  7897. * Dictionary API
  7898. *************************/
  7899. /*! ZSTDv06_decompress_usingDict() :
  7900. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  7901. * Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
  7902. * Note : dict can be NULL, in which case, it's equivalent to ZSTDv06_decompressDCtx() */
  7903. ZSTDLIBv06_API size_t ZSTDv06_decompress_usingDict(ZSTDv06_DCtx* dctx,
  7904. void* dst, size_t dstCapacity,
  7905. const void* src, size_t srcSize,
  7906. const void* dict,size_t dictSize);
  7907. /*-************************
  7908. * Advanced Streaming API
  7909. ***************************/
  7910. struct ZSTDv06_frameParams_s { unsigned long long frameContentSize; unsigned windowLog; };
  7911. typedef struct ZSTDv06_frameParams_s ZSTDv06_frameParams;
  7912. ZSTDLIBv06_API size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  7913. ZSTDLIBv06_API size_t ZSTDv06_decompressBegin_usingDict(ZSTDv06_DCtx* dctx, const void* dict, size_t dictSize);
  7914. ZSTDLIBv06_API void ZSTDv06_copyDCtx(ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx);
  7915. ZSTDLIBv06_API size_t ZSTDv06_nextSrcSizeToDecompress(ZSTDv06_DCtx* dctx);
  7916. ZSTDLIBv06_API size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  7917. /* *************************************
  7918. * ZBUFF API
  7919. ***************************************/
  7920. typedef struct ZBUFFv06_DCtx_s ZBUFFv06_DCtx;
  7921. ZSTDLIBv06_API ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void);
  7922. ZSTDLIBv06_API size_t ZBUFFv06_freeDCtx(ZBUFFv06_DCtx* dctx);
  7923. ZSTDLIBv06_API size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* dctx);
  7924. ZSTDLIBv06_API size_t ZBUFFv06_decompressInitDictionary(ZBUFFv06_DCtx* dctx, const void* dict, size_t dictSize);
  7925. ZSTDLIBv06_API size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* dctx,
  7926. void* dst, size_t* dstCapacityPtr,
  7927. const void* src, size_t* srcSizePtr);
  7928. /*-***************************************************************************
  7929. * Streaming decompression howto
  7930. *
  7931. * A ZBUFFv06_DCtx object is required to track streaming operations.
  7932. * Use ZBUFFv06_createDCtx() and ZBUFFv06_freeDCtx() to create/release resources.
  7933. * Use ZBUFFv06_decompressInit() to start a new decompression operation,
  7934. * or ZBUFFv06_decompressInitDictionary() if decompression requires a dictionary.
  7935. * Note that ZBUFFv06_DCtx objects can be re-init multiple times.
  7936. *
  7937. * Use ZBUFFv06_decompressContinue() repetitively to consume your input.
  7938. * *srcSizePtr and *dstCapacityPtr can be any size.
  7939. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  7940. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  7941. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  7942. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  7943. * or 0 when a frame is completely decoded,
  7944. * or an error code, which can be tested using ZBUFFv06_isError().
  7945. *
  7946. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv06_recommendedDInSize() and ZBUFFv06_recommendedDOutSize()
  7947. * output : ZBUFFv06_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  7948. * input : ZBUFFv06_recommendedDInSize == 128KB + 3;
  7949. * just follow indications from ZBUFFv06_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  7950. * *******************************************************************************/
  7951. /* *************************************
  7952. * Tool functions
  7953. ***************************************/
  7954. ZSTDLIBv06_API unsigned ZBUFFv06_isError(size_t errorCode);
  7955. ZSTDLIBv06_API const char* ZBUFFv06_getErrorName(size_t errorCode);
  7956. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  7957. * These sizes are just hints, they tend to offer better latency */
  7958. ZSTDLIBv06_API size_t ZBUFFv06_recommendedDInSize(void);
  7959. ZSTDLIBv06_API size_t ZBUFFv06_recommendedDOutSize(void);
  7960. /*-*************************************
  7961. * Constants
  7962. ***************************************/
  7963. #define ZSTDv06_MAGICNUMBER 0xFD2FB526 /* v0.6 */
  7964. #if defined (__cplusplus)
  7965. }
  7966. #endif
  7967. #endif /* ZSTDv06_BUFFERED_H */
  7968. /**** ended inlining zstd_v06.h ****/
  7969. #endif
  7970. #if (ZSTD_LEGACY_SUPPORT <= 7)
  7971. /**** start inlining zstd_v07.h ****/
  7972. /*
  7973. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  7974. * All rights reserved.
  7975. *
  7976. * This source code is licensed under both the BSD-style license (found in the
  7977. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7978. * in the COPYING file in the root directory of this source tree).
  7979. * You may select, at your option, one of the above-listed licenses.
  7980. */
  7981. #ifndef ZSTDv07_H_235446
  7982. #define ZSTDv07_H_235446
  7983. #if defined (__cplusplus)
  7984. extern "C" {
  7985. #endif
  7986. /*====== Dependency ======*/
  7987. #include <stddef.h> /* size_t */
  7988. /*====== Export for Windows ======*/
  7989. /*!
  7990. * ZSTDv07_DLL_EXPORT :
  7991. * Enable exporting of functions when building a Windows DLL
  7992. */
  7993. #if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1)
  7994. # define ZSTDLIBv07_API __declspec(dllexport)
  7995. #else
  7996. # define ZSTDLIBv07_API
  7997. #endif
  7998. /* *************************************
  7999. * Simple API
  8000. ***************************************/
  8001. /*! ZSTDv07_getDecompressedSize() :
  8002. * @return : decompressed size if known, 0 otherwise.
  8003. note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause.
  8004. note 2 : decompressed size could be wrong or intentionally modified !
  8005. always ensure results fit within application's authorized limits */
  8006. unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize);
  8007. /*! ZSTDv07_decompress() :
  8008. `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail.
  8009. `dstCapacity` must be equal or larger than originalSize.
  8010. @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  8011. or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */
  8012. ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,
  8013. const void* src, size_t compressedSize);
  8014. /**
  8015. ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format
  8016. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  8017. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  8018. or an error code if it fails (which can be tested using ZSTDv01_isError())
  8019. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  8020. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  8021. note : assumes `cSize` and `dBound` are _not_ NULL.
  8022. */
  8023. void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  8024. size_t* cSize, unsigned long long* dBound);
  8025. /*====== Helper functions ======*/
  8026. ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  8027. ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */
  8028. /*-*************************************
  8029. * Explicit memory management
  8030. ***************************************/
  8031. /** Decompression context */
  8032. typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;
  8033. ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);
  8034. ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */
  8035. /** ZSTDv07_decompressDCtx() :
  8036. * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */
  8037. ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  8038. /*-************************
  8039. * Simple dictionary API
  8040. ***************************/
  8041. /*! ZSTDv07_decompress_usingDict() :
  8042. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  8043. * Dictionary must be identical to the one used during compression.
  8044. * Note : This function load the dictionary, resulting in a significant startup time */
  8045. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,
  8046. void* dst, size_t dstCapacity,
  8047. const void* src, size_t srcSize,
  8048. const void* dict,size_t dictSize);
  8049. /*-**************************
  8050. * Advanced Dictionary API
  8051. ****************************/
  8052. /*! ZSTDv07_createDDict() :
  8053. * Create a digested dictionary, ready to start decompression operation without startup delay.
  8054. * `dict` can be released after creation */
  8055. typedef struct ZSTDv07_DDict_s ZSTDv07_DDict;
  8056. ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);
  8057. ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);
  8058. /*! ZSTDv07_decompress_usingDDict() :
  8059. * Decompression using a pre-digested Dictionary
  8060. * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */
  8061. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,
  8062. void* dst, size_t dstCapacity,
  8063. const void* src, size_t srcSize,
  8064. const ZSTDv07_DDict* ddict);
  8065. typedef struct {
  8066. unsigned long long frameContentSize;
  8067. unsigned windowSize;
  8068. unsigned dictID;
  8069. unsigned checksumFlag;
  8070. } ZSTDv07_frameParams;
  8071. ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  8072. /* *************************************
  8073. * Streaming functions
  8074. ***************************************/
  8075. typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;
  8076. ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);
  8077. ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);
  8078. ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);
  8079. ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);
  8080. ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,
  8081. void* dst, size_t* dstCapacityPtr,
  8082. const void* src, size_t* srcSizePtr);
  8083. /*-***************************************************************************
  8084. * Streaming decompression howto
  8085. *
  8086. * A ZBUFFv07_DCtx object is required to track streaming operations.
  8087. * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.
  8088. * Use ZBUFFv07_decompressInit() to start a new decompression operation,
  8089. * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.
  8090. * Note that ZBUFFv07_DCtx objects can be re-init multiple times.
  8091. *
  8092. * Use ZBUFFv07_decompressContinue() repetitively to consume your input.
  8093. * *srcSizePtr and *dstCapacityPtr can be any size.
  8094. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  8095. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  8096. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  8097. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  8098. * or 0 when a frame is completely decoded,
  8099. * or an error code, which can be tested using ZBUFFv07_isError().
  8100. *
  8101. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()
  8102. * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  8103. * input : ZBUFFv07_recommendedDInSize == 128KB + 3;
  8104. * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  8105. * *******************************************************************************/
  8106. /* *************************************
  8107. * Tool functions
  8108. ***************************************/
  8109. ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);
  8110. ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);
  8111. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  8112. * These sizes are just hints, they tend to offer better latency */
  8113. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);
  8114. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);
  8115. /*-*************************************
  8116. * Constants
  8117. ***************************************/
  8118. #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */
  8119. #if defined (__cplusplus)
  8120. }
  8121. #endif
  8122. #endif /* ZSTDv07_H_235446 */
  8123. /**** ended inlining zstd_v07.h ****/
  8124. #endif
  8125. /** ZSTD_isLegacy() :
  8126. @return : > 0 if supported by legacy decoder. 0 otherwise.
  8127. return value is the version.
  8128. */
  8129. MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
  8130. {
  8131. U32 magicNumberLE;
  8132. if (srcSize<4) return 0;
  8133. magicNumberLE = MEM_readLE32(src);
  8134. switch(magicNumberLE)
  8135. {
  8136. #if (ZSTD_LEGACY_SUPPORT <= 1)
  8137. case ZSTDv01_magicNumberLE:return 1;
  8138. #endif
  8139. #if (ZSTD_LEGACY_SUPPORT <= 2)
  8140. case ZSTDv02_magicNumber : return 2;
  8141. #endif
  8142. #if (ZSTD_LEGACY_SUPPORT <= 3)
  8143. case ZSTDv03_magicNumber : return 3;
  8144. #endif
  8145. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8146. case ZSTDv04_magicNumber : return 4;
  8147. #endif
  8148. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8149. case ZSTDv05_MAGICNUMBER : return 5;
  8150. #endif
  8151. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8152. case ZSTDv06_MAGICNUMBER : return 6;
  8153. #endif
  8154. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8155. case ZSTDv07_MAGICNUMBER : return 7;
  8156. #endif
  8157. default : return 0;
  8158. }
  8159. }
  8160. MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
  8161. {
  8162. U32 const version = ZSTD_isLegacy(src, srcSize);
  8163. if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */
  8164. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8165. if (version==5) {
  8166. ZSTDv05_parameters fParams;
  8167. size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);
  8168. if (frResult != 0) return 0;
  8169. return fParams.srcSize;
  8170. }
  8171. #endif
  8172. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8173. if (version==6) {
  8174. ZSTDv06_frameParams fParams;
  8175. size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);
  8176. if (frResult != 0) return 0;
  8177. return fParams.frameContentSize;
  8178. }
  8179. #endif
  8180. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8181. if (version==7) {
  8182. ZSTDv07_frameParams fParams;
  8183. size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize);
  8184. if (frResult != 0) return 0;
  8185. return fParams.frameContentSize;
  8186. }
  8187. #endif
  8188. return 0; /* should not be possible */
  8189. }
  8190. MEM_STATIC size_t ZSTD_decompressLegacy(
  8191. void* dst, size_t dstCapacity,
  8192. const void* src, size_t compressedSize,
  8193. const void* dict,size_t dictSize)
  8194. {
  8195. U32 const version = ZSTD_isLegacy(src, compressedSize);
  8196. (void)dst; (void)dstCapacity; (void)dict; (void)dictSize; /* unused when ZSTD_LEGACY_SUPPORT >= 8 */
  8197. switch(version)
  8198. {
  8199. #if (ZSTD_LEGACY_SUPPORT <= 1)
  8200. case 1 :
  8201. return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
  8202. #endif
  8203. #if (ZSTD_LEGACY_SUPPORT <= 2)
  8204. case 2 :
  8205. return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
  8206. #endif
  8207. #if (ZSTD_LEGACY_SUPPORT <= 3)
  8208. case 3 :
  8209. return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
  8210. #endif
  8211. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8212. case 4 :
  8213. return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
  8214. #endif
  8215. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8216. case 5 :
  8217. { size_t result;
  8218. ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
  8219. if (zd==NULL) return ERROR(memory_allocation);
  8220. result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  8221. ZSTDv05_freeDCtx(zd);
  8222. return result;
  8223. }
  8224. #endif
  8225. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8226. case 6 :
  8227. { size_t result;
  8228. ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
  8229. if (zd==NULL) return ERROR(memory_allocation);
  8230. result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  8231. ZSTDv06_freeDCtx(zd);
  8232. return result;
  8233. }
  8234. #endif
  8235. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8236. case 7 :
  8237. { size_t result;
  8238. ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();
  8239. if (zd==NULL) return ERROR(memory_allocation);
  8240. result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  8241. ZSTDv07_freeDCtx(zd);
  8242. return result;
  8243. }
  8244. #endif
  8245. default :
  8246. return ERROR(prefix_unknown);
  8247. }
  8248. }
  8249. MEM_STATIC ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)
  8250. {
  8251. ZSTD_frameSizeInfo frameSizeInfo;
  8252. U32 const version = ZSTD_isLegacy(src, srcSize);
  8253. switch(version)
  8254. {
  8255. #if (ZSTD_LEGACY_SUPPORT <= 1)
  8256. case 1 :
  8257. ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,
  8258. &frameSizeInfo.compressedSize,
  8259. &frameSizeInfo.decompressedBound);
  8260. break;
  8261. #endif
  8262. #if (ZSTD_LEGACY_SUPPORT <= 2)
  8263. case 2 :
  8264. ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,
  8265. &frameSizeInfo.compressedSize,
  8266. &frameSizeInfo.decompressedBound);
  8267. break;
  8268. #endif
  8269. #if (ZSTD_LEGACY_SUPPORT <= 3)
  8270. case 3 :
  8271. ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,
  8272. &frameSizeInfo.compressedSize,
  8273. &frameSizeInfo.decompressedBound);
  8274. break;
  8275. #endif
  8276. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8277. case 4 :
  8278. ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,
  8279. &frameSizeInfo.compressedSize,
  8280. &frameSizeInfo.decompressedBound);
  8281. break;
  8282. #endif
  8283. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8284. case 5 :
  8285. ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,
  8286. &frameSizeInfo.compressedSize,
  8287. &frameSizeInfo.decompressedBound);
  8288. break;
  8289. #endif
  8290. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8291. case 6 :
  8292. ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,
  8293. &frameSizeInfo.compressedSize,
  8294. &frameSizeInfo.decompressedBound);
  8295. break;
  8296. #endif
  8297. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8298. case 7 :
  8299. ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,
  8300. &frameSizeInfo.compressedSize,
  8301. &frameSizeInfo.decompressedBound);
  8302. break;
  8303. #endif
  8304. default :
  8305. frameSizeInfo.compressedSize = ERROR(prefix_unknown);
  8306. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  8307. break;
  8308. }
  8309. if (!ZSTD_isError(frameSizeInfo.compressedSize) && frameSizeInfo.compressedSize > srcSize) {
  8310. frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
  8311. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  8312. }
  8313. return frameSizeInfo;
  8314. }
  8315. MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)
  8316. {
  8317. ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);
  8318. return frameSizeInfo.compressedSize;
  8319. }
  8320. MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
  8321. {
  8322. switch(version)
  8323. {
  8324. default :
  8325. case 1 :
  8326. case 2 :
  8327. case 3 :
  8328. (void)legacyContext;
  8329. return ERROR(version_unsupported);
  8330. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8331. case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
  8332. #endif
  8333. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8334. case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
  8335. #endif
  8336. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8337. case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
  8338. #endif
  8339. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8340. case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
  8341. #endif
  8342. }
  8343. }
  8344. MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
  8345. const void* dict, size_t dictSize)
  8346. {
  8347. DEBUGLOG(5, "ZSTD_initLegacyStream for v0.%u", newVersion);
  8348. if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
  8349. switch(newVersion)
  8350. {
  8351. default :
  8352. case 1 :
  8353. case 2 :
  8354. case 3 :
  8355. (void)dict; (void)dictSize;
  8356. return 0;
  8357. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8358. case 4 :
  8359. {
  8360. ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
  8361. if (dctx==NULL) return ERROR(memory_allocation);
  8362. ZBUFFv04_decompressInit(dctx);
  8363. ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
  8364. *legacyContext = dctx;
  8365. return 0;
  8366. }
  8367. #endif
  8368. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8369. case 5 :
  8370. {
  8371. ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
  8372. if (dctx==NULL) return ERROR(memory_allocation);
  8373. ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
  8374. *legacyContext = dctx;
  8375. return 0;
  8376. }
  8377. #endif
  8378. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8379. case 6 :
  8380. {
  8381. ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
  8382. if (dctx==NULL) return ERROR(memory_allocation);
  8383. ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
  8384. *legacyContext = dctx;
  8385. return 0;
  8386. }
  8387. #endif
  8388. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8389. case 7 :
  8390. {
  8391. ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
  8392. if (dctx==NULL) return ERROR(memory_allocation);
  8393. ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
  8394. *legacyContext = dctx;
  8395. return 0;
  8396. }
  8397. #endif
  8398. }
  8399. }
  8400. MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
  8401. ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  8402. {
  8403. DEBUGLOG(5, "ZSTD_decompressLegacyStream for v0.%u", version);
  8404. switch(version)
  8405. {
  8406. default :
  8407. case 1 :
  8408. case 2 :
  8409. case 3 :
  8410. (void)legacyContext; (void)output; (void)input;
  8411. return ERROR(version_unsupported);
  8412. #if (ZSTD_LEGACY_SUPPORT <= 4)
  8413. case 4 :
  8414. {
  8415. ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
  8416. const void* src = (const char*)input->src + input->pos;
  8417. size_t readSize = input->size - input->pos;
  8418. void* dst = (char*)output->dst + output->pos;
  8419. size_t decodedSize = output->size - output->pos;
  8420. size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  8421. output->pos += decodedSize;
  8422. input->pos += readSize;
  8423. return hintSize;
  8424. }
  8425. #endif
  8426. #if (ZSTD_LEGACY_SUPPORT <= 5)
  8427. case 5 :
  8428. {
  8429. ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
  8430. const void* src = (const char*)input->src + input->pos;
  8431. size_t readSize = input->size - input->pos;
  8432. void* dst = (char*)output->dst + output->pos;
  8433. size_t decodedSize = output->size - output->pos;
  8434. size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  8435. output->pos += decodedSize;
  8436. input->pos += readSize;
  8437. return hintSize;
  8438. }
  8439. #endif
  8440. #if (ZSTD_LEGACY_SUPPORT <= 6)
  8441. case 6 :
  8442. {
  8443. ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
  8444. const void* src = (const char*)input->src + input->pos;
  8445. size_t readSize = input->size - input->pos;
  8446. void* dst = (char*)output->dst + output->pos;
  8447. size_t decodedSize = output->size - output->pos;
  8448. size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  8449. output->pos += decodedSize;
  8450. input->pos += readSize;
  8451. return hintSize;
  8452. }
  8453. #endif
  8454. #if (ZSTD_LEGACY_SUPPORT <= 7)
  8455. case 7 :
  8456. {
  8457. ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
  8458. const void* src = (const char*)input->src + input->pos;
  8459. size_t readSize = input->size - input->pos;
  8460. void* dst = (char*)output->dst + output->pos;
  8461. size_t decodedSize = output->size - output->pos;
  8462. size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  8463. output->pos += decodedSize;
  8464. input->pos += readSize;
  8465. return hintSize;
  8466. }
  8467. #endif
  8468. }
  8469. }
  8470. #if defined (__cplusplus)
  8471. }
  8472. #endif
  8473. #endif /* ZSTD_LEGACY_H */
  8474. /**** ended inlining ../legacy/zstd_legacy.h ****/
  8475. #endif
  8476. /*-*******************************************************
  8477. * Types
  8478. *********************************************************/
  8479. struct ZSTD_DDict_s {
  8480. void* dictBuffer;
  8481. const void* dictContent;
  8482. size_t dictSize;
  8483. ZSTD_entropyDTables_t entropy;
  8484. U32 dictID;
  8485. U32 entropyPresent;
  8486. ZSTD_customMem cMem;
  8487. }; /* typedef'd to ZSTD_DDict within "zstd.h" */
  8488. const void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict)
  8489. {
  8490. assert(ddict != NULL);
  8491. return ddict->dictContent;
  8492. }
  8493. size_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict)
  8494. {
  8495. assert(ddict != NULL);
  8496. return ddict->dictSize;
  8497. }
  8498. void ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  8499. {
  8500. DEBUGLOG(4, "ZSTD_copyDDictParameters");
  8501. assert(dctx != NULL);
  8502. assert(ddict != NULL);
  8503. dctx->dictID = ddict->dictID;
  8504. dctx->prefixStart = ddict->dictContent;
  8505. dctx->virtualStart = ddict->dictContent;
  8506. dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
  8507. dctx->previousDstEnd = dctx->dictEnd;
  8508. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  8509. dctx->dictContentBeginForFuzzing = dctx->prefixStart;
  8510. dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
  8511. #endif
  8512. if (ddict->entropyPresent) {
  8513. dctx->litEntropy = 1;
  8514. dctx->fseEntropy = 1;
  8515. dctx->LLTptr = ddict->entropy.LLTable;
  8516. dctx->MLTptr = ddict->entropy.MLTable;
  8517. dctx->OFTptr = ddict->entropy.OFTable;
  8518. dctx->HUFptr = ddict->entropy.hufTable;
  8519. dctx->entropy.rep[0] = ddict->entropy.rep[0];
  8520. dctx->entropy.rep[1] = ddict->entropy.rep[1];
  8521. dctx->entropy.rep[2] = ddict->entropy.rep[2];
  8522. } else {
  8523. dctx->litEntropy = 0;
  8524. dctx->fseEntropy = 0;
  8525. }
  8526. }
  8527. static size_t
  8528. ZSTD_loadEntropy_intoDDict(ZSTD_DDict* ddict,
  8529. ZSTD_dictContentType_e dictContentType)
  8530. {
  8531. ddict->dictID = 0;
  8532. ddict->entropyPresent = 0;
  8533. if (dictContentType == ZSTD_dct_rawContent) return 0;
  8534. if (ddict->dictSize < 8) {
  8535. if (dictContentType == ZSTD_dct_fullDict)
  8536. return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
  8537. return 0; /* pure content mode */
  8538. }
  8539. { U32 const magic = MEM_readLE32(ddict->dictContent);
  8540. if (magic != ZSTD_MAGIC_DICTIONARY) {
  8541. if (dictContentType == ZSTD_dct_fullDict)
  8542. return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
  8543. return 0; /* pure content mode */
  8544. }
  8545. }
  8546. ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
  8547. /* load entropy tables */
  8548. RETURN_ERROR_IF(ZSTD_isError(ZSTD_loadDEntropy(
  8549. &ddict->entropy, ddict->dictContent, ddict->dictSize)),
  8550. dictionary_corrupted, "");
  8551. ddict->entropyPresent = 1;
  8552. return 0;
  8553. }
  8554. static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,
  8555. const void* dict, size_t dictSize,
  8556. ZSTD_dictLoadMethod_e dictLoadMethod,
  8557. ZSTD_dictContentType_e dictContentType)
  8558. {
  8559. if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
  8560. ddict->dictBuffer = NULL;
  8561. ddict->dictContent = dict;
  8562. if (!dict) dictSize = 0;
  8563. } else {
  8564. void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);
  8565. ddict->dictBuffer = internalBuffer;
  8566. ddict->dictContent = internalBuffer;
  8567. if (!internalBuffer) return ERROR(memory_allocation);
  8568. memcpy(internalBuffer, dict, dictSize);
  8569. }
  8570. ddict->dictSize = dictSize;
  8571. ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
  8572. /* parse dictionary content */
  8573. FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , "");
  8574. return 0;
  8575. }
  8576. ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
  8577. ZSTD_dictLoadMethod_e dictLoadMethod,
  8578. ZSTD_dictContentType_e dictContentType,
  8579. ZSTD_customMem customMem)
  8580. {
  8581. if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
  8582. { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
  8583. if (ddict == NULL) return NULL;
  8584. ddict->cMem = customMem;
  8585. { size_t const initResult = ZSTD_initDDict_internal(ddict,
  8586. dict, dictSize,
  8587. dictLoadMethod, dictContentType);
  8588. if (ZSTD_isError(initResult)) {
  8589. ZSTD_freeDDict(ddict);
  8590. return NULL;
  8591. } }
  8592. return ddict;
  8593. }
  8594. }
  8595. /*! ZSTD_createDDict() :
  8596. * Create a digested dictionary, to start decompression without startup delay.
  8597. * `dict` content is copied inside DDict.
  8598. * Consequently, `dict` can be released after `ZSTD_DDict` creation */
  8599. ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
  8600. {
  8601. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  8602. return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);
  8603. }
  8604. /*! ZSTD_createDDict_byReference() :
  8605. * Create a digested dictionary, to start decompression without startup delay.
  8606. * Dictionary content is simply referenced, it will be accessed during decompression.
  8607. * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */
  8608. ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)
  8609. {
  8610. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  8611. return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
  8612. }
  8613. const ZSTD_DDict* ZSTD_initStaticDDict(
  8614. void* sBuffer, size_t sBufferSize,
  8615. const void* dict, size_t dictSize,
  8616. ZSTD_dictLoadMethod_e dictLoadMethod,
  8617. ZSTD_dictContentType_e dictContentType)
  8618. {
  8619. size_t const neededSpace = sizeof(ZSTD_DDict)
  8620. + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  8621. ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
  8622. assert(sBuffer != NULL);
  8623. assert(dict != NULL);
  8624. if ((size_t)sBuffer & 7) return NULL; /* 8-aligned */
  8625. if (sBufferSize < neededSpace) return NULL;
  8626. if (dictLoadMethod == ZSTD_dlm_byCopy) {
  8627. memcpy(ddict+1, dict, dictSize); /* local copy */
  8628. dict = ddict+1;
  8629. }
  8630. if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
  8631. dict, dictSize,
  8632. ZSTD_dlm_byRef, dictContentType) ))
  8633. return NULL;
  8634. return ddict;
  8635. }
  8636. size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
  8637. {
  8638. if (ddict==NULL) return 0; /* support free on NULL */
  8639. { ZSTD_customMem const cMem = ddict->cMem;
  8640. ZSTD_free(ddict->dictBuffer, cMem);
  8641. ZSTD_free(ddict, cMem);
  8642. return 0;
  8643. }
  8644. }
  8645. /*! ZSTD_estimateDDictSize() :
  8646. * Estimate amount of memory that will be needed to create a dictionary for decompression.
  8647. * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */
  8648. size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
  8649. {
  8650. return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  8651. }
  8652. size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
  8653. {
  8654. if (ddict==NULL) return 0; /* support sizeof on NULL */
  8655. return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
  8656. }
  8657. /*! ZSTD_getDictID_fromDDict() :
  8658. * Provides the dictID of the dictionary loaded into `ddict`.
  8659. * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
  8660. * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
  8661. unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)
  8662. {
  8663. if (ddict==NULL) return 0;
  8664. return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize);
  8665. }
  8666. /**** ended inlining decompress/zstd_ddict.c ****/
  8667. /**** start inlining decompress/zstd_decompress.c ****/
  8668. /*
  8669. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  8670. * All rights reserved.
  8671. *
  8672. * This source code is licensed under both the BSD-style license (found in the
  8673. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8674. * in the COPYING file in the root directory of this source tree).
  8675. * You may select, at your option, one of the above-listed licenses.
  8676. */
  8677. /* ***************************************************************
  8678. * Tuning parameters
  8679. *****************************************************************/
  8680. /*!
  8681. * HEAPMODE :
  8682. * Select how default decompression function ZSTD_decompress() allocates its context,
  8683. * on stack (0), or into heap (1, default; requires malloc()).
  8684. * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.
  8685. */
  8686. #ifndef ZSTD_HEAPMODE
  8687. # define ZSTD_HEAPMODE 1
  8688. #endif
  8689. /*!
  8690. * LEGACY_SUPPORT :
  8691. * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)
  8692. */
  8693. #ifndef ZSTD_LEGACY_SUPPORT
  8694. # define ZSTD_LEGACY_SUPPORT 0
  8695. #endif
  8696. /*!
  8697. * MAXWINDOWSIZE_DEFAULT :
  8698. * maximum window size accepted by DStream __by default__.
  8699. * Frames requiring more memory will be rejected.
  8700. * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().
  8701. */
  8702. #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
  8703. # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
  8704. #endif
  8705. /*!
  8706. * NO_FORWARD_PROGRESS_MAX :
  8707. * maximum allowed nb of calls to ZSTD_decompressStream()
  8708. * without any forward progress
  8709. * (defined as: no byte read from input, and no byte flushed to output)
  8710. * before triggering an error.
  8711. */
  8712. #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
  8713. # define ZSTD_NO_FORWARD_PROGRESS_MAX 16
  8714. #endif
  8715. /*-*******************************************************
  8716. * Dependencies
  8717. *********************************************************/
  8718. /**** skipping file: ../common/cpu.h ****/
  8719. /**** skipping file: ../common/mem.h ****/
  8720. #define FSE_STATIC_LINKING_ONLY
  8721. /**** skipping file: ../common/fse.h ****/
  8722. #define HUF_STATIC_LINKING_ONLY
  8723. /**** skipping file: ../common/huf.h ****/
  8724. /**** skipping file: ../common/zstd_internal.h ****/
  8725. /**** skipping file: zstd_decompress_internal.h ****/
  8726. /**** skipping file: zstd_ddict.h ****/
  8727. /**** start inlining zstd_decompress_block.h ****/
  8728. /*
  8729. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  8730. * All rights reserved.
  8731. *
  8732. * This source code is licensed under both the BSD-style license (found in the
  8733. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8734. * in the COPYING file in the root directory of this source tree).
  8735. * You may select, at your option, one of the above-listed licenses.
  8736. */
  8737. #ifndef ZSTD_DEC_BLOCK_H
  8738. #define ZSTD_DEC_BLOCK_H
  8739. /*-*******************************************************
  8740. * Dependencies
  8741. *********************************************************/
  8742. #include <stddef.h> /* size_t */
  8743. /**** skipping file: ../zstd.h ****/
  8744. /**** skipping file: ../common/zstd_internal.h ****/
  8745. /**** skipping file: zstd_decompress_internal.h ****/
  8746. /* === Prototypes === */
  8747. /* note: prototypes already published within `zstd.h` :
  8748. * ZSTD_decompressBlock()
  8749. */
  8750. /* note: prototypes already published within `zstd_internal.h` :
  8751. * ZSTD_getcBlockSize()
  8752. * ZSTD_decodeSeqHeaders()
  8753. */
  8754. /* ZSTD_decompressBlock_internal() :
  8755. * decompress block, starting at `src`,
  8756. * into destination buffer `dst`.
  8757. * @return : decompressed block size,
  8758. * or an error code (which can be tested using ZSTD_isError())
  8759. */
  8760. size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
  8761. void* dst, size_t dstCapacity,
  8762. const void* src, size_t srcSize, const int frame);
  8763. /* ZSTD_buildFSETable() :
  8764. * generate FSE decoding table for one symbol (ll, ml or off)
  8765. * this function must be called with valid parameters only
  8766. * (dt is large enough, normalizedCounter distribution total is a power of 2, max is within range, etc.)
  8767. * in which case it cannot fail.
  8768. * Internal use only.
  8769. */
  8770. void ZSTD_buildFSETable(ZSTD_seqSymbol* dt,
  8771. const short* normalizedCounter, unsigned maxSymbolValue,
  8772. const U32* baseValue, const U32* nbAdditionalBits,
  8773. unsigned tableLog);
  8774. #endif /* ZSTD_DEC_BLOCK_H */
  8775. /**** ended inlining zstd_decompress_block.h ****/
  8776. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  8777. /**** skipping file: ../legacy/zstd_legacy.h ****/
  8778. #endif
  8779. /*-*************************************************************
  8780. * Context management
  8781. ***************************************************************/
  8782. size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
  8783. {
  8784. if (dctx==NULL) return 0; /* support sizeof NULL */
  8785. return sizeof(*dctx)
  8786. + ZSTD_sizeof_DDict(dctx->ddictLocal)
  8787. + dctx->inBuffSize + dctx->outBuffSize;
  8788. }
  8789. size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
  8790. static size_t ZSTD_startingInputLength(ZSTD_format_e format)
  8791. {
  8792. size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format);
  8793. /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
  8794. assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
  8795. return startingInputLength;
  8796. }
  8797. static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
  8798. {
  8799. dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
  8800. dctx->staticSize = 0;
  8801. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  8802. dctx->ddict = NULL;
  8803. dctx->ddictLocal = NULL;
  8804. dctx->dictEnd = NULL;
  8805. dctx->ddictIsCold = 0;
  8806. dctx->dictUses = ZSTD_dont_use;
  8807. dctx->inBuff = NULL;
  8808. dctx->inBuffSize = 0;
  8809. dctx->outBuffSize = 0;
  8810. dctx->streamStage = zdss_init;
  8811. dctx->legacyContext = NULL;
  8812. dctx->previousLegacyVersion = 0;
  8813. dctx->noForwardProgress = 0;
  8814. dctx->oversizedDuration = 0;
  8815. dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
  8816. dctx->outBufferMode = ZSTD_obm_buffered;
  8817. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  8818. dctx->dictContentEndForFuzzing = NULL;
  8819. #endif
  8820. }
  8821. ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
  8822. {
  8823. ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
  8824. if ((size_t)workspace & 7) return NULL; /* 8-aligned */
  8825. if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
  8826. ZSTD_initDCtx_internal(dctx);
  8827. dctx->staticSize = workspaceSize;
  8828. dctx->inBuff = (char*)(dctx+1);
  8829. return dctx;
  8830. }
  8831. ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
  8832. {
  8833. if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
  8834. { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
  8835. if (!dctx) return NULL;
  8836. dctx->customMem = customMem;
  8837. ZSTD_initDCtx_internal(dctx);
  8838. return dctx;
  8839. }
  8840. }
  8841. ZSTD_DCtx* ZSTD_createDCtx(void)
  8842. {
  8843. DEBUGLOG(3, "ZSTD_createDCtx");
  8844. return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
  8845. }
  8846. static void ZSTD_clearDict(ZSTD_DCtx* dctx)
  8847. {
  8848. ZSTD_freeDDict(dctx->ddictLocal);
  8849. dctx->ddictLocal = NULL;
  8850. dctx->ddict = NULL;
  8851. dctx->dictUses = ZSTD_dont_use;
  8852. }
  8853. size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
  8854. {
  8855. if (dctx==NULL) return 0; /* support free on NULL */
  8856. RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
  8857. { ZSTD_customMem const cMem = dctx->customMem;
  8858. ZSTD_clearDict(dctx);
  8859. ZSTD_free(dctx->inBuff, cMem);
  8860. dctx->inBuff = NULL;
  8861. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  8862. if (dctx->legacyContext)
  8863. ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
  8864. #endif
  8865. ZSTD_free(dctx, cMem);
  8866. return 0;
  8867. }
  8868. }
  8869. /* no longer useful */
  8870. void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
  8871. {
  8872. size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
  8873. memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
  8874. }
  8875. /*-*************************************************************
  8876. * Frame header decoding
  8877. ***************************************************************/
  8878. /*! ZSTD_isFrame() :
  8879. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  8880. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  8881. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  8882. * Note 3 : Skippable Frame Identifiers are considered valid. */
  8883. unsigned ZSTD_isFrame(const void* buffer, size_t size)
  8884. {
  8885. if (size < ZSTD_FRAMEIDSIZE) return 0;
  8886. { U32 const magic = MEM_readLE32(buffer);
  8887. if (magic == ZSTD_MAGICNUMBER) return 1;
  8888. if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
  8889. }
  8890. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  8891. if (ZSTD_isLegacy(buffer, size)) return 1;
  8892. #endif
  8893. return 0;
  8894. }
  8895. /** ZSTD_frameHeaderSize_internal() :
  8896. * srcSize must be large enough to reach header size fields.
  8897. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
  8898. * @return : size of the Frame Header
  8899. * or an error code, which can be tested with ZSTD_isError() */
  8900. static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
  8901. {
  8902. size_t const minInputSize = ZSTD_startingInputLength(format);
  8903. RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, "");
  8904. { BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
  8905. U32 const dictID= fhd & 3;
  8906. U32 const singleSegment = (fhd >> 5) & 1;
  8907. U32 const fcsId = fhd >> 6;
  8908. return minInputSize + !singleSegment
  8909. + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
  8910. + (singleSegment && !fcsId);
  8911. }
  8912. }
  8913. /** ZSTD_frameHeaderSize() :
  8914. * srcSize must be >= ZSTD_frameHeaderSize_prefix.
  8915. * @return : size of the Frame Header,
  8916. * or an error code (if srcSize is too small) */
  8917. size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
  8918. {
  8919. return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
  8920. }
  8921. /** ZSTD_getFrameHeader_advanced() :
  8922. * decode Frame Header, or require larger `srcSize`.
  8923. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
  8924. * @return : 0, `zfhPtr` is correctly filled,
  8925. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  8926. * or an error code, which can be tested using ZSTD_isError() */
  8927. size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
  8928. {
  8929. const BYTE* ip = (const BYTE*)src;
  8930. size_t const minInputSize = ZSTD_startingInputLength(format);
  8931. memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
  8932. if (srcSize < minInputSize) return minInputSize;
  8933. RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter");
  8934. if ( (format != ZSTD_f_zstd1_magicless)
  8935. && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
  8936. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  8937. /* skippable frame */
  8938. if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
  8939. return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
  8940. memset(zfhPtr, 0, sizeof(*zfhPtr));
  8941. zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
  8942. zfhPtr->frameType = ZSTD_skippableFrame;
  8943. return 0;
  8944. }
  8945. RETURN_ERROR(prefix_unknown, "");
  8946. }
  8947. /* ensure there is enough `srcSize` to fully read/decode frame header */
  8948. { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
  8949. if (srcSize < fhsize) return fhsize;
  8950. zfhPtr->headerSize = (U32)fhsize;
  8951. }
  8952. { BYTE const fhdByte = ip[minInputSize-1];
  8953. size_t pos = minInputSize;
  8954. U32 const dictIDSizeCode = fhdByte&3;
  8955. U32 const checksumFlag = (fhdByte>>2)&1;
  8956. U32 const singleSegment = (fhdByte>>5)&1;
  8957. U32 const fcsID = fhdByte>>6;
  8958. U64 windowSize = 0;
  8959. U32 dictID = 0;
  8960. U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
  8961. RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,
  8962. "reserved bits, must be zero");
  8963. if (!singleSegment) {
  8964. BYTE const wlByte = ip[pos++];
  8965. U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
  8966. RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, "");
  8967. windowSize = (1ULL << windowLog);
  8968. windowSize += (windowSize >> 3) * (wlByte&7);
  8969. }
  8970. switch(dictIDSizeCode)
  8971. {
  8972. default: assert(0); /* impossible */
  8973. case 0 : break;
  8974. case 1 : dictID = ip[pos]; pos++; break;
  8975. case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
  8976. case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
  8977. }
  8978. switch(fcsID)
  8979. {
  8980. default: assert(0); /* impossible */
  8981. case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
  8982. case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
  8983. case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
  8984. case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
  8985. }
  8986. if (singleSegment) windowSize = frameContentSize;
  8987. zfhPtr->frameType = ZSTD_frame;
  8988. zfhPtr->frameContentSize = frameContentSize;
  8989. zfhPtr->windowSize = windowSize;
  8990. zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  8991. zfhPtr->dictID = dictID;
  8992. zfhPtr->checksumFlag = checksumFlag;
  8993. }
  8994. return 0;
  8995. }
  8996. /** ZSTD_getFrameHeader() :
  8997. * decode Frame Header, or require larger `srcSize`.
  8998. * note : this function does not consume input, it only reads it.
  8999. * @return : 0, `zfhPtr` is correctly filled,
  9000. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  9001. * or an error code, which can be tested using ZSTD_isError() */
  9002. size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
  9003. {
  9004. return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
  9005. }
  9006. /** ZSTD_getFrameContentSize() :
  9007. * compatible with legacy mode
  9008. * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
  9009. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  9010. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
  9011. unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
  9012. {
  9013. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  9014. if (ZSTD_isLegacy(src, srcSize)) {
  9015. unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
  9016. return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
  9017. }
  9018. #endif
  9019. { ZSTD_frameHeader zfh;
  9020. if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
  9021. return ZSTD_CONTENTSIZE_ERROR;
  9022. if (zfh.frameType == ZSTD_skippableFrame) {
  9023. return 0;
  9024. } else {
  9025. return zfh.frameContentSize;
  9026. } }
  9027. }
  9028. static size_t readSkippableFrameSize(void const* src, size_t srcSize)
  9029. {
  9030. size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
  9031. U32 sizeU32;
  9032. RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
  9033. sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
  9034. RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,
  9035. frameParameter_unsupported, "");
  9036. {
  9037. size_t const skippableSize = skippableHeaderSize + sizeU32;
  9038. RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, "");
  9039. return skippableSize;
  9040. }
  9041. }
  9042. /** ZSTD_findDecompressedSize() :
  9043. * compatible with legacy mode
  9044. * `srcSize` must be the exact length of some number of ZSTD compressed and/or
  9045. * skippable frames
  9046. * @return : decompressed size of the frames contained */
  9047. unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
  9048. {
  9049. unsigned long long totalDstSize = 0;
  9050. while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) {
  9051. U32 const magicNumber = MEM_readLE32(src);
  9052. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  9053. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  9054. if (ZSTD_isError(skippableSize)) {
  9055. return ZSTD_CONTENTSIZE_ERROR;
  9056. }
  9057. assert(skippableSize <= srcSize);
  9058. src = (const BYTE *)src + skippableSize;
  9059. srcSize -= skippableSize;
  9060. continue;
  9061. }
  9062. { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  9063. if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;
  9064. /* check for overflow */
  9065. if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;
  9066. totalDstSize += ret;
  9067. }
  9068. { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
  9069. if (ZSTD_isError(frameSrcSize)) {
  9070. return ZSTD_CONTENTSIZE_ERROR;
  9071. }
  9072. src = (const BYTE *)src + frameSrcSize;
  9073. srcSize -= frameSrcSize;
  9074. }
  9075. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  9076. if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
  9077. return totalDstSize;
  9078. }
  9079. /** ZSTD_getDecompressedSize() :
  9080. * compatible with legacy mode
  9081. * @return : decompressed size if known, 0 otherwise
  9082. note : 0 can mean any of the following :
  9083. - frame content is empty
  9084. - decompressed size field is not present in frame header
  9085. - frame header unknown / not supported
  9086. - frame header not complete (`srcSize` too small) */
  9087. unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
  9088. {
  9089. unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  9090. ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
  9091. return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
  9092. }
  9093. /** ZSTD_decodeFrameHeader() :
  9094. * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
  9095. * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
  9096. static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
  9097. {
  9098. size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
  9099. if (ZSTD_isError(result)) return result; /* invalid header */
  9100. RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
  9101. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  9102. /* Skip the dictID check in fuzzing mode, because it makes the search
  9103. * harder.
  9104. */
  9105. RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
  9106. dictionary_wrong, "");
  9107. #endif
  9108. if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
  9109. return 0;
  9110. }
  9111. static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
  9112. {
  9113. ZSTD_frameSizeInfo frameSizeInfo;
  9114. frameSizeInfo.compressedSize = ret;
  9115. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  9116. return frameSizeInfo;
  9117. }
  9118. static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)
  9119. {
  9120. ZSTD_frameSizeInfo frameSizeInfo;
  9121. memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
  9122. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  9123. if (ZSTD_isLegacy(src, srcSize))
  9124. return ZSTD_findFrameSizeInfoLegacy(src, srcSize);
  9125. #endif
  9126. if ((srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
  9127. && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  9128. frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);
  9129. assert(ZSTD_isError(frameSizeInfo.compressedSize) ||
  9130. frameSizeInfo.compressedSize <= srcSize);
  9131. return frameSizeInfo;
  9132. } else {
  9133. const BYTE* ip = (const BYTE*)src;
  9134. const BYTE* const ipstart = ip;
  9135. size_t remainingSize = srcSize;
  9136. size_t nbBlocks = 0;
  9137. ZSTD_frameHeader zfh;
  9138. /* Extract Frame Header */
  9139. { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);
  9140. if (ZSTD_isError(ret))
  9141. return ZSTD_errorFrameSizeInfo(ret);
  9142. if (ret > 0)
  9143. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  9144. }
  9145. ip += zfh.headerSize;
  9146. remainingSize -= zfh.headerSize;
  9147. /* Iterate over each block */
  9148. while (1) {
  9149. blockProperties_t blockProperties;
  9150. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
  9151. if (ZSTD_isError(cBlockSize))
  9152. return ZSTD_errorFrameSizeInfo(cBlockSize);
  9153. if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
  9154. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  9155. ip += ZSTD_blockHeaderSize + cBlockSize;
  9156. remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
  9157. nbBlocks++;
  9158. if (blockProperties.lastBlock) break;
  9159. }
  9160. /* Final frame content checksum */
  9161. if (zfh.checksumFlag) {
  9162. if (remainingSize < 4)
  9163. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  9164. ip += 4;
  9165. }
  9166. frameSizeInfo.compressedSize = ip - ipstart;
  9167. frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
  9168. ? zfh.frameContentSize
  9169. : nbBlocks * zfh.blockSizeMax;
  9170. return frameSizeInfo;
  9171. }
  9172. }
  9173. /** ZSTD_findFrameCompressedSize() :
  9174. * compatible with legacy mode
  9175. * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
  9176. * `srcSize` must be at least as large as the frame contained
  9177. * @return : the compressed size of the frame starting at `src` */
  9178. size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
  9179. {
  9180. ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
  9181. return frameSizeInfo.compressedSize;
  9182. }
  9183. /** ZSTD_decompressBound() :
  9184. * compatible with legacy mode
  9185. * `src` must point to the start of a ZSTD frame or a skippeable frame
  9186. * `srcSize` must be at least as large as the frame contained
  9187. * @return : the maximum decompressed size of the compressed source
  9188. */
  9189. unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
  9190. {
  9191. unsigned long long bound = 0;
  9192. /* Iterate over each frame */
  9193. while (srcSize > 0) {
  9194. ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
  9195. size_t const compressedSize = frameSizeInfo.compressedSize;
  9196. unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
  9197. if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
  9198. return ZSTD_CONTENTSIZE_ERROR;
  9199. assert(srcSize >= compressedSize);
  9200. src = (const BYTE*)src + compressedSize;
  9201. srcSize -= compressedSize;
  9202. bound += decompressedBound;
  9203. }
  9204. return bound;
  9205. }
  9206. /*-*************************************************************
  9207. * Frame decoding
  9208. ***************************************************************/
  9209. /** ZSTD_insertBlock() :
  9210. * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
  9211. size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
  9212. {
  9213. DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
  9214. ZSTD_checkContinuity(dctx, blockStart);
  9215. dctx->previousDstEnd = (const char*)blockStart + blockSize;
  9216. return blockSize;
  9217. }
  9218. static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
  9219. const void* src, size_t srcSize)
  9220. {
  9221. DEBUGLOG(5, "ZSTD_copyRawBlock");
  9222. if (dst == NULL) {
  9223. if (srcSize == 0) return 0;
  9224. RETURN_ERROR(dstBuffer_null, "");
  9225. }
  9226. RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
  9227. memcpy(dst, src, srcSize);
  9228. return srcSize;
  9229. }
  9230. static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
  9231. BYTE b,
  9232. size_t regenSize)
  9233. {
  9234. if (dst == NULL) {
  9235. if (regenSize == 0) return 0;
  9236. RETURN_ERROR(dstBuffer_null, "");
  9237. }
  9238. RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
  9239. memset(dst, b, regenSize);
  9240. return regenSize;
  9241. }
  9242. /*! ZSTD_decompressFrame() :
  9243. * @dctx must be properly initialized
  9244. * will update *srcPtr and *srcSizePtr,
  9245. * to make *srcPtr progress by one frame. */
  9246. static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
  9247. void* dst, size_t dstCapacity,
  9248. const void** srcPtr, size_t *srcSizePtr)
  9249. {
  9250. const BYTE* ip = (const BYTE*)(*srcPtr);
  9251. BYTE* const ostart = (BYTE* const)dst;
  9252. BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;
  9253. BYTE* op = ostart;
  9254. size_t remainingSrcSize = *srcSizePtr;
  9255. DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
  9256. /* check */
  9257. RETURN_ERROR_IF(
  9258. remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize,
  9259. srcSize_wrong, "");
  9260. /* Frame Header */
  9261. { size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal(
  9262. ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format);
  9263. if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
  9264. RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,
  9265. srcSize_wrong, "");
  9266. FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , "");
  9267. ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
  9268. }
  9269. /* Loop on each block */
  9270. while (1) {
  9271. size_t decodedSize;
  9272. blockProperties_t blockProperties;
  9273. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
  9274. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  9275. ip += ZSTD_blockHeaderSize;
  9276. remainingSrcSize -= ZSTD_blockHeaderSize;
  9277. RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, "");
  9278. switch(blockProperties.blockType)
  9279. {
  9280. case bt_compressed:
  9281. decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);
  9282. break;
  9283. case bt_raw :
  9284. decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
  9285. break;
  9286. case bt_rle :
  9287. decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize);
  9288. break;
  9289. case bt_reserved :
  9290. default:
  9291. RETURN_ERROR(corruption_detected, "invalid block type");
  9292. }
  9293. if (ZSTD_isError(decodedSize)) return decodedSize;
  9294. if (dctx->fParams.checksumFlag)
  9295. XXH64_update(&dctx->xxhState, op, decodedSize);
  9296. if (decodedSize != 0)
  9297. op += decodedSize;
  9298. assert(ip != NULL);
  9299. ip += cBlockSize;
  9300. remainingSrcSize -= cBlockSize;
  9301. if (blockProperties.lastBlock) break;
  9302. }
  9303. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  9304. RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,
  9305. corruption_detected, "");
  9306. }
  9307. if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
  9308. U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
  9309. U32 checkRead;
  9310. RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
  9311. checkRead = MEM_readLE32(ip);
  9312. RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
  9313. ip += 4;
  9314. remainingSrcSize -= 4;
  9315. }
  9316. /* Allow caller to get size read */
  9317. *srcPtr = ip;
  9318. *srcSizePtr = remainingSrcSize;
  9319. return op-ostart;
  9320. }
  9321. static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
  9322. void* dst, size_t dstCapacity,
  9323. const void* src, size_t srcSize,
  9324. const void* dict, size_t dictSize,
  9325. const ZSTD_DDict* ddict)
  9326. {
  9327. void* const dststart = dst;
  9328. int moreThan1Frame = 0;
  9329. DEBUGLOG(5, "ZSTD_decompressMultiFrame");
  9330. assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
  9331. if (ddict) {
  9332. dict = ZSTD_DDict_dictContent(ddict);
  9333. dictSize = ZSTD_DDict_dictSize(ddict);
  9334. }
  9335. while (srcSize >= ZSTD_startingInputLength(dctx->format)) {
  9336. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  9337. if (ZSTD_isLegacy(src, srcSize)) {
  9338. size_t decodedSize;
  9339. size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  9340. if (ZSTD_isError(frameSize)) return frameSize;
  9341. RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
  9342. "legacy support is not compatible with static dctx");
  9343. decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
  9344. if (ZSTD_isError(decodedSize)) return decodedSize;
  9345. assert(decodedSize <=- dstCapacity);
  9346. dst = (BYTE*)dst + decodedSize;
  9347. dstCapacity -= decodedSize;
  9348. src = (const BYTE*)src + frameSize;
  9349. srcSize -= frameSize;
  9350. continue;
  9351. }
  9352. #endif
  9353. { U32 const magicNumber = MEM_readLE32(src);
  9354. DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
  9355. (unsigned)magicNumber, ZSTD_MAGICNUMBER);
  9356. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  9357. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  9358. FORWARD_IF_ERROR(skippableSize, "readSkippableFrameSize failed");
  9359. assert(skippableSize <= srcSize);
  9360. src = (const BYTE *)src + skippableSize;
  9361. srcSize -= skippableSize;
  9362. continue;
  9363. } }
  9364. if (ddict) {
  9365. /* we were called from ZSTD_decompress_usingDDict */
  9366. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), "");
  9367. } else {
  9368. /* this will initialize correctly with no dict if dict == NULL, so
  9369. * use this in all cases but ddict */
  9370. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), "");
  9371. }
  9372. ZSTD_checkContinuity(dctx, dst);
  9373. { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
  9374. &src, &srcSize);
  9375. RETURN_ERROR_IF(
  9376. (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
  9377. && (moreThan1Frame==1),
  9378. srcSize_wrong,
  9379. "at least one frame successfully completed, but following "
  9380. "bytes are garbage: it's more likely to be a srcSize error, "
  9381. "specifying more bytes than compressed size of frame(s). This "
  9382. "error message replaces ERROR(prefix_unknown), which would be "
  9383. "confusing, as the first header is actually correct. Note that "
  9384. "one could be unlucky, it might be a corruption error instead, "
  9385. "happening right at the place where we expect zstd magic "
  9386. "bytes. But this is _much_ less likely than a srcSize field "
  9387. "error.");
  9388. if (ZSTD_isError(res)) return res;
  9389. assert(res <= dstCapacity);
  9390. if (res != 0)
  9391. dst = (BYTE*)dst + res;
  9392. dstCapacity -= res;
  9393. }
  9394. moreThan1Frame = 1;
  9395. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  9396. RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
  9397. return (BYTE*)dst - (BYTE*)dststart;
  9398. }
  9399. size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  9400. void* dst, size_t dstCapacity,
  9401. const void* src, size_t srcSize,
  9402. const void* dict, size_t dictSize)
  9403. {
  9404. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
  9405. }
  9406. static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
  9407. {
  9408. switch (dctx->dictUses) {
  9409. default:
  9410. assert(0 /* Impossible */);
  9411. /* fall-through */
  9412. case ZSTD_dont_use:
  9413. ZSTD_clearDict(dctx);
  9414. return NULL;
  9415. case ZSTD_use_indefinitely:
  9416. return dctx->ddict;
  9417. case ZSTD_use_once:
  9418. dctx->dictUses = ZSTD_dont_use;
  9419. return dctx->ddict;
  9420. }
  9421. }
  9422. size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  9423. {
  9424. return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
  9425. }
  9426. size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  9427. {
  9428. #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
  9429. size_t regenSize;
  9430. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  9431. RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!");
  9432. regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
  9433. ZSTD_freeDCtx(dctx);
  9434. return regenSize;
  9435. #else /* stack mode */
  9436. ZSTD_DCtx dctx;
  9437. ZSTD_initDCtx_internal(&dctx);
  9438. return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
  9439. #endif
  9440. }
  9441. /*-**************************************
  9442. * Advanced Streaming Decompression API
  9443. * Bufferless and synchronous
  9444. ****************************************/
  9445. size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
  9446. /**
  9447. * Similar to ZSTD_nextSrcSizeToDecompress(), but when when a block input can be streamed,
  9448. * we allow taking a partial block as the input. Currently only raw uncompressed blocks can
  9449. * be streamed.
  9450. *
  9451. * For blocks that can be streamed, this allows us to reduce the latency until we produce
  9452. * output, and avoid copying the input.
  9453. *
  9454. * @param inputSize - The total amount of input that the caller currently has.
  9455. */
  9456. static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) {
  9457. if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock))
  9458. return dctx->expected;
  9459. if (dctx->bType != bt_raw)
  9460. return dctx->expected;
  9461. return MIN(MAX(inputSize, 1), dctx->expected);
  9462. }
  9463. ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
  9464. switch(dctx->stage)
  9465. {
  9466. default: /* should not happen */
  9467. assert(0);
  9468. case ZSTDds_getFrameHeaderSize:
  9469. case ZSTDds_decodeFrameHeader:
  9470. return ZSTDnit_frameHeader;
  9471. case ZSTDds_decodeBlockHeader:
  9472. return ZSTDnit_blockHeader;
  9473. case ZSTDds_decompressBlock:
  9474. return ZSTDnit_block;
  9475. case ZSTDds_decompressLastBlock:
  9476. return ZSTDnit_lastBlock;
  9477. case ZSTDds_checkChecksum:
  9478. return ZSTDnit_checksum;
  9479. case ZSTDds_decodeSkippableHeader:
  9480. case ZSTDds_skipFrame:
  9481. return ZSTDnit_skippableFrame;
  9482. }
  9483. }
  9484. static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
  9485. /** ZSTD_decompressContinue() :
  9486. * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
  9487. * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
  9488. * or an error code, which can be tested using ZSTD_isError() */
  9489. size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  9490. {
  9491. DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
  9492. /* Sanity check */
  9493. RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
  9494. if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
  9495. switch (dctx->stage)
  9496. {
  9497. case ZSTDds_getFrameHeaderSize :
  9498. assert(src != NULL);
  9499. if (dctx->format == ZSTD_f_zstd1) { /* allows header */
  9500. assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
  9501. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  9502. memcpy(dctx->headerBuffer, src, srcSize);
  9503. dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */
  9504. dctx->stage = ZSTDds_decodeSkippableHeader;
  9505. return 0;
  9506. } }
  9507. dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
  9508. if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
  9509. memcpy(dctx->headerBuffer, src, srcSize);
  9510. dctx->expected = dctx->headerSize - srcSize;
  9511. dctx->stage = ZSTDds_decodeFrameHeader;
  9512. return 0;
  9513. case ZSTDds_decodeFrameHeader:
  9514. assert(src != NULL);
  9515. memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
  9516. FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), "");
  9517. dctx->expected = ZSTD_blockHeaderSize;
  9518. dctx->stage = ZSTDds_decodeBlockHeader;
  9519. return 0;
  9520. case ZSTDds_decodeBlockHeader:
  9521. { blockProperties_t bp;
  9522. size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
  9523. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  9524. RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum");
  9525. dctx->expected = cBlockSize;
  9526. dctx->bType = bp.blockType;
  9527. dctx->rleSize = bp.origSize;
  9528. if (cBlockSize) {
  9529. dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
  9530. return 0;
  9531. }
  9532. /* empty block */
  9533. if (bp.lastBlock) {
  9534. if (dctx->fParams.checksumFlag) {
  9535. dctx->expected = 4;
  9536. dctx->stage = ZSTDds_checkChecksum;
  9537. } else {
  9538. dctx->expected = 0; /* end of frame */
  9539. dctx->stage = ZSTDds_getFrameHeaderSize;
  9540. }
  9541. } else {
  9542. dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */
  9543. dctx->stage = ZSTDds_decodeBlockHeader;
  9544. }
  9545. return 0;
  9546. }
  9547. case ZSTDds_decompressLastBlock:
  9548. case ZSTDds_decompressBlock:
  9549. DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
  9550. { size_t rSize;
  9551. switch(dctx->bType)
  9552. {
  9553. case bt_compressed:
  9554. DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
  9555. rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);
  9556. dctx->expected = 0; /* Streaming not supported */
  9557. break;
  9558. case bt_raw :
  9559. assert(srcSize <= dctx->expected);
  9560. rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
  9561. FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed");
  9562. assert(rSize == srcSize);
  9563. dctx->expected -= rSize;
  9564. break;
  9565. case bt_rle :
  9566. rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
  9567. dctx->expected = 0; /* Streaming not supported */
  9568. break;
  9569. case bt_reserved : /* should never happen */
  9570. default:
  9571. RETURN_ERROR(corruption_detected, "invalid block type");
  9572. }
  9573. FORWARD_IF_ERROR(rSize, "");
  9574. RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
  9575. DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
  9576. dctx->decodedSize += rSize;
  9577. if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
  9578. dctx->previousDstEnd = (char*)dst + rSize;
  9579. /* Stay on the same stage until we are finished streaming the block. */
  9580. if (dctx->expected > 0) {
  9581. return rSize;
  9582. }
  9583. if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
  9584. DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
  9585. RETURN_ERROR_IF(
  9586. dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  9587. && dctx->decodedSize != dctx->fParams.frameContentSize,
  9588. corruption_detected, "");
  9589. if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
  9590. dctx->expected = 4;
  9591. dctx->stage = ZSTDds_checkChecksum;
  9592. } else {
  9593. dctx->expected = 0; /* ends here */
  9594. dctx->stage = ZSTDds_getFrameHeaderSize;
  9595. }
  9596. } else {
  9597. dctx->stage = ZSTDds_decodeBlockHeader;
  9598. dctx->expected = ZSTD_blockHeaderSize;
  9599. }
  9600. return rSize;
  9601. }
  9602. case ZSTDds_checkChecksum:
  9603. assert(srcSize == 4); /* guaranteed by dctx->expected */
  9604. { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
  9605. U32 const check32 = MEM_readLE32(src);
  9606. DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
  9607. RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
  9608. dctx->expected = 0;
  9609. dctx->stage = ZSTDds_getFrameHeaderSize;
  9610. return 0;
  9611. }
  9612. case ZSTDds_decodeSkippableHeader:
  9613. assert(src != NULL);
  9614. assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
  9615. memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
  9616. dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
  9617. dctx->stage = ZSTDds_skipFrame;
  9618. return 0;
  9619. case ZSTDds_skipFrame:
  9620. dctx->expected = 0;
  9621. dctx->stage = ZSTDds_getFrameHeaderSize;
  9622. return 0;
  9623. default:
  9624. assert(0); /* impossible */
  9625. RETURN_ERROR(GENERIC, "impossible to reach"); /* some compiler require default to do something */
  9626. }
  9627. }
  9628. static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  9629. {
  9630. dctx->dictEnd = dctx->previousDstEnd;
  9631. dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
  9632. dctx->prefixStart = dict;
  9633. dctx->previousDstEnd = (const char*)dict + dictSize;
  9634. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  9635. dctx->dictContentBeginForFuzzing = dctx->prefixStart;
  9636. dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
  9637. #endif
  9638. return 0;
  9639. }
  9640. /*! ZSTD_loadDEntropy() :
  9641. * dict : must point at beginning of a valid zstd dictionary.
  9642. * @return : size of entropy tables read */
  9643. size_t
  9644. ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  9645. const void* const dict, size_t const dictSize)
  9646. {
  9647. const BYTE* dictPtr = (const BYTE*)dict;
  9648. const BYTE* const dictEnd = dictPtr + dictSize;
  9649. RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small");
  9650. assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */
  9651. dictPtr += 8; /* skip header = magic + dictID */
  9652. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
  9653. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
  9654. ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
  9655. { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */
  9656. size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
  9657. #ifdef HUF_FORCE_DECOMPRESS_X1
  9658. /* in minimal huffman, we always use X1 variants */
  9659. size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
  9660. dictPtr, dictEnd - dictPtr,
  9661. workspace, workspaceSize);
  9662. #else
  9663. size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
  9664. dictPtr, dictEnd - dictPtr,
  9665. workspace, workspaceSize);
  9666. #endif
  9667. RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, "");
  9668. dictPtr += hSize;
  9669. }
  9670. { short offcodeNCount[MaxOff+1];
  9671. unsigned offcodeMaxValue = MaxOff, offcodeLog;
  9672. size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
  9673. RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, "");
  9674. RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, "");
  9675. RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, "");
  9676. ZSTD_buildFSETable( entropy->OFTable,
  9677. offcodeNCount, offcodeMaxValue,
  9678. OF_base, OF_bits,
  9679. offcodeLog);
  9680. dictPtr += offcodeHeaderSize;
  9681. }
  9682. { short matchlengthNCount[MaxML+1];
  9683. unsigned matchlengthMaxValue = MaxML, matchlengthLog;
  9684. size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
  9685. RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, "");
  9686. RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, "");
  9687. RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, "");
  9688. ZSTD_buildFSETable( entropy->MLTable,
  9689. matchlengthNCount, matchlengthMaxValue,
  9690. ML_base, ML_bits,
  9691. matchlengthLog);
  9692. dictPtr += matchlengthHeaderSize;
  9693. }
  9694. { short litlengthNCount[MaxLL+1];
  9695. unsigned litlengthMaxValue = MaxLL, litlengthLog;
  9696. size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
  9697. RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, "");
  9698. RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, "");
  9699. RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, "");
  9700. ZSTD_buildFSETable( entropy->LLTable,
  9701. litlengthNCount, litlengthMaxValue,
  9702. LL_base, LL_bits,
  9703. litlengthLog);
  9704. dictPtr += litlengthHeaderSize;
  9705. }
  9706. RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, "");
  9707. { int i;
  9708. size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
  9709. for (i=0; i<3; i++) {
  9710. U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
  9711. RETURN_ERROR_IF(rep==0 || rep > dictContentSize,
  9712. dictionary_corrupted, "");
  9713. entropy->rep[i] = rep;
  9714. } }
  9715. return dictPtr - (const BYTE*)dict;
  9716. }
  9717. static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  9718. {
  9719. if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
  9720. { U32 const magic = MEM_readLE32(dict);
  9721. if (magic != ZSTD_MAGIC_DICTIONARY) {
  9722. return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
  9723. } }
  9724. dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  9725. /* load entropy tables */
  9726. { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
  9727. RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, "");
  9728. dict = (const char*)dict + eSize;
  9729. dictSize -= eSize;
  9730. }
  9731. dctx->litEntropy = dctx->fseEntropy = 1;
  9732. /* reference dictionary content */
  9733. return ZSTD_refDictContent(dctx, dict, dictSize);
  9734. }
  9735. size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
  9736. {
  9737. assert(dctx != NULL);
  9738. dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
  9739. dctx->stage = ZSTDds_getFrameHeaderSize;
  9740. dctx->decodedSize = 0;
  9741. dctx->previousDstEnd = NULL;
  9742. dctx->prefixStart = NULL;
  9743. dctx->virtualStart = NULL;
  9744. dctx->dictEnd = NULL;
  9745. dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
  9746. dctx->litEntropy = dctx->fseEntropy = 0;
  9747. dctx->dictID = 0;
  9748. dctx->bType = bt_reserved;
  9749. ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
  9750. memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
  9751. dctx->LLTptr = dctx->entropy.LLTable;
  9752. dctx->MLTptr = dctx->entropy.MLTable;
  9753. dctx->OFTptr = dctx->entropy.OFTable;
  9754. dctx->HUFptr = dctx->entropy.hufTable;
  9755. return 0;
  9756. }
  9757. size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  9758. {
  9759. FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
  9760. if (dict && dictSize)
  9761. RETURN_ERROR_IF(
  9762. ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
  9763. dictionary_corrupted, "");
  9764. return 0;
  9765. }
  9766. /* ====== ZSTD_DDict ====== */
  9767. size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  9768. {
  9769. DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
  9770. assert(dctx != NULL);
  9771. if (ddict) {
  9772. const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
  9773. size_t const dictSize = ZSTD_DDict_dictSize(ddict);
  9774. const void* const dictEnd = dictStart + dictSize;
  9775. dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
  9776. DEBUGLOG(4, "DDict is %s",
  9777. dctx->ddictIsCold ? "~cold~" : "hot!");
  9778. }
  9779. FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
  9780. if (ddict) { /* NULL ddict is equivalent to no dictionary */
  9781. ZSTD_copyDDictParameters(dctx, ddict);
  9782. }
  9783. return 0;
  9784. }
  9785. /*! ZSTD_getDictID_fromDict() :
  9786. * Provides the dictID stored within dictionary.
  9787. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  9788. * It can still be loaded, but as a content-only dictionary. */
  9789. unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
  9790. {
  9791. if (dictSize < 8) return 0;
  9792. if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
  9793. return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  9794. }
  9795. /*! ZSTD_getDictID_fromFrame() :
  9796. * Provides the dictID required to decompress frame stored within `src`.
  9797. * If @return == 0, the dictID could not be decoded.
  9798. * This could for one of the following reasons :
  9799. * - The frame does not require a dictionary (most common case).
  9800. * - The frame was built with dictID intentionally removed.
  9801. * Needed dictionary is a hidden information.
  9802. * Note : this use case also happens when using a non-conformant dictionary.
  9803. * - `srcSize` is too small, and as a result, frame header could not be decoded.
  9804. * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
  9805. * - This is not a Zstandard frame.
  9806. * When identifying the exact failure cause, it's possible to use
  9807. * ZSTD_getFrameHeader(), which will provide a more precise error code. */
  9808. unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
  9809. {
  9810. ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };
  9811. size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
  9812. if (ZSTD_isError(hError)) return 0;
  9813. return zfp.dictID;
  9814. }
  9815. /*! ZSTD_decompress_usingDDict() :
  9816. * Decompression using a pre-digested Dictionary
  9817. * Use dictionary without significant overhead. */
  9818. size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  9819. void* dst, size_t dstCapacity,
  9820. const void* src, size_t srcSize,
  9821. const ZSTD_DDict* ddict)
  9822. {
  9823. /* pass content and size in case legacy frames are encountered */
  9824. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
  9825. NULL, 0,
  9826. ddict);
  9827. }
  9828. /*=====================================
  9829. * Streaming decompression
  9830. *====================================*/
  9831. ZSTD_DStream* ZSTD_createDStream(void)
  9832. {
  9833. DEBUGLOG(3, "ZSTD_createDStream");
  9834. return ZSTD_createDStream_advanced(ZSTD_defaultCMem);
  9835. }
  9836. ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
  9837. {
  9838. return ZSTD_initStaticDCtx(workspace, workspaceSize);
  9839. }
  9840. ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
  9841. {
  9842. return ZSTD_createDCtx_advanced(customMem);
  9843. }
  9844. size_t ZSTD_freeDStream(ZSTD_DStream* zds)
  9845. {
  9846. return ZSTD_freeDCtx(zds);
  9847. }
  9848. /* *** Initialization *** */
  9849. size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
  9850. size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
  9851. size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
  9852. const void* dict, size_t dictSize,
  9853. ZSTD_dictLoadMethod_e dictLoadMethod,
  9854. ZSTD_dictContentType_e dictContentType)
  9855. {
  9856. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  9857. ZSTD_clearDict(dctx);
  9858. if (dict && dictSize != 0) {
  9859. dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
  9860. RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!");
  9861. dctx->ddict = dctx->ddictLocal;
  9862. dctx->dictUses = ZSTD_use_indefinitely;
  9863. }
  9864. return 0;
  9865. }
  9866. size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  9867. {
  9868. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
  9869. }
  9870. size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  9871. {
  9872. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
  9873. }
  9874. size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
  9875. {
  9876. FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), "");
  9877. dctx->dictUses = ZSTD_use_once;
  9878. return 0;
  9879. }
  9880. size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
  9881. {
  9882. return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
  9883. }
  9884. /* ZSTD_initDStream_usingDict() :
  9885. * return : expected size, aka ZSTD_startingInputLength().
  9886. * this function cannot fail */
  9887. size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
  9888. {
  9889. DEBUGLOG(4, "ZSTD_initDStream_usingDict");
  9890. FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , "");
  9891. FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , "");
  9892. return ZSTD_startingInputLength(zds->format);
  9893. }
  9894. /* note : this variant can't fail */
  9895. size_t ZSTD_initDStream(ZSTD_DStream* zds)
  9896. {
  9897. DEBUGLOG(4, "ZSTD_initDStream");
  9898. return ZSTD_initDStream_usingDDict(zds, NULL);
  9899. }
  9900. /* ZSTD_initDStream_usingDDict() :
  9901. * ddict will just be referenced, and must outlive decompression session
  9902. * this function cannot fail */
  9903. size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
  9904. {
  9905. FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , "");
  9906. FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , "");
  9907. return ZSTD_startingInputLength(dctx->format);
  9908. }
  9909. /* ZSTD_resetDStream() :
  9910. * return : expected size, aka ZSTD_startingInputLength().
  9911. * this function cannot fail */
  9912. size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
  9913. {
  9914. FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), "");
  9915. return ZSTD_startingInputLength(dctx->format);
  9916. }
  9917. size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  9918. {
  9919. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  9920. ZSTD_clearDict(dctx);
  9921. if (ddict) {
  9922. dctx->ddict = ddict;
  9923. dctx->dictUses = ZSTD_use_indefinitely;
  9924. }
  9925. return 0;
  9926. }
  9927. /* ZSTD_DCtx_setMaxWindowSize() :
  9928. * note : no direct equivalence in ZSTD_DCtx_setParameter,
  9929. * since this version sets windowSize, and the other sets windowLog */
  9930. size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
  9931. {
  9932. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
  9933. size_t const min = (size_t)1 << bounds.lowerBound;
  9934. size_t const max = (size_t)1 << bounds.upperBound;
  9935. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  9936. RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, "");
  9937. RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, "");
  9938. dctx->maxWindowSize = maxWindowSize;
  9939. return 0;
  9940. }
  9941. size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
  9942. {
  9943. return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format);
  9944. }
  9945. ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
  9946. {
  9947. ZSTD_bounds bounds = { 0, 0, 0 };
  9948. switch(dParam) {
  9949. case ZSTD_d_windowLogMax:
  9950. bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
  9951. bounds.upperBound = ZSTD_WINDOWLOG_MAX;
  9952. return bounds;
  9953. case ZSTD_d_format:
  9954. bounds.lowerBound = (int)ZSTD_f_zstd1;
  9955. bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
  9956. ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
  9957. return bounds;
  9958. case ZSTD_d_stableOutBuffer:
  9959. bounds.lowerBound = (int)ZSTD_obm_buffered;
  9960. bounds.upperBound = (int)ZSTD_obm_stable;
  9961. return bounds;
  9962. default:;
  9963. }
  9964. bounds.error = ERROR(parameter_unsupported);
  9965. return bounds;
  9966. }
  9967. /* ZSTD_dParam_withinBounds:
  9968. * @return 1 if value is within dParam bounds,
  9969. * 0 otherwise */
  9970. static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
  9971. {
  9972. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
  9973. if (ZSTD_isError(bounds.error)) return 0;
  9974. if (value < bounds.lowerBound) return 0;
  9975. if (value > bounds.upperBound) return 0;
  9976. return 1;
  9977. }
  9978. #define CHECK_DBOUNDS(p,v) { \
  9979. RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
  9980. }
  9981. size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
  9982. {
  9983. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  9984. switch(dParam) {
  9985. case ZSTD_d_windowLogMax:
  9986. if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;
  9987. CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
  9988. dctx->maxWindowSize = ((size_t)1) << value;
  9989. return 0;
  9990. case ZSTD_d_format:
  9991. CHECK_DBOUNDS(ZSTD_d_format, value);
  9992. dctx->format = (ZSTD_format_e)value;
  9993. return 0;
  9994. case ZSTD_d_stableOutBuffer:
  9995. CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);
  9996. dctx->outBufferMode = (ZSTD_outBufferMode_e)value;
  9997. return 0;
  9998. default:;
  9999. }
  10000. RETURN_ERROR(parameter_unsupported, "");
  10001. }
  10002. size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
  10003. {
  10004. if ( (reset == ZSTD_reset_session_only)
  10005. || (reset == ZSTD_reset_session_and_parameters) ) {
  10006. dctx->streamStage = zdss_init;
  10007. dctx->noForwardProgress = 0;
  10008. }
  10009. if ( (reset == ZSTD_reset_parameters)
  10010. || (reset == ZSTD_reset_session_and_parameters) ) {
  10011. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  10012. ZSTD_clearDict(dctx);
  10013. dctx->format = ZSTD_f_zstd1;
  10014. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  10015. }
  10016. return 0;
  10017. }
  10018. size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
  10019. {
  10020. return ZSTD_sizeof_DCtx(dctx);
  10021. }
  10022. size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
  10023. {
  10024. size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  10025. unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);
  10026. unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
  10027. size_t const minRBSize = (size_t) neededSize;
  10028. RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,
  10029. frameParameter_windowTooLarge, "");
  10030. return minRBSize;
  10031. }
  10032. size_t ZSTD_estimateDStreamSize(size_t windowSize)
  10033. {
  10034. size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  10035. size_t const inBuffSize = blockSize; /* no block can be larger */
  10036. size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
  10037. return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
  10038. }
  10039. size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
  10040. {
  10041. U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
  10042. ZSTD_frameHeader zfh;
  10043. size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
  10044. if (ZSTD_isError(err)) return err;
  10045. RETURN_ERROR_IF(err>0, srcSize_wrong, "");
  10046. RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,
  10047. frameParameter_windowTooLarge, "");
  10048. return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
  10049. }
  10050. /* ***** Decompression ***** */
  10051. static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
  10052. {
  10053. return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR;
  10054. }
  10055. static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
  10056. {
  10057. if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))
  10058. zds->oversizedDuration++;
  10059. else
  10060. zds->oversizedDuration = 0;
  10061. }
  10062. static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds)
  10063. {
  10064. return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION;
  10065. }
  10066. /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */
  10067. static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output)
  10068. {
  10069. ZSTD_outBuffer const expect = zds->expectedOutBuffer;
  10070. /* No requirement when ZSTD_obm_stable is not enabled. */
  10071. if (zds->outBufferMode != ZSTD_obm_stable)
  10072. return 0;
  10073. /* Any buffer is allowed in zdss_init, this must be the same for every other call until
  10074. * the context is reset.
  10075. */
  10076. if (zds->streamStage == zdss_init)
  10077. return 0;
  10078. /* The buffer must match our expectation exactly. */
  10079. if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)
  10080. return 0;
  10081. RETURN_ERROR(dstBuffer_wrong, "ZSTD_obm_stable enabled but output differs!");
  10082. }
  10083. /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
  10084. * and updates the stage and the output buffer state. This call is extracted so it can be
  10085. * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.
  10086. * NOTE: You must break after calling this function since the streamStage is modified.
  10087. */
  10088. static size_t ZSTD_decompressContinueStream(
  10089. ZSTD_DStream* zds, char** op, char* oend,
  10090. void const* src, size_t srcSize) {
  10091. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  10092. if (zds->outBufferMode == ZSTD_obm_buffered) {
  10093. size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;
  10094. size_t const decodedSize = ZSTD_decompressContinue(zds,
  10095. zds->outBuff + zds->outStart, dstSize, src, srcSize);
  10096. FORWARD_IF_ERROR(decodedSize, "");
  10097. if (!decodedSize && !isSkipFrame) {
  10098. zds->streamStage = zdss_read;
  10099. } else {
  10100. zds->outEnd = zds->outStart + decodedSize;
  10101. zds->streamStage = zdss_flush;
  10102. }
  10103. } else {
  10104. /* Write directly into the output buffer */
  10105. size_t const dstSize = isSkipFrame ? 0 : oend - *op;
  10106. size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
  10107. FORWARD_IF_ERROR(decodedSize, "");
  10108. *op += decodedSize;
  10109. /* Flushing is not needed. */
  10110. zds->streamStage = zdss_read;
  10111. assert(*op <= oend);
  10112. assert(zds->outBufferMode == ZSTD_obm_stable);
  10113. }
  10114. return 0;
  10115. }
  10116. size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  10117. {
  10118. const char* const src = (const char*)input->src;
  10119. const char* const istart = input->pos != 0 ? src + input->pos : src;
  10120. const char* const iend = input->size != 0 ? src + input->size : src;
  10121. const char* ip = istart;
  10122. char* const dst = (char*)output->dst;
  10123. char* const ostart = output->pos != 0 ? dst + output->pos : dst;
  10124. char* const oend = output->size != 0 ? dst + output->size : dst;
  10125. char* op = ostart;
  10126. U32 someMoreWork = 1;
  10127. DEBUGLOG(5, "ZSTD_decompressStream");
  10128. RETURN_ERROR_IF(
  10129. input->pos > input->size,
  10130. srcSize_wrong,
  10131. "forbidden. in: pos: %u vs size: %u",
  10132. (U32)input->pos, (U32)input->size);
  10133. RETURN_ERROR_IF(
  10134. output->pos > output->size,
  10135. dstSize_tooSmall,
  10136. "forbidden. out: pos: %u vs size: %u",
  10137. (U32)output->pos, (U32)output->size);
  10138. DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
  10139. FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), "");
  10140. while (someMoreWork) {
  10141. switch(zds->streamStage)
  10142. {
  10143. case zdss_init :
  10144. DEBUGLOG(5, "stage zdss_init => transparent reset ");
  10145. zds->streamStage = zdss_loadHeader;
  10146. zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
  10147. zds->legacyVersion = 0;
  10148. zds->hostageByte = 0;
  10149. zds->expectedOutBuffer = *output;
  10150. /* fall-through */
  10151. case zdss_loadHeader :
  10152. DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
  10153. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  10154. if (zds->legacyVersion) {
  10155. RETURN_ERROR_IF(zds->staticSize, memory_allocation,
  10156. "legacy support is incompatible with static dctx");
  10157. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
  10158. if (hint==0) zds->streamStage = zdss_init;
  10159. return hint;
  10160. } }
  10161. #endif
  10162. { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
  10163. DEBUGLOG(5, "header size : %u", (U32)hSize);
  10164. if (ZSTD_isError(hSize)) {
  10165. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  10166. U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
  10167. if (legacyVersion) {
  10168. ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);
  10169. const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;
  10170. size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;
  10171. DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
  10172. RETURN_ERROR_IF(zds->staticSize, memory_allocation,
  10173. "legacy support is incompatible with static dctx");
  10174. FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext,
  10175. zds->previousLegacyVersion, legacyVersion,
  10176. dict, dictSize), "");
  10177. zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
  10178. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
  10179. if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */
  10180. return hint;
  10181. } }
  10182. #endif
  10183. return hSize; /* error */
  10184. }
  10185. if (hSize != 0) { /* need more input */
  10186. size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
  10187. size_t const remainingInput = (size_t)(iend-ip);
  10188. assert(iend >= ip);
  10189. if (toLoad > remainingInput) { /* not enough input to load full header */
  10190. if (remainingInput > 0) {
  10191. memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
  10192. zds->lhSize += remainingInput;
  10193. }
  10194. input->pos = input->size;
  10195. return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
  10196. }
  10197. assert(ip != NULL);
  10198. memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
  10199. break;
  10200. } }
  10201. /* check for single-pass mode opportunity */
  10202. if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  10203. && zds->fParams.frameType != ZSTD_skippableFrame
  10204. && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
  10205. size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);
  10206. if (cSize <= (size_t)(iend-istart)) {
  10207. /* shortcut : using single-pass mode */
  10208. size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, ZSTD_getDDict(zds));
  10209. if (ZSTD_isError(decompressedSize)) return decompressedSize;
  10210. DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
  10211. ip = istart + cSize;
  10212. op += decompressedSize;
  10213. zds->expected = 0;
  10214. zds->streamStage = zdss_init;
  10215. someMoreWork = 0;
  10216. break;
  10217. } }
  10218. /* Check output buffer is large enough for ZSTD_odm_stable. */
  10219. if (zds->outBufferMode == ZSTD_obm_stable
  10220. && zds->fParams.frameType != ZSTD_skippableFrame
  10221. && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  10222. && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {
  10223. RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small");
  10224. }
  10225. /* Consume header (see ZSTDds_decodeFrameHeader) */
  10226. DEBUGLOG(4, "Consume header");
  10227. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), "");
  10228. if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  10229. zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
  10230. zds->stage = ZSTDds_skipFrame;
  10231. } else {
  10232. FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), "");
  10233. zds->expected = ZSTD_blockHeaderSize;
  10234. zds->stage = ZSTDds_decodeBlockHeader;
  10235. }
  10236. /* control buffer memory usage */
  10237. DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
  10238. (U32)(zds->fParams.windowSize >>10),
  10239. (U32)(zds->maxWindowSize >> 10) );
  10240. zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
  10241. RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,
  10242. frameParameter_windowTooLarge, "");
  10243. /* Adapt buffer sizes to frame header instructions */
  10244. { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
  10245. size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_obm_buffered
  10246. ? ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize)
  10247. : 0;
  10248. ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize);
  10249. { int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);
  10250. int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);
  10251. if (tooSmall || tooLarge) {
  10252. size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
  10253. DEBUGLOG(4, "inBuff : from %u to %u",
  10254. (U32)zds->inBuffSize, (U32)neededInBuffSize);
  10255. DEBUGLOG(4, "outBuff : from %u to %u",
  10256. (U32)zds->outBuffSize, (U32)neededOutBuffSize);
  10257. if (zds->staticSize) { /* static DCtx */
  10258. DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
  10259. assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */
  10260. RETURN_ERROR_IF(
  10261. bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
  10262. memory_allocation, "");
  10263. } else {
  10264. ZSTD_free(zds->inBuff, zds->customMem);
  10265. zds->inBuffSize = 0;
  10266. zds->outBuffSize = 0;
  10267. zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);
  10268. RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, "");
  10269. }
  10270. zds->inBuffSize = neededInBuffSize;
  10271. zds->outBuff = zds->inBuff + zds->inBuffSize;
  10272. zds->outBuffSize = neededOutBuffSize;
  10273. } } }
  10274. zds->streamStage = zdss_read;
  10275. /* fall-through */
  10276. case zdss_read:
  10277. DEBUGLOG(5, "stage zdss_read");
  10278. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip);
  10279. DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
  10280. if (neededInSize==0) { /* end of frame */
  10281. zds->streamStage = zdss_init;
  10282. someMoreWork = 0;
  10283. break;
  10284. }
  10285. if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
  10286. FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), "");
  10287. ip += neededInSize;
  10288. /* Function modifies the stage so we must break */
  10289. break;
  10290. } }
  10291. if (ip==iend) { someMoreWork = 0; break; } /* no more input */
  10292. zds->streamStage = zdss_load;
  10293. /* fall-through */
  10294. case zdss_load:
  10295. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  10296. size_t const toLoad = neededInSize - zds->inPos;
  10297. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  10298. size_t loadedSize;
  10299. /* At this point we shouldn't be decompressing a block that we can stream. */
  10300. assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip));
  10301. if (isSkipFrame) {
  10302. loadedSize = MIN(toLoad, (size_t)(iend-ip));
  10303. } else {
  10304. RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
  10305. corruption_detected,
  10306. "should never happen");
  10307. loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
  10308. }
  10309. ip += loadedSize;
  10310. zds->inPos += loadedSize;
  10311. if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
  10312. /* decode loaded input */
  10313. zds->inPos = 0; /* input is consumed */
  10314. FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), "");
  10315. /* Function modifies the stage so we must break */
  10316. break;
  10317. }
  10318. case zdss_flush:
  10319. { size_t const toFlushSize = zds->outEnd - zds->outStart;
  10320. size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
  10321. op += flushedSize;
  10322. zds->outStart += flushedSize;
  10323. if (flushedSize == toFlushSize) { /* flush completed */
  10324. zds->streamStage = zdss_read;
  10325. if ( (zds->outBuffSize < zds->fParams.frameContentSize)
  10326. && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
  10327. DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
  10328. (int)(zds->outBuffSize - zds->outStart),
  10329. (U32)zds->fParams.blockSizeMax);
  10330. zds->outStart = zds->outEnd = 0;
  10331. }
  10332. break;
  10333. } }
  10334. /* cannot complete flush */
  10335. someMoreWork = 0;
  10336. break;
  10337. default:
  10338. assert(0); /* impossible */
  10339. RETURN_ERROR(GENERIC, "impossible to reach"); /* some compiler require default to do something */
  10340. } }
  10341. /* result */
  10342. input->pos = (size_t)(ip - (const char*)(input->src));
  10343. output->pos = (size_t)(op - (char*)(output->dst));
  10344. /* Update the expected output buffer for ZSTD_obm_stable. */
  10345. zds->expectedOutBuffer = *output;
  10346. if ((ip==istart) && (op==ostart)) { /* no forward progress */
  10347. zds->noForwardProgress ++;
  10348. if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
  10349. RETURN_ERROR_IF(op==oend, dstSize_tooSmall, "");
  10350. RETURN_ERROR_IF(ip==iend, srcSize_wrong, "");
  10351. assert(0);
  10352. }
  10353. } else {
  10354. zds->noForwardProgress = 0;
  10355. }
  10356. { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
  10357. if (!nextSrcSizeHint) { /* frame fully decoded */
  10358. if (zds->outEnd == zds->outStart) { /* output fully flushed */
  10359. if (zds->hostageByte) {
  10360. if (input->pos >= input->size) {
  10361. /* can't release hostage (not present) */
  10362. zds->streamStage = zdss_read;
  10363. return 1;
  10364. }
  10365. input->pos++; /* release hostage */
  10366. } /* zds->hostageByte */
  10367. return 0;
  10368. } /* zds->outEnd == zds->outStart */
  10369. if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
  10370. input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
  10371. zds->hostageByte=1;
  10372. }
  10373. return 1;
  10374. } /* nextSrcSizeHint==0 */
  10375. nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
  10376. assert(zds->inPos <= nextSrcSizeHint);
  10377. nextSrcSizeHint -= zds->inPos; /* part already loaded*/
  10378. return nextSrcSizeHint;
  10379. }
  10380. }
  10381. size_t ZSTD_decompressStream_simpleArgs (
  10382. ZSTD_DCtx* dctx,
  10383. void* dst, size_t dstCapacity, size_t* dstPos,
  10384. const void* src, size_t srcSize, size_t* srcPos)
  10385. {
  10386. ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };
  10387. ZSTD_inBuffer input = { src, srcSize, *srcPos };
  10388. /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
  10389. size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
  10390. *dstPos = output.pos;
  10391. *srcPos = input.pos;
  10392. return cErr;
  10393. }
  10394. /**** ended inlining decompress/zstd_decompress.c ****/
  10395. /**** start inlining decompress/zstd_decompress_block.c ****/
  10396. /*
  10397. * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
  10398. * All rights reserved.
  10399. *
  10400. * This source code is licensed under both the BSD-style license (found in the
  10401. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  10402. * in the COPYING file in the root directory of this source tree).
  10403. * You may select, at your option, one of the above-listed licenses.
  10404. */
  10405. /* zstd_decompress_block :
  10406. * this module takes care of decompressing _compressed_ block */
  10407. /*-*******************************************************
  10408. * Dependencies
  10409. *********************************************************/
  10410. /**** skipping file: ../common/compiler.h ****/
  10411. /**** skipping file: ../common/cpu.h ****/
  10412. /**** skipping file: ../common/mem.h ****/
  10413. #define FSE_STATIC_LINKING_ONLY
  10414. /**** skipping file: ../common/fse.h ****/
  10415. #define HUF_STATIC_LINKING_ONLY
  10416. /**** skipping file: ../common/huf.h ****/
  10417. /**** skipping file: ../common/zstd_internal.h ****/
  10418. /**** skipping file: zstd_decompress_internal.h ****/
  10419. /**** skipping file: zstd_ddict.h ****/
  10420. /**** skipping file: zstd_decompress_block.h ****/
  10421. /*_*******************************************************
  10422. * Macros
  10423. **********************************************************/
  10424. /* These two optional macros force the use one way or another of the two
  10425. * ZSTD_decompressSequences implementations. You can't force in both directions
  10426. * at the same time.
  10427. */
  10428. #if defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  10429. defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  10430. #error "Cannot force the use of the short and the long ZSTD_decompressSequences variants!"
  10431. #endif
  10432. /*_*******************************************************
  10433. * Memory operations
  10434. **********************************************************/
  10435. static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
  10436. /*-*************************************************************
  10437. * Block decoding
  10438. ***************************************************************/
  10439. /*! ZSTD_getcBlockSize() :
  10440. * Provides the size of compressed block from block header `src` */
  10441. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  10442. blockProperties_t* bpPtr)
  10443. {
  10444. RETURN_ERROR_IF(srcSize < ZSTD_blockHeaderSize, srcSize_wrong, "");
  10445. { U32 const cBlockHeader = MEM_readLE24(src);
  10446. U32 const cSize = cBlockHeader >> 3;
  10447. bpPtr->lastBlock = cBlockHeader & 1;
  10448. bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
  10449. bpPtr->origSize = cSize; /* only useful for RLE */
  10450. if (bpPtr->blockType == bt_rle) return 1;
  10451. RETURN_ERROR_IF(bpPtr->blockType == bt_reserved, corruption_detected, "");
  10452. return cSize;
  10453. }
  10454. }
  10455. /* Hidden declaration for fullbench */
  10456. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  10457. const void* src, size_t srcSize);
  10458. /*! ZSTD_decodeLiteralsBlock() :
  10459. * @return : nb of bytes read from src (< srcSize )
  10460. * note : symbol not declared but exposed for fullbench */
  10461. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  10462. const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
  10463. {
  10464. DEBUGLOG(5, "ZSTD_decodeLiteralsBlock");
  10465. RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, "");
  10466. { const BYTE* const istart = (const BYTE*) src;
  10467. symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
  10468. switch(litEncType)
  10469. {
  10470. case set_repeat:
  10471. DEBUGLOG(5, "set_repeat flag : re-using stats from previous compressed literals block");
  10472. RETURN_ERROR_IF(dctx->litEntropy==0, dictionary_corrupted, "");
  10473. /* fall-through */
  10474. case set_compressed:
  10475. RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3");
  10476. { size_t lhSize, litSize, litCSize;
  10477. U32 singleStream=0;
  10478. U32 const lhlCode = (istart[0] >> 2) & 3;
  10479. U32 const lhc = MEM_readLE32(istart);
  10480. size_t hufSuccess;
  10481. switch(lhlCode)
  10482. {
  10483. case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
  10484. /* 2 - 2 - 10 - 10 */
  10485. singleStream = !lhlCode;
  10486. lhSize = 3;
  10487. litSize = (lhc >> 4) & 0x3FF;
  10488. litCSize = (lhc >> 14) & 0x3FF;
  10489. break;
  10490. case 2:
  10491. /* 2 - 2 - 14 - 14 */
  10492. lhSize = 4;
  10493. litSize = (lhc >> 4) & 0x3FFF;
  10494. litCSize = lhc >> 18;
  10495. break;
  10496. case 3:
  10497. /* 2 - 2 - 18 - 18 */
  10498. lhSize = 5;
  10499. litSize = (lhc >> 4) & 0x3FFFF;
  10500. litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);
  10501. break;
  10502. }
  10503. RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
  10504. RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, "");
  10505. /* prefetch huffman table if cold */
  10506. if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) {
  10507. PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable));
  10508. }
  10509. if (litEncType==set_repeat) {
  10510. if (singleStream) {
  10511. hufSuccess = HUF_decompress1X_usingDTable_bmi2(
  10512. dctx->litBuffer, litSize, istart+lhSize, litCSize,
  10513. dctx->HUFptr, dctx->bmi2);
  10514. } else {
  10515. hufSuccess = HUF_decompress4X_usingDTable_bmi2(
  10516. dctx->litBuffer, litSize, istart+lhSize, litCSize,
  10517. dctx->HUFptr, dctx->bmi2);
  10518. }
  10519. } else {
  10520. if (singleStream) {
  10521. #if defined(HUF_FORCE_DECOMPRESS_X2)
  10522. hufSuccess = HUF_decompress1X_DCtx_wksp(
  10523. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  10524. istart+lhSize, litCSize, dctx->workspace,
  10525. sizeof(dctx->workspace));
  10526. #else
  10527. hufSuccess = HUF_decompress1X1_DCtx_wksp_bmi2(
  10528. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  10529. istart+lhSize, litCSize, dctx->workspace,
  10530. sizeof(dctx->workspace), dctx->bmi2);
  10531. #endif
  10532. } else {
  10533. hufSuccess = HUF_decompress4X_hufOnly_wksp_bmi2(
  10534. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  10535. istart+lhSize, litCSize, dctx->workspace,
  10536. sizeof(dctx->workspace), dctx->bmi2);
  10537. }
  10538. }
  10539. RETURN_ERROR_IF(HUF_isError(hufSuccess), corruption_detected, "");
  10540. dctx->litPtr = dctx->litBuffer;
  10541. dctx->litSize = litSize;
  10542. dctx->litEntropy = 1;
  10543. if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable;
  10544. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  10545. return litCSize + lhSize;
  10546. }
  10547. case set_basic:
  10548. { size_t litSize, lhSize;
  10549. U32 const lhlCode = ((istart[0]) >> 2) & 3;
  10550. switch(lhlCode)
  10551. {
  10552. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  10553. lhSize = 1;
  10554. litSize = istart[0] >> 3;
  10555. break;
  10556. case 1:
  10557. lhSize = 2;
  10558. litSize = MEM_readLE16(istart) >> 4;
  10559. break;
  10560. case 3:
  10561. lhSize = 3;
  10562. litSize = MEM_readLE24(istart) >> 4;
  10563. break;
  10564. }
  10565. if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
  10566. RETURN_ERROR_IF(litSize+lhSize > srcSize, corruption_detected, "");
  10567. memcpy(dctx->litBuffer, istart+lhSize, litSize);
  10568. dctx->litPtr = dctx->litBuffer;
  10569. dctx->litSize = litSize;
  10570. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  10571. return lhSize+litSize;
  10572. }
  10573. /* direct reference into compressed stream */
  10574. dctx->litPtr = istart+lhSize;
  10575. dctx->litSize = litSize;
  10576. return lhSize+litSize;
  10577. }
  10578. case set_rle:
  10579. { U32 const lhlCode = ((istart[0]) >> 2) & 3;
  10580. size_t litSize, lhSize;
  10581. switch(lhlCode)
  10582. {
  10583. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  10584. lhSize = 1;
  10585. litSize = istart[0] >> 3;
  10586. break;
  10587. case 1:
  10588. lhSize = 2;
  10589. litSize = MEM_readLE16(istart) >> 4;
  10590. break;
  10591. case 3:
  10592. lhSize = 3;
  10593. litSize = MEM_readLE24(istart) >> 4;
  10594. RETURN_ERROR_IF(srcSize<4, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4");
  10595. break;
  10596. }
  10597. RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
  10598. memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
  10599. dctx->litPtr = dctx->litBuffer;
  10600. dctx->litSize = litSize;
  10601. return lhSize+1;
  10602. }
  10603. default:
  10604. RETURN_ERROR(corruption_detected, "impossible");
  10605. }
  10606. }
  10607. }
  10608. /* Default FSE distribution tables.
  10609. * These are pre-calculated FSE decoding tables using default distributions as defined in specification :
  10610. * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#default-distributions
  10611. * They were generated programmatically with following method :
  10612. * - start from default distributions, present in /lib/common/zstd_internal.h
  10613. * - generate tables normally, using ZSTD_buildFSETable()
  10614. * - printout the content of tables
  10615. * - pretify output, report below, test with fuzzer to ensure it's correct */
  10616. /* Default FSE distribution table for Literal Lengths */
  10617. static const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {
  10618. { 1, 1, 1, LL_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  10619. /* nextState, nbAddBits, nbBits, baseVal */
  10620. { 0, 0, 4, 0}, { 16, 0, 4, 0},
  10621. { 32, 0, 5, 1}, { 0, 0, 5, 3},
  10622. { 0, 0, 5, 4}, { 0, 0, 5, 6},
  10623. { 0, 0, 5, 7}, { 0, 0, 5, 9},
  10624. { 0, 0, 5, 10}, { 0, 0, 5, 12},
  10625. { 0, 0, 6, 14}, { 0, 1, 5, 16},
  10626. { 0, 1, 5, 20}, { 0, 1, 5, 22},
  10627. { 0, 2, 5, 28}, { 0, 3, 5, 32},
  10628. { 0, 4, 5, 48}, { 32, 6, 5, 64},
  10629. { 0, 7, 5, 128}, { 0, 8, 6, 256},
  10630. { 0, 10, 6, 1024}, { 0, 12, 6, 4096},
  10631. { 32, 0, 4, 0}, { 0, 0, 4, 1},
  10632. { 0, 0, 5, 2}, { 32, 0, 5, 4},
  10633. { 0, 0, 5, 5}, { 32, 0, 5, 7},
  10634. { 0, 0, 5, 8}, { 32, 0, 5, 10},
  10635. { 0, 0, 5, 11}, { 0, 0, 6, 13},
  10636. { 32, 1, 5, 16}, { 0, 1, 5, 18},
  10637. { 32, 1, 5, 22}, { 0, 2, 5, 24},
  10638. { 32, 3, 5, 32}, { 0, 3, 5, 40},
  10639. { 0, 6, 4, 64}, { 16, 6, 4, 64},
  10640. { 32, 7, 5, 128}, { 0, 9, 6, 512},
  10641. { 0, 11, 6, 2048}, { 48, 0, 4, 0},
  10642. { 16, 0, 4, 1}, { 32, 0, 5, 2},
  10643. { 32, 0, 5, 3}, { 32, 0, 5, 5},
  10644. { 32, 0, 5, 6}, { 32, 0, 5, 8},
  10645. { 32, 0, 5, 9}, { 32, 0, 5, 11},
  10646. { 32, 0, 5, 12}, { 0, 0, 6, 15},
  10647. { 32, 1, 5, 18}, { 32, 1, 5, 20},
  10648. { 32, 2, 5, 24}, { 32, 2, 5, 28},
  10649. { 32, 3, 5, 40}, { 32, 4, 5, 48},
  10650. { 0, 16, 6,65536}, { 0, 15, 6,32768},
  10651. { 0, 14, 6,16384}, { 0, 13, 6, 8192},
  10652. }; /* LL_defaultDTable */
  10653. /* Default FSE distribution table for Offset Codes */
  10654. static const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {
  10655. { 1, 1, 1, OF_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  10656. /* nextState, nbAddBits, nbBits, baseVal */
  10657. { 0, 0, 5, 0}, { 0, 6, 4, 61},
  10658. { 0, 9, 5, 509}, { 0, 15, 5,32765},
  10659. { 0, 21, 5,2097149}, { 0, 3, 5, 5},
  10660. { 0, 7, 4, 125}, { 0, 12, 5, 4093},
  10661. { 0, 18, 5,262141}, { 0, 23, 5,8388605},
  10662. { 0, 5, 5, 29}, { 0, 8, 4, 253},
  10663. { 0, 14, 5,16381}, { 0, 20, 5,1048573},
  10664. { 0, 2, 5, 1}, { 16, 7, 4, 125},
  10665. { 0, 11, 5, 2045}, { 0, 17, 5,131069},
  10666. { 0, 22, 5,4194301}, { 0, 4, 5, 13},
  10667. { 16, 8, 4, 253}, { 0, 13, 5, 8189},
  10668. { 0, 19, 5,524285}, { 0, 1, 5, 1},
  10669. { 16, 6, 4, 61}, { 0, 10, 5, 1021},
  10670. { 0, 16, 5,65533}, { 0, 28, 5,268435453},
  10671. { 0, 27, 5,134217725}, { 0, 26, 5,67108861},
  10672. { 0, 25, 5,33554429}, { 0, 24, 5,16777213},
  10673. }; /* OF_defaultDTable */
  10674. /* Default FSE distribution table for Match Lengths */
  10675. static const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {
  10676. { 1, 1, 1, ML_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  10677. /* nextState, nbAddBits, nbBits, baseVal */
  10678. { 0, 0, 6, 3}, { 0, 0, 4, 4},
  10679. { 32, 0, 5, 5}, { 0, 0, 5, 6},
  10680. { 0, 0, 5, 8}, { 0, 0, 5, 9},
  10681. { 0, 0, 5, 11}, { 0, 0, 6, 13},
  10682. { 0, 0, 6, 16}, { 0, 0, 6, 19},
  10683. { 0, 0, 6, 22}, { 0, 0, 6, 25},
  10684. { 0, 0, 6, 28}, { 0, 0, 6, 31},
  10685. { 0, 0, 6, 34}, { 0, 1, 6, 37},
  10686. { 0, 1, 6, 41}, { 0, 2, 6, 47},
  10687. { 0, 3, 6, 59}, { 0, 4, 6, 83},
  10688. { 0, 7, 6, 131}, { 0, 9, 6, 515},
  10689. { 16, 0, 4, 4}, { 0, 0, 4, 5},
  10690. { 32, 0, 5, 6}, { 0, 0, 5, 7},
  10691. { 32, 0, 5, 9}, { 0, 0, 5, 10},
  10692. { 0, 0, 6, 12}, { 0, 0, 6, 15},
  10693. { 0, 0, 6, 18}, { 0, 0, 6, 21},
  10694. { 0, 0, 6, 24}, { 0, 0, 6, 27},
  10695. { 0, 0, 6, 30}, { 0, 0, 6, 33},
  10696. { 0, 1, 6, 35}, { 0, 1, 6, 39},
  10697. { 0, 2, 6, 43}, { 0, 3, 6, 51},
  10698. { 0, 4, 6, 67}, { 0, 5, 6, 99},
  10699. { 0, 8, 6, 259}, { 32, 0, 4, 4},
  10700. { 48, 0, 4, 4}, { 16, 0, 4, 5},
  10701. { 32, 0, 5, 7}, { 32, 0, 5, 8},
  10702. { 32, 0, 5, 10}, { 32, 0, 5, 11},
  10703. { 0, 0, 6, 14}, { 0, 0, 6, 17},
  10704. { 0, 0, 6, 20}, { 0, 0, 6, 23},
  10705. { 0, 0, 6, 26}, { 0, 0, 6, 29},
  10706. { 0, 0, 6, 32}, { 0, 16, 6,65539},
  10707. { 0, 15, 6,32771}, { 0, 14, 6,16387},
  10708. { 0, 13, 6, 8195}, { 0, 12, 6, 4099},
  10709. { 0, 11, 6, 2051}, { 0, 10, 6, 1027},
  10710. }; /* ML_defaultDTable */
  10711. static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U32 nbAddBits)
  10712. {
  10713. void* ptr = dt;
  10714. ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr;
  10715. ZSTD_seqSymbol* const cell = dt + 1;
  10716. DTableH->tableLog = 0;
  10717. DTableH->fastMode = 0;
  10718. cell->nbBits = 0;
  10719. cell->nextState = 0;
  10720. assert(nbAddBits < 255);
  10721. cell->nbAdditionalBits = (BYTE)nbAddBits;
  10722. cell->baseValue = baseValue;
  10723. }
  10724. /* ZSTD_buildFSETable() :
  10725. * generate FSE decoding table for one symbol (ll, ml or off)
  10726. * cannot fail if input is valid =>
  10727. * all inputs are presumed validated at this stage */
  10728. void
  10729. ZSTD_buildFSETable(ZSTD_seqSymbol* dt,
  10730. const short* normalizedCounter, unsigned maxSymbolValue,
  10731. const U32* baseValue, const U32* nbAdditionalBits,
  10732. unsigned tableLog)
  10733. {
  10734. ZSTD_seqSymbol* const tableDecode = dt+1;
  10735. U16 symbolNext[MaxSeq+1];
  10736. U32 const maxSV1 = maxSymbolValue + 1;
  10737. U32 const tableSize = 1 << tableLog;
  10738. U32 highThreshold = tableSize-1;
  10739. /* Sanity Checks */
  10740. assert(maxSymbolValue <= MaxSeq);
  10741. assert(tableLog <= MaxFSELog);
  10742. /* Init, lay down lowprob symbols */
  10743. { ZSTD_seqSymbol_header DTableH;
  10744. DTableH.tableLog = tableLog;
  10745. DTableH.fastMode = 1;
  10746. { S16 const largeLimit= (S16)(1 << (tableLog-1));
  10747. U32 s;
  10748. for (s=0; s<maxSV1; s++) {
  10749. if (normalizedCounter[s]==-1) {
  10750. tableDecode[highThreshold--].baseValue = s;
  10751. symbolNext[s] = 1;
  10752. } else {
  10753. if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
  10754. assert(normalizedCounter[s]>=0);
  10755. symbolNext[s] = (U16)normalizedCounter[s];
  10756. } } }
  10757. memcpy(dt, &DTableH, sizeof(DTableH));
  10758. }
  10759. /* Spread symbols */
  10760. { U32 const tableMask = tableSize-1;
  10761. U32 const step = FSE_TABLESTEP(tableSize);
  10762. U32 s, position = 0;
  10763. for (s=0; s<maxSV1; s++) {
  10764. int i;
  10765. for (i=0; i<normalizedCounter[s]; i++) {
  10766. tableDecode[position].baseValue = s;
  10767. position = (position + step) & tableMask;
  10768. while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
  10769. } }
  10770. assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  10771. }
  10772. /* Build Decoding table */
  10773. { U32 u;
  10774. for (u=0; u<tableSize; u++) {
  10775. U32 const symbol = tableDecode[u].baseValue;
  10776. U32 const nextState = symbolNext[symbol]++;
  10777. tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
  10778. tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
  10779. assert(nbAdditionalBits[symbol] < 255);
  10780. tableDecode[u].nbAdditionalBits = (BYTE)nbAdditionalBits[symbol];
  10781. tableDecode[u].baseValue = baseValue[symbol];
  10782. } }
  10783. }
  10784. /*! ZSTD_buildSeqTable() :
  10785. * @return : nb bytes read from src,
  10786. * or an error code if it fails */
  10787. static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr,
  10788. symbolEncodingType_e type, unsigned max, U32 maxLog,
  10789. const void* src, size_t srcSize,
  10790. const U32* baseValue, const U32* nbAdditionalBits,
  10791. const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable,
  10792. int ddictIsCold, int nbSeq)
  10793. {
  10794. switch(type)
  10795. {
  10796. case set_rle :
  10797. RETURN_ERROR_IF(!srcSize, srcSize_wrong, "");
  10798. RETURN_ERROR_IF((*(const BYTE*)src) > max, corruption_detected, "");
  10799. { U32 const symbol = *(const BYTE*)src;
  10800. U32 const baseline = baseValue[symbol];
  10801. U32 const nbBits = nbAdditionalBits[symbol];
  10802. ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits);
  10803. }
  10804. *DTablePtr = DTableSpace;
  10805. return 1;
  10806. case set_basic :
  10807. *DTablePtr = defaultTable;
  10808. return 0;
  10809. case set_repeat:
  10810. RETURN_ERROR_IF(!flagRepeatTable, corruption_detected, "");
  10811. /* prefetch FSE table if used */
  10812. if (ddictIsCold && (nbSeq > 24 /* heuristic */)) {
  10813. const void* const pStart = *DTablePtr;
  10814. size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog));
  10815. PREFETCH_AREA(pStart, pSize);
  10816. }
  10817. return 0;
  10818. case set_compressed :
  10819. { unsigned tableLog;
  10820. S16 norm[MaxSeq+1];
  10821. size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
  10822. RETURN_ERROR_IF(FSE_isError(headerSize), corruption_detected, "");
  10823. RETURN_ERROR_IF(tableLog > maxLog, corruption_detected, "");
  10824. ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog);
  10825. *DTablePtr = DTableSpace;
  10826. return headerSize;
  10827. }
  10828. default :
  10829. assert(0);
  10830. RETURN_ERROR(GENERIC, "impossible");
  10831. }
  10832. }
  10833. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  10834. const void* src, size_t srcSize)
  10835. {
  10836. const BYTE* const istart = (const BYTE* const)src;
  10837. const BYTE* const iend = istart + srcSize;
  10838. const BYTE* ip = istart;
  10839. int nbSeq;
  10840. DEBUGLOG(5, "ZSTD_decodeSeqHeaders");
  10841. /* check */
  10842. RETURN_ERROR_IF(srcSize < MIN_SEQUENCES_SIZE, srcSize_wrong, "");
  10843. /* SeqHead */
  10844. nbSeq = *ip++;
  10845. if (!nbSeq) {
  10846. *nbSeqPtr=0;
  10847. RETURN_ERROR_IF(srcSize != 1, srcSize_wrong, "");
  10848. return 1;
  10849. }
  10850. if (nbSeq > 0x7F) {
  10851. if (nbSeq == 0xFF) {
  10852. RETURN_ERROR_IF(ip+2 > iend, srcSize_wrong, "");
  10853. nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
  10854. } else {
  10855. RETURN_ERROR_IF(ip >= iend, srcSize_wrong, "");
  10856. nbSeq = ((nbSeq-0x80)<<8) + *ip++;
  10857. }
  10858. }
  10859. *nbSeqPtr = nbSeq;
  10860. /* FSE table descriptors */
  10861. RETURN_ERROR_IF(ip+1 > iend, srcSize_wrong, ""); /* minimum possible size: 1 byte for symbol encoding types */
  10862. { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
  10863. symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
  10864. symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
  10865. ip++;
  10866. /* Build DTables */
  10867. { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,
  10868. LLtype, MaxLL, LLFSELog,
  10869. ip, iend-ip,
  10870. LL_base, LL_bits,
  10871. LL_defaultDTable, dctx->fseEntropy,
  10872. dctx->ddictIsCold, nbSeq);
  10873. RETURN_ERROR_IF(ZSTD_isError(llhSize), corruption_detected, "ZSTD_buildSeqTable failed");
  10874. ip += llhSize;
  10875. }
  10876. { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,
  10877. OFtype, MaxOff, OffFSELog,
  10878. ip, iend-ip,
  10879. OF_base, OF_bits,
  10880. OF_defaultDTable, dctx->fseEntropy,
  10881. dctx->ddictIsCold, nbSeq);
  10882. RETURN_ERROR_IF(ZSTD_isError(ofhSize), corruption_detected, "ZSTD_buildSeqTable failed");
  10883. ip += ofhSize;
  10884. }
  10885. { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,
  10886. MLtype, MaxML, MLFSELog,
  10887. ip, iend-ip,
  10888. ML_base, ML_bits,
  10889. ML_defaultDTable, dctx->fseEntropy,
  10890. dctx->ddictIsCold, nbSeq);
  10891. RETURN_ERROR_IF(ZSTD_isError(mlhSize), corruption_detected, "ZSTD_buildSeqTable failed");
  10892. ip += mlhSize;
  10893. }
  10894. }
  10895. return ip-istart;
  10896. }
  10897. typedef struct {
  10898. size_t litLength;
  10899. size_t matchLength;
  10900. size_t offset;
  10901. const BYTE* match;
  10902. } seq_t;
  10903. typedef struct {
  10904. size_t state;
  10905. const ZSTD_seqSymbol* table;
  10906. } ZSTD_fseState;
  10907. typedef struct {
  10908. BIT_DStream_t DStream;
  10909. ZSTD_fseState stateLL;
  10910. ZSTD_fseState stateOffb;
  10911. ZSTD_fseState stateML;
  10912. size_t prevOffset[ZSTD_REP_NUM];
  10913. const BYTE* prefixStart;
  10914. const BYTE* dictEnd;
  10915. size_t pos;
  10916. } seqState_t;
  10917. /*! ZSTD_overlapCopy8() :
  10918. * Copies 8 bytes from ip to op and updates op and ip where ip <= op.
  10919. * If the offset is < 8 then the offset is spread to at least 8 bytes.
  10920. *
  10921. * Precondition: *ip <= *op
  10922. * Postcondition: *op - *op >= 8
  10923. */
  10924. HINT_INLINE void ZSTD_overlapCopy8(BYTE** op, BYTE const** ip, size_t offset) {
  10925. assert(*ip <= *op);
  10926. if (offset < 8) {
  10927. /* close range match, overlap */
  10928. static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
  10929. static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
  10930. int const sub2 = dec64table[offset];
  10931. (*op)[0] = (*ip)[0];
  10932. (*op)[1] = (*ip)[1];
  10933. (*op)[2] = (*ip)[2];
  10934. (*op)[3] = (*ip)[3];
  10935. *ip += dec32table[offset];
  10936. ZSTD_copy4(*op+4, *ip);
  10937. *ip -= sub2;
  10938. } else {
  10939. ZSTD_copy8(*op, *ip);
  10940. }
  10941. *ip += 8;
  10942. *op += 8;
  10943. assert(*op - *ip >= 8);
  10944. }
  10945. /*! ZSTD_safecopy() :
  10946. * Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer
  10947. * and write up to 16 bytes past oend_w (op >= oend_w is allowed).
  10948. * This function is only called in the uncommon case where the sequence is near the end of the block. It
  10949. * should be fast for a single long sequence, but can be slow for several short sequences.
  10950. *
  10951. * @param ovtype controls the overlap detection
  10952. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  10953. * - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart.
  10954. * The src buffer must be before the dst buffer.
  10955. */
  10956. static void ZSTD_safecopy(BYTE* op, BYTE* const oend_w, BYTE const* ip, ptrdiff_t length, ZSTD_overlap_e ovtype) {
  10957. ptrdiff_t const diff = op - ip;
  10958. BYTE* const oend = op + length;
  10959. assert((ovtype == ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) ||
  10960. (ovtype == ZSTD_overlap_src_before_dst && diff >= 0));
  10961. if (length < 8) {
  10962. /* Handle short lengths. */
  10963. while (op < oend) *op++ = *ip++;
  10964. return;
  10965. }
  10966. if (ovtype == ZSTD_overlap_src_before_dst) {
  10967. /* Copy 8 bytes and ensure the offset >= 8 when there can be overlap. */
  10968. assert(length >= 8);
  10969. ZSTD_overlapCopy8(&op, &ip, diff);
  10970. assert(op - ip >= 8);
  10971. assert(op <= oend);
  10972. }
  10973. if (oend <= oend_w) {
  10974. /* No risk of overwrite. */
  10975. ZSTD_wildcopy(op, ip, length, ovtype);
  10976. return;
  10977. }
  10978. if (op <= oend_w) {
  10979. /* Wildcopy until we get close to the end. */
  10980. assert(oend > oend_w);
  10981. ZSTD_wildcopy(op, ip, oend_w - op, ovtype);
  10982. ip += oend_w - op;
  10983. op = oend_w;
  10984. }
  10985. /* Handle the leftovers. */
  10986. while (op < oend) *op++ = *ip++;
  10987. }
  10988. /* ZSTD_execSequenceEnd():
  10989. * This version handles cases that are near the end of the output buffer. It requires
  10990. * more careful checks to make sure there is no overflow. By separating out these hard
  10991. * and unlikely cases, we can speed up the common cases.
  10992. *
  10993. * NOTE: This function needs to be fast for a single long sequence, but doesn't need
  10994. * to be optimized for many small sequences, since those fall into ZSTD_execSequence().
  10995. */
  10996. FORCE_NOINLINE
  10997. size_t ZSTD_execSequenceEnd(BYTE* op,
  10998. BYTE* const oend, seq_t sequence,
  10999. const BYTE** litPtr, const BYTE* const litLimit,
  11000. const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
  11001. {
  11002. BYTE* const oLitEnd = op + sequence.litLength;
  11003. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  11004. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  11005. const BYTE* match = oLitEnd - sequence.offset;
  11006. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  11007. /* bounds checks : careful of address space overflow in 32-bit mode */
  11008. RETURN_ERROR_IF(sequenceLength > (size_t)(oend - op), dstSize_tooSmall, "last match must fit within dstBuffer");
  11009. RETURN_ERROR_IF(sequence.litLength > (size_t)(litLimit - *litPtr), corruption_detected, "try to read beyond literal buffer");
  11010. assert(op < op + sequenceLength);
  11011. assert(oLitEnd < op + sequenceLength);
  11012. /* copy literals */
  11013. ZSTD_safecopy(op, oend_w, *litPtr, sequence.litLength, ZSTD_no_overlap);
  11014. op = oLitEnd;
  11015. *litPtr = iLitEnd;
  11016. /* copy Match */
  11017. if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
  11018. /* offset beyond prefix */
  11019. RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, "");
  11020. match = dictEnd - (prefixStart-match);
  11021. if (match + sequence.matchLength <= dictEnd) {
  11022. memmove(oLitEnd, match, sequence.matchLength);
  11023. return sequenceLength;
  11024. }
  11025. /* span extDict & currentPrefixSegment */
  11026. { size_t const length1 = dictEnd - match;
  11027. memmove(oLitEnd, match, length1);
  11028. op = oLitEnd + length1;
  11029. sequence.matchLength -= length1;
  11030. match = prefixStart;
  11031. } }
  11032. ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst);
  11033. return sequenceLength;
  11034. }
  11035. HINT_INLINE
  11036. size_t ZSTD_execSequence(BYTE* op,
  11037. BYTE* const oend, seq_t sequence,
  11038. const BYTE** litPtr, const BYTE* const litLimit,
  11039. const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
  11040. {
  11041. BYTE* const oLitEnd = op + sequence.litLength;
  11042. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  11043. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  11044. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; /* risk : address space underflow on oend=NULL */
  11045. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  11046. const BYTE* match = oLitEnd - sequence.offset;
  11047. assert(op != NULL /* Precondition */);
  11048. assert(oend_w < oend /* No underflow */);
  11049. /* Handle edge cases in a slow path:
  11050. * - Read beyond end of literals
  11051. * - Match end is within WILDCOPY_OVERLIMIT of oend
  11052. * - 32-bit mode and the match length overflows
  11053. */
  11054. if (UNLIKELY(
  11055. iLitEnd > litLimit ||
  11056. oMatchEnd > oend_w ||
  11057. (MEM_32bits() && (size_t)(oend - op) < sequenceLength + WILDCOPY_OVERLENGTH)))
  11058. return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd);
  11059. /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */
  11060. assert(op <= oLitEnd /* No overflow */);
  11061. assert(oLitEnd < oMatchEnd /* Non-zero match & no overflow */);
  11062. assert(oMatchEnd <= oend /* No underflow */);
  11063. assert(iLitEnd <= litLimit /* Literal length is in bounds */);
  11064. assert(oLitEnd <= oend_w /* Can wildcopy literals */);
  11065. assert(oMatchEnd <= oend_w /* Can wildcopy matches */);
  11066. /* Copy Literals:
  11067. * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9.
  11068. * We likely don't need the full 32-byte wildcopy.
  11069. */
  11070. assert(WILDCOPY_OVERLENGTH >= 16);
  11071. ZSTD_copy16(op, (*litPtr));
  11072. if (UNLIKELY(sequence.litLength > 16)) {
  11073. ZSTD_wildcopy(op+16, (*litPtr)+16, sequence.litLength-16, ZSTD_no_overlap);
  11074. }
  11075. op = oLitEnd;
  11076. *litPtr = iLitEnd; /* update for next sequence */
  11077. /* Copy Match */
  11078. if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
  11079. /* offset beyond prefix -> go into extDict */
  11080. RETURN_ERROR_IF(UNLIKELY(sequence.offset > (size_t)(oLitEnd - virtualStart)), corruption_detected, "");
  11081. match = dictEnd + (match - prefixStart);
  11082. if (match + sequence.matchLength <= dictEnd) {
  11083. memmove(oLitEnd, match, sequence.matchLength);
  11084. return sequenceLength;
  11085. }
  11086. /* span extDict & currentPrefixSegment */
  11087. { size_t const length1 = dictEnd - match;
  11088. memmove(oLitEnd, match, length1);
  11089. op = oLitEnd + length1;
  11090. sequence.matchLength -= length1;
  11091. match = prefixStart;
  11092. } }
  11093. /* Match within prefix of 1 or more bytes */
  11094. assert(op <= oMatchEnd);
  11095. assert(oMatchEnd <= oend_w);
  11096. assert(match >= prefixStart);
  11097. assert(sequence.matchLength >= 1);
  11098. /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy
  11099. * without overlap checking.
  11100. */
  11101. if (LIKELY(sequence.offset >= WILDCOPY_VECLEN)) {
  11102. /* We bet on a full wildcopy for matches, since we expect matches to be
  11103. * longer than literals (in general). In silesia, ~10% of matches are longer
  11104. * than 16 bytes.
  11105. */
  11106. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength, ZSTD_no_overlap);
  11107. return sequenceLength;
  11108. }
  11109. assert(sequence.offset < WILDCOPY_VECLEN);
  11110. /* Copy 8 bytes and spread the offset to be >= 8. */
  11111. ZSTD_overlapCopy8(&op, &match, sequence.offset);
  11112. /* If the match length is > 8 bytes, then continue with the wildcopy. */
  11113. if (sequence.matchLength > 8) {
  11114. assert(op < oMatchEnd);
  11115. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8, ZSTD_overlap_src_before_dst);
  11116. }
  11117. return sequenceLength;
  11118. }
  11119. static void
  11120. ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt)
  11121. {
  11122. const void* ptr = dt;
  11123. const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr;
  11124. DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
  11125. DEBUGLOG(6, "ZSTD_initFseState : val=%u using %u bits",
  11126. (U32)DStatePtr->state, DTableH->tableLog);
  11127. BIT_reloadDStream(bitD);
  11128. DStatePtr->table = dt + 1;
  11129. }
  11130. FORCE_INLINE_TEMPLATE void
  11131. ZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD)
  11132. {
  11133. ZSTD_seqSymbol const DInfo = DStatePtr->table[DStatePtr->state];
  11134. U32 const nbBits = DInfo.nbBits;
  11135. size_t const lowBits = BIT_readBits(bitD, nbBits);
  11136. DStatePtr->state = DInfo.nextState + lowBits;
  11137. }
  11138. FORCE_INLINE_TEMPLATE void
  11139. ZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ZSTD_seqSymbol const DInfo)
  11140. {
  11141. U32 const nbBits = DInfo.nbBits;
  11142. size_t const lowBits = BIT_readBits(bitD, nbBits);
  11143. DStatePtr->state = DInfo.nextState + lowBits;
  11144. }
  11145. /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum
  11146. * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1)
  11147. * bits before reloading. This value is the maximum number of bytes we read
  11148. * after reloading when we are decoding long offsets.
  11149. */
  11150. #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \
  11151. (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \
  11152. ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \
  11153. : 0)
  11154. typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e;
  11155. typedef enum { ZSTD_p_noPrefetch=0, ZSTD_p_prefetch=1 } ZSTD_prefetch_e;
  11156. FORCE_INLINE_TEMPLATE seq_t
  11157. ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets, const ZSTD_prefetch_e prefetch)
  11158. {
  11159. seq_t seq;
  11160. ZSTD_seqSymbol const llDInfo = seqState->stateLL.table[seqState->stateLL.state];
  11161. ZSTD_seqSymbol const mlDInfo = seqState->stateML.table[seqState->stateML.state];
  11162. ZSTD_seqSymbol const ofDInfo = seqState->stateOffb.table[seqState->stateOffb.state];
  11163. U32 const llBase = llDInfo.baseValue;
  11164. U32 const mlBase = mlDInfo.baseValue;
  11165. U32 const ofBase = ofDInfo.baseValue;
  11166. BYTE const llBits = llDInfo.nbAdditionalBits;
  11167. BYTE const mlBits = mlDInfo.nbAdditionalBits;
  11168. BYTE const ofBits = ofDInfo.nbAdditionalBits;
  11169. BYTE const totalBits = llBits+mlBits+ofBits;
  11170. /* sequence */
  11171. { size_t offset;
  11172. if (ofBits > 1) {
  11173. ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
  11174. ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
  11175. assert(ofBits <= MaxOff);
  11176. if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) {
  11177. U32 const extraBits = ofBits - MIN(ofBits, 32 - seqState->DStream.bitsConsumed);
  11178. offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
  11179. BIT_reloadDStream(&seqState->DStream);
  11180. if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
  11181. assert(extraBits <= LONG_OFFSETS_MAX_EXTRA_BITS_32); /* to avoid another reload */
  11182. } else {
  11183. offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
  11184. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
  11185. }
  11186. seqState->prevOffset[2] = seqState->prevOffset[1];
  11187. seqState->prevOffset[1] = seqState->prevOffset[0];
  11188. seqState->prevOffset[0] = offset;
  11189. } else {
  11190. U32 const ll0 = (llBase == 0);
  11191. if (LIKELY((ofBits == 0))) {
  11192. if (LIKELY(!ll0))
  11193. offset = seqState->prevOffset[0];
  11194. else {
  11195. offset = seqState->prevOffset[1];
  11196. seqState->prevOffset[1] = seqState->prevOffset[0];
  11197. seqState->prevOffset[0] = offset;
  11198. }
  11199. } else {
  11200. offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1);
  11201. { size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
  11202. temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
  11203. if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
  11204. seqState->prevOffset[1] = seqState->prevOffset[0];
  11205. seqState->prevOffset[0] = offset = temp;
  11206. } } }
  11207. seq.offset = offset;
  11208. }
  11209. seq.matchLength = mlBase;
  11210. if (mlBits > 0)
  11211. seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/);
  11212. if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
  11213. BIT_reloadDStream(&seqState->DStream);
  11214. if (MEM_64bits() && UNLIKELY(totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
  11215. BIT_reloadDStream(&seqState->DStream);
  11216. /* Ensure there are enough bits to read the rest of data in 64-bit mode. */
  11217. ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
  11218. seq.litLength = llBase;
  11219. if (llBits > 0)
  11220. seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits/*>0*/);
  11221. if (MEM_32bits())
  11222. BIT_reloadDStream(&seqState->DStream);
  11223. DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u",
  11224. (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
  11225. if (prefetch == ZSTD_p_prefetch) {
  11226. size_t const pos = seqState->pos + seq.litLength;
  11227. const BYTE* const matchBase = (seq.offset > pos) ? seqState->dictEnd : seqState->prefixStart;
  11228. seq.match = matchBase + pos - seq.offset; /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted.
  11229. * No consequence though : no memory access will occur, offset is only used for prefetching */
  11230. seqState->pos = pos + seq.matchLength;
  11231. }
  11232. /* ANS state update
  11233. * gcc-9.0.0 does 2.5% worse with ZSTD_updateFseStateWithDInfo().
  11234. * clang-9.2.0 does 7% worse with ZSTD_updateFseState().
  11235. * Naturally it seems like ZSTD_updateFseStateWithDInfo() should be the
  11236. * better option, so it is the default for other compilers. But, if you
  11237. * measure that it is worse, please put up a pull request.
  11238. */
  11239. {
  11240. #if defined(__GNUC__) && !defined(__clang__)
  11241. const int kUseUpdateFseState = 1;
  11242. #else
  11243. const int kUseUpdateFseState = 0;
  11244. #endif
  11245. if (kUseUpdateFseState) {
  11246. ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
  11247. ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
  11248. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  11249. ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
  11250. } else {
  11251. ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llDInfo); /* <= 9 bits */
  11252. ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlDInfo); /* <= 9 bits */
  11253. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  11254. ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofDInfo); /* <= 8 bits */
  11255. }
  11256. }
  11257. return seq;
  11258. }
  11259. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  11260. MEM_STATIC int ZSTD_dictionaryIsActive(ZSTD_DCtx const* dctx, BYTE const* prefixStart, BYTE const* oLitEnd)
  11261. {
  11262. size_t const windowSize = dctx->fParams.windowSize;
  11263. /* No dictionary used. */
  11264. if (dctx->dictContentEndForFuzzing == NULL) return 0;
  11265. /* Dictionary is our prefix. */
  11266. if (prefixStart == dctx->dictContentBeginForFuzzing) return 1;
  11267. /* Dictionary is not our ext-dict. */
  11268. if (dctx->dictEnd != dctx->dictContentEndForFuzzing) return 0;
  11269. /* Dictionary is not within our window size. */
  11270. if ((size_t)(oLitEnd - prefixStart) >= windowSize) return 0;
  11271. /* Dictionary is active. */
  11272. return 1;
  11273. }
  11274. MEM_STATIC void ZSTD_assertValidSequence(
  11275. ZSTD_DCtx const* dctx,
  11276. BYTE const* op, BYTE const* oend,
  11277. seq_t const seq,
  11278. BYTE const* prefixStart, BYTE const* virtualStart)
  11279. {
  11280. #if DEBUGLEVEL >= 1
  11281. size_t const windowSize = dctx->fParams.windowSize;
  11282. size_t const sequenceSize = seq.litLength + seq.matchLength;
  11283. BYTE const* const oLitEnd = op + seq.litLength;
  11284. DEBUGLOG(6, "Checking sequence: litL=%u matchL=%u offset=%u",
  11285. (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
  11286. assert(op <= oend);
  11287. assert((size_t)(oend - op) >= sequenceSize);
  11288. assert(sequenceSize <= ZSTD_BLOCKSIZE_MAX);
  11289. if (ZSTD_dictionaryIsActive(dctx, prefixStart, oLitEnd)) {
  11290. size_t const dictSize = (size_t)((char const*)dctx->dictContentEndForFuzzing - (char const*)dctx->dictContentBeginForFuzzing);
  11291. /* Offset must be within the dictionary. */
  11292. assert(seq.offset <= (size_t)(oLitEnd - virtualStart));
  11293. assert(seq.offset <= windowSize + dictSize);
  11294. } else {
  11295. /* Offset must be within our window. */
  11296. assert(seq.offset <= windowSize);
  11297. }
  11298. #else
  11299. (void)dctx, (void)op, (void)oend, (void)seq, (void)prefixStart, (void)virtualStart;
  11300. #endif
  11301. }
  11302. #endif
  11303. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  11304. FORCE_INLINE_TEMPLATE size_t
  11305. DONT_VECTORIZE
  11306. ZSTD_decompressSequences_body( ZSTD_DCtx* dctx,
  11307. void* dst, size_t maxDstSize,
  11308. const void* seqStart, size_t seqSize, int nbSeq,
  11309. const ZSTD_longOffset_e isLongOffset,
  11310. const int frame)
  11311. {
  11312. const BYTE* ip = (const BYTE*)seqStart;
  11313. const BYTE* const iend = ip + seqSize;
  11314. BYTE* const ostart = (BYTE* const)dst;
  11315. BYTE* const oend = ostart + maxDstSize;
  11316. BYTE* op = ostart;
  11317. const BYTE* litPtr = dctx->litPtr;
  11318. const BYTE* const litEnd = litPtr + dctx->litSize;
  11319. const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
  11320. const BYTE* const vBase = (const BYTE*) (dctx->virtualStart);
  11321. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  11322. DEBUGLOG(5, "ZSTD_decompressSequences_body");
  11323. (void)frame;
  11324. /* Regen sequences */
  11325. if (nbSeq) {
  11326. seqState_t seqState;
  11327. size_t error = 0;
  11328. dctx->fseEntropy = 1;
  11329. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  11330. RETURN_ERROR_IF(
  11331. ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
  11332. corruption_detected, "");
  11333. ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  11334. ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  11335. ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  11336. assert(dst != NULL);
  11337. ZSTD_STATIC_ASSERT(
  11338. BIT_DStream_unfinished < BIT_DStream_completed &&
  11339. BIT_DStream_endOfBuffer < BIT_DStream_completed &&
  11340. BIT_DStream_completed < BIT_DStream_overflow);
  11341. #if defined(__GNUC__) && defined(__x86_64__)
  11342. /* Align the decompression loop to 32 + 16 bytes.
  11343. *
  11344. * zstd compiled with gcc-9 on an Intel i9-9900k shows 10% decompression
  11345. * speed swings based on the alignment of the decompression loop. This
  11346. * performance swing is caused by parts of the decompression loop falling
  11347. * out of the DSB. The entire decompression loop should fit in the DSB,
  11348. * when it can't we get much worse performance. You can measure if you've
  11349. * hit the good case or the bad case with this perf command for some
  11350. * compressed file test.zst:
  11351. *
  11352. * perf stat -e cycles -e instructions -e idq.all_dsb_cycles_any_uops \
  11353. * -e idq.all_mite_cycles_any_uops -- ./zstd -tq test.zst
  11354. *
  11355. * If you see most cycles served out of the MITE you've hit the bad case.
  11356. * If you see most cycles served out of the DSB you've hit the good case.
  11357. * If it is pretty even then you may be in an okay case.
  11358. *
  11359. * I've been able to reproduce this issue on the following CPUs:
  11360. * - Kabylake: Macbook Pro (15-inch, 2019) 2.4 GHz Intel Core i9
  11361. * Use Instruments->Counters to get DSB/MITE cycles.
  11362. * I never got performance swings, but I was able to
  11363. * go from the good case of mostly DSB to half of the
  11364. * cycles served from MITE.
  11365. * - Coffeelake: Intel i9-9900k
  11366. *
  11367. * I haven't been able to reproduce the instability or DSB misses on any
  11368. * of the following CPUS:
  11369. * - Haswell
  11370. * - Broadwell: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GH
  11371. * - Skylake
  11372. *
  11373. * If you are seeing performance stability this script can help test.
  11374. * It tests on 4 commits in zstd where I saw performance change.
  11375. *
  11376. * https://gist.github.com/terrelln/9889fc06a423fd5ca6e99351564473f4
  11377. */
  11378. __asm__(".p2align 5");
  11379. __asm__("nop");
  11380. __asm__(".p2align 4");
  11381. #endif
  11382. for ( ; ; ) {
  11383. seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_noPrefetch);
  11384. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd);
  11385. #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
  11386. assert(!ZSTD_isError(oneSeqSize));
  11387. if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase);
  11388. #endif
  11389. DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize);
  11390. BIT_reloadDStream(&(seqState.DStream));
  11391. /* gcc and clang both don't like early returns in this loop.
  11392. * gcc doesn't like early breaks either.
  11393. * Instead save an error and report it at the end.
  11394. * When there is an error, don't increment op, so we don't
  11395. * overwrite.
  11396. */
  11397. if (UNLIKELY(ZSTD_isError(oneSeqSize))) error = oneSeqSize;
  11398. else op += oneSeqSize;
  11399. if (UNLIKELY(!--nbSeq)) break;
  11400. }
  11401. /* check if reached exact end */
  11402. DEBUGLOG(5, "ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i", nbSeq);
  11403. if (ZSTD_isError(error)) return error;
  11404. RETURN_ERROR_IF(nbSeq, corruption_detected, "");
  11405. RETURN_ERROR_IF(BIT_reloadDStream(&seqState.DStream) < BIT_DStream_completed, corruption_detected, "");
  11406. /* save reps for next block */
  11407. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  11408. }
  11409. /* last literal segment */
  11410. { size_t const lastLLSize = litEnd - litPtr;
  11411. RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, "");
  11412. if (op != NULL) {
  11413. memcpy(op, litPtr, lastLLSize);
  11414. op += lastLLSize;
  11415. }
  11416. }
  11417. return op-ostart;
  11418. }
  11419. static size_t
  11420. ZSTD_decompressSequences_default(ZSTD_DCtx* dctx,
  11421. void* dst, size_t maxDstSize,
  11422. const void* seqStart, size_t seqSize, int nbSeq,
  11423. const ZSTD_longOffset_e isLongOffset,
  11424. const int frame)
  11425. {
  11426. return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11427. }
  11428. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  11429. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  11430. FORCE_INLINE_TEMPLATE size_t
  11431. ZSTD_decompressSequencesLong_body(
  11432. ZSTD_DCtx* dctx,
  11433. void* dst, size_t maxDstSize,
  11434. const void* seqStart, size_t seqSize, int nbSeq,
  11435. const ZSTD_longOffset_e isLongOffset,
  11436. const int frame)
  11437. {
  11438. const BYTE* ip = (const BYTE*)seqStart;
  11439. const BYTE* const iend = ip + seqSize;
  11440. BYTE* const ostart = (BYTE* const)dst;
  11441. BYTE* const oend = ostart + maxDstSize;
  11442. BYTE* op = ostart;
  11443. const BYTE* litPtr = dctx->litPtr;
  11444. const BYTE* const litEnd = litPtr + dctx->litSize;
  11445. const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
  11446. const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart);
  11447. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  11448. (void)frame;
  11449. /* Regen sequences */
  11450. if (nbSeq) {
  11451. #define STORED_SEQS 4
  11452. #define STORED_SEQS_MASK (STORED_SEQS-1)
  11453. #define ADVANCED_SEQS 4
  11454. seq_t sequences[STORED_SEQS];
  11455. int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);
  11456. seqState_t seqState;
  11457. int seqNb;
  11458. dctx->fseEntropy = 1;
  11459. { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  11460. seqState.prefixStart = prefixStart;
  11461. seqState.pos = (size_t)(op-prefixStart);
  11462. seqState.dictEnd = dictEnd;
  11463. assert(dst != NULL);
  11464. assert(iend >= ip);
  11465. RETURN_ERROR_IF(
  11466. ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
  11467. corruption_detected, "");
  11468. ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  11469. ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  11470. ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  11471. /* prepare in advance */
  11472. for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && (seqNb<seqAdvance); seqNb++) {
  11473. sequences[seqNb] = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);
  11474. PREFETCH_L1(sequences[seqNb].match); PREFETCH_L1(sequences[seqNb].match + sequences[seqNb].matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
  11475. }
  11476. RETURN_ERROR_IF(seqNb<seqAdvance, corruption_detected, "");
  11477. /* decode and decompress */
  11478. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (seqNb<nbSeq) ; seqNb++) {
  11479. seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);
  11480. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
  11481. #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
  11482. assert(!ZSTD_isError(oneSeqSize));
  11483. if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], prefixStart, dictStart);
  11484. #endif
  11485. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  11486. PREFETCH_L1(sequence.match); PREFETCH_L1(sequence.match + sequence.matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
  11487. sequences[seqNb & STORED_SEQS_MASK] = sequence;
  11488. op += oneSeqSize;
  11489. }
  11490. RETURN_ERROR_IF(seqNb<nbSeq, corruption_detected, "");
  11491. /* finish queue */
  11492. seqNb -= seqAdvance;
  11493. for ( ; seqNb<nbSeq ; seqNb++) {
  11494. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb&STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
  11495. #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
  11496. assert(!ZSTD_isError(oneSeqSize));
  11497. if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[seqNb&STORED_SEQS_MASK], prefixStart, dictStart);
  11498. #endif
  11499. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  11500. op += oneSeqSize;
  11501. }
  11502. /* save reps for next block */
  11503. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  11504. }
  11505. /* last literal segment */
  11506. { size_t const lastLLSize = litEnd - litPtr;
  11507. RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, "");
  11508. if (op != NULL) {
  11509. memcpy(op, litPtr, lastLLSize);
  11510. op += lastLLSize;
  11511. }
  11512. }
  11513. return op-ostart;
  11514. }
  11515. static size_t
  11516. ZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx,
  11517. void* dst, size_t maxDstSize,
  11518. const void* seqStart, size_t seqSize, int nbSeq,
  11519. const ZSTD_longOffset_e isLongOffset,
  11520. const int frame)
  11521. {
  11522. return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11523. }
  11524. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  11525. #if DYNAMIC_BMI2
  11526. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  11527. static TARGET_ATTRIBUTE("bmi2") size_t
  11528. DONT_VECTORIZE
  11529. ZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx,
  11530. void* dst, size_t maxDstSize,
  11531. const void* seqStart, size_t seqSize, int nbSeq,
  11532. const ZSTD_longOffset_e isLongOffset,
  11533. const int frame)
  11534. {
  11535. return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11536. }
  11537. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  11538. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  11539. static TARGET_ATTRIBUTE("bmi2") size_t
  11540. ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx,
  11541. void* dst, size_t maxDstSize,
  11542. const void* seqStart, size_t seqSize, int nbSeq,
  11543. const ZSTD_longOffset_e isLongOffset,
  11544. const int frame)
  11545. {
  11546. return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11547. }
  11548. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  11549. #endif /* DYNAMIC_BMI2 */
  11550. typedef size_t (*ZSTD_decompressSequences_t)(
  11551. ZSTD_DCtx* dctx,
  11552. void* dst, size_t maxDstSize,
  11553. const void* seqStart, size_t seqSize, int nbSeq,
  11554. const ZSTD_longOffset_e isLongOffset,
  11555. const int frame);
  11556. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  11557. static size_t
  11558. ZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize,
  11559. const void* seqStart, size_t seqSize, int nbSeq,
  11560. const ZSTD_longOffset_e isLongOffset,
  11561. const int frame)
  11562. {
  11563. DEBUGLOG(5, "ZSTD_decompressSequences");
  11564. #if DYNAMIC_BMI2
  11565. if (dctx->bmi2) {
  11566. return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11567. }
  11568. #endif
  11569. return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11570. }
  11571. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  11572. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  11573. /* ZSTD_decompressSequencesLong() :
  11574. * decompression function triggered when a minimum share of offsets is considered "long",
  11575. * aka out of cache.
  11576. * note : "long" definition seems overloaded here, sometimes meaning "wider than bitstream register", and sometimes meaning "farther than memory cache distance".
  11577. * This function will try to mitigate main memory latency through the use of prefetching */
  11578. static size_t
  11579. ZSTD_decompressSequencesLong(ZSTD_DCtx* dctx,
  11580. void* dst, size_t maxDstSize,
  11581. const void* seqStart, size_t seqSize, int nbSeq,
  11582. const ZSTD_longOffset_e isLongOffset,
  11583. const int frame)
  11584. {
  11585. DEBUGLOG(5, "ZSTD_decompressSequencesLong");
  11586. #if DYNAMIC_BMI2
  11587. if (dctx->bmi2) {
  11588. return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11589. }
  11590. #endif
  11591. return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
  11592. }
  11593. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  11594. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  11595. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  11596. /* ZSTD_getLongOffsetsShare() :
  11597. * condition : offTable must be valid
  11598. * @return : "share" of long offsets (arbitrarily defined as > (1<<23))
  11599. * compared to maximum possible of (1<<OffFSELog) */
  11600. static unsigned
  11601. ZSTD_getLongOffsetsShare(const ZSTD_seqSymbol* offTable)
  11602. {
  11603. const void* ptr = offTable;
  11604. U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog;
  11605. const ZSTD_seqSymbol* table = offTable + 1;
  11606. U32 const max = 1 << tableLog;
  11607. U32 u, total = 0;
  11608. DEBUGLOG(5, "ZSTD_getLongOffsetsShare: (tableLog=%u)", tableLog);
  11609. assert(max <= (1 << OffFSELog)); /* max not too large */
  11610. for (u=0; u<max; u++) {
  11611. if (table[u].nbAdditionalBits > 22) total += 1;
  11612. }
  11613. assert(tableLog <= OffFSELog);
  11614. total <<= (OffFSELog - tableLog); /* scale to OffFSELog */
  11615. return total;
  11616. }
  11617. #endif
  11618. size_t
  11619. ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
  11620. void* dst, size_t dstCapacity,
  11621. const void* src, size_t srcSize, const int frame)
  11622. { /* blockType == blockCompressed */
  11623. const BYTE* ip = (const BYTE*)src;
  11624. /* isLongOffset must be true if there are long offsets.
  11625. * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.
  11626. * We don't expect that to be the case in 64-bit mode.
  11627. * In block mode, window size is not known, so we have to be conservative.
  11628. * (note: but it could be evaluated from current-lowLimit)
  11629. */
  11630. ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || (dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))));
  11631. DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize);
  11632. RETURN_ERROR_IF(srcSize >= ZSTD_BLOCKSIZE_MAX, srcSize_wrong, "");
  11633. /* Decode literals section */
  11634. { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
  11635. DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : %u", (U32)litCSize);
  11636. if (ZSTD_isError(litCSize)) return litCSize;
  11637. ip += litCSize;
  11638. srcSize -= litCSize;
  11639. }
  11640. /* Build Decoding Tables */
  11641. {
  11642. /* These macros control at build-time which decompressor implementation
  11643. * we use. If neither is defined, we do some inspection and dispatch at
  11644. * runtime.
  11645. */
  11646. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  11647. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  11648. int usePrefetchDecoder = dctx->ddictIsCold;
  11649. #endif
  11650. int nbSeq;
  11651. size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize);
  11652. if (ZSTD_isError(seqHSize)) return seqHSize;
  11653. ip += seqHSize;
  11654. srcSize -= seqHSize;
  11655. RETURN_ERROR_IF(dst == NULL && nbSeq > 0, dstSize_tooSmall, "NULL not handled");
  11656. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  11657. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  11658. if ( !usePrefetchDecoder
  11659. && (!frame || (dctx->fParams.windowSize > (1<<24)))
  11660. && (nbSeq>ADVANCED_SEQS) ) { /* could probably use a larger nbSeq limit */
  11661. U32 const shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr);
  11662. U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */
  11663. usePrefetchDecoder = (shareLongOffsets >= minShare);
  11664. }
  11665. #endif
  11666. dctx->ddictIsCold = 0;
  11667. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  11668. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  11669. if (usePrefetchDecoder)
  11670. #endif
  11671. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  11672. return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);
  11673. #endif
  11674. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  11675. /* else */
  11676. return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);
  11677. #endif
  11678. }
  11679. }
  11680. void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
  11681. {
  11682. if (dst != dctx->previousDstEnd) { /* not contiguous */
  11683. dctx->dictEnd = dctx->previousDstEnd;
  11684. dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
  11685. dctx->prefixStart = dst;
  11686. dctx->previousDstEnd = dst;
  11687. }
  11688. }
  11689. size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
  11690. void* dst, size_t dstCapacity,
  11691. const void* src, size_t srcSize)
  11692. {
  11693. size_t dSize;
  11694. ZSTD_checkContinuity(dctx, dst);
  11695. dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0);
  11696. dctx->previousDstEnd = (char*)dst + dSize;
  11697. return dSize;
  11698. }
  11699. /**** ended inlining decompress/zstd_decompress_block.c ****/