pngrutil.c 144 KB

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