fuzzer.c 199 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /*-************************************
  11. * Compiler specific
  12. **************************************/
  13. #ifdef _MSC_VER /* Visual Studio */
  14. # define _CRT_SECURE_NO_WARNINGS /* fgets */
  15. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  16. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  17. #endif
  18. /*-************************************
  19. * Includes
  20. **************************************/
  21. #include <stdlib.h> /* free */
  22. #include <stdio.h> /* fgets, sscanf */
  23. #include <string.h> /* strcmp */
  24. #undef NDEBUG
  25. #include <assert.h>
  26. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */
  27. #include "debug.h" /* DEBUG_STATIC_ASSERT */
  28. #include "fse.h"
  29. #define ZSTD_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still test some deprecated functions */
  30. #include "zstd.h" /* ZSTD_VERSION_STRING */
  31. #include "zstd_errors.h" /* ZSTD_getErrorCode */
  32. #define ZDICT_STATIC_LINKING_ONLY
  33. #include "zdict.h" /* ZDICT_trainFromBuffer */
  34. #include "mem.h"
  35. #include "datagen.h" /* RDG_genBuffer */
  36. #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  37. #include "xxhash.h" /* XXH64 */
  38. #include "util.h"
  39. #include "timefn.h" /* SEC_TO_MICRO, UTIL_time_t, UTIL_TIME_INITIALIZER, UTIL_clockSpanMicro, UTIL_getTime */
  40. /* must be included after util.h, due to ERROR macro redefinition issue on Visual Studio */
  41. #include "zstd_internal.h" /* ZSTD_WORKSPACETOOLARGE_MAXDURATION, ZSTD_WORKSPACETOOLARGE_FACTOR, KB, MB */
  42. #include "threading.h" /* ZSTD_pthread_create, ZSTD_pthread_join */
  43. /*-************************************
  44. * Constants
  45. **************************************/
  46. #define GB *(1U<<30)
  47. static const int FUZ_compressibility_default = 50;
  48. static const int nbTestsDefault = 30000;
  49. /*-************************************
  50. * Display Macros
  51. **************************************/
  52. #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
  53. #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
  54. static U32 g_displayLevel = 2;
  55. static const U64 g_refreshRate = SEC_TO_MICRO / 6;
  56. static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
  57. #define DISPLAYUPDATE(l, ...) \
  58. if (g_displayLevel>=l) { \
  59. if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
  60. { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
  61. if (g_displayLevel>=4) fflush(stderr); } \
  62. }
  63. /*-*******************************************************
  64. * Compile time test
  65. *********************************************************/
  66. #undef MIN
  67. #undef MAX
  68. /* Declaring the function, to avoid -Wmissing-prototype */
  69. void FUZ_bug976(void);
  70. void FUZ_bug976(void)
  71. { /* these constants shall not depend on MIN() macro */
  72. assert(ZSTD_HASHLOG_MAX < 31);
  73. assert(ZSTD_CHAINLOG_MAX < 31);
  74. }
  75. /*-*******************************************************
  76. * Internal functions
  77. *********************************************************/
  78. #define MIN(a,b) ((a)<(b)?(a):(b))
  79. #define MAX(a,b) ((a)>(b)?(a):(b))
  80. #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  81. static U32 FUZ_rand(U32* src)
  82. {
  83. static const U32 prime1 = 2654435761U;
  84. static const U32 prime2 = 2246822519U;
  85. U32 rand32 = *src;
  86. rand32 *= prime1;
  87. rand32 += prime2;
  88. rand32 = FUZ_rotl32(rand32, 13);
  89. *src = rand32;
  90. return rand32 >> 5;
  91. }
  92. static U32 FUZ_highbit32(U32 v32)
  93. {
  94. unsigned nbBits = 0;
  95. if (v32==0) return 0;
  96. while (v32) v32 >>= 1, nbBits++;
  97. return nbBits;
  98. }
  99. /*=============================================
  100. * Test macros
  101. =============================================*/
  102. #define CHECK_Z(f) { \
  103. size_t const err = f; \
  104. if (ZSTD_isError(err)) { \
  105. DISPLAY("Error => %s : %s ", \
  106. #f, ZSTD_getErrorName(err)); \
  107. exit(1); \
  108. } }
  109. #define CHECK_VAR(var, fn) var = fn; if (ZSTD_isError(var)) { DISPLAYLEVEL(1, "%s : fails : %s \n", #fn, ZSTD_getErrorName(var)); goto _output_error; }
  110. #define CHECK_NEWV(var, fn) size_t const CHECK_VAR(var, fn)
  111. #define CHECK(fn) { CHECK_NEWV(__err, fn); }
  112. #define CHECKPLUS(var, fn, more) { CHECK_NEWV(var, fn); more; }
  113. #define CHECK_OP(op, lhs, rhs) { \
  114. if (!((lhs) op (rhs))) { \
  115. DISPLAY("Error L%u => FAILED %s %s %s ", __LINE__, #lhs, #op, #rhs); \
  116. goto _output_error; \
  117. } \
  118. }
  119. #define CHECK_EQ(lhs, rhs) CHECK_OP(==, lhs, rhs)
  120. #define CHECK_LT(lhs, rhs) CHECK_OP(<, lhs, rhs)
  121. /*=============================================
  122. * Memory Tests
  123. =============================================*/
  124. #if defined(__APPLE__) && defined(__MACH__)
  125. #include <malloc/malloc.h> /* malloc_size */
  126. typedef struct {
  127. unsigned long long totalMalloc;
  128. size_t currentMalloc;
  129. size_t peakMalloc;
  130. unsigned nbMalloc;
  131. unsigned nbFree;
  132. } mallocCounter_t;
  133. static const mallocCounter_t INIT_MALLOC_COUNTER = { 0, 0, 0, 0, 0 };
  134. static void* FUZ_mallocDebug(void* counter, size_t size)
  135. {
  136. mallocCounter_t* const mcPtr = (mallocCounter_t*)counter;
  137. void* const ptr = malloc(size);
  138. if (ptr==NULL) return NULL;
  139. DISPLAYLEVEL(4, "allocating %u KB => effectively %u KB \n",
  140. (unsigned)(size >> 10), (unsigned)(malloc_size(ptr) >> 10)); /* OS-X specific */
  141. mcPtr->totalMalloc += size;
  142. mcPtr->currentMalloc += size;
  143. if (mcPtr->currentMalloc > mcPtr->peakMalloc)
  144. mcPtr->peakMalloc = mcPtr->currentMalloc;
  145. mcPtr->nbMalloc += 1;
  146. return ptr;
  147. }
  148. static void FUZ_freeDebug(void* counter, void* address)
  149. {
  150. mallocCounter_t* const mcPtr = (mallocCounter_t*)counter;
  151. DISPLAYLEVEL(4, "freeing %u KB \n", (unsigned)(malloc_size(address) >> 10));
  152. mcPtr->nbFree += 1;
  153. mcPtr->currentMalloc -= malloc_size(address); /* OS-X specific */
  154. free(address);
  155. }
  156. static void FUZ_displayMallocStats(mallocCounter_t count)
  157. {
  158. DISPLAYLEVEL(3, "peak:%6u KB, nbMallocs:%2u, total:%6u KB \n",
  159. (unsigned)(count.peakMalloc >> 10),
  160. count.nbMalloc,
  161. (unsigned)(count.totalMalloc >> 10));
  162. }
  163. static int FUZ_mallocTests_internal(unsigned seed, double compressibility, unsigned part,
  164. void* inBuffer, size_t inSize, void* outBuffer, size_t outSize)
  165. {
  166. /* test only played in verbose mode, as they are long */
  167. if (g_displayLevel<3) return 0;
  168. /* Create compressible noise */
  169. if (!inBuffer || !outBuffer) {
  170. DISPLAY("Not enough memory, aborting\n");
  171. exit(1);
  172. }
  173. RDG_genBuffer(inBuffer, inSize, compressibility, 0. /*auto*/, seed);
  174. /* simple compression tests */
  175. if (part <= 1)
  176. { int compressionLevel;
  177. for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
  178. mallocCounter_t malcount = INIT_MALLOC_COUNTER;
  179. ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
  180. ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
  181. CHECK_Z( ZSTD_compressCCtx(cctx, outBuffer, outSize, inBuffer, inSize, compressionLevel) );
  182. ZSTD_freeCCtx(cctx);
  183. DISPLAYLEVEL(3, "compressCCtx level %i : ", compressionLevel);
  184. FUZ_displayMallocStats(malcount);
  185. } }
  186. /* streaming compression tests */
  187. if (part <= 2)
  188. { int compressionLevel;
  189. for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
  190. mallocCounter_t malcount = INIT_MALLOC_COUNTER;
  191. ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
  192. ZSTD_CCtx* const cstream = ZSTD_createCStream_advanced(cMem);
  193. ZSTD_outBuffer out = { outBuffer, outSize, 0 };
  194. ZSTD_inBuffer in = { inBuffer, inSize, 0 };
  195. CHECK_Z( ZSTD_initCStream(cstream, compressionLevel) );
  196. CHECK_Z( ZSTD_compressStream(cstream, &out, &in) );
  197. CHECK_Z( ZSTD_endStream(cstream, &out) );
  198. ZSTD_freeCStream(cstream);
  199. DISPLAYLEVEL(3, "compressStream level %i : ", compressionLevel);
  200. FUZ_displayMallocStats(malcount);
  201. } }
  202. /* advanced MT API test */
  203. if (part <= 3)
  204. { int nbThreads;
  205. for (nbThreads=1; nbThreads<=4; nbThreads++) {
  206. int compressionLevel;
  207. for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
  208. mallocCounter_t malcount = INIT_MALLOC_COUNTER;
  209. ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
  210. ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
  211. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
  212. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads) );
  213. CHECK_Z( ZSTD_compress2(cctx, outBuffer, outSize, inBuffer, inSize) );
  214. ZSTD_freeCCtx(cctx);
  215. DISPLAYLEVEL(3, "compress_generic,-T%i,end level %i : ",
  216. nbThreads, compressionLevel);
  217. FUZ_displayMallocStats(malcount);
  218. } } }
  219. /* advanced MT streaming API test */
  220. if (part <= 4)
  221. { int nbThreads;
  222. for (nbThreads=1; nbThreads<=4; nbThreads++) {
  223. int compressionLevel;
  224. for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
  225. mallocCounter_t malcount = INIT_MALLOC_COUNTER;
  226. ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
  227. ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
  228. ZSTD_outBuffer out = { outBuffer, outSize, 0 };
  229. ZSTD_inBuffer in = { inBuffer, inSize, 0 };
  230. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
  231. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads) );
  232. CHECK_Z( ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue) );
  233. while ( ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end) ) {}
  234. ZSTD_freeCCtx(cctx);
  235. DISPLAYLEVEL(3, "compress_generic,-T%i,continue level %i : ",
  236. nbThreads, compressionLevel);
  237. FUZ_displayMallocStats(malcount);
  238. } } }
  239. return 0;
  240. }
  241. static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
  242. {
  243. size_t const inSize = 64 MB + 16 MB + 4 MB + 1 MB + 256 KB + 64 KB; /* 85.3 MB */
  244. size_t const outSize = ZSTD_compressBound(inSize);
  245. void* const inBuffer = malloc(inSize);
  246. void* const outBuffer = malloc(outSize);
  247. int result;
  248. /* Create compressible noise */
  249. if (!inBuffer || !outBuffer) {
  250. DISPLAY("Not enough memory, aborting \n");
  251. exit(1);
  252. }
  253. result = FUZ_mallocTests_internal(seed, compressibility, part,
  254. inBuffer, inSize, outBuffer, outSize);
  255. free(inBuffer);
  256. free(outBuffer);
  257. return result;
  258. }
  259. #else
  260. static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
  261. {
  262. (void)seed; (void)compressibility; (void)part;
  263. return 0;
  264. }
  265. #endif
  266. static void FUZ_decodeSequences(BYTE* dst, ZSTD_Sequence* seqs, size_t seqsSize,
  267. BYTE* src, size_t size, ZSTD_sequenceFormat_e format)
  268. {
  269. size_t i;
  270. size_t j;
  271. for(i = 0; i < seqsSize; ++i) {
  272. assert(dst + seqs[i].litLength + seqs[i].matchLength <= dst + size);
  273. assert(src + seqs[i].litLength + seqs[i].matchLength <= src + size);
  274. if (format == ZSTD_sf_noBlockDelimiters) {
  275. assert(seqs[i].matchLength != 0 || seqs[i].offset != 0);
  276. }
  277. memcpy(dst, src, seqs[i].litLength);
  278. dst += seqs[i].litLength;
  279. src += seqs[i].litLength;
  280. size -= seqs[i].litLength;
  281. if (seqs[i].offset != 0) {
  282. for (j = 0; j < seqs[i].matchLength; ++j)
  283. dst[j] = dst[j - seqs[i].offset];
  284. dst += seqs[i].matchLength;
  285. src += seqs[i].matchLength;
  286. size -= seqs[i].matchLength;
  287. }
  288. }
  289. if (format == ZSTD_sf_noBlockDelimiters) {
  290. memcpy(dst, src, size);
  291. }
  292. }
  293. #ifdef ZSTD_MULTITHREAD
  294. typedef struct {
  295. ZSTD_CCtx* cctx;
  296. ZSTD_threadPool* pool;
  297. void* CNBuffer;
  298. size_t CNBuffSize;
  299. void* compressedBuffer;
  300. size_t compressedBufferSize;
  301. void* decodedBuffer;
  302. int err;
  303. } threadPoolTests_compressionJob_payload;
  304. static void* threadPoolTests_compressionJob(void* payload) {
  305. threadPoolTests_compressionJob_payload* args = (threadPoolTests_compressionJob_payload*)payload;
  306. size_t cSize;
  307. if (ZSTD_isError(ZSTD_CCtx_refThreadPool(args->cctx, args->pool))) args->err = 1;
  308. cSize = ZSTD_compress2(args->cctx, args->compressedBuffer, args->compressedBufferSize, args->CNBuffer, args->CNBuffSize);
  309. if (ZSTD_isError(cSize)) args->err = 1;
  310. if (ZSTD_isError(ZSTD_decompress(args->decodedBuffer, args->CNBuffSize, args->compressedBuffer, cSize))) args->err = 1;
  311. return payload;
  312. }
  313. static int threadPoolTests(void) {
  314. int testResult = 0;
  315. size_t err;
  316. size_t const CNBuffSize = 5 MB;
  317. void* const CNBuffer = malloc(CNBuffSize);
  318. size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
  319. void* const compressedBuffer = malloc(compressedBufferSize);
  320. void* const decodedBuffer = malloc(CNBuffSize);
  321. size_t const kPoolNumThreads = 8;
  322. RDG_genBuffer(CNBuffer, CNBuffSize, 0.5, 0.5, 0);
  323. DISPLAYLEVEL(3, "thread pool test : threadPool re-use roundtrips: ");
  324. {
  325. ZSTD_CCtx* cctx = ZSTD_createCCtx();
  326. ZSTD_threadPool* pool = ZSTD_createThreadPool(kPoolNumThreads);
  327. size_t nbThreads = 1;
  328. for (; nbThreads <= kPoolNumThreads; ++nbThreads) {
  329. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  330. ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, (int)nbThreads);
  331. err = ZSTD_CCtx_refThreadPool(cctx, pool);
  332. if (ZSTD_isError(err)) {
  333. DISPLAYLEVEL(3, "refThreadPool error!\n");
  334. ZSTD_freeCCtx(cctx);
  335. goto _output_error;
  336. }
  337. err = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  338. if (ZSTD_isError(err)) {
  339. DISPLAYLEVEL(3, "Compression error!\n");
  340. ZSTD_freeCCtx(cctx);
  341. goto _output_error;
  342. }
  343. err = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, err);
  344. if (ZSTD_isError(err)) {
  345. DISPLAYLEVEL(3, "Decompression error!\n");
  346. ZSTD_freeCCtx(cctx);
  347. goto _output_error;
  348. }
  349. }
  350. ZSTD_freeCCtx(cctx);
  351. ZSTD_freeThreadPool(pool);
  352. }
  353. DISPLAYLEVEL(3, "OK \n");
  354. DISPLAYLEVEL(3, "thread pool test : threadPool simultaneous usage: ");
  355. {
  356. void* const decodedBuffer2 = malloc(CNBuffSize);
  357. void* const compressedBuffer2 = malloc(compressedBufferSize);
  358. ZSTD_threadPool* pool = ZSTD_createThreadPool(kPoolNumThreads);
  359. ZSTD_CCtx* cctx1 = ZSTD_createCCtx();
  360. ZSTD_CCtx* cctx2 = ZSTD_createCCtx();
  361. ZSTD_pthread_t t1;
  362. ZSTD_pthread_t t2;
  363. threadPoolTests_compressionJob_payload p1 = {cctx1, pool, CNBuffer, CNBuffSize,
  364. compressedBuffer, compressedBufferSize, decodedBuffer, 0 /* err */};
  365. threadPoolTests_compressionJob_payload p2 = {cctx2, pool, CNBuffer, CNBuffSize,
  366. compressedBuffer2, compressedBufferSize, decodedBuffer2, 0 /* err */};
  367. ZSTD_CCtx_setParameter(cctx1, ZSTD_c_nbWorkers, 2);
  368. ZSTD_CCtx_setParameter(cctx2, ZSTD_c_nbWorkers, 2);
  369. ZSTD_CCtx_refThreadPool(cctx1, pool);
  370. ZSTD_CCtx_refThreadPool(cctx2, pool);
  371. ZSTD_pthread_create(&t1, NULL, threadPoolTests_compressionJob, &p1);
  372. ZSTD_pthread_create(&t2, NULL, threadPoolTests_compressionJob, &p2);
  373. ZSTD_pthread_join(t1, NULL);
  374. ZSTD_pthread_join(t2, NULL);
  375. assert(!memcmp(decodedBuffer, decodedBuffer2, CNBuffSize));
  376. free(decodedBuffer2);
  377. free(compressedBuffer2);
  378. ZSTD_freeThreadPool(pool);
  379. ZSTD_freeCCtx(cctx1);
  380. ZSTD_freeCCtx(cctx2);
  381. if (p1.err || p2.err) goto _output_error;
  382. }
  383. DISPLAYLEVEL(3, "OK \n");
  384. _end:
  385. free(CNBuffer);
  386. free(compressedBuffer);
  387. free(decodedBuffer);
  388. return testResult;
  389. _output_error:
  390. testResult = 1;
  391. DISPLAY("Error detected in Unit tests ! \n");
  392. goto _end;
  393. }
  394. #endif /* ZSTD_MULTITHREAD */
  395. /*=============================================
  396. * Unit tests
  397. =============================================*/
  398. static int basicUnitTests(U32 const seed, double compressibility)
  399. {
  400. size_t const CNBuffSize = 5 MB;
  401. void* const CNBuffer = malloc(CNBuffSize);
  402. size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
  403. void* const compressedBuffer = malloc(compressedBufferSize);
  404. void* const decodedBuffer = malloc(CNBuffSize);
  405. int testResult = 0;
  406. unsigned testNb=0;
  407. size_t cSize;
  408. /* Create compressible noise */
  409. if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
  410. DISPLAY("Not enough memory, aborting\n");
  411. testResult = 1;
  412. goto _end;
  413. }
  414. RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
  415. /* Basic tests */
  416. DISPLAYLEVEL(3, "test%3u : ZSTD_getErrorName : ", testNb++);
  417. { const char* errorString = ZSTD_getErrorName(0);
  418. DISPLAYLEVEL(3, "OK : %s \n", errorString);
  419. }
  420. DISPLAYLEVEL(3, "test%3u : ZSTD_getErrorName with wrong value : ", testNb++);
  421. { const char* errorString = ZSTD_getErrorName(499);
  422. DISPLAYLEVEL(3, "OK : %s \n", errorString);
  423. }
  424. DISPLAYLEVEL(3, "test%3u : min compression level : ", testNb++);
  425. { int const mcl = ZSTD_minCLevel();
  426. DISPLAYLEVEL(3, "%i (OK) \n", mcl);
  427. }
  428. DISPLAYLEVEL(3, "test%3u : default compression level : ", testNb++);
  429. { int const defaultCLevel = ZSTD_defaultCLevel();
  430. if (defaultCLevel != ZSTD_CLEVEL_DEFAULT) goto _output_error;
  431. DISPLAYLEVEL(3, "%i (OK) \n", defaultCLevel);
  432. }
  433. DISPLAYLEVEL(3, "test%3u : ZSTD_versionNumber : ", testNb++);
  434. { unsigned const vn = ZSTD_versionNumber();
  435. DISPLAYLEVEL(3, "%u (OK) \n", vn);
  436. }
  437. DISPLAYLEVEL(3, "test%3u : ZSTD_adjustCParams : ", testNb++);
  438. {
  439. ZSTD_compressionParameters params;
  440. memset(&params, 0, sizeof(params));
  441. params.windowLog = 10;
  442. params.hashLog = 19;
  443. params.chainLog = 19;
  444. params = ZSTD_adjustCParams(params, 1000, 100000);
  445. if (params.hashLog != 18) goto _output_error;
  446. if (params.chainLog != 17) goto _output_error;
  447. }
  448. DISPLAYLEVEL(3, "OK \n");
  449. DISPLAYLEVEL(3, "test%3u : compress %u bytes : ", testNb++, (unsigned)CNBuffSize);
  450. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  451. if (cctx==NULL) goto _output_error;
  452. CHECK_VAR(cSize, ZSTD_compressCCtx(cctx,
  453. compressedBuffer, compressedBufferSize,
  454. CNBuffer, CNBuffSize, 1) );
  455. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  456. DISPLAYLEVEL(3, "test%3i : size of cctx for level 1 : ", testNb++);
  457. { size_t const cctxSize = ZSTD_sizeof_CCtx(cctx);
  458. DISPLAYLEVEL(3, "%u bytes \n", (unsigned)cctxSize);
  459. }
  460. ZSTD_freeCCtx(cctx);
  461. }
  462. DISPLAYLEVEL(3, "test%3i : decompress skippable frame -8 size : ", testNb++);
  463. {
  464. char const skippable8[] = "\x50\x2a\x4d\x18\xf8\xff\xff\xff";
  465. size_t const size = ZSTD_decompress(NULL, 0, skippable8, 8);
  466. if (!ZSTD_isError(size)) goto _output_error;
  467. }
  468. DISPLAYLEVEL(3, "OK \n");
  469. DISPLAYLEVEL(3, "test%3i : ZSTD_getFrameContentSize test : ", testNb++);
  470. { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
  471. if (rSize != CNBuffSize) goto _output_error;
  472. }
  473. DISPLAYLEVEL(3, "OK \n");
  474. DISPLAYLEVEL(3, "test%3i : ZSTD_getDecompressedSize test : ", testNb++);
  475. { unsigned long long const rSize = ZSTD_getDecompressedSize(compressedBuffer, cSize);
  476. if (rSize != CNBuffSize) goto _output_error;
  477. }
  478. DISPLAYLEVEL(3, "OK \n");
  479. DISPLAYLEVEL(3, "test%3i : ZSTD_findDecompressedSize test : ", testNb++);
  480. { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
  481. if (rSize != CNBuffSize) goto _output_error;
  482. }
  483. DISPLAYLEVEL(3, "OK \n");
  484. DISPLAYLEVEL(3, "test%3i : tight ZSTD_decompressBound test : ", testNb++);
  485. {
  486. unsigned long long bound = ZSTD_decompressBound(compressedBuffer, cSize);
  487. if (bound != CNBuffSize) goto _output_error;
  488. }
  489. DISPLAYLEVEL(3, "OK \n");
  490. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressBound test with invalid srcSize : ", testNb++);
  491. {
  492. unsigned long long bound = ZSTD_decompressBound(compressedBuffer, cSize - 1);
  493. if (bound != ZSTD_CONTENTSIZE_ERROR) goto _output_error;
  494. }
  495. DISPLAYLEVEL(3, "OK \n");
  496. DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
  497. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  498. if (r != CNBuffSize) goto _output_error; }
  499. DISPLAYLEVEL(3, "OK \n");
  500. DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
  501. { size_t u;
  502. for (u=0; u<CNBuffSize; u++) {
  503. if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;
  504. } }
  505. DISPLAYLEVEL(3, "OK \n");
  506. DISPLAYLEVEL(3, "test%3u : invalid endDirective : ", testNb++);
  507. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  508. ZSTD_inBuffer inb = { CNBuffer, CNBuffSize, 0 };
  509. ZSTD_outBuffer outb = { compressedBuffer, compressedBufferSize, 0 };
  510. if (cctx==NULL) goto _output_error;
  511. CHECK( ZSTD_isError( ZSTD_compressStream2(cctx, &outb, &inb, (ZSTD_EndDirective) 3) ) ); /* must fail */
  512. CHECK( ZSTD_isError( ZSTD_compressStream2(cctx, &outb, &inb, (ZSTD_EndDirective)-1) ) ); /* must fail */
  513. ZSTD_freeCCtx(cctx);
  514. }
  515. DISPLAYLEVEL(3, "OK \n");
  516. DISPLAYLEVEL(3, "test%3i : ZSTD_checkCParams : ", testNb++);
  517. {
  518. ZSTD_parameters params = ZSTD_getParams(3, 0, 0);
  519. assert(!ZSTD_checkCParams(params.cParams));
  520. }
  521. DISPLAYLEVEL(3, "OK \n");
  522. DISPLAYLEVEL(3, "test%3i : ZSTD_createDCtx_advanced and ZSTD_sizeof_DCtx: ", testNb++);
  523. {
  524. ZSTD_DCtx* const dctx = ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
  525. assert(dctx != NULL);
  526. assert(ZSTD_sizeof_DCtx(dctx) != 0);
  527. ZSTD_freeDCtx(dctx);
  528. }
  529. DISPLAYLEVEL(3, "OK \n");
  530. DISPLAYLEVEL(3, "test%3i : misc unaccounted for zstd symbols : ", testNb++);
  531. {
  532. /* %p takes a void*. In ISO C, it's illegal to cast a function pointer
  533. * to a data pointer. (Although in POSIX you're required to be allowed
  534. * to do it...) So we have to fall back to our trusty friend memcpy. */
  535. unsigned (* const funcptr_getDictID)(const ZSTD_DDict* ddict) =
  536. ZSTD_getDictID_fromDDict;
  537. ZSTD_DStream* (* const funcptr_createDStream)(
  538. ZSTD_customMem customMem) = ZSTD_createDStream_advanced;
  539. void (* const funcptr_copyDCtx)(
  540. ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx) = ZSTD_copyDCtx;
  541. ZSTD_nextInputType_e (* const funcptr_nextInputType)(ZSTD_DCtx* dctx) =
  542. ZSTD_nextInputType;
  543. const void *voidptr_getDictID;
  544. const void *voidptr_createDStream;
  545. const void *voidptr_copyDCtx;
  546. const void *voidptr_nextInputType;
  547. DEBUG_STATIC_ASSERT(sizeof(funcptr_getDictID) == sizeof(voidptr_getDictID));
  548. memcpy(
  549. (void*)&voidptr_getDictID,
  550. (const void*)&funcptr_getDictID,
  551. sizeof(void*));
  552. memcpy(
  553. (void*)&voidptr_createDStream,
  554. (const void*)&funcptr_createDStream,
  555. sizeof(void*));
  556. memcpy(
  557. (void*)&voidptr_copyDCtx,
  558. (const void*)&funcptr_copyDCtx,
  559. sizeof(void*));
  560. memcpy(
  561. (void*)&voidptr_nextInputType,
  562. (const void*)&funcptr_nextInputType,
  563. sizeof(void*));
  564. DISPLAYLEVEL(3, "%p ", voidptr_getDictID);
  565. DISPLAYLEVEL(3, "%p ", voidptr_createDStream);
  566. DISPLAYLEVEL(3, "%p ", voidptr_copyDCtx);
  567. DISPLAYLEVEL(3, "%p ", voidptr_nextInputType);
  568. }
  569. DISPLAYLEVEL(3, ": OK \n");
  570. DISPLAYLEVEL(3, "test%3i : decompress with null dict : ", testNb++);
  571. { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
  572. { size_t const r = ZSTD_decompress_usingDict(dctx,
  573. decodedBuffer, CNBuffSize,
  574. compressedBuffer, cSize,
  575. NULL, 0);
  576. if (r != CNBuffSize) goto _output_error;
  577. }
  578. ZSTD_freeDCtx(dctx);
  579. }
  580. DISPLAYLEVEL(3, "OK \n");
  581. DISPLAYLEVEL(3, "test%3i : decompress with null DDict : ", testNb++);
  582. { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
  583. { size_t const r = ZSTD_decompress_usingDDict(dctx,
  584. decodedBuffer, CNBuffSize,
  585. compressedBuffer, cSize,
  586. NULL);
  587. if (r != CNBuffSize) goto _output_error;
  588. }
  589. ZSTD_freeDCtx(dctx);
  590. }
  591. DISPLAYLEVEL(3, "OK \n");
  592. DISPLAYLEVEL(3, "test%3i : decompress with 1 missing byte : ", testNb++);
  593. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
  594. if (!ZSTD_isError(r)) goto _output_error;
  595. if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
  596. DISPLAYLEVEL(3, "OK \n");
  597. DISPLAYLEVEL(3, "test%3i : decompress with 1 too much byte : ", testNb++);
  598. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
  599. if (!ZSTD_isError(r)) goto _output_error;
  600. if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
  601. DISPLAYLEVEL(3, "OK \n");
  602. DISPLAYLEVEL(3, "test%3i : decompress too large input : ", testNb++);
  603. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, compressedBufferSize);
  604. if (!ZSTD_isError(r)) goto _output_error;
  605. if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
  606. DISPLAYLEVEL(3, "OK \n");
  607. DISPLAYLEVEL(3, "test%3i : decompress into NULL buffer : ", testNb++);
  608. { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, compressedBufferSize);
  609. if (!ZSTD_isError(r)) goto _output_error;
  610. if (ZSTD_getErrorCode(r) != ZSTD_error_dstSize_tooSmall) goto _output_error; }
  611. DISPLAYLEVEL(3, "OK \n");
  612. DISPLAYLEVEL(3, "test%3i : decompress with corrupted checksum : ", testNb++);
  613. { /* create compressed buffer with checksumming enabled */
  614. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  615. if (!cctx) {
  616. DISPLAY("Not enough memory, aborting\n");
  617. testResult = 1;
  618. goto _end;
  619. }
  620. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
  621. CHECK_VAR(cSize, ZSTD_compress2(cctx,
  622. compressedBuffer, compressedBufferSize,
  623. CNBuffer, CNBuffSize) );
  624. ZSTD_freeCCtx(cctx);
  625. }
  626. { /* copy the compressed buffer and corrupt the checksum */
  627. size_t r;
  628. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  629. if (!dctx) {
  630. DISPLAY("Not enough memory, aborting\n");
  631. testResult = 1;
  632. goto _end;
  633. }
  634. ((char*)compressedBuffer)[cSize-1] += 1;
  635. r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  636. if (!ZSTD_isError(r)) goto _output_error;
  637. if (ZSTD_getErrorCode(r) != ZSTD_error_checksum_wrong) goto _output_error;
  638. CHECK_Z(ZSTD_DCtx_setParameter(dctx, ZSTD_d_forceIgnoreChecksum, ZSTD_d_ignoreChecksum));
  639. r = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
  640. if (!ZSTD_isError(r)) goto _output_error; /* wrong checksum size should still throw error */
  641. r = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  642. if (ZSTD_isError(r)) goto _output_error;
  643. ZSTD_freeDCtx(dctx);
  644. }
  645. DISPLAYLEVEL(3, "OK \n");
  646. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressBound test with content size missing : ", testNb++);
  647. { /* create compressed buffer with content size missing */
  648. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  649. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0) );
  650. CHECK_VAR(cSize, ZSTD_compress2(cctx,
  651. compressedBuffer, compressedBufferSize,
  652. CNBuffer, CNBuffSize) );
  653. ZSTD_freeCCtx(cctx);
  654. }
  655. { /* ensure frame content size is missing */
  656. ZSTD_frameHeader zfh;
  657. size_t const ret = ZSTD_getFrameHeader(&zfh, compressedBuffer, compressedBufferSize);
  658. if (ret != 0 || zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
  659. }
  660. { /* ensure CNBuffSize <= decompressBound */
  661. unsigned long long const bound = ZSTD_decompressBound(compressedBuffer, compressedBufferSize);
  662. if (CNBuffSize > bound) goto _output_error;
  663. }
  664. DISPLAYLEVEL(3, "OK \n");
  665. DISPLAYLEVEL(3, "test%3d: check DCtx size is reduced after many oversized calls : ", testNb++);
  666. {
  667. size_t const largeFrameSrcSize = 200;
  668. size_t const smallFrameSrcSize = 10;
  669. size_t const nbFrames = 256;
  670. size_t i = 0, consumed = 0, produced = 0, prevDCtxSize = 0;
  671. int sizeReduced = 0;
  672. BYTE* const dst = (BYTE*)compressedBuffer;
  673. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  674. /* create a large frame and then a bunch of small frames */
  675. size_t srcSize = ZSTD_compress((void*)dst,
  676. compressedBufferSize, CNBuffer, largeFrameSrcSize, 3);
  677. for (i = 0; i < nbFrames; i++)
  678. srcSize += ZSTD_compress((void*)(dst + srcSize),
  679. compressedBufferSize - srcSize, CNBuffer,
  680. smallFrameSrcSize, 3);
  681. /* decompressStream and make sure that dctx size was reduced at least once */
  682. while (consumed < srcSize) {
  683. ZSTD_inBuffer in = {(void*)(dst + consumed), MIN(1, srcSize - consumed), 0};
  684. ZSTD_outBuffer out = {(BYTE*)CNBuffer + produced, CNBuffSize - produced, 0};
  685. ZSTD_decompressStream(dctx, &out, &in);
  686. consumed += in.pos;
  687. produced += out.pos;
  688. /* success! size was reduced from the previous frame */
  689. if (prevDCtxSize > ZSTD_sizeof_DCtx(dctx))
  690. sizeReduced = 1;
  691. prevDCtxSize = ZSTD_sizeof_DCtx(dctx);
  692. }
  693. assert(sizeReduced);
  694. ZSTD_freeDCtx(dctx);
  695. }
  696. DISPLAYLEVEL(3, "OK \n");
  697. {
  698. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  699. ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 100, 1);
  700. ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
  701. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
  702. DISPLAYLEVEL(3, "test%3i : ZSTD_compressCCtx() doesn't use advanced parameters", testNb++);
  703. CHECK_Z(ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 1));
  704. if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
  705. DISPLAYLEVEL(3, "OK \n");
  706. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingDict() doesn't use advanced parameters: ", testNb++);
  707. CHECK_Z(ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize, NULL, 0, NULL, 0, 1));
  708. if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
  709. DISPLAYLEVEL(3, "OK \n");
  710. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict() doesn't use advanced parameters: ", testNb++);
  711. CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, NULL, 0, cdict));
  712. if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
  713. DISPLAYLEVEL(3, "OK \n");
  714. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_advanced() doesn't use advanced parameters: ", testNb++);
  715. CHECK_Z(ZSTD_compress_advanced(cctx, compressedBuffer, compressedBufferSize, NULL, 0, NULL, 0, params));
  716. if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
  717. DISPLAYLEVEL(3, "OK \n");
  718. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict_advanced() doesn't use advanced parameters: ", testNb++);
  719. CHECK_Z(ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, compressedBufferSize, NULL, 0, cdict, params.fParams));
  720. if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
  721. DISPLAYLEVEL(3, "OK \n");
  722. ZSTD_freeCDict(cdict);
  723. ZSTD_freeCCtx(cctx);
  724. }
  725. DISPLAYLEVEL(3, "test%3i : ldm fill dict out-of-bounds check", testNb++);
  726. {
  727. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  728. size_t const size = (1U << 10);
  729. size_t const dstCapacity = ZSTD_compressBound(size);
  730. void* dict = (void*)malloc(size);
  731. void* src = (void*)malloc(size);
  732. void* dst = (void*)malloc(dstCapacity);
  733. RDG_genBuffer(dict, size, 0.5, 0.5, seed);
  734. RDG_genBuffer(src, size, 0.5, 0.5, seed);
  735. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  736. assert(!ZSTD_isError(ZSTD_compress_usingDict(cctx, dst, dstCapacity, src, size, dict, size, 3)));
  737. ZSTD_freeCCtx(cctx);
  738. free(dict);
  739. free(src);
  740. free(dst);
  741. }
  742. DISPLAYLEVEL(3, "OK \n");
  743. DISPLAYLEVEL(3, "test%3i : testing dict compression with enableLdm and forceMaxWindow : ", testNb++);
  744. {
  745. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  746. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  747. void* dict = (void*)malloc(CNBuffSize);
  748. int nbWorkers;
  749. for (nbWorkers = 0; nbWorkers < 3; ++nbWorkers) {
  750. RDG_genBuffer(dict, CNBuffSize, 0.5, 0.5, seed);
  751. RDG_genBuffer(CNBuffer, CNBuffSize, 0.6, 0.6, seed);
  752. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbWorkers));
  753. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  754. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceMaxWindow, 1));
  755. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  756. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, CNBuffSize));
  757. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  758. CHECK_Z(cSize);
  759. CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, CNBuffSize));
  760. }
  761. ZSTD_freeCCtx(cctx);
  762. ZSTD_freeDCtx(dctx);
  763. free(dict);
  764. }
  765. DISPLAYLEVEL(3, "OK \n");
  766. DISPLAYLEVEL(3, "test%3i : testing dict compression for determinism : ", testNb++);
  767. {
  768. size_t const testSize = 1024;
  769. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  770. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  771. char* dict = (char*)malloc(2 * testSize);
  772. int ldmEnabled, level;
  773. RDG_genBuffer(dict, testSize, 0.5, 0.5, seed);
  774. RDG_genBuffer(CNBuffer, testSize, 0.6, 0.6, seed);
  775. memcpy(dict + testSize, CNBuffer, testSize);
  776. for (level = 1; level <= 5; ++level) {
  777. for (ldmEnabled = 0; ldmEnabled <= 1; ++ldmEnabled) {
  778. size_t cSize0;
  779. XXH64_hash_t compressedChecksum0;
  780. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  781. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level));
  782. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ldmEnabled));
  783. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_deterministicRefPrefix, 1));
  784. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, testSize));
  785. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, testSize);
  786. CHECK_Z(cSize);
  787. CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, testSize, compressedBuffer, cSize, dict, testSize));
  788. cSize0 = cSize;
  789. compressedChecksum0 = XXH64(compressedBuffer, cSize, 0);
  790. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, testSize));
  791. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, dict + testSize, testSize);
  792. CHECK_Z(cSize);
  793. if (cSize != cSize0) goto _output_error;
  794. if (XXH64(compressedBuffer, cSize, 0) != compressedChecksum0) goto _output_error;
  795. }
  796. }
  797. ZSTD_freeCCtx(cctx);
  798. ZSTD_freeDCtx(dctx);
  799. free(dict);
  800. }
  801. DISPLAYLEVEL(3, "OK \n");
  802. DISPLAYLEVEL(3, "test%3i : LDM + opt parser with small uncompressible block ", testNb++);
  803. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  804. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  805. size_t const srcSize = 300 KB;
  806. size_t const flushSize = 128 KB + 5;
  807. size_t const dstSize = ZSTD_compressBound(srcSize);
  808. char* src = (char*)CNBuffer;
  809. char* dst = (char*)compressedBuffer;
  810. ZSTD_outBuffer out = { dst, dstSize, 0 };
  811. ZSTD_inBuffer in = { src, flushSize, 0 };
  812. if (!cctx || !dctx) {
  813. DISPLAY("Not enough memory, aborting\n");
  814. testResult = 1;
  815. goto _end;
  816. }
  817. RDG_genBuffer(src, srcSize, 0.5, 0.5, seed);
  818. /* Force an LDM to exist that crosses block boundary into uncompressible block */
  819. memcpy(src + 125 KB, src, 3 KB + 5);
  820. /* Enable MT, LDM, and opt parser */
  821. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1));
  822. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  823. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  824. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
  825. /* Flushes a block of 128 KB and block of 5 bytes */
  826. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  827. /* Compress the rest */
  828. in.size = 300 KB;
  829. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  830. CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, dst, out.pos));
  831. ZSTD_freeCCtx(cctx);
  832. ZSTD_freeDCtx(dctx);
  833. }
  834. DISPLAYLEVEL(3, "OK \n");
  835. DISPLAYLEVEL(3, "test%3i : testing ldm dictionary gets invalidated : ", testNb++);
  836. {
  837. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  838. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  839. void* dict = (void*)malloc(CNBuffSize);
  840. size_t const kWindowLog = 10;
  841. size_t const kWindowSize = (size_t)1 << kWindowLog;
  842. size_t const dictSize = kWindowSize * 10;
  843. size_t const srcSize1 = kWindowSize / 2;
  844. size_t const srcSize2 = kWindowSize * 10;
  845. if (CNBuffSize < dictSize) goto _output_error;
  846. RDG_genBuffer(dict, dictSize, 0.5, 0.5, seed);
  847. RDG_genBuffer(CNBuffer, srcSize1 + srcSize2, 0.5, 0.5, seed);
  848. /* Enable checksum to verify round trip. */
  849. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  850. /* Disable content size to skip single-pass decompression. */
  851. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0));
  852. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, (int)kWindowLog));
  853. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  854. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmMinMatch, 32));
  855. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashRateLog, 1));
  856. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashLog, 16));
  857. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmBucketSizeLog, 3));
  858. /* Round trip once with a dictionary. */
  859. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
  860. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize1);
  861. CHECK_Z(cSize);
  862. CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
  863. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize2);
  864. /* Streaming decompression to catch out of bounds offsets. */
  865. {
  866. ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
  867. ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
  868. size_t const dSize = ZSTD_decompressStream(dctx, &out, &in);
  869. CHECK_Z(dSize);
  870. if (dSize != 0) goto _output_error;
  871. }
  872. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
  873. /* Round trip once with a dictionary. */
  874. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
  875. {
  876. ZSTD_inBuffer in = {CNBuffer, srcSize1, 0};
  877. ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0};
  878. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  879. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  880. cSize = out.pos;
  881. }
  882. CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
  883. {
  884. ZSTD_inBuffer in = {CNBuffer, srcSize2, 0};
  885. ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0};
  886. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  887. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  888. cSize = out.pos;
  889. }
  890. /* Streaming decompression to catch out of bounds offsets. */
  891. {
  892. ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
  893. ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
  894. size_t const dSize = ZSTD_decompressStream(dctx, &out, &in);
  895. CHECK_Z(dSize);
  896. if (dSize != 0) goto _output_error;
  897. }
  898. ZSTD_freeCCtx(cctx);
  899. ZSTD_freeDCtx(dctx);
  900. free(dict);
  901. }
  902. DISPLAYLEVEL(3, "OK \n");
  903. /* Note: this test takes 0.5 seconds to run */
  904. DISPLAYLEVEL(3, "test%3i : testing refPrefx vs refPrefx + ldm (size comparison) : ", testNb++);
  905. {
  906. /* test a big buffer so that ldm can take effect */
  907. size_t const size = 100 MB;
  908. int const windowLog = 27;
  909. size_t const dstSize = ZSTD_compressBound(size);
  910. void* dict = (void*)malloc(size);
  911. void* src = (void*)malloc(size);
  912. void* dst = (void*)malloc(dstSize);
  913. void* recon = (void*)malloc(size);
  914. size_t refPrefixCompressedSize = 0;
  915. size_t refPrefixLdmComrpessedSize = 0;
  916. size_t reconSize = 0;
  917. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  918. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  919. /* make dict and src the same uncompressible data */
  920. RDG_genBuffer(src, size, 0, 0, seed);
  921. memcpy(dict, src, size);
  922. assert(!memcmp(dict, src, size));
  923. /* set level 1 and windowLog to cover src */
  924. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1));
  925. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, windowLog));
  926. /* compress on level 1 using just refPrefix and no ldm */
  927. ZSTD_CCtx_refPrefix(cctx, dict, size);
  928. refPrefixCompressedSize = ZSTD_compress2(cctx, dst, dstSize, src, size);
  929. assert(!ZSTD_isError(refPrefixCompressedSize));
  930. /* test round trip just refPrefix */
  931. ZSTD_DCtx_refPrefix(dctx, dict, size);
  932. reconSize = ZSTD_decompressDCtx(dctx, recon, size, dst, refPrefixCompressedSize);
  933. assert(!ZSTD_isError(reconSize));
  934. assert(reconSize == size);
  935. assert(!memcmp(recon, src, size));
  936. /* compress on level 1 using refPrefix and ldm */
  937. ZSTD_CCtx_refPrefix(cctx, dict, size);;
  938. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1))
  939. refPrefixLdmComrpessedSize = ZSTD_compress2(cctx, dst, dstSize, src, size);
  940. assert(!ZSTD_isError(refPrefixLdmComrpessedSize));
  941. /* test round trip refPrefix + ldm*/
  942. ZSTD_DCtx_refPrefix(dctx, dict, size);
  943. reconSize = ZSTD_decompressDCtx(dctx, recon, size, dst, refPrefixLdmComrpessedSize);
  944. assert(!ZSTD_isError(reconSize));
  945. assert(reconSize == size);
  946. assert(!memcmp(recon, src, size));
  947. /* make sure that refPrefixCompressedSize is significantly greater */
  948. assert(refPrefixCompressedSize > 10 * refPrefixLdmComrpessedSize);
  949. /* make sure the ldm comrpessed size is less than 1% of original */
  950. assert((double)refPrefixLdmComrpessedSize / (double)size < 0.01);
  951. ZSTD_freeDCtx(dctx);
  952. ZSTD_freeCCtx(cctx);
  953. free(recon);
  954. free(dict);
  955. free(src);
  956. free(dst);
  957. }
  958. DISPLAYLEVEL(3, "OK \n");
  959. DISPLAYLEVEL(3, "test%3d: superblock uncompressible data, too many nocompress superblocks : ", testNb++);
  960. {
  961. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  962. const BYTE* src = (BYTE*)CNBuffer; BYTE* dst = (BYTE*)compressedBuffer;
  963. size_t srcSize = 321656; size_t dstCapacity = ZSTD_compressBound(srcSize);
  964. /* This is the number of bytes to stream before ending. This value
  965. * was obtained by trial and error :/. */
  966. const size_t streamCompressThreshold = 161792;
  967. const size_t streamCompressDelta = 1024;
  968. /* The first 1/5 of the buffer is compressible and the last 4/5 is
  969. * uncompressible. This is an approximation of the type of data
  970. * the fuzzer generated to catch this bug. Streams like this were making
  971. * zstd generate noCompress superblocks (which are larger than the src
  972. * they come from). Do this enough times, and we'll run out of room
  973. * and throw a dstSize_tooSmall error. */
  974. const size_t compressiblePartSize = srcSize/5;
  975. const size_t uncompressiblePartSize = srcSize-compressiblePartSize;
  976. RDG_genBuffer(CNBuffer, compressiblePartSize, 0.5, 0.5, seed);
  977. RDG_genBuffer((BYTE*)CNBuffer+compressiblePartSize, uncompressiblePartSize, 0, 0, seed);
  978. /* Setting target block size so that superblock is used */
  979. assert(cctx != NULL);
  980. ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 81);
  981. { size_t read;
  982. for (read = 0; read < streamCompressThreshold; read += streamCompressDelta) {
  983. ZSTD_inBuffer in = {src, streamCompressDelta, 0};
  984. ZSTD_outBuffer out = {dst, dstCapacity, 0};
  985. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue));
  986. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  987. src += streamCompressDelta; srcSize -= streamCompressDelta;
  988. dst += out.pos; dstCapacity -= out.pos;
  989. } }
  990. /* This is trying to catch a dstSize_tooSmall error */
  991. { ZSTD_inBuffer in = {src, srcSize, 0};
  992. ZSTD_outBuffer out = {dst, dstCapacity, 0};
  993. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  994. }
  995. ZSTD_freeCCtx(cctx);
  996. }
  997. DISPLAYLEVEL(3, "OK \n");
  998. DISPLAYLEVEL(3, "test%3d: superblock with no literals : ", testNb++);
  999. /* Generate the same data 20 times over */
  1000. { size_t const avgChunkSize = CNBuffSize / 20;
  1001. size_t b;
  1002. for (b = 0; b < CNBuffSize; b += avgChunkSize) {
  1003. size_t const chunkSize = MIN(CNBuffSize - b, avgChunkSize);
  1004. RDG_genBuffer((char*)CNBuffer + b, chunkSize, compressibility, 0. /* auto */, seed);
  1005. } }
  1006. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1007. size_t const normalCSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  1008. size_t const allowedExpansion = (CNBuffSize * 3 / 1000);
  1009. size_t superCSize;
  1010. CHECK_Z(normalCSize);
  1011. ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19);
  1012. ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 1000);
  1013. superCSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  1014. CHECK_Z(superCSize);
  1015. if (superCSize > normalCSize + allowedExpansion) {
  1016. DISPLAYLEVEL(1, "Superblock too big: %u > %u + %u \n", (U32)superCSize, (U32)normalCSize, (U32)allowedExpansion);
  1017. goto _output_error;
  1018. }
  1019. ZSTD_freeCCtx(cctx);
  1020. }
  1021. DISPLAYLEVEL(3, "OK \n");
  1022. RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0. /*auto*/, seed);
  1023. DISPLAYLEVEL(3, "test%3d: superblock enough room for checksum : ", testNb++)
  1024. /* This tests whether or not we leave enough room for the checksum at the end
  1025. * of the dst buffer. The bug that motivated this test was found by the
  1026. * stream_round_trip fuzzer but this crashes for the same reason and is
  1027. * far more compact than re-creating the stream_round_trip fuzzer's code path */
  1028. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1029. ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 64);
  1030. assert(!ZSTD_isError(ZSTD_compress2(cctx, compressedBuffer, 1339, CNBuffer, 1278)));
  1031. ZSTD_freeCCtx(cctx);
  1032. }
  1033. DISPLAYLEVEL(3, "OK \n");
  1034. DISPLAYLEVEL(3, "test%3i : compress a NULL input with each level : ", testNb++);
  1035. { int level = -1;
  1036. ZSTD_CCtx* cctx = ZSTD_createCCtx();
  1037. if (!cctx) goto _output_error;
  1038. for (level = -1; level <= ZSTD_maxCLevel(); ++level) {
  1039. CHECK_Z( ZSTD_compress(compressedBuffer, compressedBufferSize, NULL, 0, level) );
  1040. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level) );
  1041. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, NULL, 0) );
  1042. }
  1043. ZSTD_freeCCtx(cctx);
  1044. }
  1045. DISPLAYLEVEL(3, "OK \n");
  1046. DISPLAYLEVEL(3, "test%3d : check CCtx size after compressing empty input : ", testNb++);
  1047. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1048. size_t const r = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 19);
  1049. if (ZSTD_isError(r)) goto _output_error;
  1050. if (ZSTD_sizeof_CCtx(cctx) > (1U << 20)) goto _output_error;
  1051. ZSTD_freeCCtx(cctx);
  1052. cSize = r;
  1053. }
  1054. DISPLAYLEVEL(3, "OK \n");
  1055. DISPLAYLEVEL(3, "test%3d : decompress empty frame into NULL : ", testNb++);
  1056. { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, cSize);
  1057. if (ZSTD_isError(r)) goto _output_error;
  1058. if (r != 0) goto _output_error;
  1059. }
  1060. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1061. ZSTD_outBuffer output;
  1062. if (cctx==NULL) goto _output_error;
  1063. output.dst = compressedBuffer;
  1064. output.size = compressedBufferSize;
  1065. output.pos = 0;
  1066. CHECK_Z( ZSTD_initCStream(cctx, 1) ); /* content size unknown */
  1067. CHECK_Z( ZSTD_flushStream(cctx, &output) ); /* ensure no possibility to "concatenate" and determine the content size */
  1068. CHECK_Z( ZSTD_endStream(cctx, &output) );
  1069. ZSTD_freeCCtx(cctx);
  1070. /* single scan decompression */
  1071. { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, output.pos);
  1072. if (ZSTD_isError(r)) goto _output_error;
  1073. if (r != 0) goto _output_error;
  1074. }
  1075. /* streaming decompression */
  1076. { ZSTD_DCtx* const dstream = ZSTD_createDStream();
  1077. ZSTD_inBuffer dinput;
  1078. ZSTD_outBuffer doutput;
  1079. size_t ipos;
  1080. if (dstream==NULL) goto _output_error;
  1081. dinput.src = compressedBuffer;
  1082. dinput.size = 0;
  1083. dinput.pos = 0;
  1084. doutput.dst = NULL;
  1085. doutput.size = 0;
  1086. doutput.pos = 0;
  1087. CHECK_Z ( ZSTD_initDStream(dstream) );
  1088. for (ipos=1; ipos<=output.pos; ipos++) {
  1089. dinput.size = ipos;
  1090. CHECK_Z ( ZSTD_decompressStream(dstream, &doutput, &dinput) );
  1091. }
  1092. if (doutput.pos != 0) goto _output_error;
  1093. ZSTD_freeDStream(dstream);
  1094. }
  1095. }
  1096. DISPLAYLEVEL(3, "OK \n");
  1097. DISPLAYLEVEL(3, "test%3d : re-use CCtx with expanding block size : ", testNb++);
  1098. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1099. ZSTD_parameters const params = ZSTD_getParams(1, ZSTD_CONTENTSIZE_UNKNOWN, 0);
  1100. assert(params.fParams.contentSizeFlag == 1); /* block size will be adapted if pledgedSrcSize is enabled */
  1101. CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, 1 /*pledgedSrcSize*/) );
  1102. CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 1) ); /* creates a block size of 1 */
  1103. CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN) ); /* re-use same parameters */
  1104. { size_t const inSize = 2* 128 KB;
  1105. size_t const outSize = ZSTD_compressBound(inSize);
  1106. CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, outSize, CNBuffer, inSize) );
  1107. /* will fail if blockSize is not resized */
  1108. }
  1109. ZSTD_freeCCtx(cctx);
  1110. }
  1111. DISPLAYLEVEL(3, "OK \n");
  1112. DISPLAYLEVEL(3, "test%3d : re-using a CCtx should compress the same : ", testNb++);
  1113. { size_t const sampleSize = 30;
  1114. int i;
  1115. for (i=0; i<20; i++)
  1116. ((char*)CNBuffer)[i] = (char)i; /* ensure no match during initial section */
  1117. memcpy((char*)CNBuffer + 20, CNBuffer, 10); /* create one match, starting from beginning of sample, which is the difficult case (see #1241) */
  1118. for (i=1; i<=19; i++) {
  1119. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1120. size_t size1, size2;
  1121. DISPLAYLEVEL(5, "l%i ", i);
  1122. size1 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize, i);
  1123. CHECK_Z(size1);
  1124. size2 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize, i);
  1125. CHECK_Z(size2);
  1126. CHECK_EQ(size1, size2);
  1127. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, i) );
  1128. size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize);
  1129. CHECK_Z(size2);
  1130. CHECK_EQ(size1, size2);
  1131. size2 = ZSTD_compress2(cctx, compressedBuffer, ZSTD_compressBound(sampleSize) - 1, CNBuffer, sampleSize); /* force streaming, as output buffer is not large enough to guarantee success */
  1132. CHECK_Z(size2);
  1133. CHECK_EQ(size1, size2);
  1134. { ZSTD_inBuffer inb;
  1135. ZSTD_outBuffer outb;
  1136. inb.src = CNBuffer;
  1137. inb.pos = 0;
  1138. inb.size = sampleSize;
  1139. outb.dst = compressedBuffer;
  1140. outb.pos = 0;
  1141. outb.size = ZSTD_compressBound(sampleSize) - 1; /* force streaming, as output buffer is not large enough to guarantee success */
  1142. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) );
  1143. assert(inb.pos == inb.size);
  1144. CHECK_EQ(size1, outb.pos);
  1145. }
  1146. ZSTD_freeCCtx(cctx);
  1147. }
  1148. }
  1149. DISPLAYLEVEL(3, "OK \n");
  1150. DISPLAYLEVEL(3, "test%3d : btultra2 & 1st block : ", testNb++);
  1151. { size_t const sampleSize = 1024;
  1152. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1153. ZSTD_inBuffer inb;
  1154. ZSTD_outBuffer outb;
  1155. inb.src = CNBuffer;
  1156. inb.pos = 0;
  1157. inb.size = 0;
  1158. outb.dst = compressedBuffer;
  1159. outb.pos = 0;
  1160. outb.size = compressedBufferSize;
  1161. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel()) );
  1162. inb.size = sampleSize; /* start with something, so that context is already used */
  1163. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) ); /* will break internal assert if stats_init is not disabled */
  1164. assert(inb.pos == inb.size);
  1165. outb.pos = 0; /* cancel output */
  1166. CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(cctx, sampleSize) );
  1167. inb.size = 4; /* too small size : compression will be skipped */
  1168. inb.pos = 0;
  1169. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
  1170. assert(inb.pos == inb.size);
  1171. inb.size += 5; /* too small size : compression will be skipped */
  1172. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
  1173. assert(inb.pos == inb.size);
  1174. inb.size += 11; /* small enough to attempt compression */
  1175. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
  1176. assert(inb.pos == inb.size);
  1177. assert(inb.pos < sampleSize);
  1178. inb.size = sampleSize; /* large enough to trigger stats_init, but no longer at beginning */
  1179. CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) ); /* will break internal assert if stats_init is not disabled */
  1180. assert(inb.pos == inb.size);
  1181. ZSTD_freeCCtx(cctx);
  1182. }
  1183. DISPLAYLEVEL(3, "OK \n");
  1184. DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_getParameter() : ", testNb++);
  1185. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1186. ZSTD_outBuffer out = {NULL, 0, 0};
  1187. ZSTD_inBuffer in = {NULL, 0, 0};
  1188. int value;
  1189. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1190. CHECK_EQ(value, 3);
  1191. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1192. CHECK_EQ(value, 0);
  1193. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, ZSTD_HASHLOG_MIN));
  1194. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1195. CHECK_EQ(value, 3);
  1196. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1197. CHECK_EQ(value, ZSTD_HASHLOG_MIN);
  1198. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7));
  1199. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1200. CHECK_EQ(value, 7);
  1201. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1202. CHECK_EQ(value, ZSTD_HASHLOG_MIN);
  1203. /* Start a compression job */
  1204. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
  1205. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1206. CHECK_EQ(value, 7);
  1207. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1208. CHECK_EQ(value, ZSTD_HASHLOG_MIN);
  1209. /* Reset the CCtx */
  1210. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  1211. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1212. CHECK_EQ(value, 7);
  1213. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1214. CHECK_EQ(value, ZSTD_HASHLOG_MIN);
  1215. /* Reset the parameters */
  1216. ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
  1217. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
  1218. CHECK_EQ(value, 3);
  1219. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
  1220. CHECK_EQ(value, 0);
  1221. ZSTD_freeCCtx(cctx);
  1222. }
  1223. DISPLAYLEVEL(3, "OK \n");
  1224. DISPLAYLEVEL(3, "test%3d : ldm conditionally enabled by default doesn't change cctx params: ", testNb++);
  1225. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1226. ZSTD_outBuffer out = {NULL, 0, 0};
  1227. ZSTD_inBuffer in = {NULL, 0, 0};
  1228. int value;
  1229. /* Even if LDM will be enabled by default in the applied params (since wlog >= 27 and strategy >= btopt),
  1230. * we should not modify the actual parameter specified by the user within the CCtx
  1231. */
  1232. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 27));
  1233. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt));
  1234. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue));
  1235. CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_enableLongDistanceMatching, &value));
  1236. CHECK_EQ(value, 0);
  1237. ZSTD_freeCCtx(cctx);
  1238. }
  1239. DISPLAYLEVEL(3, "OK \n");
  1240. /* this test is really too long, and should be made faster */
  1241. DISPLAYLEVEL(3, "test%3d : overflow protection with large windowLog : ", testNb++);
  1242. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1243. ZSTD_parameters params = ZSTD_getParams(-999, ZSTD_CONTENTSIZE_UNKNOWN, 0);
  1244. size_t const nbCompressions = ((1U << 31) / CNBuffSize) + 2; /* ensure U32 overflow protection is triggered */
  1245. size_t cnb;
  1246. assert(cctx != NULL);
  1247. params.fParams.contentSizeFlag = 0;
  1248. params.cParams.windowLog = ZSTD_WINDOWLOG_MAX;
  1249. for (cnb = 0; cnb < nbCompressions; ++cnb) {
  1250. DISPLAYLEVEL(6, "run %zu / %zu \n", cnb, nbCompressions);
  1251. CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN) ); /* re-use same parameters */
  1252. CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize) );
  1253. }
  1254. ZSTD_freeCCtx(cctx);
  1255. }
  1256. DISPLAYLEVEL(3, "OK \n");
  1257. DISPLAYLEVEL(3, "test%3d : size down context : ", testNb++);
  1258. { ZSTD_CCtx* const largeCCtx = ZSTD_createCCtx();
  1259. assert(largeCCtx != NULL);
  1260. CHECK_Z( ZSTD_compressBegin(largeCCtx, 19) ); /* streaming implies ZSTD_CONTENTSIZE_UNKNOWN, which maximizes memory usage */
  1261. CHECK_Z( ZSTD_compressEnd(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1) );
  1262. { size_t const largeCCtxSize = ZSTD_sizeof_CCtx(largeCCtx); /* size of context must be measured after compression */
  1263. { ZSTD_CCtx* const smallCCtx = ZSTD_createCCtx();
  1264. assert(smallCCtx != NULL);
  1265. CHECK_Z(ZSTD_compressCCtx(smallCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
  1266. { size_t const smallCCtxSize = ZSTD_sizeof_CCtx(smallCCtx);
  1267. DISPLAYLEVEL(5, "(large) %zuKB > 32*%zuKB (small) : ",
  1268. largeCCtxSize>>10, smallCCtxSize>>10);
  1269. assert(largeCCtxSize > 32* smallCCtxSize); /* note : "too large" definition is handled within zstd_compress.c .
  1270. * make this test case extreme, so that it doesn't depend on a possibly fluctuating definition */
  1271. }
  1272. ZSTD_freeCCtx(smallCCtx);
  1273. }
  1274. { U32 const maxNbAttempts = 1100; /* nb of usages before triggering size down is handled within zstd_compress.c.
  1275. * currently defined as 128x, but could be adjusted in the future.
  1276. * make this test long enough so that it's not too much tied to the current definition within zstd_compress.c */
  1277. unsigned u;
  1278. for (u=0; u<maxNbAttempts; u++) {
  1279. CHECK_Z(ZSTD_compressCCtx(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
  1280. if (ZSTD_sizeof_CCtx(largeCCtx) < largeCCtxSize) break; /* sized down */
  1281. }
  1282. DISPLAYLEVEL(5, "size down after %u attempts : ", u);
  1283. if (u==maxNbAttempts) goto _output_error; /* no sizedown happened */
  1284. }
  1285. }
  1286. ZSTD_freeCCtx(largeCCtx);
  1287. }
  1288. DISPLAYLEVEL(3, "OK \n");
  1289. /* Static CCtx tests */
  1290. #define STATIC_CCTX_LEVEL 4
  1291. DISPLAYLEVEL(3, "test%3i : create static CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
  1292. { size_t const staticCStreamSize = ZSTD_estimateCStreamSize(STATIC_CCTX_LEVEL);
  1293. void* const staticCCtxBuffer = malloc(staticCStreamSize);
  1294. size_t const staticDCtxSize = ZSTD_estimateDCtxSize();
  1295. void* const staticDCtxBuffer = malloc(staticDCtxSize);
  1296. DISPLAYLEVEL(4, "CStream size = %u, ", (U32)staticCStreamSize);
  1297. if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) {
  1298. free(staticCCtxBuffer);
  1299. free(staticDCtxBuffer);
  1300. DISPLAY("Not enough memory, aborting\n");
  1301. testResult = 1;
  1302. goto _end;
  1303. }
  1304. { size_t const smallInSize = 32 KB;
  1305. ZSTD_compressionParameters const cparams_small = ZSTD_getCParams(STATIC_CCTX_LEVEL, smallInSize, 0);
  1306. size_t const smallCCtxSize = ZSTD_estimateCCtxSize_usingCParams(cparams_small);
  1307. size_t const staticCCtxSize = ZSTD_estimateCCtxSize(STATIC_CCTX_LEVEL);
  1308. ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, smallCCtxSize);
  1309. ZSTD_DCtx* const staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize);
  1310. DISPLAYLEVEL(4, "Full CCtx size = %u, ", (U32)staticCCtxSize);
  1311. DISPLAYLEVEL(4, "CCtx for 32 KB = %u, ", (U32)smallCCtxSize);
  1312. if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error;
  1313. DISPLAYLEVEL(3, "OK \n");
  1314. DISPLAYLEVEL(3, "test%3i : compress small input with small static CCtx : ", testNb++);
  1315. CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
  1316. compressedBuffer, compressedBufferSize,
  1317. CNBuffer, smallInSize, STATIC_CCTX_LEVEL) );
  1318. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
  1319. (unsigned)cSize, (double)cSize/smallInSize*100);
  1320. DISPLAYLEVEL(3, "test%3i : compress large input with small static CCtx (must fail) : ", testNb++);
  1321. { size_t const r = ZSTD_compressCCtx(staticCCtx,
  1322. compressedBuffer, compressedBufferSize,
  1323. CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL);
  1324. if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_memory_allocation) goto _output_error;
  1325. }
  1326. DISPLAYLEVEL(3, "OK \n");
  1327. DISPLAYLEVEL(3, "test%3i : resize context to full CCtx size : ", testNb++);
  1328. staticCCtx = ZSTD_initStaticCStream(staticCCtxBuffer, staticCCtxSize);
  1329. DISPLAYLEVEL(4, "staticCCtxBuffer = %p, staticCCtx = %p , ", staticCCtxBuffer, (void*)staticCCtx);
  1330. if (staticCCtx == NULL) goto _output_error;
  1331. DISPLAYLEVEL(3, "OK \n");
  1332. DISPLAYLEVEL(3, "test%3i : compress large input with static CCtx : ", testNb++);
  1333. CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
  1334. compressedBuffer, compressedBufferSize,
  1335. CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL) );
  1336. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
  1337. (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1338. DISPLAYLEVEL(3, "test%3i : compress small input often enough to trigger context reduce : ", testNb++);
  1339. { int nbc;
  1340. assert(staticCCtxSize > smallCCtxSize * ZSTD_WORKSPACETOOLARGE_FACTOR); /* ensure size down scenario */
  1341. assert(CNBuffSize > smallInSize + ZSTD_WORKSPACETOOLARGE_MAXDURATION + 3);
  1342. for (nbc=0; nbc<ZSTD_WORKSPACETOOLARGE_MAXDURATION+2; nbc++) {
  1343. CHECK_Z(ZSTD_compressCCtx(staticCCtx,
  1344. compressedBuffer, compressedBufferSize,
  1345. (char*)CNBuffer + nbc, smallInSize,
  1346. STATIC_CCTX_LEVEL) );
  1347. } }
  1348. DISPLAYLEVEL(3, "OK \n")
  1349. DISPLAYLEVEL(3, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
  1350. CHECK_Z( ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL) );
  1351. DISPLAYLEVEL(3, "OK \n");
  1352. DISPLAYLEVEL(3, "test%3i : compression again with static CCtx : ", testNb++);
  1353. CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
  1354. compressedBuffer, compressedBufferSize,
  1355. CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL) );
  1356. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
  1357. (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1358. DISPLAYLEVEL(3, "test%3i : simple decompression test with static DCtx : ", testNb++);
  1359. { size_t const r = ZSTD_decompressDCtx(staticDCtx,
  1360. decodedBuffer, CNBuffSize,
  1361. compressedBuffer, cSize);
  1362. if (r != CNBuffSize) goto _output_error; }
  1363. DISPLAYLEVEL(3, "OK \n");
  1364. DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
  1365. if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
  1366. DISPLAYLEVEL(3, "OK \n");
  1367. DISPLAYLEVEL(3, "test%3i : init CCtx for too large level (must fail) : ", testNb++);
  1368. { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel());
  1369. if (!ZSTD_isError(r)) goto _output_error; }
  1370. DISPLAYLEVEL(3, "OK \n");
  1371. DISPLAYLEVEL(3, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1);
  1372. CHECK( ZSTD_compressBegin(staticCCtx, 1) );
  1373. DISPLAYLEVEL(3, "OK \n");
  1374. DISPLAYLEVEL(3, "test%3i : use CStream on CCtx-sized static context (should fail) : ", testNb++);
  1375. CHECK_Z( ZSTD_initCStream(staticCCtx, STATIC_CCTX_LEVEL) ); /* note : doesn't allocate */
  1376. { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
  1377. ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
  1378. size_t const r = ZSTD_compressStream(staticCCtx, &output, &input); /* now allocates, should fail */
  1379. if (!ZSTD_isError(r)) goto _output_error;
  1380. }
  1381. DISPLAYLEVEL(3, "OK \n");
  1382. DISPLAYLEVEL(3, "test%3i : resize context to CStream size, then stream compress : ", testNb++);
  1383. staticCCtx = ZSTD_initStaticCStream(staticCCtxBuffer, staticCStreamSize);
  1384. assert(staticCCtx != NULL);
  1385. CHECK_Z( ZSTD_initCStream(staticCCtx, STATIC_CCTX_LEVEL) ); /* note : doesn't allocate */
  1386. { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
  1387. ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
  1388. CHECK_Z( ZSTD_compressStream(staticCCtx, &output, &input) );
  1389. }
  1390. DISPLAYLEVEL(3, "OK \n");
  1391. DISPLAYLEVEL(3, "test%3i : CStream for small level %u : ", testNb++, 1);
  1392. CHECK_Z( ZSTD_initCStream(staticCCtx, 1) ); /* note : doesn't allocate */
  1393. { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
  1394. ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
  1395. CHECK_Z( ZSTD_compressStream(staticCCtx, &output, &input) );
  1396. }
  1397. DISPLAYLEVEL(3, "OK \n");
  1398. DISPLAYLEVEL(3, "test%3i : init static CStream with dictionary (should fail) : ", testNb++);
  1399. { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1);
  1400. if (!ZSTD_isError(r)) goto _output_error; }
  1401. DISPLAYLEVEL(3, "OK \n");
  1402. DISPLAYLEVEL(3, "test%3i : use DStream on DCtx-sized static context (should fail) : ", testNb++);
  1403. CHECK_Z( ZSTD_initDStream(staticDCtx) );
  1404. { ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 };
  1405. ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 };
  1406. size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input);
  1407. if (!ZSTD_isError(r)) goto _output_error;
  1408. }
  1409. DISPLAYLEVEL(3, "OK \n");
  1410. }
  1411. free(staticCCtxBuffer);
  1412. free(staticDCtxBuffer);
  1413. }
  1414. DISPLAYLEVEL(3, "test%3i : Static context sizes for negative levels : ", testNb++);
  1415. { size_t const cctxSizeN1 = ZSTD_estimateCCtxSize(-1);
  1416. size_t const cctxSizeP1 = ZSTD_estimateCCtxSize(1);
  1417. size_t const cstreamSizeN1 = ZSTD_estimateCStreamSize(-1);
  1418. size_t const cstreamSizeP1 = ZSTD_estimateCStreamSize(1);
  1419. if (!(0 < cctxSizeN1 && cctxSizeN1 <= cctxSizeP1)) goto _output_error;
  1420. if (!(0 < cstreamSizeN1 && cstreamSizeN1 <= cstreamSizeP1)) goto _output_error;
  1421. }
  1422. DISPLAYLEVEL(3, "OK \n");
  1423. /* ZSTDMT simple MT compression test */
  1424. DISPLAYLEVEL(3, "test%3i : create ZSTDMT CCtx : ", testNb++);
  1425. { ZSTD_CCtx* const mtctx = ZSTD_createCCtx();
  1426. if (mtctx==NULL) {
  1427. DISPLAY("mtctx : not enough memory, aborting \n");
  1428. testResult = 1;
  1429. goto _end;
  1430. }
  1431. CHECK( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_nbWorkers, 2) );
  1432. CHECK( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_compressionLevel, 1) );
  1433. DISPLAYLEVEL(3, "OK \n");
  1434. DISPLAYLEVEL(3, "test%3u : compress %u bytes with 2 threads : ", testNb++, (unsigned)CNBuffSize);
  1435. CHECK_VAR(cSize, ZSTD_compress2(mtctx,
  1436. compressedBuffer, compressedBufferSize,
  1437. CNBuffer, CNBuffSize) );
  1438. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1439. DISPLAYLEVEL(3, "test%3i : decompressed size test : ", testNb++);
  1440. { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
  1441. if (rSize != CNBuffSize) {
  1442. DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (unsigned)rSize, (unsigned)CNBuffSize);
  1443. goto _output_error;
  1444. } }
  1445. DISPLAYLEVEL(3, "OK \n");
  1446. DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
  1447. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  1448. if (r != CNBuffSize) goto _output_error; }
  1449. DISPLAYLEVEL(3, "OK \n");
  1450. DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
  1451. { size_t u;
  1452. for (u=0; u<CNBuffSize; u++) {
  1453. if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;
  1454. } }
  1455. DISPLAYLEVEL(3, "OK \n");
  1456. DISPLAYLEVEL(3, "test%3i : compress -T2 with checksum : ", testNb++);
  1457. CHECK( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_checksumFlag, 1) );
  1458. CHECK( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_contentSizeFlag, 1) );
  1459. CHECK( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_overlapLog, 3) );
  1460. CHECK_VAR(cSize, ZSTD_compress2(mtctx,
  1461. compressedBuffer, compressedBufferSize,
  1462. CNBuffer, CNBuffSize) );
  1463. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1464. DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
  1465. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  1466. if (r != CNBuffSize) goto _output_error; }
  1467. DISPLAYLEVEL(3, "OK \n");
  1468. ZSTD_freeCCtx(mtctx);
  1469. }
  1470. DISPLAYLEVEL(3, "test%3u : compress empty string and decompress with small window log : ", testNb++);
  1471. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1472. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  1473. char out[32];
  1474. if (cctx == NULL || dctx == NULL) goto _output_error;
  1475. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0) );
  1476. CHECK_VAR(cSize, ZSTD_compress2(cctx, out, sizeof(out), NULL, 0) );
  1477. DISPLAYLEVEL(3, "OK (%u bytes)\n", (unsigned)cSize);
  1478. CHECK( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 10) );
  1479. { char const* outPtr = out;
  1480. ZSTD_inBuffer inBuffer = { outPtr, cSize, 0 };
  1481. ZSTD_outBuffer outBuffer = { NULL, 0, 0 };
  1482. size_t dSize;
  1483. CHECK_VAR(dSize, ZSTD_decompressStream(dctx, &outBuffer, &inBuffer) );
  1484. if (dSize != 0) goto _output_error;
  1485. }
  1486. ZSTD_freeDCtx(dctx);
  1487. ZSTD_freeCCtx(cctx);
  1488. }
  1489. DISPLAYLEVEL(3, "test%3i : compress with block splitting : ", testNb++)
  1490. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  1491. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_useBlockSplitter, ZSTD_ps_enable) );
  1492. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  1493. CHECK(cSize);
  1494. ZSTD_freeCCtx(cctx);
  1495. }
  1496. DISPLAYLEVEL(3, "OK \n");
  1497. DISPLAYLEVEL(3, "test%3i : compress -T2 with/without literals compression : ", testNb++)
  1498. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  1499. size_t cSize1, cSize2;
  1500. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
  1501. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2) );
  1502. cSize1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  1503. CHECK(cSize1);
  1504. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_literalCompressionMode, ZSTD_ps_disable) );
  1505. cSize2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  1506. CHECK(cSize2);
  1507. CHECK_LT(cSize1, cSize2);
  1508. ZSTD_freeCCtx(cctx);
  1509. }
  1510. DISPLAYLEVEL(3, "OK \n");
  1511. DISPLAYLEVEL(3, "test%3i : Multithreaded ZSTD_compress2() with rsyncable : ", testNb++)
  1512. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  1513. /* Set rsyncable and don't give the ZSTD_compressBound(CNBuffSize) so
  1514. * ZSTDMT is forced to not take the shortcut.
  1515. */
  1516. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
  1517. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1) );
  1518. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_rsyncable, 1) );
  1519. CHECK( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize - 1, CNBuffer, CNBuffSize) );
  1520. ZSTD_freeCCtx(cctx);
  1521. }
  1522. DISPLAYLEVEL(3, "OK \n");
  1523. DISPLAYLEVEL(3, "test%3i : setting multithreaded parameters : ", testNb++)
  1524. { ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
  1525. int const jobSize = 512 KB;
  1526. int value;
  1527. /* Check that the overlap log and job size are unset. */
  1528. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
  1529. CHECK_EQ(value, 0);
  1530. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
  1531. CHECK_EQ(value, 0);
  1532. /* Set and check the overlap log and job size. */
  1533. CHECK( ZSTD_CCtxParams_setParameter(params, ZSTD_c_overlapLog, 5) );
  1534. CHECK( ZSTD_CCtxParams_setParameter(params, ZSTD_c_jobSize, jobSize) );
  1535. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
  1536. CHECK_EQ(value, 5);
  1537. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
  1538. CHECK_EQ(value, jobSize);
  1539. /* Set the number of workers and check the overlap log and job size. */
  1540. CHECK( ZSTD_CCtxParams_setParameter(params, ZSTD_c_nbWorkers, 2) );
  1541. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
  1542. CHECK_EQ(value, 5);
  1543. CHECK( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
  1544. CHECK_EQ(value, jobSize);
  1545. ZSTD_freeCCtxParams(params);
  1546. }
  1547. DISPLAYLEVEL(3, "OK \n");
  1548. /* Simple API multiframe test */
  1549. DISPLAYLEVEL(3, "test%3i : compress multiple frames : ", testNb++);
  1550. { size_t off = 0;
  1551. int i;
  1552. int const segs = 4;
  1553. /* only use the first half so we don't push against size limit of compressedBuffer */
  1554. size_t const segSize = (CNBuffSize / 2) / segs;
  1555. const U32 skipLen = 129 KB;
  1556. char* const skipBuff = (char*)malloc(skipLen);
  1557. assert(skipBuff != NULL);
  1558. memset(skipBuff, 0, skipLen);
  1559. for (i = 0; i < segs; i++) {
  1560. CHECK_NEWV(r, ZSTD_compress(
  1561. (BYTE*)compressedBuffer + off, CNBuffSize - off,
  1562. (BYTE*)CNBuffer + segSize * (size_t)i, segSize,
  1563. 5) );
  1564. off += r;
  1565. if (i == segs/2) {
  1566. /* insert skippable frame */
  1567. size_t const skippableSize =
  1568. ZSTD_writeSkippableFrame((BYTE*)compressedBuffer + off, compressedBufferSize,
  1569. skipBuff, skipLen, seed % 15);
  1570. CHECK_Z(skippableSize);
  1571. off += skippableSize;
  1572. }
  1573. }
  1574. cSize = off;
  1575. free(skipBuff);
  1576. }
  1577. DISPLAYLEVEL(3, "OK \n");
  1578. DISPLAYLEVEL(3, "test%3i : get decompressed size of multiple frames : ", testNb++);
  1579. { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
  1580. if (r != CNBuffSize / 2) goto _output_error; }
  1581. DISPLAYLEVEL(3, "OK \n");
  1582. DISPLAYLEVEL(3, "test%3i : get tight decompressed bound of multiple frames : ", testNb++);
  1583. { unsigned long long const bound = ZSTD_decompressBound(compressedBuffer, cSize);
  1584. if (bound != CNBuffSize / 2) goto _output_error; }
  1585. DISPLAYLEVEL(3, "OK \n");
  1586. DISPLAYLEVEL(3, "test%3i : decompress multiple frames : ", testNb++);
  1587. { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
  1588. if (r != CNBuffSize / 2) goto _output_error; }
  1589. DISPLAYLEVEL(3, "OK \n");
  1590. DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
  1591. if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
  1592. DISPLAYLEVEL(3, "OK \n");
  1593. /* Simple API skippable frame test */
  1594. DISPLAYLEVEL(3, "test%3i : read/write a skippable frame : ", testNb++);
  1595. { U32 i;
  1596. unsigned readMagic;
  1597. unsigned long long receivedSize;
  1598. size_t skippableSize;
  1599. const U32 skipLen = 129 KB;
  1600. char* const skipBuff = (char*)malloc(skipLen);
  1601. assert(skipBuff != NULL);
  1602. for (i = 0; i < skipLen; i++)
  1603. skipBuff[i] = (char) ((seed + i) % 256);
  1604. skippableSize = ZSTD_writeSkippableFrame(
  1605. compressedBuffer, compressedBufferSize,
  1606. skipBuff, skipLen, seed % 15);
  1607. CHECK_Z(skippableSize);
  1608. CHECK_EQ(1, ZSTD_isSkippableFrame(compressedBuffer, skippableSize));
  1609. receivedSize = ZSTD_readSkippableFrame(decodedBuffer, CNBuffSize, &readMagic, compressedBuffer, skippableSize);
  1610. CHECK_EQ(skippableSize, receivedSize + ZSTD_SKIPPABLEHEADERSIZE);
  1611. CHECK_EQ(seed % 15, readMagic);
  1612. if (memcmp(decodedBuffer, skipBuff, skipLen) != 0) goto _output_error;
  1613. free(skipBuff);
  1614. }
  1615. DISPLAYLEVEL(3, "OK \n");
  1616. DISPLAYLEVEL(3, "test%3i : read/write an empty skippable frame : ", testNb++);
  1617. {
  1618. unsigned readMagic;
  1619. unsigned long long receivedSize;
  1620. size_t skippableSize;
  1621. skippableSize = ZSTD_writeSkippableFrame(
  1622. compressedBuffer, compressedBufferSize,
  1623. CNBuffer, 0, seed % 15);
  1624. CHECK_EQ(ZSTD_SKIPPABLEHEADERSIZE, skippableSize);
  1625. CHECK_EQ(1, ZSTD_isSkippableFrame(compressedBuffer, skippableSize));
  1626. receivedSize = ZSTD_readSkippableFrame(NULL, 0, &readMagic, compressedBuffer, skippableSize);
  1627. CHECK_EQ(skippableSize, receivedSize + ZSTD_SKIPPABLEHEADERSIZE);
  1628. CHECK_EQ(seed % 15, readMagic);
  1629. }
  1630. DISPLAYLEVEL(3, "OK \n");
  1631. /* Dictionary and CCtx Duplication tests */
  1632. { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
  1633. ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
  1634. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  1635. static const size_t dictSize = 551;
  1636. assert(dctx != NULL); assert(ctxOrig != NULL); assert(ctxDuplicated != NULL);
  1637. DISPLAYLEVEL(3, "test%3i : copy context too soon : ", testNb++);
  1638. { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
  1639. if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */
  1640. DISPLAYLEVEL(3, "OK \n");
  1641. DISPLAYLEVEL(3, "test%3i : load dictionary into context : ", testNb++);
  1642. CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
  1643. CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
  1644. DISPLAYLEVEL(3, "OK \n");
  1645. DISPLAYLEVEL(3, "test%3i : compress with flat dictionary : ", testNb++);
  1646. cSize = 0;
  1647. CHECKPLUS(r, ZSTD_compressEnd(ctxOrig,
  1648. compressedBuffer, compressedBufferSize,
  1649. (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
  1650. cSize += r);
  1651. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1652. DISPLAYLEVEL(3, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
  1653. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  1654. decodedBuffer, CNBuffSize,
  1655. compressedBuffer, cSize,
  1656. CNBuffer, dictSize),
  1657. if (r != CNBuffSize - dictSize) goto _output_error);
  1658. DISPLAYLEVEL(3, "OK \n");
  1659. DISPLAYLEVEL(3, "test%3i : compress with duplicated context : ", testNb++);
  1660. { size_t const cSizeOrig = cSize;
  1661. cSize = 0;
  1662. CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated,
  1663. compressedBuffer, compressedBufferSize,
  1664. (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
  1665. cSize += r);
  1666. if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */
  1667. }
  1668. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1669. DISPLAYLEVEL(3, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
  1670. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  1671. decodedBuffer, CNBuffSize,
  1672. compressedBuffer, cSize,
  1673. CNBuffer, dictSize),
  1674. if (r != CNBuffSize - dictSize) goto _output_error);
  1675. DISPLAYLEVEL(3, "OK \n");
  1676. DISPLAYLEVEL(3, "test%3i : decompress with DDict : ", testNb++);
  1677. { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, dictSize);
  1678. size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
  1679. if (r != CNBuffSize - dictSize) goto _output_error;
  1680. DISPLAYLEVEL(3, "OK (size of DDict : %u) \n", (unsigned)ZSTD_sizeof_DDict(ddict));
  1681. ZSTD_freeDDict(ddict);
  1682. }
  1683. DISPLAYLEVEL(3, "test%3i : decompress with static DDict : ", testNb++);
  1684. { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
  1685. void* const ddictBuffer = malloc(ddictBufferSize);
  1686. if (ddictBuffer == NULL) goto _output_error;
  1687. { const ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
  1688. size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
  1689. if (r != CNBuffSize - dictSize) goto _output_error;
  1690. }
  1691. free(ddictBuffer);
  1692. DISPLAYLEVEL(3, "OK (size of static DDict : %u) \n", (unsigned)ddictBufferSize);
  1693. }
  1694. DISPLAYLEVEL(3, "test%3i : check content size on duplicated context : ", testNb++);
  1695. { size_t const testSize = CNBuffSize / 3;
  1696. CHECK( ZSTD_compressBegin(ctxOrig, ZSTD_defaultCLevel()) );
  1697. CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
  1698. CHECK_VAR(cSize, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
  1699. (const char*)CNBuffer + dictSize, testSize) );
  1700. { ZSTD_frameHeader zfh;
  1701. if (ZSTD_getFrameHeader(&zfh, compressedBuffer, cSize)) goto _output_error;
  1702. if ((zfh.frameContentSize != testSize) && (zfh.frameContentSize != 0)) goto _output_error;
  1703. } }
  1704. DISPLAYLEVEL(3, "OK \n");
  1705. /* Note : these tests should be replaced by proper regression tests,
  1706. * but existing ones do not focus on small data + dictionary + all levels.
  1707. */
  1708. if ((int)(compressibility * 100 + 0.1) == FUZ_compressibility_default) { /* test only valid with known input */
  1709. size_t const flatdictSize = 22 KB;
  1710. size_t const contentSize = 9 KB;
  1711. const void* const dict = (const char*)CNBuffer;
  1712. const void* const contentStart = (const char*)dict + flatdictSize;
  1713. /* These upper bounds are generally within a few bytes of the compressed size */
  1714. size_t target_nodict_cSize[22+1] = { 3840, 3770, 3870, 3830, 3770,
  1715. 3770, 3770, 3770, 3750, 3750,
  1716. 3742, 3675, 3674, 3665, 3664,
  1717. 3663, 3662, 3661, 3660, 3660,
  1718. 3660, 3660, 3660 };
  1719. size_t const target_wdict_cSize[22+1] = { 2830, 2896, 2893, 2820, 2940,
  1720. 2950, 2950, 2925, 2900, 2891,
  1721. 2910, 2910, 2910, 2780, 2775,
  1722. 2765, 2760, 2755, 2754, 2753,
  1723. 2753, 2753, 2753 };
  1724. int l = 1;
  1725. int const maxLevel = ZSTD_maxCLevel();
  1726. /* clevels with strategies that support rowhash on small inputs */
  1727. int rowLevel = 4;
  1728. int const rowLevelEnd = 8;
  1729. DISPLAYLEVEL(3, "test%3i : flat-dictionary efficiency test : \n", testNb++);
  1730. assert(maxLevel == 22);
  1731. RDG_genBuffer(CNBuffer, flatdictSize + contentSize, compressibility, 0., seed);
  1732. DISPLAYLEVEL(4, "content hash : %016llx; dict hash : %016llx \n",
  1733. (unsigned long long)XXH64(contentStart, contentSize, 0),
  1734. (unsigned long long)XXH64(dict, flatdictSize, 0));
  1735. for ( ; l <= maxLevel; l++) {
  1736. size_t const nodict_cSize = ZSTD_compress(compressedBuffer, compressedBufferSize,
  1737. contentStart, contentSize, l);
  1738. if (nodict_cSize > target_nodict_cSize[l]) {
  1739. DISPLAYLEVEL(1, "error : compression at level %i worse than expected (%u > %u) \n",
  1740. l, (unsigned)nodict_cSize, (unsigned)target_nodict_cSize[l]);
  1741. goto _output_error;
  1742. }
  1743. DISPLAYLEVEL(4, "level %i : max expected %u >= reached %u \n",
  1744. l, (unsigned)target_nodict_cSize[l], (unsigned)nodict_cSize);
  1745. }
  1746. for ( l=1 ; l <= maxLevel; l++) {
  1747. size_t const wdict_cSize = ZSTD_compress_usingDict(ctxOrig,
  1748. compressedBuffer, compressedBufferSize,
  1749. contentStart, contentSize,
  1750. dict, flatdictSize,
  1751. l);
  1752. if (wdict_cSize > target_wdict_cSize[l]) {
  1753. DISPLAYLEVEL(1, "error : compression with dictionary at level %i worse than expected (%u > %u) \n",
  1754. l, (unsigned)wdict_cSize, (unsigned)target_wdict_cSize[l]);
  1755. goto _output_error;
  1756. }
  1757. DISPLAYLEVEL(4, "level %i with dictionary : max expected %u >= reached %u \n",
  1758. l, (unsigned)target_wdict_cSize[l], (unsigned)wdict_cSize);
  1759. }
  1760. /* Compression with ZSTD_compress2 and row match finder force enabled.
  1761. * Give some slack for force-enabled row matchfinder since we're on a small input (9KB)
  1762. */
  1763. for ( ; rowLevel <= rowLevelEnd; ++rowLevel) target_nodict_cSize[rowLevel] += 5;
  1764. for (l=1 ; l <= maxLevel; l++) {
  1765. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1766. size_t nodict_cSize;
  1767. ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, l);
  1768. ZSTD_CCtx_setParameter(cctx, ZSTD_c_useRowMatchFinder, ZSTD_ps_enable);
  1769. nodict_cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize,
  1770. contentStart, contentSize);
  1771. if (nodict_cSize > target_nodict_cSize[l]) {
  1772. DISPLAYLEVEL(1, "error : compression with compress2 at level %i worse than expected (%u > %u) \n",
  1773. l, (unsigned)nodict_cSize, (unsigned)target_nodict_cSize[l]);
  1774. ZSTD_freeCCtx(cctx);
  1775. goto _output_error;
  1776. }
  1777. DISPLAYLEVEL(4, "level %i with compress2 : max expected %u >= reached %u \n",
  1778. l, (unsigned)target_nodict_cSize[l], (unsigned)nodict_cSize);
  1779. ZSTD_freeCCtx(cctx);
  1780. }
  1781. /* Dict compression with DMS */
  1782. for ( l=1 ; l <= maxLevel; l++) {
  1783. size_t wdict_cSize;
  1784. CHECK_Z( ZSTD_CCtx_loadDictionary(ctxOrig, dict, flatdictSize) );
  1785. CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_compressionLevel, l) );
  1786. CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_enableDedicatedDictSearch, 0) );
  1787. CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_forceAttachDict, ZSTD_dictForceAttach) );
  1788. wdict_cSize = ZSTD_compress2(ctxOrig, compressedBuffer, compressedBufferSize, contentStart, contentSize);
  1789. if (wdict_cSize > target_wdict_cSize[l]) {
  1790. DISPLAYLEVEL(1, "error : compression with dictionary and compress2 at level %i worse than expected (%u > %u) \n",
  1791. l, (unsigned)wdict_cSize, (unsigned)target_wdict_cSize[l]);
  1792. goto _output_error;
  1793. }
  1794. DISPLAYLEVEL(4, "level %i with dictionary and compress2 : max expected %u >= reached %u \n",
  1795. l, (unsigned)target_wdict_cSize[l], (unsigned)wdict_cSize);
  1796. }
  1797. DISPLAYLEVEL(4, "compression efficiency tests OK \n");
  1798. }
  1799. ZSTD_freeCCtx(ctxOrig);
  1800. ZSTD_freeCCtx(ctxDuplicated);
  1801. ZSTD_freeDCtx(dctx);
  1802. }
  1803. /* Dictionary and dictBuilder tests */
  1804. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  1805. size_t const dictBufferCapacity = 16 KB;
  1806. void* const dictBuffer = malloc(dictBufferCapacity);
  1807. size_t const totalSampleSize = 1 MB;
  1808. size_t const sampleUnitSize = 8 KB;
  1809. U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
  1810. size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
  1811. size_t dictSize;
  1812. U32 dictID;
  1813. size_t dictHeaderSize;
  1814. size_t dictBufferFixedSize = 144;
  1815. unsigned char const dictBufferFixed[144] = {0x37, 0xa4, 0x30, 0xec, 0x63, 0x00, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1f,
  1816. 0x0f, 0x00, 0x28, 0xe5, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1817. 0x00, 0x80, 0x0f, 0x9e, 0x0f, 0x00, 0x00, 0x24, 0x40, 0x80, 0x00, 0x01,
  1818. 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0xde, 0x08,
  1819. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  1820. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  1821. 0x08, 0x08, 0x08, 0x08, 0xbc, 0xe1, 0x4b, 0x92, 0x0e, 0xb4, 0x7b, 0x18,
  1822. 0x86, 0x61, 0x18, 0xc6, 0x18, 0x63, 0x8c, 0x31, 0xc6, 0x18, 0x63, 0x8c,
  1823. 0x31, 0x66, 0x66, 0x66, 0x66, 0xb6, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x04,
  1824. 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x73, 0x6f, 0x64, 0x61,
  1825. 0x6c, 0x65, 0x73, 0x20, 0x74, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x20, 0x65,
  1826. 0x6c, 0x65, 0x69, 0x66, 0x65, 0x6e, 0x64, 0x2e, 0x20, 0x41, 0x6c, 0x69};
  1827. if (dictBuffer==NULL || samplesSizes==NULL) {
  1828. free(dictBuffer);
  1829. free(samplesSizes);
  1830. goto _output_error;
  1831. }
  1832. DISPLAYLEVEL(3, "test%3i : dictBuilder on cyclic data : ", testNb++);
  1833. assert(compressedBufferSize >= totalSampleSize);
  1834. { U32 u; for (u=0; u<totalSampleSize; u++) ((BYTE*)decodedBuffer)[u] = (BYTE)u; }
  1835. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1836. { size_t const sDictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
  1837. decodedBuffer, samplesSizes, nbSamples);
  1838. if (ZDICT_isError(sDictSize)) goto _output_error;
  1839. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)sDictSize);
  1840. }
  1841. DISPLAYLEVEL(3, "test%3i : dictBuilder : ", testNb++);
  1842. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1843. dictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
  1844. CNBuffer, samplesSizes, nbSamples);
  1845. if (ZDICT_isError(dictSize)) goto _output_error;
  1846. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  1847. DISPLAYLEVEL(3, "test%3i : Multithreaded COVER dictBuilder : ", testNb++);
  1848. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1849. { ZDICT_cover_params_t coverParams;
  1850. memset(&coverParams, 0, sizeof(coverParams));
  1851. coverParams.steps = 8;
  1852. coverParams.nbThreads = 4;
  1853. dictSize = ZDICT_optimizeTrainFromBuffer_cover(
  1854. dictBuffer, dictBufferCapacity,
  1855. CNBuffer, samplesSizes, nbSamples/8, /* less samples for faster tests */
  1856. &coverParams);
  1857. if (ZDICT_isError(dictSize)) goto _output_error;
  1858. }
  1859. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  1860. DISPLAYLEVEL(3, "test%3i : COVER dictBuilder with shrinkDict: ", testNb++);
  1861. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1862. { ZDICT_cover_params_t coverParams;
  1863. memset(&coverParams, 0, sizeof(coverParams));
  1864. coverParams.steps = 8;
  1865. coverParams.nbThreads = 4;
  1866. coverParams.shrinkDict = 1;
  1867. coverParams.shrinkDictMaxRegression = 1;
  1868. dictSize = ZDICT_optimizeTrainFromBuffer_cover(
  1869. dictBuffer, dictBufferCapacity,
  1870. CNBuffer, samplesSizes, nbSamples/8, /* less samples for faster tests */
  1871. &coverParams);
  1872. if (ZDICT_isError(dictSize)) goto _output_error;
  1873. }
  1874. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  1875. DISPLAYLEVEL(3, "test%3i : Multithreaded FASTCOVER dictBuilder : ", testNb++);
  1876. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1877. { ZDICT_fastCover_params_t fastCoverParams;
  1878. memset(&fastCoverParams, 0, sizeof(fastCoverParams));
  1879. fastCoverParams.steps = 8;
  1880. fastCoverParams.nbThreads = 4;
  1881. dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(
  1882. dictBuffer, dictBufferCapacity,
  1883. CNBuffer, samplesSizes, nbSamples,
  1884. &fastCoverParams);
  1885. if (ZDICT_isError(dictSize)) goto _output_error;
  1886. }
  1887. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  1888. DISPLAYLEVEL(3, "test%3i : FASTCOVER dictBuilder with shrinkDict: ", testNb++);
  1889. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  1890. { ZDICT_fastCover_params_t fastCoverParams;
  1891. memset(&fastCoverParams, 0, sizeof(fastCoverParams));
  1892. fastCoverParams.steps = 8;
  1893. fastCoverParams.nbThreads = 4;
  1894. fastCoverParams.shrinkDict = 1;
  1895. fastCoverParams.shrinkDictMaxRegression = 1;
  1896. dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(
  1897. dictBuffer, dictBufferCapacity,
  1898. CNBuffer, samplesSizes, nbSamples,
  1899. &fastCoverParams);
  1900. if (ZDICT_isError(dictSize)) goto _output_error;
  1901. }
  1902. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  1903. DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
  1904. dictID = ZDICT_getDictID(dictBuffer, dictSize);
  1905. if (dictID==0) goto _output_error;
  1906. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
  1907. DISPLAYLEVEL(3, "test%3i : check dict header size no error : ", testNb++);
  1908. dictHeaderSize = ZDICT_getDictHeaderSize(dictBuffer, dictSize);
  1909. if (dictHeaderSize==0) goto _output_error;
  1910. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictHeaderSize);
  1911. DISPLAYLEVEL(3, "test%3i : check dict header size correctness : ", testNb++);
  1912. { dictHeaderSize = ZDICT_getDictHeaderSize(dictBufferFixed, dictBufferFixedSize);
  1913. if (dictHeaderSize != 115) goto _output_error;
  1914. }
  1915. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictHeaderSize);
  1916. DISPLAYLEVEL(3, "test%3i : compress with dictionary : ", testNb++);
  1917. cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize,
  1918. CNBuffer, CNBuffSize,
  1919. dictBuffer, dictSize, 4);
  1920. if (ZSTD_isError(cSize)) goto _output_error;
  1921. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1922. DISPLAYLEVEL(3, "test%3i : retrieve dictID from dictionary : ", testNb++);
  1923. { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
  1924. if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
  1925. }
  1926. DISPLAYLEVEL(3, "OK \n");
  1927. DISPLAYLEVEL(3, "test%3i : retrieve dictID from frame : ", testNb++);
  1928. { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
  1929. if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
  1930. }
  1931. DISPLAYLEVEL(3, "OK \n");
  1932. DISPLAYLEVEL(3, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
  1933. { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
  1934. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  1935. decodedBuffer, CNBuffSize,
  1936. compressedBuffer, cSize,
  1937. dictBuffer, dictSize),
  1938. if (r != CNBuffSize) goto _output_error);
  1939. ZSTD_freeDCtx(dctx);
  1940. }
  1941. DISPLAYLEVEL(3, "OK \n");
  1942. DISPLAYLEVEL(3, "test%3i : estimate CDict size : ", testNb++);
  1943. { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
  1944. size_t const estimatedSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byRef);
  1945. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)estimatedSize);
  1946. }
  1947. DISPLAYLEVEL(3, "test%3i : compress with CDict ", testNb++);
  1948. { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
  1949. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
  1950. ZSTD_dlm_byRef, ZSTD_dct_auto,
  1951. cParams, ZSTD_defaultCMem);
  1952. assert(cdict != NULL);
  1953. DISPLAYLEVEL(3, "(size : %u) : ", (unsigned)ZSTD_sizeof_CDict(cdict));
  1954. assert(ZSTD_getDictID_fromDict(dictBuffer, dictSize) == ZSTD_getDictID_fromCDict(cdict));
  1955. cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize,
  1956. CNBuffer, CNBuffSize, cdict);
  1957. ZSTD_freeCDict(cdict);
  1958. if (ZSTD_isError(cSize)) goto _output_error;
  1959. }
  1960. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  1961. DISPLAYLEVEL(3, "test%3i : retrieve dictID from frame : ", testNb++);
  1962. { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
  1963. if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
  1964. }
  1965. DISPLAYLEVEL(3, "OK \n");
  1966. DISPLAYLEVEL(3, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
  1967. { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
  1968. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  1969. decodedBuffer, CNBuffSize,
  1970. compressedBuffer, cSize,
  1971. dictBuffer, dictSize),
  1972. if (r != CNBuffSize) goto _output_error);
  1973. ZSTD_freeDCtx(dctx);
  1974. }
  1975. DISPLAYLEVEL(3, "OK \n");
  1976. DISPLAYLEVEL(3, "test%3i : compress with static CDict : ", testNb++);
  1977. { int const maxLevel = ZSTD_maxCLevel();
  1978. int level;
  1979. for (level = 1; level <= maxLevel; ++level) {
  1980. ZSTD_compressionParameters const cParams = ZSTD_getCParams(level, CNBuffSize, dictSize);
  1981. size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy);
  1982. void* const cdictBuffer = malloc(cdictSize);
  1983. if (cdictBuffer==NULL) goto _output_error;
  1984. { const ZSTD_CDict* const cdict = ZSTD_initStaticCDict(
  1985. cdictBuffer, cdictSize,
  1986. dictBuffer, dictSize,
  1987. ZSTD_dlm_byCopy, ZSTD_dct_auto,
  1988. cParams);
  1989. if (cdict == NULL) {
  1990. DISPLAY("ZSTD_initStaticCDict failed ");
  1991. goto _output_error;
  1992. }
  1993. cSize = ZSTD_compress_usingCDict(cctx,
  1994. compressedBuffer, compressedBufferSize,
  1995. CNBuffer, MIN(10 KB, CNBuffSize), cdict);
  1996. if (ZSTD_isError(cSize)) {
  1997. DISPLAY("ZSTD_compress_usingCDict failed ");
  1998. goto _output_error;
  1999. } }
  2000. free(cdictBuffer);
  2001. } }
  2002. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  2003. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++);
  2004. { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ };
  2005. ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
  2006. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cParams, ZSTD_defaultCMem);
  2007. assert(cdict != NULL);
  2008. cSize = ZSTD_compress_usingCDict_advanced(cctx,
  2009. compressedBuffer, compressedBufferSize,
  2010. CNBuffer, CNBuffSize,
  2011. cdict, fParams);
  2012. ZSTD_freeCDict(cdict);
  2013. if (ZSTD_isError(cSize)) goto _output_error;
  2014. }
  2015. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  2016. DISPLAYLEVEL(3, "test%3i : try retrieving contentSize from frame : ", testNb++);
  2017. { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
  2018. if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
  2019. }
  2020. DISPLAYLEVEL(3, "OK (unknown)\n");
  2021. DISPLAYLEVEL(3, "test%3i : frame built without dictID should be decompressible : ", testNb++);
  2022. { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2023. assert(dctx != NULL);
  2024. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  2025. decodedBuffer, CNBuffSize,
  2026. compressedBuffer, cSize,
  2027. dictBuffer, dictSize),
  2028. if (r != CNBuffSize) goto _output_error);
  2029. ZSTD_freeDCtx(dctx);
  2030. }
  2031. DISPLAYLEVEL(3, "OK \n");
  2032. DISPLAYLEVEL(3, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++);
  2033. { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
  2034. p.fParams.noDictIDFlag = 1;
  2035. cSize = ZSTD_compress_advanced(cctx, compressedBuffer, compressedBufferSize,
  2036. CNBuffer, CNBuffSize,
  2037. dictBuffer, dictSize, p);
  2038. if (ZSTD_isError(cSize)) goto _output_error;
  2039. }
  2040. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
  2041. DISPLAYLEVEL(3, "test%3i : frame built without dictID should be decompressible : ", testNb++);
  2042. { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
  2043. CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
  2044. decodedBuffer, CNBuffSize,
  2045. compressedBuffer, cSize,
  2046. dictBuffer, dictSize),
  2047. if (r != CNBuffSize) goto _output_error);
  2048. ZSTD_freeDCtx(dctx);
  2049. }
  2050. DISPLAYLEVEL(3, "OK \n");
  2051. DISPLAYLEVEL(3, "test%3i : dictionary containing only header should return error : ", testNb++);
  2052. { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2053. assert(dctx != NULL);
  2054. { const size_t ret = ZSTD_decompress_usingDict(
  2055. dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
  2056. "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
  2057. if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted)
  2058. goto _output_error;
  2059. }
  2060. ZSTD_freeDCtx(dctx);
  2061. }
  2062. DISPLAYLEVEL(3, "OK \n");
  2063. DISPLAYLEVEL(3, "test%3i : Building cdict w/ ZSTD_dct_fullDict on a good dictionary : ", testNb++);
  2064. { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
  2065. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
  2066. if (cdict==NULL) goto _output_error;
  2067. ZSTD_freeCDict(cdict);
  2068. }
  2069. DISPLAYLEVEL(3, "OK \n");
  2070. DISPLAYLEVEL(3, "test%3i : Building cdict w/ ZSTD_dct_fullDict on a rawContent (must fail) : ", testNb++);
  2071. { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
  2072. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
  2073. if (cdict!=NULL) goto _output_error;
  2074. ZSTD_freeCDict(cdict);
  2075. }
  2076. DISPLAYLEVEL(3, "OK \n");
  2077. { char* rawDictBuffer = (char*)malloc(dictSize);
  2078. assert(rawDictBuffer);
  2079. memcpy(rawDictBuffer, (char*)dictBuffer + 2, dictSize - 2);
  2080. memset(rawDictBuffer + dictSize - 2, 0, 2);
  2081. MEM_writeLE32((char*)rawDictBuffer, ZSTD_MAGIC_DICTIONARY);
  2082. DISPLAYLEVEL(3, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dct_auto should fail : ", testNb++);
  2083. {
  2084. size_t ret;
  2085. /* Either operation is allowed to fail, but one must fail. */
  2086. ret = ZSTD_CCtx_loadDictionary_advanced(
  2087. cctx, (const char*)rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
  2088. if (!ZSTD_isError(ret)) {
  2089. ret = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100));
  2090. if (!ZSTD_isError(ret)) goto _output_error;
  2091. }
  2092. }
  2093. DISPLAYLEVEL(3, "OK \n");
  2094. DISPLAYLEVEL(3, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dct_rawContent should pass : ", testNb++);
  2095. {
  2096. size_t ret;
  2097. ret = ZSTD_CCtx_loadDictionary_advanced(
  2098. cctx, (const char*)rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_rawContent);
  2099. if (ZSTD_isError(ret)) goto _output_error;
  2100. ret = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100));
  2101. if (ZSTD_isError(ret)) goto _output_error;
  2102. }
  2103. DISPLAYLEVEL(3, "OK \n");
  2104. DISPLAYLEVEL(3, "test%3i : Testing non-attached CDict with ZSTD_dct_rawContent : ", testNb++);
  2105. { size_t const srcSize = MIN(CNBuffSize, 100);
  2106. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2107. /* Force the dictionary to be reloaded in raw content mode */
  2108. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, ZSTD_dictForceLoad));
  2109. CHECK_Z(ZSTD_CCtx_loadDictionary_advanced(cctx, rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_rawContent));
  2110. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize);
  2111. CHECK_Z(cSize);
  2112. }
  2113. DISPLAYLEVEL(3, "OK \n");
  2114. free(rawDictBuffer);
  2115. }
  2116. DISPLAYLEVEL(3, "test%3i : ZSTD_CCtx_refCDict() then set parameters : ", testNb++);
  2117. { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, dictSize, 1);
  2118. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2119. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
  2120. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 12 ));
  2121. CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
  2122. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
  2123. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 12 ));
  2124. ZSTD_freeCDict(cdict);
  2125. }
  2126. DISPLAYLEVEL(3, "OK \n");
  2127. DISPLAYLEVEL(3, "test%3i : Loading dictionary before setting parameters is the same as loading after : ", testNb++);
  2128. {
  2129. size_t size1, size2;
  2130. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2131. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7) );
  2132. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
  2133. size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2134. if (ZSTD_isError(size1)) goto _output_error;
  2135. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2136. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
  2137. CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7) );
  2138. size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2139. if (ZSTD_isError(size2)) goto _output_error;
  2140. if (size1 != size2) goto _output_error;
  2141. }
  2142. DISPLAYLEVEL(3, "OK \n");
  2143. DISPLAYLEVEL(3, "test%3i : Loading a dictionary clears the prefix : ", testNb++);
  2144. {
  2145. CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
  2146. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
  2147. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2148. }
  2149. DISPLAYLEVEL(3, "OK \n");
  2150. DISPLAYLEVEL(3, "test%3i : Loading a dictionary clears the cdict : ", testNb++);
  2151. {
  2152. ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
  2153. CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
  2154. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
  2155. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2156. ZSTD_freeCDict(cdict);
  2157. }
  2158. DISPLAYLEVEL(3, "OK \n");
  2159. DISPLAYLEVEL(3, "test%3i : Loading a cdict clears the prefix : ", testNb++);
  2160. {
  2161. ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
  2162. CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
  2163. CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
  2164. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2165. ZSTD_freeCDict(cdict);
  2166. }
  2167. DISPLAYLEVEL(3, "OK \n");
  2168. DISPLAYLEVEL(3, "test%3i : Loading a cdict clears the dictionary : ", testNb++);
  2169. {
  2170. ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
  2171. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
  2172. CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
  2173. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2174. ZSTD_freeCDict(cdict);
  2175. }
  2176. DISPLAYLEVEL(3, "OK \n");
  2177. DISPLAYLEVEL(3, "test%3i : Loading a prefix clears the dictionary : ", testNb++);
  2178. {
  2179. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
  2180. CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
  2181. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2182. }
  2183. DISPLAYLEVEL(3, "OK \n");
  2184. DISPLAYLEVEL(3, "test%3i : Loading a prefix clears the cdict : ", testNb++);
  2185. {
  2186. ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
  2187. CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
  2188. CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
  2189. CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
  2190. ZSTD_freeCDict(cdict);
  2191. }
  2192. DISPLAYLEVEL(3, "OK \n");
  2193. DISPLAYLEVEL(3, "test%3i : Loaded dictionary persists across reset session : ", testNb++);
  2194. {
  2195. size_t size1, size2;
  2196. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2197. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
  2198. size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2199. if (ZSTD_isError(size1)) goto _output_error;
  2200. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  2201. size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2202. if (ZSTD_isError(size2)) goto _output_error;
  2203. if (size1 != size2) goto _output_error;
  2204. }
  2205. DISPLAYLEVEL(3, "OK \n");
  2206. DISPLAYLEVEL(3, "test%3i : Loaded dictionary is cleared after resetting parameters : ", testNb++);
  2207. {
  2208. size_t size1, size2;
  2209. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2210. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
  2211. size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2212. if (ZSTD_isError(size1)) goto _output_error;
  2213. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2214. size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2215. if (ZSTD_isError(size2)) goto _output_error;
  2216. if (size1 == size2) goto _output_error;
  2217. }
  2218. DISPLAYLEVEL(3, "OK \n");
  2219. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2220. CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, dictBuffer, dictSize) );
  2221. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
  2222. CHECK_Z(cSize);
  2223. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with dictionary : ", testNb++);
  2224. {
  2225. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  2226. size_t ret;
  2227. /* We should fail to decompress without a dictionary. */
  2228. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2229. ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2230. if (!ZSTD_isError(ret)) goto _output_error;
  2231. /* We should succeed to decompress with the dictionary. */
  2232. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2233. CHECK_Z( ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictSize) );
  2234. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2235. /* The dictionary should persist across calls. */
  2236. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2237. /* When we reset the context the dictionary is cleared. */
  2238. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2239. ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2240. if (!ZSTD_isError(ret)) goto _output_error;
  2241. ZSTD_freeDCtx(dctx);
  2242. }
  2243. DISPLAYLEVEL(3, "OK \n");
  2244. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with ddict : ", testNb++);
  2245. {
  2246. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  2247. ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictSize);
  2248. size_t ret;
  2249. /* We should succeed to decompress with the ddict. */
  2250. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2251. CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddict) );
  2252. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2253. /* The ddict should persist across calls. */
  2254. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2255. /* When we reset the context the ddict is cleared. */
  2256. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2257. ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2258. if (!ZSTD_isError(ret)) goto _output_error;
  2259. ZSTD_freeDCtx(dctx);
  2260. ZSTD_freeDDict(ddict);
  2261. }
  2262. DISPLAYLEVEL(3, "OK \n");
  2263. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with prefix : ", testNb++);
  2264. {
  2265. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  2266. size_t ret;
  2267. /* We should succeed to decompress with the prefix. */
  2268. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2269. CHECK_Z( ZSTD_DCtx_refPrefix_advanced(dctx, dictBuffer, dictSize, ZSTD_dct_auto) );
  2270. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2271. /* The prefix should be cleared after the first compression. */
  2272. ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2273. if (!ZSTD_isError(ret)) goto _output_error;
  2274. ZSTD_freeDCtx(dctx);
  2275. }
  2276. DISPLAYLEVEL(3, "OK \n");
  2277. DISPLAYLEVEL(3, "test%3i : Dictionary with non-default repcodes : ", testNb++);
  2278. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  2279. dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
  2280. CNBuffer, samplesSizes, nbSamples);
  2281. if (ZDICT_isError(dictSize)) goto _output_error;
  2282. /* Set all the repcodes to non-default */
  2283. {
  2284. BYTE* dictPtr = (BYTE*)dictBuffer;
  2285. BYTE* dictLimit = dictPtr + dictSize - 12;
  2286. /* Find the repcodes */
  2287. while (dictPtr < dictLimit &&
  2288. (MEM_readLE32(dictPtr) != 1 || MEM_readLE32(dictPtr + 4) != 4 ||
  2289. MEM_readLE32(dictPtr + 8) != 8)) {
  2290. ++dictPtr;
  2291. }
  2292. if (dictPtr >= dictLimit) goto _output_error;
  2293. MEM_writeLE32(dictPtr + 0, 10);
  2294. MEM_writeLE32(dictPtr + 4, 10);
  2295. MEM_writeLE32(dictPtr + 8, 10);
  2296. /* Set the last 8 bytes to 'x' */
  2297. memset((BYTE*)dictBuffer + dictSize - 8, 'x', 8);
  2298. }
  2299. /* The optimal parser checks all the repcodes.
  2300. * Make sure at least one is a match >= targetLength so that it is
  2301. * immediately chosen. This will make sure that the compressor and
  2302. * decompressor agree on at least one of the repcodes.
  2303. */
  2304. { size_t dSize;
  2305. BYTE data[1024];
  2306. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2307. ZSTD_compressionParameters const cParams = ZSTD_getCParams(19, CNBuffSize, dictSize);
  2308. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
  2309. ZSTD_dlm_byRef, ZSTD_dct_auto,
  2310. cParams, ZSTD_defaultCMem);
  2311. assert(dctx != NULL); assert(cdict != NULL);
  2312. memset(data, 'x', sizeof(data));
  2313. cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize,
  2314. data, sizeof(data), cdict);
  2315. ZSTD_freeCDict(cdict);
  2316. if (ZSTD_isError(cSize)) { DISPLAYLEVEL(5, "Compression error %s : ", ZSTD_getErrorName(cSize)); goto _output_error; }
  2317. dSize = ZSTD_decompress_usingDict(dctx, decodedBuffer, sizeof(data), compressedBuffer, cSize, dictBuffer, dictSize);
  2318. if (ZSTD_isError(dSize)) { DISPLAYLEVEL(5, "Decompression error %s : ", ZSTD_getErrorName(dSize)); goto _output_error; }
  2319. if (memcmp(data, decodedBuffer, sizeof(data))) { DISPLAYLEVEL(5, "Data corruption : "); goto _output_error; }
  2320. ZSTD_freeDCtx(dctx);
  2321. }
  2322. DISPLAYLEVEL(3, "OK \n");
  2323. DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with multiple ddicts : ", testNb++);
  2324. {
  2325. const size_t numDicts = 128;
  2326. const size_t numFrames = 4;
  2327. size_t i;
  2328. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  2329. ZSTD_DDict** ddictTable = (ZSTD_DDict**)malloc(sizeof(ZSTD_DDict*)*numDicts);
  2330. ZSTD_CDict** cdictTable = (ZSTD_CDict**)malloc(sizeof(ZSTD_CDict*)*numDicts);
  2331. U32 dictIDSeed = seed;
  2332. /* Create new compressed buffer that will hold frames with differing dictIDs */
  2333. char* dictBufferMulti = (char*)malloc(sizeof(char) * dictBufferFixedSize); /* Modifiable copy of fixed full dict buffer */
  2334. ZSTD_memcpy(dictBufferMulti, dictBufferFixed, dictBufferFixedSize);
  2335. /* Create a bunch of DDicts with random dict IDs */
  2336. for (i = 0; i < numDicts; ++i) {
  2337. U32 currDictID = FUZ_rand(&dictIDSeed);
  2338. MEM_writeLE32(dictBufferMulti+ZSTD_FRAMEIDSIZE, currDictID);
  2339. ddictTable[i] = ZSTD_createDDict(dictBufferMulti, dictBufferFixedSize);
  2340. cdictTable[i] = ZSTD_createCDict(dictBufferMulti, dictBufferFixedSize, 3);
  2341. if (!ddictTable[i] || !cdictTable[i] || ZSTD_getDictID_fromCDict(cdictTable[i]) != ZSTD_getDictID_fromDDict(ddictTable[i])) {
  2342. goto _output_error;
  2343. }
  2344. }
  2345. /* Compress a few frames using random CDicts */
  2346. {
  2347. size_t off = 0;
  2348. /* only use the first half so we don't push against size limit of compressedBuffer */
  2349. size_t const segSize = (CNBuffSize / 2) / numFrames;
  2350. for (i = 0; i < numFrames; i++) {
  2351. size_t dictIdx = FUZ_rand(&dictIDSeed) % numDicts;
  2352. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2353. { CHECK_NEWV(r, ZSTD_compress_usingCDict(cctx,
  2354. (BYTE*)compressedBuffer + off, CNBuffSize - off,
  2355. (BYTE*)CNBuffer + segSize * (size_t)i, segSize,
  2356. cdictTable[dictIdx]));
  2357. off += r;
  2358. }
  2359. }
  2360. cSize = off;
  2361. }
  2362. /* We should succeed to decompression even though different dicts were used on different frames */
  2363. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2364. ZSTD_DCtx_setParameter(dctx, ZSTD_d_refMultipleDDicts, ZSTD_rmd_refMultipleDDicts);
  2365. /* Reference every single ddict we made */
  2366. for (i = 0; i < numDicts; ++i) {
  2367. CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddictTable[i]));
  2368. }
  2369. CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2370. /* Streaming decompression should also work */
  2371. {
  2372. ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
  2373. ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
  2374. while (in.pos < in.size) {
  2375. CHECK_Z(ZSTD_decompressStream(dctx, &out, &in));
  2376. }
  2377. }
  2378. ZSTD_freeDCtx(dctx);
  2379. for (i = 0; i < numDicts; ++i) {
  2380. ZSTD_freeCDict(cdictTable[i]);
  2381. ZSTD_freeDDict(ddictTable[i]);
  2382. }
  2383. free(dictBufferMulti);
  2384. free(ddictTable);
  2385. free(cdictTable);
  2386. }
  2387. DISPLAYLEVEL(3, "OK \n");
  2388. ZSTD_freeCCtx(cctx);
  2389. free(dictBuffer);
  2390. free(samplesSizes);
  2391. }
  2392. /* COVER dictionary builder tests */
  2393. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2394. size_t dictSize = 16 KB;
  2395. size_t optDictSize = dictSize;
  2396. void* dictBuffer = malloc(dictSize);
  2397. size_t const totalSampleSize = 1 MB;
  2398. size_t const sampleUnitSize = 8 KB;
  2399. U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
  2400. size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
  2401. U32 seed32 = seed;
  2402. ZDICT_cover_params_t params;
  2403. U32 dictID;
  2404. if (dictBuffer==NULL || samplesSizes==NULL) {
  2405. free(dictBuffer);
  2406. free(samplesSizes);
  2407. goto _output_error;
  2408. }
  2409. DISPLAYLEVEL(3, "test%3i : ZDICT_trainFromBuffer_cover : ", testNb++);
  2410. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  2411. memset(&params, 0, sizeof(params));
  2412. params.d = 1 + (FUZ_rand(&seed32) % 16);
  2413. params.k = params.d + (FUZ_rand(&seed32) % 256);
  2414. dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, dictSize,
  2415. CNBuffer, samplesSizes, nbSamples,
  2416. params);
  2417. if (ZDICT_isError(dictSize)) goto _output_error;
  2418. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
  2419. DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
  2420. dictID = ZDICT_getDictID(dictBuffer, dictSize);
  2421. if (dictID==0) goto _output_error;
  2422. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
  2423. DISPLAYLEVEL(3, "test%3i : ZDICT_optimizeTrainFromBuffer_cover : ", testNb++);
  2424. memset(&params, 0, sizeof(params));
  2425. params.steps = 4;
  2426. optDictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, optDictSize,
  2427. CNBuffer, samplesSizes,
  2428. nbSamples / 4, &params);
  2429. if (ZDICT_isError(optDictSize)) goto _output_error;
  2430. DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)optDictSize);
  2431. DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
  2432. dictID = ZDICT_getDictID(dictBuffer, optDictSize);
  2433. if (dictID==0) goto _output_error;
  2434. DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
  2435. ZSTD_freeCCtx(cctx);
  2436. free(dictBuffer);
  2437. free(samplesSizes);
  2438. }
  2439. /* Decompression defense tests */
  2440. DISPLAYLEVEL(3, "test%3i : Check input length for magic number : ", testNb++);
  2441. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); /* too small input */
  2442. if (!ZSTD_isError(r)) goto _output_error;
  2443. if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
  2444. DISPLAYLEVEL(3, "OK \n");
  2445. DISPLAYLEVEL(3, "test%3i : Check magic Number : ", testNb++);
  2446. ((char*)(CNBuffer))[0] = 1;
  2447. { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
  2448. if (!ZSTD_isError(r)) goto _output_error; }
  2449. DISPLAYLEVEL(3, "OK \n");
  2450. /* content size verification test */
  2451. DISPLAYLEVEL(3, "test%3i : Content size verification : ", testNb++);
  2452. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2453. size_t const srcSize = 5000;
  2454. size_t const wrongSrcSize = (srcSize + 1000);
  2455. ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0);
  2456. params.fParams.contentSizeFlag = 1;
  2457. CHECK( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize) );
  2458. { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize);
  2459. if (!ZSTD_isError(result)) goto _output_error;
  2460. if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error;
  2461. DISPLAYLEVEL(3, "OK : %s \n", ZSTD_getErrorName(result));
  2462. }
  2463. ZSTD_freeCCtx(cctx);
  2464. }
  2465. /* negative compression level test : ensure simple API and advanced API produce same result */
  2466. DISPLAYLEVEL(3, "test%3i : negative compression level : ", testNb++);
  2467. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2468. size_t const srcSize = CNBuffSize / 5;
  2469. int const compressionLevel = -1;
  2470. assert(cctx != NULL);
  2471. { size_t const cSize_1pass = ZSTD_compress(compressedBuffer, compressedBufferSize,
  2472. CNBuffer, srcSize, compressionLevel);
  2473. if (ZSTD_isError(cSize_1pass)) goto _output_error;
  2474. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
  2475. { size_t const compressionResult = ZSTD_compress2(cctx,
  2476. compressedBuffer, compressedBufferSize,
  2477. CNBuffer, srcSize);
  2478. DISPLAYLEVEL(5, "simple=%zu vs %zu=advanced : ", cSize_1pass, compressionResult);
  2479. if (ZSTD_isError(compressionResult)) goto _output_error;
  2480. if (compressionResult != cSize_1pass) goto _output_error;
  2481. } }
  2482. ZSTD_freeCCtx(cctx);
  2483. }
  2484. DISPLAYLEVEL(3, "OK \n");
  2485. /* parameters order test */
  2486. { size_t const inputSize = CNBuffSize / 2;
  2487. U64 xxh64;
  2488. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2489. DISPLAYLEVEL(3, "test%3i : parameters in order : ", testNb++);
  2490. assert(cctx != NULL);
  2491. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 2) );
  2492. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1) );
  2493. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 18) );
  2494. { size_t const compressedSize = ZSTD_compress2(cctx,
  2495. compressedBuffer, ZSTD_compressBound(inputSize),
  2496. CNBuffer, inputSize);
  2497. CHECK(compressedSize);
  2498. cSize = compressedSize;
  2499. xxh64 = XXH64(compressedBuffer, compressedSize, 0);
  2500. }
  2501. DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)cSize);
  2502. ZSTD_freeCCtx(cctx);
  2503. }
  2504. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  2505. DISPLAYLEVEL(3, "test%3i : parameters disordered : ", testNb++);
  2506. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 18) );
  2507. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1) );
  2508. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 2) );
  2509. { size_t const result = ZSTD_compress2(cctx,
  2510. compressedBuffer, ZSTD_compressBound(inputSize),
  2511. CNBuffer, inputSize);
  2512. CHECK(result);
  2513. if (result != cSize) goto _output_error; /* must result in same compressed result, hence same size */
  2514. if (XXH64(compressedBuffer, result, 0) != xxh64) goto _output_error; /* must result in exactly same content, hence same hash */
  2515. DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)result);
  2516. }
  2517. ZSTD_freeCCtx(cctx);
  2518. }
  2519. }
  2520. /* advanced parameters for decompression */
  2521. { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2522. assert(dctx != NULL);
  2523. DISPLAYLEVEL(3, "test%3i : get dParameter bounds ", testNb++);
  2524. { ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
  2525. CHECK(bounds.error);
  2526. }
  2527. DISPLAYLEVEL(3, "OK \n");
  2528. DISPLAYLEVEL(3, "test%3i : wrong dParameter : ", testNb++);
  2529. { size_t const sr = ZSTD_DCtx_setParameter(dctx, (ZSTD_dParameter)999999, 0);
  2530. if (!ZSTD_isError(sr)) goto _output_error;
  2531. }
  2532. { ZSTD_bounds const bounds = ZSTD_dParam_getBounds((ZSTD_dParameter)999998);
  2533. if (!ZSTD_isError(bounds.error)) goto _output_error;
  2534. }
  2535. DISPLAYLEVEL(3, "OK \n");
  2536. DISPLAYLEVEL(3, "test%3i : out of bound dParameter : ", testNb++);
  2537. { size_t const sr = ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 9999);
  2538. if (!ZSTD_isError(sr)) goto _output_error;
  2539. }
  2540. { size_t const sr = ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (ZSTD_format_e)888);
  2541. if (!ZSTD_isError(sr)) goto _output_error;
  2542. }
  2543. DISPLAYLEVEL(3, "OK \n");
  2544. ZSTD_freeDCtx(dctx);
  2545. }
  2546. /* custom formats tests */
  2547. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2548. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2549. size_t const inputSize = CNBuffSize / 2; /* won't cause pb with small dict size */
  2550. assert(dctx != NULL); assert(cctx != NULL);
  2551. /* basic block compression */
  2552. DISPLAYLEVEL(3, "test%3i : magic-less format test : ", testNb++);
  2553. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
  2554. { ZSTD_inBuffer in = { CNBuffer, inputSize, 0 };
  2555. ZSTD_outBuffer out = { compressedBuffer, ZSTD_compressBound(inputSize), 0 };
  2556. size_t const result = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  2557. if (result != 0) goto _output_error;
  2558. if (in.pos != in.size) goto _output_error;
  2559. cSize = out.pos;
  2560. }
  2561. DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)cSize);
  2562. DISPLAYLEVEL(3, "test%3i : decompress normally (should fail) : ", testNb++);
  2563. { size_t const decodeResult = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2564. if (ZSTD_getErrorCode(decodeResult) != ZSTD_error_prefix_unknown) goto _output_error;
  2565. DISPLAYLEVEL(3, "OK : %s \n", ZSTD_getErrorName(decodeResult));
  2566. }
  2567. DISPLAYLEVEL(3, "test%3i : decompress of magic-less frame : ", testNb++);
  2568. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2569. CHECK( ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless) );
  2570. { ZSTD_frameHeader zfh;
  2571. size_t const zfhrt = ZSTD_getFrameHeader_advanced(&zfh, compressedBuffer, cSize, ZSTD_f_zstd1_magicless);
  2572. if (zfhrt != 0) goto _output_error;
  2573. }
  2574. /* one shot */
  2575. { size_t const result = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2576. if (result != inputSize) goto _output_error;
  2577. DISPLAYLEVEL(3, "one-shot OK, ");
  2578. }
  2579. /* streaming */
  2580. { ZSTD_inBuffer in = { compressedBuffer, cSize, 0 };
  2581. ZSTD_outBuffer out = { decodedBuffer, CNBuffSize, 0 };
  2582. size_t const result = ZSTD_decompressStream(dctx, &out, &in);
  2583. if (result != 0) goto _output_error;
  2584. if (in.pos != in.size) goto _output_error;
  2585. if (out.pos != inputSize) goto _output_error;
  2586. DISPLAYLEVEL(3, "streaming OK : regenerated %u bytes \n", (unsigned)out.pos);
  2587. }
  2588. /* basic block compression */
  2589. DISPLAYLEVEL(3, "test%3i : empty magic-less format test : ", testNb++);
  2590. CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
  2591. { ZSTD_inBuffer in = { CNBuffer, 0, 0 };
  2592. ZSTD_outBuffer out = { compressedBuffer, ZSTD_compressBound(0), 0 };
  2593. size_t const result = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  2594. if (result != 0) goto _output_error;
  2595. if (in.pos != in.size) goto _output_error;
  2596. cSize = out.pos;
  2597. }
  2598. DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)0, (unsigned)cSize);
  2599. DISPLAYLEVEL(3, "test%3i : decompress of empty magic-less frame : ", testNb++);
  2600. ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
  2601. CHECK( ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless) );
  2602. /* one shot */
  2603. { size_t const result = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
  2604. if (result != 0) goto _output_error;
  2605. DISPLAYLEVEL(3, "one-shot OK, ");
  2606. }
  2607. /* streaming */
  2608. { ZSTD_inBuffer in = { compressedBuffer, cSize, 0 };
  2609. ZSTD_outBuffer out = { decodedBuffer, CNBuffSize, 0 };
  2610. size_t const result = ZSTD_decompressStream(dctx, &out, &in);
  2611. if (result != 0) goto _output_error;
  2612. if (in.pos != in.size) goto _output_error;
  2613. if (out.pos != 0) goto _output_error;
  2614. DISPLAYLEVEL(3, "streaming OK : regenerated %u bytes \n", (unsigned)out.pos);
  2615. }
  2616. ZSTD_freeCCtx(cctx);
  2617. ZSTD_freeDCtx(dctx);
  2618. }
  2619. DISPLAYLEVEL(3, "test%3i : Decompression parameter reset test : ", testNb++);
  2620. {
  2621. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2622. /* Attempt to future proof this to new parameters. */
  2623. int const maxParam = 2000;
  2624. int param;
  2625. if (ZSTD_d_experimentalParam3 > maxParam) goto _output_error;
  2626. for (param = 0; param < maxParam; ++param) {
  2627. ZSTD_dParameter dParam = (ZSTD_dParameter)param;
  2628. ZSTD_bounds bounds = ZSTD_dParam_getBounds(dParam);
  2629. int value1;
  2630. int value2;
  2631. int check;
  2632. if (ZSTD_isError(bounds.error))
  2633. continue;
  2634. CHECK(ZSTD_DCtx_getParameter(dctx, dParam, &value1));
  2635. value2 = (value1 != bounds.lowerBound) ? bounds.lowerBound : bounds.upperBound;
  2636. CHECK(ZSTD_DCtx_setParameter(dctx, dParam, value2));
  2637. CHECK(ZSTD_DCtx_getParameter(dctx, dParam, &check));
  2638. if (check != value2) goto _output_error;
  2639. CHECK(ZSTD_DCtx_reset(dctx, ZSTD_reset_parameters));
  2640. CHECK(ZSTD_DCtx_getParameter(dctx, dParam, &check));
  2641. if (check != value1) goto _output_error;
  2642. }
  2643. ZSTD_freeDCtx(dctx);
  2644. }
  2645. DISPLAYLEVEL(3, "OK \n");
  2646. /* block API tests */
  2647. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2648. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  2649. static const size_t dictSize = 65 KB;
  2650. static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */
  2651. size_t cSize2;
  2652. assert(cctx != NULL); assert(dctx != NULL);
  2653. /* basic block compression */
  2654. DISPLAYLEVEL(3, "test%3i : Block compression test : ", testNb++);
  2655. CHECK( ZSTD_compressBegin(cctx, 5) );
  2656. CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
  2657. CHECK_VAR(cSize, ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize) );
  2658. DISPLAYLEVEL(3, "OK \n");
  2659. DISPLAYLEVEL(3, "test%3i : Block decompression test : ", testNb++);
  2660. CHECK( ZSTD_decompressBegin(dctx) );
  2661. { CHECK_NEWV(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
  2662. if (r != blockSize) goto _output_error; }
  2663. DISPLAYLEVEL(3, "OK \n");
  2664. /* very long stream of block compression */
  2665. DISPLAYLEVEL(3, "test%3i : Huge block streaming compression test : ", testNb++);
  2666. CHECK( ZSTD_compressBegin(cctx, -199) ); /* we just want to quickly overflow internal U32 index */
  2667. CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
  2668. { U64 const toCompress = 5000000000ULL; /* > 4 GB */
  2669. U64 compressed = 0;
  2670. while (compressed < toCompress) {
  2671. size_t const blockCSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
  2672. assert(blockCSize != 0);
  2673. if (ZSTD_isError(blockCSize)) goto _output_error;
  2674. compressed += blockCSize;
  2675. } }
  2676. DISPLAYLEVEL(3, "OK \n");
  2677. /* dictionary block compression */
  2678. DISPLAYLEVEL(3, "test%3i : Dictionary Block compression test : ", testNb++);
  2679. CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
  2680. CHECK_VAR(cSize, ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize));
  2681. RDG_genBuffer((char*)CNBuffer+dictSize+blockSize, blockSize, 0.0, 0.0, seed); /* create a non-compressible second block */
  2682. { CHECK_NEWV(r, ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize) ); /* for cctx history consistency */
  2683. assert(r == 0); /* non-compressible block */ }
  2684. memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* send non-compressed block (without header) */
  2685. CHECK_VAR(cSize2, ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
  2686. (char*)CNBuffer+dictSize+2*blockSize, blockSize));
  2687. DISPLAYLEVEL(3, "OK \n");
  2688. DISPLAYLEVEL(3, "test%3i : Dictionary Block decompression test : ", testNb++);
  2689. CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
  2690. { CHECK_NEWV( r, ZSTD_decompressBlock(dctx, decodedBuffer, blockSize, compressedBuffer, cSize) );
  2691. if (r != blockSize) {
  2692. DISPLAYLEVEL(1, "ZSTD_decompressBlock() with _usingDict() fails : %u, instead of %u expected \n", (unsigned)r, (unsigned)blockSize);
  2693. goto _output_error;
  2694. } }
  2695. memcpy((char*)decodedBuffer+blockSize, (char*)compressedBuffer+cSize, blockSize);
  2696. ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */
  2697. { CHECK_NEWV( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, blockSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
  2698. if (r != blockSize) {
  2699. DISPLAYLEVEL(1, "ZSTD_decompressBlock() with _usingDict() and after insertBlock() fails : %u, instead of %u expected \n", (unsigned)r, (unsigned)blockSize);
  2700. goto _output_error;
  2701. } }
  2702. assert(memcpy((char*)CNBuffer+dictSize, decodedBuffer, blockSize*3)); /* ensure regenerated content is identical to origin */
  2703. DISPLAYLEVEL(3, "OK \n");
  2704. DISPLAYLEVEL(3, "test%3i : Block compression with CDict : ", testNb++);
  2705. { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, dictSize, 3);
  2706. if (cdict==NULL) goto _output_error;
  2707. CHECK( ZSTD_compressBegin_usingCDict(cctx, cdict) );
  2708. CHECK( ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize) );
  2709. ZSTD_freeCDict(cdict);
  2710. }
  2711. DISPLAYLEVEL(3, "OK \n");
  2712. ZSTD_freeCCtx(cctx);
  2713. ZSTD_freeDCtx(dctx);
  2714. }
  2715. /* long rle test */
  2716. { size_t sampleSize = 0;
  2717. size_t expectedCompressedSize = 39; /* block 1, 2: compressed, block 3: RLE, zstd 1.4.4 */
  2718. DISPLAYLEVEL(3, "test%3i : Long RLE test : ", testNb++);
  2719. memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
  2720. sampleSize += 256 KB - 1;
  2721. memset((char*)CNBuffer+sampleSize, 'A', 96 KB);
  2722. sampleSize += 96 KB;
  2723. cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
  2724. if (ZSTD_isError(cSize) || cSize > expectedCompressedSize) goto _output_error;
  2725. { CHECK_NEWV(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
  2726. if (regenSize!=sampleSize) goto _output_error; }
  2727. DISPLAYLEVEL(3, "OK \n");
  2728. }
  2729. DISPLAYLEVEL(3, "test%3i : ZSTD_generateSequences decode from sequences test : ", testNb++);
  2730. {
  2731. size_t srcSize = 150 KB;
  2732. BYTE* src = (BYTE*)CNBuffer;
  2733. BYTE* decoded = (BYTE*)compressedBuffer;
  2734. ZSTD_CCtx* cctx = ZSTD_createCCtx();
  2735. ZSTD_Sequence* seqs = (ZSTD_Sequence*)malloc(srcSize * sizeof(ZSTD_Sequence));
  2736. size_t seqsSize;
  2737. if (seqs == NULL) goto _output_error;
  2738. assert(cctx != NULL);
  2739. ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19);
  2740. /* Populate src with random data */
  2741. RDG_genBuffer(CNBuffer, srcSize, compressibility, 0.5, seed);
  2742. /* Test with block delimiters roundtrip */
  2743. seqsSize = ZSTD_generateSequences(cctx, seqs, srcSize, src, srcSize);
  2744. FUZ_decodeSequences(decoded, seqs, seqsSize, src, srcSize, ZSTD_sf_explicitBlockDelimiters);
  2745. assert(!memcmp(CNBuffer, compressedBuffer, srcSize));
  2746. /* Test no block delimiters roundtrip */
  2747. seqsSize = ZSTD_mergeBlockDelimiters(seqs, seqsSize);
  2748. FUZ_decodeSequences(decoded, seqs, seqsSize, src, srcSize, ZSTD_sf_noBlockDelimiters);
  2749. assert(!memcmp(CNBuffer, compressedBuffer, srcSize));
  2750. ZSTD_freeCCtx(cctx);
  2751. free(seqs);
  2752. }
  2753. DISPLAYLEVEL(3, "OK \n");
  2754. DISPLAYLEVEL(3, "test%3i : ZSTD_getSequences followed by ZSTD_compressSequences : ", testNb++);
  2755. {
  2756. size_t srcSize = 500 KB;
  2757. BYTE* src = (BYTE*)CNBuffer;
  2758. BYTE* dst = (BYTE*)compressedBuffer;
  2759. size_t dstSize = ZSTD_compressBound(srcSize);
  2760. size_t decompressSize = srcSize;
  2761. char* decompressBuffer = (char*)malloc(decompressSize);
  2762. size_t compressedSize;
  2763. size_t dSize;
  2764. ZSTD_CCtx* cctx = ZSTD_createCCtx();
  2765. ZSTD_Sequence* seqs = (ZSTD_Sequence*)malloc(srcSize * sizeof(ZSTD_Sequence));
  2766. size_t seqsSize;
  2767. if (seqs == NULL) goto _output_error;
  2768. assert(cctx != NULL);
  2769. /* Populate src with random data */
  2770. RDG_genBuffer(CNBuffer, srcSize, compressibility, 0., seed);
  2771. /* Test with block delimiters roundtrip */
  2772. seqsSize = ZSTD_generateSequences(cctx, seqs, srcSize, src, srcSize);
  2773. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2774. ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_explicitBlockDelimiters);
  2775. compressedSize = ZSTD_compressSequences(cctx, dst, dstSize, seqs, seqsSize, src, srcSize);
  2776. if (ZSTD_isError(compressedSize)) {
  2777. DISPLAY("Error in sequence compression with block delims\n");
  2778. goto _output_error;
  2779. }
  2780. dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
  2781. if (ZSTD_isError(dSize)) {
  2782. DISPLAY("Error in sequence compression roundtrip with block delims\n");
  2783. goto _output_error;
  2784. }
  2785. assert(!memcmp(decompressBuffer, src, srcSize));
  2786. /* Test with no block delimiters roundtrip */
  2787. seqsSize = ZSTD_mergeBlockDelimiters(seqs, seqsSize);
  2788. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  2789. ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_noBlockDelimiters);
  2790. compressedSize = ZSTD_compressSequences(cctx, dst, dstSize, seqs, seqsSize, src, srcSize);
  2791. if (ZSTD_isError(compressedSize)) {
  2792. DISPLAY("Error in sequence compression with no block delims\n");
  2793. goto _output_error;
  2794. }
  2795. dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
  2796. if (ZSTD_isError(dSize)) {
  2797. DISPLAY("Error in sequence compression roundtrip with no block delims\n");
  2798. goto _output_error;
  2799. }
  2800. assert(!memcmp(decompressBuffer, src, srcSize));
  2801. ZSTD_freeCCtx(cctx);
  2802. free(decompressBuffer);
  2803. free(seqs);
  2804. }
  2805. DISPLAYLEVEL(3, "OK \n");
  2806. /* Multiple blocks of zeros test */
  2807. #define LONGZEROSLENGTH 1000000 /* 1MB of zeros */
  2808. DISPLAYLEVEL(3, "test%3i : compress %u zeroes : ", testNb++, LONGZEROSLENGTH);
  2809. memset(CNBuffer, 0, LONGZEROSLENGTH);
  2810. CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(LONGZEROSLENGTH), CNBuffer, LONGZEROSLENGTH, 1) );
  2811. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/LONGZEROSLENGTH*100);
  2812. DISPLAYLEVEL(3, "test%3i : decompress %u zeroes : ", testNb++, LONGZEROSLENGTH);
  2813. { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, LONGZEROSLENGTH, compressedBuffer, cSize) );
  2814. if (r != LONGZEROSLENGTH) goto _output_error; }
  2815. DISPLAYLEVEL(3, "OK \n");
  2816. /* All zeroes test (test bug #137) */
  2817. #define ZEROESLENGTH 100
  2818. DISPLAYLEVEL(3, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
  2819. memset(CNBuffer, 0, ZEROESLENGTH);
  2820. CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
  2821. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/ZEROESLENGTH*100);
  2822. DISPLAYLEVEL(3, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
  2823. { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
  2824. if (r != ZEROESLENGTH) goto _output_error; }
  2825. DISPLAYLEVEL(3, "OK \n");
  2826. /* nbSeq limit test */
  2827. #define _3BYTESTESTLENGTH 131000
  2828. #define NB3BYTESSEQLOG 9
  2829. #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
  2830. #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
  2831. /* creates a buffer full of 3-bytes sequences */
  2832. { BYTE _3BytesSeqs[NB3BYTESSEQ][3];
  2833. U32 rSeed = 1;
  2834. /* create batch of 3-bytes sequences */
  2835. { int i;
  2836. for (i=0; i < NB3BYTESSEQ; i++) {
  2837. _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
  2838. _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
  2839. _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
  2840. } }
  2841. /* randomly fills CNBuffer with prepared 3-bytes sequences */
  2842. { int i;
  2843. for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
  2844. U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
  2845. ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
  2846. ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
  2847. ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
  2848. } } }
  2849. DISPLAYLEVEL(3, "test%3i : growing nbSeq : ", testNb++);
  2850. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2851. size_t const maxNbSeq = _3BYTESTESTLENGTH / 3;
  2852. size_t const bound = ZSTD_compressBound(_3BYTESTESTLENGTH);
  2853. size_t nbSeq = 1;
  2854. while (nbSeq <= maxNbSeq) {
  2855. CHECK(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, nbSeq * 3, 19));
  2856. /* Check every sequence for the first 100, then skip more rapidly. */
  2857. if (nbSeq < 100) {
  2858. ++nbSeq;
  2859. } else {
  2860. nbSeq += (nbSeq >> 2);
  2861. }
  2862. }
  2863. ZSTD_freeCCtx(cctx);
  2864. }
  2865. DISPLAYLEVEL(3, "OK \n");
  2866. DISPLAYLEVEL(3, "test%3i : compress lots 3-bytes sequences : ", testNb++);
  2867. CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
  2868. CNBuffer, _3BYTESTESTLENGTH, 19) );
  2869. DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
  2870. DISPLAYLEVEL(3, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
  2871. { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
  2872. if (r != _3BYTESTESTLENGTH) goto _output_error; }
  2873. DISPLAYLEVEL(3, "OK \n");
  2874. DISPLAYLEVEL(3, "test%3i : growing literals buffer : ", testNb++);
  2875. RDG_genBuffer(CNBuffer, CNBuffSize, 0.0, 0.1, seed);
  2876. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2877. size_t const bound = ZSTD_compressBound(CNBuffSize);
  2878. size_t size = 1;
  2879. while (size <= CNBuffSize) {
  2880. CHECK(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, size, 3));
  2881. /* Check every size for the first 100, then skip more rapidly. */
  2882. if (size < 100) {
  2883. ++size;
  2884. } else {
  2885. size += (size >> 2);
  2886. }
  2887. }
  2888. ZSTD_freeCCtx(cctx);
  2889. }
  2890. DISPLAYLEVEL(3, "OK \n");
  2891. DISPLAYLEVEL(3, "test%3i : incompressible data and ill suited dictionary : ", testNb++);
  2892. { /* Train a dictionary on low characters */
  2893. size_t dictSize = 16 KB;
  2894. void* const dictBuffer = malloc(dictSize);
  2895. size_t const totalSampleSize = 1 MB;
  2896. size_t const sampleUnitSize = 8 KB;
  2897. U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
  2898. size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
  2899. if (!dictBuffer || !samplesSizes) goto _output_error;
  2900. { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
  2901. dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, CNBuffer, samplesSizes, nbSamples);
  2902. if (ZDICT_isError(dictSize)) goto _output_error;
  2903. /* Reverse the characters to make the dictionary ill suited */
  2904. { U32 u;
  2905. for (u = 0; u < CNBuffSize; ++u) {
  2906. ((BYTE*)CNBuffer)[u] = 255 - ((BYTE*)CNBuffer)[u];
  2907. }
  2908. }
  2909. { /* Compress the data */
  2910. size_t const inputSize = 500;
  2911. size_t const outputSize = ZSTD_compressBound(inputSize);
  2912. void* const outputBuffer = malloc(outputSize);
  2913. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2914. if (!outputBuffer || !cctx) goto _output_error;
  2915. CHECK(ZSTD_compress_usingDict(cctx, outputBuffer, outputSize, CNBuffer, inputSize, dictBuffer, dictSize, 1));
  2916. free(outputBuffer);
  2917. ZSTD_freeCCtx(cctx);
  2918. }
  2919. free(dictBuffer);
  2920. free(samplesSizes);
  2921. }
  2922. DISPLAYLEVEL(3, "OK \n");
  2923. /* findFrameCompressedSize on skippable frames */
  2924. DISPLAYLEVEL(3, "test%3i : frame compressed size of skippable frame : ", testNb++);
  2925. { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
  2926. size_t const frameSrcSize = 13;
  2927. if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
  2928. DISPLAYLEVEL(3, "OK \n");
  2929. /* error string tests */
  2930. DISPLAYLEVEL(3, "test%3i : testing ZSTD error code strings : ", testNb++);
  2931. if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
  2932. if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
  2933. if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
  2934. if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
  2935. if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
  2936. if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
  2937. DISPLAYLEVEL(3, "OK \n");
  2938. DISPLAYLEVEL(3, "test%3i : testing ZSTD dictionary sizes : ", testNb++);
  2939. RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
  2940. {
  2941. size_t const size = MIN(128 KB, CNBuffSize);
  2942. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  2943. ZSTD_CDict* const lgCDict = ZSTD_createCDict(CNBuffer, size, 1);
  2944. ZSTD_CDict* const smCDict = ZSTD_createCDict(CNBuffer, 1 KB, 1);
  2945. ZSTD_frameHeader lgHeader;
  2946. ZSTD_frameHeader smHeader;
  2947. CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, lgCDict));
  2948. CHECK_Z(ZSTD_getFrameHeader(&lgHeader, compressedBuffer, compressedBufferSize));
  2949. CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, smCDict));
  2950. CHECK_Z(ZSTD_getFrameHeader(&smHeader, compressedBuffer, compressedBufferSize));
  2951. if (lgHeader.windowSize != smHeader.windowSize) goto _output_error;
  2952. ZSTD_freeCDict(smCDict);
  2953. ZSTD_freeCDict(lgCDict);
  2954. ZSTD_freeCCtx(cctx);
  2955. }
  2956. DISPLAYLEVEL(3, "OK \n");
  2957. DISPLAYLEVEL(3, "test%3i : testing FSE_normalizeCount() PR#1255: ", testNb++);
  2958. {
  2959. short norm[32];
  2960. unsigned count[32];
  2961. unsigned const tableLog = 5;
  2962. size_t const nbSeq = 32;
  2963. unsigned const maxSymbolValue = 31;
  2964. size_t i;
  2965. for (i = 0; i < 32; ++i)
  2966. count[i] = 1;
  2967. /* Calling FSE_normalizeCount() on a uniform distribution should not
  2968. * cause a division by zero.
  2969. */
  2970. FSE_normalizeCount(norm, tableLog, count, nbSeq, maxSymbolValue, /* useLowProbCount */ 1);
  2971. }
  2972. DISPLAYLEVEL(3, "OK \n");
  2973. DISPLAYLEVEL(3, "test%3i : testing FSE_writeNCount() PR#2779: ", testNb++);
  2974. {
  2975. size_t const outBufSize = 9;
  2976. short const count[11] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 9, 18};
  2977. unsigned const tableLog = 5;
  2978. unsigned const maxSymbolValue = 10;
  2979. BYTE* outBuf = (BYTE*)malloc(outBufSize*sizeof(BYTE));
  2980. /* Ensure that this write doesn't write out of bounds, and that
  2981. * FSE_writeNCount_generic() is *not* called with writeIsSafe == 1.
  2982. */
  2983. FSE_writeNCount(outBuf, outBufSize, count, maxSymbolValue, tableLog);
  2984. free(outBuf);
  2985. }
  2986. DISPLAYLEVEL(3, "OK \n");
  2987. #ifdef ZSTD_MULTITHREAD
  2988. DISPLAYLEVEL(3, "test%3i : passing wrong full dict should fail on compressStream2 refPrefix ", testNb++);
  2989. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  2990. size_t const srcSize = 1 MB + 5; /* A little more than ZSTDMT_JOBSIZE_MIN */
  2991. size_t const dstSize = ZSTD_compressBound(srcSize);
  2992. void* const src = CNBuffer;
  2993. void* const dst = compressedBuffer;
  2994. void* dict = (void*)malloc(srcSize);
  2995. RDG_genBuffer(src, srcSize, compressibility, 0.5, seed);
  2996. RDG_genBuffer(dict, srcSize, compressibility, 0., seed);
  2997. /* Make sure there is no ZSTD_MAGIC_NUMBER */
  2998. memset(dict, 0, sizeof(U32));
  2999. /* something more than 1 */
  3000. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
  3001. /* lie and claim this is a full dict */
  3002. CHECK_Z(ZSTD_CCtx_refPrefix_advanced(cctx, dict, srcSize, ZSTD_dct_fullDict));
  3003. { ZSTD_outBuffer out = {dst, dstSize, 0};
  3004. ZSTD_inBuffer in = {src, srcSize, 0};
  3005. /* should fail because its not a full dict like we said it was */
  3006. assert(ZSTD_isError(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush)));
  3007. }
  3008. ZSTD_freeCCtx(cctx);
  3009. free(dict);
  3010. }
  3011. DISPLAYLEVEL(3, "OK \n");
  3012. DISPLAYLEVEL(3, "test%3i : small dictionary with multithreading and LDM ", testNb++);
  3013. { ZSTD_CCtx* cctx = ZSTD_createCCtx();
  3014. size_t const srcSize = 1 MB + 5; /* A little more than ZSTDMT_JOBSIZE_MIN */
  3015. size_t const dictSize = 10;
  3016. size_t const dstSize = ZSTD_compressBound(srcSize);
  3017. void* const src = CNBuffer;
  3018. void* const dst = compressedBuffer;
  3019. void* dict = (void*)malloc(dictSize);
  3020. RDG_genBuffer(src, srcSize, compressibility, 0.5, seed);
  3021. RDG_genBuffer(dict, dictSize, compressibility, 0., seed);
  3022. /* Make sure there is no ZSTD_MAGIC_NUMBER */
  3023. memset(dict, 0, sizeof(U32));
  3024. /* Enable MT, LDM, and use refPrefix() for a small dict */
  3025. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
  3026. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  3027. CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
  3028. CHECK_Z(ZSTD_compress2(cctx, dst, dstSize, src, srcSize));
  3029. ZSTD_freeCCtx(cctx);
  3030. free(dict);
  3031. }
  3032. DISPLAYLEVEL(3, "OK \n");
  3033. DISPLAYLEVEL(3, "test%3i : ZSTD_getCParams() + dictionary ", testNb++);
  3034. {
  3035. ZSTD_compressionParameters const medium = ZSTD_getCParams(1, 16*1024-1, 0);
  3036. ZSTD_compressionParameters const large = ZSTD_getCParams(1, 128*1024-1, 0);
  3037. ZSTD_compressionParameters const smallDict = ZSTD_getCParams(1, 0, 400);
  3038. ZSTD_compressionParameters const mediumDict = ZSTD_getCParams(1, 0, 10000);
  3039. ZSTD_compressionParameters const largeDict = ZSTD_getCParams(1, 0, 100000);
  3040. assert(!memcmp(&smallDict, &mediumDict, sizeof(smallDict)));
  3041. assert(!memcmp(&medium, &mediumDict, sizeof(medium)));
  3042. assert(!memcmp(&large, &largeDict, sizeof(large)));
  3043. }
  3044. DISPLAYLEVEL(3, "OK \n");
  3045. DISPLAYLEVEL(3, "test%3i : ZSTD_adjustCParams() + dictionary ", testNb++);
  3046. {
  3047. ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, 0, 0);
  3048. ZSTD_compressionParameters const smallDict = ZSTD_adjustCParams(cParams, 0, 400);
  3049. ZSTD_compressionParameters const smallSrcAndDict = ZSTD_adjustCParams(cParams, 500, 400);
  3050. assert(smallSrcAndDict.windowLog == 10);
  3051. assert(!memcmp(&cParams, &smallDict, sizeof(cParams)));
  3052. }
  3053. DISPLAYLEVEL(3, "OK \n");
  3054. DISPLAYLEVEL(3, "test%3i : check compression mem usage monotonicity over levels for estimateCCtxSize() : ", testNb++);
  3055. {
  3056. int level = 1;
  3057. size_t prevSize = 0;
  3058. for (; level < ZSTD_maxCLevel(); ++level) {
  3059. size_t const currSize = ZSTD_estimateCCtxSize(level);
  3060. if (prevSize > currSize) {
  3061. DISPLAYLEVEL(3, "Error! previous cctx size: %zu at level: %d is larger than current cctx size: %zu at level: %d",
  3062. prevSize, level-1, currSize, level);
  3063. goto _output_error;
  3064. }
  3065. prevSize = currSize;
  3066. }
  3067. }
  3068. DISPLAYLEVEL(3, "OK \n");
  3069. DISPLAYLEVEL(3, "test%3i : check estimateCCtxSize() always larger or equal to ZSTD_estimateCCtxSize_usingCParams() : ", testNb++);
  3070. {
  3071. size_t const kSizeIncrement = 2 KB;
  3072. int level = -3;
  3073. for (; level <= ZSTD_maxCLevel(); ++level) {
  3074. size_t dictSize = 0;
  3075. for (; dictSize <= 256 KB; dictSize += 8 * kSizeIncrement) {
  3076. size_t srcSize = 2 KB;
  3077. for (; srcSize < 300 KB; srcSize += kSizeIncrement) {
  3078. ZSTD_compressionParameters const cParams = ZSTD_getCParams(level, srcSize, dictSize);
  3079. size_t const cctxSizeUsingCParams = ZSTD_estimateCCtxSize_usingCParams(cParams);
  3080. size_t const cctxSizeUsingLevel = ZSTD_estimateCCtxSize(level);
  3081. if (cctxSizeUsingLevel < cctxSizeUsingCParams
  3082. || ZSTD_isError(cctxSizeUsingCParams)
  3083. || ZSTD_isError(cctxSizeUsingLevel)) {
  3084. DISPLAYLEVEL(3, "error! l: %d dict: %zu srcSize: %zu cctx size cpar: %zu, cctx size level: %zu\n",
  3085. level, dictSize, srcSize, cctxSizeUsingCParams, cctxSizeUsingLevel);
  3086. goto _output_error;
  3087. } } } } }
  3088. DISPLAYLEVEL(3, "OK \n");
  3089. DISPLAYLEVEL(3, "test%3i : thread pool API tests : \n", testNb++)
  3090. {
  3091. int const threadPoolTestResult = threadPoolTests();
  3092. if (threadPoolTestResult) {
  3093. goto _output_error;
  3094. }
  3095. }
  3096. DISPLAYLEVEL(3, "thread pool tests OK \n");
  3097. #endif /* ZSTD_MULTITHREAD */
  3098. _end:
  3099. free(CNBuffer);
  3100. free(compressedBuffer);
  3101. free(decodedBuffer);
  3102. return testResult;
  3103. _output_error:
  3104. testResult = 1;
  3105. DISPLAY("Error detected in Unit tests ! \n");
  3106. goto _end;
  3107. }
  3108. static int longUnitTests(U32 const seed, double compressibility)
  3109. {
  3110. size_t const CNBuffSize = 5 MB;
  3111. void* const CNBuffer = malloc(CNBuffSize);
  3112. size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
  3113. void* const compressedBuffer = malloc(compressedBufferSize);
  3114. void* const decodedBuffer = malloc(CNBuffSize);
  3115. int testResult = 0;
  3116. unsigned testNb=0;
  3117. size_t cSize;
  3118. /* Create compressible noise */
  3119. if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
  3120. DISPLAY("Not enough memory, aborting\n");
  3121. testResult = 1;
  3122. goto _end;
  3123. }
  3124. RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
  3125. /* note : this test is rather long, it would be great to find a way to speed up its execution */
  3126. DISPLAYLEVEL(3, "longtest%3i : table cleanliness through index reduction : ", testNb++);
  3127. { int cLevel;
  3128. size_t approxIndex = 0;
  3129. size_t maxIndex = ((3U << 29) + (1U << ZSTD_WINDOWLOG_MAX)); /* ZSTD_CURRENT_MAX from zstd_compress_internal.h */
  3130. /* Provision enough space in a static context so that we can do all
  3131. * this without ever reallocating, which would reset the indices. */
  3132. size_t const staticCCtxSize = ZSTD_estimateCStreamSize(22);
  3133. void* const staticCCtxBuffer = malloc(staticCCtxSize);
  3134. ZSTD_CCtx* const cctx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize);
  3135. /* bump the indices so the following compressions happen at high
  3136. * indices. */
  3137. { ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize, 0 };
  3138. ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
  3139. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  3140. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, -500));
  3141. while (approxIndex <= (maxIndex / 4) * 3) {
  3142. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  3143. approxIndex += in.pos;
  3144. CHECK(in.pos == in.size);
  3145. in.pos = 0;
  3146. out.pos = 0;
  3147. }
  3148. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  3149. }
  3150. /* spew a bunch of stuff into the table area */
  3151. for (cLevel = 1; cLevel <= 22; cLevel++) {
  3152. ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize / (unsigned)cLevel, 0 };
  3153. ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
  3154. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  3155. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel));
  3156. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  3157. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  3158. approxIndex += in.pos;
  3159. }
  3160. /* now crank the indices so we overflow */
  3161. { ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize, 0 };
  3162. ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
  3163. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  3164. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, -500));
  3165. while (approxIndex <= maxIndex) {
  3166. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  3167. approxIndex += in.pos;
  3168. CHECK(in.pos == in.size);
  3169. in.pos = 0;
  3170. out.pos = 0;
  3171. }
  3172. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  3173. }
  3174. /* do a bunch of compressions again in low indices and ensure we don't
  3175. * hit untracked invalid indices */
  3176. for (cLevel = 1; cLevel <= 22; cLevel++) {
  3177. ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize / (unsigned)cLevel, 0 };
  3178. ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
  3179. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  3180. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel));
  3181. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
  3182. CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
  3183. approxIndex += in.pos;
  3184. }
  3185. free(staticCCtxBuffer);
  3186. }
  3187. DISPLAYLEVEL(3, "OK \n");
  3188. DISPLAYLEVEL(3, "longtest%3i : testing ldm no regressions in size for opt parser : ", testNb++);
  3189. { size_t cSizeLdm;
  3190. size_t cSizeNoLdm;
  3191. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  3192. RDG_genBuffer(CNBuffer, CNBuffSize, 0.5, 0.5, seed);
  3193. /* Enable checksum to verify round trip. */
  3194. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  3195. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1));
  3196. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
  3197. /* Round trip once with ldm. */
  3198. cSizeLdm = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  3199. CHECK_Z(cSizeLdm);
  3200. CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSizeLdm));
  3201. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
  3202. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
  3203. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 0));
  3204. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
  3205. /* Round trip once without ldm. */
  3206. cSizeNoLdm = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  3207. CHECK_Z(cSizeNoLdm);
  3208. CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSizeNoLdm));
  3209. if (cSizeLdm > cSizeNoLdm) {
  3210. DISPLAY("Using long mode should not cause regressions for btopt+\n");
  3211. testResult = 1;
  3212. goto _end;
  3213. }
  3214. ZSTD_freeCCtx(cctx);
  3215. }
  3216. DISPLAYLEVEL(3, "OK \n");
  3217. DISPLAYLEVEL(3, "longtest%3i : testing cdict compression with different attachment strategies : ", testNb++);
  3218. { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  3219. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  3220. size_t dictSize = CNBuffSize;
  3221. void* dict = (void*)malloc(dictSize);
  3222. ZSTD_CCtx_params* cctx_params = ZSTD_createCCtxParams();
  3223. ZSTD_dictAttachPref_e const attachPrefs[] = {
  3224. ZSTD_dictDefaultAttach,
  3225. ZSTD_dictForceAttach,
  3226. ZSTD_dictForceCopy,
  3227. ZSTD_dictForceLoad,
  3228. ZSTD_dictDefaultAttach,
  3229. ZSTD_dictForceAttach,
  3230. ZSTD_dictForceCopy,
  3231. ZSTD_dictForceLoad
  3232. };
  3233. int const enableDedicatedDictSearch[] = {0, 0, 0, 0, 1, 1, 1, 1};
  3234. int cLevel;
  3235. int i;
  3236. RDG_genBuffer(dict, dictSize, 0.5, 0.5, seed);
  3237. RDG_genBuffer(CNBuffer, CNBuffSize, 0.6, 0.6, seed);
  3238. CHECK(cctx_params != NULL);
  3239. for (dictSize = CNBuffSize; dictSize; dictSize = dictSize >> 3) {
  3240. DISPLAYLEVEL(3, "\n Testing with dictSize %u ", (U32)dictSize);
  3241. for (cLevel = 4; cLevel < 13; cLevel++) {
  3242. for (i = 0; i < 8; ++i) {
  3243. ZSTD_dictAttachPref_e const attachPref = attachPrefs[i];
  3244. int const enableDDS = enableDedicatedDictSearch[i];
  3245. ZSTD_CDict* cdict;
  3246. DISPLAYLEVEL(5, "\n dictSize %u cLevel %d iter %d ", (U32)dictSize, cLevel, i);
  3247. ZSTD_CCtxParams_init(cctx_params, cLevel);
  3248. CHECK_Z(ZSTD_CCtxParams_setParameter(cctx_params, ZSTD_c_enableDedicatedDictSearch, enableDDS));
  3249. cdict = ZSTD_createCDict_advanced2(dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cctx_params, ZSTD_defaultCMem);
  3250. CHECK(cdict != NULL);
  3251. CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
  3252. CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, (int)attachPref));
  3253. cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
  3254. CHECK_Z(cSize);
  3255. CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
  3256. DISPLAYLEVEL(5, "compressed to %u bytes ", (U32)cSize);
  3257. CHECK_Z(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters));
  3258. ZSTD_freeCDict(cdict);
  3259. } } }
  3260. ZSTD_freeCCtx(cctx);
  3261. ZSTD_freeDCtx(dctx);
  3262. ZSTD_freeCCtxParams(cctx_params);
  3263. free(dict);
  3264. }
  3265. DISPLAYLEVEL(3, "OK \n");
  3266. _end:
  3267. free(CNBuffer);
  3268. free(compressedBuffer);
  3269. free(decodedBuffer);
  3270. return testResult;
  3271. _output_error:
  3272. testResult = 1;
  3273. DISPLAY("Error detected in Unit tests ! \n");
  3274. goto _end;
  3275. }
  3276. static size_t findDiff(const void* buf1, const void* buf2, size_t max)
  3277. {
  3278. const BYTE* b1 = (const BYTE*)buf1;
  3279. const BYTE* b2 = (const BYTE*)buf2;
  3280. size_t u;
  3281. for (u=0; u<max; u++) {
  3282. if (b1[u] != b2[u]) break;
  3283. }
  3284. return u;
  3285. }
  3286. static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams)
  3287. {
  3288. ZSTD_parameters params;
  3289. params.cParams = cParams;
  3290. params.fParams = fParams;
  3291. return params;
  3292. }
  3293. static size_t FUZ_rLogLength(U32* seed, U32 logLength)
  3294. {
  3295. size_t const lengthMask = ((size_t)1 << logLength) - 1;
  3296. return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
  3297. }
  3298. static size_t FUZ_randomLength(U32* seed, U32 maxLog)
  3299. {
  3300. U32 const logLength = FUZ_rand(seed) % maxLog;
  3301. return FUZ_rLogLength(seed, logLength);
  3302. }
  3303. #undef CHECK
  3304. #define CHECK(cond, ...) { \
  3305. if (cond) { \
  3306. DISPLAY("Error => "); \
  3307. DISPLAY(__VA_ARGS__); \
  3308. DISPLAY(" (seed %u, test nb %u) \n", (unsigned)seed, testNb); \
  3309. goto _output_error; \
  3310. } }
  3311. #undef CHECK_Z
  3312. #define CHECK_Z(f) { \
  3313. size_t const err = f; \
  3314. if (ZSTD_isError(err)) { \
  3315. DISPLAY("Error => %s : %s ", \
  3316. #f, ZSTD_getErrorName(err)); \
  3317. DISPLAY(" (seed %u, test nb %u) \n", (unsigned)seed, testNb); \
  3318. goto _output_error; \
  3319. } }
  3320. static int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests)
  3321. {
  3322. static const U32 maxSrcLog = 23;
  3323. static const U32 maxSampleLog = 22;
  3324. size_t const srcBufferSize = (size_t)1<<maxSrcLog;
  3325. size_t const dstBufferSize = (size_t)1<<maxSampleLog;
  3326. size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
  3327. BYTE* cNoiseBuffer[5];
  3328. BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
  3329. BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
  3330. BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
  3331. ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
  3332. ZSTD_CCtx* const ctx = ZSTD_createCCtx();
  3333. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  3334. U32 result = 0;
  3335. unsigned testNb = 0;
  3336. U32 coreSeed = seed;
  3337. UTIL_time_t const startClock = UTIL_getTime();
  3338. U64 const maxClockSpan = maxDurationS * SEC_TO_MICRO;
  3339. int const cLevelLimiter = bigTests ? 3 : 2;
  3340. /* allocation */
  3341. cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
  3342. cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
  3343. cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
  3344. cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
  3345. cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
  3346. CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
  3347. || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
  3348. "Not enough memory, fuzzer tests cancelled");
  3349. /* Create initial samples */
  3350. RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
  3351. RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
  3352. RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
  3353. RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
  3354. RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
  3355. /* catch up testNb */
  3356. for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
  3357. /* main test loop */
  3358. for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < maxClockSpan); testNb++ ) {
  3359. BYTE* srcBuffer; /* jumping pointer */
  3360. U32 lseed;
  3361. size_t sampleSize, maxTestSize, totalTestSize;
  3362. size_t cSize, totalCSize, totalGenSize;
  3363. U64 crcOrig;
  3364. BYTE* sampleBuffer;
  3365. const BYTE* dict;
  3366. size_t dictSize;
  3367. /* notification */
  3368. if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
  3369. else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
  3370. FUZ_rand(&coreSeed);
  3371. { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
  3372. /* srcBuffer selection [0-4] */
  3373. { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
  3374. if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
  3375. else {
  3376. buffNb >>= 3;
  3377. if (buffNb & 7) {
  3378. const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
  3379. buffNb = tnb[buffNb >> 3];
  3380. } else {
  3381. const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
  3382. buffNb = tnb[buffNb >> 3];
  3383. } }
  3384. srcBuffer = cNoiseBuffer[buffNb];
  3385. }
  3386. /* select src segment */
  3387. sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
  3388. /* create sample buffer (to catch read error with valgrind & sanitizers) */
  3389. sampleBuffer = (BYTE*)malloc(sampleSize);
  3390. CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
  3391. { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
  3392. memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
  3393. crcOrig = XXH64(sampleBuffer, sampleSize, 0);
  3394. /* compression tests */
  3395. { int const cLevelPositive = (int)
  3396. ( FUZ_rand(&lseed) %
  3397. ((U32)ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / (U32)cLevelLimiter)) )
  3398. + 1;
  3399. int const cLevel = ((FUZ_rand(&lseed) & 15) == 3) ?
  3400. - (int)((FUZ_rand(&lseed) & 7) + 1) : /* test negative cLevel */
  3401. cLevelPositive;
  3402. DISPLAYLEVEL(5, "fuzzer t%u: Simple compression test (level %i) \n", testNb, cLevel);
  3403. cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
  3404. CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
  3405. /* compression failure test : too small dest buffer */
  3406. assert(cSize > 3);
  3407. { const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1;
  3408. const size_t tooSmallSize = cSize - missing;
  3409. const unsigned endMark = 0x4DC2B1A9;
  3410. memcpy(dstBuffer+tooSmallSize, &endMark, sizeof(endMark));
  3411. DISPLAYLEVEL(5, "fuzzer t%u: compress into too small buffer of size %u (missing %u bytes) \n",
  3412. testNb, (unsigned)tooSmallSize, (unsigned)missing);
  3413. { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
  3414. CHECK(ZSTD_getErrorCode(errorCode) != ZSTD_error_dstSize_tooSmall, "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (unsigned)tooSmallSize, (unsigned)cSize); }
  3415. { unsigned endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, sizeof(endCheck));
  3416. CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow (check.%08X != %08X.mark)", endCheck, endMark); }
  3417. } }
  3418. /* frame header decompression test */
  3419. { ZSTD_frameHeader zfh;
  3420. CHECK_Z( ZSTD_getFrameHeader(&zfh, cBuffer, cSize) );
  3421. CHECK(zfh.frameContentSize != sampleSize, "Frame content size incorrect");
  3422. }
  3423. /* Decompressed size test */
  3424. { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
  3425. CHECK(rSize != sampleSize, "decompressed size incorrect");
  3426. }
  3427. /* successful decompression test */
  3428. DISPLAYLEVEL(5, "fuzzer t%u: simple decompression test \n", testNb);
  3429. { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
  3430. size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
  3431. CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (unsigned)sampleSize, (unsigned)cSize);
  3432. { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
  3433. CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (unsigned)findDiff(sampleBuffer, dstBuffer, sampleSize), (unsigned)sampleSize);
  3434. } }
  3435. free(sampleBuffer); /* no longer useful after this point */
  3436. /* truncated src decompression test */
  3437. DISPLAYLEVEL(5, "fuzzer t%u: decompression of truncated source \n", testNb);
  3438. { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
  3439. size_t const tooSmallSize = cSize - missing;
  3440. void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */
  3441. CHECK(cBufferTooSmall == NULL, "not enough memory !");
  3442. memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
  3443. { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
  3444. CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
  3445. free(cBufferTooSmall);
  3446. }
  3447. /* too small dst decompression test */
  3448. DISPLAYLEVEL(5, "fuzzer t%u: decompress into too small dst buffer \n", testNb);
  3449. if (sampleSize > 3) {
  3450. size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
  3451. size_t const tooSmallSize = sampleSize - missing;
  3452. static const BYTE token = 0xA9;
  3453. dstBuffer[tooSmallSize] = token;
  3454. { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
  3455. CHECK(ZSTD_getErrorCode(errorCode) != ZSTD_error_dstSize_tooSmall, "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (unsigned)errorCode, (unsigned)tooSmallSize); }
  3456. CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
  3457. }
  3458. /* noisy src decompression test */
  3459. if (cSize > 6) {
  3460. /* insert noise into src */
  3461. { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
  3462. size_t pos = 4; /* preserve magic number (too easy to detect) */
  3463. for (;;) {
  3464. /* keep some original src */
  3465. { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
  3466. size_t const mask = (1<<nbBits) - 1;
  3467. size_t const skipLength = FUZ_rand(&lseed) & mask;
  3468. pos += skipLength;
  3469. }
  3470. if (pos >= cSize) break;
  3471. /* add noise */
  3472. { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
  3473. U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
  3474. size_t const mask = (1<<nbBits) - 1;
  3475. size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
  3476. size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
  3477. size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
  3478. memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
  3479. pos += noiseLength;
  3480. } } }
  3481. /* decompress noisy source */
  3482. DISPLAYLEVEL(5, "fuzzer t%u: decompress noisy source \n", testNb);
  3483. { U32 const endMark = 0xA9B1C3D6;
  3484. memcpy(dstBuffer+sampleSize, &endMark, 4);
  3485. { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
  3486. /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
  3487. CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
  3488. "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (unsigned)decompressResult, (unsigned)sampleSize);
  3489. }
  3490. { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
  3491. CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
  3492. } } } /* noisy src decompression test */
  3493. /*===== Bufferless streaming compression test, scattered segments and dictionary =====*/
  3494. DISPLAYLEVEL(5, "fuzzer t%u: Bufferless streaming compression test \n", testNb);
  3495. { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
  3496. U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
  3497. int const cLevel = (FUZ_rand(&lseed) %
  3498. (ZSTD_maxCLevel() -
  3499. (MAX(testLog, dictLog) / cLevelLimiter))) +
  3500. 1;
  3501. maxTestSize = FUZ_rLogLength(&lseed, testLog);
  3502. if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
  3503. dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */
  3504. dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
  3505. DISPLAYLEVEL(6, "fuzzer t%u: Compressing up to <=%u bytes at level %i with dictionary size %u \n",
  3506. testNb, (unsigned)maxTestSize, cLevel, (unsigned)dictSize);
  3507. if (FUZ_rand(&lseed) & 0xF) {
  3508. CHECK_Z ( ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel) );
  3509. } else {
  3510. ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, ZSTD_CONTENTSIZE_UNKNOWN, dictSize);
  3511. ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
  3512. !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
  3513. 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */
  3514. ZSTD_parameters const p = FUZ_makeParams(cPar, fPar);
  3515. CHECK_Z ( ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0) );
  3516. }
  3517. CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) );
  3518. }
  3519. { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
  3520. U32 n;
  3521. XXH64_state_t xxhState;
  3522. XXH64_reset(&xxhState, 0);
  3523. for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
  3524. size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
  3525. size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
  3526. if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */
  3527. if (totalTestSize+segmentSize > maxTestSize) break;
  3528. { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
  3529. CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
  3530. cSize += compressResult;
  3531. }
  3532. XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
  3533. memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
  3534. totalTestSize += segmentSize;
  3535. }
  3536. { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
  3537. CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
  3538. cSize += flushResult;
  3539. }
  3540. crcOrig = XXH64_digest(&xxhState);
  3541. }
  3542. /* streaming decompression test */
  3543. DISPLAYLEVEL(5, "fuzzer t%u: Bufferless streaming decompression test \n", testNb);
  3544. /* ensure memory requirement is good enough (should always be true) */
  3545. { ZSTD_frameHeader zfh;
  3546. CHECK( ZSTD_getFrameHeader(&zfh, cBuffer, ZSTD_FRAMEHEADERSIZE_MAX),
  3547. "ZSTD_getFrameHeader(): error retrieving frame information");
  3548. { size_t const roundBuffSize = ZSTD_decodingBufferSize_min(zfh.windowSize, zfh.frameContentSize);
  3549. CHECK_Z(roundBuffSize);
  3550. CHECK((roundBuffSize > totalTestSize) && (zfh.frameContentSize!=ZSTD_CONTENTSIZE_UNKNOWN),
  3551. "ZSTD_decodingBufferSize_min() requires more memory (%u) than necessary (%u)",
  3552. (unsigned)roundBuffSize, (unsigned)totalTestSize );
  3553. } }
  3554. if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */
  3555. CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, dict, dictSize) );
  3556. totalCSize = 0;
  3557. totalGenSize = 0;
  3558. while (totalCSize < cSize) {
  3559. size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
  3560. size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
  3561. CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
  3562. totalGenSize += genSize;
  3563. totalCSize += inSize;
  3564. }
  3565. CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
  3566. CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
  3567. CHECK (totalCSize != cSize, "compressed data should be fully read")
  3568. { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
  3569. CHECK(crcOrig != crcDest, "streaming decompressed data corrupted (pos %u / %u)",
  3570. (unsigned)findDiff(mirrorBuffer, dstBuffer, totalTestSize), (unsigned)totalTestSize);
  3571. }
  3572. } /* for ( ; (testNb <= nbTests) */
  3573. DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
  3574. _cleanup:
  3575. ZSTD_freeCCtx(refCtx);
  3576. ZSTD_freeCCtx(ctx);
  3577. ZSTD_freeDCtx(dctx);
  3578. free(cNoiseBuffer[0]);
  3579. free(cNoiseBuffer[1]);
  3580. free(cNoiseBuffer[2]);
  3581. free(cNoiseBuffer[3]);
  3582. free(cNoiseBuffer[4]);
  3583. free(cBuffer);
  3584. free(dstBuffer);
  3585. free(mirrorBuffer);
  3586. return result;
  3587. _output_error:
  3588. result = 1;
  3589. goto _cleanup;
  3590. }
  3591. /*_*******************************************************
  3592. * Command line
  3593. *********************************************************/
  3594. static int FUZ_usage(const char* programName)
  3595. {
  3596. DISPLAY( "Usage :\n");
  3597. DISPLAY( " %s [args]\n", programName);
  3598. DISPLAY( "\n");
  3599. DISPLAY( "Arguments :\n");
  3600. DISPLAY( " -i# : Number of tests (default:%i)\n", nbTestsDefault);
  3601. DISPLAY( " -T# : Max duration to run for. Overrides number of tests. (e.g. -T1m or -T60s for one minute)\n");
  3602. DISPLAY( " -s# : Select seed (default:prompt user)\n");
  3603. DISPLAY( " -t# : Select starting test number (default:0)\n");
  3604. DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_compressibility_default);
  3605. DISPLAY( " -v : verbose\n");
  3606. DISPLAY( " -p : pause at the end\n");
  3607. DISPLAY( " -h : display help and exit\n");
  3608. return 0;
  3609. }
  3610. /*! readU32FromChar() :
  3611. @return : unsigned integer value read from input in `char` format
  3612. allows and interprets K, KB, KiB, M, MB and MiB suffix.
  3613. Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  3614. Note : function result can overflow if digit string > MAX_UINT */
  3615. static unsigned readU32FromChar(const char** stringPtr)
  3616. {
  3617. unsigned result = 0;
  3618. while ((**stringPtr >='0') && (**stringPtr <='9'))
  3619. result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
  3620. if ((**stringPtr=='K') || (**stringPtr=='M')) {
  3621. result <<= 10;
  3622. if (**stringPtr=='M') result <<= 10;
  3623. (*stringPtr)++ ;
  3624. if (**stringPtr=='i') (*stringPtr)++;
  3625. if (**stringPtr=='B') (*stringPtr)++;
  3626. }
  3627. return result;
  3628. }
  3629. /** longCommandWArg() :
  3630. * check if *stringPtr is the same as longCommand.
  3631. * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
  3632. * @return 0 and doesn't modify *stringPtr otherwise.
  3633. */
  3634. static int longCommandWArg(const char** stringPtr, const char* longCommand)
  3635. {
  3636. size_t const comSize = strlen(longCommand);
  3637. int const result = !strncmp(*stringPtr, longCommand, comSize);
  3638. if (result) *stringPtr += comSize;
  3639. return result;
  3640. }
  3641. int main(int argc, const char** argv)
  3642. {
  3643. U32 seed = 0;
  3644. int seedset = 0;
  3645. int argNb;
  3646. int nbTests = nbTestsDefault;
  3647. int testNb = 0;
  3648. int proba = FUZ_compressibility_default;
  3649. double probfloat;
  3650. int result = 0;
  3651. U32 mainPause = 0;
  3652. U32 maxDuration = 0;
  3653. int bigTests = 1;
  3654. int longTests = 0;
  3655. U32 memTestsOnly = 0;
  3656. const char* const programName = argv[0];
  3657. /* Check command line */
  3658. for (argNb=1; argNb<argc; argNb++) {
  3659. const char* argument = argv[argNb];
  3660. if(!argument) continue; /* Protection if argument empty */
  3661. /* Handle commands. Aggregated commands are allowed */
  3662. if (argument[0]=='-') {
  3663. if (longCommandWArg(&argument, "--memtest=")) { memTestsOnly = readU32FromChar(&argument); continue; }
  3664. if (!strcmp(argument, "--memtest")) { memTestsOnly=1; continue; }
  3665. if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
  3666. if (!strcmp(argument, "--long-tests")) { longTests=1; continue; }
  3667. if (!strcmp(argument, "--no-long-tests")) { longTests=0; continue; }
  3668. argument++;
  3669. while (*argument!=0) {
  3670. switch(*argument)
  3671. {
  3672. case 'h':
  3673. return FUZ_usage(programName);
  3674. case 'v':
  3675. argument++;
  3676. g_displayLevel++;
  3677. break;
  3678. case 'q':
  3679. argument++;
  3680. g_displayLevel--;
  3681. break;
  3682. case 'p': /* pause at the end */
  3683. argument++;
  3684. mainPause = 1;
  3685. break;
  3686. case 'i':
  3687. argument++; maxDuration = 0;
  3688. nbTests = (int)readU32FromChar(&argument);
  3689. break;
  3690. case 'T':
  3691. argument++;
  3692. nbTests = 0;
  3693. maxDuration = readU32FromChar(&argument);
  3694. if (*argument=='s') argument++; /* seconds */
  3695. if (*argument=='m') maxDuration *= 60, argument++; /* minutes */
  3696. if (*argument=='n') argument++;
  3697. break;
  3698. case 's':
  3699. argument++;
  3700. seedset = 1;
  3701. seed = readU32FromChar(&argument);
  3702. break;
  3703. case 't':
  3704. argument++;
  3705. testNb = (int)readU32FromChar(&argument);
  3706. break;
  3707. case 'P': /* compressibility % */
  3708. argument++;
  3709. proba = (int)readU32FromChar(&argument);
  3710. if (proba>100) proba = 100;
  3711. break;
  3712. default:
  3713. return (FUZ_usage(programName), 1);
  3714. } } } } /* for (argNb=1; argNb<argc; argNb++) */
  3715. /* Get Seed */
  3716. DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
  3717. if (!seedset) {
  3718. time_t const t = time(NULL);
  3719. U32 const h = XXH32(&t, sizeof(t), 1);
  3720. seed = h % 10000;
  3721. }
  3722. DISPLAY("Seed = %u\n", (unsigned)seed);
  3723. if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %i%%\n", proba);
  3724. probfloat = ((double)proba) / 100;
  3725. if (memTestsOnly) {
  3726. g_displayLevel = MAX(3, g_displayLevel);
  3727. return FUZ_mallocTests(seed, probfloat, memTestsOnly);
  3728. }
  3729. if (nbTests < testNb) nbTests = testNb;
  3730. if (testNb==0) {
  3731. result = basicUnitTests(0, probfloat); /* constant seed for predictability */
  3732. if (!result && longTests) {
  3733. result = longUnitTests(0, probfloat);
  3734. }
  3735. }
  3736. if (!result)
  3737. result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests);
  3738. if (mainPause) {
  3739. int unused;
  3740. DISPLAY("Press Enter \n");
  3741. unused = getchar();
  3742. (void)unused;
  3743. }
  3744. return result;
  3745. }