pngrutil.c 144 KB

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