pngrutil.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683
  1. /* pngrutil.c - utilities to read a PNG file
  2. *
  3. * Last changed in libpng 1.6.35 [July 15, 2018]
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that are only called from within
  13. * libpng itself during the course of reading an image.
  14. */
  15. #include "pngpriv.h"
  16. #ifdef PNG_READ_SUPPORTED
  17. png_uint_32 PNGAPI
  18. png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
  19. {
  20. png_uint_32 uval = png_get_uint_32(buf);
  21. if (uval > PNG_UINT_31_MAX)
  22. png_error(png_ptr, "PNG unsigned integer out of range");
  23. return (uval);
  24. }
  25. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
  26. /* The following is a variation on the above for use with the fixed
  27. * point values used for gAMA and cHRM. Instead of png_error it
  28. * issues a warning and returns (-1) - an invalid value because both
  29. * gAMA and cHRM use *unsigned* integers for fixed point values.
  30. */
  31. #define PNG_FIXED_ERROR (-1)
  32. static png_fixed_point /* PRIVATE */
  33. png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
  34. {
  35. png_uint_32 uval = png_get_uint_32(buf);
  36. if (uval <= PNG_UINT_31_MAX)
  37. return (png_fixed_point)uval; /* known to be in range */
  38. /* The caller can turn off the warning by passing NULL. */
  39. if (png_ptr != NULL)
  40. png_warning(png_ptr, "PNG fixed point integer out of range");
  41. return PNG_FIXED_ERROR;
  42. }
  43. #endif
  44. #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
  45. /* NOTE: the read macros will obscure these definitions, so that if
  46. * PNG_USE_READ_MACROS is set the library will not use them internally,
  47. * but the APIs will still be available externally.
  48. *
  49. * The parentheses around "PNGAPI function_name" in the following three
  50. * functions are necessary because they allow the macros to co-exist with
  51. * these (unused but exported) functions.
  52. */
  53. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  54. png_uint_32 (PNGAPI
  55. png_get_uint_32)(png_const_bytep buf)
  56. {
  57. png_uint_32 uval =
  58. ((png_uint_32)(*(buf )) << 24) +
  59. ((png_uint_32)(*(buf + 1)) << 16) +
  60. ((png_uint_32)(*(buf + 2)) << 8) +
  61. ((png_uint_32)(*(buf + 3)) ) ;
  62. return uval;
  63. }
  64. /* Grab a signed 32-bit integer from a buffer in big-endian format. The
  65. * data is stored in the PNG file in two's complement format and there
  66. * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
  67. * the following code does a two's complement to native conversion.
  68. */
  69. png_int_32 (PNGAPI
  70. png_get_int_32)(png_const_bytep buf)
  71. {
  72. png_uint_32 uval = png_get_uint_32(buf);
  73. if ((uval & 0x80000000) == 0) /* non-negative */
  74. return (png_int_32)uval;
  75. uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
  76. if ((uval & 0x80000000) == 0) /* no overflow */
  77. return -(png_int_32)uval;
  78. /* The following has to be safe; this function only gets called on PNG data
  79. * and if we get here that data is invalid. 0 is the most safe value and
  80. * if not then an attacker would surely just generate a PNG with 0 instead.
  81. */
  82. return 0;
  83. }
  84. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  85. png_uint_16 (PNGAPI
  86. png_get_uint_16)(png_const_bytep buf)
  87. {
  88. /* ANSI-C requires an int value to accommodate at least 16 bits so this
  89. * works and allows the compiler not to worry about possible narrowing
  90. * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
  91. * than 16 bits either.)
  92. */
  93. unsigned int val =
  94. ((unsigned int)(*buf) << 8) +
  95. ((unsigned int)(*(buf + 1)));
  96. return (png_uint_16)val;
  97. }
  98. #endif /* READ_INT_FUNCTIONS */
  99. /* Read and check the PNG file signature */
  100. void /* PRIVATE */
  101. png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
  102. {
  103. size_t num_checked, num_to_check;
  104. /* Exit if the user application does not expect a signature. */
  105. if (png_ptr->sig_bytes >= 8)
  106. return;
  107. num_checked = png_ptr->sig_bytes;
  108. num_to_check = 8 - num_checked;
  109. #ifdef PNG_IO_STATE_SUPPORTED
  110. png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
  111. #endif
  112. /* The signature must be serialized in a single I/O call. */
  113. png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  114. png_ptr->sig_bytes = 8;
  115. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
  116. {
  117. if (num_checked < 4 &&
  118. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  119. png_error(png_ptr, "Not a PNG file");
  120. else
  121. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  122. }
  123. if (num_checked < 3)
  124. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  125. }
  126. /* Read the chunk header (length + type name).
  127. * Put the type name into png_ptr->chunk_name, and return the length.
  128. */
  129. png_uint_32 /* PRIVATE */
  130. png_read_chunk_header(png_structrp png_ptr)
  131. {
  132. png_byte buf[8];
  133. png_uint_32 length;
  134. #ifdef PNG_IO_STATE_SUPPORTED
  135. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
  136. #endif
  137. /* Read the length and the chunk name.
  138. * This must be performed in a single I/O call.
  139. */
  140. png_read_data(png_ptr, buf, 8);
  141. length = png_get_uint_31(png_ptr, buf);
  142. /* Put the chunk name into png_ptr->chunk_name. */
  143. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
  144. png_debug2(0, "Reading %lx chunk, length = %lu",
  145. (unsigned long)png_ptr->chunk_name, (unsigned long)length);
  146. /* Reset the crc and run it over the chunk name. */
  147. png_reset_crc(png_ptr);
  148. png_calculate_crc(png_ptr, buf + 4, 4);
  149. /* Check to see if chunk name is valid. */
  150. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  151. /* Check for too-large chunk length */
  152. png_check_chunk_length(png_ptr, length);
  153. #ifdef PNG_IO_STATE_SUPPORTED
  154. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
  155. #endif
  156. return length;
  157. }
  158. /* Read data, and (optionally) run it through the CRC. */
  159. void /* PRIVATE */
  160. png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
  161. {
  162. if (png_ptr == NULL)
  163. return;
  164. png_read_data(png_ptr, buf, length);
  165. png_calculate_crc(png_ptr, buf, length);
  166. }
  167. /* Optionally skip data and then check the CRC. Depending on whether we
  168. * are reading an ancillary or critical chunk, and how the program has set
  169. * things up, we may calculate the CRC on the data and print a message.
  170. * Returns '1' if there was a CRC error, '0' otherwise.
  171. */
  172. int /* PRIVATE */
  173. png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
  174. {
  175. /* The size of the local buffer for inflate is a good guess as to a
  176. * reasonable size to use for buffering reads from the application.
  177. */
  178. while (skip > 0)
  179. {
  180. png_uint_32 len;
  181. png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
  182. len = (sizeof tmpbuf);
  183. if (len > skip)
  184. len = skip;
  185. skip -= len;
  186. png_crc_read(png_ptr, tmpbuf, len);
  187. }
  188. if (png_crc_error(png_ptr) != 0)
  189. {
  190. if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
  191. (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
  192. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
  193. {
  194. png_chunk_warning(png_ptr, "CRC error");
  195. }
  196. else
  197. png_chunk_error(png_ptr, "CRC error");
  198. return (1);
  199. }
  200. return (0);
  201. }
  202. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  203. * the data it has read thus far.
  204. */
  205. int /* PRIVATE */
  206. png_crc_error(png_structrp png_ptr)
  207. {
  208. png_byte crc_bytes[4];
  209. png_uint_32 crc;
  210. int need_crc = 1;
  211. if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
  212. {
  213. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  214. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  215. need_crc = 0;
  216. }
  217. else /* critical */
  218. {
  219. if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
  220. need_crc = 0;
  221. }
  222. #ifdef PNG_IO_STATE_SUPPORTED
  223. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
  224. #endif
  225. /* The chunk CRC must be serialized in a single I/O call. */
  226. png_read_data(png_ptr, crc_bytes, 4);
  227. if (need_crc != 0)
  228. {
  229. crc = png_get_uint_32(crc_bytes);
  230. return ((int)(crc != png_ptr->crc));
  231. }
  232. else
  233. return (0);
  234. }
  235. #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
  236. defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
  237. defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
  238. defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
  239. /* Manage the read buffer; this simply reallocates the buffer if it is not small
  240. * enough (or if it is not allocated). The routine returns a pointer to the
  241. * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
  242. * it will call png_error (via png_malloc) on failure. (warn == 2 means
  243. * 'silent').
  244. */
  245. static png_bytep
  246. png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
  247. {
  248. png_bytep buffer = png_ptr->read_buffer;
  249. if (buffer != NULL && new_size > png_ptr->read_buffer_size)
  250. {
  251. png_ptr->read_buffer = NULL;
  252. png_ptr->read_buffer = NULL;
  253. png_ptr->read_buffer_size = 0;
  254. png_free(png_ptr, buffer);
  255. buffer = NULL;
  256. }
  257. if (buffer == NULL)
  258. {
  259. buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
  260. if (buffer != NULL)
  261. {
  262. memset(buffer, 0, new_size); /* just in case */
  263. png_ptr->read_buffer = buffer;
  264. png_ptr->read_buffer_size = new_size;
  265. }
  266. else if (warn < 2) /* else silent */
  267. {
  268. if (warn != 0)
  269. png_chunk_warning(png_ptr, "insufficient memory to read chunk");
  270. else
  271. png_chunk_error(png_ptr, "insufficient memory to read chunk");
  272. }
  273. }
  274. return buffer;
  275. }
  276. #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
  277. /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
  278. * decompression. Returns Z_OK on success, else a zlib error code. It checks
  279. * the owner but, in final release builds, just issues a warning if some other
  280. * chunk apparently owns the stream. Prior to release it does a png_error.
  281. */
  282. static int
  283. png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
  284. {
  285. if (png_ptr->zowner != 0)
  286. {
  287. char msg[64];
  288. PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
  289. /* So the message that results is "<chunk> using zstream"; this is an
  290. * internal error, but is very useful for debugging. i18n requirements
  291. * are minimal.
  292. */
  293. (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
  294. #if PNG_RELEASE_BUILD
  295. png_chunk_warning(png_ptr, msg);
  296. png_ptr->zowner = 0;
  297. #else
  298. png_chunk_error(png_ptr, msg);
  299. #endif
  300. }
  301. /* Implementation note: unlike 'png_deflate_claim' this internal function
  302. * does not take the size of the data as an argument. Some efficiency could
  303. * be gained by using this when it is known *if* the zlib stream itself does
  304. * not record the number; however, this is an illusion: the original writer
  305. * of the PNG may have selected a lower window size, and we really must
  306. * follow that because, for systems with with limited capabilities, we
  307. * would otherwise reject the application's attempts to use a smaller window
  308. * size (zlib doesn't have an interface to say "this or lower"!).
  309. *
  310. * inflateReset2 was added to zlib 1.2.4; before this the window could not be
  311. * reset, therefore it is necessary to always allocate the maximum window
  312. * size with earlier zlibs just in case later compressed chunks need it.
  313. */
  314. {
  315. int ret; /* zlib return code */
  316. #if ZLIB_VERNUM >= 0x1240
  317. int window_bits = 0;
  318. # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
  319. if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
  320. PNG_OPTION_ON)
  321. {
  322. window_bits = 15;
  323. png_ptr->zstream_start = 0; /* fixed window size */
  324. }
  325. else
  326. {
  327. png_ptr->zstream_start = 1;
  328. }
  329. # endif
  330. #endif /* ZLIB_VERNUM >= 0x1240 */
  331. /* Set this for safety, just in case the previous owner left pointers to
  332. * memory allocations.
  333. */
  334. png_ptr->zstream.next_in = NULL;
  335. png_ptr->zstream.avail_in = 0;
  336. png_ptr->zstream.next_out = NULL;
  337. png_ptr->zstream.avail_out = 0;
  338. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
  339. {
  340. #if ZLIB_VERNUM >= 0x1240
  341. ret = inflateReset2(&png_ptr->zstream, window_bits);
  342. #else
  343. ret = inflateReset(&png_ptr->zstream);
  344. #endif
  345. }
  346. else
  347. {
  348. #if ZLIB_VERNUM >= 0x1240
  349. ret = inflateInit2(&png_ptr->zstream, window_bits);
  350. #else
  351. ret = inflateInit(&png_ptr->zstream);
  352. #endif
  353. if (ret == Z_OK)
  354. png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
  355. }
  356. #if ZLIB_VERNUM >= 0x1290 && \
  357. defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
  358. if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
  359. /* Turn off validation of the ADLER32 checksum in IDAT chunks */
  360. ret = inflateValidate(&png_ptr->zstream, 0);
  361. #endif
  362. if (ret == Z_OK)
  363. png_ptr->zowner = owner;
  364. else
  365. png_zstream_error(png_ptr, ret);
  366. return ret;
  367. }
  368. #ifdef window_bits
  369. # undef window_bits
  370. #endif
  371. }
  372. #if ZLIB_VERNUM >= 0x1240
  373. /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
  374. * in this case some zlib versions skip validation of the CINFO field and, in
  375. * certain circumstances, libpng may end up displaying an invalid image, in
  376. * contrast to implementations that call zlib in the normal way (e.g. libpng
  377. * 1.5).
  378. */
  379. int /* PRIVATE */
  380. png_zlib_inflate(png_structrp png_ptr, int flush)
  381. {
  382. if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
  383. {
  384. if ((*png_ptr->zstream.next_in >> 4) > 7)
  385. {
  386. png_ptr->zstream.msg = "invalid window size (libpng)";
  387. return Z_DATA_ERROR;
  388. }
  389. png_ptr->zstream_start = 0;
  390. }
  391. return inflate(&png_ptr->zstream, flush);
  392. }
  393. #endif /* Zlib >= 1.2.4 */
  394. #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
  395. #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
  396. /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
  397. * allow the caller to do multiple calls if required. If the 'finish' flag is
  398. * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
  399. * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
  400. * Z_OK or Z_STREAM_END will be returned on success.
  401. *
  402. * The input and output sizes are updated to the actual amounts of data consumed
  403. * or written, not the amount available (as in a z_stream). The data pointers
  404. * are not changed, so the next input is (data+input_size) and the next
  405. * available output is (output+output_size).
  406. */
  407. static int
  408. png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
  409. /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
  410. /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
  411. {
  412. if (png_ptr->zowner == owner) /* Else not claimed */
  413. {
  414. int ret;
  415. png_alloc_size_t avail_out = *output_size_ptr;
  416. png_uint_32 avail_in = *input_size_ptr;
  417. /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
  418. * can't even necessarily handle 65536 bytes) because the type uInt is
  419. * "16 bits or more". Consequently it is necessary to chunk the input to
  420. * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
  421. * maximum value that can be stored in a uInt.) It is possible to set
  422. * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
  423. * a performance advantage, because it reduces the amount of data accessed
  424. * at each step and that may give the OS more time to page it in.
  425. */
  426. png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
  427. /* avail_in and avail_out are set below from 'size' */
  428. png_ptr->zstream.avail_in = 0;
  429. png_ptr->zstream.avail_out = 0;
  430. /* Read directly into the output if it is available (this is set to
  431. * a local buffer below if output is NULL).
  432. */
  433. if (output != NULL)
  434. png_ptr->zstream.next_out = output;
  435. do
  436. {
  437. uInt avail;
  438. Byte local_buffer[PNG_INFLATE_BUF_SIZE];
  439. /* zlib INPUT BUFFER */
  440. /* The setting of 'avail_in' used to be outside the loop; by setting it
  441. * inside it is possible to chunk the input to zlib and simply rely on
  442. * zlib to advance the 'next_in' pointer. This allows arbitrary
  443. * amounts of data to be passed through zlib at the unavoidable cost of
  444. * requiring a window save (memcpy of up to 32768 output bytes)
  445. * every ZLIB_IO_MAX input bytes.
  446. */
  447. avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
  448. avail = ZLIB_IO_MAX;
  449. if (avail_in < avail)
  450. avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
  451. avail_in -= avail;
  452. png_ptr->zstream.avail_in = avail;
  453. /* zlib OUTPUT BUFFER */
  454. avail_out += png_ptr->zstream.avail_out; /* not written last time */
  455. avail = ZLIB_IO_MAX; /* maximum zlib can process */
  456. if (output == NULL)
  457. {
  458. /* Reset the output buffer each time round if output is NULL and
  459. * make available the full buffer, up to 'remaining_space'
  460. */
  461. png_ptr->zstream.next_out = local_buffer;
  462. if ((sizeof local_buffer) < avail)
  463. avail = (sizeof local_buffer);
  464. }
  465. if (avail_out < avail)
  466. avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
  467. png_ptr->zstream.avail_out = avail;
  468. avail_out -= avail;
  469. /* zlib inflate call */
  470. /* In fact 'avail_out' may be 0 at this point, that happens at the end
  471. * of the read when the final LZ end code was not passed at the end of
  472. * the previous chunk of input data. Tell zlib if we have reached the
  473. * end of the output buffer.
  474. */
  475. ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
  476. (finish ? Z_FINISH : Z_SYNC_FLUSH));
  477. } while (ret == Z_OK);
  478. /* For safety kill the local buffer pointer now */
  479. if (output == NULL)
  480. png_ptr->zstream.next_out = NULL;
  481. /* Claw back the 'size' and 'remaining_space' byte counts. */
  482. avail_in += png_ptr->zstream.avail_in;
  483. avail_out += png_ptr->zstream.avail_out;
  484. /* Update the input and output sizes; the updated values are the amount
  485. * consumed or written, effectively the inverse of what zlib uses.
  486. */
  487. if (avail_out > 0)
  488. *output_size_ptr -= avail_out;
  489. if (avail_in > 0)
  490. *input_size_ptr -= avail_in;
  491. /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
  492. png_zstream_error(png_ptr, ret);
  493. return ret;
  494. }
  495. else
  496. {
  497. /* This is a bad internal error. The recovery assigns to the zstream msg
  498. * pointer, which is not owned by the caller, but this is safe; it's only
  499. * used on errors!
  500. */
  501. png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
  502. return Z_STREAM_ERROR;
  503. }
  504. }
  505. /*
  506. * Decompress trailing data in a chunk. The assumption is that read_buffer
  507. * points at an allocated area holding the contents of a chunk with a
  508. * trailing compressed part. What we get back is an allocated area
  509. * holding the original prefix part and an uncompressed version of the
  510. * trailing part (the malloc area passed in is freed).
  511. */
  512. static int
  513. png_decompress_chunk(png_structrp png_ptr,
  514. png_uint_32 chunklength, png_uint_32 prefix_size,
  515. png_alloc_size_t *newlength /* must be initialized to the maximum! */,
  516. int terminate /*add a '\0' to the end of the uncompressed data*/)
  517. {
  518. /* TODO: implement different limits for different types of chunk.
  519. *
  520. * The caller supplies *newlength set to the maximum length of the
  521. * uncompressed data, but this routine allocates space for the prefix and
  522. * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
  523. * limited only by the maximum chunk size.
  524. */
  525. png_alloc_size_t limit = PNG_SIZE_MAX;
  526. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  527. if (png_ptr->user_chunk_malloc_max > 0 &&
  528. png_ptr->user_chunk_malloc_max < limit)
  529. limit = png_ptr->user_chunk_malloc_max;
  530. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  531. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  532. limit = PNG_USER_CHUNK_MALLOC_MAX;
  533. # endif
  534. if (limit >= prefix_size + (terminate != 0))
  535. {
  536. int ret;
  537. limit -= prefix_size + (terminate != 0);
  538. if (limit < *newlength)
  539. *newlength = limit;
  540. /* Now try to claim the stream. */
  541. ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
  542. if (ret == Z_OK)
  543. {
  544. png_uint_32 lzsize = chunklength - prefix_size;
  545. ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
  546. /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
  547. /* output: */ NULL, newlength);
  548. if (ret == Z_STREAM_END)
  549. {
  550. /* Use 'inflateReset' here, not 'inflateReset2' because this
  551. * preserves the previously decided window size (otherwise it would
  552. * be necessary to store the previous window size.) In practice
  553. * this doesn't matter anyway, because png_inflate will call inflate
  554. * with Z_FINISH in almost all cases, so the window will not be
  555. * maintained.
  556. */
  557. if (inflateReset(&png_ptr->zstream) == Z_OK)
  558. {
  559. /* Because of the limit checks above we know that the new,
  560. * expanded, size will fit in a size_t (let alone an
  561. * png_alloc_size_t). Use png_malloc_base here to avoid an
  562. * extra OOM message.
  563. */
  564. png_alloc_size_t new_size = *newlength;
  565. png_alloc_size_t buffer_size = prefix_size + new_size +
  566. (terminate != 0);
  567. png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
  568. buffer_size));
  569. if (text != NULL)
  570. {
  571. memset(text, 0, buffer_size);
  572. ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
  573. png_ptr->read_buffer + prefix_size, &lzsize,
  574. text + prefix_size, newlength);
  575. if (ret == Z_STREAM_END)
  576. {
  577. if (new_size == *newlength)
  578. {
  579. if (terminate != 0)
  580. text[prefix_size + *newlength] = 0;
  581. if (prefix_size > 0)
  582. memcpy(text, png_ptr->read_buffer, prefix_size);
  583. {
  584. png_bytep old_ptr = png_ptr->read_buffer;
  585. png_ptr->read_buffer = text;
  586. png_ptr->read_buffer_size = buffer_size;
  587. text = old_ptr; /* freed below */
  588. }
  589. }
  590. else
  591. {
  592. /* The size changed on the second read, there can be no
  593. * guarantee that anything is correct at this point.
  594. * The 'msg' pointer has been set to "unexpected end of
  595. * LZ stream", which is fine, but return an error code
  596. * that the caller won't accept.
  597. */
  598. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  599. }
  600. }
  601. else if (ret == Z_OK)
  602. ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
  603. /* Free the text pointer (this is the old read_buffer on
  604. * success)
  605. */
  606. png_free(png_ptr, text);
  607. /* This really is very benign, but it's still an error because
  608. * the extra space may otherwise be used as a Trojan Horse.
  609. */
  610. if (ret == Z_STREAM_END &&
  611. chunklength - prefix_size != lzsize)
  612. png_chunk_benign_error(png_ptr, "extra compressed data");
  613. }
  614. else
  615. {
  616. /* Out of memory allocating the buffer */
  617. ret = Z_MEM_ERROR;
  618. png_zstream_error(png_ptr, Z_MEM_ERROR);
  619. }
  620. }
  621. else
  622. {
  623. /* inflateReset failed, store the error message */
  624. png_zstream_error(png_ptr, ret);
  625. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  626. }
  627. }
  628. else if (ret == Z_OK)
  629. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  630. /* Release the claimed stream */
  631. png_ptr->zowner = 0;
  632. }
  633. else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
  634. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  635. return ret;
  636. }
  637. else
  638. {
  639. /* Application/configuration limits exceeded */
  640. png_zstream_error(png_ptr, Z_MEM_ERROR);
  641. return Z_MEM_ERROR;
  642. }
  643. }
  644. #endif /* READ_zTXt || READ_iTXt */
  645. #endif /* READ_COMPRESSED_TEXT */
  646. #ifdef PNG_READ_iCCP_SUPPORTED
  647. /* Perform a partial read and decompress, producing 'avail_out' bytes and
  648. * reading from the current chunk as required.
  649. */
  650. static int
  651. png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
  652. png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
  653. int finish)
  654. {
  655. if (png_ptr->zowner == png_ptr->chunk_name)
  656. {
  657. int ret;
  658. /* next_in and avail_in must have been initialized by the caller. */
  659. png_ptr->zstream.next_out = next_out;
  660. png_ptr->zstream.avail_out = 0; /* set in the loop */
  661. do
  662. {
  663. if (png_ptr->zstream.avail_in == 0)
  664. {
  665. if (read_size > *chunk_bytes)
  666. read_size = (uInt)*chunk_bytes;
  667. *chunk_bytes -= read_size;
  668. if (read_size > 0)
  669. png_crc_read(png_ptr, read_buffer, read_size);
  670. png_ptr->zstream.next_in = read_buffer;
  671. png_ptr->zstream.avail_in = read_size;
  672. }
  673. if (png_ptr->zstream.avail_out == 0)
  674. {
  675. uInt avail = ZLIB_IO_MAX;
  676. if (avail > *out_size)
  677. avail = (uInt)*out_size;
  678. *out_size -= avail;
  679. png_ptr->zstream.avail_out = avail;
  680. }
  681. /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
  682. * the available output is produced; this allows reading of truncated
  683. * streams.
  684. */
  685. ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
  686. Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
  687. }
  688. while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
  689. *out_size += png_ptr->zstream.avail_out;
  690. png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
  691. /* Ensure the error message pointer is always set: */
  692. png_zstream_error(png_ptr, ret);
  693. return ret;
  694. }
  695. else
  696. {
  697. png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
  698. return Z_STREAM_ERROR;
  699. }
  700. }
  701. #endif /* READ_iCCP */
  702. /* Read and check the IDHR chunk */
  703. void /* PRIVATE */
  704. png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  705. {
  706. png_byte buf[13];
  707. png_uint_32 width, height;
  708. int bit_depth, color_type, compression_type, filter_type;
  709. int interlace_type;
  710. png_debug(1, "in png_handle_IHDR");
  711. if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
  712. png_chunk_error(png_ptr, "out of place");
  713. /* Check the length */
  714. if (length != 13)
  715. png_chunk_error(png_ptr, "invalid");
  716. png_ptr->mode |= PNG_HAVE_IHDR;
  717. png_crc_read(png_ptr, buf, 13);
  718. png_crc_finish(png_ptr, 0);
  719. width = png_get_uint_31(png_ptr, buf);
  720. height = png_get_uint_31(png_ptr, buf + 4);
  721. bit_depth = buf[8];
  722. color_type = buf[9];
  723. compression_type = buf[10];
  724. filter_type = buf[11];
  725. interlace_type = buf[12];
  726. /* Set internal variables */
  727. png_ptr->width = width;
  728. png_ptr->height = height;
  729. png_ptr->bit_depth = (png_byte)bit_depth;
  730. png_ptr->interlaced = (png_byte)interlace_type;
  731. png_ptr->color_type = (png_byte)color_type;
  732. #ifdef PNG_MNG_FEATURES_SUPPORTED
  733. png_ptr->filter_type = (png_byte)filter_type;
  734. #endif
  735. png_ptr->compression_type = (png_byte)compression_type;
  736. /* Find number of channels */
  737. switch (png_ptr->color_type)
  738. {
  739. default: /* invalid, png_set_IHDR calls png_error */
  740. case PNG_COLOR_TYPE_GRAY:
  741. case PNG_COLOR_TYPE_PALETTE:
  742. png_ptr->channels = 1;
  743. break;
  744. case PNG_COLOR_TYPE_RGB:
  745. png_ptr->channels = 3;
  746. break;
  747. case PNG_COLOR_TYPE_GRAY_ALPHA:
  748. png_ptr->channels = 2;
  749. break;
  750. case PNG_COLOR_TYPE_RGB_ALPHA:
  751. png_ptr->channels = 4;
  752. break;
  753. }
  754. /* Set up other useful info */
  755. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
  756. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  757. png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
  758. png_debug1(3, "channels = %d", png_ptr->channels);
  759. png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
  760. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  761. color_type, interlace_type, compression_type, filter_type);
  762. }
  763. /* Read and check the palette */
  764. void /* PRIVATE */
  765. png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  766. {
  767. png_color palette[PNG_MAX_PALETTE_LENGTH];
  768. int max_palette_length, num, i;
  769. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  770. png_colorp pal_ptr;
  771. #endif
  772. png_debug(1, "in png_handle_PLTE");
  773. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  774. png_chunk_error(png_ptr, "missing IHDR");
  775. /* Moved to before the 'after IDAT' check below because otherwise duplicate
  776. * PLTE chunks are potentially ignored (the spec says there shall not be more
  777. * than one PLTE, the error is not treated as benign, so this check trumps
  778. * the requirement that PLTE appears before IDAT.)
  779. */
  780. else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
  781. png_chunk_error(png_ptr, "duplicate");
  782. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  783. {
  784. /* This is benign because the non-benign error happened before, when an
  785. * IDAT was encountered in a color-mapped image with no PLTE.
  786. */
  787. png_crc_finish(png_ptr, length);
  788. png_chunk_benign_error(png_ptr, "out of place");
  789. return;
  790. }
  791. png_ptr->mode |= PNG_HAVE_PLTE;
  792. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
  793. {
  794. png_crc_finish(png_ptr, length);
  795. png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
  796. return;
  797. }
  798. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  799. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  800. {
  801. png_crc_finish(png_ptr, length);
  802. return;
  803. }
  804. #endif
  805. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  806. {
  807. png_crc_finish(png_ptr, length);
  808. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  809. png_chunk_benign_error(png_ptr, "invalid");
  810. else
  811. png_chunk_error(png_ptr, "invalid");
  812. return;
  813. }
  814. /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
  815. num = (int)length / 3;
  816. /* If the palette has 256 or fewer entries but is too large for the bit
  817. * depth, we don't issue an error, to preserve the behavior of previous
  818. * libpng versions. We silently truncate the unused extra palette entries
  819. * here.
  820. */
  821. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  822. max_palette_length = (1 << png_ptr->bit_depth);
  823. else
  824. max_palette_length = PNG_MAX_PALETTE_LENGTH;
  825. if (num > max_palette_length)
  826. num = max_palette_length;
  827. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  828. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  829. {
  830. png_byte buf[3];
  831. png_crc_read(png_ptr, buf, 3);
  832. pal_ptr->red = buf[0];
  833. pal_ptr->green = buf[1];
  834. pal_ptr->blue = buf[2];
  835. }
  836. #else
  837. for (i = 0; i < num; i++)
  838. {
  839. png_byte buf[3];
  840. png_crc_read(png_ptr, buf, 3);
  841. /* Don't depend upon png_color being any order */
  842. palette[i].red = buf[0];
  843. palette[i].green = buf[1];
  844. palette[i].blue = buf[2];
  845. }
  846. #endif
  847. /* If we actually need the PLTE chunk (ie for a paletted image), we do
  848. * whatever the normal CRC configuration tells us. However, if we
  849. * have an RGB image, the PLTE can be considered ancillary, so
  850. * we will act as though it is.
  851. */
  852. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  853. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  854. #endif
  855. {
  856. png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
  857. }
  858. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  859. else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
  860. {
  861. /* If we don't want to use the data from an ancillary chunk,
  862. * we have two options: an error abort, or a warning and we
  863. * ignore the data in this chunk (which should be OK, since
  864. * it's considered ancillary for a RGB or RGBA image).
  865. *
  866. * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
  867. * chunk type to determine whether to check the ancillary or the critical
  868. * flags.
  869. */
  870. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
  871. {
  872. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
  873. return;
  874. else
  875. png_chunk_error(png_ptr, "CRC error");
  876. }
  877. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  878. else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
  879. png_chunk_warning(png_ptr, "CRC error");
  880. }
  881. #endif
  882. /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
  883. * own copy of the palette. This has the side effect that when png_start_row
  884. * is called (this happens after any call to png_read_update_info) the
  885. * info_ptr palette gets changed. This is extremely unexpected and
  886. * confusing.
  887. *
  888. * Fix this by not sharing the palette in this way.
  889. */
  890. png_set_PLTE(png_ptr, info_ptr, palette, num);
  891. /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
  892. * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
  893. * checked the apparent validity of a tRNS chunk inserted before PLTE on a
  894. * palette PNG. 1.6.0 attempts to rigorously follow the standard and
  895. * therefore does a benign error if the erroneous condition is detected *and*
  896. * cancels the tRNS if the benign error returns. The alternative is to
  897. * amend the standard since it would be rather hypocritical of the standards
  898. * maintainers to ignore it.
  899. */
  900. #ifdef PNG_READ_tRNS_SUPPORTED
  901. if (png_ptr->num_trans > 0 ||
  902. (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
  903. {
  904. /* Cancel this because otherwise it would be used if the transforms
  905. * require it. Don't cancel the 'valid' flag because this would prevent
  906. * detection of duplicate chunks.
  907. */
  908. png_ptr->num_trans = 0;
  909. if (info_ptr != NULL)
  910. info_ptr->num_trans = 0;
  911. png_chunk_benign_error(png_ptr, "tRNS must be after");
  912. }
  913. #endif
  914. #ifdef PNG_READ_hIST_SUPPORTED
  915. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
  916. png_chunk_benign_error(png_ptr, "hIST must be after");
  917. #endif
  918. #ifdef PNG_READ_bKGD_SUPPORTED
  919. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
  920. png_chunk_benign_error(png_ptr, "bKGD must be after");
  921. #endif
  922. }
  923. void /* PRIVATE */
  924. png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  925. {
  926. png_debug(1, "in png_handle_IEND");
  927. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
  928. (png_ptr->mode & PNG_HAVE_IDAT) == 0)
  929. png_chunk_error(png_ptr, "out of place");
  930. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  931. png_crc_finish(png_ptr, length);
  932. if (length != 0)
  933. png_chunk_benign_error(png_ptr, "invalid");
  934. PNG_UNUSED(info_ptr)
  935. }
  936. #ifdef PNG_READ_gAMA_SUPPORTED
  937. void /* PRIVATE */
  938. png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  939. {
  940. png_fixed_point igamma;
  941. png_byte buf[4];
  942. png_debug(1, "in png_handle_gAMA");
  943. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  944. png_chunk_error(png_ptr, "missing IHDR");
  945. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  946. {
  947. png_crc_finish(png_ptr, length);
  948. png_chunk_benign_error(png_ptr, "out of place");
  949. return;
  950. }
  951. if (length != 4)
  952. {
  953. png_crc_finish(png_ptr, length);
  954. png_chunk_benign_error(png_ptr, "invalid");
  955. return;
  956. }
  957. png_crc_read(png_ptr, buf, 4);
  958. if (png_crc_finish(png_ptr, 0) != 0)
  959. return;
  960. igamma = png_get_fixed_point(NULL, buf);
  961. png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
  962. png_colorspace_sync(png_ptr, info_ptr);
  963. }
  964. #endif
  965. #ifdef PNG_READ_sBIT_SUPPORTED
  966. void /* PRIVATE */
  967. png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  968. {
  969. unsigned int truelen, i;
  970. png_byte sample_depth;
  971. png_byte buf[4];
  972. png_debug(1, "in png_handle_sBIT");
  973. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  974. png_chunk_error(png_ptr, "missing IHDR");
  975. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  976. {
  977. png_crc_finish(png_ptr, length);
  978. png_chunk_benign_error(png_ptr, "out of place");
  979. return;
  980. }
  981. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
  982. {
  983. png_crc_finish(png_ptr, length);
  984. png_chunk_benign_error(png_ptr, "duplicate");
  985. return;
  986. }
  987. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  988. {
  989. truelen = 3;
  990. sample_depth = 8;
  991. }
  992. else
  993. {
  994. truelen = png_ptr->channels;
  995. sample_depth = png_ptr->bit_depth;
  996. }
  997. if (length != truelen || length > 4)
  998. {
  999. png_chunk_benign_error(png_ptr, "invalid");
  1000. png_crc_finish(png_ptr, length);
  1001. return;
  1002. }
  1003. buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
  1004. png_crc_read(png_ptr, buf, truelen);
  1005. if (png_crc_finish(png_ptr, 0) != 0)
  1006. return;
  1007. for (i=0; i<truelen; ++i)
  1008. {
  1009. if (buf[i] == 0 || buf[i] > sample_depth)
  1010. {
  1011. png_chunk_benign_error(png_ptr, "invalid");
  1012. return;
  1013. }
  1014. }
  1015. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1016. {
  1017. png_ptr->sig_bit.red = buf[0];
  1018. png_ptr->sig_bit.green = buf[1];
  1019. png_ptr->sig_bit.blue = buf[2];
  1020. png_ptr->sig_bit.alpha = buf[3];
  1021. }
  1022. else
  1023. {
  1024. png_ptr->sig_bit.gray = buf[0];
  1025. png_ptr->sig_bit.red = buf[0];
  1026. png_ptr->sig_bit.green = buf[0];
  1027. png_ptr->sig_bit.blue = buf[0];
  1028. png_ptr->sig_bit.alpha = buf[1];
  1029. }
  1030. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  1031. }
  1032. #endif
  1033. #ifdef PNG_READ_cHRM_SUPPORTED
  1034. void /* PRIVATE */
  1035. png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1036. {
  1037. png_byte buf[32];
  1038. png_xy xy;
  1039. png_debug(1, "in png_handle_cHRM");
  1040. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1041. png_chunk_error(png_ptr, "missing IHDR");
  1042. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1043. {
  1044. png_crc_finish(png_ptr, length);
  1045. png_chunk_benign_error(png_ptr, "out of place");
  1046. return;
  1047. }
  1048. if (length != 32)
  1049. {
  1050. png_crc_finish(png_ptr, length);
  1051. png_chunk_benign_error(png_ptr, "invalid");
  1052. return;
  1053. }
  1054. png_crc_read(png_ptr, buf, 32);
  1055. if (png_crc_finish(png_ptr, 0) != 0)
  1056. return;
  1057. xy.whitex = png_get_fixed_point(NULL, buf);
  1058. xy.whitey = png_get_fixed_point(NULL, buf + 4);
  1059. xy.redx = png_get_fixed_point(NULL, buf + 8);
  1060. xy.redy = png_get_fixed_point(NULL, buf + 12);
  1061. xy.greenx = png_get_fixed_point(NULL, buf + 16);
  1062. xy.greeny = png_get_fixed_point(NULL, buf + 20);
  1063. xy.bluex = png_get_fixed_point(NULL, buf + 24);
  1064. xy.bluey = png_get_fixed_point(NULL, buf + 28);
  1065. if (xy.whitex == PNG_FIXED_ERROR ||
  1066. xy.whitey == PNG_FIXED_ERROR ||
  1067. xy.redx == PNG_FIXED_ERROR ||
  1068. xy.redy == PNG_FIXED_ERROR ||
  1069. xy.greenx == PNG_FIXED_ERROR ||
  1070. xy.greeny == PNG_FIXED_ERROR ||
  1071. xy.bluex == PNG_FIXED_ERROR ||
  1072. xy.bluey == PNG_FIXED_ERROR)
  1073. {
  1074. png_chunk_benign_error(png_ptr, "invalid values");
  1075. return;
  1076. }
  1077. /* If a colorspace error has already been output skip this chunk */
  1078. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1079. return;
  1080. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
  1081. {
  1082. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1083. png_colorspace_sync(png_ptr, info_ptr);
  1084. png_chunk_benign_error(png_ptr, "duplicate");
  1085. return;
  1086. }
  1087. png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
  1088. (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
  1089. 1/*prefer cHRM values*/);
  1090. png_colorspace_sync(png_ptr, info_ptr);
  1091. }
  1092. #endif
  1093. #ifdef PNG_READ_sRGB_SUPPORTED
  1094. void /* PRIVATE */
  1095. png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1096. {
  1097. png_byte intent;
  1098. png_debug(1, "in png_handle_sRGB");
  1099. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1100. png_chunk_error(png_ptr, "missing IHDR");
  1101. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1102. {
  1103. png_crc_finish(png_ptr, length);
  1104. png_chunk_benign_error(png_ptr, "out of place");
  1105. return;
  1106. }
  1107. if (length != 1)
  1108. {
  1109. png_crc_finish(png_ptr, length);
  1110. png_chunk_benign_error(png_ptr, "invalid");
  1111. return;
  1112. }
  1113. png_crc_read(png_ptr, &intent, 1);
  1114. if (png_crc_finish(png_ptr, 0) != 0)
  1115. return;
  1116. /* If a colorspace error has already been output skip this chunk */
  1117. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1118. return;
  1119. /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
  1120. * this.
  1121. */
  1122. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
  1123. {
  1124. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1125. png_colorspace_sync(png_ptr, info_ptr);
  1126. png_chunk_benign_error(png_ptr, "too many profiles");
  1127. return;
  1128. }
  1129. (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
  1130. png_colorspace_sync(png_ptr, info_ptr);
  1131. }
  1132. #endif /* READ_sRGB */
  1133. #ifdef PNG_READ_iCCP_SUPPORTED
  1134. void /* PRIVATE */
  1135. png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1136. /* Note: this does not properly handle profiles that are > 64K under DOS */
  1137. {
  1138. png_const_charp errmsg = NULL; /* error message output, or no error */
  1139. int finished = 0; /* crc checked */
  1140. png_debug(1, "in png_handle_iCCP");
  1141. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1142. png_chunk_error(png_ptr, "missing IHDR");
  1143. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1144. {
  1145. png_crc_finish(png_ptr, length);
  1146. png_chunk_benign_error(png_ptr, "out of place");
  1147. return;
  1148. }
  1149. /* Consistent with all the above colorspace handling an obviously *invalid*
  1150. * chunk is just ignored, so does not invalidate the color space. An
  1151. * alternative is to set the 'invalid' flags at the start of this routine
  1152. * and only clear them in they were not set before and all the tests pass.
  1153. */
  1154. /* The keyword must be at least one character and there is a
  1155. * terminator (0) byte and the compression method byte, and the
  1156. * 'zlib' datastream is at least 11 bytes.
  1157. */
  1158. if (length < 14)
  1159. {
  1160. png_crc_finish(png_ptr, length);
  1161. png_chunk_benign_error(png_ptr, "too short");
  1162. return;
  1163. }
  1164. /* If a colorspace error has already been output skip this chunk */
  1165. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1166. {
  1167. png_crc_finish(png_ptr, length);
  1168. return;
  1169. }
  1170. /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
  1171. * this.
  1172. */
  1173. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
  1174. {
  1175. uInt read_length, keyword_length;
  1176. char keyword[81];
  1177. /* Find the keyword; the keyword plus separator and compression method
  1178. * bytes can be at most 81 characters long.
  1179. */
  1180. read_length = 81; /* maximum */
  1181. if (read_length > length)
  1182. read_length = (uInt)length;
  1183. png_crc_read(png_ptr, (png_bytep)keyword, read_length);
  1184. length -= read_length;
  1185. /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
  1186. * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
  1187. */
  1188. if (length < 11)
  1189. {
  1190. png_crc_finish(png_ptr, length);
  1191. png_chunk_benign_error(png_ptr, "too short");
  1192. return;
  1193. }
  1194. keyword_length = 0;
  1195. while (keyword_length < 80 && keyword_length < read_length &&
  1196. keyword[keyword_length] != 0)
  1197. ++keyword_length;
  1198. /* TODO: make the keyword checking common */
  1199. if (keyword_length >= 1 && keyword_length <= 79)
  1200. {
  1201. /* We only understand '0' compression - deflate - so if we get a
  1202. * different value we can't safely decode the chunk.
  1203. */
  1204. if (keyword_length+1 < read_length &&
  1205. keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
  1206. {
  1207. read_length -= keyword_length+2;
  1208. if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
  1209. {
  1210. Byte profile_header[132]={0};
  1211. Byte local_buffer[PNG_INFLATE_BUF_SIZE];
  1212. png_alloc_size_t size = (sizeof profile_header);
  1213. png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
  1214. png_ptr->zstream.avail_in = read_length;
  1215. (void)png_inflate_read(png_ptr, local_buffer,
  1216. (sizeof local_buffer), &length, profile_header, &size,
  1217. 0/*finish: don't, because the output is too small*/);
  1218. if (size == 0)
  1219. {
  1220. /* We have the ICC profile header; do the basic header checks.
  1221. */
  1222. const png_uint_32 profile_length =
  1223. png_get_uint_32(profile_header);
  1224. if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
  1225. keyword, profile_length) != 0)
  1226. {
  1227. /* The length is apparently ok, so we can check the 132
  1228. * byte header.
  1229. */
  1230. if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
  1231. keyword, profile_length, profile_header,
  1232. png_ptr->color_type) != 0)
  1233. {
  1234. /* Now read the tag table; a variable size buffer is
  1235. * needed at this point, allocate one for the whole
  1236. * profile. The header check has already validated
  1237. * that none of this stuff will overflow.
  1238. */
  1239. const png_uint_32 tag_count = png_get_uint_32(
  1240. profile_header+128);
  1241. png_bytep profile = png_read_buffer(png_ptr,
  1242. profile_length, 2/*silent*/);
  1243. if (profile != NULL)
  1244. {
  1245. memcpy(profile, profile_header,
  1246. (sizeof profile_header));
  1247. size = 12 * tag_count;
  1248. (void)png_inflate_read(png_ptr, local_buffer,
  1249. (sizeof local_buffer), &length,
  1250. profile + (sizeof profile_header), &size, 0);
  1251. /* Still expect a buffer error because we expect
  1252. * there to be some tag data!
  1253. */
  1254. if (size == 0)
  1255. {
  1256. if (png_icc_check_tag_table(png_ptr,
  1257. &png_ptr->colorspace, keyword, profile_length,
  1258. profile) != 0)
  1259. {
  1260. /* The profile has been validated for basic
  1261. * security issues, so read the whole thing in.
  1262. */
  1263. size = profile_length - (sizeof profile_header)
  1264. - 12 * tag_count;
  1265. (void)png_inflate_read(png_ptr, local_buffer,
  1266. (sizeof local_buffer), &length,
  1267. profile + (sizeof profile_header) +
  1268. 12 * tag_count, &size, 1/*finish*/);
  1269. if (length > 0 && !(png_ptr->flags &
  1270. PNG_FLAG_BENIGN_ERRORS_WARN))
  1271. errmsg = "extra compressed data";
  1272. /* But otherwise allow extra data: */
  1273. else if (size == 0)
  1274. {
  1275. if (length > 0)
  1276. {
  1277. /* This can be handled completely, so
  1278. * keep going.
  1279. */
  1280. png_chunk_warning(png_ptr,
  1281. "extra compressed data");
  1282. }
  1283. png_crc_finish(png_ptr, length);
  1284. finished = 1;
  1285. # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
  1286. /* Check for a match against sRGB */
  1287. png_icc_set_sRGB(png_ptr,
  1288. &png_ptr->colorspace, profile,
  1289. png_ptr->zstream.adler);
  1290. # endif
  1291. /* Steal the profile for info_ptr. */
  1292. if (info_ptr != NULL)
  1293. {
  1294. png_free_data(png_ptr, info_ptr,
  1295. PNG_FREE_ICCP, 0);
  1296. info_ptr->iccp_name = png_voidcast(char*,
  1297. png_malloc_base(png_ptr,
  1298. keyword_length+1));
  1299. if (info_ptr->iccp_name != NULL)
  1300. {
  1301. memcpy(info_ptr->iccp_name, keyword,
  1302. keyword_length+1);
  1303. info_ptr->iccp_proflen =
  1304. profile_length;
  1305. info_ptr->iccp_profile = profile;
  1306. png_ptr->read_buffer = NULL; /*steal*/
  1307. info_ptr->free_me |= PNG_FREE_ICCP;
  1308. info_ptr->valid |= PNG_INFO_iCCP;
  1309. }
  1310. else
  1311. {
  1312. png_ptr->colorspace.flags |=
  1313. PNG_COLORSPACE_INVALID;
  1314. errmsg = "out of memory";
  1315. }
  1316. }
  1317. /* else the profile remains in the read
  1318. * buffer which gets reused for subsequent
  1319. * chunks.
  1320. */
  1321. if (info_ptr != NULL)
  1322. png_colorspace_sync(png_ptr, info_ptr);
  1323. if (errmsg == NULL)
  1324. {
  1325. png_ptr->zowner = 0;
  1326. return;
  1327. }
  1328. }
  1329. if (errmsg == NULL)
  1330. errmsg = png_ptr->zstream.msg;
  1331. }
  1332. /* else png_icc_check_tag_table output an error */
  1333. }
  1334. else /* profile truncated */
  1335. errmsg = png_ptr->zstream.msg;
  1336. }
  1337. else
  1338. errmsg = "out of memory";
  1339. }
  1340. /* else png_icc_check_header output an error */
  1341. }
  1342. /* else png_icc_check_length output an error */
  1343. }
  1344. else /* profile truncated */
  1345. errmsg = png_ptr->zstream.msg;
  1346. /* Release the stream */
  1347. png_ptr->zowner = 0;
  1348. }
  1349. else /* png_inflate_claim failed */
  1350. errmsg = png_ptr->zstream.msg;
  1351. }
  1352. else
  1353. errmsg = "bad compression method"; /* or missing */
  1354. }
  1355. else
  1356. errmsg = "bad keyword";
  1357. }
  1358. else
  1359. errmsg = "too many profiles";
  1360. /* Failure: the reason is in 'errmsg' */
  1361. if (finished == 0)
  1362. png_crc_finish(png_ptr, length);
  1363. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1364. png_colorspace_sync(png_ptr, info_ptr);
  1365. if (errmsg != NULL) /* else already output */
  1366. png_chunk_benign_error(png_ptr, errmsg);
  1367. }
  1368. #endif /* READ_iCCP */
  1369. #ifdef PNG_READ_sPLT_SUPPORTED
  1370. void /* PRIVATE */
  1371. png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1372. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1373. {
  1374. png_bytep entry_start, buffer;
  1375. png_sPLT_t new_palette;
  1376. png_sPLT_entryp pp;
  1377. png_uint_32 data_length;
  1378. int entry_size, i;
  1379. png_uint_32 skip = 0;
  1380. png_uint_32 dl;
  1381. size_t max_dl;
  1382. png_debug(1, "in png_handle_sPLT");
  1383. #ifdef PNG_USER_LIMITS_SUPPORTED
  1384. if (png_ptr->user_chunk_cache_max != 0)
  1385. {
  1386. if (png_ptr->user_chunk_cache_max == 1)
  1387. {
  1388. png_crc_finish(png_ptr, length);
  1389. return;
  1390. }
  1391. if (--png_ptr->user_chunk_cache_max == 1)
  1392. {
  1393. png_warning(png_ptr, "No space in chunk cache for sPLT");
  1394. png_crc_finish(png_ptr, length);
  1395. return;
  1396. }
  1397. }
  1398. #endif
  1399. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1400. png_chunk_error(png_ptr, "missing IHDR");
  1401. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1402. {
  1403. png_crc_finish(png_ptr, length);
  1404. png_chunk_benign_error(png_ptr, "out of place");
  1405. return;
  1406. }
  1407. #ifdef PNG_MAX_MALLOC_64K
  1408. if (length > 65535U)
  1409. {
  1410. png_crc_finish(png_ptr, length);
  1411. png_chunk_benign_error(png_ptr, "too large to fit in memory");
  1412. return;
  1413. }
  1414. #endif
  1415. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  1416. if (buffer == NULL)
  1417. {
  1418. png_crc_finish(png_ptr, length);
  1419. png_chunk_benign_error(png_ptr, "out of memory");
  1420. return;
  1421. }
  1422. /* WARNING: this may break if size_t is less than 32 bits; it is assumed
  1423. * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
  1424. * potential breakage point if the types in pngconf.h aren't exactly right.
  1425. */
  1426. png_crc_read(png_ptr, buffer, length);
  1427. if (png_crc_finish(png_ptr, skip) != 0)
  1428. return;
  1429. buffer[length] = 0;
  1430. for (entry_start = buffer; *entry_start; entry_start++)
  1431. /* Empty loop to find end of name */ ;
  1432. ++entry_start;
  1433. /* A sample depth should follow the separator, and we should be on it */
  1434. if (length < 2U || entry_start > buffer + (length - 2U))
  1435. {
  1436. png_warning(png_ptr, "malformed sPLT chunk");
  1437. return;
  1438. }
  1439. new_palette.depth = *entry_start++;
  1440. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1441. /* This must fit in a png_uint_32 because it is derived from the original
  1442. * chunk data length.
  1443. */
  1444. data_length = length - (png_uint_32)(entry_start - buffer);
  1445. /* Integrity-check the data length */
  1446. if ((data_length % (unsigned int)entry_size) != 0)
  1447. {
  1448. png_warning(png_ptr, "sPLT chunk has bad length");
  1449. return;
  1450. }
  1451. dl = (png_uint_32)(data_length / (unsigned int)entry_size);
  1452. max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
  1453. if (dl > max_dl)
  1454. {
  1455. png_warning(png_ptr, "sPLT chunk too long");
  1456. return;
  1457. }
  1458. new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
  1459. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  1460. (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
  1461. if (new_palette.entries == NULL)
  1462. {
  1463. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1464. return;
  1465. }
  1466. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1467. for (i = 0; i < new_palette.nentries; i++)
  1468. {
  1469. pp = new_palette.entries + i;
  1470. if (new_palette.depth == 8)
  1471. {
  1472. pp->red = *entry_start++;
  1473. pp->green = *entry_start++;
  1474. pp->blue = *entry_start++;
  1475. pp->alpha = *entry_start++;
  1476. }
  1477. else
  1478. {
  1479. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1480. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1481. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1482. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1483. }
  1484. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1485. }
  1486. #else
  1487. pp = new_palette.entries;
  1488. for (i = 0; i < new_palette.nentries; i++)
  1489. {
  1490. if (new_palette.depth == 8)
  1491. {
  1492. pp[i].red = *entry_start++;
  1493. pp[i].green = *entry_start++;
  1494. pp[i].blue = *entry_start++;
  1495. pp[i].alpha = *entry_start++;
  1496. }
  1497. else
  1498. {
  1499. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1500. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1501. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1502. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1503. }
  1504. pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
  1505. }
  1506. #endif
  1507. /* Discard all chunk data except the name and stash that */
  1508. new_palette.name = (png_charp)buffer;
  1509. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1510. png_free(png_ptr, new_palette.entries);
  1511. }
  1512. #endif /* READ_sPLT */
  1513. #ifdef PNG_READ_tRNS_SUPPORTED
  1514. void /* PRIVATE */
  1515. png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1516. {
  1517. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1518. png_debug(1, "in png_handle_tRNS");
  1519. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1520. png_chunk_error(png_ptr, "missing IHDR");
  1521. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1522. {
  1523. png_crc_finish(png_ptr, length);
  1524. png_chunk_benign_error(png_ptr, "out of place");
  1525. return;
  1526. }
  1527. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
  1528. {
  1529. png_crc_finish(png_ptr, length);
  1530. png_chunk_benign_error(png_ptr, "duplicate");
  1531. return;
  1532. }
  1533. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1534. {
  1535. png_byte buf[2];
  1536. if (length != 2)
  1537. {
  1538. png_crc_finish(png_ptr, length);
  1539. png_chunk_benign_error(png_ptr, "invalid");
  1540. return;
  1541. }
  1542. png_crc_read(png_ptr, buf, 2);
  1543. png_ptr->num_trans = 1;
  1544. png_ptr->trans_color.gray = png_get_uint_16(buf);
  1545. }
  1546. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1547. {
  1548. png_byte buf[6];
  1549. if (length != 6)
  1550. {
  1551. png_crc_finish(png_ptr, length);
  1552. png_chunk_benign_error(png_ptr, "invalid");
  1553. return;
  1554. }
  1555. png_crc_read(png_ptr, buf, length);
  1556. png_ptr->num_trans = 1;
  1557. png_ptr->trans_color.red = png_get_uint_16(buf);
  1558. png_ptr->trans_color.green = png_get_uint_16(buf + 2);
  1559. png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
  1560. }
  1561. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1562. {
  1563. if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
  1564. {
  1565. /* TODO: is this actually an error in the ISO spec? */
  1566. png_crc_finish(png_ptr, length);
  1567. png_chunk_benign_error(png_ptr, "out of place");
  1568. return;
  1569. }
  1570. if (length > (unsigned int) png_ptr->num_palette ||
  1571. length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
  1572. length == 0)
  1573. {
  1574. png_crc_finish(png_ptr, length);
  1575. png_chunk_benign_error(png_ptr, "invalid");
  1576. return;
  1577. }
  1578. png_crc_read(png_ptr, readbuf, length);
  1579. png_ptr->num_trans = (png_uint_16)length;
  1580. }
  1581. else
  1582. {
  1583. png_crc_finish(png_ptr, length);
  1584. png_chunk_benign_error(png_ptr, "invalid with alpha channel");
  1585. return;
  1586. }
  1587. if (png_crc_finish(png_ptr, 0) != 0)
  1588. {
  1589. png_ptr->num_trans = 0;
  1590. return;
  1591. }
  1592. /* TODO: this is a horrible side effect in the palette case because the
  1593. * png_struct ends up with a pointer to the tRNS buffer owned by the
  1594. * png_info. Fix this.
  1595. */
  1596. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1597. &(png_ptr->trans_color));
  1598. }
  1599. #endif
  1600. #ifdef PNG_READ_bKGD_SUPPORTED
  1601. void /* PRIVATE */
  1602. png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1603. {
  1604. unsigned int truelen;
  1605. png_byte buf[6];
  1606. png_color_16 background;
  1607. png_debug(1, "in png_handle_bKGD");
  1608. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1609. png_chunk_error(png_ptr, "missing IHDR");
  1610. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
  1611. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1612. (png_ptr->mode & PNG_HAVE_PLTE) == 0))
  1613. {
  1614. png_crc_finish(png_ptr, length);
  1615. png_chunk_benign_error(png_ptr, "out of place");
  1616. return;
  1617. }
  1618. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
  1619. {
  1620. png_crc_finish(png_ptr, length);
  1621. png_chunk_benign_error(png_ptr, "duplicate");
  1622. return;
  1623. }
  1624. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1625. truelen = 1;
  1626. else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1627. truelen = 6;
  1628. else
  1629. truelen = 2;
  1630. if (length != truelen)
  1631. {
  1632. png_crc_finish(png_ptr, length);
  1633. png_chunk_benign_error(png_ptr, "invalid");
  1634. return;
  1635. }
  1636. png_crc_read(png_ptr, buf, truelen);
  1637. if (png_crc_finish(png_ptr, 0) != 0)
  1638. return;
  1639. /* We convert the index value into RGB components so that we can allow
  1640. * arbitrary RGB values for background when we have transparency, and
  1641. * so it is easy to determine the RGB values of the background color
  1642. * from the info_ptr struct.
  1643. */
  1644. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1645. {
  1646. background.index = buf[0];
  1647. if (info_ptr != NULL && info_ptr->num_palette != 0)
  1648. {
  1649. if (buf[0] >= info_ptr->num_palette)
  1650. {
  1651. png_chunk_benign_error(png_ptr, "invalid index");
  1652. return;
  1653. }
  1654. background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
  1655. background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
  1656. background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
  1657. }
  1658. else
  1659. background.red = background.green = background.blue = 0;
  1660. background.gray = 0;
  1661. }
  1662. else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
  1663. {
  1664. if (png_ptr->bit_depth <= 8)
  1665. {
  1666. if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
  1667. {
  1668. png_chunk_benign_error(png_ptr, "invalid gray level");
  1669. return;
  1670. }
  1671. }
  1672. background.index = 0;
  1673. background.red =
  1674. background.green =
  1675. background.blue =
  1676. background.gray = png_get_uint_16(buf);
  1677. }
  1678. else
  1679. {
  1680. if (png_ptr->bit_depth <= 8)
  1681. {
  1682. if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
  1683. {
  1684. png_chunk_benign_error(png_ptr, "invalid color");
  1685. return;
  1686. }
  1687. }
  1688. background.index = 0;
  1689. background.red = png_get_uint_16(buf);
  1690. background.green = png_get_uint_16(buf + 2);
  1691. background.blue = png_get_uint_16(buf + 4);
  1692. background.gray = 0;
  1693. }
  1694. png_set_bKGD(png_ptr, info_ptr, &background);
  1695. }
  1696. #endif
  1697. #ifdef PNG_READ_eXIf_SUPPORTED
  1698. void /* PRIVATE */
  1699. png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1700. {
  1701. unsigned int i;
  1702. png_debug(1, "in png_handle_eXIf");
  1703. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1704. png_chunk_error(png_ptr, "missing IHDR");
  1705. if (length < 2)
  1706. {
  1707. png_crc_finish(png_ptr, length);
  1708. png_chunk_benign_error(png_ptr, "too short");
  1709. return;
  1710. }
  1711. else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
  1712. {
  1713. png_crc_finish(png_ptr, length);
  1714. png_chunk_benign_error(png_ptr, "duplicate");
  1715. return;
  1716. }
  1717. info_ptr->free_me |= PNG_FREE_EXIF;
  1718. info_ptr->eXIf_buf = png_voidcast(png_bytep,
  1719. png_malloc_warn(png_ptr, length));
  1720. if (info_ptr->eXIf_buf == NULL)
  1721. {
  1722. png_crc_finish(png_ptr, length);
  1723. png_chunk_benign_error(png_ptr, "out of memory");
  1724. return;
  1725. }
  1726. for (i = 0; i < length; i++)
  1727. {
  1728. png_byte buf[1];
  1729. png_crc_read(png_ptr, buf, 1);
  1730. info_ptr->eXIf_buf[i] = buf[0];
  1731. if (i == 1 && buf[0] != 'M' && buf[0] != 'I'
  1732. && info_ptr->eXIf_buf[0] != buf[0])
  1733. {
  1734. png_crc_finish(png_ptr, length);
  1735. png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
  1736. png_free(png_ptr, info_ptr->eXIf_buf);
  1737. info_ptr->eXIf_buf = NULL;
  1738. return;
  1739. }
  1740. }
  1741. if (png_crc_finish(png_ptr, 0) != 0)
  1742. return;
  1743. png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
  1744. png_free(png_ptr, info_ptr->eXIf_buf);
  1745. info_ptr->eXIf_buf = NULL;
  1746. }
  1747. #endif
  1748. #ifdef PNG_READ_hIST_SUPPORTED
  1749. void /* PRIVATE */
  1750. png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1751. {
  1752. unsigned int num, i;
  1753. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1754. png_debug(1, "in png_handle_hIST");
  1755. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1756. png_chunk_error(png_ptr, "missing IHDR");
  1757. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
  1758. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  1759. {
  1760. png_crc_finish(png_ptr, length);
  1761. png_chunk_benign_error(png_ptr, "out of place");
  1762. return;
  1763. }
  1764. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
  1765. {
  1766. png_crc_finish(png_ptr, length);
  1767. png_chunk_benign_error(png_ptr, "duplicate");
  1768. return;
  1769. }
  1770. num = length / 2 ;
  1771. if (num != (unsigned int) png_ptr->num_palette ||
  1772. num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
  1773. {
  1774. png_crc_finish(png_ptr, length);
  1775. png_chunk_benign_error(png_ptr, "invalid");
  1776. return;
  1777. }
  1778. for (i = 0; i < num; i++)
  1779. {
  1780. png_byte buf[2];
  1781. png_crc_read(png_ptr, buf, 2);
  1782. readbuf[i] = png_get_uint_16(buf);
  1783. }
  1784. if (png_crc_finish(png_ptr, 0) != 0)
  1785. return;
  1786. png_set_hIST(png_ptr, info_ptr, readbuf);
  1787. }
  1788. #endif
  1789. #ifdef PNG_READ_pHYs_SUPPORTED
  1790. void /* PRIVATE */
  1791. png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1792. {
  1793. png_byte buf[9];
  1794. png_uint_32 res_x, res_y;
  1795. int unit_type;
  1796. png_debug(1, "in png_handle_pHYs");
  1797. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1798. png_chunk_error(png_ptr, "missing IHDR");
  1799. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1800. {
  1801. png_crc_finish(png_ptr, length);
  1802. png_chunk_benign_error(png_ptr, "out of place");
  1803. return;
  1804. }
  1805. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
  1806. {
  1807. png_crc_finish(png_ptr, length);
  1808. png_chunk_benign_error(png_ptr, "duplicate");
  1809. return;
  1810. }
  1811. if (length != 9)
  1812. {
  1813. png_crc_finish(png_ptr, length);
  1814. png_chunk_benign_error(png_ptr, "invalid");
  1815. return;
  1816. }
  1817. png_crc_read(png_ptr, buf, 9);
  1818. if (png_crc_finish(png_ptr, 0) != 0)
  1819. return;
  1820. res_x = png_get_uint_32(buf);
  1821. res_y = png_get_uint_32(buf + 4);
  1822. unit_type = buf[8];
  1823. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1824. }
  1825. #endif
  1826. #ifdef PNG_READ_oFFs_SUPPORTED
  1827. void /* PRIVATE */
  1828. png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1829. {
  1830. png_byte buf[9];
  1831. png_int_32 offset_x, offset_y;
  1832. int unit_type;
  1833. png_debug(1, "in png_handle_oFFs");
  1834. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1835. png_chunk_error(png_ptr, "missing IHDR");
  1836. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1837. {
  1838. png_crc_finish(png_ptr, length);
  1839. png_chunk_benign_error(png_ptr, "out of place");
  1840. return;
  1841. }
  1842. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
  1843. {
  1844. png_crc_finish(png_ptr, length);
  1845. png_chunk_benign_error(png_ptr, "duplicate");
  1846. return;
  1847. }
  1848. if (length != 9)
  1849. {
  1850. png_crc_finish(png_ptr, length);
  1851. png_chunk_benign_error(png_ptr, "invalid");
  1852. return;
  1853. }
  1854. png_crc_read(png_ptr, buf, 9);
  1855. if (png_crc_finish(png_ptr, 0) != 0)
  1856. return;
  1857. offset_x = png_get_int_32(buf);
  1858. offset_y = png_get_int_32(buf + 4);
  1859. unit_type = buf[8];
  1860. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1861. }
  1862. #endif
  1863. #ifdef PNG_READ_pCAL_SUPPORTED
  1864. /* Read the pCAL chunk (described in the PNG Extensions document) */
  1865. void /* PRIVATE */
  1866. png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1867. {
  1868. png_int_32 X0, X1;
  1869. png_byte type, nparams;
  1870. png_bytep buffer, buf, units, endptr;
  1871. png_charpp params;
  1872. int i;
  1873. png_debug(1, "in png_handle_pCAL");
  1874. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1875. png_chunk_error(png_ptr, "missing IHDR");
  1876. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1877. {
  1878. png_crc_finish(png_ptr, length);
  1879. png_chunk_benign_error(png_ptr, "out of place");
  1880. return;
  1881. }
  1882. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
  1883. {
  1884. png_crc_finish(png_ptr, length);
  1885. png_chunk_benign_error(png_ptr, "duplicate");
  1886. return;
  1887. }
  1888. png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
  1889. length + 1);
  1890. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  1891. if (buffer == NULL)
  1892. {
  1893. png_crc_finish(png_ptr, length);
  1894. png_chunk_benign_error(png_ptr, "out of memory");
  1895. return;
  1896. }
  1897. png_crc_read(png_ptr, buffer, length);
  1898. if (png_crc_finish(png_ptr, 0) != 0)
  1899. return;
  1900. buffer[length] = 0; /* Null terminate the last string */
  1901. png_debug(3, "Finding end of pCAL purpose string");
  1902. for (buf = buffer; *buf; buf++)
  1903. /* Empty loop */ ;
  1904. endptr = buffer + length;
  1905. /* We need to have at least 12 bytes after the purpose string
  1906. * in order to get the parameter information.
  1907. */
  1908. if (endptr - buf <= 12)
  1909. {
  1910. png_chunk_benign_error(png_ptr, "invalid");
  1911. return;
  1912. }
  1913. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
  1914. X0 = png_get_int_32((png_bytep)buf+1);
  1915. X1 = png_get_int_32((png_bytep)buf+5);
  1916. type = buf[9];
  1917. nparams = buf[10];
  1918. units = buf + 11;
  1919. png_debug(3, "Checking pCAL equation type and number of parameters");
  1920. /* Check that we have the right number of parameters for known
  1921. * equation types.
  1922. */
  1923. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1924. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1925. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1926. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1927. {
  1928. png_chunk_benign_error(png_ptr, "invalid parameter count");
  1929. return;
  1930. }
  1931. else if (type >= PNG_EQUATION_LAST)
  1932. {
  1933. png_chunk_benign_error(png_ptr, "unrecognized equation type");
  1934. }
  1935. for (buf = units; *buf; buf++)
  1936. /* Empty loop to move past the units string. */ ;
  1937. png_debug(3, "Allocating pCAL parameters array");
  1938. params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
  1939. nparams * (sizeof (png_charp))));
  1940. if (params == NULL)
  1941. {
  1942. png_chunk_benign_error(png_ptr, "out of memory");
  1943. return;
  1944. }
  1945. /* Get pointers to the start of each parameter string. */
  1946. for (i = 0; i < nparams; i++)
  1947. {
  1948. buf++; /* Skip the null string terminator from previous parameter. */
  1949. png_debug1(3, "Reading pCAL parameter %d", i);
  1950. for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
  1951. /* Empty loop to move past each parameter string */ ;
  1952. /* Make sure we haven't run out of data yet */
  1953. if (buf > endptr)
  1954. {
  1955. png_free(png_ptr, params);
  1956. png_chunk_benign_error(png_ptr, "invalid data");
  1957. return;
  1958. }
  1959. }
  1960. png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
  1961. (png_charp)units, params);
  1962. png_free(png_ptr, params);
  1963. }
  1964. #endif
  1965. #ifdef PNG_READ_sCAL_SUPPORTED
  1966. /* Read the sCAL chunk */
  1967. void /* PRIVATE */
  1968. png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1969. {
  1970. png_bytep buffer;
  1971. size_t i;
  1972. int state;
  1973. png_debug(1, "in png_handle_sCAL");
  1974. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1975. png_chunk_error(png_ptr, "missing IHDR");
  1976. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1977. {
  1978. png_crc_finish(png_ptr, length);
  1979. png_chunk_benign_error(png_ptr, "out of place");
  1980. return;
  1981. }
  1982. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
  1983. {
  1984. png_crc_finish(png_ptr, length);
  1985. png_chunk_benign_error(png_ptr, "duplicate");
  1986. return;
  1987. }
  1988. /* Need unit type, width, \0, height: minimum 4 bytes */
  1989. else if (length < 4)
  1990. {
  1991. png_crc_finish(png_ptr, length);
  1992. png_chunk_benign_error(png_ptr, "invalid");
  1993. return;
  1994. }
  1995. png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
  1996. length + 1);
  1997. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  1998. if (buffer == NULL)
  1999. {
  2000. png_chunk_benign_error(png_ptr, "out of memory");
  2001. png_crc_finish(png_ptr, length);
  2002. return;
  2003. }
  2004. png_crc_read(png_ptr, buffer, length);
  2005. buffer[length] = 0; /* Null terminate the last string */
  2006. if (png_crc_finish(png_ptr, 0) != 0)
  2007. return;
  2008. /* Validate the unit. */
  2009. if (buffer[0] != 1 && buffer[0] != 2)
  2010. {
  2011. png_chunk_benign_error(png_ptr, "invalid unit");
  2012. return;
  2013. }
  2014. /* Validate the ASCII numbers, need two ASCII numbers separated by
  2015. * a '\0' and they need to fit exactly in the chunk data.
  2016. */
  2017. i = 1;
  2018. state = 0;
  2019. if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
  2020. i >= length || buffer[i++] != 0)
  2021. png_chunk_benign_error(png_ptr, "bad width format");
  2022. else if (PNG_FP_IS_POSITIVE(state) == 0)
  2023. png_chunk_benign_error(png_ptr, "non-positive width");
  2024. else
  2025. {
  2026. size_t heighti = i;
  2027. state = 0;
  2028. if (png_check_fp_number((png_const_charp)buffer, length,
  2029. &state, &i) == 0 || i != length)
  2030. png_chunk_benign_error(png_ptr, "bad height format");
  2031. else if (PNG_FP_IS_POSITIVE(state) == 0)
  2032. png_chunk_benign_error(png_ptr, "non-positive height");
  2033. else
  2034. /* This is the (only) success case. */
  2035. png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
  2036. (png_charp)buffer+1, (png_charp)buffer+heighti);
  2037. }
  2038. }
  2039. #endif
  2040. #ifdef PNG_READ_tIME_SUPPORTED
  2041. void /* PRIVATE */
  2042. png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2043. {
  2044. png_byte buf[7];
  2045. png_time mod_time;
  2046. png_debug(1, "in png_handle_tIME");
  2047. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2048. png_chunk_error(png_ptr, "missing IHDR");
  2049. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
  2050. {
  2051. png_crc_finish(png_ptr, length);
  2052. png_chunk_benign_error(png_ptr, "duplicate");
  2053. return;
  2054. }
  2055. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2056. png_ptr->mode |= PNG_AFTER_IDAT;
  2057. if (length != 7)
  2058. {
  2059. png_crc_finish(png_ptr, length);
  2060. png_chunk_benign_error(png_ptr, "invalid");
  2061. return;
  2062. }
  2063. png_crc_read(png_ptr, buf, 7);
  2064. if (png_crc_finish(png_ptr, 0) != 0)
  2065. return;
  2066. mod_time.second = buf[6];
  2067. mod_time.minute = buf[5];
  2068. mod_time.hour = buf[4];
  2069. mod_time.day = buf[3];
  2070. mod_time.month = buf[2];
  2071. mod_time.year = png_get_uint_16(buf);
  2072. png_set_tIME(png_ptr, info_ptr, &mod_time);
  2073. }
  2074. #endif
  2075. #ifdef PNG_READ_tEXt_SUPPORTED
  2076. /* Note: this does not properly handle chunks that are > 64K under DOS */
  2077. void /* PRIVATE */
  2078. png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2079. {
  2080. png_text text_info;
  2081. png_bytep buffer;
  2082. png_charp key;
  2083. png_charp text;
  2084. png_uint_32 skip = 0;
  2085. png_debug(1, "in png_handle_tEXt");
  2086. #ifdef PNG_USER_LIMITS_SUPPORTED
  2087. if (png_ptr->user_chunk_cache_max != 0)
  2088. {
  2089. if (png_ptr->user_chunk_cache_max == 1)
  2090. {
  2091. png_crc_finish(png_ptr, length);
  2092. return;
  2093. }
  2094. if (--png_ptr->user_chunk_cache_max == 1)
  2095. {
  2096. png_crc_finish(png_ptr, length);
  2097. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2098. return;
  2099. }
  2100. }
  2101. #endif
  2102. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2103. png_chunk_error(png_ptr, "missing IHDR");
  2104. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2105. png_ptr->mode |= PNG_AFTER_IDAT;
  2106. #ifdef PNG_MAX_MALLOC_64K
  2107. if (length > 65535U)
  2108. {
  2109. png_crc_finish(png_ptr, length);
  2110. png_chunk_benign_error(png_ptr, "too large to fit in memory");
  2111. return;
  2112. }
  2113. #endif
  2114. buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
  2115. if (buffer == NULL)
  2116. {
  2117. png_chunk_benign_error(png_ptr, "out of memory");
  2118. return;
  2119. }
  2120. png_crc_read(png_ptr, buffer, length);
  2121. if (png_crc_finish(png_ptr, skip) != 0)
  2122. return;
  2123. key = (png_charp)buffer;
  2124. key[length] = 0;
  2125. for (text = key; *text; text++)
  2126. /* Empty loop to find end of key */ ;
  2127. if (text != key + length)
  2128. text++;
  2129. text_info.compression = PNG_TEXT_COMPRESSION_NONE;
  2130. text_info.key = key;
  2131. text_info.lang = NULL;
  2132. text_info.lang_key = NULL;
  2133. text_info.itxt_length = 0;
  2134. text_info.text = text;
  2135. text_info.text_length = strlen(text);
  2136. if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
  2137. png_warning(png_ptr, "Insufficient memory to process text chunk");
  2138. }
  2139. #endif
  2140. #ifdef PNG_READ_zTXt_SUPPORTED
  2141. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2142. void /* PRIVATE */
  2143. png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2144. {
  2145. png_const_charp errmsg = NULL;
  2146. png_bytep buffer;
  2147. png_uint_32 keyword_length;
  2148. png_debug(1, "in png_handle_zTXt");
  2149. #ifdef PNG_USER_LIMITS_SUPPORTED
  2150. if (png_ptr->user_chunk_cache_max != 0)
  2151. {
  2152. if (png_ptr->user_chunk_cache_max == 1)
  2153. {
  2154. png_crc_finish(png_ptr, length);
  2155. return;
  2156. }
  2157. if (--png_ptr->user_chunk_cache_max == 1)
  2158. {
  2159. png_crc_finish(png_ptr, length);
  2160. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2161. return;
  2162. }
  2163. }
  2164. #endif
  2165. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2166. png_chunk_error(png_ptr, "missing IHDR");
  2167. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2168. png_ptr->mode |= PNG_AFTER_IDAT;
  2169. /* Note, "length" is sufficient here; we won't be adding
  2170. * a null terminator later.
  2171. */
  2172. buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
  2173. if (buffer == NULL)
  2174. {
  2175. png_crc_finish(png_ptr, length);
  2176. png_chunk_benign_error(png_ptr, "out of memory");
  2177. return;
  2178. }
  2179. png_crc_read(png_ptr, buffer, length);
  2180. if (png_crc_finish(png_ptr, 0) != 0)
  2181. return;
  2182. /* TODO: also check that the keyword contents match the spec! */
  2183. for (keyword_length = 0;
  2184. keyword_length < length && buffer[keyword_length] != 0;
  2185. ++keyword_length)
  2186. /* Empty loop to find end of name */ ;
  2187. if (keyword_length > 79 || keyword_length < 1)
  2188. errmsg = "bad keyword";
  2189. /* zTXt must have some LZ data after the keyword, although it may expand to
  2190. * zero bytes; we need a '\0' at the end of the keyword, the compression type
  2191. * then the LZ data:
  2192. */
  2193. else if (keyword_length + 3 > length)
  2194. errmsg = "truncated";
  2195. else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
  2196. errmsg = "unknown compression type";
  2197. else
  2198. {
  2199. png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
  2200. /* TODO: at present png_decompress_chunk imposes a single application
  2201. * level memory limit, this should be split to different values for iCCP
  2202. * and text chunks.
  2203. */
  2204. if (png_decompress_chunk(png_ptr, length, keyword_length+2,
  2205. &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
  2206. {
  2207. png_text text;
  2208. if (png_ptr->read_buffer == NULL)
  2209. errmsg="Read failure in png_handle_zTXt";
  2210. else
  2211. {
  2212. /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
  2213. * except for the extra compression type byte and the fact that
  2214. * it isn't necessarily '\0' terminated.
  2215. */
  2216. buffer = png_ptr->read_buffer;
  2217. buffer[uncompressed_length+(keyword_length+2)] = 0;
  2218. text.compression = PNG_TEXT_COMPRESSION_zTXt;
  2219. text.key = (png_charp)buffer;
  2220. text.text = (png_charp)(buffer + keyword_length+2);
  2221. text.text_length = uncompressed_length;
  2222. text.itxt_length = 0;
  2223. text.lang = NULL;
  2224. text.lang_key = NULL;
  2225. if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
  2226. errmsg = "insufficient memory";
  2227. }
  2228. }
  2229. else
  2230. errmsg = png_ptr->zstream.msg;
  2231. }
  2232. if (errmsg != NULL)
  2233. png_chunk_benign_error(png_ptr, errmsg);
  2234. }
  2235. #endif
  2236. #ifdef PNG_READ_iTXt_SUPPORTED
  2237. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2238. void /* PRIVATE */
  2239. png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2240. {
  2241. png_const_charp errmsg = NULL;
  2242. png_bytep buffer;
  2243. png_uint_32 prefix_length;
  2244. png_debug(1, "in png_handle_iTXt");
  2245. #ifdef PNG_USER_LIMITS_SUPPORTED
  2246. if (png_ptr->user_chunk_cache_max != 0)
  2247. {
  2248. if (png_ptr->user_chunk_cache_max == 1)
  2249. {
  2250. png_crc_finish(png_ptr, length);
  2251. return;
  2252. }
  2253. if (--png_ptr->user_chunk_cache_max == 1)
  2254. {
  2255. png_crc_finish(png_ptr, length);
  2256. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2257. return;
  2258. }
  2259. }
  2260. #endif
  2261. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2262. png_chunk_error(png_ptr, "missing IHDR");
  2263. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2264. png_ptr->mode |= PNG_AFTER_IDAT;
  2265. buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
  2266. if (buffer == NULL)
  2267. {
  2268. png_crc_finish(png_ptr, length);
  2269. png_chunk_benign_error(png_ptr, "out of memory");
  2270. return;
  2271. }
  2272. png_crc_read(png_ptr, buffer, length);
  2273. if (png_crc_finish(png_ptr, 0) != 0)
  2274. return;
  2275. /* First the keyword. */
  2276. for (prefix_length=0;
  2277. prefix_length < length && buffer[prefix_length] != 0;
  2278. ++prefix_length)
  2279. /* Empty loop */ ;
  2280. /* Perform a basic check on the keyword length here. */
  2281. if (prefix_length > 79 || prefix_length < 1)
  2282. errmsg = "bad keyword";
  2283. /* Expect keyword, compression flag, compression type, language, translated
  2284. * keyword (both may be empty but are 0 terminated) then the text, which may
  2285. * be empty.
  2286. */
  2287. else if (prefix_length + 5 > length)
  2288. errmsg = "truncated";
  2289. else if (buffer[prefix_length+1] == 0 ||
  2290. (buffer[prefix_length+1] == 1 &&
  2291. buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
  2292. {
  2293. int compressed = buffer[prefix_length+1] != 0;
  2294. png_uint_32 language_offset, translated_keyword_offset;
  2295. png_alloc_size_t uncompressed_length = 0;
  2296. /* Now the language tag */
  2297. prefix_length += 3;
  2298. language_offset = prefix_length;
  2299. for (; prefix_length < length && buffer[prefix_length] != 0;
  2300. ++prefix_length)
  2301. /* Empty loop */ ;
  2302. /* WARNING: the length may be invalid here, this is checked below. */
  2303. translated_keyword_offset = ++prefix_length;
  2304. for (; prefix_length < length && buffer[prefix_length] != 0;
  2305. ++prefix_length)
  2306. /* Empty loop */ ;
  2307. /* prefix_length should now be at the trailing '\0' of the translated
  2308. * keyword, but it may already be over the end. None of this arithmetic
  2309. * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
  2310. * systems the available allocation may overflow.
  2311. */
  2312. ++prefix_length;
  2313. if (compressed == 0 && prefix_length <= length)
  2314. uncompressed_length = length - prefix_length;
  2315. else if (compressed != 0 && prefix_length < length)
  2316. {
  2317. uncompressed_length = PNG_SIZE_MAX;
  2318. /* TODO: at present png_decompress_chunk imposes a single application
  2319. * level memory limit, this should be split to different values for
  2320. * iCCP and text chunks.
  2321. */
  2322. if (png_decompress_chunk(png_ptr, length, prefix_length,
  2323. &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
  2324. buffer = png_ptr->read_buffer;
  2325. else
  2326. errmsg = png_ptr->zstream.msg;
  2327. }
  2328. else
  2329. errmsg = "truncated";
  2330. if (errmsg == NULL)
  2331. {
  2332. png_text text;
  2333. buffer[uncompressed_length+prefix_length] = 0;
  2334. if (compressed == 0)
  2335. text.compression = PNG_ITXT_COMPRESSION_NONE;
  2336. else
  2337. text.compression = PNG_ITXT_COMPRESSION_zTXt;
  2338. text.key = (png_charp)buffer;
  2339. text.lang = (png_charp)buffer + language_offset;
  2340. text.lang_key = (png_charp)buffer + translated_keyword_offset;
  2341. text.text = (png_charp)buffer + prefix_length;
  2342. text.text_length = 0;
  2343. text.itxt_length = uncompressed_length;
  2344. if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
  2345. errmsg = "insufficient memory";
  2346. }
  2347. }
  2348. else
  2349. errmsg = "bad compression info";
  2350. if (errmsg != NULL)
  2351. png_chunk_benign_error(png_ptr, errmsg);
  2352. }
  2353. #endif
  2354. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2355. /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
  2356. static int
  2357. png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
  2358. {
  2359. png_alloc_size_t limit = PNG_SIZE_MAX;
  2360. if (png_ptr->unknown_chunk.data != NULL)
  2361. {
  2362. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2363. png_ptr->unknown_chunk.data = NULL;
  2364. }
  2365. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2366. if (png_ptr->user_chunk_malloc_max > 0 &&
  2367. png_ptr->user_chunk_malloc_max < limit)
  2368. limit = png_ptr->user_chunk_malloc_max;
  2369. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  2370. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  2371. limit = PNG_USER_CHUNK_MALLOC_MAX;
  2372. # endif
  2373. if (length <= limit)
  2374. {
  2375. PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
  2376. /* The following is safe because of the PNG_SIZE_MAX init above */
  2377. png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
  2378. /* 'mode' is a flag array, only the bottom four bits matter here */
  2379. png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
  2380. if (length == 0)
  2381. png_ptr->unknown_chunk.data = NULL;
  2382. else
  2383. {
  2384. /* Do a 'warn' here - it is handled below. */
  2385. png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
  2386. png_malloc_warn(png_ptr, length));
  2387. }
  2388. }
  2389. if (png_ptr->unknown_chunk.data == NULL && length > 0)
  2390. {
  2391. /* This is benign because we clean up correctly */
  2392. png_crc_finish(png_ptr, length);
  2393. png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
  2394. return 0;
  2395. }
  2396. else
  2397. {
  2398. if (length > 0)
  2399. png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
  2400. png_crc_finish(png_ptr, 0);
  2401. return 1;
  2402. }
  2403. }
  2404. #endif /* READ_UNKNOWN_CHUNKS */
  2405. /* Handle an unknown, or known but disabled, chunk */
  2406. void /* PRIVATE */
  2407. png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
  2408. png_uint_32 length, int keep)
  2409. {
  2410. int handled = 0; /* the chunk was handled */
  2411. png_debug(1, "in png_handle_unknown");
  2412. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2413. /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
  2414. * the bug which meant that setting a non-default behavior for a specific
  2415. * chunk would be ignored (the default was always used unless a user
  2416. * callback was installed).
  2417. *
  2418. * 'keep' is the value from the png_chunk_unknown_handling, the setting for
  2419. * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
  2420. * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
  2421. * This is just an optimization to avoid multiple calls to the lookup
  2422. * function.
  2423. */
  2424. # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2425. # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  2426. keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
  2427. # endif
  2428. # endif
  2429. /* One of the following methods will read the chunk or skip it (at least one
  2430. * of these is always defined because this is the only way to switch on
  2431. * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  2432. */
  2433. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2434. /* The user callback takes precedence over the chunk keep value, but the
  2435. * keep value is still required to validate a save of a critical chunk.
  2436. */
  2437. if (png_ptr->read_user_chunk_fn != NULL)
  2438. {
  2439. if (png_cache_unknown_chunk(png_ptr, length) != 0)
  2440. {
  2441. /* Callback to user unknown chunk handler */
  2442. int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
  2443. &png_ptr->unknown_chunk);
  2444. /* ret is:
  2445. * negative: An error occurred; png_chunk_error will be called.
  2446. * zero: The chunk was not handled, the chunk will be discarded
  2447. * unless png_set_keep_unknown_chunks has been used to set
  2448. * a 'keep' behavior for this particular chunk, in which
  2449. * case that will be used. A critical chunk will cause an
  2450. * error at this point unless it is to be saved.
  2451. * positive: The chunk was handled, libpng will ignore/discard it.
  2452. */
  2453. if (ret < 0)
  2454. png_chunk_error(png_ptr, "error in user chunk");
  2455. else if (ret == 0)
  2456. {
  2457. /* If the keep value is 'default' or 'never' override it, but
  2458. * still error out on critical chunks unless the keep value is
  2459. * 'always' While this is weird it is the behavior in 1.4.12.
  2460. * A possible improvement would be to obey the value set for the
  2461. * chunk, but this would be an API change that would probably
  2462. * damage some applications.
  2463. *
  2464. * The png_app_warning below catches the case that matters, where
  2465. * the application has not set specific save or ignore for this
  2466. * chunk or global save or ignore.
  2467. */
  2468. if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
  2469. {
  2470. # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  2471. if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
  2472. {
  2473. png_chunk_warning(png_ptr, "Saving unknown chunk:");
  2474. png_app_warning(png_ptr,
  2475. "forcing save of an unhandled chunk;"
  2476. " please call png_set_keep_unknown_chunks");
  2477. /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
  2478. }
  2479. # endif
  2480. keep = PNG_HANDLE_CHUNK_IF_SAFE;
  2481. }
  2482. }
  2483. else /* chunk was handled */
  2484. {
  2485. handled = 1;
  2486. /* Critical chunks can be safely discarded at this point. */
  2487. keep = PNG_HANDLE_CHUNK_NEVER;
  2488. }
  2489. }
  2490. else
  2491. keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
  2492. }
  2493. else
  2494. /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
  2495. # endif /* READ_USER_CHUNKS */
  2496. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  2497. {
  2498. /* keep is currently just the per-chunk setting, if there was no
  2499. * setting change it to the global default now (not that this may
  2500. * still be AS_DEFAULT) then obtain the cache of the chunk if required,
  2501. * if not simply skip the chunk.
  2502. */
  2503. if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  2504. keep = png_ptr->unknown_default;
  2505. if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
  2506. (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
  2507. PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
  2508. {
  2509. if (png_cache_unknown_chunk(png_ptr, length) == 0)
  2510. keep = PNG_HANDLE_CHUNK_NEVER;
  2511. }
  2512. else
  2513. png_crc_finish(png_ptr, length);
  2514. }
  2515. # else
  2516. # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  2517. # error no method to support READ_UNKNOWN_CHUNKS
  2518. # endif
  2519. {
  2520. /* If here there is no read callback pointer set and no support is
  2521. * compiled in to just save the unknown chunks, so simply skip this
  2522. * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
  2523. * the app has erroneously asked for unknown chunk saving when there
  2524. * is no support.
  2525. */
  2526. if (keep > PNG_HANDLE_CHUNK_NEVER)
  2527. png_app_error(png_ptr, "no unknown chunk support available");
  2528. png_crc_finish(png_ptr, length);
  2529. }
  2530. # endif
  2531. # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  2532. /* Now store the chunk in the chunk list if appropriate, and if the limits
  2533. * permit it.
  2534. */
  2535. if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
  2536. (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
  2537. PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
  2538. {
  2539. # ifdef PNG_USER_LIMITS_SUPPORTED
  2540. switch (png_ptr->user_chunk_cache_max)
  2541. {
  2542. case 2:
  2543. png_ptr->user_chunk_cache_max = 1;
  2544. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2545. /* FALLTHROUGH */
  2546. case 1:
  2547. /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
  2548. * chunk being skipped, now there will be a hard error below.
  2549. */
  2550. break;
  2551. default: /* not at limit */
  2552. --(png_ptr->user_chunk_cache_max);
  2553. /* FALLTHROUGH */
  2554. case 0: /* no limit */
  2555. # endif /* USER_LIMITS */
  2556. /* Here when the limit isn't reached or when limits are compiled
  2557. * out; store the chunk.
  2558. */
  2559. png_set_unknown_chunks(png_ptr, info_ptr,
  2560. &png_ptr->unknown_chunk, 1);
  2561. handled = 1;
  2562. # ifdef PNG_USER_LIMITS_SUPPORTED
  2563. break;
  2564. }
  2565. # endif
  2566. }
  2567. # else /* no store support: the chunk must be handled by the user callback */
  2568. PNG_UNUSED(info_ptr)
  2569. # endif
  2570. /* Regardless of the error handling below the cached data (if any) can be
  2571. * freed now. Notice that the data is not freed if there is a png_error, but
  2572. * it will be freed by destroy_read_struct.
  2573. */
  2574. if (png_ptr->unknown_chunk.data != NULL)
  2575. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2576. png_ptr->unknown_chunk.data = NULL;
  2577. #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
  2578. /* There is no support to read an unknown chunk, so just skip it. */
  2579. png_crc_finish(png_ptr, length);
  2580. PNG_UNUSED(info_ptr)
  2581. PNG_UNUSED(keep)
  2582. #endif /* !READ_UNKNOWN_CHUNKS */
  2583. /* Check for unhandled critical chunks */
  2584. if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
  2585. png_chunk_error(png_ptr, "unhandled critical chunk");
  2586. }
  2587. /* This function is called to verify that a chunk name is valid.
  2588. * This function can't have the "critical chunk check" incorporated
  2589. * into it, since in the future we will need to be able to call user
  2590. * functions to handle unknown critical chunks after we check that
  2591. * the chunk name itself is valid.
  2592. */
  2593. /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
  2594. *
  2595. * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  2596. */
  2597. void /* PRIVATE */
  2598. png_check_chunk_name(png_const_structrp png_ptr, const png_uint_32 chunk_name)
  2599. {
  2600. int i;
  2601. png_uint_32 cn=chunk_name;
  2602. png_debug(1, "in png_check_chunk_name");
  2603. for (i=1; i<=4; ++i)
  2604. {
  2605. int c = cn & 0xff;
  2606. if (c < 65 || c > 122 || (c > 90 && c < 97))
  2607. png_chunk_error(png_ptr, "invalid chunk type");
  2608. cn >>= 8;
  2609. }
  2610. }
  2611. void /* PRIVATE */
  2612. png_check_chunk_length(png_const_structrp png_ptr, const png_uint_32 length)
  2613. {
  2614. png_alloc_size_t limit = PNG_UINT_31_MAX;
  2615. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2616. if (png_ptr->user_chunk_malloc_max > 0 &&
  2617. png_ptr->user_chunk_malloc_max < limit)
  2618. limit = png_ptr->user_chunk_malloc_max;
  2619. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  2620. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  2621. limit = PNG_USER_CHUNK_MALLOC_MAX;
  2622. # endif
  2623. if (png_ptr->chunk_name == png_IDAT)
  2624. {
  2625. png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
  2626. size_t row_factor =
  2627. (size_t)png_ptr->width
  2628. * (size_t)png_ptr->channels
  2629. * (png_ptr->bit_depth > 8? 2: 1)
  2630. + 1
  2631. + (png_ptr->interlaced? 6: 0);
  2632. if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
  2633. idat_limit = PNG_UINT_31_MAX;
  2634. else
  2635. idat_limit = png_ptr->height * row_factor;
  2636. row_factor = row_factor > 32566? 32566 : row_factor;
  2637. idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
  2638. idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
  2639. limit = limit < idat_limit? idat_limit : limit;
  2640. }
  2641. if (length > limit)
  2642. {
  2643. png_debug2(0," length = %lu, limit = %lu",
  2644. (unsigned long)length,(unsigned long)limit);
  2645. png_chunk_error(png_ptr, "chunk data is too large");
  2646. }
  2647. }
  2648. /* Combines the row recently read in with the existing pixels in the row. This
  2649. * routine takes care of alpha and transparency if requested. This routine also
  2650. * handles the two methods of progressive display of interlaced images,
  2651. * depending on the 'display' value; if 'display' is true then the whole row
  2652. * (dp) is filled from the start by replicating the available pixels. If
  2653. * 'display' is false only those pixels present in the pass are filled in.
  2654. */
  2655. void /* PRIVATE */
  2656. png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
  2657. {
  2658. unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
  2659. png_const_bytep sp = png_ptr->row_buf + 1;
  2660. png_alloc_size_t row_width = png_ptr->width;
  2661. unsigned int pass = png_ptr->pass;
  2662. png_bytep end_ptr = 0;
  2663. png_byte end_byte = 0;
  2664. unsigned int end_mask;
  2665. png_debug(1, "in png_combine_row");
  2666. /* Added in 1.5.6: it should not be possible to enter this routine until at
  2667. * least one row has been read from the PNG data and transformed.
  2668. */
  2669. if (pixel_depth == 0)
  2670. png_error(png_ptr, "internal row logic error");
  2671. /* Added in 1.5.4: the pixel depth should match the information returned by
  2672. * any call to png_read_update_info at this point. Do not continue if we got
  2673. * this wrong.
  2674. */
  2675. if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
  2676. PNG_ROWBYTES(pixel_depth, row_width))
  2677. png_error(png_ptr, "internal row size calculation error");
  2678. /* Don't expect this to ever happen: */
  2679. if (row_width == 0)
  2680. png_error(png_ptr, "internal row width error");
  2681. /* Preserve the last byte in cases where only part of it will be overwritten,
  2682. * the multiply below may overflow, we don't care because ANSI-C guarantees
  2683. * we get the low bits.
  2684. */
  2685. end_mask = (pixel_depth * row_width) & 7;
  2686. if (end_mask != 0)
  2687. {
  2688. /* end_ptr == NULL is a flag to say do nothing */
  2689. end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
  2690. end_byte = *end_ptr;
  2691. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2692. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  2693. /* little-endian byte */
  2694. end_mask = (unsigned int)(0xff << end_mask);
  2695. else /* big-endian byte */
  2696. # endif
  2697. end_mask = 0xff >> end_mask;
  2698. /* end_mask is now the bits to *keep* from the destination row */
  2699. }
  2700. /* For non-interlaced images this reduces to a memcpy(). A memcpy()
  2701. * will also happen if interlacing isn't supported or if the application
  2702. * does not call png_set_interlace_handling(). In the latter cases the
  2703. * caller just gets a sequence of the unexpanded rows from each interlace
  2704. * pass.
  2705. */
  2706. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2707. if (png_ptr->interlaced != 0 &&
  2708. (png_ptr->transformations & PNG_INTERLACE) != 0 &&
  2709. pass < 6 && (display == 0 ||
  2710. /* The following copies everything for 'display' on passes 0, 2 and 4. */
  2711. (display == 1 && (pass & 1) != 0)))
  2712. {
  2713. /* Narrow images may have no bits in a pass; the caller should handle
  2714. * this, but this test is cheap:
  2715. */
  2716. if (row_width <= PNG_PASS_START_COL(pass))
  2717. return;
  2718. if (pixel_depth < 8)
  2719. {
  2720. /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
  2721. * into 32 bits, then a single loop over the bytes using the four byte
  2722. * values in the 32-bit mask can be used. For the 'display' option the
  2723. * expanded mask may also not require any masking within a byte. To
  2724. * make this work the PACKSWAP option must be taken into account - it
  2725. * simply requires the pixels to be reversed in each byte.
  2726. *
  2727. * The 'regular' case requires a mask for each of the first 6 passes,
  2728. * the 'display' case does a copy for the even passes in the range
  2729. * 0..6. This has already been handled in the test above.
  2730. *
  2731. * The masks are arranged as four bytes with the first byte to use in
  2732. * the lowest bits (little-endian) regardless of the order (PACKSWAP or
  2733. * not) of the pixels in each byte.
  2734. *
  2735. * NOTE: the whole of this logic depends on the caller of this function
  2736. * only calling it on rows appropriate to the pass. This function only
  2737. * understands the 'x' logic; the 'y' logic is handled by the caller.
  2738. *
  2739. * The following defines allow generation of compile time constant bit
  2740. * masks for each pixel depth and each possibility of swapped or not
  2741. * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
  2742. * is in the range 0..7; and the result is 1 if the pixel is to be
  2743. * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
  2744. * for the block method.
  2745. *
  2746. * With some compilers a compile time expression of the general form:
  2747. *
  2748. * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
  2749. *
  2750. * Produces warnings with values of 'shift' in the range 33 to 63
  2751. * because the right hand side of the ?: expression is evaluated by
  2752. * the compiler even though it isn't used. Microsoft Visual C (various
  2753. * versions) and the Intel C compiler are known to do this. To avoid
  2754. * this the following macros are used in 1.5.6. This is a temporary
  2755. * solution to avoid destabilizing the code during the release process.
  2756. */
  2757. # if PNG_USE_COMPILE_TIME_MASKS
  2758. # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
  2759. # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
  2760. # else
  2761. # define PNG_LSR(x,s) ((x)>>(s))
  2762. # define PNG_LSL(x,s) ((x)<<(s))
  2763. # endif
  2764. # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
  2765. PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
  2766. # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
  2767. PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
  2768. /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
  2769. * little endian - the first pixel is at bit 0 - however the extra
  2770. * parameter 's' can be set to cause the mask position to be swapped
  2771. * within each byte, to match the PNG format. This is done by XOR of
  2772. * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
  2773. */
  2774. # define PIXEL_MASK(p,x,d,s) \
  2775. (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
  2776. /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
  2777. */
  2778. # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2779. # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2780. /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
  2781. * cases the result needs replicating, for the 4-bpp case the above
  2782. * generates a full 32 bits.
  2783. */
  2784. # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
  2785. # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
  2786. S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
  2787. S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
  2788. # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
  2789. B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
  2790. B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
  2791. #if PNG_USE_COMPILE_TIME_MASKS
  2792. /* Utility macros to construct all the masks for a depth/swap
  2793. * combination. The 's' parameter says whether the format is PNG
  2794. * (big endian bytes) or not. Only the three odd-numbered passes are
  2795. * required for the display/block algorithm.
  2796. */
  2797. # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
  2798. S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
  2799. # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
  2800. # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
  2801. /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
  2802. * then pass:
  2803. */
  2804. static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
  2805. {
  2806. /* Little-endian byte masks for PACKSWAP */
  2807. { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
  2808. /* Normal (big-endian byte) masks - PNG format */
  2809. { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
  2810. };
  2811. /* display_mask has only three entries for the odd passes, so index by
  2812. * pass>>1.
  2813. */
  2814. static PNG_CONST png_uint_32 display_mask[2][3][3] =
  2815. {
  2816. /* Little-endian byte masks for PACKSWAP */
  2817. { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
  2818. /* Normal (big-endian byte) masks - PNG format */
  2819. { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
  2820. };
  2821. # define MASK(pass,depth,display,png)\
  2822. ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
  2823. row_mask[png][DEPTH_INDEX(depth)][pass])
  2824. #else /* !PNG_USE_COMPILE_TIME_MASKS */
  2825. /* This is the runtime alternative: it seems unlikely that this will
  2826. * ever be either smaller or faster than the compile time approach.
  2827. */
  2828. # define MASK(pass,depth,display,png)\
  2829. ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
  2830. #endif /* !USE_COMPILE_TIME_MASKS */
  2831. /* Use the appropriate mask to copy the required bits. In some cases
  2832. * the byte mask will be 0 or 0xff; optimize these cases. row_width is
  2833. * the number of pixels, but the code copies bytes, so it is necessary
  2834. * to special case the end.
  2835. */
  2836. png_uint_32 pixels_per_byte = 8 / pixel_depth;
  2837. png_uint_32 mask;
  2838. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2839. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  2840. mask = MASK(pass, pixel_depth, display, 0);
  2841. else
  2842. # endif
  2843. mask = MASK(pass, pixel_depth, display, 1);
  2844. for (;;)
  2845. {
  2846. png_uint_32 m;
  2847. /* It doesn't matter in the following if png_uint_32 has more than
  2848. * 32 bits because the high bits always match those in m<<24; it is,
  2849. * however, essential to use OR here, not +, because of this.
  2850. */
  2851. m = mask;
  2852. mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
  2853. m &= 0xff;
  2854. if (m != 0) /* something to copy */
  2855. {
  2856. if (m != 0xff)
  2857. *dp = (png_byte)((*dp & ~m) | (*sp & m));
  2858. else
  2859. *dp = *sp;
  2860. }
  2861. /* NOTE: this may overwrite the last byte with garbage if the image
  2862. * is not an exact number of bytes wide; libpng has always done
  2863. * this.
  2864. */
  2865. if (row_width <= pixels_per_byte)
  2866. break; /* May need to restore part of the last byte */
  2867. row_width -= pixels_per_byte;
  2868. ++dp;
  2869. ++sp;
  2870. }
  2871. }
  2872. else /* pixel_depth >= 8 */
  2873. {
  2874. unsigned int bytes_to_copy, bytes_to_jump;
  2875. /* Validate the depth - it must be a multiple of 8 */
  2876. if (pixel_depth & 7)
  2877. png_error(png_ptr, "invalid user transform pixel depth");
  2878. pixel_depth >>= 3; /* now in bytes */
  2879. row_width *= pixel_depth;
  2880. /* Regardless of pass number the Adam 7 interlace always results in a
  2881. * fixed number of pixels to copy then to skip. There may be a
  2882. * different number of pixels to skip at the start though.
  2883. */
  2884. {
  2885. unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
  2886. row_width -= offset;
  2887. dp += offset;
  2888. sp += offset;
  2889. }
  2890. /* Work out the bytes to copy. */
  2891. if (display != 0)
  2892. {
  2893. /* When doing the 'block' algorithm the pixel in the pass gets
  2894. * replicated to adjacent pixels. This is why the even (0,2,4,6)
  2895. * passes are skipped above - the entire expanded row is copied.
  2896. */
  2897. bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
  2898. /* But don't allow this number to exceed the actual row width. */
  2899. if (bytes_to_copy > row_width)
  2900. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  2901. }
  2902. else /* normal row; Adam7 only ever gives us one pixel to copy. */
  2903. bytes_to_copy = pixel_depth;
  2904. /* In Adam7 there is a constant offset between where the pixels go. */
  2905. bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
  2906. /* And simply copy these bytes. Some optimization is possible here,
  2907. * depending on the value of 'bytes_to_copy'. Special case the low
  2908. * byte counts, which we know to be frequent.
  2909. *
  2910. * Notice that these cases all 'return' rather than 'break' - this
  2911. * avoids an unnecessary test on whether to restore the last byte
  2912. * below.
  2913. */
  2914. switch (bytes_to_copy)
  2915. {
  2916. case 1:
  2917. for (;;)
  2918. {
  2919. *dp = *sp;
  2920. if (row_width <= bytes_to_jump)
  2921. return;
  2922. dp += bytes_to_jump;
  2923. sp += bytes_to_jump;
  2924. row_width -= bytes_to_jump;
  2925. }
  2926. case 2:
  2927. /* There is a possibility of a partial copy at the end here; this
  2928. * slows the code down somewhat.
  2929. */
  2930. do
  2931. {
  2932. dp[0] = sp[0]; dp[1] = sp[1];
  2933. if (row_width <= bytes_to_jump)
  2934. return;
  2935. sp += bytes_to_jump;
  2936. dp += bytes_to_jump;
  2937. row_width -= bytes_to_jump;
  2938. }
  2939. while (row_width > 1);
  2940. /* And there can only be one byte left at this point: */
  2941. *dp = *sp;
  2942. return;
  2943. case 3:
  2944. /* This can only be the RGB case, so each copy is exactly one
  2945. * pixel and it is not necessary to check for a partial copy.
  2946. */
  2947. for (;;)
  2948. {
  2949. dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
  2950. if (row_width <= bytes_to_jump)
  2951. return;
  2952. sp += bytes_to_jump;
  2953. dp += bytes_to_jump;
  2954. row_width -= bytes_to_jump;
  2955. }
  2956. default:
  2957. #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
  2958. /* Check for double byte alignment and, if possible, use a
  2959. * 16-bit copy. Don't attempt this for narrow images - ones that
  2960. * are less than an interlace panel wide. Don't attempt it for
  2961. * wide bytes_to_copy either - use the memcpy there.
  2962. */
  2963. if (bytes_to_copy < 16 /*else use memcpy*/ &&
  2964. png_isaligned(dp, png_uint_16) &&
  2965. png_isaligned(sp, png_uint_16) &&
  2966. bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
  2967. bytes_to_jump % (sizeof (png_uint_16)) == 0)
  2968. {
  2969. /* Everything is aligned for png_uint_16 copies, but try for
  2970. * png_uint_32 first.
  2971. */
  2972. if (png_isaligned(dp, png_uint_32) &&
  2973. png_isaligned(sp, png_uint_32) &&
  2974. bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
  2975. bytes_to_jump % (sizeof (png_uint_32)) == 0)
  2976. {
  2977. png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
  2978. png_const_uint_32p sp32 = png_aligncastconst(
  2979. png_const_uint_32p, sp);
  2980. size_t skip = (bytes_to_jump-bytes_to_copy) /
  2981. (sizeof (png_uint_32));
  2982. do
  2983. {
  2984. size_t c = bytes_to_copy;
  2985. do
  2986. {
  2987. *dp32++ = *sp32++;
  2988. c -= (sizeof (png_uint_32));
  2989. }
  2990. while (c > 0);
  2991. if (row_width <= bytes_to_jump)
  2992. return;
  2993. dp32 += skip;
  2994. sp32 += skip;
  2995. row_width -= bytes_to_jump;
  2996. }
  2997. while (bytes_to_copy <= row_width);
  2998. /* Get to here when the row_width truncates the final copy.
  2999. * There will be 1-3 bytes left to copy, so don't try the
  3000. * 16-bit loop below.
  3001. */
  3002. dp = (png_bytep)dp32;
  3003. sp = (png_const_bytep)sp32;
  3004. do
  3005. *dp++ = *sp++;
  3006. while (--row_width > 0);
  3007. return;
  3008. }
  3009. /* Else do it in 16-bit quantities, but only if the size is
  3010. * not too large.
  3011. */
  3012. else
  3013. {
  3014. png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
  3015. png_const_uint_16p sp16 = png_aligncastconst(
  3016. png_const_uint_16p, sp);
  3017. size_t skip = (bytes_to_jump-bytes_to_copy) /
  3018. (sizeof (png_uint_16));
  3019. do
  3020. {
  3021. size_t c = bytes_to_copy;
  3022. do
  3023. {
  3024. *dp16++ = *sp16++;
  3025. c -= (sizeof (png_uint_16));
  3026. }
  3027. while (c > 0);
  3028. if (row_width <= bytes_to_jump)
  3029. return;
  3030. dp16 += skip;
  3031. sp16 += skip;
  3032. row_width -= bytes_to_jump;
  3033. }
  3034. while (bytes_to_copy <= row_width);
  3035. /* End of row - 1 byte left, bytes_to_copy > row_width: */
  3036. dp = (png_bytep)dp16;
  3037. sp = (png_const_bytep)sp16;
  3038. do
  3039. *dp++ = *sp++;
  3040. while (--row_width > 0);
  3041. return;
  3042. }
  3043. }
  3044. #endif /* ALIGN_TYPE code */
  3045. /* The true default - use a memcpy: */
  3046. for (;;)
  3047. {
  3048. memcpy(dp, sp, bytes_to_copy);
  3049. if (row_width <= bytes_to_jump)
  3050. return;
  3051. sp += bytes_to_jump;
  3052. dp += bytes_to_jump;
  3053. row_width -= bytes_to_jump;
  3054. if (bytes_to_copy > row_width)
  3055. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  3056. }
  3057. }
  3058. /* NOT REACHED*/
  3059. } /* pixel_depth >= 8 */
  3060. /* Here if pixel_depth < 8 to check 'end_ptr' below. */
  3061. }
  3062. else
  3063. #endif /* READ_INTERLACING */
  3064. /* If here then the switch above wasn't used so just memcpy the whole row
  3065. * from the temporary row buffer (notice that this overwrites the end of the
  3066. * destination row if it is a partial byte.)
  3067. */
  3068. memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
  3069. /* Restore the overwritten bits from the last byte if necessary. */
  3070. if (end_ptr != NULL)
  3071. *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
  3072. }
  3073. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3074. void /* PRIVATE */
  3075. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  3076. png_uint_32 transformations /* Because these may affect the byte layout */)
  3077. {
  3078. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3079. /* Offset to next interlace block */
  3080. static PNG_CONST unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3081. png_debug(1, "in png_do_read_interlace");
  3082. if (row != NULL && row_info != NULL)
  3083. {
  3084. png_uint_32 final_width;
  3085. final_width = row_info->width * png_pass_inc[pass];
  3086. switch (row_info->pixel_depth)
  3087. {
  3088. case 1:
  3089. {
  3090. png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
  3091. png_bytep dp = row + (size_t)((final_width - 1) >> 3);
  3092. unsigned int sshift, dshift;
  3093. unsigned int s_start, s_end;
  3094. int s_inc;
  3095. int jstop = (int)png_pass_inc[pass];
  3096. png_byte v;
  3097. png_uint_32 i;
  3098. int j;
  3099. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3100. if ((transformations & PNG_PACKSWAP) != 0)
  3101. {
  3102. sshift = ((row_info->width + 7) & 0x07);
  3103. dshift = ((final_width + 7) & 0x07);
  3104. s_start = 7;
  3105. s_end = 0;
  3106. s_inc = -1;
  3107. }
  3108. else
  3109. #endif
  3110. {
  3111. sshift = 7 - ((row_info->width + 7) & 0x07);
  3112. dshift = 7 - ((final_width + 7) & 0x07);
  3113. s_start = 0;
  3114. s_end = 7;
  3115. s_inc = 1;
  3116. }
  3117. for (i = 0; i < row_info->width; i++)
  3118. {
  3119. v = (png_byte)((*sp >> sshift) & 0x01);
  3120. for (j = 0; j < jstop; j++)
  3121. {
  3122. unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
  3123. tmp |= (unsigned int)(v << dshift);
  3124. *dp = (png_byte)(tmp & 0xff);
  3125. if (dshift == s_end)
  3126. {
  3127. dshift = s_start;
  3128. dp--;
  3129. }
  3130. else
  3131. dshift = (unsigned int)((int)dshift + s_inc);
  3132. }
  3133. if (sshift == s_end)
  3134. {
  3135. sshift = s_start;
  3136. sp--;
  3137. }
  3138. else
  3139. sshift = (unsigned int)((int)sshift + s_inc);
  3140. }
  3141. break;
  3142. }
  3143. case 2:
  3144. {
  3145. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  3146. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  3147. unsigned int sshift, dshift;
  3148. unsigned int s_start, s_end;
  3149. int s_inc;
  3150. int jstop = (int)png_pass_inc[pass];
  3151. png_uint_32 i;
  3152. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3153. if ((transformations & PNG_PACKSWAP) != 0)
  3154. {
  3155. sshift = (((row_info->width + 3) & 0x03) << 1);
  3156. dshift = (((final_width + 3) & 0x03) << 1);
  3157. s_start = 6;
  3158. s_end = 0;
  3159. s_inc = -2;
  3160. }
  3161. else
  3162. #endif
  3163. {
  3164. sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
  3165. dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
  3166. s_start = 0;
  3167. s_end = 6;
  3168. s_inc = 2;
  3169. }
  3170. for (i = 0; i < row_info->width; i++)
  3171. {
  3172. png_byte v;
  3173. int j;
  3174. v = (png_byte)((*sp >> sshift) & 0x03);
  3175. for (j = 0; j < jstop; j++)
  3176. {
  3177. unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
  3178. tmp |= (unsigned int)(v << dshift);
  3179. *dp = (png_byte)(tmp & 0xff);
  3180. if (dshift == s_end)
  3181. {
  3182. dshift = s_start;
  3183. dp--;
  3184. }
  3185. else
  3186. dshift = (unsigned int)((int)dshift + s_inc);
  3187. }
  3188. if (sshift == s_end)
  3189. {
  3190. sshift = s_start;
  3191. sp--;
  3192. }
  3193. else
  3194. sshift = (unsigned int)((int)sshift + s_inc);
  3195. }
  3196. break;
  3197. }
  3198. case 4:
  3199. {
  3200. png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
  3201. png_bytep dp = row + (size_t)((final_width - 1) >> 1);
  3202. unsigned int sshift, dshift;
  3203. unsigned int s_start, s_end;
  3204. int s_inc;
  3205. png_uint_32 i;
  3206. int jstop = (int)png_pass_inc[pass];
  3207. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3208. if ((transformations & PNG_PACKSWAP) != 0)
  3209. {
  3210. sshift = (((row_info->width + 1) & 0x01) << 2);
  3211. dshift = (((final_width + 1) & 0x01) << 2);
  3212. s_start = 4;
  3213. s_end = 0;
  3214. s_inc = -4;
  3215. }
  3216. else
  3217. #endif
  3218. {
  3219. sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
  3220. dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
  3221. s_start = 0;
  3222. s_end = 4;
  3223. s_inc = 4;
  3224. }
  3225. for (i = 0; i < row_info->width; i++)
  3226. {
  3227. png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
  3228. int j;
  3229. for (j = 0; j < jstop; j++)
  3230. {
  3231. unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
  3232. tmp |= (unsigned int)(v << dshift);
  3233. *dp = (png_byte)(tmp & 0xff);
  3234. if (dshift == s_end)
  3235. {
  3236. dshift = s_start;
  3237. dp--;
  3238. }
  3239. else
  3240. dshift = (unsigned int)((int)dshift + s_inc);
  3241. }
  3242. if (sshift == s_end)
  3243. {
  3244. sshift = s_start;
  3245. sp--;
  3246. }
  3247. else
  3248. sshift = (unsigned int)((int)sshift + s_inc);
  3249. }
  3250. break;
  3251. }
  3252. default:
  3253. {
  3254. size_t pixel_bytes = (row_info->pixel_depth >> 3);
  3255. png_bytep sp = row + (size_t)(row_info->width - 1)
  3256. * pixel_bytes;
  3257. png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
  3258. int jstop = (int)png_pass_inc[pass];
  3259. png_uint_32 i;
  3260. for (i = 0; i < row_info->width; i++)
  3261. {
  3262. png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
  3263. int j;
  3264. memcpy(v, sp, pixel_bytes);
  3265. for (j = 0; j < jstop; j++)
  3266. {
  3267. memcpy(dp, v, pixel_bytes);
  3268. dp -= pixel_bytes;
  3269. }
  3270. sp -= pixel_bytes;
  3271. }
  3272. break;
  3273. }
  3274. }
  3275. row_info->width = final_width;
  3276. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  3277. }
  3278. #ifndef PNG_READ_PACKSWAP_SUPPORTED
  3279. PNG_UNUSED(transformations) /* Silence compiler warning */
  3280. #endif
  3281. }
  3282. #endif /* READ_INTERLACING */
  3283. static void
  3284. png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
  3285. png_const_bytep prev_row)
  3286. {
  3287. size_t i;
  3288. size_t istop = row_info->rowbytes;
  3289. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3290. png_bytep rp = row + bpp;
  3291. PNG_UNUSED(prev_row)
  3292. for (i = bpp; i < istop; i++)
  3293. {
  3294. *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
  3295. rp++;
  3296. }
  3297. }
  3298. static void
  3299. png_read_filter_row_up(png_row_infop row_info, png_bytep row,
  3300. png_const_bytep prev_row)
  3301. {
  3302. size_t i;
  3303. size_t istop = row_info->rowbytes;
  3304. png_bytep rp = row;
  3305. png_const_bytep pp = prev_row;
  3306. for (i = 0; i < istop; i++)
  3307. {
  3308. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  3309. rp++;
  3310. }
  3311. }
  3312. static void
  3313. png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
  3314. png_const_bytep prev_row)
  3315. {
  3316. size_t i;
  3317. png_bytep rp = row;
  3318. png_const_bytep pp = prev_row;
  3319. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3320. size_t istop = row_info->rowbytes - bpp;
  3321. for (i = 0; i < bpp; i++)
  3322. {
  3323. *rp = (png_byte)(((int)(*rp) +
  3324. ((int)(*pp++) / 2 )) & 0xff);
  3325. rp++;
  3326. }
  3327. for (i = 0; i < istop; i++)
  3328. {
  3329. *rp = (png_byte)(((int)(*rp) +
  3330. (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
  3331. rp++;
  3332. }
  3333. }
  3334. static void
  3335. png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
  3336. png_const_bytep prev_row)
  3337. {
  3338. png_bytep rp_end = row + row_info->rowbytes;
  3339. int a, c;
  3340. /* First pixel/byte */
  3341. c = *prev_row++;
  3342. a = *row + c;
  3343. *row++ = (png_byte)a;
  3344. /* Remainder */
  3345. while (row < rp_end)
  3346. {
  3347. int b, pa, pb, pc, p;
  3348. a &= 0xff; /* From previous iteration or start */
  3349. b = *prev_row++;
  3350. p = b - c;
  3351. pc = a - c;
  3352. #ifdef PNG_USE_ABS
  3353. pa = abs(p);
  3354. pb = abs(pc);
  3355. pc = abs(p + pc);
  3356. #else
  3357. pa = p < 0 ? -p : p;
  3358. pb = pc < 0 ? -pc : pc;
  3359. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3360. #endif
  3361. /* Find the best predictor, the least of pa, pb, pc favoring the earlier
  3362. * ones in the case of a tie.
  3363. */
  3364. if (pb < pa)
  3365. {
  3366. pa = pb; a = b;
  3367. }
  3368. if (pc < pa) a = c;
  3369. /* Calculate the current pixel in a, and move the previous row pixel to c
  3370. * for the next time round the loop
  3371. */
  3372. c = b;
  3373. a += *row;
  3374. *row++ = (png_byte)a;
  3375. }
  3376. }
  3377. static void
  3378. png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
  3379. png_const_bytep prev_row)
  3380. {
  3381. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3382. png_bytep rp_end = row + bpp;
  3383. /* Process the first pixel in the row completely (this is the same as 'up'
  3384. * because there is only one candidate predictor for the first row).
  3385. */
  3386. while (row < rp_end)
  3387. {
  3388. int a = *row + *prev_row++;
  3389. *row++ = (png_byte)a;
  3390. }
  3391. /* Remainder */
  3392. rp_end = rp_end + (row_info->rowbytes - bpp);
  3393. while (row < rp_end)
  3394. {
  3395. int a, b, c, pa, pb, pc, p;
  3396. c = *(prev_row - bpp);
  3397. a = *(row - bpp);
  3398. b = *prev_row++;
  3399. p = b - c;
  3400. pc = a - c;
  3401. #ifdef PNG_USE_ABS
  3402. pa = abs(p);
  3403. pb = abs(pc);
  3404. pc = abs(p + pc);
  3405. #else
  3406. pa = p < 0 ? -p : p;
  3407. pb = pc < 0 ? -pc : pc;
  3408. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3409. #endif
  3410. if (pb < pa)
  3411. {
  3412. pa = pb; a = b;
  3413. }
  3414. if (pc < pa) a = c;
  3415. a += *row;
  3416. *row++ = (png_byte)a;
  3417. }
  3418. }
  3419. static void
  3420. png_init_filter_functions(png_structrp pp)
  3421. /* This function is called once for every PNG image (except for PNG images
  3422. * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
  3423. * implementations required to reverse the filtering of PNG rows. Reversing
  3424. * the filter is the first transformation performed on the row data. It is
  3425. * performed in place, therefore an implementation can be selected based on
  3426. * the image pixel format. If the implementation depends on image width then
  3427. * take care to ensure that it works correctly if the image is interlaced -
  3428. * interlacing causes the actual row width to vary.
  3429. */
  3430. {
  3431. unsigned int bpp = (pp->pixel_depth + 7) >> 3;
  3432. pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
  3433. pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
  3434. pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
  3435. if (bpp == 1)
  3436. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3437. png_read_filter_row_paeth_1byte_pixel;
  3438. else
  3439. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3440. png_read_filter_row_paeth_multibyte_pixel;
  3441. #ifdef PNG_FILTER_OPTIMIZATIONS
  3442. /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
  3443. * call to install hardware optimizations for the above functions; simply
  3444. * replace whatever elements of the pp->read_filter[] array with a hardware
  3445. * specific (or, for that matter, generic) optimization.
  3446. *
  3447. * To see an example of this examine what configure.ac does when
  3448. * --enable-arm-neon is specified on the command line.
  3449. */
  3450. PNG_FILTER_OPTIMIZATIONS(pp, bpp);
  3451. #endif
  3452. }
  3453. void /* PRIVATE */
  3454. png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
  3455. png_const_bytep prev_row, int filter)
  3456. {
  3457. /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
  3458. * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
  3459. * implementations. See png_init_filter_functions above.
  3460. */
  3461. if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
  3462. {
  3463. if (pp->read_filter[0] == NULL)
  3464. png_init_filter_functions(pp);
  3465. pp->read_filter[filter-1](row_info, row, prev_row);
  3466. }
  3467. }
  3468. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  3469. void /* PRIVATE */
  3470. png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
  3471. png_alloc_size_t avail_out)
  3472. {
  3473. /* Loop reading IDATs and decompressing the result into output[avail_out] */
  3474. png_ptr->zstream.next_out = output;
  3475. png_ptr->zstream.avail_out = 0; /* safety: set below */
  3476. if (output == NULL)
  3477. avail_out = 0;
  3478. do
  3479. {
  3480. int ret;
  3481. png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
  3482. if (png_ptr->zstream.avail_in == 0)
  3483. {
  3484. uInt avail_in;
  3485. png_bytep buffer;
  3486. while (png_ptr->idat_size == 0)
  3487. {
  3488. png_crc_finish(png_ptr, 0);
  3489. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  3490. /* This is an error even in the 'check' case because the code just
  3491. * consumed a non-IDAT header.
  3492. */
  3493. if (png_ptr->chunk_name != png_IDAT)
  3494. png_error(png_ptr, "Not enough image data");
  3495. }
  3496. avail_in = png_ptr->IDAT_read_size;
  3497. if (avail_in > png_ptr->idat_size)
  3498. avail_in = (uInt)png_ptr->idat_size;
  3499. /* A PNG with a gradually increasing IDAT size will defeat this attempt
  3500. * to minimize memory usage by causing lots of re-allocs, but
  3501. * realistically doing IDAT_read_size re-allocs is not likely to be a
  3502. * big problem.
  3503. */
  3504. buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
  3505. png_crc_read(png_ptr, buffer, avail_in);
  3506. png_ptr->idat_size -= avail_in;
  3507. png_ptr->zstream.next_in = buffer;
  3508. png_ptr->zstream.avail_in = avail_in;
  3509. }
  3510. /* And set up the output side. */
  3511. if (output != NULL) /* standard read */
  3512. {
  3513. uInt out = ZLIB_IO_MAX;
  3514. if (out > avail_out)
  3515. out = (uInt)avail_out;
  3516. avail_out -= out;
  3517. png_ptr->zstream.avail_out = out;
  3518. }
  3519. else /* after last row, checking for end */
  3520. {
  3521. png_ptr->zstream.next_out = tmpbuf;
  3522. png_ptr->zstream.avail_out = (sizeof tmpbuf);
  3523. }
  3524. /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
  3525. * process. If the LZ stream is truncated the sequential reader will
  3526. * terminally damage the stream, above, by reading the chunk header of the
  3527. * following chunk (it then exits with png_error).
  3528. *
  3529. * TODO: deal more elegantly with truncated IDAT lists.
  3530. */
  3531. ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
  3532. /* Take the unconsumed output back. */
  3533. if (output != NULL)
  3534. avail_out += png_ptr->zstream.avail_out;
  3535. else /* avail_out counts the extra bytes */
  3536. avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
  3537. png_ptr->zstream.avail_out = 0;
  3538. if (ret == Z_STREAM_END)
  3539. {
  3540. /* Do this for safety; we won't read any more into this row. */
  3541. png_ptr->zstream.next_out = NULL;
  3542. png_ptr->mode |= PNG_AFTER_IDAT;
  3543. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  3544. if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
  3545. png_chunk_benign_error(png_ptr, "Extra compressed data");
  3546. break;
  3547. }
  3548. if (ret != Z_OK)
  3549. {
  3550. png_zstream_error(png_ptr, ret);
  3551. if (output != NULL)
  3552. png_chunk_error(png_ptr, png_ptr->zstream.msg);
  3553. else /* checking */
  3554. {
  3555. png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
  3556. return;
  3557. }
  3558. }
  3559. } while (avail_out > 0);
  3560. if (avail_out > 0)
  3561. {
  3562. /* The stream ended before the image; this is the same as too few IDATs so
  3563. * should be handled the same way.
  3564. */
  3565. if (output != NULL)
  3566. png_error(png_ptr, "Not enough image data");
  3567. else /* the deflate stream contained extra data */
  3568. png_chunk_benign_error(png_ptr, "Too much image data");
  3569. }
  3570. }
  3571. void /* PRIVATE */
  3572. png_read_finish_IDAT(png_structrp png_ptr)
  3573. {
  3574. /* We don't need any more data and the stream should have ended, however the
  3575. * LZ end code may actually not have been processed. In this case we must
  3576. * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
  3577. * may still remain to be consumed.
  3578. */
  3579. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  3580. {
  3581. /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
  3582. * the compressed stream, but the stream may be damaged too, so even after
  3583. * this call we may need to terminate the zstream ownership.
  3584. */
  3585. png_read_IDAT_data(png_ptr, NULL, 0);
  3586. png_ptr->zstream.next_out = NULL; /* safety */
  3587. /* Now clear everything out for safety; the following may not have been
  3588. * done.
  3589. */
  3590. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  3591. {
  3592. png_ptr->mode |= PNG_AFTER_IDAT;
  3593. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  3594. }
  3595. }
  3596. /* If the zstream has not been released do it now *and* terminate the reading
  3597. * of the final IDAT chunk.
  3598. */
  3599. if (png_ptr->zowner == png_IDAT)
  3600. {
  3601. /* Always do this; the pointers otherwise point into the read buffer. */
  3602. png_ptr->zstream.next_in = NULL;
  3603. png_ptr->zstream.avail_in = 0;
  3604. /* Now we no longer own the zstream. */
  3605. png_ptr->zowner = 0;
  3606. /* The slightly weird semantics of the sequential IDAT reading is that we
  3607. * are always in or at the end of an IDAT chunk, so we always need to do a
  3608. * crc_finish here. If idat_size is non-zero we also need to read the
  3609. * spurious bytes at the end of the chunk now.
  3610. */
  3611. (void)png_crc_finish(png_ptr, png_ptr->idat_size);
  3612. }
  3613. }
  3614. void /* PRIVATE */
  3615. png_read_finish_row(png_structrp png_ptr)
  3616. {
  3617. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3618. /* Start of interlace block */
  3619. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3620. /* Offset to next interlace block */
  3621. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3622. /* Start of interlace block in the y direction */
  3623. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3624. /* Offset to next interlace block in the y direction */
  3625. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3626. png_debug(1, "in png_read_finish_row");
  3627. png_ptr->row_number++;
  3628. if (png_ptr->row_number < png_ptr->num_rows)
  3629. return;
  3630. if (png_ptr->interlaced != 0)
  3631. {
  3632. png_ptr->row_number = 0;
  3633. /* TO DO: don't do this if prev_row isn't needed (requires
  3634. * read-ahead of the next row's filter byte.
  3635. */
  3636. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3637. do
  3638. {
  3639. png_ptr->pass++;
  3640. if (png_ptr->pass >= 7)
  3641. break;
  3642. png_ptr->iwidth = (png_ptr->width +
  3643. png_pass_inc[png_ptr->pass] - 1 -
  3644. png_pass_start[png_ptr->pass]) /
  3645. png_pass_inc[png_ptr->pass];
  3646. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  3647. {
  3648. png_ptr->num_rows = (png_ptr->height +
  3649. png_pass_yinc[png_ptr->pass] - 1 -
  3650. png_pass_ystart[png_ptr->pass]) /
  3651. png_pass_yinc[png_ptr->pass];
  3652. }
  3653. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  3654. break; /* libpng deinterlacing sees every row */
  3655. } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
  3656. if (png_ptr->pass < 7)
  3657. return;
  3658. }
  3659. /* Here after at the end of the last row of the last pass. */
  3660. png_read_finish_IDAT(png_ptr);
  3661. }
  3662. #endif /* SEQUENTIAL_READ */
  3663. void /* PRIVATE */
  3664. png_read_start_row(png_structrp png_ptr)
  3665. {
  3666. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3667. /* Start of interlace block */
  3668. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3669. /* Offset to next interlace block */
  3670. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3671. /* Start of interlace block in the y direction */
  3672. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3673. /* Offset to next interlace block in the y direction */
  3674. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3675. unsigned int max_pixel_depth;
  3676. size_t row_bytes;
  3677. png_debug(1, "in png_read_start_row");
  3678. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  3679. png_init_read_transformations(png_ptr);
  3680. #endif
  3681. if (png_ptr->interlaced != 0)
  3682. {
  3683. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  3684. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  3685. png_pass_ystart[0]) / png_pass_yinc[0];
  3686. else
  3687. png_ptr->num_rows = png_ptr->height;
  3688. png_ptr->iwidth = (png_ptr->width +
  3689. png_pass_inc[png_ptr->pass] - 1 -
  3690. png_pass_start[png_ptr->pass]) /
  3691. png_pass_inc[png_ptr->pass];
  3692. }
  3693. else
  3694. {
  3695. png_ptr->num_rows = png_ptr->height;
  3696. png_ptr->iwidth = png_ptr->width;
  3697. }
  3698. max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
  3699. /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
  3700. * calculations to calculate the final pixel depth, then
  3701. * png_do_read_transforms actually does the transforms. This means that the
  3702. * code which effectively calculates this value is actually repeated in three
  3703. * separate places. They must all match. Innocent changes to the order of
  3704. * transformations can and will break libpng in a way that causes memory
  3705. * overwrites.
  3706. *
  3707. * TODO: fix this.
  3708. */
  3709. #ifdef PNG_READ_PACK_SUPPORTED
  3710. if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
  3711. max_pixel_depth = 8;
  3712. #endif
  3713. #ifdef PNG_READ_EXPAND_SUPPORTED
  3714. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  3715. {
  3716. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3717. {
  3718. if (png_ptr->num_trans != 0)
  3719. max_pixel_depth = 32;
  3720. else
  3721. max_pixel_depth = 24;
  3722. }
  3723. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3724. {
  3725. if (max_pixel_depth < 8)
  3726. max_pixel_depth = 8;
  3727. if (png_ptr->num_trans != 0)
  3728. max_pixel_depth *= 2;
  3729. }
  3730. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  3731. {
  3732. if (png_ptr->num_trans != 0)
  3733. {
  3734. max_pixel_depth *= 4;
  3735. max_pixel_depth /= 3;
  3736. }
  3737. }
  3738. }
  3739. #endif
  3740. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  3741. if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
  3742. {
  3743. # ifdef PNG_READ_EXPAND_SUPPORTED
  3744. /* In fact it is an error if it isn't supported, but checking is
  3745. * the safe way.
  3746. */
  3747. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  3748. {
  3749. if (png_ptr->bit_depth < 16)
  3750. max_pixel_depth *= 2;
  3751. }
  3752. else
  3753. # endif
  3754. png_ptr->transformations &= ~PNG_EXPAND_16;
  3755. }
  3756. #endif
  3757. #ifdef PNG_READ_FILLER_SUPPORTED
  3758. if ((png_ptr->transformations & (PNG_FILLER)) != 0)
  3759. {
  3760. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3761. {
  3762. if (max_pixel_depth <= 8)
  3763. max_pixel_depth = 16;
  3764. else
  3765. max_pixel_depth = 32;
  3766. }
  3767. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  3768. png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3769. {
  3770. if (max_pixel_depth <= 32)
  3771. max_pixel_depth = 32;
  3772. else
  3773. max_pixel_depth = 64;
  3774. }
  3775. }
  3776. #endif
  3777. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  3778. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
  3779. {
  3780. if (
  3781. #ifdef PNG_READ_EXPAND_SUPPORTED
  3782. (png_ptr->num_trans != 0 &&
  3783. (png_ptr->transformations & PNG_EXPAND) != 0) ||
  3784. #endif
  3785. #ifdef PNG_READ_FILLER_SUPPORTED
  3786. (png_ptr->transformations & (PNG_FILLER)) != 0 ||
  3787. #endif
  3788. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  3789. {
  3790. if (max_pixel_depth <= 16)
  3791. max_pixel_depth = 32;
  3792. else
  3793. max_pixel_depth = 64;
  3794. }
  3795. else
  3796. {
  3797. if (max_pixel_depth <= 8)
  3798. {
  3799. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3800. max_pixel_depth = 32;
  3801. else
  3802. max_pixel_depth = 24;
  3803. }
  3804. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3805. max_pixel_depth = 64;
  3806. else
  3807. max_pixel_depth = 48;
  3808. }
  3809. }
  3810. #endif
  3811. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  3812. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  3813. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  3814. {
  3815. unsigned int user_pixel_depth = png_ptr->user_transform_depth *
  3816. png_ptr->user_transform_channels;
  3817. if (user_pixel_depth > max_pixel_depth)
  3818. max_pixel_depth = user_pixel_depth;
  3819. }
  3820. #endif
  3821. /* This value is stored in png_struct and double checked in the row read
  3822. * code.
  3823. */
  3824. png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
  3825. png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
  3826. /* Align the width on the next larger 8 pixels. Mainly used
  3827. * for interlacing
  3828. */
  3829. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  3830. /* Calculate the maximum bytes needed, adding a byte and a pixel
  3831. * for safety's sake
  3832. */
  3833. row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
  3834. 1 + ((max_pixel_depth + 7) >> 3U);
  3835. #ifdef PNG_MAX_MALLOC_64K
  3836. if (row_bytes > (png_uint_32)65536L)
  3837. png_error(png_ptr, "This image requires a row greater than 64KB");
  3838. #endif
  3839. if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
  3840. {
  3841. png_free(png_ptr, png_ptr->big_row_buf);
  3842. png_free(png_ptr, png_ptr->big_prev_row);
  3843. if (png_ptr->interlaced != 0)
  3844. png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
  3845. row_bytes + 48);
  3846. else
  3847. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  3848. png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  3849. #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
  3850. /* Use 16-byte aligned memory for row_buf with at least 16 bytes
  3851. * of padding before and after row_buf; treat prev_row similarly.
  3852. * NOTE: the alignment is to the start of the pixels, one beyond the start
  3853. * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
  3854. * was incorrect; the filter byte was aligned, which had the exact
  3855. * opposite effect of that intended.
  3856. */
  3857. {
  3858. png_bytep temp = png_ptr->big_row_buf + 32;
  3859. int extra = (int)((temp - (png_bytep)0) & 0x0f);
  3860. png_ptr->row_buf = temp - extra - 1/*filter byte*/;
  3861. temp = png_ptr->big_prev_row + 32;
  3862. extra = (int)((temp - (png_bytep)0) & 0x0f);
  3863. png_ptr->prev_row = temp - extra - 1/*filter byte*/;
  3864. }
  3865. #else
  3866. /* Use 31 bytes of padding before and 17 bytes after row_buf. */
  3867. png_ptr->row_buf = png_ptr->big_row_buf + 31;
  3868. png_ptr->prev_row = png_ptr->big_prev_row + 31;
  3869. #endif
  3870. png_ptr->old_big_row_buf_size = row_bytes + 48;
  3871. }
  3872. #ifdef PNG_MAX_MALLOC_64K
  3873. if (png_ptr->rowbytes > 65535)
  3874. png_error(png_ptr, "This image requires a row greater than 64KB");
  3875. #endif
  3876. if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
  3877. png_error(png_ptr, "Row has too many bytes to allocate in memory");
  3878. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3879. png_debug1(3, "width = %u,", png_ptr->width);
  3880. png_debug1(3, "height = %u,", png_ptr->height);
  3881. png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
  3882. png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
  3883. png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
  3884. png_debug1(3, "irowbytes = %lu",
  3885. (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
  3886. /* The sequential reader needs a buffer for IDAT, but the progressive reader
  3887. * does not, so free the read buffer now regardless; the sequential reader
  3888. * reallocates it on demand.
  3889. */
  3890. if (png_ptr->read_buffer != NULL)
  3891. {
  3892. png_bytep buffer = png_ptr->read_buffer;
  3893. png_ptr->read_buffer_size = 0;
  3894. png_ptr->read_buffer = NULL;
  3895. png_free(png_ptr, buffer);
  3896. }
  3897. /* Finally claim the zstream for the inflate of the IDAT data, use the bits
  3898. * value from the stream (note that this will result in a fatal error if the
  3899. * IDAT stream has a bogus deflate header window_bits value, but this should
  3900. * not be happening any longer!)
  3901. */
  3902. if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
  3903. png_error(png_ptr, png_ptr->zstream.msg);
  3904. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  3905. }
  3906. #endif /* READ */