pngrutil.c 153 KB

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