pngrutil.c 144 KB

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